xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision eb633035)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2247cb52daSJeff Bonwick  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23663207adSDon Brady  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
270a055120SJason King  * Copyright 2017 Joyent, Inc.
28663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
29f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens /*
33fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
34fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * The overall design of the ztest program is as follows:
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
39fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
40fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
41fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
44fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
45fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
46fa9e4066Sahrens  *
47fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
48fa9e4066Sahrens  *     verify that self-healing data really works.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
51fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
52fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
55fa9e4066Sahrens  *     we run the entire test in a child of the main process.
56fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
57fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
58fa9e4066Sahrens  *     The parent then runs the test again, using the existing
595d7b4d43SMatthew Ahrens  *     storage pool, as many times as desired. If backwards compatibility
60420dfc95SChris Siden  *     testing is enabled ztest will sometimes run the "older" version
61420dfc95SChris Siden  *     of ztest after a SIGKILL.
62fa9e4066Sahrens  *
63fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
64fa9e4066Sahrens  *     many of the functional tests record the transaction group number
65fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
66fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
67fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
68fa9e4066Sahrens  *
69fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
70fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
71fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
72fa9e4066Sahrens  *
73fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
74fa9e4066Sahrens  *
75fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
76fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
77fa9e4066Sahrens  *
78420dfc95SChris Siden  * Use the -k option to set the desired frequency of kills.
79420dfc95SChris Siden  *
80420dfc95SChris Siden  * When ztest invokes itself it passes all relevant information through a
81420dfc95SChris Siden  * temporary file which is mmap-ed in the child process. This allows shared
82420dfc95SChris Siden  * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
83420dfc95SChris Siden  * stored at offset 0 of this file and contains information on the size and
84420dfc95SChris Siden  * number of shared structures in the file. The information stored in this file
85420dfc95SChris Siden  * must remain backwards compatible with older versions of ztest so that
86420dfc95SChris Siden  * ztest can invoke them during backwards compatibility testing (-B).
87fa9e4066Sahrens  */
88fa9e4066Sahrens 
89fa9e4066Sahrens #include <sys/zfs_context.h>
90fa9e4066Sahrens #include <sys/spa.h>
91fa9e4066Sahrens #include <sys/dmu.h>
92fa9e4066Sahrens #include <sys/txg.h>
932fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
94fa9e4066Sahrens #include <sys/zap.h>
95fa9e4066Sahrens #include <sys/dmu_objset.h>
96fa9e4066Sahrens #include <sys/poll.h>
97fa9e4066Sahrens #include <sys/stat.h>
98fa9e4066Sahrens #include <sys/time.h>
99fa9e4066Sahrens #include <sys/wait.h>
100fa9e4066Sahrens #include <sys/mman.h>
101fa9e4066Sahrens #include <sys/resource.h>
102fa9e4066Sahrens #include <sys/zio.h>
103fa9e4066Sahrens #include <sys/zil.h>
104b24ab676SJeff Bonwick #include <sys/zil_impl.h>
105fa9e4066Sahrens #include <sys/vdev_impl.h>
106e14bb325SJeff Bonwick #include <sys/vdev_file.h>
107094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
108fa9e4066Sahrens #include <sys/spa_impl.h>
10988ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
110fa9e4066Sahrens #include <sys/dsl_prop.h>
1114f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1123b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1133f9d6ad7SLin Ling #include <sys/dsl_scan.h>
114cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
115fa9e4066Sahrens #include <sys/refcount.h>
116ad135b5dSChristopher Siden #include <sys/zfeature.h>
1173b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
118770499e1SDan Kimmel #include <sys/abd.h>
119fa9e4066Sahrens #include <stdio.h>
120004388ebScasper #include <stdio_ext.h>
121fa9e4066Sahrens #include <stdlib.h>
122fa9e4066Sahrens #include <unistd.h>
123fa9e4066Sahrens #include <signal.h>
124fa9e4066Sahrens #include <umem.h>
125fa9e4066Sahrens #include <dlfcn.h>
126fa9e4066Sahrens #include <ctype.h>
127fa9e4066Sahrens #include <math.h>
128fa9e4066Sahrens #include <sys/fs/zfs.h>
129b24ab676SJeff Bonwick #include <libnvpair.h>
130e0f1c0afSOlaf Faaland #include <libzfs.h>
1310a055120SJason King #include <libcmdutils.h>
132fa9e4066Sahrens 
133741652b0SEtienne Dechamps static int ztest_fd_data = -1;
134741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
135420dfc95SChris Siden 
136420dfc95SChris Siden typedef struct ztest_shared_hdr {
137420dfc95SChris Siden 	uint64_t	zh_hdr_size;
138420dfc95SChris Siden 	uint64_t	zh_opts_size;
139420dfc95SChris Siden 	uint64_t	zh_size;
140420dfc95SChris Siden 	uint64_t	zh_stats_size;
141420dfc95SChris Siden 	uint64_t	zh_stats_count;
142420dfc95SChris Siden 	uint64_t	zh_ds_size;
143420dfc95SChris Siden 	uint64_t	zh_ds_count;
144420dfc95SChris Siden } ztest_shared_hdr_t;
145420dfc95SChris Siden 
146420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
147420dfc95SChris Siden 
148663207adSDon Brady enum ztest_class_state {
149663207adSDon Brady 	ZTEST_VDEV_CLASS_OFF,
150663207adSDon Brady 	ZTEST_VDEV_CLASS_ON,
151663207adSDon Brady 	ZTEST_VDEV_CLASS_RND
152663207adSDon Brady };
153663207adSDon Brady 
154420dfc95SChris Siden typedef struct ztest_shared_opts {
1559adfa60dSMatthew Ahrens 	char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
1569adfa60dSMatthew Ahrens 	char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
157420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
158420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
159420dfc95SChris Siden 	uint64_t zo_vdevs;
160420dfc95SChris Siden 	uint64_t zo_vdevtime;
161420dfc95SChris Siden 	size_t zo_vdev_size;
162420dfc95SChris Siden 	int zo_ashift;
163420dfc95SChris Siden 	int zo_mirrors;
164420dfc95SChris Siden 	int zo_raidz;
165420dfc95SChris Siden 	int zo_raidz_parity;
166420dfc95SChris Siden 	int zo_datasets;
167420dfc95SChris Siden 	int zo_threads;
168420dfc95SChris Siden 	uint64_t zo_passtime;
169420dfc95SChris Siden 	uint64_t zo_killrate;
170420dfc95SChris Siden 	int zo_verbose;
171420dfc95SChris Siden 	int zo_init;
172420dfc95SChris Siden 	uint64_t zo_time;
173420dfc95SChris Siden 	uint64_t zo_maxloops;
174243952c7SMatt Ahrens 	uint64_t zo_metaslab_force_ganging;
175e0f1c0afSOlaf Faaland 	int zo_mmp_test;
176663207adSDon Brady 	int zo_special_vdevs;
177420dfc95SChris Siden } ztest_shared_opts_t;
178420dfc95SChris Siden 
179420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
180420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
181420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
182420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
183420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
184420dfc95SChris Siden 	.zo_vdevs = 5,
185420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
186420dfc95SChris Siden 	.zo_mirrors = 2,
187420dfc95SChris Siden 	.zo_raidz = 4,
188420dfc95SChris Siden 	.zo_raidz_parity = 1,
1898363e80aSGeorge Wilson 	.zo_vdev_size = SPA_MINDEVSIZE * 4,	/* 256m default size */
190420dfc95SChris Siden 	.zo_datasets = 7,
191420dfc95SChris Siden 	.zo_threads = 23,
192420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
193420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
194420dfc95SChris Siden 	.zo_verbose = 0,
195e0f1c0afSOlaf Faaland 	.zo_mmp_test = 0,
196420dfc95SChris Siden 	.zo_init = 1,
197420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
198420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
199663207adSDon Brady 	.zo_metaslab_force_ganging = 32 << 10,
200663207adSDon Brady 	.zo_special_vdevs = ZTEST_VDEV_CLASS_RND,
201420dfc95SChris Siden };
202420dfc95SChris Siden 
203243952c7SMatt Ahrens extern uint64_t metaslab_force_ganging;
204420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
20569962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
20630beaff4SGeorge Wilson extern int metaslab_preload_limit;
207dcbf3bd6SGeorge Wilson extern boolean_t zfs_compressed_arc_enabled;
208770499e1SDan Kimmel extern boolean_t zfs_abd_scatter_enabled;
20954811da5SToomas Soome extern int dmu_object_alloc_chunk_shift;
21017f11284SSerapheim Dimitropoulos extern boolean_t zfs_force_some_double_word_sm_entries;
211a21fe349SBrian Behlendorf extern unsigned long zfs_reconstruct_indirect_damage_fraction;
212420dfc95SChris Siden 
213420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
214420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
215*eb633035STom Caputi static char *ztest_wkeydata = "abcdefghijklmnopqrstuvwxyz012345";
216420dfc95SChris Siden 
217420dfc95SChris Siden typedef struct ztest_shared_ds {
218420dfc95SChris Siden 	uint64_t	zd_seq;
219420dfc95SChris Siden } ztest_shared_ds_t;
220420dfc95SChris Siden 
221420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
222420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
223fa9e4066Sahrens 
224b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
225420dfc95SChris Siden #define	MAXFAULTS() \
226420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
227b24ab676SJeff Bonwick 
228b24ab676SJeff Bonwick enum ztest_io_type {
229b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
230b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
231b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
232b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
233b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
23480901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
235b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
236b24ab676SJeff Bonwick };
237b24ab676SJeff Bonwick 
238e05725b1Sbonwick typedef struct ztest_block_tag {
239b24ab676SJeff Bonwick 	uint64_t	bt_magic;
240e05725b1Sbonwick 	uint64_t	bt_objset;
241e05725b1Sbonwick 	uint64_t	bt_object;
24254811da5SToomas Soome 	uint64_t	bt_dnodesize;
243e05725b1Sbonwick 	uint64_t	bt_offset;
244b24ab676SJeff Bonwick 	uint64_t	bt_gen;
245e05725b1Sbonwick 	uint64_t	bt_txg;
246b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
247e05725b1Sbonwick } ztest_block_tag_t;
248e05725b1Sbonwick 
249b24ab676SJeff Bonwick typedef struct bufwad {
250b24ab676SJeff Bonwick 	uint64_t	bw_index;
251b24ab676SJeff Bonwick 	uint64_t	bw_txg;
252b24ab676SJeff Bonwick 	uint64_t	bw_data;
253b24ab676SJeff Bonwick } bufwad_t;
254b24ab676SJeff Bonwick 
255b24ab676SJeff Bonwick /*
25679315247SMatthew Ahrens  * It would be better to use a rangelock_t per object.  Unfortunately
25779315247SMatthew Ahrens  * the rangelock_t is not a drop-in replacement for rl_t, because we
25879315247SMatthew Ahrens  * still need to map from object ID to rangelock_t.
259b24ab676SJeff Bonwick  */
260b24ab676SJeff Bonwick typedef enum {
261b24ab676SJeff Bonwick 	RL_READER,
262b24ab676SJeff Bonwick 	RL_WRITER,
263b24ab676SJeff Bonwick 	RL_APPEND
264b24ab676SJeff Bonwick } rl_type_t;
265b24ab676SJeff Bonwick 
266b24ab676SJeff Bonwick typedef struct rll {
267b24ab676SJeff Bonwick 	void		*rll_writer;
268b24ab676SJeff Bonwick 	int		rll_readers;
269f06dce2cSAndrew Stormont 	kmutex_t	rll_lock;
270f06dce2cSAndrew Stormont 	kcondvar_t	rll_cv;
271b24ab676SJeff Bonwick } rll_t;
272b24ab676SJeff Bonwick 
273b24ab676SJeff Bonwick typedef struct rl {
274b24ab676SJeff Bonwick 	uint64_t	rl_object;
275b24ab676SJeff Bonwick 	uint64_t	rl_offset;
276b24ab676SJeff Bonwick 	uint64_t	rl_size;
277b24ab676SJeff Bonwick 	rll_t		*rl_lock;
278b24ab676SJeff Bonwick } rl_t;
279b24ab676SJeff Bonwick 
280b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
281b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
282b24ab676SJeff Bonwick 
283b24ab676SJeff Bonwick /*
284b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
285b24ab676SJeff Bonwick  */
286b24ab676SJeff Bonwick typedef struct ztest_od {
287b24ab676SJeff Bonwick 	uint64_t	od_dir;
288b24ab676SJeff Bonwick 	uint64_t	od_object;
289b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
290b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
291b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
292b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
29354811da5SToomas Soome 	uint64_t	od_crdnodesize;
294b24ab676SJeff Bonwick 	uint64_t	od_gen;
295b24ab676SJeff Bonwick 	uint64_t	od_crgen;
2969adfa60dSMatthew Ahrens 	char		od_name[ZFS_MAX_DATASET_NAME_LEN];
297b24ab676SJeff Bonwick } ztest_od_t;
298fa9e4066Sahrens 
299b24ab676SJeff Bonwick /*
300b24ab676SJeff Bonwick  * Per-dataset state.
301b24ab676SJeff Bonwick  */
302b24ab676SJeff Bonwick typedef struct ztest_ds {
303420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
304b24ab676SJeff Bonwick 	objset_t	*zd_os;
305f06dce2cSAndrew Stormont 	krwlock_t	zd_zilog_lock;
306b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
307b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
3089adfa60dSMatthew Ahrens 	char		zd_name[ZFS_MAX_DATASET_NAME_LEN];
309f06dce2cSAndrew Stormont 	kmutex_t	zd_dirobj_lock;
310b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
311b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
312b24ab676SJeff Bonwick } ztest_ds_t;
313b24ab676SJeff Bonwick 
314b24ab676SJeff Bonwick /*
315b24ab676SJeff Bonwick  * Per-iteration state.
316b24ab676SJeff Bonwick  */
317b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
318b24ab676SJeff Bonwick 
319b24ab676SJeff Bonwick typedef struct ztest_info {
320b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
321b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
322b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
323b24ab676SJeff Bonwick } ztest_info_t;
324fa9e4066Sahrens 
325420dfc95SChris Siden typedef struct ztest_shared_callstate {
326420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
327420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
328420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
329420dfc95SChris Siden } ztest_shared_callstate_t;
330420dfc95SChris Siden 
331420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
332420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
333420dfc95SChris Siden 
334fa9e4066Sahrens /*
335fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
336fa9e4066Sahrens  */
337fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
338fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
339fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
34054811da5SToomas Soome ztest_func_t ztest_dmu_object_next_chunk;
341d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
342fa9e4066Sahrens ztest_func_t ztest_zap;
343fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
344b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
345c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
346b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
347fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
348b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
349b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
350fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
351b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
352b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
353fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
354fa9e4066Sahrens ztest_func_t ztest_fault_inject;
355b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
356b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
357e0f1c0afSOlaf Faaland ztest_func_t ztest_mmp_enable_disable;
358b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
359b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
360fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
361fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
362fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
363663207adSDon Brady ztest_func_t ztest_vdev_class_add;
364e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3651195e687SMark J Musante ztest_func_t ztest_split_pool;
366e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
36725345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
3685cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_device_removal;
3695cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_remap_blocks;
37086714001SSerapheim Dimitropoulos ztest_func_t ztest_spa_checkpoint_create_discard;
371094e47e9SGeorge Wilson ztest_func_t ztest_initialize;
37254811da5SToomas Soome ztest_func_t ztest_verify_dnode_bt;
373fa9e4066Sahrens 
374b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
375b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
376b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
377b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
378b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
379fa9e4066Sahrens 
380fa9e4066Sahrens ztest_info_t ztest_info[] = {
381e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
382b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
383e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
38454811da5SToomas Soome 	{ ztest_dmu_object_next_chunk,		1,	&zopt_sometimes	},
385b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
386e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
387e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3881195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
389b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
390c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
391b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
392b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
393b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
394b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
395b24ab676SJeff Bonwick #if 0
396b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
397b24ab676SJeff Bonwick #endif
398b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
399b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
400b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
401a3874b8bSToomas Soome 	{ ztest_fault_inject,			1,	&zopt_incessant	},
402b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
4035c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
404e0f1c0afSOlaf Faaland 	{ ztest_mmp_enable_disable,		1,	&zopt_sometimes	},
4052c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
406a3874b8bSToomas Soome 	{ ztest_scrub,				1,	&zopt_often	},
40725345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
408b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
409a3874b8bSToomas Soome 	{ ztest_vdev_attach_detach,		1,	&zopt_incessant	},
410e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
411420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
412420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
413663207adSDon Brady 	{ ztest_vdev_class_add,			1,
414663207adSDon Brady 	    &ztest_opts.zo_vdevtime				},
415420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
416420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
4175cabbc6bSPrashanth Sreenivasa 	{ ztest_device_removal,			1,	&zopt_sometimes	},
41886714001SSerapheim Dimitropoulos 	{ ztest_remap_blocks,			1,	&zopt_sometimes },
419094e47e9SGeorge Wilson 	{ ztest_spa_checkpoint_create_discard,	1,	&zopt_rarely	},
42054811da5SToomas Soome 	{ ztest_initialize,			1,	&zopt_sometimes },
42154811da5SToomas Soome 	{ ztest_verify_dnode_bt,		1,	&zopt_sometimes }
422fa9e4066Sahrens };
423fa9e4066Sahrens 
424fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
425fa9e4066Sahrens 
426d20e665cSRicardo M. Correia /*
427d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
428d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
429d20e665cSRicardo M. Correia  */
430d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
431f06dce2cSAndrew Stormont 	kmutex_t zcl_callbacks_lock;
432d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
433d20e665cSRicardo M. Correia } ztest_cb_list_t;
434d20e665cSRicardo M. Correia 
435fa9e4066Sahrens /*
436fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
437fa9e4066Sahrens  */
438fa9e4066Sahrens typedef struct ztest_shared {
439420dfc95SChris Siden 	boolean_t	zs_do_init;
440b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
441b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
442b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
443b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
444b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
445b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
44688ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
447e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
448fa9e4066Sahrens 	uint64_t	zs_alloc;
449fa9e4066Sahrens 	uint64_t	zs_space;
4501195e687SMark J Musante 	uint64_t	zs_splits;
4511195e687SMark J Musante 	uint64_t	zs_mirrors;
452420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
453420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
454420dfc95SChris Siden 	uint64_t	zs_guid;
455fa9e4066Sahrens } ztest_shared_t;
456fa9e4066Sahrens 
457b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
458b24ab676SJeff Bonwick 
459fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
460e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
461b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
462fa9e4066Sahrens 
463420dfc95SChris Siden static spa_t *ztest_spa = NULL;
464420dfc95SChris Siden static ztest_ds_t *ztest_ds;
465420dfc95SChris Siden 
466f06dce2cSAndrew Stormont static kmutex_t ztest_vdev_lock;
4673a4b1be9SMatthew Ahrens static boolean_t ztest_device_removal_active = B_FALSE;
46854811da5SToomas Soome static kmutex_t ztest_checkpoint_lock;
469dfbb9432SGeorge Wilson 
470dfbb9432SGeorge Wilson /*
471dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
472dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
473dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
474dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
475dfbb9432SGeorge Wilson  */
476f06dce2cSAndrew Stormont static krwlock_t ztest_name_lock;
477fa9e4066Sahrens 
478420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
479e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4800a4e9518Sgw 
481d20e665cSRicardo M. Correia /* Global commit callback list */
482d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
483d20e665cSRicardo M. Correia 
484b24ab676SJeff Bonwick enum ztest_object {
485b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
486b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
487b24ab676SJeff Bonwick 	ZTEST_OBJECTS
488b24ab676SJeff Bonwick };
489fa9e4066Sahrens 
4901ce825d8Sraf static void usage(boolean_t) __NORETURN;
491f1b4288bSvb 
492fa9e4066Sahrens /*
493fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
494fa9e4066Sahrens  * debugging facilities.
495fa9e4066Sahrens  */
496fa9e4066Sahrens const char *
497fa9e4066Sahrens _umem_debug_init()
498fa9e4066Sahrens {
499fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
500fa9e4066Sahrens }
501fa9e4066Sahrens 
502fa9e4066Sahrens const char *
503fa9e4066Sahrens _umem_logging_init(void)
504fa9e4066Sahrens {
505fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
506fa9e4066Sahrens }
507fa9e4066Sahrens 
508fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
509fa9e4066Sahrens 
510fa9e4066Sahrens char *fatal_msg;
511fa9e4066Sahrens 
512fa9e4066Sahrens static void
513fa9e4066Sahrens fatal(int do_perror, char *message, ...)
514fa9e4066Sahrens {
515fa9e4066Sahrens 	va_list args;
516fa9e4066Sahrens 	int save_errno = errno;
517fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
518fa9e4066Sahrens 
519fa9e4066Sahrens 	(void) fflush(stdout);
520fa9e4066Sahrens 
521fa9e4066Sahrens 	va_start(args, message);
522fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
523fa9e4066Sahrens 	/* LINTED */
524fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
525fa9e4066Sahrens 	va_end(args);
526fa9e4066Sahrens 	if (do_perror) {
527fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
528fa9e4066Sahrens 		    ": %s", strerror(save_errno));
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
531fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
532fa9e4066Sahrens 	if (ztest_dump_core)
533fa9e4066Sahrens 		abort();
534fa9e4066Sahrens 	exit(3);
535fa9e4066Sahrens }
536fa9e4066Sahrens 
537fa9e4066Sahrens static int
538fa9e4066Sahrens str2shift(const char *buf)
539fa9e4066Sahrens {
540fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
541fa9e4066Sahrens 	int i;
542fa9e4066Sahrens 
543fa9e4066Sahrens 	if (buf[0] == '\0')
544fa9e4066Sahrens 		return (0);
545fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
546fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
547fa9e4066Sahrens 			break;
548fa9e4066Sahrens 	}
549f1b4288bSvb 	if (i == strlen(ends)) {
550f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
551f1b4288bSvb 		    buf);
552f1b4288bSvb 		usage(B_FALSE);
553f1b4288bSvb 	}
554fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
555fa9e4066Sahrens 		return (10*i);
556fa9e4066Sahrens 	}
557f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
558f1b4288bSvb 	usage(B_FALSE);
559f1b4288bSvb 	/* NOTREACHED */
560fa9e4066Sahrens }
561fa9e4066Sahrens 
562fa9e4066Sahrens static uint64_t
563fa9e4066Sahrens nicenumtoull(const char *buf)
564fa9e4066Sahrens {
565fa9e4066Sahrens 	char *end;
566fa9e4066Sahrens 	uint64_t val;
567fa9e4066Sahrens 
568fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
569fa9e4066Sahrens 	if (end == buf) {
570f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
571f1b4288bSvb 		usage(B_FALSE);
572fa9e4066Sahrens 	} else if (end[0] == '.') {
573fa9e4066Sahrens 		double fval = strtod(buf, &end);
574fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
575f1b4288bSvb 		if (fval > UINT64_MAX) {
576f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
577f1b4288bSvb 			    buf);
578f1b4288bSvb 			usage(B_FALSE);
579f1b4288bSvb 		}
580fa9e4066Sahrens 		val = (uint64_t)fval;
581fa9e4066Sahrens 	} else {
582fa9e4066Sahrens 		int shift = str2shift(end);
583f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
584f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
585f1b4288bSvb 			    buf);
586f1b4288bSvb 			usage(B_FALSE);
587f1b4288bSvb 		}
588fa9e4066Sahrens 		val <<= shift;
589fa9e4066Sahrens 	}
590fa9e4066Sahrens 	return (val);
591fa9e4066Sahrens }
592fa9e4066Sahrens 
593fa9e4066Sahrens static void
594f1b4288bSvb usage(boolean_t requested)
595fa9e4066Sahrens {
596420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
597420dfc95SChris Siden 
5980a055120SJason King 	char nice_vdev_size[NN_NUMBUF_SZ];
599243952c7SMatt Ahrens 	char nice_force_ganging[NN_NUMBUF_SZ];
600f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
601fa9e4066Sahrens 
6020a055120SJason King 	nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
603243952c7SMatt Ahrens 	nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
604243952c7SMatt Ahrens 	    sizeof (nice_force_ganging));
605fa9e4066Sahrens 
606f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
607fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
608fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
6092215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
610fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
611fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
61299653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
613fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
614fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
615fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
6162215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
6172215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
618fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
6192215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
620e0f1c0afSOlaf Faaland 	    "\t[-M] Multi-host simulate pool imported on remote host\n"
6212215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
6222215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
6232215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
6242215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
6252215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
626420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
627663207adSDon Brady 	    "\t[-C vdev class state (default: random)] special=on|off|random\n"
6280e60744cSPavel Zakharov 	    "\t[-o variable=value] ... set global variable to an unsigned\n"
6290e60744cSPavel Zakharov 	    "\t    32-bit integer value\n"
630f1b4288bSvb 	    "\t[-h] (print help)\n"
631fa9e4066Sahrens 	    "",
632420dfc95SChris Siden 	    zo->zo_pool,
633420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
6340a4e9518Sgw 	    nice_vdev_size,				/* -s */
635420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
636420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
637420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
638420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
639420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
640420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
641243952c7SMatt Ahrens 	    nice_force_ganging,				/* -g */
642420dfc95SChris Siden 	    zo->zo_init,				/* -i */
643420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
644420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
645420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
646420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
647420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
648420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
649f1b4288bSvb 	exit(requested ? 0 : 1);
650fa9e4066Sahrens }
651fa9e4066Sahrens 
652663207adSDon Brady 
653663207adSDon Brady static void
654663207adSDon Brady ztest_parse_name_value(const char *input, ztest_shared_opts_t *zo)
655663207adSDon Brady {
656663207adSDon Brady 	char name[32];
657663207adSDon Brady 	char *value;
658663207adSDon Brady 	int state = ZTEST_VDEV_CLASS_RND;
659663207adSDon Brady 
660663207adSDon Brady 	(void) strlcpy(name, input, sizeof (name));
661663207adSDon Brady 
662663207adSDon Brady 	value = strchr(name, '=');
663663207adSDon Brady 	if (value == NULL) {
664663207adSDon Brady 		(void) fprintf(stderr, "missing value in property=value "
665663207adSDon Brady 		    "'-C' argument (%s)\n", input);
666663207adSDon Brady 		usage(B_FALSE);
667663207adSDon Brady 	}
668663207adSDon Brady 	*(value) = '\0';
669663207adSDon Brady 	value++;
670663207adSDon Brady 
671663207adSDon Brady 	if (strcmp(value, "on") == 0) {
672663207adSDon Brady 		state = ZTEST_VDEV_CLASS_ON;
673663207adSDon Brady 	} else if (strcmp(value, "off") == 0) {
674663207adSDon Brady 		state = ZTEST_VDEV_CLASS_OFF;
675663207adSDon Brady 	} else if (strcmp(value, "random") == 0) {
676663207adSDon Brady 		state = ZTEST_VDEV_CLASS_RND;
677663207adSDon Brady 	} else {
678663207adSDon Brady 		(void) fprintf(stderr, "invalid property value '%s'\n", value);
679663207adSDon Brady 		usage(B_FALSE);
680663207adSDon Brady 	}
681663207adSDon Brady 
682663207adSDon Brady 	if (strcmp(name, "special") == 0) {
683663207adSDon Brady 		zo->zo_special_vdevs = state;
684663207adSDon Brady 	} else {
685663207adSDon Brady 		(void) fprintf(stderr, "invalid property name '%s'\n", name);
686663207adSDon Brady 		usage(B_FALSE);
687663207adSDon Brady 	}
688663207adSDon Brady 	if (zo->zo_verbose >= 3)
689663207adSDon Brady 		(void) printf("%s vdev state is '%s'\n", name, value);
690663207adSDon Brady }
691663207adSDon Brady 
692fa9e4066Sahrens static void
693fa9e4066Sahrens process_options(int argc, char **argv)
694fa9e4066Sahrens {
695420dfc95SChris Siden 	char *path;
696420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
697420dfc95SChris Siden 
698fa9e4066Sahrens 	int opt;
699fa9e4066Sahrens 	uint64_t value;
700420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
701fa9e4066Sahrens 
702420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
703fa9e4066Sahrens 
704fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
705663207adSDon Brady 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:")) != EOF) {
706fa9e4066Sahrens 		value = 0;
707fa9e4066Sahrens 		switch (opt) {
7083d7072f8Seschrock 		case 'v':
7093d7072f8Seschrock 		case 's':
7103d7072f8Seschrock 		case 'a':
7113d7072f8Seschrock 		case 'm':
7123d7072f8Seschrock 		case 'r':
7133d7072f8Seschrock 		case 'R':
7143d7072f8Seschrock 		case 'd':
7153d7072f8Seschrock 		case 't':
7163d7072f8Seschrock 		case 'g':
7173d7072f8Seschrock 		case 'i':
7183d7072f8Seschrock 		case 'k':
7193d7072f8Seschrock 		case 'T':
7203d7072f8Seschrock 		case 'P':
7212215e990SMark J Musante 		case 'F':
722fa9e4066Sahrens 			value = nicenumtoull(optarg);
723fa9e4066Sahrens 		}
724fa9e4066Sahrens 		switch (opt) {
7253d7072f8Seschrock 		case 'v':
726420dfc95SChris Siden 			zo->zo_vdevs = value;
727fa9e4066Sahrens 			break;
7283d7072f8Seschrock 		case 's':
729420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
730fa9e4066Sahrens 			break;
7313d7072f8Seschrock 		case 'a':
732420dfc95SChris Siden 			zo->zo_ashift = value;
733ecc2d604Sbonwick 			break;
7343d7072f8Seschrock 		case 'm':
735420dfc95SChris Siden 			zo->zo_mirrors = value;
736fa9e4066Sahrens 			break;
7373d7072f8Seschrock 		case 'r':
738420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
739fa9e4066Sahrens 			break;
7403d7072f8Seschrock 		case 'R':
741420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
74299653d4eSeschrock 			break;
7433d7072f8Seschrock 		case 'd':
744420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
745fa9e4066Sahrens 			break;
7463d7072f8Seschrock 		case 't':
747420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
748fa9e4066Sahrens 			break;
7493d7072f8Seschrock 		case 'g':
750243952c7SMatt Ahrens 			zo->zo_metaslab_force_ganging =
751243952c7SMatt Ahrens 			    MAX(SPA_MINBLOCKSIZE << 1, value);
752fa9e4066Sahrens 			break;
7533d7072f8Seschrock 		case 'i':
754420dfc95SChris Siden 			zo->zo_init = value;
755fa9e4066Sahrens 			break;
7563d7072f8Seschrock 		case 'k':
757420dfc95SChris Siden 			zo->zo_killrate = value;
758fa9e4066Sahrens 			break;
7593d7072f8Seschrock 		case 'p':
760420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
761420dfc95SChris Siden 			    sizeof (zo->zo_pool));
762fa9e4066Sahrens 			break;
7633d7072f8Seschrock 		case 'f':
764420dfc95SChris Siden 			path = realpath(optarg, NULL);
765420dfc95SChris Siden 			if (path == NULL) {
766420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
767420dfc95SChris Siden 				    optarg, strerror(errno));
768420dfc95SChris Siden 				usage(B_FALSE);
769420dfc95SChris Siden 			} else {
770420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
771420dfc95SChris Siden 				    sizeof (zo->zo_dir));
772420dfc95SChris Siden 			}
773fa9e4066Sahrens 			break;
774e0f1c0afSOlaf Faaland 		case 'M':
775e0f1c0afSOlaf Faaland 			zo->zo_mmp_test = 1;
776e0f1c0afSOlaf Faaland 			break;
7773d7072f8Seschrock 		case 'V':
778420dfc95SChris Siden 			zo->zo_verbose++;
779fa9e4066Sahrens 			break;
7803d7072f8Seschrock 		case 'E':
781420dfc95SChris Siden 			zo->zo_init = 0;
782fa9e4066Sahrens 			break;
7833d7072f8Seschrock 		case 'T':
784420dfc95SChris Siden 			zo->zo_time = value;
785fa9e4066Sahrens 			break;
7863d7072f8Seschrock 		case 'P':
787420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
788fa9e4066Sahrens 			break;
7892215e990SMark J Musante 		case 'F':
790420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
791420dfc95SChris Siden 			break;
792420dfc95SChris Siden 		case 'B':
793420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7942215e990SMark J Musante 			break;
795663207adSDon Brady 		case 'C':
796663207adSDon Brady 			ztest_parse_name_value(optarg, zo);
797663207adSDon Brady 			break;
7980e60744cSPavel Zakharov 		case 'o':
7990e60744cSPavel Zakharov 			if (set_global_var(optarg) != 0)
8000e60744cSPavel Zakharov 				usage(B_FALSE);
8010e60744cSPavel Zakharov 			break;
8023d7072f8Seschrock 		case 'h':
803f1b4288bSvb 			usage(B_TRUE);
804f1b4288bSvb 			break;
8053d7072f8Seschrock 		case '?':
8063d7072f8Seschrock 		default:
807f1b4288bSvb 			usage(B_FALSE);
808fa9e4066Sahrens 			break;
809fa9e4066Sahrens 		}
810fa9e4066Sahrens 	}
811fa9e4066Sahrens 
812420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
81399653d4eSeschrock 
814420dfc95SChris Siden 	zo->zo_vdevtime =
815420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
816b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
817420dfc95SChris Siden 
818420dfc95SChris Siden 	if (strlen(altdir) > 0) {
819741652b0SEtienne Dechamps 		char *cmd;
820741652b0SEtienne Dechamps 		char *realaltdir;
821420dfc95SChris Siden 		char *bin;
822420dfc95SChris Siden 		char *ztest;
823420dfc95SChris Siden 		char *isa;
824420dfc95SChris Siden 		int isalen;
825420dfc95SChris Siden 
826741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
827741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
828741652b0SEtienne Dechamps 
829741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
830420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
831420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
832420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
833420dfc95SChris Siden 			    altdir);
834420dfc95SChris Siden 		}
835420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
836420dfc95SChris Siden 
837420dfc95SChris Siden 		/*
838420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
839420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
840420dfc95SChris Siden 		 * 32 or 64 bit binaries.
841420dfc95SChris Siden 		 */
842420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
843420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
844420dfc95SChris Siden 		isa = bin + 9;
845420dfc95SChris Siden 		isalen = ztest - isa;
846420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
847420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
848420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
849420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
850420dfc95SChris Siden 
851420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
852420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
853420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
854420dfc95SChris Siden 			    zo->zo_alt_ztest);
855420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
856420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
857420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
858420dfc95SChris Siden 			    zo->zo_alt_libpath);
859420dfc95SChris Siden 		}
860741652b0SEtienne Dechamps 
861741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
862741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
863420dfc95SChris Siden 	}
864fa9e4066Sahrens }
865fa9e4066Sahrens 
866b24ab676SJeff Bonwick static void
867b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
868b24ab676SJeff Bonwick {
869420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
870420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
871b4952e17SGeorge Wilson 
872b4952e17SGeorge Wilson 	/*
873b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
8745cabbc6bSPrashanth Sreenivasa 	 * See comment above spa_write_cachefile().
875b4952e17SGeorge Wilson 	 */
876b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
8775cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
878b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
879b4952e17SGeorge Wilson 
880b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
881b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
882b24ab676SJeff Bonwick }
883b24ab676SJeff Bonwick 
884b24ab676SJeff Bonwick static uint64_t
885b24ab676SJeff Bonwick ztest_random(uint64_t range)
886b24ab676SJeff Bonwick {
887b24ab676SJeff Bonwick 	uint64_t r;
888b24ab676SJeff Bonwick 
889741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
890741652b0SEtienne Dechamps 
891b24ab676SJeff Bonwick 	if (range == 0)
892b24ab676SJeff Bonwick 		return (0);
893b24ab676SJeff Bonwick 
894741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
895b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
896b24ab676SJeff Bonwick 
897b24ab676SJeff Bonwick 	return (r % range);
898b24ab676SJeff Bonwick }
899b24ab676SJeff Bonwick 
900b24ab676SJeff Bonwick /* ARGSUSED */
901b24ab676SJeff Bonwick static void
902b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
903b24ab676SJeff Bonwick {
904b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
905b24ab676SJeff Bonwick }
906b24ab676SJeff Bonwick 
907ecc2d604Sbonwick static uint64_t
908ecc2d604Sbonwick ztest_get_ashift(void)
909ecc2d604Sbonwick {
910420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
9112a104a52SAlex Reece 		return (SPA_MINBLOCKSHIFT + ztest_random(5));
912420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
913ecc2d604Sbonwick }
914ecc2d604Sbonwick 
915fa9e4066Sahrens static nvlist_t *
91625345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
917fa9e4066Sahrens {
918e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
919fa9e4066Sahrens 	uint64_t vdev;
920fa9e4066Sahrens 	nvlist_t *file;
921fa9e4066Sahrens 
922e14bb325SJeff Bonwick 	if (ashift == 0)
923e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
924e14bb325SJeff Bonwick 
925e14bb325SJeff Bonwick 	if (path == NULL) {
926e14bb325SJeff Bonwick 		path = pathbuf;
927fa9e4066Sahrens 
928e14bb325SJeff Bonwick 		if (aux != NULL) {
929e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
930420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
931420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
93225345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
93325345e46SGeorge Wilson 			    aux, vdev);
934e14bb325SJeff Bonwick 		} else {
93588ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
936420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
937420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
93825345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
939e14bb325SJeff Bonwick 		}
940e14bb325SJeff Bonwick 	}
941e14bb325SJeff Bonwick 
942e14bb325SJeff Bonwick 	if (size != 0) {
943e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
944fa9e4066Sahrens 		if (fd == -1)
945e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
946fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
947e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
948fa9e4066Sahrens 		(void) close(fd);
949fa9e4066Sahrens 	}
950fa9e4066Sahrens 
951fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
952fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
953e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
954ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
955fa9e4066Sahrens 
956fa9e4066Sahrens 	return (file);
957fa9e4066Sahrens }
958fa9e4066Sahrens 
959fa9e4066Sahrens static nvlist_t *
96025345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
96125345e46SGeorge Wilson     uint64_t ashift, int r)
962fa9e4066Sahrens {
963fa9e4066Sahrens 	nvlist_t *raidz, **child;
964fa9e4066Sahrens 	int c;
965fa9e4066Sahrens 
966fa9e4066Sahrens 	if (r < 2)
96725345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
968fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
969fa9e4066Sahrens 
970fa9e4066Sahrens 	for (c = 0; c < r; c++)
97125345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
972fa9e4066Sahrens 
973fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
974fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
975fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
97699653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
977420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
978fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
979fa9e4066Sahrens 	    child, r) == 0);
980fa9e4066Sahrens 
981fa9e4066Sahrens 	for (c = 0; c < r; c++)
982fa9e4066Sahrens 		nvlist_free(child[c]);
983fa9e4066Sahrens 
984fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
985fa9e4066Sahrens 
986fa9e4066Sahrens 	return (raidz);
987fa9e4066Sahrens }
988fa9e4066Sahrens 
989fa9e4066Sahrens static nvlist_t *
99025345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
99125345e46SGeorge Wilson     uint64_t ashift, int r, int m)
992fa9e4066Sahrens {
993fa9e4066Sahrens 	nvlist_t *mirror, **child;
994fa9e4066Sahrens 	int c;
995fa9e4066Sahrens 
996fa9e4066Sahrens 	if (m < 1)
99725345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
998fa9e4066Sahrens 
999fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
1000fa9e4066Sahrens 
1001fa9e4066Sahrens 	for (c = 0; c < m; c++)
100225345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1005fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1006fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
1007fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1008fa9e4066Sahrens 	    child, m) == 0);
1009fa9e4066Sahrens 
1010fa9e4066Sahrens 	for (c = 0; c < m; c++)
1011fa9e4066Sahrens 		nvlist_free(child[c]);
1012fa9e4066Sahrens 
1013fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
1014fa9e4066Sahrens 
1015fa9e4066Sahrens 	return (mirror);
1016fa9e4066Sahrens }
1017fa9e4066Sahrens 
1018fa9e4066Sahrens static nvlist_t *
101925345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1020663207adSDon Brady     const char *class, int r, int m, int t)
1021fa9e4066Sahrens {
1022fa9e4066Sahrens 	nvlist_t *root, **child;
1023fa9e4066Sahrens 	int c;
1024663207adSDon Brady 	boolean_t log;
1025fa9e4066Sahrens 
1026fa9e4066Sahrens 	ASSERT(t > 0);
1027fa9e4066Sahrens 
1028663207adSDon Brady 	log = (class != NULL && strcmp(class, "log") == 0);
1029663207adSDon Brady 
1030fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1031fa9e4066Sahrens 
103231157203SJeff Bonwick 	for (c = 0; c < t; c++) {
103325345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
103425345e46SGeorge Wilson 		    r, m);
103531157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
103631157203SJeff Bonwick 		    log) == 0);
1037663207adSDon Brady 
1038663207adSDon Brady 		if (class != NULL && class[0] != '\0') {
1039663207adSDon Brady 			ASSERT(m > 1 || log);   /* expecting a mirror */
1040663207adSDon Brady 			VERIFY(nvlist_add_string(child[c],
1041663207adSDon Brady 			    ZPOOL_CONFIG_ALLOCATION_BIAS, class) == 0);
1042663207adSDon Brady 		}
104331157203SJeff Bonwick 	}
1044fa9e4066Sahrens 
1045fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1046fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1047e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1048fa9e4066Sahrens 	    child, t) == 0);
1049fa9e4066Sahrens 
1050fa9e4066Sahrens 	for (c = 0; c < t; c++)
1051fa9e4066Sahrens 		nvlist_free(child[c]);
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
1054fa9e4066Sahrens 
1055fa9e4066Sahrens 	return (root);
1056fa9e4066Sahrens }
1057fa9e4066Sahrens 
105825345e46SGeorge Wilson /*
105925345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
106025345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
106125345e46SGeorge Wilson  */
106225345e46SGeorge Wilson static uint64_t
106325345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
106425345e46SGeorge Wilson {
106525345e46SGeorge Wilson 	uint64_t version = initial_version;
106625345e46SGeorge Wilson 
106725345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
106825345e46SGeorge Wilson 		version = version +
106925345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
107025345e46SGeorge Wilson 	}
107125345e46SGeorge Wilson 
107225345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
107325345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
107425345e46SGeorge Wilson 
107525345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
107625345e46SGeorge Wilson 	return (version);
107725345e46SGeorge Wilson }
107825345e46SGeorge Wilson 
1079b24ab676SJeff Bonwick static int
1080b24ab676SJeff Bonwick ztest_random_blocksize(void)
1081fa9e4066Sahrens {
1082b5152584SMatthew Ahrens 	uint64_t block_shift;
1083663207adSDon Brady 
1084663207adSDon Brady 	ASSERT(ztest_spa->spa_max_ashift != 0);
1085663207adSDon Brady 
1086b5152584SMatthew Ahrens 	/*
1087b5152584SMatthew Ahrens 	 * Choose a block size >= the ashift.
1088b5152584SMatthew Ahrens 	 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1089b5152584SMatthew Ahrens 	 */
1090b5152584SMatthew Ahrens 	int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1091b5152584SMatthew Ahrens 	if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1092b5152584SMatthew Ahrens 		maxbs = 20;
109381cd5c55SMatthew Ahrens 	block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
10942a104a52SAlex Reece 	return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1095b24ab676SJeff Bonwick }
1096fa9e4066Sahrens 
109754811da5SToomas Soome static int
109854811da5SToomas Soome ztest_random_dnodesize(void)
109954811da5SToomas Soome {
110054811da5SToomas Soome 	int slots;
110154811da5SToomas Soome 	int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
110254811da5SToomas Soome 
110354811da5SToomas Soome 	if (max_slots == DNODE_MIN_SLOTS)
110454811da5SToomas Soome 		return (DNODE_MIN_SIZE);
110554811da5SToomas Soome 
110654811da5SToomas Soome 	/*
110754811da5SToomas Soome 	 * Weight the random distribution more heavily toward smaller
110854811da5SToomas Soome 	 * dnode sizes since that is more likely to reflect real-world
110954811da5SToomas Soome 	 * usage.
111054811da5SToomas Soome 	 */
111154811da5SToomas Soome 	ASSERT3U(max_slots, >, 4);
111254811da5SToomas Soome 	switch (ztest_random(10)) {
111354811da5SToomas Soome 	case 0:
111454811da5SToomas Soome 		slots = 5 + ztest_random(max_slots - 4);
111554811da5SToomas Soome 		break;
111654811da5SToomas Soome 	case 1 ... 4:
111754811da5SToomas Soome 		slots = 2 + ztest_random(3);
111854811da5SToomas Soome 		break;
111954811da5SToomas Soome 	default:
112054811da5SToomas Soome 		slots = 1;
112154811da5SToomas Soome 		break;
112254811da5SToomas Soome 	}
112354811da5SToomas Soome 
112454811da5SToomas Soome 	return (slots << DNODE_SHIFT);
112554811da5SToomas Soome }
112654811da5SToomas Soome 
1127b24ab676SJeff Bonwick static int
1128b24ab676SJeff Bonwick ztest_random_ibshift(void)
1129b24ab676SJeff Bonwick {
1130b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
1131b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1132fa9e4066Sahrens }
1133fa9e4066Sahrens 
1134b24ab676SJeff Bonwick static uint64_t
1135b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1136fa9e4066Sahrens {
1137b24ab676SJeff Bonwick 	uint64_t top;
1138b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
1139b24ab676SJeff Bonwick 	vdev_t *tvd;
1140fa9e4066Sahrens 
1141b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1142fa9e4066Sahrens 
1143b24ab676SJeff Bonwick 	do {
1144b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
1145b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
11465cabbc6bSPrashanth Sreenivasa 	} while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1147b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1148fa9e4066Sahrens 
1149b24ab676SJeff Bonwick 	return (top);
1150fa9e4066Sahrens }
1151fa9e4066Sahrens 
1152b24ab676SJeff Bonwick static uint64_t
1153b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1154fa9e4066Sahrens {
1155b24ab676SJeff Bonwick 	uint64_t value;
1156b24ab676SJeff Bonwick 
1157b24ab676SJeff Bonwick 	do {
1158b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1159b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1160b24ab676SJeff Bonwick 
1161b24ab676SJeff Bonwick 	return (value);
1162fa9e4066Sahrens }
1163fa9e4066Sahrens 
1164fa9e4066Sahrens static int
1165b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1166b24ab676SJeff Bonwick     boolean_t inherit)
1167fa9e4066Sahrens {
1168b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1169b24ab676SJeff Bonwick 	const char *valname;
1170b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1171b24ab676SJeff Bonwick 	uint64_t curval;
1172fa9e4066Sahrens 	int error;
1173fa9e4066Sahrens 
11743b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
11753b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1176fa9e4066Sahrens 
1177b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1178b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1179fa9e4066Sahrens 		return (error);
1180fa9e4066Sahrens 	}
1181fb09f5aaSMadhav Suresh 	ASSERT0(error);
1182fa9e4066Sahrens 
11833b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1184b24ab676SJeff Bonwick 
1185420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1186b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1187b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1188b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1189fa9e4066Sahrens 	}
1190fa9e4066Sahrens 
1191fa9e4066Sahrens 	return (error);
1192fa9e4066Sahrens }
1193fa9e4066Sahrens 
1194fa9e4066Sahrens static int
1195420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1196fa9e4066Sahrens {
1197420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1198b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1199fa9e4066Sahrens 	int error;
1200fa9e4066Sahrens 
1201b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1202b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1203fa9e4066Sahrens 
1204b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1205b24ab676SJeff Bonwick 
1206b24ab676SJeff Bonwick 	nvlist_free(props);
1207b24ab676SJeff Bonwick 
1208b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1209b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1210fa9e4066Sahrens 		return (error);
1211fa9e4066Sahrens 	}
1212fb09f5aaSMadhav Suresh 	ASSERT0(error);
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	return (error);
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
1217*eb633035STom Caputi static int
1218*eb633035STom Caputi ztest_dmu_objset_own(const char *name, dmu_objset_type_t type,
1219*eb633035STom Caputi     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
1220*eb633035STom Caputi {
1221*eb633035STom Caputi 	int err;
1222*eb633035STom Caputi 
1223*eb633035STom Caputi 	err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1224*eb633035STom Caputi 	if (decrypt && err == EACCES) {
1225*eb633035STom Caputi 		char ddname[ZFS_MAX_DATASET_NAME_LEN];
1226*eb633035STom Caputi 		dsl_crypto_params_t *dcp;
1227*eb633035STom Caputi 		nvlist_t *crypto_args = fnvlist_alloc();
1228*eb633035STom Caputi 		char *cp = NULL;
1229*eb633035STom Caputi 
1230*eb633035STom Caputi 		/* spa_keystore_load_wkey() expects a dsl dir name */
1231*eb633035STom Caputi 		(void) strcpy(ddname, name);
1232*eb633035STom Caputi 		cp = strchr(ddname, '@');
1233*eb633035STom Caputi 		if (cp != NULL)
1234*eb633035STom Caputi 			*cp = '\0';
1235*eb633035STom Caputi 
1236*eb633035STom Caputi 		fnvlist_add_uint8_array(crypto_args, "wkeydata",
1237*eb633035STom Caputi 		    (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
1238*eb633035STom Caputi 		VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
1239*eb633035STom Caputi 		    crypto_args, &dcp));
1240*eb633035STom Caputi 		err = spa_keystore_load_wkey(ddname, dcp, B_FALSE);
1241*eb633035STom Caputi 		dsl_crypto_params_free(dcp, B_FALSE);
1242*eb633035STom Caputi 		fnvlist_free(crypto_args);
1243*eb633035STom Caputi 
1244*eb633035STom Caputi 		if (err != 0)
1245*eb633035STom Caputi 			return (err);
1246*eb633035STom Caputi 
1247*eb633035STom Caputi 		err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1248*eb633035STom Caputi 	}
1249*eb633035STom Caputi 
1250*eb633035STom Caputi 	return (err);
1251*eb633035STom Caputi }
1252*eb633035STom Caputi 
1253b24ab676SJeff Bonwick static void
1254b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1255b24ab676SJeff Bonwick {
1256b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1257b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1258f06dce2cSAndrew Stormont 	mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1259f06dce2cSAndrew Stormont 	cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1260b24ab676SJeff Bonwick }
1261fa9e4066Sahrens 
1262b24ab676SJeff Bonwick static void
1263b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1264fa9e4066Sahrens {
1265b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1266b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1267f06dce2cSAndrew Stormont 	mutex_destroy(&rll->rll_lock);
1268f06dce2cSAndrew Stormont 	cv_destroy(&rll->rll_cv);
1269b24ab676SJeff Bonwick }
1270fa9e4066Sahrens 
1271b24ab676SJeff Bonwick static void
1272b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1273b24ab676SJeff Bonwick {
1274f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1275fa9e4066Sahrens 
1276b24ab676SJeff Bonwick 	if (type == RL_READER) {
1277b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1278f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1279b24ab676SJeff Bonwick 		rll->rll_readers++;
1280b24ab676SJeff Bonwick 	} else {
1281b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1282f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1283b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1284b24ab676SJeff Bonwick 	}
1285fa9e4066Sahrens 
1286f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1287b24ab676SJeff Bonwick }
1288fa9e4066Sahrens 
1289b24ab676SJeff Bonwick static void
1290b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1291b24ab676SJeff Bonwick {
1292f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1293fa9e4066Sahrens 
1294b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1295b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1296b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1297b24ab676SJeff Bonwick 	} else {
1298b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1299b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1300b24ab676SJeff Bonwick 		rll->rll_readers--;
1301b24ab676SJeff Bonwick 	}
1302fa9e4066Sahrens 
1303b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1304f06dce2cSAndrew Stormont 		cv_broadcast(&rll->rll_cv);
1305b24ab676SJeff Bonwick 
1306f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1307fa9e4066Sahrens }
1308fa9e4066Sahrens 
1309b24ab676SJeff Bonwick static void
1310b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
131131157203SJeff Bonwick {
1312b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
131331157203SJeff Bonwick 
1314b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1315b24ab676SJeff Bonwick }
131631157203SJeff Bonwick 
1317b24ab676SJeff Bonwick static void
1318b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1319b24ab676SJeff Bonwick {
1320b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
132131157203SJeff Bonwick 
1322b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
132331157203SJeff Bonwick }
132431157203SJeff Bonwick 
1325b24ab676SJeff Bonwick static rl_t *
1326b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1327b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
132888ecc943SGeorge Wilson {
1329b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1330b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1331b24ab676SJeff Bonwick 	rl_t *rl;
133288ecc943SGeorge Wilson 
1333b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1334b24ab676SJeff Bonwick 	rl->rl_object = object;
1335b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1336b24ab676SJeff Bonwick 	rl->rl_size = size;
1337b24ab676SJeff Bonwick 	rl->rl_lock = rll;
133888ecc943SGeorge Wilson 
1339b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
134088ecc943SGeorge Wilson 
1341b24ab676SJeff Bonwick 	return (rl);
134288ecc943SGeorge Wilson }
134388ecc943SGeorge Wilson 
1344b24ab676SJeff Bonwick static void
1345b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1346fa9e4066Sahrens {
1347b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1348fa9e4066Sahrens 
1349b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1350fa9e4066Sahrens 
1351b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1352b24ab676SJeff Bonwick }
1353fa9e4066Sahrens 
1354b24ab676SJeff Bonwick static void
1355420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1356b24ab676SJeff Bonwick {
1357b24ab676SJeff Bonwick 	zd->zd_os = os;
1358b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1359420dfc95SChris Siden 	zd->zd_shared = szd;
1360b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1361fa9e4066Sahrens 
1362420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1363420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1364420dfc95SChris Siden 
1365f06dce2cSAndrew Stormont 	rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1366f06dce2cSAndrew Stormont 	mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1367b24ab676SJeff Bonwick 
1368b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1369b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1370b24ab676SJeff Bonwick 
1371b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1372b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1373b24ab676SJeff Bonwick }
1374b24ab676SJeff Bonwick 
1375b24ab676SJeff Bonwick static void
1376b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1377b24ab676SJeff Bonwick {
1378f06dce2cSAndrew Stormont 	mutex_destroy(&zd->zd_dirobj_lock);
1379b24ab676SJeff Bonwick 
1380b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1381b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1382b24ab676SJeff Bonwick 
1383b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1384b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1385b24ab676SJeff Bonwick }
1386b24ab676SJeff Bonwick 
1387b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1388b24ab676SJeff Bonwick 
1389b24ab676SJeff Bonwick static uint64_t
1390b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1391b24ab676SJeff Bonwick {
1392b24ab676SJeff Bonwick 	uint64_t txg;
1393b24ab676SJeff Bonwick 	int error;
1394b24ab676SJeff Bonwick 
1395b24ab676SJeff Bonwick 	/*
1396b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1397b24ab676SJeff Bonwick 	 */
1398b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1399b24ab676SJeff Bonwick 	if (error) {
1400b24ab676SJeff Bonwick 		if (error == ERESTART) {
1401b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1402b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1403b24ab676SJeff Bonwick 		} else {
1404b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1405b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1406b24ab676SJeff Bonwick 		}
1407b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1408b24ab676SJeff Bonwick 		return (0);
1409b24ab676SJeff Bonwick 	}
1410b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1411b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1412b24ab676SJeff Bonwick 	return (txg);
1413b24ab676SJeff Bonwick }
1414b24ab676SJeff Bonwick 
1415b24ab676SJeff Bonwick static void
1416b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1417b24ab676SJeff Bonwick {
1418b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1419b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1420b24ab676SJeff Bonwick 
1421b24ab676SJeff Bonwick 	while (ip < ip_end)
1422b24ab676SJeff Bonwick 		*ip++ = value;
1423b24ab676SJeff Bonwick }
1424b24ab676SJeff Bonwick 
1425b24ab676SJeff Bonwick static boolean_t
1426b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1427b24ab676SJeff Bonwick {
1428b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1429b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1430b24ab676SJeff Bonwick 	uint64_t diff = 0;
1431b24ab676SJeff Bonwick 
1432b24ab676SJeff Bonwick 	while (ip < ip_end)
1433b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1434b24ab676SJeff Bonwick 
1435b24ab676SJeff Bonwick 	return (diff == 0);
1436b24ab676SJeff Bonwick }
1437b24ab676SJeff Bonwick 
1438b24ab676SJeff Bonwick static void
1439b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
144054811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
144154811da5SToomas Soome     uint64_t crtxg)
1442b24ab676SJeff Bonwick {
1443b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1444b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1445b24ab676SJeff Bonwick 	bt->bt_object = object;
144654811da5SToomas Soome 	bt->bt_dnodesize = dnodesize;
1447b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1448b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1449b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1450b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1451b24ab676SJeff Bonwick }
1452b24ab676SJeff Bonwick 
1453b24ab676SJeff Bonwick static void
1454b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
145554811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
145654811da5SToomas Soome     uint64_t crtxg)
1457b24ab676SJeff Bonwick {
14585d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
14595d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
14605d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
146154811da5SToomas Soome 	ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
14625d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
14635d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
14645d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
14655d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1466b24ab676SJeff Bonwick }
1467b24ab676SJeff Bonwick 
1468b24ab676SJeff Bonwick static ztest_block_tag_t *
1469b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1470b24ab676SJeff Bonwick {
1471b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1472b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1473b24ab676SJeff Bonwick 
1474b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1475b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1476b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1477b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1478b24ab676SJeff Bonwick 
1479b24ab676SJeff Bonwick 	return (bt);
1480b24ab676SJeff Bonwick }
1481b24ab676SJeff Bonwick 
148254811da5SToomas Soome /*
148354811da5SToomas Soome  * Generate a token to fill up unused bonus buffer space.  Try to make
148454811da5SToomas Soome  * it unique to the object, generation, and offset to verify that data
148554811da5SToomas Soome  * is not getting overwritten by data from other dnodes.
148654811da5SToomas Soome  */
148754811da5SToomas Soome #define	ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset)	\
148854811da5SToomas Soome 	(((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
148954811da5SToomas Soome 
149054811da5SToomas Soome /*
149154811da5SToomas Soome  * Fill up the unused bonus buffer region before the block tag with a
149254811da5SToomas Soome  * verifiable pattern. Filling the whole bonus area with non-zero data
149354811da5SToomas Soome  * helps ensure that all dnode traversal code properly skips the
149454811da5SToomas Soome  * interior regions of large dnodes.
149554811da5SToomas Soome  */
149654811da5SToomas Soome void
149754811da5SToomas Soome ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
149854811da5SToomas Soome     objset_t *os, uint64_t gen)
149954811da5SToomas Soome {
150054811da5SToomas Soome 	uint64_t *bonusp;
150154811da5SToomas Soome 
150254811da5SToomas Soome 	ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
150354811da5SToomas Soome 
150454811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
150554811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
150654811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
150754811da5SToomas Soome 		*bonusp = token;
150854811da5SToomas Soome 	}
150954811da5SToomas Soome }
151054811da5SToomas Soome 
151154811da5SToomas Soome /*
151254811da5SToomas Soome  * Verify that the unused area of a bonus buffer is filled with the
151354811da5SToomas Soome  * expected tokens.
151454811da5SToomas Soome  */
151554811da5SToomas Soome void
151654811da5SToomas Soome ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
151754811da5SToomas Soome     objset_t *os, uint64_t gen)
151854811da5SToomas Soome {
151954811da5SToomas Soome 	uint64_t *bonusp;
152054811da5SToomas Soome 
152154811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
152254811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
152354811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
152454811da5SToomas Soome 		VERIFY3U(*bonusp, ==, token);
152554811da5SToomas Soome 	}
152654811da5SToomas Soome }
152754811da5SToomas Soome 
1528b24ab676SJeff Bonwick /*
1529b24ab676SJeff Bonwick  * ZIL logging ops
1530b24ab676SJeff Bonwick  */
1531b24ab676SJeff Bonwick 
1532b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1533b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1534b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1535b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
153654811da5SToomas Soome #define	lrz_dnodesize	lr_crtime[1]
1537b24ab676SJeff Bonwick 
15385002558fSNeil Perrin static void
1539b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1540b24ab676SJeff Bonwick {
1541b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1542b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1543b24ab676SJeff Bonwick 	itx_t *itx;
1544b24ab676SJeff Bonwick 
1545b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15465002558fSNeil Perrin 		return;
1547b24ab676SJeff Bonwick 
1548b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1549b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1550b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1551b24ab676SJeff Bonwick 
15525002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1553b24ab676SJeff Bonwick }
1554b24ab676SJeff Bonwick 
15555002558fSNeil Perrin static void
15565002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1557b24ab676SJeff Bonwick {
1558b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1559b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1560b24ab676SJeff Bonwick 	itx_t *itx;
1561b24ab676SJeff Bonwick 
1562b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15635002558fSNeil Perrin 		return;
1564b24ab676SJeff Bonwick 
1565b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1566b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1567b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1568b24ab676SJeff Bonwick 
156951bd2f97SNeil Perrin 	itx->itx_oid = object;
15705002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1571b24ab676SJeff Bonwick }
1572b24ab676SJeff Bonwick 
15735002558fSNeil Perrin static void
1574b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1575b24ab676SJeff Bonwick {
1576b24ab676SJeff Bonwick 	itx_t *itx;
1577b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1578b24ab676SJeff Bonwick 
1579b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15805002558fSNeil Perrin 		return;
1581b24ab676SJeff Bonwick 
1582b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1583b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1584b24ab676SJeff Bonwick 
1585b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1586b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1587b24ab676SJeff Bonwick 
1588b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1589b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1590b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1591b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1592b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1593b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1594b24ab676SJeff Bonwick 	}
1595b24ab676SJeff Bonwick 	itx->itx_private = zd;
1596b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1597b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1598b24ab676SJeff Bonwick 
1599b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1600b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1601b24ab676SJeff Bonwick 
16025002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1603b24ab676SJeff Bonwick }
1604b24ab676SJeff Bonwick 
16055002558fSNeil Perrin static void
1606b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1607b24ab676SJeff Bonwick {
1608b24ab676SJeff Bonwick 	itx_t *itx;
1609b24ab676SJeff Bonwick 
1610b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
16115002558fSNeil Perrin 		return;
1612b24ab676SJeff Bonwick 
1613b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1614b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1615b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1616b24ab676SJeff Bonwick 
16175002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
16185002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1619b24ab676SJeff Bonwick }
1620b24ab676SJeff Bonwick 
16215002558fSNeil Perrin static void
1622b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1623b24ab676SJeff Bonwick {
1624b24ab676SJeff Bonwick 	itx_t *itx;
1625b24ab676SJeff Bonwick 
1626b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
16275002558fSNeil Perrin 		return;
1628b24ab676SJeff Bonwick 
1629b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1630b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1631b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1632b24ab676SJeff Bonwick 
16335002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
16345002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1635b24ab676SJeff Bonwick }
1636b24ab676SJeff Bonwick 
1637b24ab676SJeff Bonwick /*
1638b24ab676SJeff Bonwick  * ZIL replay ops
1639b24ab676SJeff Bonwick  */
1640b24ab676SJeff Bonwick static int
16413f7978d0SAlan Somers ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1642b24ab676SJeff Bonwick {
16433f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
16443f7978d0SAlan Somers 	lr_create_t *lr = arg2;
1645b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1646b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1647b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1648b24ab676SJeff Bonwick 	dmu_buf_t *db;
1649b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1650b24ab676SJeff Bonwick 	uint64_t txg;
1651b24ab676SJeff Bonwick 	int error = 0;
165254811da5SToomas Soome 	int bonuslen;
1653b24ab676SJeff Bonwick 
1654b24ab676SJeff Bonwick 	if (byteswap)
1655b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1656b24ab676SJeff Bonwick 
1657b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1658b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1659b24ab676SJeff Bonwick 
1660b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1661b24ab676SJeff Bonwick 
1662b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1663b24ab676SJeff Bonwick 
1664b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1665b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1666b24ab676SJeff Bonwick 	} else {
1667b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1668b24ab676SJeff Bonwick 	}
1669b24ab676SJeff Bonwick 
1670b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1671b24ab676SJeff Bonwick 	if (txg == 0)
1672b24ab676SJeff Bonwick 		return (ENOSPC);
1673b24ab676SJeff Bonwick 
1674b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
167554811da5SToomas Soome 	bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1676b24ab676SJeff Bonwick 
1677b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1678b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
167954811da5SToomas Soome 			lr->lr_foid = zap_create_dnsize(os,
1680b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
168154811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1682b24ab676SJeff Bonwick 		} else {
168354811da5SToomas Soome 			error = zap_create_claim_dnsize(os, lr->lr_foid,
1684b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
168554811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1686b24ab676SJeff Bonwick 		}
1687b24ab676SJeff Bonwick 	} else {
1688b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
168954811da5SToomas Soome 			lr->lr_foid = dmu_object_alloc_dnsize(os,
1690b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
169154811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1692b24ab676SJeff Bonwick 		} else {
169354811da5SToomas Soome 			error = dmu_object_claim_dnsize(os, lr->lr_foid,
1694b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
169554811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1696b24ab676SJeff Bonwick 		}
1697b24ab676SJeff Bonwick 	}
1698b24ab676SJeff Bonwick 
1699b24ab676SJeff Bonwick 	if (error) {
1700b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1701b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1702b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1703b24ab676SJeff Bonwick 		return (error);
1704b24ab676SJeff Bonwick 	}
1705b24ab676SJeff Bonwick 
1706b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1707b24ab676SJeff Bonwick 
1708b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1709b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1710b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1711b24ab676SJeff Bonwick 
1712b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1713b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1714b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
171554811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
171654811da5SToomas Soome 	    lr->lr_gen, txg, txg);
171754811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1718b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1719b24ab676SJeff Bonwick 
1720b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1721b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1722b24ab676SJeff Bonwick 
1723b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1724b24ab676SJeff Bonwick 
1725b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1726b24ab676SJeff Bonwick 
1727b24ab676SJeff Bonwick 	return (0);
1728b24ab676SJeff Bonwick }
1729b24ab676SJeff Bonwick 
1730b24ab676SJeff Bonwick static int
17313f7978d0SAlan Somers ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1732b24ab676SJeff Bonwick {
17333f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17343f7978d0SAlan Somers 	lr_remove_t *lr = arg2;
1735b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1736b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1737b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1738b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1739b24ab676SJeff Bonwick 	uint64_t object, txg;
1740b24ab676SJeff Bonwick 
1741b24ab676SJeff Bonwick 	if (byteswap)
1742b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1743b24ab676SJeff Bonwick 
1744b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1745b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1746b24ab676SJeff Bonwick 
1747b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1748b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1749b24ab676SJeff Bonwick 	ASSERT(object != 0);
1750b24ab676SJeff Bonwick 
1751b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1752b24ab676SJeff Bonwick 
1753b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1754b24ab676SJeff Bonwick 
1755b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1756b24ab676SJeff Bonwick 
1757b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1758b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1759b24ab676SJeff Bonwick 
1760b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1761b24ab676SJeff Bonwick 	if (txg == 0) {
1762b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1763b24ab676SJeff Bonwick 		return (ENOSPC);
1764b24ab676SJeff Bonwick 	}
1765b24ab676SJeff Bonwick 
1766b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1767b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1768b24ab676SJeff Bonwick 	} else {
1769b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1770b24ab676SJeff Bonwick 	}
1771b24ab676SJeff Bonwick 
1772b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1773b24ab676SJeff Bonwick 
17745002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1775b24ab676SJeff Bonwick 
1776b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1777b24ab676SJeff Bonwick 
1778b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1779b24ab676SJeff Bonwick 
1780b24ab676SJeff Bonwick 	return (0);
1781b24ab676SJeff Bonwick }
1782b24ab676SJeff Bonwick 
1783b24ab676SJeff Bonwick static int
17843f7978d0SAlan Somers ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1785b24ab676SJeff Bonwick {
17863f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17873f7978d0SAlan Somers 	lr_write_t *lr = arg2;
1788b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1789b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1790b24ab676SJeff Bonwick 	uint64_t offset, length;
1791b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1792b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1793b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1794b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1795b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1796b24ab676SJeff Bonwick 	dmu_buf_t *db;
1797b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1798b24ab676SJeff Bonwick 	rl_t *rl;
1799b24ab676SJeff Bonwick 
1800b24ab676SJeff Bonwick 	if (byteswap)
1801b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1802b24ab676SJeff Bonwick 
1803b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1804b24ab676SJeff Bonwick 	length = lr->lr_length;
1805b24ab676SJeff Bonwick 
1806b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1807b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1808b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1809b24ab676SJeff Bonwick 		if (length < blocksize) {
1810b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1811b24ab676SJeff Bonwick 			length = blocksize;
1812b24ab676SJeff Bonwick 		}
1813b24ab676SJeff Bonwick 	}
1814b24ab676SJeff Bonwick 
1815b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1816b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1817b24ab676SJeff Bonwick 
1818b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1819b24ab676SJeff Bonwick 		bt = NULL;
1820b24ab676SJeff Bonwick 
1821b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1822b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1823b24ab676SJeff Bonwick 
1824b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1825b24ab676SJeff Bonwick 
1826b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1827b24ab676SJeff Bonwick 
1828b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1829b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1830b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1831b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1832b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1833b24ab676SJeff Bonwick 
1834b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1835b24ab676SJeff Bonwick 
1836b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1837b24ab676SJeff Bonwick 
1838b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1839b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1840b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1841b24ab676SJeff Bonwick 
1842b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1843b24ab676SJeff Bonwick 	if (txg == 0) {
1844b24ab676SJeff Bonwick 		if (abuf != NULL)
1845b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1846b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1847b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1848b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1849b24ab676SJeff Bonwick 		return (ENOSPC);
1850b24ab676SJeff Bonwick 	}
1851b24ab676SJeff Bonwick 
1852b24ab676SJeff Bonwick 	if (bt != NULL) {
1853b24ab676SJeff Bonwick 		/*
1854b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1855b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1856b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1857b24ab676SJeff Bonwick 		 */
1858b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1859b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1860b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1861b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1862b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1863b24ab676SJeff Bonwick 
1864b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1865b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1866b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
186754811da5SToomas Soome 				ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1868b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1869b24ab676SJeff Bonwick 			}
1870b24ab676SJeff Bonwick 		}
1871b24ab676SJeff Bonwick 
1872b24ab676SJeff Bonwick 		/*
1873b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1874b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1875b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1876b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1877b24ab676SJeff Bonwick 		 */
1878b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
187954811da5SToomas Soome 			ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1880b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1881b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1882b24ab676SJeff Bonwick 		}
1883b24ab676SJeff Bonwick 
1884b24ab676SJeff Bonwick 		/*
1885b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1886b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1887b24ab676SJeff Bonwick 		 */
188854811da5SToomas Soome 		ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
188954811da5SToomas Soome 		    crtxg);
1890b24ab676SJeff Bonwick 	}
1891b24ab676SJeff Bonwick 
1892b24ab676SJeff Bonwick 	if (abuf == NULL) {
1893b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1894b24ab676SJeff Bonwick 	} else {
1895b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1896*eb633035STom Caputi 		dmu_assign_arcbuf_by_dbuf(db, offset, abuf, tx);
1897b24ab676SJeff Bonwick 	}
1898b24ab676SJeff Bonwick 
1899b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1900b24ab676SJeff Bonwick 
1901b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1902b24ab676SJeff Bonwick 
1903b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1904b24ab676SJeff Bonwick 
1905b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1906b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1907b24ab676SJeff Bonwick 
1908b24ab676SJeff Bonwick 	return (0);
1909b24ab676SJeff Bonwick }
1910b24ab676SJeff Bonwick 
1911b24ab676SJeff Bonwick static int
19123f7978d0SAlan Somers ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1913b24ab676SJeff Bonwick {
19143f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
19153f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
1916b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1917b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1918b24ab676SJeff Bonwick 	uint64_t txg;
1919b24ab676SJeff Bonwick 	rl_t *rl;
1920b24ab676SJeff Bonwick 
1921b24ab676SJeff Bonwick 	if (byteswap)
1922b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1923b24ab676SJeff Bonwick 
1924b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1925b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1926b24ab676SJeff Bonwick 	    RL_WRITER);
1927b24ab676SJeff Bonwick 
1928b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1929b24ab676SJeff Bonwick 
1930b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1931b24ab676SJeff Bonwick 
1932b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1933b24ab676SJeff Bonwick 	if (txg == 0) {
1934b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1935b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1936b24ab676SJeff Bonwick 		return (ENOSPC);
1937b24ab676SJeff Bonwick 	}
1938b24ab676SJeff Bonwick 
1939b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1940b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1941b24ab676SJeff Bonwick 
1942b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1943b24ab676SJeff Bonwick 
1944b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1945b24ab676SJeff Bonwick 
1946b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1947b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1948b24ab676SJeff Bonwick 
1949b24ab676SJeff Bonwick 	return (0);
1950b24ab676SJeff Bonwick }
1951b24ab676SJeff Bonwick 
1952b24ab676SJeff Bonwick static int
19533f7978d0SAlan Somers ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1954b24ab676SJeff Bonwick {
19553f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
19563f7978d0SAlan Somers 	lr_setattr_t *lr = arg2;
1957b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1958b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1959b24ab676SJeff Bonwick 	dmu_buf_t *db;
1960b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
196154811da5SToomas Soome 	uint64_t txg, lrtxg, crtxg, dnodesize;
1962b24ab676SJeff Bonwick 
1963b24ab676SJeff Bonwick 	if (byteswap)
1964b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1965b24ab676SJeff Bonwick 
1966b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1967b24ab676SJeff Bonwick 
1968b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1969b24ab676SJeff Bonwick 
1970b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1971b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1972b24ab676SJeff Bonwick 
1973b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1974b24ab676SJeff Bonwick 	if (txg == 0) {
1975b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1976b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1977b24ab676SJeff Bonwick 		return (ENOSPC);
1978b24ab676SJeff Bonwick 	}
1979b24ab676SJeff Bonwick 
1980b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1981b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1982b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1983b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
198454811da5SToomas Soome 	dnodesize = bbt->bt_dnodesize;
1985b24ab676SJeff Bonwick 
1986b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1987b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1988b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1989b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1990b24ab676SJeff Bonwick 	} else {
1991b24ab676SJeff Bonwick 		/*
1992b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1993b24ab676SJeff Bonwick 		 */
1994b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1995b24ab676SJeff Bonwick 		    sizeof (*bbt);
1996b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
1997b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
1998b24ab676SJeff Bonwick 	}
1999b24ab676SJeff Bonwick 
2000b24ab676SJeff Bonwick 	/*
2001b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
2002b24ab676SJeff Bonwick 	 */
200354811da5SToomas Soome 	ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2004b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
2005b24ab676SJeff Bonwick 
2006b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
2007b24ab676SJeff Bonwick 
2008b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2009b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
2010fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2011b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
2012b24ab676SJeff Bonwick 
201354811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
201454811da5SToomas Soome 	    txg, crtxg);
201554811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2016b24ab676SJeff Bonwick 
2017b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
2018b24ab676SJeff Bonwick 
2019b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
2020b24ab676SJeff Bonwick 
2021b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
2022b24ab676SJeff Bonwick 
2023b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
2024b24ab676SJeff Bonwick 
2025b24ab676SJeff Bonwick 	return (0);
2026b24ab676SJeff Bonwick }
2027b24ab676SJeff Bonwick 
2028b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
2029b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
2030b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
2031b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
2032b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
2033b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
2034b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
2035b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
2036b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
2037b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
2038b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
2039b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
2040b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
2041b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
2042b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
2043b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
2044b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
2045b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
2046b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
2047b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
2048b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
2049b24ab676SJeff Bonwick };
2050b24ab676SJeff Bonwick 
2051b24ab676SJeff Bonwick /*
2052b24ab676SJeff Bonwick  * ZIL get_data callbacks
2053b24ab676SJeff Bonwick  */
2054b24ab676SJeff Bonwick 
2055cab3a55eSPrakash Surya /* ARGSUSED */
2056b24ab676SJeff Bonwick static void
2057b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
2058b24ab676SJeff Bonwick {
2059b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
206079315247SMatthew Ahrens 	uint64_t object = ((rl_t *)zgd->zgd_lr)->rl_object;
2061b24ab676SJeff Bonwick 
2062b24ab676SJeff Bonwick 	if (zgd->zgd_db)
2063b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
2064b24ab676SJeff Bonwick 
206579315247SMatthew Ahrens 	ztest_range_unlock((rl_t *)zgd->zgd_lr);
2066b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2067b24ab676SJeff Bonwick 
2068b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
2069b24ab676SJeff Bonwick }
2070b24ab676SJeff Bonwick 
2071b24ab676SJeff Bonwick static int
20721271e4b1SPrakash Surya ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
20731271e4b1SPrakash Surya     zio_t *zio)
2074b24ab676SJeff Bonwick {
2075b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
2076b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2077b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
2078b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
2079b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
2080b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
2081b24ab676SJeff Bonwick 	uint64_t crtxg;
2082b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2083b24ab676SJeff Bonwick 	dmu_buf_t *db;
2084b24ab676SJeff Bonwick 	zgd_t *zgd;
2085b24ab676SJeff Bonwick 	int error;
2086b24ab676SJeff Bonwick 
20871271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
20881271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
20891271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
20901271e4b1SPrakash Surya 
2091b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2092b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
2093b24ab676SJeff Bonwick 	if (error) {
2094b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2095b24ab676SJeff Bonwick 		return (error);
2096b24ab676SJeff Bonwick 	}
2097b24ab676SJeff Bonwick 
2098b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
2099b24ab676SJeff Bonwick 
2100b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
2101b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
2102b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2103b24ab676SJeff Bonwick 		return (ENOENT);
2104b24ab676SJeff Bonwick 	}
2105b24ab676SJeff Bonwick 
2106b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
2107b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
2108b24ab676SJeff Bonwick 	db = NULL;
2109b24ab676SJeff Bonwick 
2110b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
21111271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
2112b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
2113b24ab676SJeff Bonwick 
2114b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
211579315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
211679315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2117b24ab676SJeff Bonwick 
2118b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
2119b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2120b24ab676SJeff Bonwick 		ASSERT(error == 0);
2121b24ab676SJeff Bonwick 	} else {
2122b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
2123dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
2124b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
2125dfe73b3dSJeff Bonwick 		} else {
2126dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
2127dfe73b3dSJeff Bonwick 			offset = 0;
2128dfe73b3dSJeff Bonwick 		}
2129b24ab676SJeff Bonwick 
213079315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
213179315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2132b24ab676SJeff Bonwick 
213347cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
213447cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2135b24ab676SJeff Bonwick 
2136b24ab676SJeff Bonwick 		if (error == 0) {
2137b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
213880901aeaSGeorge Wilson 
2139b24ab676SJeff Bonwick 			zgd->zgd_db = db;
2140b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
2141b24ab676SJeff Bonwick 
2142b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
2143b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
2144b24ab676SJeff Bonwick 
2145b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
2146b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
2147b24ab676SJeff Bonwick 
2148b24ab676SJeff Bonwick 			if (error == 0)
2149b24ab676SJeff Bonwick 				return (0);
2150b24ab676SJeff Bonwick 		}
2151b24ab676SJeff Bonwick 	}
2152b24ab676SJeff Bonwick 
2153b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
2154b24ab676SJeff Bonwick 
2155b24ab676SJeff Bonwick 	return (error);
2156b24ab676SJeff Bonwick }
2157b24ab676SJeff Bonwick 
2158b24ab676SJeff Bonwick static void *
2159b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
2160b24ab676SJeff Bonwick {
2161b24ab676SJeff Bonwick 	char *lr;
2162b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2163b24ab676SJeff Bonwick 
2164b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2165b24ab676SJeff Bonwick 
2166b24ab676SJeff Bonwick 	if (name)
2167b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
2168b24ab676SJeff Bonwick 
2169b24ab676SJeff Bonwick 	return (lr);
2170b24ab676SJeff Bonwick }
2171b24ab676SJeff Bonwick 
2172b24ab676SJeff Bonwick void
2173b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
2174b24ab676SJeff Bonwick {
2175b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2176b24ab676SJeff Bonwick 
2177b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
2178b24ab676SJeff Bonwick }
2179b24ab676SJeff Bonwick 
2180b24ab676SJeff Bonwick /*
2181b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
2182b24ab676SJeff Bonwick  */
2183b24ab676SJeff Bonwick static int
2184b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2185b24ab676SJeff Bonwick {
2186b24ab676SJeff Bonwick 	int missing = 0;
2187b24ab676SJeff Bonwick 	int error;
2188b24ab676SJeff Bonwick 
2189f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2190b24ab676SJeff Bonwick 
2191b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2192b24ab676SJeff Bonwick 		od->od_object = 0;
2193b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2194b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
2195b24ab676SJeff Bonwick 		if (error) {
2196b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
2197b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
2198b24ab676SJeff Bonwick 			missing++;
2199b24ab676SJeff Bonwick 		} else {
2200b24ab676SJeff Bonwick 			dmu_buf_t *db;
2201b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
2202b24ab676SJeff Bonwick 			dmu_object_info_t doi;
2203b24ab676SJeff Bonwick 
2204b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2205b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
2206b24ab676SJeff Bonwick 
2207b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
2208b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2209b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
2210b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
2211b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
2212b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2213b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
2214b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
2215b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
2216b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
2217b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
2218b24ab676SJeff Bonwick 		}
2219b24ab676SJeff Bonwick 	}
2220b24ab676SJeff Bonwick 
2221b24ab676SJeff Bonwick 	return (missing);
2222b24ab676SJeff Bonwick }
2223b24ab676SJeff Bonwick 
2224b24ab676SJeff Bonwick static int
2225b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2226b24ab676SJeff Bonwick {
2227b24ab676SJeff Bonwick 	int missing = 0;
2228b24ab676SJeff Bonwick 
2229f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2230b24ab676SJeff Bonwick 
2231b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2232b24ab676SJeff Bonwick 		if (missing) {
2233b24ab676SJeff Bonwick 			od->od_object = 0;
2234b24ab676SJeff Bonwick 			missing++;
2235b24ab676SJeff Bonwick 			continue;
2236b24ab676SJeff Bonwick 		}
2237b24ab676SJeff Bonwick 
2238b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2239b24ab676SJeff Bonwick 
2240b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2241b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
2242b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
2243b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
2244b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
2245b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
224654811da5SToomas Soome 		lr->lrz_dnodesize = od->od_crdnodesize;
2247b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
2248b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
2249b24ab676SJeff Bonwick 
2250b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2251b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2252b24ab676SJeff Bonwick 			od->od_object = 0;
2253b24ab676SJeff Bonwick 			missing++;
2254b24ab676SJeff Bonwick 		} else {
2255b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2256b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2257b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2258b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2259b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2260b24ab676SJeff Bonwick 		}
2261b24ab676SJeff Bonwick 
2262b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2263b24ab676SJeff Bonwick 	}
2264b24ab676SJeff Bonwick 
2265b24ab676SJeff Bonwick 	return (missing);
2266b24ab676SJeff Bonwick }
2267b24ab676SJeff Bonwick 
2268b24ab676SJeff Bonwick static int
2269b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2270b24ab676SJeff Bonwick {
2271b24ab676SJeff Bonwick 	int missing = 0;
2272b24ab676SJeff Bonwick 	int error;
2273b24ab676SJeff Bonwick 
2274f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2275b24ab676SJeff Bonwick 
2276b24ab676SJeff Bonwick 	od += count - 1;
2277b24ab676SJeff Bonwick 
2278b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2279b24ab676SJeff Bonwick 		if (missing) {
2280b24ab676SJeff Bonwick 			missing++;
2281b24ab676SJeff Bonwick 			continue;
2282b24ab676SJeff Bonwick 		}
2283b24ab676SJeff Bonwick 
228480901aeaSGeorge Wilson 		/*
228580901aeaSGeorge Wilson 		 * No object was found.
228680901aeaSGeorge Wilson 		 */
2287b24ab676SJeff Bonwick 		if (od->od_object == 0)
2288b24ab676SJeff Bonwick 			continue;
2289b24ab676SJeff Bonwick 
2290b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2291b24ab676SJeff Bonwick 
2292b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2293b24ab676SJeff Bonwick 
2294b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2295b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2296b24ab676SJeff Bonwick 			missing++;
2297b24ab676SJeff Bonwick 		} else {
2298b24ab676SJeff Bonwick 			od->od_object = 0;
2299b24ab676SJeff Bonwick 		}
2300b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2301b24ab676SJeff Bonwick 	}
2302b24ab676SJeff Bonwick 
2303b24ab676SJeff Bonwick 	return (missing);
2304b24ab676SJeff Bonwick }
2305b24ab676SJeff Bonwick 
2306b24ab676SJeff Bonwick static int
2307b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2308b24ab676SJeff Bonwick     void *data)
2309b24ab676SJeff Bonwick {
2310b24ab676SJeff Bonwick 	lr_write_t *lr;
2311b24ab676SJeff Bonwick 	int error;
2312b24ab676SJeff Bonwick 
2313b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2314b24ab676SJeff Bonwick 
2315b24ab676SJeff Bonwick 	lr->lr_foid = object;
2316b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2317b24ab676SJeff Bonwick 	lr->lr_length = size;
2318b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2319b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2320b24ab676SJeff Bonwick 
2321b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2322b24ab676SJeff Bonwick 
2323b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2324b24ab676SJeff Bonwick 
2325b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2326b24ab676SJeff Bonwick 
2327b24ab676SJeff Bonwick 	return (error);
2328b24ab676SJeff Bonwick }
2329b24ab676SJeff Bonwick 
2330b24ab676SJeff Bonwick static int
2331b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2332b24ab676SJeff Bonwick {
2333b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2334b24ab676SJeff Bonwick 	int error;
2335b24ab676SJeff Bonwick 
2336b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2337b24ab676SJeff Bonwick 
2338b24ab676SJeff Bonwick 	lr->lr_foid = object;
2339b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2340b24ab676SJeff Bonwick 	lr->lr_length = size;
2341b24ab676SJeff Bonwick 
2342b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2343b24ab676SJeff Bonwick 
2344b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2345b24ab676SJeff Bonwick 
2346b24ab676SJeff Bonwick 	return (error);
2347b24ab676SJeff Bonwick }
2348b24ab676SJeff Bonwick 
2349b24ab676SJeff Bonwick static int
2350b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2351b24ab676SJeff Bonwick {
2352b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2353b24ab676SJeff Bonwick 	int error;
2354b24ab676SJeff Bonwick 
2355b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2356b24ab676SJeff Bonwick 
2357b24ab676SJeff Bonwick 	lr->lr_foid = object;
2358b24ab676SJeff Bonwick 	lr->lr_size = 0;
2359b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2360b24ab676SJeff Bonwick 
2361b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2362b24ab676SJeff Bonwick 
2363b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2364b24ab676SJeff Bonwick 
2365b24ab676SJeff Bonwick 	return (error);
2366b24ab676SJeff Bonwick }
2367b24ab676SJeff Bonwick 
2368b24ab676SJeff Bonwick static void
2369b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2370b24ab676SJeff Bonwick {
2371b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2372b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2373b24ab676SJeff Bonwick 	uint64_t txg;
2374b24ab676SJeff Bonwick 	rl_t *rl;
2375b24ab676SJeff Bonwick 
2376b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2377b24ab676SJeff Bonwick 
2378b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2379b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2380b24ab676SJeff Bonwick 
2381b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2382b24ab676SJeff Bonwick 
2383b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2384b24ab676SJeff Bonwick 
2385b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2386b24ab676SJeff Bonwick 
2387b24ab676SJeff Bonwick 	if (txg != 0) {
2388b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2389b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2390b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2391b24ab676SJeff Bonwick 	} else {
2392b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2393b24ab676SJeff Bonwick 	}
2394b24ab676SJeff Bonwick 
2395b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2396b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2397b24ab676SJeff Bonwick }
2398b24ab676SJeff Bonwick 
2399b24ab676SJeff Bonwick static void
2400b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2401b24ab676SJeff Bonwick {
240280901aeaSGeorge Wilson 	int err;
2403b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2404b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2405b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2406b24ab676SJeff Bonwick 	uint64_t blocksize;
2407b24ab676SJeff Bonwick 	void *data;
2408b24ab676SJeff Bonwick 
2409b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2410b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2411b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2412b24ab676SJeff Bonwick 
2413b24ab676SJeff Bonwick 	/*
2414b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2415b24ab676SJeff Bonwick 	 */
2416b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2417b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2418b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2419b24ab676SJeff Bonwick 
2420f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2421c9ba2a43SEric Schrock 
2422b24ab676SJeff Bonwick 	switch (io_type) {
2423b24ab676SJeff Bonwick 
2424b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
242554811da5SToomas Soome 		ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
242654811da5SToomas Soome 		    offset, 0, 0, 0);
2427b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2428b24ab676SJeff Bonwick 		break;
2429b24ab676SJeff Bonwick 
2430b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2431b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2432b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2433b24ab676SJeff Bonwick 			/*
2434b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2435b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2436b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2437b24ab676SJeff Bonwick 			 */
2438b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2439b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2440b24ab676SJeff Bonwick 		}
2441b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2442b24ab676SJeff Bonwick 		break;
2443b24ab676SJeff Bonwick 
2444b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2445b24ab676SJeff Bonwick 		bzero(data, blocksize);
2446b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2447b24ab676SJeff Bonwick 		break;
2448b24ab676SJeff Bonwick 
2449b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2450b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2451b24ab676SJeff Bonwick 		break;
2452b24ab676SJeff Bonwick 
2453b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2454b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2455b24ab676SJeff Bonwick 		break;
245680901aeaSGeorge Wilson 
245780901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
2458f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_READER);
245980901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
246080901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
246180901aeaSGeorge Wilson 		    B_FALSE);
246280901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
246380901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
246480901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
246580901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
246680901aeaSGeorge Wilson 		    B_FALSE);
246780901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
2468f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
246980901aeaSGeorge Wilson 
247080901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
247180901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
247280901aeaSGeorge Wilson 
247380901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
247480901aeaSGeorge Wilson 		break;
2475b24ab676SJeff Bonwick 	}
2476b24ab676SJeff Bonwick 
2477f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2478c9ba2a43SEric Schrock 
2479b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2480b24ab676SJeff Bonwick }
2481b24ab676SJeff Bonwick 
2482b24ab676SJeff Bonwick /*
2483b24ab676SJeff Bonwick  * Initialize an object description template.
2484b24ab676SJeff Bonwick  */
2485b24ab676SJeff Bonwick static void
2486b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
248754811da5SToomas Soome     dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
248854811da5SToomas Soome     uint64_t gen)
2489b24ab676SJeff Bonwick {
2490b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2491b24ab676SJeff Bonwick 	od->od_object = 0;
2492b24ab676SJeff Bonwick 
2493b24ab676SJeff Bonwick 	od->od_crtype = type;
2494b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
249554811da5SToomas Soome 	od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2496b24ab676SJeff Bonwick 	od->od_crgen = gen;
2497b24ab676SJeff Bonwick 
2498b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2499b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2500b24ab676SJeff Bonwick 	od->od_gen = 0;
2501b24ab676SJeff Bonwick 
2502b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2503b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2504b24ab676SJeff Bonwick }
2505b24ab676SJeff Bonwick 
2506b24ab676SJeff Bonwick /*
2507b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2508b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2509b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2510b24ab676SJeff Bonwick  * use the existing objects.
2511b24ab676SJeff Bonwick  */
2512b24ab676SJeff Bonwick static int
2513b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2514b24ab676SJeff Bonwick {
2515b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2516b24ab676SJeff Bonwick 	int rv = 0;
2517b24ab676SJeff Bonwick 
2518f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2519b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2520b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2521b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2522b24ab676SJeff Bonwick 		rv = -1;
2523b24ab676SJeff Bonwick 	zd->zd_od = od;
2524f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2525b24ab676SJeff Bonwick 
2526b24ab676SJeff Bonwick 	return (rv);
2527b24ab676SJeff Bonwick }
2528b24ab676SJeff Bonwick 
2529b24ab676SJeff Bonwick /* ARGSUSED */
2530b24ab676SJeff Bonwick void
2531b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2532b24ab676SJeff Bonwick {
2533b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2534b24ab676SJeff Bonwick 
2535f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2536c9ba2a43SEric Schrock 
25375002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2538b24ab676SJeff Bonwick 
2539b24ab676SJeff Bonwick 	/*
2540b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2541b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2542b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2543b24ab676SJeff Bonwick 	 */
2544b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2545420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2546420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2547420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2548b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2549c9ba2a43SEric Schrock 
2550f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2551c9ba2a43SEric Schrock }
2552c9ba2a43SEric Schrock 
2553c9ba2a43SEric Schrock /*
2554c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2555c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2556c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2557c9ba2a43SEric Schrock  */
2558c9ba2a43SEric Schrock /* ARGSUSED */
2559c9ba2a43SEric Schrock void
2560c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2561c9ba2a43SEric Schrock {
2562c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2563c9ba2a43SEric Schrock 
256480901aeaSGeorge Wilson 	/*
256580901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
256680901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
256780901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
256880901aeaSGeorge Wilson 	 */
2569f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2570f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2571c9ba2a43SEric Schrock 
2572c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2573c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2574c9ba2a43SEric Schrock 
2575c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2576c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2577c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2578c9ba2a43SEric Schrock 
2579f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2580f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2581b24ab676SJeff Bonwick }
2582b24ab676SJeff Bonwick 
2583b24ab676SJeff Bonwick /*
2584b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2585b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2586b24ab676SJeff Bonwick  */
2587b24ab676SJeff Bonwick /* ARGSUSED */
2588b24ab676SJeff Bonwick void
2589b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2590b24ab676SJeff Bonwick {
2591420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2592b24ab676SJeff Bonwick 	spa_t *spa;
2593b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2594b24ab676SJeff Bonwick 
2595e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2596e0f1c0afSOlaf Faaland 		return;
2597e0f1c0afSOlaf Faaland 
2598b24ab676SJeff Bonwick 	/*
2599b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2600b24ab676SJeff Bonwick 	 */
2601663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2602b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
2603*eb633035STom Caputi 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2604b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2605b24ab676SJeff Bonwick 
2606b24ab676SJeff Bonwick 	/*
2607b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2608b24ab676SJeff Bonwick 	 */
2609663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 2, 1);
2610b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
2611*eb633035STom Caputi 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2612b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2613b24ab676SJeff Bonwick 
2614b24ab676SJeff Bonwick 	/*
2615b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2616b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2617b24ab676SJeff Bonwick 	 */
2618f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
2619663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2620*eb633035STom Caputi 	VERIFY3U(EEXIST, ==,
2621*eb633035STom Caputi 	    spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2622b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2623b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2624420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2625b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2626b24ab676SJeff Bonwick 
2627f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
2628b24ab676SJeff Bonwick }
2629b24ab676SJeff Bonwick 
2630e0f1c0afSOlaf Faaland /*
2631e0f1c0afSOlaf Faaland  * Start and then stop the MMP threads to ensure the startup and shutdown code
2632e0f1c0afSOlaf Faaland  * works properly.  Actual protection and property-related code tested via ZTS.
2633e0f1c0afSOlaf Faaland  */
2634e0f1c0afSOlaf Faaland /* ARGSUSED */
2635e0f1c0afSOlaf Faaland void
2636e0f1c0afSOlaf Faaland ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2637e0f1c0afSOlaf Faaland {
2638e0f1c0afSOlaf Faaland 	ztest_shared_opts_t *zo = &ztest_opts;
2639e0f1c0afSOlaf Faaland 	spa_t *spa = ztest_spa;
2640e0f1c0afSOlaf Faaland 
2641e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2642e0f1c0afSOlaf Faaland 		return;
2643e0f1c0afSOlaf Faaland 
2644e0f1c0afSOlaf Faaland 	/*
2645e0f1c0afSOlaf Faaland 	 * Since enabling MMP involves setting a property, it could not be done
2646e0f1c0afSOlaf Faaland 	 * while the pool is suspended.
2647e0f1c0afSOlaf Faaland 	 */
2648e0f1c0afSOlaf Faaland 	if (spa_suspended(spa))
2649e0f1c0afSOlaf Faaland 		return;
2650e0f1c0afSOlaf Faaland 
2651e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2652e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2653e0f1c0afSOlaf Faaland 
2654e0f1c0afSOlaf Faaland 	zfs_multihost_fail_intervals = 0;
2655e0f1c0afSOlaf Faaland 
2656e0f1c0afSOlaf Faaland 	if (!spa_multihost(spa)) {
2657e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_TRUE;
2658e0f1c0afSOlaf Faaland 		mmp_thread_start(spa);
2659e0f1c0afSOlaf Faaland 	}
2660e0f1c0afSOlaf Faaland 
2661e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2662e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2663e0f1c0afSOlaf Faaland 
2664e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2665e0f1c0afSOlaf Faaland 	mmp_signal_all_threads();
2666e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2667e0f1c0afSOlaf Faaland 
2668e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2669e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2670e0f1c0afSOlaf Faaland 
2671e0f1c0afSOlaf Faaland 	if (spa_multihost(spa)) {
2672e0f1c0afSOlaf Faaland 		mmp_thread_stop(spa);
2673e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_FALSE;
2674e0f1c0afSOlaf Faaland 	}
2675e0f1c0afSOlaf Faaland 
2676e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2677e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2678e0f1c0afSOlaf Faaland }
2679e0f1c0afSOlaf Faaland 
268025345e46SGeorge Wilson /* ARGSUSED */
268125345e46SGeorge Wilson void
268225345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
268325345e46SGeorge Wilson {
268425345e46SGeorge Wilson 	spa_t *spa;
268525345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
268625345e46SGeorge Wilson 	uint64_t version, newversion;
268725345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
268825345e46SGeorge Wilson 	char *name;
268925345e46SGeorge Wilson 
2690e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2691e0f1c0afSOlaf Faaland 		return;
2692e0f1c0afSOlaf Faaland 
2693f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
269425345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
269525345e46SGeorge Wilson 
269625345e46SGeorge Wilson 	/*
269725345e46SGeorge Wilson 	 * Clean up from previous runs.
269825345e46SGeorge Wilson 	 */
269925345e46SGeorge Wilson 	(void) spa_destroy(name);
270025345e46SGeorge Wilson 
270125345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2702663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
270325345e46SGeorge Wilson 
270425345e46SGeorge Wilson 	/*
270525345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
270625345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
270725345e46SGeorge Wilson 	 */
270825345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
270925345e46SGeorge Wilson 	case 0:
271025345e46SGeorge Wilson 	case 1:
271125345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
271225345e46SGeorge Wilson 		break;
271325345e46SGeorge Wilson 	case 2:
271425345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
271525345e46SGeorge Wilson 		break;
271625345e46SGeorge Wilson 	case 3:
271725345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
271825345e46SGeorge Wilson 		break;
271925345e46SGeorge Wilson 	}
272025345e46SGeorge Wilson 
272125345e46SGeorge Wilson 	/*
272225345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
272325345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
272425345e46SGeorge Wilson 	 */
272525345e46SGeorge Wilson 	do {
272625345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
272725345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
272825345e46SGeorge Wilson 
272925345e46SGeorge Wilson 	props = fnvlist_alloc();
273025345e46SGeorge Wilson 	fnvlist_add_uint64(props,
273125345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2732*eb633035STom Caputi 	VERIFY0(spa_create(name, nvroot, props, NULL, NULL));
273325345e46SGeorge Wilson 	fnvlist_free(nvroot);
273425345e46SGeorge Wilson 	fnvlist_free(props);
273525345e46SGeorge Wilson 
273625345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
273725345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
273825345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
273925345e46SGeorge Wilson 
274025345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
274125345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
274225345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
274325345e46SGeorge Wilson 	}
274425345e46SGeorge Wilson 
274525345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
274625345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
274725345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
274825345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
274925345e46SGeorge Wilson 	spa_close(spa, FTAG);
275025345e46SGeorge Wilson 
275125345e46SGeorge Wilson 	strfree(name);
2752f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
275325345e46SGeorge Wilson }
275425345e46SGeorge Wilson 
275586714001SSerapheim Dimitropoulos static void
275686714001SSerapheim Dimitropoulos ztest_spa_checkpoint(spa_t *spa)
275786714001SSerapheim Dimitropoulos {
275886714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
275986714001SSerapheim Dimitropoulos 
276086714001SSerapheim Dimitropoulos 	int error = spa_checkpoint(spa->spa_name);
276186714001SSerapheim Dimitropoulos 
276286714001SSerapheim Dimitropoulos 	switch (error) {
276386714001SSerapheim Dimitropoulos 	case 0:
276486714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
276586714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
276686714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
276786714001SSerapheim Dimitropoulos 		break;
276886714001SSerapheim Dimitropoulos 	case ENOSPC:
276986714001SSerapheim Dimitropoulos 		ztest_record_enospc(FTAG);
277086714001SSerapheim Dimitropoulos 		break;
277186714001SSerapheim Dimitropoulos 	default:
277286714001SSerapheim Dimitropoulos 		fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
277386714001SSerapheim Dimitropoulos 	}
277486714001SSerapheim Dimitropoulos }
277586714001SSerapheim Dimitropoulos 
277686714001SSerapheim Dimitropoulos static void
277786714001SSerapheim Dimitropoulos ztest_spa_discard_checkpoint(spa_t *spa)
277886714001SSerapheim Dimitropoulos {
277986714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
278086714001SSerapheim Dimitropoulos 
278186714001SSerapheim Dimitropoulos 	int error = spa_checkpoint_discard(spa->spa_name);
278286714001SSerapheim Dimitropoulos 
278386714001SSerapheim Dimitropoulos 	switch (error) {
278486714001SSerapheim Dimitropoulos 	case 0:
278586714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
278686714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
278786714001SSerapheim Dimitropoulos 		break;
278886714001SSerapheim Dimitropoulos 	default:
278986714001SSerapheim Dimitropoulos 		fatal(0, "spa_discard_checkpoint(%s) = %d",
279086714001SSerapheim Dimitropoulos 		    spa->spa_name, error);
279186714001SSerapheim Dimitropoulos 	}
279286714001SSerapheim Dimitropoulos 
279386714001SSerapheim Dimitropoulos }
279486714001SSerapheim Dimitropoulos 
279586714001SSerapheim Dimitropoulos /* ARGSUSED */
279686714001SSerapheim Dimitropoulos void
279786714001SSerapheim Dimitropoulos ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
279886714001SSerapheim Dimitropoulos {
279986714001SSerapheim Dimitropoulos 	spa_t *spa = ztest_spa;
280086714001SSerapheim Dimitropoulos 
280186714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
280286714001SSerapheim Dimitropoulos 	if (ztest_random(2) == 0) {
280386714001SSerapheim Dimitropoulos 		ztest_spa_checkpoint(spa);
280486714001SSerapheim Dimitropoulos 	} else {
280586714001SSerapheim Dimitropoulos 		ztest_spa_discard_checkpoint(spa);
280686714001SSerapheim Dimitropoulos 	}
280786714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
280886714001SSerapheim Dimitropoulos }
280986714001SSerapheim Dimitropoulos 
281086714001SSerapheim Dimitropoulos 
2811b24ab676SJeff Bonwick static vdev_t *
2812b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2813b24ab676SJeff Bonwick {
2814b24ab676SJeff Bonwick 	vdev_t *mvd;
2815b24ab676SJeff Bonwick 
2816b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2817b24ab676SJeff Bonwick 		return (vd);
2818b24ab676SJeff Bonwick 
2819b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2820b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2821b24ab676SJeff Bonwick 		    NULL)
2822b24ab676SJeff Bonwick 			return (mvd);
2823b24ab676SJeff Bonwick 
2824b24ab676SJeff Bonwick 	return (NULL);
2825b24ab676SJeff Bonwick }
2826b24ab676SJeff Bonwick 
2827b24ab676SJeff Bonwick /*
2828b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2829b24ab676SJeff Bonwick  */
2830b24ab676SJeff Bonwick int
2831b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2832b24ab676SJeff Bonwick {
2833b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2834b24ab676SJeff Bonwick 	int c;
2835b24ab676SJeff Bonwick 
2836b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2837b24ab676SJeff Bonwick 
2838b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2839b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2840b24ab676SJeff Bonwick 
2841b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2842b24ab676SJeff Bonwick 			break;
2843b24ab676SJeff Bonwick 	}
2844b24ab676SJeff Bonwick 	return (c);
2845b24ab676SJeff Bonwick }
2846b24ab676SJeff Bonwick 
2847b24ab676SJeff Bonwick /*
2848b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2849b24ab676SJeff Bonwick  */
2850b24ab676SJeff Bonwick /* ARGSUSED */
2851b24ab676SJeff Bonwick void
2852b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2853b24ab676SJeff Bonwick {
2854b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2855420dfc95SChris Siden 	spa_t *spa = ztest_spa;
28561195e687SMark J Musante 	uint64_t leaves;
2857b24ab676SJeff Bonwick 	uint64_t guid;
2858b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2859b24ab676SJeff Bonwick 	int error;
2860b24ab676SJeff Bonwick 
2861e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2862e0f1c0afSOlaf Faaland 		return;
2863e0f1c0afSOlaf Faaland 
2864f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
28653b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2866b24ab676SJeff Bonwick 
2867b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2868b24ab676SJeff Bonwick 
2869b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2870b24ab676SJeff Bonwick 
2871b24ab676SJeff Bonwick 	/*
2872b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2873b24ab676SJeff Bonwick 	 */
287488ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2875663207adSDon Brady 		metaslab_group_t *mg;
2876663207adSDon Brady 
287788ecc943SGeorge Wilson 		/*
2878663207adSDon Brady 		 * find the first real slog in log allocation class
287988ecc943SGeorge Wilson 		 */
2880663207adSDon Brady 		mg =  spa_log_class(spa)->mc_rotor;
2881663207adSDon Brady 		while (!mg->mg_vd->vdev_islog)
2882663207adSDon Brady 			mg = mg->mg_next;
2883663207adSDon Brady 
2884663207adSDon Brady 		guid = mg->mg_vd->vdev_guid;
28858654d025Sperrin 
288688ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2887fa9e4066Sahrens 
288888ecc943SGeorge Wilson 		/*
288988ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
289088ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
289188ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
289288ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
289388ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
289488ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
289588ecc943SGeorge Wilson 		 */
2896f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_WRITER);
289788ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2898f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
2899fa9e4066Sahrens 
290086714001SSerapheim Dimitropoulos 		switch (error) {
290186714001SSerapheim Dimitropoulos 		case 0:
290286714001SSerapheim Dimitropoulos 		case EEXIST:
290386714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
290486714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
290586714001SSerapheim Dimitropoulos 			break;
290686714001SSerapheim Dimitropoulos 		default:
290788ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
290886714001SSerapheim Dimitropoulos 		}
290988ecc943SGeorge Wilson 	} else {
291088ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
291188ecc943SGeorge Wilson 
291288ecc943SGeorge Wilson 		/*
2913663207adSDon Brady 		 * Make 1/4 of the devices be log devices
291488ecc943SGeorge Wilson 		 */
291525345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2916663207adSDon Brady 		    ztest_opts.zo_vdev_size, 0, (ztest_random(4) == 0) ?
2917663207adSDon Brady 		    "log" : NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
291888ecc943SGeorge Wilson 
291988ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
292088ecc943SGeorge Wilson 		nvlist_free(nvroot);
292188ecc943SGeorge Wilson 
292286714001SSerapheim Dimitropoulos 		switch (error) {
292386714001SSerapheim Dimitropoulos 		case 0:
292486714001SSerapheim Dimitropoulos 			break;
292586714001SSerapheim Dimitropoulos 		case ENOSPC:
292688ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
292786714001SSerapheim Dimitropoulos 			break;
292886714001SSerapheim Dimitropoulos 		default:
292988ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
293086714001SSerapheim Dimitropoulos 		}
293188ecc943SGeorge Wilson 	}
293288ecc943SGeorge Wilson 
2933f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
2934e14bb325SJeff Bonwick }
2935fa9e4066Sahrens 
2936663207adSDon Brady /* ARGSUSED */
2937663207adSDon Brady void
2938663207adSDon Brady ztest_vdev_class_add(ztest_ds_t *zd, uint64_t id)
2939663207adSDon Brady {
2940663207adSDon Brady 	ztest_shared_t *zs = ztest_shared;
2941663207adSDon Brady 	spa_t *spa = ztest_spa;
2942663207adSDon Brady 	uint64_t leaves;
2943663207adSDon Brady 	nvlist_t *nvroot;
2944663207adSDon Brady 	const char *class = (ztest_random(2) == 0) ?
2945663207adSDon Brady 	    VDEV_ALLOC_BIAS_SPECIAL : VDEV_ALLOC_BIAS_DEDUP;
2946663207adSDon Brady 	int error;
2947663207adSDon Brady 
2948663207adSDon Brady 	/*
2949663207adSDon Brady 	 * By default add a special vdev 50% of the time
2950663207adSDon Brady 	 */
2951663207adSDon Brady 	if ((ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_OFF) ||
2952663207adSDon Brady 	    (ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_RND &&
2953663207adSDon Brady 	    ztest_random(2) == 0)) {
2954663207adSDon Brady 		return;
2955663207adSDon Brady 	}
2956663207adSDon Brady 
2957663207adSDon Brady 	mutex_enter(&ztest_vdev_lock);
2958663207adSDon Brady 
2959663207adSDon Brady 	/* Only test with mirrors */
2960663207adSDon Brady 	if (zs->zs_mirrors < 2) {
2961663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2962663207adSDon Brady 		return;
2963663207adSDon Brady 	}
2964663207adSDon Brady 
2965663207adSDon Brady 	/* requires feature@allocation_classes */
2966663207adSDon Brady 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)) {
2967663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2968663207adSDon Brady 		return;
2969663207adSDon Brady 	}
2970663207adSDon Brady 
2971663207adSDon Brady 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2972663207adSDon Brady 
2973663207adSDon Brady 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2974663207adSDon Brady 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2975663207adSDon Brady 	spa_config_exit(spa, SCL_VDEV, FTAG);
2976663207adSDon Brady 
2977663207adSDon Brady 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
2978663207adSDon Brady 	    class, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
2979663207adSDon Brady 
2980663207adSDon Brady 	error = spa_vdev_add(spa, nvroot);
2981663207adSDon Brady 	nvlist_free(nvroot);
2982663207adSDon Brady 
2983663207adSDon Brady 	if (error == ENOSPC)
2984663207adSDon Brady 		ztest_record_enospc("spa_vdev_add");
2985663207adSDon Brady 	else if (error != 0)
2986663207adSDon Brady 		fatal(0, "spa_vdev_add() = %d", error);
2987663207adSDon Brady 
2988663207adSDon Brady 	/*
2989663207adSDon Brady 	 * 50% of the time allow small blocks in the special class
2990663207adSDon Brady 	 */
2991663207adSDon Brady 	if (error == 0 &&
2992663207adSDon Brady 	    spa_special_class(spa)->mc_groups == 1 && ztest_random(2) == 0) {
2993663207adSDon Brady 		if (ztest_opts.zo_verbose >= 3)
2994663207adSDon Brady 			(void) printf("Enabling special VDEV small blocks\n");
2995663207adSDon Brady 		(void) ztest_dsl_prop_set_uint64(zd->zd_name,
2996663207adSDon Brady 		    ZFS_PROP_SPECIAL_SMALL_BLOCKS, 32768, B_FALSE);
2997663207adSDon Brady 	}
2998663207adSDon Brady 
2999663207adSDon Brady 	mutex_exit(&ztest_vdev_lock);
3000663207adSDon Brady 
3001663207adSDon Brady 	if (ztest_opts.zo_verbose >= 3) {
3002663207adSDon Brady 		metaslab_class_t *mc;
3003663207adSDon Brady 
3004663207adSDon Brady 		if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3005663207adSDon Brady 			mc = spa_special_class(spa);
3006663207adSDon Brady 		else
3007663207adSDon Brady 			mc = spa_dedup_class(spa);
3008663207adSDon Brady 		(void) printf("Added a %s mirrored vdev (of %d)\n",
3009663207adSDon Brady 		    class, (int)mc->mc_groups);
3010663207adSDon Brady 	}
3011663207adSDon Brady }
3012663207adSDon Brady 
3013e14bb325SJeff Bonwick /*
3014e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3015e14bb325SJeff Bonwick  */
3016b24ab676SJeff Bonwick /* ARGSUSED */
3017e14bb325SJeff Bonwick void
3018b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
3019e14bb325SJeff Bonwick {
3020b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
3021420dfc95SChris Siden 	spa_t *spa = ztest_spa;
302231157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3023e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
3024e14bb325SJeff Bonwick 	char *aux;
3025e14bb325SJeff Bonwick 	uint64_t guid = 0;
3026e14bb325SJeff Bonwick 	int error;
3027e14bb325SJeff Bonwick 
3028e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3029e0f1c0afSOlaf Faaland 		return;
3030e0f1c0afSOlaf Faaland 
303131157203SJeff Bonwick 	if (ztest_random(2) == 0) {
3032e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
3033e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
3034e14bb325SJeff Bonwick 	} else {
3035e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
3036e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
3037e14bb325SJeff Bonwick 	}
3038e14bb325SJeff Bonwick 
3039f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3040e14bb325SJeff Bonwick 
3041e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3042e14bb325SJeff Bonwick 
3043e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
3044e14bb325SJeff Bonwick 		/*
3045e14bb325SJeff Bonwick 		 * Pick a random device to remove.
3046e14bb325SJeff Bonwick 		 */
3047e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3048e14bb325SJeff Bonwick 	} else {
3049e14bb325SJeff Bonwick 		/*
3050e14bb325SJeff Bonwick 		 * Find an unused device we can add.
3051e14bb325SJeff Bonwick 		 */
3052b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
3053e14bb325SJeff Bonwick 		for (;;) {
3054e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
3055e14bb325SJeff Bonwick 			int c;
3056420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
3057420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3058420dfc95SChris Siden 			    zs->zs_vdev_aux);
3059e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
3060e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
3061e14bb325SJeff Bonwick 				    path) == 0)
3062e14bb325SJeff Bonwick 					break;
306331157203SJeff Bonwick 			if (c == sav->sav_count &&
306431157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
3065e14bb325SJeff Bonwick 				break;
3066b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
3067e14bb325SJeff Bonwick 		}
3068e14bb325SJeff Bonwick 	}
3069e14bb325SJeff Bonwick 
3070e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
3071e14bb325SJeff Bonwick 
3072e14bb325SJeff Bonwick 	if (guid == 0) {
3073e14bb325SJeff Bonwick 		/*
3074e14bb325SJeff Bonwick 		 * Add a new device.
3075e14bb325SJeff Bonwick 		 */
307625345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3077663207adSDon Brady 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, NULL, 0, 0, 1);
3078e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
307986714001SSerapheim Dimitropoulos 
308086714001SSerapheim Dimitropoulos 		switch (error) {
308186714001SSerapheim Dimitropoulos 		case 0:
308286714001SSerapheim Dimitropoulos 			break;
308386714001SSerapheim Dimitropoulos 		default:
3084e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
308586714001SSerapheim Dimitropoulos 		}
3086e14bb325SJeff Bonwick 		nvlist_free(nvroot);
3087e14bb325SJeff Bonwick 	} else {
3088e14bb325SJeff Bonwick 		/*
3089e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
3090e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
3091e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
3092e14bb325SJeff Bonwick 		 */
3093e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
3094573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
3095e14bb325SJeff Bonwick 
3096e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
309786714001SSerapheim Dimitropoulos 
309886714001SSerapheim Dimitropoulos 		switch (error) {
309986714001SSerapheim Dimitropoulos 		case 0:
310086714001SSerapheim Dimitropoulos 		case EBUSY:
310186714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
310286714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
310386714001SSerapheim Dimitropoulos 			break;
310486714001SSerapheim Dimitropoulos 		default:
3105e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
310686714001SSerapheim Dimitropoulos 		}
3107e14bb325SJeff Bonwick 	}
3108e14bb325SJeff Bonwick 
3109f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3110fa9e4066Sahrens }
3111fa9e4066Sahrens 
31121195e687SMark J Musante /*
31131195e687SMark J Musante  * split a pool if it has mirror tlvdevs
31141195e687SMark J Musante  */
31151195e687SMark J Musante /* ARGSUSED */
31161195e687SMark J Musante void
31171195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
31181195e687SMark J Musante {
31191195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
3120420dfc95SChris Siden 	spa_t *spa = ztest_spa;
31211195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
31221195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
31231195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
31241195e687SMark J Musante 	int error = 0;
31251195e687SMark J Musante 
3126e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3127e0f1c0afSOlaf Faaland 		return;
3128e0f1c0afSOlaf Faaland 
3129f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
31301195e687SMark J Musante 
31311195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
3132420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3133f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
31341195e687SMark J Musante 		return;
31351195e687SMark J Musante 	}
31361195e687SMark J Musante 
31371195e687SMark J Musante 	/* clean up the old pool, if any */
31381195e687SMark J Musante 	(void) spa_destroy("splitp");
31391195e687SMark J Musante 
31401195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
31411195e687SMark J Musante 
31421195e687SMark J Musante 	/* generate a config from the existing config */
314398295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
31441195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
31451195e687SMark J Musante 	    &tree) == 0);
314698295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
314798295d61SMark J Musante 
31481195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
31491195e687SMark J Musante 	    &children) == 0);
31501195e687SMark J Musante 
31511195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
31521195e687SMark J Musante 	for (c = 0; c < children; c++) {
31531195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
31541195e687SMark J Musante 		nvlist_t **mchild;
31551195e687SMark J Musante 		uint_t mchildren;
31561195e687SMark J Musante 
31571195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
31581195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
31591195e687SMark J Musante 			    0) == 0);
31601195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
31611195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
31621195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
31631195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
31641195e687SMark J Musante 			if (lastlogid == 0)
31651195e687SMark J Musante 				lastlogid = schildren;
31661195e687SMark J Musante 			++schildren;
31671195e687SMark J Musante 			continue;
31681195e687SMark J Musante 		}
31691195e687SMark J Musante 		lastlogid = 0;
31701195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
31711195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
31721195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
31731195e687SMark J Musante 	}
31741195e687SMark J Musante 
31751195e687SMark J Musante 	/* OK, create a config that can be used to split */
31761195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
31771195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
31781195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
31791195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
31801195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
31811195e687SMark J Musante 
31821195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
31831195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
31841195e687SMark J Musante 
31851195e687SMark J Musante 	for (c = 0; c < schildren; c++)
31861195e687SMark J Musante 		nvlist_free(schild[c]);
31871195e687SMark J Musante 	free(schild);
31881195e687SMark J Musante 	nvlist_free(split);
31891195e687SMark J Musante 
31901195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
31911195e687SMark J Musante 
3192f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
31931195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3194f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
31951195e687SMark J Musante 
31961195e687SMark J Musante 	nvlist_free(config);
31971195e687SMark J Musante 
31981195e687SMark J Musante 	if (error == 0) {
31991195e687SMark J Musante 		(void) printf("successful split - results:\n");
32001195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
32011195e687SMark J Musante 		show_pool_stats(spa);
32021195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
32031195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
32041195e687SMark J Musante 		++zs->zs_splits;
32051195e687SMark J Musante 		--zs->zs_mirrors;
32061195e687SMark J Musante 	}
3207f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
32081195e687SMark J Musante }
32091195e687SMark J Musante 
3210fa9e4066Sahrens /*
3211fa9e4066Sahrens  * Verify that we can attach and detach devices.
3212fa9e4066Sahrens  */
3213b24ab676SJeff Bonwick /* ARGSUSED */
3214fa9e4066Sahrens void
3215b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3216fa9e4066Sahrens {
3217b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
3218420dfc95SChris Siden 	spa_t *spa = ztest_spa;
321931157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
3220fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3221ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
3222e14bb325SJeff Bonwick 	nvlist_t *root;
32231195e687SMark J Musante 	uint64_t leaves;
3224fa9e4066Sahrens 	uint64_t leaf, top;
3225ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
32268ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
3227b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
3228ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
3229fa9e4066Sahrens 	int replacing;
32309af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
323131157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
323231157203SJeff Bonwick 	int oldvd_is_log;
3233fa9e4066Sahrens 	int error, expected_error;
3234fa9e4066Sahrens 
3235e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3236e0f1c0afSOlaf Faaland 		return;
3237e0f1c0afSOlaf Faaland 
3238f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3239420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3240fa9e4066Sahrens 
32415cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32425cabbc6bSPrashanth Sreenivasa 
32435cabbc6bSPrashanth Sreenivasa 	/*
32445cabbc6bSPrashanth Sreenivasa 	 * If a vdev is in the process of being removed, its removal may
32455cabbc6bSPrashanth Sreenivasa 	 * finish while we are in progress, leading to an unexpected error
32465cabbc6bSPrashanth Sreenivasa 	 * value.  Don't bother trying to attach while we are in the middle
32475cabbc6bSPrashanth Sreenivasa 	 * of removal.
32485cabbc6bSPrashanth Sreenivasa 	 */
32493a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
32505cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
3251f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
32525cabbc6bSPrashanth Sreenivasa 		return;
32535cabbc6bSPrashanth Sreenivasa 	}
3254fa9e4066Sahrens 
3255fa9e4066Sahrens 	/*
3256fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
3257fa9e4066Sahrens 	 */
3258fa9e4066Sahrens 	replacing = ztest_random(2);
3259fa9e4066Sahrens 
3260fa9e4066Sahrens 	/*
3261fa9e4066Sahrens 	 * Pick a random top-level vdev.
3262fa9e4066Sahrens 	 */
3263b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3264fa9e4066Sahrens 
3265fa9e4066Sahrens 	/*
3266fa9e4066Sahrens 	 * Pick a random leaf within it.
3267fa9e4066Sahrens 	 */
3268d41c4376SMark J Musante 	leaf = ztest_random(leaves);
3269fa9e4066Sahrens 
3270fa9e4066Sahrens 	/*
327131157203SJeff Bonwick 	 * Locate this vdev.
3272fa9e4066Sahrens 	 */
327331157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
3274663207adSDon Brady 
3275663207adSDon Brady 	/* pick a child from the mirror */
32761195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
32778ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
32781195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3279420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
32808ad4d6ddSJeff Bonwick 	}
3281663207adSDon Brady 
3282663207adSDon Brady 	/* pick a child out of the raidz group */
3283420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
32848ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3285420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3286420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
32878ad4d6ddSJeff Bonwick 	}
3288fa9e4066Sahrens 
3289fa9e4066Sahrens 	/*
329031157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
329131157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
3292fa9e4066Sahrens 	 */
329331157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
32949af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
32958ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
32968ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
329731157203SJeff Bonwick 	}
3298fa9e4066Sahrens 
329931157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
3300573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
330131157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
330231157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
3303ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
33048ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
3305fa9e4066Sahrens 
33069af0a4dfSJeff Bonwick 	/*
33079af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
33089af0a4dfSJeff Bonwick 	 */
33099af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
33105cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
33118ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
33128ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
331386714001SSerapheim Dimitropoulos 		    error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
331486714001SSerapheim Dimitropoulos 		    error != ZFS_ERR_DISCARDING_CHECKPOINT)
33158ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
3316f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
33179af0a4dfSJeff Bonwick 		return;
33189af0a4dfSJeff Bonwick 	}
33199af0a4dfSJeff Bonwick 
3320fa9e4066Sahrens 	/*
332131157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
332231157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3323fa9e4066Sahrens 	 */
332431157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
332531157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
332631157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
332731157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
332831157203SJeff Bonwick 	} else {
332931157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3330420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
3331420dfc95SChris Siden 		    top * leaves + leaf);
333231157203SJeff Bonwick 		if (ztest_random(2) == 0)
333331157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
333431157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
333531157203SJeff Bonwick 	}
333631157203SJeff Bonwick 
333731157203SJeff Bonwick 	if (newvd) {
33385cabbc6bSPrashanth Sreenivasa 		/*
33395cabbc6bSPrashanth Sreenivasa 		 * Reopen to ensure the vdev's asize field isn't stale.
33405cabbc6bSPrashanth Sreenivasa 		 */
33415cabbc6bSPrashanth Sreenivasa 		vdev_reopen(newvd);
3342573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
334331157203SJeff Bonwick 	} else {
334431157203SJeff Bonwick 		/*
334531157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
334631157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
334731157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
334831157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
334931157203SJeff Bonwick 		 */
335031157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
335131157203SJeff Bonwick 	}
3352fa9e4066Sahrens 
3353fa9e4066Sahrens 	/*
3354fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3355fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
3356fa9e4066Sahrens 	 *
3357ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
3358fa9e4066Sahrens 	 *
3359ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
3360fa9e4066Sahrens 	 */
336131157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
336231157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
336331157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
336431157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
336531157203SJeff Bonwick 		expected_error = ENOTSUP;
336631157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
3367fa9e4066Sahrens 		expected_error = ENOTSUP;
336831157203SJeff Bonwick 	else if (newvd == oldvd)
336931157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
337031157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
337131157203SJeff Bonwick 		expected_error = EBUSY;
3372ea8dc4b6Seschrock 	else if (newsize < oldsize)
3373fa9e4066Sahrens 		expected_error = EOVERFLOW;
3374ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
3375ecc2d604Sbonwick 		expected_error = EDOM;
3376fa9e4066Sahrens 	else
3377fa9e4066Sahrens 		expected_error = 0;
3378fa9e4066Sahrens 
33795cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_ALL, FTAG);
3380fa9e4066Sahrens 
3381fa9e4066Sahrens 	/*
3382ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
3383fa9e4066Sahrens 	 */
338425345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3385663207adSDon Brady 	    ashift, NULL, 0, 0, 1);
3386fa9e4066Sahrens 
338731157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
3388fa9e4066Sahrens 
3389fa9e4066Sahrens 	nvlist_free(root);
3390fa9e4066Sahrens 
3391fa9e4066Sahrens 	/*
3392fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
3393fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
3394fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
3395fa9e4066Sahrens 	 */
3396fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
3397fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
3398fa9e4066Sahrens 		expected_error = error;
3399fa9e4066Sahrens 
3400f0aa80d4Sbonwick 	/*
3401f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
3402f0aa80d4Sbonwick 	 */
3403088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
3404f0aa80d4Sbonwick 		expected_error = error;
3405f0aa80d4Sbonwick 
340686714001SSerapheim Dimitropoulos 	if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
340786714001SSerapheim Dimitropoulos 	    error == ZFS_ERR_DISCARDING_CHECKPOINT)
340886714001SSerapheim Dimitropoulos 		expected_error = error;
340986714001SSerapheim Dimitropoulos 
3410088f3894Sahrens 	/* XXX workaround 6690467 */
3411088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
3412088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
3413088f3894Sahrens 		    "returned %d, expected %d",
3414b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
3415b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
3416fa9e4066Sahrens 	}
3417fa9e4066Sahrens 
3418f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3419fa9e4066Sahrens }
3420fa9e4066Sahrens 
34215cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
34225cabbc6bSPrashanth Sreenivasa void
34235cabbc6bSPrashanth Sreenivasa ztest_device_removal(ztest_ds_t *zd, uint64_t id)
34245cabbc6bSPrashanth Sreenivasa {
34255cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ztest_spa;
34265cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
34275cabbc6bSPrashanth Sreenivasa 	uint64_t guid;
34283a4b1be9SMatthew Ahrens 	int error;
34295cabbc6bSPrashanth Sreenivasa 
3430f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
34315cabbc6bSPrashanth Sreenivasa 
34323a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
34333a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34343a4b1be9SMatthew Ahrens 		return;
34353a4b1be9SMatthew Ahrens 	}
34363a4b1be9SMatthew Ahrens 
34373a4b1be9SMatthew Ahrens 	/*
34383a4b1be9SMatthew Ahrens 	 * Remove a random top-level vdev and wait for removal to finish.
34393a4b1be9SMatthew Ahrens 	 */
34405cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34415cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
34425cabbc6bSPrashanth Sreenivasa 	guid = vd->vdev_guid;
34435cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
34445cabbc6bSPrashanth Sreenivasa 
34453a4b1be9SMatthew Ahrens 	error = spa_vdev_remove(spa, guid, B_FALSE);
34463a4b1be9SMatthew Ahrens 	if (error == 0) {
34473a4b1be9SMatthew Ahrens 		ztest_device_removal_active = B_TRUE;
34483a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34493a4b1be9SMatthew Ahrens 
34503a4b1be9SMatthew Ahrens 		while (spa->spa_vdev_removal != NULL)
34513a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34523a4b1be9SMatthew Ahrens 	} else {
34533a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34543a4b1be9SMatthew Ahrens 		return;
34553a4b1be9SMatthew Ahrens 	}
34565cabbc6bSPrashanth Sreenivasa 
34573a4b1be9SMatthew Ahrens 	/*
34583a4b1be9SMatthew Ahrens 	 * The pool needs to be scrubbed after completing device removal.
34593a4b1be9SMatthew Ahrens 	 * Failure to do so may result in checksum errors due to the
34603a4b1be9SMatthew Ahrens 	 * strategy employed by ztest_fault_inject() when selecting which
34613a4b1be9SMatthew Ahrens 	 * offset are redundant and can be damaged.
34623a4b1be9SMatthew Ahrens 	 */
34633a4b1be9SMatthew Ahrens 	error = spa_scan(spa, POOL_SCAN_SCRUB);
34643a4b1be9SMatthew Ahrens 	if (error == 0) {
34653a4b1be9SMatthew Ahrens 		while (dsl_scan_scrubbing(spa_get_dsl(spa)))
34663a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34673a4b1be9SMatthew Ahrens 	}
34683a4b1be9SMatthew Ahrens 
34693a4b1be9SMatthew Ahrens 	mutex_enter(&ztest_vdev_lock);
34703a4b1be9SMatthew Ahrens 	ztest_device_removal_active = B_FALSE;
3471f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
34725cabbc6bSPrashanth Sreenivasa }
34735cabbc6bSPrashanth Sreenivasa 
3474573ca77eSGeorge Wilson /*
3475573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
3476573ca77eSGeorge Wilson  */
3477573ca77eSGeorge Wilson vdev_t *
3478573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
3479573ca77eSGeorge Wilson {
3480573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3481573ca77eSGeorge Wilson 	size_t *newsize = arg;
3482573ca77eSGeorge Wilson 	size_t fsize;
3483573ca77eSGeorge Wilson 	int fd;
3484573ca77eSGeorge Wilson 
3485573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3486573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3487573ca77eSGeorge Wilson 
3488573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3489573ca77eSGeorge Wilson 		return (vd);
3490573ca77eSGeorge Wilson 
3491573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
3492573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
3493573ca77eSGeorge Wilson 
3494420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3495573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
3496573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3497573ca77eSGeorge Wilson 	}
3498573ca77eSGeorge Wilson 	(void) close(fd);
3499573ca77eSGeorge Wilson 	return (NULL);
3500573ca77eSGeorge Wilson }
3501573ca77eSGeorge Wilson 
3502573ca77eSGeorge Wilson /*
3503573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
3504573ca77eSGeorge Wilson  */
3505573ca77eSGeorge Wilson /* ARGSUSED */
3506573ca77eSGeorge Wilson vdev_t *
3507573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
3508573ca77eSGeorge Wilson {
3509573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3510573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
3511573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
35128f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
3513095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3514095bcd66SGeorge Wilson 	int error;
3515573ca77eSGeorge Wilson 
3516573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3517573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3518573ca77eSGeorge Wilson 
3519573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
3520573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3521095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3522573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3523573ca77eSGeorge Wilson 
3524095bcd66SGeorge Wilson 	/*
3525095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
3526095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
3527095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
3528095bcd66SGeorge Wilson 	 */
3529095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
3530420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3531095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
3532095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
3533095bcd66SGeorge Wilson 		}
3534095bcd66SGeorge Wilson 		return (vd);
3535095bcd66SGeorge Wilson 	}
3536095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3537095bcd66SGeorge Wilson 
3538573ca77eSGeorge Wilson 	/*
3539573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
3540573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
3541573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
3542573ca77eSGeorge Wilson 	 * trying to online it.
3543573ca77eSGeorge Wilson 	 */
35448f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
3545420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
35468f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
35478f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
3548b24ab676SJeff Bonwick 			    "got gen %llu\n",
3549b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
35508f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
35518f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
35528f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
3553573ca77eSGeorge Wilson 		}
3554573ca77eSGeorge Wilson 		return (vd);
3555573ca77eSGeorge Wilson 	}
3556573ca77eSGeorge Wilson 	return (NULL);
3557573ca77eSGeorge Wilson }
3558573ca77eSGeorge Wilson 
3559573ca77eSGeorge Wilson /*
3560573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
3561573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
3562573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3563573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3564573ca77eSGeorge Wilson  * leaf vdev we encounter.
3565573ca77eSGeorge Wilson  */
3566573ca77eSGeorge Wilson vdev_t *
3567573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3568573ca77eSGeorge Wilson {
3569573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3570573ca77eSGeorge Wilson 		if (func == NULL)
3571573ca77eSGeorge Wilson 			return (vd);
3572573ca77eSGeorge Wilson 		else
3573573ca77eSGeorge Wilson 			return (func(vd, arg));
3574573ca77eSGeorge Wilson 	}
3575573ca77eSGeorge Wilson 
3576573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3577573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3578573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3579573ca77eSGeorge Wilson 			return (cvd);
3580573ca77eSGeorge Wilson 	}
3581573ca77eSGeorge Wilson 	return (NULL);
3582573ca77eSGeorge Wilson }
3583573ca77eSGeorge Wilson 
3584fa9e4066Sahrens /*
3585fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3586fa9e4066Sahrens  */
3587b24ab676SJeff Bonwick /* ARGSUSED */
3588fa9e4066Sahrens void
3589b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3590fa9e4066Sahrens {
3591420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3592b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3593b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3594b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3595573ca77eSGeorge Wilson 	size_t psize, newsize;
3596b24ab676SJeff Bonwick 	uint64_t top;
3597b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3598fa9e4066Sahrens 
359986714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
3600f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3601573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3602573ca77eSGeorge Wilson 
36035cabbc6bSPrashanth Sreenivasa 	/*
36045cabbc6bSPrashanth Sreenivasa 	 * If there is a vdev removal in progress, it could complete while
36055cabbc6bSPrashanth Sreenivasa 	 * we are running, in which case we would not be able to verify
36065cabbc6bSPrashanth Sreenivasa 	 * that the metaslab_class space increased (because it decreases
36075cabbc6bSPrashanth Sreenivasa 	 * when the device removal completes).
36085cabbc6bSPrashanth Sreenivasa 	 */
36093a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
361086714001SSerapheim Dimitropoulos 		spa_config_exit(spa, SCL_STATE, spa);
3611f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
361286714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
36135cabbc6bSPrashanth Sreenivasa 		return;
36145cabbc6bSPrashanth Sreenivasa 	}
36155cabbc6bSPrashanth Sreenivasa 
3616b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3617573ca77eSGeorge Wilson 
3618b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3619b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3620b24ab676SJeff Bonwick 	mc = mg->mg_class;
3621b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3622b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3623fa9e4066Sahrens 
3624fa9e4066Sahrens 	/*
3625573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3626573ca77eSGeorge Wilson 	 * our top-level device.
3627fa9e4066Sahrens 	 */
3628573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3629573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3630573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3631fa9e4066Sahrens 
3632573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3633fa9e4066Sahrens 
3634573ca77eSGeorge Wilson 	/*
36358f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
36368f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3637573ca77eSGeorge Wilson 	 */
36388f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3639420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3640573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3641f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
364286714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3643573ca77eSGeorge Wilson 		return;
3644573ca77eSGeorge Wilson 	}
3645573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3646663207adSDon Brady 	newsize = psize + MAX(psize / 8, SPA_MAXBLOCKSIZE);
3647573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3648fa9e4066Sahrens 
3649420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3650b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3651573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3652573ca77eSGeorge Wilson 	}
3653573ca77eSGeorge Wilson 
3654573ca77eSGeorge Wilson 	/*
3655573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3656573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3657573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3658573ca77eSGeorge Wilson 	 */
3659573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3660573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3661573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3662420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3663b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3664b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3665b24ab676SJeff Bonwick 		}
3666b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3667f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
366886714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3669b24ab676SJeff Bonwick 		return;
3670b24ab676SJeff Bonwick 	}
3671b24ab676SJeff Bonwick 
3672b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3673b24ab676SJeff Bonwick 
3674b24ab676SJeff Bonwick 	/*
3675b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3676b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3677b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3678b24ab676SJeff Bonwick 	 */
3679b24ab676SJeff Bonwick 	for (;;) {
3680b24ab676SJeff Bonwick 		boolean_t done;
3681b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3682b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3683b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3684b24ab676SJeff Bonwick 		if (done)
3685b24ab676SJeff Bonwick 			break;
3686b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3687b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3688b24ab676SJeff Bonwick 	}
3689b24ab676SJeff Bonwick 
3690b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3691b24ab676SJeff Bonwick 
3692b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3693b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3694b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3695b24ab676SJeff Bonwick 
3696b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3697420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3698b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3699b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3700fa9e4066Sahrens 		}
3701b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3702f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
370386714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3704573ca77eSGeorge Wilson 		return;
3705fa9e4066Sahrens 	}
3706fa9e4066Sahrens 
3707573ca77eSGeorge Wilson 	/*
3708b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3709573ca77eSGeorge Wilson 	 */
37105cabbc6bSPrashanth Sreenivasa 	if (new_ms_count <= old_ms_count) {
37115cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3712b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
37135cabbc6bSPrashanth Sreenivasa 	}
3714573ca77eSGeorge Wilson 
3715573ca77eSGeorge Wilson 	/*
3716573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3717573ca77eSGeorge Wilson 	 */
37185cabbc6bSPrashanth Sreenivasa 	if (new_class_space <= old_class_space) {
37195cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3720b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
37215cabbc6bSPrashanth Sreenivasa 	}
3722b24ab676SJeff Bonwick 
3723420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
37240a055120SJason King 		char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3725573ca77eSGeorge Wilson 
37260a055120SJason King 		nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
37270a055120SJason King 		nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3728573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3729573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3730573ca77eSGeorge Wilson 	}
3731b24ab676SJeff Bonwick 
3732573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3733f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
373486714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
3735fa9e4066Sahrens }
3736fa9e4066Sahrens 
3737b24ab676SJeff Bonwick /*
3738b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3739b24ab676SJeff Bonwick  */
3740fa9e4066Sahrens /* ARGSUSED */
3741fa9e4066Sahrens static void
3742b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3743fa9e4066Sahrens {
3744fa9e4066Sahrens 	/*
3745b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3746fa9e4066Sahrens 	 */
3747b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3748fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3749fa9e4066Sahrens }
3750fa9e4066Sahrens 
375155da60b9SMark J Musante static int
375255da60b9SMark J Musante ztest_dataset_create(char *dsname)
375355da60b9SMark J Musante {
3754*eb633035STom Caputi 	int err;
3755*eb633035STom Caputi 	uint64_t rand;
3756*eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
3757*eb633035STom Caputi 
3758*eb633035STom Caputi 	/*
3759*eb633035STom Caputi 	 * 50% of the time, we create encrypted datasets
3760*eb633035STom Caputi 	 * using a random cipher suite and a hard-coded
3761*eb633035STom Caputi 	 * wrapping key.
3762*eb633035STom Caputi 	 */
3763*eb633035STom Caputi #ifdef WITHCRYPTO
3764*eb633035STom Caputi 	/*
3765*eb633035STom Caputi 	 * Until the crypto framework is compiled in userland, the ztest using
3766*eb633035STom Caputi 	 * crypto will not work.
3767*eb633035STom Caputi 	 */
3768*eb633035STom Caputi 	rand = ztest_random(2);
3769*eb633035STom Caputi #else
3770*eb633035STom Caputi 	rand = 0;
3771*eb633035STom Caputi #endif
3772*eb633035STom Caputi 	if (rand != 0) {
3773*eb633035STom Caputi 		nvlist_t *crypto_args = fnvlist_alloc();
3774*eb633035STom Caputi 		nvlist_t *props = fnvlist_alloc();
3775*eb633035STom Caputi 
3776*eb633035STom Caputi 		/* slight bias towards the default cipher suite */
3777*eb633035STom Caputi 		rand = ztest_random(ZIO_CRYPT_FUNCTIONS);
3778*eb633035STom Caputi 		if (rand < ZIO_CRYPT_AES_128_CCM)
3779*eb633035STom Caputi 			rand = ZIO_CRYPT_ON;
3780*eb633035STom Caputi 
3781*eb633035STom Caputi 		fnvlist_add_uint64(props,
3782*eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_ENCRYPTION), rand);
3783*eb633035STom Caputi 		fnvlist_add_uint8_array(crypto_args, "wkeydata",
3784*eb633035STom Caputi 		    (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
3785*eb633035STom Caputi 
3786*eb633035STom Caputi 		/*
3787*eb633035STom Caputi 		 * These parameters aren't really used by the kernel. They
3788*eb633035STom Caputi 		 * are simply stored so that userspace knows how to load
3789*eb633035STom Caputi 		 * the wrapping key.
3790*eb633035STom Caputi 		 */
3791*eb633035STom Caputi 		fnvlist_add_uint64(props,
3792*eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_KEYFORMAT), ZFS_KEYFORMAT_RAW);
3793*eb633035STom Caputi 		fnvlist_add_string(props,
3794*eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), "prompt");
3795*eb633035STom Caputi 		fnvlist_add_uint64(props,
3796*eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 0ULL);
3797*eb633035STom Caputi 		fnvlist_add_uint64(props,
3798*eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 0ULL);
3799*eb633035STom Caputi 
3800*eb633035STom Caputi 		VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, props,
3801*eb633035STom Caputi 		    crypto_args, &dcp));
3802*eb633035STom Caputi 
3803*eb633035STom Caputi 		fnvlist_free(crypto_args);
3804*eb633035STom Caputi 		fnvlist_free(props);
3805*eb633035STom Caputi 	}
3806*eb633035STom Caputi 
3807*eb633035STom Caputi 	err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, dcp,
380855da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
3809*eb633035STom Caputi 	dsl_crypto_params_free(dcp, !!err);
381055da60b9SMark J Musante 
3811*eb633035STom Caputi 	rand = ztest_random(100);
3812*eb633035STom Caputi 	if (err || rand < 80)
381355da60b9SMark J Musante 		return (err);
381455da60b9SMark J Musante 
3815420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3816420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
381755da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
381855da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
381955da60b9SMark J Musante }
382055da60b9SMark J Musante 
3821b24ab676SJeff Bonwick /* ARGSUSED */
38221d452cf5Sahrens static int
3823fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3824fa9e4066Sahrens {
3825fa9e4066Sahrens 	objset_t *os;
3826b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3827fa9e4066Sahrens 	int error;
3828fa9e4066Sahrens 
3829fa9e4066Sahrens 	/*
3830fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3831fa9e4066Sahrens 	 */
3832*eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3833*eb633035STom Caputi 	    B_TRUE, FTAG, &os));
3834b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3835e1930233Sbonwick 	if (error != ENOENT) {
3836e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3837fb09f5aaSMadhav Suresh 		ASSERT0(error);
3838b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3839b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3840e1930233Sbonwick 	}
3841*eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
3842fa9e4066Sahrens 
3843fa9e4066Sahrens 	/*
3844fa9e4066Sahrens 	 * Destroy the dataset.
3845fa9e4066Sahrens 	 */
38463b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
3847754998c8SChris Williamson 		VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
38483b2aab18SMatthew Ahrens 	} else {
3849754998c8SChris Williamson 		error = dsl_destroy_head(name);
3850754998c8SChris Williamson 		/* There could be a hold on this dataset */
3851754998c8SChris Williamson 		if (error != EBUSY)
3852754998c8SChris Williamson 			ASSERT0(error);
38533b2aab18SMatthew Ahrens 	}
38541d452cf5Sahrens 	return (0);
3855fa9e4066Sahrens }
3856fa9e4066Sahrens 
3857b24ab676SJeff Bonwick static boolean_t
3858b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3859fa9e4066Sahrens {
38609adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3861b24ab676SJeff Bonwick 	int error;
3862b24ab676SJeff Bonwick 
38633b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3864b24ab676SJeff Bonwick 
38653b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3866b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3867b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3868b24ab676SJeff Bonwick 		return (B_FALSE);
3869b24ab676SJeff Bonwick 	}
38703b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
38713b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
38723b2aab18SMatthew Ahrens 		    snapname, error);
38733b2aab18SMatthew Ahrens 	}
3874b24ab676SJeff Bonwick 	return (B_TRUE);
3875b24ab676SJeff Bonwick }
3876b24ab676SJeff Bonwick 
3877b24ab676SJeff Bonwick static boolean_t
3878b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3879b24ab676SJeff Bonwick {
38809adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3881b24ab676SJeff Bonwick 	int error;
3882b24ab676SJeff Bonwick 
38839adfa60dSMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3884b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3885b24ab676SJeff Bonwick 
38863b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3887b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3888b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3889b24ab676SJeff Bonwick 	return (B_TRUE);
3890fa9e4066Sahrens }
3891fa9e4066Sahrens 
3892b24ab676SJeff Bonwick /* ARGSUSED */
3893fa9e4066Sahrens void
3894b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3895fa9e4066Sahrens {
3896b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3897b24ab676SJeff Bonwick 	int iters;
3898fa9e4066Sahrens 	int error;
3899745cd3c5Smaybee 	objset_t *os, *os2;
39009adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3901fa9e4066Sahrens 	zilog_t *zilog;
3902fa9e4066Sahrens 
3903f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3904b24ab676SJeff Bonwick 
39059adfa60dSMatthew Ahrens 	(void) snprintf(name, sizeof (name), "%s/temp_%llu",
3906420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3907fa9e4066Sahrens 
3908fa9e4066Sahrens 	/*
3909fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3910fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3911b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3912fa9e4066Sahrens 	 */
3913fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3914*eb633035STom Caputi 	    ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3915*eb633035STom Caputi 	    B_TRUE, FTAG, &os) == 0) {
3916420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3917b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3918b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3919*eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, FTAG);
3920fa9e4066Sahrens 	}
3921fa9e4066Sahrens 
3922fa9e4066Sahrens 	/*
3923fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3924fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3925fa9e4066Sahrens 	 * and all of its snapshots.
3926fa9e4066Sahrens 	 */
3927b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
39280b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3929fa9e4066Sahrens 
3930fa9e4066Sahrens 	/*
3931fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3932fa9e4066Sahrens 	 */
3933*eb633035STom Caputi 	VERIFY3U(ENOENT, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3934*eb633035STom Caputi 	    B_TRUE, FTAG, &os));
3935fa9e4066Sahrens 
3936fa9e4066Sahrens 	/*
3937fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3938fa9e4066Sahrens 	 */
393955da60b9SMark J Musante 	error = ztest_dataset_create(name);
3940fa9e4066Sahrens 	if (error) {
3941fa9e4066Sahrens 		if (error == ENOSPC) {
3942b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3943f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
3944fa9e4066Sahrens 			return;
3945fa9e4066Sahrens 		}
3946fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3947fa9e4066Sahrens 	}
3948fa9e4066Sahrens 
3949*eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3950*eb633035STom Caputi 	    FTAG, &os));
3951b24ab676SJeff Bonwick 
3952420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3953fa9e4066Sahrens 
3954fa9e4066Sahrens 	/*
3955fa9e4066Sahrens 	 * Open the intent log for it.
3956fa9e4066Sahrens 	 */
3957b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3958fa9e4066Sahrens 
3959fa9e4066Sahrens 	/*
3960b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3961b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3962fa9e4066Sahrens 	 */
3963b24ab676SJeff Bonwick 	iters = ztest_random(5);
3964b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3965b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3966b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3967b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3968fa9e4066Sahrens 	}
3969fa9e4066Sahrens 
3970fa9e4066Sahrens 	/*
3971fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3972fa9e4066Sahrens 	 */
3973b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3974*eb633035STom Caputi 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
3975fa9e4066Sahrens 
3976fa9e4066Sahrens 	/*
3977503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3978745cd3c5Smaybee 	 */
3979b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3980503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3981503ad85cSMatthew Ahrens 
3982503ad85cSMatthew Ahrens 	/*
3983b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3984503ad85cSMatthew Ahrens 	 */
3985*eb633035STom Caputi 	VERIFY3U(EBUSY, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER,
3986*eb633035STom Caputi 	    B_FALSE, B_TRUE, FTAG, &os2));
3987fa9e4066Sahrens 
3988fa9e4066Sahrens 	zil_close(zilog);
3989*eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
3990b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3991fa9e4066Sahrens 
3992f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
3993fa9e4066Sahrens }
3994fa9e4066Sahrens 
3995fa9e4066Sahrens /*
3996fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3997fa9e4066Sahrens  */
3998fa9e4066Sahrens void
3999b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
4000fa9e4066Sahrens {
4001f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
4002b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
4003b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
4004f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
40054f5064b7SMark J Musante }
40064f5064b7SMark J Musante 
4007718d718aSGeorge Wilson /*
4008718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
4009718d718aSGeorge Wilson  */
4010718d718aSGeorge Wilson void
4011b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
4012718d718aSGeorge Wilson {
40139adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
40149adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
40159adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
40169adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
40179adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
4018718d718aSGeorge Wilson 	int error;
4019718d718aSGeorge Wilson 
40209adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
40219adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
40229adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
40239adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
40249adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
40259adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
40269adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
40279adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
40289adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
40299adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
4030718d718aSGeorge Wilson 
40313b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
4032718d718aSGeorge Wilson 	if (error && error != ENOENT)
40333b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
40343b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
4035718d718aSGeorge Wilson 	if (error && error != ENOENT)
40363b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
40373b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
4038718d718aSGeorge Wilson 	if (error && error != ENOENT)
40393b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
40403b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
4041718d718aSGeorge Wilson 	if (error && error != ENOENT)
40423b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
40433b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
4044718d718aSGeorge Wilson 	if (error && error != ENOENT)
40453b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
4046718d718aSGeorge Wilson }
4047718d718aSGeorge Wilson 
40484f5064b7SMark J Musante /*
40494f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
40504f5064b7SMark J Musante  */
40514f5064b7SMark J Musante void
4052b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
40534f5064b7SMark J Musante {
40543b2aab18SMatthew Ahrens 	objset_t *os;
40559adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
40569adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
40579adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
40589adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
40599adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
4060b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
4061b24ab676SJeff Bonwick 	int error;
40624f5064b7SMark J Musante 
4063f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
40644f5064b7SMark J Musante 
4065b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
4066718d718aSGeorge Wilson 
40679adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
40689adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
40699adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
40709adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
40719adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
40729adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
40739adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
40749adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
40759adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
40769adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
407705312e2cSMark J Musante 
40784445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
4079b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4080b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4081b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4082b2d22b66SGeorge Wilson 			goto out;
4083b2d22b66SGeorge Wilson 		}
4084b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
4085b2d22b66SGeorge Wilson 	}
40864f5064b7SMark J Musante 
40873b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
4088b2d22b66SGeorge Wilson 	if (error) {
4089b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4090b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4091b2d22b66SGeorge Wilson 			goto out;
4092b2d22b66SGeorge Wilson 		}
4093b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
4094b2d22b66SGeorge Wilson 	}
40954f5064b7SMark J Musante 
40964445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4097b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4098b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4099b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4100b2d22b66SGeorge Wilson 			goto out;
4101b2d22b66SGeorge Wilson 		}
4102b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4103b2d22b66SGeorge Wilson 	}
41044f5064b7SMark J Musante 
41054445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4106b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4107b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4108b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4109b2d22b66SGeorge Wilson 			goto out;
4110b2d22b66SGeorge Wilson 		}
4111b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4112b2d22b66SGeorge Wilson 	}
41134f5064b7SMark J Musante 
41143b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
4115b2d22b66SGeorge Wilson 	if (error) {
4116b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4117b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4118b2d22b66SGeorge Wilson 			goto out;
4119b2d22b66SGeorge Wilson 		}
4120b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4121b2d22b66SGeorge Wilson 	}
41224f5064b7SMark J Musante 
4123*eb633035STom Caputi 	error = ztest_dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
4124*eb633035STom Caputi 	    FTAG, &os);
41254f5064b7SMark J Musante 	if (error)
41263b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4127681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
41285d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
4129*eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, FTAG);
41305d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
41315d7b4d43SMatthew Ahrens 		goto out;
41325d7b4d43SMatthew Ahrens 	}
41334f5064b7SMark J Musante 	if (error != EBUSY)
41344f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
41354f5064b7SMark J Musante 		    error);
4136*eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
41374f5064b7SMark J Musante 
4138b2d22b66SGeorge Wilson out:
4139b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
41404f5064b7SMark J Musante 
4141f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
4142fa9e4066Sahrens }
4143fa9e4066Sahrens 
4144fa9e4066Sahrens /*
4145fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
4146fa9e4066Sahrens  */
4147fa9e4066Sahrens void
4148b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4149fa9e4066Sahrens {
4150b24ab676SJeff Bonwick 	ztest_od_t od[4];
4151b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
4152fa9e4066Sahrens 
415354811da5SToomas Soome 	for (int b = 0; b < batchsize; b++) {
415454811da5SToomas Soome 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
415554811da5SToomas Soome 		    0, 0, 0);
415654811da5SToomas Soome 	}
4157fa9e4066Sahrens 
4158fa9e4066Sahrens 	/*
4159b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
4160b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
4161fa9e4066Sahrens 	 */
4162b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
4163b24ab676SJeff Bonwick 		return;
4164fa9e4066Sahrens 
4165b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
4166b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
4167b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4168fa9e4066Sahrens }
4169fa9e4066Sahrens 
417054811da5SToomas Soome /*
417154811da5SToomas Soome  * Rewind the global allocator to verify object allocation backfilling.
417254811da5SToomas Soome  */
417354811da5SToomas Soome void
417454811da5SToomas Soome ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
417554811da5SToomas Soome {
417654811da5SToomas Soome 	objset_t *os = zd->zd_os;
417754811da5SToomas Soome 	int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
417854811da5SToomas Soome 	uint64_t object;
417954811da5SToomas Soome 
418054811da5SToomas Soome 	/*
418154811da5SToomas Soome 	 * Rewind the global allocator randomly back to a lower object number
418254811da5SToomas Soome 	 * to force backfilling and reclamation of recently freed dnodes.
418354811da5SToomas Soome 	 */
418454811da5SToomas Soome 	mutex_enter(&os->os_obj_lock);
418554811da5SToomas Soome 	object = ztest_random(os->os_obj_next_chunk);
418654811da5SToomas Soome 	os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
418754811da5SToomas Soome 	mutex_exit(&os->os_obj_lock);
418854811da5SToomas Soome }
418954811da5SToomas Soome 
4190fa9e4066Sahrens /*
4191fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
4192fa9e4066Sahrens  */
4193fa9e4066Sahrens void
4194b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4195fa9e4066Sahrens {
4196b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4197b24ab676SJeff Bonwick 	ztest_od_t od[2];
4198fa9e4066Sahrens 	dmu_tx_t *tx;
4199fa9e4066Sahrens 	int i, freeit, error;
4200fa9e4066Sahrens 	uint64_t n, s, txg;
4201fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4202b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4203b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4204fa9e4066Sahrens 	uint64_t regions = 997;
4205fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
4206fa9e4066Sahrens 	uint64_t width = 40;
4207fa9e4066Sahrens 	int free_percent = 5;
4208fa9e4066Sahrens 
4209fa9e4066Sahrens 	/*
4210fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
4211fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
4212fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
4213fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
4214fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
4215fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
4216fa9e4066Sahrens 	 *
4217fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
4218fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
4219fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
4220fa9e4066Sahrens 	 *
4221fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
4222fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
4223fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
4224fa9e4066Sahrens 	 *
4225fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
4226fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
4227fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
4228fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
4229fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
4230fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
4231fa9e4066Sahrens 	 */
4232fa9e4066Sahrens 
4233fa9e4066Sahrens 	/*
4234fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
4235fa9e4066Sahrens 	 */
423654811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
423754811da5SToomas Soome 	    chunksize);
423854811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
423954811da5SToomas Soome 	    chunksize);
4240fa9e4066Sahrens 
4241b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4242b24ab676SJeff Bonwick 		return;
4243fa9e4066Sahrens 
4244b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4245b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4246b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
4247b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
4248fa9e4066Sahrens 
4249fa9e4066Sahrens 	/*
4250fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
4251fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
4252fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
4253fa9e4066Sahrens 	 * handle this race correctly.
4254fa9e4066Sahrens 	 */
4255fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4256fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
4257a2cdcdd2SPaul Dagnelie 	dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4258a2cdcdd2SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_READ);
4259fa9e4066Sahrens 
4260fa9e4066Sahrens 	/*
4261fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
4262fa9e4066Sahrens 	 */
4263fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4264fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
4265fa9e4066Sahrens 
4266fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
4267fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
4268fa9e4066Sahrens 
4269b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4270b24ab676SJeff Bonwick 	bigsize = s * chunksize;
4271fa9e4066Sahrens 
4272fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4273fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4274fa9e4066Sahrens 
4275fa9e4066Sahrens 	/*
4276fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
4277fa9e4066Sahrens 	 * overwriting it.
4278fa9e4066Sahrens 	 */
4279fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
4280fa9e4066Sahrens 
4281fa9e4066Sahrens 	/*
4282fa9e4066Sahrens 	 * Read the current contents of our objects.
4283fa9e4066Sahrens 	 */
4284b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
42857bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4286fb09f5aaSMadhav Suresh 	ASSERT0(error);
4287b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
42887bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4289fb09f5aaSMadhav Suresh 	ASSERT0(error);
4290fa9e4066Sahrens 
4291fa9e4066Sahrens 	/*
4292fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
4293fa9e4066Sahrens 	 */
4294fa9e4066Sahrens 	tx = dmu_tx_create(os);
4295fa9e4066Sahrens 
4296b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
4297fa9e4066Sahrens 
4298fa9e4066Sahrens 	if (freeit)
4299b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4300fa9e4066Sahrens 	else
4301b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4302fa9e4066Sahrens 
4303be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
4304be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
4305be9000ccSMatthew Ahrens 
4306b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4307b24ab676SJeff Bonwick 	if (txg == 0) {
4308fa9e4066Sahrens 		umem_free(packbuf, packsize);
4309fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
4310fa9e4066Sahrens 		return;
4311fa9e4066Sahrens 	}
4312fa9e4066Sahrens 
43135d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
43145d7b4d43SMatthew Ahrens 	do {
43155d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
43165d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
43175d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
43185d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
4319b24ab676SJeff Bonwick 
43205d7b4d43SMatthew Ahrens 	enum zio_compress comp;
43215d7b4d43SMatthew Ahrens 	do {
43225d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
43235d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
43245d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
43255d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
4326fa9e4066Sahrens 
4327fa9e4066Sahrens 	/*
4328fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
4329fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
4330fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
4331fa9e4066Sahrens 	 * with the new values we want to write out.
4332fa9e4066Sahrens 	 */
4333fa9e4066Sahrens 	for (i = 0; i < s; i++) {
4334fa9e4066Sahrens 		/* LINTED */
4335fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4336fa9e4066Sahrens 		/* LINTED */
4337b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4338fa9e4066Sahrens 		/* LINTED */
4339b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4340fa9e4066Sahrens 
4341fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4342fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4343fa9e4066Sahrens 
4344fa9e4066Sahrens 		if (pack->bw_txg > txg)
4345fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
4346fa9e4066Sahrens 			    pack->bw_txg, txg);
4347fa9e4066Sahrens 
4348fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
4349fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4350fa9e4066Sahrens 			    pack->bw_index, n, i);
4351fa9e4066Sahrens 
4352fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4353fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4354fa9e4066Sahrens 
4355fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4356fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4357fa9e4066Sahrens 
4358fa9e4066Sahrens 		if (freeit) {
4359fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
4360fa9e4066Sahrens 		} else {
4361fa9e4066Sahrens 			pack->bw_index = n + i;
4362fa9e4066Sahrens 			pack->bw_txg = txg;
4363fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
4364fa9e4066Sahrens 		}
4365fa9e4066Sahrens 		*bigH = *pack;
4366fa9e4066Sahrens 		*bigT = *pack;
4367fa9e4066Sahrens 	}
4368fa9e4066Sahrens 
4369fa9e4066Sahrens 	/*
4370fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
4371fa9e4066Sahrens 	 * Now write them out.
4372fa9e4066Sahrens 	 */
4373b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4374fa9e4066Sahrens 
4375fa9e4066Sahrens 	if (freeit) {
4376420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4377fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
4378fa9e4066Sahrens 			    " txg %llx\n",
4379fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4380fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4381fa9e4066Sahrens 			    (u_longlong_t)txg);
4382fa9e4066Sahrens 		}
4383b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4384fa9e4066Sahrens 	} else {
4385420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4386fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
4387fa9e4066Sahrens 			    " txg %llx\n",
4388fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4389fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4390fa9e4066Sahrens 			    (u_longlong_t)txg);
4391fa9e4066Sahrens 		}
4392b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4393fa9e4066Sahrens 	}
4394fa9e4066Sahrens 
4395fa9e4066Sahrens 	dmu_tx_commit(tx);
4396fa9e4066Sahrens 
4397fa9e4066Sahrens 	/*
4398fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
4399fa9e4066Sahrens 	 */
4400fa9e4066Sahrens 	{
4401fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4402fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4403fa9e4066Sahrens 
4404b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
44057bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
4406b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
44077bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
4408fa9e4066Sahrens 
4409fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4410fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4411fa9e4066Sahrens 
4412fa9e4066Sahrens 		umem_free(packcheck, packsize);
4413fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
4414fa9e4066Sahrens 	}
4415fa9e4066Sahrens 
4416fa9e4066Sahrens 	umem_free(packbuf, packsize);
4417fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
4418fa9e4066Sahrens }
4419fa9e4066Sahrens 
44202fdbea25SAleksandr Guzovskiy void
44212fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4422b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
44232fdbea25SAleksandr Guzovskiy {
44242fdbea25SAleksandr Guzovskiy 	uint64_t i;
44252fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
44262fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
44272fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
44282fdbea25SAleksandr Guzovskiy 
44292fdbea25SAleksandr Guzovskiy 	/*
44302fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
44312fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
44322fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
44332fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
44342fdbea25SAleksandr Guzovskiy 	 */
44352fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
44362fdbea25SAleksandr Guzovskiy 		/* LINTED */
44372fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
44382fdbea25SAleksandr Guzovskiy 		/* LINTED */
4439b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
44402fdbea25SAleksandr Guzovskiy 		/* LINTED */
4441b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
44422fdbea25SAleksandr Guzovskiy 
44432fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
44442fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
44452fdbea25SAleksandr Guzovskiy 
44462fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
44472fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
44482fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
44492fdbea25SAleksandr Guzovskiy 
44502fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
44512fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
44522fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
44532fdbea25SAleksandr Guzovskiy 
44542fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
44552fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
44562fdbea25SAleksandr Guzovskiy 
44572fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
44582fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
44592fdbea25SAleksandr Guzovskiy 
44602fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
44612fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
44622fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
44632fdbea25SAleksandr Guzovskiy 
44642fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
44652fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
44662fdbea25SAleksandr Guzovskiy 	}
44672fdbea25SAleksandr Guzovskiy }
44682fdbea25SAleksandr Guzovskiy 
44692fdbea25SAleksandr Guzovskiy void
4470b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
44712fdbea25SAleksandr Guzovskiy {
4472b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4473b24ab676SJeff Bonwick 	ztest_od_t od[2];
44742fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
44752fdbea25SAleksandr Guzovskiy 	uint64_t i;
44762fdbea25SAleksandr Guzovskiy 	int error;
44772fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
44782fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
4479b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4480b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4481b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
44822fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
44832fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
44842fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
44852fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
44862fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
4487b24ab676SJeff Bonwick 	dmu_object_info_t doi;
44882fdbea25SAleksandr Guzovskiy 
44892fdbea25SAleksandr Guzovskiy 	/*
44902fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
44912fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
44922fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
44932fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
44942fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
44952fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
44962fdbea25SAleksandr Guzovskiy 	 *
44972fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
44982fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
44992fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
45002fdbea25SAleksandr Guzovskiy 	 *
45012fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
4502*eb633035STom Caputi 	 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
45032fdbea25SAleksandr Guzovskiy 	 */
45042fdbea25SAleksandr Guzovskiy 
45052fdbea25SAleksandr Guzovskiy 	/*
45062fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
45072fdbea25SAleksandr Guzovskiy 	 */
450854811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
450954811da5SToomas Soome 	    0, 0);
451054811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
451154811da5SToomas Soome 	    chunksize);
45122fdbea25SAleksandr Guzovskiy 
4513b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4514b24ab676SJeff Bonwick 		return;
45152fdbea25SAleksandr Guzovskiy 
4516b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4517b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4518b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
4519b24ab676SJeff Bonwick 	chunksize = blocksize;
4520b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
45212fdbea25SAleksandr Guzovskiy 
4522b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4523b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
4524b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
4525b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
45262fdbea25SAleksandr Guzovskiy 
45272fdbea25SAleksandr Guzovskiy 	/*
45282fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
45292fdbea25SAleksandr Guzovskiy 	 */
45302fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
45312fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
45322fdbea25SAleksandr Guzovskiy 
45332fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
45342fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
45352fdbea25SAleksandr Guzovskiy 
4536b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4537b24ab676SJeff Bonwick 	bigsize = s * chunksize;
45382fdbea25SAleksandr Guzovskiy 
45392fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
45402fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
45412fdbea25SAleksandr Guzovskiy 
4542b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
45432fdbea25SAleksandr Guzovskiy 
45442fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
45452fdbea25SAleksandr Guzovskiy 
45462fdbea25SAleksandr Guzovskiy 	/*
45472fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
45482fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
45492fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
45502fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
45512fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
45522fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
45532fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
45542fdbea25SAleksandr Guzovskiy 	 */
45552fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
45562fdbea25SAleksandr Guzovskiy 		uint64_t j;
45572fdbea25SAleksandr Guzovskiy 		uint64_t off;
45582fdbea25SAleksandr Guzovskiy 
45592fdbea25SAleksandr Guzovskiy 		/*
45602fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
45612fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
4562*eb633035STom Caputi 		 * dmu_assign_arcbuf_by_dbuf() when it can't directly
45632fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
45642fdbea25SAleksandr Guzovskiy 		 */
45652fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
456689c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
45672fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
4568b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
45692fdbea25SAleksandr Guzovskiy 			} else {
45702fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
4571b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
45722fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
4573b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
45742fdbea25SAleksandr Guzovskiy 			}
45752fdbea25SAleksandr Guzovskiy 		}
45762fdbea25SAleksandr Guzovskiy 
45772fdbea25SAleksandr Guzovskiy 		/*
45782fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
45792fdbea25SAleksandr Guzovskiy 		 */
45802fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
45812fdbea25SAleksandr Guzovskiy 
4582b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
4583b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
45842fdbea25SAleksandr Guzovskiy 
4585b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4586b24ab676SJeff Bonwick 		if (txg == 0) {
45872fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
45882fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
45892fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
459089c86e32SChris Williamson 				if (i != 5 ||
459189c86e32SChris Williamson 				    chunksize < (SPA_MINBLOCKSIZE * 2)) {
45922fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
45932fdbea25SAleksandr Guzovskiy 				} else {
45942fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
45952fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
45962fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
45972fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
45982fdbea25SAleksandr Guzovskiy 				}
45992fdbea25SAleksandr Guzovskiy 			}
46002fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
46012fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
46022fdbea25SAleksandr Guzovskiy 			return;
46032fdbea25SAleksandr Guzovskiy 		}
46042fdbea25SAleksandr Guzovskiy 
46052fdbea25SAleksandr Guzovskiy 		/*
46062fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
4607*eb633035STom Caputi 		 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4608*eb633035STom Caputi 		 * no existing dbufs for the specified offsets.
46092fdbea25SAleksandr Guzovskiy 		 */
46102fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
4611b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
46127bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
4613fb09f5aaSMadhav Suresh 			ASSERT0(error);
4614b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
46157bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
4616fb09f5aaSMadhav Suresh 			ASSERT0(error);
46172fdbea25SAleksandr Guzovskiy 		}
46182fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4619b24ab676SJeff Bonwick 		    n, chunksize, txg);
46202fdbea25SAleksandr Guzovskiy 
46212fdbea25SAleksandr Guzovskiy 		/*
46222fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
46232fdbea25SAleksandr Guzovskiy 		 * Now write them out.
46242fdbea25SAleksandr Guzovskiy 		 */
4625b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4626420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
46272fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
46282fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
46292fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
46302fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
46312fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
46322fdbea25SAleksandr Guzovskiy 		}
4633b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
46342fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
463589c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
46362fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
4637b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
46382fdbea25SAleksandr Guzovskiy 			} else {
46392fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
46402fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
4641b24ab676SJeff Bonwick 				    chunksize / 2);
46422fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
4643b24ab676SJeff Bonwick 				    chunksize / 2,
46442fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
4645b24ab676SJeff Bonwick 				    chunksize / 2);
46462fdbea25SAleksandr Guzovskiy 			}
46472fdbea25SAleksandr Guzovskiy 
46482fdbea25SAleksandr Guzovskiy 			if (i == 1) {
4649b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
465047cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
46512fdbea25SAleksandr Guzovskiy 			}
465289c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4653*eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db, off,
46542fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
46552fdbea25SAleksandr Guzovskiy 			} else {
4656*eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db, off,
46572fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
4658*eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db,
4659b24ab676SJeff Bonwick 				    off + chunksize / 2,
46602fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
46612fdbea25SAleksandr Guzovskiy 			}
46622fdbea25SAleksandr Guzovskiy 			if (i == 1) {
46632fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
46642fdbea25SAleksandr Guzovskiy 			}
46652fdbea25SAleksandr Guzovskiy 		}
46662fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
46672fdbea25SAleksandr Guzovskiy 
46682fdbea25SAleksandr Guzovskiy 		/*
46692fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
46702fdbea25SAleksandr Guzovskiy 		 */
46712fdbea25SAleksandr Guzovskiy 		{
46722fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
46732fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
46742fdbea25SAleksandr Guzovskiy 
4675b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
46767bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
4677b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
46787bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
46792fdbea25SAleksandr Guzovskiy 
46802fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
46812fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
46822fdbea25SAleksandr Guzovskiy 
46832fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
46842fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
46852fdbea25SAleksandr Guzovskiy 		}
46862fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4687b24ab676SJeff Bonwick 			txg_wait_open(dmu_objset_pool(os), 0);
4688b24ab676SJeff Bonwick 		} else if (i == 3) {
4689b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4690fa9e4066Sahrens 		}
4691e05725b1Sbonwick 	}
4692e05725b1Sbonwick 
4693b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4694b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4695b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4696b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4697b24ab676SJeff Bonwick }
4698fa9e4066Sahrens 
4699b24ab676SJeff Bonwick /* ARGSUSED */
4700b24ab676SJeff Bonwick void
4701b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4702b24ab676SJeff Bonwick {
4703b24ab676SJeff Bonwick 	ztest_od_t od[1];
4704b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4705b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4706fa9e4066Sahrens 
4707e05725b1Sbonwick 	/*
4708b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4709b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4710b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4711e05725b1Sbonwick 	 */
471254811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
471354811da5SToomas Soome 	    0, 0, 0);
4714fa9e4066Sahrens 
4715b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4716e05725b1Sbonwick 		return;
4717fa9e4066Sahrens 
4718b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4719b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4720b24ab676SJeff Bonwick }
4721fa9e4066Sahrens 
4722b24ab676SJeff Bonwick void
4723b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4724b24ab676SJeff Bonwick {
4725b24ab676SJeff Bonwick 	ztest_od_t od[1];
4726b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4727b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4728b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4729b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4730b24ab676SJeff Bonwick 	void *data;
4731fa9e4066Sahrens 
473254811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
473354811da5SToomas Soome 	    0, 0);
4734fa9e4066Sahrens 
4735b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4736e05725b1Sbonwick 		return;
47370e34b6a7Sbonwick 
4738b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4739f4a72450SJeff Bonwick 		return;
4740f4a72450SJeff Bonwick 
4741b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4742fa9e4066Sahrens 
4743b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4744b24ab676SJeff Bonwick 
4745b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4746b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4747b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4748b24ab676SJeff Bonwick 		    data) != 0)
4749b24ab676SJeff Bonwick 			break;
4750b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4751b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4752b24ab676SJeff Bonwick 	}
4753b24ab676SJeff Bonwick 
4754b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4755fa9e4066Sahrens }
4756fa9e4066Sahrens 
4757fa9e4066Sahrens /*
4758fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4759fa9e4066Sahrens  */
4760fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4761fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4762fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4763fa9e4066Sahrens 
4764fa9e4066Sahrens void
4765b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4766fa9e4066Sahrens {
4767b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4768b24ab676SJeff Bonwick 	ztest_od_t od[1];
4769fa9e4066Sahrens 	uint64_t object;
4770fa9e4066Sahrens 	uint64_t txg, last_txg;
4771fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4772fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4773fa9e4066Sahrens 	int i, ints;
4774fa9e4066Sahrens 	dmu_tx_t *tx;
4775fa9e4066Sahrens 	char propname[100], txgname[100];
4776fa9e4066Sahrens 	int error;
4777fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4778fa9e4066Sahrens 
477954811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4780fa9e4066Sahrens 
4781b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4782b24ab676SJeff Bonwick 		return;
4783fa9e4066Sahrens 
4784b24ab676SJeff Bonwick 	object = od[0].od_object;
4785fa9e4066Sahrens 
4786b24ab676SJeff Bonwick 	/*
4787b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4788b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4789b24ab676SJeff Bonwick 	 */
4790b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4791b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4792b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4793b24ab676SJeff Bonwick 	if (txg == 0)
4794b24ab676SJeff Bonwick 		return;
4795b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4796b24ab676SJeff Bonwick 		value[i] = i;
4797b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4798b24ab676SJeff Bonwick 		    1, &value[i], tx));
4799b24ab676SJeff Bonwick 	}
4800b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4801b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4802b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4803b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4804b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4805b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4806b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4807fa9e4066Sahrens 	}
4808b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4809b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4810b24ab676SJeff Bonwick 	}
4811b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4812fa9e4066Sahrens 
4813b24ab676SJeff Bonwick 	/*
4814b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4815b24ab676SJeff Bonwick 	 */
4816fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4817fa9e4066Sahrens 
4818e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4819e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4820e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4821e05725b1Sbonwick 	bzero(value, sizeof (value));
4822e05725b1Sbonwick 	last_txg = 0;
4823fa9e4066Sahrens 
4824e05725b1Sbonwick 	/*
4825e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4826e05725b1Sbonwick 	 */
4827e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4828e05725b1Sbonwick 	if (error == 0) {
4829e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4830e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4831fa9e4066Sahrens 
4832e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4833e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4834fa9e4066Sahrens 
4835e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4836e05725b1Sbonwick 		    &zl_ints) == 0);
4837fa9e4066Sahrens 
4838e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4839e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4840fa9e4066Sahrens 
4841e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4842e05725b1Sbonwick 		    zl_ints, value) == 0);
4843fa9e4066Sahrens 
4844e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4845e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4846fa9e4066Sahrens 		}
4847e05725b1Sbonwick 	} else {
4848e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4849e05725b1Sbonwick 	}
4850fa9e4066Sahrens 
4851e05725b1Sbonwick 	/*
4852e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4853e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4854e05725b1Sbonwick 	 * in which the property was last updated.  The second
4855e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4856e05725b1Sbonwick 	 * should be txg + object + n.
4857e05725b1Sbonwick 	 */
4858e05725b1Sbonwick 	tx = dmu_tx_create(os);
4859b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4860b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4861b24ab676SJeff Bonwick 	if (txg == 0)
4862e05725b1Sbonwick 		return;
4863fa9e4066Sahrens 
4864e05725b1Sbonwick 	if (last_txg > txg)
4865e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4866fa9e4066Sahrens 
4867e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4868e05725b1Sbonwick 		value[i] = txg + object + i;
4869fa9e4066Sahrens 
4870b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4871b24ab676SJeff Bonwick 	    1, &txg, tx));
4872b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4873b24ab676SJeff Bonwick 	    ints, value, tx));
4874fa9e4066Sahrens 
4875e05725b1Sbonwick 	dmu_tx_commit(tx);
4876fa9e4066Sahrens 
4877e05725b1Sbonwick 	/*
4878e05725b1Sbonwick 	 * Remove a random pair of entries.
4879e05725b1Sbonwick 	 */
4880e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4881e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4882e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4883fa9e4066Sahrens 
4884e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4885fa9e4066Sahrens 
4886e05725b1Sbonwick 	if (error == ENOENT)
4887e05725b1Sbonwick 		return;
4888fa9e4066Sahrens 
4889fb09f5aaSMadhav Suresh 	ASSERT0(error);
4890fa9e4066Sahrens 
4891e05725b1Sbonwick 	tx = dmu_tx_create(os);
4892b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4893b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4894b24ab676SJeff Bonwick 	if (txg == 0)
489512a2833aSSanjeev Bagewadi 		return;
4896b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4897b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
489812a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
489912a2833aSSanjeev Bagewadi }
490012a2833aSSanjeev Bagewadi 
490112a2833aSSanjeev Bagewadi /*
490212a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
490312a2833aSSanjeev Bagewadi  */
490412a2833aSSanjeev Bagewadi void
4905b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
490612a2833aSSanjeev Bagewadi {
4907b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4908b24ab676SJeff Bonwick 	ztest_od_t od[1];
4909b24ab676SJeff Bonwick 	uint64_t object, txg;
491012a2833aSSanjeev Bagewadi 
491154811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
491212a2833aSSanjeev Bagewadi 
4913b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4914b24ab676SJeff Bonwick 		return;
491512a2833aSSanjeev Bagewadi 
4916b24ab676SJeff Bonwick 	object = od[0].od_object;
491712a2833aSSanjeev Bagewadi 
491812a2833aSSanjeev Bagewadi 	/*
4919b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
492012a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4921b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
492212a2833aSSanjeev Bagewadi 	 */
4923b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
49249adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
4925b24ab676SJeff Bonwick 		uint64_t value = i;
4926b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4927b24ab676SJeff Bonwick 		int error;
492812a2833aSSanjeev Bagewadi 
4929b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4930b24ab676SJeff Bonwick 		    id, value);
493112a2833aSSanjeev Bagewadi 
4932b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4933b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4934b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4935b24ab676SJeff Bonwick 		if (txg == 0)
493612a2833aSSanjeev Bagewadi 			return;
4937b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4938b24ab676SJeff Bonwick 		    &value, tx);
493912a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
494012a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
494112a2833aSSanjeev Bagewadi 	}
4942fa9e4066Sahrens }
4943fa9e4066Sahrens 
4944b24ab676SJeff Bonwick /* ARGSUSED */
4945fa9e4066Sahrens void
4946b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4947fa9e4066Sahrens {
4948b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4949b24ab676SJeff Bonwick 	ztest_od_t od[1];
4950fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4951fa9e4066Sahrens 	dmu_tx_t *tx;
4952fa9e4066Sahrens 	int i, namelen, error;
4953b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4954fa9e4066Sahrens 	char name[20], string_value[20];
4955fa9e4066Sahrens 	void *data;
4956fa9e4066Sahrens 
495754811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
495854811da5SToomas Soome 	    0, 0, 0);
4959b24ab676SJeff Bonwick 
4960b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4961b24ab676SJeff Bonwick 		return;
4962b24ab676SJeff Bonwick 
4963b24ab676SJeff Bonwick 	object = od[0].od_object;
4964b24ab676SJeff Bonwick 
4965e05725b1Sbonwick 	/*
4966e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4967e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4968e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4969e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4970e05725b1Sbonwick 	 */
4971e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4972fa9e4066Sahrens 
4973e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4974e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4975e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4976e05725b1Sbonwick 		name[i] = '.';
4977e05725b1Sbonwick 	name[i] = '\0';
4978fa9e4066Sahrens 
4979b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4980e05725b1Sbonwick 		wsize = sizeof (txg);
4981e05725b1Sbonwick 		wc = 1;
4982e05725b1Sbonwick 		data = &txg;
4983e05725b1Sbonwick 	} else {
4984e05725b1Sbonwick 		wsize = 1;
4985e05725b1Sbonwick 		wc = namelen;
4986e05725b1Sbonwick 		data = string_value;
4987e05725b1Sbonwick 	}
4988fa9e4066Sahrens 
4989e05725b1Sbonwick 	count = -1ULL;
49903b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4991e05725b1Sbonwick 	ASSERT(count != -1ULL);
4992fa9e4066Sahrens 
4993e05725b1Sbonwick 	/*
4994e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4995e05725b1Sbonwick 	 */
4996e05725b1Sbonwick 	i = ztest_random(5);
4997e05725b1Sbonwick 
4998e05725b1Sbonwick 	if (i >= 2) {
4999e05725b1Sbonwick 		tx = dmu_tx_create(os);
5000b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
5001b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5002b24ab676SJeff Bonwick 		if (txg == 0)
5003e05725b1Sbonwick 			return;
5004e05725b1Sbonwick 		bcopy(name, string_value, namelen);
5005e05725b1Sbonwick 	} else {
5006e05725b1Sbonwick 		tx = NULL;
5007e05725b1Sbonwick 		txg = 0;
5008e05725b1Sbonwick 		bzero(string_value, namelen);
5009e05725b1Sbonwick 	}
5010fa9e4066Sahrens 
5011e05725b1Sbonwick 	switch (i) {
5012fa9e4066Sahrens 
5013e05725b1Sbonwick 	case 0:
5014e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
5015e05725b1Sbonwick 		if (error == 0) {
5016e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
5017e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
5018e05725b1Sbonwick 		} else {
5019e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
5020e05725b1Sbonwick 		}
5021e05725b1Sbonwick 		break;
5022fa9e4066Sahrens 
5023e05725b1Sbonwick 	case 1:
5024e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
5025e05725b1Sbonwick 		if (error == 0) {
5026e05725b1Sbonwick 			if (data == string_value &&
5027e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
5028e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
5029e05725b1Sbonwick 				    name, data, namelen);
5030e05725b1Sbonwick 		} else {
5031e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
5032e05725b1Sbonwick 		}
5033e05725b1Sbonwick 		break;
5034fa9e4066Sahrens 
5035e05725b1Sbonwick 	case 2:
5036e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
5037e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
5038e05725b1Sbonwick 		break;
5039fa9e4066Sahrens 
5040e05725b1Sbonwick 	case 3:
5041e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
5042e05725b1Sbonwick 		break;
5043fa9e4066Sahrens 
5044e05725b1Sbonwick 	case 4:
5045e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
5046e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
5047e05725b1Sbonwick 		break;
5048fa9e4066Sahrens 	}
5049e05725b1Sbonwick 
5050e05725b1Sbonwick 	if (tx != NULL)
5051e05725b1Sbonwick 		dmu_tx_commit(tx);
5052fa9e4066Sahrens }
5053fa9e4066Sahrens 
5054d20e665cSRicardo M. Correia /*
5055d20e665cSRicardo M. Correia  * Commit callback data.
5056d20e665cSRicardo M. Correia  */
5057d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
5058d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
5059d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
5060d20e665cSRicardo M. Correia 	int			zcd_expected_err;
5061d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
5062d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
5063d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
5064d20e665cSRicardo M. Correia } ztest_cb_data_t;
5065d20e665cSRicardo M. Correia 
5066d20e665cSRicardo M. Correia /* This is the actual commit callback function */
5067d20e665cSRicardo M. Correia static void
5068d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
5069d20e665cSRicardo M. Correia {
5070d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
5071d20e665cSRicardo M. Correia 	uint64_t synced_txg;
5072d20e665cSRicardo M. Correia 
5073d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
5074d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
5075d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
5076d20e665cSRicardo M. Correia 
5077d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
5078d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
5079d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
5080d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
5081d20e665cSRicardo M. Correia 		    synced_txg);
5082d20e665cSRicardo M. Correia 
5083d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
5084d20e665cSRicardo M. Correia 
5085d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
5086fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
5087d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
5088d20e665cSRicardo M. Correia 
5089d20e665cSRicardo M. Correia 		/*
5090d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
5091d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
5092d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
5093d20e665cSRicardo M. Correia 		 */
5094d20e665cSRicardo M. Correia 		return;
5095d20e665cSRicardo M. Correia 	}
5096d20e665cSRicardo M. Correia 
5097d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
5098d20e665cSRicardo M. Correia 	if (!data->zcd_added)
5099d20e665cSRicardo M. Correia 		goto out;
5100d20e665cSRicardo M. Correia 
5101d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
5102d20e665cSRicardo M. Correia 
5103d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
5104f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5105d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
5106f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5107d20e665cSRicardo M. Correia 
5108d20e665cSRicardo M. Correia out:
5109d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
5110d20e665cSRicardo M. Correia }
5111d20e665cSRicardo M. Correia 
5112d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
5113d20e665cSRicardo M. Correia static ztest_cb_data_t *
5114d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
5115d20e665cSRicardo M. Correia {
5116d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
5117d20e665cSRicardo M. Correia 
5118d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5119d20e665cSRicardo M. Correia 
5120d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
5121d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
5122d20e665cSRicardo M. Correia 
5123d20e665cSRicardo M. Correia 	return (cb_data);
5124d20e665cSRicardo M. Correia }
5125d20e665cSRicardo M. Correia 
5126d20e665cSRicardo M. Correia /*
5127d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
5128d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
5129d20e665cSRicardo M. Correia  * implementation bug.
5130d20e665cSRicardo M. Correia  */
5131d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
5132d20e665cSRicardo M. Correia 
5133d20e665cSRicardo M. Correia /*
5134d20e665cSRicardo M. Correia  * Commit callback test.
5135d20e665cSRicardo M. Correia  */
5136d20e665cSRicardo M. Correia void
5137b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5138d20e665cSRicardo M. Correia {
5139b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5140b24ab676SJeff Bonwick 	ztest_od_t od[1];
5141d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
5142d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
5143d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
5144d20e665cSRicardo M. Correia 	int i, error;
5145d20e665cSRicardo M. Correia 
514654811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5147b24ab676SJeff Bonwick 
5148b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5149b24ab676SJeff Bonwick 		return;
5150b24ab676SJeff Bonwick 
5151d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
5152d20e665cSRicardo M. Correia 
5153d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
5154d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5155d20e665cSRicardo M. Correia 
5156b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
5157d20e665cSRicardo M. Correia 
5158d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
5159d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
5160d20e665cSRicardo M. Correia 		error = -1;
5161d20e665cSRicardo M. Correia 
5162d20e665cSRicardo M. Correia 	if (!error)
5163d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
5164d20e665cSRicardo M. Correia 
5165d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
5166d20e665cSRicardo M. Correia 
5167d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
5168d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
5169d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5170d20e665cSRicardo M. Correia 
5171d20e665cSRicardo M. Correia 	if (error) {
5172d20e665cSRicardo M. Correia 		/*
5173d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
5174d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
5175d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
5176d20e665cSRicardo M. Correia 		 * so we will check for that.
5177d20e665cSRicardo M. Correia 		 */
5178d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5179d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
5180d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
5181d20e665cSRicardo M. Correia 		}
5182d20e665cSRicardo M. Correia 
5183d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
5184d20e665cSRicardo M. Correia 
5185d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5186d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
5187d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5188d20e665cSRicardo M. Correia 		}
5189d20e665cSRicardo M. Correia 
5190d20e665cSRicardo M. Correia 		return;
5191d20e665cSRicardo M. Correia 	}
5192d20e665cSRicardo M. Correia 
5193d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
5194d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5195d20e665cSRicardo M. Correia 
5196d20e665cSRicardo M. Correia 	/*
5197d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
5198d20e665cSRicardo M. Correia 	 */
5199b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
5200d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
5201d20e665cSRicardo M. Correia 
5202d20e665cSRicardo M. Correia 	if (old_txg > txg)
5203d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5204d20e665cSRicardo M. Correia 		    old_txg, txg);
5205d20e665cSRicardo M. Correia 
5206b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
5207d20e665cSRicardo M. Correia 
5208f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5209d20e665cSRicardo M. Correia 
5210d20e665cSRicardo M. Correia 	/*
5211d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
5212d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
5213d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
5214d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
5215d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
5216d20e665cSRicardo M. Correia 	 * implementation.
5217d20e665cSRicardo M. Correia 	 *
5218d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
5219d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
5220d20e665cSRicardo M. Correia 	 * implementation bug..
5221d20e665cSRicardo M. Correia 	 */
5222d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
5223d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
5224b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
5225d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5226d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5227d20e665cSRicardo M. Correia 	}
5228d20e665cSRicardo M. Correia 
5229d20e665cSRicardo M. Correia 	/*
5230d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
5231d20e665cSRicardo M. Correia 	 *
5232d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
5233d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
5234d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
5235d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
5236d20e665cSRicardo M. Correia 	 */
5237d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
5238d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5239d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5240d20e665cSRicardo M. Correia 
5241d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
5242d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
5243d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
5244d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5245d20e665cSRicardo M. Correia 		else
5246d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5247d20e665cSRicardo M. Correia 			    cb_data[i]);
5248d20e665cSRicardo M. Correia 
5249d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
5250d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
5251d20e665cSRicardo M. Correia 
5252d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
5253d20e665cSRicardo M. Correia 	}
5254d20e665cSRicardo M. Correia 
5255f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5256d20e665cSRicardo M. Correia 
5257d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
5258d20e665cSRicardo M. Correia }
5259d20e665cSRicardo M. Correia 
526054811da5SToomas Soome /*
526154811da5SToomas Soome  * Visit each object in the dataset. Verify that its properties
526254811da5SToomas Soome  * are consistent what was stored in the block tag when it was created,
526354811da5SToomas Soome  * and that its unused bonus buffer space has not been overwritten.
526454811da5SToomas Soome  */
526554811da5SToomas Soome void
526654811da5SToomas Soome ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
526754811da5SToomas Soome {
526854811da5SToomas Soome 	objset_t *os = zd->zd_os;
526954811da5SToomas Soome 	uint64_t obj;
527054811da5SToomas Soome 	int err = 0;
527154811da5SToomas Soome 
527254811da5SToomas Soome 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
527354811da5SToomas Soome 		ztest_block_tag_t *bt = NULL;
527454811da5SToomas Soome 		dmu_object_info_t doi;
527554811da5SToomas Soome 		dmu_buf_t *db;
527654811da5SToomas Soome 
527754811da5SToomas Soome 		if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
527854811da5SToomas Soome 			continue;
527954811da5SToomas Soome 
528054811da5SToomas Soome 		dmu_object_info_from_db(db, &doi);
528154811da5SToomas Soome 		if (doi.doi_bonus_size >= sizeof (*bt))
528254811da5SToomas Soome 			bt = ztest_bt_bonus(db);
528354811da5SToomas Soome 
528454811da5SToomas Soome 		if (bt && bt->bt_magic == BT_MAGIC) {
528554811da5SToomas Soome 			ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
528654811da5SToomas Soome 			    bt->bt_offset, bt->bt_gen, bt->bt_txg,
528754811da5SToomas Soome 			    bt->bt_crtxg);
528854811da5SToomas Soome 			ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
528954811da5SToomas Soome 		}
529054811da5SToomas Soome 
529154811da5SToomas Soome 		dmu_buf_rele(db, FTAG);
529254811da5SToomas Soome 	}
529354811da5SToomas Soome }
529454811da5SToomas Soome 
5295b24ab676SJeff Bonwick /* ARGSUSED */
5296fa9e4066Sahrens void
5297b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5298fa9e4066Sahrens {
5299b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
5300b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
5301b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
5302b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
5303b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
5304b24ab676SJeff Bonwick 	};
5305fa9e4066Sahrens 
5306f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5307fa9e4066Sahrens 
5308b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5309b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5310b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5311fa9e4066Sahrens 
5312f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5313b24ab676SJeff Bonwick }
5314fa9e4066Sahrens 
53155cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
53165cabbc6bSPrashanth Sreenivasa void
53175cabbc6bSPrashanth Sreenivasa ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
53185cabbc6bSPrashanth Sreenivasa {
5319f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
53205cabbc6bSPrashanth Sreenivasa 
53215cabbc6bSPrashanth Sreenivasa 	int error = dmu_objset_remap_indirects(zd->zd_name);
53225cabbc6bSPrashanth Sreenivasa 	if (error == ENOSPC)
53235cabbc6bSPrashanth Sreenivasa 		error = 0;
53245cabbc6bSPrashanth Sreenivasa 	ASSERT0(error);
53255cabbc6bSPrashanth Sreenivasa 
5326f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
53275cabbc6bSPrashanth Sreenivasa }
53285cabbc6bSPrashanth Sreenivasa 
5329b24ab676SJeff Bonwick /* ARGSUSED */
5330b24ab676SJeff Bonwick void
5331b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5332b24ab676SJeff Bonwick {
5333b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
5334ea8dc4b6Seschrock 
5335f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5336ea8dc4b6Seschrock 
5337420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5338b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5339fa9e4066Sahrens 
5340fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
5341fa9e4066Sahrens 
5342420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5343b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
5344fa9e4066Sahrens 
5345b24ab676SJeff Bonwick 	nvlist_free(props);
5346fa9e4066Sahrens 
5347f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5348fa9e4066Sahrens }
5349fa9e4066Sahrens 
53503b2aab18SMatthew Ahrens static int
53513b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
53523b2aab18SMatthew Ahrens {
53533b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
53543b2aab18SMatthew Ahrens 	int error;
53553b2aab18SMatthew Ahrens 
53563b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
53573b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
53583b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
53593b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
53603b2aab18SMatthew Ahrens 	fnvlist_free(holds);
53613b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
53623b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
53633b2aab18SMatthew Ahrens 	return (error);
53643b2aab18SMatthew Ahrens }
53653b2aab18SMatthew Ahrens 
53665c987a37SChris Kirby /*
53675c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
53685c987a37SChris Kirby  */
53695c987a37SChris Kirby void
5370b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
53715c987a37SChris Kirby {
53725c987a37SChris Kirby 	int error;
5373b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
53745c987a37SChris Kirby 	objset_t *origin;
53755c987a37SChris Kirby 	char snapname[100];
53765c987a37SChris Kirby 	char fullname[100];
53775c987a37SChris Kirby 	char clonename[100];
53785c987a37SChris Kirby 	char tag[100];
53799adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
53803b2aab18SMatthew Ahrens 	nvlist_t *holds;
53815c987a37SChris Kirby 
5382f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
53835c987a37SChris Kirby 
53845c987a37SChris Kirby 	dmu_objset_name(os, osname);
53855c987a37SChris Kirby 
53863b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
53873b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
53883b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
53893b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
53903b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
53915c987a37SChris Kirby 
53925c987a37SChris Kirby 	/*
53935c987a37SChris Kirby 	 * Clean up from any previous run.
53945c987a37SChris Kirby 	 */
53953b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
53963b2aab18SMatthew Ahrens 	if (error != ENOENT)
53973b2aab18SMatthew Ahrens 		ASSERT0(error);
53983b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
53993b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
54003b2aab18SMatthew Ahrens 		ASSERT0(error);
54013b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
54023b2aab18SMatthew Ahrens 	if (error != ENOENT)
54033b2aab18SMatthew Ahrens 		ASSERT0(error);
54045c987a37SChris Kirby 
54055c987a37SChris Kirby 	/*
54065c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
54075c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
54085c987a37SChris Kirby 	 */
54094445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
54105c987a37SChris Kirby 	if (error) {
54115c987a37SChris Kirby 		if (error == ENOSPC) {
54125c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
54135c987a37SChris Kirby 			goto out;
54145c987a37SChris Kirby 		}
54155c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
54165c987a37SChris Kirby 	}
54175c987a37SChris Kirby 
54183b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
54195c987a37SChris Kirby 	if (error) {
54205c987a37SChris Kirby 		if (error == ENOSPC) {
54215c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
54225c987a37SChris Kirby 			goto out;
54235c987a37SChris Kirby 		}
54245c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
54255c987a37SChris Kirby 	}
54265c987a37SChris Kirby 
54273b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
54285c987a37SChris Kirby 	if (error) {
54293b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
54305c987a37SChris Kirby 		    fullname, error);
54315c987a37SChris Kirby 	}
54325c987a37SChris Kirby 
54333b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
54345c987a37SChris Kirby 	if (error)
54353b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
54365c987a37SChris Kirby 
54375c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
54385c987a37SChris Kirby 	if (error != ENOENT)
54395c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
54405c987a37SChris Kirby 
54415c987a37SChris Kirby 	/*
54425c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
54435c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
54445c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
54455c987a37SChris Kirby 	 */
54464445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
54475c987a37SChris Kirby 	if (error) {
54485c987a37SChris Kirby 		if (error == ENOSPC) {
54495c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
54505c987a37SChris Kirby 			goto out;
54515c987a37SChris Kirby 		}
54525c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
54535c987a37SChris Kirby 	}
54545c987a37SChris Kirby 
54553b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
54563b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
54573b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
54583b2aab18SMatthew Ahrens 	fnvlist_free(holds);
54593b2aab18SMatthew Ahrens 
54605d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
54615d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
54625d7b4d43SMatthew Ahrens 		goto out;
54635d7b4d43SMatthew Ahrens 	} else if (error) {
54645d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
54655d7b4d43SMatthew Ahrens 		    fullname, tag, error);
54665d7b4d43SMatthew Ahrens 	}
54675c987a37SChris Kirby 
54683b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
54695c987a37SChris Kirby 	if (error != EBUSY) {
54703b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
54715c987a37SChris Kirby 		    fullname, error);
54725c987a37SChris Kirby 	}
54735c987a37SChris Kirby 
54743b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
54755c987a37SChris Kirby 	if (error) {
54763b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
54775c987a37SChris Kirby 		    fullname, error);
54785c987a37SChris Kirby 	}
54795c987a37SChris Kirby 
54803b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
54815c987a37SChris Kirby 	if (error)
5482a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
54835c987a37SChris Kirby 
54843b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
54855c987a37SChris Kirby 
54865c987a37SChris Kirby out:
5487f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
54885c987a37SChris Kirby }
54895c987a37SChris Kirby 
5490fa9e4066Sahrens /*
5491fa9e4066Sahrens  * Inject random faults into the on-disk data.
5492fa9e4066Sahrens  */
5493b24ab676SJeff Bonwick /* ARGSUSED */
5494fa9e4066Sahrens void
5495b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5496fa9e4066Sahrens {
5497b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5498420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5499fa9e4066Sahrens 	int fd;
5500fa9e4066Sahrens 	uint64_t offset;
55011195e687SMark J Musante 	uint64_t leaves;
5502fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
5503fa9e4066Sahrens 	uint64_t top, leaf;
5504fa9e4066Sahrens 	char path0[MAXPATHLEN];
5505fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
5506fa9e4066Sahrens 	size_t fsize;
5507f34284d8SMatthew Ahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;
5508fa9e4066Sahrens 	int iters = 1000;
55091195e687SMark J Musante 	int maxfaults;
55101195e687SMark J Musante 	int mirror_save;
5511e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
5512ea8dc4b6Seschrock 	uint64_t guid0 = 0;
55138f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
5514ea8dc4b6Seschrock 
5515f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
55163a4b1be9SMatthew Ahrens 
55173a4b1be9SMatthew Ahrens 	/*
55183a4b1be9SMatthew Ahrens 	 * Device removal is in progress, fault injection must be disabled
55193a4b1be9SMatthew Ahrens 	 * until it completes and the pool is scrubbed.  The fault injection
55203a4b1be9SMatthew Ahrens 	 * strategy for damaging blocks does not take in to account evacuated
55213a4b1be9SMatthew Ahrens 	 * blocks which may have already been damaged.
55223a4b1be9SMatthew Ahrens 	 */
55233a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
55243a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
55253a4b1be9SMatthew Ahrens 		return;
55263a4b1be9SMatthew Ahrens 	}
55273a4b1be9SMatthew Ahrens 
55281195e687SMark J Musante 	maxfaults = MAXFAULTS();
5529420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
55301195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
5531f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
55321195e687SMark J Musante 
5533e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
5534fa9e4066Sahrens 
55352c1e2b44SGeorge Wilson 	/*
55362c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
55372c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
55382c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
55392c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
55402c1e2b44SGeorge Wilson 	 */
5541f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
55422c1e2b44SGeorge Wilson 
5543fa9e4066Sahrens 	/*
5544e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5545fa9e4066Sahrens 	 */
5546e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5547fa9e4066Sahrens 
5548e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
5549e14bb325SJeff Bonwick 		/*
5550b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
5551e14bb325SJeff Bonwick 		 */
5552b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
55531195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
5554fa9e4066Sahrens 
5555e14bb325SJeff Bonwick 		/*
5556e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
5557e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
5558e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
5559e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
5560e14bb325SJeff Bonwick 		 */
5561e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
5562420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5563420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
5564e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5565420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5566420dfc95SChris Siden 		    top * leaves + leaf);
5567fa9e4066Sahrens 
5568e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
55698f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
55708f18d1faSGeorge Wilson 			islog = B_TRUE;
55718f18d1faSGeorge Wilson 
55722c1e2b44SGeorge Wilson 		/*
55732c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
55742c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
55752c1e2b44SGeorge Wilson 		 * resilvering.
55762c1e2b44SGeorge Wilson 		 */
55772c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
55782c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5579b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
5580e14bb325SJeff Bonwick 			/*
5581e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
5582e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
5583e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
5584e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
5585e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
5586e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
5587e14bb325SJeff Bonwick 			 * combination of this with injection of random data
5588e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
5589e14bb325SJeff Bonwick 			 */
5590e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
5591e14bb325SJeff Bonwick 
55925cabbc6bSPrashanth Sreenivasa 			zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
55935cabbc6bSPrashanth Sreenivasa 			    (long long)vd0->vdev_id, (int)maxfaults);
55945cabbc6bSPrashanth Sreenivasa 
5595e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
5596e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
5597e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
5598e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
5599e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
5600e14bb325SJeff Bonwick 			} else {
5601e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
5602e14bb325SJeff Bonwick 			}
5603e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
5604e14bb325SJeff Bonwick 		}
5605e14bb325SJeff Bonwick 	} else {
5606e14bb325SJeff Bonwick 		/*
5607e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
5608e14bb325SJeff Bonwick 		 */
5609e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
5610fa9e4066Sahrens 
5611e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
5612e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
5613f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
5614e14bb325SJeff Bonwick 			return;
5615e14bb325SJeff Bonwick 		}
5616e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5617ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
5618e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
5619e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
5620e14bb325SJeff Bonwick 
5621e14bb325SJeff Bonwick 		leaf = 0;
5622e14bb325SJeff Bonwick 		leaves = 1;
5623e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
5624fa9e4066Sahrens 	}
5625fa9e4066Sahrens 
5626e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
5627f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5628e14bb325SJeff Bonwick 
5629fa9e4066Sahrens 	/*
56308f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
56318f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
5632fa9e4066Sahrens 	 */
56338f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
56348ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
56358ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
56368ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
56378f18d1faSGeorge Wilson 
56388f18d1faSGeorge Wilson 			/*
56398f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
56408f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
56418f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
56428f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
56438f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
56448f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
56458f18d1faSGeorge Wilson 			 */
56468f18d1faSGeorge Wilson 			if (islog)
5647f06dce2cSAndrew Stormont 				rw_enter(&ztest_name_lock, RW_WRITER);
56488f18d1faSGeorge Wilson 
56498ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
56508f18d1faSGeorge Wilson 
56518f18d1faSGeorge Wilson 			if (islog)
5652f06dce2cSAndrew Stormont 				rw_exit(&ztest_name_lock);
56538ad4d6ddSJeff Bonwick 		} else {
56549253d63dSGeorge Wilson 			/*
56559253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
56569253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
56579253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
56589253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
56599253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
56609253d63dSGeorge Wilson 			 * prevent a race between injection testing and
56619253d63dSGeorge Wilson 			 * aux_vdev removal.
56629253d63dSGeorge Wilson 			 */
5663f06dce2cSAndrew Stormont 			mutex_enter(&ztest_vdev_lock);
56648ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
5665f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
56668ad4d6ddSJeff Bonwick 		}
5667fa9e4066Sahrens 	}
5668fa9e4066Sahrens 
56698f18d1faSGeorge Wilson 	if (maxfaults == 0)
56708f18d1faSGeorge Wilson 		return;
56718f18d1faSGeorge Wilson 
5672fa9e4066Sahrens 	/*
5673ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
5674fa9e4066Sahrens 	 */
5675fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
5676fa9e4066Sahrens 
567786714001SSerapheim Dimitropoulos 	if (fd == -1) /* we hit a gap in the device namespace */
5678fa9e4066Sahrens 		return;
5679fa9e4066Sahrens 
5680fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
5681fa9e4066Sahrens 
5682fa9e4066Sahrens 	while (--iters != 0) {
5683f9eb9fdfSMatthew Ahrens 		/*
5684f9eb9fdfSMatthew Ahrens 		 * The offset must be chosen carefully to ensure that
5685f9eb9fdfSMatthew Ahrens 		 * we do not inject a given logical block with errors
5686f9eb9fdfSMatthew Ahrens 		 * on two different leaf devices, because ZFS can not
5687f9eb9fdfSMatthew Ahrens 		 * tolerate that (if maxfaults==1).
5688f9eb9fdfSMatthew Ahrens 		 *
5689f9eb9fdfSMatthew Ahrens 		 * We divide each leaf into chunks of size
5690f9eb9fdfSMatthew Ahrens 		 * (# leaves * SPA_MAXBLOCKSIZE * 4).  Within each chunk
5691f9eb9fdfSMatthew Ahrens 		 * there is a series of ranges to which we can inject errors.
5692f9eb9fdfSMatthew Ahrens 		 * Each range can accept errors on only a single leaf vdev.
5693f9eb9fdfSMatthew Ahrens 		 * The error injection ranges are separated by ranges
5694f9eb9fdfSMatthew Ahrens 		 * which we will not inject errors on any device (DMZs).
5695f9eb9fdfSMatthew Ahrens 		 * Each DMZ must be large enough such that a single block
5696f9eb9fdfSMatthew Ahrens 		 * can not straddle it, so that a single block can not be
5697f9eb9fdfSMatthew Ahrens 		 * a target in two different injection ranges (on different
5698f9eb9fdfSMatthew Ahrens 		 * leaf vdevs).
5699f9eb9fdfSMatthew Ahrens 		 *
5700f9eb9fdfSMatthew Ahrens 		 * For example, with 3 leaves, each chunk looks like:
5701f9eb9fdfSMatthew Ahrens 		 *    0 to  32M: injection range for leaf 0
5702f9eb9fdfSMatthew Ahrens 		 *  32M to  64M: DMZ - no injection allowed
5703f9eb9fdfSMatthew Ahrens 		 *  64M to  96M: injection range for leaf 1
5704f9eb9fdfSMatthew Ahrens 		 *  96M to 128M: DMZ - no injection allowed
5705f9eb9fdfSMatthew Ahrens 		 * 128M to 160M: injection range for leaf 2
5706f9eb9fdfSMatthew Ahrens 		 * 160M to 192M: DMZ - no injection allowed
5707f9eb9fdfSMatthew Ahrens 		 */
5708fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
5709fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
5710fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5711fa9e4066Sahrens 
5712f34284d8SMatthew Ahrens 		/*
5713f34284d8SMatthew Ahrens 		 * Only allow damage to the labels at one end of the vdev.
5714f34284d8SMatthew Ahrens 		 *
5715f34284d8SMatthew Ahrens 		 * If all labels are damaged, the device will be totally
5716f34284d8SMatthew Ahrens 		 * inaccessible, which will result in loss of data,
5717f34284d8SMatthew Ahrens 		 * because we also damage (parts of) the other side of
5718f34284d8SMatthew Ahrens 		 * the mirror/raidz.
5719f34284d8SMatthew Ahrens 		 *
5720f34284d8SMatthew Ahrens 		 * Additionally, we will always have both an even and an
5721f34284d8SMatthew Ahrens 		 * odd label, so that we can handle crashes in the
5722f34284d8SMatthew Ahrens 		 * middle of vdev_config_sync().
5723f34284d8SMatthew Ahrens 		 */
5724f34284d8SMatthew Ahrens 		if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5725f34284d8SMatthew Ahrens 			continue;
5726f34284d8SMatthew Ahrens 
5727f34284d8SMatthew Ahrens 		/*
5728f34284d8SMatthew Ahrens 		 * The two end labels are stored at the "end" of the disk, but
5729f34284d8SMatthew Ahrens 		 * the end of the disk (vdev_psize) is aligned to
5730f34284d8SMatthew Ahrens 		 * sizeof (vdev_label_t).
5731f34284d8SMatthew Ahrens 		 */
5732f34284d8SMatthew Ahrens 		uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5733f34284d8SMatthew Ahrens 		if ((leaf & 1) == 1 &&
5734f34284d8SMatthew Ahrens 		    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5735fa9e4066Sahrens 			continue;
5736fa9e4066Sahrens 
5737f06dce2cSAndrew Stormont 		mutex_enter(&ztest_vdev_lock);
57381195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
5739f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
57401195e687SMark J Musante 			(void) close(fd);
57411195e687SMark J Musante 			return;
57421195e687SMark J Musante 		}
5743fa9e4066Sahrens 
5744fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5745fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
5746fa9e4066Sahrens 			    offset, pathrand);
57471195e687SMark J Musante 
5748f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
57491195e687SMark J Musante 
5750420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
57511195e687SMark J Musante 			(void) printf("injected bad word into %s,"
57521195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5753fa9e4066Sahrens 	}
5754fa9e4066Sahrens 
5755fa9e4066Sahrens 	(void) close(fd);
5756fa9e4066Sahrens }
5757fa9e4066Sahrens 
5758fa9e4066Sahrens /*
5759b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
5760fa9e4066Sahrens  */
5761fa9e4066Sahrens void
5762b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5763fa9e4066Sahrens {
5764b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5765420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5766b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5767b24ab676SJeff Bonwick 	ztest_od_t od[1];
5768b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
5769b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
5770b24ab676SJeff Bonwick 	dmu_buf_t *db;
5771b24ab676SJeff Bonwick 	dmu_tx_t *tx;
5772770499e1SDan Kimmel 	abd_t *abd;
5773b24ab676SJeff Bonwick 	blkptr_t blk;
5774b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
5775fa9e4066Sahrens 
5776b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
5777b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
5778fa9e4066Sahrens 
577954811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
578054811da5SToomas Soome 	    0, 0);
5781fa9e4066Sahrens 
5782b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5783b24ab676SJeff Bonwick 		return;
5784fa9e4066Sahrens 
5785fa9e4066Sahrens 	/*
5786b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
5787b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
5788fa9e4066Sahrens 	 */
5789f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5790fa9e4066Sahrens 
5791b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5792b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
5793b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5794b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
5795f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5796b24ab676SJeff Bonwick 		return;
5797b24ab676SJeff Bonwick 	}
5798b24ab676SJeff Bonwick 
5799aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
5800aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5801aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5802aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5803aab80726SGeorge Wilson 
5804b24ab676SJeff Bonwick 	object = od[0].od_object;
5805b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5806aab80726SGeorge Wilson 	pattern = zs->zs_guid ^ dds.dds_guid;
5807b24ab676SJeff Bonwick 
5808b24ab676SJeff Bonwick 	ASSERT(object != 0);
5809b24ab676SJeff Bonwick 
5810b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5811b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5812b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5813b24ab676SJeff Bonwick 	if (txg == 0) {
5814f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5815b24ab676SJeff Bonwick 		return;
5816b24ab676SJeff Bonwick 	}
5817fa9e4066Sahrens 
5818fa9e4066Sahrens 	/*
5819b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5820fa9e4066Sahrens 	 */
5821b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5822b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
58233b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
58243b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
58253b2aab18SMatthew Ahrens 		if (error != 0) {
58263b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
58273b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
58283b2aab18SMatthew Ahrens 		}
5829b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5830b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5831b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5832b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5833b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5834b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5835b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5836b24ab676SJeff Bonwick 	}
5837fa9e4066Sahrens 
5838b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5839b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5840fa9e4066Sahrens 
5841fa9e4066Sahrens 	/*
5842b24ab676SJeff Bonwick 	 * Find out what block we got.
5843fa9e4066Sahrens 	 */
584480901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
584580901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5846b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5847b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5848fa9e4066Sahrens 
5849fa9e4066Sahrens 	/*
5850b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5851fa9e4066Sahrens 	 */
5852b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5853770499e1SDan Kimmel 	abd = abd_alloc_linear(psize, B_TRUE);
5854770499e1SDan Kimmel 	ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5855fa9e4066Sahrens 
5856b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5857770499e1SDan Kimmel 	    abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5858b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5859fa9e4066Sahrens 
5860770499e1SDan Kimmel 	abd_free(abd);
5861fa9e4066Sahrens 
5862f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5863fa9e4066Sahrens }
5864fa9e4066Sahrens 
5865fa9e4066Sahrens /*
5866b24ab676SJeff Bonwick  * Scrub the pool.
5867fa9e4066Sahrens  */
5868b24ab676SJeff Bonwick /* ARGSUSED */
5869b24ab676SJeff Bonwick void
5870b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5871fa9e4066Sahrens {
5872420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5873fa9e4066Sahrens 
58743a4b1be9SMatthew Ahrens 	/*
58753a4b1be9SMatthew Ahrens 	 * Scrub in progress by device removal.
58763a4b1be9SMatthew Ahrens 	 */
58773a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active)
58783a4b1be9SMatthew Ahrens 		return;
58793a4b1be9SMatthew Ahrens 
58803f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5881b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
58823f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5883b24ab676SJeff Bonwick }
5884fa9e4066Sahrens 
5885e9103aaeSGarrett D'Amore /*
5886e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5887e9103aaeSGarrett D'Amore  */
5888e9103aaeSGarrett D'Amore /* ARGSUSED */
5889e9103aaeSGarrett D'Amore void
5890e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5891e9103aaeSGarrett D'Amore {
5892420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5893e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5894dfbb9432SGeorge Wilson 	int error;
5895e9103aaeSGarrett D'Amore 
5896e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
5897e0f1c0afSOlaf Faaland 		return;
5898e0f1c0afSOlaf Faaland 
5899e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5900e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5901dfbb9432SGeorge Wilson 
5902f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5903dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5904f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5905dfbb9432SGeorge Wilson 
5906dfbb9432SGeorge Wilson 	if (error != 0)
5907e9103aaeSGarrett D'Amore 		return;
5908e9103aaeSGarrett D'Amore 
590925345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5910e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5911e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5912e9103aaeSGarrett D'Amore 	}
5913e9103aaeSGarrett D'Amore 
5914e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5915e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5916e9103aaeSGarrett D'Amore }
5917e9103aaeSGarrett D'Amore 
5918094e47e9SGeorge Wilson static vdev_t *
5919094e47e9SGeorge Wilson ztest_random_concrete_vdev_leaf(vdev_t *vd)
5920094e47e9SGeorge Wilson {
5921094e47e9SGeorge Wilson 	if (vd == NULL)
5922094e47e9SGeorge Wilson 		return (NULL);
5923094e47e9SGeorge Wilson 
5924094e47e9SGeorge Wilson 	if (vd->vdev_children == 0)
5925094e47e9SGeorge Wilson 		return (vd);
5926094e47e9SGeorge Wilson 
5927094e47e9SGeorge Wilson 	vdev_t *eligible[vd->vdev_children];
5928094e47e9SGeorge Wilson 	int eligible_idx = 0, i;
5929094e47e9SGeorge Wilson 	for (i = 0; i < vd->vdev_children; i++) {
5930094e47e9SGeorge Wilson 		vdev_t *cvd = vd->vdev_child[i];
5931094e47e9SGeorge Wilson 		if (cvd->vdev_top->vdev_removing)
5932094e47e9SGeorge Wilson 			continue;
5933094e47e9SGeorge Wilson 		if (cvd->vdev_children > 0 ||
5934094e47e9SGeorge Wilson 		    (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5935094e47e9SGeorge Wilson 			eligible[eligible_idx++] = cvd;
5936094e47e9SGeorge Wilson 		}
5937094e47e9SGeorge Wilson 	}
5938094e47e9SGeorge Wilson 	VERIFY(eligible_idx > 0);
5939094e47e9SGeorge Wilson 
5940094e47e9SGeorge Wilson 	uint64_t child_no = ztest_random(eligible_idx);
5941094e47e9SGeorge Wilson 	return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5942094e47e9SGeorge Wilson }
5943094e47e9SGeorge Wilson 
5944094e47e9SGeorge Wilson /* ARGSUSED */
5945094e47e9SGeorge Wilson void
5946094e47e9SGeorge Wilson ztest_initialize(ztest_ds_t *zd, uint64_t id)
5947094e47e9SGeorge Wilson {
5948094e47e9SGeorge Wilson 	spa_t *spa = ztest_spa;
5949094e47e9SGeorge Wilson 	int error = 0;
5950094e47e9SGeorge Wilson 
5951094e47e9SGeorge Wilson 	mutex_enter(&ztest_vdev_lock);
5952094e47e9SGeorge Wilson 
5953094e47e9SGeorge Wilson 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5954094e47e9SGeorge Wilson 
5955094e47e9SGeorge Wilson 	/* Random leaf vdev */
5956094e47e9SGeorge Wilson 	vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5957094e47e9SGeorge Wilson 	if (rand_vd == NULL) {
5958094e47e9SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
5959094e47e9SGeorge Wilson 		mutex_exit(&ztest_vdev_lock);
5960094e47e9SGeorge Wilson 		return;
5961094e47e9SGeorge Wilson 	}
5962094e47e9SGeorge Wilson 
5963094e47e9SGeorge Wilson 	/*
5964094e47e9SGeorge Wilson 	 * The random vdev we've selected may change as soon as we
5965094e47e9SGeorge Wilson 	 * drop the spa_config_lock. We create local copies of things
5966094e47e9SGeorge Wilson 	 * we're interested in.
5967094e47e9SGeorge Wilson 	 */
5968094e47e9SGeorge Wilson 	uint64_t guid = rand_vd->vdev_guid;
5969094e47e9SGeorge Wilson 	char *path = strdup(rand_vd->vdev_path);
5970094e47e9SGeorge Wilson 	boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5971094e47e9SGeorge Wilson 
5972094e47e9SGeorge Wilson 	zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5973094e47e9SGeorge Wilson 	spa_config_exit(spa, SCL_VDEV, FTAG);
5974094e47e9SGeorge Wilson 
5975094e47e9SGeorge Wilson 	uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5976094e47e9SGeorge Wilson 	error = spa_vdev_initialize(spa, guid, cmd);
5977094e47e9SGeorge Wilson 	switch (cmd) {
5978094e47e9SGeorge Wilson 	case POOL_INITIALIZE_CANCEL:
5979094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5980094e47e9SGeorge Wilson 			(void) printf("Cancel initialize %s", path);
5981094e47e9SGeorge Wilson 			if (!active)
5982094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5983094e47e9SGeorge Wilson 			(void) printf("\n");
5984094e47e9SGeorge Wilson 		}
5985094e47e9SGeorge Wilson 		break;
5986094e47e9SGeorge Wilson 	case POOL_INITIALIZE_DO:
5987094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5988094e47e9SGeorge Wilson 			(void) printf("Start initialize %s", path);
5989094e47e9SGeorge Wilson 			if (active && error == 0)
5990094e47e9SGeorge Wilson 				(void) printf(" failed (already active)");
5991094e47e9SGeorge Wilson 			else if (error != 0)
5992094e47e9SGeorge Wilson 				(void) printf(" failed (error %d)", error);
5993094e47e9SGeorge Wilson 			(void) printf("\n");
5994094e47e9SGeorge Wilson 		}
5995094e47e9SGeorge Wilson 		break;
5996094e47e9SGeorge Wilson 	case POOL_INITIALIZE_SUSPEND:
5997094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5998094e47e9SGeorge Wilson 			(void) printf("Suspend initialize %s", path);
5999094e47e9SGeorge Wilson 			if (!active)
6000094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
6001094e47e9SGeorge Wilson 			(void) printf("\n");
6002094e47e9SGeorge Wilson 		}
6003094e47e9SGeorge Wilson 		break;
6004094e47e9SGeorge Wilson 	}
6005094e47e9SGeorge Wilson 	free(path);
6006094e47e9SGeorge Wilson 	mutex_exit(&ztest_vdev_lock);
6007094e47e9SGeorge Wilson }
6008094e47e9SGeorge Wilson 
6009b24ab676SJeff Bonwick /*
6010b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
6011b24ab676SJeff Bonwick  */
6012fa9e4066Sahrens static void
6013b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
6014fa9e4066Sahrens {
6015fa9e4066Sahrens 	int status;
6016fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
6017fa9e4066Sahrens 	char zbuf[1024];
6018fa9e4066Sahrens 	char *bin;
60198654d025Sperrin 	char *ztest;
60208654d025Sperrin 	char *isa;
60218654d025Sperrin 	int isalen;
6022fa9e4066Sahrens 	FILE *fp;
6023fa9e4066Sahrens 
6024fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
6025fa9e4066Sahrens 
6026fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
6027fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
60288654d025Sperrin 	ztest = strstr(bin, "/ztest");
60298654d025Sperrin 	isa = bin + 8;
60308654d025Sperrin 	isalen = ztest - isa;
60318654d025Sperrin 	isa = strdup(isa);
6032fa9e4066Sahrens 	/* LINTED */
60333a57275aSck 	(void) sprintf(bin,
6034a21fe349SBrian Behlendorf 	    "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s "
6035a21fe349SBrian Behlendorf 	    "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
60368654d025Sperrin 	    isalen,
60378654d025Sperrin 	    isa,
6038420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
6039420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
6040f0ba89beSJeff Bonwick 	    spa_config_path,
604188b7b0f2SMatthew Ahrens 	    pool);
60428654d025Sperrin 	free(isa);
6043fa9e4066Sahrens 
6044420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
6045fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
6046fa9e4066Sahrens 
6047fa9e4066Sahrens 	fp = popen(zdb, "r");
6048fa9e4066Sahrens 
6049fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
6050420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
6051fa9e4066Sahrens 			(void) printf("%s", zbuf);
6052fa9e4066Sahrens 
6053fa9e4066Sahrens 	status = pclose(fp);
6054fa9e4066Sahrens 
6055fa9e4066Sahrens 	if (status == 0)
6056fa9e4066Sahrens 		return;
6057fa9e4066Sahrens 
6058fa9e4066Sahrens 	ztest_dump_core = 0;
6059fa9e4066Sahrens 	if (WIFEXITED(status))
6060fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
6061fa9e4066Sahrens 	else
6062fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
6063fa9e4066Sahrens }
6064fa9e4066Sahrens 
6065fa9e4066Sahrens static void
6066fa9e4066Sahrens ztest_walk_pool_directory(char *header)
6067fa9e4066Sahrens {
6068fa9e4066Sahrens 	spa_t *spa = NULL;
6069fa9e4066Sahrens 
6070420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
6071fa9e4066Sahrens 		(void) printf("%s\n", header);
6072fa9e4066Sahrens 
6073fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
6074fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
6075420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
6076fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
6077fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
6078fa9e4066Sahrens }
6079fa9e4066Sahrens 
6080fa9e4066Sahrens static void
6081fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
6082fa9e4066Sahrens {
60838ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
6084fa9e4066Sahrens 	uint64_t pool_guid;
6085fa9e4066Sahrens 	spa_t *spa;
60863b2aab18SMatthew Ahrens 	int error;
6087fa9e4066Sahrens 
6088420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
6089fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
6090fa9e4066Sahrens 		    oldname, newname);
6091fa9e4066Sahrens 	}
6092fa9e4066Sahrens 
6093fa9e4066Sahrens 	/*
6094fa9e4066Sahrens 	 * Clean up from previous runs.
6095fa9e4066Sahrens 	 */
6096fa9e4066Sahrens 	(void) spa_destroy(newname);
6097fa9e4066Sahrens 
6098fa9e4066Sahrens 	/*
6099fa9e4066Sahrens 	 * Get the pool's configuration and guid.
6100fa9e4066Sahrens 	 */
6101b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6102fa9e4066Sahrens 
61038ad4d6ddSJeff Bonwick 	/*
61048ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
61058ad4d6ddSJeff Bonwick 	 */
61068ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
61073f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
61088ad4d6ddSJeff Bonwick 
6109fa9e4066Sahrens 	pool_guid = spa_guid(spa);
6110fa9e4066Sahrens 	spa_close(spa, FTAG);
6111fa9e4066Sahrens 
6112fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
6113fa9e4066Sahrens 
6114fa9e4066Sahrens 	/*
6115fa9e4066Sahrens 	 * Export it.
6116fa9e4066Sahrens 	 */
6117b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6118fa9e4066Sahrens 
6119fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
6120fa9e4066Sahrens 
61218ad4d6ddSJeff Bonwick 	/*
61228ad4d6ddSJeff Bonwick 	 * Try to import it.
61238ad4d6ddSJeff Bonwick 	 */
61248ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
61258ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
61268ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
61278ad4d6ddSJeff Bonwick 
6128fa9e4066Sahrens 	/*
6129fa9e4066Sahrens 	 * Import it under the new name.
6130fa9e4066Sahrens 	 */
61313b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
61323b2aab18SMatthew Ahrens 	if (error != 0) {
61333b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
61343b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
61353b2aab18SMatthew Ahrens 		    oldname, newname, error);
61363b2aab18SMatthew Ahrens 	}
6137fa9e4066Sahrens 
6138fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
6139fa9e4066Sahrens 
6140fa9e4066Sahrens 	/*
6141fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
6142fa9e4066Sahrens 	 */
61434b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6144fa9e4066Sahrens 
6145fa9e4066Sahrens 	/*
6146fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
6147fa9e4066Sahrens 	 */
61484b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6149fa9e4066Sahrens 
6150fa9e4066Sahrens 	/*
6151fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
6152fa9e4066Sahrens 	 */
6153b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6154fa9e4066Sahrens 
6155fa9e4066Sahrens 	/*
6156fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
6157fa9e4066Sahrens 	 */
6158b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6159fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
6160fa9e4066Sahrens 	spa_close(spa, FTAG);
6161fa9e4066Sahrens 
6162fa9e4066Sahrens 	nvlist_free(config);
6163fa9e4066Sahrens }
6164fa9e4066Sahrens 
61658ad4d6ddSJeff Bonwick static void
61668ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
61678ad4d6ddSJeff Bonwick {
6168420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6169b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
6170b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
6171b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
6172b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
6173b24ab676SJeff Bonwick 	(void) zio_resume(spa);
61748ad4d6ddSJeff Bonwick }
61758ad4d6ddSJeff Bonwick 
61760a4e9518Sgw static void *
61778ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
61780a4e9518Sgw {
6179e14bb325SJeff Bonwick 	spa_t *spa = arg;
61800a4e9518Sgw 
61810a4e9518Sgw 	while (!ztest_exiting) {
6182b24ab676SJeff Bonwick 		if (spa_suspended(spa))
6183b24ab676SJeff Bonwick 			ztest_resume(spa);
6184b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
6185dcbf3bd6SGeorge Wilson 
6186dcbf3bd6SGeorge Wilson 		/*
6187dcbf3bd6SGeorge Wilson 		 * Periodically change the zfs_compressed_arc_enabled setting.
6188dcbf3bd6SGeorge Wilson 		 */
6189dcbf3bd6SGeorge Wilson 		if (ztest_random(10) == 0)
6190dcbf3bd6SGeorge Wilson 			zfs_compressed_arc_enabled = ztest_random(2);
6191770499e1SDan Kimmel 
6192770499e1SDan Kimmel 		/*
6193770499e1SDan Kimmel 		 * Periodically change the zfs_abd_scatter_enabled setting.
6194770499e1SDan Kimmel 		 */
6195770499e1SDan Kimmel 		if (ztest_random(10) == 0)
6196770499e1SDan Kimmel 			zfs_abd_scatter_enabled = ztest_random(2);
61970a4e9518Sgw 	}
61980a4e9518Sgw 	return (NULL);
61990a4e9518Sgw }
62000a4e9518Sgw 
6201b24ab676SJeff Bonwick static void *
6202b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
6203b24ab676SJeff Bonwick {
6204b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
62052c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
62062c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
6207b24ab676SJeff Bonwick 
62082c1e2b44SGeorge Wilson 	for (;;) {
620969962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
621069962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
6211b24ab676SJeff Bonwick 
621269962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6213b24ab676SJeff Bonwick 
62142c1e2b44SGeorge Wilson 		/*
62152c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
62162c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
62172c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
62182c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
62192c1e2b44SGeorge Wilson 		 */
62200713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
62212c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
62222c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
622369962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
62242c1e2b44SGeorge Wilson 			return (NULL);
62252c1e2b44SGeorge Wilson 		}
62262c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
6227b24ab676SJeff Bonwick 
622869962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
62292c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
62302c1e2b44SGeorge Wilson 		    total);
62312c1e2b44SGeorge Wilson 	}
6232b24ab676SJeff Bonwick }
6233b24ab676SJeff Bonwick 
6234b24ab676SJeff Bonwick static void
6235420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6236b24ab676SJeff Bonwick {
6237420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6238420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6239b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
6240b24ab676SJeff Bonwick 
6241b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
6242b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
6243b24ab676SJeff Bonwick 
6244b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
6245b24ab676SJeff Bonwick 
6246420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
6247420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
6248b24ab676SJeff Bonwick 
6249420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
6250b24ab676SJeff Bonwick 		Dl_info dli;
6251b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
6252b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
6253b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
6254b24ab676SJeff Bonwick 	}
6255b24ab676SJeff Bonwick }
6256b24ab676SJeff Bonwick 
6257fa9e4066Sahrens static void *
6258fa9e4066Sahrens ztest_thread(void *arg)
6259fa9e4066Sahrens {
6260420dfc95SChris Siden 	int rand;
6261b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
6262fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
6263b24ab676SJeff Bonwick 	uint64_t call_next;
6264b24ab676SJeff Bonwick 	hrtime_t now;
6265fa9e4066Sahrens 	ztest_info_t *zi;
6266420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6267fa9e4066Sahrens 
6268b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
6269fa9e4066Sahrens 		/*
6270fa9e4066Sahrens 		 * See if it's time to force a crash.
6271fa9e4066Sahrens 		 */
6272b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
6273b24ab676SJeff Bonwick 			ztest_kill(zs);
6274fa9e4066Sahrens 
6275fa9e4066Sahrens 		/*
6276b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
6277fa9e4066Sahrens 		 */
6278b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
6279b24ab676SJeff Bonwick 			break;
6280fa9e4066Sahrens 
6281fa9e4066Sahrens 		/*
6282b24ab676SJeff Bonwick 		 * Pick a random function to execute.
6283fa9e4066Sahrens 		 */
6284420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
6285420dfc95SChris Siden 		zi = &ztest_info[rand];
6286420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6287420dfc95SChris Siden 		call_next = zc->zc_next;
6288b24ab676SJeff Bonwick 
6289b24ab676SJeff Bonwick 		if (now >= call_next &&
6290420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
6291420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6292420dfc95SChris Siden 			ztest_execute(rand, zi, id);
6293420dfc95SChris Siden 		}
6294b24ab676SJeff Bonwick 	}
6295fa9e4066Sahrens 
6296b24ab676SJeff Bonwick 	return (NULL);
6297b24ab676SJeff Bonwick }
6298fa9e4066Sahrens 
6299b24ab676SJeff Bonwick static void
6300b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
6301b24ab676SJeff Bonwick {
63029adfa60dSMatthew Ahrens 	(void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6303b24ab676SJeff Bonwick }
6304fa9e4066Sahrens 
6305b24ab676SJeff Bonwick static void
6306420dfc95SChris Siden ztest_dataset_destroy(int d)
6307b24ab676SJeff Bonwick {
63089adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6309fa9e4066Sahrens 
6310420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6311fa9e4066Sahrens 
6312420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6313b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
6314fa9e4066Sahrens 
6315b24ab676SJeff Bonwick 	/*
6316b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
6317b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
6318b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
6319b24ab676SJeff Bonwick 	 */
6320420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
6321420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
6322b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
6323420dfc95SChris Siden 	}
6324fa9e4066Sahrens 
6325b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6326b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6327b24ab676SJeff Bonwick }
6328b24ab676SJeff Bonwick 
6329b24ab676SJeff Bonwick static void
6330b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6331b24ab676SJeff Bonwick {
6332b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
6333b24ab676SJeff Bonwick 
6334b24ab676SJeff Bonwick 	/*
6335b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
6336b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
6337b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6338b24ab676SJeff Bonwick 	 * If not, we have an object leak.
6339b24ab676SJeff Bonwick 	 *
6340b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
6341b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
6342b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
6343b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
6344b24ab676SJeff Bonwick 	 */
6345b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6346b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6347b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
6348b24ab676SJeff Bonwick }
6349b24ab676SJeff Bonwick 
6350b24ab676SJeff Bonwick static int
6351420dfc95SChris Siden ztest_dataset_open(int d)
6352b24ab676SJeff Bonwick {
6353420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6354420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6355b24ab676SJeff Bonwick 	objset_t *os;
6356b24ab676SJeff Bonwick 	zilog_t *zilog;
63579adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6358b24ab676SJeff Bonwick 	int error;
6359b24ab676SJeff Bonwick 
6360420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6361b24ab676SJeff Bonwick 
6362f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
6363b24ab676SJeff Bonwick 
636455da60b9SMark J Musante 	error = ztest_dataset_create(name);
6365b24ab676SJeff Bonwick 	if (error == ENOSPC) {
6366f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
6367b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
6368b24ab676SJeff Bonwick 		return (error);
6369fa9e4066Sahrens 	}
6370b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
6371fa9e4066Sahrens 
6372*eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
6373*eb633035STom Caputi 	    B_TRUE, zd, &os));
6374f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
6375b24ab676SJeff Bonwick 
6376420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6377b24ab676SJeff Bonwick 
6378b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
6379b24ab676SJeff Bonwick 
6380b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6381b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
6382b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
6383b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
6384b24ab676SJeff Bonwick 
6385b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6386b24ab676SJeff Bonwick 
6387b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
6388b24ab676SJeff Bonwick 
6389b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6390b24ab676SJeff Bonwick 
6391420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
6392b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6393b24ab676SJeff Bonwick 		    zd->zd_name,
6394b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
6395b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
6396b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
6397b24ab676SJeff Bonwick 
6398b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
6399b24ab676SJeff Bonwick 
6400b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
6401b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
6402b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
6403b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
6404b24ab676SJeff Bonwick 
6405b24ab676SJeff Bonwick 	return (0);
6406b24ab676SJeff Bonwick }
6407b24ab676SJeff Bonwick 
6408b24ab676SJeff Bonwick static void
6409420dfc95SChris Siden ztest_dataset_close(int d)
6410b24ab676SJeff Bonwick {
6411420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6412b24ab676SJeff Bonwick 
6413b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
6414*eb633035STom Caputi 	dmu_objset_disown(zd->zd_os, B_TRUE, zd);
6415b24ab676SJeff Bonwick 
6416b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
6417fa9e4066Sahrens }
6418fa9e4066Sahrens 
6419fa9e4066Sahrens /*
6420fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
6421fa9e4066Sahrens  */
6422fa9e4066Sahrens static void
6423b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
6424fa9e4066Sahrens {
6425b24ab676SJeff Bonwick 	thread_t *tid;
6426fa9e4066Sahrens 	spa_t *spa;
6427e9103aaeSGarrett D'Amore 	objset_t *os;
6428e14bb325SJeff Bonwick 	thread_t resume_tid;
6429b24ab676SJeff Bonwick 	int error;
6430e14bb325SJeff Bonwick 
6431e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
6432fa9e4066Sahrens 
6433fa9e4066Sahrens 	/*
6434b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
6435fa9e4066Sahrens 	 */
643686714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6437f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6438f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6439fa9e4066Sahrens 
6440b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
6441420dfc95SChris Siden 	zs->zs_thread_stop =
6442420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6443b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6444b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
6445420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
6446420dfc95SChris Siden 		zs->zs_thread_kill -=
6447420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
6448420dfc95SChris Siden 	}
6449fa9e4066Sahrens 
6450f06dce2cSAndrew Stormont 	mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6451fa9e4066Sahrens 
6452b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6453b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
6454fa9e4066Sahrens 
64550a4e9518Sgw 	/*
6456e14bb325SJeff Bonwick 	 * Open our pool.
64570a4e9518Sgw 	 */
6458b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
64593b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
646030beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
6461420dfc95SChris Siden 	ztest_spa = spa;
6462b24ab676SJeff Bonwick 
6463aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
6464*eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(ztest_opts.zo_pool,
6465*eb633035STom Caputi 	    DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
6466aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6467aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
6468aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6469aab80726SGeorge Wilson 	zs->zs_guid = dds.dds_guid;
6470*eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
6471e9103aaeSGarrett D'Amore 
6472b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
64730a4e9518Sgw 
64748ad4d6ddSJeff Bonwick 	/*
64758ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
64768ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
64778ad4d6ddSJeff Bonwick 	 * the only valid replica.
64788ad4d6ddSJeff Bonwick 	 */
64791195e687SMark J Musante 	if (MAXFAULTS() == 0)
64808ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
64818ad4d6ddSJeff Bonwick 	else
64828ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
64838ad4d6ddSJeff Bonwick 
6484fa9e4066Sahrens 	/*
6485e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
6486fa9e4066Sahrens 	 */
64878ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6488e14bb325SJeff Bonwick 	    &resume_tid) == 0);
6489fa9e4066Sahrens 
6490b24ab676SJeff Bonwick 	/*
6491b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
6492b24ab676SJeff Bonwick 	 */
6493b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6494b24ab676SJeff Bonwick 	    NULL) == 0);
6495b24ab676SJeff Bonwick 
6496fa9e4066Sahrens 	/*
649786714001SSerapheim Dimitropoulos 	 * Verify that we can safely inquire about any object,
6498fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
6499fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
6500fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
6501fa9e4066Sahrens 	 */
6502b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
6503b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
6504fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
6505fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
6506ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
6507ea8dc4b6Seschrock 			    error == EINVAL);
6508fa9e4066Sahrens 		}
6509fa9e4066Sahrens 	}
6510fa9e4066Sahrens 
6511fa9e4066Sahrens 	/*
6512b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
6513fa9e4066Sahrens 	 */
6514b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
6515420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
6516420dfc95SChris Siden 		ztest_dataset_destroy(d);
6517b24ab676SJeff Bonwick 	}
6518fa9e4066Sahrens 	zs->zs_enospc_count = 0;
6519fa9e4066Sahrens 
6520420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6521420dfc95SChris Siden 	    UMEM_NOFAIL);
6522fa9e4066Sahrens 
6523420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
6524fa9e4066Sahrens 		(void) printf("starting main threads...\n");
6525fa9e4066Sahrens 
6526b24ab676SJeff Bonwick 	/*
6527b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
6528b24ab676SJeff Bonwick 	 */
6529420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
6530420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
6531420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
6532b24ab676SJeff Bonwick 			return;
6533b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6534b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
6535fa9e4066Sahrens 	}
6536fa9e4066Sahrens 
6537b24ab676SJeff Bonwick 	/*
6538b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
6539b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
6540b24ab676SJeff Bonwick 	 */
6541420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6542b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6543420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
6544420dfc95SChris Siden 			ztest_dataset_close(t);
6545fa9e4066Sahrens 	}
6546fa9e4066Sahrens 
6547fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
6548fa9e4066Sahrens 
6549b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6550b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6551b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
6552b24ab676SJeff Bonwick 
6553420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6554b24ab676SJeff Bonwick 
6555b24ab676SJeff Bonwick 	/* Kill the resume thread */
6556b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
6557b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6558b24ab676SJeff Bonwick 	ztest_resume(spa);
6559b24ab676SJeff Bonwick 
6560b24ab676SJeff Bonwick 	/*
6561b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
6562b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
6563b24ab676SJeff Bonwick 	 */
6564a2cdcdd2SPaul Dagnelie 	for (uint64_t object = 1; object < 50; object++) {
6565a2cdcdd2SPaul Dagnelie 		dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6566a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
6567a2cdcdd2SPaul Dagnelie 	}
6568b24ab676SJeff Bonwick 
6569b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6570fa9e4066Sahrens 
6571fa9e4066Sahrens 	/*
6572b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
6573fa9e4066Sahrens 	 */
6574b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
6575b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6576420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
6577b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
6578b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
6579b24ab676SJeff Bonwick 
6580b24ab676SJeff Bonwick 	/*
6581b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
6582b24ab676SJeff Bonwick 	 * different name.
6583b24ab676SJeff Bonwick 	 */
6584e0f1c0afSOlaf Faaland 	if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
65859adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
65869adfa60dSMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s_import",
6587420dfc95SChris Siden 		    ztest_opts.zo_pool);
6588420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
6589420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
6590b24ab676SJeff Bonwick 	}
6591b24ab676SJeff Bonwick 
6592b24ab676SJeff Bonwick 	kernel_fini();
659326cb54a9SGeorge Wilson 
659426cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
659526cb54a9SGeorge Wilson 
6596f06dce2cSAndrew Stormont 	mutex_destroy(&zcl.zcl_callbacks_lock);
659726cb54a9SGeorge Wilson 
6598f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6599f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
660086714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6601b24ab676SJeff Bonwick }
6602b24ab676SJeff Bonwick 
6603b24ab676SJeff Bonwick static void
6604420dfc95SChris Siden ztest_freeze(void)
6605b24ab676SJeff Bonwick {
6606420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
6607b24ab676SJeff Bonwick 	spa_t *spa;
66082215e990SMark J Musante 	int numloops = 0;
6609b24ab676SJeff Bonwick 
6610420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6611b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
6612718d718aSGeorge Wilson 
6613b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6614b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6615b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
661680901aeaSGeorge Wilson 	ztest_spa = spa;
6617718d718aSGeorge Wilson 
6618b24ab676SJeff Bonwick 	/*
6619b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
6620b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
6621b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
6622b24ab676SJeff Bonwick 	 */
6623b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6624b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
66255002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
6626fa9e4066Sahrens 	}
6627fa9e4066Sahrens 
6628ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
6629fa9e4066Sahrens 
6630b24ab676SJeff Bonwick 	/*
6631b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
6632b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
6633b24ab676SJeff Bonwick 	 */
6634b24ab676SJeff Bonwick 	spa_freeze(spa);
6635e14bb325SJeff Bonwick 
66362a104a52SAlex Reece 	/*
66372a104a52SAlex Reece 	 * Because it is hard to predict how much space a write will actually
66382a104a52SAlex Reece 	 * require beforehand, we leave ourselves some fudge space to write over
66392a104a52SAlex Reece 	 * capacity.
66402a104a52SAlex Reece 	 */
66412a104a52SAlex Reece 	uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
66422a104a52SAlex Reece 
6643b24ab676SJeff Bonwick 	/*
6644b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
6645b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6646b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
6647b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
6648b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
66492a104a52SAlex Reece 	 *
66502a104a52SAlex Reece 	 * Run a random number of times less than zo_maxloops and ensure we do
66512a104a52SAlex Reece 	 * not run out of space on the pool.
6652b24ab676SJeff Bonwick 	 */
6653420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
66542a104a52SAlex Reece 	    numloops++ < ztest_opts.zo_maxloops &&
66552a104a52SAlex Reece 	    metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
66562a104a52SAlex Reece 		ztest_od_t od;
665754811da5SToomas Soome 		ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
66582a104a52SAlex Reece 		VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
66592a104a52SAlex Reece 		ztest_io(zd, od.od_object,
66602a104a52SAlex Reece 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6661b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
6662b24ab676SJeff Bonwick 	}
6663e14bb325SJeff Bonwick 
6664fa9e4066Sahrens 	/*
6665b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
6666fa9e4066Sahrens 	 */
66675002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
6668b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
6669fa9e4066Sahrens 
6670b24ab676SJeff Bonwick 	/*
6671b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
6672b24ab676SJeff Bonwick 	 */
6673420dfc95SChris Siden 	ztest_dataset_close(0);
6674e05725b1Sbonwick 	spa_close(spa, FTAG);
6675b24ab676SJeff Bonwick 	kernel_fini();
6676e05725b1Sbonwick 
6677b24ab676SJeff Bonwick 	/*
6678b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
6679b24ab676SJeff Bonwick 	 */
6680b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6681b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6682ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6683b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
6684dfbb9432SGeorge Wilson 	ztest_spa = spa;
6685dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
6686*eb633035STom Caputi 	ztest_dataset_close(0);
6687dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
6688dfbb9432SGeorge Wilson 
6689b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6690fa9e4066Sahrens 	kernel_fini();
6691fa9e4066Sahrens }
6692fa9e4066Sahrens 
6693fa9e4066Sahrens void
6694fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
6695fa9e4066Sahrens {
6696fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
6697fa9e4066Sahrens 	hrtime_t m = s / 60;
6698fa9e4066Sahrens 	hrtime_t h = m / 60;
6699fa9e4066Sahrens 	hrtime_t d = h / 24;
6700fa9e4066Sahrens 
6701fa9e4066Sahrens 	s -= m * 60;
6702fa9e4066Sahrens 	m -= h * 60;
6703fa9e4066Sahrens 	h -= d * 24;
6704fa9e4066Sahrens 
6705fa9e4066Sahrens 	timebuf[0] = '\0';
6706fa9e4066Sahrens 
6707fa9e4066Sahrens 	if (d)
6708fa9e4066Sahrens 		(void) sprintf(timebuf,
6709fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
6710fa9e4066Sahrens 	else if (h)
6711fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6712fa9e4066Sahrens 	else if (m)
6713fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
6714fa9e4066Sahrens 	else
6715fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
6716fa9e4066Sahrens }
6717fa9e4066Sahrens 
67181195e687SMark J Musante static nvlist_t *
67191195e687SMark J Musante make_random_props()
67201195e687SMark J Musante {
67211195e687SMark J Musante 	nvlist_t *props;
67221195e687SMark J Musante 
67231195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6724663207adSDon Brady 
6725ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
6726ad135b5dSChristopher Siden 		return (props);
67271195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
67281195e687SMark J Musante 
67291195e687SMark J Musante 	return (props);
67301195e687SMark J Musante }
67311195e687SMark J Musante 
6732e0f1c0afSOlaf Faaland /*
6733e0f1c0afSOlaf Faaland  * Import a storage pool with the given name.
6734e0f1c0afSOlaf Faaland  */
6735e0f1c0afSOlaf Faaland static void
6736e0f1c0afSOlaf Faaland ztest_import(ztest_shared_t *zs)
6737e0f1c0afSOlaf Faaland {
6738e0f1c0afSOlaf Faaland 	libzfs_handle_t *hdl;
6739e0f1c0afSOlaf Faaland 	importargs_t args = { 0 };
6740e0f1c0afSOlaf Faaland 	spa_t *spa;
6741e0f1c0afSOlaf Faaland 	nvlist_t *cfg = NULL;
6742e0f1c0afSOlaf Faaland 	int nsearch = 1;
6743e0f1c0afSOlaf Faaland 	char *searchdirs[nsearch];
6744e0f1c0afSOlaf Faaland 	char *name = ztest_opts.zo_pool;
6745e0f1c0afSOlaf Faaland 	int flags = ZFS_IMPORT_MISSING_LOG;
6746e0f1c0afSOlaf Faaland 	int error;
6747e0f1c0afSOlaf Faaland 
6748e0f1c0afSOlaf Faaland 	mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6749e0f1c0afSOlaf Faaland 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6750e0f1c0afSOlaf Faaland 
6751e0f1c0afSOlaf Faaland 	kernel_init(FREAD | FWRITE);
6752e0f1c0afSOlaf Faaland 	hdl = libzfs_init();
6753e0f1c0afSOlaf Faaland 
6754e0f1c0afSOlaf Faaland 	searchdirs[0] = ztest_opts.zo_dir;
6755e0f1c0afSOlaf Faaland 	args.paths = nsearch;
6756e0f1c0afSOlaf Faaland 	args.path = searchdirs;
6757e0f1c0afSOlaf Faaland 	args.can_be_active = B_FALSE;
6758e0f1c0afSOlaf Faaland 
6759e0f1c0afSOlaf Faaland 	error = zpool_tryimport(hdl, name, &cfg, &args);
6760e0f1c0afSOlaf Faaland 	if (error)
6761e0f1c0afSOlaf Faaland 		(void) fatal(0, "No pools found\n");
6762e0f1c0afSOlaf Faaland 
6763e0f1c0afSOlaf Faaland 	VERIFY0(spa_import(name, cfg, NULL, flags));
6764e0f1c0afSOlaf Faaland 	VERIFY0(spa_open(name, &spa, FTAG));
6765e0f1c0afSOlaf Faaland 	zs->zs_metaslab_sz =
6766e0f1c0afSOlaf Faaland 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6767e0f1c0afSOlaf Faaland 	spa_close(spa, FTAG);
6768e0f1c0afSOlaf Faaland 
6769e0f1c0afSOlaf Faaland 	libzfs_fini(hdl);
6770e0f1c0afSOlaf Faaland 	kernel_fini();
6771e0f1c0afSOlaf Faaland 
6772e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6773e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6774e0f1c0afSOlaf Faaland 		ztest_freeze();
6775e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6776e0f1c0afSOlaf Faaland 	}
6777e0f1c0afSOlaf Faaland 
6778e0f1c0afSOlaf Faaland 	rw_destroy(&ztest_name_lock);
6779e0f1c0afSOlaf Faaland 	mutex_destroy(&ztest_vdev_lock);
6780e0f1c0afSOlaf Faaland }
6781e0f1c0afSOlaf Faaland 
6782fa9e4066Sahrens /*
6783fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
6784b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
6785fa9e4066Sahrens  */
6786fa9e4066Sahrens static void
6787b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
6788fa9e4066Sahrens {
6789fa9e4066Sahrens 	spa_t *spa;
67901195e687SMark J Musante 	nvlist_t *nvroot, *props;
6791fa9e4066Sahrens 
6792f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
679386714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6794f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6795b24ab676SJeff Bonwick 
6796fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
6797fa9e4066Sahrens 
6798fa9e4066Sahrens 	/*
6799fa9e4066Sahrens 	 * Create the storage pool.
6800fa9e4066Sahrens 	 */
6801420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
680288ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
68031195e687SMark J Musante 	zs->zs_splits = 0;
6804420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
680525345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6806663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
68071195e687SMark J Musante 	props = make_random_props();
6808ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
6809ad135b5dSChristopher Siden 		char buf[1024];
6810ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
6811ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
6812b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6813ad135b5dSChristopher Siden 	}
6814*eb633035STom Caputi 	VERIFY3U(0, ==,
6815*eb633035STom Caputi 	    spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
6816fa9e4066Sahrens 	nvlist_free(nvroot);
68171df447ebSRichard Yao 	nvlist_free(props);
6818fa9e4066Sahrens 
6819b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6820420dfc95SChris Siden 	zs->zs_metaslab_sz =
6821420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6822ad135b5dSChristopher Siden 
6823fa9e4066Sahrens 	spa_close(spa, FTAG);
6824fa9e4066Sahrens 
6825fa9e4066Sahrens 	kernel_fini();
6826b24ab676SJeff Bonwick 
6827e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6828e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6829e0f1c0afSOlaf Faaland 		ztest_freeze();
6830e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6831e0f1c0afSOlaf Faaland 	}
6832420dfc95SChris Siden 
6833f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6834f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
683586714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6836420dfc95SChris Siden }
6837420dfc95SChris Siden 
6838420dfc95SChris Siden static void
6839741652b0SEtienne Dechamps setup_data_fd(void)
6840420dfc95SChris Siden {
6841741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6842420dfc95SChris Siden 
6843741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
6844741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
6845741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
6846420dfc95SChris Siden }
6847420dfc95SChris Siden 
6848ad135b5dSChristopher Siden static int
6849ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
6850ad135b5dSChristopher Siden {
6851ad135b5dSChristopher Siden 	int size;
6852ad135b5dSChristopher Siden 
6853ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
6854ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
6855ad135b5dSChristopher Siden 	size += hdr->zh_size;
6856ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
6857ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
6858ad135b5dSChristopher Siden 
6859ad135b5dSChristopher Siden 	return (size);
6860ad135b5dSChristopher Siden }
6861ad135b5dSChristopher Siden 
6862420dfc95SChris Siden static void
6863420dfc95SChris Siden setup_hdr(void)
6864420dfc95SChris Siden {
6865ad135b5dSChristopher Siden 	int size;
6866420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6867420dfc95SChris Siden 
6868420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6869741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6870420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6871420dfc95SChris Siden 
6872741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6873ad135b5dSChristopher Siden 
6874420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6875420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6876420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
6877420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6878420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
6879420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6880420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
6881420dfc95SChris Siden 
6882ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6883741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6884ad135b5dSChristopher Siden 
6885420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6886420dfc95SChris Siden }
6887420dfc95SChris Siden 
6888420dfc95SChris Siden static void
6889420dfc95SChris Siden setup_data(void)
6890420dfc95SChris Siden {
6891420dfc95SChris Siden 	int size, offset;
6892420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6893420dfc95SChris Siden 	uint8_t *buf;
6894420dfc95SChris Siden 
6895420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6896741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6897420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6898420dfc95SChris Siden 
6899ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6900420dfc95SChris Siden 
6901420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6902420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6903741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6904420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6905420dfc95SChris Siden 	buf = (uint8_t *)hdr;
6906420dfc95SChris Siden 
6907420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
6908420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
6909420dfc95SChris Siden 	offset += hdr->zh_opts_size;
6910420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
6911420dfc95SChris Siden 	offset += hdr->zh_size;
6912420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
6913420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
6914420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
6915420dfc95SChris Siden }
6916420dfc95SChris Siden 
6917420dfc95SChris Siden static boolean_t
6918420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6919420dfc95SChris Siden {
6920420dfc95SChris Siden 	pid_t pid;
6921420dfc95SChris Siden 	int status;
6922741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
6923420dfc95SChris Siden 
6924420dfc95SChris Siden 	pid = fork();
6925420dfc95SChris Siden 
6926420dfc95SChris Siden 	if (cmd == NULL) {
6927741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6928741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6929420dfc95SChris Siden 		cmd = cmdbuf;
6930420dfc95SChris Siden 	}
6931b24ab676SJeff Bonwick 
6932420dfc95SChris Siden 	if (pid == -1)
6933420dfc95SChris Siden 		fatal(1, "fork failed");
6934b24ab676SJeff Bonwick 
6935420dfc95SChris Siden 	if (pid == 0) {	/* child */
6936420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
6937741652b0SEtienne Dechamps 		char fd_data_str[12];
693826cb54a9SGeorge Wilson 
6939420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
6940420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
6941741652b0SEtienne Dechamps 
6942741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
6943741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
6944741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6945741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6946741652b0SEtienne Dechamps 
6947420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
6948420dfc95SChris Siden 		if (libpath != NULL)
6949420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6950420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
6951420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
6952420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
6953420dfc95SChris Siden 	}
6954420dfc95SChris Siden 
6955741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
6956741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
6957741652b0SEtienne Dechamps 		cmd = NULL;
6958741652b0SEtienne Dechamps 	}
6959741652b0SEtienne Dechamps 
6960420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
6961420dfc95SChris Siden 		continue;
6962420dfc95SChris Siden 	if (statusp != NULL)
6963420dfc95SChris Siden 		*statusp = status;
6964420dfc95SChris Siden 
6965420dfc95SChris Siden 	if (WIFEXITED(status)) {
6966420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
6967420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
6968420dfc95SChris Siden 			    WEXITSTATUS(status));
6969420dfc95SChris Siden 			exit(2);
6970420dfc95SChris Siden 		}
6971420dfc95SChris Siden 		return (B_FALSE);
6972420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
6973420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6974420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
6975420dfc95SChris Siden 			    WTERMSIG(status));
6976420dfc95SChris Siden 			exit(3);
6977420dfc95SChris Siden 		}
6978420dfc95SChris Siden 		return (B_TRUE);
6979420dfc95SChris Siden 	} else {
6980420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
6981420dfc95SChris Siden 		exit(4);
6982420dfc95SChris Siden 		/* NOTREACHED */
6983420dfc95SChris Siden 	}
6984420dfc95SChris Siden }
6985420dfc95SChris Siden 
6986420dfc95SChris Siden static void
6987420dfc95SChris Siden ztest_run_init(void)
6988420dfc95SChris Siden {
6989420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
6990420dfc95SChris Siden 
6991420dfc95SChris Siden 	/*
6992420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
6993420dfc95SChris Siden 	 */
6994420dfc95SChris Siden 	(void) remove(spa_config_path);
6995420dfc95SChris Siden 
6996e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_init == 0) {
6997e0f1c0afSOlaf Faaland 		if (ztest_opts.zo_verbose >= 1)
6998e0f1c0afSOlaf Faaland 			(void) printf("Importing pool %s\n",
6999e0f1c0afSOlaf Faaland 			    ztest_opts.zo_pool);
7000e0f1c0afSOlaf Faaland 		ztest_import(zs);
7001e0f1c0afSOlaf Faaland 		return;
7002e0f1c0afSOlaf Faaland 	}
7003e0f1c0afSOlaf Faaland 
7004420dfc95SChris Siden 	/*
7005420dfc95SChris Siden 	 * Create and initialize our storage pool.
7006420dfc95SChris Siden 	 */
7007420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
7008420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
7009420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
7010420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
7011420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
7012420dfc95SChris Siden 		}
7013420dfc95SChris Siden 		ztest_init(zs);
7014420dfc95SChris Siden 	}
7015fa9e4066Sahrens }
7016fa9e4066Sahrens 
7017fa9e4066Sahrens int
7018fa9e4066Sahrens main(int argc, char **argv)
7019fa9e4066Sahrens {
7020fa9e4066Sahrens 	int kills = 0;
7021fa9e4066Sahrens 	int iters = 0;
7022420dfc95SChris Siden 	int older = 0;
7023420dfc95SChris Siden 	int newer = 0;
7024fa9e4066Sahrens 	ztest_shared_t *zs;
7025fa9e4066Sahrens 	ztest_info_t *zi;
7026420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
7027fa9e4066Sahrens 	char timebuf[100];
70280a055120SJason King 	char numbuf[NN_NUMBUF_SZ];
7029741652b0SEtienne Dechamps 	char *cmd;
7030420dfc95SChris Siden 	boolean_t hasalt;
7031741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
7032fa9e4066Sahrens 
7033fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
7034fa9e4066Sahrens 
7035cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
703669962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
703717f11284SSerapheim Dimitropoulos 	/*
703817f11284SSerapheim Dimitropoulos 	 * As two-word space map entries may not come up often (especially
703917f11284SSerapheim Dimitropoulos 	 * if pool and vdev sizes are small) we want to force at least some
704017f11284SSerapheim Dimitropoulos 	 * of them so the feature get tested.
704117f11284SSerapheim Dimitropoulos 	 */
704217f11284SSerapheim Dimitropoulos 	zfs_force_some_double_word_sm_entries = B_TRUE;
7043cd1c8b85SMatthew Ahrens 
7044a21fe349SBrian Behlendorf 	/*
7045a21fe349SBrian Behlendorf 	 * Verify that even extensively damaged split blocks with many
7046a21fe349SBrian Behlendorf 	 * segments can be reconstructed in a reasonable amount of time
7047a21fe349SBrian Behlendorf 	 * when reconstruction is known to be possible.
7048a21fe349SBrian Behlendorf 	 */
7049a21fe349SBrian Behlendorf 	zfs_reconstruct_indirect_damage_fraction = 4;
7050a21fe349SBrian Behlendorf 
7051741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
7052741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
7053741652b0SEtienne Dechamps 
7054741652b0SEtienne Dechamps 	if (!fd_data_str) {
7055420dfc95SChris Siden 		process_options(argc, argv);
7056fa9e4066Sahrens 
7057741652b0SEtienne Dechamps 		setup_data_fd();
7058420dfc95SChris Siden 		setup_hdr();
7059420dfc95SChris Siden 		setup_data();
7060420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
7061420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
7062420dfc95SChris Siden 	} else {
7063741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
7064420dfc95SChris Siden 		setup_data();
7065420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
7066420dfc95SChris Siden 	}
7067420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
7068fa9e4066Sahrens 
7069f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
7070741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
7071741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
7072f0ba89beSJeff Bonwick 
7073420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
7074420dfc95SChris Siden 	    UMEM_NOFAIL);
7075420dfc95SChris Siden 	zs = ztest_shared;
7076ea8dc4b6Seschrock 
7077741652b0SEtienne Dechamps 	if (fd_data_str) {
7078243952c7SMatt Ahrens 		metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
7079420dfc95SChris Siden 		metaslab_df_alloc_threshold =
7080420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
7081b24ab676SJeff Bonwick 
7082420dfc95SChris Siden 		if (zs->zs_do_init)
7083420dfc95SChris Siden 			ztest_run_init();
7084420dfc95SChris Siden 		else
7085420dfc95SChris Siden 			ztest_run(zs);
7086420dfc95SChris Siden 		exit(0);
7087420dfc95SChris Siden 	}
7088fa9e4066Sahrens 
7089420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
7090420dfc95SChris Siden 
7091420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
7092fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
7093fa9e4066Sahrens 		    " %llu seconds...\n",
7094420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
7095420dfc95SChris Siden 		    ztest_opts.zo_datasets,
7096420dfc95SChris Siden 		    ztest_opts.zo_threads,
7097420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
7098fa9e4066Sahrens 	}
7099fa9e4066Sahrens 
7100741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7101741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7102420dfc95SChris Siden 
7103420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
7104420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7105420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7106420dfc95SChris Siden 			(void) printf("Executing older ztest for "
7107420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
7108420dfc95SChris Siden 		}
7109420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7110420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7111420dfc95SChris Siden 	} else {
7112420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7113fa9e4066Sahrens 	}
7114420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
7115fa9e4066Sahrens 
7116b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
7117420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7118fa9e4066Sahrens 
7119b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
7120420dfc95SChris Siden 		zi = &ztest_info[f];
7121420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
7122b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7123420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
7124fa9e4066Sahrens 		else
7125420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
7126b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
7127fa9e4066Sahrens 	}
7128fa9e4066Sahrens 
7129fa9e4066Sahrens 	/*
7130fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
7131fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
7132fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
7133fa9e4066Sahrens 	 */
7134b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
7135fa9e4066Sahrens 		int status;
7136420dfc95SChris Siden 		boolean_t killed;
7137fa9e4066Sahrens 
7138fa9e4066Sahrens 		/*
7139fa9e4066Sahrens 		 * Initialize the workload counters for each function.
7140fa9e4066Sahrens 		 */
7141b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
7142420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
7143420dfc95SChris Siden 			zc->zc_count = 0;
7144420dfc95SChris Siden 			zc->zc_time = 0;
7145fa9e4066Sahrens 		}
7146fa9e4066Sahrens 
7147d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
7148420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
7149420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
7150fa9e4066Sahrens 
7151420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
7152420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7153420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
7154420dfc95SChris Siden 				    cmd);
7155fa9e4066Sahrens 			}
7156420dfc95SChris Siden 			newer++;
7157420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
71585ad82045Snd 		} else {
7159420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7160420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
7161420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
7162420dfc95SChris Siden 			}
7163420dfc95SChris Siden 			older++;
7164420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
7165420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
7166fa9e4066Sahrens 		}
7167fa9e4066Sahrens 
7168420dfc95SChris Siden 		if (killed)
7169420dfc95SChris Siden 			kills++;
7170fa9e4066Sahrens 		iters++;
7171fa9e4066Sahrens 
7172420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7173fa9e4066Sahrens 			hrtime_t now = gethrtime();
7174fa9e4066Sahrens 
7175b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
7176b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
71770a055120SJason King 			nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7178fa9e4066Sahrens 
7179fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7180fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7181fa9e4066Sahrens 			    iters,
7182fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
7183fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
7184fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
7185fa9e4066Sahrens 			    numbuf,
7186b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
7187420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
7188fa9e4066Sahrens 		}
7189fa9e4066Sahrens 
7190420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
7191fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
7192fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7193fa9e4066Sahrens 			    "Calls", "Time", "Function");
7194fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7195fa9e4066Sahrens 			    "-----", "----", "--------");
7196b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
7197fa9e4066Sahrens 				Dl_info dli;
7198fa9e4066Sahrens 
7199420dfc95SChris Siden 				zi = &ztest_info[f];
7200420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
7201420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
7202fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
7203fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
7204420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
7205fa9e4066Sahrens 				    dli.dli_sname);
7206fa9e4066Sahrens 			}
7207fa9e4066Sahrens 			(void) printf("\n");
7208fa9e4066Sahrens 		}
7209fa9e4066Sahrens 
7210e0f1c0afSOlaf Faaland 		if (!ztest_opts.zo_mmp_test)
7211e0f1c0afSOlaf Faaland 			ztest_run_zdb(ztest_opts.zo_pool);
7212b24ab676SJeff Bonwick 	}
7213fa9e4066Sahrens 
7214420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
7215420dfc95SChris Siden 		if (hasalt) {
7216420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
7217420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
7218420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
7219420dfc95SChris Siden 			    cmd);
7220420dfc95SChris Siden 		}
7221fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7222fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7223fa9e4066Sahrens 	}
7224fa9e4066Sahrens 
7225741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
7226741652b0SEtienne Dechamps 
7227fa9e4066Sahrens 	return (0);
7228fa9e4066Sahrens }
7229