xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 12296)
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 /*
2212285SJeff.Bonwick@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens /*
26789Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
27789Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
28789Sahrens  *
29789Sahrens  * The overall design of the ztest program is as follows:
30789Sahrens  *
31789Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
32789Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
33789Sahrens  *     we have a simple routine to test that functionality.  These
34789Sahrens  *     individual routines do not have to do anything "stressful".
35789Sahrens  *
36789Sahrens  * (2) We turn these simple functionality tests into a stress test by
37789Sahrens  *     running them all in parallel, with as many threads as desired,
38789Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
39789Sahrens  *
40789Sahrens  * (3) While all this is happening, we inject faults into the pool to
41789Sahrens  *     verify that self-healing data really works.
42789Sahrens  *
43789Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
44789Sahrens  *     functions.  Thus even individual objects vary from block to block
45789Sahrens  *     in which checksum they use and whether they're compressed.
46789Sahrens  *
47789Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
48789Sahrens  *     we run the entire test in a child of the main process.
49789Sahrens  *     At random times, the child self-immolates with a SIGKILL.
50789Sahrens  *     This is the software equivalent of pulling the power cord.
51789Sahrens  *     The parent then runs the test again, using the existing
52789Sahrens  *     storage pool, as many times as desired.
53789Sahrens  *
54789Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
55789Sahrens  *     many of the functional tests record the transaction group number
56789Sahrens  *     as part of their data.  When reading old data, they verify that
57789Sahrens  *     the transaction group number is less than the current, open txg.
58789Sahrens  *     If you add a new test, please do this if applicable.
59789Sahrens  *
60789Sahrens  * When run with no arguments, ztest runs for about five minutes and
61789Sahrens  * produces no output if successful.  To get a little bit of information,
62789Sahrens  * specify -V.  To get more information, specify -VV, and so on.
63789Sahrens  *
64789Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
65789Sahrens  *
66789Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
67789Sahrens  * to increase the pool capacity, fanout, and overall stress level.
68789Sahrens  *
69789Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
70789Sahrens  * This can be useful when you're trying to distinguish temporal incursions
71789Sahrens  * from plain old race conditions.
72789Sahrens  */
73789Sahrens 
74789Sahrens #include <sys/zfs_context.h>
75789Sahrens #include <sys/spa.h>
76789Sahrens #include <sys/dmu.h>
77789Sahrens #include <sys/txg.h>
789412SAleksandr.Guzovskiy@Sun.COM #include <sys/dbuf.h>
79789Sahrens #include <sys/zap.h>
80789Sahrens #include <sys/dmu_objset.h>
81789Sahrens #include <sys/poll.h>
82789Sahrens #include <sys/stat.h>
83789Sahrens #include <sys/time.h>
84789Sahrens #include <sys/wait.h>
85789Sahrens #include <sys/mman.h>
86789Sahrens #include <sys/resource.h>
87789Sahrens #include <sys/zio.h>
88789Sahrens #include <sys/zil.h>
8910922SJeff.Bonwick@Sun.COM #include <sys/zil_impl.h>
90789Sahrens #include <sys/vdev_impl.h>
917754SJeff.Bonwick@Sun.COM #include <sys/vdev_file.h>
92789Sahrens #include <sys/spa_impl.h>
9310594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
94789Sahrens #include <sys/dsl_prop.h>
958779SMark.Musante@Sun.COM #include <sys/dsl_dataset.h>
96*12296SLin.Ling@Sun.COM #include <sys/dsl_scan.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 */
12811818SMark.Musante@Sun.COM static uint64_t zopt_maxloops = 50;	/* max loops during spa_freeze() */
129789Sahrens 
13010922SJeff.Bonwick@Sun.COM #define	BT_MAGIC	0x123456789abcdefULL
13111422SMark.Musante@Sun.COM #define	MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
13210922SJeff.Bonwick@Sun.COM 
13310922SJeff.Bonwick@Sun.COM enum ztest_io_type {
13410922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_TAG,
13510922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_PATTERN,
13610922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_ZEROES,
13710922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TRUNCATE,
13810922SJeff.Bonwick@Sun.COM 	ZTEST_IO_SETATTR,
13910922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TYPES
14010922SJeff.Bonwick@Sun.COM };
14110922SJeff.Bonwick@Sun.COM 
1425530Sbonwick typedef struct ztest_block_tag {
14310922SJeff.Bonwick@Sun.COM 	uint64_t	bt_magic;
1445530Sbonwick 	uint64_t	bt_objset;
1455530Sbonwick 	uint64_t	bt_object;
1465530Sbonwick 	uint64_t	bt_offset;
14710922SJeff.Bonwick@Sun.COM 	uint64_t	bt_gen;
1485530Sbonwick 	uint64_t	bt_txg;
14910922SJeff.Bonwick@Sun.COM 	uint64_t	bt_crtxg;
1505530Sbonwick } ztest_block_tag_t;
1515530Sbonwick 
15210922SJeff.Bonwick@Sun.COM typedef struct bufwad {
15310922SJeff.Bonwick@Sun.COM 	uint64_t	bw_index;
15410922SJeff.Bonwick@Sun.COM 	uint64_t	bw_txg;
15510922SJeff.Bonwick@Sun.COM 	uint64_t	bw_data;
15610922SJeff.Bonwick@Sun.COM } bufwad_t;
15710922SJeff.Bonwick@Sun.COM 
15810922SJeff.Bonwick@Sun.COM /*
15910922SJeff.Bonwick@Sun.COM  * XXX -- fix zfs range locks to be generic so we can use them here.
16010922SJeff.Bonwick@Sun.COM  */
16110922SJeff.Bonwick@Sun.COM typedef enum {
16210922SJeff.Bonwick@Sun.COM 	RL_READER,
16310922SJeff.Bonwick@Sun.COM 	RL_WRITER,
16410922SJeff.Bonwick@Sun.COM 	RL_APPEND
16510922SJeff.Bonwick@Sun.COM } rl_type_t;
16610922SJeff.Bonwick@Sun.COM 
16710922SJeff.Bonwick@Sun.COM typedef struct rll {
16810922SJeff.Bonwick@Sun.COM 	void		*rll_writer;
16910922SJeff.Bonwick@Sun.COM 	int		rll_readers;
17010922SJeff.Bonwick@Sun.COM 	mutex_t		rll_lock;
17110922SJeff.Bonwick@Sun.COM 	cond_t		rll_cv;
17210922SJeff.Bonwick@Sun.COM } rll_t;
17310922SJeff.Bonwick@Sun.COM 
17410922SJeff.Bonwick@Sun.COM typedef struct rl {
17510922SJeff.Bonwick@Sun.COM 	uint64_t	rl_object;
17610922SJeff.Bonwick@Sun.COM 	uint64_t	rl_offset;
17710922SJeff.Bonwick@Sun.COM 	uint64_t	rl_size;
17810922SJeff.Bonwick@Sun.COM 	rll_t		*rl_lock;
17910922SJeff.Bonwick@Sun.COM } rl_t;
18010922SJeff.Bonwick@Sun.COM 
18110922SJeff.Bonwick@Sun.COM #define	ZTEST_RANGE_LOCKS	64
18210922SJeff.Bonwick@Sun.COM #define	ZTEST_OBJECT_LOCKS	64
18310922SJeff.Bonwick@Sun.COM 
18410922SJeff.Bonwick@Sun.COM /*
18510922SJeff.Bonwick@Sun.COM  * Object descriptor.  Used as a template for object lookup/create/remove.
18610922SJeff.Bonwick@Sun.COM  */
18710922SJeff.Bonwick@Sun.COM typedef struct ztest_od {
18810922SJeff.Bonwick@Sun.COM 	uint64_t	od_dir;
18910922SJeff.Bonwick@Sun.COM 	uint64_t	od_object;
19010922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_type;
19110922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_crtype;
19210922SJeff.Bonwick@Sun.COM 	uint64_t	od_blocksize;
19310922SJeff.Bonwick@Sun.COM 	uint64_t	od_crblocksize;
19410922SJeff.Bonwick@Sun.COM 	uint64_t	od_gen;
19510922SJeff.Bonwick@Sun.COM 	uint64_t	od_crgen;
19610922SJeff.Bonwick@Sun.COM 	char		od_name[MAXNAMELEN];
19710922SJeff.Bonwick@Sun.COM } ztest_od_t;
19810922SJeff.Bonwick@Sun.COM 
19910922SJeff.Bonwick@Sun.COM /*
20010922SJeff.Bonwick@Sun.COM  * Per-dataset state.
20110922SJeff.Bonwick@Sun.COM  */
20210922SJeff.Bonwick@Sun.COM typedef struct ztest_ds {
20310922SJeff.Bonwick@Sun.COM 	objset_t	*zd_os;
20410922SJeff.Bonwick@Sun.COM 	zilog_t		*zd_zilog;
20510922SJeff.Bonwick@Sun.COM 	uint64_t	zd_seq;
20610922SJeff.Bonwick@Sun.COM 	ztest_od_t	*zd_od;		/* debugging aid */
20710922SJeff.Bonwick@Sun.COM 	char		zd_name[MAXNAMELEN];
20810922SJeff.Bonwick@Sun.COM 	mutex_t		zd_dirobj_lock;
20910922SJeff.Bonwick@Sun.COM 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
21010922SJeff.Bonwick@Sun.COM 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
21110922SJeff.Bonwick@Sun.COM } ztest_ds_t;
21210922SJeff.Bonwick@Sun.COM 
21310922SJeff.Bonwick@Sun.COM /*
21410922SJeff.Bonwick@Sun.COM  * Per-iteration state.
21510922SJeff.Bonwick@Sun.COM  */
21610922SJeff.Bonwick@Sun.COM typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
21710922SJeff.Bonwick@Sun.COM 
21810922SJeff.Bonwick@Sun.COM typedef struct ztest_info {
21910922SJeff.Bonwick@Sun.COM 	ztest_func_t	*zi_func;	/* test function */
22010922SJeff.Bonwick@Sun.COM 	uint64_t	zi_iters;	/* iterations per execution */
22110922SJeff.Bonwick@Sun.COM 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
22210922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_count;	/* per-pass count */
22310922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_time;	/* per-pass time */
22410922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_next;	/* next time to call this function */
22510922SJeff.Bonwick@Sun.COM } ztest_info_t;
226789Sahrens 
227789Sahrens /*
228789Sahrens  * Note: these aren't static because we want dladdr() to work.
229789Sahrens  */
230789Sahrens ztest_func_t ztest_dmu_read_write;
231789Sahrens ztest_func_t ztest_dmu_write_parallel;
232789Sahrens ztest_func_t ztest_dmu_object_alloc_free;
23310612SRicardo.M.Correia@Sun.COM ztest_func_t ztest_dmu_commit_callbacks;
234789Sahrens ztest_func_t ztest_zap;
23510922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zap_parallel;
23610922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zil_commit;
23710922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_read_write_zcopy;
23810922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_objset_create_destroy;
23910922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_prealloc;
24010431SSanjeev.Bagewadi@Sun.COM ztest_func_t ztest_fzap;
24110922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_create_destroy;
242789Sahrens ztest_func_t ztest_dsl_prop_get_set;
24310922SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_prop_get_set;
244789Sahrens ztest_func_t ztest_spa_create_destroy;
245789Sahrens ztest_func_t ztest_fault_inject;
24610922SJeff.Bonwick@Sun.COM ztest_func_t ztest_ddt_repair;
24710922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_hold;
2487754SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_rename;
24910922SJeff.Bonwick@Sun.COM ztest_func_t ztest_scrub;
25010922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dsl_dataset_promote_busy;
251789Sahrens ztest_func_t ztest_vdev_attach_detach;
252789Sahrens ztest_func_t ztest_vdev_LUN_growth;
253789Sahrens ztest_func_t ztest_vdev_add_remove;
2547754SJeff.Bonwick@Sun.COM ztest_func_t ztest_vdev_aux_add_remove;
25511422SMark.Musante@Sun.COM ztest_func_t ztest_split_pool;
25610922SJeff.Bonwick@Sun.COM 
25710922SJeff.Bonwick@Sun.COM uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
25810922SJeff.Bonwick@Sun.COM uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
25910922SJeff.Bonwick@Sun.COM uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
26010922SJeff.Bonwick@Sun.COM uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
26110922SJeff.Bonwick@Sun.COM uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
262789Sahrens 
263789Sahrens ztest_info_t ztest_info[] = {
2645530Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
26510922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
2665530Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
26710922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
2685530Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
2695530Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
27011422SMark.Musante@Sun.COM 	{ ztest_split_pool,			1,	&zopt_always	},
27110922SJeff.Bonwick@Sun.COM 	{ ztest_zil_commit,			1,	&zopt_incessant	},
27210922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
27310922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
27410922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
27510922SJeff.Bonwick@Sun.COM 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
27610922SJeff.Bonwick@Sun.COM #if 0
27710922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
27810922SJeff.Bonwick@Sun.COM #endif
27910922SJeff.Bonwick@Sun.COM 	{ ztest_fzap,				1,	&zopt_sometimes	},
28010922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
28110922SJeff.Bonwick@Sun.COM 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
2825530Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
28310922SJeff.Bonwick@Sun.COM 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
28410693Schris.kirby@sun.com 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
2855530Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
28610922SJeff.Bonwick@Sun.COM 	{ ztest_scrub,				1,	&zopt_rarely	},
28710922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
288*12296SLin.Ling@Sun.COM 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely },
2897754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
290*12296SLin.Ling@Sun.COM 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime },
2917754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
292789Sahrens };
293789Sahrens 
294789Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
295789Sahrens 
296789Sahrens /*
29710612SRicardo.M.Correia@Sun.COM  * The following struct is used to hold a list of uncalled commit callbacks.
29810612SRicardo.M.Correia@Sun.COM  * The callbacks are ordered by txg number.
29910612SRicardo.M.Correia@Sun.COM  */
30010612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_list {
30110612SRicardo.M.Correia@Sun.COM 	mutex_t	zcl_callbacks_lock;
30210612SRicardo.M.Correia@Sun.COM 	list_t	zcl_callbacks;
30310612SRicardo.M.Correia@Sun.COM } ztest_cb_list_t;
30410612SRicardo.M.Correia@Sun.COM 
30510612SRicardo.M.Correia@Sun.COM /*
306789Sahrens  * Stuff we need to share writably between parent and child.
307789Sahrens  */
308789Sahrens typedef struct ztest_shared {
30910922SJeff.Bonwick@Sun.COM 	char		*zs_pool;
31010922SJeff.Bonwick@Sun.COM 	spa_t		*zs_spa;
31110922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_start;
31210922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_stop;
31310922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_start;
31410922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_stop;
31510922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_kill;
31610922SJeff.Bonwick@Sun.COM 	uint64_t	zs_enospc_count;
31710594SGeorge.Wilson@Sun.COM 	uint64_t	zs_vdev_next_leaf;
3187754SJeff.Bonwick@Sun.COM 	uint64_t	zs_vdev_aux;
319789Sahrens 	uint64_t	zs_alloc;
320789Sahrens 	uint64_t	zs_space;
32110922SJeff.Bonwick@Sun.COM 	mutex_t		zs_vdev_lock;
32210922SJeff.Bonwick@Sun.COM 	rwlock_t	zs_name_lock;
323789Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
32411422SMark.Musante@Sun.COM 	uint64_t	zs_splits;
32511422SMark.Musante@Sun.COM 	uint64_t	zs_mirrors;
32610922SJeff.Bonwick@Sun.COM 	ztest_ds_t	zs_zd[];
327789Sahrens } ztest_shared_t;
328789Sahrens 
32910922SJeff.Bonwick@Sun.COM #define	ID_PARALLEL	-1ULL
33010922SJeff.Bonwick@Sun.COM 
331789Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
3327754SJeff.Bonwick@Sun.COM static char ztest_aux_template[] = "%s/%s.%s.%llu";
33310922SJeff.Bonwick@Sun.COM ztest_shared_t *ztest_shared;
33410922SJeff.Bonwick@Sun.COM uint64_t *ztest_seq;
335789Sahrens 
336789Sahrens static int ztest_random_fd;
337789Sahrens static int ztest_dump_core = 1;
338789Sahrens 
3397754SJeff.Bonwick@Sun.COM static boolean_t ztest_exiting;
3405329Sgw25295 
34110612SRicardo.M.Correia@Sun.COM /* Global commit callback list */
34210612SRicardo.M.Correia@Sun.COM static ztest_cb_list_t zcl;
34310612SRicardo.M.Correia@Sun.COM 
3445530Sbonwick extern uint64_t metaslab_gang_bang;
3459480SGeorge.Wilson@Sun.COM extern uint64_t metaslab_df_alloc_threshold;
34610922SJeff.Bonwick@Sun.COM static uint64_t metaslab_sz;
34710922SJeff.Bonwick@Sun.COM 
34810922SJeff.Bonwick@Sun.COM enum ztest_object {
34910922SJeff.Bonwick@Sun.COM 	ZTEST_META_DNODE = 0,
35010922SJeff.Bonwick@Sun.COM 	ZTEST_DIROBJ,
35110922SJeff.Bonwick@Sun.COM 	ZTEST_OBJECTS
35210922SJeff.Bonwick@Sun.COM };
353789Sahrens 
3544008Sraf static void usage(boolean_t) __NORETURN;
3553972Svb160487 
356789Sahrens /*
357789Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
358789Sahrens  * debugging facilities.
359789Sahrens  */
360789Sahrens const char *
361789Sahrens _umem_debug_init()
362789Sahrens {
363789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
364789Sahrens }
365789Sahrens 
366789Sahrens const char *
367789Sahrens _umem_logging_init(void)
368789Sahrens {
369789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
370789Sahrens }
371789Sahrens 
372789Sahrens #define	FATAL_MSG_SZ	1024
373789Sahrens 
374789Sahrens char *fatal_msg;
375789Sahrens 
376789Sahrens static void
377789Sahrens fatal(int do_perror, char *message, ...)
378789Sahrens {
379789Sahrens 	va_list args;
380789Sahrens 	int save_errno = errno;
381789Sahrens 	char buf[FATAL_MSG_SZ];
382789Sahrens 
383789Sahrens 	(void) fflush(stdout);
384789Sahrens 
385789Sahrens 	va_start(args, message);
386789Sahrens 	(void) sprintf(buf, "ztest: ");
387789Sahrens 	/* LINTED */
388789Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
389789Sahrens 	va_end(args);
390789Sahrens 	if (do_perror) {
391789Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
392789Sahrens 		    ": %s", strerror(save_errno));
393789Sahrens 	}
394789Sahrens 	(void) fprintf(stderr, "%s\n", buf);
395789Sahrens 	fatal_msg = buf;			/* to ease debugging */
396789Sahrens 	if (ztest_dump_core)
397789Sahrens 		abort();
398789Sahrens 	exit(3);
399789Sahrens }
400789Sahrens 
401789Sahrens static int
402789Sahrens str2shift(const char *buf)
403789Sahrens {
404789Sahrens 	const char *ends = "BKMGTPEZ";
405789Sahrens 	int i;
406789Sahrens 
407789Sahrens 	if (buf[0] == '\0')
408789Sahrens 		return (0);
409789Sahrens 	for (i = 0; i < strlen(ends); i++) {
410789Sahrens 		if (toupper(buf[0]) == ends[i])
411789Sahrens 			break;
412789Sahrens 	}
4133972Svb160487 	if (i == strlen(ends)) {
4143972Svb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
4153972Svb160487 		    buf);
4163972Svb160487 		usage(B_FALSE);
4173972Svb160487 	}
418789Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
419789Sahrens 		return (10*i);
420789Sahrens 	}
4213972Svb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
4223972Svb160487 	usage(B_FALSE);
4233972Svb160487 	/* NOTREACHED */
424789Sahrens }
425789Sahrens 
426789Sahrens static uint64_t
427789Sahrens nicenumtoull(const char *buf)
428789Sahrens {
429789Sahrens 	char *end;
430789Sahrens 	uint64_t val;
431789Sahrens 
432789Sahrens 	val = strtoull(buf, &end, 0);
433789Sahrens 	if (end == buf) {
4343972Svb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
4353972Svb160487 		usage(B_FALSE);
436789Sahrens 	} else if (end[0] == '.') {
437789Sahrens 		double fval = strtod(buf, &end);
438789Sahrens 		fval *= pow(2, str2shift(end));
4393972Svb160487 		if (fval > UINT64_MAX) {
4403972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4413972Svb160487 			    buf);
4423972Svb160487 			usage(B_FALSE);
4433972Svb160487 		}
444789Sahrens 		val = (uint64_t)fval;
445789Sahrens 	} else {
446789Sahrens 		int shift = str2shift(end);
4473972Svb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
4483972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4493972Svb160487 			    buf);
4503972Svb160487 			usage(B_FALSE);
4513972Svb160487 		}
452789Sahrens 		val <<= shift;
453789Sahrens 	}
454789Sahrens 	return (val);
455789Sahrens }
456789Sahrens 
457789Sahrens static void
4583972Svb160487 usage(boolean_t requested)
459789Sahrens {
460789Sahrens 	char nice_vdev_size[10];
461789Sahrens 	char nice_gang_bang[10];
4623972Svb160487 	FILE *fp = requested ? stdout : stderr;
463789Sahrens 
464789Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
4655530Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
466789Sahrens 
4673972Svb160487 	(void) fprintf(fp, "Usage: %s\n"
468789Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
469789Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
47011818SMark.Musante@Sun.COM 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
471789Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
472789Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
4732082Seschrock 	    "\t[-R raidz_parity (default: %d)]\n"
474789Sahrens 	    "\t[-d datasets (default: %d)]\n"
475789Sahrens 	    "\t[-t threads (default: %d)]\n"
476789Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
47711818SMark.Musante@Sun.COM 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
47811818SMark.Musante@Sun.COM 	    "\t[-k kill_percentage (default: %llu%%)]\n"
479789Sahrens 	    "\t[-p pool_name (default: %s)]\n"
48011818SMark.Musante@Sun.COM 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
48111818SMark.Musante@Sun.COM 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
48211818SMark.Musante@Sun.COM 	    "\t[-E] use existing pool instead of creating new one\n"
48311818SMark.Musante@Sun.COM 	    "\t[-T time (default: %llu sec)] total run time\n"
48411818SMark.Musante@Sun.COM 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
48511818SMark.Musante@Sun.COM 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
4863972Svb160487 	    "\t[-h] (print help)\n"
487789Sahrens 	    "",
488789Sahrens 	    cmdname,
4895329Sgw25295 	    (u_longlong_t)zopt_vdevs,			/* -v */
4905329Sgw25295 	    nice_vdev_size,				/* -s */
4915329Sgw25295 	    zopt_ashift,				/* -a */
4925329Sgw25295 	    zopt_mirrors,				/* -m */
4935329Sgw25295 	    zopt_raidz,					/* -r */
4945329Sgw25295 	    zopt_raidz_parity,				/* -R */
4955329Sgw25295 	    zopt_datasets,				/* -d */
4965329Sgw25295 	    zopt_threads,				/* -t */
4975329Sgw25295 	    nice_gang_bang,				/* -g */
4985329Sgw25295 	    zopt_init,					/* -i */
4995329Sgw25295 	    (u_longlong_t)zopt_killrate,		/* -k */
5005329Sgw25295 	    zopt_pool,					/* -p */
5015329Sgw25295 	    zopt_dir,					/* -f */
5025329Sgw25295 	    (u_longlong_t)zopt_time,			/* -T */
50311818SMark.Musante@Sun.COM 	    (u_longlong_t)zopt_maxloops,		/* -F */
5047754SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zopt_passtime);		/* -P */
5053972Svb160487 	exit(requested ? 0 : 1);
506789Sahrens }
507789Sahrens 
508789Sahrens static void
509789Sahrens process_options(int argc, char **argv)
510789Sahrens {
511789Sahrens 	int opt;
512789Sahrens 	uint64_t value;
513789Sahrens 
514789Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
5155530Sbonwick 	metaslab_gang_bang = 32 << 10;
516789Sahrens 
517789Sahrens 	while ((opt = getopt(argc, argv,
51811818SMark.Musante@Sun.COM 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
519789Sahrens 		value = 0;
520789Sahrens 		switch (opt) {
5214451Seschrock 		case 'v':
5224451Seschrock 		case 's':
5234451Seschrock 		case 'a':
5244451Seschrock 		case 'm':
5254451Seschrock 		case 'r':
5264451Seschrock 		case 'R':
5274451Seschrock 		case 'd':
5284451Seschrock 		case 't':
5294451Seschrock 		case 'g':
5304451Seschrock 		case 'i':
5314451Seschrock 		case 'k':
5324451Seschrock 		case 'T':
5334451Seschrock 		case 'P':
53411818SMark.Musante@Sun.COM 		case 'F':
535789Sahrens 			value = nicenumtoull(optarg);
536789Sahrens 		}
537789Sahrens 		switch (opt) {
5384451Seschrock 		case 'v':
539789Sahrens 			zopt_vdevs = value;
540789Sahrens 			break;
5414451Seschrock 		case 's':
542789Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
543789Sahrens 			break;
5444451Seschrock 		case 'a':
5451732Sbonwick 			zopt_ashift = value;
5461732Sbonwick 			break;
5474451Seschrock 		case 'm':
548789Sahrens 			zopt_mirrors = value;
549789Sahrens 			break;
5504451Seschrock 		case 'r':
551789Sahrens 			zopt_raidz = MAX(1, value);
552789Sahrens 			break;
5534451Seschrock 		case 'R':
55410105Sadam.leventhal@sun.com 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
5552082Seschrock 			break;
5564451Seschrock 		case 'd':
5571732Sbonwick 			zopt_datasets = MAX(1, value);
558789Sahrens 			break;
5594451Seschrock 		case 't':
560789Sahrens 			zopt_threads = MAX(1, value);
561789Sahrens 			break;
5624451Seschrock 		case 'g':
5635530Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
564789Sahrens 			break;
5654451Seschrock 		case 'i':
566789Sahrens 			zopt_init = value;
567789Sahrens 			break;
5684451Seschrock 		case 'k':
569789Sahrens 			zopt_killrate = value;
570789Sahrens 			break;
5714451Seschrock 		case 'p':
572789Sahrens 			zopt_pool = strdup(optarg);
573789Sahrens 			break;
5744451Seschrock 		case 'f':
575789Sahrens 			zopt_dir = strdup(optarg);
576789Sahrens 			break;
5774451Seschrock 		case 'V':
578789Sahrens 			zopt_verbose++;
579789Sahrens 			break;
5804451Seschrock 		case 'E':
581789Sahrens 			zopt_init = 0;
582789Sahrens 			break;
5834451Seschrock 		case 'T':
584789Sahrens 			zopt_time = value;
585789Sahrens 			break;
5864451Seschrock 		case 'P':
587789Sahrens 			zopt_passtime = MAX(1, value);
588789Sahrens 			break;
58911818SMark.Musante@Sun.COM 		case 'F':
59011818SMark.Musante@Sun.COM 			zopt_maxloops = MAX(1, value);
59111818SMark.Musante@Sun.COM 			break;
5924451Seschrock 		case 'h':
5933972Svb160487 			usage(B_TRUE);
5943972Svb160487 			break;
5954451Seschrock 		case '?':
5964451Seschrock 		default:
5973972Svb160487 			usage(B_FALSE);
598789Sahrens 			break;
599789Sahrens 		}
600789Sahrens 	}
601789Sahrens 
6022082Seschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
6032082Seschrock 
60410922SJeff.Bonwick@Sun.COM 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
60510922SJeff.Bonwick@Sun.COM 	    UINT64_MAX >> 2);
606789Sahrens }
607789Sahrens 
60810922SJeff.Bonwick@Sun.COM static void
60910922SJeff.Bonwick@Sun.COM ztest_kill(ztest_shared_t *zs)
61010922SJeff.Bonwick@Sun.COM {
61110922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
61210922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
61310922SJeff.Bonwick@Sun.COM 	(void) kill(getpid(), SIGKILL);
61410922SJeff.Bonwick@Sun.COM }
61510922SJeff.Bonwick@Sun.COM 
61610922SJeff.Bonwick@Sun.COM static uint64_t
61710922SJeff.Bonwick@Sun.COM ztest_random(uint64_t range)
61810922SJeff.Bonwick@Sun.COM {
61910922SJeff.Bonwick@Sun.COM 	uint64_t r;
62010922SJeff.Bonwick@Sun.COM 
62110922SJeff.Bonwick@Sun.COM 	if (range == 0)
62210922SJeff.Bonwick@Sun.COM 		return (0);
62310922SJeff.Bonwick@Sun.COM 
62410922SJeff.Bonwick@Sun.COM 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
62510922SJeff.Bonwick@Sun.COM 		fatal(1, "short read from /dev/urandom");
62610922SJeff.Bonwick@Sun.COM 
62710922SJeff.Bonwick@Sun.COM 	return (r % range);
62810922SJeff.Bonwick@Sun.COM }
62910922SJeff.Bonwick@Sun.COM 
63010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
63110922SJeff.Bonwick@Sun.COM static void
63210922SJeff.Bonwick@Sun.COM ztest_record_enospc(const char *s)
63310922SJeff.Bonwick@Sun.COM {
63410922SJeff.Bonwick@Sun.COM 	ztest_shared->zs_enospc_count++;
63510922SJeff.Bonwick@Sun.COM }
63610922SJeff.Bonwick@Sun.COM 
6371732Sbonwick static uint64_t
6381732Sbonwick ztest_get_ashift(void)
6391732Sbonwick {
6401732Sbonwick 	if (zopt_ashift == 0)
6411732Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
6421732Sbonwick 	return (zopt_ashift);
6431732Sbonwick }
6441732Sbonwick 
645789Sahrens static nvlist_t *
6467754SJeff.Bonwick@Sun.COM make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
647789Sahrens {
6487754SJeff.Bonwick@Sun.COM 	char pathbuf[MAXPATHLEN];
649789Sahrens 	uint64_t vdev;
650789Sahrens 	nvlist_t *file;
651789Sahrens 
6527754SJeff.Bonwick@Sun.COM 	if (ashift == 0)
6537754SJeff.Bonwick@Sun.COM 		ashift = ztest_get_ashift();
6547754SJeff.Bonwick@Sun.COM 
6557754SJeff.Bonwick@Sun.COM 	if (path == NULL) {
6567754SJeff.Bonwick@Sun.COM 		path = pathbuf;
6577754SJeff.Bonwick@Sun.COM 
6587754SJeff.Bonwick@Sun.COM 		if (aux != NULL) {
6597754SJeff.Bonwick@Sun.COM 			vdev = ztest_shared->zs_vdev_aux;
6607754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template,
6617754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, aux, vdev);
6627754SJeff.Bonwick@Sun.COM 		} else {
66310594SGeorge.Wilson@Sun.COM 			vdev = ztest_shared->zs_vdev_next_leaf++;
6647754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_dev_template,
6657754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, vdev);
6667754SJeff.Bonwick@Sun.COM 		}
6677754SJeff.Bonwick@Sun.COM 	}
6687754SJeff.Bonwick@Sun.COM 
6697754SJeff.Bonwick@Sun.COM 	if (size != 0) {
6707754SJeff.Bonwick@Sun.COM 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
671789Sahrens 		if (fd == -1)
6727754SJeff.Bonwick@Sun.COM 			fatal(1, "can't open %s", path);
673789Sahrens 		if (ftruncate(fd, size) != 0)
6747754SJeff.Bonwick@Sun.COM 			fatal(1, "can't ftruncate %s", path);
675789Sahrens 		(void) close(fd);
676789Sahrens 	}
677789Sahrens 
678789Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
679789Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
6807754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
6811732Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
682789Sahrens 
683789Sahrens 	return (file);
684789Sahrens }
685789Sahrens 
686789Sahrens static nvlist_t *
6877754SJeff.Bonwick@Sun.COM make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
688789Sahrens {
689789Sahrens 	nvlist_t *raidz, **child;
690789Sahrens 	int c;
691789Sahrens 
692789Sahrens 	if (r < 2)
6937754SJeff.Bonwick@Sun.COM 		return (make_vdev_file(path, aux, size, ashift));
694789Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
695789Sahrens 
696789Sahrens 	for (c = 0; c < r; c++)
6977754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_file(path, aux, size, ashift);
698789Sahrens 
699789Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
700789Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
701789Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
7022082Seschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
7032082Seschrock 	    zopt_raidz_parity) == 0);
704789Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
705789Sahrens 	    child, r) == 0);
706789Sahrens 
707789Sahrens 	for (c = 0; c < r; c++)
708789Sahrens 		nvlist_free(child[c]);
709789Sahrens 
710789Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
711789Sahrens 
712789Sahrens 	return (raidz);
713789Sahrens }
714789Sahrens 
715789Sahrens static nvlist_t *
7167754SJeff.Bonwick@Sun.COM make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
7177768SJeff.Bonwick@Sun.COM 	int r, int m)
718789Sahrens {
719789Sahrens 	nvlist_t *mirror, **child;
720789Sahrens 	int c;
721789Sahrens 
722789Sahrens 	if (m < 1)
7237754SJeff.Bonwick@Sun.COM 		return (make_vdev_raidz(path, aux, size, ashift, r));
724789Sahrens 
725789Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
726789Sahrens 
727789Sahrens 	for (c = 0; c < m; c++)
7287754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
729789Sahrens 
730789Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
731789Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
732789Sahrens 	    VDEV_TYPE_MIRROR) == 0);
733789Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
734789Sahrens 	    child, m) == 0);
735789Sahrens 
736789Sahrens 	for (c = 0; c < m; c++)
737789Sahrens 		nvlist_free(child[c]);
738789Sahrens 
739789Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
740789Sahrens 
741789Sahrens 	return (mirror);
742789Sahrens }
743789Sahrens 
744789Sahrens static nvlist_t *
7457754SJeff.Bonwick@Sun.COM make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
7467754SJeff.Bonwick@Sun.COM 	int log, int r, int m, int t)
747789Sahrens {
748789Sahrens 	nvlist_t *root, **child;
749789Sahrens 	int c;
750789Sahrens 
751789Sahrens 	ASSERT(t > 0);
752789Sahrens 
753789Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
754789Sahrens 
7557768SJeff.Bonwick@Sun.COM 	for (c = 0; c < t; c++) {
7567768SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
7577768SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
7587768SJeff.Bonwick@Sun.COM 		    log) == 0);
7597768SJeff.Bonwick@Sun.COM 	}
760789Sahrens 
761789Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
762789Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
7637754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
764789Sahrens 	    child, t) == 0);
765789Sahrens 
766789Sahrens 	for (c = 0; c < t; c++)
767789Sahrens 		nvlist_free(child[c]);
768789Sahrens 
769789Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
770789Sahrens 
771789Sahrens 	return (root);
772789Sahrens }
773789Sahrens 
77410922SJeff.Bonwick@Sun.COM static int
77510922SJeff.Bonwick@Sun.COM ztest_random_blocksize(void)
776789Sahrens {
77710922SJeff.Bonwick@Sun.COM 	return (1 << (SPA_MINBLOCKSHIFT +
77810922SJeff.Bonwick@Sun.COM 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
779789Sahrens }
780789Sahrens 
781789Sahrens static int
78210922SJeff.Bonwick@Sun.COM ztest_random_ibshift(void)
78310922SJeff.Bonwick@Sun.COM {
78410922SJeff.Bonwick@Sun.COM 	return (DN_MIN_INDBLKSHIFT +
78510922SJeff.Bonwick@Sun.COM 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
78610922SJeff.Bonwick@Sun.COM }
78710922SJeff.Bonwick@Sun.COM 
78810922SJeff.Bonwick@Sun.COM static uint64_t
78910922SJeff.Bonwick@Sun.COM ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
790789Sahrens {
79110922SJeff.Bonwick@Sun.COM 	uint64_t top;
79210922SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
79310922SJeff.Bonwick@Sun.COM 	vdev_t *tvd;
79410922SJeff.Bonwick@Sun.COM 
79510922SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
79610922SJeff.Bonwick@Sun.COM 
79710922SJeff.Bonwick@Sun.COM 	do {
79810922SJeff.Bonwick@Sun.COM 		top = ztest_random(rvd->vdev_children);
79910922SJeff.Bonwick@Sun.COM 		tvd = rvd->vdev_child[top];
80010922SJeff.Bonwick@Sun.COM 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
80110922SJeff.Bonwick@Sun.COM 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
80210922SJeff.Bonwick@Sun.COM 
80310922SJeff.Bonwick@Sun.COM 	return (top);
80410922SJeff.Bonwick@Sun.COM }
80510922SJeff.Bonwick@Sun.COM 
80610922SJeff.Bonwick@Sun.COM static uint64_t
80710922SJeff.Bonwick@Sun.COM ztest_random_dsl_prop(zfs_prop_t prop)
80810922SJeff.Bonwick@Sun.COM {
80910922SJeff.Bonwick@Sun.COM 	uint64_t value;
81010922SJeff.Bonwick@Sun.COM 
81110922SJeff.Bonwick@Sun.COM 	do {
81210922SJeff.Bonwick@Sun.COM 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
81310922SJeff.Bonwick@Sun.COM 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
81410922SJeff.Bonwick@Sun.COM 
81510922SJeff.Bonwick@Sun.COM 	return (value);
81610922SJeff.Bonwick@Sun.COM }
81710922SJeff.Bonwick@Sun.COM 
81810922SJeff.Bonwick@Sun.COM static int
81910922SJeff.Bonwick@Sun.COM ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
82010922SJeff.Bonwick@Sun.COM     boolean_t inherit)
82110922SJeff.Bonwick@Sun.COM {
82210922SJeff.Bonwick@Sun.COM 	const char *propname = zfs_prop_to_name(prop);
82310922SJeff.Bonwick@Sun.COM 	const char *valname;
82410922SJeff.Bonwick@Sun.COM 	char setpoint[MAXPATHLEN];
82510922SJeff.Bonwick@Sun.COM 	uint64_t curval;
826789Sahrens 	int error;
827789Sahrens 
82811022STom.Erickson@Sun.COM 	error = dsl_prop_set(osname, propname,
82911022STom.Erickson@Sun.COM 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
83011022STom.Erickson@Sun.COM 	    sizeof (value), 1, &value);
83110922SJeff.Bonwick@Sun.COM 
83210922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
83310922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
834789Sahrens 		return (error);
835789Sahrens 	}
8362082Seschrock 	ASSERT3U(error, ==, 0);
83710922SJeff.Bonwick@Sun.COM 
83810922SJeff.Bonwick@Sun.COM 	VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
83910922SJeff.Bonwick@Sun.COM 	    1, &curval, setpoint), ==, 0);
84010922SJeff.Bonwick@Sun.COM 
84110922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
84210922SJeff.Bonwick@Sun.COM 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
84310922SJeff.Bonwick@Sun.COM 		(void) printf("%s %s = %s at '%s'\n",
84410922SJeff.Bonwick@Sun.COM 		    osname, propname, valname, setpoint);
845789Sahrens 	}
846789Sahrens 
847789Sahrens 	return (error);
848789Sahrens }
849789Sahrens 
850789Sahrens static int
85110922SJeff.Bonwick@Sun.COM ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
85210922SJeff.Bonwick@Sun.COM {
85310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
85410922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
85510922SJeff.Bonwick@Sun.COM 	int error;
85610922SJeff.Bonwick@Sun.COM 
85710922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
85810922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
85910922SJeff.Bonwick@Sun.COM 
86010922SJeff.Bonwick@Sun.COM 	error = spa_prop_set(spa, props);
86110922SJeff.Bonwick@Sun.COM 
86210922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
86310922SJeff.Bonwick@Sun.COM 
86410922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
86510922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
86610922SJeff.Bonwick@Sun.COM 		return (error);
86710922SJeff.Bonwick@Sun.COM 	}
86810922SJeff.Bonwick@Sun.COM 	ASSERT3U(error, ==, 0);
86910922SJeff.Bonwick@Sun.COM 
87010922SJeff.Bonwick@Sun.COM 	return (error);
87110922SJeff.Bonwick@Sun.COM }
87210922SJeff.Bonwick@Sun.COM 
87310922SJeff.Bonwick@Sun.COM static void
87410922SJeff.Bonwick@Sun.COM ztest_rll_init(rll_t *rll)
87510922SJeff.Bonwick@Sun.COM {
87610922SJeff.Bonwick@Sun.COM 	rll->rll_writer = NULL;
87710922SJeff.Bonwick@Sun.COM 	rll->rll_readers = 0;
87810922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
87910922SJeff.Bonwick@Sun.COM 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
88010922SJeff.Bonwick@Sun.COM }
88110922SJeff.Bonwick@Sun.COM 
88210922SJeff.Bonwick@Sun.COM static void
88310922SJeff.Bonwick@Sun.COM ztest_rll_destroy(rll_t *rll)
88410922SJeff.Bonwick@Sun.COM {
88510922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_writer == NULL);
88610922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_readers == 0);
88710922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
88810922SJeff.Bonwick@Sun.COM 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
88910922SJeff.Bonwick@Sun.COM }
89010922SJeff.Bonwick@Sun.COM 
89110922SJeff.Bonwick@Sun.COM static void
89210922SJeff.Bonwick@Sun.COM ztest_rll_lock(rll_t *rll, rl_type_t type)
89310922SJeff.Bonwick@Sun.COM {
89410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
89510922SJeff.Bonwick@Sun.COM 
89610922SJeff.Bonwick@Sun.COM 	if (type == RL_READER) {
89710922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL)
89810922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
89910922SJeff.Bonwick@Sun.COM 		rll->rll_readers++;
90010922SJeff.Bonwick@Sun.COM 	} else {
90110922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL || rll->rll_readers)
90210922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
90310922SJeff.Bonwick@Sun.COM 		rll->rll_writer = curthread;
90410922SJeff.Bonwick@Sun.COM 	}
90510922SJeff.Bonwick@Sun.COM 
90610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
90710922SJeff.Bonwick@Sun.COM }
90810922SJeff.Bonwick@Sun.COM 
90910922SJeff.Bonwick@Sun.COM static void
91010922SJeff.Bonwick@Sun.COM ztest_rll_unlock(rll_t *rll)
91110922SJeff.Bonwick@Sun.COM {
91210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
91310922SJeff.Bonwick@Sun.COM 
91410922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer) {
91510922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers == 0);
91610922SJeff.Bonwick@Sun.COM 		rll->rll_writer = NULL;
91710922SJeff.Bonwick@Sun.COM 	} else {
91810922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers != 0);
91910922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_writer == NULL);
92010922SJeff.Bonwick@Sun.COM 		rll->rll_readers--;
92110922SJeff.Bonwick@Sun.COM 	}
92210922SJeff.Bonwick@Sun.COM 
92310922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
92410922SJeff.Bonwick@Sun.COM 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
92510922SJeff.Bonwick@Sun.COM 
92610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
92710922SJeff.Bonwick@Sun.COM }
92810922SJeff.Bonwick@Sun.COM 
92910922SJeff.Bonwick@Sun.COM static void
93010922SJeff.Bonwick@Sun.COM ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
93110922SJeff.Bonwick@Sun.COM {
93210922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
93310922SJeff.Bonwick@Sun.COM 
93410922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
93510922SJeff.Bonwick@Sun.COM }
93610922SJeff.Bonwick@Sun.COM 
93710922SJeff.Bonwick@Sun.COM static void
93810922SJeff.Bonwick@Sun.COM ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
93910922SJeff.Bonwick@Sun.COM {
94010922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
94110922SJeff.Bonwick@Sun.COM 
94210922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
94310922SJeff.Bonwick@Sun.COM }
94410922SJeff.Bonwick@Sun.COM 
94510922SJeff.Bonwick@Sun.COM static rl_t *
94610922SJeff.Bonwick@Sun.COM ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
94710922SJeff.Bonwick@Sun.COM     uint64_t size, rl_type_t type)
94810922SJeff.Bonwick@Sun.COM {
94910922SJeff.Bonwick@Sun.COM 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
95010922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
95110922SJeff.Bonwick@Sun.COM 	rl_t *rl;
95210922SJeff.Bonwick@Sun.COM 
95310922SJeff.Bonwick@Sun.COM 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
95410922SJeff.Bonwick@Sun.COM 	rl->rl_object = object;
95510922SJeff.Bonwick@Sun.COM 	rl->rl_offset = offset;
95610922SJeff.Bonwick@Sun.COM 	rl->rl_size = size;
95710922SJeff.Bonwick@Sun.COM 	rl->rl_lock = rll;
95810922SJeff.Bonwick@Sun.COM 
95910922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
96010922SJeff.Bonwick@Sun.COM 
96110922SJeff.Bonwick@Sun.COM 	return (rl);
96210922SJeff.Bonwick@Sun.COM }
96310922SJeff.Bonwick@Sun.COM 
96410922SJeff.Bonwick@Sun.COM static void
96510922SJeff.Bonwick@Sun.COM ztest_range_unlock(rl_t *rl)
96610922SJeff.Bonwick@Sun.COM {
96710922SJeff.Bonwick@Sun.COM 	rll_t *rll = rl->rl_lock;
96810922SJeff.Bonwick@Sun.COM 
96910922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
97010922SJeff.Bonwick@Sun.COM 
97110922SJeff.Bonwick@Sun.COM 	umem_free(rl, sizeof (*rl));
97210922SJeff.Bonwick@Sun.COM }
97310922SJeff.Bonwick@Sun.COM 
97410922SJeff.Bonwick@Sun.COM static void
97510922SJeff.Bonwick@Sun.COM ztest_zd_init(ztest_ds_t *zd, objset_t *os)
97610922SJeff.Bonwick@Sun.COM {
97710922SJeff.Bonwick@Sun.COM 	zd->zd_os = os;
97810922SJeff.Bonwick@Sun.COM 	zd->zd_zilog = dmu_objset_zil(os);
97910922SJeff.Bonwick@Sun.COM 	zd->zd_seq = 0;
98010922SJeff.Bonwick@Sun.COM 	dmu_objset_name(os, zd->zd_name);
98110922SJeff.Bonwick@Sun.COM 
98210922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
98310922SJeff.Bonwick@Sun.COM 
98410922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
98510922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_object_lock[l]);
98610922SJeff.Bonwick@Sun.COM 
98710922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
98810922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_range_lock[l]);
98910922SJeff.Bonwick@Sun.COM }
99010922SJeff.Bonwick@Sun.COM 
99110922SJeff.Bonwick@Sun.COM static void
99210922SJeff.Bonwick@Sun.COM ztest_zd_fini(ztest_ds_t *zd)
99310922SJeff.Bonwick@Sun.COM {
99410922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
99510922SJeff.Bonwick@Sun.COM 
99610922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
99710922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_object_lock[l]);
99810922SJeff.Bonwick@Sun.COM 
99910922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
100010922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_range_lock[l]);
100110922SJeff.Bonwick@Sun.COM }
100210922SJeff.Bonwick@Sun.COM 
100310922SJeff.Bonwick@Sun.COM #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
100410922SJeff.Bonwick@Sun.COM 
100510922SJeff.Bonwick@Sun.COM static uint64_t
100610922SJeff.Bonwick@Sun.COM ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1007789Sahrens {
100810922SJeff.Bonwick@Sun.COM 	uint64_t txg;
100910922SJeff.Bonwick@Sun.COM 	int error;
101010922SJeff.Bonwick@Sun.COM 
101110922SJeff.Bonwick@Sun.COM 	/*
101210922SJeff.Bonwick@Sun.COM 	 * Attempt to assign tx to some transaction group.
101310922SJeff.Bonwick@Sun.COM 	 */
101410922SJeff.Bonwick@Sun.COM 	error = dmu_tx_assign(tx, txg_how);
101510922SJeff.Bonwick@Sun.COM 	if (error) {
101610922SJeff.Bonwick@Sun.COM 		if (error == ERESTART) {
101710922SJeff.Bonwick@Sun.COM 			ASSERT(txg_how == TXG_NOWAIT);
101810922SJeff.Bonwick@Sun.COM 			dmu_tx_wait(tx);
101910922SJeff.Bonwick@Sun.COM 		} else {
102010922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
102110922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(tag);
102210922SJeff.Bonwick@Sun.COM 		}
102310922SJeff.Bonwick@Sun.COM 		dmu_tx_abort(tx);
102410922SJeff.Bonwick@Sun.COM 		return (0);
102510922SJeff.Bonwick@Sun.COM 	}
102610922SJeff.Bonwick@Sun.COM 	txg = dmu_tx_get_txg(tx);
102710922SJeff.Bonwick@Sun.COM 	ASSERT(txg != 0);
102810922SJeff.Bonwick@Sun.COM 	return (txg);
102910922SJeff.Bonwick@Sun.COM }
103010922SJeff.Bonwick@Sun.COM 
103110922SJeff.Bonwick@Sun.COM static void
103210922SJeff.Bonwick@Sun.COM ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
103310922SJeff.Bonwick@Sun.COM {
103410922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
103510922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
103610922SJeff.Bonwick@Sun.COM 
103710922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
103810922SJeff.Bonwick@Sun.COM 		*ip++ = value;
103910922SJeff.Bonwick@Sun.COM }
104010922SJeff.Bonwick@Sun.COM 
104110922SJeff.Bonwick@Sun.COM static boolean_t
104210922SJeff.Bonwick@Sun.COM ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
104310922SJeff.Bonwick@Sun.COM {
104410922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
104510922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
104610922SJeff.Bonwick@Sun.COM 	uint64_t diff = 0;
104710922SJeff.Bonwick@Sun.COM 
104810922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
104910922SJeff.Bonwick@Sun.COM 		diff |= (value - *ip++);
105010922SJeff.Bonwick@Sun.COM 
105110922SJeff.Bonwick@Sun.COM 	return (diff == 0);
105210922SJeff.Bonwick@Sun.COM }
105310922SJeff.Bonwick@Sun.COM 
105410922SJeff.Bonwick@Sun.COM static void
105510922SJeff.Bonwick@Sun.COM ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
105610922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
105710922SJeff.Bonwick@Sun.COM {
105810922SJeff.Bonwick@Sun.COM 	bt->bt_magic = BT_MAGIC;
105910922SJeff.Bonwick@Sun.COM 	bt->bt_objset = dmu_objset_id(os);
106010922SJeff.Bonwick@Sun.COM 	bt->bt_object = object;
106110922SJeff.Bonwick@Sun.COM 	bt->bt_offset = offset;
106210922SJeff.Bonwick@Sun.COM 	bt->bt_gen = gen;
106310922SJeff.Bonwick@Sun.COM 	bt->bt_txg = txg;
106410922SJeff.Bonwick@Sun.COM 	bt->bt_crtxg = crtxg;
106510922SJeff.Bonwick@Sun.COM }
106610922SJeff.Bonwick@Sun.COM 
106710922SJeff.Bonwick@Sun.COM static void
106810922SJeff.Bonwick@Sun.COM ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
106910922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
107010922SJeff.Bonwick@Sun.COM {
107110922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_magic == BT_MAGIC);
107210922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_objset == dmu_objset_id(os));
107310922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_object == object);
107410922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_offset == offset);
107510922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_gen <= gen);
107610922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_txg <= txg);
107710922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_crtxg == crtxg);
107810922SJeff.Bonwick@Sun.COM }
107910922SJeff.Bonwick@Sun.COM 
108010922SJeff.Bonwick@Sun.COM static ztest_block_tag_t *
108110922SJeff.Bonwick@Sun.COM ztest_bt_bonus(dmu_buf_t *db)
108210922SJeff.Bonwick@Sun.COM {
108310922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
108410922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt;
108510922SJeff.Bonwick@Sun.COM 
108610922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
108710922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
108810922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
108910922SJeff.Bonwick@Sun.COM 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
109010922SJeff.Bonwick@Sun.COM 
109110922SJeff.Bonwick@Sun.COM 	return (bt);
109210922SJeff.Bonwick@Sun.COM }
109310922SJeff.Bonwick@Sun.COM 
109410922SJeff.Bonwick@Sun.COM /*
109510922SJeff.Bonwick@Sun.COM  * ZIL logging ops
109610922SJeff.Bonwick@Sun.COM  */
109710922SJeff.Bonwick@Sun.COM 
109810922SJeff.Bonwick@Sun.COM #define	lrz_type	lr_mode
109910922SJeff.Bonwick@Sun.COM #define	lrz_blocksize	lr_uid
110010922SJeff.Bonwick@Sun.COM #define	lrz_ibshift	lr_gid
110110922SJeff.Bonwick@Sun.COM #define	lrz_bonustype	lr_rdev
110210922SJeff.Bonwick@Sun.COM #define	lrz_bonuslen	lr_crtime[1]
110310922SJeff.Bonwick@Sun.COM 
110410922SJeff.Bonwick@Sun.COM static uint64_t
110510922SJeff.Bonwick@Sun.COM ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
110610922SJeff.Bonwick@Sun.COM {
110710922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
110810922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
110910922SJeff.Bonwick@Sun.COM 	itx_t *itx;
111010922SJeff.Bonwick@Sun.COM 
111110922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
111210922SJeff.Bonwick@Sun.COM 		return (0);
111310922SJeff.Bonwick@Sun.COM 
111410922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
111510922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
111610922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
111710922SJeff.Bonwick@Sun.COM 
111810922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
111910922SJeff.Bonwick@Sun.COM }
112010922SJeff.Bonwick@Sun.COM 
112110922SJeff.Bonwick@Sun.COM static uint64_t
112210922SJeff.Bonwick@Sun.COM ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr)
112310922SJeff.Bonwick@Sun.COM {
112410922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
112510922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
112610922SJeff.Bonwick@Sun.COM 	itx_t *itx;
112710922SJeff.Bonwick@Sun.COM 
112810922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
112910922SJeff.Bonwick@Sun.COM 		return (0);
113010922SJeff.Bonwick@Sun.COM 
113110922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
113210922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
113310922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
113410922SJeff.Bonwick@Sun.COM 
113510922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
113610922SJeff.Bonwick@Sun.COM }
113710922SJeff.Bonwick@Sun.COM 
113810922SJeff.Bonwick@Sun.COM static uint64_t
113910922SJeff.Bonwick@Sun.COM ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
114010922SJeff.Bonwick@Sun.COM {
114110922SJeff.Bonwick@Sun.COM 	itx_t *itx;
114210922SJeff.Bonwick@Sun.COM 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
114310922SJeff.Bonwick@Sun.COM 
114410922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
114510922SJeff.Bonwick@Sun.COM 		return (0);
114610922SJeff.Bonwick@Sun.COM 
114710922SJeff.Bonwick@Sun.COM 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
114810922SJeff.Bonwick@Sun.COM 		write_state = WR_INDIRECT;
114910922SJeff.Bonwick@Sun.COM 
115010922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_WRITE,
115110922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
115210922SJeff.Bonwick@Sun.COM 
115310922SJeff.Bonwick@Sun.COM 	if (write_state == WR_COPIED &&
115410922SJeff.Bonwick@Sun.COM 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
115510922SJeff.Bonwick@Sun.COM 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
115610922SJeff.Bonwick@Sun.COM 		zil_itx_destroy(itx);
115710922SJeff.Bonwick@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
115810922SJeff.Bonwick@Sun.COM 		write_state = WR_NEED_COPY;
115910922SJeff.Bonwick@Sun.COM 	}
116010922SJeff.Bonwick@Sun.COM 	itx->itx_private = zd;
116110922SJeff.Bonwick@Sun.COM 	itx->itx_wr_state = write_state;
116210922SJeff.Bonwick@Sun.COM 	itx->itx_sync = (ztest_random(8) == 0);
116310922SJeff.Bonwick@Sun.COM 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
116410922SJeff.Bonwick@Sun.COM 
116510922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
116610922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
116710922SJeff.Bonwick@Sun.COM 
116810922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
116910922SJeff.Bonwick@Sun.COM }
117010922SJeff.Bonwick@Sun.COM 
117110922SJeff.Bonwick@Sun.COM static uint64_t
117210922SJeff.Bonwick@Sun.COM ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
117310922SJeff.Bonwick@Sun.COM {
117410922SJeff.Bonwick@Sun.COM 	itx_t *itx;
117510922SJeff.Bonwick@Sun.COM 
117610922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
117710922SJeff.Bonwick@Sun.COM 		return (0);
117810922SJeff.Bonwick@Sun.COM 
117910922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
118010922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
118110922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
118210922SJeff.Bonwick@Sun.COM 
118310922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
118410922SJeff.Bonwick@Sun.COM }
118510922SJeff.Bonwick@Sun.COM 
118610922SJeff.Bonwick@Sun.COM static uint64_t
118710922SJeff.Bonwick@Sun.COM ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
118810922SJeff.Bonwick@Sun.COM {
118910922SJeff.Bonwick@Sun.COM 	itx_t *itx;
119010922SJeff.Bonwick@Sun.COM 
119110922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
119210922SJeff.Bonwick@Sun.COM 		return (0);
119310922SJeff.Bonwick@Sun.COM 
119410922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
119510922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
119610922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
119710922SJeff.Bonwick@Sun.COM 
119810922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
119910922SJeff.Bonwick@Sun.COM }
120010922SJeff.Bonwick@Sun.COM 
120110922SJeff.Bonwick@Sun.COM /*
120210922SJeff.Bonwick@Sun.COM  * ZIL replay ops
120310922SJeff.Bonwick@Sun.COM  */
120410922SJeff.Bonwick@Sun.COM static int
120510922SJeff.Bonwick@Sun.COM ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
120610922SJeff.Bonwick@Sun.COM {
120710922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
120810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
120910922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
121010922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1211789Sahrens 	dmu_tx_t *tx;
121210922SJeff.Bonwick@Sun.COM 	uint64_t txg;
121310922SJeff.Bonwick@Sun.COM 	int error = 0;
1214789Sahrens 
1215789Sahrens 	if (byteswap)
1216789Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
1217789Sahrens 
121810922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
121910922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
122010922SJeff.Bonwick@Sun.COM 
1221789Sahrens 	tx = dmu_tx_create(os);
122210922SJeff.Bonwick@Sun.COM 
122310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
122410922SJeff.Bonwick@Sun.COM 
122510922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
122610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
122710922SJeff.Bonwick@Sun.COM 	} else {
122810922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
122910922SJeff.Bonwick@Sun.COM 	}
123010922SJeff.Bonwick@Sun.COM 
123110922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
123210922SJeff.Bonwick@Sun.COM 	if (txg == 0)
123310922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
123410922SJeff.Bonwick@Sun.COM 
123510922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
123610922SJeff.Bonwick@Sun.COM 
123710922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
123810922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
123910922SJeff.Bonwick@Sun.COM 			lr->lr_foid = zap_create(os,
124010922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124110922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124210922SJeff.Bonwick@Sun.COM 		} else {
124310922SJeff.Bonwick@Sun.COM 			error = zap_create_claim(os, lr->lr_foid,
124410922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124510922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124610922SJeff.Bonwick@Sun.COM 		}
124710922SJeff.Bonwick@Sun.COM 	} else {
124810922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
124910922SJeff.Bonwick@Sun.COM 			lr->lr_foid = dmu_object_alloc(os,
125010922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125110922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125210922SJeff.Bonwick@Sun.COM 		} else {
125310922SJeff.Bonwick@Sun.COM 			error = dmu_object_claim(os, lr->lr_foid,
125410922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125510922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125610922SJeff.Bonwick@Sun.COM 		}
125710922SJeff.Bonwick@Sun.COM 	}
125810922SJeff.Bonwick@Sun.COM 
1259789Sahrens 	if (error) {
126010922SJeff.Bonwick@Sun.COM 		ASSERT3U(error, ==, EEXIST);
126110922SJeff.Bonwick@Sun.COM 		ASSERT(zd->zd_zilog->zl_replay);
126210922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1263789Sahrens 		return (error);
1264789Sahrens 	}
1265789Sahrens 
126610922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_foid != 0);
126710922SJeff.Bonwick@Sun.COM 
126810922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
126910922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
127010922SJeff.Bonwick@Sun.COM 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
127110922SJeff.Bonwick@Sun.COM 
127210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
127310922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
127410922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
127510922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
127610922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
127710922SJeff.Bonwick@Sun.COM 
127810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
127910922SJeff.Bonwick@Sun.COM 	    &lr->lr_foid, tx));
128010922SJeff.Bonwick@Sun.COM 
128110922SJeff.Bonwick@Sun.COM 	(void) ztest_log_create(zd, tx, lr);
128210922SJeff.Bonwick@Sun.COM 
128310922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
128410922SJeff.Bonwick@Sun.COM 
128510922SJeff.Bonwick@Sun.COM 	return (0);
128610922SJeff.Bonwick@Sun.COM }
128710922SJeff.Bonwick@Sun.COM 
128810922SJeff.Bonwick@Sun.COM static int
128910922SJeff.Bonwick@Sun.COM ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
129010922SJeff.Bonwick@Sun.COM {
129110922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
129210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
129310922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
129410922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
129510922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
129610922SJeff.Bonwick@Sun.COM 
129710922SJeff.Bonwick@Sun.COM 	if (byteswap)
129810922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
129910922SJeff.Bonwick@Sun.COM 
130010922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
130110922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
130210922SJeff.Bonwick@Sun.COM 
130310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
130410922SJeff.Bonwick@Sun.COM 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
130510922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
130610922SJeff.Bonwick@Sun.COM 
130710922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_WRITER);
130810922SJeff.Bonwick@Sun.COM 
130910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
131010922SJeff.Bonwick@Sun.COM 
131110922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
131210922SJeff.Bonwick@Sun.COM 
131310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
131410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
131510922SJeff.Bonwick@Sun.COM 
131610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
131710922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
131810922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
131910922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
132010922SJeff.Bonwick@Sun.COM 	}
132110922SJeff.Bonwick@Sun.COM 
132210922SJeff.Bonwick@Sun.COM 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
132310922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
132410922SJeff.Bonwick@Sun.COM 	} else {
132510922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
132610922SJeff.Bonwick@Sun.COM 	}
132710922SJeff.Bonwick@Sun.COM 
132810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
132910922SJeff.Bonwick@Sun.COM 
133010922SJeff.Bonwick@Sun.COM 	(void) ztest_log_remove(zd, tx, lr);
133110922SJeff.Bonwick@Sun.COM 
1332789Sahrens 	dmu_tx_commit(tx);
1333789Sahrens 
133410922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
133510922SJeff.Bonwick@Sun.COM 
133610922SJeff.Bonwick@Sun.COM 	return (0);
133710922SJeff.Bonwick@Sun.COM }
133810922SJeff.Bonwick@Sun.COM 
133910922SJeff.Bonwick@Sun.COM static int
134010922SJeff.Bonwick@Sun.COM ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
134110922SJeff.Bonwick@Sun.COM {
134210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
134310922SJeff.Bonwick@Sun.COM 	void *data = lr + 1;			/* data follows lr */
134410922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
134510922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt = data;
134610922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
134710922SJeff.Bonwick@Sun.COM 	uint64_t gen, txg, lrtxg, crtxg;
134810922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
134910922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
135010922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
135110922SJeff.Bonwick@Sun.COM 	arc_buf_t *abuf = NULL;
135210922SJeff.Bonwick@Sun.COM 	rl_t *rl;
135310922SJeff.Bonwick@Sun.COM 
135410922SJeff.Bonwick@Sun.COM 	if (byteswap)
135510922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
135610922SJeff.Bonwick@Sun.COM 
135710922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
135810922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
135910922SJeff.Bonwick@Sun.COM 
136010922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
136110922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
136210922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
136310922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
136410922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
136510922SJeff.Bonwick@Sun.COM 			length = blocksize;
136610922SJeff.Bonwick@Sun.COM 		}
136710922SJeff.Bonwick@Sun.COM 	}
136810922SJeff.Bonwick@Sun.COM 
136910922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
137010922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(bt, sizeof (*bt));
137110922SJeff.Bonwick@Sun.COM 
137210922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic != BT_MAGIC)
137310922SJeff.Bonwick@Sun.COM 		bt = NULL;
137410922SJeff.Bonwick@Sun.COM 
137510922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
137610922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
137710922SJeff.Bonwick@Sun.COM 
137810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
137910922SJeff.Bonwick@Sun.COM 
138010922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
138110922SJeff.Bonwick@Sun.COM 
138210922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
138310922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
138410922SJeff.Bonwick@Sun.COM 	gen = bbt->bt_gen;
138510922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
138610922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
138710922SJeff.Bonwick@Sun.COM 
138810922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
138910922SJeff.Bonwick@Sun.COM 
139010922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
139110922SJeff.Bonwick@Sun.COM 
139210922SJeff.Bonwick@Sun.COM 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
139310922SJeff.Bonwick@Sun.COM 	    P2PHASE(offset, length) == 0)
139410922SJeff.Bonwick@Sun.COM 		abuf = dmu_request_arcbuf(db, length);
139510922SJeff.Bonwick@Sun.COM 
139610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
139710922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
139810922SJeff.Bonwick@Sun.COM 		if (abuf != NULL)
139910922SJeff.Bonwick@Sun.COM 			dmu_return_arcbuf(abuf);
140010922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
140110922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
140210922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
140310922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
140410922SJeff.Bonwick@Sun.COM 	}
140510922SJeff.Bonwick@Sun.COM 
140610922SJeff.Bonwick@Sun.COM 	if (bt != NULL) {
140710922SJeff.Bonwick@Sun.COM 		/*
140810922SJeff.Bonwick@Sun.COM 		 * Usually, verify the old data before writing new data --
140910922SJeff.Bonwick@Sun.COM 		 * but not always, because we also want to verify correct
141010922SJeff.Bonwick@Sun.COM 		 * behavior when the data was not recently read into cache.
141110922SJeff.Bonwick@Sun.COM 		 */
141210922SJeff.Bonwick@Sun.COM 		ASSERT(offset % doi.doi_data_block_size == 0);
141310922SJeff.Bonwick@Sun.COM 		if (ztest_random(4) != 0) {
141410922SJeff.Bonwick@Sun.COM 			int prefetch = ztest_random(2) ?
141510922SJeff.Bonwick@Sun.COM 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
141610922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t rbt;
141710922SJeff.Bonwick@Sun.COM 
141810922SJeff.Bonwick@Sun.COM 			VERIFY(dmu_read(os, lr->lr_foid, offset,
141910922SJeff.Bonwick@Sun.COM 			    sizeof (rbt), &rbt, prefetch) == 0);
142010922SJeff.Bonwick@Sun.COM 			if (rbt.bt_magic == BT_MAGIC) {
142110922SJeff.Bonwick@Sun.COM 				ztest_bt_verify(&rbt, os, lr->lr_foid,
142210922SJeff.Bonwick@Sun.COM 				    offset, gen, txg, crtxg);
142310922SJeff.Bonwick@Sun.COM 			}
142410922SJeff.Bonwick@Sun.COM 		}
142510922SJeff.Bonwick@Sun.COM 
142610922SJeff.Bonwick@Sun.COM 		/*
142710922SJeff.Bonwick@Sun.COM 		 * Writes can appear to be newer than the bonus buffer because
142810922SJeff.Bonwick@Sun.COM 		 * the ztest_get_data() callback does a dmu_read() of the
142910922SJeff.Bonwick@Sun.COM 		 * open-context data, which may be different than the data
143010922SJeff.Bonwick@Sun.COM 		 * as it was when the write was generated.
143110922SJeff.Bonwick@Sun.COM 		 */
143210922SJeff.Bonwick@Sun.COM 		if (zd->zd_zilog->zl_replay) {
143310922SJeff.Bonwick@Sun.COM 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
143410922SJeff.Bonwick@Sun.COM 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
143510922SJeff.Bonwick@Sun.COM 			    bt->bt_crtxg);
143610922SJeff.Bonwick@Sun.COM 		}
143710922SJeff.Bonwick@Sun.COM 
143810922SJeff.Bonwick@Sun.COM 		/*
143910922SJeff.Bonwick@Sun.COM 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
144010922SJeff.Bonwick@Sun.COM 		 * so that all of the usual ASSERTs will work.
144110922SJeff.Bonwick@Sun.COM 		 */
144210922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
144310922SJeff.Bonwick@Sun.COM 	}
144410922SJeff.Bonwick@Sun.COM 
144510922SJeff.Bonwick@Sun.COM 	if (abuf == NULL) {
144610922SJeff.Bonwick@Sun.COM 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
144710922SJeff.Bonwick@Sun.COM 	} else {
144810922SJeff.Bonwick@Sun.COM 		bcopy(data, abuf->b_data, length);
144910922SJeff.Bonwick@Sun.COM 		dmu_assign_arcbuf(db, offset, abuf, tx);
145010922SJeff.Bonwick@Sun.COM 	}
145110922SJeff.Bonwick@Sun.COM 
145210922SJeff.Bonwick@Sun.COM 	(void) ztest_log_write(zd, tx, lr);
145310922SJeff.Bonwick@Sun.COM 
145410922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
145510922SJeff.Bonwick@Sun.COM 
145610922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
145710922SJeff.Bonwick@Sun.COM 
145810922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
145910922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
146010922SJeff.Bonwick@Sun.COM 
146110922SJeff.Bonwick@Sun.COM 	return (0);
146210922SJeff.Bonwick@Sun.COM }
146310922SJeff.Bonwick@Sun.COM 
146410922SJeff.Bonwick@Sun.COM static int
146510922SJeff.Bonwick@Sun.COM ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
146610922SJeff.Bonwick@Sun.COM {
146710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
146810922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
146910922SJeff.Bonwick@Sun.COM 	uint64_t txg;
147010922SJeff.Bonwick@Sun.COM 	rl_t *rl;
147110922SJeff.Bonwick@Sun.COM 
147210922SJeff.Bonwick@Sun.COM 	if (byteswap)
147310922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
147410922SJeff.Bonwick@Sun.COM 
147510922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
147610922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
147710922SJeff.Bonwick@Sun.COM 	    RL_WRITER);
147810922SJeff.Bonwick@Sun.COM 
147910922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
148010922SJeff.Bonwick@Sun.COM 
148110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
148210922SJeff.Bonwick@Sun.COM 
148310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
148410922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
148510922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
148610922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
148710922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
148810922SJeff.Bonwick@Sun.COM 	}
148910922SJeff.Bonwick@Sun.COM 
149010922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
149110922SJeff.Bonwick@Sun.COM 	    lr->lr_length, tx) == 0);
149210922SJeff.Bonwick@Sun.COM 
149310922SJeff.Bonwick@Sun.COM 	(void) ztest_log_truncate(zd, tx, lr);
149410922SJeff.Bonwick@Sun.COM 
149510922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
149610922SJeff.Bonwick@Sun.COM 
149710922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
149810922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
149910922SJeff.Bonwick@Sun.COM 
150010922SJeff.Bonwick@Sun.COM 	return (0);
150110922SJeff.Bonwick@Sun.COM }
150210922SJeff.Bonwick@Sun.COM 
150310922SJeff.Bonwick@Sun.COM static int
150410922SJeff.Bonwick@Sun.COM ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
150510922SJeff.Bonwick@Sun.COM {
150610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
150710922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
150810922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
150910922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
151010922SJeff.Bonwick@Sun.COM 	uint64_t txg, lrtxg, crtxg;
151110922SJeff.Bonwick@Sun.COM 
151210922SJeff.Bonwick@Sun.COM 	if (byteswap)
151310922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
151410922SJeff.Bonwick@Sun.COM 
151510922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
151610922SJeff.Bonwick@Sun.COM 
151710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
151810922SJeff.Bonwick@Sun.COM 
151910922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
152010922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_bonus(tx, lr->lr_foid);
152110922SJeff.Bonwick@Sun.COM 
152210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
152310922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
152410922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
152510922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
152610922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
152710922SJeff.Bonwick@Sun.COM 	}
152810922SJeff.Bonwick@Sun.COM 
152910922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
153010922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
153110922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
153210922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
153310922SJeff.Bonwick@Sun.COM 
153410922SJeff.Bonwick@Sun.COM 	if (zd->zd_zilog->zl_replay) {
153510922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_size != 0);
153610922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_mode != 0);
153710922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg != 0);
153810922SJeff.Bonwick@Sun.COM 	} else {
153910922SJeff.Bonwick@Sun.COM 		/*
154010922SJeff.Bonwick@Sun.COM 		 * Randomly change the size and increment the generation.
154110922SJeff.Bonwick@Sun.COM 		 */
154210922SJeff.Bonwick@Sun.COM 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
154310922SJeff.Bonwick@Sun.COM 		    sizeof (*bbt);
154410922SJeff.Bonwick@Sun.COM 		lr->lr_mode = bbt->bt_gen + 1;
154510922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg == 0);
154610922SJeff.Bonwick@Sun.COM 	}
154710922SJeff.Bonwick@Sun.COM 
154810922SJeff.Bonwick@Sun.COM 	/*
154910922SJeff.Bonwick@Sun.COM 	 * Verify that the current bonus buffer is not newer than our txg.
155010922SJeff.Bonwick@Sun.COM 	 */
155110922SJeff.Bonwick@Sun.COM 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
155210922SJeff.Bonwick@Sun.COM 	    MAX(txg, lrtxg), crtxg);
155310922SJeff.Bonwick@Sun.COM 
155410922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
155510922SJeff.Bonwick@Sun.COM 
155610922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
155710922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, <=, db->db_size);
155810922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
155910922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
156010922SJeff.Bonwick@Sun.COM 
156110922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
156210922SJeff.Bonwick@Sun.COM 
156310922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
156410922SJeff.Bonwick@Sun.COM 
156510922SJeff.Bonwick@Sun.COM 	(void) ztest_log_setattr(zd, tx, lr);
156610922SJeff.Bonwick@Sun.COM 
156710922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
156810922SJeff.Bonwick@Sun.COM 
156910922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
157010922SJeff.Bonwick@Sun.COM 
157110922SJeff.Bonwick@Sun.COM 	return (0);
1572789Sahrens }
1573789Sahrens 
1574789Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1575789Sahrens 	NULL,			/* 0 no such transaction type */
1576789Sahrens 	ztest_replay_create,	/* TX_CREATE */
1577789Sahrens 	NULL,			/* TX_MKDIR */
1578789Sahrens 	NULL,			/* TX_MKXATTR */
1579789Sahrens 	NULL,			/* TX_SYMLINK */
1580789Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
1581789Sahrens 	NULL,			/* TX_RMDIR */
1582789Sahrens 	NULL,			/* TX_LINK */
1583789Sahrens 	NULL,			/* TX_RENAME */
158410922SJeff.Bonwick@Sun.COM 	ztest_replay_write,	/* TX_WRITE */
158510922SJeff.Bonwick@Sun.COM 	ztest_replay_truncate,	/* TX_TRUNCATE */
158610922SJeff.Bonwick@Sun.COM 	ztest_replay_setattr,	/* TX_SETATTR */
1587789Sahrens 	NULL,			/* TX_ACL */
158810800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL */
158910800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ATTR */
159010800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL_ATTR */
159110800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL */
159210800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ATTR */
159310800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL_ATTR */
159410800SNeil.Perrin@Sun.COM 	NULL,			/* TX_WRITE2 */
1595789Sahrens };
1596789Sahrens 
1597789Sahrens /*
159810922SJeff.Bonwick@Sun.COM  * ZIL get_data callbacks
159910922SJeff.Bonwick@Sun.COM  */
160010922SJeff.Bonwick@Sun.COM 
160110922SJeff.Bonwick@Sun.COM static void
160210922SJeff.Bonwick@Sun.COM ztest_get_done(zgd_t *zgd, int error)
160310922SJeff.Bonwick@Sun.COM {
160410922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = zgd->zgd_private;
160510922SJeff.Bonwick@Sun.COM 	uint64_t object = zgd->zgd_rl->rl_object;
160610922SJeff.Bonwick@Sun.COM 
160710922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
160810922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
160910922SJeff.Bonwick@Sun.COM 
161010922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(zgd->zgd_rl);
161110922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
161210922SJeff.Bonwick@Sun.COM 
161310922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
161410922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
161510922SJeff.Bonwick@Sun.COM 
161610922SJeff.Bonwick@Sun.COM 	umem_free(zgd, sizeof (*zgd));
161710922SJeff.Bonwick@Sun.COM }
161810922SJeff.Bonwick@Sun.COM 
161910922SJeff.Bonwick@Sun.COM static int
162010922SJeff.Bonwick@Sun.COM ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
162110922SJeff.Bonwick@Sun.COM {
162210922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = arg;
162310922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
162410922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
162510922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
162610922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
162710922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
162810922SJeff.Bonwick@Sun.COM 	uint64_t txg = lr->lr_common.lrc_txg;
162910922SJeff.Bonwick@Sun.COM 	uint64_t crtxg;
163010922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
163110922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
163210922SJeff.Bonwick@Sun.COM 	zgd_t *zgd;
163310922SJeff.Bonwick@Sun.COM 	int error;
163410922SJeff.Bonwick@Sun.COM 
163510922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
163610922SJeff.Bonwick@Sun.COM 	error = dmu_bonus_hold(os, object, FTAG, &db);
163710922SJeff.Bonwick@Sun.COM 	if (error) {
163810922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
163910922SJeff.Bonwick@Sun.COM 		return (error);
164010922SJeff.Bonwick@Sun.COM 	}
164110922SJeff.Bonwick@Sun.COM 
164210922SJeff.Bonwick@Sun.COM 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
164310922SJeff.Bonwick@Sun.COM 
164410922SJeff.Bonwick@Sun.COM 	if (crtxg == 0 || crtxg > txg) {
164510922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
164610922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
164710922SJeff.Bonwick@Sun.COM 		return (ENOENT);
164810922SJeff.Bonwick@Sun.COM 	}
164910922SJeff.Bonwick@Sun.COM 
165010922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
165110922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
165210922SJeff.Bonwick@Sun.COM 	db = NULL;
165310922SJeff.Bonwick@Sun.COM 
165410922SJeff.Bonwick@Sun.COM 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
165510922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zd->zd_zilog;
165610922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zd;
165710922SJeff.Bonwick@Sun.COM 
165810922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
165910922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
166010922SJeff.Bonwick@Sun.COM 		    RL_READER);
166110922SJeff.Bonwick@Sun.COM 
166210922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
166310922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
166410922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0);
166510922SJeff.Bonwick@Sun.COM 	} else {
166610922SJeff.Bonwick@Sun.COM 		size = doi.doi_data_block_size;
166710945SJeff.Bonwick@Sun.COM 		if (ISP2(size)) {
166810922SJeff.Bonwick@Sun.COM 			offset = P2ALIGN(offset, size);
166910945SJeff.Bonwick@Sun.COM 		} else {
167010945SJeff.Bonwick@Sun.COM 			ASSERT(offset < size);
167110945SJeff.Bonwick@Sun.COM 			offset = 0;
167210945SJeff.Bonwick@Sun.COM 		}
167310922SJeff.Bonwick@Sun.COM 
167410922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
167510922SJeff.Bonwick@Sun.COM 		    RL_READER);
167610922SJeff.Bonwick@Sun.COM 
167712285SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db,
167812285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
167910922SJeff.Bonwick@Sun.COM 
168010922SJeff.Bonwick@Sun.COM 		if (error == 0) {
168110922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
168210922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
168310922SJeff.Bonwick@Sun.COM 
168410922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
168510922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
168610922SJeff.Bonwick@Sun.COM 
168710922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
168810922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
168910922SJeff.Bonwick@Sun.COM 
169010922SJeff.Bonwick@Sun.COM 			if (error == 0)
169110922SJeff.Bonwick@Sun.COM 				return (0);
169210922SJeff.Bonwick@Sun.COM 		}
169310922SJeff.Bonwick@Sun.COM 	}
169410922SJeff.Bonwick@Sun.COM 
169510922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
169610922SJeff.Bonwick@Sun.COM 
169710922SJeff.Bonwick@Sun.COM 	return (error);
169810922SJeff.Bonwick@Sun.COM }
169910922SJeff.Bonwick@Sun.COM 
170010922SJeff.Bonwick@Sun.COM static void *
170110922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
170210922SJeff.Bonwick@Sun.COM {
170310922SJeff.Bonwick@Sun.COM 	char *lr;
170410922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
170510922SJeff.Bonwick@Sun.COM 
170610922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
170710922SJeff.Bonwick@Sun.COM 
170810922SJeff.Bonwick@Sun.COM 	if (name)
170910922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
171010922SJeff.Bonwick@Sun.COM 
171110922SJeff.Bonwick@Sun.COM 	return (lr);
171210922SJeff.Bonwick@Sun.COM }
171310922SJeff.Bonwick@Sun.COM 
171410922SJeff.Bonwick@Sun.COM void
171510922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
171610922SJeff.Bonwick@Sun.COM {
171710922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
171810922SJeff.Bonwick@Sun.COM 
171910922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
172010922SJeff.Bonwick@Sun.COM }
172110922SJeff.Bonwick@Sun.COM 
172210922SJeff.Bonwick@Sun.COM /*
172310922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
172410922SJeff.Bonwick@Sun.COM  */
172510922SJeff.Bonwick@Sun.COM static int
172610922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
172710922SJeff.Bonwick@Sun.COM {
172810922SJeff.Bonwick@Sun.COM 	int missing = 0;
172910922SJeff.Bonwick@Sun.COM 	int error;
173010922SJeff.Bonwick@Sun.COM 
173110922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
173210922SJeff.Bonwick@Sun.COM 
173310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
173410922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
173510922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
173610922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
173710922SJeff.Bonwick@Sun.COM 		if (error) {
173810922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
173910922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
174010922SJeff.Bonwick@Sun.COM 			missing++;
174110922SJeff.Bonwick@Sun.COM 		} else {
174210922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
174310922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
174410922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
174510922SJeff.Bonwick@Sun.COM 
174610922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
174710922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
174810922SJeff.Bonwick@Sun.COM 
174910922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
175010922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
175110922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
175210922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
175310922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
175410922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
175510922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
175610922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
175710922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
175810922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
175910922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
176010922SJeff.Bonwick@Sun.COM 		}
176110922SJeff.Bonwick@Sun.COM 	}
176210922SJeff.Bonwick@Sun.COM 
176310922SJeff.Bonwick@Sun.COM 	return (missing);
176410922SJeff.Bonwick@Sun.COM }
176510922SJeff.Bonwick@Sun.COM 
176610922SJeff.Bonwick@Sun.COM static int
176710922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
176810922SJeff.Bonwick@Sun.COM {
176910922SJeff.Bonwick@Sun.COM 	int missing = 0;
177010922SJeff.Bonwick@Sun.COM 
177110922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
177210922SJeff.Bonwick@Sun.COM 
177310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
177410922SJeff.Bonwick@Sun.COM 		if (missing) {
177510922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
177610922SJeff.Bonwick@Sun.COM 			missing++;
177710922SJeff.Bonwick@Sun.COM 			continue;
177810922SJeff.Bonwick@Sun.COM 		}
177910922SJeff.Bonwick@Sun.COM 
178010922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
178110922SJeff.Bonwick@Sun.COM 
178210922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
178310922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
178410922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
178510922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
178610922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
178710922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
178810922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
178910922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
179010922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
179110922SJeff.Bonwick@Sun.COM 
179210922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
179310922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
179410922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
179510922SJeff.Bonwick@Sun.COM 			missing++;
179610922SJeff.Bonwick@Sun.COM 		} else {
179710922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
179810922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
179910922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
180010922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
180110922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
180210922SJeff.Bonwick@Sun.COM 		}
180310922SJeff.Bonwick@Sun.COM 
180410922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
180510922SJeff.Bonwick@Sun.COM 	}
180610922SJeff.Bonwick@Sun.COM 
180710922SJeff.Bonwick@Sun.COM 	return (missing);
180810922SJeff.Bonwick@Sun.COM }
180910922SJeff.Bonwick@Sun.COM 
181010922SJeff.Bonwick@Sun.COM static int
181110922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
181210922SJeff.Bonwick@Sun.COM {
181310922SJeff.Bonwick@Sun.COM 	int missing = 0;
181410922SJeff.Bonwick@Sun.COM 	int error;
181510922SJeff.Bonwick@Sun.COM 
181610922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
181710922SJeff.Bonwick@Sun.COM 
181810922SJeff.Bonwick@Sun.COM 	od += count - 1;
181910922SJeff.Bonwick@Sun.COM 
182010922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
182110922SJeff.Bonwick@Sun.COM 		if (missing) {
182210922SJeff.Bonwick@Sun.COM 			missing++;
182310922SJeff.Bonwick@Sun.COM 			continue;
182410922SJeff.Bonwick@Sun.COM 		}
182510922SJeff.Bonwick@Sun.COM 
182610922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
182710922SJeff.Bonwick@Sun.COM 			continue;
182810922SJeff.Bonwick@Sun.COM 
182910922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
183010922SJeff.Bonwick@Sun.COM 
183110922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
183210922SJeff.Bonwick@Sun.COM 
183310922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
183410922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
183510922SJeff.Bonwick@Sun.COM 			missing++;
183610922SJeff.Bonwick@Sun.COM 		} else {
183710922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
183810922SJeff.Bonwick@Sun.COM 		}
183910922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
184010922SJeff.Bonwick@Sun.COM 	}
184110922SJeff.Bonwick@Sun.COM 
184210922SJeff.Bonwick@Sun.COM 	return (missing);
184310922SJeff.Bonwick@Sun.COM }
184410922SJeff.Bonwick@Sun.COM 
184510922SJeff.Bonwick@Sun.COM static int
184610922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
184710922SJeff.Bonwick@Sun.COM     void *data)
184810922SJeff.Bonwick@Sun.COM {
184910922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
185010922SJeff.Bonwick@Sun.COM 	int error;
185110922SJeff.Bonwick@Sun.COM 
185210922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
185310922SJeff.Bonwick@Sun.COM 
185410922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
185510922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
185610922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
185710922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
185810922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
185910922SJeff.Bonwick@Sun.COM 
186010922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
186110922SJeff.Bonwick@Sun.COM 
186210922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
186310922SJeff.Bonwick@Sun.COM 
186410922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
186510922SJeff.Bonwick@Sun.COM 
186610922SJeff.Bonwick@Sun.COM 	return (error);
186710922SJeff.Bonwick@Sun.COM }
186810922SJeff.Bonwick@Sun.COM 
186910922SJeff.Bonwick@Sun.COM static int
187010922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
187110922SJeff.Bonwick@Sun.COM {
187210922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
187310922SJeff.Bonwick@Sun.COM 	int error;
187410922SJeff.Bonwick@Sun.COM 
187510922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
187610922SJeff.Bonwick@Sun.COM 
187710922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
187810922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
187910922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
188010922SJeff.Bonwick@Sun.COM 
188110922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
188210922SJeff.Bonwick@Sun.COM 
188310922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
188410922SJeff.Bonwick@Sun.COM 
188510922SJeff.Bonwick@Sun.COM 	return (error);
188610922SJeff.Bonwick@Sun.COM }
188710922SJeff.Bonwick@Sun.COM 
188810922SJeff.Bonwick@Sun.COM static int
188910922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
189010922SJeff.Bonwick@Sun.COM {
189110922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
189210922SJeff.Bonwick@Sun.COM 	int error;
189310922SJeff.Bonwick@Sun.COM 
189410922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
189510922SJeff.Bonwick@Sun.COM 
189610922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
189710922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
189810922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
189910922SJeff.Bonwick@Sun.COM 
190010922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
190110922SJeff.Bonwick@Sun.COM 
190210922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
190310922SJeff.Bonwick@Sun.COM 
190410922SJeff.Bonwick@Sun.COM 	return (error);
190510922SJeff.Bonwick@Sun.COM }
190610922SJeff.Bonwick@Sun.COM 
190710922SJeff.Bonwick@Sun.COM static void
190810922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
190910922SJeff.Bonwick@Sun.COM {
191010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
191110922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
191210922SJeff.Bonwick@Sun.COM 	uint64_t txg;
191310922SJeff.Bonwick@Sun.COM 	rl_t *rl;
191410922SJeff.Bonwick@Sun.COM 
191510922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
191610922SJeff.Bonwick@Sun.COM 
191710922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
191810922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
191910922SJeff.Bonwick@Sun.COM 
192010922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
192110922SJeff.Bonwick@Sun.COM 
192210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
192310922SJeff.Bonwick@Sun.COM 
192410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
192510922SJeff.Bonwick@Sun.COM 
192610922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
192710922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
192810922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
192910922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
193010922SJeff.Bonwick@Sun.COM 	} else {
193110922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
193210922SJeff.Bonwick@Sun.COM 	}
193310922SJeff.Bonwick@Sun.COM 
193410922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
193510922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
193610922SJeff.Bonwick@Sun.COM }
193710922SJeff.Bonwick@Sun.COM 
193810922SJeff.Bonwick@Sun.COM static void
193910922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
194010922SJeff.Bonwick@Sun.COM {
194110922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
194210922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
194310922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
194410922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
194510922SJeff.Bonwick@Sun.COM 	void *data;
194610922SJeff.Bonwick@Sun.COM 
194710922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
194810922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
194910922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
195010922SJeff.Bonwick@Sun.COM 
195110922SJeff.Bonwick@Sun.COM 	/*
195210922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
195310922SJeff.Bonwick@Sun.COM 	 */
195410922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
195510922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
195610922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
195710922SJeff.Bonwick@Sun.COM 
195810922SJeff.Bonwick@Sun.COM 	switch (io_type) {
195910922SJeff.Bonwick@Sun.COM 
196010922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
196110922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
196210922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
196310922SJeff.Bonwick@Sun.COM 		break;
196410922SJeff.Bonwick@Sun.COM 
196510922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
196610922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
196710922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
196810922SJeff.Bonwick@Sun.COM 			/*
196910922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
197010922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
197110922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
197210922SJeff.Bonwick@Sun.COM 			 */
197310922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
197410922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
197510922SJeff.Bonwick@Sun.COM 		}
197610922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
197710922SJeff.Bonwick@Sun.COM 		break;
197810922SJeff.Bonwick@Sun.COM 
197910922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
198010922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
198110922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
198210922SJeff.Bonwick@Sun.COM 		break;
198310922SJeff.Bonwick@Sun.COM 
198410922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
198510922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
198610922SJeff.Bonwick@Sun.COM 		break;
198710922SJeff.Bonwick@Sun.COM 
198810922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
198910922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
199010922SJeff.Bonwick@Sun.COM 		break;
199110922SJeff.Bonwick@Sun.COM 	}
199210922SJeff.Bonwick@Sun.COM 
199310922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
199410922SJeff.Bonwick@Sun.COM }
199510922SJeff.Bonwick@Sun.COM 
199610922SJeff.Bonwick@Sun.COM /*
199710922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
199810922SJeff.Bonwick@Sun.COM  */
199910922SJeff.Bonwick@Sun.COM static void
200010922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
200110922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
200210922SJeff.Bonwick@Sun.COM {
200310922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
200410922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
200510922SJeff.Bonwick@Sun.COM 
200610922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
200710922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
200810922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
200910922SJeff.Bonwick@Sun.COM 
201010922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
201110922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
201210922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
201310922SJeff.Bonwick@Sun.COM 
201410922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
201510922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
201610922SJeff.Bonwick@Sun.COM }
201710922SJeff.Bonwick@Sun.COM 
201810922SJeff.Bonwick@Sun.COM /*
201910922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
202010922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
202110922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
202210922SJeff.Bonwick@Sun.COM  * use the existing objects.
202310922SJeff.Bonwick@Sun.COM  */
202410922SJeff.Bonwick@Sun.COM static int
202510922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
202610922SJeff.Bonwick@Sun.COM {
202710922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
202810922SJeff.Bonwick@Sun.COM 	int rv = 0;
202910922SJeff.Bonwick@Sun.COM 
203010922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
203110922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
203210922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
203310922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
203410922SJeff.Bonwick@Sun.COM 		rv = -1;
203510922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
203610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
203710922SJeff.Bonwick@Sun.COM 
203810922SJeff.Bonwick@Sun.COM 	return (rv);
203910922SJeff.Bonwick@Sun.COM }
204010922SJeff.Bonwick@Sun.COM 
204110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
204210922SJeff.Bonwick@Sun.COM void
204310922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
204410922SJeff.Bonwick@Sun.COM {
204510922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
204610922SJeff.Bonwick@Sun.COM 
204710922SJeff.Bonwick@Sun.COM 	zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS));
204810922SJeff.Bonwick@Sun.COM 
204910922SJeff.Bonwick@Sun.COM 	/*
205010922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
205110922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
205210922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
205310922SJeff.Bonwick@Sun.COM 	 */
205410922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
205510922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
205610922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
205710922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
205810922SJeff.Bonwick@Sun.COM }
205910922SJeff.Bonwick@Sun.COM 
206010922SJeff.Bonwick@Sun.COM /*
2061789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2062789Sahrens  * or create a pool with a bad vdev spec.
2063789Sahrens  */
206410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2065789Sahrens void
206610922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2067789Sahrens {
206810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2069789Sahrens 	spa_t *spa;
2070789Sahrens 	nvlist_t *nvroot;
2071789Sahrens 
2072789Sahrens 	/*
2073789Sahrens 	 * Attempt to create using a bad file.
2074789Sahrens 	 */
20757754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
207610922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
207710922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2078789Sahrens 	nvlist_free(nvroot);
2079789Sahrens 
2080789Sahrens 	/*
2081789Sahrens 	 * Attempt to create using a bad mirror.
2082789Sahrens 	 */
20837754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
208410922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
208510922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2086789Sahrens 	nvlist_free(nvroot);
2087789Sahrens 
2088789Sahrens 	/*
2089789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2090789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2091789Sahrens 	 */
209210922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20937754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
209410922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2095789Sahrens 	nvlist_free(nvroot);
209610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
209710922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2098789Sahrens 	spa_close(spa, FTAG);
209910922SJeff.Bonwick@Sun.COM 
210010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2101789Sahrens }
2102789Sahrens 
21037768SJeff.Bonwick@Sun.COM static vdev_t *
21047768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
21057768SJeff.Bonwick@Sun.COM {
21067768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
21077768SJeff.Bonwick@Sun.COM 
21087768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
21097768SJeff.Bonwick@Sun.COM 		return (vd);
21107768SJeff.Bonwick@Sun.COM 
21117768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
21127768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
21137768SJeff.Bonwick@Sun.COM 		    NULL)
21147768SJeff.Bonwick@Sun.COM 			return (mvd);
21157768SJeff.Bonwick@Sun.COM 
21167768SJeff.Bonwick@Sun.COM 	return (NULL);
21177768SJeff.Bonwick@Sun.COM }
21187768SJeff.Bonwick@Sun.COM 
2119789Sahrens /*
212010594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
212110594SGeorge.Wilson@Sun.COM  */
212210594SGeorge.Wilson@Sun.COM int
212310594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
212410594SGeorge.Wilson@Sun.COM {
212510594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
212610594SGeorge.Wilson@Sun.COM 	int c;
212710594SGeorge.Wilson@Sun.COM 
212810594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
212910594SGeorge.Wilson@Sun.COM 
213010594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
213110594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
213210594SGeorge.Wilson@Sun.COM 
213310594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
213410594SGeorge.Wilson@Sun.COM 			break;
213510594SGeorge.Wilson@Sun.COM 	}
213610594SGeorge.Wilson@Sun.COM 	return (c);
213710594SGeorge.Wilson@Sun.COM }
213810594SGeorge.Wilson@Sun.COM 
213910594SGeorge.Wilson@Sun.COM /*
2140789Sahrens  * Verify that vdev_add() works as expected.
2141789Sahrens  */
214210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2143789Sahrens void
214410922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2145789Sahrens {
214610922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
214710922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
214811422SMark.Musante@Sun.COM 	uint64_t leaves;
214910594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2150789Sahrens 	nvlist_t *nvroot;
2151789Sahrens 	int error;
2152789Sahrens 
215310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
215411422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2155789Sahrens 
21567754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2157789Sahrens 
215810594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2159789Sahrens 
21604527Sperrin 	/*
216110594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21624527Sperrin 	 */
216310594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
216410594SGeorge.Wilson@Sun.COM 		/*
216510594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
216610594SGeorge.Wilson@Sun.COM 		 */
216710922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
216810594SGeorge.Wilson@Sun.COM 
216910594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
217010594SGeorge.Wilson@Sun.COM 
217110594SGeorge.Wilson@Sun.COM 		/*
217210594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
217310594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
217410594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
217510594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
217610594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
217710594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
217810594SGeorge.Wilson@Sun.COM 		 */
217910922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
218010594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
218110922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
218210594SGeorge.Wilson@Sun.COM 
218310594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
218410594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
218510594SGeorge.Wilson@Sun.COM 	} else {
218610594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
218710594SGeorge.Wilson@Sun.COM 
218810594SGeorge.Wilson@Sun.COM 		/*
218910594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
219010594SGeorge.Wilson@Sun.COM 		 */
219110594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
219211422SMark.Musante@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
219310594SGeorge.Wilson@Sun.COM 
219410594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
219510594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
219610594SGeorge.Wilson@Sun.COM 
219710594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
219810594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
219910594SGeorge.Wilson@Sun.COM 		else if (error != 0)
220010594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
220110594SGeorge.Wilson@Sun.COM 	}
2202789Sahrens 
220310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
22047754SJeff.Bonwick@Sun.COM }
22057754SJeff.Bonwick@Sun.COM 
22067754SJeff.Bonwick@Sun.COM /*
22077754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
22087754SJeff.Bonwick@Sun.COM  */
220910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
22107754SJeff.Bonwick@Sun.COM void
221110922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
22127754SJeff.Bonwick@Sun.COM {
221310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
221410922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22157768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22167754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22177754SJeff.Bonwick@Sun.COM 	char *aux;
22187754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22197754SJeff.Bonwick@Sun.COM 	int error;
22207754SJeff.Bonwick@Sun.COM 
22217768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22227754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22237754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22247754SJeff.Bonwick@Sun.COM 	} else {
22257754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22267754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22277754SJeff.Bonwick@Sun.COM 	}
22287754SJeff.Bonwick@Sun.COM 
222910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22307754SJeff.Bonwick@Sun.COM 
22317754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22327754SJeff.Bonwick@Sun.COM 
22337754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22347754SJeff.Bonwick@Sun.COM 		/*
22357754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22367754SJeff.Bonwick@Sun.COM 		 */
22377754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22387754SJeff.Bonwick@Sun.COM 	} else {
22397754SJeff.Bonwick@Sun.COM 		/*
22407754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22417754SJeff.Bonwick@Sun.COM 		 */
224210922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22437754SJeff.Bonwick@Sun.COM 		for (;;) {
22447754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22457754SJeff.Bonwick@Sun.COM 			int c;
22467754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
224710922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22487754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22497754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22507754SJeff.Bonwick@Sun.COM 				    path) == 0)
22517754SJeff.Bonwick@Sun.COM 					break;
22527768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22537768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22547754SJeff.Bonwick@Sun.COM 				break;
225510922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22567754SJeff.Bonwick@Sun.COM 		}
22577754SJeff.Bonwick@Sun.COM 	}
22587754SJeff.Bonwick@Sun.COM 
22597754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22607754SJeff.Bonwick@Sun.COM 
22617754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22627754SJeff.Bonwick@Sun.COM 		/*
22637754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22647754SJeff.Bonwick@Sun.COM 		 */
22657768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22667768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22677754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22687754SJeff.Bonwick@Sun.COM 		if (error != 0)
22697754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22707754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22717754SJeff.Bonwick@Sun.COM 	} else {
22727754SJeff.Bonwick@Sun.COM 		/*
22737754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22747754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22757754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22767754SJeff.Bonwick@Sun.COM 		 */
22777754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22789816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22797754SJeff.Bonwick@Sun.COM 
22807754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22817754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22827754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22837754SJeff.Bonwick@Sun.COM 	}
22847754SJeff.Bonwick@Sun.COM 
228510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2286789Sahrens }
2287789Sahrens 
2288789Sahrens /*
228911422SMark.Musante@Sun.COM  * split a pool if it has mirror tlvdevs
229011422SMark.Musante@Sun.COM  */
229111422SMark.Musante@Sun.COM /* ARGSUSED */
229211422SMark.Musante@Sun.COM void
229311422SMark.Musante@Sun.COM ztest_split_pool(ztest_ds_t *zd, uint64_t id)
229411422SMark.Musante@Sun.COM {
229511422SMark.Musante@Sun.COM 	ztest_shared_t *zs = ztest_shared;
229611422SMark.Musante@Sun.COM 	spa_t *spa = zs->zs_spa;
229711422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
229811422SMark.Musante@Sun.COM 	nvlist_t *tree, **child, *config, *split, **schild;
229911422SMark.Musante@Sun.COM 	uint_t c, children, schildren = 0, lastlogid = 0;
230011422SMark.Musante@Sun.COM 	int error = 0;
230111422SMark.Musante@Sun.COM 
230211422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
230311422SMark.Musante@Sun.COM 
230411422SMark.Musante@Sun.COM 	/* ensure we have a useable config; mirrors of raidz aren't supported */
230511422SMark.Musante@Sun.COM 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
230611422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
230711422SMark.Musante@Sun.COM 		return;
230811422SMark.Musante@Sun.COM 	}
230911422SMark.Musante@Sun.COM 
231011422SMark.Musante@Sun.COM 	/* clean up the old pool, if any */
231111422SMark.Musante@Sun.COM 	(void) spa_destroy("splitp");
231211422SMark.Musante@Sun.COM 
231311422SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
231411422SMark.Musante@Sun.COM 
231511422SMark.Musante@Sun.COM 	/* generate a config from the existing config */
231611864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
231711422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
231811422SMark.Musante@Sun.COM 	    &tree) == 0);
231911864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
232011864SMark.Musante@Sun.COM 
232111422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
232211422SMark.Musante@Sun.COM 	    &children) == 0);
232311422SMark.Musante@Sun.COM 
232411422SMark.Musante@Sun.COM 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
232511422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
232611422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
232711422SMark.Musante@Sun.COM 		nvlist_t **mchild;
232811422SMark.Musante@Sun.COM 		uint_t mchildren;
232911422SMark.Musante@Sun.COM 
233011422SMark.Musante@Sun.COM 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
233111422SMark.Musante@Sun.COM 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
233211422SMark.Musante@Sun.COM 			    0) == 0);
233311422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_string(schild[schildren],
233411422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
233511422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(schild[schildren],
233611422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
233711422SMark.Musante@Sun.COM 			if (lastlogid == 0)
233811422SMark.Musante@Sun.COM 				lastlogid = schildren;
233911422SMark.Musante@Sun.COM 			++schildren;
234011422SMark.Musante@Sun.COM 			continue;
234111422SMark.Musante@Sun.COM 		}
234211422SMark.Musante@Sun.COM 		lastlogid = 0;
234311422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist_array(child[c],
234411422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
234511422SMark.Musante@Sun.COM 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
234611422SMark.Musante@Sun.COM 	}
234711422SMark.Musante@Sun.COM 
234811422SMark.Musante@Sun.COM 	/* OK, create a config that can be used to split */
234911422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
235011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
235111422SMark.Musante@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
235211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
235311422SMark.Musante@Sun.COM 	    lastlogid != 0 ? lastlogid : schildren) == 0);
235411422SMark.Musante@Sun.COM 
235511422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
235611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
235711422SMark.Musante@Sun.COM 
235811422SMark.Musante@Sun.COM 	for (c = 0; c < schildren; c++)
235911422SMark.Musante@Sun.COM 		nvlist_free(schild[c]);
236011422SMark.Musante@Sun.COM 	free(schild);
236111422SMark.Musante@Sun.COM 	nvlist_free(split);
236211422SMark.Musante@Sun.COM 
236311422SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
236411422SMark.Musante@Sun.COM 
236511422SMark.Musante@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
236611422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
236711422SMark.Musante@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
236811422SMark.Musante@Sun.COM 
236911422SMark.Musante@Sun.COM 	nvlist_free(config);
237011422SMark.Musante@Sun.COM 
237111422SMark.Musante@Sun.COM 	if (error == 0) {
237211422SMark.Musante@Sun.COM 		(void) printf("successful split - results:\n");
237311422SMark.Musante@Sun.COM 		mutex_enter(&spa_namespace_lock);
237411422SMark.Musante@Sun.COM 		show_pool_stats(spa);
237511422SMark.Musante@Sun.COM 		show_pool_stats(spa_lookup("splitp"));
237611422SMark.Musante@Sun.COM 		mutex_exit(&spa_namespace_lock);
237711422SMark.Musante@Sun.COM 		++zs->zs_splits;
237811422SMark.Musante@Sun.COM 		--zs->zs_mirrors;
237911422SMark.Musante@Sun.COM 	}
238011422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
238111422SMark.Musante@Sun.COM 
238211422SMark.Musante@Sun.COM }
238311422SMark.Musante@Sun.COM 
238411422SMark.Musante@Sun.COM /*
2385789Sahrens  * Verify that we can attach and detach devices.
2386789Sahrens  */
238710922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2388789Sahrens void
238910922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2390789Sahrens {
239110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
239210922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
23937768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2394789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23951544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
23967754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
239711422SMark.Musante@Sun.COM 	uint64_t leaves;
2398789Sahrens 	uint64_t leaf, top;
23991732Sbonwick 	uint64_t ashift = ztest_get_ashift();
24008241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
24011544Seschrock 	size_t oldsize, newsize;
24021544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2403789Sahrens 	int replacing;
24047793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
24057768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
24067768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2407789Sahrens 	int error, expected_error;
2408789Sahrens 
240910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
241011422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2411789Sahrens 
24127754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2413789Sahrens 
2414789Sahrens 	/*
2415789Sahrens 	 * Decide whether to do an attach or a replace.
2416789Sahrens 	 */
2417789Sahrens 	replacing = ztest_random(2);
2418789Sahrens 
2419789Sahrens 	/*
2420789Sahrens 	 * Pick a random top-level vdev.
2421789Sahrens 	 */
242210922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2423789Sahrens 
2424789Sahrens 	/*
2425789Sahrens 	 * Pick a random leaf within it.
2426789Sahrens 	 */
242711497SMark.Musante@Sun.COM 	leaf = ztest_random(leaves);
2428789Sahrens 
2429789Sahrens 	/*
24307768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2431789Sahrens 	 */
24327768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
243311422SMark.Musante@Sun.COM 	if (zs->zs_mirrors >= 1) {
24348241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
243511422SMark.Musante@Sun.COM 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
24367768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
24378241SJeff.Bonwick@Sun.COM 	}
24388241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
24398241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
24408241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
24417768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
24428241SJeff.Bonwick@Sun.COM 	}
2443789Sahrens 
2444789Sahrens 	/*
24457768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
24467768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2447789Sahrens 	 */
24487768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
24497793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
24508241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
24518241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
24527768SJeff.Bonwick@Sun.COM 	}
24537768SJeff.Bonwick@Sun.COM 
24547768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
24559816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
24567768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
24577768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
24581544Seschrock 	pvd = oldvd->vdev_parent;
24598241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2460789Sahrens 
2461789Sahrens 	/*
24627793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
24637793SJeff.Bonwick@Sun.COM 	 */
24647793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
24657793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
24668241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
24678241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
24688241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
24698241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
247010922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
24717793SJeff.Bonwick@Sun.COM 		return;
24727793SJeff.Bonwick@Sun.COM 	}
24737793SJeff.Bonwick@Sun.COM 
24747793SJeff.Bonwick@Sun.COM 	/*
24757768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
24767768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2477789Sahrens 	 */
24787768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
24797768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
24807768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
24817768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
24827768SJeff.Bonwick@Sun.COM 	} else {
24837768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
24847768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
24857768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
24867768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
24877768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
24887768SJeff.Bonwick@Sun.COM 	}
24897768SJeff.Bonwick@Sun.COM 
24907768SJeff.Bonwick@Sun.COM 	if (newvd) {
24919816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
24927768SJeff.Bonwick@Sun.COM 	} else {
24937768SJeff.Bonwick@Sun.COM 		/*
24947768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
24957768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
24967768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
24977768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
24987768SJeff.Bonwick@Sun.COM 		 */
24997768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
25007768SJeff.Bonwick@Sun.COM 	}
2501789Sahrens 
2502789Sahrens 	/*
2503789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2504789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2505789Sahrens 	 *
25061544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2507789Sahrens 	 *
25081544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2509789Sahrens 	 */
25107768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
25117768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
25127768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
25137768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
25147768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25157768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
25167768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25177768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
25187768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
25197768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
25202174Seschrock 		expected_error = EBUSY;
25211544Seschrock 	else if (newsize < oldsize)
2522789Sahrens 		expected_error = EOVERFLOW;
25231732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
25241732Sbonwick 		expected_error = EDOM;
2525789Sahrens 	else
2526789Sahrens 		expected_error = 0;
2527789Sahrens 
25287754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2529789Sahrens 
2530789Sahrens 	/*
25311544Seschrock 	 * Build the nvlist describing newpath.
2532789Sahrens 	 */
25337754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
25347754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2535789Sahrens 
25367768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2537789Sahrens 
2538789Sahrens 	nvlist_free(root);
2539789Sahrens 
2540789Sahrens 	/*
2541789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2542789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2543789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2544789Sahrens 	 */
2545789Sahrens 	if (expected_error == ENOTSUP &&
2546789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2547789Sahrens 		expected_error = error;
2548789Sahrens 
2549797Sbonwick 	/*
2550797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2551797Sbonwick 	 */
25527046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2553797Sbonwick 		expected_error = error;
2554797Sbonwick 
25557046Sahrens 	/* XXX workaround 6690467 */
25567046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
25577046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
25587046Sahrens 		    "returned %d, expected %d",
25597046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
25607046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2561789Sahrens 	}
2562789Sahrens 
256310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2564789Sahrens }
2565789Sahrens 
2566789Sahrens /*
25679816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
25689816SGeorge.Wilson@Sun.COM  */
25699816SGeorge.Wilson@Sun.COM vdev_t *
25709816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
25719816SGeorge.Wilson@Sun.COM {
25729816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25739816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
25749816SGeorge.Wilson@Sun.COM 	size_t fsize;
25759816SGeorge.Wilson@Sun.COM 	int fd;
25769816SGeorge.Wilson@Sun.COM 
25779816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25789816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25799816SGeorge.Wilson@Sun.COM 
25809816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
25819816SGeorge.Wilson@Sun.COM 		return (vd);
25829816SGeorge.Wilson@Sun.COM 
25839816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
25849816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
25859816SGeorge.Wilson@Sun.COM 
25869816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
25879816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
25889816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
25899816SGeorge.Wilson@Sun.COM 	}
25909816SGeorge.Wilson@Sun.COM 	(void) close(fd);
25919816SGeorge.Wilson@Sun.COM 	return (NULL);
25929816SGeorge.Wilson@Sun.COM }
25939816SGeorge.Wilson@Sun.COM 
25949816SGeorge.Wilson@Sun.COM /*
25959816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
25969816SGeorge.Wilson@Sun.COM  */
25979816SGeorge.Wilson@Sun.COM /* ARGSUSED */
25989816SGeorge.Wilson@Sun.COM vdev_t *
25999816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
26009816SGeorge.Wilson@Sun.COM {
26019816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
26029816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
26039816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
260410685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
260510850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
260610850SGeorge.Wilson@Sun.COM 	int error;
26079816SGeorge.Wilson@Sun.COM 
26089816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
26099816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
26109816SGeorge.Wilson@Sun.COM 
26119816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
26129816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
261310850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
26149816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26159816SGeorge.Wilson@Sun.COM 
26169816SGeorge.Wilson@Sun.COM 	/*
261710850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
261810850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
261910850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
262010850SGeorge.Wilson@Sun.COM 	 */
262110850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
262210850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
262310850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
262410850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
262510850SGeorge.Wilson@Sun.COM 		}
262610850SGeorge.Wilson@Sun.COM 		return (vd);
262710850SGeorge.Wilson@Sun.COM 	}
262810850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
262910850SGeorge.Wilson@Sun.COM 
263010850SGeorge.Wilson@Sun.COM 	/*
26319816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
26329816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
26339816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
26349816SGeorge.Wilson@Sun.COM 	 * trying to online it.
26359816SGeorge.Wilson@Sun.COM 	 */
263610685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
263710685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
263810685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
263910685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
264010922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
264110922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
264210685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
264310685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
264410685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
26459816SGeorge.Wilson@Sun.COM 		}
26469816SGeorge.Wilson@Sun.COM 		return (vd);
26479816SGeorge.Wilson@Sun.COM 	}
26489816SGeorge.Wilson@Sun.COM 	return (NULL);
26499816SGeorge.Wilson@Sun.COM }
26509816SGeorge.Wilson@Sun.COM 
26519816SGeorge.Wilson@Sun.COM /*
26529816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
26539816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
26549816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
26559816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
26569816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
26579816SGeorge.Wilson@Sun.COM  */
26589816SGeorge.Wilson@Sun.COM vdev_t *
26599816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
26609816SGeorge.Wilson@Sun.COM {
26619816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26629816SGeorge.Wilson@Sun.COM 		if (func == NULL)
26639816SGeorge.Wilson@Sun.COM 			return (vd);
26649816SGeorge.Wilson@Sun.COM 		else
26659816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
26669816SGeorge.Wilson@Sun.COM 	}
26679816SGeorge.Wilson@Sun.COM 
26689816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
26699816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
26709816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
26719816SGeorge.Wilson@Sun.COM 			return (cvd);
26729816SGeorge.Wilson@Sun.COM 	}
26739816SGeorge.Wilson@Sun.COM 	return (NULL);
26749816SGeorge.Wilson@Sun.COM }
26759816SGeorge.Wilson@Sun.COM 
26769816SGeorge.Wilson@Sun.COM /*
2677789Sahrens  * Verify that dynamic LUN growth works as expected.
2678789Sahrens  */
267910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2680789Sahrens void
268110922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2682789Sahrens {
268310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
268410922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
268510922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
268610922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
268710922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
26889816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
268910922SJeff.Bonwick@Sun.COM 	uint64_t top;
269010922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
269110922SJeff.Bonwick@Sun.COM 
269210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
26939816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26949816SGeorge.Wilson@Sun.COM 
269510922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
269610922SJeff.Bonwick@Sun.COM 
269710922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
269810922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
269910922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
270010922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
270110922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
27029816SGeorge.Wilson@Sun.COM 
27039816SGeorge.Wilson@Sun.COM 	/*
27049816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
27059816SGeorge.Wilson@Sun.COM 	 * our top-level device.
27069816SGeorge.Wilson@Sun.COM 	 */
27079816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
27089816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
27099816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
27109816SGeorge.Wilson@Sun.COM 
27119816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
27129816SGeorge.Wilson@Sun.COM 
27139816SGeorge.Wilson@Sun.COM 	/*
271410685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
271510685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
27169816SGeorge.Wilson@Sun.COM 	 */
271710685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
271810685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
27199816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
272010922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27219816SGeorge.Wilson@Sun.COM 		return;
27229816SGeorge.Wilson@Sun.COM 	}
27239816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
27249816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
27259816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
27269816SGeorge.Wilson@Sun.COM 
272710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
272810922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
27299816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
27309816SGeorge.Wilson@Sun.COM 	}
27319816SGeorge.Wilson@Sun.COM 
2732789Sahrens 	/*
27339816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
27349816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
27359816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
27369816SGeorge.Wilson@Sun.COM 	 */
27379816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
27389816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
27399816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
27409816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
27419816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
274210685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
27439816SGeorge.Wilson@Sun.COM 		}
274410922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
274510922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27469816SGeorge.Wilson@Sun.COM 		return;
27479816SGeorge.Wilson@Sun.COM 	}
27489816SGeorge.Wilson@Sun.COM 
274910922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
27509816SGeorge.Wilson@Sun.COM 
27519816SGeorge.Wilson@Sun.COM 	/*
27529816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
27539816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
27549816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2755789Sahrens 	 */
275610922SJeff.Bonwick@Sun.COM 	for (;;) {
275710922SJeff.Bonwick@Sun.COM 		boolean_t done;
275810922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
275910922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
276010922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
276110922SJeff.Bonwick@Sun.COM 		if (done)
276210922SJeff.Bonwick@Sun.COM 			break;
276310922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
276410922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
276510922SJeff.Bonwick@Sun.COM 	}
27669816SGeorge.Wilson@Sun.COM 
27679816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
276810922SJeff.Bonwick@Sun.COM 
276910922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
277010922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
277110922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
277210922SJeff.Bonwick@Sun.COM 
277310922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
277410922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
277510922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
277610922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
277710922SJeff.Bonwick@Sun.COM 		}
277810922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
277910922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
278010922SJeff.Bonwick@Sun.COM 		return;
278110922SJeff.Bonwick@Sun.COM 	}
278210922SJeff.Bonwick@Sun.COM 
278310922SJeff.Bonwick@Sun.COM 	/*
278410922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
278510922SJeff.Bonwick@Sun.COM 	 */
278610922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
278710922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
278810922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
27899816SGeorge.Wilson@Sun.COM 
27909816SGeorge.Wilson@Sun.COM 	/*
27919816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
27929816SGeorge.Wilson@Sun.COM 	 */
279310922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
279410922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
279510922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
279610922SJeff.Bonwick@Sun.COM 
279710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
27989816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
27999816SGeorge.Wilson@Sun.COM 
280010922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
280110922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
28029816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
28039816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2804789Sahrens 	}
280510922SJeff.Bonwick@Sun.COM 
28069816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
280710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
280810922SJeff.Bonwick@Sun.COM }
280910922SJeff.Bonwick@Sun.COM 
281010922SJeff.Bonwick@Sun.COM /*
281110922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
281210922SJeff.Bonwick@Sun.COM  */
281310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
281410922SJeff.Bonwick@Sun.COM static void
281510922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
281610922SJeff.Bonwick@Sun.COM {
281710922SJeff.Bonwick@Sun.COM 	/*
281810922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
281910922SJeff.Bonwick@Sun.COM 	 */
282010922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
282110922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2822789Sahrens }
2823789Sahrens 
282412294SMark.Musante@Sun.COM static int
282512294SMark.Musante@Sun.COM ztest_dataset_create(char *dsname)
282612294SMark.Musante@Sun.COM {
282712294SMark.Musante@Sun.COM 	uint64_t zilset = ztest_random(100);
282812294SMark.Musante@Sun.COM 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
282912294SMark.Musante@Sun.COM 	    ztest_objset_create_cb, NULL);
283012294SMark.Musante@Sun.COM 
283112294SMark.Musante@Sun.COM 	if (err || zilset < 80)
283212294SMark.Musante@Sun.COM 		return (err);
283312294SMark.Musante@Sun.COM 
283412294SMark.Musante@Sun.COM 	(void) printf("Setting dataset %s to sync always\n", dsname);
283512294SMark.Musante@Sun.COM 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
283612294SMark.Musante@Sun.COM 	    ZFS_SYNC_ALWAYS, B_FALSE));
283712294SMark.Musante@Sun.COM }
283812294SMark.Musante@Sun.COM 
2839789Sahrens /* ARGSUSED */
284010922SJeff.Bonwick@Sun.COM static int
284111209SMatthew.Ahrens@Sun.COM ztest_objset_destroy_cb(const char *name, void *arg)
2842789Sahrens {
2843789Sahrens 	objset_t *os;
284410922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2845789Sahrens 	int error;
2846789Sahrens 
2847789Sahrens 	/*
2848789Sahrens 	 * Verify that the dataset contains a directory object.
2849789Sahrens 	 */
285010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
285110922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
28521731Sbonwick 	if (error != ENOENT) {
28531731Sbonwick 		/* We could have crashed in the middle of destroying it */
28541731Sbonwick 		ASSERT3U(error, ==, 0);
285510922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
285610922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
28571731Sbonwick 	}
285810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2859789Sahrens 
2860789Sahrens 	/*
2861789Sahrens 	 * Destroy the dataset.
2862789Sahrens 	 */
286310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
28642199Sahrens 	return (0);
2865789Sahrens }
2866789Sahrens 
286710922SJeff.Bonwick@Sun.COM static boolean_t
286810922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2869789Sahrens {
287010922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
287110922SJeff.Bonwick@Sun.COM 	int error;
287210922SJeff.Bonwick@Sun.COM 
287310922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
287410922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
287510922SJeff.Bonwick@Sun.COM 
287610922SJeff.Bonwick@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
287710922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
287810922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
287910922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
288010922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
288110922SJeff.Bonwick@Sun.COM 	}
288210922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
288310922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
288410922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2885789Sahrens }
2886789Sahrens 
288710922SJeff.Bonwick@Sun.COM static boolean_t
288810922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
288910922SJeff.Bonwick@Sun.COM {
289010922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
289110922SJeff.Bonwick@Sun.COM 	int error;
289210922SJeff.Bonwick@Sun.COM 
289310922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
289410922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
289510922SJeff.Bonwick@Sun.COM 
289610922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
289710922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
289810922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
289910922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
290010922SJeff.Bonwick@Sun.COM }
290110922SJeff.Bonwick@Sun.COM 
290210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2903789Sahrens void
290410922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2905789Sahrens {
290610922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
290710922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
290810922SJeff.Bonwick@Sun.COM 	int iters;
2909789Sahrens 	int error;
29106689Smaybee 	objset_t *os, *os2;
291110922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2912789Sahrens 	zilog_t *zilog;
291310922SJeff.Bonwick@Sun.COM 
291410922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
291510922SJeff.Bonwick@Sun.COM 
291610922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
291710922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2918789Sahrens 
2919789Sahrens 	/*
2920789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2921789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
292210922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2923789Sahrens 	 */
2924789Sahrens 	if (ztest_random(2) == 0 &&
292510298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
292610922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
292710922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
292810922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
292910298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2930789Sahrens 	}
2931789Sahrens 
2932789Sahrens 	/*
2933789Sahrens 	 * There may be an old instance of the dataset we're about to
2934789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2935789Sahrens 	 * and all of its snapshots.
2936789Sahrens 	 */
293710922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
29382417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2939789Sahrens 
2940789Sahrens 	/*
2941789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2942789Sahrens 	 */
294310922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2944789Sahrens 
2945789Sahrens 	/*
2946789Sahrens 	 * Verify that we can create a new dataset.
2947789Sahrens 	 */
294812294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
2949789Sahrens 	if (error) {
2950789Sahrens 		if (error == ENOSPC) {
295110922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
295210922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2953789Sahrens 			return;
2954789Sahrens 		}
2955789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2956789Sahrens 	}
2957789Sahrens 
295810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
295910922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
296010922SJeff.Bonwick@Sun.COM 
296110922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2962789Sahrens 
2963789Sahrens 	/*
2964789Sahrens 	 * Open the intent log for it.
2965789Sahrens 	 */
296610922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2967789Sahrens 
2968789Sahrens 	/*
296910922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
297010922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2971789Sahrens 	 */
297210922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
297310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
297410922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
297510922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
297610922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2977789Sahrens 	}
2978789Sahrens 
2979789Sahrens 	/*
2980789Sahrens 	 * Verify that we cannot create an existing dataset.
2981789Sahrens 	 */
298210922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
298310922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2984789Sahrens 
2985789Sahrens 	/*
298610298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2987789Sahrens 	 */
298810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
298910298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
299010298SMatthew.Ahrens@Sun.COM 
299110298SMatthew.Ahrens@Sun.COM 	/*
299210922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
299310298SMatthew.Ahrens@Sun.COM 	 */
299410922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
299510922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
2996789Sahrens 
2997789Sahrens 	zil_close(zilog);
299810298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
299910922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
300010922SJeff.Bonwick@Sun.COM 
300110922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3002789Sahrens }
3003789Sahrens 
3004789Sahrens /*
3005789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3006789Sahrens  */
3007789Sahrens void
300810922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3009789Sahrens {
301010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
301110922SJeff.Bonwick@Sun.COM 
301210922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
301310922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
301410922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
301510922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3016789Sahrens }
3017789Sahrens 
3018789Sahrens /*
30199691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
30209691SGeorge.Wilson@Sun.COM  */
30219691SGeorge.Wilson@Sun.COM void
302210922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
30239691SGeorge.Wilson@Sun.COM {
302410922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
302510922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
302610922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
302710922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
302810922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
30299691SGeorge.Wilson@Sun.COM 	int error;
30309691SGeorge.Wilson@Sun.COM 
303110922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
303210922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
303310922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
303410922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
303510922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30369691SGeorge.Wilson@Sun.COM 
303710242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
30389691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30399691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
304010242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
30419691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30429691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
304310242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
30449691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30459691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
304610242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
30479691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30489691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
304910242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
30509691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30519691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
30529691SGeorge.Wilson@Sun.COM }
30539691SGeorge.Wilson@Sun.COM 
30549691SGeorge.Wilson@Sun.COM /*
30558779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
30568779SMark.Musante@Sun.COM  */
30578779SMark.Musante@Sun.COM void
305810922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
30598779SMark.Musante@Sun.COM {
306010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
30618779SMark.Musante@Sun.COM 	objset_t *clone;
30628779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
306310922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
306410922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
306510922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
306610922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
306710922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
306810922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
306910922SJeff.Bonwick@Sun.COM 	int error;
307010922SJeff.Bonwick@Sun.COM 
307110922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
307210922SJeff.Bonwick@Sun.COM 
307310922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
307410922SJeff.Bonwick@Sun.COM 
307510922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
307610922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
307710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
307810922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
307910922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30808837SMark.Musante@Sun.COM 
30819355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
308210922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30839229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30849229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
308510922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30869229SGeorge.Wilson@Sun.COM 			goto out;
30879229SGeorge.Wilson@Sun.COM 		}
30889229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
30899229SGeorge.Wilson@Sun.COM 	}
30908779SMark.Musante@Sun.COM 
309110298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
30928779SMark.Musante@Sun.COM 	if (error)
30938779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
30948779SMark.Musante@Sun.COM 
309510272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
309610298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
30979229SGeorge.Wilson@Sun.COM 	if (error) {
30989229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
309910922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31009229SGeorge.Wilson@Sun.COM 			goto out;
31019229SGeorge.Wilson@Sun.COM 		}
31028779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
31039229SGeorge.Wilson@Sun.COM 	}
31048779SMark.Musante@Sun.COM 
31058779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
310610922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
31079229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31089229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
310910922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31109229SGeorge.Wilson@Sun.COM 			goto out;
31119229SGeorge.Wilson@Sun.COM 		}
31129229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
31139229SGeorge.Wilson@Sun.COM 	}
31148779SMark.Musante@Sun.COM 
31158779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
311610922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
31179229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31189229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
311910922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31209229SGeorge.Wilson@Sun.COM 			goto out;
31219229SGeorge.Wilson@Sun.COM 		}
31229229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31239229SGeorge.Wilson@Sun.COM 	}
31248779SMark.Musante@Sun.COM 
312510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
31268779SMark.Musante@Sun.COM 	if (error)
31278779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31288779SMark.Musante@Sun.COM 
312910272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
313010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31319229SGeorge.Wilson@Sun.COM 	if (error) {
31329229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
313310922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31349229SGeorge.Wilson@Sun.COM 			goto out;
31359229SGeorge.Wilson@Sun.COM 		}
31368779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
31379229SGeorge.Wilson@Sun.COM 	}
31388779SMark.Musante@Sun.COM 
313910298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
31408779SMark.Musante@Sun.COM 	if (error)
31418779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
314210588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
31438779SMark.Musante@Sun.COM 	if (error != EBUSY)
31448779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
31458779SMark.Musante@Sun.COM 		    error);
31468779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
31478779SMark.Musante@Sun.COM 
31489229SGeorge.Wilson@Sun.COM out:
314910922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
315010922SJeff.Bonwick@Sun.COM 
315110922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
31528779SMark.Musante@Sun.COM }
31538779SMark.Musante@Sun.COM 
31548779SMark.Musante@Sun.COM /*
3155789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3156789Sahrens  */
3157789Sahrens void
315810922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3159789Sahrens {
316010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
316110922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
316210922SJeff.Bonwick@Sun.COM 
316310922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
316410922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3165789Sahrens 
3166789Sahrens 	/*
316710922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
316810922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3169789Sahrens 	 */
317010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
317110922SJeff.Bonwick@Sun.COM 		return;
317210922SJeff.Bonwick@Sun.COM 
317310922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
317410922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
317510922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3176789Sahrens }
3177789Sahrens 
3178789Sahrens /*
3179789Sahrens  * Verify that dmu_{read,write} work as expected.
3180789Sahrens  */
3181789Sahrens void
318210922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3183789Sahrens {
318410922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
318510922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3186789Sahrens 	dmu_tx_t *tx;
3187789Sahrens 	int i, freeit, error;
3188789Sahrens 	uint64_t n, s, txg;
3189789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
319010922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
319110922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3192789Sahrens 	uint64_t regions = 997;
3193789Sahrens 	uint64_t stride = 123456789ULL;
3194789Sahrens 	uint64_t width = 40;
3195789Sahrens 	int free_percent = 5;
3196789Sahrens 
3197789Sahrens 	/*
3198789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3199789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3200789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3201789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3202789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3203789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3204789Sahrens 	 *
3205789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3206789Sahrens 	 *	bigobj, at the head of the nth chunk
3207789Sahrens 	 *	bigobj, at the tail of the nth chunk
3208789Sahrens 	 *
3209789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3210789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3211789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3212789Sahrens 	 *
3213789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3214789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3215789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3216789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3217789Sahrens 	 * tracking which parts of an object are allocated and free,
3218789Sahrens 	 * and that the contents of the allocated blocks are correct.
3219789Sahrens 	 */
3220789Sahrens 
3221789Sahrens 	/*
3222789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3223789Sahrens 	 */
322410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
322510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
322610922SJeff.Bonwick@Sun.COM 
322710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
322810922SJeff.Bonwick@Sun.COM 		return;
322910922SJeff.Bonwick@Sun.COM 
323010922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
323110922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
323210922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
323310922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3234789Sahrens 
3235789Sahrens 	/*
3236789Sahrens 	 * Prefetch a random chunk of the big object.
3237789Sahrens 	 * Our aim here is to get some async reads in flight
3238789Sahrens 	 * for blocks that we may free below; the DMU should
3239789Sahrens 	 * handle this race correctly.
3240789Sahrens 	 */
3241789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3242789Sahrens 	s = 1 + ztest_random(2 * width - 1);
324310922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3244789Sahrens 
3245789Sahrens 	/*
3246789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3247789Sahrens 	 */
3248789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3249789Sahrens 	s = 1 + ztest_random(width - 1);
3250789Sahrens 
3251789Sahrens 	packoff = n * sizeof (bufwad_t);
3252789Sahrens 	packsize = s * sizeof (bufwad_t);
3253789Sahrens 
325410922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
325510922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3256789Sahrens 
3257789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3258789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3259789Sahrens 
3260789Sahrens 	/*
3261789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3262789Sahrens 	 * overwriting it.
3263789Sahrens 	 */
3264789Sahrens 	freeit = (ztest_random(100) < free_percent);
3265789Sahrens 
3266789Sahrens 	/*
3267789Sahrens 	 * Read the current contents of our objects.
3268789Sahrens 	 */
326910922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
32709512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32711544Seschrock 	ASSERT3U(error, ==, 0);
327210922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
32739512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32741544Seschrock 	ASSERT3U(error, ==, 0);
3275789Sahrens 
3276789Sahrens 	/*
3277789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3278789Sahrens 	 */
3279789Sahrens 	tx = dmu_tx_create(os);
3280789Sahrens 
328110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3282789Sahrens 
3283789Sahrens 	if (freeit)
328410922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3285789Sahrens 	else
328610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
328710922SJeff.Bonwick@Sun.COM 
328810922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
328910922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3290789Sahrens 		umem_free(packbuf, packsize);
3291789Sahrens 		umem_free(bigbuf, bigsize);
3292789Sahrens 		return;
3293789Sahrens 	}
3294789Sahrens 
329510922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
329610922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
329710922SJeff.Bonwick@Sun.COM 
329810922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
329910922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3300789Sahrens 
3301789Sahrens 	/*
3302789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3303789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3304789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3305789Sahrens 	 * with the new values we want to write out.
3306789Sahrens 	 */
3307789Sahrens 	for (i = 0; i < s; i++) {
3308789Sahrens 		/* LINTED */
3309789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3310789Sahrens 		/* LINTED */
331110922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3312789Sahrens 		/* LINTED */
331310922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3314789Sahrens 
3315789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3316789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3317789Sahrens 
3318789Sahrens 		if (pack->bw_txg > txg)
3319789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3320789Sahrens 			    pack->bw_txg, txg);
3321789Sahrens 
3322789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3323789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3324789Sahrens 			    pack->bw_index, n, i);
3325789Sahrens 
3326789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3327789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3328789Sahrens 
3329789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3330789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3331789Sahrens 
3332789Sahrens 		if (freeit) {
3333789Sahrens 			bzero(pack, sizeof (bufwad_t));
3334789Sahrens 		} else {
3335789Sahrens 			pack->bw_index = n + i;
3336789Sahrens 			pack->bw_txg = txg;
3337789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3338789Sahrens 		}
3339789Sahrens 		*bigH = *pack;
3340789Sahrens 		*bigT = *pack;
3341789Sahrens 	}
3342789Sahrens 
3343789Sahrens 	/*
3344789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3345789Sahrens 	 * Now write them out.
3346789Sahrens 	 */
334710922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3348789Sahrens 
3349789Sahrens 	if (freeit) {
335010922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3351789Sahrens 			(void) printf("freeing offset %llx size %llx"
3352789Sahrens 			    " txg %llx\n",
3353789Sahrens 			    (u_longlong_t)bigoff,
3354789Sahrens 			    (u_longlong_t)bigsize,
3355789Sahrens 			    (u_longlong_t)txg);
3356789Sahrens 		}
335710922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3358789Sahrens 	} else {
335910922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3360789Sahrens 			(void) printf("writing offset %llx size %llx"
3361789Sahrens 			    " txg %llx\n",
3362789Sahrens 			    (u_longlong_t)bigoff,
3363789Sahrens 			    (u_longlong_t)bigsize,
3364789Sahrens 			    (u_longlong_t)txg);
3365789Sahrens 		}
336610922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3367789Sahrens 	}
3368789Sahrens 
3369789Sahrens 	dmu_tx_commit(tx);
3370789Sahrens 
3371789Sahrens 	/*
3372789Sahrens 	 * Sanity check the stuff we just wrote.
3373789Sahrens 	 */
3374789Sahrens 	{
3375789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3376789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3377789Sahrens 
337810922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
33799512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
338010922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
33819512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3382789Sahrens 
3383789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3384789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3385789Sahrens 
3386789Sahrens 		umem_free(packcheck, packsize);
3387789Sahrens 		umem_free(bigcheck, bigsize);
3388789Sahrens 	}
3389789Sahrens 
3390789Sahrens 	umem_free(packbuf, packsize);
3391789Sahrens 	umem_free(bigbuf, bigsize);
3392789Sahrens }
3393789Sahrens 
3394789Sahrens void
33959412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
339610922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
33979412SAleksandr.Guzovskiy@Sun.COM {
33989412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
33999412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
34009412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
34019412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
34029412SAleksandr.Guzovskiy@Sun.COM 
34039412SAleksandr.Guzovskiy@Sun.COM 	/*
34049412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
34059412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
34069412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
34079412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
34089412SAleksandr.Guzovskiy@Sun.COM 	 */
34099412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
34109412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
34119412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
34129412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341310922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34149412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341510922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34169412SAleksandr.Guzovskiy@Sun.COM 
34179412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
34189412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
34199412SAleksandr.Guzovskiy@Sun.COM 
34209412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
34219412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
34229412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
34239412SAleksandr.Guzovskiy@Sun.COM 
34249412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
34259412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
34269412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
34279412SAleksandr.Guzovskiy@Sun.COM 
34289412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
34299412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
34309412SAleksandr.Guzovskiy@Sun.COM 
34319412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
34329412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
34339412SAleksandr.Guzovskiy@Sun.COM 
34349412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
34359412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
34369412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
34379412SAleksandr.Guzovskiy@Sun.COM 
34389412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
34399412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
34409412SAleksandr.Guzovskiy@Sun.COM 	}
34419412SAleksandr.Guzovskiy@Sun.COM }
34429412SAleksandr.Guzovskiy@Sun.COM 
34439412SAleksandr.Guzovskiy@Sun.COM void
344410922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
34459412SAleksandr.Guzovskiy@Sun.COM {
344610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
344710922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
34489412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
34499412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34509412SAleksandr.Guzovskiy@Sun.COM 	int error;
34519412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
34529412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
345310922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
345410922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
345510922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
34569412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
34579412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
34589412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
34599412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
34609412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
346110922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
34629412SAleksandr.Guzovskiy@Sun.COM 
34639412SAleksandr.Guzovskiy@Sun.COM 	/*
34649412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
34659412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
34669412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
34679412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
34689412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
34699412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
34709412SAleksandr.Guzovskiy@Sun.COM 	 *
34719412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
34729412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
34739412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
34749412SAleksandr.Guzovskiy@Sun.COM 	 *
34759412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
34769412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
34779412SAleksandr.Guzovskiy@Sun.COM 	 */
34789412SAleksandr.Guzovskiy@Sun.COM 
34799412SAleksandr.Guzovskiy@Sun.COM 	/*
34809412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
34819412SAleksandr.Guzovskiy@Sun.COM 	 */
348210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
348310922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
348410922SJeff.Bonwick@Sun.COM 
348510922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
348610922SJeff.Bonwick@Sun.COM 		return;
348710922SJeff.Bonwick@Sun.COM 
348810922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
348910922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
349010922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
349110922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
349210922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
349310922SJeff.Bonwick@Sun.COM 
349410922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
349510922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
349610922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
349710922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
34989412SAleksandr.Guzovskiy@Sun.COM 
34999412SAleksandr.Guzovskiy@Sun.COM 	/*
35009412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
35019412SAleksandr.Guzovskiy@Sun.COM 	 */
35029412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
35039412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
35049412SAleksandr.Guzovskiy@Sun.COM 
35059412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
35069412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
35079412SAleksandr.Guzovskiy@Sun.COM 
350810922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
350910922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
35109412SAleksandr.Guzovskiy@Sun.COM 
35119412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
35129412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
35139412SAleksandr.Guzovskiy@Sun.COM 
351410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
35159412SAleksandr.Guzovskiy@Sun.COM 
35169412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
35179412SAleksandr.Guzovskiy@Sun.COM 
35189412SAleksandr.Guzovskiy@Sun.COM 	/*
35199412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
35209412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
35219412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
35229412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
35239412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
35249412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
35259412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
35269412SAleksandr.Guzovskiy@Sun.COM 	 */
35279412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
35289412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
35299412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
35309412SAleksandr.Guzovskiy@Sun.COM 
35319412SAleksandr.Guzovskiy@Sun.COM 		/*
35329412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
35339412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
35349412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
35359412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
35369412SAleksandr.Guzovskiy@Sun.COM 		 */
35379412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
35389412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35399412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
354010922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
35419412SAleksandr.Guzovskiy@Sun.COM 			} else {
35429412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
354310922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35449412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
354510922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35469412SAleksandr.Guzovskiy@Sun.COM 			}
35479412SAleksandr.Guzovskiy@Sun.COM 		}
35489412SAleksandr.Guzovskiy@Sun.COM 
35499412SAleksandr.Guzovskiy@Sun.COM 		/*
35509412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
35519412SAleksandr.Guzovskiy@Sun.COM 		 */
35529412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
35539412SAleksandr.Guzovskiy@Sun.COM 
355410922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
355510922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
355610922SJeff.Bonwick@Sun.COM 
355710922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
355810922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
35599412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
35609412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
35619412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
35629412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
35639412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
35649412SAleksandr.Guzovskiy@Sun.COM 				} else {
35659412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35669412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
35679412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35689412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
35699412SAleksandr.Guzovskiy@Sun.COM 				}
35709412SAleksandr.Guzovskiy@Sun.COM 			}
35719412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35729412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
35739412SAleksandr.Guzovskiy@Sun.COM 			return;
35749412SAleksandr.Guzovskiy@Sun.COM 		}
35759412SAleksandr.Guzovskiy@Sun.COM 
35769412SAleksandr.Guzovskiy@Sun.COM 		/*
35779412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
35789412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
35799412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
35809412SAleksandr.Guzovskiy@Sun.COM 		 */
35819412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
358210922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
35839512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
35849412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
358510922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
35869512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
35879412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
35889412SAleksandr.Guzovskiy@Sun.COM 		}
35899412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
359010922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
35919412SAleksandr.Guzovskiy@Sun.COM 
35929412SAleksandr.Guzovskiy@Sun.COM 		/*
35939412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
35949412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
35959412SAleksandr.Guzovskiy@Sun.COM 		 */
359610922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
359710922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
35989412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
35999412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
36009412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
36019412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
36029412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
36039412SAleksandr.Guzovskiy@Sun.COM 		}
360410922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
36059412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
36069412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36079412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
360810922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
36099412SAleksandr.Guzovskiy@Sun.COM 			} else {
36109412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
36119412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
361210922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36139412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
361410922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
36159412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
361610922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36179412SAleksandr.Guzovskiy@Sun.COM 			}
36189412SAleksandr.Guzovskiy@Sun.COM 
36199412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
362010922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
362112285SJeff.Bonwick@Sun.COM 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
36229412SAleksandr.Guzovskiy@Sun.COM 			}
36239412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36249412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36259412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
36269412SAleksandr.Guzovskiy@Sun.COM 			} else {
36279412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36289412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
36299412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
363010922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
36319412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
36329412SAleksandr.Guzovskiy@Sun.COM 			}
36339412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
36349412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
36359412SAleksandr.Guzovskiy@Sun.COM 			}
36369412SAleksandr.Guzovskiy@Sun.COM 		}
36379412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
36389412SAleksandr.Guzovskiy@Sun.COM 
36399412SAleksandr.Guzovskiy@Sun.COM 		/*
36409412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
36419412SAleksandr.Guzovskiy@Sun.COM 		 */
36429412SAleksandr.Guzovskiy@Sun.COM 		{
36439412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
36449412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
36459412SAleksandr.Guzovskiy@Sun.COM 
364610922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
36479512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
364810922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
36499512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
36509412SAleksandr.Guzovskiy@Sun.COM 
36519412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
36529412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
36539412SAleksandr.Guzovskiy@Sun.COM 
36549412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
36559412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
36569412SAleksandr.Guzovskiy@Sun.COM 		}
36579412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
36589412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
36599412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
36609412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
36619412SAleksandr.Guzovskiy@Sun.COM 		}
36629412SAleksandr.Guzovskiy@Sun.COM 	}
36639412SAleksandr.Guzovskiy@Sun.COM 
36649412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
36659412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
36669412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
36679412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
36689412SAleksandr.Guzovskiy@Sun.COM }
36699412SAleksandr.Guzovskiy@Sun.COM 
367010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
36719412SAleksandr.Guzovskiy@Sun.COM void
367210922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
36733711Smaybee {
367410922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
367510922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
367610922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
36773711Smaybee 
36783711Smaybee 	/*
367910922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
368010922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
368110922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
36823711Smaybee 	 */
368310922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
368410922SJeff.Bonwick@Sun.COM 
368510922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
368610922SJeff.Bonwick@Sun.COM 		return;
368710922SJeff.Bonwick@Sun.COM 
368810922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
368910922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
36903711Smaybee }
36913711Smaybee 
36923711Smaybee void
369310922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3694789Sahrens {
369510922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
369610922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
369710922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
369810922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
369910922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
370010922SJeff.Bonwick@Sun.COM 	void *data;
370110922SJeff.Bonwick@Sun.COM 
370210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
370310922SJeff.Bonwick@Sun.COM 
370410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
37055530Sbonwick 		return;
370610922SJeff.Bonwick@Sun.COM 
370710922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
370810922SJeff.Bonwick@Sun.COM 		return;
370910922SJeff.Bonwick@Sun.COM 
371010922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
371110922SJeff.Bonwick@Sun.COM 
371210922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
371310922SJeff.Bonwick@Sun.COM 
371410922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
371510922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
371610922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
371710922SJeff.Bonwick@Sun.COM 		    data) != 0)
371810922SJeff.Bonwick@Sun.COM 			break;
371910922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
372010922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
37215530Sbonwick 	}
372210922SJeff.Bonwick@Sun.COM 
372310922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3724789Sahrens }
3725789Sahrens 
3726789Sahrens /*
3727789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3728789Sahrens  */
3729789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3730789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3731789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3732789Sahrens 
3733789Sahrens void
373410922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3735789Sahrens {
373610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
373710922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3738789Sahrens 	uint64_t object;
3739789Sahrens 	uint64_t txg, last_txg;
3740789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3741789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3742789Sahrens 	int i, ints;
3743789Sahrens 	dmu_tx_t *tx;
3744789Sahrens 	char propname[100], txgname[100];
3745789Sahrens 	int error;
3746789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3747789Sahrens 
374810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
374910922SJeff.Bonwick@Sun.COM 
375010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
375110922SJeff.Bonwick@Sun.COM 		return;
375210922SJeff.Bonwick@Sun.COM 
375310922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3754789Sahrens 
3755789Sahrens 	/*
375610922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
375710922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3758789Sahrens 	 */
375910922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
376010922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
376110922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
376210922SJeff.Bonwick@Sun.COM 	if (txg == 0)
376310922SJeff.Bonwick@Sun.COM 		return;
376410922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
376510922SJeff.Bonwick@Sun.COM 		value[i] = i;
376610922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
376710922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3768789Sahrens 	}
376910922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
377010922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
377110922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
377210922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
377310922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
377410922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
377510922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
377610922SJeff.Bonwick@Sun.COM 	}
377710922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
377810922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
377910922SJeff.Bonwick@Sun.COM 	}
378010922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
378110922SJeff.Bonwick@Sun.COM 
378210922SJeff.Bonwick@Sun.COM 	/*
378310922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
378410922SJeff.Bonwick@Sun.COM 	 */
3785789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3786789Sahrens 
37875530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37885530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37895530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37905530Sbonwick 	bzero(value, sizeof (value));
37915530Sbonwick 	last_txg = 0;
37925530Sbonwick 
37935530Sbonwick 	/*
37945530Sbonwick 	 * If these zap entries already exist, validate their contents.
37955530Sbonwick 	 */
37965530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
37975530Sbonwick 	if (error == 0) {
37985530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
37995530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
38005530Sbonwick 
38015530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
38025530Sbonwick 		    zl_ints, &last_txg) == 0);
38035530Sbonwick 
38045530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
38055530Sbonwick 		    &zl_ints) == 0);
38065530Sbonwick 
38075530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
38085530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
38095530Sbonwick 
38105530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
38115530Sbonwick 		    zl_ints, value) == 0);
38125530Sbonwick 
38135530Sbonwick 		for (i = 0; i < ints; i++) {
38145530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3815789Sahrens 		}
38165530Sbonwick 	} else {
38175530Sbonwick 		ASSERT3U(error, ==, ENOENT);
38185530Sbonwick 	}
38195530Sbonwick 
38205530Sbonwick 	/*
38215530Sbonwick 	 * Atomically update two entries in our zap object.
38225530Sbonwick 	 * The first is named txg_%llu, and contains the txg
38235530Sbonwick 	 * in which the property was last updated.  The second
38245530Sbonwick 	 * is named prop_%llu, and the nth element of its value
38255530Sbonwick 	 * should be txg + object + n.
38265530Sbonwick 	 */
38275530Sbonwick 	tx = dmu_tx_create(os);
382810922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
382910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
383010922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38315530Sbonwick 		return;
38325530Sbonwick 
38335530Sbonwick 	if (last_txg > txg)
38345530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
38355530Sbonwick 
38365530Sbonwick 	for (i = 0; i < ints; i++)
38375530Sbonwick 		value[i] = txg + object + i;
38385530Sbonwick 
383910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
384010922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
384110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
384210922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
38435530Sbonwick 
38445530Sbonwick 	dmu_tx_commit(tx);
38455530Sbonwick 
38465530Sbonwick 	/*
38475530Sbonwick 	 * Remove a random pair of entries.
38485530Sbonwick 	 */
38495530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
38505530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
38515530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
38525530Sbonwick 
38535530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38545530Sbonwick 
38555530Sbonwick 	if (error == ENOENT)
38565530Sbonwick 		return;
38575530Sbonwick 
38585530Sbonwick 	ASSERT3U(error, ==, 0);
38595530Sbonwick 
38605530Sbonwick 	tx = dmu_tx_create(os);
386110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
386210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
386310922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38645530Sbonwick 		return;
386510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
386610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3867789Sahrens 	dmu_tx_commit(tx);
3868789Sahrens }
3869789Sahrens 
387010431SSanjeev.Bagewadi@Sun.COM /*
387110431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
387210431SSanjeev.Bagewadi@Sun.COM  */
387310431SSanjeev.Bagewadi@Sun.COM void
387410922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
387510431SSanjeev.Bagewadi@Sun.COM {
387610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
387710922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
387810922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
387910922SJeff.Bonwick@Sun.COM 
388010922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
388110922SJeff.Bonwick@Sun.COM 
388210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
388310922SJeff.Bonwick@Sun.COM 		return;
388410922SJeff.Bonwick@Sun.COM 
388510922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
388610431SSanjeev.Bagewadi@Sun.COM 
388710431SSanjeev.Bagewadi@Sun.COM 	/*
388810922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
388910922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
389010922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
389110431SSanjeev.Bagewadi@Sun.COM 	 */
389210922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
389310922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
389410922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
389510922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
389610922SJeff.Bonwick@Sun.COM 		int error;
389710922SJeff.Bonwick@Sun.COM 
389810922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
389910922SJeff.Bonwick@Sun.COM 		    id, value);
390010431SSanjeev.Bagewadi@Sun.COM 
390110431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
390210922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
390310922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
390410922SJeff.Bonwick@Sun.COM 		if (txg == 0)
390510431SSanjeev.Bagewadi@Sun.COM 			return;
390610922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
390710922SJeff.Bonwick@Sun.COM 		    &value, tx);
390810431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
390910431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
391010431SSanjeev.Bagewadi@Sun.COM 	}
391110431SSanjeev.Bagewadi@Sun.COM }
391210431SSanjeev.Bagewadi@Sun.COM 
391310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3914789Sahrens void
391510922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3916789Sahrens {
391710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
391810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3919789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3920789Sahrens 	dmu_tx_t *tx;
3921789Sahrens 	int i, namelen, error;
392210922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3923789Sahrens 	char name[20], string_value[20];
3924789Sahrens 	void *data;
3925789Sahrens 
392610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
392710922SJeff.Bonwick@Sun.COM 
392810922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
392910922SJeff.Bonwick@Sun.COM 		return;
393010922SJeff.Bonwick@Sun.COM 
393110922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
393210922SJeff.Bonwick@Sun.COM 
39335530Sbonwick 	/*
39345530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
39355530Sbonwick 	 * x is a random printable character and the dots are dots.
39365530Sbonwick 	 * There are 94 such characters, and the name length goes from
39375530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
39385530Sbonwick 	 */
39395530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
39405530Sbonwick 
39415530Sbonwick 	for (i = 0; i < 3; i++)
39425530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
39435530Sbonwick 	for (; i < namelen - 1; i++)
39445530Sbonwick 		name[i] = '.';
39455530Sbonwick 	name[i] = '\0';
39465530Sbonwick 
394710922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
39485530Sbonwick 		wsize = sizeof (txg);
39495530Sbonwick 		wc = 1;
39505530Sbonwick 		data = &txg;
39515530Sbonwick 	} else {
39525530Sbonwick 		wsize = 1;
39535530Sbonwick 		wc = namelen;
39545530Sbonwick 		data = string_value;
39555530Sbonwick 	}
39565530Sbonwick 
39575530Sbonwick 	count = -1ULL;
39585530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
39595530Sbonwick 	ASSERT(count != -1ULL);
39605530Sbonwick 
39615530Sbonwick 	/*
39625530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
39635530Sbonwick 	 */
39645530Sbonwick 	i = ztest_random(5);
39655530Sbonwick 
39665530Sbonwick 	if (i >= 2) {
39675530Sbonwick 		tx = dmu_tx_create(os);
396810922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
396910922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
397010922SJeff.Bonwick@Sun.COM 		if (txg == 0)
39715530Sbonwick 			return;
39725530Sbonwick 		bcopy(name, string_value, namelen);
39735530Sbonwick 	} else {
39745530Sbonwick 		tx = NULL;
39755530Sbonwick 		txg = 0;
39765530Sbonwick 		bzero(string_value, namelen);
39775530Sbonwick 	}
39785530Sbonwick 
39795530Sbonwick 	switch (i) {
39805530Sbonwick 
39815530Sbonwick 	case 0:
39825530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
39835530Sbonwick 		if (error == 0) {
39845530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
39855530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3986789Sahrens 		} else {
39875530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3988789Sahrens 		}
39895530Sbonwick 		break;
39905530Sbonwick 
39915530Sbonwick 	case 1:
39925530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
39935530Sbonwick 		if (error == 0) {
39945530Sbonwick 			if (data == string_value &&
39955530Sbonwick 			    bcmp(name, data, namelen) != 0)
39965530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
39975530Sbonwick 				    name, data, namelen);
39985530Sbonwick 		} else {
39995530Sbonwick 			ASSERT3U(error, ==, ENOENT);
4000789Sahrens 		}
40015530Sbonwick 		break;
40025530Sbonwick 
40035530Sbonwick 	case 2:
40045530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
40055530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
40065530Sbonwick 		break;
40075530Sbonwick 
40085530Sbonwick 	case 3:
40095530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
40105530Sbonwick 		break;
40115530Sbonwick 
40125530Sbonwick 	case 4:
40135530Sbonwick 		error = zap_remove(os, object, name, tx);
40145530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
40155530Sbonwick 		break;
4016789Sahrens 	}
40175530Sbonwick 
40185530Sbonwick 	if (tx != NULL)
40195530Sbonwick 		dmu_tx_commit(tx);
4020789Sahrens }
4021789Sahrens 
402210612SRicardo.M.Correia@Sun.COM /*
402310612SRicardo.M.Correia@Sun.COM  * Commit callback data.
402410612SRicardo.M.Correia@Sun.COM  */
402510612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
402610612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
402710612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
402810612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
402910612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
403010612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
403110612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
403210612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
403310612SRicardo.M.Correia@Sun.COM 
403410612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
403510612SRicardo.M.Correia@Sun.COM static void
403610612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
403710612SRicardo.M.Correia@Sun.COM {
403810612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
403910612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
404010612SRicardo.M.Correia@Sun.COM 
404110612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
404210612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
404310612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
404410612SRicardo.M.Correia@Sun.COM 
404510612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
404610612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
404710612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
404810612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
404910612SRicardo.M.Correia@Sun.COM 		    synced_txg);
405010612SRicardo.M.Correia@Sun.COM 
405110612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
405210612SRicardo.M.Correia@Sun.COM 
405310612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
405410612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
405510612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
405610612SRicardo.M.Correia@Sun.COM 
405710612SRicardo.M.Correia@Sun.COM 		/*
405810612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
405910612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
406010612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
406110612SRicardo.M.Correia@Sun.COM 		 */
406210612SRicardo.M.Correia@Sun.COM 		return;
406310612SRicardo.M.Correia@Sun.COM 	}
406410612SRicardo.M.Correia@Sun.COM 
406510612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
406610612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
406710612SRicardo.M.Correia@Sun.COM 		goto out;
406810612SRicardo.M.Correia@Sun.COM 
406910612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
407010612SRicardo.M.Correia@Sun.COM 
407110612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
407210612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
407310612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
407410612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
407510612SRicardo.M.Correia@Sun.COM 
407610612SRicardo.M.Correia@Sun.COM out:
407710612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
407810612SRicardo.M.Correia@Sun.COM }
407910612SRicardo.M.Correia@Sun.COM 
408010612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
408110612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
408210612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
408310612SRicardo.M.Correia@Sun.COM {
408410612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
408510612SRicardo.M.Correia@Sun.COM 
408610612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
408710612SRicardo.M.Correia@Sun.COM 
408810612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
408910612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
409010612SRicardo.M.Correia@Sun.COM 
409110612SRicardo.M.Correia@Sun.COM 	return (cb_data);
409210612SRicardo.M.Correia@Sun.COM }
409310612SRicardo.M.Correia@Sun.COM 
409410612SRicardo.M.Correia@Sun.COM /*
409510612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
409610612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
409710612SRicardo.M.Correia@Sun.COM  * implementation bug.
409810612SRicardo.M.Correia@Sun.COM  */
409910612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
410010612SRicardo.M.Correia@Sun.COM 
410110612SRicardo.M.Correia@Sun.COM /*
410210612SRicardo.M.Correia@Sun.COM  * Commit callback test.
410310612SRicardo.M.Correia@Sun.COM  */
410410612SRicardo.M.Correia@Sun.COM void
410510922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
410610612SRicardo.M.Correia@Sun.COM {
410710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
410810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
410910612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
411010612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
411110612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
411210612SRicardo.M.Correia@Sun.COM 	int i, error;
411310612SRicardo.M.Correia@Sun.COM 
411410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
411510922SJeff.Bonwick@Sun.COM 
411610922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
411710922SJeff.Bonwick@Sun.COM 		return;
411810922SJeff.Bonwick@Sun.COM 
411910612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
412010612SRicardo.M.Correia@Sun.COM 
412110612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
412210612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
412310612SRicardo.M.Correia@Sun.COM 
412410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
412510612SRicardo.M.Correia@Sun.COM 
412610612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
412710612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
412810612SRicardo.M.Correia@Sun.COM 		error = -1;
412910612SRicardo.M.Correia@Sun.COM 
413010612SRicardo.M.Correia@Sun.COM 	if (!error)
413110612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
413210612SRicardo.M.Correia@Sun.COM 
413310612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
413410612SRicardo.M.Correia@Sun.COM 
413510612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
413610612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
413710612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
413810612SRicardo.M.Correia@Sun.COM 
413910612SRicardo.M.Correia@Sun.COM 	if (error) {
414010612SRicardo.M.Correia@Sun.COM 		/*
414110612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
414210612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
414310612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
414410612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
414510612SRicardo.M.Correia@Sun.COM 		 */
414610612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
414710612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
414810612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
414910612SRicardo.M.Correia@Sun.COM 		}
415010612SRicardo.M.Correia@Sun.COM 
415110612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
415210612SRicardo.M.Correia@Sun.COM 
415310612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
415410612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
415510612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
415610612SRicardo.M.Correia@Sun.COM 		}
415710612SRicardo.M.Correia@Sun.COM 
415810612SRicardo.M.Correia@Sun.COM 		return;
415910612SRicardo.M.Correia@Sun.COM 	}
416010612SRicardo.M.Correia@Sun.COM 
416110612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
416210612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
416310612SRicardo.M.Correia@Sun.COM 
416410612SRicardo.M.Correia@Sun.COM 	/*
416510612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
416610612SRicardo.M.Correia@Sun.COM 	 */
416710922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
416810612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
416910612SRicardo.M.Correia@Sun.COM 
417010612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
417110612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
417210612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
417310612SRicardo.M.Correia@Sun.COM 
417410922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
417510612SRicardo.M.Correia@Sun.COM 
417610612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
417710612SRicardo.M.Correia@Sun.COM 
417810612SRicardo.M.Correia@Sun.COM 	/*
417910612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
418010612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
418110612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
418210612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
418310612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
418410612SRicardo.M.Correia@Sun.COM 	 * implementation.
418510612SRicardo.M.Correia@Sun.COM 	 *
418610612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
418710612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
418810612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
418910612SRicardo.M.Correia@Sun.COM 	 */
419010612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
419110612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
419210612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
419310612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
419410612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
419510612SRicardo.M.Correia@Sun.COM 	}
419610612SRicardo.M.Correia@Sun.COM 
419710612SRicardo.M.Correia@Sun.COM 	/*
419810612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
419910612SRicardo.M.Correia@Sun.COM 	 *
420010612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
420110612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
420210612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
420310612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
420410612SRicardo.M.Correia@Sun.COM 	 */
420510612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
420610612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
420710612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
420810612SRicardo.M.Correia@Sun.COM 
420910612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
421010612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
421110612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
421210612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
421310612SRicardo.M.Correia@Sun.COM 		else
421410612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
421510612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
421610612SRicardo.M.Correia@Sun.COM 
421710612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
421810612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
421910612SRicardo.M.Correia@Sun.COM 
422010612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
422110612SRicardo.M.Correia@Sun.COM 	}
422210612SRicardo.M.Correia@Sun.COM 
422310612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
422410612SRicardo.M.Correia@Sun.COM 
422510612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
422610612SRicardo.M.Correia@Sun.COM }
422710612SRicardo.M.Correia@Sun.COM 
422810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4229789Sahrens void
423010922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4231789Sahrens {
423210922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
423310922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
423410922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
423510922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
423610922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
423710922SJeff.Bonwick@Sun.COM 	};
423810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
423910922SJeff.Bonwick@Sun.COM 
424010922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
424110922SJeff.Bonwick@Sun.COM 
424210922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
424310922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
424410922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
424510922SJeff.Bonwick@Sun.COM 
424610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
424710922SJeff.Bonwick@Sun.COM }
424810922SJeff.Bonwick@Sun.COM 
424910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
425010922SJeff.Bonwick@Sun.COM void
425110922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
425210922SJeff.Bonwick@Sun.COM {
425310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
425410922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
425510922SJeff.Bonwick@Sun.COM 
425610922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
425710922SJeff.Bonwick@Sun.COM 
425810922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
425910922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
426010922SJeff.Bonwick@Sun.COM 
426110922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
426210922SJeff.Bonwick@Sun.COM 
426310922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
426410922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
426510922SJeff.Bonwick@Sun.COM 
426610922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
426710922SJeff.Bonwick@Sun.COM 
426810922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4269789Sahrens }
4270789Sahrens 
4271789Sahrens /*
427210693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
427310693Schris.kirby@sun.com  */
427410693Schris.kirby@sun.com void
427510922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
427610693Schris.kirby@sun.com {
427710693Schris.kirby@sun.com 	int error;
427810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
427910693Schris.kirby@sun.com 	objset_t *origin;
428010693Schris.kirby@sun.com 	char snapname[100];
428110693Schris.kirby@sun.com 	char fullname[100];
428210693Schris.kirby@sun.com 	char clonename[100];
428310693Schris.kirby@sun.com 	char tag[100];
428410693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
428510693Schris.kirby@sun.com 
428610693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
428710693Schris.kirby@sun.com 
428810693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
428910693Schris.kirby@sun.com 
429010922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
429110693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
429210922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
429310922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
429410693Schris.kirby@sun.com 
429510693Schris.kirby@sun.com 	/*
429610693Schris.kirby@sun.com 	 * Clean up from any previous run.
429710693Schris.kirby@sun.com 	 */
429810693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
429910693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
430010693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
430110693Schris.kirby@sun.com 
430210693Schris.kirby@sun.com 	/*
430310693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
430410693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
430510693Schris.kirby@sun.com 	 */
430610693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
430710693Schris.kirby@sun.com 	if (error) {
430810693Schris.kirby@sun.com 		if (error == ENOSPC) {
430910693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
431010693Schris.kirby@sun.com 			goto out;
431110693Schris.kirby@sun.com 		}
431210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
431310693Schris.kirby@sun.com 	}
431410693Schris.kirby@sun.com 
431510693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
431610693Schris.kirby@sun.com 	if (error)
431710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
431810693Schris.kirby@sun.com 
431910693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
432010693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
432110693Schris.kirby@sun.com 	if (error) {
432210693Schris.kirby@sun.com 		if (error == ENOSPC) {
432310693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
432410693Schris.kirby@sun.com 			goto out;
432510693Schris.kirby@sun.com 		}
432610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
432710693Schris.kirby@sun.com 	}
432810693Schris.kirby@sun.com 
432910693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
433010693Schris.kirby@sun.com 	if (error) {
433110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
433210693Schris.kirby@sun.com 		    fullname, error);
433310693Schris.kirby@sun.com 	}
433410693Schris.kirby@sun.com 
433510693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
433610693Schris.kirby@sun.com 	if (error)
433710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
433810693Schris.kirby@sun.com 
433910693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
434010693Schris.kirby@sun.com 	if (error != ENOENT)
434110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
434210693Schris.kirby@sun.com 
434310693Schris.kirby@sun.com 	/*
434410693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
434510693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
434610693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
434710693Schris.kirby@sun.com 	 */
434810693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
434910693Schris.kirby@sun.com 	if (error) {
435010693Schris.kirby@sun.com 		if (error == ENOSPC) {
435110693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
435210693Schris.kirby@sun.com 			goto out;
435310693Schris.kirby@sun.com 		}
435410693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
435510693Schris.kirby@sun.com 	}
435610693Schris.kirby@sun.com 
435710693Schris.kirby@sun.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE, B_TRUE);
435810693Schris.kirby@sun.com 	if (error)
435910693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
436010693Schris.kirby@sun.com 
436110693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
436210693Schris.kirby@sun.com 	if (error != EBUSY) {
436310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
436410693Schris.kirby@sun.com 		    fullname, error);
436510693Schris.kirby@sun.com 	}
436610693Schris.kirby@sun.com 
436710693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
436810693Schris.kirby@sun.com 	if (error) {
436910693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
437010693Schris.kirby@sun.com 		    fullname, error);
437110693Schris.kirby@sun.com 	}
437210693Schris.kirby@sun.com 
437310693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
437410693Schris.kirby@sun.com 	if (error)
437510693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
437610693Schris.kirby@sun.com 
437710693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
437810693Schris.kirby@sun.com 
437910693Schris.kirby@sun.com out:
438010693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
438110693Schris.kirby@sun.com }
438210693Schris.kirby@sun.com 
438310693Schris.kirby@sun.com /*
4384789Sahrens  * Inject random faults into the on-disk data.
4385789Sahrens  */
438610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4387789Sahrens void
438810922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4389789Sahrens {
439010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
439110922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4392789Sahrens 	int fd;
4393789Sahrens 	uint64_t offset;
439411422SMark.Musante@Sun.COM 	uint64_t leaves;
4395789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4396789Sahrens 	uint64_t top, leaf;
4397789Sahrens 	char path0[MAXPATHLEN];
4398789Sahrens 	char pathrand[MAXPATHLEN];
4399789Sahrens 	size_t fsize;
4400789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4401789Sahrens 	int iters = 1000;
440211422SMark.Musante@Sun.COM 	int maxfaults;
440311422SMark.Musante@Sun.COM 	int mirror_save;
44047754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
44051544Seschrock 	uint64_t guid0 = 0;
440610685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
44071544Seschrock 
440811422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
440911422SMark.Musante@Sun.COM 	maxfaults = MAXFAULTS();
441011422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
441111422SMark.Musante@Sun.COM 	mirror_save = zs->zs_mirrors;
441211422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
441311422SMark.Musante@Sun.COM 
44147754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4415789Sahrens 
4416789Sahrens 	/*
44177754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4418789Sahrens 	 */
44197754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44207754SJeff.Bonwick@Sun.COM 
44217754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
44227754SJeff.Bonwick@Sun.COM 		/*
442310922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
44247754SJeff.Bonwick@Sun.COM 		 */
442510922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
442611422SMark.Musante@Sun.COM 		leaf = ztest_random(leaves) + zs->zs_splits;
44277754SJeff.Bonwick@Sun.COM 
44287754SJeff.Bonwick@Sun.COM 		/*
44297754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
44307754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
44317754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
44327754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
44337754SJeff.Bonwick@Sun.COM 		 */
44347754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
443511422SMark.Musante@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
44367754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
44377754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
44387754SJeff.Bonwick@Sun.COM 
44397754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
444010685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
444110685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
444210685SGeorge.Wilson@Sun.COM 
44437754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
44447754SJeff.Bonwick@Sun.COM 			/*
44457754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
44467754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
44477754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
44487754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
44497754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
44507754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
44517754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
44527754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
44537754SJeff.Bonwick@Sun.COM 			 */
44547754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
44557754SJeff.Bonwick@Sun.COM 
44567754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
44577754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
44587754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
44597754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
44607754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
44617754SJeff.Bonwick@Sun.COM 			} else {
44627754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
44637754SJeff.Bonwick@Sun.COM 			}
44647754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
44657754SJeff.Bonwick@Sun.COM 		}
44667754SJeff.Bonwick@Sun.COM 	} else {
44677754SJeff.Bonwick@Sun.COM 		/*
44687754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
44697754SJeff.Bonwick@Sun.COM 		 */
44707754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
44717754SJeff.Bonwick@Sun.COM 
44727754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
44737754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
44747754SJeff.Bonwick@Sun.COM 			return;
44757754SJeff.Bonwick@Sun.COM 		}
44767754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
44777754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
44787754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
44797754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
44807754SJeff.Bonwick@Sun.COM 
44817754SJeff.Bonwick@Sun.COM 		leaf = 0;
44827754SJeff.Bonwick@Sun.COM 		leaves = 1;
44837754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
44847754SJeff.Bonwick@Sun.COM 	}
4485789Sahrens 
44867754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
44877754SJeff.Bonwick@Sun.COM 
44881544Seschrock 	/*
448910685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
449010685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
44911544Seschrock 	 */
449210685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
44938241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
44948241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
44958241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
449610685SGeorge.Wilson@Sun.COM 
449710685SGeorge.Wilson@Sun.COM 			/*
449810685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
449910685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
450010685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
450110685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
450210685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
450310685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
450410685SGeorge.Wilson@Sun.COM 			 */
450510685SGeorge.Wilson@Sun.COM 			if (islog)
450610685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
450710685SGeorge.Wilson@Sun.COM 
45088241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
450910685SGeorge.Wilson@Sun.COM 
451010685SGeorge.Wilson@Sun.COM 			if (islog)
451110685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
45128241SJeff.Bonwick@Sun.COM 		} else {
45138241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
45148241SJeff.Bonwick@Sun.COM 		}
4515789Sahrens 	}
4516789Sahrens 
451710685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
451810685SGeorge.Wilson@Sun.COM 		return;
451910685SGeorge.Wilson@Sun.COM 
4520789Sahrens 	/*
45211544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4522789Sahrens 	 */
4523789Sahrens 	fd = open(pathrand, O_RDWR);
4524789Sahrens 
4525789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4526789Sahrens 		return;
4527789Sahrens 
4528789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4529789Sahrens 
4530789Sahrens 	while (--iters != 0) {
4531789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4532789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4533789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4534789Sahrens 
4535789Sahrens 		if (offset >= fsize)
4536789Sahrens 			continue;
4537789Sahrens 
453811422SMark.Musante@Sun.COM 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
453911422SMark.Musante@Sun.COM 		if (mirror_save != zs->zs_mirrors) {
454011422SMark.Musante@Sun.COM 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
454111422SMark.Musante@Sun.COM 			(void) close(fd);
454211422SMark.Musante@Sun.COM 			return;
454311422SMark.Musante@Sun.COM 		}
4544789Sahrens 
4545789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4546789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4547789Sahrens 			    offset, pathrand);
454811422SMark.Musante@Sun.COM 
454911422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
455011422SMark.Musante@Sun.COM 
455111422SMark.Musante@Sun.COM 		if (zopt_verbose >= 7)
455211422SMark.Musante@Sun.COM 			(void) printf("injected bad word into %s,"
455311422SMark.Musante@Sun.COM 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4554789Sahrens 	}
4555789Sahrens 
4556789Sahrens 	(void) close(fd);
4557789Sahrens }
4558789Sahrens 
4559789Sahrens /*
456010922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4561789Sahrens  */
4562789Sahrens void
456310922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4564789Sahrens {
456510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
456610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
456710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
456810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
456910922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
457010922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
457110922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
457210922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
457310922SJeff.Bonwick@Sun.COM 	void *buf;
457410922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
457510922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
457610922SJeff.Bonwick@Sun.COM 
457710922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
457810922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
457910922SJeff.Bonwick@Sun.COM 
458010922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
458110922SJeff.Bonwick@Sun.COM 
458210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
458310922SJeff.Bonwick@Sun.COM 		return;
458410922SJeff.Bonwick@Sun.COM 
458510922SJeff.Bonwick@Sun.COM 	/*
458610922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
458710922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
458810922SJeff.Bonwick@Sun.COM 	 */
458910922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
459010922SJeff.Bonwick@Sun.COM 
459110922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
459210922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
459310922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
459410922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
459510922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
459610922SJeff.Bonwick@Sun.COM 		return;
459710922SJeff.Bonwick@Sun.COM 	}
459810922SJeff.Bonwick@Sun.COM 
459910922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
460010922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
460110922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
460210922SJeff.Bonwick@Sun.COM 
460310922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
460410922SJeff.Bonwick@Sun.COM 
460510922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
460610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
460710922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
460810922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
460910922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
461010922SJeff.Bonwick@Sun.COM 		return;
461110922SJeff.Bonwick@Sun.COM 	}
461210922SJeff.Bonwick@Sun.COM 
461310922SJeff.Bonwick@Sun.COM 	/*
461410922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
461510922SJeff.Bonwick@Sun.COM 	 */
461610922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
461710922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
461812285SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
461912285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH) == 0);
462010922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
462110922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
462210922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
462310922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
462410922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
462510922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
462610922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
462710922SJeff.Bonwick@Sun.COM 	}
462810922SJeff.Bonwick@Sun.COM 
462910922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
463010922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
463110922SJeff.Bonwick@Sun.COM 
463210922SJeff.Bonwick@Sun.COM 	/*
463310922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
463410922SJeff.Bonwick@Sun.COM 	 */
463512285SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
463612285SJeff.Bonwick@Sun.COM 	    DMU_READ_NO_PREFETCH) == 0);
463710922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
463810922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
463910922SJeff.Bonwick@Sun.COM 
464010922SJeff.Bonwick@Sun.COM 	/*
464110922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
464210922SJeff.Bonwick@Sun.COM 	 */
464310922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
464410922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
464510922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
464610922SJeff.Bonwick@Sun.COM 
464710922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
464810922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
464910922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
465010922SJeff.Bonwick@Sun.COM 
465110922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
465210922SJeff.Bonwick@Sun.COM 
465310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
465410922SJeff.Bonwick@Sun.COM }
465510922SJeff.Bonwick@Sun.COM 
465610922SJeff.Bonwick@Sun.COM /*
465710922SJeff.Bonwick@Sun.COM  * Scrub the pool.
465810922SJeff.Bonwick@Sun.COM  */
465910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
466010922SJeff.Bonwick@Sun.COM void
466110922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
466210922SJeff.Bonwick@Sun.COM {
466310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
466410922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4665789Sahrens 
4666*12296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
466710922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4668*12296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
4669789Sahrens }
4670789Sahrens 
4671789Sahrens /*
4672789Sahrens  * Rename the pool to a different name and then rename it back.
4673789Sahrens  */
467410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4675789Sahrens void
467610922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4677789Sahrens {
467810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4679789Sahrens 	char *oldname, *newname;
4680789Sahrens 	spa_t *spa;
4681789Sahrens 
468210922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
468310922SJeff.Bonwick@Sun.COM 
468410922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4685789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4686789Sahrens 	(void) strcpy(newname, oldname);
4687789Sahrens 	(void) strcat(newname, "_tmp");
4688789Sahrens 
4689789Sahrens 	/*
4690789Sahrens 	 * Do the rename
4691789Sahrens 	 */
469210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4693789Sahrens 
4694789Sahrens 	/*
4695789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4696789Sahrens 	 */
469710922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4698789Sahrens 
4699789Sahrens 	/*
4700789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4701789Sahrens 	 */
470210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
470310922SJeff.Bonwick@Sun.COM 
470410922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4705789Sahrens 	spa_close(spa, FTAG);
4706789Sahrens 
4707789Sahrens 	/*
4708789Sahrens 	 * Rename it back to the original
4709789Sahrens 	 */
471010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4711789Sahrens 
4712789Sahrens 	/*
4713789Sahrens 	 * Make sure it can still be opened
4714789Sahrens 	 */
471510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
471610922SJeff.Bonwick@Sun.COM 
471710922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4718789Sahrens 	spa_close(spa, FTAG);
4719789Sahrens 
4720789Sahrens 	umem_free(newname, strlen(newname) + 1);
4721789Sahrens 
472210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4723789Sahrens }
4724789Sahrens 
4725789Sahrens /*
472610922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4727789Sahrens  */
4728789Sahrens static void
472910922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4730789Sahrens {
4731789Sahrens 	int status;
4732789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4733789Sahrens 	char zbuf[1024];
4734789Sahrens 	char *bin;
47354527Sperrin 	char *ztest;
47364527Sperrin 	char *isa;
47374527Sperrin 	int isalen;
4738789Sahrens 	FILE *fp;
4739789Sahrens 
4740789Sahrens 	(void) realpath(getexecname(), zdb);
4741789Sahrens 
4742789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4743789Sahrens 	bin = strstr(zdb, "/usr/bin/");
47444527Sperrin 	ztest = strstr(bin, "/ztest");
47454527Sperrin 	isa = bin + 8;
47464527Sperrin 	isalen = ztest - isa;
47474527Sperrin 	isa = strdup(isa);
4748789Sahrens 	/* LINTED */
47495994Sck153898 	(void) sprintf(bin,
475011811SJeff.Bonwick@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
47514527Sperrin 	    isalen,
47524527Sperrin 	    isa,
4753789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4754789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
475511811SJeff.Bonwick@Sun.COM 	    spa_config_path,
47567837SMatthew.Ahrens@Sun.COM 	    pool);
47574527Sperrin 	free(isa);
4758789Sahrens 
4759789Sahrens 	if (zopt_verbose >= 5)
4760789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4761789Sahrens 
4762789Sahrens 	fp = popen(zdb, "r");
4763789Sahrens 
4764789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4765789Sahrens 		if (zopt_verbose >= 3)
4766789Sahrens 			(void) printf("%s", zbuf);
4767789Sahrens 
4768789Sahrens 	status = pclose(fp);
4769789Sahrens 
4770789Sahrens 	if (status == 0)
4771789Sahrens 		return;
4772789Sahrens 
4773789Sahrens 	ztest_dump_core = 0;
4774789Sahrens 	if (WIFEXITED(status))
4775789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4776789Sahrens 	else
4777789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4778789Sahrens }
4779789Sahrens 
4780789Sahrens static void
4781789Sahrens ztest_walk_pool_directory(char *header)
4782789Sahrens {
4783789Sahrens 	spa_t *spa = NULL;
4784789Sahrens 
4785789Sahrens 	if (zopt_verbose >= 6)
4786789Sahrens 		(void) printf("%s\n", header);
4787789Sahrens 
4788789Sahrens 	mutex_enter(&spa_namespace_lock);
4789789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4790789Sahrens 		if (zopt_verbose >= 6)
4791789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4792789Sahrens 	mutex_exit(&spa_namespace_lock);
4793789Sahrens }
4794789Sahrens 
4795789Sahrens static void
4796789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4797789Sahrens {
47988241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4799789Sahrens 	uint64_t pool_guid;
4800789Sahrens 	spa_t *spa;
4801789Sahrens 
4802789Sahrens 	if (zopt_verbose >= 4) {
4803789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4804789Sahrens 		    oldname, newname);
4805789Sahrens 	}
4806789Sahrens 
4807789Sahrens 	/*
4808789Sahrens 	 * Clean up from previous runs.
4809789Sahrens 	 */
4810789Sahrens 	(void) spa_destroy(newname);
4811789Sahrens 
4812789Sahrens 	/*
4813789Sahrens 	 * Get the pool's configuration and guid.
4814789Sahrens 	 */
481510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4816789Sahrens 
48178241SJeff.Bonwick@Sun.COM 	/*
48188241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
48198241SJeff.Bonwick@Sun.COM 	 */
48208241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
4821*12296SLin.Ling@Sun.COM 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
48228241SJeff.Bonwick@Sun.COM 
4823789Sahrens 	pool_guid = spa_guid(spa);
4824789Sahrens 	spa_close(spa, FTAG);
4825789Sahrens 
4826789Sahrens 	ztest_walk_pool_directory("pools before export");
4827789Sahrens 
4828789Sahrens 	/*
4829789Sahrens 	 * Export it.
4830789Sahrens 	 */
483110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4832789Sahrens 
4833789Sahrens 	ztest_walk_pool_directory("pools after export");
4834789Sahrens 
4835789Sahrens 	/*
48368241SJeff.Bonwick@Sun.COM 	 * Try to import it.
48378241SJeff.Bonwick@Sun.COM 	 */
48388241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
48398241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
48408241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
48418241SJeff.Bonwick@Sun.COM 
48428241SJeff.Bonwick@Sun.COM 	/*
4843789Sahrens 	 * Import it under the new name.
4844789Sahrens 	 */
484510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL));
4846789Sahrens 
4847789Sahrens 	ztest_walk_pool_directory("pools after import");
4848789Sahrens 
4849789Sahrens 	/*
4850789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4851789Sahrens 	 */
485210922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL));
4853789Sahrens 
4854789Sahrens 	/*
4855789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4856789Sahrens 	 */
485710922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL));
4858789Sahrens 
4859789Sahrens 	/*
4860789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4861789Sahrens 	 */
486210922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4863789Sahrens 
4864789Sahrens 	/*
4865789Sahrens 	 * Verify that we can open and close the pool using the new name.
4866789Sahrens 	 */
486710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4868789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4869789Sahrens 	spa_close(spa, FTAG);
4870789Sahrens 
4871789Sahrens 	nvlist_free(config);
4872789Sahrens }
4873789Sahrens 
48748241SJeff.Bonwick@Sun.COM static void
48758241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
48768241SJeff.Bonwick@Sun.COM {
487710922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
487810922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
487910922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
488010922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
488110922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
488210922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
48838241SJeff.Bonwick@Sun.COM }
48848241SJeff.Bonwick@Sun.COM 
48855329Sgw25295 static void *
48868241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
48875329Sgw25295 {
48887754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
48895329Sgw25295 
48905329Sgw25295 	while (!ztest_exiting) {
489110922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
489210922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
489310922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
48945329Sgw25295 	}
48955329Sgw25295 	return (NULL);
48965329Sgw25295 }
48975329Sgw25295 
4898789Sahrens static void *
489910922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
490010922SJeff.Bonwick@Sun.COM {
490110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
490210922SJeff.Bonwick@Sun.COM 	int grace = 300;
490310922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
490410922SJeff.Bonwick@Sun.COM 
490510922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
490610922SJeff.Bonwick@Sun.COM 
490710922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
490810922SJeff.Bonwick@Sun.COM 
490910922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
491010922SJeff.Bonwick@Sun.COM 
491110922SJeff.Bonwick@Sun.COM 	return (NULL);
491210922SJeff.Bonwick@Sun.COM }
491310922SJeff.Bonwick@Sun.COM 
491410922SJeff.Bonwick@Sun.COM static void
491510922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
491610922SJeff.Bonwick@Sun.COM {
491710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
491810922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
491910922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
492010922SJeff.Bonwick@Sun.COM 
492110922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
492210922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
492310922SJeff.Bonwick@Sun.COM 
492410922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
492510922SJeff.Bonwick@Sun.COM 
492610922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
492710922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
492810922SJeff.Bonwick@Sun.COM 
492910922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
493010922SJeff.Bonwick@Sun.COM 		Dl_info dli;
493110922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
493210922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
493310922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
493410922SJeff.Bonwick@Sun.COM 	}
493510922SJeff.Bonwick@Sun.COM }
493610922SJeff.Bonwick@Sun.COM 
493710922SJeff.Bonwick@Sun.COM static void *
4938789Sahrens ztest_thread(void *arg)
4939789Sahrens {
494010922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4941789Sahrens 	ztest_shared_t *zs = ztest_shared;
494210922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
494310922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4944789Sahrens 	ztest_info_t *zi;
494510922SJeff.Bonwick@Sun.COM 
494610922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4947789Sahrens 		/*
4948789Sahrens 		 * See if it's time to force a crash.
4949789Sahrens 		 */
495010922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
495110922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4952789Sahrens 
4953789Sahrens 		/*
4954789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4955789Sahrens 		 */
4956789Sahrens 		if (zs->zs_enospc_count > 10)
4957789Sahrens 			break;
495810922SJeff.Bonwick@Sun.COM 
495910922SJeff.Bonwick@Sun.COM 		/*
496010922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
496110922SJeff.Bonwick@Sun.COM 		 */
496210922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
496310922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
496410922SJeff.Bonwick@Sun.COM 
496510922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
496610922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
496710922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
496810922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4969789Sahrens 	}
4970789Sahrens 
4971789Sahrens 	return (NULL);
4972789Sahrens }
4973789Sahrens 
497410922SJeff.Bonwick@Sun.COM static void
497510922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
497610922SJeff.Bonwick@Sun.COM {
497710922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
497810922SJeff.Bonwick@Sun.COM }
497910922SJeff.Bonwick@Sun.COM 
498010922SJeff.Bonwick@Sun.COM static void
498110922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
498210922SJeff.Bonwick@Sun.COM {
498310922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
498410922SJeff.Bonwick@Sun.COM 
498510922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
498610922SJeff.Bonwick@Sun.COM 
498710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
498810922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
498910922SJeff.Bonwick@Sun.COM 
499010922SJeff.Bonwick@Sun.COM 	/*
499110922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
499210922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
499310922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
499410922SJeff.Bonwick@Sun.COM 	 */
499510922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
499610922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
499710922SJeff.Bonwick@Sun.COM 
499810922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
499910922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
500010922SJeff.Bonwick@Sun.COM }
500110922SJeff.Bonwick@Sun.COM 
500210922SJeff.Bonwick@Sun.COM static void
500310922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
500410922SJeff.Bonwick@Sun.COM {
500510922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
500610922SJeff.Bonwick@Sun.COM 
500710922SJeff.Bonwick@Sun.COM 	/*
500810922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
500910922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
501010922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
501110922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
501210922SJeff.Bonwick@Sun.COM 	 *
501310922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
501410922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
501510922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
501610922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
501710922SJeff.Bonwick@Sun.COM 	 */
501810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
501910922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
502010922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
502110922SJeff.Bonwick@Sun.COM }
502210922SJeff.Bonwick@Sun.COM 
502310922SJeff.Bonwick@Sun.COM static int
502410922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
502510922SJeff.Bonwick@Sun.COM {
502610922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
502710922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
502810922SJeff.Bonwick@Sun.COM 	objset_t *os;
502910922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
503010922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
503110922SJeff.Bonwick@Sun.COM 	int error;
503210922SJeff.Bonwick@Sun.COM 
503310922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
503410922SJeff.Bonwick@Sun.COM 
503510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
503610922SJeff.Bonwick@Sun.COM 
503712294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
503810922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
503910922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
504010922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
504110922SJeff.Bonwick@Sun.COM 		return (error);
504210922SJeff.Bonwick@Sun.COM 	}
504310922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
504410922SJeff.Bonwick@Sun.COM 
504510922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
504610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
504710922SJeff.Bonwick@Sun.COM 
504810922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
504910922SJeff.Bonwick@Sun.COM 
505010922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
505110922SJeff.Bonwick@Sun.COM 
505210922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
505310922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
505410922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
505510922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
505610922SJeff.Bonwick@Sun.COM 
505710922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
505810922SJeff.Bonwick@Sun.COM 
505910922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
506010922SJeff.Bonwick@Sun.COM 
506110922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
506210922SJeff.Bonwick@Sun.COM 
506310922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
506410922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
506510922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
506610922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
506710922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
506810922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
506910922SJeff.Bonwick@Sun.COM 
507010922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
507110922SJeff.Bonwick@Sun.COM 
507210922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
507310922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
507410922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
507510922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
507610922SJeff.Bonwick@Sun.COM 
507710922SJeff.Bonwick@Sun.COM 	return (0);
507810922SJeff.Bonwick@Sun.COM }
507910922SJeff.Bonwick@Sun.COM 
508010922SJeff.Bonwick@Sun.COM static void
508110922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
508210922SJeff.Bonwick@Sun.COM {
508310922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
508410922SJeff.Bonwick@Sun.COM 
508510922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
508610922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
508710922SJeff.Bonwick@Sun.COM 
508810922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
508910922SJeff.Bonwick@Sun.COM }
509010922SJeff.Bonwick@Sun.COM 
5091789Sahrens /*
5092789Sahrens  * Kick off threads to run tests on all datasets in parallel.
5093789Sahrens  */
5094789Sahrens static void
509510922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
5096789Sahrens {
509710922SJeff.Bonwick@Sun.COM 	thread_t *tid;
5098789Sahrens 	spa_t *spa;
50997754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
510010922SJeff.Bonwick@Sun.COM 	int error;
51017754SJeff.Bonwick@Sun.COM 
51027754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
5103789Sahrens 
510410922SJeff.Bonwick@Sun.COM 	/*
510510922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
510610922SJeff.Bonwick@Sun.COM 	 */
510710922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
510810922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
510910922SJeff.Bonwick@Sun.COM 
511010922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
511110922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
511210922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
511310922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
511410922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
511510922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
511610922SJeff.Bonwick@Sun.COM 
511710922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
511810612SRicardo.M.Correia@Sun.COM 
511910612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
512010612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
512110612SRicardo.M.Correia@Sun.COM 
5122789Sahrens 	/*
51237754SJeff.Bonwick@Sun.COM 	 * Open our pool.
51245329Sgw25295 	 */
512510922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
512610922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
512710922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
512810922SJeff.Bonwick@Sun.COM 
512910922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
51305329Sgw25295 
51315329Sgw25295 	/*
51328241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
51338241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
51348241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
51358241SJeff.Bonwick@Sun.COM 	 */
513611422SMark.Musante@Sun.COM 	if (MAXFAULTS() == 0)
51378241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
51388241SJeff.Bonwick@Sun.COM 	else
51398241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
51408241SJeff.Bonwick@Sun.COM 
51418241SJeff.Bonwick@Sun.COM 	/*
51427754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5143789Sahrens 	 */
51448241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
51457754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5146789Sahrens 
5147789Sahrens 	/*
514810922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
514910922SJeff.Bonwick@Sun.COM 	 */
515010922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
515110922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
515210922SJeff.Bonwick@Sun.COM 
515310922SJeff.Bonwick@Sun.COM 	/*
5154789Sahrens 	 * Verify that we can safely inquire about about any object,
5155789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5156789Sahrens 	 * we probe a 5-wide window around each power of two.
5157789Sahrens 	 * This hits all edge cases, including zero and the max.
5158789Sahrens 	 */
515910922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
516010922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5161789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5162789Sahrens 			    (1ULL << t) + d, NULL);
51631544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
51641544Seschrock 			    error == EINVAL);
5165789Sahrens 		}
5166789Sahrens 	}
5167789Sahrens 
5168789Sahrens 	/*
516910922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5170789Sahrens 	 */
517110922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
517210922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
517310922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
517410922SJeff.Bonwick@Sun.COM 	}
5175789Sahrens 	zs->zs_enospc_count = 0;
5176789Sahrens 
517710922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5178789Sahrens 
5179789Sahrens 	if (zopt_verbose >= 4)
5180789Sahrens 		(void) printf("starting main threads...\n");
5181789Sahrens 
518210922SJeff.Bonwick@Sun.COM 	/*
518310922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
518410922SJeff.Bonwick@Sun.COM 	 */
518510922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
518610922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
518710922SJeff.Bonwick@Sun.COM 			return;
518810922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
518910922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5190789Sahrens 	}
5191789Sahrens 
5192789Sahrens 	/*
519310922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
519410922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5195789Sahrens 	 */
519610922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
519710922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
519810922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
519910922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5200789Sahrens 	}
5201789Sahrens 
52021544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5203789Sahrens 
520410922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
520510922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
520610922SJeff.Bonwick@Sun.COM 
520710922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
52087754SJeff.Bonwick@Sun.COM 
52097754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
52107754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
52117754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
52128241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
52137754SJeff.Bonwick@Sun.COM 
5214789Sahrens 	/*
5215789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5216789Sahrens 	 * spa_close() should wait for it to complete.
5217789Sahrens 	 */
521810922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
521910922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5220789Sahrens 
52215530Sbonwick 	spa_close(spa, FTAG);
52225530Sbonwick 
522310922SJeff.Bonwick@Sun.COM 	/*
522410922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
522510922SJeff.Bonwick@Sun.COM 	 */
522610922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
522710922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
522810922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
522910922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
523010922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
523110922SJeff.Bonwick@Sun.COM 
523210922SJeff.Bonwick@Sun.COM 	/*
523310922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
523410922SJeff.Bonwick@Sun.COM 	 * different name.
523510922SJeff.Bonwick@Sun.COM 	 */
523610922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
523710922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
523810922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
523910922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
524010922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
524110922SJeff.Bonwick@Sun.COM 	}
524210922SJeff.Bonwick@Sun.COM 
524310922SJeff.Bonwick@Sun.COM 	kernel_fini();
524410922SJeff.Bonwick@Sun.COM }
524510922SJeff.Bonwick@Sun.COM 
524610922SJeff.Bonwick@Sun.COM static void
524710922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
524810922SJeff.Bonwick@Sun.COM {
524910922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
525010922SJeff.Bonwick@Sun.COM 	spa_t *spa;
525111818SMark.Musante@Sun.COM 	int numloops = 0;
525210922SJeff.Bonwick@Sun.COM 
525310922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
525410922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
525510922SJeff.Bonwick@Sun.COM 
525610922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
525710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
525810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
525910922SJeff.Bonwick@Sun.COM 
526010922SJeff.Bonwick@Sun.COM 	/*
526110922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
526210922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
526310922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
526410922SJeff.Bonwick@Sun.COM 	 */
526510922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
526610922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
526710922SJeff.Bonwick@Sun.COM 		zil_commit(zd->zd_zilog, UINT64_MAX, 0);
526810922SJeff.Bonwick@Sun.COM 	}
526910922SJeff.Bonwick@Sun.COM 
527010922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
527110922SJeff.Bonwick@Sun.COM 
527210922SJeff.Bonwick@Sun.COM 	/*
527310922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
527410922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
527510922SJeff.Bonwick@Sun.COM 	 */
527610922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
527710922SJeff.Bonwick@Sun.COM 
527810922SJeff.Bonwick@Sun.COM 	/*
527910922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
528010922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
528110922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
528210922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
528310922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
528410922SJeff.Bonwick@Sun.COM 	 */
528511818SMark.Musante@Sun.COM 	while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
528610922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
528710922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
528810922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
528910922SJeff.Bonwick@Sun.COM 	}
529010922SJeff.Bonwick@Sun.COM 
529110922SJeff.Bonwick@Sun.COM 	/*
529210922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
529310922SJeff.Bonwick@Sun.COM 	 */
529410922SJeff.Bonwick@Sun.COM 	zil_commit(zd->zd_zilog, UINT64_MAX, 0);
529510922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
529610922SJeff.Bonwick@Sun.COM 
529710922SJeff.Bonwick@Sun.COM 	/*
529810922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
529910922SJeff.Bonwick@Sun.COM 	 */
530010922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
530110922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
530210922SJeff.Bonwick@Sun.COM 	kernel_fini();
530310922SJeff.Bonwick@Sun.COM 
530410922SJeff.Bonwick@Sun.COM 	/*
530510922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
530610922SJeff.Bonwick@Sun.COM 	 */
530710922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
530810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
530910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
531010922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
531110922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5312789Sahrens 	kernel_fini();
531310612SRicardo.M.Correia@Sun.COM 
531410612SRicardo.M.Correia@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
531510612SRicardo.M.Correia@Sun.COM 
531610612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
531710612SRicardo.M.Correia@Sun.COM 
531810612SRicardo.M.Correia@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
531910612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5320789Sahrens }
5321789Sahrens 
5322789Sahrens void
5323789Sahrens print_time(hrtime_t t, char *timebuf)
5324789Sahrens {
5325789Sahrens 	hrtime_t s = t / NANOSEC;
5326789Sahrens 	hrtime_t m = s / 60;
5327789Sahrens 	hrtime_t h = m / 60;
5328789Sahrens 	hrtime_t d = h / 24;
5329789Sahrens 
5330789Sahrens 	s -= m * 60;
5331789Sahrens 	m -= h * 60;
5332789Sahrens 	h -= d * 24;
5333789Sahrens 
5334789Sahrens 	timebuf[0] = '\0';
5335789Sahrens 
5336789Sahrens 	if (d)
5337789Sahrens 		(void) sprintf(timebuf,
5338789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5339789Sahrens 	else if (h)
5340789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5341789Sahrens 	else if (m)
5342789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5343789Sahrens 	else
5344789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5345789Sahrens }
5346789Sahrens 
534711422SMark.Musante@Sun.COM static nvlist_t *
534811422SMark.Musante@Sun.COM make_random_props()
534911422SMark.Musante@Sun.COM {
535011422SMark.Musante@Sun.COM 	nvlist_t *props;
535111422SMark.Musante@Sun.COM 
535211422SMark.Musante@Sun.COM 	if (ztest_random(2) == 0)
535311422SMark.Musante@Sun.COM 		return (NULL);
535411422SMark.Musante@Sun.COM 
535511422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
535611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
535711422SMark.Musante@Sun.COM 
535811422SMark.Musante@Sun.COM 	(void) printf("props:\n");
535911422SMark.Musante@Sun.COM 	dump_nvlist(props, 4);
536011422SMark.Musante@Sun.COM 
536111422SMark.Musante@Sun.COM 	return (props);
536211422SMark.Musante@Sun.COM }
536311422SMark.Musante@Sun.COM 
5364789Sahrens /*
5365789Sahrens  * Create a storage pool with the given name and initial vdev size.
536610922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5367789Sahrens  */
5368789Sahrens static void
536910922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5370789Sahrens {
5371789Sahrens 	spa_t *spa;
537211422SMark.Musante@Sun.COM 	nvlist_t *nvroot, *props;
5373789Sahrens 
537410922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
537510922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
537610922SJeff.Bonwick@Sun.COM 
5377789Sahrens 	kernel_init(FREAD | FWRITE);
5378789Sahrens 
5379789Sahrens 	/*
5380789Sahrens 	 * Create the storage pool.
5381789Sahrens 	 */
538210922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
538310594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
538411422SMark.Musante@Sun.COM 	zs->zs_splits = 0;
538511422SMark.Musante@Sun.COM 	zs->zs_mirrors = zopt_mirrors;
53867754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
538711422SMark.Musante@Sun.COM 	    0, zopt_raidz, zs->zs_mirrors, 1);
538811422SMark.Musante@Sun.COM 	props = make_random_props();
538911422SMark.Musante@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5390789Sahrens 	nvlist_free(nvroot);
5391789Sahrens 
539210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
53939480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5394789Sahrens 	spa_close(spa, FTAG);
5395789Sahrens 
5396789Sahrens 	kernel_fini();
539710922SJeff.Bonwick@Sun.COM 
539810922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
539910922SJeff.Bonwick@Sun.COM 
540010922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
540110922SJeff.Bonwick@Sun.COM 
540210922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5403789Sahrens }
5404789Sahrens 
5405789Sahrens int
5406789Sahrens main(int argc, char **argv)
5407789Sahrens {
5408789Sahrens 	int kills = 0;
5409789Sahrens 	int iters = 0;
5410789Sahrens 	ztest_shared_t *zs;
541110922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5412789Sahrens 	ztest_info_t *zi;
5413789Sahrens 	char timebuf[100];
5414789Sahrens 	char numbuf[6];
541510922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5416789Sahrens 
5417789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5418789Sahrens 
5419789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5420789Sahrens 
5421789Sahrens 	process_options(argc, argv);
5422789Sahrens 
542311811SJeff.Bonwick@Sun.COM 	/* Override location of zpool.cache */
542411811SJeff.Bonwick@Sun.COM 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
542511811SJeff.Bonwick@Sun.COM 
54261544Seschrock 	/*
54271544Seschrock 	 * Blow away any existing copy of zpool.cache
54281544Seschrock 	 */
54291544Seschrock 	if (zopt_init != 0)
543011811SJeff.Bonwick@Sun.COM 		(void) remove(spa_config_path);
54311544Seschrock 
543210922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
543310922SJeff.Bonwick@Sun.COM 
5434789Sahrens 	zs = ztest_shared = (void *)mmap(0,
543510922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5436789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5437789Sahrens 
5438789Sahrens 	if (zopt_verbose >= 1) {
5439789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5440789Sahrens 		    " %llu seconds...\n",
54411732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5442789Sahrens 		    (u_longlong_t)zopt_time);
5443789Sahrens 	}
5444789Sahrens 
5445789Sahrens 	/*
5446789Sahrens 	 * Create and initialize our storage pool.
5447789Sahrens 	 */
544810922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5449789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5450789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5451789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
545210922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
545310922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5454789Sahrens 	}
5455789Sahrens 
545610922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
545710922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
545810922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
545910922SJeff.Bonwick@Sun.COM 
546010922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5461789Sahrens 		zi = &zs->zs_info[f];
5462789Sahrens 		*zi = ztest_info[f];
546310922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
546410922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5465789Sahrens 		else
546610922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
546710922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5468789Sahrens 	}
5469789Sahrens 
5470789Sahrens 	/*
5471789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5472789Sahrens 	 * to verify that self-healing data works, and forced crashes
5473789Sahrens 	 * to verify that we never lose on-disk consistency.
5474789Sahrens 	 */
547510922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5476789Sahrens 		int status;
5477789Sahrens 		pid_t pid;
5478789Sahrens 
5479789Sahrens 		/*
5480789Sahrens 		 * Initialize the workload counters for each function.
5481789Sahrens 		 */
548210922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5483789Sahrens 			zi = &zs->zs_info[f];
548410922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5485789Sahrens 			zi->zi_call_time = 0;
5486789Sahrens 		}
5487789Sahrens 
54889480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
54899480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
54909480SGeorge.Wilson@Sun.COM 
5491789Sahrens 		pid = fork();
5492789Sahrens 
5493789Sahrens 		if (pid == -1)
5494789Sahrens 			fatal(1, "fork failed");
5495789Sahrens 
5496789Sahrens 		if (pid == 0) {	/* child */
5497789Sahrens 			struct rlimit rl = { 1024, 1024 };
5498789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
54991914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
550010922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5501789Sahrens 			exit(0);
5502789Sahrens 		}
5503789Sahrens 
55042856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5505789Sahrens 			continue;
5506789Sahrens 
5507789Sahrens 		if (WIFEXITED(status)) {
5508789Sahrens 			if (WEXITSTATUS(status) != 0) {
5509789Sahrens 				(void) fprintf(stderr,
5510789Sahrens 				    "child exited with code %d\n",
5511789Sahrens 				    WEXITSTATUS(status));
5512789Sahrens 				exit(2);
5513789Sahrens 			}
55142856Snd150628 		} else if (WIFSIGNALED(status)) {
5515789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5516789Sahrens 				(void) fprintf(stderr,
5517789Sahrens 				    "child died with signal %d\n",
5518789Sahrens 				    WTERMSIG(status));
5519789Sahrens 				exit(3);
5520789Sahrens 			}
5521789Sahrens 			kills++;
55222856Snd150628 		} else {
55232856Snd150628 			(void) fprintf(stderr, "something strange happened "
55242856Snd150628 			    "to child\n");
55252856Snd150628 			exit(4);
5526789Sahrens 		}
5527789Sahrens 
5528789Sahrens 		iters++;
5529789Sahrens 
5530789Sahrens 		if (zopt_verbose >= 1) {
5531789Sahrens 			hrtime_t now = gethrtime();
5532789Sahrens 
553310922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
553410922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5535789Sahrens 			nicenum(zs->zs_space, numbuf);
5536789Sahrens 
5537789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5538789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5539789Sahrens 			    iters,
5540789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5541789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5542789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5543789Sahrens 			    numbuf,
554410922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5545789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5546789Sahrens 		}
5547789Sahrens 
5548789Sahrens 		if (zopt_verbose >= 2) {
5549789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5550789Sahrens 			(void) printf("%7s %9s   %s\n",
5551789Sahrens 			    "Calls", "Time", "Function");
5552789Sahrens 			(void) printf("%7s %9s   %s\n",
5553789Sahrens 			    "-----", "----", "--------");
555410922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5555789Sahrens 				Dl_info dli;
5556789Sahrens 
5557789Sahrens 				zi = &zs->zs_info[f];
5558789Sahrens 				print_time(zi->zi_call_time, timebuf);
5559789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5560789Sahrens 				(void) printf("%7llu %9s   %s\n",
556110922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5562789Sahrens 				    dli.dli_sname);
5563789Sahrens 			}
5564789Sahrens 			(void) printf("\n");
5565789Sahrens 		}
5566789Sahrens 
5567789Sahrens 		/*
556810922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
556910922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
557010922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5571789Sahrens 		 */
557210922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
557310922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
557410922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
557510922SJeff.Bonwick@Sun.COM 		} else {
557610922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
557710922SJeff.Bonwick@Sun.COM 			kernel_fini();
557810922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
557910922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
558010922SJeff.Bonwick@Sun.COM 			    zopt_pool);
558110922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
558210922SJeff.Bonwick@Sun.COM 		}
5583789Sahrens 		kernel_fini();
558410922SJeff.Bonwick@Sun.COM 
558510922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5586789Sahrens 	}
5587789Sahrens 
5588789Sahrens 	if (zopt_verbose >= 1) {
5589789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5590789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5591789Sahrens 	}
5592789Sahrens 
5593789Sahrens 	return (0);
5594789Sahrens }
5595