xref: /illumos-gate/usr/src/cmd/ztest/ztest.c (revision 094e47e9)
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.
2321f7c81cSMatthew Ahrens  * Copyright (c) 2011, 2017 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.
28f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens /*
32fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
33fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
34fa9e4066Sahrens  *
35fa9e4066Sahrens  * The overall design of the ztest program is as follows:
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
38fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
39fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
40fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
41fa9e4066Sahrens  *
42fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
43fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
44fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
45fa9e4066Sahrens  *
46fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
47fa9e4066Sahrens  *     verify that self-healing data really works.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
50fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
51fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
54fa9e4066Sahrens  *     we run the entire test in a child of the main process.
55fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
56fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
57fa9e4066Sahrens  *     The parent then runs the test again, using the existing
585d7b4d43SMatthew Ahrens  *     storage pool, as many times as desired. If backwards compatibility
59420dfc95SChris Siden  *     testing is enabled ztest will sometimes run the "older" version
60420dfc95SChris Siden  *     of ztest after a SIGKILL.
61fa9e4066Sahrens  *
62fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
63fa9e4066Sahrens  *     many of the functional tests record the transaction group number
64fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
65fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
66fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
67fa9e4066Sahrens  *
68fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
69fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
70fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
71fa9e4066Sahrens  *
72fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
73fa9e4066Sahrens  *
74fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
76fa9e4066Sahrens  *
77420dfc95SChris Siden  * Use the -k option to set the desired frequency of kills.
78420dfc95SChris Siden  *
79420dfc95SChris Siden  * When ztest invokes itself it passes all relevant information through a
80420dfc95SChris Siden  * temporary file which is mmap-ed in the child process. This allows shared
81420dfc95SChris Siden  * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82420dfc95SChris Siden  * stored at offset 0 of this file and contains information on the size and
83420dfc95SChris Siden  * number of shared structures in the file. The information stored in this file
84420dfc95SChris Siden  * must remain backwards compatible with older versions of ztest so that
85420dfc95SChris Siden  * ztest can invoke them during backwards compatibility testing (-B).
86fa9e4066Sahrens  */
87fa9e4066Sahrens 
88fa9e4066Sahrens #include <sys/zfs_context.h>
89fa9e4066Sahrens #include <sys/spa.h>
90fa9e4066Sahrens #include <sys/dmu.h>
91fa9e4066Sahrens #include <sys/txg.h>
922fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
93fa9e4066Sahrens #include <sys/zap.h>
94fa9e4066Sahrens #include <sys/dmu_objset.h>
95fa9e4066Sahrens #include <sys/poll.h>
96fa9e4066Sahrens #include <sys/stat.h>
97fa9e4066Sahrens #include <sys/time.h>
98fa9e4066Sahrens #include <sys/wait.h>
99fa9e4066Sahrens #include <sys/mman.h>
100fa9e4066Sahrens #include <sys/resource.h>
101fa9e4066Sahrens #include <sys/zio.h>
102fa9e4066Sahrens #include <sys/zil.h>
103b24ab676SJeff Bonwick #include <sys/zil_impl.h>
104fa9e4066Sahrens #include <sys/vdev_impl.h>
105e14bb325SJeff Bonwick #include <sys/vdev_file.h>
106*094e47e9SGeorge Wilson #include <sys/vdev_initialize.h>
107fa9e4066Sahrens #include <sys/spa_impl.h>
10888ecc943SGeorge Wilson #include <sys/metaslab_impl.h>
109fa9e4066Sahrens #include <sys/dsl_prop.h>
1104f5064b7SMark J Musante #include <sys/dsl_dataset.h>
1113b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
1123f9d6ad7SLin Ling #include <sys/dsl_scan.h>
113cde58dbcSMatthew Ahrens #include <sys/zio_checksum.h>
114fa9e4066Sahrens #include <sys/refcount.h>
115ad135b5dSChristopher Siden #include <sys/zfeature.h>
1163b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
117770499e1SDan Kimmel #include <sys/abd.h>
118fa9e4066Sahrens #include <stdio.h>
119004388ebScasper #include <stdio_ext.h>
120fa9e4066Sahrens #include <stdlib.h>
121fa9e4066Sahrens #include <unistd.h>
122fa9e4066Sahrens #include <signal.h>
123fa9e4066Sahrens #include <umem.h>
124fa9e4066Sahrens #include <dlfcn.h>
125fa9e4066Sahrens #include <ctype.h>
126fa9e4066Sahrens #include <math.h>
127fa9e4066Sahrens #include <sys/fs/zfs.h>
128b24ab676SJeff Bonwick #include <libnvpair.h>
1290a055120SJason King #include <libcmdutils.h>
130fa9e4066Sahrens 
131741652b0SEtienne Dechamps static int ztest_fd_data = -1;
132741652b0SEtienne Dechamps static int ztest_fd_rand = -1;
133420dfc95SChris Siden 
134420dfc95SChris Siden typedef struct ztest_shared_hdr {
135420dfc95SChris Siden 	uint64_t	zh_hdr_size;
136420dfc95SChris Siden 	uint64_t	zh_opts_size;
137420dfc95SChris Siden 	uint64_t	zh_size;
138420dfc95SChris Siden 	uint64_t	zh_stats_size;
139420dfc95SChris Siden 	uint64_t	zh_stats_count;
140420dfc95SChris Siden 	uint64_t	zh_ds_size;
141420dfc95SChris Siden 	uint64_t	zh_ds_count;
142420dfc95SChris Siden } ztest_shared_hdr_t;
143420dfc95SChris Siden 
144420dfc95SChris Siden static ztest_shared_hdr_t *ztest_shared_hdr;
145420dfc95SChris Siden 
146420dfc95SChris Siden typedef struct ztest_shared_opts {
1479adfa60dSMatthew Ahrens 	char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
1489adfa60dSMatthew Ahrens 	char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
149420dfc95SChris Siden 	char zo_alt_ztest[MAXNAMELEN];
150420dfc95SChris Siden 	char zo_alt_libpath[MAXNAMELEN];
151420dfc95SChris Siden 	uint64_t zo_vdevs;
152420dfc95SChris Siden 	uint64_t zo_vdevtime;
153420dfc95SChris Siden 	size_t zo_vdev_size;
154420dfc95SChris Siden 	int zo_ashift;
155420dfc95SChris Siden 	int zo_mirrors;
156420dfc95SChris Siden 	int zo_raidz;
157420dfc95SChris Siden 	int zo_raidz_parity;
158420dfc95SChris Siden 	int zo_datasets;
159420dfc95SChris Siden 	int zo_threads;
160420dfc95SChris Siden 	uint64_t zo_passtime;
161420dfc95SChris Siden 	uint64_t zo_killrate;
162420dfc95SChris Siden 	int zo_verbose;
163420dfc95SChris Siden 	int zo_init;
164420dfc95SChris Siden 	uint64_t zo_time;
165420dfc95SChris Siden 	uint64_t zo_maxloops;
166243952c7SMatt Ahrens 	uint64_t zo_metaslab_force_ganging;
167420dfc95SChris Siden } ztest_shared_opts_t;
168420dfc95SChris Siden 
169420dfc95SChris Siden static const ztest_shared_opts_t ztest_opts_defaults = {
170420dfc95SChris Siden 	.zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
171420dfc95SChris Siden 	.zo_dir = { '/', 't', 'm', 'p', '\0' },
172420dfc95SChris Siden 	.zo_alt_ztest = { '\0' },
173420dfc95SChris Siden 	.zo_alt_libpath = { '\0' },
174420dfc95SChris Siden 	.zo_vdevs = 5,
175420dfc95SChris Siden 	.zo_ashift = SPA_MINBLOCKSHIFT,
176420dfc95SChris Siden 	.zo_mirrors = 2,
177420dfc95SChris Siden 	.zo_raidz = 4,
178420dfc95SChris Siden 	.zo_raidz_parity = 1,
1798363e80aSGeorge Wilson 	.zo_vdev_size = SPA_MINDEVSIZE * 4,	/* 256m default size */
180420dfc95SChris Siden 	.zo_datasets = 7,
181420dfc95SChris Siden 	.zo_threads = 23,
182420dfc95SChris Siden 	.zo_passtime = 60,		/* 60 seconds */
183420dfc95SChris Siden 	.zo_killrate = 70,		/* 70% kill rate */
184420dfc95SChris Siden 	.zo_verbose = 0,
185420dfc95SChris Siden 	.zo_init = 1,
186420dfc95SChris Siden 	.zo_time = 300,			/* 5 minutes */
187420dfc95SChris Siden 	.zo_maxloops = 50,		/* max loops during spa_freeze() */
188243952c7SMatt Ahrens 	.zo_metaslab_force_ganging = 32 << 10
189420dfc95SChris Siden };
190420dfc95SChris Siden 
191243952c7SMatt Ahrens extern uint64_t metaslab_force_ganging;
192420dfc95SChris Siden extern uint64_t metaslab_df_alloc_threshold;
19369962b56SMatthew Ahrens extern uint64_t zfs_deadman_synctime_ms;
19430beaff4SGeorge Wilson extern int metaslab_preload_limit;
195dcbf3bd6SGeorge Wilson extern boolean_t zfs_compressed_arc_enabled;
196770499e1SDan Kimmel extern boolean_t zfs_abd_scatter_enabled;
19717f11284SSerapheim Dimitropoulos extern boolean_t zfs_force_some_double_word_sm_entries;
198420dfc95SChris Siden 
199420dfc95SChris Siden static ztest_shared_opts_t *ztest_shared_opts;
200420dfc95SChris Siden static ztest_shared_opts_t ztest_opts;
201420dfc95SChris Siden 
202420dfc95SChris Siden typedef struct ztest_shared_ds {
203420dfc95SChris Siden 	uint64_t	zd_seq;
204420dfc95SChris Siden } ztest_shared_ds_t;
205420dfc95SChris Siden 
206420dfc95SChris Siden static ztest_shared_ds_t *ztest_shared_ds;
207420dfc95SChris Siden #define	ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
208fa9e4066Sahrens 
209b24ab676SJeff Bonwick #define	BT_MAGIC	0x123456789abcdefULL
210420dfc95SChris Siden #define	MAXFAULTS() \
211420dfc95SChris Siden 	(MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
212b24ab676SJeff Bonwick 
213b24ab676SJeff Bonwick enum ztest_io_type {
214b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_TAG,
215b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_PATTERN,
216b24ab676SJeff Bonwick 	ZTEST_IO_WRITE_ZEROES,
217b24ab676SJeff Bonwick 	ZTEST_IO_TRUNCATE,
218b24ab676SJeff Bonwick 	ZTEST_IO_SETATTR,
21980901aeaSGeorge Wilson 	ZTEST_IO_REWRITE,
220b24ab676SJeff Bonwick 	ZTEST_IO_TYPES
221b24ab676SJeff Bonwick };
222b24ab676SJeff Bonwick 
223e05725b1Sbonwick typedef struct ztest_block_tag {
224b24ab676SJeff Bonwick 	uint64_t	bt_magic;
225e05725b1Sbonwick 	uint64_t	bt_objset;
226e05725b1Sbonwick 	uint64_t	bt_object;
227e05725b1Sbonwick 	uint64_t	bt_offset;
228b24ab676SJeff Bonwick 	uint64_t	bt_gen;
229e05725b1Sbonwick 	uint64_t	bt_txg;
230b24ab676SJeff Bonwick 	uint64_t	bt_crtxg;
231e05725b1Sbonwick } ztest_block_tag_t;
232e05725b1Sbonwick 
233b24ab676SJeff Bonwick typedef struct bufwad {
234b24ab676SJeff Bonwick 	uint64_t	bw_index;
235b24ab676SJeff Bonwick 	uint64_t	bw_txg;
236b24ab676SJeff Bonwick 	uint64_t	bw_data;
237b24ab676SJeff Bonwick } bufwad_t;
238b24ab676SJeff Bonwick 
239b24ab676SJeff Bonwick /*
240b24ab676SJeff Bonwick  * XXX -- fix zfs range locks to be generic so we can use them here.
241b24ab676SJeff Bonwick  */
242b24ab676SJeff Bonwick typedef enum {
243b24ab676SJeff Bonwick 	RL_READER,
244b24ab676SJeff Bonwick 	RL_WRITER,
245b24ab676SJeff Bonwick 	RL_APPEND
246b24ab676SJeff Bonwick } rl_type_t;
247b24ab676SJeff Bonwick 
248b24ab676SJeff Bonwick typedef struct rll {
249b24ab676SJeff Bonwick 	void		*rll_writer;
250b24ab676SJeff Bonwick 	int		rll_readers;
251f06dce2cSAndrew Stormont 	kmutex_t	rll_lock;
252f06dce2cSAndrew Stormont 	kcondvar_t	rll_cv;
253b24ab676SJeff Bonwick } rll_t;
254b24ab676SJeff Bonwick 
255b24ab676SJeff Bonwick typedef struct rl {
256b24ab676SJeff Bonwick 	uint64_t	rl_object;
257b24ab676SJeff Bonwick 	uint64_t	rl_offset;
258b24ab676SJeff Bonwick 	uint64_t	rl_size;
259b24ab676SJeff Bonwick 	rll_t		*rl_lock;
260b24ab676SJeff Bonwick } rl_t;
261b24ab676SJeff Bonwick 
262b24ab676SJeff Bonwick #define	ZTEST_RANGE_LOCKS	64
263b24ab676SJeff Bonwick #define	ZTEST_OBJECT_LOCKS	64
264b24ab676SJeff Bonwick 
265b24ab676SJeff Bonwick /*
266b24ab676SJeff Bonwick  * Object descriptor.  Used as a template for object lookup/create/remove.
267b24ab676SJeff Bonwick  */
268b24ab676SJeff Bonwick typedef struct ztest_od {
269b24ab676SJeff Bonwick 	uint64_t	od_dir;
270b24ab676SJeff Bonwick 	uint64_t	od_object;
271b24ab676SJeff Bonwick 	dmu_object_type_t od_type;
272b24ab676SJeff Bonwick 	dmu_object_type_t od_crtype;
273b24ab676SJeff Bonwick 	uint64_t	od_blocksize;
274b24ab676SJeff Bonwick 	uint64_t	od_crblocksize;
275b24ab676SJeff Bonwick 	uint64_t	od_gen;
276b24ab676SJeff Bonwick 	uint64_t	od_crgen;
2779adfa60dSMatthew Ahrens 	char		od_name[ZFS_MAX_DATASET_NAME_LEN];
278b24ab676SJeff Bonwick } ztest_od_t;
279fa9e4066Sahrens 
280b24ab676SJeff Bonwick /*
281b24ab676SJeff Bonwick  * Per-dataset state.
282b24ab676SJeff Bonwick  */
283b24ab676SJeff Bonwick typedef struct ztest_ds {
284420dfc95SChris Siden 	ztest_shared_ds_t *zd_shared;
285b24ab676SJeff Bonwick 	objset_t	*zd_os;
286f06dce2cSAndrew Stormont 	krwlock_t	zd_zilog_lock;
287b24ab676SJeff Bonwick 	zilog_t		*zd_zilog;
288b24ab676SJeff Bonwick 	ztest_od_t	*zd_od;		/* debugging aid */
2899adfa60dSMatthew Ahrens 	char		zd_name[ZFS_MAX_DATASET_NAME_LEN];
290f06dce2cSAndrew Stormont 	kmutex_t	zd_dirobj_lock;
291b24ab676SJeff Bonwick 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
292b24ab676SJeff Bonwick 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
293b24ab676SJeff Bonwick } ztest_ds_t;
294b24ab676SJeff Bonwick 
295b24ab676SJeff Bonwick /*
296b24ab676SJeff Bonwick  * Per-iteration state.
297b24ab676SJeff Bonwick  */
298b24ab676SJeff Bonwick typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
299b24ab676SJeff Bonwick 
300b24ab676SJeff Bonwick typedef struct ztest_info {
301b24ab676SJeff Bonwick 	ztest_func_t	*zi_func;	/* test function */
302b24ab676SJeff Bonwick 	uint64_t	zi_iters;	/* iterations per execution */
303b24ab676SJeff Bonwick 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
304b24ab676SJeff Bonwick } ztest_info_t;
305fa9e4066Sahrens 
306420dfc95SChris Siden typedef struct ztest_shared_callstate {
307420dfc95SChris Siden 	uint64_t	zc_count;	/* per-pass count */
308420dfc95SChris Siden 	uint64_t	zc_time;	/* per-pass time */
309420dfc95SChris Siden 	uint64_t	zc_next;	/* next time to call this function */
310420dfc95SChris Siden } ztest_shared_callstate_t;
311420dfc95SChris Siden 
312420dfc95SChris Siden static ztest_shared_callstate_t *ztest_shared_callstate;
313420dfc95SChris Siden #define	ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
314420dfc95SChris Siden 
315fa9e4066Sahrens /*
316fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
317fa9e4066Sahrens  */
318fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
319fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
320fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
321d20e665cSRicardo M. Correia ztest_func_t ztest_dmu_commit_callbacks;
322fa9e4066Sahrens ztest_func_t ztest_zap;
323fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
324b24ab676SJeff Bonwick ztest_func_t ztest_zil_commit;
325c9ba2a43SEric Schrock ztest_func_t ztest_zil_remount;
326b24ab676SJeff Bonwick ztest_func_t ztest_dmu_read_write_zcopy;
327fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
328b24ab676SJeff Bonwick ztest_func_t ztest_dmu_prealloc;
329b24ab676SJeff Bonwick ztest_func_t ztest_fzap;
330fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
331b24ab676SJeff Bonwick ztest_func_t ztest_dsl_prop_get_set;
332b24ab676SJeff Bonwick ztest_func_t ztest_spa_prop_get_set;
333fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
334fa9e4066Sahrens ztest_func_t ztest_fault_inject;
335b24ab676SJeff Bonwick ztest_func_t ztest_ddt_repair;
336b24ab676SJeff Bonwick ztest_func_t ztest_dmu_snapshot_hold;
337e14bb325SJeff Bonwick ztest_func_t ztest_spa_rename;
338b24ab676SJeff Bonwick ztest_func_t ztest_scrub;
339b24ab676SJeff Bonwick ztest_func_t ztest_dsl_dataset_promote_busy;
340fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
341fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
342fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
343e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
3441195e687SMark J Musante ztest_func_t ztest_split_pool;
345e9103aaeSGarrett D'Amore ztest_func_t ztest_reguid;
34625345e46SGeorge Wilson ztest_func_t ztest_spa_upgrade;
3475cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_device_removal;
3485cabbc6bSPrashanth Sreenivasa ztest_func_t ztest_remap_blocks;
34986714001SSerapheim Dimitropoulos ztest_func_t ztest_spa_checkpoint_create_discard;
350*094e47e9SGeorge Wilson ztest_func_t ztest_initialize;
351fa9e4066Sahrens 
352b24ab676SJeff Bonwick uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
353b24ab676SJeff Bonwick uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
354b24ab676SJeff Bonwick uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
355b24ab676SJeff Bonwick uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
356b24ab676SJeff Bonwick uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
357fa9e4066Sahrens 
358fa9e4066Sahrens ztest_info_t ztest_info[] = {
359e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
360b24ab676SJeff Bonwick 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
361e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
362b24ab676SJeff Bonwick 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
363e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
364e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
3651195e687SMark J Musante 	{ ztest_split_pool,			1,	&zopt_always	},
366b24ab676SJeff Bonwick 	{ ztest_zil_commit,			1,	&zopt_incessant	},
367c9ba2a43SEric Schrock 	{ ztest_zil_remount,			1,	&zopt_sometimes	},
368b24ab676SJeff Bonwick 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
369b24ab676SJeff Bonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
370b24ab676SJeff Bonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
371b24ab676SJeff Bonwick 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
372b24ab676SJeff Bonwick #if 0
373b24ab676SJeff Bonwick 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
374b24ab676SJeff Bonwick #endif
375b24ab676SJeff Bonwick 	{ ztest_fzap,				1,	&zopt_sometimes	},
376b24ab676SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
377b24ab676SJeff Bonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
378e05725b1Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
379b24ab676SJeff Bonwick 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
3805c987a37SChris Kirby 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
3812c1e2b44SGeorge Wilson 	{ ztest_reguid,				1,	&zopt_rarely	},
382e05725b1Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
383b24ab676SJeff Bonwick 	{ ztest_scrub,				1,	&zopt_rarely	},
38425345e46SGeorge Wilson 	{ ztest_spa_upgrade,			1,	&zopt_rarely	},
385b24ab676SJeff Bonwick 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
3863b2aab18SMatthew Ahrens 	{ ztest_vdev_attach_detach,		1,	&zopt_sometimes	},
387e14bb325SJeff Bonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
388420dfc95SChris Siden 	{ ztest_vdev_add_remove,		1,
389420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
390420dfc95SChris Siden 	{ ztest_vdev_aux_add_remove,		1,
391420dfc95SChris Siden 	    &ztest_opts.zo_vdevtime				},
3925cabbc6bSPrashanth Sreenivasa 	{ ztest_device_removal,			1,	&zopt_sometimes	},
39386714001SSerapheim Dimitropoulos 	{ ztest_remap_blocks,			1,	&zopt_sometimes },
394*094e47e9SGeorge Wilson 	{ ztest_spa_checkpoint_create_discard,	1,	&zopt_rarely	},
395*094e47e9SGeorge Wilson 	{ ztest_initialize,			1,	&zopt_sometimes }
396fa9e4066Sahrens };
397fa9e4066Sahrens 
398fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
399fa9e4066Sahrens 
400d20e665cSRicardo M. Correia /*
401d20e665cSRicardo M. Correia  * The following struct is used to hold a list of uncalled commit callbacks.
402d20e665cSRicardo M. Correia  * The callbacks are ordered by txg number.
403d20e665cSRicardo M. Correia  */
404d20e665cSRicardo M. Correia typedef struct ztest_cb_list {
405f06dce2cSAndrew Stormont 	kmutex_t zcl_callbacks_lock;
406d20e665cSRicardo M. Correia 	list_t	zcl_callbacks;
407d20e665cSRicardo M. Correia } ztest_cb_list_t;
408d20e665cSRicardo M. Correia 
409fa9e4066Sahrens /*
410fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
411fa9e4066Sahrens  */
412fa9e4066Sahrens typedef struct ztest_shared {
413420dfc95SChris Siden 	boolean_t	zs_do_init;
414b24ab676SJeff Bonwick 	hrtime_t	zs_proc_start;
415b24ab676SJeff Bonwick 	hrtime_t	zs_proc_stop;
416b24ab676SJeff Bonwick 	hrtime_t	zs_thread_start;
417b24ab676SJeff Bonwick 	hrtime_t	zs_thread_stop;
418b24ab676SJeff Bonwick 	hrtime_t	zs_thread_kill;
419b24ab676SJeff Bonwick 	uint64_t	zs_enospc_count;
42088ecc943SGeorge Wilson 	uint64_t	zs_vdev_next_leaf;
421e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
422fa9e4066Sahrens 	uint64_t	zs_alloc;
423fa9e4066Sahrens 	uint64_t	zs_space;
4241195e687SMark J Musante 	uint64_t	zs_splits;
4251195e687SMark J Musante 	uint64_t	zs_mirrors;
426420dfc95SChris Siden 	uint64_t	zs_metaslab_sz;
427420dfc95SChris Siden 	uint64_t	zs_metaslab_df_alloc_threshold;
428420dfc95SChris Siden 	uint64_t	zs_guid;
429fa9e4066Sahrens } ztest_shared_t;
430fa9e4066Sahrens 
431b24ab676SJeff Bonwick #define	ID_PARALLEL	-1ULL
432b24ab676SJeff Bonwick 
433fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
434e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
435b24ab676SJeff Bonwick ztest_shared_t *ztest_shared;
436fa9e4066Sahrens 
437420dfc95SChris Siden static spa_t *ztest_spa = NULL;
438420dfc95SChris Siden static ztest_ds_t *ztest_ds;
439420dfc95SChris Siden 
440f06dce2cSAndrew Stormont static kmutex_t ztest_vdev_lock;
44186714001SSerapheim Dimitropoulos static kmutex_t ztest_checkpoint_lock;
4423a4b1be9SMatthew Ahrens static boolean_t ztest_device_removal_active = B_FALSE;
443dfbb9432SGeorge Wilson 
444dfbb9432SGeorge Wilson /*
445dfbb9432SGeorge Wilson  * The ztest_name_lock protects the pool and dataset namespace used by
446dfbb9432SGeorge Wilson  * the individual tests. To modify the namespace, consumers must grab
447dfbb9432SGeorge Wilson  * this lock as writer. Grabbing the lock as reader will ensure that the
448dfbb9432SGeorge Wilson  * namespace does not change while the lock is held.
449dfbb9432SGeorge Wilson  */
450f06dce2cSAndrew Stormont static krwlock_t ztest_name_lock;
451fa9e4066Sahrens 
452420dfc95SChris Siden static boolean_t ztest_dump_core = B_TRUE;
453e14bb325SJeff Bonwick static boolean_t ztest_exiting;
4540a4e9518Sgw 
455d20e665cSRicardo M. Correia /* Global commit callback list */
456d20e665cSRicardo M. Correia static ztest_cb_list_t zcl;
457d20e665cSRicardo M. Correia 
458b24ab676SJeff Bonwick enum ztest_object {
459b24ab676SJeff Bonwick 	ZTEST_META_DNODE = 0,
460b24ab676SJeff Bonwick 	ZTEST_DIROBJ,
461b24ab676SJeff Bonwick 	ZTEST_OBJECTS
462b24ab676SJeff Bonwick };
463fa9e4066Sahrens 
4641ce825d8Sraf static void usage(boolean_t) __NORETURN;
465f1b4288bSvb 
466fa9e4066Sahrens /*
467fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
468fa9e4066Sahrens  * debugging facilities.
469fa9e4066Sahrens  */
470fa9e4066Sahrens const char *
471fa9e4066Sahrens _umem_debug_init()
472fa9e4066Sahrens {
473fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
474fa9e4066Sahrens }
475fa9e4066Sahrens 
476fa9e4066Sahrens const char *
477fa9e4066Sahrens _umem_logging_init(void)
478fa9e4066Sahrens {
479fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
480fa9e4066Sahrens }
481fa9e4066Sahrens 
482fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
483fa9e4066Sahrens 
484fa9e4066Sahrens char *fatal_msg;
485fa9e4066Sahrens 
486fa9e4066Sahrens static void
487fa9e4066Sahrens fatal(int do_perror, char *message, ...)
488fa9e4066Sahrens {
489fa9e4066Sahrens 	va_list args;
490fa9e4066Sahrens 	int save_errno = errno;
491fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
492fa9e4066Sahrens 
493fa9e4066Sahrens 	(void) fflush(stdout);
494fa9e4066Sahrens 
495fa9e4066Sahrens 	va_start(args, message);
496fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
497fa9e4066Sahrens 	/* LINTED */
498fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
499fa9e4066Sahrens 	va_end(args);
500fa9e4066Sahrens 	if (do_perror) {
501fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
502fa9e4066Sahrens 		    ": %s", strerror(save_errno));
503fa9e4066Sahrens 	}
504fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
505fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
506fa9e4066Sahrens 	if (ztest_dump_core)
507fa9e4066Sahrens 		abort();
508fa9e4066Sahrens 	exit(3);
509fa9e4066Sahrens }
510fa9e4066Sahrens 
511fa9e4066Sahrens static int
512fa9e4066Sahrens str2shift(const char *buf)
513fa9e4066Sahrens {
514fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
515fa9e4066Sahrens 	int i;
516fa9e4066Sahrens 
517fa9e4066Sahrens 	if (buf[0] == '\0')
518fa9e4066Sahrens 		return (0);
519fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
520fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
521fa9e4066Sahrens 			break;
522fa9e4066Sahrens 	}
523f1b4288bSvb 	if (i == strlen(ends)) {
524f1b4288bSvb 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
525f1b4288bSvb 		    buf);
526f1b4288bSvb 		usage(B_FALSE);
527f1b4288bSvb 	}
528fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
529fa9e4066Sahrens 		return (10*i);
530fa9e4066Sahrens 	}
531f1b4288bSvb 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
532f1b4288bSvb 	usage(B_FALSE);
533f1b4288bSvb 	/* NOTREACHED */
534fa9e4066Sahrens }
535fa9e4066Sahrens 
536fa9e4066Sahrens static uint64_t
537fa9e4066Sahrens nicenumtoull(const char *buf)
538fa9e4066Sahrens {
539fa9e4066Sahrens 	char *end;
540fa9e4066Sahrens 	uint64_t val;
541fa9e4066Sahrens 
542fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
543fa9e4066Sahrens 	if (end == buf) {
544f1b4288bSvb 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
545f1b4288bSvb 		usage(B_FALSE);
546fa9e4066Sahrens 	} else if (end[0] == '.') {
547fa9e4066Sahrens 		double fval = strtod(buf, &end);
548fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
549f1b4288bSvb 		if (fval > UINT64_MAX) {
550f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
551f1b4288bSvb 			    buf);
552f1b4288bSvb 			usage(B_FALSE);
553f1b4288bSvb 		}
554fa9e4066Sahrens 		val = (uint64_t)fval;
555fa9e4066Sahrens 	} else {
556fa9e4066Sahrens 		int shift = str2shift(end);
557f1b4288bSvb 		if (shift >= 64 || (val << shift) >> shift != val) {
558f1b4288bSvb 			(void) fprintf(stderr, "ztest: value too large: %s\n",
559f1b4288bSvb 			    buf);
560f1b4288bSvb 			usage(B_FALSE);
561f1b4288bSvb 		}
562fa9e4066Sahrens 		val <<= shift;
563fa9e4066Sahrens 	}
564fa9e4066Sahrens 	return (val);
565fa9e4066Sahrens }
566fa9e4066Sahrens 
567fa9e4066Sahrens static void
568f1b4288bSvb usage(boolean_t requested)
569fa9e4066Sahrens {
570420dfc95SChris Siden 	const ztest_shared_opts_t *zo = &ztest_opts_defaults;
571420dfc95SChris Siden 
5720a055120SJason King 	char nice_vdev_size[NN_NUMBUF_SZ];
573243952c7SMatt Ahrens 	char nice_force_ganging[NN_NUMBUF_SZ];
574f1b4288bSvb 	FILE *fp = requested ? stdout : stderr;
575fa9e4066Sahrens 
5760a055120SJason King 	nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
577243952c7SMatt Ahrens 	nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
578243952c7SMatt Ahrens 	    sizeof (nice_force_ganging));
579fa9e4066Sahrens 
580f1b4288bSvb 	(void) fprintf(fp, "Usage: %s\n"
581fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
582fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
5832215e990SMark J Musante 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
584fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
585fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
58699653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
587fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
588fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
589fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
5902215e990SMark J Musante 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
5912215e990SMark J Musante 	    "\t[-k kill_percentage (default: %llu%%)]\n"
592fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
5932215e990SMark J Musante 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
5942215e990SMark J Musante 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
5952215e990SMark J Musante 	    "\t[-E] use existing pool instead of creating new one\n"
5962215e990SMark J Musante 	    "\t[-T time (default: %llu sec)] total run time\n"
5972215e990SMark J Musante 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
5982215e990SMark J Musante 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
599420dfc95SChris Siden 	    "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
6000e60744cSPavel Zakharov 	    "\t[-o variable=value] ... set global variable to an unsigned\n"
6010e60744cSPavel Zakharov 	    "\t    32-bit integer value\n"
602f1b4288bSvb 	    "\t[-h] (print help)\n"
603fa9e4066Sahrens 	    "",
604420dfc95SChris Siden 	    zo->zo_pool,
605420dfc95SChris Siden 	    (u_longlong_t)zo->zo_vdevs,			/* -v */
6060a4e9518Sgw 	    nice_vdev_size,				/* -s */
607420dfc95SChris Siden 	    zo->zo_ashift,				/* -a */
608420dfc95SChris Siden 	    zo->zo_mirrors,				/* -m */
609420dfc95SChris Siden 	    zo->zo_raidz,				/* -r */
610420dfc95SChris Siden 	    zo->zo_raidz_parity,			/* -R */
611420dfc95SChris Siden 	    zo->zo_datasets,				/* -d */
612420dfc95SChris Siden 	    zo->zo_threads,				/* -t */
613243952c7SMatt Ahrens 	    nice_force_ganging,				/* -g */
614420dfc95SChris Siden 	    zo->zo_init,				/* -i */
615420dfc95SChris Siden 	    (u_longlong_t)zo->zo_killrate,		/* -k */
616420dfc95SChris Siden 	    zo->zo_pool,				/* -p */
617420dfc95SChris Siden 	    zo->zo_dir,					/* -f */
618420dfc95SChris Siden 	    (u_longlong_t)zo->zo_time,			/* -T */
619420dfc95SChris Siden 	    (u_longlong_t)zo->zo_maxloops,		/* -F */
620420dfc95SChris Siden 	    (u_longlong_t)zo->zo_passtime);
621f1b4288bSvb 	exit(requested ? 0 : 1);
622fa9e4066Sahrens }
623fa9e4066Sahrens 
624fa9e4066Sahrens static void
625fa9e4066Sahrens process_options(int argc, char **argv)
626fa9e4066Sahrens {
627420dfc95SChris Siden 	char *path;
628420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
629420dfc95SChris Siden 
630fa9e4066Sahrens 	int opt;
631fa9e4066Sahrens 	uint64_t value;
632420dfc95SChris Siden 	char altdir[MAXNAMELEN] = { 0 };
633fa9e4066Sahrens 
634420dfc95SChris Siden 	bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
635fa9e4066Sahrens 
636fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
6370e60744cSPavel Zakharov 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
638fa9e4066Sahrens 		value = 0;
639fa9e4066Sahrens 		switch (opt) {
6403d7072f8Seschrock 		case 'v':
6413d7072f8Seschrock 		case 's':
6423d7072f8Seschrock 		case 'a':
6433d7072f8Seschrock 		case 'm':
6443d7072f8Seschrock 		case 'r':
6453d7072f8Seschrock 		case 'R':
6463d7072f8Seschrock 		case 'd':
6473d7072f8Seschrock 		case 't':
6483d7072f8Seschrock 		case 'g':
6493d7072f8Seschrock 		case 'i':
6503d7072f8Seschrock 		case 'k':
6513d7072f8Seschrock 		case 'T':
6523d7072f8Seschrock 		case 'P':
6532215e990SMark J Musante 		case 'F':
654fa9e4066Sahrens 			value = nicenumtoull(optarg);
655fa9e4066Sahrens 		}
656fa9e4066Sahrens 		switch (opt) {
6573d7072f8Seschrock 		case 'v':
658420dfc95SChris Siden 			zo->zo_vdevs = value;
659fa9e4066Sahrens 			break;
6603d7072f8Seschrock 		case 's':
661420dfc95SChris Siden 			zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
662fa9e4066Sahrens 			break;
6633d7072f8Seschrock 		case 'a':
664420dfc95SChris Siden 			zo->zo_ashift = value;
665ecc2d604Sbonwick 			break;
6663d7072f8Seschrock 		case 'm':
667420dfc95SChris Siden 			zo->zo_mirrors = value;
668fa9e4066Sahrens 			break;
6693d7072f8Seschrock 		case 'r':
670420dfc95SChris Siden 			zo->zo_raidz = MAX(1, value);
671fa9e4066Sahrens 			break;
6723d7072f8Seschrock 		case 'R':
673420dfc95SChris Siden 			zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
67499653d4eSeschrock 			break;
6753d7072f8Seschrock 		case 'd':
676420dfc95SChris Siden 			zo->zo_datasets = MAX(1, value);
677fa9e4066Sahrens 			break;
6783d7072f8Seschrock 		case 't':
679420dfc95SChris Siden 			zo->zo_threads = MAX(1, value);
680fa9e4066Sahrens 			break;
6813d7072f8Seschrock 		case 'g':
682243952c7SMatt Ahrens 			zo->zo_metaslab_force_ganging =
683243952c7SMatt Ahrens 			    MAX(SPA_MINBLOCKSIZE << 1, value);
684fa9e4066Sahrens 			break;
6853d7072f8Seschrock 		case 'i':
686420dfc95SChris Siden 			zo->zo_init = value;
687fa9e4066Sahrens 			break;
6883d7072f8Seschrock 		case 'k':
689420dfc95SChris Siden 			zo->zo_killrate = value;
690fa9e4066Sahrens 			break;
6913d7072f8Seschrock 		case 'p':
692420dfc95SChris Siden 			(void) strlcpy(zo->zo_pool, optarg,
693420dfc95SChris Siden 			    sizeof (zo->zo_pool));
694fa9e4066Sahrens 			break;
6953d7072f8Seschrock 		case 'f':
696420dfc95SChris Siden 			path = realpath(optarg, NULL);
697420dfc95SChris Siden 			if (path == NULL) {
698420dfc95SChris Siden 				(void) fprintf(stderr, "error: %s: %s\n",
699420dfc95SChris Siden 				    optarg, strerror(errno));
700420dfc95SChris Siden 				usage(B_FALSE);
701420dfc95SChris Siden 			} else {
702420dfc95SChris Siden 				(void) strlcpy(zo->zo_dir, path,
703420dfc95SChris Siden 				    sizeof (zo->zo_dir));
704420dfc95SChris Siden 			}
705fa9e4066Sahrens 			break;
7063d7072f8Seschrock 		case 'V':
707420dfc95SChris Siden 			zo->zo_verbose++;
708fa9e4066Sahrens 			break;
7093d7072f8Seschrock 		case 'E':
710420dfc95SChris Siden 			zo->zo_init = 0;
711fa9e4066Sahrens 			break;
7123d7072f8Seschrock 		case 'T':
713420dfc95SChris Siden 			zo->zo_time = value;
714fa9e4066Sahrens 			break;
7153d7072f8Seschrock 		case 'P':
716420dfc95SChris Siden 			zo->zo_passtime = MAX(1, value);
717fa9e4066Sahrens 			break;
7182215e990SMark J Musante 		case 'F':
719420dfc95SChris Siden 			zo->zo_maxloops = MAX(1, value);
720420dfc95SChris Siden 			break;
721420dfc95SChris Siden 		case 'B':
722420dfc95SChris Siden 			(void) strlcpy(altdir, optarg, sizeof (altdir));
7232215e990SMark J Musante 			break;
7240e60744cSPavel Zakharov 		case 'o':
7250e60744cSPavel Zakharov 			if (set_global_var(optarg) != 0)
7260e60744cSPavel Zakharov 				usage(B_FALSE);
7270e60744cSPavel Zakharov 			break;
7283d7072f8Seschrock 		case 'h':
729f1b4288bSvb 			usage(B_TRUE);
730f1b4288bSvb 			break;
7313d7072f8Seschrock 		case '?':
7323d7072f8Seschrock 		default:
733f1b4288bSvb 			usage(B_FALSE);
734fa9e4066Sahrens 			break;
735fa9e4066Sahrens 		}
736fa9e4066Sahrens 	}
737fa9e4066Sahrens 
738420dfc95SChris Siden 	zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
73999653d4eSeschrock 
740420dfc95SChris Siden 	zo->zo_vdevtime =
741420dfc95SChris Siden 	    (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
742b24ab676SJeff Bonwick 	    UINT64_MAX >> 2);
743420dfc95SChris Siden 
744420dfc95SChris Siden 	if (strlen(altdir) > 0) {
745741652b0SEtienne Dechamps 		char *cmd;
746741652b0SEtienne Dechamps 		char *realaltdir;
747420dfc95SChris Siden 		char *bin;
748420dfc95SChris Siden 		char *ztest;
749420dfc95SChris Siden 		char *isa;
750420dfc95SChris Siden 		int isalen;
751420dfc95SChris Siden 
752741652b0SEtienne Dechamps 		cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
753741652b0SEtienne Dechamps 		realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
754741652b0SEtienne Dechamps 
755741652b0SEtienne Dechamps 		VERIFY(NULL != realpath(getexecname(), cmd));
756420dfc95SChris Siden 		if (0 != access(altdir, F_OK)) {
757420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
758420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest path: %s",
759420dfc95SChris Siden 			    altdir);
760420dfc95SChris Siden 		}
761420dfc95SChris Siden 		VERIFY(NULL != realpath(altdir, realaltdir));
762420dfc95SChris Siden 
763420dfc95SChris Siden 		/*
764420dfc95SChris Siden 		 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
765420dfc95SChris Siden 		 * We want to extract <isa> to determine if we should use
766420dfc95SChris Siden 		 * 32 or 64 bit binaries.
767420dfc95SChris Siden 		 */
768420dfc95SChris Siden 		bin = strstr(cmd, "/usr/bin/");
769420dfc95SChris Siden 		ztest = strstr(bin, "/ztest");
770420dfc95SChris Siden 		isa = bin + 9;
771420dfc95SChris Siden 		isalen = ztest - isa;
772420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
773420dfc95SChris Siden 		    "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
774420dfc95SChris Siden 		(void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
775420dfc95SChris Siden 		    "%s/usr/lib/%.*s", realaltdir, isalen, isa);
776420dfc95SChris Siden 
777420dfc95SChris Siden 		if (0 != access(zo->zo_alt_ztest, X_OK)) {
778420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
779420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate ztest: %s",
780420dfc95SChris Siden 			    zo->zo_alt_ztest);
781420dfc95SChris Siden 		} else if (0 != access(zo->zo_alt_libpath, X_OK)) {
782420dfc95SChris Siden 			ztest_dump_core = B_FALSE;
783420dfc95SChris Siden 			fatal(B_TRUE, "invalid alternate lib directory %s",
784420dfc95SChris Siden 			    zo->zo_alt_libpath);
785420dfc95SChris Siden 		}
786741652b0SEtienne Dechamps 
787741652b0SEtienne Dechamps 		umem_free(cmd, MAXPATHLEN);
788741652b0SEtienne Dechamps 		umem_free(realaltdir, MAXPATHLEN);
789420dfc95SChris Siden 	}
790fa9e4066Sahrens }
791fa9e4066Sahrens 
792b24ab676SJeff Bonwick static void
793b24ab676SJeff Bonwick ztest_kill(ztest_shared_t *zs)
794b24ab676SJeff Bonwick {
795420dfc95SChris Siden 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
796420dfc95SChris Siden 	zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
797b4952e17SGeorge Wilson 
798b4952e17SGeorge Wilson 	/*
799b4952e17SGeorge Wilson 	 * Before we kill off ztest, make sure that the config is updated.
8005cabbc6bSPrashanth Sreenivasa 	 * See comment above spa_write_cachefile().
801b4952e17SGeorge Wilson 	 */
802b4952e17SGeorge Wilson 	mutex_enter(&spa_namespace_lock);
8035cabbc6bSPrashanth Sreenivasa 	spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
804b4952e17SGeorge Wilson 	mutex_exit(&spa_namespace_lock);
805b4952e17SGeorge Wilson 
806b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
807b24ab676SJeff Bonwick 	(void) kill(getpid(), SIGKILL);
808b24ab676SJeff Bonwick }
809b24ab676SJeff Bonwick 
810b24ab676SJeff Bonwick static uint64_t
811b24ab676SJeff Bonwick ztest_random(uint64_t range)
812b24ab676SJeff Bonwick {
813b24ab676SJeff Bonwick 	uint64_t r;
814b24ab676SJeff Bonwick 
815741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
816741652b0SEtienne Dechamps 
817b24ab676SJeff Bonwick 	if (range == 0)
818b24ab676SJeff Bonwick 		return (0);
819b24ab676SJeff Bonwick 
820741652b0SEtienne Dechamps 	if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
821b24ab676SJeff Bonwick 		fatal(1, "short read from /dev/urandom");
822b24ab676SJeff Bonwick 
823b24ab676SJeff Bonwick 	return (r % range);
824b24ab676SJeff Bonwick }
825b24ab676SJeff Bonwick 
826b24ab676SJeff Bonwick /* ARGSUSED */
827b24ab676SJeff Bonwick static void
828b24ab676SJeff Bonwick ztest_record_enospc(const char *s)
829b24ab676SJeff Bonwick {
830b24ab676SJeff Bonwick 	ztest_shared->zs_enospc_count++;
831b24ab676SJeff Bonwick }
832b24ab676SJeff Bonwick 
833ecc2d604Sbonwick static uint64_t
834ecc2d604Sbonwick ztest_get_ashift(void)
835ecc2d604Sbonwick {
836420dfc95SChris Siden 	if (ztest_opts.zo_ashift == 0)
8372a104a52SAlex Reece 		return (SPA_MINBLOCKSHIFT + ztest_random(5));
838420dfc95SChris Siden 	return (ztest_opts.zo_ashift);
839ecc2d604Sbonwick }
840ecc2d604Sbonwick 
841fa9e4066Sahrens static nvlist_t *
84225345e46SGeorge Wilson make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
843fa9e4066Sahrens {
844e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
845fa9e4066Sahrens 	uint64_t vdev;
846fa9e4066Sahrens 	nvlist_t *file;
847fa9e4066Sahrens 
848e14bb325SJeff Bonwick 	if (ashift == 0)
849e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
850e14bb325SJeff Bonwick 
851e14bb325SJeff Bonwick 	if (path == NULL) {
852e14bb325SJeff Bonwick 		path = pathbuf;
853fa9e4066Sahrens 
854e14bb325SJeff Bonwick 		if (aux != NULL) {
855e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
856420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
857420dfc95SChris Siden 			    ztest_aux_template, ztest_opts.zo_dir,
85825345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool,
85925345e46SGeorge Wilson 			    aux, vdev);
860e14bb325SJeff Bonwick 		} else {
86188ecc943SGeorge Wilson 			vdev = ztest_shared->zs_vdev_next_leaf++;
862420dfc95SChris Siden 			(void) snprintf(path, sizeof (pathbuf),
863420dfc95SChris Siden 			    ztest_dev_template, ztest_opts.zo_dir,
86425345e46SGeorge Wilson 			    pool == NULL ? ztest_opts.zo_pool : pool, vdev);
865e14bb325SJeff Bonwick 		}
866e14bb325SJeff Bonwick 	}
867e14bb325SJeff Bonwick 
868e14bb325SJeff Bonwick 	if (size != 0) {
869e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
870fa9e4066Sahrens 		if (fd == -1)
871e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
872fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
873e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
874fa9e4066Sahrens 		(void) close(fd);
875fa9e4066Sahrens 	}
876fa9e4066Sahrens 
877fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
878fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
879e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
880ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
881fa9e4066Sahrens 
882fa9e4066Sahrens 	return (file);
883fa9e4066Sahrens }
884fa9e4066Sahrens 
885fa9e4066Sahrens static nvlist_t *
88625345e46SGeorge Wilson make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
88725345e46SGeorge Wilson     uint64_t ashift, int r)
888fa9e4066Sahrens {
889fa9e4066Sahrens 	nvlist_t *raidz, **child;
890fa9e4066Sahrens 	int c;
891fa9e4066Sahrens 
892fa9e4066Sahrens 	if (r < 2)
89325345e46SGeorge Wilson 		return (make_vdev_file(path, aux, pool, size, ashift));
894fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
895fa9e4066Sahrens 
896fa9e4066Sahrens 	for (c = 0; c < r; c++)
89725345e46SGeorge Wilson 		child[c] = make_vdev_file(path, aux, pool, size, ashift);
898fa9e4066Sahrens 
899fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
900fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
901fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
90299653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
903420dfc95SChris Siden 	    ztest_opts.zo_raidz_parity) == 0);
904fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
905fa9e4066Sahrens 	    child, r) == 0);
906fa9e4066Sahrens 
907fa9e4066Sahrens 	for (c = 0; c < r; c++)
908fa9e4066Sahrens 		nvlist_free(child[c]);
909fa9e4066Sahrens 
910fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
911fa9e4066Sahrens 
912fa9e4066Sahrens 	return (raidz);
913fa9e4066Sahrens }
914fa9e4066Sahrens 
915fa9e4066Sahrens static nvlist_t *
91625345e46SGeorge Wilson make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
91725345e46SGeorge Wilson     uint64_t ashift, int r, int m)
918fa9e4066Sahrens {
919fa9e4066Sahrens 	nvlist_t *mirror, **child;
920fa9e4066Sahrens 	int c;
921fa9e4066Sahrens 
922fa9e4066Sahrens 	if (m < 1)
92325345e46SGeorge Wilson 		return (make_vdev_raidz(path, aux, pool, size, ashift, r));
924fa9e4066Sahrens 
925fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
926fa9e4066Sahrens 
927fa9e4066Sahrens 	for (c = 0; c < m; c++)
92825345e46SGeorge Wilson 		child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
929fa9e4066Sahrens 
930fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
931fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
932fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
933fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
934fa9e4066Sahrens 	    child, m) == 0);
935fa9e4066Sahrens 
936fa9e4066Sahrens 	for (c = 0; c < m; c++)
937fa9e4066Sahrens 		nvlist_free(child[c]);
938fa9e4066Sahrens 
939fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
940fa9e4066Sahrens 
941fa9e4066Sahrens 	return (mirror);
942fa9e4066Sahrens }
943fa9e4066Sahrens 
944fa9e4066Sahrens static nvlist_t *
94525345e46SGeorge Wilson make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
94625345e46SGeorge Wilson     int log, int r, int m, int t)
947fa9e4066Sahrens {
948fa9e4066Sahrens 	nvlist_t *root, **child;
949fa9e4066Sahrens 	int c;
950fa9e4066Sahrens 
951fa9e4066Sahrens 	ASSERT(t > 0);
952fa9e4066Sahrens 
953fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
954fa9e4066Sahrens 
95531157203SJeff Bonwick 	for (c = 0; c < t; c++) {
95625345e46SGeorge Wilson 		child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
95725345e46SGeorge Wilson 		    r, m);
95831157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
95931157203SJeff Bonwick 		    log) == 0);
96031157203SJeff Bonwick 	}
961fa9e4066Sahrens 
962fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
963fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
964e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
965fa9e4066Sahrens 	    child, t) == 0);
966fa9e4066Sahrens 
967fa9e4066Sahrens 	for (c = 0; c < t; c++)
968fa9e4066Sahrens 		nvlist_free(child[c]);
969fa9e4066Sahrens 
970fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
971fa9e4066Sahrens 
972fa9e4066Sahrens 	return (root);
973fa9e4066Sahrens }
974fa9e4066Sahrens 
97525345e46SGeorge Wilson /*
97625345e46SGeorge Wilson  * Find a random spa version. Returns back a random spa version in the
97725345e46SGeorge Wilson  * range [initial_version, SPA_VERSION_FEATURES].
97825345e46SGeorge Wilson  */
97925345e46SGeorge Wilson static uint64_t
98025345e46SGeorge Wilson ztest_random_spa_version(uint64_t initial_version)
98125345e46SGeorge Wilson {
98225345e46SGeorge Wilson 	uint64_t version = initial_version;
98325345e46SGeorge Wilson 
98425345e46SGeorge Wilson 	if (version <= SPA_VERSION_BEFORE_FEATURES) {
98525345e46SGeorge Wilson 		version = version +
98625345e46SGeorge Wilson 		    ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
98725345e46SGeorge Wilson 	}
98825345e46SGeorge Wilson 
98925345e46SGeorge Wilson 	if (version > SPA_VERSION_BEFORE_FEATURES)
99025345e46SGeorge Wilson 		version = SPA_VERSION_FEATURES;
99125345e46SGeorge Wilson 
99225345e46SGeorge Wilson 	ASSERT(SPA_VERSION_IS_SUPPORTED(version));
99325345e46SGeorge Wilson 	return (version);
99425345e46SGeorge Wilson }
99525345e46SGeorge Wilson 
996b24ab676SJeff Bonwick static int
997b24ab676SJeff Bonwick ztest_random_blocksize(void)
998fa9e4066Sahrens {
999b5152584SMatthew Ahrens 	uint64_t block_shift;
1000b5152584SMatthew Ahrens 	/*
1001b5152584SMatthew Ahrens 	 * Choose a block size >= the ashift.
1002b5152584SMatthew Ahrens 	 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1003b5152584SMatthew Ahrens 	 */
1004b5152584SMatthew Ahrens 	int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1005b5152584SMatthew Ahrens 	if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1006b5152584SMatthew Ahrens 		maxbs = 20;
100781cd5c55SMatthew Ahrens 	block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
10082a104a52SAlex Reece 	return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1009b24ab676SJeff Bonwick }
1010fa9e4066Sahrens 
1011b24ab676SJeff Bonwick static int
1012b24ab676SJeff Bonwick ztest_random_ibshift(void)
1013b24ab676SJeff Bonwick {
1014b24ab676SJeff Bonwick 	return (DN_MIN_INDBLKSHIFT +
1015b24ab676SJeff Bonwick 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1016fa9e4066Sahrens }
1017fa9e4066Sahrens 
1018b24ab676SJeff Bonwick static uint64_t
1019b24ab676SJeff Bonwick ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1020fa9e4066Sahrens {
1021b24ab676SJeff Bonwick 	uint64_t top;
1022b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
1023b24ab676SJeff Bonwick 	vdev_t *tvd;
1024fa9e4066Sahrens 
1025b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1026fa9e4066Sahrens 
1027b24ab676SJeff Bonwick 	do {
1028b24ab676SJeff Bonwick 		top = ztest_random(rvd->vdev_children);
1029b24ab676SJeff Bonwick 		tvd = rvd->vdev_child[top];
10305cabbc6bSPrashanth Sreenivasa 	} while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1031b24ab676SJeff Bonwick 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1032fa9e4066Sahrens 
1033b24ab676SJeff Bonwick 	return (top);
1034fa9e4066Sahrens }
1035fa9e4066Sahrens 
1036b24ab676SJeff Bonwick static uint64_t
1037b24ab676SJeff Bonwick ztest_random_dsl_prop(zfs_prop_t prop)
1038fa9e4066Sahrens {
1039b24ab676SJeff Bonwick 	uint64_t value;
1040b24ab676SJeff Bonwick 
1041b24ab676SJeff Bonwick 	do {
1042b24ab676SJeff Bonwick 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1043b24ab676SJeff Bonwick 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1044b24ab676SJeff Bonwick 
1045b24ab676SJeff Bonwick 	return (value);
1046fa9e4066Sahrens }
1047fa9e4066Sahrens 
1048fa9e4066Sahrens static int
1049b24ab676SJeff Bonwick ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1050b24ab676SJeff Bonwick     boolean_t inherit)
1051fa9e4066Sahrens {
1052b24ab676SJeff Bonwick 	const char *propname = zfs_prop_to_name(prop);
1053b24ab676SJeff Bonwick 	const char *valname;
1054b24ab676SJeff Bonwick 	char setpoint[MAXPATHLEN];
1055b24ab676SJeff Bonwick 	uint64_t curval;
1056fa9e4066Sahrens 	int error;
1057fa9e4066Sahrens 
10583b2aab18SMatthew Ahrens 	error = dsl_prop_set_int(osname, propname,
10593b2aab18SMatthew Ahrens 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1060fa9e4066Sahrens 
1061b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1062b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1063fa9e4066Sahrens 		return (error);
1064fa9e4066Sahrens 	}
1065fb09f5aaSMadhav Suresh 	ASSERT0(error);
1066fa9e4066Sahrens 
10673b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1068b24ab676SJeff Bonwick 
1069420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
1070b24ab676SJeff Bonwick 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1071b24ab676SJeff Bonwick 		(void) printf("%s %s = %s at '%s'\n",
1072b24ab676SJeff Bonwick 		    osname, propname, valname, setpoint);
1073fa9e4066Sahrens 	}
1074fa9e4066Sahrens 
1075fa9e4066Sahrens 	return (error);
1076fa9e4066Sahrens }
1077fa9e4066Sahrens 
1078fa9e4066Sahrens static int
1079420dfc95SChris Siden ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1080fa9e4066Sahrens {
1081420dfc95SChris Siden 	spa_t *spa = ztest_spa;
1082b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
1083fa9e4066Sahrens 	int error;
1084fa9e4066Sahrens 
1085b24ab676SJeff Bonwick 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1086b24ab676SJeff Bonwick 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1087fa9e4066Sahrens 
1088b24ab676SJeff Bonwick 	error = spa_prop_set(spa, props);
1089b24ab676SJeff Bonwick 
1090b24ab676SJeff Bonwick 	nvlist_free(props);
1091b24ab676SJeff Bonwick 
1092b24ab676SJeff Bonwick 	if (error == ENOSPC) {
1093b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
1094fa9e4066Sahrens 		return (error);
1095fa9e4066Sahrens 	}
1096fb09f5aaSMadhav Suresh 	ASSERT0(error);
1097fa9e4066Sahrens 
1098fa9e4066Sahrens 	return (error);
1099fa9e4066Sahrens }
1100fa9e4066Sahrens 
1101b24ab676SJeff Bonwick static void
1102b24ab676SJeff Bonwick ztest_rll_init(rll_t *rll)
1103b24ab676SJeff Bonwick {
1104b24ab676SJeff Bonwick 	rll->rll_writer = NULL;
1105b24ab676SJeff Bonwick 	rll->rll_readers = 0;
1106f06dce2cSAndrew Stormont 	mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1107f06dce2cSAndrew Stormont 	cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1108b24ab676SJeff Bonwick }
1109fa9e4066Sahrens 
1110b24ab676SJeff Bonwick static void
1111b24ab676SJeff Bonwick ztest_rll_destroy(rll_t *rll)
1112fa9e4066Sahrens {
1113b24ab676SJeff Bonwick 	ASSERT(rll->rll_writer == NULL);
1114b24ab676SJeff Bonwick 	ASSERT(rll->rll_readers == 0);
1115f06dce2cSAndrew Stormont 	mutex_destroy(&rll->rll_lock);
1116f06dce2cSAndrew Stormont 	cv_destroy(&rll->rll_cv);
1117b24ab676SJeff Bonwick }
1118fa9e4066Sahrens 
1119b24ab676SJeff Bonwick static void
1120b24ab676SJeff Bonwick ztest_rll_lock(rll_t *rll, rl_type_t type)
1121b24ab676SJeff Bonwick {
1122f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1123fa9e4066Sahrens 
1124b24ab676SJeff Bonwick 	if (type == RL_READER) {
1125b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL)
1126f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1127b24ab676SJeff Bonwick 		rll->rll_readers++;
1128b24ab676SJeff Bonwick 	} else {
1129b24ab676SJeff Bonwick 		while (rll->rll_writer != NULL || rll->rll_readers)
1130f06dce2cSAndrew Stormont 			cv_wait(&rll->rll_cv, &rll->rll_lock);
1131b24ab676SJeff Bonwick 		rll->rll_writer = curthread;
1132b24ab676SJeff Bonwick 	}
1133fa9e4066Sahrens 
1134f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1135b24ab676SJeff Bonwick }
1136fa9e4066Sahrens 
1137b24ab676SJeff Bonwick static void
1138b24ab676SJeff Bonwick ztest_rll_unlock(rll_t *rll)
1139b24ab676SJeff Bonwick {
1140f06dce2cSAndrew Stormont 	mutex_enter(&rll->rll_lock);
1141fa9e4066Sahrens 
1142b24ab676SJeff Bonwick 	if (rll->rll_writer) {
1143b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers == 0);
1144b24ab676SJeff Bonwick 		rll->rll_writer = NULL;
1145b24ab676SJeff Bonwick 	} else {
1146b24ab676SJeff Bonwick 		ASSERT(rll->rll_readers != 0);
1147b24ab676SJeff Bonwick 		ASSERT(rll->rll_writer == NULL);
1148b24ab676SJeff Bonwick 		rll->rll_readers--;
1149b24ab676SJeff Bonwick 	}
1150fa9e4066Sahrens 
1151b24ab676SJeff Bonwick 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
1152f06dce2cSAndrew Stormont 		cv_broadcast(&rll->rll_cv);
1153b24ab676SJeff Bonwick 
1154f06dce2cSAndrew Stormont 	mutex_exit(&rll->rll_lock);
1155fa9e4066Sahrens }
1156fa9e4066Sahrens 
1157b24ab676SJeff Bonwick static void
1158b24ab676SJeff Bonwick ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
115931157203SJeff Bonwick {
1160b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
116131157203SJeff Bonwick 
1162b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
1163b24ab676SJeff Bonwick }
116431157203SJeff Bonwick 
1165b24ab676SJeff Bonwick static void
1166b24ab676SJeff Bonwick ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1167b24ab676SJeff Bonwick {
1168b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
116931157203SJeff Bonwick 
1170b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
117131157203SJeff Bonwick }
117231157203SJeff Bonwick 
1173b24ab676SJeff Bonwick static rl_t *
1174b24ab676SJeff Bonwick ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1175b24ab676SJeff Bonwick     uint64_t size, rl_type_t type)
117688ecc943SGeorge Wilson {
1177b24ab676SJeff Bonwick 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1178b24ab676SJeff Bonwick 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1179b24ab676SJeff Bonwick 	rl_t *rl;
118088ecc943SGeorge Wilson 
1181b24ab676SJeff Bonwick 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1182b24ab676SJeff Bonwick 	rl->rl_object = object;
1183b24ab676SJeff Bonwick 	rl->rl_offset = offset;
1184b24ab676SJeff Bonwick 	rl->rl_size = size;
1185b24ab676SJeff Bonwick 	rl->rl_lock = rll;
118688ecc943SGeorge Wilson 
1187b24ab676SJeff Bonwick 	ztest_rll_lock(rll, type);
118888ecc943SGeorge Wilson 
1189b24ab676SJeff Bonwick 	return (rl);
119088ecc943SGeorge Wilson }
119188ecc943SGeorge Wilson 
1192b24ab676SJeff Bonwick static void
1193b24ab676SJeff Bonwick ztest_range_unlock(rl_t *rl)
1194fa9e4066Sahrens {
1195b24ab676SJeff Bonwick 	rll_t *rll = rl->rl_lock;
1196fa9e4066Sahrens 
1197b24ab676SJeff Bonwick 	ztest_rll_unlock(rll);
1198fa9e4066Sahrens 
1199b24ab676SJeff Bonwick 	umem_free(rl, sizeof (*rl));
1200b24ab676SJeff Bonwick }
1201fa9e4066Sahrens 
1202b24ab676SJeff Bonwick static void
1203420dfc95SChris Siden ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1204b24ab676SJeff Bonwick {
1205b24ab676SJeff Bonwick 	zd->zd_os = os;
1206b24ab676SJeff Bonwick 	zd->zd_zilog = dmu_objset_zil(os);
1207420dfc95SChris Siden 	zd->zd_shared = szd;
1208b24ab676SJeff Bonwick 	dmu_objset_name(os, zd->zd_name);
1209fa9e4066Sahrens 
1210420dfc95SChris Siden 	if (zd->zd_shared != NULL)
1211420dfc95SChris Siden 		zd->zd_shared->zd_seq = 0;
1212420dfc95SChris Siden 
1213f06dce2cSAndrew Stormont 	rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1214f06dce2cSAndrew Stormont 	mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1215b24ab676SJeff Bonwick 
1216b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1217b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_object_lock[l]);
1218b24ab676SJeff Bonwick 
1219b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1220b24ab676SJeff Bonwick 		ztest_rll_init(&zd->zd_range_lock[l]);
1221b24ab676SJeff Bonwick }
1222b24ab676SJeff Bonwick 
1223b24ab676SJeff Bonwick static void
1224b24ab676SJeff Bonwick ztest_zd_fini(ztest_ds_t *zd)
1225b24ab676SJeff Bonwick {
1226f06dce2cSAndrew Stormont 	mutex_destroy(&zd->zd_dirobj_lock);
1227b24ab676SJeff Bonwick 
1228b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1229b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_object_lock[l]);
1230b24ab676SJeff Bonwick 
1231b24ab676SJeff Bonwick 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1232b24ab676SJeff Bonwick 		ztest_rll_destroy(&zd->zd_range_lock[l]);
1233b24ab676SJeff Bonwick }
1234b24ab676SJeff Bonwick 
1235b24ab676SJeff Bonwick #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1236b24ab676SJeff Bonwick 
1237b24ab676SJeff Bonwick static uint64_t
1238b24ab676SJeff Bonwick ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1239b24ab676SJeff Bonwick {
1240b24ab676SJeff Bonwick 	uint64_t txg;
1241b24ab676SJeff Bonwick 	int error;
1242b24ab676SJeff Bonwick 
1243b24ab676SJeff Bonwick 	/*
1244b24ab676SJeff Bonwick 	 * Attempt to assign tx to some transaction group.
1245b24ab676SJeff Bonwick 	 */
1246b24ab676SJeff Bonwick 	error = dmu_tx_assign(tx, txg_how);
1247b24ab676SJeff Bonwick 	if (error) {
1248b24ab676SJeff Bonwick 		if (error == ERESTART) {
1249b24ab676SJeff Bonwick 			ASSERT(txg_how == TXG_NOWAIT);
1250b24ab676SJeff Bonwick 			dmu_tx_wait(tx);
1251b24ab676SJeff Bonwick 		} else {
1252b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
1253b24ab676SJeff Bonwick 			ztest_record_enospc(tag);
1254b24ab676SJeff Bonwick 		}
1255b24ab676SJeff Bonwick 		dmu_tx_abort(tx);
1256b24ab676SJeff Bonwick 		return (0);
1257b24ab676SJeff Bonwick 	}
1258b24ab676SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
1259b24ab676SJeff Bonwick 	ASSERT(txg != 0);
1260b24ab676SJeff Bonwick 	return (txg);
1261b24ab676SJeff Bonwick }
1262b24ab676SJeff Bonwick 
1263b24ab676SJeff Bonwick static void
1264b24ab676SJeff Bonwick ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1265b24ab676SJeff Bonwick {
1266b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1267b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1268b24ab676SJeff Bonwick 
1269b24ab676SJeff Bonwick 	while (ip < ip_end)
1270b24ab676SJeff Bonwick 		*ip++ = value;
1271b24ab676SJeff Bonwick }
1272b24ab676SJeff Bonwick 
1273b24ab676SJeff Bonwick static boolean_t
1274b24ab676SJeff Bonwick ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1275b24ab676SJeff Bonwick {
1276b24ab676SJeff Bonwick 	uint64_t *ip = buf;
1277b24ab676SJeff Bonwick 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1278b24ab676SJeff Bonwick 	uint64_t diff = 0;
1279b24ab676SJeff Bonwick 
1280b24ab676SJeff Bonwick 	while (ip < ip_end)
1281b24ab676SJeff Bonwick 		diff |= (value - *ip++);
1282b24ab676SJeff Bonwick 
1283b24ab676SJeff Bonwick 	return (diff == 0);
1284b24ab676SJeff Bonwick }
1285b24ab676SJeff Bonwick 
1286b24ab676SJeff Bonwick static void
1287b24ab676SJeff Bonwick ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1288b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1289b24ab676SJeff Bonwick {
1290b24ab676SJeff Bonwick 	bt->bt_magic = BT_MAGIC;
1291b24ab676SJeff Bonwick 	bt->bt_objset = dmu_objset_id(os);
1292b24ab676SJeff Bonwick 	bt->bt_object = object;
1293b24ab676SJeff Bonwick 	bt->bt_offset = offset;
1294b24ab676SJeff Bonwick 	bt->bt_gen = gen;
1295b24ab676SJeff Bonwick 	bt->bt_txg = txg;
1296b24ab676SJeff Bonwick 	bt->bt_crtxg = crtxg;
1297b24ab676SJeff Bonwick }
1298b24ab676SJeff Bonwick 
1299b24ab676SJeff Bonwick static void
1300b24ab676SJeff Bonwick ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1301b24ab676SJeff Bonwick     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1302b24ab676SJeff Bonwick {
13035d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
13045d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
13055d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_object, ==, object);
13065d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_offset, ==, offset);
13075d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_gen, <=, gen);
13085d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_txg, <=, txg);
13095d7b4d43SMatthew Ahrens 	ASSERT3U(bt->bt_crtxg, ==, crtxg);
1310b24ab676SJeff Bonwick }
1311b24ab676SJeff Bonwick 
1312b24ab676SJeff Bonwick static ztest_block_tag_t *
1313b24ab676SJeff Bonwick ztest_bt_bonus(dmu_buf_t *db)
1314b24ab676SJeff Bonwick {
1315b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1316b24ab676SJeff Bonwick 	ztest_block_tag_t *bt;
1317b24ab676SJeff Bonwick 
1318b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1319b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1320b24ab676SJeff Bonwick 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1321b24ab676SJeff Bonwick 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1322b24ab676SJeff Bonwick 
1323b24ab676SJeff Bonwick 	return (bt);
1324b24ab676SJeff Bonwick }
1325b24ab676SJeff Bonwick 
1326b24ab676SJeff Bonwick /*
1327b24ab676SJeff Bonwick  * ZIL logging ops
1328b24ab676SJeff Bonwick  */
1329b24ab676SJeff Bonwick 
1330b24ab676SJeff Bonwick #define	lrz_type	lr_mode
1331b24ab676SJeff Bonwick #define	lrz_blocksize	lr_uid
1332b24ab676SJeff Bonwick #define	lrz_ibshift	lr_gid
1333b24ab676SJeff Bonwick #define	lrz_bonustype	lr_rdev
1334b24ab676SJeff Bonwick #define	lrz_bonuslen	lr_crtime[1]
1335b24ab676SJeff Bonwick 
13365002558fSNeil Perrin static void
1337b24ab676SJeff Bonwick ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1338b24ab676SJeff Bonwick {
1339b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1340b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1341b24ab676SJeff Bonwick 	itx_t *itx;
1342b24ab676SJeff Bonwick 
1343b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13445002558fSNeil Perrin 		return;
1345b24ab676SJeff Bonwick 
1346b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1347b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1348b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1349b24ab676SJeff Bonwick 
13505002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1351b24ab676SJeff Bonwick }
1352b24ab676SJeff Bonwick 
13535002558fSNeil Perrin static void
13545002558fSNeil Perrin ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1355b24ab676SJeff Bonwick {
1356b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1357b24ab676SJeff Bonwick 	size_t namesize = strlen(name) + 1;
1358b24ab676SJeff Bonwick 	itx_t *itx;
1359b24ab676SJeff Bonwick 
1360b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13615002558fSNeil Perrin 		return;
1362b24ab676SJeff Bonwick 
1363b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1364b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1365b24ab676SJeff Bonwick 	    sizeof (*lr) + namesize - sizeof (lr_t));
1366b24ab676SJeff Bonwick 
136751bd2f97SNeil Perrin 	itx->itx_oid = object;
13685002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1369b24ab676SJeff Bonwick }
1370b24ab676SJeff Bonwick 
13715002558fSNeil Perrin static void
1372b24ab676SJeff Bonwick ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1373b24ab676SJeff Bonwick {
1374b24ab676SJeff Bonwick 	itx_t *itx;
1375b24ab676SJeff Bonwick 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1376b24ab676SJeff Bonwick 
1377b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
13785002558fSNeil Perrin 		return;
1379b24ab676SJeff Bonwick 
1380b24ab676SJeff Bonwick 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1381b24ab676SJeff Bonwick 		write_state = WR_INDIRECT;
1382b24ab676SJeff Bonwick 
1383b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_WRITE,
1384b24ab676SJeff Bonwick 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1385b24ab676SJeff Bonwick 
1386b24ab676SJeff Bonwick 	if (write_state == WR_COPIED &&
1387b24ab676SJeff Bonwick 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1388b24ab676SJeff Bonwick 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1389b24ab676SJeff Bonwick 		zil_itx_destroy(itx);
1390b24ab676SJeff Bonwick 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1391b24ab676SJeff Bonwick 		write_state = WR_NEED_COPY;
1392b24ab676SJeff Bonwick 	}
1393b24ab676SJeff Bonwick 	itx->itx_private = zd;
1394b24ab676SJeff Bonwick 	itx->itx_wr_state = write_state;
1395b24ab676SJeff Bonwick 	itx->itx_sync = (ztest_random(8) == 0);
1396b24ab676SJeff Bonwick 
1397b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1398b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1399b24ab676SJeff Bonwick 
14005002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1401b24ab676SJeff Bonwick }
1402b24ab676SJeff Bonwick 
14035002558fSNeil Perrin static void
1404b24ab676SJeff Bonwick ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1405b24ab676SJeff Bonwick {
1406b24ab676SJeff Bonwick 	itx_t *itx;
1407b24ab676SJeff Bonwick 
1408b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
14095002558fSNeil Perrin 		return;
1410b24ab676SJeff Bonwick 
1411b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1412b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1413b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1414b24ab676SJeff Bonwick 
14155002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
14165002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1417b24ab676SJeff Bonwick }
1418b24ab676SJeff Bonwick 
14195002558fSNeil Perrin static void
1420b24ab676SJeff Bonwick ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1421b24ab676SJeff Bonwick {
1422b24ab676SJeff Bonwick 	itx_t *itx;
1423b24ab676SJeff Bonwick 
1424b24ab676SJeff Bonwick 	if (zil_replaying(zd->zd_zilog, tx))
14255002558fSNeil Perrin 		return;
1426b24ab676SJeff Bonwick 
1427b24ab676SJeff Bonwick 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1428b24ab676SJeff Bonwick 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1429b24ab676SJeff Bonwick 	    sizeof (*lr) - sizeof (lr_t));
1430b24ab676SJeff Bonwick 
14315002558fSNeil Perrin 	itx->itx_sync = B_FALSE;
14325002558fSNeil Perrin 	zil_itx_assign(zd->zd_zilog, itx, tx);
1433b24ab676SJeff Bonwick }
1434b24ab676SJeff Bonwick 
1435b24ab676SJeff Bonwick /*
1436b24ab676SJeff Bonwick  * ZIL replay ops
1437b24ab676SJeff Bonwick  */
1438b24ab676SJeff Bonwick static int
14393f7978d0SAlan Somers ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1440b24ab676SJeff Bonwick {
14413f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
14423f7978d0SAlan Somers 	lr_create_t *lr = arg2;
1443b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1444b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1445b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1446b24ab676SJeff Bonwick 	dmu_buf_t *db;
1447b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1448b24ab676SJeff Bonwick 	uint64_t txg;
1449b24ab676SJeff Bonwick 	int error = 0;
1450b24ab676SJeff Bonwick 
1451b24ab676SJeff Bonwick 	if (byteswap)
1452b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1453b24ab676SJeff Bonwick 
1454b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1455b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1456b24ab676SJeff Bonwick 
1457b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1458b24ab676SJeff Bonwick 
1459b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1460b24ab676SJeff Bonwick 
1461b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1462b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1463b24ab676SJeff Bonwick 	} else {
1464b24ab676SJeff Bonwick 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1465b24ab676SJeff Bonwick 	}
1466b24ab676SJeff Bonwick 
1467b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1468b24ab676SJeff Bonwick 	if (txg == 0)
1469b24ab676SJeff Bonwick 		return (ENOSPC);
1470b24ab676SJeff Bonwick 
1471b24ab676SJeff Bonwick 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1472b24ab676SJeff Bonwick 
1473b24ab676SJeff Bonwick 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1474b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1475b24ab676SJeff Bonwick 			lr->lr_foid = zap_create(os,
1476b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1477b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1478b24ab676SJeff Bonwick 		} else {
1479b24ab676SJeff Bonwick 			error = zap_create_claim(os, lr->lr_foid,
1480b24ab676SJeff Bonwick 			    lr->lrz_type, lr->lrz_bonustype,
1481b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1482b24ab676SJeff Bonwick 		}
1483b24ab676SJeff Bonwick 	} else {
1484b24ab676SJeff Bonwick 		if (lr->lr_foid == 0) {
1485b24ab676SJeff Bonwick 			lr->lr_foid = dmu_object_alloc(os,
1486b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1487b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1488b24ab676SJeff Bonwick 		} else {
1489b24ab676SJeff Bonwick 			error = dmu_object_claim(os, lr->lr_foid,
1490b24ab676SJeff Bonwick 			    lr->lrz_type, 0, lr->lrz_bonustype,
1491b24ab676SJeff Bonwick 			    lr->lrz_bonuslen, tx);
1492b24ab676SJeff Bonwick 		}
1493b24ab676SJeff Bonwick 	}
1494b24ab676SJeff Bonwick 
1495b24ab676SJeff Bonwick 	if (error) {
1496b24ab676SJeff Bonwick 		ASSERT3U(error, ==, EEXIST);
1497b24ab676SJeff Bonwick 		ASSERT(zd->zd_zilog->zl_replay);
1498b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
1499b24ab676SJeff Bonwick 		return (error);
1500b24ab676SJeff Bonwick 	}
1501b24ab676SJeff Bonwick 
1502b24ab676SJeff Bonwick 	ASSERT(lr->lr_foid != 0);
1503b24ab676SJeff Bonwick 
1504b24ab676SJeff Bonwick 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1505b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1506b24ab676SJeff Bonwick 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1507b24ab676SJeff Bonwick 
1508b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1509b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1510b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1511b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1512b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1513b24ab676SJeff Bonwick 
1514b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1515b24ab676SJeff Bonwick 	    &lr->lr_foid, tx));
1516b24ab676SJeff Bonwick 
1517b24ab676SJeff Bonwick 	(void) ztest_log_create(zd, tx, lr);
1518b24ab676SJeff Bonwick 
1519b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1520b24ab676SJeff Bonwick 
1521b24ab676SJeff Bonwick 	return (0);
1522b24ab676SJeff Bonwick }
1523b24ab676SJeff Bonwick 
1524b24ab676SJeff Bonwick static int
15253f7978d0SAlan Somers ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1526b24ab676SJeff Bonwick {
15273f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
15283f7978d0SAlan Somers 	lr_remove_t *lr = arg2;
1529b24ab676SJeff Bonwick 	char *name = (void *)(lr + 1);		/* name follows lr */
1530b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1531b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1532b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1533b24ab676SJeff Bonwick 	uint64_t object, txg;
1534b24ab676SJeff Bonwick 
1535b24ab676SJeff Bonwick 	if (byteswap)
1536b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1537b24ab676SJeff Bonwick 
1538b24ab676SJeff Bonwick 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1539b24ab676SJeff Bonwick 	ASSERT(name[0] != '\0');
1540b24ab676SJeff Bonwick 
1541b420f3adSRichard Lowe 	VERIFY3U(0, ==,
1542b24ab676SJeff Bonwick 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1543b24ab676SJeff Bonwick 	ASSERT(object != 0);
1544b24ab676SJeff Bonwick 
1545b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_WRITER);
1546b24ab676SJeff Bonwick 
1547b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1548b24ab676SJeff Bonwick 
1549b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1550b24ab676SJeff Bonwick 
1551b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1552b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1553b24ab676SJeff Bonwick 
1554b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1555b24ab676SJeff Bonwick 	if (txg == 0) {
1556b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1557b24ab676SJeff Bonwick 		return (ENOSPC);
1558b24ab676SJeff Bonwick 	}
1559b24ab676SJeff Bonwick 
1560b24ab676SJeff Bonwick 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1561b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1562b24ab676SJeff Bonwick 	} else {
1563b420f3adSRichard Lowe 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1564b24ab676SJeff Bonwick 	}
1565b24ab676SJeff Bonwick 
1566b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1567b24ab676SJeff Bonwick 
15685002558fSNeil Perrin 	(void) ztest_log_remove(zd, tx, lr, object);
1569b24ab676SJeff Bonwick 
1570b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1571b24ab676SJeff Bonwick 
1572b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1573b24ab676SJeff Bonwick 
1574b24ab676SJeff Bonwick 	return (0);
1575b24ab676SJeff Bonwick }
1576b24ab676SJeff Bonwick 
1577b24ab676SJeff Bonwick static int
15783f7978d0SAlan Somers ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1579b24ab676SJeff Bonwick {
15803f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
15813f7978d0SAlan Somers 	lr_write_t *lr = arg2;
1582b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1583b24ab676SJeff Bonwick 	void *data = lr + 1;			/* data follows lr */
1584b24ab676SJeff Bonwick 	uint64_t offset, length;
1585b24ab676SJeff Bonwick 	ztest_block_tag_t *bt = data;
1586b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1587b24ab676SJeff Bonwick 	uint64_t gen, txg, lrtxg, crtxg;
1588b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1589b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1590b24ab676SJeff Bonwick 	dmu_buf_t *db;
1591b24ab676SJeff Bonwick 	arc_buf_t *abuf = NULL;
1592b24ab676SJeff Bonwick 	rl_t *rl;
1593b24ab676SJeff Bonwick 
1594b24ab676SJeff Bonwick 	if (byteswap)
1595b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1596b24ab676SJeff Bonwick 
1597b24ab676SJeff Bonwick 	offset = lr->lr_offset;
1598b24ab676SJeff Bonwick 	length = lr->lr_length;
1599b24ab676SJeff Bonwick 
1600b24ab676SJeff Bonwick 	/* If it's a dmu_sync() block, write the whole block */
1601b24ab676SJeff Bonwick 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1602b24ab676SJeff Bonwick 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1603b24ab676SJeff Bonwick 		if (length < blocksize) {
1604b24ab676SJeff Bonwick 			offset -= offset % blocksize;
1605b24ab676SJeff Bonwick 			length = blocksize;
1606b24ab676SJeff Bonwick 		}
1607b24ab676SJeff Bonwick 	}
1608b24ab676SJeff Bonwick 
1609b24ab676SJeff Bonwick 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1610b24ab676SJeff Bonwick 		byteswap_uint64_array(bt, sizeof (*bt));
1611b24ab676SJeff Bonwick 
1612b24ab676SJeff Bonwick 	if (bt->bt_magic != BT_MAGIC)
1613b24ab676SJeff Bonwick 		bt = NULL;
1614b24ab676SJeff Bonwick 
1615b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1616b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1617b24ab676SJeff Bonwick 
1618b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1619b24ab676SJeff Bonwick 
1620b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1621b24ab676SJeff Bonwick 
1622b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1623b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1624b24ab676SJeff Bonwick 	gen = bbt->bt_gen;
1625b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1626b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1627b24ab676SJeff Bonwick 
1628b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1629b24ab676SJeff Bonwick 
1630b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1631b24ab676SJeff Bonwick 
1632b24ab676SJeff Bonwick 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1633b24ab676SJeff Bonwick 	    P2PHASE(offset, length) == 0)
1634b24ab676SJeff Bonwick 		abuf = dmu_request_arcbuf(db, length);
1635b24ab676SJeff Bonwick 
1636b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1637b24ab676SJeff Bonwick 	if (txg == 0) {
1638b24ab676SJeff Bonwick 		if (abuf != NULL)
1639b24ab676SJeff Bonwick 			dmu_return_arcbuf(abuf);
1640b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1641b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1642b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1643b24ab676SJeff Bonwick 		return (ENOSPC);
1644b24ab676SJeff Bonwick 	}
1645b24ab676SJeff Bonwick 
1646b24ab676SJeff Bonwick 	if (bt != NULL) {
1647b24ab676SJeff Bonwick 		/*
1648b24ab676SJeff Bonwick 		 * Usually, verify the old data before writing new data --
1649b24ab676SJeff Bonwick 		 * but not always, because we also want to verify correct
1650b24ab676SJeff Bonwick 		 * behavior when the data was not recently read into cache.
1651b24ab676SJeff Bonwick 		 */
1652b24ab676SJeff Bonwick 		ASSERT(offset % doi.doi_data_block_size == 0);
1653b24ab676SJeff Bonwick 		if (ztest_random(4) != 0) {
1654b24ab676SJeff Bonwick 			int prefetch = ztest_random(2) ?
1655b24ab676SJeff Bonwick 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1656b24ab676SJeff Bonwick 			ztest_block_tag_t rbt;
1657b24ab676SJeff Bonwick 
1658b24ab676SJeff Bonwick 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1659b24ab676SJeff Bonwick 			    sizeof (rbt), &rbt, prefetch) == 0);
1660b24ab676SJeff Bonwick 			if (rbt.bt_magic == BT_MAGIC) {
1661b24ab676SJeff Bonwick 				ztest_bt_verify(&rbt, os, lr->lr_foid,
1662b24ab676SJeff Bonwick 				    offset, gen, txg, crtxg);
1663b24ab676SJeff Bonwick 			}
1664b24ab676SJeff Bonwick 		}
1665b24ab676SJeff Bonwick 
1666b24ab676SJeff Bonwick 		/*
1667b24ab676SJeff Bonwick 		 * Writes can appear to be newer than the bonus buffer because
1668b24ab676SJeff Bonwick 		 * the ztest_get_data() callback does a dmu_read() of the
1669b24ab676SJeff Bonwick 		 * open-context data, which may be different than the data
1670b24ab676SJeff Bonwick 		 * as it was when the write was generated.
1671b24ab676SJeff Bonwick 		 */
1672b24ab676SJeff Bonwick 		if (zd->zd_zilog->zl_replay) {
1673b24ab676SJeff Bonwick 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
1674b24ab676SJeff Bonwick 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1675b24ab676SJeff Bonwick 			    bt->bt_crtxg);
1676b24ab676SJeff Bonwick 		}
1677b24ab676SJeff Bonwick 
1678b24ab676SJeff Bonwick 		/*
1679b24ab676SJeff Bonwick 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1680b24ab676SJeff Bonwick 		 * so that all of the usual ASSERTs will work.
1681b24ab676SJeff Bonwick 		 */
1682b24ab676SJeff Bonwick 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1683b24ab676SJeff Bonwick 	}
1684b24ab676SJeff Bonwick 
1685b24ab676SJeff Bonwick 	if (abuf == NULL) {
1686b24ab676SJeff Bonwick 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1687b24ab676SJeff Bonwick 	} else {
1688b24ab676SJeff Bonwick 		bcopy(data, abuf->b_data, length);
1689b24ab676SJeff Bonwick 		dmu_assign_arcbuf(db, offset, abuf, tx);
1690b24ab676SJeff Bonwick 	}
1691b24ab676SJeff Bonwick 
1692b24ab676SJeff Bonwick 	(void) ztest_log_write(zd, tx, lr);
1693b24ab676SJeff Bonwick 
1694b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1695b24ab676SJeff Bonwick 
1696b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1697b24ab676SJeff Bonwick 
1698b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1699b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1700b24ab676SJeff Bonwick 
1701b24ab676SJeff Bonwick 	return (0);
1702b24ab676SJeff Bonwick }
1703b24ab676SJeff Bonwick 
1704b24ab676SJeff Bonwick static int
17053f7978d0SAlan Somers ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1706b24ab676SJeff Bonwick {
17073f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17083f7978d0SAlan Somers 	lr_truncate_t *lr = arg2;
1709b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1710b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1711b24ab676SJeff Bonwick 	uint64_t txg;
1712b24ab676SJeff Bonwick 	rl_t *rl;
1713b24ab676SJeff Bonwick 
1714b24ab676SJeff Bonwick 	if (byteswap)
1715b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1716b24ab676SJeff Bonwick 
1717b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1718b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1719b24ab676SJeff Bonwick 	    RL_WRITER);
1720b24ab676SJeff Bonwick 
1721b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1722b24ab676SJeff Bonwick 
1723b24ab676SJeff Bonwick 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1724b24ab676SJeff Bonwick 
1725b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1726b24ab676SJeff Bonwick 	if (txg == 0) {
1727b24ab676SJeff Bonwick 		ztest_range_unlock(rl);
1728b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1729b24ab676SJeff Bonwick 		return (ENOSPC);
1730b24ab676SJeff Bonwick 	}
1731b24ab676SJeff Bonwick 
1732b24ab676SJeff Bonwick 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1733b24ab676SJeff Bonwick 	    lr->lr_length, tx) == 0);
1734b24ab676SJeff Bonwick 
1735b24ab676SJeff Bonwick 	(void) ztest_log_truncate(zd, tx, lr);
1736b24ab676SJeff Bonwick 
1737b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1738b24ab676SJeff Bonwick 
1739b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
1740b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1741b24ab676SJeff Bonwick 
1742b24ab676SJeff Bonwick 	return (0);
1743b24ab676SJeff Bonwick }
1744b24ab676SJeff Bonwick 
1745b24ab676SJeff Bonwick static int
17463f7978d0SAlan Somers ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1747b24ab676SJeff Bonwick {
17483f7978d0SAlan Somers 	ztest_ds_t *zd = arg1;
17493f7978d0SAlan Somers 	lr_setattr_t *lr = arg2;
1750b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1751b24ab676SJeff Bonwick 	dmu_tx_t *tx;
1752b24ab676SJeff Bonwick 	dmu_buf_t *db;
1753b24ab676SJeff Bonwick 	ztest_block_tag_t *bbt;
1754b24ab676SJeff Bonwick 	uint64_t txg, lrtxg, crtxg;
1755b24ab676SJeff Bonwick 
1756b24ab676SJeff Bonwick 	if (byteswap)
1757b24ab676SJeff Bonwick 		byteswap_uint64_array(lr, sizeof (*lr));
1758b24ab676SJeff Bonwick 
1759b24ab676SJeff Bonwick 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1760b24ab676SJeff Bonwick 
1761b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1762b24ab676SJeff Bonwick 
1763b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
1764b24ab676SJeff Bonwick 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1765b24ab676SJeff Bonwick 
1766b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1767b24ab676SJeff Bonwick 	if (txg == 0) {
1768b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1769b24ab676SJeff Bonwick 		ztest_object_unlock(zd, lr->lr_foid);
1770b24ab676SJeff Bonwick 		return (ENOSPC);
1771b24ab676SJeff Bonwick 	}
1772b24ab676SJeff Bonwick 
1773b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1774b24ab676SJeff Bonwick 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1775b24ab676SJeff Bonwick 	crtxg = bbt->bt_crtxg;
1776b24ab676SJeff Bonwick 	lrtxg = lr->lr_common.lrc_txg;
1777b24ab676SJeff Bonwick 
1778b24ab676SJeff Bonwick 	if (zd->zd_zilog->zl_replay) {
1779b24ab676SJeff Bonwick 		ASSERT(lr->lr_size != 0);
1780b24ab676SJeff Bonwick 		ASSERT(lr->lr_mode != 0);
1781b24ab676SJeff Bonwick 		ASSERT(lrtxg != 0);
1782b24ab676SJeff Bonwick 	} else {
1783b24ab676SJeff Bonwick 		/*
1784b24ab676SJeff Bonwick 		 * Randomly change the size and increment the generation.
1785b24ab676SJeff Bonwick 		 */
1786b24ab676SJeff Bonwick 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1787b24ab676SJeff Bonwick 		    sizeof (*bbt);
1788b24ab676SJeff Bonwick 		lr->lr_mode = bbt->bt_gen + 1;
1789b24ab676SJeff Bonwick 		ASSERT(lrtxg == 0);
1790b24ab676SJeff Bonwick 	}
1791b24ab676SJeff Bonwick 
1792b24ab676SJeff Bonwick 	/*
1793b24ab676SJeff Bonwick 	 * Verify that the current bonus buffer is not newer than our txg.
1794b24ab676SJeff Bonwick 	 */
1795b24ab676SJeff Bonwick 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1796b24ab676SJeff Bonwick 	    MAX(txg, lrtxg), crtxg);
1797b24ab676SJeff Bonwick 
1798b24ab676SJeff Bonwick 	dmu_buf_will_dirty(db, tx);
1799b24ab676SJeff Bonwick 
1800b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1801b24ab676SJeff Bonwick 	ASSERT3U(lr->lr_size, <=, db->db_size);
1802fb09f5aaSMadhav Suresh 	VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1803b24ab676SJeff Bonwick 	bbt = ztest_bt_bonus(db);
1804b24ab676SJeff Bonwick 
1805b24ab676SJeff Bonwick 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1806b24ab676SJeff Bonwick 
1807b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1808b24ab676SJeff Bonwick 
1809b24ab676SJeff Bonwick 	(void) ztest_log_setattr(zd, tx, lr);
1810b24ab676SJeff Bonwick 
1811b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
1812b24ab676SJeff Bonwick 
1813b24ab676SJeff Bonwick 	ztest_object_unlock(zd, lr->lr_foid);
1814b24ab676SJeff Bonwick 
1815b24ab676SJeff Bonwick 	return (0);
1816b24ab676SJeff Bonwick }
1817b24ab676SJeff Bonwick 
1818b24ab676SJeff Bonwick zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1819b24ab676SJeff Bonwick 	NULL,			/* 0 no such transaction type */
1820b24ab676SJeff Bonwick 	ztest_replay_create,	/* TX_CREATE */
1821b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR */
1822b24ab676SJeff Bonwick 	NULL,			/* TX_MKXATTR */
1823b24ab676SJeff Bonwick 	NULL,			/* TX_SYMLINK */
1824b24ab676SJeff Bonwick 	ztest_replay_remove,	/* TX_REMOVE */
1825b24ab676SJeff Bonwick 	NULL,			/* TX_RMDIR */
1826b24ab676SJeff Bonwick 	NULL,			/* TX_LINK */
1827b24ab676SJeff Bonwick 	NULL,			/* TX_RENAME */
1828b24ab676SJeff Bonwick 	ztest_replay_write,	/* TX_WRITE */
1829b24ab676SJeff Bonwick 	ztest_replay_truncate,	/* TX_TRUNCATE */
1830b24ab676SJeff Bonwick 	ztest_replay_setattr,	/* TX_SETATTR */
1831b24ab676SJeff Bonwick 	NULL,			/* TX_ACL */
1832b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL */
1833b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ATTR */
1834b24ab676SJeff Bonwick 	NULL,			/* TX_CREATE_ACL_ATTR */
1835b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL */
1836b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ATTR */
1837b24ab676SJeff Bonwick 	NULL,			/* TX_MKDIR_ACL_ATTR */
1838b24ab676SJeff Bonwick 	NULL,			/* TX_WRITE2 */
1839b24ab676SJeff Bonwick };
1840b24ab676SJeff Bonwick 
1841b24ab676SJeff Bonwick /*
1842b24ab676SJeff Bonwick  * ZIL get_data callbacks
1843b24ab676SJeff Bonwick  */
1844b24ab676SJeff Bonwick 
1845b24ab676SJeff Bonwick static void
1846b24ab676SJeff Bonwick ztest_get_done(zgd_t *zgd, int error)
1847b24ab676SJeff Bonwick {
1848b24ab676SJeff Bonwick 	ztest_ds_t *zd = zgd->zgd_private;
1849b24ab676SJeff Bonwick 	uint64_t object = zgd->zgd_rl->rl_object;
1850b24ab676SJeff Bonwick 
1851b24ab676SJeff Bonwick 	if (zgd->zgd_db)
1852b24ab676SJeff Bonwick 		dmu_buf_rele(zgd->zgd_db, zgd);
1853b24ab676SJeff Bonwick 
1854b24ab676SJeff Bonwick 	ztest_range_unlock(zgd->zgd_rl);
1855b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
1856b24ab676SJeff Bonwick 
1857b24ab676SJeff Bonwick 	if (error == 0 && zgd->zgd_bp)
18581271e4b1SPrakash Surya 		zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1859b24ab676SJeff Bonwick 
1860b24ab676SJeff Bonwick 	umem_free(zgd, sizeof (*zgd));
1861b24ab676SJeff Bonwick }
1862b24ab676SJeff Bonwick 
1863b24ab676SJeff Bonwick static int
18641271e4b1SPrakash Surya ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
18651271e4b1SPrakash Surya     zio_t *zio)
1866b24ab676SJeff Bonwick {
1867b24ab676SJeff Bonwick 	ztest_ds_t *zd = arg;
1868b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
1869b24ab676SJeff Bonwick 	uint64_t object = lr->lr_foid;
1870b24ab676SJeff Bonwick 	uint64_t offset = lr->lr_offset;
1871b24ab676SJeff Bonwick 	uint64_t size = lr->lr_length;
1872b24ab676SJeff Bonwick 	uint64_t txg = lr->lr_common.lrc_txg;
1873b24ab676SJeff Bonwick 	uint64_t crtxg;
1874b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1875b24ab676SJeff Bonwick 	dmu_buf_t *db;
1876b24ab676SJeff Bonwick 	zgd_t *zgd;
1877b24ab676SJeff Bonwick 	int error;
1878b24ab676SJeff Bonwick 
18791271e4b1SPrakash Surya 	ASSERT3P(lwb, !=, NULL);
18801271e4b1SPrakash Surya 	ASSERT3P(zio, !=, NULL);
18811271e4b1SPrakash Surya 	ASSERT3U(size, !=, 0);
18821271e4b1SPrakash Surya 
1883b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
1884b24ab676SJeff Bonwick 	error = dmu_bonus_hold(os, object, FTAG, &db);
1885b24ab676SJeff Bonwick 	if (error) {
1886b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1887b24ab676SJeff Bonwick 		return (error);
1888b24ab676SJeff Bonwick 	}
1889b24ab676SJeff Bonwick 
1890b24ab676SJeff Bonwick 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
1891b24ab676SJeff Bonwick 
1892b24ab676SJeff Bonwick 	if (crtxg == 0 || crtxg > txg) {
1893b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
1894b24ab676SJeff Bonwick 		ztest_object_unlock(zd, object);
1895b24ab676SJeff Bonwick 		return (ENOENT);
1896b24ab676SJeff Bonwick 	}
1897b24ab676SJeff Bonwick 
1898b24ab676SJeff Bonwick 	dmu_object_info_from_db(db, &doi);
1899b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
1900b24ab676SJeff Bonwick 	db = NULL;
1901b24ab676SJeff Bonwick 
1902b24ab676SJeff Bonwick 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
19031271e4b1SPrakash Surya 	zgd->zgd_lwb = lwb;
1904b24ab676SJeff Bonwick 	zgd->zgd_private = zd;
1905b24ab676SJeff Bonwick 
1906b24ab676SJeff Bonwick 	if (buf != NULL) {	/* immediate write */
1907b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1908b24ab676SJeff Bonwick 		    RL_READER);
1909b24ab676SJeff Bonwick 
1910b24ab676SJeff Bonwick 		error = dmu_read(os, object, offset, size, buf,
1911b24ab676SJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1912b24ab676SJeff Bonwick 		ASSERT(error == 0);
1913b24ab676SJeff Bonwick 	} else {
1914b24ab676SJeff Bonwick 		size = doi.doi_data_block_size;
1915dfe73b3dSJeff Bonwick 		if (ISP2(size)) {
1916b24ab676SJeff Bonwick 			offset = P2ALIGN(offset, size);
1917dfe73b3dSJeff Bonwick 		} else {
1918dfe73b3dSJeff Bonwick 			ASSERT(offset < size);
1919dfe73b3dSJeff Bonwick 			offset = 0;
1920dfe73b3dSJeff Bonwick 		}
1921b24ab676SJeff Bonwick 
1922b24ab676SJeff Bonwick 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1923b24ab676SJeff Bonwick 		    RL_READER);
1924b24ab676SJeff Bonwick 
192547cb52daSJeff Bonwick 		error = dmu_buf_hold(os, object, offset, zgd, &db,
192647cb52daSJeff Bonwick 		    DMU_READ_NO_PREFETCH);
1927b24ab676SJeff Bonwick 
1928b24ab676SJeff Bonwick 		if (error == 0) {
1929b7edcb94SMatthew Ahrens 			blkptr_t *bp = &lr->lr_blkptr;
193080901aeaSGeorge Wilson 
1931b24ab676SJeff Bonwick 			zgd->zgd_db = db;
1932b24ab676SJeff Bonwick 			zgd->zgd_bp = bp;
1933b24ab676SJeff Bonwick 
1934b24ab676SJeff Bonwick 			ASSERT(db->db_offset == offset);
1935b24ab676SJeff Bonwick 			ASSERT(db->db_size == size);
1936b24ab676SJeff Bonwick 
1937b24ab676SJeff Bonwick 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1938b24ab676SJeff Bonwick 			    ztest_get_done, zgd);
1939b24ab676SJeff Bonwick 
1940b24ab676SJeff Bonwick 			if (error == 0)
1941b24ab676SJeff Bonwick 				return (0);
1942b24ab676SJeff Bonwick 		}
1943b24ab676SJeff Bonwick 	}
1944b24ab676SJeff Bonwick 
1945b24ab676SJeff Bonwick 	ztest_get_done(zgd, error);
1946b24ab676SJeff Bonwick 
1947b24ab676SJeff Bonwick 	return (error);
1948b24ab676SJeff Bonwick }
1949b24ab676SJeff Bonwick 
1950b24ab676SJeff Bonwick static void *
1951b24ab676SJeff Bonwick ztest_lr_alloc(size_t lrsize, char *name)
1952b24ab676SJeff Bonwick {
1953b24ab676SJeff Bonwick 	char *lr;
1954b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1955b24ab676SJeff Bonwick 
1956b24ab676SJeff Bonwick 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1957b24ab676SJeff Bonwick 
1958b24ab676SJeff Bonwick 	if (name)
1959b24ab676SJeff Bonwick 		bcopy(name, lr + lrsize, namesize);
1960b24ab676SJeff Bonwick 
1961b24ab676SJeff Bonwick 	return (lr);
1962b24ab676SJeff Bonwick }
1963b24ab676SJeff Bonwick 
1964b24ab676SJeff Bonwick void
1965b24ab676SJeff Bonwick ztest_lr_free(void *lr, size_t lrsize, char *name)
1966b24ab676SJeff Bonwick {
1967b24ab676SJeff Bonwick 	size_t namesize = name ? strlen(name) + 1 : 0;
1968b24ab676SJeff Bonwick 
1969b24ab676SJeff Bonwick 	umem_free(lr, lrsize + namesize);
1970b24ab676SJeff Bonwick }
1971b24ab676SJeff Bonwick 
1972b24ab676SJeff Bonwick /*
1973b24ab676SJeff Bonwick  * Lookup a bunch of objects.  Returns the number of objects not found.
1974b24ab676SJeff Bonwick  */
1975b24ab676SJeff Bonwick static int
1976b24ab676SJeff Bonwick ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1977b24ab676SJeff Bonwick {
1978b24ab676SJeff Bonwick 	int missing = 0;
1979b24ab676SJeff Bonwick 	int error;
1980b24ab676SJeff Bonwick 
1981f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
1982b24ab676SJeff Bonwick 
1983b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
1984b24ab676SJeff Bonwick 		od->od_object = 0;
1985b24ab676SJeff Bonwick 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1986b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &od->od_object);
1987b24ab676SJeff Bonwick 		if (error) {
1988b24ab676SJeff Bonwick 			ASSERT(error == ENOENT);
1989b24ab676SJeff Bonwick 			ASSERT(od->od_object == 0);
1990b24ab676SJeff Bonwick 			missing++;
1991b24ab676SJeff Bonwick 		} else {
1992b24ab676SJeff Bonwick 			dmu_buf_t *db;
1993b24ab676SJeff Bonwick 			ztest_block_tag_t *bbt;
1994b24ab676SJeff Bonwick 			dmu_object_info_t doi;
1995b24ab676SJeff Bonwick 
1996b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
1997b24ab676SJeff Bonwick 			ASSERT(missing == 0);	/* there should be no gaps */
1998b24ab676SJeff Bonwick 
1999b24ab676SJeff Bonwick 			ztest_object_lock(zd, od->od_object, RL_READER);
2000b420f3adSRichard Lowe 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2001b24ab676SJeff Bonwick 			    od->od_object, FTAG, &db));
2002b24ab676SJeff Bonwick 			dmu_object_info_from_db(db, &doi);
2003b24ab676SJeff Bonwick 			bbt = ztest_bt_bonus(db);
2004b24ab676SJeff Bonwick 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2005b24ab676SJeff Bonwick 			od->od_type = doi.doi_type;
2006b24ab676SJeff Bonwick 			od->od_blocksize = doi.doi_data_block_size;
2007b24ab676SJeff Bonwick 			od->od_gen = bbt->bt_gen;
2008b24ab676SJeff Bonwick 			dmu_buf_rele(db, FTAG);
2009b24ab676SJeff Bonwick 			ztest_object_unlock(zd, od->od_object);
2010b24ab676SJeff Bonwick 		}
2011b24ab676SJeff Bonwick 	}
2012b24ab676SJeff Bonwick 
2013b24ab676SJeff Bonwick 	return (missing);
2014b24ab676SJeff Bonwick }
2015b24ab676SJeff Bonwick 
2016b24ab676SJeff Bonwick static int
2017b24ab676SJeff Bonwick ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2018b24ab676SJeff Bonwick {
2019b24ab676SJeff Bonwick 	int missing = 0;
2020b24ab676SJeff Bonwick 
2021f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2022b24ab676SJeff Bonwick 
2023b24ab676SJeff Bonwick 	for (int i = 0; i < count; i++, od++) {
2024b24ab676SJeff Bonwick 		if (missing) {
2025b24ab676SJeff Bonwick 			od->od_object = 0;
2026b24ab676SJeff Bonwick 			missing++;
2027b24ab676SJeff Bonwick 			continue;
2028b24ab676SJeff Bonwick 		}
2029b24ab676SJeff Bonwick 
2030b24ab676SJeff Bonwick 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2031b24ab676SJeff Bonwick 
2032b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2033b24ab676SJeff Bonwick 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
2034b24ab676SJeff Bonwick 		lr->lrz_type = od->od_crtype;
2035b24ab676SJeff Bonwick 		lr->lrz_blocksize = od->od_crblocksize;
2036b24ab676SJeff Bonwick 		lr->lrz_ibshift = ztest_random_ibshift();
2037b24ab676SJeff Bonwick 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2038b24ab676SJeff Bonwick 		lr->lrz_bonuslen = dmu_bonus_max();
2039b24ab676SJeff Bonwick 		lr->lr_gen = od->od_crgen;
2040b24ab676SJeff Bonwick 		lr->lr_crtime[0] = time(NULL);
2041b24ab676SJeff Bonwick 
2042b24ab676SJeff Bonwick 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2043b24ab676SJeff Bonwick 			ASSERT(missing == 0);
2044b24ab676SJeff Bonwick 			od->od_object = 0;
2045b24ab676SJeff Bonwick 			missing++;
2046b24ab676SJeff Bonwick 		} else {
2047b24ab676SJeff Bonwick 			od->od_object = lr->lr_foid;
2048b24ab676SJeff Bonwick 			od->od_type = od->od_crtype;
2049b24ab676SJeff Bonwick 			od->od_blocksize = od->od_crblocksize;
2050b24ab676SJeff Bonwick 			od->od_gen = od->od_crgen;
2051b24ab676SJeff Bonwick 			ASSERT(od->od_object != 0);
2052b24ab676SJeff Bonwick 		}
2053b24ab676SJeff Bonwick 
2054b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2055b24ab676SJeff Bonwick 	}
2056b24ab676SJeff Bonwick 
2057b24ab676SJeff Bonwick 	return (missing);
2058b24ab676SJeff Bonwick }
2059b24ab676SJeff Bonwick 
2060b24ab676SJeff Bonwick static int
2061b24ab676SJeff Bonwick ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2062b24ab676SJeff Bonwick {
2063b24ab676SJeff Bonwick 	int missing = 0;
2064b24ab676SJeff Bonwick 	int error;
2065b24ab676SJeff Bonwick 
2066f06dce2cSAndrew Stormont 	ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2067b24ab676SJeff Bonwick 
2068b24ab676SJeff Bonwick 	od += count - 1;
2069b24ab676SJeff Bonwick 
2070b24ab676SJeff Bonwick 	for (int i = count - 1; i >= 0; i--, od--) {
2071b24ab676SJeff Bonwick 		if (missing) {
2072b24ab676SJeff Bonwick 			missing++;
2073b24ab676SJeff Bonwick 			continue;
2074b24ab676SJeff Bonwick 		}
2075b24ab676SJeff Bonwick 
207680901aeaSGeorge Wilson 		/*
207780901aeaSGeorge Wilson 		 * No object was found.
207880901aeaSGeorge Wilson 		 */
2079b24ab676SJeff Bonwick 		if (od->od_object == 0)
2080b24ab676SJeff Bonwick 			continue;
2081b24ab676SJeff Bonwick 
2082b24ab676SJeff Bonwick 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2083b24ab676SJeff Bonwick 
2084b24ab676SJeff Bonwick 		lr->lr_doid = od->od_dir;
2085b24ab676SJeff Bonwick 
2086b24ab676SJeff Bonwick 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2087b24ab676SJeff Bonwick 			ASSERT3U(error, ==, ENOSPC);
2088b24ab676SJeff Bonwick 			missing++;
2089b24ab676SJeff Bonwick 		} else {
2090b24ab676SJeff Bonwick 			od->od_object = 0;
2091b24ab676SJeff Bonwick 		}
2092b24ab676SJeff Bonwick 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
2093b24ab676SJeff Bonwick 	}
2094b24ab676SJeff Bonwick 
2095b24ab676SJeff Bonwick 	return (missing);
2096b24ab676SJeff Bonwick }
2097b24ab676SJeff Bonwick 
2098b24ab676SJeff Bonwick static int
2099b24ab676SJeff Bonwick ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2100b24ab676SJeff Bonwick     void *data)
2101b24ab676SJeff Bonwick {
2102b24ab676SJeff Bonwick 	lr_write_t *lr;
2103b24ab676SJeff Bonwick 	int error;
2104b24ab676SJeff Bonwick 
2105b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2106b24ab676SJeff Bonwick 
2107b24ab676SJeff Bonwick 	lr->lr_foid = object;
2108b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2109b24ab676SJeff Bonwick 	lr->lr_length = size;
2110b24ab676SJeff Bonwick 	lr->lr_blkoff = 0;
2111b24ab676SJeff Bonwick 	BP_ZERO(&lr->lr_blkptr);
2112b24ab676SJeff Bonwick 
2113b24ab676SJeff Bonwick 	bcopy(data, lr + 1, size);
2114b24ab676SJeff Bonwick 
2115b24ab676SJeff Bonwick 	error = ztest_replay_write(zd, lr, B_FALSE);
2116b24ab676SJeff Bonwick 
2117b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2118b24ab676SJeff Bonwick 
2119b24ab676SJeff Bonwick 	return (error);
2120b24ab676SJeff Bonwick }
2121b24ab676SJeff Bonwick 
2122b24ab676SJeff Bonwick static int
2123b24ab676SJeff Bonwick ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2124b24ab676SJeff Bonwick {
2125b24ab676SJeff Bonwick 	lr_truncate_t *lr;
2126b24ab676SJeff Bonwick 	int error;
2127b24ab676SJeff Bonwick 
2128b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2129b24ab676SJeff Bonwick 
2130b24ab676SJeff Bonwick 	lr->lr_foid = object;
2131b24ab676SJeff Bonwick 	lr->lr_offset = offset;
2132b24ab676SJeff Bonwick 	lr->lr_length = size;
2133b24ab676SJeff Bonwick 
2134b24ab676SJeff Bonwick 	error = ztest_replay_truncate(zd, lr, B_FALSE);
2135b24ab676SJeff Bonwick 
2136b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2137b24ab676SJeff Bonwick 
2138b24ab676SJeff Bonwick 	return (error);
2139b24ab676SJeff Bonwick }
2140b24ab676SJeff Bonwick 
2141b24ab676SJeff Bonwick static int
2142b24ab676SJeff Bonwick ztest_setattr(ztest_ds_t *zd, uint64_t object)
2143b24ab676SJeff Bonwick {
2144b24ab676SJeff Bonwick 	lr_setattr_t *lr;
2145b24ab676SJeff Bonwick 	int error;
2146b24ab676SJeff Bonwick 
2147b24ab676SJeff Bonwick 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
2148b24ab676SJeff Bonwick 
2149b24ab676SJeff Bonwick 	lr->lr_foid = object;
2150b24ab676SJeff Bonwick 	lr->lr_size = 0;
2151b24ab676SJeff Bonwick 	lr->lr_mode = 0;
2152b24ab676SJeff Bonwick 
2153b24ab676SJeff Bonwick 	error = ztest_replay_setattr(zd, lr, B_FALSE);
2154b24ab676SJeff Bonwick 
2155b24ab676SJeff Bonwick 	ztest_lr_free(lr, sizeof (*lr), NULL);
2156b24ab676SJeff Bonwick 
2157b24ab676SJeff Bonwick 	return (error);
2158b24ab676SJeff Bonwick }
2159b24ab676SJeff Bonwick 
2160b24ab676SJeff Bonwick static void
2161b24ab676SJeff Bonwick ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2162b24ab676SJeff Bonwick {
2163b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
2164b24ab676SJeff Bonwick 	dmu_tx_t *tx;
2165b24ab676SJeff Bonwick 	uint64_t txg;
2166b24ab676SJeff Bonwick 	rl_t *rl;
2167b24ab676SJeff Bonwick 
2168b24ab676SJeff Bonwick 	txg_wait_synced(dmu_objset_pool(os), 0);
2169b24ab676SJeff Bonwick 
2170b24ab676SJeff Bonwick 	ztest_object_lock(zd, object, RL_READER);
2171b24ab676SJeff Bonwick 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2172b24ab676SJeff Bonwick 
2173b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
2174b24ab676SJeff Bonwick 
2175b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, offset, size);
2176b24ab676SJeff Bonwick 
2177b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2178b24ab676SJeff Bonwick 
2179b24ab676SJeff Bonwick 	if (txg != 0) {
2180b24ab676SJeff Bonwick 		dmu_prealloc(os, object, offset, size, tx);
2181b24ab676SJeff Bonwick 		dmu_tx_commit(tx);
2182b24ab676SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2183b24ab676SJeff Bonwick 	} else {
2184b24ab676SJeff Bonwick 		(void) dmu_free_long_range(os, object, offset, size);
2185b24ab676SJeff Bonwick 	}
2186b24ab676SJeff Bonwick 
2187b24ab676SJeff Bonwick 	ztest_range_unlock(rl);
2188b24ab676SJeff Bonwick 	ztest_object_unlock(zd, object);
2189b24ab676SJeff Bonwick }
2190b24ab676SJeff Bonwick 
2191b24ab676SJeff Bonwick static void
2192b24ab676SJeff Bonwick ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2193b24ab676SJeff Bonwick {
219480901aeaSGeorge Wilson 	int err;
2195b24ab676SJeff Bonwick 	ztest_block_tag_t wbt;
2196b24ab676SJeff Bonwick 	dmu_object_info_t doi;
2197b24ab676SJeff Bonwick 	enum ztest_io_type io_type;
2198b24ab676SJeff Bonwick 	uint64_t blocksize;
2199b24ab676SJeff Bonwick 	void *data;
2200b24ab676SJeff Bonwick 
2201b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2202b24ab676SJeff Bonwick 	blocksize = doi.doi_data_block_size;
2203b24ab676SJeff Bonwick 	data = umem_alloc(blocksize, UMEM_NOFAIL);
2204b24ab676SJeff Bonwick 
2205b24ab676SJeff Bonwick 	/*
2206b24ab676SJeff Bonwick 	 * Pick an i/o type at random, biased toward writing block tags.
2207b24ab676SJeff Bonwick 	 */
2208b24ab676SJeff Bonwick 	io_type = ztest_random(ZTEST_IO_TYPES);
2209b24ab676SJeff Bonwick 	if (ztest_random(2) == 0)
2210b24ab676SJeff Bonwick 		io_type = ZTEST_IO_WRITE_TAG;
2211b24ab676SJeff Bonwick 
2212f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2213c9ba2a43SEric Schrock 
2214b24ab676SJeff Bonwick 	switch (io_type) {
2215b24ab676SJeff Bonwick 
2216b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_TAG:
2217b24ab676SJeff Bonwick 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2218b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2219b24ab676SJeff Bonwick 		break;
2220b24ab676SJeff Bonwick 
2221b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_PATTERN:
2222b24ab676SJeff Bonwick 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
2223b24ab676SJeff Bonwick 		if (ztest_random(2) == 0) {
2224b24ab676SJeff Bonwick 			/*
2225b24ab676SJeff Bonwick 			 * Induce fletcher2 collisions to ensure that
2226b24ab676SJeff Bonwick 			 * zio_ddt_collision() detects and resolves them
2227b24ab676SJeff Bonwick 			 * when using fletcher2-verify for deduplication.
2228b24ab676SJeff Bonwick 			 */
2229b24ab676SJeff Bonwick 			((uint64_t *)data)[0] ^= 1ULL << 63;
2230b24ab676SJeff Bonwick 			((uint64_t *)data)[4] ^= 1ULL << 63;
2231b24ab676SJeff Bonwick 		}
2232b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2233b24ab676SJeff Bonwick 		break;
2234b24ab676SJeff Bonwick 
2235b24ab676SJeff Bonwick 	case ZTEST_IO_WRITE_ZEROES:
2236b24ab676SJeff Bonwick 		bzero(data, blocksize);
2237b24ab676SJeff Bonwick 		(void) ztest_write(zd, object, offset, blocksize, data);
2238b24ab676SJeff Bonwick 		break;
2239b24ab676SJeff Bonwick 
2240b24ab676SJeff Bonwick 	case ZTEST_IO_TRUNCATE:
2241b24ab676SJeff Bonwick 		(void) ztest_truncate(zd, object, offset, blocksize);
2242b24ab676SJeff Bonwick 		break;
2243b24ab676SJeff Bonwick 
2244b24ab676SJeff Bonwick 	case ZTEST_IO_SETATTR:
2245b24ab676SJeff Bonwick 		(void) ztest_setattr(zd, object);
2246b24ab676SJeff Bonwick 		break;
224780901aeaSGeorge Wilson 
224880901aeaSGeorge Wilson 	case ZTEST_IO_REWRITE:
2249f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_READER);
225080901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
225180901aeaSGeorge Wilson 		    ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
225280901aeaSGeorge Wilson 		    B_FALSE);
225380901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
225480901aeaSGeorge Wilson 		err = ztest_dsl_prop_set_uint64(zd->zd_name,
225580901aeaSGeorge Wilson 		    ZFS_PROP_COMPRESSION,
225680901aeaSGeorge Wilson 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
225780901aeaSGeorge Wilson 		    B_FALSE);
225880901aeaSGeorge Wilson 		VERIFY(err == 0 || err == ENOSPC);
2259f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
226080901aeaSGeorge Wilson 
226180901aeaSGeorge Wilson 		VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
226280901aeaSGeorge Wilson 		    DMU_READ_NO_PREFETCH));
226380901aeaSGeorge Wilson 
226480901aeaSGeorge Wilson 		(void) ztest_write(zd, object, offset, blocksize, data);
226580901aeaSGeorge Wilson 		break;
2266b24ab676SJeff Bonwick 	}
2267b24ab676SJeff Bonwick 
2268f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2269c9ba2a43SEric Schrock 
2270b24ab676SJeff Bonwick 	umem_free(data, blocksize);
2271b24ab676SJeff Bonwick }
2272b24ab676SJeff Bonwick 
2273b24ab676SJeff Bonwick /*
2274b24ab676SJeff Bonwick  * Initialize an object description template.
2275b24ab676SJeff Bonwick  */
2276b24ab676SJeff Bonwick static void
2277b24ab676SJeff Bonwick ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2278b24ab676SJeff Bonwick     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2279b24ab676SJeff Bonwick {
2280b24ab676SJeff Bonwick 	od->od_dir = ZTEST_DIROBJ;
2281b24ab676SJeff Bonwick 	od->od_object = 0;
2282b24ab676SJeff Bonwick 
2283b24ab676SJeff Bonwick 	od->od_crtype = type;
2284b24ab676SJeff Bonwick 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2285b24ab676SJeff Bonwick 	od->od_crgen = gen;
2286b24ab676SJeff Bonwick 
2287b24ab676SJeff Bonwick 	od->od_type = DMU_OT_NONE;
2288b24ab676SJeff Bonwick 	od->od_blocksize = 0;
2289b24ab676SJeff Bonwick 	od->od_gen = 0;
2290b24ab676SJeff Bonwick 
2291b24ab676SJeff Bonwick 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2292b24ab676SJeff Bonwick 	    tag, (int64_t)id, index);
2293b24ab676SJeff Bonwick }
2294b24ab676SJeff Bonwick 
2295b24ab676SJeff Bonwick /*
2296b24ab676SJeff Bonwick  * Lookup or create the objects for a test using the od template.
2297b24ab676SJeff Bonwick  * If the objects do not all exist, or if 'remove' is specified,
2298b24ab676SJeff Bonwick  * remove any existing objects and create new ones.  Otherwise,
2299b24ab676SJeff Bonwick  * use the existing objects.
2300b24ab676SJeff Bonwick  */
2301b24ab676SJeff Bonwick static int
2302b24ab676SJeff Bonwick ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2303b24ab676SJeff Bonwick {
2304b24ab676SJeff Bonwick 	int count = size / sizeof (*od);
2305b24ab676SJeff Bonwick 	int rv = 0;
2306b24ab676SJeff Bonwick 
2307f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2308b24ab676SJeff Bonwick 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2309b24ab676SJeff Bonwick 	    (ztest_remove(zd, od, count) != 0 ||
2310b24ab676SJeff Bonwick 	    ztest_create(zd, od, count) != 0))
2311b24ab676SJeff Bonwick 		rv = -1;
2312b24ab676SJeff Bonwick 	zd->zd_od = od;
2313f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2314b24ab676SJeff Bonwick 
2315b24ab676SJeff Bonwick 	return (rv);
2316b24ab676SJeff Bonwick }
2317b24ab676SJeff Bonwick 
2318b24ab676SJeff Bonwick /* ARGSUSED */
2319b24ab676SJeff Bonwick void
2320b24ab676SJeff Bonwick ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2321b24ab676SJeff Bonwick {
2322b24ab676SJeff Bonwick 	zilog_t *zilog = zd->zd_zilog;
2323b24ab676SJeff Bonwick 
2324f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_READER);
2325c9ba2a43SEric Schrock 
23265002558fSNeil Perrin 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2327b24ab676SJeff Bonwick 
2328b24ab676SJeff Bonwick 	/*
2329b24ab676SJeff Bonwick 	 * Remember the committed values in zd, which is in parent/child
2330b24ab676SJeff Bonwick 	 * shared memory.  If we die, the next iteration of ztest_run()
2331b24ab676SJeff Bonwick 	 * will verify that the log really does contain this record.
2332b24ab676SJeff Bonwick 	 */
2333b24ab676SJeff Bonwick 	mutex_enter(&zilog->zl_lock);
2334420dfc95SChris Siden 	ASSERT(zd->zd_shared != NULL);
2335420dfc95SChris Siden 	ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2336420dfc95SChris Siden 	zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2337b24ab676SJeff Bonwick 	mutex_exit(&zilog->zl_lock);
2338c9ba2a43SEric Schrock 
2339f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2340c9ba2a43SEric Schrock }
2341c9ba2a43SEric Schrock 
2342c9ba2a43SEric Schrock /*
2343c9ba2a43SEric Schrock  * This function is designed to simulate the operations that occur during a
2344c9ba2a43SEric Schrock  * mount/unmount operation.  We hold the dataset across these operations in an
2345c9ba2a43SEric Schrock  * attempt to expose any implicit assumptions about ZIL management.
2346c9ba2a43SEric Schrock  */
2347c9ba2a43SEric Schrock /* ARGSUSED */
2348c9ba2a43SEric Schrock void
2349c9ba2a43SEric Schrock ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2350c9ba2a43SEric Schrock {
2351c9ba2a43SEric Schrock 	objset_t *os = zd->zd_os;
2352c9ba2a43SEric Schrock 
235380901aeaSGeorge Wilson 	/*
235480901aeaSGeorge Wilson 	 * We grab the zd_dirobj_lock to ensure that no other thread is
235580901aeaSGeorge Wilson 	 * updating the zil (i.e. adding in-memory log records) and the
235680901aeaSGeorge Wilson 	 * zd_zilog_lock to block any I/O.
235780901aeaSGeorge Wilson 	 */
2358f06dce2cSAndrew Stormont 	mutex_enter(&zd->zd_dirobj_lock);
2359f06dce2cSAndrew Stormont 	rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2360c9ba2a43SEric Schrock 
2361c9ba2a43SEric Schrock 	/* zfsvfs_teardown() */
2362c9ba2a43SEric Schrock 	zil_close(zd->zd_zilog);
2363c9ba2a43SEric Schrock 
2364c9ba2a43SEric Schrock 	/* zfsvfs_setup() */
2365c9ba2a43SEric Schrock 	VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2366c9ba2a43SEric Schrock 	zil_replay(os, zd, ztest_replay_vector);
2367c9ba2a43SEric Schrock 
2368f06dce2cSAndrew Stormont 	rw_exit(&zd->zd_zilog_lock);
2369f06dce2cSAndrew Stormont 	mutex_exit(&zd->zd_dirobj_lock);
2370b24ab676SJeff Bonwick }
2371b24ab676SJeff Bonwick 
2372b24ab676SJeff Bonwick /*
2373b24ab676SJeff Bonwick  * Verify that we can't destroy an active pool, create an existing pool,
2374b24ab676SJeff Bonwick  * or create a pool with a bad vdev spec.
2375b24ab676SJeff Bonwick  */
2376b24ab676SJeff Bonwick /* ARGSUSED */
2377b24ab676SJeff Bonwick void
2378b24ab676SJeff Bonwick ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2379b24ab676SJeff Bonwick {
2380420dfc95SChris Siden 	ztest_shared_opts_t *zo = &ztest_opts;
2381b24ab676SJeff Bonwick 	spa_t *spa;
2382b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2383b24ab676SJeff Bonwick 
2384b24ab676SJeff Bonwick 	/*
2385b24ab676SJeff Bonwick 	 * Attempt to create using a bad file.
2386b24ab676SJeff Bonwick 	 */
238725345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2388b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23894445fffbSMatthew Ahrens 	    spa_create("ztest_bad_file", nvroot, NULL, NULL));
2390b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2391b24ab676SJeff Bonwick 
2392b24ab676SJeff Bonwick 	/*
2393b24ab676SJeff Bonwick 	 * Attempt to create using a bad mirror.
2394b24ab676SJeff Bonwick 	 */
239525345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2396b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==,
23974445fffbSMatthew Ahrens 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2398b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2399b24ab676SJeff Bonwick 
2400b24ab676SJeff Bonwick 	/*
2401b24ab676SJeff Bonwick 	 * Attempt to create an existing pool.  It shouldn't matter
2402b24ab676SJeff Bonwick 	 * what's in the nvroot; we should fail with EEXIST.
2403b24ab676SJeff Bonwick 	 */
2404f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
240525345e46SGeorge Wilson 	nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
24064445fffbSMatthew Ahrens 	VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2407b24ab676SJeff Bonwick 	nvlist_free(nvroot);
2408b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2409420dfc95SChris Siden 	VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2410b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
2411b24ab676SJeff Bonwick 
2412f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
2413b24ab676SJeff Bonwick }
2414b24ab676SJeff Bonwick 
241525345e46SGeorge Wilson /* ARGSUSED */
241625345e46SGeorge Wilson void
241725345e46SGeorge Wilson ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
241825345e46SGeorge Wilson {
241925345e46SGeorge Wilson 	spa_t *spa;
242025345e46SGeorge Wilson 	uint64_t initial_version = SPA_VERSION_INITIAL;
242125345e46SGeorge Wilson 	uint64_t version, newversion;
242225345e46SGeorge Wilson 	nvlist_t *nvroot, *props;
242325345e46SGeorge Wilson 	char *name;
242425345e46SGeorge Wilson 
2425f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
242625345e46SGeorge Wilson 	name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
242725345e46SGeorge Wilson 
242825345e46SGeorge Wilson 	/*
242925345e46SGeorge Wilson 	 * Clean up from previous runs.
243025345e46SGeorge Wilson 	 */
243125345e46SGeorge Wilson 	(void) spa_destroy(name);
243225345e46SGeorge Wilson 
243325345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
243425345e46SGeorge Wilson 	    0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
243525345e46SGeorge Wilson 
243625345e46SGeorge Wilson 	/*
243725345e46SGeorge Wilson 	 * If we're configuring a RAIDZ device then make sure that the
243825345e46SGeorge Wilson 	 * the initial version is capable of supporting that feature.
243925345e46SGeorge Wilson 	 */
244025345e46SGeorge Wilson 	switch (ztest_opts.zo_raidz_parity) {
244125345e46SGeorge Wilson 	case 0:
244225345e46SGeorge Wilson 	case 1:
244325345e46SGeorge Wilson 		initial_version = SPA_VERSION_INITIAL;
244425345e46SGeorge Wilson 		break;
244525345e46SGeorge Wilson 	case 2:
244625345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ2;
244725345e46SGeorge Wilson 		break;
244825345e46SGeorge Wilson 	case 3:
244925345e46SGeorge Wilson 		initial_version = SPA_VERSION_RAIDZ3;
245025345e46SGeorge Wilson 		break;
245125345e46SGeorge Wilson 	}
245225345e46SGeorge Wilson 
245325345e46SGeorge Wilson 	/*
245425345e46SGeorge Wilson 	 * Create a pool with a spa version that can be upgraded. Pick
245525345e46SGeorge Wilson 	 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
245625345e46SGeorge Wilson 	 */
245725345e46SGeorge Wilson 	do {
245825345e46SGeorge Wilson 		version = ztest_random_spa_version(initial_version);
245925345e46SGeorge Wilson 	} while (version > SPA_VERSION_BEFORE_FEATURES);
246025345e46SGeorge Wilson 
246125345e46SGeorge Wilson 	props = fnvlist_alloc();
246225345e46SGeorge Wilson 	fnvlist_add_uint64(props,
246325345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
246425345e46SGeorge Wilson 	VERIFY0(spa_create(name, nvroot, props, NULL));
246525345e46SGeorge Wilson 	fnvlist_free(nvroot);
246625345e46SGeorge Wilson 	fnvlist_free(props);
246725345e46SGeorge Wilson 
246825345e46SGeorge Wilson 	VERIFY0(spa_open(name, &spa, FTAG));
246925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, version);
247025345e46SGeorge Wilson 	newversion = ztest_random_spa_version(version + 1);
247125345e46SGeorge Wilson 
247225345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
247325345e46SGeorge Wilson 		(void) printf("upgrading spa version from %llu to %llu\n",
247425345e46SGeorge Wilson 		    (u_longlong_t)version, (u_longlong_t)newversion);
247525345e46SGeorge Wilson 	}
247625345e46SGeorge Wilson 
247725345e46SGeorge Wilson 	spa_upgrade(spa, newversion);
247825345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), >, version);
247925345e46SGeorge Wilson 	VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
248025345e46SGeorge Wilson 	    zpool_prop_to_name(ZPOOL_PROP_VERSION)));
248125345e46SGeorge Wilson 	spa_close(spa, FTAG);
248225345e46SGeorge Wilson 
248325345e46SGeorge Wilson 	strfree(name);
2484f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
248525345e46SGeorge Wilson }
248625345e46SGeorge Wilson 
248786714001SSerapheim Dimitropoulos static void
248886714001SSerapheim Dimitropoulos ztest_spa_checkpoint(spa_t *spa)
248986714001SSerapheim Dimitropoulos {
249086714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
249186714001SSerapheim Dimitropoulos 
249286714001SSerapheim Dimitropoulos 	int error = spa_checkpoint(spa->spa_name);
249386714001SSerapheim Dimitropoulos 
249486714001SSerapheim Dimitropoulos 	switch (error) {
249586714001SSerapheim Dimitropoulos 	case 0:
249686714001SSerapheim Dimitropoulos 	case ZFS_ERR_DEVRM_IN_PROGRESS:
249786714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
249886714001SSerapheim Dimitropoulos 	case ZFS_ERR_CHECKPOINT_EXISTS:
249986714001SSerapheim Dimitropoulos 		break;
250086714001SSerapheim Dimitropoulos 	case ENOSPC:
250186714001SSerapheim Dimitropoulos 		ztest_record_enospc(FTAG);
250286714001SSerapheim Dimitropoulos 		break;
250386714001SSerapheim Dimitropoulos 	default:
250486714001SSerapheim Dimitropoulos 		fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
250586714001SSerapheim Dimitropoulos 	}
250686714001SSerapheim Dimitropoulos }
250786714001SSerapheim Dimitropoulos 
250886714001SSerapheim Dimitropoulos static void
250986714001SSerapheim Dimitropoulos ztest_spa_discard_checkpoint(spa_t *spa)
251086714001SSerapheim Dimitropoulos {
251186714001SSerapheim Dimitropoulos 	ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
251286714001SSerapheim Dimitropoulos 
251386714001SSerapheim Dimitropoulos 	int error = spa_checkpoint_discard(spa->spa_name);
251486714001SSerapheim Dimitropoulos 
251586714001SSerapheim Dimitropoulos 	switch (error) {
251686714001SSerapheim Dimitropoulos 	case 0:
251786714001SSerapheim Dimitropoulos 	case ZFS_ERR_DISCARDING_CHECKPOINT:
251886714001SSerapheim Dimitropoulos 	case ZFS_ERR_NO_CHECKPOINT:
251986714001SSerapheim Dimitropoulos 		break;
252086714001SSerapheim Dimitropoulos 	default:
252186714001SSerapheim Dimitropoulos 		fatal(0, "spa_discard_checkpoint(%s) = %d",
252286714001SSerapheim Dimitropoulos 		    spa->spa_name, error);
252386714001SSerapheim Dimitropoulos 	}
252486714001SSerapheim Dimitropoulos 
252586714001SSerapheim Dimitropoulos }
252686714001SSerapheim Dimitropoulos 
252786714001SSerapheim Dimitropoulos /* ARGSUSED */
252886714001SSerapheim Dimitropoulos void
252986714001SSerapheim Dimitropoulos ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
253086714001SSerapheim Dimitropoulos {
253186714001SSerapheim Dimitropoulos 	spa_t *spa = ztest_spa;
253286714001SSerapheim Dimitropoulos 
253386714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
253486714001SSerapheim Dimitropoulos 	if (ztest_random(2) == 0) {
253586714001SSerapheim Dimitropoulos 		ztest_spa_checkpoint(spa);
253686714001SSerapheim Dimitropoulos 	} else {
253786714001SSerapheim Dimitropoulos 		ztest_spa_discard_checkpoint(spa);
253886714001SSerapheim Dimitropoulos 	}
253986714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
254086714001SSerapheim Dimitropoulos }
254186714001SSerapheim Dimitropoulos 
254286714001SSerapheim Dimitropoulos 
2543b24ab676SJeff Bonwick static vdev_t *
2544b24ab676SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
2545b24ab676SJeff Bonwick {
2546b24ab676SJeff Bonwick 	vdev_t *mvd;
2547b24ab676SJeff Bonwick 
2548b24ab676SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2549b24ab676SJeff Bonwick 		return (vd);
2550b24ab676SJeff Bonwick 
2551b24ab676SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
2552b24ab676SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2553b24ab676SJeff Bonwick 		    NULL)
2554b24ab676SJeff Bonwick 			return (mvd);
2555b24ab676SJeff Bonwick 
2556b24ab676SJeff Bonwick 	return (NULL);
2557b24ab676SJeff Bonwick }
2558b24ab676SJeff Bonwick 
2559b24ab676SJeff Bonwick /*
2560b24ab676SJeff Bonwick  * Find the first available hole which can be used as a top-level.
2561b24ab676SJeff Bonwick  */
2562b24ab676SJeff Bonwick int
2563b24ab676SJeff Bonwick find_vdev_hole(spa_t *spa)
2564b24ab676SJeff Bonwick {
2565b24ab676SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2566b24ab676SJeff Bonwick 	int c;
2567b24ab676SJeff Bonwick 
2568b24ab676SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2569b24ab676SJeff Bonwick 
2570b24ab676SJeff Bonwick 	for (c = 0; c < rvd->vdev_children; c++) {
2571b24ab676SJeff Bonwick 		vdev_t *cvd = rvd->vdev_child[c];
2572b24ab676SJeff Bonwick 
2573b24ab676SJeff Bonwick 		if (cvd->vdev_ishole)
2574b24ab676SJeff Bonwick 			break;
2575b24ab676SJeff Bonwick 	}
2576b24ab676SJeff Bonwick 	return (c);
2577b24ab676SJeff Bonwick }
2578b24ab676SJeff Bonwick 
2579b24ab676SJeff Bonwick /*
2580b24ab676SJeff Bonwick  * Verify that vdev_add() works as expected.
2581b24ab676SJeff Bonwick  */
2582b24ab676SJeff Bonwick /* ARGSUSED */
2583b24ab676SJeff Bonwick void
2584b24ab676SJeff Bonwick ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2585b24ab676SJeff Bonwick {
2586b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2587420dfc95SChris Siden 	spa_t *spa = ztest_spa;
25881195e687SMark J Musante 	uint64_t leaves;
2589b24ab676SJeff Bonwick 	uint64_t guid;
2590b24ab676SJeff Bonwick 	nvlist_t *nvroot;
2591b24ab676SJeff Bonwick 	int error;
2592b24ab676SJeff Bonwick 
2593f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
25943b2aab18SMatthew Ahrens 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2595b24ab676SJeff Bonwick 
2596b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2597b24ab676SJeff Bonwick 
2598b24ab676SJeff Bonwick 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2599b24ab676SJeff Bonwick 
2600b24ab676SJeff Bonwick 	/*
2601b24ab676SJeff Bonwick 	 * If we have slogs then remove them 1/4 of the time.
2602b24ab676SJeff Bonwick 	 */
260388ecc943SGeorge Wilson 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
260488ecc943SGeorge Wilson 		/*
260588ecc943SGeorge Wilson 		 * Grab the guid from the head of the log class rotor.
260688ecc943SGeorge Wilson 		 */
2607b24ab676SJeff Bonwick 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
26088654d025Sperrin 
260988ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
2610fa9e4066Sahrens 
261188ecc943SGeorge Wilson 		/*
261288ecc943SGeorge Wilson 		 * We have to grab the zs_name_lock as writer to
261388ecc943SGeorge Wilson 		 * prevent a race between removing a slog (dmu_objset_find)
261488ecc943SGeorge Wilson 		 * and destroying a dataset. Removing the slog will
261588ecc943SGeorge Wilson 		 * grab a reference on the dataset which may cause
261688ecc943SGeorge Wilson 		 * dmu_objset_destroy() to fail with EBUSY thus
261788ecc943SGeorge Wilson 		 * leaving the dataset in an inconsistent state.
261888ecc943SGeorge Wilson 		 */
2619f06dce2cSAndrew Stormont 		rw_enter(&ztest_name_lock, RW_WRITER);
262088ecc943SGeorge Wilson 		error = spa_vdev_remove(spa, guid, B_FALSE);
2621f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
2622fa9e4066Sahrens 
262386714001SSerapheim Dimitropoulos 		switch (error) {
262486714001SSerapheim Dimitropoulos 		case 0:
262586714001SSerapheim Dimitropoulos 		case EEXIST:
262686714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
262786714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
262886714001SSerapheim Dimitropoulos 			break;
262986714001SSerapheim Dimitropoulos 		default:
263088ecc943SGeorge Wilson 			fatal(0, "spa_vdev_remove() = %d", error);
263186714001SSerapheim Dimitropoulos 		}
263288ecc943SGeorge Wilson 	} else {
263388ecc943SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
263488ecc943SGeorge Wilson 
263588ecc943SGeorge Wilson 		/*
263688ecc943SGeorge Wilson 		 * Make 1/4 of the devices be log devices.
263788ecc943SGeorge Wilson 		 */
263825345e46SGeorge Wilson 		nvroot = make_vdev_root(NULL, NULL, NULL,
2639420dfc95SChris Siden 		    ztest_opts.zo_vdev_size, 0,
2640420dfc95SChris Siden 		    ztest_random(4) == 0, ztest_opts.zo_raidz,
2641420dfc95SChris Siden 		    zs->zs_mirrors, 1);
264288ecc943SGeorge Wilson 
264388ecc943SGeorge Wilson 		error = spa_vdev_add(spa, nvroot);
264488ecc943SGeorge Wilson 		nvlist_free(nvroot);
264588ecc943SGeorge Wilson 
264686714001SSerapheim Dimitropoulos 		switch (error) {
264786714001SSerapheim Dimitropoulos 		case 0:
264886714001SSerapheim Dimitropoulos 			break;
264986714001SSerapheim Dimitropoulos 		case ENOSPC:
265088ecc943SGeorge Wilson 			ztest_record_enospc("spa_vdev_add");
265186714001SSerapheim Dimitropoulos 			break;
265286714001SSerapheim Dimitropoulos 		default:
265388ecc943SGeorge Wilson 			fatal(0, "spa_vdev_add() = %d", error);
265486714001SSerapheim Dimitropoulos 		}
265588ecc943SGeorge Wilson 	}
265688ecc943SGeorge Wilson 
2657f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
2658e14bb325SJeff Bonwick }
2659fa9e4066Sahrens 
2660e14bb325SJeff Bonwick /*
2661e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2662e14bb325SJeff Bonwick  */
2663b24ab676SJeff Bonwick /* ARGSUSED */
2664e14bb325SJeff Bonwick void
2665b24ab676SJeff Bonwick ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2666e14bb325SJeff Bonwick {
2667b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2668420dfc95SChris Siden 	spa_t *spa = ztest_spa;
266931157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
2670e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
2671e14bb325SJeff Bonwick 	char *aux;
2672e14bb325SJeff Bonwick 	uint64_t guid = 0;
2673e14bb325SJeff Bonwick 	int error;
2674e14bb325SJeff Bonwick 
267531157203SJeff Bonwick 	if (ztest_random(2) == 0) {
2676e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
2677e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
2678e14bb325SJeff Bonwick 	} else {
2679e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
2680e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
2681e14bb325SJeff Bonwick 	}
2682e14bb325SJeff Bonwick 
2683f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
2684e14bb325SJeff Bonwick 
2685e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2686e14bb325SJeff Bonwick 
2687e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
2688e14bb325SJeff Bonwick 		/*
2689e14bb325SJeff Bonwick 		 * Pick a random device to remove.
2690e14bb325SJeff Bonwick 		 */
2691e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2692e14bb325SJeff Bonwick 	} else {
2693e14bb325SJeff Bonwick 		/*
2694e14bb325SJeff Bonwick 		 * Find an unused device we can add.
2695e14bb325SJeff Bonwick 		 */
2696b24ab676SJeff Bonwick 		zs->zs_vdev_aux = 0;
2697e14bb325SJeff Bonwick 		for (;;) {
2698e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
2699e14bb325SJeff Bonwick 			int c;
2700420dfc95SChris Siden 			(void) snprintf(path, sizeof (path), ztest_aux_template,
2701420dfc95SChris Siden 			    ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2702420dfc95SChris Siden 			    zs->zs_vdev_aux);
2703e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
2704e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
2705e14bb325SJeff Bonwick 				    path) == 0)
2706e14bb325SJeff Bonwick 					break;
270731157203SJeff Bonwick 			if (c == sav->sav_count &&
270831157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
2709e14bb325SJeff Bonwick 				break;
2710b24ab676SJeff Bonwick 			zs->zs_vdev_aux++;
2711e14bb325SJeff Bonwick 		}
2712e14bb325SJeff Bonwick 	}
2713e14bb325SJeff Bonwick 
2714e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
2715e14bb325SJeff Bonwick 
2716e14bb325SJeff Bonwick 	if (guid == 0) {
2717e14bb325SJeff Bonwick 		/*
2718e14bb325SJeff Bonwick 		 * Add a new device.
2719e14bb325SJeff Bonwick 		 */
272025345e46SGeorge Wilson 		nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2721420dfc95SChris Siden 		    (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2722e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
272386714001SSerapheim Dimitropoulos 
272486714001SSerapheim Dimitropoulos 		switch (error) {
272586714001SSerapheim Dimitropoulos 		case 0:
272686714001SSerapheim Dimitropoulos 			break;
272786714001SSerapheim Dimitropoulos 		default:
2728e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
272986714001SSerapheim Dimitropoulos 		}
2730e14bb325SJeff Bonwick 		nvlist_free(nvroot);
2731e14bb325SJeff Bonwick 	} else {
2732e14bb325SJeff Bonwick 		/*
2733e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
2734e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
2735e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
2736e14bb325SJeff Bonwick 		 */
2737e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
2738573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
2739e14bb325SJeff Bonwick 
2740e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
274186714001SSerapheim Dimitropoulos 
274286714001SSerapheim Dimitropoulos 		switch (error) {
274386714001SSerapheim Dimitropoulos 		case 0:
274486714001SSerapheim Dimitropoulos 		case EBUSY:
274586714001SSerapheim Dimitropoulos 		case ZFS_ERR_CHECKPOINT_EXISTS:
274686714001SSerapheim Dimitropoulos 		case ZFS_ERR_DISCARDING_CHECKPOINT:
274786714001SSerapheim Dimitropoulos 			break;
274886714001SSerapheim Dimitropoulos 		default:
2749e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
275086714001SSerapheim Dimitropoulos 		}
2751e14bb325SJeff Bonwick 	}
2752e14bb325SJeff Bonwick 
2753f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
2754fa9e4066Sahrens }
2755fa9e4066Sahrens 
27561195e687SMark J Musante /*
27571195e687SMark J Musante  * split a pool if it has mirror tlvdevs
27581195e687SMark J Musante  */
27591195e687SMark J Musante /* ARGSUSED */
27601195e687SMark J Musante void
27611195e687SMark J Musante ztest_split_pool(ztest_ds_t *zd, uint64_t id)
27621195e687SMark J Musante {
27631195e687SMark J Musante 	ztest_shared_t *zs = ztest_shared;
2764420dfc95SChris Siden 	spa_t *spa = ztest_spa;
27651195e687SMark J Musante 	vdev_t *rvd = spa->spa_root_vdev;
27661195e687SMark J Musante 	nvlist_t *tree, **child, *config, *split, **schild;
27671195e687SMark J Musante 	uint_t c, children, schildren = 0, lastlogid = 0;
27681195e687SMark J Musante 	int error = 0;
27691195e687SMark J Musante 
2770f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
27711195e687SMark J Musante 
27721195e687SMark J Musante 	/* ensure we have a useable config; mirrors of raidz aren't supported */
2773420dfc95SChris Siden 	if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2774f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
27751195e687SMark J Musante 		return;
27761195e687SMark J Musante 	}
27771195e687SMark J Musante 
27781195e687SMark J Musante 	/* clean up the old pool, if any */
27791195e687SMark J Musante 	(void) spa_destroy("splitp");
27801195e687SMark J Musante 
27811195e687SMark J Musante 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
27821195e687SMark J Musante 
27831195e687SMark J Musante 	/* generate a config from the existing config */
278498295d61SMark J Musante 	mutex_enter(&spa->spa_props_lock);
27851195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
27861195e687SMark J Musante 	    &tree) == 0);
278798295d61SMark J Musante 	mutex_exit(&spa->spa_props_lock);
278898295d61SMark J Musante 
27891195e687SMark J Musante 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
27901195e687SMark J Musante 	    &children) == 0);
27911195e687SMark J Musante 
27921195e687SMark J Musante 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
27931195e687SMark J Musante 	for (c = 0; c < children; c++) {
27941195e687SMark J Musante 		vdev_t *tvd = rvd->vdev_child[c];
27951195e687SMark J Musante 		nvlist_t **mchild;
27961195e687SMark J Musante 		uint_t mchildren;
27971195e687SMark J Musante 
27981195e687SMark J Musante 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
27991195e687SMark J Musante 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
28001195e687SMark J Musante 			    0) == 0);
28011195e687SMark J Musante 			VERIFY(nvlist_add_string(schild[schildren],
28021195e687SMark J Musante 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
28031195e687SMark J Musante 			VERIFY(nvlist_add_uint64(schild[schildren],
28041195e687SMark J Musante 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
28051195e687SMark J Musante 			if (lastlogid == 0)
28061195e687SMark J Musante 				lastlogid = schildren;
28071195e687SMark J Musante 			++schildren;
28081195e687SMark J Musante 			continue;
28091195e687SMark J Musante 		}
28101195e687SMark J Musante 		lastlogid = 0;
28111195e687SMark J Musante 		VERIFY(nvlist_lookup_nvlist_array(child[c],
28121195e687SMark J Musante 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
28131195e687SMark J Musante 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
28141195e687SMark J Musante 	}
28151195e687SMark J Musante 
28161195e687SMark J Musante 	/* OK, create a config that can be used to split */
28171195e687SMark J Musante 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
28181195e687SMark J Musante 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
28191195e687SMark J Musante 	    VDEV_TYPE_ROOT) == 0);
28201195e687SMark J Musante 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
28211195e687SMark J Musante 	    lastlogid != 0 ? lastlogid : schildren) == 0);
28221195e687SMark J Musante 
28231195e687SMark J Musante 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
28241195e687SMark J Musante 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
28251195e687SMark J Musante 
28261195e687SMark J Musante 	for (c = 0; c < schildren; c++)
28271195e687SMark J Musante 		nvlist_free(schild[c]);
28281195e687SMark J Musante 	free(schild);
28291195e687SMark J Musante 	nvlist_free(split);
28301195e687SMark J Musante 
28311195e687SMark J Musante 	spa_config_exit(spa, SCL_VDEV, FTAG);
28321195e687SMark J Musante 
2833f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
28341195e687SMark J Musante 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2835f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
28361195e687SMark J Musante 
28371195e687SMark J Musante 	nvlist_free(config);
28381195e687SMark J Musante 
28391195e687SMark J Musante 	if (error == 0) {
28401195e687SMark J Musante 		(void) printf("successful split - results:\n");
28411195e687SMark J Musante 		mutex_enter(&spa_namespace_lock);
28421195e687SMark J Musante 		show_pool_stats(spa);
28431195e687SMark J Musante 		show_pool_stats(spa_lookup("splitp"));
28441195e687SMark J Musante 		mutex_exit(&spa_namespace_lock);
28451195e687SMark J Musante 		++zs->zs_splits;
28461195e687SMark J Musante 		--zs->zs_mirrors;
28471195e687SMark J Musante 	}
2848f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
28491195e687SMark J Musante }
28501195e687SMark J Musante 
2851fa9e4066Sahrens /*
2852fa9e4066Sahrens  * Verify that we can attach and detach devices.
2853fa9e4066Sahrens  */
2854b24ab676SJeff Bonwick /* ARGSUSED */
2855fa9e4066Sahrens void
2856b24ab676SJeff Bonwick ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2857fa9e4066Sahrens {
2858b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
2859420dfc95SChris Siden 	spa_t *spa = ztest_spa;
286031157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
2861fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2862ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
2863e14bb325SJeff Bonwick 	nvlist_t *root;
28641195e687SMark J Musante 	uint64_t leaves;
2865fa9e4066Sahrens 	uint64_t leaf, top;
2866ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
28678ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
2868b4952e17SGeorge Wilson 	uint64_t oldsize, newsize;
2869ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2870fa9e4066Sahrens 	int replacing;
28719af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
287231157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
287331157203SJeff Bonwick 	int oldvd_is_log;
2874fa9e4066Sahrens 	int error, expected_error;
2875fa9e4066Sahrens 
2876f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
2877420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2878fa9e4066Sahrens 
28795cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
28805cabbc6bSPrashanth Sreenivasa 
28815cabbc6bSPrashanth Sreenivasa 	/*
28825cabbc6bSPrashanth Sreenivasa 	 * If a vdev is in the process of being removed, its removal may
28835cabbc6bSPrashanth Sreenivasa 	 * finish while we are in progress, leading to an unexpected error
28845cabbc6bSPrashanth Sreenivasa 	 * value.  Don't bother trying to attach while we are in the middle
28855cabbc6bSPrashanth Sreenivasa 	 * of removal.
28865cabbc6bSPrashanth Sreenivasa 	 */
28873a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
28885cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
2889f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
28905cabbc6bSPrashanth Sreenivasa 		return;
28915cabbc6bSPrashanth Sreenivasa 	}
2892fa9e4066Sahrens 
2893fa9e4066Sahrens 	/*
2894fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
2895fa9e4066Sahrens 	 */
2896fa9e4066Sahrens 	replacing = ztest_random(2);
2897fa9e4066Sahrens 
2898fa9e4066Sahrens 	/*
2899fa9e4066Sahrens 	 * Pick a random top-level vdev.
2900fa9e4066Sahrens 	 */
2901b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
2902fa9e4066Sahrens 
2903fa9e4066Sahrens 	/*
2904fa9e4066Sahrens 	 * Pick a random leaf within it.
2905fa9e4066Sahrens 	 */
2906d41c4376SMark J Musante 	leaf = ztest_random(leaves);
2907fa9e4066Sahrens 
2908fa9e4066Sahrens 	/*
290931157203SJeff Bonwick 	 * Locate this vdev.
2910fa9e4066Sahrens 	 */
291131157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
29121195e687SMark J Musante 	if (zs->zs_mirrors >= 1) {
29138ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
29141195e687SMark J Musante 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2915420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
29168ad4d6ddSJeff Bonwick 	}
2917420dfc95SChris Siden 	if (ztest_opts.zo_raidz > 1) {
29188ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2919420dfc95SChris Siden 		ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2920420dfc95SChris Siden 		oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
29218ad4d6ddSJeff Bonwick 	}
2922fa9e4066Sahrens 
2923fa9e4066Sahrens 	/*
292431157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
292531157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
2926fa9e4066Sahrens 	 */
292731157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
29289af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
29298ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
29308ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
293131157203SJeff Bonwick 	}
2932fa9e4066Sahrens 
293331157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
2934573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
293531157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
293631157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
2937ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
29388ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
2939fa9e4066Sahrens 
29409af0a4dfSJeff Bonwick 	/*
29419af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
29429af0a4dfSJeff Bonwick 	 */
29439af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
29445cabbc6bSPrashanth Sreenivasa 		spa_config_exit(spa, SCL_ALL, FTAG);
29458ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
29468ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
294786714001SSerapheim Dimitropoulos 		    error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
294886714001SSerapheim Dimitropoulos 		    error != ZFS_ERR_DISCARDING_CHECKPOINT)
29498ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
2950f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
29519af0a4dfSJeff Bonwick 		return;
29529af0a4dfSJeff Bonwick 	}
29539af0a4dfSJeff Bonwick 
2954fa9e4066Sahrens 	/*
295531157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
295631157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2957fa9e4066Sahrens 	 */
295831157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
295931157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
296031157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
296131157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
296231157203SJeff Bonwick 	} else {
296331157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2964420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
2965420dfc95SChris Siden 		    top * leaves + leaf);
296631157203SJeff Bonwick 		if (ztest_random(2) == 0)
296731157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
296831157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
296931157203SJeff Bonwick 	}
297031157203SJeff Bonwick 
297131157203SJeff Bonwick 	if (newvd) {
29725cabbc6bSPrashanth Sreenivasa 		/*
29735cabbc6bSPrashanth Sreenivasa 		 * Reopen to ensure the vdev's asize field isn't stale.
29745cabbc6bSPrashanth Sreenivasa 		 */
29755cabbc6bSPrashanth Sreenivasa 		vdev_reopen(newvd);
2976573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
297731157203SJeff Bonwick 	} else {
297831157203SJeff Bonwick 		/*
297931157203SJeff Bonwick 		 * Make newsize a little bigger or smaller than oldsize.
298031157203SJeff Bonwick 		 * If it's smaller, the attach should fail.
298131157203SJeff Bonwick 		 * If it's larger, and we're doing a replace,
298231157203SJeff Bonwick 		 * we should get dynamic LUN growth when we're done.
298331157203SJeff Bonwick 		 */
298431157203SJeff Bonwick 		newsize = 10 * oldsize / (9 + ztest_random(3));
298531157203SJeff Bonwick 	}
2986fa9e4066Sahrens 
2987fa9e4066Sahrens 	/*
2988fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2989fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2990fa9e4066Sahrens 	 *
2991ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2992fa9e4066Sahrens 	 *
2993ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2994fa9e4066Sahrens 	 */
299531157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
299631157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
299731157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
299831157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
299931157203SJeff Bonwick 		expected_error = ENOTSUP;
300031157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
3001fa9e4066Sahrens 		expected_error = ENOTSUP;
300231157203SJeff Bonwick 	else if (newvd == oldvd)
300331157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
300431157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
300531157203SJeff Bonwick 		expected_error = EBUSY;
3006ea8dc4b6Seschrock 	else if (newsize < oldsize)
3007fa9e4066Sahrens 		expected_error = EOVERFLOW;
3008ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
3009ecc2d604Sbonwick 		expected_error = EDOM;
3010fa9e4066Sahrens 	else
3011fa9e4066Sahrens 		expected_error = 0;
3012fa9e4066Sahrens 
30135cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_ALL, FTAG);
3014fa9e4066Sahrens 
3015fa9e4066Sahrens 	/*
3016ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
3017fa9e4066Sahrens 	 */
301825345e46SGeorge Wilson 	root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3019e14bb325SJeff Bonwick 	    ashift, 0, 0, 0, 1);
3020fa9e4066Sahrens 
302131157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
3022fa9e4066Sahrens 
3023fa9e4066Sahrens 	nvlist_free(root);
3024fa9e4066Sahrens 
3025fa9e4066Sahrens 	/*
3026fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
3027fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
3028fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
3029fa9e4066Sahrens 	 */
3030fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
3031fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
3032fa9e4066Sahrens 		expected_error = error;
3033fa9e4066Sahrens 
3034f0aa80d4Sbonwick 	/*
3035f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
3036f0aa80d4Sbonwick 	 */
3037088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
3038f0aa80d4Sbonwick 		expected_error = error;
3039f0aa80d4Sbonwick 
304086714001SSerapheim Dimitropoulos 	if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
304186714001SSerapheim Dimitropoulos 	    error == ZFS_ERR_DISCARDING_CHECKPOINT)
304286714001SSerapheim Dimitropoulos 		expected_error = error;
304386714001SSerapheim Dimitropoulos 
3044088f3894Sahrens 	/* XXX workaround 6690467 */
3045088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
3046088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
3047088f3894Sahrens 		    "returned %d, expected %d",
3048b4952e17SGeorge Wilson 		    oldpath, oldsize, newpath,
3049b4952e17SGeorge Wilson 		    newsize, replacing, error, expected_error);
3050fa9e4066Sahrens 	}
3051fa9e4066Sahrens 
3052f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
3053fa9e4066Sahrens }
3054fa9e4066Sahrens 
30555cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
30565cabbc6bSPrashanth Sreenivasa void
30575cabbc6bSPrashanth Sreenivasa ztest_device_removal(ztest_ds_t *zd, uint64_t id)
30585cabbc6bSPrashanth Sreenivasa {
30595cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ztest_spa;
30605cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
30615cabbc6bSPrashanth Sreenivasa 	uint64_t guid;
30623a4b1be9SMatthew Ahrens 	int error;
30635cabbc6bSPrashanth Sreenivasa 
3064f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
30655cabbc6bSPrashanth Sreenivasa 
30663a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
30673a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
30683a4b1be9SMatthew Ahrens 		return;
30693a4b1be9SMatthew Ahrens 	}
30703a4b1be9SMatthew Ahrens 
30713a4b1be9SMatthew Ahrens 	/*
30723a4b1be9SMatthew Ahrens 	 * Remove a random top-level vdev and wait for removal to finish.
30733a4b1be9SMatthew Ahrens 	 */
30745cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
30755cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
30765cabbc6bSPrashanth Sreenivasa 	guid = vd->vdev_guid;
30775cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
30785cabbc6bSPrashanth Sreenivasa 
30793a4b1be9SMatthew Ahrens 	error = spa_vdev_remove(spa, guid, B_FALSE);
30803a4b1be9SMatthew Ahrens 	if (error == 0) {
30813a4b1be9SMatthew Ahrens 		ztest_device_removal_active = B_TRUE;
30823a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
30833a4b1be9SMatthew Ahrens 
30843a4b1be9SMatthew Ahrens 		while (spa->spa_vdev_removal != NULL)
30853a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
30863a4b1be9SMatthew Ahrens 	} else {
30873a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
30883a4b1be9SMatthew Ahrens 		return;
30893a4b1be9SMatthew Ahrens 	}
30905cabbc6bSPrashanth Sreenivasa 
30913a4b1be9SMatthew Ahrens 	/*
30923a4b1be9SMatthew Ahrens 	 * The pool needs to be scrubbed after completing device removal.
30933a4b1be9SMatthew Ahrens 	 * Failure to do so may result in checksum errors due to the
30943a4b1be9SMatthew Ahrens 	 * strategy employed by ztest_fault_inject() when selecting which
30953a4b1be9SMatthew Ahrens 	 * offset are redundant and can be damaged.
30963a4b1be9SMatthew Ahrens 	 */
30973a4b1be9SMatthew Ahrens 	error = spa_scan(spa, POOL_SCAN_SCRUB);
30983a4b1be9SMatthew Ahrens 	if (error == 0) {
30993a4b1be9SMatthew Ahrens 		while (dsl_scan_scrubbing(spa_get_dsl(spa)))
31003a4b1be9SMatthew Ahrens 			txg_wait_synced(spa_get_dsl(spa), 0);
31013a4b1be9SMatthew Ahrens 	}
31023a4b1be9SMatthew Ahrens 
31033a4b1be9SMatthew Ahrens 	mutex_enter(&ztest_vdev_lock);
31043a4b1be9SMatthew Ahrens 	ztest_device_removal_active = B_FALSE;
3105f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
31065cabbc6bSPrashanth Sreenivasa }
31075cabbc6bSPrashanth Sreenivasa 
3108573ca77eSGeorge Wilson /*
3109573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
3110573ca77eSGeorge Wilson  */
3111573ca77eSGeorge Wilson vdev_t *
3112573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
3113573ca77eSGeorge Wilson {
3114573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3115573ca77eSGeorge Wilson 	size_t *newsize = arg;
3116573ca77eSGeorge Wilson 	size_t fsize;
3117573ca77eSGeorge Wilson 	int fd;
3118573ca77eSGeorge Wilson 
3119573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3120573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3121573ca77eSGeorge Wilson 
3122573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3123573ca77eSGeorge Wilson 		return (vd);
3124573ca77eSGeorge Wilson 
3125573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
3126573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
3127573ca77eSGeorge Wilson 
3128420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3129573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
3130573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3131573ca77eSGeorge Wilson 	}
3132573ca77eSGeorge Wilson 	(void) close(fd);
3133573ca77eSGeorge Wilson 	return (NULL);
3134573ca77eSGeorge Wilson }
3135573ca77eSGeorge Wilson 
3136573ca77eSGeorge Wilson /*
3137573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
3138573ca77eSGeorge Wilson  */
3139573ca77eSGeorge Wilson /* ARGSUSED */
3140573ca77eSGeorge Wilson vdev_t *
3141573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
3142573ca77eSGeorge Wilson {
3143573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
3144573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
3145573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
31468f18d1faSGeorge Wilson 	uint64_t generation = spa->spa_config_generation + 1;
3147095bcd66SGeorge Wilson 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3148095bcd66SGeorge Wilson 	int error;
3149573ca77eSGeorge Wilson 
3150573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3151573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3152573ca77eSGeorge Wilson 
3153573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
3154573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3155095bcd66SGeorge Wilson 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3156573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3157573ca77eSGeorge Wilson 
3158095bcd66SGeorge Wilson 	/*
3159095bcd66SGeorge Wilson 	 * If vdev_online returned an error or the underlying vdev_open
3160095bcd66SGeorge Wilson 	 * failed then we abort the expand. The only way to know that
3161095bcd66SGeorge Wilson 	 * vdev_open fails is by checking the returned newstate.
3162095bcd66SGeorge Wilson 	 */
3163095bcd66SGeorge Wilson 	if (error || newstate != VDEV_STATE_HEALTHY) {
3164420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3165095bcd66SGeorge Wilson 			(void) printf("Unable to expand vdev, state %llu, "
3166095bcd66SGeorge Wilson 			    "error %d\n", (u_longlong_t)newstate, error);
3167095bcd66SGeorge Wilson 		}
3168095bcd66SGeorge Wilson 		return (vd);
3169095bcd66SGeorge Wilson 	}
3170095bcd66SGeorge Wilson 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3171095bcd66SGeorge Wilson 
3172573ca77eSGeorge Wilson 	/*
3173573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
3174573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
3175573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
3176573ca77eSGeorge Wilson 	 * trying to online it.
3177573ca77eSGeorge Wilson 	 */
31788f18d1faSGeorge Wilson 	if (generation != spa->spa_config_generation) {
3179420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
31808f18d1faSGeorge Wilson 			(void) printf("vdev configuration has changed, "
31818f18d1faSGeorge Wilson 			    "guid %llu, state %llu, expected gen %llu, "
3182b24ab676SJeff Bonwick 			    "got gen %llu\n",
3183b24ab676SJeff Bonwick 			    (u_longlong_t)guid,
31848f18d1faSGeorge Wilson 			    (u_longlong_t)tvd->vdev_state,
31858f18d1faSGeorge Wilson 			    (u_longlong_t)generation,
31868f18d1faSGeorge Wilson 			    (u_longlong_t)spa->spa_config_generation);
3187573ca77eSGeorge Wilson 		}
3188573ca77eSGeorge Wilson 		return (vd);
3189573ca77eSGeorge Wilson 	}
3190573ca77eSGeorge Wilson 	return (NULL);
3191573ca77eSGeorge Wilson }
3192573ca77eSGeorge Wilson 
3193573ca77eSGeorge Wilson /*
3194573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
3195573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
3196573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
3197573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
3198573ca77eSGeorge Wilson  * leaf vdev we encounter.
3199573ca77eSGeorge Wilson  */
3200573ca77eSGeorge Wilson vdev_t *
3201573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3202573ca77eSGeorge Wilson {
3203573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
3204573ca77eSGeorge Wilson 		if (func == NULL)
3205573ca77eSGeorge Wilson 			return (vd);
3206573ca77eSGeorge Wilson 		else
3207573ca77eSGeorge Wilson 			return (func(vd, arg));
3208573ca77eSGeorge Wilson 	}
3209573ca77eSGeorge Wilson 
3210573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
3211573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
3212573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3213573ca77eSGeorge Wilson 			return (cvd);
3214573ca77eSGeorge Wilson 	}
3215573ca77eSGeorge Wilson 	return (NULL);
3216573ca77eSGeorge Wilson }
3217573ca77eSGeorge Wilson 
3218fa9e4066Sahrens /*
3219fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
3220fa9e4066Sahrens  */
3221b24ab676SJeff Bonwick /* ARGSUSED */
3222fa9e4066Sahrens void
3223b24ab676SJeff Bonwick ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3224fa9e4066Sahrens {
3225420dfc95SChris Siden 	spa_t *spa = ztest_spa;
3226b24ab676SJeff Bonwick 	vdev_t *vd, *tvd;
3227b24ab676SJeff Bonwick 	metaslab_class_t *mc;
3228b24ab676SJeff Bonwick 	metaslab_group_t *mg;
3229573ca77eSGeorge Wilson 	size_t psize, newsize;
3230b24ab676SJeff Bonwick 	uint64_t top;
3231b24ab676SJeff Bonwick 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3232fa9e4066Sahrens 
323386714001SSerapheim Dimitropoulos 	mutex_enter(&ztest_checkpoint_lock);
3234f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
3235573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3236573ca77eSGeorge Wilson 
32375cabbc6bSPrashanth Sreenivasa 	/*
32385cabbc6bSPrashanth Sreenivasa 	 * If there is a vdev removal in progress, it could complete while
32395cabbc6bSPrashanth Sreenivasa 	 * we are running, in which case we would not be able to verify
32405cabbc6bSPrashanth Sreenivasa 	 * that the metaslab_class space increased (because it decreases
32415cabbc6bSPrashanth Sreenivasa 	 * when the device removal completes).
32425cabbc6bSPrashanth Sreenivasa 	 */
32433a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
324486714001SSerapheim Dimitropoulos 		spa_config_exit(spa, SCL_STATE, spa);
3245f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
324686714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
32475cabbc6bSPrashanth Sreenivasa 		return;
32485cabbc6bSPrashanth Sreenivasa 	}
32495cabbc6bSPrashanth Sreenivasa 
3250b24ab676SJeff Bonwick 	top = ztest_random_vdev_top(spa, B_TRUE);
3251573ca77eSGeorge Wilson 
3252b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3253b24ab676SJeff Bonwick 	mg = tvd->vdev_mg;
3254b24ab676SJeff Bonwick 	mc = mg->mg_class;
3255b24ab676SJeff Bonwick 	old_ms_count = tvd->vdev_ms_count;
3256b24ab676SJeff Bonwick 	old_class_space = metaslab_class_get_space(mc);
3257fa9e4066Sahrens 
3258fa9e4066Sahrens 	/*
3259573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
3260573ca77eSGeorge Wilson 	 * our top-level device.
3261fa9e4066Sahrens 	 */
3262573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
3263573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
3264573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
3265fa9e4066Sahrens 
3266573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
3267fa9e4066Sahrens 
3268573ca77eSGeorge Wilson 	/*
32698f18d1faSGeorge Wilson 	 * We only try to expand the vdev if it's healthy, less than 4x its
32708f18d1faSGeorge Wilson 	 * original size, and it has a valid psize.
3271573ca77eSGeorge Wilson 	 */
32728f18d1faSGeorge Wilson 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3273420dfc95SChris Siden 	    psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3274573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
3275f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
327686714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3277573ca77eSGeorge Wilson 		return;
3278573ca77eSGeorge Wilson 	}
3279573ca77eSGeorge Wilson 	ASSERT(psize > 0);
3280573ca77eSGeorge Wilson 	newsize = psize + psize / 8;
3281573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
3282fa9e4066Sahrens 
3283420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6) {
3284b24ab676SJeff Bonwick 		(void) printf("Expanding LUN %s from %lu to %lu\n",
3285573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3286573ca77eSGeorge Wilson 	}
3287573ca77eSGeorge Wilson 
3288573ca77eSGeorge Wilson 	/*
3289573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
3290573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
3291573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
3292573ca77eSGeorge Wilson 	 */
3293573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3294573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3295573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
3296420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3297b24ab676SJeff Bonwick 			(void) printf("Could not expand LUN because "
3298b24ab676SJeff Bonwick 			    "the vdev configuration changed.\n");
3299b24ab676SJeff Bonwick 		}
3300b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3301f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
330286714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3303b24ab676SJeff Bonwick 		return;
3304b24ab676SJeff Bonwick 	}
3305b24ab676SJeff Bonwick 
3306b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, spa);
3307b24ab676SJeff Bonwick 
3308b24ab676SJeff Bonwick 	/*
3309b24ab676SJeff Bonwick 	 * Expanding the LUN will update the config asynchronously,
3310b24ab676SJeff Bonwick 	 * thus we must wait for the async thread to complete any
3311b24ab676SJeff Bonwick 	 * pending tasks before proceeding.
3312b24ab676SJeff Bonwick 	 */
3313b24ab676SJeff Bonwick 	for (;;) {
3314b24ab676SJeff Bonwick 		boolean_t done;
3315b24ab676SJeff Bonwick 		mutex_enter(&spa->spa_async_lock);
3316b24ab676SJeff Bonwick 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3317b24ab676SJeff Bonwick 		mutex_exit(&spa->spa_async_lock);
3318b24ab676SJeff Bonwick 		if (done)
3319b24ab676SJeff Bonwick 			break;
3320b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
3321b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
3322b24ab676SJeff Bonwick 	}
3323b24ab676SJeff Bonwick 
3324b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3325b24ab676SJeff Bonwick 
3326b24ab676SJeff Bonwick 	tvd = spa->spa_root_vdev->vdev_child[top];
3327b24ab676SJeff Bonwick 	new_ms_count = tvd->vdev_ms_count;
3328b24ab676SJeff Bonwick 	new_class_space = metaslab_class_get_space(mc);
3329b24ab676SJeff Bonwick 
3330b24ab676SJeff Bonwick 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3331420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 5) {
3332b24ab676SJeff Bonwick 			(void) printf("Could not verify LUN expansion due to "
3333b24ab676SJeff Bonwick 			    "intervening vdev offline or remove.\n");
3334fa9e4066Sahrens 		}
3335b24ab676SJeff Bonwick 		spa_config_exit(spa, SCL_STATE, spa);
3336f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
333786714001SSerapheim Dimitropoulos 		mutex_exit(&ztest_checkpoint_lock);
3338573ca77eSGeorge Wilson 		return;
3339fa9e4066Sahrens 	}
3340fa9e4066Sahrens 
3341573ca77eSGeorge Wilson 	/*
3342b24ab676SJeff Bonwick 	 * Make sure we were able to grow the vdev.
3343573ca77eSGeorge Wilson 	 */
33445cabbc6bSPrashanth Sreenivasa 	if (new_ms_count <= old_ms_count) {
33455cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3346b24ab676SJeff Bonwick 		    old_ms_count, new_ms_count);
33475cabbc6bSPrashanth Sreenivasa 	}
3348573ca77eSGeorge Wilson 
3349573ca77eSGeorge Wilson 	/*
3350573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
3351573ca77eSGeorge Wilson 	 */
33525cabbc6bSPrashanth Sreenivasa 	if (new_class_space <= old_class_space) {
33535cabbc6bSPrashanth Sreenivasa 		fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3354b24ab676SJeff Bonwick 		    old_class_space, new_class_space);
33555cabbc6bSPrashanth Sreenivasa 	}
3356b24ab676SJeff Bonwick 
3357420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5) {
33580a055120SJason King 		char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3359573ca77eSGeorge Wilson 
33600a055120SJason King 		nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
33610a055120SJason King 		nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3362573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
3363573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
3364573ca77eSGeorge Wilson 	}
3365b24ab676SJeff Bonwick 
3366573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
3367f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
336886714001SSerapheim Dimitropoulos 	mutex_exit(&ztest_checkpoint_lock);
3369fa9e4066Sahrens }
3370fa9e4066Sahrens 
3371b24ab676SJeff Bonwick /*
3372b24ab676SJeff Bonwick  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3373b24ab676SJeff Bonwick  */
3374fa9e4066Sahrens /* ARGSUSED */
3375fa9e4066Sahrens static void
3376b24ab676SJeff Bonwick ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3377fa9e4066Sahrens {
3378fa9e4066Sahrens 	/*
3379b24ab676SJeff Bonwick 	 * Create the objects common to all ztest datasets.
3380fa9e4066Sahrens 	 */
3381b24ab676SJeff Bonwick 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3382fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3383fa9e4066Sahrens }
3384fa9e4066Sahrens 
338555da60b9SMark J Musante static int
338655da60b9SMark J Musante ztest_dataset_create(char *dsname)
338755da60b9SMark J Musante {
338855da60b9SMark J Musante 	uint64_t zilset = ztest_random(100);
338955da60b9SMark J Musante 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
339055da60b9SMark J Musante 	    ztest_objset_create_cb, NULL);
339155da60b9SMark J Musante 
339255da60b9SMark J Musante 	if (err || zilset < 80)
339355da60b9SMark J Musante 		return (err);
339455da60b9SMark J Musante 
3395420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
3396420dfc95SChris Siden 		(void) printf("Setting dataset %s to sync always\n", dsname);
339755da60b9SMark J Musante 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
339855da60b9SMark J Musante 	    ZFS_SYNC_ALWAYS, B_FALSE));
339955da60b9SMark J Musante }
340055da60b9SMark J Musante 
3401b24ab676SJeff Bonwick /* ARGSUSED */
34021d452cf5Sahrens static int
3403fd136879SMatthew Ahrens ztest_objset_destroy_cb(const char *name, void *arg)
3404fa9e4066Sahrens {
3405fa9e4066Sahrens 	objset_t *os;
3406b24ab676SJeff Bonwick 	dmu_object_info_t doi;
3407fa9e4066Sahrens 	int error;
3408fa9e4066Sahrens 
3409fa9e4066Sahrens 	/*
3410fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
3411fa9e4066Sahrens 	 */
34123b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3413b24ab676SJeff Bonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3414e1930233Sbonwick 	if (error != ENOENT) {
3415e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
3416fb09f5aaSMadhav Suresh 		ASSERT0(error);
3417b24ab676SJeff Bonwick 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3418b24ab676SJeff Bonwick 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3419e1930233Sbonwick 	}
34203b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3421fa9e4066Sahrens 
3422fa9e4066Sahrens 	/*
3423fa9e4066Sahrens 	 * Destroy the dataset.
3424fa9e4066Sahrens 	 */
34253b2aab18SMatthew Ahrens 	if (strchr(name, '@') != NULL) {
3426754998c8SChris Williamson 		VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
34273b2aab18SMatthew Ahrens 	} else {
3428754998c8SChris Williamson 		error = dsl_destroy_head(name);
3429754998c8SChris Williamson 		/* There could be a hold on this dataset */
3430754998c8SChris Williamson 		if (error != EBUSY)
3431754998c8SChris Williamson 			ASSERT0(error);
34323b2aab18SMatthew Ahrens 	}
34331d452cf5Sahrens 	return (0);
3434fa9e4066Sahrens }
3435fa9e4066Sahrens 
3436b24ab676SJeff Bonwick static boolean_t
3437b24ab676SJeff Bonwick ztest_snapshot_create(char *osname, uint64_t id)
3438fa9e4066Sahrens {
34399adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3440b24ab676SJeff Bonwick 	int error;
3441b24ab676SJeff Bonwick 
34423b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3443b24ab676SJeff Bonwick 
34443b2aab18SMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
3445b24ab676SJeff Bonwick 	if (error == ENOSPC) {
3446b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
3447b24ab676SJeff Bonwick 		return (B_FALSE);
3448b24ab676SJeff Bonwick 	}
34493b2aab18SMatthew Ahrens 	if (error != 0 && error != EEXIST) {
34503b2aab18SMatthew Ahrens 		fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
34513b2aab18SMatthew Ahrens 		    snapname, error);
34523b2aab18SMatthew Ahrens 	}
3453b24ab676SJeff Bonwick 	return (B_TRUE);
3454b24ab676SJeff Bonwick }
3455b24ab676SJeff Bonwick 
3456b24ab676SJeff Bonwick static boolean_t
3457b24ab676SJeff Bonwick ztest_snapshot_destroy(char *osname, uint64_t id)
3458b24ab676SJeff Bonwick {
34599adfa60dSMatthew Ahrens 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3460b24ab676SJeff Bonwick 	int error;
3461b24ab676SJeff Bonwick 
34629adfa60dSMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3463b24ab676SJeff Bonwick 	    (u_longlong_t)id);
3464b24ab676SJeff Bonwick 
34653b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snapname, B_FALSE);
3466b24ab676SJeff Bonwick 	if (error != 0 && error != ENOENT)
3467b24ab676SJeff Bonwick 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3468b24ab676SJeff Bonwick 	return (B_TRUE);
3469fa9e4066Sahrens }
3470fa9e4066Sahrens 
3471b24ab676SJeff Bonwick /* ARGSUSED */
3472fa9e4066Sahrens void
3473b24ab676SJeff Bonwick ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3474fa9e4066Sahrens {
3475b24ab676SJeff Bonwick 	ztest_ds_t zdtmp;
3476b24ab676SJeff Bonwick 	int iters;
3477fa9e4066Sahrens 	int error;
3478745cd3c5Smaybee 	objset_t *os, *os2;
34799adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
3480fa9e4066Sahrens 	zilog_t *zilog;
3481fa9e4066Sahrens 
3482f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3483b24ab676SJeff Bonwick 
34849adfa60dSMatthew Ahrens 	(void) snprintf(name, sizeof (name), "%s/temp_%llu",
3485420dfc95SChris Siden 	    ztest_opts.zo_pool, (u_longlong_t)id);
3486fa9e4066Sahrens 
3487fa9e4066Sahrens 	/*
3488fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
3489fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
3490b24ab676SJeff Bonwick 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3491fa9e4066Sahrens 	 */
3492fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
3493503ad85cSMatthew Ahrens 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3494420dfc95SChris Siden 		ztest_zd_init(&zdtmp, NULL, os);
3495b24ab676SJeff Bonwick 		zil_replay(os, &zdtmp, ztest_replay_vector);
3496b24ab676SJeff Bonwick 		ztest_zd_fini(&zdtmp);
3497503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
3498fa9e4066Sahrens 	}
3499fa9e4066Sahrens 
3500fa9e4066Sahrens 	/*
3501fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
3502fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
3503fa9e4066Sahrens 	 * and all of its snapshots.
3504fa9e4066Sahrens 	 */
3505b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
35060b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3507fa9e4066Sahrens 
3508fa9e4066Sahrens 	/*
3509fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
3510fa9e4066Sahrens 	 */
35113b2aab18SMatthew Ahrens 	VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
35123b2aab18SMatthew Ahrens 	    FTAG, &os));
3513fa9e4066Sahrens 
3514fa9e4066Sahrens 	/*
3515fa9e4066Sahrens 	 * Verify that we can create a new dataset.
3516fa9e4066Sahrens 	 */
351755da60b9SMark J Musante 	error = ztest_dataset_create(name);
3518fa9e4066Sahrens 	if (error) {
3519fa9e4066Sahrens 		if (error == ENOSPC) {
3520b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3521f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
3522fa9e4066Sahrens 			return;
3523fa9e4066Sahrens 		}
3524fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
3525fa9e4066Sahrens 	}
3526fa9e4066Sahrens 
35273b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3528b24ab676SJeff Bonwick 
3529420dfc95SChris Siden 	ztest_zd_init(&zdtmp, NULL, os);
3530fa9e4066Sahrens 
3531fa9e4066Sahrens 	/*
3532fa9e4066Sahrens 	 * Open the intent log for it.
3533fa9e4066Sahrens 	 */
3534b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
3535fa9e4066Sahrens 
3536fa9e4066Sahrens 	/*
3537b24ab676SJeff Bonwick 	 * Put some objects in there, do a little I/O to them,
3538b24ab676SJeff Bonwick 	 * and randomly take a couple of snapshots along the way.
3539fa9e4066Sahrens 	 */
3540b24ab676SJeff Bonwick 	iters = ztest_random(5);
3541b24ab676SJeff Bonwick 	for (int i = 0; i < iters; i++) {
3542b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(&zdtmp, id);
3543b24ab676SJeff Bonwick 		if (ztest_random(iters) == 0)
3544b24ab676SJeff Bonwick 			(void) ztest_snapshot_create(name, i);
3545fa9e4066Sahrens 	}
3546fa9e4066Sahrens 
3547fa9e4066Sahrens 	/*
3548fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
3549fa9e4066Sahrens 	 */
3550b24ab676SJeff Bonwick 	VERIFY3U(EEXIST, ==,
3551b24ab676SJeff Bonwick 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3552fa9e4066Sahrens 
3553fa9e4066Sahrens 	/*
3554503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
3555745cd3c5Smaybee 	 */
3556b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3557503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
3558503ad85cSMatthew Ahrens 
3559503ad85cSMatthew Ahrens 	/*
3560b24ab676SJeff Bonwick 	 * Verify that we cannot own an objset that is already owned.
3561503ad85cSMatthew Ahrens 	 */
3562b24ab676SJeff Bonwick 	VERIFY3U(EBUSY, ==,
3563b24ab676SJeff Bonwick 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3564fa9e4066Sahrens 
3565fa9e4066Sahrens 	zil_close(zilog);
3566503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
3567b24ab676SJeff Bonwick 	ztest_zd_fini(&zdtmp);
3568fa9e4066Sahrens 
3569f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
3570fa9e4066Sahrens }
3571fa9e4066Sahrens 
3572fa9e4066Sahrens /*
3573fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3574fa9e4066Sahrens  */
3575fa9e4066Sahrens void
3576b24ab676SJeff Bonwick ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3577fa9e4066Sahrens {
3578f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
3579b24ab676SJeff Bonwick 	(void) ztest_snapshot_destroy(zd->zd_name, id);
3580b24ab676SJeff Bonwick 	(void) ztest_snapshot_create(zd->zd_name, id);
3581f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
35824f5064b7SMark J Musante }
35834f5064b7SMark J Musante 
3584718d718aSGeorge Wilson /*
3585718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
3586718d718aSGeorge Wilson  */
3587718d718aSGeorge Wilson void
3588b24ab676SJeff Bonwick ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3589718d718aSGeorge Wilson {
35909adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
35919adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
35929adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
35939adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
35949adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3595718d718aSGeorge Wilson 	int error;
3596718d718aSGeorge Wilson 
35979adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
35989adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
35999adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
36009adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
36019adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
36029adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
36039adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
36049adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
36059adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
36069adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
3607718d718aSGeorge Wilson 
36083b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone2name);
3609718d718aSGeorge Wilson 	if (error && error != ENOENT)
36103b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
36113b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap3name, B_FALSE);
3612718d718aSGeorge Wilson 	if (error && error != ENOENT)
36133b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
36143b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap2name, B_FALSE);
3615718d718aSGeorge Wilson 	if (error && error != ENOENT)
36163b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
36173b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clone1name);
3618718d718aSGeorge Wilson 	if (error && error != ENOENT)
36193b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
36203b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(snap1name, B_FALSE);
3621718d718aSGeorge Wilson 	if (error && error != ENOENT)
36223b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3623718d718aSGeorge Wilson }
3624718d718aSGeorge Wilson 
36254f5064b7SMark J Musante /*
36264f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
36274f5064b7SMark J Musante  */
36284f5064b7SMark J Musante void
3629b24ab676SJeff Bonwick ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
36304f5064b7SMark J Musante {
36313b2aab18SMatthew Ahrens 	objset_t *os;
36329adfa60dSMatthew Ahrens 	char snap1name[ZFS_MAX_DATASET_NAME_LEN];
36339adfa60dSMatthew Ahrens 	char clone1name[ZFS_MAX_DATASET_NAME_LEN];
36349adfa60dSMatthew Ahrens 	char snap2name[ZFS_MAX_DATASET_NAME_LEN];
36359adfa60dSMatthew Ahrens 	char clone2name[ZFS_MAX_DATASET_NAME_LEN];
36369adfa60dSMatthew Ahrens 	char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3637b24ab676SJeff Bonwick 	char *osname = zd->zd_name;
3638b24ab676SJeff Bonwick 	int error;
36394f5064b7SMark J Musante 
3640f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
36414f5064b7SMark J Musante 
3642b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
3643718d718aSGeorge Wilson 
36449adfa60dSMatthew Ahrens 	(void) snprintf(snap1name, sizeof (snap1name),
36459adfa60dSMatthew Ahrens 	    "%s@s1_%llu", osname, id);
36469adfa60dSMatthew Ahrens 	(void) snprintf(clone1name, sizeof (clone1name),
36479adfa60dSMatthew Ahrens 	    "%s/c1_%llu", osname, id);
36489adfa60dSMatthew Ahrens 	(void) snprintf(snap2name, sizeof (snap2name),
36499adfa60dSMatthew Ahrens 	    "%s@s2_%llu", clone1name, id);
36509adfa60dSMatthew Ahrens 	(void) snprintf(clone2name, sizeof (clone2name),
36519adfa60dSMatthew Ahrens 	    "%s/c2_%llu", osname, id);
36529adfa60dSMatthew Ahrens 	(void) snprintf(snap3name, sizeof (snap3name),
36539adfa60dSMatthew Ahrens 	    "%s@s3_%llu", clone1name, id);
365405312e2cSMark J Musante 
36554445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3656b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3657b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3658b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3659b2d22b66SGeorge Wilson 			goto out;
3660b2d22b66SGeorge Wilson 		}
3661b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3662b2d22b66SGeorge Wilson 	}
36634f5064b7SMark J Musante 
36643b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone1name, snap1name);
3665b2d22b66SGeorge Wilson 	if (error) {
3666b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3667b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3668b2d22b66SGeorge Wilson 			goto out;
3669b2d22b66SGeorge Wilson 		}
3670b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3671b2d22b66SGeorge Wilson 	}
36724f5064b7SMark J Musante 
36734445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3674b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3675b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3676b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3677b2d22b66SGeorge Wilson 			goto out;
3678b2d22b66SGeorge Wilson 		}
3679b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3680b2d22b66SGeorge Wilson 	}
36814f5064b7SMark J Musante 
36824445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3683b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
3684b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3685b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3686b2d22b66SGeorge Wilson 			goto out;
3687b2d22b66SGeorge Wilson 		}
3688b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3689b2d22b66SGeorge Wilson 	}
36904f5064b7SMark J Musante 
36913b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clone2name, snap3name);
3692b2d22b66SGeorge Wilson 	if (error) {
3693b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
3694b24ab676SJeff Bonwick 			ztest_record_enospc(FTAG);
3695b2d22b66SGeorge Wilson 			goto out;
3696b2d22b66SGeorge Wilson 		}
3697b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3698b2d22b66SGeorge Wilson 	}
36994f5064b7SMark J Musante 
37003b2aab18SMatthew Ahrens 	error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
37014f5064b7SMark J Musante 	if (error)
37023b2aab18SMatthew Ahrens 		fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3703681d9761SEric Taylor 	error = dsl_dataset_promote(clone2name, NULL);
37045d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
37055d7b4d43SMatthew Ahrens 		dmu_objset_disown(os, FTAG);
37065d7b4d43SMatthew Ahrens 		ztest_record_enospc(FTAG);
37075d7b4d43SMatthew Ahrens 		goto out;
37085d7b4d43SMatthew Ahrens 	}
37094f5064b7SMark J Musante 	if (error != EBUSY)
37104f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
37114f5064b7SMark J Musante 		    error);
37123b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
37134f5064b7SMark J Musante 
3714b2d22b66SGeorge Wilson out:
3715b24ab676SJeff Bonwick 	ztest_dsl_dataset_cleanup(osname, id);
37164f5064b7SMark J Musante 
3717f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
3718fa9e4066Sahrens }
3719fa9e4066Sahrens 
3720fa9e4066Sahrens /*
3721fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3722fa9e4066Sahrens  */
3723fa9e4066Sahrens void
3724b24ab676SJeff Bonwick ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3725fa9e4066Sahrens {
3726b24ab676SJeff Bonwick 	ztest_od_t od[4];
3727b24ab676SJeff Bonwick 	int batchsize = sizeof (od) / sizeof (od[0]);
3728fa9e4066Sahrens 
3729b24ab676SJeff Bonwick 	for (int b = 0; b < batchsize; b++)
3730b24ab676SJeff Bonwick 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3731fa9e4066Sahrens 
3732fa9e4066Sahrens 	/*
3733b24ab676SJeff Bonwick 	 * Destroy the previous batch of objects, create a new batch,
3734b24ab676SJeff Bonwick 	 * and do some I/O on the new objects.
3735fa9e4066Sahrens 	 */
3736b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3737b24ab676SJeff Bonwick 		return;
3738fa9e4066Sahrens 
3739b24ab676SJeff Bonwick 	while (ztest_random(4 * batchsize) != 0)
3740b24ab676SJeff Bonwick 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
3741b24ab676SJeff Bonwick 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3742fa9e4066Sahrens }
3743fa9e4066Sahrens 
3744fa9e4066Sahrens /*
3745fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
3746fa9e4066Sahrens  */
3747fa9e4066Sahrens void
3748b24ab676SJeff Bonwick ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3749fa9e4066Sahrens {
3750b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
3751b24ab676SJeff Bonwick 	ztest_od_t od[2];
3752fa9e4066Sahrens 	dmu_tx_t *tx;
3753fa9e4066Sahrens 	int i, freeit, error;
3754fa9e4066Sahrens 	uint64_t n, s, txg;
3755fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3756b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3757b24ab676SJeff Bonwick 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3758fa9e4066Sahrens 	uint64_t regions = 997;
3759fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
3760fa9e4066Sahrens 	uint64_t width = 40;
3761fa9e4066Sahrens 	int free_percent = 5;
3762fa9e4066Sahrens 
3763fa9e4066Sahrens 	/*
3764fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3765fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3766fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3767fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3768fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3769fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
3770fa9e4066Sahrens 	 *
3771fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3772fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
3773fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
3774fa9e4066Sahrens 	 *
3775fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3776fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
3777fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3778fa9e4066Sahrens 	 *
3779fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
3780fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
3781fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3782fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3783fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
3784fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
3785fa9e4066Sahrens 	 */
3786fa9e4066Sahrens 
3787fa9e4066Sahrens 	/*
3788fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3789fa9e4066Sahrens 	 */
3790b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3791b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3792fa9e4066Sahrens 
3793b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3794b24ab676SJeff Bonwick 		return;
3795fa9e4066Sahrens 
3796b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
3797b24ab676SJeff Bonwick 	packobj = od[1].od_object;
3798b24ab676SJeff Bonwick 	chunksize = od[0].od_gen;
3799b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
3800fa9e4066Sahrens 
3801fa9e4066Sahrens 	/*
3802fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
3803fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
3804fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
3805fa9e4066Sahrens 	 * handle this race correctly.
3806fa9e4066Sahrens 	 */
3807fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3808fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
3809a2cdcdd2SPaul Dagnelie 	dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3810a2cdcdd2SPaul Dagnelie 	    ZIO_PRIORITY_SYNC_READ);
3811fa9e4066Sahrens 
3812fa9e4066Sahrens 	/*
3813fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3814fa9e4066Sahrens 	 */
3815fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3816fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
3817fa9e4066Sahrens 
3818fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
3819fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
3820fa9e4066Sahrens 
3821b24ab676SJeff Bonwick 	bigoff = n * chunksize;
3822b24ab676SJeff Bonwick 	bigsize = s * chunksize;
3823fa9e4066Sahrens 
3824fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3825fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3826fa9e4066Sahrens 
3827fa9e4066Sahrens 	/*
3828fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3829fa9e4066Sahrens 	 * overwriting it.
3830fa9e4066Sahrens 	 */
3831fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
3832fa9e4066Sahrens 
3833fa9e4066Sahrens 	/*
3834fa9e4066Sahrens 	 * Read the current contents of our objects.
3835fa9e4066Sahrens 	 */
3836b24ab676SJeff Bonwick 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
38377bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3838fb09f5aaSMadhav Suresh 	ASSERT0(error);
3839b24ab676SJeff Bonwick 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
38407bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
3841fb09f5aaSMadhav Suresh 	ASSERT0(error);
3842fa9e4066Sahrens 
3843fa9e4066Sahrens 	/*
3844fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3845fa9e4066Sahrens 	 */
3846fa9e4066Sahrens 	tx = dmu_tx_create(os);
3847fa9e4066Sahrens 
3848b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3849fa9e4066Sahrens 
3850fa9e4066Sahrens 	if (freeit)
3851b24ab676SJeff Bonwick 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3852fa9e4066Sahrens 	else
3853b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3854fa9e4066Sahrens 
3855be9000ccSMatthew Ahrens 	/* This accounts for setting the checksum/compression. */
3856be9000ccSMatthew Ahrens 	dmu_tx_hold_bonus(tx, bigobj);
3857be9000ccSMatthew Ahrens 
3858b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3859b24ab676SJeff Bonwick 	if (txg == 0) {
3860fa9e4066Sahrens 		umem_free(packbuf, packsize);
3861fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
3862fa9e4066Sahrens 		return;
3863fa9e4066Sahrens 	}
3864fa9e4066Sahrens 
38655d7b4d43SMatthew Ahrens 	enum zio_checksum cksum;
38665d7b4d43SMatthew Ahrens 	do {
38675d7b4d43SMatthew Ahrens 		cksum = (enum zio_checksum)
38685d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
38695d7b4d43SMatthew Ahrens 	} while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
38705d7b4d43SMatthew Ahrens 	dmu_object_set_checksum(os, bigobj, cksum, tx);
3871b24ab676SJeff Bonwick 
38725d7b4d43SMatthew Ahrens 	enum zio_compress comp;
38735d7b4d43SMatthew Ahrens 	do {
38745d7b4d43SMatthew Ahrens 		comp = (enum zio_compress)
38755d7b4d43SMatthew Ahrens 		    ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
38765d7b4d43SMatthew Ahrens 	} while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
38775d7b4d43SMatthew Ahrens 	dmu_object_set_compress(os, bigobj, comp, tx);
3878fa9e4066Sahrens 
3879fa9e4066Sahrens 	/*
3880fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3881fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3882fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3883fa9e4066Sahrens 	 * with the new values we want to write out.
3884fa9e4066Sahrens 	 */
3885fa9e4066Sahrens 	for (i = 0; i < s; i++) {
3886fa9e4066Sahrens 		/* LINTED */
3887fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3888fa9e4066Sahrens 		/* LINTED */
3889b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3890fa9e4066Sahrens 		/* LINTED */
3891b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3892fa9e4066Sahrens 
3893fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3894fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3895fa9e4066Sahrens 
3896fa9e4066Sahrens 		if (pack->bw_txg > txg)
3897fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3898fa9e4066Sahrens 			    pack->bw_txg, txg);
3899fa9e4066Sahrens 
3900fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3901fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3902fa9e4066Sahrens 			    pack->bw_index, n, i);
3903fa9e4066Sahrens 
3904fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3905fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3906fa9e4066Sahrens 
3907fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3908fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3909fa9e4066Sahrens 
3910fa9e4066Sahrens 		if (freeit) {
3911fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
3912fa9e4066Sahrens 		} else {
3913fa9e4066Sahrens 			pack->bw_index = n + i;
3914fa9e4066Sahrens 			pack->bw_txg = txg;
3915fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3916fa9e4066Sahrens 		}
3917fa9e4066Sahrens 		*bigH = *pack;
3918fa9e4066Sahrens 		*bigT = *pack;
3919fa9e4066Sahrens 	}
3920fa9e4066Sahrens 
3921fa9e4066Sahrens 	/*
3922fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
3923fa9e4066Sahrens 	 * Now write them out.
3924fa9e4066Sahrens 	 */
3925b24ab676SJeff Bonwick 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3926fa9e4066Sahrens 
3927fa9e4066Sahrens 	if (freeit) {
3928420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3929fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
3930fa9e4066Sahrens 			    " txg %llx\n",
3931fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3932fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3933fa9e4066Sahrens 			    (u_longlong_t)txg);
3934fa9e4066Sahrens 		}
3935b24ab676SJeff Bonwick 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3936fa9e4066Sahrens 	} else {
3937420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
3938fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
3939fa9e4066Sahrens 			    " txg %llx\n",
3940fa9e4066Sahrens 			    (u_longlong_t)bigoff,
3941fa9e4066Sahrens 			    (u_longlong_t)bigsize,
3942fa9e4066Sahrens 			    (u_longlong_t)txg);
3943fa9e4066Sahrens 		}
3944b24ab676SJeff Bonwick 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3945fa9e4066Sahrens 	}
3946fa9e4066Sahrens 
3947fa9e4066Sahrens 	dmu_tx_commit(tx);
3948fa9e4066Sahrens 
3949fa9e4066Sahrens 	/*
3950fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
3951fa9e4066Sahrens 	 */
3952fa9e4066Sahrens 	{
3953fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3954fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3955fa9e4066Sahrens 
3956b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, packobj, packoff,
39577bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
3958b24ab676SJeff Bonwick 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
39597bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3960fa9e4066Sahrens 
3961fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3962fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3963fa9e4066Sahrens 
3964fa9e4066Sahrens 		umem_free(packcheck, packsize);
3965fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
3966fa9e4066Sahrens 	}
3967fa9e4066Sahrens 
3968fa9e4066Sahrens 	umem_free(packbuf, packsize);
3969fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
3970fa9e4066Sahrens }
3971fa9e4066Sahrens 
39722fdbea25SAleksandr Guzovskiy void
39732fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3974b24ab676SJeff Bonwick     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
39752fdbea25SAleksandr Guzovskiy {
39762fdbea25SAleksandr Guzovskiy 	uint64_t i;
39772fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
39782fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
39792fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
39802fdbea25SAleksandr Guzovskiy 
39812fdbea25SAleksandr Guzovskiy 	/*
39822fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
39832fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
39842fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
39852fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
39862fdbea25SAleksandr Guzovskiy 	 */
39872fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
39882fdbea25SAleksandr Guzovskiy 		/* LINTED */
39892fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
39902fdbea25SAleksandr Guzovskiy 		/* LINTED */
3991b24ab676SJeff Bonwick 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
39922fdbea25SAleksandr Guzovskiy 		/* LINTED */
3993b24ab676SJeff Bonwick 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
39942fdbea25SAleksandr Guzovskiy 
39952fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
39962fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
39972fdbea25SAleksandr Guzovskiy 
39982fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
39992fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
40002fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
40012fdbea25SAleksandr Guzovskiy 
40022fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
40032fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
40042fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
40052fdbea25SAleksandr Guzovskiy 
40062fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
40072fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
40082fdbea25SAleksandr Guzovskiy 
40092fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
40102fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
40112fdbea25SAleksandr Guzovskiy 
40122fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
40132fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
40142fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
40152fdbea25SAleksandr Guzovskiy 
40162fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
40172fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
40182fdbea25SAleksandr Guzovskiy 	}
40192fdbea25SAleksandr Guzovskiy }
40202fdbea25SAleksandr Guzovskiy 
40212fdbea25SAleksandr Guzovskiy void
4022b24ab676SJeff Bonwick ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
40232fdbea25SAleksandr Guzovskiy {
4024b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4025b24ab676SJeff Bonwick 	ztest_od_t od[2];
40262fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
40272fdbea25SAleksandr Guzovskiy 	uint64_t i;
40282fdbea25SAleksandr Guzovskiy 	int error;
40292fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
40302fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
4031b24ab676SJeff Bonwick 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4032b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4033b24ab676SJeff Bonwick 	uint64_t chunksize = blocksize;
40342fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
40352fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
40362fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
40372fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
40382fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
4039b24ab676SJeff Bonwick 	dmu_object_info_t doi;
40402fdbea25SAleksandr Guzovskiy 
40412fdbea25SAleksandr Guzovskiy 	/*
40422fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
40432fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
40442fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
40452fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
40462fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
40472fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
40482fdbea25SAleksandr Guzovskiy 	 *
40492fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
40502fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
40512fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
40522fdbea25SAleksandr Guzovskiy 	 *
40532fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
40542fdbea25SAleksandr Guzovskiy 	 * dmu_assign_arcbuf() can be tested for object updates.
40552fdbea25SAleksandr Guzovskiy 	 */
40562fdbea25SAleksandr Guzovskiy 
40572fdbea25SAleksandr Guzovskiy 	/*
40582fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
40592fdbea25SAleksandr Guzovskiy 	 */
4060b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4061b24ab676SJeff Bonwick 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
40622fdbea25SAleksandr Guzovskiy 
4063b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4064b24ab676SJeff Bonwick 		return;
40652fdbea25SAleksandr Guzovskiy 
4066b24ab676SJeff Bonwick 	bigobj = od[0].od_object;
4067b24ab676SJeff Bonwick 	packobj = od[1].od_object;
4068b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
4069b24ab676SJeff Bonwick 	chunksize = blocksize;
4070b24ab676SJeff Bonwick 	ASSERT(chunksize == od[1].od_gen);
40712fdbea25SAleksandr Guzovskiy 
4072b24ab676SJeff Bonwick 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4073b24ab676SJeff Bonwick 	VERIFY(ISP2(doi.doi_data_block_size));
4074b24ab676SJeff Bonwick 	VERIFY(chunksize == doi.doi_data_block_size);
4075b24ab676SJeff Bonwick 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
40762fdbea25SAleksandr Guzovskiy 
40772fdbea25SAleksandr Guzovskiy 	/*
40782fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
40792fdbea25SAleksandr Guzovskiy 	 */
40802fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
40812fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
40822fdbea25SAleksandr Guzovskiy 
40832fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
40842fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
40852fdbea25SAleksandr Guzovskiy 
4086b24ab676SJeff Bonwick 	bigoff = n * chunksize;
4087b24ab676SJeff Bonwick 	bigsize = s * chunksize;
40882fdbea25SAleksandr Guzovskiy 
40892fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
40902fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
40912fdbea25SAleksandr Guzovskiy 
4092b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
40932fdbea25SAleksandr Guzovskiy 
40942fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
40952fdbea25SAleksandr Guzovskiy 
40962fdbea25SAleksandr Guzovskiy 	/*
40972fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
40982fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
40992fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
41002fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
41012fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
41022fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
41032fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
41042fdbea25SAleksandr Guzovskiy 	 */
41052fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
41062fdbea25SAleksandr Guzovskiy 		uint64_t j;
41072fdbea25SAleksandr Guzovskiy 		uint64_t off;
41082fdbea25SAleksandr Guzovskiy 
41092fdbea25SAleksandr Guzovskiy 		/*
41102fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
41112fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
41122fdbea25SAleksandr Guzovskiy 		 * dmu_assign_arcbuf() when it can't directly
41132fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
41142fdbea25SAleksandr Guzovskiy 		 */
41152fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
411689c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
41172fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
4118b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize);
41192fdbea25SAleksandr Guzovskiy 			} else {
41202fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
4121b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
41222fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
4123b24ab676SJeff Bonwick 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
41242fdbea25SAleksandr Guzovskiy 			}
41252fdbea25SAleksandr Guzovskiy 		}
41262fdbea25SAleksandr Guzovskiy 
41272fdbea25SAleksandr Guzovskiy 		/*
41282fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
41292fdbea25SAleksandr Guzovskiy 		 */
41302fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
41312fdbea25SAleksandr Guzovskiy 
4132b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
4133b24ab676SJeff Bonwick 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
41342fdbea25SAleksandr Guzovskiy 
4135b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4136b24ab676SJeff Bonwick 		if (txg == 0) {
41372fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
41382fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
41392fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
414089c86e32SChris Williamson 				if (i != 5 ||
414189c86e32SChris Williamson 				    chunksize < (SPA_MINBLOCKSIZE * 2)) {
41422fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
41432fdbea25SAleksandr Guzovskiy 				} else {
41442fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
41452fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
41462fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
41472fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
41482fdbea25SAleksandr Guzovskiy 				}
41492fdbea25SAleksandr Guzovskiy 			}
41502fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
41512fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
41522fdbea25SAleksandr Guzovskiy 			return;
41532fdbea25SAleksandr Guzovskiy 		}
41542fdbea25SAleksandr Guzovskiy 
41552fdbea25SAleksandr Guzovskiy 		/*
41562fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
41572fdbea25SAleksandr Guzovskiy 		 * test dmu_assign_arcbuf() for the case when there're no
41582fdbea25SAleksandr Guzovskiy 		 * existing dbufs for the specified offsets.
41592fdbea25SAleksandr Guzovskiy 		 */
41602fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
4161b24ab676SJeff Bonwick 			error = dmu_read(os, packobj, packoff,
41627bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
4163fb09f5aaSMadhav Suresh 			ASSERT0(error);
4164b24ab676SJeff Bonwick 			error = dmu_read(os, bigobj, bigoff, bigsize,
41657bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
4166fb09f5aaSMadhav Suresh 			ASSERT0(error);
41672fdbea25SAleksandr Guzovskiy 		}
41682fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4169b24ab676SJeff Bonwick 		    n, chunksize, txg);
41702fdbea25SAleksandr Guzovskiy 
41712fdbea25SAleksandr Guzovskiy 		/*
41722fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
41732fdbea25SAleksandr Guzovskiy 		 * Now write them out.
41742fdbea25SAleksandr Guzovskiy 		 */
4175b24ab676SJeff Bonwick 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4176420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7) {
41772fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
41782fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
41792fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
41802fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
41812fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
41822fdbea25SAleksandr Guzovskiy 		}
4183b24ab676SJeff Bonwick 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
41842fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
418589c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
41862fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
4187b24ab676SJeff Bonwick 				    bigbuf_arcbufs[j]->b_data, chunksize);
41882fdbea25SAleksandr Guzovskiy 			} else {
41892fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
41902fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
4191b24ab676SJeff Bonwick 				    chunksize / 2);
41922fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
4193b24ab676SJeff Bonwick 				    chunksize / 2,
41942fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
4195b24ab676SJeff Bonwick 				    chunksize / 2);
41962fdbea25SAleksandr Guzovskiy 			}
41972fdbea25SAleksandr Guzovskiy 
41982fdbea25SAleksandr Guzovskiy 			if (i == 1) {
4199b24ab676SJeff Bonwick 				VERIFY(dmu_buf_hold(os, bigobj, off,
420047cb52daSJeff Bonwick 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
42012fdbea25SAleksandr Guzovskiy 			}
420289c86e32SChris Williamson 			if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
42032fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
42042fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
42052fdbea25SAleksandr Guzovskiy 			} else {
42062fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
42072fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
42082fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db,
4209b24ab676SJeff Bonwick 				    off + chunksize / 2,
42102fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
42112fdbea25SAleksandr Guzovskiy 			}
42122fdbea25SAleksandr Guzovskiy 			if (i == 1) {
42132fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
42142fdbea25SAleksandr Guzovskiy 			}
42152fdbea25SAleksandr Guzovskiy 		}
42162fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
42172fdbea25SAleksandr Guzovskiy 
42182fdbea25SAleksandr Guzovskiy 		/*
42192fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
42202fdbea25SAleksandr Guzovskiy 		 */
42212fdbea25SAleksandr Guzovskiy 		{
42222fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
42232fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
42242fdbea25SAleksandr Guzovskiy 
4225b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, packobj, packoff,
42267bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
4227b24ab676SJeff Bonwick 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
42287bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
42292fdbea25SAleksandr Guzovskiy 
42302fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
42312fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
42322fdbea25SAleksandr Guzovskiy 
42332fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
42342fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
42352fdbea25SAleksandr Guzovskiy 		}
42362fdbea25SAleksandr Guzovskiy 		if (i == 2) {
4237b24ab676SJeff Bonwick 			txg_wait_open(dmu_objset_pool(os), 0);
4238b24ab676SJeff Bonwick 		} else if (i == 3) {
4239b24ab676SJeff Bonwick 			txg_wait_synced(dmu_objset_pool(os), 0);
4240fa9e4066Sahrens 		}
4241e05725b1Sbonwick 	}
4242e05725b1Sbonwick 
4243b24ab676SJeff Bonwick 	dmu_buf_rele(bonus_db, FTAG);
4244b24ab676SJeff Bonwick 	umem_free(packbuf, packsize);
4245b24ab676SJeff Bonwick 	umem_free(bigbuf, bigsize);
4246b24ab676SJeff Bonwick 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4247b24ab676SJeff Bonwick }
4248fa9e4066Sahrens 
4249b24ab676SJeff Bonwick /* ARGSUSED */
4250b24ab676SJeff Bonwick void
4251b24ab676SJeff Bonwick ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4252b24ab676SJeff Bonwick {
4253b24ab676SJeff Bonwick 	ztest_od_t od[1];
4254b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4255b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4256fa9e4066Sahrens 
4257e05725b1Sbonwick 	/*
4258b24ab676SJeff Bonwick 	 * Have multiple threads write to large offsets in an object
4259b24ab676SJeff Bonwick 	 * to verify that parallel writes to an object -- even to the
4260b24ab676SJeff Bonwick 	 * same blocks within the object -- doesn't cause any trouble.
4261e05725b1Sbonwick 	 */
4262b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4263fa9e4066Sahrens 
4264b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4265e05725b1Sbonwick 		return;
4266fa9e4066Sahrens 
4267b24ab676SJeff Bonwick 	while (ztest_random(10) != 0)
4268b24ab676SJeff Bonwick 		ztest_io(zd, od[0].od_object, offset);
4269b24ab676SJeff Bonwick }
4270fa9e4066Sahrens 
4271b24ab676SJeff Bonwick void
4272b24ab676SJeff Bonwick ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4273b24ab676SJeff Bonwick {
4274b24ab676SJeff Bonwick 	ztest_od_t od[1];
4275b24ab676SJeff Bonwick 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4276b24ab676SJeff Bonwick 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4277b24ab676SJeff Bonwick 	uint64_t count = ztest_random(20) + 1;
4278b24ab676SJeff Bonwick 	uint64_t blocksize = ztest_random_blocksize();
4279b24ab676SJeff Bonwick 	void *data;
4280fa9e4066Sahrens 
4281b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4282fa9e4066Sahrens 
4283b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4284e05725b1Sbonwick 		return;
42850e34b6a7Sbonwick 
4286b24ab676SJeff Bonwick 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4287f4a72450SJeff Bonwick 		return;
4288f4a72450SJeff Bonwick 
4289b24ab676SJeff Bonwick 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4290fa9e4066Sahrens 
4291b24ab676SJeff Bonwick 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
4292b24ab676SJeff Bonwick 
4293b24ab676SJeff Bonwick 	while (ztest_random(count) != 0) {
4294b24ab676SJeff Bonwick 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
4295b24ab676SJeff Bonwick 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4296b24ab676SJeff Bonwick 		    data) != 0)
4297b24ab676SJeff Bonwick 			break;
4298b24ab676SJeff Bonwick 		while (ztest_random(4) != 0)
4299b24ab676SJeff Bonwick 			ztest_io(zd, od[0].od_object, randoff);
4300b24ab676SJeff Bonwick 	}
4301b24ab676SJeff Bonwick 
4302b24ab676SJeff Bonwick 	umem_free(data, blocksize);
4303fa9e4066Sahrens }
4304fa9e4066Sahrens 
4305fa9e4066Sahrens /*
4306fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
4307fa9e4066Sahrens  */
4308fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
4309fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
4310fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
4311fa9e4066Sahrens 
4312fa9e4066Sahrens void
4313b24ab676SJeff Bonwick ztest_zap(ztest_ds_t *zd, uint64_t id)
4314fa9e4066Sahrens {
4315b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4316b24ab676SJeff Bonwick 	ztest_od_t od[1];
4317fa9e4066Sahrens 	uint64_t object;
4318fa9e4066Sahrens 	uint64_t txg, last_txg;
4319fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
4320fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
4321fa9e4066Sahrens 	int i, ints;
4322fa9e4066Sahrens 	dmu_tx_t *tx;
4323fa9e4066Sahrens 	char propname[100], txgname[100];
4324fa9e4066Sahrens 	int error;
4325fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4326fa9e4066Sahrens 
4327b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4328fa9e4066Sahrens 
4329b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4330b24ab676SJeff Bonwick 		return;
4331fa9e4066Sahrens 
4332b24ab676SJeff Bonwick 	object = od[0].od_object;
4333fa9e4066Sahrens 
4334b24ab676SJeff Bonwick 	/*
4335b24ab676SJeff Bonwick 	 * Generate a known hash collision, and verify that
4336b24ab676SJeff Bonwick 	 * we can lookup and remove both entries.
4337b24ab676SJeff Bonwick 	 */
4338b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
4339b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4340b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4341b24ab676SJeff Bonwick 	if (txg == 0)
4342b24ab676SJeff Bonwick 		return;
4343b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4344b24ab676SJeff Bonwick 		value[i] = i;
4345b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4346b24ab676SJeff Bonwick 		    1, &value[i], tx));
4347b24ab676SJeff Bonwick 	}
4348b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4349b24ab676SJeff Bonwick 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4350b24ab676SJeff Bonwick 		    sizeof (uint64_t), 1, &value[i], tx));
4351b420f3adSRichard Lowe 		VERIFY3U(0, ==,
4352b420f3adSRichard Lowe 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4353b24ab676SJeff Bonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4354b24ab676SJeff Bonwick 		ASSERT3U(zl_ints, ==, 1);
4355fa9e4066Sahrens 	}
4356b24ab676SJeff Bonwick 	for (i = 0; i < 2; i++) {
4357b420f3adSRichard Lowe 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4358b24ab676SJeff Bonwick 	}
4359b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
4360fa9e4066Sahrens 
4361b24ab676SJeff Bonwick 	/*
4362b24ab676SJeff Bonwick 	 * Generate a buch of random entries.
4363b24ab676SJeff Bonwick 	 */
4364fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4365fa9e4066Sahrens 
4366e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4367e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4368e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4369e05725b1Sbonwick 	bzero(value, sizeof (value));
4370e05725b1Sbonwick 	last_txg = 0;
4371fa9e4066Sahrens 
4372e05725b1Sbonwick 	/*
4373e05725b1Sbonwick 	 * If these zap entries already exist, validate their contents.
4374e05725b1Sbonwick 	 */
4375e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4376e05725b1Sbonwick 	if (error == 0) {
4377e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4378e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, 1);
4379fa9e4066Sahrens 
4380e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4381e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
4382fa9e4066Sahrens 
4383e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
4384e05725b1Sbonwick 		    &zl_ints) == 0);
4385fa9e4066Sahrens 
4386e05725b1Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4387e05725b1Sbonwick 		ASSERT3U(zl_ints, ==, ints);
4388fa9e4066Sahrens 
4389e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
4390e05725b1Sbonwick 		    zl_ints, value) == 0);
4391fa9e4066Sahrens 
4392e05725b1Sbonwick 		for (i = 0; i < ints; i++) {
4393e05725b1Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
4394fa9e4066Sahrens 		}
4395e05725b1Sbonwick 	} else {
4396e05725b1Sbonwick 		ASSERT3U(error, ==, ENOENT);
4397e05725b1Sbonwick 	}
4398fa9e4066Sahrens 
4399e05725b1Sbonwick 	/*
4400e05725b1Sbonwick 	 * Atomically update two entries in our zap object.
4401e05725b1Sbonwick 	 * The first is named txg_%llu, and contains the txg
4402e05725b1Sbonwick 	 * in which the property was last updated.  The second
4403e05725b1Sbonwick 	 * is named prop_%llu, and the nth element of its value
4404e05725b1Sbonwick 	 * should be txg + object + n.
4405e05725b1Sbonwick 	 */
4406e05725b1Sbonwick 	tx = dmu_tx_create(os);
4407b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4408b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4409b24ab676SJeff Bonwick 	if (txg == 0)
4410e05725b1Sbonwick 		return;
4411fa9e4066Sahrens 
4412e05725b1Sbonwick 	if (last_txg > txg)
4413e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4414fa9e4066Sahrens 
4415e05725b1Sbonwick 	for (i = 0; i < ints; i++)
4416e05725b1Sbonwick 		value[i] = txg + object + i;
4417fa9e4066Sahrens 
4418b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4419b24ab676SJeff Bonwick 	    1, &txg, tx));
4420b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4421b24ab676SJeff Bonwick 	    ints, value, tx));
4422fa9e4066Sahrens 
4423e05725b1Sbonwick 	dmu_tx_commit(tx);
4424fa9e4066Sahrens 
4425e05725b1Sbonwick 	/*
4426e05725b1Sbonwick 	 * Remove a random pair of entries.
4427e05725b1Sbonwick 	 */
4428e05725b1Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4429e05725b1Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4430e05725b1Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4431fa9e4066Sahrens 
4432e05725b1Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4433fa9e4066Sahrens 
4434e05725b1Sbonwick 	if (error == ENOENT)
4435e05725b1Sbonwick 		return;
4436fa9e4066Sahrens 
4437fb09f5aaSMadhav Suresh 	ASSERT0(error);
4438fa9e4066Sahrens 
4439e05725b1Sbonwick 	tx = dmu_tx_create(os);
4440b24ab676SJeff Bonwick 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4441b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4442b24ab676SJeff Bonwick 	if (txg == 0)
444312a2833aSSanjeev Bagewadi 		return;
4444b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4445b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
444612a2833aSSanjeev Bagewadi 	dmu_tx_commit(tx);
444712a2833aSSanjeev Bagewadi }
444812a2833aSSanjeev Bagewadi 
444912a2833aSSanjeev Bagewadi /*
445012a2833aSSanjeev Bagewadi  * Testcase to test the upgrading of a microzap to fatzap.
445112a2833aSSanjeev Bagewadi  */
445212a2833aSSanjeev Bagewadi void
4453b24ab676SJeff Bonwick ztest_fzap(ztest_ds_t *zd, uint64_t id)
445412a2833aSSanjeev Bagewadi {
4455b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4456b24ab676SJeff Bonwick 	ztest_od_t od[1];
4457b24ab676SJeff Bonwick 	uint64_t object, txg;
445812a2833aSSanjeev Bagewadi 
4459b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
446012a2833aSSanjeev Bagewadi 
4461b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4462b24ab676SJeff Bonwick 		return;
446312a2833aSSanjeev Bagewadi 
4464b24ab676SJeff Bonwick 	object = od[0].od_object;
446512a2833aSSanjeev Bagewadi 
446612a2833aSSanjeev Bagewadi 	/*
4467b24ab676SJeff Bonwick 	 * Add entries to this ZAP and make sure it spills over
446812a2833aSSanjeev Bagewadi 	 * and gets upgraded to a fatzap. Also, since we are adding
4469b24ab676SJeff Bonwick 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
447012a2833aSSanjeev Bagewadi 	 */
4471b24ab676SJeff Bonwick 	for (int i = 0; i < 2050; i++) {
44729adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
4473b24ab676SJeff Bonwick 		uint64_t value = i;
4474b24ab676SJeff Bonwick 		dmu_tx_t *tx;
4475b24ab676SJeff Bonwick 		int error;
447612a2833aSSanjeev Bagewadi 
4477b24ab676SJeff Bonwick 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4478b24ab676SJeff Bonwick 		    id, value);
447912a2833aSSanjeev Bagewadi 
4480b24ab676SJeff Bonwick 		tx = dmu_tx_create(os);
4481b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
4482b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4483b24ab676SJeff Bonwick 		if (txg == 0)
448412a2833aSSanjeev Bagewadi 			return;
4485b24ab676SJeff Bonwick 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
4486b24ab676SJeff Bonwick 		    &value, tx);
448712a2833aSSanjeev Bagewadi 		ASSERT(error == 0 || error == EEXIST);
448812a2833aSSanjeev Bagewadi 		dmu_tx_commit(tx);
448912a2833aSSanjeev Bagewadi 	}
4490fa9e4066Sahrens }
4491fa9e4066Sahrens 
4492b24ab676SJeff Bonwick /* ARGSUSED */
4493fa9e4066Sahrens void
4494b24ab676SJeff Bonwick ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4495fa9e4066Sahrens {
4496b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4497b24ab676SJeff Bonwick 	ztest_od_t od[1];
4498fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4499fa9e4066Sahrens 	dmu_tx_t *tx;
4500fa9e4066Sahrens 	int i, namelen, error;
4501b24ab676SJeff Bonwick 	int micro = ztest_random(2);
4502fa9e4066Sahrens 	char name[20], string_value[20];
4503fa9e4066Sahrens 	void *data;
4504fa9e4066Sahrens 
4505b24ab676SJeff Bonwick 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4506b24ab676SJeff Bonwick 
4507b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4508b24ab676SJeff Bonwick 		return;
4509b24ab676SJeff Bonwick 
4510b24ab676SJeff Bonwick 	object = od[0].od_object;
4511b24ab676SJeff Bonwick 
4512e05725b1Sbonwick 	/*
4513e05725b1Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
4514e05725b1Sbonwick 	 * x is a random printable character and the dots are dots.
4515e05725b1Sbonwick 	 * There are 94 such characters, and the name length goes from
4516e05725b1Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4517e05725b1Sbonwick 	 */
4518e05725b1Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4519fa9e4066Sahrens 
4520e05725b1Sbonwick 	for (i = 0; i < 3; i++)
4521e05725b1Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
4522e05725b1Sbonwick 	for (; i < namelen - 1; i++)
4523e05725b1Sbonwick 		name[i] = '.';
4524e05725b1Sbonwick 	name[i] = '\0';
4525fa9e4066Sahrens 
4526b24ab676SJeff Bonwick 	if ((namelen & 1) || micro) {
4527e05725b1Sbonwick 		wsize = sizeof (txg);
4528e05725b1Sbonwick 		wc = 1;
4529e05725b1Sbonwick 		data = &txg;
4530e05725b1Sbonwick 	} else {
4531e05725b1Sbonwick 		wsize = 1;
4532e05725b1Sbonwick 		wc = namelen;
4533e05725b1Sbonwick 		data = string_value;
4534e05725b1Sbonwick 	}
4535fa9e4066Sahrens 
4536e05725b1Sbonwick 	count = -1ULL;
45373b2aab18SMatthew Ahrens 	VERIFY0(zap_count(os, object, &count));
4538e05725b1Sbonwick 	ASSERT(count != -1ULL);
4539fa9e4066Sahrens 
4540e05725b1Sbonwick 	/*
4541e05725b1Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
4542e05725b1Sbonwick 	 */
4543e05725b1Sbonwick 	i = ztest_random(5);
4544e05725b1Sbonwick 
4545e05725b1Sbonwick 	if (i >= 2) {
4546e05725b1Sbonwick 		tx = dmu_tx_create(os);
4547b24ab676SJeff Bonwick 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4548b24ab676SJeff Bonwick 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4549b24ab676SJeff Bonwick 		if (txg == 0)
4550e05725b1Sbonwick 			return;
4551e05725b1Sbonwick 		bcopy(name, string_value, namelen);
4552e05725b1Sbonwick 	} else {
4553e05725b1Sbonwick 		tx = NULL;
4554e05725b1Sbonwick 		txg = 0;
4555e05725b1Sbonwick 		bzero(string_value, namelen);
4556e05725b1Sbonwick 	}
4557fa9e4066Sahrens 
4558e05725b1Sbonwick 	switch (i) {
4559fa9e4066Sahrens 
4560e05725b1Sbonwick 	case 0:
4561e05725b1Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4562e05725b1Sbonwick 		if (error == 0) {
4563e05725b1Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
4564e05725b1Sbonwick 			ASSERT3U(wc, ==, zl_wc);
4565e05725b1Sbonwick 		} else {
4566e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4567e05725b1Sbonwick 		}
4568e05725b1Sbonwick 		break;
4569fa9e4066Sahrens 
4570e05725b1Sbonwick 	case 1:
4571e05725b1Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
4572e05725b1Sbonwick 		if (error == 0) {
4573e05725b1Sbonwick 			if (data == string_value &&
4574e05725b1Sbonwick 			    bcmp(name, data, namelen) != 0)
4575e05725b1Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
4576e05725b1Sbonwick 				    name, data, namelen);
4577e05725b1Sbonwick 		} else {
4578e05725b1Sbonwick 			ASSERT3U(error, ==, ENOENT);
4579e05725b1Sbonwick 		}
4580e05725b1Sbonwick 		break;
4581fa9e4066Sahrens 
4582e05725b1Sbonwick 	case 2:
4583e05725b1Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
4584e05725b1Sbonwick 		ASSERT(error == 0 || error == EEXIST);
4585e05725b1Sbonwick 		break;
4586fa9e4066Sahrens 
4587e05725b1Sbonwick 	case 3:
4588e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4589e05725b1Sbonwick 		break;
4590fa9e4066Sahrens 
4591e05725b1Sbonwick 	case 4:
4592e05725b1Sbonwick 		error = zap_remove(os, object, name, tx);
4593e05725b1Sbonwick 		ASSERT(error == 0 || error == ENOENT);
4594e05725b1Sbonwick 		break;
4595fa9e4066Sahrens 	}
4596e05725b1Sbonwick 
4597e05725b1Sbonwick 	if (tx != NULL)
4598e05725b1Sbonwick 		dmu_tx_commit(tx);
4599fa9e4066Sahrens }
4600fa9e4066Sahrens 
4601d20e665cSRicardo M. Correia /*
4602d20e665cSRicardo M. Correia  * Commit callback data.
4603d20e665cSRicardo M. Correia  */
4604d20e665cSRicardo M. Correia typedef struct ztest_cb_data {
4605d20e665cSRicardo M. Correia 	list_node_t		zcd_node;
4606d20e665cSRicardo M. Correia 	uint64_t		zcd_txg;
4607d20e665cSRicardo M. Correia 	int			zcd_expected_err;
4608d20e665cSRicardo M. Correia 	boolean_t		zcd_added;
4609d20e665cSRicardo M. Correia 	boolean_t		zcd_called;
4610d20e665cSRicardo M. Correia 	spa_t			*zcd_spa;
4611d20e665cSRicardo M. Correia } ztest_cb_data_t;
4612d20e665cSRicardo M. Correia 
4613d20e665cSRicardo M. Correia /* This is the actual commit callback function */
4614d20e665cSRicardo M. Correia static void
4615d20e665cSRicardo M. Correia ztest_commit_callback(void *arg, int error)
4616d20e665cSRicardo M. Correia {
4617d20e665cSRicardo M. Correia 	ztest_cb_data_t *data = arg;
4618d20e665cSRicardo M. Correia 	uint64_t synced_txg;
4619d20e665cSRicardo M. Correia 
4620d20e665cSRicardo M. Correia 	VERIFY(data != NULL);
4621d20e665cSRicardo M. Correia 	VERIFY3S(data->zcd_expected_err, ==, error);
4622d20e665cSRicardo M. Correia 	VERIFY(!data->zcd_called);
4623d20e665cSRicardo M. Correia 
4624d20e665cSRicardo M. Correia 	synced_txg = spa_last_synced_txg(data->zcd_spa);
4625d20e665cSRicardo M. Correia 	if (data->zcd_txg > synced_txg)
4626d20e665cSRicardo M. Correia 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4627d20e665cSRicardo M. Correia 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4628d20e665cSRicardo M. Correia 		    synced_txg);
4629d20e665cSRicardo M. Correia 
4630d20e665cSRicardo M. Correia 	data->zcd_called = B_TRUE;
4631d20e665cSRicardo M. Correia 
4632d20e665cSRicardo M. Correia 	if (error == ECANCELED) {
4633fb09f5aaSMadhav Suresh 		ASSERT0(data->zcd_txg);
4634d20e665cSRicardo M. Correia 		ASSERT(!data->zcd_added);
4635d20e665cSRicardo M. Correia 
4636d20e665cSRicardo M. Correia 		/*
4637d20e665cSRicardo M. Correia 		 * The private callback data should be destroyed here, but
4638d20e665cSRicardo M. Correia 		 * since we are going to check the zcd_called field after
4639d20e665cSRicardo M. Correia 		 * dmu_tx_abort(), we will destroy it there.
4640d20e665cSRicardo M. Correia 		 */
4641d20e665cSRicardo M. Correia 		return;
4642d20e665cSRicardo M. Correia 	}
4643d20e665cSRicardo M. Correia 
4644d20e665cSRicardo M. Correia 	/* Was this callback added to the global callback list? */
4645d20e665cSRicardo M. Correia 	if (!data->zcd_added)
4646d20e665cSRicardo M. Correia 		goto out;
4647d20e665cSRicardo M. Correia 
4648d20e665cSRicardo M. Correia 	ASSERT3U(data->zcd_txg, !=, 0);
4649d20e665cSRicardo M. Correia 
4650d20e665cSRicardo M. Correia 	/* Remove our callback from the list */
4651f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
4652d20e665cSRicardo M. Correia 	list_remove(&zcl.zcl_callbacks, data);
4653f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
4654d20e665cSRicardo M. Correia 
4655d20e665cSRicardo M. Correia out:
4656d20e665cSRicardo M. Correia 	umem_free(data, sizeof (ztest_cb_data_t));
4657d20e665cSRicardo M. Correia }
4658d20e665cSRicardo M. Correia 
4659d20e665cSRicardo M. Correia /* Allocate and initialize callback data structure */
4660d20e665cSRicardo M. Correia static ztest_cb_data_t *
4661d20e665cSRicardo M. Correia ztest_create_cb_data(objset_t *os, uint64_t txg)
4662d20e665cSRicardo M. Correia {
4663d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data;
4664d20e665cSRicardo M. Correia 
4665d20e665cSRicardo M. Correia 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4666d20e665cSRicardo M. Correia 
4667d20e665cSRicardo M. Correia 	cb_data->zcd_txg = txg;
4668d20e665cSRicardo M. Correia 	cb_data->zcd_spa = dmu_objset_spa(os);
4669d20e665cSRicardo M. Correia 
4670d20e665cSRicardo M. Correia 	return (cb_data);
4671d20e665cSRicardo M. Correia }
4672d20e665cSRicardo M. Correia 
4673d20e665cSRicardo M. Correia /*
4674d20e665cSRicardo M. Correia  * If a number of txgs equal to this threshold have been created after a commit
4675d20e665cSRicardo M. Correia  * callback has been registered but not called, then we assume there is an
4676d20e665cSRicardo M. Correia  * implementation bug.
4677d20e665cSRicardo M. Correia  */
4678d20e665cSRicardo M. Correia #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
4679d20e665cSRicardo M. Correia 
4680d20e665cSRicardo M. Correia /*
4681d20e665cSRicardo M. Correia  * Commit callback test.
4682d20e665cSRicardo M. Correia  */
4683d20e665cSRicardo M. Correia void
4684b24ab676SJeff Bonwick ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4685d20e665cSRicardo M. Correia {
4686b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
4687b24ab676SJeff Bonwick 	ztest_od_t od[1];
4688d20e665cSRicardo M. Correia 	dmu_tx_t *tx;
4689d20e665cSRicardo M. Correia 	ztest_cb_data_t *cb_data[3], *tmp_cb;
4690d20e665cSRicardo M. Correia 	uint64_t old_txg, txg;
4691d20e665cSRicardo M. Correia 	int i, error;
4692d20e665cSRicardo M. Correia 
4693b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4694b24ab676SJeff Bonwick 
4695b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4696b24ab676SJeff Bonwick 		return;
4697b24ab676SJeff Bonwick 
4698d20e665cSRicardo M. Correia 	tx = dmu_tx_create(os);
4699d20e665cSRicardo M. Correia 
4700d20e665cSRicardo M. Correia 	cb_data[0] = ztest_create_cb_data(os, 0);
4701d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4702d20e665cSRicardo M. Correia 
4703b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4704d20e665cSRicardo M. Correia 
4705d20e665cSRicardo M. Correia 	/* Every once in a while, abort the transaction on purpose */
4706d20e665cSRicardo M. Correia 	if (ztest_random(100) == 0)
4707d20e665cSRicardo M. Correia 		error = -1;
4708d20e665cSRicardo M. Correia 
4709d20e665cSRicardo M. Correia 	if (!error)
4710d20e665cSRicardo M. Correia 		error = dmu_tx_assign(tx, TXG_NOWAIT);
4711d20e665cSRicardo M. Correia 
4712d20e665cSRicardo M. Correia 	txg = error ? 0 : dmu_tx_get_txg(tx);
4713d20e665cSRicardo M. Correia 
4714d20e665cSRicardo M. Correia 	cb_data[0]->zcd_txg = txg;
4715d20e665cSRicardo M. Correia 	cb_data[1] = ztest_create_cb_data(os, txg);
4716d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4717d20e665cSRicardo M. Correia 
4718d20e665cSRicardo M. Correia 	if (error) {
4719d20e665cSRicardo M. Correia 		/*
4720d20e665cSRicardo M. Correia 		 * It's not a strict requirement to call the registered
4721d20e665cSRicardo M. Correia 		 * callbacks from inside dmu_tx_abort(), but that's what
4722d20e665cSRicardo M. Correia 		 * it's supposed to happen in the current implementation
4723d20e665cSRicardo M. Correia 		 * so we will check for that.
4724d20e665cSRicardo M. Correia 		 */
4725d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4726d20e665cSRicardo M. Correia 			cb_data[i]->zcd_expected_err = ECANCELED;
4727d20e665cSRicardo M. Correia 			VERIFY(!cb_data[i]->zcd_called);
4728d20e665cSRicardo M. Correia 		}
4729d20e665cSRicardo M. Correia 
4730d20e665cSRicardo M. Correia 		dmu_tx_abort(tx);
4731d20e665cSRicardo M. Correia 
4732d20e665cSRicardo M. Correia 		for (i = 0; i < 2; i++) {
4733d20e665cSRicardo M. Correia 			VERIFY(cb_data[i]->zcd_called);
4734d20e665cSRicardo M. Correia 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4735d20e665cSRicardo M. Correia 		}
4736d20e665cSRicardo M. Correia 
4737d20e665cSRicardo M. Correia 		return;
4738d20e665cSRicardo M. Correia 	}
4739d20e665cSRicardo M. Correia 
4740d20e665cSRicardo M. Correia 	cb_data[2] = ztest_create_cb_data(os, txg);
4741d20e665cSRicardo M. Correia 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4742d20e665cSRicardo M. Correia 
4743d20e665cSRicardo M. Correia 	/*
4744d20e665cSRicardo M. Correia 	 * Read existing data to make sure there isn't a future leak.
4745d20e665cSRicardo M. Correia 	 */
4746b24ab676SJeff Bonwick 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4747d20e665cSRicardo M. Correia 	    &old_txg, DMU_READ_PREFETCH));
4748d20e665cSRicardo M. Correia 
4749d20e665cSRicardo M. Correia 	if (old_txg > txg)
4750d20e665cSRicardo M. Correia 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4751d20e665cSRicardo M. Correia 		    old_txg, txg);
4752d20e665cSRicardo M. Correia 
4753b24ab676SJeff Bonwick 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4754d20e665cSRicardo M. Correia 
4755f06dce2cSAndrew Stormont 	mutex_enter(&zcl.zcl_callbacks_lock);
4756d20e665cSRicardo M. Correia 
4757d20e665cSRicardo M. Correia 	/*
4758d20e665cSRicardo M. Correia 	 * Since commit callbacks don't have any ordering requirement and since
4759d20e665cSRicardo M. Correia 	 * it is theoretically possible for a commit callback to be called
4760d20e665cSRicardo M. Correia 	 * after an arbitrary amount of time has elapsed since its txg has been
4761d20e665cSRicardo M. Correia 	 * synced, it is difficult to reliably determine whether a commit
4762d20e665cSRicardo M. Correia 	 * callback hasn't been called due to high load or due to a flawed
4763d20e665cSRicardo M. Correia 	 * implementation.
4764d20e665cSRicardo M. Correia 	 *
4765d20e665cSRicardo M. Correia 	 * In practice, we will assume that if after a certain number of txgs a
4766d20e665cSRicardo M. Correia 	 * commit callback hasn't been called, then most likely there's an
4767d20e665cSRicardo M. Correia 	 * implementation bug..
4768d20e665cSRicardo M. Correia 	 */
4769d20e665cSRicardo M. Correia 	tmp_cb = list_head(&zcl.zcl_callbacks);
4770d20e665cSRicardo M. Correia 	if (tmp_cb != NULL &&
4771b3d9f2e2SWill Andrews 	    (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4772d20e665cSRicardo M. Correia 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4773d20e665cSRicardo M. Correia 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4774d20e665cSRicardo M. Correia 	}
4775d20e665cSRicardo M. Correia 
4776d20e665cSRicardo M. Correia 	/*
4777d20e665cSRicardo M. Correia 	 * Let's find the place to insert our callbacks.
4778d20e665cSRicardo M. Correia 	 *
4779d20e665cSRicardo M. Correia 	 * Even though the list is ordered by txg, it is possible for the
4780d20e665cSRicardo M. Correia 	 * insertion point to not be the end because our txg may already be
4781d20e665cSRicardo M. Correia 	 * quiescing at this point and other callbacks in the open txg
4782d20e665cSRicardo M. Correia 	 * (from other objsets) may have sneaked in.
4783d20e665cSRicardo M. Correia 	 */
4784d20e665cSRicardo M. Correia 	tmp_cb = list_tail(&zcl.zcl_callbacks);
4785d20e665cSRicardo M. Correia 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4786d20e665cSRicardo M. Correia 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4787d20e665cSRicardo M. Correia 
4788d20e665cSRicardo M. Correia 	/* Add the 3 callbacks to the list */
4789d20e665cSRicardo M. Correia 	for (i = 0; i < 3; i++) {
4790d20e665cSRicardo M. Correia 		if (tmp_cb == NULL)
4791d20e665cSRicardo M. Correia 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4792d20e665cSRicardo M. Correia 		else
4793d20e665cSRicardo M. Correia 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4794d20e665cSRicardo M. Correia 			    cb_data[i]);
4795d20e665cSRicardo M. Correia 
4796d20e665cSRicardo M. Correia 		cb_data[i]->zcd_added = B_TRUE;
4797d20e665cSRicardo M. Correia 		VERIFY(!cb_data[i]->zcd_called);
4798d20e665cSRicardo M. Correia 
4799d20e665cSRicardo M. Correia 		tmp_cb = cb_data[i];
4800d20e665cSRicardo M. Correia 	}
4801d20e665cSRicardo M. Correia 
4802f06dce2cSAndrew Stormont 	mutex_exit(&zcl.zcl_callbacks_lock);
4803d20e665cSRicardo M. Correia 
4804d20e665cSRicardo M. Correia 	dmu_tx_commit(tx);
4805d20e665cSRicardo M. Correia }
4806d20e665cSRicardo M. Correia 
4807b24ab676SJeff Bonwick /* ARGSUSED */
4808fa9e4066Sahrens void
4809b24ab676SJeff Bonwick ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4810fa9e4066Sahrens {
4811b24ab676SJeff Bonwick 	zfs_prop_t proplist[] = {
4812b24ab676SJeff Bonwick 		ZFS_PROP_CHECKSUM,
4813b24ab676SJeff Bonwick 		ZFS_PROP_COMPRESSION,
4814b24ab676SJeff Bonwick 		ZFS_PROP_COPIES,
4815b24ab676SJeff Bonwick 		ZFS_PROP_DEDUP
4816b24ab676SJeff Bonwick 	};
4817fa9e4066Sahrens 
4818f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
4819fa9e4066Sahrens 
4820b24ab676SJeff Bonwick 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4821b24ab676SJeff Bonwick 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4822b24ab676SJeff Bonwick 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4823fa9e4066Sahrens 
4824f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
4825b24ab676SJeff Bonwick }
4826fa9e4066Sahrens 
48275cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
48285cabbc6bSPrashanth Sreenivasa void
48295cabbc6bSPrashanth Sreenivasa ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
48305cabbc6bSPrashanth Sreenivasa {
4831f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
48325cabbc6bSPrashanth Sreenivasa 
48335cabbc6bSPrashanth Sreenivasa 	int error = dmu_objset_remap_indirects(zd->zd_name);
48345cabbc6bSPrashanth Sreenivasa 	if (error == ENOSPC)
48355cabbc6bSPrashanth Sreenivasa 		error = 0;
48365cabbc6bSPrashanth Sreenivasa 	ASSERT0(error);
48375cabbc6bSPrashanth Sreenivasa 
4838f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
48395cabbc6bSPrashanth Sreenivasa }
48405cabbc6bSPrashanth Sreenivasa 
4841b24ab676SJeff Bonwick /* ARGSUSED */
4842b24ab676SJeff Bonwick void
4843b24ab676SJeff Bonwick ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4844b24ab676SJeff Bonwick {
4845b24ab676SJeff Bonwick 	nvlist_t *props = NULL;
4846ea8dc4b6Seschrock 
4847f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
4848ea8dc4b6Seschrock 
4849420dfc95SChris Siden 	(void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4850b24ab676SJeff Bonwick 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4851fa9e4066Sahrens 
4852fb09f5aaSMadhav Suresh 	VERIFY0(spa_prop_get(ztest_spa, &props));
4853fa9e4066Sahrens 
4854420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
4855b24ab676SJeff Bonwick 		dump_nvlist(props, 4);
4856fa9e4066Sahrens 
4857b24ab676SJeff Bonwick 	nvlist_free(props);
4858fa9e4066Sahrens 
4859f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
4860fa9e4066Sahrens }
4861fa9e4066Sahrens 
48623b2aab18SMatthew Ahrens static int
48633b2aab18SMatthew Ahrens user_release_one(const char *snapname, const char *holdname)
48643b2aab18SMatthew Ahrens {
48653b2aab18SMatthew Ahrens 	nvlist_t *snaps, *holds;
48663b2aab18SMatthew Ahrens 	int error;
48673b2aab18SMatthew Ahrens 
48683b2aab18SMatthew Ahrens 	snaps = fnvlist_alloc();
48693b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
48703b2aab18SMatthew Ahrens 	fnvlist_add_boolean(holds, holdname);
48713b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(snaps, snapname, holds);
48723b2aab18SMatthew Ahrens 	fnvlist_free(holds);
48733b2aab18SMatthew Ahrens 	error = dsl_dataset_user_release(snaps, NULL);
48743b2aab18SMatthew Ahrens 	fnvlist_free(snaps);
48753b2aab18SMatthew Ahrens 	return (error);
48763b2aab18SMatthew Ahrens }
48773b2aab18SMatthew Ahrens 
48785c987a37SChris Kirby /*
48795c987a37SChris Kirby  * Test snapshot hold/release and deferred destroy.
48805c987a37SChris Kirby  */
48815c987a37SChris Kirby void
4882b24ab676SJeff Bonwick ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
48835c987a37SChris Kirby {
48845c987a37SChris Kirby 	int error;
4885b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
48865c987a37SChris Kirby 	objset_t *origin;
48875c987a37SChris Kirby 	char snapname[100];
48885c987a37SChris Kirby 	char fullname[100];
48895c987a37SChris Kirby 	char clonename[100];
48905c987a37SChris Kirby 	char tag[100];
48919adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
48923b2aab18SMatthew Ahrens 	nvlist_t *holds;
48935c987a37SChris Kirby 
4894f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
48955c987a37SChris Kirby 
48965c987a37SChris Kirby 	dmu_objset_name(os, osname);
48975c987a37SChris Kirby 
48983b2aab18SMatthew Ahrens 	(void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
48993b2aab18SMatthew Ahrens 	(void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
49003b2aab18SMatthew Ahrens 	(void) snprintf(clonename, sizeof (clonename),
49013b2aab18SMatthew Ahrens 	    "%s/ch1_%llu", osname, id);
49023b2aab18SMatthew Ahrens 	(void) snprintf(tag, sizeof (tag), "tag_%llu", id);
49035c987a37SChris Kirby 
49045c987a37SChris Kirby 	/*
49055c987a37SChris Kirby 	 * Clean up from any previous run.
49065c987a37SChris Kirby 	 */
49073b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
49083b2aab18SMatthew Ahrens 	if (error != ENOENT)
49093b2aab18SMatthew Ahrens 		ASSERT0(error);
49103b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
49113b2aab18SMatthew Ahrens 	if (error != ESRCH && error != ENOENT)
49123b2aab18SMatthew Ahrens 		ASSERT0(error);
49133b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
49143b2aab18SMatthew Ahrens 	if (error != ENOENT)
49153b2aab18SMatthew Ahrens 		ASSERT0(error);
49165c987a37SChris Kirby 
49175c987a37SChris Kirby 	/*
49185c987a37SChris Kirby 	 * Create snapshot, clone it, mark snap for deferred destroy,
49195c987a37SChris Kirby 	 * destroy clone, verify snap was also destroyed.
49205c987a37SChris Kirby 	 */
49214445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
49225c987a37SChris Kirby 	if (error) {
49235c987a37SChris Kirby 		if (error == ENOSPC) {
49245c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
49255c987a37SChris Kirby 			goto out;
49265c987a37SChris Kirby 		}
49275c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
49285c987a37SChris Kirby 	}
49295c987a37SChris Kirby 
49303b2aab18SMatthew Ahrens 	error = dmu_objset_clone(clonename, fullname);
49315c987a37SChris Kirby 	if (error) {
49325c987a37SChris Kirby 		if (error == ENOSPC) {
49335c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_clone");
49345c987a37SChris Kirby 			goto out;
49355c987a37SChris Kirby 		}
49365c987a37SChris Kirby 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
49375c987a37SChris Kirby 	}
49385c987a37SChris Kirby 
49393b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
49405c987a37SChris Kirby 	if (error) {
49413b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
49425c987a37SChris Kirby 		    fullname, error);
49435c987a37SChris Kirby 	}
49445c987a37SChris Kirby 
49453b2aab18SMatthew Ahrens 	error = dsl_destroy_head(clonename);
49465c987a37SChris Kirby 	if (error)
49473b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
49485c987a37SChris Kirby 
49495c987a37SChris Kirby 	error = dmu_objset_hold(fullname, FTAG, &origin);
49505c987a37SChris Kirby 	if (error != ENOENT)
49515c987a37SChris Kirby 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
49525c987a37SChris Kirby 
49535c987a37SChris Kirby 	/*
49545c987a37SChris Kirby 	 * Create snapshot, add temporary hold, verify that we can't
49555c987a37SChris Kirby 	 * destroy a held snapshot, mark for deferred destroy,
49565c987a37SChris Kirby 	 * release hold, verify snapshot was destroyed.
49575c987a37SChris Kirby 	 */
49584445fffbSMatthew Ahrens 	error = dmu_objset_snapshot_one(osname, snapname);
49595c987a37SChris Kirby 	if (error) {
49605c987a37SChris Kirby 		if (error == ENOSPC) {
49615c987a37SChris Kirby 			ztest_record_enospc("dmu_objset_snapshot");
49625c987a37SChris Kirby 			goto out;
49635c987a37SChris Kirby 		}
49645c987a37SChris Kirby 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
49655c987a37SChris Kirby 	}
49665c987a37SChris Kirby 
49673b2aab18SMatthew Ahrens 	holds = fnvlist_alloc();
49683b2aab18SMatthew Ahrens 	fnvlist_add_string(holds, fullname, tag);
49693b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold(holds, 0, NULL);
49703b2aab18SMatthew Ahrens 	fnvlist_free(holds);
49713b2aab18SMatthew Ahrens 
49725d7b4d43SMatthew Ahrens 	if (error == ENOSPC) {
49735d7b4d43SMatthew Ahrens 		ztest_record_enospc("dsl_dataset_user_hold");
49745d7b4d43SMatthew Ahrens 		goto out;
49755d7b4d43SMatthew Ahrens 	} else if (error) {
49765d7b4d43SMatthew Ahrens 		fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
49775d7b4d43SMatthew Ahrens 		    fullname, tag, error);
49785d7b4d43SMatthew Ahrens 	}
49795c987a37SChris Kirby 
49803b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_FALSE);
49815c987a37SChris Kirby 	if (error != EBUSY) {
49823b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
49835c987a37SChris Kirby 		    fullname, error);
49845c987a37SChris Kirby 	}
49855c987a37SChris Kirby 
49863b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshot(fullname, B_TRUE);
49875c987a37SChris Kirby 	if (error) {
49883b2aab18SMatthew Ahrens 		fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
49895c987a37SChris Kirby 		    fullname, error);
49905c987a37SChris Kirby 	}
49915c987a37SChris Kirby 
49923b2aab18SMatthew Ahrens 	error = user_release_one(fullname, tag);
49935c987a37SChris Kirby 	if (error)
4994a7a845e4SSteven Hartland 		fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
49955c987a37SChris Kirby 
49963b2aab18SMatthew Ahrens 	VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
49975c987a37SChris Kirby 
49985c987a37SChris Kirby out:
4999f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
50005c987a37SChris Kirby }
50015c987a37SChris Kirby 
5002fa9e4066Sahrens /*
5003fa9e4066Sahrens  * Inject random faults into the on-disk data.
5004fa9e4066Sahrens  */
5005b24ab676SJeff Bonwick /* ARGSUSED */
5006fa9e4066Sahrens void
5007b24ab676SJeff Bonwick ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5008fa9e4066Sahrens {
5009b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5010420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5011fa9e4066Sahrens 	int fd;
5012fa9e4066Sahrens 	uint64_t offset;
50131195e687SMark J Musante 	uint64_t leaves;
5014fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
5015fa9e4066Sahrens 	uint64_t top, leaf;
5016fa9e4066Sahrens 	char path0[MAXPATHLEN];
5017fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
5018fa9e4066Sahrens 	size_t fsize;
5019f34284d8SMatthew Ahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;
5020fa9e4066Sahrens 	int iters = 1000;
50211195e687SMark J Musante 	int maxfaults;
50221195e687SMark J Musante 	int mirror_save;
5023e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
5024ea8dc4b6Seschrock 	uint64_t guid0 = 0;
50258f18d1faSGeorge Wilson 	boolean_t islog = B_FALSE;
5026ea8dc4b6Seschrock 
5027f06dce2cSAndrew Stormont 	mutex_enter(&ztest_vdev_lock);
50283a4b1be9SMatthew Ahrens 
50293a4b1be9SMatthew Ahrens 	/*
50303a4b1be9SMatthew Ahrens 	 * Device removal is in progress, fault injection must be disabled
50313a4b1be9SMatthew Ahrens 	 * until it completes and the pool is scrubbed.  The fault injection
50323a4b1be9SMatthew Ahrens 	 * strategy for damaging blocks does not take in to account evacuated
50333a4b1be9SMatthew Ahrens 	 * blocks which may have already been damaged.
50343a4b1be9SMatthew Ahrens 	 */
50353a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active) {
50363a4b1be9SMatthew Ahrens 		mutex_exit(&ztest_vdev_lock);
50373a4b1be9SMatthew Ahrens 		return;
50383a4b1be9SMatthew Ahrens 	}
50393a4b1be9SMatthew Ahrens 
50401195e687SMark J Musante 	maxfaults = MAXFAULTS();
5041420dfc95SChris Siden 	leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
50421195e687SMark J Musante 	mirror_save = zs->zs_mirrors;
5043f06dce2cSAndrew Stormont 	mutex_exit(&ztest_vdev_lock);
50441195e687SMark J Musante 
5045e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
5046fa9e4066Sahrens 
50472c1e2b44SGeorge Wilson 	/*
50482c1e2b44SGeorge Wilson 	 * Grab the name lock as reader. There are some operations
50492c1e2b44SGeorge Wilson 	 * which don't like to have their vdevs changed while
50502c1e2b44SGeorge Wilson 	 * they are in progress (i.e. spa_change_guid). Those
50512c1e2b44SGeorge Wilson 	 * operations will have grabbed the name lock as writer.
50522c1e2b44SGeorge Wilson 	 */
5053f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
50542c1e2b44SGeorge Wilson 
5055fa9e4066Sahrens 	/*
5056e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5057fa9e4066Sahrens 	 */
5058e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5059fa9e4066Sahrens 
5060e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
5061e14bb325SJeff Bonwick 		/*
5062b24ab676SJeff Bonwick 		 * Inject errors on a normal data device or slog device.
5063e14bb325SJeff Bonwick 		 */
5064b24ab676SJeff Bonwick 		top = ztest_random_vdev_top(spa, B_TRUE);
50651195e687SMark J Musante 		leaf = ztest_random(leaves) + zs->zs_splits;
5066fa9e4066Sahrens 
5067e14bb325SJeff Bonwick 		/*
5068e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
5069e14bb325SJeff Bonwick 		 * and to the random leaf we selected.  We'll induce transient
5070e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
5071e14bb325SJeff Bonwick 		 * and we'll write random garbage to the randomly chosen leaf.
5072e14bb325SJeff Bonwick 		 */
5073e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
5074420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5075420dfc95SChris Siden 		    top * leaves + zs->zs_splits);
5076e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5077420dfc95SChris Siden 		    ztest_opts.zo_dir, ztest_opts.zo_pool,
5078420dfc95SChris Siden 		    top * leaves + leaf);
5079fa9e4066Sahrens 
5080e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
50818f18d1faSGeorge Wilson 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
50828f18d1faSGeorge Wilson 			islog = B_TRUE;
50838f18d1faSGeorge Wilson 
50842c1e2b44SGeorge Wilson 		/*
50852c1e2b44SGeorge Wilson 		 * If the top-level vdev needs to be resilvered
50862c1e2b44SGeorge Wilson 		 * then we only allow faults on the device that is
50872c1e2b44SGeorge Wilson 		 * resilvering.
50882c1e2b44SGeorge Wilson 		 */
50892c1e2b44SGeorge Wilson 		if (vd0 != NULL && maxfaults != 1 &&
50902c1e2b44SGeorge Wilson 		    (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5091b4952e17SGeorge Wilson 		    vd0->vdev_resilver_txg != 0)) {
5092e14bb325SJeff Bonwick 			/*
5093e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
5094e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
5095e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
5096e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
5097e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
5098e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
5099e14bb325SJeff Bonwick 			 * combination of this with injection of random data
5100e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
5101e14bb325SJeff Bonwick 			 */
5102e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
5103e14bb325SJeff Bonwick 
51045cabbc6bSPrashanth Sreenivasa 			zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
51055cabbc6bSPrashanth Sreenivasa 			    (long long)vd0->vdev_id, (int)maxfaults);
51065cabbc6bSPrashanth Sreenivasa 
5107e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
5108e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
5109e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
5110e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
5111e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
5112e14bb325SJeff Bonwick 			} else {
5113e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
5114e14bb325SJeff Bonwick 			}
5115e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
5116e14bb325SJeff Bonwick 		}
5117e14bb325SJeff Bonwick 	} else {
5118e14bb325SJeff Bonwick 		/*
5119e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
5120e14bb325SJeff Bonwick 		 */
5121e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
5122fa9e4066Sahrens 
5123e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
5124e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
5125f06dce2cSAndrew Stormont 			rw_exit(&ztest_name_lock);
5126e14bb325SJeff Bonwick 			return;
5127e14bb325SJeff Bonwick 		}
5128e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5129ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
5130e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
5131e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
5132e14bb325SJeff Bonwick 
5133e14bb325SJeff Bonwick 		leaf = 0;
5134e14bb325SJeff Bonwick 		leaves = 1;
5135e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
5136fa9e4066Sahrens 	}
5137fa9e4066Sahrens 
5138e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
5139f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5140e14bb325SJeff Bonwick 
5141fa9e4066Sahrens 	/*
51428f18d1faSGeorge Wilson 	 * If we can tolerate two or more faults, or we're dealing
51438f18d1faSGeorge Wilson 	 * with a slog, randomly online/offline vd0.
5144fa9e4066Sahrens 	 */
51458f18d1faSGeorge Wilson 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
51468ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
51478ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
51488ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
51498f18d1faSGeorge Wilson 
51508f18d1faSGeorge Wilson 			/*
51518f18d1faSGeorge Wilson 			 * We have to grab the zs_name_lock as writer to
51528f18d1faSGeorge Wilson 			 * prevent a race between offlining a slog and
51538f18d1faSGeorge Wilson 			 * destroying a dataset. Offlining the slog will
51548f18d1faSGeorge Wilson 			 * grab a reference on the dataset which may cause
51558f18d1faSGeorge Wilson 			 * dmu_objset_destroy() to fail with EBUSY thus
51568f18d1faSGeorge Wilson 			 * leaving the dataset in an inconsistent state.
51578f18d1faSGeorge Wilson 			 */
51588f18d1faSGeorge Wilson 			if (islog)
5159f06dce2cSAndrew Stormont 				rw_enter(&ztest_name_lock, RW_WRITER);
51608f18d1faSGeorge Wilson 
51618ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
51628f18d1faSGeorge Wilson 
51638f18d1faSGeorge Wilson 			if (islog)
5164f06dce2cSAndrew Stormont 				rw_exit(&ztest_name_lock);
51658ad4d6ddSJeff Bonwick 		} else {
51669253d63dSGeorge Wilson 			/*
51679253d63dSGeorge Wilson 			 * Ideally we would like to be able to randomly
51689253d63dSGeorge Wilson 			 * call vdev_[on|off]line without holding locks
51699253d63dSGeorge Wilson 			 * to force unpredictable failures but the side
51709253d63dSGeorge Wilson 			 * effects of vdev_[on|off]line prevent us from
51719253d63dSGeorge Wilson 			 * doing so. We grab the ztest_vdev_lock here to
51729253d63dSGeorge Wilson 			 * prevent a race between injection testing and
51739253d63dSGeorge Wilson 			 * aux_vdev removal.
51749253d63dSGeorge Wilson 			 */
5175f06dce2cSAndrew Stormont 			mutex_enter(&ztest_vdev_lock);
51768ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
5177f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
51788ad4d6ddSJeff Bonwick 		}
5179fa9e4066Sahrens 	}
5180fa9e4066Sahrens 
51818f18d1faSGeorge Wilson 	if (maxfaults == 0)
51828f18d1faSGeorge Wilson 		return;
51838f18d1faSGeorge Wilson 
5184fa9e4066Sahrens 	/*
5185ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
5186fa9e4066Sahrens 	 */
5187fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
5188fa9e4066Sahrens 
518986714001SSerapheim Dimitropoulos 	if (fd == -1) /* we hit a gap in the device namespace */
5190fa9e4066Sahrens 		return;
5191fa9e4066Sahrens 
5192fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
5193fa9e4066Sahrens 
5194fa9e4066Sahrens 	while (--iters != 0) {
5195f9eb9fdfSMatthew Ahrens 		/*
5196f9eb9fdfSMatthew Ahrens 		 * The offset must be chosen carefully to ensure that
5197f9eb9fdfSMatthew Ahrens 		 * we do not inject a given logical block with errors
5198f9eb9fdfSMatthew Ahrens 		 * on two different leaf devices, because ZFS can not
5199f9eb9fdfSMatthew Ahrens 		 * tolerate that (if maxfaults==1).
5200f9eb9fdfSMatthew Ahrens 		 *
5201f9eb9fdfSMatthew Ahrens 		 * We divide each leaf into chunks of size
5202f9eb9fdfSMatthew Ahrens 		 * (# leaves * SPA_MAXBLOCKSIZE * 4).  Within each chunk
5203f9eb9fdfSMatthew Ahrens 		 * there is a series of ranges to which we can inject errors.
5204f9eb9fdfSMatthew Ahrens 		 * Each range can accept errors on only a single leaf vdev.
5205f9eb9fdfSMatthew Ahrens 		 * The error injection ranges are separated by ranges
5206f9eb9fdfSMatthew Ahrens 		 * which we will not inject errors on any device (DMZs).
5207f9eb9fdfSMatthew Ahrens 		 * Each DMZ must be large enough such that a single block
5208f9eb9fdfSMatthew Ahrens 		 * can not straddle it, so that a single block can not be
5209f9eb9fdfSMatthew Ahrens 		 * a target in two different injection ranges (on different
5210f9eb9fdfSMatthew Ahrens 		 * leaf vdevs).
5211f9eb9fdfSMatthew Ahrens 		 *
5212f9eb9fdfSMatthew Ahrens 		 * For example, with 3 leaves, each chunk looks like:
5213f9eb9fdfSMatthew Ahrens 		 *    0 to  32M: injection range for leaf 0
5214f9eb9fdfSMatthew Ahrens 		 *  32M to  64M: DMZ - no injection allowed
5215f9eb9fdfSMatthew Ahrens 		 *  64M to  96M: injection range for leaf 1
5216f9eb9fdfSMatthew Ahrens 		 *  96M to 128M: DMZ - no injection allowed
5217f9eb9fdfSMatthew Ahrens 		 * 128M to 160M: injection range for leaf 2
5218f9eb9fdfSMatthew Ahrens 		 * 160M to 192M: DMZ - no injection allowed
5219f9eb9fdfSMatthew Ahrens 		 */
5220fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
5221fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
5222fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5223fa9e4066Sahrens 
5224f34284d8SMatthew Ahrens 		/*
5225f34284d8SMatthew Ahrens 		 * Only allow damage to the labels at one end of the vdev.
5226f34284d8SMatthew Ahrens 		 *
5227f34284d8SMatthew Ahrens 		 * If all labels are damaged, the device will be totally
5228f34284d8SMatthew Ahrens 		 * inaccessible, which will result in loss of data,
5229f34284d8SMatthew Ahrens 		 * because we also damage (parts of) the other side of
5230f34284d8SMatthew Ahrens 		 * the mirror/raidz.
5231f34284d8SMatthew Ahrens 		 *
5232f34284d8SMatthew Ahrens 		 * Additionally, we will always have both an even and an
5233f34284d8SMatthew Ahrens 		 * odd label, so that we can handle crashes in the
5234f34284d8SMatthew Ahrens 		 * middle of vdev_config_sync().
5235f34284d8SMatthew Ahrens 		 */
5236f34284d8SMatthew Ahrens 		if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5237f34284d8SMatthew Ahrens 			continue;
5238f34284d8SMatthew Ahrens 
5239f34284d8SMatthew Ahrens 		/*
5240f34284d8SMatthew Ahrens 		 * The two end labels are stored at the "end" of the disk, but
5241f34284d8SMatthew Ahrens 		 * the end of the disk (vdev_psize) is aligned to
5242f34284d8SMatthew Ahrens 		 * sizeof (vdev_label_t).
5243f34284d8SMatthew Ahrens 		 */
5244f34284d8SMatthew Ahrens 		uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5245f34284d8SMatthew Ahrens 		if ((leaf & 1) == 1 &&
5246f34284d8SMatthew Ahrens 		    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5247fa9e4066Sahrens 			continue;
5248fa9e4066Sahrens 
5249f06dce2cSAndrew Stormont 		mutex_enter(&ztest_vdev_lock);
52501195e687SMark J Musante 		if (mirror_save != zs->zs_mirrors) {
5251f06dce2cSAndrew Stormont 			mutex_exit(&ztest_vdev_lock);
52521195e687SMark J Musante 			(void) close(fd);
52531195e687SMark J Musante 			return;
52541195e687SMark J Musante 		}
5255fa9e4066Sahrens 
5256fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5257fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
5258fa9e4066Sahrens 			    offset, pathrand);
52591195e687SMark J Musante 
5260f06dce2cSAndrew Stormont 		mutex_exit(&ztest_vdev_lock);
52611195e687SMark J Musante 
5262420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 7)
52631195e687SMark J Musante 			(void) printf("injected bad word into %s,"
52641195e687SMark J Musante 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5265fa9e4066Sahrens 	}
5266fa9e4066Sahrens 
5267fa9e4066Sahrens 	(void) close(fd);
5268fa9e4066Sahrens }
5269fa9e4066Sahrens 
5270fa9e4066Sahrens /*
5271b24ab676SJeff Bonwick  * Verify that DDT repair works as expected.
5272fa9e4066Sahrens  */
5273fa9e4066Sahrens void
5274b24ab676SJeff Bonwick ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5275fa9e4066Sahrens {
5276b24ab676SJeff Bonwick 	ztest_shared_t *zs = ztest_shared;
5277420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5278b24ab676SJeff Bonwick 	objset_t *os = zd->zd_os;
5279b24ab676SJeff Bonwick 	ztest_od_t od[1];
5280b24ab676SJeff Bonwick 	uint64_t object, blocksize, txg, pattern, psize;
5281b24ab676SJeff Bonwick 	enum zio_checksum checksum = spa_dedup_checksum(spa);
5282b24ab676SJeff Bonwick 	dmu_buf_t *db;
5283b24ab676SJeff Bonwick 	dmu_tx_t *tx;
5284770499e1SDan Kimmel 	abd_t *abd;
5285b24ab676SJeff Bonwick 	blkptr_t blk;
5286b24ab676SJeff Bonwick 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
5287fa9e4066Sahrens 
5288b24ab676SJeff Bonwick 	blocksize = ztest_random_blocksize();
5289b24ab676SJeff Bonwick 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
5290fa9e4066Sahrens 
5291b24ab676SJeff Bonwick 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5292fa9e4066Sahrens 
5293b24ab676SJeff Bonwick 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5294b24ab676SJeff Bonwick 		return;
5295fa9e4066Sahrens 
5296fa9e4066Sahrens 	/*
5297b24ab676SJeff Bonwick 	 * Take the name lock as writer to prevent anyone else from changing
5298b24ab676SJeff Bonwick 	 * the pool and dataset properies we need to maintain during this test.
5299fa9e4066Sahrens 	 */
5300f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5301fa9e4066Sahrens 
5302b24ab676SJeff Bonwick 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5303b24ab676SJeff Bonwick 	    B_FALSE) != 0 ||
5304b24ab676SJeff Bonwick 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5305b24ab676SJeff Bonwick 	    B_FALSE) != 0) {
5306f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5307b24ab676SJeff Bonwick 		return;
5308b24ab676SJeff Bonwick 	}
5309b24ab676SJeff Bonwick 
5310aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
5311aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5312aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
5313aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5314aab80726SGeorge Wilson 
5315b24ab676SJeff Bonwick 	object = od[0].od_object;
5316b24ab676SJeff Bonwick 	blocksize = od[0].od_blocksize;
5317aab80726SGeorge Wilson 	pattern = zs->zs_guid ^ dds.dds_guid;
5318b24ab676SJeff Bonwick 
5319b24ab676SJeff Bonwick 	ASSERT(object != 0);
5320b24ab676SJeff Bonwick 
5321b24ab676SJeff Bonwick 	tx = dmu_tx_create(os);
5322b24ab676SJeff Bonwick 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5323b24ab676SJeff Bonwick 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5324b24ab676SJeff Bonwick 	if (txg == 0) {
5325f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5326b24ab676SJeff Bonwick 		return;
5327b24ab676SJeff Bonwick 	}
5328fa9e4066Sahrens 
5329fa9e4066Sahrens 	/*
5330b24ab676SJeff Bonwick 	 * Write all the copies of our block.
5331fa9e4066Sahrens 	 */
5332b24ab676SJeff Bonwick 	for (int i = 0; i < copies; i++) {
5333b24ab676SJeff Bonwick 		uint64_t offset = i * blocksize;
53343b2aab18SMatthew Ahrens 		int error = dmu_buf_hold(os, object, offset, FTAG, &db,
53353b2aab18SMatthew Ahrens 		    DMU_READ_NO_PREFETCH);
53363b2aab18SMatthew Ahrens 		if (error != 0) {
53373b2aab18SMatthew Ahrens 			fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
53383b2aab18SMatthew Ahrens 			    os, (long long)object, (long long) offset, error);
53393b2aab18SMatthew Ahrens 		}
5340b24ab676SJeff Bonwick 		ASSERT(db->db_offset == offset);
5341b24ab676SJeff Bonwick 		ASSERT(db->db_size == blocksize);
5342b24ab676SJeff Bonwick 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5343b24ab676SJeff Bonwick 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5344b24ab676SJeff Bonwick 		dmu_buf_will_fill(db, tx);
5345b24ab676SJeff Bonwick 		ztest_pattern_set(db->db_data, db->db_size, pattern);
5346b24ab676SJeff Bonwick 		dmu_buf_rele(db, FTAG);
5347b24ab676SJeff Bonwick 	}
5348fa9e4066Sahrens 
5349b24ab676SJeff Bonwick 	dmu_tx_commit(tx);
5350b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), txg);
5351fa9e4066Sahrens 
5352fa9e4066Sahrens 	/*
5353b24ab676SJeff Bonwick 	 * Find out what block we got.
5354fa9e4066Sahrens 	 */
535580901aeaSGeorge Wilson 	VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
535680901aeaSGeorge Wilson 	    DMU_READ_NO_PREFETCH));
5357b24ab676SJeff Bonwick 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5358b24ab676SJeff Bonwick 	dmu_buf_rele(db, FTAG);
5359fa9e4066Sahrens 
5360fa9e4066Sahrens 	/*
5361b24ab676SJeff Bonwick 	 * Damage the block.  Dedup-ditto will save us when we read it later.
5362fa9e4066Sahrens 	 */
5363b24ab676SJeff Bonwick 	psize = BP_GET_PSIZE(&blk);
5364770499e1SDan Kimmel 	abd = abd_alloc_linear(psize, B_TRUE);
5365770499e1SDan Kimmel 	ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5366fa9e4066Sahrens 
5367b24ab676SJeff Bonwick 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5368770499e1SDan Kimmel 	    abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5369b24ab676SJeff Bonwick 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5370fa9e4066Sahrens 
5371770499e1SDan Kimmel 	abd_free(abd);
5372fa9e4066Sahrens 
5373f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5374fa9e4066Sahrens }
5375fa9e4066Sahrens 
5376fa9e4066Sahrens /*
5377b24ab676SJeff Bonwick  * Scrub the pool.
5378fa9e4066Sahrens  */
5379b24ab676SJeff Bonwick /* ARGSUSED */
5380b24ab676SJeff Bonwick void
5381b24ab676SJeff Bonwick ztest_scrub(ztest_ds_t *zd, uint64_t id)
5382fa9e4066Sahrens {
5383420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5384fa9e4066Sahrens 
53853a4b1be9SMatthew Ahrens 	/*
53863a4b1be9SMatthew Ahrens 	 * Scrub in progress by device removal.
53873a4b1be9SMatthew Ahrens 	 */
53883a4b1be9SMatthew Ahrens 	if (ztest_device_removal_active)
53893a4b1be9SMatthew Ahrens 		return;
53903a4b1be9SMatthew Ahrens 
53913f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5392b24ab676SJeff Bonwick 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
53933f9d6ad7SLin Ling 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
5394b24ab676SJeff Bonwick }
5395fa9e4066Sahrens 
5396e9103aaeSGarrett D'Amore /*
5397e9103aaeSGarrett D'Amore  * Change the guid for the pool.
5398e9103aaeSGarrett D'Amore  */
5399e9103aaeSGarrett D'Amore /* ARGSUSED */
5400e9103aaeSGarrett D'Amore void
5401e9103aaeSGarrett D'Amore ztest_reguid(ztest_ds_t *zd, uint64_t id)
5402e9103aaeSGarrett D'Amore {
5403420dfc95SChris Siden 	spa_t *spa = ztest_spa;
5404e9103aaeSGarrett D'Amore 	uint64_t orig, load;
5405dfbb9432SGeorge Wilson 	int error;
5406e9103aaeSGarrett D'Amore 
5407e9103aaeSGarrett D'Amore 	orig = spa_guid(spa);
5408e9103aaeSGarrett D'Amore 	load = spa_load_guid(spa);
5409dfbb9432SGeorge Wilson 
5410f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5411dfbb9432SGeorge Wilson 	error = spa_change_guid(spa);
5412f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5413dfbb9432SGeorge Wilson 
5414dfbb9432SGeorge Wilson 	if (error != 0)
5415e9103aaeSGarrett D'Amore 		return;
5416e9103aaeSGarrett D'Amore 
541725345e46SGeorge Wilson 	if (ztest_opts.zo_verbose >= 4) {
5418e9103aaeSGarrett D'Amore 		(void) printf("Changed guid old %llu -> %llu\n",
5419e9103aaeSGarrett D'Amore 		    (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5420e9103aaeSGarrett D'Amore 	}
5421e9103aaeSGarrett D'Amore 
5422e9103aaeSGarrett D'Amore 	VERIFY3U(orig, !=, spa_guid(spa));
5423e9103aaeSGarrett D'Amore 	VERIFY3U(load, ==, spa_load_guid(spa));
5424e9103aaeSGarrett D'Amore }
5425e9103aaeSGarrett D'Amore 
5426b24ab676SJeff Bonwick /*
5427b24ab676SJeff Bonwick  * Rename the pool to a different name and then rename it back.
5428b24ab676SJeff Bonwick  */
5429b24ab676SJeff Bonwick /* ARGSUSED */
5430b24ab676SJeff Bonwick void
5431b24ab676SJeff Bonwick ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5432b24ab676SJeff Bonwick {
5433b24ab676SJeff Bonwick 	char *oldname, *newname;
5434b24ab676SJeff Bonwick 	spa_t *spa;
5435fa9e4066Sahrens 
5436f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_WRITER);
5437fa9e4066Sahrens 
5438420dfc95SChris Siden 	oldname = ztest_opts.zo_pool;
5439b24ab676SJeff Bonwick 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5440b24ab676SJeff Bonwick 	(void) strcpy(newname, oldname);
5441b24ab676SJeff Bonwick 	(void) strcat(newname, "_tmp");
5442fa9e4066Sahrens 
5443fa9e4066Sahrens 	/*
5444b24ab676SJeff Bonwick 	 * Do the rename
5445fa9e4066Sahrens 	 */
5446b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(oldname, newname));
5447fa9e4066Sahrens 
5448fa9e4066Sahrens 	/*
5449b24ab676SJeff Bonwick 	 * Try to open it under the old name, which shouldn't exist
5450fa9e4066Sahrens 	 */
5451b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5452fa9e4066Sahrens 
5453fa9e4066Sahrens 	/*
5454b24ab676SJeff Bonwick 	 * Open it under the new name and make sure it's still the same spa_t.
5455fa9e4066Sahrens 	 */
5456b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5457fa9e4066Sahrens 
5458420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5459b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5460fa9e4066Sahrens 
5461b24ab676SJeff Bonwick 	/*
5462b24ab676SJeff Bonwick 	 * Rename it back to the original
5463b24ab676SJeff Bonwick 	 */
5464b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_rename(newname, oldname));
5465fa9e4066Sahrens 
5466fa9e4066Sahrens 	/*
5467b24ab676SJeff Bonwick 	 * Make sure it can still be opened
5468fa9e4066Sahrens 	 */
5469b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5470fa9e4066Sahrens 
5471420dfc95SChris Siden 	ASSERT(spa == ztest_spa);
5472b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
5473fa9e4066Sahrens 
5474b24ab676SJeff Bonwick 	umem_free(newname, strlen(newname) + 1);
5475b24ab676SJeff Bonwick 
5476f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5477fa9e4066Sahrens }
5478fa9e4066Sahrens 
5479*094e47e9SGeorge Wilson static vdev_t *
5480*094e47e9SGeorge Wilson ztest_random_concrete_vdev_leaf(vdev_t *vd)
5481*094e47e9SGeorge Wilson {
5482*094e47e9SGeorge Wilson 	if (vd == NULL)
5483*094e47e9SGeorge Wilson 		return (NULL);
5484*094e47e9SGeorge Wilson 
5485*094e47e9SGeorge Wilson 	if (vd->vdev_children == 0)
5486*094e47e9SGeorge Wilson 		return (vd);
5487*094e47e9SGeorge Wilson 
5488*094e47e9SGeorge Wilson 	vdev_t *eligible[vd->vdev_children];
5489*094e47e9SGeorge Wilson 	int eligible_idx = 0, i;
5490*094e47e9SGeorge Wilson 	for (i = 0; i < vd->vdev_children; i++) {
5491*094e47e9SGeorge Wilson 		vdev_t *cvd = vd->vdev_child[i];
5492*094e47e9SGeorge Wilson 		if (cvd->vdev_top->vdev_removing)
5493*094e47e9SGeorge Wilson 			continue;
5494*094e47e9SGeorge Wilson 		if (cvd->vdev_children > 0 ||
5495*094e47e9SGeorge Wilson 		    (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5496*094e47e9SGeorge Wilson 			eligible[eligible_idx++] = cvd;
5497*094e47e9SGeorge Wilson 		}
5498*094e47e9SGeorge Wilson 	}
5499*094e47e9SGeorge Wilson 	VERIFY(eligible_idx > 0);
5500*094e47e9SGeorge Wilson 
5501*094e47e9SGeorge Wilson 	uint64_t child_no = ztest_random(eligible_idx);
5502*094e47e9SGeorge Wilson 	return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5503*094e47e9SGeorge Wilson }
5504*094e47e9SGeorge Wilson 
5505*094e47e9SGeorge Wilson /* ARGSUSED */
5506*094e47e9SGeorge Wilson void
5507*094e47e9SGeorge Wilson ztest_initialize(ztest_ds_t *zd, uint64_t id)
5508*094e47e9SGeorge Wilson {
5509*094e47e9SGeorge Wilson 	spa_t *spa = ztest_spa;
5510*094e47e9SGeorge Wilson 	int error = 0;
5511*094e47e9SGeorge Wilson 
5512*094e47e9SGeorge Wilson 	mutex_enter(&ztest_vdev_lock);
5513*094e47e9SGeorge Wilson 
5514*094e47e9SGeorge Wilson 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5515*094e47e9SGeorge Wilson 
5516*094e47e9SGeorge Wilson 	/* Random leaf vdev */
5517*094e47e9SGeorge Wilson 	vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5518*094e47e9SGeorge Wilson 	if (rand_vd == NULL) {
5519*094e47e9SGeorge Wilson 		spa_config_exit(spa, SCL_VDEV, FTAG);
5520*094e47e9SGeorge Wilson 		mutex_exit(&ztest_vdev_lock);
5521*094e47e9SGeorge Wilson 		return;
5522*094e47e9SGeorge Wilson 	}
5523*094e47e9SGeorge Wilson 
5524*094e47e9SGeorge Wilson 	/*
5525*094e47e9SGeorge Wilson 	 * The random vdev we've selected may change as soon as we
5526*094e47e9SGeorge Wilson 	 * drop the spa_config_lock. We create local copies of things
5527*094e47e9SGeorge Wilson 	 * we're interested in.
5528*094e47e9SGeorge Wilson 	 */
5529*094e47e9SGeorge Wilson 	uint64_t guid = rand_vd->vdev_guid;
5530*094e47e9SGeorge Wilson 	char *path = strdup(rand_vd->vdev_path);
5531*094e47e9SGeorge Wilson 	boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5532*094e47e9SGeorge Wilson 
5533*094e47e9SGeorge Wilson 	zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5534*094e47e9SGeorge Wilson 	spa_config_exit(spa, SCL_VDEV, FTAG);
5535*094e47e9SGeorge Wilson 
5536*094e47e9SGeorge Wilson 	uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5537*094e47e9SGeorge Wilson 	error = spa_vdev_initialize(spa, guid, cmd);
5538*094e47e9SGeorge Wilson 	switch (cmd) {
5539*094e47e9SGeorge Wilson 	case POOL_INITIALIZE_CANCEL:
5540*094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5541*094e47e9SGeorge Wilson 			(void) printf("Cancel initialize %s", path);
5542*094e47e9SGeorge Wilson 			if (!active)
5543*094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5544*094e47e9SGeorge Wilson 			(void) printf("\n");
5545*094e47e9SGeorge Wilson 		}
5546*094e47e9SGeorge Wilson 		break;
5547*094e47e9SGeorge Wilson 	case POOL_INITIALIZE_DO:
5548*094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5549*094e47e9SGeorge Wilson 			(void) printf("Start initialize %s", path);
5550*094e47e9SGeorge Wilson 			if (active && error == 0)
5551*094e47e9SGeorge Wilson 				(void) printf(" failed (already active)");
5552*094e47e9SGeorge Wilson 			else if (error != 0)
5553*094e47e9SGeorge Wilson 				(void) printf(" failed (error %d)", error);
5554*094e47e9SGeorge Wilson 			(void) printf("\n");
5555*094e47e9SGeorge Wilson 		}
5556*094e47e9SGeorge Wilson 		break;
5557*094e47e9SGeorge Wilson 	case POOL_INITIALIZE_SUSPEND:
5558*094e47e9SGeorge Wilson 		if (ztest_opts.zo_verbose >= 4) {
5559*094e47e9SGeorge Wilson 			(void) printf("Suspend initialize %s", path);
5560*094e47e9SGeorge Wilson 			if (!active)
5561*094e47e9SGeorge Wilson 				(void) printf(" failed (no initialize active)");
5562*094e47e9SGeorge Wilson 			(void) printf("\n");
5563*094e47e9SGeorge Wilson 		}
5564*094e47e9SGeorge Wilson 		break;
5565*094e47e9SGeorge Wilson 	}
5566*094e47e9SGeorge Wilson 	free(path);
5567*094e47e9SGeorge Wilson 	mutex_exit(&ztest_vdev_lock);
5568*094e47e9SGeorge Wilson }
5569*094e47e9SGeorge Wilson 
5570b24ab676SJeff Bonwick /*
5571b24ab676SJeff Bonwick  * Verify pool integrity by running zdb.
5572b24ab676SJeff Bonwick  */
5573fa9e4066Sahrens static void
5574b24ab676SJeff Bonwick ztest_run_zdb(char *pool)
5575fa9e4066Sahrens {
5576fa9e4066Sahrens 	int status;
5577fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5578fa9e4066Sahrens 	char zbuf[1024];
5579fa9e4066Sahrens 	char *bin;
55808654d025Sperrin 	char *ztest;
55818654d025Sperrin 	char *isa;
55828654d025Sperrin 	int isalen;
5583fa9e4066Sahrens 	FILE *fp;
5584fa9e4066Sahrens 
5585fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
5586fa9e4066Sahrens 
5587fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5588fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
55898654d025Sperrin 	ztest = strstr(bin, "/ztest");
55908654d025Sperrin 	isa = bin + 8;
55918654d025Sperrin 	isalen = ztest - isa;
55928654d025Sperrin 	isa = strdup(isa);
5593fa9e4066Sahrens 	/* LINTED */
55943a57275aSck 	(void) sprintf(bin,
5595c3c65d17SPavel Zakharov 	    "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
55968654d025Sperrin 	    isalen,
55978654d025Sperrin 	    isa,
5598420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 3 ? "s" : "",
5599420dfc95SChris Siden 	    ztest_opts.zo_verbose >= 4 ? "v" : "",
5600f0ba89beSJeff Bonwick 	    spa_config_path,
560188b7b0f2SMatthew Ahrens 	    pool);
56028654d025Sperrin 	free(isa);
5603fa9e4066Sahrens 
5604420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 5)
5605fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
5606fa9e4066Sahrens 
5607fa9e4066Sahrens 	fp = popen(zdb, "r");
5608fa9e4066Sahrens 
5609fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5610420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3)
5611fa9e4066Sahrens 			(void) printf("%s", zbuf);
5612fa9e4066Sahrens 
5613fa9e4066Sahrens 	status = pclose(fp);
5614fa9e4066Sahrens 
5615fa9e4066Sahrens 	if (status == 0)
5616fa9e4066Sahrens 		return;
5617fa9e4066Sahrens 
5618fa9e4066Sahrens 	ztest_dump_core = 0;
5619fa9e4066Sahrens 	if (WIFEXITED(status))
5620fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5621fa9e4066Sahrens 	else
5622fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5623fa9e4066Sahrens }
5624fa9e4066Sahrens 
5625fa9e4066Sahrens static void
5626fa9e4066Sahrens ztest_walk_pool_directory(char *header)
5627fa9e4066Sahrens {
5628fa9e4066Sahrens 	spa_t *spa = NULL;
5629fa9e4066Sahrens 
5630420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5631fa9e4066Sahrens 		(void) printf("%s\n", header);
5632fa9e4066Sahrens 
5633fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
5634fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
5635420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 6)
5636fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
5637fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
5638fa9e4066Sahrens }
5639fa9e4066Sahrens 
5640fa9e4066Sahrens static void
5641fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
5642fa9e4066Sahrens {
56438ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
5644fa9e4066Sahrens 	uint64_t pool_guid;
5645fa9e4066Sahrens 	spa_t *spa;
56463b2aab18SMatthew Ahrens 	int error;
5647fa9e4066Sahrens 
5648420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5649fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
5650fa9e4066Sahrens 		    oldname, newname);
5651fa9e4066Sahrens 	}
5652fa9e4066Sahrens 
5653fa9e4066Sahrens 	/*
5654fa9e4066Sahrens 	 * Clean up from previous runs.
5655fa9e4066Sahrens 	 */
5656fa9e4066Sahrens 	(void) spa_destroy(newname);
5657fa9e4066Sahrens 
5658fa9e4066Sahrens 	/*
5659fa9e4066Sahrens 	 * Get the pool's configuration and guid.
5660fa9e4066Sahrens 	 */
5661b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5662fa9e4066Sahrens 
56638ad4d6ddSJeff Bonwick 	/*
56648ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
56658ad4d6ddSJeff Bonwick 	 */
56668ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
56673f9d6ad7SLin Ling 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
56688ad4d6ddSJeff Bonwick 
5669fa9e4066Sahrens 	pool_guid = spa_guid(spa);
5670fa9e4066Sahrens 	spa_close(spa, FTAG);
5671fa9e4066Sahrens 
5672fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
5673fa9e4066Sahrens 
5674fa9e4066Sahrens 	/*
5675fa9e4066Sahrens 	 * Export it.
5676fa9e4066Sahrens 	 */
5677b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5678fa9e4066Sahrens 
5679fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
5680fa9e4066Sahrens 
56818ad4d6ddSJeff Bonwick 	/*
56828ad4d6ddSJeff Bonwick 	 * Try to import it.
56838ad4d6ddSJeff Bonwick 	 */
56848ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
56858ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
56868ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
56878ad4d6ddSJeff Bonwick 
5688fa9e4066Sahrens 	/*
5689fa9e4066Sahrens 	 * Import it under the new name.
5690fa9e4066Sahrens 	 */
56913b2aab18SMatthew Ahrens 	error = spa_import(newname, config, NULL, 0);
56923b2aab18SMatthew Ahrens 	if (error != 0) {
56933b2aab18SMatthew Ahrens 		dump_nvlist(config, 0);
56943b2aab18SMatthew Ahrens 		fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
56953b2aab18SMatthew Ahrens 		    oldname, newname, error);
56963b2aab18SMatthew Ahrens 	}
5697fa9e4066Sahrens 
5698fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
5699fa9e4066Sahrens 
5700fa9e4066Sahrens 	/*
5701fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
5702fa9e4066Sahrens 	 */
57034b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5704fa9e4066Sahrens 
5705fa9e4066Sahrens 	/*
5706fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
5707fa9e4066Sahrens 	 */
57084b964adaSGeorge Wilson 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5709fa9e4066Sahrens 
5710fa9e4066Sahrens 	/*
5711fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
5712fa9e4066Sahrens 	 */
5713b24ab676SJeff Bonwick 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5714fa9e4066Sahrens 
5715fa9e4066Sahrens 	/*
5716fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
5717fa9e4066Sahrens 	 */
5718b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5719fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
5720fa9e4066Sahrens 	spa_close(spa, FTAG);
5721fa9e4066Sahrens 
5722fa9e4066Sahrens 	nvlist_free(config);
5723fa9e4066Sahrens }
5724fa9e4066Sahrens 
57258ad4d6ddSJeff Bonwick static void
57268ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
57278ad4d6ddSJeff Bonwick {
5728420dfc95SChris Siden 	if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5729b24ab676SJeff Bonwick 		(void) printf("resuming from suspended state\n");
5730b24ab676SJeff Bonwick 	spa_vdev_state_enter(spa, SCL_NONE);
5731b24ab676SJeff Bonwick 	vdev_clear(spa, NULL);
5732b24ab676SJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
5733b24ab676SJeff Bonwick 	(void) zio_resume(spa);
57348ad4d6ddSJeff Bonwick }
57358ad4d6ddSJeff Bonwick 
57360a4e9518Sgw static void *
57378ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
57380a4e9518Sgw {
5739e14bb325SJeff Bonwick 	spa_t *spa = arg;
57400a4e9518Sgw 
57410a4e9518Sgw 	while (!ztest_exiting) {
5742b24ab676SJeff Bonwick 		if (spa_suspended(spa))
5743b24ab676SJeff Bonwick 			ztest_resume(spa);
5744b24ab676SJeff Bonwick 		(void) poll(NULL, 0, 100);
5745dcbf3bd6SGeorge Wilson 
5746dcbf3bd6SGeorge Wilson 		/*
5747dcbf3bd6SGeorge Wilson 		 * Periodically change the zfs_compressed_arc_enabled setting.
5748dcbf3bd6SGeorge Wilson 		 */
5749dcbf3bd6SGeorge Wilson 		if (ztest_random(10) == 0)
5750dcbf3bd6SGeorge Wilson 			zfs_compressed_arc_enabled = ztest_random(2);
5751770499e1SDan Kimmel 
5752770499e1SDan Kimmel 		/*
5753770499e1SDan Kimmel 		 * Periodically change the zfs_abd_scatter_enabled setting.
5754770499e1SDan Kimmel 		 */
5755770499e1SDan Kimmel 		if (ztest_random(10) == 0)
5756770499e1SDan Kimmel 			zfs_abd_scatter_enabled = ztest_random(2);
57570a4e9518Sgw 	}
57580a4e9518Sgw 	return (NULL);
57590a4e9518Sgw }
57600a4e9518Sgw 
5761b24ab676SJeff Bonwick static void *
5762b24ab676SJeff Bonwick ztest_deadman_thread(void *arg)
5763b24ab676SJeff Bonwick {
5764b24ab676SJeff Bonwick 	ztest_shared_t *zs = arg;
57652c1e2b44SGeorge Wilson 	spa_t *spa = ztest_spa;
57662c1e2b44SGeorge Wilson 	hrtime_t delta, total = 0;
5767b24ab676SJeff Bonwick 
57682c1e2b44SGeorge Wilson 	for (;;) {
576969962b56SMatthew Ahrens 		delta = zs->zs_thread_stop - zs->zs_thread_start +
577069962b56SMatthew Ahrens 		    MSEC2NSEC(zfs_deadman_synctime_ms);
5771b24ab676SJeff Bonwick 
577269962b56SMatthew Ahrens 		(void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5773b24ab676SJeff Bonwick 
57742c1e2b44SGeorge Wilson 		/*
57752c1e2b44SGeorge Wilson 		 * If the pool is suspended then fail immediately. Otherwise,
57762c1e2b44SGeorge Wilson 		 * check to see if the pool is making any progress. If
57772c1e2b44SGeorge Wilson 		 * vdev_deadman() discovers that there hasn't been any recent
57782c1e2b44SGeorge Wilson 		 * I/Os then it will end up aborting the tests.
57792c1e2b44SGeorge Wilson 		 */
57800713e232SGeorge Wilson 		if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
57812c1e2b44SGeorge Wilson 			fatal(0, "aborting test after %llu seconds because "
57822c1e2b44SGeorge Wilson 			    "pool has transitioned to a suspended state.",
578369962b56SMatthew Ahrens 			    zfs_deadman_synctime_ms / 1000);
57842c1e2b44SGeorge Wilson 			return (NULL);
57852c1e2b44SGeorge Wilson 		}
57862c1e2b44SGeorge Wilson 		vdev_deadman(spa->spa_root_vdev);
5787b24ab676SJeff Bonwick 
578869962b56SMatthew Ahrens 		total += zfs_deadman_synctime_ms/1000;
57892c1e2b44SGeorge Wilson 		(void) printf("ztest has been running for %lld seconds\n",
57902c1e2b44SGeorge Wilson 		    total);
57912c1e2b44SGeorge Wilson 	}
5792b24ab676SJeff Bonwick }
5793b24ab676SJeff Bonwick 
5794b24ab676SJeff Bonwick static void
5795420dfc95SChris Siden ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5796b24ab676SJeff Bonwick {
5797420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5798420dfc95SChris Siden 	ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5799b24ab676SJeff Bonwick 	hrtime_t functime = gethrtime();
5800b24ab676SJeff Bonwick 
5801b24ab676SJeff Bonwick 	for (int i = 0; i < zi->zi_iters; i++)
5802b24ab676SJeff Bonwick 		zi->zi_func(zd, id);
5803b24ab676SJeff Bonwick 
5804b24ab676SJeff Bonwick 	functime = gethrtime() - functime;
5805b24ab676SJeff Bonwick 
5806420dfc95SChris Siden 	atomic_add_64(&zc->zc_count, 1);
5807420dfc95SChris Siden 	atomic_add_64(&zc->zc_time, functime);
5808b24ab676SJeff Bonwick 
5809420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4) {
5810b24ab676SJeff Bonwick 		Dl_info dli;
5811b24ab676SJeff Bonwick 		(void) dladdr((void *)zi->zi_func, &dli);
5812b24ab676SJeff Bonwick 		(void) printf("%6.2f sec in %s\n",
5813b24ab676SJeff Bonwick 		    (double)functime / NANOSEC, dli.dli_sname);
5814b24ab676SJeff Bonwick 	}
5815b24ab676SJeff Bonwick }
5816b24ab676SJeff Bonwick 
5817fa9e4066Sahrens static void *
5818fa9e4066Sahrens ztest_thread(void *arg)
5819fa9e4066Sahrens {
5820420dfc95SChris Siden 	int rand;
5821b24ab676SJeff Bonwick 	uint64_t id = (uintptr_t)arg;
5822fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
5823b24ab676SJeff Bonwick 	uint64_t call_next;
5824b24ab676SJeff Bonwick 	hrtime_t now;
5825fa9e4066Sahrens 	ztest_info_t *zi;
5826420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
5827fa9e4066Sahrens 
5828b24ab676SJeff Bonwick 	while ((now = gethrtime()) < zs->zs_thread_stop) {
5829fa9e4066Sahrens 		/*
5830fa9e4066Sahrens 		 * See if it's time to force a crash.
5831fa9e4066Sahrens 		 */
5832b24ab676SJeff Bonwick 		if (now > zs->zs_thread_kill)
5833b24ab676SJeff Bonwick 			ztest_kill(zs);
5834fa9e4066Sahrens 
5835fa9e4066Sahrens 		/*
5836b24ab676SJeff Bonwick 		 * If we're getting ENOSPC with some regularity, stop.
5837fa9e4066Sahrens 		 */
5838b24ab676SJeff Bonwick 		if (zs->zs_enospc_count > 10)
5839b24ab676SJeff Bonwick 			break;
5840fa9e4066Sahrens 
5841fa9e4066Sahrens 		/*
5842b24ab676SJeff Bonwick 		 * Pick a random function to execute.
5843fa9e4066Sahrens 		 */
5844420dfc95SChris Siden 		rand = ztest_random(ZTEST_FUNCS);
5845420dfc95SChris Siden 		zi = &ztest_info[rand];
5846420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5847420dfc95SChris Siden 		call_next = zc->zc_next;
5848b24ab676SJeff Bonwick 
5849b24ab676SJeff Bonwick 		if (now >= call_next &&
5850420dfc95SChris Siden 		    atomic_cas_64(&zc->zc_next, call_next, call_next +
5851420dfc95SChris Siden 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5852420dfc95SChris Siden 			ztest_execute(rand, zi, id);
5853420dfc95SChris Siden 		}
5854b24ab676SJeff Bonwick 	}
5855fa9e4066Sahrens 
5856b24ab676SJeff Bonwick 	return (NULL);
5857b24ab676SJeff Bonwick }
5858fa9e4066Sahrens 
5859b24ab676SJeff Bonwick static void
5860b24ab676SJeff Bonwick ztest_dataset_name(char *dsname, char *pool, int d)
5861b24ab676SJeff Bonwick {
58629adfa60dSMatthew Ahrens 	(void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5863b24ab676SJeff Bonwick }
5864fa9e4066Sahrens 
5865b24ab676SJeff Bonwick static void
5866420dfc95SChris Siden ztest_dataset_destroy(int d)
5867b24ab676SJeff Bonwick {
58689adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
5869fa9e4066Sahrens 
5870420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5871fa9e4066Sahrens 
5872420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
5873b24ab676SJeff Bonwick 		(void) printf("Destroying %s to free up space\n", name);
5874fa9e4066Sahrens 
5875b24ab676SJeff Bonwick 	/*
5876b24ab676SJeff Bonwick 	 * Cleanup any non-standard clones and snapshots.  In general,
5877b24ab676SJeff Bonwick 	 * ztest thread t operates on dataset (t % zopt_datasets),
5878b24ab676SJeff Bonwick 	 * so there may be more than one thing to clean up.
5879b24ab676SJeff Bonwick 	 */
5880420dfc95SChris Siden 	for (int t = d; t < ztest_opts.zo_threads;
5881420dfc95SChris Siden 	    t += ztest_opts.zo_datasets) {
5882b24ab676SJeff Bonwick 		ztest_dsl_dataset_cleanup(name, t);
5883420dfc95SChris Siden 	}
5884fa9e4066Sahrens 
5885b24ab676SJeff Bonwick 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5886b24ab676SJeff Bonwick 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5887b24ab676SJeff Bonwick }
5888b24ab676SJeff Bonwick 
5889b24ab676SJeff Bonwick static void
5890b24ab676SJeff Bonwick ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5891b24ab676SJeff Bonwick {
5892b24ab676SJeff Bonwick 	uint64_t usedobjs, dirobjs, scratch;
5893b24ab676SJeff Bonwick 
5894b24ab676SJeff Bonwick 	/*
5895b24ab676SJeff Bonwick 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
5896b24ab676SJeff Bonwick 	 * Therefore, the number of objects in use should equal the
5897b24ab676SJeff Bonwick 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5898b24ab676SJeff Bonwick 	 * If not, we have an object leak.
5899b24ab676SJeff Bonwick 	 *
5900b24ab676SJeff Bonwick 	 * Note that we can only check this in ztest_dataset_open(),
5901b24ab676SJeff Bonwick 	 * when the open-context and syncing-context values agree.
5902b24ab676SJeff Bonwick 	 * That's because zap_count() returns the open-context value,
5903b24ab676SJeff Bonwick 	 * while dmu_objset_space() returns the rootbp fill count.
5904b24ab676SJeff Bonwick 	 */
5905b420f3adSRichard Lowe 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5906b24ab676SJeff Bonwick 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5907b24ab676SJeff Bonwick 	ASSERT3U(dirobjs + 1, ==, usedobjs);
5908b24ab676SJeff Bonwick }
5909b24ab676SJeff Bonwick 
5910b24ab676SJeff Bonwick static int
5911420dfc95SChris Siden ztest_dataset_open(int d)
5912b24ab676SJeff Bonwick {
5913420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5914420dfc95SChris Siden 	uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5915b24ab676SJeff Bonwick 	objset_t *os;
5916b24ab676SJeff Bonwick 	zilog_t *zilog;
59179adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
5918b24ab676SJeff Bonwick 	int error;
5919b24ab676SJeff Bonwick 
5920420dfc95SChris Siden 	ztest_dataset_name(name, ztest_opts.zo_pool, d);
5921b24ab676SJeff Bonwick 
5922f06dce2cSAndrew Stormont 	rw_enter(&ztest_name_lock, RW_READER);
5923b24ab676SJeff Bonwick 
592455da60b9SMark J Musante 	error = ztest_dataset_create(name);
5925b24ab676SJeff Bonwick 	if (error == ENOSPC) {
5926f06dce2cSAndrew Stormont 		rw_exit(&ztest_name_lock);
5927b24ab676SJeff Bonwick 		ztest_record_enospc(FTAG);
5928b24ab676SJeff Bonwick 		return (error);
5929fa9e4066Sahrens 	}
5930b24ab676SJeff Bonwick 	ASSERT(error == 0 || error == EEXIST);
5931fa9e4066Sahrens 
59323b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5933f06dce2cSAndrew Stormont 	rw_exit(&ztest_name_lock);
5934b24ab676SJeff Bonwick 
5935420dfc95SChris Siden 	ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5936b24ab676SJeff Bonwick 
5937b24ab676SJeff Bonwick 	zilog = zd->zd_zilog;
5938b24ab676SJeff Bonwick 
5939b24ab676SJeff Bonwick 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5940b24ab676SJeff Bonwick 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
5941b24ab676SJeff Bonwick 		fatal(0, "missing log records: claimed %llu < committed %llu",
5942b24ab676SJeff Bonwick 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
5943b24ab676SJeff Bonwick 
5944b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5945b24ab676SJeff Bonwick 
5946b24ab676SJeff Bonwick 	zil_replay(os, zd, ztest_replay_vector);
5947b24ab676SJeff Bonwick 
5948b24ab676SJeff Bonwick 	ztest_dataset_dirobj_verify(zd);
5949b24ab676SJeff Bonwick 
5950420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 6)
5951b24ab676SJeff Bonwick 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5952b24ab676SJeff Bonwick 		    zd->zd_name,
5953b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_blk_count,
5954b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_parse_lr_count,
5955b24ab676SJeff Bonwick 		    (u_longlong_t)zilog->zl_replaying_seq);
5956b24ab676SJeff Bonwick 
5957b24ab676SJeff Bonwick 	zilog = zil_open(os, ztest_get_data);
5958b24ab676SJeff Bonwick 
5959b24ab676SJeff Bonwick 	if (zilog->zl_replaying_seq != 0 &&
5960b24ab676SJeff Bonwick 	    zilog->zl_replaying_seq < committed_seq)
5961b24ab676SJeff Bonwick 		fatal(0, "missing log records: replayed %llu < committed %llu",
5962b24ab676SJeff Bonwick 		    zilog->zl_replaying_seq, committed_seq);
5963b24ab676SJeff Bonwick 
5964b24ab676SJeff Bonwick 	return (0);
5965b24ab676SJeff Bonwick }
5966b24ab676SJeff Bonwick 
5967b24ab676SJeff Bonwick static void
5968420dfc95SChris Siden ztest_dataset_close(int d)
5969b24ab676SJeff Bonwick {
5970420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[d];
5971b24ab676SJeff Bonwick 
5972b24ab676SJeff Bonwick 	zil_close(zd->zd_zilog);
59733b2aab18SMatthew Ahrens 	dmu_objset_disown(zd->zd_os, zd);
5974b24ab676SJeff Bonwick 
5975b24ab676SJeff Bonwick 	ztest_zd_fini(zd);
5976fa9e4066Sahrens }
5977fa9e4066Sahrens 
5978fa9e4066Sahrens /*
5979fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
5980fa9e4066Sahrens  */
5981fa9e4066Sahrens static void
5982b24ab676SJeff Bonwick ztest_run(ztest_shared_t *zs)
5983fa9e4066Sahrens {
5984b24ab676SJeff Bonwick 	thread_t *tid;
5985fa9e4066Sahrens 	spa_t *spa;
5986e9103aaeSGarrett D'Amore 	objset_t *os;
5987e14bb325SJeff Bonwick 	thread_t resume_tid;
5988b24ab676SJeff Bonwick 	int error;
5989e14bb325SJeff Bonwick 
5990e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
5991fa9e4066Sahrens 
5992fa9e4066Sahrens 	/*
5993b24ab676SJeff Bonwick 	 * Initialize parent/child shared state.
5994fa9e4066Sahrens 	 */
599586714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
5996f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
5997f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
5998fa9e4066Sahrens 
5999b24ab676SJeff Bonwick 	zs->zs_thread_start = gethrtime();
6000420dfc95SChris Siden 	zs->zs_thread_stop =
6001420dfc95SChris Siden 	    zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6002b24ab676SJeff Bonwick 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6003b24ab676SJeff Bonwick 	zs->zs_thread_kill = zs->zs_thread_stop;
6004420dfc95SChris Siden 	if (ztest_random(100) < ztest_opts.zo_killrate) {
6005420dfc95SChris Siden 		zs->zs_thread_kill -=
6006420dfc95SChris Siden 		    ztest_random(ztest_opts.zo_passtime * NANOSEC);
6007420dfc95SChris Siden 	}
6008fa9e4066Sahrens 
6009f06dce2cSAndrew Stormont 	mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6010fa9e4066Sahrens 
6011b24ab676SJeff Bonwick 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6012b24ab676SJeff Bonwick 	    offsetof(ztest_cb_data_t, zcd_node));
6013fa9e4066Sahrens 
60140a4e9518Sgw 	/*
6015e14bb325SJeff Bonwick 	 * Open our pool.
60160a4e9518Sgw 	 */
6017b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
60183b2aab18SMatthew Ahrens 	VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
601930beaff4SGeorge Wilson 	metaslab_preload_limit = ztest_random(20) + 1;
6020420dfc95SChris Siden 	ztest_spa = spa;
6021b24ab676SJeff Bonwick 
6022aab80726SGeorge Wilson 	dmu_objset_stats_t dds;
60233b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
60243b2aab18SMatthew Ahrens 	    DMU_OST_ANY, B_TRUE, FTAG, &os));
6025aab80726SGeorge Wilson 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6026aab80726SGeorge Wilson 	dmu_objset_fast_stat(os, &dds);
6027aab80726SGeorge Wilson 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6028aab80726SGeorge Wilson 	zs->zs_guid = dds.dds_guid;
60293b2aab18SMatthew Ahrens 	dmu_objset_disown(os, FTAG);
6030e9103aaeSGarrett D'Amore 
6031b24ab676SJeff Bonwick 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
60320a4e9518Sgw 
60338ad4d6ddSJeff Bonwick 	/*
60348ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
60358ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
60368ad4d6ddSJeff Bonwick 	 * the only valid replica.
60378ad4d6ddSJeff Bonwick 	 */
60381195e687SMark J Musante 	if (MAXFAULTS() == 0)
60398ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
60408ad4d6ddSJeff Bonwick 	else
60418ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
60428ad4d6ddSJeff Bonwick 
6043fa9e4066Sahrens 	/*
6044e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
6045fa9e4066Sahrens 	 */
60468ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6047e14bb325SJeff Bonwick 	    &resume_tid) == 0);
6048fa9e4066Sahrens 
6049b24ab676SJeff Bonwick 	/*
6050b24ab676SJeff Bonwick 	 * Create a deadman thread to abort() if we hang.
6051b24ab676SJeff Bonwick 	 */
6052b24ab676SJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6053b24ab676SJeff Bonwick 	    NULL) == 0);
6054b24ab676SJeff Bonwick 
6055fa9e4066Sahrens 	/*
605686714001SSerapheim Dimitropoulos 	 * Verify that we can safely inquire about any object,
6057fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
6058fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
6059fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
6060fa9e4066Sahrens 	 */
6061b24ab676SJeff Bonwick 	for (int t = 0; t < 64; t++) {
6062b24ab676SJeff Bonwick 		for (int d = -5; d <= 5; d++) {
6063fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
6064fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
6065ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
6066ea8dc4b6Seschrock 			    error == EINVAL);
6067fa9e4066Sahrens 		}
6068fa9e4066Sahrens 	}
6069fa9e4066Sahrens 
6070fa9e4066Sahrens 	/*
6071b24ab676SJeff Bonwick 	 * If we got any ENOSPC errors on the previous run, destroy something.
6072fa9e4066Sahrens 	 */
6073b24ab676SJeff Bonwick 	if (zs->zs_enospc_count != 0) {
6074420dfc95SChris Siden 		int d = ztest_random(ztest_opts.zo_datasets);
6075420dfc95SChris Siden 		ztest_dataset_destroy(d);
6076b24ab676SJeff Bonwick 	}
6077fa9e4066Sahrens 	zs->zs_enospc_count = 0;
6078fa9e4066Sahrens 
6079420dfc95SChris Siden 	tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6080420dfc95SChris Siden 	    UMEM_NOFAIL);
6081fa9e4066Sahrens 
6082420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 4)
6083fa9e4066Sahrens 		(void) printf("starting main threads...\n");
6084fa9e4066Sahrens 
6085b24ab676SJeff Bonwick 	/*
6086b24ab676SJeff Bonwick 	 * Kick off all the tests that run in parallel.
6087b24ab676SJeff Bonwick 	 */
6088420dfc95SChris Siden 	for (int t = 0; t < ztest_opts.zo_threads; t++) {
6089420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets &&
6090420dfc95SChris Siden 		    ztest_dataset_open(t) != 0)
6091b24ab676SJeff Bonwick 			return;
6092b24ab676SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6093b24ab676SJeff Bonwick 		    THR_BOUND, &tid[t]) == 0);
6094fa9e4066Sahrens 	}
6095fa9e4066Sahrens 
6096b24ab676SJeff Bonwick 	/*
6097b24ab676SJeff Bonwick 	 * Wait for all of the tests to complete.  We go in reverse order
6098b24ab676SJeff Bonwick 	 * so we don't close datasets while threads are still using them.
6099b24ab676SJeff Bonwick 	 */
6100420dfc95SChris Siden 	for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6101b24ab676SJeff Bonwick 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6102420dfc95SChris Siden 		if (t < ztest_opts.zo_datasets)
6103420dfc95SChris Siden 			ztest_dataset_close(t);
6104fa9e4066Sahrens 	}
6105fa9e4066Sahrens 
6106fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
6107fa9e4066Sahrens 
6108b24ab676SJeff Bonwick 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6109b24ab676SJeff Bonwick 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6110b4952e17SGeorge Wilson 	zfs_dbgmsg_print(FTAG);
6111b24ab676SJeff Bonwick 
6112420dfc95SChris Siden 	umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6113b24ab676SJeff Bonwick 
6114b24ab676SJeff Bonwick 	/* Kill the resume thread */
6115b24ab676SJeff Bonwick 	ztest_exiting = B_TRUE;
6116b24ab676SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6117b24ab676SJeff Bonwick 	ztest_resume(spa);
6118b24ab676SJeff Bonwick 
6119b24ab676SJeff Bonwick 	/*
6120b24ab676SJeff Bonwick 	 * Right before closing the pool, kick off a bunch of async I/O;
6121b24ab676SJeff Bonwick 	 * spa_close() should wait for it to complete.
6122b24ab676SJeff Bonwick 	 */
6123a2cdcdd2SPaul Dagnelie 	for (uint64_t object = 1; object < 50; object++) {
6124a2cdcdd2SPaul Dagnelie 		dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6125a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
6126a2cdcdd2SPaul Dagnelie 	}
6127b24ab676SJeff Bonwick 
6128b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6129fa9e4066Sahrens 
6130fa9e4066Sahrens 	/*
6131b24ab676SJeff Bonwick 	 * Verify that we can loop over all pools.
6132fa9e4066Sahrens 	 */
6133b24ab676SJeff Bonwick 	mutex_enter(&spa_namespace_lock);
6134b24ab676SJeff Bonwick 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6135420dfc95SChris Siden 		if (ztest_opts.zo_verbose > 3)
6136b24ab676SJeff Bonwick 			(void) printf("spa_next: found %s\n", spa_name(spa));
6137b24ab676SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
6138b24ab676SJeff Bonwick 
6139b24ab676SJeff Bonwick 	/*
6140b24ab676SJeff Bonwick 	 * Verify that we can export the pool and reimport it under a
6141b24ab676SJeff Bonwick 	 * different name.
6142b24ab676SJeff Bonwick 	 */
6143b24ab676SJeff Bonwick 	if (ztest_random(2) == 0) {
61449adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
61459adfa60dSMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s_import",
6146420dfc95SChris Siden 		    ztest_opts.zo_pool);
6147420dfc95SChris Siden 		ztest_spa_import_export(ztest_opts.zo_pool, name);
6148420dfc95SChris Siden 		ztest_spa_import_export(name, ztest_opts.zo_pool);
6149b24ab676SJeff Bonwick 	}
6150b24ab676SJeff Bonwick 
6151b24ab676SJeff Bonwick 	kernel_fini();
615226cb54a9SGeorge Wilson 
615326cb54a9SGeorge Wilson 	list_destroy(&zcl.zcl_callbacks);
615426cb54a9SGeorge Wilson 
6155f06dce2cSAndrew Stormont 	mutex_destroy(&zcl.zcl_callbacks_lock);
615626cb54a9SGeorge Wilson 
6157f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6158f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
615986714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6160b24ab676SJeff Bonwick }
6161b24ab676SJeff Bonwick 
6162b24ab676SJeff Bonwick static void
6163420dfc95SChris Siden ztest_freeze(void)
6164b24ab676SJeff Bonwick {
6165420dfc95SChris Siden 	ztest_ds_t *zd = &ztest_ds[0];
6166b24ab676SJeff Bonwick 	spa_t *spa;
61672215e990SMark J Musante 	int numloops = 0;
6168b24ab676SJeff Bonwick 
6169420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 3)
6170b24ab676SJeff Bonwick 		(void) printf("testing spa_freeze()...\n");
6171718d718aSGeorge Wilson 
6172b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6173b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6174b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
617580901aeaSGeorge Wilson 	ztest_spa = spa;
6176718d718aSGeorge Wilson 
6177b24ab676SJeff Bonwick 	/*
6178b24ab676SJeff Bonwick 	 * Force the first log block to be transactionally allocated.
6179b24ab676SJeff Bonwick 	 * We have to do this before we freeze the pool -- otherwise
6180b24ab676SJeff Bonwick 	 * the log chain won't be anchored.
6181b24ab676SJeff Bonwick 	 */
6182b24ab676SJeff Bonwick 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6183b24ab676SJeff Bonwick 		ztest_dmu_object_alloc_free(zd, 0);
61845002558fSNeil Perrin 		zil_commit(zd->zd_zilog, 0);
6185fa9e4066Sahrens 	}
6186fa9e4066Sahrens 
6187ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
6188fa9e4066Sahrens 
6189b24ab676SJeff Bonwick 	/*
6190b24ab676SJeff Bonwick 	 * Freeze the pool.  This stops spa_sync() from doing anything,
6191b24ab676SJeff Bonwick 	 * so that the only way to record changes from now on is the ZIL.
6192b24ab676SJeff Bonwick 	 */
6193b24ab676SJeff Bonwick 	spa_freeze(spa);
6194e14bb325SJeff Bonwick 
61952a104a52SAlex Reece 	/*
61962a104a52SAlex Reece 	 * Because it is hard to predict how much space a write will actually
61972a104a52SAlex Reece 	 * require beforehand, we leave ourselves some fudge space to write over
61982a104a52SAlex Reece 	 * capacity.
61992a104a52SAlex Reece 	 */
62002a104a52SAlex Reece 	uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
62012a104a52SAlex Reece 
6202b24ab676SJeff Bonwick 	/*
6203b24ab676SJeff Bonwick 	 * Run tests that generate log records but don't alter the pool config
6204b24ab676SJeff Bonwick 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6205b24ab676SJeff Bonwick 	 * We do a txg_wait_synced() after each iteration to force the txg
6206b24ab676SJeff Bonwick 	 * to increase well beyond the last synced value in the uberblock.
6207b24ab676SJeff Bonwick 	 * The ZIL should be OK with that.
62082a104a52SAlex Reece 	 *
62092a104a52SAlex Reece 	 * Run a random number of times less than zo_maxloops and ensure we do
62102a104a52SAlex Reece 	 * not run out of space on the pool.
6211b24ab676SJeff Bonwick 	 */
6212420dfc95SChris Siden 	while (ztest_random(10) != 0 &&
62132a104a52SAlex Reece 	    numloops++ < ztest_opts.zo_maxloops &&
62142a104a52SAlex Reece 	    metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
62152a104a52SAlex Reece 		ztest_od_t od;
62162a104a52SAlex Reece 		ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
62172a104a52SAlex Reece 		VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
62182a104a52SAlex Reece 		ztest_io(zd, od.od_object,
62192a104a52SAlex Reece 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6220b24ab676SJeff Bonwick 		txg_wait_synced(spa_get_dsl(spa), 0);
6221b24ab676SJeff Bonwick 	}
6222e14bb325SJeff Bonwick 
6223fa9e4066Sahrens 	/*
6224b24ab676SJeff Bonwick 	 * Commit all of the changes we just generated.
6225fa9e4066Sahrens 	 */
62265002558fSNeil Perrin 	zil_commit(zd->zd_zilog, 0);
6227b24ab676SJeff Bonwick 	txg_wait_synced(spa_get_dsl(spa), 0);
6228fa9e4066Sahrens 
6229b24ab676SJeff Bonwick 	/*
6230b24ab676SJeff Bonwick 	 * Close our dataset and close the pool.
6231b24ab676SJeff Bonwick 	 */
6232420dfc95SChris Siden 	ztest_dataset_close(0);
6233e05725b1Sbonwick 	spa_close(spa, FTAG);
6234b24ab676SJeff Bonwick 	kernel_fini();
6235e05725b1Sbonwick 
6236b24ab676SJeff Bonwick 	/*
6237b24ab676SJeff Bonwick 	 * Open and close the pool and dataset to induce log replay.
6238b24ab676SJeff Bonwick 	 */
6239b24ab676SJeff Bonwick 	kernel_init(FREAD | FWRITE);
6240b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6241ce636f8bSMatthew Ahrens 	ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6242b420f3adSRichard Lowe 	VERIFY3U(0, ==, ztest_dataset_open(0));
6243420dfc95SChris Siden 	ztest_dataset_close(0);
6244dfbb9432SGeorge Wilson 
6245dfbb9432SGeorge Wilson 	ztest_spa = spa;
6246dfbb9432SGeorge Wilson 	txg_wait_synced(spa_get_dsl(spa), 0);
6247dfbb9432SGeorge Wilson 	ztest_reguid(NULL, 0);
6248dfbb9432SGeorge Wilson 
6249b24ab676SJeff Bonwick 	spa_close(spa, FTAG);
6250fa9e4066Sahrens 	kernel_fini();
6251fa9e4066Sahrens }
6252fa9e4066Sahrens 
6253fa9e4066Sahrens void
6254fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
6255fa9e4066Sahrens {
6256fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
6257fa9e4066Sahrens 	hrtime_t m = s / 60;
6258fa9e4066Sahrens 	hrtime_t h = m / 60;
6259fa9e4066Sahrens 	hrtime_t d = h / 24;
6260fa9e4066Sahrens 
6261fa9e4066Sahrens 	s -= m * 60;
6262fa9e4066Sahrens 	m -= h * 60;
6263fa9e4066Sahrens 	h -= d * 24;
6264fa9e4066Sahrens 
6265fa9e4066Sahrens 	timebuf[0] = '\0';
6266fa9e4066Sahrens 
6267fa9e4066Sahrens 	if (d)
6268fa9e4066Sahrens 		(void) sprintf(timebuf,
6269fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
6270fa9e4066Sahrens 	else if (h)
6271fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6272fa9e4066Sahrens 	else if (m)
6273fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
6274fa9e4066Sahrens 	else
6275fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
6276fa9e4066Sahrens }
6277fa9e4066Sahrens 
62781195e687SMark J Musante static nvlist_t *
62791195e687SMark J Musante make_random_props()
62801195e687SMark J Musante {
62811195e687SMark J Musante 	nvlist_t *props;
62821195e687SMark J Musante 
62831195e687SMark J Musante 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6284ad135b5dSChristopher Siden 	if (ztest_random(2) == 0)
6285ad135b5dSChristopher Siden 		return (props);
62861195e687SMark J Musante 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
62871195e687SMark J Musante 
62881195e687SMark J Musante 	return (props);
62891195e687SMark J Musante }
62901195e687SMark J Musante 
6291fa9e4066Sahrens /*
6292fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
6293b24ab676SJeff Bonwick  * Then test spa_freeze() functionality.
6294fa9e4066Sahrens  */
6295fa9e4066Sahrens static void
6296b24ab676SJeff Bonwick ztest_init(ztest_shared_t *zs)
6297fa9e4066Sahrens {
6298fa9e4066Sahrens 	spa_t *spa;
62991195e687SMark J Musante 	nvlist_t *nvroot, *props;
6300fa9e4066Sahrens 
6301f06dce2cSAndrew Stormont 	mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
630286714001SSerapheim Dimitropoulos 	mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6303f06dce2cSAndrew Stormont 	rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6304b24ab676SJeff Bonwick 
6305fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
6306fa9e4066Sahrens 
6307fa9e4066Sahrens 	/*
6308fa9e4066Sahrens 	 * Create the storage pool.
6309fa9e4066Sahrens 	 */
6310420dfc95SChris Siden 	(void) spa_destroy(ztest_opts.zo_pool);
631188ecc943SGeorge Wilson 	ztest_shared->zs_vdev_next_leaf = 0;
63121195e687SMark J Musante 	zs->zs_splits = 0;
6313420dfc95SChris Siden 	zs->zs_mirrors = ztest_opts.zo_mirrors;
631425345e46SGeorge Wilson 	nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6315420dfc95SChris Siden 	    0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
63161195e687SMark J Musante 	props = make_random_props();
6317ad135b5dSChristopher Siden 	for (int i = 0; i < SPA_FEATURES; i++) {
6318ad135b5dSChristopher Siden 		char buf[1024];
6319ad135b5dSChristopher Siden 		(void) snprintf(buf, sizeof (buf), "feature@%s",
6320ad135b5dSChristopher Siden 		    spa_feature_table[i].fi_uname);
6321b420f3adSRichard Lowe 		VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6322ad135b5dSChristopher Siden 	}
6323b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6324fa9e4066Sahrens 	nvlist_free(nvroot);
63251df447ebSRichard Yao 	nvlist_free(props);
6326fa9e4066Sahrens 
6327b420f3adSRichard Lowe 	VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6328420dfc95SChris Siden 	zs->zs_metaslab_sz =
6329420dfc95SChris Siden 	    1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6330ad135b5dSChristopher Siden 
6331fa9e4066Sahrens 	spa_close(spa, FTAG);
6332fa9e4066Sahrens 
6333fa9e4066Sahrens 	kernel_fini();
6334b24ab676SJeff Bonwick 
6335420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
6336420dfc95SChris Siden 
6337420dfc95SChris Siden 	ztest_freeze();
6338420dfc95SChris Siden 
6339420dfc95SChris Siden 	ztest_run_zdb(ztest_opts.zo_pool);
6340420dfc95SChris Siden 
6341f06dce2cSAndrew Stormont 	rw_destroy(&ztest_name_lock);
6342f06dce2cSAndrew Stormont 	mutex_destroy(&ztest_vdev_lock);
634386714001SSerapheim Dimitropoulos 	mutex_destroy(&ztest_checkpoint_lock);
6344420dfc95SChris Siden }
6345420dfc95SChris Siden 
6346420dfc95SChris Siden static void
6347741652b0SEtienne Dechamps setup_data_fd(void)
6348420dfc95SChris Siden {
6349741652b0SEtienne Dechamps 	static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6350420dfc95SChris Siden 
6351741652b0SEtienne Dechamps 	ztest_fd_data = mkstemp(ztest_name_data);
6352741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_data, >=, 0);
6353741652b0SEtienne Dechamps 	(void) unlink(ztest_name_data);
6354420dfc95SChris Siden }
6355420dfc95SChris Siden 
6356741652b0SEtienne Dechamps 
6357ad135b5dSChristopher Siden static int
6358ad135b5dSChristopher Siden shared_data_size(ztest_shared_hdr_t *hdr)
6359ad135b5dSChristopher Siden {
6360ad135b5dSChristopher Siden 	int size;
6361ad135b5dSChristopher Siden 
6362ad135b5dSChristopher Siden 	size = hdr->zh_hdr_size;
6363ad135b5dSChristopher Siden 	size += hdr->zh_opts_size;
6364ad135b5dSChristopher Siden 	size += hdr->zh_size;
6365ad135b5dSChristopher Siden 	size += hdr->zh_stats_size * hdr->zh_stats_count;
6366ad135b5dSChristopher Siden 	size += hdr->zh_ds_size * hdr->zh_ds_count;
6367ad135b5dSChristopher Siden 
6368ad135b5dSChristopher Siden 	return (size);
6369ad135b5dSChristopher Siden }
6370ad135b5dSChristopher Siden 
6371420dfc95SChris Siden static void
6372420dfc95SChris Siden setup_hdr(void)
6373420dfc95SChris Siden {
6374ad135b5dSChristopher Siden 	int size;
6375420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6376420dfc95SChris Siden 
6377420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6378741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6379420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6380420dfc95SChris Siden 
6381741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6382ad135b5dSChristopher Siden 
6383420dfc95SChris Siden 	hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6384420dfc95SChris Siden 	hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6385420dfc95SChris Siden 	hdr->zh_size = sizeof (ztest_shared_t);
6386420dfc95SChris Siden 	hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6387420dfc95SChris Siden 	hdr->zh_stats_count = ZTEST_FUNCS;
6388420dfc95SChris Siden 	hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6389420dfc95SChris Siden 	hdr->zh_ds_count = ztest_opts.zo_datasets;
6390420dfc95SChris Siden 
6391ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6392741652b0SEtienne Dechamps 	VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6393ad135b5dSChristopher Siden 
6394420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6395420dfc95SChris Siden }
6396420dfc95SChris Siden 
6397420dfc95SChris Siden static void
6398420dfc95SChris Siden setup_data(void)
6399420dfc95SChris Siden {
6400420dfc95SChris Siden 	int size, offset;
6401420dfc95SChris Siden 	ztest_shared_hdr_t *hdr;
6402420dfc95SChris Siden 	uint8_t *buf;
6403420dfc95SChris Siden 
6404420dfc95SChris Siden 	hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6405741652b0SEtienne Dechamps 	    PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6406420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6407420dfc95SChris Siden 
6408ad135b5dSChristopher Siden 	size = shared_data_size(hdr);
6409420dfc95SChris Siden 
6410420dfc95SChris Siden 	(void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6411420dfc95SChris Siden 	hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6412741652b0SEtienne Dechamps 	    PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6413420dfc95SChris Siden 	ASSERT(hdr != MAP_FAILED);
6414420dfc95SChris Siden 	buf = (uint8_t *)hdr;
6415420dfc95SChris Siden 
6416420dfc95SChris Siden 	offset = hdr->zh_hdr_size;
6417420dfc95SChris Siden 	ztest_shared_opts = (void *)&buf[offset];
6418420dfc95SChris Siden 	offset += hdr->zh_opts_size;
6419420dfc95SChris Siden 	ztest_shared = (void *)&buf[offset];
6420420dfc95SChris Siden 	offset += hdr->zh_size;
6421420dfc95SChris Siden 	ztest_shared_callstate = (void *)&buf[offset];
6422420dfc95SChris Siden 	offset += hdr->zh_stats_size * hdr->zh_stats_count;
6423420dfc95SChris Siden 	ztest_shared_ds = (void *)&buf[offset];
6424420dfc95SChris Siden }
6425420dfc95SChris Siden 
6426420dfc95SChris Siden static boolean_t
6427420dfc95SChris Siden exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6428420dfc95SChris Siden {
6429420dfc95SChris Siden 	pid_t pid;
6430420dfc95SChris Siden 	int status;
6431741652b0SEtienne Dechamps 	char *cmdbuf = NULL;
6432420dfc95SChris Siden 
6433420dfc95SChris Siden 	pid = fork();
6434420dfc95SChris Siden 
6435420dfc95SChris Siden 	if (cmd == NULL) {
6436741652b0SEtienne Dechamps 		cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6437741652b0SEtienne Dechamps 		(void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6438420dfc95SChris Siden 		cmd = cmdbuf;
6439420dfc95SChris Siden 	}
6440b24ab676SJeff Bonwick 
6441420dfc95SChris Siden 	if (pid == -1)
6442420dfc95SChris Siden 		fatal(1, "fork failed");
6443b24ab676SJeff Bonwick 
6444420dfc95SChris Siden 	if (pid == 0) {	/* child */
6445420dfc95SChris Siden 		char *emptyargv[2] = { cmd, NULL };
6446741652b0SEtienne Dechamps 		char fd_data_str[12];
644726cb54a9SGeorge Wilson 
6448420dfc95SChris Siden 		struct rlimit rl = { 1024, 1024 };
6449420dfc95SChris Siden 		(void) setrlimit(RLIMIT_NOFILE, &rl);
6450741652b0SEtienne Dechamps 
6451741652b0SEtienne Dechamps 		(void) close(ztest_fd_rand);
6452741652b0SEtienne Dechamps 		VERIFY3U(11, >=,
6453741652b0SEtienne Dechamps 		    snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6454741652b0SEtienne Dechamps 		VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6455741652b0SEtienne Dechamps 
6456420dfc95SChris Siden 		(void) enable_extended_FILE_stdio(-1, -1);
6457420dfc95SChris Siden 		if (libpath != NULL)
6458420dfc95SChris Siden 			VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6459420dfc95SChris Siden 		(void) execv(cmd, emptyargv);
6460420dfc95SChris Siden 		ztest_dump_core = B_FALSE;
6461420dfc95SChris Siden 		fatal(B_TRUE, "exec failed: %s", cmd);
6462420dfc95SChris Siden 	}
6463420dfc95SChris Siden 
6464741652b0SEtienne Dechamps 	if (cmdbuf != NULL) {
6465741652b0SEtienne Dechamps 		umem_free(cmdbuf, MAXPATHLEN);
6466741652b0SEtienne Dechamps 		cmd = NULL;
6467741652b0SEtienne Dechamps 	}
6468741652b0SEtienne Dechamps 
6469420dfc95SChris Siden 	while (waitpid(pid, &status, 0) != pid)
6470420dfc95SChris Siden 		continue;
6471420dfc95SChris Siden 	if (statusp != NULL)
6472420dfc95SChris Siden 		*statusp = status;
6473420dfc95SChris Siden 
6474420dfc95SChris Siden 	if (WIFEXITED(status)) {
6475420dfc95SChris Siden 		if (WEXITSTATUS(status) != 0) {
6476420dfc95SChris Siden 			(void) fprintf(stderr, "child exited with code %d\n",
6477420dfc95SChris Siden 			    WEXITSTATUS(status));
6478420dfc95SChris Siden 			exit(2);
6479420dfc95SChris Siden 		}
6480420dfc95SChris Siden 		return (B_FALSE);
6481420dfc95SChris Siden 	} else if (WIFSIGNALED(status)) {
6482420dfc95SChris Siden 		if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6483420dfc95SChris Siden 			(void) fprintf(stderr, "child died with signal %d\n",
6484420dfc95SChris Siden 			    WTERMSIG(status));
6485420dfc95SChris Siden 			exit(3);
6486420dfc95SChris Siden 		}
6487420dfc95SChris Siden 		return (B_TRUE);
6488420dfc95SChris Siden 	} else {
6489420dfc95SChris Siden 		(void) fprintf(stderr, "something strange happened to child\n");
6490420dfc95SChris Siden 		exit(4);
6491420dfc95SChris Siden 		/* NOTREACHED */
6492420dfc95SChris Siden 	}
6493420dfc95SChris Siden }
6494420dfc95SChris Siden 
6495420dfc95SChris Siden static void
6496420dfc95SChris Siden ztest_run_init(void)
6497420dfc95SChris Siden {
6498420dfc95SChris Siden 	ztest_shared_t *zs = ztest_shared;
6499420dfc95SChris Siden 
6500420dfc95SChris Siden 	ASSERT(ztest_opts.zo_init != 0);
6501420dfc95SChris Siden 
6502420dfc95SChris Siden 	/*
6503420dfc95SChris Siden 	 * Blow away any existing copy of zpool.cache
6504420dfc95SChris Siden 	 */
6505420dfc95SChris Siden 	(void) remove(spa_config_path);
6506420dfc95SChris Siden 
6507420dfc95SChris Siden 	/*
6508420dfc95SChris Siden 	 * Create and initialize our storage pool.
6509420dfc95SChris Siden 	 */
6510420dfc95SChris Siden 	for (int i = 1; i <= ztest_opts.zo_init; i++) {
6511420dfc95SChris Siden 		bzero(zs, sizeof (ztest_shared_t));
6512420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 3 &&
6513420dfc95SChris Siden 		    ztest_opts.zo_init != 1) {
6514420dfc95SChris Siden 			(void) printf("ztest_init(), pass %d\n", i);
6515420dfc95SChris Siden 		}
6516420dfc95SChris Siden 		ztest_init(zs);
6517420dfc95SChris Siden 	}
6518fa9e4066Sahrens }
6519fa9e4066Sahrens 
6520fa9e4066Sahrens int
6521fa9e4066Sahrens main(int argc, char **argv)
6522fa9e4066Sahrens {
6523fa9e4066Sahrens 	int kills = 0;
6524fa9e4066Sahrens 	int iters = 0;
6525420dfc95SChris Siden 	int older = 0;
6526420dfc95SChris Siden 	int newer = 0;
6527fa9e4066Sahrens 	ztest_shared_t *zs;
6528fa9e4066Sahrens 	ztest_info_t *zi;
6529420dfc95SChris Siden 	ztest_shared_callstate_t *zc;
6530fa9e4066Sahrens 	char timebuf[100];
65310a055120SJason King 	char numbuf[NN_NUMBUF_SZ];
6532b24ab676SJeff Bonwick 	spa_t *spa;
6533741652b0SEtienne Dechamps 	char *cmd;
6534420dfc95SChris Siden 	boolean_t hasalt;
6535741652b0SEtienne Dechamps 	char *fd_data_str = getenv("ZTEST_FD_DATA");
6536fa9e4066Sahrens 
6537fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
6538fa9e4066Sahrens 
6539cd1c8b85SMatthew Ahrens 	dprintf_setup(&argc, argv);
654069962b56SMatthew Ahrens 	zfs_deadman_synctime_ms = 300000;
654117f11284SSerapheim Dimitropoulos 	/*
654217f11284SSerapheim Dimitropoulos 	 * As two-word space map entries may not come up often (especially
654317f11284SSerapheim Dimitropoulos 	 * if pool and vdev sizes are small) we want to force at least some
654417f11284SSerapheim Dimitropoulos 	 * of them so the feature get tested.
654517f11284SSerapheim Dimitropoulos 	 */
654617f11284SSerapheim Dimitropoulos 	zfs_force_some_double_word_sm_entries = B_TRUE;
6547cd1c8b85SMatthew Ahrens 
6548741652b0SEtienne Dechamps 	ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6549741652b0SEtienne Dechamps 	ASSERT3S(ztest_fd_rand, >=, 0);
6550741652b0SEtienne Dechamps 
6551741652b0SEtienne Dechamps 	if (!fd_data_str) {
6552420dfc95SChris Siden 		process_options(argc, argv);
6553fa9e4066Sahrens 
6554741652b0SEtienne Dechamps 		setup_data_fd();
6555420dfc95SChris Siden 		setup_hdr();
6556420dfc95SChris Siden 		setup_data();
6557420dfc95SChris Siden 		bcopy(&ztest_opts, ztest_shared_opts,
6558420dfc95SChris Siden 		    sizeof (*ztest_shared_opts));
6559420dfc95SChris Siden 	} else {
6560741652b0SEtienne Dechamps 		ztest_fd_data = atoi(fd_data_str);
6561420dfc95SChris Siden 		setup_data();
6562420dfc95SChris Siden 		bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6563420dfc95SChris Siden 	}
6564420dfc95SChris Siden 	ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6565fa9e4066Sahrens 
6566f0ba89beSJeff Bonwick 	/* Override location of zpool.cache */
6567741652b0SEtienne Dechamps 	VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6568741652b0SEtienne Dechamps 	    ztest_opts.zo_dir), !=, -1);
6569f0ba89beSJeff Bonwick 
6570420dfc95SChris Siden 	ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6571420dfc95SChris Siden 	    UMEM_NOFAIL);
6572420dfc95SChris Siden 	zs = ztest_shared;
6573ea8dc4b6Seschrock 
6574741652b0SEtienne Dechamps 	if (fd_data_str) {
6575243952c7SMatt Ahrens 		metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6576420dfc95SChris Siden 		metaslab_df_alloc_threshold =
6577420dfc95SChris Siden 		    zs->zs_metaslab_df_alloc_threshold;
6578b24ab676SJeff Bonwick 
6579420dfc95SChris Siden 		if (zs->zs_do_init)
6580420dfc95SChris Siden 			ztest_run_init();
6581420dfc95SChris Siden 		else
6582420dfc95SChris Siden 			ztest_run(zs);
6583420dfc95SChris Siden 		exit(0);
6584420dfc95SChris Siden 	}
6585fa9e4066Sahrens 
6586420dfc95SChris Siden 	hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6587420dfc95SChris Siden 
6588420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6589fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
6590fa9e4066Sahrens 		    " %llu seconds...\n",
6591420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_vdevs,
6592420dfc95SChris Siden 		    ztest_opts.zo_datasets,
6593420dfc95SChris Siden 		    ztest_opts.zo_threads,
6594420dfc95SChris Siden 		    (u_longlong_t)ztest_opts.zo_time);
6595fa9e4066Sahrens 	}
6596fa9e4066Sahrens 
6597741652b0SEtienne Dechamps 	cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6598741652b0SEtienne Dechamps 	(void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6599420dfc95SChris Siden 
6600420dfc95SChris Siden 	zs->zs_do_init = B_TRUE;
6601420dfc95SChris Siden 	if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6602420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6603420dfc95SChris Siden 			(void) printf("Executing older ztest for "
6604420dfc95SChris Siden 			    "initialization: %s\n", ztest_opts.zo_alt_ztest);
6605420dfc95SChris Siden 		}
6606420dfc95SChris Siden 		VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6607420dfc95SChris Siden 		    ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6608420dfc95SChris Siden 	} else {
6609420dfc95SChris Siden 		VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6610fa9e4066Sahrens 	}
6611420dfc95SChris Siden 	zs->zs_do_init = B_FALSE;
6612fa9e4066Sahrens 
6613b24ab676SJeff Bonwick 	zs->zs_proc_start = gethrtime();
6614420dfc95SChris Siden 	zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6615fa9e4066Sahrens 
6616b24ab676SJeff Bonwick 	for (int f = 0; f < ZTEST_FUNCS; f++) {
6617420dfc95SChris Siden 		zi = &ztest_info[f];
6618420dfc95SChris Siden 		zc = ZTEST_GET_SHARED_CALLSTATE(f);
6619b24ab676SJeff Bonwick 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6620420dfc95SChris Siden 			zc->zc_next = UINT64_MAX;
6621fa9e4066Sahrens 		else
6622420dfc95SChris Siden 			zc->zc_next = zs->zs_proc_start +
6623b24ab676SJeff Bonwick 			    ztest_random(2 * zi->zi_interval[0] + 1);
6624fa9e4066Sahrens 	}
6625fa9e4066Sahrens 
6626fa9e4066Sahrens 	/*
6627fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
6628fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
6629fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
6630fa9e4066Sahrens 	 */
6631b24ab676SJeff Bonwick 	while (gethrtime() < zs->zs_proc_stop) {
6632fa9e4066Sahrens 		int status;
6633420dfc95SChris Siden 		boolean_t killed;
6634fa9e4066Sahrens 
6635fa9e4066Sahrens 		/*
6636fa9e4066Sahrens 		 * Initialize the workload counters for each function.
6637fa9e4066Sahrens 		 */
6638b24ab676SJeff Bonwick 		for (int f = 0; f < ZTEST_FUNCS; f++) {
6639420dfc95SChris Siden 			zc = ZTEST_GET_SHARED_CALLSTATE(f);
6640420dfc95SChris Siden 			zc->zc_count = 0;
6641420dfc95SChris Siden 			zc->zc_time = 0;
6642fa9e4066Sahrens 		}
6643fa9e4066Sahrens 
6644d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
6645420dfc95SChris Siden 		zs->zs_metaslab_df_alloc_threshold =
6646420dfc95SChris Siden 		    ztest_random(zs->zs_metaslab_sz / 4) + 1;
6647fa9e4066Sahrens 
6648420dfc95SChris Siden 		if (!hasalt || ztest_random(2) == 0) {
6649420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6650420dfc95SChris Siden 				(void) printf("Executing newer ztest: %s\n",
6651420dfc95SChris Siden 				    cmd);
6652fa9e4066Sahrens 			}
6653420dfc95SChris Siden 			newer++;
6654420dfc95SChris Siden 			killed = exec_child(cmd, NULL, B_TRUE, &status);
66555ad82045Snd 		} else {
6656420dfc95SChris Siden 			if (hasalt && ztest_opts.zo_verbose >= 1) {
6657420dfc95SChris Siden 				(void) printf("Executing older ztest: %s\n",
6658420dfc95SChris Siden 				    ztest_opts.zo_alt_ztest);
6659420dfc95SChris Siden 			}
6660420dfc95SChris Siden 			older++;
6661420dfc95SChris Siden 			killed = exec_child(ztest_opts.zo_alt_ztest,
6662420dfc95SChris Siden 			    ztest_opts.zo_alt_libpath, B_TRUE, &status);
6663fa9e4066Sahrens 		}
6664fa9e4066Sahrens 
6665420dfc95SChris Siden 		if (killed)
6666420dfc95SChris Siden 			kills++;
6667fa9e4066Sahrens 		iters++;
6668fa9e4066Sahrens 
6669420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 1) {
6670fa9e4066Sahrens 			hrtime_t now = gethrtime();
6671fa9e4066Sahrens 
6672b24ab676SJeff Bonwick 			now = MIN(now, zs->zs_proc_stop);
6673b24ab676SJeff Bonwick 			print_time(zs->zs_proc_stop - now, timebuf);
66740a055120SJason King 			nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6675fa9e4066Sahrens 
6676fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6677fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6678fa9e4066Sahrens 			    iters,
6679fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
6680fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
6681fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
6682fa9e4066Sahrens 			    numbuf,
6683b24ab676SJeff Bonwick 			    100.0 * (now - zs->zs_proc_start) /
6684420dfc95SChris Siden 			    (ztest_opts.zo_time * NANOSEC), timebuf);
6685fa9e4066Sahrens 		}
6686fa9e4066Sahrens 
6687420dfc95SChris Siden 		if (ztest_opts.zo_verbose >= 2) {
6688fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
6689fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6690fa9e4066Sahrens 			    "Calls", "Time", "Function");
6691fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
6692fa9e4066Sahrens 			    "-----", "----", "--------");
6693b24ab676SJeff Bonwick 			for (int f = 0; f < ZTEST_FUNCS; f++) {
6694fa9e4066Sahrens 				Dl_info dli;
6695fa9e4066Sahrens 
6696420dfc95SChris Siden 				zi = &ztest_info[f];
6697420dfc95SChris Siden 				zc = ZTEST_GET_SHARED_CALLSTATE(f);
6698420dfc95SChris Siden 				print_time(zc->zc_time, timebuf);
6699fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
6700fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
6701420dfc95SChris Siden 				    (u_longlong_t)zc->zc_count, timebuf,
6702fa9e4066Sahrens 				    dli.dli_sname);
6703fa9e4066Sahrens 			}
6704fa9e4066Sahrens 			(void) printf("\n");
6705fa9e4066Sahrens 		}
6706fa9e4066Sahrens 
6707fa9e4066Sahrens 		/*
6708b24ab676SJeff Bonwick 		 * It's possible that we killed a child during a rename test,
6709b24ab676SJeff Bonwick 		 * in which case we'll have a 'ztest_tmp' pool lying around
6710b24ab676SJeff Bonwick 		 * instead of 'ztest'.  Do a blind rename in case this happened.
6711fa9e4066Sahrens 		 */
6712b24ab676SJeff Bonwick 		kernel_init(FREAD);
6713420dfc95SChris Siden 		if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6714b24ab676SJeff Bonwick 			spa_close(spa, FTAG);
6715b24ab676SJeff Bonwick 		} else {
67169adfa60dSMatthew Ahrens 			char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6717b24ab676SJeff Bonwick 			kernel_fini();
6718b24ab676SJeff Bonwick 			kernel_init(FREAD | FWRITE);
6719b24ab676SJeff Bonwick 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6720420dfc95SChris Siden 			    ztest_opts.zo_pool);
6721420dfc95SChris Siden 			(void) spa_rename(tmpname, ztest_opts.zo_pool);
6722b24ab676SJeff Bonwick 		}
6723fa9e4066Sahrens 		kernel_fini();
6724fa9e4066Sahrens 
6725420dfc95SChris Siden 		ztest_run_zdb(ztest_opts.zo_pool);
6726b24ab676SJeff Bonwick 	}
6727fa9e4066Sahrens 
6728420dfc95SChris Siden 	if (ztest_opts.zo_verbose >= 1) {
6729420dfc95SChris Siden 		if (hasalt) {
6730420dfc95SChris Siden 			(void) printf("%d runs of older ztest: %s\n", older,
6731420dfc95SChris Siden 			    ztest_opts.zo_alt_ztest);
6732420dfc95SChris Siden 			(void) printf("%d runs of newer ztest: %s\n", newer,
6733420dfc95SChris Siden 			    cmd);
6734420dfc95SChris Siden 		}
6735fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6736fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6737fa9e4066Sahrens 	}
6738fa9e4066Sahrens 
6739741652b0SEtienne Dechamps 	umem_free(cmd, MAXNAMELEN);
6740741652b0SEtienne Dechamps 
6741fa9e4066Sahrens 	return (0);
6742fa9e4066Sahrens }
6743