xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 10922)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
228779SMark.Musante@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
28789Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
29789Sahrens  *
30789Sahrens  * The overall design of the ztest program is as follows:
31789Sahrens  *
32789Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
33789Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
34789Sahrens  *     we have a simple routine to test that functionality.  These
35789Sahrens  *     individual routines do not have to do anything "stressful".
36789Sahrens  *
37789Sahrens  * (2) We turn these simple functionality tests into a stress test by
38789Sahrens  *     running them all in parallel, with as many threads as desired,
39789Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
40789Sahrens  *
41789Sahrens  * (3) While all this is happening, we inject faults into the pool to
42789Sahrens  *     verify that self-healing data really works.
43789Sahrens  *
44789Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
45789Sahrens  *     functions.  Thus even individual objects vary from block to block
46789Sahrens  *     in which checksum they use and whether they're compressed.
47789Sahrens  *
48789Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
49789Sahrens  *     we run the entire test in a child of the main process.
50789Sahrens  *     At random times, the child self-immolates with a SIGKILL.
51789Sahrens  *     This is the software equivalent of pulling the power cord.
52789Sahrens  *     The parent then runs the test again, using the existing
53789Sahrens  *     storage pool, as many times as desired.
54789Sahrens  *
55789Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
56789Sahrens  *     many of the functional tests record the transaction group number
57789Sahrens  *     as part of their data.  When reading old data, they verify that
58789Sahrens  *     the transaction group number is less than the current, open txg.
59789Sahrens  *     If you add a new test, please do this if applicable.
60789Sahrens  *
61789Sahrens  * When run with no arguments, ztest runs for about five minutes and
62789Sahrens  * produces no output if successful.  To get a little bit of information,
63789Sahrens  * specify -V.  To get more information, specify -VV, and so on.
64789Sahrens  *
65789Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
66789Sahrens  *
67789Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68789Sahrens  * to increase the pool capacity, fanout, and overall stress level.
69789Sahrens  *
70789Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
71789Sahrens  * This can be useful when you're trying to distinguish temporal incursions
72789Sahrens  * from plain old race conditions.
73789Sahrens  */
74789Sahrens 
75789Sahrens #include <sys/zfs_context.h>
76789Sahrens #include <sys/spa.h>
77789Sahrens #include <sys/dmu.h>
78789Sahrens #include <sys/txg.h>
799412SAleksandr.Guzovskiy@Sun.COM #include <sys/dbuf.h>
80789Sahrens #include <sys/zap.h>
81789Sahrens #include <sys/dmu_objset.h>
82789Sahrens #include <sys/poll.h>
83789Sahrens #include <sys/stat.h>
84789Sahrens #include <sys/time.h>
85789Sahrens #include <sys/wait.h>
86789Sahrens #include <sys/mman.h>
87789Sahrens #include <sys/resource.h>
88789Sahrens #include <sys/zio.h>
89789Sahrens #include <sys/zil.h>
90*10922SJeff.Bonwick@Sun.COM #include <sys/zil_impl.h>
91789Sahrens #include <sys/vdev_impl.h>
927754SJeff.Bonwick@Sun.COM #include <sys/vdev_file.h>
93789Sahrens #include <sys/spa_impl.h>
9410594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
95789Sahrens #include <sys/dsl_prop.h>
968779SMark.Musante@Sun.COM #include <sys/dsl_dataset.h>
97789Sahrens #include <sys/refcount.h>
98789Sahrens #include <stdio.h>
991914Scasper #include <stdio_ext.h>
100789Sahrens #include <stdlib.h>
101789Sahrens #include <unistd.h>
102789Sahrens #include <signal.h>
103789Sahrens #include <umem.h>
104789Sahrens #include <dlfcn.h>
105789Sahrens #include <ctype.h>
106789Sahrens #include <math.h>
107789Sahrens #include <sys/fs/zfs.h>
108*10922SJeff.Bonwick@Sun.COM #include <libnvpair.h>
109789Sahrens 
110789Sahrens static char cmdname[] = "ztest";
111789Sahrens static char *zopt_pool = cmdname;
112789Sahrens 
113789Sahrens static uint64_t zopt_vdevs = 5;
114789Sahrens static uint64_t zopt_vdevtime;
1151732Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT;
116789Sahrens static int zopt_mirrors = 2;
117789Sahrens static int zopt_raidz = 4;
1182082Seschrock static int zopt_raidz_parity = 1;
119789Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE;
1201732Sbonwick static int zopt_datasets = 7;
121789Sahrens static int zopt_threads = 23;
122789Sahrens static uint64_t zopt_passtime = 60;	/* 60 seconds */
123789Sahrens static uint64_t zopt_killrate = 70;	/* 70% kill rate */
124789Sahrens static int zopt_verbose = 0;
125789Sahrens static int zopt_init = 1;
126789Sahrens static char *zopt_dir = "/tmp";
127789Sahrens static uint64_t zopt_time = 300;	/* 5 minutes */
128789Sahrens static int zopt_maxfaults;
129789Sahrens 
130*10922SJeff.Bonwick@Sun.COM #define	BT_MAGIC	0x123456789abcdefULL
131*10922SJeff.Bonwick@Sun.COM 
132*10922SJeff.Bonwick@Sun.COM enum ztest_io_type {
133*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_TAG,
134*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_PATTERN,
135*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_ZEROES,
136*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TRUNCATE,
137*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_SETATTR,
138*10922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TYPES
139*10922SJeff.Bonwick@Sun.COM };
140*10922SJeff.Bonwick@Sun.COM 
1415530Sbonwick typedef struct ztest_block_tag {
142*10922SJeff.Bonwick@Sun.COM 	uint64_t	bt_magic;
1435530Sbonwick 	uint64_t	bt_objset;
1445530Sbonwick 	uint64_t	bt_object;
1455530Sbonwick 	uint64_t	bt_offset;
146*10922SJeff.Bonwick@Sun.COM 	uint64_t	bt_gen;
1475530Sbonwick 	uint64_t	bt_txg;
148*10922SJeff.Bonwick@Sun.COM 	uint64_t	bt_crtxg;
1495530Sbonwick } ztest_block_tag_t;
1505530Sbonwick 
151*10922SJeff.Bonwick@Sun.COM typedef struct bufwad {
152*10922SJeff.Bonwick@Sun.COM 	uint64_t	bw_index;
153*10922SJeff.Bonwick@Sun.COM 	uint64_t	bw_txg;
154*10922SJeff.Bonwick@Sun.COM 	uint64_t	bw_data;
155*10922SJeff.Bonwick@Sun.COM } bufwad_t;
156*10922SJeff.Bonwick@Sun.COM 
157*10922SJeff.Bonwick@Sun.COM /*
158*10922SJeff.Bonwick@Sun.COM  * XXX -- fix zfs range locks to be generic so we can use them here.
159*10922SJeff.Bonwick@Sun.COM  */
160*10922SJeff.Bonwick@Sun.COM typedef enum {
161*10922SJeff.Bonwick@Sun.COM 	RL_READER,
162*10922SJeff.Bonwick@Sun.COM 	RL_WRITER,
163*10922SJeff.Bonwick@Sun.COM 	RL_APPEND
164*10922SJeff.Bonwick@Sun.COM } rl_type_t;
165*10922SJeff.Bonwick@Sun.COM 
166*10922SJeff.Bonwick@Sun.COM typedef struct rll {
167*10922SJeff.Bonwick@Sun.COM 	void		*rll_writer;
168*10922SJeff.Bonwick@Sun.COM 	int		rll_readers;
169*10922SJeff.Bonwick@Sun.COM 	mutex_t		rll_lock;
170*10922SJeff.Bonwick@Sun.COM 	cond_t		rll_cv;
171*10922SJeff.Bonwick@Sun.COM } rll_t;
172*10922SJeff.Bonwick@Sun.COM 
173*10922SJeff.Bonwick@Sun.COM typedef struct rl {
174*10922SJeff.Bonwick@Sun.COM 	uint64_t	rl_object;
175*10922SJeff.Bonwick@Sun.COM 	uint64_t	rl_offset;
176*10922SJeff.Bonwick@Sun.COM 	uint64_t	rl_size;
177*10922SJeff.Bonwick@Sun.COM 	rll_t		*rl_lock;
178*10922SJeff.Bonwick@Sun.COM } rl_t;
179*10922SJeff.Bonwick@Sun.COM 
180*10922SJeff.Bonwick@Sun.COM #define	ZTEST_RANGE_LOCKS	64
181*10922SJeff.Bonwick@Sun.COM #define	ZTEST_OBJECT_LOCKS	64
182*10922SJeff.Bonwick@Sun.COM 
183*10922SJeff.Bonwick@Sun.COM /*
184*10922SJeff.Bonwick@Sun.COM  * Object descriptor.  Used as a template for object lookup/create/remove.
185*10922SJeff.Bonwick@Sun.COM  */
186*10922SJeff.Bonwick@Sun.COM typedef struct ztest_od {
187*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_dir;
188*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_object;
189*10922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_type;
190*10922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_crtype;
191*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_blocksize;
192*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_crblocksize;
193*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_gen;
194*10922SJeff.Bonwick@Sun.COM 	uint64_t	od_crgen;
195*10922SJeff.Bonwick@Sun.COM 	char		od_name[MAXNAMELEN];
196*10922SJeff.Bonwick@Sun.COM } ztest_od_t;
197*10922SJeff.Bonwick@Sun.COM 
198*10922SJeff.Bonwick@Sun.COM /*
199*10922SJeff.Bonwick@Sun.COM  * Per-dataset state.
200*10922SJeff.Bonwick@Sun.COM  */
201*10922SJeff.Bonwick@Sun.COM typedef struct ztest_ds {
202*10922SJeff.Bonwick@Sun.COM 	objset_t	*zd_os;
203*10922SJeff.Bonwick@Sun.COM 	zilog_t		*zd_zilog;
204*10922SJeff.Bonwick@Sun.COM 	uint64_t	zd_seq;
205*10922SJeff.Bonwick@Sun.COM 	ztest_od_t	*zd_od;		/* debugging aid */
206*10922SJeff.Bonwick@Sun.COM 	char		zd_name[MAXNAMELEN];
207*10922SJeff.Bonwick@Sun.COM 	mutex_t		zd_dirobj_lock;
208*10922SJeff.Bonwick@Sun.COM 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
209*10922SJeff.Bonwick@Sun.COM 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
210*10922SJeff.Bonwick@Sun.COM } ztest_ds_t;
211*10922SJeff.Bonwick@Sun.COM 
212*10922SJeff.Bonwick@Sun.COM /*
213*10922SJeff.Bonwick@Sun.COM  * Per-iteration state.
214*10922SJeff.Bonwick@Sun.COM  */
215*10922SJeff.Bonwick@Sun.COM typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
216*10922SJeff.Bonwick@Sun.COM 
217*10922SJeff.Bonwick@Sun.COM typedef struct ztest_info {
218*10922SJeff.Bonwick@Sun.COM 	ztest_func_t	*zi_func;	/* test function */
219*10922SJeff.Bonwick@Sun.COM 	uint64_t	zi_iters;	/* iterations per execution */
220*10922SJeff.Bonwick@Sun.COM 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
221*10922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_count;	/* per-pass count */
222*10922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_time;	/* per-pass time */
223*10922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_next;	/* next time to call this function */
224*10922SJeff.Bonwick@Sun.COM } ztest_info_t;
225789Sahrens 
226789Sahrens /*
227789Sahrens  * Note: these aren't static because we want dladdr() to work.
228789Sahrens  */
229789Sahrens ztest_func_t ztest_dmu_read_write;
230789Sahrens ztest_func_t ztest_dmu_write_parallel;
231789Sahrens ztest_func_t ztest_dmu_object_alloc_free;
23210612SRicardo.M.Correia@Sun.COM ztest_func_t ztest_dmu_commit_callbacks;
233789Sahrens ztest_func_t ztest_zap;
234*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zap_parallel;
235*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zil_commit;
236*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_read_write_zcopy;
237*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_objset_create_destroy;
238*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_prealloc;
23910431SSanjeev.Bagewadi@Sun.COM ztest_func_t ztest_fzap;
240*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_create_destroy;
241789Sahrens ztest_func_t ztest_dsl_prop_get_set;
242*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_prop_get_set;
243789Sahrens ztest_func_t ztest_spa_create_destroy;
244789Sahrens ztest_func_t ztest_fault_inject;
245*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_ddt_repair;
246*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_hold;
2477754SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_rename;
248*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_scrub;
249*10922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dsl_dataset_promote_busy;
250789Sahrens ztest_func_t ztest_vdev_attach_detach;
251789Sahrens ztest_func_t ztest_vdev_LUN_growth;
252789Sahrens ztest_func_t ztest_vdev_add_remove;
2537754SJeff.Bonwick@Sun.COM ztest_func_t ztest_vdev_aux_add_remove;
254*10922SJeff.Bonwick@Sun.COM 
255*10922SJeff.Bonwick@Sun.COM uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
256*10922SJeff.Bonwick@Sun.COM uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
257*10922SJeff.Bonwick@Sun.COM uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
258*10922SJeff.Bonwick@Sun.COM uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
259*10922SJeff.Bonwick@Sun.COM uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
260789Sahrens 
261789Sahrens ztest_info_t ztest_info[] = {
2625530Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
263*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
2645530Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
265*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
2665530Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
2675530Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
268*10922SJeff.Bonwick@Sun.COM 	{ ztest_zil_commit,			1,	&zopt_incessant	},
269*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
270*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
271*10922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
272*10922SJeff.Bonwick@Sun.COM 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
273*10922SJeff.Bonwick@Sun.COM #if 0
274*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
275*10922SJeff.Bonwick@Sun.COM #endif
276*10922SJeff.Bonwick@Sun.COM 	{ ztest_fzap,				1,	&zopt_sometimes	},
277*10922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
278*10922SJeff.Bonwick@Sun.COM 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
2795530Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
280*10922SJeff.Bonwick@Sun.COM 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
28110693Schris.kirby@sun.com 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
2825530Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
283*10922SJeff.Bonwick@Sun.COM 	{ ztest_scrub,				1,	&zopt_rarely	},
284*10922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
2857754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
2867754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
2877754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime	},
2887754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
289789Sahrens };
290789Sahrens 
291789Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
292789Sahrens 
293789Sahrens /*
29410612SRicardo.M.Correia@Sun.COM  * The following struct is used to hold a list of uncalled commit callbacks.
29510612SRicardo.M.Correia@Sun.COM  * The callbacks are ordered by txg number.
29610612SRicardo.M.Correia@Sun.COM  */
29710612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_list {
29810612SRicardo.M.Correia@Sun.COM 	mutex_t	zcl_callbacks_lock;
29910612SRicardo.M.Correia@Sun.COM 	list_t	zcl_callbacks;
30010612SRicardo.M.Correia@Sun.COM } ztest_cb_list_t;
30110612SRicardo.M.Correia@Sun.COM 
30210612SRicardo.M.Correia@Sun.COM /*
303789Sahrens  * Stuff we need to share writably between parent and child.
304789Sahrens  */
305789Sahrens typedef struct ztest_shared {
306*10922SJeff.Bonwick@Sun.COM 	char		*zs_pool;
307*10922SJeff.Bonwick@Sun.COM 	spa_t		*zs_spa;
308*10922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_start;
309*10922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_stop;
310*10922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_start;
311*10922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_stop;
312*10922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_kill;
313*10922SJeff.Bonwick@Sun.COM 	uint64_t	zs_enospc_count;
31410594SGeorge.Wilson@Sun.COM 	uint64_t	zs_vdev_next_leaf;
3157754SJeff.Bonwick@Sun.COM 	uint64_t	zs_vdev_aux;
316789Sahrens 	uint64_t	zs_alloc;
317789Sahrens 	uint64_t	zs_space;
318*10922SJeff.Bonwick@Sun.COM 	mutex_t		zs_vdev_lock;
319*10922SJeff.Bonwick@Sun.COM 	rwlock_t	zs_name_lock;
320789Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
321*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t	zs_zd[];
322789Sahrens } ztest_shared_t;
323789Sahrens 
324*10922SJeff.Bonwick@Sun.COM #define	ID_PARALLEL	-1ULL
325*10922SJeff.Bonwick@Sun.COM 
326789Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
3277754SJeff.Bonwick@Sun.COM static char ztest_aux_template[] = "%s/%s.%s.%llu";
328*10922SJeff.Bonwick@Sun.COM ztest_shared_t *ztest_shared;
329*10922SJeff.Bonwick@Sun.COM uint64_t *ztest_seq;
330789Sahrens 
331789Sahrens static int ztest_random_fd;
332789Sahrens static int ztest_dump_core = 1;
333789Sahrens 
3347754SJeff.Bonwick@Sun.COM static boolean_t ztest_exiting;
3355329Sgw25295 
33610612SRicardo.M.Correia@Sun.COM /* Global commit callback list */
33710612SRicardo.M.Correia@Sun.COM static ztest_cb_list_t zcl;
33810612SRicardo.M.Correia@Sun.COM 
3395530Sbonwick extern uint64_t metaslab_gang_bang;
3409480SGeorge.Wilson@Sun.COM extern uint64_t metaslab_df_alloc_threshold;
341*10922SJeff.Bonwick@Sun.COM static uint64_t metaslab_sz;
342*10922SJeff.Bonwick@Sun.COM 
343*10922SJeff.Bonwick@Sun.COM enum ztest_object {
344*10922SJeff.Bonwick@Sun.COM 	ZTEST_META_DNODE = 0,
345*10922SJeff.Bonwick@Sun.COM 	ZTEST_DIROBJ,
346*10922SJeff.Bonwick@Sun.COM 	ZTEST_OBJECTS
347*10922SJeff.Bonwick@Sun.COM };
348789Sahrens 
3494008Sraf static void usage(boolean_t) __NORETURN;
3503972Svb160487 
351789Sahrens /*
352789Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
353789Sahrens  * debugging facilities.
354789Sahrens  */
355789Sahrens const char *
356789Sahrens _umem_debug_init()
357789Sahrens {
358789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
359789Sahrens }
360789Sahrens 
361789Sahrens const char *
362789Sahrens _umem_logging_init(void)
363789Sahrens {
364789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
365789Sahrens }
366789Sahrens 
367789Sahrens #define	FATAL_MSG_SZ	1024
368789Sahrens 
369789Sahrens char *fatal_msg;
370789Sahrens 
371789Sahrens static void
372789Sahrens fatal(int do_perror, char *message, ...)
373789Sahrens {
374789Sahrens 	va_list args;
375789Sahrens 	int save_errno = errno;
376789Sahrens 	char buf[FATAL_MSG_SZ];
377789Sahrens 
378789Sahrens 	(void) fflush(stdout);
379789Sahrens 
380789Sahrens 	va_start(args, message);
381789Sahrens 	(void) sprintf(buf, "ztest: ");
382789Sahrens 	/* LINTED */
383789Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
384789Sahrens 	va_end(args);
385789Sahrens 	if (do_perror) {
386789Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
387789Sahrens 		    ": %s", strerror(save_errno));
388789Sahrens 	}
389789Sahrens 	(void) fprintf(stderr, "%s\n", buf);
390789Sahrens 	fatal_msg = buf;			/* to ease debugging */
391789Sahrens 	if (ztest_dump_core)
392789Sahrens 		abort();
393789Sahrens 	exit(3);
394789Sahrens }
395789Sahrens 
396789Sahrens static int
397789Sahrens str2shift(const char *buf)
398789Sahrens {
399789Sahrens 	const char *ends = "BKMGTPEZ";
400789Sahrens 	int i;
401789Sahrens 
402789Sahrens 	if (buf[0] == '\0')
403789Sahrens 		return (0);
404789Sahrens 	for (i = 0; i < strlen(ends); i++) {
405789Sahrens 		if (toupper(buf[0]) == ends[i])
406789Sahrens 			break;
407789Sahrens 	}
4083972Svb160487 	if (i == strlen(ends)) {
4093972Svb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
4103972Svb160487 		    buf);
4113972Svb160487 		usage(B_FALSE);
4123972Svb160487 	}
413789Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
414789Sahrens 		return (10*i);
415789Sahrens 	}
4163972Svb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
4173972Svb160487 	usage(B_FALSE);
4183972Svb160487 	/* NOTREACHED */
419789Sahrens }
420789Sahrens 
421789Sahrens static uint64_t
422789Sahrens nicenumtoull(const char *buf)
423789Sahrens {
424789Sahrens 	char *end;
425789Sahrens 	uint64_t val;
426789Sahrens 
427789Sahrens 	val = strtoull(buf, &end, 0);
428789Sahrens 	if (end == buf) {
4293972Svb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
4303972Svb160487 		usage(B_FALSE);
431789Sahrens 	} else if (end[0] == '.') {
432789Sahrens 		double fval = strtod(buf, &end);
433789Sahrens 		fval *= pow(2, str2shift(end));
4343972Svb160487 		if (fval > UINT64_MAX) {
4353972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4363972Svb160487 			    buf);
4373972Svb160487 			usage(B_FALSE);
4383972Svb160487 		}
439789Sahrens 		val = (uint64_t)fval;
440789Sahrens 	} else {
441789Sahrens 		int shift = str2shift(end);
4423972Svb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
4433972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4443972Svb160487 			    buf);
4453972Svb160487 			usage(B_FALSE);
4463972Svb160487 		}
447789Sahrens 		val <<= shift;
448789Sahrens 	}
449789Sahrens 	return (val);
450789Sahrens }
451789Sahrens 
452789Sahrens static void
4533972Svb160487 usage(boolean_t requested)
454789Sahrens {
455789Sahrens 	char nice_vdev_size[10];
456789Sahrens 	char nice_gang_bang[10];
4573972Svb160487 	FILE *fp = requested ? stdout : stderr;
458789Sahrens 
459789Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
4605530Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
461789Sahrens 
4623972Svb160487 	(void) fprintf(fp, "Usage: %s\n"
463789Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
464789Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
4651732Sbonwick 	    "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
466789Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
467789Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
4682082Seschrock 	    "\t[-R raidz_parity (default: %d)]\n"
469789Sahrens 	    "\t[-d datasets (default: %d)]\n"
470789Sahrens 	    "\t[-t threads (default: %d)]\n"
471789Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
472789Sahrens 	    "\t[-i initialize pool i times (default: %d)]\n"
473789Sahrens 	    "\t[-k kill percentage (default: %llu%%)]\n"
474789Sahrens 	    "\t[-p pool_name (default: %s)]\n"
475789Sahrens 	    "\t[-f file directory for vdev files (default: %s)]\n"
476789Sahrens 	    "\t[-V(erbose)] (use multiple times for ever more blather)\n"
4771732Sbonwick 	    "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
478789Sahrens 	    "\t[-T time] total run time (default: %llu sec)\n"
479789Sahrens 	    "\t[-P passtime] time per pass (default: %llu sec)\n"
4803972Svb160487 	    "\t[-h] (print help)\n"
481789Sahrens 	    "",
482789Sahrens 	    cmdname,
4835329Sgw25295 	    (u_longlong_t)zopt_vdevs,			/* -v */
4845329Sgw25295 	    nice_vdev_size,				/* -s */
4855329Sgw25295 	    zopt_ashift,				/* -a */
4865329Sgw25295 	    zopt_mirrors,				/* -m */
4875329Sgw25295 	    zopt_raidz,					/* -r */
4885329Sgw25295 	    zopt_raidz_parity,				/* -R */
4895329Sgw25295 	    zopt_datasets,				/* -d */
4905329Sgw25295 	    zopt_threads,				/* -t */
4915329Sgw25295 	    nice_gang_bang,				/* -g */
4925329Sgw25295 	    zopt_init,					/* -i */
4935329Sgw25295 	    (u_longlong_t)zopt_killrate,		/* -k */
4945329Sgw25295 	    zopt_pool,					/* -p */
4955329Sgw25295 	    zopt_dir,					/* -f */
4965329Sgw25295 	    (u_longlong_t)zopt_time,			/* -T */
4977754SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zopt_passtime);		/* -P */
4983972Svb160487 	exit(requested ? 0 : 1);
499789Sahrens }
500789Sahrens 
501789Sahrens static void
502789Sahrens process_options(int argc, char **argv)
503789Sahrens {
504789Sahrens 	int opt;
505789Sahrens 	uint64_t value;
506789Sahrens 
507789Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
5085530Sbonwick 	metaslab_gang_bang = 32 << 10;
509789Sahrens 
510789Sahrens 	while ((opt = getopt(argc, argv,
5117754SJeff.Bonwick@Sun.COM 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
512789Sahrens 		value = 0;
513789Sahrens 		switch (opt) {
5144451Seschrock 		case 'v':
5154451Seschrock 		case 's':
5164451Seschrock 		case 'a':
5174451Seschrock 		case 'm':
5184451Seschrock 		case 'r':
5194451Seschrock 		case 'R':
5204451Seschrock 		case 'd':
5214451Seschrock 		case 't':
5224451Seschrock 		case 'g':
5234451Seschrock 		case 'i':
5244451Seschrock 		case 'k':
5254451Seschrock 		case 'T':
5264451Seschrock 		case 'P':
527789Sahrens 			value = nicenumtoull(optarg);
528789Sahrens 		}
529789Sahrens 		switch (opt) {
5304451Seschrock 		case 'v':
531789Sahrens 			zopt_vdevs = value;
532789Sahrens 			break;
5334451Seschrock 		case 's':
534789Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
535789Sahrens 			break;
5364451Seschrock 		case 'a':
5371732Sbonwick 			zopt_ashift = value;
5381732Sbonwick 			break;
5394451Seschrock 		case 'm':
540789Sahrens 			zopt_mirrors = value;
541789Sahrens 			break;
5424451Seschrock 		case 'r':
543789Sahrens 			zopt_raidz = MAX(1, value);
544789Sahrens 			break;
5454451Seschrock 		case 'R':
54610105Sadam.leventhal@sun.com 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
5472082Seschrock 			break;
5484451Seschrock 		case 'd':
5491732Sbonwick 			zopt_datasets = MAX(1, value);
550789Sahrens 			break;
5514451Seschrock 		case 't':
552789Sahrens 			zopt_threads = MAX(1, value);
553789Sahrens 			break;
5544451Seschrock 		case 'g':
5555530Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
556789Sahrens 			break;
5574451Seschrock 		case 'i':
558789Sahrens 			zopt_init = value;
559789Sahrens 			break;
5604451Seschrock 		case 'k':
561789Sahrens 			zopt_killrate = value;
562789Sahrens 			break;
5634451Seschrock 		case 'p':
564789Sahrens 			zopt_pool = strdup(optarg);
565789Sahrens 			break;
5664451Seschrock 		case 'f':
567789Sahrens 			zopt_dir = strdup(optarg);
568789Sahrens 			break;
5694451Seschrock 		case 'V':
570789Sahrens 			zopt_verbose++;
571789Sahrens 			break;
5724451Seschrock 		case 'E':
573789Sahrens 			zopt_init = 0;
574789Sahrens 			break;
5754451Seschrock 		case 'T':
576789Sahrens 			zopt_time = value;
577789Sahrens 			break;
5784451Seschrock 		case 'P':
579789Sahrens 			zopt_passtime = MAX(1, value);
580789Sahrens 			break;
5814451Seschrock 		case 'h':
5823972Svb160487 			usage(B_TRUE);
5833972Svb160487 			break;
5844451Seschrock 		case '?':
5854451Seschrock 		default:
5863972Svb160487 			usage(B_FALSE);
587789Sahrens 			break;
588789Sahrens 		}
589789Sahrens 	}
590789Sahrens 
5912082Seschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
5922082Seschrock 
593*10922SJeff.Bonwick@Sun.COM 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
594*10922SJeff.Bonwick@Sun.COM 	    UINT64_MAX >> 2);
5952082Seschrock 	zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
596789Sahrens }
597789Sahrens 
598*10922SJeff.Bonwick@Sun.COM static void
599*10922SJeff.Bonwick@Sun.COM ztest_kill(ztest_shared_t *zs)
600*10922SJeff.Bonwick@Sun.COM {
601*10922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
602*10922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
603*10922SJeff.Bonwick@Sun.COM 	(void) kill(getpid(), SIGKILL);
604*10922SJeff.Bonwick@Sun.COM }
605*10922SJeff.Bonwick@Sun.COM 
606*10922SJeff.Bonwick@Sun.COM static uint64_t
607*10922SJeff.Bonwick@Sun.COM ztest_random(uint64_t range)
608*10922SJeff.Bonwick@Sun.COM {
609*10922SJeff.Bonwick@Sun.COM 	uint64_t r;
610*10922SJeff.Bonwick@Sun.COM 
611*10922SJeff.Bonwick@Sun.COM 	if (range == 0)
612*10922SJeff.Bonwick@Sun.COM 		return (0);
613*10922SJeff.Bonwick@Sun.COM 
614*10922SJeff.Bonwick@Sun.COM 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
615*10922SJeff.Bonwick@Sun.COM 		fatal(1, "short read from /dev/urandom");
616*10922SJeff.Bonwick@Sun.COM 
617*10922SJeff.Bonwick@Sun.COM 	return (r % range);
618*10922SJeff.Bonwick@Sun.COM }
619*10922SJeff.Bonwick@Sun.COM 
620*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
621*10922SJeff.Bonwick@Sun.COM static void
622*10922SJeff.Bonwick@Sun.COM ztest_record_enospc(const char *s)
623*10922SJeff.Bonwick@Sun.COM {
624*10922SJeff.Bonwick@Sun.COM 	ztest_shared->zs_enospc_count++;
625*10922SJeff.Bonwick@Sun.COM }
626*10922SJeff.Bonwick@Sun.COM 
6271732Sbonwick static uint64_t
6281732Sbonwick ztest_get_ashift(void)
6291732Sbonwick {
6301732Sbonwick 	if (zopt_ashift == 0)
6311732Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
6321732Sbonwick 	return (zopt_ashift);
6331732Sbonwick }
6341732Sbonwick 
635789Sahrens static nvlist_t *
6367754SJeff.Bonwick@Sun.COM make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
637789Sahrens {
6387754SJeff.Bonwick@Sun.COM 	char pathbuf[MAXPATHLEN];
639789Sahrens 	uint64_t vdev;
640789Sahrens 	nvlist_t *file;
641789Sahrens 
6427754SJeff.Bonwick@Sun.COM 	if (ashift == 0)
6437754SJeff.Bonwick@Sun.COM 		ashift = ztest_get_ashift();
6447754SJeff.Bonwick@Sun.COM 
6457754SJeff.Bonwick@Sun.COM 	if (path == NULL) {
6467754SJeff.Bonwick@Sun.COM 		path = pathbuf;
6477754SJeff.Bonwick@Sun.COM 
6487754SJeff.Bonwick@Sun.COM 		if (aux != NULL) {
6497754SJeff.Bonwick@Sun.COM 			vdev = ztest_shared->zs_vdev_aux;
6507754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template,
6517754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, aux, vdev);
6527754SJeff.Bonwick@Sun.COM 		} else {
65310594SGeorge.Wilson@Sun.COM 			vdev = ztest_shared->zs_vdev_next_leaf++;
6547754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_dev_template,
6557754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, vdev);
6567754SJeff.Bonwick@Sun.COM 		}
6577754SJeff.Bonwick@Sun.COM 	}
6587754SJeff.Bonwick@Sun.COM 
6597754SJeff.Bonwick@Sun.COM 	if (size != 0) {
6607754SJeff.Bonwick@Sun.COM 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
661789Sahrens 		if (fd == -1)
6627754SJeff.Bonwick@Sun.COM 			fatal(1, "can't open %s", path);
663789Sahrens 		if (ftruncate(fd, size) != 0)
6647754SJeff.Bonwick@Sun.COM 			fatal(1, "can't ftruncate %s", path);
665789Sahrens 		(void) close(fd);
666789Sahrens 	}
667789Sahrens 
668789Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
669789Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
6707754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
6711732Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
672789Sahrens 
673789Sahrens 	return (file);
674789Sahrens }
675789Sahrens 
676789Sahrens static nvlist_t *
6777754SJeff.Bonwick@Sun.COM make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
678789Sahrens {
679789Sahrens 	nvlist_t *raidz, **child;
680789Sahrens 	int c;
681789Sahrens 
682789Sahrens 	if (r < 2)
6837754SJeff.Bonwick@Sun.COM 		return (make_vdev_file(path, aux, size, ashift));
684789Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
685789Sahrens 
686789Sahrens 	for (c = 0; c < r; c++)
6877754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_file(path, aux, size, ashift);
688789Sahrens 
689789Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
690789Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
691789Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
6922082Seschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
6932082Seschrock 	    zopt_raidz_parity) == 0);
694789Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
695789Sahrens 	    child, r) == 0);
696789Sahrens 
697789Sahrens 	for (c = 0; c < r; c++)
698789Sahrens 		nvlist_free(child[c]);
699789Sahrens 
700789Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
701789Sahrens 
702789Sahrens 	return (raidz);
703789Sahrens }
704789Sahrens 
705789Sahrens static nvlist_t *
7067754SJeff.Bonwick@Sun.COM make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
7077768SJeff.Bonwick@Sun.COM 	int r, int m)
708789Sahrens {
709789Sahrens 	nvlist_t *mirror, **child;
710789Sahrens 	int c;
711789Sahrens 
712789Sahrens 	if (m < 1)
7137754SJeff.Bonwick@Sun.COM 		return (make_vdev_raidz(path, aux, size, ashift, r));
714789Sahrens 
715789Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
716789Sahrens 
717789Sahrens 	for (c = 0; c < m; c++)
7187754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
719789Sahrens 
720789Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
721789Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
722789Sahrens 	    VDEV_TYPE_MIRROR) == 0);
723789Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
724789Sahrens 	    child, m) == 0);
725789Sahrens 
726789Sahrens 	for (c = 0; c < m; c++)
727789Sahrens 		nvlist_free(child[c]);
728789Sahrens 
729789Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
730789Sahrens 
731789Sahrens 	return (mirror);
732789Sahrens }
733789Sahrens 
734789Sahrens static nvlist_t *
7357754SJeff.Bonwick@Sun.COM make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
7367754SJeff.Bonwick@Sun.COM 	int log, int r, int m, int t)
737789Sahrens {
738789Sahrens 	nvlist_t *root, **child;
739789Sahrens 	int c;
740789Sahrens 
741789Sahrens 	ASSERT(t > 0);
742789Sahrens 
743789Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
744789Sahrens 
7457768SJeff.Bonwick@Sun.COM 	for (c = 0; c < t; c++) {
7467768SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
7477768SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
7487768SJeff.Bonwick@Sun.COM 		    log) == 0);
7497768SJeff.Bonwick@Sun.COM 	}
750789Sahrens 
751789Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
752789Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
7537754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
754789Sahrens 	    child, t) == 0);
755789Sahrens 
756789Sahrens 	for (c = 0; c < t; c++)
757789Sahrens 		nvlist_free(child[c]);
758789Sahrens 
759789Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
760789Sahrens 
761789Sahrens 	return (root);
762789Sahrens }
763789Sahrens 
764*10922SJeff.Bonwick@Sun.COM static int
765*10922SJeff.Bonwick@Sun.COM ztest_random_blocksize(void)
766789Sahrens {
767*10922SJeff.Bonwick@Sun.COM 	return (1 << (SPA_MINBLOCKSHIFT +
768*10922SJeff.Bonwick@Sun.COM 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
769789Sahrens }
770789Sahrens 
771789Sahrens static int
772*10922SJeff.Bonwick@Sun.COM ztest_random_ibshift(void)
773*10922SJeff.Bonwick@Sun.COM {
774*10922SJeff.Bonwick@Sun.COM 	return (DN_MIN_INDBLKSHIFT +
775*10922SJeff.Bonwick@Sun.COM 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
776*10922SJeff.Bonwick@Sun.COM }
777*10922SJeff.Bonwick@Sun.COM 
778*10922SJeff.Bonwick@Sun.COM static uint64_t
779*10922SJeff.Bonwick@Sun.COM ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
780789Sahrens {
781*10922SJeff.Bonwick@Sun.COM 	uint64_t top;
782*10922SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
783*10922SJeff.Bonwick@Sun.COM 	vdev_t *tvd;
784*10922SJeff.Bonwick@Sun.COM 
785*10922SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
786*10922SJeff.Bonwick@Sun.COM 
787*10922SJeff.Bonwick@Sun.COM 	do {
788*10922SJeff.Bonwick@Sun.COM 		top = ztest_random(rvd->vdev_children);
789*10922SJeff.Bonwick@Sun.COM 		tvd = rvd->vdev_child[top];
790*10922SJeff.Bonwick@Sun.COM 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
791*10922SJeff.Bonwick@Sun.COM 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
792*10922SJeff.Bonwick@Sun.COM 
793*10922SJeff.Bonwick@Sun.COM 	return (top);
794*10922SJeff.Bonwick@Sun.COM }
795*10922SJeff.Bonwick@Sun.COM 
796*10922SJeff.Bonwick@Sun.COM static uint64_t
797*10922SJeff.Bonwick@Sun.COM ztest_random_dsl_prop(zfs_prop_t prop)
798*10922SJeff.Bonwick@Sun.COM {
799*10922SJeff.Bonwick@Sun.COM 	uint64_t value;
800*10922SJeff.Bonwick@Sun.COM 
801*10922SJeff.Bonwick@Sun.COM 	do {
802*10922SJeff.Bonwick@Sun.COM 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
803*10922SJeff.Bonwick@Sun.COM 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
804*10922SJeff.Bonwick@Sun.COM 
805*10922SJeff.Bonwick@Sun.COM 	return (value);
806*10922SJeff.Bonwick@Sun.COM }
807*10922SJeff.Bonwick@Sun.COM 
808*10922SJeff.Bonwick@Sun.COM static int
809*10922SJeff.Bonwick@Sun.COM ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
810*10922SJeff.Bonwick@Sun.COM     boolean_t inherit)
811*10922SJeff.Bonwick@Sun.COM {
812*10922SJeff.Bonwick@Sun.COM 	const char *propname = zfs_prop_to_name(prop);
813*10922SJeff.Bonwick@Sun.COM 	const char *valname;
814*10922SJeff.Bonwick@Sun.COM 	char setpoint[MAXPATHLEN];
815*10922SJeff.Bonwick@Sun.COM 	uint64_t curval;
816789Sahrens 	int error;
817789Sahrens 
818*10922SJeff.Bonwick@Sun.COM 	error = dsl_prop_set(osname, propname, sizeof (value),
819*10922SJeff.Bonwick@Sun.COM 	    inherit ? 0 : 1, &value);
820*10922SJeff.Bonwick@Sun.COM 
821*10922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
822*10922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
823789Sahrens 		return (error);
824789Sahrens 	}
8252082Seschrock 	ASSERT3U(error, ==, 0);
826*10922SJeff.Bonwick@Sun.COM 
827*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
828*10922SJeff.Bonwick@Sun.COM 	    1, &curval, setpoint), ==, 0);
829*10922SJeff.Bonwick@Sun.COM 
830*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
831*10922SJeff.Bonwick@Sun.COM 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
832*10922SJeff.Bonwick@Sun.COM 		(void) printf("%s %s = %s at '%s'\n",
833*10922SJeff.Bonwick@Sun.COM 		    osname, propname, valname, setpoint);
834789Sahrens 	}
835789Sahrens 
836789Sahrens 	return (error);
837789Sahrens }
838789Sahrens 
839*10922SJeff.Bonwick@Sun.COM #if 0
840789Sahrens static int
841*10922SJeff.Bonwick@Sun.COM ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
842*10922SJeff.Bonwick@Sun.COM {
843*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
844*10922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
845*10922SJeff.Bonwick@Sun.COM 	int error;
846*10922SJeff.Bonwick@Sun.COM 
847*10922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
848*10922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
849*10922SJeff.Bonwick@Sun.COM 
850*10922SJeff.Bonwick@Sun.COM 	error = spa_prop_set(spa, props);
851*10922SJeff.Bonwick@Sun.COM 
852*10922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
853*10922SJeff.Bonwick@Sun.COM 
854*10922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
855*10922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
856*10922SJeff.Bonwick@Sun.COM 		return (error);
857*10922SJeff.Bonwick@Sun.COM 	}
858*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(error, ==, 0);
859*10922SJeff.Bonwick@Sun.COM 
860*10922SJeff.Bonwick@Sun.COM 	return (error);
861*10922SJeff.Bonwick@Sun.COM }
862*10922SJeff.Bonwick@Sun.COM #endif
863*10922SJeff.Bonwick@Sun.COM 
864*10922SJeff.Bonwick@Sun.COM static void
865*10922SJeff.Bonwick@Sun.COM ztest_rll_init(rll_t *rll)
866*10922SJeff.Bonwick@Sun.COM {
867*10922SJeff.Bonwick@Sun.COM 	rll->rll_writer = NULL;
868*10922SJeff.Bonwick@Sun.COM 	rll->rll_readers = 0;
869*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
870*10922SJeff.Bonwick@Sun.COM 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
871*10922SJeff.Bonwick@Sun.COM }
872*10922SJeff.Bonwick@Sun.COM 
873*10922SJeff.Bonwick@Sun.COM static void
874*10922SJeff.Bonwick@Sun.COM ztest_rll_destroy(rll_t *rll)
875*10922SJeff.Bonwick@Sun.COM {
876*10922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_writer == NULL);
877*10922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_readers == 0);
878*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
879*10922SJeff.Bonwick@Sun.COM 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
880*10922SJeff.Bonwick@Sun.COM }
881*10922SJeff.Bonwick@Sun.COM 
882*10922SJeff.Bonwick@Sun.COM static void
883*10922SJeff.Bonwick@Sun.COM ztest_rll_lock(rll_t *rll, rl_type_t type)
884*10922SJeff.Bonwick@Sun.COM {
885*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
886*10922SJeff.Bonwick@Sun.COM 
887*10922SJeff.Bonwick@Sun.COM 	if (type == RL_READER) {
888*10922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL)
889*10922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
890*10922SJeff.Bonwick@Sun.COM 		rll->rll_readers++;
891*10922SJeff.Bonwick@Sun.COM 	} else {
892*10922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL || rll->rll_readers)
893*10922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
894*10922SJeff.Bonwick@Sun.COM 		rll->rll_writer = curthread;
895*10922SJeff.Bonwick@Sun.COM 	}
896*10922SJeff.Bonwick@Sun.COM 
897*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
898*10922SJeff.Bonwick@Sun.COM }
899*10922SJeff.Bonwick@Sun.COM 
900*10922SJeff.Bonwick@Sun.COM static void
901*10922SJeff.Bonwick@Sun.COM ztest_rll_unlock(rll_t *rll)
902*10922SJeff.Bonwick@Sun.COM {
903*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
904*10922SJeff.Bonwick@Sun.COM 
905*10922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer) {
906*10922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers == 0);
907*10922SJeff.Bonwick@Sun.COM 		rll->rll_writer = NULL;
908*10922SJeff.Bonwick@Sun.COM 	} else {
909*10922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers != 0);
910*10922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_writer == NULL);
911*10922SJeff.Bonwick@Sun.COM 		rll->rll_readers--;
912*10922SJeff.Bonwick@Sun.COM 	}
913*10922SJeff.Bonwick@Sun.COM 
914*10922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
915*10922SJeff.Bonwick@Sun.COM 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
916*10922SJeff.Bonwick@Sun.COM 
917*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
918*10922SJeff.Bonwick@Sun.COM }
919*10922SJeff.Bonwick@Sun.COM 
920*10922SJeff.Bonwick@Sun.COM static void
921*10922SJeff.Bonwick@Sun.COM ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
922*10922SJeff.Bonwick@Sun.COM {
923*10922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
924*10922SJeff.Bonwick@Sun.COM 
925*10922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
926*10922SJeff.Bonwick@Sun.COM }
927*10922SJeff.Bonwick@Sun.COM 
928*10922SJeff.Bonwick@Sun.COM static void
929*10922SJeff.Bonwick@Sun.COM ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
930*10922SJeff.Bonwick@Sun.COM {
931*10922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
932*10922SJeff.Bonwick@Sun.COM 
933*10922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
934*10922SJeff.Bonwick@Sun.COM }
935*10922SJeff.Bonwick@Sun.COM 
936*10922SJeff.Bonwick@Sun.COM static rl_t *
937*10922SJeff.Bonwick@Sun.COM ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
938*10922SJeff.Bonwick@Sun.COM     uint64_t size, rl_type_t type)
939*10922SJeff.Bonwick@Sun.COM {
940*10922SJeff.Bonwick@Sun.COM 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
941*10922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
942*10922SJeff.Bonwick@Sun.COM 	rl_t *rl;
943*10922SJeff.Bonwick@Sun.COM 
944*10922SJeff.Bonwick@Sun.COM 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
945*10922SJeff.Bonwick@Sun.COM 	rl->rl_object = object;
946*10922SJeff.Bonwick@Sun.COM 	rl->rl_offset = offset;
947*10922SJeff.Bonwick@Sun.COM 	rl->rl_size = size;
948*10922SJeff.Bonwick@Sun.COM 	rl->rl_lock = rll;
949*10922SJeff.Bonwick@Sun.COM 
950*10922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
951*10922SJeff.Bonwick@Sun.COM 
952*10922SJeff.Bonwick@Sun.COM 	return (rl);
953*10922SJeff.Bonwick@Sun.COM }
954*10922SJeff.Bonwick@Sun.COM 
955*10922SJeff.Bonwick@Sun.COM static void
956*10922SJeff.Bonwick@Sun.COM ztest_range_unlock(rl_t *rl)
957*10922SJeff.Bonwick@Sun.COM {
958*10922SJeff.Bonwick@Sun.COM 	rll_t *rll = rl->rl_lock;
959*10922SJeff.Bonwick@Sun.COM 
960*10922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
961*10922SJeff.Bonwick@Sun.COM 
962*10922SJeff.Bonwick@Sun.COM 	umem_free(rl, sizeof (*rl));
963*10922SJeff.Bonwick@Sun.COM }
964*10922SJeff.Bonwick@Sun.COM 
965*10922SJeff.Bonwick@Sun.COM static void
966*10922SJeff.Bonwick@Sun.COM ztest_zd_init(ztest_ds_t *zd, objset_t *os)
967*10922SJeff.Bonwick@Sun.COM {
968*10922SJeff.Bonwick@Sun.COM 	zd->zd_os = os;
969*10922SJeff.Bonwick@Sun.COM 	zd->zd_zilog = dmu_objset_zil(os);
970*10922SJeff.Bonwick@Sun.COM 	zd->zd_seq = 0;
971*10922SJeff.Bonwick@Sun.COM 	dmu_objset_name(os, zd->zd_name);
972*10922SJeff.Bonwick@Sun.COM 
973*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
974*10922SJeff.Bonwick@Sun.COM 
975*10922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
976*10922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_object_lock[l]);
977*10922SJeff.Bonwick@Sun.COM 
978*10922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
979*10922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_range_lock[l]);
980*10922SJeff.Bonwick@Sun.COM }
981*10922SJeff.Bonwick@Sun.COM 
982*10922SJeff.Bonwick@Sun.COM static void
983*10922SJeff.Bonwick@Sun.COM ztest_zd_fini(ztest_ds_t *zd)
984*10922SJeff.Bonwick@Sun.COM {
985*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
986*10922SJeff.Bonwick@Sun.COM 
987*10922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
988*10922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_object_lock[l]);
989*10922SJeff.Bonwick@Sun.COM 
990*10922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
991*10922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_range_lock[l]);
992*10922SJeff.Bonwick@Sun.COM }
993*10922SJeff.Bonwick@Sun.COM 
994*10922SJeff.Bonwick@Sun.COM #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
995*10922SJeff.Bonwick@Sun.COM 
996*10922SJeff.Bonwick@Sun.COM static uint64_t
997*10922SJeff.Bonwick@Sun.COM ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
998789Sahrens {
999*10922SJeff.Bonwick@Sun.COM 	uint64_t txg;
1000*10922SJeff.Bonwick@Sun.COM 	int error;
1001*10922SJeff.Bonwick@Sun.COM 
1002*10922SJeff.Bonwick@Sun.COM 	/*
1003*10922SJeff.Bonwick@Sun.COM 	 * Attempt to assign tx to some transaction group.
1004*10922SJeff.Bonwick@Sun.COM 	 */
1005*10922SJeff.Bonwick@Sun.COM 	error = dmu_tx_assign(tx, txg_how);
1006*10922SJeff.Bonwick@Sun.COM 	if (error) {
1007*10922SJeff.Bonwick@Sun.COM 		if (error == ERESTART) {
1008*10922SJeff.Bonwick@Sun.COM 			ASSERT(txg_how == TXG_NOWAIT);
1009*10922SJeff.Bonwick@Sun.COM 			dmu_tx_wait(tx);
1010*10922SJeff.Bonwick@Sun.COM 		} else {
1011*10922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
1012*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(tag);
1013*10922SJeff.Bonwick@Sun.COM 		}
1014*10922SJeff.Bonwick@Sun.COM 		dmu_tx_abort(tx);
1015*10922SJeff.Bonwick@Sun.COM 		return (0);
1016*10922SJeff.Bonwick@Sun.COM 	}
1017*10922SJeff.Bonwick@Sun.COM 	txg = dmu_tx_get_txg(tx);
1018*10922SJeff.Bonwick@Sun.COM 	ASSERT(txg != 0);
1019*10922SJeff.Bonwick@Sun.COM 	return (txg);
1020*10922SJeff.Bonwick@Sun.COM }
1021*10922SJeff.Bonwick@Sun.COM 
1022*10922SJeff.Bonwick@Sun.COM static void
1023*10922SJeff.Bonwick@Sun.COM ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1024*10922SJeff.Bonwick@Sun.COM {
1025*10922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
1026*10922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1027*10922SJeff.Bonwick@Sun.COM 
1028*10922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
1029*10922SJeff.Bonwick@Sun.COM 		*ip++ = value;
1030*10922SJeff.Bonwick@Sun.COM }
1031*10922SJeff.Bonwick@Sun.COM 
1032*10922SJeff.Bonwick@Sun.COM static boolean_t
1033*10922SJeff.Bonwick@Sun.COM ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1034*10922SJeff.Bonwick@Sun.COM {
1035*10922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
1036*10922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1037*10922SJeff.Bonwick@Sun.COM 	uint64_t diff = 0;
1038*10922SJeff.Bonwick@Sun.COM 
1039*10922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
1040*10922SJeff.Bonwick@Sun.COM 		diff |= (value - *ip++);
1041*10922SJeff.Bonwick@Sun.COM 
1042*10922SJeff.Bonwick@Sun.COM 	return (diff == 0);
1043*10922SJeff.Bonwick@Sun.COM }
1044*10922SJeff.Bonwick@Sun.COM 
1045*10922SJeff.Bonwick@Sun.COM static void
1046*10922SJeff.Bonwick@Sun.COM ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1047*10922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1048*10922SJeff.Bonwick@Sun.COM {
1049*10922SJeff.Bonwick@Sun.COM 	bt->bt_magic = BT_MAGIC;
1050*10922SJeff.Bonwick@Sun.COM 	bt->bt_objset = dmu_objset_id(os);
1051*10922SJeff.Bonwick@Sun.COM 	bt->bt_object = object;
1052*10922SJeff.Bonwick@Sun.COM 	bt->bt_offset = offset;
1053*10922SJeff.Bonwick@Sun.COM 	bt->bt_gen = gen;
1054*10922SJeff.Bonwick@Sun.COM 	bt->bt_txg = txg;
1055*10922SJeff.Bonwick@Sun.COM 	bt->bt_crtxg = crtxg;
1056*10922SJeff.Bonwick@Sun.COM }
1057*10922SJeff.Bonwick@Sun.COM 
1058*10922SJeff.Bonwick@Sun.COM static void
1059*10922SJeff.Bonwick@Sun.COM ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1060*10922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1061*10922SJeff.Bonwick@Sun.COM {
1062*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_magic == BT_MAGIC);
1063*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_objset == dmu_objset_id(os));
1064*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_object == object);
1065*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_offset == offset);
1066*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_gen <= gen);
1067*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_txg <= txg);
1068*10922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_crtxg == crtxg);
1069*10922SJeff.Bonwick@Sun.COM }
1070*10922SJeff.Bonwick@Sun.COM 
1071*10922SJeff.Bonwick@Sun.COM static ztest_block_tag_t *
1072*10922SJeff.Bonwick@Sun.COM ztest_bt_bonus(dmu_buf_t *db)
1073*10922SJeff.Bonwick@Sun.COM {
1074*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
1075*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt;
1076*10922SJeff.Bonwick@Sun.COM 
1077*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
1078*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1079*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1080*10922SJeff.Bonwick@Sun.COM 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1081*10922SJeff.Bonwick@Sun.COM 
1082*10922SJeff.Bonwick@Sun.COM 	return (bt);
1083*10922SJeff.Bonwick@Sun.COM }
1084*10922SJeff.Bonwick@Sun.COM 
1085*10922SJeff.Bonwick@Sun.COM /*
1086*10922SJeff.Bonwick@Sun.COM  * ZIL logging ops
1087*10922SJeff.Bonwick@Sun.COM  */
1088*10922SJeff.Bonwick@Sun.COM 
1089*10922SJeff.Bonwick@Sun.COM #define	lrz_type	lr_mode
1090*10922SJeff.Bonwick@Sun.COM #define	lrz_blocksize	lr_uid
1091*10922SJeff.Bonwick@Sun.COM #define	lrz_ibshift	lr_gid
1092*10922SJeff.Bonwick@Sun.COM #define	lrz_bonustype	lr_rdev
1093*10922SJeff.Bonwick@Sun.COM #define	lrz_bonuslen	lr_crtime[1]
1094*10922SJeff.Bonwick@Sun.COM 
1095*10922SJeff.Bonwick@Sun.COM static uint64_t
1096*10922SJeff.Bonwick@Sun.COM ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1097*10922SJeff.Bonwick@Sun.COM {
1098*10922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
1099*10922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
1100*10922SJeff.Bonwick@Sun.COM 	itx_t *itx;
1101*10922SJeff.Bonwick@Sun.COM 
1102*10922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
1103*10922SJeff.Bonwick@Sun.COM 		return (0);
1104*10922SJeff.Bonwick@Sun.COM 
1105*10922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1106*10922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1107*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
1108*10922SJeff.Bonwick@Sun.COM 
1109*10922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
1110*10922SJeff.Bonwick@Sun.COM }
1111*10922SJeff.Bonwick@Sun.COM 
1112*10922SJeff.Bonwick@Sun.COM static uint64_t
1113*10922SJeff.Bonwick@Sun.COM ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr)
1114*10922SJeff.Bonwick@Sun.COM {
1115*10922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
1116*10922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
1117*10922SJeff.Bonwick@Sun.COM 	itx_t *itx;
1118*10922SJeff.Bonwick@Sun.COM 
1119*10922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
1120*10922SJeff.Bonwick@Sun.COM 		return (0);
1121*10922SJeff.Bonwick@Sun.COM 
1122*10922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1123*10922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1124*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
1125*10922SJeff.Bonwick@Sun.COM 
1126*10922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
1127*10922SJeff.Bonwick@Sun.COM }
1128*10922SJeff.Bonwick@Sun.COM 
1129*10922SJeff.Bonwick@Sun.COM static uint64_t
1130*10922SJeff.Bonwick@Sun.COM ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1131*10922SJeff.Bonwick@Sun.COM {
1132*10922SJeff.Bonwick@Sun.COM 	itx_t *itx;
1133*10922SJeff.Bonwick@Sun.COM 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1134*10922SJeff.Bonwick@Sun.COM 
1135*10922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
1136*10922SJeff.Bonwick@Sun.COM 		return (0);
1137*10922SJeff.Bonwick@Sun.COM 
1138*10922SJeff.Bonwick@Sun.COM 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
1139*10922SJeff.Bonwick@Sun.COM 		write_state = WR_INDIRECT;
1140*10922SJeff.Bonwick@Sun.COM 
1141*10922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_WRITE,
1142*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1143*10922SJeff.Bonwick@Sun.COM 
1144*10922SJeff.Bonwick@Sun.COM 	if (write_state == WR_COPIED &&
1145*10922SJeff.Bonwick@Sun.COM 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1146*10922SJeff.Bonwick@Sun.COM 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1147*10922SJeff.Bonwick@Sun.COM 		zil_itx_destroy(itx);
1148*10922SJeff.Bonwick@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1149*10922SJeff.Bonwick@Sun.COM 		write_state = WR_NEED_COPY;
1150*10922SJeff.Bonwick@Sun.COM 	}
1151*10922SJeff.Bonwick@Sun.COM 	itx->itx_private = zd;
1152*10922SJeff.Bonwick@Sun.COM 	itx->itx_wr_state = write_state;
1153*10922SJeff.Bonwick@Sun.COM 	itx->itx_sync = (ztest_random(8) == 0);
1154*10922SJeff.Bonwick@Sun.COM 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1155*10922SJeff.Bonwick@Sun.COM 
1156*10922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1157*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
1158*10922SJeff.Bonwick@Sun.COM 
1159*10922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
1160*10922SJeff.Bonwick@Sun.COM }
1161*10922SJeff.Bonwick@Sun.COM 
1162*10922SJeff.Bonwick@Sun.COM static uint64_t
1163*10922SJeff.Bonwick@Sun.COM ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1164*10922SJeff.Bonwick@Sun.COM {
1165*10922SJeff.Bonwick@Sun.COM 	itx_t *itx;
1166*10922SJeff.Bonwick@Sun.COM 
1167*10922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
1168*10922SJeff.Bonwick@Sun.COM 		return (0);
1169*10922SJeff.Bonwick@Sun.COM 
1170*10922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1171*10922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1172*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
1173*10922SJeff.Bonwick@Sun.COM 
1174*10922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
1175*10922SJeff.Bonwick@Sun.COM }
1176*10922SJeff.Bonwick@Sun.COM 
1177*10922SJeff.Bonwick@Sun.COM static uint64_t
1178*10922SJeff.Bonwick@Sun.COM ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1179*10922SJeff.Bonwick@Sun.COM {
1180*10922SJeff.Bonwick@Sun.COM 	itx_t *itx;
1181*10922SJeff.Bonwick@Sun.COM 
1182*10922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
1183*10922SJeff.Bonwick@Sun.COM 		return (0);
1184*10922SJeff.Bonwick@Sun.COM 
1185*10922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1186*10922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1187*10922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
1188*10922SJeff.Bonwick@Sun.COM 
1189*10922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
1190*10922SJeff.Bonwick@Sun.COM }
1191*10922SJeff.Bonwick@Sun.COM 
1192*10922SJeff.Bonwick@Sun.COM /*
1193*10922SJeff.Bonwick@Sun.COM  * ZIL replay ops
1194*10922SJeff.Bonwick@Sun.COM  */
1195*10922SJeff.Bonwick@Sun.COM static int
1196*10922SJeff.Bonwick@Sun.COM ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1197*10922SJeff.Bonwick@Sun.COM {
1198*10922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
1199*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1200*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
1201*10922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1202789Sahrens 	dmu_tx_t *tx;
1203*10922SJeff.Bonwick@Sun.COM 	uint64_t txg;
1204*10922SJeff.Bonwick@Sun.COM 	int error = 0;
1205789Sahrens 
1206789Sahrens 	if (byteswap)
1207789Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
1208789Sahrens 
1209*10922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1210*10922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
1211*10922SJeff.Bonwick@Sun.COM 
1212789Sahrens 	tx = dmu_tx_create(os);
1213*10922SJeff.Bonwick@Sun.COM 
1214*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1215*10922SJeff.Bonwick@Sun.COM 
1216*10922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1217*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1218*10922SJeff.Bonwick@Sun.COM 	} else {
1219*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1220*10922SJeff.Bonwick@Sun.COM 	}
1221*10922SJeff.Bonwick@Sun.COM 
1222*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1223*10922SJeff.Bonwick@Sun.COM 	if (txg == 0)
1224*10922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
1225*10922SJeff.Bonwick@Sun.COM 
1226*10922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1227*10922SJeff.Bonwick@Sun.COM 
1228*10922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1229*10922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
1230*10922SJeff.Bonwick@Sun.COM 			lr->lr_foid = zap_create(os,
1231*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
1232*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
1233*10922SJeff.Bonwick@Sun.COM 		} else {
1234*10922SJeff.Bonwick@Sun.COM 			error = zap_create_claim(os, lr->lr_foid,
1235*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
1236*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
1237*10922SJeff.Bonwick@Sun.COM 		}
1238*10922SJeff.Bonwick@Sun.COM 	} else {
1239*10922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
1240*10922SJeff.Bonwick@Sun.COM 			lr->lr_foid = dmu_object_alloc(os,
1241*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
1242*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
1243*10922SJeff.Bonwick@Sun.COM 		} else {
1244*10922SJeff.Bonwick@Sun.COM 			error = dmu_object_claim(os, lr->lr_foid,
1245*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
1246*10922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
1247*10922SJeff.Bonwick@Sun.COM 		}
1248*10922SJeff.Bonwick@Sun.COM 	}
1249*10922SJeff.Bonwick@Sun.COM 
1250789Sahrens 	if (error) {
1251*10922SJeff.Bonwick@Sun.COM 		ASSERT3U(error, ==, EEXIST);
1252*10922SJeff.Bonwick@Sun.COM 		ASSERT(zd->zd_zilog->zl_replay);
1253*10922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1254789Sahrens 		return (error);
1255789Sahrens 	}
1256789Sahrens 
1257*10922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_foid != 0);
1258*10922SJeff.Bonwick@Sun.COM 
1259*10922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1260*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1261*10922SJeff.Bonwick@Sun.COM 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
1262*10922SJeff.Bonwick@Sun.COM 
1263*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1264*10922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
1265*10922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
1266*10922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1267*10922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
1268*10922SJeff.Bonwick@Sun.COM 
1269*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1270*10922SJeff.Bonwick@Sun.COM 	    &lr->lr_foid, tx));
1271*10922SJeff.Bonwick@Sun.COM 
1272*10922SJeff.Bonwick@Sun.COM 	(void) ztest_log_create(zd, tx, lr);
1273*10922SJeff.Bonwick@Sun.COM 
1274*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
1275*10922SJeff.Bonwick@Sun.COM 
1276*10922SJeff.Bonwick@Sun.COM 	return (0);
1277*10922SJeff.Bonwick@Sun.COM }
1278*10922SJeff.Bonwick@Sun.COM 
1279*10922SJeff.Bonwick@Sun.COM static int
1280*10922SJeff.Bonwick@Sun.COM ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1281*10922SJeff.Bonwick@Sun.COM {
1282*10922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
1283*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1284*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
1285*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
1286*10922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
1287*10922SJeff.Bonwick@Sun.COM 
1288*10922SJeff.Bonwick@Sun.COM 	if (byteswap)
1289*10922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
1290*10922SJeff.Bonwick@Sun.COM 
1291*10922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1292*10922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
1293*10922SJeff.Bonwick@Sun.COM 
1294*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
1295*10922SJeff.Bonwick@Sun.COM 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1296*10922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
1297*10922SJeff.Bonwick@Sun.COM 
1298*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_WRITER);
1299*10922SJeff.Bonwick@Sun.COM 
1300*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1301*10922SJeff.Bonwick@Sun.COM 
1302*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
1303*10922SJeff.Bonwick@Sun.COM 
1304*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1305*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1306*10922SJeff.Bonwick@Sun.COM 
1307*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1308*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
1309*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
1310*10922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
1311*10922SJeff.Bonwick@Sun.COM 	}
1312*10922SJeff.Bonwick@Sun.COM 
1313*10922SJeff.Bonwick@Sun.COM 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1314*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
1315*10922SJeff.Bonwick@Sun.COM 	} else {
1316*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1317*10922SJeff.Bonwick@Sun.COM 	}
1318*10922SJeff.Bonwick@Sun.COM 
1319*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1320*10922SJeff.Bonwick@Sun.COM 
1321*10922SJeff.Bonwick@Sun.COM 	(void) ztest_log_remove(zd, tx, lr);
1322*10922SJeff.Bonwick@Sun.COM 
1323789Sahrens 	dmu_tx_commit(tx);
1324789Sahrens 
1325*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
1326*10922SJeff.Bonwick@Sun.COM 
1327*10922SJeff.Bonwick@Sun.COM 	return (0);
1328*10922SJeff.Bonwick@Sun.COM }
1329*10922SJeff.Bonwick@Sun.COM 
1330*10922SJeff.Bonwick@Sun.COM static int
1331*10922SJeff.Bonwick@Sun.COM ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1332*10922SJeff.Bonwick@Sun.COM {
1333*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1334*10922SJeff.Bonwick@Sun.COM 	void *data = lr + 1;			/* data follows lr */
1335*10922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
1336*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt = data;
1337*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
1338*10922SJeff.Bonwick@Sun.COM 	uint64_t gen, txg, lrtxg, crtxg;
1339*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
1340*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
1341*10922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1342*10922SJeff.Bonwick@Sun.COM 	arc_buf_t *abuf = NULL;
1343*10922SJeff.Bonwick@Sun.COM 	rl_t *rl;
1344*10922SJeff.Bonwick@Sun.COM 
1345*10922SJeff.Bonwick@Sun.COM 	if (byteswap)
1346*10922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
1347*10922SJeff.Bonwick@Sun.COM 
1348*10922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
1349*10922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
1350*10922SJeff.Bonwick@Sun.COM 
1351*10922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
1352*10922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1353*10922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1354*10922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
1355*10922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
1356*10922SJeff.Bonwick@Sun.COM 			length = blocksize;
1357*10922SJeff.Bonwick@Sun.COM 		}
1358*10922SJeff.Bonwick@Sun.COM 	}
1359*10922SJeff.Bonwick@Sun.COM 
1360*10922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1361*10922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(bt, sizeof (*bt));
1362*10922SJeff.Bonwick@Sun.COM 
1363*10922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic != BT_MAGIC)
1364*10922SJeff.Bonwick@Sun.COM 		bt = NULL;
1365*10922SJeff.Bonwick@Sun.COM 
1366*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1367*10922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1368*10922SJeff.Bonwick@Sun.COM 
1369*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1370*10922SJeff.Bonwick@Sun.COM 
1371*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
1372*10922SJeff.Bonwick@Sun.COM 
1373*10922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
1374*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1375*10922SJeff.Bonwick@Sun.COM 	gen = bbt->bt_gen;
1376*10922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
1377*10922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
1378*10922SJeff.Bonwick@Sun.COM 
1379*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
1380*10922SJeff.Bonwick@Sun.COM 
1381*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1382*10922SJeff.Bonwick@Sun.COM 
1383*10922SJeff.Bonwick@Sun.COM 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1384*10922SJeff.Bonwick@Sun.COM 	    P2PHASE(offset, length) == 0)
1385*10922SJeff.Bonwick@Sun.COM 		abuf = dmu_request_arcbuf(db, length);
1386*10922SJeff.Bonwick@Sun.COM 
1387*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1388*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
1389*10922SJeff.Bonwick@Sun.COM 		if (abuf != NULL)
1390*10922SJeff.Bonwick@Sun.COM 			dmu_return_arcbuf(abuf);
1391*10922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
1392*10922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
1393*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
1394*10922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
1395*10922SJeff.Bonwick@Sun.COM 	}
1396*10922SJeff.Bonwick@Sun.COM 
1397*10922SJeff.Bonwick@Sun.COM 	if (bt != NULL) {
1398*10922SJeff.Bonwick@Sun.COM 		/*
1399*10922SJeff.Bonwick@Sun.COM 		 * Usually, verify the old data before writing new data --
1400*10922SJeff.Bonwick@Sun.COM 		 * but not always, because we also want to verify correct
1401*10922SJeff.Bonwick@Sun.COM 		 * behavior when the data was not recently read into cache.
1402*10922SJeff.Bonwick@Sun.COM 		 */
1403*10922SJeff.Bonwick@Sun.COM 		ASSERT(offset % doi.doi_data_block_size == 0);
1404*10922SJeff.Bonwick@Sun.COM 		if (ztest_random(4) != 0) {
1405*10922SJeff.Bonwick@Sun.COM 			int prefetch = ztest_random(2) ?
1406*10922SJeff.Bonwick@Sun.COM 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1407*10922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t rbt;
1408*10922SJeff.Bonwick@Sun.COM 
1409*10922SJeff.Bonwick@Sun.COM 			VERIFY(dmu_read(os, lr->lr_foid, offset,
1410*10922SJeff.Bonwick@Sun.COM 			    sizeof (rbt), &rbt, prefetch) == 0);
1411*10922SJeff.Bonwick@Sun.COM 			if (rbt.bt_magic == BT_MAGIC) {
1412*10922SJeff.Bonwick@Sun.COM 				ztest_bt_verify(&rbt, os, lr->lr_foid,
1413*10922SJeff.Bonwick@Sun.COM 				    offset, gen, txg, crtxg);
1414*10922SJeff.Bonwick@Sun.COM 			}
1415*10922SJeff.Bonwick@Sun.COM 		}
1416*10922SJeff.Bonwick@Sun.COM 
1417*10922SJeff.Bonwick@Sun.COM 		/*
1418*10922SJeff.Bonwick@Sun.COM 		 * Writes can appear to be newer than the bonus buffer because
1419*10922SJeff.Bonwick@Sun.COM 		 * the ztest_get_data() callback does a dmu_read() of the
1420*10922SJeff.Bonwick@Sun.COM 		 * open-context data, which may be different than the data
1421*10922SJeff.Bonwick@Sun.COM 		 * as it was when the write was generated.
1422*10922SJeff.Bonwick@Sun.COM 		 */
1423*10922SJeff.Bonwick@Sun.COM 		if (zd->zd_zilog->zl_replay) {
1424*10922SJeff.Bonwick@Sun.COM 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
1425*10922SJeff.Bonwick@Sun.COM 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1426*10922SJeff.Bonwick@Sun.COM 			    bt->bt_crtxg);
1427*10922SJeff.Bonwick@Sun.COM 		}
1428*10922SJeff.Bonwick@Sun.COM 
1429*10922SJeff.Bonwick@Sun.COM 		/*
1430*10922SJeff.Bonwick@Sun.COM 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
1431*10922SJeff.Bonwick@Sun.COM 		 * so that all of the usual ASSERTs will work.
1432*10922SJeff.Bonwick@Sun.COM 		 */
1433*10922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1434*10922SJeff.Bonwick@Sun.COM 	}
1435*10922SJeff.Bonwick@Sun.COM 
1436*10922SJeff.Bonwick@Sun.COM 	if (abuf == NULL) {
1437*10922SJeff.Bonwick@Sun.COM 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
1438*10922SJeff.Bonwick@Sun.COM 	} else {
1439*10922SJeff.Bonwick@Sun.COM 		bcopy(data, abuf->b_data, length);
1440*10922SJeff.Bonwick@Sun.COM 		dmu_assign_arcbuf(db, offset, abuf, tx);
1441*10922SJeff.Bonwick@Sun.COM 	}
1442*10922SJeff.Bonwick@Sun.COM 
1443*10922SJeff.Bonwick@Sun.COM 	(void) ztest_log_write(zd, tx, lr);
1444*10922SJeff.Bonwick@Sun.COM 
1445*10922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
1446*10922SJeff.Bonwick@Sun.COM 
1447*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
1448*10922SJeff.Bonwick@Sun.COM 
1449*10922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
1450*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
1451*10922SJeff.Bonwick@Sun.COM 
1452*10922SJeff.Bonwick@Sun.COM 	return (0);
1453*10922SJeff.Bonwick@Sun.COM }
1454*10922SJeff.Bonwick@Sun.COM 
1455*10922SJeff.Bonwick@Sun.COM static int
1456*10922SJeff.Bonwick@Sun.COM ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1457*10922SJeff.Bonwick@Sun.COM {
1458*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1459*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
1460*10922SJeff.Bonwick@Sun.COM 	uint64_t txg;
1461*10922SJeff.Bonwick@Sun.COM 	rl_t *rl;
1462*10922SJeff.Bonwick@Sun.COM 
1463*10922SJeff.Bonwick@Sun.COM 	if (byteswap)
1464*10922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
1465*10922SJeff.Bonwick@Sun.COM 
1466*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
1467*10922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1468*10922SJeff.Bonwick@Sun.COM 	    RL_WRITER);
1469*10922SJeff.Bonwick@Sun.COM 
1470*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
1471*10922SJeff.Bonwick@Sun.COM 
1472*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1473*10922SJeff.Bonwick@Sun.COM 
1474*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1475*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
1476*10922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
1477*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
1478*10922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
1479*10922SJeff.Bonwick@Sun.COM 	}
1480*10922SJeff.Bonwick@Sun.COM 
1481*10922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1482*10922SJeff.Bonwick@Sun.COM 	    lr->lr_length, tx) == 0);
1483*10922SJeff.Bonwick@Sun.COM 
1484*10922SJeff.Bonwick@Sun.COM 	(void) ztest_log_truncate(zd, tx, lr);
1485*10922SJeff.Bonwick@Sun.COM 
1486*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
1487*10922SJeff.Bonwick@Sun.COM 
1488*10922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
1489*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
1490*10922SJeff.Bonwick@Sun.COM 
1491*10922SJeff.Bonwick@Sun.COM 	return (0);
1492*10922SJeff.Bonwick@Sun.COM }
1493*10922SJeff.Bonwick@Sun.COM 
1494*10922SJeff.Bonwick@Sun.COM static int
1495*10922SJeff.Bonwick@Sun.COM ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1496*10922SJeff.Bonwick@Sun.COM {
1497*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1498*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
1499*10922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1500*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
1501*10922SJeff.Bonwick@Sun.COM 	uint64_t txg, lrtxg, crtxg;
1502*10922SJeff.Bonwick@Sun.COM 
1503*10922SJeff.Bonwick@Sun.COM 	if (byteswap)
1504*10922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
1505*10922SJeff.Bonwick@Sun.COM 
1506*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1507*10922SJeff.Bonwick@Sun.COM 
1508*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1509*10922SJeff.Bonwick@Sun.COM 
1510*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
1511*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_bonus(tx, lr->lr_foid);
1512*10922SJeff.Bonwick@Sun.COM 
1513*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1514*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
1515*10922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
1516*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
1517*10922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
1518*10922SJeff.Bonwick@Sun.COM 	}
1519*10922SJeff.Bonwick@Sun.COM 
1520*10922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
1521*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1522*10922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
1523*10922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
1524*10922SJeff.Bonwick@Sun.COM 
1525*10922SJeff.Bonwick@Sun.COM 	if (zd->zd_zilog->zl_replay) {
1526*10922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_size != 0);
1527*10922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_mode != 0);
1528*10922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg != 0);
1529*10922SJeff.Bonwick@Sun.COM 	} else {
1530*10922SJeff.Bonwick@Sun.COM 		/*
1531*10922SJeff.Bonwick@Sun.COM 		 * Randomly change the size and increment the generation.
1532*10922SJeff.Bonwick@Sun.COM 		 */
1533*10922SJeff.Bonwick@Sun.COM 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1534*10922SJeff.Bonwick@Sun.COM 		    sizeof (*bbt);
1535*10922SJeff.Bonwick@Sun.COM 		lr->lr_mode = bbt->bt_gen + 1;
1536*10922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg == 0);
1537*10922SJeff.Bonwick@Sun.COM 	}
1538*10922SJeff.Bonwick@Sun.COM 
1539*10922SJeff.Bonwick@Sun.COM 	/*
1540*10922SJeff.Bonwick@Sun.COM 	 * Verify that the current bonus buffer is not newer than our txg.
1541*10922SJeff.Bonwick@Sun.COM 	 */
1542*10922SJeff.Bonwick@Sun.COM 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1543*10922SJeff.Bonwick@Sun.COM 	    MAX(txg, lrtxg), crtxg);
1544*10922SJeff.Bonwick@Sun.COM 
1545*10922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
1546*10922SJeff.Bonwick@Sun.COM 
1547*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1548*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, <=, db->db_size);
1549*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1550*10922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
1551*10922SJeff.Bonwick@Sun.COM 
1552*10922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1553*10922SJeff.Bonwick@Sun.COM 
1554*10922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
1555*10922SJeff.Bonwick@Sun.COM 
1556*10922SJeff.Bonwick@Sun.COM 	(void) ztest_log_setattr(zd, tx, lr);
1557*10922SJeff.Bonwick@Sun.COM 
1558*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
1559*10922SJeff.Bonwick@Sun.COM 
1560*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
1561*10922SJeff.Bonwick@Sun.COM 
1562*10922SJeff.Bonwick@Sun.COM 	return (0);
1563789Sahrens }
1564789Sahrens 
1565789Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1566789Sahrens 	NULL,			/* 0 no such transaction type */
1567789Sahrens 	ztest_replay_create,	/* TX_CREATE */
1568789Sahrens 	NULL,			/* TX_MKDIR */
1569789Sahrens 	NULL,			/* TX_MKXATTR */
1570789Sahrens 	NULL,			/* TX_SYMLINK */
1571789Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
1572789Sahrens 	NULL,			/* TX_RMDIR */
1573789Sahrens 	NULL,			/* TX_LINK */
1574789Sahrens 	NULL,			/* TX_RENAME */
1575*10922SJeff.Bonwick@Sun.COM 	ztest_replay_write,	/* TX_WRITE */
1576*10922SJeff.Bonwick@Sun.COM 	ztest_replay_truncate,	/* TX_TRUNCATE */
1577*10922SJeff.Bonwick@Sun.COM 	ztest_replay_setattr,	/* TX_SETATTR */
1578789Sahrens 	NULL,			/* TX_ACL */
157910800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL */
158010800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ATTR */
158110800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL_ATTR */
158210800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL */
158310800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ATTR */
158410800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL_ATTR */
158510800SNeil.Perrin@Sun.COM 	NULL,			/* TX_WRITE2 */
1586789Sahrens };
1587789Sahrens 
1588789Sahrens /*
1589*10922SJeff.Bonwick@Sun.COM  * ZIL get_data callbacks
1590*10922SJeff.Bonwick@Sun.COM  */
1591*10922SJeff.Bonwick@Sun.COM 
1592*10922SJeff.Bonwick@Sun.COM static void
1593*10922SJeff.Bonwick@Sun.COM ztest_get_done(zgd_t *zgd, int error)
1594*10922SJeff.Bonwick@Sun.COM {
1595*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = zgd->zgd_private;
1596*10922SJeff.Bonwick@Sun.COM 	uint64_t object = zgd->zgd_rl->rl_object;
1597*10922SJeff.Bonwick@Sun.COM 
1598*10922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
1599*10922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
1600*10922SJeff.Bonwick@Sun.COM 
1601*10922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(zgd->zgd_rl);
1602*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
1603*10922SJeff.Bonwick@Sun.COM 
1604*10922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
1605*10922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1606*10922SJeff.Bonwick@Sun.COM 
1607*10922SJeff.Bonwick@Sun.COM 	umem_free(zgd, sizeof (*zgd));
1608*10922SJeff.Bonwick@Sun.COM }
1609*10922SJeff.Bonwick@Sun.COM 
1610*10922SJeff.Bonwick@Sun.COM static int
1611*10922SJeff.Bonwick@Sun.COM ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1612*10922SJeff.Bonwick@Sun.COM {
1613*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = arg;
1614*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1615*10922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
1616*10922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
1617*10922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
1618*10922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
1619*10922SJeff.Bonwick@Sun.COM 	uint64_t txg = lr->lr_common.lrc_txg;
1620*10922SJeff.Bonwick@Sun.COM 	uint64_t crtxg;
1621*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
1622*10922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1623*10922SJeff.Bonwick@Sun.COM 	zgd_t *zgd;
1624*10922SJeff.Bonwick@Sun.COM 	int error;
1625*10922SJeff.Bonwick@Sun.COM 
1626*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
1627*10922SJeff.Bonwick@Sun.COM 	error = dmu_bonus_hold(os, object, FTAG, &db);
1628*10922SJeff.Bonwick@Sun.COM 	if (error) {
1629*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
1630*10922SJeff.Bonwick@Sun.COM 		return (error);
1631*10922SJeff.Bonwick@Sun.COM 	}
1632*10922SJeff.Bonwick@Sun.COM 
1633*10922SJeff.Bonwick@Sun.COM 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
1634*10922SJeff.Bonwick@Sun.COM 
1635*10922SJeff.Bonwick@Sun.COM 	if (crtxg == 0 || crtxg > txg) {
1636*10922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
1637*10922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
1638*10922SJeff.Bonwick@Sun.COM 		return (ENOENT);
1639*10922SJeff.Bonwick@Sun.COM 	}
1640*10922SJeff.Bonwick@Sun.COM 
1641*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
1642*10922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
1643*10922SJeff.Bonwick@Sun.COM 	db = NULL;
1644*10922SJeff.Bonwick@Sun.COM 
1645*10922SJeff.Bonwick@Sun.COM 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1646*10922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zd->zd_zilog;
1647*10922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zd;
1648*10922SJeff.Bonwick@Sun.COM 
1649*10922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
1650*10922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1651*10922SJeff.Bonwick@Sun.COM 		    RL_READER);
1652*10922SJeff.Bonwick@Sun.COM 
1653*10922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
1654*10922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
1655*10922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0);
1656*10922SJeff.Bonwick@Sun.COM 	} else {
1657*10922SJeff.Bonwick@Sun.COM 		size = doi.doi_data_block_size;
1658*10922SJeff.Bonwick@Sun.COM 		if (ISP2(size))
1659*10922SJeff.Bonwick@Sun.COM 			offset = P2ALIGN(offset, size);
1660*10922SJeff.Bonwick@Sun.COM 
1661*10922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1662*10922SJeff.Bonwick@Sun.COM 		    RL_READER);
1663*10922SJeff.Bonwick@Sun.COM 
1664*10922SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db);
1665*10922SJeff.Bonwick@Sun.COM 
1666*10922SJeff.Bonwick@Sun.COM 		if (error == 0) {
1667*10922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
1668*10922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
1669*10922SJeff.Bonwick@Sun.COM 
1670*10922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
1671*10922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
1672*10922SJeff.Bonwick@Sun.COM 
1673*10922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1674*10922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
1675*10922SJeff.Bonwick@Sun.COM 
1676*10922SJeff.Bonwick@Sun.COM 			if (error == 0)
1677*10922SJeff.Bonwick@Sun.COM 				return (0);
1678*10922SJeff.Bonwick@Sun.COM 		}
1679*10922SJeff.Bonwick@Sun.COM 	}
1680*10922SJeff.Bonwick@Sun.COM 
1681*10922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
1682*10922SJeff.Bonwick@Sun.COM 
1683*10922SJeff.Bonwick@Sun.COM 	return (error);
1684*10922SJeff.Bonwick@Sun.COM }
1685*10922SJeff.Bonwick@Sun.COM 
1686*10922SJeff.Bonwick@Sun.COM static void *
1687*10922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
1688*10922SJeff.Bonwick@Sun.COM {
1689*10922SJeff.Bonwick@Sun.COM 	char *lr;
1690*10922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
1691*10922SJeff.Bonwick@Sun.COM 
1692*10922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1693*10922SJeff.Bonwick@Sun.COM 
1694*10922SJeff.Bonwick@Sun.COM 	if (name)
1695*10922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
1696*10922SJeff.Bonwick@Sun.COM 
1697*10922SJeff.Bonwick@Sun.COM 	return (lr);
1698*10922SJeff.Bonwick@Sun.COM }
1699*10922SJeff.Bonwick@Sun.COM 
1700*10922SJeff.Bonwick@Sun.COM void
1701*10922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
1702*10922SJeff.Bonwick@Sun.COM {
1703*10922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
1704*10922SJeff.Bonwick@Sun.COM 
1705*10922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
1706*10922SJeff.Bonwick@Sun.COM }
1707*10922SJeff.Bonwick@Sun.COM 
1708*10922SJeff.Bonwick@Sun.COM /*
1709*10922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
1710*10922SJeff.Bonwick@Sun.COM  */
1711*10922SJeff.Bonwick@Sun.COM static int
1712*10922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1713*10922SJeff.Bonwick@Sun.COM {
1714*10922SJeff.Bonwick@Sun.COM 	int missing = 0;
1715*10922SJeff.Bonwick@Sun.COM 	int error;
1716*10922SJeff.Bonwick@Sun.COM 
1717*10922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1718*10922SJeff.Bonwick@Sun.COM 
1719*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
1720*10922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
1721*10922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1722*10922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
1723*10922SJeff.Bonwick@Sun.COM 		if (error) {
1724*10922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
1725*10922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
1726*10922SJeff.Bonwick@Sun.COM 			missing++;
1727*10922SJeff.Bonwick@Sun.COM 		} else {
1728*10922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
1729*10922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
1730*10922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
1731*10922SJeff.Bonwick@Sun.COM 
1732*10922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
1733*10922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
1734*10922SJeff.Bonwick@Sun.COM 
1735*10922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
1736*10922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1737*10922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
1738*10922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
1739*10922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
1740*10922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1741*10922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
1742*10922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
1743*10922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
1744*10922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
1745*10922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
1746*10922SJeff.Bonwick@Sun.COM 		}
1747*10922SJeff.Bonwick@Sun.COM 	}
1748*10922SJeff.Bonwick@Sun.COM 
1749*10922SJeff.Bonwick@Sun.COM 	return (missing);
1750*10922SJeff.Bonwick@Sun.COM }
1751*10922SJeff.Bonwick@Sun.COM 
1752*10922SJeff.Bonwick@Sun.COM static int
1753*10922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1754*10922SJeff.Bonwick@Sun.COM {
1755*10922SJeff.Bonwick@Sun.COM 	int missing = 0;
1756*10922SJeff.Bonwick@Sun.COM 
1757*10922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1758*10922SJeff.Bonwick@Sun.COM 
1759*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
1760*10922SJeff.Bonwick@Sun.COM 		if (missing) {
1761*10922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
1762*10922SJeff.Bonwick@Sun.COM 			missing++;
1763*10922SJeff.Bonwick@Sun.COM 			continue;
1764*10922SJeff.Bonwick@Sun.COM 		}
1765*10922SJeff.Bonwick@Sun.COM 
1766*10922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1767*10922SJeff.Bonwick@Sun.COM 
1768*10922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
1769*10922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
1770*10922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
1771*10922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
1772*10922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
1773*10922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1774*10922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
1775*10922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
1776*10922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
1777*10922SJeff.Bonwick@Sun.COM 
1778*10922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1779*10922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
1780*10922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
1781*10922SJeff.Bonwick@Sun.COM 			missing++;
1782*10922SJeff.Bonwick@Sun.COM 		} else {
1783*10922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
1784*10922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
1785*10922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
1786*10922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
1787*10922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
1788*10922SJeff.Bonwick@Sun.COM 		}
1789*10922SJeff.Bonwick@Sun.COM 
1790*10922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
1791*10922SJeff.Bonwick@Sun.COM 	}
1792*10922SJeff.Bonwick@Sun.COM 
1793*10922SJeff.Bonwick@Sun.COM 	return (missing);
1794*10922SJeff.Bonwick@Sun.COM }
1795*10922SJeff.Bonwick@Sun.COM 
1796*10922SJeff.Bonwick@Sun.COM static int
1797*10922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1798*10922SJeff.Bonwick@Sun.COM {
1799*10922SJeff.Bonwick@Sun.COM 	int missing = 0;
1800*10922SJeff.Bonwick@Sun.COM 	int error;
1801*10922SJeff.Bonwick@Sun.COM 
1802*10922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1803*10922SJeff.Bonwick@Sun.COM 
1804*10922SJeff.Bonwick@Sun.COM 	od += count - 1;
1805*10922SJeff.Bonwick@Sun.COM 
1806*10922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
1807*10922SJeff.Bonwick@Sun.COM 		if (missing) {
1808*10922SJeff.Bonwick@Sun.COM 			missing++;
1809*10922SJeff.Bonwick@Sun.COM 			continue;
1810*10922SJeff.Bonwick@Sun.COM 		}
1811*10922SJeff.Bonwick@Sun.COM 
1812*10922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
1813*10922SJeff.Bonwick@Sun.COM 			continue;
1814*10922SJeff.Bonwick@Sun.COM 
1815*10922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1816*10922SJeff.Bonwick@Sun.COM 
1817*10922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
1818*10922SJeff.Bonwick@Sun.COM 
1819*10922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1820*10922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
1821*10922SJeff.Bonwick@Sun.COM 			missing++;
1822*10922SJeff.Bonwick@Sun.COM 		} else {
1823*10922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
1824*10922SJeff.Bonwick@Sun.COM 		}
1825*10922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
1826*10922SJeff.Bonwick@Sun.COM 	}
1827*10922SJeff.Bonwick@Sun.COM 
1828*10922SJeff.Bonwick@Sun.COM 	return (missing);
1829*10922SJeff.Bonwick@Sun.COM }
1830*10922SJeff.Bonwick@Sun.COM 
1831*10922SJeff.Bonwick@Sun.COM static int
1832*10922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1833*10922SJeff.Bonwick@Sun.COM     void *data)
1834*10922SJeff.Bonwick@Sun.COM {
1835*10922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
1836*10922SJeff.Bonwick@Sun.COM 	int error;
1837*10922SJeff.Bonwick@Sun.COM 
1838*10922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1839*10922SJeff.Bonwick@Sun.COM 
1840*10922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
1841*10922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
1842*10922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
1843*10922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
1844*10922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
1845*10922SJeff.Bonwick@Sun.COM 
1846*10922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
1847*10922SJeff.Bonwick@Sun.COM 
1848*10922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
1849*10922SJeff.Bonwick@Sun.COM 
1850*10922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1851*10922SJeff.Bonwick@Sun.COM 
1852*10922SJeff.Bonwick@Sun.COM 	return (error);
1853*10922SJeff.Bonwick@Sun.COM }
1854*10922SJeff.Bonwick@Sun.COM 
1855*10922SJeff.Bonwick@Sun.COM static int
1856*10922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1857*10922SJeff.Bonwick@Sun.COM {
1858*10922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
1859*10922SJeff.Bonwick@Sun.COM 	int error;
1860*10922SJeff.Bonwick@Sun.COM 
1861*10922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
1862*10922SJeff.Bonwick@Sun.COM 
1863*10922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
1864*10922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
1865*10922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
1866*10922SJeff.Bonwick@Sun.COM 
1867*10922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
1868*10922SJeff.Bonwick@Sun.COM 
1869*10922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
1870*10922SJeff.Bonwick@Sun.COM 
1871*10922SJeff.Bonwick@Sun.COM 	return (error);
1872*10922SJeff.Bonwick@Sun.COM }
1873*10922SJeff.Bonwick@Sun.COM 
1874*10922SJeff.Bonwick@Sun.COM static int
1875*10922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
1876*10922SJeff.Bonwick@Sun.COM {
1877*10922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
1878*10922SJeff.Bonwick@Sun.COM 	int error;
1879*10922SJeff.Bonwick@Sun.COM 
1880*10922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
1881*10922SJeff.Bonwick@Sun.COM 
1882*10922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
1883*10922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
1884*10922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
1885*10922SJeff.Bonwick@Sun.COM 
1886*10922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
1887*10922SJeff.Bonwick@Sun.COM 
1888*10922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
1889*10922SJeff.Bonwick@Sun.COM 
1890*10922SJeff.Bonwick@Sun.COM 	return (error);
1891*10922SJeff.Bonwick@Sun.COM }
1892*10922SJeff.Bonwick@Sun.COM 
1893*10922SJeff.Bonwick@Sun.COM static void
1894*10922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1895*10922SJeff.Bonwick@Sun.COM {
1896*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
1897*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
1898*10922SJeff.Bonwick@Sun.COM 	uint64_t txg;
1899*10922SJeff.Bonwick@Sun.COM 	rl_t *rl;
1900*10922SJeff.Bonwick@Sun.COM 
1901*10922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
1902*10922SJeff.Bonwick@Sun.COM 
1903*10922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
1904*10922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1905*10922SJeff.Bonwick@Sun.COM 
1906*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
1907*10922SJeff.Bonwick@Sun.COM 
1908*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
1909*10922SJeff.Bonwick@Sun.COM 
1910*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1911*10922SJeff.Bonwick@Sun.COM 
1912*10922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
1913*10922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
1914*10922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1915*10922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
1916*10922SJeff.Bonwick@Sun.COM 	} else {
1917*10922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
1918*10922SJeff.Bonwick@Sun.COM 	}
1919*10922SJeff.Bonwick@Sun.COM 
1920*10922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
1921*10922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
1922*10922SJeff.Bonwick@Sun.COM }
1923*10922SJeff.Bonwick@Sun.COM 
1924*10922SJeff.Bonwick@Sun.COM static void
1925*10922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1926*10922SJeff.Bonwick@Sun.COM {
1927*10922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
1928*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
1929*10922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
1930*10922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
1931*10922SJeff.Bonwick@Sun.COM 	void *data;
1932*10922SJeff.Bonwick@Sun.COM 
1933*10922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1934*10922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
1935*10922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
1936*10922SJeff.Bonwick@Sun.COM 
1937*10922SJeff.Bonwick@Sun.COM 	/*
1938*10922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
1939*10922SJeff.Bonwick@Sun.COM 	 */
1940*10922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
1941*10922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
1942*10922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
1943*10922SJeff.Bonwick@Sun.COM 
1944*10922SJeff.Bonwick@Sun.COM 	switch (io_type) {
1945*10922SJeff.Bonwick@Sun.COM 
1946*10922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
1947*10922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1948*10922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1949*10922SJeff.Bonwick@Sun.COM 		break;
1950*10922SJeff.Bonwick@Sun.COM 
1951*10922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
1952*10922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
1953*10922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
1954*10922SJeff.Bonwick@Sun.COM 			/*
1955*10922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
1956*10922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
1957*10922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
1958*10922SJeff.Bonwick@Sun.COM 			 */
1959*10922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
1960*10922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
1961*10922SJeff.Bonwick@Sun.COM 		}
1962*10922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
1963*10922SJeff.Bonwick@Sun.COM 		break;
1964*10922SJeff.Bonwick@Sun.COM 
1965*10922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
1966*10922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
1967*10922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
1968*10922SJeff.Bonwick@Sun.COM 		break;
1969*10922SJeff.Bonwick@Sun.COM 
1970*10922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
1971*10922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
1972*10922SJeff.Bonwick@Sun.COM 		break;
1973*10922SJeff.Bonwick@Sun.COM 
1974*10922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
1975*10922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
1976*10922SJeff.Bonwick@Sun.COM 		break;
1977*10922SJeff.Bonwick@Sun.COM 	}
1978*10922SJeff.Bonwick@Sun.COM 
1979*10922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
1980*10922SJeff.Bonwick@Sun.COM }
1981*10922SJeff.Bonwick@Sun.COM 
1982*10922SJeff.Bonwick@Sun.COM /*
1983*10922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
1984*10922SJeff.Bonwick@Sun.COM  */
1985*10922SJeff.Bonwick@Sun.COM static void
1986*10922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
1987*10922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
1988*10922SJeff.Bonwick@Sun.COM {
1989*10922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
1990*10922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
1991*10922SJeff.Bonwick@Sun.COM 
1992*10922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
1993*10922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
1994*10922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
1995*10922SJeff.Bonwick@Sun.COM 
1996*10922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
1997*10922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
1998*10922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
1999*10922SJeff.Bonwick@Sun.COM 
2000*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2001*10922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
2002*10922SJeff.Bonwick@Sun.COM }
2003*10922SJeff.Bonwick@Sun.COM 
2004*10922SJeff.Bonwick@Sun.COM /*
2005*10922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
2006*10922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
2007*10922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
2008*10922SJeff.Bonwick@Sun.COM  * use the existing objects.
2009*10922SJeff.Bonwick@Sun.COM  */
2010*10922SJeff.Bonwick@Sun.COM static int
2011*10922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2012*10922SJeff.Bonwick@Sun.COM {
2013*10922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
2014*10922SJeff.Bonwick@Sun.COM 	int rv = 0;
2015*10922SJeff.Bonwick@Sun.COM 
2016*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2017*10922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2018*10922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
2019*10922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
2020*10922SJeff.Bonwick@Sun.COM 		rv = -1;
2021*10922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
2022*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2023*10922SJeff.Bonwick@Sun.COM 
2024*10922SJeff.Bonwick@Sun.COM 	return (rv);
2025*10922SJeff.Bonwick@Sun.COM }
2026*10922SJeff.Bonwick@Sun.COM 
2027*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2028*10922SJeff.Bonwick@Sun.COM void
2029*10922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2030*10922SJeff.Bonwick@Sun.COM {
2031*10922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
2032*10922SJeff.Bonwick@Sun.COM 
2033*10922SJeff.Bonwick@Sun.COM 	zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS));
2034*10922SJeff.Bonwick@Sun.COM 
2035*10922SJeff.Bonwick@Sun.COM 	/*
2036*10922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
2037*10922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
2038*10922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
2039*10922SJeff.Bonwick@Sun.COM 	 */
2040*10922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
2041*10922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2042*10922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
2043*10922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
2044*10922SJeff.Bonwick@Sun.COM }
2045*10922SJeff.Bonwick@Sun.COM 
2046*10922SJeff.Bonwick@Sun.COM /*
2047789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2048789Sahrens  * or create a pool with a bad vdev spec.
2049789Sahrens  */
2050*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2051789Sahrens void
2052*10922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2053789Sahrens {
2054*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2055789Sahrens 	spa_t *spa;
2056789Sahrens 	nvlist_t *nvroot;
2057789Sahrens 
2058789Sahrens 	/*
2059789Sahrens 	 * Attempt to create using a bad file.
2060789Sahrens 	 */
20617754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2062*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
2063*10922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2064789Sahrens 	nvlist_free(nvroot);
2065789Sahrens 
2066789Sahrens 	/*
2067789Sahrens 	 * Attempt to create using a bad mirror.
2068789Sahrens 	 */
20697754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2070*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
2071*10922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2072789Sahrens 	nvlist_free(nvroot);
2073789Sahrens 
2074789Sahrens 	/*
2075789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2076789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2077789Sahrens 	 */
2078*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20797754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2080*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2081789Sahrens 	nvlist_free(nvroot);
2082*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2083*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2084789Sahrens 	spa_close(spa, FTAG);
2085*10922SJeff.Bonwick@Sun.COM 
2086*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2087789Sahrens }
2088789Sahrens 
20897768SJeff.Bonwick@Sun.COM static vdev_t *
20907768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
20917768SJeff.Bonwick@Sun.COM {
20927768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
20937768SJeff.Bonwick@Sun.COM 
20947768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
20957768SJeff.Bonwick@Sun.COM 		return (vd);
20967768SJeff.Bonwick@Sun.COM 
20977768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
20987768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
20997768SJeff.Bonwick@Sun.COM 		    NULL)
21007768SJeff.Bonwick@Sun.COM 			return (mvd);
21017768SJeff.Bonwick@Sun.COM 
21027768SJeff.Bonwick@Sun.COM 	return (NULL);
21037768SJeff.Bonwick@Sun.COM }
21047768SJeff.Bonwick@Sun.COM 
2105789Sahrens /*
210610594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
210710594SGeorge.Wilson@Sun.COM  */
210810594SGeorge.Wilson@Sun.COM int
210910594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
211010594SGeorge.Wilson@Sun.COM {
211110594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
211210594SGeorge.Wilson@Sun.COM 	int c;
211310594SGeorge.Wilson@Sun.COM 
211410594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
211510594SGeorge.Wilson@Sun.COM 
211610594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
211710594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
211810594SGeorge.Wilson@Sun.COM 
211910594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
212010594SGeorge.Wilson@Sun.COM 			break;
212110594SGeorge.Wilson@Sun.COM 	}
212210594SGeorge.Wilson@Sun.COM 	return (c);
212310594SGeorge.Wilson@Sun.COM }
212410594SGeorge.Wilson@Sun.COM 
212510594SGeorge.Wilson@Sun.COM /*
2126789Sahrens  * Verify that vdev_add() works as expected.
2127789Sahrens  */
2128*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2129789Sahrens void
2130*10922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2131789Sahrens {
2132*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2133*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
2134789Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
213510594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2136789Sahrens 	nvlist_t *nvroot;
2137789Sahrens 	int error;
2138789Sahrens 
2139*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2140789Sahrens 
21417754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2142789Sahrens 
214310594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2144789Sahrens 
21454527Sperrin 	/*
214610594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21474527Sperrin 	 */
214810594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
214910594SGeorge.Wilson@Sun.COM 		/*
215010594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
215110594SGeorge.Wilson@Sun.COM 		 */
2152*10922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
215310594SGeorge.Wilson@Sun.COM 
215410594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
215510594SGeorge.Wilson@Sun.COM 
215610594SGeorge.Wilson@Sun.COM 		/*
215710594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
215810594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
215910594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
216010594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
216110594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
216210594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
216310594SGeorge.Wilson@Sun.COM 		 */
2164*10922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
216510594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
2166*10922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
216710594SGeorge.Wilson@Sun.COM 
216810594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
216910594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
217010594SGeorge.Wilson@Sun.COM 	} else {
217110594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
217210594SGeorge.Wilson@Sun.COM 
217310594SGeorge.Wilson@Sun.COM 		/*
217410594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
217510594SGeorge.Wilson@Sun.COM 		 */
217610594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
217710594SGeorge.Wilson@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
217810594SGeorge.Wilson@Sun.COM 
217910594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
218010594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
218110594SGeorge.Wilson@Sun.COM 
218210594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
218310594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
218410594SGeorge.Wilson@Sun.COM 		else if (error != 0)
218510594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
218610594SGeorge.Wilson@Sun.COM 	}
2187789Sahrens 
2188*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
21897754SJeff.Bonwick@Sun.COM }
21907754SJeff.Bonwick@Sun.COM 
21917754SJeff.Bonwick@Sun.COM /*
21927754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
21937754SJeff.Bonwick@Sun.COM  */
2194*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
21957754SJeff.Bonwick@Sun.COM void
2196*10922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
21977754SJeff.Bonwick@Sun.COM {
2198*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2199*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22007768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22017754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22027754SJeff.Bonwick@Sun.COM 	char *aux;
22037754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22047754SJeff.Bonwick@Sun.COM 	int error;
22057754SJeff.Bonwick@Sun.COM 
22067768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22077754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22087754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22097754SJeff.Bonwick@Sun.COM 	} else {
22107754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22117754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22127754SJeff.Bonwick@Sun.COM 	}
22137754SJeff.Bonwick@Sun.COM 
2214*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22157754SJeff.Bonwick@Sun.COM 
22167754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22177754SJeff.Bonwick@Sun.COM 
22187754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22197754SJeff.Bonwick@Sun.COM 		/*
22207754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22217754SJeff.Bonwick@Sun.COM 		 */
22227754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22237754SJeff.Bonwick@Sun.COM 	} else {
22247754SJeff.Bonwick@Sun.COM 		/*
22257754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22267754SJeff.Bonwick@Sun.COM 		 */
2227*10922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22287754SJeff.Bonwick@Sun.COM 		for (;;) {
22297754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22307754SJeff.Bonwick@Sun.COM 			int c;
22317754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
2232*10922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22337754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22347754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22357754SJeff.Bonwick@Sun.COM 				    path) == 0)
22367754SJeff.Bonwick@Sun.COM 					break;
22377768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22387768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22397754SJeff.Bonwick@Sun.COM 				break;
2240*10922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22417754SJeff.Bonwick@Sun.COM 		}
22427754SJeff.Bonwick@Sun.COM 	}
22437754SJeff.Bonwick@Sun.COM 
22447754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22457754SJeff.Bonwick@Sun.COM 
22467754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22477754SJeff.Bonwick@Sun.COM 		/*
22487754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22497754SJeff.Bonwick@Sun.COM 		 */
22507768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22517768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22527754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22537754SJeff.Bonwick@Sun.COM 		if (error != 0)
22547754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22557754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22567754SJeff.Bonwick@Sun.COM 	} else {
22577754SJeff.Bonwick@Sun.COM 		/*
22587754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22597754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22607754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22617754SJeff.Bonwick@Sun.COM 		 */
22627754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22639816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22647754SJeff.Bonwick@Sun.COM 
22657754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22667754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22677754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22687754SJeff.Bonwick@Sun.COM 	}
22697754SJeff.Bonwick@Sun.COM 
2270*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2271789Sahrens }
2272789Sahrens 
2273789Sahrens /*
2274789Sahrens  * Verify that we can attach and detach devices.
2275789Sahrens  */
2276*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2277789Sahrens void
2278*10922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2279789Sahrens {
2280*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2281*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22827768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2283789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
22841544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
22857754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
2286789Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2287789Sahrens 	uint64_t leaf, top;
22881732Sbonwick 	uint64_t ashift = ztest_get_ashift();
22898241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
22901544Seschrock 	size_t oldsize, newsize;
22911544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2292789Sahrens 	int replacing;
22937793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
22947768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
22957768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2296789Sahrens 	int error, expected_error;
2297789Sahrens 
2298*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2299789Sahrens 
23007754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2301789Sahrens 
2302789Sahrens 	/*
2303789Sahrens 	 * Decide whether to do an attach or a replace.
2304789Sahrens 	 */
2305789Sahrens 	replacing = ztest_random(2);
2306789Sahrens 
2307789Sahrens 	/*
2308789Sahrens 	 * Pick a random top-level vdev.
2309789Sahrens 	 */
2310*10922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2311789Sahrens 
2312789Sahrens 	/*
2313789Sahrens 	 * Pick a random leaf within it.
2314789Sahrens 	 */
2315789Sahrens 	leaf = ztest_random(leaves);
2316789Sahrens 
2317789Sahrens 	/*
23187768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2319789Sahrens 	 */
23207768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
23218241SJeff.Bonwick@Sun.COM 	if (zopt_mirrors >= 1) {
23228241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
23238241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= zopt_mirrors);
23247768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
23258241SJeff.Bonwick@Sun.COM 	}
23268241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
23278241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
23288241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
23297768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
23308241SJeff.Bonwick@Sun.COM 	}
2331789Sahrens 
2332789Sahrens 	/*
23337768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
23347768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2335789Sahrens 	 */
23367768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
23377793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
23388241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
23398241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
23407768SJeff.Bonwick@Sun.COM 	}
23417768SJeff.Bonwick@Sun.COM 
23427768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
23439816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
23447768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
23457768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
23461544Seschrock 	pvd = oldvd->vdev_parent;
23478241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2348789Sahrens 
2349789Sahrens 	/*
23507793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
23517793SJeff.Bonwick@Sun.COM 	 */
23527793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
23537793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
23548241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
23558241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
23568241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
23578241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
2358*10922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
23597793SJeff.Bonwick@Sun.COM 		return;
23607793SJeff.Bonwick@Sun.COM 	}
23617793SJeff.Bonwick@Sun.COM 
23627793SJeff.Bonwick@Sun.COM 	/*
23637768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
23647768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2365789Sahrens 	 */
23667768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
23677768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
23687768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
23697768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
23707768SJeff.Bonwick@Sun.COM 	} else {
23717768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
23727768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
23737768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
23747768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
23757768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
23767768SJeff.Bonwick@Sun.COM 	}
23777768SJeff.Bonwick@Sun.COM 
23787768SJeff.Bonwick@Sun.COM 	if (newvd) {
23799816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
23807768SJeff.Bonwick@Sun.COM 	} else {
23817768SJeff.Bonwick@Sun.COM 		/*
23827768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
23837768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
23847768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
23857768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
23867768SJeff.Bonwick@Sun.COM 		 */
23877768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
23887768SJeff.Bonwick@Sun.COM 	}
2389789Sahrens 
2390789Sahrens 	/*
2391789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2392789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2393789Sahrens 	 *
23941544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2395789Sahrens 	 *
23961544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2397789Sahrens 	 */
23987768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
23997768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
24007768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
24017768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
24027768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
24037768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
24047768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
24057768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
24067768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
24077768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
24082174Seschrock 		expected_error = EBUSY;
24091544Seschrock 	else if (newsize < oldsize)
2410789Sahrens 		expected_error = EOVERFLOW;
24111732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
24121732Sbonwick 		expected_error = EDOM;
2413789Sahrens 	else
2414789Sahrens 		expected_error = 0;
2415789Sahrens 
24167754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2417789Sahrens 
2418789Sahrens 	/*
24191544Seschrock 	 * Build the nvlist describing newpath.
2420789Sahrens 	 */
24217754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
24227754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2423789Sahrens 
24247768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2425789Sahrens 
2426789Sahrens 	nvlist_free(root);
2427789Sahrens 
2428789Sahrens 	/*
2429789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2430789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2431789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2432789Sahrens 	 */
2433789Sahrens 	if (expected_error == ENOTSUP &&
2434789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2435789Sahrens 		expected_error = error;
2436789Sahrens 
2437797Sbonwick 	/*
2438797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2439797Sbonwick 	 */
24407046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2441797Sbonwick 		expected_error = error;
2442797Sbonwick 
24437046Sahrens 	/* XXX workaround 6690467 */
24447046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
24457046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
24467046Sahrens 		    "returned %d, expected %d",
24477046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
24487046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2449789Sahrens 	}
2450789Sahrens 
2451*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2452789Sahrens }
2453789Sahrens 
2454789Sahrens /*
24559816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
24569816SGeorge.Wilson@Sun.COM  */
24579816SGeorge.Wilson@Sun.COM vdev_t *
24589816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
24599816SGeorge.Wilson@Sun.COM {
24609816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
24619816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
24629816SGeorge.Wilson@Sun.COM 	size_t fsize;
24639816SGeorge.Wilson@Sun.COM 	int fd;
24649816SGeorge.Wilson@Sun.COM 
24659816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
24669816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
24679816SGeorge.Wilson@Sun.COM 
24689816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
24699816SGeorge.Wilson@Sun.COM 		return (vd);
24709816SGeorge.Wilson@Sun.COM 
24719816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
24729816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
24739816SGeorge.Wilson@Sun.COM 
24749816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
24759816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
24769816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
24779816SGeorge.Wilson@Sun.COM 	}
24789816SGeorge.Wilson@Sun.COM 	(void) close(fd);
24799816SGeorge.Wilson@Sun.COM 	return (NULL);
24809816SGeorge.Wilson@Sun.COM }
24819816SGeorge.Wilson@Sun.COM 
24829816SGeorge.Wilson@Sun.COM /*
24839816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
24849816SGeorge.Wilson@Sun.COM  */
24859816SGeorge.Wilson@Sun.COM /* ARGSUSED */
24869816SGeorge.Wilson@Sun.COM vdev_t *
24879816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
24889816SGeorge.Wilson@Sun.COM {
24899816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
24909816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
24919816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
249210685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
249310850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
249410850SGeorge.Wilson@Sun.COM 	int error;
24959816SGeorge.Wilson@Sun.COM 
24969816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
24979816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
24989816SGeorge.Wilson@Sun.COM 
24999816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
25009816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
250110850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
25029816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
25039816SGeorge.Wilson@Sun.COM 
25049816SGeorge.Wilson@Sun.COM 	/*
250510850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
250610850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
250710850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
250810850SGeorge.Wilson@Sun.COM 	 */
250910850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
251010850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
251110850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
251210850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
251310850SGeorge.Wilson@Sun.COM 		}
251410850SGeorge.Wilson@Sun.COM 		return (vd);
251510850SGeorge.Wilson@Sun.COM 	}
251610850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
251710850SGeorge.Wilson@Sun.COM 
251810850SGeorge.Wilson@Sun.COM 	/*
25199816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
25209816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
25219816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
25229816SGeorge.Wilson@Sun.COM 	 * trying to online it.
25239816SGeorge.Wilson@Sun.COM 	 */
252410685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
252510685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
252610685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
252710685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
2528*10922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
2529*10922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
253010685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
253110685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
253210685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
25339816SGeorge.Wilson@Sun.COM 		}
25349816SGeorge.Wilson@Sun.COM 		return (vd);
25359816SGeorge.Wilson@Sun.COM 	}
25369816SGeorge.Wilson@Sun.COM 	return (NULL);
25379816SGeorge.Wilson@Sun.COM }
25389816SGeorge.Wilson@Sun.COM 
25399816SGeorge.Wilson@Sun.COM /*
25409816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
25419816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
25429816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
25439816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
25449816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
25459816SGeorge.Wilson@Sun.COM  */
25469816SGeorge.Wilson@Sun.COM vdev_t *
25479816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
25489816SGeorge.Wilson@Sun.COM {
25499816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
25509816SGeorge.Wilson@Sun.COM 		if (func == NULL)
25519816SGeorge.Wilson@Sun.COM 			return (vd);
25529816SGeorge.Wilson@Sun.COM 		else
25539816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
25549816SGeorge.Wilson@Sun.COM 	}
25559816SGeorge.Wilson@Sun.COM 
25569816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
25579816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
25589816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
25599816SGeorge.Wilson@Sun.COM 			return (cvd);
25609816SGeorge.Wilson@Sun.COM 	}
25619816SGeorge.Wilson@Sun.COM 	return (NULL);
25629816SGeorge.Wilson@Sun.COM }
25639816SGeorge.Wilson@Sun.COM 
25649816SGeorge.Wilson@Sun.COM /*
2565789Sahrens  * Verify that dynamic LUN growth works as expected.
2566789Sahrens  */
2567*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2568789Sahrens void
2569*10922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2570789Sahrens {
2571*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2572*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
2573*10922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
2574*10922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
2575*10922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
25769816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
2577*10922SJeff.Bonwick@Sun.COM 	uint64_t top;
2578*10922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2579*10922SJeff.Bonwick@Sun.COM 
2580*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
25819816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
25829816SGeorge.Wilson@Sun.COM 
2583*10922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2584*10922SJeff.Bonwick@Sun.COM 
2585*10922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
2586*10922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
2587*10922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
2588*10922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
2589*10922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
25909816SGeorge.Wilson@Sun.COM 
25919816SGeorge.Wilson@Sun.COM 	/*
25929816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
25939816SGeorge.Wilson@Sun.COM 	 * our top-level device.
25949816SGeorge.Wilson@Sun.COM 	 */
25959816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
25969816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
25979816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25989816SGeorge.Wilson@Sun.COM 
25999816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
26009816SGeorge.Wilson@Sun.COM 
26019816SGeorge.Wilson@Sun.COM 	/*
260210685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
260310685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
26049816SGeorge.Wilson@Sun.COM 	 */
260510685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
260610685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
26079816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
2608*10922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
26099816SGeorge.Wilson@Sun.COM 		return;
26109816SGeorge.Wilson@Sun.COM 	}
26119816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
26129816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
26139816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
26149816SGeorge.Wilson@Sun.COM 
2615*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
2616*10922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
26179816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
26189816SGeorge.Wilson@Sun.COM 	}
26199816SGeorge.Wilson@Sun.COM 
2620789Sahrens 	/*
26219816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
26229816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
26239816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
26249816SGeorge.Wilson@Sun.COM 	 */
26259816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
26269816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
26279816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
26289816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
26299816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
263010685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
26319816SGeorge.Wilson@Sun.COM 		}
2632*10922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
2633*10922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
26349816SGeorge.Wilson@Sun.COM 		return;
26359816SGeorge.Wilson@Sun.COM 	}
26369816SGeorge.Wilson@Sun.COM 
2637*10922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
26389816SGeorge.Wilson@Sun.COM 
26399816SGeorge.Wilson@Sun.COM 	/*
26409816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
26419816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
26429816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2643789Sahrens 	 */
2644*10922SJeff.Bonwick@Sun.COM 	for (;;) {
2645*10922SJeff.Bonwick@Sun.COM 		boolean_t done;
2646*10922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
2647*10922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2648*10922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
2649*10922SJeff.Bonwick@Sun.COM 		if (done)
2650*10922SJeff.Bonwick@Sun.COM 			break;
2651*10922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
2652*10922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
2653*10922SJeff.Bonwick@Sun.COM 	}
26549816SGeorge.Wilson@Sun.COM 
26559816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2656*10922SJeff.Bonwick@Sun.COM 
2657*10922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
2658*10922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
2659*10922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
2660*10922SJeff.Bonwick@Sun.COM 
2661*10922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2662*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
2663*10922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
2664*10922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
2665*10922SJeff.Bonwick@Sun.COM 		}
2666*10922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
2667*10922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2668*10922SJeff.Bonwick@Sun.COM 		return;
2669*10922SJeff.Bonwick@Sun.COM 	}
2670*10922SJeff.Bonwick@Sun.COM 
2671*10922SJeff.Bonwick@Sun.COM 	/*
2672*10922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
2673*10922SJeff.Bonwick@Sun.COM 	 */
2674*10922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
2675*10922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2676*10922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
26779816SGeorge.Wilson@Sun.COM 
26789816SGeorge.Wilson@Sun.COM 	/*
26799816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
26809816SGeorge.Wilson@Sun.COM 	 */
2681*10922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
2682*10922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2683*10922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
2684*10922SJeff.Bonwick@Sun.COM 
2685*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
26869816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
26879816SGeorge.Wilson@Sun.COM 
2688*10922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
2689*10922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
26909816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
26919816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2692789Sahrens 	}
2693*10922SJeff.Bonwick@Sun.COM 
26949816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
2695*10922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2696*10922SJeff.Bonwick@Sun.COM }
2697*10922SJeff.Bonwick@Sun.COM 
2698*10922SJeff.Bonwick@Sun.COM /*
2699*10922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2700*10922SJeff.Bonwick@Sun.COM  */
2701*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2702*10922SJeff.Bonwick@Sun.COM static void
2703*10922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2704*10922SJeff.Bonwick@Sun.COM {
2705*10922SJeff.Bonwick@Sun.COM 	/*
2706*10922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
2707*10922SJeff.Bonwick@Sun.COM 	 */
2708*10922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2709*10922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2710789Sahrens }
2711789Sahrens 
2712789Sahrens /* ARGSUSED */
2713*10922SJeff.Bonwick@Sun.COM static int
2714*10922SJeff.Bonwick@Sun.COM ztest_objset_destroy_cb(char *name, void *arg)
2715789Sahrens {
2716789Sahrens 	objset_t *os;
2717*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2718789Sahrens 	int error;
2719789Sahrens 
2720789Sahrens 	/*
2721789Sahrens 	 * Verify that the dataset contains a directory object.
2722789Sahrens 	 */
2723*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2724*10922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
27251731Sbonwick 	if (error != ENOENT) {
27261731Sbonwick 		/* We could have crashed in the middle of destroying it */
27271731Sbonwick 		ASSERT3U(error, ==, 0);
2728*10922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2729*10922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
27301731Sbonwick 	}
273110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2732789Sahrens 
2733789Sahrens 	/*
2734789Sahrens 	 * Destroy the dataset.
2735789Sahrens 	 */
2736*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
27372199Sahrens 	return (0);
2738789Sahrens }
2739789Sahrens 
2740*10922SJeff.Bonwick@Sun.COM static boolean_t
2741*10922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2742789Sahrens {
2743*10922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
2744*10922SJeff.Bonwick@Sun.COM 	int error;
2745*10922SJeff.Bonwick@Sun.COM 
2746*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2747*10922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
2748*10922SJeff.Bonwick@Sun.COM 
2749*10922SJeff.Bonwick@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2750*10922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
2751*10922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
2752*10922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
2753*10922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
2754*10922SJeff.Bonwick@Sun.COM 	}
2755*10922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
2756*10922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2757*10922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2758789Sahrens }
2759789Sahrens 
2760*10922SJeff.Bonwick@Sun.COM static boolean_t
2761*10922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
2762*10922SJeff.Bonwick@Sun.COM {
2763*10922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
2764*10922SJeff.Bonwick@Sun.COM 	int error;
2765*10922SJeff.Bonwick@Sun.COM 
2766*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2767*10922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
2768*10922SJeff.Bonwick@Sun.COM 
2769*10922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
2770*10922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
2771*10922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2772*10922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2773*10922SJeff.Bonwick@Sun.COM }
2774*10922SJeff.Bonwick@Sun.COM 
2775*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2776789Sahrens void
2777*10922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2778789Sahrens {
2779*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2780*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
2781*10922SJeff.Bonwick@Sun.COM 	int iters;
2782789Sahrens 	int error;
27836689Smaybee 	objset_t *os, *os2;
2784*10922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2785789Sahrens 	zilog_t *zilog;
2786*10922SJeff.Bonwick@Sun.COM 
2787*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
2788*10922SJeff.Bonwick@Sun.COM 
2789*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2790*10922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2791789Sahrens 
2792789Sahrens 	/*
2793789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2794789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
2795*10922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2796789Sahrens 	 */
2797789Sahrens 	if (ztest_random(2) == 0 &&
279810298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2799*10922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
2800*10922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
2801*10922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
280210298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2803789Sahrens 	}
2804789Sahrens 
2805789Sahrens 	/*
2806789Sahrens 	 * There may be an old instance of the dataset we're about to
2807789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2808789Sahrens 	 * and all of its snapshots.
2809789Sahrens 	 */
2810*10922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
28112417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2812789Sahrens 
2813789Sahrens 	/*
2814789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2815789Sahrens 	 */
2816*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2817789Sahrens 
2818789Sahrens 	/*
2819789Sahrens 	 * Verify that we can create a new dataset.
2820789Sahrens 	 */
282110272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
2822*10922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
2823789Sahrens 	if (error) {
2824789Sahrens 		if (error == ENOSPC) {
2825*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
2826*10922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2827789Sahrens 			return;
2828789Sahrens 		}
2829789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2830789Sahrens 	}
2831789Sahrens 
2832*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
2833*10922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
2834*10922SJeff.Bonwick@Sun.COM 
2835*10922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2836789Sahrens 
2837789Sahrens 	/*
2838789Sahrens 	 * Open the intent log for it.
2839789Sahrens 	 */
2840*10922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2841789Sahrens 
2842789Sahrens 	/*
2843*10922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
2844*10922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2845789Sahrens 	 */
2846*10922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
2847*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
2848*10922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
2849*10922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
2850*10922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2851789Sahrens 	}
2852789Sahrens 
2853789Sahrens 	/*
2854789Sahrens 	 * Verify that we cannot create an existing dataset.
2855789Sahrens 	 */
2856*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
2857*10922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2858789Sahrens 
2859789Sahrens 	/*
286010298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2861789Sahrens 	 */
2862*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
286310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
286410298SMatthew.Ahrens@Sun.COM 
286510298SMatthew.Ahrens@Sun.COM 	/*
2866*10922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
286710298SMatthew.Ahrens@Sun.COM 	 */
2868*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
2869*10922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
2870789Sahrens 
2871789Sahrens 	zil_close(zilog);
287210298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
2873*10922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
2874*10922SJeff.Bonwick@Sun.COM 
2875*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2876789Sahrens }
2877789Sahrens 
2878789Sahrens /*
2879789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
2880789Sahrens  */
2881789Sahrens void
2882*10922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
2883789Sahrens {
2884*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2885*10922SJeff.Bonwick@Sun.COM 
2886*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
2887*10922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
2888*10922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
2889*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2890789Sahrens }
2891789Sahrens 
2892789Sahrens /*
28939691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
28949691SGeorge.Wilson@Sun.COM  */
28959691SGeorge.Wilson@Sun.COM void
2896*10922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
28979691SGeorge.Wilson@Sun.COM {
2898*10922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
2899*10922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
2900*10922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
2901*10922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
2902*10922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
29039691SGeorge.Wilson@Sun.COM 	int error;
29049691SGeorge.Wilson@Sun.COM 
2905*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
2906*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
2907*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
2908*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
2909*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
29109691SGeorge.Wilson@Sun.COM 
291110242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
29129691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
29139691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
291410242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
29159691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
29169691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
291710242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
29189691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
29199691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
292010242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
29219691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
29229691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
292310242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
29249691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
29259691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
29269691SGeorge.Wilson@Sun.COM }
29279691SGeorge.Wilson@Sun.COM 
29289691SGeorge.Wilson@Sun.COM /*
29298779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
29308779SMark.Musante@Sun.COM  */
29318779SMark.Musante@Sun.COM void
2932*10922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
29338779SMark.Musante@Sun.COM {
2934*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
29358779SMark.Musante@Sun.COM 	objset_t *clone;
29368779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
2937*10922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
2938*10922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
2939*10922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
2940*10922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
2941*10922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
2942*10922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
2943*10922SJeff.Bonwick@Sun.COM 	int error;
2944*10922SJeff.Bonwick@Sun.COM 
2945*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
2946*10922SJeff.Bonwick@Sun.COM 
2947*10922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
2948*10922SJeff.Bonwick@Sun.COM 
2949*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
2950*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
2951*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
2952*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
2953*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
29548837SMark.Musante@Sun.COM 
29559355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
2956*10922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
29579229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
29589229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
2959*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
29609229SGeorge.Wilson@Sun.COM 			goto out;
29619229SGeorge.Wilson@Sun.COM 		}
29629229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
29639229SGeorge.Wilson@Sun.COM 	}
29648779SMark.Musante@Sun.COM 
296510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
29668779SMark.Musante@Sun.COM 	if (error)
29678779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
29688779SMark.Musante@Sun.COM 
296910272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
297010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
29719229SGeorge.Wilson@Sun.COM 	if (error) {
29729229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
2973*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
29749229SGeorge.Wilson@Sun.COM 			goto out;
29759229SGeorge.Wilson@Sun.COM 		}
29768779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
29779229SGeorge.Wilson@Sun.COM 	}
29788779SMark.Musante@Sun.COM 
29798779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
2980*10922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
29819229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
29829229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
2983*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
29849229SGeorge.Wilson@Sun.COM 			goto out;
29859229SGeorge.Wilson@Sun.COM 		}
29869229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
29879229SGeorge.Wilson@Sun.COM 	}
29888779SMark.Musante@Sun.COM 
29898779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
2990*10922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
29919229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
29929229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
2993*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
29949229SGeorge.Wilson@Sun.COM 			goto out;
29959229SGeorge.Wilson@Sun.COM 		}
29969229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
29979229SGeorge.Wilson@Sun.COM 	}
29988779SMark.Musante@Sun.COM 
299910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
30008779SMark.Musante@Sun.COM 	if (error)
30018779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
30028779SMark.Musante@Sun.COM 
300310272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
300410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
30059229SGeorge.Wilson@Sun.COM 	if (error) {
30069229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
3007*10922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30089229SGeorge.Wilson@Sun.COM 			goto out;
30099229SGeorge.Wilson@Sun.COM 		}
30108779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
30119229SGeorge.Wilson@Sun.COM 	}
30128779SMark.Musante@Sun.COM 
301310298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
30148779SMark.Musante@Sun.COM 	if (error)
30158779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
301610588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
30178779SMark.Musante@Sun.COM 	if (error != EBUSY)
30188779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
30198779SMark.Musante@Sun.COM 		    error);
30208779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
30218779SMark.Musante@Sun.COM 
30229229SGeorge.Wilson@Sun.COM out:
3023*10922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
3024*10922SJeff.Bonwick@Sun.COM 
3025*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
30268779SMark.Musante@Sun.COM }
30278779SMark.Musante@Sun.COM 
30288779SMark.Musante@Sun.COM /*
3029789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3030789Sahrens  */
3031789Sahrens void
3032*10922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3033789Sahrens {
3034*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
3035*10922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
3036*10922SJeff.Bonwick@Sun.COM 
3037*10922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
3038*10922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3039789Sahrens 
3040789Sahrens 	/*
3041*10922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
3042*10922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3043789Sahrens 	 */
3044*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3045*10922SJeff.Bonwick@Sun.COM 		return;
3046*10922SJeff.Bonwick@Sun.COM 
3047*10922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
3048*10922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
3049*10922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3050789Sahrens }
3051789Sahrens 
3052789Sahrens /*
3053789Sahrens  * Verify that dmu_{read,write} work as expected.
3054789Sahrens  */
3055789Sahrens void
3056*10922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3057789Sahrens {
3058*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3059*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3060789Sahrens 	dmu_tx_t *tx;
3061789Sahrens 	int i, freeit, error;
3062789Sahrens 	uint64_t n, s, txg;
3063789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3064*10922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3065*10922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3066789Sahrens 	uint64_t regions = 997;
3067789Sahrens 	uint64_t stride = 123456789ULL;
3068789Sahrens 	uint64_t width = 40;
3069789Sahrens 	int free_percent = 5;
3070789Sahrens 
3071789Sahrens 	/*
3072789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3073789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3074789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3075789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3076789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3077789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3078789Sahrens 	 *
3079789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3080789Sahrens 	 *	bigobj, at the head of the nth chunk
3081789Sahrens 	 *	bigobj, at the tail of the nth chunk
3082789Sahrens 	 *
3083789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3084789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3085789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3086789Sahrens 	 *
3087789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3088789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3089789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3090789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3091789Sahrens 	 * tracking which parts of an object are allocated and free,
3092789Sahrens 	 * and that the contents of the allocated blocks are correct.
3093789Sahrens 	 */
3094789Sahrens 
3095789Sahrens 	/*
3096789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3097789Sahrens 	 */
3098*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3099*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3100*10922SJeff.Bonwick@Sun.COM 
3101*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3102*10922SJeff.Bonwick@Sun.COM 		return;
3103*10922SJeff.Bonwick@Sun.COM 
3104*10922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
3105*10922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
3106*10922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
3107*10922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3108789Sahrens 
3109789Sahrens 	/*
3110789Sahrens 	 * Prefetch a random chunk of the big object.
3111789Sahrens 	 * Our aim here is to get some async reads in flight
3112789Sahrens 	 * for blocks that we may free below; the DMU should
3113789Sahrens 	 * handle this race correctly.
3114789Sahrens 	 */
3115789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3116789Sahrens 	s = 1 + ztest_random(2 * width - 1);
3117*10922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3118789Sahrens 
3119789Sahrens 	/*
3120789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3121789Sahrens 	 */
3122789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3123789Sahrens 	s = 1 + ztest_random(width - 1);
3124789Sahrens 
3125789Sahrens 	packoff = n * sizeof (bufwad_t);
3126789Sahrens 	packsize = s * sizeof (bufwad_t);
3127789Sahrens 
3128*10922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
3129*10922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3130789Sahrens 
3131789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3132789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3133789Sahrens 
3134789Sahrens 	/*
3135789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3136789Sahrens 	 * overwriting it.
3137789Sahrens 	 */
3138789Sahrens 	freeit = (ztest_random(100) < free_percent);
3139789Sahrens 
3140789Sahrens 	/*
3141789Sahrens 	 * Read the current contents of our objects.
3142789Sahrens 	 */
3143*10922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
31449512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
31451544Seschrock 	ASSERT3U(error, ==, 0);
3146*10922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
31479512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
31481544Seschrock 	ASSERT3U(error, ==, 0);
3149789Sahrens 
3150789Sahrens 	/*
3151789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3152789Sahrens 	 */
3153789Sahrens 	tx = dmu_tx_create(os);
3154789Sahrens 
3155*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3156789Sahrens 
3157789Sahrens 	if (freeit)
3158*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3159789Sahrens 	else
3160*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3161*10922SJeff.Bonwick@Sun.COM 
3162*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3163*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3164789Sahrens 		umem_free(packbuf, packsize);
3165789Sahrens 		umem_free(bigbuf, bigsize);
3166789Sahrens 		return;
3167789Sahrens 	}
3168789Sahrens 
3169*10922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
3170*10922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3171*10922SJeff.Bonwick@Sun.COM 
3172*10922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
3173*10922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3174789Sahrens 
3175789Sahrens 	/*
3176789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3177789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3178789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3179789Sahrens 	 * with the new values we want to write out.
3180789Sahrens 	 */
3181789Sahrens 	for (i = 0; i < s; i++) {
3182789Sahrens 		/* LINTED */
3183789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3184789Sahrens 		/* LINTED */
3185*10922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3186789Sahrens 		/* LINTED */
3187*10922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3188789Sahrens 
3189789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3190789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3191789Sahrens 
3192789Sahrens 		if (pack->bw_txg > txg)
3193789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3194789Sahrens 			    pack->bw_txg, txg);
3195789Sahrens 
3196789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3197789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3198789Sahrens 			    pack->bw_index, n, i);
3199789Sahrens 
3200789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3201789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3202789Sahrens 
3203789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3204789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3205789Sahrens 
3206789Sahrens 		if (freeit) {
3207789Sahrens 			bzero(pack, sizeof (bufwad_t));
3208789Sahrens 		} else {
3209789Sahrens 			pack->bw_index = n + i;
3210789Sahrens 			pack->bw_txg = txg;
3211789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3212789Sahrens 		}
3213789Sahrens 		*bigH = *pack;
3214789Sahrens 		*bigT = *pack;
3215789Sahrens 	}
3216789Sahrens 
3217789Sahrens 	/*
3218789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3219789Sahrens 	 * Now write them out.
3220789Sahrens 	 */
3221*10922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3222789Sahrens 
3223789Sahrens 	if (freeit) {
3224*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3225789Sahrens 			(void) printf("freeing offset %llx size %llx"
3226789Sahrens 			    " txg %llx\n",
3227789Sahrens 			    (u_longlong_t)bigoff,
3228789Sahrens 			    (u_longlong_t)bigsize,
3229789Sahrens 			    (u_longlong_t)txg);
3230789Sahrens 		}
3231*10922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3232789Sahrens 	} else {
3233*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3234789Sahrens 			(void) printf("writing offset %llx size %llx"
3235789Sahrens 			    " txg %llx\n",
3236789Sahrens 			    (u_longlong_t)bigoff,
3237789Sahrens 			    (u_longlong_t)bigsize,
3238789Sahrens 			    (u_longlong_t)txg);
3239789Sahrens 		}
3240*10922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3241789Sahrens 	}
3242789Sahrens 
3243789Sahrens 	dmu_tx_commit(tx);
3244789Sahrens 
3245789Sahrens 	/*
3246789Sahrens 	 * Sanity check the stuff we just wrote.
3247789Sahrens 	 */
3248789Sahrens 	{
3249789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3250789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3251789Sahrens 
3252*10922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
32539512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
3254*10922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
32559512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3256789Sahrens 
3257789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3258789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3259789Sahrens 
3260789Sahrens 		umem_free(packcheck, packsize);
3261789Sahrens 		umem_free(bigcheck, bigsize);
3262789Sahrens 	}
3263789Sahrens 
3264789Sahrens 	umem_free(packbuf, packsize);
3265789Sahrens 	umem_free(bigbuf, bigsize);
3266789Sahrens }
3267789Sahrens 
3268789Sahrens void
32699412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3270*10922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
32719412SAleksandr.Guzovskiy@Sun.COM {
32729412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
32739412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
32749412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
32759412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
32769412SAleksandr.Guzovskiy@Sun.COM 
32779412SAleksandr.Guzovskiy@Sun.COM 	/*
32789412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
32799412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
32809412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
32819412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
32829412SAleksandr.Guzovskiy@Sun.COM 	 */
32839412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
32849412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
32859412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
32869412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
3287*10922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
32889412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
3289*10922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
32909412SAleksandr.Guzovskiy@Sun.COM 
32919412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
32929412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
32939412SAleksandr.Guzovskiy@Sun.COM 
32949412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
32959412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
32969412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
32979412SAleksandr.Guzovskiy@Sun.COM 
32989412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
32999412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
33009412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
33019412SAleksandr.Guzovskiy@Sun.COM 
33029412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
33039412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
33049412SAleksandr.Guzovskiy@Sun.COM 
33059412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
33069412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
33079412SAleksandr.Guzovskiy@Sun.COM 
33089412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
33099412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
33109412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
33119412SAleksandr.Guzovskiy@Sun.COM 
33129412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
33139412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
33149412SAleksandr.Guzovskiy@Sun.COM 	}
33159412SAleksandr.Guzovskiy@Sun.COM }
33169412SAleksandr.Guzovskiy@Sun.COM 
33179412SAleksandr.Guzovskiy@Sun.COM void
3318*10922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
33199412SAleksandr.Guzovskiy@Sun.COM {
3320*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3321*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
33229412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
33239412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
33249412SAleksandr.Guzovskiy@Sun.COM 	int error;
33259412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
33269412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
3327*10922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3328*10922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
3329*10922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
33309412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
33319412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
33329412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
33339412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
33349412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
3335*10922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
33369412SAleksandr.Guzovskiy@Sun.COM 
33379412SAleksandr.Guzovskiy@Sun.COM 	/*
33389412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
33399412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
33409412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
33419412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
33429412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
33439412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
33449412SAleksandr.Guzovskiy@Sun.COM 	 *
33459412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
33469412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
33479412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
33489412SAleksandr.Guzovskiy@Sun.COM 	 *
33499412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
33509412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
33519412SAleksandr.Guzovskiy@Sun.COM 	 */
33529412SAleksandr.Guzovskiy@Sun.COM 
33539412SAleksandr.Guzovskiy@Sun.COM 	/*
33549412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
33559412SAleksandr.Guzovskiy@Sun.COM 	 */
3356*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3357*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3358*10922SJeff.Bonwick@Sun.COM 
3359*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3360*10922SJeff.Bonwick@Sun.COM 		return;
3361*10922SJeff.Bonwick@Sun.COM 
3362*10922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
3363*10922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
3364*10922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
3365*10922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
3366*10922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3367*10922SJeff.Bonwick@Sun.COM 
3368*10922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3369*10922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
3370*10922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
3371*10922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
33729412SAleksandr.Guzovskiy@Sun.COM 
33739412SAleksandr.Guzovskiy@Sun.COM 	/*
33749412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
33759412SAleksandr.Guzovskiy@Sun.COM 	 */
33769412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
33779412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
33789412SAleksandr.Guzovskiy@Sun.COM 
33799412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
33809412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
33819412SAleksandr.Guzovskiy@Sun.COM 
3382*10922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
3383*10922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
33849412SAleksandr.Guzovskiy@Sun.COM 
33859412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
33869412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
33879412SAleksandr.Guzovskiy@Sun.COM 
3388*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
33899412SAleksandr.Guzovskiy@Sun.COM 
33909412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
33919412SAleksandr.Guzovskiy@Sun.COM 
33929412SAleksandr.Guzovskiy@Sun.COM 	/*
33939412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
33949412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
33959412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
33969412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
33979412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
33989412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
33999412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
34009412SAleksandr.Guzovskiy@Sun.COM 	 */
34019412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
34029412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
34039412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
34049412SAleksandr.Guzovskiy@Sun.COM 
34059412SAleksandr.Guzovskiy@Sun.COM 		/*
34069412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
34079412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
34089412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
34099412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
34109412SAleksandr.Guzovskiy@Sun.COM 		 */
34119412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
34129412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
34139412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
3414*10922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
34159412SAleksandr.Guzovskiy@Sun.COM 			} else {
34169412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
3417*10922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
34189412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
3419*10922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
34209412SAleksandr.Guzovskiy@Sun.COM 			}
34219412SAleksandr.Guzovskiy@Sun.COM 		}
34229412SAleksandr.Guzovskiy@Sun.COM 
34239412SAleksandr.Guzovskiy@Sun.COM 		/*
34249412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
34259412SAleksandr.Guzovskiy@Sun.COM 		 */
34269412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
34279412SAleksandr.Guzovskiy@Sun.COM 
3428*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
3429*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3430*10922SJeff.Bonwick@Sun.COM 
3431*10922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3432*10922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
34339412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
34349412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
34359412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
34369412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
34379412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
34389412SAleksandr.Guzovskiy@Sun.COM 				} else {
34399412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
34409412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
34419412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
34429412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
34439412SAleksandr.Guzovskiy@Sun.COM 				}
34449412SAleksandr.Guzovskiy@Sun.COM 			}
34459412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
34469412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
34479412SAleksandr.Guzovskiy@Sun.COM 			return;
34489412SAleksandr.Guzovskiy@Sun.COM 		}
34499412SAleksandr.Guzovskiy@Sun.COM 
34509412SAleksandr.Guzovskiy@Sun.COM 		/*
34519412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
34529412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
34539412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
34549412SAleksandr.Guzovskiy@Sun.COM 		 */
34559412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
3456*10922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
34579512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
34589412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
3459*10922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
34609512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
34619412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
34629412SAleksandr.Guzovskiy@Sun.COM 		}
34639412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3464*10922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
34659412SAleksandr.Guzovskiy@Sun.COM 
34669412SAleksandr.Guzovskiy@Sun.COM 		/*
34679412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
34689412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
34699412SAleksandr.Guzovskiy@Sun.COM 		 */
3470*10922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3471*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
34729412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
34739412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
34749412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
34759412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
34769412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
34779412SAleksandr.Guzovskiy@Sun.COM 		}
3478*10922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
34799412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
34809412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
34819412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
3482*10922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
34839412SAleksandr.Guzovskiy@Sun.COM 			} else {
34849412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
34859412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
3486*10922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
34879412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
3488*10922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
34899412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
3490*10922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
34919412SAleksandr.Guzovskiy@Sun.COM 			}
34929412SAleksandr.Guzovskiy@Sun.COM 
34939412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
3494*10922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
34959412SAleksandr.Guzovskiy@Sun.COM 				    FTAG, &dbt) == 0);
34969412SAleksandr.Guzovskiy@Sun.COM 			}
34979412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
34989412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
34999412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
35009412SAleksandr.Guzovskiy@Sun.COM 			} else {
35019412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
35029412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
35039412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
3504*10922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
35059412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
35069412SAleksandr.Guzovskiy@Sun.COM 			}
35079412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
35089412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
35099412SAleksandr.Guzovskiy@Sun.COM 			}
35109412SAleksandr.Guzovskiy@Sun.COM 		}
35119412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
35129412SAleksandr.Guzovskiy@Sun.COM 
35139412SAleksandr.Guzovskiy@Sun.COM 		/*
35149412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
35159412SAleksandr.Guzovskiy@Sun.COM 		 */
35169412SAleksandr.Guzovskiy@Sun.COM 		{
35179412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
35189412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
35199412SAleksandr.Guzovskiy@Sun.COM 
3520*10922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
35219512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
3522*10922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
35239512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
35249412SAleksandr.Guzovskiy@Sun.COM 
35259412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
35269412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
35279412SAleksandr.Guzovskiy@Sun.COM 
35289412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
35299412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
35309412SAleksandr.Guzovskiy@Sun.COM 		}
35319412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
35329412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
35339412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
35349412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
35359412SAleksandr.Guzovskiy@Sun.COM 		}
35369412SAleksandr.Guzovskiy@Sun.COM 	}
35379412SAleksandr.Guzovskiy@Sun.COM 
35389412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
35399412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
35409412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
35419412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35429412SAleksandr.Guzovskiy@Sun.COM }
35439412SAleksandr.Guzovskiy@Sun.COM 
3544*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
35459412SAleksandr.Guzovskiy@Sun.COM void
3546*10922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
35473711Smaybee {
3548*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3549*10922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3550*10922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
35513711Smaybee 
35523711Smaybee 	/*
3553*10922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
3554*10922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
3555*10922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
35563711Smaybee 	 */
3557*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3558*10922SJeff.Bonwick@Sun.COM 
3559*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3560*10922SJeff.Bonwick@Sun.COM 		return;
3561*10922SJeff.Bonwick@Sun.COM 
3562*10922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
3563*10922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
35643711Smaybee }
35653711Smaybee 
35663711Smaybee void
3567*10922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3568789Sahrens {
3569*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3570*10922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3571*10922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3572*10922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
3573*10922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
3574*10922SJeff.Bonwick@Sun.COM 	void *data;
3575*10922SJeff.Bonwick@Sun.COM 
3576*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3577*10922SJeff.Bonwick@Sun.COM 
3578*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
35795530Sbonwick 		return;
3580*10922SJeff.Bonwick@Sun.COM 
3581*10922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
3582*10922SJeff.Bonwick@Sun.COM 		return;
3583*10922SJeff.Bonwick@Sun.COM 
3584*10922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
3585*10922SJeff.Bonwick@Sun.COM 
3586*10922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
3587*10922SJeff.Bonwick@Sun.COM 
3588*10922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
3589*10922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
3590*10922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3591*10922SJeff.Bonwick@Sun.COM 		    data) != 0)
3592*10922SJeff.Bonwick@Sun.COM 			break;
3593*10922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
3594*10922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
35955530Sbonwick 	}
3596*10922SJeff.Bonwick@Sun.COM 
3597*10922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3598789Sahrens }
3599789Sahrens 
3600789Sahrens /*
3601789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3602789Sahrens  */
3603789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3604789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3605789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3606789Sahrens 
3607789Sahrens void
3608*10922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3609789Sahrens {
3610*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3611*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3612789Sahrens 	uint64_t object;
3613789Sahrens 	uint64_t txg, last_txg;
3614789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3615789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3616789Sahrens 	int i, ints;
3617789Sahrens 	dmu_tx_t *tx;
3618789Sahrens 	char propname[100], txgname[100];
3619789Sahrens 	int error;
3620789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3621789Sahrens 
3622*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3623*10922SJeff.Bonwick@Sun.COM 
3624*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3625*10922SJeff.Bonwick@Sun.COM 		return;
3626*10922SJeff.Bonwick@Sun.COM 
3627*10922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3628789Sahrens 
3629789Sahrens 	/*
3630*10922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
3631*10922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3632789Sahrens 	 */
3633*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
3634*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3635*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3636*10922SJeff.Bonwick@Sun.COM 	if (txg == 0)
3637*10922SJeff.Bonwick@Sun.COM 		return;
3638*10922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
3639*10922SJeff.Bonwick@Sun.COM 		value[i] = i;
3640*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3641*10922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3642789Sahrens 	}
3643*10922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
3644*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3645*10922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
3646*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
3647*10922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3648*10922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3649*10922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
3650*10922SJeff.Bonwick@Sun.COM 	}
3651*10922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
3652*10922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3653*10922SJeff.Bonwick@Sun.COM 	}
3654*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
3655*10922SJeff.Bonwick@Sun.COM 
3656*10922SJeff.Bonwick@Sun.COM 	/*
3657*10922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
3658*10922SJeff.Bonwick@Sun.COM 	 */
3659789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3660789Sahrens 
36615530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
36625530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
36635530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
36645530Sbonwick 	bzero(value, sizeof (value));
36655530Sbonwick 	last_txg = 0;
36665530Sbonwick 
36675530Sbonwick 	/*
36685530Sbonwick 	 * If these zap entries already exist, validate their contents.
36695530Sbonwick 	 */
36705530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
36715530Sbonwick 	if (error == 0) {
36725530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
36735530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
36745530Sbonwick 
36755530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
36765530Sbonwick 		    zl_ints, &last_txg) == 0);
36775530Sbonwick 
36785530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
36795530Sbonwick 		    &zl_ints) == 0);
36805530Sbonwick 
36815530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
36825530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
36835530Sbonwick 
36845530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
36855530Sbonwick 		    zl_ints, value) == 0);
36865530Sbonwick 
36875530Sbonwick 		for (i = 0; i < ints; i++) {
36885530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3689789Sahrens 		}
36905530Sbonwick 	} else {
36915530Sbonwick 		ASSERT3U(error, ==, ENOENT);
36925530Sbonwick 	}
36935530Sbonwick 
36945530Sbonwick 	/*
36955530Sbonwick 	 * Atomically update two entries in our zap object.
36965530Sbonwick 	 * The first is named txg_%llu, and contains the txg
36975530Sbonwick 	 * in which the property was last updated.  The second
36985530Sbonwick 	 * is named prop_%llu, and the nth element of its value
36995530Sbonwick 	 * should be txg + object + n.
37005530Sbonwick 	 */
37015530Sbonwick 	tx = dmu_tx_create(os);
3702*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3703*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3704*10922SJeff.Bonwick@Sun.COM 	if (txg == 0)
37055530Sbonwick 		return;
37065530Sbonwick 
37075530Sbonwick 	if (last_txg > txg)
37085530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
37095530Sbonwick 
37105530Sbonwick 	for (i = 0; i < ints; i++)
37115530Sbonwick 		value[i] = txg + object + i;
37125530Sbonwick 
3713*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3714*10922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
3715*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3716*10922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
37175530Sbonwick 
37185530Sbonwick 	dmu_tx_commit(tx);
37195530Sbonwick 
37205530Sbonwick 	/*
37215530Sbonwick 	 * Remove a random pair of entries.
37225530Sbonwick 	 */
37235530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37245530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37255530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37265530Sbonwick 
37275530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
37285530Sbonwick 
37295530Sbonwick 	if (error == ENOENT)
37305530Sbonwick 		return;
37315530Sbonwick 
37325530Sbonwick 	ASSERT3U(error, ==, 0);
37335530Sbonwick 
37345530Sbonwick 	tx = dmu_tx_create(os);
3735*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3736*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3737*10922SJeff.Bonwick@Sun.COM 	if (txg == 0)
37385530Sbonwick 		return;
3739*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3740*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3741789Sahrens 	dmu_tx_commit(tx);
3742789Sahrens }
3743789Sahrens 
374410431SSanjeev.Bagewadi@Sun.COM /*
374510431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
374610431SSanjeev.Bagewadi@Sun.COM  */
374710431SSanjeev.Bagewadi@Sun.COM void
3748*10922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
374910431SSanjeev.Bagewadi@Sun.COM {
3750*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3751*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3752*10922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
3753*10922SJeff.Bonwick@Sun.COM 
3754*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3755*10922SJeff.Bonwick@Sun.COM 
3756*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3757*10922SJeff.Bonwick@Sun.COM 		return;
3758*10922SJeff.Bonwick@Sun.COM 
3759*10922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
376010431SSanjeev.Bagewadi@Sun.COM 
376110431SSanjeev.Bagewadi@Sun.COM 	/*
3762*10922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
3763*10922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
3764*10922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
376510431SSanjeev.Bagewadi@Sun.COM 	 */
3766*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
3767*10922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
3768*10922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
3769*10922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
3770*10922SJeff.Bonwick@Sun.COM 		int error;
3771*10922SJeff.Bonwick@Sun.COM 
3772*10922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
3773*10922SJeff.Bonwick@Sun.COM 		    id, value);
377410431SSanjeev.Bagewadi@Sun.COM 
377510431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
3776*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
3777*10922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3778*10922SJeff.Bonwick@Sun.COM 		if (txg == 0)
377910431SSanjeev.Bagewadi@Sun.COM 			return;
3780*10922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
3781*10922SJeff.Bonwick@Sun.COM 		    &value, tx);
378210431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
378310431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
378410431SSanjeev.Bagewadi@Sun.COM 	}
378510431SSanjeev.Bagewadi@Sun.COM }
378610431SSanjeev.Bagewadi@Sun.COM 
3787*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3788789Sahrens void
3789*10922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3790789Sahrens {
3791*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3792*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3793789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3794789Sahrens 	dmu_tx_t *tx;
3795789Sahrens 	int i, namelen, error;
3796*10922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3797789Sahrens 	char name[20], string_value[20];
3798789Sahrens 	void *data;
3799789Sahrens 
3800*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3801*10922SJeff.Bonwick@Sun.COM 
3802*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3803*10922SJeff.Bonwick@Sun.COM 		return;
3804*10922SJeff.Bonwick@Sun.COM 
3805*10922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3806*10922SJeff.Bonwick@Sun.COM 
38075530Sbonwick 	/*
38085530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
38095530Sbonwick 	 * x is a random printable character and the dots are dots.
38105530Sbonwick 	 * There are 94 such characters, and the name length goes from
38115530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
38125530Sbonwick 	 */
38135530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
38145530Sbonwick 
38155530Sbonwick 	for (i = 0; i < 3; i++)
38165530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
38175530Sbonwick 	for (; i < namelen - 1; i++)
38185530Sbonwick 		name[i] = '.';
38195530Sbonwick 	name[i] = '\0';
38205530Sbonwick 
3821*10922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
38225530Sbonwick 		wsize = sizeof (txg);
38235530Sbonwick 		wc = 1;
38245530Sbonwick 		data = &txg;
38255530Sbonwick 	} else {
38265530Sbonwick 		wsize = 1;
38275530Sbonwick 		wc = namelen;
38285530Sbonwick 		data = string_value;
38295530Sbonwick 	}
38305530Sbonwick 
38315530Sbonwick 	count = -1ULL;
38325530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
38335530Sbonwick 	ASSERT(count != -1ULL);
38345530Sbonwick 
38355530Sbonwick 	/*
38365530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
38375530Sbonwick 	 */
38385530Sbonwick 	i = ztest_random(5);
38395530Sbonwick 
38405530Sbonwick 	if (i >= 2) {
38415530Sbonwick 		tx = dmu_tx_create(os);
3842*10922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3843*10922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3844*10922SJeff.Bonwick@Sun.COM 		if (txg == 0)
38455530Sbonwick 			return;
38465530Sbonwick 		bcopy(name, string_value, namelen);
38475530Sbonwick 	} else {
38485530Sbonwick 		tx = NULL;
38495530Sbonwick 		txg = 0;
38505530Sbonwick 		bzero(string_value, namelen);
38515530Sbonwick 	}
38525530Sbonwick 
38535530Sbonwick 	switch (i) {
38545530Sbonwick 
38555530Sbonwick 	case 0:
38565530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
38575530Sbonwick 		if (error == 0) {
38585530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
38595530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3860789Sahrens 		} else {
38615530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3862789Sahrens 		}
38635530Sbonwick 		break;
38645530Sbonwick 
38655530Sbonwick 	case 1:
38665530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
38675530Sbonwick 		if (error == 0) {
38685530Sbonwick 			if (data == string_value &&
38695530Sbonwick 			    bcmp(name, data, namelen) != 0)
38705530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
38715530Sbonwick 				    name, data, namelen);
38725530Sbonwick 		} else {
38735530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3874789Sahrens 		}
38755530Sbonwick 		break;
38765530Sbonwick 
38775530Sbonwick 	case 2:
38785530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
38795530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
38805530Sbonwick 		break;
38815530Sbonwick 
38825530Sbonwick 	case 3:
38835530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
38845530Sbonwick 		break;
38855530Sbonwick 
38865530Sbonwick 	case 4:
38875530Sbonwick 		error = zap_remove(os, object, name, tx);
38885530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
38895530Sbonwick 		break;
3890789Sahrens 	}
38915530Sbonwick 
38925530Sbonwick 	if (tx != NULL)
38935530Sbonwick 		dmu_tx_commit(tx);
3894789Sahrens }
3895789Sahrens 
389610612SRicardo.M.Correia@Sun.COM /*
389710612SRicardo.M.Correia@Sun.COM  * Commit callback data.
389810612SRicardo.M.Correia@Sun.COM  */
389910612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
390010612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
390110612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
390210612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
390310612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
390410612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
390510612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
390610612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
390710612SRicardo.M.Correia@Sun.COM 
390810612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
390910612SRicardo.M.Correia@Sun.COM static void
391010612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
391110612SRicardo.M.Correia@Sun.COM {
391210612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
391310612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
391410612SRicardo.M.Correia@Sun.COM 
391510612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
391610612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
391710612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
391810612SRicardo.M.Correia@Sun.COM 
391910612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
392010612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
392110612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
392210612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
392310612SRicardo.M.Correia@Sun.COM 		    synced_txg);
392410612SRicardo.M.Correia@Sun.COM 
392510612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
392610612SRicardo.M.Correia@Sun.COM 
392710612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
392810612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
392910612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
393010612SRicardo.M.Correia@Sun.COM 
393110612SRicardo.M.Correia@Sun.COM 		/*
393210612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
393310612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
393410612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
393510612SRicardo.M.Correia@Sun.COM 		 */
393610612SRicardo.M.Correia@Sun.COM 		return;
393710612SRicardo.M.Correia@Sun.COM 	}
393810612SRicardo.M.Correia@Sun.COM 
393910612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
394010612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
394110612SRicardo.M.Correia@Sun.COM 		goto out;
394210612SRicardo.M.Correia@Sun.COM 
394310612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
394410612SRicardo.M.Correia@Sun.COM 
394510612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
394610612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
394710612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
394810612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
394910612SRicardo.M.Correia@Sun.COM 
395010612SRicardo.M.Correia@Sun.COM out:
395110612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
395210612SRicardo.M.Correia@Sun.COM }
395310612SRicardo.M.Correia@Sun.COM 
395410612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
395510612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
395610612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
395710612SRicardo.M.Correia@Sun.COM {
395810612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
395910612SRicardo.M.Correia@Sun.COM 
396010612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
396110612SRicardo.M.Correia@Sun.COM 
396210612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
396310612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
396410612SRicardo.M.Correia@Sun.COM 
396510612SRicardo.M.Correia@Sun.COM 	return (cb_data);
396610612SRicardo.M.Correia@Sun.COM }
396710612SRicardo.M.Correia@Sun.COM 
396810612SRicardo.M.Correia@Sun.COM /*
396910612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
397010612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
397110612SRicardo.M.Correia@Sun.COM  * implementation bug.
397210612SRicardo.M.Correia@Sun.COM  */
397310612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
397410612SRicardo.M.Correia@Sun.COM 
397510612SRicardo.M.Correia@Sun.COM /*
397610612SRicardo.M.Correia@Sun.COM  * Commit callback test.
397710612SRicardo.M.Correia@Sun.COM  */
397810612SRicardo.M.Correia@Sun.COM void
3979*10922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
398010612SRicardo.M.Correia@Sun.COM {
3981*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
3982*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
398310612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
398410612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
398510612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
398610612SRicardo.M.Correia@Sun.COM 	int i, error;
398710612SRicardo.M.Correia@Sun.COM 
3988*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3989*10922SJeff.Bonwick@Sun.COM 
3990*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3991*10922SJeff.Bonwick@Sun.COM 		return;
3992*10922SJeff.Bonwick@Sun.COM 
399310612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
399410612SRicardo.M.Correia@Sun.COM 
399510612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
399610612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
399710612SRicardo.M.Correia@Sun.COM 
3998*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
399910612SRicardo.M.Correia@Sun.COM 
400010612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
400110612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
400210612SRicardo.M.Correia@Sun.COM 		error = -1;
400310612SRicardo.M.Correia@Sun.COM 
400410612SRicardo.M.Correia@Sun.COM 	if (!error)
400510612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
400610612SRicardo.M.Correia@Sun.COM 
400710612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
400810612SRicardo.M.Correia@Sun.COM 
400910612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
401010612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
401110612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
401210612SRicardo.M.Correia@Sun.COM 
401310612SRicardo.M.Correia@Sun.COM 	if (error) {
401410612SRicardo.M.Correia@Sun.COM 		/*
401510612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
401610612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
401710612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
401810612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
401910612SRicardo.M.Correia@Sun.COM 		 */
402010612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
402110612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
402210612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
402310612SRicardo.M.Correia@Sun.COM 		}
402410612SRicardo.M.Correia@Sun.COM 
402510612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
402610612SRicardo.M.Correia@Sun.COM 
402710612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
402810612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
402910612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
403010612SRicardo.M.Correia@Sun.COM 		}
403110612SRicardo.M.Correia@Sun.COM 
403210612SRicardo.M.Correia@Sun.COM 		return;
403310612SRicardo.M.Correia@Sun.COM 	}
403410612SRicardo.M.Correia@Sun.COM 
403510612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
403610612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
403710612SRicardo.M.Correia@Sun.COM 
403810612SRicardo.M.Correia@Sun.COM 	/*
403910612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
404010612SRicardo.M.Correia@Sun.COM 	 */
4041*10922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
404210612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
404310612SRicardo.M.Correia@Sun.COM 
404410612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
404510612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
404610612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
404710612SRicardo.M.Correia@Sun.COM 
4048*10922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
404910612SRicardo.M.Correia@Sun.COM 
405010612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
405110612SRicardo.M.Correia@Sun.COM 
405210612SRicardo.M.Correia@Sun.COM 	/*
405310612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
405410612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
405510612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
405610612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
405710612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
405810612SRicardo.M.Correia@Sun.COM 	 * implementation.
405910612SRicardo.M.Correia@Sun.COM 	 *
406010612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
406110612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
406210612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
406310612SRicardo.M.Correia@Sun.COM 	 */
406410612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
406510612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
406610612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
406710612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
406810612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
406910612SRicardo.M.Correia@Sun.COM 	}
407010612SRicardo.M.Correia@Sun.COM 
407110612SRicardo.M.Correia@Sun.COM 	/*
407210612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
407310612SRicardo.M.Correia@Sun.COM 	 *
407410612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
407510612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
407610612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
407710612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
407810612SRicardo.M.Correia@Sun.COM 	 */
407910612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
408010612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
408110612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
408210612SRicardo.M.Correia@Sun.COM 
408310612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
408410612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
408510612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
408610612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
408710612SRicardo.M.Correia@Sun.COM 		else
408810612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
408910612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
409010612SRicardo.M.Correia@Sun.COM 
409110612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
409210612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
409310612SRicardo.M.Correia@Sun.COM 
409410612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
409510612SRicardo.M.Correia@Sun.COM 	}
409610612SRicardo.M.Correia@Sun.COM 
409710612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
409810612SRicardo.M.Correia@Sun.COM 
409910612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
410010612SRicardo.M.Correia@Sun.COM }
410110612SRicardo.M.Correia@Sun.COM 
4102*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4103789Sahrens void
4104*10922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4105789Sahrens {
4106*10922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
4107*10922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
4108*10922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
4109*10922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
4110*10922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
4111*10922SJeff.Bonwick@Sun.COM 	};
4112*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4113*10922SJeff.Bonwick@Sun.COM 
4114*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
4115*10922SJeff.Bonwick@Sun.COM 
4116*10922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4117*10922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4118*10922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4119*10922SJeff.Bonwick@Sun.COM 
4120*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4121*10922SJeff.Bonwick@Sun.COM }
4122*10922SJeff.Bonwick@Sun.COM 
4123*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4124*10922SJeff.Bonwick@Sun.COM void
4125*10922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4126*10922SJeff.Bonwick@Sun.COM {
4127*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4128*10922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
4129*10922SJeff.Bonwick@Sun.COM 
4130*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
4131*10922SJeff.Bonwick@Sun.COM 
4132*10922SJeff.Bonwick@Sun.COM #if 0
4133*10922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4134*10922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4135*10922SJeff.Bonwick@Sun.COM #endif
4136*10922SJeff.Bonwick@Sun.COM 
4137*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4138*10922SJeff.Bonwick@Sun.COM 
4139*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
4140*10922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
4141*10922SJeff.Bonwick@Sun.COM 
4142*10922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
4143*10922SJeff.Bonwick@Sun.COM 
4144*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4145789Sahrens }
4146789Sahrens 
4147789Sahrens /*
414810693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
414910693Schris.kirby@sun.com  */
415010693Schris.kirby@sun.com void
4151*10922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
415210693Schris.kirby@sun.com {
415310693Schris.kirby@sun.com 	int error;
4154*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
415510693Schris.kirby@sun.com 	objset_t *origin;
415610693Schris.kirby@sun.com 	char snapname[100];
415710693Schris.kirby@sun.com 	char fullname[100];
415810693Schris.kirby@sun.com 	char clonename[100];
415910693Schris.kirby@sun.com 	char tag[100];
416010693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
416110693Schris.kirby@sun.com 
416210693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
416310693Schris.kirby@sun.com 
416410693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
416510693Schris.kirby@sun.com 
4166*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
416710693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4168*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4169*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
417010693Schris.kirby@sun.com 
417110693Schris.kirby@sun.com 	/*
417210693Schris.kirby@sun.com 	 * Clean up from any previous run.
417310693Schris.kirby@sun.com 	 */
417410693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
417510693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
417610693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
417710693Schris.kirby@sun.com 
417810693Schris.kirby@sun.com 	/*
417910693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
418010693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
418110693Schris.kirby@sun.com 	 */
418210693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
418310693Schris.kirby@sun.com 	if (error) {
418410693Schris.kirby@sun.com 		if (error == ENOSPC) {
418510693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
418610693Schris.kirby@sun.com 			goto out;
418710693Schris.kirby@sun.com 		}
418810693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
418910693Schris.kirby@sun.com 	}
419010693Schris.kirby@sun.com 
419110693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
419210693Schris.kirby@sun.com 	if (error)
419310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
419410693Schris.kirby@sun.com 
419510693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
419610693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
419710693Schris.kirby@sun.com 	if (error) {
419810693Schris.kirby@sun.com 		if (error == ENOSPC) {
419910693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
420010693Schris.kirby@sun.com 			goto out;
420110693Schris.kirby@sun.com 		}
420210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
420310693Schris.kirby@sun.com 	}
420410693Schris.kirby@sun.com 
420510693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
420610693Schris.kirby@sun.com 	if (error) {
420710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
420810693Schris.kirby@sun.com 		    fullname, error);
420910693Schris.kirby@sun.com 	}
421010693Schris.kirby@sun.com 
421110693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
421210693Schris.kirby@sun.com 	if (error)
421310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
421410693Schris.kirby@sun.com 
421510693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
421610693Schris.kirby@sun.com 	if (error != ENOENT)
421710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
421810693Schris.kirby@sun.com 
421910693Schris.kirby@sun.com 	/*
422010693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
422110693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
422210693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
422310693Schris.kirby@sun.com 	 */
422410693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
422510693Schris.kirby@sun.com 	if (error) {
422610693Schris.kirby@sun.com 		if (error == ENOSPC) {
422710693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
422810693Schris.kirby@sun.com 			goto out;
422910693Schris.kirby@sun.com 		}
423010693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
423110693Schris.kirby@sun.com 	}
423210693Schris.kirby@sun.com 
423310693Schris.kirby@sun.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE, B_TRUE);
423410693Schris.kirby@sun.com 	if (error)
423510693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
423610693Schris.kirby@sun.com 
423710693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
423810693Schris.kirby@sun.com 	if (error != EBUSY) {
423910693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
424010693Schris.kirby@sun.com 		    fullname, error);
424110693Schris.kirby@sun.com 	}
424210693Schris.kirby@sun.com 
424310693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
424410693Schris.kirby@sun.com 	if (error) {
424510693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
424610693Schris.kirby@sun.com 		    fullname, error);
424710693Schris.kirby@sun.com 	}
424810693Schris.kirby@sun.com 
424910693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
425010693Schris.kirby@sun.com 	if (error)
425110693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
425210693Schris.kirby@sun.com 
425310693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
425410693Schris.kirby@sun.com 
425510693Schris.kirby@sun.com out:
425610693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
425710693Schris.kirby@sun.com }
425810693Schris.kirby@sun.com 
425910693Schris.kirby@sun.com /*
4260789Sahrens  * Inject random faults into the on-disk data.
4261789Sahrens  */
4262*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4263789Sahrens void
4264*10922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4265789Sahrens {
4266*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4267*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4268789Sahrens 	int fd;
4269789Sahrens 	uint64_t offset;
4270789Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
4271789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4272789Sahrens 	uint64_t top, leaf;
4273789Sahrens 	char path0[MAXPATHLEN];
4274789Sahrens 	char pathrand[MAXPATHLEN];
4275789Sahrens 	size_t fsize;
4276789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4277789Sahrens 	int iters = 1000;
42787754SJeff.Bonwick@Sun.COM 	int maxfaults = zopt_maxfaults;
42797754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
42801544Seschrock 	uint64_t guid0 = 0;
428110685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
42821544Seschrock 
42837754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4284789Sahrens 
4285789Sahrens 	/*
42867754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4287789Sahrens 	 */
42887754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
42897754SJeff.Bonwick@Sun.COM 
42907754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
42917754SJeff.Bonwick@Sun.COM 		/*
4292*10922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
42937754SJeff.Bonwick@Sun.COM 		 */
4294*10922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
42957754SJeff.Bonwick@Sun.COM 		leaf = ztest_random(leaves);
42967754SJeff.Bonwick@Sun.COM 
42977754SJeff.Bonwick@Sun.COM 		/*
42987754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
42997754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
43007754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
43017754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
43027754SJeff.Bonwick@Sun.COM 		 */
43037754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
43047754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + 0);
43057754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
43067754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
43077754SJeff.Bonwick@Sun.COM 
43087754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
430910685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
431010685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
431110685SGeorge.Wilson@Sun.COM 
43127754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
43137754SJeff.Bonwick@Sun.COM 			/*
43147754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
43157754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
43167754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
43177754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
43187754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
43197754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
43207754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
43217754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
43227754SJeff.Bonwick@Sun.COM 			 */
43237754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
43247754SJeff.Bonwick@Sun.COM 
43257754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
43267754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
43277754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
43287754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
43297754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
43307754SJeff.Bonwick@Sun.COM 			} else {
43317754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
43327754SJeff.Bonwick@Sun.COM 			}
43337754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
43347754SJeff.Bonwick@Sun.COM 		}
43357754SJeff.Bonwick@Sun.COM 	} else {
43367754SJeff.Bonwick@Sun.COM 		/*
43377754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
43387754SJeff.Bonwick@Sun.COM 		 */
43397754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
43407754SJeff.Bonwick@Sun.COM 
43417754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
43427754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
43437754SJeff.Bonwick@Sun.COM 			return;
43447754SJeff.Bonwick@Sun.COM 		}
43457754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
43467754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
43477754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
43487754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
43497754SJeff.Bonwick@Sun.COM 
43507754SJeff.Bonwick@Sun.COM 		leaf = 0;
43517754SJeff.Bonwick@Sun.COM 		leaves = 1;
43527754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
43537754SJeff.Bonwick@Sun.COM 	}
4354789Sahrens 
43557754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
43567754SJeff.Bonwick@Sun.COM 
43571544Seschrock 	/*
435810685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
435910685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
43601544Seschrock 	 */
436110685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
43628241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
43638241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
43648241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
436510685SGeorge.Wilson@Sun.COM 
436610685SGeorge.Wilson@Sun.COM 			/*
436710685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
436810685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
436910685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
437010685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
437110685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
437210685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
437310685SGeorge.Wilson@Sun.COM 			 */
437410685SGeorge.Wilson@Sun.COM 			if (islog)
437510685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
437610685SGeorge.Wilson@Sun.COM 
43778241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
437810685SGeorge.Wilson@Sun.COM 
437910685SGeorge.Wilson@Sun.COM 			if (islog)
438010685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
43818241SJeff.Bonwick@Sun.COM 		} else {
43828241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
43838241SJeff.Bonwick@Sun.COM 		}
4384789Sahrens 	}
4385789Sahrens 
438610685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
438710685SGeorge.Wilson@Sun.COM 		return;
438810685SGeorge.Wilson@Sun.COM 
4389789Sahrens 	/*
43901544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4391789Sahrens 	 */
4392789Sahrens 	fd = open(pathrand, O_RDWR);
4393789Sahrens 
4394789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4395789Sahrens 		return;
4396789Sahrens 
4397789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4398789Sahrens 
4399789Sahrens 	while (--iters != 0) {
4400789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4401789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4402789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4403789Sahrens 
4404789Sahrens 		if (offset >= fsize)
4405789Sahrens 			continue;
4406789Sahrens 
4407*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7)
4408789Sahrens 			(void) printf("injecting bad word into %s,"
4409789Sahrens 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4410789Sahrens 
4411789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4412789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4413789Sahrens 			    offset, pathrand);
4414789Sahrens 	}
4415789Sahrens 
4416789Sahrens 	(void) close(fd);
4417789Sahrens }
4418789Sahrens 
4419789Sahrens /*
4420*10922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4421789Sahrens  */
4422789Sahrens void
4423*10922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4424789Sahrens {
4425*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4426*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4427*10922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
4428*10922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
4429*10922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
4430*10922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
4431*10922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
4432*10922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
4433*10922SJeff.Bonwick@Sun.COM 	void *buf;
4434*10922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
4435*10922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
4436*10922SJeff.Bonwick@Sun.COM 
4437*10922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
4438*10922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
4439*10922SJeff.Bonwick@Sun.COM 
4440*10922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4441*10922SJeff.Bonwick@Sun.COM 
4442*10922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4443*10922SJeff.Bonwick@Sun.COM 		return;
4444*10922SJeff.Bonwick@Sun.COM 
4445*10922SJeff.Bonwick@Sun.COM 	/*
4446*10922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
4447*10922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
4448*10922SJeff.Bonwick@Sun.COM 	 */
4449*10922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
4450*10922SJeff.Bonwick@Sun.COM 
4451*10922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4452*10922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
4453*10922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4454*10922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
4455*10922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
4456*10922SJeff.Bonwick@Sun.COM 		return;
4457*10922SJeff.Bonwick@Sun.COM 	}
4458*10922SJeff.Bonwick@Sun.COM 
4459*10922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
4460*10922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
4461*10922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4462*10922SJeff.Bonwick@Sun.COM 
4463*10922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
4464*10922SJeff.Bonwick@Sun.COM 
4465*10922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
4466*10922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4467*10922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4468*10922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
4469*10922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
4470*10922SJeff.Bonwick@Sun.COM 		return;
4471*10922SJeff.Bonwick@Sun.COM 	}
4472*10922SJeff.Bonwick@Sun.COM 
4473*10922SJeff.Bonwick@Sun.COM 	/*
4474*10922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
4475*10922SJeff.Bonwick@Sun.COM 	 */
4476*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
4477*10922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
4478*10922SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db) == 0);
4479*10922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
4480*10922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
4481*10922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4482*10922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4483*10922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
4484*10922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
4485*10922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
4486*10922SJeff.Bonwick@Sun.COM 	}
4487*10922SJeff.Bonwick@Sun.COM 
4488*10922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
4489*10922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
4490*10922SJeff.Bonwick@Sun.COM 
4491*10922SJeff.Bonwick@Sun.COM 	/*
4492*10922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
4493*10922SJeff.Bonwick@Sun.COM 	 */
4494*10922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db) == 0);
4495*10922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4496*10922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
4497*10922SJeff.Bonwick@Sun.COM 
4498*10922SJeff.Bonwick@Sun.COM 	/*
4499*10922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
4500*10922SJeff.Bonwick@Sun.COM 	 */
4501*10922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
4502*10922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
4503*10922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
4504*10922SJeff.Bonwick@Sun.COM 
4505*10922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4506*10922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4507*10922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4508*10922SJeff.Bonwick@Sun.COM 
4509*10922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
4510*10922SJeff.Bonwick@Sun.COM 
4511*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4512*10922SJeff.Bonwick@Sun.COM }
4513*10922SJeff.Bonwick@Sun.COM 
4514*10922SJeff.Bonwick@Sun.COM /*
4515*10922SJeff.Bonwick@Sun.COM  * Scrub the pool.
4516*10922SJeff.Bonwick@Sun.COM  */
4517*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4518*10922SJeff.Bonwick@Sun.COM void
4519*10922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
4520*10922SJeff.Bonwick@Sun.COM {
4521*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4522*10922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4523789Sahrens 
45247046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
4525*10922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
45267046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
4527789Sahrens }
4528789Sahrens 
4529789Sahrens /*
4530789Sahrens  * Rename the pool to a different name and then rename it back.
4531789Sahrens  */
4532*10922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4533789Sahrens void
4534*10922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4535789Sahrens {
4536*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4537789Sahrens 	char *oldname, *newname;
4538789Sahrens 	spa_t *spa;
4539789Sahrens 
4540*10922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
4541*10922SJeff.Bonwick@Sun.COM 
4542*10922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4543789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4544789Sahrens 	(void) strcpy(newname, oldname);
4545789Sahrens 	(void) strcat(newname, "_tmp");
4546789Sahrens 
4547789Sahrens 	/*
4548789Sahrens 	 * Do the rename
4549789Sahrens 	 */
4550*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4551789Sahrens 
4552789Sahrens 	/*
4553789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4554789Sahrens 	 */
4555*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4556789Sahrens 
4557789Sahrens 	/*
4558789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4559789Sahrens 	 */
4560*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4561*10922SJeff.Bonwick@Sun.COM 
4562*10922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4563789Sahrens 	spa_close(spa, FTAG);
4564789Sahrens 
4565789Sahrens 	/*
4566789Sahrens 	 * Rename it back to the original
4567789Sahrens 	 */
4568*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4569789Sahrens 
4570789Sahrens 	/*
4571789Sahrens 	 * Make sure it can still be opened
4572789Sahrens 	 */
4573*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4574*10922SJeff.Bonwick@Sun.COM 
4575*10922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4576789Sahrens 	spa_close(spa, FTAG);
4577789Sahrens 
4578789Sahrens 	umem_free(newname, strlen(newname) + 1);
4579789Sahrens 
4580*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4581789Sahrens }
4582789Sahrens 
4583789Sahrens /*
4584*10922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4585789Sahrens  */
4586789Sahrens static void
4587*10922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4588789Sahrens {
4589789Sahrens 	int status;
4590789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4591789Sahrens 	char zbuf[1024];
4592789Sahrens 	char *bin;
45934527Sperrin 	char *ztest;
45944527Sperrin 	char *isa;
45954527Sperrin 	int isalen;
4596789Sahrens 	FILE *fp;
4597789Sahrens 
4598789Sahrens 	(void) realpath(getexecname(), zdb);
4599789Sahrens 
4600789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4601789Sahrens 	bin = strstr(zdb, "/usr/bin/");
46024527Sperrin 	ztest = strstr(bin, "/ztest");
46034527Sperrin 	isa = bin + 8;
46044527Sperrin 	isalen = ztest - isa;
46054527Sperrin 	isa = strdup(isa);
4606789Sahrens 	/* LINTED */
46075994Sck153898 	(void) sprintf(bin,
46089463SVictor.Latushkin@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s",
46094527Sperrin 	    isalen,
46104527Sperrin 	    isa,
4611789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4612789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
46137837SMatthew.Ahrens@Sun.COM 	    pool);
46144527Sperrin 	free(isa);
4615789Sahrens 
4616789Sahrens 	if (zopt_verbose >= 5)
4617789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4618789Sahrens 
4619789Sahrens 	fp = popen(zdb, "r");
4620789Sahrens 
4621789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4622789Sahrens 		if (zopt_verbose >= 3)
4623789Sahrens 			(void) printf("%s", zbuf);
4624789Sahrens 
4625789Sahrens 	status = pclose(fp);
4626789Sahrens 
4627789Sahrens 	if (status == 0)
4628789Sahrens 		return;
4629789Sahrens 
4630789Sahrens 	ztest_dump_core = 0;
4631789Sahrens 	if (WIFEXITED(status))
4632789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4633789Sahrens 	else
4634789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4635789Sahrens }
4636789Sahrens 
4637789Sahrens static void
4638789Sahrens ztest_walk_pool_directory(char *header)
4639789Sahrens {
4640789Sahrens 	spa_t *spa = NULL;
4641789Sahrens 
4642789Sahrens 	if (zopt_verbose >= 6)
4643789Sahrens 		(void) printf("%s\n", header);
4644789Sahrens 
4645789Sahrens 	mutex_enter(&spa_namespace_lock);
4646789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4647789Sahrens 		if (zopt_verbose >= 6)
4648789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4649789Sahrens 	mutex_exit(&spa_namespace_lock);
4650789Sahrens }
4651789Sahrens 
4652789Sahrens static void
4653789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4654789Sahrens {
46558241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4656789Sahrens 	uint64_t pool_guid;
4657789Sahrens 	spa_t *spa;
4658789Sahrens 
4659789Sahrens 	if (zopt_verbose >= 4) {
4660789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4661789Sahrens 		    oldname, newname);
4662789Sahrens 	}
4663789Sahrens 
4664789Sahrens 	/*
4665789Sahrens 	 * Clean up from previous runs.
4666789Sahrens 	 */
4667789Sahrens 	(void) spa_destroy(newname);
4668789Sahrens 
4669789Sahrens 	/*
4670789Sahrens 	 * Get the pool's configuration and guid.
4671789Sahrens 	 */
4672*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4673789Sahrens 
46748241SJeff.Bonwick@Sun.COM 	/*
46758241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
46768241SJeff.Bonwick@Sun.COM 	 */
46778241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
46788241SJeff.Bonwick@Sun.COM 		(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
46798241SJeff.Bonwick@Sun.COM 
4680789Sahrens 	pool_guid = spa_guid(spa);
4681789Sahrens 	spa_close(spa, FTAG);
4682789Sahrens 
4683789Sahrens 	ztest_walk_pool_directory("pools before export");
4684789Sahrens 
4685789Sahrens 	/*
4686789Sahrens 	 * Export it.
4687789Sahrens 	 */
4688*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4689789Sahrens 
4690789Sahrens 	ztest_walk_pool_directory("pools after export");
4691789Sahrens 
4692789Sahrens 	/*
46938241SJeff.Bonwick@Sun.COM 	 * Try to import it.
46948241SJeff.Bonwick@Sun.COM 	 */
46958241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
46968241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
46978241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
46988241SJeff.Bonwick@Sun.COM 
46998241SJeff.Bonwick@Sun.COM 	/*
4700789Sahrens 	 * Import it under the new name.
4701789Sahrens 	 */
4702*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL));
4703789Sahrens 
4704789Sahrens 	ztest_walk_pool_directory("pools after import");
4705789Sahrens 
4706789Sahrens 	/*
4707789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4708789Sahrens 	 */
4709*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL));
4710789Sahrens 
4711789Sahrens 	/*
4712789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4713789Sahrens 	 */
4714*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL));
4715789Sahrens 
4716789Sahrens 	/*
4717789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4718789Sahrens 	 */
4719*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4720789Sahrens 
4721789Sahrens 	/*
4722789Sahrens 	 * Verify that we can open and close the pool using the new name.
4723789Sahrens 	 */
4724*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4725789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4726789Sahrens 	spa_close(spa, FTAG);
4727789Sahrens 
4728789Sahrens 	nvlist_free(config);
4729789Sahrens }
4730789Sahrens 
47318241SJeff.Bonwick@Sun.COM static void
47328241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
47338241SJeff.Bonwick@Sun.COM {
4734*10922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
4735*10922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
4736*10922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
4737*10922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
4738*10922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
4739*10922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
47408241SJeff.Bonwick@Sun.COM }
47418241SJeff.Bonwick@Sun.COM 
47425329Sgw25295 static void *
47438241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
47445329Sgw25295 {
47457754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
47465329Sgw25295 
47475329Sgw25295 	while (!ztest_exiting) {
4748*10922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
4749*10922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
4750*10922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
47515329Sgw25295 	}
47525329Sgw25295 	return (NULL);
47535329Sgw25295 }
47545329Sgw25295 
4755789Sahrens static void *
4756*10922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
4757*10922SJeff.Bonwick@Sun.COM {
4758*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
4759*10922SJeff.Bonwick@Sun.COM 	int grace = 300;
4760*10922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
4761*10922SJeff.Bonwick@Sun.COM 
4762*10922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4763*10922SJeff.Bonwick@Sun.COM 
4764*10922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
4765*10922SJeff.Bonwick@Sun.COM 
4766*10922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
4767*10922SJeff.Bonwick@Sun.COM 
4768*10922SJeff.Bonwick@Sun.COM 	return (NULL);
4769*10922SJeff.Bonwick@Sun.COM }
4770*10922SJeff.Bonwick@Sun.COM 
4771*10922SJeff.Bonwick@Sun.COM static void
4772*10922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
4773*10922SJeff.Bonwick@Sun.COM {
4774*10922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4775*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4776*10922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
4777*10922SJeff.Bonwick@Sun.COM 
4778*10922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
4779*10922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
4780*10922SJeff.Bonwick@Sun.COM 
4781*10922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
4782*10922SJeff.Bonwick@Sun.COM 
4783*10922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
4784*10922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
4785*10922SJeff.Bonwick@Sun.COM 
4786*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
4787*10922SJeff.Bonwick@Sun.COM 		Dl_info dli;
4788*10922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
4789*10922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
4790*10922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
4791*10922SJeff.Bonwick@Sun.COM 	}
4792*10922SJeff.Bonwick@Sun.COM }
4793*10922SJeff.Bonwick@Sun.COM 
4794*10922SJeff.Bonwick@Sun.COM static void *
4795789Sahrens ztest_thread(void *arg)
4796789Sahrens {
4797*10922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4798789Sahrens 	ztest_shared_t *zs = ztest_shared;
4799*10922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
4800*10922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4801789Sahrens 	ztest_info_t *zi;
4802*10922SJeff.Bonwick@Sun.COM 
4803*10922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4804789Sahrens 		/*
4805789Sahrens 		 * See if it's time to force a crash.
4806789Sahrens 		 */
4807*10922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
4808*10922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4809789Sahrens 
4810789Sahrens 		/*
4811789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4812789Sahrens 		 */
4813789Sahrens 		if (zs->zs_enospc_count > 10)
4814789Sahrens 			break;
4815*10922SJeff.Bonwick@Sun.COM 
4816*10922SJeff.Bonwick@Sun.COM 		/*
4817*10922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
4818*10922SJeff.Bonwick@Sun.COM 		 */
4819*10922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
4820*10922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
4821*10922SJeff.Bonwick@Sun.COM 
4822*10922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
4823*10922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
4824*10922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
4825*10922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4826789Sahrens 	}
4827789Sahrens 
4828789Sahrens 	return (NULL);
4829789Sahrens }
4830789Sahrens 
4831*10922SJeff.Bonwick@Sun.COM static void
4832*10922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
4833*10922SJeff.Bonwick@Sun.COM {
4834*10922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
4835*10922SJeff.Bonwick@Sun.COM }
4836*10922SJeff.Bonwick@Sun.COM 
4837*10922SJeff.Bonwick@Sun.COM static void
4838*10922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
4839*10922SJeff.Bonwick@Sun.COM {
4840*10922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
4841*10922SJeff.Bonwick@Sun.COM 
4842*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
4843*10922SJeff.Bonwick@Sun.COM 
4844*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
4845*10922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
4846*10922SJeff.Bonwick@Sun.COM 
4847*10922SJeff.Bonwick@Sun.COM 	/*
4848*10922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
4849*10922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
4850*10922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
4851*10922SJeff.Bonwick@Sun.COM 	 */
4852*10922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
4853*10922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
4854*10922SJeff.Bonwick@Sun.COM 
4855*10922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
4856*10922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
4857*10922SJeff.Bonwick@Sun.COM }
4858*10922SJeff.Bonwick@Sun.COM 
4859*10922SJeff.Bonwick@Sun.COM static void
4860*10922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
4861*10922SJeff.Bonwick@Sun.COM {
4862*10922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
4863*10922SJeff.Bonwick@Sun.COM 
4864*10922SJeff.Bonwick@Sun.COM 	/*
4865*10922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
4866*10922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
4867*10922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
4868*10922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
4869*10922SJeff.Bonwick@Sun.COM 	 *
4870*10922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
4871*10922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
4872*10922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
4873*10922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
4874*10922SJeff.Bonwick@Sun.COM 	 */
4875*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
4876*10922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
4877*10922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
4878*10922SJeff.Bonwick@Sun.COM }
4879*10922SJeff.Bonwick@Sun.COM 
4880*10922SJeff.Bonwick@Sun.COM static int
4881*10922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
4882*10922SJeff.Bonwick@Sun.COM {
4883*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
4884*10922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
4885*10922SJeff.Bonwick@Sun.COM 	objset_t *os;
4886*10922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
4887*10922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
4888*10922SJeff.Bonwick@Sun.COM 	int error;
4889*10922SJeff.Bonwick@Sun.COM 
4890*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
4891*10922SJeff.Bonwick@Sun.COM 
4892*10922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
4893*10922SJeff.Bonwick@Sun.COM 
4894*10922SJeff.Bonwick@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
4895*10922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
4896*10922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
4897*10922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
4898*10922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
4899*10922SJeff.Bonwick@Sun.COM 		return (error);
4900*10922SJeff.Bonwick@Sun.COM 	}
4901*10922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
4902*10922SJeff.Bonwick@Sun.COM 
4903*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
4904*10922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4905*10922SJeff.Bonwick@Sun.COM 
4906*10922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
4907*10922SJeff.Bonwick@Sun.COM 
4908*10922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
4909*10922SJeff.Bonwick@Sun.COM 
4910*10922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
4911*10922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
4912*10922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
4913*10922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
4914*10922SJeff.Bonwick@Sun.COM 
4915*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
4916*10922SJeff.Bonwick@Sun.COM 
4917*10922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
4918*10922SJeff.Bonwick@Sun.COM 
4919*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
4920*10922SJeff.Bonwick@Sun.COM 
4921*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
4922*10922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
4923*10922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
4924*10922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
4925*10922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
4926*10922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
4927*10922SJeff.Bonwick@Sun.COM 
4928*10922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
4929*10922SJeff.Bonwick@Sun.COM 
4930*10922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
4931*10922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
4932*10922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
4933*10922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
4934*10922SJeff.Bonwick@Sun.COM 
4935*10922SJeff.Bonwick@Sun.COM 	return (0);
4936*10922SJeff.Bonwick@Sun.COM }
4937*10922SJeff.Bonwick@Sun.COM 
4938*10922SJeff.Bonwick@Sun.COM static void
4939*10922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
4940*10922SJeff.Bonwick@Sun.COM {
4941*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
4942*10922SJeff.Bonwick@Sun.COM 
4943*10922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
4944*10922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
4945*10922SJeff.Bonwick@Sun.COM 
4946*10922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
4947*10922SJeff.Bonwick@Sun.COM }
4948*10922SJeff.Bonwick@Sun.COM 
4949789Sahrens /*
4950789Sahrens  * Kick off threads to run tests on all datasets in parallel.
4951789Sahrens  */
4952789Sahrens static void
4953*10922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
4954789Sahrens {
4955*10922SJeff.Bonwick@Sun.COM 	thread_t *tid;
4956789Sahrens 	spa_t *spa;
49577754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
4958*10922SJeff.Bonwick@Sun.COM 	int error;
49597754SJeff.Bonwick@Sun.COM 
49607754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
4961789Sahrens 
4962*10922SJeff.Bonwick@Sun.COM 	/*
4963*10922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
4964*10922SJeff.Bonwick@Sun.COM 	 */
4965*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
4966*10922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
4967*10922SJeff.Bonwick@Sun.COM 
4968*10922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
4969*10922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
4970*10922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
4971*10922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
4972*10922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
4973*10922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
4974*10922SJeff.Bonwick@Sun.COM 
4975*10922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
497610612SRicardo.M.Correia@Sun.COM 
497710612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
497810612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
497910612SRicardo.M.Correia@Sun.COM 
4980789Sahrens 	/*
49817754SJeff.Bonwick@Sun.COM 	 * Open our pool.
49825329Sgw25295 	 */
4983*10922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
4984*10922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
4985*10922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
4986*10922SJeff.Bonwick@Sun.COM 
4987*10922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
49885329Sgw25295 
49895329Sgw25295 	/*
49908241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
49918241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
49928241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
49938241SJeff.Bonwick@Sun.COM 	 */
49948241SJeff.Bonwick@Sun.COM 	if (zopt_maxfaults == 0)
49958241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
49968241SJeff.Bonwick@Sun.COM 	else
49978241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
49988241SJeff.Bonwick@Sun.COM 
49998241SJeff.Bonwick@Sun.COM 	/*
50007754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5001789Sahrens 	 */
50028241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
50037754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5004789Sahrens 
5005789Sahrens 	/*
5006*10922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
5007*10922SJeff.Bonwick@Sun.COM 	 */
5008*10922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5009*10922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
5010*10922SJeff.Bonwick@Sun.COM 
5011*10922SJeff.Bonwick@Sun.COM 	/*
5012789Sahrens 	 * Verify that we can safely inquire about about any object,
5013789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5014789Sahrens 	 * we probe a 5-wide window around each power of two.
5015789Sahrens 	 * This hits all edge cases, including zero and the max.
5016789Sahrens 	 */
5017*10922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
5018*10922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5019789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5020789Sahrens 			    (1ULL << t) + d, NULL);
50211544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
50221544Seschrock 			    error == EINVAL);
5023789Sahrens 		}
5024789Sahrens 	}
5025789Sahrens 
5026789Sahrens 	/*
5027*10922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5028789Sahrens 	 */
5029*10922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
5030*10922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
5031*10922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
5032*10922SJeff.Bonwick@Sun.COM 	}
5033789Sahrens 	zs->zs_enospc_count = 0;
5034789Sahrens 
5035*10922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5036789Sahrens 
5037789Sahrens 	if (zopt_verbose >= 4)
5038789Sahrens 		(void) printf("starting main threads...\n");
5039789Sahrens 
5040*10922SJeff.Bonwick@Sun.COM 	/*
5041*10922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
5042*10922SJeff.Bonwick@Sun.COM 	 */
5043*10922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
5044*10922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5045*10922SJeff.Bonwick@Sun.COM 			return;
5046*10922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5047*10922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5048789Sahrens 	}
5049789Sahrens 
5050789Sahrens 	/*
5051*10922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
5052*10922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5053789Sahrens 	 */
5054*10922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
5055*10922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5056*10922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
5057*10922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5058789Sahrens 	}
5059789Sahrens 
50601544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5061789Sahrens 
5062*10922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5063*10922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5064*10922SJeff.Bonwick@Sun.COM 
5065*10922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
50667754SJeff.Bonwick@Sun.COM 
50677754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
50687754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
50697754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
50708241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
50717754SJeff.Bonwick@Sun.COM 
5072789Sahrens 	/*
5073789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5074789Sahrens 	 * spa_close() should wait for it to complete.
5075789Sahrens 	 */
5076*10922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
5077*10922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5078789Sahrens 
50795530Sbonwick 	spa_close(spa, FTAG);
50805530Sbonwick 
5081*10922SJeff.Bonwick@Sun.COM 	/*
5082*10922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
5083*10922SJeff.Bonwick@Sun.COM 	 */
5084*10922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
5085*10922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5086*10922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
5087*10922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
5088*10922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
5089*10922SJeff.Bonwick@Sun.COM 
5090*10922SJeff.Bonwick@Sun.COM 	/*
5091*10922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
5092*10922SJeff.Bonwick@Sun.COM 	 * different name.
5093*10922SJeff.Bonwick@Sun.COM 	 */
5094*10922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
5095*10922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
5096*10922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5097*10922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
5098*10922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
5099*10922SJeff.Bonwick@Sun.COM 	}
5100*10922SJeff.Bonwick@Sun.COM 
5101*10922SJeff.Bonwick@Sun.COM 	kernel_fini();
5102*10922SJeff.Bonwick@Sun.COM }
5103*10922SJeff.Bonwick@Sun.COM 
5104*10922SJeff.Bonwick@Sun.COM static void
5105*10922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
5106*10922SJeff.Bonwick@Sun.COM {
5107*10922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
5108*10922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5109*10922SJeff.Bonwick@Sun.COM 
5110*10922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
5111*10922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
5112*10922SJeff.Bonwick@Sun.COM 
5113*10922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
5114*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5115*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5116*10922SJeff.Bonwick@Sun.COM 
5117*10922SJeff.Bonwick@Sun.COM 	/*
5118*10922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
5119*10922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
5120*10922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
5121*10922SJeff.Bonwick@Sun.COM 	 */
5122*10922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5123*10922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
5124*10922SJeff.Bonwick@Sun.COM 		zil_commit(zd->zd_zilog, UINT64_MAX, 0);
5125*10922SJeff.Bonwick@Sun.COM 	}
5126*10922SJeff.Bonwick@Sun.COM 
5127*10922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
5128*10922SJeff.Bonwick@Sun.COM 
5129*10922SJeff.Bonwick@Sun.COM 	/*
5130*10922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
5131*10922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
5132*10922SJeff.Bonwick@Sun.COM 	 */
5133*10922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
5134*10922SJeff.Bonwick@Sun.COM 
5135*10922SJeff.Bonwick@Sun.COM 	/*
5136*10922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
5137*10922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5138*10922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
5139*10922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
5140*10922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
5141*10922SJeff.Bonwick@Sun.COM 	 */
5142*10922SJeff.Bonwick@Sun.COM 	while (ztest_random(20) != 0) {
5143*10922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
5144*10922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
5145*10922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
5146*10922SJeff.Bonwick@Sun.COM 	}
5147*10922SJeff.Bonwick@Sun.COM 
5148*10922SJeff.Bonwick@Sun.COM 	/*
5149*10922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
5150*10922SJeff.Bonwick@Sun.COM 	 */
5151*10922SJeff.Bonwick@Sun.COM 	zil_commit(zd->zd_zilog, UINT64_MAX, 0);
5152*10922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
5153*10922SJeff.Bonwick@Sun.COM 
5154*10922SJeff.Bonwick@Sun.COM 	/*
5155*10922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
5156*10922SJeff.Bonwick@Sun.COM 	 */
5157*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
5158*10922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5159*10922SJeff.Bonwick@Sun.COM 	kernel_fini();
5160*10922SJeff.Bonwick@Sun.COM 
5161*10922SJeff.Bonwick@Sun.COM 	/*
5162*10922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
5163*10922SJeff.Bonwick@Sun.COM 	 */
5164*10922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
5165*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5166*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5167*10922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
5168*10922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5169789Sahrens 	kernel_fini();
517010612SRicardo.M.Correia@Sun.COM 
517110612SRicardo.M.Correia@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
517210612SRicardo.M.Correia@Sun.COM 
517310612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
517410612SRicardo.M.Correia@Sun.COM 
517510612SRicardo.M.Correia@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
517610612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5177789Sahrens }
5178789Sahrens 
5179789Sahrens void
5180789Sahrens print_time(hrtime_t t, char *timebuf)
5181789Sahrens {
5182789Sahrens 	hrtime_t s = t / NANOSEC;
5183789Sahrens 	hrtime_t m = s / 60;
5184789Sahrens 	hrtime_t h = m / 60;
5185789Sahrens 	hrtime_t d = h / 24;
5186789Sahrens 
5187789Sahrens 	s -= m * 60;
5188789Sahrens 	m -= h * 60;
5189789Sahrens 	h -= d * 24;
5190789Sahrens 
5191789Sahrens 	timebuf[0] = '\0';
5192789Sahrens 
5193789Sahrens 	if (d)
5194789Sahrens 		(void) sprintf(timebuf,
5195789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5196789Sahrens 	else if (h)
5197789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5198789Sahrens 	else if (m)
5199789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5200789Sahrens 	else
5201789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5202789Sahrens }
5203789Sahrens 
5204789Sahrens /*
5205789Sahrens  * Create a storage pool with the given name and initial vdev size.
5206*10922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5207789Sahrens  */
5208789Sahrens static void
5209*10922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5210789Sahrens {
5211789Sahrens 	spa_t *spa;
5212789Sahrens 	nvlist_t *nvroot;
5213789Sahrens 
5214*10922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5215*10922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5216*10922SJeff.Bonwick@Sun.COM 
5217789Sahrens 	kernel_init(FREAD | FWRITE);
5218789Sahrens 
5219789Sahrens 	/*
5220789Sahrens 	 * Create the storage pool.
5221789Sahrens 	 */
5222*10922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
522310594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
52247754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
52257754SJeff.Bonwick@Sun.COM 	    0, zopt_raidz, zopt_mirrors, 1);
5226*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
5227789Sahrens 	nvlist_free(nvroot);
5228789Sahrens 
5229*10922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
52309480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5231789Sahrens 	spa_close(spa, FTAG);
5232789Sahrens 
5233789Sahrens 	kernel_fini();
5234*10922SJeff.Bonwick@Sun.COM 
5235*10922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5236*10922SJeff.Bonwick@Sun.COM 
5237*10922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
5238*10922SJeff.Bonwick@Sun.COM 
5239*10922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5240789Sahrens }
5241789Sahrens 
5242789Sahrens int
5243789Sahrens main(int argc, char **argv)
5244789Sahrens {
5245789Sahrens 	int kills = 0;
5246789Sahrens 	int iters = 0;
5247789Sahrens 	ztest_shared_t *zs;
5248*10922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5249789Sahrens 	ztest_info_t *zi;
5250789Sahrens 	char timebuf[100];
5251789Sahrens 	char numbuf[6];
5252*10922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5253789Sahrens 
5254789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5255789Sahrens 
5256789Sahrens 	/* Override location of zpool.cache */
52576643Seschrock 	spa_config_path = "/tmp/zpool.cache";
5258789Sahrens 
5259789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5260789Sahrens 
5261789Sahrens 	process_options(argc, argv);
5262789Sahrens 
52631544Seschrock 	/*
52641544Seschrock 	 * Blow away any existing copy of zpool.cache
52651544Seschrock 	 */
52661544Seschrock 	if (zopt_init != 0)
52671544Seschrock 		(void) remove("/tmp/zpool.cache");
52681544Seschrock 
5269*10922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5270*10922SJeff.Bonwick@Sun.COM 
5271789Sahrens 	zs = ztest_shared = (void *)mmap(0,
5272*10922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5273789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5274789Sahrens 
5275789Sahrens 	if (zopt_verbose >= 1) {
5276789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5277789Sahrens 		    " %llu seconds...\n",
52781732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5279789Sahrens 		    (u_longlong_t)zopt_time);
5280789Sahrens 	}
5281789Sahrens 
5282789Sahrens 	/*
5283789Sahrens 	 * Create and initialize our storage pool.
5284789Sahrens 	 */
5285*10922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5286789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5287789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5288789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
5289*10922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
5290*10922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5291789Sahrens 	}
5292789Sahrens 
5293*10922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
5294*10922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
5295*10922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5296*10922SJeff.Bonwick@Sun.COM 
5297*10922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5298789Sahrens 		zi = &zs->zs_info[f];
5299789Sahrens 		*zi = ztest_info[f];
5300*10922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5301*10922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5302789Sahrens 		else
5303*10922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
5304*10922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5305789Sahrens 	}
5306789Sahrens 
5307789Sahrens 	/*
5308789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5309789Sahrens 	 * to verify that self-healing data works, and forced crashes
5310789Sahrens 	 * to verify that we never lose on-disk consistency.
5311789Sahrens 	 */
5312*10922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5313789Sahrens 		int status;
5314789Sahrens 		pid_t pid;
5315789Sahrens 
5316789Sahrens 		/*
5317789Sahrens 		 * Initialize the workload counters for each function.
5318789Sahrens 		 */
5319*10922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5320789Sahrens 			zi = &zs->zs_info[f];
5321*10922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5322789Sahrens 			zi->zi_call_time = 0;
5323789Sahrens 		}
5324789Sahrens 
53259480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
53269480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
53279480SGeorge.Wilson@Sun.COM 
5328789Sahrens 		pid = fork();
5329789Sahrens 
5330789Sahrens 		if (pid == -1)
5331789Sahrens 			fatal(1, "fork failed");
5332789Sahrens 
5333789Sahrens 		if (pid == 0) {	/* child */
5334789Sahrens 			struct rlimit rl = { 1024, 1024 };
5335789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
53361914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
5337*10922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5338789Sahrens 			exit(0);
5339789Sahrens 		}
5340789Sahrens 
53412856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5342789Sahrens 			continue;
5343789Sahrens 
5344789Sahrens 		if (WIFEXITED(status)) {
5345789Sahrens 			if (WEXITSTATUS(status) != 0) {
5346789Sahrens 				(void) fprintf(stderr,
5347789Sahrens 				    "child exited with code %d\n",
5348789Sahrens 				    WEXITSTATUS(status));
5349789Sahrens 				exit(2);
5350789Sahrens 			}
53512856Snd150628 		} else if (WIFSIGNALED(status)) {
5352789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5353789Sahrens 				(void) fprintf(stderr,
5354789Sahrens 				    "child died with signal %d\n",
5355789Sahrens 				    WTERMSIG(status));
5356789Sahrens 				exit(3);
5357789Sahrens 			}
5358789Sahrens 			kills++;
53592856Snd150628 		} else {
53602856Snd150628 			(void) fprintf(stderr, "something strange happened "
53612856Snd150628 			    "to child\n");
53622856Snd150628 			exit(4);
5363789Sahrens 		}
5364789Sahrens 
5365789Sahrens 		iters++;
5366789Sahrens 
5367789Sahrens 		if (zopt_verbose >= 1) {
5368789Sahrens 			hrtime_t now = gethrtime();
5369789Sahrens 
5370*10922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
5371*10922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5372789Sahrens 			nicenum(zs->zs_space, numbuf);
5373789Sahrens 
5374789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5375789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5376789Sahrens 			    iters,
5377789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5378789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5379789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5380789Sahrens 			    numbuf,
5381*10922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5382789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5383789Sahrens 		}
5384789Sahrens 
5385789Sahrens 		if (zopt_verbose >= 2) {
5386789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5387789Sahrens 			(void) printf("%7s %9s   %s\n",
5388789Sahrens 			    "Calls", "Time", "Function");
5389789Sahrens 			(void) printf("%7s %9s   %s\n",
5390789Sahrens 			    "-----", "----", "--------");
5391*10922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5392789Sahrens 				Dl_info dli;
5393789Sahrens 
5394789Sahrens 				zi = &zs->zs_info[f];
5395789Sahrens 				print_time(zi->zi_call_time, timebuf);
5396789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5397789Sahrens 				(void) printf("%7llu %9s   %s\n",
5398*10922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5399789Sahrens 				    dli.dli_sname);
5400789Sahrens 			}
5401789Sahrens 			(void) printf("\n");
5402789Sahrens 		}
5403789Sahrens 
5404789Sahrens 		/*
5405*10922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
5406*10922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
5407*10922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5408789Sahrens 		 */
5409*10922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
5410*10922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5411*10922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
5412*10922SJeff.Bonwick@Sun.COM 		} else {
5413*10922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
5414*10922SJeff.Bonwick@Sun.COM 			kernel_fini();
5415*10922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
5416*10922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5417*10922SJeff.Bonwick@Sun.COM 			    zopt_pool);
5418*10922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
5419*10922SJeff.Bonwick@Sun.COM 		}
5420789Sahrens 		kernel_fini();
5421*10922SJeff.Bonwick@Sun.COM 
5422*10922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5423789Sahrens 	}
5424789Sahrens 
5425789Sahrens 	if (zopt_verbose >= 1) {
5426789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5427789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5428789Sahrens 	}
5429789Sahrens 
5430789Sahrens 	return (0);
5431789Sahrens }
5432