xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision 30beaff42d8240ebf5386e8b7a14e3d137a1631f)
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.
23*30beaff4SGeorge Wilson  * Copyright (c) 2011, 2014 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.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
30fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * The overall design of the ztest program is as follows:
33fa9e4066Sahrens  *
34fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
35fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
36fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
37fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
38fa9e4066Sahrens  *
39fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
40fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
41fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
44fa9e4066Sahrens  *     verify that self-healing data really works.
45fa9e4066Sahrens  *
46fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
47fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
48fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
51fa9e4066Sahrens  *     we run the entire test in a child of the main process.
52fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
53fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
54fa9e4066Sahrens  *     The parent then runs the test again, using the existing
555d7b4d43SMatthew Ahrens  *     storage pool, as many times as desired. If backwards compatibility
56420dfc95SChris Siden  *     testing is enabled ztest will sometimes run the "older" version
57420dfc95SChris Siden  *     of ztest after a SIGKILL.
58fa9e4066Sahrens  *
59fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
60fa9e4066Sahrens  *     many of the functional tests record the transaction group number
61fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
62fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
63fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
64fa9e4066Sahrens  *
65fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
66fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
67fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
68fa9e4066Sahrens  *
69fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
70fa9e4066Sahrens  *
71fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
72fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
73fa9e4066Sahrens  *
74420dfc95SChris Siden  * Use the -k option to set the desired frequency of kills.
75420dfc95SChris Siden  *
76420dfc95SChris Siden  * When ztest invokes itself it passes all relevant information through a
77420dfc95SChris Siden  * temporary file which is mmap-ed in the child process. This allows shared
78420dfc95SChris Siden  * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
79420dfc95SChris Siden  * stored at offset 0 of this file and contains information on the size and
80420dfc95SChris Siden  * number of shared structures in the file. The information stored in this file
81420dfc95SChris Siden  * must remain backwards compatible with older versions of ztest so that
82420dfc95SChris Siden  * ztest can invoke them during backwards compatibility testing (-B).
83fa9e4066Sahrens  */
84fa9e4066Sahrens 
85fa9e4066Sahrens #include <sys/zfs_context.h>
86fa9e4066Sahrens #include <sys/spa.h>
87fa9e4066Sahrens #include <sys/dmu.h>
88fa9e4066Sahrens #include <sys/txg.h>
892fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
90fa9e4066Sahrens #include <sys/zap.h>
91fa9e4066Sahrens #include <sys/dmu_objset.h>
92fa9e4066Sahrens #include <sys/poll.h>
93fa9e4066Sahrens #include <sys/stat.h>
94fa9e4066Sahrens #include <sys/time.h>
95fa9e4066Sahrens #include <sys/wait.h>
96fa9e4066Sahrens #include <sys/mman.h>
97fa9e4066Sahrens #include <sys/resource.h>
98fa9e4066Sahrens #include <sys/zio.h>
99fa9e4066Sahrens #include <sys/zil.h>
100b24ab676SJeff Bonwick #include <sys/zil_impl.h>
101fa9e4066Sahrens #include <sys/vdev_impl.h>
102e14bb325SJeff Bonwick #include <sys/vdev_file.h>
103fa9e4066Sahrens #include <sys/spa_impl.h>
10488ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
105fa9e4066Sahrens #include <sys/dsl_prop.h>
1064f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1073b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1083f9d6ad7SLin Ling #include <sys/dsl_scan.h>
109cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
110fa9e4066Sahrens #include <sys/refcount.h>
111ad135b5dSChristopher Siden #include <sys/zfeature.h>
1123b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
113fa9e4066Sahrens #include <stdio.h>
114004388ebScasper #include <stdio_ext.h>
115fa9e4066Sahrens #include <stdlib.h>
116fa9e4066Sahrens #include <unistd.h>
117fa9e4066Sahrens #include <signal.h>
118fa9e4066Sahrens #include <umem.h>
119fa9e4066Sahrens #include <dlfcn.h>
120fa9e4066Sahrens #include <ctype.h>
121fa9e4066Sahrens #include <math.h>
122fa9e4066Sahrens #include <sys/fs/zfs.h>
123b24ab676SJeff Bonwick #include <libnvpair.h>
124fa9e4066Sahrens 
125741652b0SEtienne Dechamps static int ztest_fd_data = -1;
126741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
127420dfc95SChris Siden 
128420dfc95SChris Siden typedef struct ztest_shared_hdr {
129420dfc95SChris Siden 	uint64_t	zh_hdr_size;
130420dfc95SChris Siden 	uint64_t	zh_opts_size;
131420dfc95SChris Siden 	uint64_t	zh_size;
132420dfc95SChris Siden 	uint64_t	zh_stats_size;
133420dfc95SChris Siden 	uint64_t	zh_stats_count;
134420dfc95SChris Siden 	uint64_t	zh_ds_size;
135420dfc95SChris Siden 	uint64_t	zh_ds_count;
136420dfc95SChris Siden } ztest_shared_hdr_t;
137420dfc95SChris Siden 
138420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
139420dfc95SChris Siden 
140420dfc95SChris Siden typedef struct ztest_shared_opts {
141420dfc95SChris Siden 	char zo_pool[MAXNAMELEN];
142420dfc95SChris Siden 	char zo_dir[MAXNAMELEN];
143420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
144420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
145420dfc95SChris Siden 	uint64_t zo_vdevs;
146420dfc95SChris Siden 	uint64_t zo_vdevtime;
147420dfc95SChris Siden 	size_t zo_vdev_size;
148420dfc95SChris Siden 	int zo_ashift;
149420dfc95SChris Siden 	int zo_mirrors;
150420dfc95SChris Siden 	int zo_raidz;
151420dfc95SChris Siden 	int zo_raidz_parity;
152420dfc95SChris Siden 	int zo_datasets;
153420dfc95SChris Siden 	int zo_threads;
154420dfc95SChris Siden 	uint64_t zo_passtime;
155420dfc95SChris Siden 	uint64_t zo_killrate;
156420dfc95SChris Siden 	int zo_verbose;
157420dfc95SChris Siden 	int zo_init;
158420dfc95SChris Siden 	uint64_t zo_time;
159420dfc95SChris Siden 	uint64_t zo_maxloops;
160420dfc95SChris Siden 	uint64_t zo_metaslab_gang_bang;
161420dfc95SChris Siden } ztest_shared_opts_t;
162420dfc95SChris Siden 
163420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
164420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
165420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
166420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
167420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
168420dfc95SChris Siden 	.zo_vdevs = 5,
169420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
170420dfc95SChris Siden 	.zo_mirrors = 2,
171420dfc95SChris Siden 	.zo_raidz = 4,
172420dfc95SChris Siden 	.zo_raidz_parity = 1,
173420dfc95SChris Siden 	.zo_vdev_size = SPA_MINDEVSIZE,
174420dfc95SChris Siden 	.zo_datasets = 7,
175420dfc95SChris Siden 	.zo_threads = 23,
176420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
177420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
178420dfc95SChris Siden 	.zo_verbose = 0,
179420dfc95SChris Siden 	.zo_init = 1,
180420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
181420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
182420dfc95SChris Siden 	.zo_metaslab_gang_bang = 32 << 10
183420dfc95SChris Siden };
184420dfc95SChris Siden 
185420dfc95SChris Siden extern uint64_t metaslab_gang_bang;
186420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
18769962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
188*30beaff4SGeorge Wilson extern int metaslab_preload_limit;
189420dfc95SChris Siden 
190420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
191420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
192420dfc95SChris Siden 
193420dfc95SChris Siden typedef struct ztest_shared_ds {
194420dfc95SChris Siden 	uint64_t	zd_seq;
195420dfc95SChris Siden } ztest_shared_ds_t;
196420dfc95SChris Siden 
197420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
198420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
199fa9e4066Sahrens 
200b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
201420dfc95SChris Siden #define	MAXFAULTS() \
202420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
203b24ab676SJeff Bonwick 
204b24ab676SJeff Bonwick enum ztest_io_type {
205b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
206b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
207b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
208b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
209b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
21080901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
211b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
212b24ab676SJeff Bonwick };
213b24ab676SJeff Bonwick 
214e05725b1Sbonwick typedef struct ztest_block_tag {
215b24ab676SJeff Bonwick 	uint64_t	bt_magic;
216e05725b1Sbonwick 	uint64_t	bt_objset;
217e05725b1Sbonwick 	uint64_t	bt_object;
218e05725b1Sbonwick 	uint64_t	bt_offset;
219b24ab676SJeff Bonwick 	uint64_t	bt_gen;
220e05725b1Sbonwick 	uint64_t	bt_txg;
221b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
222e05725b1Sbonwick } ztest_block_tag_t;
223e05725b1Sbonwick 
224b24ab676SJeff Bonwick typedef struct bufwad {
225b24ab676SJeff Bonwick 	uint64_t	bw_index;
226b24ab676SJeff Bonwick 	uint64_t	bw_txg;
227b24ab676SJeff Bonwick 	uint64_t	bw_data;
228b24ab676SJeff Bonwick } bufwad_t;
229b24ab676SJeff Bonwick 
230b24ab676SJeff Bonwick /*
231b24ab676SJeff Bonwick  * XXX -- fix zfs range locks to be generic so we can use them here.
232b24ab676SJeff Bonwick  */
233b24ab676SJeff Bonwick typedef enum {
234b24ab676SJeff Bonwick 	RL_READER,
235b24ab676SJeff Bonwick 	RL_WRITER,
236b24ab676SJeff Bonwick 	RL_APPEND
237b24ab676SJeff Bonwick } rl_type_t;
238b24ab676SJeff Bonwick 
239b24ab676SJeff Bonwick typedef struct rll {
240b24ab676SJeff Bonwick 	void		*rll_writer;
241b24ab676SJeff Bonwick 	int		rll_readers;
242b24ab676SJeff Bonwick 	mutex_t		rll_lock;
243b24ab676SJeff Bonwick 	cond_t		rll_cv;
244b24ab676SJeff Bonwick } rll_t;
245b24ab676SJeff Bonwick 
246b24ab676SJeff Bonwick typedef struct rl {
247b24ab676SJeff Bonwick 	uint64_t	rl_object;
248b24ab676SJeff Bonwick 	uint64_t	rl_offset;
249b24ab676SJeff Bonwick 	uint64_t	rl_size;
250b24ab676SJeff Bonwick 	rll_t		*rl_lock;
251b24ab676SJeff Bonwick } rl_t;
252b24ab676SJeff Bonwick 
253b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
254b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
255b24ab676SJeff Bonwick 
256b24ab676SJeff Bonwick /*
257b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
258b24ab676SJeff Bonwick  */
259b24ab676SJeff Bonwick typedef struct ztest_od {
260b24ab676SJeff Bonwick 	uint64_t	od_dir;
261b24ab676SJeff Bonwick 	uint64_t	od_object;
262b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
263b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
264b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
265b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
266b24ab676SJeff Bonwick 	uint64_t	od_gen;
267b24ab676SJeff Bonwick 	uint64_t	od_crgen;
268b24ab676SJeff Bonwick 	char		od_name[MAXNAMELEN];
269b24ab676SJeff Bonwick } ztest_od_t;
270fa9e4066Sahrens 
271b24ab676SJeff Bonwick /*
272b24ab676SJeff Bonwick  * Per-dataset state.
273b24ab676SJeff Bonwick  */
274b24ab676SJeff Bonwick typedef struct ztest_ds {
275420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
276b24ab676SJeff Bonwick 	objset_t	*zd_os;
277c9ba2a43SEric Schrock 	rwlock_t	zd_zilog_lock;
278b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
279b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
280b24ab676SJeff Bonwick 	char		zd_name[MAXNAMELEN];
281b24ab676SJeff Bonwick 	mutex_t		zd_dirobj_lock;
282b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
283b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
284b24ab676SJeff Bonwick } ztest_ds_t;
285b24ab676SJeff Bonwick 
286b24ab676SJeff Bonwick /*
287b24ab676SJeff Bonwick  * Per-iteration state.
288b24ab676SJeff Bonwick  */
289b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
290b24ab676SJeff Bonwick 
291b24ab676SJeff Bonwick typedef struct ztest_info {
292b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
293b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
294b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
295b24ab676SJeff Bonwick } ztest_info_t;
296fa9e4066Sahrens 
297420dfc95SChris Siden typedef struct ztest_shared_callstate {
298420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
299420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
300420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
301420dfc95SChris Siden } ztest_shared_callstate_t;
302420dfc95SChris Siden 
303420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
304420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
305420dfc95SChris Siden 
306fa9e4066Sahrens /*
307fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
308fa9e4066Sahrens  */
309fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
310fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
311fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
312d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
313fa9e4066Sahrens ztest_func_t ztest_zap;
314fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
315b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
316c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
317b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
318fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
319b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
320b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
321fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
322b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
323b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
324fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
325fa9e4066Sahrens ztest_func_t ztest_fault_inject;
326b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
327b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
328e14bb325SJeff Bonwick ztest_func_t ztest_spa_rename;
329b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
330b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
331fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
332fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
333fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
334e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3351195e687SMark J Musante ztest_func_t ztest_split_pool;
336e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
33725345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
338fa9e4066Sahrens 
339b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
340b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
341b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
342b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
343b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
344fa9e4066Sahrens 
345fa9e4066Sahrens ztest_info_t ztest_info[] = {
346e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
347b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
348e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
349b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
350e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
351e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3521195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
353b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
354c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
355b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
356b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
357b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
358b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
359b24ab676SJeff Bonwick #if 0
360b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
361b24ab676SJeff Bonwick #endif
362b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
363b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
364b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
365e05725b1Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
366b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
3675c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
3682c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
369e05725b1Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
370b24ab676SJeff Bonwick 	{ ztest_scrub,				1,	&zopt_rarely	},
37125345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
372b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
3733b2aab18SMatthew Ahrens 	{ ztest_vdev_attach_detach,		1,	&zopt_sometimes	},
374e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
375420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
376420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
377420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
378420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
379fa9e4066Sahrens };
380fa9e4066Sahrens 
381fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
382fa9e4066Sahrens 
383d20e665cSRicardo M. Correia /*
384d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
385d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
386d20e665cSRicardo M. Correia  */
387d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
388d20e665cSRicardo M. Correia 	mutex_t	zcl_callbacks_lock;
389d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
390d20e665cSRicardo M. Correia } ztest_cb_list_t;
391d20e665cSRicardo M. Correia 
392fa9e4066Sahrens /*
393fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
394fa9e4066Sahrens  */
395fa9e4066Sahrens typedef struct ztest_shared {
396420dfc95SChris Siden 	boolean_t	zs_do_init;
397b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
398b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
399b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
400b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
401b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
402b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
40388ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
404e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
405fa9e4066Sahrens 	uint64_t	zs_alloc;
406fa9e4066Sahrens 	uint64_t	zs_space;
4071195e687SMark J Musante 	uint64_t	zs_splits;
4081195e687SMark J Musante 	uint64_t	zs_mirrors;
409420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
410420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
411420dfc95SChris Siden 	uint64_t	zs_guid;
412fa9e4066Sahrens } ztest_shared_t;
413fa9e4066Sahrens 
414b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
415b24ab676SJeff Bonwick 
416fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
417e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
418b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
419fa9e4066Sahrens 
420420dfc95SChris Siden static spa_t *ztest_spa = NULL;
421420dfc95SChris Siden static ztest_ds_t *ztest_ds;
422420dfc95SChris Siden 
423420dfc95SChris Siden static mutex_t ztest_vdev_lock;
424dfbb9432SGeorge Wilson 
425dfbb9432SGeorge Wilson /*
426dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
427dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
428dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
429dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
430dfbb9432SGeorge Wilson  */
431420dfc95SChris Siden static rwlock_t ztest_name_lock;
432fa9e4066Sahrens 
433420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
434e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4350a4e9518Sgw 
436d20e665cSRicardo M. Correia /* Global commit callback list */
437d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
438d20e665cSRicardo M. Correia 
439b24ab676SJeff Bonwick enum ztest_object {
440b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
441b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
442b24ab676SJeff Bonwick 	ZTEST_OBJECTS
443b24ab676SJeff Bonwick };
444fa9e4066Sahrens 
4451ce825d8Sraf static void usage(boolean_t) __NORETURN;
446f1b4288bSvb 
447fa9e4066Sahrens /*
448fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
449fa9e4066Sahrens  * debugging facilities.
450fa9e4066Sahrens  */
451fa9e4066Sahrens const char *
452fa9e4066Sahrens _umem_debug_init()
453fa9e4066Sahrens {
454fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
455fa9e4066Sahrens }
456fa9e4066Sahrens 
457fa9e4066Sahrens const char *
458fa9e4066Sahrens _umem_logging_init(void)
459fa9e4066Sahrens {
460fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
461fa9e4066Sahrens }
462fa9e4066Sahrens 
463fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
464fa9e4066Sahrens 
465fa9e4066Sahrens char *fatal_msg;
466fa9e4066Sahrens 
467fa9e4066Sahrens static void
468fa9e4066Sahrens fatal(int do_perror, char *message, ...)
469fa9e4066Sahrens {
470fa9e4066Sahrens 	va_list args;
471fa9e4066Sahrens 	int save_errno = errno;
472fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
473fa9e4066Sahrens 
474fa9e4066Sahrens 	(void) fflush(stdout);
475fa9e4066Sahrens 
476fa9e4066Sahrens 	va_start(args, message);
477fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
478fa9e4066Sahrens 	/* LINTED */
479fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
480fa9e4066Sahrens 	va_end(args);
481fa9e4066Sahrens 	if (do_perror) {
482fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
483fa9e4066Sahrens 		    ": %s", strerror(save_errno));
484fa9e4066Sahrens 	}
485fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
486fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
487fa9e4066Sahrens 	if (ztest_dump_core)
488fa9e4066Sahrens 		abort();
489fa9e4066Sahrens 	exit(3);
490fa9e4066Sahrens }
491fa9e4066Sahrens 
492fa9e4066Sahrens static int
493fa9e4066Sahrens str2shift(const char *buf)
494fa9e4066Sahrens {
495fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
496fa9e4066Sahrens 	int i;
497fa9e4066Sahrens 
498fa9e4066Sahrens 	if (buf[0] == '\0')
499fa9e4066Sahrens 		return (0);
500fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
501fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
502fa9e4066Sahrens 			break;
503fa9e4066Sahrens 	}
504f1b4288bSvb 	if (i == strlen(ends)) {
505f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
506f1b4288bSvb 		    buf);
507f1b4288bSvb 		usage(B_FALSE);
508f1b4288bSvb 	}
509fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
510fa9e4066Sahrens 		return (10*i);
511fa9e4066Sahrens 	}
512f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
513f1b4288bSvb 	usage(B_FALSE);
514f1b4288bSvb 	/* NOTREACHED */
515fa9e4066Sahrens }
516fa9e4066Sahrens 
517fa9e4066Sahrens static uint64_t
518fa9e4066Sahrens nicenumtoull(const char *buf)
519fa9e4066Sahrens {
520fa9e4066Sahrens 	char *end;
521fa9e4066Sahrens 	uint64_t val;
522fa9e4066Sahrens 
523fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
524fa9e4066Sahrens 	if (end == buf) {
525f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
526f1b4288bSvb 		usage(B_FALSE);
527fa9e4066Sahrens 	} else if (end[0] == '.') {
528fa9e4066Sahrens 		double fval = strtod(buf, &end);
529fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
530f1b4288bSvb 		if (fval > UINT64_MAX) {
531f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
532f1b4288bSvb 			    buf);
533f1b4288bSvb 			usage(B_FALSE);
534f1b4288bSvb 		}
535fa9e4066Sahrens 		val = (uint64_t)fval;
536fa9e4066Sahrens 	} else {
537fa9e4066Sahrens 		int shift = str2shift(end);
538f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
539f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
540f1b4288bSvb 			    buf);
541f1b4288bSvb 			usage(B_FALSE);
542f1b4288bSvb 		}
543fa9e4066Sahrens 		val <<= shift;
544fa9e4066Sahrens 	}
545fa9e4066Sahrens 	return (val);
546fa9e4066Sahrens }
547fa9e4066Sahrens 
548fa9e4066Sahrens static void
549f1b4288bSvb usage(boolean_t requested)
550fa9e4066Sahrens {
551420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
552420dfc95SChris Siden 
553fa9e4066Sahrens 	char nice_vdev_size[10];
554fa9e4066Sahrens 	char nice_gang_bang[10];
555f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
556fa9e4066Sahrens 
557420dfc95SChris Siden 	nicenum(zo->zo_vdev_size, nice_vdev_size);
558420dfc95SChris Siden 	nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
559fa9e4066Sahrens 
560f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
561fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
562fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
5632215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
564fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
565fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
56699653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
567fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
568fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
569fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
5702215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
5712215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
572fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
5732215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
5742215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
5752215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
5762215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
5772215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
5782215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
579420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
580f1b4288bSvb 	    "\t[-h] (print help)\n"
581fa9e4066Sahrens 	    "",
582420dfc95SChris Siden 	    zo->zo_pool,
583420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
5840a4e9518Sgw 	    nice_vdev_size,				/* -s */
585420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
586420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
587420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
588420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
589420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
590420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
5910a4e9518Sgw 	    nice_gang_bang,				/* -g */
592420dfc95SChris Siden 	    zo->zo_init,				/* -i */
593420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
594420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
595420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
596420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
597420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
598420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
599f1b4288bSvb 	exit(requested ? 0 : 1);
600fa9e4066Sahrens }
601fa9e4066Sahrens 
602fa9e4066Sahrens static void
603fa9e4066Sahrens process_options(int argc, char **argv)
604fa9e4066Sahrens {
605420dfc95SChris Siden 	char *path;
606420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
607420dfc95SChris Siden 
608fa9e4066Sahrens 	int opt;
609fa9e4066Sahrens 	uint64_t value;
610420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
611fa9e4066Sahrens 
612420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
613fa9e4066Sahrens 
614fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
615420dfc95SChris Siden 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
616fa9e4066Sahrens 		value = 0;
617fa9e4066Sahrens 		switch (opt) {
6183d7072f8Seschrock 		case 'v':
6193d7072f8Seschrock 		case 's':
6203d7072f8Seschrock 		case 'a':
6213d7072f8Seschrock 		case 'm':
6223d7072f8Seschrock 		case 'r':
6233d7072f8Seschrock 		case 'R':
6243d7072f8Seschrock 		case 'd':
6253d7072f8Seschrock 		case 't':
6263d7072f8Seschrock 		case 'g':
6273d7072f8Seschrock 		case 'i':
6283d7072f8Seschrock 		case 'k':
6293d7072f8Seschrock 		case 'T':
6303d7072f8Seschrock 		case 'P':
6312215e990SMark J Musante 		case 'F':
632fa9e4066Sahrens 			value = nicenumtoull(optarg);
633fa9e4066Sahrens 		}
634fa9e4066Sahrens 		switch (opt) {
6353d7072f8Seschrock 		case 'v':
636420dfc95SChris Siden 			zo->zo_vdevs = value;
637fa9e4066Sahrens 			break;
6383d7072f8Seschrock 		case 's':
639420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
640fa9e4066Sahrens 			break;
6413d7072f8Seschrock 		case 'a':
642420dfc95SChris Siden 			zo->zo_ashift = value;
643ecc2d604Sbonwick 			break;
6443d7072f8Seschrock 		case 'm':
645420dfc95SChris Siden 			zo->zo_mirrors = value;
646fa9e4066Sahrens 			break;
6473d7072f8Seschrock 		case 'r':
648420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
649fa9e4066Sahrens 			break;
6503d7072f8Seschrock 		case 'R':
651420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
65299653d4eSeschrock 			break;
6533d7072f8Seschrock 		case 'd':
654420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
655fa9e4066Sahrens 			break;
6563d7072f8Seschrock 		case 't':
657420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
658fa9e4066Sahrens 			break;
6593d7072f8Seschrock 		case 'g':
660420dfc95SChris Siden 			zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
661420dfc95SChris Siden 			    value);
662fa9e4066Sahrens 			break;
6633d7072f8Seschrock 		case 'i':
664420dfc95SChris Siden 			zo->zo_init = value;
665fa9e4066Sahrens 			break;
6663d7072f8Seschrock 		case 'k':
667420dfc95SChris Siden 			zo->zo_killrate = value;
668fa9e4066Sahrens 			break;
6693d7072f8Seschrock 		case 'p':
670420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
671420dfc95SChris Siden 			    sizeof (zo->zo_pool));
672fa9e4066Sahrens 			break;
6733d7072f8Seschrock 		case 'f':
674420dfc95SChris Siden 			path = realpath(optarg, NULL);
675420dfc95SChris Siden 			if (path == NULL) {
676420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
677420dfc95SChris Siden 				    optarg, strerror(errno));
678420dfc95SChris Siden 				usage(B_FALSE);
679420dfc95SChris Siden 			} else {
680420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
681420dfc95SChris Siden 				    sizeof (zo->zo_dir));
682420dfc95SChris Siden 			}
683fa9e4066Sahrens 			break;
6843d7072f8Seschrock 		case 'V':
685420dfc95SChris Siden 			zo->zo_verbose++;
686fa9e4066Sahrens 			break;
6873d7072f8Seschrock 		case 'E':
688420dfc95SChris Siden 			zo->zo_init = 0;
689fa9e4066Sahrens 			break;
6903d7072f8Seschrock 		case 'T':
691420dfc95SChris Siden 			zo->zo_time = value;
692fa9e4066Sahrens 			break;
6933d7072f8Seschrock 		case 'P':
694420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
695fa9e4066Sahrens 			break;
6962215e990SMark J Musante 		case 'F':
697420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
698420dfc95SChris Siden 			break;
699420dfc95SChris Siden 		case 'B':
700420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7012215e990SMark J Musante 			break;
7023d7072f8Seschrock 		case 'h':
703f1b4288bSvb 			usage(B_TRUE);
704f1b4288bSvb 			break;
7053d7072f8Seschrock 		case '?':
7063d7072f8Seschrock 		default:
707f1b4288bSvb 			usage(B_FALSE);
708fa9e4066Sahrens 			break;
709fa9e4066Sahrens 		}
710fa9e4066Sahrens 	}
711fa9e4066Sahrens 
712420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
71399653d4eSeschrock 
714420dfc95SChris Siden 	zo->zo_vdevtime =
715420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
716b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
717420dfc95SChris Siden 
718420dfc95SChris Siden 	if (strlen(altdir) > 0) {
719741652b0SEtienne Dechamps 		char *cmd;
720741652b0SEtienne Dechamps 		char *realaltdir;
721420dfc95SChris Siden 		char *bin;
722420dfc95SChris Siden 		char *ztest;
723420dfc95SChris Siden 		char *isa;
724420dfc95SChris Siden 		int isalen;
725420dfc95SChris Siden 
726741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
727741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
728741652b0SEtienne Dechamps 
729741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
730420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
731420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
732420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
733420dfc95SChris Siden 			    altdir);
734420dfc95SChris Siden 		}
735420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
736420dfc95SChris Siden 
737420dfc95SChris Siden 		/*
738420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
739420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
740420dfc95SChris Siden 		 * 32 or 64 bit binaries.
741420dfc95SChris Siden 		 */
742420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
743420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
744420dfc95SChris Siden 		isa = bin + 9;
745420dfc95SChris Siden 		isalen = ztest - isa;
746420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
747420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
748420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
749420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
750420dfc95SChris Siden 
751420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
752420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
753420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
754420dfc95SChris Siden 			    zo->zo_alt_ztest);
755420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
756420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
757420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
758420dfc95SChris Siden 			    zo->zo_alt_libpath);
759420dfc95SChris Siden 		}
760741652b0SEtienne Dechamps 
761741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
762741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
763420dfc95SChris Siden 	}
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766b24ab676SJeff Bonwick static void
767b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
768b24ab676SJeff Bonwick {
769420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
770420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
771b4952e17SGeorge Wilson 
772b4952e17SGeorge Wilson 	/*
773b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
774b4952e17SGeorge Wilson 	 * See comment above spa_config_sync().
775b4952e17SGeorge Wilson 	 */
776b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
777b4952e17SGeorge Wilson 	spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
778b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
779b4952e17SGeorge Wilson 
780b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
781b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
782b24ab676SJeff Bonwick }
783b24ab676SJeff Bonwick 
784b24ab676SJeff Bonwick static uint64_t
785b24ab676SJeff Bonwick ztest_random(uint64_t range)
786b24ab676SJeff Bonwick {
787b24ab676SJeff Bonwick 	uint64_t r;
788b24ab676SJeff Bonwick 
789741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
790741652b0SEtienne Dechamps 
791b24ab676SJeff Bonwick 	if (range == 0)
792b24ab676SJeff Bonwick 		return (0);
793b24ab676SJeff Bonwick 
794741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
795b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
796b24ab676SJeff Bonwick 
797b24ab676SJeff Bonwick 	return (r % range);
798b24ab676SJeff Bonwick }
799b24ab676SJeff Bonwick 
800b24ab676SJeff Bonwick /* ARGSUSED */
801b24ab676SJeff Bonwick static void
802b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
803b24ab676SJeff Bonwick {
804b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
805b24ab676SJeff Bonwick }
806b24ab676SJeff Bonwick 
807ecc2d604Sbonwick static uint64_t
808ecc2d604Sbonwick ztest_get_ashift(void)
809ecc2d604Sbonwick {
810420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
811ecc2d604Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
812420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
813ecc2d604Sbonwick }
814ecc2d604Sbonwick 
815fa9e4066Sahrens static nvlist_t *
81625345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
817fa9e4066Sahrens {
818e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
819fa9e4066Sahrens 	uint64_t vdev;
820fa9e4066Sahrens 	nvlist_t *file;
821fa9e4066Sahrens 
822e14bb325SJeff Bonwick 	if (ashift == 0)
823e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
824e14bb325SJeff Bonwick 
825e14bb325SJeff Bonwick 	if (path == NULL) {
826e14bb325SJeff Bonwick 		path = pathbuf;
827fa9e4066Sahrens 
828e14bb325SJeff Bonwick 		if (aux != NULL) {
829e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
830420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
831420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
83225345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
83325345e46SGeorge Wilson 			    aux, vdev);
834e14bb325SJeff Bonwick 		} else {
83588ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
836420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
837420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
83825345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
839e14bb325SJeff Bonwick 		}
840e14bb325SJeff Bonwick 	}
841e14bb325SJeff Bonwick 
842e14bb325SJeff Bonwick 	if (size != 0) {
843e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
844fa9e4066Sahrens 		if (fd == -1)
845e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
846fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
847e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
848fa9e4066Sahrens 		(void) close(fd);
849fa9e4066Sahrens 	}
850fa9e4066Sahrens 
851fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
852fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
853e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
854ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
855fa9e4066Sahrens 
856fa9e4066Sahrens 	return (file);
857fa9e4066Sahrens }
858fa9e4066Sahrens 
859fa9e4066Sahrens static nvlist_t *
86025345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
86125345e46SGeorge Wilson     uint64_t ashift, int r)
862fa9e4066Sahrens {
863fa9e4066Sahrens 	nvlist_t *raidz, **child;
864fa9e4066Sahrens 	int c;
865fa9e4066Sahrens 
866fa9e4066Sahrens 	if (r < 2)
86725345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
868fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
869fa9e4066Sahrens 
870fa9e4066Sahrens 	for (c = 0; c < r; c++)
87125345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
872fa9e4066Sahrens 
873fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
874fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
875fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
87699653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
877420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
878fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
879fa9e4066Sahrens 	    child, r) == 0);
880fa9e4066Sahrens 
881fa9e4066Sahrens 	for (c = 0; c < r; c++)
882fa9e4066Sahrens 		nvlist_free(child[c]);
883fa9e4066Sahrens 
884fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
885fa9e4066Sahrens 
886fa9e4066Sahrens 	return (raidz);
887fa9e4066Sahrens }
888fa9e4066Sahrens 
889fa9e4066Sahrens static nvlist_t *
89025345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
89125345e46SGeorge Wilson     uint64_t ashift, int r, int m)
892fa9e4066Sahrens {
893fa9e4066Sahrens 	nvlist_t *mirror, **child;
894fa9e4066Sahrens 	int c;
895fa9e4066Sahrens 
896fa9e4066Sahrens 	if (m < 1)
89725345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
898fa9e4066Sahrens 
899fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
900fa9e4066Sahrens 
901fa9e4066Sahrens 	for (c = 0; c < m; c++)
90225345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
903fa9e4066Sahrens 
904fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
905fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
906fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
907fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
908fa9e4066Sahrens 	    child, m) == 0);
909fa9e4066Sahrens 
910fa9e4066Sahrens 	for (c = 0; c < m; c++)
911fa9e4066Sahrens 		nvlist_free(child[c]);
912fa9e4066Sahrens 
913fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
914fa9e4066Sahrens 
915fa9e4066Sahrens 	return (mirror);
916fa9e4066Sahrens }
917fa9e4066Sahrens 
918fa9e4066Sahrens static nvlist_t *
91925345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
92025345e46SGeorge Wilson     int log, int r, int m, int t)
921fa9e4066Sahrens {
922fa9e4066Sahrens 	nvlist_t *root, **child;
923fa9e4066Sahrens 	int c;
924fa9e4066Sahrens 
925fa9e4066Sahrens 	ASSERT(t > 0);
926fa9e4066Sahrens 
927fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
928fa9e4066Sahrens 
92931157203SJeff Bonwick 	for (c = 0; c < t; c++) {
93025345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
93125345e46SGeorge Wilson 		    r, m);
93231157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
93331157203SJeff Bonwick 		    log) == 0);
93431157203SJeff Bonwick 	}
935fa9e4066Sahrens 
936fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
937fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
938e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
939fa9e4066Sahrens 	    child, t) == 0);
940fa9e4066Sahrens 
941fa9e4066Sahrens 	for (c = 0; c < t; c++)
942fa9e4066Sahrens 		nvlist_free(child[c]);
943fa9e4066Sahrens 
944fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
945fa9e4066Sahrens 
946fa9e4066Sahrens 	return (root);
947fa9e4066Sahrens }
948fa9e4066Sahrens 
94925345e46SGeorge Wilson /*
95025345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
95125345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
95225345e46SGeorge Wilson  */
95325345e46SGeorge Wilson static uint64_t
95425345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
95525345e46SGeorge Wilson {
95625345e46SGeorge Wilson 	uint64_t version = initial_version;
95725345e46SGeorge Wilson 
95825345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
95925345e46SGeorge Wilson 		version = version +
96025345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
96125345e46SGeorge Wilson 	}
96225345e46SGeorge Wilson 
96325345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
96425345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
96525345e46SGeorge Wilson 
96625345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
96725345e46SGeorge Wilson 	return (version);
96825345e46SGeorge Wilson }
96925345e46SGeorge Wilson 
970b24ab676SJeff Bonwick static int
971b24ab676SJeff Bonwick ztest_random_blocksize(void)
972fa9e4066Sahrens {
973b24ab676SJeff Bonwick 	return (1 << (SPA_MINBLOCKSHIFT +
974b24ab676SJeff Bonwick 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
975b24ab676SJeff Bonwick }
976fa9e4066Sahrens 
977b24ab676SJeff Bonwick static int
978b24ab676SJeff Bonwick ztest_random_ibshift(void)
979b24ab676SJeff Bonwick {
980b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
981b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
982fa9e4066Sahrens }
983fa9e4066Sahrens 
984b24ab676SJeff Bonwick static uint64_t
985b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
986fa9e4066Sahrens {
987b24ab676SJeff Bonwick 	uint64_t top;
988b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
989b24ab676SJeff Bonwick 	vdev_t *tvd;
990fa9e4066Sahrens 
991b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
992fa9e4066Sahrens 
993b24ab676SJeff Bonwick 	do {
994b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
995b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
996b24ab676SJeff Bonwick 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
997b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
998fa9e4066Sahrens 
999b24ab676SJeff Bonwick 	return (top);
1000fa9e4066Sahrens }
1001fa9e4066Sahrens 
1002b24ab676SJeff Bonwick static uint64_t
1003b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1004fa9e4066Sahrens {
1005b24ab676SJeff Bonwick 	uint64_t value;
1006b24ab676SJeff Bonwick 
1007b24ab676SJeff Bonwick 	do {
1008b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1009b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1010b24ab676SJeff Bonwick 
1011b24ab676SJeff Bonwick 	return (value);
1012fa9e4066Sahrens }
1013fa9e4066Sahrens 
1014fa9e4066Sahrens static int
1015b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1016b24ab676SJeff Bonwick     boolean_t inherit)
1017fa9e4066Sahrens {
1018b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1019b24ab676SJeff Bonwick 	const char *valname;
1020b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1021b24ab676SJeff Bonwick 	uint64_t curval;
1022fa9e4066Sahrens 	int error;
1023fa9e4066Sahrens 
10243b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
10253b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1026fa9e4066Sahrens 
1027b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1028b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1029fa9e4066Sahrens 		return (error);
1030fa9e4066Sahrens 	}
1031fb09f5aaSMadhav Suresh 	ASSERT0(error);
1032fa9e4066Sahrens 
10333b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1034b24ab676SJeff Bonwick 
1035420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1036b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1037b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1038b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1039fa9e4066Sahrens 	}
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	return (error);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens 
1044fa9e4066Sahrens static int
1045420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1046fa9e4066Sahrens {
1047420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1048b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1049fa9e4066Sahrens 	int error;
1050fa9e4066Sahrens 
1051b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1052b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1053fa9e4066Sahrens 
1054b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1055b24ab676SJeff Bonwick 
1056b24ab676SJeff Bonwick 	nvlist_free(props);
1057b24ab676SJeff Bonwick 
1058b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1059b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1060fa9e4066Sahrens 		return (error);
1061fa9e4066Sahrens 	}
1062fb09f5aaSMadhav Suresh 	ASSERT0(error);
1063fa9e4066Sahrens 
1064fa9e4066Sahrens 	return (error);
1065fa9e4066Sahrens }
1066fa9e4066Sahrens 
1067b24ab676SJeff Bonwick static void
1068b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1069b24ab676SJeff Bonwick {
1070b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1071b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1072b24ab676SJeff Bonwick 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
1073b24ab676SJeff Bonwick 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
1074b24ab676SJeff Bonwick }
1075fa9e4066Sahrens 
1076b24ab676SJeff Bonwick static void
1077b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1078fa9e4066Sahrens {
1079b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1080b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1081b24ab676SJeff Bonwick 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
1082b24ab676SJeff Bonwick 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
1083b24ab676SJeff Bonwick }
1084fa9e4066Sahrens 
1085b24ab676SJeff Bonwick static void
1086b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1087b24ab676SJeff Bonwick {
1088b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
1089fa9e4066Sahrens 
1090b24ab676SJeff Bonwick 	if (type == RL_READER) {
1091b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1092b24ab676SJeff Bonwick 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
1093b24ab676SJeff Bonwick 		rll->rll_readers++;
1094b24ab676SJeff Bonwick 	} else {
1095b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1096b24ab676SJeff Bonwick 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
1097b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1098b24ab676SJeff Bonwick 	}
1099fa9e4066Sahrens 
1100b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
1101b24ab676SJeff Bonwick }
1102fa9e4066Sahrens 
1103b24ab676SJeff Bonwick static void
1104b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1105b24ab676SJeff Bonwick {
1106b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
1107fa9e4066Sahrens 
1108b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1109b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1110b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1111b24ab676SJeff Bonwick 	} else {
1112b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1113b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1114b24ab676SJeff Bonwick 		rll->rll_readers--;
1115b24ab676SJeff Bonwick 	}
1116fa9e4066Sahrens 
1117b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1118b24ab676SJeff Bonwick 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
1119b24ab676SJeff Bonwick 
1120b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
1121fa9e4066Sahrens }
1122fa9e4066Sahrens 
1123b24ab676SJeff Bonwick static void
1124b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
112531157203SJeff Bonwick {
1126b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
112731157203SJeff Bonwick 
1128b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1129b24ab676SJeff Bonwick }
113031157203SJeff Bonwick 
1131b24ab676SJeff Bonwick static void
1132b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1133b24ab676SJeff Bonwick {
1134b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
113531157203SJeff Bonwick 
1136b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
113731157203SJeff Bonwick }
113831157203SJeff Bonwick 
1139b24ab676SJeff Bonwick static rl_t *
1140b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1141b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
114288ecc943SGeorge Wilson {
1143b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1144b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1145b24ab676SJeff Bonwick 	rl_t *rl;
114688ecc943SGeorge Wilson 
1147b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1148b24ab676SJeff Bonwick 	rl->rl_object = object;
1149b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1150b24ab676SJeff Bonwick 	rl->rl_size = size;
1151b24ab676SJeff Bonwick 	rl->rl_lock = rll;
115288ecc943SGeorge Wilson 
1153b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
115488ecc943SGeorge Wilson 
1155b24ab676SJeff Bonwick 	return (rl);
115688ecc943SGeorge Wilson }
115788ecc943SGeorge Wilson 
1158b24ab676SJeff Bonwick static void
1159b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1160fa9e4066Sahrens {
1161b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1162fa9e4066Sahrens 
1163b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1164fa9e4066Sahrens 
1165b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1166b24ab676SJeff Bonwick }
1167fa9e4066Sahrens 
1168b24ab676SJeff Bonwick static void
1169420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1170b24ab676SJeff Bonwick {
1171b24ab676SJeff Bonwick 	zd->zd_os = os;
1172b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1173420dfc95SChris Siden 	zd->zd_shared = szd;
1174b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1175fa9e4066Sahrens 
1176420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1177420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1178420dfc95SChris Siden 
1179c9ba2a43SEric Schrock 	VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1180b24ab676SJeff Bonwick 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
1181b24ab676SJeff Bonwick 
1182b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1183b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1184b24ab676SJeff Bonwick 
1185b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1186b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1187b24ab676SJeff Bonwick }
1188b24ab676SJeff Bonwick 
1189b24ab676SJeff Bonwick static void
1190b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1191b24ab676SJeff Bonwick {
1192b24ab676SJeff Bonwick 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1193b24ab676SJeff Bonwick 
1194b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1195b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1196b24ab676SJeff Bonwick 
1197b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1198b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1199b24ab676SJeff Bonwick }
1200b24ab676SJeff Bonwick 
1201b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1202b24ab676SJeff Bonwick 
1203b24ab676SJeff Bonwick static uint64_t
1204b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1205b24ab676SJeff Bonwick {
1206b24ab676SJeff Bonwick 	uint64_t txg;
1207b24ab676SJeff Bonwick 	int error;
1208b24ab676SJeff Bonwick 
1209b24ab676SJeff Bonwick 	/*
1210b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1211b24ab676SJeff Bonwick 	 */
1212b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1213b24ab676SJeff Bonwick 	if (error) {
1214b24ab676SJeff Bonwick 		if (error == ERESTART) {
1215b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1216b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1217b24ab676SJeff Bonwick 		} else {
1218b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1219b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1220b24ab676SJeff Bonwick 		}
1221b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1222b24ab676SJeff Bonwick 		return (0);
1223b24ab676SJeff Bonwick 	}
1224b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1225b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1226b24ab676SJeff Bonwick 	return (txg);
1227b24ab676SJeff Bonwick }
1228b24ab676SJeff Bonwick 
1229b24ab676SJeff Bonwick static void
1230b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1231b24ab676SJeff Bonwick {
1232b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1233b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1234b24ab676SJeff Bonwick 
1235b24ab676SJeff Bonwick 	while (ip < ip_end)
1236b24ab676SJeff Bonwick 		*ip++ = value;
1237b24ab676SJeff Bonwick }
1238b24ab676SJeff Bonwick 
1239b24ab676SJeff Bonwick static boolean_t
1240b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1241b24ab676SJeff Bonwick {
1242b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1243b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1244b24ab676SJeff Bonwick 	uint64_t diff = 0;
1245b24ab676SJeff Bonwick 
1246b24ab676SJeff Bonwick 	while (ip < ip_end)
1247b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1248b24ab676SJeff Bonwick 
1249b24ab676SJeff Bonwick 	return (diff == 0);
1250b24ab676SJeff Bonwick }
1251b24ab676SJeff Bonwick 
1252b24ab676SJeff Bonwick static void
1253b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1254b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1255b24ab676SJeff Bonwick {
1256b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1257b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1258b24ab676SJeff Bonwick 	bt->bt_object = object;
1259b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1260b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1261b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1262b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1263b24ab676SJeff Bonwick }
1264b24ab676SJeff Bonwick 
1265b24ab676SJeff Bonwick static void
1266b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1267b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1268b24ab676SJeff Bonwick {
12695d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
12705d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
12715d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
12725d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
12735d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
12745d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
12755d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1276b24ab676SJeff Bonwick }
1277b24ab676SJeff Bonwick 
1278b24ab676SJeff Bonwick static ztest_block_tag_t *
1279b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1280b24ab676SJeff Bonwick {
1281b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1282b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1283b24ab676SJeff Bonwick 
1284b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1285b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1286b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1287b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1288b24ab676SJeff Bonwick 
1289b24ab676SJeff Bonwick 	return (bt);
1290b24ab676SJeff Bonwick }
1291b24ab676SJeff Bonwick 
1292b24ab676SJeff Bonwick /*
1293b24ab676SJeff Bonwick  * ZIL logging ops
1294b24ab676SJeff Bonwick  */
1295b24ab676SJeff Bonwick 
1296b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1297b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1298b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1299b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
1300b24ab676SJeff Bonwick #define	lrz_bonuslen	lr_crtime[1]
1301b24ab676SJeff Bonwick 
13025002558fSNeil Perrin static void
1303b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1304b24ab676SJeff Bonwick {
1305b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1306b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1307b24ab676SJeff Bonwick 	itx_t *itx;
1308b24ab676SJeff Bonwick 
1309b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13105002558fSNeil Perrin 		return;
1311b24ab676SJeff Bonwick 
1312b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1313b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1314b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1315b24ab676SJeff Bonwick 
13165002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1317b24ab676SJeff Bonwick }
1318b24ab676SJeff Bonwick 
13195002558fSNeil Perrin static void
13205002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1321b24ab676SJeff Bonwick {
1322b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1323b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1324b24ab676SJeff Bonwick 	itx_t *itx;
1325b24ab676SJeff Bonwick 
1326b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13275002558fSNeil Perrin 		return;
1328b24ab676SJeff Bonwick 
1329b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1330b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1331b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1332b24ab676SJeff Bonwick 
133351bd2f97SNeil Perrin 	itx->itx_oid = object;
13345002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1335b24ab676SJeff Bonwick }
1336b24ab676SJeff Bonwick 
13375002558fSNeil Perrin static void
1338b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1339b24ab676SJeff Bonwick {
1340b24ab676SJeff Bonwick 	itx_t *itx;
1341b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1342b24ab676SJeff Bonwick 
1343b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13445002558fSNeil Perrin 		return;
1345b24ab676SJeff Bonwick 
1346b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1347b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1348b24ab676SJeff Bonwick 
1349b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1350b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1351b24ab676SJeff Bonwick 
1352b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1353b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1354b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1355b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1356b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1357b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1358b24ab676SJeff Bonwick 	}
1359b24ab676SJeff Bonwick 	itx->itx_private = zd;
1360b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1361b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1362b24ab676SJeff Bonwick 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1363b24ab676SJeff Bonwick 
1364b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1365b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1366b24ab676SJeff Bonwick 
13675002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1368b24ab676SJeff Bonwick }
1369b24ab676SJeff Bonwick 
13705002558fSNeil Perrin static void
1371b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1372b24ab676SJeff Bonwick {
1373b24ab676SJeff Bonwick 	itx_t *itx;
1374b24ab676SJeff Bonwick 
1375b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13765002558fSNeil Perrin 		return;
1377b24ab676SJeff Bonwick 
1378b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1379b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1380b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1381b24ab676SJeff Bonwick 
13825002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
13835002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1384b24ab676SJeff Bonwick }
1385b24ab676SJeff Bonwick 
13865002558fSNeil Perrin static void
1387b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1388b24ab676SJeff Bonwick {
1389b24ab676SJeff Bonwick 	itx_t *itx;
1390b24ab676SJeff Bonwick 
1391b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13925002558fSNeil Perrin 		return;
1393b24ab676SJeff Bonwick 
1394b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1395b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1396b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1397b24ab676SJeff Bonwick 
13985002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
13995002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1400b24ab676SJeff Bonwick }
1401b24ab676SJeff Bonwick 
1402b24ab676SJeff Bonwick /*
1403b24ab676SJeff Bonwick  * ZIL replay ops
1404b24ab676SJeff Bonwick  */
1405b24ab676SJeff Bonwick static int
1406b24ab676SJeff Bonwick ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1407b24ab676SJeff Bonwick {
1408b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1409b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1410b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1411b24ab676SJeff Bonwick 	dmu_buf_t *db;
1412b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1413b24ab676SJeff Bonwick 	uint64_t txg;
1414b24ab676SJeff Bonwick 	int error = 0;
1415b24ab676SJeff Bonwick 
1416b24ab676SJeff Bonwick 	if (byteswap)
1417b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1418b24ab676SJeff Bonwick 
1419b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1420b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1421b24ab676SJeff Bonwick 
1422b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1423b24ab676SJeff Bonwick 
1424b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1425b24ab676SJeff Bonwick 
1426b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1427b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1428b24ab676SJeff Bonwick 	} else {
1429b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1430b24ab676SJeff Bonwick 	}
1431b24ab676SJeff Bonwick 
1432b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1433b24ab676SJeff Bonwick 	if (txg == 0)
1434b24ab676SJeff Bonwick 		return (ENOSPC);
1435b24ab676SJeff Bonwick 
1436b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1437b24ab676SJeff Bonwick 
1438b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1439b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1440b24ab676SJeff Bonwick 			lr->lr_foid = zap_create(os,
1441b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1442b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1443b24ab676SJeff Bonwick 		} else {
1444b24ab676SJeff Bonwick 			error = zap_create_claim(os, lr->lr_foid,
1445b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1446b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1447b24ab676SJeff Bonwick 		}
1448b24ab676SJeff Bonwick 	} else {
1449b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1450b24ab676SJeff Bonwick 			lr->lr_foid = dmu_object_alloc(os,
1451b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1452b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1453b24ab676SJeff Bonwick 		} else {
1454b24ab676SJeff Bonwick 			error = dmu_object_claim(os, lr->lr_foid,
1455b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1456b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1457b24ab676SJeff Bonwick 		}
1458b24ab676SJeff Bonwick 	}
1459b24ab676SJeff Bonwick 
1460b24ab676SJeff Bonwick 	if (error) {
1461b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1462b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1463b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1464b24ab676SJeff Bonwick 		return (error);
1465b24ab676SJeff Bonwick 	}
1466b24ab676SJeff Bonwick 
1467b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1468b24ab676SJeff Bonwick 
1469b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1470b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1471b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1472b24ab676SJeff Bonwick 
1473b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1474b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1475b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1476b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1477b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1478b24ab676SJeff Bonwick 
1479b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1480b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1481b24ab676SJeff Bonwick 
1482b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1483b24ab676SJeff Bonwick 
1484b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1485b24ab676SJeff Bonwick 
1486b24ab676SJeff Bonwick 	return (0);
1487b24ab676SJeff Bonwick }
1488b24ab676SJeff Bonwick 
1489b24ab676SJeff Bonwick static int
1490b24ab676SJeff Bonwick ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1491b24ab676SJeff Bonwick {
1492b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1493b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1494b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1495b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1496b24ab676SJeff Bonwick 	uint64_t object, txg;
1497b24ab676SJeff Bonwick 
1498b24ab676SJeff Bonwick 	if (byteswap)
1499b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1500b24ab676SJeff Bonwick 
1501b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1502b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1503b24ab676SJeff Bonwick 
1504b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1505b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1506b24ab676SJeff Bonwick 	ASSERT(object != 0);
1507b24ab676SJeff Bonwick 
1508b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1509b24ab676SJeff Bonwick 
1510b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1511b24ab676SJeff Bonwick 
1512b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1513b24ab676SJeff Bonwick 
1514b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1515b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1516b24ab676SJeff Bonwick 
1517b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1518b24ab676SJeff Bonwick 	if (txg == 0) {
1519b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1520b24ab676SJeff Bonwick 		return (ENOSPC);
1521b24ab676SJeff Bonwick 	}
1522b24ab676SJeff Bonwick 
1523b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1524b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1525b24ab676SJeff Bonwick 	} else {
1526b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1527b24ab676SJeff Bonwick 	}
1528b24ab676SJeff Bonwick 
1529b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1530b24ab676SJeff Bonwick 
15315002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1532b24ab676SJeff Bonwick 
1533b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1534b24ab676SJeff Bonwick 
1535b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1536b24ab676SJeff Bonwick 
1537b24ab676SJeff Bonwick 	return (0);
1538b24ab676SJeff Bonwick }
1539b24ab676SJeff Bonwick 
1540b24ab676SJeff Bonwick static int
1541b24ab676SJeff Bonwick ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1542b24ab676SJeff Bonwick {
1543b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1544b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1545b24ab676SJeff Bonwick 	uint64_t offset, length;
1546b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1547b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1548b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1549b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1550b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1551b24ab676SJeff Bonwick 	dmu_buf_t *db;
1552b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1553b24ab676SJeff Bonwick 	rl_t *rl;
1554b24ab676SJeff Bonwick 
1555b24ab676SJeff Bonwick 	if (byteswap)
1556b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1557b24ab676SJeff Bonwick 
1558b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1559b24ab676SJeff Bonwick 	length = lr->lr_length;
1560b24ab676SJeff Bonwick 
1561b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1562b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1563b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1564b24ab676SJeff Bonwick 		if (length < blocksize) {
1565b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1566b24ab676SJeff Bonwick 			length = blocksize;
1567b24ab676SJeff Bonwick 		}
1568b24ab676SJeff Bonwick 	}
1569b24ab676SJeff Bonwick 
1570b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1571b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1572b24ab676SJeff Bonwick 
1573b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1574b24ab676SJeff Bonwick 		bt = NULL;
1575b24ab676SJeff Bonwick 
1576b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1577b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1578b24ab676SJeff Bonwick 
1579b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1580b24ab676SJeff Bonwick 
1581b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1582b24ab676SJeff Bonwick 
1583b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1584b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1585b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1586b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1587b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1588b24ab676SJeff Bonwick 
1589b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1590b24ab676SJeff Bonwick 
1591b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1592b24ab676SJeff Bonwick 
1593b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1594b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1595b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1596b24ab676SJeff Bonwick 
1597b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1598b24ab676SJeff Bonwick 	if (txg == 0) {
1599b24ab676SJeff Bonwick 		if (abuf != NULL)
1600b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1601b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1602b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1603b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1604b24ab676SJeff Bonwick 		return (ENOSPC);
1605b24ab676SJeff Bonwick 	}
1606b24ab676SJeff Bonwick 
1607b24ab676SJeff Bonwick 	if (bt != NULL) {
1608b24ab676SJeff Bonwick 		/*
1609b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1610b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1611b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1612b24ab676SJeff Bonwick 		 */
1613b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1614b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1615b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1616b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1617b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1618b24ab676SJeff Bonwick 
1619b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1620b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1621b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
1622b24ab676SJeff Bonwick 				ztest_bt_verify(&rbt, os, lr->lr_foid,
1623b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1624b24ab676SJeff Bonwick 			}
1625b24ab676SJeff Bonwick 		}
1626b24ab676SJeff Bonwick 
1627b24ab676SJeff Bonwick 		/*
1628b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1629b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1630b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1631b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1632b24ab676SJeff Bonwick 		 */
1633b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
1634b24ab676SJeff Bonwick 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
1635b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1636b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1637b24ab676SJeff Bonwick 		}
1638b24ab676SJeff Bonwick 
1639b24ab676SJeff Bonwick 		/*
1640b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1641b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1642b24ab676SJeff Bonwick 		 */
1643b24ab676SJeff Bonwick 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1644b24ab676SJeff Bonwick 	}
1645b24ab676SJeff Bonwick 
1646b24ab676SJeff Bonwick 	if (abuf == NULL) {
1647b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1648b24ab676SJeff Bonwick 	} else {
1649b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1650b24ab676SJeff Bonwick 		dmu_assign_arcbuf(db, offset, abuf, tx);
1651b24ab676SJeff Bonwick 	}
1652b24ab676SJeff Bonwick 
1653b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1654b24ab676SJeff Bonwick 
1655b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1656b24ab676SJeff Bonwick 
1657b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1658b24ab676SJeff Bonwick 
1659b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1660b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1661b24ab676SJeff Bonwick 
1662b24ab676SJeff Bonwick 	return (0);
1663b24ab676SJeff Bonwick }
1664b24ab676SJeff Bonwick 
1665b24ab676SJeff Bonwick static int
1666b24ab676SJeff Bonwick ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1667b24ab676SJeff Bonwick {
1668b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1669b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1670b24ab676SJeff Bonwick 	uint64_t txg;
1671b24ab676SJeff Bonwick 	rl_t *rl;
1672b24ab676SJeff Bonwick 
1673b24ab676SJeff Bonwick 	if (byteswap)
1674b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1675b24ab676SJeff Bonwick 
1676b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1677b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1678b24ab676SJeff Bonwick 	    RL_WRITER);
1679b24ab676SJeff Bonwick 
1680b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1681b24ab676SJeff Bonwick 
1682b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1683b24ab676SJeff Bonwick 
1684b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1685b24ab676SJeff Bonwick 	if (txg == 0) {
1686b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1687b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1688b24ab676SJeff Bonwick 		return (ENOSPC);
1689b24ab676SJeff Bonwick 	}
1690b24ab676SJeff Bonwick 
1691b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1692b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1693b24ab676SJeff Bonwick 
1694b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1695b24ab676SJeff Bonwick 
1696b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1697b24ab676SJeff Bonwick 
1698b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1699b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1700b24ab676SJeff Bonwick 
1701b24ab676SJeff Bonwick 	return (0);
1702b24ab676SJeff Bonwick }
1703b24ab676SJeff Bonwick 
1704b24ab676SJeff Bonwick static int
1705b24ab676SJeff Bonwick ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1706b24ab676SJeff Bonwick {
1707b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1708b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1709b24ab676SJeff Bonwick 	dmu_buf_t *db;
1710b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1711b24ab676SJeff Bonwick 	uint64_t txg, lrtxg, crtxg;
1712b24ab676SJeff Bonwick 
1713b24ab676SJeff Bonwick 	if (byteswap)
1714b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1715b24ab676SJeff Bonwick 
1716b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1717b24ab676SJeff Bonwick 
1718b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1719b24ab676SJeff Bonwick 
1720b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1721b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1722b24ab676SJeff Bonwick 
1723b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1724b24ab676SJeff Bonwick 	if (txg == 0) {
1725b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1726b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1727b24ab676SJeff Bonwick 		return (ENOSPC);
1728b24ab676SJeff Bonwick 	}
1729b24ab676SJeff Bonwick 
1730b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1731b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1732b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1733b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1734b24ab676SJeff Bonwick 
1735b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1736b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1737b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1738b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1739b24ab676SJeff Bonwick 	} else {
1740b24ab676SJeff Bonwick 		/*
1741b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1742b24ab676SJeff Bonwick 		 */
1743b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1744b24ab676SJeff Bonwick 		    sizeof (*bbt);
1745b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
1746b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
1747b24ab676SJeff Bonwick 	}
1748b24ab676SJeff Bonwick 
1749b24ab676SJeff Bonwick 	/*
1750b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
1751b24ab676SJeff Bonwick 	 */
1752b24ab676SJeff Bonwick 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1753b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
1754b24ab676SJeff Bonwick 
1755b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1756b24ab676SJeff Bonwick 
1757b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1758b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
1759fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1760b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1761b24ab676SJeff Bonwick 
1762b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1763b24ab676SJeff Bonwick 
1764b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1765b24ab676SJeff Bonwick 
1766b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
1767b24ab676SJeff Bonwick 
1768b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1769b24ab676SJeff Bonwick 
1770b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1771b24ab676SJeff Bonwick 
1772b24ab676SJeff Bonwick 	return (0);
1773b24ab676SJeff Bonwick }
1774b24ab676SJeff Bonwick 
1775b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1776b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
1777b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
1778b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
1779b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
1780b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
1781b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
1782b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
1783b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
1784b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
1785b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
1786b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
1787b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
1788b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
1789b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
1790b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
1791b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
1792b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
1793b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
1794b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
1795b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
1796b24ab676SJeff Bonwick };
1797b24ab676SJeff Bonwick 
1798b24ab676SJeff Bonwick /*
1799b24ab676SJeff Bonwick  * ZIL get_data callbacks
1800b24ab676SJeff Bonwick  */
1801b24ab676SJeff Bonwick 
1802b24ab676SJeff Bonwick static void
1803b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
1804b24ab676SJeff Bonwick {
1805b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
1806b24ab676SJeff Bonwick 	uint64_t object = zgd->zgd_rl->rl_object;
1807b24ab676SJeff Bonwick 
1808b24ab676SJeff Bonwick 	if (zgd->zgd_db)
1809b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
1810b24ab676SJeff Bonwick 
1811b24ab676SJeff Bonwick 	ztest_range_unlock(zgd->zgd_rl);
1812b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1813b24ab676SJeff Bonwick 
1814b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
1815b24ab676SJeff Bonwick 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1816b24ab676SJeff Bonwick 
1817b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
1818b24ab676SJeff Bonwick }
1819b24ab676SJeff Bonwick 
1820b24ab676SJeff Bonwick static int
1821b24ab676SJeff Bonwick ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1822b24ab676SJeff Bonwick {
1823b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
1824b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1825b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
1826b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
1827b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
1828b24ab676SJeff Bonwick 	blkptr_t *bp = &lr->lr_blkptr;
1829b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
1830b24ab676SJeff Bonwick 	uint64_t crtxg;
1831b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1832b24ab676SJeff Bonwick 	dmu_buf_t *db;
1833b24ab676SJeff Bonwick 	zgd_t *zgd;
1834b24ab676SJeff Bonwick 	int error;
1835b24ab676SJeff Bonwick 
1836b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
1837b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
1838b24ab676SJeff Bonwick 	if (error) {
1839b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1840b24ab676SJeff Bonwick 		return (error);
1841b24ab676SJeff Bonwick 	}
1842b24ab676SJeff Bonwick 
1843b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
1844b24ab676SJeff Bonwick 
1845b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
1846b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1847b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1848b24ab676SJeff Bonwick 		return (ENOENT);
1849b24ab676SJeff Bonwick 	}
1850b24ab676SJeff Bonwick 
1851b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1852b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1853b24ab676SJeff Bonwick 	db = NULL;
1854b24ab676SJeff Bonwick 
1855b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1856b24ab676SJeff Bonwick 	zgd->zgd_zilog = zd->zd_zilog;
1857b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
1858b24ab676SJeff Bonwick 
1859b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
1860b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1861b24ab676SJeff Bonwick 		    RL_READER);
1862b24ab676SJeff Bonwick 
1863b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
1864b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1865b24ab676SJeff Bonwick 		ASSERT(error == 0);
1866b24ab676SJeff Bonwick 	} else {
1867b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
1868dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
1869b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
1870dfe73b3dSJeff Bonwick 		} else {
1871dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
1872dfe73b3dSJeff Bonwick 			offset = 0;
1873dfe73b3dSJeff Bonwick 		}
1874b24ab676SJeff Bonwick 
1875b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1876b24ab676SJeff Bonwick 		    RL_READER);
1877b24ab676SJeff Bonwick 
187847cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
187947cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1880b24ab676SJeff Bonwick 
1881b24ab676SJeff Bonwick 		if (error == 0) {
188280901aeaSGeorge Wilson 			blkptr_t *obp = dmu_buf_get_blkptr(db);
188380901aeaSGeorge Wilson 			if (obp) {
188480901aeaSGeorge Wilson 				ASSERT(BP_IS_HOLE(bp));
188580901aeaSGeorge Wilson 				*bp = *obp;
188680901aeaSGeorge Wilson 			}
188780901aeaSGeorge Wilson 
1888b24ab676SJeff Bonwick 			zgd->zgd_db = db;
1889b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
1890b24ab676SJeff Bonwick 
1891b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
1892b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
1893b24ab676SJeff Bonwick 
1894b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1895b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
1896b24ab676SJeff Bonwick 
1897b24ab676SJeff Bonwick 			if (error == 0)
1898b24ab676SJeff Bonwick 				return (0);
1899b24ab676SJeff Bonwick 		}
1900b24ab676SJeff Bonwick 	}
1901b24ab676SJeff Bonwick 
1902b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
1903b24ab676SJeff Bonwick 
1904b24ab676SJeff Bonwick 	return (error);
1905b24ab676SJeff Bonwick }
1906b24ab676SJeff Bonwick 
1907b24ab676SJeff Bonwick static void *
1908b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
1909b24ab676SJeff Bonwick {
1910b24ab676SJeff Bonwick 	char *lr;
1911b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1912b24ab676SJeff Bonwick 
1913b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1914b24ab676SJeff Bonwick 
1915b24ab676SJeff Bonwick 	if (name)
1916b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
1917b24ab676SJeff Bonwick 
1918b24ab676SJeff Bonwick 	return (lr);
1919b24ab676SJeff Bonwick }
1920b24ab676SJeff Bonwick 
1921b24ab676SJeff Bonwick void
1922b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
1923b24ab676SJeff Bonwick {
1924b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1925b24ab676SJeff Bonwick 
1926b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
1927b24ab676SJeff Bonwick }
1928b24ab676SJeff Bonwick 
1929b24ab676SJeff Bonwick /*
1930b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
1931b24ab676SJeff Bonwick  */
1932b24ab676SJeff Bonwick static int
1933b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1934b24ab676SJeff Bonwick {
1935b24ab676SJeff Bonwick 	int missing = 0;
1936b24ab676SJeff Bonwick 	int error;
1937b24ab676SJeff Bonwick 
1938b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1939b24ab676SJeff Bonwick 
1940b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
1941b24ab676SJeff Bonwick 		od->od_object = 0;
1942b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1943b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
1944b24ab676SJeff Bonwick 		if (error) {
1945b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
1946b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
1947b24ab676SJeff Bonwick 			missing++;
1948b24ab676SJeff Bonwick 		} else {
1949b24ab676SJeff Bonwick 			dmu_buf_t *db;
1950b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
1951b24ab676SJeff Bonwick 			dmu_object_info_t doi;
1952b24ab676SJeff Bonwick 
1953b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
1954b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
1955b24ab676SJeff Bonwick 
1956b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
1957b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1958b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
1959b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
1960b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
1961b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1962b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
1963b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
1964b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
1965b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
1966b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
1967b24ab676SJeff Bonwick 		}
1968b24ab676SJeff Bonwick 	}
1969b24ab676SJeff Bonwick 
1970b24ab676SJeff Bonwick 	return (missing);
1971b24ab676SJeff Bonwick }
1972b24ab676SJeff Bonwick 
1973b24ab676SJeff Bonwick static int
1974b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1975b24ab676SJeff Bonwick {
1976b24ab676SJeff Bonwick 	int missing = 0;
1977b24ab676SJeff Bonwick 
1978b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1979b24ab676SJeff Bonwick 
1980b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
1981b24ab676SJeff Bonwick 		if (missing) {
1982b24ab676SJeff Bonwick 			od->od_object = 0;
1983b24ab676SJeff Bonwick 			missing++;
1984b24ab676SJeff Bonwick 			continue;
1985b24ab676SJeff Bonwick 		}
1986b24ab676SJeff Bonwick 
1987b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1988b24ab676SJeff Bonwick 
1989b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
1990b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
1991b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
1992b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
1993b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
1994b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1995b24ab676SJeff Bonwick 		lr->lrz_bonuslen = dmu_bonus_max();
1996b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
1997b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
1998b24ab676SJeff Bonwick 
1999b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2000b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2001b24ab676SJeff Bonwick 			od->od_object = 0;
2002b24ab676SJeff Bonwick 			missing++;
2003b24ab676SJeff Bonwick 		} else {
2004b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2005b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2006b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2007b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2008b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2009b24ab676SJeff Bonwick 		}
2010b24ab676SJeff Bonwick 
2011b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2012b24ab676SJeff Bonwick 	}
2013b24ab676SJeff Bonwick 
2014b24ab676SJeff Bonwick 	return (missing);
2015b24ab676SJeff Bonwick }
2016b24ab676SJeff Bonwick 
2017b24ab676SJeff Bonwick static int
2018b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2019b24ab676SJeff Bonwick {
2020b24ab676SJeff Bonwick 	int missing = 0;
2021b24ab676SJeff Bonwick 	int error;
2022b24ab676SJeff Bonwick 
2023b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
2024b24ab676SJeff Bonwick 
2025b24ab676SJeff Bonwick 	od += count - 1;
2026b24ab676SJeff Bonwick 
2027b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2028b24ab676SJeff Bonwick 		if (missing) {
2029b24ab676SJeff Bonwick 			missing++;
2030b24ab676SJeff Bonwick 			continue;
2031b24ab676SJeff Bonwick 		}
2032b24ab676SJeff Bonwick 
203380901aeaSGeorge Wilson 		/*
203480901aeaSGeorge Wilson 		 * No object was found.
203580901aeaSGeorge Wilson 		 */
2036b24ab676SJeff Bonwick 		if (od->od_object == 0)
2037b24ab676SJeff Bonwick 			continue;
2038b24ab676SJeff Bonwick 
2039b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2040b24ab676SJeff Bonwick 
2041b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2042b24ab676SJeff Bonwick 
2043b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2044b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2045b24ab676SJeff Bonwick 			missing++;
2046b24ab676SJeff Bonwick 		} else {
2047b24ab676SJeff Bonwick 			od->od_object = 0;
2048b24ab676SJeff Bonwick 		}
2049b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2050b24ab676SJeff Bonwick 	}
2051b24ab676SJeff Bonwick 
2052b24ab676SJeff Bonwick 	return (missing);
2053b24ab676SJeff Bonwick }
2054b24ab676SJeff Bonwick 
2055b24ab676SJeff Bonwick static int
2056b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2057b24ab676SJeff Bonwick     void *data)
2058b24ab676SJeff Bonwick {
2059b24ab676SJeff Bonwick 	lr_write_t *lr;
2060b24ab676SJeff Bonwick 	int error;
2061b24ab676SJeff Bonwick 
2062b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2063b24ab676SJeff Bonwick 
2064b24ab676SJeff Bonwick 	lr->lr_foid = object;
2065b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2066b24ab676SJeff Bonwick 	lr->lr_length = size;
2067b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2068b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2069b24ab676SJeff Bonwick 
2070b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2071b24ab676SJeff Bonwick 
2072b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2073b24ab676SJeff Bonwick 
2074b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2075b24ab676SJeff Bonwick 
2076b24ab676SJeff Bonwick 	return (error);
2077b24ab676SJeff Bonwick }
2078b24ab676SJeff Bonwick 
2079b24ab676SJeff Bonwick static int
2080b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2081b24ab676SJeff Bonwick {
2082b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2083b24ab676SJeff Bonwick 	int error;
2084b24ab676SJeff Bonwick 
2085b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2086b24ab676SJeff Bonwick 
2087b24ab676SJeff Bonwick 	lr->lr_foid = object;
2088b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2089b24ab676SJeff Bonwick 	lr->lr_length = size;
2090b24ab676SJeff Bonwick 
2091b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2092b24ab676SJeff Bonwick 
2093b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2094b24ab676SJeff Bonwick 
2095b24ab676SJeff Bonwick 	return (error);
2096b24ab676SJeff Bonwick }
2097b24ab676SJeff Bonwick 
2098b24ab676SJeff Bonwick static int
2099b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2100b24ab676SJeff Bonwick {
2101b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2102b24ab676SJeff Bonwick 	int error;
2103b24ab676SJeff Bonwick 
2104b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2105b24ab676SJeff Bonwick 
2106b24ab676SJeff Bonwick 	lr->lr_foid = object;
2107b24ab676SJeff Bonwick 	lr->lr_size = 0;
2108b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2109b24ab676SJeff Bonwick 
2110b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2111b24ab676SJeff Bonwick 
2112b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2113b24ab676SJeff Bonwick 
2114b24ab676SJeff Bonwick 	return (error);
2115b24ab676SJeff Bonwick }
2116b24ab676SJeff Bonwick 
2117b24ab676SJeff Bonwick static void
2118b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2119b24ab676SJeff Bonwick {
2120b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2121b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2122b24ab676SJeff Bonwick 	uint64_t txg;
2123b24ab676SJeff Bonwick 	rl_t *rl;
2124b24ab676SJeff Bonwick 
2125b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2126b24ab676SJeff Bonwick 
2127b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2128b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2129b24ab676SJeff Bonwick 
2130b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2131b24ab676SJeff Bonwick 
2132b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2133b24ab676SJeff Bonwick 
2134b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2135b24ab676SJeff Bonwick 
2136b24ab676SJeff Bonwick 	if (txg != 0) {
2137b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2138b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2139b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2140b24ab676SJeff Bonwick 	} else {
2141b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2142b24ab676SJeff Bonwick 	}
2143b24ab676SJeff Bonwick 
2144b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2145b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2146b24ab676SJeff Bonwick }
2147b24ab676SJeff Bonwick 
2148b24ab676SJeff Bonwick static void
2149b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2150b24ab676SJeff Bonwick {
215180901aeaSGeorge Wilson 	int err;
2152b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2153b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2154b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2155b24ab676SJeff Bonwick 	uint64_t blocksize;
2156b24ab676SJeff Bonwick 	void *data;
2157b24ab676SJeff Bonwick 
2158b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2159b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2160b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2161b24ab676SJeff Bonwick 
2162b24ab676SJeff Bonwick 	/*
2163b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2164b24ab676SJeff Bonwick 	 */
2165b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2166b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2167b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2168b24ab676SJeff Bonwick 
2169c9ba2a43SEric Schrock 	(void) rw_rdlock(&zd->zd_zilog_lock);
2170c9ba2a43SEric Schrock 
2171b24ab676SJeff Bonwick 	switch (io_type) {
2172b24ab676SJeff Bonwick 
2173b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
2174b24ab676SJeff Bonwick 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2175b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2176b24ab676SJeff Bonwick 		break;
2177b24ab676SJeff Bonwick 
2178b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2179b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2180b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2181b24ab676SJeff Bonwick 			/*
2182b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2183b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2184b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2185b24ab676SJeff Bonwick 			 */
2186b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2187b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2188b24ab676SJeff Bonwick 		}
2189b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2190b24ab676SJeff Bonwick 		break;
2191b24ab676SJeff Bonwick 
2192b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2193b24ab676SJeff Bonwick 		bzero(data, blocksize);
2194b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2195b24ab676SJeff Bonwick 		break;
2196b24ab676SJeff Bonwick 
2197b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2198b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2199b24ab676SJeff Bonwick 		break;
2200b24ab676SJeff Bonwick 
2201b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2202b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2203b24ab676SJeff Bonwick 		break;
220480901aeaSGeorge Wilson 
220580901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
220680901aeaSGeorge Wilson 		(void) rw_rdlock(&ztest_name_lock);
220780901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
220880901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
220980901aeaSGeorge Wilson 		    B_FALSE);
221080901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
221180901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
221280901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
221380901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
221480901aeaSGeorge Wilson 		    B_FALSE);
221580901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
221680901aeaSGeorge Wilson 		(void) rw_unlock(&ztest_name_lock);
221780901aeaSGeorge Wilson 
221880901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
221980901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
222080901aeaSGeorge Wilson 
222180901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
222280901aeaSGeorge Wilson 		break;
2223b24ab676SJeff Bonwick 	}
2224b24ab676SJeff Bonwick 
2225c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2226c9ba2a43SEric Schrock 
2227b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2228b24ab676SJeff Bonwick }
2229b24ab676SJeff Bonwick 
2230b24ab676SJeff Bonwick /*
2231b24ab676SJeff Bonwick  * Initialize an object description template.
2232b24ab676SJeff Bonwick  */
2233b24ab676SJeff Bonwick static void
2234b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2235b24ab676SJeff Bonwick     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2236b24ab676SJeff Bonwick {
2237b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2238b24ab676SJeff Bonwick 	od->od_object = 0;
2239b24ab676SJeff Bonwick 
2240b24ab676SJeff Bonwick 	od->od_crtype = type;
2241b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2242b24ab676SJeff Bonwick 	od->od_crgen = gen;
2243b24ab676SJeff Bonwick 
2244b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2245b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2246b24ab676SJeff Bonwick 	od->od_gen = 0;
2247b24ab676SJeff Bonwick 
2248b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2249b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2250b24ab676SJeff Bonwick }
2251b24ab676SJeff Bonwick 
2252b24ab676SJeff Bonwick /*
2253b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2254b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2255b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2256b24ab676SJeff Bonwick  * use the existing objects.
2257b24ab676SJeff Bonwick  */
2258b24ab676SJeff Bonwick static int
2259b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2260b24ab676SJeff Bonwick {
2261b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2262b24ab676SJeff Bonwick 	int rv = 0;
2263b24ab676SJeff Bonwick 
2264b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2265b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2266b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2267b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2268b24ab676SJeff Bonwick 		rv = -1;
2269b24ab676SJeff Bonwick 	zd->zd_od = od;
2270b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2271b24ab676SJeff Bonwick 
2272b24ab676SJeff Bonwick 	return (rv);
2273b24ab676SJeff Bonwick }
2274b24ab676SJeff Bonwick 
2275b24ab676SJeff Bonwick /* ARGSUSED */
2276b24ab676SJeff Bonwick void
2277b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2278b24ab676SJeff Bonwick {
2279b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2280b24ab676SJeff Bonwick 
2281c9ba2a43SEric Schrock 	(void) rw_rdlock(&zd->zd_zilog_lock);
2282c9ba2a43SEric Schrock 
22835002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2284b24ab676SJeff Bonwick 
2285b24ab676SJeff Bonwick 	/*
2286b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2287b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2288b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2289b24ab676SJeff Bonwick 	 */
2290b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2291420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2292420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2293420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2294b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2295c9ba2a43SEric Schrock 
2296c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2297c9ba2a43SEric Schrock }
2298c9ba2a43SEric Schrock 
2299c9ba2a43SEric Schrock /*
2300c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2301c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2302c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2303c9ba2a43SEric Schrock  */
2304c9ba2a43SEric Schrock /* ARGSUSED */
2305c9ba2a43SEric Schrock void
2306c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2307c9ba2a43SEric Schrock {
2308c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2309c9ba2a43SEric Schrock 
231080901aeaSGeorge Wilson 	/*
231180901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
231280901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
231380901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
231480901aeaSGeorge Wilson 	 */
231580901aeaSGeorge Wilson 	VERIFY0(mutex_lock(&zd->zd_dirobj_lock));
2316c9ba2a43SEric Schrock 	(void) rw_wrlock(&zd->zd_zilog_lock);
2317c9ba2a43SEric Schrock 
2318c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2319c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2320c9ba2a43SEric Schrock 
2321c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2322c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2323c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2324c9ba2a43SEric Schrock 
2325c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2326ce636f8bSMatthew Ahrens 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2327b24ab676SJeff Bonwick }
2328b24ab676SJeff Bonwick 
2329b24ab676SJeff Bonwick /*
2330b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2331b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2332b24ab676SJeff Bonwick  */
2333b24ab676SJeff Bonwick /* ARGSUSED */
2334b24ab676SJeff Bonwick void
2335b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2336b24ab676SJeff Bonwick {
2337420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2338b24ab676SJeff Bonwick 	spa_t *spa;
2339b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2340b24ab676SJeff Bonwick 
2341b24ab676SJeff Bonwick 	/*
2342b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2343b24ab676SJeff Bonwick 	 */
234425345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2345b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23464445fffbSMatthew Ahrens 	    spa_create("ztest_bad_file", nvroot, NULL, NULL));
2347b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2348b24ab676SJeff Bonwick 
2349b24ab676SJeff Bonwick 	/*
2350b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2351b24ab676SJeff Bonwick 	 */
235225345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2353b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23544445fffbSMatthew Ahrens 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2355b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2356b24ab676SJeff Bonwick 
2357b24ab676SJeff Bonwick 	/*
2358b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2359b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2360b24ab676SJeff Bonwick 	 */
2361420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
236225345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
23634445fffbSMatthew Ahrens 	VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2364b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2365b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2366420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2367b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2368b24ab676SJeff Bonwick 
2369420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
2370b24ab676SJeff Bonwick }
2371b24ab676SJeff Bonwick 
237225345e46SGeorge Wilson /* ARGSUSED */
237325345e46SGeorge Wilson void
237425345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
237525345e46SGeorge Wilson {
237625345e46SGeorge Wilson 	spa_t *spa;
237725345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
237825345e46SGeorge Wilson 	uint64_t version, newversion;
237925345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
238025345e46SGeorge Wilson 	char *name;
238125345e46SGeorge Wilson 
238225345e46SGeorge Wilson 	VERIFY0(mutex_lock(&ztest_vdev_lock));
238325345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
238425345e46SGeorge Wilson 
238525345e46SGeorge Wilson 	/*
238625345e46SGeorge Wilson 	 * Clean up from previous runs.
238725345e46SGeorge Wilson 	 */
238825345e46SGeorge Wilson 	(void) spa_destroy(name);
238925345e46SGeorge Wilson 
239025345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
239125345e46SGeorge Wilson 	    0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
239225345e46SGeorge Wilson 
239325345e46SGeorge Wilson 	/*
239425345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
239525345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
239625345e46SGeorge Wilson 	 */
239725345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
239825345e46SGeorge Wilson 	case 0:
239925345e46SGeorge Wilson 	case 1:
240025345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
240125345e46SGeorge Wilson 		break;
240225345e46SGeorge Wilson 	case 2:
240325345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
240425345e46SGeorge Wilson 		break;
240525345e46SGeorge Wilson 	case 3:
240625345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
240725345e46SGeorge Wilson 		break;
240825345e46SGeorge Wilson 	}
240925345e46SGeorge Wilson 
241025345e46SGeorge Wilson 	/*
241125345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
241225345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
241325345e46SGeorge Wilson 	 */
241425345e46SGeorge Wilson 	do {
241525345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
241625345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
241725345e46SGeorge Wilson 
241825345e46SGeorge Wilson 	props = fnvlist_alloc();
241925345e46SGeorge Wilson 	fnvlist_add_uint64(props,
242025345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
242125345e46SGeorge Wilson 	VERIFY0(spa_create(name, nvroot, props, NULL));
242225345e46SGeorge Wilson 	fnvlist_free(nvroot);
242325345e46SGeorge Wilson 	fnvlist_free(props);
242425345e46SGeorge Wilson 
242525345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
242625345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
242725345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
242825345e46SGeorge Wilson 
242925345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
243025345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
243125345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
243225345e46SGeorge Wilson 	}
243325345e46SGeorge Wilson 
243425345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
243525345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
243625345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
243725345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
243825345e46SGeorge Wilson 	spa_close(spa, FTAG);
243925345e46SGeorge Wilson 
244025345e46SGeorge Wilson 	strfree(name);
244125345e46SGeorge Wilson 	VERIFY0(mutex_unlock(&ztest_vdev_lock));
244225345e46SGeorge Wilson }
244325345e46SGeorge Wilson 
2444b24ab676SJeff Bonwick static vdev_t *
2445b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2446b24ab676SJeff Bonwick {
2447b24ab676SJeff Bonwick 	vdev_t *mvd;
2448b24ab676SJeff Bonwick 
2449b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2450b24ab676SJeff Bonwick 		return (vd);
2451b24ab676SJeff Bonwick 
2452b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2453b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2454b24ab676SJeff Bonwick 		    NULL)
2455b24ab676SJeff Bonwick 			return (mvd);
2456b24ab676SJeff Bonwick 
2457b24ab676SJeff Bonwick 	return (NULL);
2458b24ab676SJeff Bonwick }
2459b24ab676SJeff Bonwick 
2460b24ab676SJeff Bonwick /*
2461b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2462b24ab676SJeff Bonwick  */
2463b24ab676SJeff Bonwick int
2464b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2465b24ab676SJeff Bonwick {
2466b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2467b24ab676SJeff Bonwick 	int c;
2468b24ab676SJeff Bonwick 
2469b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2470b24ab676SJeff Bonwick 
2471b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2472b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2473b24ab676SJeff Bonwick 
2474b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2475b24ab676SJeff Bonwick 			break;
2476b24ab676SJeff Bonwick 	}
2477b24ab676SJeff Bonwick 	return (c);
2478b24ab676SJeff Bonwick }
2479b24ab676SJeff Bonwick 
2480b24ab676SJeff Bonwick /*
2481b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2482b24ab676SJeff Bonwick  */
2483b24ab676SJeff Bonwick /* ARGSUSED */
2484b24ab676SJeff Bonwick void
2485b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2486b24ab676SJeff Bonwick {
2487b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2488420dfc95SChris Siden 	spa_t *spa = ztest_spa;
24891195e687SMark J Musante 	uint64_t leaves;
2490b24ab676SJeff Bonwick 	uint64_t guid;
2491b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2492b24ab676SJeff Bonwick 	int error;
2493b24ab676SJeff Bonwick 
2494420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
24953b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2496b24ab676SJeff Bonwick 
2497b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2498b24ab676SJeff Bonwick 
2499b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2500b24ab676SJeff Bonwick 
2501b24ab676SJeff Bonwick 	/*
2502b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2503b24ab676SJeff Bonwick 	 */
250488ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
250588ecc943SGeorge Wilson 		/*
250688ecc943SGeorge Wilson 		 * Grab the guid from the head of the log class rotor.
250788ecc943SGeorge Wilson 		 */
2508b24ab676SJeff Bonwick 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
25098654d025Sperrin 
251088ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2511fa9e4066Sahrens 
251288ecc943SGeorge Wilson 		/*
251388ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
251488ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
251588ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
251688ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
251788ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
251888ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
251988ecc943SGeorge Wilson 		 */
2520420dfc95SChris Siden 		VERIFY(rw_wrlock(&ztest_name_lock) == 0);
252188ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2522420dfc95SChris Siden 		VERIFY(rw_unlock(&ztest_name_lock) == 0);
2523fa9e4066Sahrens 
252488ecc943SGeorge Wilson 		if (error && error != EEXIST)
252588ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
252688ecc943SGeorge Wilson 	} else {
252788ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
252888ecc943SGeorge Wilson 
252988ecc943SGeorge Wilson 		/*
253088ecc943SGeorge Wilson 		 * Make 1/4 of the devices be log devices.
253188ecc943SGeorge Wilson 		 */
253225345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2533420dfc95SChris Siden 		    ztest_opts.zo_vdev_size, 0,
2534420dfc95SChris Siden 		    ztest_random(4) == 0, ztest_opts.zo_raidz,
2535420dfc95SChris Siden 		    zs->zs_mirrors, 1);
253688ecc943SGeorge Wilson 
253788ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
253888ecc943SGeorge Wilson 		nvlist_free(nvroot);
253988ecc943SGeorge Wilson 
254088ecc943SGeorge Wilson 		if (error == ENOSPC)
254188ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
254288ecc943SGeorge Wilson 		else if (error != 0)
254388ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
254488ecc943SGeorge Wilson 	}
254588ecc943SGeorge Wilson 
2546420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2547e14bb325SJeff Bonwick }
2548fa9e4066Sahrens 
2549e14bb325SJeff Bonwick /*
2550e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2551e14bb325SJeff Bonwick  */
2552b24ab676SJeff Bonwick /* ARGSUSED */
2553e14bb325SJeff Bonwick void
2554b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2555e14bb325SJeff Bonwick {
2556b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2557420dfc95SChris Siden 	spa_t *spa = ztest_spa;
255831157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2559e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
2560e14bb325SJeff Bonwick 	char *aux;
2561e14bb325SJeff Bonwick 	uint64_t guid = 0;
2562e14bb325SJeff Bonwick 	int error;
2563e14bb325SJeff Bonwick 
256431157203SJeff Bonwick 	if (ztest_random(2) == 0) {
2565e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
2566e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
2567e14bb325SJeff Bonwick 	} else {
2568e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
2569e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
2570e14bb325SJeff Bonwick 	}
2571e14bb325SJeff Bonwick 
2572420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
2573e14bb325SJeff Bonwick 
2574e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2575e14bb325SJeff Bonwick 
2576e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
2577e14bb325SJeff Bonwick 		/*
2578e14bb325SJeff Bonwick 		 * Pick a random device to remove.
2579e14bb325SJeff Bonwick 		 */
2580e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2581e14bb325SJeff Bonwick 	} else {
2582e14bb325SJeff Bonwick 		/*
2583e14bb325SJeff Bonwick 		 * Find an unused device we can add.
2584e14bb325SJeff Bonwick 		 */
2585b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
2586e14bb325SJeff Bonwick 		for (;;) {
2587e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
2588e14bb325SJeff Bonwick 			int c;
2589420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
2590420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2591420dfc95SChris Siden 			    zs->zs_vdev_aux);
2592e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
2593e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
2594e14bb325SJeff Bonwick 				    path) == 0)
2595e14bb325SJeff Bonwick 					break;
259631157203SJeff Bonwick 			if (c == sav->sav_count &&
259731157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
2598e14bb325SJeff Bonwick 				break;
2599b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
2600e14bb325SJeff Bonwick 		}
2601e14bb325SJeff Bonwick 	}
2602e14bb325SJeff Bonwick 
2603e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
2604e14bb325SJeff Bonwick 
2605e14bb325SJeff Bonwick 	if (guid == 0) {
2606e14bb325SJeff Bonwick 		/*
2607e14bb325SJeff Bonwick 		 * Add a new device.
2608e14bb325SJeff Bonwick 		 */
260925345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2610420dfc95SChris Siden 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2611e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
2612e14bb325SJeff Bonwick 		if (error != 0)
2613e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2614e14bb325SJeff Bonwick 		nvlist_free(nvroot);
2615e14bb325SJeff Bonwick 	} else {
2616e14bb325SJeff Bonwick 		/*
2617e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
2618e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
2619e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
2620e14bb325SJeff Bonwick 		 */
2621e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
2622573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
2623e14bb325SJeff Bonwick 
2624e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
2625e14bb325SJeff Bonwick 		if (error != 0 && error != EBUSY)
2626e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2627e14bb325SJeff Bonwick 	}
2628e14bb325SJeff Bonwick 
2629420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2630fa9e4066Sahrens }
2631fa9e4066Sahrens 
26321195e687SMark J Musante /*
26331195e687SMark J Musante  * split a pool if it has mirror tlvdevs
26341195e687SMark J Musante  */
26351195e687SMark J Musante /* ARGSUSED */
26361195e687SMark J Musante void
26371195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
26381195e687SMark J Musante {
26391195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
2640420dfc95SChris Siden 	spa_t *spa = ztest_spa;
26411195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
26421195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
26431195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
26441195e687SMark J Musante 	int error = 0;
26451195e687SMark J Musante 
2646420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
26471195e687SMark J Musante 
26481195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
2649420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2650420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
26511195e687SMark J Musante 		return;
26521195e687SMark J Musante 	}
26531195e687SMark J Musante 
26541195e687SMark J Musante 	/* clean up the old pool, if any */
26551195e687SMark J Musante 	(void) spa_destroy("splitp");
26561195e687SMark J Musante 
26571195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
26581195e687SMark J Musante 
26591195e687SMark J Musante 	/* generate a config from the existing config */
266098295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
26611195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
26621195e687SMark J Musante 	    &tree) == 0);
266398295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
266498295d61SMark J Musante 
26651195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
26661195e687SMark J Musante 	    &children) == 0);
26671195e687SMark J Musante 
26681195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
26691195e687SMark J Musante 	for (c = 0; c < children; c++) {
26701195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
26711195e687SMark J Musante 		nvlist_t **mchild;
26721195e687SMark J Musante 		uint_t mchildren;
26731195e687SMark J Musante 
26741195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
26751195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
26761195e687SMark J Musante 			    0) == 0);
26771195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
26781195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
26791195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
26801195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
26811195e687SMark J Musante 			if (lastlogid == 0)
26821195e687SMark J Musante 				lastlogid = schildren;
26831195e687SMark J Musante 			++schildren;
26841195e687SMark J Musante 			continue;
26851195e687SMark J Musante 		}
26861195e687SMark J Musante 		lastlogid = 0;
26871195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
26881195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
26891195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
26901195e687SMark J Musante 	}
26911195e687SMark J Musante 
26921195e687SMark J Musante 	/* OK, create a config that can be used to split */
26931195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
26941195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
26951195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
26961195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
26971195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
26981195e687SMark J Musante 
26991195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
27001195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
27011195e687SMark J Musante 
27021195e687SMark J Musante 	for (c = 0; c < schildren; c++)
27031195e687SMark J Musante 		nvlist_free(schild[c]);
27041195e687SMark J Musante 	free(schild);
27051195e687SMark J Musante 	nvlist_free(split);
27061195e687SMark J Musante 
27071195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
27081195e687SMark J Musante 
2709420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
27101195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2711420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
27121195e687SMark J Musante 
27131195e687SMark J Musante 	nvlist_free(config);
27141195e687SMark J Musante 
27151195e687SMark J Musante 	if (error == 0) {
27161195e687SMark J Musante 		(void) printf("successful split - results:\n");
27171195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
27181195e687SMark J Musante 		show_pool_stats(spa);
27191195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
27201195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
27211195e687SMark J Musante 		++zs->zs_splits;
27221195e687SMark J Musante 		--zs->zs_mirrors;
27231195e687SMark J Musante 	}
2724420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
27251195e687SMark J Musante 
27261195e687SMark J Musante }
27271195e687SMark J Musante 
2728fa9e4066Sahrens /*
2729fa9e4066Sahrens  * Verify that we can attach and detach devices.
2730fa9e4066Sahrens  */
2731b24ab676SJeff Bonwick /* ARGSUSED */
2732fa9e4066Sahrens void
2733b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2734fa9e4066Sahrens {
2735b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2736420dfc95SChris Siden 	spa_t *spa = ztest_spa;
273731157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
2738fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2739ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
2740e14bb325SJeff Bonwick 	nvlist_t *root;
27411195e687SMark J Musante 	uint64_t leaves;
2742fa9e4066Sahrens 	uint64_t leaf, top;
2743ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
27448ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
2745b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
2746ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2747fa9e4066Sahrens 	int replacing;
27489af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
274931157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
275031157203SJeff Bonwick 	int oldvd_is_log;
2751fa9e4066Sahrens 	int error, expected_error;
2752fa9e4066Sahrens 
2753420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
2754420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2755fa9e4066Sahrens 
2756e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2757fa9e4066Sahrens 
2758fa9e4066Sahrens 	/*
2759fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
2760fa9e4066Sahrens 	 */
2761fa9e4066Sahrens 	replacing = ztest_random(2);
2762fa9e4066Sahrens 
2763fa9e4066Sahrens 	/*
2764fa9e4066Sahrens 	 * Pick a random top-level vdev.
2765fa9e4066Sahrens 	 */
2766b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	/*
2769fa9e4066Sahrens 	 * Pick a random leaf within it.
2770fa9e4066Sahrens 	 */
2771d41c4376SMark J Musante 	leaf = ztest_random(leaves);
2772fa9e4066Sahrens 
2773fa9e4066Sahrens 	/*
277431157203SJeff Bonwick 	 * Locate this vdev.
2775fa9e4066Sahrens 	 */
277631157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
27771195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
27788ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
27791195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2780420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
27818ad4d6ddSJeff Bonwick 	}
2782420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
27838ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2784420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2785420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
27868ad4d6ddSJeff Bonwick 	}
2787fa9e4066Sahrens 
2788fa9e4066Sahrens 	/*
278931157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
279031157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
2791fa9e4066Sahrens 	 */
279231157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
27939af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
27948ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
27958ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
279631157203SJeff Bonwick 	}
2797fa9e4066Sahrens 
279831157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
2799573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
280031157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
280131157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
2802ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
28038ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
2804fa9e4066Sahrens 
28059af0a4dfSJeff Bonwick 	/*
28069af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
28079af0a4dfSJeff Bonwick 	 */
28089af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
28099af0a4dfSJeff Bonwick 		spa_config_exit(spa, SCL_VDEV, FTAG);
28108ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
28118ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
28128ad4d6ddSJeff Bonwick 		    error != ENOTSUP)
28138ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
2814420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
28159af0a4dfSJeff Bonwick 		return;
28169af0a4dfSJeff Bonwick 	}
28179af0a4dfSJeff Bonwick 
2818fa9e4066Sahrens 	/*
281931157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
282031157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2821fa9e4066Sahrens 	 */
282231157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
282331157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
282431157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
282531157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
282631157203SJeff Bonwick 	} else {
282731157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2828420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
2829420dfc95SChris Siden 		    top * leaves + leaf);
283031157203SJeff Bonwick 		if (ztest_random(2) == 0)
283131157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
283231157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
283331157203SJeff Bonwick 	}
283431157203SJeff Bonwick 
283531157203SJeff Bonwick 	if (newvd) {
2836573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
283731157203SJeff Bonwick 	} else {
283831157203SJeff Bonwick 		/*
283931157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
284031157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
284131157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
284231157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
284331157203SJeff Bonwick 		 */
284431157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
284531157203SJeff Bonwick 	}
2846fa9e4066Sahrens 
2847fa9e4066Sahrens 	/*
2848fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2849fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2850fa9e4066Sahrens 	 *
2851ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2852fa9e4066Sahrens 	 *
2853ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2854fa9e4066Sahrens 	 */
285531157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
285631157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
285731157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
285831157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
285931157203SJeff Bonwick 		expected_error = ENOTSUP;
286031157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
2861fa9e4066Sahrens 		expected_error = ENOTSUP;
286231157203SJeff Bonwick 	else if (newvd == oldvd)
286331157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
286431157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
286531157203SJeff Bonwick 		expected_error = EBUSY;
2866ea8dc4b6Seschrock 	else if (newsize < oldsize)
2867fa9e4066Sahrens 		expected_error = EOVERFLOW;
2868ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
2869ecc2d604Sbonwick 		expected_error = EDOM;
2870fa9e4066Sahrens 	else
2871fa9e4066Sahrens 		expected_error = 0;
2872fa9e4066Sahrens 
2873e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
2874fa9e4066Sahrens 
2875fa9e4066Sahrens 	/*
2876ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
2877fa9e4066Sahrens 	 */
287825345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2879e14bb325SJeff Bonwick 	    ashift, 0, 0, 0, 1);
2880fa9e4066Sahrens 
288131157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2882fa9e4066Sahrens 
2883fa9e4066Sahrens 	nvlist_free(root);
2884fa9e4066Sahrens 
2885fa9e4066Sahrens 	/*
2886fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2887fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2888fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2889fa9e4066Sahrens 	 */
2890fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
2891fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2892fa9e4066Sahrens 		expected_error = error;
2893fa9e4066Sahrens 
2894f0aa80d4Sbonwick 	/*
2895f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2896f0aa80d4Sbonwick 	 */
2897088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2898f0aa80d4Sbonwick 		expected_error = error;
2899f0aa80d4Sbonwick 
2900088f3894Sahrens 	/* XXX workaround 6690467 */
2901088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
2902088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
2903088f3894Sahrens 		    "returned %d, expected %d",
2904b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
2905b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
2906fa9e4066Sahrens 	}
2907fa9e4066Sahrens 
2908420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2909fa9e4066Sahrens }
2910fa9e4066Sahrens 
2911573ca77eSGeorge Wilson /*
2912573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
2913573ca77eSGeorge Wilson  */
2914573ca77eSGeorge Wilson vdev_t *
2915573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
2916573ca77eSGeorge Wilson {
2917573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2918573ca77eSGeorge Wilson 	size_t *newsize = arg;
2919573ca77eSGeorge Wilson 	size_t fsize;
2920573ca77eSGeorge Wilson 	int fd;
2921573ca77eSGeorge Wilson 
2922573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2923573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2924573ca77eSGeorge Wilson 
2925573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2926573ca77eSGeorge Wilson 		return (vd);
2927573ca77eSGeorge Wilson 
2928573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
2929573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
2930573ca77eSGeorge Wilson 
2931420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
2932573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
2933573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2934573ca77eSGeorge Wilson 	}
2935573ca77eSGeorge Wilson 	(void) close(fd);
2936573ca77eSGeorge Wilson 	return (NULL);
2937573ca77eSGeorge Wilson }
2938573ca77eSGeorge Wilson 
2939573ca77eSGeorge Wilson /*
2940573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
2941573ca77eSGeorge Wilson  */
2942573ca77eSGeorge Wilson /* ARGSUSED */
2943573ca77eSGeorge Wilson vdev_t *
2944573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
2945573ca77eSGeorge Wilson {
2946573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2947573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
2948573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
29498f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
2950095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2951095bcd66SGeorge Wilson 	int error;
2952573ca77eSGeorge Wilson 
2953573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2954573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2955573ca77eSGeorge Wilson 
2956573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
2957573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
2958095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2959573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2960573ca77eSGeorge Wilson 
2961095bcd66SGeorge Wilson 	/*
2962095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
2963095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
2964095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
2965095bcd66SGeorge Wilson 	 */
2966095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
2967420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
2968095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
2969095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
2970095bcd66SGeorge Wilson 		}
2971095bcd66SGeorge Wilson 		return (vd);
2972095bcd66SGeorge Wilson 	}
2973095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2974095bcd66SGeorge Wilson 
2975573ca77eSGeorge Wilson 	/*
2976573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
2977573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
2978573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
2979573ca77eSGeorge Wilson 	 * trying to online it.
2980573ca77eSGeorge Wilson 	 */
29818f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
2982420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
29838f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
29848f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
2985b24ab676SJeff Bonwick 			    "got gen %llu\n",
2986b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
29878f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
29888f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
29898f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
2990573ca77eSGeorge Wilson 		}
2991573ca77eSGeorge Wilson 		return (vd);
2992573ca77eSGeorge Wilson 	}
2993573ca77eSGeorge Wilson 	return (NULL);
2994573ca77eSGeorge Wilson }
2995573ca77eSGeorge Wilson 
2996573ca77eSGeorge Wilson /*
2997573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
2998573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
2999573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3000573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3001573ca77eSGeorge Wilson  * leaf vdev we encounter.
3002573ca77eSGeorge Wilson  */
3003573ca77eSGeorge Wilson vdev_t *
3004573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3005573ca77eSGeorge Wilson {
3006573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3007573ca77eSGeorge Wilson 		if (func == NULL)
3008573ca77eSGeorge Wilson 			return (vd);
3009573ca77eSGeorge Wilson 		else
3010573ca77eSGeorge Wilson 			return (func(vd, arg));
3011573ca77eSGeorge Wilson 	}
3012573ca77eSGeorge Wilson 
3013573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3014573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3015573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3016573ca77eSGeorge Wilson 			return (cvd);
3017573ca77eSGeorge Wilson 	}
3018573ca77eSGeorge Wilson 	return (NULL);
3019573ca77eSGeorge Wilson }
3020573ca77eSGeorge Wilson 
3021fa9e4066Sahrens /*
3022fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3023fa9e4066Sahrens  */
3024b24ab676SJeff Bonwick /* ARGSUSED */
3025fa9e4066Sahrens void
3026b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3027fa9e4066Sahrens {
3028420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3029b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3030b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3031b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3032573ca77eSGeorge Wilson 	size_t psize, newsize;
3033b24ab676SJeff Bonwick 	uint64_t top;
3034b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3035fa9e4066Sahrens 
3036420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
3037573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3038573ca77eSGeorge Wilson 
3039b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3040573ca77eSGeorge Wilson 
3041b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3042b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3043b24ab676SJeff Bonwick 	mc = mg->mg_class;
3044b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3045b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3046fa9e4066Sahrens 
3047fa9e4066Sahrens 	/*
3048573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3049573ca77eSGeorge Wilson 	 * our top-level device.
3050fa9e4066Sahrens 	 */
3051573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3052573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3053573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3054fa9e4066Sahrens 
3055573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3056fa9e4066Sahrens 
3057573ca77eSGeorge Wilson 	/*
30588f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
30598f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3060573ca77eSGeorge Wilson 	 */
30618f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3062420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3063573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3064420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3065573ca77eSGeorge Wilson 		return;
3066573ca77eSGeorge Wilson 	}
3067573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3068573ca77eSGeorge Wilson 	newsize = psize + psize / 8;
3069573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3070fa9e4066Sahrens 
3071420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3072b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3073573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3074573ca77eSGeorge Wilson 	}
3075573ca77eSGeorge Wilson 
3076573ca77eSGeorge Wilson 	/*
3077573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3078573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3079573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3080573ca77eSGeorge Wilson 	 */
3081573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3082573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3083573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3084420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3085b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3086b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3087b24ab676SJeff Bonwick 		}
3088b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3089420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3090b24ab676SJeff Bonwick 		return;
3091b24ab676SJeff Bonwick 	}
3092b24ab676SJeff Bonwick 
3093b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3094b24ab676SJeff Bonwick 
3095b24ab676SJeff Bonwick 	/*
3096b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3097b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3098b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3099b24ab676SJeff Bonwick 	 */
3100b24ab676SJeff Bonwick 	for (;;) {
3101b24ab676SJeff Bonwick 		boolean_t done;
3102b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3103b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3104b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3105b24ab676SJeff Bonwick 		if (done)
3106b24ab676SJeff Bonwick 			break;
3107b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3108b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3109b24ab676SJeff Bonwick 	}
3110b24ab676SJeff Bonwick 
3111b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3112b24ab676SJeff Bonwick 
3113b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3114b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3115b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3116b24ab676SJeff Bonwick 
3117b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3118420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3119b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3120b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3121fa9e4066Sahrens 		}
3122b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3123420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3124573ca77eSGeorge Wilson 		return;
3125fa9e4066Sahrens 	}
3126fa9e4066Sahrens 
3127573ca77eSGeorge Wilson 	/*
3128b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3129573ca77eSGeorge Wilson 	 */
3130b24ab676SJeff Bonwick 	if (new_ms_count <= old_ms_count)
3131b24ab676SJeff Bonwick 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3132b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
3133573ca77eSGeorge Wilson 
3134573ca77eSGeorge Wilson 	/*
3135573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3136573ca77eSGeorge Wilson 	 */
3137b24ab676SJeff Bonwick 	if (new_class_space <= old_class_space)
3138b24ab676SJeff Bonwick 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3139b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
3140b24ab676SJeff Bonwick 
3141420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
3142573ca77eSGeorge Wilson 		char oldnumbuf[6], newnumbuf[6];
3143573ca77eSGeorge Wilson 
3144b24ab676SJeff Bonwick 		nicenum(old_class_space, oldnumbuf);
3145b24ab676SJeff Bonwick 		nicenum(new_class_space, newnumbuf);
3146573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3147573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3148573ca77eSGeorge Wilson 	}
3149b24ab676SJeff Bonwick 
3150573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3151420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3152fa9e4066Sahrens }
3153fa9e4066Sahrens 
3154b24ab676SJeff Bonwick /*
3155b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3156b24ab676SJeff Bonwick  */
3157fa9e4066Sahrens /* ARGSUSED */
3158fa9e4066Sahrens static void
3159b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3160fa9e4066Sahrens {
3161fa9e4066Sahrens 	/*
3162b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3163fa9e4066Sahrens 	 */
3164b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3165fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3166fa9e4066Sahrens }
3167fa9e4066Sahrens 
316855da60b9SMark J Musante static int
316955da60b9SMark J Musante ztest_dataset_create(char *dsname)
317055da60b9SMark J Musante {
317155da60b9SMark J Musante 	uint64_t zilset = ztest_random(100);
317255da60b9SMark J Musante 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
317355da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
317455da60b9SMark J Musante 
317555da60b9SMark J Musante 	if (err || zilset < 80)
317655da60b9SMark J Musante 		return (err);
317755da60b9SMark J Musante 
3178420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3179420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
318055da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
318155da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
318255da60b9SMark J Musante }
318355da60b9SMark J Musante 
3184b24ab676SJeff Bonwick /* ARGSUSED */
31851d452cf5Sahrens static int
3186fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3187fa9e4066Sahrens {
3188fa9e4066Sahrens 	objset_t *os;
3189b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3190fa9e4066Sahrens 	int error;
3191fa9e4066Sahrens 
3192fa9e4066Sahrens 	/*
3193fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3194fa9e4066Sahrens 	 */
31953b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3196b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3197e1930233Sbonwick 	if (error != ENOENT) {
3198e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3199fb09f5aaSMadhav Suresh 		ASSERT0(error);
3200b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3201b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3202e1930233Sbonwick 	}
32033b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3204fa9e4066Sahrens 
3205fa9e4066Sahrens 	/*
3206fa9e4066Sahrens 	 * Destroy the dataset.
3207fa9e4066Sahrens 	 */
32083b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
32093b2aab18SMatthew Ahrens 		VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
32103b2aab18SMatthew Ahrens 	} else {
32113b2aab18SMatthew Ahrens 		VERIFY0(dsl_destroy_head(name));
32123b2aab18SMatthew Ahrens 	}
32131d452cf5Sahrens 	return (0);
3214fa9e4066Sahrens }
3215fa9e4066Sahrens 
3216b24ab676SJeff Bonwick static boolean_t
3217b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3218fa9e4066Sahrens {
3219b24ab676SJeff Bonwick 	char snapname[MAXNAMELEN];
3220b24ab676SJeff Bonwick 	int error;
3221b24ab676SJeff Bonwick 
32223b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3223b24ab676SJeff Bonwick 
32243b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3225b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3226b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3227b24ab676SJeff Bonwick 		return (B_FALSE);
3228b24ab676SJeff Bonwick 	}
32293b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
32303b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
32313b2aab18SMatthew Ahrens 		    snapname, error);
32323b2aab18SMatthew Ahrens 	}
3233b24ab676SJeff Bonwick 	return (B_TRUE);
3234b24ab676SJeff Bonwick }
3235b24ab676SJeff Bonwick 
3236b24ab676SJeff Bonwick static boolean_t
3237b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3238b24ab676SJeff Bonwick {
3239b24ab676SJeff Bonwick 	char snapname[MAXNAMELEN];
3240b24ab676SJeff Bonwick 	int error;
3241b24ab676SJeff Bonwick 
3242b24ab676SJeff Bonwick 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3243b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3244b24ab676SJeff Bonwick 
32453b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3246b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3247b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3248b24ab676SJeff Bonwick 	return (B_TRUE);
3249fa9e4066Sahrens }
3250fa9e4066Sahrens 
3251b24ab676SJeff Bonwick /* ARGSUSED */
3252fa9e4066Sahrens void
3253b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3254fa9e4066Sahrens {
3255b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3256b24ab676SJeff Bonwick 	int iters;
3257fa9e4066Sahrens 	int error;
3258745cd3c5Smaybee 	objset_t *os, *os2;
3259b24ab676SJeff Bonwick 	char name[MAXNAMELEN];
3260fa9e4066Sahrens 	zilog_t *zilog;
3261fa9e4066Sahrens 
3262420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
3263b24ab676SJeff Bonwick 
3264b24ab676SJeff Bonwick 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3265420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3266fa9e4066Sahrens 
3267fa9e4066Sahrens 	/*
3268fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3269fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3270b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3271fa9e4066Sahrens 	 */
3272fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3273503ad85cSMatthew Ahrens 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3274420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3275b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3276b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3277503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
3278fa9e4066Sahrens 	}
3279fa9e4066Sahrens 
3280fa9e4066Sahrens 	/*
3281fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3282fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3283fa9e4066Sahrens 	 * and all of its snapshots.
3284fa9e4066Sahrens 	 */
3285b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
32860b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3287fa9e4066Sahrens 
3288fa9e4066Sahrens 	/*
3289fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3290fa9e4066Sahrens 	 */
32913b2aab18SMatthew Ahrens 	VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
32923b2aab18SMatthew Ahrens 	    FTAG, &os));
3293fa9e4066Sahrens 
3294fa9e4066Sahrens 	/*
3295fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3296fa9e4066Sahrens 	 */
329755da60b9SMark J Musante 	error = ztest_dataset_create(name);
3298fa9e4066Sahrens 	if (error) {
3299fa9e4066Sahrens 		if (error == ENOSPC) {
3300b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3301420dfc95SChris Siden 			(void) rw_unlock(&ztest_name_lock);
3302fa9e4066Sahrens 			return;
3303fa9e4066Sahrens 		}
3304fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3305fa9e4066Sahrens 	}
3306fa9e4066Sahrens 
33073b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3308b24ab676SJeff Bonwick 
3309420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3310fa9e4066Sahrens 
3311fa9e4066Sahrens 	/*
3312fa9e4066Sahrens 	 * Open the intent log for it.
3313fa9e4066Sahrens 	 */
3314b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3315fa9e4066Sahrens 
3316fa9e4066Sahrens 	/*
3317b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3318b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3319fa9e4066Sahrens 	 */
3320b24ab676SJeff Bonwick 	iters = ztest_random(5);
3321b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3322b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3323b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3324b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3325fa9e4066Sahrens 	}
3326fa9e4066Sahrens 
3327fa9e4066Sahrens 	/*
3328fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3329fa9e4066Sahrens 	 */
3330b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3331b24ab676SJeff Bonwick 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3332fa9e4066Sahrens 
3333fa9e4066Sahrens 	/*
3334503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3335745cd3c5Smaybee 	 */
3336b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3337503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3338503ad85cSMatthew Ahrens 
3339503ad85cSMatthew Ahrens 	/*
3340b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3341503ad85cSMatthew Ahrens 	 */
3342b24ab676SJeff Bonwick 	VERIFY3U(EBUSY, ==,
3343b24ab676SJeff Bonwick 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3344fa9e4066Sahrens 
3345fa9e4066Sahrens 	zil_close(zilog);
3346503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3347b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3348fa9e4066Sahrens 
3349420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
3350fa9e4066Sahrens }
3351fa9e4066Sahrens 
3352fa9e4066Sahrens /*
3353fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3354fa9e4066Sahrens  */
3355fa9e4066Sahrens void
3356b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3357fa9e4066Sahrens {
3358420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
3359b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
3360b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
3361420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
33624f5064b7SMark J Musante }
33634f5064b7SMark J Musante 
3364718d718aSGeorge Wilson /*
3365718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
3366718d718aSGeorge Wilson  */
3367718d718aSGeorge Wilson void
3368b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3369718d718aSGeorge Wilson {
3370b24ab676SJeff Bonwick 	char snap1name[MAXNAMELEN];
3371b24ab676SJeff Bonwick 	char clone1name[MAXNAMELEN];
3372b24ab676SJeff Bonwick 	char snap2name[MAXNAMELEN];
3373b24ab676SJeff Bonwick 	char clone2name[MAXNAMELEN];
3374b24ab676SJeff Bonwick 	char snap3name[MAXNAMELEN];
3375718d718aSGeorge Wilson 	int error;
3376718d718aSGeorge Wilson 
3377b24ab676SJeff Bonwick 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3378b24ab676SJeff Bonwick 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3379b24ab676SJeff Bonwick 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3380b24ab676SJeff Bonwick 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3381b24ab676SJeff Bonwick 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
3382718d718aSGeorge Wilson 
33833b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
3384718d718aSGeorge Wilson 	if (error && error != ENOENT)
33853b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
33863b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
3387718d718aSGeorge Wilson 	if (error && error != ENOENT)
33883b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
33893b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
3390718d718aSGeorge Wilson 	if (error && error != ENOENT)
33913b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
33923b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
3393718d718aSGeorge Wilson 	if (error && error != ENOENT)
33943b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
33953b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
3396718d718aSGeorge Wilson 	if (error && error != ENOENT)
33973b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3398718d718aSGeorge Wilson }
3399718d718aSGeorge Wilson 
34004f5064b7SMark J Musante /*
34014f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
34024f5064b7SMark J Musante  */
34034f5064b7SMark J Musante void
3404b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
34054f5064b7SMark J Musante {
34063b2aab18SMatthew Ahrens 	objset_t *os;
3407b24ab676SJeff Bonwick 	char snap1name[MAXNAMELEN];
3408b24ab676SJeff Bonwick 	char clone1name[MAXNAMELEN];
3409b24ab676SJeff Bonwick 	char snap2name[MAXNAMELEN];
3410b24ab676SJeff Bonwick 	char clone2name[MAXNAMELEN];
3411b24ab676SJeff Bonwick 	char snap3name[MAXNAMELEN];
3412b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
3413b24ab676SJeff Bonwick 	int error;
34144f5064b7SMark J Musante 
3415420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
34164f5064b7SMark J Musante 
3417b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
3418718d718aSGeorge Wilson 
3419b24ab676SJeff Bonwick 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3420b24ab676SJeff Bonwick 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3421b24ab676SJeff Bonwick 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3422b24ab676SJeff Bonwick 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3423b24ab676SJeff Bonwick 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
342405312e2cSMark J Musante 
34254445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3426b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3427b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3428b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3429b2d22b66SGeorge Wilson 			goto out;
3430b2d22b66SGeorge Wilson 		}
3431b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3432b2d22b66SGeorge Wilson 	}
34334f5064b7SMark J Musante 
34343b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
3435b2d22b66SGeorge Wilson 	if (error) {
3436b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3437b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3438b2d22b66SGeorge Wilson 			goto out;
3439b2d22b66SGeorge Wilson 		}
3440b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3441b2d22b66SGeorge Wilson 	}
34424f5064b7SMark J Musante 
34434445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3444b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3445b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3446b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3447b2d22b66SGeorge Wilson 			goto out;
3448b2d22b66SGeorge Wilson 		}
3449b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3450b2d22b66SGeorge Wilson 	}
34514f5064b7SMark J Musante 
34524445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3453b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3454b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3455b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3456b2d22b66SGeorge Wilson 			goto out;
3457b2d22b66SGeorge Wilson 		}
3458b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3459b2d22b66SGeorge Wilson 	}
34604f5064b7SMark J Musante 
34613b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
3462b2d22b66SGeorge Wilson 	if (error) {
3463b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3464b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3465b2d22b66SGeorge Wilson 			goto out;
3466b2d22b66SGeorge Wilson 		}
3467b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3468b2d22b66SGeorge Wilson 	}
34694f5064b7SMark J Musante 
34703b2aab18SMatthew Ahrens 	error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
34714f5064b7SMark J Musante 	if (error)
34723b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3473681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
34745d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
34755d7b4d43SMatthew Ahrens 		dmu_objset_disown(os, FTAG);
34765d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
34775d7b4d43SMatthew Ahrens 		goto out;
34785d7b4d43SMatthew Ahrens 	}
34794f5064b7SMark J Musante 	if (error != EBUSY)
34804f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
34814f5064b7SMark J Musante 		    error);
34823b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
34834f5064b7SMark J Musante 
3484b2d22b66SGeorge Wilson out:
3485b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
34864f5064b7SMark J Musante 
3487420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
3488fa9e4066Sahrens }
3489fa9e4066Sahrens 
3490fa9e4066Sahrens /*
3491fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3492fa9e4066Sahrens  */
3493fa9e4066Sahrens void
3494b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3495fa9e4066Sahrens {
3496b24ab676SJeff Bonwick 	ztest_od_t od[4];
3497b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
3498fa9e4066Sahrens 
3499b24ab676SJeff Bonwick 	for (int b = 0; b < batchsize; b++)
3500b24ab676SJeff Bonwick 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3501fa9e4066Sahrens 
3502fa9e4066Sahrens 	/*
3503b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
3504b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
3505fa9e4066Sahrens 	 */
3506b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3507b24ab676SJeff Bonwick 		return;
3508fa9e4066Sahrens 
3509b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
3510b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
3511b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3512fa9e4066Sahrens }
3513fa9e4066Sahrens 
3514fa9e4066Sahrens /*
3515fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
3516fa9e4066Sahrens  */
3517fa9e4066Sahrens void
3518b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3519fa9e4066Sahrens {
3520b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
3521b24ab676SJeff Bonwick 	ztest_od_t od[2];
3522fa9e4066Sahrens 	dmu_tx_t *tx;
3523fa9e4066Sahrens 	int i, freeit, error;
3524fa9e4066Sahrens 	uint64_t n, s, txg;
3525fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3526b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3527b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3528fa9e4066Sahrens 	uint64_t regions = 997;
3529fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
3530fa9e4066Sahrens 	uint64_t width = 40;
3531fa9e4066Sahrens 	int free_percent = 5;
3532fa9e4066Sahrens 
3533fa9e4066Sahrens 	/*
3534fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3535fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3536fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3537fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3538fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3539fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
3540fa9e4066Sahrens 	 *
3541fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3542fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
3543fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
3544fa9e4066Sahrens 	 *
3545fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3546fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
3547fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3548fa9e4066Sahrens 	 *
3549fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
3550fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
3551fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3552fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3553fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
3554fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
3555fa9e4066Sahrens 	 */
3556fa9e4066Sahrens 
3557fa9e4066Sahrens 	/*
3558fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3559fa9e4066Sahrens 	 */
3560b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3561b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3562fa9e4066Sahrens 
3563b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3564b24ab676SJeff Bonwick 		return;
3565fa9e4066Sahrens 
3566b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
3567b24ab676SJeff Bonwick 	packobj = od[1].od_object;
3568b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
3569b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
3570fa9e4066Sahrens 
3571fa9e4066Sahrens 	/*
3572fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
3573fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
3574fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
3575fa9e4066Sahrens 	 * handle this race correctly.
3576fa9e4066Sahrens 	 */
3577fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3578fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
3579b24ab676SJeff Bonwick 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3580fa9e4066Sahrens 
3581fa9e4066Sahrens 	/*
3582fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3583fa9e4066Sahrens 	 */
3584fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3585fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
3586fa9e4066Sahrens 
3587fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
3588fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
3589fa9e4066Sahrens 
3590b24ab676SJeff Bonwick 	bigoff = n * chunksize;
3591b24ab676SJeff Bonwick 	bigsize = s * chunksize;
3592fa9e4066Sahrens 
3593fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3594fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3595fa9e4066Sahrens 
3596fa9e4066Sahrens 	/*
3597fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3598fa9e4066Sahrens 	 * overwriting it.
3599fa9e4066Sahrens 	 */
3600fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
3601fa9e4066Sahrens 
3602fa9e4066Sahrens 	/*
3603fa9e4066Sahrens 	 * Read the current contents of our objects.
3604fa9e4066Sahrens 	 */
3605b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
36067bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3607fb09f5aaSMadhav Suresh 	ASSERT0(error);
3608b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
36097bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3610fb09f5aaSMadhav Suresh 	ASSERT0(error);
3611fa9e4066Sahrens 
3612fa9e4066Sahrens 	/*
3613fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3614fa9e4066Sahrens 	 */
3615fa9e4066Sahrens 	tx = dmu_tx_create(os);
3616fa9e4066Sahrens 
3617b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3618fa9e4066Sahrens 
3619fa9e4066Sahrens 	if (freeit)
3620b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3621fa9e4066Sahrens 	else
3622b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3623fa9e4066Sahrens 
3624be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
3625be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
3626be9000ccSMatthew Ahrens 
3627b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3628b24ab676SJeff Bonwick 	if (txg == 0) {
3629fa9e4066Sahrens 		umem_free(packbuf, packsize);
3630fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
3631fa9e4066Sahrens 		return;
3632fa9e4066Sahrens 	}
3633fa9e4066Sahrens 
36345d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
36355d7b4d43SMatthew Ahrens 	do {
36365d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
36375d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
36385d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
36395d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
3640b24ab676SJeff Bonwick 
36415d7b4d43SMatthew Ahrens 	enum zio_compress comp;
36425d7b4d43SMatthew Ahrens 	do {
36435d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
36445d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
36455d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
36465d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
3647fa9e4066Sahrens 
3648fa9e4066Sahrens 	/*
3649fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3650fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3651fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3652fa9e4066Sahrens 	 * with the new values we want to write out.
3653fa9e4066Sahrens 	 */
3654fa9e4066Sahrens 	for (i = 0; i < s; i++) {
3655fa9e4066Sahrens 		/* LINTED */
3656fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3657fa9e4066Sahrens 		/* LINTED */
3658b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3659fa9e4066Sahrens 		/* LINTED */
3660b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3661fa9e4066Sahrens 
3662fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3663fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3664fa9e4066Sahrens 
3665fa9e4066Sahrens 		if (pack->bw_txg > txg)
3666fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3667fa9e4066Sahrens 			    pack->bw_txg, txg);
3668fa9e4066Sahrens 
3669fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3670fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3671fa9e4066Sahrens 			    pack->bw_index, n, i);
3672fa9e4066Sahrens 
3673fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3674fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3675fa9e4066Sahrens 
3676fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3677fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3678fa9e4066Sahrens 
3679fa9e4066Sahrens 		if (freeit) {
3680fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
3681fa9e4066Sahrens 		} else {
3682fa9e4066Sahrens 			pack->bw_index = n + i;
3683fa9e4066Sahrens 			pack->bw_txg = txg;
3684fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3685fa9e4066Sahrens 		}
3686fa9e4066Sahrens 		*bigH = *pack;
3687fa9e4066Sahrens 		*bigT = *pack;
3688fa9e4066Sahrens 	}
3689fa9e4066Sahrens 
3690fa9e4066Sahrens 	/*
3691fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
3692fa9e4066Sahrens 	 * Now write them out.
3693fa9e4066Sahrens 	 */
3694b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3695fa9e4066Sahrens 
3696fa9e4066Sahrens 	if (freeit) {
3697420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3698fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
3699fa9e4066Sahrens 			    " txg %llx\n",
3700fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3701fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3702fa9e4066Sahrens 			    (u_longlong_t)txg);
3703fa9e4066Sahrens 		}
3704b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3705fa9e4066Sahrens 	} else {
3706420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3707fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
3708fa9e4066Sahrens 			    " txg %llx\n",
3709fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3710fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3711fa9e4066Sahrens 			    (u_longlong_t)txg);
3712fa9e4066Sahrens 		}
3713b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3714fa9e4066Sahrens 	}
3715fa9e4066Sahrens 
3716fa9e4066Sahrens 	dmu_tx_commit(tx);
3717fa9e4066Sahrens 
3718fa9e4066Sahrens 	/*
3719fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
3720fa9e4066Sahrens 	 */
3721fa9e4066Sahrens 	{
3722fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3723fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3724fa9e4066Sahrens 
3725b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
37267bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
3727b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
37287bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3729fa9e4066Sahrens 
3730fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3731fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3732fa9e4066Sahrens 
3733fa9e4066Sahrens 		umem_free(packcheck, packsize);
3734fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
3735fa9e4066Sahrens 	}
3736fa9e4066Sahrens 
3737fa9e4066Sahrens 	umem_free(packbuf, packsize);
3738fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
3739fa9e4066Sahrens }
3740fa9e4066Sahrens 
37412fdbea25SAleksandr Guzovskiy void
37422fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3743b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
37442fdbea25SAleksandr Guzovskiy {
37452fdbea25SAleksandr Guzovskiy 	uint64_t i;
37462fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
37472fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
37482fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
37492fdbea25SAleksandr Guzovskiy 
37502fdbea25SAleksandr Guzovskiy 	/*
37512fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
37522fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
37532fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
37542fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
37552fdbea25SAleksandr Guzovskiy 	 */
37562fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
37572fdbea25SAleksandr Guzovskiy 		/* LINTED */
37582fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
37592fdbea25SAleksandr Guzovskiy 		/* LINTED */
3760b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
37612fdbea25SAleksandr Guzovskiy 		/* LINTED */
3762b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
37632fdbea25SAleksandr Guzovskiy 
37642fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
37652fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
37662fdbea25SAleksandr Guzovskiy 
37672fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
37682fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
37692fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
37702fdbea25SAleksandr Guzovskiy 
37712fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
37722fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
37732fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
37742fdbea25SAleksandr Guzovskiy 
37752fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
37762fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
37772fdbea25SAleksandr Guzovskiy 
37782fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
37792fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
37802fdbea25SAleksandr Guzovskiy 
37812fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
37822fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
37832fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
37842fdbea25SAleksandr Guzovskiy 
37852fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
37862fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
37872fdbea25SAleksandr Guzovskiy 	}
37882fdbea25SAleksandr Guzovskiy }
37892fdbea25SAleksandr Guzovskiy 
37902fdbea25SAleksandr Guzovskiy void
3791b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
37922fdbea25SAleksandr Guzovskiy {
3793b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
3794b24ab676SJeff Bonwick 	ztest_od_t od[2];
37952fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
37962fdbea25SAleksandr Guzovskiy 	uint64_t i;
37972fdbea25SAleksandr Guzovskiy 	int error;
37982fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
37992fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
3800b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3801b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
3802b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
38032fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
38042fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
38052fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
38062fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
38072fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
3808b24ab676SJeff Bonwick 	dmu_object_info_t doi;
38092fdbea25SAleksandr Guzovskiy 
38102fdbea25SAleksandr Guzovskiy 	/*
38112fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
38122fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
38132fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
38142fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
38152fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
38162fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
38172fdbea25SAleksandr Guzovskiy 	 *
38182fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
38192fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
38202fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
38212fdbea25SAleksandr Guzovskiy 	 *
38222fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
38232fdbea25SAleksandr Guzovskiy 	 * dmu_assign_arcbuf() can be tested for object updates.
38242fdbea25SAleksandr Guzovskiy 	 */
38252fdbea25SAleksandr Guzovskiy 
38262fdbea25SAleksandr Guzovskiy 	/*
38272fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
38282fdbea25SAleksandr Guzovskiy 	 */
3829b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3830b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
38312fdbea25SAleksandr Guzovskiy 
3832b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3833b24ab676SJeff Bonwick 		return;
38342fdbea25SAleksandr Guzovskiy 
3835b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
3836b24ab676SJeff Bonwick 	packobj = od[1].od_object;
3837b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
3838b24ab676SJeff Bonwick 	chunksize = blocksize;
3839b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
38402fdbea25SAleksandr Guzovskiy 
3841b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3842b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
3843b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
3844b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
38452fdbea25SAleksandr Guzovskiy 
38462fdbea25SAleksandr Guzovskiy 	/*
38472fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
38482fdbea25SAleksandr Guzovskiy 	 */
38492fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
38502fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
38512fdbea25SAleksandr Guzovskiy 
38522fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
38532fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
38542fdbea25SAleksandr Guzovskiy 
3855b24ab676SJeff Bonwick 	bigoff = n * chunksize;
3856b24ab676SJeff Bonwick 	bigsize = s * chunksize;
38572fdbea25SAleksandr Guzovskiy 
38582fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
38592fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
38602fdbea25SAleksandr Guzovskiy 
3861b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
38622fdbea25SAleksandr Guzovskiy 
38632fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
38642fdbea25SAleksandr Guzovskiy 
38652fdbea25SAleksandr Guzovskiy 	/*
38662fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
38672fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
38682fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
38692fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
38702fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
38712fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
38722fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
38732fdbea25SAleksandr Guzovskiy 	 */
38742fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
38752fdbea25SAleksandr Guzovskiy 		uint64_t j;
38762fdbea25SAleksandr Guzovskiy 		uint64_t off;
38772fdbea25SAleksandr Guzovskiy 
38782fdbea25SAleksandr Guzovskiy 		/*
38792fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
38802fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
38812fdbea25SAleksandr Guzovskiy 		 * dmu_assign_arcbuf() when it can't directly
38822fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
38832fdbea25SAleksandr Guzovskiy 		 */
38842fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
38852fdbea25SAleksandr Guzovskiy 			if (i != 5) {
38862fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
3887b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
38882fdbea25SAleksandr Guzovskiy 			} else {
38892fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
3890b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
38912fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
3892b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
38932fdbea25SAleksandr Guzovskiy 			}
38942fdbea25SAleksandr Guzovskiy 		}
38952fdbea25SAleksandr Guzovskiy 
38962fdbea25SAleksandr Guzovskiy 		/*
38972fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
38982fdbea25SAleksandr Guzovskiy 		 */
38992fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
39002fdbea25SAleksandr Guzovskiy 
3901b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
3902b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
39032fdbea25SAleksandr Guzovskiy 
3904b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3905b24ab676SJeff Bonwick 		if (txg == 0) {
39062fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
39072fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
39082fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
39092fdbea25SAleksandr Guzovskiy 				if (i != 5) {
39102fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
39112fdbea25SAleksandr Guzovskiy 				} else {
39122fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
39132fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
39142fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
39152fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
39162fdbea25SAleksandr Guzovskiy 				}
39172fdbea25SAleksandr Guzovskiy 			}
39182fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
39192fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
39202fdbea25SAleksandr Guzovskiy 			return;
39212fdbea25SAleksandr Guzovskiy 		}
39222fdbea25SAleksandr Guzovskiy 
39232fdbea25SAleksandr Guzovskiy 		/*
39242fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
39252fdbea25SAleksandr Guzovskiy 		 * test dmu_assign_arcbuf() for the case when there're no
39262fdbea25SAleksandr Guzovskiy 		 * existing dbufs for the specified offsets.
39272fdbea25SAleksandr Guzovskiy 		 */
39282fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
3929b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
39307bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
3931fb09f5aaSMadhav Suresh 			ASSERT0(error);
3932b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
39337bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
3934fb09f5aaSMadhav Suresh 			ASSERT0(error);
39352fdbea25SAleksandr Guzovskiy 		}
39362fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3937b24ab676SJeff Bonwick 		    n, chunksize, txg);
39382fdbea25SAleksandr Guzovskiy 
39392fdbea25SAleksandr Guzovskiy 		/*
39402fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
39412fdbea25SAleksandr Guzovskiy 		 * Now write them out.
39422fdbea25SAleksandr Guzovskiy 		 */
3943b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3944420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
39452fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
39462fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
39472fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
39482fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
39492fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
39502fdbea25SAleksandr Guzovskiy 		}
3951b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
39522fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
39532fdbea25SAleksandr Guzovskiy 			if (i != 5) {
39542fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
3955b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
39562fdbea25SAleksandr Guzovskiy 			} else {
39572fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
39582fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
3959b24ab676SJeff Bonwick 				    chunksize / 2);
39602fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
3961b24ab676SJeff Bonwick 				    chunksize / 2,
39622fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
3963b24ab676SJeff Bonwick 				    chunksize / 2);
39642fdbea25SAleksandr Guzovskiy 			}
39652fdbea25SAleksandr Guzovskiy 
39662fdbea25SAleksandr Guzovskiy 			if (i == 1) {
3967b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
396847cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
39692fdbea25SAleksandr Guzovskiy 			}
39702fdbea25SAleksandr Guzovskiy 			if (i != 5) {
39712fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
39722fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
39732fdbea25SAleksandr Guzovskiy 			} else {
39742fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
39752fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
39762fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db,
3977b24ab676SJeff Bonwick 				    off + chunksize / 2,
39782fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
39792fdbea25SAleksandr Guzovskiy 			}
39802fdbea25SAleksandr Guzovskiy 			if (i == 1) {
39812fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
39822fdbea25SAleksandr Guzovskiy 			}
39832fdbea25SAleksandr Guzovskiy 		}
39842fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
39852fdbea25SAleksandr Guzovskiy 
39862fdbea25SAleksandr Guzovskiy 		/*
39872fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
39882fdbea25SAleksandr Guzovskiy 		 */
39892fdbea25SAleksandr Guzovskiy 		{
39902fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
39912fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
39922fdbea25SAleksandr Guzovskiy 
3993b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
39947bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
3995b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
39967bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
39972fdbea25SAleksandr Guzovskiy 
39982fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
39992fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
40002fdbea25SAleksandr Guzovskiy 
40012fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
40022fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
40032fdbea25SAleksandr Guzovskiy 		}
40042fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4005b24ab676SJeff Bonwick 			txg_wait_open(dmu_objset_pool(os), 0);
4006b24ab676SJeff Bonwick 		} else if (i == 3) {
4007b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4008fa9e4066Sahrens 		}
4009e05725b1Sbonwick 	}
4010e05725b1Sbonwick 
4011b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4012b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4013b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4014b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4015b24ab676SJeff Bonwick }
4016fa9e4066Sahrens 
4017b24ab676SJeff Bonwick /* ARGSUSED */
4018b24ab676SJeff Bonwick void
4019b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4020b24ab676SJeff Bonwick {
4021b24ab676SJeff Bonwick 	ztest_od_t od[1];
4022b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4023b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4024fa9e4066Sahrens 
4025e05725b1Sbonwick 	/*
4026b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4027b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4028b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4029e05725b1Sbonwick 	 */
4030b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4031fa9e4066Sahrens 
4032b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4033e05725b1Sbonwick 		return;
4034fa9e4066Sahrens 
4035b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4036b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4037b24ab676SJeff Bonwick }
4038fa9e4066Sahrens 
4039b24ab676SJeff Bonwick void
4040b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4041b24ab676SJeff Bonwick {
4042b24ab676SJeff Bonwick 	ztest_od_t od[1];
4043b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4044b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4045b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4046b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4047b24ab676SJeff Bonwick 	void *data;
4048fa9e4066Sahrens 
4049b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4050fa9e4066Sahrens 
4051b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4052e05725b1Sbonwick 		return;
40530e34b6a7Sbonwick 
4054b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4055f4a72450SJeff Bonwick 		return;
4056f4a72450SJeff Bonwick 
4057b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4058fa9e4066Sahrens 
4059b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4060b24ab676SJeff Bonwick 
4061b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4062b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4063b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4064b24ab676SJeff Bonwick 		    data) != 0)
4065b24ab676SJeff Bonwick 			break;
4066b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4067b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4068b24ab676SJeff Bonwick 	}
4069b24ab676SJeff Bonwick 
4070b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4071fa9e4066Sahrens }
4072fa9e4066Sahrens 
4073fa9e4066Sahrens /*
4074fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4075fa9e4066Sahrens  */
4076fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4077fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4078fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4079fa9e4066Sahrens 
4080fa9e4066Sahrens void
4081b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4082fa9e4066Sahrens {
4083b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4084b24ab676SJeff Bonwick 	ztest_od_t od[1];
4085fa9e4066Sahrens 	uint64_t object;
4086fa9e4066Sahrens 	uint64_t txg, last_txg;
4087fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4088fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4089fa9e4066Sahrens 	int i, ints;
4090fa9e4066Sahrens 	dmu_tx_t *tx;
4091fa9e4066Sahrens 	char propname[100], txgname[100];
4092fa9e4066Sahrens 	int error;
4093fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4094fa9e4066Sahrens 
4095b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4096fa9e4066Sahrens 
4097b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4098b24ab676SJeff Bonwick 		return;
4099fa9e4066Sahrens 
4100b24ab676SJeff Bonwick 	object = od[0].od_object;
4101fa9e4066Sahrens 
4102b24ab676SJeff Bonwick 	/*
4103b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4104b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4105b24ab676SJeff Bonwick 	 */
4106b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4107b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4108b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4109b24ab676SJeff Bonwick 	if (txg == 0)
4110b24ab676SJeff Bonwick 		return;
4111b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4112b24ab676SJeff Bonwick 		value[i] = i;
4113b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4114b24ab676SJeff Bonwick 		    1, &value[i], tx));
4115b24ab676SJeff Bonwick 	}
4116b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4117b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4118b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4119b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4120b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4121b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4122b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4123fa9e4066Sahrens 	}
4124b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4125b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4126b24ab676SJeff Bonwick 	}
4127b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4128fa9e4066Sahrens 
4129b24ab676SJeff Bonwick 	/*
4130b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4131b24ab676SJeff Bonwick 	 */
4132fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4133fa9e4066Sahrens 
4134e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4135e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4136e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4137e05725b1Sbonwick 	bzero(value, sizeof (value));
4138e05725b1Sbonwick 	last_txg = 0;
4139fa9e4066Sahrens 
4140e05725b1Sbonwick 	/*
4141e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4142e05725b1Sbonwick 	 */
4143e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4144e05725b1Sbonwick 	if (error == 0) {
4145e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4146e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4147fa9e4066Sahrens 
4148e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4149e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4150fa9e4066Sahrens 
4151e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4152e05725b1Sbonwick 		    &zl_ints) == 0);
4153fa9e4066Sahrens 
4154e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4155e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4156fa9e4066Sahrens 
4157e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4158e05725b1Sbonwick 		    zl_ints, value) == 0);
4159fa9e4066Sahrens 
4160e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4161e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4162fa9e4066Sahrens 		}
4163e05725b1Sbonwick 	} else {
4164e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4165e05725b1Sbonwick 	}
4166fa9e4066Sahrens 
4167e05725b1Sbonwick 	/*
4168e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4169e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4170e05725b1Sbonwick 	 * in which the property was last updated.  The second
4171e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4172e05725b1Sbonwick 	 * should be txg + object + n.
4173e05725b1Sbonwick 	 */
4174e05725b1Sbonwick 	tx = dmu_tx_create(os);
4175b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4176b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4177b24ab676SJeff Bonwick 	if (txg == 0)
4178e05725b1Sbonwick 		return;
4179fa9e4066Sahrens 
4180e05725b1Sbonwick 	if (last_txg > txg)
4181e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4182fa9e4066Sahrens 
4183e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4184e05725b1Sbonwick 		value[i] = txg + object + i;
4185fa9e4066Sahrens 
4186b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4187b24ab676SJeff Bonwick 	    1, &txg, tx));
4188b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4189b24ab676SJeff Bonwick 	    ints, value, tx));
4190fa9e4066Sahrens 
4191e05725b1Sbonwick 	dmu_tx_commit(tx);
4192fa9e4066Sahrens 
4193e05725b1Sbonwick 	/*
4194e05725b1Sbonwick 	 * Remove a random pair of entries.
4195e05725b1Sbonwick 	 */
4196e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4197e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4198e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4199fa9e4066Sahrens 
4200e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4201fa9e4066Sahrens 
4202e05725b1Sbonwick 	if (error == ENOENT)
4203e05725b1Sbonwick 		return;
4204fa9e4066Sahrens 
4205fb09f5aaSMadhav Suresh 	ASSERT0(error);
4206fa9e4066Sahrens 
4207e05725b1Sbonwick 	tx = dmu_tx_create(os);
4208b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4209b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4210b24ab676SJeff Bonwick 	if (txg == 0)
421112a2833aSSanjeev Bagewadi 		return;
4212b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4213b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
421412a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
421512a2833aSSanjeev Bagewadi }
421612a2833aSSanjeev Bagewadi 
421712a2833aSSanjeev Bagewadi /*
421812a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
421912a2833aSSanjeev Bagewadi  */
422012a2833aSSanjeev Bagewadi void
4221b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
422212a2833aSSanjeev Bagewadi {
4223b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4224b24ab676SJeff Bonwick 	ztest_od_t od[1];
4225b24ab676SJeff Bonwick 	uint64_t object, txg;
422612a2833aSSanjeev Bagewadi 
4227b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
422812a2833aSSanjeev Bagewadi 
4229b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4230b24ab676SJeff Bonwick 		return;
423112a2833aSSanjeev Bagewadi 
4232b24ab676SJeff Bonwick 	object = od[0].od_object;
423312a2833aSSanjeev Bagewadi 
423412a2833aSSanjeev Bagewadi 	/*
4235b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
423612a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4237b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
423812a2833aSSanjeev Bagewadi 	 */
4239b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
4240b24ab676SJeff Bonwick 		char name[MAXNAMELEN];
4241b24ab676SJeff Bonwick 		uint64_t value = i;
4242b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4243b24ab676SJeff Bonwick 		int error;
424412a2833aSSanjeev Bagewadi 
4245b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4246b24ab676SJeff Bonwick 		    id, value);
424712a2833aSSanjeev Bagewadi 
4248b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4249b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4250b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4251b24ab676SJeff Bonwick 		if (txg == 0)
425212a2833aSSanjeev Bagewadi 			return;
4253b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4254b24ab676SJeff Bonwick 		    &value, tx);
425512a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
425612a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
425712a2833aSSanjeev Bagewadi 	}
4258fa9e4066Sahrens }
4259fa9e4066Sahrens 
4260b24ab676SJeff Bonwick /* ARGSUSED */
4261fa9e4066Sahrens void
4262b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4263fa9e4066Sahrens {
4264b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4265b24ab676SJeff Bonwick 	ztest_od_t od[1];
4266fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4267fa9e4066Sahrens 	dmu_tx_t *tx;
4268fa9e4066Sahrens 	int i, namelen, error;
4269b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4270fa9e4066Sahrens 	char name[20], string_value[20];
4271fa9e4066Sahrens 	void *data;
4272fa9e4066Sahrens 
4273b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4274b24ab676SJeff Bonwick 
4275b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4276b24ab676SJeff Bonwick 		return;
4277b24ab676SJeff Bonwick 
4278b24ab676SJeff Bonwick 	object = od[0].od_object;
4279b24ab676SJeff Bonwick 
4280e05725b1Sbonwick 	/*
4281e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4282e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4283e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4284e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4285e05725b1Sbonwick 	 */
4286e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4287fa9e4066Sahrens 
4288e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4289e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4290e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4291e05725b1Sbonwick 		name[i] = '.';
4292e05725b1Sbonwick 	name[i] = '\0';
4293fa9e4066Sahrens 
4294b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4295e05725b1Sbonwick 		wsize = sizeof (txg);
4296e05725b1Sbonwick 		wc = 1;
4297e05725b1Sbonwick 		data = &txg;
4298e05725b1Sbonwick 	} else {
4299e05725b1Sbonwick 		wsize = 1;
4300e05725b1Sbonwick 		wc = namelen;
4301e05725b1Sbonwick 		data = string_value;
4302e05725b1Sbonwick 	}
4303fa9e4066Sahrens 
4304e05725b1Sbonwick 	count = -1ULL;
43053b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4306e05725b1Sbonwick 	ASSERT(count != -1ULL);
4307fa9e4066Sahrens 
4308e05725b1Sbonwick 	/*
4309e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4310e05725b1Sbonwick 	 */
4311e05725b1Sbonwick 	i = ztest_random(5);
4312e05725b1Sbonwick 
4313e05725b1Sbonwick 	if (i >= 2) {
4314e05725b1Sbonwick 		tx = dmu_tx_create(os);
4315b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4316b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4317b24ab676SJeff Bonwick 		if (txg == 0)
4318e05725b1Sbonwick 			return;
4319e05725b1Sbonwick 		bcopy(name, string_value, namelen);
4320e05725b1Sbonwick 	} else {
4321e05725b1Sbonwick 		tx = NULL;
4322e05725b1Sbonwick 		txg = 0;
4323e05725b1Sbonwick 		bzero(string_value, namelen);
4324e05725b1Sbonwick 	}
4325fa9e4066Sahrens 
4326e05725b1Sbonwick 	switch (i) {
4327fa9e4066Sahrens 
4328e05725b1Sbonwick 	case 0:
4329e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4330e05725b1Sbonwick 		if (error == 0) {
4331e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
4332e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
4333e05725b1Sbonwick 		} else {
4334e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4335e05725b1Sbonwick 		}
4336e05725b1Sbonwick 		break;
4337fa9e4066Sahrens 
4338e05725b1Sbonwick 	case 1:
4339e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
4340e05725b1Sbonwick 		if (error == 0) {
4341e05725b1Sbonwick 			if (data == string_value &&
4342e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
4343e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
4344e05725b1Sbonwick 				    name, data, namelen);
4345e05725b1Sbonwick 		} else {
4346e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4347e05725b1Sbonwick 		}
4348e05725b1Sbonwick 		break;
4349fa9e4066Sahrens 
4350e05725b1Sbonwick 	case 2:
4351e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
4352e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
4353e05725b1Sbonwick 		break;
4354fa9e4066Sahrens 
4355e05725b1Sbonwick 	case 3:
4356e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4357e05725b1Sbonwick 		break;
4358fa9e4066Sahrens 
4359e05725b1Sbonwick 	case 4:
4360e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
4361e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
4362e05725b1Sbonwick 		break;
4363fa9e4066Sahrens 	}
4364e05725b1Sbonwick 
4365e05725b1Sbonwick 	if (tx != NULL)
4366e05725b1Sbonwick 		dmu_tx_commit(tx);
4367fa9e4066Sahrens }
4368fa9e4066Sahrens 
4369d20e665cSRicardo M. Correia /*
4370d20e665cSRicardo M. Correia  * Commit callback data.
4371d20e665cSRicardo M. Correia  */
4372d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
4373d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
4374d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
4375d20e665cSRicardo M. Correia 	int			zcd_expected_err;
4376d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
4377d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
4378d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
4379d20e665cSRicardo M. Correia } ztest_cb_data_t;
4380d20e665cSRicardo M. Correia 
4381d20e665cSRicardo M. Correia /* This is the actual commit callback function */
4382d20e665cSRicardo M. Correia static void
4383d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
4384d20e665cSRicardo M. Correia {
4385d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
4386d20e665cSRicardo M. Correia 	uint64_t synced_txg;
4387d20e665cSRicardo M. Correia 
4388d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
4389d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
4390d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
4391d20e665cSRicardo M. Correia 
4392d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
4393d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
4394d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4395d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4396d20e665cSRicardo M. Correia 		    synced_txg);
4397d20e665cSRicardo M. Correia 
4398d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
4399d20e665cSRicardo M. Correia 
4400d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
4401fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
4402d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
4403d20e665cSRicardo M. Correia 
4404d20e665cSRicardo M. Correia 		/*
4405d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
4406d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
4407d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
4408d20e665cSRicardo M. Correia 		 */
4409d20e665cSRicardo M. Correia 		return;
4410d20e665cSRicardo M. Correia 	}
4411d20e665cSRicardo M. Correia 
4412d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
4413d20e665cSRicardo M. Correia 	if (!data->zcd_added)
4414d20e665cSRicardo M. Correia 		goto out;
4415d20e665cSRicardo M. Correia 
4416d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
4417d20e665cSRicardo M. Correia 
4418d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
4419d20e665cSRicardo M. Correia 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4420d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
4421d20e665cSRicardo M. Correia 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4422d20e665cSRicardo M. Correia 
4423d20e665cSRicardo M. Correia out:
4424d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
4425d20e665cSRicardo M. Correia }
4426d20e665cSRicardo M. Correia 
4427d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
4428d20e665cSRicardo M. Correia static ztest_cb_data_t *
4429d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
4430d20e665cSRicardo M. Correia {
4431d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
4432d20e665cSRicardo M. Correia 
4433d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4434d20e665cSRicardo M. Correia 
4435d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
4436d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
4437d20e665cSRicardo M. Correia 
4438d20e665cSRicardo M. Correia 	return (cb_data);
4439d20e665cSRicardo M. Correia }
4440d20e665cSRicardo M. Correia 
4441d20e665cSRicardo M. Correia /*
4442d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
4443d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
4444d20e665cSRicardo M. Correia  * implementation bug.
4445d20e665cSRicardo M. Correia  */
4446d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
4447d20e665cSRicardo M. Correia 
4448d20e665cSRicardo M. Correia /*
4449d20e665cSRicardo M. Correia  * Commit callback test.
4450d20e665cSRicardo M. Correia  */
4451d20e665cSRicardo M. Correia void
4452b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4453d20e665cSRicardo M. Correia {
4454b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4455b24ab676SJeff Bonwick 	ztest_od_t od[1];
4456d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
4457d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
4458d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
4459d20e665cSRicardo M. Correia 	int i, error;
4460d20e665cSRicardo M. Correia 
4461b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4462b24ab676SJeff Bonwick 
4463b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4464b24ab676SJeff Bonwick 		return;
4465b24ab676SJeff Bonwick 
4466d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
4467d20e665cSRicardo M. Correia 
4468d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
4469d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4470d20e665cSRicardo M. Correia 
4471b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4472d20e665cSRicardo M. Correia 
4473d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
4474d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
4475d20e665cSRicardo M. Correia 		error = -1;
4476d20e665cSRicardo M. Correia 
4477d20e665cSRicardo M. Correia 	if (!error)
4478d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
4479d20e665cSRicardo M. Correia 
4480d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
4481d20e665cSRicardo M. Correia 
4482d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
4483d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
4484d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4485d20e665cSRicardo M. Correia 
4486d20e665cSRicardo M. Correia 	if (error) {
4487d20e665cSRicardo M. Correia 		/*
4488d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
4489d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
4490d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
4491d20e665cSRicardo M. Correia 		 * so we will check for that.
4492d20e665cSRicardo M. Correia 		 */
4493d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4494d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
4495d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
4496d20e665cSRicardo M. Correia 		}
4497d20e665cSRicardo M. Correia 
4498d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
4499d20e665cSRicardo M. Correia 
4500d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4501d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
4502d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4503d20e665cSRicardo M. Correia 		}
4504d20e665cSRicardo M. Correia 
4505d20e665cSRicardo M. Correia 		return;
4506d20e665cSRicardo M. Correia 	}
4507d20e665cSRicardo M. Correia 
4508d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
4509d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4510d20e665cSRicardo M. Correia 
4511d20e665cSRicardo M. Correia 	/*
4512d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
4513d20e665cSRicardo M. Correia 	 */
4514b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4515d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
4516d20e665cSRicardo M. Correia 
4517d20e665cSRicardo M. Correia 	if (old_txg > txg)
4518d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4519d20e665cSRicardo M. Correia 		    old_txg, txg);
4520d20e665cSRicardo M. Correia 
4521b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4522d20e665cSRicardo M. Correia 
4523d20e665cSRicardo M. Correia 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4524d20e665cSRicardo M. Correia 
4525d20e665cSRicardo M. Correia 	/*
4526d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
4527d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
4528d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
4529d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
4530d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
4531d20e665cSRicardo M. Correia 	 * implementation.
4532d20e665cSRicardo M. Correia 	 *
4533d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
4534d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
4535d20e665cSRicardo M. Correia 	 * implementation bug..
4536d20e665cSRicardo M. Correia 	 */
4537d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
4538d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
4539b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4540d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4541d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4542d20e665cSRicardo M. Correia 	}
4543d20e665cSRicardo M. Correia 
4544d20e665cSRicardo M. Correia 	/*
4545d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
4546d20e665cSRicardo M. Correia 	 *
4547d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
4548d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
4549d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
4550d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
4551d20e665cSRicardo M. Correia 	 */
4552d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
4553d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4554d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4555d20e665cSRicardo M. Correia 
4556d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
4557d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
4558d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
4559d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4560d20e665cSRicardo M. Correia 		else
4561d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4562d20e665cSRicardo M. Correia 			    cb_data[i]);
4563d20e665cSRicardo M. Correia 
4564d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
4565d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
4566d20e665cSRicardo M. Correia 
4567d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
4568d20e665cSRicardo M. Correia 	}
4569d20e665cSRicardo M. Correia 
4570d20e665cSRicardo M. Correia 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4571d20e665cSRicardo M. Correia 
4572d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
4573d20e665cSRicardo M. Correia }
4574d20e665cSRicardo M. Correia 
4575b24ab676SJeff Bonwick /* ARGSUSED */
4576fa9e4066Sahrens void
4577b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4578fa9e4066Sahrens {
4579b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
4580b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
4581b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
4582b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
4583b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
4584b24ab676SJeff Bonwick 	};
4585fa9e4066Sahrens 
4586420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
4587fa9e4066Sahrens 
4588b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4589b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4590b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4591fa9e4066Sahrens 
4592420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
4593b24ab676SJeff Bonwick }
4594fa9e4066Sahrens 
4595b24ab676SJeff Bonwick /* ARGSUSED */
4596b24ab676SJeff Bonwick void
4597b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4598b24ab676SJeff Bonwick {
4599b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
4600ea8dc4b6Seschrock 
4601420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
4602ea8dc4b6Seschrock 
4603420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4604b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4605fa9e4066Sahrens 
4606fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
4607fa9e4066Sahrens 
4608420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
4609b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
4610fa9e4066Sahrens 
4611b24ab676SJeff Bonwick 	nvlist_free(props);
4612fa9e4066Sahrens 
4613420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
4614fa9e4066Sahrens }
4615fa9e4066Sahrens 
46163b2aab18SMatthew Ahrens static int
46173b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
46183b2aab18SMatthew Ahrens {
46193b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
46203b2aab18SMatthew Ahrens 	int error;
46213b2aab18SMatthew Ahrens 
46223b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
46233b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
46243b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
46253b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
46263b2aab18SMatthew Ahrens 	fnvlist_free(holds);
46273b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
46283b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
46293b2aab18SMatthew Ahrens 	return (error);
46303b2aab18SMatthew Ahrens }
46313b2aab18SMatthew Ahrens 
46325c987a37SChris Kirby /*
46335c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
46345c987a37SChris Kirby  */
46355c987a37SChris Kirby void
4636b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
46375c987a37SChris Kirby {
46385c987a37SChris Kirby 	int error;
4639b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
46405c987a37SChris Kirby 	objset_t *origin;
46415c987a37SChris Kirby 	char snapname[100];
46425c987a37SChris Kirby 	char fullname[100];
46435c987a37SChris Kirby 	char clonename[100];
46445c987a37SChris Kirby 	char tag[100];
46455c987a37SChris Kirby 	char osname[MAXNAMELEN];
46463b2aab18SMatthew Ahrens 	nvlist_t *holds;
46475c987a37SChris Kirby 
4648420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
46495c987a37SChris Kirby 
46505c987a37SChris Kirby 	dmu_objset_name(os, osname);
46515c987a37SChris Kirby 
46523b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
46533b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
46543b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
46553b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
46563b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
46575c987a37SChris Kirby 
46585c987a37SChris Kirby 	/*
46595c987a37SChris Kirby 	 * Clean up from any previous run.
46605c987a37SChris Kirby 	 */
46613b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
46623b2aab18SMatthew Ahrens 	if (error != ENOENT)
46633b2aab18SMatthew Ahrens 		ASSERT0(error);
46643b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
46653b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
46663b2aab18SMatthew Ahrens 		ASSERT0(error);
46673b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
46683b2aab18SMatthew Ahrens 	if (error != ENOENT)
46693b2aab18SMatthew Ahrens 		ASSERT0(error);
46705c987a37SChris Kirby 
46715c987a37SChris Kirby 	/*
46725c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
46735c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
46745c987a37SChris Kirby 	 */
46754445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
46765c987a37SChris Kirby 	if (error) {
46775c987a37SChris Kirby 		if (error == ENOSPC) {
46785c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
46795c987a37SChris Kirby 			goto out;
46805c987a37SChris Kirby 		}
46815c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
46825c987a37SChris Kirby 	}
46835c987a37SChris Kirby 
46843b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
46855c987a37SChris Kirby 	if (error) {
46865c987a37SChris Kirby 		if (error == ENOSPC) {
46875c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
46885c987a37SChris Kirby 			goto out;
46895c987a37SChris Kirby 		}
46905c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
46915c987a37SChris Kirby 	}
46925c987a37SChris Kirby 
46933b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
46945c987a37SChris Kirby 	if (error) {
46953b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
46965c987a37SChris Kirby 		    fullname, error);
46975c987a37SChris Kirby 	}
46985c987a37SChris Kirby 
46993b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
47005c987a37SChris Kirby 	if (error)
47013b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
47025c987a37SChris Kirby 
47035c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
47045c987a37SChris Kirby 	if (error != ENOENT)
47055c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
47065c987a37SChris Kirby 
47075c987a37SChris Kirby 	/*
47085c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
47095c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
47105c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
47115c987a37SChris Kirby 	 */
47124445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
47135c987a37SChris Kirby 	if (error) {
47145c987a37SChris Kirby 		if (error == ENOSPC) {
47155c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
47165c987a37SChris Kirby 			goto out;
47175c987a37SChris Kirby 		}
47185c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
47195c987a37SChris Kirby 	}
47205c987a37SChris Kirby 
47213b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
47223b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
47233b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
47243b2aab18SMatthew Ahrens 	fnvlist_free(holds);
47253b2aab18SMatthew Ahrens 
47265d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
47275d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
47285d7b4d43SMatthew Ahrens 		goto out;
47295d7b4d43SMatthew Ahrens 	} else if (error) {
47305d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
47315d7b4d43SMatthew Ahrens 		    fullname, tag, error);
47325d7b4d43SMatthew Ahrens 	}
47335c987a37SChris Kirby 
47343b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
47355c987a37SChris Kirby 	if (error != EBUSY) {
47363b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
47375c987a37SChris Kirby 		    fullname, error);
47385c987a37SChris Kirby 	}
47395c987a37SChris Kirby 
47403b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
47415c987a37SChris Kirby 	if (error) {
47423b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
47435c987a37SChris Kirby 		    fullname, error);
47445c987a37SChris Kirby 	}
47455c987a37SChris Kirby 
47463b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
47475c987a37SChris Kirby 	if (error)
4748a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
47495c987a37SChris Kirby 
47503b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
47515c987a37SChris Kirby 
47525c987a37SChris Kirby out:
4753420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
47545c987a37SChris Kirby }
47555c987a37SChris Kirby 
4756fa9e4066Sahrens /*
4757fa9e4066Sahrens  * Inject random faults into the on-disk data.
4758fa9e4066Sahrens  */
4759b24ab676SJeff Bonwick /* ARGSUSED */
4760fa9e4066Sahrens void
4761b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4762fa9e4066Sahrens {
4763b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
4764420dfc95SChris Siden 	spa_t *spa = ztest_spa;
4765fa9e4066Sahrens 	int fd;
4766fa9e4066Sahrens 	uint64_t offset;
47671195e687SMark J Musante 	uint64_t leaves;
4768fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4769fa9e4066Sahrens 	uint64_t top, leaf;
4770fa9e4066Sahrens 	char path0[MAXPATHLEN];
4771fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
4772fa9e4066Sahrens 	size_t fsize;
4773fa9e4066Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4774fa9e4066Sahrens 	int iters = 1000;
47751195e687SMark J Musante 	int maxfaults;
47761195e687SMark J Musante 	int mirror_save;
4777e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
4778ea8dc4b6Seschrock 	uint64_t guid0 = 0;
47798f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
4780ea8dc4b6Seschrock 
4781420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
47821195e687SMark J Musante 	maxfaults = MAXFAULTS();
4783420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
47841195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
4785420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
47861195e687SMark J Musante 
4787e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
4788fa9e4066Sahrens 
47892c1e2b44SGeorge Wilson 	/*
47902c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
47912c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
47922c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
47932c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
47942c1e2b44SGeorge Wilson 	 */
47952c1e2b44SGeorge Wilson 	(void) rw_rdlock(&ztest_name_lock);
47962c1e2b44SGeorge Wilson 
4797fa9e4066Sahrens 	/*
4798e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4799fa9e4066Sahrens 	 */
4800e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4801fa9e4066Sahrens 
4802e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
4803e14bb325SJeff Bonwick 		/*
4804b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
4805e14bb325SJeff Bonwick 		 */
4806b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
48071195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
4808fa9e4066Sahrens 
4809e14bb325SJeff Bonwick 		/*
4810e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
4811e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
4812e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
4813e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
4814e14bb325SJeff Bonwick 		 */
4815e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
4816420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
4817420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
4818e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4819420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
4820420dfc95SChris Siden 		    top * leaves + leaf);
4821fa9e4066Sahrens 
4822e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
48238f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
48248f18d1faSGeorge Wilson 			islog = B_TRUE;
48258f18d1faSGeorge Wilson 
48262c1e2b44SGeorge Wilson 		/*
48272c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
48282c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
48292c1e2b44SGeorge Wilson 		 * resilvering.
48302c1e2b44SGeorge Wilson 		 */
48312c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
48322c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
4833b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
4834e14bb325SJeff Bonwick 			/*
4835e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
4836e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
4837e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
4838e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
4839e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
4840e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
4841e14bb325SJeff Bonwick 			 * combination of this with injection of random data
4842e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
4843e14bb325SJeff Bonwick 			 */
4844e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
4845e14bb325SJeff Bonwick 
4846e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
4847e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
4848e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
4849e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
4850e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
4851e14bb325SJeff Bonwick 			} else {
4852e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
4853e14bb325SJeff Bonwick 			}
4854e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
4855e14bb325SJeff Bonwick 		}
4856e14bb325SJeff Bonwick 	} else {
4857e14bb325SJeff Bonwick 		/*
4858e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
4859e14bb325SJeff Bonwick 		 */
4860e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
4861fa9e4066Sahrens 
4862e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
4863e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
48642c1e2b44SGeorge Wilson 			(void) rw_unlock(&ztest_name_lock);
4865e14bb325SJeff Bonwick 			return;
4866e14bb325SJeff Bonwick 		}
4867e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4868ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
4869e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
4870e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
4871e14bb325SJeff Bonwick 
4872e14bb325SJeff Bonwick 		leaf = 0;
4873e14bb325SJeff Bonwick 		leaves = 1;
4874e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
4875fa9e4066Sahrens 	}
4876fa9e4066Sahrens 
4877e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
48782c1e2b44SGeorge Wilson 	(void) rw_unlock(&ztest_name_lock);
4879e14bb325SJeff Bonwick 
4880fa9e4066Sahrens 	/*
48818f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
48828f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
4883fa9e4066Sahrens 	 */
48848f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
48858ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
48868ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
48878ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
48888f18d1faSGeorge Wilson 
48898f18d1faSGeorge Wilson 			/*
48908f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
48918f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
48928f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
48938f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
48948f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
48958f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
48968f18d1faSGeorge Wilson 			 */
48978f18d1faSGeorge Wilson 			if (islog)
4898420dfc95SChris Siden 				(void) rw_wrlock(&ztest_name_lock);
48998f18d1faSGeorge Wilson 
49008ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
49018f18d1faSGeorge Wilson 
49028f18d1faSGeorge Wilson 			if (islog)
4903420dfc95SChris Siden 				(void) rw_unlock(&ztest_name_lock);
49048ad4d6ddSJeff Bonwick 		} else {
49059253d63dSGeorge Wilson 			/*
49069253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
49079253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
49089253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
49099253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
49109253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
49119253d63dSGeorge Wilson 			 * prevent a race between injection testing and
49129253d63dSGeorge Wilson 			 * aux_vdev removal.
49139253d63dSGeorge Wilson 			 */
49149253d63dSGeorge Wilson 			VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
49158ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
49169253d63dSGeorge Wilson 			VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
49178ad4d6ddSJeff Bonwick 		}
4918fa9e4066Sahrens 	}
4919fa9e4066Sahrens 
49208f18d1faSGeorge Wilson 	if (maxfaults == 0)
49218f18d1faSGeorge Wilson 		return;
49228f18d1faSGeorge Wilson 
4923fa9e4066Sahrens 	/*
4924ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4925fa9e4066Sahrens 	 */
4926fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
4927fa9e4066Sahrens 
4928fa9e4066Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4929fa9e4066Sahrens 		return;
4930fa9e4066Sahrens 
4931fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4932fa9e4066Sahrens 
4933fa9e4066Sahrens 	while (--iters != 0) {
4934fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4935fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4936fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4937fa9e4066Sahrens 
4938fa9e4066Sahrens 		if (offset >= fsize)
4939fa9e4066Sahrens 			continue;
4940fa9e4066Sahrens 
4941420dfc95SChris Siden 		VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
49421195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
4943420dfc95SChris Siden 			VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
49441195e687SMark J Musante 			(void) close(fd);
49451195e687SMark J Musante 			return;
49461195e687SMark J Musante 		}
4947fa9e4066Sahrens 
4948fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4949fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4950fa9e4066Sahrens 			    offset, pathrand);
49511195e687SMark J Musante 
4952420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
49531195e687SMark J Musante 
4954420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
49551195e687SMark J Musante 			(void) printf("injected bad word into %s,"
49561195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4957fa9e4066Sahrens 	}
4958fa9e4066Sahrens 
4959fa9e4066Sahrens 	(void) close(fd);
4960fa9e4066Sahrens }
4961fa9e4066Sahrens 
4962fa9e4066Sahrens /*
4963b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
4964fa9e4066Sahrens  */
4965fa9e4066Sahrens void
4966b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4967fa9e4066Sahrens {
4968b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
4969420dfc95SChris Siden 	spa_t *spa = ztest_spa;
4970b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4971b24ab676SJeff Bonwick 	ztest_od_t od[1];
4972b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
4973b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
4974b24ab676SJeff Bonwick 	dmu_buf_t *db;
4975b24ab676SJeff Bonwick 	dmu_tx_t *tx;
4976b24ab676SJeff Bonwick 	void *buf;
4977b24ab676SJeff Bonwick 	blkptr_t blk;
4978b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
4979fa9e4066Sahrens 
4980b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
4981b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
4982fa9e4066Sahrens 
4983b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4984fa9e4066Sahrens 
4985b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4986b24ab676SJeff Bonwick 		return;
4987fa9e4066Sahrens 
4988fa9e4066Sahrens 	/*
4989b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
4990b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
4991fa9e4066Sahrens 	 */
4992420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
4993fa9e4066Sahrens 
4994b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4995b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
4996b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4997b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
4998420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
4999b24ab676SJeff Bonwick 		return;
5000b24ab676SJeff Bonwick 	}
5001b24ab676SJeff Bonwick 
5002b24ab676SJeff Bonwick 	object = od[0].od_object;
5003b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5004e9103aaeSGarrett D'Amore 	pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5005b24ab676SJeff Bonwick 
5006b24ab676SJeff Bonwick 	ASSERT(object != 0);
5007b24ab676SJeff Bonwick 
5008b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5009b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5010b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5011b24ab676SJeff Bonwick 	if (txg == 0) {
5012420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
5013b24ab676SJeff Bonwick 		return;
5014b24ab676SJeff Bonwick 	}
5015fa9e4066Sahrens 
5016fa9e4066Sahrens 	/*
5017b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5018fa9e4066Sahrens 	 */
5019b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5020b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
50213b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
50223b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
50233b2aab18SMatthew Ahrens 		if (error != 0) {
50243b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
50253b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
50263b2aab18SMatthew Ahrens 		}
5027b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5028b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5029b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5030b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5031b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5032b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5033b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5034b24ab676SJeff Bonwick 	}
5035fa9e4066Sahrens 
5036b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5037b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5038fa9e4066Sahrens 
5039fa9e4066Sahrens 	/*
5040b24ab676SJeff Bonwick 	 * Find out what block we got.
5041fa9e4066Sahrens 	 */
504280901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
504380901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5044b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5045b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5046fa9e4066Sahrens 
5047fa9e4066Sahrens 	/*
5048b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5049fa9e4066Sahrens 	 */
5050b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5051b24ab676SJeff Bonwick 	buf = zio_buf_alloc(psize);
5052b24ab676SJeff Bonwick 	ztest_pattern_set(buf, psize, ~pattern);
5053fa9e4066Sahrens 
5054b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5055b24ab676SJeff Bonwick 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5056b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5057fa9e4066Sahrens 
5058b24ab676SJeff Bonwick 	zio_buf_free(buf, psize);
5059fa9e4066Sahrens 
5060420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5061fa9e4066Sahrens }
5062fa9e4066Sahrens 
5063fa9e4066Sahrens /*
5064b24ab676SJeff Bonwick  * Scrub the pool.
5065fa9e4066Sahrens  */
5066b24ab676SJeff Bonwick /* ARGSUSED */
5067b24ab676SJeff Bonwick void
5068b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5069fa9e4066Sahrens {
5070420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5071fa9e4066Sahrens 
50723f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5073b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
50743f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5075b24ab676SJeff Bonwick }
5076fa9e4066Sahrens 
5077e9103aaeSGarrett D'Amore /*
5078e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5079e9103aaeSGarrett D'Amore  */
5080e9103aaeSGarrett D'Amore /* ARGSUSED */
5081e9103aaeSGarrett D'Amore void
5082e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5083e9103aaeSGarrett D'Amore {
5084420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5085e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5086dfbb9432SGeorge Wilson 	int error;
5087e9103aaeSGarrett D'Amore 
5088e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5089e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5090dfbb9432SGeorge Wilson 
5091dfbb9432SGeorge Wilson 	(void) rw_wrlock(&ztest_name_lock);
5092dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5093dfbb9432SGeorge Wilson 	(void) rw_unlock(&ztest_name_lock);
5094dfbb9432SGeorge Wilson 
5095dfbb9432SGeorge Wilson 	if (error != 0)
5096e9103aaeSGarrett D'Amore 		return;
5097e9103aaeSGarrett D'Amore 
509825345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5099e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5100e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5101e9103aaeSGarrett D'Amore 	}
5102e9103aaeSGarrett D'Amore 
5103e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5104e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5105e9103aaeSGarrett D'Amore }
5106e9103aaeSGarrett D'Amore 
5107b24ab676SJeff Bonwick /*
5108b24ab676SJeff Bonwick  * Rename the pool to a different name and then rename it back.
5109b24ab676SJeff Bonwick  */
5110b24ab676SJeff Bonwick /* ARGSUSED */
5111b24ab676SJeff Bonwick void
5112b24ab676SJeff Bonwick ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5113b24ab676SJeff Bonwick {
5114b24ab676SJeff Bonwick 	char *oldname, *newname;
5115b24ab676SJeff Bonwick 	spa_t *spa;
5116fa9e4066Sahrens 
5117420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
5118fa9e4066Sahrens 
5119420dfc95SChris Siden 	oldname = ztest_opts.zo_pool;
5120b24ab676SJeff Bonwick 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5121b24ab676SJeff Bonwick 	(void) strcpy(newname, oldname);
5122b24ab676SJeff Bonwick 	(void) strcat(newname, "_tmp");
5123fa9e4066Sahrens 
5124fa9e4066Sahrens 	/*
5125b24ab676SJeff Bonwick 	 * Do the rename
5126fa9e4066Sahrens 	 */
5127b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(oldname, newname));
5128fa9e4066Sahrens 
5129fa9e4066Sahrens 	/*
5130b24ab676SJeff Bonwick 	 * Try to open it under the old name, which shouldn't exist
5131fa9e4066Sahrens 	 */
5132b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5133fa9e4066Sahrens 
5134fa9e4066Sahrens 	/*
5135b24ab676SJeff Bonwick 	 * Open it under the new name and make sure it's still the same spa_t.
5136fa9e4066Sahrens 	 */
5137b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5138fa9e4066Sahrens 
5139420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5140b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5141fa9e4066Sahrens 
5142b24ab676SJeff Bonwick 	/*
5143b24ab676SJeff Bonwick 	 * Rename it back to the original
5144b24ab676SJeff Bonwick 	 */
5145b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(newname, oldname));
5146fa9e4066Sahrens 
5147fa9e4066Sahrens 	/*
5148b24ab676SJeff Bonwick 	 * Make sure it can still be opened
5149fa9e4066Sahrens 	 */
5150b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5151fa9e4066Sahrens 
5152420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5153b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5154fa9e4066Sahrens 
5155b24ab676SJeff Bonwick 	umem_free(newname, strlen(newname) + 1);
5156b24ab676SJeff Bonwick 
5157420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5158fa9e4066Sahrens }
5159fa9e4066Sahrens 
5160b24ab676SJeff Bonwick /*
5161b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
5162b24ab676SJeff Bonwick  */
5163fa9e4066Sahrens static void
5164b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
5165fa9e4066Sahrens {
5166fa9e4066Sahrens 	int status;
5167fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5168fa9e4066Sahrens 	char zbuf[1024];
5169fa9e4066Sahrens 	char *bin;
51708654d025Sperrin 	char *ztest;
51718654d025Sperrin 	char *isa;
51728654d025Sperrin 	int isalen;
5173fa9e4066Sahrens 	FILE *fp;
5174fa9e4066Sahrens 
5175fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
5176fa9e4066Sahrens 
5177fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5178fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
51798654d025Sperrin 	ztest = strstr(bin, "/ztest");
51808654d025Sperrin 	isa = bin + 8;
51818654d025Sperrin 	isalen = ztest - isa;
51828654d025Sperrin 	isa = strdup(isa);
5183fa9e4066Sahrens 	/* LINTED */
51843a57275aSck 	(void) sprintf(bin,
51855d7b4d43SMatthew Ahrens 	    "/usr/sbin%.*s/zdb -bcc%s%s -d -U %s %s",
51868654d025Sperrin 	    isalen,
51878654d025Sperrin 	    isa,
5188420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
5189420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
5190f0ba89beSJeff Bonwick 	    spa_config_path,
519188b7b0f2SMatthew Ahrens 	    pool);
51928654d025Sperrin 	free(isa);
5193fa9e4066Sahrens 
5194420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
5195fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
5196fa9e4066Sahrens 
5197fa9e4066Sahrens 	fp = popen(zdb, "r");
5198fa9e4066Sahrens 
5199fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5200420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
5201fa9e4066Sahrens 			(void) printf("%s", zbuf);
5202fa9e4066Sahrens 
5203fa9e4066Sahrens 	status = pclose(fp);
5204fa9e4066Sahrens 
5205fa9e4066Sahrens 	if (status == 0)
5206fa9e4066Sahrens 		return;
5207fa9e4066Sahrens 
5208fa9e4066Sahrens 	ztest_dump_core = 0;
5209fa9e4066Sahrens 	if (WIFEXITED(status))
5210fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5211fa9e4066Sahrens 	else
5212fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5213fa9e4066Sahrens }
5214fa9e4066Sahrens 
5215fa9e4066Sahrens static void
5216fa9e4066Sahrens ztest_walk_pool_directory(char *header)
5217fa9e4066Sahrens {
5218fa9e4066Sahrens 	spa_t *spa = NULL;
5219fa9e4066Sahrens 
5220420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5221fa9e4066Sahrens 		(void) printf("%s\n", header);
5222fa9e4066Sahrens 
5223fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
5224fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
5225420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
5226fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
5227fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
5228fa9e4066Sahrens }
5229fa9e4066Sahrens 
5230fa9e4066Sahrens static void
5231fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
5232fa9e4066Sahrens {
52338ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
5234fa9e4066Sahrens 	uint64_t pool_guid;
5235fa9e4066Sahrens 	spa_t *spa;
52363b2aab18SMatthew Ahrens 	int error;
5237fa9e4066Sahrens 
5238420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5239fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
5240fa9e4066Sahrens 		    oldname, newname);
5241fa9e4066Sahrens 	}
5242fa9e4066Sahrens 
5243fa9e4066Sahrens 	/*
5244fa9e4066Sahrens 	 * Clean up from previous runs.
5245fa9e4066Sahrens 	 */
5246fa9e4066Sahrens 	(void) spa_destroy(newname);
5247fa9e4066Sahrens 
5248fa9e4066Sahrens 	/*
5249fa9e4066Sahrens 	 * Get the pool's configuration and guid.
5250fa9e4066Sahrens 	 */
5251b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5252fa9e4066Sahrens 
52538ad4d6ddSJeff Bonwick 	/*
52548ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
52558ad4d6ddSJeff Bonwick 	 */
52568ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
52573f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
52588ad4d6ddSJeff Bonwick 
5259fa9e4066Sahrens 	pool_guid = spa_guid(spa);
5260fa9e4066Sahrens 	spa_close(spa, FTAG);
5261fa9e4066Sahrens 
5262fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
5263fa9e4066Sahrens 
5264fa9e4066Sahrens 	/*
5265fa9e4066Sahrens 	 * Export it.
5266fa9e4066Sahrens 	 */
5267b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5268fa9e4066Sahrens 
5269fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
5270fa9e4066Sahrens 
52718ad4d6ddSJeff Bonwick 	/*
52728ad4d6ddSJeff Bonwick 	 * Try to import it.
52738ad4d6ddSJeff Bonwick 	 */
52748ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
52758ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
52768ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
52778ad4d6ddSJeff Bonwick 
5278fa9e4066Sahrens 	/*
5279fa9e4066Sahrens 	 * Import it under the new name.
5280fa9e4066Sahrens 	 */
52813b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
52823b2aab18SMatthew Ahrens 	if (error != 0) {
52833b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
52843b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
52853b2aab18SMatthew Ahrens 		    oldname, newname, error);
52863b2aab18SMatthew Ahrens 	}
5287fa9e4066Sahrens 
5288fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
5289fa9e4066Sahrens 
5290fa9e4066Sahrens 	/*
5291fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
5292fa9e4066Sahrens 	 */
52934b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5294fa9e4066Sahrens 
5295fa9e4066Sahrens 	/*
5296fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
5297fa9e4066Sahrens 	 */
52984b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5299fa9e4066Sahrens 
5300fa9e4066Sahrens 	/*
5301fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
5302fa9e4066Sahrens 	 */
5303b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5304fa9e4066Sahrens 
5305fa9e4066Sahrens 	/*
5306fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
5307fa9e4066Sahrens 	 */
5308b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5309fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
5310fa9e4066Sahrens 	spa_close(spa, FTAG);
5311fa9e4066Sahrens 
5312fa9e4066Sahrens 	nvlist_free(config);
5313fa9e4066Sahrens }
5314fa9e4066Sahrens 
53158ad4d6ddSJeff Bonwick static void
53168ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
53178ad4d6ddSJeff Bonwick {
5318420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5319b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
5320b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
5321b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
5322b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
5323b24ab676SJeff Bonwick 	(void) zio_resume(spa);
53248ad4d6ddSJeff Bonwick }
53258ad4d6ddSJeff Bonwick 
53260a4e9518Sgw static void *
53278ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
53280a4e9518Sgw {
5329e14bb325SJeff Bonwick 	spa_t *spa = arg;
53300a4e9518Sgw 
53310a4e9518Sgw 	while (!ztest_exiting) {
5332b24ab676SJeff Bonwick 		if (spa_suspended(spa))
5333b24ab676SJeff Bonwick 			ztest_resume(spa);
5334b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
53350a4e9518Sgw 	}
53360a4e9518Sgw 	return (NULL);
53370a4e9518Sgw }
53380a4e9518Sgw 
5339b24ab676SJeff Bonwick static void *
5340b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
5341b24ab676SJeff Bonwick {
5342b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
53432c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
53442c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
5345b24ab676SJeff Bonwick 
53462c1e2b44SGeorge Wilson 	for (;;) {
534769962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
534869962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
5349b24ab676SJeff Bonwick 
535069962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5351b24ab676SJeff Bonwick 
53522c1e2b44SGeorge Wilson 		/*
53532c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
53542c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
53552c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
53562c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
53572c1e2b44SGeorge Wilson 		 */
53580713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
53592c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
53602c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
536169962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
53622c1e2b44SGeorge Wilson 			return (NULL);
53632c1e2b44SGeorge Wilson 		}
53642c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
5365b24ab676SJeff Bonwick 
536669962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
53672c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
53682c1e2b44SGeorge Wilson 		    total);
53692c1e2b44SGeorge Wilson 	}
5370b24ab676SJeff Bonwick }
5371b24ab676SJeff Bonwick 
5372b24ab676SJeff Bonwick static void
5373420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5374b24ab676SJeff Bonwick {
5375420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5376420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5377b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
5378b24ab676SJeff Bonwick 
5379b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
5380b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
5381b24ab676SJeff Bonwick 
5382b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
5383b24ab676SJeff Bonwick 
5384420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
5385420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
5386b24ab676SJeff Bonwick 
5387420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5388b24ab676SJeff Bonwick 		Dl_info dli;
5389b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
5390b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
5391b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
5392b24ab676SJeff Bonwick 	}
5393b24ab676SJeff Bonwick }
5394b24ab676SJeff Bonwick 
5395fa9e4066Sahrens static void *
5396fa9e4066Sahrens ztest_thread(void *arg)
5397fa9e4066Sahrens {
5398420dfc95SChris Siden 	int rand;
5399b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
5400fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
5401b24ab676SJeff Bonwick 	uint64_t call_next;
5402b24ab676SJeff Bonwick 	hrtime_t now;
5403fa9e4066Sahrens 	ztest_info_t *zi;
5404420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
5405fa9e4066Sahrens 
5406b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
5407fa9e4066Sahrens 		/*
5408fa9e4066Sahrens 		 * See if it's time to force a crash.
5409fa9e4066Sahrens 		 */
5410b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
5411b24ab676SJeff Bonwick 			ztest_kill(zs);
5412fa9e4066Sahrens 
5413fa9e4066Sahrens 		/*
5414b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
5415fa9e4066Sahrens 		 */
5416b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
5417b24ab676SJeff Bonwick 			break;
5418fa9e4066Sahrens 
5419fa9e4066Sahrens 		/*
5420b24ab676SJeff Bonwick 		 * Pick a random function to execute.
5421fa9e4066Sahrens 		 */
5422420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
5423420dfc95SChris Siden 		zi = &ztest_info[rand];
5424420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5425420dfc95SChris Siden 		call_next = zc->zc_next;
5426b24ab676SJeff Bonwick 
5427b24ab676SJeff Bonwick 		if (now >= call_next &&
5428420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
5429420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5430420dfc95SChris Siden 			ztest_execute(rand, zi, id);
5431420dfc95SChris Siden 		}
5432b24ab676SJeff Bonwick 	}
5433fa9e4066Sahrens 
5434b24ab676SJeff Bonwick 	return (NULL);
5435b24ab676SJeff Bonwick }
5436fa9e4066Sahrens 
5437b24ab676SJeff Bonwick static void
5438b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
5439b24ab676SJeff Bonwick {
5440b24ab676SJeff Bonwick 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5441b24ab676SJeff Bonwick }
5442fa9e4066Sahrens 
5443b24ab676SJeff Bonwick static void
5444420dfc95SChris Siden ztest_dataset_destroy(int d)
5445b24ab676SJeff Bonwick {
5446b24ab676SJeff Bonwick 	char name[MAXNAMELEN];
5447fa9e4066Sahrens 
5448420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5449fa9e4066Sahrens 
5450420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
5451b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
5452fa9e4066Sahrens 
5453b24ab676SJeff Bonwick 	/*
5454b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
5455b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
5456b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
5457b24ab676SJeff Bonwick 	 */
5458420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
5459420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
5460b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
5461420dfc95SChris Siden 	}
5462fa9e4066Sahrens 
5463b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5464b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5465b24ab676SJeff Bonwick }
5466b24ab676SJeff Bonwick 
5467b24ab676SJeff Bonwick static void
5468b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5469b24ab676SJeff Bonwick {
5470b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
5471b24ab676SJeff Bonwick 
5472b24ab676SJeff Bonwick 	/*
5473b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
5474b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
5475b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5476b24ab676SJeff Bonwick 	 * If not, we have an object leak.
5477b24ab676SJeff Bonwick 	 *
5478b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
5479b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
5480b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
5481b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
5482b24ab676SJeff Bonwick 	 */
5483b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5484b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5485b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
5486b24ab676SJeff Bonwick }
5487b24ab676SJeff Bonwick 
5488b24ab676SJeff Bonwick static int
5489420dfc95SChris Siden ztest_dataset_open(int d)
5490b24ab676SJeff Bonwick {
5491420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5492420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5493b24ab676SJeff Bonwick 	objset_t *os;
5494b24ab676SJeff Bonwick 	zilog_t *zilog;
5495b24ab676SJeff Bonwick 	char name[MAXNAMELEN];
5496b24ab676SJeff Bonwick 	int error;
5497b24ab676SJeff Bonwick 
5498420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5499b24ab676SJeff Bonwick 
5500420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
5501b24ab676SJeff Bonwick 
550255da60b9SMark J Musante 	error = ztest_dataset_create(name);
5503b24ab676SJeff Bonwick 	if (error == ENOSPC) {
5504420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
5505b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
5506b24ab676SJeff Bonwick 		return (error);
5507fa9e4066Sahrens 	}
5508b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
5509fa9e4066Sahrens 
55103b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5511420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5512b24ab676SJeff Bonwick 
5513420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5514b24ab676SJeff Bonwick 
5515b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
5516b24ab676SJeff Bonwick 
5517b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5518b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
5519b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
5520b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
5521b24ab676SJeff Bonwick 
5522b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5523b24ab676SJeff Bonwick 
5524b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
5525b24ab676SJeff Bonwick 
5526b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5527b24ab676SJeff Bonwick 
5528420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5529b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5530b24ab676SJeff Bonwick 		    zd->zd_name,
5531b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
5532b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
5533b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
5534b24ab676SJeff Bonwick 
5535b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
5536b24ab676SJeff Bonwick 
5537b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
5538b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
5539b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
5540b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
5541b24ab676SJeff Bonwick 
5542b24ab676SJeff Bonwick 	return (0);
5543b24ab676SJeff Bonwick }
5544b24ab676SJeff Bonwick 
5545b24ab676SJeff Bonwick static void
5546420dfc95SChris Siden ztest_dataset_close(int d)
5547b24ab676SJeff Bonwick {
5548420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5549b24ab676SJeff Bonwick 
5550b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
55513b2aab18SMatthew Ahrens 	dmu_objset_disown(zd->zd_os, zd);
5552b24ab676SJeff Bonwick 
5553b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
5554fa9e4066Sahrens }
5555fa9e4066Sahrens 
5556fa9e4066Sahrens /*
5557fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
5558fa9e4066Sahrens  */
5559fa9e4066Sahrens static void
5560b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
5561fa9e4066Sahrens {
5562b24ab676SJeff Bonwick 	thread_t *tid;
5563fa9e4066Sahrens 	spa_t *spa;
5564e9103aaeSGarrett D'Amore 	objset_t *os;
5565e14bb325SJeff Bonwick 	thread_t resume_tid;
5566b24ab676SJeff Bonwick 	int error;
5567e14bb325SJeff Bonwick 
5568e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
5569fa9e4066Sahrens 
5570fa9e4066Sahrens 	/*
5571b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
5572fa9e4066Sahrens 	 */
5573420dfc95SChris Siden 	VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
5574420dfc95SChris Siden 	VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5575fa9e4066Sahrens 
5576b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
5577420dfc95SChris Siden 	zs->zs_thread_stop =
5578420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5579b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5580b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
5581420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
5582420dfc95SChris Siden 		zs->zs_thread_kill -=
5583420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
5584420dfc95SChris Siden 	}
5585fa9e4066Sahrens 
5586b24ab676SJeff Bonwick 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5587fa9e4066Sahrens 
5588b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5589b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
5590fa9e4066Sahrens 
55910a4e9518Sgw 	/*
5592e14bb325SJeff Bonwick 	 * Open our pool.
55930a4e9518Sgw 	 */
5594b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
55953b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
559609c9d376SGeorge Wilson 	spa->spa_debug = B_TRUE;
5597*30beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
5598420dfc95SChris Siden 	ztest_spa = spa;
5599b24ab676SJeff Bonwick 
56003b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
56013b2aab18SMatthew Ahrens 	    DMU_OST_ANY, B_TRUE, FTAG, &os));
5602e9103aaeSGarrett D'Amore 	zs->zs_guid = dmu_objset_fsid_guid(os);
56033b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
5604e9103aaeSGarrett D'Amore 
5605b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
56060a4e9518Sgw 
56078ad4d6ddSJeff Bonwick 	/*
56088ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
56098ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
56108ad4d6ddSJeff Bonwick 	 * the only valid replica.
56118ad4d6ddSJeff Bonwick 	 */
56121195e687SMark J Musante 	if (MAXFAULTS() == 0)
56138ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
56148ad4d6ddSJeff Bonwick 	else
56158ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
56168ad4d6ddSJeff Bonwick 
5617fa9e4066Sahrens 	/*
5618e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
5619fa9e4066Sahrens 	 */
56208ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5621e14bb325SJeff Bonwick 	    &resume_tid) == 0);
5622fa9e4066Sahrens 
5623b24ab676SJeff Bonwick 	/*
5624b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
5625b24ab676SJeff Bonwick 	 */
5626b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5627b24ab676SJeff Bonwick 	    NULL) == 0);
5628b24ab676SJeff Bonwick 
5629fa9e4066Sahrens 	/*
5630fa9e4066Sahrens 	 * Verify that we can safely inquire about about any object,
5631fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
5632fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
5633fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
5634fa9e4066Sahrens 	 */
5635b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
5636b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
5637fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5638fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
5639ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
5640ea8dc4b6Seschrock 			    error == EINVAL);
5641fa9e4066Sahrens 		}
5642fa9e4066Sahrens 	}
5643fa9e4066Sahrens 
5644fa9e4066Sahrens 	/*
5645b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
5646fa9e4066Sahrens 	 */
5647b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
5648420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
5649420dfc95SChris Siden 		ztest_dataset_destroy(d);
5650b24ab676SJeff Bonwick 	}
5651fa9e4066Sahrens 	zs->zs_enospc_count = 0;
5652fa9e4066Sahrens 
5653420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
5654420dfc95SChris Siden 	    UMEM_NOFAIL);
5655fa9e4066Sahrens 
5656420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
5657fa9e4066Sahrens 		(void) printf("starting main threads...\n");
5658fa9e4066Sahrens 
5659b24ab676SJeff Bonwick 	/*
5660b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
5661b24ab676SJeff Bonwick 	 */
5662420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
5663420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
5664420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
5665b24ab676SJeff Bonwick 			return;
5666b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5667b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
5668fa9e4066Sahrens 	}
5669fa9e4066Sahrens 
5670b24ab676SJeff Bonwick 	/*
5671b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
5672b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
5673b24ab676SJeff Bonwick 	 */
5674420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5675b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5676420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
5677420dfc95SChris Siden 			ztest_dataset_close(t);
5678fa9e4066Sahrens 	}
5679fa9e4066Sahrens 
5680fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
5681fa9e4066Sahrens 
5682b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5683b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5684b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
5685b24ab676SJeff Bonwick 
5686420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
5687b24ab676SJeff Bonwick 
5688b24ab676SJeff Bonwick 	/* Kill the resume thread */
5689b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
5690b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5691b24ab676SJeff Bonwick 	ztest_resume(spa);
5692b24ab676SJeff Bonwick 
5693b24ab676SJeff Bonwick 	/*
5694b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
5695b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
5696b24ab676SJeff Bonwick 	 */
5697b24ab676SJeff Bonwick 	for (uint64_t object = 1; object < 50; object++)
5698b24ab676SJeff Bonwick 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5699b24ab676SJeff Bonwick 
5700b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5701fa9e4066Sahrens 
5702fa9e4066Sahrens 	/*
5703b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
5704fa9e4066Sahrens 	 */
5705b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
5706b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5707420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
5708b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
5709b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
5710b24ab676SJeff Bonwick 
5711b24ab676SJeff Bonwick 	/*
5712b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
5713b24ab676SJeff Bonwick 	 * different name.
5714b24ab676SJeff Bonwick 	 */
5715b24ab676SJeff Bonwick 	if (ztest_random(2) == 0) {
5716b24ab676SJeff Bonwick 		char name[MAXNAMELEN];
5717420dfc95SChris Siden 		(void) snprintf(name, MAXNAMELEN, "%s_import",
5718420dfc95SChris Siden 		    ztest_opts.zo_pool);
5719420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
5720420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
5721b24ab676SJeff Bonwick 	}
5722b24ab676SJeff Bonwick 
5723b24ab676SJeff Bonwick 	kernel_fini();
572426cb54a9SGeorge Wilson 
572526cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
572626cb54a9SGeorge Wilson 
572726cb54a9SGeorge Wilson 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
572826cb54a9SGeorge Wilson 
5729420dfc95SChris Siden 	(void) rwlock_destroy(&ztest_name_lock);
5730420dfc95SChris Siden 	(void) _mutex_destroy(&ztest_vdev_lock);
5731b24ab676SJeff Bonwick }
5732b24ab676SJeff Bonwick 
5733b24ab676SJeff Bonwick static void
5734420dfc95SChris Siden ztest_freeze(void)
5735b24ab676SJeff Bonwick {
5736420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
5737b24ab676SJeff Bonwick 	spa_t *spa;
57382215e990SMark J Musante 	int numloops = 0;
5739b24ab676SJeff Bonwick 
5740420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
5741b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
5742718d718aSGeorge Wilson 
5743b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
5744b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5745b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
574680901aeaSGeorge Wilson 	spa->spa_debug = B_TRUE;
574780901aeaSGeorge Wilson 	ztest_spa = spa;
5748718d718aSGeorge Wilson 
5749b24ab676SJeff Bonwick 	/*
5750b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
5751b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
5752b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
5753b24ab676SJeff Bonwick 	 */
5754b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5755b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
57565002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
5757fa9e4066Sahrens 	}
5758fa9e4066Sahrens 
5759ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5760fa9e4066Sahrens 
5761b24ab676SJeff Bonwick 	/*
5762b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
5763b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
5764b24ab676SJeff Bonwick 	 */
5765b24ab676SJeff Bonwick 	spa_freeze(spa);
5766e14bb325SJeff Bonwick 
5767b24ab676SJeff Bonwick 	/*
5768b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
5769b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5770b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
5771b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
5772b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
5773b24ab676SJeff Bonwick 	 */
5774420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
5775420dfc95SChris Siden 	    numloops++ < ztest_opts.zo_maxloops) {
5776b24ab676SJeff Bonwick 		ztest_dmu_write_parallel(zd, 0);
5777b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
5778b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
5779b24ab676SJeff Bonwick 	}
5780e14bb325SJeff Bonwick 
5781fa9e4066Sahrens 	/*
5782b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
5783fa9e4066Sahrens 	 */
57845002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
5785b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
5786fa9e4066Sahrens 
5787b24ab676SJeff Bonwick 	/*
5788b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
5789b24ab676SJeff Bonwick 	 */
5790420dfc95SChris Siden 	ztest_dataset_close(0);
5791e05725b1Sbonwick 	spa_close(spa, FTAG);
5792b24ab676SJeff Bonwick 	kernel_fini();
5793e05725b1Sbonwick 
5794b24ab676SJeff Bonwick 	/*
5795b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
5796b24ab676SJeff Bonwick 	 */
5797b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
5798b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5799ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
5800b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
5801420dfc95SChris Siden 	ztest_dataset_close(0);
5802dfbb9432SGeorge Wilson 
5803dfbb9432SGeorge Wilson 	spa->spa_debug = B_TRUE;
5804dfbb9432SGeorge Wilson 	ztest_spa = spa;
5805dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
5806dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
5807dfbb9432SGeorge Wilson 
5808b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5809fa9e4066Sahrens 	kernel_fini();
5810fa9e4066Sahrens }
5811fa9e4066Sahrens 
5812fa9e4066Sahrens void
5813fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
5814fa9e4066Sahrens {
5815fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
5816fa9e4066Sahrens 	hrtime_t m = s / 60;
5817fa9e4066Sahrens 	hrtime_t h = m / 60;
5818fa9e4066Sahrens 	hrtime_t d = h / 24;
5819fa9e4066Sahrens 
5820fa9e4066Sahrens 	s -= m * 60;
5821fa9e4066Sahrens 	m -= h * 60;
5822fa9e4066Sahrens 	h -= d * 24;
5823fa9e4066Sahrens 
5824fa9e4066Sahrens 	timebuf[0] = '\0';
5825fa9e4066Sahrens 
5826fa9e4066Sahrens 	if (d)
5827fa9e4066Sahrens 		(void) sprintf(timebuf,
5828fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5829fa9e4066Sahrens 	else if (h)
5830fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5831fa9e4066Sahrens 	else if (m)
5832fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5833fa9e4066Sahrens 	else
5834fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
5835fa9e4066Sahrens }
5836fa9e4066Sahrens 
58371195e687SMark J Musante static nvlist_t *
58381195e687SMark J Musante make_random_props()
58391195e687SMark J Musante {
58401195e687SMark J Musante 	nvlist_t *props;
58411195e687SMark J Musante 
58421195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5843ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
5844ad135b5dSChristopher Siden 		return (props);
58451195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
58461195e687SMark J Musante 
58471195e687SMark J Musante 	return (props);
58481195e687SMark J Musante }
58491195e687SMark J Musante 
5850fa9e4066Sahrens /*
5851fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
5852b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
5853fa9e4066Sahrens  */
5854fa9e4066Sahrens static void
5855b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
5856fa9e4066Sahrens {
5857fa9e4066Sahrens 	spa_t *spa;
58581195e687SMark J Musante 	nvlist_t *nvroot, *props;
5859fa9e4066Sahrens 
5860420dfc95SChris Siden 	VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
5861420dfc95SChris Siden 	VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5862b24ab676SJeff Bonwick 
5863fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
5864fa9e4066Sahrens 
5865fa9e4066Sahrens 	/*
5866fa9e4066Sahrens 	 * Create the storage pool.
5867fa9e4066Sahrens 	 */
5868420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
586988ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
58701195e687SMark J Musante 	zs->zs_splits = 0;
5871420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
587225345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
5873420dfc95SChris Siden 	    0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
58741195e687SMark J Musante 	props = make_random_props();
5875ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
5876ad135b5dSChristopher Siden 		char buf[1024];
5877ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
5878ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
5879b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
5880ad135b5dSChristopher Siden 	}
5881b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
5882fa9e4066Sahrens 	nvlist_free(nvroot);
5883fa9e4066Sahrens 
5884b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5885420dfc95SChris Siden 	zs->zs_metaslab_sz =
5886420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5887ad135b5dSChristopher Siden 
5888fa9e4066Sahrens 	spa_close(spa, FTAG);
5889fa9e4066Sahrens 
5890fa9e4066Sahrens 	kernel_fini();
5891b24ab676SJeff Bonwick 
5892420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
5893420dfc95SChris Siden 
5894420dfc95SChris Siden 	ztest_freeze();
5895420dfc95SChris Siden 
5896420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
5897420dfc95SChris Siden 
5898420dfc95SChris Siden 	(void) rwlock_destroy(&ztest_name_lock);
5899420dfc95SChris Siden 	(void) _mutex_destroy(&ztest_vdev_lock);
5900420dfc95SChris Siden }
5901420dfc95SChris Siden 
5902420dfc95SChris Siden static void
5903741652b0SEtienne Dechamps setup_data_fd(void)
5904420dfc95SChris Siden {
5905741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
5906420dfc95SChris Siden 
5907741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
5908741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
5909741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
5910420dfc95SChris Siden }
5911420dfc95SChris Siden 
5912741652b0SEtienne Dechamps 
5913ad135b5dSChristopher Siden static int
5914ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
5915ad135b5dSChristopher Siden {
5916ad135b5dSChristopher Siden 	int size;
5917ad135b5dSChristopher Siden 
5918ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
5919ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
5920ad135b5dSChristopher Siden 	size += hdr->zh_size;
5921ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
5922ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
5923ad135b5dSChristopher Siden 
5924ad135b5dSChristopher Siden 	return (size);
5925ad135b5dSChristopher Siden }
5926ad135b5dSChristopher Siden 
5927420dfc95SChris Siden static void
5928420dfc95SChris Siden setup_hdr(void)
5929420dfc95SChris Siden {
5930ad135b5dSChristopher Siden 	int size;
5931420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
5932420dfc95SChris Siden 
5933420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5934741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5935420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
5936420dfc95SChris Siden 
5937741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
5938ad135b5dSChristopher Siden 
5939420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
5940420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
5941420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
5942420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
5943420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
5944420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
5945420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
5946420dfc95SChris Siden 
5947ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
5948741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
5949ad135b5dSChristopher Siden 
5950420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5951420dfc95SChris Siden }
5952420dfc95SChris Siden 
5953420dfc95SChris Siden static void
5954420dfc95SChris Siden setup_data(void)
5955420dfc95SChris Siden {
5956420dfc95SChris Siden 	int size, offset;
5957420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
5958420dfc95SChris Siden 	uint8_t *buf;
5959420dfc95SChris Siden 
5960420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
5961741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
5962420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
5963420dfc95SChris Siden 
5964ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
5965420dfc95SChris Siden 
5966420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
5967420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
5968741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
5969420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
5970420dfc95SChris Siden 	buf = (uint8_t *)hdr;
5971420dfc95SChris Siden 
5972420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
5973420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
5974420dfc95SChris Siden 	offset += hdr->zh_opts_size;
5975420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
5976420dfc95SChris Siden 	offset += hdr->zh_size;
5977420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
5978420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
5979420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
5980420dfc95SChris Siden }
5981420dfc95SChris Siden 
5982420dfc95SChris Siden static boolean_t
5983420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
5984420dfc95SChris Siden {
5985420dfc95SChris Siden 	pid_t pid;
5986420dfc95SChris Siden 	int status;
5987741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
5988420dfc95SChris Siden 
5989420dfc95SChris Siden 	pid = fork();
5990420dfc95SChris Siden 
5991420dfc95SChris Siden 	if (cmd == NULL) {
5992741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5993741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
5994420dfc95SChris Siden 		cmd = cmdbuf;
5995420dfc95SChris Siden 	}
5996b24ab676SJeff Bonwick 
5997420dfc95SChris Siden 	if (pid == -1)
5998420dfc95SChris Siden 		fatal(1, "fork failed");
5999b24ab676SJeff Bonwick 
6000420dfc95SChris Siden 	if (pid == 0) {	/* child */
6001420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
6002741652b0SEtienne Dechamps 		char fd_data_str[12];
600326cb54a9SGeorge Wilson 
6004420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
6005420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
6006741652b0SEtienne Dechamps 
6007741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
6008741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
6009741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6010741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6011741652b0SEtienne Dechamps 
6012420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
6013420dfc95SChris Siden 		if (libpath != NULL)
6014420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6015420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
6016420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
6017420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
6018420dfc95SChris Siden 	}
6019420dfc95SChris Siden 
6020741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
6021741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
6022741652b0SEtienne Dechamps 		cmd = NULL;
6023741652b0SEtienne Dechamps 	}
6024741652b0SEtienne Dechamps 
6025420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
6026420dfc95SChris Siden 		continue;
6027420dfc95SChris Siden 	if (statusp != NULL)
6028420dfc95SChris Siden 		*statusp = status;
6029420dfc95SChris Siden 
6030420dfc95SChris Siden 	if (WIFEXITED(status)) {
6031420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
6032420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
6033420dfc95SChris Siden 			    WEXITSTATUS(status));
6034420dfc95SChris Siden 			exit(2);
6035420dfc95SChris Siden 		}
6036420dfc95SChris Siden 		return (B_FALSE);
6037420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
6038420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6039420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
6040420dfc95SChris Siden 			    WTERMSIG(status));
6041420dfc95SChris Siden 			exit(3);
6042420dfc95SChris Siden 		}
6043420dfc95SChris Siden 		return (B_TRUE);
6044420dfc95SChris Siden 	} else {
6045420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
6046420dfc95SChris Siden 		exit(4);
6047420dfc95SChris Siden 		/* NOTREACHED */
6048420dfc95SChris Siden 	}
6049420dfc95SChris Siden }
6050420dfc95SChris Siden 
6051420dfc95SChris Siden static void
6052420dfc95SChris Siden ztest_run_init(void)
6053420dfc95SChris Siden {
6054420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
6055420dfc95SChris Siden 
6056420dfc95SChris Siden 	ASSERT(ztest_opts.zo_init != 0);
6057420dfc95SChris Siden 
6058420dfc95SChris Siden 	/*
6059420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
6060420dfc95SChris Siden 	 */
6061420dfc95SChris Siden 	(void) remove(spa_config_path);
6062420dfc95SChris Siden 
6063420dfc95SChris Siden 	/*
6064420dfc95SChris Siden 	 * Create and initialize our storage pool.
6065420dfc95SChris Siden 	 */
6066420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
6067420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
6068420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
6069420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
6070420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
6071420dfc95SChris Siden 		}
6072420dfc95SChris Siden 		ztest_init(zs);
6073420dfc95SChris Siden 	}
6074fa9e4066Sahrens }
6075fa9e4066Sahrens 
6076fa9e4066Sahrens int
6077fa9e4066Sahrens main(int argc, char **argv)
6078fa9e4066Sahrens {
6079fa9e4066Sahrens 	int kills = 0;
6080fa9e4066Sahrens 	int iters = 0;
6081420dfc95SChris Siden 	int older = 0;
6082420dfc95SChris Siden 	int newer = 0;
6083fa9e4066Sahrens 	ztest_shared_t *zs;
6084fa9e4066Sahrens 	ztest_info_t *zi;
6085420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6086fa9e4066Sahrens 	char timebuf[100];
6087fa9e4066Sahrens 	char numbuf[6];
6088b24ab676SJeff Bonwick 	spa_t *spa;
6089741652b0SEtienne Dechamps 	char *cmd;
6090420dfc95SChris Siden 	boolean_t hasalt;
6091741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
6092fa9e4066Sahrens 
6093fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
6094fa9e4066Sahrens 
6095cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
609669962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
6097cd1c8b85SMatthew Ahrens 
6098741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6099741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
6100741652b0SEtienne Dechamps 
6101741652b0SEtienne Dechamps 	if (!fd_data_str) {
6102420dfc95SChris Siden 		process_options(argc, argv);
6103fa9e4066Sahrens 
6104741652b0SEtienne Dechamps 		setup_data_fd();
6105420dfc95SChris Siden 		setup_hdr();
6106420dfc95SChris Siden 		setup_data();
6107420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
6108420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
6109420dfc95SChris Siden 	} else {
6110741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
6111420dfc95SChris Siden 		setup_data();
6112420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6113420dfc95SChris Siden 	}
6114420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6115fa9e4066Sahrens 
6116f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
6117741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6118741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
6119f0ba89beSJeff Bonwick 
6120420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6121420dfc95SChris Siden 	    UMEM_NOFAIL);
6122420dfc95SChris Siden 	zs = ztest_shared;
6123ea8dc4b6Seschrock 
6124741652b0SEtienne Dechamps 	if (fd_data_str) {
6125420dfc95SChris Siden 		metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6126420dfc95SChris Siden 		metaslab_df_alloc_threshold =
6127420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
6128b24ab676SJeff Bonwick 
6129420dfc95SChris Siden 		if (zs->zs_do_init)
6130420dfc95SChris Siden 			ztest_run_init();
6131420dfc95SChris Siden 		else
6132420dfc95SChris Siden 			ztest_run(zs);
6133420dfc95SChris Siden 		exit(0);
6134420dfc95SChris Siden 	}
6135fa9e4066Sahrens 
6136420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6137420dfc95SChris Siden 
6138420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6139fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
6140fa9e4066Sahrens 		    " %llu seconds...\n",
6141420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
6142420dfc95SChris Siden 		    ztest_opts.zo_datasets,
6143420dfc95SChris Siden 		    ztest_opts.zo_threads,
6144420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
6145fa9e4066Sahrens 	}
6146fa9e4066Sahrens 
6147741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6148741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6149420dfc95SChris Siden 
6150420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
6151420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6152420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6153420dfc95SChris Siden 			(void) printf("Executing older ztest for "
6154420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
6155420dfc95SChris Siden 		}
6156420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6157420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6158420dfc95SChris Siden 	} else {
6159420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6160fa9e4066Sahrens 	}
6161420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
6162fa9e4066Sahrens 
6163b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
6164420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6165fa9e4066Sahrens 
6166b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
6167420dfc95SChris Siden 		zi = &ztest_info[f];
6168420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
6169b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6170420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
6171fa9e4066Sahrens 		else
6172420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
6173b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
6174fa9e4066Sahrens 	}
6175fa9e4066Sahrens 
6176fa9e4066Sahrens 	/*
6177fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
6178fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
6179fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
6180fa9e4066Sahrens 	 */
6181b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
6182fa9e4066Sahrens 		int status;
6183420dfc95SChris Siden 		boolean_t killed;
6184fa9e4066Sahrens 
6185fa9e4066Sahrens 		/*
6186fa9e4066Sahrens 		 * Initialize the workload counters for each function.
6187fa9e4066Sahrens 		 */
6188b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
6189420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
6190420dfc95SChris Siden 			zc->zc_count = 0;
6191420dfc95SChris Siden 			zc->zc_time = 0;
6192fa9e4066Sahrens 		}
6193fa9e4066Sahrens 
6194d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
6195420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
6196420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
6197fa9e4066Sahrens 
6198420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
6199420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6200420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
6201420dfc95SChris Siden 				    cmd);
6202fa9e4066Sahrens 			}
6203420dfc95SChris Siden 			newer++;
6204420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
62055ad82045Snd 		} else {
6206420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6207420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
6208420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
6209420dfc95SChris Siden 			}
6210420dfc95SChris Siden 			older++;
6211420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
6212420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
6213fa9e4066Sahrens 		}
6214fa9e4066Sahrens 
6215420dfc95SChris Siden 		if (killed)
6216420dfc95SChris Siden 			kills++;
6217fa9e4066Sahrens 		iters++;
6218fa9e4066Sahrens 
6219420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6220fa9e4066Sahrens 			hrtime_t now = gethrtime();
6221fa9e4066Sahrens 
6222b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
6223b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
6224fa9e4066Sahrens 			nicenum(zs->zs_space, numbuf);
6225fa9e4066Sahrens 
6226fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6227fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6228fa9e4066Sahrens 			    iters,
6229fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
6230fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
6231fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
6232fa9e4066Sahrens 			    numbuf,
6233b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
6234420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
6235fa9e4066Sahrens 		}
6236fa9e4066Sahrens 
6237420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
6238fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
6239fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6240fa9e4066Sahrens 			    "Calls", "Time", "Function");
6241fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6242fa9e4066Sahrens 			    "-----", "----", "--------");
6243b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
6244fa9e4066Sahrens 				Dl_info dli;
6245fa9e4066Sahrens 
6246420dfc95SChris Siden 				zi = &ztest_info[f];
6247420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
6248420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
6249fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
6250fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
6251420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
6252fa9e4066Sahrens 				    dli.dli_sname);
6253fa9e4066Sahrens 			}
6254fa9e4066Sahrens 			(void) printf("\n");
6255fa9e4066Sahrens 		}
6256fa9e4066Sahrens 
6257fa9e4066Sahrens 		/*
6258b24ab676SJeff Bonwick 		 * It's possible that we killed a child during a rename test,
6259b24ab676SJeff Bonwick 		 * in which case we'll have a 'ztest_tmp' pool lying around
6260b24ab676SJeff Bonwick 		 * instead of 'ztest'.  Do a blind rename in case this happened.
6261fa9e4066Sahrens 		 */
6262b24ab676SJeff Bonwick 		kernel_init(FREAD);
6263420dfc95SChris Siden 		if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6264b24ab676SJeff Bonwick 			spa_close(spa, FTAG);
6265b24ab676SJeff Bonwick 		} else {
6266b24ab676SJeff Bonwick 			char tmpname[MAXNAMELEN];
6267b24ab676SJeff Bonwick 			kernel_fini();
6268b24ab676SJeff Bonwick 			kernel_init(FREAD | FWRITE);
6269b24ab676SJeff Bonwick 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6270420dfc95SChris Siden 			    ztest_opts.zo_pool);
6271420dfc95SChris Siden 			(void) spa_rename(tmpname, ztest_opts.zo_pool);
6272b24ab676SJeff Bonwick 		}
6273fa9e4066Sahrens 		kernel_fini();
6274fa9e4066Sahrens 
6275420dfc95SChris Siden 		ztest_run_zdb(ztest_opts.zo_pool);
6276b24ab676SJeff Bonwick 	}
6277fa9e4066Sahrens 
6278420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6279420dfc95SChris Siden 		if (hasalt) {
6280420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
6281420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
6282420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
6283420dfc95SChris Siden 			    cmd);
6284420dfc95SChris Siden 		}
6285fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6286fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6287fa9e4066Sahrens 	}
6288fa9e4066Sahrens 
6289741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
6290741652b0SEtienne Dechamps 
6291fa9e4066Sahrens 	return (0);
6292fa9e4066Sahrens }
6293