xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision a3874b8b1fe5103fc1f961609557c0587435fec0)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2247cb52daSJeff Bonwick  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23663207adSDon Brady  * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
270a055120SJason King  * Copyright 2017 Joyent, Inc.
28663207adSDon Brady  * Copyright (c) 2017, Intel Corporation.
29f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
30fa9e4066Sahrens  */
31fa9e4066Sahrens 
32fa9e4066Sahrens /*
33fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
34fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * The overall design of the ztest program is as follows:
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
39fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
40fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
41fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
44fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
45fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
46fa9e4066Sahrens  *
47fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
48fa9e4066Sahrens  *     verify that self-healing data really works.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
51fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
52fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
55fa9e4066Sahrens  *     we run the entire test in a child of the main process.
56fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
57fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
58fa9e4066Sahrens  *     The parent then runs the test again, using the existing
595d7b4d43SMatthew Ahrens  *     storage pool, as many times as desired. If backwards compatibility
60420dfc95SChris Siden  *     testing is enabled ztest will sometimes run the "older" version
61420dfc95SChris Siden  *     of ztest after a SIGKILL.
62fa9e4066Sahrens  *
63fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
64fa9e4066Sahrens  *     many of the functional tests record the transaction group number
65fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
66fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
67fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
68fa9e4066Sahrens  *
69fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
70fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
71fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
72fa9e4066Sahrens  *
73fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
74fa9e4066Sahrens  *
75fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
76fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
77fa9e4066Sahrens  *
78420dfc95SChris Siden  * Use the -k option to set the desired frequency of kills.
79420dfc95SChris Siden  *
80420dfc95SChris Siden  * When ztest invokes itself it passes all relevant information through a
81420dfc95SChris Siden  * temporary file which is mmap-ed in the child process. This allows shared
82420dfc95SChris Siden  * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
83420dfc95SChris Siden  * stored at offset 0 of this file and contains information on the size and
84420dfc95SChris Siden  * number of shared structures in the file. The information stored in this file
85420dfc95SChris Siden  * must remain backwards compatible with older versions of ztest so that
86420dfc95SChris Siden  * ztest can invoke them during backwards compatibility testing (-B).
87fa9e4066Sahrens  */
88fa9e4066Sahrens 
89fa9e4066Sahrens #include <sys/zfs_context.h>
90fa9e4066Sahrens #include <sys/spa.h>
91fa9e4066Sahrens #include <sys/dmu.h>
92fa9e4066Sahrens #include <sys/txg.h>
932fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
94fa9e4066Sahrens #include <sys/zap.h>
95fa9e4066Sahrens #include <sys/dmu_objset.h>
96fa9e4066Sahrens #include <sys/poll.h>
97fa9e4066Sahrens #include <sys/stat.h>
98fa9e4066Sahrens #include <sys/time.h>
99fa9e4066Sahrens #include <sys/wait.h>
100fa9e4066Sahrens #include <sys/mman.h>
101fa9e4066Sahrens #include <sys/resource.h>
102fa9e4066Sahrens #include <sys/zio.h>
103fa9e4066Sahrens #include <sys/zil.h>
104b24ab676SJeff Bonwick #include <sys/zil_impl.h>
105fa9e4066Sahrens #include <sys/vdev_impl.h>
106e14bb325SJeff Bonwick #include <sys/vdev_file.h>
107094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
108fa9e4066Sahrens #include <sys/spa_impl.h>
10988ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
110fa9e4066Sahrens #include <sys/dsl_prop.h>
1114f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1123b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1133f9d6ad7SLin Ling #include <sys/dsl_scan.h>
114cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
115fa9e4066Sahrens #include <sys/refcount.h>
116ad135b5dSChristopher Siden #include <sys/zfeature.h>
1173b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
118770499e1SDan Kimmel #include <sys/abd.h>
119fa9e4066Sahrens #include <stdio.h>
120004388ebScasper #include <stdio_ext.h>
121fa9e4066Sahrens #include <stdlib.h>
122fa9e4066Sahrens #include <unistd.h>
123fa9e4066Sahrens #include <signal.h>
124fa9e4066Sahrens #include <umem.h>
125fa9e4066Sahrens #include <dlfcn.h>
126fa9e4066Sahrens #include <ctype.h>
127fa9e4066Sahrens #include <math.h>
128fa9e4066Sahrens #include <sys/fs/zfs.h>
129b24ab676SJeff Bonwick #include <libnvpair.h>
130e0f1c0afSOlaf Faaland #include <libzfs.h>
1310a055120SJason King #include <libcmdutils.h>
132fa9e4066Sahrens 
133741652b0SEtienne Dechamps static int ztest_fd_data = -1;
134741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
135420dfc95SChris Siden 
136420dfc95SChris Siden typedef struct ztest_shared_hdr {
137420dfc95SChris Siden 	uint64_t	zh_hdr_size;
138420dfc95SChris Siden 	uint64_t	zh_opts_size;
139420dfc95SChris Siden 	uint64_t	zh_size;
140420dfc95SChris Siden 	uint64_t	zh_stats_size;
141420dfc95SChris Siden 	uint64_t	zh_stats_count;
142420dfc95SChris Siden 	uint64_t	zh_ds_size;
143420dfc95SChris Siden 	uint64_t	zh_ds_count;
144420dfc95SChris Siden } ztest_shared_hdr_t;
145420dfc95SChris Siden 
146420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
147420dfc95SChris Siden 
148663207adSDon Brady enum ztest_class_state {
149663207adSDon Brady 	ZTEST_VDEV_CLASS_OFF,
150663207adSDon Brady 	ZTEST_VDEV_CLASS_ON,
151663207adSDon Brady 	ZTEST_VDEV_CLASS_RND
152663207adSDon Brady };
153663207adSDon Brady 
154420dfc95SChris Siden typedef struct ztest_shared_opts {
1559adfa60dSMatthew Ahrens 	char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
1569adfa60dSMatthew Ahrens 	char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
157420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
158420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
159420dfc95SChris Siden 	uint64_t zo_vdevs;
160420dfc95SChris Siden 	uint64_t zo_vdevtime;
161420dfc95SChris Siden 	size_t zo_vdev_size;
162420dfc95SChris Siden 	int zo_ashift;
163420dfc95SChris Siden 	int zo_mirrors;
164420dfc95SChris Siden 	int zo_raidz;
165420dfc95SChris Siden 	int zo_raidz_parity;
166420dfc95SChris Siden 	int zo_datasets;
167420dfc95SChris Siden 	int zo_threads;
168420dfc95SChris Siden 	uint64_t zo_passtime;
169420dfc95SChris Siden 	uint64_t zo_killrate;
170420dfc95SChris Siden 	int zo_verbose;
171420dfc95SChris Siden 	int zo_init;
172420dfc95SChris Siden 	uint64_t zo_time;
173420dfc95SChris Siden 	uint64_t zo_maxloops;
174243952c7SMatt Ahrens 	uint64_t zo_metaslab_force_ganging;
175e0f1c0afSOlaf Faaland 	int zo_mmp_test;
176663207adSDon Brady 	int zo_special_vdevs;
177420dfc95SChris Siden } ztest_shared_opts_t;
178420dfc95SChris Siden 
179420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
180420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
181420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
182420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
183420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
184420dfc95SChris Siden 	.zo_vdevs = 5,
185420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
186420dfc95SChris Siden 	.zo_mirrors = 2,
187420dfc95SChris Siden 	.zo_raidz = 4,
188420dfc95SChris Siden 	.zo_raidz_parity = 1,
1898363e80aSGeorge Wilson 	.zo_vdev_size = SPA_MINDEVSIZE * 4,	/* 256m default size */
190420dfc95SChris Siden 	.zo_datasets = 7,
191420dfc95SChris Siden 	.zo_threads = 23,
192420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
193420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
194420dfc95SChris Siden 	.zo_verbose = 0,
195e0f1c0afSOlaf Faaland 	.zo_mmp_test = 0,
196420dfc95SChris Siden 	.zo_init = 1,
197420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
198420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
199663207adSDon Brady 	.zo_metaslab_force_ganging = 32 << 10,
200663207adSDon Brady 	.zo_special_vdevs = ZTEST_VDEV_CLASS_RND,
201420dfc95SChris Siden };
202420dfc95SChris Siden 
203243952c7SMatt Ahrens extern uint64_t metaslab_force_ganging;
204420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
20569962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
20630beaff4SGeorge Wilson extern int metaslab_preload_limit;
207dcbf3bd6SGeorge Wilson extern boolean_t zfs_compressed_arc_enabled;
208770499e1SDan Kimmel extern boolean_t zfs_abd_scatter_enabled;
20954811da5SToomas Soome extern int dmu_object_alloc_chunk_shift;
21017f11284SSerapheim Dimitropoulos extern boolean_t zfs_force_some_double_word_sm_entries;
211a21fe349SBrian Behlendorf extern unsigned long zfs_reconstruct_indirect_damage_fraction;
212420dfc95SChris Siden 
213420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
214420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
215420dfc95SChris Siden 
216420dfc95SChris Siden typedef struct ztest_shared_ds {
217420dfc95SChris Siden 	uint64_t	zd_seq;
218420dfc95SChris Siden } ztest_shared_ds_t;
219420dfc95SChris Siden 
220420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
221420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
222fa9e4066Sahrens 
223b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
224420dfc95SChris Siden #define	MAXFAULTS() \
225420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
226b24ab676SJeff Bonwick 
227b24ab676SJeff Bonwick enum ztest_io_type {
228b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
229b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
230b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
231b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
232b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
23380901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
234b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
235b24ab676SJeff Bonwick };
236b24ab676SJeff Bonwick 
237e05725b1Sbonwick typedef struct ztest_block_tag {
238b24ab676SJeff Bonwick 	uint64_t	bt_magic;
239e05725b1Sbonwick 	uint64_t	bt_objset;
240e05725b1Sbonwick 	uint64_t	bt_object;
24154811da5SToomas Soome 	uint64_t	bt_dnodesize;
242e05725b1Sbonwick 	uint64_t	bt_offset;
243b24ab676SJeff Bonwick 	uint64_t	bt_gen;
244e05725b1Sbonwick 	uint64_t	bt_txg;
245b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
246e05725b1Sbonwick } ztest_block_tag_t;
247e05725b1Sbonwick 
248b24ab676SJeff Bonwick typedef struct bufwad {
249b24ab676SJeff Bonwick 	uint64_t	bw_index;
250b24ab676SJeff Bonwick 	uint64_t	bw_txg;
251b24ab676SJeff Bonwick 	uint64_t	bw_data;
252b24ab676SJeff Bonwick } bufwad_t;
253b24ab676SJeff Bonwick 
254b24ab676SJeff Bonwick /*
25579315247SMatthew Ahrens  * It would be better to use a rangelock_t per object.  Unfortunately
25679315247SMatthew Ahrens  * the rangelock_t is not a drop-in replacement for rl_t, because we
25779315247SMatthew Ahrens  * still need to map from object ID to rangelock_t.
258b24ab676SJeff Bonwick  */
259b24ab676SJeff Bonwick typedef enum {
260b24ab676SJeff Bonwick 	RL_READER,
261b24ab676SJeff Bonwick 	RL_WRITER,
262b24ab676SJeff Bonwick 	RL_APPEND
263b24ab676SJeff Bonwick } rl_type_t;
264b24ab676SJeff Bonwick 
265b24ab676SJeff Bonwick typedef struct rll {
266b24ab676SJeff Bonwick 	void		*rll_writer;
267b24ab676SJeff Bonwick 	int		rll_readers;
268f06dce2cSAndrew Stormont 	kmutex_t	rll_lock;
269f06dce2cSAndrew Stormont 	kcondvar_t	rll_cv;
270b24ab676SJeff Bonwick } rll_t;
271b24ab676SJeff Bonwick 
272b24ab676SJeff Bonwick typedef struct rl {
273b24ab676SJeff Bonwick 	uint64_t	rl_object;
274b24ab676SJeff Bonwick 	uint64_t	rl_offset;
275b24ab676SJeff Bonwick 	uint64_t	rl_size;
276b24ab676SJeff Bonwick 	rll_t		*rl_lock;
277b24ab676SJeff Bonwick } rl_t;
278b24ab676SJeff Bonwick 
279b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
280b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
281b24ab676SJeff Bonwick 
282b24ab676SJeff Bonwick /*
283b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
284b24ab676SJeff Bonwick  */
285b24ab676SJeff Bonwick typedef struct ztest_od {
286b24ab676SJeff Bonwick 	uint64_t	od_dir;
287b24ab676SJeff Bonwick 	uint64_t	od_object;
288b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
289b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
290b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
291b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
29254811da5SToomas Soome 	uint64_t	od_crdnodesize;
293b24ab676SJeff Bonwick 	uint64_t	od_gen;
294b24ab676SJeff Bonwick 	uint64_t	od_crgen;
2959adfa60dSMatthew Ahrens 	char		od_name[ZFS_MAX_DATASET_NAME_LEN];
296b24ab676SJeff Bonwick } ztest_od_t;
297fa9e4066Sahrens 
298b24ab676SJeff Bonwick /*
299b24ab676SJeff Bonwick  * Per-dataset state.
300b24ab676SJeff Bonwick  */
301b24ab676SJeff Bonwick typedef struct ztest_ds {
302420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
303b24ab676SJeff Bonwick 	objset_t	*zd_os;
304f06dce2cSAndrew Stormont 	krwlock_t	zd_zilog_lock;
305b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
306b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
3079adfa60dSMatthew Ahrens 	char		zd_name[ZFS_MAX_DATASET_NAME_LEN];
308f06dce2cSAndrew Stormont 	kmutex_t	zd_dirobj_lock;
309b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
310b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
311b24ab676SJeff Bonwick } ztest_ds_t;
312b24ab676SJeff Bonwick 
313b24ab676SJeff Bonwick /*
314b24ab676SJeff Bonwick  * Per-iteration state.
315b24ab676SJeff Bonwick  */
316b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
317b24ab676SJeff Bonwick 
318b24ab676SJeff Bonwick typedef struct ztest_info {
319b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
320b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
321b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
322b24ab676SJeff Bonwick } ztest_info_t;
323fa9e4066Sahrens 
324420dfc95SChris Siden typedef struct ztest_shared_callstate {
325420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
326420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
327420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
328420dfc95SChris Siden } ztest_shared_callstate_t;
329420dfc95SChris Siden 
330420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
331420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
332420dfc95SChris Siden 
333fa9e4066Sahrens /*
334fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
335fa9e4066Sahrens  */
336fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
337fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
338fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
33954811da5SToomas Soome ztest_func_t ztest_dmu_object_next_chunk;
340d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
341fa9e4066Sahrens ztest_func_t ztest_zap;
342fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
343b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
344c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
345b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
346fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
347b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
348b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
349fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
350b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
351b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
352fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
353fa9e4066Sahrens ztest_func_t ztest_fault_inject;
354b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
355b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
356e0f1c0afSOlaf Faaland ztest_func_t ztest_mmp_enable_disable;
357b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
358b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
359fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
360fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
361fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
362663207adSDon Brady ztest_func_t ztest_vdev_class_add;
363e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3641195e687SMark J Musante ztest_func_t ztest_split_pool;
365e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
36625345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
3675cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_device_removal;
3685cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_remap_blocks;
36986714001SSerapheim Dimitropoulos ztest_func_t ztest_spa_checkpoint_create_discard;
370094e47e9SGeorge Wilson ztest_func_t ztest_initialize;
37154811da5SToomas Soome ztest_func_t ztest_verify_dnode_bt;
372fa9e4066Sahrens 
373b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
374b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
375b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
376b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
377b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
378fa9e4066Sahrens 
379fa9e4066Sahrens ztest_info_t ztest_info[] = {
380e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
381b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
382e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
38354811da5SToomas Soome 	{ ztest_dmu_object_next_chunk,		1,	&zopt_sometimes	},
384b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
385e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
386e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3871195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
388b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
389c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
390b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
391b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
392b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
393b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
394b24ab676SJeff Bonwick #if 0
395b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
396b24ab676SJeff Bonwick #endif
397b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
398b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
399b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
400*a3874b8bSToomas Soome 	{ ztest_fault_inject,			1,	&zopt_incessant	},
401b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
4025c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
403e0f1c0afSOlaf Faaland 	{ ztest_mmp_enable_disable,		1,	&zopt_sometimes	},
4042c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
405*a3874b8bSToomas Soome 	{ ztest_scrub,				1,	&zopt_often	},
40625345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
407b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
408*a3874b8bSToomas Soome 	{ ztest_vdev_attach_detach,		1,	&zopt_incessant	},
409e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
410420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
411420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
412663207adSDon Brady 	{ ztest_vdev_class_add,			1,
413663207adSDon Brady 	    &ztest_opts.zo_vdevtime				},
414420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
415420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
4165cabbc6bSPrashanth Sreenivasa 	{ ztest_device_removal,			1,	&zopt_sometimes	},
41786714001SSerapheim Dimitropoulos 	{ ztest_remap_blocks,			1,	&zopt_sometimes },
418094e47e9SGeorge Wilson 	{ ztest_spa_checkpoint_create_discard,	1,	&zopt_rarely	},
41954811da5SToomas Soome 	{ ztest_initialize,			1,	&zopt_sometimes },
42054811da5SToomas Soome 	{ ztest_verify_dnode_bt,		1,	&zopt_sometimes }
421fa9e4066Sahrens };
422fa9e4066Sahrens 
423fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
424fa9e4066Sahrens 
425d20e665cSRicardo M. Correia /*
426d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
427d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
428d20e665cSRicardo M. Correia  */
429d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
430f06dce2cSAndrew Stormont 	kmutex_t zcl_callbacks_lock;
431d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
432d20e665cSRicardo M. Correia } ztest_cb_list_t;
433d20e665cSRicardo M. Correia 
434fa9e4066Sahrens /*
435fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
436fa9e4066Sahrens  */
437fa9e4066Sahrens typedef struct ztest_shared {
438420dfc95SChris Siden 	boolean_t	zs_do_init;
439b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
440b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
441b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
442b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
443b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
444b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
44588ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
446e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
447fa9e4066Sahrens 	uint64_t	zs_alloc;
448fa9e4066Sahrens 	uint64_t	zs_space;
4491195e687SMark J Musante 	uint64_t	zs_splits;
4501195e687SMark J Musante 	uint64_t	zs_mirrors;
451420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
452420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
453420dfc95SChris Siden 	uint64_t	zs_guid;
454fa9e4066Sahrens } ztest_shared_t;
455fa9e4066Sahrens 
456b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
457b24ab676SJeff Bonwick 
458fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
459e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
460b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
461fa9e4066Sahrens 
462420dfc95SChris Siden static spa_t *ztest_spa = NULL;
463420dfc95SChris Siden static ztest_ds_t *ztest_ds;
464420dfc95SChris Siden 
465f06dce2cSAndrew Stormont static kmutex_t ztest_vdev_lock;
4663a4b1be9SMatthew Ahrens static boolean_t ztest_device_removal_active = B_FALSE;
46754811da5SToomas Soome static kmutex_t ztest_checkpoint_lock;
468dfbb9432SGeorge Wilson 
469dfbb9432SGeorge Wilson /*
470dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
471dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
472dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
473dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
474dfbb9432SGeorge Wilson  */
475f06dce2cSAndrew Stormont static krwlock_t ztest_name_lock;
476fa9e4066Sahrens 
477420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
478e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4790a4e9518Sgw 
480d20e665cSRicardo M. Correia /* Global commit callback list */
481d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
482d20e665cSRicardo M. Correia 
483b24ab676SJeff Bonwick enum ztest_object {
484b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
485b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
486b24ab676SJeff Bonwick 	ZTEST_OBJECTS
487b24ab676SJeff Bonwick };
488fa9e4066Sahrens 
4891ce825d8Sraf static void usage(boolean_t) __NORETURN;
490f1b4288bSvb 
491fa9e4066Sahrens /*
492fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
493fa9e4066Sahrens  * debugging facilities.
494fa9e4066Sahrens  */
495fa9e4066Sahrens const char *
496fa9e4066Sahrens _umem_debug_init()
497fa9e4066Sahrens {
498fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
499fa9e4066Sahrens }
500fa9e4066Sahrens 
501fa9e4066Sahrens const char *
502fa9e4066Sahrens _umem_logging_init(void)
503fa9e4066Sahrens {
504fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
505fa9e4066Sahrens }
506fa9e4066Sahrens 
507fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
508fa9e4066Sahrens 
509fa9e4066Sahrens char *fatal_msg;
510fa9e4066Sahrens 
511fa9e4066Sahrens static void
512fa9e4066Sahrens fatal(int do_perror, char *message, ...)
513fa9e4066Sahrens {
514fa9e4066Sahrens 	va_list args;
515fa9e4066Sahrens 	int save_errno = errno;
516fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
517fa9e4066Sahrens 
518fa9e4066Sahrens 	(void) fflush(stdout);
519fa9e4066Sahrens 
520fa9e4066Sahrens 	va_start(args, message);
521fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
522fa9e4066Sahrens 	/* LINTED */
523fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
524fa9e4066Sahrens 	va_end(args);
525fa9e4066Sahrens 	if (do_perror) {
526fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
527fa9e4066Sahrens 		    ": %s", strerror(save_errno));
528fa9e4066Sahrens 	}
529fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
530fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
531fa9e4066Sahrens 	if (ztest_dump_core)
532fa9e4066Sahrens 		abort();
533fa9e4066Sahrens 	exit(3);
534fa9e4066Sahrens }
535fa9e4066Sahrens 
536fa9e4066Sahrens static int
537fa9e4066Sahrens str2shift(const char *buf)
538fa9e4066Sahrens {
539fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
540fa9e4066Sahrens 	int i;
541fa9e4066Sahrens 
542fa9e4066Sahrens 	if (buf[0] == '\0')
543fa9e4066Sahrens 		return (0);
544fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
545fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
546fa9e4066Sahrens 			break;
547fa9e4066Sahrens 	}
548f1b4288bSvb 	if (i == strlen(ends)) {
549f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
550f1b4288bSvb 		    buf);
551f1b4288bSvb 		usage(B_FALSE);
552f1b4288bSvb 	}
553fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
554fa9e4066Sahrens 		return (10*i);
555fa9e4066Sahrens 	}
556f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
557f1b4288bSvb 	usage(B_FALSE);
558f1b4288bSvb 	/* NOTREACHED */
559fa9e4066Sahrens }
560fa9e4066Sahrens 
561fa9e4066Sahrens static uint64_t
562fa9e4066Sahrens nicenumtoull(const char *buf)
563fa9e4066Sahrens {
564fa9e4066Sahrens 	char *end;
565fa9e4066Sahrens 	uint64_t val;
566fa9e4066Sahrens 
567fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
568fa9e4066Sahrens 	if (end == buf) {
569f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
570f1b4288bSvb 		usage(B_FALSE);
571fa9e4066Sahrens 	} else if (end[0] == '.') {
572fa9e4066Sahrens 		double fval = strtod(buf, &end);
573fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
574f1b4288bSvb 		if (fval > UINT64_MAX) {
575f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
576f1b4288bSvb 			    buf);
577f1b4288bSvb 			usage(B_FALSE);
578f1b4288bSvb 		}
579fa9e4066Sahrens 		val = (uint64_t)fval;
580fa9e4066Sahrens 	} else {
581fa9e4066Sahrens 		int shift = str2shift(end);
582f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
583f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
584f1b4288bSvb 			    buf);
585f1b4288bSvb 			usage(B_FALSE);
586f1b4288bSvb 		}
587fa9e4066Sahrens 		val <<= shift;
588fa9e4066Sahrens 	}
589fa9e4066Sahrens 	return (val);
590fa9e4066Sahrens }
591fa9e4066Sahrens 
592fa9e4066Sahrens static void
593f1b4288bSvb usage(boolean_t requested)
594fa9e4066Sahrens {
595420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
596420dfc95SChris Siden 
5970a055120SJason King 	char nice_vdev_size[NN_NUMBUF_SZ];
598243952c7SMatt Ahrens 	char nice_force_ganging[NN_NUMBUF_SZ];
599f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
600fa9e4066Sahrens 
6010a055120SJason King 	nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
602243952c7SMatt Ahrens 	nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
603243952c7SMatt Ahrens 	    sizeof (nice_force_ganging));
604fa9e4066Sahrens 
605f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
606fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
607fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
6082215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
609fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
610fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
61199653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
612fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
613fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
614fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
6152215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
6162215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
617fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
6182215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
619e0f1c0afSOlaf Faaland 	    "\t[-M] Multi-host simulate pool imported on remote host\n"
6202215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
6212215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
6222215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
6232215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
6242215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
625420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
626663207adSDon Brady 	    "\t[-C vdev class state (default: random)] special=on|off|random\n"
6270e60744cSPavel Zakharov 	    "\t[-o variable=value] ... set global variable to an unsigned\n"
6280e60744cSPavel Zakharov 	    "\t    32-bit integer value\n"
629f1b4288bSvb 	    "\t[-h] (print help)\n"
630fa9e4066Sahrens 	    "",
631420dfc95SChris Siden 	    zo->zo_pool,
632420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
6330a4e9518Sgw 	    nice_vdev_size,				/* -s */
634420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
635420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
636420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
637420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
638420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
639420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
640243952c7SMatt Ahrens 	    nice_force_ganging,				/* -g */
641420dfc95SChris Siden 	    zo->zo_init,				/* -i */
642420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
643420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
644420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
645420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
646420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
647420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
648f1b4288bSvb 	exit(requested ? 0 : 1);
649fa9e4066Sahrens }
650fa9e4066Sahrens 
651663207adSDon Brady 
652663207adSDon Brady static void
653663207adSDon Brady ztest_parse_name_value(const char *input, ztest_shared_opts_t *zo)
654663207adSDon Brady {
655663207adSDon Brady 	char name[32];
656663207adSDon Brady 	char *value;
657663207adSDon Brady 	int state = ZTEST_VDEV_CLASS_RND;
658663207adSDon Brady 
659663207adSDon Brady 	(void) strlcpy(name, input, sizeof (name));
660663207adSDon Brady 
661663207adSDon Brady 	value = strchr(name, '=');
662663207adSDon Brady 	if (value == NULL) {
663663207adSDon Brady 		(void) fprintf(stderr, "missing value in property=value "
664663207adSDon Brady 		    "'-C' argument (%s)\n", input);
665663207adSDon Brady 		usage(B_FALSE);
666663207adSDon Brady 	}
667663207adSDon Brady 	*(value) = '\0';
668663207adSDon Brady 	value++;
669663207adSDon Brady 
670663207adSDon Brady 	if (strcmp(value, "on") == 0) {
671663207adSDon Brady 		state = ZTEST_VDEV_CLASS_ON;
672663207adSDon Brady 	} else if (strcmp(value, "off") == 0) {
673663207adSDon Brady 		state = ZTEST_VDEV_CLASS_OFF;
674663207adSDon Brady 	} else if (strcmp(value, "random") == 0) {
675663207adSDon Brady 		state = ZTEST_VDEV_CLASS_RND;
676663207adSDon Brady 	} else {
677663207adSDon Brady 		(void) fprintf(stderr, "invalid property value '%s'\n", value);
678663207adSDon Brady 		usage(B_FALSE);
679663207adSDon Brady 	}
680663207adSDon Brady 
681663207adSDon Brady 	if (strcmp(name, "special") == 0) {
682663207adSDon Brady 		zo->zo_special_vdevs = state;
683663207adSDon Brady 	} else {
684663207adSDon Brady 		(void) fprintf(stderr, "invalid property name '%s'\n", name);
685663207adSDon Brady 		usage(B_FALSE);
686663207adSDon Brady 	}
687663207adSDon Brady 	if (zo->zo_verbose >= 3)
688663207adSDon Brady 		(void) printf("%s vdev state is '%s'\n", name, value);
689663207adSDon Brady }
690663207adSDon Brady 
691fa9e4066Sahrens static void
692fa9e4066Sahrens process_options(int argc, char **argv)
693fa9e4066Sahrens {
694420dfc95SChris Siden 	char *path;
695420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
696420dfc95SChris Siden 
697fa9e4066Sahrens 	int opt;
698fa9e4066Sahrens 	uint64_t value;
699420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
700fa9e4066Sahrens 
701420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
702fa9e4066Sahrens 
703fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
704663207adSDon Brady 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:")) != EOF) {
705fa9e4066Sahrens 		value = 0;
706fa9e4066Sahrens 		switch (opt) {
7073d7072f8Seschrock 		case 'v':
7083d7072f8Seschrock 		case 's':
7093d7072f8Seschrock 		case 'a':
7103d7072f8Seschrock 		case 'm':
7113d7072f8Seschrock 		case 'r':
7123d7072f8Seschrock 		case 'R':
7133d7072f8Seschrock 		case 'd':
7143d7072f8Seschrock 		case 't':
7153d7072f8Seschrock 		case 'g':
7163d7072f8Seschrock 		case 'i':
7173d7072f8Seschrock 		case 'k':
7183d7072f8Seschrock 		case 'T':
7193d7072f8Seschrock 		case 'P':
7202215e990SMark J Musante 		case 'F':
721fa9e4066Sahrens 			value = nicenumtoull(optarg);
722fa9e4066Sahrens 		}
723fa9e4066Sahrens 		switch (opt) {
7243d7072f8Seschrock 		case 'v':
725420dfc95SChris Siden 			zo->zo_vdevs = value;
726fa9e4066Sahrens 			break;
7273d7072f8Seschrock 		case 's':
728420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
729fa9e4066Sahrens 			break;
7303d7072f8Seschrock 		case 'a':
731420dfc95SChris Siden 			zo->zo_ashift = value;
732ecc2d604Sbonwick 			break;
7333d7072f8Seschrock 		case 'm':
734420dfc95SChris Siden 			zo->zo_mirrors = value;
735fa9e4066Sahrens 			break;
7363d7072f8Seschrock 		case 'r':
737420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
738fa9e4066Sahrens 			break;
7393d7072f8Seschrock 		case 'R':
740420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
74199653d4eSeschrock 			break;
7423d7072f8Seschrock 		case 'd':
743420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
744fa9e4066Sahrens 			break;
7453d7072f8Seschrock 		case 't':
746420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
747fa9e4066Sahrens 			break;
7483d7072f8Seschrock 		case 'g':
749243952c7SMatt Ahrens 			zo->zo_metaslab_force_ganging =
750243952c7SMatt Ahrens 			    MAX(SPA_MINBLOCKSIZE << 1, value);
751fa9e4066Sahrens 			break;
7523d7072f8Seschrock 		case 'i':
753420dfc95SChris Siden 			zo->zo_init = value;
754fa9e4066Sahrens 			break;
7553d7072f8Seschrock 		case 'k':
756420dfc95SChris Siden 			zo->zo_killrate = value;
757fa9e4066Sahrens 			break;
7583d7072f8Seschrock 		case 'p':
759420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
760420dfc95SChris Siden 			    sizeof (zo->zo_pool));
761fa9e4066Sahrens 			break;
7623d7072f8Seschrock 		case 'f':
763420dfc95SChris Siden 			path = realpath(optarg, NULL);
764420dfc95SChris Siden 			if (path == NULL) {
765420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
766420dfc95SChris Siden 				    optarg, strerror(errno));
767420dfc95SChris Siden 				usage(B_FALSE);
768420dfc95SChris Siden 			} else {
769420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
770420dfc95SChris Siden 				    sizeof (zo->zo_dir));
771420dfc95SChris Siden 			}
772fa9e4066Sahrens 			break;
773e0f1c0afSOlaf Faaland 		case 'M':
774e0f1c0afSOlaf Faaland 			zo->zo_mmp_test = 1;
775e0f1c0afSOlaf Faaland 			break;
7763d7072f8Seschrock 		case 'V':
777420dfc95SChris Siden 			zo->zo_verbose++;
778fa9e4066Sahrens 			break;
7793d7072f8Seschrock 		case 'E':
780420dfc95SChris Siden 			zo->zo_init = 0;
781fa9e4066Sahrens 			break;
7823d7072f8Seschrock 		case 'T':
783420dfc95SChris Siden 			zo->zo_time = value;
784fa9e4066Sahrens 			break;
7853d7072f8Seschrock 		case 'P':
786420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
787fa9e4066Sahrens 			break;
7882215e990SMark J Musante 		case 'F':
789420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
790420dfc95SChris Siden 			break;
791420dfc95SChris Siden 		case 'B':
792420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7932215e990SMark J Musante 			break;
794663207adSDon Brady 		case 'C':
795663207adSDon Brady 			ztest_parse_name_value(optarg, zo);
796663207adSDon Brady 			break;
7970e60744cSPavel Zakharov 		case 'o':
7980e60744cSPavel Zakharov 			if (set_global_var(optarg) != 0)
7990e60744cSPavel Zakharov 				usage(B_FALSE);
8000e60744cSPavel Zakharov 			break;
8013d7072f8Seschrock 		case 'h':
802f1b4288bSvb 			usage(B_TRUE);
803f1b4288bSvb 			break;
8043d7072f8Seschrock 		case '?':
8053d7072f8Seschrock 		default:
806f1b4288bSvb 			usage(B_FALSE);
807fa9e4066Sahrens 			break;
808fa9e4066Sahrens 		}
809fa9e4066Sahrens 	}
810fa9e4066Sahrens 
811420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
81299653d4eSeschrock 
813420dfc95SChris Siden 	zo->zo_vdevtime =
814420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
815b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
816420dfc95SChris Siden 
817420dfc95SChris Siden 	if (strlen(altdir) > 0) {
818741652b0SEtienne Dechamps 		char *cmd;
819741652b0SEtienne Dechamps 		char *realaltdir;
820420dfc95SChris Siden 		char *bin;
821420dfc95SChris Siden 		char *ztest;
822420dfc95SChris Siden 		char *isa;
823420dfc95SChris Siden 		int isalen;
824420dfc95SChris Siden 
825741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
826741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
827741652b0SEtienne Dechamps 
828741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
829420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
830420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
831420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
832420dfc95SChris Siden 			    altdir);
833420dfc95SChris Siden 		}
834420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
835420dfc95SChris Siden 
836420dfc95SChris Siden 		/*
837420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
838420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
839420dfc95SChris Siden 		 * 32 or 64 bit binaries.
840420dfc95SChris Siden 		 */
841420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
842420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
843420dfc95SChris Siden 		isa = bin + 9;
844420dfc95SChris Siden 		isalen = ztest - isa;
845420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
846420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
847420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
848420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
849420dfc95SChris Siden 
850420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
851420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
852420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
853420dfc95SChris Siden 			    zo->zo_alt_ztest);
854420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
855420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
856420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
857420dfc95SChris Siden 			    zo->zo_alt_libpath);
858420dfc95SChris Siden 		}
859741652b0SEtienne Dechamps 
860741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
861741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
862420dfc95SChris Siden 	}
863fa9e4066Sahrens }
864fa9e4066Sahrens 
865b24ab676SJeff Bonwick static void
866b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
867b24ab676SJeff Bonwick {
868420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
869420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
870b4952e17SGeorge Wilson 
871b4952e17SGeorge Wilson 	/*
872b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
8735cabbc6bSPrashanth Sreenivasa 	 * See comment above spa_write_cachefile().
874b4952e17SGeorge Wilson 	 */
875b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
8765cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
877b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
878b4952e17SGeorge Wilson 
879b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
880b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
881b24ab676SJeff Bonwick }
882b24ab676SJeff Bonwick 
883b24ab676SJeff Bonwick static uint64_t
884b24ab676SJeff Bonwick ztest_random(uint64_t range)
885b24ab676SJeff Bonwick {
886b24ab676SJeff Bonwick 	uint64_t r;
887b24ab676SJeff Bonwick 
888741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
889741652b0SEtienne Dechamps 
890b24ab676SJeff Bonwick 	if (range == 0)
891b24ab676SJeff Bonwick 		return (0);
892b24ab676SJeff Bonwick 
893741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
894b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
895b24ab676SJeff Bonwick 
896b24ab676SJeff Bonwick 	return (r % range);
897b24ab676SJeff Bonwick }
898b24ab676SJeff Bonwick 
899b24ab676SJeff Bonwick /* ARGSUSED */
900b24ab676SJeff Bonwick static void
901b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
902b24ab676SJeff Bonwick {
903b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
904b24ab676SJeff Bonwick }
905b24ab676SJeff Bonwick 
906ecc2d604Sbonwick static uint64_t
907ecc2d604Sbonwick ztest_get_ashift(void)
908ecc2d604Sbonwick {
909420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
9102a104a52SAlex Reece 		return (SPA_MINBLOCKSHIFT + ztest_random(5));
911420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
912ecc2d604Sbonwick }
913ecc2d604Sbonwick 
914fa9e4066Sahrens static nvlist_t *
91525345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
916fa9e4066Sahrens {
917e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
918fa9e4066Sahrens 	uint64_t vdev;
919fa9e4066Sahrens 	nvlist_t *file;
920fa9e4066Sahrens 
921e14bb325SJeff Bonwick 	if (ashift == 0)
922e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
923e14bb325SJeff Bonwick 
924e14bb325SJeff Bonwick 	if (path == NULL) {
925e14bb325SJeff Bonwick 		path = pathbuf;
926fa9e4066Sahrens 
927e14bb325SJeff Bonwick 		if (aux != NULL) {
928e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
929420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
930420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
93125345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
93225345e46SGeorge Wilson 			    aux, vdev);
933e14bb325SJeff Bonwick 		} else {
93488ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
935420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
936420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
93725345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
938e14bb325SJeff Bonwick 		}
939e14bb325SJeff Bonwick 	}
940e14bb325SJeff Bonwick 
941e14bb325SJeff Bonwick 	if (size != 0) {
942e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
943fa9e4066Sahrens 		if (fd == -1)
944e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
945fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
946e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
947fa9e4066Sahrens 		(void) close(fd);
948fa9e4066Sahrens 	}
949fa9e4066Sahrens 
950fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
951fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
952e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
953ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
954fa9e4066Sahrens 
955fa9e4066Sahrens 	return (file);
956fa9e4066Sahrens }
957fa9e4066Sahrens 
958fa9e4066Sahrens static nvlist_t *
95925345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
96025345e46SGeorge Wilson     uint64_t ashift, int r)
961fa9e4066Sahrens {
962fa9e4066Sahrens 	nvlist_t *raidz, **child;
963fa9e4066Sahrens 	int c;
964fa9e4066Sahrens 
965fa9e4066Sahrens 	if (r < 2)
96625345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
967fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
968fa9e4066Sahrens 
969fa9e4066Sahrens 	for (c = 0; c < r; c++)
97025345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
971fa9e4066Sahrens 
972fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
973fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
974fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
97599653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
976420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
977fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
978fa9e4066Sahrens 	    child, r) == 0);
979fa9e4066Sahrens 
980fa9e4066Sahrens 	for (c = 0; c < r; c++)
981fa9e4066Sahrens 		nvlist_free(child[c]);
982fa9e4066Sahrens 
983fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
984fa9e4066Sahrens 
985fa9e4066Sahrens 	return (raidz);
986fa9e4066Sahrens }
987fa9e4066Sahrens 
988fa9e4066Sahrens static nvlist_t *
98925345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
99025345e46SGeorge Wilson     uint64_t ashift, int r, int m)
991fa9e4066Sahrens {
992fa9e4066Sahrens 	nvlist_t *mirror, **child;
993fa9e4066Sahrens 	int c;
994fa9e4066Sahrens 
995fa9e4066Sahrens 	if (m < 1)
99625345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
997fa9e4066Sahrens 
998fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
999fa9e4066Sahrens 
1000fa9e4066Sahrens 	for (c = 0; c < m; c++)
100125345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1002fa9e4066Sahrens 
1003fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1004fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1005fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
1006fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1007fa9e4066Sahrens 	    child, m) == 0);
1008fa9e4066Sahrens 
1009fa9e4066Sahrens 	for (c = 0; c < m; c++)
1010fa9e4066Sahrens 		nvlist_free(child[c]);
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
1013fa9e4066Sahrens 
1014fa9e4066Sahrens 	return (mirror);
1015fa9e4066Sahrens }
1016fa9e4066Sahrens 
1017fa9e4066Sahrens static nvlist_t *
101825345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1019663207adSDon Brady     const char *class, int r, int m, int t)
1020fa9e4066Sahrens {
1021fa9e4066Sahrens 	nvlist_t *root, **child;
1022fa9e4066Sahrens 	int c;
1023663207adSDon Brady 	boolean_t log;
1024fa9e4066Sahrens 
1025fa9e4066Sahrens 	ASSERT(t > 0);
1026fa9e4066Sahrens 
1027663207adSDon Brady 	log = (class != NULL && strcmp(class, "log") == 0);
1028663207adSDon Brady 
1029fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1030fa9e4066Sahrens 
103131157203SJeff Bonwick 	for (c = 0; c < t; c++) {
103225345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
103325345e46SGeorge Wilson 		    r, m);
103431157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
103531157203SJeff Bonwick 		    log) == 0);
1036663207adSDon Brady 
1037663207adSDon Brady 		if (class != NULL && class[0] != '\0') {
1038663207adSDon Brady 			ASSERT(m > 1 || log);   /* expecting a mirror */
1039663207adSDon Brady 			VERIFY(nvlist_add_string(child[c],
1040663207adSDon Brady 			    ZPOOL_CONFIG_ALLOCATION_BIAS, class) == 0);
1041663207adSDon Brady 		}
104231157203SJeff Bonwick 	}
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1045fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1046e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1047fa9e4066Sahrens 	    child, t) == 0);
1048fa9e4066Sahrens 
1049fa9e4066Sahrens 	for (c = 0; c < t; c++)
1050fa9e4066Sahrens 		nvlist_free(child[c]);
1051fa9e4066Sahrens 
1052fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
1053fa9e4066Sahrens 
1054fa9e4066Sahrens 	return (root);
1055fa9e4066Sahrens }
1056fa9e4066Sahrens 
105725345e46SGeorge Wilson /*
105825345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
105925345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
106025345e46SGeorge Wilson  */
106125345e46SGeorge Wilson static uint64_t
106225345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
106325345e46SGeorge Wilson {
106425345e46SGeorge Wilson 	uint64_t version = initial_version;
106525345e46SGeorge Wilson 
106625345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
106725345e46SGeorge Wilson 		version = version +
106825345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
106925345e46SGeorge Wilson 	}
107025345e46SGeorge Wilson 
107125345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
107225345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
107325345e46SGeorge Wilson 
107425345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
107525345e46SGeorge Wilson 	return (version);
107625345e46SGeorge Wilson }
107725345e46SGeorge Wilson 
1078b24ab676SJeff Bonwick static int
1079b24ab676SJeff Bonwick ztest_random_blocksize(void)
1080fa9e4066Sahrens {
1081b5152584SMatthew Ahrens 	uint64_t block_shift;
1082663207adSDon Brady 
1083663207adSDon Brady 	ASSERT(ztest_spa->spa_max_ashift != 0);
1084663207adSDon Brady 
1085b5152584SMatthew Ahrens 	/*
1086b5152584SMatthew Ahrens 	 * Choose a block size >= the ashift.
1087b5152584SMatthew Ahrens 	 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1088b5152584SMatthew Ahrens 	 */
1089b5152584SMatthew Ahrens 	int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1090b5152584SMatthew Ahrens 	if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1091b5152584SMatthew Ahrens 		maxbs = 20;
109281cd5c55SMatthew Ahrens 	block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
10932a104a52SAlex Reece 	return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1094b24ab676SJeff Bonwick }
1095fa9e4066Sahrens 
109654811da5SToomas Soome static int
109754811da5SToomas Soome ztest_random_dnodesize(void)
109854811da5SToomas Soome {
109954811da5SToomas Soome 	int slots;
110054811da5SToomas Soome 	int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
110154811da5SToomas Soome 
110254811da5SToomas Soome 	if (max_slots == DNODE_MIN_SLOTS)
110354811da5SToomas Soome 		return (DNODE_MIN_SIZE);
110454811da5SToomas Soome 
110554811da5SToomas Soome 	/*
110654811da5SToomas Soome 	 * Weight the random distribution more heavily toward smaller
110754811da5SToomas Soome 	 * dnode sizes since that is more likely to reflect real-world
110854811da5SToomas Soome 	 * usage.
110954811da5SToomas Soome 	 */
111054811da5SToomas Soome 	ASSERT3U(max_slots, >, 4);
111154811da5SToomas Soome 	switch (ztest_random(10)) {
111254811da5SToomas Soome 	case 0:
111354811da5SToomas Soome 		slots = 5 + ztest_random(max_slots - 4);
111454811da5SToomas Soome 		break;
111554811da5SToomas Soome 	case 1 ... 4:
111654811da5SToomas Soome 		slots = 2 + ztest_random(3);
111754811da5SToomas Soome 		break;
111854811da5SToomas Soome 	default:
111954811da5SToomas Soome 		slots = 1;
112054811da5SToomas Soome 		break;
112154811da5SToomas Soome 	}
112254811da5SToomas Soome 
112354811da5SToomas Soome 	return (slots << DNODE_SHIFT);
112454811da5SToomas Soome }
112554811da5SToomas Soome 
1126b24ab676SJeff Bonwick static int
1127b24ab676SJeff Bonwick ztest_random_ibshift(void)
1128b24ab676SJeff Bonwick {
1129b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
1130b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1131fa9e4066Sahrens }
1132fa9e4066Sahrens 
1133b24ab676SJeff Bonwick static uint64_t
1134b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1135fa9e4066Sahrens {
1136b24ab676SJeff Bonwick 	uint64_t top;
1137b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
1138b24ab676SJeff Bonwick 	vdev_t *tvd;
1139fa9e4066Sahrens 
1140b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1141fa9e4066Sahrens 
1142b24ab676SJeff Bonwick 	do {
1143b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
1144b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
11455cabbc6bSPrashanth Sreenivasa 	} while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1146b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1147fa9e4066Sahrens 
1148b24ab676SJeff Bonwick 	return (top);
1149fa9e4066Sahrens }
1150fa9e4066Sahrens 
1151b24ab676SJeff Bonwick static uint64_t
1152b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1153fa9e4066Sahrens {
1154b24ab676SJeff Bonwick 	uint64_t value;
1155b24ab676SJeff Bonwick 
1156b24ab676SJeff Bonwick 	do {
1157b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1158b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1159b24ab676SJeff Bonwick 
1160b24ab676SJeff Bonwick 	return (value);
1161fa9e4066Sahrens }
1162fa9e4066Sahrens 
1163fa9e4066Sahrens static int
1164b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1165b24ab676SJeff Bonwick     boolean_t inherit)
1166fa9e4066Sahrens {
1167b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1168b24ab676SJeff Bonwick 	const char *valname;
1169b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1170b24ab676SJeff Bonwick 	uint64_t curval;
1171fa9e4066Sahrens 	int error;
1172fa9e4066Sahrens 
11733b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
11743b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1175fa9e4066Sahrens 
1176b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1177b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1178fa9e4066Sahrens 		return (error);
1179fa9e4066Sahrens 	}
1180fb09f5aaSMadhav Suresh 	ASSERT0(error);
1181fa9e4066Sahrens 
11823b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1183b24ab676SJeff Bonwick 
1184420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1185b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1186b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1187b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1188fa9e4066Sahrens 	}
1189fa9e4066Sahrens 
1190fa9e4066Sahrens 	return (error);
1191fa9e4066Sahrens }
1192fa9e4066Sahrens 
1193fa9e4066Sahrens static int
1194420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1195fa9e4066Sahrens {
1196420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1197b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1198fa9e4066Sahrens 	int error;
1199fa9e4066Sahrens 
1200b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1201b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1202fa9e4066Sahrens 
1203b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1204b24ab676SJeff Bonwick 
1205b24ab676SJeff Bonwick 	nvlist_free(props);
1206b24ab676SJeff Bonwick 
1207b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1208b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1209fa9e4066Sahrens 		return (error);
1210fa9e4066Sahrens 	}
1211fb09f5aaSMadhav Suresh 	ASSERT0(error);
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	return (error);
1214fa9e4066Sahrens }
1215fa9e4066Sahrens 
1216b24ab676SJeff Bonwick static void
1217b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1218b24ab676SJeff Bonwick {
1219b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1220b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1221f06dce2cSAndrew Stormont 	mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1222f06dce2cSAndrew Stormont 	cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1223b24ab676SJeff Bonwick }
1224fa9e4066Sahrens 
1225b24ab676SJeff Bonwick static void
1226b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1227fa9e4066Sahrens {
1228b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1229b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1230f06dce2cSAndrew Stormont 	mutex_destroy(&rll->rll_lock);
1231f06dce2cSAndrew Stormont 	cv_destroy(&rll->rll_cv);
1232b24ab676SJeff Bonwick }
1233fa9e4066Sahrens 
1234b24ab676SJeff Bonwick static void
1235b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1236b24ab676SJeff Bonwick {
1237f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1238fa9e4066Sahrens 
1239b24ab676SJeff Bonwick 	if (type == RL_READER) {
1240b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1241f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1242b24ab676SJeff Bonwick 		rll->rll_readers++;
1243b24ab676SJeff Bonwick 	} else {
1244b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1245f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1246b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1247b24ab676SJeff Bonwick 	}
1248fa9e4066Sahrens 
1249f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1250b24ab676SJeff Bonwick }
1251fa9e4066Sahrens 
1252b24ab676SJeff Bonwick static void
1253b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1254b24ab676SJeff Bonwick {
1255f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1256fa9e4066Sahrens 
1257b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1258b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1259b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1260b24ab676SJeff Bonwick 	} else {
1261b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1262b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1263b24ab676SJeff Bonwick 		rll->rll_readers--;
1264b24ab676SJeff Bonwick 	}
1265fa9e4066Sahrens 
1266b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1267f06dce2cSAndrew Stormont 		cv_broadcast(&rll->rll_cv);
1268b24ab676SJeff Bonwick 
1269f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1270fa9e4066Sahrens }
1271fa9e4066Sahrens 
1272b24ab676SJeff Bonwick static void
1273b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
127431157203SJeff Bonwick {
1275b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
127631157203SJeff Bonwick 
1277b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1278b24ab676SJeff Bonwick }
127931157203SJeff Bonwick 
1280b24ab676SJeff Bonwick static void
1281b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1282b24ab676SJeff Bonwick {
1283b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
128431157203SJeff Bonwick 
1285b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
128631157203SJeff Bonwick }
128731157203SJeff Bonwick 
1288b24ab676SJeff Bonwick static rl_t *
1289b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1290b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
129188ecc943SGeorge Wilson {
1292b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1293b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1294b24ab676SJeff Bonwick 	rl_t *rl;
129588ecc943SGeorge Wilson 
1296b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1297b24ab676SJeff Bonwick 	rl->rl_object = object;
1298b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1299b24ab676SJeff Bonwick 	rl->rl_size = size;
1300b24ab676SJeff Bonwick 	rl->rl_lock = rll;
130188ecc943SGeorge Wilson 
1302b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
130388ecc943SGeorge Wilson 
1304b24ab676SJeff Bonwick 	return (rl);
130588ecc943SGeorge Wilson }
130688ecc943SGeorge Wilson 
1307b24ab676SJeff Bonwick static void
1308b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1309fa9e4066Sahrens {
1310b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1311fa9e4066Sahrens 
1312b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1313fa9e4066Sahrens 
1314b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1315b24ab676SJeff Bonwick }
1316fa9e4066Sahrens 
1317b24ab676SJeff Bonwick static void
1318420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1319b24ab676SJeff Bonwick {
1320b24ab676SJeff Bonwick 	zd->zd_os = os;
1321b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1322420dfc95SChris Siden 	zd->zd_shared = szd;
1323b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1324fa9e4066Sahrens 
1325420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1326420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1327420dfc95SChris Siden 
1328f06dce2cSAndrew Stormont 	rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1329f06dce2cSAndrew Stormont 	mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1330b24ab676SJeff Bonwick 
1331b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1332b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1333b24ab676SJeff Bonwick 
1334b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1335b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1336b24ab676SJeff Bonwick }
1337b24ab676SJeff Bonwick 
1338b24ab676SJeff Bonwick static void
1339b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1340b24ab676SJeff Bonwick {
1341f06dce2cSAndrew Stormont 	mutex_destroy(&zd->zd_dirobj_lock);
1342b24ab676SJeff Bonwick 
1343b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1344b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1345b24ab676SJeff Bonwick 
1346b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1347b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1348b24ab676SJeff Bonwick }
1349b24ab676SJeff Bonwick 
1350b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1351b24ab676SJeff Bonwick 
1352b24ab676SJeff Bonwick static uint64_t
1353b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1354b24ab676SJeff Bonwick {
1355b24ab676SJeff Bonwick 	uint64_t txg;
1356b24ab676SJeff Bonwick 	int error;
1357b24ab676SJeff Bonwick 
1358b24ab676SJeff Bonwick 	/*
1359b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1360b24ab676SJeff Bonwick 	 */
1361b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1362b24ab676SJeff Bonwick 	if (error) {
1363b24ab676SJeff Bonwick 		if (error == ERESTART) {
1364b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1365b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1366b24ab676SJeff Bonwick 		} else {
1367b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1368b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1369b24ab676SJeff Bonwick 		}
1370b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1371b24ab676SJeff Bonwick 		return (0);
1372b24ab676SJeff Bonwick 	}
1373b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1374b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1375b24ab676SJeff Bonwick 	return (txg);
1376b24ab676SJeff Bonwick }
1377b24ab676SJeff Bonwick 
1378b24ab676SJeff Bonwick static void
1379b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1380b24ab676SJeff Bonwick {
1381b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1382b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1383b24ab676SJeff Bonwick 
1384b24ab676SJeff Bonwick 	while (ip < ip_end)
1385b24ab676SJeff Bonwick 		*ip++ = value;
1386b24ab676SJeff Bonwick }
1387b24ab676SJeff Bonwick 
1388b24ab676SJeff Bonwick static boolean_t
1389b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1390b24ab676SJeff Bonwick {
1391b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1392b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1393b24ab676SJeff Bonwick 	uint64_t diff = 0;
1394b24ab676SJeff Bonwick 
1395b24ab676SJeff Bonwick 	while (ip < ip_end)
1396b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1397b24ab676SJeff Bonwick 
1398b24ab676SJeff Bonwick 	return (diff == 0);
1399b24ab676SJeff Bonwick }
1400b24ab676SJeff Bonwick 
1401b24ab676SJeff Bonwick static void
1402b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
140354811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
140454811da5SToomas Soome     uint64_t crtxg)
1405b24ab676SJeff Bonwick {
1406b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1407b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1408b24ab676SJeff Bonwick 	bt->bt_object = object;
140954811da5SToomas Soome 	bt->bt_dnodesize = dnodesize;
1410b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1411b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1412b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1413b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1414b24ab676SJeff Bonwick }
1415b24ab676SJeff Bonwick 
1416b24ab676SJeff Bonwick static void
1417b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
141854811da5SToomas Soome     uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
141954811da5SToomas Soome     uint64_t crtxg)
1420b24ab676SJeff Bonwick {
14215d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
14225d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
14235d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
142454811da5SToomas Soome 	ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
14255d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
14265d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
14275d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
14285d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1429b24ab676SJeff Bonwick }
1430b24ab676SJeff Bonwick 
1431b24ab676SJeff Bonwick static ztest_block_tag_t *
1432b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1433b24ab676SJeff Bonwick {
1434b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1435b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1436b24ab676SJeff Bonwick 
1437b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1438b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1439b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1440b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1441b24ab676SJeff Bonwick 
1442b24ab676SJeff Bonwick 	return (bt);
1443b24ab676SJeff Bonwick }
1444b24ab676SJeff Bonwick 
144554811da5SToomas Soome /*
144654811da5SToomas Soome  * Generate a token to fill up unused bonus buffer space.  Try to make
144754811da5SToomas Soome  * it unique to the object, generation, and offset to verify that data
144854811da5SToomas Soome  * is not getting overwritten by data from other dnodes.
144954811da5SToomas Soome  */
145054811da5SToomas Soome #define	ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset)	\
145154811da5SToomas Soome 	(((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
145254811da5SToomas Soome 
145354811da5SToomas Soome /*
145454811da5SToomas Soome  * Fill up the unused bonus buffer region before the block tag with a
145554811da5SToomas Soome  * verifiable pattern. Filling the whole bonus area with non-zero data
145654811da5SToomas Soome  * helps ensure that all dnode traversal code properly skips the
145754811da5SToomas Soome  * interior regions of large dnodes.
145854811da5SToomas Soome  */
145954811da5SToomas Soome void
146054811da5SToomas Soome ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
146154811da5SToomas Soome     objset_t *os, uint64_t gen)
146254811da5SToomas Soome {
146354811da5SToomas Soome 	uint64_t *bonusp;
146454811da5SToomas Soome 
146554811da5SToomas Soome 	ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
146654811da5SToomas Soome 
146754811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
146854811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
146954811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
147054811da5SToomas Soome 		*bonusp = token;
147154811da5SToomas Soome 	}
147254811da5SToomas Soome }
147354811da5SToomas Soome 
147454811da5SToomas Soome /*
147554811da5SToomas Soome  * Verify that the unused area of a bonus buffer is filled with the
147654811da5SToomas Soome  * expected tokens.
147754811da5SToomas Soome  */
147854811da5SToomas Soome void
147954811da5SToomas Soome ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
148054811da5SToomas Soome     objset_t *os, uint64_t gen)
148154811da5SToomas Soome {
148254811da5SToomas Soome 	uint64_t *bonusp;
148354811da5SToomas Soome 
148454811da5SToomas Soome 	for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
148554811da5SToomas Soome 		uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
148654811da5SToomas Soome 		    gen, bonusp - (uint64_t *)db->db_data);
148754811da5SToomas Soome 		VERIFY3U(*bonusp, ==, token);
148854811da5SToomas Soome 	}
148954811da5SToomas Soome }
149054811da5SToomas Soome 
1491b24ab676SJeff Bonwick /*
1492b24ab676SJeff Bonwick  * ZIL logging ops
1493b24ab676SJeff Bonwick  */
1494b24ab676SJeff Bonwick 
1495b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1496b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1497b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1498b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
149954811da5SToomas Soome #define	lrz_dnodesize	lr_crtime[1]
1500b24ab676SJeff Bonwick 
15015002558fSNeil Perrin static void
1502b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1503b24ab676SJeff Bonwick {
1504b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1505b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1506b24ab676SJeff Bonwick 	itx_t *itx;
1507b24ab676SJeff Bonwick 
1508b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15095002558fSNeil Perrin 		return;
1510b24ab676SJeff Bonwick 
1511b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1512b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1513b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1514b24ab676SJeff Bonwick 
15155002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1516b24ab676SJeff Bonwick }
1517b24ab676SJeff Bonwick 
15185002558fSNeil Perrin static void
15195002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1520b24ab676SJeff Bonwick {
1521b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1522b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1523b24ab676SJeff Bonwick 	itx_t *itx;
1524b24ab676SJeff Bonwick 
1525b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15265002558fSNeil Perrin 		return;
1527b24ab676SJeff Bonwick 
1528b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1529b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1530b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1531b24ab676SJeff Bonwick 
153251bd2f97SNeil Perrin 	itx->itx_oid = object;
15335002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1534b24ab676SJeff Bonwick }
1535b24ab676SJeff Bonwick 
15365002558fSNeil Perrin static void
1537b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1538b24ab676SJeff Bonwick {
1539b24ab676SJeff Bonwick 	itx_t *itx;
1540b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1541b24ab676SJeff Bonwick 
1542b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15435002558fSNeil Perrin 		return;
1544b24ab676SJeff Bonwick 
1545b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1546b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1547b24ab676SJeff Bonwick 
1548b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1549b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1550b24ab676SJeff Bonwick 
1551b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1552b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1553b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1554b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1555b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1556b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1557b24ab676SJeff Bonwick 	}
1558b24ab676SJeff Bonwick 	itx->itx_private = zd;
1559b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1560b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1561b24ab676SJeff Bonwick 
1562b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1563b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1564b24ab676SJeff Bonwick 
15655002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1566b24ab676SJeff Bonwick }
1567b24ab676SJeff Bonwick 
15685002558fSNeil Perrin static void
1569b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1570b24ab676SJeff Bonwick {
1571b24ab676SJeff Bonwick 	itx_t *itx;
1572b24ab676SJeff Bonwick 
1573b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15745002558fSNeil Perrin 		return;
1575b24ab676SJeff Bonwick 
1576b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1577b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1578b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1579b24ab676SJeff Bonwick 
15805002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
15815002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1582b24ab676SJeff Bonwick }
1583b24ab676SJeff Bonwick 
15845002558fSNeil Perrin static void
1585b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1586b24ab676SJeff Bonwick {
1587b24ab676SJeff Bonwick 	itx_t *itx;
1588b24ab676SJeff Bonwick 
1589b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
15905002558fSNeil Perrin 		return;
1591b24ab676SJeff Bonwick 
1592b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1593b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1594b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1595b24ab676SJeff Bonwick 
15965002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
15975002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1598b24ab676SJeff Bonwick }
1599b24ab676SJeff Bonwick 
1600b24ab676SJeff Bonwick /*
1601b24ab676SJeff Bonwick  * ZIL replay ops
1602b24ab676SJeff Bonwick  */
1603b24ab676SJeff Bonwick static int
16043f7978d0SAlan Somers ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1605b24ab676SJeff Bonwick {
16063f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
16073f7978d0SAlan Somers 	lr_create_t *lr = arg2;
1608b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1609b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1610b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1611b24ab676SJeff Bonwick 	dmu_buf_t *db;
1612b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1613b24ab676SJeff Bonwick 	uint64_t txg;
1614b24ab676SJeff Bonwick 	int error = 0;
161554811da5SToomas Soome 	int bonuslen;
1616b24ab676SJeff Bonwick 
1617b24ab676SJeff Bonwick 	if (byteswap)
1618b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1619b24ab676SJeff Bonwick 
1620b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1621b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1622b24ab676SJeff Bonwick 
1623b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1624b24ab676SJeff Bonwick 
1625b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1626b24ab676SJeff Bonwick 
1627b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1628b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1629b24ab676SJeff Bonwick 	} else {
1630b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1631b24ab676SJeff Bonwick 	}
1632b24ab676SJeff Bonwick 
1633b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1634b24ab676SJeff Bonwick 	if (txg == 0)
1635b24ab676SJeff Bonwick 		return (ENOSPC);
1636b24ab676SJeff Bonwick 
1637b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
163854811da5SToomas Soome 	bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1639b24ab676SJeff Bonwick 
1640b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1641b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
164254811da5SToomas Soome 			lr->lr_foid = zap_create_dnsize(os,
1643b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
164454811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1645b24ab676SJeff Bonwick 		} else {
164654811da5SToomas Soome 			error = zap_create_claim_dnsize(os, lr->lr_foid,
1647b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
164854811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1649b24ab676SJeff Bonwick 		}
1650b24ab676SJeff Bonwick 	} else {
1651b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
165254811da5SToomas Soome 			lr->lr_foid = dmu_object_alloc_dnsize(os,
1653b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
165454811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1655b24ab676SJeff Bonwick 		} else {
165654811da5SToomas Soome 			error = dmu_object_claim_dnsize(os, lr->lr_foid,
1657b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
165854811da5SToomas Soome 			    bonuslen, lr->lrz_dnodesize, tx);
1659b24ab676SJeff Bonwick 		}
1660b24ab676SJeff Bonwick 	}
1661b24ab676SJeff Bonwick 
1662b24ab676SJeff Bonwick 	if (error) {
1663b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1664b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1665b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1666b24ab676SJeff Bonwick 		return (error);
1667b24ab676SJeff Bonwick 	}
1668b24ab676SJeff Bonwick 
1669b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1670b24ab676SJeff Bonwick 
1671b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1672b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1673b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1674b24ab676SJeff Bonwick 
1675b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1676b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1677b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
167854811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
167954811da5SToomas Soome 	    lr->lr_gen, txg, txg);
168054811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1681b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1682b24ab676SJeff Bonwick 
1683b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1684b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1685b24ab676SJeff Bonwick 
1686b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1687b24ab676SJeff Bonwick 
1688b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1689b24ab676SJeff Bonwick 
1690b24ab676SJeff Bonwick 	return (0);
1691b24ab676SJeff Bonwick }
1692b24ab676SJeff Bonwick 
1693b24ab676SJeff Bonwick static int
16943f7978d0SAlan Somers ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1695b24ab676SJeff Bonwick {
16963f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
16973f7978d0SAlan Somers 	lr_remove_t *lr = arg2;
1698b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1699b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1700b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1701b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1702b24ab676SJeff Bonwick 	uint64_t object, txg;
1703b24ab676SJeff Bonwick 
1704b24ab676SJeff Bonwick 	if (byteswap)
1705b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1706b24ab676SJeff Bonwick 
1707b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1708b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1709b24ab676SJeff Bonwick 
1710b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1711b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1712b24ab676SJeff Bonwick 	ASSERT(object != 0);
1713b24ab676SJeff Bonwick 
1714b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1715b24ab676SJeff Bonwick 
1716b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1717b24ab676SJeff Bonwick 
1718b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1719b24ab676SJeff Bonwick 
1720b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1721b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1722b24ab676SJeff Bonwick 
1723b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1724b24ab676SJeff Bonwick 	if (txg == 0) {
1725b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1726b24ab676SJeff Bonwick 		return (ENOSPC);
1727b24ab676SJeff Bonwick 	}
1728b24ab676SJeff Bonwick 
1729b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1730b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1731b24ab676SJeff Bonwick 	} else {
1732b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1733b24ab676SJeff Bonwick 	}
1734b24ab676SJeff Bonwick 
1735b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1736b24ab676SJeff Bonwick 
17375002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1738b24ab676SJeff Bonwick 
1739b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1740b24ab676SJeff Bonwick 
1741b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1742b24ab676SJeff Bonwick 
1743b24ab676SJeff Bonwick 	return (0);
1744b24ab676SJeff Bonwick }
1745b24ab676SJeff Bonwick 
1746b24ab676SJeff Bonwick static int
17473f7978d0SAlan Somers ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1748b24ab676SJeff Bonwick {
17493f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17503f7978d0SAlan Somers 	lr_write_t *lr = arg2;
1751b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1752b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1753b24ab676SJeff Bonwick 	uint64_t offset, length;
1754b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1755b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1756b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1757b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1758b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1759b24ab676SJeff Bonwick 	dmu_buf_t *db;
1760b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1761b24ab676SJeff Bonwick 	rl_t *rl;
1762b24ab676SJeff Bonwick 
1763b24ab676SJeff Bonwick 	if (byteswap)
1764b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1765b24ab676SJeff Bonwick 
1766b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1767b24ab676SJeff Bonwick 	length = lr->lr_length;
1768b24ab676SJeff Bonwick 
1769b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1770b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1771b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1772b24ab676SJeff Bonwick 		if (length < blocksize) {
1773b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1774b24ab676SJeff Bonwick 			length = blocksize;
1775b24ab676SJeff Bonwick 		}
1776b24ab676SJeff Bonwick 	}
1777b24ab676SJeff Bonwick 
1778b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1779b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1780b24ab676SJeff Bonwick 
1781b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1782b24ab676SJeff Bonwick 		bt = NULL;
1783b24ab676SJeff Bonwick 
1784b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1785b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1786b24ab676SJeff Bonwick 
1787b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1788b24ab676SJeff Bonwick 
1789b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1790b24ab676SJeff Bonwick 
1791b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1792b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1793b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1794b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1795b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1796b24ab676SJeff Bonwick 
1797b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1798b24ab676SJeff Bonwick 
1799b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1800b24ab676SJeff Bonwick 
1801b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1802b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1803b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1804b24ab676SJeff Bonwick 
1805b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1806b24ab676SJeff Bonwick 	if (txg == 0) {
1807b24ab676SJeff Bonwick 		if (abuf != NULL)
1808b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1809b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1810b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1811b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1812b24ab676SJeff Bonwick 		return (ENOSPC);
1813b24ab676SJeff Bonwick 	}
1814b24ab676SJeff Bonwick 
1815b24ab676SJeff Bonwick 	if (bt != NULL) {
1816b24ab676SJeff Bonwick 		/*
1817b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1818b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1819b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1820b24ab676SJeff Bonwick 		 */
1821b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1822b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1823b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1824b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1825b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1826b24ab676SJeff Bonwick 
1827b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1828b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1829b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
183054811da5SToomas Soome 				ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1831b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1832b24ab676SJeff Bonwick 			}
1833b24ab676SJeff Bonwick 		}
1834b24ab676SJeff Bonwick 
1835b24ab676SJeff Bonwick 		/*
1836b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1837b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1838b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1839b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1840b24ab676SJeff Bonwick 		 */
1841b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
184254811da5SToomas Soome 			ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1843b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1844b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1845b24ab676SJeff Bonwick 		}
1846b24ab676SJeff Bonwick 
1847b24ab676SJeff Bonwick 		/*
1848b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1849b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1850b24ab676SJeff Bonwick 		 */
185154811da5SToomas Soome 		ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
185254811da5SToomas Soome 		    crtxg);
1853b24ab676SJeff Bonwick 	}
1854b24ab676SJeff Bonwick 
1855b24ab676SJeff Bonwick 	if (abuf == NULL) {
1856b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1857b24ab676SJeff Bonwick 	} else {
1858b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1859b24ab676SJeff Bonwick 		dmu_assign_arcbuf(db, offset, abuf, tx);
1860b24ab676SJeff Bonwick 	}
1861b24ab676SJeff Bonwick 
1862b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1863b24ab676SJeff Bonwick 
1864b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1865b24ab676SJeff Bonwick 
1866b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1867b24ab676SJeff Bonwick 
1868b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1869b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1870b24ab676SJeff Bonwick 
1871b24ab676SJeff Bonwick 	return (0);
1872b24ab676SJeff Bonwick }
1873b24ab676SJeff Bonwick 
1874b24ab676SJeff Bonwick static int
18753f7978d0SAlan Somers ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1876b24ab676SJeff Bonwick {
18773f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
18783f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
1879b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1880b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1881b24ab676SJeff Bonwick 	uint64_t txg;
1882b24ab676SJeff Bonwick 	rl_t *rl;
1883b24ab676SJeff Bonwick 
1884b24ab676SJeff Bonwick 	if (byteswap)
1885b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1886b24ab676SJeff Bonwick 
1887b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1888b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1889b24ab676SJeff Bonwick 	    RL_WRITER);
1890b24ab676SJeff Bonwick 
1891b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1892b24ab676SJeff Bonwick 
1893b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1894b24ab676SJeff Bonwick 
1895b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1896b24ab676SJeff Bonwick 	if (txg == 0) {
1897b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1898b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1899b24ab676SJeff Bonwick 		return (ENOSPC);
1900b24ab676SJeff Bonwick 	}
1901b24ab676SJeff Bonwick 
1902b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1903b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1904b24ab676SJeff Bonwick 
1905b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1906b24ab676SJeff Bonwick 
1907b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1908b24ab676SJeff Bonwick 
1909b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1910b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1911b24ab676SJeff Bonwick 
1912b24ab676SJeff Bonwick 	return (0);
1913b24ab676SJeff Bonwick }
1914b24ab676SJeff Bonwick 
1915b24ab676SJeff Bonwick static int
19163f7978d0SAlan Somers ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1917b24ab676SJeff Bonwick {
19183f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
19193f7978d0SAlan Somers 	lr_setattr_t *lr = arg2;
1920b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1921b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1922b24ab676SJeff Bonwick 	dmu_buf_t *db;
1923b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
192454811da5SToomas Soome 	uint64_t txg, lrtxg, crtxg, dnodesize;
1925b24ab676SJeff Bonwick 
1926b24ab676SJeff Bonwick 	if (byteswap)
1927b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1928b24ab676SJeff Bonwick 
1929b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1930b24ab676SJeff Bonwick 
1931b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1932b24ab676SJeff Bonwick 
1933b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1934b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1935b24ab676SJeff Bonwick 
1936b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1937b24ab676SJeff Bonwick 	if (txg == 0) {
1938b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1939b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1940b24ab676SJeff Bonwick 		return (ENOSPC);
1941b24ab676SJeff Bonwick 	}
1942b24ab676SJeff Bonwick 
1943b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1944b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1945b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1946b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
194754811da5SToomas Soome 	dnodesize = bbt->bt_dnodesize;
1948b24ab676SJeff Bonwick 
1949b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1950b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1951b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1952b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1953b24ab676SJeff Bonwick 	} else {
1954b24ab676SJeff Bonwick 		/*
1955b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1956b24ab676SJeff Bonwick 		 */
1957b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1958b24ab676SJeff Bonwick 		    sizeof (*bbt);
1959b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
1960b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
1961b24ab676SJeff Bonwick 	}
1962b24ab676SJeff Bonwick 
1963b24ab676SJeff Bonwick 	/*
1964b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
1965b24ab676SJeff Bonwick 	 */
196654811da5SToomas Soome 	ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1967b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
1968b24ab676SJeff Bonwick 
1969b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1970b24ab676SJeff Bonwick 
1971b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1972b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
1973fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1974b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1975b24ab676SJeff Bonwick 
197654811da5SToomas Soome 	ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
197754811da5SToomas Soome 	    txg, crtxg);
197854811da5SToomas Soome 	ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1979b24ab676SJeff Bonwick 
1980b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1981b24ab676SJeff Bonwick 
1982b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
1983b24ab676SJeff Bonwick 
1984b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1985b24ab676SJeff Bonwick 
1986b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1987b24ab676SJeff Bonwick 
1988b24ab676SJeff Bonwick 	return (0);
1989b24ab676SJeff Bonwick }
1990b24ab676SJeff Bonwick 
1991b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1992b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
1993b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
1994b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
1995b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
1996b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
1997b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
1998b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
1999b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
2000b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
2001b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
2002b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
2003b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
2004b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
2005b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
2006b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
2007b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
2008b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
2009b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
2010b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
2011b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
2012b24ab676SJeff Bonwick };
2013b24ab676SJeff Bonwick 
2014b24ab676SJeff Bonwick /*
2015b24ab676SJeff Bonwick  * ZIL get_data callbacks
2016b24ab676SJeff Bonwick  */
2017b24ab676SJeff Bonwick 
2018cab3a55eSPrakash Surya /* ARGSUSED */
2019b24ab676SJeff Bonwick static void
2020b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
2021b24ab676SJeff Bonwick {
2022b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
202379315247SMatthew Ahrens 	uint64_t object = ((rl_t *)zgd->zgd_lr)->rl_object;
2024b24ab676SJeff Bonwick 
2025b24ab676SJeff Bonwick 	if (zgd->zgd_db)
2026b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
2027b24ab676SJeff Bonwick 
202879315247SMatthew Ahrens 	ztest_range_unlock((rl_t *)zgd->zgd_lr);
2029b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2030b24ab676SJeff Bonwick 
2031b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
2032b24ab676SJeff Bonwick }
2033b24ab676SJeff Bonwick 
2034b24ab676SJeff Bonwick static int
20351271e4b1SPrakash Surya ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
20361271e4b1SPrakash Surya     zio_t *zio)
2037b24ab676SJeff Bonwick {
2038b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
2039b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2040b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
2041b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
2042b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
2043b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
2044b24ab676SJeff Bonwick 	uint64_t crtxg;
2045b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2046b24ab676SJeff Bonwick 	dmu_buf_t *db;
2047b24ab676SJeff Bonwick 	zgd_t *zgd;
2048b24ab676SJeff Bonwick 	int error;
2049b24ab676SJeff Bonwick 
20501271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
20511271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
20521271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
20531271e4b1SPrakash Surya 
2054b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2055b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
2056b24ab676SJeff Bonwick 	if (error) {
2057b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2058b24ab676SJeff Bonwick 		return (error);
2059b24ab676SJeff Bonwick 	}
2060b24ab676SJeff Bonwick 
2061b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
2062b24ab676SJeff Bonwick 
2063b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
2064b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
2065b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
2066b24ab676SJeff Bonwick 		return (ENOENT);
2067b24ab676SJeff Bonwick 	}
2068b24ab676SJeff Bonwick 
2069b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
2070b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
2071b24ab676SJeff Bonwick 	db = NULL;
2072b24ab676SJeff Bonwick 
2073b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
20741271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
2075b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
2076b24ab676SJeff Bonwick 
2077b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
207879315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
207979315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2080b24ab676SJeff Bonwick 
2081b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
2082b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2083b24ab676SJeff Bonwick 		ASSERT(error == 0);
2084b24ab676SJeff Bonwick 	} else {
2085b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
2086dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
2087b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
2088dfe73b3dSJeff Bonwick 		} else {
2089dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
2090dfe73b3dSJeff Bonwick 			offset = 0;
2091dfe73b3dSJeff Bonwick 		}
2092b24ab676SJeff Bonwick 
209379315247SMatthew Ahrens 		zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
209479315247SMatthew Ahrens 		    object, offset, size, RL_READER);
2095b24ab676SJeff Bonwick 
209647cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
209747cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
2098b24ab676SJeff Bonwick 
2099b24ab676SJeff Bonwick 		if (error == 0) {
2100b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
210180901aeaSGeorge Wilson 
2102b24ab676SJeff Bonwick 			zgd->zgd_db = db;
2103b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
2104b24ab676SJeff Bonwick 
2105b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
2106b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
2107b24ab676SJeff Bonwick 
2108b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
2109b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
2110b24ab676SJeff Bonwick 
2111b24ab676SJeff Bonwick 			if (error == 0)
2112b24ab676SJeff Bonwick 				return (0);
2113b24ab676SJeff Bonwick 		}
2114b24ab676SJeff Bonwick 	}
2115b24ab676SJeff Bonwick 
2116b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
2117b24ab676SJeff Bonwick 
2118b24ab676SJeff Bonwick 	return (error);
2119b24ab676SJeff Bonwick }
2120b24ab676SJeff Bonwick 
2121b24ab676SJeff Bonwick static void *
2122b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
2123b24ab676SJeff Bonwick {
2124b24ab676SJeff Bonwick 	char *lr;
2125b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2126b24ab676SJeff Bonwick 
2127b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2128b24ab676SJeff Bonwick 
2129b24ab676SJeff Bonwick 	if (name)
2130b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
2131b24ab676SJeff Bonwick 
2132b24ab676SJeff Bonwick 	return (lr);
2133b24ab676SJeff Bonwick }
2134b24ab676SJeff Bonwick 
2135b24ab676SJeff Bonwick void
2136b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
2137b24ab676SJeff Bonwick {
2138b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
2139b24ab676SJeff Bonwick 
2140b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
2141b24ab676SJeff Bonwick }
2142b24ab676SJeff Bonwick 
2143b24ab676SJeff Bonwick /*
2144b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
2145b24ab676SJeff Bonwick  */
2146b24ab676SJeff Bonwick static int
2147b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2148b24ab676SJeff Bonwick {
2149b24ab676SJeff Bonwick 	int missing = 0;
2150b24ab676SJeff Bonwick 	int error;
2151b24ab676SJeff Bonwick 
2152f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2153b24ab676SJeff Bonwick 
2154b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2155b24ab676SJeff Bonwick 		od->od_object = 0;
2156b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2157b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
2158b24ab676SJeff Bonwick 		if (error) {
2159b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
2160b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
2161b24ab676SJeff Bonwick 			missing++;
2162b24ab676SJeff Bonwick 		} else {
2163b24ab676SJeff Bonwick 			dmu_buf_t *db;
2164b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
2165b24ab676SJeff Bonwick 			dmu_object_info_t doi;
2166b24ab676SJeff Bonwick 
2167b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2168b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
2169b24ab676SJeff Bonwick 
2170b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
2171b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2172b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
2173b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
2174b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
2175b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2176b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
2177b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
2178b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
2179b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
2180b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
2181b24ab676SJeff Bonwick 		}
2182b24ab676SJeff Bonwick 	}
2183b24ab676SJeff Bonwick 
2184b24ab676SJeff Bonwick 	return (missing);
2185b24ab676SJeff Bonwick }
2186b24ab676SJeff Bonwick 
2187b24ab676SJeff Bonwick static int
2188b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2189b24ab676SJeff Bonwick {
2190b24ab676SJeff Bonwick 	int missing = 0;
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 		if (missing) {
2196b24ab676SJeff Bonwick 			od->od_object = 0;
2197b24ab676SJeff Bonwick 			missing++;
2198b24ab676SJeff Bonwick 			continue;
2199b24ab676SJeff Bonwick 		}
2200b24ab676SJeff Bonwick 
2201b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2202b24ab676SJeff Bonwick 
2203b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2204b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
2205b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
2206b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
2207b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
2208b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
220954811da5SToomas Soome 		lr->lrz_dnodesize = od->od_crdnodesize;
2210b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
2211b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
2212b24ab676SJeff Bonwick 
2213b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2214b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2215b24ab676SJeff Bonwick 			od->od_object = 0;
2216b24ab676SJeff Bonwick 			missing++;
2217b24ab676SJeff Bonwick 		} else {
2218b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2219b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2220b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2221b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2222b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2223b24ab676SJeff Bonwick 		}
2224b24ab676SJeff Bonwick 
2225b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2226b24ab676SJeff Bonwick 	}
2227b24ab676SJeff Bonwick 
2228b24ab676SJeff Bonwick 	return (missing);
2229b24ab676SJeff Bonwick }
2230b24ab676SJeff Bonwick 
2231b24ab676SJeff Bonwick static int
2232b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2233b24ab676SJeff Bonwick {
2234b24ab676SJeff Bonwick 	int missing = 0;
2235b24ab676SJeff Bonwick 	int error;
2236b24ab676SJeff Bonwick 
2237f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2238b24ab676SJeff Bonwick 
2239b24ab676SJeff Bonwick 	od += count - 1;
2240b24ab676SJeff Bonwick 
2241b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2242b24ab676SJeff Bonwick 		if (missing) {
2243b24ab676SJeff Bonwick 			missing++;
2244b24ab676SJeff Bonwick 			continue;
2245b24ab676SJeff Bonwick 		}
2246b24ab676SJeff Bonwick 
224780901aeaSGeorge Wilson 		/*
224880901aeaSGeorge Wilson 		 * No object was found.
224980901aeaSGeorge Wilson 		 */
2250b24ab676SJeff Bonwick 		if (od->od_object == 0)
2251b24ab676SJeff Bonwick 			continue;
2252b24ab676SJeff Bonwick 
2253b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2254b24ab676SJeff Bonwick 
2255b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2256b24ab676SJeff Bonwick 
2257b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2258b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2259b24ab676SJeff Bonwick 			missing++;
2260b24ab676SJeff Bonwick 		} else {
2261b24ab676SJeff Bonwick 			od->od_object = 0;
2262b24ab676SJeff Bonwick 		}
2263b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2264b24ab676SJeff Bonwick 	}
2265b24ab676SJeff Bonwick 
2266b24ab676SJeff Bonwick 	return (missing);
2267b24ab676SJeff Bonwick }
2268b24ab676SJeff Bonwick 
2269b24ab676SJeff Bonwick static int
2270b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2271b24ab676SJeff Bonwick     void *data)
2272b24ab676SJeff Bonwick {
2273b24ab676SJeff Bonwick 	lr_write_t *lr;
2274b24ab676SJeff Bonwick 	int error;
2275b24ab676SJeff Bonwick 
2276b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2277b24ab676SJeff Bonwick 
2278b24ab676SJeff Bonwick 	lr->lr_foid = object;
2279b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2280b24ab676SJeff Bonwick 	lr->lr_length = size;
2281b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2282b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2283b24ab676SJeff Bonwick 
2284b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2285b24ab676SJeff Bonwick 
2286b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2287b24ab676SJeff Bonwick 
2288b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2289b24ab676SJeff Bonwick 
2290b24ab676SJeff Bonwick 	return (error);
2291b24ab676SJeff Bonwick }
2292b24ab676SJeff Bonwick 
2293b24ab676SJeff Bonwick static int
2294b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2295b24ab676SJeff Bonwick {
2296b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2297b24ab676SJeff Bonwick 	int error;
2298b24ab676SJeff Bonwick 
2299b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2300b24ab676SJeff Bonwick 
2301b24ab676SJeff Bonwick 	lr->lr_foid = object;
2302b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2303b24ab676SJeff Bonwick 	lr->lr_length = size;
2304b24ab676SJeff Bonwick 
2305b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2306b24ab676SJeff Bonwick 
2307b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2308b24ab676SJeff Bonwick 
2309b24ab676SJeff Bonwick 	return (error);
2310b24ab676SJeff Bonwick }
2311b24ab676SJeff Bonwick 
2312b24ab676SJeff Bonwick static int
2313b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2314b24ab676SJeff Bonwick {
2315b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2316b24ab676SJeff Bonwick 	int error;
2317b24ab676SJeff Bonwick 
2318b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2319b24ab676SJeff Bonwick 
2320b24ab676SJeff Bonwick 	lr->lr_foid = object;
2321b24ab676SJeff Bonwick 	lr->lr_size = 0;
2322b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2323b24ab676SJeff Bonwick 
2324b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2325b24ab676SJeff Bonwick 
2326b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2327b24ab676SJeff Bonwick 
2328b24ab676SJeff Bonwick 	return (error);
2329b24ab676SJeff Bonwick }
2330b24ab676SJeff Bonwick 
2331b24ab676SJeff Bonwick static void
2332b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2333b24ab676SJeff Bonwick {
2334b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2335b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2336b24ab676SJeff Bonwick 	uint64_t txg;
2337b24ab676SJeff Bonwick 	rl_t *rl;
2338b24ab676SJeff Bonwick 
2339b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2340b24ab676SJeff Bonwick 
2341b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2342b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2343b24ab676SJeff Bonwick 
2344b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2345b24ab676SJeff Bonwick 
2346b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2347b24ab676SJeff Bonwick 
2348b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2349b24ab676SJeff Bonwick 
2350b24ab676SJeff Bonwick 	if (txg != 0) {
2351b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2352b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2353b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2354b24ab676SJeff Bonwick 	} else {
2355b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2356b24ab676SJeff Bonwick 	}
2357b24ab676SJeff Bonwick 
2358b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2359b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2360b24ab676SJeff Bonwick }
2361b24ab676SJeff Bonwick 
2362b24ab676SJeff Bonwick static void
2363b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2364b24ab676SJeff Bonwick {
236580901aeaSGeorge Wilson 	int err;
2366b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2367b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2368b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2369b24ab676SJeff Bonwick 	uint64_t blocksize;
2370b24ab676SJeff Bonwick 	void *data;
2371b24ab676SJeff Bonwick 
2372b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2373b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2374b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2375b24ab676SJeff Bonwick 
2376b24ab676SJeff Bonwick 	/*
2377b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2378b24ab676SJeff Bonwick 	 */
2379b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2380b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2381b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2382b24ab676SJeff Bonwick 
2383f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2384c9ba2a43SEric Schrock 
2385b24ab676SJeff Bonwick 	switch (io_type) {
2386b24ab676SJeff Bonwick 
2387b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
238854811da5SToomas Soome 		ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
238954811da5SToomas Soome 		    offset, 0, 0, 0);
2390b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2391b24ab676SJeff Bonwick 		break;
2392b24ab676SJeff Bonwick 
2393b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2394b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2395b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2396b24ab676SJeff Bonwick 			/*
2397b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2398b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2399b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2400b24ab676SJeff Bonwick 			 */
2401b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2402b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2403b24ab676SJeff Bonwick 		}
2404b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2405b24ab676SJeff Bonwick 		break;
2406b24ab676SJeff Bonwick 
2407b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2408b24ab676SJeff Bonwick 		bzero(data, blocksize);
2409b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2410b24ab676SJeff Bonwick 		break;
2411b24ab676SJeff Bonwick 
2412b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2413b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2414b24ab676SJeff Bonwick 		break;
2415b24ab676SJeff Bonwick 
2416b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2417b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2418b24ab676SJeff Bonwick 		break;
241980901aeaSGeorge Wilson 
242080901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
2421f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_READER);
242280901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
242380901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
242480901aeaSGeorge Wilson 		    B_FALSE);
242580901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
242680901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
242780901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
242880901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
242980901aeaSGeorge Wilson 		    B_FALSE);
243080901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
2431f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
243280901aeaSGeorge Wilson 
243380901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
243480901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
243580901aeaSGeorge Wilson 
243680901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
243780901aeaSGeorge Wilson 		break;
2438b24ab676SJeff Bonwick 	}
2439b24ab676SJeff Bonwick 
2440f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2441c9ba2a43SEric Schrock 
2442b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2443b24ab676SJeff Bonwick }
2444b24ab676SJeff Bonwick 
2445b24ab676SJeff Bonwick /*
2446b24ab676SJeff Bonwick  * Initialize an object description template.
2447b24ab676SJeff Bonwick  */
2448b24ab676SJeff Bonwick static void
2449b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
245054811da5SToomas Soome     dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
245154811da5SToomas Soome     uint64_t gen)
2452b24ab676SJeff Bonwick {
2453b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2454b24ab676SJeff Bonwick 	od->od_object = 0;
2455b24ab676SJeff Bonwick 
2456b24ab676SJeff Bonwick 	od->od_crtype = type;
2457b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
245854811da5SToomas Soome 	od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2459b24ab676SJeff Bonwick 	od->od_crgen = gen;
2460b24ab676SJeff Bonwick 
2461b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2462b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2463b24ab676SJeff Bonwick 	od->od_gen = 0;
2464b24ab676SJeff Bonwick 
2465b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2466b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2467b24ab676SJeff Bonwick }
2468b24ab676SJeff Bonwick 
2469b24ab676SJeff Bonwick /*
2470b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2471b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2472b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2473b24ab676SJeff Bonwick  * use the existing objects.
2474b24ab676SJeff Bonwick  */
2475b24ab676SJeff Bonwick static int
2476b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2477b24ab676SJeff Bonwick {
2478b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2479b24ab676SJeff Bonwick 	int rv = 0;
2480b24ab676SJeff Bonwick 
2481f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2482b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2483b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2484b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2485b24ab676SJeff Bonwick 		rv = -1;
2486b24ab676SJeff Bonwick 	zd->zd_od = od;
2487f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2488b24ab676SJeff Bonwick 
2489b24ab676SJeff Bonwick 	return (rv);
2490b24ab676SJeff Bonwick }
2491b24ab676SJeff Bonwick 
2492b24ab676SJeff Bonwick /* ARGSUSED */
2493b24ab676SJeff Bonwick void
2494b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2495b24ab676SJeff Bonwick {
2496b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2497b24ab676SJeff Bonwick 
2498f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2499c9ba2a43SEric Schrock 
25005002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2501b24ab676SJeff Bonwick 
2502b24ab676SJeff Bonwick 	/*
2503b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2504b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2505b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2506b24ab676SJeff Bonwick 	 */
2507b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2508420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2509420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2510420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2511b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2512c9ba2a43SEric Schrock 
2513f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2514c9ba2a43SEric Schrock }
2515c9ba2a43SEric Schrock 
2516c9ba2a43SEric Schrock /*
2517c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2518c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2519c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2520c9ba2a43SEric Schrock  */
2521c9ba2a43SEric Schrock /* ARGSUSED */
2522c9ba2a43SEric Schrock void
2523c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2524c9ba2a43SEric Schrock {
2525c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2526c9ba2a43SEric Schrock 
252780901aeaSGeorge Wilson 	/*
252880901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
252980901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
253080901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
253180901aeaSGeorge Wilson 	 */
2532f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2533f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2534c9ba2a43SEric Schrock 
2535c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2536c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2537c9ba2a43SEric Schrock 
2538c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2539c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2540c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2541c9ba2a43SEric Schrock 
2542f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2543f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2544b24ab676SJeff Bonwick }
2545b24ab676SJeff Bonwick 
2546b24ab676SJeff Bonwick /*
2547b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2548b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2549b24ab676SJeff Bonwick  */
2550b24ab676SJeff Bonwick /* ARGSUSED */
2551b24ab676SJeff Bonwick void
2552b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2553b24ab676SJeff Bonwick {
2554420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2555b24ab676SJeff Bonwick 	spa_t *spa;
2556b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2557b24ab676SJeff Bonwick 
2558e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2559e0f1c0afSOlaf Faaland 		return;
2560e0f1c0afSOlaf Faaland 
2561b24ab676SJeff Bonwick 	/*
2562b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2563b24ab676SJeff Bonwick 	 */
2564663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2565b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
25664445fffbSMatthew Ahrens 	    spa_create("ztest_bad_file", nvroot, NULL, NULL));
2567b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2568b24ab676SJeff Bonwick 
2569b24ab676SJeff Bonwick 	/*
2570b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2571b24ab676SJeff Bonwick 	 */
2572663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 2, 1);
2573b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
25744445fffbSMatthew Ahrens 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2575b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2576b24ab676SJeff Bonwick 
2577b24ab676SJeff Bonwick 	/*
2578b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2579b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2580b24ab676SJeff Bonwick 	 */
2581f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
2582663207adSDon Brady 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
25834445fffbSMatthew Ahrens 	VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2584b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2585b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2586420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2587b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2588b24ab676SJeff Bonwick 
2589f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
2590b24ab676SJeff Bonwick }
2591b24ab676SJeff Bonwick 
2592e0f1c0afSOlaf Faaland /*
2593e0f1c0afSOlaf Faaland  * Start and then stop the MMP threads to ensure the startup and shutdown code
2594e0f1c0afSOlaf Faaland  * works properly.  Actual protection and property-related code tested via ZTS.
2595e0f1c0afSOlaf Faaland  */
2596e0f1c0afSOlaf Faaland /* ARGSUSED */
2597e0f1c0afSOlaf Faaland void
2598e0f1c0afSOlaf Faaland ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2599e0f1c0afSOlaf Faaland {
2600e0f1c0afSOlaf Faaland 	ztest_shared_opts_t *zo = &ztest_opts;
2601e0f1c0afSOlaf Faaland 	spa_t *spa = ztest_spa;
2602e0f1c0afSOlaf Faaland 
2603e0f1c0afSOlaf Faaland 	if (zo->zo_mmp_test)
2604e0f1c0afSOlaf Faaland 		return;
2605e0f1c0afSOlaf Faaland 
2606e0f1c0afSOlaf Faaland 	/*
2607e0f1c0afSOlaf Faaland 	 * Since enabling MMP involves setting a property, it could not be done
2608e0f1c0afSOlaf Faaland 	 * while the pool is suspended.
2609e0f1c0afSOlaf Faaland 	 */
2610e0f1c0afSOlaf Faaland 	if (spa_suspended(spa))
2611e0f1c0afSOlaf Faaland 		return;
2612e0f1c0afSOlaf Faaland 
2613e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2614e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2615e0f1c0afSOlaf Faaland 
2616e0f1c0afSOlaf Faaland 	zfs_multihost_fail_intervals = 0;
2617e0f1c0afSOlaf Faaland 
2618e0f1c0afSOlaf Faaland 	if (!spa_multihost(spa)) {
2619e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_TRUE;
2620e0f1c0afSOlaf Faaland 		mmp_thread_start(spa);
2621e0f1c0afSOlaf Faaland 	}
2622e0f1c0afSOlaf Faaland 
2623e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2624e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2625e0f1c0afSOlaf Faaland 
2626e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2627e0f1c0afSOlaf Faaland 	mmp_signal_all_threads();
2628e0f1c0afSOlaf Faaland 	txg_wait_synced(spa_get_dsl(spa), 0);
2629e0f1c0afSOlaf Faaland 
2630e0f1c0afSOlaf Faaland 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2631e0f1c0afSOlaf Faaland 	mutex_enter(&spa->spa_props_lock);
2632e0f1c0afSOlaf Faaland 
2633e0f1c0afSOlaf Faaland 	if (spa_multihost(spa)) {
2634e0f1c0afSOlaf Faaland 		mmp_thread_stop(spa);
2635e0f1c0afSOlaf Faaland 		spa->spa_multihost = B_FALSE;
2636e0f1c0afSOlaf Faaland 	}
2637e0f1c0afSOlaf Faaland 
2638e0f1c0afSOlaf Faaland 	mutex_exit(&spa->spa_props_lock);
2639e0f1c0afSOlaf Faaland 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2640e0f1c0afSOlaf Faaland }
2641e0f1c0afSOlaf Faaland 
264225345e46SGeorge Wilson /* ARGSUSED */
264325345e46SGeorge Wilson void
264425345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
264525345e46SGeorge Wilson {
264625345e46SGeorge Wilson 	spa_t *spa;
264725345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
264825345e46SGeorge Wilson 	uint64_t version, newversion;
264925345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
265025345e46SGeorge Wilson 	char *name;
265125345e46SGeorge Wilson 
2652e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2653e0f1c0afSOlaf Faaland 		return;
2654e0f1c0afSOlaf Faaland 
2655f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
265625345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
265725345e46SGeorge Wilson 
265825345e46SGeorge Wilson 	/*
265925345e46SGeorge Wilson 	 * Clean up from previous runs.
266025345e46SGeorge Wilson 	 */
266125345e46SGeorge Wilson 	(void) spa_destroy(name);
266225345e46SGeorge Wilson 
266325345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2664663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
266525345e46SGeorge Wilson 
266625345e46SGeorge Wilson 	/*
266725345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
266825345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
266925345e46SGeorge Wilson 	 */
267025345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
267125345e46SGeorge Wilson 	case 0:
267225345e46SGeorge Wilson 	case 1:
267325345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
267425345e46SGeorge Wilson 		break;
267525345e46SGeorge Wilson 	case 2:
267625345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
267725345e46SGeorge Wilson 		break;
267825345e46SGeorge Wilson 	case 3:
267925345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
268025345e46SGeorge Wilson 		break;
268125345e46SGeorge Wilson 	}
268225345e46SGeorge Wilson 
268325345e46SGeorge Wilson 	/*
268425345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
268525345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
268625345e46SGeorge Wilson 	 */
268725345e46SGeorge Wilson 	do {
268825345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
268925345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
269025345e46SGeorge Wilson 
269125345e46SGeorge Wilson 	props = fnvlist_alloc();
269225345e46SGeorge Wilson 	fnvlist_add_uint64(props,
269325345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
269425345e46SGeorge Wilson 	VERIFY0(spa_create(name, nvroot, props, NULL));
269525345e46SGeorge Wilson 	fnvlist_free(nvroot);
269625345e46SGeorge Wilson 	fnvlist_free(props);
269725345e46SGeorge Wilson 
269825345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
269925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
270025345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
270125345e46SGeorge Wilson 
270225345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
270325345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
270425345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
270525345e46SGeorge Wilson 	}
270625345e46SGeorge Wilson 
270725345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
270825345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
270925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
271025345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
271125345e46SGeorge Wilson 	spa_close(spa, FTAG);
271225345e46SGeorge Wilson 
271325345e46SGeorge Wilson 	strfree(name);
2714f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
271525345e46SGeorge Wilson }
271625345e46SGeorge Wilson 
271786714001SSerapheim Dimitropoulos static void
271886714001SSerapheim Dimitropoulos ztest_spa_checkpoint(spa_t *spa)
271986714001SSerapheim Dimitropoulos {
272086714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
272186714001SSerapheim Dimitropoulos 
272286714001SSerapheim Dimitropoulos 	int error = spa_checkpoint(spa->spa_name);
272386714001SSerapheim Dimitropoulos 
272486714001SSerapheim Dimitropoulos 	switch (error) {
272586714001SSerapheim Dimitropoulos 	case 0:
272686714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
272786714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
272886714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
272986714001SSerapheim Dimitropoulos 		break;
273086714001SSerapheim Dimitropoulos 	case ENOSPC:
273186714001SSerapheim Dimitropoulos 		ztest_record_enospc(FTAG);
273286714001SSerapheim Dimitropoulos 		break;
273386714001SSerapheim Dimitropoulos 	default:
273486714001SSerapheim Dimitropoulos 		fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
273586714001SSerapheim Dimitropoulos 	}
273686714001SSerapheim Dimitropoulos }
273786714001SSerapheim Dimitropoulos 
273886714001SSerapheim Dimitropoulos static void
273986714001SSerapheim Dimitropoulos ztest_spa_discard_checkpoint(spa_t *spa)
274086714001SSerapheim Dimitropoulos {
274186714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
274286714001SSerapheim Dimitropoulos 
274386714001SSerapheim Dimitropoulos 	int error = spa_checkpoint_discard(spa->spa_name);
274486714001SSerapheim Dimitropoulos 
274586714001SSerapheim Dimitropoulos 	switch (error) {
274686714001SSerapheim Dimitropoulos 	case 0:
274786714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
274886714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
274986714001SSerapheim Dimitropoulos 		break;
275086714001SSerapheim Dimitropoulos 	default:
275186714001SSerapheim Dimitropoulos 		fatal(0, "spa_discard_checkpoint(%s) = %d",
275286714001SSerapheim Dimitropoulos 		    spa->spa_name, error);
275386714001SSerapheim Dimitropoulos 	}
275486714001SSerapheim Dimitropoulos 
275586714001SSerapheim Dimitropoulos }
275686714001SSerapheim Dimitropoulos 
275786714001SSerapheim Dimitropoulos /* ARGSUSED */
275886714001SSerapheim Dimitropoulos void
275986714001SSerapheim Dimitropoulos ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
276086714001SSerapheim Dimitropoulos {
276186714001SSerapheim Dimitropoulos 	spa_t *spa = ztest_spa;
276286714001SSerapheim Dimitropoulos 
276386714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
276486714001SSerapheim Dimitropoulos 	if (ztest_random(2) == 0) {
276586714001SSerapheim Dimitropoulos 		ztest_spa_checkpoint(spa);
276686714001SSerapheim Dimitropoulos 	} else {
276786714001SSerapheim Dimitropoulos 		ztest_spa_discard_checkpoint(spa);
276886714001SSerapheim Dimitropoulos 	}
276986714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
277086714001SSerapheim Dimitropoulos }
277186714001SSerapheim Dimitropoulos 
277286714001SSerapheim Dimitropoulos 
2773b24ab676SJeff Bonwick static vdev_t *
2774b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2775b24ab676SJeff Bonwick {
2776b24ab676SJeff Bonwick 	vdev_t *mvd;
2777b24ab676SJeff Bonwick 
2778b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2779b24ab676SJeff Bonwick 		return (vd);
2780b24ab676SJeff Bonwick 
2781b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2782b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2783b24ab676SJeff Bonwick 		    NULL)
2784b24ab676SJeff Bonwick 			return (mvd);
2785b24ab676SJeff Bonwick 
2786b24ab676SJeff Bonwick 	return (NULL);
2787b24ab676SJeff Bonwick }
2788b24ab676SJeff Bonwick 
2789b24ab676SJeff Bonwick /*
2790b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2791b24ab676SJeff Bonwick  */
2792b24ab676SJeff Bonwick int
2793b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2794b24ab676SJeff Bonwick {
2795b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2796b24ab676SJeff Bonwick 	int c;
2797b24ab676SJeff Bonwick 
2798b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2799b24ab676SJeff Bonwick 
2800b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2801b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2802b24ab676SJeff Bonwick 
2803b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2804b24ab676SJeff Bonwick 			break;
2805b24ab676SJeff Bonwick 	}
2806b24ab676SJeff Bonwick 	return (c);
2807b24ab676SJeff Bonwick }
2808b24ab676SJeff Bonwick 
2809b24ab676SJeff Bonwick /*
2810b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2811b24ab676SJeff Bonwick  */
2812b24ab676SJeff Bonwick /* ARGSUSED */
2813b24ab676SJeff Bonwick void
2814b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2815b24ab676SJeff Bonwick {
2816b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2817420dfc95SChris Siden 	spa_t *spa = ztest_spa;
28181195e687SMark J Musante 	uint64_t leaves;
2819b24ab676SJeff Bonwick 	uint64_t guid;
2820b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2821b24ab676SJeff Bonwick 	int error;
2822b24ab676SJeff Bonwick 
2823e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2824e0f1c0afSOlaf Faaland 		return;
2825e0f1c0afSOlaf Faaland 
2826f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
28273b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2828b24ab676SJeff Bonwick 
2829b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2830b24ab676SJeff Bonwick 
2831b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2832b24ab676SJeff Bonwick 
2833b24ab676SJeff Bonwick 	/*
2834b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2835b24ab676SJeff Bonwick 	 */
283688ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2837663207adSDon Brady 		metaslab_group_t *mg;
2838663207adSDon Brady 
283988ecc943SGeorge Wilson 		/*
2840663207adSDon Brady 		 * find the first real slog in log allocation class
284188ecc943SGeorge Wilson 		 */
2842663207adSDon Brady 		mg =  spa_log_class(spa)->mc_rotor;
2843663207adSDon Brady 		while (!mg->mg_vd->vdev_islog)
2844663207adSDon Brady 			mg = mg->mg_next;
2845663207adSDon Brady 
2846663207adSDon Brady 		guid = mg->mg_vd->vdev_guid;
28478654d025Sperrin 
284888ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2849fa9e4066Sahrens 
285088ecc943SGeorge Wilson 		/*
285188ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
285288ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
285388ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
285488ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
285588ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
285688ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
285788ecc943SGeorge Wilson 		 */
2858f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_WRITER);
285988ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2860f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
2861fa9e4066Sahrens 
286286714001SSerapheim Dimitropoulos 		switch (error) {
286386714001SSerapheim Dimitropoulos 		case 0:
286486714001SSerapheim Dimitropoulos 		case EEXIST:
286586714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
286686714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
286786714001SSerapheim Dimitropoulos 			break;
286886714001SSerapheim Dimitropoulos 		default:
286988ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
287086714001SSerapheim Dimitropoulos 		}
287188ecc943SGeorge Wilson 	} else {
287288ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
287388ecc943SGeorge Wilson 
287488ecc943SGeorge Wilson 		/*
2875663207adSDon Brady 		 * Make 1/4 of the devices be log devices
287688ecc943SGeorge Wilson 		 */
287725345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2878663207adSDon Brady 		    ztest_opts.zo_vdev_size, 0, (ztest_random(4) == 0) ?
2879663207adSDon Brady 		    "log" : NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
288088ecc943SGeorge Wilson 
288188ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
288288ecc943SGeorge Wilson 		nvlist_free(nvroot);
288388ecc943SGeorge Wilson 
288486714001SSerapheim Dimitropoulos 		switch (error) {
288586714001SSerapheim Dimitropoulos 		case 0:
288686714001SSerapheim Dimitropoulos 			break;
288786714001SSerapheim Dimitropoulos 		case ENOSPC:
288888ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
288986714001SSerapheim Dimitropoulos 			break;
289086714001SSerapheim Dimitropoulos 		default:
289188ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
289286714001SSerapheim Dimitropoulos 		}
289388ecc943SGeorge Wilson 	}
289488ecc943SGeorge Wilson 
2895f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
2896e14bb325SJeff Bonwick }
2897fa9e4066Sahrens 
2898663207adSDon Brady /* ARGSUSED */
2899663207adSDon Brady void
2900663207adSDon Brady ztest_vdev_class_add(ztest_ds_t *zd, uint64_t id)
2901663207adSDon Brady {
2902663207adSDon Brady 	ztest_shared_t *zs = ztest_shared;
2903663207adSDon Brady 	spa_t *spa = ztest_spa;
2904663207adSDon Brady 	uint64_t leaves;
2905663207adSDon Brady 	nvlist_t *nvroot;
2906663207adSDon Brady 	const char *class = (ztest_random(2) == 0) ?
2907663207adSDon Brady 	    VDEV_ALLOC_BIAS_SPECIAL : VDEV_ALLOC_BIAS_DEDUP;
2908663207adSDon Brady 	int error;
2909663207adSDon Brady 
2910663207adSDon Brady 	/*
2911663207adSDon Brady 	 * By default add a special vdev 50% of the time
2912663207adSDon Brady 	 */
2913663207adSDon Brady 	if ((ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_OFF) ||
2914663207adSDon Brady 	    (ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_RND &&
2915663207adSDon Brady 	    ztest_random(2) == 0)) {
2916663207adSDon Brady 		return;
2917663207adSDon Brady 	}
2918663207adSDon Brady 
2919663207adSDon Brady 	mutex_enter(&ztest_vdev_lock);
2920663207adSDon Brady 
2921663207adSDon Brady 	/* Only test with mirrors */
2922663207adSDon Brady 	if (zs->zs_mirrors < 2) {
2923663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2924663207adSDon Brady 		return;
2925663207adSDon Brady 	}
2926663207adSDon Brady 
2927663207adSDon Brady 	/* requires feature@allocation_classes */
2928663207adSDon Brady 	if (!spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)) {
2929663207adSDon Brady 		mutex_exit(&ztest_vdev_lock);
2930663207adSDon Brady 		return;
2931663207adSDon Brady 	}
2932663207adSDon Brady 
2933663207adSDon Brady 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2934663207adSDon Brady 
2935663207adSDon Brady 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2936663207adSDon Brady 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2937663207adSDon Brady 	spa_config_exit(spa, SCL_VDEV, FTAG);
2938663207adSDon Brady 
2939663207adSDon Brady 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
2940663207adSDon Brady 	    class, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
2941663207adSDon Brady 
2942663207adSDon Brady 	error = spa_vdev_add(spa, nvroot);
2943663207adSDon Brady 	nvlist_free(nvroot);
2944663207adSDon Brady 
2945663207adSDon Brady 	if (error == ENOSPC)
2946663207adSDon Brady 		ztest_record_enospc("spa_vdev_add");
2947663207adSDon Brady 	else if (error != 0)
2948663207adSDon Brady 		fatal(0, "spa_vdev_add() = %d", error);
2949663207adSDon Brady 
2950663207adSDon Brady 	/*
2951663207adSDon Brady 	 * 50% of the time allow small blocks in the special class
2952663207adSDon Brady 	 */
2953663207adSDon Brady 	if (error == 0 &&
2954663207adSDon Brady 	    spa_special_class(spa)->mc_groups == 1 && ztest_random(2) == 0) {
2955663207adSDon Brady 		if (ztest_opts.zo_verbose >= 3)
2956663207adSDon Brady 			(void) printf("Enabling special VDEV small blocks\n");
2957663207adSDon Brady 		(void) ztest_dsl_prop_set_uint64(zd->zd_name,
2958663207adSDon Brady 		    ZFS_PROP_SPECIAL_SMALL_BLOCKS, 32768, B_FALSE);
2959663207adSDon Brady 	}
2960663207adSDon Brady 
2961663207adSDon Brady 	mutex_exit(&ztest_vdev_lock);
2962663207adSDon Brady 
2963663207adSDon Brady 	if (ztest_opts.zo_verbose >= 3) {
2964663207adSDon Brady 		metaslab_class_t *mc;
2965663207adSDon Brady 
2966663207adSDon Brady 		if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL) == 0)
2967663207adSDon Brady 			mc = spa_special_class(spa);
2968663207adSDon Brady 		else
2969663207adSDon Brady 			mc = spa_dedup_class(spa);
2970663207adSDon Brady 		(void) printf("Added a %s mirrored vdev (of %d)\n",
2971663207adSDon Brady 		    class, (int)mc->mc_groups);
2972663207adSDon Brady 	}
2973663207adSDon Brady }
2974663207adSDon Brady 
2975e14bb325SJeff Bonwick /*
2976e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2977e14bb325SJeff Bonwick  */
2978b24ab676SJeff Bonwick /* ARGSUSED */
2979e14bb325SJeff Bonwick void
2980b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2981e14bb325SJeff Bonwick {
2982b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2983420dfc95SChris Siden 	spa_t *spa = ztest_spa;
298431157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2985e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
2986e14bb325SJeff Bonwick 	char *aux;
2987e14bb325SJeff Bonwick 	uint64_t guid = 0;
2988e14bb325SJeff Bonwick 	int error;
2989e14bb325SJeff Bonwick 
2990e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
2991e0f1c0afSOlaf Faaland 		return;
2992e0f1c0afSOlaf Faaland 
299331157203SJeff Bonwick 	if (ztest_random(2) == 0) {
2994e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
2995e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
2996e14bb325SJeff Bonwick 	} else {
2997e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
2998e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
2999e14bb325SJeff Bonwick 	}
3000e14bb325SJeff Bonwick 
3001f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3002e14bb325SJeff Bonwick 
3003e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3004e14bb325SJeff Bonwick 
3005e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
3006e14bb325SJeff Bonwick 		/*
3007e14bb325SJeff Bonwick 		 * Pick a random device to remove.
3008e14bb325SJeff Bonwick 		 */
3009e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3010e14bb325SJeff Bonwick 	} else {
3011e14bb325SJeff Bonwick 		/*
3012e14bb325SJeff Bonwick 		 * Find an unused device we can add.
3013e14bb325SJeff Bonwick 		 */
3014b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
3015e14bb325SJeff Bonwick 		for (;;) {
3016e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
3017e14bb325SJeff Bonwick 			int c;
3018420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
3019420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3020420dfc95SChris Siden 			    zs->zs_vdev_aux);
3021e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
3022e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
3023e14bb325SJeff Bonwick 				    path) == 0)
3024e14bb325SJeff Bonwick 					break;
302531157203SJeff Bonwick 			if (c == sav->sav_count &&
302631157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
3027e14bb325SJeff Bonwick 				break;
3028b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
3029e14bb325SJeff Bonwick 		}
3030e14bb325SJeff Bonwick 	}
3031e14bb325SJeff Bonwick 
3032e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
3033e14bb325SJeff Bonwick 
3034e14bb325SJeff Bonwick 	if (guid == 0) {
3035e14bb325SJeff Bonwick 		/*
3036e14bb325SJeff Bonwick 		 * Add a new device.
3037e14bb325SJeff Bonwick 		 */
303825345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3039663207adSDon Brady 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, NULL, 0, 0, 1);
3040e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
304186714001SSerapheim Dimitropoulos 
304286714001SSerapheim Dimitropoulos 		switch (error) {
304386714001SSerapheim Dimitropoulos 		case 0:
304486714001SSerapheim Dimitropoulos 			break;
304586714001SSerapheim Dimitropoulos 		default:
3046e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
304786714001SSerapheim Dimitropoulos 		}
3048e14bb325SJeff Bonwick 		nvlist_free(nvroot);
3049e14bb325SJeff Bonwick 	} else {
3050e14bb325SJeff Bonwick 		/*
3051e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
3052e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
3053e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
3054e14bb325SJeff Bonwick 		 */
3055e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
3056573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
3057e14bb325SJeff Bonwick 
3058e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
305986714001SSerapheim Dimitropoulos 
306086714001SSerapheim Dimitropoulos 		switch (error) {
306186714001SSerapheim Dimitropoulos 		case 0:
306286714001SSerapheim Dimitropoulos 		case EBUSY:
306386714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
306486714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
306586714001SSerapheim Dimitropoulos 			break;
306686714001SSerapheim Dimitropoulos 		default:
3067e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
306886714001SSerapheim Dimitropoulos 		}
3069e14bb325SJeff Bonwick 	}
3070e14bb325SJeff Bonwick 
3071f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3072fa9e4066Sahrens }
3073fa9e4066Sahrens 
30741195e687SMark J Musante /*
30751195e687SMark J Musante  * split a pool if it has mirror tlvdevs
30761195e687SMark J Musante  */
30771195e687SMark J Musante /* ARGSUSED */
30781195e687SMark J Musante void
30791195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
30801195e687SMark J Musante {
30811195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
3082420dfc95SChris Siden 	spa_t *spa = ztest_spa;
30831195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
30841195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
30851195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
30861195e687SMark J Musante 	int error = 0;
30871195e687SMark J Musante 
3088e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3089e0f1c0afSOlaf Faaland 		return;
3090e0f1c0afSOlaf Faaland 
3091f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
30921195e687SMark J Musante 
30931195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
3094420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3095f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
30961195e687SMark J Musante 		return;
30971195e687SMark J Musante 	}
30981195e687SMark J Musante 
30991195e687SMark J Musante 	/* clean up the old pool, if any */
31001195e687SMark J Musante 	(void) spa_destroy("splitp");
31011195e687SMark J Musante 
31021195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
31031195e687SMark J Musante 
31041195e687SMark J Musante 	/* generate a config from the existing config */
310598295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
31061195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
31071195e687SMark J Musante 	    &tree) == 0);
310898295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
310998295d61SMark J Musante 
31101195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
31111195e687SMark J Musante 	    &children) == 0);
31121195e687SMark J Musante 
31131195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
31141195e687SMark J Musante 	for (c = 0; c < children; c++) {
31151195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
31161195e687SMark J Musante 		nvlist_t **mchild;
31171195e687SMark J Musante 		uint_t mchildren;
31181195e687SMark J Musante 
31191195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
31201195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
31211195e687SMark J Musante 			    0) == 0);
31221195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
31231195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
31241195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
31251195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
31261195e687SMark J Musante 			if (lastlogid == 0)
31271195e687SMark J Musante 				lastlogid = schildren;
31281195e687SMark J Musante 			++schildren;
31291195e687SMark J Musante 			continue;
31301195e687SMark J Musante 		}
31311195e687SMark J Musante 		lastlogid = 0;
31321195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
31331195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
31341195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
31351195e687SMark J Musante 	}
31361195e687SMark J Musante 
31371195e687SMark J Musante 	/* OK, create a config that can be used to split */
31381195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
31391195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
31401195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
31411195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
31421195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
31431195e687SMark J Musante 
31441195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
31451195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
31461195e687SMark J Musante 
31471195e687SMark J Musante 	for (c = 0; c < schildren; c++)
31481195e687SMark J Musante 		nvlist_free(schild[c]);
31491195e687SMark J Musante 	free(schild);
31501195e687SMark J Musante 	nvlist_free(split);
31511195e687SMark J Musante 
31521195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
31531195e687SMark J Musante 
3154f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
31551195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3156f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
31571195e687SMark J Musante 
31581195e687SMark J Musante 	nvlist_free(config);
31591195e687SMark J Musante 
31601195e687SMark J Musante 	if (error == 0) {
31611195e687SMark J Musante 		(void) printf("successful split - results:\n");
31621195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
31631195e687SMark J Musante 		show_pool_stats(spa);
31641195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
31651195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
31661195e687SMark J Musante 		++zs->zs_splits;
31671195e687SMark J Musante 		--zs->zs_mirrors;
31681195e687SMark J Musante 	}
3169f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
31701195e687SMark J Musante }
31711195e687SMark J Musante 
3172fa9e4066Sahrens /*
3173fa9e4066Sahrens  * Verify that we can attach and detach devices.
3174fa9e4066Sahrens  */
3175b24ab676SJeff Bonwick /* ARGSUSED */
3176fa9e4066Sahrens void
3177b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3178fa9e4066Sahrens {
3179b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
3180420dfc95SChris Siden 	spa_t *spa = ztest_spa;
318131157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
3182fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
3183ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
3184e14bb325SJeff Bonwick 	nvlist_t *root;
31851195e687SMark J Musante 	uint64_t leaves;
3186fa9e4066Sahrens 	uint64_t leaf, top;
3187ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
31888ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
3189b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
3190ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
3191fa9e4066Sahrens 	int replacing;
31929af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
319331157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
319431157203SJeff Bonwick 	int oldvd_is_log;
3195fa9e4066Sahrens 	int error, expected_error;
3196fa9e4066Sahrens 
3197e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
3198e0f1c0afSOlaf Faaland 		return;
3199e0f1c0afSOlaf Faaland 
3200f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3201420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3202fa9e4066Sahrens 
32035cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
32045cabbc6bSPrashanth Sreenivasa 
32055cabbc6bSPrashanth Sreenivasa 	/*
32065cabbc6bSPrashanth Sreenivasa 	 * If a vdev is in the process of being removed, its removal may
32075cabbc6bSPrashanth Sreenivasa 	 * finish while we are in progress, leading to an unexpected error
32085cabbc6bSPrashanth Sreenivasa 	 * value.  Don't bother trying to attach while we are in the middle
32095cabbc6bSPrashanth Sreenivasa 	 * of removal.
32105cabbc6bSPrashanth Sreenivasa 	 */
32113a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
32125cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
3213f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
32145cabbc6bSPrashanth Sreenivasa 		return;
32155cabbc6bSPrashanth Sreenivasa 	}
3216fa9e4066Sahrens 
3217fa9e4066Sahrens 	/*
3218fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
3219fa9e4066Sahrens 	 */
3220fa9e4066Sahrens 	replacing = ztest_random(2);
3221fa9e4066Sahrens 
3222fa9e4066Sahrens 	/*
3223fa9e4066Sahrens 	 * Pick a random top-level vdev.
3224fa9e4066Sahrens 	 */
3225b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3226fa9e4066Sahrens 
3227fa9e4066Sahrens 	/*
3228fa9e4066Sahrens 	 * Pick a random leaf within it.
3229fa9e4066Sahrens 	 */
3230d41c4376SMark J Musante 	leaf = ztest_random(leaves);
3231fa9e4066Sahrens 
3232fa9e4066Sahrens 	/*
323331157203SJeff Bonwick 	 * Locate this vdev.
3234fa9e4066Sahrens 	 */
323531157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
3236663207adSDon Brady 
3237663207adSDon Brady 	/* pick a child from the mirror */
32381195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
32398ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
32401195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3241420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
32428ad4d6ddSJeff Bonwick 	}
3243663207adSDon Brady 
3244663207adSDon Brady 	/* pick a child out of the raidz group */
3245420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
32468ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3247420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3248420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
32498ad4d6ddSJeff Bonwick 	}
3250fa9e4066Sahrens 
3251fa9e4066Sahrens 	/*
325231157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
325331157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
3254fa9e4066Sahrens 	 */
325531157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
32569af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
32578ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
32588ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
325931157203SJeff Bonwick 	}
3260fa9e4066Sahrens 
326131157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
3262573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
326331157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
326431157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
3265ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
32668ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
3267fa9e4066Sahrens 
32689af0a4dfSJeff Bonwick 	/*
32699af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
32709af0a4dfSJeff Bonwick 	 */
32719af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
32725cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
32738ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
32748ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
327586714001SSerapheim Dimitropoulos 		    error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
327686714001SSerapheim Dimitropoulos 		    error != ZFS_ERR_DISCARDING_CHECKPOINT)
32778ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
3278f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
32799af0a4dfSJeff Bonwick 		return;
32809af0a4dfSJeff Bonwick 	}
32819af0a4dfSJeff Bonwick 
3282fa9e4066Sahrens 	/*
328331157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
328431157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3285fa9e4066Sahrens 	 */
328631157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
328731157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
328831157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
328931157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
329031157203SJeff Bonwick 	} else {
329131157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3292420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
3293420dfc95SChris Siden 		    top * leaves + leaf);
329431157203SJeff Bonwick 		if (ztest_random(2) == 0)
329531157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
329631157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
329731157203SJeff Bonwick 	}
329831157203SJeff Bonwick 
329931157203SJeff Bonwick 	if (newvd) {
33005cabbc6bSPrashanth Sreenivasa 		/*
33015cabbc6bSPrashanth Sreenivasa 		 * Reopen to ensure the vdev's asize field isn't stale.
33025cabbc6bSPrashanth Sreenivasa 		 */
33035cabbc6bSPrashanth Sreenivasa 		vdev_reopen(newvd);
3304573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
330531157203SJeff Bonwick 	} else {
330631157203SJeff Bonwick 		/*
330731157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
330831157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
330931157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
331031157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
331131157203SJeff Bonwick 		 */
331231157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
331331157203SJeff Bonwick 	}
3314fa9e4066Sahrens 
3315fa9e4066Sahrens 	/*
3316fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3317fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
3318fa9e4066Sahrens 	 *
3319ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
3320fa9e4066Sahrens 	 *
3321ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
3322fa9e4066Sahrens 	 */
332331157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
332431157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
332531157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
332631157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
332731157203SJeff Bonwick 		expected_error = ENOTSUP;
332831157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
3329fa9e4066Sahrens 		expected_error = ENOTSUP;
333031157203SJeff Bonwick 	else if (newvd == oldvd)
333131157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
333231157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
333331157203SJeff Bonwick 		expected_error = EBUSY;
3334ea8dc4b6Seschrock 	else if (newsize < oldsize)
3335fa9e4066Sahrens 		expected_error = EOVERFLOW;
3336ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
3337ecc2d604Sbonwick 		expected_error = EDOM;
3338fa9e4066Sahrens 	else
3339fa9e4066Sahrens 		expected_error = 0;
3340fa9e4066Sahrens 
33415cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_ALL, FTAG);
3342fa9e4066Sahrens 
3343fa9e4066Sahrens 	/*
3344ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
3345fa9e4066Sahrens 	 */
334625345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3347663207adSDon Brady 	    ashift, NULL, 0, 0, 1);
3348fa9e4066Sahrens 
334931157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
3350fa9e4066Sahrens 
3351fa9e4066Sahrens 	nvlist_free(root);
3352fa9e4066Sahrens 
3353fa9e4066Sahrens 	/*
3354fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
3355fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
3356fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
3357fa9e4066Sahrens 	 */
3358fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
3359fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
3360fa9e4066Sahrens 		expected_error = error;
3361fa9e4066Sahrens 
3362f0aa80d4Sbonwick 	/*
3363f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
3364f0aa80d4Sbonwick 	 */
3365088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
3366f0aa80d4Sbonwick 		expected_error = error;
3367f0aa80d4Sbonwick 
336886714001SSerapheim Dimitropoulos 	if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
336986714001SSerapheim Dimitropoulos 	    error == ZFS_ERR_DISCARDING_CHECKPOINT)
337086714001SSerapheim Dimitropoulos 		expected_error = error;
337186714001SSerapheim Dimitropoulos 
3372088f3894Sahrens 	/* XXX workaround 6690467 */
3373088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
3374088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
3375088f3894Sahrens 		    "returned %d, expected %d",
3376b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
3377b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
3378fa9e4066Sahrens 	}
3379fa9e4066Sahrens 
3380f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3381fa9e4066Sahrens }
3382fa9e4066Sahrens 
33835cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
33845cabbc6bSPrashanth Sreenivasa void
33855cabbc6bSPrashanth Sreenivasa ztest_device_removal(ztest_ds_t *zd, uint64_t id)
33865cabbc6bSPrashanth Sreenivasa {
33875cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ztest_spa;
33885cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
33895cabbc6bSPrashanth Sreenivasa 	uint64_t guid;
33903a4b1be9SMatthew Ahrens 	int error;
33915cabbc6bSPrashanth Sreenivasa 
3392f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
33935cabbc6bSPrashanth Sreenivasa 
33943a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
33953a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
33963a4b1be9SMatthew Ahrens 		return;
33973a4b1be9SMatthew Ahrens 	}
33983a4b1be9SMatthew Ahrens 
33993a4b1be9SMatthew Ahrens 	/*
34003a4b1be9SMatthew Ahrens 	 * Remove a random top-level vdev and wait for removal to finish.
34013a4b1be9SMatthew Ahrens 	 */
34025cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34035cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
34045cabbc6bSPrashanth Sreenivasa 	guid = vd->vdev_guid;
34055cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
34065cabbc6bSPrashanth Sreenivasa 
34073a4b1be9SMatthew Ahrens 	error = spa_vdev_remove(spa, guid, B_FALSE);
34083a4b1be9SMatthew Ahrens 	if (error == 0) {
34093a4b1be9SMatthew Ahrens 		ztest_device_removal_active = B_TRUE;
34103a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34113a4b1be9SMatthew Ahrens 
34123a4b1be9SMatthew Ahrens 		while (spa->spa_vdev_removal != NULL)
34133a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34143a4b1be9SMatthew Ahrens 	} else {
34153a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
34163a4b1be9SMatthew Ahrens 		return;
34173a4b1be9SMatthew Ahrens 	}
34185cabbc6bSPrashanth Sreenivasa 
34193a4b1be9SMatthew Ahrens 	/*
34203a4b1be9SMatthew Ahrens 	 * The pool needs to be scrubbed after completing device removal.
34213a4b1be9SMatthew Ahrens 	 * Failure to do so may result in checksum errors due to the
34223a4b1be9SMatthew Ahrens 	 * strategy employed by ztest_fault_inject() when selecting which
34233a4b1be9SMatthew Ahrens 	 * offset are redundant and can be damaged.
34243a4b1be9SMatthew Ahrens 	 */
34253a4b1be9SMatthew Ahrens 	error = spa_scan(spa, POOL_SCAN_SCRUB);
34263a4b1be9SMatthew Ahrens 	if (error == 0) {
34273a4b1be9SMatthew Ahrens 		while (dsl_scan_scrubbing(spa_get_dsl(spa)))
34283a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
34293a4b1be9SMatthew Ahrens 	}
34303a4b1be9SMatthew Ahrens 
34313a4b1be9SMatthew Ahrens 	mutex_enter(&ztest_vdev_lock);
34323a4b1be9SMatthew Ahrens 	ztest_device_removal_active = B_FALSE;
3433f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
34345cabbc6bSPrashanth Sreenivasa }
34355cabbc6bSPrashanth Sreenivasa 
3436573ca77eSGeorge Wilson /*
3437573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
3438573ca77eSGeorge Wilson  */
3439573ca77eSGeorge Wilson vdev_t *
3440573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
3441573ca77eSGeorge Wilson {
3442573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3443573ca77eSGeorge Wilson 	size_t *newsize = arg;
3444573ca77eSGeorge Wilson 	size_t fsize;
3445573ca77eSGeorge Wilson 	int fd;
3446573ca77eSGeorge Wilson 
3447573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3448573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3449573ca77eSGeorge Wilson 
3450573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3451573ca77eSGeorge Wilson 		return (vd);
3452573ca77eSGeorge Wilson 
3453573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
3454573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
3455573ca77eSGeorge Wilson 
3456420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3457573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
3458573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3459573ca77eSGeorge Wilson 	}
3460573ca77eSGeorge Wilson 	(void) close(fd);
3461573ca77eSGeorge Wilson 	return (NULL);
3462573ca77eSGeorge Wilson }
3463573ca77eSGeorge Wilson 
3464573ca77eSGeorge Wilson /*
3465573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
3466573ca77eSGeorge Wilson  */
3467573ca77eSGeorge Wilson /* ARGSUSED */
3468573ca77eSGeorge Wilson vdev_t *
3469573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
3470573ca77eSGeorge Wilson {
3471573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3472573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
3473573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
34748f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
3475095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3476095bcd66SGeorge Wilson 	int error;
3477573ca77eSGeorge Wilson 
3478573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3479573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3480573ca77eSGeorge Wilson 
3481573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
3482573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3483095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3484573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3485573ca77eSGeorge Wilson 
3486095bcd66SGeorge Wilson 	/*
3487095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
3488095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
3489095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
3490095bcd66SGeorge Wilson 	 */
3491095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
3492420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3493095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
3494095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
3495095bcd66SGeorge Wilson 		}
3496095bcd66SGeorge Wilson 		return (vd);
3497095bcd66SGeorge Wilson 	}
3498095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3499095bcd66SGeorge Wilson 
3500573ca77eSGeorge Wilson 	/*
3501573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
3502573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
3503573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
3504573ca77eSGeorge Wilson 	 * trying to online it.
3505573ca77eSGeorge Wilson 	 */
35068f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
3507420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
35088f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
35098f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
3510b24ab676SJeff Bonwick 			    "got gen %llu\n",
3511b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
35128f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
35138f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
35148f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
3515573ca77eSGeorge Wilson 		}
3516573ca77eSGeorge Wilson 		return (vd);
3517573ca77eSGeorge Wilson 	}
3518573ca77eSGeorge Wilson 	return (NULL);
3519573ca77eSGeorge Wilson }
3520573ca77eSGeorge Wilson 
3521573ca77eSGeorge Wilson /*
3522573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
3523573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
3524573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3525573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3526573ca77eSGeorge Wilson  * leaf vdev we encounter.
3527573ca77eSGeorge Wilson  */
3528573ca77eSGeorge Wilson vdev_t *
3529573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3530573ca77eSGeorge Wilson {
3531573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3532573ca77eSGeorge Wilson 		if (func == NULL)
3533573ca77eSGeorge Wilson 			return (vd);
3534573ca77eSGeorge Wilson 		else
3535573ca77eSGeorge Wilson 			return (func(vd, arg));
3536573ca77eSGeorge Wilson 	}
3537573ca77eSGeorge Wilson 
3538573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3539573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3540573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3541573ca77eSGeorge Wilson 			return (cvd);
3542573ca77eSGeorge Wilson 	}
3543573ca77eSGeorge Wilson 	return (NULL);
3544573ca77eSGeorge Wilson }
3545573ca77eSGeorge Wilson 
3546fa9e4066Sahrens /*
3547fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3548fa9e4066Sahrens  */
3549b24ab676SJeff Bonwick /* ARGSUSED */
3550fa9e4066Sahrens void
3551b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3552fa9e4066Sahrens {
3553420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3554b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3555b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3556b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3557573ca77eSGeorge Wilson 	size_t psize, newsize;
3558b24ab676SJeff Bonwick 	uint64_t top;
3559b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3560fa9e4066Sahrens 
356186714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
3562f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3563573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3564573ca77eSGeorge Wilson 
35655cabbc6bSPrashanth Sreenivasa 	/*
35665cabbc6bSPrashanth Sreenivasa 	 * If there is a vdev removal in progress, it could complete while
35675cabbc6bSPrashanth Sreenivasa 	 * we are running, in which case we would not be able to verify
35685cabbc6bSPrashanth Sreenivasa 	 * that the metaslab_class space increased (because it decreases
35695cabbc6bSPrashanth Sreenivasa 	 * when the device removal completes).
35705cabbc6bSPrashanth Sreenivasa 	 */
35713a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
357286714001SSerapheim Dimitropoulos 		spa_config_exit(spa, SCL_STATE, spa);
3573f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
357486714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
35755cabbc6bSPrashanth Sreenivasa 		return;
35765cabbc6bSPrashanth Sreenivasa 	}
35775cabbc6bSPrashanth Sreenivasa 
3578b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3579573ca77eSGeorge Wilson 
3580b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3581b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3582b24ab676SJeff Bonwick 	mc = mg->mg_class;
3583b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3584b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3585fa9e4066Sahrens 
3586fa9e4066Sahrens 	/*
3587573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3588573ca77eSGeorge Wilson 	 * our top-level device.
3589fa9e4066Sahrens 	 */
3590573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3591573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3592573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3593fa9e4066Sahrens 
3594573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3595fa9e4066Sahrens 
3596573ca77eSGeorge Wilson 	/*
35978f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
35988f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3599573ca77eSGeorge Wilson 	 */
36008f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3601420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3602573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3603f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
360486714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3605573ca77eSGeorge Wilson 		return;
3606573ca77eSGeorge Wilson 	}
3607573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3608663207adSDon Brady 	newsize = psize + MAX(psize / 8, SPA_MAXBLOCKSIZE);
3609573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3610fa9e4066Sahrens 
3611420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3612b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3613573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3614573ca77eSGeorge Wilson 	}
3615573ca77eSGeorge Wilson 
3616573ca77eSGeorge Wilson 	/*
3617573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3618573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3619573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3620573ca77eSGeorge Wilson 	 */
3621573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3622573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3623573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3624420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3625b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3626b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3627b24ab676SJeff Bonwick 		}
3628b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3629f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
363086714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3631b24ab676SJeff Bonwick 		return;
3632b24ab676SJeff Bonwick 	}
3633b24ab676SJeff Bonwick 
3634b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3635b24ab676SJeff Bonwick 
3636b24ab676SJeff Bonwick 	/*
3637b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3638b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3639b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3640b24ab676SJeff Bonwick 	 */
3641b24ab676SJeff Bonwick 	for (;;) {
3642b24ab676SJeff Bonwick 		boolean_t done;
3643b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3644b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3645b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3646b24ab676SJeff Bonwick 		if (done)
3647b24ab676SJeff Bonwick 			break;
3648b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3649b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3650b24ab676SJeff Bonwick 	}
3651b24ab676SJeff Bonwick 
3652b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3653b24ab676SJeff Bonwick 
3654b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3655b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3656b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3657b24ab676SJeff Bonwick 
3658b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3659420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3660b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3661b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3662fa9e4066Sahrens 		}
3663b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3664f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
366586714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3666573ca77eSGeorge Wilson 		return;
3667fa9e4066Sahrens 	}
3668fa9e4066Sahrens 
3669573ca77eSGeorge Wilson 	/*
3670b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3671573ca77eSGeorge Wilson 	 */
36725cabbc6bSPrashanth Sreenivasa 	if (new_ms_count <= old_ms_count) {
36735cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3674b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
36755cabbc6bSPrashanth Sreenivasa 	}
3676573ca77eSGeorge Wilson 
3677573ca77eSGeorge Wilson 	/*
3678573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3679573ca77eSGeorge Wilson 	 */
36805cabbc6bSPrashanth Sreenivasa 	if (new_class_space <= old_class_space) {
36815cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3682b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
36835cabbc6bSPrashanth Sreenivasa 	}
3684b24ab676SJeff Bonwick 
3685420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
36860a055120SJason King 		char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3687573ca77eSGeorge Wilson 
36880a055120SJason King 		nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
36890a055120SJason King 		nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3690573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3691573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3692573ca77eSGeorge Wilson 	}
3693b24ab676SJeff Bonwick 
3694573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3695f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
369686714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
3697fa9e4066Sahrens }
3698fa9e4066Sahrens 
3699b24ab676SJeff Bonwick /*
3700b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3701b24ab676SJeff Bonwick  */
3702fa9e4066Sahrens /* ARGSUSED */
3703fa9e4066Sahrens static void
3704b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3705fa9e4066Sahrens {
3706fa9e4066Sahrens 	/*
3707b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3708fa9e4066Sahrens 	 */
3709b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3710fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3711fa9e4066Sahrens }
3712fa9e4066Sahrens 
371355da60b9SMark J Musante static int
371455da60b9SMark J Musante ztest_dataset_create(char *dsname)
371555da60b9SMark J Musante {
371655da60b9SMark J Musante 	uint64_t zilset = ztest_random(100);
371755da60b9SMark J Musante 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
371855da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
371955da60b9SMark J Musante 
372055da60b9SMark J Musante 	if (err || zilset < 80)
372155da60b9SMark J Musante 		return (err);
372255da60b9SMark J Musante 
3723420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3724420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
372555da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
372655da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
372755da60b9SMark J Musante }
372855da60b9SMark J Musante 
3729b24ab676SJeff Bonwick /* ARGSUSED */
37301d452cf5Sahrens static int
3731fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3732fa9e4066Sahrens {
3733fa9e4066Sahrens 	objset_t *os;
3734b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3735fa9e4066Sahrens 	int error;
3736fa9e4066Sahrens 
3737fa9e4066Sahrens 	/*
3738fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3739fa9e4066Sahrens 	 */
37403b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3741b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3742e1930233Sbonwick 	if (error != ENOENT) {
3743e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3744fb09f5aaSMadhav Suresh 		ASSERT0(error);
3745b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3746b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3747e1930233Sbonwick 	}
37483b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3749fa9e4066Sahrens 
3750fa9e4066Sahrens 	/*
3751fa9e4066Sahrens 	 * Destroy the dataset.
3752fa9e4066Sahrens 	 */
37533b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
3754754998c8SChris Williamson 		VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
37553b2aab18SMatthew Ahrens 	} else {
3756754998c8SChris Williamson 		error = dsl_destroy_head(name);
3757754998c8SChris Williamson 		/* There could be a hold on this dataset */
3758754998c8SChris Williamson 		if (error != EBUSY)
3759754998c8SChris Williamson 			ASSERT0(error);
37603b2aab18SMatthew Ahrens 	}
37611d452cf5Sahrens 	return (0);
3762fa9e4066Sahrens }
3763fa9e4066Sahrens 
3764b24ab676SJeff Bonwick static boolean_t
3765b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3766fa9e4066Sahrens {
37679adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3768b24ab676SJeff Bonwick 	int error;
3769b24ab676SJeff Bonwick 
37703b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3771b24ab676SJeff Bonwick 
37723b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3773b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3774b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3775b24ab676SJeff Bonwick 		return (B_FALSE);
3776b24ab676SJeff Bonwick 	}
37773b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
37783b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
37793b2aab18SMatthew Ahrens 		    snapname, error);
37803b2aab18SMatthew Ahrens 	}
3781b24ab676SJeff Bonwick 	return (B_TRUE);
3782b24ab676SJeff Bonwick }
3783b24ab676SJeff Bonwick 
3784b24ab676SJeff Bonwick static boolean_t
3785b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3786b24ab676SJeff Bonwick {
37879adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3788b24ab676SJeff Bonwick 	int error;
3789b24ab676SJeff Bonwick 
37909adfa60dSMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3791b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3792b24ab676SJeff Bonwick 
37933b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3794b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3795b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3796b24ab676SJeff Bonwick 	return (B_TRUE);
3797fa9e4066Sahrens }
3798fa9e4066Sahrens 
3799b24ab676SJeff Bonwick /* ARGSUSED */
3800fa9e4066Sahrens void
3801b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3802fa9e4066Sahrens {
3803b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3804b24ab676SJeff Bonwick 	int iters;
3805fa9e4066Sahrens 	int error;
3806745cd3c5Smaybee 	objset_t *os, *os2;
38079adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3808fa9e4066Sahrens 	zilog_t *zilog;
3809fa9e4066Sahrens 
3810f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3811b24ab676SJeff Bonwick 
38129adfa60dSMatthew Ahrens 	(void) snprintf(name, sizeof (name), "%s/temp_%llu",
3813420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3814fa9e4066Sahrens 
3815fa9e4066Sahrens 	/*
3816fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3817fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3818b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3819fa9e4066Sahrens 	 */
3820fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3821503ad85cSMatthew Ahrens 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3822420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3823b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3824b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3825503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
3826fa9e4066Sahrens 	}
3827fa9e4066Sahrens 
3828fa9e4066Sahrens 	/*
3829fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3830fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3831fa9e4066Sahrens 	 * and all of its snapshots.
3832fa9e4066Sahrens 	 */
3833b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
38340b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3835fa9e4066Sahrens 
3836fa9e4066Sahrens 	/*
3837fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3838fa9e4066Sahrens 	 */
38393b2aab18SMatthew Ahrens 	VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
38403b2aab18SMatthew Ahrens 	    FTAG, &os));
3841fa9e4066Sahrens 
3842fa9e4066Sahrens 	/*
3843fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3844fa9e4066Sahrens 	 */
384555da60b9SMark J Musante 	error = ztest_dataset_create(name);
3846fa9e4066Sahrens 	if (error) {
3847fa9e4066Sahrens 		if (error == ENOSPC) {
3848b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3849f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
3850fa9e4066Sahrens 			return;
3851fa9e4066Sahrens 		}
3852fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3853fa9e4066Sahrens 	}
3854fa9e4066Sahrens 
38553b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3856b24ab676SJeff Bonwick 
3857420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3858fa9e4066Sahrens 
3859fa9e4066Sahrens 	/*
3860fa9e4066Sahrens 	 * Open the intent log for it.
3861fa9e4066Sahrens 	 */
3862b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3863fa9e4066Sahrens 
3864fa9e4066Sahrens 	/*
3865b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3866b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3867fa9e4066Sahrens 	 */
3868b24ab676SJeff Bonwick 	iters = ztest_random(5);
3869b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3870b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3871b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3872b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3873fa9e4066Sahrens 	}
3874fa9e4066Sahrens 
3875fa9e4066Sahrens 	/*
3876fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3877fa9e4066Sahrens 	 */
3878b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3879b24ab676SJeff Bonwick 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3880fa9e4066Sahrens 
3881fa9e4066Sahrens 	/*
3882503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3883745cd3c5Smaybee 	 */
3884b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3885503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3886503ad85cSMatthew Ahrens 
3887503ad85cSMatthew Ahrens 	/*
3888b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3889503ad85cSMatthew Ahrens 	 */
3890b24ab676SJeff Bonwick 	VERIFY3U(EBUSY, ==,
3891b24ab676SJeff Bonwick 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3892fa9e4066Sahrens 
3893fa9e4066Sahrens 	zil_close(zilog);
3894503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3895b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3896fa9e4066Sahrens 
3897f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
3898fa9e4066Sahrens }
3899fa9e4066Sahrens 
3900fa9e4066Sahrens /*
3901fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3902fa9e4066Sahrens  */
3903fa9e4066Sahrens void
3904b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3905fa9e4066Sahrens {
3906f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3907b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
3908b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
3909f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
39104f5064b7SMark J Musante }
39114f5064b7SMark J Musante 
3912718d718aSGeorge Wilson /*
3913718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
3914718d718aSGeorge Wilson  */
3915718d718aSGeorge Wilson void
3916b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3917718d718aSGeorge Wilson {
39189adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
39199adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
39209adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
39219adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
39229adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3923718d718aSGeorge Wilson 	int error;
3924718d718aSGeorge Wilson 
39259adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
39269adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
39279adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
39289adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
39299adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
39309adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
39319adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
39329adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
39339adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
39349adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
3935718d718aSGeorge Wilson 
39363b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
3937718d718aSGeorge Wilson 	if (error && error != ENOENT)
39383b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
39393b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
3940718d718aSGeorge Wilson 	if (error && error != ENOENT)
39413b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
39423b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
3943718d718aSGeorge Wilson 	if (error && error != ENOENT)
39443b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
39453b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
3946718d718aSGeorge Wilson 	if (error && error != ENOENT)
39473b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
39483b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
3949718d718aSGeorge Wilson 	if (error && error != ENOENT)
39503b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3951718d718aSGeorge Wilson }
3952718d718aSGeorge Wilson 
39534f5064b7SMark J Musante /*
39544f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
39554f5064b7SMark J Musante  */
39564f5064b7SMark J Musante void
3957b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
39584f5064b7SMark J Musante {
39593b2aab18SMatthew Ahrens 	objset_t *os;
39609adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
39619adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
39629adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
39639adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
39649adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3965b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
3966b24ab676SJeff Bonwick 	int error;
39674f5064b7SMark J Musante 
3968f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
39694f5064b7SMark J Musante 
3970b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
3971718d718aSGeorge Wilson 
39729adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
39739adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
39749adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
39759adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
39769adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
39779adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
39789adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
39799adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
39809adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
39819adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
398205312e2cSMark J Musante 
39834445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3984b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3985b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3986b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3987b2d22b66SGeorge Wilson 			goto out;
3988b2d22b66SGeorge Wilson 		}
3989b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3990b2d22b66SGeorge Wilson 	}
39914f5064b7SMark J Musante 
39923b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
3993b2d22b66SGeorge Wilson 	if (error) {
3994b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3995b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3996b2d22b66SGeorge Wilson 			goto out;
3997b2d22b66SGeorge Wilson 		}
3998b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3999b2d22b66SGeorge Wilson 	}
40004f5064b7SMark J Musante 
40014445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4002b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4003b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4004b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4005b2d22b66SGeorge Wilson 			goto out;
4006b2d22b66SGeorge Wilson 		}
4007b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4008b2d22b66SGeorge Wilson 	}
40094f5064b7SMark J Musante 
40104445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4011b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
4012b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4013b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4014b2d22b66SGeorge Wilson 			goto out;
4015b2d22b66SGeorge Wilson 		}
4016b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4017b2d22b66SGeorge Wilson 	}
40184f5064b7SMark J Musante 
40193b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
4020b2d22b66SGeorge Wilson 	if (error) {
4021b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
4022b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
4023b2d22b66SGeorge Wilson 			goto out;
4024b2d22b66SGeorge Wilson 		}
4025b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4026b2d22b66SGeorge Wilson 	}
40274f5064b7SMark J Musante 
40283b2aab18SMatthew Ahrens 	error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
40294f5064b7SMark J Musante 	if (error)
40303b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4031681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
40325d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
40335d7b4d43SMatthew Ahrens 		dmu_objset_disown(os, FTAG);
40345d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
40355d7b4d43SMatthew Ahrens 		goto out;
40365d7b4d43SMatthew Ahrens 	}
40374f5064b7SMark J Musante 	if (error != EBUSY)
40384f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
40394f5064b7SMark J Musante 		    error);
40403b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
40414f5064b7SMark J Musante 
4042b2d22b66SGeorge Wilson out:
4043b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
40444f5064b7SMark J Musante 
4045f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
4046fa9e4066Sahrens }
4047fa9e4066Sahrens 
4048fa9e4066Sahrens /*
4049fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
4050fa9e4066Sahrens  */
4051fa9e4066Sahrens void
4052b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4053fa9e4066Sahrens {
4054b24ab676SJeff Bonwick 	ztest_od_t od[4];
4055b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
4056fa9e4066Sahrens 
405754811da5SToomas Soome 	for (int b = 0; b < batchsize; b++) {
405854811da5SToomas Soome 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
405954811da5SToomas Soome 		    0, 0, 0);
406054811da5SToomas Soome 	}
4061fa9e4066Sahrens 
4062fa9e4066Sahrens 	/*
4063b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
4064b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
4065fa9e4066Sahrens 	 */
4066b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
4067b24ab676SJeff Bonwick 		return;
4068fa9e4066Sahrens 
4069b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
4070b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
4071b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4072fa9e4066Sahrens }
4073fa9e4066Sahrens 
407454811da5SToomas Soome /*
407554811da5SToomas Soome  * Rewind the global allocator to verify object allocation backfilling.
407654811da5SToomas Soome  */
407754811da5SToomas Soome void
407854811da5SToomas Soome ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
407954811da5SToomas Soome {
408054811da5SToomas Soome 	objset_t *os = zd->zd_os;
408154811da5SToomas Soome 	int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
408254811da5SToomas Soome 	uint64_t object;
408354811da5SToomas Soome 
408454811da5SToomas Soome 	/*
408554811da5SToomas Soome 	 * Rewind the global allocator randomly back to a lower object number
408654811da5SToomas Soome 	 * to force backfilling and reclamation of recently freed dnodes.
408754811da5SToomas Soome 	 */
408854811da5SToomas Soome 	mutex_enter(&os->os_obj_lock);
408954811da5SToomas Soome 	object = ztest_random(os->os_obj_next_chunk);
409054811da5SToomas Soome 	os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
409154811da5SToomas Soome 	mutex_exit(&os->os_obj_lock);
409254811da5SToomas Soome }
409354811da5SToomas Soome 
4094fa9e4066Sahrens /*
4095fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
4096fa9e4066Sahrens  */
4097fa9e4066Sahrens void
4098b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4099fa9e4066Sahrens {
4100b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4101b24ab676SJeff Bonwick 	ztest_od_t od[2];
4102fa9e4066Sahrens 	dmu_tx_t *tx;
4103fa9e4066Sahrens 	int i, freeit, error;
4104fa9e4066Sahrens 	uint64_t n, s, txg;
4105fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4106b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4107b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4108fa9e4066Sahrens 	uint64_t regions = 997;
4109fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
4110fa9e4066Sahrens 	uint64_t width = 40;
4111fa9e4066Sahrens 	int free_percent = 5;
4112fa9e4066Sahrens 
4113fa9e4066Sahrens 	/*
4114fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
4115fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
4116fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
4117fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
4118fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
4119fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
4120fa9e4066Sahrens 	 *
4121fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
4122fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
4123fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
4124fa9e4066Sahrens 	 *
4125fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
4126fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
4127fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
4128fa9e4066Sahrens 	 *
4129fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
4130fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
4131fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
4132fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
4133fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
4134fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
4135fa9e4066Sahrens 	 */
4136fa9e4066Sahrens 
4137fa9e4066Sahrens 	/*
4138fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
4139fa9e4066Sahrens 	 */
414054811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
414154811da5SToomas Soome 	    chunksize);
414254811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
414354811da5SToomas Soome 	    chunksize);
4144fa9e4066Sahrens 
4145b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4146b24ab676SJeff Bonwick 		return;
4147fa9e4066Sahrens 
4148b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4149b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4150b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
4151b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
4152fa9e4066Sahrens 
4153fa9e4066Sahrens 	/*
4154fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
4155fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
4156fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
4157fa9e4066Sahrens 	 * handle this race correctly.
4158fa9e4066Sahrens 	 */
4159fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4160fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
4161a2cdcdd2SPaul Dagnelie 	dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4162a2cdcdd2SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_READ);
4163fa9e4066Sahrens 
4164fa9e4066Sahrens 	/*
4165fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
4166fa9e4066Sahrens 	 */
4167fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
4168fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
4169fa9e4066Sahrens 
4170fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
4171fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
4172fa9e4066Sahrens 
4173b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4174b24ab676SJeff Bonwick 	bigsize = s * chunksize;
4175fa9e4066Sahrens 
4176fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4177fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4178fa9e4066Sahrens 
4179fa9e4066Sahrens 	/*
4180fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
4181fa9e4066Sahrens 	 * overwriting it.
4182fa9e4066Sahrens 	 */
4183fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
4184fa9e4066Sahrens 
4185fa9e4066Sahrens 	/*
4186fa9e4066Sahrens 	 * Read the current contents of our objects.
4187fa9e4066Sahrens 	 */
4188b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
41897bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4190fb09f5aaSMadhav Suresh 	ASSERT0(error);
4191b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
41927bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
4193fb09f5aaSMadhav Suresh 	ASSERT0(error);
4194fa9e4066Sahrens 
4195fa9e4066Sahrens 	/*
4196fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
4197fa9e4066Sahrens 	 */
4198fa9e4066Sahrens 	tx = dmu_tx_create(os);
4199fa9e4066Sahrens 
4200b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
4201fa9e4066Sahrens 
4202fa9e4066Sahrens 	if (freeit)
4203b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4204fa9e4066Sahrens 	else
4205b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4206fa9e4066Sahrens 
4207be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
4208be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
4209be9000ccSMatthew Ahrens 
4210b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4211b24ab676SJeff Bonwick 	if (txg == 0) {
4212fa9e4066Sahrens 		umem_free(packbuf, packsize);
4213fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
4214fa9e4066Sahrens 		return;
4215fa9e4066Sahrens 	}
4216fa9e4066Sahrens 
42175d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
42185d7b4d43SMatthew Ahrens 	do {
42195d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
42205d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
42215d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
42225d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
4223b24ab676SJeff Bonwick 
42245d7b4d43SMatthew Ahrens 	enum zio_compress comp;
42255d7b4d43SMatthew Ahrens 	do {
42265d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
42275d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
42285d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
42295d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
4230fa9e4066Sahrens 
4231fa9e4066Sahrens 	/*
4232fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
4233fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
4234fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
4235fa9e4066Sahrens 	 * with the new values we want to write out.
4236fa9e4066Sahrens 	 */
4237fa9e4066Sahrens 	for (i = 0; i < s; i++) {
4238fa9e4066Sahrens 		/* LINTED */
4239fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4240fa9e4066Sahrens 		/* LINTED */
4241b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4242fa9e4066Sahrens 		/* LINTED */
4243b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4244fa9e4066Sahrens 
4245fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4246fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4247fa9e4066Sahrens 
4248fa9e4066Sahrens 		if (pack->bw_txg > txg)
4249fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
4250fa9e4066Sahrens 			    pack->bw_txg, txg);
4251fa9e4066Sahrens 
4252fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
4253fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4254fa9e4066Sahrens 			    pack->bw_index, n, i);
4255fa9e4066Sahrens 
4256fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4257fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4258fa9e4066Sahrens 
4259fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4260fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4261fa9e4066Sahrens 
4262fa9e4066Sahrens 		if (freeit) {
4263fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
4264fa9e4066Sahrens 		} else {
4265fa9e4066Sahrens 			pack->bw_index = n + i;
4266fa9e4066Sahrens 			pack->bw_txg = txg;
4267fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
4268fa9e4066Sahrens 		}
4269fa9e4066Sahrens 		*bigH = *pack;
4270fa9e4066Sahrens 		*bigT = *pack;
4271fa9e4066Sahrens 	}
4272fa9e4066Sahrens 
4273fa9e4066Sahrens 	/*
4274fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
4275fa9e4066Sahrens 	 * Now write them out.
4276fa9e4066Sahrens 	 */
4277b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4278fa9e4066Sahrens 
4279fa9e4066Sahrens 	if (freeit) {
4280420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4281fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
4282fa9e4066Sahrens 			    " txg %llx\n",
4283fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4284fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4285fa9e4066Sahrens 			    (u_longlong_t)txg);
4286fa9e4066Sahrens 		}
4287b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4288fa9e4066Sahrens 	} else {
4289420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
4290fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
4291fa9e4066Sahrens 			    " txg %llx\n",
4292fa9e4066Sahrens 			    (u_longlong_t)bigoff,
4293fa9e4066Sahrens 			    (u_longlong_t)bigsize,
4294fa9e4066Sahrens 			    (u_longlong_t)txg);
4295fa9e4066Sahrens 		}
4296b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4297fa9e4066Sahrens 	}
4298fa9e4066Sahrens 
4299fa9e4066Sahrens 	dmu_tx_commit(tx);
4300fa9e4066Sahrens 
4301fa9e4066Sahrens 	/*
4302fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
4303fa9e4066Sahrens 	 */
4304fa9e4066Sahrens 	{
4305fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4306fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4307fa9e4066Sahrens 
4308b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
43097bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
4310b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
43117bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
4312fa9e4066Sahrens 
4313fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4314fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4315fa9e4066Sahrens 
4316fa9e4066Sahrens 		umem_free(packcheck, packsize);
4317fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
4318fa9e4066Sahrens 	}
4319fa9e4066Sahrens 
4320fa9e4066Sahrens 	umem_free(packbuf, packsize);
4321fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
4322fa9e4066Sahrens }
4323fa9e4066Sahrens 
43242fdbea25SAleksandr Guzovskiy void
43252fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4326b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
43272fdbea25SAleksandr Guzovskiy {
43282fdbea25SAleksandr Guzovskiy 	uint64_t i;
43292fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
43302fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
43312fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
43322fdbea25SAleksandr Guzovskiy 
43332fdbea25SAleksandr Guzovskiy 	/*
43342fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
43352fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
43362fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
43372fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
43382fdbea25SAleksandr Guzovskiy 	 */
43392fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
43402fdbea25SAleksandr Guzovskiy 		/* LINTED */
43412fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
43422fdbea25SAleksandr Guzovskiy 		/* LINTED */
4343b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
43442fdbea25SAleksandr Guzovskiy 		/* LINTED */
4345b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
43462fdbea25SAleksandr Guzovskiy 
43472fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
43482fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
43492fdbea25SAleksandr Guzovskiy 
43502fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
43512fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
43522fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
43532fdbea25SAleksandr Guzovskiy 
43542fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
43552fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
43562fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
43572fdbea25SAleksandr Guzovskiy 
43582fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
43592fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
43602fdbea25SAleksandr Guzovskiy 
43612fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
43622fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
43632fdbea25SAleksandr Guzovskiy 
43642fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
43652fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
43662fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
43672fdbea25SAleksandr Guzovskiy 
43682fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
43692fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
43702fdbea25SAleksandr Guzovskiy 	}
43712fdbea25SAleksandr Guzovskiy }
43722fdbea25SAleksandr Guzovskiy 
43732fdbea25SAleksandr Guzovskiy void
4374b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
43752fdbea25SAleksandr Guzovskiy {
4376b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4377b24ab676SJeff Bonwick 	ztest_od_t od[2];
43782fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
43792fdbea25SAleksandr Guzovskiy 	uint64_t i;
43802fdbea25SAleksandr Guzovskiy 	int error;
43812fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
43822fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
4383b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4384b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4385b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
43862fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
43872fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
43882fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
43892fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
43902fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
4391b24ab676SJeff Bonwick 	dmu_object_info_t doi;
43922fdbea25SAleksandr Guzovskiy 
43932fdbea25SAleksandr Guzovskiy 	/*
43942fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
43952fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
43962fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
43972fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
43982fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
43992fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
44002fdbea25SAleksandr Guzovskiy 	 *
44012fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
44022fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
44032fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
44042fdbea25SAleksandr Guzovskiy 	 *
44052fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
44062fdbea25SAleksandr Guzovskiy 	 * dmu_assign_arcbuf() can be tested for object updates.
44072fdbea25SAleksandr Guzovskiy 	 */
44082fdbea25SAleksandr Guzovskiy 
44092fdbea25SAleksandr Guzovskiy 	/*
44102fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
44112fdbea25SAleksandr Guzovskiy 	 */
441254811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
441354811da5SToomas Soome 	    0, 0);
441454811da5SToomas Soome 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
441554811da5SToomas Soome 	    chunksize);
44162fdbea25SAleksandr Guzovskiy 
4417b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4418b24ab676SJeff Bonwick 		return;
44192fdbea25SAleksandr Guzovskiy 
4420b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4421b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4422b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
4423b24ab676SJeff Bonwick 	chunksize = blocksize;
4424b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
44252fdbea25SAleksandr Guzovskiy 
4426b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4427b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
4428b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
4429b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
44302fdbea25SAleksandr Guzovskiy 
44312fdbea25SAleksandr Guzovskiy 	/*
44322fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
44332fdbea25SAleksandr Guzovskiy 	 */
44342fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
44352fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
44362fdbea25SAleksandr Guzovskiy 
44372fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
44382fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
44392fdbea25SAleksandr Guzovskiy 
4440b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4441b24ab676SJeff Bonwick 	bigsize = s * chunksize;
44422fdbea25SAleksandr Guzovskiy 
44432fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
44442fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
44452fdbea25SAleksandr Guzovskiy 
4446b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
44472fdbea25SAleksandr Guzovskiy 
44482fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
44492fdbea25SAleksandr Guzovskiy 
44502fdbea25SAleksandr Guzovskiy 	/*
44512fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
44522fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
44532fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
44542fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
44552fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
44562fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
44572fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
44582fdbea25SAleksandr Guzovskiy 	 */
44592fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
44602fdbea25SAleksandr Guzovskiy 		uint64_t j;
44612fdbea25SAleksandr Guzovskiy 		uint64_t off;
44622fdbea25SAleksandr Guzovskiy 
44632fdbea25SAleksandr Guzovskiy 		/*
44642fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
44652fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
44662fdbea25SAleksandr Guzovskiy 		 * dmu_assign_arcbuf() when it can't directly
44672fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
44682fdbea25SAleksandr Guzovskiy 		 */
44692fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
447089c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
44712fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
4472b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
44732fdbea25SAleksandr Guzovskiy 			} else {
44742fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
4475b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
44762fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
4477b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
44782fdbea25SAleksandr Guzovskiy 			}
44792fdbea25SAleksandr Guzovskiy 		}
44802fdbea25SAleksandr Guzovskiy 
44812fdbea25SAleksandr Guzovskiy 		/*
44822fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
44832fdbea25SAleksandr Guzovskiy 		 */
44842fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
44852fdbea25SAleksandr Guzovskiy 
4486b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
4487b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
44882fdbea25SAleksandr Guzovskiy 
4489b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4490b24ab676SJeff Bonwick 		if (txg == 0) {
44912fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
44922fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
44932fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
449489c86e32SChris Williamson 				if (i != 5 ||
449589c86e32SChris Williamson 				    chunksize < (SPA_MINBLOCKSIZE * 2)) {
44962fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
44972fdbea25SAleksandr Guzovskiy 				} else {
44982fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
44992fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
45002fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
45012fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
45022fdbea25SAleksandr Guzovskiy 				}
45032fdbea25SAleksandr Guzovskiy 			}
45042fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
45052fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
45062fdbea25SAleksandr Guzovskiy 			return;
45072fdbea25SAleksandr Guzovskiy 		}
45082fdbea25SAleksandr Guzovskiy 
45092fdbea25SAleksandr Guzovskiy 		/*
45102fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
45112fdbea25SAleksandr Guzovskiy 		 * test dmu_assign_arcbuf() for the case when there're no
45122fdbea25SAleksandr Guzovskiy 		 * existing dbufs for the specified offsets.
45132fdbea25SAleksandr Guzovskiy 		 */
45142fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
4515b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
45167bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
4517fb09f5aaSMadhav Suresh 			ASSERT0(error);
4518b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
45197bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
4520fb09f5aaSMadhav Suresh 			ASSERT0(error);
45212fdbea25SAleksandr Guzovskiy 		}
45222fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4523b24ab676SJeff Bonwick 		    n, chunksize, txg);
45242fdbea25SAleksandr Guzovskiy 
45252fdbea25SAleksandr Guzovskiy 		/*
45262fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
45272fdbea25SAleksandr Guzovskiy 		 * Now write them out.
45282fdbea25SAleksandr Guzovskiy 		 */
4529b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4530420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
45312fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
45322fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
45332fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
45342fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
45352fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
45362fdbea25SAleksandr Guzovskiy 		}
4537b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
45382fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
453989c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
45402fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
4541b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
45422fdbea25SAleksandr Guzovskiy 			} else {
45432fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
45442fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
4545b24ab676SJeff Bonwick 				    chunksize / 2);
45462fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
4547b24ab676SJeff Bonwick 				    chunksize / 2,
45482fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
4549b24ab676SJeff Bonwick 				    chunksize / 2);
45502fdbea25SAleksandr Guzovskiy 			}
45512fdbea25SAleksandr Guzovskiy 
45522fdbea25SAleksandr Guzovskiy 			if (i == 1) {
4553b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
455447cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
45552fdbea25SAleksandr Guzovskiy 			}
455689c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
45572fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
45582fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
45592fdbea25SAleksandr Guzovskiy 			} else {
45602fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
45612fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
45622fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db,
4563b24ab676SJeff Bonwick 				    off + chunksize / 2,
45642fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
45652fdbea25SAleksandr Guzovskiy 			}
45662fdbea25SAleksandr Guzovskiy 			if (i == 1) {
45672fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
45682fdbea25SAleksandr Guzovskiy 			}
45692fdbea25SAleksandr Guzovskiy 		}
45702fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
45712fdbea25SAleksandr Guzovskiy 
45722fdbea25SAleksandr Guzovskiy 		/*
45732fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
45742fdbea25SAleksandr Guzovskiy 		 */
45752fdbea25SAleksandr Guzovskiy 		{
45762fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
45772fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
45782fdbea25SAleksandr Guzovskiy 
4579b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
45807bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
4581b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
45827bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
45832fdbea25SAleksandr Guzovskiy 
45842fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
45852fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
45862fdbea25SAleksandr Guzovskiy 
45872fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
45882fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
45892fdbea25SAleksandr Guzovskiy 		}
45902fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4591b24ab676SJeff Bonwick 			txg_wait_open(dmu_objset_pool(os), 0);
4592b24ab676SJeff Bonwick 		} else if (i == 3) {
4593b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4594fa9e4066Sahrens 		}
4595e05725b1Sbonwick 	}
4596e05725b1Sbonwick 
4597b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4598b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4599b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4600b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4601b24ab676SJeff Bonwick }
4602fa9e4066Sahrens 
4603b24ab676SJeff Bonwick /* ARGSUSED */
4604b24ab676SJeff Bonwick void
4605b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4606b24ab676SJeff Bonwick {
4607b24ab676SJeff Bonwick 	ztest_od_t od[1];
4608b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4609b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4610fa9e4066Sahrens 
4611e05725b1Sbonwick 	/*
4612b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4613b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4614b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4615e05725b1Sbonwick 	 */
461654811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
461754811da5SToomas Soome 	    0, 0, 0);
4618fa9e4066Sahrens 
4619b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4620e05725b1Sbonwick 		return;
4621fa9e4066Sahrens 
4622b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4623b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4624b24ab676SJeff Bonwick }
4625fa9e4066Sahrens 
4626b24ab676SJeff Bonwick void
4627b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4628b24ab676SJeff Bonwick {
4629b24ab676SJeff Bonwick 	ztest_od_t od[1];
4630b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4631b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4632b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4633b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4634b24ab676SJeff Bonwick 	void *data;
4635fa9e4066Sahrens 
463654811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
463754811da5SToomas Soome 	    0, 0);
4638fa9e4066Sahrens 
4639b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4640e05725b1Sbonwick 		return;
46410e34b6a7Sbonwick 
4642b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4643f4a72450SJeff Bonwick 		return;
4644f4a72450SJeff Bonwick 
4645b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4646fa9e4066Sahrens 
4647b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4648b24ab676SJeff Bonwick 
4649b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4650b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4651b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4652b24ab676SJeff Bonwick 		    data) != 0)
4653b24ab676SJeff Bonwick 			break;
4654b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4655b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4656b24ab676SJeff Bonwick 	}
4657b24ab676SJeff Bonwick 
4658b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4659fa9e4066Sahrens }
4660fa9e4066Sahrens 
4661fa9e4066Sahrens /*
4662fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4663fa9e4066Sahrens  */
4664fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4665fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4666fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4667fa9e4066Sahrens 
4668fa9e4066Sahrens void
4669b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4670fa9e4066Sahrens {
4671b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4672b24ab676SJeff Bonwick 	ztest_od_t od[1];
4673fa9e4066Sahrens 	uint64_t object;
4674fa9e4066Sahrens 	uint64_t txg, last_txg;
4675fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4676fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4677fa9e4066Sahrens 	int i, ints;
4678fa9e4066Sahrens 	dmu_tx_t *tx;
4679fa9e4066Sahrens 	char propname[100], txgname[100];
4680fa9e4066Sahrens 	int error;
4681fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4682fa9e4066Sahrens 
468354811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4684fa9e4066Sahrens 
4685b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4686b24ab676SJeff Bonwick 		return;
4687fa9e4066Sahrens 
4688b24ab676SJeff Bonwick 	object = od[0].od_object;
4689fa9e4066Sahrens 
4690b24ab676SJeff Bonwick 	/*
4691b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4692b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4693b24ab676SJeff Bonwick 	 */
4694b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4695b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4696b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4697b24ab676SJeff Bonwick 	if (txg == 0)
4698b24ab676SJeff Bonwick 		return;
4699b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4700b24ab676SJeff Bonwick 		value[i] = i;
4701b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4702b24ab676SJeff Bonwick 		    1, &value[i], tx));
4703b24ab676SJeff Bonwick 	}
4704b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4705b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4706b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4707b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4708b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4709b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4710b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4711fa9e4066Sahrens 	}
4712b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4713b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4714b24ab676SJeff Bonwick 	}
4715b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4716fa9e4066Sahrens 
4717b24ab676SJeff Bonwick 	/*
4718b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4719b24ab676SJeff Bonwick 	 */
4720fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4721fa9e4066Sahrens 
4722e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4723e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4724e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4725e05725b1Sbonwick 	bzero(value, sizeof (value));
4726e05725b1Sbonwick 	last_txg = 0;
4727fa9e4066Sahrens 
4728e05725b1Sbonwick 	/*
4729e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4730e05725b1Sbonwick 	 */
4731e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4732e05725b1Sbonwick 	if (error == 0) {
4733e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4734e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4735fa9e4066Sahrens 
4736e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4737e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4738fa9e4066Sahrens 
4739e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4740e05725b1Sbonwick 		    &zl_ints) == 0);
4741fa9e4066Sahrens 
4742e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4743e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4744fa9e4066Sahrens 
4745e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4746e05725b1Sbonwick 		    zl_ints, value) == 0);
4747fa9e4066Sahrens 
4748e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4749e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4750fa9e4066Sahrens 		}
4751e05725b1Sbonwick 	} else {
4752e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4753e05725b1Sbonwick 	}
4754fa9e4066Sahrens 
4755e05725b1Sbonwick 	/*
4756e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4757e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4758e05725b1Sbonwick 	 * in which the property was last updated.  The second
4759e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4760e05725b1Sbonwick 	 * should be txg + object + n.
4761e05725b1Sbonwick 	 */
4762e05725b1Sbonwick 	tx = dmu_tx_create(os);
4763b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4764b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4765b24ab676SJeff Bonwick 	if (txg == 0)
4766e05725b1Sbonwick 		return;
4767fa9e4066Sahrens 
4768e05725b1Sbonwick 	if (last_txg > txg)
4769e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4770fa9e4066Sahrens 
4771e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4772e05725b1Sbonwick 		value[i] = txg + object + i;
4773fa9e4066Sahrens 
4774b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4775b24ab676SJeff Bonwick 	    1, &txg, tx));
4776b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4777b24ab676SJeff Bonwick 	    ints, value, tx));
4778fa9e4066Sahrens 
4779e05725b1Sbonwick 	dmu_tx_commit(tx);
4780fa9e4066Sahrens 
4781e05725b1Sbonwick 	/*
4782e05725b1Sbonwick 	 * Remove a random pair of entries.
4783e05725b1Sbonwick 	 */
4784e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4785e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4786e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4787fa9e4066Sahrens 
4788e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4789fa9e4066Sahrens 
4790e05725b1Sbonwick 	if (error == ENOENT)
4791e05725b1Sbonwick 		return;
4792fa9e4066Sahrens 
4793fb09f5aaSMadhav Suresh 	ASSERT0(error);
4794fa9e4066Sahrens 
4795e05725b1Sbonwick 	tx = dmu_tx_create(os);
4796b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4797b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4798b24ab676SJeff Bonwick 	if (txg == 0)
479912a2833aSSanjeev Bagewadi 		return;
4800b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4801b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
480212a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
480312a2833aSSanjeev Bagewadi }
480412a2833aSSanjeev Bagewadi 
480512a2833aSSanjeev Bagewadi /*
480612a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
480712a2833aSSanjeev Bagewadi  */
480812a2833aSSanjeev Bagewadi void
4809b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
481012a2833aSSanjeev Bagewadi {
4811b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4812b24ab676SJeff Bonwick 	ztest_od_t od[1];
4813b24ab676SJeff Bonwick 	uint64_t object, txg;
481412a2833aSSanjeev Bagewadi 
481554811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
481612a2833aSSanjeev Bagewadi 
4817b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4818b24ab676SJeff Bonwick 		return;
481912a2833aSSanjeev Bagewadi 
4820b24ab676SJeff Bonwick 	object = od[0].od_object;
482112a2833aSSanjeev Bagewadi 
482212a2833aSSanjeev Bagewadi 	/*
4823b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
482412a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4825b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
482612a2833aSSanjeev Bagewadi 	 */
4827b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
48289adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
4829b24ab676SJeff Bonwick 		uint64_t value = i;
4830b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4831b24ab676SJeff Bonwick 		int error;
483212a2833aSSanjeev Bagewadi 
4833b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4834b24ab676SJeff Bonwick 		    id, value);
483512a2833aSSanjeev Bagewadi 
4836b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4837b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4838b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4839b24ab676SJeff Bonwick 		if (txg == 0)
484012a2833aSSanjeev Bagewadi 			return;
4841b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4842b24ab676SJeff Bonwick 		    &value, tx);
484312a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
484412a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
484512a2833aSSanjeev Bagewadi 	}
4846fa9e4066Sahrens }
4847fa9e4066Sahrens 
4848b24ab676SJeff Bonwick /* ARGSUSED */
4849fa9e4066Sahrens void
4850b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4851fa9e4066Sahrens {
4852b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4853b24ab676SJeff Bonwick 	ztest_od_t od[1];
4854fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4855fa9e4066Sahrens 	dmu_tx_t *tx;
4856fa9e4066Sahrens 	int i, namelen, error;
4857b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4858fa9e4066Sahrens 	char name[20], string_value[20];
4859fa9e4066Sahrens 	void *data;
4860fa9e4066Sahrens 
486154811da5SToomas Soome 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
486254811da5SToomas Soome 	    0, 0, 0);
4863b24ab676SJeff Bonwick 
4864b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4865b24ab676SJeff Bonwick 		return;
4866b24ab676SJeff Bonwick 
4867b24ab676SJeff Bonwick 	object = od[0].od_object;
4868b24ab676SJeff Bonwick 
4869e05725b1Sbonwick 	/*
4870e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4871e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4872e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4873e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4874e05725b1Sbonwick 	 */
4875e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4876fa9e4066Sahrens 
4877e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4878e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4879e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4880e05725b1Sbonwick 		name[i] = '.';
4881e05725b1Sbonwick 	name[i] = '\0';
4882fa9e4066Sahrens 
4883b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4884e05725b1Sbonwick 		wsize = sizeof (txg);
4885e05725b1Sbonwick 		wc = 1;
4886e05725b1Sbonwick 		data = &txg;
4887e05725b1Sbonwick 	} else {
4888e05725b1Sbonwick 		wsize = 1;
4889e05725b1Sbonwick 		wc = namelen;
4890e05725b1Sbonwick 		data = string_value;
4891e05725b1Sbonwick 	}
4892fa9e4066Sahrens 
4893e05725b1Sbonwick 	count = -1ULL;
48943b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4895e05725b1Sbonwick 	ASSERT(count != -1ULL);
4896fa9e4066Sahrens 
4897e05725b1Sbonwick 	/*
4898e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4899e05725b1Sbonwick 	 */
4900e05725b1Sbonwick 	i = ztest_random(5);
4901e05725b1Sbonwick 
4902e05725b1Sbonwick 	if (i >= 2) {
4903e05725b1Sbonwick 		tx = dmu_tx_create(os);
4904b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4905b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4906b24ab676SJeff Bonwick 		if (txg == 0)
4907e05725b1Sbonwick 			return;
4908e05725b1Sbonwick 		bcopy(name, string_value, namelen);
4909e05725b1Sbonwick 	} else {
4910e05725b1Sbonwick 		tx = NULL;
4911e05725b1Sbonwick 		txg = 0;
4912e05725b1Sbonwick 		bzero(string_value, namelen);
4913e05725b1Sbonwick 	}
4914fa9e4066Sahrens 
4915e05725b1Sbonwick 	switch (i) {
4916fa9e4066Sahrens 
4917e05725b1Sbonwick 	case 0:
4918e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4919e05725b1Sbonwick 		if (error == 0) {
4920e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
4921e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
4922e05725b1Sbonwick 		} else {
4923e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4924e05725b1Sbonwick 		}
4925e05725b1Sbonwick 		break;
4926fa9e4066Sahrens 
4927e05725b1Sbonwick 	case 1:
4928e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
4929e05725b1Sbonwick 		if (error == 0) {
4930e05725b1Sbonwick 			if (data == string_value &&
4931e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
4932e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
4933e05725b1Sbonwick 				    name, data, namelen);
4934e05725b1Sbonwick 		} else {
4935e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4936e05725b1Sbonwick 		}
4937e05725b1Sbonwick 		break;
4938fa9e4066Sahrens 
4939e05725b1Sbonwick 	case 2:
4940e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
4941e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
4942e05725b1Sbonwick 		break;
4943fa9e4066Sahrens 
4944e05725b1Sbonwick 	case 3:
4945e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4946e05725b1Sbonwick 		break;
4947fa9e4066Sahrens 
4948e05725b1Sbonwick 	case 4:
4949e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
4950e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
4951e05725b1Sbonwick 		break;
4952fa9e4066Sahrens 	}
4953e05725b1Sbonwick 
4954e05725b1Sbonwick 	if (tx != NULL)
4955e05725b1Sbonwick 		dmu_tx_commit(tx);
4956fa9e4066Sahrens }
4957fa9e4066Sahrens 
4958d20e665cSRicardo M. Correia /*
4959d20e665cSRicardo M. Correia  * Commit callback data.
4960d20e665cSRicardo M. Correia  */
4961d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
4962d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
4963d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
4964d20e665cSRicardo M. Correia 	int			zcd_expected_err;
4965d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
4966d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
4967d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
4968d20e665cSRicardo M. Correia } ztest_cb_data_t;
4969d20e665cSRicardo M. Correia 
4970d20e665cSRicardo M. Correia /* This is the actual commit callback function */
4971d20e665cSRicardo M. Correia static void
4972d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
4973d20e665cSRicardo M. Correia {
4974d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
4975d20e665cSRicardo M. Correia 	uint64_t synced_txg;
4976d20e665cSRicardo M. Correia 
4977d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
4978d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
4979d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
4980d20e665cSRicardo M. Correia 
4981d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
4982d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
4983d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4984d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4985d20e665cSRicardo M. Correia 		    synced_txg);
4986d20e665cSRicardo M. Correia 
4987d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
4988d20e665cSRicardo M. Correia 
4989d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
4990fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
4991d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
4992d20e665cSRicardo M. Correia 
4993d20e665cSRicardo M. Correia 		/*
4994d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
4995d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
4996d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
4997d20e665cSRicardo M. Correia 		 */
4998d20e665cSRicardo M. Correia 		return;
4999d20e665cSRicardo M. Correia 	}
5000d20e665cSRicardo M. Correia 
5001d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
5002d20e665cSRicardo M. Correia 	if (!data->zcd_added)
5003d20e665cSRicardo M. Correia 		goto out;
5004d20e665cSRicardo M. Correia 
5005d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
5006d20e665cSRicardo M. Correia 
5007d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
5008f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5009d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
5010f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5011d20e665cSRicardo M. Correia 
5012d20e665cSRicardo M. Correia out:
5013d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
5014d20e665cSRicardo M. Correia }
5015d20e665cSRicardo M. Correia 
5016d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
5017d20e665cSRicardo M. Correia static ztest_cb_data_t *
5018d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
5019d20e665cSRicardo M. Correia {
5020d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
5021d20e665cSRicardo M. Correia 
5022d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5023d20e665cSRicardo M. Correia 
5024d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
5025d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
5026d20e665cSRicardo M. Correia 
5027d20e665cSRicardo M. Correia 	return (cb_data);
5028d20e665cSRicardo M. Correia }
5029d20e665cSRicardo M. Correia 
5030d20e665cSRicardo M. Correia /*
5031d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
5032d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
5033d20e665cSRicardo M. Correia  * implementation bug.
5034d20e665cSRicardo M. Correia  */
5035d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
5036d20e665cSRicardo M. Correia 
5037d20e665cSRicardo M. Correia /*
5038d20e665cSRicardo M. Correia  * Commit callback test.
5039d20e665cSRicardo M. Correia  */
5040d20e665cSRicardo M. Correia void
5041b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5042d20e665cSRicardo M. Correia {
5043b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5044b24ab676SJeff Bonwick 	ztest_od_t od[1];
5045d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
5046d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
5047d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
5048d20e665cSRicardo M. Correia 	int i, error;
5049d20e665cSRicardo M. Correia 
505054811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5051b24ab676SJeff Bonwick 
5052b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5053b24ab676SJeff Bonwick 		return;
5054b24ab676SJeff Bonwick 
5055d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
5056d20e665cSRicardo M. Correia 
5057d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
5058d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5059d20e665cSRicardo M. Correia 
5060b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
5061d20e665cSRicardo M. Correia 
5062d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
5063d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
5064d20e665cSRicardo M. Correia 		error = -1;
5065d20e665cSRicardo M. Correia 
5066d20e665cSRicardo M. Correia 	if (!error)
5067d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
5068d20e665cSRicardo M. Correia 
5069d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
5070d20e665cSRicardo M. Correia 
5071d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
5072d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
5073d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5074d20e665cSRicardo M. Correia 
5075d20e665cSRicardo M. Correia 	if (error) {
5076d20e665cSRicardo M. Correia 		/*
5077d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
5078d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
5079d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
5080d20e665cSRicardo M. Correia 		 * so we will check for that.
5081d20e665cSRicardo M. Correia 		 */
5082d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5083d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
5084d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
5085d20e665cSRicardo M. Correia 		}
5086d20e665cSRicardo M. Correia 
5087d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
5088d20e665cSRicardo M. Correia 
5089d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
5090d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
5091d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5092d20e665cSRicardo M. Correia 		}
5093d20e665cSRicardo M. Correia 
5094d20e665cSRicardo M. Correia 		return;
5095d20e665cSRicardo M. Correia 	}
5096d20e665cSRicardo M. Correia 
5097d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
5098d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5099d20e665cSRicardo M. Correia 
5100d20e665cSRicardo M. Correia 	/*
5101d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
5102d20e665cSRicardo M. Correia 	 */
5103b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
5104d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
5105d20e665cSRicardo M. Correia 
5106d20e665cSRicardo M. Correia 	if (old_txg > txg)
5107d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5108d20e665cSRicardo M. Correia 		    old_txg, txg);
5109d20e665cSRicardo M. Correia 
5110b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
5111d20e665cSRicardo M. Correia 
5112f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
5113d20e665cSRicardo M. Correia 
5114d20e665cSRicardo M. Correia 	/*
5115d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
5116d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
5117d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
5118d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
5119d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
5120d20e665cSRicardo M. Correia 	 * implementation.
5121d20e665cSRicardo M. Correia 	 *
5122d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
5123d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
5124d20e665cSRicardo M. Correia 	 * implementation bug..
5125d20e665cSRicardo M. Correia 	 */
5126d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
5127d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
5128b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
5129d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5130d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5131d20e665cSRicardo M. Correia 	}
5132d20e665cSRicardo M. Correia 
5133d20e665cSRicardo M. Correia 	/*
5134d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
5135d20e665cSRicardo M. Correia 	 *
5136d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
5137d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
5138d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
5139d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
5140d20e665cSRicardo M. Correia 	 */
5141d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
5142d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5143d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5144d20e665cSRicardo M. Correia 
5145d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
5146d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
5147d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
5148d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5149d20e665cSRicardo M. Correia 		else
5150d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5151d20e665cSRicardo M. Correia 			    cb_data[i]);
5152d20e665cSRicardo M. Correia 
5153d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
5154d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
5155d20e665cSRicardo M. Correia 
5156d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
5157d20e665cSRicardo M. Correia 	}
5158d20e665cSRicardo M. Correia 
5159f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
5160d20e665cSRicardo M. Correia 
5161d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
5162d20e665cSRicardo M. Correia }
5163d20e665cSRicardo M. Correia 
516454811da5SToomas Soome /*
516554811da5SToomas Soome  * Visit each object in the dataset. Verify that its properties
516654811da5SToomas Soome  * are consistent what was stored in the block tag when it was created,
516754811da5SToomas Soome  * and that its unused bonus buffer space has not been overwritten.
516854811da5SToomas Soome  */
516954811da5SToomas Soome void
517054811da5SToomas Soome ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
517154811da5SToomas Soome {
517254811da5SToomas Soome 	objset_t *os = zd->zd_os;
517354811da5SToomas Soome 	uint64_t obj;
517454811da5SToomas Soome 	int err = 0;
517554811da5SToomas Soome 
517654811da5SToomas Soome 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
517754811da5SToomas Soome 		ztest_block_tag_t *bt = NULL;
517854811da5SToomas Soome 		dmu_object_info_t doi;
517954811da5SToomas Soome 		dmu_buf_t *db;
518054811da5SToomas Soome 
518154811da5SToomas Soome 		if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
518254811da5SToomas Soome 			continue;
518354811da5SToomas Soome 
518454811da5SToomas Soome 		dmu_object_info_from_db(db, &doi);
518554811da5SToomas Soome 		if (doi.doi_bonus_size >= sizeof (*bt))
518654811da5SToomas Soome 			bt = ztest_bt_bonus(db);
518754811da5SToomas Soome 
518854811da5SToomas Soome 		if (bt && bt->bt_magic == BT_MAGIC) {
518954811da5SToomas Soome 			ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
519054811da5SToomas Soome 			    bt->bt_offset, bt->bt_gen, bt->bt_txg,
519154811da5SToomas Soome 			    bt->bt_crtxg);
519254811da5SToomas Soome 			ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
519354811da5SToomas Soome 		}
519454811da5SToomas Soome 
519554811da5SToomas Soome 		dmu_buf_rele(db, FTAG);
519654811da5SToomas Soome 	}
519754811da5SToomas Soome }
519854811da5SToomas Soome 
5199b24ab676SJeff Bonwick /* ARGSUSED */
5200fa9e4066Sahrens void
5201b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5202fa9e4066Sahrens {
5203b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
5204b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
5205b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
5206b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
5207b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
5208b24ab676SJeff Bonwick 	};
5209fa9e4066Sahrens 
5210f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5211fa9e4066Sahrens 
5212b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5213b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5214b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5215fa9e4066Sahrens 
5216f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5217b24ab676SJeff Bonwick }
5218fa9e4066Sahrens 
52195cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
52205cabbc6bSPrashanth Sreenivasa void
52215cabbc6bSPrashanth Sreenivasa ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
52225cabbc6bSPrashanth Sreenivasa {
5223f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
52245cabbc6bSPrashanth Sreenivasa 
52255cabbc6bSPrashanth Sreenivasa 	int error = dmu_objset_remap_indirects(zd->zd_name);
52265cabbc6bSPrashanth Sreenivasa 	if (error == ENOSPC)
52275cabbc6bSPrashanth Sreenivasa 		error = 0;
52285cabbc6bSPrashanth Sreenivasa 	ASSERT0(error);
52295cabbc6bSPrashanth Sreenivasa 
5230f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
52315cabbc6bSPrashanth Sreenivasa }
52325cabbc6bSPrashanth Sreenivasa 
5233b24ab676SJeff Bonwick /* ARGSUSED */
5234b24ab676SJeff Bonwick void
5235b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5236b24ab676SJeff Bonwick {
5237b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
5238ea8dc4b6Seschrock 
5239f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5240ea8dc4b6Seschrock 
5241420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5242b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5243fa9e4066Sahrens 
5244fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
5245fa9e4066Sahrens 
5246420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5247b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
5248fa9e4066Sahrens 
5249b24ab676SJeff Bonwick 	nvlist_free(props);
5250fa9e4066Sahrens 
5251f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5252fa9e4066Sahrens }
5253fa9e4066Sahrens 
52543b2aab18SMatthew Ahrens static int
52553b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
52563b2aab18SMatthew Ahrens {
52573b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
52583b2aab18SMatthew Ahrens 	int error;
52593b2aab18SMatthew Ahrens 
52603b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
52613b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
52623b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
52633b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
52643b2aab18SMatthew Ahrens 	fnvlist_free(holds);
52653b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
52663b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
52673b2aab18SMatthew Ahrens 	return (error);
52683b2aab18SMatthew Ahrens }
52693b2aab18SMatthew Ahrens 
52705c987a37SChris Kirby /*
52715c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
52725c987a37SChris Kirby  */
52735c987a37SChris Kirby void
5274b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
52755c987a37SChris Kirby {
52765c987a37SChris Kirby 	int error;
5277b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
52785c987a37SChris Kirby 	objset_t *origin;
52795c987a37SChris Kirby 	char snapname[100];
52805c987a37SChris Kirby 	char fullname[100];
52815c987a37SChris Kirby 	char clonename[100];
52825c987a37SChris Kirby 	char tag[100];
52839adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
52843b2aab18SMatthew Ahrens 	nvlist_t *holds;
52855c987a37SChris Kirby 
5286f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
52875c987a37SChris Kirby 
52885c987a37SChris Kirby 	dmu_objset_name(os, osname);
52895c987a37SChris Kirby 
52903b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
52913b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
52923b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
52933b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
52943b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
52955c987a37SChris Kirby 
52965c987a37SChris Kirby 	/*
52975c987a37SChris Kirby 	 * Clean up from any previous run.
52985c987a37SChris Kirby 	 */
52993b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
53003b2aab18SMatthew Ahrens 	if (error != ENOENT)
53013b2aab18SMatthew Ahrens 		ASSERT0(error);
53023b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
53033b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
53043b2aab18SMatthew Ahrens 		ASSERT0(error);
53053b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
53063b2aab18SMatthew Ahrens 	if (error != ENOENT)
53073b2aab18SMatthew Ahrens 		ASSERT0(error);
53085c987a37SChris Kirby 
53095c987a37SChris Kirby 	/*
53105c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
53115c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
53125c987a37SChris Kirby 	 */
53134445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
53145c987a37SChris Kirby 	if (error) {
53155c987a37SChris Kirby 		if (error == ENOSPC) {
53165c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
53175c987a37SChris Kirby 			goto out;
53185c987a37SChris Kirby 		}
53195c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
53205c987a37SChris Kirby 	}
53215c987a37SChris Kirby 
53223b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
53235c987a37SChris Kirby 	if (error) {
53245c987a37SChris Kirby 		if (error == ENOSPC) {
53255c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
53265c987a37SChris Kirby 			goto out;
53275c987a37SChris Kirby 		}
53285c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
53295c987a37SChris Kirby 	}
53305c987a37SChris Kirby 
53313b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
53325c987a37SChris Kirby 	if (error) {
53333b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
53345c987a37SChris Kirby 		    fullname, error);
53355c987a37SChris Kirby 	}
53365c987a37SChris Kirby 
53373b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
53385c987a37SChris Kirby 	if (error)
53393b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
53405c987a37SChris Kirby 
53415c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
53425c987a37SChris Kirby 	if (error != ENOENT)
53435c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
53445c987a37SChris Kirby 
53455c987a37SChris Kirby 	/*
53465c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
53475c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
53485c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
53495c987a37SChris Kirby 	 */
53504445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
53515c987a37SChris Kirby 	if (error) {
53525c987a37SChris Kirby 		if (error == ENOSPC) {
53535c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
53545c987a37SChris Kirby 			goto out;
53555c987a37SChris Kirby 		}
53565c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
53575c987a37SChris Kirby 	}
53585c987a37SChris Kirby 
53593b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
53603b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
53613b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
53623b2aab18SMatthew Ahrens 	fnvlist_free(holds);
53633b2aab18SMatthew Ahrens 
53645d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
53655d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
53665d7b4d43SMatthew Ahrens 		goto out;
53675d7b4d43SMatthew Ahrens 	} else if (error) {
53685d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
53695d7b4d43SMatthew Ahrens 		    fullname, tag, error);
53705d7b4d43SMatthew Ahrens 	}
53715c987a37SChris Kirby 
53723b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
53735c987a37SChris Kirby 	if (error != EBUSY) {
53743b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
53755c987a37SChris Kirby 		    fullname, error);
53765c987a37SChris Kirby 	}
53775c987a37SChris Kirby 
53783b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
53795c987a37SChris Kirby 	if (error) {
53803b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
53815c987a37SChris Kirby 		    fullname, error);
53825c987a37SChris Kirby 	}
53835c987a37SChris Kirby 
53843b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
53855c987a37SChris Kirby 	if (error)
5386a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
53875c987a37SChris Kirby 
53883b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
53895c987a37SChris Kirby 
53905c987a37SChris Kirby out:
5391f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
53925c987a37SChris Kirby }
53935c987a37SChris Kirby 
5394fa9e4066Sahrens /*
5395fa9e4066Sahrens  * Inject random faults into the on-disk data.
5396fa9e4066Sahrens  */
5397b24ab676SJeff Bonwick /* ARGSUSED */
5398fa9e4066Sahrens void
5399b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5400fa9e4066Sahrens {
5401b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5402420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5403fa9e4066Sahrens 	int fd;
5404fa9e4066Sahrens 	uint64_t offset;
54051195e687SMark J Musante 	uint64_t leaves;
5406fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
5407fa9e4066Sahrens 	uint64_t top, leaf;
5408fa9e4066Sahrens 	char path0[MAXPATHLEN];
5409fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
5410fa9e4066Sahrens 	size_t fsize;
5411f34284d8SMatthew Ahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;
5412fa9e4066Sahrens 	int iters = 1000;
54131195e687SMark J Musante 	int maxfaults;
54141195e687SMark J Musante 	int mirror_save;
5415e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
5416ea8dc4b6Seschrock 	uint64_t guid0 = 0;
54178f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
5418ea8dc4b6Seschrock 
5419f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
54203a4b1be9SMatthew Ahrens 
54213a4b1be9SMatthew Ahrens 	/*
54223a4b1be9SMatthew Ahrens 	 * Device removal is in progress, fault injection must be disabled
54233a4b1be9SMatthew Ahrens 	 * until it completes and the pool is scrubbed.  The fault injection
54243a4b1be9SMatthew Ahrens 	 * strategy for damaging blocks does not take in to account evacuated
54253a4b1be9SMatthew Ahrens 	 * blocks which may have already been damaged.
54263a4b1be9SMatthew Ahrens 	 */
54273a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
54283a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
54293a4b1be9SMatthew Ahrens 		return;
54303a4b1be9SMatthew Ahrens 	}
54313a4b1be9SMatthew Ahrens 
54321195e687SMark J Musante 	maxfaults = MAXFAULTS();
5433420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
54341195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
5435f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
54361195e687SMark J Musante 
5437e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
5438fa9e4066Sahrens 
54392c1e2b44SGeorge Wilson 	/*
54402c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
54412c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
54422c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
54432c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
54442c1e2b44SGeorge Wilson 	 */
5445f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
54462c1e2b44SGeorge Wilson 
5447fa9e4066Sahrens 	/*
5448e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5449fa9e4066Sahrens 	 */
5450e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5451fa9e4066Sahrens 
5452e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
5453e14bb325SJeff Bonwick 		/*
5454b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
5455e14bb325SJeff Bonwick 		 */
5456b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
54571195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
5458fa9e4066Sahrens 
5459e14bb325SJeff Bonwick 		/*
5460e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
5461e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
5462e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
5463e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
5464e14bb325SJeff Bonwick 		 */
5465e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
5466420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5467420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
5468e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5469420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5470420dfc95SChris Siden 		    top * leaves + leaf);
5471fa9e4066Sahrens 
5472e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
54738f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
54748f18d1faSGeorge Wilson 			islog = B_TRUE;
54758f18d1faSGeorge Wilson 
54762c1e2b44SGeorge Wilson 		/*
54772c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
54782c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
54792c1e2b44SGeorge Wilson 		 * resilvering.
54802c1e2b44SGeorge Wilson 		 */
54812c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
54822c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5483b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
5484e14bb325SJeff Bonwick 			/*
5485e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
5486e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
5487e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
5488e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
5489e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
5490e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
5491e14bb325SJeff Bonwick 			 * combination of this with injection of random data
5492e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
5493e14bb325SJeff Bonwick 			 */
5494e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
5495e14bb325SJeff Bonwick 
54965cabbc6bSPrashanth Sreenivasa 			zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
54975cabbc6bSPrashanth Sreenivasa 			    (long long)vd0->vdev_id, (int)maxfaults);
54985cabbc6bSPrashanth Sreenivasa 
5499e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
5500e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
5501e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
5502e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
5503e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
5504e14bb325SJeff Bonwick 			} else {
5505e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
5506e14bb325SJeff Bonwick 			}
5507e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
5508e14bb325SJeff Bonwick 		}
5509e14bb325SJeff Bonwick 	} else {
5510e14bb325SJeff Bonwick 		/*
5511e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
5512e14bb325SJeff Bonwick 		 */
5513e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
5514fa9e4066Sahrens 
5515e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
5516e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
5517f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
5518e14bb325SJeff Bonwick 			return;
5519e14bb325SJeff Bonwick 		}
5520e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5521ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
5522e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
5523e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
5524e14bb325SJeff Bonwick 
5525e14bb325SJeff Bonwick 		leaf = 0;
5526e14bb325SJeff Bonwick 		leaves = 1;
5527e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
5528fa9e4066Sahrens 	}
5529fa9e4066Sahrens 
5530e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
5531f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5532e14bb325SJeff Bonwick 
5533fa9e4066Sahrens 	/*
55348f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
55358f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
5536fa9e4066Sahrens 	 */
55378f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
55388ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
55398ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
55408ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
55418f18d1faSGeorge Wilson 
55428f18d1faSGeorge Wilson 			/*
55438f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
55448f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
55458f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
55468f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
55478f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
55488f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
55498f18d1faSGeorge Wilson 			 */
55508f18d1faSGeorge Wilson 			if (islog)
5551f06dce2cSAndrew Stormont 				rw_enter(&ztest_name_lock, RW_WRITER);
55528f18d1faSGeorge Wilson 
55538ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
55548f18d1faSGeorge Wilson 
55558f18d1faSGeorge Wilson 			if (islog)
5556f06dce2cSAndrew Stormont 				rw_exit(&ztest_name_lock);
55578ad4d6ddSJeff Bonwick 		} else {
55589253d63dSGeorge Wilson 			/*
55599253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
55609253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
55619253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
55629253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
55639253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
55649253d63dSGeorge Wilson 			 * prevent a race between injection testing and
55659253d63dSGeorge Wilson 			 * aux_vdev removal.
55669253d63dSGeorge Wilson 			 */
5567f06dce2cSAndrew Stormont 			mutex_enter(&ztest_vdev_lock);
55688ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
5569f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
55708ad4d6ddSJeff Bonwick 		}
5571fa9e4066Sahrens 	}
5572fa9e4066Sahrens 
55738f18d1faSGeorge Wilson 	if (maxfaults == 0)
55748f18d1faSGeorge Wilson 		return;
55758f18d1faSGeorge Wilson 
5576fa9e4066Sahrens 	/*
5577ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
5578fa9e4066Sahrens 	 */
5579fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
5580fa9e4066Sahrens 
558186714001SSerapheim Dimitropoulos 	if (fd == -1) /* we hit a gap in the device namespace */
5582fa9e4066Sahrens 		return;
5583fa9e4066Sahrens 
5584fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
5585fa9e4066Sahrens 
5586fa9e4066Sahrens 	while (--iters != 0) {
5587f9eb9fdfSMatthew Ahrens 		/*
5588f9eb9fdfSMatthew Ahrens 		 * The offset must be chosen carefully to ensure that
5589f9eb9fdfSMatthew Ahrens 		 * we do not inject a given logical block with errors
5590f9eb9fdfSMatthew Ahrens 		 * on two different leaf devices, because ZFS can not
5591f9eb9fdfSMatthew Ahrens 		 * tolerate that (if maxfaults==1).
5592f9eb9fdfSMatthew Ahrens 		 *
5593f9eb9fdfSMatthew Ahrens 		 * We divide each leaf into chunks of size
5594f9eb9fdfSMatthew Ahrens 		 * (# leaves * SPA_MAXBLOCKSIZE * 4).  Within each chunk
5595f9eb9fdfSMatthew Ahrens 		 * there is a series of ranges to which we can inject errors.
5596f9eb9fdfSMatthew Ahrens 		 * Each range can accept errors on only a single leaf vdev.
5597f9eb9fdfSMatthew Ahrens 		 * The error injection ranges are separated by ranges
5598f9eb9fdfSMatthew Ahrens 		 * which we will not inject errors on any device (DMZs).
5599f9eb9fdfSMatthew Ahrens 		 * Each DMZ must be large enough such that a single block
5600f9eb9fdfSMatthew Ahrens 		 * can not straddle it, so that a single block can not be
5601f9eb9fdfSMatthew Ahrens 		 * a target in two different injection ranges (on different
5602f9eb9fdfSMatthew Ahrens 		 * leaf vdevs).
5603f9eb9fdfSMatthew Ahrens 		 *
5604f9eb9fdfSMatthew Ahrens 		 * For example, with 3 leaves, each chunk looks like:
5605f9eb9fdfSMatthew Ahrens 		 *    0 to  32M: injection range for leaf 0
5606f9eb9fdfSMatthew Ahrens 		 *  32M to  64M: DMZ - no injection allowed
5607f9eb9fdfSMatthew Ahrens 		 *  64M to  96M: injection range for leaf 1
5608f9eb9fdfSMatthew Ahrens 		 *  96M to 128M: DMZ - no injection allowed
5609f9eb9fdfSMatthew Ahrens 		 * 128M to 160M: injection range for leaf 2
5610f9eb9fdfSMatthew Ahrens 		 * 160M to 192M: DMZ - no injection allowed
5611f9eb9fdfSMatthew Ahrens 		 */
5612fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
5613fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
5614fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5615fa9e4066Sahrens 
5616f34284d8SMatthew Ahrens 		/*
5617f34284d8SMatthew Ahrens 		 * Only allow damage to the labels at one end of the vdev.
5618f34284d8SMatthew Ahrens 		 *
5619f34284d8SMatthew Ahrens 		 * If all labels are damaged, the device will be totally
5620f34284d8SMatthew Ahrens 		 * inaccessible, which will result in loss of data,
5621f34284d8SMatthew Ahrens 		 * because we also damage (parts of) the other side of
5622f34284d8SMatthew Ahrens 		 * the mirror/raidz.
5623f34284d8SMatthew Ahrens 		 *
5624f34284d8SMatthew Ahrens 		 * Additionally, we will always have both an even and an
5625f34284d8SMatthew Ahrens 		 * odd label, so that we can handle crashes in the
5626f34284d8SMatthew Ahrens 		 * middle of vdev_config_sync().
5627f34284d8SMatthew Ahrens 		 */
5628f34284d8SMatthew Ahrens 		if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5629f34284d8SMatthew Ahrens 			continue;
5630f34284d8SMatthew Ahrens 
5631f34284d8SMatthew Ahrens 		/*
5632f34284d8SMatthew Ahrens 		 * The two end labels are stored at the "end" of the disk, but
5633f34284d8SMatthew Ahrens 		 * the end of the disk (vdev_psize) is aligned to
5634f34284d8SMatthew Ahrens 		 * sizeof (vdev_label_t).
5635f34284d8SMatthew Ahrens 		 */
5636f34284d8SMatthew Ahrens 		uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5637f34284d8SMatthew Ahrens 		if ((leaf & 1) == 1 &&
5638f34284d8SMatthew Ahrens 		    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5639fa9e4066Sahrens 			continue;
5640fa9e4066Sahrens 
5641f06dce2cSAndrew Stormont 		mutex_enter(&ztest_vdev_lock);
56421195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
5643f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
56441195e687SMark J Musante 			(void) close(fd);
56451195e687SMark J Musante 			return;
56461195e687SMark J Musante 		}
5647fa9e4066Sahrens 
5648fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5649fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
5650fa9e4066Sahrens 			    offset, pathrand);
56511195e687SMark J Musante 
5652f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
56531195e687SMark J Musante 
5654420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
56551195e687SMark J Musante 			(void) printf("injected bad word into %s,"
56561195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5657fa9e4066Sahrens 	}
5658fa9e4066Sahrens 
5659fa9e4066Sahrens 	(void) close(fd);
5660fa9e4066Sahrens }
5661fa9e4066Sahrens 
5662fa9e4066Sahrens /*
5663b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
5664fa9e4066Sahrens  */
5665fa9e4066Sahrens void
5666b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5667fa9e4066Sahrens {
5668b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5669420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5670b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5671b24ab676SJeff Bonwick 	ztest_od_t od[1];
5672b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
5673b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
5674b24ab676SJeff Bonwick 	dmu_buf_t *db;
5675b24ab676SJeff Bonwick 	dmu_tx_t *tx;
5676770499e1SDan Kimmel 	abd_t *abd;
5677b24ab676SJeff Bonwick 	blkptr_t blk;
5678b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
5679fa9e4066Sahrens 
5680b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
5681b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
5682fa9e4066Sahrens 
568354811da5SToomas Soome 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
568454811da5SToomas Soome 	    0, 0);
5685fa9e4066Sahrens 
5686b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5687b24ab676SJeff Bonwick 		return;
5688fa9e4066Sahrens 
5689fa9e4066Sahrens 	/*
5690b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
5691b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
5692fa9e4066Sahrens 	 */
5693f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5694fa9e4066Sahrens 
5695b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5696b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
5697b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5698b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
5699f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5700b24ab676SJeff Bonwick 		return;
5701b24ab676SJeff Bonwick 	}
5702b24ab676SJeff Bonwick 
5703aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
5704aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5705aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5706aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5707aab80726SGeorge Wilson 
5708b24ab676SJeff Bonwick 	object = od[0].od_object;
5709b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5710aab80726SGeorge Wilson 	pattern = zs->zs_guid ^ dds.dds_guid;
5711b24ab676SJeff Bonwick 
5712b24ab676SJeff Bonwick 	ASSERT(object != 0);
5713b24ab676SJeff Bonwick 
5714b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5715b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5716b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5717b24ab676SJeff Bonwick 	if (txg == 0) {
5718f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5719b24ab676SJeff Bonwick 		return;
5720b24ab676SJeff Bonwick 	}
5721fa9e4066Sahrens 
5722fa9e4066Sahrens 	/*
5723b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5724fa9e4066Sahrens 	 */
5725b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5726b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
57273b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
57283b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
57293b2aab18SMatthew Ahrens 		if (error != 0) {
57303b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
57313b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
57323b2aab18SMatthew Ahrens 		}
5733b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5734b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5735b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5736b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5737b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5738b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5739b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5740b24ab676SJeff Bonwick 	}
5741fa9e4066Sahrens 
5742b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5743b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5744fa9e4066Sahrens 
5745fa9e4066Sahrens 	/*
5746b24ab676SJeff Bonwick 	 * Find out what block we got.
5747fa9e4066Sahrens 	 */
574880901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
574980901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5750b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5751b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5752fa9e4066Sahrens 
5753fa9e4066Sahrens 	/*
5754b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5755fa9e4066Sahrens 	 */
5756b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5757770499e1SDan Kimmel 	abd = abd_alloc_linear(psize, B_TRUE);
5758770499e1SDan Kimmel 	ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5759fa9e4066Sahrens 
5760b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5761770499e1SDan Kimmel 	    abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5762b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5763fa9e4066Sahrens 
5764770499e1SDan Kimmel 	abd_free(abd);
5765fa9e4066Sahrens 
5766f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5767fa9e4066Sahrens }
5768fa9e4066Sahrens 
5769fa9e4066Sahrens /*
5770b24ab676SJeff Bonwick  * Scrub the pool.
5771fa9e4066Sahrens  */
5772b24ab676SJeff Bonwick /* ARGSUSED */
5773b24ab676SJeff Bonwick void
5774b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5775fa9e4066Sahrens {
5776420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5777fa9e4066Sahrens 
57783a4b1be9SMatthew Ahrens 	/*
57793a4b1be9SMatthew Ahrens 	 * Scrub in progress by device removal.
57803a4b1be9SMatthew Ahrens 	 */
57813a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active)
57823a4b1be9SMatthew Ahrens 		return;
57833a4b1be9SMatthew Ahrens 
57843f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5785b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
57863f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5787b24ab676SJeff Bonwick }
5788fa9e4066Sahrens 
5789e9103aaeSGarrett D'Amore /*
5790e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5791e9103aaeSGarrett D'Amore  */
5792e9103aaeSGarrett D'Amore /* ARGSUSED */
5793e9103aaeSGarrett D'Amore void
5794e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5795e9103aaeSGarrett D'Amore {
5796420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5797e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5798dfbb9432SGeorge Wilson 	int error;
5799e9103aaeSGarrett D'Amore 
5800e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_mmp_test)
5801e0f1c0afSOlaf Faaland 		return;
5802e0f1c0afSOlaf Faaland 
5803e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5804e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5805dfbb9432SGeorge Wilson 
5806f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5807dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5808f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5809dfbb9432SGeorge Wilson 
5810dfbb9432SGeorge Wilson 	if (error != 0)
5811e9103aaeSGarrett D'Amore 		return;
5812e9103aaeSGarrett D'Amore 
581325345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5814e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5815e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5816e9103aaeSGarrett D'Amore 	}
5817e9103aaeSGarrett D'Amore 
5818e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5819e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5820e9103aaeSGarrett D'Amore }
5821e9103aaeSGarrett D'Amore 
5822094e47e9SGeorge Wilson static vdev_t *
5823094e47e9SGeorge Wilson ztest_random_concrete_vdev_leaf(vdev_t *vd)
5824094e47e9SGeorge Wilson {
5825094e47e9SGeorge Wilson 	if (vd == NULL)
5826094e47e9SGeorge Wilson 		return (NULL);
5827094e47e9SGeorge Wilson 
5828094e47e9SGeorge Wilson 	if (vd->vdev_children == 0)
5829094e47e9SGeorge Wilson 		return (vd);
5830094e47e9SGeorge Wilson 
5831094e47e9SGeorge Wilson 	vdev_t *eligible[vd->vdev_children];
5832094e47e9SGeorge Wilson 	int eligible_idx = 0, i;
5833094e47e9SGeorge Wilson 	for (i = 0; i < vd->vdev_children; i++) {
5834094e47e9SGeorge Wilson 		vdev_t *cvd = vd->vdev_child[i];
5835094e47e9SGeorge Wilson 		if (cvd->vdev_top->vdev_removing)
5836094e47e9SGeorge Wilson 			continue;
5837094e47e9SGeorge Wilson 		if (cvd->vdev_children > 0 ||
5838094e47e9SGeorge Wilson 		    (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5839094e47e9SGeorge Wilson 			eligible[eligible_idx++] = cvd;
5840094e47e9SGeorge Wilson 		}
5841094e47e9SGeorge Wilson 	}
5842094e47e9SGeorge Wilson 	VERIFY(eligible_idx > 0);
5843094e47e9SGeorge Wilson 
5844094e47e9SGeorge Wilson 	uint64_t child_no = ztest_random(eligible_idx);
5845094e47e9SGeorge Wilson 	return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5846094e47e9SGeorge Wilson }
5847094e47e9SGeorge Wilson 
5848094e47e9SGeorge Wilson /* ARGSUSED */
5849094e47e9SGeorge Wilson void
5850094e47e9SGeorge Wilson ztest_initialize(ztest_ds_t *zd, uint64_t id)
5851094e47e9SGeorge Wilson {
5852094e47e9SGeorge Wilson 	spa_t *spa = ztest_spa;
5853094e47e9SGeorge Wilson 	int error = 0;
5854094e47e9SGeorge Wilson 
5855094e47e9SGeorge Wilson 	mutex_enter(&ztest_vdev_lock);
5856094e47e9SGeorge Wilson 
5857094e47e9SGeorge Wilson 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5858094e47e9SGeorge Wilson 
5859094e47e9SGeorge Wilson 	/* Random leaf vdev */
5860094e47e9SGeorge Wilson 	vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5861094e47e9SGeorge Wilson 	if (rand_vd == NULL) {
5862094e47e9SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
5863094e47e9SGeorge Wilson 		mutex_exit(&ztest_vdev_lock);
5864094e47e9SGeorge Wilson 		return;
5865094e47e9SGeorge Wilson 	}
5866094e47e9SGeorge Wilson 
5867094e47e9SGeorge Wilson 	/*
5868094e47e9SGeorge Wilson 	 * The random vdev we've selected may change as soon as we
5869094e47e9SGeorge Wilson 	 * drop the spa_config_lock. We create local copies of things
5870094e47e9SGeorge Wilson 	 * we're interested in.
5871094e47e9SGeorge Wilson 	 */
5872094e47e9SGeorge Wilson 	uint64_t guid = rand_vd->vdev_guid;
5873094e47e9SGeorge Wilson 	char *path = strdup(rand_vd->vdev_path);
5874094e47e9SGeorge Wilson 	boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5875094e47e9SGeorge Wilson 
5876094e47e9SGeorge Wilson 	zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5877094e47e9SGeorge Wilson 	spa_config_exit(spa, SCL_VDEV, FTAG);
5878094e47e9SGeorge Wilson 
5879094e47e9SGeorge Wilson 	uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5880094e47e9SGeorge Wilson 	error = spa_vdev_initialize(spa, guid, cmd);
5881094e47e9SGeorge Wilson 	switch (cmd) {
5882094e47e9SGeorge Wilson 	case POOL_INITIALIZE_CANCEL:
5883094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5884094e47e9SGeorge Wilson 			(void) printf("Cancel initialize %s", path);
5885094e47e9SGeorge Wilson 			if (!active)
5886094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5887094e47e9SGeorge Wilson 			(void) printf("\n");
5888094e47e9SGeorge Wilson 		}
5889094e47e9SGeorge Wilson 		break;
5890094e47e9SGeorge Wilson 	case POOL_INITIALIZE_DO:
5891094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5892094e47e9SGeorge Wilson 			(void) printf("Start initialize %s", path);
5893094e47e9SGeorge Wilson 			if (active && error == 0)
5894094e47e9SGeorge Wilson 				(void) printf(" failed (already active)");
5895094e47e9SGeorge Wilson 			else if (error != 0)
5896094e47e9SGeorge Wilson 				(void) printf(" failed (error %d)", error);
5897094e47e9SGeorge Wilson 			(void) printf("\n");
5898094e47e9SGeorge Wilson 		}
5899094e47e9SGeorge Wilson 		break;
5900094e47e9SGeorge Wilson 	case POOL_INITIALIZE_SUSPEND:
5901094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5902094e47e9SGeorge Wilson 			(void) printf("Suspend initialize %s", path);
5903094e47e9SGeorge Wilson 			if (!active)
5904094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5905094e47e9SGeorge Wilson 			(void) printf("\n");
5906094e47e9SGeorge Wilson 		}
5907094e47e9SGeorge Wilson 		break;
5908094e47e9SGeorge Wilson 	}
5909094e47e9SGeorge Wilson 	free(path);
5910094e47e9SGeorge Wilson 	mutex_exit(&ztest_vdev_lock);
5911094e47e9SGeorge Wilson }
5912094e47e9SGeorge Wilson 
5913b24ab676SJeff Bonwick /*
5914b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
5915b24ab676SJeff Bonwick  */
5916fa9e4066Sahrens static void
5917b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
5918fa9e4066Sahrens {
5919fa9e4066Sahrens 	int status;
5920fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5921fa9e4066Sahrens 	char zbuf[1024];
5922fa9e4066Sahrens 	char *bin;
59238654d025Sperrin 	char *ztest;
59248654d025Sperrin 	char *isa;
59258654d025Sperrin 	int isalen;
5926fa9e4066Sahrens 	FILE *fp;
5927fa9e4066Sahrens 
5928fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
5929fa9e4066Sahrens 
5930fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5931fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
59328654d025Sperrin 	ztest = strstr(bin, "/ztest");
59338654d025Sperrin 	isa = bin + 8;
59348654d025Sperrin 	isalen = ztest - isa;
59358654d025Sperrin 	isa = strdup(isa);
5936fa9e4066Sahrens 	/* LINTED */
59373a57275aSck 	(void) sprintf(bin,
5938a21fe349SBrian Behlendorf 	    "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s "
5939a21fe349SBrian Behlendorf 	    "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
59408654d025Sperrin 	    isalen,
59418654d025Sperrin 	    isa,
5942420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
5943420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
5944f0ba89beSJeff Bonwick 	    spa_config_path,
594588b7b0f2SMatthew Ahrens 	    pool);
59468654d025Sperrin 	free(isa);
5947fa9e4066Sahrens 
5948420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
5949fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
5950fa9e4066Sahrens 
5951fa9e4066Sahrens 	fp = popen(zdb, "r");
5952fa9e4066Sahrens 
5953fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5954420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
5955fa9e4066Sahrens 			(void) printf("%s", zbuf);
5956fa9e4066Sahrens 
5957fa9e4066Sahrens 	status = pclose(fp);
5958fa9e4066Sahrens 
5959fa9e4066Sahrens 	if (status == 0)
5960fa9e4066Sahrens 		return;
5961fa9e4066Sahrens 
5962fa9e4066Sahrens 	ztest_dump_core = 0;
5963fa9e4066Sahrens 	if (WIFEXITED(status))
5964fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5965fa9e4066Sahrens 	else
5966fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5967fa9e4066Sahrens }
5968fa9e4066Sahrens 
5969fa9e4066Sahrens static void
5970fa9e4066Sahrens ztest_walk_pool_directory(char *header)
5971fa9e4066Sahrens {
5972fa9e4066Sahrens 	spa_t *spa = NULL;
5973fa9e4066Sahrens 
5974420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5975fa9e4066Sahrens 		(void) printf("%s\n", header);
5976fa9e4066Sahrens 
5977fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
5978fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
5979420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
5980fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
5981fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
5982fa9e4066Sahrens }
5983fa9e4066Sahrens 
5984fa9e4066Sahrens static void
5985fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
5986fa9e4066Sahrens {
59878ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
5988fa9e4066Sahrens 	uint64_t pool_guid;
5989fa9e4066Sahrens 	spa_t *spa;
59903b2aab18SMatthew Ahrens 	int error;
5991fa9e4066Sahrens 
5992420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5993fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
5994fa9e4066Sahrens 		    oldname, newname);
5995fa9e4066Sahrens 	}
5996fa9e4066Sahrens 
5997fa9e4066Sahrens 	/*
5998fa9e4066Sahrens 	 * Clean up from previous runs.
5999fa9e4066Sahrens 	 */
6000fa9e4066Sahrens 	(void) spa_destroy(newname);
6001fa9e4066Sahrens 
6002fa9e4066Sahrens 	/*
6003fa9e4066Sahrens 	 * Get the pool's configuration and guid.
6004fa9e4066Sahrens 	 */
6005b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6006fa9e4066Sahrens 
60078ad4d6ddSJeff Bonwick 	/*
60088ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
60098ad4d6ddSJeff Bonwick 	 */
60108ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
60113f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
60128ad4d6ddSJeff Bonwick 
6013fa9e4066Sahrens 	pool_guid = spa_guid(spa);
6014fa9e4066Sahrens 	spa_close(spa, FTAG);
6015fa9e4066Sahrens 
6016fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
6017fa9e4066Sahrens 
6018fa9e4066Sahrens 	/*
6019fa9e4066Sahrens 	 * Export it.
6020fa9e4066Sahrens 	 */
6021b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6022fa9e4066Sahrens 
6023fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
6024fa9e4066Sahrens 
60258ad4d6ddSJeff Bonwick 	/*
60268ad4d6ddSJeff Bonwick 	 * Try to import it.
60278ad4d6ddSJeff Bonwick 	 */
60288ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
60298ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
60308ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
60318ad4d6ddSJeff Bonwick 
6032fa9e4066Sahrens 	/*
6033fa9e4066Sahrens 	 * Import it under the new name.
6034fa9e4066Sahrens 	 */
60353b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
60363b2aab18SMatthew Ahrens 	if (error != 0) {
60373b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
60383b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
60393b2aab18SMatthew Ahrens 		    oldname, newname, error);
60403b2aab18SMatthew Ahrens 	}
6041fa9e4066Sahrens 
6042fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
6043fa9e4066Sahrens 
6044fa9e4066Sahrens 	/*
6045fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
6046fa9e4066Sahrens 	 */
60474b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6048fa9e4066Sahrens 
6049fa9e4066Sahrens 	/*
6050fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
6051fa9e4066Sahrens 	 */
60524b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6053fa9e4066Sahrens 
6054fa9e4066Sahrens 	/*
6055fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
6056fa9e4066Sahrens 	 */
6057b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6058fa9e4066Sahrens 
6059fa9e4066Sahrens 	/*
6060fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
6061fa9e4066Sahrens 	 */
6062b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6063fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
6064fa9e4066Sahrens 	spa_close(spa, FTAG);
6065fa9e4066Sahrens 
6066fa9e4066Sahrens 	nvlist_free(config);
6067fa9e4066Sahrens }
6068fa9e4066Sahrens 
60698ad4d6ddSJeff Bonwick static void
60708ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
60718ad4d6ddSJeff Bonwick {
6072420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6073b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
6074b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
6075b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
6076b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
6077b24ab676SJeff Bonwick 	(void) zio_resume(spa);
60788ad4d6ddSJeff Bonwick }
60798ad4d6ddSJeff Bonwick 
60800a4e9518Sgw static void *
60818ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
60820a4e9518Sgw {
6083e14bb325SJeff Bonwick 	spa_t *spa = arg;
60840a4e9518Sgw 
60850a4e9518Sgw 	while (!ztest_exiting) {
6086b24ab676SJeff Bonwick 		if (spa_suspended(spa))
6087b24ab676SJeff Bonwick 			ztest_resume(spa);
6088b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
6089dcbf3bd6SGeorge Wilson 
6090dcbf3bd6SGeorge Wilson 		/*
6091dcbf3bd6SGeorge Wilson 		 * Periodically change the zfs_compressed_arc_enabled setting.
6092dcbf3bd6SGeorge Wilson 		 */
6093dcbf3bd6SGeorge Wilson 		if (ztest_random(10) == 0)
6094dcbf3bd6SGeorge Wilson 			zfs_compressed_arc_enabled = ztest_random(2);
6095770499e1SDan Kimmel 
6096770499e1SDan Kimmel 		/*
6097770499e1SDan Kimmel 		 * Periodically change the zfs_abd_scatter_enabled setting.
6098770499e1SDan Kimmel 		 */
6099770499e1SDan Kimmel 		if (ztest_random(10) == 0)
6100770499e1SDan Kimmel 			zfs_abd_scatter_enabled = ztest_random(2);
61010a4e9518Sgw 	}
61020a4e9518Sgw 	return (NULL);
61030a4e9518Sgw }
61040a4e9518Sgw 
6105b24ab676SJeff Bonwick static void *
6106b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
6107b24ab676SJeff Bonwick {
6108b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
61092c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
61102c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
6111b24ab676SJeff Bonwick 
61122c1e2b44SGeorge Wilson 	for (;;) {
611369962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
611469962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
6115b24ab676SJeff Bonwick 
611669962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6117b24ab676SJeff Bonwick 
61182c1e2b44SGeorge Wilson 		/*
61192c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
61202c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
61212c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
61222c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
61232c1e2b44SGeorge Wilson 		 */
61240713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
61252c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
61262c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
612769962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
61282c1e2b44SGeorge Wilson 			return (NULL);
61292c1e2b44SGeorge Wilson 		}
61302c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
6131b24ab676SJeff Bonwick 
613269962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
61332c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
61342c1e2b44SGeorge Wilson 		    total);
61352c1e2b44SGeorge Wilson 	}
6136b24ab676SJeff Bonwick }
6137b24ab676SJeff Bonwick 
6138b24ab676SJeff Bonwick static void
6139420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6140b24ab676SJeff Bonwick {
6141420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6142420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6143b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
6144b24ab676SJeff Bonwick 
6145b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
6146b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
6147b24ab676SJeff Bonwick 
6148b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
6149b24ab676SJeff Bonwick 
6150420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
6151420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
6152b24ab676SJeff Bonwick 
6153420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
6154b24ab676SJeff Bonwick 		Dl_info dli;
6155b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
6156b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
6157b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
6158b24ab676SJeff Bonwick 	}
6159b24ab676SJeff Bonwick }
6160b24ab676SJeff Bonwick 
6161fa9e4066Sahrens static void *
6162fa9e4066Sahrens ztest_thread(void *arg)
6163fa9e4066Sahrens {
6164420dfc95SChris Siden 	int rand;
6165b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
6166fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
6167b24ab676SJeff Bonwick 	uint64_t call_next;
6168b24ab676SJeff Bonwick 	hrtime_t now;
6169fa9e4066Sahrens 	ztest_info_t *zi;
6170420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6171fa9e4066Sahrens 
6172b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
6173fa9e4066Sahrens 		/*
6174fa9e4066Sahrens 		 * See if it's time to force a crash.
6175fa9e4066Sahrens 		 */
6176b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
6177b24ab676SJeff Bonwick 			ztest_kill(zs);
6178fa9e4066Sahrens 
6179fa9e4066Sahrens 		/*
6180b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
6181fa9e4066Sahrens 		 */
6182b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
6183b24ab676SJeff Bonwick 			break;
6184fa9e4066Sahrens 
6185fa9e4066Sahrens 		/*
6186b24ab676SJeff Bonwick 		 * Pick a random function to execute.
6187fa9e4066Sahrens 		 */
6188420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
6189420dfc95SChris Siden 		zi = &ztest_info[rand];
6190420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6191420dfc95SChris Siden 		call_next = zc->zc_next;
6192b24ab676SJeff Bonwick 
6193b24ab676SJeff Bonwick 		if (now >= call_next &&
6194420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
6195420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6196420dfc95SChris Siden 			ztest_execute(rand, zi, id);
6197420dfc95SChris Siden 		}
6198b24ab676SJeff Bonwick 	}
6199fa9e4066Sahrens 
6200b24ab676SJeff Bonwick 	return (NULL);
6201b24ab676SJeff Bonwick }
6202fa9e4066Sahrens 
6203b24ab676SJeff Bonwick static void
6204b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
6205b24ab676SJeff Bonwick {
62069adfa60dSMatthew Ahrens 	(void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6207b24ab676SJeff Bonwick }
6208fa9e4066Sahrens 
6209b24ab676SJeff Bonwick static void
6210420dfc95SChris Siden ztest_dataset_destroy(int d)
6211b24ab676SJeff Bonwick {
62129adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6213fa9e4066Sahrens 
6214420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6215fa9e4066Sahrens 
6216420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6217b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
6218fa9e4066Sahrens 
6219b24ab676SJeff Bonwick 	/*
6220b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
6221b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
6222b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
6223b24ab676SJeff Bonwick 	 */
6224420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
6225420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
6226b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
6227420dfc95SChris Siden 	}
6228fa9e4066Sahrens 
6229b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6230b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6231b24ab676SJeff Bonwick }
6232b24ab676SJeff Bonwick 
6233b24ab676SJeff Bonwick static void
6234b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6235b24ab676SJeff Bonwick {
6236b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
6237b24ab676SJeff Bonwick 
6238b24ab676SJeff Bonwick 	/*
6239b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
6240b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
6241b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6242b24ab676SJeff Bonwick 	 * If not, we have an object leak.
6243b24ab676SJeff Bonwick 	 *
6244b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
6245b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
6246b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
6247b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
6248b24ab676SJeff Bonwick 	 */
6249b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6250b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6251b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
6252b24ab676SJeff Bonwick }
6253b24ab676SJeff Bonwick 
6254b24ab676SJeff Bonwick static int
6255420dfc95SChris Siden ztest_dataset_open(int d)
6256b24ab676SJeff Bonwick {
6257420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6258420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6259b24ab676SJeff Bonwick 	objset_t *os;
6260b24ab676SJeff Bonwick 	zilog_t *zilog;
62619adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
6262b24ab676SJeff Bonwick 	int error;
6263b24ab676SJeff Bonwick 
6264420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
6265b24ab676SJeff Bonwick 
6266f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
6267b24ab676SJeff Bonwick 
626855da60b9SMark J Musante 	error = ztest_dataset_create(name);
6269b24ab676SJeff Bonwick 	if (error == ENOSPC) {
6270f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
6271b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
6272b24ab676SJeff Bonwick 		return (error);
6273fa9e4066Sahrens 	}
6274b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
6275fa9e4066Sahrens 
62763b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6277f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
6278b24ab676SJeff Bonwick 
6279420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6280b24ab676SJeff Bonwick 
6281b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
6282b24ab676SJeff Bonwick 
6283b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6284b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
6285b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
6286b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
6287b24ab676SJeff Bonwick 
6288b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6289b24ab676SJeff Bonwick 
6290b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
6291b24ab676SJeff Bonwick 
6292b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
6293b24ab676SJeff Bonwick 
6294420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
6295b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6296b24ab676SJeff Bonwick 		    zd->zd_name,
6297b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
6298b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
6299b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
6300b24ab676SJeff Bonwick 
6301b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
6302b24ab676SJeff Bonwick 
6303b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
6304b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
6305b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
6306b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
6307b24ab676SJeff Bonwick 
6308b24ab676SJeff Bonwick 	return (0);
6309b24ab676SJeff Bonwick }
6310b24ab676SJeff Bonwick 
6311b24ab676SJeff Bonwick static void
6312420dfc95SChris Siden ztest_dataset_close(int d)
6313b24ab676SJeff Bonwick {
6314420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
6315b24ab676SJeff Bonwick 
6316b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
63173b2aab18SMatthew Ahrens 	dmu_objset_disown(zd->zd_os, zd);
6318b24ab676SJeff Bonwick 
6319b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
6320fa9e4066Sahrens }
6321fa9e4066Sahrens 
6322fa9e4066Sahrens /*
6323fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
6324fa9e4066Sahrens  */
6325fa9e4066Sahrens static void
6326b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
6327fa9e4066Sahrens {
6328b24ab676SJeff Bonwick 	thread_t *tid;
6329fa9e4066Sahrens 	spa_t *spa;
6330e9103aaeSGarrett D'Amore 	objset_t *os;
6331e14bb325SJeff Bonwick 	thread_t resume_tid;
6332b24ab676SJeff Bonwick 	int error;
6333e14bb325SJeff Bonwick 
6334e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
6335fa9e4066Sahrens 
6336fa9e4066Sahrens 	/*
6337b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
6338fa9e4066Sahrens 	 */
633986714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6340f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6341f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6342fa9e4066Sahrens 
6343b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
6344420dfc95SChris Siden 	zs->zs_thread_stop =
6345420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6346b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6347b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
6348420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
6349420dfc95SChris Siden 		zs->zs_thread_kill -=
6350420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
6351420dfc95SChris Siden 	}
6352fa9e4066Sahrens 
6353f06dce2cSAndrew Stormont 	mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6354fa9e4066Sahrens 
6355b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6356b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
6357fa9e4066Sahrens 
63580a4e9518Sgw 	/*
6359e14bb325SJeff Bonwick 	 * Open our pool.
63600a4e9518Sgw 	 */
6361b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
63623b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
636330beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
6364420dfc95SChris Siden 	ztest_spa = spa;
6365b24ab676SJeff Bonwick 
6366aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
63673b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
63683b2aab18SMatthew Ahrens 	    DMU_OST_ANY, B_TRUE, FTAG, &os));
6369aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6370aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
6371aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6372aab80726SGeorge Wilson 	zs->zs_guid = dds.dds_guid;
63733b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
6374e9103aaeSGarrett D'Amore 
6375b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
63760a4e9518Sgw 
63778ad4d6ddSJeff Bonwick 	/*
63788ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
63798ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
63808ad4d6ddSJeff Bonwick 	 * the only valid replica.
63818ad4d6ddSJeff Bonwick 	 */
63821195e687SMark J Musante 	if (MAXFAULTS() == 0)
63838ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
63848ad4d6ddSJeff Bonwick 	else
63858ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
63868ad4d6ddSJeff Bonwick 
6387fa9e4066Sahrens 	/*
6388e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
6389fa9e4066Sahrens 	 */
63908ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6391e14bb325SJeff Bonwick 	    &resume_tid) == 0);
6392fa9e4066Sahrens 
6393b24ab676SJeff Bonwick 	/*
6394b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
6395b24ab676SJeff Bonwick 	 */
6396b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6397b24ab676SJeff Bonwick 	    NULL) == 0);
6398b24ab676SJeff Bonwick 
6399fa9e4066Sahrens 	/*
640086714001SSerapheim Dimitropoulos 	 * Verify that we can safely inquire about any object,
6401fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
6402fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
6403fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
6404fa9e4066Sahrens 	 */
6405b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
6406b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
6407fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
6408fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
6409ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
6410ea8dc4b6Seschrock 			    error == EINVAL);
6411fa9e4066Sahrens 		}
6412fa9e4066Sahrens 	}
6413fa9e4066Sahrens 
6414fa9e4066Sahrens 	/*
6415b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
6416fa9e4066Sahrens 	 */
6417b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
6418420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
6419420dfc95SChris Siden 		ztest_dataset_destroy(d);
6420b24ab676SJeff Bonwick 	}
6421fa9e4066Sahrens 	zs->zs_enospc_count = 0;
6422fa9e4066Sahrens 
6423420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6424420dfc95SChris Siden 	    UMEM_NOFAIL);
6425fa9e4066Sahrens 
6426420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
6427fa9e4066Sahrens 		(void) printf("starting main threads...\n");
6428fa9e4066Sahrens 
6429b24ab676SJeff Bonwick 	/*
6430b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
6431b24ab676SJeff Bonwick 	 */
6432420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
6433420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
6434420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
6435b24ab676SJeff Bonwick 			return;
6436b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6437b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
6438fa9e4066Sahrens 	}
6439fa9e4066Sahrens 
6440b24ab676SJeff Bonwick 	/*
6441b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
6442b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
6443b24ab676SJeff Bonwick 	 */
6444420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6445b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6446420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
6447420dfc95SChris Siden 			ztest_dataset_close(t);
6448fa9e4066Sahrens 	}
6449fa9e4066Sahrens 
6450fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
6451fa9e4066Sahrens 
6452b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6453b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6454b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
6455b24ab676SJeff Bonwick 
6456420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6457b24ab676SJeff Bonwick 
6458b24ab676SJeff Bonwick 	/* Kill the resume thread */
6459b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
6460b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6461b24ab676SJeff Bonwick 	ztest_resume(spa);
6462b24ab676SJeff Bonwick 
6463b24ab676SJeff Bonwick 	/*
6464b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
6465b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
6466b24ab676SJeff Bonwick 	 */
6467a2cdcdd2SPaul Dagnelie 	for (uint64_t object = 1; object < 50; object++) {
6468a2cdcdd2SPaul Dagnelie 		dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6469a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
6470a2cdcdd2SPaul Dagnelie 	}
6471b24ab676SJeff Bonwick 
6472b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6473fa9e4066Sahrens 
6474fa9e4066Sahrens 	/*
6475b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
6476fa9e4066Sahrens 	 */
6477b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
6478b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6479420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
6480b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
6481b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
6482b24ab676SJeff Bonwick 
6483b24ab676SJeff Bonwick 	/*
6484b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
6485b24ab676SJeff Bonwick 	 * different name.
6486b24ab676SJeff Bonwick 	 */
6487e0f1c0afSOlaf Faaland 	if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
64889adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
64899adfa60dSMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s_import",
6490420dfc95SChris Siden 		    ztest_opts.zo_pool);
6491420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
6492420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
6493b24ab676SJeff Bonwick 	}
6494b24ab676SJeff Bonwick 
6495b24ab676SJeff Bonwick 	kernel_fini();
649626cb54a9SGeorge Wilson 
649726cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
649826cb54a9SGeorge Wilson 
6499f06dce2cSAndrew Stormont 	mutex_destroy(&zcl.zcl_callbacks_lock);
650026cb54a9SGeorge Wilson 
6501f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6502f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
650386714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6504b24ab676SJeff Bonwick }
6505b24ab676SJeff Bonwick 
6506b24ab676SJeff Bonwick static void
6507420dfc95SChris Siden ztest_freeze(void)
6508b24ab676SJeff Bonwick {
6509420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
6510b24ab676SJeff Bonwick 	spa_t *spa;
65112215e990SMark J Musante 	int numloops = 0;
6512b24ab676SJeff Bonwick 
6513420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6514b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
6515718d718aSGeorge Wilson 
6516b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6517b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6518b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
651980901aeaSGeorge Wilson 	ztest_spa = spa;
6520718d718aSGeorge Wilson 
6521b24ab676SJeff Bonwick 	/*
6522b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
6523b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
6524b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
6525b24ab676SJeff Bonwick 	 */
6526b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6527b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
65285002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
6529fa9e4066Sahrens 	}
6530fa9e4066Sahrens 
6531ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
6532fa9e4066Sahrens 
6533b24ab676SJeff Bonwick 	/*
6534b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
6535b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
6536b24ab676SJeff Bonwick 	 */
6537b24ab676SJeff Bonwick 	spa_freeze(spa);
6538e14bb325SJeff Bonwick 
65392a104a52SAlex Reece 	/*
65402a104a52SAlex Reece 	 * Because it is hard to predict how much space a write will actually
65412a104a52SAlex Reece 	 * require beforehand, we leave ourselves some fudge space to write over
65422a104a52SAlex Reece 	 * capacity.
65432a104a52SAlex Reece 	 */
65442a104a52SAlex Reece 	uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
65452a104a52SAlex Reece 
6546b24ab676SJeff Bonwick 	/*
6547b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
6548b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6549b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
6550b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
6551b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
65522a104a52SAlex Reece 	 *
65532a104a52SAlex Reece 	 * Run a random number of times less than zo_maxloops and ensure we do
65542a104a52SAlex Reece 	 * not run out of space on the pool.
6555b24ab676SJeff Bonwick 	 */
6556420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
65572a104a52SAlex Reece 	    numloops++ < ztest_opts.zo_maxloops &&
65582a104a52SAlex Reece 	    metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
65592a104a52SAlex Reece 		ztest_od_t od;
656054811da5SToomas Soome 		ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
65612a104a52SAlex Reece 		VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
65622a104a52SAlex Reece 		ztest_io(zd, od.od_object,
65632a104a52SAlex Reece 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6564b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
6565b24ab676SJeff Bonwick 	}
6566e14bb325SJeff Bonwick 
6567fa9e4066Sahrens 	/*
6568b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
6569fa9e4066Sahrens 	 */
65705002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
6571b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
6572fa9e4066Sahrens 
6573b24ab676SJeff Bonwick 	/*
6574b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
6575b24ab676SJeff Bonwick 	 */
6576420dfc95SChris Siden 	ztest_dataset_close(0);
6577e05725b1Sbonwick 	spa_close(spa, FTAG);
6578b24ab676SJeff Bonwick 	kernel_fini();
6579e05725b1Sbonwick 
6580b24ab676SJeff Bonwick 	/*
6581b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
6582b24ab676SJeff Bonwick 	 */
6583b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6584b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6585ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6586b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
6587420dfc95SChris Siden 	ztest_dataset_close(0);
6588dfbb9432SGeorge Wilson 
6589dfbb9432SGeorge Wilson 	ztest_spa = spa;
6590dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
6591dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
6592dfbb9432SGeorge Wilson 
6593b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6594fa9e4066Sahrens 	kernel_fini();
6595fa9e4066Sahrens }
6596fa9e4066Sahrens 
6597fa9e4066Sahrens void
6598fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
6599fa9e4066Sahrens {
6600fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
6601fa9e4066Sahrens 	hrtime_t m = s / 60;
6602fa9e4066Sahrens 	hrtime_t h = m / 60;
6603fa9e4066Sahrens 	hrtime_t d = h / 24;
6604fa9e4066Sahrens 
6605fa9e4066Sahrens 	s -= m * 60;
6606fa9e4066Sahrens 	m -= h * 60;
6607fa9e4066Sahrens 	h -= d * 24;
6608fa9e4066Sahrens 
6609fa9e4066Sahrens 	timebuf[0] = '\0';
6610fa9e4066Sahrens 
6611fa9e4066Sahrens 	if (d)
6612fa9e4066Sahrens 		(void) sprintf(timebuf,
6613fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
6614fa9e4066Sahrens 	else if (h)
6615fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6616fa9e4066Sahrens 	else if (m)
6617fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
6618fa9e4066Sahrens 	else
6619fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
6620fa9e4066Sahrens }
6621fa9e4066Sahrens 
66221195e687SMark J Musante static nvlist_t *
66231195e687SMark J Musante make_random_props()
66241195e687SMark J Musante {
66251195e687SMark J Musante 	nvlist_t *props;
66261195e687SMark J Musante 
66271195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6628663207adSDon Brady 
6629ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
6630ad135b5dSChristopher Siden 		return (props);
66311195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
66321195e687SMark J Musante 
66331195e687SMark J Musante 	return (props);
66341195e687SMark J Musante }
66351195e687SMark J Musante 
6636e0f1c0afSOlaf Faaland /*
6637e0f1c0afSOlaf Faaland  * Import a storage pool with the given name.
6638e0f1c0afSOlaf Faaland  */
6639e0f1c0afSOlaf Faaland static void
6640e0f1c0afSOlaf Faaland ztest_import(ztest_shared_t *zs)
6641e0f1c0afSOlaf Faaland {
6642e0f1c0afSOlaf Faaland 	libzfs_handle_t *hdl;
6643e0f1c0afSOlaf Faaland 	importargs_t args = { 0 };
6644e0f1c0afSOlaf Faaland 	spa_t *spa;
6645e0f1c0afSOlaf Faaland 	nvlist_t *cfg = NULL;
6646e0f1c0afSOlaf Faaland 	int nsearch = 1;
6647e0f1c0afSOlaf Faaland 	char *searchdirs[nsearch];
6648e0f1c0afSOlaf Faaland 	char *name = ztest_opts.zo_pool;
6649e0f1c0afSOlaf Faaland 	int flags = ZFS_IMPORT_MISSING_LOG;
6650e0f1c0afSOlaf Faaland 	int error;
6651e0f1c0afSOlaf Faaland 
6652e0f1c0afSOlaf Faaland 	mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6653e0f1c0afSOlaf Faaland 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6654e0f1c0afSOlaf Faaland 
6655e0f1c0afSOlaf Faaland 	kernel_init(FREAD | FWRITE);
6656e0f1c0afSOlaf Faaland 	hdl = libzfs_init();
6657e0f1c0afSOlaf Faaland 
6658e0f1c0afSOlaf Faaland 	searchdirs[0] = ztest_opts.zo_dir;
6659e0f1c0afSOlaf Faaland 	args.paths = nsearch;
6660e0f1c0afSOlaf Faaland 	args.path = searchdirs;
6661e0f1c0afSOlaf Faaland 	args.can_be_active = B_FALSE;
6662e0f1c0afSOlaf Faaland 
6663e0f1c0afSOlaf Faaland 	error = zpool_tryimport(hdl, name, &cfg, &args);
6664e0f1c0afSOlaf Faaland 	if (error)
6665e0f1c0afSOlaf Faaland 		(void) fatal(0, "No pools found\n");
6666e0f1c0afSOlaf Faaland 
6667e0f1c0afSOlaf Faaland 	VERIFY0(spa_import(name, cfg, NULL, flags));
6668e0f1c0afSOlaf Faaland 	VERIFY0(spa_open(name, &spa, FTAG));
6669e0f1c0afSOlaf Faaland 	zs->zs_metaslab_sz =
6670e0f1c0afSOlaf Faaland 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6671e0f1c0afSOlaf Faaland 	spa_close(spa, FTAG);
6672e0f1c0afSOlaf Faaland 
6673e0f1c0afSOlaf Faaland 	libzfs_fini(hdl);
6674e0f1c0afSOlaf Faaland 	kernel_fini();
6675e0f1c0afSOlaf Faaland 
6676e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6677e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6678e0f1c0afSOlaf Faaland 		ztest_freeze();
6679e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6680e0f1c0afSOlaf Faaland 	}
6681e0f1c0afSOlaf Faaland 
6682e0f1c0afSOlaf Faaland 	rw_destroy(&ztest_name_lock);
6683e0f1c0afSOlaf Faaland 	mutex_destroy(&ztest_vdev_lock);
6684e0f1c0afSOlaf Faaland }
6685e0f1c0afSOlaf Faaland 
6686fa9e4066Sahrens /*
6687fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
6688b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
6689fa9e4066Sahrens  */
6690fa9e4066Sahrens static void
6691b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
6692fa9e4066Sahrens {
6693fa9e4066Sahrens 	spa_t *spa;
66941195e687SMark J Musante 	nvlist_t *nvroot, *props;
6695fa9e4066Sahrens 
6696f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
669786714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6698f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6699b24ab676SJeff Bonwick 
6700fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
6701fa9e4066Sahrens 
6702fa9e4066Sahrens 	/*
6703fa9e4066Sahrens 	 * Create the storage pool.
6704fa9e4066Sahrens 	 */
6705420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
670688ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
67071195e687SMark J Musante 	zs->zs_splits = 0;
6708420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
670925345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6710663207adSDon Brady 	    NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
67111195e687SMark J Musante 	props = make_random_props();
6712ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
6713ad135b5dSChristopher Siden 		char buf[1024];
6714ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
6715ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
6716b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6717ad135b5dSChristopher Siden 	}
6718b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6719fa9e4066Sahrens 	nvlist_free(nvroot);
67201df447ebSRichard Yao 	nvlist_free(props);
6721fa9e4066Sahrens 
6722b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6723420dfc95SChris Siden 	zs->zs_metaslab_sz =
6724420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6725ad135b5dSChristopher Siden 
6726fa9e4066Sahrens 	spa_close(spa, FTAG);
6727fa9e4066Sahrens 
6728fa9e4066Sahrens 	kernel_fini();
6729b24ab676SJeff Bonwick 
6730e0f1c0afSOlaf Faaland 	if (!ztest_opts.zo_mmp_test) {
6731e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6732e0f1c0afSOlaf Faaland 		ztest_freeze();
6733e0f1c0afSOlaf Faaland 		ztest_run_zdb(ztest_opts.zo_pool);
6734e0f1c0afSOlaf Faaland 	}
6735420dfc95SChris Siden 
6736f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6737f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
673886714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6739420dfc95SChris Siden }
6740420dfc95SChris Siden 
6741420dfc95SChris Siden static void
6742741652b0SEtienne Dechamps setup_data_fd(void)
6743420dfc95SChris Siden {
6744741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6745420dfc95SChris Siden 
6746741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
6747741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
6748741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
6749420dfc95SChris Siden }
6750420dfc95SChris Siden 
6751741652b0SEtienne Dechamps 
6752ad135b5dSChristopher Siden static int
6753ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
6754ad135b5dSChristopher Siden {
6755ad135b5dSChristopher Siden 	int size;
6756ad135b5dSChristopher Siden 
6757ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
6758ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
6759ad135b5dSChristopher Siden 	size += hdr->zh_size;
6760ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
6761ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
6762ad135b5dSChristopher Siden 
6763ad135b5dSChristopher Siden 	return (size);
6764ad135b5dSChristopher Siden }
6765ad135b5dSChristopher Siden 
6766420dfc95SChris Siden static void
6767420dfc95SChris Siden setup_hdr(void)
6768420dfc95SChris Siden {
6769ad135b5dSChristopher Siden 	int size;
6770420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6771420dfc95SChris Siden 
6772420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6773741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6774420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6775420dfc95SChris Siden 
6776741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6777ad135b5dSChristopher Siden 
6778420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6779420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6780420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
6781420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6782420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
6783420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6784420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
6785420dfc95SChris Siden 
6786ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6787741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6788ad135b5dSChristopher Siden 
6789420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6790420dfc95SChris Siden }
6791420dfc95SChris Siden 
6792420dfc95SChris Siden static void
6793420dfc95SChris Siden setup_data(void)
6794420dfc95SChris Siden {
6795420dfc95SChris Siden 	int size, offset;
6796420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6797420dfc95SChris Siden 	uint8_t *buf;
6798420dfc95SChris Siden 
6799420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6800741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6801420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6802420dfc95SChris Siden 
6803ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6804420dfc95SChris Siden 
6805420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6806420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6807741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6808420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6809420dfc95SChris Siden 	buf = (uint8_t *)hdr;
6810420dfc95SChris Siden 
6811420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
6812420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
6813420dfc95SChris Siden 	offset += hdr->zh_opts_size;
6814420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
6815420dfc95SChris Siden 	offset += hdr->zh_size;
6816420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
6817420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
6818420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
6819420dfc95SChris Siden }
6820420dfc95SChris Siden 
6821420dfc95SChris Siden static boolean_t
6822420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6823420dfc95SChris Siden {
6824420dfc95SChris Siden 	pid_t pid;
6825420dfc95SChris Siden 	int status;
6826741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
6827420dfc95SChris Siden 
6828420dfc95SChris Siden 	pid = fork();
6829420dfc95SChris Siden 
6830420dfc95SChris Siden 	if (cmd == NULL) {
6831741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6832741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6833420dfc95SChris Siden 		cmd = cmdbuf;
6834420dfc95SChris Siden 	}
6835b24ab676SJeff Bonwick 
6836420dfc95SChris Siden 	if (pid == -1)
6837420dfc95SChris Siden 		fatal(1, "fork failed");
6838b24ab676SJeff Bonwick 
6839420dfc95SChris Siden 	if (pid == 0) {	/* child */
6840420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
6841741652b0SEtienne Dechamps 		char fd_data_str[12];
684226cb54a9SGeorge Wilson 
6843420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
6844420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
6845741652b0SEtienne Dechamps 
6846741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
6847741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
6848741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6849741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6850741652b0SEtienne Dechamps 
6851420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
6852420dfc95SChris Siden 		if (libpath != NULL)
6853420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6854420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
6855420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
6856420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
6857420dfc95SChris Siden 	}
6858420dfc95SChris Siden 
6859741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
6860741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
6861741652b0SEtienne Dechamps 		cmd = NULL;
6862741652b0SEtienne Dechamps 	}
6863741652b0SEtienne Dechamps 
6864420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
6865420dfc95SChris Siden 		continue;
6866420dfc95SChris Siden 	if (statusp != NULL)
6867420dfc95SChris Siden 		*statusp = status;
6868420dfc95SChris Siden 
6869420dfc95SChris Siden 	if (WIFEXITED(status)) {
6870420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
6871420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
6872420dfc95SChris Siden 			    WEXITSTATUS(status));
6873420dfc95SChris Siden 			exit(2);
6874420dfc95SChris Siden 		}
6875420dfc95SChris Siden 		return (B_FALSE);
6876420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
6877420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6878420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
6879420dfc95SChris Siden 			    WTERMSIG(status));
6880420dfc95SChris Siden 			exit(3);
6881420dfc95SChris Siden 		}
6882420dfc95SChris Siden 		return (B_TRUE);
6883420dfc95SChris Siden 	} else {
6884420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
6885420dfc95SChris Siden 		exit(4);
6886420dfc95SChris Siden 		/* NOTREACHED */
6887420dfc95SChris Siden 	}
6888420dfc95SChris Siden }
6889420dfc95SChris Siden 
6890420dfc95SChris Siden static void
6891420dfc95SChris Siden ztest_run_init(void)
6892420dfc95SChris Siden {
6893420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
6894420dfc95SChris Siden 
6895420dfc95SChris Siden 	/*
6896420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
6897420dfc95SChris Siden 	 */
6898420dfc95SChris Siden 	(void) remove(spa_config_path);
6899420dfc95SChris Siden 
6900e0f1c0afSOlaf Faaland 	if (ztest_opts.zo_init == 0) {
6901e0f1c0afSOlaf Faaland 		if (ztest_opts.zo_verbose >= 1)
6902e0f1c0afSOlaf Faaland 			(void) printf("Importing pool %s\n",
6903e0f1c0afSOlaf Faaland 			    ztest_opts.zo_pool);
6904e0f1c0afSOlaf Faaland 		ztest_import(zs);
6905e0f1c0afSOlaf Faaland 		return;
6906e0f1c0afSOlaf Faaland 	}
6907e0f1c0afSOlaf Faaland 
6908420dfc95SChris Siden 	/*
6909420dfc95SChris Siden 	 * Create and initialize our storage pool.
6910420dfc95SChris Siden 	 */
6911420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
6912420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
6913420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
6914420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
6915420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
6916420dfc95SChris Siden 		}
6917420dfc95SChris Siden 		ztest_init(zs);
6918420dfc95SChris Siden 	}
6919fa9e4066Sahrens }
6920fa9e4066Sahrens 
6921fa9e4066Sahrens int
6922fa9e4066Sahrens main(int argc, char **argv)
6923fa9e4066Sahrens {
6924fa9e4066Sahrens 	int kills = 0;
6925fa9e4066Sahrens 	int iters = 0;
6926420dfc95SChris Siden 	int older = 0;
6927420dfc95SChris Siden 	int newer = 0;
6928fa9e4066Sahrens 	ztest_shared_t *zs;
6929fa9e4066Sahrens 	ztest_info_t *zi;
6930420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6931fa9e4066Sahrens 	char timebuf[100];
69320a055120SJason King 	char numbuf[NN_NUMBUF_SZ];
6933741652b0SEtienne Dechamps 	char *cmd;
6934420dfc95SChris Siden 	boolean_t hasalt;
6935741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
6936fa9e4066Sahrens 
6937fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
6938fa9e4066Sahrens 
6939cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
694069962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
694117f11284SSerapheim Dimitropoulos 	/*
694217f11284SSerapheim Dimitropoulos 	 * As two-word space map entries may not come up often (especially
694317f11284SSerapheim Dimitropoulos 	 * if pool and vdev sizes are small) we want to force at least some
694417f11284SSerapheim Dimitropoulos 	 * of them so the feature get tested.
694517f11284SSerapheim Dimitropoulos 	 */
694617f11284SSerapheim Dimitropoulos 	zfs_force_some_double_word_sm_entries = B_TRUE;
6947cd1c8b85SMatthew Ahrens 
6948a21fe349SBrian Behlendorf 	/*
6949a21fe349SBrian Behlendorf 	 * Verify that even extensively damaged split blocks with many
6950a21fe349SBrian Behlendorf 	 * segments can be reconstructed in a reasonable amount of time
6951a21fe349SBrian Behlendorf 	 * when reconstruction is known to be possible.
6952a21fe349SBrian Behlendorf 	 */
6953a21fe349SBrian Behlendorf 	zfs_reconstruct_indirect_damage_fraction = 4;
6954a21fe349SBrian Behlendorf 
6955741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6956741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
6957741652b0SEtienne Dechamps 
6958741652b0SEtienne Dechamps 	if (!fd_data_str) {
6959420dfc95SChris Siden 		process_options(argc, argv);
6960fa9e4066Sahrens 
6961741652b0SEtienne Dechamps 		setup_data_fd();
6962420dfc95SChris Siden 		setup_hdr();
6963420dfc95SChris Siden 		setup_data();
6964420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
6965420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
6966420dfc95SChris Siden 	} else {
6967741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
6968420dfc95SChris Siden 		setup_data();
6969420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6970420dfc95SChris Siden 	}
6971420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6972fa9e4066Sahrens 
6973f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
6974741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6975741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
6976f0ba89beSJeff Bonwick 
6977420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6978420dfc95SChris Siden 	    UMEM_NOFAIL);
6979420dfc95SChris Siden 	zs = ztest_shared;
6980ea8dc4b6Seschrock 
6981741652b0SEtienne Dechamps 	if (fd_data_str) {
6982243952c7SMatt Ahrens 		metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6983420dfc95SChris Siden 		metaslab_df_alloc_threshold =
6984420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
6985b24ab676SJeff Bonwick 
6986420dfc95SChris Siden 		if (zs->zs_do_init)
6987420dfc95SChris Siden 			ztest_run_init();
6988420dfc95SChris Siden 		else
6989420dfc95SChris Siden 			ztest_run(zs);
6990420dfc95SChris Siden 		exit(0);
6991420dfc95SChris Siden 	}
6992fa9e4066Sahrens 
6993420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6994420dfc95SChris Siden 
6995420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6996fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
6997fa9e4066Sahrens 		    " %llu seconds...\n",
6998420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
6999420dfc95SChris Siden 		    ztest_opts.zo_datasets,
7000420dfc95SChris Siden 		    ztest_opts.zo_threads,
7001420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
7002fa9e4066Sahrens 	}
7003fa9e4066Sahrens 
7004741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7005741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7006420dfc95SChris Siden 
7007420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
7008420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7009420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7010420dfc95SChris Siden 			(void) printf("Executing older ztest for "
7011420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
7012420dfc95SChris Siden 		}
7013420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7014420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7015420dfc95SChris Siden 	} else {
7016420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7017fa9e4066Sahrens 	}
7018420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
7019fa9e4066Sahrens 
7020b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
7021420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7022fa9e4066Sahrens 
7023b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
7024420dfc95SChris Siden 		zi = &ztest_info[f];
7025420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
7026b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7027420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
7028fa9e4066Sahrens 		else
7029420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
7030b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
7031fa9e4066Sahrens 	}
7032fa9e4066Sahrens 
7033fa9e4066Sahrens 	/*
7034fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
7035fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
7036fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
7037fa9e4066Sahrens 	 */
7038b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
7039fa9e4066Sahrens 		int status;
7040420dfc95SChris Siden 		boolean_t killed;
7041fa9e4066Sahrens 
7042fa9e4066Sahrens 		/*
7043fa9e4066Sahrens 		 * Initialize the workload counters for each function.
7044fa9e4066Sahrens 		 */
7045b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
7046420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
7047420dfc95SChris Siden 			zc->zc_count = 0;
7048420dfc95SChris Siden 			zc->zc_time = 0;
7049fa9e4066Sahrens 		}
7050fa9e4066Sahrens 
7051d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
7052420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
7053420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
7054fa9e4066Sahrens 
7055420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
7056420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7057420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
7058420dfc95SChris Siden 				    cmd);
7059fa9e4066Sahrens 			}
7060420dfc95SChris Siden 			newer++;
7061420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
70625ad82045Snd 		} else {
7063420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
7064420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
7065420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
7066420dfc95SChris Siden 			}
7067420dfc95SChris Siden 			older++;
7068420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
7069420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
7070fa9e4066Sahrens 		}
7071fa9e4066Sahrens 
7072420dfc95SChris Siden 		if (killed)
7073420dfc95SChris Siden 			kills++;
7074fa9e4066Sahrens 		iters++;
7075fa9e4066Sahrens 
7076420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
7077fa9e4066Sahrens 			hrtime_t now = gethrtime();
7078fa9e4066Sahrens 
7079b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
7080b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
70810a055120SJason King 			nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7082fa9e4066Sahrens 
7083fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7084fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7085fa9e4066Sahrens 			    iters,
7086fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
7087fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
7088fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
7089fa9e4066Sahrens 			    numbuf,
7090b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
7091420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
7092fa9e4066Sahrens 		}
7093fa9e4066Sahrens 
7094420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
7095fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
7096fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7097fa9e4066Sahrens 			    "Calls", "Time", "Function");
7098fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
7099fa9e4066Sahrens 			    "-----", "----", "--------");
7100b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
7101fa9e4066Sahrens 				Dl_info dli;
7102fa9e4066Sahrens 
7103420dfc95SChris Siden 				zi = &ztest_info[f];
7104420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
7105420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
7106fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
7107fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
7108420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
7109fa9e4066Sahrens 				    dli.dli_sname);
7110fa9e4066Sahrens 			}
7111fa9e4066Sahrens 			(void) printf("\n");
7112fa9e4066Sahrens 		}
7113fa9e4066Sahrens 
7114e0f1c0afSOlaf Faaland 		if (!ztest_opts.zo_mmp_test)
7115e0f1c0afSOlaf Faaland 			ztest_run_zdb(ztest_opts.zo_pool);
7116b24ab676SJeff Bonwick 	}
7117fa9e4066Sahrens 
7118420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
7119420dfc95SChris Siden 		if (hasalt) {
7120420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
7121420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
7122420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
7123420dfc95SChris Siden 			    cmd);
7124420dfc95SChris Siden 		}
7125fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7126fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7127fa9e4066Sahrens 	}
7128fa9e4066Sahrens 
7129741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
7130741652b0SEtienne Dechamps 
7131fa9e4066Sahrens 	return (0);
7132fa9e4066Sahrens }
7133