xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision 084fd14f)
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>
108*084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
109fa9e4066Sahrens #include <sys/spa_impl.h>
11088ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
111fa9e4066Sahrens #include <sys/dsl_prop.h>
1124f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1133b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1143f9d6ad7SLin Ling #include <sys/dsl_scan.h>
115cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
116fa9e4066Sahrens #include <sys/refcount.h>
117ad135b5dSChristopher Siden #include <sys/zfeature.h>
1183b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
119770499e1SDan Kimmel #include <sys/abd.h>
120fa9e4066Sahrens #include <stdio.h>
121004388ebScasper #include <stdio_ext.h>
122fa9e4066Sahrens #include <stdlib.h>
123fa9e4066Sahrens #include <unistd.h>
124fa9e4066Sahrens #include <signal.h>
125fa9e4066Sahrens #include <umem.h>
126fa9e4066Sahrens #include <dlfcn.h>
127fa9e4066Sahrens #include <ctype.h>
128fa9e4066Sahrens #include <math.h>
129fa9e4066Sahrens #include <sys/fs/zfs.h>
130b24ab676SJeff Bonwick #include <libnvpair.h>
131e0f1c0afSOlaf Faaland #include <libzfs.h>
1320a055120SJason King #include <libcmdutils.h>
133fa9e4066Sahrens 
134741652b0SEtienne Dechamps static int ztest_fd_data = -1;
135741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
136420dfc95SChris Siden 
137420dfc95SChris Siden typedef struct ztest_shared_hdr {
138420dfc95SChris Siden 	uint64_t	zh_hdr_size;
139420dfc95SChris Siden 	uint64_t	zh_opts_size;
140420dfc95SChris Siden 	uint64_t	zh_size;
141420dfc95SChris Siden 	uint64_t	zh_stats_size;
142420dfc95SChris Siden 	uint64_t	zh_stats_count;
143420dfc95SChris Siden 	uint64_t	zh_ds_size;
144420dfc95SChris Siden 	uint64_t	zh_ds_count;
145420dfc95SChris Siden } ztest_shared_hdr_t;
146420dfc95SChris Siden 
147420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
148420dfc95SChris Siden 
149663207adSDon Brady enum ztest_class_state {
150663207adSDon Brady 	ZTEST_VDEV_CLASS_OFF,
151663207adSDon Brady 	ZTEST_VDEV_CLASS_ON,
152663207adSDon Brady 	ZTEST_VDEV_CLASS_RND
153663207adSDon Brady };
154663207adSDon Brady 
155420dfc95SChris Siden typedef struct ztest_shared_opts {
1569adfa60dSMatthew Ahrens 	char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
1579adfa60dSMatthew Ahrens 	char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
158420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
159420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
160420dfc95SChris Siden 	uint64_t zo_vdevs;
161420dfc95SChris Siden 	uint64_t zo_vdevtime;
162420dfc95SChris Siden 	size_t zo_vdev_size;
163420dfc95SChris Siden 	int zo_ashift;
164420dfc95SChris Siden 	int zo_mirrors;
165420dfc95SChris Siden 	int zo_raidz;
166420dfc95SChris Siden 	int zo_raidz_parity;
167420dfc95SChris Siden 	int zo_datasets;
168420dfc95SChris Siden 	int zo_threads;
169420dfc95SChris Siden 	uint64_t zo_passtime;
170420dfc95SChris Siden 	uint64_t zo_killrate;
171420dfc95SChris Siden 	int zo_verbose;
172420dfc95SChris Siden 	int zo_init;
173420dfc95SChris Siden 	uint64_t zo_time;
174420dfc95SChris Siden 	uint64_t zo_maxloops;
175243952c7SMatt Ahrens 	uint64_t zo_metaslab_force_ganging;
176e0f1c0afSOlaf Faaland 	int zo_mmp_test;
177663207adSDon Brady 	int zo_special_vdevs;
178420dfc95SChris Siden } ztest_shared_opts_t;
179420dfc95SChris Siden 
180420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
181420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
182420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
183420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
184420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
185420dfc95SChris Siden 	.zo_vdevs = 5,
186420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
187420dfc95SChris Siden 	.zo_mirrors = 2,
188420dfc95SChris Siden 	.zo_raidz = 4,
189420dfc95SChris Siden 	.zo_raidz_parity = 1,
1908363e80aSGeorge Wilson 	.zo_vdev_size = SPA_MINDEVSIZE * 4,	/* 256m default size */
191420dfc95SChris Siden 	.zo_datasets = 7,
192420dfc95SChris Siden 	.zo_threads = 23,
193420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
194420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
195420dfc95SChris Siden 	.zo_verbose = 0,
196e0f1c0afSOlaf Faaland 	.zo_mmp_test = 0,
197420dfc95SChris Siden 	.zo_init = 1,
198420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
199420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
200663207adSDon Brady 	.zo_metaslab_force_ganging = 32 << 10,
201663207adSDon Brady 	.zo_special_vdevs = ZTEST_VDEV_CLASS_RND,
202420dfc95SChris Siden };
203420dfc95SChris Siden 
204243952c7SMatt Ahrens extern uint64_t metaslab_force_ganging;
205420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
20669962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
20730beaff4SGeorge Wilson extern int metaslab_preload_limit;
208dcbf3bd6SGeorge Wilson extern boolean_t zfs_compressed_arc_enabled;
209770499e1SDan Kimmel extern boolean_t zfs_abd_scatter_enabled;
21054811da5SToomas Soome extern int dmu_object_alloc_chunk_shift;
21117f11284SSerapheim Dimitropoulos extern boolean_t zfs_force_some_double_word_sm_entries;
212a21fe349SBrian Behlendorf extern unsigned long zfs_reconstruct_indirect_damage_fraction;
213420dfc95SChris Siden 
214420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
215420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
216eb633035STom Caputi static char *ztest_wkeydata = "abcdefghijklmnopqrstuvwxyz012345";
217420dfc95SChris Siden 
218420dfc95SChris Siden typedef struct ztest_shared_ds {
219420dfc95SChris Siden 	uint64_t	zd_seq;
220420dfc95SChris Siden } ztest_shared_ds_t;
221420dfc95SChris Siden 
222420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
223420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
224fa9e4066Sahrens 
225b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
226420dfc95SChris Siden #define	MAXFAULTS() \
227420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
228b24ab676SJeff Bonwick 
229b24ab676SJeff Bonwick enum ztest_io_type {
230b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
231b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
232b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
233b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
234b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
23580901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
236b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
237b24ab676SJeff Bonwick };
238b24ab676SJeff Bonwick 
239e05725b1Sbonwick typedef struct ztest_block_tag {
240b24ab676SJeff Bonwick 	uint64_t	bt_magic;
241e05725b1Sbonwick 	uint64_t	bt_objset;
242e05725b1Sbonwick 	uint64_t	bt_object;
24354811da5SToomas Soome 	uint64_t	bt_dnodesize;
244e05725b1Sbonwick 	uint64_t	bt_offset;
245b24ab676SJeff Bonwick 	uint64_t	bt_gen;
246e05725b1Sbonwick 	uint64_t	bt_txg;
247b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
248e05725b1Sbonwick } ztest_block_tag_t;
249e05725b1Sbonwick 
250b24ab676SJeff Bonwick typedef struct bufwad {
251b24ab676SJeff Bonwick 	uint64_t	bw_index;
252b24ab676SJeff Bonwick 	uint64_t	bw_txg;
253b24ab676SJeff Bonwick 	uint64_t	bw_data;
254b24ab676SJeff Bonwick } bufwad_t;
255b24ab676SJeff Bonwick 
256b24ab676SJeff Bonwick /*
25779315247SMatthew Ahrens  * It would be better to use a rangelock_t per object.  Unfortunately
25879315247SMatthew Ahrens  * the rangelock_t is not a drop-in replacement for rl_t, because we
25979315247SMatthew Ahrens  * still need to map from object ID to rangelock_t.
260b24ab676SJeff Bonwick  */
261b24ab676SJeff Bonwick typedef enum {
262b24ab676SJeff Bonwick 	RL_READER,
263b24ab676SJeff Bonwick 	RL_WRITER,
264b24ab676SJeff Bonwick 	RL_APPEND
265b24ab676SJeff Bonwick } rl_type_t;
266b24ab676SJeff Bonwick 
267b24ab676SJeff Bonwick typedef struct rll {
268b24ab676SJeff Bonwick 	void		*rll_writer;
269b24ab676SJeff Bonwick 	int		rll_readers;
270f06dce2cSAndrew Stormont 	kmutex_t	rll_lock;
271f06dce2cSAndrew Stormont 	kcondvar_t	rll_cv;
272b24ab676SJeff Bonwick } rll_t;
273b24ab676SJeff Bonwick 
274b24ab676SJeff Bonwick typedef struct rl {
275b24ab676SJeff Bonwick 	uint64_t	rl_object;
276b24ab676SJeff Bonwick 	uint64_t	rl_offset;
277b24ab676SJeff Bonwick 	uint64_t	rl_size;
278b24ab676SJeff Bonwick 	rll_t		*rl_lock;
279b24ab676SJeff Bonwick } rl_t;
280b24ab676SJeff Bonwick 
281b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
282b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
283b24ab676SJeff Bonwick 
284b24ab676SJeff Bonwick /*
285b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
286b24ab676SJeff Bonwick  */
287b24ab676SJeff Bonwick typedef struct ztest_od {
288b24ab676SJeff Bonwick 	uint64_t	od_dir;
289b24ab676SJeff Bonwick 	uint64_t	od_object;
290b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
291b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
292b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
293b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
29454811da5SToomas Soome 	uint64_t	od_crdnodesize;
295b24ab676SJeff Bonwick 	uint64_t	od_gen;
296b24ab676SJeff Bonwick 	uint64_t	od_crgen;
2979adfa60dSMatthew Ahrens 	char		od_name[ZFS_MAX_DATASET_NAME_LEN];
298b24ab676SJeff Bonwick } ztest_od_t;
299fa9e4066Sahrens 
300b24ab676SJeff Bonwick /*
301b24ab676SJeff Bonwick  * Per-dataset state.
302b24ab676SJeff Bonwick  */
303b24ab676SJeff Bonwick typedef struct ztest_ds {
304420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
305b24ab676SJeff Bonwick 	objset_t	*zd_os;
306f06dce2cSAndrew Stormont 	krwlock_t	zd_zilog_lock;
307b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
308b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
3099adfa60dSMatthew Ahrens 	char		zd_name[ZFS_MAX_DATASET_NAME_LEN];
310f06dce2cSAndrew Stormont 	kmutex_t	zd_dirobj_lock;
311b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
312b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
313b24ab676SJeff Bonwick } ztest_ds_t;
314b24ab676SJeff Bonwick 
315b24ab676SJeff Bonwick /*
316b24ab676SJeff Bonwick  * Per-iteration state.
317b24ab676SJeff Bonwick  */
318b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
319b24ab676SJeff Bonwick 
320b24ab676SJeff Bonwick typedef struct ztest_info {
321b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
322b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
323b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
324b24ab676SJeff Bonwick } ztest_info_t;
325fa9e4066Sahrens 
326420dfc95SChris Siden typedef struct ztest_shared_callstate {
327420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
328420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
329420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
330420dfc95SChris Siden } ztest_shared_callstate_t;
331420dfc95SChris Siden 
332420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
333420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
334420dfc95SChris Siden 
335fa9e4066Sahrens /*
336fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
337fa9e4066Sahrens  */
338fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
339fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
340fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
34154811da5SToomas Soome ztest_func_t ztest_dmu_object_next_chunk;
342d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
343fa9e4066Sahrens ztest_func_t ztest_zap;
344fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
345b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
346c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
347b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
348fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
349b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
350b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
351fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
352b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
353b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
354fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
355fa9e4066Sahrens ztest_func_t ztest_fault_inject;
356b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
357b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
358e0f1c0afSOlaf Faaland ztest_func_t ztest_mmp_enable_disable;
359b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
360b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
361fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
362fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
363fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
364663207adSDon Brady ztest_func_t ztest_vdev_class_add;
365e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3661195e687SMark J Musante ztest_func_t ztest_split_pool;
367e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
36825345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
3695cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_device_removal;
3705cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_remap_blocks;
37186714001SSerapheim Dimitropoulos ztest_func_t ztest_spa_checkpoint_create_discard;
372094e47e9SGeorge Wilson ztest_func_t ztest_initialize;
373*084fd14fSBrian Behlendorf ztest_func_t ztest_trim;
37454811da5SToomas Soome ztest_func_t ztest_verify_dnode_bt;
375fa9e4066Sahrens 
376b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
377b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
378b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
379b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
380b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
381fa9e4066Sahrens 
382fa9e4066Sahrens ztest_info_t ztest_info[] = {
383e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
384b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
385e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
38654811da5SToomas Soome 	{ ztest_dmu_object_next_chunk,		1,	&zopt_sometimes	},
387b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
388e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
389e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3901195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
391b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
392c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
393b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
394b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
395b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
396b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
397b24ab676SJeff Bonwick #if 0
398b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
399b24ab676SJeff Bonwick #endif
400b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
401b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
402b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
403a3874b8bSToomas Soome 	{ ztest_fault_inject,			1,	&zopt_incessant	},
404b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
4055c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
406e0f1c0afSOlaf Faaland 	{ ztest_mmp_enable_disable,		1,	&zopt_sometimes	},
4072c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
408a3874b8bSToomas Soome 	{ ztest_scrub,				1,	&zopt_often	},
40925345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
410b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
411a3874b8bSToomas Soome 	{ ztest_vdev_attach_detach,		1,	&zopt_incessant	},
412e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
413420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
414420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
415663207adSDon Brady 	{ ztest_vdev_class_add,			1,
416663207adSDon Brady 	    &ztest_opts.zo_vdevtime				},
417420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
418420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
4195cabbc6bSPrashanth Sreenivasa 	{ ztest_device_removal,			1,	&zopt_sometimes	},
42086714001SSerapheim Dimitropoulos 	{ ztest_remap_blocks,			1,	&zopt_sometimes },
421094e47e9SGeorge Wilson 	{ ztest_spa_checkpoint_create_discard,	1,	&zopt_rarely	},
42254811da5SToomas Soome 	{ ztest_initialize,			1,	&zopt_sometimes },
423*084fd14fSBrian Behlendorf 	{ ztest_trim,				1,	&zopt_sometimes },
42454811da5SToomas Soome 	{ ztest_verify_dnode_bt,		1,	&zopt_sometimes }
425fa9e4066Sahrens };
426fa9e4066Sahrens 
427fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
428fa9e4066Sahrens 
429d20e665cSRicardo M. Correia /*
430d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
431d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
432d20e665cSRicardo M. Correia  */
433d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
434f06dce2cSAndrew Stormont 	kmutex_t zcl_callbacks_lock;
435d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
436d20e665cSRicardo M. Correia } ztest_cb_list_t;
437d20e665cSRicardo M. Correia 
438fa9e4066Sahrens /*
439fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
440fa9e4066Sahrens  */
441fa9e4066Sahrens typedef struct ztest_shared {
442420dfc95SChris Siden 	boolean_t	zs_do_init;
443b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
444b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
445b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
446b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
447b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
448b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
44988ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
450e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
451fa9e4066Sahrens 	uint64_t	zs_alloc;
452fa9e4066Sahrens 	uint64_t	zs_space;
4531195e687SMark J Musante 	uint64_t	zs_splits;
4541195e687SMark J Musante 	uint64_t	zs_mirrors;
455420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
456420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
457420dfc95SChris Siden 	uint64_t	zs_guid;
458fa9e4066Sahrens } ztest_shared_t;
459fa9e4066Sahrens 
460b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
461b24ab676SJeff Bonwick 
462fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
463e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
464b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
465fa9e4066Sahrens 
466420dfc95SChris Siden static spa_t *ztest_spa = NULL;
467420dfc95SChris Siden static ztest_ds_t *ztest_ds;
468420dfc95SChris Siden 
469f06dce2cSAndrew Stormont static kmutex_t ztest_vdev_lock;
4703a4b1be9SMatthew Ahrens static boolean_t ztest_device_removal_active = B_FALSE;
47154811da5SToomas Soome static kmutex_t ztest_checkpoint_lock;
472dfbb9432SGeorge Wilson 
473dfbb9432SGeorge Wilson /*
474dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
475dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
476dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
477dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
478dfbb9432SGeorge Wilson  */
479f06dce2cSAndrew Stormont static krwlock_t ztest_name_lock;
480fa9e4066Sahrens 
481420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
482e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4830a4e9518Sgw 
484d20e665cSRicardo M. Correia /* Global commit callback list */
485d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
486d20e665cSRicardo M. Correia 
487b24ab676SJeff Bonwick enum ztest_object {
488b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
489b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
490b24ab676SJeff Bonwick 	ZTEST_OBJECTS
491b24ab676SJeff Bonwick };
492fa9e4066Sahrens 
4931ce825d8Sraf static void usage(boolean_t) __NORETURN;
494f1b4288bSvb 
495fa9e4066Sahrens /*
496fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
497fa9e4066Sahrens  * debugging facilities.
498fa9e4066Sahrens  */
499fa9e4066Sahrens const char *
500fa9e4066Sahrens _umem_debug_init()
501fa9e4066Sahrens {
502fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
503fa9e4066Sahrens }
504fa9e4066Sahrens 
505fa9e4066Sahrens const char *
506fa9e4066Sahrens _umem_logging_init(void)
507fa9e4066Sahrens {
508fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
509fa9e4066Sahrens }
510fa9e4066Sahrens 
511fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
512fa9e4066Sahrens 
513fa9e4066Sahrens char *fatal_msg;
514fa9e4066Sahrens 
515fa9e4066Sahrens static void
516fa9e4066Sahrens fatal(int do_perror, char *message, ...)
517fa9e4066Sahrens {
518fa9e4066Sahrens 	va_list args;
519fa9e4066Sahrens 	int save_errno = errno;
520fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
521fa9e4066Sahrens 
522fa9e4066Sahrens 	(void) fflush(stdout);
523fa9e4066Sahrens 
524fa9e4066Sahrens 	va_start(args, message);
525fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
526fa9e4066Sahrens 	/* LINTED */
527fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
528fa9e4066Sahrens 	va_end(args);
529fa9e4066Sahrens 	if (do_perror) {
530fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
531fa9e4066Sahrens 		    ": %s", strerror(save_errno));
532fa9e4066Sahrens 	}
533fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
534fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
535fa9e4066Sahrens 	if (ztest_dump_core)
536fa9e4066Sahrens 		abort();
537fa9e4066Sahrens 	exit(3);
538fa9e4066Sahrens }
539fa9e4066Sahrens 
540fa9e4066Sahrens static int
541fa9e4066Sahrens str2shift(const char *buf)
542fa9e4066Sahrens {
543fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
544fa9e4066Sahrens 	int i;
545fa9e4066Sahrens 
546fa9e4066Sahrens 	if (buf[0] == '\0')
547fa9e4066Sahrens 		return (0);
548fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
549fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
550fa9e4066Sahrens 			break;
551fa9e4066Sahrens 	}
552f1b4288bSvb 	if (i == strlen(ends)) {
553f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
554f1b4288bSvb 		    buf);
555f1b4288bSvb 		usage(B_FALSE);
556f1b4288bSvb 	}
557fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
558fa9e4066Sahrens 		return (10*i);
559fa9e4066Sahrens 	}
560f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
561f1b4288bSvb 	usage(B_FALSE);
562f1b4288bSvb 	/* NOTREACHED */
563fa9e4066Sahrens }
564fa9e4066Sahrens 
565fa9e4066Sahrens static uint64_t
566fa9e4066Sahrens nicenumtoull(const char *buf)
567fa9e4066Sahrens {
568fa9e4066Sahrens 	char *end;
569fa9e4066Sahrens 	uint64_t val;
570fa9e4066Sahrens 
571fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
572fa9e4066Sahrens 	if (end == buf) {
573f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
574f1b4288bSvb 		usage(B_FALSE);
575fa9e4066Sahrens 	} else if (end[0] == '.') {
576fa9e4066Sahrens 		double fval = strtod(buf, &end);
577fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
578f1b4288bSvb 		if (fval > UINT64_MAX) {
579f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
580f1b4288bSvb 			    buf);
581f1b4288bSvb 			usage(B_FALSE);
582f1b4288bSvb 		}
583fa9e4066Sahrens 		val = (uint64_t)fval;
584fa9e4066Sahrens 	} else {
585fa9e4066Sahrens 		int shift = str2shift(end);
586f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
587f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
588f1b4288bSvb 			    buf);
589f1b4288bSvb 			usage(B_FALSE);
590f1b4288bSvb 		}
591fa9e4066Sahrens 		val <<= shift;
592fa9e4066Sahrens 	}
593fa9e4066Sahrens 	return (val);
594fa9e4066Sahrens }
595fa9e4066Sahrens 
596fa9e4066Sahrens static void
597f1b4288bSvb usage(boolean_t requested)
598fa9e4066Sahrens {
599420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
600420dfc95SChris Siden 
6010a055120SJason King 	char nice_vdev_size[NN_NUMBUF_SZ];
602243952c7SMatt Ahrens 	char nice_force_ganging[NN_NUMBUF_SZ];
603f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
604fa9e4066Sahrens 
6050a055120SJason King 	nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
606243952c7SMatt Ahrens 	nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
607243952c7SMatt Ahrens 	    sizeof (nice_force_ganging));
608fa9e4066Sahrens 
609f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
610fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
611fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
6122215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
613fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
614fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
61599653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
616fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
617fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
618fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
6192215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
6202215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
621fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
6222215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
623e0f1c0afSOlaf Faaland 	    "\t[-M] Multi-host simulate pool imported on remote host\n"
6242215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
6252215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
6262215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
6272215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
6282215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
629420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
630663207adSDon Brady 	    "\t[-C vdev class state (default: random)] special=on|off|random\n"
6310e60744cSPavel Zakharov 	    "\t[-o variable=value] ... set global variable to an unsigned\n"
6320e60744cSPavel Zakharov 	    "\t    32-bit integer value\n"
633f1b4288bSvb 	    "\t[-h] (print help)\n"
634fa9e4066Sahrens 	    "",
635420dfc95SChris Siden 	    zo->zo_pool,
636420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
6370a4e9518Sgw 	    nice_vdev_size,				/* -s */
638420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
639420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
640420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
641420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
642420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
643420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
644243952c7SMatt Ahrens 	    nice_force_ganging,				/* -g */
645420dfc95SChris Siden 	    zo->zo_init,				/* -i */
646420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
647420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
648420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
649420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
650420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
651420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
652f1b4288bSvb 	exit(requested ? 0 : 1);
653fa9e4066Sahrens }
654fa9e4066Sahrens 
655663207adSDon Brady 
656663207adSDon Brady static void
657663207adSDon Brady ztest_parse_name_value(const char *input, ztest_shared_opts_t *zo)
658663207adSDon Brady {
659663207adSDon Brady 	char name[32];
660663207adSDon Brady 	char *value;
661663207adSDon Brady 	int state = ZTEST_VDEV_CLASS_RND;
662663207adSDon Brady 
663663207adSDon Brady 	(void) strlcpy(name, input, sizeof (name));
664663207adSDon Brady 
665663207adSDon Brady 	value = strchr(name, '=');
666663207adSDon Brady 	if (value == NULL) {
667663207adSDon Brady 		(void) fprintf(stderr, "missing value in property=value "
668663207adSDon Brady 		    "'-C' argument (%s)\n", input);
669663207adSDon Brady 		usage(B_FALSE);
670663207adSDon Brady 	}
671663207adSDon Brady 	*(value) = '\0';
672663207adSDon Brady 	value++;
673663207adSDon Brady 
674663207adSDon Brady 	if (strcmp(value, "on") == 0) {
675663207adSDon Brady 		state = ZTEST_VDEV_CLASS_ON;
676663207adSDon Brady 	} else if (strcmp(value, "off") == 0) {
677663207adSDon Brady 		state = ZTEST_VDEV_CLASS_OFF;
678663207adSDon Brady 	} else if (strcmp(value, "random") == 0) {
679663207adSDon Brady 		state = ZTEST_VDEV_CLASS_RND;
680663207adSDon Brady 	} else {
681663207adSDon Brady 		(void) fprintf(stderr, "invalid property value '%s'\n", value);
682663207adSDon Brady 		usage(B_FALSE);
683663207adSDon Brady 	}
684663207adSDon Brady 
685663207adSDon Brady 	if (strcmp(name, "special") == 0) {
686663207adSDon Brady 		zo->zo_special_vdevs = state;
687663207adSDon Brady 	} else {
688663207adSDon Brady 		(void) fprintf(stderr, "invalid property name '%s'\n", name);
689663207adSDon Brady 		usage(B_FALSE);
690663207adSDon Brady 	}
691663207adSDon Brady 	if (zo->zo_verbose >= 3)
692663207adSDon Brady 		(void) printf("%s vdev state is '%s'\n", name, value);
693663207adSDon Brady }
694663207adSDon Brady 
695fa9e4066Sahrens static void
696fa9e4066Sahrens process_options(int argc, char **argv)
697fa9e4066Sahrens {
698420dfc95SChris Siden 	char *path;
699420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
700420dfc95SChris Siden 
701fa9e4066Sahrens 	int opt;
702fa9e4066Sahrens 	uint64_t value;
703420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
704fa9e4066Sahrens 
705420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
706fa9e4066Sahrens 
707fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
708663207adSDon Brady 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:")) != EOF) {
709fa9e4066Sahrens 		value = 0;
710fa9e4066Sahrens 		switch (opt) {
7113d7072f8Seschrock 		case 'v':
7123d7072f8Seschrock 		case 's':
7133d7072f8Seschrock 		case 'a':
7143d7072f8Seschrock 		case 'm':
7153d7072f8Seschrock 		case 'r':
7163d7072f8Seschrock 		case 'R':
7173d7072f8Seschrock 		case 'd':
7183d7072f8Seschrock 		case 't':
7193d7072f8Seschrock 		case 'g':
7203d7072f8Seschrock 		case 'i':
7213d7072f8Seschrock 		case 'k':
7223d7072f8Seschrock 		case 'T':
7233d7072f8Seschrock 		case 'P':
7242215e990SMark J Musante 		case 'F':
725fa9e4066Sahrens 			value = nicenumtoull(optarg);
726fa9e4066Sahrens 		}
727fa9e4066Sahrens 		switch (opt) {
7283d7072f8Seschrock 		case 'v':
729420dfc95SChris Siden 			zo->zo_vdevs = value;
730fa9e4066Sahrens 			break;
7313d7072f8Seschrock 		case 's':
732420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
733fa9e4066Sahrens 			break;
7343d7072f8Seschrock 		case 'a':
735420dfc95SChris Siden 			zo->zo_ashift = value;
736ecc2d604Sbonwick 			break;
7373d7072f8Seschrock 		case 'm':
738420dfc95SChris Siden 			zo->zo_mirrors = value;
739fa9e4066Sahrens 			break;
7403d7072f8Seschrock 		case 'r':
741420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
742fa9e4066Sahrens 			break;
7433d7072f8Seschrock 		case 'R':
744420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
74599653d4eSeschrock 			break;
7463d7072f8Seschrock 		case 'd':
747420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
748fa9e4066Sahrens 			break;
7493d7072f8Seschrock 		case 't':
750420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
751fa9e4066Sahrens 			break;
7523d7072f8Seschrock 		case 'g':
753243952c7SMatt Ahrens 			zo->zo_metaslab_force_ganging =
754243952c7SMatt Ahrens 			    MAX(SPA_MINBLOCKSIZE << 1, value);
755fa9e4066Sahrens 			break;
7563d7072f8Seschrock 		case 'i':
757420dfc95SChris Siden 			zo->zo_init = value;
758fa9e4066Sahrens 			break;
7593d7072f8Seschrock 		case 'k':
760420dfc95SChris Siden 			zo->zo_killrate = value;
761fa9e4066Sahrens 			break;
7623d7072f8Seschrock 		case 'p':
763420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
764420dfc95SChris Siden 			    sizeof (zo->zo_pool));
765fa9e4066Sahrens 			break;
7663d7072f8Seschrock 		case 'f':
767420dfc95SChris Siden 			path = realpath(optarg, NULL);
768420dfc95SChris Siden 			if (path == NULL) {
769420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
770420dfc95SChris Siden 				    optarg, strerror(errno));
771420dfc95SChris Siden 				usage(B_FALSE);
772420dfc95SChris Siden 			} else {
773420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
774420dfc95SChris Siden 				    sizeof (zo->zo_dir));
775420dfc95SChris Siden 			}
776fa9e4066Sahrens 			break;
777e0f1c0afSOlaf Faaland 		case 'M':
778e0f1c0afSOlaf Faaland 			zo->zo_mmp_test = 1;
779e0f1c0afSOlaf Faaland 			break;
7803d7072f8Seschrock 		case 'V':
781420dfc95SChris Siden 			zo->zo_verbose++;
782fa9e4066Sahrens 			break;
7833d7072f8Seschrock 		case 'E':
784420dfc95SChris Siden 			zo->zo_init = 0;
785fa9e4066Sahrens 			break;
7863d7072f8Seschrock 		case 'T':
787420dfc95SChris Siden 			zo->zo_time = value;
788fa9e4066Sahrens 			break;
7893d7072f8Seschrock 		case 'P':
790420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
791fa9e4066Sahrens 			break;
7922215e990SMark J Musante 		case 'F':
793420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
794420dfc95SChris Siden 			break;
795420dfc95SChris Siden 		case 'B':
796420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7972215e990SMark J Musante 			break;
798663207adSDon Brady 		case 'C':
799663207adSDon Brady 			ztest_parse_name_value(optarg, zo);
800663207adSDon Brady 			break;
8010e60744cSPavel Zakharov 		case 'o':
8020e60744cSPavel Zakharov 			if (set_global_var(optarg) != 0)
8030e60744cSPavel Zakharov 				usage(B_FALSE);
8040e60744cSPavel Zakharov 			break;
8053d7072f8Seschrock 		case 'h':
806f1b4288bSvb 			usage(B_TRUE);
807f1b4288bSvb 			break;
8083d7072f8Seschrock 		case '?':
8093d7072f8Seschrock 		default:
810f1b4288bSvb 			usage(B_FALSE);
811fa9e4066Sahrens 			break;
812fa9e4066Sahrens 		}
813fa9e4066Sahrens 	}
814fa9e4066Sahrens 
815420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
81699653d4eSeschrock 
817420dfc95SChris Siden 	zo->zo_vdevtime =
818420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
819b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
820420dfc95SChris Siden 
821420dfc95SChris Siden 	if (strlen(altdir) > 0) {
822741652b0SEtienne Dechamps 		char *cmd;
823741652b0SEtienne Dechamps 		char *realaltdir;
824420dfc95SChris Siden 		char *bin;
825420dfc95SChris Siden 		char *ztest;
826420dfc95SChris Siden 		char *isa;
827420dfc95SChris Siden 		int isalen;
828420dfc95SChris Siden 
829741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
830741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
831741652b0SEtienne Dechamps 
832741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
833420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
834420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
835420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
836420dfc95SChris Siden 			    altdir);
837420dfc95SChris Siden 		}
838420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
839420dfc95SChris Siden 
840420dfc95SChris Siden 		/*
841420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
842420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
843420dfc95SChris Siden 		 * 32 or 64 bit binaries.
844420dfc95SChris Siden 		 */
845420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
846420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
847420dfc95SChris Siden 		isa = bin + 9;
848420dfc95SChris Siden 		isalen = ztest - isa;
849420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
850420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
851420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
852420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
853420dfc95SChris Siden 
854420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
855420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
856420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
857420dfc95SChris Siden 			    zo->zo_alt_ztest);
858420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
859420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
860420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
861420dfc95SChris Siden 			    zo->zo_alt_libpath);
862420dfc95SChris Siden 		}
863741652b0SEtienne Dechamps 
864741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
865741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
866420dfc95SChris Siden 	}
867fa9e4066Sahrens }
868fa9e4066Sahrens 
869b24ab676SJeff Bonwick static void
870b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
871b24ab676SJeff Bonwick {
872420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
873420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
874b4952e17SGeorge Wilson 
875b4952e17SGeorge Wilson 	/*
876b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
8775cabbc6bSPrashanth Sreenivasa 	 * See comment above spa_write_cachefile().
878b4952e17SGeorge Wilson 	 */
879b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
8805cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
881b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
882b4952e17SGeorge Wilson 
883b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
884b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
885b24ab676SJeff Bonwick }
886b24ab676SJeff Bonwick 
887b24ab676SJeff Bonwick static uint64_t
888b24ab676SJeff Bonwick ztest_random(uint64_t range)
889b24ab676SJeff Bonwick {
890b24ab676SJeff Bonwick 	uint64_t r;
891b24ab676SJeff Bonwick 
892741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
893741652b0SEtienne Dechamps 
894b24ab676SJeff Bonwick 	if (range == 0)
895b24ab676SJeff Bonwick 		return (0);
896b24ab676SJeff Bonwick 
897741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
898b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
899b24ab676SJeff Bonwick 
900b24ab676SJeff Bonwick 	return (r % range);
901b24ab676SJeff Bonwick }
902b24ab676SJeff Bonwick 
903b24ab676SJeff Bonwick /* ARGSUSED */
904b24ab676SJeff Bonwick static void
905b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
906b24ab676SJeff Bonwick {
907b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
908b24ab676SJeff Bonwick }
909b24ab676SJeff Bonwick 
910ecc2d604Sbonwick static uint64_t
911ecc2d604Sbonwick ztest_get_ashift(void)
912ecc2d604Sbonwick {
913420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
9142a104a52SAlex Reece 		return (SPA_MINBLOCKSHIFT + ztest_random(5));
915420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
916ecc2d604Sbonwick }
917ecc2d604Sbonwick 
918fa9e4066Sahrens static nvlist_t *
91925345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
920fa9e4066Sahrens {
921e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
922fa9e4066Sahrens 	uint64_t vdev;
923fa9e4066Sahrens 	nvlist_t *file;
924fa9e4066Sahrens 
925e14bb325SJeff Bonwick 	if (ashift == 0)
926e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
927e14bb325SJeff Bonwick 
928e14bb325SJeff Bonwick 	if (path == NULL) {
929e14bb325SJeff Bonwick 		path = pathbuf;
930fa9e4066Sahrens 
931e14bb325SJeff Bonwick 		if (aux != NULL) {
932e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
933420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
934420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
93525345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
93625345e46SGeorge Wilson 			    aux, vdev);
937e14bb325SJeff Bonwick 		} else {
93888ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
939420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
940420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
94125345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
942e14bb325SJeff Bonwick 		}
943e14bb325SJeff Bonwick 	}
944e14bb325SJeff Bonwick 
945e14bb325SJeff Bonwick 	if (size != 0) {
946e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
947fa9e4066Sahrens 		if (fd == -1)
948e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
949fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
950e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
951fa9e4066Sahrens 		(void) close(fd);
952fa9e4066Sahrens 	}
953fa9e4066Sahrens 
954fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
955fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
956e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
957ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
958fa9e4066Sahrens 
959fa9e4066Sahrens 	return (file);
960fa9e4066Sahrens }
961fa9e4066Sahrens 
962fa9e4066Sahrens static nvlist_t *
96325345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
96425345e46SGeorge Wilson     uint64_t ashift, int r)
965fa9e4066Sahrens {
966fa9e4066Sahrens 	nvlist_t *raidz, **child;
967fa9e4066Sahrens 	int c;
968fa9e4066Sahrens 
969fa9e4066Sahrens 	if (r < 2)
97025345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
971fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
972fa9e4066Sahrens 
973fa9e4066Sahrens 	for (c = 0; c < r; c++)
97425345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
975fa9e4066Sahrens 
976fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
977fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
978fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
97999653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
980420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
981fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
982fa9e4066Sahrens 	    child, r) == 0);
983fa9e4066Sahrens 
984fa9e4066Sahrens 	for (c = 0; c < r; c++)
985fa9e4066Sahrens 		nvlist_free(child[c]);
986fa9e4066Sahrens 
987fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
988fa9e4066Sahrens 
989fa9e4066Sahrens 	return (raidz);
990fa9e4066Sahrens }
991fa9e4066Sahrens 
992fa9e4066Sahrens static nvlist_t *
99325345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
99425345e46SGeorge Wilson     uint64_t ashift, int r, int m)
995fa9e4066Sahrens {
996fa9e4066Sahrens 	nvlist_t *mirror, **child;
997fa9e4066Sahrens 	int c;
998fa9e4066Sahrens 
999fa9e4066Sahrens 	if (m < 1)
100025345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
1001fa9e4066Sahrens 
1002fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	for (c = 0; c < m; c++)
100525345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1006fa9e4066Sahrens 
1007fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1008fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1009fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
1010fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1011fa9e4066Sahrens 	    child, m) == 0);
1012fa9e4066Sahrens 
1013fa9e4066Sahrens 	for (c = 0; c < m; c++)
1014fa9e4066Sahrens 		nvlist_free(child[c]);
1015fa9e4066Sahrens 
1016fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
1017fa9e4066Sahrens 
1018fa9e4066Sahrens 	return (mirror);
1019fa9e4066Sahrens }
1020fa9e4066Sahrens 
1021fa9e4066Sahrens static nvlist_t *
102225345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1023663207adSDon Brady     const char *class, int r, int m, int t)
1024fa9e4066Sahrens {
1025fa9e4066Sahrens 	nvlist_t *root, **child;
1026fa9e4066Sahrens 	int c;
1027663207adSDon Brady 	boolean_t log;
1028fa9e4066Sahrens 
1029fa9e4066Sahrens 	ASSERT(t > 0);
1030fa9e4066Sahrens 
1031663207adSDon Brady 	log = (class != NULL && strcmp(class, "log") == 0);
1032663207adSDon Brady 
1033fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1034fa9e4066Sahrens 
103531157203SJeff Bonwick 	for (c = 0; c < t; c++) {
103625345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
103725345e46SGeorge Wilson 		    r, m);
103831157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
103931157203SJeff Bonwick 		    log) == 0);
1040663207adSDon Brady 
1041663207adSDon Brady 		if (class != NULL && class[0] != '\0') {
1042663207adSDon Brady 			ASSERT(m > 1 || log);   /* expecting a mirror */
1043663207adSDon Brady 			VERIFY(nvlist_add_string(child[c],
1044663207adSDon Brady 			    ZPOOL_CONFIG_ALLOCATION_BIAS, class) == 0);
1045663207adSDon Brady 		}
104631157203SJeff Bonwick 	}
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1049fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1050e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1051fa9e4066Sahrens 	    child, t) == 0);
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	for (c = 0; c < t; c++)
1054fa9e4066Sahrens 		nvlist_free(child[c]);
1055fa9e4066Sahrens 
1056fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
1057fa9e4066Sahrens 
1058fa9e4066Sahrens 	return (root);
1059fa9e4066Sahrens }
1060fa9e4066Sahrens 
106125345e46SGeorge Wilson /*
106225345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
106325345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
106425345e46SGeorge Wilson  */
106525345e46SGeorge Wilson static uint64_t
106625345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
106725345e46SGeorge Wilson {
106825345e46SGeorge Wilson 	uint64_t version = initial_version;
106925345e46SGeorge Wilson 
107025345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
107125345e46SGeorge Wilson 		version = version +
107225345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
107325345e46SGeorge Wilson 	}
107425345e46SGeorge Wilson 
107525345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
107625345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
107725345e46SGeorge Wilson 
107825345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
107925345e46SGeorge Wilson 	return (version);
108025345e46SGeorge Wilson }
108125345e46SGeorge Wilson 
1082b24ab676SJeff Bonwick static int
1083b24ab676SJeff Bonwick ztest_random_blocksize(void)
1084fa9e4066Sahrens {
1085b5152584SMatthew Ahrens 	uint64_t block_shift;
1086663207adSDon Brady 
1087663207adSDon Brady 	ASSERT(ztest_spa->spa_max_ashift != 0);
1088663207adSDon Brady 
1089b5152584SMatthew Ahrens 	/*
1090b5152584SMatthew Ahrens 	 * Choose a block size >= the ashift.
1091b5152584SMatthew Ahrens 	 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1092b5152584SMatthew Ahrens 	 */
1093b5152584SMatthew Ahrens 	int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1094b5152584SMatthew Ahrens 	if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1095b5152584SMatthew Ahrens 		maxbs = 20;
109681cd5c55SMatthew Ahrens 	block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
10972a104a52SAlex Reece 	return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1098b24ab676SJeff Bonwick }
1099fa9e4066Sahrens 
110054811da5SToomas Soome static int
110154811da5SToomas Soome ztest_random_dnodesize(void)
110254811da5SToomas Soome {
110354811da5SToomas Soome 	int slots;
110454811da5SToomas Soome 	int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
110554811da5SToomas Soome 
110654811da5SToomas Soome 	if (max_slots == DNODE_MIN_SLOTS)
110754811da5SToomas Soome 		return (DNODE_MIN_SIZE);
110854811da5SToomas Soome 
110954811da5SToomas Soome 	/*
111054811da5SToomas Soome 	 * Weight the random distribution more heavily toward smaller
111154811da5SToomas Soome 	 * dnode sizes since that is more likely to reflect real-world
111254811da5SToomas Soome 	 * usage.
111354811da5SToomas Soome 	 */
111454811da5SToomas Soome 	ASSERT3U(max_slots, >, 4);
111554811da5SToomas Soome 	switch (ztest_random(10)) {
111654811da5SToomas Soome 	case 0:
111754811da5SToomas Soome 		slots = 5 + ztest_random(max_slots - 4);
111854811da5SToomas Soome 		break;
111954811da5SToomas Soome 	case 1 ... 4:
112054811da5SToomas Soome 		slots = 2 + ztest_random(3);
112154811da5SToomas Soome 		break;
112254811da5SToomas Soome 	default:
112354811da5SToomas Soome 		slots = 1;
112454811da5SToomas Soome 		break;
112554811da5SToomas Soome 	}
112654811da5SToomas Soome 
112754811da5SToomas Soome 	return (slots << DNODE_SHIFT);
112854811da5SToomas Soome }
112954811da5SToomas Soome 
1130b24ab676SJeff Bonwick static int
1131b24ab676SJeff Bonwick ztest_random_ibshift(void)
1132b24ab676SJeff Bonwick {
1133b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
1134b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1135fa9e4066Sahrens }
1136fa9e4066Sahrens 
1137b24ab676SJeff Bonwick static uint64_t
1138b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1139fa9e4066Sahrens {
1140b24ab676SJeff Bonwick 	uint64_t top;
1141b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
1142b24ab676SJeff Bonwick 	vdev_t *tvd;
1143fa9e4066Sahrens 
1144b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1145fa9e4066Sahrens 
1146b24ab676SJeff Bonwick 	do {
1147b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
1148b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
11495cabbc6bSPrashanth Sreenivasa 	} while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1150b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1151fa9e4066Sahrens 
1152b24ab676SJeff Bonwick 	return (top);
1153fa9e4066Sahrens }
1154fa9e4066Sahrens 
1155b24ab676SJeff Bonwick static uint64_t
1156b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1157fa9e4066Sahrens {
1158b24ab676SJeff Bonwick 	uint64_t value;
1159b24ab676SJeff Bonwick 
1160b24ab676SJeff Bonwick 	do {
1161b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1162b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1163b24ab676SJeff Bonwick 
1164b24ab676SJeff Bonwick 	return (value);
1165fa9e4066Sahrens }
1166fa9e4066Sahrens 
1167fa9e4066Sahrens static int
1168b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1169b24ab676SJeff Bonwick     boolean_t inherit)
1170fa9e4066Sahrens {
1171b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1172b24ab676SJeff Bonwick 	const char *valname;
1173b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1174b24ab676SJeff Bonwick 	uint64_t curval;
1175fa9e4066Sahrens 	int error;
1176fa9e4066Sahrens 
11773b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
11783b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1179fa9e4066Sahrens 
1180b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1181b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1182fa9e4066Sahrens 		return (error);
1183fa9e4066Sahrens 	}
1184fb09f5aaSMadhav Suresh 	ASSERT0(error);
1185fa9e4066Sahrens 
11863b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1187b24ab676SJeff Bonwick 
1188420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1189b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1190b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1191b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1192fa9e4066Sahrens 	}
1193fa9e4066Sahrens 
1194fa9e4066Sahrens 	return (error);
1195fa9e4066Sahrens }
1196fa9e4066Sahrens 
1197fa9e4066Sahrens static int
1198420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1199fa9e4066Sahrens {
1200420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1201b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1202fa9e4066Sahrens 	int error;
1203fa9e4066Sahrens 
1204b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1205b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1206fa9e4066Sahrens 
1207b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1208b24ab676SJeff Bonwick 
1209b24ab676SJeff Bonwick 	nvlist_free(props);
1210b24ab676SJeff Bonwick 
1211b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1212b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1213fa9e4066Sahrens 		return (error);
1214fa9e4066Sahrens 	}
1215fb09f5aaSMadhav Suresh 	ASSERT0(error);
1216fa9e4066Sahrens 
1217fa9e4066Sahrens 	return (error);
1218fa9e4066Sahrens }
1219fa9e4066Sahrens 
1220eb633035STom Caputi static int
1221eb633035STom Caputi ztest_dmu_objset_own(const char *name, dmu_objset_type_t type,
1222eb633035STom Caputi     boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
1223eb633035STom Caputi {
1224eb633035STom Caputi 	int err;
1225eb633035STom Caputi 
1226eb633035STom Caputi 	err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1227eb633035STom Caputi 	if (decrypt && err == EACCES) {
1228eb633035STom Caputi 		char ddname[ZFS_MAX_DATASET_NAME_LEN];
1229eb633035STom Caputi 		dsl_crypto_params_t *dcp;
1230eb633035STom Caputi 		nvlist_t *crypto_args = fnvlist_alloc();
1231eb633035STom Caputi 		char *cp = NULL;
1232eb633035STom Caputi 
1233eb633035STom Caputi 		/* spa_keystore_load_wkey() expects a dsl dir name */
1234eb633035STom Caputi 		(void) strcpy(ddname, name);
1235eb633035STom Caputi 		cp = strchr(ddname, '@');
1236eb633035STom Caputi 		if (cp != NULL)
1237eb633035STom Caputi 			*cp = '\0';
1238eb633035STom Caputi 
1239eb633035STom Caputi 		fnvlist_add_uint8_array(crypto_args, "wkeydata",
1240eb633035STom Caputi 		    (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
1241eb633035STom Caputi 		VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
1242eb633035STom Caputi 		    crypto_args, &dcp));
1243eb633035STom Caputi 		err = spa_keystore_load_wkey(ddname, dcp, B_FALSE);
1244eb633035STom Caputi 		dsl_crypto_params_free(dcp, B_FALSE);
1245eb633035STom Caputi 		fnvlist_free(crypto_args);
1246eb633035STom Caputi 
1247eb633035STom Caputi 		if (err != 0)
1248eb633035STom Caputi 			return (err);
1249eb633035STom Caputi 
1250eb633035STom Caputi 		err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1251eb633035STom Caputi 	}
1252eb633035STom Caputi 
1253eb633035STom Caputi 	return (err);
1254eb633035STom Caputi }
1255eb633035STom Caputi 
1256b24ab676SJeff Bonwick static void
1257b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1258b24ab676SJeff Bonwick {
1259b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1260b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1261f06dce2cSAndrew Stormont 	mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1262f06dce2cSAndrew Stormont 	cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1263b24ab676SJeff Bonwick }
1264fa9e4066Sahrens 
1265b24ab676SJeff Bonwick static void
1266b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1267fa9e4066Sahrens {
1268b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1269b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1270f06dce2cSAndrew Stormont 	mutex_destroy(&rll->rll_lock);
1271f06dce2cSAndrew Stormont 	cv_destroy(&rll->rll_cv);
1272b24ab676SJeff Bonwick }
1273fa9e4066Sahrens 
1274b24ab676SJeff Bonwick static void
1275b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1276b24ab676SJeff Bonwick {
1277f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1278fa9e4066Sahrens 
1279b24ab676SJeff Bonwick 	if (type == RL_READER) {
1280b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1281f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1282b24ab676SJeff Bonwick 		rll->rll_readers++;
1283b24ab676SJeff Bonwick 	} else {
1284b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1285f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1286b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1287b24ab676SJeff Bonwick 	}
1288fa9e4066Sahrens 
1289f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1290b24ab676SJeff Bonwick }
1291fa9e4066Sahrens 
1292b24ab676SJeff Bonwick static void
1293b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1294b24ab676SJeff Bonwick {
1295f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1296fa9e4066Sahrens 
1297b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1298b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1299b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1300b24ab676SJeff Bonwick 	} else {
1301b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1302b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1303b24ab676SJeff Bonwick 		rll->rll_readers--;
1304b24ab676SJeff Bonwick 	}
1305fa9e4066Sahrens 
1306b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1307f06dce2cSAndrew Stormont 		cv_broadcast(&rll->rll_cv);
1308b24ab676SJeff Bonwick 
1309f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1310fa9e4066Sahrens }
1311fa9e4066Sahrens 
1312b24ab676SJeff Bonwick static void
1313b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
131431157203SJeff Bonwick {
1315b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
131631157203SJeff Bonwick 
1317b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1318b24ab676SJeff Bonwick }
131931157203SJeff Bonwick 
1320b24ab676SJeff Bonwick static void
1321b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1322b24ab676SJeff Bonwick {
1323b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
132431157203SJeff Bonwick 
1325b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
132631157203SJeff Bonwick }
132731157203SJeff Bonwick 
1328b24ab676SJeff Bonwick static rl_t *
1329b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1330b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
133188ecc943SGeorge Wilson {
1332b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1333b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1334b24ab676SJeff Bonwick 	rl_t *rl;
133588ecc943SGeorge Wilson 
1336b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1337b24ab676SJeff Bonwick 	rl->rl_object = object;
1338b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1339b24ab676SJeff Bonwick 	rl->rl_size = size;
1340b24ab676SJeff Bonwick 	rl->rl_lock = rll;
134188ecc943SGeorge Wilson 
1342b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
134388ecc943SGeorge Wilson 
1344b24ab676SJeff Bonwick 	return (rl);
134588ecc943SGeorge Wilson }
134688ecc943SGeorge Wilson 
1347b24ab676SJeff Bonwick static void
1348b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1349fa9e4066Sahrens {
1350b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1351fa9e4066Sahrens 
1352b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1353fa9e4066Sahrens 
1354b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1355b24ab676SJeff Bonwick }
1356fa9e4066Sahrens 
1357b24ab676SJeff Bonwick static void
1358420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1359b24ab676SJeff Bonwick {
1360b24ab676SJeff Bonwick 	zd->zd_os = os;
1361b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1362420dfc95SChris Siden 	zd->zd_shared = szd;
1363b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1364fa9e4066Sahrens 
1365420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1366420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1367420dfc95SChris Siden 
1368f06dce2cSAndrew Stormont 	rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1369f06dce2cSAndrew Stormont 	mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1370b24ab676SJeff Bonwick 
1371b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1372b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1373b24ab676SJeff Bonwick 
1374b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1375b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1376b24ab676SJeff Bonwick }
1377b24ab676SJeff Bonwick 
1378b24ab676SJeff Bonwick static void
1379b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1380b24ab676SJeff Bonwick {
1381f06dce2cSAndrew Stormont 	mutex_destroy(&zd->zd_dirobj_lock);
1382b24ab676SJeff Bonwick 
1383b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1384b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1385b24ab676SJeff Bonwick 
1386b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1387b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1388b24ab676SJeff Bonwick }
1389b24ab676SJeff Bonwick 
1390b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1391b24ab676SJeff Bonwick 
1392b24ab676SJeff Bonwick static uint64_t
1393b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1394b24ab676SJeff Bonwick {
1395b24ab676SJeff Bonwick 	uint64_t txg;
1396b24ab676SJeff Bonwick 	int error;
1397b24ab676SJeff Bonwick 
1398b24ab676SJeff Bonwick 	/*
1399b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1400b24ab676SJeff Bonwick 	 */
1401b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1402b24ab676SJeff Bonwick 	if (error) {
1403b24ab676SJeff Bonwick 		if (error == ERESTART) {
1404b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1405b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1406b24ab676SJeff Bonwick 		} else {
1407b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1408b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1409b24ab676SJeff Bonwick 		}
1410b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1411b24ab676SJeff Bonwick 		return (0);
1412b24ab676SJeff Bonwick 	}
1413b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1414b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1415b24ab676SJeff Bonwick 	return (txg);
1416b24ab676SJeff Bonwick }
1417b24ab676SJeff Bonwick 
1418b24ab676SJeff Bonwick static void
1419b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1420b24ab676SJeff Bonwick {
1421b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1422b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1423b24ab676SJeff Bonwick 
1424b24ab676SJeff Bonwick 	while (ip < ip_end)
1425b24ab676SJeff Bonwick 		*ip++ = value;
1426b24ab676SJeff Bonwick }
1427b24ab676SJeff Bonwick 
1428b24ab676SJeff Bonwick static boolean_t
1429b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1430b24ab676SJeff Bonwick {
1431b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1432b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1433b24ab676SJeff Bonwick 	uint64_t diff = 0;
1434b24ab676SJeff Bonwick 
1435b24ab676SJeff Bonwick 	while (ip < ip_end)
1436b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1437b24ab676SJeff Bonwick 
1438b24ab676SJeff Bonwick 	return (diff == 0);
1439b24ab676SJeff Bonwick }
1440b24ab676SJeff Bonwick 
1441b24ab676SJeff Bonwick static void
1442b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
144354811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
144454811da5SToomas Soome     uint64_t crtxg)
1445b24ab676SJeff Bonwick {
1446b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1447b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1448b24ab676SJeff Bonwick 	bt->bt_object = object;
144954811da5SToomas Soome 	bt->bt_dnodesize = dnodesize;
1450b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1451b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1452b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1453b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1454b24ab676SJeff Bonwick }
1455b24ab676SJeff Bonwick 
1456b24ab676SJeff Bonwick static void
1457b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
145854811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
145954811da5SToomas Soome     uint64_t crtxg)
1460b24ab676SJeff Bonwick {
14615d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
14625d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
14635d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
146454811da5SToomas Soome 	ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
14655d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
14665d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
14675d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
14685d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1469b24ab676SJeff Bonwick }
1470b24ab676SJeff Bonwick 
1471b24ab676SJeff Bonwick static ztest_block_tag_t *
1472b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1473b24ab676SJeff Bonwick {
1474b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1475b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1476b24ab676SJeff Bonwick 
1477b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1478b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1479b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1480b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1481b24ab676SJeff Bonwick 
1482b24ab676SJeff Bonwick 	return (bt);
1483b24ab676SJeff Bonwick }
1484b24ab676SJeff Bonwick 
148554811da5SToomas Soome /*
148654811da5SToomas Soome  * Generate a token to fill up unused bonus buffer space.  Try to make
148754811da5SToomas Soome  * it unique to the object, generation, and offset to verify that data
148854811da5SToomas Soome  * is not getting overwritten by data from other dnodes.
148954811da5SToomas Soome  */
149054811da5SToomas Soome #define	ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset)	\
149154811da5SToomas Soome 	(((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
149254811da5SToomas Soome 
149354811da5SToomas Soome /*
149454811da5SToomas Soome  * Fill up the unused bonus buffer region before the block tag with a
149554811da5SToomas Soome  * verifiable pattern. Filling the whole bonus area with non-zero data
149654811da5SToomas Soome  * helps ensure that all dnode traversal code properly skips the
149754811da5SToomas Soome  * interior regions of large dnodes.
149854811da5SToomas Soome  */
149954811da5SToomas Soome void
150054811da5SToomas Soome ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
150154811da5SToomas Soome     objset_t *os, uint64_t gen)
150254811da5SToomas Soome {
150354811da5SToomas Soome 	uint64_t *bonusp;
150454811da5SToomas Soome 
150554811da5SToomas Soome 	ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
150654811da5SToomas Soome 
150754811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
150854811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
150954811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
151054811da5SToomas Soome 		*bonusp = token;
151154811da5SToomas Soome 	}
151254811da5SToomas Soome }
151354811da5SToomas Soome 
151454811da5SToomas Soome /*
151554811da5SToomas Soome  * Verify that the unused area of a bonus buffer is filled with the
151654811da5SToomas Soome  * expected tokens.
151754811da5SToomas Soome  */
151854811da5SToomas Soome void
151954811da5SToomas Soome ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
152054811da5SToomas Soome     objset_t *os, uint64_t gen)
152154811da5SToomas Soome {
152254811da5SToomas Soome 	uint64_t *bonusp;
152354811da5SToomas Soome 
152454811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
152554811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
152654811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
152754811da5SToomas Soome 		VERIFY3U(*bonusp, ==, token);
152854811da5SToomas Soome 	}
152954811da5SToomas Soome }
153054811da5SToomas Soome 
1531b24ab676SJeff Bonwick /*
1532b24ab676SJeff Bonwick  * ZIL logging ops
1533b24ab676SJeff Bonwick  */
1534b24ab676SJeff Bonwick 
1535b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1536b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1537b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1538b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
153954811da5SToomas Soome #define	lrz_dnodesize	lr_crtime[1]
1540b24ab676SJeff Bonwick 
15415002558fSNeil Perrin static void
1542b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1543b24ab676SJeff Bonwick {
1544b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1545b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1546b24ab676SJeff Bonwick 	itx_t *itx;
1547b24ab676SJeff Bonwick 
1548b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15495002558fSNeil Perrin 		return;
1550b24ab676SJeff Bonwick 
1551b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1552b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1553b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1554b24ab676SJeff Bonwick 
15555002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1556b24ab676SJeff Bonwick }
1557b24ab676SJeff Bonwick 
15585002558fSNeil Perrin static void
15595002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1560b24ab676SJeff Bonwick {
1561b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1562b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1563b24ab676SJeff Bonwick 	itx_t *itx;
1564b24ab676SJeff Bonwick 
1565b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15665002558fSNeil Perrin 		return;
1567b24ab676SJeff Bonwick 
1568b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1569b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1570b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1571b24ab676SJeff Bonwick 
157251bd2f97SNeil Perrin 	itx->itx_oid = object;
15735002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1574b24ab676SJeff Bonwick }
1575b24ab676SJeff Bonwick 
15765002558fSNeil Perrin static void
1577b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1578b24ab676SJeff Bonwick {
1579b24ab676SJeff Bonwick 	itx_t *itx;
1580b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1581b24ab676SJeff Bonwick 
1582b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15835002558fSNeil Perrin 		return;
1584b24ab676SJeff Bonwick 
1585b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1586b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1587b24ab676SJeff Bonwick 
1588b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1589b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1590b24ab676SJeff Bonwick 
1591b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1592b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1593b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1594b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1595b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1596b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1597b24ab676SJeff Bonwick 	}
1598b24ab676SJeff Bonwick 	itx->itx_private = zd;
1599b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1600b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1601b24ab676SJeff Bonwick 
1602b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1603b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1604b24ab676SJeff Bonwick 
16055002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1606b24ab676SJeff Bonwick }
1607b24ab676SJeff Bonwick 
16085002558fSNeil Perrin static void
1609b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1610b24ab676SJeff Bonwick {
1611b24ab676SJeff Bonwick 	itx_t *itx;
1612b24ab676SJeff Bonwick 
1613b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
16145002558fSNeil Perrin 		return;
1615b24ab676SJeff Bonwick 
1616b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1617b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1618b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1619b24ab676SJeff Bonwick 
16205002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
16215002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1622b24ab676SJeff Bonwick }
1623b24ab676SJeff Bonwick 
16245002558fSNeil Perrin static void
1625b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1626b24ab676SJeff Bonwick {
1627b24ab676SJeff Bonwick 	itx_t *itx;
1628b24ab676SJeff Bonwick 
1629b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
16305002558fSNeil Perrin 		return;
1631b24ab676SJeff Bonwick 
1632b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1633b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1634b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1635b24ab676SJeff Bonwick 
16365002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
16375002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1638b24ab676SJeff Bonwick }
1639b24ab676SJeff Bonwick 
1640b24ab676SJeff Bonwick /*
1641b24ab676SJeff Bonwick  * ZIL replay ops
1642b24ab676SJeff Bonwick  */
1643b24ab676SJeff Bonwick static int
16443f7978d0SAlan Somers ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1645b24ab676SJeff Bonwick {
16463f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
16473f7978d0SAlan Somers 	lr_create_t *lr = arg2;
1648b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1649b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1650b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1651b24ab676SJeff Bonwick 	dmu_buf_t *db;
1652b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1653b24ab676SJeff Bonwick 	uint64_t txg;
1654b24ab676SJeff Bonwick 	int error = 0;
165554811da5SToomas Soome 	int bonuslen;
1656b24ab676SJeff Bonwick 
1657b24ab676SJeff Bonwick 	if (byteswap)
1658b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1659b24ab676SJeff Bonwick 
1660b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1661b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1662b24ab676SJeff Bonwick 
1663b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1664b24ab676SJeff Bonwick 
1665b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1666b24ab676SJeff Bonwick 
1667b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1668b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1669b24ab676SJeff Bonwick 	} else {
1670b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1671b24ab676SJeff Bonwick 	}
1672b24ab676SJeff Bonwick 
1673b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1674b24ab676SJeff Bonwick 	if (txg == 0)
1675b24ab676SJeff Bonwick 		return (ENOSPC);
1676b24ab676SJeff Bonwick 
1677b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
167854811da5SToomas Soome 	bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1679b24ab676SJeff Bonwick 
1680b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1681b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
168254811da5SToomas Soome 			lr->lr_foid = zap_create_dnsize(os,
1683b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
168454811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1685b24ab676SJeff Bonwick 		} else {
168654811da5SToomas Soome 			error = zap_create_claim_dnsize(os, lr->lr_foid,
1687b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
168854811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1689b24ab676SJeff Bonwick 		}
1690b24ab676SJeff Bonwick 	} else {
1691b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
169254811da5SToomas Soome 			lr->lr_foid = dmu_object_alloc_dnsize(os,
1693b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
169454811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1695b24ab676SJeff Bonwick 		} else {
169654811da5SToomas Soome 			error = dmu_object_claim_dnsize(os, lr->lr_foid,
1697b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
169854811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1699b24ab676SJeff Bonwick 		}
1700b24ab676SJeff Bonwick 	}
1701b24ab676SJeff Bonwick 
1702b24ab676SJeff Bonwick 	if (error) {
1703b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1704b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1705b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1706b24ab676SJeff Bonwick 		return (error);
1707b24ab676SJeff Bonwick 	}
1708b24ab676SJeff Bonwick 
1709b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1710b24ab676SJeff Bonwick 
1711b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1712b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1713b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1714b24ab676SJeff Bonwick 
1715b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1716b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1717b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
171854811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
171954811da5SToomas Soome 	    lr->lr_gen, txg, txg);
172054811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1721b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1722b24ab676SJeff Bonwick 
1723b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1724b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1725b24ab676SJeff Bonwick 
1726b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1727b24ab676SJeff Bonwick 
1728b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1729b24ab676SJeff Bonwick 
1730b24ab676SJeff Bonwick 	return (0);
1731b24ab676SJeff Bonwick }
1732b24ab676SJeff Bonwick 
1733b24ab676SJeff Bonwick static int
17343f7978d0SAlan Somers ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1735b24ab676SJeff Bonwick {
17363f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17373f7978d0SAlan Somers 	lr_remove_t *lr = arg2;
1738b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1739b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1740b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1741b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1742b24ab676SJeff Bonwick 	uint64_t object, txg;
1743b24ab676SJeff Bonwick 
1744b24ab676SJeff Bonwick 	if (byteswap)
1745b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1746b24ab676SJeff Bonwick 
1747b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1748b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1749b24ab676SJeff Bonwick 
1750b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1751b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1752b24ab676SJeff Bonwick 	ASSERT(object != 0);
1753b24ab676SJeff Bonwick 
1754b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1755b24ab676SJeff Bonwick 
1756b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1757b24ab676SJeff Bonwick 
1758b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1759b24ab676SJeff Bonwick 
1760b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1761b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1762b24ab676SJeff Bonwick 
1763b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1764b24ab676SJeff Bonwick 	if (txg == 0) {
1765b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1766b24ab676SJeff Bonwick 		return (ENOSPC);
1767b24ab676SJeff Bonwick 	}
1768b24ab676SJeff Bonwick 
1769b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1770b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1771b24ab676SJeff Bonwick 	} else {
1772b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1773b24ab676SJeff Bonwick 	}
1774b24ab676SJeff Bonwick 
1775b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1776b24ab676SJeff Bonwick 
17775002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1778b24ab676SJeff Bonwick 
1779b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1780b24ab676SJeff Bonwick 
1781b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1782b24ab676SJeff Bonwick 
1783b24ab676SJeff Bonwick 	return (0);
1784b24ab676SJeff Bonwick }
1785b24ab676SJeff Bonwick 
1786b24ab676SJeff Bonwick static int
17873f7978d0SAlan Somers ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1788b24ab676SJeff Bonwick {
17893f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17903f7978d0SAlan Somers 	lr_write_t *lr = arg2;
1791b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1792b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1793b24ab676SJeff Bonwick 	uint64_t offset, length;
1794b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1795b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1796b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1797b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1798b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1799b24ab676SJeff Bonwick 	dmu_buf_t *db;
1800b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1801b24ab676SJeff Bonwick 	rl_t *rl;
1802b24ab676SJeff Bonwick 
1803b24ab676SJeff Bonwick 	if (byteswap)
1804b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1805b24ab676SJeff Bonwick 
1806b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1807b24ab676SJeff Bonwick 	length = lr->lr_length;
1808b24ab676SJeff Bonwick 
1809b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1810b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1811b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1812b24ab676SJeff Bonwick 		if (length < blocksize) {
1813b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1814b24ab676SJeff Bonwick 			length = blocksize;
1815b24ab676SJeff Bonwick 		}
1816b24ab676SJeff Bonwick 	}
1817b24ab676SJeff Bonwick 
1818b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1819b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1820b24ab676SJeff Bonwick 
1821b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1822b24ab676SJeff Bonwick 		bt = NULL;
1823b24ab676SJeff Bonwick 
1824b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1825b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1826b24ab676SJeff Bonwick 
1827b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1828b24ab676SJeff Bonwick 
1829b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1830b24ab676SJeff Bonwick 
1831b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1832b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1833b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1834b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1835b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1836b24ab676SJeff Bonwick 
1837b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1838b24ab676SJeff Bonwick 
1839b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1840b24ab676SJeff Bonwick 
1841b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1842b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1843b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1844b24ab676SJeff Bonwick 
1845b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1846b24ab676SJeff Bonwick 	if (txg == 0) {
1847b24ab676SJeff Bonwick 		if (abuf != NULL)
1848b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1849b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1850b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1851b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1852b24ab676SJeff Bonwick 		return (ENOSPC);
1853b24ab676SJeff Bonwick 	}
1854b24ab676SJeff Bonwick 
1855b24ab676SJeff Bonwick 	if (bt != NULL) {
1856b24ab676SJeff Bonwick 		/*
1857b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1858b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1859b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1860b24ab676SJeff Bonwick 		 */
1861b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1862b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1863b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1864b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1865b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1866b24ab676SJeff Bonwick 
1867b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1868b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1869b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
187054811da5SToomas Soome 				ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1871b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1872b24ab676SJeff Bonwick 			}
1873b24ab676SJeff Bonwick 		}
1874b24ab676SJeff Bonwick 
1875b24ab676SJeff Bonwick 		/*
1876b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1877b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1878b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1879b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1880b24ab676SJeff Bonwick 		 */
1881b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
188254811da5SToomas Soome 			ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1883b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1884b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1885b24ab676SJeff Bonwick 		}
1886b24ab676SJeff Bonwick 
1887b24ab676SJeff Bonwick 		/*
1888b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1889b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1890b24ab676SJeff Bonwick 		 */
189154811da5SToomas Soome 		ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
189254811da5SToomas Soome 		    crtxg);
1893b24ab676SJeff Bonwick 	}
1894b24ab676SJeff Bonwick 
1895b24ab676SJeff Bonwick 	if (abuf == NULL) {
1896b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1897b24ab676SJeff Bonwick 	} else {
1898b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1899eb633035STom Caputi 		dmu_assign_arcbuf_by_dbuf(db, offset, abuf, tx);
1900b24ab676SJeff Bonwick 	}
1901b24ab676SJeff Bonwick 
1902b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1903b24ab676SJeff Bonwick 
1904b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1905b24ab676SJeff Bonwick 
1906b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1907b24ab676SJeff Bonwick 
1908b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1909b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1910b24ab676SJeff Bonwick 
1911b24ab676SJeff Bonwick 	return (0);
1912b24ab676SJeff Bonwick }
1913b24ab676SJeff Bonwick 
1914b24ab676SJeff Bonwick static int
19153f7978d0SAlan Somers ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1916b24ab676SJeff Bonwick {
19173f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
19183f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
1919b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1920b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1921b24ab676SJeff Bonwick 	uint64_t txg;
1922b24ab676SJeff Bonwick 	rl_t *rl;
1923b24ab676SJeff Bonwick 
1924b24ab676SJeff Bonwick 	if (byteswap)
1925b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1926b24ab676SJeff Bonwick 
1927b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1928b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1929b24ab676SJeff Bonwick 	    RL_WRITER);
1930b24ab676SJeff Bonwick 
1931b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1932b24ab676SJeff Bonwick 
1933b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1934b24ab676SJeff Bonwick 
1935b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1936b24ab676SJeff Bonwick 	if (txg == 0) {
1937b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1938b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1939b24ab676SJeff Bonwick 		return (ENOSPC);
1940b24ab676SJeff Bonwick 	}
1941b24ab676SJeff Bonwick 
1942b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1943b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1944b24ab676SJeff Bonwick 
1945b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1946b24ab676SJeff Bonwick 
1947b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1948b24ab676SJeff Bonwick 
1949b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1950b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1951b24ab676SJeff Bonwick 
1952b24ab676SJeff Bonwick 	return (0);
1953b24ab676SJeff Bonwick }
1954b24ab676SJeff Bonwick 
1955b24ab676SJeff Bonwick static int
19563f7978d0SAlan Somers ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1957b24ab676SJeff Bonwick {
19583f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
19593f7978d0SAlan Somers 	lr_setattr_t *lr = arg2;
1960b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1961b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1962b24ab676SJeff Bonwick 	dmu_buf_t *db;
1963b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
196454811da5SToomas Soome 	uint64_t txg, lrtxg, crtxg, dnodesize;
1965b24ab676SJeff Bonwick 
1966b24ab676SJeff Bonwick 	if (byteswap)
1967b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1968b24ab676SJeff Bonwick 
1969b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1970b24ab676SJeff Bonwick 
1971b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1972b24ab676SJeff Bonwick 
1973b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1974b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1975b24ab676SJeff Bonwick 
1976b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1977b24ab676SJeff Bonwick 	if (txg == 0) {
1978b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1979b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1980b24ab676SJeff Bonwick 		return (ENOSPC);
1981b24ab676SJeff Bonwick 	}
1982b24ab676SJeff Bonwick 
1983b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1984b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1985b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1986b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
198754811da5SToomas Soome 	dnodesize = bbt->bt_dnodesize;
1988b24ab676SJeff Bonwick 
1989b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1990b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1991b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1992b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1993b24ab676SJeff Bonwick 	} else {
1994b24ab676SJeff Bonwick 		/*
1995b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1996b24ab676SJeff Bonwick 		 */
1997b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1998b24ab676SJeff Bonwick 		    sizeof (*bbt);
1999b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
2000b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
2001b24ab676SJeff Bonwick 	}
2002b24ab676SJeff Bonwick 
2003b24ab676SJeff Bonwick 	/*
2004b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
2005b24ab676SJeff Bonwick 	 */
200654811da5SToomas Soome 	ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2007b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
2008b24ab676SJeff Bonwick 
2009b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
2010b24ab676SJeff Bonwick 
2011b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2012b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
2013fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2014b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
2015b24ab676SJeff Bonwick 
201654811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
201754811da5SToomas Soome 	    txg, crtxg);
201854811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2019b24ab676SJeff Bonwick 
2020b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
2021b24ab676SJeff Bonwick 
2022b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
2023b24ab676SJeff Bonwick 
2024b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
2025b24ab676SJeff Bonwick 
2026b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
2027b24ab676SJeff Bonwick 
2028b24ab676SJeff Bonwick 	return (0);
2029b24ab676SJeff Bonwick }
2030b24ab676SJeff Bonwick 
2031b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
2032b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
2033b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
2034b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
2035b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
2036b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
2037b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
2038b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
2039b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
2040b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
2041b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
2042b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
2043b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
2044b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
2045b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
2046b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
2047b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
2048b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
2049b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
2050b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
2051b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
2052b24ab676SJeff Bonwick };
2053b24ab676SJeff Bonwick 
2054b24ab676SJeff Bonwick /*
2055b24ab676SJeff Bonwick  * ZIL get_data callbacks
2056b24ab676SJeff Bonwick  */
2057b24ab676SJeff Bonwick 
2058cab3a55eSPrakash Surya /* ARGSUSED */
2059b24ab676SJeff Bonwick static void
2060b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
2061b24ab676SJeff Bonwick {
2062b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
206379315247SMatthew Ahrens 	uint64_t object = ((rl_t *)zgd->zgd_lr)->rl_object;
2064b24ab676SJeff Bonwick 
2065b24ab676SJeff Bonwick 	if (zgd->zgd_db)
2066b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
2067b24ab676SJeff Bonwick 
206879315247SMatthew Ahrens 	ztest_range_unlock((rl_t *)zgd->zgd_lr);
2069b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2070b24ab676SJeff Bonwick 
2071b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
2072b24ab676SJeff Bonwick }
2073b24ab676SJeff Bonwick 
2074b24ab676SJeff Bonwick static int
20751271e4b1SPrakash Surya ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
20761271e4b1SPrakash Surya     zio_t *zio)
2077b24ab676SJeff Bonwick {
2078b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
2079b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2080b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
2081b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
2082b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
2083b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
2084b24ab676SJeff Bonwick 	uint64_t crtxg;
2085b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2086b24ab676SJeff Bonwick 	dmu_buf_t *db;
2087b24ab676SJeff Bonwick 	zgd_t *zgd;
2088b24ab676SJeff Bonwick 	int error;
2089b24ab676SJeff Bonwick 
20901271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
20911271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
20921271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
20931271e4b1SPrakash Surya 
2094b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2095b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
2096b24ab676SJeff Bonwick 	if (error) {
2097b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2098b24ab676SJeff Bonwick 		return (error);
2099b24ab676SJeff Bonwick 	}
2100b24ab676SJeff Bonwick 
2101b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
2102b24ab676SJeff Bonwick 
2103b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
2104b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
2105b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2106b24ab676SJeff Bonwick 		return (ENOENT);
2107b24ab676SJeff Bonwick 	}
2108b24ab676SJeff Bonwick 
2109b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
2110b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
2111b24ab676SJeff Bonwick 	db = NULL;
2112b24ab676SJeff Bonwick 
2113b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
21141271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
2115b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
2116b24ab676SJeff Bonwick 
2117b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
211879315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
211979315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2120b24ab676SJeff Bonwick 
2121b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
2122b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2123b24ab676SJeff Bonwick 		ASSERT(error == 0);
2124b24ab676SJeff Bonwick 	} else {
2125b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
2126dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
2127b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
2128dfe73b3dSJeff Bonwick 		} else {
2129dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
2130dfe73b3dSJeff Bonwick 			offset = 0;
2131dfe73b3dSJeff Bonwick 		}
2132b24ab676SJeff Bonwick 
213379315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
213479315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2135b24ab676SJeff Bonwick 
213647cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
213747cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2138b24ab676SJeff Bonwick 
2139b24ab676SJeff Bonwick 		if (error == 0) {
2140b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
214180901aeaSGeorge Wilson 
2142b24ab676SJeff Bonwick 			zgd->zgd_db = db;
2143b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
2144b24ab676SJeff Bonwick 
2145b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
2146b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
2147b24ab676SJeff Bonwick 
2148b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
2149b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
2150b24ab676SJeff Bonwick 
2151b24ab676SJeff Bonwick 			if (error == 0)
2152b24ab676SJeff Bonwick 				return (0);
2153b24ab676SJeff Bonwick 		}
2154b24ab676SJeff Bonwick 	}
2155b24ab676SJeff Bonwick 
2156b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
2157b24ab676SJeff Bonwick 
2158b24ab676SJeff Bonwick 	return (error);
2159b24ab676SJeff Bonwick }
2160b24ab676SJeff Bonwick 
2161b24ab676SJeff Bonwick static void *
2162b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
2163b24ab676SJeff Bonwick {
2164b24ab676SJeff Bonwick 	char *lr;
2165b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2166b24ab676SJeff Bonwick 
2167b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2168b24ab676SJeff Bonwick 
2169b24ab676SJeff Bonwick 	if (name)
2170b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
2171b24ab676SJeff Bonwick 
2172b24ab676SJeff Bonwick 	return (lr);
2173b24ab676SJeff Bonwick }
2174b24ab676SJeff Bonwick 
2175b24ab676SJeff Bonwick void
2176b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
2177b24ab676SJeff Bonwick {
2178b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2179b24ab676SJeff Bonwick 
2180b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
2181b24ab676SJeff Bonwick }
2182b24ab676SJeff Bonwick 
2183b24ab676SJeff Bonwick /*
2184b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
2185b24ab676SJeff Bonwick  */
2186b24ab676SJeff Bonwick static int
2187b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2188b24ab676SJeff Bonwick {
2189b24ab676SJeff Bonwick 	int missing = 0;
2190b24ab676SJeff Bonwick 	int error;
2191b24ab676SJeff Bonwick 
2192f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2193b24ab676SJeff Bonwick 
2194b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2195b24ab676SJeff Bonwick 		od->od_object = 0;
2196b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2197b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
2198b24ab676SJeff Bonwick 		if (error) {
2199b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
2200b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
2201b24ab676SJeff Bonwick 			missing++;
2202b24ab676SJeff Bonwick 		} else {
2203b24ab676SJeff Bonwick 			dmu_buf_t *db;
2204b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
2205b24ab676SJeff Bonwick 			dmu_object_info_t doi;
2206b24ab676SJeff Bonwick 
2207b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2208b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
2209b24ab676SJeff Bonwick 
2210b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
2211b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2212b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
2213b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
2214b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
2215b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2216b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
2217b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
2218b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
2219b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
2220b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
2221b24ab676SJeff Bonwick 		}
2222b24ab676SJeff Bonwick 	}
2223b24ab676SJeff Bonwick 
2224b24ab676SJeff Bonwick 	return (missing);
2225b24ab676SJeff Bonwick }
2226b24ab676SJeff Bonwick 
2227b24ab676SJeff Bonwick static int
2228b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2229b24ab676SJeff Bonwick {
2230b24ab676SJeff Bonwick 	int missing = 0;
2231b24ab676SJeff Bonwick 
2232f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2233b24ab676SJeff Bonwick 
2234b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2235b24ab676SJeff Bonwick 		if (missing) {
2236b24ab676SJeff Bonwick 			od->od_object = 0;
2237b24ab676SJeff Bonwick 			missing++;
2238b24ab676SJeff Bonwick 			continue;
2239b24ab676SJeff Bonwick 		}
2240b24ab676SJeff Bonwick 
2241b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2242b24ab676SJeff Bonwick 
2243b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2244b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
2245b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
2246b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
2247b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
2248b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
224954811da5SToomas Soome 		lr->lrz_dnodesize = od->od_crdnodesize;
2250b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
2251b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
2252b24ab676SJeff Bonwick 
2253b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2254b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2255b24ab676SJeff Bonwick 			od->od_object = 0;
2256b24ab676SJeff Bonwick 			missing++;
2257b24ab676SJeff Bonwick 		} else {
2258b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2259b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2260b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2261b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2262b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2263b24ab676SJeff Bonwick 		}
2264b24ab676SJeff Bonwick 
2265b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2266b24ab676SJeff Bonwick 	}
2267b24ab676SJeff Bonwick 
2268b24ab676SJeff Bonwick 	return (missing);
2269b24ab676SJeff Bonwick }
2270b24ab676SJeff Bonwick 
2271b24ab676SJeff Bonwick static int
2272b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2273b24ab676SJeff Bonwick {
2274b24ab676SJeff Bonwick 	int missing = 0;
2275b24ab676SJeff Bonwick 	int error;
2276b24ab676SJeff Bonwick 
2277f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2278b24ab676SJeff Bonwick 
2279b24ab676SJeff Bonwick 	od += count - 1;
2280b24ab676SJeff Bonwick 
2281b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2282b24ab676SJeff Bonwick 		if (missing) {
2283b24ab676SJeff Bonwick 			missing++;
2284b24ab676SJeff Bonwick 			continue;
2285b24ab676SJeff Bonwick 		}
2286b24ab676SJeff Bonwick 
228780901aeaSGeorge Wilson 		/*
228880901aeaSGeorge Wilson 		 * No object was found.
228980901aeaSGeorge Wilson 		 */
2290b24ab676SJeff Bonwick 		if (od->od_object == 0)
2291b24ab676SJeff Bonwick 			continue;
2292b24ab676SJeff Bonwick 
2293b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2294b24ab676SJeff Bonwick 
2295b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2296b24ab676SJeff Bonwick 
2297b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2298b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2299b24ab676SJeff Bonwick 			missing++;
2300b24ab676SJeff Bonwick 		} else {
2301b24ab676SJeff Bonwick 			od->od_object = 0;
2302b24ab676SJeff Bonwick 		}
2303b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2304b24ab676SJeff Bonwick 	}
2305b24ab676SJeff Bonwick 
2306b24ab676SJeff Bonwick 	return (missing);
2307b24ab676SJeff Bonwick }
2308b24ab676SJeff Bonwick 
2309b24ab676SJeff Bonwick static int
2310b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2311b24ab676SJeff Bonwick     void *data)
2312b24ab676SJeff Bonwick {
2313b24ab676SJeff Bonwick 	lr_write_t *lr;
2314b24ab676SJeff Bonwick 	int error;
2315b24ab676SJeff Bonwick 
2316b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2317b24ab676SJeff Bonwick 
2318b24ab676SJeff Bonwick 	lr->lr_foid = object;
2319b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2320b24ab676SJeff Bonwick 	lr->lr_length = size;
2321b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2322b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2323b24ab676SJeff Bonwick 
2324b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2325b24ab676SJeff Bonwick 
2326b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2327b24ab676SJeff Bonwick 
2328b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2329b24ab676SJeff Bonwick 
2330b24ab676SJeff Bonwick 	return (error);
2331b24ab676SJeff Bonwick }
2332b24ab676SJeff Bonwick 
2333b24ab676SJeff Bonwick static int
2334b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2335b24ab676SJeff Bonwick {
2336b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2337b24ab676SJeff Bonwick 	int error;
2338b24ab676SJeff Bonwick 
2339b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2340b24ab676SJeff Bonwick 
2341b24ab676SJeff Bonwick 	lr->lr_foid = object;
2342b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2343b24ab676SJeff Bonwick 	lr->lr_length = size;
2344b24ab676SJeff Bonwick 
2345b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2346b24ab676SJeff Bonwick 
2347b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2348b24ab676SJeff Bonwick 
2349b24ab676SJeff Bonwick 	return (error);
2350b24ab676SJeff Bonwick }
2351b24ab676SJeff Bonwick 
2352b24ab676SJeff Bonwick static int
2353b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2354b24ab676SJeff Bonwick {
2355b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2356b24ab676SJeff Bonwick 	int error;
2357b24ab676SJeff Bonwick 
2358b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2359b24ab676SJeff Bonwick 
2360b24ab676SJeff Bonwick 	lr->lr_foid = object;
2361b24ab676SJeff Bonwick 	lr->lr_size = 0;
2362b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2363b24ab676SJeff Bonwick 
2364b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2365b24ab676SJeff Bonwick 
2366b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2367b24ab676SJeff Bonwick 
2368b24ab676SJeff Bonwick 	return (error);
2369b24ab676SJeff Bonwick }
2370b24ab676SJeff Bonwick 
2371b24ab676SJeff Bonwick static void
2372b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2373b24ab676SJeff Bonwick {
2374b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2375b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2376b24ab676SJeff Bonwick 	uint64_t txg;
2377b24ab676SJeff Bonwick 	rl_t *rl;
2378b24ab676SJeff Bonwick 
2379b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2380b24ab676SJeff Bonwick 
2381b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2382b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2383b24ab676SJeff Bonwick 
2384b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2385b24ab676SJeff Bonwick 
2386b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2387b24ab676SJeff Bonwick 
2388b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2389b24ab676SJeff Bonwick 
2390b24ab676SJeff Bonwick 	if (txg != 0) {
2391b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2392b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2393b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2394b24ab676SJeff Bonwick 	} else {
2395b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2396b24ab676SJeff Bonwick 	}
2397b24ab676SJeff Bonwick 
2398b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2399b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2400b24ab676SJeff Bonwick }
2401b24ab676SJeff Bonwick 
2402b24ab676SJeff Bonwick static void
2403b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2404b24ab676SJeff Bonwick {
240580901aeaSGeorge Wilson 	int err;
2406b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2407b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2408b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2409b24ab676SJeff Bonwick 	uint64_t blocksize;
2410b24ab676SJeff Bonwick 	void *data;
2411b24ab676SJeff Bonwick 
2412b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2413b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2414b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2415b24ab676SJeff Bonwick 
2416b24ab676SJeff Bonwick 	/*
2417b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2418b24ab676SJeff Bonwick 	 */
2419b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2420b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2421b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2422b24ab676SJeff Bonwick 
2423f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2424c9ba2a43SEric Schrock 
2425b24ab676SJeff Bonwick 	switch (io_type) {
2426b24ab676SJeff Bonwick 
2427b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
242854811da5SToomas Soome 		ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
242954811da5SToomas Soome 		    offset, 0, 0, 0);
2430b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2431b24ab676SJeff Bonwick 		break;
2432b24ab676SJeff Bonwick 
2433b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2434b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2435b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2436b24ab676SJeff Bonwick 			/*
2437b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2438b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2439b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2440b24ab676SJeff Bonwick 			 */
2441b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2442b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2443b24ab676SJeff Bonwick 		}
2444b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2445b24ab676SJeff Bonwick 		break;
2446b24ab676SJeff Bonwick 
2447b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2448b24ab676SJeff Bonwick 		bzero(data, blocksize);
2449b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2450b24ab676SJeff Bonwick 		break;
2451b24ab676SJeff Bonwick 
2452b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2453b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2454b24ab676SJeff Bonwick 		break;
2455b24ab676SJeff Bonwick 
2456b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2457b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2458b24ab676SJeff Bonwick 		break;
245980901aeaSGeorge Wilson 
246080901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
2461f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_READER);
246280901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
246380901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
246480901aeaSGeorge Wilson 		    B_FALSE);
246580901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
246680901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
246780901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
246880901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
246980901aeaSGeorge Wilson 		    B_FALSE);
247080901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
2471f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
247280901aeaSGeorge Wilson 
247380901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
247480901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
247580901aeaSGeorge Wilson 
247680901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
247780901aeaSGeorge Wilson 		break;
2478b24ab676SJeff Bonwick 	}
2479b24ab676SJeff Bonwick 
2480f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2481c9ba2a43SEric Schrock 
2482b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2483b24ab676SJeff Bonwick }
2484b24ab676SJeff Bonwick 
2485b24ab676SJeff Bonwick /*
2486b24ab676SJeff Bonwick  * Initialize an object description template.
2487b24ab676SJeff Bonwick  */
2488b24ab676SJeff Bonwick static void
2489b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
249054811da5SToomas Soome     dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
249154811da5SToomas Soome     uint64_t gen)
2492b24ab676SJeff Bonwick {
2493b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2494b24ab676SJeff Bonwick 	od->od_object = 0;
2495b24ab676SJeff Bonwick 
2496b24ab676SJeff Bonwick 	od->od_crtype = type;
2497b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
249854811da5SToomas Soome 	od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2499b24ab676SJeff Bonwick 	od->od_crgen = gen;
2500b24ab676SJeff Bonwick 
2501b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2502b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2503b24ab676SJeff Bonwick 	od->od_gen = 0;
2504b24ab676SJeff Bonwick 
2505b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2506b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2507b24ab676SJeff Bonwick }
2508b24ab676SJeff Bonwick 
2509b24ab676SJeff Bonwick /*
2510b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2511b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2512b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2513b24ab676SJeff Bonwick  * use the existing objects.
2514b24ab676SJeff Bonwick  */
2515b24ab676SJeff Bonwick static int
2516b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2517b24ab676SJeff Bonwick {
2518b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2519b24ab676SJeff Bonwick 	int rv = 0;
2520b24ab676SJeff Bonwick 
2521f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2522b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2523b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2524b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2525b24ab676SJeff Bonwick 		rv = -1;
2526b24ab676SJeff Bonwick 	zd->zd_od = od;
2527f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2528b24ab676SJeff Bonwick 
2529b24ab676SJeff Bonwick 	return (rv);
2530b24ab676SJeff Bonwick }
2531b24ab676SJeff Bonwick 
2532b24ab676SJeff Bonwick /* ARGSUSED */
2533b24ab676SJeff Bonwick void
2534b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2535b24ab676SJeff Bonwick {
2536b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2537b24ab676SJeff Bonwick 
2538f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2539c9ba2a43SEric Schrock 
25405002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2541b24ab676SJeff Bonwick 
2542b24ab676SJeff Bonwick 	/*
2543b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2544b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2545b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2546b24ab676SJeff Bonwick 	 */
2547b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2548420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2549420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2550420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2551b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2552c9ba2a43SEric Schrock 
2553f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2554c9ba2a43SEric Schrock }
2555c9ba2a43SEric Schrock 
2556c9ba2a43SEric Schrock /*
2557c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2558c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2559c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2560c9ba2a43SEric Schrock  */
2561c9ba2a43SEric Schrock /* ARGSUSED */
2562c9ba2a43SEric Schrock void
2563c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2564c9ba2a43SEric Schrock {
2565c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2566c9ba2a43SEric Schrock 
256780901aeaSGeorge Wilson 	/*
256880901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
256980901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
257080901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
257180901aeaSGeorge Wilson 	 */
2572f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2573f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2574c9ba2a43SEric Schrock 
2575c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2576c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2577c9ba2a43SEric Schrock 
2578c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2579c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2580c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2581c9ba2a43SEric Schrock 
2582f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2583f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2584b24ab676SJeff Bonwick }
2585b24ab676SJeff Bonwick 
2586b24ab676SJeff Bonwick /*
2587b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2588b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2589b24ab676SJeff Bonwick  */
2590b24ab676SJeff Bonwick /* ARGSUSED */
2591b24ab676SJeff Bonwick void
2592b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2593b24ab676SJeff Bonwick {
2594420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2595b24ab676SJeff Bonwick 	spa_t *spa;
2596b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2597b24ab676SJeff Bonwick 
2598e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2599e0f1c0afSOlaf Faaland 		return;
2600e0f1c0afSOlaf Faaland 
2601b24ab676SJeff Bonwick 	/*
2602b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2603b24ab676SJeff Bonwick 	 */
2604663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2605b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
2606eb633035STom Caputi 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2607b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2608b24ab676SJeff Bonwick 
2609b24ab676SJeff Bonwick 	/*
2610b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2611b24ab676SJeff Bonwick 	 */
2612663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 2, 1);
2613b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
2614eb633035STom Caputi 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2615b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2616b24ab676SJeff Bonwick 
2617b24ab676SJeff Bonwick 	/*
2618b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2619b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2620b24ab676SJeff Bonwick 	 */
2621f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
2622663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2623eb633035STom Caputi 	VERIFY3U(EEXIST, ==,
2624eb633035STom Caputi 	    spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2625b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2626b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2627420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2628b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2629b24ab676SJeff Bonwick 
2630f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
2631b24ab676SJeff Bonwick }
2632b24ab676SJeff Bonwick 
2633e0f1c0afSOlaf Faaland /*
2634e0f1c0afSOlaf Faaland  * Start and then stop the MMP threads to ensure the startup and shutdown code
2635e0f1c0afSOlaf Faaland  * works properly.  Actual protection and property-related code tested via ZTS.
2636e0f1c0afSOlaf Faaland  */
2637e0f1c0afSOlaf Faaland /* ARGSUSED */
2638e0f1c0afSOlaf Faaland void
2639e0f1c0afSOlaf Faaland ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2640e0f1c0afSOlaf Faaland {
2641e0f1c0afSOlaf Faaland 	ztest_shared_opts_t *zo = &ztest_opts;
2642e0f1c0afSOlaf Faaland 	spa_t *spa = ztest_spa;
2643e0f1c0afSOlaf Faaland 
2644e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2645e0f1c0afSOlaf Faaland 		return;
2646e0f1c0afSOlaf Faaland 
2647e0f1c0afSOlaf Faaland 	/*
2648e0f1c0afSOlaf Faaland 	 * Since enabling MMP involves setting a property, it could not be done
2649e0f1c0afSOlaf Faaland 	 * while the pool is suspended.
2650e0f1c0afSOlaf Faaland 	 */
2651e0f1c0afSOlaf Faaland 	if (spa_suspended(spa))
2652e0f1c0afSOlaf Faaland 		return;
2653e0f1c0afSOlaf Faaland 
2654e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2655e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2656e0f1c0afSOlaf Faaland 
2657e0f1c0afSOlaf Faaland 	zfs_multihost_fail_intervals = 0;
2658e0f1c0afSOlaf Faaland 
2659e0f1c0afSOlaf Faaland 	if (!spa_multihost(spa)) {
2660e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_TRUE;
2661e0f1c0afSOlaf Faaland 		mmp_thread_start(spa);
2662e0f1c0afSOlaf Faaland 	}
2663e0f1c0afSOlaf Faaland 
2664e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2665e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2666e0f1c0afSOlaf Faaland 
2667e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2668e0f1c0afSOlaf Faaland 	mmp_signal_all_threads();
2669e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2670e0f1c0afSOlaf Faaland 
2671e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2672e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2673e0f1c0afSOlaf Faaland 
2674e0f1c0afSOlaf Faaland 	if (spa_multihost(spa)) {
2675e0f1c0afSOlaf Faaland 		mmp_thread_stop(spa);
2676e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_FALSE;
2677e0f1c0afSOlaf Faaland 	}
2678e0f1c0afSOlaf Faaland 
2679e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2680e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2681e0f1c0afSOlaf Faaland }
2682e0f1c0afSOlaf Faaland 
268325345e46SGeorge Wilson /* ARGSUSED */
268425345e46SGeorge Wilson void
268525345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
268625345e46SGeorge Wilson {
268725345e46SGeorge Wilson 	spa_t *spa;
268825345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
268925345e46SGeorge Wilson 	uint64_t version, newversion;
269025345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
269125345e46SGeorge Wilson 	char *name;
269225345e46SGeorge Wilson 
2693e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2694e0f1c0afSOlaf Faaland 		return;
2695e0f1c0afSOlaf Faaland 
2696f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
269725345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
269825345e46SGeorge Wilson 
269925345e46SGeorge Wilson 	/*
270025345e46SGeorge Wilson 	 * Clean up from previous runs.
270125345e46SGeorge Wilson 	 */
270225345e46SGeorge Wilson 	(void) spa_destroy(name);
270325345e46SGeorge Wilson 
270425345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2705663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
270625345e46SGeorge Wilson 
270725345e46SGeorge Wilson 	/*
270825345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
270925345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
271025345e46SGeorge Wilson 	 */
271125345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
271225345e46SGeorge Wilson 	case 0:
271325345e46SGeorge Wilson 	case 1:
271425345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
271525345e46SGeorge Wilson 		break;
271625345e46SGeorge Wilson 	case 2:
271725345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
271825345e46SGeorge Wilson 		break;
271925345e46SGeorge Wilson 	case 3:
272025345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
272125345e46SGeorge Wilson 		break;
272225345e46SGeorge Wilson 	}
272325345e46SGeorge Wilson 
272425345e46SGeorge Wilson 	/*
272525345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
272625345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
272725345e46SGeorge Wilson 	 */
272825345e46SGeorge Wilson 	do {
272925345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
273025345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
273125345e46SGeorge Wilson 
273225345e46SGeorge Wilson 	props = fnvlist_alloc();
273325345e46SGeorge Wilson 	fnvlist_add_uint64(props,
273425345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2735eb633035STom Caputi 	VERIFY0(spa_create(name, nvroot, props, NULL, NULL));
273625345e46SGeorge Wilson 	fnvlist_free(nvroot);
273725345e46SGeorge Wilson 	fnvlist_free(props);
273825345e46SGeorge Wilson 
273925345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
274025345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
274125345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
274225345e46SGeorge Wilson 
274325345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
274425345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
274525345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
274625345e46SGeorge Wilson 	}
274725345e46SGeorge Wilson 
274825345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
274925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
275025345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
275125345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
275225345e46SGeorge Wilson 	spa_close(spa, FTAG);
275325345e46SGeorge Wilson 
275425345e46SGeorge Wilson 	strfree(name);
2755f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
275625345e46SGeorge Wilson }
275725345e46SGeorge Wilson 
275886714001SSerapheim Dimitropoulos static void
275986714001SSerapheim Dimitropoulos ztest_spa_checkpoint(spa_t *spa)
276086714001SSerapheim Dimitropoulos {
276186714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
276286714001SSerapheim Dimitropoulos 
276386714001SSerapheim Dimitropoulos 	int error = spa_checkpoint(spa->spa_name);
276486714001SSerapheim Dimitropoulos 
276586714001SSerapheim Dimitropoulos 	switch (error) {
276686714001SSerapheim Dimitropoulos 	case 0:
276786714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
276886714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
276986714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
277086714001SSerapheim Dimitropoulos 		break;
277186714001SSerapheim Dimitropoulos 	case ENOSPC:
277286714001SSerapheim Dimitropoulos 		ztest_record_enospc(FTAG);
277386714001SSerapheim Dimitropoulos 		break;
277486714001SSerapheim Dimitropoulos 	default:
277586714001SSerapheim Dimitropoulos 		fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
277686714001SSerapheim Dimitropoulos 	}
277786714001SSerapheim Dimitropoulos }
277886714001SSerapheim Dimitropoulos 
277986714001SSerapheim Dimitropoulos static void
278086714001SSerapheim Dimitropoulos ztest_spa_discard_checkpoint(spa_t *spa)
278186714001SSerapheim Dimitropoulos {
278286714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
278386714001SSerapheim Dimitropoulos 
278486714001SSerapheim Dimitropoulos 	int error = spa_checkpoint_discard(spa->spa_name);
278586714001SSerapheim Dimitropoulos 
278686714001SSerapheim Dimitropoulos 	switch (error) {
278786714001SSerapheim Dimitropoulos 	case 0:
278886714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
278986714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
279086714001SSerapheim Dimitropoulos 		break;
279186714001SSerapheim Dimitropoulos 	default:
279286714001SSerapheim Dimitropoulos 		fatal(0, "spa_discard_checkpoint(%s) = %d",
279386714001SSerapheim Dimitropoulos 		    spa->spa_name, error);
279486714001SSerapheim Dimitropoulos 	}
279586714001SSerapheim Dimitropoulos 
279686714001SSerapheim Dimitropoulos }
279786714001SSerapheim Dimitropoulos 
279886714001SSerapheim Dimitropoulos /* ARGSUSED */
279986714001SSerapheim Dimitropoulos void
280086714001SSerapheim Dimitropoulos ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
280186714001SSerapheim Dimitropoulos {
280286714001SSerapheim Dimitropoulos 	spa_t *spa = ztest_spa;
280386714001SSerapheim Dimitropoulos 
280486714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
280586714001SSerapheim Dimitropoulos 	if (ztest_random(2) == 0) {
280686714001SSerapheim Dimitropoulos 		ztest_spa_checkpoint(spa);
280786714001SSerapheim Dimitropoulos 	} else {
280886714001SSerapheim Dimitropoulos 		ztest_spa_discard_checkpoint(spa);
280986714001SSerapheim Dimitropoulos 	}
281086714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
281186714001SSerapheim Dimitropoulos }
281286714001SSerapheim Dimitropoulos 
281386714001SSerapheim Dimitropoulos 
2814b24ab676SJeff Bonwick static vdev_t *
2815b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2816b24ab676SJeff Bonwick {
2817b24ab676SJeff Bonwick 	vdev_t *mvd;
2818b24ab676SJeff Bonwick 
2819b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2820b24ab676SJeff Bonwick 		return (vd);
2821b24ab676SJeff Bonwick 
2822b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2823b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2824b24ab676SJeff Bonwick 		    NULL)
2825b24ab676SJeff Bonwick 			return (mvd);
2826b24ab676SJeff Bonwick 
2827b24ab676SJeff Bonwick 	return (NULL);
2828b24ab676SJeff Bonwick }
2829b24ab676SJeff Bonwick 
2830b24ab676SJeff Bonwick /*
2831b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2832b24ab676SJeff Bonwick  */
2833b24ab676SJeff Bonwick int
2834b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2835b24ab676SJeff Bonwick {
2836b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2837b24ab676SJeff Bonwick 	int c;
2838b24ab676SJeff Bonwick 
2839b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2840b24ab676SJeff Bonwick 
2841b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2842b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2843b24ab676SJeff Bonwick 
2844b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2845b24ab676SJeff Bonwick 			break;
2846b24ab676SJeff Bonwick 	}
2847b24ab676SJeff Bonwick 	return (c);
2848b24ab676SJeff Bonwick }
2849b24ab676SJeff Bonwick 
2850b24ab676SJeff Bonwick /*
2851b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2852b24ab676SJeff Bonwick  */
2853b24ab676SJeff Bonwick /* ARGSUSED */
2854b24ab676SJeff Bonwick void
2855b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2856b24ab676SJeff Bonwick {
2857b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2858420dfc95SChris Siden 	spa_t *spa = ztest_spa;
28591195e687SMark J Musante 	uint64_t leaves;
2860b24ab676SJeff Bonwick 	uint64_t guid;
2861b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2862b24ab676SJeff Bonwick 	int error;
2863b24ab676SJeff Bonwick 
2864e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2865e0f1c0afSOlaf Faaland 		return;
2866e0f1c0afSOlaf Faaland 
2867f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
28683b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2869b24ab676SJeff Bonwick 
2870b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2871b24ab676SJeff Bonwick 
2872b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2873b24ab676SJeff Bonwick 
2874b24ab676SJeff Bonwick 	/*
2875b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2876b24ab676SJeff Bonwick 	 */
287788ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2878663207adSDon Brady 		metaslab_group_t *mg;
2879663207adSDon Brady 
288088ecc943SGeorge Wilson 		/*
2881663207adSDon Brady 		 * find the first real slog in log allocation class
288288ecc943SGeorge Wilson 		 */
2883663207adSDon Brady 		mg =  spa_log_class(spa)->mc_rotor;
2884663207adSDon Brady 		while (!mg->mg_vd->vdev_islog)
2885663207adSDon Brady 			mg = mg->mg_next;
2886663207adSDon Brady 
2887663207adSDon Brady 		guid = mg->mg_vd->vdev_guid;
28888654d025Sperrin 
288988ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2890fa9e4066Sahrens 
289188ecc943SGeorge Wilson 		/*
289288ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
289388ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
289488ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
289588ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
289688ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
289788ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
289888ecc943SGeorge Wilson 		 */
2899f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_WRITER);
290088ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2901f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
2902fa9e4066Sahrens 
290386714001SSerapheim Dimitropoulos 		switch (error) {
290486714001SSerapheim Dimitropoulos 		case 0:
290586714001SSerapheim Dimitropoulos 		case EEXIST:
290686714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
290786714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
290886714001SSerapheim Dimitropoulos 			break;
290986714001SSerapheim Dimitropoulos 		default:
291088ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
291186714001SSerapheim Dimitropoulos 		}
291288ecc943SGeorge Wilson 	} else {
291388ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
291488ecc943SGeorge Wilson 
291588ecc943SGeorge Wilson 		/*
2916663207adSDon Brady 		 * Make 1/4 of the devices be log devices
291788ecc943SGeorge Wilson 		 */
291825345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2919663207adSDon Brady 		    ztest_opts.zo_vdev_size, 0, (ztest_random(4) == 0) ?
2920663207adSDon Brady 		    "log" : NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
292188ecc943SGeorge Wilson 
292288ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
292388ecc943SGeorge Wilson 		nvlist_free(nvroot);
292488ecc943SGeorge Wilson 
292586714001SSerapheim Dimitropoulos 		switch (error) {
292686714001SSerapheim Dimitropoulos 		case 0:
292786714001SSerapheim Dimitropoulos 			break;
292886714001SSerapheim Dimitropoulos 		case ENOSPC:
292988ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
293086714001SSerapheim Dimitropoulos 			break;
293186714001SSerapheim Dimitropoulos 		default:
293288ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
293386714001SSerapheim Dimitropoulos 		}
293488ecc943SGeorge Wilson 	}
293588ecc943SGeorge Wilson 
2936f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
2937e14bb325SJeff Bonwick }
2938fa9e4066Sahrens 
2939663207adSDon Brady /* ARGSUSED */
2940663207adSDon Brady void
2941663207adSDon Brady ztest_vdev_class_add(ztest_ds_t *zd, uint64_t id)
2942663207adSDon Brady {
2943663207adSDon Brady 	ztest_shared_t *zs = ztest_shared;
2944663207adSDon Brady 	spa_t *spa = ztest_spa;
2945663207adSDon Brady 	uint64_t leaves;
2946663207adSDon Brady 	nvlist_t *nvroot;
2947663207adSDon Brady 	const char *class = (ztest_random(2) == 0) ?
2948663207adSDon Brady 	    VDEV_ALLOC_BIAS_SPECIAL : VDEV_ALLOC_BIAS_DEDUP;
2949663207adSDon Brady 	int error;
2950663207adSDon Brady 
2951663207adSDon Brady 	/*
2952663207adSDon Brady 	 * By default add a special vdev 50% of the time
2953663207adSDon Brady 	 */
2954663207adSDon Brady 	if ((ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_OFF) ||
2955663207adSDon Brady 	    (ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_RND &&
2956663207adSDon Brady 	    ztest_random(2) == 0)) {
2957663207adSDon Brady 		return;
2958663207adSDon Brady 	}
2959663207adSDon Brady 
2960663207adSDon Brady 	mutex_enter(&ztest_vdev_lock);
2961663207adSDon Brady 
2962663207adSDon Brady 	/* Only test with mirrors */
2963663207adSDon Brady 	if (zs->zs_mirrors < 2) {
2964663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2965663207adSDon Brady 		return;
2966663207adSDon Brady 	}
2967663207adSDon Brady 
2968663207adSDon Brady 	/* requires feature@allocation_classes */
2969663207adSDon Brady 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)) {
2970663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2971663207adSDon Brady 		return;
2972663207adSDon Brady 	}
2973663207adSDon Brady 
2974663207adSDon Brady 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2975663207adSDon Brady 
2976663207adSDon Brady 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2977663207adSDon Brady 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2978663207adSDon Brady 	spa_config_exit(spa, SCL_VDEV, FTAG);
2979663207adSDon Brady 
2980663207adSDon Brady 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
2981663207adSDon Brady 	    class, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
2982663207adSDon Brady 
2983663207adSDon Brady 	error = spa_vdev_add(spa, nvroot);
2984663207adSDon Brady 	nvlist_free(nvroot);
2985663207adSDon Brady 
2986663207adSDon Brady 	if (error == ENOSPC)
2987663207adSDon Brady 		ztest_record_enospc("spa_vdev_add");
2988663207adSDon Brady 	else if (error != 0)
2989663207adSDon Brady 		fatal(0, "spa_vdev_add() = %d", error);
2990663207adSDon Brady 
2991663207adSDon Brady 	/*
2992663207adSDon Brady 	 * 50% of the time allow small blocks in the special class
2993663207adSDon Brady 	 */
2994663207adSDon Brady 	if (error == 0 &&
2995663207adSDon Brady 	    spa_special_class(spa)->mc_groups == 1 && ztest_random(2) == 0) {
2996663207adSDon Brady 		if (ztest_opts.zo_verbose >= 3)
2997663207adSDon Brady 			(void) printf("Enabling special VDEV small blocks\n");
2998663207adSDon Brady 		(void) ztest_dsl_prop_set_uint64(zd->zd_name,
2999663207adSDon Brady 		    ZFS_PROP_SPECIAL_SMALL_BLOCKS, 32768, B_FALSE);
3000663207adSDon Brady 	}
3001663207adSDon Brady 
3002663207adSDon Brady 	mutex_exit(&ztest_vdev_lock);
3003663207adSDon Brady 
3004663207adSDon Brady 	if (ztest_opts.zo_verbose >= 3) {
3005663207adSDon Brady 		metaslab_class_t *mc;
3006663207adSDon Brady 
3007663207adSDon Brady 		if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3008663207adSDon Brady 			mc = spa_special_class(spa);
3009663207adSDon Brady 		else
3010663207adSDon Brady 			mc = spa_dedup_class(spa);
3011663207adSDon Brady 		(void) printf("Added a %s mirrored vdev (of %d)\n",
3012663207adSDon Brady 		    class, (int)mc->mc_groups);
3013663207adSDon Brady 	}
3014663207adSDon Brady }
3015663207adSDon Brady 
3016e14bb325SJeff Bonwick /*
3017e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3018e14bb325SJeff Bonwick  */
3019b24ab676SJeff Bonwick /* ARGSUSED */
3020e14bb325SJeff Bonwick void
3021b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
3022e14bb325SJeff Bonwick {
3023b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
3024420dfc95SChris Siden 	spa_t *spa = ztest_spa;
302531157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
3026e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
3027e14bb325SJeff Bonwick 	char *aux;
3028e14bb325SJeff Bonwick 	uint64_t guid = 0;
3029e14bb325SJeff Bonwick 	int error;
3030e14bb325SJeff Bonwick 
3031e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3032e0f1c0afSOlaf Faaland 		return;
3033e0f1c0afSOlaf Faaland 
303431157203SJeff Bonwick 	if (ztest_random(2) == 0) {
3035e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
3036e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
3037e14bb325SJeff Bonwick 	} else {
3038e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
3039e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
3040e14bb325SJeff Bonwick 	}
3041e14bb325SJeff Bonwick 
3042f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3043e14bb325SJeff Bonwick 
3044e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3045e14bb325SJeff Bonwick 
3046e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
3047e14bb325SJeff Bonwick 		/*
3048e14bb325SJeff Bonwick 		 * Pick a random device to remove.
3049e14bb325SJeff Bonwick 		 */
3050e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3051e14bb325SJeff Bonwick 	} else {
3052e14bb325SJeff Bonwick 		/*
3053e14bb325SJeff Bonwick 		 * Find an unused device we can add.
3054e14bb325SJeff Bonwick 		 */
3055b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
3056e14bb325SJeff Bonwick 		for (;;) {
3057e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
3058e14bb325SJeff Bonwick 			int c;
3059420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
3060420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3061420dfc95SChris Siden 			    zs->zs_vdev_aux);
3062e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
3063e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
3064e14bb325SJeff Bonwick 				    path) == 0)
3065e14bb325SJeff Bonwick 					break;
306631157203SJeff Bonwick 			if (c == sav->sav_count &&
306731157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
3068e14bb325SJeff Bonwick 				break;
3069b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
3070e14bb325SJeff Bonwick 		}
3071e14bb325SJeff Bonwick 	}
3072e14bb325SJeff Bonwick 
3073e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
3074e14bb325SJeff Bonwick 
3075e14bb325SJeff Bonwick 	if (guid == 0) {
3076e14bb325SJeff Bonwick 		/*
3077e14bb325SJeff Bonwick 		 * Add a new device.
3078e14bb325SJeff Bonwick 		 */
307925345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3080663207adSDon Brady 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, NULL, 0, 0, 1);
3081e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
308286714001SSerapheim Dimitropoulos 
308386714001SSerapheim Dimitropoulos 		switch (error) {
308486714001SSerapheim Dimitropoulos 		case 0:
308586714001SSerapheim Dimitropoulos 			break;
308686714001SSerapheim Dimitropoulos 		default:
3087e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
308886714001SSerapheim Dimitropoulos 		}
3089e14bb325SJeff Bonwick 		nvlist_free(nvroot);
3090e14bb325SJeff Bonwick 	} else {
3091e14bb325SJeff Bonwick 		/*
3092e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
3093e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
3094e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
3095e14bb325SJeff Bonwick 		 */
3096e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
3097573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
3098e14bb325SJeff Bonwick 
3099e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
310086714001SSerapheim Dimitropoulos 
310186714001SSerapheim Dimitropoulos 		switch (error) {
310286714001SSerapheim Dimitropoulos 		case 0:
310386714001SSerapheim Dimitropoulos 		case EBUSY:
310486714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
310586714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
310686714001SSerapheim Dimitropoulos 			break;
310786714001SSerapheim Dimitropoulos 		default:
3108e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
310986714001SSerapheim Dimitropoulos 		}
3110e14bb325SJeff Bonwick 	}
3111e14bb325SJeff Bonwick 
3112f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3113fa9e4066Sahrens }
3114fa9e4066Sahrens 
31151195e687SMark J Musante /*
31161195e687SMark J Musante  * split a pool if it has mirror tlvdevs
31171195e687SMark J Musante  */
31181195e687SMark J Musante /* ARGSUSED */
31191195e687SMark J Musante void
31201195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
31211195e687SMark J Musante {
31221195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
3123420dfc95SChris Siden 	spa_t *spa = ztest_spa;
31241195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
31251195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
31261195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
31271195e687SMark J Musante 	int error = 0;
31281195e687SMark J Musante 
3129e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3130e0f1c0afSOlaf Faaland 		return;
3131e0f1c0afSOlaf Faaland 
3132f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
31331195e687SMark J Musante 
31341195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
3135420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3136f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
31371195e687SMark J Musante 		return;
31381195e687SMark J Musante 	}
31391195e687SMark J Musante 
31401195e687SMark J Musante 	/* clean up the old pool, if any */
31411195e687SMark J Musante 	(void) spa_destroy("splitp");
31421195e687SMark J Musante 
31431195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
31441195e687SMark J Musante 
31451195e687SMark J Musante 	/* generate a config from the existing config */
314698295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
31471195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
31481195e687SMark J Musante 	    &tree) == 0);
314998295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
315098295d61SMark J Musante 
31511195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
31521195e687SMark J Musante 	    &children) == 0);
31531195e687SMark J Musante 
31541195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
31551195e687SMark J Musante 	for (c = 0; c < children; c++) {
31561195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
31571195e687SMark J Musante 		nvlist_t **mchild;
31581195e687SMark J Musante 		uint_t mchildren;
31591195e687SMark J Musante 
31601195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
31611195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
31621195e687SMark J Musante 			    0) == 0);
31631195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
31641195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
31651195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
31661195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
31671195e687SMark J Musante 			if (lastlogid == 0)
31681195e687SMark J Musante 				lastlogid = schildren;
31691195e687SMark J Musante 			++schildren;
31701195e687SMark J Musante 			continue;
31711195e687SMark J Musante 		}
31721195e687SMark J Musante 		lastlogid = 0;
31731195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
31741195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
31751195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
31761195e687SMark J Musante 	}
31771195e687SMark J Musante 
31781195e687SMark J Musante 	/* OK, create a config that can be used to split */
31791195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
31801195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
31811195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
31821195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
31831195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
31841195e687SMark J Musante 
31851195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
31861195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
31871195e687SMark J Musante 
31881195e687SMark J Musante 	for (c = 0; c < schildren; c++)
31891195e687SMark J Musante 		nvlist_free(schild[c]);
31901195e687SMark J Musante 	free(schild);
31911195e687SMark J Musante 	nvlist_free(split);
31921195e687SMark J Musante 
31931195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
31941195e687SMark J Musante 
3195f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
31961195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3197f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
31981195e687SMark J Musante 
31991195e687SMark J Musante 	nvlist_free(config);
32001195e687SMark J Musante 
32011195e687SMark J Musante 	if (error == 0) {
32021195e687SMark J Musante 		(void) printf("successful split - results:\n");
32031195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
32041195e687SMark J Musante 		show_pool_stats(spa);
32051195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
32061195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
32071195e687SMark J Musante 		++zs->zs_splits;
32081195e687SMark J Musante 		--zs->zs_mirrors;
32091195e687SMark J Musante 	}
3210f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
32111195e687SMark J Musante }
32121195e687SMark J Musante 
3213fa9e4066Sahrens /*
3214fa9e4066Sahrens  * Verify that we can attach and detach devices.
3215fa9e4066Sahrens  */
3216b24ab676SJeff Bonwick /* ARGSUSED */
3217fa9e4066Sahrens void
3218b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3219fa9e4066Sahrens {
3220b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
3221420dfc95SChris Siden 	spa_t *spa = ztest_spa;
322231157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
3223fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3224ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
3225e14bb325SJeff Bonwick 	nvlist_t *root;
32261195e687SMark J Musante 	uint64_t leaves;
3227fa9e4066Sahrens 	uint64_t leaf, top;
3228ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
32298ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
3230b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
3231ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
3232fa9e4066Sahrens 	int replacing;
32339af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
323431157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
323531157203SJeff Bonwick 	int oldvd_is_log;
3236fa9e4066Sahrens 	int error, expected_error;
3237fa9e4066Sahrens 
3238e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3239e0f1c0afSOlaf Faaland 		return;
3240e0f1c0afSOlaf Faaland 
3241f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3242420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3243fa9e4066Sahrens 
32445cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32455cabbc6bSPrashanth Sreenivasa 
32465cabbc6bSPrashanth Sreenivasa 	/*
32475cabbc6bSPrashanth Sreenivasa 	 * If a vdev is in the process of being removed, its removal may
32485cabbc6bSPrashanth Sreenivasa 	 * finish while we are in progress, leading to an unexpected error
32495cabbc6bSPrashanth Sreenivasa 	 * value.  Don't bother trying to attach while we are in the middle
32505cabbc6bSPrashanth Sreenivasa 	 * of removal.
32515cabbc6bSPrashanth Sreenivasa 	 */
32523a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
32535cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
3254f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
32555cabbc6bSPrashanth Sreenivasa 		return;
32565cabbc6bSPrashanth Sreenivasa 	}
3257fa9e4066Sahrens 
3258fa9e4066Sahrens 	/*
3259fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
3260fa9e4066Sahrens 	 */
3261fa9e4066Sahrens 	replacing = ztest_random(2);
3262fa9e4066Sahrens 
3263fa9e4066Sahrens 	/*
3264fa9e4066Sahrens 	 * Pick a random top-level vdev.
3265fa9e4066Sahrens 	 */
3266b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3267fa9e4066Sahrens 
3268fa9e4066Sahrens 	/*
3269fa9e4066Sahrens 	 * Pick a random leaf within it.
3270fa9e4066Sahrens 	 */
3271d41c4376SMark J Musante 	leaf = ztest_random(leaves);
3272fa9e4066Sahrens 
3273fa9e4066Sahrens 	/*
327431157203SJeff Bonwick 	 * Locate this vdev.
3275fa9e4066Sahrens 	 */
327631157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
3277663207adSDon Brady 
3278663207adSDon Brady 	/* pick a child from the mirror */
32791195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
32808ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
32811195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3282420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
32838ad4d6ddSJeff Bonwick 	}
3284663207adSDon Brady 
3285663207adSDon Brady 	/* pick a child out of the raidz group */
3286420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
32878ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3288420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3289420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
32908ad4d6ddSJeff Bonwick 	}
3291fa9e4066Sahrens 
3292fa9e4066Sahrens 	/*
329331157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
329431157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
3295fa9e4066Sahrens 	 */
329631157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
32979af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
32988ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
32998ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
330031157203SJeff Bonwick 	}
3301fa9e4066Sahrens 
330231157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
3303573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
330431157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
330531157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
3306ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
33078ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
3308fa9e4066Sahrens 
33099af0a4dfSJeff Bonwick 	/*
33109af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
33119af0a4dfSJeff Bonwick 	 */
33129af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
33135cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
33148ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
33158ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
331686714001SSerapheim Dimitropoulos 		    error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
331786714001SSerapheim Dimitropoulos 		    error != ZFS_ERR_DISCARDING_CHECKPOINT)
33188ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
3319f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
33209af0a4dfSJeff Bonwick 		return;
33219af0a4dfSJeff Bonwick 	}
33229af0a4dfSJeff Bonwick 
3323fa9e4066Sahrens 	/*
332431157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
332531157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3326fa9e4066Sahrens 	 */
332731157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
332831157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
332931157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
333031157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
333131157203SJeff Bonwick 	} else {
333231157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3333420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
3334420dfc95SChris Siden 		    top * leaves + leaf);
333531157203SJeff Bonwick 		if (ztest_random(2) == 0)
333631157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
333731157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
333831157203SJeff Bonwick 	}
333931157203SJeff Bonwick 
334031157203SJeff Bonwick 	if (newvd) {
33415cabbc6bSPrashanth Sreenivasa 		/*
33425cabbc6bSPrashanth Sreenivasa 		 * Reopen to ensure the vdev's asize field isn't stale.
33435cabbc6bSPrashanth Sreenivasa 		 */
33445cabbc6bSPrashanth Sreenivasa 		vdev_reopen(newvd);
3345573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
334631157203SJeff Bonwick 	} else {
334731157203SJeff Bonwick 		/*
334831157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
334931157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
335031157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
335131157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
335231157203SJeff Bonwick 		 */
335331157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
335431157203SJeff Bonwick 	}
3355fa9e4066Sahrens 
3356fa9e4066Sahrens 	/*
3357fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3358fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
3359fa9e4066Sahrens 	 *
3360ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
3361fa9e4066Sahrens 	 *
3362ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
3363fa9e4066Sahrens 	 */
336431157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
336531157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
336631157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
336731157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
336831157203SJeff Bonwick 		expected_error = ENOTSUP;
336931157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
3370fa9e4066Sahrens 		expected_error = ENOTSUP;
337131157203SJeff Bonwick 	else if (newvd == oldvd)
337231157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
337331157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
337431157203SJeff Bonwick 		expected_error = EBUSY;
3375ea8dc4b6Seschrock 	else if (newsize < oldsize)
3376fa9e4066Sahrens 		expected_error = EOVERFLOW;
3377ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
3378ecc2d604Sbonwick 		expected_error = EDOM;
3379fa9e4066Sahrens 	else
3380fa9e4066Sahrens 		expected_error = 0;
3381fa9e4066Sahrens 
33825cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_ALL, FTAG);
3383fa9e4066Sahrens 
3384fa9e4066Sahrens 	/*
3385ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
3386fa9e4066Sahrens 	 */
338725345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3388663207adSDon Brady 	    ashift, NULL, 0, 0, 1);
3389fa9e4066Sahrens 
339031157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
3391fa9e4066Sahrens 
3392fa9e4066Sahrens 	nvlist_free(root);
3393fa9e4066Sahrens 
3394fa9e4066Sahrens 	/*
3395fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
3396fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
3397fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
3398fa9e4066Sahrens 	 */
3399fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
3400fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
3401fa9e4066Sahrens 		expected_error = error;
3402fa9e4066Sahrens 
3403f0aa80d4Sbonwick 	/*
3404f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
3405f0aa80d4Sbonwick 	 */
3406088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
3407f0aa80d4Sbonwick 		expected_error = error;
3408f0aa80d4Sbonwick 
340986714001SSerapheim Dimitropoulos 	if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
341086714001SSerapheim Dimitropoulos 	    error == ZFS_ERR_DISCARDING_CHECKPOINT)
341186714001SSerapheim Dimitropoulos 		expected_error = error;
341286714001SSerapheim Dimitropoulos 
3413088f3894Sahrens 	/* XXX workaround 6690467 */
3414088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
3415088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
3416088f3894Sahrens 		    "returned %d, expected %d",
3417b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
3418b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
3419fa9e4066Sahrens 	}
3420fa9e4066Sahrens 
3421f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3422fa9e4066Sahrens }
3423fa9e4066Sahrens 
34245cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
34255cabbc6bSPrashanth Sreenivasa void
34265cabbc6bSPrashanth Sreenivasa ztest_device_removal(ztest_ds_t *zd, uint64_t id)
34275cabbc6bSPrashanth Sreenivasa {
34285cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ztest_spa;
34295cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
34305cabbc6bSPrashanth Sreenivasa 	uint64_t guid;
34313a4b1be9SMatthew Ahrens 	int error;
34325cabbc6bSPrashanth Sreenivasa 
3433f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
34345cabbc6bSPrashanth Sreenivasa 
34353a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
34363a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34373a4b1be9SMatthew Ahrens 		return;
34383a4b1be9SMatthew Ahrens 	}
34393a4b1be9SMatthew Ahrens 
34403a4b1be9SMatthew Ahrens 	/*
34413a4b1be9SMatthew Ahrens 	 * Remove a random top-level vdev and wait for removal to finish.
34423a4b1be9SMatthew Ahrens 	 */
34435cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34445cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
34455cabbc6bSPrashanth Sreenivasa 	guid = vd->vdev_guid;
34465cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
34475cabbc6bSPrashanth Sreenivasa 
34483a4b1be9SMatthew Ahrens 	error = spa_vdev_remove(spa, guid, B_FALSE);
34493a4b1be9SMatthew Ahrens 	if (error == 0) {
34503a4b1be9SMatthew Ahrens 		ztest_device_removal_active = B_TRUE;
34513a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34523a4b1be9SMatthew Ahrens 
34533a4b1be9SMatthew Ahrens 		while (spa->spa_vdev_removal != NULL)
34543a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34553a4b1be9SMatthew Ahrens 	} else {
34563a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34573a4b1be9SMatthew Ahrens 		return;
34583a4b1be9SMatthew Ahrens 	}
34595cabbc6bSPrashanth Sreenivasa 
34603a4b1be9SMatthew Ahrens 	/*
34613a4b1be9SMatthew Ahrens 	 * The pool needs to be scrubbed after completing device removal.
34623a4b1be9SMatthew Ahrens 	 * Failure to do so may result in checksum errors due to the
34633a4b1be9SMatthew Ahrens 	 * strategy employed by ztest_fault_inject() when selecting which
34643a4b1be9SMatthew Ahrens 	 * offset are redundant and can be damaged.
34653a4b1be9SMatthew Ahrens 	 */
34663a4b1be9SMatthew Ahrens 	error = spa_scan(spa, POOL_SCAN_SCRUB);
34673a4b1be9SMatthew Ahrens 	if (error == 0) {
34683a4b1be9SMatthew Ahrens 		while (dsl_scan_scrubbing(spa_get_dsl(spa)))
34693a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34703a4b1be9SMatthew Ahrens 	}
34713a4b1be9SMatthew Ahrens 
34723a4b1be9SMatthew Ahrens 	mutex_enter(&ztest_vdev_lock);
34733a4b1be9SMatthew Ahrens 	ztest_device_removal_active = B_FALSE;
3474f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
34755cabbc6bSPrashanth Sreenivasa }
34765cabbc6bSPrashanth Sreenivasa 
3477573ca77eSGeorge Wilson /*
3478573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
3479573ca77eSGeorge Wilson  */
3480573ca77eSGeorge Wilson vdev_t *
3481573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
3482573ca77eSGeorge Wilson {
3483573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3484573ca77eSGeorge Wilson 	size_t *newsize = arg;
3485573ca77eSGeorge Wilson 	size_t fsize;
3486573ca77eSGeorge Wilson 	int fd;
3487573ca77eSGeorge Wilson 
3488573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3489573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3490573ca77eSGeorge Wilson 
3491573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3492573ca77eSGeorge Wilson 		return (vd);
3493573ca77eSGeorge Wilson 
3494573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
3495573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
3496573ca77eSGeorge Wilson 
3497420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3498573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
3499573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3500573ca77eSGeorge Wilson 	}
3501573ca77eSGeorge Wilson 	(void) close(fd);
3502573ca77eSGeorge Wilson 	return (NULL);
3503573ca77eSGeorge Wilson }
3504573ca77eSGeorge Wilson 
3505573ca77eSGeorge Wilson /*
3506573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
3507573ca77eSGeorge Wilson  */
3508573ca77eSGeorge Wilson /* ARGSUSED */
3509573ca77eSGeorge Wilson vdev_t *
3510573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
3511573ca77eSGeorge Wilson {
3512573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3513573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
3514573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
35158f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
3516095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3517095bcd66SGeorge Wilson 	int error;
3518573ca77eSGeorge Wilson 
3519573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3520573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3521573ca77eSGeorge Wilson 
3522573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
3523573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3524095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3525573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3526573ca77eSGeorge Wilson 
3527095bcd66SGeorge Wilson 	/*
3528095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
3529095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
3530095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
3531095bcd66SGeorge Wilson 	 */
3532095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
3533420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3534095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
3535095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
3536095bcd66SGeorge Wilson 		}
3537095bcd66SGeorge Wilson 		return (vd);
3538095bcd66SGeorge Wilson 	}
3539095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3540095bcd66SGeorge Wilson 
3541573ca77eSGeorge Wilson 	/*
3542573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
3543573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
3544573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
3545573ca77eSGeorge Wilson 	 * trying to online it.
3546573ca77eSGeorge Wilson 	 */
35478f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
3548420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
35498f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
35508f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
3551b24ab676SJeff Bonwick 			    "got gen %llu\n",
3552b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
35538f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
35548f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
35558f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
3556573ca77eSGeorge Wilson 		}
3557573ca77eSGeorge Wilson 		return (vd);
3558573ca77eSGeorge Wilson 	}
3559573ca77eSGeorge Wilson 	return (NULL);
3560573ca77eSGeorge Wilson }
3561573ca77eSGeorge Wilson 
3562573ca77eSGeorge Wilson /*
3563573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
3564573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
3565573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3566573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3567573ca77eSGeorge Wilson  * leaf vdev we encounter.
3568573ca77eSGeorge Wilson  */
3569573ca77eSGeorge Wilson vdev_t *
3570573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3571573ca77eSGeorge Wilson {
3572573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3573573ca77eSGeorge Wilson 		if (func == NULL)
3574573ca77eSGeorge Wilson 			return (vd);
3575573ca77eSGeorge Wilson 		else
3576573ca77eSGeorge Wilson 			return (func(vd, arg));
3577573ca77eSGeorge Wilson 	}
3578573ca77eSGeorge Wilson 
3579573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3580573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3581573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3582573ca77eSGeorge Wilson 			return (cvd);
3583573ca77eSGeorge Wilson 	}
3584573ca77eSGeorge Wilson 	return (NULL);
3585573ca77eSGeorge Wilson }
3586573ca77eSGeorge Wilson 
3587fa9e4066Sahrens /*
3588fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3589fa9e4066Sahrens  */
3590b24ab676SJeff Bonwick /* ARGSUSED */
3591fa9e4066Sahrens void
3592b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3593fa9e4066Sahrens {
3594420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3595b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3596b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3597b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3598573ca77eSGeorge Wilson 	size_t psize, newsize;
3599b24ab676SJeff Bonwick 	uint64_t top;
3600b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3601fa9e4066Sahrens 
360286714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
3603f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3604573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3605573ca77eSGeorge Wilson 
36065cabbc6bSPrashanth Sreenivasa 	/*
36075cabbc6bSPrashanth Sreenivasa 	 * If there is a vdev removal in progress, it could complete while
36085cabbc6bSPrashanth Sreenivasa 	 * we are running, in which case we would not be able to verify
36095cabbc6bSPrashanth Sreenivasa 	 * that the metaslab_class space increased (because it decreases
36105cabbc6bSPrashanth Sreenivasa 	 * when the device removal completes).
36115cabbc6bSPrashanth Sreenivasa 	 */
36123a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
361386714001SSerapheim Dimitropoulos 		spa_config_exit(spa, SCL_STATE, spa);
3614f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
361586714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
36165cabbc6bSPrashanth Sreenivasa 		return;
36175cabbc6bSPrashanth Sreenivasa 	}
36185cabbc6bSPrashanth Sreenivasa 
3619b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3620573ca77eSGeorge Wilson 
3621b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3622b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3623b24ab676SJeff Bonwick 	mc = mg->mg_class;
3624b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3625b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3626fa9e4066Sahrens 
3627fa9e4066Sahrens 	/*
3628573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3629573ca77eSGeorge Wilson 	 * our top-level device.
3630fa9e4066Sahrens 	 */
3631573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3632573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3633573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3634fa9e4066Sahrens 
3635573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3636fa9e4066Sahrens 
3637573ca77eSGeorge Wilson 	/*
36388f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
36398f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3640573ca77eSGeorge Wilson 	 */
36418f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3642420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3643573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3644f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
364586714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3646573ca77eSGeorge Wilson 		return;
3647573ca77eSGeorge Wilson 	}
3648573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3649663207adSDon Brady 	newsize = psize + MAX(psize / 8, SPA_MAXBLOCKSIZE);
3650573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3651fa9e4066Sahrens 
3652420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3653b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3654573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3655573ca77eSGeorge Wilson 	}
3656573ca77eSGeorge Wilson 
3657573ca77eSGeorge Wilson 	/*
3658573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3659573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3660573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3661573ca77eSGeorge Wilson 	 */
3662573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3663573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3664573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3665420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3666b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3667b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3668b24ab676SJeff Bonwick 		}
3669b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3670f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
367186714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3672b24ab676SJeff Bonwick 		return;
3673b24ab676SJeff Bonwick 	}
3674b24ab676SJeff Bonwick 
3675b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3676b24ab676SJeff Bonwick 
3677b24ab676SJeff Bonwick 	/*
3678b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3679b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3680b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3681b24ab676SJeff Bonwick 	 */
3682b24ab676SJeff Bonwick 	for (;;) {
3683b24ab676SJeff Bonwick 		boolean_t done;
3684b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3685b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3686b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3687b24ab676SJeff Bonwick 		if (done)
3688b24ab676SJeff Bonwick 			break;
3689b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3690b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3691b24ab676SJeff Bonwick 	}
3692b24ab676SJeff Bonwick 
3693b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3694b24ab676SJeff Bonwick 
3695b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3696b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3697b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3698b24ab676SJeff Bonwick 
3699b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3700420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3701b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3702b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3703fa9e4066Sahrens 		}
3704b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3705f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
370686714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3707573ca77eSGeorge Wilson 		return;
3708fa9e4066Sahrens 	}
3709fa9e4066Sahrens 
3710573ca77eSGeorge Wilson 	/*
3711b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3712573ca77eSGeorge Wilson 	 */
37135cabbc6bSPrashanth Sreenivasa 	if (new_ms_count <= old_ms_count) {
37145cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3715b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
37165cabbc6bSPrashanth Sreenivasa 	}
3717573ca77eSGeorge Wilson 
3718573ca77eSGeorge Wilson 	/*
3719573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3720573ca77eSGeorge Wilson 	 */
37215cabbc6bSPrashanth Sreenivasa 	if (new_class_space <= old_class_space) {
37225cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3723b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
37245cabbc6bSPrashanth Sreenivasa 	}
3725b24ab676SJeff Bonwick 
3726420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
37270a055120SJason King 		char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3728573ca77eSGeorge Wilson 
37290a055120SJason King 		nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
37300a055120SJason King 		nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3731573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3732573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3733573ca77eSGeorge Wilson 	}
3734b24ab676SJeff Bonwick 
3735573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3736f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
373786714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
3738fa9e4066Sahrens }
3739fa9e4066Sahrens 
3740b24ab676SJeff Bonwick /*
3741b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3742b24ab676SJeff Bonwick  */
3743fa9e4066Sahrens /* ARGSUSED */
3744fa9e4066Sahrens static void
3745b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3746fa9e4066Sahrens {
3747fa9e4066Sahrens 	/*
3748b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3749fa9e4066Sahrens 	 */
3750b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3751fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3752fa9e4066Sahrens }
3753fa9e4066Sahrens 
375455da60b9SMark J Musante static int
375555da60b9SMark J Musante ztest_dataset_create(char *dsname)
375655da60b9SMark J Musante {
3757eb633035STom Caputi 	int err;
3758eb633035STom Caputi 	uint64_t rand;
3759eb633035STom Caputi 	dsl_crypto_params_t *dcp = NULL;
3760eb633035STom Caputi 
3761eb633035STom Caputi 	/*
3762eb633035STom Caputi 	 * 50% of the time, we create encrypted datasets
3763eb633035STom Caputi 	 * using a random cipher suite and a hard-coded
3764eb633035STom Caputi 	 * wrapping key.
3765eb633035STom Caputi 	 */
3766eb633035STom Caputi #ifdef WITHCRYPTO
3767eb633035STom Caputi 	/*
3768eb633035STom Caputi 	 * Until the crypto framework is compiled in userland, the ztest using
3769eb633035STom Caputi 	 * crypto will not work.
3770eb633035STom Caputi 	 */
3771eb633035STom Caputi 	rand = ztest_random(2);
3772eb633035STom Caputi #else
3773eb633035STom Caputi 	rand = 0;
3774eb633035STom Caputi #endif
3775eb633035STom Caputi 	if (rand != 0) {
3776eb633035STom Caputi 		nvlist_t *crypto_args = fnvlist_alloc();
3777eb633035STom Caputi 		nvlist_t *props = fnvlist_alloc();
3778eb633035STom Caputi 
3779eb633035STom Caputi 		/* slight bias towards the default cipher suite */
3780eb633035STom Caputi 		rand = ztest_random(ZIO_CRYPT_FUNCTIONS);
3781eb633035STom Caputi 		if (rand < ZIO_CRYPT_AES_128_CCM)
3782eb633035STom Caputi 			rand = ZIO_CRYPT_ON;
3783eb633035STom Caputi 
3784eb633035STom Caputi 		fnvlist_add_uint64(props,
3785eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_ENCRYPTION), rand);
3786eb633035STom Caputi 		fnvlist_add_uint8_array(crypto_args, "wkeydata",
3787eb633035STom Caputi 		    (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
3788eb633035STom Caputi 
3789eb633035STom Caputi 		/*
3790eb633035STom Caputi 		 * These parameters aren't really used by the kernel. They
3791eb633035STom Caputi 		 * are simply stored so that userspace knows how to load
3792eb633035STom Caputi 		 * the wrapping key.
3793eb633035STom Caputi 		 */
3794eb633035STom Caputi 		fnvlist_add_uint64(props,
3795eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_KEYFORMAT), ZFS_KEYFORMAT_RAW);
3796eb633035STom Caputi 		fnvlist_add_string(props,
3797eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), "prompt");
3798eb633035STom Caputi 		fnvlist_add_uint64(props,
3799eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 0ULL);
3800eb633035STom Caputi 		fnvlist_add_uint64(props,
3801eb633035STom Caputi 		    zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 0ULL);
3802eb633035STom Caputi 
3803eb633035STom Caputi 		VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, props,
3804eb633035STom Caputi 		    crypto_args, &dcp));
3805eb633035STom Caputi 
3806eb633035STom Caputi 		fnvlist_free(crypto_args);
3807eb633035STom Caputi 		fnvlist_free(props);
3808eb633035STom Caputi 	}
3809eb633035STom Caputi 
3810eb633035STom Caputi 	err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, dcp,
381155da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
3812eb633035STom Caputi 	dsl_crypto_params_free(dcp, !!err);
381355da60b9SMark J Musante 
3814eb633035STom Caputi 	rand = ztest_random(100);
3815eb633035STom Caputi 	if (err || rand < 80)
381655da60b9SMark J Musante 		return (err);
381755da60b9SMark J Musante 
3818420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3819420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
382055da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
382155da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
382255da60b9SMark J Musante }
382355da60b9SMark J Musante 
3824b24ab676SJeff Bonwick /* ARGSUSED */
38251d452cf5Sahrens static int
3826fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3827fa9e4066Sahrens {
3828fa9e4066Sahrens 	objset_t *os;
3829b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3830fa9e4066Sahrens 	int error;
3831fa9e4066Sahrens 
3832fa9e4066Sahrens 	/*
3833fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3834fa9e4066Sahrens 	 */
3835eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3836eb633035STom Caputi 	    B_TRUE, FTAG, &os));
3837b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3838e1930233Sbonwick 	if (error != ENOENT) {
3839e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3840fb09f5aaSMadhav Suresh 		ASSERT0(error);
3841b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3842b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3843e1930233Sbonwick 	}
3844eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
3845fa9e4066Sahrens 
3846fa9e4066Sahrens 	/*
3847fa9e4066Sahrens 	 * Destroy the dataset.
3848fa9e4066Sahrens 	 */
38493b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
3850754998c8SChris Williamson 		VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
38513b2aab18SMatthew Ahrens 	} else {
3852754998c8SChris Williamson 		error = dsl_destroy_head(name);
3853754998c8SChris Williamson 		/* There could be a hold on this dataset */
3854754998c8SChris Williamson 		if (error != EBUSY)
3855754998c8SChris Williamson 			ASSERT0(error);
38563b2aab18SMatthew Ahrens 	}
38571d452cf5Sahrens 	return (0);
3858fa9e4066Sahrens }
3859fa9e4066Sahrens 
3860b24ab676SJeff Bonwick static boolean_t
3861b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3862fa9e4066Sahrens {
38639adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3864b24ab676SJeff Bonwick 	int error;
3865b24ab676SJeff Bonwick 
38663b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3867b24ab676SJeff Bonwick 
38683b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3869b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3870b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3871b24ab676SJeff Bonwick 		return (B_FALSE);
3872b24ab676SJeff Bonwick 	}
38733b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
38743b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
38753b2aab18SMatthew Ahrens 		    snapname, error);
38763b2aab18SMatthew Ahrens 	}
3877b24ab676SJeff Bonwick 	return (B_TRUE);
3878b24ab676SJeff Bonwick }
3879b24ab676SJeff Bonwick 
3880b24ab676SJeff Bonwick static boolean_t
3881b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3882b24ab676SJeff Bonwick {
38839adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3884b24ab676SJeff Bonwick 	int error;
3885b24ab676SJeff Bonwick 
38869adfa60dSMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3887b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3888b24ab676SJeff Bonwick 
38893b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3890b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3891b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3892b24ab676SJeff Bonwick 	return (B_TRUE);
3893fa9e4066Sahrens }
3894fa9e4066Sahrens 
3895b24ab676SJeff Bonwick /* ARGSUSED */
3896fa9e4066Sahrens void
3897b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3898fa9e4066Sahrens {
3899b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3900b24ab676SJeff Bonwick 	int iters;
3901fa9e4066Sahrens 	int error;
3902745cd3c5Smaybee 	objset_t *os, *os2;
39039adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3904fa9e4066Sahrens 	zilog_t *zilog;
3905fa9e4066Sahrens 
3906f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3907b24ab676SJeff Bonwick 
39089adfa60dSMatthew Ahrens 	(void) snprintf(name, sizeof (name), "%s/temp_%llu",
3909420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3910fa9e4066Sahrens 
3911fa9e4066Sahrens 	/*
3912fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3913fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3914b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3915fa9e4066Sahrens 	 */
3916fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3917eb633035STom Caputi 	    ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3918eb633035STom Caputi 	    B_TRUE, FTAG, &os) == 0) {
3919420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3920b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3921b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3922eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, FTAG);
3923fa9e4066Sahrens 	}
3924fa9e4066Sahrens 
3925fa9e4066Sahrens 	/*
3926fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3927fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3928fa9e4066Sahrens 	 * and all of its snapshots.
3929fa9e4066Sahrens 	 */
3930b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
39310b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3932fa9e4066Sahrens 
3933fa9e4066Sahrens 	/*
3934fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3935fa9e4066Sahrens 	 */
3936eb633035STom Caputi 	VERIFY3U(ENOENT, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3937eb633035STom Caputi 	    B_TRUE, FTAG, &os));
3938fa9e4066Sahrens 
3939fa9e4066Sahrens 	/*
3940fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3941fa9e4066Sahrens 	 */
394255da60b9SMark J Musante 	error = ztest_dataset_create(name);
3943fa9e4066Sahrens 	if (error) {
3944fa9e4066Sahrens 		if (error == ENOSPC) {
3945b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3946f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
3947fa9e4066Sahrens 			return;
3948fa9e4066Sahrens 		}
3949fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3950fa9e4066Sahrens 	}
3951fa9e4066Sahrens 
3952eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3953eb633035STom Caputi 	    FTAG, &os));
3954b24ab676SJeff Bonwick 
3955420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3956fa9e4066Sahrens 
3957fa9e4066Sahrens 	/*
3958fa9e4066Sahrens 	 * Open the intent log for it.
3959fa9e4066Sahrens 	 */
3960b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3961fa9e4066Sahrens 
3962fa9e4066Sahrens 	/*
3963b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3964b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3965fa9e4066Sahrens 	 */
3966b24ab676SJeff Bonwick 	iters = ztest_random(5);
3967b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3968b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3969b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3970b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3971fa9e4066Sahrens 	}
3972fa9e4066Sahrens 
3973fa9e4066Sahrens 	/*
3974fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3975fa9e4066Sahrens 	 */
3976b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3977eb633035STom Caputi 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
3978fa9e4066Sahrens 
3979fa9e4066Sahrens 	/*
3980503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3981745cd3c5Smaybee 	 */
3982b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3983503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3984503ad85cSMatthew Ahrens 
3985503ad85cSMatthew Ahrens 	/*
3986b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3987503ad85cSMatthew Ahrens 	 */
3988eb633035STom Caputi 	VERIFY3U(EBUSY, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER,
3989eb633035STom Caputi 	    B_FALSE, B_TRUE, FTAG, &os2));
3990fa9e4066Sahrens 
3991fa9e4066Sahrens 	zil_close(zilog);
3992eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
3993b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3994fa9e4066Sahrens 
3995f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
3996fa9e4066Sahrens }
3997fa9e4066Sahrens 
3998fa9e4066Sahrens /*
3999fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4000fa9e4066Sahrens  */
4001fa9e4066Sahrens void
4002b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
4003fa9e4066Sahrens {
4004f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
4005b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
4006b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
4007f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
40084f5064b7SMark J Musante }
40094f5064b7SMark J Musante 
4010718d718aSGeorge Wilson /*
4011718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
4012718d718aSGeorge Wilson  */
4013718d718aSGeorge Wilson void
4014b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
4015718d718aSGeorge Wilson {
40169adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
40179adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
40189adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
40199adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
40209adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
4021718d718aSGeorge Wilson 	int error;
4022718d718aSGeorge Wilson 
40239adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
40249adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
40259adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
40269adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
40279adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
40289adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
40299adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
40309adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
40319adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
40329adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
4033718d718aSGeorge Wilson 
40343b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
4035718d718aSGeorge Wilson 	if (error && error != ENOENT)
40363b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
40373b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
4038718d718aSGeorge Wilson 	if (error && error != ENOENT)
40393b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
40403b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
4041718d718aSGeorge Wilson 	if (error && error != ENOENT)
40423b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
40433b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
4044718d718aSGeorge Wilson 	if (error && error != ENOENT)
40453b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
40463b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
4047718d718aSGeorge Wilson 	if (error && error != ENOENT)
40483b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
4049718d718aSGeorge Wilson }
4050718d718aSGeorge Wilson 
40514f5064b7SMark J Musante /*
40524f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
40534f5064b7SMark J Musante  */
40544f5064b7SMark J Musante void
4055b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
40564f5064b7SMark J Musante {
40573b2aab18SMatthew Ahrens 	objset_t *os;
40589adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
40599adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
40609adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
40619adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
40629adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
4063b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
4064b24ab676SJeff Bonwick 	int error;
40654f5064b7SMark J Musante 
4066f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
40674f5064b7SMark J Musante 
4068b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
4069718d718aSGeorge Wilson 
40709adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
40719adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
40729adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
40739adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
40749adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
40759adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
40769adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
40779adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
40789adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
40799adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
408005312e2cSMark J Musante 
40814445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
4082b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4083b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4084b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4085b2d22b66SGeorge Wilson 			goto out;
4086b2d22b66SGeorge Wilson 		}
4087b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
4088b2d22b66SGeorge Wilson 	}
40894f5064b7SMark J Musante 
40903b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
4091b2d22b66SGeorge Wilson 	if (error) {
4092b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4093b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4094b2d22b66SGeorge Wilson 			goto out;
4095b2d22b66SGeorge Wilson 		}
4096b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
4097b2d22b66SGeorge Wilson 	}
40984f5064b7SMark J Musante 
40994445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4100b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4101b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4102b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4103b2d22b66SGeorge Wilson 			goto out;
4104b2d22b66SGeorge Wilson 		}
4105b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4106b2d22b66SGeorge Wilson 	}
41074f5064b7SMark J Musante 
41084445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4109b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4110b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4111b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4112b2d22b66SGeorge Wilson 			goto out;
4113b2d22b66SGeorge Wilson 		}
4114b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4115b2d22b66SGeorge Wilson 	}
41164f5064b7SMark J Musante 
41173b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
4118b2d22b66SGeorge Wilson 	if (error) {
4119b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4120b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4121b2d22b66SGeorge Wilson 			goto out;
4122b2d22b66SGeorge Wilson 		}
4123b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4124b2d22b66SGeorge Wilson 	}
41254f5064b7SMark J Musante 
4126eb633035STom Caputi 	error = ztest_dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
4127eb633035STom Caputi 	    FTAG, &os);
41284f5064b7SMark J Musante 	if (error)
41293b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4130681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
41315d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
4132eb633035STom Caputi 		dmu_objset_disown(os, B_TRUE, FTAG);
41335d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
41345d7b4d43SMatthew Ahrens 		goto out;
41355d7b4d43SMatthew Ahrens 	}
41364f5064b7SMark J Musante 	if (error != EBUSY)
41374f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
41384f5064b7SMark J Musante 		    error);
4139eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
41404f5064b7SMark J Musante 
4141b2d22b66SGeorge Wilson out:
4142b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
41434f5064b7SMark J Musante 
4144f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
4145fa9e4066Sahrens }
4146fa9e4066Sahrens 
4147fa9e4066Sahrens /*
4148fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
4149fa9e4066Sahrens  */
4150fa9e4066Sahrens void
4151b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4152fa9e4066Sahrens {
4153b24ab676SJeff Bonwick 	ztest_od_t od[4];
4154b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
4155fa9e4066Sahrens 
415654811da5SToomas Soome 	for (int b = 0; b < batchsize; b++) {
415754811da5SToomas Soome 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
415854811da5SToomas Soome 		    0, 0, 0);
415954811da5SToomas Soome 	}
4160fa9e4066Sahrens 
4161fa9e4066Sahrens 	/*
4162b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
4163b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
4164fa9e4066Sahrens 	 */
4165b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
4166b24ab676SJeff Bonwick 		return;
4167fa9e4066Sahrens 
4168b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
4169b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
4170b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4171fa9e4066Sahrens }
4172fa9e4066Sahrens 
417354811da5SToomas Soome /*
417454811da5SToomas Soome  * Rewind the global allocator to verify object allocation backfilling.
417554811da5SToomas Soome  */
417654811da5SToomas Soome void
417754811da5SToomas Soome ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
417854811da5SToomas Soome {
417954811da5SToomas Soome 	objset_t *os = zd->zd_os;
418054811da5SToomas Soome 	int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
418154811da5SToomas Soome 	uint64_t object;
418254811da5SToomas Soome 
418354811da5SToomas Soome 	/*
418454811da5SToomas Soome 	 * Rewind the global allocator randomly back to a lower object number
418554811da5SToomas Soome 	 * to force backfilling and reclamation of recently freed dnodes.
418654811da5SToomas Soome 	 */
418754811da5SToomas Soome 	mutex_enter(&os->os_obj_lock);
418854811da5SToomas Soome 	object = ztest_random(os->os_obj_next_chunk);
418954811da5SToomas Soome 	os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
419054811da5SToomas Soome 	mutex_exit(&os->os_obj_lock);
419154811da5SToomas Soome }
419254811da5SToomas Soome 
4193fa9e4066Sahrens /*
4194fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
4195fa9e4066Sahrens  */
4196fa9e4066Sahrens void
4197b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4198fa9e4066Sahrens {
4199b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4200b24ab676SJeff Bonwick 	ztest_od_t od[2];
4201fa9e4066Sahrens 	dmu_tx_t *tx;
4202fa9e4066Sahrens 	int i, freeit, error;
4203fa9e4066Sahrens 	uint64_t n, s, txg;
4204fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4205b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4206b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4207fa9e4066Sahrens 	uint64_t regions = 997;
4208fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
4209fa9e4066Sahrens 	uint64_t width = 40;
4210fa9e4066Sahrens 	int free_percent = 5;
4211fa9e4066Sahrens 
4212fa9e4066Sahrens 	/*
4213fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
4214fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
4215fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
4216fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
4217fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
4218fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
4219fa9e4066Sahrens 	 *
4220fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
4221fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
4222fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
4223fa9e4066Sahrens 	 *
4224fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
4225fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
4226fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
4227fa9e4066Sahrens 	 *
4228fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
4229fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
4230fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
4231fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
4232fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
4233fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
4234fa9e4066Sahrens 	 */
4235fa9e4066Sahrens 
4236fa9e4066Sahrens 	/*
4237fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
4238fa9e4066Sahrens 	 */
423954811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
424054811da5SToomas Soome 	    chunksize);
424154811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
424254811da5SToomas Soome 	    chunksize);
4243fa9e4066Sahrens 
4244b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4245b24ab676SJeff Bonwick 		return;
4246fa9e4066Sahrens 
4247b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4248b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4249b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
4250b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
4251fa9e4066Sahrens 
4252fa9e4066Sahrens 	/*
4253fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
4254fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
4255fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
4256fa9e4066Sahrens 	 * handle this race correctly.
4257fa9e4066Sahrens 	 */
4258fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4259fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
4260a2cdcdd2SPaul Dagnelie 	dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4261a2cdcdd2SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_READ);
4262fa9e4066Sahrens 
4263fa9e4066Sahrens 	/*
4264fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
4265fa9e4066Sahrens 	 */
4266fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4267fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
4268fa9e4066Sahrens 
4269fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
4270fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
4271fa9e4066Sahrens 
4272b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4273b24ab676SJeff Bonwick 	bigsize = s * chunksize;
4274fa9e4066Sahrens 
4275fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4276fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4277fa9e4066Sahrens 
4278fa9e4066Sahrens 	/*
4279fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
4280fa9e4066Sahrens 	 * overwriting it.
4281fa9e4066Sahrens 	 */
4282fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
4283fa9e4066Sahrens 
4284fa9e4066Sahrens 	/*
4285fa9e4066Sahrens 	 * Read the current contents of our objects.
4286fa9e4066Sahrens 	 */
4287b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
42887bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4289fb09f5aaSMadhav Suresh 	ASSERT0(error);
4290b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
42917bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4292fb09f5aaSMadhav Suresh 	ASSERT0(error);
4293fa9e4066Sahrens 
4294fa9e4066Sahrens 	/*
4295fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
4296fa9e4066Sahrens 	 */
4297fa9e4066Sahrens 	tx = dmu_tx_create(os);
4298fa9e4066Sahrens 
4299b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
4300fa9e4066Sahrens 
4301fa9e4066Sahrens 	if (freeit)
4302b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4303fa9e4066Sahrens 	else
4304b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4305fa9e4066Sahrens 
4306be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
4307be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
4308be9000ccSMatthew Ahrens 
4309b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4310b24ab676SJeff Bonwick 	if (txg == 0) {
4311fa9e4066Sahrens 		umem_free(packbuf, packsize);
4312fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
4313fa9e4066Sahrens 		return;
4314fa9e4066Sahrens 	}
4315fa9e4066Sahrens 
43165d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
43175d7b4d43SMatthew Ahrens 	do {
43185d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
43195d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
43205d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
43215d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
4322b24ab676SJeff Bonwick 
43235d7b4d43SMatthew Ahrens 	enum zio_compress comp;
43245d7b4d43SMatthew Ahrens 	do {
43255d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
43265d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
43275d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
43285d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
4329fa9e4066Sahrens 
4330fa9e4066Sahrens 	/*
4331fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
4332fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
4333fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
4334fa9e4066Sahrens 	 * with the new values we want to write out.
4335fa9e4066Sahrens 	 */
4336fa9e4066Sahrens 	for (i = 0; i < s; i++) {
4337fa9e4066Sahrens 		/* LINTED */
4338fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4339fa9e4066Sahrens 		/* LINTED */
4340b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4341fa9e4066Sahrens 		/* LINTED */
4342b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4343fa9e4066Sahrens 
4344fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4345fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4346fa9e4066Sahrens 
4347fa9e4066Sahrens 		if (pack->bw_txg > txg)
4348fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
4349fa9e4066Sahrens 			    pack->bw_txg, txg);
4350fa9e4066Sahrens 
4351fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
4352fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4353fa9e4066Sahrens 			    pack->bw_index, n, i);
4354fa9e4066Sahrens 
4355fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4356fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4357fa9e4066Sahrens 
4358fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4359fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4360fa9e4066Sahrens 
4361fa9e4066Sahrens 		if (freeit) {
4362fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
4363fa9e4066Sahrens 		} else {
4364fa9e4066Sahrens 			pack->bw_index = n + i;
4365fa9e4066Sahrens 			pack->bw_txg = txg;
4366fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
4367fa9e4066Sahrens 		}
4368fa9e4066Sahrens 		*bigH = *pack;
4369fa9e4066Sahrens 		*bigT = *pack;
4370fa9e4066Sahrens 	}
4371fa9e4066Sahrens 
4372fa9e4066Sahrens 	/*
4373fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
4374fa9e4066Sahrens 	 * Now write them out.
4375fa9e4066Sahrens 	 */
4376b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4377fa9e4066Sahrens 
4378fa9e4066Sahrens 	if (freeit) {
4379420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4380fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
4381fa9e4066Sahrens 			    " txg %llx\n",
4382fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4383fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4384fa9e4066Sahrens 			    (u_longlong_t)txg);
4385fa9e4066Sahrens 		}
4386b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4387fa9e4066Sahrens 	} else {
4388420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4389fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
4390fa9e4066Sahrens 			    " txg %llx\n",
4391fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4392fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4393fa9e4066Sahrens 			    (u_longlong_t)txg);
4394fa9e4066Sahrens 		}
4395b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4396fa9e4066Sahrens 	}
4397fa9e4066Sahrens 
4398fa9e4066Sahrens 	dmu_tx_commit(tx);
4399fa9e4066Sahrens 
4400fa9e4066Sahrens 	/*
4401fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
4402fa9e4066Sahrens 	 */
4403fa9e4066Sahrens 	{
4404fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4405fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4406fa9e4066Sahrens 
4407b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
44087bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
4409b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
44107bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
4411fa9e4066Sahrens 
4412fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4413fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4414fa9e4066Sahrens 
4415fa9e4066Sahrens 		umem_free(packcheck, packsize);
4416fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
4417fa9e4066Sahrens 	}
4418fa9e4066Sahrens 
4419fa9e4066Sahrens 	umem_free(packbuf, packsize);
4420fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
4421fa9e4066Sahrens }
4422fa9e4066Sahrens 
44232fdbea25SAleksandr Guzovskiy void
44242fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4425b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
44262fdbea25SAleksandr Guzovskiy {
44272fdbea25SAleksandr Guzovskiy 	uint64_t i;
44282fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
44292fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
44302fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
44312fdbea25SAleksandr Guzovskiy 
44322fdbea25SAleksandr Guzovskiy 	/*
44332fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
44342fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
44352fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
44362fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
44372fdbea25SAleksandr Guzovskiy 	 */
44382fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
44392fdbea25SAleksandr Guzovskiy 		/* LINTED */
44402fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
44412fdbea25SAleksandr Guzovskiy 		/* LINTED */
4442b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
44432fdbea25SAleksandr Guzovskiy 		/* LINTED */
4444b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
44452fdbea25SAleksandr Guzovskiy 
44462fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
44472fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
44482fdbea25SAleksandr Guzovskiy 
44492fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
44502fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
44512fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
44522fdbea25SAleksandr Guzovskiy 
44532fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
44542fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
44552fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
44562fdbea25SAleksandr Guzovskiy 
44572fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
44582fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
44592fdbea25SAleksandr Guzovskiy 
44602fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
44612fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
44622fdbea25SAleksandr Guzovskiy 
44632fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
44642fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
44652fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
44662fdbea25SAleksandr Guzovskiy 
44672fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
44682fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
44692fdbea25SAleksandr Guzovskiy 	}
44702fdbea25SAleksandr Guzovskiy }
44712fdbea25SAleksandr Guzovskiy 
44722fdbea25SAleksandr Guzovskiy void
4473b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
44742fdbea25SAleksandr Guzovskiy {
4475b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4476b24ab676SJeff Bonwick 	ztest_od_t od[2];
44772fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
44782fdbea25SAleksandr Guzovskiy 	uint64_t i;
44792fdbea25SAleksandr Guzovskiy 	int error;
44802fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
44812fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
4482b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4483b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4484b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
44852fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
44862fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
44872fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
44882fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
44892fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
4490b24ab676SJeff Bonwick 	dmu_object_info_t doi;
44912fdbea25SAleksandr Guzovskiy 
44922fdbea25SAleksandr Guzovskiy 	/*
44932fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
44942fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
44952fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
44962fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
44972fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
44982fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
44992fdbea25SAleksandr Guzovskiy 	 *
45002fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
45012fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
45022fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
45032fdbea25SAleksandr Guzovskiy 	 *
45042fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
4505eb633035STom Caputi 	 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
45062fdbea25SAleksandr Guzovskiy 	 */
45072fdbea25SAleksandr Guzovskiy 
45082fdbea25SAleksandr Guzovskiy 	/*
45092fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
45102fdbea25SAleksandr Guzovskiy 	 */
451154811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
451254811da5SToomas Soome 	    0, 0);
451354811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
451454811da5SToomas Soome 	    chunksize);
45152fdbea25SAleksandr Guzovskiy 
4516b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4517b24ab676SJeff Bonwick 		return;
45182fdbea25SAleksandr Guzovskiy 
4519b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4520b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4521b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
4522b24ab676SJeff Bonwick 	chunksize = blocksize;
4523b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
45242fdbea25SAleksandr Guzovskiy 
4525b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4526b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
4527b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
4528b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
45292fdbea25SAleksandr Guzovskiy 
45302fdbea25SAleksandr Guzovskiy 	/*
45312fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
45322fdbea25SAleksandr Guzovskiy 	 */
45332fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
45342fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
45352fdbea25SAleksandr Guzovskiy 
45362fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
45372fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
45382fdbea25SAleksandr Guzovskiy 
4539b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4540b24ab676SJeff Bonwick 	bigsize = s * chunksize;
45412fdbea25SAleksandr Guzovskiy 
45422fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
45432fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
45442fdbea25SAleksandr Guzovskiy 
4545b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
45462fdbea25SAleksandr Guzovskiy 
45472fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
45482fdbea25SAleksandr Guzovskiy 
45492fdbea25SAleksandr Guzovskiy 	/*
45502fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
45512fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
45522fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
45532fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
45542fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
45552fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
45562fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
45572fdbea25SAleksandr Guzovskiy 	 */
45582fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
45592fdbea25SAleksandr Guzovskiy 		uint64_t j;
45602fdbea25SAleksandr Guzovskiy 		uint64_t off;
45612fdbea25SAleksandr Guzovskiy 
45622fdbea25SAleksandr Guzovskiy 		/*
45632fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
45642fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
4565eb633035STom Caputi 		 * dmu_assign_arcbuf_by_dbuf() when it can't directly
45662fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
45672fdbea25SAleksandr Guzovskiy 		 */
45682fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
456989c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
45702fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
4571b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
45722fdbea25SAleksandr Guzovskiy 			} else {
45732fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
4574b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
45752fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
4576b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
45772fdbea25SAleksandr Guzovskiy 			}
45782fdbea25SAleksandr Guzovskiy 		}
45792fdbea25SAleksandr Guzovskiy 
45802fdbea25SAleksandr Guzovskiy 		/*
45812fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
45822fdbea25SAleksandr Guzovskiy 		 */
45832fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
45842fdbea25SAleksandr Guzovskiy 
4585b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
4586b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
45872fdbea25SAleksandr Guzovskiy 
4588b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4589b24ab676SJeff Bonwick 		if (txg == 0) {
45902fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
45912fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
45922fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
459389c86e32SChris Williamson 				if (i != 5 ||
459489c86e32SChris Williamson 				    chunksize < (SPA_MINBLOCKSIZE * 2)) {
45952fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
45962fdbea25SAleksandr Guzovskiy 				} else {
45972fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
45982fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
45992fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
46002fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
46012fdbea25SAleksandr Guzovskiy 				}
46022fdbea25SAleksandr Guzovskiy 			}
46032fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
46042fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
46052fdbea25SAleksandr Guzovskiy 			return;
46062fdbea25SAleksandr Guzovskiy 		}
46072fdbea25SAleksandr Guzovskiy 
46082fdbea25SAleksandr Guzovskiy 		/*
46092fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
4610eb633035STom Caputi 		 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4611eb633035STom Caputi 		 * no existing dbufs for the specified offsets.
46122fdbea25SAleksandr Guzovskiy 		 */
46132fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
4614b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
46157bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
4616fb09f5aaSMadhav Suresh 			ASSERT0(error);
4617b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
46187bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
4619fb09f5aaSMadhav Suresh 			ASSERT0(error);
46202fdbea25SAleksandr Guzovskiy 		}
46212fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4622b24ab676SJeff Bonwick 		    n, chunksize, txg);
46232fdbea25SAleksandr Guzovskiy 
46242fdbea25SAleksandr Guzovskiy 		/*
46252fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
46262fdbea25SAleksandr Guzovskiy 		 * Now write them out.
46272fdbea25SAleksandr Guzovskiy 		 */
4628b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4629420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
46302fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
46312fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
46322fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
46332fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
46342fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
46352fdbea25SAleksandr Guzovskiy 		}
4636b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
46372fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
463889c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
46392fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
4640b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
46412fdbea25SAleksandr Guzovskiy 			} else {
46422fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
46432fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
4644b24ab676SJeff Bonwick 				    chunksize / 2);
46452fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
4646b24ab676SJeff Bonwick 				    chunksize / 2,
46472fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
4648b24ab676SJeff Bonwick 				    chunksize / 2);
46492fdbea25SAleksandr Guzovskiy 			}
46502fdbea25SAleksandr Guzovskiy 
46512fdbea25SAleksandr Guzovskiy 			if (i == 1) {
4652b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
465347cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
46542fdbea25SAleksandr Guzovskiy 			}
465589c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4656eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db, off,
46572fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
46582fdbea25SAleksandr Guzovskiy 			} else {
4659eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db, off,
46602fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
4661eb633035STom Caputi 				dmu_assign_arcbuf_by_dbuf(bonus_db,
4662b24ab676SJeff Bonwick 				    off + chunksize / 2,
46632fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
46642fdbea25SAleksandr Guzovskiy 			}
46652fdbea25SAleksandr Guzovskiy 			if (i == 1) {
46662fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
46672fdbea25SAleksandr Guzovskiy 			}
46682fdbea25SAleksandr Guzovskiy 		}
46692fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
46702fdbea25SAleksandr Guzovskiy 
46712fdbea25SAleksandr Guzovskiy 		/*
46722fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
46732fdbea25SAleksandr Guzovskiy 		 */
46742fdbea25SAleksandr Guzovskiy 		{
46752fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
46762fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
46772fdbea25SAleksandr Guzovskiy 
4678b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
46797bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
4680b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
46817bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
46822fdbea25SAleksandr Guzovskiy 
46832fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
46842fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
46852fdbea25SAleksandr Guzovskiy 
46862fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
46872fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
46882fdbea25SAleksandr Guzovskiy 		}
46892fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4690*084fd14fSBrian Behlendorf 			txg_wait_open(dmu_objset_pool(os), 0, B_TRUE);
4691b24ab676SJeff Bonwick 		} else if (i == 3) {
4692b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4693fa9e4066Sahrens 		}
4694e05725b1Sbonwick 	}
4695e05725b1Sbonwick 
4696b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4697b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4698b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4699b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4700b24ab676SJeff Bonwick }
4701fa9e4066Sahrens 
4702b24ab676SJeff Bonwick /* ARGSUSED */
4703b24ab676SJeff Bonwick void
4704b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4705b24ab676SJeff Bonwick {
4706b24ab676SJeff Bonwick 	ztest_od_t od[1];
4707b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4708b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4709fa9e4066Sahrens 
4710e05725b1Sbonwick 	/*
4711b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4712b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4713b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4714e05725b1Sbonwick 	 */
471554811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
471654811da5SToomas Soome 	    0, 0, 0);
4717fa9e4066Sahrens 
4718b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4719e05725b1Sbonwick 		return;
4720fa9e4066Sahrens 
4721b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4722b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4723b24ab676SJeff Bonwick }
4724fa9e4066Sahrens 
4725b24ab676SJeff Bonwick void
4726b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4727b24ab676SJeff Bonwick {
4728b24ab676SJeff Bonwick 	ztest_od_t od[1];
4729b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4730b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4731b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4732b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4733b24ab676SJeff Bonwick 	void *data;
4734fa9e4066Sahrens 
473554811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
473654811da5SToomas Soome 	    0, 0);
4737fa9e4066Sahrens 
4738b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4739e05725b1Sbonwick 		return;
47400e34b6a7Sbonwick 
4741b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4742f4a72450SJeff Bonwick 		return;
4743f4a72450SJeff Bonwick 
4744b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4745fa9e4066Sahrens 
4746b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4747b24ab676SJeff Bonwick 
4748b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4749b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4750b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4751b24ab676SJeff Bonwick 		    data) != 0)
4752b24ab676SJeff Bonwick 			break;
4753b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4754b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4755b24ab676SJeff Bonwick 	}
4756b24ab676SJeff Bonwick 
4757b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4758fa9e4066Sahrens }
4759fa9e4066Sahrens 
4760fa9e4066Sahrens /*
4761fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4762fa9e4066Sahrens  */
4763fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4764fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4765fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4766fa9e4066Sahrens 
4767fa9e4066Sahrens void
4768b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4769fa9e4066Sahrens {
4770b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4771b24ab676SJeff Bonwick 	ztest_od_t od[1];
4772fa9e4066Sahrens 	uint64_t object;
4773fa9e4066Sahrens 	uint64_t txg, last_txg;
4774fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4775fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4776fa9e4066Sahrens 	int i, ints;
4777fa9e4066Sahrens 	dmu_tx_t *tx;
4778fa9e4066Sahrens 	char propname[100], txgname[100];
4779fa9e4066Sahrens 	int error;
4780fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4781fa9e4066Sahrens 
478254811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4783fa9e4066Sahrens 
4784b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4785b24ab676SJeff Bonwick 		return;
4786fa9e4066Sahrens 
4787b24ab676SJeff Bonwick 	object = od[0].od_object;
4788fa9e4066Sahrens 
4789b24ab676SJeff Bonwick 	/*
4790b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4791b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4792b24ab676SJeff Bonwick 	 */
4793b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4794b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4795b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4796b24ab676SJeff Bonwick 	if (txg == 0)
4797b24ab676SJeff Bonwick 		return;
4798b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4799b24ab676SJeff Bonwick 		value[i] = i;
4800b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4801b24ab676SJeff Bonwick 		    1, &value[i], tx));
4802b24ab676SJeff Bonwick 	}
4803b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4804b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4805b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4806b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4807b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4808b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4809b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4810fa9e4066Sahrens 	}
4811b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4812b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4813b24ab676SJeff Bonwick 	}
4814b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4815fa9e4066Sahrens 
4816b24ab676SJeff Bonwick 	/*
4817b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4818b24ab676SJeff Bonwick 	 */
4819fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4820fa9e4066Sahrens 
4821e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4822e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4823e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4824e05725b1Sbonwick 	bzero(value, sizeof (value));
4825e05725b1Sbonwick 	last_txg = 0;
4826fa9e4066Sahrens 
4827e05725b1Sbonwick 	/*
4828e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4829e05725b1Sbonwick 	 */
4830e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4831e05725b1Sbonwick 	if (error == 0) {
4832e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4833e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4834fa9e4066Sahrens 
4835e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4836e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4837fa9e4066Sahrens 
4838e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4839e05725b1Sbonwick 		    &zl_ints) == 0);
4840fa9e4066Sahrens 
4841e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4842e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4843fa9e4066Sahrens 
4844e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4845e05725b1Sbonwick 		    zl_ints, value) == 0);
4846fa9e4066Sahrens 
4847e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4848e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4849fa9e4066Sahrens 		}
4850e05725b1Sbonwick 	} else {
4851e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4852e05725b1Sbonwick 	}
4853fa9e4066Sahrens 
4854e05725b1Sbonwick 	/*
4855e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4856e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4857e05725b1Sbonwick 	 * in which the property was last updated.  The second
4858e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4859e05725b1Sbonwick 	 * should be txg + object + n.
4860e05725b1Sbonwick 	 */
4861e05725b1Sbonwick 	tx = dmu_tx_create(os);
4862b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4863b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4864b24ab676SJeff Bonwick 	if (txg == 0)
4865e05725b1Sbonwick 		return;
4866fa9e4066Sahrens 
4867e05725b1Sbonwick 	if (last_txg > txg)
4868e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4869fa9e4066Sahrens 
4870e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4871e05725b1Sbonwick 		value[i] = txg + object + i;
4872fa9e4066Sahrens 
4873b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4874b24ab676SJeff Bonwick 	    1, &txg, tx));
4875b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4876b24ab676SJeff Bonwick 	    ints, value, tx));
4877fa9e4066Sahrens 
4878e05725b1Sbonwick 	dmu_tx_commit(tx);
4879fa9e4066Sahrens 
4880e05725b1Sbonwick 	/*
4881e05725b1Sbonwick 	 * Remove a random pair of entries.
4882e05725b1Sbonwick 	 */
4883e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4884e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4885e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4886fa9e4066Sahrens 
4887e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4888fa9e4066Sahrens 
4889e05725b1Sbonwick 	if (error == ENOENT)
4890e05725b1Sbonwick 		return;
4891fa9e4066Sahrens 
4892fb09f5aaSMadhav Suresh 	ASSERT0(error);
4893fa9e4066Sahrens 
4894e05725b1Sbonwick 	tx = dmu_tx_create(os);
4895b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4896b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4897b24ab676SJeff Bonwick 	if (txg == 0)
489812a2833aSSanjeev Bagewadi 		return;
4899b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4900b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
490112a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
490212a2833aSSanjeev Bagewadi }
490312a2833aSSanjeev Bagewadi 
490412a2833aSSanjeev Bagewadi /*
490512a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
490612a2833aSSanjeev Bagewadi  */
490712a2833aSSanjeev Bagewadi void
4908b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
490912a2833aSSanjeev Bagewadi {
4910b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4911b24ab676SJeff Bonwick 	ztest_od_t od[1];
4912b24ab676SJeff Bonwick 	uint64_t object, txg;
491312a2833aSSanjeev Bagewadi 
491454811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
491512a2833aSSanjeev Bagewadi 
4916b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4917b24ab676SJeff Bonwick 		return;
491812a2833aSSanjeev Bagewadi 
4919b24ab676SJeff Bonwick 	object = od[0].od_object;
492012a2833aSSanjeev Bagewadi 
492112a2833aSSanjeev Bagewadi 	/*
4922b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
492312a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4924b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
492512a2833aSSanjeev Bagewadi 	 */
4926b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
49279adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
4928b24ab676SJeff Bonwick 		uint64_t value = i;
4929b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4930b24ab676SJeff Bonwick 		int error;
493112a2833aSSanjeev Bagewadi 
4932b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4933b24ab676SJeff Bonwick 		    id, value);
493412a2833aSSanjeev Bagewadi 
4935b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4936b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4937b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4938b24ab676SJeff Bonwick 		if (txg == 0)
493912a2833aSSanjeev Bagewadi 			return;
4940b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4941b24ab676SJeff Bonwick 		    &value, tx);
494212a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
494312a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
494412a2833aSSanjeev Bagewadi 	}
4945fa9e4066Sahrens }
4946fa9e4066Sahrens 
4947b24ab676SJeff Bonwick /* ARGSUSED */
4948fa9e4066Sahrens void
4949b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4950fa9e4066Sahrens {
4951b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4952b24ab676SJeff Bonwick 	ztest_od_t od[1];
4953fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4954fa9e4066Sahrens 	dmu_tx_t *tx;
4955fa9e4066Sahrens 	int i, namelen, error;
4956b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4957fa9e4066Sahrens 	char name[20], string_value[20];
4958fa9e4066Sahrens 	void *data;
4959fa9e4066Sahrens 
496054811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
496154811da5SToomas Soome 	    0, 0, 0);
4962b24ab676SJeff Bonwick 
4963b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4964b24ab676SJeff Bonwick 		return;
4965b24ab676SJeff Bonwick 
4966b24ab676SJeff Bonwick 	object = od[0].od_object;
4967b24ab676SJeff Bonwick 
4968e05725b1Sbonwick 	/*
4969e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4970e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4971e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4972e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4973e05725b1Sbonwick 	 */
4974e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4975fa9e4066Sahrens 
4976e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4977e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4978e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4979e05725b1Sbonwick 		name[i] = '.';
4980e05725b1Sbonwick 	name[i] = '\0';
4981fa9e4066Sahrens 
4982b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4983e05725b1Sbonwick 		wsize = sizeof (txg);
4984e05725b1Sbonwick 		wc = 1;
4985e05725b1Sbonwick 		data = &txg;
4986e05725b1Sbonwick 	} else {
4987e05725b1Sbonwick 		wsize = 1;
4988e05725b1Sbonwick 		wc = namelen;
4989e05725b1Sbonwick 		data = string_value;
4990e05725b1Sbonwick 	}
4991fa9e4066Sahrens 
4992e05725b1Sbonwick 	count = -1ULL;
49933b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4994e05725b1Sbonwick 	ASSERT(count != -1ULL);
4995fa9e4066Sahrens 
4996e05725b1Sbonwick 	/*
4997e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4998e05725b1Sbonwick 	 */
4999e05725b1Sbonwick 	i = ztest_random(5);
5000e05725b1Sbonwick 
5001e05725b1Sbonwick 	if (i >= 2) {
5002e05725b1Sbonwick 		tx = dmu_tx_create(os);
5003b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
5004b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5005b24ab676SJeff Bonwick 		if (txg == 0)
5006e05725b1Sbonwick 			return;
5007e05725b1Sbonwick 		bcopy(name, string_value, namelen);
5008e05725b1Sbonwick 	} else {
5009e05725b1Sbonwick 		tx = NULL;
5010e05725b1Sbonwick 		txg = 0;
5011e05725b1Sbonwick 		bzero(string_value, namelen);
5012e05725b1Sbonwick 	}
5013fa9e4066Sahrens 
5014e05725b1Sbonwick 	switch (i) {
5015fa9e4066Sahrens 
5016e05725b1Sbonwick 	case 0:
5017e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
5018e05725b1Sbonwick 		if (error == 0) {
5019e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
5020e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
5021e05725b1Sbonwick 		} else {
5022e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
5023e05725b1Sbonwick 		}
5024e05725b1Sbonwick 		break;
5025fa9e4066Sahrens 
5026e05725b1Sbonwick 	case 1:
5027e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
5028e05725b1Sbonwick 		if (error == 0) {
5029e05725b1Sbonwick 			if (data == string_value &&
5030e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
5031e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
5032e05725b1Sbonwick 				    name, data, namelen);
5033e05725b1Sbonwick 		} else {
5034e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
5035e05725b1Sbonwick 		}
5036e05725b1Sbonwick 		break;
5037fa9e4066Sahrens 
5038e05725b1Sbonwick 	case 2:
5039e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
5040e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
5041e05725b1Sbonwick 		break;
5042fa9e4066Sahrens 
5043e05725b1Sbonwick 	case 3:
5044e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
5045e05725b1Sbonwick 		break;
5046fa9e4066Sahrens 
5047e05725b1Sbonwick 	case 4:
5048e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
5049e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
5050e05725b1Sbonwick 		break;
5051fa9e4066Sahrens 	}
5052e05725b1Sbonwick 
5053e05725b1Sbonwick 	if (tx != NULL)
5054e05725b1Sbonwick 		dmu_tx_commit(tx);
5055fa9e4066Sahrens }
5056fa9e4066Sahrens 
5057d20e665cSRicardo M. Correia /*
5058d20e665cSRicardo M. Correia  * Commit callback data.
5059d20e665cSRicardo M. Correia  */
5060d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
5061d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
5062d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
5063d20e665cSRicardo M. Correia 	int			zcd_expected_err;
5064d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
5065d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
5066d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
5067d20e665cSRicardo M. Correia } ztest_cb_data_t;
5068d20e665cSRicardo M. Correia 
5069d20e665cSRicardo M. Correia /* This is the actual commit callback function */
5070d20e665cSRicardo M. Correia static void
5071d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
5072d20e665cSRicardo M. Correia {
5073d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
5074d20e665cSRicardo M. Correia 	uint64_t synced_txg;
5075d20e665cSRicardo M. Correia 
5076d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
5077d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
5078d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
5079d20e665cSRicardo M. Correia 
5080d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
5081d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
5082d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
5083d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
5084d20e665cSRicardo M. Correia 		    synced_txg);
5085d20e665cSRicardo M. Correia 
5086d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
5087d20e665cSRicardo M. Correia 
5088d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
5089fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
5090d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
5091d20e665cSRicardo M. Correia 
5092d20e665cSRicardo M. Correia 		/*
5093d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
5094d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
5095d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
5096d20e665cSRicardo M. Correia 		 */
5097d20e665cSRicardo M. Correia 		return;
5098d20e665cSRicardo M. Correia 	}
5099d20e665cSRicardo M. Correia 
5100d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
5101d20e665cSRicardo M. Correia 	if (!data->zcd_added)
5102d20e665cSRicardo M. Correia 		goto out;
5103d20e665cSRicardo M. Correia 
5104d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
5105d20e665cSRicardo M. Correia 
5106d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
5107f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5108d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
5109f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5110d20e665cSRicardo M. Correia 
5111d20e665cSRicardo M. Correia out:
5112d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
5113d20e665cSRicardo M. Correia }
5114d20e665cSRicardo M. Correia 
5115d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
5116d20e665cSRicardo M. Correia static ztest_cb_data_t *
5117d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
5118d20e665cSRicardo M. Correia {
5119d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
5120d20e665cSRicardo M. Correia 
5121d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5122d20e665cSRicardo M. Correia 
5123d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
5124d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
5125d20e665cSRicardo M. Correia 
5126d20e665cSRicardo M. Correia 	return (cb_data);
5127d20e665cSRicardo M. Correia }
5128d20e665cSRicardo M. Correia 
5129d20e665cSRicardo M. Correia /*
5130d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
5131d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
5132d20e665cSRicardo M. Correia  * implementation bug.
5133d20e665cSRicardo M. Correia  */
5134d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
5135d20e665cSRicardo M. Correia 
5136d20e665cSRicardo M. Correia /*
5137d20e665cSRicardo M. Correia  * Commit callback test.
5138d20e665cSRicardo M. Correia  */
5139d20e665cSRicardo M. Correia void
5140b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5141d20e665cSRicardo M. Correia {
5142b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5143b24ab676SJeff Bonwick 	ztest_od_t od[1];
5144d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
5145d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
5146d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
5147d20e665cSRicardo M. Correia 	int i, error;
5148d20e665cSRicardo M. Correia 
514954811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5150b24ab676SJeff Bonwick 
5151b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5152b24ab676SJeff Bonwick 		return;
5153b24ab676SJeff Bonwick 
5154d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
5155d20e665cSRicardo M. Correia 
5156d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
5157d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5158d20e665cSRicardo M. Correia 
5159b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
5160d20e665cSRicardo M. Correia 
5161d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
5162d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
5163d20e665cSRicardo M. Correia 		error = -1;
5164d20e665cSRicardo M. Correia 
5165d20e665cSRicardo M. Correia 	if (!error)
5166d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
5167d20e665cSRicardo M. Correia 
5168d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
5169d20e665cSRicardo M. Correia 
5170d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
5171d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
5172d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5173d20e665cSRicardo M. Correia 
5174d20e665cSRicardo M. Correia 	if (error) {
5175d20e665cSRicardo M. Correia 		/*
5176d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
5177d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
5178d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
5179d20e665cSRicardo M. Correia 		 * so we will check for that.
5180d20e665cSRicardo M. Correia 		 */
5181d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5182d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
5183d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
5184d20e665cSRicardo M. Correia 		}
5185d20e665cSRicardo M. Correia 
5186d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
5187d20e665cSRicardo M. Correia 
5188d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5189d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
5190d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5191d20e665cSRicardo M. Correia 		}
5192d20e665cSRicardo M. Correia 
5193d20e665cSRicardo M. Correia 		return;
5194d20e665cSRicardo M. Correia 	}
5195d20e665cSRicardo M. Correia 
5196d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
5197d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5198d20e665cSRicardo M. Correia 
5199d20e665cSRicardo M. Correia 	/*
5200d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
5201d20e665cSRicardo M. Correia 	 */
5202b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
5203d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
5204d20e665cSRicardo M. Correia 
5205d20e665cSRicardo M. Correia 	if (old_txg > txg)
5206d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5207d20e665cSRicardo M. Correia 		    old_txg, txg);
5208d20e665cSRicardo M. Correia 
5209b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
5210d20e665cSRicardo M. Correia 
5211f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5212d20e665cSRicardo M. Correia 
5213d20e665cSRicardo M. Correia 	/*
5214d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
5215d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
5216d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
5217d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
5218d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
5219d20e665cSRicardo M. Correia 	 * implementation.
5220d20e665cSRicardo M. Correia 	 *
5221d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
5222d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
5223d20e665cSRicardo M. Correia 	 * implementation bug..
5224d20e665cSRicardo M. Correia 	 */
5225d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
5226d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
5227b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
5228d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5229d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5230d20e665cSRicardo M. Correia 	}
5231d20e665cSRicardo M. Correia 
5232d20e665cSRicardo M. Correia 	/*
5233d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
5234d20e665cSRicardo M. Correia 	 *
5235d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
5236d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
5237d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
5238d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
5239d20e665cSRicardo M. Correia 	 */
5240d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
5241d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5242d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5243d20e665cSRicardo M. Correia 
5244d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
5245d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
5246d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
5247d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5248d20e665cSRicardo M. Correia 		else
5249d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5250d20e665cSRicardo M. Correia 			    cb_data[i]);
5251d20e665cSRicardo M. Correia 
5252d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
5253d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
5254d20e665cSRicardo M. Correia 
5255d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
5256d20e665cSRicardo M. Correia 	}
5257d20e665cSRicardo M. Correia 
5258f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5259d20e665cSRicardo M. Correia 
5260d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
5261d20e665cSRicardo M. Correia }
5262d20e665cSRicardo M. Correia 
526354811da5SToomas Soome /*
526454811da5SToomas Soome  * Visit each object in the dataset. Verify that its properties
526554811da5SToomas Soome  * are consistent what was stored in the block tag when it was created,
526654811da5SToomas Soome  * and that its unused bonus buffer space has not been overwritten.
526754811da5SToomas Soome  */
526854811da5SToomas Soome void
526954811da5SToomas Soome ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
527054811da5SToomas Soome {
527154811da5SToomas Soome 	objset_t *os = zd->zd_os;
527254811da5SToomas Soome 	uint64_t obj;
527354811da5SToomas Soome 	int err = 0;
527454811da5SToomas Soome 
527554811da5SToomas Soome 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
527654811da5SToomas Soome 		ztest_block_tag_t *bt = NULL;
527754811da5SToomas Soome 		dmu_object_info_t doi;
527854811da5SToomas Soome 		dmu_buf_t *db;
527954811da5SToomas Soome 
528054811da5SToomas Soome 		if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
528154811da5SToomas Soome 			continue;
528254811da5SToomas Soome 
528354811da5SToomas Soome 		dmu_object_info_from_db(db, &doi);
528454811da5SToomas Soome 		if (doi.doi_bonus_size >= sizeof (*bt))
528554811da5SToomas Soome 			bt = ztest_bt_bonus(db);
528654811da5SToomas Soome 
528754811da5SToomas Soome 		if (bt && bt->bt_magic == BT_MAGIC) {
528854811da5SToomas Soome 			ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
528954811da5SToomas Soome 			    bt->bt_offset, bt->bt_gen, bt->bt_txg,
529054811da5SToomas Soome 			    bt->bt_crtxg);
529154811da5SToomas Soome 			ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
529254811da5SToomas Soome 		}
529354811da5SToomas Soome 
529454811da5SToomas Soome 		dmu_buf_rele(db, FTAG);
529554811da5SToomas Soome 	}
529654811da5SToomas Soome }
529754811da5SToomas Soome 
5298b24ab676SJeff Bonwick /* ARGSUSED */
5299fa9e4066Sahrens void
5300b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5301fa9e4066Sahrens {
5302b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
5303b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
5304b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
5305b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
5306b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
5307b24ab676SJeff Bonwick 	};
5308fa9e4066Sahrens 
5309f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5310fa9e4066Sahrens 
5311b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5312b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5313b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5314fa9e4066Sahrens 
5315f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5316b24ab676SJeff Bonwick }
5317fa9e4066Sahrens 
53185cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
53195cabbc6bSPrashanth Sreenivasa void
53205cabbc6bSPrashanth Sreenivasa ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
53215cabbc6bSPrashanth Sreenivasa {
5322f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
53235cabbc6bSPrashanth Sreenivasa 
53245cabbc6bSPrashanth Sreenivasa 	int error = dmu_objset_remap_indirects(zd->zd_name);
53255cabbc6bSPrashanth Sreenivasa 	if (error == ENOSPC)
53265cabbc6bSPrashanth Sreenivasa 		error = 0;
53275cabbc6bSPrashanth Sreenivasa 	ASSERT0(error);
53285cabbc6bSPrashanth Sreenivasa 
5329f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
53305cabbc6bSPrashanth Sreenivasa }
53315cabbc6bSPrashanth Sreenivasa 
5332b24ab676SJeff Bonwick /* ARGSUSED */
5333b24ab676SJeff Bonwick void
5334b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5335b24ab676SJeff Bonwick {
5336b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
5337ea8dc4b6Seschrock 
5338f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5339ea8dc4b6Seschrock 
5340420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5341b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5342fa9e4066Sahrens 
5343*084fd14fSBrian Behlendorf 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_AUTOTRIM, ztest_random(2));
5344*084fd14fSBrian Behlendorf 
5345fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
5346fa9e4066Sahrens 
5347420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5348b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
5349fa9e4066Sahrens 
5350b24ab676SJeff Bonwick 	nvlist_free(props);
5351fa9e4066Sahrens 
5352f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5353fa9e4066Sahrens }
5354fa9e4066Sahrens 
53553b2aab18SMatthew Ahrens static int
53563b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
53573b2aab18SMatthew Ahrens {
53583b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
53593b2aab18SMatthew Ahrens 	int error;
53603b2aab18SMatthew Ahrens 
53613b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
53623b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
53633b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
53643b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
53653b2aab18SMatthew Ahrens 	fnvlist_free(holds);
53663b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
53673b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
53683b2aab18SMatthew Ahrens 	return (error);
53693b2aab18SMatthew Ahrens }
53703b2aab18SMatthew Ahrens 
53715c987a37SChris Kirby /*
53725c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
53735c987a37SChris Kirby  */
53745c987a37SChris Kirby void
5375b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
53765c987a37SChris Kirby {
53775c987a37SChris Kirby 	int error;
5378b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
53795c987a37SChris Kirby 	objset_t *origin;
53805c987a37SChris Kirby 	char snapname[100];
53815c987a37SChris Kirby 	char fullname[100];
53825c987a37SChris Kirby 	char clonename[100];
53835c987a37SChris Kirby 	char tag[100];
53849adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
53853b2aab18SMatthew Ahrens 	nvlist_t *holds;
53865c987a37SChris Kirby 
5387f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
53885c987a37SChris Kirby 
53895c987a37SChris Kirby 	dmu_objset_name(os, osname);
53905c987a37SChris Kirby 
53913b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
53923b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
53933b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
53943b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
53953b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
53965c987a37SChris Kirby 
53975c987a37SChris Kirby 	/*
53985c987a37SChris Kirby 	 * Clean up from any previous run.
53995c987a37SChris Kirby 	 */
54003b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
54013b2aab18SMatthew Ahrens 	if (error != ENOENT)
54023b2aab18SMatthew Ahrens 		ASSERT0(error);
54033b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
54043b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
54053b2aab18SMatthew Ahrens 		ASSERT0(error);
54063b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
54073b2aab18SMatthew Ahrens 	if (error != ENOENT)
54083b2aab18SMatthew Ahrens 		ASSERT0(error);
54095c987a37SChris Kirby 
54105c987a37SChris Kirby 	/*
54115c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
54125c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
54135c987a37SChris Kirby 	 */
54144445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
54155c987a37SChris Kirby 	if (error) {
54165c987a37SChris Kirby 		if (error == ENOSPC) {
54175c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
54185c987a37SChris Kirby 			goto out;
54195c987a37SChris Kirby 		}
54205c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
54215c987a37SChris Kirby 	}
54225c987a37SChris Kirby 
54233b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
54245c987a37SChris Kirby 	if (error) {
54255c987a37SChris Kirby 		if (error == ENOSPC) {
54265c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
54275c987a37SChris Kirby 			goto out;
54285c987a37SChris Kirby 		}
54295c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
54305c987a37SChris Kirby 	}
54315c987a37SChris Kirby 
54323b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
54335c987a37SChris Kirby 	if (error) {
54343b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
54355c987a37SChris Kirby 		    fullname, error);
54365c987a37SChris Kirby 	}
54375c987a37SChris Kirby 
54383b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
54395c987a37SChris Kirby 	if (error)
54403b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
54415c987a37SChris Kirby 
54425c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
54435c987a37SChris Kirby 	if (error != ENOENT)
54445c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
54455c987a37SChris Kirby 
54465c987a37SChris Kirby 	/*
54475c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
54485c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
54495c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
54505c987a37SChris Kirby 	 */
54514445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
54525c987a37SChris Kirby 	if (error) {
54535c987a37SChris Kirby 		if (error == ENOSPC) {
54545c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
54555c987a37SChris Kirby 			goto out;
54565c987a37SChris Kirby 		}
54575c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
54585c987a37SChris Kirby 	}
54595c987a37SChris Kirby 
54603b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
54613b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
54623b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
54633b2aab18SMatthew Ahrens 	fnvlist_free(holds);
54643b2aab18SMatthew Ahrens 
54655d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
54665d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
54675d7b4d43SMatthew Ahrens 		goto out;
54685d7b4d43SMatthew Ahrens 	} else if (error) {
54695d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
54705d7b4d43SMatthew Ahrens 		    fullname, tag, error);
54715d7b4d43SMatthew Ahrens 	}
54725c987a37SChris Kirby 
54733b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
54745c987a37SChris Kirby 	if (error != EBUSY) {
54753b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
54765c987a37SChris Kirby 		    fullname, error);
54775c987a37SChris Kirby 	}
54785c987a37SChris Kirby 
54793b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
54805c987a37SChris Kirby 	if (error) {
54813b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
54825c987a37SChris Kirby 		    fullname, error);
54835c987a37SChris Kirby 	}
54845c987a37SChris Kirby 
54853b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
54865c987a37SChris Kirby 	if (error)
5487a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
54885c987a37SChris Kirby 
54893b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
54905c987a37SChris Kirby 
54915c987a37SChris Kirby out:
5492f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
54935c987a37SChris Kirby }
54945c987a37SChris Kirby 
5495fa9e4066Sahrens /*
5496fa9e4066Sahrens  * Inject random faults into the on-disk data.
5497fa9e4066Sahrens  */
5498b24ab676SJeff Bonwick /* ARGSUSED */
5499fa9e4066Sahrens void
5500b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5501fa9e4066Sahrens {
5502b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5503420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5504fa9e4066Sahrens 	int fd;
5505fa9e4066Sahrens 	uint64_t offset;
55061195e687SMark J Musante 	uint64_t leaves;
5507fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
5508fa9e4066Sahrens 	uint64_t top, leaf;
5509fa9e4066Sahrens 	char path0[MAXPATHLEN];
5510fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
5511fa9e4066Sahrens 	size_t fsize;
5512f34284d8SMatthew Ahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;
5513fa9e4066Sahrens 	int iters = 1000;
55141195e687SMark J Musante 	int maxfaults;
55151195e687SMark J Musante 	int mirror_save;
5516e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
5517ea8dc4b6Seschrock 	uint64_t guid0 = 0;
55188f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
5519ea8dc4b6Seschrock 
5520f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
55213a4b1be9SMatthew Ahrens 
55223a4b1be9SMatthew Ahrens 	/*
55233a4b1be9SMatthew Ahrens 	 * Device removal is in progress, fault injection must be disabled
55243a4b1be9SMatthew Ahrens 	 * until it completes and the pool is scrubbed.  The fault injection
55253a4b1be9SMatthew Ahrens 	 * strategy for damaging blocks does not take in to account evacuated
55263a4b1be9SMatthew Ahrens 	 * blocks which may have already been damaged.
55273a4b1be9SMatthew Ahrens 	 */
55283a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
55293a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
55303a4b1be9SMatthew Ahrens 		return;
55313a4b1be9SMatthew Ahrens 	}
55323a4b1be9SMatthew Ahrens 
55331195e687SMark J Musante 	maxfaults = MAXFAULTS();
5534420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
55351195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
5536f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
55371195e687SMark J Musante 
5538e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
5539fa9e4066Sahrens 
55402c1e2b44SGeorge Wilson 	/*
55412c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
55422c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
55432c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
55442c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
55452c1e2b44SGeorge Wilson 	 */
5546f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
55472c1e2b44SGeorge Wilson 
5548fa9e4066Sahrens 	/*
5549e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5550fa9e4066Sahrens 	 */
5551e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5552fa9e4066Sahrens 
5553e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
5554e14bb325SJeff Bonwick 		/*
5555b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
5556e14bb325SJeff Bonwick 		 */
5557b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
55581195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
5559fa9e4066Sahrens 
5560e14bb325SJeff Bonwick 		/*
5561e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
5562e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
5563e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
5564e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
5565e14bb325SJeff Bonwick 		 */
5566e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
5567420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5568420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
5569e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5570420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5571420dfc95SChris Siden 		    top * leaves + leaf);
5572fa9e4066Sahrens 
5573e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
55748f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
55758f18d1faSGeorge Wilson 			islog = B_TRUE;
55768f18d1faSGeorge Wilson 
55772c1e2b44SGeorge Wilson 		/*
55782c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
55792c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
55802c1e2b44SGeorge Wilson 		 * resilvering.
55812c1e2b44SGeorge Wilson 		 */
55822c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
55832c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5584b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
5585e14bb325SJeff Bonwick 			/*
5586e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
5587e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
5588e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
5589e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
5590e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
5591e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
5592e14bb325SJeff Bonwick 			 * combination of this with injection of random data
5593e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
5594e14bb325SJeff Bonwick 			 */
5595e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
5596e14bb325SJeff Bonwick 
55975cabbc6bSPrashanth Sreenivasa 			zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
55985cabbc6bSPrashanth Sreenivasa 			    (long long)vd0->vdev_id, (int)maxfaults);
55995cabbc6bSPrashanth Sreenivasa 
5600e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
5601e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
5602e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
5603e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
5604e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
5605e14bb325SJeff Bonwick 			} else {
5606e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
5607e14bb325SJeff Bonwick 			}
5608e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
5609e14bb325SJeff Bonwick 		}
5610e14bb325SJeff Bonwick 	} else {
5611e14bb325SJeff Bonwick 		/*
5612e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
5613e14bb325SJeff Bonwick 		 */
5614e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
5615fa9e4066Sahrens 
5616e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
5617e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
5618f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
5619e14bb325SJeff Bonwick 			return;
5620e14bb325SJeff Bonwick 		}
5621e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5622ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
5623e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
5624e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
5625e14bb325SJeff Bonwick 
5626e14bb325SJeff Bonwick 		leaf = 0;
5627e14bb325SJeff Bonwick 		leaves = 1;
5628e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
5629fa9e4066Sahrens 	}
5630fa9e4066Sahrens 
5631e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
5632f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5633e14bb325SJeff Bonwick 
5634fa9e4066Sahrens 	/*
56358f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
56368f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
5637fa9e4066Sahrens 	 */
56388f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
56398ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
56408ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
56418ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
56428f18d1faSGeorge Wilson 
56438f18d1faSGeorge Wilson 			/*
56448f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
56458f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
56468f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
56478f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
56488f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
56498f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
56508f18d1faSGeorge Wilson 			 */
56518f18d1faSGeorge Wilson 			if (islog)
5652f06dce2cSAndrew Stormont 				rw_enter(&ztest_name_lock, RW_WRITER);
56538f18d1faSGeorge Wilson 
56548ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
56558f18d1faSGeorge Wilson 
56568f18d1faSGeorge Wilson 			if (islog)
5657f06dce2cSAndrew Stormont 				rw_exit(&ztest_name_lock);
56588ad4d6ddSJeff Bonwick 		} else {
56599253d63dSGeorge Wilson 			/*
56609253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
56619253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
56629253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
56639253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
56649253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
56659253d63dSGeorge Wilson 			 * prevent a race between injection testing and
56669253d63dSGeorge Wilson 			 * aux_vdev removal.
56679253d63dSGeorge Wilson 			 */
5668f06dce2cSAndrew Stormont 			mutex_enter(&ztest_vdev_lock);
56698ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
5670f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
56718ad4d6ddSJeff Bonwick 		}
5672fa9e4066Sahrens 	}
5673fa9e4066Sahrens 
56748f18d1faSGeorge Wilson 	if (maxfaults == 0)
56758f18d1faSGeorge Wilson 		return;
56768f18d1faSGeorge Wilson 
5677fa9e4066Sahrens 	/*
5678ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
5679fa9e4066Sahrens 	 */
5680fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
5681fa9e4066Sahrens 
568286714001SSerapheim Dimitropoulos 	if (fd == -1) /* we hit a gap in the device namespace */
5683fa9e4066Sahrens 		return;
5684fa9e4066Sahrens 
5685fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
5686fa9e4066Sahrens 
5687fa9e4066Sahrens 	while (--iters != 0) {
5688f9eb9fdfSMatthew Ahrens 		/*
5689f9eb9fdfSMatthew Ahrens 		 * The offset must be chosen carefully to ensure that
5690f9eb9fdfSMatthew Ahrens 		 * we do not inject a given logical block with errors
5691f9eb9fdfSMatthew Ahrens 		 * on two different leaf devices, because ZFS can not
5692f9eb9fdfSMatthew Ahrens 		 * tolerate that (if maxfaults==1).
5693f9eb9fdfSMatthew Ahrens 		 *
5694f9eb9fdfSMatthew Ahrens 		 * We divide each leaf into chunks of size
5695f9eb9fdfSMatthew Ahrens 		 * (# leaves * SPA_MAXBLOCKSIZE * 4).  Within each chunk
5696f9eb9fdfSMatthew Ahrens 		 * there is a series of ranges to which we can inject errors.
5697f9eb9fdfSMatthew Ahrens 		 * Each range can accept errors on only a single leaf vdev.
5698f9eb9fdfSMatthew Ahrens 		 * The error injection ranges are separated by ranges
5699f9eb9fdfSMatthew Ahrens 		 * which we will not inject errors on any device (DMZs).
5700f9eb9fdfSMatthew Ahrens 		 * Each DMZ must be large enough such that a single block
5701f9eb9fdfSMatthew Ahrens 		 * can not straddle it, so that a single block can not be
5702f9eb9fdfSMatthew Ahrens 		 * a target in two different injection ranges (on different
5703f9eb9fdfSMatthew Ahrens 		 * leaf vdevs).
5704f9eb9fdfSMatthew Ahrens 		 *
5705f9eb9fdfSMatthew Ahrens 		 * For example, with 3 leaves, each chunk looks like:
5706f9eb9fdfSMatthew Ahrens 		 *    0 to  32M: injection range for leaf 0
5707f9eb9fdfSMatthew Ahrens 		 *  32M to  64M: DMZ - no injection allowed
5708f9eb9fdfSMatthew Ahrens 		 *  64M to  96M: injection range for leaf 1
5709f9eb9fdfSMatthew Ahrens 		 *  96M to 128M: DMZ - no injection allowed
5710f9eb9fdfSMatthew Ahrens 		 * 128M to 160M: injection range for leaf 2
5711f9eb9fdfSMatthew Ahrens 		 * 160M to 192M: DMZ - no injection allowed
5712f9eb9fdfSMatthew Ahrens 		 */
5713fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
5714fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
5715fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5716fa9e4066Sahrens 
5717f34284d8SMatthew Ahrens 		/*
5718f34284d8SMatthew Ahrens 		 * Only allow damage to the labels at one end of the vdev.
5719f34284d8SMatthew Ahrens 		 *
5720f34284d8SMatthew Ahrens 		 * If all labels are damaged, the device will be totally
5721f34284d8SMatthew Ahrens 		 * inaccessible, which will result in loss of data,
5722f34284d8SMatthew Ahrens 		 * because we also damage (parts of) the other side of
5723f34284d8SMatthew Ahrens 		 * the mirror/raidz.
5724f34284d8SMatthew Ahrens 		 *
5725f34284d8SMatthew Ahrens 		 * Additionally, we will always have both an even and an
5726f34284d8SMatthew Ahrens 		 * odd label, so that we can handle crashes in the
5727f34284d8SMatthew Ahrens 		 * middle of vdev_config_sync().
5728f34284d8SMatthew Ahrens 		 */
5729f34284d8SMatthew Ahrens 		if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5730f34284d8SMatthew Ahrens 			continue;
5731f34284d8SMatthew Ahrens 
5732f34284d8SMatthew Ahrens 		/*
5733f34284d8SMatthew Ahrens 		 * The two end labels are stored at the "end" of the disk, but
5734f34284d8SMatthew Ahrens 		 * the end of the disk (vdev_psize) is aligned to
5735f34284d8SMatthew Ahrens 		 * sizeof (vdev_label_t).
5736f34284d8SMatthew Ahrens 		 */
5737f34284d8SMatthew Ahrens 		uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5738f34284d8SMatthew Ahrens 		if ((leaf & 1) == 1 &&
5739f34284d8SMatthew Ahrens 		    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5740fa9e4066Sahrens 			continue;
5741fa9e4066Sahrens 
5742f06dce2cSAndrew Stormont 		mutex_enter(&ztest_vdev_lock);
57431195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
5744f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
57451195e687SMark J Musante 			(void) close(fd);
57461195e687SMark J Musante 			return;
57471195e687SMark J Musante 		}
5748fa9e4066Sahrens 
5749fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5750fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
5751fa9e4066Sahrens 			    offset, pathrand);
57521195e687SMark J Musante 
5753f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
57541195e687SMark J Musante 
5755420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
57561195e687SMark J Musante 			(void) printf("injected bad word into %s,"
57571195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5758fa9e4066Sahrens 	}
5759fa9e4066Sahrens 
5760fa9e4066Sahrens 	(void) close(fd);
5761fa9e4066Sahrens }
5762fa9e4066Sahrens 
5763fa9e4066Sahrens /*
5764b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
5765fa9e4066Sahrens  */
5766fa9e4066Sahrens void
5767b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5768fa9e4066Sahrens {
5769b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5770420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5771b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5772b24ab676SJeff Bonwick 	ztest_od_t od[1];
5773b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
5774b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
5775b24ab676SJeff Bonwick 	dmu_buf_t *db;
5776b24ab676SJeff Bonwick 	dmu_tx_t *tx;
5777770499e1SDan Kimmel 	abd_t *abd;
5778b24ab676SJeff Bonwick 	blkptr_t blk;
5779b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
5780fa9e4066Sahrens 
5781b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
5782b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
5783fa9e4066Sahrens 
578454811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
578554811da5SToomas Soome 	    0, 0);
5786fa9e4066Sahrens 
5787b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5788b24ab676SJeff Bonwick 		return;
5789fa9e4066Sahrens 
5790fa9e4066Sahrens 	/*
5791b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
5792b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
5793fa9e4066Sahrens 	 */
5794f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5795fa9e4066Sahrens 
5796b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5797b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
5798b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5799b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
5800f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5801b24ab676SJeff Bonwick 		return;
5802b24ab676SJeff Bonwick 	}
5803b24ab676SJeff Bonwick 
5804aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
5805aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5806aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5807aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5808aab80726SGeorge Wilson 
5809b24ab676SJeff Bonwick 	object = od[0].od_object;
5810b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5811aab80726SGeorge Wilson 	pattern = zs->zs_guid ^ dds.dds_guid;
5812b24ab676SJeff Bonwick 
5813b24ab676SJeff Bonwick 	ASSERT(object != 0);
5814b24ab676SJeff Bonwick 
5815b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5816b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5817b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5818b24ab676SJeff Bonwick 	if (txg == 0) {
5819f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5820b24ab676SJeff Bonwick 		return;
5821b24ab676SJeff Bonwick 	}
5822fa9e4066Sahrens 
5823fa9e4066Sahrens 	/*
5824b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5825fa9e4066Sahrens 	 */
5826b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5827b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
58283b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
58293b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
58303b2aab18SMatthew Ahrens 		if (error != 0) {
58313b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
58323b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
58333b2aab18SMatthew Ahrens 		}
5834b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5835b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5836b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5837b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5838b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5839b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5840b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5841b24ab676SJeff Bonwick 	}
5842fa9e4066Sahrens 
5843b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5844b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5845fa9e4066Sahrens 
5846fa9e4066Sahrens 	/*
5847b24ab676SJeff Bonwick 	 * Find out what block we got.
5848fa9e4066Sahrens 	 */
584980901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
585080901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5851b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5852b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5853fa9e4066Sahrens 
5854fa9e4066Sahrens 	/*
5855b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5856fa9e4066Sahrens 	 */
5857b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5858770499e1SDan Kimmel 	abd = abd_alloc_linear(psize, B_TRUE);
5859770499e1SDan Kimmel 	ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5860fa9e4066Sahrens 
5861b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5862770499e1SDan Kimmel 	    abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5863b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5864fa9e4066Sahrens 
5865770499e1SDan Kimmel 	abd_free(abd);
5866fa9e4066Sahrens 
5867f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5868fa9e4066Sahrens }
5869fa9e4066Sahrens 
5870fa9e4066Sahrens /*
5871b24ab676SJeff Bonwick  * Scrub the pool.
5872fa9e4066Sahrens  */
5873b24ab676SJeff Bonwick /* ARGSUSED */
5874b24ab676SJeff Bonwick void
5875b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5876fa9e4066Sahrens {
5877420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5878fa9e4066Sahrens 
58793a4b1be9SMatthew Ahrens 	/*
58803a4b1be9SMatthew Ahrens 	 * Scrub in progress by device removal.
58813a4b1be9SMatthew Ahrens 	 */
58823a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active)
58833a4b1be9SMatthew Ahrens 		return;
58843a4b1be9SMatthew Ahrens 
58853f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5886b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
58873f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5888b24ab676SJeff Bonwick }
5889fa9e4066Sahrens 
5890e9103aaeSGarrett D'Amore /*
5891e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5892e9103aaeSGarrett D'Amore  */
5893e9103aaeSGarrett D'Amore /* ARGSUSED */
5894e9103aaeSGarrett D'Amore void
5895e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5896e9103aaeSGarrett D'Amore {
5897420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5898e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5899dfbb9432SGeorge Wilson 	int error;
5900e9103aaeSGarrett D'Amore 
5901e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
5902e0f1c0afSOlaf Faaland 		return;
5903e0f1c0afSOlaf Faaland 
5904e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5905e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5906dfbb9432SGeorge Wilson 
5907f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5908dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5909f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5910dfbb9432SGeorge Wilson 
5911dfbb9432SGeorge Wilson 	if (error != 0)
5912e9103aaeSGarrett D'Amore 		return;
5913e9103aaeSGarrett D'Amore 
591425345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5915e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5916e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5917e9103aaeSGarrett D'Amore 	}
5918e9103aaeSGarrett D'Amore 
5919e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5920e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5921e9103aaeSGarrett D'Amore }
5922e9103aaeSGarrett D'Amore 
5923094e47e9SGeorge Wilson static vdev_t *
5924094e47e9SGeorge Wilson ztest_random_concrete_vdev_leaf(vdev_t *vd)
5925094e47e9SGeorge Wilson {
5926094e47e9SGeorge Wilson 	if (vd == NULL)
5927094e47e9SGeorge Wilson 		return (NULL);
5928094e47e9SGeorge Wilson 
5929094e47e9SGeorge Wilson 	if (vd->vdev_children == 0)
5930094e47e9SGeorge Wilson 		return (vd);
5931094e47e9SGeorge Wilson 
5932094e47e9SGeorge Wilson 	vdev_t *eligible[vd->vdev_children];
5933094e47e9SGeorge Wilson 	int eligible_idx = 0, i;
5934094e47e9SGeorge Wilson 	for (i = 0; i < vd->vdev_children; i++) {
5935094e47e9SGeorge Wilson 		vdev_t *cvd = vd->vdev_child[i];
5936094e47e9SGeorge Wilson 		if (cvd->vdev_top->vdev_removing)
5937094e47e9SGeorge Wilson 			continue;
5938094e47e9SGeorge Wilson 		if (cvd->vdev_children > 0 ||
5939094e47e9SGeorge Wilson 		    (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5940094e47e9SGeorge Wilson 			eligible[eligible_idx++] = cvd;
5941094e47e9SGeorge Wilson 		}
5942094e47e9SGeorge Wilson 	}
5943094e47e9SGeorge Wilson 	VERIFY(eligible_idx > 0);
5944094e47e9SGeorge Wilson 
5945094e47e9SGeorge Wilson 	uint64_t child_no = ztest_random(eligible_idx);
5946094e47e9SGeorge Wilson 	return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5947094e47e9SGeorge Wilson }
5948094e47e9SGeorge Wilson 
5949094e47e9SGeorge Wilson /* ARGSUSED */
5950094e47e9SGeorge Wilson void
5951094e47e9SGeorge Wilson ztest_initialize(ztest_ds_t *zd, uint64_t id)
5952094e47e9SGeorge Wilson {
5953094e47e9SGeorge Wilson 	spa_t *spa = ztest_spa;
5954094e47e9SGeorge Wilson 	int error = 0;
5955094e47e9SGeorge Wilson 
5956094e47e9SGeorge Wilson 	mutex_enter(&ztest_vdev_lock);
5957094e47e9SGeorge Wilson 
5958094e47e9SGeorge Wilson 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5959094e47e9SGeorge Wilson 
5960094e47e9SGeorge Wilson 	/* Random leaf vdev */
5961094e47e9SGeorge Wilson 	vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5962094e47e9SGeorge Wilson 	if (rand_vd == NULL) {
5963094e47e9SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
5964094e47e9SGeorge Wilson 		mutex_exit(&ztest_vdev_lock);
5965094e47e9SGeorge Wilson 		return;
5966094e47e9SGeorge Wilson 	}
5967094e47e9SGeorge Wilson 
5968094e47e9SGeorge Wilson 	/*
5969094e47e9SGeorge Wilson 	 * The random vdev we've selected may change as soon as we
5970094e47e9SGeorge Wilson 	 * drop the spa_config_lock. We create local copies of things
5971094e47e9SGeorge Wilson 	 * we're interested in.
5972094e47e9SGeorge Wilson 	 */
5973094e47e9SGeorge Wilson 	uint64_t guid = rand_vd->vdev_guid;
5974094e47e9SGeorge Wilson 	char *path = strdup(rand_vd->vdev_path);
5975094e47e9SGeorge Wilson 	boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5976094e47e9SGeorge Wilson 
5977094e47e9SGeorge Wilson 	zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5978094e47e9SGeorge Wilson 	spa_config_exit(spa, SCL_VDEV, FTAG);
5979094e47e9SGeorge Wilson 
5980094e47e9SGeorge Wilson 	uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5981*084fd14fSBrian Behlendorf 
5982*084fd14fSBrian Behlendorf 	nvlist_t *vdev_guids = fnvlist_alloc();
5983*084fd14fSBrian Behlendorf 	nvlist_t *vdev_errlist = fnvlist_alloc();
5984*084fd14fSBrian Behlendorf 	fnvlist_add_uint64(vdev_guids, path, guid);
5985*084fd14fSBrian Behlendorf 	error = spa_vdev_initialize(spa, vdev_guids, cmd, vdev_errlist);
5986*084fd14fSBrian Behlendorf 	fnvlist_free(vdev_guids);
5987*084fd14fSBrian Behlendorf 	fnvlist_free(vdev_errlist);
5988*084fd14fSBrian Behlendorf 
5989094e47e9SGeorge Wilson 	switch (cmd) {
5990094e47e9SGeorge Wilson 	case POOL_INITIALIZE_CANCEL:
5991094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5992094e47e9SGeorge Wilson 			(void) printf("Cancel initialize %s", path);
5993094e47e9SGeorge Wilson 			if (!active)
5994094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5995094e47e9SGeorge Wilson 			(void) printf("\n");
5996094e47e9SGeorge Wilson 		}
5997094e47e9SGeorge Wilson 		break;
5998*084fd14fSBrian Behlendorf 	case POOL_INITIALIZE_START:
5999094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
6000094e47e9SGeorge Wilson 			(void) printf("Start initialize %s", path);
6001094e47e9SGeorge Wilson 			if (active && error == 0)
6002094e47e9SGeorge Wilson 				(void) printf(" failed (already active)");
6003094e47e9SGeorge Wilson 			else if (error != 0)
6004094e47e9SGeorge Wilson 				(void) printf(" failed (error %d)", error);
6005094e47e9SGeorge Wilson 			(void) printf("\n");
6006094e47e9SGeorge Wilson 		}
6007094e47e9SGeorge Wilson 		break;
6008094e47e9SGeorge Wilson 	case POOL_INITIALIZE_SUSPEND:
6009094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
6010094e47e9SGeorge Wilson 			(void) printf("Suspend initialize %s", path);
6011094e47e9SGeorge Wilson 			if (!active)
6012094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
6013094e47e9SGeorge Wilson 			(void) printf("\n");
6014094e47e9SGeorge Wilson 		}
6015094e47e9SGeorge Wilson 		break;
6016094e47e9SGeorge Wilson 	}
6017094e47e9SGeorge Wilson 	free(path);
6018094e47e9SGeorge Wilson 	mutex_exit(&ztest_vdev_lock);
6019094e47e9SGeorge Wilson }
6020094e47e9SGeorge Wilson 
6021*084fd14fSBrian Behlendorf /* ARGSUSED */
6022*084fd14fSBrian Behlendorf void
6023*084fd14fSBrian Behlendorf ztest_trim(ztest_ds_t *zd, uint64_t id)
6024*084fd14fSBrian Behlendorf {
6025*084fd14fSBrian Behlendorf 	spa_t *spa = ztest_spa;
6026*084fd14fSBrian Behlendorf 	int error = 0;
6027*084fd14fSBrian Behlendorf 
6028*084fd14fSBrian Behlendorf 	mutex_enter(&ztest_vdev_lock);
6029*084fd14fSBrian Behlendorf 
6030*084fd14fSBrian Behlendorf 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
6031*084fd14fSBrian Behlendorf 
6032*084fd14fSBrian Behlendorf 	/* Random leaf vdev */
6033*084fd14fSBrian Behlendorf 	vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
6034*084fd14fSBrian Behlendorf 	if (rand_vd == NULL) {
6035*084fd14fSBrian Behlendorf 		spa_config_exit(spa, SCL_VDEV, FTAG);
6036*084fd14fSBrian Behlendorf 		mutex_exit(&ztest_vdev_lock);
6037*084fd14fSBrian Behlendorf 		return;
6038*084fd14fSBrian Behlendorf 	}
6039*084fd14fSBrian Behlendorf 
6040*084fd14fSBrian Behlendorf 	/*
6041*084fd14fSBrian Behlendorf 	 * The random vdev we've selected may change as soon as we
6042*084fd14fSBrian Behlendorf 	 * drop the spa_config_lock. We create local copies of things
6043*084fd14fSBrian Behlendorf 	 * we're interested in.
6044*084fd14fSBrian Behlendorf 	 */
6045*084fd14fSBrian Behlendorf 	uint64_t guid = rand_vd->vdev_guid;
6046*084fd14fSBrian Behlendorf 	char *path = strdup(rand_vd->vdev_path);
6047*084fd14fSBrian Behlendorf 	boolean_t active = rand_vd->vdev_trim_thread != NULL;
6048*084fd14fSBrian Behlendorf 
6049*084fd14fSBrian Behlendorf 	zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
6050*084fd14fSBrian Behlendorf 	spa_config_exit(spa, SCL_VDEV, FTAG);
6051*084fd14fSBrian Behlendorf 
6052*084fd14fSBrian Behlendorf 	uint64_t cmd = ztest_random(POOL_TRIM_FUNCS);
6053*084fd14fSBrian Behlendorf 	uint64_t rate = 1 << ztest_random(30);
6054*084fd14fSBrian Behlendorf 	boolean_t partial = (ztest_random(5) > 0);
6055*084fd14fSBrian Behlendorf 	boolean_t secure = (ztest_random(5) > 0);
6056*084fd14fSBrian Behlendorf 
6057*084fd14fSBrian Behlendorf 	nvlist_t *vdev_guids = fnvlist_alloc();
6058*084fd14fSBrian Behlendorf 	nvlist_t *vdev_errlist = fnvlist_alloc();
6059*084fd14fSBrian Behlendorf 	fnvlist_add_uint64(vdev_guids, path, guid);
6060*084fd14fSBrian Behlendorf 	error = spa_vdev_trim(spa, vdev_guids, cmd, rate, partial,
6061*084fd14fSBrian Behlendorf 	    secure, vdev_errlist);
6062*084fd14fSBrian Behlendorf 	fnvlist_free(vdev_guids);
6063*084fd14fSBrian Behlendorf 	fnvlist_free(vdev_errlist);
6064*084fd14fSBrian Behlendorf 
6065*084fd14fSBrian Behlendorf 	switch (cmd) {
6066*084fd14fSBrian Behlendorf 	case POOL_TRIM_CANCEL:
6067*084fd14fSBrian Behlendorf 		if (ztest_opts.zo_verbose >= 4) {
6068*084fd14fSBrian Behlendorf 			(void) printf("Cancel TRIM %s", path);
6069*084fd14fSBrian Behlendorf 			if (!active)
6070*084fd14fSBrian Behlendorf 				(void) printf(" failed (no TRIM active)");
6071*084fd14fSBrian Behlendorf 			(void) printf("\n");
6072*084fd14fSBrian Behlendorf 		}
6073*084fd14fSBrian Behlendorf 		break;
6074*084fd14fSBrian Behlendorf 	case POOL_TRIM_START:
6075*084fd14fSBrian Behlendorf 		if (ztest_opts.zo_verbose >= 4) {
6076*084fd14fSBrian Behlendorf 			(void) printf("Start TRIM %s", path);
6077*084fd14fSBrian Behlendorf 			if (active && error == 0)
6078*084fd14fSBrian Behlendorf 				(void) printf(" failed (already active)");
6079*084fd14fSBrian Behlendorf 			else if (error != 0)
6080*084fd14fSBrian Behlendorf 				(void) printf(" failed (error %d)", error);
6081*084fd14fSBrian Behlendorf 			(void) printf("\n");
6082*084fd14fSBrian Behlendorf 		}
6083*084fd14fSBrian Behlendorf 		break;
6084*084fd14fSBrian Behlendorf 	case POOL_TRIM_SUSPEND:
6085*084fd14fSBrian Behlendorf 		if (ztest_opts.zo_verbose >= 4) {
6086*084fd14fSBrian Behlendorf 			(void) printf("Suspend TRIM %s", path);
6087*084fd14fSBrian Behlendorf 			if (!active)
6088*084fd14fSBrian Behlendorf 				(void) printf(" failed (no TRIM active)");
6089*084fd14fSBrian Behlendorf 			(void) printf("\n");
6090*084fd14fSBrian Behlendorf 		}
6091*084fd14fSBrian Behlendorf 		break;
6092*084fd14fSBrian Behlendorf 	}
6093*084fd14fSBrian Behlendorf 	free(path);
6094*084fd14fSBrian Behlendorf 	mutex_exit(&ztest_vdev_lock);
6095*084fd14fSBrian Behlendorf }
6096*084fd14fSBrian Behlendorf 
6097b24ab676SJeff Bonwick /*
6098b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
6099b24ab676SJeff Bonwick  */
6100fa9e4066Sahrens static void
6101b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
6102fa9e4066Sahrens {
6103fa9e4066Sahrens 	int status;
6104fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
6105fa9e4066Sahrens 	char zbuf[1024];
6106fa9e4066Sahrens 	char *bin;
61078654d025Sperrin 	char *ztest;
61088654d025Sperrin 	char *isa;
61098654d025Sperrin 	int isalen;
6110fa9e4066Sahrens 	FILE *fp;
6111fa9e4066Sahrens 
6112fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
6113fa9e4066Sahrens 
6114fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
6115fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
61168654d025Sperrin 	ztest = strstr(bin, "/ztest");
61178654d025Sperrin 	isa = bin + 8;
61188654d025Sperrin 	isalen = ztest - isa;
61198654d025Sperrin 	isa = strdup(isa);
6120fa9e4066Sahrens 	/* LINTED */
61213a57275aSck 	(void) sprintf(bin,
6122a21fe349SBrian Behlendorf 	    "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s "
6123a21fe349SBrian Behlendorf 	    "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
61248654d025Sperrin 	    isalen,
61258654d025Sperrin 	    isa,
6126420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
6127420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
6128f0ba89beSJeff Bonwick 	    spa_config_path,
612988b7b0f2SMatthew Ahrens 	    pool);
61308654d025Sperrin 	free(isa);
6131fa9e4066Sahrens 
6132420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
6133fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
6134fa9e4066Sahrens 
6135fa9e4066Sahrens 	fp = popen(zdb, "r");
6136fa9e4066Sahrens 
6137fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
6138420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
6139fa9e4066Sahrens 			(void) printf("%s", zbuf);
6140fa9e4066Sahrens 
6141fa9e4066Sahrens 	status = pclose(fp);
6142fa9e4066Sahrens 
6143fa9e4066Sahrens 	if (status == 0)
6144fa9e4066Sahrens 		return;
6145fa9e4066Sahrens 
6146fa9e4066Sahrens 	ztest_dump_core = 0;
6147fa9e4066Sahrens 	if (WIFEXITED(status))
6148fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
6149fa9e4066Sahrens 	else
6150fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
6151fa9e4066Sahrens }
6152fa9e4066Sahrens 
6153fa9e4066Sahrens static void
6154fa9e4066Sahrens ztest_walk_pool_directory(char *header)
6155fa9e4066Sahrens {
6156fa9e4066Sahrens 	spa_t *spa = NULL;
6157fa9e4066Sahrens 
6158420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
6159fa9e4066Sahrens 		(void) printf("%s\n", header);
6160fa9e4066Sahrens 
6161fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
6162fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
6163420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
6164fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
6165fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
6166fa9e4066Sahrens }
6167fa9e4066Sahrens 
6168fa9e4066Sahrens static void
6169fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
6170fa9e4066Sahrens {
61718ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
6172fa9e4066Sahrens 	uint64_t pool_guid;
6173fa9e4066Sahrens 	spa_t *spa;
61743b2aab18SMatthew Ahrens 	int error;
6175fa9e4066Sahrens 
6176420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
6177fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
6178fa9e4066Sahrens 		    oldname, newname);
6179fa9e4066Sahrens 	}
6180fa9e4066Sahrens 
6181fa9e4066Sahrens 	/*
6182fa9e4066Sahrens 	 * Clean up from previous runs.
6183fa9e4066Sahrens 	 */
6184fa9e4066Sahrens 	(void) spa_destroy(newname);
6185fa9e4066Sahrens 
6186fa9e4066Sahrens 	/*
6187fa9e4066Sahrens 	 * Get the pool's configuration and guid.
6188fa9e4066Sahrens 	 */
6189b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6190fa9e4066Sahrens 
61918ad4d6ddSJeff Bonwick 	/*
61928ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
61938ad4d6ddSJeff Bonwick 	 */
61948ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
61953f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
61968ad4d6ddSJeff Bonwick 
6197fa9e4066Sahrens 	pool_guid = spa_guid(spa);
6198fa9e4066Sahrens 	spa_close(spa, FTAG);
6199fa9e4066Sahrens 
6200fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
6201fa9e4066Sahrens 
6202fa9e4066Sahrens 	/*
6203fa9e4066Sahrens 	 * Export it.
6204fa9e4066Sahrens 	 */
6205b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6206fa9e4066Sahrens 
6207fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
6208fa9e4066Sahrens 
62098ad4d6ddSJeff Bonwick 	/*
62108ad4d6ddSJeff Bonwick 	 * Try to import it.
62118ad4d6ddSJeff Bonwick 	 */
62128ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
62138ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
62148ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
62158ad4d6ddSJeff Bonwick 
6216fa9e4066Sahrens 	/*
6217fa9e4066Sahrens 	 * Import it under the new name.
6218fa9e4066Sahrens 	 */
62193b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
62203b2aab18SMatthew Ahrens 	if (error != 0) {
62213b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
62223b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
62233b2aab18SMatthew Ahrens 		    oldname, newname, error);
62243b2aab18SMatthew Ahrens 	}
6225fa9e4066Sahrens 
6226fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
6227fa9e4066Sahrens 
6228fa9e4066Sahrens 	/*
6229fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
6230fa9e4066Sahrens 	 */
62314b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6232fa9e4066Sahrens 
6233fa9e4066Sahrens 	/*
6234fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
6235fa9e4066Sahrens 	 */
62364b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6237fa9e4066Sahrens 
6238fa9e4066Sahrens 	/*
6239fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
6240fa9e4066Sahrens 	 */
6241b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6242fa9e4066Sahrens 
6243fa9e4066Sahrens 	/*
6244fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
6245fa9e4066Sahrens 	 */
6246b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6247fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
6248fa9e4066Sahrens 	spa_close(spa, FTAG);
6249fa9e4066Sahrens 
6250fa9e4066Sahrens 	nvlist_free(config);
6251fa9e4066Sahrens }
6252fa9e4066Sahrens 
62538ad4d6ddSJeff Bonwick static void
62548ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
62558ad4d6ddSJeff Bonwick {
6256420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6257b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
6258b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
6259b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
6260b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
6261b24ab676SJeff Bonwick 	(void) zio_resume(spa);
62628ad4d6ddSJeff Bonwick }
62638ad4d6ddSJeff Bonwick 
62640a4e9518Sgw static void *
62658ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
62660a4e9518Sgw {
6267e14bb325SJeff Bonwick 	spa_t *spa = arg;
62680a4e9518Sgw 
62690a4e9518Sgw 	while (!ztest_exiting) {
6270b24ab676SJeff Bonwick 		if (spa_suspended(spa))
6271b24ab676SJeff Bonwick 			ztest_resume(spa);
6272b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
6273dcbf3bd6SGeorge Wilson 
6274dcbf3bd6SGeorge Wilson 		/*
6275dcbf3bd6SGeorge Wilson 		 * Periodically change the zfs_compressed_arc_enabled setting.
6276dcbf3bd6SGeorge Wilson 		 */
6277dcbf3bd6SGeorge Wilson 		if (ztest_random(10) == 0)
6278dcbf3bd6SGeorge Wilson 			zfs_compressed_arc_enabled = ztest_random(2);
6279770499e1SDan Kimmel 
6280770499e1SDan Kimmel 		/*
6281770499e1SDan Kimmel 		 * Periodically change the zfs_abd_scatter_enabled setting.
6282770499e1SDan Kimmel 		 */
6283770499e1SDan Kimmel 		if (ztest_random(10) == 0)
6284770499e1SDan Kimmel 			zfs_abd_scatter_enabled = ztest_random(2);
62850a4e9518Sgw 	}
62860a4e9518Sgw 	return (NULL);
62870a4e9518Sgw }
62880a4e9518Sgw 
6289b24ab676SJeff Bonwick static void *
6290b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
6291b24ab676SJeff Bonwick {
6292b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
62932c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
62942c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
6295b24ab676SJeff Bonwick 
62962c1e2b44SGeorge Wilson 	for (;;) {
629769962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
629869962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
6299b24ab676SJeff Bonwick 
630069962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6301b24ab676SJeff Bonwick 
63022c1e2b44SGeorge Wilson 		/*
63032c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
63042c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
63052c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
63062c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
63072c1e2b44SGeorge Wilson 		 */
63080713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
63092c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
63102c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
631169962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
63122c1e2b44SGeorge Wilson 			return (NULL);
63132c1e2b44SGeorge Wilson 		}
63142c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
6315b24ab676SJeff Bonwick 
631669962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
63172c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
63182c1e2b44SGeorge Wilson 		    total);
63192c1e2b44SGeorge Wilson 	}
6320b24ab676SJeff Bonwick }
6321b24ab676SJeff Bonwick 
6322b24ab676SJeff Bonwick static void
6323420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6324b24ab676SJeff Bonwick {
6325420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6326420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6327b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
6328b24ab676SJeff Bonwick 
6329b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
6330b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
6331b24ab676SJeff Bonwick 
6332b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
6333b24ab676SJeff Bonwick 
6334420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
6335420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
6336b24ab676SJeff Bonwick 
6337420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
6338b24ab676SJeff Bonwick 		Dl_info dli;
6339b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
6340b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
6341b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
6342b24ab676SJeff Bonwick 	}
6343b24ab676SJeff Bonwick }
6344b24ab676SJeff Bonwick 
6345fa9e4066Sahrens static void *
6346fa9e4066Sahrens ztest_thread(void *arg)
6347fa9e4066Sahrens {
6348420dfc95SChris Siden 	int rand;
6349b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
6350fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
6351b24ab676SJeff Bonwick 	uint64_t call_next;
6352b24ab676SJeff Bonwick 	hrtime_t now;
6353fa9e4066Sahrens 	ztest_info_t *zi;
6354420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6355fa9e4066Sahrens 
6356b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
6357fa9e4066Sahrens 		/*
6358fa9e4066Sahrens 		 * See if it's time to force a crash.
6359fa9e4066Sahrens 		 */
6360b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
6361b24ab676SJeff Bonwick 			ztest_kill(zs);
6362fa9e4066Sahrens 
6363fa9e4066Sahrens 		/*
6364b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
6365fa9e4066Sahrens 		 */
6366b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
6367b24ab676SJeff Bonwick 			break;
6368fa9e4066Sahrens 
6369fa9e4066Sahrens 		/*
6370b24ab676SJeff Bonwick 		 * Pick a random function to execute.
6371fa9e4066Sahrens 		 */
6372420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
6373420dfc95SChris Siden 		zi = &ztest_info[rand];
6374420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6375420dfc95SChris Siden 		call_next = zc->zc_next;
6376b24ab676SJeff Bonwick 
6377b24ab676SJeff Bonwick 		if (now >= call_next &&
6378420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
6379420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6380420dfc95SChris Siden 			ztest_execute(rand, zi, id);
6381420dfc95SChris Siden 		}
6382b24ab676SJeff Bonwick 	}
6383fa9e4066Sahrens 
6384b24ab676SJeff Bonwick 	return (NULL);
6385b24ab676SJeff Bonwick }
6386fa9e4066Sahrens 
6387b24ab676SJeff Bonwick static void
6388b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
6389b24ab676SJeff Bonwick {
63909adfa60dSMatthew Ahrens 	(void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6391b24ab676SJeff Bonwick }
6392fa9e4066Sahrens 
6393b24ab676SJeff Bonwick static void
6394420dfc95SChris Siden ztest_dataset_destroy(int d)
6395b24ab676SJeff Bonwick {
63969adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6397fa9e4066Sahrens 
6398420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6399fa9e4066Sahrens 
6400420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6401b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
6402fa9e4066Sahrens 
6403b24ab676SJeff Bonwick 	/*
6404b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
6405b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
6406b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
6407b24ab676SJeff Bonwick 	 */
6408420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
6409420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
6410b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
6411420dfc95SChris Siden 	}
6412fa9e4066Sahrens 
6413b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6414b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6415b24ab676SJeff Bonwick }
6416b24ab676SJeff Bonwick 
6417b24ab676SJeff Bonwick static void
6418b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6419b24ab676SJeff Bonwick {
6420b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
6421b24ab676SJeff Bonwick 
6422b24ab676SJeff Bonwick 	/*
6423b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
6424b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
6425b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6426b24ab676SJeff Bonwick 	 * If not, we have an object leak.
6427b24ab676SJeff Bonwick 	 *
6428b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
6429b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
6430b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
6431b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
6432b24ab676SJeff Bonwick 	 */
6433b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6434b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6435b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
6436b24ab676SJeff Bonwick }
6437b24ab676SJeff Bonwick 
6438b24ab676SJeff Bonwick static int
6439420dfc95SChris Siden ztest_dataset_open(int d)
6440b24ab676SJeff Bonwick {
6441420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6442420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6443b24ab676SJeff Bonwick 	objset_t *os;
6444b24ab676SJeff Bonwick 	zilog_t *zilog;
64459adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6446b24ab676SJeff Bonwick 	int error;
6447b24ab676SJeff Bonwick 
6448420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6449b24ab676SJeff Bonwick 
6450f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
6451b24ab676SJeff Bonwick 
645255da60b9SMark J Musante 	error = ztest_dataset_create(name);
6453b24ab676SJeff Bonwick 	if (error == ENOSPC) {
6454f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
6455b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
6456b24ab676SJeff Bonwick 		return (error);
6457fa9e4066Sahrens 	}
6458b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
6459fa9e4066Sahrens 
6460eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
6461eb633035STom Caputi 	    B_TRUE, zd, &os));
6462f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
6463b24ab676SJeff Bonwick 
6464420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6465b24ab676SJeff Bonwick 
6466b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
6467b24ab676SJeff Bonwick 
6468b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6469b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
6470b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
6471b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
6472b24ab676SJeff Bonwick 
6473b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6474b24ab676SJeff Bonwick 
6475b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
6476b24ab676SJeff Bonwick 
6477b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6478b24ab676SJeff Bonwick 
6479420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
6480b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6481b24ab676SJeff Bonwick 		    zd->zd_name,
6482b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
6483b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
6484b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
6485b24ab676SJeff Bonwick 
6486b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
6487b24ab676SJeff Bonwick 
6488b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
6489b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
6490b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
6491b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
6492b24ab676SJeff Bonwick 
6493b24ab676SJeff Bonwick 	return (0);
6494b24ab676SJeff Bonwick }
6495b24ab676SJeff Bonwick 
6496b24ab676SJeff Bonwick static void
6497420dfc95SChris Siden ztest_dataset_close(int d)
6498b24ab676SJeff Bonwick {
6499420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6500b24ab676SJeff Bonwick 
6501b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
6502eb633035STom Caputi 	dmu_objset_disown(zd->zd_os, B_TRUE, zd);
6503b24ab676SJeff Bonwick 
6504b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
6505fa9e4066Sahrens }
6506fa9e4066Sahrens 
6507fa9e4066Sahrens /*
6508fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
6509fa9e4066Sahrens  */
6510fa9e4066Sahrens static void
6511b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
6512fa9e4066Sahrens {
6513b24ab676SJeff Bonwick 	thread_t *tid;
6514fa9e4066Sahrens 	spa_t *spa;
6515e9103aaeSGarrett D'Amore 	objset_t *os;
6516e14bb325SJeff Bonwick 	thread_t resume_tid;
6517b24ab676SJeff Bonwick 	int error;
6518e14bb325SJeff Bonwick 
6519e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
6520fa9e4066Sahrens 
6521fa9e4066Sahrens 	/*
6522b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
6523fa9e4066Sahrens 	 */
652486714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6525f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6526f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6527fa9e4066Sahrens 
6528b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
6529420dfc95SChris Siden 	zs->zs_thread_stop =
6530420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6531b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6532b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
6533420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
6534420dfc95SChris Siden 		zs->zs_thread_kill -=
6535420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
6536420dfc95SChris Siden 	}
6537fa9e4066Sahrens 
6538f06dce2cSAndrew Stormont 	mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6539fa9e4066Sahrens 
6540b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6541b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
6542fa9e4066Sahrens 
65430a4e9518Sgw 	/*
6544e14bb325SJeff Bonwick 	 * Open our pool.
65450a4e9518Sgw 	 */
6546b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
65473b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
654830beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
6549420dfc95SChris Siden 	ztest_spa = spa;
6550b24ab676SJeff Bonwick 
6551aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
6552eb633035STom Caputi 	VERIFY0(ztest_dmu_objset_own(ztest_opts.zo_pool,
6553eb633035STom Caputi 	    DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
6554aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6555aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
6556aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6557aab80726SGeorge Wilson 	zs->zs_guid = dds.dds_guid;
6558eb633035STom Caputi 	dmu_objset_disown(os, B_TRUE, FTAG);
6559e9103aaeSGarrett D'Amore 
6560b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
65610a4e9518Sgw 
65628ad4d6ddSJeff Bonwick 	/*
65638ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
65648ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
65658ad4d6ddSJeff Bonwick 	 * the only valid replica.
65668ad4d6ddSJeff Bonwick 	 */
65671195e687SMark J Musante 	if (MAXFAULTS() == 0)
65688ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
65698ad4d6ddSJeff Bonwick 	else
65708ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
65718ad4d6ddSJeff Bonwick 
6572fa9e4066Sahrens 	/*
6573e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
6574fa9e4066Sahrens 	 */
65758ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6576e14bb325SJeff Bonwick 	    &resume_tid) == 0);
6577fa9e4066Sahrens 
6578b24ab676SJeff Bonwick 	/*
6579b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
6580b24ab676SJeff Bonwick 	 */
6581b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6582b24ab676SJeff Bonwick 	    NULL) == 0);
6583b24ab676SJeff Bonwick 
6584fa9e4066Sahrens 	/*
658586714001SSerapheim Dimitropoulos 	 * Verify that we can safely inquire about any object,
6586fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
6587fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
6588fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
6589fa9e4066Sahrens 	 */
6590b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
6591b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
6592fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
6593fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
6594ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
6595ea8dc4b6Seschrock 			    error == EINVAL);
6596fa9e4066Sahrens 		}
6597fa9e4066Sahrens 	}
6598fa9e4066Sahrens 
6599fa9e4066Sahrens 	/*
6600b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
6601fa9e4066Sahrens 	 */
6602b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
6603420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
6604420dfc95SChris Siden 		ztest_dataset_destroy(d);
6605b24ab676SJeff Bonwick 	}
6606fa9e4066Sahrens 	zs->zs_enospc_count = 0;
6607fa9e4066Sahrens 
6608420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6609420dfc95SChris Siden 	    UMEM_NOFAIL);
6610fa9e4066Sahrens 
6611420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
6612fa9e4066Sahrens 		(void) printf("starting main threads...\n");
6613fa9e4066Sahrens 
6614b24ab676SJeff Bonwick 	/*
6615b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
6616b24ab676SJeff Bonwick 	 */
6617420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
6618420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
6619420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
6620b24ab676SJeff Bonwick 			return;
6621b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6622b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
6623fa9e4066Sahrens 	}
6624fa9e4066Sahrens 
6625b24ab676SJeff Bonwick 	/*
6626b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
6627b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
6628b24ab676SJeff Bonwick 	 */
6629420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6630b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6631420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
6632420dfc95SChris Siden 			ztest_dataset_close(t);
6633fa9e4066Sahrens 	}
6634fa9e4066Sahrens 
6635fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
6636fa9e4066Sahrens 
6637b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6638b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6639b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
6640b24ab676SJeff Bonwick 
6641420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6642b24ab676SJeff Bonwick 
6643b24ab676SJeff Bonwick 	/* Kill the resume thread */
6644b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
6645b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6646b24ab676SJeff Bonwick 	ztest_resume(spa);
6647b24ab676SJeff Bonwick 
6648b24ab676SJeff Bonwick 	/*
6649b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
6650b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
6651b24ab676SJeff Bonwick 	 */
6652a2cdcdd2SPaul Dagnelie 	for (uint64_t object = 1; object < 50; object++) {
6653a2cdcdd2SPaul Dagnelie 		dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6654a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
6655a2cdcdd2SPaul Dagnelie 	}
6656b24ab676SJeff Bonwick 
6657b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6658fa9e4066Sahrens 
6659fa9e4066Sahrens 	/*
6660b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
6661fa9e4066Sahrens 	 */
6662b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
6663b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6664420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
6665b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
6666b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
6667b24ab676SJeff Bonwick 
6668b24ab676SJeff Bonwick 	/*
6669b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
6670b24ab676SJeff Bonwick 	 * different name.
6671b24ab676SJeff Bonwick 	 */
6672e0f1c0afSOlaf Faaland 	if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
66739adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
66749adfa60dSMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s_import",
6675420dfc95SChris Siden 		    ztest_opts.zo_pool);
6676420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
6677420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
6678b24ab676SJeff Bonwick 	}
6679b24ab676SJeff Bonwick 
6680b24ab676SJeff Bonwick 	kernel_fini();
668126cb54a9SGeorge Wilson 
668226cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
668326cb54a9SGeorge Wilson 
6684f06dce2cSAndrew Stormont 	mutex_destroy(&zcl.zcl_callbacks_lock);
668526cb54a9SGeorge Wilson 
6686f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6687f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
668886714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6689b24ab676SJeff Bonwick }
6690b24ab676SJeff Bonwick 
6691b24ab676SJeff Bonwick static void
6692420dfc95SChris Siden ztest_freeze(void)
6693b24ab676SJeff Bonwick {
6694420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
6695b24ab676SJeff Bonwick 	spa_t *spa;
66962215e990SMark J Musante 	int numloops = 0;
6697b24ab676SJeff Bonwick 
6698420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6699b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
6700718d718aSGeorge Wilson 
6701b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6702b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6703b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
670480901aeaSGeorge Wilson 	ztest_spa = spa;
6705718d718aSGeorge Wilson 
6706b24ab676SJeff Bonwick 	/*
6707b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
6708b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
6709b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
6710b24ab676SJeff Bonwick 	 */
6711b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6712b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
67135002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
6714fa9e4066Sahrens 	}
6715fa9e4066Sahrens 
6716ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
6717fa9e4066Sahrens 
6718b24ab676SJeff Bonwick 	/*
6719b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
6720b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
6721b24ab676SJeff Bonwick 	 */
6722b24ab676SJeff Bonwick 	spa_freeze(spa);
6723e14bb325SJeff Bonwick 
67242a104a52SAlex Reece 	/*
67252a104a52SAlex Reece 	 * Because it is hard to predict how much space a write will actually
67262a104a52SAlex Reece 	 * require beforehand, we leave ourselves some fudge space to write over
67272a104a52SAlex Reece 	 * capacity.
67282a104a52SAlex Reece 	 */
67292a104a52SAlex Reece 	uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
67302a104a52SAlex Reece 
6731b24ab676SJeff Bonwick 	/*
6732b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
6733b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6734b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
6735b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
6736b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
67372a104a52SAlex Reece 	 *
67382a104a52SAlex Reece 	 * Run a random number of times less than zo_maxloops and ensure we do
67392a104a52SAlex Reece 	 * not run out of space on the pool.
6740b24ab676SJeff Bonwick 	 */
6741420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
67422a104a52SAlex Reece 	    numloops++ < ztest_opts.zo_maxloops &&
67432a104a52SAlex Reece 	    metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
67442a104a52SAlex Reece 		ztest_od_t od;
674554811da5SToomas Soome 		ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
67462a104a52SAlex Reece 		VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
67472a104a52SAlex Reece 		ztest_io(zd, od.od_object,
67482a104a52SAlex Reece 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6749b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
6750b24ab676SJeff Bonwick 	}
6751e14bb325SJeff Bonwick 
6752fa9e4066Sahrens 	/*
6753b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
6754fa9e4066Sahrens 	 */
67555002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
6756b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
6757fa9e4066Sahrens 
6758b24ab676SJeff Bonwick 	/*
6759b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
6760b24ab676SJeff Bonwick 	 */
6761420dfc95SChris Siden 	ztest_dataset_close(0);
6762e05725b1Sbonwick 	spa_close(spa, FTAG);
6763b24ab676SJeff Bonwick 	kernel_fini();
6764e05725b1Sbonwick 
6765b24ab676SJeff Bonwick 	/*
6766b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
6767b24ab676SJeff Bonwick 	 */
6768b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6769b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6770ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6771b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
6772dfbb9432SGeorge Wilson 	ztest_spa = spa;
6773dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
6774eb633035STom Caputi 	ztest_dataset_close(0);
6775dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
6776dfbb9432SGeorge Wilson 
6777b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6778fa9e4066Sahrens 	kernel_fini();
6779fa9e4066Sahrens }
6780fa9e4066Sahrens 
6781fa9e4066Sahrens void
6782fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
6783fa9e4066Sahrens {
6784fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
6785fa9e4066Sahrens 	hrtime_t m = s / 60;
6786fa9e4066Sahrens 	hrtime_t h = m / 60;
6787fa9e4066Sahrens 	hrtime_t d = h / 24;
6788fa9e4066Sahrens 
6789fa9e4066Sahrens 	s -= m * 60;
6790fa9e4066Sahrens 	m -= h * 60;
6791fa9e4066Sahrens 	h -= d * 24;
6792fa9e4066Sahrens 
6793fa9e4066Sahrens 	timebuf[0] = '\0';
6794fa9e4066Sahrens 
6795fa9e4066Sahrens 	if (d)
6796fa9e4066Sahrens 		(void) sprintf(timebuf,
6797fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
6798fa9e4066Sahrens 	else if (h)
6799fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6800fa9e4066Sahrens 	else if (m)
6801fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
6802fa9e4066Sahrens 	else
6803fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
6804fa9e4066Sahrens }
6805fa9e4066Sahrens 
68061195e687SMark J Musante static nvlist_t *
68071195e687SMark J Musante make_random_props()
68081195e687SMark J Musante {
68091195e687SMark J Musante 	nvlist_t *props;
68101195e687SMark J Musante 
68111195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6812663207adSDon Brady 
6813ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
6814ad135b5dSChristopher Siden 		return (props);
68151195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
68161195e687SMark J Musante 
68171195e687SMark J Musante 	return (props);
68181195e687SMark J Musante }
68191195e687SMark J Musante 
6820e0f1c0afSOlaf Faaland /*
6821e0f1c0afSOlaf Faaland  * Import a storage pool with the given name.
6822e0f1c0afSOlaf Faaland  */
6823e0f1c0afSOlaf Faaland static void
6824e0f1c0afSOlaf Faaland ztest_import(ztest_shared_t *zs)
6825e0f1c0afSOlaf Faaland {
6826e0f1c0afSOlaf Faaland 	libzfs_handle_t *hdl;
6827e0f1c0afSOlaf Faaland 	importargs_t args = { 0 };
6828e0f1c0afSOlaf Faaland 	spa_t *spa;
6829e0f1c0afSOlaf Faaland 	nvlist_t *cfg = NULL;
6830e0f1c0afSOlaf Faaland 	int nsearch = 1;
6831e0f1c0afSOlaf Faaland 	char *searchdirs[nsearch];
6832e0f1c0afSOlaf Faaland 	char *name = ztest_opts.zo_pool;
6833e0f1c0afSOlaf Faaland 	int flags = ZFS_IMPORT_MISSING_LOG;
6834e0f1c0afSOlaf Faaland 	int error;
6835e0f1c0afSOlaf Faaland 
6836e0f1c0afSOlaf Faaland 	mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6837e0f1c0afSOlaf Faaland 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6838e0f1c0afSOlaf Faaland 
6839e0f1c0afSOlaf Faaland 	kernel_init(FREAD | FWRITE);
6840e0f1c0afSOlaf Faaland 	hdl = libzfs_init();
6841e0f1c0afSOlaf Faaland 
6842e0f1c0afSOlaf Faaland 	searchdirs[0] = ztest_opts.zo_dir;
6843e0f1c0afSOlaf Faaland 	args.paths = nsearch;
6844e0f1c0afSOlaf Faaland 	args.path = searchdirs;
6845e0f1c0afSOlaf Faaland 	args.can_be_active = B_FALSE;
6846e0f1c0afSOlaf Faaland 
6847e0f1c0afSOlaf Faaland 	error = zpool_tryimport(hdl, name, &cfg, &args);
6848e0f1c0afSOlaf Faaland 	if (error)
6849e0f1c0afSOlaf Faaland 		(void) fatal(0, "No pools found\n");
6850e0f1c0afSOlaf Faaland 
6851e0f1c0afSOlaf Faaland 	VERIFY0(spa_import(name, cfg, NULL, flags));
6852e0f1c0afSOlaf Faaland 	VERIFY0(spa_open(name, &spa, FTAG));
6853e0f1c0afSOlaf Faaland 	zs->zs_metaslab_sz =
6854e0f1c0afSOlaf Faaland 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6855e0f1c0afSOlaf Faaland 	spa_close(spa, FTAG);
6856e0f1c0afSOlaf Faaland 
6857e0f1c0afSOlaf Faaland 	libzfs_fini(hdl);
6858e0f1c0afSOlaf Faaland 	kernel_fini();
6859e0f1c0afSOlaf Faaland 
6860e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6861e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6862e0f1c0afSOlaf Faaland 		ztest_freeze();
6863e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6864e0f1c0afSOlaf Faaland 	}
6865e0f1c0afSOlaf Faaland 
6866e0f1c0afSOlaf Faaland 	rw_destroy(&ztest_name_lock);
6867e0f1c0afSOlaf Faaland 	mutex_destroy(&ztest_vdev_lock);
6868e0f1c0afSOlaf Faaland }
6869e0f1c0afSOlaf Faaland 
6870fa9e4066Sahrens /*
6871fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
6872b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
6873fa9e4066Sahrens  */
6874fa9e4066Sahrens static void
6875b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
6876fa9e4066Sahrens {
6877fa9e4066Sahrens 	spa_t *spa;
68781195e687SMark J Musante 	nvlist_t *nvroot, *props;
6879fa9e4066Sahrens 
6880f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
688186714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6882f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6883b24ab676SJeff Bonwick 
6884fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
6885fa9e4066Sahrens 
6886fa9e4066Sahrens 	/*
6887fa9e4066Sahrens 	 * Create the storage pool.
6888fa9e4066Sahrens 	 */
6889420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
689088ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
68911195e687SMark J Musante 	zs->zs_splits = 0;
6892420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
689325345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6894663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
68951195e687SMark J Musante 	props = make_random_props();
6896ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
6897ad135b5dSChristopher Siden 		char buf[1024];
6898ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
6899ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
6900b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6901ad135b5dSChristopher Siden 	}
6902eb633035STom Caputi 	VERIFY3U(0, ==,
6903eb633035STom Caputi 	    spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
6904fa9e4066Sahrens 	nvlist_free(nvroot);
69051df447ebSRichard Yao 	nvlist_free(props);
6906fa9e4066Sahrens 
6907b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6908420dfc95SChris Siden 	zs->zs_metaslab_sz =
6909420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6910ad135b5dSChristopher Siden 
6911fa9e4066Sahrens 	spa_close(spa, FTAG);
6912fa9e4066Sahrens 
6913fa9e4066Sahrens 	kernel_fini();
6914b24ab676SJeff Bonwick 
6915e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6916e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6917e0f1c0afSOlaf Faaland 		ztest_freeze();
6918e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6919e0f1c0afSOlaf Faaland 	}
6920420dfc95SChris Siden 
6921f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6922f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
692386714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6924420dfc95SChris Siden }
6925420dfc95SChris Siden 
6926420dfc95SChris Siden static void
6927741652b0SEtienne Dechamps setup_data_fd(void)
6928420dfc95SChris Siden {
6929741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6930420dfc95SChris Siden 
6931741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
6932741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
6933741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
6934420dfc95SChris Siden }
6935420dfc95SChris Siden 
6936ad135b5dSChristopher Siden static int
6937ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
6938ad135b5dSChristopher Siden {
6939ad135b5dSChristopher Siden 	int size;
6940ad135b5dSChristopher Siden 
6941ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
6942ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
6943ad135b5dSChristopher Siden 	size += hdr->zh_size;
6944ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
6945ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
6946ad135b5dSChristopher Siden 
6947ad135b5dSChristopher Siden 	return (size);
6948ad135b5dSChristopher Siden }
6949ad135b5dSChristopher Siden 
6950420dfc95SChris Siden static void
6951420dfc95SChris Siden setup_hdr(void)
6952420dfc95SChris Siden {
6953ad135b5dSChristopher Siden 	int size;
6954420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6955420dfc95SChris Siden 
6956420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6957741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6958420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6959420dfc95SChris Siden 
6960741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6961ad135b5dSChristopher Siden 
6962420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6963420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6964420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
6965420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6966420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
6967420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6968420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
6969420dfc95SChris Siden 
6970ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6971741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6972ad135b5dSChristopher Siden 
6973420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6974420dfc95SChris Siden }
6975420dfc95SChris Siden 
6976420dfc95SChris Siden static void
6977420dfc95SChris Siden setup_data(void)
6978420dfc95SChris Siden {
6979420dfc95SChris Siden 	int size, offset;
6980420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6981420dfc95SChris Siden 	uint8_t *buf;
6982420dfc95SChris Siden 
6983420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6984741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6985420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6986420dfc95SChris Siden 
6987ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6988420dfc95SChris Siden 
6989420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6990420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6991741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6992420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6993420dfc95SChris Siden 	buf = (uint8_t *)hdr;
6994420dfc95SChris Siden 
6995420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
6996420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
6997420dfc95SChris Siden 	offset += hdr->zh_opts_size;
6998420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
6999420dfc95SChris Siden 	offset += hdr->zh_size;
7000420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
7001420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
7002420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
7003420dfc95SChris Siden }
7004420dfc95SChris Siden 
7005420dfc95SChris Siden static boolean_t
7006420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
7007420dfc95SChris Siden {
7008420dfc95SChris Siden 	pid_t pid;
7009420dfc95SChris Siden 	int status;
7010741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
7011420dfc95SChris Siden 
7012420dfc95SChris Siden 	pid = fork();
7013420dfc95SChris Siden 
7014420dfc95SChris Siden 	if (cmd == NULL) {
7015741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
7016741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
7017420dfc95SChris Siden 		cmd = cmdbuf;
7018420dfc95SChris Siden 	}
7019b24ab676SJeff Bonwick 
7020420dfc95SChris Siden 	if (pid == -1)
7021420dfc95SChris Siden 		fatal(1, "fork failed");
7022b24ab676SJeff Bonwick 
7023420dfc95SChris Siden 	if (pid == 0) {	/* child */
7024420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
7025741652b0SEtienne Dechamps 		char fd_data_str[12];
702626cb54a9SGeorge Wilson 
7027420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
7028420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
7029741652b0SEtienne Dechamps 
7030741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
7031741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
7032741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
7033741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
7034741652b0SEtienne Dechamps 
7035420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
7036420dfc95SChris Siden 		if (libpath != NULL)
7037420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
7038420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
7039420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
7040420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
7041420dfc95SChris Siden 	}
7042420dfc95SChris Siden 
7043741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
7044741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
7045741652b0SEtienne Dechamps 		cmd = NULL;
7046741652b0SEtienne Dechamps 	}
7047741652b0SEtienne Dechamps 
7048420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
7049420dfc95SChris Siden 		continue;
7050420dfc95SChris Siden 	if (statusp != NULL)
7051420dfc95SChris Siden 		*statusp = status;
7052420dfc95SChris Siden 
7053420dfc95SChris Siden 	if (WIFEXITED(status)) {
7054420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
7055420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
7056420dfc95SChris Siden 			    WEXITSTATUS(status));
7057420dfc95SChris Siden 			exit(2);
7058420dfc95SChris Siden 		}
7059420dfc95SChris Siden 		return (B_FALSE);
7060420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
7061420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
7062420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
7063420dfc95SChris Siden 			    WTERMSIG(status));
7064420dfc95SChris Siden 			exit(3);
7065420dfc95SChris Siden 		}
7066420dfc95SChris Siden 		return (B_TRUE);
7067420dfc95SChris Siden 	} else {
7068420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
7069420dfc95SChris Siden 		exit(4);
7070420dfc95SChris Siden 		/* NOTREACHED */
7071420dfc95SChris Siden 	}
7072420dfc95SChris Siden }
7073420dfc95SChris Siden 
7074420dfc95SChris Siden static void
7075420dfc95SChris Siden ztest_run_init(void)
7076420dfc95SChris Siden {
7077420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
7078420dfc95SChris Siden 
7079420dfc95SChris Siden 	/*
7080420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
7081420dfc95SChris Siden 	 */
7082420dfc95SChris Siden 	(void) remove(spa_config_path);
7083420dfc95SChris Siden 
7084e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_init == 0) {
7085e0f1c0afSOlaf Faaland 		if (ztest_opts.zo_verbose >= 1)
7086e0f1c0afSOlaf Faaland 			(void) printf("Importing pool %s\n",
7087e0f1c0afSOlaf Faaland 			    ztest_opts.zo_pool);
7088e0f1c0afSOlaf Faaland 		ztest_import(zs);
7089e0f1c0afSOlaf Faaland 		return;
7090e0f1c0afSOlaf Faaland 	}
7091e0f1c0afSOlaf Faaland 
7092420dfc95SChris Siden 	/*
7093420dfc95SChris Siden 	 * Create and initialize our storage pool.
7094420dfc95SChris Siden 	 */
7095420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
7096420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
7097420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
7098420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
7099420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
7100420dfc95SChris Siden 		}
7101420dfc95SChris Siden 		ztest_init(zs);
7102420dfc95SChris Siden 	}
7103fa9e4066Sahrens }
7104fa9e4066Sahrens 
7105fa9e4066Sahrens int
7106fa9e4066Sahrens main(int argc, char **argv)
7107fa9e4066Sahrens {
7108fa9e4066Sahrens 	int kills = 0;
7109fa9e4066Sahrens 	int iters = 0;
7110420dfc95SChris Siden 	int older = 0;
7111420dfc95SChris Siden 	int newer = 0;
7112fa9e4066Sahrens 	ztest_shared_t *zs;
7113fa9e4066Sahrens 	ztest_info_t *zi;
7114420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
7115fa9e4066Sahrens 	char timebuf[100];
71160a055120SJason King 	char numbuf[NN_NUMBUF_SZ];
7117741652b0SEtienne Dechamps 	char *cmd;
7118420dfc95SChris Siden 	boolean_t hasalt;
7119741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
7120fa9e4066Sahrens 
7121fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
7122fa9e4066Sahrens 
7123cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
712469962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
712517f11284SSerapheim Dimitropoulos 	/*
712617f11284SSerapheim Dimitropoulos 	 * As two-word space map entries may not come up often (especially
712717f11284SSerapheim Dimitropoulos 	 * if pool and vdev sizes are small) we want to force at least some
712817f11284SSerapheim Dimitropoulos 	 * of them so the feature get tested.
712917f11284SSerapheim Dimitropoulos 	 */
713017f11284SSerapheim Dimitropoulos 	zfs_force_some_double_word_sm_entries = B_TRUE;
7131cd1c8b85SMatthew Ahrens 
7132a21fe349SBrian Behlendorf 	/*
7133a21fe349SBrian Behlendorf 	 * Verify that even extensively damaged split blocks with many
7134a21fe349SBrian Behlendorf 	 * segments can be reconstructed in a reasonable amount of time
7135a21fe349SBrian Behlendorf 	 * when reconstruction is known to be possible.
7136a21fe349SBrian Behlendorf 	 */
7137a21fe349SBrian Behlendorf 	zfs_reconstruct_indirect_damage_fraction = 4;
7138a21fe349SBrian Behlendorf 
7139741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
7140741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
7141741652b0SEtienne Dechamps 
7142741652b0SEtienne Dechamps 	if (!fd_data_str) {
7143420dfc95SChris Siden 		process_options(argc, argv);
7144fa9e4066Sahrens 
7145741652b0SEtienne Dechamps 		setup_data_fd();
7146420dfc95SChris Siden 		setup_hdr();
7147420dfc95SChris Siden 		setup_data();
7148420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
7149420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
7150420dfc95SChris Siden 	} else {
7151741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
7152420dfc95SChris Siden 		setup_data();
7153420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
7154420dfc95SChris Siden 	}
7155420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
7156fa9e4066Sahrens 
7157f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
7158741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
7159741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
7160f0ba89beSJeff Bonwick 
7161420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
7162420dfc95SChris Siden 	    UMEM_NOFAIL);
7163420dfc95SChris Siden 	zs = ztest_shared;
7164ea8dc4b6Seschrock 
7165741652b0SEtienne Dechamps 	if (fd_data_str) {
7166243952c7SMatt Ahrens 		metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
7167420dfc95SChris Siden 		metaslab_df_alloc_threshold =
7168420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
7169b24ab676SJeff Bonwick 
7170420dfc95SChris Siden 		if (zs->zs_do_init)
7171420dfc95SChris Siden 			ztest_run_init();
7172420dfc95SChris Siden 		else
7173420dfc95SChris Siden 			ztest_run(zs);
7174420dfc95SChris Siden 		exit(0);
7175420dfc95SChris Siden 	}
7176fa9e4066Sahrens 
7177420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
7178420dfc95SChris Siden 
7179420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
7180fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
7181fa9e4066Sahrens 		    " %llu seconds...\n",
7182420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
7183420dfc95SChris Siden 		    ztest_opts.zo_datasets,
7184420dfc95SChris Siden 		    ztest_opts.zo_threads,
7185420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
7186fa9e4066Sahrens 	}
7187fa9e4066Sahrens 
7188741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7189741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7190420dfc95SChris Siden 
7191420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
7192420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7193420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7194420dfc95SChris Siden 			(void) printf("Executing older ztest for "
7195420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
7196420dfc95SChris Siden 		}
7197420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7198420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7199420dfc95SChris Siden 	} else {
7200420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7201fa9e4066Sahrens 	}
7202420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
7203fa9e4066Sahrens 
7204b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
7205420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7206fa9e4066Sahrens 
7207b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
7208420dfc95SChris Siden 		zi = &ztest_info[f];
7209420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
7210b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7211420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
7212fa9e4066Sahrens 		else
7213420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
7214b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
7215fa9e4066Sahrens 	}
7216fa9e4066Sahrens 
7217fa9e4066Sahrens 	/*
7218fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
7219fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
7220fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
7221fa9e4066Sahrens 	 */
7222b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
7223fa9e4066Sahrens 		int status;
7224420dfc95SChris Siden 		boolean_t killed;
7225fa9e4066Sahrens 
7226fa9e4066Sahrens 		/*
7227fa9e4066Sahrens 		 * Initialize the workload counters for each function.
7228fa9e4066Sahrens 		 */
7229b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
7230420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
7231420dfc95SChris Siden 			zc->zc_count = 0;
7232420dfc95SChris Siden 			zc->zc_time = 0;
7233fa9e4066Sahrens 		}
7234fa9e4066Sahrens 
7235d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
7236420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
7237420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
7238fa9e4066Sahrens 
7239420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
7240420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7241420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
7242420dfc95SChris Siden 				    cmd);
7243fa9e4066Sahrens 			}
7244420dfc95SChris Siden 			newer++;
7245420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
72465ad82045Snd 		} else {
7247420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7248420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
7249420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
7250420dfc95SChris Siden 			}
7251420dfc95SChris Siden 			older++;
7252420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
7253420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
7254fa9e4066Sahrens 		}
7255fa9e4066Sahrens 
7256420dfc95SChris Siden 		if (killed)
7257420dfc95SChris Siden 			kills++;
7258fa9e4066Sahrens 		iters++;
7259fa9e4066Sahrens 
7260420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7261fa9e4066Sahrens 			hrtime_t now = gethrtime();
7262fa9e4066Sahrens 
7263b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
7264b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
72650a055120SJason King 			nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7266fa9e4066Sahrens 
7267fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7268fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7269fa9e4066Sahrens 			    iters,
7270fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
7271fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
7272fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
7273fa9e4066Sahrens 			    numbuf,
7274b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
7275420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
7276fa9e4066Sahrens 		}
7277fa9e4066Sahrens 
7278420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
7279fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
7280fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7281fa9e4066Sahrens 			    "Calls", "Time", "Function");
7282fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7283fa9e4066Sahrens 			    "-----", "----", "--------");
7284b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
7285fa9e4066Sahrens 				Dl_info dli;
7286fa9e4066Sahrens 
7287420dfc95SChris Siden 				zi = &ztest_info[f];
7288420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
7289420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
7290fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
7291fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
7292420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
7293fa9e4066Sahrens 				    dli.dli_sname);
7294fa9e4066Sahrens 			}
7295fa9e4066Sahrens 			(void) printf("\n");
7296fa9e4066Sahrens 		}
7297fa9e4066Sahrens 
7298e0f1c0afSOlaf Faaland 		if (!ztest_opts.zo_mmp_test)
7299e0f1c0afSOlaf Faaland 			ztest_run_zdb(ztest_opts.zo_pool);
7300b24ab676SJeff Bonwick 	}
7301fa9e4066Sahrens 
7302420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
7303420dfc95SChris Siden 		if (hasalt) {
7304420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
7305420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
7306420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
7307420dfc95SChris Siden 			    cmd);
7308420dfc95SChris Siden 		}
7309fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7310fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7311fa9e4066Sahrens 	}
7312fa9e4066Sahrens 
7313741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
7314741652b0SEtienne Dechamps 
7315fa9e4066Sahrens 	return (0);
7316fa9e4066Sahrens }
7317