xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 12527)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51485Slling  * Common Development and Distribution License (the "License").
61485Slling  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212285SJeff.Bonwick@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens /*
26789Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
27789Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
28789Sahrens  *
29789Sahrens  * The overall design of the ztest program is as follows:
30789Sahrens  *
31789Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
32789Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
33789Sahrens  *     we have a simple routine to test that functionality.  These
34789Sahrens  *     individual routines do not have to do anything "stressful".
35789Sahrens  *
36789Sahrens  * (2) We turn these simple functionality tests into a stress test by
37789Sahrens  *     running them all in parallel, with as many threads as desired,
38789Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
39789Sahrens  *
40789Sahrens  * (3) While all this is happening, we inject faults into the pool to
41789Sahrens  *     verify that self-healing data really works.
42789Sahrens  *
43789Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
44789Sahrens  *     functions.  Thus even individual objects vary from block to block
45789Sahrens  *     in which checksum they use and whether they're compressed.
46789Sahrens  *
47789Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
48789Sahrens  *     we run the entire test in a child of the main process.
49789Sahrens  *     At random times, the child self-immolates with a SIGKILL.
50789Sahrens  *     This is the software equivalent of pulling the power cord.
51789Sahrens  *     The parent then runs the test again, using the existing
52789Sahrens  *     storage pool, as many times as desired.
53789Sahrens  *
54789Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
55789Sahrens  *     many of the functional tests record the transaction group number
56789Sahrens  *     as part of their data.  When reading old data, they verify that
57789Sahrens  *     the transaction group number is less than the current, open txg.
58789Sahrens  *     If you add a new test, please do this if applicable.
59789Sahrens  *
60789Sahrens  * When run with no arguments, ztest runs for about five minutes and
61789Sahrens  * produces no output if successful.  To get a little bit of information,
62789Sahrens  * specify -V.  To get more information, specify -VV, and so on.
63789Sahrens  *
64789Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
65789Sahrens  *
66789Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
67789Sahrens  * to increase the pool capacity, fanout, and overall stress level.
68789Sahrens  *
69789Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
70789Sahrens  * This can be useful when you're trying to distinguish temporal incursions
71789Sahrens  * from plain old race conditions.
72789Sahrens  */
73789Sahrens 
74789Sahrens #include <sys/zfs_context.h>
75789Sahrens #include <sys/spa.h>
76789Sahrens #include <sys/dmu.h>
77789Sahrens #include <sys/txg.h>
789412SAleksandr.Guzovskiy@Sun.COM #include <sys/dbuf.h>
79789Sahrens #include <sys/zap.h>
80789Sahrens #include <sys/dmu_objset.h>
81789Sahrens #include <sys/poll.h>
82789Sahrens #include <sys/stat.h>
83789Sahrens #include <sys/time.h>
84789Sahrens #include <sys/wait.h>
85789Sahrens #include <sys/mman.h>
86789Sahrens #include <sys/resource.h>
87789Sahrens #include <sys/zio.h>
88789Sahrens #include <sys/zil.h>
8910922SJeff.Bonwick@Sun.COM #include <sys/zil_impl.h>
90789Sahrens #include <sys/vdev_impl.h>
917754SJeff.Bonwick@Sun.COM #include <sys/vdev_file.h>
92789Sahrens #include <sys/spa_impl.h>
9310594SGeorge.Wilson@Sun.COM #include <sys/metaslab_impl.h>
94789Sahrens #include <sys/dsl_prop.h>
958779SMark.Musante@Sun.COM #include <sys/dsl_dataset.h>
9612296SLin.Ling@Sun.COM #include <sys/dsl_scan.h>
9712470SMatthew.Ahrens@Sun.COM #include <sys/zio_checksum.h>
98789Sahrens #include <sys/refcount.h>
99789Sahrens #include <stdio.h>
1001914Scasper #include <stdio_ext.h>
101789Sahrens #include <stdlib.h>
102789Sahrens #include <unistd.h>
103789Sahrens #include <signal.h>
104789Sahrens #include <umem.h>
105789Sahrens #include <dlfcn.h>
106789Sahrens #include <ctype.h>
107789Sahrens #include <math.h>
108789Sahrens #include <sys/fs/zfs.h>
10910922SJeff.Bonwick@Sun.COM #include <libnvpair.h>
110789Sahrens 
111789Sahrens static char cmdname[] = "ztest";
112789Sahrens static char *zopt_pool = cmdname;
113789Sahrens 
114789Sahrens static uint64_t zopt_vdevs = 5;
115789Sahrens static uint64_t zopt_vdevtime;
1161732Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT;
117789Sahrens static int zopt_mirrors = 2;
118789Sahrens static int zopt_raidz = 4;
1192082Seschrock static int zopt_raidz_parity = 1;
120789Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE;
1211732Sbonwick static int zopt_datasets = 7;
122789Sahrens static int zopt_threads = 23;
123789Sahrens static uint64_t zopt_passtime = 60;	/* 60 seconds */
124789Sahrens static uint64_t zopt_killrate = 70;	/* 70% kill rate */
125789Sahrens static int zopt_verbose = 0;
126789Sahrens static int zopt_init = 1;
127789Sahrens static char *zopt_dir = "/tmp";
128789Sahrens static uint64_t zopt_time = 300;	/* 5 minutes */
12911818SMark.Musante@Sun.COM static uint64_t zopt_maxloops = 50;	/* max loops during spa_freeze() */
130789Sahrens 
13110922SJeff.Bonwick@Sun.COM #define	BT_MAGIC	0x123456789abcdefULL
13211422SMark.Musante@Sun.COM #define	MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
13310922SJeff.Bonwick@Sun.COM 
13410922SJeff.Bonwick@Sun.COM enum ztest_io_type {
13510922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_TAG,
13610922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_PATTERN,
13710922SJeff.Bonwick@Sun.COM 	ZTEST_IO_WRITE_ZEROES,
13810922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TRUNCATE,
13910922SJeff.Bonwick@Sun.COM 	ZTEST_IO_SETATTR,
14010922SJeff.Bonwick@Sun.COM 	ZTEST_IO_TYPES
14110922SJeff.Bonwick@Sun.COM };
14210922SJeff.Bonwick@Sun.COM 
1435530Sbonwick typedef struct ztest_block_tag {
14410922SJeff.Bonwick@Sun.COM 	uint64_t	bt_magic;
1455530Sbonwick 	uint64_t	bt_objset;
1465530Sbonwick 	uint64_t	bt_object;
1475530Sbonwick 	uint64_t	bt_offset;
14810922SJeff.Bonwick@Sun.COM 	uint64_t	bt_gen;
1495530Sbonwick 	uint64_t	bt_txg;
15010922SJeff.Bonwick@Sun.COM 	uint64_t	bt_crtxg;
1515530Sbonwick } ztest_block_tag_t;
1525530Sbonwick 
15310922SJeff.Bonwick@Sun.COM typedef struct bufwad {
15410922SJeff.Bonwick@Sun.COM 	uint64_t	bw_index;
15510922SJeff.Bonwick@Sun.COM 	uint64_t	bw_txg;
15610922SJeff.Bonwick@Sun.COM 	uint64_t	bw_data;
15710922SJeff.Bonwick@Sun.COM } bufwad_t;
15810922SJeff.Bonwick@Sun.COM 
15910922SJeff.Bonwick@Sun.COM /*
16010922SJeff.Bonwick@Sun.COM  * XXX -- fix zfs range locks to be generic so we can use them here.
16110922SJeff.Bonwick@Sun.COM  */
16210922SJeff.Bonwick@Sun.COM typedef enum {
16310922SJeff.Bonwick@Sun.COM 	RL_READER,
16410922SJeff.Bonwick@Sun.COM 	RL_WRITER,
16510922SJeff.Bonwick@Sun.COM 	RL_APPEND
16610922SJeff.Bonwick@Sun.COM } rl_type_t;
16710922SJeff.Bonwick@Sun.COM 
16810922SJeff.Bonwick@Sun.COM typedef struct rll {
16910922SJeff.Bonwick@Sun.COM 	void		*rll_writer;
17010922SJeff.Bonwick@Sun.COM 	int		rll_readers;
17110922SJeff.Bonwick@Sun.COM 	mutex_t		rll_lock;
17210922SJeff.Bonwick@Sun.COM 	cond_t		rll_cv;
17310922SJeff.Bonwick@Sun.COM } rll_t;
17410922SJeff.Bonwick@Sun.COM 
17510922SJeff.Bonwick@Sun.COM typedef struct rl {
17610922SJeff.Bonwick@Sun.COM 	uint64_t	rl_object;
17710922SJeff.Bonwick@Sun.COM 	uint64_t	rl_offset;
17810922SJeff.Bonwick@Sun.COM 	uint64_t	rl_size;
17910922SJeff.Bonwick@Sun.COM 	rll_t		*rl_lock;
18010922SJeff.Bonwick@Sun.COM } rl_t;
18110922SJeff.Bonwick@Sun.COM 
18210922SJeff.Bonwick@Sun.COM #define	ZTEST_RANGE_LOCKS	64
18310922SJeff.Bonwick@Sun.COM #define	ZTEST_OBJECT_LOCKS	64
18410922SJeff.Bonwick@Sun.COM 
18510922SJeff.Bonwick@Sun.COM /*
18610922SJeff.Bonwick@Sun.COM  * Object descriptor.  Used as a template for object lookup/create/remove.
18710922SJeff.Bonwick@Sun.COM  */
18810922SJeff.Bonwick@Sun.COM typedef struct ztest_od {
18910922SJeff.Bonwick@Sun.COM 	uint64_t	od_dir;
19010922SJeff.Bonwick@Sun.COM 	uint64_t	od_object;
19110922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_type;
19210922SJeff.Bonwick@Sun.COM 	dmu_object_type_t od_crtype;
19310922SJeff.Bonwick@Sun.COM 	uint64_t	od_blocksize;
19410922SJeff.Bonwick@Sun.COM 	uint64_t	od_crblocksize;
19510922SJeff.Bonwick@Sun.COM 	uint64_t	od_gen;
19610922SJeff.Bonwick@Sun.COM 	uint64_t	od_crgen;
19710922SJeff.Bonwick@Sun.COM 	char		od_name[MAXNAMELEN];
19810922SJeff.Bonwick@Sun.COM } ztest_od_t;
19910922SJeff.Bonwick@Sun.COM 
20010922SJeff.Bonwick@Sun.COM /*
20110922SJeff.Bonwick@Sun.COM  * Per-dataset state.
20210922SJeff.Bonwick@Sun.COM  */
20310922SJeff.Bonwick@Sun.COM typedef struct ztest_ds {
20410922SJeff.Bonwick@Sun.COM 	objset_t	*zd_os;
20510922SJeff.Bonwick@Sun.COM 	zilog_t		*zd_zilog;
20610922SJeff.Bonwick@Sun.COM 	uint64_t	zd_seq;
20710922SJeff.Bonwick@Sun.COM 	ztest_od_t	*zd_od;		/* debugging aid */
20810922SJeff.Bonwick@Sun.COM 	char		zd_name[MAXNAMELEN];
20910922SJeff.Bonwick@Sun.COM 	mutex_t		zd_dirobj_lock;
21010922SJeff.Bonwick@Sun.COM 	rll_t		zd_object_lock[ZTEST_OBJECT_LOCKS];
21110922SJeff.Bonwick@Sun.COM 	rll_t		zd_range_lock[ZTEST_RANGE_LOCKS];
21210922SJeff.Bonwick@Sun.COM } ztest_ds_t;
21310922SJeff.Bonwick@Sun.COM 
21410922SJeff.Bonwick@Sun.COM /*
21510922SJeff.Bonwick@Sun.COM  * Per-iteration state.
21610922SJeff.Bonwick@Sun.COM  */
21710922SJeff.Bonwick@Sun.COM typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
21810922SJeff.Bonwick@Sun.COM 
21910922SJeff.Bonwick@Sun.COM typedef struct ztest_info {
22010922SJeff.Bonwick@Sun.COM 	ztest_func_t	*zi_func;	/* test function */
22110922SJeff.Bonwick@Sun.COM 	uint64_t	zi_iters;	/* iterations per execution */
22210922SJeff.Bonwick@Sun.COM 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
22310922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_count;	/* per-pass count */
22410922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_time;	/* per-pass time */
22510922SJeff.Bonwick@Sun.COM 	uint64_t	zi_call_next;	/* next time to call this function */
22610922SJeff.Bonwick@Sun.COM } ztest_info_t;
227789Sahrens 
228789Sahrens /*
229789Sahrens  * Note: these aren't static because we want dladdr() to work.
230789Sahrens  */
231789Sahrens ztest_func_t ztest_dmu_read_write;
232789Sahrens ztest_func_t ztest_dmu_write_parallel;
233789Sahrens ztest_func_t ztest_dmu_object_alloc_free;
23410612SRicardo.M.Correia@Sun.COM ztest_func_t ztest_dmu_commit_callbacks;
235789Sahrens ztest_func_t ztest_zap;
23610922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zap_parallel;
23710922SJeff.Bonwick@Sun.COM ztest_func_t ztest_zil_commit;
23810922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_read_write_zcopy;
23910922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_objset_create_destroy;
24010922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_prealloc;
24110431SSanjeev.Bagewadi@Sun.COM ztest_func_t ztest_fzap;
24210922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_create_destroy;
243789Sahrens ztest_func_t ztest_dsl_prop_get_set;
24410922SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_prop_get_set;
245789Sahrens ztest_func_t ztest_spa_create_destroy;
246789Sahrens ztest_func_t ztest_fault_inject;
24710922SJeff.Bonwick@Sun.COM ztest_func_t ztest_ddt_repair;
24810922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dmu_snapshot_hold;
2497754SJeff.Bonwick@Sun.COM ztest_func_t ztest_spa_rename;
25010922SJeff.Bonwick@Sun.COM ztest_func_t ztest_scrub;
25110922SJeff.Bonwick@Sun.COM ztest_func_t ztest_dsl_dataset_promote_busy;
252789Sahrens ztest_func_t ztest_vdev_attach_detach;
253789Sahrens ztest_func_t ztest_vdev_LUN_growth;
254789Sahrens ztest_func_t ztest_vdev_add_remove;
2557754SJeff.Bonwick@Sun.COM ztest_func_t ztest_vdev_aux_add_remove;
25611422SMark.Musante@Sun.COM ztest_func_t ztest_split_pool;
25710922SJeff.Bonwick@Sun.COM 
25810922SJeff.Bonwick@Sun.COM uint64_t zopt_always = 0ULL * NANOSEC;		/* all the time */
25910922SJeff.Bonwick@Sun.COM uint64_t zopt_incessant = 1ULL * NANOSEC / 10;	/* every 1/10 second */
26010922SJeff.Bonwick@Sun.COM uint64_t zopt_often = 1ULL * NANOSEC;		/* every second */
26110922SJeff.Bonwick@Sun.COM uint64_t zopt_sometimes = 10ULL * NANOSEC;	/* every 10 seconds */
26210922SJeff.Bonwick@Sun.COM uint64_t zopt_rarely = 60ULL * NANOSEC;		/* every 60 seconds */
263789Sahrens 
264789Sahrens ztest_info_t ztest_info[] = {
2655530Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
26610922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_write_parallel,		10,	&zopt_always	},
2675530Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
26810922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_commit_callbacks,		1,	&zopt_always	},
2695530Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
2705530Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
27111422SMark.Musante@Sun.COM 	{ ztest_split_pool,			1,	&zopt_always	},
27210922SJeff.Bonwick@Sun.COM 	{ ztest_zil_commit,			1,	&zopt_incessant	},
27310922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_often	},
27410922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_often	},
27510922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_prop_get_set,		1,	&zopt_often	},
27610922SJeff.Bonwick@Sun.COM 	{ ztest_spa_prop_get_set,		1,	&zopt_sometimes	},
27710922SJeff.Bonwick@Sun.COM #if 0
27810922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_prealloc,			1,	&zopt_sometimes	},
27910922SJeff.Bonwick@Sun.COM #endif
28010922SJeff.Bonwick@Sun.COM 	{ ztest_fzap,				1,	&zopt_sometimes	},
28110922SJeff.Bonwick@Sun.COM 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes	},
28210922SJeff.Bonwick@Sun.COM 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
2835530Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
28410922SJeff.Bonwick@Sun.COM 	{ ztest_ddt_repair,			1,	&zopt_sometimes	},
28510693Schris.kirby@sun.com 	{ ztest_dmu_snapshot_hold,		1,	&zopt_sometimes	},
2865530Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
28710922SJeff.Bonwick@Sun.COM 	{ ztest_scrub,				1,	&zopt_rarely	},
28810922SJeff.Bonwick@Sun.COM 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
28912296SLin.Ling@Sun.COM 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely },
2907754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
29112296SLin.Ling@Sun.COM 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime },
2927754SJeff.Bonwick@Sun.COM 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
293789Sahrens };
294789Sahrens 
295789Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
296789Sahrens 
297789Sahrens /*
29810612SRicardo.M.Correia@Sun.COM  * The following struct is used to hold a list of uncalled commit callbacks.
29910612SRicardo.M.Correia@Sun.COM  * The callbacks are ordered by txg number.
30010612SRicardo.M.Correia@Sun.COM  */
30110612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_list {
30210612SRicardo.M.Correia@Sun.COM 	mutex_t	zcl_callbacks_lock;
30310612SRicardo.M.Correia@Sun.COM 	list_t	zcl_callbacks;
30410612SRicardo.M.Correia@Sun.COM } ztest_cb_list_t;
30510612SRicardo.M.Correia@Sun.COM 
30610612SRicardo.M.Correia@Sun.COM /*
307789Sahrens  * Stuff we need to share writably between parent and child.
308789Sahrens  */
309789Sahrens typedef struct ztest_shared {
31010922SJeff.Bonwick@Sun.COM 	char		*zs_pool;
31110922SJeff.Bonwick@Sun.COM 	spa_t		*zs_spa;
31210922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_start;
31310922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_proc_stop;
31410922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_start;
31510922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_stop;
31610922SJeff.Bonwick@Sun.COM 	hrtime_t	zs_thread_kill;
31710922SJeff.Bonwick@Sun.COM 	uint64_t	zs_enospc_count;
31810594SGeorge.Wilson@Sun.COM 	uint64_t	zs_vdev_next_leaf;
3197754SJeff.Bonwick@Sun.COM 	uint64_t	zs_vdev_aux;
320789Sahrens 	uint64_t	zs_alloc;
321789Sahrens 	uint64_t	zs_space;
32210922SJeff.Bonwick@Sun.COM 	mutex_t		zs_vdev_lock;
32310922SJeff.Bonwick@Sun.COM 	rwlock_t	zs_name_lock;
324789Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
32511422SMark.Musante@Sun.COM 	uint64_t	zs_splits;
32611422SMark.Musante@Sun.COM 	uint64_t	zs_mirrors;
32710922SJeff.Bonwick@Sun.COM 	ztest_ds_t	zs_zd[];
328789Sahrens } ztest_shared_t;
329789Sahrens 
33010922SJeff.Bonwick@Sun.COM #define	ID_PARALLEL	-1ULL
33110922SJeff.Bonwick@Sun.COM 
332789Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
3337754SJeff.Bonwick@Sun.COM static char ztest_aux_template[] = "%s/%s.%s.%llu";
33410922SJeff.Bonwick@Sun.COM ztest_shared_t *ztest_shared;
33510922SJeff.Bonwick@Sun.COM uint64_t *ztest_seq;
336789Sahrens 
337789Sahrens static int ztest_random_fd;
338789Sahrens static int ztest_dump_core = 1;
339789Sahrens 
3407754SJeff.Bonwick@Sun.COM static boolean_t ztest_exiting;
3415329Sgw25295 
34210612SRicardo.M.Correia@Sun.COM /* Global commit callback list */
34310612SRicardo.M.Correia@Sun.COM static ztest_cb_list_t zcl;
34410612SRicardo.M.Correia@Sun.COM 
3455530Sbonwick extern uint64_t metaslab_gang_bang;
3469480SGeorge.Wilson@Sun.COM extern uint64_t metaslab_df_alloc_threshold;
34710922SJeff.Bonwick@Sun.COM static uint64_t metaslab_sz;
34810922SJeff.Bonwick@Sun.COM 
34910922SJeff.Bonwick@Sun.COM enum ztest_object {
35010922SJeff.Bonwick@Sun.COM 	ZTEST_META_DNODE = 0,
35110922SJeff.Bonwick@Sun.COM 	ZTEST_DIROBJ,
35210922SJeff.Bonwick@Sun.COM 	ZTEST_OBJECTS
35310922SJeff.Bonwick@Sun.COM };
354789Sahrens 
3554008Sraf static void usage(boolean_t) __NORETURN;
3563972Svb160487 
357789Sahrens /*
358789Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
359789Sahrens  * debugging facilities.
360789Sahrens  */
361789Sahrens const char *
362789Sahrens _umem_debug_init()
363789Sahrens {
364789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
365789Sahrens }
366789Sahrens 
367789Sahrens const char *
368789Sahrens _umem_logging_init(void)
369789Sahrens {
370789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
371789Sahrens }
372789Sahrens 
373789Sahrens #define	FATAL_MSG_SZ	1024
374789Sahrens 
375789Sahrens char *fatal_msg;
376789Sahrens 
377789Sahrens static void
378789Sahrens fatal(int do_perror, char *message, ...)
379789Sahrens {
380789Sahrens 	va_list args;
381789Sahrens 	int save_errno = errno;
382789Sahrens 	char buf[FATAL_MSG_SZ];
383789Sahrens 
384789Sahrens 	(void) fflush(stdout);
385789Sahrens 
386789Sahrens 	va_start(args, message);
387789Sahrens 	(void) sprintf(buf, "ztest: ");
388789Sahrens 	/* LINTED */
389789Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
390789Sahrens 	va_end(args);
391789Sahrens 	if (do_perror) {
392789Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
393789Sahrens 		    ": %s", strerror(save_errno));
394789Sahrens 	}
395789Sahrens 	(void) fprintf(stderr, "%s\n", buf);
396789Sahrens 	fatal_msg = buf;			/* to ease debugging */
397789Sahrens 	if (ztest_dump_core)
398789Sahrens 		abort();
399789Sahrens 	exit(3);
400789Sahrens }
401789Sahrens 
402789Sahrens static int
403789Sahrens str2shift(const char *buf)
404789Sahrens {
405789Sahrens 	const char *ends = "BKMGTPEZ";
406789Sahrens 	int i;
407789Sahrens 
408789Sahrens 	if (buf[0] == '\0')
409789Sahrens 		return (0);
410789Sahrens 	for (i = 0; i < strlen(ends); i++) {
411789Sahrens 		if (toupper(buf[0]) == ends[i])
412789Sahrens 			break;
413789Sahrens 	}
4143972Svb160487 	if (i == strlen(ends)) {
4153972Svb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
4163972Svb160487 		    buf);
4173972Svb160487 		usage(B_FALSE);
4183972Svb160487 	}
419789Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
420789Sahrens 		return (10*i);
421789Sahrens 	}
4223972Svb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
4233972Svb160487 	usage(B_FALSE);
4243972Svb160487 	/* NOTREACHED */
425789Sahrens }
426789Sahrens 
427789Sahrens static uint64_t
428789Sahrens nicenumtoull(const char *buf)
429789Sahrens {
430789Sahrens 	char *end;
431789Sahrens 	uint64_t val;
432789Sahrens 
433789Sahrens 	val = strtoull(buf, &end, 0);
434789Sahrens 	if (end == buf) {
4353972Svb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
4363972Svb160487 		usage(B_FALSE);
437789Sahrens 	} else if (end[0] == '.') {
438789Sahrens 		double fval = strtod(buf, &end);
439789Sahrens 		fval *= pow(2, str2shift(end));
4403972Svb160487 		if (fval > UINT64_MAX) {
4413972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4423972Svb160487 			    buf);
4433972Svb160487 			usage(B_FALSE);
4443972Svb160487 		}
445789Sahrens 		val = (uint64_t)fval;
446789Sahrens 	} else {
447789Sahrens 		int shift = str2shift(end);
4483972Svb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
4493972Svb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
4503972Svb160487 			    buf);
4513972Svb160487 			usage(B_FALSE);
4523972Svb160487 		}
453789Sahrens 		val <<= shift;
454789Sahrens 	}
455789Sahrens 	return (val);
456789Sahrens }
457789Sahrens 
458789Sahrens static void
4593972Svb160487 usage(boolean_t requested)
460789Sahrens {
461789Sahrens 	char nice_vdev_size[10];
462789Sahrens 	char nice_gang_bang[10];
4633972Svb160487 	FILE *fp = requested ? stdout : stderr;
464789Sahrens 
465789Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
4665530Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
467789Sahrens 
4683972Svb160487 	(void) fprintf(fp, "Usage: %s\n"
469789Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
470789Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
47111818SMark.Musante@Sun.COM 	    "\t[-a alignment_shift (default: %d)] use 0 for random\n"
472789Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
473789Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
4742082Seschrock 	    "\t[-R raidz_parity (default: %d)]\n"
475789Sahrens 	    "\t[-d datasets (default: %d)]\n"
476789Sahrens 	    "\t[-t threads (default: %d)]\n"
477789Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
47811818SMark.Musante@Sun.COM 	    "\t[-i init_count (default: %d)] initialize pool i times\n"
47911818SMark.Musante@Sun.COM 	    "\t[-k kill_percentage (default: %llu%%)]\n"
480789Sahrens 	    "\t[-p pool_name (default: %s)]\n"
48111818SMark.Musante@Sun.COM 	    "\t[-f dir (default: %s)] file directory for vdev files\n"
48211818SMark.Musante@Sun.COM 	    "\t[-V] verbose (use multiple times for ever more blather)\n"
48311818SMark.Musante@Sun.COM 	    "\t[-E] use existing pool instead of creating new one\n"
48411818SMark.Musante@Sun.COM 	    "\t[-T time (default: %llu sec)] total run time\n"
48511818SMark.Musante@Sun.COM 	    "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
48611818SMark.Musante@Sun.COM 	    "\t[-P passtime (default: %llu sec)] time per pass\n"
4873972Svb160487 	    "\t[-h] (print help)\n"
488789Sahrens 	    "",
489789Sahrens 	    cmdname,
4905329Sgw25295 	    (u_longlong_t)zopt_vdevs,			/* -v */
4915329Sgw25295 	    nice_vdev_size,				/* -s */
4925329Sgw25295 	    zopt_ashift,				/* -a */
4935329Sgw25295 	    zopt_mirrors,				/* -m */
4945329Sgw25295 	    zopt_raidz,					/* -r */
4955329Sgw25295 	    zopt_raidz_parity,				/* -R */
4965329Sgw25295 	    zopt_datasets,				/* -d */
4975329Sgw25295 	    zopt_threads,				/* -t */
4985329Sgw25295 	    nice_gang_bang,				/* -g */
4995329Sgw25295 	    zopt_init,					/* -i */
5005329Sgw25295 	    (u_longlong_t)zopt_killrate,		/* -k */
5015329Sgw25295 	    zopt_pool,					/* -p */
5025329Sgw25295 	    zopt_dir,					/* -f */
5035329Sgw25295 	    (u_longlong_t)zopt_time,			/* -T */
50411818SMark.Musante@Sun.COM 	    (u_longlong_t)zopt_maxloops,		/* -F */
5057754SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zopt_passtime);		/* -P */
5063972Svb160487 	exit(requested ? 0 : 1);
507789Sahrens }
508789Sahrens 
509789Sahrens static void
510789Sahrens process_options(int argc, char **argv)
511789Sahrens {
512789Sahrens 	int opt;
513789Sahrens 	uint64_t value;
514789Sahrens 
515789Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
5165530Sbonwick 	metaslab_gang_bang = 32 << 10;
517789Sahrens 
518789Sahrens 	while ((opt = getopt(argc, argv,
51911818SMark.Musante@Sun.COM 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
520789Sahrens 		value = 0;
521789Sahrens 		switch (opt) {
5224451Seschrock 		case 'v':
5234451Seschrock 		case 's':
5244451Seschrock 		case 'a':
5254451Seschrock 		case 'm':
5264451Seschrock 		case 'r':
5274451Seschrock 		case 'R':
5284451Seschrock 		case 'd':
5294451Seschrock 		case 't':
5304451Seschrock 		case 'g':
5314451Seschrock 		case 'i':
5324451Seschrock 		case 'k':
5334451Seschrock 		case 'T':
5344451Seschrock 		case 'P':
53511818SMark.Musante@Sun.COM 		case 'F':
536789Sahrens 			value = nicenumtoull(optarg);
537789Sahrens 		}
538789Sahrens 		switch (opt) {
5394451Seschrock 		case 'v':
540789Sahrens 			zopt_vdevs = value;
541789Sahrens 			break;
5424451Seschrock 		case 's':
543789Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
544789Sahrens 			break;
5454451Seschrock 		case 'a':
5461732Sbonwick 			zopt_ashift = value;
5471732Sbonwick 			break;
5484451Seschrock 		case 'm':
549789Sahrens 			zopt_mirrors = value;
550789Sahrens 			break;
5514451Seschrock 		case 'r':
552789Sahrens 			zopt_raidz = MAX(1, value);
553789Sahrens 			break;
5544451Seschrock 		case 'R':
55510105Sadam.leventhal@sun.com 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
5562082Seschrock 			break;
5574451Seschrock 		case 'd':
5581732Sbonwick 			zopt_datasets = MAX(1, value);
559789Sahrens 			break;
5604451Seschrock 		case 't':
561789Sahrens 			zopt_threads = MAX(1, value);
562789Sahrens 			break;
5634451Seschrock 		case 'g':
5645530Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
565789Sahrens 			break;
5664451Seschrock 		case 'i':
567789Sahrens 			zopt_init = value;
568789Sahrens 			break;
5694451Seschrock 		case 'k':
570789Sahrens 			zopt_killrate = value;
571789Sahrens 			break;
5724451Seschrock 		case 'p':
573789Sahrens 			zopt_pool = strdup(optarg);
574789Sahrens 			break;
5754451Seschrock 		case 'f':
576789Sahrens 			zopt_dir = strdup(optarg);
577789Sahrens 			break;
5784451Seschrock 		case 'V':
579789Sahrens 			zopt_verbose++;
580789Sahrens 			break;
5814451Seschrock 		case 'E':
582789Sahrens 			zopt_init = 0;
583789Sahrens 			break;
5844451Seschrock 		case 'T':
585789Sahrens 			zopt_time = value;
586789Sahrens 			break;
5874451Seschrock 		case 'P':
588789Sahrens 			zopt_passtime = MAX(1, value);
589789Sahrens 			break;
59011818SMark.Musante@Sun.COM 		case 'F':
59111818SMark.Musante@Sun.COM 			zopt_maxloops = MAX(1, value);
59211818SMark.Musante@Sun.COM 			break;
5934451Seschrock 		case 'h':
5943972Svb160487 			usage(B_TRUE);
5953972Svb160487 			break;
5964451Seschrock 		case '?':
5974451Seschrock 		default:
5983972Svb160487 			usage(B_FALSE);
599789Sahrens 			break;
600789Sahrens 		}
601789Sahrens 	}
602789Sahrens 
6032082Seschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
6042082Seschrock 
60510922SJeff.Bonwick@Sun.COM 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
60610922SJeff.Bonwick@Sun.COM 	    UINT64_MAX >> 2);
607789Sahrens }
608789Sahrens 
60910922SJeff.Bonwick@Sun.COM static void
61010922SJeff.Bonwick@Sun.COM ztest_kill(ztest_shared_t *zs)
61110922SJeff.Bonwick@Sun.COM {
61210922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
61310922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
61410922SJeff.Bonwick@Sun.COM 	(void) kill(getpid(), SIGKILL);
61510922SJeff.Bonwick@Sun.COM }
61610922SJeff.Bonwick@Sun.COM 
61710922SJeff.Bonwick@Sun.COM static uint64_t
61810922SJeff.Bonwick@Sun.COM ztest_random(uint64_t range)
61910922SJeff.Bonwick@Sun.COM {
62010922SJeff.Bonwick@Sun.COM 	uint64_t r;
62110922SJeff.Bonwick@Sun.COM 
62210922SJeff.Bonwick@Sun.COM 	if (range == 0)
62310922SJeff.Bonwick@Sun.COM 		return (0);
62410922SJeff.Bonwick@Sun.COM 
62510922SJeff.Bonwick@Sun.COM 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
62610922SJeff.Bonwick@Sun.COM 		fatal(1, "short read from /dev/urandom");
62710922SJeff.Bonwick@Sun.COM 
62810922SJeff.Bonwick@Sun.COM 	return (r % range);
62910922SJeff.Bonwick@Sun.COM }
63010922SJeff.Bonwick@Sun.COM 
63110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
63210922SJeff.Bonwick@Sun.COM static void
63310922SJeff.Bonwick@Sun.COM ztest_record_enospc(const char *s)
63410922SJeff.Bonwick@Sun.COM {
63510922SJeff.Bonwick@Sun.COM 	ztest_shared->zs_enospc_count++;
63610922SJeff.Bonwick@Sun.COM }
63710922SJeff.Bonwick@Sun.COM 
6381732Sbonwick static uint64_t
6391732Sbonwick ztest_get_ashift(void)
6401732Sbonwick {
6411732Sbonwick 	if (zopt_ashift == 0)
6421732Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
6431732Sbonwick 	return (zopt_ashift);
6441732Sbonwick }
6451732Sbonwick 
646789Sahrens static nvlist_t *
6477754SJeff.Bonwick@Sun.COM make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
648789Sahrens {
6497754SJeff.Bonwick@Sun.COM 	char pathbuf[MAXPATHLEN];
650789Sahrens 	uint64_t vdev;
651789Sahrens 	nvlist_t *file;
652789Sahrens 
6537754SJeff.Bonwick@Sun.COM 	if (ashift == 0)
6547754SJeff.Bonwick@Sun.COM 		ashift = ztest_get_ashift();
6557754SJeff.Bonwick@Sun.COM 
6567754SJeff.Bonwick@Sun.COM 	if (path == NULL) {
6577754SJeff.Bonwick@Sun.COM 		path = pathbuf;
6587754SJeff.Bonwick@Sun.COM 
6597754SJeff.Bonwick@Sun.COM 		if (aux != NULL) {
6607754SJeff.Bonwick@Sun.COM 			vdev = ztest_shared->zs_vdev_aux;
6617754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template,
6627754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, aux, vdev);
6637754SJeff.Bonwick@Sun.COM 		} else {
66410594SGeorge.Wilson@Sun.COM 			vdev = ztest_shared->zs_vdev_next_leaf++;
6657754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_dev_template,
6667754SJeff.Bonwick@Sun.COM 			    zopt_dir, zopt_pool, vdev);
6677754SJeff.Bonwick@Sun.COM 		}
6687754SJeff.Bonwick@Sun.COM 	}
6697754SJeff.Bonwick@Sun.COM 
6707754SJeff.Bonwick@Sun.COM 	if (size != 0) {
6717754SJeff.Bonwick@Sun.COM 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
672789Sahrens 		if (fd == -1)
6737754SJeff.Bonwick@Sun.COM 			fatal(1, "can't open %s", path);
674789Sahrens 		if (ftruncate(fd, size) != 0)
6757754SJeff.Bonwick@Sun.COM 			fatal(1, "can't ftruncate %s", path);
676789Sahrens 		(void) close(fd);
677789Sahrens 	}
678789Sahrens 
679789Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
680789Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
6817754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
6821732Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
683789Sahrens 
684789Sahrens 	return (file);
685789Sahrens }
686789Sahrens 
687789Sahrens static nvlist_t *
6887754SJeff.Bonwick@Sun.COM make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
689789Sahrens {
690789Sahrens 	nvlist_t *raidz, **child;
691789Sahrens 	int c;
692789Sahrens 
693789Sahrens 	if (r < 2)
6947754SJeff.Bonwick@Sun.COM 		return (make_vdev_file(path, aux, size, ashift));
695789Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
696789Sahrens 
697789Sahrens 	for (c = 0; c < r; c++)
6987754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_file(path, aux, size, ashift);
699789Sahrens 
700789Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
701789Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
702789Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
7032082Seschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
7042082Seschrock 	    zopt_raidz_parity) == 0);
705789Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
706789Sahrens 	    child, r) == 0);
707789Sahrens 
708789Sahrens 	for (c = 0; c < r; c++)
709789Sahrens 		nvlist_free(child[c]);
710789Sahrens 
711789Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
712789Sahrens 
713789Sahrens 	return (raidz);
714789Sahrens }
715789Sahrens 
716789Sahrens static nvlist_t *
7177754SJeff.Bonwick@Sun.COM make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
7187768SJeff.Bonwick@Sun.COM 	int r, int m)
719789Sahrens {
720789Sahrens 	nvlist_t *mirror, **child;
721789Sahrens 	int c;
722789Sahrens 
723789Sahrens 	if (m < 1)
7247754SJeff.Bonwick@Sun.COM 		return (make_vdev_raidz(path, aux, size, ashift, r));
725789Sahrens 
726789Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
727789Sahrens 
728789Sahrens 	for (c = 0; c < m; c++)
7297754SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
730789Sahrens 
731789Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
732789Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
733789Sahrens 	    VDEV_TYPE_MIRROR) == 0);
734789Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
735789Sahrens 	    child, m) == 0);
736789Sahrens 
737789Sahrens 	for (c = 0; c < m; c++)
738789Sahrens 		nvlist_free(child[c]);
739789Sahrens 
740789Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
741789Sahrens 
742789Sahrens 	return (mirror);
743789Sahrens }
744789Sahrens 
745789Sahrens static nvlist_t *
7467754SJeff.Bonwick@Sun.COM make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
7477754SJeff.Bonwick@Sun.COM 	int log, int r, int m, int t)
748789Sahrens {
749789Sahrens 	nvlist_t *root, **child;
750789Sahrens 	int c;
751789Sahrens 
752789Sahrens 	ASSERT(t > 0);
753789Sahrens 
754789Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
755789Sahrens 
7567768SJeff.Bonwick@Sun.COM 	for (c = 0; c < t; c++) {
7577768SJeff.Bonwick@Sun.COM 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
7587768SJeff.Bonwick@Sun.COM 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
7597768SJeff.Bonwick@Sun.COM 		    log) == 0);
7607768SJeff.Bonwick@Sun.COM 	}
761789Sahrens 
762789Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
763789Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
7647754SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
765789Sahrens 	    child, t) == 0);
766789Sahrens 
767789Sahrens 	for (c = 0; c < t; c++)
768789Sahrens 		nvlist_free(child[c]);
769789Sahrens 
770789Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
771789Sahrens 
772789Sahrens 	return (root);
773789Sahrens }
774789Sahrens 
77510922SJeff.Bonwick@Sun.COM static int
77610922SJeff.Bonwick@Sun.COM ztest_random_blocksize(void)
777789Sahrens {
77810922SJeff.Bonwick@Sun.COM 	return (1 << (SPA_MINBLOCKSHIFT +
77910922SJeff.Bonwick@Sun.COM 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
780789Sahrens }
781789Sahrens 
782789Sahrens static int
78310922SJeff.Bonwick@Sun.COM ztest_random_ibshift(void)
78410922SJeff.Bonwick@Sun.COM {
78510922SJeff.Bonwick@Sun.COM 	return (DN_MIN_INDBLKSHIFT +
78610922SJeff.Bonwick@Sun.COM 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
78710922SJeff.Bonwick@Sun.COM }
78810922SJeff.Bonwick@Sun.COM 
78910922SJeff.Bonwick@Sun.COM static uint64_t
79010922SJeff.Bonwick@Sun.COM ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
791789Sahrens {
79210922SJeff.Bonwick@Sun.COM 	uint64_t top;
79310922SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
79410922SJeff.Bonwick@Sun.COM 	vdev_t *tvd;
79510922SJeff.Bonwick@Sun.COM 
79610922SJeff.Bonwick@Sun.COM 	ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
79710922SJeff.Bonwick@Sun.COM 
79810922SJeff.Bonwick@Sun.COM 	do {
79910922SJeff.Bonwick@Sun.COM 		top = ztest_random(rvd->vdev_children);
80010922SJeff.Bonwick@Sun.COM 		tvd = rvd->vdev_child[top];
80110922SJeff.Bonwick@Sun.COM 	} while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
80210922SJeff.Bonwick@Sun.COM 	    tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
80310922SJeff.Bonwick@Sun.COM 
80410922SJeff.Bonwick@Sun.COM 	return (top);
80510922SJeff.Bonwick@Sun.COM }
80610922SJeff.Bonwick@Sun.COM 
80710922SJeff.Bonwick@Sun.COM static uint64_t
80810922SJeff.Bonwick@Sun.COM ztest_random_dsl_prop(zfs_prop_t prop)
80910922SJeff.Bonwick@Sun.COM {
81010922SJeff.Bonwick@Sun.COM 	uint64_t value;
81110922SJeff.Bonwick@Sun.COM 
81210922SJeff.Bonwick@Sun.COM 	do {
81310922SJeff.Bonwick@Sun.COM 		value = zfs_prop_random_value(prop, ztest_random(-1ULL));
81410922SJeff.Bonwick@Sun.COM 	} while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
81510922SJeff.Bonwick@Sun.COM 
81610922SJeff.Bonwick@Sun.COM 	return (value);
81710922SJeff.Bonwick@Sun.COM }
81810922SJeff.Bonwick@Sun.COM 
81910922SJeff.Bonwick@Sun.COM static int
82010922SJeff.Bonwick@Sun.COM ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
82110922SJeff.Bonwick@Sun.COM     boolean_t inherit)
82210922SJeff.Bonwick@Sun.COM {
82310922SJeff.Bonwick@Sun.COM 	const char *propname = zfs_prop_to_name(prop);
82410922SJeff.Bonwick@Sun.COM 	const char *valname;
82510922SJeff.Bonwick@Sun.COM 	char setpoint[MAXPATHLEN];
82610922SJeff.Bonwick@Sun.COM 	uint64_t curval;
827789Sahrens 	int error;
828789Sahrens 
82911022STom.Erickson@Sun.COM 	error = dsl_prop_set(osname, propname,
83011022STom.Erickson@Sun.COM 	    (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
83111022STom.Erickson@Sun.COM 	    sizeof (value), 1, &value);
83210922SJeff.Bonwick@Sun.COM 
83310922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
83410922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
835789Sahrens 		return (error);
836789Sahrens 	}
8372082Seschrock 	ASSERT3U(error, ==, 0);
83810922SJeff.Bonwick@Sun.COM 
83910922SJeff.Bonwick@Sun.COM 	VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
84010922SJeff.Bonwick@Sun.COM 	    1, &curval, setpoint), ==, 0);
84110922SJeff.Bonwick@Sun.COM 
84210922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
84310922SJeff.Bonwick@Sun.COM 		VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
84410922SJeff.Bonwick@Sun.COM 		(void) printf("%s %s = %s at '%s'\n",
84510922SJeff.Bonwick@Sun.COM 		    osname, propname, valname, setpoint);
846789Sahrens 	}
847789Sahrens 
848789Sahrens 	return (error);
849789Sahrens }
850789Sahrens 
851789Sahrens static int
85210922SJeff.Bonwick@Sun.COM ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
85310922SJeff.Bonwick@Sun.COM {
85410922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
85510922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
85610922SJeff.Bonwick@Sun.COM 	int error;
85710922SJeff.Bonwick@Sun.COM 
85810922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
85910922SJeff.Bonwick@Sun.COM 	VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
86010922SJeff.Bonwick@Sun.COM 
86110922SJeff.Bonwick@Sun.COM 	error = spa_prop_set(spa, props);
86210922SJeff.Bonwick@Sun.COM 
86310922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
86410922SJeff.Bonwick@Sun.COM 
86510922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
86610922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
86710922SJeff.Bonwick@Sun.COM 		return (error);
86810922SJeff.Bonwick@Sun.COM 	}
86910922SJeff.Bonwick@Sun.COM 	ASSERT3U(error, ==, 0);
87010922SJeff.Bonwick@Sun.COM 
87110922SJeff.Bonwick@Sun.COM 	return (error);
87210922SJeff.Bonwick@Sun.COM }
87310922SJeff.Bonwick@Sun.COM 
87410922SJeff.Bonwick@Sun.COM static void
87510922SJeff.Bonwick@Sun.COM ztest_rll_init(rll_t *rll)
87610922SJeff.Bonwick@Sun.COM {
87710922SJeff.Bonwick@Sun.COM 	rll->rll_writer = NULL;
87810922SJeff.Bonwick@Sun.COM 	rll->rll_readers = 0;
87910922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
88010922SJeff.Bonwick@Sun.COM 	VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
88110922SJeff.Bonwick@Sun.COM }
88210922SJeff.Bonwick@Sun.COM 
88310922SJeff.Bonwick@Sun.COM static void
88410922SJeff.Bonwick@Sun.COM ztest_rll_destroy(rll_t *rll)
88510922SJeff.Bonwick@Sun.COM {
88610922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_writer == NULL);
88710922SJeff.Bonwick@Sun.COM 	ASSERT(rll->rll_readers == 0);
88810922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
88910922SJeff.Bonwick@Sun.COM 	VERIFY(cond_destroy(&rll->rll_cv) == 0);
89010922SJeff.Bonwick@Sun.COM }
89110922SJeff.Bonwick@Sun.COM 
89210922SJeff.Bonwick@Sun.COM static void
89310922SJeff.Bonwick@Sun.COM ztest_rll_lock(rll_t *rll, rl_type_t type)
89410922SJeff.Bonwick@Sun.COM {
89510922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
89610922SJeff.Bonwick@Sun.COM 
89710922SJeff.Bonwick@Sun.COM 	if (type == RL_READER) {
89810922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL)
89910922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
90010922SJeff.Bonwick@Sun.COM 		rll->rll_readers++;
90110922SJeff.Bonwick@Sun.COM 	} else {
90210922SJeff.Bonwick@Sun.COM 		while (rll->rll_writer != NULL || rll->rll_readers)
90310922SJeff.Bonwick@Sun.COM 			(void) cond_wait(&rll->rll_cv, &rll->rll_lock);
90410922SJeff.Bonwick@Sun.COM 		rll->rll_writer = curthread;
90510922SJeff.Bonwick@Sun.COM 	}
90610922SJeff.Bonwick@Sun.COM 
90710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
90810922SJeff.Bonwick@Sun.COM }
90910922SJeff.Bonwick@Sun.COM 
91010922SJeff.Bonwick@Sun.COM static void
91110922SJeff.Bonwick@Sun.COM ztest_rll_unlock(rll_t *rll)
91210922SJeff.Bonwick@Sun.COM {
91310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&rll->rll_lock) == 0);
91410922SJeff.Bonwick@Sun.COM 
91510922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer) {
91610922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers == 0);
91710922SJeff.Bonwick@Sun.COM 		rll->rll_writer = NULL;
91810922SJeff.Bonwick@Sun.COM 	} else {
91910922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_readers != 0);
92010922SJeff.Bonwick@Sun.COM 		ASSERT(rll->rll_writer == NULL);
92110922SJeff.Bonwick@Sun.COM 		rll->rll_readers--;
92210922SJeff.Bonwick@Sun.COM 	}
92310922SJeff.Bonwick@Sun.COM 
92410922SJeff.Bonwick@Sun.COM 	if (rll->rll_writer == NULL && rll->rll_readers == 0)
92510922SJeff.Bonwick@Sun.COM 		VERIFY(cond_broadcast(&rll->rll_cv) == 0);
92610922SJeff.Bonwick@Sun.COM 
92710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&rll->rll_lock) == 0);
92810922SJeff.Bonwick@Sun.COM }
92910922SJeff.Bonwick@Sun.COM 
93010922SJeff.Bonwick@Sun.COM static void
93110922SJeff.Bonwick@Sun.COM ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
93210922SJeff.Bonwick@Sun.COM {
93310922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
93410922SJeff.Bonwick@Sun.COM 
93510922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
93610922SJeff.Bonwick@Sun.COM }
93710922SJeff.Bonwick@Sun.COM 
93810922SJeff.Bonwick@Sun.COM static void
93910922SJeff.Bonwick@Sun.COM ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
94010922SJeff.Bonwick@Sun.COM {
94110922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
94210922SJeff.Bonwick@Sun.COM 
94310922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
94410922SJeff.Bonwick@Sun.COM }
94510922SJeff.Bonwick@Sun.COM 
94610922SJeff.Bonwick@Sun.COM static rl_t *
94710922SJeff.Bonwick@Sun.COM ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
94810922SJeff.Bonwick@Sun.COM     uint64_t size, rl_type_t type)
94910922SJeff.Bonwick@Sun.COM {
95010922SJeff.Bonwick@Sun.COM 	uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
95110922SJeff.Bonwick@Sun.COM 	rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
95210922SJeff.Bonwick@Sun.COM 	rl_t *rl;
95310922SJeff.Bonwick@Sun.COM 
95410922SJeff.Bonwick@Sun.COM 	rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
95510922SJeff.Bonwick@Sun.COM 	rl->rl_object = object;
95610922SJeff.Bonwick@Sun.COM 	rl->rl_offset = offset;
95710922SJeff.Bonwick@Sun.COM 	rl->rl_size = size;
95810922SJeff.Bonwick@Sun.COM 	rl->rl_lock = rll;
95910922SJeff.Bonwick@Sun.COM 
96010922SJeff.Bonwick@Sun.COM 	ztest_rll_lock(rll, type);
96110922SJeff.Bonwick@Sun.COM 
96210922SJeff.Bonwick@Sun.COM 	return (rl);
96310922SJeff.Bonwick@Sun.COM }
96410922SJeff.Bonwick@Sun.COM 
96510922SJeff.Bonwick@Sun.COM static void
96610922SJeff.Bonwick@Sun.COM ztest_range_unlock(rl_t *rl)
96710922SJeff.Bonwick@Sun.COM {
96810922SJeff.Bonwick@Sun.COM 	rll_t *rll = rl->rl_lock;
96910922SJeff.Bonwick@Sun.COM 
97010922SJeff.Bonwick@Sun.COM 	ztest_rll_unlock(rll);
97110922SJeff.Bonwick@Sun.COM 
97210922SJeff.Bonwick@Sun.COM 	umem_free(rl, sizeof (*rl));
97310922SJeff.Bonwick@Sun.COM }
97410922SJeff.Bonwick@Sun.COM 
97510922SJeff.Bonwick@Sun.COM static void
97610922SJeff.Bonwick@Sun.COM ztest_zd_init(ztest_ds_t *zd, objset_t *os)
97710922SJeff.Bonwick@Sun.COM {
97810922SJeff.Bonwick@Sun.COM 	zd->zd_os = os;
97910922SJeff.Bonwick@Sun.COM 	zd->zd_zilog = dmu_objset_zil(os);
98010922SJeff.Bonwick@Sun.COM 	zd->zd_seq = 0;
98110922SJeff.Bonwick@Sun.COM 	dmu_objset_name(os, zd->zd_name);
98210922SJeff.Bonwick@Sun.COM 
98310922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
98410922SJeff.Bonwick@Sun.COM 
98510922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
98610922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_object_lock[l]);
98710922SJeff.Bonwick@Sun.COM 
98810922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
98910922SJeff.Bonwick@Sun.COM 		ztest_rll_init(&zd->zd_range_lock[l]);
99010922SJeff.Bonwick@Sun.COM }
99110922SJeff.Bonwick@Sun.COM 
99210922SJeff.Bonwick@Sun.COM static void
99310922SJeff.Bonwick@Sun.COM ztest_zd_fini(ztest_ds_t *zd)
99410922SJeff.Bonwick@Sun.COM {
99510922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
99610922SJeff.Bonwick@Sun.COM 
99710922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
99810922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_object_lock[l]);
99910922SJeff.Bonwick@Sun.COM 
100010922SJeff.Bonwick@Sun.COM 	for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
100110922SJeff.Bonwick@Sun.COM 		ztest_rll_destroy(&zd->zd_range_lock[l]);
100210922SJeff.Bonwick@Sun.COM }
100310922SJeff.Bonwick@Sun.COM 
100410922SJeff.Bonwick@Sun.COM #define	TXG_MIGHTWAIT	(ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
100510922SJeff.Bonwick@Sun.COM 
100610922SJeff.Bonwick@Sun.COM static uint64_t
100710922SJeff.Bonwick@Sun.COM ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1008789Sahrens {
100910922SJeff.Bonwick@Sun.COM 	uint64_t txg;
101010922SJeff.Bonwick@Sun.COM 	int error;
101110922SJeff.Bonwick@Sun.COM 
101210922SJeff.Bonwick@Sun.COM 	/*
101310922SJeff.Bonwick@Sun.COM 	 * Attempt to assign tx to some transaction group.
101410922SJeff.Bonwick@Sun.COM 	 */
101510922SJeff.Bonwick@Sun.COM 	error = dmu_tx_assign(tx, txg_how);
101610922SJeff.Bonwick@Sun.COM 	if (error) {
101710922SJeff.Bonwick@Sun.COM 		if (error == ERESTART) {
101810922SJeff.Bonwick@Sun.COM 			ASSERT(txg_how == TXG_NOWAIT);
101910922SJeff.Bonwick@Sun.COM 			dmu_tx_wait(tx);
102010922SJeff.Bonwick@Sun.COM 		} else {
102110922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
102210922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(tag);
102310922SJeff.Bonwick@Sun.COM 		}
102410922SJeff.Bonwick@Sun.COM 		dmu_tx_abort(tx);
102510922SJeff.Bonwick@Sun.COM 		return (0);
102610922SJeff.Bonwick@Sun.COM 	}
102710922SJeff.Bonwick@Sun.COM 	txg = dmu_tx_get_txg(tx);
102810922SJeff.Bonwick@Sun.COM 	ASSERT(txg != 0);
102910922SJeff.Bonwick@Sun.COM 	return (txg);
103010922SJeff.Bonwick@Sun.COM }
103110922SJeff.Bonwick@Sun.COM 
103210922SJeff.Bonwick@Sun.COM static void
103310922SJeff.Bonwick@Sun.COM ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
103410922SJeff.Bonwick@Sun.COM {
103510922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
103610922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
103710922SJeff.Bonwick@Sun.COM 
103810922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
103910922SJeff.Bonwick@Sun.COM 		*ip++ = value;
104010922SJeff.Bonwick@Sun.COM }
104110922SJeff.Bonwick@Sun.COM 
104210922SJeff.Bonwick@Sun.COM static boolean_t
104310922SJeff.Bonwick@Sun.COM ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
104410922SJeff.Bonwick@Sun.COM {
104510922SJeff.Bonwick@Sun.COM 	uint64_t *ip = buf;
104610922SJeff.Bonwick@Sun.COM 	uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
104710922SJeff.Bonwick@Sun.COM 	uint64_t diff = 0;
104810922SJeff.Bonwick@Sun.COM 
104910922SJeff.Bonwick@Sun.COM 	while (ip < ip_end)
105010922SJeff.Bonwick@Sun.COM 		diff |= (value - *ip++);
105110922SJeff.Bonwick@Sun.COM 
105210922SJeff.Bonwick@Sun.COM 	return (diff == 0);
105310922SJeff.Bonwick@Sun.COM }
105410922SJeff.Bonwick@Sun.COM 
105510922SJeff.Bonwick@Sun.COM static void
105610922SJeff.Bonwick@Sun.COM ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
105710922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
105810922SJeff.Bonwick@Sun.COM {
105910922SJeff.Bonwick@Sun.COM 	bt->bt_magic = BT_MAGIC;
106010922SJeff.Bonwick@Sun.COM 	bt->bt_objset = dmu_objset_id(os);
106110922SJeff.Bonwick@Sun.COM 	bt->bt_object = object;
106210922SJeff.Bonwick@Sun.COM 	bt->bt_offset = offset;
106310922SJeff.Bonwick@Sun.COM 	bt->bt_gen = gen;
106410922SJeff.Bonwick@Sun.COM 	bt->bt_txg = txg;
106510922SJeff.Bonwick@Sun.COM 	bt->bt_crtxg = crtxg;
106610922SJeff.Bonwick@Sun.COM }
106710922SJeff.Bonwick@Sun.COM 
106810922SJeff.Bonwick@Sun.COM static void
106910922SJeff.Bonwick@Sun.COM ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
107010922SJeff.Bonwick@Sun.COM     uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
107110922SJeff.Bonwick@Sun.COM {
107210922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_magic == BT_MAGIC);
107310922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_objset == dmu_objset_id(os));
107410922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_object == object);
107510922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_offset == offset);
107610922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_gen <= gen);
107710922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_txg <= txg);
107810922SJeff.Bonwick@Sun.COM 	ASSERT(bt->bt_crtxg == crtxg);
107910922SJeff.Bonwick@Sun.COM }
108010922SJeff.Bonwick@Sun.COM 
108110922SJeff.Bonwick@Sun.COM static ztest_block_tag_t *
108210922SJeff.Bonwick@Sun.COM ztest_bt_bonus(dmu_buf_t *db)
108310922SJeff.Bonwick@Sun.COM {
108410922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
108510922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt;
108610922SJeff.Bonwick@Sun.COM 
108710922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
108810922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
108910922SJeff.Bonwick@Sun.COM 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
109010922SJeff.Bonwick@Sun.COM 	bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
109110922SJeff.Bonwick@Sun.COM 
109210922SJeff.Bonwick@Sun.COM 	return (bt);
109310922SJeff.Bonwick@Sun.COM }
109410922SJeff.Bonwick@Sun.COM 
109510922SJeff.Bonwick@Sun.COM /*
109610922SJeff.Bonwick@Sun.COM  * ZIL logging ops
109710922SJeff.Bonwick@Sun.COM  */
109810922SJeff.Bonwick@Sun.COM 
109910922SJeff.Bonwick@Sun.COM #define	lrz_type	lr_mode
110010922SJeff.Bonwick@Sun.COM #define	lrz_blocksize	lr_uid
110110922SJeff.Bonwick@Sun.COM #define	lrz_ibshift	lr_gid
110210922SJeff.Bonwick@Sun.COM #define	lrz_bonustype	lr_rdev
110310922SJeff.Bonwick@Sun.COM #define	lrz_bonuslen	lr_crtime[1]
110410922SJeff.Bonwick@Sun.COM 
110510922SJeff.Bonwick@Sun.COM static uint64_t
110610922SJeff.Bonwick@Sun.COM ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
110710922SJeff.Bonwick@Sun.COM {
110810922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
110910922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
111010922SJeff.Bonwick@Sun.COM 	itx_t *itx;
111110922SJeff.Bonwick@Sun.COM 
111210922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
111310922SJeff.Bonwick@Sun.COM 		return (0);
111410922SJeff.Bonwick@Sun.COM 
111510922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
111610922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
111710922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
111810922SJeff.Bonwick@Sun.COM 
111910922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
112010922SJeff.Bonwick@Sun.COM }
112110922SJeff.Bonwick@Sun.COM 
112210922SJeff.Bonwick@Sun.COM static uint64_t
112310922SJeff.Bonwick@Sun.COM ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr)
112410922SJeff.Bonwick@Sun.COM {
112510922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
112610922SJeff.Bonwick@Sun.COM 	size_t namesize = strlen(name) + 1;
112710922SJeff.Bonwick@Sun.COM 	itx_t *itx;
112810922SJeff.Bonwick@Sun.COM 
112910922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
113010922SJeff.Bonwick@Sun.COM 		return (0);
113110922SJeff.Bonwick@Sun.COM 
113210922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
113310922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
113410922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + namesize - sizeof (lr_t));
113510922SJeff.Bonwick@Sun.COM 
113610922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
113710922SJeff.Bonwick@Sun.COM }
113810922SJeff.Bonwick@Sun.COM 
113910922SJeff.Bonwick@Sun.COM static uint64_t
114010922SJeff.Bonwick@Sun.COM ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
114110922SJeff.Bonwick@Sun.COM {
114210922SJeff.Bonwick@Sun.COM 	itx_t *itx;
114310922SJeff.Bonwick@Sun.COM 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
114410922SJeff.Bonwick@Sun.COM 
114510922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
114610922SJeff.Bonwick@Sun.COM 		return (0);
114710922SJeff.Bonwick@Sun.COM 
114810922SJeff.Bonwick@Sun.COM 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
114910922SJeff.Bonwick@Sun.COM 		write_state = WR_INDIRECT;
115010922SJeff.Bonwick@Sun.COM 
115110922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_WRITE,
115210922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
115310922SJeff.Bonwick@Sun.COM 
115410922SJeff.Bonwick@Sun.COM 	if (write_state == WR_COPIED &&
115510922SJeff.Bonwick@Sun.COM 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
115610922SJeff.Bonwick@Sun.COM 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
115710922SJeff.Bonwick@Sun.COM 		zil_itx_destroy(itx);
115810922SJeff.Bonwick@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
115910922SJeff.Bonwick@Sun.COM 		write_state = WR_NEED_COPY;
116010922SJeff.Bonwick@Sun.COM 	}
116110922SJeff.Bonwick@Sun.COM 	itx->itx_private = zd;
116210922SJeff.Bonwick@Sun.COM 	itx->itx_wr_state = write_state;
116310922SJeff.Bonwick@Sun.COM 	itx->itx_sync = (ztest_random(8) == 0);
116410922SJeff.Bonwick@Sun.COM 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
116510922SJeff.Bonwick@Sun.COM 
116610922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
116710922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
116810922SJeff.Bonwick@Sun.COM 
116910922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
117010922SJeff.Bonwick@Sun.COM }
117110922SJeff.Bonwick@Sun.COM 
117210922SJeff.Bonwick@Sun.COM static uint64_t
117310922SJeff.Bonwick@Sun.COM ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
117410922SJeff.Bonwick@Sun.COM {
117510922SJeff.Bonwick@Sun.COM 	itx_t *itx;
117610922SJeff.Bonwick@Sun.COM 
117710922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
117810922SJeff.Bonwick@Sun.COM 		return (0);
117910922SJeff.Bonwick@Sun.COM 
118010922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
118110922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
118210922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
118310922SJeff.Bonwick@Sun.COM 
118410922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
118510922SJeff.Bonwick@Sun.COM }
118610922SJeff.Bonwick@Sun.COM 
118710922SJeff.Bonwick@Sun.COM static uint64_t
118810922SJeff.Bonwick@Sun.COM ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
118910922SJeff.Bonwick@Sun.COM {
119010922SJeff.Bonwick@Sun.COM 	itx_t *itx;
119110922SJeff.Bonwick@Sun.COM 
119210922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
119310922SJeff.Bonwick@Sun.COM 		return (0);
119410922SJeff.Bonwick@Sun.COM 
119510922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
119610922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
119710922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
119810922SJeff.Bonwick@Sun.COM 
119910922SJeff.Bonwick@Sun.COM 	return (zil_itx_assign(zd->zd_zilog, itx, tx));
120010922SJeff.Bonwick@Sun.COM }
120110922SJeff.Bonwick@Sun.COM 
120210922SJeff.Bonwick@Sun.COM /*
120310922SJeff.Bonwick@Sun.COM  * ZIL replay ops
120410922SJeff.Bonwick@Sun.COM  */
120510922SJeff.Bonwick@Sun.COM static int
120610922SJeff.Bonwick@Sun.COM ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
120710922SJeff.Bonwick@Sun.COM {
120810922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
120910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
121010922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
121110922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1212789Sahrens 	dmu_tx_t *tx;
121310922SJeff.Bonwick@Sun.COM 	uint64_t txg;
121410922SJeff.Bonwick@Sun.COM 	int error = 0;
1215789Sahrens 
1216789Sahrens 	if (byteswap)
1217789Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
1218789Sahrens 
121910922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
122010922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
122110922SJeff.Bonwick@Sun.COM 
1222789Sahrens 	tx = dmu_tx_create(os);
122310922SJeff.Bonwick@Sun.COM 
122410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
122510922SJeff.Bonwick@Sun.COM 
122610922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
122710922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
122810922SJeff.Bonwick@Sun.COM 	} else {
122910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
123010922SJeff.Bonwick@Sun.COM 	}
123110922SJeff.Bonwick@Sun.COM 
123210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
123310922SJeff.Bonwick@Sun.COM 	if (txg == 0)
123410922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
123510922SJeff.Bonwick@Sun.COM 
123610922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
123710922SJeff.Bonwick@Sun.COM 
123810922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
123910922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
124010922SJeff.Bonwick@Sun.COM 			lr->lr_foid = zap_create(os,
124110922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124210922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124310922SJeff.Bonwick@Sun.COM 		} else {
124410922SJeff.Bonwick@Sun.COM 			error = zap_create_claim(os, lr->lr_foid,
124510922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124610922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124710922SJeff.Bonwick@Sun.COM 		}
124810922SJeff.Bonwick@Sun.COM 	} else {
124910922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
125010922SJeff.Bonwick@Sun.COM 			lr->lr_foid = dmu_object_alloc(os,
125110922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125210922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125310922SJeff.Bonwick@Sun.COM 		} else {
125410922SJeff.Bonwick@Sun.COM 			error = dmu_object_claim(os, lr->lr_foid,
125510922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125610922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125710922SJeff.Bonwick@Sun.COM 		}
125810922SJeff.Bonwick@Sun.COM 	}
125910922SJeff.Bonwick@Sun.COM 
1260789Sahrens 	if (error) {
126110922SJeff.Bonwick@Sun.COM 		ASSERT3U(error, ==, EEXIST);
126210922SJeff.Bonwick@Sun.COM 		ASSERT(zd->zd_zilog->zl_replay);
126310922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1264789Sahrens 		return (error);
1265789Sahrens 	}
1266789Sahrens 
126710922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_foid != 0);
126810922SJeff.Bonwick@Sun.COM 
126910922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
127010922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
127110922SJeff.Bonwick@Sun.COM 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
127210922SJeff.Bonwick@Sun.COM 
127310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
127410922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
127510922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
127610922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
127710922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
127810922SJeff.Bonwick@Sun.COM 
127910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
128010922SJeff.Bonwick@Sun.COM 	    &lr->lr_foid, tx));
128110922SJeff.Bonwick@Sun.COM 
128210922SJeff.Bonwick@Sun.COM 	(void) ztest_log_create(zd, tx, lr);
128310922SJeff.Bonwick@Sun.COM 
128410922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
128510922SJeff.Bonwick@Sun.COM 
128610922SJeff.Bonwick@Sun.COM 	return (0);
128710922SJeff.Bonwick@Sun.COM }
128810922SJeff.Bonwick@Sun.COM 
128910922SJeff.Bonwick@Sun.COM static int
129010922SJeff.Bonwick@Sun.COM ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
129110922SJeff.Bonwick@Sun.COM {
129210922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
129310922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
129410922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
129510922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
129610922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
129710922SJeff.Bonwick@Sun.COM 
129810922SJeff.Bonwick@Sun.COM 	if (byteswap)
129910922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
130010922SJeff.Bonwick@Sun.COM 
130110922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
130210922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
130310922SJeff.Bonwick@Sun.COM 
130410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
130510922SJeff.Bonwick@Sun.COM 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
130610922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
130710922SJeff.Bonwick@Sun.COM 
130810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_WRITER);
130910922SJeff.Bonwick@Sun.COM 
131010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
131110922SJeff.Bonwick@Sun.COM 
131210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
131310922SJeff.Bonwick@Sun.COM 
131410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
131510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
131610922SJeff.Bonwick@Sun.COM 
131710922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
131810922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
131910922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
132010922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
132110922SJeff.Bonwick@Sun.COM 	}
132210922SJeff.Bonwick@Sun.COM 
132310922SJeff.Bonwick@Sun.COM 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
132410922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
132510922SJeff.Bonwick@Sun.COM 	} else {
132610922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
132710922SJeff.Bonwick@Sun.COM 	}
132810922SJeff.Bonwick@Sun.COM 
132910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
133010922SJeff.Bonwick@Sun.COM 
133110922SJeff.Bonwick@Sun.COM 	(void) ztest_log_remove(zd, tx, lr);
133210922SJeff.Bonwick@Sun.COM 
1333789Sahrens 	dmu_tx_commit(tx);
1334789Sahrens 
133510922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
133610922SJeff.Bonwick@Sun.COM 
133710922SJeff.Bonwick@Sun.COM 	return (0);
133810922SJeff.Bonwick@Sun.COM }
133910922SJeff.Bonwick@Sun.COM 
134010922SJeff.Bonwick@Sun.COM static int
134110922SJeff.Bonwick@Sun.COM ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
134210922SJeff.Bonwick@Sun.COM {
134310922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
134410922SJeff.Bonwick@Sun.COM 	void *data = lr + 1;			/* data follows lr */
134510922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
134610922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt = data;
134710922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
134810922SJeff.Bonwick@Sun.COM 	uint64_t gen, txg, lrtxg, crtxg;
134910922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
135010922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
135110922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
135210922SJeff.Bonwick@Sun.COM 	arc_buf_t *abuf = NULL;
135310922SJeff.Bonwick@Sun.COM 	rl_t *rl;
135410922SJeff.Bonwick@Sun.COM 
135510922SJeff.Bonwick@Sun.COM 	if (byteswap)
135610922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
135710922SJeff.Bonwick@Sun.COM 
135810922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
135910922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
136010922SJeff.Bonwick@Sun.COM 
136110922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
136210922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
136310922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
136410922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
136510922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
136610922SJeff.Bonwick@Sun.COM 			length = blocksize;
136710922SJeff.Bonwick@Sun.COM 		}
136810922SJeff.Bonwick@Sun.COM 	}
136910922SJeff.Bonwick@Sun.COM 
137010922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
137110922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(bt, sizeof (*bt));
137210922SJeff.Bonwick@Sun.COM 
137310922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic != BT_MAGIC)
137410922SJeff.Bonwick@Sun.COM 		bt = NULL;
137510922SJeff.Bonwick@Sun.COM 
137610922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
137710922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
137810922SJeff.Bonwick@Sun.COM 
137910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
138010922SJeff.Bonwick@Sun.COM 
138110922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
138210922SJeff.Bonwick@Sun.COM 
138310922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
138410922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
138510922SJeff.Bonwick@Sun.COM 	gen = bbt->bt_gen;
138610922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
138710922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
138810922SJeff.Bonwick@Sun.COM 
138910922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
139010922SJeff.Bonwick@Sun.COM 
139110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
139210922SJeff.Bonwick@Sun.COM 
139310922SJeff.Bonwick@Sun.COM 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
139410922SJeff.Bonwick@Sun.COM 	    P2PHASE(offset, length) == 0)
139510922SJeff.Bonwick@Sun.COM 		abuf = dmu_request_arcbuf(db, length);
139610922SJeff.Bonwick@Sun.COM 
139710922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
139810922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
139910922SJeff.Bonwick@Sun.COM 		if (abuf != NULL)
140010922SJeff.Bonwick@Sun.COM 			dmu_return_arcbuf(abuf);
140110922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
140210922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
140310922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
140410922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
140510922SJeff.Bonwick@Sun.COM 	}
140610922SJeff.Bonwick@Sun.COM 
140710922SJeff.Bonwick@Sun.COM 	if (bt != NULL) {
140810922SJeff.Bonwick@Sun.COM 		/*
140910922SJeff.Bonwick@Sun.COM 		 * Usually, verify the old data before writing new data --
141010922SJeff.Bonwick@Sun.COM 		 * but not always, because we also want to verify correct
141110922SJeff.Bonwick@Sun.COM 		 * behavior when the data was not recently read into cache.
141210922SJeff.Bonwick@Sun.COM 		 */
141310922SJeff.Bonwick@Sun.COM 		ASSERT(offset % doi.doi_data_block_size == 0);
141410922SJeff.Bonwick@Sun.COM 		if (ztest_random(4) != 0) {
141510922SJeff.Bonwick@Sun.COM 			int prefetch = ztest_random(2) ?
141610922SJeff.Bonwick@Sun.COM 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
141710922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t rbt;
141810922SJeff.Bonwick@Sun.COM 
141910922SJeff.Bonwick@Sun.COM 			VERIFY(dmu_read(os, lr->lr_foid, offset,
142010922SJeff.Bonwick@Sun.COM 			    sizeof (rbt), &rbt, prefetch) == 0);
142110922SJeff.Bonwick@Sun.COM 			if (rbt.bt_magic == BT_MAGIC) {
142210922SJeff.Bonwick@Sun.COM 				ztest_bt_verify(&rbt, os, lr->lr_foid,
142310922SJeff.Bonwick@Sun.COM 				    offset, gen, txg, crtxg);
142410922SJeff.Bonwick@Sun.COM 			}
142510922SJeff.Bonwick@Sun.COM 		}
142610922SJeff.Bonwick@Sun.COM 
142710922SJeff.Bonwick@Sun.COM 		/*
142810922SJeff.Bonwick@Sun.COM 		 * Writes can appear to be newer than the bonus buffer because
142910922SJeff.Bonwick@Sun.COM 		 * the ztest_get_data() callback does a dmu_read() of the
143010922SJeff.Bonwick@Sun.COM 		 * open-context data, which may be different than the data
143110922SJeff.Bonwick@Sun.COM 		 * as it was when the write was generated.
143210922SJeff.Bonwick@Sun.COM 		 */
143310922SJeff.Bonwick@Sun.COM 		if (zd->zd_zilog->zl_replay) {
143410922SJeff.Bonwick@Sun.COM 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
143510922SJeff.Bonwick@Sun.COM 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
143610922SJeff.Bonwick@Sun.COM 			    bt->bt_crtxg);
143710922SJeff.Bonwick@Sun.COM 		}
143810922SJeff.Bonwick@Sun.COM 
143910922SJeff.Bonwick@Sun.COM 		/*
144010922SJeff.Bonwick@Sun.COM 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
144110922SJeff.Bonwick@Sun.COM 		 * so that all of the usual ASSERTs will work.
144210922SJeff.Bonwick@Sun.COM 		 */
144310922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
144410922SJeff.Bonwick@Sun.COM 	}
144510922SJeff.Bonwick@Sun.COM 
144610922SJeff.Bonwick@Sun.COM 	if (abuf == NULL) {
144710922SJeff.Bonwick@Sun.COM 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
144810922SJeff.Bonwick@Sun.COM 	} else {
144910922SJeff.Bonwick@Sun.COM 		bcopy(data, abuf->b_data, length);
145010922SJeff.Bonwick@Sun.COM 		dmu_assign_arcbuf(db, offset, abuf, tx);
145110922SJeff.Bonwick@Sun.COM 	}
145210922SJeff.Bonwick@Sun.COM 
145310922SJeff.Bonwick@Sun.COM 	(void) ztest_log_write(zd, tx, lr);
145410922SJeff.Bonwick@Sun.COM 
145510922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
145610922SJeff.Bonwick@Sun.COM 
145710922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
145810922SJeff.Bonwick@Sun.COM 
145910922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
146010922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
146110922SJeff.Bonwick@Sun.COM 
146210922SJeff.Bonwick@Sun.COM 	return (0);
146310922SJeff.Bonwick@Sun.COM }
146410922SJeff.Bonwick@Sun.COM 
146510922SJeff.Bonwick@Sun.COM static int
146610922SJeff.Bonwick@Sun.COM ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
146710922SJeff.Bonwick@Sun.COM {
146810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
146910922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
147010922SJeff.Bonwick@Sun.COM 	uint64_t txg;
147110922SJeff.Bonwick@Sun.COM 	rl_t *rl;
147210922SJeff.Bonwick@Sun.COM 
147310922SJeff.Bonwick@Sun.COM 	if (byteswap)
147410922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
147510922SJeff.Bonwick@Sun.COM 
147610922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
147710922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
147810922SJeff.Bonwick@Sun.COM 	    RL_WRITER);
147910922SJeff.Bonwick@Sun.COM 
148010922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
148110922SJeff.Bonwick@Sun.COM 
148210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
148310922SJeff.Bonwick@Sun.COM 
148410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
148510922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
148610922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
148710922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
148810922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
148910922SJeff.Bonwick@Sun.COM 	}
149010922SJeff.Bonwick@Sun.COM 
149110922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
149210922SJeff.Bonwick@Sun.COM 	    lr->lr_length, tx) == 0);
149310922SJeff.Bonwick@Sun.COM 
149410922SJeff.Bonwick@Sun.COM 	(void) ztest_log_truncate(zd, tx, lr);
149510922SJeff.Bonwick@Sun.COM 
149610922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
149710922SJeff.Bonwick@Sun.COM 
149810922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
149910922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
150010922SJeff.Bonwick@Sun.COM 
150110922SJeff.Bonwick@Sun.COM 	return (0);
150210922SJeff.Bonwick@Sun.COM }
150310922SJeff.Bonwick@Sun.COM 
150410922SJeff.Bonwick@Sun.COM static int
150510922SJeff.Bonwick@Sun.COM ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
150610922SJeff.Bonwick@Sun.COM {
150710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
150810922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
150910922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
151010922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
151110922SJeff.Bonwick@Sun.COM 	uint64_t txg, lrtxg, crtxg;
151210922SJeff.Bonwick@Sun.COM 
151310922SJeff.Bonwick@Sun.COM 	if (byteswap)
151410922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
151510922SJeff.Bonwick@Sun.COM 
151610922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
151710922SJeff.Bonwick@Sun.COM 
151810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
151910922SJeff.Bonwick@Sun.COM 
152010922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
152110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_bonus(tx, lr->lr_foid);
152210922SJeff.Bonwick@Sun.COM 
152310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
152410922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
152510922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
152610922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
152710922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
152810922SJeff.Bonwick@Sun.COM 	}
152910922SJeff.Bonwick@Sun.COM 
153010922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
153110922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
153210922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
153310922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
153410922SJeff.Bonwick@Sun.COM 
153510922SJeff.Bonwick@Sun.COM 	if (zd->zd_zilog->zl_replay) {
153610922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_size != 0);
153710922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_mode != 0);
153810922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg != 0);
153910922SJeff.Bonwick@Sun.COM 	} else {
154010922SJeff.Bonwick@Sun.COM 		/*
154110922SJeff.Bonwick@Sun.COM 		 * Randomly change the size and increment the generation.
154210922SJeff.Bonwick@Sun.COM 		 */
154310922SJeff.Bonwick@Sun.COM 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
154410922SJeff.Bonwick@Sun.COM 		    sizeof (*bbt);
154510922SJeff.Bonwick@Sun.COM 		lr->lr_mode = bbt->bt_gen + 1;
154610922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg == 0);
154710922SJeff.Bonwick@Sun.COM 	}
154810922SJeff.Bonwick@Sun.COM 
154910922SJeff.Bonwick@Sun.COM 	/*
155010922SJeff.Bonwick@Sun.COM 	 * Verify that the current bonus buffer is not newer than our txg.
155110922SJeff.Bonwick@Sun.COM 	 */
155210922SJeff.Bonwick@Sun.COM 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
155310922SJeff.Bonwick@Sun.COM 	    MAX(txg, lrtxg), crtxg);
155410922SJeff.Bonwick@Sun.COM 
155510922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
155610922SJeff.Bonwick@Sun.COM 
155710922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
155810922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, <=, db->db_size);
155910922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
156010922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
156110922SJeff.Bonwick@Sun.COM 
156210922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
156310922SJeff.Bonwick@Sun.COM 
156410922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
156510922SJeff.Bonwick@Sun.COM 
156610922SJeff.Bonwick@Sun.COM 	(void) ztest_log_setattr(zd, tx, lr);
156710922SJeff.Bonwick@Sun.COM 
156810922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
156910922SJeff.Bonwick@Sun.COM 
157010922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
157110922SJeff.Bonwick@Sun.COM 
157210922SJeff.Bonwick@Sun.COM 	return (0);
1573789Sahrens }
1574789Sahrens 
1575789Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1576789Sahrens 	NULL,			/* 0 no such transaction type */
1577789Sahrens 	ztest_replay_create,	/* TX_CREATE */
1578789Sahrens 	NULL,			/* TX_MKDIR */
1579789Sahrens 	NULL,			/* TX_MKXATTR */
1580789Sahrens 	NULL,			/* TX_SYMLINK */
1581789Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
1582789Sahrens 	NULL,			/* TX_RMDIR */
1583789Sahrens 	NULL,			/* TX_LINK */
1584789Sahrens 	NULL,			/* TX_RENAME */
158510922SJeff.Bonwick@Sun.COM 	ztest_replay_write,	/* TX_WRITE */
158610922SJeff.Bonwick@Sun.COM 	ztest_replay_truncate,	/* TX_TRUNCATE */
158710922SJeff.Bonwick@Sun.COM 	ztest_replay_setattr,	/* TX_SETATTR */
1588789Sahrens 	NULL,			/* TX_ACL */
158910800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL */
159010800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ATTR */
159110800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL_ATTR */
159210800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL */
159310800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ATTR */
159410800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL_ATTR */
159510800SNeil.Perrin@Sun.COM 	NULL,			/* TX_WRITE2 */
1596789Sahrens };
1597789Sahrens 
1598789Sahrens /*
159910922SJeff.Bonwick@Sun.COM  * ZIL get_data callbacks
160010922SJeff.Bonwick@Sun.COM  */
160110922SJeff.Bonwick@Sun.COM 
160210922SJeff.Bonwick@Sun.COM static void
160310922SJeff.Bonwick@Sun.COM ztest_get_done(zgd_t *zgd, int error)
160410922SJeff.Bonwick@Sun.COM {
160510922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = zgd->zgd_private;
160610922SJeff.Bonwick@Sun.COM 	uint64_t object = zgd->zgd_rl->rl_object;
160710922SJeff.Bonwick@Sun.COM 
160810922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
160910922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
161010922SJeff.Bonwick@Sun.COM 
161110922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(zgd->zgd_rl);
161210922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
161310922SJeff.Bonwick@Sun.COM 
161410922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
161510922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
161610922SJeff.Bonwick@Sun.COM 
161710922SJeff.Bonwick@Sun.COM 	umem_free(zgd, sizeof (*zgd));
161810922SJeff.Bonwick@Sun.COM }
161910922SJeff.Bonwick@Sun.COM 
162010922SJeff.Bonwick@Sun.COM static int
162110922SJeff.Bonwick@Sun.COM ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
162210922SJeff.Bonwick@Sun.COM {
162310922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = arg;
162410922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
162510922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
162610922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
162710922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
162810922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
162910922SJeff.Bonwick@Sun.COM 	uint64_t txg = lr->lr_common.lrc_txg;
163010922SJeff.Bonwick@Sun.COM 	uint64_t crtxg;
163110922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
163210922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
163310922SJeff.Bonwick@Sun.COM 	zgd_t *zgd;
163410922SJeff.Bonwick@Sun.COM 	int error;
163510922SJeff.Bonwick@Sun.COM 
163610922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
163710922SJeff.Bonwick@Sun.COM 	error = dmu_bonus_hold(os, object, FTAG, &db);
163810922SJeff.Bonwick@Sun.COM 	if (error) {
163910922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
164010922SJeff.Bonwick@Sun.COM 		return (error);
164110922SJeff.Bonwick@Sun.COM 	}
164210922SJeff.Bonwick@Sun.COM 
164310922SJeff.Bonwick@Sun.COM 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
164410922SJeff.Bonwick@Sun.COM 
164510922SJeff.Bonwick@Sun.COM 	if (crtxg == 0 || crtxg > txg) {
164610922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
164710922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
164810922SJeff.Bonwick@Sun.COM 		return (ENOENT);
164910922SJeff.Bonwick@Sun.COM 	}
165010922SJeff.Bonwick@Sun.COM 
165110922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
165210922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
165310922SJeff.Bonwick@Sun.COM 	db = NULL;
165410922SJeff.Bonwick@Sun.COM 
165510922SJeff.Bonwick@Sun.COM 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
165610922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zd->zd_zilog;
165710922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zd;
165810922SJeff.Bonwick@Sun.COM 
165910922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
166010922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
166110922SJeff.Bonwick@Sun.COM 		    RL_READER);
166210922SJeff.Bonwick@Sun.COM 
166310922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
166410922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
166510922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0);
166610922SJeff.Bonwick@Sun.COM 	} else {
166710922SJeff.Bonwick@Sun.COM 		size = doi.doi_data_block_size;
166810945SJeff.Bonwick@Sun.COM 		if (ISP2(size)) {
166910922SJeff.Bonwick@Sun.COM 			offset = P2ALIGN(offset, size);
167010945SJeff.Bonwick@Sun.COM 		} else {
167110945SJeff.Bonwick@Sun.COM 			ASSERT(offset < size);
167210945SJeff.Bonwick@Sun.COM 			offset = 0;
167310945SJeff.Bonwick@Sun.COM 		}
167410922SJeff.Bonwick@Sun.COM 
167510922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
167610922SJeff.Bonwick@Sun.COM 		    RL_READER);
167710922SJeff.Bonwick@Sun.COM 
167812285SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db,
167912285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
168010922SJeff.Bonwick@Sun.COM 
168110922SJeff.Bonwick@Sun.COM 		if (error == 0) {
168210922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
168310922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
168410922SJeff.Bonwick@Sun.COM 
168510922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
168610922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
168710922SJeff.Bonwick@Sun.COM 
168810922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
168910922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
169010922SJeff.Bonwick@Sun.COM 
169110922SJeff.Bonwick@Sun.COM 			if (error == 0)
169210922SJeff.Bonwick@Sun.COM 				return (0);
169310922SJeff.Bonwick@Sun.COM 		}
169410922SJeff.Bonwick@Sun.COM 	}
169510922SJeff.Bonwick@Sun.COM 
169610922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
169710922SJeff.Bonwick@Sun.COM 
169810922SJeff.Bonwick@Sun.COM 	return (error);
169910922SJeff.Bonwick@Sun.COM }
170010922SJeff.Bonwick@Sun.COM 
170110922SJeff.Bonwick@Sun.COM static void *
170210922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
170310922SJeff.Bonwick@Sun.COM {
170410922SJeff.Bonwick@Sun.COM 	char *lr;
170510922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
170610922SJeff.Bonwick@Sun.COM 
170710922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
170810922SJeff.Bonwick@Sun.COM 
170910922SJeff.Bonwick@Sun.COM 	if (name)
171010922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
171110922SJeff.Bonwick@Sun.COM 
171210922SJeff.Bonwick@Sun.COM 	return (lr);
171310922SJeff.Bonwick@Sun.COM }
171410922SJeff.Bonwick@Sun.COM 
171510922SJeff.Bonwick@Sun.COM void
171610922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
171710922SJeff.Bonwick@Sun.COM {
171810922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
171910922SJeff.Bonwick@Sun.COM 
172010922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
172110922SJeff.Bonwick@Sun.COM }
172210922SJeff.Bonwick@Sun.COM 
172310922SJeff.Bonwick@Sun.COM /*
172410922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
172510922SJeff.Bonwick@Sun.COM  */
172610922SJeff.Bonwick@Sun.COM static int
172710922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
172810922SJeff.Bonwick@Sun.COM {
172910922SJeff.Bonwick@Sun.COM 	int missing = 0;
173010922SJeff.Bonwick@Sun.COM 	int error;
173110922SJeff.Bonwick@Sun.COM 
173210922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
173310922SJeff.Bonwick@Sun.COM 
173410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
173510922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
173610922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
173710922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
173810922SJeff.Bonwick@Sun.COM 		if (error) {
173910922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
174010922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
174110922SJeff.Bonwick@Sun.COM 			missing++;
174210922SJeff.Bonwick@Sun.COM 		} else {
174310922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
174410922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
174510922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
174610922SJeff.Bonwick@Sun.COM 
174710922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
174810922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
174910922SJeff.Bonwick@Sun.COM 
175010922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
175110922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
175210922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
175310922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
175410922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
175510922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
175610922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
175710922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
175810922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
175910922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
176010922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
176110922SJeff.Bonwick@Sun.COM 		}
176210922SJeff.Bonwick@Sun.COM 	}
176310922SJeff.Bonwick@Sun.COM 
176410922SJeff.Bonwick@Sun.COM 	return (missing);
176510922SJeff.Bonwick@Sun.COM }
176610922SJeff.Bonwick@Sun.COM 
176710922SJeff.Bonwick@Sun.COM static int
176810922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
176910922SJeff.Bonwick@Sun.COM {
177010922SJeff.Bonwick@Sun.COM 	int missing = 0;
177110922SJeff.Bonwick@Sun.COM 
177210922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
177310922SJeff.Bonwick@Sun.COM 
177410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
177510922SJeff.Bonwick@Sun.COM 		if (missing) {
177610922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
177710922SJeff.Bonwick@Sun.COM 			missing++;
177810922SJeff.Bonwick@Sun.COM 			continue;
177910922SJeff.Bonwick@Sun.COM 		}
178010922SJeff.Bonwick@Sun.COM 
178110922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
178210922SJeff.Bonwick@Sun.COM 
178310922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
178410922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
178510922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
178610922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
178710922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
178810922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
178910922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
179010922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
179110922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
179210922SJeff.Bonwick@Sun.COM 
179310922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
179410922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
179510922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
179610922SJeff.Bonwick@Sun.COM 			missing++;
179710922SJeff.Bonwick@Sun.COM 		} else {
179810922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
179910922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
180010922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
180110922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
180210922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
180310922SJeff.Bonwick@Sun.COM 		}
180410922SJeff.Bonwick@Sun.COM 
180510922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
180610922SJeff.Bonwick@Sun.COM 	}
180710922SJeff.Bonwick@Sun.COM 
180810922SJeff.Bonwick@Sun.COM 	return (missing);
180910922SJeff.Bonwick@Sun.COM }
181010922SJeff.Bonwick@Sun.COM 
181110922SJeff.Bonwick@Sun.COM static int
181210922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
181310922SJeff.Bonwick@Sun.COM {
181410922SJeff.Bonwick@Sun.COM 	int missing = 0;
181510922SJeff.Bonwick@Sun.COM 	int error;
181610922SJeff.Bonwick@Sun.COM 
181710922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
181810922SJeff.Bonwick@Sun.COM 
181910922SJeff.Bonwick@Sun.COM 	od += count - 1;
182010922SJeff.Bonwick@Sun.COM 
182110922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
182210922SJeff.Bonwick@Sun.COM 		if (missing) {
182310922SJeff.Bonwick@Sun.COM 			missing++;
182410922SJeff.Bonwick@Sun.COM 			continue;
182510922SJeff.Bonwick@Sun.COM 		}
182610922SJeff.Bonwick@Sun.COM 
182710922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
182810922SJeff.Bonwick@Sun.COM 			continue;
182910922SJeff.Bonwick@Sun.COM 
183010922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
183110922SJeff.Bonwick@Sun.COM 
183210922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
183310922SJeff.Bonwick@Sun.COM 
183410922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
183510922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
183610922SJeff.Bonwick@Sun.COM 			missing++;
183710922SJeff.Bonwick@Sun.COM 		} else {
183810922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
183910922SJeff.Bonwick@Sun.COM 		}
184010922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
184110922SJeff.Bonwick@Sun.COM 	}
184210922SJeff.Bonwick@Sun.COM 
184310922SJeff.Bonwick@Sun.COM 	return (missing);
184410922SJeff.Bonwick@Sun.COM }
184510922SJeff.Bonwick@Sun.COM 
184610922SJeff.Bonwick@Sun.COM static int
184710922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
184810922SJeff.Bonwick@Sun.COM     void *data)
184910922SJeff.Bonwick@Sun.COM {
185010922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
185110922SJeff.Bonwick@Sun.COM 	int error;
185210922SJeff.Bonwick@Sun.COM 
185310922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
185410922SJeff.Bonwick@Sun.COM 
185510922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
185610922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
185710922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
185810922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
185910922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
186010922SJeff.Bonwick@Sun.COM 
186110922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
186210922SJeff.Bonwick@Sun.COM 
186310922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
186410922SJeff.Bonwick@Sun.COM 
186510922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
186610922SJeff.Bonwick@Sun.COM 
186710922SJeff.Bonwick@Sun.COM 	return (error);
186810922SJeff.Bonwick@Sun.COM }
186910922SJeff.Bonwick@Sun.COM 
187010922SJeff.Bonwick@Sun.COM static int
187110922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
187210922SJeff.Bonwick@Sun.COM {
187310922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
187410922SJeff.Bonwick@Sun.COM 	int error;
187510922SJeff.Bonwick@Sun.COM 
187610922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
187710922SJeff.Bonwick@Sun.COM 
187810922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
187910922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
188010922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
188110922SJeff.Bonwick@Sun.COM 
188210922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
188310922SJeff.Bonwick@Sun.COM 
188410922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
188510922SJeff.Bonwick@Sun.COM 
188610922SJeff.Bonwick@Sun.COM 	return (error);
188710922SJeff.Bonwick@Sun.COM }
188810922SJeff.Bonwick@Sun.COM 
188910922SJeff.Bonwick@Sun.COM static int
189010922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
189110922SJeff.Bonwick@Sun.COM {
189210922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
189310922SJeff.Bonwick@Sun.COM 	int error;
189410922SJeff.Bonwick@Sun.COM 
189510922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
189610922SJeff.Bonwick@Sun.COM 
189710922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
189810922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
189910922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
190010922SJeff.Bonwick@Sun.COM 
190110922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
190210922SJeff.Bonwick@Sun.COM 
190310922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
190410922SJeff.Bonwick@Sun.COM 
190510922SJeff.Bonwick@Sun.COM 	return (error);
190610922SJeff.Bonwick@Sun.COM }
190710922SJeff.Bonwick@Sun.COM 
190810922SJeff.Bonwick@Sun.COM static void
190910922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
191010922SJeff.Bonwick@Sun.COM {
191110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
191210922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
191310922SJeff.Bonwick@Sun.COM 	uint64_t txg;
191410922SJeff.Bonwick@Sun.COM 	rl_t *rl;
191510922SJeff.Bonwick@Sun.COM 
191610922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
191710922SJeff.Bonwick@Sun.COM 
191810922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
191910922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
192010922SJeff.Bonwick@Sun.COM 
192110922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
192210922SJeff.Bonwick@Sun.COM 
192310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
192410922SJeff.Bonwick@Sun.COM 
192510922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
192610922SJeff.Bonwick@Sun.COM 
192710922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
192810922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
192910922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
193010922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
193110922SJeff.Bonwick@Sun.COM 	} else {
193210922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
193310922SJeff.Bonwick@Sun.COM 	}
193410922SJeff.Bonwick@Sun.COM 
193510922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
193610922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
193710922SJeff.Bonwick@Sun.COM }
193810922SJeff.Bonwick@Sun.COM 
193910922SJeff.Bonwick@Sun.COM static void
194010922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
194110922SJeff.Bonwick@Sun.COM {
194210922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
194310922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
194410922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
194510922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
194610922SJeff.Bonwick@Sun.COM 	void *data;
194710922SJeff.Bonwick@Sun.COM 
194810922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
194910922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
195010922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
195110922SJeff.Bonwick@Sun.COM 
195210922SJeff.Bonwick@Sun.COM 	/*
195310922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
195410922SJeff.Bonwick@Sun.COM 	 */
195510922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
195610922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
195710922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
195810922SJeff.Bonwick@Sun.COM 
195910922SJeff.Bonwick@Sun.COM 	switch (io_type) {
196010922SJeff.Bonwick@Sun.COM 
196110922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
196210922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
196310922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
196410922SJeff.Bonwick@Sun.COM 		break;
196510922SJeff.Bonwick@Sun.COM 
196610922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
196710922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
196810922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
196910922SJeff.Bonwick@Sun.COM 			/*
197010922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
197110922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
197210922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
197310922SJeff.Bonwick@Sun.COM 			 */
197410922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
197510922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
197610922SJeff.Bonwick@Sun.COM 		}
197710922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
197810922SJeff.Bonwick@Sun.COM 		break;
197910922SJeff.Bonwick@Sun.COM 
198010922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
198110922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
198210922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
198310922SJeff.Bonwick@Sun.COM 		break;
198410922SJeff.Bonwick@Sun.COM 
198510922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
198610922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
198710922SJeff.Bonwick@Sun.COM 		break;
198810922SJeff.Bonwick@Sun.COM 
198910922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
199010922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
199110922SJeff.Bonwick@Sun.COM 		break;
199210922SJeff.Bonwick@Sun.COM 	}
199310922SJeff.Bonwick@Sun.COM 
199410922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
199510922SJeff.Bonwick@Sun.COM }
199610922SJeff.Bonwick@Sun.COM 
199710922SJeff.Bonwick@Sun.COM /*
199810922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
199910922SJeff.Bonwick@Sun.COM  */
200010922SJeff.Bonwick@Sun.COM static void
200110922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
200210922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
200310922SJeff.Bonwick@Sun.COM {
200410922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
200510922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
200610922SJeff.Bonwick@Sun.COM 
200710922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
200810922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
200910922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
201010922SJeff.Bonwick@Sun.COM 
201110922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
201210922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
201310922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
201410922SJeff.Bonwick@Sun.COM 
201510922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
201610922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
201710922SJeff.Bonwick@Sun.COM }
201810922SJeff.Bonwick@Sun.COM 
201910922SJeff.Bonwick@Sun.COM /*
202010922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
202110922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
202210922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
202310922SJeff.Bonwick@Sun.COM  * use the existing objects.
202410922SJeff.Bonwick@Sun.COM  */
202510922SJeff.Bonwick@Sun.COM static int
202610922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
202710922SJeff.Bonwick@Sun.COM {
202810922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
202910922SJeff.Bonwick@Sun.COM 	int rv = 0;
203010922SJeff.Bonwick@Sun.COM 
203110922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
203210922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
203310922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
203410922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
203510922SJeff.Bonwick@Sun.COM 		rv = -1;
203610922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
203710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
203810922SJeff.Bonwick@Sun.COM 
203910922SJeff.Bonwick@Sun.COM 	return (rv);
204010922SJeff.Bonwick@Sun.COM }
204110922SJeff.Bonwick@Sun.COM 
204210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
204310922SJeff.Bonwick@Sun.COM void
204410922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
204510922SJeff.Bonwick@Sun.COM {
204610922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
204710922SJeff.Bonwick@Sun.COM 
204810922SJeff.Bonwick@Sun.COM 	zil_commit(zilog, UINT64_MAX, ztest_random(ZTEST_OBJECTS));
204910922SJeff.Bonwick@Sun.COM 
205010922SJeff.Bonwick@Sun.COM 	/*
205110922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
205210922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
205310922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
205410922SJeff.Bonwick@Sun.COM 	 */
205510922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
205610922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
205710922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
205810922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
205910922SJeff.Bonwick@Sun.COM }
206010922SJeff.Bonwick@Sun.COM 
206110922SJeff.Bonwick@Sun.COM /*
2062789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2063789Sahrens  * or create a pool with a bad vdev spec.
2064789Sahrens  */
206510922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2066789Sahrens void
206710922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2068789Sahrens {
206910922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2070789Sahrens 	spa_t *spa;
2071789Sahrens 	nvlist_t *nvroot;
2072789Sahrens 
2073789Sahrens 	/*
2074789Sahrens 	 * Attempt to create using a bad file.
2075789Sahrens 	 */
20767754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
207710922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
207810922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2079789Sahrens 	nvlist_free(nvroot);
2080789Sahrens 
2081789Sahrens 	/*
2082789Sahrens 	 * Attempt to create using a bad mirror.
2083789Sahrens 	 */
20847754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
208510922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
208610922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2087789Sahrens 	nvlist_free(nvroot);
2088789Sahrens 
2089789Sahrens 	/*
2090789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2091789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2092789Sahrens 	 */
209310922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20947754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
209510922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2096789Sahrens 	nvlist_free(nvroot);
209710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
209810922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2099789Sahrens 	spa_close(spa, FTAG);
210010922SJeff.Bonwick@Sun.COM 
210110922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2102789Sahrens }
2103789Sahrens 
21047768SJeff.Bonwick@Sun.COM static vdev_t *
21057768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
21067768SJeff.Bonwick@Sun.COM {
21077768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
21087768SJeff.Bonwick@Sun.COM 
21097768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
21107768SJeff.Bonwick@Sun.COM 		return (vd);
21117768SJeff.Bonwick@Sun.COM 
21127768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
21137768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
21147768SJeff.Bonwick@Sun.COM 		    NULL)
21157768SJeff.Bonwick@Sun.COM 			return (mvd);
21167768SJeff.Bonwick@Sun.COM 
21177768SJeff.Bonwick@Sun.COM 	return (NULL);
21187768SJeff.Bonwick@Sun.COM }
21197768SJeff.Bonwick@Sun.COM 
2120789Sahrens /*
212110594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
212210594SGeorge.Wilson@Sun.COM  */
212310594SGeorge.Wilson@Sun.COM int
212410594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
212510594SGeorge.Wilson@Sun.COM {
212610594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
212710594SGeorge.Wilson@Sun.COM 	int c;
212810594SGeorge.Wilson@Sun.COM 
212910594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
213010594SGeorge.Wilson@Sun.COM 
213110594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
213210594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
213310594SGeorge.Wilson@Sun.COM 
213410594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
213510594SGeorge.Wilson@Sun.COM 			break;
213610594SGeorge.Wilson@Sun.COM 	}
213710594SGeorge.Wilson@Sun.COM 	return (c);
213810594SGeorge.Wilson@Sun.COM }
213910594SGeorge.Wilson@Sun.COM 
214010594SGeorge.Wilson@Sun.COM /*
2141789Sahrens  * Verify that vdev_add() works as expected.
2142789Sahrens  */
214310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2144789Sahrens void
214510922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2146789Sahrens {
214710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
214810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
214911422SMark.Musante@Sun.COM 	uint64_t leaves;
215010594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2151789Sahrens 	nvlist_t *nvroot;
2152789Sahrens 	int error;
2153789Sahrens 
215410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
215511422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2156789Sahrens 
21577754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2158789Sahrens 
215910594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2160789Sahrens 
21614527Sperrin 	/*
216210594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21634527Sperrin 	 */
216410594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
216510594SGeorge.Wilson@Sun.COM 		/*
216610594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
216710594SGeorge.Wilson@Sun.COM 		 */
216810922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
216910594SGeorge.Wilson@Sun.COM 
217010594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
217110594SGeorge.Wilson@Sun.COM 
217210594SGeorge.Wilson@Sun.COM 		/*
217310594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
217410594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
217510594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
217610594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
217710594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
217810594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
217910594SGeorge.Wilson@Sun.COM 		 */
218010922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
218110594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
218210922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
218310594SGeorge.Wilson@Sun.COM 
218410594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
218510594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
218610594SGeorge.Wilson@Sun.COM 	} else {
218710594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
218810594SGeorge.Wilson@Sun.COM 
218910594SGeorge.Wilson@Sun.COM 		/*
219010594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
219110594SGeorge.Wilson@Sun.COM 		 */
219210594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
219311422SMark.Musante@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
219410594SGeorge.Wilson@Sun.COM 
219510594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
219610594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
219710594SGeorge.Wilson@Sun.COM 
219810594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
219910594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
220010594SGeorge.Wilson@Sun.COM 		else if (error != 0)
220110594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
220210594SGeorge.Wilson@Sun.COM 	}
2203789Sahrens 
220410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
22057754SJeff.Bonwick@Sun.COM }
22067754SJeff.Bonwick@Sun.COM 
22077754SJeff.Bonwick@Sun.COM /*
22087754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
22097754SJeff.Bonwick@Sun.COM  */
221010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
22117754SJeff.Bonwick@Sun.COM void
221210922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
22137754SJeff.Bonwick@Sun.COM {
221410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
221510922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22167768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22177754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22187754SJeff.Bonwick@Sun.COM 	char *aux;
22197754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22207754SJeff.Bonwick@Sun.COM 	int error;
22217754SJeff.Bonwick@Sun.COM 
22227768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22237754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22247754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22257754SJeff.Bonwick@Sun.COM 	} else {
22267754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22277754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22287754SJeff.Bonwick@Sun.COM 	}
22297754SJeff.Bonwick@Sun.COM 
223010922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22317754SJeff.Bonwick@Sun.COM 
22327754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22337754SJeff.Bonwick@Sun.COM 
22347754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22357754SJeff.Bonwick@Sun.COM 		/*
22367754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22377754SJeff.Bonwick@Sun.COM 		 */
22387754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22397754SJeff.Bonwick@Sun.COM 	} else {
22407754SJeff.Bonwick@Sun.COM 		/*
22417754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22427754SJeff.Bonwick@Sun.COM 		 */
224310922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22447754SJeff.Bonwick@Sun.COM 		for (;;) {
22457754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22467754SJeff.Bonwick@Sun.COM 			int c;
22477754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
224810922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22497754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22507754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22517754SJeff.Bonwick@Sun.COM 				    path) == 0)
22527754SJeff.Bonwick@Sun.COM 					break;
22537768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22547768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22557754SJeff.Bonwick@Sun.COM 				break;
225610922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22577754SJeff.Bonwick@Sun.COM 		}
22587754SJeff.Bonwick@Sun.COM 	}
22597754SJeff.Bonwick@Sun.COM 
22607754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22617754SJeff.Bonwick@Sun.COM 
22627754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22637754SJeff.Bonwick@Sun.COM 		/*
22647754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22657754SJeff.Bonwick@Sun.COM 		 */
22667768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22677768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22687754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22697754SJeff.Bonwick@Sun.COM 		if (error != 0)
22707754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22717754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22727754SJeff.Bonwick@Sun.COM 	} else {
22737754SJeff.Bonwick@Sun.COM 		/*
22747754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22757754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22767754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22777754SJeff.Bonwick@Sun.COM 		 */
22787754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22799816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22807754SJeff.Bonwick@Sun.COM 
22817754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22827754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22837754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22847754SJeff.Bonwick@Sun.COM 	}
22857754SJeff.Bonwick@Sun.COM 
228610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2287789Sahrens }
2288789Sahrens 
2289789Sahrens /*
229011422SMark.Musante@Sun.COM  * split a pool if it has mirror tlvdevs
229111422SMark.Musante@Sun.COM  */
229211422SMark.Musante@Sun.COM /* ARGSUSED */
229311422SMark.Musante@Sun.COM void
229411422SMark.Musante@Sun.COM ztest_split_pool(ztest_ds_t *zd, uint64_t id)
229511422SMark.Musante@Sun.COM {
229611422SMark.Musante@Sun.COM 	ztest_shared_t *zs = ztest_shared;
229711422SMark.Musante@Sun.COM 	spa_t *spa = zs->zs_spa;
229811422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
229911422SMark.Musante@Sun.COM 	nvlist_t *tree, **child, *config, *split, **schild;
230011422SMark.Musante@Sun.COM 	uint_t c, children, schildren = 0, lastlogid = 0;
230111422SMark.Musante@Sun.COM 	int error = 0;
230211422SMark.Musante@Sun.COM 
230311422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
230411422SMark.Musante@Sun.COM 
230511422SMark.Musante@Sun.COM 	/* ensure we have a useable config; mirrors of raidz aren't supported */
230611422SMark.Musante@Sun.COM 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
230711422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
230811422SMark.Musante@Sun.COM 		return;
230911422SMark.Musante@Sun.COM 	}
231011422SMark.Musante@Sun.COM 
231111422SMark.Musante@Sun.COM 	/* clean up the old pool, if any */
231211422SMark.Musante@Sun.COM 	(void) spa_destroy("splitp");
231311422SMark.Musante@Sun.COM 
231411422SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
231511422SMark.Musante@Sun.COM 
231611422SMark.Musante@Sun.COM 	/* generate a config from the existing config */
231711864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
231811422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
231911422SMark.Musante@Sun.COM 	    &tree) == 0);
232011864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
232111864SMark.Musante@Sun.COM 
232211422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
232311422SMark.Musante@Sun.COM 	    &children) == 0);
232411422SMark.Musante@Sun.COM 
232511422SMark.Musante@Sun.COM 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
232611422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
232711422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
232811422SMark.Musante@Sun.COM 		nvlist_t **mchild;
232911422SMark.Musante@Sun.COM 		uint_t mchildren;
233011422SMark.Musante@Sun.COM 
233111422SMark.Musante@Sun.COM 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
233211422SMark.Musante@Sun.COM 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
233311422SMark.Musante@Sun.COM 			    0) == 0);
233411422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_string(schild[schildren],
233511422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
233611422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(schild[schildren],
233711422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
233811422SMark.Musante@Sun.COM 			if (lastlogid == 0)
233911422SMark.Musante@Sun.COM 				lastlogid = schildren;
234011422SMark.Musante@Sun.COM 			++schildren;
234111422SMark.Musante@Sun.COM 			continue;
234211422SMark.Musante@Sun.COM 		}
234311422SMark.Musante@Sun.COM 		lastlogid = 0;
234411422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist_array(child[c],
234511422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
234611422SMark.Musante@Sun.COM 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
234711422SMark.Musante@Sun.COM 	}
234811422SMark.Musante@Sun.COM 
234911422SMark.Musante@Sun.COM 	/* OK, create a config that can be used to split */
235011422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
235111422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
235211422SMark.Musante@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
235311422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
235411422SMark.Musante@Sun.COM 	    lastlogid != 0 ? lastlogid : schildren) == 0);
235511422SMark.Musante@Sun.COM 
235611422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
235711422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
235811422SMark.Musante@Sun.COM 
235911422SMark.Musante@Sun.COM 	for (c = 0; c < schildren; c++)
236011422SMark.Musante@Sun.COM 		nvlist_free(schild[c]);
236111422SMark.Musante@Sun.COM 	free(schild);
236211422SMark.Musante@Sun.COM 	nvlist_free(split);
236311422SMark.Musante@Sun.COM 
236411422SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
236511422SMark.Musante@Sun.COM 
236611422SMark.Musante@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
236711422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
236811422SMark.Musante@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
236911422SMark.Musante@Sun.COM 
237011422SMark.Musante@Sun.COM 	nvlist_free(config);
237111422SMark.Musante@Sun.COM 
237211422SMark.Musante@Sun.COM 	if (error == 0) {
237311422SMark.Musante@Sun.COM 		(void) printf("successful split - results:\n");
237411422SMark.Musante@Sun.COM 		mutex_enter(&spa_namespace_lock);
237511422SMark.Musante@Sun.COM 		show_pool_stats(spa);
237611422SMark.Musante@Sun.COM 		show_pool_stats(spa_lookup("splitp"));
237711422SMark.Musante@Sun.COM 		mutex_exit(&spa_namespace_lock);
237811422SMark.Musante@Sun.COM 		++zs->zs_splits;
237911422SMark.Musante@Sun.COM 		--zs->zs_mirrors;
238011422SMark.Musante@Sun.COM 	}
238111422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
238211422SMark.Musante@Sun.COM 
238311422SMark.Musante@Sun.COM }
238411422SMark.Musante@Sun.COM 
238511422SMark.Musante@Sun.COM /*
2386789Sahrens  * Verify that we can attach and detach devices.
2387789Sahrens  */
238810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2389789Sahrens void
239010922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2391789Sahrens {
239210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
239310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
23947768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2395789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23961544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
23977754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
239811422SMark.Musante@Sun.COM 	uint64_t leaves;
2399789Sahrens 	uint64_t leaf, top;
24001732Sbonwick 	uint64_t ashift = ztest_get_ashift();
24018241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
24021544Seschrock 	size_t oldsize, newsize;
24031544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2404789Sahrens 	int replacing;
24057793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
24067768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
24077768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2408789Sahrens 	int error, expected_error;
2409789Sahrens 
241010922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
241111422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2412789Sahrens 
24137754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2414789Sahrens 
2415789Sahrens 	/*
2416789Sahrens 	 * Decide whether to do an attach or a replace.
2417789Sahrens 	 */
2418789Sahrens 	replacing = ztest_random(2);
2419789Sahrens 
2420789Sahrens 	/*
2421789Sahrens 	 * Pick a random top-level vdev.
2422789Sahrens 	 */
242310922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2424789Sahrens 
2425789Sahrens 	/*
2426789Sahrens 	 * Pick a random leaf within it.
2427789Sahrens 	 */
242811497SMark.Musante@Sun.COM 	leaf = ztest_random(leaves);
2429789Sahrens 
2430789Sahrens 	/*
24317768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2432789Sahrens 	 */
24337768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
243411422SMark.Musante@Sun.COM 	if (zs->zs_mirrors >= 1) {
24358241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
243611422SMark.Musante@Sun.COM 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
24377768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
24388241SJeff.Bonwick@Sun.COM 	}
24398241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
24408241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
24418241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
24427768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
24438241SJeff.Bonwick@Sun.COM 	}
2444789Sahrens 
2445789Sahrens 	/*
24467768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
24477768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2448789Sahrens 	 */
24497768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
24507793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
24518241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
24528241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
24537768SJeff.Bonwick@Sun.COM 	}
24547768SJeff.Bonwick@Sun.COM 
24557768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
24569816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
24577768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
24587768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
24591544Seschrock 	pvd = oldvd->vdev_parent;
24608241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2461789Sahrens 
2462789Sahrens 	/*
24637793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
24647793SJeff.Bonwick@Sun.COM 	 */
24657793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
24667793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
24678241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
24688241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
24698241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
24708241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
247110922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
24727793SJeff.Bonwick@Sun.COM 		return;
24737793SJeff.Bonwick@Sun.COM 	}
24747793SJeff.Bonwick@Sun.COM 
24757793SJeff.Bonwick@Sun.COM 	/*
24767768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
24777768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2478789Sahrens 	 */
24797768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
24807768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
24817768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
24827768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
24837768SJeff.Bonwick@Sun.COM 	} else {
24847768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
24857768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
24867768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
24877768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
24887768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
24897768SJeff.Bonwick@Sun.COM 	}
24907768SJeff.Bonwick@Sun.COM 
24917768SJeff.Bonwick@Sun.COM 	if (newvd) {
24929816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
24937768SJeff.Bonwick@Sun.COM 	} else {
24947768SJeff.Bonwick@Sun.COM 		/*
24957768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
24967768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
24977768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
24987768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
24997768SJeff.Bonwick@Sun.COM 		 */
25007768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
25017768SJeff.Bonwick@Sun.COM 	}
2502789Sahrens 
2503789Sahrens 	/*
2504789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2505789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2506789Sahrens 	 *
25071544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2508789Sahrens 	 *
25091544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2510789Sahrens 	 */
25117768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
25127768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
25137768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
25147768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
25157768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25167768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
25177768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25187768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
25197768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
25207768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
25212174Seschrock 		expected_error = EBUSY;
25221544Seschrock 	else if (newsize < oldsize)
2523789Sahrens 		expected_error = EOVERFLOW;
25241732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
25251732Sbonwick 		expected_error = EDOM;
2526789Sahrens 	else
2527789Sahrens 		expected_error = 0;
2528789Sahrens 
25297754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2530789Sahrens 
2531789Sahrens 	/*
25321544Seschrock 	 * Build the nvlist describing newpath.
2533789Sahrens 	 */
25347754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
25357754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2536789Sahrens 
25377768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2538789Sahrens 
2539789Sahrens 	nvlist_free(root);
2540789Sahrens 
2541789Sahrens 	/*
2542789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2543789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2544789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2545789Sahrens 	 */
2546789Sahrens 	if (expected_error == ENOTSUP &&
2547789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2548789Sahrens 		expected_error = error;
2549789Sahrens 
2550797Sbonwick 	/*
2551797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2552797Sbonwick 	 */
25537046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2554797Sbonwick 		expected_error = error;
2555797Sbonwick 
25567046Sahrens 	/* XXX workaround 6690467 */
25577046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
25587046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
25597046Sahrens 		    "returned %d, expected %d",
25607046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
25617046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2562789Sahrens 	}
2563789Sahrens 
256410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2565789Sahrens }
2566789Sahrens 
2567789Sahrens /*
25689816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
25699816SGeorge.Wilson@Sun.COM  */
25709816SGeorge.Wilson@Sun.COM vdev_t *
25719816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
25729816SGeorge.Wilson@Sun.COM {
25739816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25749816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
25759816SGeorge.Wilson@Sun.COM 	size_t fsize;
25769816SGeorge.Wilson@Sun.COM 	int fd;
25779816SGeorge.Wilson@Sun.COM 
25789816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25799816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25809816SGeorge.Wilson@Sun.COM 
25819816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
25829816SGeorge.Wilson@Sun.COM 		return (vd);
25839816SGeorge.Wilson@Sun.COM 
25849816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
25859816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
25869816SGeorge.Wilson@Sun.COM 
25879816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
25889816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
25899816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
25909816SGeorge.Wilson@Sun.COM 	}
25919816SGeorge.Wilson@Sun.COM 	(void) close(fd);
25929816SGeorge.Wilson@Sun.COM 	return (NULL);
25939816SGeorge.Wilson@Sun.COM }
25949816SGeorge.Wilson@Sun.COM 
25959816SGeorge.Wilson@Sun.COM /*
25969816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
25979816SGeorge.Wilson@Sun.COM  */
25989816SGeorge.Wilson@Sun.COM /* ARGSUSED */
25999816SGeorge.Wilson@Sun.COM vdev_t *
26009816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
26019816SGeorge.Wilson@Sun.COM {
26029816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
26039816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
26049816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
260510685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
260610850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
260710850SGeorge.Wilson@Sun.COM 	int error;
26089816SGeorge.Wilson@Sun.COM 
26099816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
26109816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
26119816SGeorge.Wilson@Sun.COM 
26129816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
26139816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
261410850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
26159816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26169816SGeorge.Wilson@Sun.COM 
26179816SGeorge.Wilson@Sun.COM 	/*
261810850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
261910850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
262010850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
262110850SGeorge.Wilson@Sun.COM 	 */
262210850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
262310850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
262410850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
262510850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
262610850SGeorge.Wilson@Sun.COM 		}
262710850SGeorge.Wilson@Sun.COM 		return (vd);
262810850SGeorge.Wilson@Sun.COM 	}
262910850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
263010850SGeorge.Wilson@Sun.COM 
263110850SGeorge.Wilson@Sun.COM 	/*
26329816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
26339816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
26349816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
26359816SGeorge.Wilson@Sun.COM 	 * trying to online it.
26369816SGeorge.Wilson@Sun.COM 	 */
263710685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
263810685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
263910685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
264010685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
264110922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
264210922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
264310685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
264410685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
264510685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
26469816SGeorge.Wilson@Sun.COM 		}
26479816SGeorge.Wilson@Sun.COM 		return (vd);
26489816SGeorge.Wilson@Sun.COM 	}
26499816SGeorge.Wilson@Sun.COM 	return (NULL);
26509816SGeorge.Wilson@Sun.COM }
26519816SGeorge.Wilson@Sun.COM 
26529816SGeorge.Wilson@Sun.COM /*
26539816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
26549816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
26559816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
26569816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
26579816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
26589816SGeorge.Wilson@Sun.COM  */
26599816SGeorge.Wilson@Sun.COM vdev_t *
26609816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
26619816SGeorge.Wilson@Sun.COM {
26629816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26639816SGeorge.Wilson@Sun.COM 		if (func == NULL)
26649816SGeorge.Wilson@Sun.COM 			return (vd);
26659816SGeorge.Wilson@Sun.COM 		else
26669816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
26679816SGeorge.Wilson@Sun.COM 	}
26689816SGeorge.Wilson@Sun.COM 
26699816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
26709816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
26719816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
26729816SGeorge.Wilson@Sun.COM 			return (cvd);
26739816SGeorge.Wilson@Sun.COM 	}
26749816SGeorge.Wilson@Sun.COM 	return (NULL);
26759816SGeorge.Wilson@Sun.COM }
26769816SGeorge.Wilson@Sun.COM 
26779816SGeorge.Wilson@Sun.COM /*
2678789Sahrens  * Verify that dynamic LUN growth works as expected.
2679789Sahrens  */
268010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2681789Sahrens void
268210922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2683789Sahrens {
268410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
268510922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
268610922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
268710922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
268810922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
26899816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
269010922SJeff.Bonwick@Sun.COM 	uint64_t top;
269110922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
269210922SJeff.Bonwick@Sun.COM 
269310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
26949816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26959816SGeorge.Wilson@Sun.COM 
269610922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
269710922SJeff.Bonwick@Sun.COM 
269810922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
269910922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
270010922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
270110922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
270210922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
27039816SGeorge.Wilson@Sun.COM 
27049816SGeorge.Wilson@Sun.COM 	/*
27059816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
27069816SGeorge.Wilson@Sun.COM 	 * our top-level device.
27079816SGeorge.Wilson@Sun.COM 	 */
27089816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
27099816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
27109816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
27119816SGeorge.Wilson@Sun.COM 
27129816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
27139816SGeorge.Wilson@Sun.COM 
27149816SGeorge.Wilson@Sun.COM 	/*
271510685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
271610685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
27179816SGeorge.Wilson@Sun.COM 	 */
271810685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
271910685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
27209816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
272110922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27229816SGeorge.Wilson@Sun.COM 		return;
27239816SGeorge.Wilson@Sun.COM 	}
27249816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
27259816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
27269816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
27279816SGeorge.Wilson@Sun.COM 
272810922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
272910922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
27309816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
27319816SGeorge.Wilson@Sun.COM 	}
27329816SGeorge.Wilson@Sun.COM 
2733789Sahrens 	/*
27349816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
27359816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
27369816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
27379816SGeorge.Wilson@Sun.COM 	 */
27389816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
27399816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
27409816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
27419816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
27429816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
274310685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
27449816SGeorge.Wilson@Sun.COM 		}
274510922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
274610922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27479816SGeorge.Wilson@Sun.COM 		return;
27489816SGeorge.Wilson@Sun.COM 	}
27499816SGeorge.Wilson@Sun.COM 
275010922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
27519816SGeorge.Wilson@Sun.COM 
27529816SGeorge.Wilson@Sun.COM 	/*
27539816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
27549816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
27559816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2756789Sahrens 	 */
275710922SJeff.Bonwick@Sun.COM 	for (;;) {
275810922SJeff.Bonwick@Sun.COM 		boolean_t done;
275910922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
276010922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
276110922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
276210922SJeff.Bonwick@Sun.COM 		if (done)
276310922SJeff.Bonwick@Sun.COM 			break;
276410922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
276510922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
276610922SJeff.Bonwick@Sun.COM 	}
27679816SGeorge.Wilson@Sun.COM 
27689816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
276910922SJeff.Bonwick@Sun.COM 
277010922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
277110922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
277210922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
277310922SJeff.Bonwick@Sun.COM 
277410922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
277510922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
277610922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
277710922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
277810922SJeff.Bonwick@Sun.COM 		}
277910922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
278010922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
278110922SJeff.Bonwick@Sun.COM 		return;
278210922SJeff.Bonwick@Sun.COM 	}
278310922SJeff.Bonwick@Sun.COM 
278410922SJeff.Bonwick@Sun.COM 	/*
278510922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
278610922SJeff.Bonwick@Sun.COM 	 */
278710922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
278810922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
278910922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
27909816SGeorge.Wilson@Sun.COM 
27919816SGeorge.Wilson@Sun.COM 	/*
27929816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
27939816SGeorge.Wilson@Sun.COM 	 */
279410922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
279510922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
279610922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
279710922SJeff.Bonwick@Sun.COM 
279810922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
27999816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
28009816SGeorge.Wilson@Sun.COM 
280110922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
280210922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
28039816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
28049816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2805789Sahrens 	}
280610922SJeff.Bonwick@Sun.COM 
28079816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
280810922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
280910922SJeff.Bonwick@Sun.COM }
281010922SJeff.Bonwick@Sun.COM 
281110922SJeff.Bonwick@Sun.COM /*
281210922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
281310922SJeff.Bonwick@Sun.COM  */
281410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
281510922SJeff.Bonwick@Sun.COM static void
281610922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
281710922SJeff.Bonwick@Sun.COM {
281810922SJeff.Bonwick@Sun.COM 	/*
281910922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
282010922SJeff.Bonwick@Sun.COM 	 */
282110922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
282210922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2823789Sahrens }
2824789Sahrens 
282512294SMark.Musante@Sun.COM static int
282612294SMark.Musante@Sun.COM ztest_dataset_create(char *dsname)
282712294SMark.Musante@Sun.COM {
282812294SMark.Musante@Sun.COM 	uint64_t zilset = ztest_random(100);
282912294SMark.Musante@Sun.COM 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
283012294SMark.Musante@Sun.COM 	    ztest_objset_create_cb, NULL);
283112294SMark.Musante@Sun.COM 
283212294SMark.Musante@Sun.COM 	if (err || zilset < 80)
283312294SMark.Musante@Sun.COM 		return (err);
283412294SMark.Musante@Sun.COM 
283512294SMark.Musante@Sun.COM 	(void) printf("Setting dataset %s to sync always\n", dsname);
283612294SMark.Musante@Sun.COM 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
283712294SMark.Musante@Sun.COM 	    ZFS_SYNC_ALWAYS, B_FALSE));
283812294SMark.Musante@Sun.COM }
283912294SMark.Musante@Sun.COM 
2840789Sahrens /* ARGSUSED */
284110922SJeff.Bonwick@Sun.COM static int
284211209SMatthew.Ahrens@Sun.COM ztest_objset_destroy_cb(const char *name, void *arg)
2843789Sahrens {
2844789Sahrens 	objset_t *os;
284510922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2846789Sahrens 	int error;
2847789Sahrens 
2848789Sahrens 	/*
2849789Sahrens 	 * Verify that the dataset contains a directory object.
2850789Sahrens 	 */
285110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
285210922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
28531731Sbonwick 	if (error != ENOENT) {
28541731Sbonwick 		/* We could have crashed in the middle of destroying it */
28551731Sbonwick 		ASSERT3U(error, ==, 0);
285610922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
285710922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
28581731Sbonwick 	}
285910298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2860789Sahrens 
2861789Sahrens 	/*
2862789Sahrens 	 * Destroy the dataset.
2863789Sahrens 	 */
286410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
28652199Sahrens 	return (0);
2866789Sahrens }
2867789Sahrens 
286810922SJeff.Bonwick@Sun.COM static boolean_t
286910922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2870789Sahrens {
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_snapshot(osname, strchr(snapname, '@') + 1,
287810922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
287910922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
288010922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
288110922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
288210922SJeff.Bonwick@Sun.COM 	}
288310922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
288410922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
288510922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2886789Sahrens }
2887789Sahrens 
288810922SJeff.Bonwick@Sun.COM static boolean_t
288910922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
289010922SJeff.Bonwick@Sun.COM {
289110922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
289210922SJeff.Bonwick@Sun.COM 	int error;
289310922SJeff.Bonwick@Sun.COM 
289410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
289510922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
289610922SJeff.Bonwick@Sun.COM 
289710922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
289810922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
289910922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
290010922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
290110922SJeff.Bonwick@Sun.COM }
290210922SJeff.Bonwick@Sun.COM 
290310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2904789Sahrens void
290510922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2906789Sahrens {
290710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
290810922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
290910922SJeff.Bonwick@Sun.COM 	int iters;
2910789Sahrens 	int error;
29116689Smaybee 	objset_t *os, *os2;
291210922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2913789Sahrens 	zilog_t *zilog;
291410922SJeff.Bonwick@Sun.COM 
291510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
291610922SJeff.Bonwick@Sun.COM 
291710922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
291810922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2919789Sahrens 
2920789Sahrens 	/*
2921789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2922789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
292310922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2924789Sahrens 	 */
2925789Sahrens 	if (ztest_random(2) == 0 &&
292610298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
292710922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
292810922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
292910922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
293010298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2931789Sahrens 	}
2932789Sahrens 
2933789Sahrens 	/*
2934789Sahrens 	 * There may be an old instance of the dataset we're about to
2935789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2936789Sahrens 	 * and all of its snapshots.
2937789Sahrens 	 */
293810922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
29392417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2940789Sahrens 
2941789Sahrens 	/*
2942789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2943789Sahrens 	 */
294410922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2945789Sahrens 
2946789Sahrens 	/*
2947789Sahrens 	 * Verify that we can create a new dataset.
2948789Sahrens 	 */
294912294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
2950789Sahrens 	if (error) {
2951789Sahrens 		if (error == ENOSPC) {
295210922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
295310922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2954789Sahrens 			return;
2955789Sahrens 		}
2956789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2957789Sahrens 	}
2958789Sahrens 
295910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
296010922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
296110922SJeff.Bonwick@Sun.COM 
296210922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2963789Sahrens 
2964789Sahrens 	/*
2965789Sahrens 	 * Open the intent log for it.
2966789Sahrens 	 */
296710922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2968789Sahrens 
2969789Sahrens 	/*
297010922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
297110922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2972789Sahrens 	 */
297310922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
297410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
297510922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
297610922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
297710922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2978789Sahrens 	}
2979789Sahrens 
2980789Sahrens 	/*
2981789Sahrens 	 * Verify that we cannot create an existing dataset.
2982789Sahrens 	 */
298310922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
298410922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2985789Sahrens 
2986789Sahrens 	/*
298710298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2988789Sahrens 	 */
298910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
299010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
299110298SMatthew.Ahrens@Sun.COM 
299210298SMatthew.Ahrens@Sun.COM 	/*
299310922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
299410298SMatthew.Ahrens@Sun.COM 	 */
299510922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
299610922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
2997789Sahrens 
2998789Sahrens 	zil_close(zilog);
299910298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
300010922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
300110922SJeff.Bonwick@Sun.COM 
300210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3003789Sahrens }
3004789Sahrens 
3005789Sahrens /*
3006789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3007789Sahrens  */
3008789Sahrens void
300910922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3010789Sahrens {
301110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
301210922SJeff.Bonwick@Sun.COM 
301310922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
301410922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
301510922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
301610922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3017789Sahrens }
3018789Sahrens 
3019789Sahrens /*
30209691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
30219691SGeorge.Wilson@Sun.COM  */
30229691SGeorge.Wilson@Sun.COM void
302310922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
30249691SGeorge.Wilson@Sun.COM {
302510922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
302610922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
302710922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
302810922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
302910922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
30309691SGeorge.Wilson@Sun.COM 	int error;
30319691SGeorge.Wilson@Sun.COM 
303210922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
303310922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
303410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
303510922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
303610922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30379691SGeorge.Wilson@Sun.COM 
303810242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
30399691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30409691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
304110242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
30429691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30439691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
304410242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
30459691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30469691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
304710242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
30489691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30499691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
305010242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
30519691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30529691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
30539691SGeorge.Wilson@Sun.COM }
30549691SGeorge.Wilson@Sun.COM 
30559691SGeorge.Wilson@Sun.COM /*
30568779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
30578779SMark.Musante@Sun.COM  */
30588779SMark.Musante@Sun.COM void
305910922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
30608779SMark.Musante@Sun.COM {
306110922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
30628779SMark.Musante@Sun.COM 	objset_t *clone;
30638779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
306410922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
306510922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
306610922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
306710922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
306810922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
306910922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
307010922SJeff.Bonwick@Sun.COM 	int error;
307110922SJeff.Bonwick@Sun.COM 
307210922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
307310922SJeff.Bonwick@Sun.COM 
307410922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
307510922SJeff.Bonwick@Sun.COM 
307610922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
307710922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
307810922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
307910922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
308010922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30818837SMark.Musante@Sun.COM 
30829355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
308310922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
30849229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30859229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
308610922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30879229SGeorge.Wilson@Sun.COM 			goto out;
30889229SGeorge.Wilson@Sun.COM 		}
30899229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
30909229SGeorge.Wilson@Sun.COM 	}
30918779SMark.Musante@Sun.COM 
309210298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
30938779SMark.Musante@Sun.COM 	if (error)
30948779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
30958779SMark.Musante@Sun.COM 
309610272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
309710298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
30989229SGeorge.Wilson@Sun.COM 	if (error) {
30999229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
310010922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31019229SGeorge.Wilson@Sun.COM 			goto out;
31029229SGeorge.Wilson@Sun.COM 		}
31038779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
31049229SGeorge.Wilson@Sun.COM 	}
31058779SMark.Musante@Sun.COM 
31068779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
310710922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
31089229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31099229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
311010922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31119229SGeorge.Wilson@Sun.COM 			goto out;
31129229SGeorge.Wilson@Sun.COM 		}
31139229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
31149229SGeorge.Wilson@Sun.COM 	}
31158779SMark.Musante@Sun.COM 
31168779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
311710922SJeff.Bonwick@Sun.COM 	    NULL, B_FALSE);
31189229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31199229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
312010922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31219229SGeorge.Wilson@Sun.COM 			goto out;
31229229SGeorge.Wilson@Sun.COM 		}
31239229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31249229SGeorge.Wilson@Sun.COM 	}
31258779SMark.Musante@Sun.COM 
312610298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
31278779SMark.Musante@Sun.COM 	if (error)
31288779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31298779SMark.Musante@Sun.COM 
313010272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
313110298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31329229SGeorge.Wilson@Sun.COM 	if (error) {
31339229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
313410922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31359229SGeorge.Wilson@Sun.COM 			goto out;
31369229SGeorge.Wilson@Sun.COM 		}
31378779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
31389229SGeorge.Wilson@Sun.COM 	}
31398779SMark.Musante@Sun.COM 
314012470SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
31418779SMark.Musante@Sun.COM 	if (error)
314212470SMatthew.Ahrens@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
314310588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
31448779SMark.Musante@Sun.COM 	if (error != EBUSY)
31458779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
31468779SMark.Musante@Sun.COM 		    error);
31478779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
31488779SMark.Musante@Sun.COM 
31499229SGeorge.Wilson@Sun.COM out:
315010922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
315110922SJeff.Bonwick@Sun.COM 
315210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
31538779SMark.Musante@Sun.COM }
31548779SMark.Musante@Sun.COM 
31558779SMark.Musante@Sun.COM /*
3156789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3157789Sahrens  */
3158789Sahrens void
315910922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3160789Sahrens {
316110922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
316210922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
316310922SJeff.Bonwick@Sun.COM 
316410922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
316510922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3166789Sahrens 
3167789Sahrens 	/*
316810922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
316910922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3170789Sahrens 	 */
317110922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
317210922SJeff.Bonwick@Sun.COM 		return;
317310922SJeff.Bonwick@Sun.COM 
317410922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
317510922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
317610922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3177789Sahrens }
3178789Sahrens 
3179789Sahrens /*
3180789Sahrens  * Verify that dmu_{read,write} work as expected.
3181789Sahrens  */
3182789Sahrens void
318310922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3184789Sahrens {
318510922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
318610922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3187789Sahrens 	dmu_tx_t *tx;
3188789Sahrens 	int i, freeit, error;
3189789Sahrens 	uint64_t n, s, txg;
3190789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
319110922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
319210922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3193789Sahrens 	uint64_t regions = 997;
3194789Sahrens 	uint64_t stride = 123456789ULL;
3195789Sahrens 	uint64_t width = 40;
3196789Sahrens 	int free_percent = 5;
3197789Sahrens 
3198789Sahrens 	/*
3199789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3200789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3201789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3202789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3203789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3204789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3205789Sahrens 	 *
3206789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3207789Sahrens 	 *	bigobj, at the head of the nth chunk
3208789Sahrens 	 *	bigobj, at the tail of the nth chunk
3209789Sahrens 	 *
3210789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3211789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3212789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3213789Sahrens 	 *
3214789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3215789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3216789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3217789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3218789Sahrens 	 * tracking which parts of an object are allocated and free,
3219789Sahrens 	 * and that the contents of the allocated blocks are correct.
3220789Sahrens 	 */
3221789Sahrens 
3222789Sahrens 	/*
3223789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3224789Sahrens 	 */
322510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
322610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
322710922SJeff.Bonwick@Sun.COM 
322810922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
322910922SJeff.Bonwick@Sun.COM 		return;
323010922SJeff.Bonwick@Sun.COM 
323110922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
323210922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
323310922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
323410922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3235789Sahrens 
3236789Sahrens 	/*
3237789Sahrens 	 * Prefetch a random chunk of the big object.
3238789Sahrens 	 * Our aim here is to get some async reads in flight
3239789Sahrens 	 * for blocks that we may free below; the DMU should
3240789Sahrens 	 * handle this race correctly.
3241789Sahrens 	 */
3242789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3243789Sahrens 	s = 1 + ztest_random(2 * width - 1);
324410922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3245789Sahrens 
3246789Sahrens 	/*
3247789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3248789Sahrens 	 */
3249789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3250789Sahrens 	s = 1 + ztest_random(width - 1);
3251789Sahrens 
3252789Sahrens 	packoff = n * sizeof (bufwad_t);
3253789Sahrens 	packsize = s * sizeof (bufwad_t);
3254789Sahrens 
325510922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
325610922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3257789Sahrens 
3258789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3259789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3260789Sahrens 
3261789Sahrens 	/*
3262789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3263789Sahrens 	 * overwriting it.
3264789Sahrens 	 */
3265789Sahrens 	freeit = (ztest_random(100) < free_percent);
3266789Sahrens 
3267789Sahrens 	/*
3268789Sahrens 	 * Read the current contents of our objects.
3269789Sahrens 	 */
327010922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
32719512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32721544Seschrock 	ASSERT3U(error, ==, 0);
327310922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
32749512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32751544Seschrock 	ASSERT3U(error, ==, 0);
3276789Sahrens 
3277789Sahrens 	/*
3278789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3279789Sahrens 	 */
3280789Sahrens 	tx = dmu_tx_create(os);
3281789Sahrens 
328210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3283789Sahrens 
3284789Sahrens 	if (freeit)
328510922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3286789Sahrens 	else
328710922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
328810922SJeff.Bonwick@Sun.COM 
328910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
329010922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3291789Sahrens 		umem_free(packbuf, packsize);
3292789Sahrens 		umem_free(bigbuf, bigsize);
3293789Sahrens 		return;
3294789Sahrens 	}
3295789Sahrens 
329610922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
329710922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
329810922SJeff.Bonwick@Sun.COM 
329910922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
330010922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3301789Sahrens 
3302789Sahrens 	/*
3303789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3304789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3305789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3306789Sahrens 	 * with the new values we want to write out.
3307789Sahrens 	 */
3308789Sahrens 	for (i = 0; i < s; i++) {
3309789Sahrens 		/* LINTED */
3310789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3311789Sahrens 		/* LINTED */
331210922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3313789Sahrens 		/* LINTED */
331410922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3315789Sahrens 
3316789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3317789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3318789Sahrens 
3319789Sahrens 		if (pack->bw_txg > txg)
3320789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3321789Sahrens 			    pack->bw_txg, txg);
3322789Sahrens 
3323789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3324789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3325789Sahrens 			    pack->bw_index, n, i);
3326789Sahrens 
3327789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3328789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3329789Sahrens 
3330789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3331789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3332789Sahrens 
3333789Sahrens 		if (freeit) {
3334789Sahrens 			bzero(pack, sizeof (bufwad_t));
3335789Sahrens 		} else {
3336789Sahrens 			pack->bw_index = n + i;
3337789Sahrens 			pack->bw_txg = txg;
3338789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3339789Sahrens 		}
3340789Sahrens 		*bigH = *pack;
3341789Sahrens 		*bigT = *pack;
3342789Sahrens 	}
3343789Sahrens 
3344789Sahrens 	/*
3345789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3346789Sahrens 	 * Now write them out.
3347789Sahrens 	 */
334810922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3349789Sahrens 
3350789Sahrens 	if (freeit) {
335110922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3352789Sahrens 			(void) printf("freeing offset %llx size %llx"
3353789Sahrens 			    " txg %llx\n",
3354789Sahrens 			    (u_longlong_t)bigoff,
3355789Sahrens 			    (u_longlong_t)bigsize,
3356789Sahrens 			    (u_longlong_t)txg);
3357789Sahrens 		}
335810922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3359789Sahrens 	} else {
336010922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3361789Sahrens 			(void) printf("writing offset %llx size %llx"
3362789Sahrens 			    " txg %llx\n",
3363789Sahrens 			    (u_longlong_t)bigoff,
3364789Sahrens 			    (u_longlong_t)bigsize,
3365789Sahrens 			    (u_longlong_t)txg);
3366789Sahrens 		}
336710922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3368789Sahrens 	}
3369789Sahrens 
3370789Sahrens 	dmu_tx_commit(tx);
3371789Sahrens 
3372789Sahrens 	/*
3373789Sahrens 	 * Sanity check the stuff we just wrote.
3374789Sahrens 	 */
3375789Sahrens 	{
3376789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3377789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3378789Sahrens 
337910922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
33809512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
338110922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
33829512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3383789Sahrens 
3384789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3385789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3386789Sahrens 
3387789Sahrens 		umem_free(packcheck, packsize);
3388789Sahrens 		umem_free(bigcheck, bigsize);
3389789Sahrens 	}
3390789Sahrens 
3391789Sahrens 	umem_free(packbuf, packsize);
3392789Sahrens 	umem_free(bigbuf, bigsize);
3393789Sahrens }
3394789Sahrens 
3395789Sahrens void
33969412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
339710922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
33989412SAleksandr.Guzovskiy@Sun.COM {
33999412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34009412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
34019412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
34029412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
34039412SAleksandr.Guzovskiy@Sun.COM 
34049412SAleksandr.Guzovskiy@Sun.COM 	/*
34059412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
34069412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
34079412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
34089412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
34099412SAleksandr.Guzovskiy@Sun.COM 	 */
34109412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
34119412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
34129412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
34139412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341410922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34159412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341610922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34179412SAleksandr.Guzovskiy@Sun.COM 
34189412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
34199412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
34209412SAleksandr.Guzovskiy@Sun.COM 
34219412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
34229412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
34239412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
34249412SAleksandr.Guzovskiy@Sun.COM 
34259412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
34269412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
34279412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
34289412SAleksandr.Guzovskiy@Sun.COM 
34299412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
34309412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
34319412SAleksandr.Guzovskiy@Sun.COM 
34329412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
34339412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
34349412SAleksandr.Guzovskiy@Sun.COM 
34359412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
34369412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
34379412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
34389412SAleksandr.Guzovskiy@Sun.COM 
34399412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
34409412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
34419412SAleksandr.Guzovskiy@Sun.COM 	}
34429412SAleksandr.Guzovskiy@Sun.COM }
34439412SAleksandr.Guzovskiy@Sun.COM 
34449412SAleksandr.Guzovskiy@Sun.COM void
344510922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
34469412SAleksandr.Guzovskiy@Sun.COM {
344710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
344810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
34499412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
34509412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34519412SAleksandr.Guzovskiy@Sun.COM 	int error;
34529412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
34539412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
345410922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
345510922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
345610922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
34579412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
34589412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
34599412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
34609412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
34619412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
346210922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
34639412SAleksandr.Guzovskiy@Sun.COM 
34649412SAleksandr.Guzovskiy@Sun.COM 	/*
34659412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
34669412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
34679412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
34689412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
34699412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
34709412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
34719412SAleksandr.Guzovskiy@Sun.COM 	 *
34729412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
34739412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
34749412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
34759412SAleksandr.Guzovskiy@Sun.COM 	 *
34769412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
34779412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
34789412SAleksandr.Guzovskiy@Sun.COM 	 */
34799412SAleksandr.Guzovskiy@Sun.COM 
34809412SAleksandr.Guzovskiy@Sun.COM 	/*
34819412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
34829412SAleksandr.Guzovskiy@Sun.COM 	 */
348310922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
348410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
348510922SJeff.Bonwick@Sun.COM 
348610922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
348710922SJeff.Bonwick@Sun.COM 		return;
348810922SJeff.Bonwick@Sun.COM 
348910922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
349010922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
349110922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
349210922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
349310922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
349410922SJeff.Bonwick@Sun.COM 
349510922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
349610922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
349710922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
349810922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
34999412SAleksandr.Guzovskiy@Sun.COM 
35009412SAleksandr.Guzovskiy@Sun.COM 	/*
35019412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
35029412SAleksandr.Guzovskiy@Sun.COM 	 */
35039412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
35049412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
35059412SAleksandr.Guzovskiy@Sun.COM 
35069412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
35079412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
35089412SAleksandr.Guzovskiy@Sun.COM 
350910922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
351010922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
35119412SAleksandr.Guzovskiy@Sun.COM 
35129412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
35139412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
35149412SAleksandr.Guzovskiy@Sun.COM 
351510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
35169412SAleksandr.Guzovskiy@Sun.COM 
35179412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
35189412SAleksandr.Guzovskiy@Sun.COM 
35199412SAleksandr.Guzovskiy@Sun.COM 	/*
35209412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
35219412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
35229412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
35239412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
35249412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
35259412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
35269412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
35279412SAleksandr.Guzovskiy@Sun.COM 	 */
35289412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
35299412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
35309412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
35319412SAleksandr.Guzovskiy@Sun.COM 
35329412SAleksandr.Guzovskiy@Sun.COM 		/*
35339412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
35349412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
35359412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
35369412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
35379412SAleksandr.Guzovskiy@Sun.COM 		 */
35389412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
35399412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35409412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
354110922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
35429412SAleksandr.Guzovskiy@Sun.COM 			} else {
35439412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
354410922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35459412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
354610922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35479412SAleksandr.Guzovskiy@Sun.COM 			}
35489412SAleksandr.Guzovskiy@Sun.COM 		}
35499412SAleksandr.Guzovskiy@Sun.COM 
35509412SAleksandr.Guzovskiy@Sun.COM 		/*
35519412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
35529412SAleksandr.Guzovskiy@Sun.COM 		 */
35539412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
35549412SAleksandr.Guzovskiy@Sun.COM 
355510922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
355610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
355710922SJeff.Bonwick@Sun.COM 
355810922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
355910922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
35609412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
35619412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
35629412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
35639412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
35649412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
35659412SAleksandr.Guzovskiy@Sun.COM 				} else {
35669412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35679412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
35689412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35699412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
35709412SAleksandr.Guzovskiy@Sun.COM 				}
35719412SAleksandr.Guzovskiy@Sun.COM 			}
35729412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35739412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
35749412SAleksandr.Guzovskiy@Sun.COM 			return;
35759412SAleksandr.Guzovskiy@Sun.COM 		}
35769412SAleksandr.Guzovskiy@Sun.COM 
35779412SAleksandr.Guzovskiy@Sun.COM 		/*
35789412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
35799412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
35809412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
35819412SAleksandr.Guzovskiy@Sun.COM 		 */
35829412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
358310922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
35849512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
35859412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
358610922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
35879512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
35889412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
35899412SAleksandr.Guzovskiy@Sun.COM 		}
35909412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
359110922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
35929412SAleksandr.Guzovskiy@Sun.COM 
35939412SAleksandr.Guzovskiy@Sun.COM 		/*
35949412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
35959412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
35969412SAleksandr.Guzovskiy@Sun.COM 		 */
359710922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
359810922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
35999412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
36009412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
36019412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
36029412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
36039412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
36049412SAleksandr.Guzovskiy@Sun.COM 		}
360510922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
36069412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
36079412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36089412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
360910922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
36109412SAleksandr.Guzovskiy@Sun.COM 			} else {
36119412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
36129412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
361310922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36149412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
361510922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
36169412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
361710922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36189412SAleksandr.Guzovskiy@Sun.COM 			}
36199412SAleksandr.Guzovskiy@Sun.COM 
36209412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
362110922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
362212285SJeff.Bonwick@Sun.COM 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
36239412SAleksandr.Guzovskiy@Sun.COM 			}
36249412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36259412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36269412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
36279412SAleksandr.Guzovskiy@Sun.COM 			} else {
36289412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36299412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
36309412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
363110922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
36329412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
36339412SAleksandr.Guzovskiy@Sun.COM 			}
36349412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
36359412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
36369412SAleksandr.Guzovskiy@Sun.COM 			}
36379412SAleksandr.Guzovskiy@Sun.COM 		}
36389412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
36399412SAleksandr.Guzovskiy@Sun.COM 
36409412SAleksandr.Guzovskiy@Sun.COM 		/*
36419412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
36429412SAleksandr.Guzovskiy@Sun.COM 		 */
36439412SAleksandr.Guzovskiy@Sun.COM 		{
36449412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
36459412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
36469412SAleksandr.Guzovskiy@Sun.COM 
364710922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
36489512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
364910922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
36509512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
36519412SAleksandr.Guzovskiy@Sun.COM 
36529412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
36539412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
36549412SAleksandr.Guzovskiy@Sun.COM 
36559412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
36569412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
36579412SAleksandr.Guzovskiy@Sun.COM 		}
36589412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
36599412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
36609412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
36619412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
36629412SAleksandr.Guzovskiy@Sun.COM 		}
36639412SAleksandr.Guzovskiy@Sun.COM 	}
36649412SAleksandr.Guzovskiy@Sun.COM 
36659412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
36669412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
36679412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
36689412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
36699412SAleksandr.Guzovskiy@Sun.COM }
36709412SAleksandr.Guzovskiy@Sun.COM 
367110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
36729412SAleksandr.Guzovskiy@Sun.COM void
367310922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
36743711Smaybee {
367510922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
367610922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
367710922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
36783711Smaybee 
36793711Smaybee 	/*
368010922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
368110922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
368210922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
36833711Smaybee 	 */
368410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
368510922SJeff.Bonwick@Sun.COM 
368610922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
368710922SJeff.Bonwick@Sun.COM 		return;
368810922SJeff.Bonwick@Sun.COM 
368910922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
369010922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
36913711Smaybee }
36923711Smaybee 
36933711Smaybee void
369410922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3695789Sahrens {
369610922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
369710922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
369810922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
369910922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
370010922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
370110922SJeff.Bonwick@Sun.COM 	void *data;
370210922SJeff.Bonwick@Sun.COM 
370310922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
370410922SJeff.Bonwick@Sun.COM 
370510922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
37065530Sbonwick 		return;
370710922SJeff.Bonwick@Sun.COM 
370810922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
370910922SJeff.Bonwick@Sun.COM 		return;
371010922SJeff.Bonwick@Sun.COM 
371110922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
371210922SJeff.Bonwick@Sun.COM 
371310922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
371410922SJeff.Bonwick@Sun.COM 
371510922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
371610922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
371710922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
371810922SJeff.Bonwick@Sun.COM 		    data) != 0)
371910922SJeff.Bonwick@Sun.COM 			break;
372010922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
372110922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
37225530Sbonwick 	}
372310922SJeff.Bonwick@Sun.COM 
372410922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3725789Sahrens }
3726789Sahrens 
3727789Sahrens /*
3728789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3729789Sahrens  */
3730789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3731789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3732789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3733789Sahrens 
3734789Sahrens void
373510922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3736789Sahrens {
373710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
373810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3739789Sahrens 	uint64_t object;
3740789Sahrens 	uint64_t txg, last_txg;
3741789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3742789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3743789Sahrens 	int i, ints;
3744789Sahrens 	dmu_tx_t *tx;
3745789Sahrens 	char propname[100], txgname[100];
3746789Sahrens 	int error;
3747789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3748789Sahrens 
374910922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
375010922SJeff.Bonwick@Sun.COM 
375110922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
375210922SJeff.Bonwick@Sun.COM 		return;
375310922SJeff.Bonwick@Sun.COM 
375410922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3755789Sahrens 
3756789Sahrens 	/*
375710922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
375810922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3759789Sahrens 	 */
376010922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
376110922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
376210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
376310922SJeff.Bonwick@Sun.COM 	if (txg == 0)
376410922SJeff.Bonwick@Sun.COM 		return;
376510922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
376610922SJeff.Bonwick@Sun.COM 		value[i] = i;
376710922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
376810922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3769789Sahrens 	}
377010922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
377110922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
377210922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
377310922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
377410922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
377510922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
377610922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
377710922SJeff.Bonwick@Sun.COM 	}
377810922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
377910922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
378010922SJeff.Bonwick@Sun.COM 	}
378110922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
378210922SJeff.Bonwick@Sun.COM 
378310922SJeff.Bonwick@Sun.COM 	/*
378410922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
378510922SJeff.Bonwick@Sun.COM 	 */
3786789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3787789Sahrens 
37885530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37895530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37905530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37915530Sbonwick 	bzero(value, sizeof (value));
37925530Sbonwick 	last_txg = 0;
37935530Sbonwick 
37945530Sbonwick 	/*
37955530Sbonwick 	 * If these zap entries already exist, validate their contents.
37965530Sbonwick 	 */
37975530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
37985530Sbonwick 	if (error == 0) {
37995530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
38005530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
38015530Sbonwick 
38025530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
38035530Sbonwick 		    zl_ints, &last_txg) == 0);
38045530Sbonwick 
38055530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
38065530Sbonwick 		    &zl_ints) == 0);
38075530Sbonwick 
38085530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
38095530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
38105530Sbonwick 
38115530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
38125530Sbonwick 		    zl_ints, value) == 0);
38135530Sbonwick 
38145530Sbonwick 		for (i = 0; i < ints; i++) {
38155530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3816789Sahrens 		}
38175530Sbonwick 	} else {
38185530Sbonwick 		ASSERT3U(error, ==, ENOENT);
38195530Sbonwick 	}
38205530Sbonwick 
38215530Sbonwick 	/*
38225530Sbonwick 	 * Atomically update two entries in our zap object.
38235530Sbonwick 	 * The first is named txg_%llu, and contains the txg
38245530Sbonwick 	 * in which the property was last updated.  The second
38255530Sbonwick 	 * is named prop_%llu, and the nth element of its value
38265530Sbonwick 	 * should be txg + object + n.
38275530Sbonwick 	 */
38285530Sbonwick 	tx = dmu_tx_create(os);
382910922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
383010922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
383110922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38325530Sbonwick 		return;
38335530Sbonwick 
38345530Sbonwick 	if (last_txg > txg)
38355530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
38365530Sbonwick 
38375530Sbonwick 	for (i = 0; i < ints; i++)
38385530Sbonwick 		value[i] = txg + object + i;
38395530Sbonwick 
384010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
384110922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
384210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
384310922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
38445530Sbonwick 
38455530Sbonwick 	dmu_tx_commit(tx);
38465530Sbonwick 
38475530Sbonwick 	/*
38485530Sbonwick 	 * Remove a random pair of entries.
38495530Sbonwick 	 */
38505530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
38515530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
38525530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
38535530Sbonwick 
38545530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38555530Sbonwick 
38565530Sbonwick 	if (error == ENOENT)
38575530Sbonwick 		return;
38585530Sbonwick 
38595530Sbonwick 	ASSERT3U(error, ==, 0);
38605530Sbonwick 
38615530Sbonwick 	tx = dmu_tx_create(os);
386210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
386310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
386410922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38655530Sbonwick 		return;
386610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
386710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3868789Sahrens 	dmu_tx_commit(tx);
3869789Sahrens }
3870789Sahrens 
387110431SSanjeev.Bagewadi@Sun.COM /*
387210431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
387310431SSanjeev.Bagewadi@Sun.COM  */
387410431SSanjeev.Bagewadi@Sun.COM void
387510922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
387610431SSanjeev.Bagewadi@Sun.COM {
387710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
387810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
387910922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
388010922SJeff.Bonwick@Sun.COM 
388110922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
388210922SJeff.Bonwick@Sun.COM 
388310922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
388410922SJeff.Bonwick@Sun.COM 		return;
388510922SJeff.Bonwick@Sun.COM 
388610922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
388710431SSanjeev.Bagewadi@Sun.COM 
388810431SSanjeev.Bagewadi@Sun.COM 	/*
388910922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
389010922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
389110922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
389210431SSanjeev.Bagewadi@Sun.COM 	 */
389310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
389410922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
389510922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
389610922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
389710922SJeff.Bonwick@Sun.COM 		int error;
389810922SJeff.Bonwick@Sun.COM 
389910922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
390010922SJeff.Bonwick@Sun.COM 		    id, value);
390110431SSanjeev.Bagewadi@Sun.COM 
390210431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
390310922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
390410922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
390510922SJeff.Bonwick@Sun.COM 		if (txg == 0)
390610431SSanjeev.Bagewadi@Sun.COM 			return;
390710922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
390810922SJeff.Bonwick@Sun.COM 		    &value, tx);
390910431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
391010431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
391110431SSanjeev.Bagewadi@Sun.COM 	}
391210431SSanjeev.Bagewadi@Sun.COM }
391310431SSanjeev.Bagewadi@Sun.COM 
391410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3915789Sahrens void
391610922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3917789Sahrens {
391810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
391910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3920789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3921789Sahrens 	dmu_tx_t *tx;
3922789Sahrens 	int i, namelen, error;
392310922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3924789Sahrens 	char name[20], string_value[20];
3925789Sahrens 	void *data;
3926789Sahrens 
392710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
392810922SJeff.Bonwick@Sun.COM 
392910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
393010922SJeff.Bonwick@Sun.COM 		return;
393110922SJeff.Bonwick@Sun.COM 
393210922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
393310922SJeff.Bonwick@Sun.COM 
39345530Sbonwick 	/*
39355530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
39365530Sbonwick 	 * x is a random printable character and the dots are dots.
39375530Sbonwick 	 * There are 94 such characters, and the name length goes from
39385530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
39395530Sbonwick 	 */
39405530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
39415530Sbonwick 
39425530Sbonwick 	for (i = 0; i < 3; i++)
39435530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
39445530Sbonwick 	for (; i < namelen - 1; i++)
39455530Sbonwick 		name[i] = '.';
39465530Sbonwick 	name[i] = '\0';
39475530Sbonwick 
394810922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
39495530Sbonwick 		wsize = sizeof (txg);
39505530Sbonwick 		wc = 1;
39515530Sbonwick 		data = &txg;
39525530Sbonwick 	} else {
39535530Sbonwick 		wsize = 1;
39545530Sbonwick 		wc = namelen;
39555530Sbonwick 		data = string_value;
39565530Sbonwick 	}
39575530Sbonwick 
39585530Sbonwick 	count = -1ULL;
39595530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
39605530Sbonwick 	ASSERT(count != -1ULL);
39615530Sbonwick 
39625530Sbonwick 	/*
39635530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
39645530Sbonwick 	 */
39655530Sbonwick 	i = ztest_random(5);
39665530Sbonwick 
39675530Sbonwick 	if (i >= 2) {
39685530Sbonwick 		tx = dmu_tx_create(os);
396910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
397010922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
397110922SJeff.Bonwick@Sun.COM 		if (txg == 0)
39725530Sbonwick 			return;
39735530Sbonwick 		bcopy(name, string_value, namelen);
39745530Sbonwick 	} else {
39755530Sbonwick 		tx = NULL;
39765530Sbonwick 		txg = 0;
39775530Sbonwick 		bzero(string_value, namelen);
39785530Sbonwick 	}
39795530Sbonwick 
39805530Sbonwick 	switch (i) {
39815530Sbonwick 
39825530Sbonwick 	case 0:
39835530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
39845530Sbonwick 		if (error == 0) {
39855530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
39865530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3987789Sahrens 		} else {
39885530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3989789Sahrens 		}
39905530Sbonwick 		break;
39915530Sbonwick 
39925530Sbonwick 	case 1:
39935530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
39945530Sbonwick 		if (error == 0) {
39955530Sbonwick 			if (data == string_value &&
39965530Sbonwick 			    bcmp(name, data, namelen) != 0)
39975530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
39985530Sbonwick 				    name, data, namelen);
39995530Sbonwick 		} else {
40005530Sbonwick 			ASSERT3U(error, ==, ENOENT);
4001789Sahrens 		}
40025530Sbonwick 		break;
40035530Sbonwick 
40045530Sbonwick 	case 2:
40055530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
40065530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
40075530Sbonwick 		break;
40085530Sbonwick 
40095530Sbonwick 	case 3:
40105530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
40115530Sbonwick 		break;
40125530Sbonwick 
40135530Sbonwick 	case 4:
40145530Sbonwick 		error = zap_remove(os, object, name, tx);
40155530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
40165530Sbonwick 		break;
4017789Sahrens 	}
40185530Sbonwick 
40195530Sbonwick 	if (tx != NULL)
40205530Sbonwick 		dmu_tx_commit(tx);
4021789Sahrens }
4022789Sahrens 
402310612SRicardo.M.Correia@Sun.COM /*
402410612SRicardo.M.Correia@Sun.COM  * Commit callback data.
402510612SRicardo.M.Correia@Sun.COM  */
402610612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
402710612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
402810612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
402910612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
403010612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
403110612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
403210612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
403310612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
403410612SRicardo.M.Correia@Sun.COM 
403510612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
403610612SRicardo.M.Correia@Sun.COM static void
403710612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
403810612SRicardo.M.Correia@Sun.COM {
403910612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
404010612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
404110612SRicardo.M.Correia@Sun.COM 
404210612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
404310612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
404410612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
404510612SRicardo.M.Correia@Sun.COM 
404610612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
404710612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
404810612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
404910612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
405010612SRicardo.M.Correia@Sun.COM 		    synced_txg);
405110612SRicardo.M.Correia@Sun.COM 
405210612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
405310612SRicardo.M.Correia@Sun.COM 
405410612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
405510612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
405610612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
405710612SRicardo.M.Correia@Sun.COM 
405810612SRicardo.M.Correia@Sun.COM 		/*
405910612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
406010612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
406110612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
406210612SRicardo.M.Correia@Sun.COM 		 */
406310612SRicardo.M.Correia@Sun.COM 		return;
406410612SRicardo.M.Correia@Sun.COM 	}
406510612SRicardo.M.Correia@Sun.COM 
406610612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
406710612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
406810612SRicardo.M.Correia@Sun.COM 		goto out;
406910612SRicardo.M.Correia@Sun.COM 
407010612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
407110612SRicardo.M.Correia@Sun.COM 
407210612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
407310612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
407410612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
407510612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
407610612SRicardo.M.Correia@Sun.COM 
407710612SRicardo.M.Correia@Sun.COM out:
407810612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
407910612SRicardo.M.Correia@Sun.COM }
408010612SRicardo.M.Correia@Sun.COM 
408110612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
408210612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
408310612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
408410612SRicardo.M.Correia@Sun.COM {
408510612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
408610612SRicardo.M.Correia@Sun.COM 
408710612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
408810612SRicardo.M.Correia@Sun.COM 
408910612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
409010612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
409110612SRicardo.M.Correia@Sun.COM 
409210612SRicardo.M.Correia@Sun.COM 	return (cb_data);
409310612SRicardo.M.Correia@Sun.COM }
409410612SRicardo.M.Correia@Sun.COM 
409510612SRicardo.M.Correia@Sun.COM /*
409610612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
409710612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
409810612SRicardo.M.Correia@Sun.COM  * implementation bug.
409910612SRicardo.M.Correia@Sun.COM  */
410010612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
410110612SRicardo.M.Correia@Sun.COM 
410210612SRicardo.M.Correia@Sun.COM /*
410310612SRicardo.M.Correia@Sun.COM  * Commit callback test.
410410612SRicardo.M.Correia@Sun.COM  */
410510612SRicardo.M.Correia@Sun.COM void
410610922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
410710612SRicardo.M.Correia@Sun.COM {
410810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
410910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
411010612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
411110612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
411210612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
411310612SRicardo.M.Correia@Sun.COM 	int i, error;
411410612SRicardo.M.Correia@Sun.COM 
411510922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
411610922SJeff.Bonwick@Sun.COM 
411710922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
411810922SJeff.Bonwick@Sun.COM 		return;
411910922SJeff.Bonwick@Sun.COM 
412010612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
412110612SRicardo.M.Correia@Sun.COM 
412210612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
412310612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
412410612SRicardo.M.Correia@Sun.COM 
412510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
412610612SRicardo.M.Correia@Sun.COM 
412710612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
412810612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
412910612SRicardo.M.Correia@Sun.COM 		error = -1;
413010612SRicardo.M.Correia@Sun.COM 
413110612SRicardo.M.Correia@Sun.COM 	if (!error)
413210612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
413310612SRicardo.M.Correia@Sun.COM 
413410612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
413510612SRicardo.M.Correia@Sun.COM 
413610612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
413710612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
413810612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
413910612SRicardo.M.Correia@Sun.COM 
414010612SRicardo.M.Correia@Sun.COM 	if (error) {
414110612SRicardo.M.Correia@Sun.COM 		/*
414210612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
414310612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
414410612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
414510612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
414610612SRicardo.M.Correia@Sun.COM 		 */
414710612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
414810612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
414910612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
415010612SRicardo.M.Correia@Sun.COM 		}
415110612SRicardo.M.Correia@Sun.COM 
415210612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
415310612SRicardo.M.Correia@Sun.COM 
415410612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
415510612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
415610612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
415710612SRicardo.M.Correia@Sun.COM 		}
415810612SRicardo.M.Correia@Sun.COM 
415910612SRicardo.M.Correia@Sun.COM 		return;
416010612SRicardo.M.Correia@Sun.COM 	}
416110612SRicardo.M.Correia@Sun.COM 
416210612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
416310612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
416410612SRicardo.M.Correia@Sun.COM 
416510612SRicardo.M.Correia@Sun.COM 	/*
416610612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
416710612SRicardo.M.Correia@Sun.COM 	 */
416810922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
416910612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
417010612SRicardo.M.Correia@Sun.COM 
417110612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
417210612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
417310612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
417410612SRicardo.M.Correia@Sun.COM 
417510922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
417610612SRicardo.M.Correia@Sun.COM 
417710612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
417810612SRicardo.M.Correia@Sun.COM 
417910612SRicardo.M.Correia@Sun.COM 	/*
418010612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
418110612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
418210612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
418310612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
418410612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
418510612SRicardo.M.Correia@Sun.COM 	 * implementation.
418610612SRicardo.M.Correia@Sun.COM 	 *
418710612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
418810612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
418910612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
419010612SRicardo.M.Correia@Sun.COM 	 */
419110612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
419210612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
419310612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
419410612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
419510612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
419610612SRicardo.M.Correia@Sun.COM 	}
419710612SRicardo.M.Correia@Sun.COM 
419810612SRicardo.M.Correia@Sun.COM 	/*
419910612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
420010612SRicardo.M.Correia@Sun.COM 	 *
420110612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
420210612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
420310612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
420410612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
420510612SRicardo.M.Correia@Sun.COM 	 */
420610612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
420710612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
420810612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
420910612SRicardo.M.Correia@Sun.COM 
421010612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
421110612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
421210612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
421310612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
421410612SRicardo.M.Correia@Sun.COM 		else
421510612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
421610612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
421710612SRicardo.M.Correia@Sun.COM 
421810612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
421910612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
422010612SRicardo.M.Correia@Sun.COM 
422110612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
422210612SRicardo.M.Correia@Sun.COM 	}
422310612SRicardo.M.Correia@Sun.COM 
422410612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
422510612SRicardo.M.Correia@Sun.COM 
422610612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
422710612SRicardo.M.Correia@Sun.COM }
422810612SRicardo.M.Correia@Sun.COM 
422910922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4230789Sahrens void
423110922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4232789Sahrens {
423310922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
423410922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
423510922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
423610922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
423710922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
423810922SJeff.Bonwick@Sun.COM 	};
423910922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
424010922SJeff.Bonwick@Sun.COM 
424110922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
424210922SJeff.Bonwick@Sun.COM 
424310922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
424410922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
424510922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
424610922SJeff.Bonwick@Sun.COM 
424710922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
424810922SJeff.Bonwick@Sun.COM }
424910922SJeff.Bonwick@Sun.COM 
425010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
425110922SJeff.Bonwick@Sun.COM void
425210922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
425310922SJeff.Bonwick@Sun.COM {
425410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
425510922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
425610922SJeff.Bonwick@Sun.COM 
425710922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
425810922SJeff.Bonwick@Sun.COM 
425910922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
426010922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
426110922SJeff.Bonwick@Sun.COM 
426210922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
426310922SJeff.Bonwick@Sun.COM 
426410922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
426510922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
426610922SJeff.Bonwick@Sun.COM 
426710922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
426810922SJeff.Bonwick@Sun.COM 
426910922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4270789Sahrens }
4271789Sahrens 
4272789Sahrens /*
427310693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
427410693Schris.kirby@sun.com  */
427510693Schris.kirby@sun.com void
427610922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
427710693Schris.kirby@sun.com {
427810693Schris.kirby@sun.com 	int error;
427910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
428010693Schris.kirby@sun.com 	objset_t *origin;
428110693Schris.kirby@sun.com 	char snapname[100];
428210693Schris.kirby@sun.com 	char fullname[100];
428310693Schris.kirby@sun.com 	char clonename[100];
428410693Schris.kirby@sun.com 	char tag[100];
428510693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
428610693Schris.kirby@sun.com 
428710693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
428810693Schris.kirby@sun.com 
428910693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
429010693Schris.kirby@sun.com 
429110922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
429210693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
429310922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
429410922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
429510693Schris.kirby@sun.com 
429610693Schris.kirby@sun.com 	/*
429710693Schris.kirby@sun.com 	 * Clean up from any previous run.
429810693Schris.kirby@sun.com 	 */
429910693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
430010693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
430110693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
430210693Schris.kirby@sun.com 
430310693Schris.kirby@sun.com 	/*
430410693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
430510693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
430610693Schris.kirby@sun.com 	 */
430710693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
430810693Schris.kirby@sun.com 	if (error) {
430910693Schris.kirby@sun.com 		if (error == ENOSPC) {
431010693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
431110693Schris.kirby@sun.com 			goto out;
431210693Schris.kirby@sun.com 		}
431310693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
431410693Schris.kirby@sun.com 	}
431510693Schris.kirby@sun.com 
431610693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
431710693Schris.kirby@sun.com 	if (error)
431810693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
431910693Schris.kirby@sun.com 
432010693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
432110693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
432210693Schris.kirby@sun.com 	if (error) {
432310693Schris.kirby@sun.com 		if (error == ENOSPC) {
432410693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
432510693Schris.kirby@sun.com 			goto out;
432610693Schris.kirby@sun.com 		}
432710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
432810693Schris.kirby@sun.com 	}
432910693Schris.kirby@sun.com 
433010693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
433110693Schris.kirby@sun.com 	if (error) {
433210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
433310693Schris.kirby@sun.com 		    fullname, error);
433410693Schris.kirby@sun.com 	}
433510693Schris.kirby@sun.com 
433610693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
433710693Schris.kirby@sun.com 	if (error)
433810693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
433910693Schris.kirby@sun.com 
434010693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
434110693Schris.kirby@sun.com 	if (error != ENOENT)
434210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
434310693Schris.kirby@sun.com 
434410693Schris.kirby@sun.com 	/*
434510693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
434610693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
434710693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
434810693Schris.kirby@sun.com 	 */
434910693Schris.kirby@sun.com 	error = dmu_objset_snapshot(osname, snapname, NULL, FALSE);
435010693Schris.kirby@sun.com 	if (error) {
435110693Schris.kirby@sun.com 		if (error == ENOSPC) {
435210693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
435310693Schris.kirby@sun.com 			goto out;
435410693Schris.kirby@sun.com 		}
435510693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
435610693Schris.kirby@sun.com 	}
435710693Schris.kirby@sun.com 
4358*12527SChris.Kirby@oracle.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4359*12527SChris.Kirby@oracle.com 	    B_TRUE, -1);
436010693Schris.kirby@sun.com 	if (error)
436110693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
436210693Schris.kirby@sun.com 
436310693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
436410693Schris.kirby@sun.com 	if (error != EBUSY) {
436510693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
436610693Schris.kirby@sun.com 		    fullname, error);
436710693Schris.kirby@sun.com 	}
436810693Schris.kirby@sun.com 
436910693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
437010693Schris.kirby@sun.com 	if (error) {
437110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
437210693Schris.kirby@sun.com 		    fullname, error);
437310693Schris.kirby@sun.com 	}
437410693Schris.kirby@sun.com 
437510693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
437610693Schris.kirby@sun.com 	if (error)
437710693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
437810693Schris.kirby@sun.com 
437910693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
438010693Schris.kirby@sun.com 
438110693Schris.kirby@sun.com out:
438210693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
438310693Schris.kirby@sun.com }
438410693Schris.kirby@sun.com 
438510693Schris.kirby@sun.com /*
4386789Sahrens  * Inject random faults into the on-disk data.
4387789Sahrens  */
438810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4389789Sahrens void
439010922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4391789Sahrens {
439210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
439310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4394789Sahrens 	int fd;
4395789Sahrens 	uint64_t offset;
439611422SMark.Musante@Sun.COM 	uint64_t leaves;
4397789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4398789Sahrens 	uint64_t top, leaf;
4399789Sahrens 	char path0[MAXPATHLEN];
4400789Sahrens 	char pathrand[MAXPATHLEN];
4401789Sahrens 	size_t fsize;
4402789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4403789Sahrens 	int iters = 1000;
440411422SMark.Musante@Sun.COM 	int maxfaults;
440511422SMark.Musante@Sun.COM 	int mirror_save;
44067754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
44071544Seschrock 	uint64_t guid0 = 0;
440810685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
44091544Seschrock 
441011422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
441111422SMark.Musante@Sun.COM 	maxfaults = MAXFAULTS();
441211422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
441311422SMark.Musante@Sun.COM 	mirror_save = zs->zs_mirrors;
441411422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
441511422SMark.Musante@Sun.COM 
44167754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4417789Sahrens 
4418789Sahrens 	/*
44197754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4420789Sahrens 	 */
44217754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44227754SJeff.Bonwick@Sun.COM 
44237754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
44247754SJeff.Bonwick@Sun.COM 		/*
442510922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
44267754SJeff.Bonwick@Sun.COM 		 */
442710922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
442811422SMark.Musante@Sun.COM 		leaf = ztest_random(leaves) + zs->zs_splits;
44297754SJeff.Bonwick@Sun.COM 
44307754SJeff.Bonwick@Sun.COM 		/*
44317754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
44327754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
44337754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
44347754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
44357754SJeff.Bonwick@Sun.COM 		 */
44367754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
443711422SMark.Musante@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
44387754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
44397754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
44407754SJeff.Bonwick@Sun.COM 
44417754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
444210685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
444310685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
444410685SGeorge.Wilson@Sun.COM 
44457754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
44467754SJeff.Bonwick@Sun.COM 			/*
44477754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
44487754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
44497754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
44507754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
44517754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
44527754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
44537754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
44547754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
44557754SJeff.Bonwick@Sun.COM 			 */
44567754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
44577754SJeff.Bonwick@Sun.COM 
44587754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
44597754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
44607754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
44617754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
44627754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
44637754SJeff.Bonwick@Sun.COM 			} else {
44647754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
44657754SJeff.Bonwick@Sun.COM 			}
44667754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
44677754SJeff.Bonwick@Sun.COM 		}
44687754SJeff.Bonwick@Sun.COM 	} else {
44697754SJeff.Bonwick@Sun.COM 		/*
44707754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
44717754SJeff.Bonwick@Sun.COM 		 */
44727754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
44737754SJeff.Bonwick@Sun.COM 
44747754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
44757754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
44767754SJeff.Bonwick@Sun.COM 			return;
44777754SJeff.Bonwick@Sun.COM 		}
44787754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
44797754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
44807754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
44817754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
44827754SJeff.Bonwick@Sun.COM 
44837754SJeff.Bonwick@Sun.COM 		leaf = 0;
44847754SJeff.Bonwick@Sun.COM 		leaves = 1;
44857754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
44867754SJeff.Bonwick@Sun.COM 	}
4487789Sahrens 
44887754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
44897754SJeff.Bonwick@Sun.COM 
44901544Seschrock 	/*
449110685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
449210685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
44931544Seschrock 	 */
449410685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
44958241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
44968241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
44978241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
449810685SGeorge.Wilson@Sun.COM 
449910685SGeorge.Wilson@Sun.COM 			/*
450010685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
450110685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
450210685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
450310685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
450410685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
450510685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
450610685SGeorge.Wilson@Sun.COM 			 */
450710685SGeorge.Wilson@Sun.COM 			if (islog)
450810685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
450910685SGeorge.Wilson@Sun.COM 
45108241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
451110685SGeorge.Wilson@Sun.COM 
451210685SGeorge.Wilson@Sun.COM 			if (islog)
451310685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
45148241SJeff.Bonwick@Sun.COM 		} else {
45158241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
45168241SJeff.Bonwick@Sun.COM 		}
4517789Sahrens 	}
4518789Sahrens 
451910685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
452010685SGeorge.Wilson@Sun.COM 		return;
452110685SGeorge.Wilson@Sun.COM 
4522789Sahrens 	/*
45231544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4524789Sahrens 	 */
4525789Sahrens 	fd = open(pathrand, O_RDWR);
4526789Sahrens 
4527789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4528789Sahrens 		return;
4529789Sahrens 
4530789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4531789Sahrens 
4532789Sahrens 	while (--iters != 0) {
4533789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4534789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4535789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4536789Sahrens 
4537789Sahrens 		if (offset >= fsize)
4538789Sahrens 			continue;
4539789Sahrens 
454011422SMark.Musante@Sun.COM 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
454111422SMark.Musante@Sun.COM 		if (mirror_save != zs->zs_mirrors) {
454211422SMark.Musante@Sun.COM 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
454311422SMark.Musante@Sun.COM 			(void) close(fd);
454411422SMark.Musante@Sun.COM 			return;
454511422SMark.Musante@Sun.COM 		}
4546789Sahrens 
4547789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4548789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4549789Sahrens 			    offset, pathrand);
455011422SMark.Musante@Sun.COM 
455111422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
455211422SMark.Musante@Sun.COM 
455311422SMark.Musante@Sun.COM 		if (zopt_verbose >= 7)
455411422SMark.Musante@Sun.COM 			(void) printf("injected bad word into %s,"
455511422SMark.Musante@Sun.COM 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4556789Sahrens 	}
4557789Sahrens 
4558789Sahrens 	(void) close(fd);
4559789Sahrens }
4560789Sahrens 
4561789Sahrens /*
456210922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4563789Sahrens  */
4564789Sahrens void
456510922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4566789Sahrens {
456710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
456810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
456910922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
457010922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
457110922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
457210922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
457310922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
457410922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
457510922SJeff.Bonwick@Sun.COM 	void *buf;
457610922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
457710922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
457810922SJeff.Bonwick@Sun.COM 
457910922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
458010922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
458110922SJeff.Bonwick@Sun.COM 
458210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
458310922SJeff.Bonwick@Sun.COM 
458410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
458510922SJeff.Bonwick@Sun.COM 		return;
458610922SJeff.Bonwick@Sun.COM 
458710922SJeff.Bonwick@Sun.COM 	/*
458810922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
458910922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
459010922SJeff.Bonwick@Sun.COM 	 */
459110922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
459210922SJeff.Bonwick@Sun.COM 
459310922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
459410922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
459510922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
459610922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
459710922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
459810922SJeff.Bonwick@Sun.COM 		return;
459910922SJeff.Bonwick@Sun.COM 	}
460010922SJeff.Bonwick@Sun.COM 
460110922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
460210922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
460310922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
460410922SJeff.Bonwick@Sun.COM 
460510922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
460610922SJeff.Bonwick@Sun.COM 
460710922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
460810922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
460910922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
461010922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
461110922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
461210922SJeff.Bonwick@Sun.COM 		return;
461310922SJeff.Bonwick@Sun.COM 	}
461410922SJeff.Bonwick@Sun.COM 
461510922SJeff.Bonwick@Sun.COM 	/*
461610922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
461710922SJeff.Bonwick@Sun.COM 	 */
461810922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
461910922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
462012285SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
462112285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH) == 0);
462210922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
462310922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
462410922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
462510922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
462610922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
462710922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
462810922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
462910922SJeff.Bonwick@Sun.COM 	}
463010922SJeff.Bonwick@Sun.COM 
463110922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
463210922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
463310922SJeff.Bonwick@Sun.COM 
463410922SJeff.Bonwick@Sun.COM 	/*
463510922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
463610922SJeff.Bonwick@Sun.COM 	 */
463712285SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
463812285SJeff.Bonwick@Sun.COM 	    DMU_READ_NO_PREFETCH) == 0);
463910922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
464010922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
464110922SJeff.Bonwick@Sun.COM 
464210922SJeff.Bonwick@Sun.COM 	/*
464310922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
464410922SJeff.Bonwick@Sun.COM 	 */
464510922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
464610922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
464710922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
464810922SJeff.Bonwick@Sun.COM 
464910922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
465010922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
465110922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
465210922SJeff.Bonwick@Sun.COM 
465310922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
465410922SJeff.Bonwick@Sun.COM 
465510922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
465610922SJeff.Bonwick@Sun.COM }
465710922SJeff.Bonwick@Sun.COM 
465810922SJeff.Bonwick@Sun.COM /*
465910922SJeff.Bonwick@Sun.COM  * Scrub the pool.
466010922SJeff.Bonwick@Sun.COM  */
466110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
466210922SJeff.Bonwick@Sun.COM void
466310922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
466410922SJeff.Bonwick@Sun.COM {
466510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
466610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4667789Sahrens 
466812296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
466910922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
467012296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
4671789Sahrens }
4672789Sahrens 
4673789Sahrens /*
4674789Sahrens  * Rename the pool to a different name and then rename it back.
4675789Sahrens  */
467610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4677789Sahrens void
467810922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4679789Sahrens {
468010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4681789Sahrens 	char *oldname, *newname;
4682789Sahrens 	spa_t *spa;
4683789Sahrens 
468410922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
468510922SJeff.Bonwick@Sun.COM 
468610922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4687789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4688789Sahrens 	(void) strcpy(newname, oldname);
4689789Sahrens 	(void) strcat(newname, "_tmp");
4690789Sahrens 
4691789Sahrens 	/*
4692789Sahrens 	 * Do the rename
4693789Sahrens 	 */
469410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4695789Sahrens 
4696789Sahrens 	/*
4697789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4698789Sahrens 	 */
469910922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4700789Sahrens 
4701789Sahrens 	/*
4702789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4703789Sahrens 	 */
470410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
470510922SJeff.Bonwick@Sun.COM 
470610922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4707789Sahrens 	spa_close(spa, FTAG);
4708789Sahrens 
4709789Sahrens 	/*
4710789Sahrens 	 * Rename it back to the original
4711789Sahrens 	 */
471210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4713789Sahrens 
4714789Sahrens 	/*
4715789Sahrens 	 * Make sure it can still be opened
4716789Sahrens 	 */
471710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
471810922SJeff.Bonwick@Sun.COM 
471910922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4720789Sahrens 	spa_close(spa, FTAG);
4721789Sahrens 
4722789Sahrens 	umem_free(newname, strlen(newname) + 1);
4723789Sahrens 
472410922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4725789Sahrens }
4726789Sahrens 
4727789Sahrens /*
472810922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4729789Sahrens  */
4730789Sahrens static void
473110922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4732789Sahrens {
4733789Sahrens 	int status;
4734789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4735789Sahrens 	char zbuf[1024];
4736789Sahrens 	char *bin;
47374527Sperrin 	char *ztest;
47384527Sperrin 	char *isa;
47394527Sperrin 	int isalen;
4740789Sahrens 	FILE *fp;
4741789Sahrens 
4742789Sahrens 	(void) realpath(getexecname(), zdb);
4743789Sahrens 
4744789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4745789Sahrens 	bin = strstr(zdb, "/usr/bin/");
47464527Sperrin 	ztest = strstr(bin, "/ztest");
47474527Sperrin 	isa = bin + 8;
47484527Sperrin 	isalen = ztest - isa;
47494527Sperrin 	isa = strdup(isa);
4750789Sahrens 	/* LINTED */
47515994Sck153898 	(void) sprintf(bin,
475211811SJeff.Bonwick@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
47534527Sperrin 	    isalen,
47544527Sperrin 	    isa,
4755789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4756789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
475711811SJeff.Bonwick@Sun.COM 	    spa_config_path,
47587837SMatthew.Ahrens@Sun.COM 	    pool);
47594527Sperrin 	free(isa);
4760789Sahrens 
4761789Sahrens 	if (zopt_verbose >= 5)
4762789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4763789Sahrens 
4764789Sahrens 	fp = popen(zdb, "r");
4765789Sahrens 
4766789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4767789Sahrens 		if (zopt_verbose >= 3)
4768789Sahrens 			(void) printf("%s", zbuf);
4769789Sahrens 
4770789Sahrens 	status = pclose(fp);
4771789Sahrens 
4772789Sahrens 	if (status == 0)
4773789Sahrens 		return;
4774789Sahrens 
4775789Sahrens 	ztest_dump_core = 0;
4776789Sahrens 	if (WIFEXITED(status))
4777789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4778789Sahrens 	else
4779789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4780789Sahrens }
4781789Sahrens 
4782789Sahrens static void
4783789Sahrens ztest_walk_pool_directory(char *header)
4784789Sahrens {
4785789Sahrens 	spa_t *spa = NULL;
4786789Sahrens 
4787789Sahrens 	if (zopt_verbose >= 6)
4788789Sahrens 		(void) printf("%s\n", header);
4789789Sahrens 
4790789Sahrens 	mutex_enter(&spa_namespace_lock);
4791789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4792789Sahrens 		if (zopt_verbose >= 6)
4793789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4794789Sahrens 	mutex_exit(&spa_namespace_lock);
4795789Sahrens }
4796789Sahrens 
4797789Sahrens static void
4798789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4799789Sahrens {
48008241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4801789Sahrens 	uint64_t pool_guid;
4802789Sahrens 	spa_t *spa;
4803789Sahrens 
4804789Sahrens 	if (zopt_verbose >= 4) {
4805789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4806789Sahrens 		    oldname, newname);
4807789Sahrens 	}
4808789Sahrens 
4809789Sahrens 	/*
4810789Sahrens 	 * Clean up from previous runs.
4811789Sahrens 	 */
4812789Sahrens 	(void) spa_destroy(newname);
4813789Sahrens 
4814789Sahrens 	/*
4815789Sahrens 	 * Get the pool's configuration and guid.
4816789Sahrens 	 */
481710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4818789Sahrens 
48198241SJeff.Bonwick@Sun.COM 	/*
48208241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
48218241SJeff.Bonwick@Sun.COM 	 */
48228241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
482312296SLin.Ling@Sun.COM 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
48248241SJeff.Bonwick@Sun.COM 
4825789Sahrens 	pool_guid = spa_guid(spa);
4826789Sahrens 	spa_close(spa, FTAG);
4827789Sahrens 
4828789Sahrens 	ztest_walk_pool_directory("pools before export");
4829789Sahrens 
4830789Sahrens 	/*
4831789Sahrens 	 * Export it.
4832789Sahrens 	 */
483310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4834789Sahrens 
4835789Sahrens 	ztest_walk_pool_directory("pools after export");
4836789Sahrens 
4837789Sahrens 	/*
48388241SJeff.Bonwick@Sun.COM 	 * Try to import it.
48398241SJeff.Bonwick@Sun.COM 	 */
48408241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
48418241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
48428241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
48438241SJeff.Bonwick@Sun.COM 
48448241SJeff.Bonwick@Sun.COM 	/*
4845789Sahrens 	 * Import it under the new name.
4846789Sahrens 	 */
484710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL));
4848789Sahrens 
4849789Sahrens 	ztest_walk_pool_directory("pools after import");
4850789Sahrens 
4851789Sahrens 	/*
4852789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4853789Sahrens 	 */
485410922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL));
4855789Sahrens 
4856789Sahrens 	/*
4857789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4858789Sahrens 	 */
485910922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL));
4860789Sahrens 
4861789Sahrens 	/*
4862789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4863789Sahrens 	 */
486410922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4865789Sahrens 
4866789Sahrens 	/*
4867789Sahrens 	 * Verify that we can open and close the pool using the new name.
4868789Sahrens 	 */
486910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4870789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4871789Sahrens 	spa_close(spa, FTAG);
4872789Sahrens 
4873789Sahrens 	nvlist_free(config);
4874789Sahrens }
4875789Sahrens 
48768241SJeff.Bonwick@Sun.COM static void
48778241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
48788241SJeff.Bonwick@Sun.COM {
487910922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
488010922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
488110922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
488210922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
488310922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
488410922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
48858241SJeff.Bonwick@Sun.COM }
48868241SJeff.Bonwick@Sun.COM 
48875329Sgw25295 static void *
48888241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
48895329Sgw25295 {
48907754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
48915329Sgw25295 
48925329Sgw25295 	while (!ztest_exiting) {
489310922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
489410922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
489510922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
48965329Sgw25295 	}
48975329Sgw25295 	return (NULL);
48985329Sgw25295 }
48995329Sgw25295 
4900789Sahrens static void *
490110922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
490210922SJeff.Bonwick@Sun.COM {
490310922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
490410922SJeff.Bonwick@Sun.COM 	int grace = 300;
490510922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
490610922SJeff.Bonwick@Sun.COM 
490710922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
490810922SJeff.Bonwick@Sun.COM 
490910922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
491010922SJeff.Bonwick@Sun.COM 
491110922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
491210922SJeff.Bonwick@Sun.COM 
491310922SJeff.Bonwick@Sun.COM 	return (NULL);
491410922SJeff.Bonwick@Sun.COM }
491510922SJeff.Bonwick@Sun.COM 
491610922SJeff.Bonwick@Sun.COM static void
491710922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
491810922SJeff.Bonwick@Sun.COM {
491910922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
492010922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
492110922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
492210922SJeff.Bonwick@Sun.COM 
492310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
492410922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
492510922SJeff.Bonwick@Sun.COM 
492610922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
492710922SJeff.Bonwick@Sun.COM 
492810922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
492910922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
493010922SJeff.Bonwick@Sun.COM 
493110922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
493210922SJeff.Bonwick@Sun.COM 		Dl_info dli;
493310922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
493410922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
493510922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
493610922SJeff.Bonwick@Sun.COM 	}
493710922SJeff.Bonwick@Sun.COM }
493810922SJeff.Bonwick@Sun.COM 
493910922SJeff.Bonwick@Sun.COM static void *
4940789Sahrens ztest_thread(void *arg)
4941789Sahrens {
494210922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4943789Sahrens 	ztest_shared_t *zs = ztest_shared;
494410922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
494510922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4946789Sahrens 	ztest_info_t *zi;
494710922SJeff.Bonwick@Sun.COM 
494810922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4949789Sahrens 		/*
4950789Sahrens 		 * See if it's time to force a crash.
4951789Sahrens 		 */
495210922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
495310922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4954789Sahrens 
4955789Sahrens 		/*
4956789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4957789Sahrens 		 */
4958789Sahrens 		if (zs->zs_enospc_count > 10)
4959789Sahrens 			break;
496010922SJeff.Bonwick@Sun.COM 
496110922SJeff.Bonwick@Sun.COM 		/*
496210922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
496310922SJeff.Bonwick@Sun.COM 		 */
496410922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
496510922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
496610922SJeff.Bonwick@Sun.COM 
496710922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
496810922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
496910922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
497010922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4971789Sahrens 	}
4972789Sahrens 
4973789Sahrens 	return (NULL);
4974789Sahrens }
4975789Sahrens 
497610922SJeff.Bonwick@Sun.COM static void
497710922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
497810922SJeff.Bonwick@Sun.COM {
497910922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
498010922SJeff.Bonwick@Sun.COM }
498110922SJeff.Bonwick@Sun.COM 
498210922SJeff.Bonwick@Sun.COM static void
498310922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
498410922SJeff.Bonwick@Sun.COM {
498510922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
498610922SJeff.Bonwick@Sun.COM 
498710922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
498810922SJeff.Bonwick@Sun.COM 
498910922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
499010922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
499110922SJeff.Bonwick@Sun.COM 
499210922SJeff.Bonwick@Sun.COM 	/*
499310922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
499410922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
499510922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
499610922SJeff.Bonwick@Sun.COM 	 */
499710922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
499810922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
499910922SJeff.Bonwick@Sun.COM 
500010922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
500110922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
500210922SJeff.Bonwick@Sun.COM }
500310922SJeff.Bonwick@Sun.COM 
500410922SJeff.Bonwick@Sun.COM static void
500510922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
500610922SJeff.Bonwick@Sun.COM {
500710922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
500810922SJeff.Bonwick@Sun.COM 
500910922SJeff.Bonwick@Sun.COM 	/*
501010922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
501110922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
501210922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
501310922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
501410922SJeff.Bonwick@Sun.COM 	 *
501510922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
501610922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
501710922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
501810922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
501910922SJeff.Bonwick@Sun.COM 	 */
502010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
502110922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
502210922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
502310922SJeff.Bonwick@Sun.COM }
502410922SJeff.Bonwick@Sun.COM 
502510922SJeff.Bonwick@Sun.COM static int
502610922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
502710922SJeff.Bonwick@Sun.COM {
502810922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
502910922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
503010922SJeff.Bonwick@Sun.COM 	objset_t *os;
503110922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
503210922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
503310922SJeff.Bonwick@Sun.COM 	int error;
503410922SJeff.Bonwick@Sun.COM 
503510922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
503610922SJeff.Bonwick@Sun.COM 
503710922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
503810922SJeff.Bonwick@Sun.COM 
503912294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
504010922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
504110922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
504210922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
504310922SJeff.Bonwick@Sun.COM 		return (error);
504410922SJeff.Bonwick@Sun.COM 	}
504510922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
504610922SJeff.Bonwick@Sun.COM 
504710922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
504810922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
504910922SJeff.Bonwick@Sun.COM 
505010922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
505110922SJeff.Bonwick@Sun.COM 
505210922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
505310922SJeff.Bonwick@Sun.COM 
505410922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
505510922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
505610922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
505710922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
505810922SJeff.Bonwick@Sun.COM 
505910922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
506010922SJeff.Bonwick@Sun.COM 
506110922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
506210922SJeff.Bonwick@Sun.COM 
506310922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
506410922SJeff.Bonwick@Sun.COM 
506510922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
506610922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
506710922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
506810922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
506910922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
507010922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
507110922SJeff.Bonwick@Sun.COM 
507210922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
507310922SJeff.Bonwick@Sun.COM 
507410922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
507510922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
507610922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
507710922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
507810922SJeff.Bonwick@Sun.COM 
507910922SJeff.Bonwick@Sun.COM 	return (0);
508010922SJeff.Bonwick@Sun.COM }
508110922SJeff.Bonwick@Sun.COM 
508210922SJeff.Bonwick@Sun.COM static void
508310922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
508410922SJeff.Bonwick@Sun.COM {
508510922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
508610922SJeff.Bonwick@Sun.COM 
508710922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
508810922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
508910922SJeff.Bonwick@Sun.COM 
509010922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
509110922SJeff.Bonwick@Sun.COM }
509210922SJeff.Bonwick@Sun.COM 
5093789Sahrens /*
5094789Sahrens  * Kick off threads to run tests on all datasets in parallel.
5095789Sahrens  */
5096789Sahrens static void
509710922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
5098789Sahrens {
509910922SJeff.Bonwick@Sun.COM 	thread_t *tid;
5100789Sahrens 	spa_t *spa;
51017754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
510210922SJeff.Bonwick@Sun.COM 	int error;
51037754SJeff.Bonwick@Sun.COM 
51047754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
5105789Sahrens 
510610922SJeff.Bonwick@Sun.COM 	/*
510710922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
510810922SJeff.Bonwick@Sun.COM 	 */
510910922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
511010922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
511110922SJeff.Bonwick@Sun.COM 
511210922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
511310922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
511410922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
511510922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
511610922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
511710922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
511810922SJeff.Bonwick@Sun.COM 
511910922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
512010612SRicardo.M.Correia@Sun.COM 
512110612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
512210612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
512310612SRicardo.M.Correia@Sun.COM 
5124789Sahrens 	/*
51257754SJeff.Bonwick@Sun.COM 	 * Open our pool.
51265329Sgw25295 	 */
512710922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
512810922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
512910922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
513010922SJeff.Bonwick@Sun.COM 
513110922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
51325329Sgw25295 
51335329Sgw25295 	/*
51348241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
51358241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
51368241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
51378241SJeff.Bonwick@Sun.COM 	 */
513811422SMark.Musante@Sun.COM 	if (MAXFAULTS() == 0)
51398241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
51408241SJeff.Bonwick@Sun.COM 	else
51418241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
51428241SJeff.Bonwick@Sun.COM 
51438241SJeff.Bonwick@Sun.COM 	/*
51447754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5145789Sahrens 	 */
51468241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
51477754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5148789Sahrens 
5149789Sahrens 	/*
515010922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
515110922SJeff.Bonwick@Sun.COM 	 */
515210922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
515310922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
515410922SJeff.Bonwick@Sun.COM 
515510922SJeff.Bonwick@Sun.COM 	/*
5156789Sahrens 	 * Verify that we can safely inquire about about any object,
5157789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5158789Sahrens 	 * we probe a 5-wide window around each power of two.
5159789Sahrens 	 * This hits all edge cases, including zero and the max.
5160789Sahrens 	 */
516110922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
516210922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5163789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5164789Sahrens 			    (1ULL << t) + d, NULL);
51651544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
51661544Seschrock 			    error == EINVAL);
5167789Sahrens 		}
5168789Sahrens 	}
5169789Sahrens 
5170789Sahrens 	/*
517110922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5172789Sahrens 	 */
517310922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
517410922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
517510922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
517610922SJeff.Bonwick@Sun.COM 	}
5177789Sahrens 	zs->zs_enospc_count = 0;
5178789Sahrens 
517910922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5180789Sahrens 
5181789Sahrens 	if (zopt_verbose >= 4)
5182789Sahrens 		(void) printf("starting main threads...\n");
5183789Sahrens 
518410922SJeff.Bonwick@Sun.COM 	/*
518510922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
518610922SJeff.Bonwick@Sun.COM 	 */
518710922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
518810922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
518910922SJeff.Bonwick@Sun.COM 			return;
519010922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
519110922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5192789Sahrens 	}
5193789Sahrens 
5194789Sahrens 	/*
519510922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
519610922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5197789Sahrens 	 */
519810922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
519910922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
520010922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
520110922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5202789Sahrens 	}
5203789Sahrens 
52041544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5205789Sahrens 
520610922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
520710922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
520810922SJeff.Bonwick@Sun.COM 
520910922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
52107754SJeff.Bonwick@Sun.COM 
52117754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
52127754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
52137754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
52148241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
52157754SJeff.Bonwick@Sun.COM 
5216789Sahrens 	/*
5217789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5218789Sahrens 	 * spa_close() should wait for it to complete.
5219789Sahrens 	 */
522010922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
522110922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5222789Sahrens 
52235530Sbonwick 	spa_close(spa, FTAG);
52245530Sbonwick 
522510922SJeff.Bonwick@Sun.COM 	/*
522610922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
522710922SJeff.Bonwick@Sun.COM 	 */
522810922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
522910922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
523010922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
523110922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
523210922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
523310922SJeff.Bonwick@Sun.COM 
523410922SJeff.Bonwick@Sun.COM 	/*
523510922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
523610922SJeff.Bonwick@Sun.COM 	 * different name.
523710922SJeff.Bonwick@Sun.COM 	 */
523810922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
523910922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
524010922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
524110922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
524210922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
524310922SJeff.Bonwick@Sun.COM 	}
524410922SJeff.Bonwick@Sun.COM 
524510922SJeff.Bonwick@Sun.COM 	kernel_fini();
524610922SJeff.Bonwick@Sun.COM }
524710922SJeff.Bonwick@Sun.COM 
524810922SJeff.Bonwick@Sun.COM static void
524910922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
525010922SJeff.Bonwick@Sun.COM {
525110922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
525210922SJeff.Bonwick@Sun.COM 	spa_t *spa;
525311818SMark.Musante@Sun.COM 	int numloops = 0;
525410922SJeff.Bonwick@Sun.COM 
525510922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
525610922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
525710922SJeff.Bonwick@Sun.COM 
525810922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
525910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
526010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
526110922SJeff.Bonwick@Sun.COM 
526210922SJeff.Bonwick@Sun.COM 	/*
526310922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
526410922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
526510922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
526610922SJeff.Bonwick@Sun.COM 	 */
526710922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
526810922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
526910922SJeff.Bonwick@Sun.COM 		zil_commit(zd->zd_zilog, UINT64_MAX, 0);
527010922SJeff.Bonwick@Sun.COM 	}
527110922SJeff.Bonwick@Sun.COM 
527210922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
527310922SJeff.Bonwick@Sun.COM 
527410922SJeff.Bonwick@Sun.COM 	/*
527510922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
527610922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
527710922SJeff.Bonwick@Sun.COM 	 */
527810922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
527910922SJeff.Bonwick@Sun.COM 
528010922SJeff.Bonwick@Sun.COM 	/*
528110922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
528210922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
528310922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
528410922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
528510922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
528610922SJeff.Bonwick@Sun.COM 	 */
528711818SMark.Musante@Sun.COM 	while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
528810922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
528910922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
529010922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
529110922SJeff.Bonwick@Sun.COM 	}
529210922SJeff.Bonwick@Sun.COM 
529310922SJeff.Bonwick@Sun.COM 	/*
529410922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
529510922SJeff.Bonwick@Sun.COM 	 */
529610922SJeff.Bonwick@Sun.COM 	zil_commit(zd->zd_zilog, UINT64_MAX, 0);
529710922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
529810922SJeff.Bonwick@Sun.COM 
529910922SJeff.Bonwick@Sun.COM 	/*
530010922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
530110922SJeff.Bonwick@Sun.COM 	 */
530210922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
530310922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
530410922SJeff.Bonwick@Sun.COM 	kernel_fini();
530510922SJeff.Bonwick@Sun.COM 
530610922SJeff.Bonwick@Sun.COM 	/*
530710922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
530810922SJeff.Bonwick@Sun.COM 	 */
530910922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
531010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
531110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
531210922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
531310922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5314789Sahrens 	kernel_fini();
531510612SRicardo.M.Correia@Sun.COM 
531610612SRicardo.M.Correia@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
531710612SRicardo.M.Correia@Sun.COM 
531810612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
531910612SRicardo.M.Correia@Sun.COM 
532010612SRicardo.M.Correia@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
532110612SRicardo.M.Correia@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5322789Sahrens }
5323789Sahrens 
5324789Sahrens void
5325789Sahrens print_time(hrtime_t t, char *timebuf)
5326789Sahrens {
5327789Sahrens 	hrtime_t s = t / NANOSEC;
5328789Sahrens 	hrtime_t m = s / 60;
5329789Sahrens 	hrtime_t h = m / 60;
5330789Sahrens 	hrtime_t d = h / 24;
5331789Sahrens 
5332789Sahrens 	s -= m * 60;
5333789Sahrens 	m -= h * 60;
5334789Sahrens 	h -= d * 24;
5335789Sahrens 
5336789Sahrens 	timebuf[0] = '\0';
5337789Sahrens 
5338789Sahrens 	if (d)
5339789Sahrens 		(void) sprintf(timebuf,
5340789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5341789Sahrens 	else if (h)
5342789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5343789Sahrens 	else if (m)
5344789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5345789Sahrens 	else
5346789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5347789Sahrens }
5348789Sahrens 
534911422SMark.Musante@Sun.COM static nvlist_t *
535011422SMark.Musante@Sun.COM make_random_props()
535111422SMark.Musante@Sun.COM {
535211422SMark.Musante@Sun.COM 	nvlist_t *props;
535311422SMark.Musante@Sun.COM 
535411422SMark.Musante@Sun.COM 	if (ztest_random(2) == 0)
535511422SMark.Musante@Sun.COM 		return (NULL);
535611422SMark.Musante@Sun.COM 
535711422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
535811422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
535911422SMark.Musante@Sun.COM 
536011422SMark.Musante@Sun.COM 	(void) printf("props:\n");
536111422SMark.Musante@Sun.COM 	dump_nvlist(props, 4);
536211422SMark.Musante@Sun.COM 
536311422SMark.Musante@Sun.COM 	return (props);
536411422SMark.Musante@Sun.COM }
536511422SMark.Musante@Sun.COM 
5366789Sahrens /*
5367789Sahrens  * Create a storage pool with the given name and initial vdev size.
536810922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5369789Sahrens  */
5370789Sahrens static void
537110922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5372789Sahrens {
5373789Sahrens 	spa_t *spa;
537411422SMark.Musante@Sun.COM 	nvlist_t *nvroot, *props;
5375789Sahrens 
537610922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
537710922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
537810922SJeff.Bonwick@Sun.COM 
5379789Sahrens 	kernel_init(FREAD | FWRITE);
5380789Sahrens 
5381789Sahrens 	/*
5382789Sahrens 	 * Create the storage pool.
5383789Sahrens 	 */
538410922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
538510594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
538611422SMark.Musante@Sun.COM 	zs->zs_splits = 0;
538711422SMark.Musante@Sun.COM 	zs->zs_mirrors = zopt_mirrors;
53887754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
538911422SMark.Musante@Sun.COM 	    0, zopt_raidz, zs->zs_mirrors, 1);
539011422SMark.Musante@Sun.COM 	props = make_random_props();
539111422SMark.Musante@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5392789Sahrens 	nvlist_free(nvroot);
5393789Sahrens 
539410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
53959480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5396789Sahrens 	spa_close(spa, FTAG);
5397789Sahrens 
5398789Sahrens 	kernel_fini();
539910922SJeff.Bonwick@Sun.COM 
540010922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
540110922SJeff.Bonwick@Sun.COM 
540210922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
540310922SJeff.Bonwick@Sun.COM 
540410922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
5405789Sahrens }
5406789Sahrens 
5407789Sahrens int
5408789Sahrens main(int argc, char **argv)
5409789Sahrens {
5410789Sahrens 	int kills = 0;
5411789Sahrens 	int iters = 0;
5412789Sahrens 	ztest_shared_t *zs;
541310922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5414789Sahrens 	ztest_info_t *zi;
5415789Sahrens 	char timebuf[100];
5416789Sahrens 	char numbuf[6];
541710922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5418789Sahrens 
5419789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5420789Sahrens 
5421789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5422789Sahrens 
5423789Sahrens 	process_options(argc, argv);
5424789Sahrens 
542511811SJeff.Bonwick@Sun.COM 	/* Override location of zpool.cache */
542611811SJeff.Bonwick@Sun.COM 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
542711811SJeff.Bonwick@Sun.COM 
54281544Seschrock 	/*
54291544Seschrock 	 * Blow away any existing copy of zpool.cache
54301544Seschrock 	 */
54311544Seschrock 	if (zopt_init != 0)
543211811SJeff.Bonwick@Sun.COM 		(void) remove(spa_config_path);
54331544Seschrock 
543410922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
543510922SJeff.Bonwick@Sun.COM 
5436789Sahrens 	zs = ztest_shared = (void *)mmap(0,
543710922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5438789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5439789Sahrens 
5440789Sahrens 	if (zopt_verbose >= 1) {
5441789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5442789Sahrens 		    " %llu seconds...\n",
54431732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5444789Sahrens 		    (u_longlong_t)zopt_time);
5445789Sahrens 	}
5446789Sahrens 
5447789Sahrens 	/*
5448789Sahrens 	 * Create and initialize our storage pool.
5449789Sahrens 	 */
545010922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5451789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5452789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5453789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
545410922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
545510922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5456789Sahrens 	}
5457789Sahrens 
545810922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
545910922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
546010922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
546110922SJeff.Bonwick@Sun.COM 
546210922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5463789Sahrens 		zi = &zs->zs_info[f];
5464789Sahrens 		*zi = ztest_info[f];
546510922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
546610922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5467789Sahrens 		else
546810922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
546910922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5470789Sahrens 	}
5471789Sahrens 
5472789Sahrens 	/*
5473789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5474789Sahrens 	 * to verify that self-healing data works, and forced crashes
5475789Sahrens 	 * to verify that we never lose on-disk consistency.
5476789Sahrens 	 */
547710922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5478789Sahrens 		int status;
5479789Sahrens 		pid_t pid;
5480789Sahrens 
5481789Sahrens 		/*
5482789Sahrens 		 * Initialize the workload counters for each function.
5483789Sahrens 		 */
548410922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5485789Sahrens 			zi = &zs->zs_info[f];
548610922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5487789Sahrens 			zi->zi_call_time = 0;
5488789Sahrens 		}
5489789Sahrens 
54909480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
54919480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
54929480SGeorge.Wilson@Sun.COM 
5493789Sahrens 		pid = fork();
5494789Sahrens 
5495789Sahrens 		if (pid == -1)
5496789Sahrens 			fatal(1, "fork failed");
5497789Sahrens 
5498789Sahrens 		if (pid == 0) {	/* child */
5499789Sahrens 			struct rlimit rl = { 1024, 1024 };
5500789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
55011914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
550210922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5503789Sahrens 			exit(0);
5504789Sahrens 		}
5505789Sahrens 
55062856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5507789Sahrens 			continue;
5508789Sahrens 
5509789Sahrens 		if (WIFEXITED(status)) {
5510789Sahrens 			if (WEXITSTATUS(status) != 0) {
5511789Sahrens 				(void) fprintf(stderr,
5512789Sahrens 				    "child exited with code %d\n",
5513789Sahrens 				    WEXITSTATUS(status));
5514789Sahrens 				exit(2);
5515789Sahrens 			}
55162856Snd150628 		} else if (WIFSIGNALED(status)) {
5517789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5518789Sahrens 				(void) fprintf(stderr,
5519789Sahrens 				    "child died with signal %d\n",
5520789Sahrens 				    WTERMSIG(status));
5521789Sahrens 				exit(3);
5522789Sahrens 			}
5523789Sahrens 			kills++;
55242856Snd150628 		} else {
55252856Snd150628 			(void) fprintf(stderr, "something strange happened "
55262856Snd150628 			    "to child\n");
55272856Snd150628 			exit(4);
5528789Sahrens 		}
5529789Sahrens 
5530789Sahrens 		iters++;
5531789Sahrens 
5532789Sahrens 		if (zopt_verbose >= 1) {
5533789Sahrens 			hrtime_t now = gethrtime();
5534789Sahrens 
553510922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
553610922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5537789Sahrens 			nicenum(zs->zs_space, numbuf);
5538789Sahrens 
5539789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5540789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5541789Sahrens 			    iters,
5542789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5543789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5544789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5545789Sahrens 			    numbuf,
554610922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5547789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5548789Sahrens 		}
5549789Sahrens 
5550789Sahrens 		if (zopt_verbose >= 2) {
5551789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5552789Sahrens 			(void) printf("%7s %9s   %s\n",
5553789Sahrens 			    "Calls", "Time", "Function");
5554789Sahrens 			(void) printf("%7s %9s   %s\n",
5555789Sahrens 			    "-----", "----", "--------");
555610922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5557789Sahrens 				Dl_info dli;
5558789Sahrens 
5559789Sahrens 				zi = &zs->zs_info[f];
5560789Sahrens 				print_time(zi->zi_call_time, timebuf);
5561789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5562789Sahrens 				(void) printf("%7llu %9s   %s\n",
556310922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5564789Sahrens 				    dli.dli_sname);
5565789Sahrens 			}
5566789Sahrens 			(void) printf("\n");
5567789Sahrens 		}
5568789Sahrens 
5569789Sahrens 		/*
557010922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
557110922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
557210922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5573789Sahrens 		 */
557410922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
557510922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
557610922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
557710922SJeff.Bonwick@Sun.COM 		} else {
557810922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
557910922SJeff.Bonwick@Sun.COM 			kernel_fini();
558010922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
558110922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
558210922SJeff.Bonwick@Sun.COM 			    zopt_pool);
558310922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
558410922SJeff.Bonwick@Sun.COM 		}
5585789Sahrens 		kernel_fini();
558610922SJeff.Bonwick@Sun.COM 
558710922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5588789Sahrens 	}
5589789Sahrens 
5590789Sahrens 	if (zopt_verbose >= 1) {
5591789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5592789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5593789Sahrens 	}
5594789Sahrens 
5595789Sahrens 	return (0);
5596789Sahrens }
5597