xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 11811)
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 /*
2211422SMark.Musante@Sun.COM  * Copyright 2010 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>
9010922SJeff.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>
10810922SJeff.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 
12910922SJeff.Bonwick@Sun.COM #define	BT_MAGIC	0x123456789abcdefULL
13011422SMark.Musante@Sun.COM #define	MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
13110922SJeff.Bonwick@Sun.COM 
13210922SJeff.Bonwick@Sun.COM enum ztest_io_type {
13310922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_TAG,
13410922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_PATTERN,
13510922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_ZEROES,
13610922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TRUNCATE,
13710922SJeff.Bonwick@Sun.COM 	ZTEST_IO_SETATTR,
13810922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TYPES
13910922SJeff.Bonwick@Sun.COM };
14010922SJeff.Bonwick@Sun.COM 
1415530Sbonwick typedef struct ztest_block_tag {
14210922SJeff.Bonwick@Sun.COM 	uint64_t	bt_magic;
1435530Sbonwick 	uint64_t	bt_objset;
1445530Sbonwick 	uint64_t	bt_object;
1455530Sbonwick 	uint64_t	bt_offset;
14610922SJeff.Bonwick@Sun.COM 	uint64_t	bt_gen;
1475530Sbonwick 	uint64_t	bt_txg;
14810922SJeff.Bonwick@Sun.COM 	uint64_t	bt_crtxg;
1495530Sbonwick } ztest_block_tag_t;
1505530Sbonwick 
15110922SJeff.Bonwick@Sun.COM typedef struct bufwad {
15210922SJeff.Bonwick@Sun.COM 	uint64_t	bw_index;
15310922SJeff.Bonwick@Sun.COM 	uint64_t	bw_txg;
15410922SJeff.Bonwick@Sun.COM 	uint64_t	bw_data;
15510922SJeff.Bonwick@Sun.COM } bufwad_t;
15610922SJeff.Bonwick@Sun.COM 
15710922SJeff.Bonwick@Sun.COM /*
15810922SJeff.Bonwick@Sun.COM  * XXX -- fix zfs range locks to be generic so we can use them here.
15910922SJeff.Bonwick@Sun.COM  */
16010922SJeff.Bonwick@Sun.COM typedef enum {
16110922SJeff.Bonwick@Sun.COM 	RL_READER,
16210922SJeff.Bonwick@Sun.COM 	RL_WRITER,
16310922SJeff.Bonwick@Sun.COM 	RL_APPEND
16410922SJeff.Bonwick@Sun.COM } rl_type_t;
16510922SJeff.Bonwick@Sun.COM 
16610922SJeff.Bonwick@Sun.COM typedef struct rll {
16710922SJeff.Bonwick@Sun.COM 	void		*rll_writer;
16810922SJeff.Bonwick@Sun.COM 	int		rll_readers;
16910922SJeff.Bonwick@Sun.COM 	mutex_t		rll_lock;
17010922SJeff.Bonwick@Sun.COM 	cond_t		rll_cv;
17110922SJeff.Bonwick@Sun.COM } rll_t;
17210922SJeff.Bonwick@Sun.COM 
17310922SJeff.Bonwick@Sun.COM typedef struct rl {
17410922SJeff.Bonwick@Sun.COM 	uint64_t	rl_object;
17510922SJeff.Bonwick@Sun.COM 	uint64_t	rl_offset;
17610922SJeff.Bonwick@Sun.COM 	uint64_t	rl_size;
17710922SJeff.Bonwick@Sun.COM 	rll_t		*rl_lock;
17810922SJeff.Bonwick@Sun.COM } rl_t;
17910922SJeff.Bonwick@Sun.COM 
18010922SJeff.Bonwick@Sun.COM #define	ZTEST_RANGE_LOCKS	64
18110922SJeff.Bonwick@Sun.COM #define	ZTEST_OBJECT_LOCKS	64
18210922SJeff.Bonwick@Sun.COM 
18310922SJeff.Bonwick@Sun.COM /*
18410922SJeff.Bonwick@Sun.COM  * Object descriptor.  Used as a template for object lookup/create/remove.
18510922SJeff.Bonwick@Sun.COM  */
18610922SJeff.Bonwick@Sun.COM typedef struct ztest_od {
18710922SJeff.Bonwick@Sun.COM 	uint64_t	od_dir;
18810922SJeff.Bonwick@Sun.COM 	uint64_t	od_object;
18910922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_type;
19010922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_crtype;
19110922SJeff.Bonwick@Sun.COM 	uint64_t	od_blocksize;
19210922SJeff.Bonwick@Sun.COM 	uint64_t	od_crblocksize;
19310922SJeff.Bonwick@Sun.COM 	uint64_t	od_gen;
19410922SJeff.Bonwick@Sun.COM 	uint64_t	od_crgen;
19510922SJeff.Bonwick@Sun.COM 	char		od_name[MAXNAMELEN];
19610922SJeff.Bonwick@Sun.COM } ztest_od_t;
19710922SJeff.Bonwick@Sun.COM 
19810922SJeff.Bonwick@Sun.COM /*
19910922SJeff.Bonwick@Sun.COM  * Per-dataset state.
20010922SJeff.Bonwick@Sun.COM  */
20110922SJeff.Bonwick@Sun.COM typedef struct ztest_ds {
20210922SJeff.Bonwick@Sun.COM 	objset_t	*zd_os;
20310922SJeff.Bonwick@Sun.COM 	zilog_t		*zd_zilog;
20410922SJeff.Bonwick@Sun.COM 	uint64_t	zd_seq;
20510922SJeff.Bonwick@Sun.COM 	ztest_od_t	*zd_od;		/* debugging aid */
20610922SJeff.Bonwick@Sun.COM 	char		zd_name[MAXNAMELEN];
20710922SJeff.Bonwick@Sun.COM 	mutex_t		zd_dirobj_lock;
20810922SJeff.Bonwick@Sun.COM 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
20910922SJeff.Bonwick@Sun.COM 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
21010922SJeff.Bonwick@Sun.COM } ztest_ds_t;
21110922SJeff.Bonwick@Sun.COM 
21210922SJeff.Bonwick@Sun.COM /*
21310922SJeff.Bonwick@Sun.COM  * Per-iteration state.
21410922SJeff.Bonwick@Sun.COM  */
21510922SJeff.Bonwick@Sun.COM typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
21610922SJeff.Bonwick@Sun.COM 
21710922SJeff.Bonwick@Sun.COM typedef struct ztest_info {
21810922SJeff.Bonwick@Sun.COM 	ztest_func_t	*zi_func;	/* test function */
21910922SJeff.Bonwick@Sun.COM 	uint64_t	zi_iters;	/* iterations per execution */
22010922SJeff.Bonwick@Sun.COM 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
22110922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_count;	/* per-pass count */
22210922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_time;	/* per-pass time */
22310922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_next;	/* next time to call this function */
22410922SJeff.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;
23410922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zap_parallel;
23510922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zil_commit;
23610922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_read_write_zcopy;
23710922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_objset_create_destroy;
23810922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_prealloc;
23910431SSanjeev.Bagewadi@Sun.COM ztest_func_t ztest_fzap;
24010922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_create_destroy;
241789Sahrens ztest_func_t ztest_dsl_prop_get_set;
24210922SJeff.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;
24510922SJeff.Bonwick@Sun.COM ztest_func_t ztest_ddt_repair;
24610922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_hold;
2477754SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_rename;
24810922SJeff.Bonwick@Sun.COM ztest_func_t ztest_scrub;
24910922SJeff.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;
25411422SMark.Musante@Sun.COM ztest_func_t ztest_split_pool;
25510922SJeff.Bonwick@Sun.COM 
25610922SJeff.Bonwick@Sun.COM uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
25710922SJeff.Bonwick@Sun.COM uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
25810922SJeff.Bonwick@Sun.COM uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
25910922SJeff.Bonwick@Sun.COM uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
26010922SJeff.Bonwick@Sun.COM uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
261789Sahrens 
262789Sahrens ztest_info_t ztest_info[] = {
2635530Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
26410922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
2655530Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
26610922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
2675530Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
2685530Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
26911422SMark.Musante@Sun.COM 	{ ztest_split_pool,			1,	&zopt_always	},
27010922SJeff.Bonwick@Sun.COM 	{ ztest_zil_commit,			1,	&zopt_incessant	},
27110922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
27210922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
27310922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
27410922SJeff.Bonwick@Sun.COM 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
27510922SJeff.Bonwick@Sun.COM #if 0
27610922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
27710922SJeff.Bonwick@Sun.COM #endif
27810922SJeff.Bonwick@Sun.COM 	{ ztest_fzap,				1,	&zopt_sometimes	},
27910922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
28010922SJeff.Bonwick@Sun.COM 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
2815530Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
28210922SJeff.Bonwick@Sun.COM 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
28310693Schris.kirby@sun.com 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
2845530Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
28510922SJeff.Bonwick@Sun.COM 	{ ztest_scrub,				1,	&zopt_rarely	},
28610922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
2877754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
2887754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
2897754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime	},
2907754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
291789Sahrens };
292789Sahrens 
293789Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
294789Sahrens 
295789Sahrens /*
29610612SRicardo.M.Correia@Sun.COM  * The following struct is used to hold a list of uncalled commit callbacks.
29710612SRicardo.M.Correia@Sun.COM  * The callbacks are ordered by txg number.
29810612SRicardo.M.Correia@Sun.COM  */
29910612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_list {
30010612SRicardo.M.Correia@Sun.COM 	mutex_t	zcl_callbacks_lock;
30110612SRicardo.M.Correia@Sun.COM 	list_t	zcl_callbacks;
30210612SRicardo.M.Correia@Sun.COM } ztest_cb_list_t;
30310612SRicardo.M.Correia@Sun.COM 
30410612SRicardo.M.Correia@Sun.COM /*
305789Sahrens  * Stuff we need to share writably between parent and child.
306789Sahrens  */
307789Sahrens typedef struct ztest_shared {
30810922SJeff.Bonwick@Sun.COM 	char		*zs_pool;
30910922SJeff.Bonwick@Sun.COM 	spa_t		*zs_spa;
31010922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_start;
31110922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_stop;
31210922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_start;
31310922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_stop;
31410922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_kill;
31510922SJeff.Bonwick@Sun.COM 	uint64_t	zs_enospc_count;
31610594SGeorge.Wilson@Sun.COM 	uint64_t	zs_vdev_next_leaf;
3177754SJeff.Bonwick@Sun.COM 	uint64_t	zs_vdev_aux;
318789Sahrens 	uint64_t	zs_alloc;
319789Sahrens 	uint64_t	zs_space;
32010922SJeff.Bonwick@Sun.COM 	mutex_t		zs_vdev_lock;
32110922SJeff.Bonwick@Sun.COM 	rwlock_t	zs_name_lock;
322789Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
32311422SMark.Musante@Sun.COM 	uint64_t	zs_splits;
32411422SMark.Musante@Sun.COM 	uint64_t	zs_mirrors;
32510922SJeff.Bonwick@Sun.COM 	ztest_ds_t	zs_zd[];
326789Sahrens } ztest_shared_t;
327789Sahrens 
32810922SJeff.Bonwick@Sun.COM #define	ID_PARALLEL	-1ULL
32910922SJeff.Bonwick@Sun.COM 
330789Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
3317754SJeff.Bonwick@Sun.COM static char ztest_aux_template[] = "%s/%s.%s.%llu";
33210922SJeff.Bonwick@Sun.COM ztest_shared_t *ztest_shared;
33310922SJeff.Bonwick@Sun.COM uint64_t *ztest_seq;
334789Sahrens 
335789Sahrens static int ztest_random_fd;
336789Sahrens static int ztest_dump_core = 1;
337789Sahrens 
3387754SJeff.Bonwick@Sun.COM static boolean_t ztest_exiting;
3395329Sgw25295 
34010612SRicardo.M.Correia@Sun.COM /* Global commit callback list */
34110612SRicardo.M.Correia@Sun.COM static ztest_cb_list_t zcl;
34210612SRicardo.M.Correia@Sun.COM 
3435530Sbonwick extern uint64_t metaslab_gang_bang;
3449480SGeorge.Wilson@Sun.COM extern uint64_t metaslab_df_alloc_threshold;
34510922SJeff.Bonwick@Sun.COM static uint64_t metaslab_sz;
34610922SJeff.Bonwick@Sun.COM 
34710922SJeff.Bonwick@Sun.COM enum ztest_object {
34810922SJeff.Bonwick@Sun.COM 	ZTEST_META_DNODE = 0,
34910922SJeff.Bonwick@Sun.COM 	ZTEST_DIROBJ,
35010922SJeff.Bonwick@Sun.COM 	ZTEST_OBJECTS
35110922SJeff.Bonwick@Sun.COM };
352789Sahrens 
3534008Sraf static void usage(boolean_t) __NORETURN;
3543972Svb160487 
355789Sahrens /*
356789Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
357789Sahrens  * debugging facilities.
358789Sahrens  */
359789Sahrens const char *
360789Sahrens _umem_debug_init()
361789Sahrens {
362789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
363789Sahrens }
364789Sahrens 
365789Sahrens const char *
366789Sahrens _umem_logging_init(void)
367789Sahrens {
368789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
369789Sahrens }
370789Sahrens 
371789Sahrens #define	FATAL_MSG_SZ	1024
372789Sahrens 
373789Sahrens char *fatal_msg;
374789Sahrens 
375789Sahrens static void
376789Sahrens fatal(int do_perror, char *message, ...)
377789Sahrens {
378789Sahrens 	va_list args;
379789Sahrens 	int save_errno = errno;
380789Sahrens 	char buf[FATAL_MSG_SZ];
381789Sahrens 
382789Sahrens 	(void) fflush(stdout);
383789Sahrens 
384789Sahrens 	va_start(args, message);
385789Sahrens 	(void) sprintf(buf, "ztest: ");
386789Sahrens 	/* LINTED */
387789Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
388789Sahrens 	va_end(args);
389789Sahrens 	if (do_perror) {
390789Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
391789Sahrens 		    ": %s", strerror(save_errno));
392789Sahrens 	}
393789Sahrens 	(void) fprintf(stderr, "%s\n", buf);
394789Sahrens 	fatal_msg = buf;			/* to ease debugging */
395789Sahrens 	if (ztest_dump_core)
396789Sahrens 		abort();
397789Sahrens 	exit(3);
398789Sahrens }
399789Sahrens 
400789Sahrens static int
401789Sahrens str2shift(const char *buf)
402789Sahrens {
403789Sahrens 	const char *ends = "BKMGTPEZ";
404789Sahrens 	int i;
405789Sahrens 
406789Sahrens 	if (buf[0] == '\0')
407789Sahrens 		return (0);
408789Sahrens 	for (i = 0; i < strlen(ends); i++) {
409789Sahrens 		if (toupper(buf[0]) == ends[i])
410789Sahrens 			break;
411789Sahrens 	}
4123972Svb160487 	if (i == strlen(ends)) {
4133972Svb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
4143972Svb160487 		    buf);
4153972Svb160487 		usage(B_FALSE);
4163972Svb160487 	}
417789Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
418789Sahrens 		return (10*i);
419789Sahrens 	}
4203972Svb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
4213972Svb160487 	usage(B_FALSE);
4223972Svb160487 	/* NOTREACHED */
423789Sahrens }
424789Sahrens 
425789Sahrens static uint64_t
426789Sahrens nicenumtoull(const char *buf)
427789Sahrens {
428789Sahrens 	char *end;
429789Sahrens 	uint64_t val;
430789Sahrens 
431789Sahrens 	val = strtoull(buf, &end, 0);
432789Sahrens 	if (end == buf) {
4333972Svb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
4343972Svb160487 		usage(B_FALSE);
435789Sahrens 	} else if (end[0] == '.') {
436789Sahrens 		double fval = strtod(buf, &end);
437789Sahrens 		fval *= pow(2, str2shift(end));
4383972Svb160487 		if (fval > UINT64_MAX) {
4393972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4403972Svb160487 			    buf);
4413972Svb160487 			usage(B_FALSE);
4423972Svb160487 		}
443789Sahrens 		val = (uint64_t)fval;
444789Sahrens 	} else {
445789Sahrens 		int shift = str2shift(end);
4463972Svb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
4473972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4483972Svb160487 			    buf);
4493972Svb160487 			usage(B_FALSE);
4503972Svb160487 		}
451789Sahrens 		val <<= shift;
452789Sahrens 	}
453789Sahrens 	return (val);
454789Sahrens }
455789Sahrens 
456789Sahrens static void
4573972Svb160487 usage(boolean_t requested)
458789Sahrens {
459789Sahrens 	char nice_vdev_size[10];
460789Sahrens 	char nice_gang_bang[10];
4613972Svb160487 	FILE *fp = requested ? stdout : stderr;
462789Sahrens 
463789Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
4645530Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
465789Sahrens 
4663972Svb160487 	(void) fprintf(fp, "Usage: %s\n"
467789Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
468789Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
4691732Sbonwick 	    "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
470789Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
471789Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
4722082Seschrock 	    "\t[-R raidz_parity (default: %d)]\n"
473789Sahrens 	    "\t[-d datasets (default: %d)]\n"
474789Sahrens 	    "\t[-t threads (default: %d)]\n"
475789Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
476789Sahrens 	    "\t[-i initialize pool i times (default: %d)]\n"
477789Sahrens 	    "\t[-k kill percentage (default: %llu%%)]\n"
478789Sahrens 	    "\t[-p pool_name (default: %s)]\n"
479789Sahrens 	    "\t[-f file directory for vdev files (default: %s)]\n"
480789Sahrens 	    "\t[-V(erbose)] (use multiple times for ever more blather)\n"
4811732Sbonwick 	    "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
482789Sahrens 	    "\t[-T time] total run time (default: %llu sec)\n"
483789Sahrens 	    "\t[-P passtime] time per pass (default: %llu sec)\n"
4843972Svb160487 	    "\t[-h] (print help)\n"
485789Sahrens 	    "",
486789Sahrens 	    cmdname,
4875329Sgw25295 	    (u_longlong_t)zopt_vdevs,			/* -v */
4885329Sgw25295 	    nice_vdev_size,				/* -s */
4895329Sgw25295 	    zopt_ashift,				/* -a */
4905329Sgw25295 	    zopt_mirrors,				/* -m */
4915329Sgw25295 	    zopt_raidz,					/* -r */
4925329Sgw25295 	    zopt_raidz_parity,				/* -R */
4935329Sgw25295 	    zopt_datasets,				/* -d */
4945329Sgw25295 	    zopt_threads,				/* -t */
4955329Sgw25295 	    nice_gang_bang,				/* -g */
4965329Sgw25295 	    zopt_init,					/* -i */
4975329Sgw25295 	    (u_longlong_t)zopt_killrate,		/* -k */
4985329Sgw25295 	    zopt_pool,					/* -p */
4995329Sgw25295 	    zopt_dir,					/* -f */
5005329Sgw25295 	    (u_longlong_t)zopt_time,			/* -T */
5017754SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zopt_passtime);		/* -P */
5023972Svb160487 	exit(requested ? 0 : 1);
503789Sahrens }
504789Sahrens 
505789Sahrens static void
506789Sahrens process_options(int argc, char **argv)
507789Sahrens {
508789Sahrens 	int opt;
509789Sahrens 	uint64_t value;
510789Sahrens 
511789Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
5125530Sbonwick 	metaslab_gang_bang = 32 << 10;
513789Sahrens 
514789Sahrens 	while ((opt = getopt(argc, argv,
5157754SJeff.Bonwick@Sun.COM 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
516789Sahrens 		value = 0;
517789Sahrens 		switch (opt) {
5184451Seschrock 		case 'v':
5194451Seschrock 		case 's':
5204451Seschrock 		case 'a':
5214451Seschrock 		case 'm':
5224451Seschrock 		case 'r':
5234451Seschrock 		case 'R':
5244451Seschrock 		case 'd':
5254451Seschrock 		case 't':
5264451Seschrock 		case 'g':
5274451Seschrock 		case 'i':
5284451Seschrock 		case 'k':
5294451Seschrock 		case 'T':
5304451Seschrock 		case 'P':
531789Sahrens 			value = nicenumtoull(optarg);
532789Sahrens 		}
533789Sahrens 		switch (opt) {
5344451Seschrock 		case 'v':
535789Sahrens 			zopt_vdevs = value;
536789Sahrens 			break;
5374451Seschrock 		case 's':
538789Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
539789Sahrens 			break;
5404451Seschrock 		case 'a':
5411732Sbonwick 			zopt_ashift = value;
5421732Sbonwick 			break;
5434451Seschrock 		case 'm':
544789Sahrens 			zopt_mirrors = value;
545789Sahrens 			break;
5464451Seschrock 		case 'r':
547789Sahrens 			zopt_raidz = MAX(1, value);
548789Sahrens 			break;
5494451Seschrock 		case 'R':
55010105Sadam.leventhal@sun.com 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
5512082Seschrock 			break;
5524451Seschrock 		case 'd':
5531732Sbonwick 			zopt_datasets = MAX(1, value);
554789Sahrens 			break;
5554451Seschrock 		case 't':
556789Sahrens 			zopt_threads = MAX(1, value);
557789Sahrens 			break;
5584451Seschrock 		case 'g':
5595530Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
560789Sahrens 			break;
5614451Seschrock 		case 'i':
562789Sahrens 			zopt_init = value;
563789Sahrens 			break;
5644451Seschrock 		case 'k':
565789Sahrens 			zopt_killrate = value;
566789Sahrens 			break;
5674451Seschrock 		case 'p':
568789Sahrens 			zopt_pool = strdup(optarg);
569789Sahrens 			break;
5704451Seschrock 		case 'f':
571789Sahrens 			zopt_dir = strdup(optarg);
572789Sahrens 			break;
5734451Seschrock 		case 'V':
574789Sahrens 			zopt_verbose++;
575789Sahrens 			break;
5764451Seschrock 		case 'E':
577789Sahrens 			zopt_init = 0;
578789Sahrens 			break;
5794451Seschrock 		case 'T':
580789Sahrens 			zopt_time = value;
581789Sahrens 			break;
5824451Seschrock 		case 'P':
583789Sahrens 			zopt_passtime = MAX(1, value);
584789Sahrens 			break;
5854451Seschrock 		case 'h':
5863972Svb160487 			usage(B_TRUE);
5873972Svb160487 			break;
5884451Seschrock 		case '?':
5894451Seschrock 		default:
5903972Svb160487 			usage(B_FALSE);
591789Sahrens 			break;
592789Sahrens 		}
593789Sahrens 	}
594789Sahrens 
5952082Seschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
5962082Seschrock 
59710922SJeff.Bonwick@Sun.COM 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
59810922SJeff.Bonwick@Sun.COM 	    UINT64_MAX >> 2);
599789Sahrens }
600789Sahrens 
60110922SJeff.Bonwick@Sun.COM static void
60210922SJeff.Bonwick@Sun.COM ztest_kill(ztest_shared_t *zs)
60310922SJeff.Bonwick@Sun.COM {
60410922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
60510922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
60610922SJeff.Bonwick@Sun.COM 	(void) kill(getpid(), SIGKILL);
60710922SJeff.Bonwick@Sun.COM }
60810922SJeff.Bonwick@Sun.COM 
60910922SJeff.Bonwick@Sun.COM static uint64_t
61010922SJeff.Bonwick@Sun.COM ztest_random(uint64_t range)
61110922SJeff.Bonwick@Sun.COM {
61210922SJeff.Bonwick@Sun.COM 	uint64_t r;
61310922SJeff.Bonwick@Sun.COM 
61410922SJeff.Bonwick@Sun.COM 	if (range == 0)
61510922SJeff.Bonwick@Sun.COM 		return (0);
61610922SJeff.Bonwick@Sun.COM 
61710922SJeff.Bonwick@Sun.COM 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
61810922SJeff.Bonwick@Sun.COM 		fatal(1, "short read from /dev/urandom");
61910922SJeff.Bonwick@Sun.COM 
62010922SJeff.Bonwick@Sun.COM 	return (r % range);
62110922SJeff.Bonwick@Sun.COM }
62210922SJeff.Bonwick@Sun.COM 
62310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
62410922SJeff.Bonwick@Sun.COM static void
62510922SJeff.Bonwick@Sun.COM ztest_record_enospc(const char *s)
62610922SJeff.Bonwick@Sun.COM {
62710922SJeff.Bonwick@Sun.COM 	ztest_shared->zs_enospc_count++;
62810922SJeff.Bonwick@Sun.COM }
62910922SJeff.Bonwick@Sun.COM 
6301732Sbonwick static uint64_t
6311732Sbonwick ztest_get_ashift(void)
6321732Sbonwick {
6331732Sbonwick 	if (zopt_ashift == 0)
6341732Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
6351732Sbonwick 	return (zopt_ashift);
6361732Sbonwick }
6371732Sbonwick 
638789Sahrens static nvlist_t *
6397754SJeff.Bonwick@Sun.COM make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
640789Sahrens {
6417754SJeff.Bonwick@Sun.COM 	char pathbuf[MAXPATHLEN];
642789Sahrens 	uint64_t vdev;
643789Sahrens 	nvlist_t *file;
644789Sahrens 
6457754SJeff.Bonwick@Sun.COM 	if (ashift == 0)
6467754SJeff.Bonwick@Sun.COM 		ashift = ztest_get_ashift();
6477754SJeff.Bonwick@Sun.COM 
6487754SJeff.Bonwick@Sun.COM 	if (path == NULL) {
6497754SJeff.Bonwick@Sun.COM 		path = pathbuf;
6507754SJeff.Bonwick@Sun.COM 
6517754SJeff.Bonwick@Sun.COM 		if (aux != NULL) {
6527754SJeff.Bonwick@Sun.COM 			vdev = ztest_shared->zs_vdev_aux;
6537754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template,
6547754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, aux, vdev);
6557754SJeff.Bonwick@Sun.COM 		} else {
65610594SGeorge.Wilson@Sun.COM 			vdev = ztest_shared->zs_vdev_next_leaf++;
6577754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_dev_template,
6587754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, vdev);
6597754SJeff.Bonwick@Sun.COM 		}
6607754SJeff.Bonwick@Sun.COM 	}
6617754SJeff.Bonwick@Sun.COM 
6627754SJeff.Bonwick@Sun.COM 	if (size != 0) {
6637754SJeff.Bonwick@Sun.COM 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
664789Sahrens 		if (fd == -1)
6657754SJeff.Bonwick@Sun.COM 			fatal(1, "can't open %s", path);
666789Sahrens 		if (ftruncate(fd, size) != 0)
6677754SJeff.Bonwick@Sun.COM 			fatal(1, "can't ftruncate %s", path);
668789Sahrens 		(void) close(fd);
669789Sahrens 	}
670789Sahrens 
671789Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
672789Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
6737754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
6741732Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
675789Sahrens 
676789Sahrens 	return (file);
677789Sahrens }
678789Sahrens 
679789Sahrens static nvlist_t *
6807754SJeff.Bonwick@Sun.COM make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
681789Sahrens {
682789Sahrens 	nvlist_t *raidz, **child;
683789Sahrens 	int c;
684789Sahrens 
685789Sahrens 	if (r < 2)
6867754SJeff.Bonwick@Sun.COM 		return (make_vdev_file(path, aux, size, ashift));
687789Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
688789Sahrens 
689789Sahrens 	for (c = 0; c < r; c++)
6907754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_file(path, aux, size, ashift);
691789Sahrens 
692789Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
693789Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
694789Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
6952082Seschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
6962082Seschrock 	    zopt_raidz_parity) == 0);
697789Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
698789Sahrens 	    child, r) == 0);
699789Sahrens 
700789Sahrens 	for (c = 0; c < r; c++)
701789Sahrens 		nvlist_free(child[c]);
702789Sahrens 
703789Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
704789Sahrens 
705789Sahrens 	return (raidz);
706789Sahrens }
707789Sahrens 
708789Sahrens static nvlist_t *
7097754SJeff.Bonwick@Sun.COM make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
7107768SJeff.Bonwick@Sun.COM 	int r, int m)
711789Sahrens {
712789Sahrens 	nvlist_t *mirror, **child;
713789Sahrens 	int c;
714789Sahrens 
715789Sahrens 	if (m < 1)
7167754SJeff.Bonwick@Sun.COM 		return (make_vdev_raidz(path, aux, size, ashift, r));
717789Sahrens 
718789Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
719789Sahrens 
720789Sahrens 	for (c = 0; c < m; c++)
7217754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
722789Sahrens 
723789Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
724789Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
725789Sahrens 	    VDEV_TYPE_MIRROR) == 0);
726789Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
727789Sahrens 	    child, m) == 0);
728789Sahrens 
729789Sahrens 	for (c = 0; c < m; c++)
730789Sahrens 		nvlist_free(child[c]);
731789Sahrens 
732789Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
733789Sahrens 
734789Sahrens 	return (mirror);
735789Sahrens }
736789Sahrens 
737789Sahrens static nvlist_t *
7387754SJeff.Bonwick@Sun.COM make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
7397754SJeff.Bonwick@Sun.COM 	int log, int r, int m, int t)
740789Sahrens {
741789Sahrens 	nvlist_t *root, **child;
742789Sahrens 	int c;
743789Sahrens 
744789Sahrens 	ASSERT(t > 0);
745789Sahrens 
746789Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
747789Sahrens 
7487768SJeff.Bonwick@Sun.COM 	for (c = 0; c < t; c++) {
7497768SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
7507768SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
7517768SJeff.Bonwick@Sun.COM 		    log) == 0);
7527768SJeff.Bonwick@Sun.COM 	}
753789Sahrens 
754789Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
755789Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
7567754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
757789Sahrens 	    child, t) == 0);
758789Sahrens 
759789Sahrens 	for (c = 0; c < t; c++)
760789Sahrens 		nvlist_free(child[c]);
761789Sahrens 
762789Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
763789Sahrens 
764789Sahrens 	return (root);
765789Sahrens }
766789Sahrens 
76710922SJeff.Bonwick@Sun.COM static int
76810922SJeff.Bonwick@Sun.COM ztest_random_blocksize(void)
769789Sahrens {
77010922SJeff.Bonwick@Sun.COM 	return (1 << (SPA_MINBLOCKSHIFT +
77110922SJeff.Bonwick@Sun.COM 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
772789Sahrens }
773789Sahrens 
774789Sahrens static int
77510922SJeff.Bonwick@Sun.COM ztest_random_ibshift(void)
77610922SJeff.Bonwick@Sun.COM {
77710922SJeff.Bonwick@Sun.COM 	return (DN_MIN_INDBLKSHIFT +
77810922SJeff.Bonwick@Sun.COM 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
77910922SJeff.Bonwick@Sun.COM }
78010922SJeff.Bonwick@Sun.COM 
78110922SJeff.Bonwick@Sun.COM static uint64_t
78210922SJeff.Bonwick@Sun.COM ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
783789Sahrens {
78410922SJeff.Bonwick@Sun.COM 	uint64_t top;
78510922SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
78610922SJeff.Bonwick@Sun.COM 	vdev_t *tvd;
78710922SJeff.Bonwick@Sun.COM 
78810922SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
78910922SJeff.Bonwick@Sun.COM 
79010922SJeff.Bonwick@Sun.COM 	do {
79110922SJeff.Bonwick@Sun.COM 		top = ztest_random(rvd->vdev_children);
79210922SJeff.Bonwick@Sun.COM 		tvd = rvd->vdev_child[top];
79310922SJeff.Bonwick@Sun.COM 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
79410922SJeff.Bonwick@Sun.COM 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
79510922SJeff.Bonwick@Sun.COM 
79610922SJeff.Bonwick@Sun.COM 	return (top);
79710922SJeff.Bonwick@Sun.COM }
79810922SJeff.Bonwick@Sun.COM 
79910922SJeff.Bonwick@Sun.COM static uint64_t
80010922SJeff.Bonwick@Sun.COM ztest_random_dsl_prop(zfs_prop_t prop)
80110922SJeff.Bonwick@Sun.COM {
80210922SJeff.Bonwick@Sun.COM 	uint64_t value;
80310922SJeff.Bonwick@Sun.COM 
80410922SJeff.Bonwick@Sun.COM 	do {
80510922SJeff.Bonwick@Sun.COM 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
80610922SJeff.Bonwick@Sun.COM 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
80710922SJeff.Bonwick@Sun.COM 
80810922SJeff.Bonwick@Sun.COM 	return (value);
80910922SJeff.Bonwick@Sun.COM }
81010922SJeff.Bonwick@Sun.COM 
81110922SJeff.Bonwick@Sun.COM static int
81210922SJeff.Bonwick@Sun.COM ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
81310922SJeff.Bonwick@Sun.COM     boolean_t inherit)
81410922SJeff.Bonwick@Sun.COM {
81510922SJeff.Bonwick@Sun.COM 	const char *propname = zfs_prop_to_name(prop);
81610922SJeff.Bonwick@Sun.COM 	const char *valname;
81710922SJeff.Bonwick@Sun.COM 	char setpoint[MAXPATHLEN];
81810922SJeff.Bonwick@Sun.COM 	uint64_t curval;
819789Sahrens 	int error;
820789Sahrens 
82111022STom.Erickson@Sun.COM 	error = dsl_prop_set(osname, propname,
82211022STom.Erickson@Sun.COM 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
82311022STom.Erickson@Sun.COM 	    sizeof (value), 1, &value);
82410922SJeff.Bonwick@Sun.COM 
82510922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
82610922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
827789Sahrens 		return (error);
828789Sahrens 	}
8292082Seschrock 	ASSERT3U(error, ==, 0);
83010922SJeff.Bonwick@Sun.COM 
83110922SJeff.Bonwick@Sun.COM 	VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
83210922SJeff.Bonwick@Sun.COM 	    1, &curval, setpoint), ==, 0);
83310922SJeff.Bonwick@Sun.COM 
83410922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
83510922SJeff.Bonwick@Sun.COM 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
83610922SJeff.Bonwick@Sun.COM 		(void) printf("%s %s = %s at '%s'\n",
83710922SJeff.Bonwick@Sun.COM 		    osname, propname, valname, setpoint);
838789Sahrens 	}
839789Sahrens 
840789Sahrens 	return (error);
841789Sahrens }
842789Sahrens 
843789Sahrens static int
84410922SJeff.Bonwick@Sun.COM ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
84510922SJeff.Bonwick@Sun.COM {
84610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
84710922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
84810922SJeff.Bonwick@Sun.COM 	int error;
84910922SJeff.Bonwick@Sun.COM 
85010922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
85110922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
85210922SJeff.Bonwick@Sun.COM 
85310922SJeff.Bonwick@Sun.COM 	error = spa_prop_set(spa, props);
85410922SJeff.Bonwick@Sun.COM 
85510922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
85610922SJeff.Bonwick@Sun.COM 
85710922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
85810922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
85910922SJeff.Bonwick@Sun.COM 		return (error);
86010922SJeff.Bonwick@Sun.COM 	}
86110922SJeff.Bonwick@Sun.COM 	ASSERT3U(error, ==, 0);
86210922SJeff.Bonwick@Sun.COM 
86310922SJeff.Bonwick@Sun.COM 	return (error);
86410922SJeff.Bonwick@Sun.COM }
86510922SJeff.Bonwick@Sun.COM 
86610922SJeff.Bonwick@Sun.COM static void
86710922SJeff.Bonwick@Sun.COM ztest_rll_init(rll_t *rll)
86810922SJeff.Bonwick@Sun.COM {
86910922SJeff.Bonwick@Sun.COM 	rll->rll_writer = NULL;
87010922SJeff.Bonwick@Sun.COM 	rll->rll_readers = 0;
87110922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
87210922SJeff.Bonwick@Sun.COM 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
87310922SJeff.Bonwick@Sun.COM }
87410922SJeff.Bonwick@Sun.COM 
87510922SJeff.Bonwick@Sun.COM static void
87610922SJeff.Bonwick@Sun.COM ztest_rll_destroy(rll_t *rll)
87710922SJeff.Bonwick@Sun.COM {
87810922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_writer == NULL);
87910922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_readers == 0);
88010922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
88110922SJeff.Bonwick@Sun.COM 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
88210922SJeff.Bonwick@Sun.COM }
88310922SJeff.Bonwick@Sun.COM 
88410922SJeff.Bonwick@Sun.COM static void
88510922SJeff.Bonwick@Sun.COM ztest_rll_lock(rll_t *rll, rl_type_t type)
88610922SJeff.Bonwick@Sun.COM {
88710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
88810922SJeff.Bonwick@Sun.COM 
88910922SJeff.Bonwick@Sun.COM 	if (type == RL_READER) {
89010922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL)
89110922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
89210922SJeff.Bonwick@Sun.COM 		rll->rll_readers++;
89310922SJeff.Bonwick@Sun.COM 	} else {
89410922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL || rll->rll_readers)
89510922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
89610922SJeff.Bonwick@Sun.COM 		rll->rll_writer = curthread;
89710922SJeff.Bonwick@Sun.COM 	}
89810922SJeff.Bonwick@Sun.COM 
89910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
90010922SJeff.Bonwick@Sun.COM }
90110922SJeff.Bonwick@Sun.COM 
90210922SJeff.Bonwick@Sun.COM static void
90310922SJeff.Bonwick@Sun.COM ztest_rll_unlock(rll_t *rll)
90410922SJeff.Bonwick@Sun.COM {
90510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
90610922SJeff.Bonwick@Sun.COM 
90710922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer) {
90810922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers == 0);
90910922SJeff.Bonwick@Sun.COM 		rll->rll_writer = NULL;
91010922SJeff.Bonwick@Sun.COM 	} else {
91110922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers != 0);
91210922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_writer == NULL);
91310922SJeff.Bonwick@Sun.COM 		rll->rll_readers--;
91410922SJeff.Bonwick@Sun.COM 	}
91510922SJeff.Bonwick@Sun.COM 
91610922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
91710922SJeff.Bonwick@Sun.COM 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
91810922SJeff.Bonwick@Sun.COM 
91910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
92010922SJeff.Bonwick@Sun.COM }
92110922SJeff.Bonwick@Sun.COM 
92210922SJeff.Bonwick@Sun.COM static void
92310922SJeff.Bonwick@Sun.COM ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
92410922SJeff.Bonwick@Sun.COM {
92510922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
92610922SJeff.Bonwick@Sun.COM 
92710922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
92810922SJeff.Bonwick@Sun.COM }
92910922SJeff.Bonwick@Sun.COM 
93010922SJeff.Bonwick@Sun.COM static void
93110922SJeff.Bonwick@Sun.COM ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
93210922SJeff.Bonwick@Sun.COM {
93310922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
93410922SJeff.Bonwick@Sun.COM 
93510922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
93610922SJeff.Bonwick@Sun.COM }
93710922SJeff.Bonwick@Sun.COM 
93810922SJeff.Bonwick@Sun.COM static rl_t *
93910922SJeff.Bonwick@Sun.COM ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
94010922SJeff.Bonwick@Sun.COM     uint64_t size, rl_type_t type)
94110922SJeff.Bonwick@Sun.COM {
94210922SJeff.Bonwick@Sun.COM 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
94310922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
94410922SJeff.Bonwick@Sun.COM 	rl_t *rl;
94510922SJeff.Bonwick@Sun.COM 
94610922SJeff.Bonwick@Sun.COM 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
94710922SJeff.Bonwick@Sun.COM 	rl->rl_object = object;
94810922SJeff.Bonwick@Sun.COM 	rl->rl_offset = offset;
94910922SJeff.Bonwick@Sun.COM 	rl->rl_size = size;
95010922SJeff.Bonwick@Sun.COM 	rl->rl_lock = rll;
95110922SJeff.Bonwick@Sun.COM 
95210922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
95310922SJeff.Bonwick@Sun.COM 
95410922SJeff.Bonwick@Sun.COM 	return (rl);
95510922SJeff.Bonwick@Sun.COM }
95610922SJeff.Bonwick@Sun.COM 
95710922SJeff.Bonwick@Sun.COM static void
95810922SJeff.Bonwick@Sun.COM ztest_range_unlock(rl_t *rl)
95910922SJeff.Bonwick@Sun.COM {
96010922SJeff.Bonwick@Sun.COM 	rll_t *rll = rl->rl_lock;
96110922SJeff.Bonwick@Sun.COM 
96210922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
96310922SJeff.Bonwick@Sun.COM 
96410922SJeff.Bonwick@Sun.COM 	umem_free(rl, sizeof (*rl));
96510922SJeff.Bonwick@Sun.COM }
96610922SJeff.Bonwick@Sun.COM 
96710922SJeff.Bonwick@Sun.COM static void
96810922SJeff.Bonwick@Sun.COM ztest_zd_init(ztest_ds_t *zd, objset_t *os)
96910922SJeff.Bonwick@Sun.COM {
97010922SJeff.Bonwick@Sun.COM 	zd->zd_os = os;
97110922SJeff.Bonwick@Sun.COM 	zd->zd_zilog = dmu_objset_zil(os);
97210922SJeff.Bonwick@Sun.COM 	zd->zd_seq = 0;
97310922SJeff.Bonwick@Sun.COM 	dmu_objset_name(os, zd->zd_name);
97410922SJeff.Bonwick@Sun.COM 
97510922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
97610922SJeff.Bonwick@Sun.COM 
97710922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
97810922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_object_lock[l]);
97910922SJeff.Bonwick@Sun.COM 
98010922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
98110922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_range_lock[l]);
98210922SJeff.Bonwick@Sun.COM }
98310922SJeff.Bonwick@Sun.COM 
98410922SJeff.Bonwick@Sun.COM static void
98510922SJeff.Bonwick@Sun.COM ztest_zd_fini(ztest_ds_t *zd)
98610922SJeff.Bonwick@Sun.COM {
98710922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
98810922SJeff.Bonwick@Sun.COM 
98910922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
99010922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_object_lock[l]);
99110922SJeff.Bonwick@Sun.COM 
99210922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
99310922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_range_lock[l]);
99410922SJeff.Bonwick@Sun.COM }
99510922SJeff.Bonwick@Sun.COM 
99610922SJeff.Bonwick@Sun.COM #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
99710922SJeff.Bonwick@Sun.COM 
99810922SJeff.Bonwick@Sun.COM static uint64_t
99910922SJeff.Bonwick@Sun.COM ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1000789Sahrens {
100110922SJeff.Bonwick@Sun.COM 	uint64_t txg;
100210922SJeff.Bonwick@Sun.COM 	int error;
100310922SJeff.Bonwick@Sun.COM 
100410922SJeff.Bonwick@Sun.COM 	/*
100510922SJeff.Bonwick@Sun.COM 	 * Attempt to assign tx to some transaction group.
100610922SJeff.Bonwick@Sun.COM 	 */
100710922SJeff.Bonwick@Sun.COM 	error = dmu_tx_assign(tx, txg_how);
100810922SJeff.Bonwick@Sun.COM 	if (error) {
100910922SJeff.Bonwick@Sun.COM 		if (error == ERESTART) {
101010922SJeff.Bonwick@Sun.COM 			ASSERT(txg_how == TXG_NOWAIT);
101110922SJeff.Bonwick@Sun.COM 			dmu_tx_wait(tx);
101210922SJeff.Bonwick@Sun.COM 		} else {
101310922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
101410922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(tag);
101510922SJeff.Bonwick@Sun.COM 		}
101610922SJeff.Bonwick@Sun.COM 		dmu_tx_abort(tx);
101710922SJeff.Bonwick@Sun.COM 		return (0);
101810922SJeff.Bonwick@Sun.COM 	}
101910922SJeff.Bonwick@Sun.COM 	txg = dmu_tx_get_txg(tx);
102010922SJeff.Bonwick@Sun.COM 	ASSERT(txg != 0);
102110922SJeff.Bonwick@Sun.COM 	return (txg);
102210922SJeff.Bonwick@Sun.COM }
102310922SJeff.Bonwick@Sun.COM 
102410922SJeff.Bonwick@Sun.COM static void
102510922SJeff.Bonwick@Sun.COM ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
102610922SJeff.Bonwick@Sun.COM {
102710922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
102810922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
102910922SJeff.Bonwick@Sun.COM 
103010922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
103110922SJeff.Bonwick@Sun.COM 		*ip++ = value;
103210922SJeff.Bonwick@Sun.COM }
103310922SJeff.Bonwick@Sun.COM 
103410922SJeff.Bonwick@Sun.COM static boolean_t
103510922SJeff.Bonwick@Sun.COM ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
103610922SJeff.Bonwick@Sun.COM {
103710922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
103810922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
103910922SJeff.Bonwick@Sun.COM 	uint64_t diff = 0;
104010922SJeff.Bonwick@Sun.COM 
104110922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
104210922SJeff.Bonwick@Sun.COM 		diff |= (value - *ip++);
104310922SJeff.Bonwick@Sun.COM 
104410922SJeff.Bonwick@Sun.COM 	return (diff == 0);
104510922SJeff.Bonwick@Sun.COM }
104610922SJeff.Bonwick@Sun.COM 
104710922SJeff.Bonwick@Sun.COM static void
104810922SJeff.Bonwick@Sun.COM ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
104910922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
105010922SJeff.Bonwick@Sun.COM {
105110922SJeff.Bonwick@Sun.COM 	bt->bt_magic = BT_MAGIC;
105210922SJeff.Bonwick@Sun.COM 	bt->bt_objset = dmu_objset_id(os);
105310922SJeff.Bonwick@Sun.COM 	bt->bt_object = object;
105410922SJeff.Bonwick@Sun.COM 	bt->bt_offset = offset;
105510922SJeff.Bonwick@Sun.COM 	bt->bt_gen = gen;
105610922SJeff.Bonwick@Sun.COM 	bt->bt_txg = txg;
105710922SJeff.Bonwick@Sun.COM 	bt->bt_crtxg = crtxg;
105810922SJeff.Bonwick@Sun.COM }
105910922SJeff.Bonwick@Sun.COM 
106010922SJeff.Bonwick@Sun.COM static void
106110922SJeff.Bonwick@Sun.COM ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
106210922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
106310922SJeff.Bonwick@Sun.COM {
106410922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_magic == BT_MAGIC);
106510922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_objset == dmu_objset_id(os));
106610922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_object == object);
106710922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_offset == offset);
106810922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_gen <= gen);
106910922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_txg <= txg);
107010922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_crtxg == crtxg);
107110922SJeff.Bonwick@Sun.COM }
107210922SJeff.Bonwick@Sun.COM 
107310922SJeff.Bonwick@Sun.COM static ztest_block_tag_t *
107410922SJeff.Bonwick@Sun.COM ztest_bt_bonus(dmu_buf_t *db)
107510922SJeff.Bonwick@Sun.COM {
107610922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
107710922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt;
107810922SJeff.Bonwick@Sun.COM 
107910922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
108010922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
108110922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
108210922SJeff.Bonwick@Sun.COM 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
108310922SJeff.Bonwick@Sun.COM 
108410922SJeff.Bonwick@Sun.COM 	return (bt);
108510922SJeff.Bonwick@Sun.COM }
108610922SJeff.Bonwick@Sun.COM 
108710922SJeff.Bonwick@Sun.COM /*
108810922SJeff.Bonwick@Sun.COM  * ZIL logging ops
108910922SJeff.Bonwick@Sun.COM  */
109010922SJeff.Bonwick@Sun.COM 
109110922SJeff.Bonwick@Sun.COM #define	lrz_type	lr_mode
109210922SJeff.Bonwick@Sun.COM #define	lrz_blocksize	lr_uid
109310922SJeff.Bonwick@Sun.COM #define	lrz_ibshift	lr_gid
109410922SJeff.Bonwick@Sun.COM #define	lrz_bonustype	lr_rdev
109510922SJeff.Bonwick@Sun.COM #define	lrz_bonuslen	lr_crtime[1]
109610922SJeff.Bonwick@Sun.COM 
109710922SJeff.Bonwick@Sun.COM static uint64_t
109810922SJeff.Bonwick@Sun.COM ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
109910922SJeff.Bonwick@Sun.COM {
110010922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
110110922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
110210922SJeff.Bonwick@Sun.COM 	itx_t *itx;
110310922SJeff.Bonwick@Sun.COM 
110410922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
110510922SJeff.Bonwick@Sun.COM 		return (0);
110610922SJeff.Bonwick@Sun.COM 
110710922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
110810922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
110910922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
111010922SJeff.Bonwick@Sun.COM 
111110922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
111210922SJeff.Bonwick@Sun.COM }
111310922SJeff.Bonwick@Sun.COM 
111410922SJeff.Bonwick@Sun.COM static uint64_t
111510922SJeff.Bonwick@Sun.COM ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr)
111610922SJeff.Bonwick@Sun.COM {
111710922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
111810922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
111910922SJeff.Bonwick@Sun.COM 	itx_t *itx;
112010922SJeff.Bonwick@Sun.COM 
112110922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
112210922SJeff.Bonwick@Sun.COM 		return (0);
112310922SJeff.Bonwick@Sun.COM 
112410922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
112510922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
112610922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
112710922SJeff.Bonwick@Sun.COM 
112810922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
112910922SJeff.Bonwick@Sun.COM }
113010922SJeff.Bonwick@Sun.COM 
113110922SJeff.Bonwick@Sun.COM static uint64_t
113210922SJeff.Bonwick@Sun.COM ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
113310922SJeff.Bonwick@Sun.COM {
113410922SJeff.Bonwick@Sun.COM 	itx_t *itx;
113510922SJeff.Bonwick@Sun.COM 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
113610922SJeff.Bonwick@Sun.COM 
113710922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
113810922SJeff.Bonwick@Sun.COM 		return (0);
113910922SJeff.Bonwick@Sun.COM 
114010922SJeff.Bonwick@Sun.COM 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
114110922SJeff.Bonwick@Sun.COM 		write_state = WR_INDIRECT;
114210922SJeff.Bonwick@Sun.COM 
114310922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_WRITE,
114410922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
114510922SJeff.Bonwick@Sun.COM 
114610922SJeff.Bonwick@Sun.COM 	if (write_state == WR_COPIED &&
114710922SJeff.Bonwick@Sun.COM 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
114810922SJeff.Bonwick@Sun.COM 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
114910922SJeff.Bonwick@Sun.COM 		zil_itx_destroy(itx);
115010922SJeff.Bonwick@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
115110922SJeff.Bonwick@Sun.COM 		write_state = WR_NEED_COPY;
115210922SJeff.Bonwick@Sun.COM 	}
115310922SJeff.Bonwick@Sun.COM 	itx->itx_private = zd;
115410922SJeff.Bonwick@Sun.COM 	itx->itx_wr_state = write_state;
115510922SJeff.Bonwick@Sun.COM 	itx->itx_sync = (ztest_random(8) == 0);
115610922SJeff.Bonwick@Sun.COM 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
115710922SJeff.Bonwick@Sun.COM 
115810922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
115910922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
116010922SJeff.Bonwick@Sun.COM 
116110922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
116210922SJeff.Bonwick@Sun.COM }
116310922SJeff.Bonwick@Sun.COM 
116410922SJeff.Bonwick@Sun.COM static uint64_t
116510922SJeff.Bonwick@Sun.COM ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
116610922SJeff.Bonwick@Sun.COM {
116710922SJeff.Bonwick@Sun.COM 	itx_t *itx;
116810922SJeff.Bonwick@Sun.COM 
116910922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
117010922SJeff.Bonwick@Sun.COM 		return (0);
117110922SJeff.Bonwick@Sun.COM 
117210922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
117310922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
117410922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
117510922SJeff.Bonwick@Sun.COM 
117610922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
117710922SJeff.Bonwick@Sun.COM }
117810922SJeff.Bonwick@Sun.COM 
117910922SJeff.Bonwick@Sun.COM static uint64_t
118010922SJeff.Bonwick@Sun.COM ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
118110922SJeff.Bonwick@Sun.COM {
118210922SJeff.Bonwick@Sun.COM 	itx_t *itx;
118310922SJeff.Bonwick@Sun.COM 
118410922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
118510922SJeff.Bonwick@Sun.COM 		return (0);
118610922SJeff.Bonwick@Sun.COM 
118710922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
118810922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
118910922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
119010922SJeff.Bonwick@Sun.COM 
119110922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
119210922SJeff.Bonwick@Sun.COM }
119310922SJeff.Bonwick@Sun.COM 
119410922SJeff.Bonwick@Sun.COM /*
119510922SJeff.Bonwick@Sun.COM  * ZIL replay ops
119610922SJeff.Bonwick@Sun.COM  */
119710922SJeff.Bonwick@Sun.COM static int
119810922SJeff.Bonwick@Sun.COM ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
119910922SJeff.Bonwick@Sun.COM {
120010922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
120110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
120210922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
120310922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1204789Sahrens 	dmu_tx_t *tx;
120510922SJeff.Bonwick@Sun.COM 	uint64_t txg;
120610922SJeff.Bonwick@Sun.COM 	int error = 0;
1207789Sahrens 
1208789Sahrens 	if (byteswap)
1209789Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
1210789Sahrens 
121110922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
121210922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
121310922SJeff.Bonwick@Sun.COM 
1214789Sahrens 	tx = dmu_tx_create(os);
121510922SJeff.Bonwick@Sun.COM 
121610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
121710922SJeff.Bonwick@Sun.COM 
121810922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
121910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
122010922SJeff.Bonwick@Sun.COM 	} else {
122110922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
122210922SJeff.Bonwick@Sun.COM 	}
122310922SJeff.Bonwick@Sun.COM 
122410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
122510922SJeff.Bonwick@Sun.COM 	if (txg == 0)
122610922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
122710922SJeff.Bonwick@Sun.COM 
122810922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
122910922SJeff.Bonwick@Sun.COM 
123010922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
123110922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
123210922SJeff.Bonwick@Sun.COM 			lr->lr_foid = zap_create(os,
123310922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
123410922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
123510922SJeff.Bonwick@Sun.COM 		} else {
123610922SJeff.Bonwick@Sun.COM 			error = zap_create_claim(os, lr->lr_foid,
123710922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
123810922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
123910922SJeff.Bonwick@Sun.COM 		}
124010922SJeff.Bonwick@Sun.COM 	} else {
124110922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
124210922SJeff.Bonwick@Sun.COM 			lr->lr_foid = dmu_object_alloc(os,
124310922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
124410922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124510922SJeff.Bonwick@Sun.COM 		} else {
124610922SJeff.Bonwick@Sun.COM 			error = dmu_object_claim(os, lr->lr_foid,
124710922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
124810922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124910922SJeff.Bonwick@Sun.COM 		}
125010922SJeff.Bonwick@Sun.COM 	}
125110922SJeff.Bonwick@Sun.COM 
1252789Sahrens 	if (error) {
125310922SJeff.Bonwick@Sun.COM 		ASSERT3U(error, ==, EEXIST);
125410922SJeff.Bonwick@Sun.COM 		ASSERT(zd->zd_zilog->zl_replay);
125510922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1256789Sahrens 		return (error);
1257789Sahrens 	}
1258789Sahrens 
125910922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_foid != 0);
126010922SJeff.Bonwick@Sun.COM 
126110922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
126210922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
126310922SJeff.Bonwick@Sun.COM 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
126410922SJeff.Bonwick@Sun.COM 
126510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
126610922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
126710922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
126810922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
126910922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
127010922SJeff.Bonwick@Sun.COM 
127110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
127210922SJeff.Bonwick@Sun.COM 	    &lr->lr_foid, tx));
127310922SJeff.Bonwick@Sun.COM 
127410922SJeff.Bonwick@Sun.COM 	(void) ztest_log_create(zd, tx, lr);
127510922SJeff.Bonwick@Sun.COM 
127610922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
127710922SJeff.Bonwick@Sun.COM 
127810922SJeff.Bonwick@Sun.COM 	return (0);
127910922SJeff.Bonwick@Sun.COM }
128010922SJeff.Bonwick@Sun.COM 
128110922SJeff.Bonwick@Sun.COM static int
128210922SJeff.Bonwick@Sun.COM ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
128310922SJeff.Bonwick@Sun.COM {
128410922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
128510922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
128610922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
128710922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
128810922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
128910922SJeff.Bonwick@Sun.COM 
129010922SJeff.Bonwick@Sun.COM 	if (byteswap)
129110922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
129210922SJeff.Bonwick@Sun.COM 
129310922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
129410922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
129510922SJeff.Bonwick@Sun.COM 
129610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
129710922SJeff.Bonwick@Sun.COM 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
129810922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
129910922SJeff.Bonwick@Sun.COM 
130010922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_WRITER);
130110922SJeff.Bonwick@Sun.COM 
130210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
130310922SJeff.Bonwick@Sun.COM 
130410922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
130510922SJeff.Bonwick@Sun.COM 
130610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
130710922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
130810922SJeff.Bonwick@Sun.COM 
130910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
131010922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
131110922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
131210922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
131310922SJeff.Bonwick@Sun.COM 	}
131410922SJeff.Bonwick@Sun.COM 
131510922SJeff.Bonwick@Sun.COM 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
131610922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
131710922SJeff.Bonwick@Sun.COM 	} else {
131810922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
131910922SJeff.Bonwick@Sun.COM 	}
132010922SJeff.Bonwick@Sun.COM 
132110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
132210922SJeff.Bonwick@Sun.COM 
132310922SJeff.Bonwick@Sun.COM 	(void) ztest_log_remove(zd, tx, lr);
132410922SJeff.Bonwick@Sun.COM 
1325789Sahrens 	dmu_tx_commit(tx);
1326789Sahrens 
132710922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
132810922SJeff.Bonwick@Sun.COM 
132910922SJeff.Bonwick@Sun.COM 	return (0);
133010922SJeff.Bonwick@Sun.COM }
133110922SJeff.Bonwick@Sun.COM 
133210922SJeff.Bonwick@Sun.COM static int
133310922SJeff.Bonwick@Sun.COM ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
133410922SJeff.Bonwick@Sun.COM {
133510922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
133610922SJeff.Bonwick@Sun.COM 	void *data = lr + 1;			/* data follows lr */
133710922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
133810922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt = data;
133910922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
134010922SJeff.Bonwick@Sun.COM 	uint64_t gen, txg, lrtxg, crtxg;
134110922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
134210922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
134310922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
134410922SJeff.Bonwick@Sun.COM 	arc_buf_t *abuf = NULL;
134510922SJeff.Bonwick@Sun.COM 	rl_t *rl;
134610922SJeff.Bonwick@Sun.COM 
134710922SJeff.Bonwick@Sun.COM 	if (byteswap)
134810922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
134910922SJeff.Bonwick@Sun.COM 
135010922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
135110922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
135210922SJeff.Bonwick@Sun.COM 
135310922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
135410922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
135510922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
135610922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
135710922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
135810922SJeff.Bonwick@Sun.COM 			length = blocksize;
135910922SJeff.Bonwick@Sun.COM 		}
136010922SJeff.Bonwick@Sun.COM 	}
136110922SJeff.Bonwick@Sun.COM 
136210922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
136310922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(bt, sizeof (*bt));
136410922SJeff.Bonwick@Sun.COM 
136510922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic != BT_MAGIC)
136610922SJeff.Bonwick@Sun.COM 		bt = NULL;
136710922SJeff.Bonwick@Sun.COM 
136810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
136910922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
137010922SJeff.Bonwick@Sun.COM 
137110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
137210922SJeff.Bonwick@Sun.COM 
137310922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
137410922SJeff.Bonwick@Sun.COM 
137510922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
137610922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
137710922SJeff.Bonwick@Sun.COM 	gen = bbt->bt_gen;
137810922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
137910922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
138010922SJeff.Bonwick@Sun.COM 
138110922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
138210922SJeff.Bonwick@Sun.COM 
138310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
138410922SJeff.Bonwick@Sun.COM 
138510922SJeff.Bonwick@Sun.COM 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
138610922SJeff.Bonwick@Sun.COM 	    P2PHASE(offset, length) == 0)
138710922SJeff.Bonwick@Sun.COM 		abuf = dmu_request_arcbuf(db, length);
138810922SJeff.Bonwick@Sun.COM 
138910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
139010922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
139110922SJeff.Bonwick@Sun.COM 		if (abuf != NULL)
139210922SJeff.Bonwick@Sun.COM 			dmu_return_arcbuf(abuf);
139310922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
139410922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
139510922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
139610922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
139710922SJeff.Bonwick@Sun.COM 	}
139810922SJeff.Bonwick@Sun.COM 
139910922SJeff.Bonwick@Sun.COM 	if (bt != NULL) {
140010922SJeff.Bonwick@Sun.COM 		/*
140110922SJeff.Bonwick@Sun.COM 		 * Usually, verify the old data before writing new data --
140210922SJeff.Bonwick@Sun.COM 		 * but not always, because we also want to verify correct
140310922SJeff.Bonwick@Sun.COM 		 * behavior when the data was not recently read into cache.
140410922SJeff.Bonwick@Sun.COM 		 */
140510922SJeff.Bonwick@Sun.COM 		ASSERT(offset % doi.doi_data_block_size == 0);
140610922SJeff.Bonwick@Sun.COM 		if (ztest_random(4) != 0) {
140710922SJeff.Bonwick@Sun.COM 			int prefetch = ztest_random(2) ?
140810922SJeff.Bonwick@Sun.COM 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
140910922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t rbt;
141010922SJeff.Bonwick@Sun.COM 
141110922SJeff.Bonwick@Sun.COM 			VERIFY(dmu_read(os, lr->lr_foid, offset,
141210922SJeff.Bonwick@Sun.COM 			    sizeof (rbt), &rbt, prefetch) == 0);
141310922SJeff.Bonwick@Sun.COM 			if (rbt.bt_magic == BT_MAGIC) {
141410922SJeff.Bonwick@Sun.COM 				ztest_bt_verify(&rbt, os, lr->lr_foid,
141510922SJeff.Bonwick@Sun.COM 				    offset, gen, txg, crtxg);
141610922SJeff.Bonwick@Sun.COM 			}
141710922SJeff.Bonwick@Sun.COM 		}
141810922SJeff.Bonwick@Sun.COM 
141910922SJeff.Bonwick@Sun.COM 		/*
142010922SJeff.Bonwick@Sun.COM 		 * Writes can appear to be newer than the bonus buffer because
142110922SJeff.Bonwick@Sun.COM 		 * the ztest_get_data() callback does a dmu_read() of the
142210922SJeff.Bonwick@Sun.COM 		 * open-context data, which may be different than the data
142310922SJeff.Bonwick@Sun.COM 		 * as it was when the write was generated.
142410922SJeff.Bonwick@Sun.COM 		 */
142510922SJeff.Bonwick@Sun.COM 		if (zd->zd_zilog->zl_replay) {
142610922SJeff.Bonwick@Sun.COM 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
142710922SJeff.Bonwick@Sun.COM 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
142810922SJeff.Bonwick@Sun.COM 			    bt->bt_crtxg);
142910922SJeff.Bonwick@Sun.COM 		}
143010922SJeff.Bonwick@Sun.COM 
143110922SJeff.Bonwick@Sun.COM 		/*
143210922SJeff.Bonwick@Sun.COM 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
143310922SJeff.Bonwick@Sun.COM 		 * so that all of the usual ASSERTs will work.
143410922SJeff.Bonwick@Sun.COM 		 */
143510922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
143610922SJeff.Bonwick@Sun.COM 	}
143710922SJeff.Bonwick@Sun.COM 
143810922SJeff.Bonwick@Sun.COM 	if (abuf == NULL) {
143910922SJeff.Bonwick@Sun.COM 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
144010922SJeff.Bonwick@Sun.COM 	} else {
144110922SJeff.Bonwick@Sun.COM 		bcopy(data, abuf->b_data, length);
144210922SJeff.Bonwick@Sun.COM 		dmu_assign_arcbuf(db, offset, abuf, tx);
144310922SJeff.Bonwick@Sun.COM 	}
144410922SJeff.Bonwick@Sun.COM 
144510922SJeff.Bonwick@Sun.COM 	(void) ztest_log_write(zd, tx, lr);
144610922SJeff.Bonwick@Sun.COM 
144710922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
144810922SJeff.Bonwick@Sun.COM 
144910922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
145010922SJeff.Bonwick@Sun.COM 
145110922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
145210922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
145310922SJeff.Bonwick@Sun.COM 
145410922SJeff.Bonwick@Sun.COM 	return (0);
145510922SJeff.Bonwick@Sun.COM }
145610922SJeff.Bonwick@Sun.COM 
145710922SJeff.Bonwick@Sun.COM static int
145810922SJeff.Bonwick@Sun.COM ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
145910922SJeff.Bonwick@Sun.COM {
146010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
146110922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
146210922SJeff.Bonwick@Sun.COM 	uint64_t txg;
146310922SJeff.Bonwick@Sun.COM 	rl_t *rl;
146410922SJeff.Bonwick@Sun.COM 
146510922SJeff.Bonwick@Sun.COM 	if (byteswap)
146610922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
146710922SJeff.Bonwick@Sun.COM 
146810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
146910922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
147010922SJeff.Bonwick@Sun.COM 	    RL_WRITER);
147110922SJeff.Bonwick@Sun.COM 
147210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
147310922SJeff.Bonwick@Sun.COM 
147410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
147510922SJeff.Bonwick@Sun.COM 
147610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
147710922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
147810922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
147910922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
148010922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
148110922SJeff.Bonwick@Sun.COM 	}
148210922SJeff.Bonwick@Sun.COM 
148310922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
148410922SJeff.Bonwick@Sun.COM 	    lr->lr_length, tx) == 0);
148510922SJeff.Bonwick@Sun.COM 
148610922SJeff.Bonwick@Sun.COM 	(void) ztest_log_truncate(zd, tx, lr);
148710922SJeff.Bonwick@Sun.COM 
148810922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
148910922SJeff.Bonwick@Sun.COM 
149010922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
149110922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
149210922SJeff.Bonwick@Sun.COM 
149310922SJeff.Bonwick@Sun.COM 	return (0);
149410922SJeff.Bonwick@Sun.COM }
149510922SJeff.Bonwick@Sun.COM 
149610922SJeff.Bonwick@Sun.COM static int
149710922SJeff.Bonwick@Sun.COM ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
149810922SJeff.Bonwick@Sun.COM {
149910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
150010922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
150110922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
150210922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
150310922SJeff.Bonwick@Sun.COM 	uint64_t txg, lrtxg, crtxg;
150410922SJeff.Bonwick@Sun.COM 
150510922SJeff.Bonwick@Sun.COM 	if (byteswap)
150610922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
150710922SJeff.Bonwick@Sun.COM 
150810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
150910922SJeff.Bonwick@Sun.COM 
151010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
151110922SJeff.Bonwick@Sun.COM 
151210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
151310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_bonus(tx, lr->lr_foid);
151410922SJeff.Bonwick@Sun.COM 
151510922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
151610922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
151710922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
151810922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
151910922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
152010922SJeff.Bonwick@Sun.COM 	}
152110922SJeff.Bonwick@Sun.COM 
152210922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
152310922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
152410922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
152510922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
152610922SJeff.Bonwick@Sun.COM 
152710922SJeff.Bonwick@Sun.COM 	if (zd->zd_zilog->zl_replay) {
152810922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_size != 0);
152910922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_mode != 0);
153010922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg != 0);
153110922SJeff.Bonwick@Sun.COM 	} else {
153210922SJeff.Bonwick@Sun.COM 		/*
153310922SJeff.Bonwick@Sun.COM 		 * Randomly change the size and increment the generation.
153410922SJeff.Bonwick@Sun.COM 		 */
153510922SJeff.Bonwick@Sun.COM 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
153610922SJeff.Bonwick@Sun.COM 		    sizeof (*bbt);
153710922SJeff.Bonwick@Sun.COM 		lr->lr_mode = bbt->bt_gen + 1;
153810922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg == 0);
153910922SJeff.Bonwick@Sun.COM 	}
154010922SJeff.Bonwick@Sun.COM 
154110922SJeff.Bonwick@Sun.COM 	/*
154210922SJeff.Bonwick@Sun.COM 	 * Verify that the current bonus buffer is not newer than our txg.
154310922SJeff.Bonwick@Sun.COM 	 */
154410922SJeff.Bonwick@Sun.COM 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
154510922SJeff.Bonwick@Sun.COM 	    MAX(txg, lrtxg), crtxg);
154610922SJeff.Bonwick@Sun.COM 
154710922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
154810922SJeff.Bonwick@Sun.COM 
154910922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
155010922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, <=, db->db_size);
155110922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
155210922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
155310922SJeff.Bonwick@Sun.COM 
155410922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
155510922SJeff.Bonwick@Sun.COM 
155610922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
155710922SJeff.Bonwick@Sun.COM 
155810922SJeff.Bonwick@Sun.COM 	(void) ztest_log_setattr(zd, tx, lr);
155910922SJeff.Bonwick@Sun.COM 
156010922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
156110922SJeff.Bonwick@Sun.COM 
156210922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
156310922SJeff.Bonwick@Sun.COM 
156410922SJeff.Bonwick@Sun.COM 	return (0);
1565789Sahrens }
1566789Sahrens 
1567789Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1568789Sahrens 	NULL,			/* 0 no such transaction type */
1569789Sahrens 	ztest_replay_create,	/* TX_CREATE */
1570789Sahrens 	NULL,			/* TX_MKDIR */
1571789Sahrens 	NULL,			/* TX_MKXATTR */
1572789Sahrens 	NULL,			/* TX_SYMLINK */
1573789Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
1574789Sahrens 	NULL,			/* TX_RMDIR */
1575789Sahrens 	NULL,			/* TX_LINK */
1576789Sahrens 	NULL,			/* TX_RENAME */
157710922SJeff.Bonwick@Sun.COM 	ztest_replay_write,	/* TX_WRITE */
157810922SJeff.Bonwick@Sun.COM 	ztest_replay_truncate,	/* TX_TRUNCATE */
157910922SJeff.Bonwick@Sun.COM 	ztest_replay_setattr,	/* TX_SETATTR */
1580789Sahrens 	NULL,			/* TX_ACL */
158110800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL */
158210800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ATTR */
158310800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL_ATTR */
158410800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL */
158510800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ATTR */
158610800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL_ATTR */
158710800SNeil.Perrin@Sun.COM 	NULL,			/* TX_WRITE2 */
1588789Sahrens };
1589789Sahrens 
1590789Sahrens /*
159110922SJeff.Bonwick@Sun.COM  * ZIL get_data callbacks
159210922SJeff.Bonwick@Sun.COM  */
159310922SJeff.Bonwick@Sun.COM 
159410922SJeff.Bonwick@Sun.COM static void
159510922SJeff.Bonwick@Sun.COM ztest_get_done(zgd_t *zgd, int error)
159610922SJeff.Bonwick@Sun.COM {
159710922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = zgd->zgd_private;
159810922SJeff.Bonwick@Sun.COM 	uint64_t object = zgd->zgd_rl->rl_object;
159910922SJeff.Bonwick@Sun.COM 
160010922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
160110922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
160210922SJeff.Bonwick@Sun.COM 
160310922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(zgd->zgd_rl);
160410922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
160510922SJeff.Bonwick@Sun.COM 
160610922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
160710922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
160810922SJeff.Bonwick@Sun.COM 
160910922SJeff.Bonwick@Sun.COM 	umem_free(zgd, sizeof (*zgd));
161010922SJeff.Bonwick@Sun.COM }
161110922SJeff.Bonwick@Sun.COM 
161210922SJeff.Bonwick@Sun.COM static int
161310922SJeff.Bonwick@Sun.COM ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
161410922SJeff.Bonwick@Sun.COM {
161510922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = arg;
161610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
161710922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
161810922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
161910922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
162010922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
162110922SJeff.Bonwick@Sun.COM 	uint64_t txg = lr->lr_common.lrc_txg;
162210922SJeff.Bonwick@Sun.COM 	uint64_t crtxg;
162310922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
162410922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
162510922SJeff.Bonwick@Sun.COM 	zgd_t *zgd;
162610922SJeff.Bonwick@Sun.COM 	int error;
162710922SJeff.Bonwick@Sun.COM 
162810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
162910922SJeff.Bonwick@Sun.COM 	error = dmu_bonus_hold(os, object, FTAG, &db);
163010922SJeff.Bonwick@Sun.COM 	if (error) {
163110922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
163210922SJeff.Bonwick@Sun.COM 		return (error);
163310922SJeff.Bonwick@Sun.COM 	}
163410922SJeff.Bonwick@Sun.COM 
163510922SJeff.Bonwick@Sun.COM 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
163610922SJeff.Bonwick@Sun.COM 
163710922SJeff.Bonwick@Sun.COM 	if (crtxg == 0 || crtxg > txg) {
163810922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
163910922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
164010922SJeff.Bonwick@Sun.COM 		return (ENOENT);
164110922SJeff.Bonwick@Sun.COM 	}
164210922SJeff.Bonwick@Sun.COM 
164310922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
164410922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
164510922SJeff.Bonwick@Sun.COM 	db = NULL;
164610922SJeff.Bonwick@Sun.COM 
164710922SJeff.Bonwick@Sun.COM 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
164810922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zd->zd_zilog;
164910922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zd;
165010922SJeff.Bonwick@Sun.COM 
165110922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
165210922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
165310922SJeff.Bonwick@Sun.COM 		    RL_READER);
165410922SJeff.Bonwick@Sun.COM 
165510922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
165610922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
165710922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0);
165810922SJeff.Bonwick@Sun.COM 	} else {
165910922SJeff.Bonwick@Sun.COM 		size = doi.doi_data_block_size;
166010945SJeff.Bonwick@Sun.COM 		if (ISP2(size)) {
166110922SJeff.Bonwick@Sun.COM 			offset = P2ALIGN(offset, size);
166210945SJeff.Bonwick@Sun.COM 		} else {
166310945SJeff.Bonwick@Sun.COM 			ASSERT(offset < size);
166410945SJeff.Bonwick@Sun.COM 			offset = 0;
166510945SJeff.Bonwick@Sun.COM 		}
166610922SJeff.Bonwick@Sun.COM 
166710922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
166810922SJeff.Bonwick@Sun.COM 		    RL_READER);
166910922SJeff.Bonwick@Sun.COM 
167010922SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db);
167110922SJeff.Bonwick@Sun.COM 
167210922SJeff.Bonwick@Sun.COM 		if (error == 0) {
167310922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
167410922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
167510922SJeff.Bonwick@Sun.COM 
167610922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
167710922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
167810922SJeff.Bonwick@Sun.COM 
167910922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
168010922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
168110922SJeff.Bonwick@Sun.COM 
168210922SJeff.Bonwick@Sun.COM 			if (error == 0)
168310922SJeff.Bonwick@Sun.COM 				return (0);
168410922SJeff.Bonwick@Sun.COM 		}
168510922SJeff.Bonwick@Sun.COM 	}
168610922SJeff.Bonwick@Sun.COM 
168710922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
168810922SJeff.Bonwick@Sun.COM 
168910922SJeff.Bonwick@Sun.COM 	return (error);
169010922SJeff.Bonwick@Sun.COM }
169110922SJeff.Bonwick@Sun.COM 
169210922SJeff.Bonwick@Sun.COM static void *
169310922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
169410922SJeff.Bonwick@Sun.COM {
169510922SJeff.Bonwick@Sun.COM 	char *lr;
169610922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
169710922SJeff.Bonwick@Sun.COM 
169810922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
169910922SJeff.Bonwick@Sun.COM 
170010922SJeff.Bonwick@Sun.COM 	if (name)
170110922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
170210922SJeff.Bonwick@Sun.COM 
170310922SJeff.Bonwick@Sun.COM 	return (lr);
170410922SJeff.Bonwick@Sun.COM }
170510922SJeff.Bonwick@Sun.COM 
170610922SJeff.Bonwick@Sun.COM void
170710922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
170810922SJeff.Bonwick@Sun.COM {
170910922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
171010922SJeff.Bonwick@Sun.COM 
171110922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
171210922SJeff.Bonwick@Sun.COM }
171310922SJeff.Bonwick@Sun.COM 
171410922SJeff.Bonwick@Sun.COM /*
171510922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
171610922SJeff.Bonwick@Sun.COM  */
171710922SJeff.Bonwick@Sun.COM static int
171810922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
171910922SJeff.Bonwick@Sun.COM {
172010922SJeff.Bonwick@Sun.COM 	int missing = 0;
172110922SJeff.Bonwick@Sun.COM 	int error;
172210922SJeff.Bonwick@Sun.COM 
172310922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
172410922SJeff.Bonwick@Sun.COM 
172510922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
172610922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
172710922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
172810922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
172910922SJeff.Bonwick@Sun.COM 		if (error) {
173010922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
173110922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
173210922SJeff.Bonwick@Sun.COM 			missing++;
173310922SJeff.Bonwick@Sun.COM 		} else {
173410922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
173510922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
173610922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
173710922SJeff.Bonwick@Sun.COM 
173810922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
173910922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
174010922SJeff.Bonwick@Sun.COM 
174110922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
174210922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
174310922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
174410922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
174510922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
174610922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
174710922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
174810922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
174910922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
175010922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
175110922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
175210922SJeff.Bonwick@Sun.COM 		}
175310922SJeff.Bonwick@Sun.COM 	}
175410922SJeff.Bonwick@Sun.COM 
175510922SJeff.Bonwick@Sun.COM 	return (missing);
175610922SJeff.Bonwick@Sun.COM }
175710922SJeff.Bonwick@Sun.COM 
175810922SJeff.Bonwick@Sun.COM static int
175910922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
176010922SJeff.Bonwick@Sun.COM {
176110922SJeff.Bonwick@Sun.COM 	int missing = 0;
176210922SJeff.Bonwick@Sun.COM 
176310922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
176410922SJeff.Bonwick@Sun.COM 
176510922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
176610922SJeff.Bonwick@Sun.COM 		if (missing) {
176710922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
176810922SJeff.Bonwick@Sun.COM 			missing++;
176910922SJeff.Bonwick@Sun.COM 			continue;
177010922SJeff.Bonwick@Sun.COM 		}
177110922SJeff.Bonwick@Sun.COM 
177210922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
177310922SJeff.Bonwick@Sun.COM 
177410922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
177510922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
177610922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
177710922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
177810922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
177910922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
178010922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
178110922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
178210922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
178310922SJeff.Bonwick@Sun.COM 
178410922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
178510922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
178610922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
178710922SJeff.Bonwick@Sun.COM 			missing++;
178810922SJeff.Bonwick@Sun.COM 		} else {
178910922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
179010922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
179110922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
179210922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
179310922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
179410922SJeff.Bonwick@Sun.COM 		}
179510922SJeff.Bonwick@Sun.COM 
179610922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
179710922SJeff.Bonwick@Sun.COM 	}
179810922SJeff.Bonwick@Sun.COM 
179910922SJeff.Bonwick@Sun.COM 	return (missing);
180010922SJeff.Bonwick@Sun.COM }
180110922SJeff.Bonwick@Sun.COM 
180210922SJeff.Bonwick@Sun.COM static int
180310922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
180410922SJeff.Bonwick@Sun.COM {
180510922SJeff.Bonwick@Sun.COM 	int missing = 0;
180610922SJeff.Bonwick@Sun.COM 	int error;
180710922SJeff.Bonwick@Sun.COM 
180810922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
180910922SJeff.Bonwick@Sun.COM 
181010922SJeff.Bonwick@Sun.COM 	od += count - 1;
181110922SJeff.Bonwick@Sun.COM 
181210922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
181310922SJeff.Bonwick@Sun.COM 		if (missing) {
181410922SJeff.Bonwick@Sun.COM 			missing++;
181510922SJeff.Bonwick@Sun.COM 			continue;
181610922SJeff.Bonwick@Sun.COM 		}
181710922SJeff.Bonwick@Sun.COM 
181810922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
181910922SJeff.Bonwick@Sun.COM 			continue;
182010922SJeff.Bonwick@Sun.COM 
182110922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
182210922SJeff.Bonwick@Sun.COM 
182310922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
182410922SJeff.Bonwick@Sun.COM 
182510922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
182610922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
182710922SJeff.Bonwick@Sun.COM 			missing++;
182810922SJeff.Bonwick@Sun.COM 		} else {
182910922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
183010922SJeff.Bonwick@Sun.COM 		}
183110922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
183210922SJeff.Bonwick@Sun.COM 	}
183310922SJeff.Bonwick@Sun.COM 
183410922SJeff.Bonwick@Sun.COM 	return (missing);
183510922SJeff.Bonwick@Sun.COM }
183610922SJeff.Bonwick@Sun.COM 
183710922SJeff.Bonwick@Sun.COM static int
183810922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
183910922SJeff.Bonwick@Sun.COM     void *data)
184010922SJeff.Bonwick@Sun.COM {
184110922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
184210922SJeff.Bonwick@Sun.COM 	int error;
184310922SJeff.Bonwick@Sun.COM 
184410922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
184510922SJeff.Bonwick@Sun.COM 
184610922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
184710922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
184810922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
184910922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
185010922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
185110922SJeff.Bonwick@Sun.COM 
185210922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
185310922SJeff.Bonwick@Sun.COM 
185410922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
185510922SJeff.Bonwick@Sun.COM 
185610922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
185710922SJeff.Bonwick@Sun.COM 
185810922SJeff.Bonwick@Sun.COM 	return (error);
185910922SJeff.Bonwick@Sun.COM }
186010922SJeff.Bonwick@Sun.COM 
186110922SJeff.Bonwick@Sun.COM static int
186210922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
186310922SJeff.Bonwick@Sun.COM {
186410922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
186510922SJeff.Bonwick@Sun.COM 	int error;
186610922SJeff.Bonwick@Sun.COM 
186710922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
186810922SJeff.Bonwick@Sun.COM 
186910922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
187010922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
187110922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
187210922SJeff.Bonwick@Sun.COM 
187310922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
187410922SJeff.Bonwick@Sun.COM 
187510922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
187610922SJeff.Bonwick@Sun.COM 
187710922SJeff.Bonwick@Sun.COM 	return (error);
187810922SJeff.Bonwick@Sun.COM }
187910922SJeff.Bonwick@Sun.COM 
188010922SJeff.Bonwick@Sun.COM static int
188110922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
188210922SJeff.Bonwick@Sun.COM {
188310922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
188410922SJeff.Bonwick@Sun.COM 	int error;
188510922SJeff.Bonwick@Sun.COM 
188610922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
188710922SJeff.Bonwick@Sun.COM 
188810922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
188910922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
189010922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
189110922SJeff.Bonwick@Sun.COM 
189210922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
189310922SJeff.Bonwick@Sun.COM 
189410922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
189510922SJeff.Bonwick@Sun.COM 
189610922SJeff.Bonwick@Sun.COM 	return (error);
189710922SJeff.Bonwick@Sun.COM }
189810922SJeff.Bonwick@Sun.COM 
189910922SJeff.Bonwick@Sun.COM static void
190010922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
190110922SJeff.Bonwick@Sun.COM {
190210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
190310922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
190410922SJeff.Bonwick@Sun.COM 	uint64_t txg;
190510922SJeff.Bonwick@Sun.COM 	rl_t *rl;
190610922SJeff.Bonwick@Sun.COM 
190710922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
190810922SJeff.Bonwick@Sun.COM 
190910922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
191010922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
191110922SJeff.Bonwick@Sun.COM 
191210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
191310922SJeff.Bonwick@Sun.COM 
191410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
191510922SJeff.Bonwick@Sun.COM 
191610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
191710922SJeff.Bonwick@Sun.COM 
191810922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
191910922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
192010922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
192110922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
192210922SJeff.Bonwick@Sun.COM 	} else {
192310922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
192410922SJeff.Bonwick@Sun.COM 	}
192510922SJeff.Bonwick@Sun.COM 
192610922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
192710922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
192810922SJeff.Bonwick@Sun.COM }
192910922SJeff.Bonwick@Sun.COM 
193010922SJeff.Bonwick@Sun.COM static void
193110922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
193210922SJeff.Bonwick@Sun.COM {
193310922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
193410922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
193510922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
193610922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
193710922SJeff.Bonwick@Sun.COM 	void *data;
193810922SJeff.Bonwick@Sun.COM 
193910922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
194010922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
194110922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
194210922SJeff.Bonwick@Sun.COM 
194310922SJeff.Bonwick@Sun.COM 	/*
194410922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
194510922SJeff.Bonwick@Sun.COM 	 */
194610922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
194710922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
194810922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
194910922SJeff.Bonwick@Sun.COM 
195010922SJeff.Bonwick@Sun.COM 	switch (io_type) {
195110922SJeff.Bonwick@Sun.COM 
195210922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
195310922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
195410922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
195510922SJeff.Bonwick@Sun.COM 		break;
195610922SJeff.Bonwick@Sun.COM 
195710922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
195810922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
195910922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
196010922SJeff.Bonwick@Sun.COM 			/*
196110922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
196210922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
196310922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
196410922SJeff.Bonwick@Sun.COM 			 */
196510922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
196610922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
196710922SJeff.Bonwick@Sun.COM 		}
196810922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
196910922SJeff.Bonwick@Sun.COM 		break;
197010922SJeff.Bonwick@Sun.COM 
197110922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
197210922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
197310922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
197410922SJeff.Bonwick@Sun.COM 		break;
197510922SJeff.Bonwick@Sun.COM 
197610922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
197710922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
197810922SJeff.Bonwick@Sun.COM 		break;
197910922SJeff.Bonwick@Sun.COM 
198010922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
198110922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
198210922SJeff.Bonwick@Sun.COM 		break;
198310922SJeff.Bonwick@Sun.COM 	}
198410922SJeff.Bonwick@Sun.COM 
198510922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
198610922SJeff.Bonwick@Sun.COM }
198710922SJeff.Bonwick@Sun.COM 
198810922SJeff.Bonwick@Sun.COM /*
198910922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
199010922SJeff.Bonwick@Sun.COM  */
199110922SJeff.Bonwick@Sun.COM static void
199210922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
199310922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
199410922SJeff.Bonwick@Sun.COM {
199510922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
199610922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
199710922SJeff.Bonwick@Sun.COM 
199810922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
199910922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
200010922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
200110922SJeff.Bonwick@Sun.COM 
200210922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
200310922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
200410922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
200510922SJeff.Bonwick@Sun.COM 
200610922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
200710922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
200810922SJeff.Bonwick@Sun.COM }
200910922SJeff.Bonwick@Sun.COM 
201010922SJeff.Bonwick@Sun.COM /*
201110922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
201210922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
201310922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
201410922SJeff.Bonwick@Sun.COM  * use the existing objects.
201510922SJeff.Bonwick@Sun.COM  */
201610922SJeff.Bonwick@Sun.COM static int
201710922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
201810922SJeff.Bonwick@Sun.COM {
201910922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
202010922SJeff.Bonwick@Sun.COM 	int rv = 0;
202110922SJeff.Bonwick@Sun.COM 
202210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
202310922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
202410922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
202510922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
202610922SJeff.Bonwick@Sun.COM 		rv = -1;
202710922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
202810922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
202910922SJeff.Bonwick@Sun.COM 
203010922SJeff.Bonwick@Sun.COM 	return (rv);
203110922SJeff.Bonwick@Sun.COM }
203210922SJeff.Bonwick@Sun.COM 
203310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
203410922SJeff.Bonwick@Sun.COM void
203510922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
203610922SJeff.Bonwick@Sun.COM {
203710922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
203810922SJeff.Bonwick@Sun.COM 
203910922SJeff.Bonwick@Sun.COM 	zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS));
204010922SJeff.Bonwick@Sun.COM 
204110922SJeff.Bonwick@Sun.COM 	/*
204210922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
204310922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
204410922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
204510922SJeff.Bonwick@Sun.COM 	 */
204610922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
204710922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
204810922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
204910922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
205010922SJeff.Bonwick@Sun.COM }
205110922SJeff.Bonwick@Sun.COM 
205210922SJeff.Bonwick@Sun.COM /*
2053789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2054789Sahrens  * or create a pool with a bad vdev spec.
2055789Sahrens  */
205610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2057789Sahrens void
205810922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2059789Sahrens {
206010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2061789Sahrens 	spa_t *spa;
2062789Sahrens 	nvlist_t *nvroot;
2063789Sahrens 
2064789Sahrens 	/*
2065789Sahrens 	 * Attempt to create using a bad file.
2066789Sahrens 	 */
20677754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
206810922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
206910922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2070789Sahrens 	nvlist_free(nvroot);
2071789Sahrens 
2072789Sahrens 	/*
2073789Sahrens 	 * Attempt to create using a bad mirror.
2074789Sahrens 	 */
20757754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
207610922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
207710922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2078789Sahrens 	nvlist_free(nvroot);
2079789Sahrens 
2080789Sahrens 	/*
2081789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2082789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2083789Sahrens 	 */
208410922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20857754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
208610922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2087789Sahrens 	nvlist_free(nvroot);
208810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
208910922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2090789Sahrens 	spa_close(spa, FTAG);
209110922SJeff.Bonwick@Sun.COM 
209210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2093789Sahrens }
2094789Sahrens 
20957768SJeff.Bonwick@Sun.COM static vdev_t *
20967768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
20977768SJeff.Bonwick@Sun.COM {
20987768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
20997768SJeff.Bonwick@Sun.COM 
21007768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
21017768SJeff.Bonwick@Sun.COM 		return (vd);
21027768SJeff.Bonwick@Sun.COM 
21037768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
21047768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
21057768SJeff.Bonwick@Sun.COM 		    NULL)
21067768SJeff.Bonwick@Sun.COM 			return (mvd);
21077768SJeff.Bonwick@Sun.COM 
21087768SJeff.Bonwick@Sun.COM 	return (NULL);
21097768SJeff.Bonwick@Sun.COM }
21107768SJeff.Bonwick@Sun.COM 
2111789Sahrens /*
211210594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
211310594SGeorge.Wilson@Sun.COM  */
211410594SGeorge.Wilson@Sun.COM int
211510594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
211610594SGeorge.Wilson@Sun.COM {
211710594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
211810594SGeorge.Wilson@Sun.COM 	int c;
211910594SGeorge.Wilson@Sun.COM 
212010594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
212110594SGeorge.Wilson@Sun.COM 
212210594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
212310594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
212410594SGeorge.Wilson@Sun.COM 
212510594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
212610594SGeorge.Wilson@Sun.COM 			break;
212710594SGeorge.Wilson@Sun.COM 	}
212810594SGeorge.Wilson@Sun.COM 	return (c);
212910594SGeorge.Wilson@Sun.COM }
213010594SGeorge.Wilson@Sun.COM 
213110594SGeorge.Wilson@Sun.COM /*
2132789Sahrens  * Verify that vdev_add() works as expected.
2133789Sahrens  */
213410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2135789Sahrens void
213610922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2137789Sahrens {
213810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
213910922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
214011422SMark.Musante@Sun.COM 	uint64_t leaves;
214110594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2142789Sahrens 	nvlist_t *nvroot;
2143789Sahrens 	int error;
2144789Sahrens 
214510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
214611422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2147789Sahrens 
21487754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2149789Sahrens 
215010594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2151789Sahrens 
21524527Sperrin 	/*
215310594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21544527Sperrin 	 */
215510594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
215610594SGeorge.Wilson@Sun.COM 		/*
215710594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
215810594SGeorge.Wilson@Sun.COM 		 */
215910922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
216010594SGeorge.Wilson@Sun.COM 
216110594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
216210594SGeorge.Wilson@Sun.COM 
216310594SGeorge.Wilson@Sun.COM 		/*
216410594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
216510594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
216610594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
216710594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
216810594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
216910594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
217010594SGeorge.Wilson@Sun.COM 		 */
217110922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
217210594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
217310922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
217410594SGeorge.Wilson@Sun.COM 
217510594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
217610594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
217710594SGeorge.Wilson@Sun.COM 	} else {
217810594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
217910594SGeorge.Wilson@Sun.COM 
218010594SGeorge.Wilson@Sun.COM 		/*
218110594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
218210594SGeorge.Wilson@Sun.COM 		 */
218310594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
218411422SMark.Musante@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
218510594SGeorge.Wilson@Sun.COM 
218610594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
218710594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
218810594SGeorge.Wilson@Sun.COM 
218910594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
219010594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
219110594SGeorge.Wilson@Sun.COM 		else if (error != 0)
219210594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
219310594SGeorge.Wilson@Sun.COM 	}
2194789Sahrens 
219510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
21967754SJeff.Bonwick@Sun.COM }
21977754SJeff.Bonwick@Sun.COM 
21987754SJeff.Bonwick@Sun.COM /*
21997754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
22007754SJeff.Bonwick@Sun.COM  */
220110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
22027754SJeff.Bonwick@Sun.COM void
220310922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
22047754SJeff.Bonwick@Sun.COM {
220510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
220610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22077768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22087754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22097754SJeff.Bonwick@Sun.COM 	char *aux;
22107754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22117754SJeff.Bonwick@Sun.COM 	int error;
22127754SJeff.Bonwick@Sun.COM 
22137768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22147754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22157754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22167754SJeff.Bonwick@Sun.COM 	} else {
22177754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22187754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22197754SJeff.Bonwick@Sun.COM 	}
22207754SJeff.Bonwick@Sun.COM 
222110922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22227754SJeff.Bonwick@Sun.COM 
22237754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22247754SJeff.Bonwick@Sun.COM 
22257754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22267754SJeff.Bonwick@Sun.COM 		/*
22277754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22287754SJeff.Bonwick@Sun.COM 		 */
22297754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22307754SJeff.Bonwick@Sun.COM 	} else {
22317754SJeff.Bonwick@Sun.COM 		/*
22327754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22337754SJeff.Bonwick@Sun.COM 		 */
223410922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22357754SJeff.Bonwick@Sun.COM 		for (;;) {
22367754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22377754SJeff.Bonwick@Sun.COM 			int c;
22387754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
223910922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22407754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22417754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22427754SJeff.Bonwick@Sun.COM 				    path) == 0)
22437754SJeff.Bonwick@Sun.COM 					break;
22447768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22457768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22467754SJeff.Bonwick@Sun.COM 				break;
224710922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22487754SJeff.Bonwick@Sun.COM 		}
22497754SJeff.Bonwick@Sun.COM 	}
22507754SJeff.Bonwick@Sun.COM 
22517754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22527754SJeff.Bonwick@Sun.COM 
22537754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22547754SJeff.Bonwick@Sun.COM 		/*
22557754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22567754SJeff.Bonwick@Sun.COM 		 */
22577768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22587768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22597754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22607754SJeff.Bonwick@Sun.COM 		if (error != 0)
22617754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22627754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22637754SJeff.Bonwick@Sun.COM 	} else {
22647754SJeff.Bonwick@Sun.COM 		/*
22657754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22667754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22677754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22687754SJeff.Bonwick@Sun.COM 		 */
22697754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22709816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22717754SJeff.Bonwick@Sun.COM 
22727754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22737754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22747754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22757754SJeff.Bonwick@Sun.COM 	}
22767754SJeff.Bonwick@Sun.COM 
227710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2278789Sahrens }
2279789Sahrens 
2280789Sahrens /*
228111422SMark.Musante@Sun.COM  * split a pool if it has mirror tlvdevs
228211422SMark.Musante@Sun.COM  */
228311422SMark.Musante@Sun.COM /* ARGSUSED */
228411422SMark.Musante@Sun.COM void
228511422SMark.Musante@Sun.COM ztest_split_pool(ztest_ds_t *zd, uint64_t id)
228611422SMark.Musante@Sun.COM {
228711422SMark.Musante@Sun.COM 	ztest_shared_t *zs = ztest_shared;
228811422SMark.Musante@Sun.COM 	spa_t *spa = zs->zs_spa;
228911422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
229011422SMark.Musante@Sun.COM 	nvlist_t *tree, **child, *config, *split, **schild;
229111422SMark.Musante@Sun.COM 	uint_t c, children, schildren = 0, lastlogid = 0;
229211422SMark.Musante@Sun.COM 	int error = 0;
229311422SMark.Musante@Sun.COM 
229411422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
229511422SMark.Musante@Sun.COM 
229611422SMark.Musante@Sun.COM 	/* ensure we have a useable config; mirrors of raidz aren't supported */
229711422SMark.Musante@Sun.COM 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
229811422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
229911422SMark.Musante@Sun.COM 		return;
230011422SMark.Musante@Sun.COM 	}
230111422SMark.Musante@Sun.COM 
230211422SMark.Musante@Sun.COM 	/* clean up the old pool, if any */
230311422SMark.Musante@Sun.COM 	(void) spa_destroy("splitp");
230411422SMark.Musante@Sun.COM 
230511422SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
230611422SMark.Musante@Sun.COM 
230711422SMark.Musante@Sun.COM 	/* generate a config from the existing config */
230811422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
230911422SMark.Musante@Sun.COM 	    &tree) == 0);
231011422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
231111422SMark.Musante@Sun.COM 	    &children) == 0);
231211422SMark.Musante@Sun.COM 
231311422SMark.Musante@Sun.COM 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
231411422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
231511422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
231611422SMark.Musante@Sun.COM 		nvlist_t **mchild;
231711422SMark.Musante@Sun.COM 		uint_t mchildren;
231811422SMark.Musante@Sun.COM 
231911422SMark.Musante@Sun.COM 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
232011422SMark.Musante@Sun.COM 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
232111422SMark.Musante@Sun.COM 			    0) == 0);
232211422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_string(schild[schildren],
232311422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
232411422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(schild[schildren],
232511422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
232611422SMark.Musante@Sun.COM 			if (lastlogid == 0)
232711422SMark.Musante@Sun.COM 				lastlogid = schildren;
232811422SMark.Musante@Sun.COM 			++schildren;
232911422SMark.Musante@Sun.COM 			continue;
233011422SMark.Musante@Sun.COM 		}
233111422SMark.Musante@Sun.COM 		lastlogid = 0;
233211422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist_array(child[c],
233311422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
233411422SMark.Musante@Sun.COM 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
233511422SMark.Musante@Sun.COM 	}
233611422SMark.Musante@Sun.COM 
233711422SMark.Musante@Sun.COM 	/* OK, create a config that can be used to split */
233811422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
233911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
234011422SMark.Musante@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
234111422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
234211422SMark.Musante@Sun.COM 	    lastlogid != 0 ? lastlogid : schildren) == 0);
234311422SMark.Musante@Sun.COM 
234411422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
234511422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
234611422SMark.Musante@Sun.COM 
234711422SMark.Musante@Sun.COM 	for (c = 0; c < schildren; c++)
234811422SMark.Musante@Sun.COM 		nvlist_free(schild[c]);
234911422SMark.Musante@Sun.COM 	free(schild);
235011422SMark.Musante@Sun.COM 	nvlist_free(split);
235111422SMark.Musante@Sun.COM 
235211422SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
235311422SMark.Musante@Sun.COM 
235411422SMark.Musante@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
235511422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
235611422SMark.Musante@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
235711422SMark.Musante@Sun.COM 
235811422SMark.Musante@Sun.COM 	nvlist_free(config);
235911422SMark.Musante@Sun.COM 
236011422SMark.Musante@Sun.COM 	if (error == 0) {
236111422SMark.Musante@Sun.COM 		(void) printf("successful split - results:\n");
236211422SMark.Musante@Sun.COM 		mutex_enter(&spa_namespace_lock);
236311422SMark.Musante@Sun.COM 		show_pool_stats(spa);
236411422SMark.Musante@Sun.COM 		show_pool_stats(spa_lookup("splitp"));
236511422SMark.Musante@Sun.COM 		mutex_exit(&spa_namespace_lock);
236611422SMark.Musante@Sun.COM 		++zs->zs_splits;
236711422SMark.Musante@Sun.COM 		--zs->zs_mirrors;
236811422SMark.Musante@Sun.COM 	}
236911422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
237011422SMark.Musante@Sun.COM 
237111422SMark.Musante@Sun.COM }
237211422SMark.Musante@Sun.COM 
237311422SMark.Musante@Sun.COM /*
2374789Sahrens  * Verify that we can attach and detach devices.
2375789Sahrens  */
237610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2377789Sahrens void
237810922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2379789Sahrens {
238010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
238110922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
23827768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2383789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23841544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
23857754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
238611422SMark.Musante@Sun.COM 	uint64_t leaves;
2387789Sahrens 	uint64_t leaf, top;
23881732Sbonwick 	uint64_t ashift = ztest_get_ashift();
23898241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
23901544Seschrock 	size_t oldsize, newsize;
23911544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2392789Sahrens 	int replacing;
23937793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
23947768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
23957768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2396789Sahrens 	int error, expected_error;
2397789Sahrens 
239810922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
239911422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2400789Sahrens 
24017754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2402789Sahrens 
2403789Sahrens 	/*
2404789Sahrens 	 * Decide whether to do an attach or a replace.
2405789Sahrens 	 */
2406789Sahrens 	replacing = ztest_random(2);
2407789Sahrens 
2408789Sahrens 	/*
2409789Sahrens 	 * Pick a random top-level vdev.
2410789Sahrens 	 */
241110922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2412789Sahrens 
2413789Sahrens 	/*
2414789Sahrens 	 * Pick a random leaf within it.
2415789Sahrens 	 */
241611497SMark.Musante@Sun.COM 	leaf = ztest_random(leaves);
2417789Sahrens 
2418789Sahrens 	/*
24197768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2420789Sahrens 	 */
24217768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
242211422SMark.Musante@Sun.COM 	if (zs->zs_mirrors >= 1) {
24238241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
242411422SMark.Musante@Sun.COM 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
24257768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
24268241SJeff.Bonwick@Sun.COM 	}
24278241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
24288241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
24298241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
24307768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
24318241SJeff.Bonwick@Sun.COM 	}
2432789Sahrens 
2433789Sahrens 	/*
24347768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
24357768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2436789Sahrens 	 */
24377768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
24387793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
24398241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
24408241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
24417768SJeff.Bonwick@Sun.COM 	}
24427768SJeff.Bonwick@Sun.COM 
24437768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
24449816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
24457768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
24467768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
24471544Seschrock 	pvd = oldvd->vdev_parent;
24488241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2449789Sahrens 
2450789Sahrens 	/*
24517793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
24527793SJeff.Bonwick@Sun.COM 	 */
24537793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
24547793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
24558241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
24568241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
24578241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
24588241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
245910922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
24607793SJeff.Bonwick@Sun.COM 		return;
24617793SJeff.Bonwick@Sun.COM 	}
24627793SJeff.Bonwick@Sun.COM 
24637793SJeff.Bonwick@Sun.COM 	/*
24647768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
24657768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2466789Sahrens 	 */
24677768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
24687768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
24697768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
24707768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
24717768SJeff.Bonwick@Sun.COM 	} else {
24727768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
24737768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
24747768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
24757768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
24767768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
24777768SJeff.Bonwick@Sun.COM 	}
24787768SJeff.Bonwick@Sun.COM 
24797768SJeff.Bonwick@Sun.COM 	if (newvd) {
24809816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
24817768SJeff.Bonwick@Sun.COM 	} else {
24827768SJeff.Bonwick@Sun.COM 		/*
24837768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
24847768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
24857768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
24867768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
24877768SJeff.Bonwick@Sun.COM 		 */
24887768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
24897768SJeff.Bonwick@Sun.COM 	}
2490789Sahrens 
2491789Sahrens 	/*
2492789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2493789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2494789Sahrens 	 *
24951544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2496789Sahrens 	 *
24971544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2498789Sahrens 	 */
24997768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
25007768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
25017768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
25027768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
25037768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25047768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
25057768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25067768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
25077768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
25087768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
25092174Seschrock 		expected_error = EBUSY;
25101544Seschrock 	else if (newsize < oldsize)
2511789Sahrens 		expected_error = EOVERFLOW;
25121732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
25131732Sbonwick 		expected_error = EDOM;
2514789Sahrens 	else
2515789Sahrens 		expected_error = 0;
2516789Sahrens 
25177754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2518789Sahrens 
2519789Sahrens 	/*
25201544Seschrock 	 * Build the nvlist describing newpath.
2521789Sahrens 	 */
25227754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
25237754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2524789Sahrens 
25257768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2526789Sahrens 
2527789Sahrens 	nvlist_free(root);
2528789Sahrens 
2529789Sahrens 	/*
2530789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2531789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2532789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2533789Sahrens 	 */
2534789Sahrens 	if (expected_error == ENOTSUP &&
2535789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2536789Sahrens 		expected_error = error;
2537789Sahrens 
2538797Sbonwick 	/*
2539797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2540797Sbonwick 	 */
25417046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2542797Sbonwick 		expected_error = error;
2543797Sbonwick 
25447046Sahrens 	/* XXX workaround 6690467 */
25457046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
25467046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
25477046Sahrens 		    "returned %d, expected %d",
25487046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
25497046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2550789Sahrens 	}
2551789Sahrens 
255210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2553789Sahrens }
2554789Sahrens 
2555789Sahrens /*
25569816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
25579816SGeorge.Wilson@Sun.COM  */
25589816SGeorge.Wilson@Sun.COM vdev_t *
25599816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
25609816SGeorge.Wilson@Sun.COM {
25619816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25629816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
25639816SGeorge.Wilson@Sun.COM 	size_t fsize;
25649816SGeorge.Wilson@Sun.COM 	int fd;
25659816SGeorge.Wilson@Sun.COM 
25669816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25679816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25689816SGeorge.Wilson@Sun.COM 
25699816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
25709816SGeorge.Wilson@Sun.COM 		return (vd);
25719816SGeorge.Wilson@Sun.COM 
25729816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
25739816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
25749816SGeorge.Wilson@Sun.COM 
25759816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
25769816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
25779816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
25789816SGeorge.Wilson@Sun.COM 	}
25799816SGeorge.Wilson@Sun.COM 	(void) close(fd);
25809816SGeorge.Wilson@Sun.COM 	return (NULL);
25819816SGeorge.Wilson@Sun.COM }
25829816SGeorge.Wilson@Sun.COM 
25839816SGeorge.Wilson@Sun.COM /*
25849816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
25859816SGeorge.Wilson@Sun.COM  */
25869816SGeorge.Wilson@Sun.COM /* ARGSUSED */
25879816SGeorge.Wilson@Sun.COM vdev_t *
25889816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
25899816SGeorge.Wilson@Sun.COM {
25909816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25919816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
25929816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
259310685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
259410850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
259510850SGeorge.Wilson@Sun.COM 	int error;
25969816SGeorge.Wilson@Sun.COM 
25979816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25989816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25999816SGeorge.Wilson@Sun.COM 
26009816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
26019816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
260210850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
26039816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26049816SGeorge.Wilson@Sun.COM 
26059816SGeorge.Wilson@Sun.COM 	/*
260610850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
260710850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
260810850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
260910850SGeorge.Wilson@Sun.COM 	 */
261010850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
261110850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
261210850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
261310850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
261410850SGeorge.Wilson@Sun.COM 		}
261510850SGeorge.Wilson@Sun.COM 		return (vd);
261610850SGeorge.Wilson@Sun.COM 	}
261710850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
261810850SGeorge.Wilson@Sun.COM 
261910850SGeorge.Wilson@Sun.COM 	/*
26209816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
26219816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
26229816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
26239816SGeorge.Wilson@Sun.COM 	 * trying to online it.
26249816SGeorge.Wilson@Sun.COM 	 */
262510685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
262610685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
262710685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
262810685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
262910922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
263010922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
263110685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
263210685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
263310685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
26349816SGeorge.Wilson@Sun.COM 		}
26359816SGeorge.Wilson@Sun.COM 		return (vd);
26369816SGeorge.Wilson@Sun.COM 	}
26379816SGeorge.Wilson@Sun.COM 	return (NULL);
26389816SGeorge.Wilson@Sun.COM }
26399816SGeorge.Wilson@Sun.COM 
26409816SGeorge.Wilson@Sun.COM /*
26419816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
26429816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
26439816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
26449816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
26459816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
26469816SGeorge.Wilson@Sun.COM  */
26479816SGeorge.Wilson@Sun.COM vdev_t *
26489816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
26499816SGeorge.Wilson@Sun.COM {
26509816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26519816SGeorge.Wilson@Sun.COM 		if (func == NULL)
26529816SGeorge.Wilson@Sun.COM 			return (vd);
26539816SGeorge.Wilson@Sun.COM 		else
26549816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
26559816SGeorge.Wilson@Sun.COM 	}
26569816SGeorge.Wilson@Sun.COM 
26579816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
26589816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
26599816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
26609816SGeorge.Wilson@Sun.COM 			return (cvd);
26619816SGeorge.Wilson@Sun.COM 	}
26629816SGeorge.Wilson@Sun.COM 	return (NULL);
26639816SGeorge.Wilson@Sun.COM }
26649816SGeorge.Wilson@Sun.COM 
26659816SGeorge.Wilson@Sun.COM /*
2666789Sahrens  * Verify that dynamic LUN growth works as expected.
2667789Sahrens  */
266810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2669789Sahrens void
267010922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2671789Sahrens {
267210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
267310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
267410922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
267510922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
267610922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
26779816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
267810922SJeff.Bonwick@Sun.COM 	uint64_t top;
267910922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
268010922SJeff.Bonwick@Sun.COM 
268110922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
26829816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26839816SGeorge.Wilson@Sun.COM 
268410922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
268510922SJeff.Bonwick@Sun.COM 
268610922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
268710922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
268810922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
268910922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
269010922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
26919816SGeorge.Wilson@Sun.COM 
26929816SGeorge.Wilson@Sun.COM 	/*
26939816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
26949816SGeorge.Wilson@Sun.COM 	 * our top-level device.
26959816SGeorge.Wilson@Sun.COM 	 */
26969816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
26979816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
26989816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
26999816SGeorge.Wilson@Sun.COM 
27009816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
27019816SGeorge.Wilson@Sun.COM 
27029816SGeorge.Wilson@Sun.COM 	/*
270310685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
270410685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
27059816SGeorge.Wilson@Sun.COM 	 */
270610685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
270710685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
27089816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
270910922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27109816SGeorge.Wilson@Sun.COM 		return;
27119816SGeorge.Wilson@Sun.COM 	}
27129816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
27139816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
27149816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
27159816SGeorge.Wilson@Sun.COM 
271610922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
271710922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
27189816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
27199816SGeorge.Wilson@Sun.COM 	}
27209816SGeorge.Wilson@Sun.COM 
2721789Sahrens 	/*
27229816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
27239816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
27249816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
27259816SGeorge.Wilson@Sun.COM 	 */
27269816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
27279816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
27289816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
27299816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
27309816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
273110685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
27329816SGeorge.Wilson@Sun.COM 		}
273310922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
273410922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27359816SGeorge.Wilson@Sun.COM 		return;
27369816SGeorge.Wilson@Sun.COM 	}
27379816SGeorge.Wilson@Sun.COM 
273810922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
27399816SGeorge.Wilson@Sun.COM 
27409816SGeorge.Wilson@Sun.COM 	/*
27419816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
27429816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
27439816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2744789Sahrens 	 */
274510922SJeff.Bonwick@Sun.COM 	for (;;) {
274610922SJeff.Bonwick@Sun.COM 		boolean_t done;
274710922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
274810922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
274910922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
275010922SJeff.Bonwick@Sun.COM 		if (done)
275110922SJeff.Bonwick@Sun.COM 			break;
275210922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
275310922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
275410922SJeff.Bonwick@Sun.COM 	}
27559816SGeorge.Wilson@Sun.COM 
27569816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
275710922SJeff.Bonwick@Sun.COM 
275810922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
275910922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
276010922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
276110922SJeff.Bonwick@Sun.COM 
276210922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
276310922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
276410922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
276510922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
276610922SJeff.Bonwick@Sun.COM 		}
276710922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
276810922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
276910922SJeff.Bonwick@Sun.COM 		return;
277010922SJeff.Bonwick@Sun.COM 	}
277110922SJeff.Bonwick@Sun.COM 
277210922SJeff.Bonwick@Sun.COM 	/*
277310922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
277410922SJeff.Bonwick@Sun.COM 	 */
277510922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
277610922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
277710922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
27789816SGeorge.Wilson@Sun.COM 
27799816SGeorge.Wilson@Sun.COM 	/*
27809816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
27819816SGeorge.Wilson@Sun.COM 	 */
278210922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
278310922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
278410922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
278510922SJeff.Bonwick@Sun.COM 
278610922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
27879816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
27889816SGeorge.Wilson@Sun.COM 
278910922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
279010922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
27919816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
27929816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2793789Sahrens 	}
279410922SJeff.Bonwick@Sun.COM 
27959816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
279610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
279710922SJeff.Bonwick@Sun.COM }
279810922SJeff.Bonwick@Sun.COM 
279910922SJeff.Bonwick@Sun.COM /*
280010922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
280110922SJeff.Bonwick@Sun.COM  */
280210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
280310922SJeff.Bonwick@Sun.COM static void
280410922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
280510922SJeff.Bonwick@Sun.COM {
280610922SJeff.Bonwick@Sun.COM 	/*
280710922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
280810922SJeff.Bonwick@Sun.COM 	 */
280910922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
281010922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2811789Sahrens }
2812789Sahrens 
2813789Sahrens /* ARGSUSED */
281410922SJeff.Bonwick@Sun.COM static int
281511209SMatthew.Ahrens@Sun.COM ztest_objset_destroy_cb(const char *name, void *arg)
2816789Sahrens {
2817789Sahrens 	objset_t *os;
281810922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2819789Sahrens 	int error;
2820789Sahrens 
2821789Sahrens 	/*
2822789Sahrens 	 * Verify that the dataset contains a directory object.
2823789Sahrens 	 */
282410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
282510922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
28261731Sbonwick 	if (error != ENOENT) {
28271731Sbonwick 		/* We could have crashed in the middle of destroying it */
28281731Sbonwick 		ASSERT3U(error, ==, 0);
282910922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
283010922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
28311731Sbonwick 	}
283210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2833789Sahrens 
2834789Sahrens 	/*
2835789Sahrens 	 * Destroy the dataset.
2836789Sahrens 	 */
283710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
28382199Sahrens 	return (0);
2839789Sahrens }
2840789Sahrens 
284110922SJeff.Bonwick@Sun.COM static boolean_t
284210922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2843789Sahrens {
284410922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
284510922SJeff.Bonwick@Sun.COM 	int error;
284610922SJeff.Bonwick@Sun.COM 
284710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
284810922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
284910922SJeff.Bonwick@Sun.COM 
285010922SJeff.Bonwick@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
285110922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
285210922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
285310922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
285410922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
285510922SJeff.Bonwick@Sun.COM 	}
285610922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
285710922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
285810922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2859789Sahrens }
2860789Sahrens 
286110922SJeff.Bonwick@Sun.COM static boolean_t
286210922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
286310922SJeff.Bonwick@Sun.COM {
286410922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
286510922SJeff.Bonwick@Sun.COM 	int error;
286610922SJeff.Bonwick@Sun.COM 
286710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
286810922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
286910922SJeff.Bonwick@Sun.COM 
287010922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
287110922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
287210922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
287310922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
287410922SJeff.Bonwick@Sun.COM }
287510922SJeff.Bonwick@Sun.COM 
287610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2877789Sahrens void
287810922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2879789Sahrens {
288010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
288110922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
288210922SJeff.Bonwick@Sun.COM 	int iters;
2883789Sahrens 	int error;
28846689Smaybee 	objset_t *os, *os2;
288510922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2886789Sahrens 	zilog_t *zilog;
288710922SJeff.Bonwick@Sun.COM 
288810922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
288910922SJeff.Bonwick@Sun.COM 
289010922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
289110922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2892789Sahrens 
2893789Sahrens 	/*
2894789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2895789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
289610922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2897789Sahrens 	 */
2898789Sahrens 	if (ztest_random(2) == 0 &&
289910298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
290010922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
290110922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
290210922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
290310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2904789Sahrens 	}
2905789Sahrens 
2906789Sahrens 	/*
2907789Sahrens 	 * There may be an old instance of the dataset we're about to
2908789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2909789Sahrens 	 * and all of its snapshots.
2910789Sahrens 	 */
291110922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
29122417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2913789Sahrens 
2914789Sahrens 	/*
2915789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2916789Sahrens 	 */
291710922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2918789Sahrens 
2919789Sahrens 	/*
2920789Sahrens 	 * Verify that we can create a new dataset.
2921789Sahrens 	 */
292210272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
292310922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
2924789Sahrens 	if (error) {
2925789Sahrens 		if (error == ENOSPC) {
292610922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
292710922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2928789Sahrens 			return;
2929789Sahrens 		}
2930789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2931789Sahrens 	}
2932789Sahrens 
293310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
293410922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
293510922SJeff.Bonwick@Sun.COM 
293610922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2937789Sahrens 
2938789Sahrens 	/*
2939789Sahrens 	 * Open the intent log for it.
2940789Sahrens 	 */
294110922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2942789Sahrens 
2943789Sahrens 	/*
294410922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
294510922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2946789Sahrens 	 */
294710922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
294810922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
294910922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
295010922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
295110922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2952789Sahrens 	}
2953789Sahrens 
2954789Sahrens 	/*
2955789Sahrens 	 * Verify that we cannot create an existing dataset.
2956789Sahrens 	 */
295710922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
295810922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2959789Sahrens 
2960789Sahrens 	/*
296110298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2962789Sahrens 	 */
296310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
296410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
296510298SMatthew.Ahrens@Sun.COM 
296610298SMatthew.Ahrens@Sun.COM 	/*
296710922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
296810298SMatthew.Ahrens@Sun.COM 	 */
296910922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
297010922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
2971789Sahrens 
2972789Sahrens 	zil_close(zilog);
297310298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
297410922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
297510922SJeff.Bonwick@Sun.COM 
297610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2977789Sahrens }
2978789Sahrens 
2979789Sahrens /*
2980789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
2981789Sahrens  */
2982789Sahrens void
298310922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
2984789Sahrens {
298510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
298610922SJeff.Bonwick@Sun.COM 
298710922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
298810922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
298910922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
299010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2991789Sahrens }
2992789Sahrens 
2993789Sahrens /*
29949691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
29959691SGeorge.Wilson@Sun.COM  */
29969691SGeorge.Wilson@Sun.COM void
299710922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
29989691SGeorge.Wilson@Sun.COM {
299910922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
300010922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
300110922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
300210922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
300310922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
30049691SGeorge.Wilson@Sun.COM 	int error;
30059691SGeorge.Wilson@Sun.COM 
300610922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
300710922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
300810922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
300910922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
301010922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30119691SGeorge.Wilson@Sun.COM 
301210242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
30139691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30149691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
301510242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
30169691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30179691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
301810242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
30199691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30209691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
302110242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
30229691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30239691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
302410242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
30259691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30269691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
30279691SGeorge.Wilson@Sun.COM }
30289691SGeorge.Wilson@Sun.COM 
30299691SGeorge.Wilson@Sun.COM /*
30308779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
30318779SMark.Musante@Sun.COM  */
30328779SMark.Musante@Sun.COM void
303310922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
30348779SMark.Musante@Sun.COM {
303510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
30368779SMark.Musante@Sun.COM 	objset_t *clone;
30378779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
303810922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
303910922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
304010922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
304110922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
304210922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
304310922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
304410922SJeff.Bonwick@Sun.COM 	int error;
304510922SJeff.Bonwick@Sun.COM 
304610922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
304710922SJeff.Bonwick@Sun.COM 
304810922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
304910922SJeff.Bonwick@Sun.COM 
305010922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
305110922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
305210922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
305310922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
305410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30558837SMark.Musante@Sun.COM 
30569355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
305710922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30589229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30599229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
306010922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30619229SGeorge.Wilson@Sun.COM 			goto out;
30629229SGeorge.Wilson@Sun.COM 		}
30639229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
30649229SGeorge.Wilson@Sun.COM 	}
30658779SMark.Musante@Sun.COM 
306610298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
30678779SMark.Musante@Sun.COM 	if (error)
30688779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
30698779SMark.Musante@Sun.COM 
307010272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
307110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
30729229SGeorge.Wilson@Sun.COM 	if (error) {
30739229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
307410922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30759229SGeorge.Wilson@Sun.COM 			goto out;
30769229SGeorge.Wilson@Sun.COM 		}
30778779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
30789229SGeorge.Wilson@Sun.COM 	}
30798779SMark.Musante@Sun.COM 
30808779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
308110922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30829229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30839229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
308410922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30859229SGeorge.Wilson@Sun.COM 			goto out;
30869229SGeorge.Wilson@Sun.COM 		}
30879229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
30889229SGeorge.Wilson@Sun.COM 	}
30898779SMark.Musante@Sun.COM 
30908779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
309110922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30929229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30939229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
309410922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30959229SGeorge.Wilson@Sun.COM 			goto out;
30969229SGeorge.Wilson@Sun.COM 		}
30979229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
30989229SGeorge.Wilson@Sun.COM 	}
30998779SMark.Musante@Sun.COM 
310010298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
31018779SMark.Musante@Sun.COM 	if (error)
31028779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31038779SMark.Musante@Sun.COM 
310410272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
310510298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31069229SGeorge.Wilson@Sun.COM 	if (error) {
31079229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
310810922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31099229SGeorge.Wilson@Sun.COM 			goto out;
31109229SGeorge.Wilson@Sun.COM 		}
31118779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
31129229SGeorge.Wilson@Sun.COM 	}
31138779SMark.Musante@Sun.COM 
311410298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
31158779SMark.Musante@Sun.COM 	if (error)
31168779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
311710588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
31188779SMark.Musante@Sun.COM 	if (error != EBUSY)
31198779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
31208779SMark.Musante@Sun.COM 		    error);
31218779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
31228779SMark.Musante@Sun.COM 
31239229SGeorge.Wilson@Sun.COM out:
312410922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
312510922SJeff.Bonwick@Sun.COM 
312610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
31278779SMark.Musante@Sun.COM }
31288779SMark.Musante@Sun.COM 
31298779SMark.Musante@Sun.COM /*
3130789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3131789Sahrens  */
3132789Sahrens void
313310922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3134789Sahrens {
313510922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
313610922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
313710922SJeff.Bonwick@Sun.COM 
313810922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
313910922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3140789Sahrens 
3141789Sahrens 	/*
314210922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
314310922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3144789Sahrens 	 */
314510922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
314610922SJeff.Bonwick@Sun.COM 		return;
314710922SJeff.Bonwick@Sun.COM 
314810922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
314910922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
315010922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3151789Sahrens }
3152789Sahrens 
3153789Sahrens /*
3154789Sahrens  * Verify that dmu_{read,write} work as expected.
3155789Sahrens  */
3156789Sahrens void
315710922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3158789Sahrens {
315910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
316010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3161789Sahrens 	dmu_tx_t *tx;
3162789Sahrens 	int i, freeit, error;
3163789Sahrens 	uint64_t n, s, txg;
3164789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
316510922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
316610922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3167789Sahrens 	uint64_t regions = 997;
3168789Sahrens 	uint64_t stride = 123456789ULL;
3169789Sahrens 	uint64_t width = 40;
3170789Sahrens 	int free_percent = 5;
3171789Sahrens 
3172789Sahrens 	/*
3173789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3174789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3175789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3176789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3177789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3178789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3179789Sahrens 	 *
3180789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3181789Sahrens 	 *	bigobj, at the head of the nth chunk
3182789Sahrens 	 *	bigobj, at the tail of the nth chunk
3183789Sahrens 	 *
3184789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3185789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3186789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3187789Sahrens 	 *
3188789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3189789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3190789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3191789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3192789Sahrens 	 * tracking which parts of an object are allocated and free,
3193789Sahrens 	 * and that the contents of the allocated blocks are correct.
3194789Sahrens 	 */
3195789Sahrens 
3196789Sahrens 	/*
3197789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3198789Sahrens 	 */
319910922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
320010922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
320110922SJeff.Bonwick@Sun.COM 
320210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
320310922SJeff.Bonwick@Sun.COM 		return;
320410922SJeff.Bonwick@Sun.COM 
320510922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
320610922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
320710922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
320810922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3209789Sahrens 
3210789Sahrens 	/*
3211789Sahrens 	 * Prefetch a random chunk of the big object.
3212789Sahrens 	 * Our aim here is to get some async reads in flight
3213789Sahrens 	 * for blocks that we may free below; the DMU should
3214789Sahrens 	 * handle this race correctly.
3215789Sahrens 	 */
3216789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3217789Sahrens 	s = 1 + ztest_random(2 * width - 1);
321810922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3219789Sahrens 
3220789Sahrens 	/*
3221789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3222789Sahrens 	 */
3223789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3224789Sahrens 	s = 1 + ztest_random(width - 1);
3225789Sahrens 
3226789Sahrens 	packoff = n * sizeof (bufwad_t);
3227789Sahrens 	packsize = s * sizeof (bufwad_t);
3228789Sahrens 
322910922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
323010922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3231789Sahrens 
3232789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3233789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3234789Sahrens 
3235789Sahrens 	/*
3236789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3237789Sahrens 	 * overwriting it.
3238789Sahrens 	 */
3239789Sahrens 	freeit = (ztest_random(100) < free_percent);
3240789Sahrens 
3241789Sahrens 	/*
3242789Sahrens 	 * Read the current contents of our objects.
3243789Sahrens 	 */
324410922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
32459512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32461544Seschrock 	ASSERT3U(error, ==, 0);
324710922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
32489512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32491544Seschrock 	ASSERT3U(error, ==, 0);
3250789Sahrens 
3251789Sahrens 	/*
3252789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3253789Sahrens 	 */
3254789Sahrens 	tx = dmu_tx_create(os);
3255789Sahrens 
325610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3257789Sahrens 
3258789Sahrens 	if (freeit)
325910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3260789Sahrens 	else
326110922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
326210922SJeff.Bonwick@Sun.COM 
326310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
326410922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3265789Sahrens 		umem_free(packbuf, packsize);
3266789Sahrens 		umem_free(bigbuf, bigsize);
3267789Sahrens 		return;
3268789Sahrens 	}
3269789Sahrens 
327010922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
327110922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
327210922SJeff.Bonwick@Sun.COM 
327310922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
327410922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3275789Sahrens 
3276789Sahrens 	/*
3277789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3278789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3279789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3280789Sahrens 	 * with the new values we want to write out.
3281789Sahrens 	 */
3282789Sahrens 	for (i = 0; i < s; i++) {
3283789Sahrens 		/* LINTED */
3284789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3285789Sahrens 		/* LINTED */
328610922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3287789Sahrens 		/* LINTED */
328810922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3289789Sahrens 
3290789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3291789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3292789Sahrens 
3293789Sahrens 		if (pack->bw_txg > txg)
3294789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3295789Sahrens 			    pack->bw_txg, txg);
3296789Sahrens 
3297789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3298789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3299789Sahrens 			    pack->bw_index, n, i);
3300789Sahrens 
3301789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3302789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3303789Sahrens 
3304789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3305789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3306789Sahrens 
3307789Sahrens 		if (freeit) {
3308789Sahrens 			bzero(pack, sizeof (bufwad_t));
3309789Sahrens 		} else {
3310789Sahrens 			pack->bw_index = n + i;
3311789Sahrens 			pack->bw_txg = txg;
3312789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3313789Sahrens 		}
3314789Sahrens 		*bigH = *pack;
3315789Sahrens 		*bigT = *pack;
3316789Sahrens 	}
3317789Sahrens 
3318789Sahrens 	/*
3319789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3320789Sahrens 	 * Now write them out.
3321789Sahrens 	 */
332210922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3323789Sahrens 
3324789Sahrens 	if (freeit) {
332510922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3326789Sahrens 			(void) printf("freeing offset %llx size %llx"
3327789Sahrens 			    " txg %llx\n",
3328789Sahrens 			    (u_longlong_t)bigoff,
3329789Sahrens 			    (u_longlong_t)bigsize,
3330789Sahrens 			    (u_longlong_t)txg);
3331789Sahrens 		}
333210922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3333789Sahrens 	} else {
333410922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3335789Sahrens 			(void) printf("writing offset %llx size %llx"
3336789Sahrens 			    " txg %llx\n",
3337789Sahrens 			    (u_longlong_t)bigoff,
3338789Sahrens 			    (u_longlong_t)bigsize,
3339789Sahrens 			    (u_longlong_t)txg);
3340789Sahrens 		}
334110922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3342789Sahrens 	}
3343789Sahrens 
3344789Sahrens 	dmu_tx_commit(tx);
3345789Sahrens 
3346789Sahrens 	/*
3347789Sahrens 	 * Sanity check the stuff we just wrote.
3348789Sahrens 	 */
3349789Sahrens 	{
3350789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3351789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3352789Sahrens 
335310922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
33549512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
335510922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
33569512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3357789Sahrens 
3358789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3359789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3360789Sahrens 
3361789Sahrens 		umem_free(packcheck, packsize);
3362789Sahrens 		umem_free(bigcheck, bigsize);
3363789Sahrens 	}
3364789Sahrens 
3365789Sahrens 	umem_free(packbuf, packsize);
3366789Sahrens 	umem_free(bigbuf, bigsize);
3367789Sahrens }
3368789Sahrens 
3369789Sahrens void
33709412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
337110922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
33729412SAleksandr.Guzovskiy@Sun.COM {
33739412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
33749412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
33759412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
33769412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
33779412SAleksandr.Guzovskiy@Sun.COM 
33789412SAleksandr.Guzovskiy@Sun.COM 	/*
33799412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
33809412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
33819412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
33829412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
33839412SAleksandr.Guzovskiy@Sun.COM 	 */
33849412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
33859412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
33869412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
33879412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
338810922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
33899412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
339010922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
33919412SAleksandr.Guzovskiy@Sun.COM 
33929412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
33939412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
33949412SAleksandr.Guzovskiy@Sun.COM 
33959412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
33969412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
33979412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
33989412SAleksandr.Guzovskiy@Sun.COM 
33999412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
34009412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
34019412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
34029412SAleksandr.Guzovskiy@Sun.COM 
34039412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
34049412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
34059412SAleksandr.Guzovskiy@Sun.COM 
34069412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
34079412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
34089412SAleksandr.Guzovskiy@Sun.COM 
34099412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
34109412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
34119412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
34129412SAleksandr.Guzovskiy@Sun.COM 
34139412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
34149412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
34159412SAleksandr.Guzovskiy@Sun.COM 	}
34169412SAleksandr.Guzovskiy@Sun.COM }
34179412SAleksandr.Guzovskiy@Sun.COM 
34189412SAleksandr.Guzovskiy@Sun.COM void
341910922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
34209412SAleksandr.Guzovskiy@Sun.COM {
342110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
342210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
34239412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
34249412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34259412SAleksandr.Guzovskiy@Sun.COM 	int error;
34269412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
34279412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
342810922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
342910922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
343010922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
34319412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
34329412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
34339412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
34349412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
34359412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
343610922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
34379412SAleksandr.Guzovskiy@Sun.COM 
34389412SAleksandr.Guzovskiy@Sun.COM 	/*
34399412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
34409412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
34419412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
34429412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
34439412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
34449412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
34459412SAleksandr.Guzovskiy@Sun.COM 	 *
34469412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
34479412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
34489412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
34499412SAleksandr.Guzovskiy@Sun.COM 	 *
34509412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
34519412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
34529412SAleksandr.Guzovskiy@Sun.COM 	 */
34539412SAleksandr.Guzovskiy@Sun.COM 
34549412SAleksandr.Guzovskiy@Sun.COM 	/*
34559412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
34569412SAleksandr.Guzovskiy@Sun.COM 	 */
345710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
345810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
345910922SJeff.Bonwick@Sun.COM 
346010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
346110922SJeff.Bonwick@Sun.COM 		return;
346210922SJeff.Bonwick@Sun.COM 
346310922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
346410922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
346510922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
346610922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
346710922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
346810922SJeff.Bonwick@Sun.COM 
346910922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
347010922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
347110922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
347210922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
34739412SAleksandr.Guzovskiy@Sun.COM 
34749412SAleksandr.Guzovskiy@Sun.COM 	/*
34759412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
34769412SAleksandr.Guzovskiy@Sun.COM 	 */
34779412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
34789412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
34799412SAleksandr.Guzovskiy@Sun.COM 
34809412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
34819412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
34829412SAleksandr.Guzovskiy@Sun.COM 
348310922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
348410922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
34859412SAleksandr.Guzovskiy@Sun.COM 
34869412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
34879412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
34889412SAleksandr.Guzovskiy@Sun.COM 
348910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
34909412SAleksandr.Guzovskiy@Sun.COM 
34919412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
34929412SAleksandr.Guzovskiy@Sun.COM 
34939412SAleksandr.Guzovskiy@Sun.COM 	/*
34949412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
34959412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
34969412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
34979412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
34989412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
34999412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
35009412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
35019412SAleksandr.Guzovskiy@Sun.COM 	 */
35029412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
35039412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
35049412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
35059412SAleksandr.Guzovskiy@Sun.COM 
35069412SAleksandr.Guzovskiy@Sun.COM 		/*
35079412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
35089412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
35099412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
35109412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
35119412SAleksandr.Guzovskiy@Sun.COM 		 */
35129412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
35139412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35149412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
351510922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
35169412SAleksandr.Guzovskiy@Sun.COM 			} else {
35179412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
351810922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35199412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
352010922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35219412SAleksandr.Guzovskiy@Sun.COM 			}
35229412SAleksandr.Guzovskiy@Sun.COM 		}
35239412SAleksandr.Guzovskiy@Sun.COM 
35249412SAleksandr.Guzovskiy@Sun.COM 		/*
35259412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
35269412SAleksandr.Guzovskiy@Sun.COM 		 */
35279412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
35289412SAleksandr.Guzovskiy@Sun.COM 
352910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
353010922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
353110922SJeff.Bonwick@Sun.COM 
353210922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
353310922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
35349412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
35359412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
35369412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
35379412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
35389412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
35399412SAleksandr.Guzovskiy@Sun.COM 				} else {
35409412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35419412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
35429412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35439412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
35449412SAleksandr.Guzovskiy@Sun.COM 				}
35459412SAleksandr.Guzovskiy@Sun.COM 			}
35469412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35479412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
35489412SAleksandr.Guzovskiy@Sun.COM 			return;
35499412SAleksandr.Guzovskiy@Sun.COM 		}
35509412SAleksandr.Guzovskiy@Sun.COM 
35519412SAleksandr.Guzovskiy@Sun.COM 		/*
35529412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
35539412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
35549412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
35559412SAleksandr.Guzovskiy@Sun.COM 		 */
35569412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
355710922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
35589512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
35599412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
356010922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
35619512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
35629412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
35639412SAleksandr.Guzovskiy@Sun.COM 		}
35649412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
356510922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
35669412SAleksandr.Guzovskiy@Sun.COM 
35679412SAleksandr.Guzovskiy@Sun.COM 		/*
35689412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
35699412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
35709412SAleksandr.Guzovskiy@Sun.COM 		 */
357110922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
357210922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
35739412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
35749412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
35759412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
35769412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
35779412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
35789412SAleksandr.Guzovskiy@Sun.COM 		}
357910922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
35809412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
35819412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35829412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
358310922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
35849412SAleksandr.Guzovskiy@Sun.COM 			} else {
35859412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
35869412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
358710922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
35889412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
358910922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
35909412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
359110922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
35929412SAleksandr.Guzovskiy@Sun.COM 			}
35939412SAleksandr.Guzovskiy@Sun.COM 
35949412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
359510922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
35969412SAleksandr.Guzovskiy@Sun.COM 				    FTAG, &dbt) == 0);
35979412SAleksandr.Guzovskiy@Sun.COM 			}
35989412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35999412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36009412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
36019412SAleksandr.Guzovskiy@Sun.COM 			} else {
36029412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36039412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
36049412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
360510922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
36069412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
36079412SAleksandr.Guzovskiy@Sun.COM 			}
36089412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
36099412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
36109412SAleksandr.Guzovskiy@Sun.COM 			}
36119412SAleksandr.Guzovskiy@Sun.COM 		}
36129412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
36139412SAleksandr.Guzovskiy@Sun.COM 
36149412SAleksandr.Guzovskiy@Sun.COM 		/*
36159412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
36169412SAleksandr.Guzovskiy@Sun.COM 		 */
36179412SAleksandr.Guzovskiy@Sun.COM 		{
36189412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
36199412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
36209412SAleksandr.Guzovskiy@Sun.COM 
362110922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
36229512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
362310922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
36249512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
36259412SAleksandr.Guzovskiy@Sun.COM 
36269412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
36279412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
36289412SAleksandr.Guzovskiy@Sun.COM 
36299412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
36309412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
36319412SAleksandr.Guzovskiy@Sun.COM 		}
36329412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
36339412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
36349412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
36359412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
36369412SAleksandr.Guzovskiy@Sun.COM 		}
36379412SAleksandr.Guzovskiy@Sun.COM 	}
36389412SAleksandr.Guzovskiy@Sun.COM 
36399412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
36409412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
36419412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
36429412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
36439412SAleksandr.Guzovskiy@Sun.COM }
36449412SAleksandr.Guzovskiy@Sun.COM 
364510922SJeff.Bonwick@Sun.COM /* ARGSUSED */
36469412SAleksandr.Guzovskiy@Sun.COM void
364710922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
36483711Smaybee {
364910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
365010922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
365110922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
36523711Smaybee 
36533711Smaybee 	/*
365410922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
365510922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
365610922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
36573711Smaybee 	 */
365810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
365910922SJeff.Bonwick@Sun.COM 
366010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
366110922SJeff.Bonwick@Sun.COM 		return;
366210922SJeff.Bonwick@Sun.COM 
366310922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
366410922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
36653711Smaybee }
36663711Smaybee 
36673711Smaybee void
366810922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3669789Sahrens {
367010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
367110922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
367210922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
367310922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
367410922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
367510922SJeff.Bonwick@Sun.COM 	void *data;
367610922SJeff.Bonwick@Sun.COM 
367710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
367810922SJeff.Bonwick@Sun.COM 
367910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
36805530Sbonwick 		return;
368110922SJeff.Bonwick@Sun.COM 
368210922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
368310922SJeff.Bonwick@Sun.COM 		return;
368410922SJeff.Bonwick@Sun.COM 
368510922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
368610922SJeff.Bonwick@Sun.COM 
368710922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
368810922SJeff.Bonwick@Sun.COM 
368910922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
369010922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
369110922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
369210922SJeff.Bonwick@Sun.COM 		    data) != 0)
369310922SJeff.Bonwick@Sun.COM 			break;
369410922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
369510922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
36965530Sbonwick 	}
369710922SJeff.Bonwick@Sun.COM 
369810922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3699789Sahrens }
3700789Sahrens 
3701789Sahrens /*
3702789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3703789Sahrens  */
3704789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3705789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3706789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3707789Sahrens 
3708789Sahrens void
370910922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3710789Sahrens {
371110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
371210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3713789Sahrens 	uint64_t object;
3714789Sahrens 	uint64_t txg, last_txg;
3715789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3716789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3717789Sahrens 	int i, ints;
3718789Sahrens 	dmu_tx_t *tx;
3719789Sahrens 	char propname[100], txgname[100];
3720789Sahrens 	int error;
3721789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3722789Sahrens 
372310922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
372410922SJeff.Bonwick@Sun.COM 
372510922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
372610922SJeff.Bonwick@Sun.COM 		return;
372710922SJeff.Bonwick@Sun.COM 
372810922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3729789Sahrens 
3730789Sahrens 	/*
373110922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
373210922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3733789Sahrens 	 */
373410922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
373510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
373610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
373710922SJeff.Bonwick@Sun.COM 	if (txg == 0)
373810922SJeff.Bonwick@Sun.COM 		return;
373910922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
374010922SJeff.Bonwick@Sun.COM 		value[i] = i;
374110922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
374210922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3743789Sahrens 	}
374410922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
374510922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
374610922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
374710922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
374810922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
374910922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
375010922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
375110922SJeff.Bonwick@Sun.COM 	}
375210922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
375310922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
375410922SJeff.Bonwick@Sun.COM 	}
375510922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
375610922SJeff.Bonwick@Sun.COM 
375710922SJeff.Bonwick@Sun.COM 	/*
375810922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
375910922SJeff.Bonwick@Sun.COM 	 */
3760789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3761789Sahrens 
37625530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37635530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37645530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37655530Sbonwick 	bzero(value, sizeof (value));
37665530Sbonwick 	last_txg = 0;
37675530Sbonwick 
37685530Sbonwick 	/*
37695530Sbonwick 	 * If these zap entries already exist, validate their contents.
37705530Sbonwick 	 */
37715530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
37725530Sbonwick 	if (error == 0) {
37735530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
37745530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
37755530Sbonwick 
37765530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
37775530Sbonwick 		    zl_ints, &last_txg) == 0);
37785530Sbonwick 
37795530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
37805530Sbonwick 		    &zl_ints) == 0);
37815530Sbonwick 
37825530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
37835530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
37845530Sbonwick 
37855530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
37865530Sbonwick 		    zl_ints, value) == 0);
37875530Sbonwick 
37885530Sbonwick 		for (i = 0; i < ints; i++) {
37895530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3790789Sahrens 		}
37915530Sbonwick 	} else {
37925530Sbonwick 		ASSERT3U(error, ==, ENOENT);
37935530Sbonwick 	}
37945530Sbonwick 
37955530Sbonwick 	/*
37965530Sbonwick 	 * Atomically update two entries in our zap object.
37975530Sbonwick 	 * The first is named txg_%llu, and contains the txg
37985530Sbonwick 	 * in which the property was last updated.  The second
37995530Sbonwick 	 * is named prop_%llu, and the nth element of its value
38005530Sbonwick 	 * should be txg + object + n.
38015530Sbonwick 	 */
38025530Sbonwick 	tx = dmu_tx_create(os);
380310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
380410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
380510922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38065530Sbonwick 		return;
38075530Sbonwick 
38085530Sbonwick 	if (last_txg > txg)
38095530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
38105530Sbonwick 
38115530Sbonwick 	for (i = 0; i < ints; i++)
38125530Sbonwick 		value[i] = txg + object + i;
38135530Sbonwick 
381410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
381510922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
381610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
381710922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
38185530Sbonwick 
38195530Sbonwick 	dmu_tx_commit(tx);
38205530Sbonwick 
38215530Sbonwick 	/*
38225530Sbonwick 	 * Remove a random pair of entries.
38235530Sbonwick 	 */
38245530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
38255530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
38265530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
38275530Sbonwick 
38285530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38295530Sbonwick 
38305530Sbonwick 	if (error == ENOENT)
38315530Sbonwick 		return;
38325530Sbonwick 
38335530Sbonwick 	ASSERT3U(error, ==, 0);
38345530Sbonwick 
38355530Sbonwick 	tx = dmu_tx_create(os);
383610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
383710922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
383810922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38395530Sbonwick 		return;
384010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
384110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3842789Sahrens 	dmu_tx_commit(tx);
3843789Sahrens }
3844789Sahrens 
384510431SSanjeev.Bagewadi@Sun.COM /*
384610431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
384710431SSanjeev.Bagewadi@Sun.COM  */
384810431SSanjeev.Bagewadi@Sun.COM void
384910922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
385010431SSanjeev.Bagewadi@Sun.COM {
385110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
385210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
385310922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
385410922SJeff.Bonwick@Sun.COM 
385510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
385610922SJeff.Bonwick@Sun.COM 
385710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
385810922SJeff.Bonwick@Sun.COM 		return;
385910922SJeff.Bonwick@Sun.COM 
386010922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
386110431SSanjeev.Bagewadi@Sun.COM 
386210431SSanjeev.Bagewadi@Sun.COM 	/*
386310922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
386410922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
386510922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
386610431SSanjeev.Bagewadi@Sun.COM 	 */
386710922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
386810922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
386910922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
387010922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
387110922SJeff.Bonwick@Sun.COM 		int error;
387210922SJeff.Bonwick@Sun.COM 
387310922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
387410922SJeff.Bonwick@Sun.COM 		    id, value);
387510431SSanjeev.Bagewadi@Sun.COM 
387610431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
387710922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
387810922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
387910922SJeff.Bonwick@Sun.COM 		if (txg == 0)
388010431SSanjeev.Bagewadi@Sun.COM 			return;
388110922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
388210922SJeff.Bonwick@Sun.COM 		    &value, tx);
388310431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
388410431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
388510431SSanjeev.Bagewadi@Sun.COM 	}
388610431SSanjeev.Bagewadi@Sun.COM }
388710431SSanjeev.Bagewadi@Sun.COM 
388810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3889789Sahrens void
389010922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3891789Sahrens {
389210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
389310922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3894789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3895789Sahrens 	dmu_tx_t *tx;
3896789Sahrens 	int i, namelen, error;
389710922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3898789Sahrens 	char name[20], string_value[20];
3899789Sahrens 	void *data;
3900789Sahrens 
390110922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
390210922SJeff.Bonwick@Sun.COM 
390310922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
390410922SJeff.Bonwick@Sun.COM 		return;
390510922SJeff.Bonwick@Sun.COM 
390610922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
390710922SJeff.Bonwick@Sun.COM 
39085530Sbonwick 	/*
39095530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
39105530Sbonwick 	 * x is a random printable character and the dots are dots.
39115530Sbonwick 	 * There are 94 such characters, and the name length goes from
39125530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
39135530Sbonwick 	 */
39145530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
39155530Sbonwick 
39165530Sbonwick 	for (i = 0; i < 3; i++)
39175530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
39185530Sbonwick 	for (; i < namelen - 1; i++)
39195530Sbonwick 		name[i] = '.';
39205530Sbonwick 	name[i] = '\0';
39215530Sbonwick 
392210922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
39235530Sbonwick 		wsize = sizeof (txg);
39245530Sbonwick 		wc = 1;
39255530Sbonwick 		data = &txg;
39265530Sbonwick 	} else {
39275530Sbonwick 		wsize = 1;
39285530Sbonwick 		wc = namelen;
39295530Sbonwick 		data = string_value;
39305530Sbonwick 	}
39315530Sbonwick 
39325530Sbonwick 	count = -1ULL;
39335530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
39345530Sbonwick 	ASSERT(count != -1ULL);
39355530Sbonwick 
39365530Sbonwick 	/*
39375530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
39385530Sbonwick 	 */
39395530Sbonwick 	i = ztest_random(5);
39405530Sbonwick 
39415530Sbonwick 	if (i >= 2) {
39425530Sbonwick 		tx = dmu_tx_create(os);
394310922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
394410922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
394510922SJeff.Bonwick@Sun.COM 		if (txg == 0)
39465530Sbonwick 			return;
39475530Sbonwick 		bcopy(name, string_value, namelen);
39485530Sbonwick 	} else {
39495530Sbonwick 		tx = NULL;
39505530Sbonwick 		txg = 0;
39515530Sbonwick 		bzero(string_value, namelen);
39525530Sbonwick 	}
39535530Sbonwick 
39545530Sbonwick 	switch (i) {
39555530Sbonwick 
39565530Sbonwick 	case 0:
39575530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
39585530Sbonwick 		if (error == 0) {
39595530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
39605530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3961789Sahrens 		} else {
39625530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3963789Sahrens 		}
39645530Sbonwick 		break;
39655530Sbonwick 
39665530Sbonwick 	case 1:
39675530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
39685530Sbonwick 		if (error == 0) {
39695530Sbonwick 			if (data == string_value &&
39705530Sbonwick 			    bcmp(name, data, namelen) != 0)
39715530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
39725530Sbonwick 				    name, data, namelen);
39735530Sbonwick 		} else {
39745530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3975789Sahrens 		}
39765530Sbonwick 		break;
39775530Sbonwick 
39785530Sbonwick 	case 2:
39795530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
39805530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
39815530Sbonwick 		break;
39825530Sbonwick 
39835530Sbonwick 	case 3:
39845530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
39855530Sbonwick 		break;
39865530Sbonwick 
39875530Sbonwick 	case 4:
39885530Sbonwick 		error = zap_remove(os, object, name, tx);
39895530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
39905530Sbonwick 		break;
3991789Sahrens 	}
39925530Sbonwick 
39935530Sbonwick 	if (tx != NULL)
39945530Sbonwick 		dmu_tx_commit(tx);
3995789Sahrens }
3996789Sahrens 
399710612SRicardo.M.Correia@Sun.COM /*
399810612SRicardo.M.Correia@Sun.COM  * Commit callback data.
399910612SRicardo.M.Correia@Sun.COM  */
400010612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
400110612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
400210612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
400310612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
400410612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
400510612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
400610612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
400710612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
400810612SRicardo.M.Correia@Sun.COM 
400910612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
401010612SRicardo.M.Correia@Sun.COM static void
401110612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
401210612SRicardo.M.Correia@Sun.COM {
401310612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
401410612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
401510612SRicardo.M.Correia@Sun.COM 
401610612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
401710612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
401810612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
401910612SRicardo.M.Correia@Sun.COM 
402010612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
402110612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
402210612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
402310612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
402410612SRicardo.M.Correia@Sun.COM 		    synced_txg);
402510612SRicardo.M.Correia@Sun.COM 
402610612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
402710612SRicardo.M.Correia@Sun.COM 
402810612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
402910612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
403010612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
403110612SRicardo.M.Correia@Sun.COM 
403210612SRicardo.M.Correia@Sun.COM 		/*
403310612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
403410612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
403510612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
403610612SRicardo.M.Correia@Sun.COM 		 */
403710612SRicardo.M.Correia@Sun.COM 		return;
403810612SRicardo.M.Correia@Sun.COM 	}
403910612SRicardo.M.Correia@Sun.COM 
404010612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
404110612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
404210612SRicardo.M.Correia@Sun.COM 		goto out;
404310612SRicardo.M.Correia@Sun.COM 
404410612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
404510612SRicardo.M.Correia@Sun.COM 
404610612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
404710612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
404810612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
404910612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
405010612SRicardo.M.Correia@Sun.COM 
405110612SRicardo.M.Correia@Sun.COM out:
405210612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
405310612SRicardo.M.Correia@Sun.COM }
405410612SRicardo.M.Correia@Sun.COM 
405510612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
405610612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
405710612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
405810612SRicardo.M.Correia@Sun.COM {
405910612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
406010612SRicardo.M.Correia@Sun.COM 
406110612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
406210612SRicardo.M.Correia@Sun.COM 
406310612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
406410612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
406510612SRicardo.M.Correia@Sun.COM 
406610612SRicardo.M.Correia@Sun.COM 	return (cb_data);
406710612SRicardo.M.Correia@Sun.COM }
406810612SRicardo.M.Correia@Sun.COM 
406910612SRicardo.M.Correia@Sun.COM /*
407010612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
407110612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
407210612SRicardo.M.Correia@Sun.COM  * implementation bug.
407310612SRicardo.M.Correia@Sun.COM  */
407410612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
407510612SRicardo.M.Correia@Sun.COM 
407610612SRicardo.M.Correia@Sun.COM /*
407710612SRicardo.M.Correia@Sun.COM  * Commit callback test.
407810612SRicardo.M.Correia@Sun.COM  */
407910612SRicardo.M.Correia@Sun.COM void
408010922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
408110612SRicardo.M.Correia@Sun.COM {
408210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
408310922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
408410612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
408510612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
408610612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
408710612SRicardo.M.Correia@Sun.COM 	int i, error;
408810612SRicardo.M.Correia@Sun.COM 
408910922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
409010922SJeff.Bonwick@Sun.COM 
409110922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
409210922SJeff.Bonwick@Sun.COM 		return;
409310922SJeff.Bonwick@Sun.COM 
409410612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
409510612SRicardo.M.Correia@Sun.COM 
409610612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
409710612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
409810612SRicardo.M.Correia@Sun.COM 
409910922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
410010612SRicardo.M.Correia@Sun.COM 
410110612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
410210612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
410310612SRicardo.M.Correia@Sun.COM 		error = -1;
410410612SRicardo.M.Correia@Sun.COM 
410510612SRicardo.M.Correia@Sun.COM 	if (!error)
410610612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
410710612SRicardo.M.Correia@Sun.COM 
410810612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
410910612SRicardo.M.Correia@Sun.COM 
411010612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
411110612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
411210612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
411310612SRicardo.M.Correia@Sun.COM 
411410612SRicardo.M.Correia@Sun.COM 	if (error) {
411510612SRicardo.M.Correia@Sun.COM 		/*
411610612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
411710612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
411810612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
411910612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
412010612SRicardo.M.Correia@Sun.COM 		 */
412110612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
412210612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
412310612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
412410612SRicardo.M.Correia@Sun.COM 		}
412510612SRicardo.M.Correia@Sun.COM 
412610612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
412710612SRicardo.M.Correia@Sun.COM 
412810612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
412910612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
413010612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
413110612SRicardo.M.Correia@Sun.COM 		}
413210612SRicardo.M.Correia@Sun.COM 
413310612SRicardo.M.Correia@Sun.COM 		return;
413410612SRicardo.M.Correia@Sun.COM 	}
413510612SRicardo.M.Correia@Sun.COM 
413610612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
413710612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
413810612SRicardo.M.Correia@Sun.COM 
413910612SRicardo.M.Correia@Sun.COM 	/*
414010612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
414110612SRicardo.M.Correia@Sun.COM 	 */
414210922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
414310612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
414410612SRicardo.M.Correia@Sun.COM 
414510612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
414610612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
414710612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
414810612SRicardo.M.Correia@Sun.COM 
414910922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
415010612SRicardo.M.Correia@Sun.COM 
415110612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
415210612SRicardo.M.Correia@Sun.COM 
415310612SRicardo.M.Correia@Sun.COM 	/*
415410612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
415510612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
415610612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
415710612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
415810612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
415910612SRicardo.M.Correia@Sun.COM 	 * implementation.
416010612SRicardo.M.Correia@Sun.COM 	 *
416110612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
416210612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
416310612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
416410612SRicardo.M.Correia@Sun.COM 	 */
416510612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
416610612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
416710612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
416810612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
416910612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
417010612SRicardo.M.Correia@Sun.COM 	}
417110612SRicardo.M.Correia@Sun.COM 
417210612SRicardo.M.Correia@Sun.COM 	/*
417310612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
417410612SRicardo.M.Correia@Sun.COM 	 *
417510612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
417610612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
417710612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
417810612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
417910612SRicardo.M.Correia@Sun.COM 	 */
418010612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
418110612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
418210612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
418310612SRicardo.M.Correia@Sun.COM 
418410612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
418510612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
418610612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
418710612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
418810612SRicardo.M.Correia@Sun.COM 		else
418910612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
419010612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
419110612SRicardo.M.Correia@Sun.COM 
419210612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
419310612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
419410612SRicardo.M.Correia@Sun.COM 
419510612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
419610612SRicardo.M.Correia@Sun.COM 	}
419710612SRicardo.M.Correia@Sun.COM 
419810612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
419910612SRicardo.M.Correia@Sun.COM 
420010612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
420110612SRicardo.M.Correia@Sun.COM }
420210612SRicardo.M.Correia@Sun.COM 
420310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4204789Sahrens void
420510922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4206789Sahrens {
420710922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
420810922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
420910922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
421010922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
421110922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
421210922SJeff.Bonwick@Sun.COM 	};
421310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
421410922SJeff.Bonwick@Sun.COM 
421510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
421610922SJeff.Bonwick@Sun.COM 
421710922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
421810922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
421910922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
422010922SJeff.Bonwick@Sun.COM 
422110922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
422210922SJeff.Bonwick@Sun.COM }
422310922SJeff.Bonwick@Sun.COM 
422410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
422510922SJeff.Bonwick@Sun.COM void
422610922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
422710922SJeff.Bonwick@Sun.COM {
422810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
422910922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
423010922SJeff.Bonwick@Sun.COM 
423110922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
423210922SJeff.Bonwick@Sun.COM 
423310922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
423410922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
423510922SJeff.Bonwick@Sun.COM 
423610922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
423710922SJeff.Bonwick@Sun.COM 
423810922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
423910922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
424010922SJeff.Bonwick@Sun.COM 
424110922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
424210922SJeff.Bonwick@Sun.COM 
424310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4244789Sahrens }
4245789Sahrens 
4246789Sahrens /*
424710693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
424810693Schris.kirby@sun.com  */
424910693Schris.kirby@sun.com void
425010922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
425110693Schris.kirby@sun.com {
425210693Schris.kirby@sun.com 	int error;
425310922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
425410693Schris.kirby@sun.com 	objset_t *origin;
425510693Schris.kirby@sun.com 	char snapname[100];
425610693Schris.kirby@sun.com 	char fullname[100];
425710693Schris.kirby@sun.com 	char clonename[100];
425810693Schris.kirby@sun.com 	char tag[100];
425910693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
426010693Schris.kirby@sun.com 
426110693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
426210693Schris.kirby@sun.com 
426310693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
426410693Schris.kirby@sun.com 
426510922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
426610693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
426710922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
426810922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
426910693Schris.kirby@sun.com 
427010693Schris.kirby@sun.com 	/*
427110693Schris.kirby@sun.com 	 * Clean up from any previous run.
427210693Schris.kirby@sun.com 	 */
427310693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
427410693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
427510693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
427610693Schris.kirby@sun.com 
427710693Schris.kirby@sun.com 	/*
427810693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
427910693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
428010693Schris.kirby@sun.com 	 */
428110693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
428210693Schris.kirby@sun.com 	if (error) {
428310693Schris.kirby@sun.com 		if (error == ENOSPC) {
428410693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
428510693Schris.kirby@sun.com 			goto out;
428610693Schris.kirby@sun.com 		}
428710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
428810693Schris.kirby@sun.com 	}
428910693Schris.kirby@sun.com 
429010693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
429110693Schris.kirby@sun.com 	if (error)
429210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
429310693Schris.kirby@sun.com 
429410693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
429510693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
429610693Schris.kirby@sun.com 	if (error) {
429710693Schris.kirby@sun.com 		if (error == ENOSPC) {
429810693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
429910693Schris.kirby@sun.com 			goto out;
430010693Schris.kirby@sun.com 		}
430110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
430210693Schris.kirby@sun.com 	}
430310693Schris.kirby@sun.com 
430410693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
430510693Schris.kirby@sun.com 	if (error) {
430610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
430710693Schris.kirby@sun.com 		    fullname, error);
430810693Schris.kirby@sun.com 	}
430910693Schris.kirby@sun.com 
431010693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
431110693Schris.kirby@sun.com 	if (error)
431210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
431310693Schris.kirby@sun.com 
431410693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
431510693Schris.kirby@sun.com 	if (error != ENOENT)
431610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
431710693Schris.kirby@sun.com 
431810693Schris.kirby@sun.com 	/*
431910693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
432010693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
432110693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
432210693Schris.kirby@sun.com 	 */
432310693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
432410693Schris.kirby@sun.com 	if (error) {
432510693Schris.kirby@sun.com 		if (error == ENOSPC) {
432610693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
432710693Schris.kirby@sun.com 			goto out;
432810693Schris.kirby@sun.com 		}
432910693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
433010693Schris.kirby@sun.com 	}
433110693Schris.kirby@sun.com 
433210693Schris.kirby@sun.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE, B_TRUE);
433310693Schris.kirby@sun.com 	if (error)
433410693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
433510693Schris.kirby@sun.com 
433610693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
433710693Schris.kirby@sun.com 	if (error != EBUSY) {
433810693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
433910693Schris.kirby@sun.com 		    fullname, error);
434010693Schris.kirby@sun.com 	}
434110693Schris.kirby@sun.com 
434210693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
434310693Schris.kirby@sun.com 	if (error) {
434410693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
434510693Schris.kirby@sun.com 		    fullname, error);
434610693Schris.kirby@sun.com 	}
434710693Schris.kirby@sun.com 
434810693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
434910693Schris.kirby@sun.com 	if (error)
435010693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
435110693Schris.kirby@sun.com 
435210693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
435310693Schris.kirby@sun.com 
435410693Schris.kirby@sun.com out:
435510693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
435610693Schris.kirby@sun.com }
435710693Schris.kirby@sun.com 
435810693Schris.kirby@sun.com /*
4359789Sahrens  * Inject random faults into the on-disk data.
4360789Sahrens  */
436110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4362789Sahrens void
436310922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4364789Sahrens {
436510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
436610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4367789Sahrens 	int fd;
4368789Sahrens 	uint64_t offset;
436911422SMark.Musante@Sun.COM 	uint64_t leaves;
4370789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4371789Sahrens 	uint64_t top, leaf;
4372789Sahrens 	char path0[MAXPATHLEN];
4373789Sahrens 	char pathrand[MAXPATHLEN];
4374789Sahrens 	size_t fsize;
4375789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4376789Sahrens 	int iters = 1000;
437711422SMark.Musante@Sun.COM 	int maxfaults;
437811422SMark.Musante@Sun.COM 	int mirror_save;
43797754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
43801544Seschrock 	uint64_t guid0 = 0;
438110685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
43821544Seschrock 
438311422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
438411422SMark.Musante@Sun.COM 	maxfaults = MAXFAULTS();
438511422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
438611422SMark.Musante@Sun.COM 	mirror_save = zs->zs_mirrors;
438711422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
438811422SMark.Musante@Sun.COM 
43897754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4390789Sahrens 
4391789Sahrens 	/*
43927754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4393789Sahrens 	 */
43947754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
43957754SJeff.Bonwick@Sun.COM 
43967754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
43977754SJeff.Bonwick@Sun.COM 		/*
439810922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
43997754SJeff.Bonwick@Sun.COM 		 */
440010922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
440111422SMark.Musante@Sun.COM 		leaf = ztest_random(leaves) + zs->zs_splits;
44027754SJeff.Bonwick@Sun.COM 
44037754SJeff.Bonwick@Sun.COM 		/*
44047754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
44057754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
44067754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
44077754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
44087754SJeff.Bonwick@Sun.COM 		 */
44097754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
441011422SMark.Musante@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
44117754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
44127754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
44137754SJeff.Bonwick@Sun.COM 
44147754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
441510685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
441610685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
441710685SGeorge.Wilson@Sun.COM 
44187754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
44197754SJeff.Bonwick@Sun.COM 			/*
44207754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
44217754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
44227754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
44237754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
44247754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
44257754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
44267754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
44277754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
44287754SJeff.Bonwick@Sun.COM 			 */
44297754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
44307754SJeff.Bonwick@Sun.COM 
44317754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
44327754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
44337754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
44347754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
44357754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
44367754SJeff.Bonwick@Sun.COM 			} else {
44377754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
44387754SJeff.Bonwick@Sun.COM 			}
44397754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
44407754SJeff.Bonwick@Sun.COM 		}
44417754SJeff.Bonwick@Sun.COM 	} else {
44427754SJeff.Bonwick@Sun.COM 		/*
44437754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
44447754SJeff.Bonwick@Sun.COM 		 */
44457754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
44467754SJeff.Bonwick@Sun.COM 
44477754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
44487754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
44497754SJeff.Bonwick@Sun.COM 			return;
44507754SJeff.Bonwick@Sun.COM 		}
44517754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
44527754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
44537754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
44547754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
44557754SJeff.Bonwick@Sun.COM 
44567754SJeff.Bonwick@Sun.COM 		leaf = 0;
44577754SJeff.Bonwick@Sun.COM 		leaves = 1;
44587754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
44597754SJeff.Bonwick@Sun.COM 	}
4460789Sahrens 
44617754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
44627754SJeff.Bonwick@Sun.COM 
44631544Seschrock 	/*
446410685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
446510685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
44661544Seschrock 	 */
446710685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
44688241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
44698241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
44708241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
447110685SGeorge.Wilson@Sun.COM 
447210685SGeorge.Wilson@Sun.COM 			/*
447310685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
447410685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
447510685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
447610685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
447710685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
447810685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
447910685SGeorge.Wilson@Sun.COM 			 */
448010685SGeorge.Wilson@Sun.COM 			if (islog)
448110685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
448210685SGeorge.Wilson@Sun.COM 
44838241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
448410685SGeorge.Wilson@Sun.COM 
448510685SGeorge.Wilson@Sun.COM 			if (islog)
448610685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
44878241SJeff.Bonwick@Sun.COM 		} else {
44888241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
44898241SJeff.Bonwick@Sun.COM 		}
4490789Sahrens 	}
4491789Sahrens 
449210685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
449310685SGeorge.Wilson@Sun.COM 		return;
449410685SGeorge.Wilson@Sun.COM 
4495789Sahrens 	/*
44961544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4497789Sahrens 	 */
4498789Sahrens 	fd = open(pathrand, O_RDWR);
4499789Sahrens 
4500789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4501789Sahrens 		return;
4502789Sahrens 
4503789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4504789Sahrens 
4505789Sahrens 	while (--iters != 0) {
4506789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4507789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4508789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4509789Sahrens 
4510789Sahrens 		if (offset >= fsize)
4511789Sahrens 			continue;
4512789Sahrens 
451311422SMark.Musante@Sun.COM 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
451411422SMark.Musante@Sun.COM 		if (mirror_save != zs->zs_mirrors) {
451511422SMark.Musante@Sun.COM 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
451611422SMark.Musante@Sun.COM 			(void) close(fd);
451711422SMark.Musante@Sun.COM 			return;
451811422SMark.Musante@Sun.COM 		}
4519789Sahrens 
4520789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4521789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4522789Sahrens 			    offset, pathrand);
452311422SMark.Musante@Sun.COM 
452411422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
452511422SMark.Musante@Sun.COM 
452611422SMark.Musante@Sun.COM 		if (zopt_verbose >= 7)
452711422SMark.Musante@Sun.COM 			(void) printf("injected bad word into %s,"
452811422SMark.Musante@Sun.COM 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4529789Sahrens 	}
4530789Sahrens 
4531789Sahrens 	(void) close(fd);
4532789Sahrens }
4533789Sahrens 
4534789Sahrens /*
453510922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4536789Sahrens  */
4537789Sahrens void
453810922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4539789Sahrens {
454010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
454110922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
454210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
454310922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
454410922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
454510922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
454610922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
454710922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
454810922SJeff.Bonwick@Sun.COM 	void *buf;
454910922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
455010922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
455110922SJeff.Bonwick@Sun.COM 
455210922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
455310922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
455410922SJeff.Bonwick@Sun.COM 
455510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
455610922SJeff.Bonwick@Sun.COM 
455710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
455810922SJeff.Bonwick@Sun.COM 		return;
455910922SJeff.Bonwick@Sun.COM 
456010922SJeff.Bonwick@Sun.COM 	/*
456110922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
456210922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
456310922SJeff.Bonwick@Sun.COM 	 */
456410922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
456510922SJeff.Bonwick@Sun.COM 
456610922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
456710922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
456810922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
456910922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
457010922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
457110922SJeff.Bonwick@Sun.COM 		return;
457210922SJeff.Bonwick@Sun.COM 	}
457310922SJeff.Bonwick@Sun.COM 
457410922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
457510922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
457610922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
457710922SJeff.Bonwick@Sun.COM 
457810922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
457910922SJeff.Bonwick@Sun.COM 
458010922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
458110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
458210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
458310922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
458410922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
458510922SJeff.Bonwick@Sun.COM 		return;
458610922SJeff.Bonwick@Sun.COM 	}
458710922SJeff.Bonwick@Sun.COM 
458810922SJeff.Bonwick@Sun.COM 	/*
458910922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
459010922SJeff.Bonwick@Sun.COM 	 */
459110922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
459210922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
459310922SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db) == 0);
459410922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
459510922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
459610922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
459710922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
459810922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
459910922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
460010922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
460110922SJeff.Bonwick@Sun.COM 	}
460210922SJeff.Bonwick@Sun.COM 
460310922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
460410922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
460510922SJeff.Bonwick@Sun.COM 
460610922SJeff.Bonwick@Sun.COM 	/*
460710922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
460810922SJeff.Bonwick@Sun.COM 	 */
460910922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db) == 0);
461010922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
461110922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
461210922SJeff.Bonwick@Sun.COM 
461310922SJeff.Bonwick@Sun.COM 	/*
461410922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
461510922SJeff.Bonwick@Sun.COM 	 */
461610922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
461710922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
461810922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
461910922SJeff.Bonwick@Sun.COM 
462010922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
462110922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
462210922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
462310922SJeff.Bonwick@Sun.COM 
462410922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
462510922SJeff.Bonwick@Sun.COM 
462610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
462710922SJeff.Bonwick@Sun.COM }
462810922SJeff.Bonwick@Sun.COM 
462910922SJeff.Bonwick@Sun.COM /*
463010922SJeff.Bonwick@Sun.COM  * Scrub the pool.
463110922SJeff.Bonwick@Sun.COM  */
463210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
463310922SJeff.Bonwick@Sun.COM void
463410922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
463510922SJeff.Bonwick@Sun.COM {
463610922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
463710922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4638789Sahrens 
46397046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
464010922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
46417046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
4642789Sahrens }
4643789Sahrens 
4644789Sahrens /*
4645789Sahrens  * Rename the pool to a different name and then rename it back.
4646789Sahrens  */
464710922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4648789Sahrens void
464910922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4650789Sahrens {
465110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4652789Sahrens 	char *oldname, *newname;
4653789Sahrens 	spa_t *spa;
4654789Sahrens 
465510922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
465610922SJeff.Bonwick@Sun.COM 
465710922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4658789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4659789Sahrens 	(void) strcpy(newname, oldname);
4660789Sahrens 	(void) strcat(newname, "_tmp");
4661789Sahrens 
4662789Sahrens 	/*
4663789Sahrens 	 * Do the rename
4664789Sahrens 	 */
466510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4666789Sahrens 
4667789Sahrens 	/*
4668789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4669789Sahrens 	 */
467010922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4671789Sahrens 
4672789Sahrens 	/*
4673789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4674789Sahrens 	 */
467510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
467610922SJeff.Bonwick@Sun.COM 
467710922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4678789Sahrens 	spa_close(spa, FTAG);
4679789Sahrens 
4680789Sahrens 	/*
4681789Sahrens 	 * Rename it back to the original
4682789Sahrens 	 */
468310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4684789Sahrens 
4685789Sahrens 	/*
4686789Sahrens 	 * Make sure it can still be opened
4687789Sahrens 	 */
468810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
468910922SJeff.Bonwick@Sun.COM 
469010922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4691789Sahrens 	spa_close(spa, FTAG);
4692789Sahrens 
4693789Sahrens 	umem_free(newname, strlen(newname) + 1);
4694789Sahrens 
469510922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4696789Sahrens }
4697789Sahrens 
4698789Sahrens /*
469910922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4700789Sahrens  */
4701789Sahrens static void
470210922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4703789Sahrens {
4704789Sahrens 	int status;
4705789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4706789Sahrens 	char zbuf[1024];
4707789Sahrens 	char *bin;
47084527Sperrin 	char *ztest;
47094527Sperrin 	char *isa;
47104527Sperrin 	int isalen;
4711789Sahrens 	FILE *fp;
4712789Sahrens 
4713789Sahrens 	(void) realpath(getexecname(), zdb);
4714789Sahrens 
4715789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4716789Sahrens 	bin = strstr(zdb, "/usr/bin/");
47174527Sperrin 	ztest = strstr(bin, "/ztest");
47184527Sperrin 	isa = bin + 8;
47194527Sperrin 	isalen = ztest - isa;
47204527Sperrin 	isa = strdup(isa);
4721789Sahrens 	/* LINTED */
47225994Sck153898 	(void) sprintf(bin,
4723*11811SJeff.Bonwick@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
47244527Sperrin 	    isalen,
47254527Sperrin 	    isa,
4726789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4727789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
4728*11811SJeff.Bonwick@Sun.COM 	    spa_config_path,
47297837SMatthew.Ahrens@Sun.COM 	    pool);
47304527Sperrin 	free(isa);
4731789Sahrens 
4732789Sahrens 	if (zopt_verbose >= 5)
4733789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4734789Sahrens 
4735789Sahrens 	fp = popen(zdb, "r");
4736789Sahrens 
4737789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4738789Sahrens 		if (zopt_verbose >= 3)
4739789Sahrens 			(void) printf("%s", zbuf);
4740789Sahrens 
4741789Sahrens 	status = pclose(fp);
4742789Sahrens 
4743789Sahrens 	if (status == 0)
4744789Sahrens 		return;
4745789Sahrens 
4746789Sahrens 	ztest_dump_core = 0;
4747789Sahrens 	if (WIFEXITED(status))
4748789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4749789Sahrens 	else
4750789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4751789Sahrens }
4752789Sahrens 
4753789Sahrens static void
4754789Sahrens ztest_walk_pool_directory(char *header)
4755789Sahrens {
4756789Sahrens 	spa_t *spa = NULL;
4757789Sahrens 
4758789Sahrens 	if (zopt_verbose >= 6)
4759789Sahrens 		(void) printf("%s\n", header);
4760789Sahrens 
4761789Sahrens 	mutex_enter(&spa_namespace_lock);
4762789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4763789Sahrens 		if (zopt_verbose >= 6)
4764789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4765789Sahrens 	mutex_exit(&spa_namespace_lock);
4766789Sahrens }
4767789Sahrens 
4768789Sahrens static void
4769789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4770789Sahrens {
47718241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4772789Sahrens 	uint64_t pool_guid;
4773789Sahrens 	spa_t *spa;
4774789Sahrens 
4775789Sahrens 	if (zopt_verbose >= 4) {
4776789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4777789Sahrens 		    oldname, newname);
4778789Sahrens 	}
4779789Sahrens 
4780789Sahrens 	/*
4781789Sahrens 	 * Clean up from previous runs.
4782789Sahrens 	 */
4783789Sahrens 	(void) spa_destroy(newname);
4784789Sahrens 
4785789Sahrens 	/*
4786789Sahrens 	 * Get the pool's configuration and guid.
4787789Sahrens 	 */
478810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4789789Sahrens 
47908241SJeff.Bonwick@Sun.COM 	/*
47918241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
47928241SJeff.Bonwick@Sun.COM 	 */
47938241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
47948241SJeff.Bonwick@Sun.COM 		(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
47958241SJeff.Bonwick@Sun.COM 
4796789Sahrens 	pool_guid = spa_guid(spa);
4797789Sahrens 	spa_close(spa, FTAG);
4798789Sahrens 
4799789Sahrens 	ztest_walk_pool_directory("pools before export");
4800789Sahrens 
4801789Sahrens 	/*
4802789Sahrens 	 * Export it.
4803789Sahrens 	 */
480410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4805789Sahrens 
4806789Sahrens 	ztest_walk_pool_directory("pools after export");
4807789Sahrens 
4808789Sahrens 	/*
48098241SJeff.Bonwick@Sun.COM 	 * Try to import it.
48108241SJeff.Bonwick@Sun.COM 	 */
48118241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
48128241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
48138241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
48148241SJeff.Bonwick@Sun.COM 
48158241SJeff.Bonwick@Sun.COM 	/*
4816789Sahrens 	 * Import it under the new name.
4817789Sahrens 	 */
481810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL));
4819789Sahrens 
4820789Sahrens 	ztest_walk_pool_directory("pools after import");
4821789Sahrens 
4822789Sahrens 	/*
4823789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4824789Sahrens 	 */
482510922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL));
4826789Sahrens 
4827789Sahrens 	/*
4828789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4829789Sahrens 	 */
483010922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL));
4831789Sahrens 
4832789Sahrens 	/*
4833789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4834789Sahrens 	 */
483510922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4836789Sahrens 
4837789Sahrens 	/*
4838789Sahrens 	 * Verify that we can open and close the pool using the new name.
4839789Sahrens 	 */
484010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4841789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4842789Sahrens 	spa_close(spa, FTAG);
4843789Sahrens 
4844789Sahrens 	nvlist_free(config);
4845789Sahrens }
4846789Sahrens 
48478241SJeff.Bonwick@Sun.COM static void
48488241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
48498241SJeff.Bonwick@Sun.COM {
485010922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
485110922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
485210922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
485310922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
485410922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
485510922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
48568241SJeff.Bonwick@Sun.COM }
48578241SJeff.Bonwick@Sun.COM 
48585329Sgw25295 static void *
48598241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
48605329Sgw25295 {
48617754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
48625329Sgw25295 
48635329Sgw25295 	while (!ztest_exiting) {
486410922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
486510922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
486610922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
48675329Sgw25295 	}
48685329Sgw25295 	return (NULL);
48695329Sgw25295 }
48705329Sgw25295 
4871789Sahrens static void *
487210922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
487310922SJeff.Bonwick@Sun.COM {
487410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
487510922SJeff.Bonwick@Sun.COM 	int grace = 300;
487610922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
487710922SJeff.Bonwick@Sun.COM 
487810922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
487910922SJeff.Bonwick@Sun.COM 
488010922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
488110922SJeff.Bonwick@Sun.COM 
488210922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
488310922SJeff.Bonwick@Sun.COM 
488410922SJeff.Bonwick@Sun.COM 	return (NULL);
488510922SJeff.Bonwick@Sun.COM }
488610922SJeff.Bonwick@Sun.COM 
488710922SJeff.Bonwick@Sun.COM static void
488810922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
488910922SJeff.Bonwick@Sun.COM {
489010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
489110922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
489210922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
489310922SJeff.Bonwick@Sun.COM 
489410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
489510922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
489610922SJeff.Bonwick@Sun.COM 
489710922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
489810922SJeff.Bonwick@Sun.COM 
489910922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
490010922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
490110922SJeff.Bonwick@Sun.COM 
490210922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
490310922SJeff.Bonwick@Sun.COM 		Dl_info dli;
490410922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
490510922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
490610922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
490710922SJeff.Bonwick@Sun.COM 	}
490810922SJeff.Bonwick@Sun.COM }
490910922SJeff.Bonwick@Sun.COM 
491010922SJeff.Bonwick@Sun.COM static void *
4911789Sahrens ztest_thread(void *arg)
4912789Sahrens {
491310922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4914789Sahrens 	ztest_shared_t *zs = ztest_shared;
491510922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
491610922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4917789Sahrens 	ztest_info_t *zi;
491810922SJeff.Bonwick@Sun.COM 
491910922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4920789Sahrens 		/*
4921789Sahrens 		 * See if it's time to force a crash.
4922789Sahrens 		 */
492310922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
492410922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4925789Sahrens 
4926789Sahrens 		/*
4927789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4928789Sahrens 		 */
4929789Sahrens 		if (zs->zs_enospc_count > 10)
4930789Sahrens 			break;
493110922SJeff.Bonwick@Sun.COM 
493210922SJeff.Bonwick@Sun.COM 		/*
493310922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
493410922SJeff.Bonwick@Sun.COM 		 */
493510922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
493610922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
493710922SJeff.Bonwick@Sun.COM 
493810922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
493910922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
494010922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
494110922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4942789Sahrens 	}
4943789Sahrens 
4944789Sahrens 	return (NULL);
4945789Sahrens }
4946789Sahrens 
494710922SJeff.Bonwick@Sun.COM static void
494810922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
494910922SJeff.Bonwick@Sun.COM {
495010922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
495110922SJeff.Bonwick@Sun.COM }
495210922SJeff.Bonwick@Sun.COM 
495310922SJeff.Bonwick@Sun.COM static void
495410922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
495510922SJeff.Bonwick@Sun.COM {
495610922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
495710922SJeff.Bonwick@Sun.COM 
495810922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
495910922SJeff.Bonwick@Sun.COM 
496010922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
496110922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
496210922SJeff.Bonwick@Sun.COM 
496310922SJeff.Bonwick@Sun.COM 	/*
496410922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
496510922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
496610922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
496710922SJeff.Bonwick@Sun.COM 	 */
496810922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
496910922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
497010922SJeff.Bonwick@Sun.COM 
497110922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
497210922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
497310922SJeff.Bonwick@Sun.COM }
497410922SJeff.Bonwick@Sun.COM 
497510922SJeff.Bonwick@Sun.COM static void
497610922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
497710922SJeff.Bonwick@Sun.COM {
497810922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
497910922SJeff.Bonwick@Sun.COM 
498010922SJeff.Bonwick@Sun.COM 	/*
498110922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
498210922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
498310922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
498410922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
498510922SJeff.Bonwick@Sun.COM 	 *
498610922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
498710922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
498810922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
498910922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
499010922SJeff.Bonwick@Sun.COM 	 */
499110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
499210922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
499310922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
499410922SJeff.Bonwick@Sun.COM }
499510922SJeff.Bonwick@Sun.COM 
499610922SJeff.Bonwick@Sun.COM static int
499710922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
499810922SJeff.Bonwick@Sun.COM {
499910922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
500010922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
500110922SJeff.Bonwick@Sun.COM 	objset_t *os;
500210922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
500310922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
500410922SJeff.Bonwick@Sun.COM 	int error;
500510922SJeff.Bonwick@Sun.COM 
500610922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
500710922SJeff.Bonwick@Sun.COM 
500810922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
500910922SJeff.Bonwick@Sun.COM 
501010922SJeff.Bonwick@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
501110922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
501210922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
501310922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
501410922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
501510922SJeff.Bonwick@Sun.COM 		return (error);
501610922SJeff.Bonwick@Sun.COM 	}
501710922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
501810922SJeff.Bonwick@Sun.COM 
501910922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
502010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
502110922SJeff.Bonwick@Sun.COM 
502210922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
502310922SJeff.Bonwick@Sun.COM 
502410922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
502510922SJeff.Bonwick@Sun.COM 
502610922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
502710922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
502810922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
502910922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
503010922SJeff.Bonwick@Sun.COM 
503110922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
503210922SJeff.Bonwick@Sun.COM 
503310922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
503410922SJeff.Bonwick@Sun.COM 
503510922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
503610922SJeff.Bonwick@Sun.COM 
503710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
503810922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
503910922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
504010922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
504110922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
504210922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
504310922SJeff.Bonwick@Sun.COM 
504410922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
504510922SJeff.Bonwick@Sun.COM 
504610922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
504710922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
504810922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
504910922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
505010922SJeff.Bonwick@Sun.COM 
505110922SJeff.Bonwick@Sun.COM 	return (0);
505210922SJeff.Bonwick@Sun.COM }
505310922SJeff.Bonwick@Sun.COM 
505410922SJeff.Bonwick@Sun.COM static void
505510922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
505610922SJeff.Bonwick@Sun.COM {
505710922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
505810922SJeff.Bonwick@Sun.COM 
505910922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
506010922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
506110922SJeff.Bonwick@Sun.COM 
506210922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
506310922SJeff.Bonwick@Sun.COM }
506410922SJeff.Bonwick@Sun.COM 
5065789Sahrens /*
5066789Sahrens  * Kick off threads to run tests on all datasets in parallel.
5067789Sahrens  */
5068789Sahrens static void
506910922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
5070789Sahrens {
507110922SJeff.Bonwick@Sun.COM 	thread_t *tid;
5072789Sahrens 	spa_t *spa;
50737754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
507410922SJeff.Bonwick@Sun.COM 	int error;
50757754SJeff.Bonwick@Sun.COM 
50767754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
5077789Sahrens 
507810922SJeff.Bonwick@Sun.COM 	/*
507910922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
508010922SJeff.Bonwick@Sun.COM 	 */
508110922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
508210922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
508310922SJeff.Bonwick@Sun.COM 
508410922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
508510922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
508610922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
508710922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
508810922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
508910922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
509010922SJeff.Bonwick@Sun.COM 
509110922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
509210612SRicardo.M.Correia@Sun.COM 
509310612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
509410612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
509510612SRicardo.M.Correia@Sun.COM 
5096789Sahrens 	/*
50977754SJeff.Bonwick@Sun.COM 	 * Open our pool.
50985329Sgw25295 	 */
509910922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
510010922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
510110922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
510210922SJeff.Bonwick@Sun.COM 
510310922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
51045329Sgw25295 
51055329Sgw25295 	/*
51068241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
51078241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
51088241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
51098241SJeff.Bonwick@Sun.COM 	 */
511011422SMark.Musante@Sun.COM 	if (MAXFAULTS() == 0)
51118241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
51128241SJeff.Bonwick@Sun.COM 	else
51138241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
51148241SJeff.Bonwick@Sun.COM 
51158241SJeff.Bonwick@Sun.COM 	/*
51167754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5117789Sahrens 	 */
51188241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
51197754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5120789Sahrens 
5121789Sahrens 	/*
512210922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
512310922SJeff.Bonwick@Sun.COM 	 */
512410922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
512510922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
512610922SJeff.Bonwick@Sun.COM 
512710922SJeff.Bonwick@Sun.COM 	/*
5128789Sahrens 	 * Verify that we can safely inquire about about any object,
5129789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5130789Sahrens 	 * we probe a 5-wide window around each power of two.
5131789Sahrens 	 * This hits all edge cases, including zero and the max.
5132789Sahrens 	 */
513310922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
513410922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5135789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5136789Sahrens 			    (1ULL << t) + d, NULL);
51371544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
51381544Seschrock 			    error == EINVAL);
5139789Sahrens 		}
5140789Sahrens 	}
5141789Sahrens 
5142789Sahrens 	/*
514310922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5144789Sahrens 	 */
514510922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
514610922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
514710922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
514810922SJeff.Bonwick@Sun.COM 	}
5149789Sahrens 	zs->zs_enospc_count = 0;
5150789Sahrens 
515110922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5152789Sahrens 
5153789Sahrens 	if (zopt_verbose >= 4)
5154789Sahrens 		(void) printf("starting main threads...\n");
5155789Sahrens 
515610922SJeff.Bonwick@Sun.COM 	/*
515710922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
515810922SJeff.Bonwick@Sun.COM 	 */
515910922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
516010922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
516110922SJeff.Bonwick@Sun.COM 			return;
516210922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
516310922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5164789Sahrens 	}
5165789Sahrens 
5166789Sahrens 	/*
516710922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
516810922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5169789Sahrens 	 */
517010922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
517110922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
517210922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
517310922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5174789Sahrens 	}
5175789Sahrens 
51761544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5177789Sahrens 
517810922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
517910922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
518010922SJeff.Bonwick@Sun.COM 
518110922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
51827754SJeff.Bonwick@Sun.COM 
51837754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
51847754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
51857754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
51868241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
51877754SJeff.Bonwick@Sun.COM 
5188789Sahrens 	/*
5189789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5190789Sahrens 	 * spa_close() should wait for it to complete.
5191789Sahrens 	 */
519210922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
519310922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5194789Sahrens 
51955530Sbonwick 	spa_close(spa, FTAG);
51965530Sbonwick 
519710922SJeff.Bonwick@Sun.COM 	/*
519810922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
519910922SJeff.Bonwick@Sun.COM 	 */
520010922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
520110922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
520210922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
520310922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
520410922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
520510922SJeff.Bonwick@Sun.COM 
520610922SJeff.Bonwick@Sun.COM 	/*
520710922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
520810922SJeff.Bonwick@Sun.COM 	 * different name.
520910922SJeff.Bonwick@Sun.COM 	 */
521010922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
521110922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
521210922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
521310922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
521410922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
521510922SJeff.Bonwick@Sun.COM 	}
521610922SJeff.Bonwick@Sun.COM 
521710922SJeff.Bonwick@Sun.COM 	kernel_fini();
521810922SJeff.Bonwick@Sun.COM }
521910922SJeff.Bonwick@Sun.COM 
522010922SJeff.Bonwick@Sun.COM static void
522110922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
522210922SJeff.Bonwick@Sun.COM {
522310922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
522410922SJeff.Bonwick@Sun.COM 	spa_t *spa;
522510922SJeff.Bonwick@Sun.COM 
522610922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
522710922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
522810922SJeff.Bonwick@Sun.COM 
522910922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
523010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
523110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
523210922SJeff.Bonwick@Sun.COM 
523310922SJeff.Bonwick@Sun.COM 	/*
523410922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
523510922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
523610922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
523710922SJeff.Bonwick@Sun.COM 	 */
523810922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
523910922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
524010922SJeff.Bonwick@Sun.COM 		zil_commit(zd->zd_zilog, UINT64_MAX, 0);
524110922SJeff.Bonwick@Sun.COM 	}
524210922SJeff.Bonwick@Sun.COM 
524310922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
524410922SJeff.Bonwick@Sun.COM 
524510922SJeff.Bonwick@Sun.COM 	/*
524610922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
524710922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
524810922SJeff.Bonwick@Sun.COM 	 */
524910922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
525010922SJeff.Bonwick@Sun.COM 
525110922SJeff.Bonwick@Sun.COM 	/*
525210922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
525310922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
525410922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
525510922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
525610922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
525710922SJeff.Bonwick@Sun.COM 	 */
525810922SJeff.Bonwick@Sun.COM 	while (ztest_random(20) != 0) {
525910922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
526010922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
526110922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
526210922SJeff.Bonwick@Sun.COM 	}
526310922SJeff.Bonwick@Sun.COM 
526410922SJeff.Bonwick@Sun.COM 	/*
526510922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
526610922SJeff.Bonwick@Sun.COM 	 */
526710922SJeff.Bonwick@Sun.COM 	zil_commit(zd->zd_zilog, UINT64_MAX, 0);
526810922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
526910922SJeff.Bonwick@Sun.COM 
527010922SJeff.Bonwick@Sun.COM 	/*
527110922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
527210922SJeff.Bonwick@Sun.COM 	 */
527310922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
527410922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
527510922SJeff.Bonwick@Sun.COM 	kernel_fini();
527610922SJeff.Bonwick@Sun.COM 
527710922SJeff.Bonwick@Sun.COM 	/*
527810922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
527910922SJeff.Bonwick@Sun.COM 	 */
528010922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
528110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
528210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
528310922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
528410922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5285789Sahrens 	kernel_fini();
528610612SRicardo.M.Correia@Sun.COM 
528710612SRicardo.M.Correia@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
528810612SRicardo.M.Correia@Sun.COM 
528910612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
529010612SRicardo.M.Correia@Sun.COM 
529110612SRicardo.M.Correia@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
529210612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5293789Sahrens }
5294789Sahrens 
5295789Sahrens void
5296789Sahrens print_time(hrtime_t t, char *timebuf)
5297789Sahrens {
5298789Sahrens 	hrtime_t s = t / NANOSEC;
5299789Sahrens 	hrtime_t m = s / 60;
5300789Sahrens 	hrtime_t h = m / 60;
5301789Sahrens 	hrtime_t d = h / 24;
5302789Sahrens 
5303789Sahrens 	s -= m * 60;
5304789Sahrens 	m -= h * 60;
5305789Sahrens 	h -= d * 24;
5306789Sahrens 
5307789Sahrens 	timebuf[0] = '\0';
5308789Sahrens 
5309789Sahrens 	if (d)
5310789Sahrens 		(void) sprintf(timebuf,
5311789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5312789Sahrens 	else if (h)
5313789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5314789Sahrens 	else if (m)
5315789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5316789Sahrens 	else
5317789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5318789Sahrens }
5319789Sahrens 
532011422SMark.Musante@Sun.COM static nvlist_t *
532111422SMark.Musante@Sun.COM make_random_props()
532211422SMark.Musante@Sun.COM {
532311422SMark.Musante@Sun.COM 	nvlist_t *props;
532411422SMark.Musante@Sun.COM 
532511422SMark.Musante@Sun.COM 	if (ztest_random(2) == 0)
532611422SMark.Musante@Sun.COM 		return (NULL);
532711422SMark.Musante@Sun.COM 
532811422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
532911422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
533011422SMark.Musante@Sun.COM 
533111422SMark.Musante@Sun.COM 	(void) printf("props:\n");
533211422SMark.Musante@Sun.COM 	dump_nvlist(props, 4);
533311422SMark.Musante@Sun.COM 
533411422SMark.Musante@Sun.COM 	return (props);
533511422SMark.Musante@Sun.COM }
533611422SMark.Musante@Sun.COM 
5337789Sahrens /*
5338789Sahrens  * Create a storage pool with the given name and initial vdev size.
533910922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5340789Sahrens  */
5341789Sahrens static void
534210922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5343789Sahrens {
5344789Sahrens 	spa_t *spa;
534511422SMark.Musante@Sun.COM 	nvlist_t *nvroot, *props;
5346789Sahrens 
534710922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
534810922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
534910922SJeff.Bonwick@Sun.COM 
5350789Sahrens 	kernel_init(FREAD | FWRITE);
5351789Sahrens 
5352789Sahrens 	/*
5353789Sahrens 	 * Create the storage pool.
5354789Sahrens 	 */
535510922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
535610594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
535711422SMark.Musante@Sun.COM 	zs->zs_splits = 0;
535811422SMark.Musante@Sun.COM 	zs->zs_mirrors = zopt_mirrors;
53597754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
536011422SMark.Musante@Sun.COM 	    0, zopt_raidz, zs->zs_mirrors, 1);
536111422SMark.Musante@Sun.COM 	props = make_random_props();
536211422SMark.Musante@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5363789Sahrens 	nvlist_free(nvroot);
5364789Sahrens 
536510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
53669480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5367789Sahrens 	spa_close(spa, FTAG);
5368789Sahrens 
5369789Sahrens 	kernel_fini();
537010922SJeff.Bonwick@Sun.COM 
537110922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
537210922SJeff.Bonwick@Sun.COM 
537310922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
537410922SJeff.Bonwick@Sun.COM 
537510922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5376789Sahrens }
5377789Sahrens 
5378789Sahrens int
5379789Sahrens main(int argc, char **argv)
5380789Sahrens {
5381789Sahrens 	int kills = 0;
5382789Sahrens 	int iters = 0;
5383789Sahrens 	ztest_shared_t *zs;
538410922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5385789Sahrens 	ztest_info_t *zi;
5386789Sahrens 	char timebuf[100];
5387789Sahrens 	char numbuf[6];
538810922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5389789Sahrens 
5390789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5391789Sahrens 
5392789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5393789Sahrens 
5394789Sahrens 	process_options(argc, argv);
5395789Sahrens 
5396*11811SJeff.Bonwick@Sun.COM 	/* Override location of zpool.cache */
5397*11811SJeff.Bonwick@Sun.COM 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
5398*11811SJeff.Bonwick@Sun.COM 
53991544Seschrock 	/*
54001544Seschrock 	 * Blow away any existing copy of zpool.cache
54011544Seschrock 	 */
54021544Seschrock 	if (zopt_init != 0)
5403*11811SJeff.Bonwick@Sun.COM 		(void) remove(spa_config_path);
54041544Seschrock 
540510922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
540610922SJeff.Bonwick@Sun.COM 
5407789Sahrens 	zs = ztest_shared = (void *)mmap(0,
540810922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5409789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5410789Sahrens 
5411789Sahrens 	if (zopt_verbose >= 1) {
5412789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5413789Sahrens 		    " %llu seconds...\n",
54141732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5415789Sahrens 		    (u_longlong_t)zopt_time);
5416789Sahrens 	}
5417789Sahrens 
5418789Sahrens 	/*
5419789Sahrens 	 * Create and initialize our storage pool.
5420789Sahrens 	 */
542110922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5422789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5423789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5424789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
542510922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
542610922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5427789Sahrens 	}
5428789Sahrens 
542910922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
543010922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
543110922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
543210922SJeff.Bonwick@Sun.COM 
543310922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5434789Sahrens 		zi = &zs->zs_info[f];
5435789Sahrens 		*zi = ztest_info[f];
543610922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
543710922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5438789Sahrens 		else
543910922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
544010922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5441789Sahrens 	}
5442789Sahrens 
5443789Sahrens 	/*
5444789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5445789Sahrens 	 * to verify that self-healing data works, and forced crashes
5446789Sahrens 	 * to verify that we never lose on-disk consistency.
5447789Sahrens 	 */
544810922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5449789Sahrens 		int status;
5450789Sahrens 		pid_t pid;
5451789Sahrens 
5452789Sahrens 		/*
5453789Sahrens 		 * Initialize the workload counters for each function.
5454789Sahrens 		 */
545510922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5456789Sahrens 			zi = &zs->zs_info[f];
545710922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5458789Sahrens 			zi->zi_call_time = 0;
5459789Sahrens 		}
5460789Sahrens 
54619480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
54629480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
54639480SGeorge.Wilson@Sun.COM 
5464789Sahrens 		pid = fork();
5465789Sahrens 
5466789Sahrens 		if (pid == -1)
5467789Sahrens 			fatal(1, "fork failed");
5468789Sahrens 
5469789Sahrens 		if (pid == 0) {	/* child */
5470789Sahrens 			struct rlimit rl = { 1024, 1024 };
5471789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
54721914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
547310922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5474789Sahrens 			exit(0);
5475789Sahrens 		}
5476789Sahrens 
54772856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5478789Sahrens 			continue;
5479789Sahrens 
5480789Sahrens 		if (WIFEXITED(status)) {
5481789Sahrens 			if (WEXITSTATUS(status) != 0) {
5482789Sahrens 				(void) fprintf(stderr,
5483789Sahrens 				    "child exited with code %d\n",
5484789Sahrens 				    WEXITSTATUS(status));
5485789Sahrens 				exit(2);
5486789Sahrens 			}
54872856Snd150628 		} else if (WIFSIGNALED(status)) {
5488789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5489789Sahrens 				(void) fprintf(stderr,
5490789Sahrens 				    "child died with signal %d\n",
5491789Sahrens 				    WTERMSIG(status));
5492789Sahrens 				exit(3);
5493789Sahrens 			}
5494789Sahrens 			kills++;
54952856Snd150628 		} else {
54962856Snd150628 			(void) fprintf(stderr, "something strange happened "
54972856Snd150628 			    "to child\n");
54982856Snd150628 			exit(4);
5499789Sahrens 		}
5500789Sahrens 
5501789Sahrens 		iters++;
5502789Sahrens 
5503789Sahrens 		if (zopt_verbose >= 1) {
5504789Sahrens 			hrtime_t now = gethrtime();
5505789Sahrens 
550610922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
550710922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5508789Sahrens 			nicenum(zs->zs_space, numbuf);
5509789Sahrens 
5510789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5511789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5512789Sahrens 			    iters,
5513789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5514789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5515789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5516789Sahrens 			    numbuf,
551710922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5518789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5519789Sahrens 		}
5520789Sahrens 
5521789Sahrens 		if (zopt_verbose >= 2) {
5522789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5523789Sahrens 			(void) printf("%7s %9s   %s\n",
5524789Sahrens 			    "Calls", "Time", "Function");
5525789Sahrens 			(void) printf("%7s %9s   %s\n",
5526789Sahrens 			    "-----", "----", "--------");
552710922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5528789Sahrens 				Dl_info dli;
5529789Sahrens 
5530789Sahrens 				zi = &zs->zs_info[f];
5531789Sahrens 				print_time(zi->zi_call_time, timebuf);
5532789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5533789Sahrens 				(void) printf("%7llu %9s   %s\n",
553410922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5535789Sahrens 				    dli.dli_sname);
5536789Sahrens 			}
5537789Sahrens 			(void) printf("\n");
5538789Sahrens 		}
5539789Sahrens 
5540789Sahrens 		/*
554110922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
554210922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
554310922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5544789Sahrens 		 */
554510922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
554610922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
554710922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
554810922SJeff.Bonwick@Sun.COM 		} else {
554910922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
555010922SJeff.Bonwick@Sun.COM 			kernel_fini();
555110922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
555210922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
555310922SJeff.Bonwick@Sun.COM 			    zopt_pool);
555410922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
555510922SJeff.Bonwick@Sun.COM 		}
5556789Sahrens 		kernel_fini();
555710922SJeff.Bonwick@Sun.COM 
555810922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5559789Sahrens 	}
5560789Sahrens 
5561789Sahrens 	if (zopt_verbose >= 1) {
5562789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5563789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5564789Sahrens 	}
5565789Sahrens 
5566789Sahrens 	return (0);
5567789Sahrens }
5568