xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision 5cabbc6b)
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.
23aab80726SGeorge Wilson  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24e9103aaeSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
26c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
270a055120SJason King  * Copyright 2017 Joyent, Inc.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens /*
31fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
32fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
33fa9e4066Sahrens  *
34fa9e4066Sahrens  * The overall design of the ztest program is as follows:
35fa9e4066Sahrens  *
36fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
37fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
38fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
39fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
40fa9e4066Sahrens  *
41fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
42fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
43fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
44fa9e4066Sahrens  *
45fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
46fa9e4066Sahrens  *     verify that self-healing data really works.
47fa9e4066Sahrens  *
48fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
49fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
50fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
53fa9e4066Sahrens  *     we run the entire test in a child of the main process.
54fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
55fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
56fa9e4066Sahrens  *     The parent then runs the test again, using the existing
575d7b4d43SMatthew Ahrens  *     storage pool, as many times as desired. If backwards compatibility
58420dfc95SChris Siden  *     testing is enabled ztest will sometimes run the "older" version
59420dfc95SChris Siden  *     of ztest after a SIGKILL.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
62fa9e4066Sahrens  *     many of the functional tests record the transaction group number
63fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
64fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
65fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
66fa9e4066Sahrens  *
67fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
68fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
69fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
70fa9e4066Sahrens  *
71fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
72fa9e4066Sahrens  *
73fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
74fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
75fa9e4066Sahrens  *
76420dfc95SChris Siden  * Use the -k option to set the desired frequency of kills.
77420dfc95SChris Siden  *
78420dfc95SChris Siden  * When ztest invokes itself it passes all relevant information through a
79420dfc95SChris Siden  * temporary file which is mmap-ed in the child process. This allows shared
80420dfc95SChris Siden  * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
81420dfc95SChris Siden  * stored at offset 0 of this file and contains information on the size and
82420dfc95SChris Siden  * number of shared structures in the file. The information stored in this file
83420dfc95SChris Siden  * must remain backwards compatible with older versions of ztest so that
84420dfc95SChris Siden  * ztest can invoke them during backwards compatibility testing (-B).
85fa9e4066Sahrens  */
86fa9e4066Sahrens 
87fa9e4066Sahrens #include <sys/zfs_context.h>
88fa9e4066Sahrens #include <sys/spa.h>
89fa9e4066Sahrens #include <sys/dmu.h>
90fa9e4066Sahrens #include <sys/txg.h>
912fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
92fa9e4066Sahrens #include <sys/zap.h>
93fa9e4066Sahrens #include <sys/dmu_objset.h>
94fa9e4066Sahrens #include <sys/poll.h>
95fa9e4066Sahrens #include <sys/stat.h>
96fa9e4066Sahrens #include <sys/time.h>
97fa9e4066Sahrens #include <sys/wait.h>
98fa9e4066Sahrens #include <sys/mman.h>
99fa9e4066Sahrens #include <sys/resource.h>
100fa9e4066Sahrens #include <sys/zio.h>
101fa9e4066Sahrens #include <sys/zil.h>
102b24ab676SJeff Bonwick #include <sys/zil_impl.h>
103fa9e4066Sahrens #include <sys/vdev_impl.h>
104e14bb325SJeff Bonwick #include <sys/vdev_file.h>
105fa9e4066Sahrens #include <sys/spa_impl.h>
10688ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
107fa9e4066Sahrens #include <sys/dsl_prop.h>
1084f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1093b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1103f9d6ad7SLin Ling #include <sys/dsl_scan.h>
111cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
112fa9e4066Sahrens #include <sys/refcount.h>
113ad135b5dSChristopher Siden #include <sys/zfeature.h>
1143b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
115770499e1SDan Kimmel #include <sys/abd.h>
116fa9e4066Sahrens #include <stdio.h>
117004388ebScasper #include <stdio_ext.h>
118fa9e4066Sahrens #include <stdlib.h>
119fa9e4066Sahrens #include <unistd.h>
120fa9e4066Sahrens #include <signal.h>
121fa9e4066Sahrens #include <umem.h>
122fa9e4066Sahrens #include <dlfcn.h>
123fa9e4066Sahrens #include <ctype.h>
124fa9e4066Sahrens #include <math.h>
125fa9e4066Sahrens #include <sys/fs/zfs.h>
126b24ab676SJeff Bonwick #include <libnvpair.h>
1270a055120SJason King #include <libcmdutils.h>
128fa9e4066Sahrens 
129741652b0SEtienne Dechamps static int ztest_fd_data = -1;
130741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
131420dfc95SChris Siden 
132420dfc95SChris Siden typedef struct ztest_shared_hdr {
133420dfc95SChris Siden 	uint64_t	zh_hdr_size;
134420dfc95SChris Siden 	uint64_t	zh_opts_size;
135420dfc95SChris Siden 	uint64_t	zh_size;
136420dfc95SChris Siden 	uint64_t	zh_stats_size;
137420dfc95SChris Siden 	uint64_t	zh_stats_count;
138420dfc95SChris Siden 	uint64_t	zh_ds_size;
139420dfc95SChris Siden 	uint64_t	zh_ds_count;
140420dfc95SChris Siden } ztest_shared_hdr_t;
141420dfc95SChris Siden 
142420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
143420dfc95SChris Siden 
144420dfc95SChris Siden typedef struct ztest_shared_opts {
1459adfa60dSMatthew Ahrens 	char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
1469adfa60dSMatthew Ahrens 	char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
147420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
148420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
149420dfc95SChris Siden 	uint64_t zo_vdevs;
150420dfc95SChris Siden 	uint64_t zo_vdevtime;
151420dfc95SChris Siden 	size_t zo_vdev_size;
152420dfc95SChris Siden 	int zo_ashift;
153420dfc95SChris Siden 	int zo_mirrors;
154420dfc95SChris Siden 	int zo_raidz;
155420dfc95SChris Siden 	int zo_raidz_parity;
156420dfc95SChris Siden 	int zo_datasets;
157420dfc95SChris Siden 	int zo_threads;
158420dfc95SChris Siden 	uint64_t zo_passtime;
159420dfc95SChris Siden 	uint64_t zo_killrate;
160420dfc95SChris Siden 	int zo_verbose;
161420dfc95SChris Siden 	int zo_init;
162420dfc95SChris Siden 	uint64_t zo_time;
163420dfc95SChris Siden 	uint64_t zo_maxloops;
164420dfc95SChris Siden 	uint64_t zo_metaslab_gang_bang;
165420dfc95SChris Siden } ztest_shared_opts_t;
166420dfc95SChris Siden 
167420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
168420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
169420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
170420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
171420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
172420dfc95SChris Siden 	.zo_vdevs = 5,
173420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
174420dfc95SChris Siden 	.zo_mirrors = 2,
175420dfc95SChris Siden 	.zo_raidz = 4,
176420dfc95SChris Siden 	.zo_raidz_parity = 1,
1778363e80aSGeorge Wilson 	.zo_vdev_size = SPA_MINDEVSIZE * 4,	/* 256m default size */
178420dfc95SChris Siden 	.zo_datasets = 7,
179420dfc95SChris Siden 	.zo_threads = 23,
180420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
181420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
182420dfc95SChris Siden 	.zo_verbose = 0,
183420dfc95SChris Siden 	.zo_init = 1,
184420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
185420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
186420dfc95SChris Siden 	.zo_metaslab_gang_bang = 32 << 10
187420dfc95SChris Siden };
188420dfc95SChris Siden 
189420dfc95SChris Siden extern uint64_t metaslab_gang_bang;
190420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
19169962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
19230beaff4SGeorge Wilson extern int metaslab_preload_limit;
193dcbf3bd6SGeorge Wilson extern boolean_t zfs_compressed_arc_enabled;
194770499e1SDan Kimmel extern boolean_t zfs_abd_scatter_enabled;
195420dfc95SChris Siden 
196420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
197420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
198420dfc95SChris Siden 
199420dfc95SChris Siden typedef struct ztest_shared_ds {
200420dfc95SChris Siden 	uint64_t	zd_seq;
201420dfc95SChris Siden } ztest_shared_ds_t;
202420dfc95SChris Siden 
203420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
204420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
205fa9e4066Sahrens 
206b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
207420dfc95SChris Siden #define	MAXFAULTS() \
208420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
209b24ab676SJeff Bonwick 
210b24ab676SJeff Bonwick enum ztest_io_type {
211b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
212b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
213b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
214b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
215b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
21680901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
217b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
218b24ab676SJeff Bonwick };
219b24ab676SJeff Bonwick 
220e05725b1Sbonwick typedef struct ztest_block_tag {
221b24ab676SJeff Bonwick 	uint64_t	bt_magic;
222e05725b1Sbonwick 	uint64_t	bt_objset;
223e05725b1Sbonwick 	uint64_t	bt_object;
224e05725b1Sbonwick 	uint64_t	bt_offset;
225b24ab676SJeff Bonwick 	uint64_t	bt_gen;
226e05725b1Sbonwick 	uint64_t	bt_txg;
227b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
228e05725b1Sbonwick } ztest_block_tag_t;
229e05725b1Sbonwick 
230b24ab676SJeff Bonwick typedef struct bufwad {
231b24ab676SJeff Bonwick 	uint64_t	bw_index;
232b24ab676SJeff Bonwick 	uint64_t	bw_txg;
233b24ab676SJeff Bonwick 	uint64_t	bw_data;
234b24ab676SJeff Bonwick } bufwad_t;
235b24ab676SJeff Bonwick 
236b24ab676SJeff Bonwick /*
237b24ab676SJeff Bonwick  * XXX -- fix zfs range locks to be generic so we can use them here.
238b24ab676SJeff Bonwick  */
239b24ab676SJeff Bonwick typedef enum {
240b24ab676SJeff Bonwick 	RL_READER,
241b24ab676SJeff Bonwick 	RL_WRITER,
242b24ab676SJeff Bonwick 	RL_APPEND
243b24ab676SJeff Bonwick } rl_type_t;
244b24ab676SJeff Bonwick 
245b24ab676SJeff Bonwick typedef struct rll {
246b24ab676SJeff Bonwick 	void		*rll_writer;
247b24ab676SJeff Bonwick 	int		rll_readers;
248b24ab676SJeff Bonwick 	mutex_t		rll_lock;
249b24ab676SJeff Bonwick 	cond_t		rll_cv;
250b24ab676SJeff Bonwick } rll_t;
251b24ab676SJeff Bonwick 
252b24ab676SJeff Bonwick typedef struct rl {
253b24ab676SJeff Bonwick 	uint64_t	rl_object;
254b24ab676SJeff Bonwick 	uint64_t	rl_offset;
255b24ab676SJeff Bonwick 	uint64_t	rl_size;
256b24ab676SJeff Bonwick 	rll_t		*rl_lock;
257b24ab676SJeff Bonwick } rl_t;
258b24ab676SJeff Bonwick 
259b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
260b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
261b24ab676SJeff Bonwick 
262b24ab676SJeff Bonwick /*
263b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
264b24ab676SJeff Bonwick  */
265b24ab676SJeff Bonwick typedef struct ztest_od {
266b24ab676SJeff Bonwick 	uint64_t	od_dir;
267b24ab676SJeff Bonwick 	uint64_t	od_object;
268b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
269b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
270b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
271b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
272b24ab676SJeff Bonwick 	uint64_t	od_gen;
273b24ab676SJeff Bonwick 	uint64_t	od_crgen;
2749adfa60dSMatthew Ahrens 	char		od_name[ZFS_MAX_DATASET_NAME_LEN];
275b24ab676SJeff Bonwick } ztest_od_t;
276fa9e4066Sahrens 
277b24ab676SJeff Bonwick /*
278b24ab676SJeff Bonwick  * Per-dataset state.
279b24ab676SJeff Bonwick  */
280b24ab676SJeff Bonwick typedef struct ztest_ds {
281420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
282b24ab676SJeff Bonwick 	objset_t	*zd_os;
283c9ba2a43SEric Schrock 	rwlock_t	zd_zilog_lock;
284b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
285b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
2869adfa60dSMatthew Ahrens 	char		zd_name[ZFS_MAX_DATASET_NAME_LEN];
287b24ab676SJeff Bonwick 	mutex_t		zd_dirobj_lock;
288b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
289b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
290b24ab676SJeff Bonwick } ztest_ds_t;
291b24ab676SJeff Bonwick 
292b24ab676SJeff Bonwick /*
293b24ab676SJeff Bonwick  * Per-iteration state.
294b24ab676SJeff Bonwick  */
295b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
296b24ab676SJeff Bonwick 
297b24ab676SJeff Bonwick typedef struct ztest_info {
298b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
299b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
300b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
301b24ab676SJeff Bonwick } ztest_info_t;
302fa9e4066Sahrens 
303420dfc95SChris Siden typedef struct ztest_shared_callstate {
304420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
305420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
306420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
307420dfc95SChris Siden } ztest_shared_callstate_t;
308420dfc95SChris Siden 
309420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
310420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
311420dfc95SChris Siden 
312fa9e4066Sahrens /*
313fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
314fa9e4066Sahrens  */
315fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
316fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
317fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
318d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
319fa9e4066Sahrens ztest_func_t ztest_zap;
320fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
321b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
322c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
323b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
324fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
325b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
326b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
327fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
328b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
329b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
330fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
331fa9e4066Sahrens ztest_func_t ztest_fault_inject;
332b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
333b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
334e14bb325SJeff Bonwick ztest_func_t ztest_spa_rename;
335b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
336b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
337fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
338fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
339fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
340e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3411195e687SMark J Musante ztest_func_t ztest_split_pool;
342e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
34325345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
344*5cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_device_removal;
345*5cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_remap_blocks;
346fa9e4066Sahrens 
347b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
348b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
349b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
350b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
351b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
352fa9e4066Sahrens 
353fa9e4066Sahrens ztest_info_t ztest_info[] = {
354e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
355b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
356e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
357b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
358e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
359e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3601195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
361b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
362c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
363b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
364b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
365b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
366b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
367b24ab676SJeff Bonwick #if 0
368b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
369b24ab676SJeff Bonwick #endif
370b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
371b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
372b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
373e05725b1Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
374b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
3755c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
3762c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
377e05725b1Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
378b24ab676SJeff Bonwick 	{ ztest_scrub,				1,	&zopt_rarely	},
37925345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
380b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
3813b2aab18SMatthew Ahrens 	{ ztest_vdev_attach_detach,		1,	&zopt_sometimes	},
382e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
383420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
384420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
385420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
386420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
387*5cabbc6bSPrashanth Sreenivasa 	{ ztest_device_removal,			1,	&zopt_sometimes	},
388*5cabbc6bSPrashanth Sreenivasa 	{ ztest_remap_blocks,			1,	&zopt_sometimes }
389fa9e4066Sahrens };
390fa9e4066Sahrens 
391fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
392fa9e4066Sahrens 
393d20e665cSRicardo M. Correia /*
394d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
395d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
396d20e665cSRicardo M. Correia  */
397d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
398d20e665cSRicardo M. Correia 	mutex_t	zcl_callbacks_lock;
399d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
400d20e665cSRicardo M. Correia } ztest_cb_list_t;
401d20e665cSRicardo M. Correia 
402fa9e4066Sahrens /*
403fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
404fa9e4066Sahrens  */
405fa9e4066Sahrens typedef struct ztest_shared {
406420dfc95SChris Siden 	boolean_t	zs_do_init;
407b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
408b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
409b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
410b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
411b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
412b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
41388ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
414e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
415fa9e4066Sahrens 	uint64_t	zs_alloc;
416fa9e4066Sahrens 	uint64_t	zs_space;
4171195e687SMark J Musante 	uint64_t	zs_splits;
4181195e687SMark J Musante 	uint64_t	zs_mirrors;
419420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
420420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
421420dfc95SChris Siden 	uint64_t	zs_guid;
422fa9e4066Sahrens } ztest_shared_t;
423fa9e4066Sahrens 
424b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
425b24ab676SJeff Bonwick 
426fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
427e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
428b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
429fa9e4066Sahrens 
430420dfc95SChris Siden static spa_t *ztest_spa = NULL;
431420dfc95SChris Siden static ztest_ds_t *ztest_ds;
432420dfc95SChris Siden 
433420dfc95SChris Siden static mutex_t ztest_vdev_lock;
434dfbb9432SGeorge Wilson 
435dfbb9432SGeorge Wilson /*
436dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
437dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
438dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
439dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
440dfbb9432SGeorge Wilson  */
441420dfc95SChris Siden static rwlock_t ztest_name_lock;
442fa9e4066Sahrens 
443420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
444e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4450a4e9518Sgw 
446d20e665cSRicardo M. Correia /* Global commit callback list */
447d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
448d20e665cSRicardo M. Correia 
449b24ab676SJeff Bonwick enum ztest_object {
450b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
451b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
452b24ab676SJeff Bonwick 	ZTEST_OBJECTS
453b24ab676SJeff Bonwick };
454fa9e4066Sahrens 
4551ce825d8Sraf static void usage(boolean_t) __NORETURN;
456f1b4288bSvb 
457fa9e4066Sahrens /*
458fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
459fa9e4066Sahrens  * debugging facilities.
460fa9e4066Sahrens  */
461fa9e4066Sahrens const char *
462fa9e4066Sahrens _umem_debug_init()
463fa9e4066Sahrens {
464fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
465fa9e4066Sahrens }
466fa9e4066Sahrens 
467fa9e4066Sahrens const char *
468fa9e4066Sahrens _umem_logging_init(void)
469fa9e4066Sahrens {
470fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
471fa9e4066Sahrens }
472fa9e4066Sahrens 
473fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
474fa9e4066Sahrens 
475fa9e4066Sahrens char *fatal_msg;
476fa9e4066Sahrens 
477fa9e4066Sahrens static void
478fa9e4066Sahrens fatal(int do_perror, char *message, ...)
479fa9e4066Sahrens {
480fa9e4066Sahrens 	va_list args;
481fa9e4066Sahrens 	int save_errno = errno;
482fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
483fa9e4066Sahrens 
484fa9e4066Sahrens 	(void) fflush(stdout);
485fa9e4066Sahrens 
486fa9e4066Sahrens 	va_start(args, message);
487fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
488fa9e4066Sahrens 	/* LINTED */
489fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
490fa9e4066Sahrens 	va_end(args);
491fa9e4066Sahrens 	if (do_perror) {
492fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
493fa9e4066Sahrens 		    ": %s", strerror(save_errno));
494fa9e4066Sahrens 	}
495fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
496fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
497fa9e4066Sahrens 	if (ztest_dump_core)
498fa9e4066Sahrens 		abort();
499fa9e4066Sahrens 	exit(3);
500fa9e4066Sahrens }
501fa9e4066Sahrens 
502fa9e4066Sahrens static int
503fa9e4066Sahrens str2shift(const char *buf)
504fa9e4066Sahrens {
505fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
506fa9e4066Sahrens 	int i;
507fa9e4066Sahrens 
508fa9e4066Sahrens 	if (buf[0] == '\0')
509fa9e4066Sahrens 		return (0);
510fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
511fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
512fa9e4066Sahrens 			break;
513fa9e4066Sahrens 	}
514f1b4288bSvb 	if (i == strlen(ends)) {
515f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
516f1b4288bSvb 		    buf);
517f1b4288bSvb 		usage(B_FALSE);
518f1b4288bSvb 	}
519fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
520fa9e4066Sahrens 		return (10*i);
521fa9e4066Sahrens 	}
522f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
523f1b4288bSvb 	usage(B_FALSE);
524f1b4288bSvb 	/* NOTREACHED */
525fa9e4066Sahrens }
526fa9e4066Sahrens 
527fa9e4066Sahrens static uint64_t
528fa9e4066Sahrens nicenumtoull(const char *buf)
529fa9e4066Sahrens {
530fa9e4066Sahrens 	char *end;
531fa9e4066Sahrens 	uint64_t val;
532fa9e4066Sahrens 
533fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
534fa9e4066Sahrens 	if (end == buf) {
535f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
536f1b4288bSvb 		usage(B_FALSE);
537fa9e4066Sahrens 	} else if (end[0] == '.') {
538fa9e4066Sahrens 		double fval = strtod(buf, &end);
539fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
540f1b4288bSvb 		if (fval > UINT64_MAX) {
541f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
542f1b4288bSvb 			    buf);
543f1b4288bSvb 			usage(B_FALSE);
544f1b4288bSvb 		}
545fa9e4066Sahrens 		val = (uint64_t)fval;
546fa9e4066Sahrens 	} else {
547fa9e4066Sahrens 		int shift = str2shift(end);
548f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
549f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
550f1b4288bSvb 			    buf);
551f1b4288bSvb 			usage(B_FALSE);
552f1b4288bSvb 		}
553fa9e4066Sahrens 		val <<= shift;
554fa9e4066Sahrens 	}
555fa9e4066Sahrens 	return (val);
556fa9e4066Sahrens }
557fa9e4066Sahrens 
558fa9e4066Sahrens static void
559f1b4288bSvb usage(boolean_t requested)
560fa9e4066Sahrens {
561420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
562420dfc95SChris Siden 
5630a055120SJason King 	char nice_vdev_size[NN_NUMBUF_SZ];
5640a055120SJason King 	char nice_gang_bang[NN_NUMBUF_SZ];
565f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
566fa9e4066Sahrens 
5670a055120SJason King 	nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
5680a055120SJason King 	nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang,
5690a055120SJason King 	    sizeof (nice_gang_bang));
570fa9e4066Sahrens 
571f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
572fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
573fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
5742215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
575fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
576fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
57799653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
578fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
579fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
580fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
5812215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
5822215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
583fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
5842215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
5852215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
5862215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
5872215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
5882215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
5892215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
590420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
5910e60744cSPavel Zakharov 	    "\t[-o variable=value] ... set global variable to an unsigned\n"
5920e60744cSPavel Zakharov 	    "\t    32-bit integer value\n"
593f1b4288bSvb 	    "\t[-h] (print help)\n"
594fa9e4066Sahrens 	    "",
595420dfc95SChris Siden 	    zo->zo_pool,
596420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
5970a4e9518Sgw 	    nice_vdev_size,				/* -s */
598420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
599420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
600420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
601420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
602420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
603420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
6040a4e9518Sgw 	    nice_gang_bang,				/* -g */
605420dfc95SChris Siden 	    zo->zo_init,				/* -i */
606420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
607420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
608420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
609420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
610420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
611420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
612f1b4288bSvb 	exit(requested ? 0 : 1);
613fa9e4066Sahrens }
614fa9e4066Sahrens 
615fa9e4066Sahrens static void
616fa9e4066Sahrens process_options(int argc, char **argv)
617fa9e4066Sahrens {
618420dfc95SChris Siden 	char *path;
619420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
620420dfc95SChris Siden 
621fa9e4066Sahrens 	int opt;
622fa9e4066Sahrens 	uint64_t value;
623420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
624fa9e4066Sahrens 
625420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
626fa9e4066Sahrens 
627fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
6280e60744cSPavel Zakharov 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
629fa9e4066Sahrens 		value = 0;
630fa9e4066Sahrens 		switch (opt) {
6313d7072f8Seschrock 		case 'v':
6323d7072f8Seschrock 		case 's':
6333d7072f8Seschrock 		case 'a':
6343d7072f8Seschrock 		case 'm':
6353d7072f8Seschrock 		case 'r':
6363d7072f8Seschrock 		case 'R':
6373d7072f8Seschrock 		case 'd':
6383d7072f8Seschrock 		case 't':
6393d7072f8Seschrock 		case 'g':
6403d7072f8Seschrock 		case 'i':
6413d7072f8Seschrock 		case 'k':
6423d7072f8Seschrock 		case 'T':
6433d7072f8Seschrock 		case 'P':
6442215e990SMark J Musante 		case 'F':
645fa9e4066Sahrens 			value = nicenumtoull(optarg);
646fa9e4066Sahrens 		}
647fa9e4066Sahrens 		switch (opt) {
6483d7072f8Seschrock 		case 'v':
649420dfc95SChris Siden 			zo->zo_vdevs = value;
650fa9e4066Sahrens 			break;
6513d7072f8Seschrock 		case 's':
652420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
653fa9e4066Sahrens 			break;
6543d7072f8Seschrock 		case 'a':
655420dfc95SChris Siden 			zo->zo_ashift = value;
656ecc2d604Sbonwick 			break;
6573d7072f8Seschrock 		case 'm':
658420dfc95SChris Siden 			zo->zo_mirrors = value;
659fa9e4066Sahrens 			break;
6603d7072f8Seschrock 		case 'r':
661420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
662fa9e4066Sahrens 			break;
6633d7072f8Seschrock 		case 'R':
664420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
66599653d4eSeschrock 			break;
6663d7072f8Seschrock 		case 'd':
667420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
668fa9e4066Sahrens 			break;
6693d7072f8Seschrock 		case 't':
670420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
671fa9e4066Sahrens 			break;
6723d7072f8Seschrock 		case 'g':
673420dfc95SChris Siden 			zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
674420dfc95SChris Siden 			    value);
675fa9e4066Sahrens 			break;
6763d7072f8Seschrock 		case 'i':
677420dfc95SChris Siden 			zo->zo_init = value;
678fa9e4066Sahrens 			break;
6793d7072f8Seschrock 		case 'k':
680420dfc95SChris Siden 			zo->zo_killrate = value;
681fa9e4066Sahrens 			break;
6823d7072f8Seschrock 		case 'p':
683420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
684420dfc95SChris Siden 			    sizeof (zo->zo_pool));
685fa9e4066Sahrens 			break;
6863d7072f8Seschrock 		case 'f':
687420dfc95SChris Siden 			path = realpath(optarg, NULL);
688420dfc95SChris Siden 			if (path == NULL) {
689420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
690420dfc95SChris Siden 				    optarg, strerror(errno));
691420dfc95SChris Siden 				usage(B_FALSE);
692420dfc95SChris Siden 			} else {
693420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
694420dfc95SChris Siden 				    sizeof (zo->zo_dir));
695420dfc95SChris Siden 			}
696fa9e4066Sahrens 			break;
6973d7072f8Seschrock 		case 'V':
698420dfc95SChris Siden 			zo->zo_verbose++;
699fa9e4066Sahrens 			break;
7003d7072f8Seschrock 		case 'E':
701420dfc95SChris Siden 			zo->zo_init = 0;
702fa9e4066Sahrens 			break;
7033d7072f8Seschrock 		case 'T':
704420dfc95SChris Siden 			zo->zo_time = value;
705fa9e4066Sahrens 			break;
7063d7072f8Seschrock 		case 'P':
707420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
708fa9e4066Sahrens 			break;
7092215e990SMark J Musante 		case 'F':
710420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
711420dfc95SChris Siden 			break;
712420dfc95SChris Siden 		case 'B':
713420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7142215e990SMark J Musante 			break;
7150e60744cSPavel Zakharov 		case 'o':
7160e60744cSPavel Zakharov 			if (set_global_var(optarg) != 0)
7170e60744cSPavel Zakharov 				usage(B_FALSE);
7180e60744cSPavel Zakharov 			break;
7193d7072f8Seschrock 		case 'h':
720f1b4288bSvb 			usage(B_TRUE);
721f1b4288bSvb 			break;
7223d7072f8Seschrock 		case '?':
7233d7072f8Seschrock 		default:
724f1b4288bSvb 			usage(B_FALSE);
725fa9e4066Sahrens 			break;
726fa9e4066Sahrens 		}
727fa9e4066Sahrens 	}
728fa9e4066Sahrens 
729420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
73099653d4eSeschrock 
731420dfc95SChris Siden 	zo->zo_vdevtime =
732420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
733b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
734420dfc95SChris Siden 
735420dfc95SChris Siden 	if (strlen(altdir) > 0) {
736741652b0SEtienne Dechamps 		char *cmd;
737741652b0SEtienne Dechamps 		char *realaltdir;
738420dfc95SChris Siden 		char *bin;
739420dfc95SChris Siden 		char *ztest;
740420dfc95SChris Siden 		char *isa;
741420dfc95SChris Siden 		int isalen;
742420dfc95SChris Siden 
743741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
744741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
745741652b0SEtienne Dechamps 
746741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
747420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
748420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
749420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
750420dfc95SChris Siden 			    altdir);
751420dfc95SChris Siden 		}
752420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
753420dfc95SChris Siden 
754420dfc95SChris Siden 		/*
755420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
756420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
757420dfc95SChris Siden 		 * 32 or 64 bit binaries.
758420dfc95SChris Siden 		 */
759420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
760420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
761420dfc95SChris Siden 		isa = bin + 9;
762420dfc95SChris Siden 		isalen = ztest - isa;
763420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
764420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
765420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
766420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
767420dfc95SChris Siden 
768420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
769420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
770420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
771420dfc95SChris Siden 			    zo->zo_alt_ztest);
772420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
773420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
774420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
775420dfc95SChris Siden 			    zo->zo_alt_libpath);
776420dfc95SChris Siden 		}
777741652b0SEtienne Dechamps 
778741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
779741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
780420dfc95SChris Siden 	}
781fa9e4066Sahrens }
782fa9e4066Sahrens 
783b24ab676SJeff Bonwick static void
784b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
785b24ab676SJeff Bonwick {
786420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
787420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
788b4952e17SGeorge Wilson 
789b4952e17SGeorge Wilson 	/*
790b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
791*5cabbc6bSPrashanth Sreenivasa 	 * See comment above spa_write_cachefile().
792b4952e17SGeorge Wilson 	 */
793b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
794*5cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
795b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
796b4952e17SGeorge Wilson 
797b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
798b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
799b24ab676SJeff Bonwick }
800b24ab676SJeff Bonwick 
801b24ab676SJeff Bonwick static uint64_t
802b24ab676SJeff Bonwick ztest_random(uint64_t range)
803b24ab676SJeff Bonwick {
804b24ab676SJeff Bonwick 	uint64_t r;
805b24ab676SJeff Bonwick 
806741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
807741652b0SEtienne Dechamps 
808b24ab676SJeff Bonwick 	if (range == 0)
809b24ab676SJeff Bonwick 		return (0);
810b24ab676SJeff Bonwick 
811741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
812b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
813b24ab676SJeff Bonwick 
814b24ab676SJeff Bonwick 	return (r % range);
815b24ab676SJeff Bonwick }
816b24ab676SJeff Bonwick 
817b24ab676SJeff Bonwick /* ARGSUSED */
818b24ab676SJeff Bonwick static void
819b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
820b24ab676SJeff Bonwick {
821b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
822b24ab676SJeff Bonwick }
823b24ab676SJeff Bonwick 
824ecc2d604Sbonwick static uint64_t
825ecc2d604Sbonwick ztest_get_ashift(void)
826ecc2d604Sbonwick {
827420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
8282a104a52SAlex Reece 		return (SPA_MINBLOCKSHIFT + ztest_random(5));
829420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
830ecc2d604Sbonwick }
831ecc2d604Sbonwick 
832fa9e4066Sahrens static nvlist_t *
83325345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
834fa9e4066Sahrens {
835e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
836fa9e4066Sahrens 	uint64_t vdev;
837fa9e4066Sahrens 	nvlist_t *file;
838fa9e4066Sahrens 
839e14bb325SJeff Bonwick 	if (ashift == 0)
840e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
841e14bb325SJeff Bonwick 
842e14bb325SJeff Bonwick 	if (path == NULL) {
843e14bb325SJeff Bonwick 		path = pathbuf;
844fa9e4066Sahrens 
845e14bb325SJeff Bonwick 		if (aux != NULL) {
846e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
847420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
848420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
84925345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
85025345e46SGeorge Wilson 			    aux, vdev);
851e14bb325SJeff Bonwick 		} else {
85288ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
853420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
854420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
85525345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
856e14bb325SJeff Bonwick 		}
857e14bb325SJeff Bonwick 	}
858e14bb325SJeff Bonwick 
859e14bb325SJeff Bonwick 	if (size != 0) {
860e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
861fa9e4066Sahrens 		if (fd == -1)
862e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
863fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
864e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
865fa9e4066Sahrens 		(void) close(fd);
866fa9e4066Sahrens 	}
867fa9e4066Sahrens 
868fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
869fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
870e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
871ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
872fa9e4066Sahrens 
873fa9e4066Sahrens 	return (file);
874fa9e4066Sahrens }
875fa9e4066Sahrens 
876fa9e4066Sahrens static nvlist_t *
87725345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
87825345e46SGeorge Wilson     uint64_t ashift, int r)
879fa9e4066Sahrens {
880fa9e4066Sahrens 	nvlist_t *raidz, **child;
881fa9e4066Sahrens 	int c;
882fa9e4066Sahrens 
883fa9e4066Sahrens 	if (r < 2)
88425345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
885fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
886fa9e4066Sahrens 
887fa9e4066Sahrens 	for (c = 0; c < r; c++)
88825345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
889fa9e4066Sahrens 
890fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
891fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
892fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
89399653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
894420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
895fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
896fa9e4066Sahrens 	    child, r) == 0);
897fa9e4066Sahrens 
898fa9e4066Sahrens 	for (c = 0; c < r; c++)
899fa9e4066Sahrens 		nvlist_free(child[c]);
900fa9e4066Sahrens 
901fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
902fa9e4066Sahrens 
903fa9e4066Sahrens 	return (raidz);
904fa9e4066Sahrens }
905fa9e4066Sahrens 
906fa9e4066Sahrens static nvlist_t *
90725345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
90825345e46SGeorge Wilson     uint64_t ashift, int r, int m)
909fa9e4066Sahrens {
910fa9e4066Sahrens 	nvlist_t *mirror, **child;
911fa9e4066Sahrens 	int c;
912fa9e4066Sahrens 
913fa9e4066Sahrens 	if (m < 1)
91425345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
915fa9e4066Sahrens 
916fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
917fa9e4066Sahrens 
918fa9e4066Sahrens 	for (c = 0; c < m; c++)
91925345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
920fa9e4066Sahrens 
921fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
922fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
923fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
924fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
925fa9e4066Sahrens 	    child, m) == 0);
926fa9e4066Sahrens 
927fa9e4066Sahrens 	for (c = 0; c < m; c++)
928fa9e4066Sahrens 		nvlist_free(child[c]);
929fa9e4066Sahrens 
930fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
931fa9e4066Sahrens 
932fa9e4066Sahrens 	return (mirror);
933fa9e4066Sahrens }
934fa9e4066Sahrens 
935fa9e4066Sahrens static nvlist_t *
93625345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
93725345e46SGeorge Wilson     int log, int r, int m, int t)
938fa9e4066Sahrens {
939fa9e4066Sahrens 	nvlist_t *root, **child;
940fa9e4066Sahrens 	int c;
941fa9e4066Sahrens 
942fa9e4066Sahrens 	ASSERT(t > 0);
943fa9e4066Sahrens 
944fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
945fa9e4066Sahrens 
94631157203SJeff Bonwick 	for (c = 0; c < t; c++) {
94725345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
94825345e46SGeorge Wilson 		    r, m);
94931157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
95031157203SJeff Bonwick 		    log) == 0);
95131157203SJeff Bonwick 	}
952fa9e4066Sahrens 
953fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
954fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
955e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
956fa9e4066Sahrens 	    child, t) == 0);
957fa9e4066Sahrens 
958fa9e4066Sahrens 	for (c = 0; c < t; c++)
959fa9e4066Sahrens 		nvlist_free(child[c]);
960fa9e4066Sahrens 
961fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
962fa9e4066Sahrens 
963fa9e4066Sahrens 	return (root);
964fa9e4066Sahrens }
965fa9e4066Sahrens 
96625345e46SGeorge Wilson /*
96725345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
96825345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
96925345e46SGeorge Wilson  */
97025345e46SGeorge Wilson static uint64_t
97125345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
97225345e46SGeorge Wilson {
97325345e46SGeorge Wilson 	uint64_t version = initial_version;
97425345e46SGeorge Wilson 
97525345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
97625345e46SGeorge Wilson 		version = version +
97725345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
97825345e46SGeorge Wilson 	}
97925345e46SGeorge Wilson 
98025345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
98125345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
98225345e46SGeorge Wilson 
98325345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
98425345e46SGeorge Wilson 	return (version);
98525345e46SGeorge Wilson }
98625345e46SGeorge Wilson 
987b24ab676SJeff Bonwick static int
988b24ab676SJeff Bonwick ztest_random_blocksize(void)
989fa9e4066Sahrens {
990b5152584SMatthew Ahrens 	uint64_t block_shift;
991b5152584SMatthew Ahrens 	/*
992b5152584SMatthew Ahrens 	 * Choose a block size >= the ashift.
993b5152584SMatthew Ahrens 	 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
994b5152584SMatthew Ahrens 	 */
995b5152584SMatthew Ahrens 	int maxbs = SPA_OLD_MAXBLOCKSHIFT;
996b5152584SMatthew Ahrens 	if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
997b5152584SMatthew Ahrens 		maxbs = 20;
99881cd5c55SMatthew Ahrens 	block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
9992a104a52SAlex Reece 	return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1000b24ab676SJeff Bonwick }
1001fa9e4066Sahrens 
1002b24ab676SJeff Bonwick static int
1003b24ab676SJeff Bonwick ztest_random_ibshift(void)
1004b24ab676SJeff Bonwick {
1005b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
1006b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1007fa9e4066Sahrens }
1008fa9e4066Sahrens 
1009b24ab676SJeff Bonwick static uint64_t
1010b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1011fa9e4066Sahrens {
1012b24ab676SJeff Bonwick 	uint64_t top;
1013b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
1014b24ab676SJeff Bonwick 	vdev_t *tvd;
1015fa9e4066Sahrens 
1016b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1017fa9e4066Sahrens 
1018b24ab676SJeff Bonwick 	do {
1019b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
1020b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
1021*5cabbc6bSPrashanth Sreenivasa 	} while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1022b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1023fa9e4066Sahrens 
1024b24ab676SJeff Bonwick 	return (top);
1025fa9e4066Sahrens }
1026fa9e4066Sahrens 
1027b24ab676SJeff Bonwick static uint64_t
1028b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1029fa9e4066Sahrens {
1030b24ab676SJeff Bonwick 	uint64_t value;
1031b24ab676SJeff Bonwick 
1032b24ab676SJeff Bonwick 	do {
1033b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1034b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1035b24ab676SJeff Bonwick 
1036b24ab676SJeff Bonwick 	return (value);
1037fa9e4066Sahrens }
1038fa9e4066Sahrens 
1039fa9e4066Sahrens static int
1040b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1041b24ab676SJeff Bonwick     boolean_t inherit)
1042fa9e4066Sahrens {
1043b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1044b24ab676SJeff Bonwick 	const char *valname;
1045b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1046b24ab676SJeff Bonwick 	uint64_t curval;
1047fa9e4066Sahrens 	int error;
1048fa9e4066Sahrens 
10493b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
10503b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1051fa9e4066Sahrens 
1052b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1053b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1054fa9e4066Sahrens 		return (error);
1055fa9e4066Sahrens 	}
1056fb09f5aaSMadhav Suresh 	ASSERT0(error);
1057fa9e4066Sahrens 
10583b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1059b24ab676SJeff Bonwick 
1060420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1061b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1062b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1063b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1064fa9e4066Sahrens 	}
1065fa9e4066Sahrens 
1066fa9e4066Sahrens 	return (error);
1067fa9e4066Sahrens }
1068fa9e4066Sahrens 
1069fa9e4066Sahrens static int
1070420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1071fa9e4066Sahrens {
1072420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1073b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1074fa9e4066Sahrens 	int error;
1075fa9e4066Sahrens 
1076b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1077b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1078fa9e4066Sahrens 
1079b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1080b24ab676SJeff Bonwick 
1081b24ab676SJeff Bonwick 	nvlist_free(props);
1082b24ab676SJeff Bonwick 
1083b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1084b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1085fa9e4066Sahrens 		return (error);
1086fa9e4066Sahrens 	}
1087fb09f5aaSMadhav Suresh 	ASSERT0(error);
1088fa9e4066Sahrens 
1089fa9e4066Sahrens 	return (error);
1090fa9e4066Sahrens }
1091fa9e4066Sahrens 
1092b24ab676SJeff Bonwick static void
1093b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1094b24ab676SJeff Bonwick {
1095b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1096b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1097b24ab676SJeff Bonwick 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
1098b24ab676SJeff Bonwick 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
1099b24ab676SJeff Bonwick }
1100fa9e4066Sahrens 
1101b24ab676SJeff Bonwick static void
1102b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1103fa9e4066Sahrens {
1104b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1105b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1106b24ab676SJeff Bonwick 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
1107b24ab676SJeff Bonwick 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
1108b24ab676SJeff Bonwick }
1109fa9e4066Sahrens 
1110b24ab676SJeff Bonwick static void
1111b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1112b24ab676SJeff Bonwick {
1113b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
1114fa9e4066Sahrens 
1115b24ab676SJeff Bonwick 	if (type == RL_READER) {
1116b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1117b24ab676SJeff Bonwick 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
1118b24ab676SJeff Bonwick 		rll->rll_readers++;
1119b24ab676SJeff Bonwick 	} else {
1120b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1121b24ab676SJeff Bonwick 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
1122b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1123b24ab676SJeff Bonwick 	}
1124fa9e4066Sahrens 
1125b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
1126b24ab676SJeff Bonwick }
1127fa9e4066Sahrens 
1128b24ab676SJeff Bonwick static void
1129b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1130b24ab676SJeff Bonwick {
1131b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
1132fa9e4066Sahrens 
1133b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1134b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1135b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1136b24ab676SJeff Bonwick 	} else {
1137b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1138b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1139b24ab676SJeff Bonwick 		rll->rll_readers--;
1140b24ab676SJeff Bonwick 	}
1141fa9e4066Sahrens 
1142b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1143b24ab676SJeff Bonwick 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
1144b24ab676SJeff Bonwick 
1145b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
1146fa9e4066Sahrens }
1147fa9e4066Sahrens 
1148b24ab676SJeff Bonwick static void
1149b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
115031157203SJeff Bonwick {
1151b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
115231157203SJeff Bonwick 
1153b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1154b24ab676SJeff Bonwick }
115531157203SJeff Bonwick 
1156b24ab676SJeff Bonwick static void
1157b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1158b24ab676SJeff Bonwick {
1159b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
116031157203SJeff Bonwick 
1161b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
116231157203SJeff Bonwick }
116331157203SJeff Bonwick 
1164b24ab676SJeff Bonwick static rl_t *
1165b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1166b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
116788ecc943SGeorge Wilson {
1168b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1169b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1170b24ab676SJeff Bonwick 	rl_t *rl;
117188ecc943SGeorge Wilson 
1172b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1173b24ab676SJeff Bonwick 	rl->rl_object = object;
1174b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1175b24ab676SJeff Bonwick 	rl->rl_size = size;
1176b24ab676SJeff Bonwick 	rl->rl_lock = rll;
117788ecc943SGeorge Wilson 
1178b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
117988ecc943SGeorge Wilson 
1180b24ab676SJeff Bonwick 	return (rl);
118188ecc943SGeorge Wilson }
118288ecc943SGeorge Wilson 
1183b24ab676SJeff Bonwick static void
1184b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1185fa9e4066Sahrens {
1186b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1187fa9e4066Sahrens 
1188b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1189fa9e4066Sahrens 
1190b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1191b24ab676SJeff Bonwick }
1192fa9e4066Sahrens 
1193b24ab676SJeff Bonwick static void
1194420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1195b24ab676SJeff Bonwick {
1196b24ab676SJeff Bonwick 	zd->zd_os = os;
1197b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1198420dfc95SChris Siden 	zd->zd_shared = szd;
1199b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1200fa9e4066Sahrens 
1201420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1202420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1203420dfc95SChris Siden 
1204c9ba2a43SEric Schrock 	VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1205b24ab676SJeff Bonwick 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
1206b24ab676SJeff Bonwick 
1207b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1208b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1209b24ab676SJeff Bonwick 
1210b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1211b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1212b24ab676SJeff Bonwick }
1213b24ab676SJeff Bonwick 
1214b24ab676SJeff Bonwick static void
1215b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1216b24ab676SJeff Bonwick {
1217b24ab676SJeff Bonwick 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1218b24ab676SJeff Bonwick 
1219b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1220b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1221b24ab676SJeff Bonwick 
1222b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1223b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1224b24ab676SJeff Bonwick }
1225b24ab676SJeff Bonwick 
1226b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1227b24ab676SJeff Bonwick 
1228b24ab676SJeff Bonwick static uint64_t
1229b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1230b24ab676SJeff Bonwick {
1231b24ab676SJeff Bonwick 	uint64_t txg;
1232b24ab676SJeff Bonwick 	int error;
1233b24ab676SJeff Bonwick 
1234b24ab676SJeff Bonwick 	/*
1235b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1236b24ab676SJeff Bonwick 	 */
1237b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1238b24ab676SJeff Bonwick 	if (error) {
1239b24ab676SJeff Bonwick 		if (error == ERESTART) {
1240b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1241b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1242b24ab676SJeff Bonwick 		} else {
1243b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1244b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1245b24ab676SJeff Bonwick 		}
1246b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1247b24ab676SJeff Bonwick 		return (0);
1248b24ab676SJeff Bonwick 	}
1249b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1250b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1251b24ab676SJeff Bonwick 	return (txg);
1252b24ab676SJeff Bonwick }
1253b24ab676SJeff Bonwick 
1254b24ab676SJeff Bonwick static void
1255b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1256b24ab676SJeff Bonwick {
1257b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1258b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1259b24ab676SJeff Bonwick 
1260b24ab676SJeff Bonwick 	while (ip < ip_end)
1261b24ab676SJeff Bonwick 		*ip++ = value;
1262b24ab676SJeff Bonwick }
1263b24ab676SJeff Bonwick 
1264b24ab676SJeff Bonwick static boolean_t
1265b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1266b24ab676SJeff Bonwick {
1267b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1268b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1269b24ab676SJeff Bonwick 	uint64_t diff = 0;
1270b24ab676SJeff Bonwick 
1271b24ab676SJeff Bonwick 	while (ip < ip_end)
1272b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1273b24ab676SJeff Bonwick 
1274b24ab676SJeff Bonwick 	return (diff == 0);
1275b24ab676SJeff Bonwick }
1276b24ab676SJeff Bonwick 
1277b24ab676SJeff Bonwick static void
1278b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1279b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1280b24ab676SJeff Bonwick {
1281b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1282b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1283b24ab676SJeff Bonwick 	bt->bt_object = object;
1284b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1285b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1286b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1287b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1288b24ab676SJeff Bonwick }
1289b24ab676SJeff Bonwick 
1290b24ab676SJeff Bonwick static void
1291b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1292b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1293b24ab676SJeff Bonwick {
12945d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
12955d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
12965d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
12975d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
12985d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
12995d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
13005d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1301b24ab676SJeff Bonwick }
1302b24ab676SJeff Bonwick 
1303b24ab676SJeff Bonwick static ztest_block_tag_t *
1304b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1305b24ab676SJeff Bonwick {
1306b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1307b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1308b24ab676SJeff Bonwick 
1309b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1310b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1311b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1312b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1313b24ab676SJeff Bonwick 
1314b24ab676SJeff Bonwick 	return (bt);
1315b24ab676SJeff Bonwick }
1316b24ab676SJeff Bonwick 
1317b24ab676SJeff Bonwick /*
1318b24ab676SJeff Bonwick  * ZIL logging ops
1319b24ab676SJeff Bonwick  */
1320b24ab676SJeff Bonwick 
1321b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1322b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1323b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1324b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
1325b24ab676SJeff Bonwick #define	lrz_bonuslen	lr_crtime[1]
1326b24ab676SJeff Bonwick 
13275002558fSNeil Perrin static void
1328b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1329b24ab676SJeff Bonwick {
1330b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1331b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1332b24ab676SJeff Bonwick 	itx_t *itx;
1333b24ab676SJeff Bonwick 
1334b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13355002558fSNeil Perrin 		return;
1336b24ab676SJeff Bonwick 
1337b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1338b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1339b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1340b24ab676SJeff Bonwick 
13415002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1342b24ab676SJeff Bonwick }
1343b24ab676SJeff Bonwick 
13445002558fSNeil Perrin static void
13455002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1346b24ab676SJeff Bonwick {
1347b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1348b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1349b24ab676SJeff Bonwick 	itx_t *itx;
1350b24ab676SJeff Bonwick 
1351b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13525002558fSNeil Perrin 		return;
1353b24ab676SJeff Bonwick 
1354b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1355b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1356b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1357b24ab676SJeff Bonwick 
135851bd2f97SNeil Perrin 	itx->itx_oid = object;
13595002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1360b24ab676SJeff Bonwick }
1361b24ab676SJeff Bonwick 
13625002558fSNeil Perrin static void
1363b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1364b24ab676SJeff Bonwick {
1365b24ab676SJeff Bonwick 	itx_t *itx;
1366b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1367b24ab676SJeff Bonwick 
1368b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13695002558fSNeil Perrin 		return;
1370b24ab676SJeff Bonwick 
1371b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1372b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1373b24ab676SJeff Bonwick 
1374b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1375b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1376b24ab676SJeff Bonwick 
1377b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1378b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1379b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1380b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1381b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1382b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1383b24ab676SJeff Bonwick 	}
1384b24ab676SJeff Bonwick 	itx->itx_private = zd;
1385b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1386b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1387b24ab676SJeff Bonwick 
1388b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1389b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1390b24ab676SJeff Bonwick 
13915002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1392b24ab676SJeff Bonwick }
1393b24ab676SJeff Bonwick 
13945002558fSNeil Perrin static void
1395b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1396b24ab676SJeff Bonwick {
1397b24ab676SJeff Bonwick 	itx_t *itx;
1398b24ab676SJeff Bonwick 
1399b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
14005002558fSNeil Perrin 		return;
1401b24ab676SJeff Bonwick 
1402b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1403b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1404b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1405b24ab676SJeff Bonwick 
14065002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
14075002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1408b24ab676SJeff Bonwick }
1409b24ab676SJeff Bonwick 
14105002558fSNeil Perrin static void
1411b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1412b24ab676SJeff Bonwick {
1413b24ab676SJeff Bonwick 	itx_t *itx;
1414b24ab676SJeff Bonwick 
1415b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
14165002558fSNeil Perrin 		return;
1417b24ab676SJeff Bonwick 
1418b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1419b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1420b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1421b24ab676SJeff Bonwick 
14225002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
14235002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1424b24ab676SJeff Bonwick }
1425b24ab676SJeff Bonwick 
1426b24ab676SJeff Bonwick /*
1427b24ab676SJeff Bonwick  * ZIL replay ops
1428b24ab676SJeff Bonwick  */
1429b24ab676SJeff Bonwick static int
14303f7978d0SAlan Somers ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1431b24ab676SJeff Bonwick {
14323f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
14333f7978d0SAlan Somers 	lr_create_t *lr = arg2;
1434b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1435b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1436b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1437b24ab676SJeff Bonwick 	dmu_buf_t *db;
1438b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1439b24ab676SJeff Bonwick 	uint64_t txg;
1440b24ab676SJeff Bonwick 	int error = 0;
1441b24ab676SJeff Bonwick 
1442b24ab676SJeff Bonwick 	if (byteswap)
1443b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1444b24ab676SJeff Bonwick 
1445b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1446b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1447b24ab676SJeff Bonwick 
1448b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1449b24ab676SJeff Bonwick 
1450b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1451b24ab676SJeff Bonwick 
1452b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1453b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1454b24ab676SJeff Bonwick 	} else {
1455b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1456b24ab676SJeff Bonwick 	}
1457b24ab676SJeff Bonwick 
1458b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1459b24ab676SJeff Bonwick 	if (txg == 0)
1460b24ab676SJeff Bonwick 		return (ENOSPC);
1461b24ab676SJeff Bonwick 
1462b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1463b24ab676SJeff Bonwick 
1464b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1465b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1466b24ab676SJeff Bonwick 			lr->lr_foid = zap_create(os,
1467b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1468b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1469b24ab676SJeff Bonwick 		} else {
1470b24ab676SJeff Bonwick 			error = zap_create_claim(os, lr->lr_foid,
1471b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1472b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1473b24ab676SJeff Bonwick 		}
1474b24ab676SJeff Bonwick 	} else {
1475b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1476b24ab676SJeff Bonwick 			lr->lr_foid = dmu_object_alloc(os,
1477b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1478b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1479b24ab676SJeff Bonwick 		} else {
1480b24ab676SJeff Bonwick 			error = dmu_object_claim(os, lr->lr_foid,
1481b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1482b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1483b24ab676SJeff Bonwick 		}
1484b24ab676SJeff Bonwick 	}
1485b24ab676SJeff Bonwick 
1486b24ab676SJeff Bonwick 	if (error) {
1487b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1488b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1489b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1490b24ab676SJeff Bonwick 		return (error);
1491b24ab676SJeff Bonwick 	}
1492b24ab676SJeff Bonwick 
1493b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1494b24ab676SJeff Bonwick 
1495b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1496b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1497b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1498b24ab676SJeff Bonwick 
1499b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1500b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1501b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1502b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1503b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1504b24ab676SJeff Bonwick 
1505b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1506b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1507b24ab676SJeff Bonwick 
1508b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1509b24ab676SJeff Bonwick 
1510b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1511b24ab676SJeff Bonwick 
1512b24ab676SJeff Bonwick 	return (0);
1513b24ab676SJeff Bonwick }
1514b24ab676SJeff Bonwick 
1515b24ab676SJeff Bonwick static int
15163f7978d0SAlan Somers ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1517b24ab676SJeff Bonwick {
15183f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
15193f7978d0SAlan Somers 	lr_remove_t *lr = arg2;
1520b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1521b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1522b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1523b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1524b24ab676SJeff Bonwick 	uint64_t object, txg;
1525b24ab676SJeff Bonwick 
1526b24ab676SJeff Bonwick 	if (byteswap)
1527b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1528b24ab676SJeff Bonwick 
1529b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1530b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1531b24ab676SJeff Bonwick 
1532b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1533b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1534b24ab676SJeff Bonwick 	ASSERT(object != 0);
1535b24ab676SJeff Bonwick 
1536b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1537b24ab676SJeff Bonwick 
1538b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1539b24ab676SJeff Bonwick 
1540b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1541b24ab676SJeff Bonwick 
1542b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1543b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1544b24ab676SJeff Bonwick 
1545b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1546b24ab676SJeff Bonwick 	if (txg == 0) {
1547b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1548b24ab676SJeff Bonwick 		return (ENOSPC);
1549b24ab676SJeff Bonwick 	}
1550b24ab676SJeff Bonwick 
1551b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1552b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1553b24ab676SJeff Bonwick 	} else {
1554b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1555b24ab676SJeff Bonwick 	}
1556b24ab676SJeff Bonwick 
1557b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1558b24ab676SJeff Bonwick 
15595002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1560b24ab676SJeff Bonwick 
1561b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1562b24ab676SJeff Bonwick 
1563b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1564b24ab676SJeff Bonwick 
1565b24ab676SJeff Bonwick 	return (0);
1566b24ab676SJeff Bonwick }
1567b24ab676SJeff Bonwick 
1568b24ab676SJeff Bonwick static int
15693f7978d0SAlan Somers ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1570b24ab676SJeff Bonwick {
15713f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
15723f7978d0SAlan Somers 	lr_write_t *lr = arg2;
1573b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1574b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1575b24ab676SJeff Bonwick 	uint64_t offset, length;
1576b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1577b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1578b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1579b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1580b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1581b24ab676SJeff Bonwick 	dmu_buf_t *db;
1582b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1583b24ab676SJeff Bonwick 	rl_t *rl;
1584b24ab676SJeff Bonwick 
1585b24ab676SJeff Bonwick 	if (byteswap)
1586b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1587b24ab676SJeff Bonwick 
1588b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1589b24ab676SJeff Bonwick 	length = lr->lr_length;
1590b24ab676SJeff Bonwick 
1591b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1592b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1593b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1594b24ab676SJeff Bonwick 		if (length < blocksize) {
1595b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1596b24ab676SJeff Bonwick 			length = blocksize;
1597b24ab676SJeff Bonwick 		}
1598b24ab676SJeff Bonwick 	}
1599b24ab676SJeff Bonwick 
1600b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1601b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1602b24ab676SJeff Bonwick 
1603b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1604b24ab676SJeff Bonwick 		bt = NULL;
1605b24ab676SJeff Bonwick 
1606b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1607b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1608b24ab676SJeff Bonwick 
1609b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1610b24ab676SJeff Bonwick 
1611b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1612b24ab676SJeff Bonwick 
1613b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1614b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1615b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1616b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1617b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1618b24ab676SJeff Bonwick 
1619b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1620b24ab676SJeff Bonwick 
1621b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1622b24ab676SJeff Bonwick 
1623b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1624b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1625b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1626b24ab676SJeff Bonwick 
1627b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1628b24ab676SJeff Bonwick 	if (txg == 0) {
1629b24ab676SJeff Bonwick 		if (abuf != NULL)
1630b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1631b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1632b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1633b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1634b24ab676SJeff Bonwick 		return (ENOSPC);
1635b24ab676SJeff Bonwick 	}
1636b24ab676SJeff Bonwick 
1637b24ab676SJeff Bonwick 	if (bt != NULL) {
1638b24ab676SJeff Bonwick 		/*
1639b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1640b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1641b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1642b24ab676SJeff Bonwick 		 */
1643b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1644b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1645b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1646b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1647b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1648b24ab676SJeff Bonwick 
1649b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1650b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1651b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
1652b24ab676SJeff Bonwick 				ztest_bt_verify(&rbt, os, lr->lr_foid,
1653b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1654b24ab676SJeff Bonwick 			}
1655b24ab676SJeff Bonwick 		}
1656b24ab676SJeff Bonwick 
1657b24ab676SJeff Bonwick 		/*
1658b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1659b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1660b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1661b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1662b24ab676SJeff Bonwick 		 */
1663b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
1664b24ab676SJeff Bonwick 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
1665b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1666b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1667b24ab676SJeff Bonwick 		}
1668b24ab676SJeff Bonwick 
1669b24ab676SJeff Bonwick 		/*
1670b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1671b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1672b24ab676SJeff Bonwick 		 */
1673b24ab676SJeff Bonwick 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1674b24ab676SJeff Bonwick 	}
1675b24ab676SJeff Bonwick 
1676b24ab676SJeff Bonwick 	if (abuf == NULL) {
1677b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1678b24ab676SJeff Bonwick 	} else {
1679b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1680b24ab676SJeff Bonwick 		dmu_assign_arcbuf(db, offset, abuf, tx);
1681b24ab676SJeff Bonwick 	}
1682b24ab676SJeff Bonwick 
1683b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1684b24ab676SJeff Bonwick 
1685b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1686b24ab676SJeff Bonwick 
1687b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1688b24ab676SJeff Bonwick 
1689b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1690b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1691b24ab676SJeff Bonwick 
1692b24ab676SJeff Bonwick 	return (0);
1693b24ab676SJeff Bonwick }
1694b24ab676SJeff Bonwick 
1695b24ab676SJeff Bonwick static int
16963f7978d0SAlan Somers ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1697b24ab676SJeff Bonwick {
16983f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
16993f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
1700b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1701b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1702b24ab676SJeff Bonwick 	uint64_t txg;
1703b24ab676SJeff Bonwick 	rl_t *rl;
1704b24ab676SJeff Bonwick 
1705b24ab676SJeff Bonwick 	if (byteswap)
1706b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1707b24ab676SJeff Bonwick 
1708b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1709b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1710b24ab676SJeff Bonwick 	    RL_WRITER);
1711b24ab676SJeff Bonwick 
1712b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1713b24ab676SJeff Bonwick 
1714b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1715b24ab676SJeff Bonwick 
1716b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1717b24ab676SJeff Bonwick 	if (txg == 0) {
1718b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1719b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1720b24ab676SJeff Bonwick 		return (ENOSPC);
1721b24ab676SJeff Bonwick 	}
1722b24ab676SJeff Bonwick 
1723b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1724b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1725b24ab676SJeff Bonwick 
1726b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1727b24ab676SJeff Bonwick 
1728b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1729b24ab676SJeff Bonwick 
1730b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1731b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1732b24ab676SJeff Bonwick 
1733b24ab676SJeff Bonwick 	return (0);
1734b24ab676SJeff Bonwick }
1735b24ab676SJeff Bonwick 
1736b24ab676SJeff Bonwick static int
17373f7978d0SAlan Somers ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1738b24ab676SJeff Bonwick {
17393f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17403f7978d0SAlan Somers 	lr_setattr_t *lr = arg2;
1741b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1742b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1743b24ab676SJeff Bonwick 	dmu_buf_t *db;
1744b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1745b24ab676SJeff Bonwick 	uint64_t txg, lrtxg, crtxg;
1746b24ab676SJeff Bonwick 
1747b24ab676SJeff Bonwick 	if (byteswap)
1748b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1749b24ab676SJeff Bonwick 
1750b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1751b24ab676SJeff Bonwick 
1752b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1753b24ab676SJeff Bonwick 
1754b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1755b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1756b24ab676SJeff Bonwick 
1757b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1758b24ab676SJeff Bonwick 	if (txg == 0) {
1759b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1760b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1761b24ab676SJeff Bonwick 		return (ENOSPC);
1762b24ab676SJeff Bonwick 	}
1763b24ab676SJeff Bonwick 
1764b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1765b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1766b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1767b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1768b24ab676SJeff Bonwick 
1769b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1770b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1771b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1772b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1773b24ab676SJeff Bonwick 	} else {
1774b24ab676SJeff Bonwick 		/*
1775b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1776b24ab676SJeff Bonwick 		 */
1777b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1778b24ab676SJeff Bonwick 		    sizeof (*bbt);
1779b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
1780b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
1781b24ab676SJeff Bonwick 	}
1782b24ab676SJeff Bonwick 
1783b24ab676SJeff Bonwick 	/*
1784b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
1785b24ab676SJeff Bonwick 	 */
1786b24ab676SJeff Bonwick 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1787b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
1788b24ab676SJeff Bonwick 
1789b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1790b24ab676SJeff Bonwick 
1791b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1792b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
1793fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1794b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1795b24ab676SJeff Bonwick 
1796b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1797b24ab676SJeff Bonwick 
1798b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1799b24ab676SJeff Bonwick 
1800b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
1801b24ab676SJeff Bonwick 
1802b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1803b24ab676SJeff Bonwick 
1804b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1805b24ab676SJeff Bonwick 
1806b24ab676SJeff Bonwick 	return (0);
1807b24ab676SJeff Bonwick }
1808b24ab676SJeff Bonwick 
1809b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1810b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
1811b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
1812b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
1813b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
1814b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
1815b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
1816b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
1817b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
1818b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
1819b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
1820b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
1821b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
1822b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
1823b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
1824b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
1825b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
1826b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
1827b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
1828b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
1829b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
1830b24ab676SJeff Bonwick };
1831b24ab676SJeff Bonwick 
1832b24ab676SJeff Bonwick /*
1833b24ab676SJeff Bonwick  * ZIL get_data callbacks
1834b24ab676SJeff Bonwick  */
1835b24ab676SJeff Bonwick 
1836b24ab676SJeff Bonwick static void
1837b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
1838b24ab676SJeff Bonwick {
1839b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
1840b24ab676SJeff Bonwick 	uint64_t object = zgd->zgd_rl->rl_object;
1841b24ab676SJeff Bonwick 
1842b24ab676SJeff Bonwick 	if (zgd->zgd_db)
1843b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
1844b24ab676SJeff Bonwick 
1845b24ab676SJeff Bonwick 	ztest_range_unlock(zgd->zgd_rl);
1846b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1847b24ab676SJeff Bonwick 
1848b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
18491271e4b1SPrakash Surya 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1850b24ab676SJeff Bonwick 
1851b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
1852b24ab676SJeff Bonwick }
1853b24ab676SJeff Bonwick 
1854b24ab676SJeff Bonwick static int
18551271e4b1SPrakash Surya ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
18561271e4b1SPrakash Surya     zio_t *zio)
1857b24ab676SJeff Bonwick {
1858b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
1859b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1860b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
1861b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
1862b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
1863b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
1864b24ab676SJeff Bonwick 	uint64_t crtxg;
1865b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1866b24ab676SJeff Bonwick 	dmu_buf_t *db;
1867b24ab676SJeff Bonwick 	zgd_t *zgd;
1868b24ab676SJeff Bonwick 	int error;
1869b24ab676SJeff Bonwick 
18701271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
18711271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
18721271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
18731271e4b1SPrakash Surya 
1874b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
1875b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
1876b24ab676SJeff Bonwick 	if (error) {
1877b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1878b24ab676SJeff Bonwick 		return (error);
1879b24ab676SJeff Bonwick 	}
1880b24ab676SJeff Bonwick 
1881b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
1882b24ab676SJeff Bonwick 
1883b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
1884b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1885b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1886b24ab676SJeff Bonwick 		return (ENOENT);
1887b24ab676SJeff Bonwick 	}
1888b24ab676SJeff Bonwick 
1889b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1890b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1891b24ab676SJeff Bonwick 	db = NULL;
1892b24ab676SJeff Bonwick 
1893b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
18941271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
1895b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
1896b24ab676SJeff Bonwick 
1897b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
1898b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1899b24ab676SJeff Bonwick 		    RL_READER);
1900b24ab676SJeff Bonwick 
1901b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
1902b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1903b24ab676SJeff Bonwick 		ASSERT(error == 0);
1904b24ab676SJeff Bonwick 	} else {
1905b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
1906dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
1907b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
1908dfe73b3dSJeff Bonwick 		} else {
1909dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
1910dfe73b3dSJeff Bonwick 			offset = 0;
1911dfe73b3dSJeff Bonwick 		}
1912b24ab676SJeff Bonwick 
1913b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1914b24ab676SJeff Bonwick 		    RL_READER);
1915b24ab676SJeff Bonwick 
191647cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
191747cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1918b24ab676SJeff Bonwick 
1919b24ab676SJeff Bonwick 		if (error == 0) {
1920b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
192180901aeaSGeorge Wilson 
1922b24ab676SJeff Bonwick 			zgd->zgd_db = db;
1923b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
1924b24ab676SJeff Bonwick 
1925b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
1926b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
1927b24ab676SJeff Bonwick 
1928b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1929b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
1930b24ab676SJeff Bonwick 
1931b24ab676SJeff Bonwick 			if (error == 0)
1932b24ab676SJeff Bonwick 				return (0);
1933b24ab676SJeff Bonwick 		}
1934b24ab676SJeff Bonwick 	}
1935b24ab676SJeff Bonwick 
1936b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
1937b24ab676SJeff Bonwick 
1938b24ab676SJeff Bonwick 	return (error);
1939b24ab676SJeff Bonwick }
1940b24ab676SJeff Bonwick 
1941b24ab676SJeff Bonwick static void *
1942b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
1943b24ab676SJeff Bonwick {
1944b24ab676SJeff Bonwick 	char *lr;
1945b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1946b24ab676SJeff Bonwick 
1947b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1948b24ab676SJeff Bonwick 
1949b24ab676SJeff Bonwick 	if (name)
1950b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
1951b24ab676SJeff Bonwick 
1952b24ab676SJeff Bonwick 	return (lr);
1953b24ab676SJeff Bonwick }
1954b24ab676SJeff Bonwick 
1955b24ab676SJeff Bonwick void
1956b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
1957b24ab676SJeff Bonwick {
1958b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1959b24ab676SJeff Bonwick 
1960b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
1961b24ab676SJeff Bonwick }
1962b24ab676SJeff Bonwick 
1963b24ab676SJeff Bonwick /*
1964b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
1965b24ab676SJeff Bonwick  */
1966b24ab676SJeff Bonwick static int
1967b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1968b24ab676SJeff Bonwick {
1969b24ab676SJeff Bonwick 	int missing = 0;
1970b24ab676SJeff Bonwick 	int error;
1971b24ab676SJeff Bonwick 
1972b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1973b24ab676SJeff Bonwick 
1974b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
1975b24ab676SJeff Bonwick 		od->od_object = 0;
1976b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1977b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
1978b24ab676SJeff Bonwick 		if (error) {
1979b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
1980b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
1981b24ab676SJeff Bonwick 			missing++;
1982b24ab676SJeff Bonwick 		} else {
1983b24ab676SJeff Bonwick 			dmu_buf_t *db;
1984b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
1985b24ab676SJeff Bonwick 			dmu_object_info_t doi;
1986b24ab676SJeff Bonwick 
1987b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
1988b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
1989b24ab676SJeff Bonwick 
1990b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
1991b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1992b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
1993b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
1994b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
1995b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1996b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
1997b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
1998b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
1999b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
2000b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
2001b24ab676SJeff Bonwick 		}
2002b24ab676SJeff Bonwick 	}
2003b24ab676SJeff Bonwick 
2004b24ab676SJeff Bonwick 	return (missing);
2005b24ab676SJeff Bonwick }
2006b24ab676SJeff Bonwick 
2007b24ab676SJeff Bonwick static int
2008b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2009b24ab676SJeff Bonwick {
2010b24ab676SJeff Bonwick 	int missing = 0;
2011b24ab676SJeff Bonwick 
2012b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
2013b24ab676SJeff Bonwick 
2014b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2015b24ab676SJeff Bonwick 		if (missing) {
2016b24ab676SJeff Bonwick 			od->od_object = 0;
2017b24ab676SJeff Bonwick 			missing++;
2018b24ab676SJeff Bonwick 			continue;
2019b24ab676SJeff Bonwick 		}
2020b24ab676SJeff Bonwick 
2021b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2022b24ab676SJeff Bonwick 
2023b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2024b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
2025b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
2026b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
2027b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
2028b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2029b24ab676SJeff Bonwick 		lr->lrz_bonuslen = dmu_bonus_max();
2030b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
2031b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
2032b24ab676SJeff Bonwick 
2033b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2034b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2035b24ab676SJeff Bonwick 			od->od_object = 0;
2036b24ab676SJeff Bonwick 			missing++;
2037b24ab676SJeff Bonwick 		} else {
2038b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2039b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2040b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2041b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2042b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2043b24ab676SJeff Bonwick 		}
2044b24ab676SJeff Bonwick 
2045b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2046b24ab676SJeff Bonwick 	}
2047b24ab676SJeff Bonwick 
2048b24ab676SJeff Bonwick 	return (missing);
2049b24ab676SJeff Bonwick }
2050b24ab676SJeff Bonwick 
2051b24ab676SJeff Bonwick static int
2052b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2053b24ab676SJeff Bonwick {
2054b24ab676SJeff Bonwick 	int missing = 0;
2055b24ab676SJeff Bonwick 	int error;
2056b24ab676SJeff Bonwick 
2057b24ab676SJeff Bonwick 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
2058b24ab676SJeff Bonwick 
2059b24ab676SJeff Bonwick 	od += count - 1;
2060b24ab676SJeff Bonwick 
2061b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2062b24ab676SJeff Bonwick 		if (missing) {
2063b24ab676SJeff Bonwick 			missing++;
2064b24ab676SJeff Bonwick 			continue;
2065b24ab676SJeff Bonwick 		}
2066b24ab676SJeff Bonwick 
206780901aeaSGeorge Wilson 		/*
206880901aeaSGeorge Wilson 		 * No object was found.
206980901aeaSGeorge Wilson 		 */
2070b24ab676SJeff Bonwick 		if (od->od_object == 0)
2071b24ab676SJeff Bonwick 			continue;
2072b24ab676SJeff Bonwick 
2073b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2074b24ab676SJeff Bonwick 
2075b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2076b24ab676SJeff Bonwick 
2077b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2078b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2079b24ab676SJeff Bonwick 			missing++;
2080b24ab676SJeff Bonwick 		} else {
2081b24ab676SJeff Bonwick 			od->od_object = 0;
2082b24ab676SJeff Bonwick 		}
2083b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2084b24ab676SJeff Bonwick 	}
2085b24ab676SJeff Bonwick 
2086b24ab676SJeff Bonwick 	return (missing);
2087b24ab676SJeff Bonwick }
2088b24ab676SJeff Bonwick 
2089b24ab676SJeff Bonwick static int
2090b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2091b24ab676SJeff Bonwick     void *data)
2092b24ab676SJeff Bonwick {
2093b24ab676SJeff Bonwick 	lr_write_t *lr;
2094b24ab676SJeff Bonwick 	int error;
2095b24ab676SJeff Bonwick 
2096b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2097b24ab676SJeff Bonwick 
2098b24ab676SJeff Bonwick 	lr->lr_foid = object;
2099b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2100b24ab676SJeff Bonwick 	lr->lr_length = size;
2101b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2102b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2103b24ab676SJeff Bonwick 
2104b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2105b24ab676SJeff Bonwick 
2106b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2107b24ab676SJeff Bonwick 
2108b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2109b24ab676SJeff Bonwick 
2110b24ab676SJeff Bonwick 	return (error);
2111b24ab676SJeff Bonwick }
2112b24ab676SJeff Bonwick 
2113b24ab676SJeff Bonwick static int
2114b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2115b24ab676SJeff Bonwick {
2116b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2117b24ab676SJeff Bonwick 	int error;
2118b24ab676SJeff Bonwick 
2119b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2120b24ab676SJeff Bonwick 
2121b24ab676SJeff Bonwick 	lr->lr_foid = object;
2122b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2123b24ab676SJeff Bonwick 	lr->lr_length = size;
2124b24ab676SJeff Bonwick 
2125b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2126b24ab676SJeff Bonwick 
2127b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2128b24ab676SJeff Bonwick 
2129b24ab676SJeff Bonwick 	return (error);
2130b24ab676SJeff Bonwick }
2131b24ab676SJeff Bonwick 
2132b24ab676SJeff Bonwick static int
2133b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2134b24ab676SJeff Bonwick {
2135b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2136b24ab676SJeff Bonwick 	int error;
2137b24ab676SJeff Bonwick 
2138b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2139b24ab676SJeff Bonwick 
2140b24ab676SJeff Bonwick 	lr->lr_foid = object;
2141b24ab676SJeff Bonwick 	lr->lr_size = 0;
2142b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2143b24ab676SJeff Bonwick 
2144b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2145b24ab676SJeff Bonwick 
2146b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2147b24ab676SJeff Bonwick 
2148b24ab676SJeff Bonwick 	return (error);
2149b24ab676SJeff Bonwick }
2150b24ab676SJeff Bonwick 
2151b24ab676SJeff Bonwick static void
2152b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2153b24ab676SJeff Bonwick {
2154b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2155b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2156b24ab676SJeff Bonwick 	uint64_t txg;
2157b24ab676SJeff Bonwick 	rl_t *rl;
2158b24ab676SJeff Bonwick 
2159b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2160b24ab676SJeff Bonwick 
2161b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2162b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2163b24ab676SJeff Bonwick 
2164b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2165b24ab676SJeff Bonwick 
2166b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2167b24ab676SJeff Bonwick 
2168b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2169b24ab676SJeff Bonwick 
2170b24ab676SJeff Bonwick 	if (txg != 0) {
2171b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2172b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2173b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2174b24ab676SJeff Bonwick 	} else {
2175b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2176b24ab676SJeff Bonwick 	}
2177b24ab676SJeff Bonwick 
2178b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2179b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2180b24ab676SJeff Bonwick }
2181b24ab676SJeff Bonwick 
2182b24ab676SJeff Bonwick static void
2183b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2184b24ab676SJeff Bonwick {
218580901aeaSGeorge Wilson 	int err;
2186b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2187b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2188b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2189b24ab676SJeff Bonwick 	uint64_t blocksize;
2190b24ab676SJeff Bonwick 	void *data;
2191b24ab676SJeff Bonwick 
2192b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2193b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2194b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2195b24ab676SJeff Bonwick 
2196b24ab676SJeff Bonwick 	/*
2197b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2198b24ab676SJeff Bonwick 	 */
2199b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2200b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2201b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2202b24ab676SJeff Bonwick 
2203c9ba2a43SEric Schrock 	(void) rw_rdlock(&zd->zd_zilog_lock);
2204c9ba2a43SEric Schrock 
2205b24ab676SJeff Bonwick 	switch (io_type) {
2206b24ab676SJeff Bonwick 
2207b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
2208b24ab676SJeff Bonwick 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2209b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2210b24ab676SJeff Bonwick 		break;
2211b24ab676SJeff Bonwick 
2212b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2213b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2214b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2215b24ab676SJeff Bonwick 			/*
2216b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2217b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2218b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2219b24ab676SJeff Bonwick 			 */
2220b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2221b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2222b24ab676SJeff Bonwick 		}
2223b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2224b24ab676SJeff Bonwick 		break;
2225b24ab676SJeff Bonwick 
2226b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2227b24ab676SJeff Bonwick 		bzero(data, blocksize);
2228b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2229b24ab676SJeff Bonwick 		break;
2230b24ab676SJeff Bonwick 
2231b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2232b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2233b24ab676SJeff Bonwick 		break;
2234b24ab676SJeff Bonwick 
2235b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2236b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2237b24ab676SJeff Bonwick 		break;
223880901aeaSGeorge Wilson 
223980901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
224080901aeaSGeorge Wilson 		(void) rw_rdlock(&ztest_name_lock);
224180901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
224280901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
224380901aeaSGeorge Wilson 		    B_FALSE);
224480901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
224580901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
224680901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
224780901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
224880901aeaSGeorge Wilson 		    B_FALSE);
224980901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
225080901aeaSGeorge Wilson 		(void) rw_unlock(&ztest_name_lock);
225180901aeaSGeorge Wilson 
225280901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
225380901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
225480901aeaSGeorge Wilson 
225580901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
225680901aeaSGeorge Wilson 		break;
2257b24ab676SJeff Bonwick 	}
2258b24ab676SJeff Bonwick 
2259c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2260c9ba2a43SEric Schrock 
2261b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2262b24ab676SJeff Bonwick }
2263b24ab676SJeff Bonwick 
2264b24ab676SJeff Bonwick /*
2265b24ab676SJeff Bonwick  * Initialize an object description template.
2266b24ab676SJeff Bonwick  */
2267b24ab676SJeff Bonwick static void
2268b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2269b24ab676SJeff Bonwick     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2270b24ab676SJeff Bonwick {
2271b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2272b24ab676SJeff Bonwick 	od->od_object = 0;
2273b24ab676SJeff Bonwick 
2274b24ab676SJeff Bonwick 	od->od_crtype = type;
2275b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2276b24ab676SJeff Bonwick 	od->od_crgen = gen;
2277b24ab676SJeff Bonwick 
2278b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2279b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2280b24ab676SJeff Bonwick 	od->od_gen = 0;
2281b24ab676SJeff Bonwick 
2282b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2283b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2284b24ab676SJeff Bonwick }
2285b24ab676SJeff Bonwick 
2286b24ab676SJeff Bonwick /*
2287b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2288b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2289b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2290b24ab676SJeff Bonwick  * use the existing objects.
2291b24ab676SJeff Bonwick  */
2292b24ab676SJeff Bonwick static int
2293b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2294b24ab676SJeff Bonwick {
2295b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2296b24ab676SJeff Bonwick 	int rv = 0;
2297b24ab676SJeff Bonwick 
2298b24ab676SJeff Bonwick 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2299b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2300b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2301b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2302b24ab676SJeff Bonwick 		rv = -1;
2303b24ab676SJeff Bonwick 	zd->zd_od = od;
2304b24ab676SJeff Bonwick 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2305b24ab676SJeff Bonwick 
2306b24ab676SJeff Bonwick 	return (rv);
2307b24ab676SJeff Bonwick }
2308b24ab676SJeff Bonwick 
2309b24ab676SJeff Bonwick /* ARGSUSED */
2310b24ab676SJeff Bonwick void
2311b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2312b24ab676SJeff Bonwick {
2313b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2314b24ab676SJeff Bonwick 
2315c9ba2a43SEric Schrock 	(void) rw_rdlock(&zd->zd_zilog_lock);
2316c9ba2a43SEric Schrock 
23175002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2318b24ab676SJeff Bonwick 
2319b24ab676SJeff Bonwick 	/*
2320b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2321b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2322b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2323b24ab676SJeff Bonwick 	 */
2324b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2325420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2326420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2327420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2328b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2329c9ba2a43SEric Schrock 
2330c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2331c9ba2a43SEric Schrock }
2332c9ba2a43SEric Schrock 
2333c9ba2a43SEric Schrock /*
2334c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2335c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2336c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2337c9ba2a43SEric Schrock  */
2338c9ba2a43SEric Schrock /* ARGSUSED */
2339c9ba2a43SEric Schrock void
2340c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2341c9ba2a43SEric Schrock {
2342c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2343c9ba2a43SEric Schrock 
234480901aeaSGeorge Wilson 	/*
234580901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
234680901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
234780901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
234880901aeaSGeorge Wilson 	 */
234980901aeaSGeorge Wilson 	VERIFY0(mutex_lock(&zd->zd_dirobj_lock));
2350c9ba2a43SEric Schrock 	(void) rw_wrlock(&zd->zd_zilog_lock);
2351c9ba2a43SEric Schrock 
2352c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2353c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2354c9ba2a43SEric Schrock 
2355c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2356c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2357c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2358c9ba2a43SEric Schrock 
2359c9ba2a43SEric Schrock 	(void) rw_unlock(&zd->zd_zilog_lock);
2360ce636f8bSMatthew Ahrens 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2361b24ab676SJeff Bonwick }
2362b24ab676SJeff Bonwick 
2363b24ab676SJeff Bonwick /*
2364b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2365b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2366b24ab676SJeff Bonwick  */
2367b24ab676SJeff Bonwick /* ARGSUSED */
2368b24ab676SJeff Bonwick void
2369b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2370b24ab676SJeff Bonwick {
2371420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2372b24ab676SJeff Bonwick 	spa_t *spa;
2373b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2374b24ab676SJeff Bonwick 
2375b24ab676SJeff Bonwick 	/*
2376b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2377b24ab676SJeff Bonwick 	 */
237825345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2379b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23804445fffbSMatthew Ahrens 	    spa_create("ztest_bad_file", nvroot, NULL, NULL));
2381b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2382b24ab676SJeff Bonwick 
2383b24ab676SJeff Bonwick 	/*
2384b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2385b24ab676SJeff Bonwick 	 */
238625345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2387b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23884445fffbSMatthew Ahrens 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2389b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2390b24ab676SJeff Bonwick 
2391b24ab676SJeff Bonwick 	/*
2392b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2393b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2394b24ab676SJeff Bonwick 	 */
2395420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
239625345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
23974445fffbSMatthew Ahrens 	VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2398b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2399b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2400420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2401b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2402b24ab676SJeff Bonwick 
2403420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
2404b24ab676SJeff Bonwick }
2405b24ab676SJeff Bonwick 
240625345e46SGeorge Wilson /* ARGSUSED */
240725345e46SGeorge Wilson void
240825345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
240925345e46SGeorge Wilson {
241025345e46SGeorge Wilson 	spa_t *spa;
241125345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
241225345e46SGeorge Wilson 	uint64_t version, newversion;
241325345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
241425345e46SGeorge Wilson 	char *name;
241525345e46SGeorge Wilson 
241625345e46SGeorge Wilson 	VERIFY0(mutex_lock(&ztest_vdev_lock));
241725345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
241825345e46SGeorge Wilson 
241925345e46SGeorge Wilson 	/*
242025345e46SGeorge Wilson 	 * Clean up from previous runs.
242125345e46SGeorge Wilson 	 */
242225345e46SGeorge Wilson 	(void) spa_destroy(name);
242325345e46SGeorge Wilson 
242425345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
242525345e46SGeorge Wilson 	    0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
242625345e46SGeorge Wilson 
242725345e46SGeorge Wilson 	/*
242825345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
242925345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
243025345e46SGeorge Wilson 	 */
243125345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
243225345e46SGeorge Wilson 	case 0:
243325345e46SGeorge Wilson 	case 1:
243425345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
243525345e46SGeorge Wilson 		break;
243625345e46SGeorge Wilson 	case 2:
243725345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
243825345e46SGeorge Wilson 		break;
243925345e46SGeorge Wilson 	case 3:
244025345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
244125345e46SGeorge Wilson 		break;
244225345e46SGeorge Wilson 	}
244325345e46SGeorge Wilson 
244425345e46SGeorge Wilson 	/*
244525345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
244625345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
244725345e46SGeorge Wilson 	 */
244825345e46SGeorge Wilson 	do {
244925345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
245025345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
245125345e46SGeorge Wilson 
245225345e46SGeorge Wilson 	props = fnvlist_alloc();
245325345e46SGeorge Wilson 	fnvlist_add_uint64(props,
245425345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
245525345e46SGeorge Wilson 	VERIFY0(spa_create(name, nvroot, props, NULL));
245625345e46SGeorge Wilson 	fnvlist_free(nvroot);
245725345e46SGeorge Wilson 	fnvlist_free(props);
245825345e46SGeorge Wilson 
245925345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
246025345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
246125345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
246225345e46SGeorge Wilson 
246325345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
246425345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
246525345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
246625345e46SGeorge Wilson 	}
246725345e46SGeorge Wilson 
246825345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
246925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
247025345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
247125345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
247225345e46SGeorge Wilson 	spa_close(spa, FTAG);
247325345e46SGeorge Wilson 
247425345e46SGeorge Wilson 	strfree(name);
247525345e46SGeorge Wilson 	VERIFY0(mutex_unlock(&ztest_vdev_lock));
247625345e46SGeorge Wilson }
247725345e46SGeorge Wilson 
2478b24ab676SJeff Bonwick static vdev_t *
2479b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2480b24ab676SJeff Bonwick {
2481b24ab676SJeff Bonwick 	vdev_t *mvd;
2482b24ab676SJeff Bonwick 
2483b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2484b24ab676SJeff Bonwick 		return (vd);
2485b24ab676SJeff Bonwick 
2486b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2487b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2488b24ab676SJeff Bonwick 		    NULL)
2489b24ab676SJeff Bonwick 			return (mvd);
2490b24ab676SJeff Bonwick 
2491b24ab676SJeff Bonwick 	return (NULL);
2492b24ab676SJeff Bonwick }
2493b24ab676SJeff Bonwick 
2494b24ab676SJeff Bonwick /*
2495b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2496b24ab676SJeff Bonwick  */
2497b24ab676SJeff Bonwick int
2498b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2499b24ab676SJeff Bonwick {
2500b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2501b24ab676SJeff Bonwick 	int c;
2502b24ab676SJeff Bonwick 
2503b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2504b24ab676SJeff Bonwick 
2505b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2506b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2507b24ab676SJeff Bonwick 
2508b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2509b24ab676SJeff Bonwick 			break;
2510b24ab676SJeff Bonwick 	}
2511b24ab676SJeff Bonwick 	return (c);
2512b24ab676SJeff Bonwick }
2513b24ab676SJeff Bonwick 
2514b24ab676SJeff Bonwick /*
2515b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2516b24ab676SJeff Bonwick  */
2517b24ab676SJeff Bonwick /* ARGSUSED */
2518b24ab676SJeff Bonwick void
2519b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2520b24ab676SJeff Bonwick {
2521b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2522420dfc95SChris Siden 	spa_t *spa = ztest_spa;
25231195e687SMark J Musante 	uint64_t leaves;
2524b24ab676SJeff Bonwick 	uint64_t guid;
2525b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2526b24ab676SJeff Bonwick 	int error;
2527b24ab676SJeff Bonwick 
2528420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
25293b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2530b24ab676SJeff Bonwick 
2531b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2532b24ab676SJeff Bonwick 
2533b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2534b24ab676SJeff Bonwick 
2535b24ab676SJeff Bonwick 	/*
2536b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2537b24ab676SJeff Bonwick 	 */
253888ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
253988ecc943SGeorge Wilson 		/*
254088ecc943SGeorge Wilson 		 * Grab the guid from the head of the log class rotor.
254188ecc943SGeorge Wilson 		 */
2542b24ab676SJeff Bonwick 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
25438654d025Sperrin 
254488ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2545fa9e4066Sahrens 
254688ecc943SGeorge Wilson 		/*
254788ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
254888ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
254988ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
255088ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
255188ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
255288ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
255388ecc943SGeorge Wilson 		 */
2554420dfc95SChris Siden 		VERIFY(rw_wrlock(&ztest_name_lock) == 0);
255588ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2556420dfc95SChris Siden 		VERIFY(rw_unlock(&ztest_name_lock) == 0);
2557fa9e4066Sahrens 
255888ecc943SGeorge Wilson 		if (error && error != EEXIST)
255988ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
256088ecc943SGeorge Wilson 	} else {
256188ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
256288ecc943SGeorge Wilson 
256388ecc943SGeorge Wilson 		/*
256488ecc943SGeorge Wilson 		 * Make 1/4 of the devices be log devices.
256588ecc943SGeorge Wilson 		 */
256625345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2567420dfc95SChris Siden 		    ztest_opts.zo_vdev_size, 0,
2568420dfc95SChris Siden 		    ztest_random(4) == 0, ztest_opts.zo_raidz,
2569420dfc95SChris Siden 		    zs->zs_mirrors, 1);
257088ecc943SGeorge Wilson 
257188ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
257288ecc943SGeorge Wilson 		nvlist_free(nvroot);
257388ecc943SGeorge Wilson 
257488ecc943SGeorge Wilson 		if (error == ENOSPC)
257588ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
257688ecc943SGeorge Wilson 		else if (error != 0)
257788ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
257888ecc943SGeorge Wilson 	}
257988ecc943SGeorge Wilson 
2580420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2581e14bb325SJeff Bonwick }
2582fa9e4066Sahrens 
2583e14bb325SJeff Bonwick /*
2584e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2585e14bb325SJeff Bonwick  */
2586b24ab676SJeff Bonwick /* ARGSUSED */
2587e14bb325SJeff Bonwick void
2588b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2589e14bb325SJeff Bonwick {
2590b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2591420dfc95SChris Siden 	spa_t *spa = ztest_spa;
259231157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2593e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
2594e14bb325SJeff Bonwick 	char *aux;
2595e14bb325SJeff Bonwick 	uint64_t guid = 0;
2596e14bb325SJeff Bonwick 	int error;
2597e14bb325SJeff Bonwick 
259831157203SJeff Bonwick 	if (ztest_random(2) == 0) {
2599e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
2600e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
2601e14bb325SJeff Bonwick 	} else {
2602e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
2603e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
2604e14bb325SJeff Bonwick 	}
2605e14bb325SJeff Bonwick 
2606420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
2607e14bb325SJeff Bonwick 
2608e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2609e14bb325SJeff Bonwick 
2610e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
2611e14bb325SJeff Bonwick 		/*
2612e14bb325SJeff Bonwick 		 * Pick a random device to remove.
2613e14bb325SJeff Bonwick 		 */
2614e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2615e14bb325SJeff Bonwick 	} else {
2616e14bb325SJeff Bonwick 		/*
2617e14bb325SJeff Bonwick 		 * Find an unused device we can add.
2618e14bb325SJeff Bonwick 		 */
2619b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
2620e14bb325SJeff Bonwick 		for (;;) {
2621e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
2622e14bb325SJeff Bonwick 			int c;
2623420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
2624420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2625420dfc95SChris Siden 			    zs->zs_vdev_aux);
2626e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
2627e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
2628e14bb325SJeff Bonwick 				    path) == 0)
2629e14bb325SJeff Bonwick 					break;
263031157203SJeff Bonwick 			if (c == sav->sav_count &&
263131157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
2632e14bb325SJeff Bonwick 				break;
2633b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
2634e14bb325SJeff Bonwick 		}
2635e14bb325SJeff Bonwick 	}
2636e14bb325SJeff Bonwick 
2637e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
2638e14bb325SJeff Bonwick 
2639e14bb325SJeff Bonwick 	if (guid == 0) {
2640e14bb325SJeff Bonwick 		/*
2641e14bb325SJeff Bonwick 		 * Add a new device.
2642e14bb325SJeff Bonwick 		 */
264325345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2644420dfc95SChris Siden 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2645e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
2646e14bb325SJeff Bonwick 		if (error != 0)
2647e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2648e14bb325SJeff Bonwick 		nvlist_free(nvroot);
2649e14bb325SJeff Bonwick 	} else {
2650e14bb325SJeff Bonwick 		/*
2651e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
2652e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
2653e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
2654e14bb325SJeff Bonwick 		 */
2655e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
2656573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
2657e14bb325SJeff Bonwick 
2658e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
2659e14bb325SJeff Bonwick 		if (error != 0 && error != EBUSY)
2660e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2661e14bb325SJeff Bonwick 	}
2662e14bb325SJeff Bonwick 
2663420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2664fa9e4066Sahrens }
2665fa9e4066Sahrens 
26661195e687SMark J Musante /*
26671195e687SMark J Musante  * split a pool if it has mirror tlvdevs
26681195e687SMark J Musante  */
26691195e687SMark J Musante /* ARGSUSED */
26701195e687SMark J Musante void
26711195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
26721195e687SMark J Musante {
26731195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
2674420dfc95SChris Siden 	spa_t *spa = ztest_spa;
26751195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
26761195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
26771195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
26781195e687SMark J Musante 	int error = 0;
26791195e687SMark J Musante 
2680420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
26811195e687SMark J Musante 
26821195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
2683420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2684420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
26851195e687SMark J Musante 		return;
26861195e687SMark J Musante 	}
26871195e687SMark J Musante 
26881195e687SMark J Musante 	/* clean up the old pool, if any */
26891195e687SMark J Musante 	(void) spa_destroy("splitp");
26901195e687SMark J Musante 
26911195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
26921195e687SMark J Musante 
26931195e687SMark J Musante 	/* generate a config from the existing config */
269498295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
26951195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
26961195e687SMark J Musante 	    &tree) == 0);
269798295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
269898295d61SMark J Musante 
26991195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
27001195e687SMark J Musante 	    &children) == 0);
27011195e687SMark J Musante 
27021195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
27031195e687SMark J Musante 	for (c = 0; c < children; c++) {
27041195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
27051195e687SMark J Musante 		nvlist_t **mchild;
27061195e687SMark J Musante 		uint_t mchildren;
27071195e687SMark J Musante 
27081195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
27091195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
27101195e687SMark J Musante 			    0) == 0);
27111195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
27121195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
27131195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
27141195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
27151195e687SMark J Musante 			if (lastlogid == 0)
27161195e687SMark J Musante 				lastlogid = schildren;
27171195e687SMark J Musante 			++schildren;
27181195e687SMark J Musante 			continue;
27191195e687SMark J Musante 		}
27201195e687SMark J Musante 		lastlogid = 0;
27211195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
27221195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
27231195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
27241195e687SMark J Musante 	}
27251195e687SMark J Musante 
27261195e687SMark J Musante 	/* OK, create a config that can be used to split */
27271195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
27281195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
27291195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
27301195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
27311195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
27321195e687SMark J Musante 
27331195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
27341195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
27351195e687SMark J Musante 
27361195e687SMark J Musante 	for (c = 0; c < schildren; c++)
27371195e687SMark J Musante 		nvlist_free(schild[c]);
27381195e687SMark J Musante 	free(schild);
27391195e687SMark J Musante 	nvlist_free(split);
27401195e687SMark J Musante 
27411195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
27421195e687SMark J Musante 
2743420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
27441195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2745420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
27461195e687SMark J Musante 
27471195e687SMark J Musante 	nvlist_free(config);
27481195e687SMark J Musante 
27491195e687SMark J Musante 	if (error == 0) {
27501195e687SMark J Musante 		(void) printf("successful split - results:\n");
27511195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
27521195e687SMark J Musante 		show_pool_stats(spa);
27531195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
27541195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
27551195e687SMark J Musante 		++zs->zs_splits;
27561195e687SMark J Musante 		--zs->zs_mirrors;
27571195e687SMark J Musante 	}
2758420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
27591195e687SMark J Musante 
27601195e687SMark J Musante }
27611195e687SMark J Musante 
2762fa9e4066Sahrens /*
2763fa9e4066Sahrens  * Verify that we can attach and detach devices.
2764fa9e4066Sahrens  */
2765b24ab676SJeff Bonwick /* ARGSUSED */
2766fa9e4066Sahrens void
2767b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2768fa9e4066Sahrens {
2769b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2770420dfc95SChris Siden 	spa_t *spa = ztest_spa;
277131157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
2772fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2773ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
2774e14bb325SJeff Bonwick 	nvlist_t *root;
27751195e687SMark J Musante 	uint64_t leaves;
2776fa9e4066Sahrens 	uint64_t leaf, top;
2777ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
27788ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
2779b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
2780ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2781fa9e4066Sahrens 	int replacing;
27829af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
278331157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
278431157203SJeff Bonwick 	int oldvd_is_log;
2785fa9e4066Sahrens 	int error, expected_error;
2786fa9e4066Sahrens 
2787420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
2788420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2789fa9e4066Sahrens 
2790*5cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2791*5cabbc6bSPrashanth Sreenivasa 
2792*5cabbc6bSPrashanth Sreenivasa 	/*
2793*5cabbc6bSPrashanth Sreenivasa 	 * If a vdev is in the process of being removed, its removal may
2794*5cabbc6bSPrashanth Sreenivasa 	 * finish while we are in progress, leading to an unexpected error
2795*5cabbc6bSPrashanth Sreenivasa 	 * value.  Don't bother trying to attach while we are in the middle
2796*5cabbc6bSPrashanth Sreenivasa 	 * of removal.
2797*5cabbc6bSPrashanth Sreenivasa 	 */
2798*5cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
2799*5cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
2800*5cabbc6bSPrashanth Sreenivasa 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2801*5cabbc6bSPrashanth Sreenivasa 		return;
2802*5cabbc6bSPrashanth Sreenivasa 	}
2803fa9e4066Sahrens 
2804fa9e4066Sahrens 	/*
2805fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
2806fa9e4066Sahrens 	 */
2807fa9e4066Sahrens 	replacing = ztest_random(2);
2808fa9e4066Sahrens 
2809fa9e4066Sahrens 	/*
2810fa9e4066Sahrens 	 * Pick a random top-level vdev.
2811fa9e4066Sahrens 	 */
2812b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
2813fa9e4066Sahrens 
2814fa9e4066Sahrens 	/*
2815fa9e4066Sahrens 	 * Pick a random leaf within it.
2816fa9e4066Sahrens 	 */
2817d41c4376SMark J Musante 	leaf = ztest_random(leaves);
2818fa9e4066Sahrens 
2819fa9e4066Sahrens 	/*
282031157203SJeff Bonwick 	 * Locate this vdev.
2821fa9e4066Sahrens 	 */
282231157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
28231195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
28248ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
28251195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2826420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
28278ad4d6ddSJeff Bonwick 	}
2828420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
28298ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2830420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2831420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
28328ad4d6ddSJeff Bonwick 	}
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 	/*
283531157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
283631157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
2837fa9e4066Sahrens 	 */
283831157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
28399af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
28408ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
28418ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
284231157203SJeff Bonwick 	}
2843fa9e4066Sahrens 
284431157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
2845573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
284631157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
284731157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
2848ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
28498ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
2850fa9e4066Sahrens 
28519af0a4dfSJeff Bonwick 	/*
28529af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
28539af0a4dfSJeff Bonwick 	 */
28549af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
2855*5cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
28568ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
28578ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
28588ad4d6ddSJeff Bonwick 		    error != ENOTSUP)
28598ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
2860420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
28619af0a4dfSJeff Bonwick 		return;
28629af0a4dfSJeff Bonwick 	}
28639af0a4dfSJeff Bonwick 
2864fa9e4066Sahrens 	/*
286531157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
286631157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2867fa9e4066Sahrens 	 */
286831157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
286931157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
287031157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
287131157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
287231157203SJeff Bonwick 	} else {
287331157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2874420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
2875420dfc95SChris Siden 		    top * leaves + leaf);
287631157203SJeff Bonwick 		if (ztest_random(2) == 0)
287731157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
287831157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
287931157203SJeff Bonwick 	}
288031157203SJeff Bonwick 
288131157203SJeff Bonwick 	if (newvd) {
2882*5cabbc6bSPrashanth Sreenivasa 		/*
2883*5cabbc6bSPrashanth Sreenivasa 		 * Reopen to ensure the vdev's asize field isn't stale.
2884*5cabbc6bSPrashanth Sreenivasa 		 */
2885*5cabbc6bSPrashanth Sreenivasa 		vdev_reopen(newvd);
2886573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
288731157203SJeff Bonwick 	} else {
288831157203SJeff Bonwick 		/*
288931157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
289031157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
289131157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
289231157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
289331157203SJeff Bonwick 		 */
289431157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
289531157203SJeff Bonwick 	}
2896fa9e4066Sahrens 
2897fa9e4066Sahrens 	/*
2898fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2899fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2900fa9e4066Sahrens 	 *
2901ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2902fa9e4066Sahrens 	 *
2903ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2904fa9e4066Sahrens 	 */
290531157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
290631157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
290731157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
290831157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
290931157203SJeff Bonwick 		expected_error = ENOTSUP;
291031157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
2911fa9e4066Sahrens 		expected_error = ENOTSUP;
291231157203SJeff Bonwick 	else if (newvd == oldvd)
291331157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
291431157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
291531157203SJeff Bonwick 		expected_error = EBUSY;
2916ea8dc4b6Seschrock 	else if (newsize < oldsize)
2917fa9e4066Sahrens 		expected_error = EOVERFLOW;
2918ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
2919ecc2d604Sbonwick 		expected_error = EDOM;
2920fa9e4066Sahrens 	else
2921fa9e4066Sahrens 		expected_error = 0;
2922fa9e4066Sahrens 
2923*5cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_ALL, FTAG);
2924fa9e4066Sahrens 
2925fa9e4066Sahrens 	/*
2926ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
2927fa9e4066Sahrens 	 */
292825345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2929e14bb325SJeff Bonwick 	    ashift, 0, 0, 0, 1);
2930fa9e4066Sahrens 
293131157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2932fa9e4066Sahrens 
2933fa9e4066Sahrens 	nvlist_free(root);
2934fa9e4066Sahrens 
2935fa9e4066Sahrens 	/*
2936fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2937fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2938fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2939fa9e4066Sahrens 	 */
2940fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
2941fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2942fa9e4066Sahrens 		expected_error = error;
2943fa9e4066Sahrens 
2944f0aa80d4Sbonwick 	/*
2945f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2946f0aa80d4Sbonwick 	 */
2947088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2948f0aa80d4Sbonwick 		expected_error = error;
2949f0aa80d4Sbonwick 
2950088f3894Sahrens 	/* XXX workaround 6690467 */
2951088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
2952088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
2953088f3894Sahrens 		    "returned %d, expected %d",
2954b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
2955b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
2956fa9e4066Sahrens 	}
2957fa9e4066Sahrens 
2958420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2959fa9e4066Sahrens }
2960fa9e4066Sahrens 
2961*5cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
2962*5cabbc6bSPrashanth Sreenivasa void
2963*5cabbc6bSPrashanth Sreenivasa ztest_device_removal(ztest_ds_t *zd, uint64_t id)
2964*5cabbc6bSPrashanth Sreenivasa {
2965*5cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ztest_spa;
2966*5cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
2967*5cabbc6bSPrashanth Sreenivasa 	uint64_t guid;
2968*5cabbc6bSPrashanth Sreenivasa 
2969*5cabbc6bSPrashanth Sreenivasa 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
2970*5cabbc6bSPrashanth Sreenivasa 
2971*5cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2972*5cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
2973*5cabbc6bSPrashanth Sreenivasa 	guid = vd->vdev_guid;
2974*5cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
2975*5cabbc6bSPrashanth Sreenivasa 
2976*5cabbc6bSPrashanth Sreenivasa 	(void) spa_vdev_remove(spa, guid, B_FALSE);
2977*5cabbc6bSPrashanth Sreenivasa 
2978*5cabbc6bSPrashanth Sreenivasa 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
2979*5cabbc6bSPrashanth Sreenivasa }
2980*5cabbc6bSPrashanth Sreenivasa 
2981573ca77eSGeorge Wilson /*
2982573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
2983573ca77eSGeorge Wilson  */
2984573ca77eSGeorge Wilson vdev_t *
2985573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
2986573ca77eSGeorge Wilson {
2987573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
2988573ca77eSGeorge Wilson 	size_t *newsize = arg;
2989573ca77eSGeorge Wilson 	size_t fsize;
2990573ca77eSGeorge Wilson 	int fd;
2991573ca77eSGeorge Wilson 
2992573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2993573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
2994573ca77eSGeorge Wilson 
2995573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2996573ca77eSGeorge Wilson 		return (vd);
2997573ca77eSGeorge Wilson 
2998573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
2999573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
3000573ca77eSGeorge Wilson 
3001420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3002573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
3003573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3004573ca77eSGeorge Wilson 	}
3005573ca77eSGeorge Wilson 	(void) close(fd);
3006573ca77eSGeorge Wilson 	return (NULL);
3007573ca77eSGeorge Wilson }
3008573ca77eSGeorge Wilson 
3009573ca77eSGeorge Wilson /*
3010573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
3011573ca77eSGeorge Wilson  */
3012573ca77eSGeorge Wilson /* ARGSUSED */
3013573ca77eSGeorge Wilson vdev_t *
3014573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
3015573ca77eSGeorge Wilson {
3016573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3017573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
3018573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
30198f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
3020095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3021095bcd66SGeorge Wilson 	int error;
3022573ca77eSGeorge Wilson 
3023573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3024573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3025573ca77eSGeorge Wilson 
3026573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
3027573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3028095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3029573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3030573ca77eSGeorge Wilson 
3031095bcd66SGeorge Wilson 	/*
3032095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
3033095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
3034095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
3035095bcd66SGeorge Wilson 	 */
3036095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
3037420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3038095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
3039095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
3040095bcd66SGeorge Wilson 		}
3041095bcd66SGeorge Wilson 		return (vd);
3042095bcd66SGeorge Wilson 	}
3043095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3044095bcd66SGeorge Wilson 
3045573ca77eSGeorge Wilson 	/*
3046573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
3047573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
3048573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
3049573ca77eSGeorge Wilson 	 * trying to online it.
3050573ca77eSGeorge Wilson 	 */
30518f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
3052420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
30538f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
30548f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
3055b24ab676SJeff Bonwick 			    "got gen %llu\n",
3056b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
30578f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
30588f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
30598f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
3060573ca77eSGeorge Wilson 		}
3061573ca77eSGeorge Wilson 		return (vd);
3062573ca77eSGeorge Wilson 	}
3063573ca77eSGeorge Wilson 	return (NULL);
3064573ca77eSGeorge Wilson }
3065573ca77eSGeorge Wilson 
3066573ca77eSGeorge Wilson /*
3067573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
3068573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
3069573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3070573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3071573ca77eSGeorge Wilson  * leaf vdev we encounter.
3072573ca77eSGeorge Wilson  */
3073573ca77eSGeorge Wilson vdev_t *
3074573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3075573ca77eSGeorge Wilson {
3076573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3077573ca77eSGeorge Wilson 		if (func == NULL)
3078573ca77eSGeorge Wilson 			return (vd);
3079573ca77eSGeorge Wilson 		else
3080573ca77eSGeorge Wilson 			return (func(vd, arg));
3081573ca77eSGeorge Wilson 	}
3082573ca77eSGeorge Wilson 
3083573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3084573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3085573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3086573ca77eSGeorge Wilson 			return (cvd);
3087573ca77eSGeorge Wilson 	}
3088573ca77eSGeorge Wilson 	return (NULL);
3089573ca77eSGeorge Wilson }
3090573ca77eSGeorge Wilson 
3091fa9e4066Sahrens /*
3092fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3093fa9e4066Sahrens  */
3094b24ab676SJeff Bonwick /* ARGSUSED */
3095fa9e4066Sahrens void
3096b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3097fa9e4066Sahrens {
3098420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3099b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3100b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3101b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3102573ca77eSGeorge Wilson 	size_t psize, newsize;
3103b24ab676SJeff Bonwick 	uint64_t top;
3104b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3105fa9e4066Sahrens 
3106420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
3107573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3108573ca77eSGeorge Wilson 
3109*5cabbc6bSPrashanth Sreenivasa 	/*
3110*5cabbc6bSPrashanth Sreenivasa 	 * If there is a vdev removal in progress, it could complete while
3111*5cabbc6bSPrashanth Sreenivasa 	 * we are running, in which case we would not be able to verify
3112*5cabbc6bSPrashanth Sreenivasa 	 * that the metaslab_class space increased (because it decreases
3113*5cabbc6bSPrashanth Sreenivasa 	 * when the device removal completes).
3114*5cabbc6bSPrashanth Sreenivasa 	 */
3115*5cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal != NULL) {
3116*5cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_STATE, FTAG);
3117*5cabbc6bSPrashanth Sreenivasa 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3118*5cabbc6bSPrashanth Sreenivasa 		return;
3119*5cabbc6bSPrashanth Sreenivasa 	}
3120*5cabbc6bSPrashanth Sreenivasa 
3121b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3122573ca77eSGeorge Wilson 
3123b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3124b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3125b24ab676SJeff Bonwick 	mc = mg->mg_class;
3126b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3127b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3128fa9e4066Sahrens 
3129fa9e4066Sahrens 	/*
3130573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3131573ca77eSGeorge Wilson 	 * our top-level device.
3132fa9e4066Sahrens 	 */
3133573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3134573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3135573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3136fa9e4066Sahrens 
3137573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3138fa9e4066Sahrens 
3139573ca77eSGeorge Wilson 	/*
31408f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
31418f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3142573ca77eSGeorge Wilson 	 */
31438f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3144420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3145573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3146420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3147573ca77eSGeorge Wilson 		return;
3148573ca77eSGeorge Wilson 	}
3149573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3150573ca77eSGeorge Wilson 	newsize = psize + psize / 8;
3151573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3152fa9e4066Sahrens 
3153420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3154b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3155573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3156573ca77eSGeorge Wilson 	}
3157573ca77eSGeorge Wilson 
3158573ca77eSGeorge Wilson 	/*
3159573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3160573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3161573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3162573ca77eSGeorge Wilson 	 */
3163573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3164573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3165573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3166420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3167b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3168b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3169b24ab676SJeff Bonwick 		}
3170b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3171420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3172b24ab676SJeff Bonwick 		return;
3173b24ab676SJeff Bonwick 	}
3174b24ab676SJeff Bonwick 
3175b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3176b24ab676SJeff Bonwick 
3177b24ab676SJeff Bonwick 	/*
3178b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3179b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3180b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3181b24ab676SJeff Bonwick 	 */
3182b24ab676SJeff Bonwick 	for (;;) {
3183b24ab676SJeff Bonwick 		boolean_t done;
3184b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3185b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3186b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3187b24ab676SJeff Bonwick 		if (done)
3188b24ab676SJeff Bonwick 			break;
3189b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3190b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3191b24ab676SJeff Bonwick 	}
3192b24ab676SJeff Bonwick 
3193b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3194b24ab676SJeff Bonwick 
3195b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3196b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3197b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3198b24ab676SJeff Bonwick 
3199b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3200420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3201b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3202b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3203fa9e4066Sahrens 		}
3204b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3205420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3206573ca77eSGeorge Wilson 		return;
3207fa9e4066Sahrens 	}
3208fa9e4066Sahrens 
3209573ca77eSGeorge Wilson 	/*
3210b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3211573ca77eSGeorge Wilson 	 */
3212*5cabbc6bSPrashanth Sreenivasa 	if (new_ms_count <= old_ms_count) {
3213*5cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3214b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
3215*5cabbc6bSPrashanth Sreenivasa 	}
3216573ca77eSGeorge Wilson 
3217573ca77eSGeorge Wilson 	/*
3218573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3219573ca77eSGeorge Wilson 	 */
3220*5cabbc6bSPrashanth Sreenivasa 	if (new_class_space <= old_class_space) {
3221*5cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3222b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
3223*5cabbc6bSPrashanth Sreenivasa 	}
3224b24ab676SJeff Bonwick 
3225420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
32260a055120SJason King 		char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3227573ca77eSGeorge Wilson 
32280a055120SJason King 		nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
32290a055120SJason King 		nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3230573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3231573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3232573ca77eSGeorge Wilson 	}
3233b24ab676SJeff Bonwick 
3234573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3235420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
3236fa9e4066Sahrens }
3237fa9e4066Sahrens 
3238b24ab676SJeff Bonwick /*
3239b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3240b24ab676SJeff Bonwick  */
3241fa9e4066Sahrens /* ARGSUSED */
3242fa9e4066Sahrens static void
3243b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3244fa9e4066Sahrens {
3245fa9e4066Sahrens 	/*
3246b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3247fa9e4066Sahrens 	 */
3248b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3249fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3250fa9e4066Sahrens }
3251fa9e4066Sahrens 
325255da60b9SMark J Musante static int
325355da60b9SMark J Musante ztest_dataset_create(char *dsname)
325455da60b9SMark J Musante {
325555da60b9SMark J Musante 	uint64_t zilset = ztest_random(100);
325655da60b9SMark J Musante 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
325755da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
325855da60b9SMark J Musante 
325955da60b9SMark J Musante 	if (err || zilset < 80)
326055da60b9SMark J Musante 		return (err);
326155da60b9SMark J Musante 
3262420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3263420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
326455da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
326555da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
326655da60b9SMark J Musante }
326755da60b9SMark J Musante 
3268b24ab676SJeff Bonwick /* ARGSUSED */
32691d452cf5Sahrens static int
3270fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3271fa9e4066Sahrens {
3272fa9e4066Sahrens 	objset_t *os;
3273b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3274fa9e4066Sahrens 	int error;
3275fa9e4066Sahrens 
3276fa9e4066Sahrens 	/*
3277fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3278fa9e4066Sahrens 	 */
32793b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3280b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3281e1930233Sbonwick 	if (error != ENOENT) {
3282e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3283fb09f5aaSMadhav Suresh 		ASSERT0(error);
3284b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3285b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3286e1930233Sbonwick 	}
32873b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3288fa9e4066Sahrens 
3289fa9e4066Sahrens 	/*
3290fa9e4066Sahrens 	 * Destroy the dataset.
3291fa9e4066Sahrens 	 */
32923b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
3293754998c8SChris Williamson 		VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
32943b2aab18SMatthew Ahrens 	} else {
3295754998c8SChris Williamson 		error = dsl_destroy_head(name);
3296754998c8SChris Williamson 		/* There could be a hold on this dataset */
3297754998c8SChris Williamson 		if (error != EBUSY)
3298754998c8SChris Williamson 			ASSERT0(error);
32993b2aab18SMatthew Ahrens 	}
33001d452cf5Sahrens 	return (0);
3301fa9e4066Sahrens }
3302fa9e4066Sahrens 
3303b24ab676SJeff Bonwick static boolean_t
3304b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3305fa9e4066Sahrens {
33069adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3307b24ab676SJeff Bonwick 	int error;
3308b24ab676SJeff Bonwick 
33093b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3310b24ab676SJeff Bonwick 
33113b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3312b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3313b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3314b24ab676SJeff Bonwick 		return (B_FALSE);
3315b24ab676SJeff Bonwick 	}
33163b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
33173b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
33183b2aab18SMatthew Ahrens 		    snapname, error);
33193b2aab18SMatthew Ahrens 	}
3320b24ab676SJeff Bonwick 	return (B_TRUE);
3321b24ab676SJeff Bonwick }
3322b24ab676SJeff Bonwick 
3323b24ab676SJeff Bonwick static boolean_t
3324b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3325b24ab676SJeff Bonwick {
33269adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3327b24ab676SJeff Bonwick 	int error;
3328b24ab676SJeff Bonwick 
33299adfa60dSMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3330b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3331b24ab676SJeff Bonwick 
33323b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3333b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3334b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3335b24ab676SJeff Bonwick 	return (B_TRUE);
3336fa9e4066Sahrens }
3337fa9e4066Sahrens 
3338b24ab676SJeff Bonwick /* ARGSUSED */
3339fa9e4066Sahrens void
3340b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3341fa9e4066Sahrens {
3342b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3343b24ab676SJeff Bonwick 	int iters;
3344fa9e4066Sahrens 	int error;
3345745cd3c5Smaybee 	objset_t *os, *os2;
33469adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3347fa9e4066Sahrens 	zilog_t *zilog;
3348fa9e4066Sahrens 
3349420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
3350b24ab676SJeff Bonwick 
33519adfa60dSMatthew Ahrens 	(void) snprintf(name, sizeof (name), "%s/temp_%llu",
3352420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3353fa9e4066Sahrens 
3354fa9e4066Sahrens 	/*
3355fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3356fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3357b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3358fa9e4066Sahrens 	 */
3359fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3360503ad85cSMatthew Ahrens 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3361420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3362b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3363b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3364503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
3365fa9e4066Sahrens 	}
3366fa9e4066Sahrens 
3367fa9e4066Sahrens 	/*
3368fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3369fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3370fa9e4066Sahrens 	 * and all of its snapshots.
3371fa9e4066Sahrens 	 */
3372b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
33730b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3374fa9e4066Sahrens 
3375fa9e4066Sahrens 	/*
3376fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3377fa9e4066Sahrens 	 */
33783b2aab18SMatthew Ahrens 	VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
33793b2aab18SMatthew Ahrens 	    FTAG, &os));
3380fa9e4066Sahrens 
3381fa9e4066Sahrens 	/*
3382fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3383fa9e4066Sahrens 	 */
338455da60b9SMark J Musante 	error = ztest_dataset_create(name);
3385fa9e4066Sahrens 	if (error) {
3386fa9e4066Sahrens 		if (error == ENOSPC) {
3387b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3388420dfc95SChris Siden 			(void) rw_unlock(&ztest_name_lock);
3389fa9e4066Sahrens 			return;
3390fa9e4066Sahrens 		}
3391fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3392fa9e4066Sahrens 	}
3393fa9e4066Sahrens 
33943b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3395b24ab676SJeff Bonwick 
3396420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3397fa9e4066Sahrens 
3398fa9e4066Sahrens 	/*
3399fa9e4066Sahrens 	 * Open the intent log for it.
3400fa9e4066Sahrens 	 */
3401b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3402fa9e4066Sahrens 
3403fa9e4066Sahrens 	/*
3404b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3405b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3406fa9e4066Sahrens 	 */
3407b24ab676SJeff Bonwick 	iters = ztest_random(5);
3408b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3409b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3410b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3411b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3412fa9e4066Sahrens 	}
3413fa9e4066Sahrens 
3414fa9e4066Sahrens 	/*
3415fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3416fa9e4066Sahrens 	 */
3417b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3418b24ab676SJeff Bonwick 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3419fa9e4066Sahrens 
3420fa9e4066Sahrens 	/*
3421503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3422745cd3c5Smaybee 	 */
3423b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3424503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3425503ad85cSMatthew Ahrens 
3426503ad85cSMatthew Ahrens 	/*
3427b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3428503ad85cSMatthew Ahrens 	 */
3429b24ab676SJeff Bonwick 	VERIFY3U(EBUSY, ==,
3430b24ab676SJeff Bonwick 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3431fa9e4066Sahrens 
3432fa9e4066Sahrens 	zil_close(zilog);
3433503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3434b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3435fa9e4066Sahrens 
3436420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
3437fa9e4066Sahrens }
3438fa9e4066Sahrens 
3439fa9e4066Sahrens /*
3440fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3441fa9e4066Sahrens  */
3442fa9e4066Sahrens void
3443b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3444fa9e4066Sahrens {
3445420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
3446b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
3447b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
3448420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
34494f5064b7SMark J Musante }
34504f5064b7SMark J Musante 
3451718d718aSGeorge Wilson /*
3452718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
3453718d718aSGeorge Wilson  */
3454718d718aSGeorge Wilson void
3455b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3456718d718aSGeorge Wilson {
34579adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
34589adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
34599adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
34609adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
34619adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3462718d718aSGeorge Wilson 	int error;
3463718d718aSGeorge Wilson 
34649adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
34659adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
34669adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
34679adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
34689adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
34699adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
34709adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
34719adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
34729adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
34739adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
3474718d718aSGeorge Wilson 
34753b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
3476718d718aSGeorge Wilson 	if (error && error != ENOENT)
34773b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
34783b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
3479718d718aSGeorge Wilson 	if (error && error != ENOENT)
34803b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
34813b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
3482718d718aSGeorge Wilson 	if (error && error != ENOENT)
34833b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
34843b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
3485718d718aSGeorge Wilson 	if (error && error != ENOENT)
34863b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
34873b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
3488718d718aSGeorge Wilson 	if (error && error != ENOENT)
34893b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3490718d718aSGeorge Wilson }
3491718d718aSGeorge Wilson 
34924f5064b7SMark J Musante /*
34934f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
34944f5064b7SMark J Musante  */
34954f5064b7SMark J Musante void
3496b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
34974f5064b7SMark J Musante {
34983b2aab18SMatthew Ahrens 	objset_t *os;
34999adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
35009adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
35019adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
35029adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
35039adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3504b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
3505b24ab676SJeff Bonwick 	int error;
35064f5064b7SMark J Musante 
3507420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
35084f5064b7SMark J Musante 
3509b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
3510718d718aSGeorge Wilson 
35119adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
35129adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
35139adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
35149adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
35159adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
35169adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
35179adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
35189adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
35199adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
35209adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
352105312e2cSMark J Musante 
35224445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3523b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3524b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3525b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3526b2d22b66SGeorge Wilson 			goto out;
3527b2d22b66SGeorge Wilson 		}
3528b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3529b2d22b66SGeorge Wilson 	}
35304f5064b7SMark J Musante 
35313b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
3532b2d22b66SGeorge Wilson 	if (error) {
3533b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3534b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3535b2d22b66SGeorge Wilson 			goto out;
3536b2d22b66SGeorge Wilson 		}
3537b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3538b2d22b66SGeorge Wilson 	}
35394f5064b7SMark J Musante 
35404445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3541b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3542b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3543b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3544b2d22b66SGeorge Wilson 			goto out;
3545b2d22b66SGeorge Wilson 		}
3546b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3547b2d22b66SGeorge Wilson 	}
35484f5064b7SMark J Musante 
35494445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3550b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3551b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3552b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3553b2d22b66SGeorge Wilson 			goto out;
3554b2d22b66SGeorge Wilson 		}
3555b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3556b2d22b66SGeorge Wilson 	}
35574f5064b7SMark J Musante 
35583b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
3559b2d22b66SGeorge Wilson 	if (error) {
3560b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3561b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3562b2d22b66SGeorge Wilson 			goto out;
3563b2d22b66SGeorge Wilson 		}
3564b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3565b2d22b66SGeorge Wilson 	}
35664f5064b7SMark J Musante 
35673b2aab18SMatthew Ahrens 	error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
35684f5064b7SMark J Musante 	if (error)
35693b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3570681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
35715d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
35725d7b4d43SMatthew Ahrens 		dmu_objset_disown(os, FTAG);
35735d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
35745d7b4d43SMatthew Ahrens 		goto out;
35755d7b4d43SMatthew Ahrens 	}
35764f5064b7SMark J Musante 	if (error != EBUSY)
35774f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
35784f5064b7SMark J Musante 		    error);
35793b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
35804f5064b7SMark J Musante 
3581b2d22b66SGeorge Wilson out:
3582b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
35834f5064b7SMark J Musante 
3584420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
3585fa9e4066Sahrens }
3586fa9e4066Sahrens 
3587fa9e4066Sahrens /*
3588fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3589fa9e4066Sahrens  */
3590fa9e4066Sahrens void
3591b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3592fa9e4066Sahrens {
3593b24ab676SJeff Bonwick 	ztest_od_t od[4];
3594b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
3595fa9e4066Sahrens 
3596b24ab676SJeff Bonwick 	for (int b = 0; b < batchsize; b++)
3597b24ab676SJeff Bonwick 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3598fa9e4066Sahrens 
3599fa9e4066Sahrens 	/*
3600b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
3601b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
3602fa9e4066Sahrens 	 */
3603b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3604b24ab676SJeff Bonwick 		return;
3605fa9e4066Sahrens 
3606b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
3607b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
3608b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3609fa9e4066Sahrens }
3610fa9e4066Sahrens 
3611fa9e4066Sahrens /*
3612fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
3613fa9e4066Sahrens  */
3614fa9e4066Sahrens void
3615b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3616fa9e4066Sahrens {
3617b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
3618b24ab676SJeff Bonwick 	ztest_od_t od[2];
3619fa9e4066Sahrens 	dmu_tx_t *tx;
3620fa9e4066Sahrens 	int i, freeit, error;
3621fa9e4066Sahrens 	uint64_t n, s, txg;
3622fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3623b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3624b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3625fa9e4066Sahrens 	uint64_t regions = 997;
3626fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
3627fa9e4066Sahrens 	uint64_t width = 40;
3628fa9e4066Sahrens 	int free_percent = 5;
3629fa9e4066Sahrens 
3630fa9e4066Sahrens 	/*
3631fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3632fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3633fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3634fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3635fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3636fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
3637fa9e4066Sahrens 	 *
3638fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3639fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
3640fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
3641fa9e4066Sahrens 	 *
3642fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3643fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
3644fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3645fa9e4066Sahrens 	 *
3646fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
3647fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
3648fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3649fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3650fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
3651fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
3652fa9e4066Sahrens 	 */
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	/*
3655fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3656fa9e4066Sahrens 	 */
3657b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3658b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3659fa9e4066Sahrens 
3660b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3661b24ab676SJeff Bonwick 		return;
3662fa9e4066Sahrens 
3663b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
3664b24ab676SJeff Bonwick 	packobj = od[1].od_object;
3665b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
3666b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
3667fa9e4066Sahrens 
3668fa9e4066Sahrens 	/*
3669fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
3670fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
3671fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
3672fa9e4066Sahrens 	 * handle this race correctly.
3673fa9e4066Sahrens 	 */
3674fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3675fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
3676a2cdcdd2SPaul Dagnelie 	dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3677a2cdcdd2SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_READ);
3678fa9e4066Sahrens 
3679fa9e4066Sahrens 	/*
3680fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3681fa9e4066Sahrens 	 */
3682fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3683fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
3684fa9e4066Sahrens 
3685fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
3686fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
3687fa9e4066Sahrens 
3688b24ab676SJeff Bonwick 	bigoff = n * chunksize;
3689b24ab676SJeff Bonwick 	bigsize = s * chunksize;
3690fa9e4066Sahrens 
3691fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3692fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3693fa9e4066Sahrens 
3694fa9e4066Sahrens 	/*
3695fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3696fa9e4066Sahrens 	 * overwriting it.
3697fa9e4066Sahrens 	 */
3698fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
3699fa9e4066Sahrens 
3700fa9e4066Sahrens 	/*
3701fa9e4066Sahrens 	 * Read the current contents of our objects.
3702fa9e4066Sahrens 	 */
3703b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
37047bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3705fb09f5aaSMadhav Suresh 	ASSERT0(error);
3706b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
37077bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3708fb09f5aaSMadhav Suresh 	ASSERT0(error);
3709fa9e4066Sahrens 
3710fa9e4066Sahrens 	/*
3711fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3712fa9e4066Sahrens 	 */
3713fa9e4066Sahrens 	tx = dmu_tx_create(os);
3714fa9e4066Sahrens 
3715b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3716fa9e4066Sahrens 
3717fa9e4066Sahrens 	if (freeit)
3718b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3719fa9e4066Sahrens 	else
3720b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3721fa9e4066Sahrens 
3722be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
3723be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
3724be9000ccSMatthew Ahrens 
3725b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3726b24ab676SJeff Bonwick 	if (txg == 0) {
3727fa9e4066Sahrens 		umem_free(packbuf, packsize);
3728fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
3729fa9e4066Sahrens 		return;
3730fa9e4066Sahrens 	}
3731fa9e4066Sahrens 
37325d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
37335d7b4d43SMatthew Ahrens 	do {
37345d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
37355d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
37365d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
37375d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
3738b24ab676SJeff Bonwick 
37395d7b4d43SMatthew Ahrens 	enum zio_compress comp;
37405d7b4d43SMatthew Ahrens 	do {
37415d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
37425d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
37435d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
37445d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
3745fa9e4066Sahrens 
3746fa9e4066Sahrens 	/*
3747fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3748fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3749fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3750fa9e4066Sahrens 	 * with the new values we want to write out.
3751fa9e4066Sahrens 	 */
3752fa9e4066Sahrens 	for (i = 0; i < s; i++) {
3753fa9e4066Sahrens 		/* LINTED */
3754fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3755fa9e4066Sahrens 		/* LINTED */
3756b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3757fa9e4066Sahrens 		/* LINTED */
3758b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3759fa9e4066Sahrens 
3760fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3761fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3762fa9e4066Sahrens 
3763fa9e4066Sahrens 		if (pack->bw_txg > txg)
3764fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3765fa9e4066Sahrens 			    pack->bw_txg, txg);
3766fa9e4066Sahrens 
3767fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3768fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3769fa9e4066Sahrens 			    pack->bw_index, n, i);
3770fa9e4066Sahrens 
3771fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3772fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3773fa9e4066Sahrens 
3774fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3775fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3776fa9e4066Sahrens 
3777fa9e4066Sahrens 		if (freeit) {
3778fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
3779fa9e4066Sahrens 		} else {
3780fa9e4066Sahrens 			pack->bw_index = n + i;
3781fa9e4066Sahrens 			pack->bw_txg = txg;
3782fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3783fa9e4066Sahrens 		}
3784fa9e4066Sahrens 		*bigH = *pack;
3785fa9e4066Sahrens 		*bigT = *pack;
3786fa9e4066Sahrens 	}
3787fa9e4066Sahrens 
3788fa9e4066Sahrens 	/*
3789fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
3790fa9e4066Sahrens 	 * Now write them out.
3791fa9e4066Sahrens 	 */
3792b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3793fa9e4066Sahrens 
3794fa9e4066Sahrens 	if (freeit) {
3795420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3796fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
3797fa9e4066Sahrens 			    " txg %llx\n",
3798fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3799fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3800fa9e4066Sahrens 			    (u_longlong_t)txg);
3801fa9e4066Sahrens 		}
3802b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3803fa9e4066Sahrens 	} else {
3804420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3805fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
3806fa9e4066Sahrens 			    " txg %llx\n",
3807fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3808fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3809fa9e4066Sahrens 			    (u_longlong_t)txg);
3810fa9e4066Sahrens 		}
3811b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3812fa9e4066Sahrens 	}
3813fa9e4066Sahrens 
3814fa9e4066Sahrens 	dmu_tx_commit(tx);
3815fa9e4066Sahrens 
3816fa9e4066Sahrens 	/*
3817fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
3818fa9e4066Sahrens 	 */
3819fa9e4066Sahrens 	{
3820fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3821fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3822fa9e4066Sahrens 
3823b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
38247bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
3825b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
38267bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3827fa9e4066Sahrens 
3828fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3829fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3830fa9e4066Sahrens 
3831fa9e4066Sahrens 		umem_free(packcheck, packsize);
3832fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
3833fa9e4066Sahrens 	}
3834fa9e4066Sahrens 
3835fa9e4066Sahrens 	umem_free(packbuf, packsize);
3836fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
3837fa9e4066Sahrens }
3838fa9e4066Sahrens 
38392fdbea25SAleksandr Guzovskiy void
38402fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3841b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
38422fdbea25SAleksandr Guzovskiy {
38432fdbea25SAleksandr Guzovskiy 	uint64_t i;
38442fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
38452fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
38462fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
38472fdbea25SAleksandr Guzovskiy 
38482fdbea25SAleksandr Guzovskiy 	/*
38492fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
38502fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
38512fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
38522fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
38532fdbea25SAleksandr Guzovskiy 	 */
38542fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
38552fdbea25SAleksandr Guzovskiy 		/* LINTED */
38562fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
38572fdbea25SAleksandr Guzovskiy 		/* LINTED */
3858b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
38592fdbea25SAleksandr Guzovskiy 		/* LINTED */
3860b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
38612fdbea25SAleksandr Guzovskiy 
38622fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
38632fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
38642fdbea25SAleksandr Guzovskiy 
38652fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
38662fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
38672fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
38682fdbea25SAleksandr Guzovskiy 
38692fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
38702fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
38712fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
38722fdbea25SAleksandr Guzovskiy 
38732fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
38742fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
38752fdbea25SAleksandr Guzovskiy 
38762fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
38772fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
38782fdbea25SAleksandr Guzovskiy 
38792fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
38802fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
38812fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
38822fdbea25SAleksandr Guzovskiy 
38832fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
38842fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
38852fdbea25SAleksandr Guzovskiy 	}
38862fdbea25SAleksandr Guzovskiy }
38872fdbea25SAleksandr Guzovskiy 
38882fdbea25SAleksandr Guzovskiy void
3889b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
38902fdbea25SAleksandr Guzovskiy {
3891b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
3892b24ab676SJeff Bonwick 	ztest_od_t od[2];
38932fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
38942fdbea25SAleksandr Guzovskiy 	uint64_t i;
38952fdbea25SAleksandr Guzovskiy 	int error;
38962fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
38972fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
3898b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3899b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
3900b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
39012fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
39022fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
39032fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
39042fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
39052fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
3906b24ab676SJeff Bonwick 	dmu_object_info_t doi;
39072fdbea25SAleksandr Guzovskiy 
39082fdbea25SAleksandr Guzovskiy 	/*
39092fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
39102fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
39112fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
39122fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
39132fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
39142fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
39152fdbea25SAleksandr Guzovskiy 	 *
39162fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
39172fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
39182fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
39192fdbea25SAleksandr Guzovskiy 	 *
39202fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
39212fdbea25SAleksandr Guzovskiy 	 * dmu_assign_arcbuf() can be tested for object updates.
39222fdbea25SAleksandr Guzovskiy 	 */
39232fdbea25SAleksandr Guzovskiy 
39242fdbea25SAleksandr Guzovskiy 	/*
39252fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
39262fdbea25SAleksandr Guzovskiy 	 */
3927b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3928b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
39292fdbea25SAleksandr Guzovskiy 
3930b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3931b24ab676SJeff Bonwick 		return;
39322fdbea25SAleksandr Guzovskiy 
3933b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
3934b24ab676SJeff Bonwick 	packobj = od[1].od_object;
3935b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
3936b24ab676SJeff Bonwick 	chunksize = blocksize;
3937b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
39382fdbea25SAleksandr Guzovskiy 
3939b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3940b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
3941b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
3942b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
39432fdbea25SAleksandr Guzovskiy 
39442fdbea25SAleksandr Guzovskiy 	/*
39452fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
39462fdbea25SAleksandr Guzovskiy 	 */
39472fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
39482fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
39492fdbea25SAleksandr Guzovskiy 
39502fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
39512fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
39522fdbea25SAleksandr Guzovskiy 
3953b24ab676SJeff Bonwick 	bigoff = n * chunksize;
3954b24ab676SJeff Bonwick 	bigsize = s * chunksize;
39552fdbea25SAleksandr Guzovskiy 
39562fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
39572fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
39582fdbea25SAleksandr Guzovskiy 
3959b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
39602fdbea25SAleksandr Guzovskiy 
39612fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
39622fdbea25SAleksandr Guzovskiy 
39632fdbea25SAleksandr Guzovskiy 	/*
39642fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
39652fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
39662fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
39672fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
39682fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
39692fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
39702fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
39712fdbea25SAleksandr Guzovskiy 	 */
39722fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
39732fdbea25SAleksandr Guzovskiy 		uint64_t j;
39742fdbea25SAleksandr Guzovskiy 		uint64_t off;
39752fdbea25SAleksandr Guzovskiy 
39762fdbea25SAleksandr Guzovskiy 		/*
39772fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
39782fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
39792fdbea25SAleksandr Guzovskiy 		 * dmu_assign_arcbuf() when it can't directly
39802fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
39812fdbea25SAleksandr Guzovskiy 		 */
39822fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
398389c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
39842fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
3985b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
39862fdbea25SAleksandr Guzovskiy 			} else {
39872fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
3988b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
39892fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
3990b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
39912fdbea25SAleksandr Guzovskiy 			}
39922fdbea25SAleksandr Guzovskiy 		}
39932fdbea25SAleksandr Guzovskiy 
39942fdbea25SAleksandr Guzovskiy 		/*
39952fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
39962fdbea25SAleksandr Guzovskiy 		 */
39972fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
39982fdbea25SAleksandr Guzovskiy 
3999b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
4000b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
40012fdbea25SAleksandr Guzovskiy 
4002b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4003b24ab676SJeff Bonwick 		if (txg == 0) {
40042fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
40052fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
40062fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
400789c86e32SChris Williamson 				if (i != 5 ||
400889c86e32SChris Williamson 				    chunksize < (SPA_MINBLOCKSIZE * 2)) {
40092fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
40102fdbea25SAleksandr Guzovskiy 				} else {
40112fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
40122fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
40132fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
40142fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
40152fdbea25SAleksandr Guzovskiy 				}
40162fdbea25SAleksandr Guzovskiy 			}
40172fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40182fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
40192fdbea25SAleksandr Guzovskiy 			return;
40202fdbea25SAleksandr Guzovskiy 		}
40212fdbea25SAleksandr Guzovskiy 
40222fdbea25SAleksandr Guzovskiy 		/*
40232fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
40242fdbea25SAleksandr Guzovskiy 		 * test dmu_assign_arcbuf() for the case when there're no
40252fdbea25SAleksandr Guzovskiy 		 * existing dbufs for the specified offsets.
40262fdbea25SAleksandr Guzovskiy 		 */
40272fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
4028b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
40297bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
4030fb09f5aaSMadhav Suresh 			ASSERT0(error);
4031b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
40327bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
4033fb09f5aaSMadhav Suresh 			ASSERT0(error);
40342fdbea25SAleksandr Guzovskiy 		}
40352fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4036b24ab676SJeff Bonwick 		    n, chunksize, txg);
40372fdbea25SAleksandr Guzovskiy 
40382fdbea25SAleksandr Guzovskiy 		/*
40392fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
40402fdbea25SAleksandr Guzovskiy 		 * Now write them out.
40412fdbea25SAleksandr Guzovskiy 		 */
4042b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4043420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
40442fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
40452fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
40462fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
40472fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
40482fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
40492fdbea25SAleksandr Guzovskiy 		}
4050b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
40512fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
405289c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
40532fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
4054b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
40552fdbea25SAleksandr Guzovskiy 			} else {
40562fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
40572fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
4058b24ab676SJeff Bonwick 				    chunksize / 2);
40592fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
4060b24ab676SJeff Bonwick 				    chunksize / 2,
40612fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
4062b24ab676SJeff Bonwick 				    chunksize / 2);
40632fdbea25SAleksandr Guzovskiy 			}
40642fdbea25SAleksandr Guzovskiy 
40652fdbea25SAleksandr Guzovskiy 			if (i == 1) {
4066b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
406747cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
40682fdbea25SAleksandr Guzovskiy 			}
406989c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
40702fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
40712fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
40722fdbea25SAleksandr Guzovskiy 			} else {
40732fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
40742fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
40752fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db,
4076b24ab676SJeff Bonwick 				    off + chunksize / 2,
40772fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
40782fdbea25SAleksandr Guzovskiy 			}
40792fdbea25SAleksandr Guzovskiy 			if (i == 1) {
40802fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
40812fdbea25SAleksandr Guzovskiy 			}
40822fdbea25SAleksandr Guzovskiy 		}
40832fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
40842fdbea25SAleksandr Guzovskiy 
40852fdbea25SAleksandr Guzovskiy 		/*
40862fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
40872fdbea25SAleksandr Guzovskiy 		 */
40882fdbea25SAleksandr Guzovskiy 		{
40892fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
40902fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
40912fdbea25SAleksandr Guzovskiy 
4092b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
40937bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
4094b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
40957bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
40962fdbea25SAleksandr Guzovskiy 
40972fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
40982fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
40992fdbea25SAleksandr Guzovskiy 
41002fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
41012fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
41022fdbea25SAleksandr Guzovskiy 		}
41032fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4104b24ab676SJeff Bonwick 			txg_wait_open(dmu_objset_pool(os), 0);
4105b24ab676SJeff Bonwick 		} else if (i == 3) {
4106b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4107fa9e4066Sahrens 		}
4108e05725b1Sbonwick 	}
4109e05725b1Sbonwick 
4110b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4111b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4112b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4113b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4114b24ab676SJeff Bonwick }
4115fa9e4066Sahrens 
4116b24ab676SJeff Bonwick /* ARGSUSED */
4117b24ab676SJeff Bonwick void
4118b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4119b24ab676SJeff Bonwick {
4120b24ab676SJeff Bonwick 	ztest_od_t od[1];
4121b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4122b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4123fa9e4066Sahrens 
4124e05725b1Sbonwick 	/*
4125b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4126b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4127b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4128e05725b1Sbonwick 	 */
4129b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4130fa9e4066Sahrens 
4131b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4132e05725b1Sbonwick 		return;
4133fa9e4066Sahrens 
4134b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4135b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4136b24ab676SJeff Bonwick }
4137fa9e4066Sahrens 
4138b24ab676SJeff Bonwick void
4139b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4140b24ab676SJeff Bonwick {
4141b24ab676SJeff Bonwick 	ztest_od_t od[1];
4142b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4143b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4144b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4145b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4146b24ab676SJeff Bonwick 	void *data;
4147fa9e4066Sahrens 
4148b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4149fa9e4066Sahrens 
4150b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4151e05725b1Sbonwick 		return;
41520e34b6a7Sbonwick 
4153b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4154f4a72450SJeff Bonwick 		return;
4155f4a72450SJeff Bonwick 
4156b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4157fa9e4066Sahrens 
4158b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4159b24ab676SJeff Bonwick 
4160b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4161b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4162b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4163b24ab676SJeff Bonwick 		    data) != 0)
4164b24ab676SJeff Bonwick 			break;
4165b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4166b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4167b24ab676SJeff Bonwick 	}
4168b24ab676SJeff Bonwick 
4169b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4170fa9e4066Sahrens }
4171fa9e4066Sahrens 
4172fa9e4066Sahrens /*
4173fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4174fa9e4066Sahrens  */
4175fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4176fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4177fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4178fa9e4066Sahrens 
4179fa9e4066Sahrens void
4180b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4181fa9e4066Sahrens {
4182b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4183b24ab676SJeff Bonwick 	ztest_od_t od[1];
4184fa9e4066Sahrens 	uint64_t object;
4185fa9e4066Sahrens 	uint64_t txg, last_txg;
4186fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4187fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4188fa9e4066Sahrens 	int i, ints;
4189fa9e4066Sahrens 	dmu_tx_t *tx;
4190fa9e4066Sahrens 	char propname[100], txgname[100];
4191fa9e4066Sahrens 	int error;
4192fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4193fa9e4066Sahrens 
4194b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4195fa9e4066Sahrens 
4196b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4197b24ab676SJeff Bonwick 		return;
4198fa9e4066Sahrens 
4199b24ab676SJeff Bonwick 	object = od[0].od_object;
4200fa9e4066Sahrens 
4201b24ab676SJeff Bonwick 	/*
4202b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4203b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4204b24ab676SJeff Bonwick 	 */
4205b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4206b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4207b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4208b24ab676SJeff Bonwick 	if (txg == 0)
4209b24ab676SJeff Bonwick 		return;
4210b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4211b24ab676SJeff Bonwick 		value[i] = i;
4212b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4213b24ab676SJeff Bonwick 		    1, &value[i], tx));
4214b24ab676SJeff Bonwick 	}
4215b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4216b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4217b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4218b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4219b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4220b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4221b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4222fa9e4066Sahrens 	}
4223b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4224b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4225b24ab676SJeff Bonwick 	}
4226b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4227fa9e4066Sahrens 
4228b24ab676SJeff Bonwick 	/*
4229b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4230b24ab676SJeff Bonwick 	 */
4231fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4232fa9e4066Sahrens 
4233e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4234e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4235e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4236e05725b1Sbonwick 	bzero(value, sizeof (value));
4237e05725b1Sbonwick 	last_txg = 0;
4238fa9e4066Sahrens 
4239e05725b1Sbonwick 	/*
4240e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4241e05725b1Sbonwick 	 */
4242e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4243e05725b1Sbonwick 	if (error == 0) {
4244e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4245e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4246fa9e4066Sahrens 
4247e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4248e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4249fa9e4066Sahrens 
4250e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4251e05725b1Sbonwick 		    &zl_ints) == 0);
4252fa9e4066Sahrens 
4253e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4254e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4255fa9e4066Sahrens 
4256e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4257e05725b1Sbonwick 		    zl_ints, value) == 0);
4258fa9e4066Sahrens 
4259e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4260e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4261fa9e4066Sahrens 		}
4262e05725b1Sbonwick 	} else {
4263e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4264e05725b1Sbonwick 	}
4265fa9e4066Sahrens 
4266e05725b1Sbonwick 	/*
4267e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4268e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4269e05725b1Sbonwick 	 * in which the property was last updated.  The second
4270e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4271e05725b1Sbonwick 	 * should be txg + object + n.
4272e05725b1Sbonwick 	 */
4273e05725b1Sbonwick 	tx = dmu_tx_create(os);
4274b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4275b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4276b24ab676SJeff Bonwick 	if (txg == 0)
4277e05725b1Sbonwick 		return;
4278fa9e4066Sahrens 
4279e05725b1Sbonwick 	if (last_txg > txg)
4280e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4281fa9e4066Sahrens 
4282e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4283e05725b1Sbonwick 		value[i] = txg + object + i;
4284fa9e4066Sahrens 
4285b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4286b24ab676SJeff Bonwick 	    1, &txg, tx));
4287b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4288b24ab676SJeff Bonwick 	    ints, value, tx));
4289fa9e4066Sahrens 
4290e05725b1Sbonwick 	dmu_tx_commit(tx);
4291fa9e4066Sahrens 
4292e05725b1Sbonwick 	/*
4293e05725b1Sbonwick 	 * Remove a random pair of entries.
4294e05725b1Sbonwick 	 */
4295e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4296e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4297e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4298fa9e4066Sahrens 
4299e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4300fa9e4066Sahrens 
4301e05725b1Sbonwick 	if (error == ENOENT)
4302e05725b1Sbonwick 		return;
4303fa9e4066Sahrens 
4304fb09f5aaSMadhav Suresh 	ASSERT0(error);
4305fa9e4066Sahrens 
4306e05725b1Sbonwick 	tx = dmu_tx_create(os);
4307b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4308b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4309b24ab676SJeff Bonwick 	if (txg == 0)
431012a2833aSSanjeev Bagewadi 		return;
4311b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4312b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
431312a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
431412a2833aSSanjeev Bagewadi }
431512a2833aSSanjeev Bagewadi 
431612a2833aSSanjeev Bagewadi /*
431712a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
431812a2833aSSanjeev Bagewadi  */
431912a2833aSSanjeev Bagewadi void
4320b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
432112a2833aSSanjeev Bagewadi {
4322b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4323b24ab676SJeff Bonwick 	ztest_od_t od[1];
4324b24ab676SJeff Bonwick 	uint64_t object, txg;
432512a2833aSSanjeev Bagewadi 
4326b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
432712a2833aSSanjeev Bagewadi 
4328b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4329b24ab676SJeff Bonwick 		return;
433012a2833aSSanjeev Bagewadi 
4331b24ab676SJeff Bonwick 	object = od[0].od_object;
433212a2833aSSanjeev Bagewadi 
433312a2833aSSanjeev Bagewadi 	/*
4334b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
433512a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4336b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
433712a2833aSSanjeev Bagewadi 	 */
4338b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
43399adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
4340b24ab676SJeff Bonwick 		uint64_t value = i;
4341b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4342b24ab676SJeff Bonwick 		int error;
434312a2833aSSanjeev Bagewadi 
4344b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4345b24ab676SJeff Bonwick 		    id, value);
434612a2833aSSanjeev Bagewadi 
4347b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4348b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4349b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4350b24ab676SJeff Bonwick 		if (txg == 0)
435112a2833aSSanjeev Bagewadi 			return;
4352b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4353b24ab676SJeff Bonwick 		    &value, tx);
435412a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
435512a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
435612a2833aSSanjeev Bagewadi 	}
4357fa9e4066Sahrens }
4358fa9e4066Sahrens 
4359b24ab676SJeff Bonwick /* ARGSUSED */
4360fa9e4066Sahrens void
4361b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4362fa9e4066Sahrens {
4363b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4364b24ab676SJeff Bonwick 	ztest_od_t od[1];
4365fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4366fa9e4066Sahrens 	dmu_tx_t *tx;
4367fa9e4066Sahrens 	int i, namelen, error;
4368b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4369fa9e4066Sahrens 	char name[20], string_value[20];
4370fa9e4066Sahrens 	void *data;
4371fa9e4066Sahrens 
4372b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4373b24ab676SJeff Bonwick 
4374b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4375b24ab676SJeff Bonwick 		return;
4376b24ab676SJeff Bonwick 
4377b24ab676SJeff Bonwick 	object = od[0].od_object;
4378b24ab676SJeff Bonwick 
4379e05725b1Sbonwick 	/*
4380e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4381e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4382e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4383e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4384e05725b1Sbonwick 	 */
4385e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4386fa9e4066Sahrens 
4387e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4388e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4389e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4390e05725b1Sbonwick 		name[i] = '.';
4391e05725b1Sbonwick 	name[i] = '\0';
4392fa9e4066Sahrens 
4393b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4394e05725b1Sbonwick 		wsize = sizeof (txg);
4395e05725b1Sbonwick 		wc = 1;
4396e05725b1Sbonwick 		data = &txg;
4397e05725b1Sbonwick 	} else {
4398e05725b1Sbonwick 		wsize = 1;
4399e05725b1Sbonwick 		wc = namelen;
4400e05725b1Sbonwick 		data = string_value;
4401e05725b1Sbonwick 	}
4402fa9e4066Sahrens 
4403e05725b1Sbonwick 	count = -1ULL;
44043b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4405e05725b1Sbonwick 	ASSERT(count != -1ULL);
4406fa9e4066Sahrens 
4407e05725b1Sbonwick 	/*
4408e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4409e05725b1Sbonwick 	 */
4410e05725b1Sbonwick 	i = ztest_random(5);
4411e05725b1Sbonwick 
4412e05725b1Sbonwick 	if (i >= 2) {
4413e05725b1Sbonwick 		tx = dmu_tx_create(os);
4414b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4415b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4416b24ab676SJeff Bonwick 		if (txg == 0)
4417e05725b1Sbonwick 			return;
4418e05725b1Sbonwick 		bcopy(name, string_value, namelen);
4419e05725b1Sbonwick 	} else {
4420e05725b1Sbonwick 		tx = NULL;
4421e05725b1Sbonwick 		txg = 0;
4422e05725b1Sbonwick 		bzero(string_value, namelen);
4423e05725b1Sbonwick 	}
4424fa9e4066Sahrens 
4425e05725b1Sbonwick 	switch (i) {
4426fa9e4066Sahrens 
4427e05725b1Sbonwick 	case 0:
4428e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4429e05725b1Sbonwick 		if (error == 0) {
4430e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
4431e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
4432e05725b1Sbonwick 		} else {
4433e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4434e05725b1Sbonwick 		}
4435e05725b1Sbonwick 		break;
4436fa9e4066Sahrens 
4437e05725b1Sbonwick 	case 1:
4438e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
4439e05725b1Sbonwick 		if (error == 0) {
4440e05725b1Sbonwick 			if (data == string_value &&
4441e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
4442e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
4443e05725b1Sbonwick 				    name, data, namelen);
4444e05725b1Sbonwick 		} else {
4445e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4446e05725b1Sbonwick 		}
4447e05725b1Sbonwick 		break;
4448fa9e4066Sahrens 
4449e05725b1Sbonwick 	case 2:
4450e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
4451e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
4452e05725b1Sbonwick 		break;
4453fa9e4066Sahrens 
4454e05725b1Sbonwick 	case 3:
4455e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4456e05725b1Sbonwick 		break;
4457fa9e4066Sahrens 
4458e05725b1Sbonwick 	case 4:
4459e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
4460e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
4461e05725b1Sbonwick 		break;
4462fa9e4066Sahrens 	}
4463e05725b1Sbonwick 
4464e05725b1Sbonwick 	if (tx != NULL)
4465e05725b1Sbonwick 		dmu_tx_commit(tx);
4466fa9e4066Sahrens }
4467fa9e4066Sahrens 
4468d20e665cSRicardo M. Correia /*
4469d20e665cSRicardo M. Correia  * Commit callback data.
4470d20e665cSRicardo M. Correia  */
4471d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
4472d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
4473d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
4474d20e665cSRicardo M. Correia 	int			zcd_expected_err;
4475d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
4476d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
4477d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
4478d20e665cSRicardo M. Correia } ztest_cb_data_t;
4479d20e665cSRicardo M. Correia 
4480d20e665cSRicardo M. Correia /* This is the actual commit callback function */
4481d20e665cSRicardo M. Correia static void
4482d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
4483d20e665cSRicardo M. Correia {
4484d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
4485d20e665cSRicardo M. Correia 	uint64_t synced_txg;
4486d20e665cSRicardo M. Correia 
4487d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
4488d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
4489d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
4490d20e665cSRicardo M. Correia 
4491d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
4492d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
4493d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4494d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4495d20e665cSRicardo M. Correia 		    synced_txg);
4496d20e665cSRicardo M. Correia 
4497d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
4498d20e665cSRicardo M. Correia 
4499d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
4500fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
4501d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
4502d20e665cSRicardo M. Correia 
4503d20e665cSRicardo M. Correia 		/*
4504d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
4505d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
4506d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
4507d20e665cSRicardo M. Correia 		 */
4508d20e665cSRicardo M. Correia 		return;
4509d20e665cSRicardo M. Correia 	}
4510d20e665cSRicardo M. Correia 
4511d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
4512d20e665cSRicardo M. Correia 	if (!data->zcd_added)
4513d20e665cSRicardo M. Correia 		goto out;
4514d20e665cSRicardo M. Correia 
4515d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
4516d20e665cSRicardo M. Correia 
4517d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
4518d20e665cSRicardo M. Correia 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4519d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
4520d20e665cSRicardo M. Correia 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4521d20e665cSRicardo M. Correia 
4522d20e665cSRicardo M. Correia out:
4523d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
4524d20e665cSRicardo M. Correia }
4525d20e665cSRicardo M. Correia 
4526d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
4527d20e665cSRicardo M. Correia static ztest_cb_data_t *
4528d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
4529d20e665cSRicardo M. Correia {
4530d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
4531d20e665cSRicardo M. Correia 
4532d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4533d20e665cSRicardo M. Correia 
4534d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
4535d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
4536d20e665cSRicardo M. Correia 
4537d20e665cSRicardo M. Correia 	return (cb_data);
4538d20e665cSRicardo M. Correia }
4539d20e665cSRicardo M. Correia 
4540d20e665cSRicardo M. Correia /*
4541d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
4542d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
4543d20e665cSRicardo M. Correia  * implementation bug.
4544d20e665cSRicardo M. Correia  */
4545d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
4546d20e665cSRicardo M. Correia 
4547d20e665cSRicardo M. Correia /*
4548d20e665cSRicardo M. Correia  * Commit callback test.
4549d20e665cSRicardo M. Correia  */
4550d20e665cSRicardo M. Correia void
4551b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4552d20e665cSRicardo M. Correia {
4553b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4554b24ab676SJeff Bonwick 	ztest_od_t od[1];
4555d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
4556d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
4557d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
4558d20e665cSRicardo M. Correia 	int i, error;
4559d20e665cSRicardo M. Correia 
4560b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4561b24ab676SJeff Bonwick 
4562b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4563b24ab676SJeff Bonwick 		return;
4564b24ab676SJeff Bonwick 
4565d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
4566d20e665cSRicardo M. Correia 
4567d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
4568d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4569d20e665cSRicardo M. Correia 
4570b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4571d20e665cSRicardo M. Correia 
4572d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
4573d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
4574d20e665cSRicardo M. Correia 		error = -1;
4575d20e665cSRicardo M. Correia 
4576d20e665cSRicardo M. Correia 	if (!error)
4577d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
4578d20e665cSRicardo M. Correia 
4579d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
4580d20e665cSRicardo M. Correia 
4581d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
4582d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
4583d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4584d20e665cSRicardo M. Correia 
4585d20e665cSRicardo M. Correia 	if (error) {
4586d20e665cSRicardo M. Correia 		/*
4587d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
4588d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
4589d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
4590d20e665cSRicardo M. Correia 		 * so we will check for that.
4591d20e665cSRicardo M. Correia 		 */
4592d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4593d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
4594d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
4595d20e665cSRicardo M. Correia 		}
4596d20e665cSRicardo M. Correia 
4597d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
4598d20e665cSRicardo M. Correia 
4599d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4600d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
4601d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4602d20e665cSRicardo M. Correia 		}
4603d20e665cSRicardo M. Correia 
4604d20e665cSRicardo M. Correia 		return;
4605d20e665cSRicardo M. Correia 	}
4606d20e665cSRicardo M. Correia 
4607d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
4608d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4609d20e665cSRicardo M. Correia 
4610d20e665cSRicardo M. Correia 	/*
4611d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
4612d20e665cSRicardo M. Correia 	 */
4613b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4614d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
4615d20e665cSRicardo M. Correia 
4616d20e665cSRicardo M. Correia 	if (old_txg > txg)
4617d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4618d20e665cSRicardo M. Correia 		    old_txg, txg);
4619d20e665cSRicardo M. Correia 
4620b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4621d20e665cSRicardo M. Correia 
4622d20e665cSRicardo M. Correia 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
4623d20e665cSRicardo M. Correia 
4624d20e665cSRicardo M. Correia 	/*
4625d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
4626d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
4627d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
4628d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
4629d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
4630d20e665cSRicardo M. Correia 	 * implementation.
4631d20e665cSRicardo M. Correia 	 *
4632d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
4633d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
4634d20e665cSRicardo M. Correia 	 * implementation bug..
4635d20e665cSRicardo M. Correia 	 */
4636d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
4637d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
4638b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4639d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4640d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4641d20e665cSRicardo M. Correia 	}
4642d20e665cSRicardo M. Correia 
4643d20e665cSRicardo M. Correia 	/*
4644d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
4645d20e665cSRicardo M. Correia 	 *
4646d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
4647d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
4648d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
4649d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
4650d20e665cSRicardo M. Correia 	 */
4651d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
4652d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4653d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4654d20e665cSRicardo M. Correia 
4655d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
4656d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
4657d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
4658d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4659d20e665cSRicardo M. Correia 		else
4660d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4661d20e665cSRicardo M. Correia 			    cb_data[i]);
4662d20e665cSRicardo M. Correia 
4663d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
4664d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
4665d20e665cSRicardo M. Correia 
4666d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
4667d20e665cSRicardo M. Correia 	}
4668d20e665cSRicardo M. Correia 
4669d20e665cSRicardo M. Correia 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
4670d20e665cSRicardo M. Correia 
4671d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
4672d20e665cSRicardo M. Correia }
4673d20e665cSRicardo M. Correia 
4674b24ab676SJeff Bonwick /* ARGSUSED */
4675fa9e4066Sahrens void
4676b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4677fa9e4066Sahrens {
4678b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
4679b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
4680b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
4681b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
4682b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
4683b24ab676SJeff Bonwick 	};
4684fa9e4066Sahrens 
4685420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
4686fa9e4066Sahrens 
4687b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4688b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4689b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4690fa9e4066Sahrens 
4691420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
4692b24ab676SJeff Bonwick }
4693fa9e4066Sahrens 
4694*5cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
4695*5cabbc6bSPrashanth Sreenivasa void
4696*5cabbc6bSPrashanth Sreenivasa ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4697*5cabbc6bSPrashanth Sreenivasa {
4698*5cabbc6bSPrashanth Sreenivasa 	(void) rw_rdlock(&ztest_name_lock);
4699*5cabbc6bSPrashanth Sreenivasa 
4700*5cabbc6bSPrashanth Sreenivasa 	int error = dmu_objset_remap_indirects(zd->zd_name);
4701*5cabbc6bSPrashanth Sreenivasa 	if (error == ENOSPC)
4702*5cabbc6bSPrashanth Sreenivasa 		error = 0;
4703*5cabbc6bSPrashanth Sreenivasa 	ASSERT0(error);
4704*5cabbc6bSPrashanth Sreenivasa 
4705*5cabbc6bSPrashanth Sreenivasa 	(void) rw_unlock(&ztest_name_lock);
4706*5cabbc6bSPrashanth Sreenivasa }
4707*5cabbc6bSPrashanth Sreenivasa 
4708b24ab676SJeff Bonwick /* ARGSUSED */
4709b24ab676SJeff Bonwick void
4710b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4711b24ab676SJeff Bonwick {
4712b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
4713ea8dc4b6Seschrock 
4714420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
4715ea8dc4b6Seschrock 
4716420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4717b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4718fa9e4066Sahrens 
4719fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
4720fa9e4066Sahrens 
4721420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
4722b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
4723fa9e4066Sahrens 
4724b24ab676SJeff Bonwick 	nvlist_free(props);
4725fa9e4066Sahrens 
4726420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
4727fa9e4066Sahrens }
4728fa9e4066Sahrens 
47293b2aab18SMatthew Ahrens static int
47303b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
47313b2aab18SMatthew Ahrens {
47323b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
47333b2aab18SMatthew Ahrens 	int error;
47343b2aab18SMatthew Ahrens 
47353b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
47363b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
47373b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
47383b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
47393b2aab18SMatthew Ahrens 	fnvlist_free(holds);
47403b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
47413b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
47423b2aab18SMatthew Ahrens 	return (error);
47433b2aab18SMatthew Ahrens }
47443b2aab18SMatthew Ahrens 
47455c987a37SChris Kirby /*
47465c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
47475c987a37SChris Kirby  */
47485c987a37SChris Kirby void
4749b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
47505c987a37SChris Kirby {
47515c987a37SChris Kirby 	int error;
4752b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
47535c987a37SChris Kirby 	objset_t *origin;
47545c987a37SChris Kirby 	char snapname[100];
47555c987a37SChris Kirby 	char fullname[100];
47565c987a37SChris Kirby 	char clonename[100];
47575c987a37SChris Kirby 	char tag[100];
47589adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
47593b2aab18SMatthew Ahrens 	nvlist_t *holds;
47605c987a37SChris Kirby 
4761420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
47625c987a37SChris Kirby 
47635c987a37SChris Kirby 	dmu_objset_name(os, osname);
47645c987a37SChris Kirby 
47653b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
47663b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
47673b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
47683b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
47693b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
47705c987a37SChris Kirby 
47715c987a37SChris Kirby 	/*
47725c987a37SChris Kirby 	 * Clean up from any previous run.
47735c987a37SChris Kirby 	 */
47743b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
47753b2aab18SMatthew Ahrens 	if (error != ENOENT)
47763b2aab18SMatthew Ahrens 		ASSERT0(error);
47773b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
47783b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
47793b2aab18SMatthew Ahrens 		ASSERT0(error);
47803b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
47813b2aab18SMatthew Ahrens 	if (error != ENOENT)
47823b2aab18SMatthew Ahrens 		ASSERT0(error);
47835c987a37SChris Kirby 
47845c987a37SChris Kirby 	/*
47855c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
47865c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
47875c987a37SChris Kirby 	 */
47884445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
47895c987a37SChris Kirby 	if (error) {
47905c987a37SChris Kirby 		if (error == ENOSPC) {
47915c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
47925c987a37SChris Kirby 			goto out;
47935c987a37SChris Kirby 		}
47945c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
47955c987a37SChris Kirby 	}
47965c987a37SChris Kirby 
47973b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
47985c987a37SChris Kirby 	if (error) {
47995c987a37SChris Kirby 		if (error == ENOSPC) {
48005c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
48015c987a37SChris Kirby 			goto out;
48025c987a37SChris Kirby 		}
48035c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
48045c987a37SChris Kirby 	}
48055c987a37SChris Kirby 
48063b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
48075c987a37SChris Kirby 	if (error) {
48083b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
48095c987a37SChris Kirby 		    fullname, error);
48105c987a37SChris Kirby 	}
48115c987a37SChris Kirby 
48123b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
48135c987a37SChris Kirby 	if (error)
48143b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
48155c987a37SChris Kirby 
48165c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
48175c987a37SChris Kirby 	if (error != ENOENT)
48185c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
48195c987a37SChris Kirby 
48205c987a37SChris Kirby 	/*
48215c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
48225c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
48235c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
48245c987a37SChris Kirby 	 */
48254445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
48265c987a37SChris Kirby 	if (error) {
48275c987a37SChris Kirby 		if (error == ENOSPC) {
48285c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
48295c987a37SChris Kirby 			goto out;
48305c987a37SChris Kirby 		}
48315c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
48325c987a37SChris Kirby 	}
48335c987a37SChris Kirby 
48343b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
48353b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
48363b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
48373b2aab18SMatthew Ahrens 	fnvlist_free(holds);
48383b2aab18SMatthew Ahrens 
48395d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
48405d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
48415d7b4d43SMatthew Ahrens 		goto out;
48425d7b4d43SMatthew Ahrens 	} else if (error) {
48435d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
48445d7b4d43SMatthew Ahrens 		    fullname, tag, error);
48455d7b4d43SMatthew Ahrens 	}
48465c987a37SChris Kirby 
48473b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
48485c987a37SChris Kirby 	if (error != EBUSY) {
48493b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
48505c987a37SChris Kirby 		    fullname, error);
48515c987a37SChris Kirby 	}
48525c987a37SChris Kirby 
48533b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
48545c987a37SChris Kirby 	if (error) {
48553b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
48565c987a37SChris Kirby 		    fullname, error);
48575c987a37SChris Kirby 	}
48585c987a37SChris Kirby 
48593b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
48605c987a37SChris Kirby 	if (error)
4861a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
48625c987a37SChris Kirby 
48633b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
48645c987a37SChris Kirby 
48655c987a37SChris Kirby out:
4866420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
48675c987a37SChris Kirby }
48685c987a37SChris Kirby 
4869fa9e4066Sahrens /*
4870fa9e4066Sahrens  * Inject random faults into the on-disk data.
4871fa9e4066Sahrens  */
4872b24ab676SJeff Bonwick /* ARGSUSED */
4873fa9e4066Sahrens void
4874b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4875fa9e4066Sahrens {
4876b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
4877420dfc95SChris Siden 	spa_t *spa = ztest_spa;
4878fa9e4066Sahrens 	int fd;
4879fa9e4066Sahrens 	uint64_t offset;
48801195e687SMark J Musante 	uint64_t leaves;
4881fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4882fa9e4066Sahrens 	uint64_t top, leaf;
4883fa9e4066Sahrens 	char path0[MAXPATHLEN];
4884fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
4885fa9e4066Sahrens 	size_t fsize;
4886f34284d8SMatthew Ahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;
4887fa9e4066Sahrens 	int iters = 1000;
48881195e687SMark J Musante 	int maxfaults;
48891195e687SMark J Musante 	int mirror_save;
4890e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
4891ea8dc4b6Seschrock 	uint64_t guid0 = 0;
48928f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
4893ea8dc4b6Seschrock 
4894420dfc95SChris Siden 	VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
48951195e687SMark J Musante 	maxfaults = MAXFAULTS();
4896420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
48971195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
4898420dfc95SChris Siden 	VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
48991195e687SMark J Musante 
4900e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
4901fa9e4066Sahrens 
49022c1e2b44SGeorge Wilson 	/*
49032c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
49042c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
49052c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
49062c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
49072c1e2b44SGeorge Wilson 	 */
49082c1e2b44SGeorge Wilson 	(void) rw_rdlock(&ztest_name_lock);
49092c1e2b44SGeorge Wilson 
4910fa9e4066Sahrens 	/*
4911e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4912fa9e4066Sahrens 	 */
4913e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4914fa9e4066Sahrens 
4915e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
4916e14bb325SJeff Bonwick 		/*
4917b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
4918e14bb325SJeff Bonwick 		 */
4919b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
49201195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
4921fa9e4066Sahrens 
4922e14bb325SJeff Bonwick 		/*
4923e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
4924e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
4925e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
4926e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
4927e14bb325SJeff Bonwick 		 */
4928e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
4929420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
4930420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
4931e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4932420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
4933420dfc95SChris Siden 		    top * leaves + leaf);
4934fa9e4066Sahrens 
4935e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
49368f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
49378f18d1faSGeorge Wilson 			islog = B_TRUE;
49388f18d1faSGeorge Wilson 
49392c1e2b44SGeorge Wilson 		/*
49402c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
49412c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
49422c1e2b44SGeorge Wilson 		 * resilvering.
49432c1e2b44SGeorge Wilson 		 */
49442c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
49452c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
4946b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
4947e14bb325SJeff Bonwick 			/*
4948e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
4949e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
4950e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
4951e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
4952e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
4953e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
4954e14bb325SJeff Bonwick 			 * combination of this with injection of random data
4955e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
4956e14bb325SJeff Bonwick 			 */
4957e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
4958e14bb325SJeff Bonwick 
4959*5cabbc6bSPrashanth Sreenivasa 			zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
4960*5cabbc6bSPrashanth Sreenivasa 			    (long long)vd0->vdev_id, (int)maxfaults);
4961*5cabbc6bSPrashanth Sreenivasa 
4962e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
4963e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
4964e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
4965e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
4966e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
4967e14bb325SJeff Bonwick 			} else {
4968e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
4969e14bb325SJeff Bonwick 			}
4970e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
4971e14bb325SJeff Bonwick 		}
4972e14bb325SJeff Bonwick 	} else {
4973e14bb325SJeff Bonwick 		/*
4974e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
4975e14bb325SJeff Bonwick 		 */
4976e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
4977fa9e4066Sahrens 
4978e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
4979e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
49802c1e2b44SGeorge Wilson 			(void) rw_unlock(&ztest_name_lock);
4981e14bb325SJeff Bonwick 			return;
4982e14bb325SJeff Bonwick 		}
4983e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4984ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
4985e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
4986e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
4987e14bb325SJeff Bonwick 
4988e14bb325SJeff Bonwick 		leaf = 0;
4989e14bb325SJeff Bonwick 		leaves = 1;
4990e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
4991fa9e4066Sahrens 	}
4992fa9e4066Sahrens 
4993e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
49942c1e2b44SGeorge Wilson 	(void) rw_unlock(&ztest_name_lock);
4995e14bb325SJeff Bonwick 
4996fa9e4066Sahrens 	/*
49978f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
49988f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
4999fa9e4066Sahrens 	 */
50008f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
50018ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
50028ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
50038ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
50048f18d1faSGeorge Wilson 
50058f18d1faSGeorge Wilson 			/*
50068f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
50078f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
50088f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
50098f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
50108f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
50118f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
50128f18d1faSGeorge Wilson 			 */
50138f18d1faSGeorge Wilson 			if (islog)
5014420dfc95SChris Siden 				(void) rw_wrlock(&ztest_name_lock);
50158f18d1faSGeorge Wilson 
50168ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
50178f18d1faSGeorge Wilson 
50188f18d1faSGeorge Wilson 			if (islog)
5019420dfc95SChris Siden 				(void) rw_unlock(&ztest_name_lock);
50208ad4d6ddSJeff Bonwick 		} else {
50219253d63dSGeorge Wilson 			/*
50229253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
50239253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
50249253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
50259253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
50269253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
50279253d63dSGeorge Wilson 			 * prevent a race between injection testing and
50289253d63dSGeorge Wilson 			 * aux_vdev removal.
50299253d63dSGeorge Wilson 			 */
50309253d63dSGeorge Wilson 			VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
50318ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
50329253d63dSGeorge Wilson 			VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
50338ad4d6ddSJeff Bonwick 		}
5034fa9e4066Sahrens 	}
5035fa9e4066Sahrens 
50368f18d1faSGeorge Wilson 	if (maxfaults == 0)
50378f18d1faSGeorge Wilson 		return;
50388f18d1faSGeorge Wilson 
5039fa9e4066Sahrens 	/*
5040ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
5041fa9e4066Sahrens 	 */
5042fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
5043fa9e4066Sahrens 
5044fa9e4066Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
5045fa9e4066Sahrens 		return;
5046fa9e4066Sahrens 
5047fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
5048fa9e4066Sahrens 
5049fa9e4066Sahrens 	while (--iters != 0) {
5050f9eb9fdfSMatthew Ahrens 		/*
5051f9eb9fdfSMatthew Ahrens 		 * The offset must be chosen carefully to ensure that
5052f9eb9fdfSMatthew Ahrens 		 * we do not inject a given logical block with errors
5053f9eb9fdfSMatthew Ahrens 		 * on two different leaf devices, because ZFS can not
5054f9eb9fdfSMatthew Ahrens 		 * tolerate that (if maxfaults==1).
5055f9eb9fdfSMatthew Ahrens 		 *
5056f9eb9fdfSMatthew Ahrens 		 * We divide each leaf into chunks of size
5057f9eb9fdfSMatthew Ahrens 		 * (# leaves * SPA_MAXBLOCKSIZE * 4).  Within each chunk
5058f9eb9fdfSMatthew Ahrens 		 * there is a series of ranges to which we can inject errors.
5059f9eb9fdfSMatthew Ahrens 		 * Each range can accept errors on only a single leaf vdev.
5060f9eb9fdfSMatthew Ahrens 		 * The error injection ranges are separated by ranges
5061f9eb9fdfSMatthew Ahrens 		 * which we will not inject errors on any device (DMZs).
5062f9eb9fdfSMatthew Ahrens 		 * Each DMZ must be large enough such that a single block
5063f9eb9fdfSMatthew Ahrens 		 * can not straddle it, so that a single block can not be
5064f9eb9fdfSMatthew Ahrens 		 * a target in two different injection ranges (on different
5065f9eb9fdfSMatthew Ahrens 		 * leaf vdevs).
5066f9eb9fdfSMatthew Ahrens 		 *
5067f9eb9fdfSMatthew Ahrens 		 * For example, with 3 leaves, each chunk looks like:
5068f9eb9fdfSMatthew Ahrens 		 *    0 to  32M: injection range for leaf 0
5069f9eb9fdfSMatthew Ahrens 		 *  32M to  64M: DMZ - no injection allowed
5070f9eb9fdfSMatthew Ahrens 		 *  64M to  96M: injection range for leaf 1
5071f9eb9fdfSMatthew Ahrens 		 *  96M to 128M: DMZ - no injection allowed
5072f9eb9fdfSMatthew Ahrens 		 * 128M to 160M: injection range for leaf 2
5073f9eb9fdfSMatthew Ahrens 		 * 160M to 192M: DMZ - no injection allowed
5074f9eb9fdfSMatthew Ahrens 		 */
5075fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
5076fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
5077fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5078fa9e4066Sahrens 
5079f34284d8SMatthew Ahrens 		/*
5080f34284d8SMatthew Ahrens 		 * Only allow damage to the labels at one end of the vdev.
5081f34284d8SMatthew Ahrens 		 *
5082f34284d8SMatthew Ahrens 		 * If all labels are damaged, the device will be totally
5083f34284d8SMatthew Ahrens 		 * inaccessible, which will result in loss of data,
5084f34284d8SMatthew Ahrens 		 * because we also damage (parts of) the other side of
5085f34284d8SMatthew Ahrens 		 * the mirror/raidz.
5086f34284d8SMatthew Ahrens 		 *
5087f34284d8SMatthew Ahrens 		 * Additionally, we will always have both an even and an
5088f34284d8SMatthew Ahrens 		 * odd label, so that we can handle crashes in the
5089f34284d8SMatthew Ahrens 		 * middle of vdev_config_sync().
5090f34284d8SMatthew Ahrens 		 */
5091f34284d8SMatthew Ahrens 		if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5092f34284d8SMatthew Ahrens 			continue;
5093f34284d8SMatthew Ahrens 
5094f34284d8SMatthew Ahrens 		/*
5095f34284d8SMatthew Ahrens 		 * The two end labels are stored at the "end" of the disk, but
5096f34284d8SMatthew Ahrens 		 * the end of the disk (vdev_psize) is aligned to
5097f34284d8SMatthew Ahrens 		 * sizeof (vdev_label_t).
5098f34284d8SMatthew Ahrens 		 */
5099f34284d8SMatthew Ahrens 		uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5100f34284d8SMatthew Ahrens 		if ((leaf & 1) == 1 &&
5101f34284d8SMatthew Ahrens 		    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5102fa9e4066Sahrens 			continue;
5103fa9e4066Sahrens 
5104420dfc95SChris Siden 		VERIFY(mutex_lock(&ztest_vdev_lock) == 0);
51051195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
5106420dfc95SChris Siden 			VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
51071195e687SMark J Musante 			(void) close(fd);
51081195e687SMark J Musante 			return;
51091195e687SMark J Musante 		}
5110fa9e4066Sahrens 
5111fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5112fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
5113fa9e4066Sahrens 			    offset, pathrand);
51141195e687SMark J Musante 
5115420dfc95SChris Siden 		VERIFY(mutex_unlock(&ztest_vdev_lock) == 0);
51161195e687SMark J Musante 
5117420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
51181195e687SMark J Musante 			(void) printf("injected bad word into %s,"
51191195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5120fa9e4066Sahrens 	}
5121fa9e4066Sahrens 
5122fa9e4066Sahrens 	(void) close(fd);
5123fa9e4066Sahrens }
5124fa9e4066Sahrens 
5125fa9e4066Sahrens /*
5126b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
5127fa9e4066Sahrens  */
5128fa9e4066Sahrens void
5129b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5130fa9e4066Sahrens {
5131b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5132420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5133b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5134b24ab676SJeff Bonwick 	ztest_od_t od[1];
5135b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
5136b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
5137b24ab676SJeff Bonwick 	dmu_buf_t *db;
5138b24ab676SJeff Bonwick 	dmu_tx_t *tx;
5139770499e1SDan Kimmel 	abd_t *abd;
5140b24ab676SJeff Bonwick 	blkptr_t blk;
5141b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
5142fa9e4066Sahrens 
5143b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
5144b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
5145fa9e4066Sahrens 
5146b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5147fa9e4066Sahrens 
5148b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5149b24ab676SJeff Bonwick 		return;
5150fa9e4066Sahrens 
5151fa9e4066Sahrens 	/*
5152b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
5153b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
5154fa9e4066Sahrens 	 */
5155420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
5156fa9e4066Sahrens 
5157b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5158b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
5159b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5160b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
5161420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
5162b24ab676SJeff Bonwick 		return;
5163b24ab676SJeff Bonwick 	}
5164b24ab676SJeff Bonwick 
5165aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
5166aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5167aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5168aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5169aab80726SGeorge Wilson 
5170b24ab676SJeff Bonwick 	object = od[0].od_object;
5171b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5172aab80726SGeorge Wilson 	pattern = zs->zs_guid ^ dds.dds_guid;
5173b24ab676SJeff Bonwick 
5174b24ab676SJeff Bonwick 	ASSERT(object != 0);
5175b24ab676SJeff Bonwick 
5176b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5177b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5178b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5179b24ab676SJeff Bonwick 	if (txg == 0) {
5180420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
5181b24ab676SJeff Bonwick 		return;
5182b24ab676SJeff Bonwick 	}
5183fa9e4066Sahrens 
5184fa9e4066Sahrens 	/*
5185b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5186fa9e4066Sahrens 	 */
5187b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5188b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
51893b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
51903b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
51913b2aab18SMatthew Ahrens 		if (error != 0) {
51923b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
51933b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
51943b2aab18SMatthew Ahrens 		}
5195b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5196b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5197b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5198b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5199b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5200b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5201b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5202b24ab676SJeff Bonwick 	}
5203fa9e4066Sahrens 
5204b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5205b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5206fa9e4066Sahrens 
5207fa9e4066Sahrens 	/*
5208b24ab676SJeff Bonwick 	 * Find out what block we got.
5209fa9e4066Sahrens 	 */
521080901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
521180901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5212b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5213b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5214fa9e4066Sahrens 
5215fa9e4066Sahrens 	/*
5216b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5217fa9e4066Sahrens 	 */
5218b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5219770499e1SDan Kimmel 	abd = abd_alloc_linear(psize, B_TRUE);
5220770499e1SDan Kimmel 	ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5221fa9e4066Sahrens 
5222b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5223770499e1SDan Kimmel 	    abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5224b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5225fa9e4066Sahrens 
5226770499e1SDan Kimmel 	abd_free(abd);
5227fa9e4066Sahrens 
5228420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5229fa9e4066Sahrens }
5230fa9e4066Sahrens 
5231fa9e4066Sahrens /*
5232b24ab676SJeff Bonwick  * Scrub the pool.
5233fa9e4066Sahrens  */
5234b24ab676SJeff Bonwick /* ARGSUSED */
5235b24ab676SJeff Bonwick void
5236b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5237fa9e4066Sahrens {
5238420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5239fa9e4066Sahrens 
52403f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5241b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
52423f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5243b24ab676SJeff Bonwick }
5244fa9e4066Sahrens 
5245e9103aaeSGarrett D'Amore /*
5246e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5247e9103aaeSGarrett D'Amore  */
5248e9103aaeSGarrett D'Amore /* ARGSUSED */
5249e9103aaeSGarrett D'Amore void
5250e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5251e9103aaeSGarrett D'Amore {
5252420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5253e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5254dfbb9432SGeorge Wilson 	int error;
5255e9103aaeSGarrett D'Amore 
5256e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5257e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5258dfbb9432SGeorge Wilson 
5259dfbb9432SGeorge Wilson 	(void) rw_wrlock(&ztest_name_lock);
5260dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5261dfbb9432SGeorge Wilson 	(void) rw_unlock(&ztest_name_lock);
5262dfbb9432SGeorge Wilson 
5263dfbb9432SGeorge Wilson 	if (error != 0)
5264e9103aaeSGarrett D'Amore 		return;
5265e9103aaeSGarrett D'Amore 
526625345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5267e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5268e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5269e9103aaeSGarrett D'Amore 	}
5270e9103aaeSGarrett D'Amore 
5271e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5272e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5273e9103aaeSGarrett D'Amore }
5274e9103aaeSGarrett D'Amore 
5275b24ab676SJeff Bonwick /*
5276b24ab676SJeff Bonwick  * Rename the pool to a different name and then rename it back.
5277b24ab676SJeff Bonwick  */
5278b24ab676SJeff Bonwick /* ARGSUSED */
5279b24ab676SJeff Bonwick void
5280b24ab676SJeff Bonwick ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5281b24ab676SJeff Bonwick {
5282b24ab676SJeff Bonwick 	char *oldname, *newname;
5283b24ab676SJeff Bonwick 	spa_t *spa;
5284fa9e4066Sahrens 
5285420dfc95SChris Siden 	(void) rw_wrlock(&ztest_name_lock);
5286fa9e4066Sahrens 
5287420dfc95SChris Siden 	oldname = ztest_opts.zo_pool;
5288b24ab676SJeff Bonwick 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5289b24ab676SJeff Bonwick 	(void) strcpy(newname, oldname);
5290b24ab676SJeff Bonwick 	(void) strcat(newname, "_tmp");
5291fa9e4066Sahrens 
5292fa9e4066Sahrens 	/*
5293b24ab676SJeff Bonwick 	 * Do the rename
5294fa9e4066Sahrens 	 */
5295b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(oldname, newname));
5296fa9e4066Sahrens 
5297fa9e4066Sahrens 	/*
5298b24ab676SJeff Bonwick 	 * Try to open it under the old name, which shouldn't exist
5299fa9e4066Sahrens 	 */
5300b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5301fa9e4066Sahrens 
5302fa9e4066Sahrens 	/*
5303b24ab676SJeff Bonwick 	 * Open it under the new name and make sure it's still the same spa_t.
5304fa9e4066Sahrens 	 */
5305b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5306fa9e4066Sahrens 
5307420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5308b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5309fa9e4066Sahrens 
5310b24ab676SJeff Bonwick 	/*
5311b24ab676SJeff Bonwick 	 * Rename it back to the original
5312b24ab676SJeff Bonwick 	 */
5313b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(newname, oldname));
5314fa9e4066Sahrens 
5315fa9e4066Sahrens 	/*
5316b24ab676SJeff Bonwick 	 * Make sure it can still be opened
5317fa9e4066Sahrens 	 */
5318b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5319fa9e4066Sahrens 
5320420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5321b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5322fa9e4066Sahrens 
5323b24ab676SJeff Bonwick 	umem_free(newname, strlen(newname) + 1);
5324b24ab676SJeff Bonwick 
5325420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5326fa9e4066Sahrens }
5327fa9e4066Sahrens 
5328b24ab676SJeff Bonwick /*
5329b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
5330b24ab676SJeff Bonwick  */
5331fa9e4066Sahrens static void
5332b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
5333fa9e4066Sahrens {
5334fa9e4066Sahrens 	int status;
5335fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5336fa9e4066Sahrens 	char zbuf[1024];
5337fa9e4066Sahrens 	char *bin;
53388654d025Sperrin 	char *ztest;
53398654d025Sperrin 	char *isa;
53408654d025Sperrin 	int isalen;
5341fa9e4066Sahrens 	FILE *fp;
5342fa9e4066Sahrens 
5343fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
5344fa9e4066Sahrens 
5345fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5346fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
53478654d025Sperrin 	ztest = strstr(bin, "/ztest");
53488654d025Sperrin 	isa = bin + 8;
53498654d025Sperrin 	isalen = ztest - isa;
53508654d025Sperrin 	isa = strdup(isa);
5351fa9e4066Sahrens 	/* LINTED */
53523a57275aSck 	(void) sprintf(bin,
5353c3c65d17SPavel Zakharov 	    "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
53548654d025Sperrin 	    isalen,
53558654d025Sperrin 	    isa,
5356420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
5357420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
5358f0ba89beSJeff Bonwick 	    spa_config_path,
535988b7b0f2SMatthew Ahrens 	    pool);
53608654d025Sperrin 	free(isa);
5361fa9e4066Sahrens 
5362420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
5363fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
5364fa9e4066Sahrens 
5365fa9e4066Sahrens 	fp = popen(zdb, "r");
5366fa9e4066Sahrens 
5367fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5368420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
5369fa9e4066Sahrens 			(void) printf("%s", zbuf);
5370fa9e4066Sahrens 
5371fa9e4066Sahrens 	status = pclose(fp);
5372fa9e4066Sahrens 
5373fa9e4066Sahrens 	if (status == 0)
5374fa9e4066Sahrens 		return;
5375fa9e4066Sahrens 
5376fa9e4066Sahrens 	ztest_dump_core = 0;
5377fa9e4066Sahrens 	if (WIFEXITED(status))
5378fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5379fa9e4066Sahrens 	else
5380fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5381fa9e4066Sahrens }
5382fa9e4066Sahrens 
5383fa9e4066Sahrens static void
5384fa9e4066Sahrens ztest_walk_pool_directory(char *header)
5385fa9e4066Sahrens {
5386fa9e4066Sahrens 	spa_t *spa = NULL;
5387fa9e4066Sahrens 
5388420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5389fa9e4066Sahrens 		(void) printf("%s\n", header);
5390fa9e4066Sahrens 
5391fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
5392fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
5393420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
5394fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
5395fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
5396fa9e4066Sahrens }
5397fa9e4066Sahrens 
5398fa9e4066Sahrens static void
5399fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
5400fa9e4066Sahrens {
54018ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
5402fa9e4066Sahrens 	uint64_t pool_guid;
5403fa9e4066Sahrens 	spa_t *spa;
54043b2aab18SMatthew Ahrens 	int error;
5405fa9e4066Sahrens 
5406420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5407fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
5408fa9e4066Sahrens 		    oldname, newname);
5409fa9e4066Sahrens 	}
5410fa9e4066Sahrens 
5411fa9e4066Sahrens 	/*
5412fa9e4066Sahrens 	 * Clean up from previous runs.
5413fa9e4066Sahrens 	 */
5414fa9e4066Sahrens 	(void) spa_destroy(newname);
5415fa9e4066Sahrens 
5416fa9e4066Sahrens 	/*
5417fa9e4066Sahrens 	 * Get the pool's configuration and guid.
5418fa9e4066Sahrens 	 */
5419b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5420fa9e4066Sahrens 
54218ad4d6ddSJeff Bonwick 	/*
54228ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
54238ad4d6ddSJeff Bonwick 	 */
54248ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
54253f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
54268ad4d6ddSJeff Bonwick 
5427fa9e4066Sahrens 	pool_guid = spa_guid(spa);
5428fa9e4066Sahrens 	spa_close(spa, FTAG);
5429fa9e4066Sahrens 
5430fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
5431fa9e4066Sahrens 
5432fa9e4066Sahrens 	/*
5433fa9e4066Sahrens 	 * Export it.
5434fa9e4066Sahrens 	 */
5435b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5436fa9e4066Sahrens 
5437fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
5438fa9e4066Sahrens 
54398ad4d6ddSJeff Bonwick 	/*
54408ad4d6ddSJeff Bonwick 	 * Try to import it.
54418ad4d6ddSJeff Bonwick 	 */
54428ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
54438ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
54448ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
54458ad4d6ddSJeff Bonwick 
5446fa9e4066Sahrens 	/*
5447fa9e4066Sahrens 	 * Import it under the new name.
5448fa9e4066Sahrens 	 */
54493b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
54503b2aab18SMatthew Ahrens 	if (error != 0) {
54513b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
54523b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
54533b2aab18SMatthew Ahrens 		    oldname, newname, error);
54543b2aab18SMatthew Ahrens 	}
5455fa9e4066Sahrens 
5456fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
5457fa9e4066Sahrens 
5458fa9e4066Sahrens 	/*
5459fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
5460fa9e4066Sahrens 	 */
54614b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5462fa9e4066Sahrens 
5463fa9e4066Sahrens 	/*
5464fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
5465fa9e4066Sahrens 	 */
54664b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5467fa9e4066Sahrens 
5468fa9e4066Sahrens 	/*
5469fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
5470fa9e4066Sahrens 	 */
5471b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5472fa9e4066Sahrens 
5473fa9e4066Sahrens 	/*
5474fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
5475fa9e4066Sahrens 	 */
5476b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5477fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
5478fa9e4066Sahrens 	spa_close(spa, FTAG);
5479fa9e4066Sahrens 
5480fa9e4066Sahrens 	nvlist_free(config);
5481fa9e4066Sahrens }
5482fa9e4066Sahrens 
54838ad4d6ddSJeff Bonwick static void
54848ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
54858ad4d6ddSJeff Bonwick {
5486420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5487b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
5488b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
5489b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
5490b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
5491b24ab676SJeff Bonwick 	(void) zio_resume(spa);
54928ad4d6ddSJeff Bonwick }
54938ad4d6ddSJeff Bonwick 
54940a4e9518Sgw static void *
54958ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
54960a4e9518Sgw {
5497e14bb325SJeff Bonwick 	spa_t *spa = arg;
54980a4e9518Sgw 
54990a4e9518Sgw 	while (!ztest_exiting) {
5500b24ab676SJeff Bonwick 		if (spa_suspended(spa))
5501b24ab676SJeff Bonwick 			ztest_resume(spa);
5502b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
5503dcbf3bd6SGeorge Wilson 
5504dcbf3bd6SGeorge Wilson 		/*
5505dcbf3bd6SGeorge Wilson 		 * Periodically change the zfs_compressed_arc_enabled setting.
5506dcbf3bd6SGeorge Wilson 		 */
5507dcbf3bd6SGeorge Wilson 		if (ztest_random(10) == 0)
5508dcbf3bd6SGeorge Wilson 			zfs_compressed_arc_enabled = ztest_random(2);
5509770499e1SDan Kimmel 
5510770499e1SDan Kimmel 		/*
5511770499e1SDan Kimmel 		 * Periodically change the zfs_abd_scatter_enabled setting.
5512770499e1SDan Kimmel 		 */
5513770499e1SDan Kimmel 		if (ztest_random(10) == 0)
5514770499e1SDan Kimmel 			zfs_abd_scatter_enabled = ztest_random(2);
55150a4e9518Sgw 	}
55160a4e9518Sgw 	return (NULL);
55170a4e9518Sgw }
55180a4e9518Sgw 
5519b24ab676SJeff Bonwick static void *
5520b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
5521b24ab676SJeff Bonwick {
5522b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
55232c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
55242c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
5525b24ab676SJeff Bonwick 
55262c1e2b44SGeorge Wilson 	for (;;) {
552769962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
552869962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
5529b24ab676SJeff Bonwick 
553069962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5531b24ab676SJeff Bonwick 
55322c1e2b44SGeorge Wilson 		/*
55332c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
55342c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
55352c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
55362c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
55372c1e2b44SGeorge Wilson 		 */
55380713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
55392c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
55402c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
554169962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
55422c1e2b44SGeorge Wilson 			return (NULL);
55432c1e2b44SGeorge Wilson 		}
55442c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
5545b24ab676SJeff Bonwick 
554669962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
55472c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
55482c1e2b44SGeorge Wilson 		    total);
55492c1e2b44SGeorge Wilson 	}
5550b24ab676SJeff Bonwick }
5551b24ab676SJeff Bonwick 
5552b24ab676SJeff Bonwick static void
5553420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5554b24ab676SJeff Bonwick {
5555420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5556420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5557b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
5558b24ab676SJeff Bonwick 
5559b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
5560b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
5561b24ab676SJeff Bonwick 
5562b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
5563b24ab676SJeff Bonwick 
5564420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
5565420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
5566b24ab676SJeff Bonwick 
5567420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5568b24ab676SJeff Bonwick 		Dl_info dli;
5569b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
5570b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
5571b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
5572b24ab676SJeff Bonwick 	}
5573b24ab676SJeff Bonwick }
5574b24ab676SJeff Bonwick 
5575fa9e4066Sahrens static void *
5576fa9e4066Sahrens ztest_thread(void *arg)
5577fa9e4066Sahrens {
5578420dfc95SChris Siden 	int rand;
5579b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
5580fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
5581b24ab676SJeff Bonwick 	uint64_t call_next;
5582b24ab676SJeff Bonwick 	hrtime_t now;
5583fa9e4066Sahrens 	ztest_info_t *zi;
5584420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
5585fa9e4066Sahrens 
5586b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
5587fa9e4066Sahrens 		/*
5588fa9e4066Sahrens 		 * See if it's time to force a crash.
5589fa9e4066Sahrens 		 */
5590b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
5591b24ab676SJeff Bonwick 			ztest_kill(zs);
5592fa9e4066Sahrens 
5593fa9e4066Sahrens 		/*
5594b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
5595fa9e4066Sahrens 		 */
5596b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
5597b24ab676SJeff Bonwick 			break;
5598fa9e4066Sahrens 
5599fa9e4066Sahrens 		/*
5600b24ab676SJeff Bonwick 		 * Pick a random function to execute.
5601fa9e4066Sahrens 		 */
5602420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
5603420dfc95SChris Siden 		zi = &ztest_info[rand];
5604420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5605420dfc95SChris Siden 		call_next = zc->zc_next;
5606b24ab676SJeff Bonwick 
5607b24ab676SJeff Bonwick 		if (now >= call_next &&
5608420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
5609420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5610420dfc95SChris Siden 			ztest_execute(rand, zi, id);
5611420dfc95SChris Siden 		}
5612b24ab676SJeff Bonwick 	}
5613fa9e4066Sahrens 
5614b24ab676SJeff Bonwick 	return (NULL);
5615b24ab676SJeff Bonwick }
5616fa9e4066Sahrens 
5617b24ab676SJeff Bonwick static void
5618b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
5619b24ab676SJeff Bonwick {
56209adfa60dSMatthew Ahrens 	(void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5621b24ab676SJeff Bonwick }
5622fa9e4066Sahrens 
5623b24ab676SJeff Bonwick static void
5624420dfc95SChris Siden ztest_dataset_destroy(int d)
5625b24ab676SJeff Bonwick {
56269adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
5627fa9e4066Sahrens 
5628420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5629fa9e4066Sahrens 
5630420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
5631b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
5632fa9e4066Sahrens 
5633b24ab676SJeff Bonwick 	/*
5634b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
5635b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
5636b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
5637b24ab676SJeff Bonwick 	 */
5638420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
5639420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
5640b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
5641420dfc95SChris Siden 	}
5642fa9e4066Sahrens 
5643b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5644b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5645b24ab676SJeff Bonwick }
5646b24ab676SJeff Bonwick 
5647b24ab676SJeff Bonwick static void
5648b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5649b24ab676SJeff Bonwick {
5650b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
5651b24ab676SJeff Bonwick 
5652b24ab676SJeff Bonwick 	/*
5653b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
5654b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
5655b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5656b24ab676SJeff Bonwick 	 * If not, we have an object leak.
5657b24ab676SJeff Bonwick 	 *
5658b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
5659b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
5660b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
5661b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
5662b24ab676SJeff Bonwick 	 */
5663b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5664b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5665b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
5666b24ab676SJeff Bonwick }
5667b24ab676SJeff Bonwick 
5668b24ab676SJeff Bonwick static int
5669420dfc95SChris Siden ztest_dataset_open(int d)
5670b24ab676SJeff Bonwick {
5671420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5672420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5673b24ab676SJeff Bonwick 	objset_t *os;
5674b24ab676SJeff Bonwick 	zilog_t *zilog;
56759adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
5676b24ab676SJeff Bonwick 	int error;
5677b24ab676SJeff Bonwick 
5678420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5679b24ab676SJeff Bonwick 
5680420dfc95SChris Siden 	(void) rw_rdlock(&ztest_name_lock);
5681b24ab676SJeff Bonwick 
568255da60b9SMark J Musante 	error = ztest_dataset_create(name);
5683b24ab676SJeff Bonwick 	if (error == ENOSPC) {
5684420dfc95SChris Siden 		(void) rw_unlock(&ztest_name_lock);
5685b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
5686b24ab676SJeff Bonwick 		return (error);
5687fa9e4066Sahrens 	}
5688b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
5689fa9e4066Sahrens 
56903b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5691420dfc95SChris Siden 	(void) rw_unlock(&ztest_name_lock);
5692b24ab676SJeff Bonwick 
5693420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5694b24ab676SJeff Bonwick 
5695b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
5696b24ab676SJeff Bonwick 
5697b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5698b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
5699b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
5700b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
5701b24ab676SJeff Bonwick 
5702b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5703b24ab676SJeff Bonwick 
5704b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
5705b24ab676SJeff Bonwick 
5706b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5707b24ab676SJeff Bonwick 
5708420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5709b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5710b24ab676SJeff Bonwick 		    zd->zd_name,
5711b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
5712b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
5713b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
5714b24ab676SJeff Bonwick 
5715b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
5716b24ab676SJeff Bonwick 
5717b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
5718b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
5719b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
5720b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
5721b24ab676SJeff Bonwick 
5722b24ab676SJeff Bonwick 	return (0);
5723b24ab676SJeff Bonwick }
5724b24ab676SJeff Bonwick 
5725b24ab676SJeff Bonwick static void
5726420dfc95SChris Siden ztest_dataset_close(int d)
5727b24ab676SJeff Bonwick {
5728420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5729b24ab676SJeff Bonwick 
5730b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
57313b2aab18SMatthew Ahrens 	dmu_objset_disown(zd->zd_os, zd);
5732b24ab676SJeff Bonwick 
5733b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
5734fa9e4066Sahrens }
5735fa9e4066Sahrens 
5736fa9e4066Sahrens /*
5737fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
5738fa9e4066Sahrens  */
5739fa9e4066Sahrens static void
5740b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
5741fa9e4066Sahrens {
5742b24ab676SJeff Bonwick 	thread_t *tid;
5743fa9e4066Sahrens 	spa_t *spa;
5744e9103aaeSGarrett D'Amore 	objset_t *os;
5745e14bb325SJeff Bonwick 	thread_t resume_tid;
5746b24ab676SJeff Bonwick 	int error;
5747e14bb325SJeff Bonwick 
5748e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
5749fa9e4066Sahrens 
5750fa9e4066Sahrens 	/*
5751b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
5752fa9e4066Sahrens 	 */
5753420dfc95SChris Siden 	VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
5754420dfc95SChris Siden 	VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5755fa9e4066Sahrens 
5756b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
5757420dfc95SChris Siden 	zs->zs_thread_stop =
5758420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5759b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5760b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
5761420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
5762420dfc95SChris Siden 		zs->zs_thread_kill -=
5763420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
5764420dfc95SChris Siden 	}
5765fa9e4066Sahrens 
5766b24ab676SJeff Bonwick 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5767fa9e4066Sahrens 
5768b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5769b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
5770fa9e4066Sahrens 
57710a4e9518Sgw 	/*
5772e14bb325SJeff Bonwick 	 * Open our pool.
57730a4e9518Sgw 	 */
5774b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
57753b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
577609c9d376SGeorge Wilson 	spa->spa_debug = B_TRUE;
577730beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
5778420dfc95SChris Siden 	ztest_spa = spa;
5779b24ab676SJeff Bonwick 
5780aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
57813b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
57823b2aab18SMatthew Ahrens 	    DMU_OST_ANY, B_TRUE, FTAG, &os));
5783aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5784aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5785aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5786aab80726SGeorge Wilson 	zs->zs_guid = dds.dds_guid;
57873b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
5788e9103aaeSGarrett D'Amore 
5789b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
57900a4e9518Sgw 
57918ad4d6ddSJeff Bonwick 	/*
57928ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
57938ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
57948ad4d6ddSJeff Bonwick 	 * the only valid replica.
57958ad4d6ddSJeff Bonwick 	 */
57961195e687SMark J Musante 	if (MAXFAULTS() == 0)
57978ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
57988ad4d6ddSJeff Bonwick 	else
57998ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
58008ad4d6ddSJeff Bonwick 
5801fa9e4066Sahrens 	/*
5802e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
5803fa9e4066Sahrens 	 */
58048ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5805e14bb325SJeff Bonwick 	    &resume_tid) == 0);
5806fa9e4066Sahrens 
5807b24ab676SJeff Bonwick 	/*
5808b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
5809b24ab676SJeff Bonwick 	 */
5810b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5811b24ab676SJeff Bonwick 	    NULL) == 0);
5812b24ab676SJeff Bonwick 
5813fa9e4066Sahrens 	/*
5814fa9e4066Sahrens 	 * Verify that we can safely inquire about about any object,
5815fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
5816fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
5817fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
5818fa9e4066Sahrens 	 */
5819b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
5820b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
5821fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5822fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
5823ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
5824ea8dc4b6Seschrock 			    error == EINVAL);
5825fa9e4066Sahrens 		}
5826fa9e4066Sahrens 	}
5827fa9e4066Sahrens 
5828fa9e4066Sahrens 	/*
5829b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
5830fa9e4066Sahrens 	 */
5831b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
5832420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
5833420dfc95SChris Siden 		ztest_dataset_destroy(d);
5834b24ab676SJeff Bonwick 	}
5835fa9e4066Sahrens 	zs->zs_enospc_count = 0;
5836fa9e4066Sahrens 
5837420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
5838420dfc95SChris Siden 	    UMEM_NOFAIL);
5839fa9e4066Sahrens 
5840420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
5841fa9e4066Sahrens 		(void) printf("starting main threads...\n");
5842fa9e4066Sahrens 
5843b24ab676SJeff Bonwick 	/*
5844b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
5845b24ab676SJeff Bonwick 	 */
5846420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
5847420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
5848420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
5849b24ab676SJeff Bonwick 			return;
5850b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5851b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
5852fa9e4066Sahrens 	}
5853fa9e4066Sahrens 
5854b24ab676SJeff Bonwick 	/*
5855b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
5856b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
5857b24ab676SJeff Bonwick 	 */
5858420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5859b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5860420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
5861420dfc95SChris Siden 			ztest_dataset_close(t);
5862fa9e4066Sahrens 	}
5863fa9e4066Sahrens 
5864fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
5865fa9e4066Sahrens 
5866b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5867b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5868b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
5869b24ab676SJeff Bonwick 
5870420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
5871b24ab676SJeff Bonwick 
5872b24ab676SJeff Bonwick 	/* Kill the resume thread */
5873b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
5874b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5875b24ab676SJeff Bonwick 	ztest_resume(spa);
5876b24ab676SJeff Bonwick 
5877b24ab676SJeff Bonwick 	/*
5878b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
5879b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
5880b24ab676SJeff Bonwick 	 */
5881a2cdcdd2SPaul Dagnelie 	for (uint64_t object = 1; object < 50; object++) {
5882a2cdcdd2SPaul Dagnelie 		dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
5883a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
5884a2cdcdd2SPaul Dagnelie 	}
5885b24ab676SJeff Bonwick 
5886b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5887fa9e4066Sahrens 
5888fa9e4066Sahrens 	/*
5889b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
5890fa9e4066Sahrens 	 */
5891b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
5892b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5893420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
5894b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
5895b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
5896b24ab676SJeff Bonwick 
5897b24ab676SJeff Bonwick 	/*
5898b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
5899b24ab676SJeff Bonwick 	 * different name.
5900b24ab676SJeff Bonwick 	 */
5901b24ab676SJeff Bonwick 	if (ztest_random(2) == 0) {
59029adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
59039adfa60dSMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s_import",
5904420dfc95SChris Siden 		    ztest_opts.zo_pool);
5905420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
5906420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
5907b24ab676SJeff Bonwick 	}
5908b24ab676SJeff Bonwick 
5909b24ab676SJeff Bonwick 	kernel_fini();
591026cb54a9SGeorge Wilson 
591126cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
591226cb54a9SGeorge Wilson 
591326cb54a9SGeorge Wilson 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
591426cb54a9SGeorge Wilson 
5915420dfc95SChris Siden 	(void) rwlock_destroy(&ztest_name_lock);
5916420dfc95SChris Siden 	(void) _mutex_destroy(&ztest_vdev_lock);
5917b24ab676SJeff Bonwick }
5918b24ab676SJeff Bonwick 
5919b24ab676SJeff Bonwick static void
5920420dfc95SChris Siden ztest_freeze(void)
5921b24ab676SJeff Bonwick {
5922420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
5923b24ab676SJeff Bonwick 	spa_t *spa;
59242215e990SMark J Musante 	int numloops = 0;
5925b24ab676SJeff Bonwick 
5926420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
5927b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
5928718d718aSGeorge Wilson 
5929b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
5930b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5931b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
593280901aeaSGeorge Wilson 	spa->spa_debug = B_TRUE;
593380901aeaSGeorge Wilson 	ztest_spa = spa;
5934718d718aSGeorge Wilson 
5935b24ab676SJeff Bonwick 	/*
5936b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
5937b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
5938b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
5939b24ab676SJeff Bonwick 	 */
5940b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5941b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
59425002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
5943fa9e4066Sahrens 	}
5944fa9e4066Sahrens 
5945ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5946fa9e4066Sahrens 
5947b24ab676SJeff Bonwick 	/*
5948b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
5949b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
5950b24ab676SJeff Bonwick 	 */
5951b24ab676SJeff Bonwick 	spa_freeze(spa);
5952e14bb325SJeff Bonwick 
59532a104a52SAlex Reece 	/*
59542a104a52SAlex Reece 	 * Because it is hard to predict how much space a write will actually
59552a104a52SAlex Reece 	 * require beforehand, we leave ourselves some fudge space to write over
59562a104a52SAlex Reece 	 * capacity.
59572a104a52SAlex Reece 	 */
59582a104a52SAlex Reece 	uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
59592a104a52SAlex Reece 
5960b24ab676SJeff Bonwick 	/*
5961b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
5962b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5963b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
5964b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
5965b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
59662a104a52SAlex Reece 	 *
59672a104a52SAlex Reece 	 * Run a random number of times less than zo_maxloops and ensure we do
59682a104a52SAlex Reece 	 * not run out of space on the pool.
5969b24ab676SJeff Bonwick 	 */
5970420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
59712a104a52SAlex Reece 	    numloops++ < ztest_opts.zo_maxloops &&
59722a104a52SAlex Reece 	    metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
59732a104a52SAlex Reece 		ztest_od_t od;
59742a104a52SAlex Reece 		ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
59752a104a52SAlex Reece 		VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
59762a104a52SAlex Reece 		ztest_io(zd, od.od_object,
59772a104a52SAlex Reece 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
5978b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
5979b24ab676SJeff Bonwick 	}
5980e14bb325SJeff Bonwick 
5981fa9e4066Sahrens 	/*
5982b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
5983fa9e4066Sahrens 	 */
59845002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
5985b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
5986fa9e4066Sahrens 
5987b24ab676SJeff Bonwick 	/*
5988b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
5989b24ab676SJeff Bonwick 	 */
5990420dfc95SChris Siden 	ztest_dataset_close(0);
5991e05725b1Sbonwick 	spa_close(spa, FTAG);
5992b24ab676SJeff Bonwick 	kernel_fini();
5993e05725b1Sbonwick 
5994b24ab676SJeff Bonwick 	/*
5995b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
5996b24ab676SJeff Bonwick 	 */
5997b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
5998b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5999ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6000b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
6001420dfc95SChris Siden 	ztest_dataset_close(0);
6002dfbb9432SGeorge Wilson 
6003dfbb9432SGeorge Wilson 	spa->spa_debug = B_TRUE;
6004dfbb9432SGeorge Wilson 	ztest_spa = spa;
6005dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
6006dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
6007dfbb9432SGeorge Wilson 
6008b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6009fa9e4066Sahrens 	kernel_fini();
6010fa9e4066Sahrens }
6011fa9e4066Sahrens 
6012fa9e4066Sahrens void
6013fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
6014fa9e4066Sahrens {
6015fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
6016fa9e4066Sahrens 	hrtime_t m = s / 60;
6017fa9e4066Sahrens 	hrtime_t h = m / 60;
6018fa9e4066Sahrens 	hrtime_t d = h / 24;
6019fa9e4066Sahrens 
6020fa9e4066Sahrens 	s -= m * 60;
6021fa9e4066Sahrens 	m -= h * 60;
6022fa9e4066Sahrens 	h -= d * 24;
6023fa9e4066Sahrens 
6024fa9e4066Sahrens 	timebuf[0] = '\0';
6025fa9e4066Sahrens 
6026fa9e4066Sahrens 	if (d)
6027fa9e4066Sahrens 		(void) sprintf(timebuf,
6028fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
6029fa9e4066Sahrens 	else if (h)
6030fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6031fa9e4066Sahrens 	else if (m)
6032fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
6033fa9e4066Sahrens 	else
6034fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
6035fa9e4066Sahrens }
6036fa9e4066Sahrens 
60371195e687SMark J Musante static nvlist_t *
60381195e687SMark J Musante make_random_props()
60391195e687SMark J Musante {
60401195e687SMark J Musante 	nvlist_t *props;
60411195e687SMark J Musante 
60421195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6043ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
6044ad135b5dSChristopher Siden 		return (props);
60451195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
60461195e687SMark J Musante 
60471195e687SMark J Musante 	return (props);
60481195e687SMark J Musante }
60491195e687SMark J Musante 
6050fa9e4066Sahrens /*
6051fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
6052b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
6053fa9e4066Sahrens  */
6054fa9e4066Sahrens static void
6055b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
6056fa9e4066Sahrens {
6057fa9e4066Sahrens 	spa_t *spa;
60581195e687SMark J Musante 	nvlist_t *nvroot, *props;
6059fa9e4066Sahrens 
6060420dfc95SChris Siden 	VERIFY(_mutex_init(&ztest_vdev_lock, USYNC_THREAD, NULL) == 0);
6061420dfc95SChris Siden 	VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6062b24ab676SJeff Bonwick 
6063fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
6064fa9e4066Sahrens 
6065fa9e4066Sahrens 	/*
6066fa9e4066Sahrens 	 * Create the storage pool.
6067fa9e4066Sahrens 	 */
6068420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
606988ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
60701195e687SMark J Musante 	zs->zs_splits = 0;
6071420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
607225345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6073420dfc95SChris Siden 	    0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
60741195e687SMark J Musante 	props = make_random_props();
6075ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
6076ad135b5dSChristopher Siden 		char buf[1024];
6077ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
6078ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
6079b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6080ad135b5dSChristopher Siden 	}
6081b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6082fa9e4066Sahrens 	nvlist_free(nvroot);
60831df447ebSRichard Yao 	nvlist_free(props);
6084fa9e4066Sahrens 
6085b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6086420dfc95SChris Siden 	zs->zs_metaslab_sz =
6087420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6088ad135b5dSChristopher Siden 
6089fa9e4066Sahrens 	spa_close(spa, FTAG);
6090fa9e4066Sahrens 
6091fa9e4066Sahrens 	kernel_fini();
6092b24ab676SJeff Bonwick 
6093420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
6094420dfc95SChris Siden 
6095420dfc95SChris Siden 	ztest_freeze();
6096420dfc95SChris Siden 
6097420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
6098420dfc95SChris Siden 
6099420dfc95SChris Siden 	(void) rwlock_destroy(&ztest_name_lock);
6100420dfc95SChris Siden 	(void) _mutex_destroy(&ztest_vdev_lock);
6101420dfc95SChris Siden }
6102420dfc95SChris Siden 
6103420dfc95SChris Siden static void
6104741652b0SEtienne Dechamps setup_data_fd(void)
6105420dfc95SChris Siden {
6106741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6107420dfc95SChris Siden 
6108741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
6109741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
6110741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
6111420dfc95SChris Siden }
6112420dfc95SChris Siden 
6113741652b0SEtienne Dechamps 
6114ad135b5dSChristopher Siden static int
6115ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
6116ad135b5dSChristopher Siden {
6117ad135b5dSChristopher Siden 	int size;
6118ad135b5dSChristopher Siden 
6119ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
6120ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
6121ad135b5dSChristopher Siden 	size += hdr->zh_size;
6122ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
6123ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
6124ad135b5dSChristopher Siden 
6125ad135b5dSChristopher Siden 	return (size);
6126ad135b5dSChristopher Siden }
6127ad135b5dSChristopher Siden 
6128420dfc95SChris Siden static void
6129420dfc95SChris Siden setup_hdr(void)
6130420dfc95SChris Siden {
6131ad135b5dSChristopher Siden 	int size;
6132420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6133420dfc95SChris Siden 
6134420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6135741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6136420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6137420dfc95SChris Siden 
6138741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6139ad135b5dSChristopher Siden 
6140420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6141420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6142420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
6143420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6144420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
6145420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6146420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
6147420dfc95SChris Siden 
6148ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6149741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6150ad135b5dSChristopher Siden 
6151420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6152420dfc95SChris Siden }
6153420dfc95SChris Siden 
6154420dfc95SChris Siden static void
6155420dfc95SChris Siden setup_data(void)
6156420dfc95SChris Siden {
6157420dfc95SChris Siden 	int size, offset;
6158420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6159420dfc95SChris Siden 	uint8_t *buf;
6160420dfc95SChris Siden 
6161420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6162741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6163420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6164420dfc95SChris Siden 
6165ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6166420dfc95SChris Siden 
6167420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6168420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6169741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6170420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6171420dfc95SChris Siden 	buf = (uint8_t *)hdr;
6172420dfc95SChris Siden 
6173420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
6174420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
6175420dfc95SChris Siden 	offset += hdr->zh_opts_size;
6176420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
6177420dfc95SChris Siden 	offset += hdr->zh_size;
6178420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
6179420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
6180420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
6181420dfc95SChris Siden }
6182420dfc95SChris Siden 
6183420dfc95SChris Siden static boolean_t
6184420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6185420dfc95SChris Siden {
6186420dfc95SChris Siden 	pid_t pid;
6187420dfc95SChris Siden 	int status;
6188741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
6189420dfc95SChris Siden 
6190420dfc95SChris Siden 	pid = fork();
6191420dfc95SChris Siden 
6192420dfc95SChris Siden 	if (cmd == NULL) {
6193741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6194741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6195420dfc95SChris Siden 		cmd = cmdbuf;
6196420dfc95SChris Siden 	}
6197b24ab676SJeff Bonwick 
6198420dfc95SChris Siden 	if (pid == -1)
6199420dfc95SChris Siden 		fatal(1, "fork failed");
6200b24ab676SJeff Bonwick 
6201420dfc95SChris Siden 	if (pid == 0) {	/* child */
6202420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
6203741652b0SEtienne Dechamps 		char fd_data_str[12];
620426cb54a9SGeorge Wilson 
6205420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
6206420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
6207741652b0SEtienne Dechamps 
6208741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
6209741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
6210741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6211741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6212741652b0SEtienne Dechamps 
6213420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
6214420dfc95SChris Siden 		if (libpath != NULL)
6215420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6216420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
6217420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
6218420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
6219420dfc95SChris Siden 	}
6220420dfc95SChris Siden 
6221741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
6222741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
6223741652b0SEtienne Dechamps 		cmd = NULL;
6224741652b0SEtienne Dechamps 	}
6225741652b0SEtienne Dechamps 
6226420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
6227420dfc95SChris Siden 		continue;
6228420dfc95SChris Siden 	if (statusp != NULL)
6229420dfc95SChris Siden 		*statusp = status;
6230420dfc95SChris Siden 
6231420dfc95SChris Siden 	if (WIFEXITED(status)) {
6232420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
6233420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
6234420dfc95SChris Siden 			    WEXITSTATUS(status));
6235420dfc95SChris Siden 			exit(2);
6236420dfc95SChris Siden 		}
6237420dfc95SChris Siden 		return (B_FALSE);
6238420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
6239420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6240420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
6241420dfc95SChris Siden 			    WTERMSIG(status));
6242420dfc95SChris Siden 			exit(3);
6243420dfc95SChris Siden 		}
6244420dfc95SChris Siden 		return (B_TRUE);
6245420dfc95SChris Siden 	} else {
6246420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
6247420dfc95SChris Siden 		exit(4);
6248420dfc95SChris Siden 		/* NOTREACHED */
6249420dfc95SChris Siden 	}
6250420dfc95SChris Siden }
6251420dfc95SChris Siden 
6252420dfc95SChris Siden static void
6253420dfc95SChris Siden ztest_run_init(void)
6254420dfc95SChris Siden {
6255420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
6256420dfc95SChris Siden 
6257420dfc95SChris Siden 	ASSERT(ztest_opts.zo_init != 0);
6258420dfc95SChris Siden 
6259420dfc95SChris Siden 	/*
6260420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
6261420dfc95SChris Siden 	 */
6262420dfc95SChris Siden 	(void) remove(spa_config_path);
6263420dfc95SChris Siden 
6264420dfc95SChris Siden 	/*
6265420dfc95SChris Siden 	 * Create and initialize our storage pool.
6266420dfc95SChris Siden 	 */
6267420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
6268420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
6269420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
6270420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
6271420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
6272420dfc95SChris Siden 		}
6273420dfc95SChris Siden 		ztest_init(zs);
6274420dfc95SChris Siden 	}
6275fa9e4066Sahrens }
6276fa9e4066Sahrens 
6277fa9e4066Sahrens int
6278fa9e4066Sahrens main(int argc, char **argv)
6279fa9e4066Sahrens {
6280fa9e4066Sahrens 	int kills = 0;
6281fa9e4066Sahrens 	int iters = 0;
6282420dfc95SChris Siden 	int older = 0;
6283420dfc95SChris Siden 	int newer = 0;
6284fa9e4066Sahrens 	ztest_shared_t *zs;
6285fa9e4066Sahrens 	ztest_info_t *zi;
6286420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6287fa9e4066Sahrens 	char timebuf[100];
62880a055120SJason King 	char numbuf[NN_NUMBUF_SZ];
6289b24ab676SJeff Bonwick 	spa_t *spa;
6290741652b0SEtienne Dechamps 	char *cmd;
6291420dfc95SChris Siden 	boolean_t hasalt;
6292741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
6293fa9e4066Sahrens 
6294fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
6295fa9e4066Sahrens 
6296cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
629769962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
6298cd1c8b85SMatthew Ahrens 
6299741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6300741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
6301741652b0SEtienne Dechamps 
6302741652b0SEtienne Dechamps 	if (!fd_data_str) {
6303420dfc95SChris Siden 		process_options(argc, argv);
6304fa9e4066Sahrens 
6305741652b0SEtienne Dechamps 		setup_data_fd();
6306420dfc95SChris Siden 		setup_hdr();
6307420dfc95SChris Siden 		setup_data();
6308420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
6309420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
6310420dfc95SChris Siden 	} else {
6311741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
6312420dfc95SChris Siden 		setup_data();
6313420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6314420dfc95SChris Siden 	}
6315420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6316fa9e4066Sahrens 
6317f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
6318741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6319741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
6320f0ba89beSJeff Bonwick 
6321420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6322420dfc95SChris Siden 	    UMEM_NOFAIL);
6323420dfc95SChris Siden 	zs = ztest_shared;
6324ea8dc4b6Seschrock 
6325741652b0SEtienne Dechamps 	if (fd_data_str) {
6326420dfc95SChris Siden 		metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6327420dfc95SChris Siden 		metaslab_df_alloc_threshold =
6328420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
6329b24ab676SJeff Bonwick 
6330420dfc95SChris Siden 		if (zs->zs_do_init)
6331420dfc95SChris Siden 			ztest_run_init();
6332420dfc95SChris Siden 		else
6333420dfc95SChris Siden 			ztest_run(zs);
6334420dfc95SChris Siden 		exit(0);
6335420dfc95SChris Siden 	}
6336fa9e4066Sahrens 
6337420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6338420dfc95SChris Siden 
6339420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6340fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
6341fa9e4066Sahrens 		    " %llu seconds...\n",
6342420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
6343420dfc95SChris Siden 		    ztest_opts.zo_datasets,
6344420dfc95SChris Siden 		    ztest_opts.zo_threads,
6345420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
6346fa9e4066Sahrens 	}
6347fa9e4066Sahrens 
6348741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6349741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6350420dfc95SChris Siden 
6351420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
6352420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6353420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6354420dfc95SChris Siden 			(void) printf("Executing older ztest for "
6355420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
6356420dfc95SChris Siden 		}
6357420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6358420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6359420dfc95SChris Siden 	} else {
6360420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6361fa9e4066Sahrens 	}
6362420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
6363fa9e4066Sahrens 
6364b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
6365420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6366fa9e4066Sahrens 
6367b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
6368420dfc95SChris Siden 		zi = &ztest_info[f];
6369420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
6370b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6371420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
6372fa9e4066Sahrens 		else
6373420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
6374b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
6375fa9e4066Sahrens 	}
6376fa9e4066Sahrens 
6377fa9e4066Sahrens 	/*
6378fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
6379fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
6380fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
6381fa9e4066Sahrens 	 */
6382b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
6383fa9e4066Sahrens 		int status;
6384420dfc95SChris Siden 		boolean_t killed;
6385fa9e4066Sahrens 
6386fa9e4066Sahrens 		/*
6387fa9e4066Sahrens 		 * Initialize the workload counters for each function.
6388fa9e4066Sahrens 		 */
6389b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
6390420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
6391420dfc95SChris Siden 			zc->zc_count = 0;
6392420dfc95SChris Siden 			zc->zc_time = 0;
6393fa9e4066Sahrens 		}
6394fa9e4066Sahrens 
6395d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
6396420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
6397420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
6398fa9e4066Sahrens 
6399420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
6400420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6401420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
6402420dfc95SChris Siden 				    cmd);
6403fa9e4066Sahrens 			}
6404420dfc95SChris Siden 			newer++;
6405420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
64065ad82045Snd 		} else {
6407420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6408420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
6409420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
6410420dfc95SChris Siden 			}
6411420dfc95SChris Siden 			older++;
6412420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
6413420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
6414fa9e4066Sahrens 		}
6415fa9e4066Sahrens 
6416420dfc95SChris Siden 		if (killed)
6417420dfc95SChris Siden 			kills++;
6418fa9e4066Sahrens 		iters++;
6419fa9e4066Sahrens 
6420420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6421fa9e4066Sahrens 			hrtime_t now = gethrtime();
6422fa9e4066Sahrens 
6423b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
6424b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
64250a055120SJason King 			nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6426fa9e4066Sahrens 
6427fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6428fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6429fa9e4066Sahrens 			    iters,
6430fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
6431fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
6432fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
6433fa9e4066Sahrens 			    numbuf,
6434b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
6435420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
6436fa9e4066Sahrens 		}
6437fa9e4066Sahrens 
6438420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
6439fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
6440fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6441fa9e4066Sahrens 			    "Calls", "Time", "Function");
6442fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6443fa9e4066Sahrens 			    "-----", "----", "--------");
6444b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
6445fa9e4066Sahrens 				Dl_info dli;
6446fa9e4066Sahrens 
6447420dfc95SChris Siden 				zi = &ztest_info[f];
6448420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
6449420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
6450fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
6451fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
6452420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
6453fa9e4066Sahrens 				    dli.dli_sname);
6454fa9e4066Sahrens 			}
6455fa9e4066Sahrens 			(void) printf("\n");
6456fa9e4066Sahrens 		}
6457fa9e4066Sahrens 
6458fa9e4066Sahrens 		/*
6459b24ab676SJeff Bonwick 		 * It's possible that we killed a child during a rename test,
6460b24ab676SJeff Bonwick 		 * in which case we'll have a 'ztest_tmp' pool lying around
6461b24ab676SJeff Bonwick 		 * instead of 'ztest'.  Do a blind rename in case this happened.
6462fa9e4066Sahrens 		 */
6463b24ab676SJeff Bonwick 		kernel_init(FREAD);
6464420dfc95SChris Siden 		if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6465b24ab676SJeff Bonwick 			spa_close(spa, FTAG);
6466b24ab676SJeff Bonwick 		} else {
64679adfa60dSMatthew Ahrens 			char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6468b24ab676SJeff Bonwick 			kernel_fini();
6469b24ab676SJeff Bonwick 			kernel_init(FREAD | FWRITE);
6470b24ab676SJeff Bonwick 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6471420dfc95SChris Siden 			    ztest_opts.zo_pool);
6472420dfc95SChris Siden 			(void) spa_rename(tmpname, ztest_opts.zo_pool);
6473b24ab676SJeff Bonwick 		}
6474fa9e4066Sahrens 		kernel_fini();
6475fa9e4066Sahrens 
6476420dfc95SChris Siden 		ztest_run_zdb(ztest_opts.zo_pool);
6477b24ab676SJeff Bonwick 	}
6478fa9e4066Sahrens 
6479420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6480420dfc95SChris Siden 		if (hasalt) {
6481420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
6482420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
6483420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
6484420dfc95SChris Siden 			    cmd);
6485420dfc95SChris Siden 		}
6486fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6487fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6488fa9e4066Sahrens 	}
6489fa9e4066Sahrens 
6490741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
6491741652b0SEtienne Dechamps 
6492fa9e4066Sahrens 	return (0);
6493fa9e4066Sahrens }
6494