xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 11818)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2211422SMark.Musante@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens /*
27789Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
28789Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
29789Sahrens  *
30789Sahrens  * The overall design of the ztest program is as follows:
31789Sahrens  *
32789Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
33789Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
34789Sahrens  *     we have a simple routine to test that functionality.  These
35789Sahrens  *     individual routines do not have to do anything "stressful".
36789Sahrens  *
37789Sahrens  * (2) We turn these simple functionality tests into a stress test by
38789Sahrens  *     running them all in parallel, with as many threads as desired,
39789Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
40789Sahrens  *
41789Sahrens  * (3) While all this is happening, we inject faults into the pool to
42789Sahrens  *     verify that self-healing data really works.
43789Sahrens  *
44789Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
45789Sahrens  *     functions.  Thus even individual objects vary from block to block
46789Sahrens  *     in which checksum they use and whether they're compressed.
47789Sahrens  *
48789Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
49789Sahrens  *     we run the entire test in a child of the main process.
50789Sahrens  *     At random times, the child self-immolates with a SIGKILL.
51789Sahrens  *     This is the software equivalent of pulling the power cord.
52789Sahrens  *     The parent then runs the test again, using the existing
53789Sahrens  *     storage pool, as many times as desired.
54789Sahrens  *
55789Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
56789Sahrens  *     many of the functional tests record the transaction group number
57789Sahrens  *     as part of their data.  When reading old data, they verify that
58789Sahrens  *     the transaction group number is less than the current, open txg.
59789Sahrens  *     If you add a new test, please do this if applicable.
60789Sahrens  *
61789Sahrens  * When run with no arguments, ztest runs for about five minutes and
62789Sahrens  * produces no output if successful.  To get a little bit of information,
63789Sahrens  * specify -V.  To get more information, specify -VV, and so on.
64789Sahrens  *
65789Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
66789Sahrens  *
67789Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68789Sahrens  * to increase the pool capacity, fanout, and overall stress level.
69789Sahrens  *
70789Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
71789Sahrens  * This can be useful when you're trying to distinguish temporal incursions
72789Sahrens  * from plain old race conditions.
73789Sahrens  */
74789Sahrens 
75789Sahrens #include <sys/zfs_context.h>
76789Sahrens #include <sys/spa.h>
77789Sahrens #include <sys/dmu.h>
78789Sahrens #include <sys/txg.h>
799412SAleksandr.Guzovskiy@Sun.COM #include <sys/dbuf.h>
80789Sahrens #include <sys/zap.h>
81789Sahrens #include <sys/dmu_objset.h>
82789Sahrens #include <sys/poll.h>
83789Sahrens #include <sys/stat.h>
84789Sahrens #include <sys/time.h>
85789Sahrens #include <sys/wait.h>
86789Sahrens #include <sys/mman.h>
87789Sahrens #include <sys/resource.h>
88789Sahrens #include <sys/zio.h>
89789Sahrens #include <sys/zil.h>
9010922SJeff.Bonwick@Sun.COM #include <sys/zil_impl.h>
91789Sahrens #include <sys/vdev_impl.h>
927754SJeff.Bonwick@Sun.COM #include <sys/vdev_file.h>
93789Sahrens #include <sys/spa_impl.h>
9410594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
95789Sahrens #include <sys/dsl_prop.h>
968779SMark.Musante@Sun.COM #include <sys/dsl_dataset.h>
97789Sahrens #include <sys/refcount.h>
98789Sahrens #include <stdio.h>
991914Scasper #include <stdio_ext.h>
100789Sahrens #include <stdlib.h>
101789Sahrens #include <unistd.h>
102789Sahrens #include <signal.h>
103789Sahrens #include <umem.h>
104789Sahrens #include <dlfcn.h>
105789Sahrens #include <ctype.h>
106789Sahrens #include <math.h>
107789Sahrens #include <sys/fs/zfs.h>
10810922SJeff.Bonwick@Sun.COM #include <libnvpair.h>
109789Sahrens 
110789Sahrens static char cmdname[] = "ztest";
111789Sahrens static char *zopt_pool = cmdname;
112789Sahrens 
113789Sahrens static uint64_t zopt_vdevs = 5;
114789Sahrens static uint64_t zopt_vdevtime;
1151732Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT;
116789Sahrens static int zopt_mirrors = 2;
117789Sahrens static int zopt_raidz = 4;
1182082Seschrock static int zopt_raidz_parity = 1;
119789Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE;
1201732Sbonwick static int zopt_datasets = 7;
121789Sahrens static int zopt_threads = 23;
122789Sahrens static uint64_t zopt_passtime = 60;	/* 60 seconds */
123789Sahrens static uint64_t zopt_killrate = 70;	/* 70% kill rate */
124789Sahrens static int zopt_verbose = 0;
125789Sahrens static int zopt_init = 1;
126789Sahrens static char *zopt_dir = "/tmp";
127789Sahrens static uint64_t zopt_time = 300;	/* 5 minutes */
128*11818SMark.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	},
2887754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
2897754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
2907754SJeff.Bonwick@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"
470*11818SMark.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"
477*11818SMark.Musante@Sun.COM 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
478*11818SMark.Musante@Sun.COM 	    "\t[-k kill_percentage (default: %llu%%)]\n"
479789Sahrens 	    "\t[-p pool_name (default: %s)]\n"
480*11818SMark.Musante@Sun.COM 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
481*11818SMark.Musante@Sun.COM 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
482*11818SMark.Musante@Sun.COM 	    "\t[-E] use existing pool instead of creating new one\n"
483*11818SMark.Musante@Sun.COM 	    "\t[-T time (default: %llu sec)] total run time\n"
484*11818SMark.Musante@Sun.COM 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
485*11818SMark.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 */
503*11818SMark.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,
518*11818SMark.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':
534*11818SMark.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;
589*11818SMark.Musante@Sun.COM 		case 'F':
590*11818SMark.Musante@Sun.COM 			zopt_maxloops = MAX(1, value);
591*11818SMark.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 
167710922SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db);
167810922SJeff.Bonwick@Sun.COM 
167910922SJeff.Bonwick@Sun.COM 		if (error == 0) {
168010922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
168110922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
168210922SJeff.Bonwick@Sun.COM 
168310922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
168410922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
168510922SJeff.Bonwick@Sun.COM 
168610922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
168710922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
168810922SJeff.Bonwick@Sun.COM 
168910922SJeff.Bonwick@Sun.COM 			if (error == 0)
169010922SJeff.Bonwick@Sun.COM 				return (0);
169110922SJeff.Bonwick@Sun.COM 		}
169210922SJeff.Bonwick@Sun.COM 	}
169310922SJeff.Bonwick@Sun.COM 
169410922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
169510922SJeff.Bonwick@Sun.COM 
169610922SJeff.Bonwick@Sun.COM 	return (error);
169710922SJeff.Bonwick@Sun.COM }
169810922SJeff.Bonwick@Sun.COM 
169910922SJeff.Bonwick@Sun.COM static void *
170010922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
170110922SJeff.Bonwick@Sun.COM {
170210922SJeff.Bonwick@Sun.COM 	char *lr;
170310922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
170410922SJeff.Bonwick@Sun.COM 
170510922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
170610922SJeff.Bonwick@Sun.COM 
170710922SJeff.Bonwick@Sun.COM 	if (name)
170810922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
170910922SJeff.Bonwick@Sun.COM 
171010922SJeff.Bonwick@Sun.COM 	return (lr);
171110922SJeff.Bonwick@Sun.COM }
171210922SJeff.Bonwick@Sun.COM 
171310922SJeff.Bonwick@Sun.COM void
171410922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
171510922SJeff.Bonwick@Sun.COM {
171610922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
171710922SJeff.Bonwick@Sun.COM 
171810922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
171910922SJeff.Bonwick@Sun.COM }
172010922SJeff.Bonwick@Sun.COM 
172110922SJeff.Bonwick@Sun.COM /*
172210922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
172310922SJeff.Bonwick@Sun.COM  */
172410922SJeff.Bonwick@Sun.COM static int
172510922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
172610922SJeff.Bonwick@Sun.COM {
172710922SJeff.Bonwick@Sun.COM 	int missing = 0;
172810922SJeff.Bonwick@Sun.COM 	int error;
172910922SJeff.Bonwick@Sun.COM 
173010922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
173110922SJeff.Bonwick@Sun.COM 
173210922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
173310922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
173410922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
173510922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
173610922SJeff.Bonwick@Sun.COM 		if (error) {
173710922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
173810922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
173910922SJeff.Bonwick@Sun.COM 			missing++;
174010922SJeff.Bonwick@Sun.COM 		} else {
174110922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
174210922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
174310922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
174410922SJeff.Bonwick@Sun.COM 
174510922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
174610922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
174710922SJeff.Bonwick@Sun.COM 
174810922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
174910922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
175010922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
175110922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
175210922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
175310922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
175410922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
175510922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
175610922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
175710922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
175810922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
175910922SJeff.Bonwick@Sun.COM 		}
176010922SJeff.Bonwick@Sun.COM 	}
176110922SJeff.Bonwick@Sun.COM 
176210922SJeff.Bonwick@Sun.COM 	return (missing);
176310922SJeff.Bonwick@Sun.COM }
176410922SJeff.Bonwick@Sun.COM 
176510922SJeff.Bonwick@Sun.COM static int
176610922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
176710922SJeff.Bonwick@Sun.COM {
176810922SJeff.Bonwick@Sun.COM 	int missing = 0;
176910922SJeff.Bonwick@Sun.COM 
177010922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
177110922SJeff.Bonwick@Sun.COM 
177210922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
177310922SJeff.Bonwick@Sun.COM 		if (missing) {
177410922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
177510922SJeff.Bonwick@Sun.COM 			missing++;
177610922SJeff.Bonwick@Sun.COM 			continue;
177710922SJeff.Bonwick@Sun.COM 		}
177810922SJeff.Bonwick@Sun.COM 
177910922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
178010922SJeff.Bonwick@Sun.COM 
178110922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
178210922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
178310922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
178410922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
178510922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
178610922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
178710922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
178810922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
178910922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
179010922SJeff.Bonwick@Sun.COM 
179110922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
179210922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
179310922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
179410922SJeff.Bonwick@Sun.COM 			missing++;
179510922SJeff.Bonwick@Sun.COM 		} else {
179610922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
179710922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
179810922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
179910922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
180010922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
180110922SJeff.Bonwick@Sun.COM 		}
180210922SJeff.Bonwick@Sun.COM 
180310922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
180410922SJeff.Bonwick@Sun.COM 	}
180510922SJeff.Bonwick@Sun.COM 
180610922SJeff.Bonwick@Sun.COM 	return (missing);
180710922SJeff.Bonwick@Sun.COM }
180810922SJeff.Bonwick@Sun.COM 
180910922SJeff.Bonwick@Sun.COM static int
181010922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
181110922SJeff.Bonwick@Sun.COM {
181210922SJeff.Bonwick@Sun.COM 	int missing = 0;
181310922SJeff.Bonwick@Sun.COM 	int error;
181410922SJeff.Bonwick@Sun.COM 
181510922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
181610922SJeff.Bonwick@Sun.COM 
181710922SJeff.Bonwick@Sun.COM 	od += count - 1;
181810922SJeff.Bonwick@Sun.COM 
181910922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
182010922SJeff.Bonwick@Sun.COM 		if (missing) {
182110922SJeff.Bonwick@Sun.COM 			missing++;
182210922SJeff.Bonwick@Sun.COM 			continue;
182310922SJeff.Bonwick@Sun.COM 		}
182410922SJeff.Bonwick@Sun.COM 
182510922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
182610922SJeff.Bonwick@Sun.COM 			continue;
182710922SJeff.Bonwick@Sun.COM 
182810922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
182910922SJeff.Bonwick@Sun.COM 
183010922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
183110922SJeff.Bonwick@Sun.COM 
183210922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
183310922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
183410922SJeff.Bonwick@Sun.COM 			missing++;
183510922SJeff.Bonwick@Sun.COM 		} else {
183610922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
183710922SJeff.Bonwick@Sun.COM 		}
183810922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
183910922SJeff.Bonwick@Sun.COM 	}
184010922SJeff.Bonwick@Sun.COM 
184110922SJeff.Bonwick@Sun.COM 	return (missing);
184210922SJeff.Bonwick@Sun.COM }
184310922SJeff.Bonwick@Sun.COM 
184410922SJeff.Bonwick@Sun.COM static int
184510922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
184610922SJeff.Bonwick@Sun.COM     void *data)
184710922SJeff.Bonwick@Sun.COM {
184810922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
184910922SJeff.Bonwick@Sun.COM 	int error;
185010922SJeff.Bonwick@Sun.COM 
185110922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
185210922SJeff.Bonwick@Sun.COM 
185310922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
185410922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
185510922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
185610922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
185710922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
185810922SJeff.Bonwick@Sun.COM 
185910922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
186010922SJeff.Bonwick@Sun.COM 
186110922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
186210922SJeff.Bonwick@Sun.COM 
186310922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
186410922SJeff.Bonwick@Sun.COM 
186510922SJeff.Bonwick@Sun.COM 	return (error);
186610922SJeff.Bonwick@Sun.COM }
186710922SJeff.Bonwick@Sun.COM 
186810922SJeff.Bonwick@Sun.COM static int
186910922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
187010922SJeff.Bonwick@Sun.COM {
187110922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
187210922SJeff.Bonwick@Sun.COM 	int error;
187310922SJeff.Bonwick@Sun.COM 
187410922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
187510922SJeff.Bonwick@Sun.COM 
187610922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
187710922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
187810922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
187910922SJeff.Bonwick@Sun.COM 
188010922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
188110922SJeff.Bonwick@Sun.COM 
188210922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
188310922SJeff.Bonwick@Sun.COM 
188410922SJeff.Bonwick@Sun.COM 	return (error);
188510922SJeff.Bonwick@Sun.COM }
188610922SJeff.Bonwick@Sun.COM 
188710922SJeff.Bonwick@Sun.COM static int
188810922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
188910922SJeff.Bonwick@Sun.COM {
189010922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
189110922SJeff.Bonwick@Sun.COM 	int error;
189210922SJeff.Bonwick@Sun.COM 
189310922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
189410922SJeff.Bonwick@Sun.COM 
189510922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
189610922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
189710922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
189810922SJeff.Bonwick@Sun.COM 
189910922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
190010922SJeff.Bonwick@Sun.COM 
190110922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
190210922SJeff.Bonwick@Sun.COM 
190310922SJeff.Bonwick@Sun.COM 	return (error);
190410922SJeff.Bonwick@Sun.COM }
190510922SJeff.Bonwick@Sun.COM 
190610922SJeff.Bonwick@Sun.COM static void
190710922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
190810922SJeff.Bonwick@Sun.COM {
190910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
191010922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
191110922SJeff.Bonwick@Sun.COM 	uint64_t txg;
191210922SJeff.Bonwick@Sun.COM 	rl_t *rl;
191310922SJeff.Bonwick@Sun.COM 
191410922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
191510922SJeff.Bonwick@Sun.COM 
191610922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
191710922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
191810922SJeff.Bonwick@Sun.COM 
191910922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
192010922SJeff.Bonwick@Sun.COM 
192110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
192210922SJeff.Bonwick@Sun.COM 
192310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
192410922SJeff.Bonwick@Sun.COM 
192510922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
192610922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
192710922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
192810922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
192910922SJeff.Bonwick@Sun.COM 	} else {
193010922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
193110922SJeff.Bonwick@Sun.COM 	}
193210922SJeff.Bonwick@Sun.COM 
193310922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
193410922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
193510922SJeff.Bonwick@Sun.COM }
193610922SJeff.Bonwick@Sun.COM 
193710922SJeff.Bonwick@Sun.COM static void
193810922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
193910922SJeff.Bonwick@Sun.COM {
194010922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
194110922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
194210922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
194310922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
194410922SJeff.Bonwick@Sun.COM 	void *data;
194510922SJeff.Bonwick@Sun.COM 
194610922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
194710922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
194810922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
194910922SJeff.Bonwick@Sun.COM 
195010922SJeff.Bonwick@Sun.COM 	/*
195110922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
195210922SJeff.Bonwick@Sun.COM 	 */
195310922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
195410922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
195510922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
195610922SJeff.Bonwick@Sun.COM 
195710922SJeff.Bonwick@Sun.COM 	switch (io_type) {
195810922SJeff.Bonwick@Sun.COM 
195910922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
196010922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
196110922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
196210922SJeff.Bonwick@Sun.COM 		break;
196310922SJeff.Bonwick@Sun.COM 
196410922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
196510922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
196610922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
196710922SJeff.Bonwick@Sun.COM 			/*
196810922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
196910922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
197010922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
197110922SJeff.Bonwick@Sun.COM 			 */
197210922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
197310922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
197410922SJeff.Bonwick@Sun.COM 		}
197510922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
197610922SJeff.Bonwick@Sun.COM 		break;
197710922SJeff.Bonwick@Sun.COM 
197810922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
197910922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
198010922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
198110922SJeff.Bonwick@Sun.COM 		break;
198210922SJeff.Bonwick@Sun.COM 
198310922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
198410922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
198510922SJeff.Bonwick@Sun.COM 		break;
198610922SJeff.Bonwick@Sun.COM 
198710922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
198810922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
198910922SJeff.Bonwick@Sun.COM 		break;
199010922SJeff.Bonwick@Sun.COM 	}
199110922SJeff.Bonwick@Sun.COM 
199210922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
199310922SJeff.Bonwick@Sun.COM }
199410922SJeff.Bonwick@Sun.COM 
199510922SJeff.Bonwick@Sun.COM /*
199610922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
199710922SJeff.Bonwick@Sun.COM  */
199810922SJeff.Bonwick@Sun.COM static void
199910922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
200010922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
200110922SJeff.Bonwick@Sun.COM {
200210922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
200310922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
200410922SJeff.Bonwick@Sun.COM 
200510922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
200610922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
200710922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
200810922SJeff.Bonwick@Sun.COM 
200910922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
201010922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
201110922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
201210922SJeff.Bonwick@Sun.COM 
201310922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
201410922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
201510922SJeff.Bonwick@Sun.COM }
201610922SJeff.Bonwick@Sun.COM 
201710922SJeff.Bonwick@Sun.COM /*
201810922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
201910922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
202010922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
202110922SJeff.Bonwick@Sun.COM  * use the existing objects.
202210922SJeff.Bonwick@Sun.COM  */
202310922SJeff.Bonwick@Sun.COM static int
202410922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
202510922SJeff.Bonwick@Sun.COM {
202610922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
202710922SJeff.Bonwick@Sun.COM 	int rv = 0;
202810922SJeff.Bonwick@Sun.COM 
202910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
203010922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
203110922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
203210922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
203310922SJeff.Bonwick@Sun.COM 		rv = -1;
203410922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
203510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
203610922SJeff.Bonwick@Sun.COM 
203710922SJeff.Bonwick@Sun.COM 	return (rv);
203810922SJeff.Bonwick@Sun.COM }
203910922SJeff.Bonwick@Sun.COM 
204010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
204110922SJeff.Bonwick@Sun.COM void
204210922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
204310922SJeff.Bonwick@Sun.COM {
204410922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
204510922SJeff.Bonwick@Sun.COM 
204610922SJeff.Bonwick@Sun.COM 	zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS));
204710922SJeff.Bonwick@Sun.COM 
204810922SJeff.Bonwick@Sun.COM 	/*
204910922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
205010922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
205110922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
205210922SJeff.Bonwick@Sun.COM 	 */
205310922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
205410922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
205510922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
205610922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
205710922SJeff.Bonwick@Sun.COM }
205810922SJeff.Bonwick@Sun.COM 
205910922SJeff.Bonwick@Sun.COM /*
2060789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2061789Sahrens  * or create a pool with a bad vdev spec.
2062789Sahrens  */
206310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2064789Sahrens void
206510922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2066789Sahrens {
206710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2068789Sahrens 	spa_t *spa;
2069789Sahrens 	nvlist_t *nvroot;
2070789Sahrens 
2071789Sahrens 	/*
2072789Sahrens 	 * Attempt to create using a bad file.
2073789Sahrens 	 */
20747754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
207510922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
207610922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2077789Sahrens 	nvlist_free(nvroot);
2078789Sahrens 
2079789Sahrens 	/*
2080789Sahrens 	 * Attempt to create using a bad mirror.
2081789Sahrens 	 */
20827754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
208310922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
208410922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2085789Sahrens 	nvlist_free(nvroot);
2086789Sahrens 
2087789Sahrens 	/*
2088789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2089789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2090789Sahrens 	 */
209110922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20927754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
209310922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2094789Sahrens 	nvlist_free(nvroot);
209510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
209610922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2097789Sahrens 	spa_close(spa, FTAG);
209810922SJeff.Bonwick@Sun.COM 
209910922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2100789Sahrens }
2101789Sahrens 
21027768SJeff.Bonwick@Sun.COM static vdev_t *
21037768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
21047768SJeff.Bonwick@Sun.COM {
21057768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
21067768SJeff.Bonwick@Sun.COM 
21077768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
21087768SJeff.Bonwick@Sun.COM 		return (vd);
21097768SJeff.Bonwick@Sun.COM 
21107768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
21117768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
21127768SJeff.Bonwick@Sun.COM 		    NULL)
21137768SJeff.Bonwick@Sun.COM 			return (mvd);
21147768SJeff.Bonwick@Sun.COM 
21157768SJeff.Bonwick@Sun.COM 	return (NULL);
21167768SJeff.Bonwick@Sun.COM }
21177768SJeff.Bonwick@Sun.COM 
2118789Sahrens /*
211910594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
212010594SGeorge.Wilson@Sun.COM  */
212110594SGeorge.Wilson@Sun.COM int
212210594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
212310594SGeorge.Wilson@Sun.COM {
212410594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
212510594SGeorge.Wilson@Sun.COM 	int c;
212610594SGeorge.Wilson@Sun.COM 
212710594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
212810594SGeorge.Wilson@Sun.COM 
212910594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
213010594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
213110594SGeorge.Wilson@Sun.COM 
213210594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
213310594SGeorge.Wilson@Sun.COM 			break;
213410594SGeorge.Wilson@Sun.COM 	}
213510594SGeorge.Wilson@Sun.COM 	return (c);
213610594SGeorge.Wilson@Sun.COM }
213710594SGeorge.Wilson@Sun.COM 
213810594SGeorge.Wilson@Sun.COM /*
2139789Sahrens  * Verify that vdev_add() works as expected.
2140789Sahrens  */
214110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2142789Sahrens void
214310922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2144789Sahrens {
214510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
214610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
214711422SMark.Musante@Sun.COM 	uint64_t leaves;
214810594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2149789Sahrens 	nvlist_t *nvroot;
2150789Sahrens 	int error;
2151789Sahrens 
215210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
215311422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2154789Sahrens 
21557754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2156789Sahrens 
215710594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2158789Sahrens 
21594527Sperrin 	/*
216010594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21614527Sperrin 	 */
216210594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
216310594SGeorge.Wilson@Sun.COM 		/*
216410594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
216510594SGeorge.Wilson@Sun.COM 		 */
216610922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
216710594SGeorge.Wilson@Sun.COM 
216810594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
216910594SGeorge.Wilson@Sun.COM 
217010594SGeorge.Wilson@Sun.COM 		/*
217110594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
217210594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
217310594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
217410594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
217510594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
217610594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
217710594SGeorge.Wilson@Sun.COM 		 */
217810922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
217910594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
218010922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
218110594SGeorge.Wilson@Sun.COM 
218210594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
218310594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
218410594SGeorge.Wilson@Sun.COM 	} else {
218510594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
218610594SGeorge.Wilson@Sun.COM 
218710594SGeorge.Wilson@Sun.COM 		/*
218810594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
218910594SGeorge.Wilson@Sun.COM 		 */
219010594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
219111422SMark.Musante@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
219210594SGeorge.Wilson@Sun.COM 
219310594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
219410594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
219510594SGeorge.Wilson@Sun.COM 
219610594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
219710594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
219810594SGeorge.Wilson@Sun.COM 		else if (error != 0)
219910594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
220010594SGeorge.Wilson@Sun.COM 	}
2201789Sahrens 
220210922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
22037754SJeff.Bonwick@Sun.COM }
22047754SJeff.Bonwick@Sun.COM 
22057754SJeff.Bonwick@Sun.COM /*
22067754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
22077754SJeff.Bonwick@Sun.COM  */
220810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
22097754SJeff.Bonwick@Sun.COM void
221010922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
22117754SJeff.Bonwick@Sun.COM {
221210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
221310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22147768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22157754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22167754SJeff.Bonwick@Sun.COM 	char *aux;
22177754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22187754SJeff.Bonwick@Sun.COM 	int error;
22197754SJeff.Bonwick@Sun.COM 
22207768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22217754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22227754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22237754SJeff.Bonwick@Sun.COM 	} else {
22247754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22257754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22267754SJeff.Bonwick@Sun.COM 	}
22277754SJeff.Bonwick@Sun.COM 
222810922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22297754SJeff.Bonwick@Sun.COM 
22307754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22317754SJeff.Bonwick@Sun.COM 
22327754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22337754SJeff.Bonwick@Sun.COM 		/*
22347754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22357754SJeff.Bonwick@Sun.COM 		 */
22367754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22377754SJeff.Bonwick@Sun.COM 	} else {
22387754SJeff.Bonwick@Sun.COM 		/*
22397754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22407754SJeff.Bonwick@Sun.COM 		 */
224110922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22427754SJeff.Bonwick@Sun.COM 		for (;;) {
22437754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22447754SJeff.Bonwick@Sun.COM 			int c;
22457754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
224610922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22477754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22487754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22497754SJeff.Bonwick@Sun.COM 				    path) == 0)
22507754SJeff.Bonwick@Sun.COM 					break;
22517768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22527768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22537754SJeff.Bonwick@Sun.COM 				break;
225410922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22557754SJeff.Bonwick@Sun.COM 		}
22567754SJeff.Bonwick@Sun.COM 	}
22577754SJeff.Bonwick@Sun.COM 
22587754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22597754SJeff.Bonwick@Sun.COM 
22607754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22617754SJeff.Bonwick@Sun.COM 		/*
22627754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22637754SJeff.Bonwick@Sun.COM 		 */
22647768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22657768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22667754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22677754SJeff.Bonwick@Sun.COM 		if (error != 0)
22687754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22697754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22707754SJeff.Bonwick@Sun.COM 	} else {
22717754SJeff.Bonwick@Sun.COM 		/*
22727754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22737754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22747754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22757754SJeff.Bonwick@Sun.COM 		 */
22767754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22779816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22787754SJeff.Bonwick@Sun.COM 
22797754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22807754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22817754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22827754SJeff.Bonwick@Sun.COM 	}
22837754SJeff.Bonwick@Sun.COM 
228410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2285789Sahrens }
2286789Sahrens 
2287789Sahrens /*
228811422SMark.Musante@Sun.COM  * split a pool if it has mirror tlvdevs
228911422SMark.Musante@Sun.COM  */
229011422SMark.Musante@Sun.COM /* ARGSUSED */
229111422SMark.Musante@Sun.COM void
229211422SMark.Musante@Sun.COM ztest_split_pool(ztest_ds_t *zd, uint64_t id)
229311422SMark.Musante@Sun.COM {
229411422SMark.Musante@Sun.COM 	ztest_shared_t *zs = ztest_shared;
229511422SMark.Musante@Sun.COM 	spa_t *spa = zs->zs_spa;
229611422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
229711422SMark.Musante@Sun.COM 	nvlist_t *tree, **child, *config, *split, **schild;
229811422SMark.Musante@Sun.COM 	uint_t c, children, schildren = 0, lastlogid = 0;
229911422SMark.Musante@Sun.COM 	int error = 0;
230011422SMark.Musante@Sun.COM 
230111422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
230211422SMark.Musante@Sun.COM 
230311422SMark.Musante@Sun.COM 	/* ensure we have a useable config; mirrors of raidz aren't supported */
230411422SMark.Musante@Sun.COM 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
230511422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
230611422SMark.Musante@Sun.COM 		return;
230711422SMark.Musante@Sun.COM 	}
230811422SMark.Musante@Sun.COM 
230911422SMark.Musante@Sun.COM 	/* clean up the old pool, if any */
231011422SMark.Musante@Sun.COM 	(void) spa_destroy("splitp");
231111422SMark.Musante@Sun.COM 
231211422SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
231311422SMark.Musante@Sun.COM 
231411422SMark.Musante@Sun.COM 	/* generate a config from the existing config */
231511422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
231611422SMark.Musante@Sun.COM 	    &tree) == 0);
231711422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
231811422SMark.Musante@Sun.COM 	    &children) == 0);
231911422SMark.Musante@Sun.COM 
232011422SMark.Musante@Sun.COM 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
232111422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
232211422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
232311422SMark.Musante@Sun.COM 		nvlist_t **mchild;
232411422SMark.Musante@Sun.COM 		uint_t mchildren;
232511422SMark.Musante@Sun.COM 
232611422SMark.Musante@Sun.COM 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
232711422SMark.Musante@Sun.COM 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
232811422SMark.Musante@Sun.COM 			    0) == 0);
232911422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_string(schild[schildren],
233011422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
233111422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(schild[schildren],
233211422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
233311422SMark.Musante@Sun.COM 			if (lastlogid == 0)
233411422SMark.Musante@Sun.COM 				lastlogid = schildren;
233511422SMark.Musante@Sun.COM 			++schildren;
233611422SMark.Musante@Sun.COM 			continue;
233711422SMark.Musante@Sun.COM 		}
233811422SMark.Musante@Sun.COM 		lastlogid = 0;
233911422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist_array(child[c],
234011422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
234111422SMark.Musante@Sun.COM 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
234211422SMark.Musante@Sun.COM 	}
234311422SMark.Musante@Sun.COM 
234411422SMark.Musante@Sun.COM 	/* OK, create a config that can be used to split */
234511422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
234611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
234711422SMark.Musante@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
234811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
234911422SMark.Musante@Sun.COM 	    lastlogid != 0 ? lastlogid : schildren) == 0);
235011422SMark.Musante@Sun.COM 
235111422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
235211422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
235311422SMark.Musante@Sun.COM 
235411422SMark.Musante@Sun.COM 	for (c = 0; c < schildren; c++)
235511422SMark.Musante@Sun.COM 		nvlist_free(schild[c]);
235611422SMark.Musante@Sun.COM 	free(schild);
235711422SMark.Musante@Sun.COM 	nvlist_free(split);
235811422SMark.Musante@Sun.COM 
235911422SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
236011422SMark.Musante@Sun.COM 
236111422SMark.Musante@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
236211422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
236311422SMark.Musante@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
236411422SMark.Musante@Sun.COM 
236511422SMark.Musante@Sun.COM 	nvlist_free(config);
236611422SMark.Musante@Sun.COM 
236711422SMark.Musante@Sun.COM 	if (error == 0) {
236811422SMark.Musante@Sun.COM 		(void) printf("successful split - results:\n");
236911422SMark.Musante@Sun.COM 		mutex_enter(&spa_namespace_lock);
237011422SMark.Musante@Sun.COM 		show_pool_stats(spa);
237111422SMark.Musante@Sun.COM 		show_pool_stats(spa_lookup("splitp"));
237211422SMark.Musante@Sun.COM 		mutex_exit(&spa_namespace_lock);
237311422SMark.Musante@Sun.COM 		++zs->zs_splits;
237411422SMark.Musante@Sun.COM 		--zs->zs_mirrors;
237511422SMark.Musante@Sun.COM 	}
237611422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
237711422SMark.Musante@Sun.COM 
237811422SMark.Musante@Sun.COM }
237911422SMark.Musante@Sun.COM 
238011422SMark.Musante@Sun.COM /*
2381789Sahrens  * Verify that we can attach and detach devices.
2382789Sahrens  */
238310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2384789Sahrens void
238510922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2386789Sahrens {
238710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
238810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
23897768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2390789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23911544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
23927754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
239311422SMark.Musante@Sun.COM 	uint64_t leaves;
2394789Sahrens 	uint64_t leaf, top;
23951732Sbonwick 	uint64_t ashift = ztest_get_ashift();
23968241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
23971544Seschrock 	size_t oldsize, newsize;
23981544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2399789Sahrens 	int replacing;
24007793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
24017768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
24027768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2403789Sahrens 	int error, expected_error;
2404789Sahrens 
240510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
240611422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2407789Sahrens 
24087754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2409789Sahrens 
2410789Sahrens 	/*
2411789Sahrens 	 * Decide whether to do an attach or a replace.
2412789Sahrens 	 */
2413789Sahrens 	replacing = ztest_random(2);
2414789Sahrens 
2415789Sahrens 	/*
2416789Sahrens 	 * Pick a random top-level vdev.
2417789Sahrens 	 */
241810922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2419789Sahrens 
2420789Sahrens 	/*
2421789Sahrens 	 * Pick a random leaf within it.
2422789Sahrens 	 */
242311497SMark.Musante@Sun.COM 	leaf = ztest_random(leaves);
2424789Sahrens 
2425789Sahrens 	/*
24267768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2427789Sahrens 	 */
24287768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
242911422SMark.Musante@Sun.COM 	if (zs->zs_mirrors >= 1) {
24308241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
243111422SMark.Musante@Sun.COM 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
24327768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
24338241SJeff.Bonwick@Sun.COM 	}
24348241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
24358241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
24368241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
24377768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
24388241SJeff.Bonwick@Sun.COM 	}
2439789Sahrens 
2440789Sahrens 	/*
24417768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
24427768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2443789Sahrens 	 */
24447768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
24457793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
24468241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
24478241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
24487768SJeff.Bonwick@Sun.COM 	}
24497768SJeff.Bonwick@Sun.COM 
24507768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
24519816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
24527768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
24537768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
24541544Seschrock 	pvd = oldvd->vdev_parent;
24558241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2456789Sahrens 
2457789Sahrens 	/*
24587793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
24597793SJeff.Bonwick@Sun.COM 	 */
24607793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
24617793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
24628241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
24638241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
24648241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
24658241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
246610922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
24677793SJeff.Bonwick@Sun.COM 		return;
24687793SJeff.Bonwick@Sun.COM 	}
24697793SJeff.Bonwick@Sun.COM 
24707793SJeff.Bonwick@Sun.COM 	/*
24717768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
24727768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2473789Sahrens 	 */
24747768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
24757768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
24767768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
24777768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
24787768SJeff.Bonwick@Sun.COM 	} else {
24797768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
24807768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
24817768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
24827768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
24837768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
24847768SJeff.Bonwick@Sun.COM 	}
24857768SJeff.Bonwick@Sun.COM 
24867768SJeff.Bonwick@Sun.COM 	if (newvd) {
24879816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
24887768SJeff.Bonwick@Sun.COM 	} else {
24897768SJeff.Bonwick@Sun.COM 		/*
24907768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
24917768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
24927768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
24937768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
24947768SJeff.Bonwick@Sun.COM 		 */
24957768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
24967768SJeff.Bonwick@Sun.COM 	}
2497789Sahrens 
2498789Sahrens 	/*
2499789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2500789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2501789Sahrens 	 *
25021544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2503789Sahrens 	 *
25041544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2505789Sahrens 	 */
25067768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
25077768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
25087768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
25097768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
25107768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25117768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
25127768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25137768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
25147768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
25157768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
25162174Seschrock 		expected_error = EBUSY;
25171544Seschrock 	else if (newsize < oldsize)
2518789Sahrens 		expected_error = EOVERFLOW;
25191732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
25201732Sbonwick 		expected_error = EDOM;
2521789Sahrens 	else
2522789Sahrens 		expected_error = 0;
2523789Sahrens 
25247754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2525789Sahrens 
2526789Sahrens 	/*
25271544Seschrock 	 * Build the nvlist describing newpath.
2528789Sahrens 	 */
25297754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
25307754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2531789Sahrens 
25327768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2533789Sahrens 
2534789Sahrens 	nvlist_free(root);
2535789Sahrens 
2536789Sahrens 	/*
2537789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2538789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2539789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2540789Sahrens 	 */
2541789Sahrens 	if (expected_error == ENOTSUP &&
2542789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2543789Sahrens 		expected_error = error;
2544789Sahrens 
2545797Sbonwick 	/*
2546797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2547797Sbonwick 	 */
25487046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2549797Sbonwick 		expected_error = error;
2550797Sbonwick 
25517046Sahrens 	/* XXX workaround 6690467 */
25527046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
25537046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
25547046Sahrens 		    "returned %d, expected %d",
25557046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
25567046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2557789Sahrens 	}
2558789Sahrens 
255910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2560789Sahrens }
2561789Sahrens 
2562789Sahrens /*
25639816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
25649816SGeorge.Wilson@Sun.COM  */
25659816SGeorge.Wilson@Sun.COM vdev_t *
25669816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
25679816SGeorge.Wilson@Sun.COM {
25689816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25699816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
25709816SGeorge.Wilson@Sun.COM 	size_t fsize;
25719816SGeorge.Wilson@Sun.COM 	int fd;
25729816SGeorge.Wilson@Sun.COM 
25739816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25749816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25759816SGeorge.Wilson@Sun.COM 
25769816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
25779816SGeorge.Wilson@Sun.COM 		return (vd);
25789816SGeorge.Wilson@Sun.COM 
25799816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
25809816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
25819816SGeorge.Wilson@Sun.COM 
25829816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
25839816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
25849816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
25859816SGeorge.Wilson@Sun.COM 	}
25869816SGeorge.Wilson@Sun.COM 	(void) close(fd);
25879816SGeorge.Wilson@Sun.COM 	return (NULL);
25889816SGeorge.Wilson@Sun.COM }
25899816SGeorge.Wilson@Sun.COM 
25909816SGeorge.Wilson@Sun.COM /*
25919816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
25929816SGeorge.Wilson@Sun.COM  */
25939816SGeorge.Wilson@Sun.COM /* ARGSUSED */
25949816SGeorge.Wilson@Sun.COM vdev_t *
25959816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
25969816SGeorge.Wilson@Sun.COM {
25979816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25989816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
25999816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
260010685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
260110850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
260210850SGeorge.Wilson@Sun.COM 	int error;
26039816SGeorge.Wilson@Sun.COM 
26049816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
26059816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
26069816SGeorge.Wilson@Sun.COM 
26079816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
26089816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
260910850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
26109816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26119816SGeorge.Wilson@Sun.COM 
26129816SGeorge.Wilson@Sun.COM 	/*
261310850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
261410850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
261510850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
261610850SGeorge.Wilson@Sun.COM 	 */
261710850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
261810850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
261910850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
262010850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
262110850SGeorge.Wilson@Sun.COM 		}
262210850SGeorge.Wilson@Sun.COM 		return (vd);
262310850SGeorge.Wilson@Sun.COM 	}
262410850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
262510850SGeorge.Wilson@Sun.COM 
262610850SGeorge.Wilson@Sun.COM 	/*
26279816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
26289816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
26299816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
26309816SGeorge.Wilson@Sun.COM 	 * trying to online it.
26319816SGeorge.Wilson@Sun.COM 	 */
263210685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
263310685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
263410685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
263510685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
263610922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
263710922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
263810685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
263910685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
264010685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
26419816SGeorge.Wilson@Sun.COM 		}
26429816SGeorge.Wilson@Sun.COM 		return (vd);
26439816SGeorge.Wilson@Sun.COM 	}
26449816SGeorge.Wilson@Sun.COM 	return (NULL);
26459816SGeorge.Wilson@Sun.COM }
26469816SGeorge.Wilson@Sun.COM 
26479816SGeorge.Wilson@Sun.COM /*
26489816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
26499816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
26509816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
26519816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
26529816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
26539816SGeorge.Wilson@Sun.COM  */
26549816SGeorge.Wilson@Sun.COM vdev_t *
26559816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
26569816SGeorge.Wilson@Sun.COM {
26579816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26589816SGeorge.Wilson@Sun.COM 		if (func == NULL)
26599816SGeorge.Wilson@Sun.COM 			return (vd);
26609816SGeorge.Wilson@Sun.COM 		else
26619816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
26629816SGeorge.Wilson@Sun.COM 	}
26639816SGeorge.Wilson@Sun.COM 
26649816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
26659816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
26669816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
26679816SGeorge.Wilson@Sun.COM 			return (cvd);
26689816SGeorge.Wilson@Sun.COM 	}
26699816SGeorge.Wilson@Sun.COM 	return (NULL);
26709816SGeorge.Wilson@Sun.COM }
26719816SGeorge.Wilson@Sun.COM 
26729816SGeorge.Wilson@Sun.COM /*
2673789Sahrens  * Verify that dynamic LUN growth works as expected.
2674789Sahrens  */
267510922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2676789Sahrens void
267710922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2678789Sahrens {
267910922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
268010922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
268110922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
268210922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
268310922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
26849816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
268510922SJeff.Bonwick@Sun.COM 	uint64_t top;
268610922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
268710922SJeff.Bonwick@Sun.COM 
268810922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
26899816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26909816SGeorge.Wilson@Sun.COM 
269110922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
269210922SJeff.Bonwick@Sun.COM 
269310922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
269410922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
269510922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
269610922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
269710922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
26989816SGeorge.Wilson@Sun.COM 
26999816SGeorge.Wilson@Sun.COM 	/*
27009816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
27019816SGeorge.Wilson@Sun.COM 	 * our top-level device.
27029816SGeorge.Wilson@Sun.COM 	 */
27039816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
27049816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
27059816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
27069816SGeorge.Wilson@Sun.COM 
27079816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
27089816SGeorge.Wilson@Sun.COM 
27099816SGeorge.Wilson@Sun.COM 	/*
271010685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
271110685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
27129816SGeorge.Wilson@Sun.COM 	 */
271310685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
271410685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
27159816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
271610922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27179816SGeorge.Wilson@Sun.COM 		return;
27189816SGeorge.Wilson@Sun.COM 	}
27199816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
27209816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
27219816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
27229816SGeorge.Wilson@Sun.COM 
272310922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
272410922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
27259816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
27269816SGeorge.Wilson@Sun.COM 	}
27279816SGeorge.Wilson@Sun.COM 
2728789Sahrens 	/*
27299816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
27309816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
27319816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
27329816SGeorge.Wilson@Sun.COM 	 */
27339816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
27349816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
27359816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
27369816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
27379816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
273810685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
27399816SGeorge.Wilson@Sun.COM 		}
274010922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
274110922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27429816SGeorge.Wilson@Sun.COM 		return;
27439816SGeorge.Wilson@Sun.COM 	}
27449816SGeorge.Wilson@Sun.COM 
274510922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
27469816SGeorge.Wilson@Sun.COM 
27479816SGeorge.Wilson@Sun.COM 	/*
27489816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
27499816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
27509816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2751789Sahrens 	 */
275210922SJeff.Bonwick@Sun.COM 	for (;;) {
275310922SJeff.Bonwick@Sun.COM 		boolean_t done;
275410922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
275510922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
275610922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
275710922SJeff.Bonwick@Sun.COM 		if (done)
275810922SJeff.Bonwick@Sun.COM 			break;
275910922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
276010922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
276110922SJeff.Bonwick@Sun.COM 	}
27629816SGeorge.Wilson@Sun.COM 
27639816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
276410922SJeff.Bonwick@Sun.COM 
276510922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
276610922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
276710922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
276810922SJeff.Bonwick@Sun.COM 
276910922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
277010922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
277110922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
277210922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
277310922SJeff.Bonwick@Sun.COM 		}
277410922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
277510922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
277610922SJeff.Bonwick@Sun.COM 		return;
277710922SJeff.Bonwick@Sun.COM 	}
277810922SJeff.Bonwick@Sun.COM 
277910922SJeff.Bonwick@Sun.COM 	/*
278010922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
278110922SJeff.Bonwick@Sun.COM 	 */
278210922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
278310922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
278410922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
27859816SGeorge.Wilson@Sun.COM 
27869816SGeorge.Wilson@Sun.COM 	/*
27879816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
27889816SGeorge.Wilson@Sun.COM 	 */
278910922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
279010922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
279110922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
279210922SJeff.Bonwick@Sun.COM 
279310922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
27949816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
27959816SGeorge.Wilson@Sun.COM 
279610922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
279710922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
27989816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
27999816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2800789Sahrens 	}
280110922SJeff.Bonwick@Sun.COM 
28029816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
280310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
280410922SJeff.Bonwick@Sun.COM }
280510922SJeff.Bonwick@Sun.COM 
280610922SJeff.Bonwick@Sun.COM /*
280710922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
280810922SJeff.Bonwick@Sun.COM  */
280910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
281010922SJeff.Bonwick@Sun.COM static void
281110922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
281210922SJeff.Bonwick@Sun.COM {
281310922SJeff.Bonwick@Sun.COM 	/*
281410922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
281510922SJeff.Bonwick@Sun.COM 	 */
281610922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
281710922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2818789Sahrens }
2819789Sahrens 
2820789Sahrens /* ARGSUSED */
282110922SJeff.Bonwick@Sun.COM static int
282211209SMatthew.Ahrens@Sun.COM ztest_objset_destroy_cb(const char *name, void *arg)
2823789Sahrens {
2824789Sahrens 	objset_t *os;
282510922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2826789Sahrens 	int error;
2827789Sahrens 
2828789Sahrens 	/*
2829789Sahrens 	 * Verify that the dataset contains a directory object.
2830789Sahrens 	 */
283110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
283210922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
28331731Sbonwick 	if (error != ENOENT) {
28341731Sbonwick 		/* We could have crashed in the middle of destroying it */
28351731Sbonwick 		ASSERT3U(error, ==, 0);
283610922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
283710922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
28381731Sbonwick 	}
283910298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2840789Sahrens 
2841789Sahrens 	/*
2842789Sahrens 	 * Destroy the dataset.
2843789Sahrens 	 */
284410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
28452199Sahrens 	return (0);
2846789Sahrens }
2847789Sahrens 
284810922SJeff.Bonwick@Sun.COM static boolean_t
284910922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2850789Sahrens {
285110922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
285210922SJeff.Bonwick@Sun.COM 	int error;
285310922SJeff.Bonwick@Sun.COM 
285410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
285510922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
285610922SJeff.Bonwick@Sun.COM 
285710922SJeff.Bonwick@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
285810922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
285910922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
286010922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
286110922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
286210922SJeff.Bonwick@Sun.COM 	}
286310922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
286410922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
286510922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2866789Sahrens }
2867789Sahrens 
286810922SJeff.Bonwick@Sun.COM static boolean_t
286910922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
287010922SJeff.Bonwick@Sun.COM {
287110922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
287210922SJeff.Bonwick@Sun.COM 	int error;
287310922SJeff.Bonwick@Sun.COM 
287410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
287510922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
287610922SJeff.Bonwick@Sun.COM 
287710922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
287810922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
287910922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
288010922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
288110922SJeff.Bonwick@Sun.COM }
288210922SJeff.Bonwick@Sun.COM 
288310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2884789Sahrens void
288510922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2886789Sahrens {
288710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
288810922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
288910922SJeff.Bonwick@Sun.COM 	int iters;
2890789Sahrens 	int error;
28916689Smaybee 	objset_t *os, *os2;
289210922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2893789Sahrens 	zilog_t *zilog;
289410922SJeff.Bonwick@Sun.COM 
289510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
289610922SJeff.Bonwick@Sun.COM 
289710922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
289810922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2899789Sahrens 
2900789Sahrens 	/*
2901789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2902789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
290310922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2904789Sahrens 	 */
2905789Sahrens 	if (ztest_random(2) == 0 &&
290610298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
290710922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
290810922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
290910922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
291010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2911789Sahrens 	}
2912789Sahrens 
2913789Sahrens 	/*
2914789Sahrens 	 * There may be an old instance of the dataset we're about to
2915789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2916789Sahrens 	 * and all of its snapshots.
2917789Sahrens 	 */
291810922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
29192417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2920789Sahrens 
2921789Sahrens 	/*
2922789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2923789Sahrens 	 */
292410922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2925789Sahrens 
2926789Sahrens 	/*
2927789Sahrens 	 * Verify that we can create a new dataset.
2928789Sahrens 	 */
292910272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
293010922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
2931789Sahrens 	if (error) {
2932789Sahrens 		if (error == ENOSPC) {
293310922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
293410922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2935789Sahrens 			return;
2936789Sahrens 		}
2937789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2938789Sahrens 	}
2939789Sahrens 
294010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
294110922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
294210922SJeff.Bonwick@Sun.COM 
294310922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2944789Sahrens 
2945789Sahrens 	/*
2946789Sahrens 	 * Open the intent log for it.
2947789Sahrens 	 */
294810922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2949789Sahrens 
2950789Sahrens 	/*
295110922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
295210922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2953789Sahrens 	 */
295410922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
295510922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
295610922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
295710922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
295810922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2959789Sahrens 	}
2960789Sahrens 
2961789Sahrens 	/*
2962789Sahrens 	 * Verify that we cannot create an existing dataset.
2963789Sahrens 	 */
296410922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
296510922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2966789Sahrens 
2967789Sahrens 	/*
296810298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2969789Sahrens 	 */
297010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
297110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
297210298SMatthew.Ahrens@Sun.COM 
297310298SMatthew.Ahrens@Sun.COM 	/*
297410922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
297510298SMatthew.Ahrens@Sun.COM 	 */
297610922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
297710922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
2978789Sahrens 
2979789Sahrens 	zil_close(zilog);
298010298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
298110922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
298210922SJeff.Bonwick@Sun.COM 
298310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2984789Sahrens }
2985789Sahrens 
2986789Sahrens /*
2987789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
2988789Sahrens  */
2989789Sahrens void
299010922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
2991789Sahrens {
299210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
299310922SJeff.Bonwick@Sun.COM 
299410922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
299510922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
299610922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
299710922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2998789Sahrens }
2999789Sahrens 
3000789Sahrens /*
30019691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
30029691SGeorge.Wilson@Sun.COM  */
30039691SGeorge.Wilson@Sun.COM void
300410922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
30059691SGeorge.Wilson@Sun.COM {
300610922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
300710922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
300810922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
300910922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
301010922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
30119691SGeorge.Wilson@Sun.COM 	int error;
30129691SGeorge.Wilson@Sun.COM 
301310922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
301410922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
301510922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
301610922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
301710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30189691SGeorge.Wilson@Sun.COM 
301910242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
30209691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30219691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
302210242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
30239691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30249691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
302510242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
30269691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30279691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
302810242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
30299691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30309691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
303110242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
30329691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30339691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
30349691SGeorge.Wilson@Sun.COM }
30359691SGeorge.Wilson@Sun.COM 
30369691SGeorge.Wilson@Sun.COM /*
30378779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
30388779SMark.Musante@Sun.COM  */
30398779SMark.Musante@Sun.COM void
304010922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
30418779SMark.Musante@Sun.COM {
304210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
30438779SMark.Musante@Sun.COM 	objset_t *clone;
30448779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
304510922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
304610922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
304710922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
304810922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
304910922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
305010922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
305110922SJeff.Bonwick@Sun.COM 	int error;
305210922SJeff.Bonwick@Sun.COM 
305310922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
305410922SJeff.Bonwick@Sun.COM 
305510922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
305610922SJeff.Bonwick@Sun.COM 
305710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
305810922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
305910922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
306010922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
306110922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30628837SMark.Musante@Sun.COM 
30639355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
306410922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30659229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30669229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
306710922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30689229SGeorge.Wilson@Sun.COM 			goto out;
30699229SGeorge.Wilson@Sun.COM 		}
30709229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
30719229SGeorge.Wilson@Sun.COM 	}
30728779SMark.Musante@Sun.COM 
307310298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
30748779SMark.Musante@Sun.COM 	if (error)
30758779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
30768779SMark.Musante@Sun.COM 
307710272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
307810298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
30799229SGeorge.Wilson@Sun.COM 	if (error) {
30809229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
308110922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30829229SGeorge.Wilson@Sun.COM 			goto out;
30839229SGeorge.Wilson@Sun.COM 		}
30848779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
30859229SGeorge.Wilson@Sun.COM 	}
30868779SMark.Musante@Sun.COM 
30878779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
308810922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30899229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30909229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
309110922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30929229SGeorge.Wilson@Sun.COM 			goto out;
30939229SGeorge.Wilson@Sun.COM 		}
30949229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
30959229SGeorge.Wilson@Sun.COM 	}
30968779SMark.Musante@Sun.COM 
30978779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
309810922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30999229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31009229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
310110922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31029229SGeorge.Wilson@Sun.COM 			goto out;
31039229SGeorge.Wilson@Sun.COM 		}
31049229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31059229SGeorge.Wilson@Sun.COM 	}
31068779SMark.Musante@Sun.COM 
310710298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
31088779SMark.Musante@Sun.COM 	if (error)
31098779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31108779SMark.Musante@Sun.COM 
311110272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
311210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31139229SGeorge.Wilson@Sun.COM 	if (error) {
31149229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
311510922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31169229SGeorge.Wilson@Sun.COM 			goto out;
31179229SGeorge.Wilson@Sun.COM 		}
31188779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
31199229SGeorge.Wilson@Sun.COM 	}
31208779SMark.Musante@Sun.COM 
312110298SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
31228779SMark.Musante@Sun.COM 	if (error)
31238779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
312410588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
31258779SMark.Musante@Sun.COM 	if (error != EBUSY)
31268779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
31278779SMark.Musante@Sun.COM 		    error);
31288779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
31298779SMark.Musante@Sun.COM 
31309229SGeorge.Wilson@Sun.COM out:
313110922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
313210922SJeff.Bonwick@Sun.COM 
313310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
31348779SMark.Musante@Sun.COM }
31358779SMark.Musante@Sun.COM 
31368779SMark.Musante@Sun.COM /*
3137789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3138789Sahrens  */
3139789Sahrens void
314010922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3141789Sahrens {
314210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
314310922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
314410922SJeff.Bonwick@Sun.COM 
314510922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
314610922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3147789Sahrens 
3148789Sahrens 	/*
314910922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
315010922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3151789Sahrens 	 */
315210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
315310922SJeff.Bonwick@Sun.COM 		return;
315410922SJeff.Bonwick@Sun.COM 
315510922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
315610922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
315710922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3158789Sahrens }
3159789Sahrens 
3160789Sahrens /*
3161789Sahrens  * Verify that dmu_{read,write} work as expected.
3162789Sahrens  */
3163789Sahrens void
316410922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3165789Sahrens {
316610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
316710922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3168789Sahrens 	dmu_tx_t *tx;
3169789Sahrens 	int i, freeit, error;
3170789Sahrens 	uint64_t n, s, txg;
3171789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
317210922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
317310922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3174789Sahrens 	uint64_t regions = 997;
3175789Sahrens 	uint64_t stride = 123456789ULL;
3176789Sahrens 	uint64_t width = 40;
3177789Sahrens 	int free_percent = 5;
3178789Sahrens 
3179789Sahrens 	/*
3180789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3181789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3182789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3183789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3184789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3185789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3186789Sahrens 	 *
3187789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3188789Sahrens 	 *	bigobj, at the head of the nth chunk
3189789Sahrens 	 *	bigobj, at the tail of the nth chunk
3190789Sahrens 	 *
3191789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3192789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3193789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3194789Sahrens 	 *
3195789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3196789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3197789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3198789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3199789Sahrens 	 * tracking which parts of an object are allocated and free,
3200789Sahrens 	 * and that the contents of the allocated blocks are correct.
3201789Sahrens 	 */
3202789Sahrens 
3203789Sahrens 	/*
3204789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3205789Sahrens 	 */
320610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
320710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
320810922SJeff.Bonwick@Sun.COM 
320910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
321010922SJeff.Bonwick@Sun.COM 		return;
321110922SJeff.Bonwick@Sun.COM 
321210922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
321310922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
321410922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
321510922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3216789Sahrens 
3217789Sahrens 	/*
3218789Sahrens 	 * Prefetch a random chunk of the big object.
3219789Sahrens 	 * Our aim here is to get some async reads in flight
3220789Sahrens 	 * for blocks that we may free below; the DMU should
3221789Sahrens 	 * handle this race correctly.
3222789Sahrens 	 */
3223789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3224789Sahrens 	s = 1 + ztest_random(2 * width - 1);
322510922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3226789Sahrens 
3227789Sahrens 	/*
3228789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3229789Sahrens 	 */
3230789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3231789Sahrens 	s = 1 + ztest_random(width - 1);
3232789Sahrens 
3233789Sahrens 	packoff = n * sizeof (bufwad_t);
3234789Sahrens 	packsize = s * sizeof (bufwad_t);
3235789Sahrens 
323610922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
323710922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3238789Sahrens 
3239789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3240789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3241789Sahrens 
3242789Sahrens 	/*
3243789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3244789Sahrens 	 * overwriting it.
3245789Sahrens 	 */
3246789Sahrens 	freeit = (ztest_random(100) < free_percent);
3247789Sahrens 
3248789Sahrens 	/*
3249789Sahrens 	 * Read the current contents of our objects.
3250789Sahrens 	 */
325110922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
32529512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32531544Seschrock 	ASSERT3U(error, ==, 0);
325410922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
32559512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32561544Seschrock 	ASSERT3U(error, ==, 0);
3257789Sahrens 
3258789Sahrens 	/*
3259789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3260789Sahrens 	 */
3261789Sahrens 	tx = dmu_tx_create(os);
3262789Sahrens 
326310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3264789Sahrens 
3265789Sahrens 	if (freeit)
326610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3267789Sahrens 	else
326810922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
326910922SJeff.Bonwick@Sun.COM 
327010922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
327110922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3272789Sahrens 		umem_free(packbuf, packsize);
3273789Sahrens 		umem_free(bigbuf, bigsize);
3274789Sahrens 		return;
3275789Sahrens 	}
3276789Sahrens 
327710922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
327810922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
327910922SJeff.Bonwick@Sun.COM 
328010922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
328110922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3282789Sahrens 
3283789Sahrens 	/*
3284789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3285789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3286789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3287789Sahrens 	 * with the new values we want to write out.
3288789Sahrens 	 */
3289789Sahrens 	for (i = 0; i < s; i++) {
3290789Sahrens 		/* LINTED */
3291789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3292789Sahrens 		/* LINTED */
329310922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3294789Sahrens 		/* LINTED */
329510922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3296789Sahrens 
3297789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3298789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3299789Sahrens 
3300789Sahrens 		if (pack->bw_txg > txg)
3301789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3302789Sahrens 			    pack->bw_txg, txg);
3303789Sahrens 
3304789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3305789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3306789Sahrens 			    pack->bw_index, n, i);
3307789Sahrens 
3308789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3309789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3310789Sahrens 
3311789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3312789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3313789Sahrens 
3314789Sahrens 		if (freeit) {
3315789Sahrens 			bzero(pack, sizeof (bufwad_t));
3316789Sahrens 		} else {
3317789Sahrens 			pack->bw_index = n + i;
3318789Sahrens 			pack->bw_txg = txg;
3319789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3320789Sahrens 		}
3321789Sahrens 		*bigH = *pack;
3322789Sahrens 		*bigT = *pack;
3323789Sahrens 	}
3324789Sahrens 
3325789Sahrens 	/*
3326789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3327789Sahrens 	 * Now write them out.
3328789Sahrens 	 */
332910922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3330789Sahrens 
3331789Sahrens 	if (freeit) {
333210922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3333789Sahrens 			(void) printf("freeing offset %llx size %llx"
3334789Sahrens 			    " txg %llx\n",
3335789Sahrens 			    (u_longlong_t)bigoff,
3336789Sahrens 			    (u_longlong_t)bigsize,
3337789Sahrens 			    (u_longlong_t)txg);
3338789Sahrens 		}
333910922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3340789Sahrens 	} else {
334110922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3342789Sahrens 			(void) printf("writing offset %llx size %llx"
3343789Sahrens 			    " txg %llx\n",
3344789Sahrens 			    (u_longlong_t)bigoff,
3345789Sahrens 			    (u_longlong_t)bigsize,
3346789Sahrens 			    (u_longlong_t)txg);
3347789Sahrens 		}
334810922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3349789Sahrens 	}
3350789Sahrens 
3351789Sahrens 	dmu_tx_commit(tx);
3352789Sahrens 
3353789Sahrens 	/*
3354789Sahrens 	 * Sanity check the stuff we just wrote.
3355789Sahrens 	 */
3356789Sahrens 	{
3357789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3358789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3359789Sahrens 
336010922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
33619512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
336210922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
33639512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3364789Sahrens 
3365789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3366789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3367789Sahrens 
3368789Sahrens 		umem_free(packcheck, packsize);
3369789Sahrens 		umem_free(bigcheck, bigsize);
3370789Sahrens 	}
3371789Sahrens 
3372789Sahrens 	umem_free(packbuf, packsize);
3373789Sahrens 	umem_free(bigbuf, bigsize);
3374789Sahrens }
3375789Sahrens 
3376789Sahrens void
33779412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
337810922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
33799412SAleksandr.Guzovskiy@Sun.COM {
33809412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
33819412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
33829412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
33839412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
33849412SAleksandr.Guzovskiy@Sun.COM 
33859412SAleksandr.Guzovskiy@Sun.COM 	/*
33869412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
33879412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
33889412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
33899412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
33909412SAleksandr.Guzovskiy@Sun.COM 	 */
33919412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
33929412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
33939412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
33949412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
339510922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
33969412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
339710922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
33989412SAleksandr.Guzovskiy@Sun.COM 
33999412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
34009412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
34019412SAleksandr.Guzovskiy@Sun.COM 
34029412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
34039412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
34049412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
34059412SAleksandr.Guzovskiy@Sun.COM 
34069412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
34079412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
34089412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
34099412SAleksandr.Guzovskiy@Sun.COM 
34109412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
34119412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
34129412SAleksandr.Guzovskiy@Sun.COM 
34139412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
34149412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
34159412SAleksandr.Guzovskiy@Sun.COM 
34169412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
34179412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
34189412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
34199412SAleksandr.Guzovskiy@Sun.COM 
34209412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
34219412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
34229412SAleksandr.Guzovskiy@Sun.COM 	}
34239412SAleksandr.Guzovskiy@Sun.COM }
34249412SAleksandr.Guzovskiy@Sun.COM 
34259412SAleksandr.Guzovskiy@Sun.COM void
342610922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
34279412SAleksandr.Guzovskiy@Sun.COM {
342810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
342910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
34309412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
34319412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34329412SAleksandr.Guzovskiy@Sun.COM 	int error;
34339412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
34349412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
343510922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
343610922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
343710922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
34389412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
34399412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
34409412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
34419412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
34429412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
344310922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
34449412SAleksandr.Guzovskiy@Sun.COM 
34459412SAleksandr.Guzovskiy@Sun.COM 	/*
34469412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
34479412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
34489412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
34499412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
34509412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
34519412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
34529412SAleksandr.Guzovskiy@Sun.COM 	 *
34539412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
34549412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
34559412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
34569412SAleksandr.Guzovskiy@Sun.COM 	 *
34579412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
34589412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
34599412SAleksandr.Guzovskiy@Sun.COM 	 */
34609412SAleksandr.Guzovskiy@Sun.COM 
34619412SAleksandr.Guzovskiy@Sun.COM 	/*
34629412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
34639412SAleksandr.Guzovskiy@Sun.COM 	 */
346410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
346510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
346610922SJeff.Bonwick@Sun.COM 
346710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
346810922SJeff.Bonwick@Sun.COM 		return;
346910922SJeff.Bonwick@Sun.COM 
347010922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
347110922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
347210922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
347310922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
347410922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
347510922SJeff.Bonwick@Sun.COM 
347610922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
347710922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
347810922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
347910922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
34809412SAleksandr.Guzovskiy@Sun.COM 
34819412SAleksandr.Guzovskiy@Sun.COM 	/*
34829412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
34839412SAleksandr.Guzovskiy@Sun.COM 	 */
34849412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
34859412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
34869412SAleksandr.Guzovskiy@Sun.COM 
34879412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
34889412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
34899412SAleksandr.Guzovskiy@Sun.COM 
349010922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
349110922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
34929412SAleksandr.Guzovskiy@Sun.COM 
34939412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
34949412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
34959412SAleksandr.Guzovskiy@Sun.COM 
349610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
34979412SAleksandr.Guzovskiy@Sun.COM 
34989412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
34999412SAleksandr.Guzovskiy@Sun.COM 
35009412SAleksandr.Guzovskiy@Sun.COM 	/*
35019412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
35029412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
35039412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
35049412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
35059412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
35069412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
35079412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
35089412SAleksandr.Guzovskiy@Sun.COM 	 */
35099412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
35109412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
35119412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
35129412SAleksandr.Guzovskiy@Sun.COM 
35139412SAleksandr.Guzovskiy@Sun.COM 		/*
35149412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
35159412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
35169412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
35179412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
35189412SAleksandr.Guzovskiy@Sun.COM 		 */
35199412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
35209412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35219412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
352210922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
35239412SAleksandr.Guzovskiy@Sun.COM 			} else {
35249412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
352510922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35269412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
352710922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35289412SAleksandr.Guzovskiy@Sun.COM 			}
35299412SAleksandr.Guzovskiy@Sun.COM 		}
35309412SAleksandr.Guzovskiy@Sun.COM 
35319412SAleksandr.Guzovskiy@Sun.COM 		/*
35329412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
35339412SAleksandr.Guzovskiy@Sun.COM 		 */
35349412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
35359412SAleksandr.Guzovskiy@Sun.COM 
353610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
353710922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
353810922SJeff.Bonwick@Sun.COM 
353910922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
354010922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
35419412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
35429412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
35439412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
35449412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
35459412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
35469412SAleksandr.Guzovskiy@Sun.COM 				} else {
35479412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35489412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
35499412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35509412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
35519412SAleksandr.Guzovskiy@Sun.COM 				}
35529412SAleksandr.Guzovskiy@Sun.COM 			}
35539412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35549412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
35559412SAleksandr.Guzovskiy@Sun.COM 			return;
35569412SAleksandr.Guzovskiy@Sun.COM 		}
35579412SAleksandr.Guzovskiy@Sun.COM 
35589412SAleksandr.Guzovskiy@Sun.COM 		/*
35599412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
35609412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
35619412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
35629412SAleksandr.Guzovskiy@Sun.COM 		 */
35639412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
356410922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
35659512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
35669412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
356710922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
35689512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
35699412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
35709412SAleksandr.Guzovskiy@Sun.COM 		}
35719412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
357210922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
35739412SAleksandr.Guzovskiy@Sun.COM 
35749412SAleksandr.Guzovskiy@Sun.COM 		/*
35759412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
35769412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
35779412SAleksandr.Guzovskiy@Sun.COM 		 */
357810922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
357910922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
35809412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
35819412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
35829412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
35839412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
35849412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
35859412SAleksandr.Guzovskiy@Sun.COM 		}
358610922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
35879412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
35889412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35899412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
359010922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
35919412SAleksandr.Guzovskiy@Sun.COM 			} else {
35929412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
35939412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
359410922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
35959412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
359610922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
35979412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
359810922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
35999412SAleksandr.Guzovskiy@Sun.COM 			}
36009412SAleksandr.Guzovskiy@Sun.COM 
36019412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
360210922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
36039412SAleksandr.Guzovskiy@Sun.COM 				    FTAG, &dbt) == 0);
36049412SAleksandr.Guzovskiy@Sun.COM 			}
36059412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36069412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36079412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
36089412SAleksandr.Guzovskiy@Sun.COM 			} else {
36099412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36109412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
36119412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
361210922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
36139412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
36149412SAleksandr.Guzovskiy@Sun.COM 			}
36159412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
36169412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
36179412SAleksandr.Guzovskiy@Sun.COM 			}
36189412SAleksandr.Guzovskiy@Sun.COM 		}
36199412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
36209412SAleksandr.Guzovskiy@Sun.COM 
36219412SAleksandr.Guzovskiy@Sun.COM 		/*
36229412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
36239412SAleksandr.Guzovskiy@Sun.COM 		 */
36249412SAleksandr.Guzovskiy@Sun.COM 		{
36259412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
36269412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
36279412SAleksandr.Guzovskiy@Sun.COM 
362810922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
36299512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
363010922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
36319512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
36329412SAleksandr.Guzovskiy@Sun.COM 
36339412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
36349412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
36359412SAleksandr.Guzovskiy@Sun.COM 
36369412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
36379412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
36389412SAleksandr.Guzovskiy@Sun.COM 		}
36399412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
36409412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
36419412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
36429412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
36439412SAleksandr.Guzovskiy@Sun.COM 		}
36449412SAleksandr.Guzovskiy@Sun.COM 	}
36459412SAleksandr.Guzovskiy@Sun.COM 
36469412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
36479412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
36489412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
36499412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
36509412SAleksandr.Guzovskiy@Sun.COM }
36519412SAleksandr.Guzovskiy@Sun.COM 
365210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
36539412SAleksandr.Guzovskiy@Sun.COM void
365410922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
36553711Smaybee {
365610922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
365710922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
365810922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
36593711Smaybee 
36603711Smaybee 	/*
366110922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
366210922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
366310922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
36643711Smaybee 	 */
366510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
366610922SJeff.Bonwick@Sun.COM 
366710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
366810922SJeff.Bonwick@Sun.COM 		return;
366910922SJeff.Bonwick@Sun.COM 
367010922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
367110922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
36723711Smaybee }
36733711Smaybee 
36743711Smaybee void
367510922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3676789Sahrens {
367710922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
367810922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
367910922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
368010922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
368110922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
368210922SJeff.Bonwick@Sun.COM 	void *data;
368310922SJeff.Bonwick@Sun.COM 
368410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
368510922SJeff.Bonwick@Sun.COM 
368610922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
36875530Sbonwick 		return;
368810922SJeff.Bonwick@Sun.COM 
368910922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
369010922SJeff.Bonwick@Sun.COM 		return;
369110922SJeff.Bonwick@Sun.COM 
369210922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
369310922SJeff.Bonwick@Sun.COM 
369410922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
369510922SJeff.Bonwick@Sun.COM 
369610922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
369710922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
369810922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
369910922SJeff.Bonwick@Sun.COM 		    data) != 0)
370010922SJeff.Bonwick@Sun.COM 			break;
370110922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
370210922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
37035530Sbonwick 	}
370410922SJeff.Bonwick@Sun.COM 
370510922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3706789Sahrens }
3707789Sahrens 
3708789Sahrens /*
3709789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3710789Sahrens  */
3711789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3712789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3713789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3714789Sahrens 
3715789Sahrens void
371610922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3717789Sahrens {
371810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
371910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3720789Sahrens 	uint64_t object;
3721789Sahrens 	uint64_t txg, last_txg;
3722789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3723789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3724789Sahrens 	int i, ints;
3725789Sahrens 	dmu_tx_t *tx;
3726789Sahrens 	char propname[100], txgname[100];
3727789Sahrens 	int error;
3728789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3729789Sahrens 
373010922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
373110922SJeff.Bonwick@Sun.COM 
373210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
373310922SJeff.Bonwick@Sun.COM 		return;
373410922SJeff.Bonwick@Sun.COM 
373510922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3736789Sahrens 
3737789Sahrens 	/*
373810922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
373910922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3740789Sahrens 	 */
374110922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
374210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
374310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
374410922SJeff.Bonwick@Sun.COM 	if (txg == 0)
374510922SJeff.Bonwick@Sun.COM 		return;
374610922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
374710922SJeff.Bonwick@Sun.COM 		value[i] = i;
374810922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
374910922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3750789Sahrens 	}
375110922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
375210922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
375310922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
375410922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
375510922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
375610922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
375710922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
375810922SJeff.Bonwick@Sun.COM 	}
375910922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
376010922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
376110922SJeff.Bonwick@Sun.COM 	}
376210922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
376310922SJeff.Bonwick@Sun.COM 
376410922SJeff.Bonwick@Sun.COM 	/*
376510922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
376610922SJeff.Bonwick@Sun.COM 	 */
3767789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3768789Sahrens 
37695530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37705530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37715530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37725530Sbonwick 	bzero(value, sizeof (value));
37735530Sbonwick 	last_txg = 0;
37745530Sbonwick 
37755530Sbonwick 	/*
37765530Sbonwick 	 * If these zap entries already exist, validate their contents.
37775530Sbonwick 	 */
37785530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
37795530Sbonwick 	if (error == 0) {
37805530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
37815530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
37825530Sbonwick 
37835530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
37845530Sbonwick 		    zl_ints, &last_txg) == 0);
37855530Sbonwick 
37865530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
37875530Sbonwick 		    &zl_ints) == 0);
37885530Sbonwick 
37895530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
37905530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
37915530Sbonwick 
37925530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
37935530Sbonwick 		    zl_ints, value) == 0);
37945530Sbonwick 
37955530Sbonwick 		for (i = 0; i < ints; i++) {
37965530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3797789Sahrens 		}
37985530Sbonwick 	} else {
37995530Sbonwick 		ASSERT3U(error, ==, ENOENT);
38005530Sbonwick 	}
38015530Sbonwick 
38025530Sbonwick 	/*
38035530Sbonwick 	 * Atomically update two entries in our zap object.
38045530Sbonwick 	 * The first is named txg_%llu, and contains the txg
38055530Sbonwick 	 * in which the property was last updated.  The second
38065530Sbonwick 	 * is named prop_%llu, and the nth element of its value
38075530Sbonwick 	 * should be txg + object + n.
38085530Sbonwick 	 */
38095530Sbonwick 	tx = dmu_tx_create(os);
381010922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
381110922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
381210922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38135530Sbonwick 		return;
38145530Sbonwick 
38155530Sbonwick 	if (last_txg > txg)
38165530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
38175530Sbonwick 
38185530Sbonwick 	for (i = 0; i < ints; i++)
38195530Sbonwick 		value[i] = txg + object + i;
38205530Sbonwick 
382110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
382210922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
382310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
382410922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
38255530Sbonwick 
38265530Sbonwick 	dmu_tx_commit(tx);
38275530Sbonwick 
38285530Sbonwick 	/*
38295530Sbonwick 	 * Remove a random pair of entries.
38305530Sbonwick 	 */
38315530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
38325530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
38335530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
38345530Sbonwick 
38355530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38365530Sbonwick 
38375530Sbonwick 	if (error == ENOENT)
38385530Sbonwick 		return;
38395530Sbonwick 
38405530Sbonwick 	ASSERT3U(error, ==, 0);
38415530Sbonwick 
38425530Sbonwick 	tx = dmu_tx_create(os);
384310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
384410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
384510922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38465530Sbonwick 		return;
384710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
384810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3849789Sahrens 	dmu_tx_commit(tx);
3850789Sahrens }
3851789Sahrens 
385210431SSanjeev.Bagewadi@Sun.COM /*
385310431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
385410431SSanjeev.Bagewadi@Sun.COM  */
385510431SSanjeev.Bagewadi@Sun.COM void
385610922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
385710431SSanjeev.Bagewadi@Sun.COM {
385810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
385910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
386010922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
386110922SJeff.Bonwick@Sun.COM 
386210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
386310922SJeff.Bonwick@Sun.COM 
386410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
386510922SJeff.Bonwick@Sun.COM 		return;
386610922SJeff.Bonwick@Sun.COM 
386710922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
386810431SSanjeev.Bagewadi@Sun.COM 
386910431SSanjeev.Bagewadi@Sun.COM 	/*
387010922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
387110922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
387210922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
387310431SSanjeev.Bagewadi@Sun.COM 	 */
387410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
387510922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
387610922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
387710922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
387810922SJeff.Bonwick@Sun.COM 		int error;
387910922SJeff.Bonwick@Sun.COM 
388010922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
388110922SJeff.Bonwick@Sun.COM 		    id, value);
388210431SSanjeev.Bagewadi@Sun.COM 
388310431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
388410922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
388510922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
388610922SJeff.Bonwick@Sun.COM 		if (txg == 0)
388710431SSanjeev.Bagewadi@Sun.COM 			return;
388810922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
388910922SJeff.Bonwick@Sun.COM 		    &value, tx);
389010431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
389110431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
389210431SSanjeev.Bagewadi@Sun.COM 	}
389310431SSanjeev.Bagewadi@Sun.COM }
389410431SSanjeev.Bagewadi@Sun.COM 
389510922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3896789Sahrens void
389710922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3898789Sahrens {
389910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
390010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3901789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3902789Sahrens 	dmu_tx_t *tx;
3903789Sahrens 	int i, namelen, error;
390410922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3905789Sahrens 	char name[20], string_value[20];
3906789Sahrens 	void *data;
3907789Sahrens 
390810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
390910922SJeff.Bonwick@Sun.COM 
391010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
391110922SJeff.Bonwick@Sun.COM 		return;
391210922SJeff.Bonwick@Sun.COM 
391310922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
391410922SJeff.Bonwick@Sun.COM 
39155530Sbonwick 	/*
39165530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
39175530Sbonwick 	 * x is a random printable character and the dots are dots.
39185530Sbonwick 	 * There are 94 such characters, and the name length goes from
39195530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
39205530Sbonwick 	 */
39215530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
39225530Sbonwick 
39235530Sbonwick 	for (i = 0; i < 3; i++)
39245530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
39255530Sbonwick 	for (; i < namelen - 1; i++)
39265530Sbonwick 		name[i] = '.';
39275530Sbonwick 	name[i] = '\0';
39285530Sbonwick 
392910922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
39305530Sbonwick 		wsize = sizeof (txg);
39315530Sbonwick 		wc = 1;
39325530Sbonwick 		data = &txg;
39335530Sbonwick 	} else {
39345530Sbonwick 		wsize = 1;
39355530Sbonwick 		wc = namelen;
39365530Sbonwick 		data = string_value;
39375530Sbonwick 	}
39385530Sbonwick 
39395530Sbonwick 	count = -1ULL;
39405530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
39415530Sbonwick 	ASSERT(count != -1ULL);
39425530Sbonwick 
39435530Sbonwick 	/*
39445530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
39455530Sbonwick 	 */
39465530Sbonwick 	i = ztest_random(5);
39475530Sbonwick 
39485530Sbonwick 	if (i >= 2) {
39495530Sbonwick 		tx = dmu_tx_create(os);
395010922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
395110922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
395210922SJeff.Bonwick@Sun.COM 		if (txg == 0)
39535530Sbonwick 			return;
39545530Sbonwick 		bcopy(name, string_value, namelen);
39555530Sbonwick 	} else {
39565530Sbonwick 		tx = NULL;
39575530Sbonwick 		txg = 0;
39585530Sbonwick 		bzero(string_value, namelen);
39595530Sbonwick 	}
39605530Sbonwick 
39615530Sbonwick 	switch (i) {
39625530Sbonwick 
39635530Sbonwick 	case 0:
39645530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
39655530Sbonwick 		if (error == 0) {
39665530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
39675530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3968789Sahrens 		} else {
39695530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3970789Sahrens 		}
39715530Sbonwick 		break;
39725530Sbonwick 
39735530Sbonwick 	case 1:
39745530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
39755530Sbonwick 		if (error == 0) {
39765530Sbonwick 			if (data == string_value &&
39775530Sbonwick 			    bcmp(name, data, namelen) != 0)
39785530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
39795530Sbonwick 				    name, data, namelen);
39805530Sbonwick 		} else {
39815530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3982789Sahrens 		}
39835530Sbonwick 		break;
39845530Sbonwick 
39855530Sbonwick 	case 2:
39865530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
39875530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
39885530Sbonwick 		break;
39895530Sbonwick 
39905530Sbonwick 	case 3:
39915530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
39925530Sbonwick 		break;
39935530Sbonwick 
39945530Sbonwick 	case 4:
39955530Sbonwick 		error = zap_remove(os, object, name, tx);
39965530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
39975530Sbonwick 		break;
3998789Sahrens 	}
39995530Sbonwick 
40005530Sbonwick 	if (tx != NULL)
40015530Sbonwick 		dmu_tx_commit(tx);
4002789Sahrens }
4003789Sahrens 
400410612SRicardo.M.Correia@Sun.COM /*
400510612SRicardo.M.Correia@Sun.COM  * Commit callback data.
400610612SRicardo.M.Correia@Sun.COM  */
400710612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
400810612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
400910612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
401010612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
401110612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
401210612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
401310612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
401410612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
401510612SRicardo.M.Correia@Sun.COM 
401610612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
401710612SRicardo.M.Correia@Sun.COM static void
401810612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
401910612SRicardo.M.Correia@Sun.COM {
402010612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
402110612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
402210612SRicardo.M.Correia@Sun.COM 
402310612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
402410612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
402510612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
402610612SRicardo.M.Correia@Sun.COM 
402710612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
402810612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
402910612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
403010612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
403110612SRicardo.M.Correia@Sun.COM 		    synced_txg);
403210612SRicardo.M.Correia@Sun.COM 
403310612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
403410612SRicardo.M.Correia@Sun.COM 
403510612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
403610612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
403710612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
403810612SRicardo.M.Correia@Sun.COM 
403910612SRicardo.M.Correia@Sun.COM 		/*
404010612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
404110612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
404210612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
404310612SRicardo.M.Correia@Sun.COM 		 */
404410612SRicardo.M.Correia@Sun.COM 		return;
404510612SRicardo.M.Correia@Sun.COM 	}
404610612SRicardo.M.Correia@Sun.COM 
404710612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
404810612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
404910612SRicardo.M.Correia@Sun.COM 		goto out;
405010612SRicardo.M.Correia@Sun.COM 
405110612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
405210612SRicardo.M.Correia@Sun.COM 
405310612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
405410612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
405510612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
405610612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
405710612SRicardo.M.Correia@Sun.COM 
405810612SRicardo.M.Correia@Sun.COM out:
405910612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
406010612SRicardo.M.Correia@Sun.COM }
406110612SRicardo.M.Correia@Sun.COM 
406210612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
406310612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
406410612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
406510612SRicardo.M.Correia@Sun.COM {
406610612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
406710612SRicardo.M.Correia@Sun.COM 
406810612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
406910612SRicardo.M.Correia@Sun.COM 
407010612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
407110612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
407210612SRicardo.M.Correia@Sun.COM 
407310612SRicardo.M.Correia@Sun.COM 	return (cb_data);
407410612SRicardo.M.Correia@Sun.COM }
407510612SRicardo.M.Correia@Sun.COM 
407610612SRicardo.M.Correia@Sun.COM /*
407710612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
407810612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
407910612SRicardo.M.Correia@Sun.COM  * implementation bug.
408010612SRicardo.M.Correia@Sun.COM  */
408110612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
408210612SRicardo.M.Correia@Sun.COM 
408310612SRicardo.M.Correia@Sun.COM /*
408410612SRicardo.M.Correia@Sun.COM  * Commit callback test.
408510612SRicardo.M.Correia@Sun.COM  */
408610612SRicardo.M.Correia@Sun.COM void
408710922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
408810612SRicardo.M.Correia@Sun.COM {
408910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
409010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
409110612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
409210612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
409310612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
409410612SRicardo.M.Correia@Sun.COM 	int i, error;
409510612SRicardo.M.Correia@Sun.COM 
409610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
409710922SJeff.Bonwick@Sun.COM 
409810922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
409910922SJeff.Bonwick@Sun.COM 		return;
410010922SJeff.Bonwick@Sun.COM 
410110612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
410210612SRicardo.M.Correia@Sun.COM 
410310612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
410410612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
410510612SRicardo.M.Correia@Sun.COM 
410610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
410710612SRicardo.M.Correia@Sun.COM 
410810612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
410910612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
411010612SRicardo.M.Correia@Sun.COM 		error = -1;
411110612SRicardo.M.Correia@Sun.COM 
411210612SRicardo.M.Correia@Sun.COM 	if (!error)
411310612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
411410612SRicardo.M.Correia@Sun.COM 
411510612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
411610612SRicardo.M.Correia@Sun.COM 
411710612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
411810612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
411910612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
412010612SRicardo.M.Correia@Sun.COM 
412110612SRicardo.M.Correia@Sun.COM 	if (error) {
412210612SRicardo.M.Correia@Sun.COM 		/*
412310612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
412410612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
412510612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
412610612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
412710612SRicardo.M.Correia@Sun.COM 		 */
412810612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
412910612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
413010612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
413110612SRicardo.M.Correia@Sun.COM 		}
413210612SRicardo.M.Correia@Sun.COM 
413310612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
413410612SRicardo.M.Correia@Sun.COM 
413510612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
413610612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
413710612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
413810612SRicardo.M.Correia@Sun.COM 		}
413910612SRicardo.M.Correia@Sun.COM 
414010612SRicardo.M.Correia@Sun.COM 		return;
414110612SRicardo.M.Correia@Sun.COM 	}
414210612SRicardo.M.Correia@Sun.COM 
414310612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
414410612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
414510612SRicardo.M.Correia@Sun.COM 
414610612SRicardo.M.Correia@Sun.COM 	/*
414710612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
414810612SRicardo.M.Correia@Sun.COM 	 */
414910922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
415010612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
415110612SRicardo.M.Correia@Sun.COM 
415210612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
415310612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
415410612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
415510612SRicardo.M.Correia@Sun.COM 
415610922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
415710612SRicardo.M.Correia@Sun.COM 
415810612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
415910612SRicardo.M.Correia@Sun.COM 
416010612SRicardo.M.Correia@Sun.COM 	/*
416110612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
416210612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
416310612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
416410612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
416510612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
416610612SRicardo.M.Correia@Sun.COM 	 * implementation.
416710612SRicardo.M.Correia@Sun.COM 	 *
416810612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
416910612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
417010612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
417110612SRicardo.M.Correia@Sun.COM 	 */
417210612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
417310612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
417410612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
417510612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
417610612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
417710612SRicardo.M.Correia@Sun.COM 	}
417810612SRicardo.M.Correia@Sun.COM 
417910612SRicardo.M.Correia@Sun.COM 	/*
418010612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
418110612SRicardo.M.Correia@Sun.COM 	 *
418210612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
418310612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
418410612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
418510612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
418610612SRicardo.M.Correia@Sun.COM 	 */
418710612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
418810612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
418910612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
419010612SRicardo.M.Correia@Sun.COM 
419110612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
419210612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
419310612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
419410612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
419510612SRicardo.M.Correia@Sun.COM 		else
419610612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
419710612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
419810612SRicardo.M.Correia@Sun.COM 
419910612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
420010612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
420110612SRicardo.M.Correia@Sun.COM 
420210612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
420310612SRicardo.M.Correia@Sun.COM 	}
420410612SRicardo.M.Correia@Sun.COM 
420510612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
420610612SRicardo.M.Correia@Sun.COM 
420710612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
420810612SRicardo.M.Correia@Sun.COM }
420910612SRicardo.M.Correia@Sun.COM 
421010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4211789Sahrens void
421210922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4213789Sahrens {
421410922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
421510922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
421610922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
421710922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
421810922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
421910922SJeff.Bonwick@Sun.COM 	};
422010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
422110922SJeff.Bonwick@Sun.COM 
422210922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
422310922SJeff.Bonwick@Sun.COM 
422410922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
422510922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
422610922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
422710922SJeff.Bonwick@Sun.COM 
422810922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
422910922SJeff.Bonwick@Sun.COM }
423010922SJeff.Bonwick@Sun.COM 
423110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
423210922SJeff.Bonwick@Sun.COM void
423310922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
423410922SJeff.Bonwick@Sun.COM {
423510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
423610922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
423710922SJeff.Bonwick@Sun.COM 
423810922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
423910922SJeff.Bonwick@Sun.COM 
424010922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
424110922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
424210922SJeff.Bonwick@Sun.COM 
424310922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
424410922SJeff.Bonwick@Sun.COM 
424510922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
424610922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
424710922SJeff.Bonwick@Sun.COM 
424810922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
424910922SJeff.Bonwick@Sun.COM 
425010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4251789Sahrens }
4252789Sahrens 
4253789Sahrens /*
425410693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
425510693Schris.kirby@sun.com  */
425610693Schris.kirby@sun.com void
425710922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
425810693Schris.kirby@sun.com {
425910693Schris.kirby@sun.com 	int error;
426010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
426110693Schris.kirby@sun.com 	objset_t *origin;
426210693Schris.kirby@sun.com 	char snapname[100];
426310693Schris.kirby@sun.com 	char fullname[100];
426410693Schris.kirby@sun.com 	char clonename[100];
426510693Schris.kirby@sun.com 	char tag[100];
426610693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
426710693Schris.kirby@sun.com 
426810693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
426910693Schris.kirby@sun.com 
427010693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
427110693Schris.kirby@sun.com 
427210922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
427310693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
427410922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
427510922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
427610693Schris.kirby@sun.com 
427710693Schris.kirby@sun.com 	/*
427810693Schris.kirby@sun.com 	 * Clean up from any previous run.
427910693Schris.kirby@sun.com 	 */
428010693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
428110693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
428210693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
428310693Schris.kirby@sun.com 
428410693Schris.kirby@sun.com 	/*
428510693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
428610693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
428710693Schris.kirby@sun.com 	 */
428810693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
428910693Schris.kirby@sun.com 	if (error) {
429010693Schris.kirby@sun.com 		if (error == ENOSPC) {
429110693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
429210693Schris.kirby@sun.com 			goto out;
429310693Schris.kirby@sun.com 		}
429410693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
429510693Schris.kirby@sun.com 	}
429610693Schris.kirby@sun.com 
429710693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
429810693Schris.kirby@sun.com 	if (error)
429910693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
430010693Schris.kirby@sun.com 
430110693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
430210693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
430310693Schris.kirby@sun.com 	if (error) {
430410693Schris.kirby@sun.com 		if (error == ENOSPC) {
430510693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
430610693Schris.kirby@sun.com 			goto out;
430710693Schris.kirby@sun.com 		}
430810693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
430910693Schris.kirby@sun.com 	}
431010693Schris.kirby@sun.com 
431110693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
431210693Schris.kirby@sun.com 	if (error) {
431310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
431410693Schris.kirby@sun.com 		    fullname, error);
431510693Schris.kirby@sun.com 	}
431610693Schris.kirby@sun.com 
431710693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
431810693Schris.kirby@sun.com 	if (error)
431910693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
432010693Schris.kirby@sun.com 
432110693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
432210693Schris.kirby@sun.com 	if (error != ENOENT)
432310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
432410693Schris.kirby@sun.com 
432510693Schris.kirby@sun.com 	/*
432610693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
432710693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
432810693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
432910693Schris.kirby@sun.com 	 */
433010693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
433110693Schris.kirby@sun.com 	if (error) {
433210693Schris.kirby@sun.com 		if (error == ENOSPC) {
433310693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
433410693Schris.kirby@sun.com 			goto out;
433510693Schris.kirby@sun.com 		}
433610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
433710693Schris.kirby@sun.com 	}
433810693Schris.kirby@sun.com 
433910693Schris.kirby@sun.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE, B_TRUE);
434010693Schris.kirby@sun.com 	if (error)
434110693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
434210693Schris.kirby@sun.com 
434310693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
434410693Schris.kirby@sun.com 	if (error != EBUSY) {
434510693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
434610693Schris.kirby@sun.com 		    fullname, error);
434710693Schris.kirby@sun.com 	}
434810693Schris.kirby@sun.com 
434910693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
435010693Schris.kirby@sun.com 	if (error) {
435110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
435210693Schris.kirby@sun.com 		    fullname, error);
435310693Schris.kirby@sun.com 	}
435410693Schris.kirby@sun.com 
435510693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
435610693Schris.kirby@sun.com 	if (error)
435710693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
435810693Schris.kirby@sun.com 
435910693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
436010693Schris.kirby@sun.com 
436110693Schris.kirby@sun.com out:
436210693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
436310693Schris.kirby@sun.com }
436410693Schris.kirby@sun.com 
436510693Schris.kirby@sun.com /*
4366789Sahrens  * Inject random faults into the on-disk data.
4367789Sahrens  */
436810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4369789Sahrens void
437010922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4371789Sahrens {
437210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
437310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4374789Sahrens 	int fd;
4375789Sahrens 	uint64_t offset;
437611422SMark.Musante@Sun.COM 	uint64_t leaves;
4377789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4378789Sahrens 	uint64_t top, leaf;
4379789Sahrens 	char path0[MAXPATHLEN];
4380789Sahrens 	char pathrand[MAXPATHLEN];
4381789Sahrens 	size_t fsize;
4382789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4383789Sahrens 	int iters = 1000;
438411422SMark.Musante@Sun.COM 	int maxfaults;
438511422SMark.Musante@Sun.COM 	int mirror_save;
43867754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
43871544Seschrock 	uint64_t guid0 = 0;
438810685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
43891544Seschrock 
439011422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
439111422SMark.Musante@Sun.COM 	maxfaults = MAXFAULTS();
439211422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
439311422SMark.Musante@Sun.COM 	mirror_save = zs->zs_mirrors;
439411422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
439511422SMark.Musante@Sun.COM 
43967754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4397789Sahrens 
4398789Sahrens 	/*
43997754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4400789Sahrens 	 */
44017754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44027754SJeff.Bonwick@Sun.COM 
44037754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
44047754SJeff.Bonwick@Sun.COM 		/*
440510922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
44067754SJeff.Bonwick@Sun.COM 		 */
440710922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
440811422SMark.Musante@Sun.COM 		leaf = ztest_random(leaves) + zs->zs_splits;
44097754SJeff.Bonwick@Sun.COM 
44107754SJeff.Bonwick@Sun.COM 		/*
44117754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
44127754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
44137754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
44147754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
44157754SJeff.Bonwick@Sun.COM 		 */
44167754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
441711422SMark.Musante@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
44187754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
44197754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
44207754SJeff.Bonwick@Sun.COM 
44217754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
442210685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
442310685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
442410685SGeorge.Wilson@Sun.COM 
44257754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
44267754SJeff.Bonwick@Sun.COM 			/*
44277754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
44287754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
44297754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
44307754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
44317754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
44327754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
44337754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
44347754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
44357754SJeff.Bonwick@Sun.COM 			 */
44367754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
44377754SJeff.Bonwick@Sun.COM 
44387754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
44397754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
44407754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
44417754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
44427754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
44437754SJeff.Bonwick@Sun.COM 			} else {
44447754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
44457754SJeff.Bonwick@Sun.COM 			}
44467754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
44477754SJeff.Bonwick@Sun.COM 		}
44487754SJeff.Bonwick@Sun.COM 	} else {
44497754SJeff.Bonwick@Sun.COM 		/*
44507754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
44517754SJeff.Bonwick@Sun.COM 		 */
44527754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
44537754SJeff.Bonwick@Sun.COM 
44547754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
44557754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
44567754SJeff.Bonwick@Sun.COM 			return;
44577754SJeff.Bonwick@Sun.COM 		}
44587754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
44597754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
44607754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
44617754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
44627754SJeff.Bonwick@Sun.COM 
44637754SJeff.Bonwick@Sun.COM 		leaf = 0;
44647754SJeff.Bonwick@Sun.COM 		leaves = 1;
44657754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
44667754SJeff.Bonwick@Sun.COM 	}
4467789Sahrens 
44687754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
44697754SJeff.Bonwick@Sun.COM 
44701544Seschrock 	/*
447110685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
447210685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
44731544Seschrock 	 */
447410685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
44758241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
44768241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
44778241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
447810685SGeorge.Wilson@Sun.COM 
447910685SGeorge.Wilson@Sun.COM 			/*
448010685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
448110685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
448210685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
448310685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
448410685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
448510685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
448610685SGeorge.Wilson@Sun.COM 			 */
448710685SGeorge.Wilson@Sun.COM 			if (islog)
448810685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
448910685SGeorge.Wilson@Sun.COM 
44908241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
449110685SGeorge.Wilson@Sun.COM 
449210685SGeorge.Wilson@Sun.COM 			if (islog)
449310685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
44948241SJeff.Bonwick@Sun.COM 		} else {
44958241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
44968241SJeff.Bonwick@Sun.COM 		}
4497789Sahrens 	}
4498789Sahrens 
449910685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
450010685SGeorge.Wilson@Sun.COM 		return;
450110685SGeorge.Wilson@Sun.COM 
4502789Sahrens 	/*
45031544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4504789Sahrens 	 */
4505789Sahrens 	fd = open(pathrand, O_RDWR);
4506789Sahrens 
4507789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4508789Sahrens 		return;
4509789Sahrens 
4510789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4511789Sahrens 
4512789Sahrens 	while (--iters != 0) {
4513789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4514789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4515789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4516789Sahrens 
4517789Sahrens 		if (offset >= fsize)
4518789Sahrens 			continue;
4519789Sahrens 
452011422SMark.Musante@Sun.COM 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
452111422SMark.Musante@Sun.COM 		if (mirror_save != zs->zs_mirrors) {
452211422SMark.Musante@Sun.COM 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
452311422SMark.Musante@Sun.COM 			(void) close(fd);
452411422SMark.Musante@Sun.COM 			return;
452511422SMark.Musante@Sun.COM 		}
4526789Sahrens 
4527789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4528789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4529789Sahrens 			    offset, pathrand);
453011422SMark.Musante@Sun.COM 
453111422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
453211422SMark.Musante@Sun.COM 
453311422SMark.Musante@Sun.COM 		if (zopt_verbose >= 7)
453411422SMark.Musante@Sun.COM 			(void) printf("injected bad word into %s,"
453511422SMark.Musante@Sun.COM 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4536789Sahrens 	}
4537789Sahrens 
4538789Sahrens 	(void) close(fd);
4539789Sahrens }
4540789Sahrens 
4541789Sahrens /*
454210922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4543789Sahrens  */
4544789Sahrens void
454510922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4546789Sahrens {
454710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
454810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
454910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
455010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
455110922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
455210922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
455310922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
455410922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
455510922SJeff.Bonwick@Sun.COM 	void *buf;
455610922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
455710922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
455810922SJeff.Bonwick@Sun.COM 
455910922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
456010922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
456110922SJeff.Bonwick@Sun.COM 
456210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
456310922SJeff.Bonwick@Sun.COM 
456410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
456510922SJeff.Bonwick@Sun.COM 		return;
456610922SJeff.Bonwick@Sun.COM 
456710922SJeff.Bonwick@Sun.COM 	/*
456810922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
456910922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
457010922SJeff.Bonwick@Sun.COM 	 */
457110922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
457210922SJeff.Bonwick@Sun.COM 
457310922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
457410922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
457510922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
457610922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
457710922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
457810922SJeff.Bonwick@Sun.COM 		return;
457910922SJeff.Bonwick@Sun.COM 	}
458010922SJeff.Bonwick@Sun.COM 
458110922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
458210922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
458310922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
458410922SJeff.Bonwick@Sun.COM 
458510922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
458610922SJeff.Bonwick@Sun.COM 
458710922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
458810922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
458910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
459010922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
459110922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
459210922SJeff.Bonwick@Sun.COM 		return;
459310922SJeff.Bonwick@Sun.COM 	}
459410922SJeff.Bonwick@Sun.COM 
459510922SJeff.Bonwick@Sun.COM 	/*
459610922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
459710922SJeff.Bonwick@Sun.COM 	 */
459810922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
459910922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
460010922SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db) == 0);
460110922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
460210922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
460310922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
460410922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
460510922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
460610922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
460710922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
460810922SJeff.Bonwick@Sun.COM 	}
460910922SJeff.Bonwick@Sun.COM 
461010922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
461110922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
461210922SJeff.Bonwick@Sun.COM 
461310922SJeff.Bonwick@Sun.COM 	/*
461410922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
461510922SJeff.Bonwick@Sun.COM 	 */
461610922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db) == 0);
461710922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
461810922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
461910922SJeff.Bonwick@Sun.COM 
462010922SJeff.Bonwick@Sun.COM 	/*
462110922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
462210922SJeff.Bonwick@Sun.COM 	 */
462310922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
462410922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
462510922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
462610922SJeff.Bonwick@Sun.COM 
462710922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
462810922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
462910922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
463010922SJeff.Bonwick@Sun.COM 
463110922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
463210922SJeff.Bonwick@Sun.COM 
463310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
463410922SJeff.Bonwick@Sun.COM }
463510922SJeff.Bonwick@Sun.COM 
463610922SJeff.Bonwick@Sun.COM /*
463710922SJeff.Bonwick@Sun.COM  * Scrub the pool.
463810922SJeff.Bonwick@Sun.COM  */
463910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
464010922SJeff.Bonwick@Sun.COM void
464110922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
464210922SJeff.Bonwick@Sun.COM {
464310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
464410922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4645789Sahrens 
46467046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
464710922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
46487046Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
4649789Sahrens }
4650789Sahrens 
4651789Sahrens /*
4652789Sahrens  * Rename the pool to a different name and then rename it back.
4653789Sahrens  */
465410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4655789Sahrens void
465610922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4657789Sahrens {
465810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4659789Sahrens 	char *oldname, *newname;
4660789Sahrens 	spa_t *spa;
4661789Sahrens 
466210922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
466310922SJeff.Bonwick@Sun.COM 
466410922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4665789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4666789Sahrens 	(void) strcpy(newname, oldname);
4667789Sahrens 	(void) strcat(newname, "_tmp");
4668789Sahrens 
4669789Sahrens 	/*
4670789Sahrens 	 * Do the rename
4671789Sahrens 	 */
467210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4673789Sahrens 
4674789Sahrens 	/*
4675789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4676789Sahrens 	 */
467710922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4678789Sahrens 
4679789Sahrens 	/*
4680789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4681789Sahrens 	 */
468210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
468310922SJeff.Bonwick@Sun.COM 
468410922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4685789Sahrens 	spa_close(spa, FTAG);
4686789Sahrens 
4687789Sahrens 	/*
4688789Sahrens 	 * Rename it back to the original
4689789Sahrens 	 */
469010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4691789Sahrens 
4692789Sahrens 	/*
4693789Sahrens 	 * Make sure it can still be opened
4694789Sahrens 	 */
469510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
469610922SJeff.Bonwick@Sun.COM 
469710922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4698789Sahrens 	spa_close(spa, FTAG);
4699789Sahrens 
4700789Sahrens 	umem_free(newname, strlen(newname) + 1);
4701789Sahrens 
470210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4703789Sahrens }
4704789Sahrens 
4705789Sahrens /*
470610922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4707789Sahrens  */
4708789Sahrens static void
470910922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4710789Sahrens {
4711789Sahrens 	int status;
4712789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4713789Sahrens 	char zbuf[1024];
4714789Sahrens 	char *bin;
47154527Sperrin 	char *ztest;
47164527Sperrin 	char *isa;
47174527Sperrin 	int isalen;
4718789Sahrens 	FILE *fp;
4719789Sahrens 
4720789Sahrens 	(void) realpath(getexecname(), zdb);
4721789Sahrens 
4722789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4723789Sahrens 	bin = strstr(zdb, "/usr/bin/");
47244527Sperrin 	ztest = strstr(bin, "/ztest");
47254527Sperrin 	isa = bin + 8;
47264527Sperrin 	isalen = ztest - isa;
47274527Sperrin 	isa = strdup(isa);
4728789Sahrens 	/* LINTED */
47295994Sck153898 	(void) sprintf(bin,
473011811SJeff.Bonwick@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
47314527Sperrin 	    isalen,
47324527Sperrin 	    isa,
4733789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4734789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
473511811SJeff.Bonwick@Sun.COM 	    spa_config_path,
47367837SMatthew.Ahrens@Sun.COM 	    pool);
47374527Sperrin 	free(isa);
4738789Sahrens 
4739789Sahrens 	if (zopt_verbose >= 5)
4740789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4741789Sahrens 
4742789Sahrens 	fp = popen(zdb, "r");
4743789Sahrens 
4744789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4745789Sahrens 		if (zopt_verbose >= 3)
4746789Sahrens 			(void) printf("%s", zbuf);
4747789Sahrens 
4748789Sahrens 	status = pclose(fp);
4749789Sahrens 
4750789Sahrens 	if (status == 0)
4751789Sahrens 		return;
4752789Sahrens 
4753789Sahrens 	ztest_dump_core = 0;
4754789Sahrens 	if (WIFEXITED(status))
4755789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4756789Sahrens 	else
4757789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4758789Sahrens }
4759789Sahrens 
4760789Sahrens static void
4761789Sahrens ztest_walk_pool_directory(char *header)
4762789Sahrens {
4763789Sahrens 	spa_t *spa = NULL;
4764789Sahrens 
4765789Sahrens 	if (zopt_verbose >= 6)
4766789Sahrens 		(void) printf("%s\n", header);
4767789Sahrens 
4768789Sahrens 	mutex_enter(&spa_namespace_lock);
4769789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4770789Sahrens 		if (zopt_verbose >= 6)
4771789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4772789Sahrens 	mutex_exit(&spa_namespace_lock);
4773789Sahrens }
4774789Sahrens 
4775789Sahrens static void
4776789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4777789Sahrens {
47788241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4779789Sahrens 	uint64_t pool_guid;
4780789Sahrens 	spa_t *spa;
4781789Sahrens 
4782789Sahrens 	if (zopt_verbose >= 4) {
4783789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4784789Sahrens 		    oldname, newname);
4785789Sahrens 	}
4786789Sahrens 
4787789Sahrens 	/*
4788789Sahrens 	 * Clean up from previous runs.
4789789Sahrens 	 */
4790789Sahrens 	(void) spa_destroy(newname);
4791789Sahrens 
4792789Sahrens 	/*
4793789Sahrens 	 * Get the pool's configuration and guid.
4794789Sahrens 	 */
479510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4796789Sahrens 
47978241SJeff.Bonwick@Sun.COM 	/*
47988241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
47998241SJeff.Bonwick@Sun.COM 	 */
48008241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
48018241SJeff.Bonwick@Sun.COM 		(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
48028241SJeff.Bonwick@Sun.COM 
4803789Sahrens 	pool_guid = spa_guid(spa);
4804789Sahrens 	spa_close(spa, FTAG);
4805789Sahrens 
4806789Sahrens 	ztest_walk_pool_directory("pools before export");
4807789Sahrens 
4808789Sahrens 	/*
4809789Sahrens 	 * Export it.
4810789Sahrens 	 */
481110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4812789Sahrens 
4813789Sahrens 	ztest_walk_pool_directory("pools after export");
4814789Sahrens 
4815789Sahrens 	/*
48168241SJeff.Bonwick@Sun.COM 	 * Try to import it.
48178241SJeff.Bonwick@Sun.COM 	 */
48188241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
48198241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
48208241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
48218241SJeff.Bonwick@Sun.COM 
48228241SJeff.Bonwick@Sun.COM 	/*
4823789Sahrens 	 * Import it under the new name.
4824789Sahrens 	 */
482510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL));
4826789Sahrens 
4827789Sahrens 	ztest_walk_pool_directory("pools after import");
4828789Sahrens 
4829789Sahrens 	/*
4830789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4831789Sahrens 	 */
483210922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL));
4833789Sahrens 
4834789Sahrens 	/*
4835789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4836789Sahrens 	 */
483710922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL));
4838789Sahrens 
4839789Sahrens 	/*
4840789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4841789Sahrens 	 */
484210922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4843789Sahrens 
4844789Sahrens 	/*
4845789Sahrens 	 * Verify that we can open and close the pool using the new name.
4846789Sahrens 	 */
484710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4848789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4849789Sahrens 	spa_close(spa, FTAG);
4850789Sahrens 
4851789Sahrens 	nvlist_free(config);
4852789Sahrens }
4853789Sahrens 
48548241SJeff.Bonwick@Sun.COM static void
48558241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
48568241SJeff.Bonwick@Sun.COM {
485710922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
485810922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
485910922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
486010922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
486110922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
486210922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
48638241SJeff.Bonwick@Sun.COM }
48648241SJeff.Bonwick@Sun.COM 
48655329Sgw25295 static void *
48668241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
48675329Sgw25295 {
48687754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
48695329Sgw25295 
48705329Sgw25295 	while (!ztest_exiting) {
487110922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
487210922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
487310922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
48745329Sgw25295 	}
48755329Sgw25295 	return (NULL);
48765329Sgw25295 }
48775329Sgw25295 
4878789Sahrens static void *
487910922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
488010922SJeff.Bonwick@Sun.COM {
488110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
488210922SJeff.Bonwick@Sun.COM 	int grace = 300;
488310922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
488410922SJeff.Bonwick@Sun.COM 
488510922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
488610922SJeff.Bonwick@Sun.COM 
488710922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
488810922SJeff.Bonwick@Sun.COM 
488910922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
489010922SJeff.Bonwick@Sun.COM 
489110922SJeff.Bonwick@Sun.COM 	return (NULL);
489210922SJeff.Bonwick@Sun.COM }
489310922SJeff.Bonwick@Sun.COM 
489410922SJeff.Bonwick@Sun.COM static void
489510922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
489610922SJeff.Bonwick@Sun.COM {
489710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
489810922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
489910922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
490010922SJeff.Bonwick@Sun.COM 
490110922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
490210922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
490310922SJeff.Bonwick@Sun.COM 
490410922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
490510922SJeff.Bonwick@Sun.COM 
490610922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
490710922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
490810922SJeff.Bonwick@Sun.COM 
490910922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
491010922SJeff.Bonwick@Sun.COM 		Dl_info dli;
491110922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
491210922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
491310922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
491410922SJeff.Bonwick@Sun.COM 	}
491510922SJeff.Bonwick@Sun.COM }
491610922SJeff.Bonwick@Sun.COM 
491710922SJeff.Bonwick@Sun.COM static void *
4918789Sahrens ztest_thread(void *arg)
4919789Sahrens {
492010922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4921789Sahrens 	ztest_shared_t *zs = ztest_shared;
492210922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
492310922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4924789Sahrens 	ztest_info_t *zi;
492510922SJeff.Bonwick@Sun.COM 
492610922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4927789Sahrens 		/*
4928789Sahrens 		 * See if it's time to force a crash.
4929789Sahrens 		 */
493010922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
493110922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4932789Sahrens 
4933789Sahrens 		/*
4934789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4935789Sahrens 		 */
4936789Sahrens 		if (zs->zs_enospc_count > 10)
4937789Sahrens 			break;
493810922SJeff.Bonwick@Sun.COM 
493910922SJeff.Bonwick@Sun.COM 		/*
494010922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
494110922SJeff.Bonwick@Sun.COM 		 */
494210922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
494310922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
494410922SJeff.Bonwick@Sun.COM 
494510922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
494610922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
494710922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
494810922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4949789Sahrens 	}
4950789Sahrens 
4951789Sahrens 	return (NULL);
4952789Sahrens }
4953789Sahrens 
495410922SJeff.Bonwick@Sun.COM static void
495510922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
495610922SJeff.Bonwick@Sun.COM {
495710922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
495810922SJeff.Bonwick@Sun.COM }
495910922SJeff.Bonwick@Sun.COM 
496010922SJeff.Bonwick@Sun.COM static void
496110922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
496210922SJeff.Bonwick@Sun.COM {
496310922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
496410922SJeff.Bonwick@Sun.COM 
496510922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
496610922SJeff.Bonwick@Sun.COM 
496710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
496810922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
496910922SJeff.Bonwick@Sun.COM 
497010922SJeff.Bonwick@Sun.COM 	/*
497110922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
497210922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
497310922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
497410922SJeff.Bonwick@Sun.COM 	 */
497510922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
497610922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
497710922SJeff.Bonwick@Sun.COM 
497810922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
497910922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
498010922SJeff.Bonwick@Sun.COM }
498110922SJeff.Bonwick@Sun.COM 
498210922SJeff.Bonwick@Sun.COM static void
498310922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
498410922SJeff.Bonwick@Sun.COM {
498510922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
498610922SJeff.Bonwick@Sun.COM 
498710922SJeff.Bonwick@Sun.COM 	/*
498810922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
498910922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
499010922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
499110922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
499210922SJeff.Bonwick@Sun.COM 	 *
499310922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
499410922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
499510922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
499610922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
499710922SJeff.Bonwick@Sun.COM 	 */
499810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
499910922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
500010922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
500110922SJeff.Bonwick@Sun.COM }
500210922SJeff.Bonwick@Sun.COM 
500310922SJeff.Bonwick@Sun.COM static int
500410922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
500510922SJeff.Bonwick@Sun.COM {
500610922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
500710922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
500810922SJeff.Bonwick@Sun.COM 	objset_t *os;
500910922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
501010922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
501110922SJeff.Bonwick@Sun.COM 	int error;
501210922SJeff.Bonwick@Sun.COM 
501310922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
501410922SJeff.Bonwick@Sun.COM 
501510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
501610922SJeff.Bonwick@Sun.COM 
501710922SJeff.Bonwick@Sun.COM 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
501810922SJeff.Bonwick@Sun.COM 	    ztest_objset_create_cb, NULL);
501910922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
502010922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
502110922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
502210922SJeff.Bonwick@Sun.COM 		return (error);
502310922SJeff.Bonwick@Sun.COM 	}
502410922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
502510922SJeff.Bonwick@Sun.COM 
502610922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
502710922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
502810922SJeff.Bonwick@Sun.COM 
502910922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
503010922SJeff.Bonwick@Sun.COM 
503110922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
503210922SJeff.Bonwick@Sun.COM 
503310922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
503410922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
503510922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
503610922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
503710922SJeff.Bonwick@Sun.COM 
503810922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
503910922SJeff.Bonwick@Sun.COM 
504010922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
504110922SJeff.Bonwick@Sun.COM 
504210922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
504310922SJeff.Bonwick@Sun.COM 
504410922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
504510922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
504610922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
504710922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
504810922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
504910922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
505010922SJeff.Bonwick@Sun.COM 
505110922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
505210922SJeff.Bonwick@Sun.COM 
505310922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
505410922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
505510922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
505610922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
505710922SJeff.Bonwick@Sun.COM 
505810922SJeff.Bonwick@Sun.COM 	return (0);
505910922SJeff.Bonwick@Sun.COM }
506010922SJeff.Bonwick@Sun.COM 
506110922SJeff.Bonwick@Sun.COM static void
506210922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
506310922SJeff.Bonwick@Sun.COM {
506410922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
506510922SJeff.Bonwick@Sun.COM 
506610922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
506710922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
506810922SJeff.Bonwick@Sun.COM 
506910922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
507010922SJeff.Bonwick@Sun.COM }
507110922SJeff.Bonwick@Sun.COM 
5072789Sahrens /*
5073789Sahrens  * Kick off threads to run tests on all datasets in parallel.
5074789Sahrens  */
5075789Sahrens static void
507610922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
5077789Sahrens {
507810922SJeff.Bonwick@Sun.COM 	thread_t *tid;
5079789Sahrens 	spa_t *spa;
50807754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
508110922SJeff.Bonwick@Sun.COM 	int error;
50827754SJeff.Bonwick@Sun.COM 
50837754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
5084789Sahrens 
508510922SJeff.Bonwick@Sun.COM 	/*
508610922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
508710922SJeff.Bonwick@Sun.COM 	 */
508810922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
508910922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
509010922SJeff.Bonwick@Sun.COM 
509110922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
509210922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
509310922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
509410922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
509510922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
509610922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
509710922SJeff.Bonwick@Sun.COM 
509810922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
509910612SRicardo.M.Correia@Sun.COM 
510010612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
510110612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
510210612SRicardo.M.Correia@Sun.COM 
5103789Sahrens 	/*
51047754SJeff.Bonwick@Sun.COM 	 * Open our pool.
51055329Sgw25295 	 */
510610922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
510710922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
510810922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
510910922SJeff.Bonwick@Sun.COM 
511010922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
51115329Sgw25295 
51125329Sgw25295 	/*
51138241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
51148241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
51158241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
51168241SJeff.Bonwick@Sun.COM 	 */
511711422SMark.Musante@Sun.COM 	if (MAXFAULTS() == 0)
51188241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
51198241SJeff.Bonwick@Sun.COM 	else
51208241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
51218241SJeff.Bonwick@Sun.COM 
51228241SJeff.Bonwick@Sun.COM 	/*
51237754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5124789Sahrens 	 */
51258241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
51267754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5127789Sahrens 
5128789Sahrens 	/*
512910922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
513010922SJeff.Bonwick@Sun.COM 	 */
513110922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
513210922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
513310922SJeff.Bonwick@Sun.COM 
513410922SJeff.Bonwick@Sun.COM 	/*
5135789Sahrens 	 * Verify that we can safely inquire about about any object,
5136789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5137789Sahrens 	 * we probe a 5-wide window around each power of two.
5138789Sahrens 	 * This hits all edge cases, including zero and the max.
5139789Sahrens 	 */
514010922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
514110922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5142789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5143789Sahrens 			    (1ULL << t) + d, NULL);
51441544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
51451544Seschrock 			    error == EINVAL);
5146789Sahrens 		}
5147789Sahrens 	}
5148789Sahrens 
5149789Sahrens 	/*
515010922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5151789Sahrens 	 */
515210922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
515310922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
515410922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
515510922SJeff.Bonwick@Sun.COM 	}
5156789Sahrens 	zs->zs_enospc_count = 0;
5157789Sahrens 
515810922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5159789Sahrens 
5160789Sahrens 	if (zopt_verbose >= 4)
5161789Sahrens 		(void) printf("starting main threads...\n");
5162789Sahrens 
516310922SJeff.Bonwick@Sun.COM 	/*
516410922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
516510922SJeff.Bonwick@Sun.COM 	 */
516610922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
516710922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
516810922SJeff.Bonwick@Sun.COM 			return;
516910922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
517010922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5171789Sahrens 	}
5172789Sahrens 
5173789Sahrens 	/*
517410922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
517510922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5176789Sahrens 	 */
517710922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
517810922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
517910922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
518010922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5181789Sahrens 	}
5182789Sahrens 
51831544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5184789Sahrens 
518510922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
518610922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
518710922SJeff.Bonwick@Sun.COM 
518810922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
51897754SJeff.Bonwick@Sun.COM 
51907754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
51917754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
51927754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
51938241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
51947754SJeff.Bonwick@Sun.COM 
5195789Sahrens 	/*
5196789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5197789Sahrens 	 * spa_close() should wait for it to complete.
5198789Sahrens 	 */
519910922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
520010922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5201789Sahrens 
52025530Sbonwick 	spa_close(spa, FTAG);
52035530Sbonwick 
520410922SJeff.Bonwick@Sun.COM 	/*
520510922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
520610922SJeff.Bonwick@Sun.COM 	 */
520710922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
520810922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
520910922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
521010922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
521110922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
521210922SJeff.Bonwick@Sun.COM 
521310922SJeff.Bonwick@Sun.COM 	/*
521410922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
521510922SJeff.Bonwick@Sun.COM 	 * different name.
521610922SJeff.Bonwick@Sun.COM 	 */
521710922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
521810922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
521910922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
522010922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
522110922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
522210922SJeff.Bonwick@Sun.COM 	}
522310922SJeff.Bonwick@Sun.COM 
522410922SJeff.Bonwick@Sun.COM 	kernel_fini();
522510922SJeff.Bonwick@Sun.COM }
522610922SJeff.Bonwick@Sun.COM 
522710922SJeff.Bonwick@Sun.COM static void
522810922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
522910922SJeff.Bonwick@Sun.COM {
523010922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
523110922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5232*11818SMark.Musante@Sun.COM 	int numloops = 0;
523310922SJeff.Bonwick@Sun.COM 
523410922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
523510922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
523610922SJeff.Bonwick@Sun.COM 
523710922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
523810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
523910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
524010922SJeff.Bonwick@Sun.COM 
524110922SJeff.Bonwick@Sun.COM 	/*
524210922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
524310922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
524410922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
524510922SJeff.Bonwick@Sun.COM 	 */
524610922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
524710922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
524810922SJeff.Bonwick@Sun.COM 		zil_commit(zd->zd_zilog, UINT64_MAX, 0);
524910922SJeff.Bonwick@Sun.COM 	}
525010922SJeff.Bonwick@Sun.COM 
525110922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
525210922SJeff.Bonwick@Sun.COM 
525310922SJeff.Bonwick@Sun.COM 	/*
525410922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
525510922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
525610922SJeff.Bonwick@Sun.COM 	 */
525710922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
525810922SJeff.Bonwick@Sun.COM 
525910922SJeff.Bonwick@Sun.COM 	/*
526010922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
526110922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
526210922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
526310922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
526410922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
526510922SJeff.Bonwick@Sun.COM 	 */
5266*11818SMark.Musante@Sun.COM 	while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
526710922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
526810922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
526910922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
527010922SJeff.Bonwick@Sun.COM 	}
527110922SJeff.Bonwick@Sun.COM 
527210922SJeff.Bonwick@Sun.COM 	/*
527310922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
527410922SJeff.Bonwick@Sun.COM 	 */
527510922SJeff.Bonwick@Sun.COM 	zil_commit(zd->zd_zilog, UINT64_MAX, 0);
527610922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
527710922SJeff.Bonwick@Sun.COM 
527810922SJeff.Bonwick@Sun.COM 	/*
527910922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
528010922SJeff.Bonwick@Sun.COM 	 */
528110922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
528210922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
528310922SJeff.Bonwick@Sun.COM 	kernel_fini();
528410922SJeff.Bonwick@Sun.COM 
528510922SJeff.Bonwick@Sun.COM 	/*
528610922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
528710922SJeff.Bonwick@Sun.COM 	 */
528810922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
528910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
529010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
529110922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
529210922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5293789Sahrens 	kernel_fini();
529410612SRicardo.M.Correia@Sun.COM 
529510612SRicardo.M.Correia@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
529610612SRicardo.M.Correia@Sun.COM 
529710612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
529810612SRicardo.M.Correia@Sun.COM 
529910612SRicardo.M.Correia@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
530010612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5301789Sahrens }
5302789Sahrens 
5303789Sahrens void
5304789Sahrens print_time(hrtime_t t, char *timebuf)
5305789Sahrens {
5306789Sahrens 	hrtime_t s = t / NANOSEC;
5307789Sahrens 	hrtime_t m = s / 60;
5308789Sahrens 	hrtime_t h = m / 60;
5309789Sahrens 	hrtime_t d = h / 24;
5310789Sahrens 
5311789Sahrens 	s -= m * 60;
5312789Sahrens 	m -= h * 60;
5313789Sahrens 	h -= d * 24;
5314789Sahrens 
5315789Sahrens 	timebuf[0] = '\0';
5316789Sahrens 
5317789Sahrens 	if (d)
5318789Sahrens 		(void) sprintf(timebuf,
5319789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5320789Sahrens 	else if (h)
5321789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5322789Sahrens 	else if (m)
5323789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5324789Sahrens 	else
5325789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5326789Sahrens }
5327789Sahrens 
532811422SMark.Musante@Sun.COM static nvlist_t *
532911422SMark.Musante@Sun.COM make_random_props()
533011422SMark.Musante@Sun.COM {
533111422SMark.Musante@Sun.COM 	nvlist_t *props;
533211422SMark.Musante@Sun.COM 
533311422SMark.Musante@Sun.COM 	if (ztest_random(2) == 0)
533411422SMark.Musante@Sun.COM 		return (NULL);
533511422SMark.Musante@Sun.COM 
533611422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
533711422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
533811422SMark.Musante@Sun.COM 
533911422SMark.Musante@Sun.COM 	(void) printf("props:\n");
534011422SMark.Musante@Sun.COM 	dump_nvlist(props, 4);
534111422SMark.Musante@Sun.COM 
534211422SMark.Musante@Sun.COM 	return (props);
534311422SMark.Musante@Sun.COM }
534411422SMark.Musante@Sun.COM 
5345789Sahrens /*
5346789Sahrens  * Create a storage pool with the given name and initial vdev size.
534710922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5348789Sahrens  */
5349789Sahrens static void
535010922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5351789Sahrens {
5352789Sahrens 	spa_t *spa;
535311422SMark.Musante@Sun.COM 	nvlist_t *nvroot, *props;
5354789Sahrens 
535510922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
535610922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
535710922SJeff.Bonwick@Sun.COM 
5358789Sahrens 	kernel_init(FREAD | FWRITE);
5359789Sahrens 
5360789Sahrens 	/*
5361789Sahrens 	 * Create the storage pool.
5362789Sahrens 	 */
536310922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
536410594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
536511422SMark.Musante@Sun.COM 	zs->zs_splits = 0;
536611422SMark.Musante@Sun.COM 	zs->zs_mirrors = zopt_mirrors;
53677754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
536811422SMark.Musante@Sun.COM 	    0, zopt_raidz, zs->zs_mirrors, 1);
536911422SMark.Musante@Sun.COM 	props = make_random_props();
537011422SMark.Musante@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5371789Sahrens 	nvlist_free(nvroot);
5372789Sahrens 
537310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
53749480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5375789Sahrens 	spa_close(spa, FTAG);
5376789Sahrens 
5377789Sahrens 	kernel_fini();
537810922SJeff.Bonwick@Sun.COM 
537910922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
538010922SJeff.Bonwick@Sun.COM 
538110922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
538210922SJeff.Bonwick@Sun.COM 
538310922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5384789Sahrens }
5385789Sahrens 
5386789Sahrens int
5387789Sahrens main(int argc, char **argv)
5388789Sahrens {
5389789Sahrens 	int kills = 0;
5390789Sahrens 	int iters = 0;
5391789Sahrens 	ztest_shared_t *zs;
539210922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5393789Sahrens 	ztest_info_t *zi;
5394789Sahrens 	char timebuf[100];
5395789Sahrens 	char numbuf[6];
539610922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5397789Sahrens 
5398789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5399789Sahrens 
5400789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5401789Sahrens 
5402789Sahrens 	process_options(argc, argv);
5403789Sahrens 
540411811SJeff.Bonwick@Sun.COM 	/* Override location of zpool.cache */
540511811SJeff.Bonwick@Sun.COM 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
540611811SJeff.Bonwick@Sun.COM 
54071544Seschrock 	/*
54081544Seschrock 	 * Blow away any existing copy of zpool.cache
54091544Seschrock 	 */
54101544Seschrock 	if (zopt_init != 0)
541111811SJeff.Bonwick@Sun.COM 		(void) remove(spa_config_path);
54121544Seschrock 
541310922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
541410922SJeff.Bonwick@Sun.COM 
5415789Sahrens 	zs = ztest_shared = (void *)mmap(0,
541610922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5417789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5418789Sahrens 
5419789Sahrens 	if (zopt_verbose >= 1) {
5420789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5421789Sahrens 		    " %llu seconds...\n",
54221732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5423789Sahrens 		    (u_longlong_t)zopt_time);
5424789Sahrens 	}
5425789Sahrens 
5426789Sahrens 	/*
5427789Sahrens 	 * Create and initialize our storage pool.
5428789Sahrens 	 */
542910922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5430789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5431789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5432789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
543310922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
543410922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5435789Sahrens 	}
5436789Sahrens 
543710922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
543810922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
543910922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
544010922SJeff.Bonwick@Sun.COM 
544110922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5442789Sahrens 		zi = &zs->zs_info[f];
5443789Sahrens 		*zi = ztest_info[f];
544410922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
544510922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5446789Sahrens 		else
544710922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
544810922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5449789Sahrens 	}
5450789Sahrens 
5451789Sahrens 	/*
5452789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5453789Sahrens 	 * to verify that self-healing data works, and forced crashes
5454789Sahrens 	 * to verify that we never lose on-disk consistency.
5455789Sahrens 	 */
545610922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5457789Sahrens 		int status;
5458789Sahrens 		pid_t pid;
5459789Sahrens 
5460789Sahrens 		/*
5461789Sahrens 		 * Initialize the workload counters for each function.
5462789Sahrens 		 */
546310922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5464789Sahrens 			zi = &zs->zs_info[f];
546510922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5466789Sahrens 			zi->zi_call_time = 0;
5467789Sahrens 		}
5468789Sahrens 
54699480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
54709480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
54719480SGeorge.Wilson@Sun.COM 
5472789Sahrens 		pid = fork();
5473789Sahrens 
5474789Sahrens 		if (pid == -1)
5475789Sahrens 			fatal(1, "fork failed");
5476789Sahrens 
5477789Sahrens 		if (pid == 0) {	/* child */
5478789Sahrens 			struct rlimit rl = { 1024, 1024 };
5479789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
54801914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
548110922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5482789Sahrens 			exit(0);
5483789Sahrens 		}
5484789Sahrens 
54852856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5486789Sahrens 			continue;
5487789Sahrens 
5488789Sahrens 		if (WIFEXITED(status)) {
5489789Sahrens 			if (WEXITSTATUS(status) != 0) {
5490789Sahrens 				(void) fprintf(stderr,
5491789Sahrens 				    "child exited with code %d\n",
5492789Sahrens 				    WEXITSTATUS(status));
5493789Sahrens 				exit(2);
5494789Sahrens 			}
54952856Snd150628 		} else if (WIFSIGNALED(status)) {
5496789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5497789Sahrens 				(void) fprintf(stderr,
5498789Sahrens 				    "child died with signal %d\n",
5499789Sahrens 				    WTERMSIG(status));
5500789Sahrens 				exit(3);
5501789Sahrens 			}
5502789Sahrens 			kills++;
55032856Snd150628 		} else {
55042856Snd150628 			(void) fprintf(stderr, "something strange happened "
55052856Snd150628 			    "to child\n");
55062856Snd150628 			exit(4);
5507789Sahrens 		}
5508789Sahrens 
5509789Sahrens 		iters++;
5510789Sahrens 
5511789Sahrens 		if (zopt_verbose >= 1) {
5512789Sahrens 			hrtime_t now = gethrtime();
5513789Sahrens 
551410922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
551510922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5516789Sahrens 			nicenum(zs->zs_space, numbuf);
5517789Sahrens 
5518789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5519789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5520789Sahrens 			    iters,
5521789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5522789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5523789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5524789Sahrens 			    numbuf,
552510922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5526789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5527789Sahrens 		}
5528789Sahrens 
5529789Sahrens 		if (zopt_verbose >= 2) {
5530789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5531789Sahrens 			(void) printf("%7s %9s   %s\n",
5532789Sahrens 			    "Calls", "Time", "Function");
5533789Sahrens 			(void) printf("%7s %9s   %s\n",
5534789Sahrens 			    "-----", "----", "--------");
553510922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5536789Sahrens 				Dl_info dli;
5537789Sahrens 
5538789Sahrens 				zi = &zs->zs_info[f];
5539789Sahrens 				print_time(zi->zi_call_time, timebuf);
5540789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5541789Sahrens 				(void) printf("%7llu %9s   %s\n",
554210922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5543789Sahrens 				    dli.dli_sname);
5544789Sahrens 			}
5545789Sahrens 			(void) printf("\n");
5546789Sahrens 		}
5547789Sahrens 
5548789Sahrens 		/*
554910922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
555010922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
555110922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5552789Sahrens 		 */
555310922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
555410922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
555510922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
555610922SJeff.Bonwick@Sun.COM 		} else {
555710922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
555810922SJeff.Bonwick@Sun.COM 			kernel_fini();
555910922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
556010922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
556110922SJeff.Bonwick@Sun.COM 			    zopt_pool);
556210922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
556310922SJeff.Bonwick@Sun.COM 		}
5564789Sahrens 		kernel_fini();
556510922SJeff.Bonwick@Sun.COM 
556610922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5567789Sahrens 	}
5568789Sahrens 
5569789Sahrens 	if (zopt_verbose >= 1) {
5570789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5571789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5572789Sahrens 	}
5573789Sahrens 
5574789Sahrens 	return (0);
5575789Sahrens }
5576