xref: /onnv-gate/usr/src/cmd/ztest/ztest.c (revision 13043:8c712bbb18ea)
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 *
_umem_debug_init()362789Sahrens _umem_debug_init()
363789Sahrens {
364789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
365789Sahrens }
366789Sahrens 
367789Sahrens const char *
_umem_logging_init(void)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
fatal(int do_perror,char * message,...)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
str2shift(const char * buf)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
nicenumtoull(const char * buf)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
usage(boolean_t requested)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
process_options(int argc,char ** argv)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
ztest_kill(ztest_shared_t * zs)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
ztest_random(uint64_t range)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
ztest_record_enospc(const char * s)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
ztest_get_ashift(void)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 *
make_vdev_file(char * path,char * aux,size_t size,uint64_t ashift)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 *
make_vdev_raidz(char * path,char * aux,size_t size,uint64_t ashift,int r)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 *
make_vdev_mirror(char * path,char * aux,size_t size,uint64_t ashift,int r,int m)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 *
make_vdev_root(char * path,char * aux,size_t size,uint64_t ashift,int log,int r,int m,int 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
ztest_random_blocksize(void)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
ztest_random_ibshift(void)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
ztest_random_vdev_top(spa_t * spa,boolean_t log_ok)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
ztest_random_dsl_prop(zfs_prop_t prop)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
ztest_dsl_prop_set_uint64(char * osname,zfs_prop_t prop,uint64_t value,boolean_t inherit)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
ztest_spa_prop_set_uint64(ztest_shared_t * zs,zpool_prop_t prop,uint64_t value)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
ztest_rll_init(rll_t * rll)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
ztest_rll_destroy(rll_t * rll)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
ztest_rll_lock(rll_t * rll,rl_type_t type)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
ztest_rll_unlock(rll_t * rll)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
ztest_object_lock(ztest_ds_t * zd,uint64_t object,rl_type_t type)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
ztest_object_unlock(ztest_ds_t * zd,uint64_t object)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 *
ztest_range_lock(ztest_ds_t * zd,uint64_t object,uint64_t offset,uint64_t size,rl_type_t type)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
ztest_range_unlock(rl_t * rl)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
ztest_zd_init(ztest_ds_t * zd,objset_t * os)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
ztest_zd_fini(ztest_ds_t * zd)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
ztest_tx_assign(dmu_tx_t * tx,uint64_t txg_how,const char * tag)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
ztest_pattern_set(void * buf,uint64_t size,uint64_t value)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
ztest_pattern_match(void * buf,uint64_t size,uint64_t value)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
ztest_bt_generate(ztest_block_tag_t * bt,objset_t * os,uint64_t object,uint64_t offset,uint64_t gen,uint64_t txg,uint64_t crtxg)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
ztest_bt_verify(ztest_block_tag_t * bt,objset_t * os,uint64_t object,uint64_t offset,uint64_t gen,uint64_t txg,uint64_t crtxg)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 *
ztest_bt_bonus(dmu_buf_t * db)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 
110512699SNeil.Perrin@Sun.COM static void
ztest_log_create(ztest_ds_t * zd,dmu_tx_t * tx,lr_create_t * lr)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))
111312699SNeil.Perrin@Sun.COM 		return;
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 
111912699SNeil.Perrin@Sun.COM 	zil_itx_assign(zd->zd_zilog, itx, tx);
112010922SJeff.Bonwick@Sun.COM }
112110922SJeff.Bonwick@Sun.COM 
112212699SNeil.Perrin@Sun.COM static void
ztest_log_remove(ztest_ds_t * zd,dmu_tx_t * tx,lr_remove_t * lr,uint64_t object)112312699SNeil.Perrin@Sun.COM ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
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))
113012699SNeil.Perrin@Sun.COM 		return;
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 
113612700SNeil.Perrin@Sun.COM 	itx->itx_oid = object;
113712699SNeil.Perrin@Sun.COM 	zil_itx_assign(zd->zd_zilog, itx, tx);
113810922SJeff.Bonwick@Sun.COM }
113910922SJeff.Bonwick@Sun.COM 
114012699SNeil.Perrin@Sun.COM static void
ztest_log_write(ztest_ds_t * zd,dmu_tx_t * tx,lr_write_t * lr)114110922SJeff.Bonwick@Sun.COM ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
114210922SJeff.Bonwick@Sun.COM {
114310922SJeff.Bonwick@Sun.COM 	itx_t *itx;
114410922SJeff.Bonwick@Sun.COM 	itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
114510922SJeff.Bonwick@Sun.COM 
114610922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
114712699SNeil.Perrin@Sun.COM 		return;
114810922SJeff.Bonwick@Sun.COM 
114910922SJeff.Bonwick@Sun.COM 	if (lr->lr_length > ZIL_MAX_LOG_DATA)
115010922SJeff.Bonwick@Sun.COM 		write_state = WR_INDIRECT;
115110922SJeff.Bonwick@Sun.COM 
115210922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_WRITE,
115310922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
115410922SJeff.Bonwick@Sun.COM 
115510922SJeff.Bonwick@Sun.COM 	if (write_state == WR_COPIED &&
115610922SJeff.Bonwick@Sun.COM 	    dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
115710922SJeff.Bonwick@Sun.COM 	    ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
115810922SJeff.Bonwick@Sun.COM 		zil_itx_destroy(itx);
115910922SJeff.Bonwick@Sun.COM 		itx = zil_itx_create(TX_WRITE, sizeof (*lr));
116010922SJeff.Bonwick@Sun.COM 		write_state = WR_NEED_COPY;
116110922SJeff.Bonwick@Sun.COM 	}
116210922SJeff.Bonwick@Sun.COM 	itx->itx_private = zd;
116310922SJeff.Bonwick@Sun.COM 	itx->itx_wr_state = write_state;
116410922SJeff.Bonwick@Sun.COM 	itx->itx_sync = (ztest_random(8) == 0);
116510922SJeff.Bonwick@Sun.COM 	itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
116610922SJeff.Bonwick@Sun.COM 
116710922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
116810922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
116910922SJeff.Bonwick@Sun.COM 
117012699SNeil.Perrin@Sun.COM 	zil_itx_assign(zd->zd_zilog, itx, tx);
117110922SJeff.Bonwick@Sun.COM }
117210922SJeff.Bonwick@Sun.COM 
117312699SNeil.Perrin@Sun.COM static void
ztest_log_truncate(ztest_ds_t * zd,dmu_tx_t * tx,lr_truncate_t * lr)117410922SJeff.Bonwick@Sun.COM ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
117510922SJeff.Bonwick@Sun.COM {
117610922SJeff.Bonwick@Sun.COM 	itx_t *itx;
117710922SJeff.Bonwick@Sun.COM 
117810922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
117912699SNeil.Perrin@Sun.COM 		return;
118010922SJeff.Bonwick@Sun.COM 
118110922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
118210922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
118310922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
118410922SJeff.Bonwick@Sun.COM 
118512699SNeil.Perrin@Sun.COM 	itx->itx_sync = B_FALSE;
118612699SNeil.Perrin@Sun.COM 	zil_itx_assign(zd->zd_zilog, itx, tx);
118710922SJeff.Bonwick@Sun.COM }
118810922SJeff.Bonwick@Sun.COM 
118912699SNeil.Perrin@Sun.COM static void
ztest_log_setattr(ztest_ds_t * zd,dmu_tx_t * tx,lr_setattr_t * lr)119010922SJeff.Bonwick@Sun.COM ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
119110922SJeff.Bonwick@Sun.COM {
119210922SJeff.Bonwick@Sun.COM 	itx_t *itx;
119310922SJeff.Bonwick@Sun.COM 
119410922SJeff.Bonwick@Sun.COM 	if (zil_replaying(zd->zd_zilog, tx))
119512699SNeil.Perrin@Sun.COM 		return;
119610922SJeff.Bonwick@Sun.COM 
119710922SJeff.Bonwick@Sun.COM 	itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
119810922SJeff.Bonwick@Sun.COM 	bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
119910922SJeff.Bonwick@Sun.COM 	    sizeof (*lr) - sizeof (lr_t));
120010922SJeff.Bonwick@Sun.COM 
120112699SNeil.Perrin@Sun.COM 	itx->itx_sync = B_FALSE;
120212699SNeil.Perrin@Sun.COM 	zil_itx_assign(zd->zd_zilog, itx, tx);
120310922SJeff.Bonwick@Sun.COM }
120410922SJeff.Bonwick@Sun.COM 
120510922SJeff.Bonwick@Sun.COM /*
120610922SJeff.Bonwick@Sun.COM  * ZIL replay ops
120710922SJeff.Bonwick@Sun.COM  */
120810922SJeff.Bonwick@Sun.COM static int
ztest_replay_create(ztest_ds_t * zd,lr_create_t * lr,boolean_t byteswap)120910922SJeff.Bonwick@Sun.COM ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
121010922SJeff.Bonwick@Sun.COM {
121110922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
121210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
121310922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
121410922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
1215789Sahrens 	dmu_tx_t *tx;
121610922SJeff.Bonwick@Sun.COM 	uint64_t txg;
121710922SJeff.Bonwick@Sun.COM 	int error = 0;
1218789Sahrens 
1219789Sahrens 	if (byteswap)
1220789Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
1221789Sahrens 
122210922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
122310922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
122410922SJeff.Bonwick@Sun.COM 
1225789Sahrens 	tx = dmu_tx_create(os);
122610922SJeff.Bonwick@Sun.COM 
122710922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
122810922SJeff.Bonwick@Sun.COM 
122910922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
123010922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
123110922SJeff.Bonwick@Sun.COM 	} else {
123210922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
123310922SJeff.Bonwick@Sun.COM 	}
123410922SJeff.Bonwick@Sun.COM 
123510922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
123610922SJeff.Bonwick@Sun.COM 	if (txg == 0)
123710922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
123810922SJeff.Bonwick@Sun.COM 
123910922SJeff.Bonwick@Sun.COM 	ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
124010922SJeff.Bonwick@Sun.COM 
124110922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
124210922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
124310922SJeff.Bonwick@Sun.COM 			lr->lr_foid = zap_create(os,
124410922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124510922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
124610922SJeff.Bonwick@Sun.COM 		} else {
124710922SJeff.Bonwick@Sun.COM 			error = zap_create_claim(os, lr->lr_foid,
124810922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, lr->lrz_bonustype,
124910922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125010922SJeff.Bonwick@Sun.COM 		}
125110922SJeff.Bonwick@Sun.COM 	} else {
125210922SJeff.Bonwick@Sun.COM 		if (lr->lr_foid == 0) {
125310922SJeff.Bonwick@Sun.COM 			lr->lr_foid = dmu_object_alloc(os,
125410922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125510922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
125610922SJeff.Bonwick@Sun.COM 		} else {
125710922SJeff.Bonwick@Sun.COM 			error = dmu_object_claim(os, lr->lr_foid,
125810922SJeff.Bonwick@Sun.COM 			    lr->lrz_type, 0, lr->lrz_bonustype,
125910922SJeff.Bonwick@Sun.COM 			    lr->lrz_bonuslen, tx);
126010922SJeff.Bonwick@Sun.COM 		}
126110922SJeff.Bonwick@Sun.COM 	}
126210922SJeff.Bonwick@Sun.COM 
1263789Sahrens 	if (error) {
126410922SJeff.Bonwick@Sun.COM 		ASSERT3U(error, ==, EEXIST);
126510922SJeff.Bonwick@Sun.COM 		ASSERT(zd->zd_zilog->zl_replay);
126610922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
1267789Sahrens 		return (error);
1268789Sahrens 	}
1269789Sahrens 
127010922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_foid != 0);
127110922SJeff.Bonwick@Sun.COM 
127210922SJeff.Bonwick@Sun.COM 	if (lr->lrz_type != DMU_OT_ZAP_OTHER)
127310922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
127410922SJeff.Bonwick@Sun.COM 		    lr->lrz_blocksize, lr->lrz_ibshift, tx));
127510922SJeff.Bonwick@Sun.COM 
127610922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
127710922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
127810922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
127910922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
128010922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
128110922SJeff.Bonwick@Sun.COM 
128210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
128310922SJeff.Bonwick@Sun.COM 	    &lr->lr_foid, tx));
128410922SJeff.Bonwick@Sun.COM 
128510922SJeff.Bonwick@Sun.COM 	(void) ztest_log_create(zd, tx, lr);
128610922SJeff.Bonwick@Sun.COM 
128710922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
128810922SJeff.Bonwick@Sun.COM 
128910922SJeff.Bonwick@Sun.COM 	return (0);
129010922SJeff.Bonwick@Sun.COM }
129110922SJeff.Bonwick@Sun.COM 
129210922SJeff.Bonwick@Sun.COM static int
ztest_replay_remove(ztest_ds_t * zd,lr_remove_t * lr,boolean_t byteswap)129310922SJeff.Bonwick@Sun.COM ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
129410922SJeff.Bonwick@Sun.COM {
129510922SJeff.Bonwick@Sun.COM 	char *name = (void *)(lr + 1);		/* name follows lr */
129610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
129710922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
129810922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
129910922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
130010922SJeff.Bonwick@Sun.COM 
130110922SJeff.Bonwick@Sun.COM 	if (byteswap)
130210922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
130310922SJeff.Bonwick@Sun.COM 
130410922SJeff.Bonwick@Sun.COM 	ASSERT(lr->lr_doid == ZTEST_DIROBJ);
130510922SJeff.Bonwick@Sun.COM 	ASSERT(name[0] != '\0');
130610922SJeff.Bonwick@Sun.COM 
130710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
130810922SJeff.Bonwick@Sun.COM 	    zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
130910922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
131010922SJeff.Bonwick@Sun.COM 
131110922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_WRITER);
131210922SJeff.Bonwick@Sun.COM 
131310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
131410922SJeff.Bonwick@Sun.COM 
131510922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
131610922SJeff.Bonwick@Sun.COM 
131710922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
131810922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
131910922SJeff.Bonwick@Sun.COM 
132010922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
132110922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
132210922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
132310922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
132410922SJeff.Bonwick@Sun.COM 	}
132510922SJeff.Bonwick@Sun.COM 
132610922SJeff.Bonwick@Sun.COM 	if (doi.doi_type == DMU_OT_ZAP_OTHER) {
132710922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_destroy(os, object, tx));
132810922SJeff.Bonwick@Sun.COM 	} else {
132910922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, dmu_object_free(os, object, tx));
133010922SJeff.Bonwick@Sun.COM 	}
133110922SJeff.Bonwick@Sun.COM 
133210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
133310922SJeff.Bonwick@Sun.COM 
133412699SNeil.Perrin@Sun.COM 	(void) ztest_log_remove(zd, tx, lr, object);
133510922SJeff.Bonwick@Sun.COM 
1336789Sahrens 	dmu_tx_commit(tx);
1337789Sahrens 
133810922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
133910922SJeff.Bonwick@Sun.COM 
134010922SJeff.Bonwick@Sun.COM 	return (0);
134110922SJeff.Bonwick@Sun.COM }
134210922SJeff.Bonwick@Sun.COM 
134310922SJeff.Bonwick@Sun.COM static int
ztest_replay_write(ztest_ds_t * zd,lr_write_t * lr,boolean_t byteswap)134410922SJeff.Bonwick@Sun.COM ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
134510922SJeff.Bonwick@Sun.COM {
134610922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
134710922SJeff.Bonwick@Sun.COM 	void *data = lr + 1;			/* data follows lr */
134810922SJeff.Bonwick@Sun.COM 	uint64_t offset, length;
134910922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bt = data;
135010922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
135110922SJeff.Bonwick@Sun.COM 	uint64_t gen, txg, lrtxg, crtxg;
135210922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
135310922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
135410922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
135510922SJeff.Bonwick@Sun.COM 	arc_buf_t *abuf = NULL;
135610922SJeff.Bonwick@Sun.COM 	rl_t *rl;
135710922SJeff.Bonwick@Sun.COM 
135810922SJeff.Bonwick@Sun.COM 	if (byteswap)
135910922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
136010922SJeff.Bonwick@Sun.COM 
136110922SJeff.Bonwick@Sun.COM 	offset = lr->lr_offset;
136210922SJeff.Bonwick@Sun.COM 	length = lr->lr_length;
136310922SJeff.Bonwick@Sun.COM 
136410922SJeff.Bonwick@Sun.COM 	/* If it's a dmu_sync() block, write the whole block */
136510922SJeff.Bonwick@Sun.COM 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
136610922SJeff.Bonwick@Sun.COM 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
136710922SJeff.Bonwick@Sun.COM 		if (length < blocksize) {
136810922SJeff.Bonwick@Sun.COM 			offset -= offset % blocksize;
136910922SJeff.Bonwick@Sun.COM 			length = blocksize;
137010922SJeff.Bonwick@Sun.COM 		}
137110922SJeff.Bonwick@Sun.COM 	}
137210922SJeff.Bonwick@Sun.COM 
137310922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic == BSWAP_64(BT_MAGIC))
137410922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(bt, sizeof (*bt));
137510922SJeff.Bonwick@Sun.COM 
137610922SJeff.Bonwick@Sun.COM 	if (bt->bt_magic != BT_MAGIC)
137710922SJeff.Bonwick@Sun.COM 		bt = NULL;
137810922SJeff.Bonwick@Sun.COM 
137910922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
138010922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
138110922SJeff.Bonwick@Sun.COM 
138210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
138310922SJeff.Bonwick@Sun.COM 
138410922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
138510922SJeff.Bonwick@Sun.COM 
138610922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
138710922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
138810922SJeff.Bonwick@Sun.COM 	gen = bbt->bt_gen;
138910922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
139010922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
139110922SJeff.Bonwick@Sun.COM 
139210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
139310922SJeff.Bonwick@Sun.COM 
139410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
139510922SJeff.Bonwick@Sun.COM 
139610922SJeff.Bonwick@Sun.COM 	if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
139710922SJeff.Bonwick@Sun.COM 	    P2PHASE(offset, length) == 0)
139810922SJeff.Bonwick@Sun.COM 		abuf = dmu_request_arcbuf(db, length);
139910922SJeff.Bonwick@Sun.COM 
140010922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
140110922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
140210922SJeff.Bonwick@Sun.COM 		if (abuf != NULL)
140310922SJeff.Bonwick@Sun.COM 			dmu_return_arcbuf(abuf);
140410922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
140510922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
140610922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
140710922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
140810922SJeff.Bonwick@Sun.COM 	}
140910922SJeff.Bonwick@Sun.COM 
141010922SJeff.Bonwick@Sun.COM 	if (bt != NULL) {
141110922SJeff.Bonwick@Sun.COM 		/*
141210922SJeff.Bonwick@Sun.COM 		 * Usually, verify the old data before writing new data --
141310922SJeff.Bonwick@Sun.COM 		 * but not always, because we also want to verify correct
141410922SJeff.Bonwick@Sun.COM 		 * behavior when the data was not recently read into cache.
141510922SJeff.Bonwick@Sun.COM 		 */
141610922SJeff.Bonwick@Sun.COM 		ASSERT(offset % doi.doi_data_block_size == 0);
141710922SJeff.Bonwick@Sun.COM 		if (ztest_random(4) != 0) {
141810922SJeff.Bonwick@Sun.COM 			int prefetch = ztest_random(2) ?
141910922SJeff.Bonwick@Sun.COM 			    DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
142010922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t rbt;
142110922SJeff.Bonwick@Sun.COM 
142210922SJeff.Bonwick@Sun.COM 			VERIFY(dmu_read(os, lr->lr_foid, offset,
142310922SJeff.Bonwick@Sun.COM 			    sizeof (rbt), &rbt, prefetch) == 0);
142410922SJeff.Bonwick@Sun.COM 			if (rbt.bt_magic == BT_MAGIC) {
142510922SJeff.Bonwick@Sun.COM 				ztest_bt_verify(&rbt, os, lr->lr_foid,
142610922SJeff.Bonwick@Sun.COM 				    offset, gen, txg, crtxg);
142710922SJeff.Bonwick@Sun.COM 			}
142810922SJeff.Bonwick@Sun.COM 		}
142910922SJeff.Bonwick@Sun.COM 
143010922SJeff.Bonwick@Sun.COM 		/*
143110922SJeff.Bonwick@Sun.COM 		 * Writes can appear to be newer than the bonus buffer because
143210922SJeff.Bonwick@Sun.COM 		 * the ztest_get_data() callback does a dmu_read() of the
143310922SJeff.Bonwick@Sun.COM 		 * open-context data, which may be different than the data
143410922SJeff.Bonwick@Sun.COM 		 * as it was when the write was generated.
143510922SJeff.Bonwick@Sun.COM 		 */
143610922SJeff.Bonwick@Sun.COM 		if (zd->zd_zilog->zl_replay) {
143710922SJeff.Bonwick@Sun.COM 			ztest_bt_verify(bt, os, lr->lr_foid, offset,
143810922SJeff.Bonwick@Sun.COM 			    MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
143910922SJeff.Bonwick@Sun.COM 			    bt->bt_crtxg);
144010922SJeff.Bonwick@Sun.COM 		}
144110922SJeff.Bonwick@Sun.COM 
144210922SJeff.Bonwick@Sun.COM 		/*
144310922SJeff.Bonwick@Sun.COM 		 * Set the bt's gen/txg to the bonus buffer's gen/txg
144410922SJeff.Bonwick@Sun.COM 		 * so that all of the usual ASSERTs will work.
144510922SJeff.Bonwick@Sun.COM 		 */
144610922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
144710922SJeff.Bonwick@Sun.COM 	}
144810922SJeff.Bonwick@Sun.COM 
144910922SJeff.Bonwick@Sun.COM 	if (abuf == NULL) {
145010922SJeff.Bonwick@Sun.COM 		dmu_write(os, lr->lr_foid, offset, length, data, tx);
145110922SJeff.Bonwick@Sun.COM 	} else {
145210922SJeff.Bonwick@Sun.COM 		bcopy(data, abuf->b_data, length);
145310922SJeff.Bonwick@Sun.COM 		dmu_assign_arcbuf(db, offset, abuf, tx);
145410922SJeff.Bonwick@Sun.COM 	}
145510922SJeff.Bonwick@Sun.COM 
145610922SJeff.Bonwick@Sun.COM 	(void) ztest_log_write(zd, tx, lr);
145710922SJeff.Bonwick@Sun.COM 
145810922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
145910922SJeff.Bonwick@Sun.COM 
146010922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
146110922SJeff.Bonwick@Sun.COM 
146210922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
146310922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
146410922SJeff.Bonwick@Sun.COM 
146510922SJeff.Bonwick@Sun.COM 	return (0);
146610922SJeff.Bonwick@Sun.COM }
146710922SJeff.Bonwick@Sun.COM 
146810922SJeff.Bonwick@Sun.COM static int
ztest_replay_truncate(ztest_ds_t * zd,lr_truncate_t * lr,boolean_t byteswap)146910922SJeff.Bonwick@Sun.COM ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
147010922SJeff.Bonwick@Sun.COM {
147110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
147210922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
147310922SJeff.Bonwick@Sun.COM 	uint64_t txg;
147410922SJeff.Bonwick@Sun.COM 	rl_t *rl;
147510922SJeff.Bonwick@Sun.COM 
147610922SJeff.Bonwick@Sun.COM 	if (byteswap)
147710922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
147810922SJeff.Bonwick@Sun.COM 
147910922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_READER);
148010922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
148110922SJeff.Bonwick@Sun.COM 	    RL_WRITER);
148210922SJeff.Bonwick@Sun.COM 
148310922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
148410922SJeff.Bonwick@Sun.COM 
148510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
148610922SJeff.Bonwick@Sun.COM 
148710922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
148810922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
148910922SJeff.Bonwick@Sun.COM 		ztest_range_unlock(rl);
149010922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
149110922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
149210922SJeff.Bonwick@Sun.COM 	}
149310922SJeff.Bonwick@Sun.COM 
149410922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
149510922SJeff.Bonwick@Sun.COM 	    lr->lr_length, tx) == 0);
149610922SJeff.Bonwick@Sun.COM 
149710922SJeff.Bonwick@Sun.COM 	(void) ztest_log_truncate(zd, tx, lr);
149810922SJeff.Bonwick@Sun.COM 
149910922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
150010922SJeff.Bonwick@Sun.COM 
150110922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
150210922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
150310922SJeff.Bonwick@Sun.COM 
150410922SJeff.Bonwick@Sun.COM 	return (0);
150510922SJeff.Bonwick@Sun.COM }
150610922SJeff.Bonwick@Sun.COM 
150710922SJeff.Bonwick@Sun.COM static int
ztest_replay_setattr(ztest_ds_t * zd,lr_setattr_t * lr,boolean_t byteswap)150810922SJeff.Bonwick@Sun.COM ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
150910922SJeff.Bonwick@Sun.COM {
151010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
151110922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
151210922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
151310922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t *bbt;
151410922SJeff.Bonwick@Sun.COM 	uint64_t txg, lrtxg, crtxg;
151510922SJeff.Bonwick@Sun.COM 
151610922SJeff.Bonwick@Sun.COM 	if (byteswap)
151710922SJeff.Bonwick@Sun.COM 		byteswap_uint64_array(lr, sizeof (*lr));
151810922SJeff.Bonwick@Sun.COM 
151910922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
152010922SJeff.Bonwick@Sun.COM 
152110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
152210922SJeff.Bonwick@Sun.COM 
152310922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
152410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_bonus(tx, lr->lr_foid);
152510922SJeff.Bonwick@Sun.COM 
152610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
152710922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
152810922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
152910922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, lr->lr_foid);
153010922SJeff.Bonwick@Sun.COM 		return (ENOSPC);
153110922SJeff.Bonwick@Sun.COM 	}
153210922SJeff.Bonwick@Sun.COM 
153310922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
153410922SJeff.Bonwick@Sun.COM 	ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
153510922SJeff.Bonwick@Sun.COM 	crtxg = bbt->bt_crtxg;
153610922SJeff.Bonwick@Sun.COM 	lrtxg = lr->lr_common.lrc_txg;
153710922SJeff.Bonwick@Sun.COM 
153810922SJeff.Bonwick@Sun.COM 	if (zd->zd_zilog->zl_replay) {
153910922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_size != 0);
154010922SJeff.Bonwick@Sun.COM 		ASSERT(lr->lr_mode != 0);
154110922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg != 0);
154210922SJeff.Bonwick@Sun.COM 	} else {
154310922SJeff.Bonwick@Sun.COM 		/*
154410922SJeff.Bonwick@Sun.COM 		 * Randomly change the size and increment the generation.
154510922SJeff.Bonwick@Sun.COM 		 */
154610922SJeff.Bonwick@Sun.COM 		lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
154710922SJeff.Bonwick@Sun.COM 		    sizeof (*bbt);
154810922SJeff.Bonwick@Sun.COM 		lr->lr_mode = bbt->bt_gen + 1;
154910922SJeff.Bonwick@Sun.COM 		ASSERT(lrtxg == 0);
155010922SJeff.Bonwick@Sun.COM 	}
155110922SJeff.Bonwick@Sun.COM 
155210922SJeff.Bonwick@Sun.COM 	/*
155310922SJeff.Bonwick@Sun.COM 	 * Verify that the current bonus buffer is not newer than our txg.
155410922SJeff.Bonwick@Sun.COM 	 */
155510922SJeff.Bonwick@Sun.COM 	ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
155610922SJeff.Bonwick@Sun.COM 	    MAX(txg, lrtxg), crtxg);
155710922SJeff.Bonwick@Sun.COM 
155810922SJeff.Bonwick@Sun.COM 	dmu_buf_will_dirty(db, tx);
155910922SJeff.Bonwick@Sun.COM 
156010922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
156110922SJeff.Bonwick@Sun.COM 	ASSERT3U(lr->lr_size, <=, db->db_size);
156210922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
156310922SJeff.Bonwick@Sun.COM 	bbt = ztest_bt_bonus(db);
156410922SJeff.Bonwick@Sun.COM 
156510922SJeff.Bonwick@Sun.COM 	ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
156610922SJeff.Bonwick@Sun.COM 
156710922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
156810922SJeff.Bonwick@Sun.COM 
156910922SJeff.Bonwick@Sun.COM 	(void) ztest_log_setattr(zd, tx, lr);
157010922SJeff.Bonwick@Sun.COM 
157110922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
157210922SJeff.Bonwick@Sun.COM 
157310922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, lr->lr_foid);
157410922SJeff.Bonwick@Sun.COM 
157510922SJeff.Bonwick@Sun.COM 	return (0);
1576789Sahrens }
1577789Sahrens 
1578789Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1579789Sahrens 	NULL,			/* 0 no such transaction type */
1580789Sahrens 	ztest_replay_create,	/* TX_CREATE */
1581789Sahrens 	NULL,			/* TX_MKDIR */
1582789Sahrens 	NULL,			/* TX_MKXATTR */
1583789Sahrens 	NULL,			/* TX_SYMLINK */
1584789Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
1585789Sahrens 	NULL,			/* TX_RMDIR */
1586789Sahrens 	NULL,			/* TX_LINK */
1587789Sahrens 	NULL,			/* TX_RENAME */
158810922SJeff.Bonwick@Sun.COM 	ztest_replay_write,	/* TX_WRITE */
158910922SJeff.Bonwick@Sun.COM 	ztest_replay_truncate,	/* TX_TRUNCATE */
159010922SJeff.Bonwick@Sun.COM 	ztest_replay_setattr,	/* TX_SETATTR */
1591789Sahrens 	NULL,			/* TX_ACL */
159210800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL */
159310800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ATTR */
159410800SNeil.Perrin@Sun.COM 	NULL,			/* TX_CREATE_ACL_ATTR */
159510800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL */
159610800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ATTR */
159710800SNeil.Perrin@Sun.COM 	NULL,			/* TX_MKDIR_ACL_ATTR */
159810800SNeil.Perrin@Sun.COM 	NULL,			/* TX_WRITE2 */
1599789Sahrens };
1600789Sahrens 
1601789Sahrens /*
160210922SJeff.Bonwick@Sun.COM  * ZIL get_data callbacks
160310922SJeff.Bonwick@Sun.COM  */
160410922SJeff.Bonwick@Sun.COM 
160510922SJeff.Bonwick@Sun.COM static void
ztest_get_done(zgd_t * zgd,int error)160610922SJeff.Bonwick@Sun.COM ztest_get_done(zgd_t *zgd, int error)
160710922SJeff.Bonwick@Sun.COM {
160810922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = zgd->zgd_private;
160910922SJeff.Bonwick@Sun.COM 	uint64_t object = zgd->zgd_rl->rl_object;
161010922SJeff.Bonwick@Sun.COM 
161110922SJeff.Bonwick@Sun.COM 	if (zgd->zgd_db)
161210922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(zgd->zgd_db, zgd);
161310922SJeff.Bonwick@Sun.COM 
161410922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(zgd->zgd_rl);
161510922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
161610922SJeff.Bonwick@Sun.COM 
161710922SJeff.Bonwick@Sun.COM 	if (error == 0 && zgd->zgd_bp)
161810922SJeff.Bonwick@Sun.COM 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
161910922SJeff.Bonwick@Sun.COM 
162010922SJeff.Bonwick@Sun.COM 	umem_free(zgd, sizeof (*zgd));
162110922SJeff.Bonwick@Sun.COM }
162210922SJeff.Bonwick@Sun.COM 
162310922SJeff.Bonwick@Sun.COM static int
ztest_get_data(void * arg,lr_write_t * lr,char * buf,zio_t * zio)162410922SJeff.Bonwick@Sun.COM ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
162510922SJeff.Bonwick@Sun.COM {
162610922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = arg;
162710922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
162810922SJeff.Bonwick@Sun.COM 	uint64_t object = lr->lr_foid;
162910922SJeff.Bonwick@Sun.COM 	uint64_t offset = lr->lr_offset;
163010922SJeff.Bonwick@Sun.COM 	uint64_t size = lr->lr_length;
163110922SJeff.Bonwick@Sun.COM 	blkptr_t *bp = &lr->lr_blkptr;
163210922SJeff.Bonwick@Sun.COM 	uint64_t txg = lr->lr_common.lrc_txg;
163310922SJeff.Bonwick@Sun.COM 	uint64_t crtxg;
163410922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
163510922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
163610922SJeff.Bonwick@Sun.COM 	zgd_t *zgd;
163710922SJeff.Bonwick@Sun.COM 	int error;
163810922SJeff.Bonwick@Sun.COM 
163910922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
164010922SJeff.Bonwick@Sun.COM 	error = dmu_bonus_hold(os, object, FTAG, &db);
164110922SJeff.Bonwick@Sun.COM 	if (error) {
164210922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
164310922SJeff.Bonwick@Sun.COM 		return (error);
164410922SJeff.Bonwick@Sun.COM 	}
164510922SJeff.Bonwick@Sun.COM 
164610922SJeff.Bonwick@Sun.COM 	crtxg = ztest_bt_bonus(db)->bt_crtxg;
164710922SJeff.Bonwick@Sun.COM 
164810922SJeff.Bonwick@Sun.COM 	if (crtxg == 0 || crtxg > txg) {
164910922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
165010922SJeff.Bonwick@Sun.COM 		ztest_object_unlock(zd, object);
165110922SJeff.Bonwick@Sun.COM 		return (ENOENT);
165210922SJeff.Bonwick@Sun.COM 	}
165310922SJeff.Bonwick@Sun.COM 
165410922SJeff.Bonwick@Sun.COM 	dmu_object_info_from_db(db, &doi);
165510922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
165610922SJeff.Bonwick@Sun.COM 	db = NULL;
165710922SJeff.Bonwick@Sun.COM 
165810922SJeff.Bonwick@Sun.COM 	zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
165910922SJeff.Bonwick@Sun.COM 	zgd->zgd_zilog = zd->zd_zilog;
166010922SJeff.Bonwick@Sun.COM 	zgd->zgd_private = zd;
166110922SJeff.Bonwick@Sun.COM 
166210922SJeff.Bonwick@Sun.COM 	if (buf != NULL) {	/* immediate write */
166310922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
166410922SJeff.Bonwick@Sun.COM 		    RL_READER);
166510922SJeff.Bonwick@Sun.COM 
166610922SJeff.Bonwick@Sun.COM 		error = dmu_read(os, object, offset, size, buf,
166710922SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
166810922SJeff.Bonwick@Sun.COM 		ASSERT(error == 0);
166910922SJeff.Bonwick@Sun.COM 	} else {
167010922SJeff.Bonwick@Sun.COM 		size = doi.doi_data_block_size;
167110945SJeff.Bonwick@Sun.COM 		if (ISP2(size)) {
167210922SJeff.Bonwick@Sun.COM 			offset = P2ALIGN(offset, size);
167310945SJeff.Bonwick@Sun.COM 		} else {
167410945SJeff.Bonwick@Sun.COM 			ASSERT(offset < size);
167510945SJeff.Bonwick@Sun.COM 			offset = 0;
167610945SJeff.Bonwick@Sun.COM 		}
167710922SJeff.Bonwick@Sun.COM 
167810922SJeff.Bonwick@Sun.COM 		zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
167910922SJeff.Bonwick@Sun.COM 		    RL_READER);
168010922SJeff.Bonwick@Sun.COM 
168112285SJeff.Bonwick@Sun.COM 		error = dmu_buf_hold(os, object, offset, zgd, &db,
168212285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH);
168310922SJeff.Bonwick@Sun.COM 
168410922SJeff.Bonwick@Sun.COM 		if (error == 0) {
168510922SJeff.Bonwick@Sun.COM 			zgd->zgd_db = db;
168610922SJeff.Bonwick@Sun.COM 			zgd->zgd_bp = bp;
168710922SJeff.Bonwick@Sun.COM 
168810922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_offset == offset);
168910922SJeff.Bonwick@Sun.COM 			ASSERT(db->db_size == size);
169010922SJeff.Bonwick@Sun.COM 
169110922SJeff.Bonwick@Sun.COM 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
169210922SJeff.Bonwick@Sun.COM 			    ztest_get_done, zgd);
169310922SJeff.Bonwick@Sun.COM 
169410922SJeff.Bonwick@Sun.COM 			if (error == 0)
169510922SJeff.Bonwick@Sun.COM 				return (0);
169610922SJeff.Bonwick@Sun.COM 		}
169710922SJeff.Bonwick@Sun.COM 	}
169810922SJeff.Bonwick@Sun.COM 
169910922SJeff.Bonwick@Sun.COM 	ztest_get_done(zgd, error);
170010922SJeff.Bonwick@Sun.COM 
170110922SJeff.Bonwick@Sun.COM 	return (error);
170210922SJeff.Bonwick@Sun.COM }
170310922SJeff.Bonwick@Sun.COM 
170410922SJeff.Bonwick@Sun.COM static void *
ztest_lr_alloc(size_t lrsize,char * name)170510922SJeff.Bonwick@Sun.COM ztest_lr_alloc(size_t lrsize, char *name)
170610922SJeff.Bonwick@Sun.COM {
170710922SJeff.Bonwick@Sun.COM 	char *lr;
170810922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
170910922SJeff.Bonwick@Sun.COM 
171010922SJeff.Bonwick@Sun.COM 	lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
171110922SJeff.Bonwick@Sun.COM 
171210922SJeff.Bonwick@Sun.COM 	if (name)
171310922SJeff.Bonwick@Sun.COM 		bcopy(name, lr + lrsize, namesize);
171410922SJeff.Bonwick@Sun.COM 
171510922SJeff.Bonwick@Sun.COM 	return (lr);
171610922SJeff.Bonwick@Sun.COM }
171710922SJeff.Bonwick@Sun.COM 
171810922SJeff.Bonwick@Sun.COM void
ztest_lr_free(void * lr,size_t lrsize,char * name)171910922SJeff.Bonwick@Sun.COM ztest_lr_free(void *lr, size_t lrsize, char *name)
172010922SJeff.Bonwick@Sun.COM {
172110922SJeff.Bonwick@Sun.COM 	size_t namesize = name ? strlen(name) + 1 : 0;
172210922SJeff.Bonwick@Sun.COM 
172310922SJeff.Bonwick@Sun.COM 	umem_free(lr, lrsize + namesize);
172410922SJeff.Bonwick@Sun.COM }
172510922SJeff.Bonwick@Sun.COM 
172610922SJeff.Bonwick@Sun.COM /*
172710922SJeff.Bonwick@Sun.COM  * Lookup a bunch of objects.  Returns the number of objects not found.
172810922SJeff.Bonwick@Sun.COM  */
172910922SJeff.Bonwick@Sun.COM static int
ztest_lookup(ztest_ds_t * zd,ztest_od_t * od,int count)173010922SJeff.Bonwick@Sun.COM ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
173110922SJeff.Bonwick@Sun.COM {
173210922SJeff.Bonwick@Sun.COM 	int missing = 0;
173310922SJeff.Bonwick@Sun.COM 	int error;
173410922SJeff.Bonwick@Sun.COM 
173510922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
173610922SJeff.Bonwick@Sun.COM 
173710922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
173810922SJeff.Bonwick@Sun.COM 		od->od_object = 0;
173910922SJeff.Bonwick@Sun.COM 		error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
174010922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &od->od_object);
174110922SJeff.Bonwick@Sun.COM 		if (error) {
174210922SJeff.Bonwick@Sun.COM 			ASSERT(error == ENOENT);
174310922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object == 0);
174410922SJeff.Bonwick@Sun.COM 			missing++;
174510922SJeff.Bonwick@Sun.COM 		} else {
174610922SJeff.Bonwick@Sun.COM 			dmu_buf_t *db;
174710922SJeff.Bonwick@Sun.COM 			ztest_block_tag_t *bbt;
174810922SJeff.Bonwick@Sun.COM 			dmu_object_info_t doi;
174910922SJeff.Bonwick@Sun.COM 
175010922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
175110922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);	/* there should be no gaps */
175210922SJeff.Bonwick@Sun.COM 
175310922SJeff.Bonwick@Sun.COM 			ztest_object_lock(zd, od->od_object, RL_READER);
175410922SJeff.Bonwick@Sun.COM 			VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
175510922SJeff.Bonwick@Sun.COM 			    od->od_object, FTAG, &db));
175610922SJeff.Bonwick@Sun.COM 			dmu_object_info_from_db(db, &doi);
175710922SJeff.Bonwick@Sun.COM 			bbt = ztest_bt_bonus(db);
175810922SJeff.Bonwick@Sun.COM 			ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
175910922SJeff.Bonwick@Sun.COM 			od->od_type = doi.doi_type;
176010922SJeff.Bonwick@Sun.COM 			od->od_blocksize = doi.doi_data_block_size;
176110922SJeff.Bonwick@Sun.COM 			od->od_gen = bbt->bt_gen;
176210922SJeff.Bonwick@Sun.COM 			dmu_buf_rele(db, FTAG);
176310922SJeff.Bonwick@Sun.COM 			ztest_object_unlock(zd, od->od_object);
176410922SJeff.Bonwick@Sun.COM 		}
176510922SJeff.Bonwick@Sun.COM 	}
176610922SJeff.Bonwick@Sun.COM 
176710922SJeff.Bonwick@Sun.COM 	return (missing);
176810922SJeff.Bonwick@Sun.COM }
176910922SJeff.Bonwick@Sun.COM 
177010922SJeff.Bonwick@Sun.COM static int
ztest_create(ztest_ds_t * zd,ztest_od_t * od,int count)177110922SJeff.Bonwick@Sun.COM ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
177210922SJeff.Bonwick@Sun.COM {
177310922SJeff.Bonwick@Sun.COM 	int missing = 0;
177410922SJeff.Bonwick@Sun.COM 
177510922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
177610922SJeff.Bonwick@Sun.COM 
177710922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < count; i++, od++) {
177810922SJeff.Bonwick@Sun.COM 		if (missing) {
177910922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
178010922SJeff.Bonwick@Sun.COM 			missing++;
178110922SJeff.Bonwick@Sun.COM 			continue;
178210922SJeff.Bonwick@Sun.COM 		}
178310922SJeff.Bonwick@Sun.COM 
178410922SJeff.Bonwick@Sun.COM 		lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
178510922SJeff.Bonwick@Sun.COM 
178610922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
178710922SJeff.Bonwick@Sun.COM 		lr->lr_foid = 0;	/* 0 to allocate, > 0 to claim */
178810922SJeff.Bonwick@Sun.COM 		lr->lrz_type = od->od_crtype;
178910922SJeff.Bonwick@Sun.COM 		lr->lrz_blocksize = od->od_crblocksize;
179010922SJeff.Bonwick@Sun.COM 		lr->lrz_ibshift = ztest_random_ibshift();
179110922SJeff.Bonwick@Sun.COM 		lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
179210922SJeff.Bonwick@Sun.COM 		lr->lrz_bonuslen = dmu_bonus_max();
179310922SJeff.Bonwick@Sun.COM 		lr->lr_gen = od->od_crgen;
179410922SJeff.Bonwick@Sun.COM 		lr->lr_crtime[0] = time(NULL);
179510922SJeff.Bonwick@Sun.COM 
179610922SJeff.Bonwick@Sun.COM 		if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
179710922SJeff.Bonwick@Sun.COM 			ASSERT(missing == 0);
179810922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
179910922SJeff.Bonwick@Sun.COM 			missing++;
180010922SJeff.Bonwick@Sun.COM 		} else {
180110922SJeff.Bonwick@Sun.COM 			od->od_object = lr->lr_foid;
180210922SJeff.Bonwick@Sun.COM 			od->od_type = od->od_crtype;
180310922SJeff.Bonwick@Sun.COM 			od->od_blocksize = od->od_crblocksize;
180410922SJeff.Bonwick@Sun.COM 			od->od_gen = od->od_crgen;
180510922SJeff.Bonwick@Sun.COM 			ASSERT(od->od_object != 0);
180610922SJeff.Bonwick@Sun.COM 		}
180710922SJeff.Bonwick@Sun.COM 
180810922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
180910922SJeff.Bonwick@Sun.COM 	}
181010922SJeff.Bonwick@Sun.COM 
181110922SJeff.Bonwick@Sun.COM 	return (missing);
181210922SJeff.Bonwick@Sun.COM }
181310922SJeff.Bonwick@Sun.COM 
181410922SJeff.Bonwick@Sun.COM static int
ztest_remove(ztest_ds_t * zd,ztest_od_t * od,int count)181510922SJeff.Bonwick@Sun.COM ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
181610922SJeff.Bonwick@Sun.COM {
181710922SJeff.Bonwick@Sun.COM 	int missing = 0;
181810922SJeff.Bonwick@Sun.COM 	int error;
181910922SJeff.Bonwick@Sun.COM 
182010922SJeff.Bonwick@Sun.COM 	ASSERT(_mutex_held(&zd->zd_dirobj_lock));
182110922SJeff.Bonwick@Sun.COM 
182210922SJeff.Bonwick@Sun.COM 	od += count - 1;
182310922SJeff.Bonwick@Sun.COM 
182410922SJeff.Bonwick@Sun.COM 	for (int i = count - 1; i >= 0; i--, od--) {
182510922SJeff.Bonwick@Sun.COM 		if (missing) {
182610922SJeff.Bonwick@Sun.COM 			missing++;
182710922SJeff.Bonwick@Sun.COM 			continue;
182810922SJeff.Bonwick@Sun.COM 		}
182910922SJeff.Bonwick@Sun.COM 
183010922SJeff.Bonwick@Sun.COM 		if (od->od_object == 0)
183110922SJeff.Bonwick@Sun.COM 			continue;
183210922SJeff.Bonwick@Sun.COM 
183310922SJeff.Bonwick@Sun.COM 		lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
183410922SJeff.Bonwick@Sun.COM 
183510922SJeff.Bonwick@Sun.COM 		lr->lr_doid = od->od_dir;
183610922SJeff.Bonwick@Sun.COM 
183710922SJeff.Bonwick@Sun.COM 		if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
183810922SJeff.Bonwick@Sun.COM 			ASSERT3U(error, ==, ENOSPC);
183910922SJeff.Bonwick@Sun.COM 			missing++;
184010922SJeff.Bonwick@Sun.COM 		} else {
184110922SJeff.Bonwick@Sun.COM 			od->od_object = 0;
184210922SJeff.Bonwick@Sun.COM 		}
184310922SJeff.Bonwick@Sun.COM 		ztest_lr_free(lr, sizeof (*lr), od->od_name);
184410922SJeff.Bonwick@Sun.COM 	}
184510922SJeff.Bonwick@Sun.COM 
184610922SJeff.Bonwick@Sun.COM 	return (missing);
184710922SJeff.Bonwick@Sun.COM }
184810922SJeff.Bonwick@Sun.COM 
184910922SJeff.Bonwick@Sun.COM static int
ztest_write(ztest_ds_t * zd,uint64_t object,uint64_t offset,uint64_t size,void * data)185010922SJeff.Bonwick@Sun.COM ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
185110922SJeff.Bonwick@Sun.COM     void *data)
185210922SJeff.Bonwick@Sun.COM {
185310922SJeff.Bonwick@Sun.COM 	lr_write_t *lr;
185410922SJeff.Bonwick@Sun.COM 	int error;
185510922SJeff.Bonwick@Sun.COM 
185610922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
185710922SJeff.Bonwick@Sun.COM 
185810922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
185910922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
186010922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
186110922SJeff.Bonwick@Sun.COM 	lr->lr_blkoff = 0;
186210922SJeff.Bonwick@Sun.COM 	BP_ZERO(&lr->lr_blkptr);
186310922SJeff.Bonwick@Sun.COM 
186410922SJeff.Bonwick@Sun.COM 	bcopy(data, lr + 1, size);
186510922SJeff.Bonwick@Sun.COM 
186610922SJeff.Bonwick@Sun.COM 	error = ztest_replay_write(zd, lr, B_FALSE);
186710922SJeff.Bonwick@Sun.COM 
186810922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr) + size, NULL);
186910922SJeff.Bonwick@Sun.COM 
187010922SJeff.Bonwick@Sun.COM 	return (error);
187110922SJeff.Bonwick@Sun.COM }
187210922SJeff.Bonwick@Sun.COM 
187310922SJeff.Bonwick@Sun.COM static int
ztest_truncate(ztest_ds_t * zd,uint64_t object,uint64_t offset,uint64_t size)187410922SJeff.Bonwick@Sun.COM ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
187510922SJeff.Bonwick@Sun.COM {
187610922SJeff.Bonwick@Sun.COM 	lr_truncate_t *lr;
187710922SJeff.Bonwick@Sun.COM 	int error;
187810922SJeff.Bonwick@Sun.COM 
187910922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
188010922SJeff.Bonwick@Sun.COM 
188110922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
188210922SJeff.Bonwick@Sun.COM 	lr->lr_offset = offset;
188310922SJeff.Bonwick@Sun.COM 	lr->lr_length = size;
188410922SJeff.Bonwick@Sun.COM 
188510922SJeff.Bonwick@Sun.COM 	error = ztest_replay_truncate(zd, lr, B_FALSE);
188610922SJeff.Bonwick@Sun.COM 
188710922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
188810922SJeff.Bonwick@Sun.COM 
188910922SJeff.Bonwick@Sun.COM 	return (error);
189010922SJeff.Bonwick@Sun.COM }
189110922SJeff.Bonwick@Sun.COM 
189210922SJeff.Bonwick@Sun.COM static int
ztest_setattr(ztest_ds_t * zd,uint64_t object)189310922SJeff.Bonwick@Sun.COM ztest_setattr(ztest_ds_t *zd, uint64_t object)
189410922SJeff.Bonwick@Sun.COM {
189510922SJeff.Bonwick@Sun.COM 	lr_setattr_t *lr;
189610922SJeff.Bonwick@Sun.COM 	int error;
189710922SJeff.Bonwick@Sun.COM 
189810922SJeff.Bonwick@Sun.COM 	lr = ztest_lr_alloc(sizeof (*lr), NULL);
189910922SJeff.Bonwick@Sun.COM 
190010922SJeff.Bonwick@Sun.COM 	lr->lr_foid = object;
190110922SJeff.Bonwick@Sun.COM 	lr->lr_size = 0;
190210922SJeff.Bonwick@Sun.COM 	lr->lr_mode = 0;
190310922SJeff.Bonwick@Sun.COM 
190410922SJeff.Bonwick@Sun.COM 	error = ztest_replay_setattr(zd, lr, B_FALSE);
190510922SJeff.Bonwick@Sun.COM 
190610922SJeff.Bonwick@Sun.COM 	ztest_lr_free(lr, sizeof (*lr), NULL);
190710922SJeff.Bonwick@Sun.COM 
190810922SJeff.Bonwick@Sun.COM 	return (error);
190910922SJeff.Bonwick@Sun.COM }
191010922SJeff.Bonwick@Sun.COM 
191110922SJeff.Bonwick@Sun.COM static void
ztest_prealloc(ztest_ds_t * zd,uint64_t object,uint64_t offset,uint64_t size)191210922SJeff.Bonwick@Sun.COM ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
191310922SJeff.Bonwick@Sun.COM {
191410922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
191510922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
191610922SJeff.Bonwick@Sun.COM 	uint64_t txg;
191710922SJeff.Bonwick@Sun.COM 	rl_t *rl;
191810922SJeff.Bonwick@Sun.COM 
191910922SJeff.Bonwick@Sun.COM 	txg_wait_synced(dmu_objset_pool(os), 0);
192010922SJeff.Bonwick@Sun.COM 
192110922SJeff.Bonwick@Sun.COM 	ztest_object_lock(zd, object, RL_READER);
192210922SJeff.Bonwick@Sun.COM 	rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
192310922SJeff.Bonwick@Sun.COM 
192410922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
192510922SJeff.Bonwick@Sun.COM 
192610922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, offset, size);
192710922SJeff.Bonwick@Sun.COM 
192810922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
192910922SJeff.Bonwick@Sun.COM 
193010922SJeff.Bonwick@Sun.COM 	if (txg != 0) {
193110922SJeff.Bonwick@Sun.COM 		dmu_prealloc(os, object, offset, size, tx);
193210922SJeff.Bonwick@Sun.COM 		dmu_tx_commit(tx);
193310922SJeff.Bonwick@Sun.COM 		txg_wait_synced(dmu_objset_pool(os), txg);
193410922SJeff.Bonwick@Sun.COM 	} else {
193510922SJeff.Bonwick@Sun.COM 		(void) dmu_free_long_range(os, object, offset, size);
193610922SJeff.Bonwick@Sun.COM 	}
193710922SJeff.Bonwick@Sun.COM 
193810922SJeff.Bonwick@Sun.COM 	ztest_range_unlock(rl);
193910922SJeff.Bonwick@Sun.COM 	ztest_object_unlock(zd, object);
194010922SJeff.Bonwick@Sun.COM }
194110922SJeff.Bonwick@Sun.COM 
194210922SJeff.Bonwick@Sun.COM static void
ztest_io(ztest_ds_t * zd,uint64_t object,uint64_t offset)194310922SJeff.Bonwick@Sun.COM ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
194410922SJeff.Bonwick@Sun.COM {
194510922SJeff.Bonwick@Sun.COM 	ztest_block_tag_t wbt;
194610922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
194710922SJeff.Bonwick@Sun.COM 	enum ztest_io_type io_type;
194810922SJeff.Bonwick@Sun.COM 	uint64_t blocksize;
194910922SJeff.Bonwick@Sun.COM 	void *data;
195010922SJeff.Bonwick@Sun.COM 
195110922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
195210922SJeff.Bonwick@Sun.COM 	blocksize = doi.doi_data_block_size;
195310922SJeff.Bonwick@Sun.COM 	data = umem_alloc(blocksize, UMEM_NOFAIL);
195410922SJeff.Bonwick@Sun.COM 
195510922SJeff.Bonwick@Sun.COM 	/*
195610922SJeff.Bonwick@Sun.COM 	 * Pick an i/o type at random, biased toward writing block tags.
195710922SJeff.Bonwick@Sun.COM 	 */
195810922SJeff.Bonwick@Sun.COM 	io_type = ztest_random(ZTEST_IO_TYPES);
195910922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
196010922SJeff.Bonwick@Sun.COM 		io_type = ZTEST_IO_WRITE_TAG;
196110922SJeff.Bonwick@Sun.COM 
196210922SJeff.Bonwick@Sun.COM 	switch (io_type) {
196310922SJeff.Bonwick@Sun.COM 
196410922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_TAG:
196510922SJeff.Bonwick@Sun.COM 		ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
196610922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
196710922SJeff.Bonwick@Sun.COM 		break;
196810922SJeff.Bonwick@Sun.COM 
196910922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_PATTERN:
197010922SJeff.Bonwick@Sun.COM 		(void) memset(data, 'a' + (object + offset) % 5, blocksize);
197110922SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0) {
197210922SJeff.Bonwick@Sun.COM 			/*
197310922SJeff.Bonwick@Sun.COM 			 * Induce fletcher2 collisions to ensure that
197410922SJeff.Bonwick@Sun.COM 			 * zio_ddt_collision() detects and resolves them
197510922SJeff.Bonwick@Sun.COM 			 * when using fletcher2-verify for deduplication.
197610922SJeff.Bonwick@Sun.COM 			 */
197710922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[0] ^= 1ULL << 63;
197810922SJeff.Bonwick@Sun.COM 			((uint64_t *)data)[4] ^= 1ULL << 63;
197910922SJeff.Bonwick@Sun.COM 		}
198010922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
198110922SJeff.Bonwick@Sun.COM 		break;
198210922SJeff.Bonwick@Sun.COM 
198310922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_WRITE_ZEROES:
198410922SJeff.Bonwick@Sun.COM 		bzero(data, blocksize);
198510922SJeff.Bonwick@Sun.COM 		(void) ztest_write(zd, object, offset, blocksize, data);
198610922SJeff.Bonwick@Sun.COM 		break;
198710922SJeff.Bonwick@Sun.COM 
198810922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_TRUNCATE:
198910922SJeff.Bonwick@Sun.COM 		(void) ztest_truncate(zd, object, offset, blocksize);
199010922SJeff.Bonwick@Sun.COM 		break;
199110922SJeff.Bonwick@Sun.COM 
199210922SJeff.Bonwick@Sun.COM 	case ZTEST_IO_SETATTR:
199310922SJeff.Bonwick@Sun.COM 		(void) ztest_setattr(zd, object);
199410922SJeff.Bonwick@Sun.COM 		break;
199510922SJeff.Bonwick@Sun.COM 	}
199610922SJeff.Bonwick@Sun.COM 
199710922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
199810922SJeff.Bonwick@Sun.COM }
199910922SJeff.Bonwick@Sun.COM 
200010922SJeff.Bonwick@Sun.COM /*
200110922SJeff.Bonwick@Sun.COM  * Initialize an object description template.
200210922SJeff.Bonwick@Sun.COM  */
200310922SJeff.Bonwick@Sun.COM static void
ztest_od_init(ztest_od_t * od,uint64_t id,char * tag,uint64_t index,dmu_object_type_t type,uint64_t blocksize,uint64_t gen)200410922SJeff.Bonwick@Sun.COM ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
200510922SJeff.Bonwick@Sun.COM     dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
200610922SJeff.Bonwick@Sun.COM {
200710922SJeff.Bonwick@Sun.COM 	od->od_dir = ZTEST_DIROBJ;
200810922SJeff.Bonwick@Sun.COM 	od->od_object = 0;
200910922SJeff.Bonwick@Sun.COM 
201010922SJeff.Bonwick@Sun.COM 	od->od_crtype = type;
201110922SJeff.Bonwick@Sun.COM 	od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
201210922SJeff.Bonwick@Sun.COM 	od->od_crgen = gen;
201310922SJeff.Bonwick@Sun.COM 
201410922SJeff.Bonwick@Sun.COM 	od->od_type = DMU_OT_NONE;
201510922SJeff.Bonwick@Sun.COM 	od->od_blocksize = 0;
201610922SJeff.Bonwick@Sun.COM 	od->od_gen = 0;
201710922SJeff.Bonwick@Sun.COM 
201810922SJeff.Bonwick@Sun.COM 	(void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
201910922SJeff.Bonwick@Sun.COM 	    tag, (int64_t)id, index);
202010922SJeff.Bonwick@Sun.COM }
202110922SJeff.Bonwick@Sun.COM 
202210922SJeff.Bonwick@Sun.COM /*
202310922SJeff.Bonwick@Sun.COM  * Lookup or create the objects for a test using the od template.
202410922SJeff.Bonwick@Sun.COM  * If the objects do not all exist, or if 'remove' is specified,
202510922SJeff.Bonwick@Sun.COM  * remove any existing objects and create new ones.  Otherwise,
202610922SJeff.Bonwick@Sun.COM  * use the existing objects.
202710922SJeff.Bonwick@Sun.COM  */
202810922SJeff.Bonwick@Sun.COM static int
ztest_object_init(ztest_ds_t * zd,ztest_od_t * od,size_t size,boolean_t remove)202910922SJeff.Bonwick@Sun.COM ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
203010922SJeff.Bonwick@Sun.COM {
203110922SJeff.Bonwick@Sun.COM 	int count = size / sizeof (*od);
203210922SJeff.Bonwick@Sun.COM 	int rv = 0;
203310922SJeff.Bonwick@Sun.COM 
203410922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
203510922SJeff.Bonwick@Sun.COM 	if ((ztest_lookup(zd, od, count) != 0 || remove) &&
203610922SJeff.Bonwick@Sun.COM 	    (ztest_remove(zd, od, count) != 0 ||
203710922SJeff.Bonwick@Sun.COM 	    ztest_create(zd, od, count) != 0))
203810922SJeff.Bonwick@Sun.COM 		rv = -1;
203910922SJeff.Bonwick@Sun.COM 	zd->zd_od = od;
204010922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
204110922SJeff.Bonwick@Sun.COM 
204210922SJeff.Bonwick@Sun.COM 	return (rv);
204310922SJeff.Bonwick@Sun.COM }
204410922SJeff.Bonwick@Sun.COM 
204510922SJeff.Bonwick@Sun.COM /* ARGSUSED */
204610922SJeff.Bonwick@Sun.COM void
ztest_zil_commit(ztest_ds_t * zd,uint64_t id)204710922SJeff.Bonwick@Sun.COM ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
204810922SJeff.Bonwick@Sun.COM {
204910922SJeff.Bonwick@Sun.COM 	zilog_t *zilog = zd->zd_zilog;
205010922SJeff.Bonwick@Sun.COM 
205112699SNeil.Perrin@Sun.COM 	zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
205210922SJeff.Bonwick@Sun.COM 
205310922SJeff.Bonwick@Sun.COM 	/*
205410922SJeff.Bonwick@Sun.COM 	 * Remember the committed values in zd, which is in parent/child
205510922SJeff.Bonwick@Sun.COM 	 * shared memory.  If we die, the next iteration of ztest_run()
205610922SJeff.Bonwick@Sun.COM 	 * will verify that the log really does contain this record.
205710922SJeff.Bonwick@Sun.COM 	 */
205810922SJeff.Bonwick@Sun.COM 	mutex_enter(&zilog->zl_lock);
205910922SJeff.Bonwick@Sun.COM 	ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
206010922SJeff.Bonwick@Sun.COM 	zd->zd_seq = zilog->zl_commit_lr_seq;
206110922SJeff.Bonwick@Sun.COM 	mutex_exit(&zilog->zl_lock);
206210922SJeff.Bonwick@Sun.COM }
206310922SJeff.Bonwick@Sun.COM 
206410922SJeff.Bonwick@Sun.COM /*
2065789Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
2066789Sahrens  * or create a pool with a bad vdev spec.
2067789Sahrens  */
206810922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2069789Sahrens void
ztest_spa_create_destroy(ztest_ds_t * zd,uint64_t id)207010922SJeff.Bonwick@Sun.COM ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2071789Sahrens {
207210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
2073789Sahrens 	spa_t *spa;
2074789Sahrens 	nvlist_t *nvroot;
2075789Sahrens 
2076789Sahrens 	/*
2077789Sahrens 	 * Attempt to create using a bad file.
2078789Sahrens 	 */
20797754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
208010922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
208110922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2082789Sahrens 	nvlist_free(nvroot);
2083789Sahrens 
2084789Sahrens 	/*
2085789Sahrens 	 * Attempt to create using a bad mirror.
2086789Sahrens 	 */
20877754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
208810922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==,
208910922SJeff.Bonwick@Sun.COM 	    spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2090789Sahrens 	nvlist_free(nvroot);
2091789Sahrens 
2092789Sahrens 	/*
2093789Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
2094789Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
2095789Sahrens 	 */
209610922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
20977754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
209810922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2099789Sahrens 	nvlist_free(nvroot);
210010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
210110922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2102789Sahrens 	spa_close(spa, FTAG);
210310922SJeff.Bonwick@Sun.COM 
210410922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
2105789Sahrens }
2106789Sahrens 
21077768SJeff.Bonwick@Sun.COM static vdev_t *
vdev_lookup_by_path(vdev_t * vd,const char * path)21087768SJeff.Bonwick@Sun.COM vdev_lookup_by_path(vdev_t *vd, const char *path)
21097768SJeff.Bonwick@Sun.COM {
21107768SJeff.Bonwick@Sun.COM 	vdev_t *mvd;
21117768SJeff.Bonwick@Sun.COM 
21127768SJeff.Bonwick@Sun.COM 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
21137768SJeff.Bonwick@Sun.COM 		return (vd);
21147768SJeff.Bonwick@Sun.COM 
21157768SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
21167768SJeff.Bonwick@Sun.COM 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
21177768SJeff.Bonwick@Sun.COM 		    NULL)
21187768SJeff.Bonwick@Sun.COM 			return (mvd);
21197768SJeff.Bonwick@Sun.COM 
21207768SJeff.Bonwick@Sun.COM 	return (NULL);
21217768SJeff.Bonwick@Sun.COM }
21227768SJeff.Bonwick@Sun.COM 
2123789Sahrens /*
212410594SGeorge.Wilson@Sun.COM  * Find the first available hole which can be used as a top-level.
212510594SGeorge.Wilson@Sun.COM  */
212610594SGeorge.Wilson@Sun.COM int
find_vdev_hole(spa_t * spa)212710594SGeorge.Wilson@Sun.COM find_vdev_hole(spa_t *spa)
212810594SGeorge.Wilson@Sun.COM {
212910594SGeorge.Wilson@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
213010594SGeorge.Wilson@Sun.COM 	int c;
213110594SGeorge.Wilson@Sun.COM 
213210594SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
213310594SGeorge.Wilson@Sun.COM 
213410594SGeorge.Wilson@Sun.COM 	for (c = 0; c < rvd->vdev_children; c++) {
213510594SGeorge.Wilson@Sun.COM 		vdev_t *cvd = rvd->vdev_child[c];
213610594SGeorge.Wilson@Sun.COM 
213710594SGeorge.Wilson@Sun.COM 		if (cvd->vdev_ishole)
213810594SGeorge.Wilson@Sun.COM 			break;
213910594SGeorge.Wilson@Sun.COM 	}
214010594SGeorge.Wilson@Sun.COM 	return (c);
214110594SGeorge.Wilson@Sun.COM }
214210594SGeorge.Wilson@Sun.COM 
214310594SGeorge.Wilson@Sun.COM /*
2144789Sahrens  * Verify that vdev_add() works as expected.
2145789Sahrens  */
214610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2147789Sahrens void
ztest_vdev_add_remove(ztest_ds_t * zd,uint64_t id)214810922SJeff.Bonwick@Sun.COM ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2149789Sahrens {
215010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
215110922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
215211422SMark.Musante@Sun.COM 	uint64_t leaves;
215310594SGeorge.Wilson@Sun.COM 	uint64_t guid;
2154789Sahrens 	nvlist_t *nvroot;
2155789Sahrens 	int error;
2156789Sahrens 
215710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
215811422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2159789Sahrens 
21607754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2161789Sahrens 
216210594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2163789Sahrens 
21644527Sperrin 	/*
216510594SGeorge.Wilson@Sun.COM 	 * If we have slogs then remove them 1/4 of the time.
21664527Sperrin 	 */
216710594SGeorge.Wilson@Sun.COM 	if (spa_has_slogs(spa) && ztest_random(4) == 0) {
216810594SGeorge.Wilson@Sun.COM 		/*
216910594SGeorge.Wilson@Sun.COM 		 * Grab the guid from the head of the log class rotor.
217010594SGeorge.Wilson@Sun.COM 		 */
217110922SJeff.Bonwick@Sun.COM 		guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
217210594SGeorge.Wilson@Sun.COM 
217310594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
217410594SGeorge.Wilson@Sun.COM 
217510594SGeorge.Wilson@Sun.COM 		/*
217610594SGeorge.Wilson@Sun.COM 		 * We have to grab the zs_name_lock as writer to
217710594SGeorge.Wilson@Sun.COM 		 * prevent a race between removing a slog (dmu_objset_find)
217810594SGeorge.Wilson@Sun.COM 		 * and destroying a dataset. Removing the slog will
217910594SGeorge.Wilson@Sun.COM 		 * grab a reference on the dataset which may cause
218010594SGeorge.Wilson@Sun.COM 		 * dmu_objset_destroy() to fail with EBUSY thus
218110594SGeorge.Wilson@Sun.COM 		 * leaving the dataset in an inconsistent state.
218210594SGeorge.Wilson@Sun.COM 		 */
218310922SJeff.Bonwick@Sun.COM 		VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
218410594SGeorge.Wilson@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
218510922SJeff.Bonwick@Sun.COM 		VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
218610594SGeorge.Wilson@Sun.COM 
218710594SGeorge.Wilson@Sun.COM 		if (error && error != EEXIST)
218810594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_remove() = %d", error);
218910594SGeorge.Wilson@Sun.COM 	} else {
219010594SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
219110594SGeorge.Wilson@Sun.COM 
219210594SGeorge.Wilson@Sun.COM 		/*
219310594SGeorge.Wilson@Sun.COM 		 * Make 1/4 of the devices be log devices.
219410594SGeorge.Wilson@Sun.COM 		 */
219510594SGeorge.Wilson@Sun.COM 		nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
219611422SMark.Musante@Sun.COM 		    ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
219710594SGeorge.Wilson@Sun.COM 
219810594SGeorge.Wilson@Sun.COM 		error = spa_vdev_add(spa, nvroot);
219910594SGeorge.Wilson@Sun.COM 		nvlist_free(nvroot);
220010594SGeorge.Wilson@Sun.COM 
220110594SGeorge.Wilson@Sun.COM 		if (error == ENOSPC)
220210594SGeorge.Wilson@Sun.COM 			ztest_record_enospc("spa_vdev_add");
220310594SGeorge.Wilson@Sun.COM 		else if (error != 0)
220410594SGeorge.Wilson@Sun.COM 			fatal(0, "spa_vdev_add() = %d", error);
220510594SGeorge.Wilson@Sun.COM 	}
2206789Sahrens 
220710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
22087754SJeff.Bonwick@Sun.COM }
22097754SJeff.Bonwick@Sun.COM 
22107754SJeff.Bonwick@Sun.COM /*
22117754SJeff.Bonwick@Sun.COM  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
22127754SJeff.Bonwick@Sun.COM  */
221310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
22147754SJeff.Bonwick@Sun.COM void
ztest_vdev_aux_add_remove(ztest_ds_t * zd,uint64_t id)221510922SJeff.Bonwick@Sun.COM ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
22167754SJeff.Bonwick@Sun.COM {
221710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
221810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
22197768SJeff.Bonwick@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
22207754SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav;
22217754SJeff.Bonwick@Sun.COM 	char *aux;
22227754SJeff.Bonwick@Sun.COM 	uint64_t guid = 0;
22237754SJeff.Bonwick@Sun.COM 	int error;
22247754SJeff.Bonwick@Sun.COM 
22257768SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
22267754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_spares;
22277754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_SPARES;
22287754SJeff.Bonwick@Sun.COM 	} else {
22297754SJeff.Bonwick@Sun.COM 		sav = &spa->spa_l2cache;
22307754SJeff.Bonwick@Sun.COM 		aux = ZPOOL_CONFIG_L2CACHE;
22317754SJeff.Bonwick@Sun.COM 	}
22327754SJeff.Bonwick@Sun.COM 
223310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
22347754SJeff.Bonwick@Sun.COM 
22357754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
22367754SJeff.Bonwick@Sun.COM 
22377754SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
22387754SJeff.Bonwick@Sun.COM 		/*
22397754SJeff.Bonwick@Sun.COM 		 * Pick a random device to remove.
22407754SJeff.Bonwick@Sun.COM 		 */
22417754SJeff.Bonwick@Sun.COM 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
22427754SJeff.Bonwick@Sun.COM 	} else {
22437754SJeff.Bonwick@Sun.COM 		/*
22447754SJeff.Bonwick@Sun.COM 		 * Find an unused device we can add.
22457754SJeff.Bonwick@Sun.COM 		 */
224610922SJeff.Bonwick@Sun.COM 		zs->zs_vdev_aux = 0;
22477754SJeff.Bonwick@Sun.COM 		for (;;) {
22487754SJeff.Bonwick@Sun.COM 			char path[MAXPATHLEN];
22497754SJeff.Bonwick@Sun.COM 			int c;
22507754SJeff.Bonwick@Sun.COM 			(void) sprintf(path, ztest_aux_template, zopt_dir,
225110922SJeff.Bonwick@Sun.COM 			    zopt_pool, aux, zs->zs_vdev_aux);
22527754SJeff.Bonwick@Sun.COM 			for (c = 0; c < sav->sav_count; c++)
22537754SJeff.Bonwick@Sun.COM 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
22547754SJeff.Bonwick@Sun.COM 				    path) == 0)
22557754SJeff.Bonwick@Sun.COM 					break;
22567768SJeff.Bonwick@Sun.COM 			if (c == sav->sav_count &&
22577768SJeff.Bonwick@Sun.COM 			    vdev_lookup_by_path(rvd, path) == NULL)
22587754SJeff.Bonwick@Sun.COM 				break;
225910922SJeff.Bonwick@Sun.COM 			zs->zs_vdev_aux++;
22607754SJeff.Bonwick@Sun.COM 		}
22617754SJeff.Bonwick@Sun.COM 	}
22627754SJeff.Bonwick@Sun.COM 
22637754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
22647754SJeff.Bonwick@Sun.COM 
22657754SJeff.Bonwick@Sun.COM 	if (guid == 0) {
22667754SJeff.Bonwick@Sun.COM 		/*
22677754SJeff.Bonwick@Sun.COM 		 * Add a new device.
22687754SJeff.Bonwick@Sun.COM 		 */
22697768SJeff.Bonwick@Sun.COM 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
22707768SJeff.Bonwick@Sun.COM 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
22717754SJeff.Bonwick@Sun.COM 		error = spa_vdev_add(spa, nvroot);
22727754SJeff.Bonwick@Sun.COM 		if (error != 0)
22737754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
22747754SJeff.Bonwick@Sun.COM 		nvlist_free(nvroot);
22757754SJeff.Bonwick@Sun.COM 	} else {
22767754SJeff.Bonwick@Sun.COM 		/*
22777754SJeff.Bonwick@Sun.COM 		 * Remove an existing device.  Sometimes, dirty its
22787754SJeff.Bonwick@Sun.COM 		 * vdev state first to make sure we handle removal
22797754SJeff.Bonwick@Sun.COM 		 * of devices that have pending state changes.
22807754SJeff.Bonwick@Sun.COM 		 */
22817754SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
22829816SGeorge.Wilson@Sun.COM 			(void) vdev_online(spa, guid, 0, NULL);
22837754SJeff.Bonwick@Sun.COM 
22847754SJeff.Bonwick@Sun.COM 		error = spa_vdev_remove(spa, guid, B_FALSE);
22857754SJeff.Bonwick@Sun.COM 		if (error != 0 && error != EBUSY)
22867754SJeff.Bonwick@Sun.COM 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
22877754SJeff.Bonwick@Sun.COM 	}
22887754SJeff.Bonwick@Sun.COM 
228910922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2290789Sahrens }
2291789Sahrens 
2292789Sahrens /*
229311422SMark.Musante@Sun.COM  * split a pool if it has mirror tlvdevs
229411422SMark.Musante@Sun.COM  */
229511422SMark.Musante@Sun.COM /* ARGSUSED */
229611422SMark.Musante@Sun.COM void
ztest_split_pool(ztest_ds_t * zd,uint64_t id)229711422SMark.Musante@Sun.COM ztest_split_pool(ztest_ds_t *zd, uint64_t id)
229811422SMark.Musante@Sun.COM {
229911422SMark.Musante@Sun.COM 	ztest_shared_t *zs = ztest_shared;
230011422SMark.Musante@Sun.COM 	spa_t *spa = zs->zs_spa;
230111422SMark.Musante@Sun.COM 	vdev_t *rvd = spa->spa_root_vdev;
230211422SMark.Musante@Sun.COM 	nvlist_t *tree, **child, *config, *split, **schild;
230311422SMark.Musante@Sun.COM 	uint_t c, children, schildren = 0, lastlogid = 0;
230411422SMark.Musante@Sun.COM 	int error = 0;
230511422SMark.Musante@Sun.COM 
230611422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
230711422SMark.Musante@Sun.COM 
230811422SMark.Musante@Sun.COM 	/* ensure we have a useable config; mirrors of raidz aren't supported */
230911422SMark.Musante@Sun.COM 	if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
231011422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
231111422SMark.Musante@Sun.COM 		return;
231211422SMark.Musante@Sun.COM 	}
231311422SMark.Musante@Sun.COM 
231411422SMark.Musante@Sun.COM 	/* clean up the old pool, if any */
231511422SMark.Musante@Sun.COM 	(void) spa_destroy("splitp");
231611422SMark.Musante@Sun.COM 
231711422SMark.Musante@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
231811422SMark.Musante@Sun.COM 
231911422SMark.Musante@Sun.COM 	/* generate a config from the existing config */
232011864SMark.Musante@Sun.COM 	mutex_enter(&spa->spa_props_lock);
232111422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
232211422SMark.Musante@Sun.COM 	    &tree) == 0);
232311864SMark.Musante@Sun.COM 	mutex_exit(&spa->spa_props_lock);
232411864SMark.Musante@Sun.COM 
232511422SMark.Musante@Sun.COM 	VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
232611422SMark.Musante@Sun.COM 	    &children) == 0);
232711422SMark.Musante@Sun.COM 
232811422SMark.Musante@Sun.COM 	schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
232911422SMark.Musante@Sun.COM 	for (c = 0; c < children; c++) {
233011422SMark.Musante@Sun.COM 		vdev_t *tvd = rvd->vdev_child[c];
233111422SMark.Musante@Sun.COM 		nvlist_t **mchild;
233211422SMark.Musante@Sun.COM 		uint_t mchildren;
233311422SMark.Musante@Sun.COM 
233411422SMark.Musante@Sun.COM 		if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
233511422SMark.Musante@Sun.COM 			VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
233611422SMark.Musante@Sun.COM 			    0) == 0);
233711422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_string(schild[schildren],
233811422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
233911422SMark.Musante@Sun.COM 			VERIFY(nvlist_add_uint64(schild[schildren],
234011422SMark.Musante@Sun.COM 			    ZPOOL_CONFIG_IS_HOLE, 1) == 0);
234111422SMark.Musante@Sun.COM 			if (lastlogid == 0)
234211422SMark.Musante@Sun.COM 				lastlogid = schildren;
234311422SMark.Musante@Sun.COM 			++schildren;
234411422SMark.Musante@Sun.COM 			continue;
234511422SMark.Musante@Sun.COM 		}
234611422SMark.Musante@Sun.COM 		lastlogid = 0;
234711422SMark.Musante@Sun.COM 		VERIFY(nvlist_lookup_nvlist_array(child[c],
234811422SMark.Musante@Sun.COM 		    ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
234911422SMark.Musante@Sun.COM 		VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
235011422SMark.Musante@Sun.COM 	}
235111422SMark.Musante@Sun.COM 
235211422SMark.Musante@Sun.COM 	/* OK, create a config that can be used to split */
235311422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
235411422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
235511422SMark.Musante@Sun.COM 	    VDEV_TYPE_ROOT) == 0);
235611422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
235711422SMark.Musante@Sun.COM 	    lastlogid != 0 ? lastlogid : schildren) == 0);
235811422SMark.Musante@Sun.COM 
235911422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
236011422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
236111422SMark.Musante@Sun.COM 
236211422SMark.Musante@Sun.COM 	for (c = 0; c < schildren; c++)
236311422SMark.Musante@Sun.COM 		nvlist_free(schild[c]);
236411422SMark.Musante@Sun.COM 	free(schild);
236511422SMark.Musante@Sun.COM 	nvlist_free(split);
236611422SMark.Musante@Sun.COM 
236711422SMark.Musante@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
236811422SMark.Musante@Sun.COM 
236911422SMark.Musante@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
237011422SMark.Musante@Sun.COM 	error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
237111422SMark.Musante@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
237211422SMark.Musante@Sun.COM 
237311422SMark.Musante@Sun.COM 	nvlist_free(config);
237411422SMark.Musante@Sun.COM 
237511422SMark.Musante@Sun.COM 	if (error == 0) {
237611422SMark.Musante@Sun.COM 		(void) printf("successful split - results:\n");
237711422SMark.Musante@Sun.COM 		mutex_enter(&spa_namespace_lock);
237811422SMark.Musante@Sun.COM 		show_pool_stats(spa);
237911422SMark.Musante@Sun.COM 		show_pool_stats(spa_lookup("splitp"));
238011422SMark.Musante@Sun.COM 		mutex_exit(&spa_namespace_lock);
238111422SMark.Musante@Sun.COM 		++zs->zs_splits;
238211422SMark.Musante@Sun.COM 		--zs->zs_mirrors;
238311422SMark.Musante@Sun.COM 	}
238411422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
238511422SMark.Musante@Sun.COM 
238611422SMark.Musante@Sun.COM }
238711422SMark.Musante@Sun.COM 
238811422SMark.Musante@Sun.COM /*
2389789Sahrens  * Verify that we can attach and detach devices.
2390789Sahrens  */
239110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2392789Sahrens void
ztest_vdev_attach_detach(ztest_ds_t * zd,uint64_t id)239310922SJeff.Bonwick@Sun.COM ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2394789Sahrens {
239510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
239610922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
23977768SJeff.Bonwick@Sun.COM 	spa_aux_vdev_t *sav = &spa->spa_spares;
2398789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
23991544Seschrock 	vdev_t *oldvd, *newvd, *pvd;
24007754SJeff.Bonwick@Sun.COM 	nvlist_t *root;
240111422SMark.Musante@Sun.COM 	uint64_t leaves;
2402789Sahrens 	uint64_t leaf, top;
24031732Sbonwick 	uint64_t ashift = ztest_get_ashift();
24048241SJeff.Bonwick@Sun.COM 	uint64_t oldguid, pguid;
24051544Seschrock 	size_t oldsize, newsize;
24061544Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2407789Sahrens 	int replacing;
24087793SJeff.Bonwick@Sun.COM 	int oldvd_has_siblings = B_FALSE;
24097768SJeff.Bonwick@Sun.COM 	int newvd_is_spare = B_FALSE;
24107768SJeff.Bonwick@Sun.COM 	int oldvd_is_log;
2411789Sahrens 	int error, expected_error;
2412789Sahrens 
241310922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
241411422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2415789Sahrens 
24167754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2417789Sahrens 
2418789Sahrens 	/*
2419789Sahrens 	 * Decide whether to do an attach or a replace.
2420789Sahrens 	 */
2421789Sahrens 	replacing = ztest_random(2);
2422789Sahrens 
2423789Sahrens 	/*
2424789Sahrens 	 * Pick a random top-level vdev.
2425789Sahrens 	 */
242610922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
2427789Sahrens 
2428789Sahrens 	/*
2429789Sahrens 	 * Pick a random leaf within it.
2430789Sahrens 	 */
243111497SMark.Musante@Sun.COM 	leaf = ztest_random(leaves);
2432789Sahrens 
2433789Sahrens 	/*
24347768SJeff.Bonwick@Sun.COM 	 * Locate this vdev.
2435789Sahrens 	 */
24367768SJeff.Bonwick@Sun.COM 	oldvd = rvd->vdev_child[top];
243711422SMark.Musante@Sun.COM 	if (zs->zs_mirrors >= 1) {
24388241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
243911422SMark.Musante@Sun.COM 		ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
24407768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
24418241SJeff.Bonwick@Sun.COM 	}
24428241SJeff.Bonwick@Sun.COM 	if (zopt_raidz > 1) {
24438241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
24448241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children == zopt_raidz);
24457768SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
24468241SJeff.Bonwick@Sun.COM 	}
2447789Sahrens 
2448789Sahrens 	/*
24497768SJeff.Bonwick@Sun.COM 	 * If we're already doing an attach or replace, oldvd may be a
24507768SJeff.Bonwick@Sun.COM 	 * mirror vdev -- in which case, pick a random child.
2451789Sahrens 	 */
24527768SJeff.Bonwick@Sun.COM 	while (oldvd->vdev_children != 0) {
24537793SJeff.Bonwick@Sun.COM 		oldvd_has_siblings = B_TRUE;
24548241SJeff.Bonwick@Sun.COM 		ASSERT(oldvd->vdev_children >= 2);
24558241SJeff.Bonwick@Sun.COM 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
24567768SJeff.Bonwick@Sun.COM 	}
24577768SJeff.Bonwick@Sun.COM 
24587768SJeff.Bonwick@Sun.COM 	oldguid = oldvd->vdev_guid;
24599816SGeorge.Wilson@Sun.COM 	oldsize = vdev_get_min_asize(oldvd);
24607768SJeff.Bonwick@Sun.COM 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
24617768SJeff.Bonwick@Sun.COM 	(void) strcpy(oldpath, oldvd->vdev_path);
24621544Seschrock 	pvd = oldvd->vdev_parent;
24638241SJeff.Bonwick@Sun.COM 	pguid = pvd->vdev_guid;
2464789Sahrens 
2465789Sahrens 	/*
24667793SJeff.Bonwick@Sun.COM 	 * If oldvd has siblings, then half of the time, detach it.
24677793SJeff.Bonwick@Sun.COM 	 */
24687793SJeff.Bonwick@Sun.COM 	if (oldvd_has_siblings && ztest_random(2) == 0) {
24697793SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_VDEV, FTAG);
24708241SJeff.Bonwick@Sun.COM 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
24718241SJeff.Bonwick@Sun.COM 		if (error != 0 && error != ENODEV && error != EBUSY &&
24728241SJeff.Bonwick@Sun.COM 		    error != ENOTSUP)
24738241SJeff.Bonwick@Sun.COM 			fatal(0, "detach (%s) returned %d", oldpath, error);
247410922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
24757793SJeff.Bonwick@Sun.COM 		return;
24767793SJeff.Bonwick@Sun.COM 	}
24777793SJeff.Bonwick@Sun.COM 
24787793SJeff.Bonwick@Sun.COM 	/*
24797768SJeff.Bonwick@Sun.COM 	 * For the new vdev, choose with equal probability between the two
24807768SJeff.Bonwick@Sun.COM 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2481789Sahrens 	 */
24827768SJeff.Bonwick@Sun.COM 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
24837768SJeff.Bonwick@Sun.COM 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
24847768SJeff.Bonwick@Sun.COM 		newvd_is_spare = B_TRUE;
24857768SJeff.Bonwick@Sun.COM 		(void) strcpy(newpath, newvd->vdev_path);
24867768SJeff.Bonwick@Sun.COM 	} else {
24877768SJeff.Bonwick@Sun.COM 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
24887768SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
24897768SJeff.Bonwick@Sun.COM 		if (ztest_random(2) == 0)
24907768SJeff.Bonwick@Sun.COM 			newpath[strlen(newpath) - 1] = 'b';
24917768SJeff.Bonwick@Sun.COM 		newvd = vdev_lookup_by_path(rvd, newpath);
24927768SJeff.Bonwick@Sun.COM 	}
24937768SJeff.Bonwick@Sun.COM 
24947768SJeff.Bonwick@Sun.COM 	if (newvd) {
24959816SGeorge.Wilson@Sun.COM 		newsize = vdev_get_min_asize(newvd);
24967768SJeff.Bonwick@Sun.COM 	} else {
24977768SJeff.Bonwick@Sun.COM 		/*
24987768SJeff.Bonwick@Sun.COM 		 * Make newsize a little bigger or smaller than oldsize.
24997768SJeff.Bonwick@Sun.COM 		 * If it's smaller, the attach should fail.
25007768SJeff.Bonwick@Sun.COM 		 * If it's larger, and we're doing a replace,
25017768SJeff.Bonwick@Sun.COM 		 * we should get dynamic LUN growth when we're done.
25027768SJeff.Bonwick@Sun.COM 		 */
25037768SJeff.Bonwick@Sun.COM 		newsize = 10 * oldsize / (9 + ztest_random(3));
25047768SJeff.Bonwick@Sun.COM 	}
2505789Sahrens 
2506789Sahrens 	/*
2507789Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2508789Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
2509789Sahrens 	 *
25101544Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
2511789Sahrens 	 *
25121544Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
2513789Sahrens 	 */
25147768SJeff.Bonwick@Sun.COM 	if (pvd->vdev_ops != &vdev_mirror_ops &&
25157768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
25167768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_replacing_ops ||
25177768SJeff.Bonwick@Sun.COM 	    pvd->vdev_ops == &vdev_spare_ops))
25187768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25197768SJeff.Bonwick@Sun.COM 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
25207768SJeff.Bonwick@Sun.COM 		expected_error = ENOTSUP;
25217768SJeff.Bonwick@Sun.COM 	else if (newvd == oldvd)
25227768SJeff.Bonwick@Sun.COM 		expected_error = replacing ? 0 : EBUSY;
25237768SJeff.Bonwick@Sun.COM 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
25242174Seschrock 		expected_error = EBUSY;
25251544Seschrock 	else if (newsize < oldsize)
2526789Sahrens 		expected_error = EOVERFLOW;
25271732Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
25281732Sbonwick 		expected_error = EDOM;
2529789Sahrens 	else
2530789Sahrens 		expected_error = 0;
2531789Sahrens 
25327754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_VDEV, FTAG);
2533789Sahrens 
2534789Sahrens 	/*
25351544Seschrock 	 * Build the nvlist describing newpath.
2536789Sahrens 	 */
25377754SJeff.Bonwick@Sun.COM 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
25387754SJeff.Bonwick@Sun.COM 	    ashift, 0, 0, 0, 1);
2539789Sahrens 
25407768SJeff.Bonwick@Sun.COM 	error = spa_vdev_attach(spa, oldguid, root, replacing);
2541789Sahrens 
2542789Sahrens 	nvlist_free(root);
2543789Sahrens 
2544789Sahrens 	/*
2545789Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
2546789Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
2547789Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
2548789Sahrens 	 */
2549789Sahrens 	if (expected_error == ENOTSUP &&
2550789Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
2551789Sahrens 		expected_error = error;
2552789Sahrens 
2553797Sbonwick 	/*
2554797Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
2555797Sbonwick 	 */
25567046Sahrens 	if (error == EOVERFLOW || error == EBUSY)
2557797Sbonwick 		expected_error = error;
2558797Sbonwick 
25597046Sahrens 	/* XXX workaround 6690467 */
25607046Sahrens 	if (error != expected_error && expected_error != EBUSY) {
25617046Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
25627046Sahrens 		    "returned %d, expected %d",
25637046Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
25647046Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
2565789Sahrens 	}
2566789Sahrens 
256710922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2568789Sahrens }
2569789Sahrens 
2570789Sahrens /*
25719816SGeorge.Wilson@Sun.COM  * Callback function which expands the physical size of the vdev.
25729816SGeorge.Wilson@Sun.COM  */
25739816SGeorge.Wilson@Sun.COM vdev_t *
grow_vdev(vdev_t * vd,void * arg)25749816SGeorge.Wilson@Sun.COM grow_vdev(vdev_t *vd, void *arg)
25759816SGeorge.Wilson@Sun.COM {
25769816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
25779816SGeorge.Wilson@Sun.COM 	size_t *newsize = arg;
25789816SGeorge.Wilson@Sun.COM 	size_t fsize;
25799816SGeorge.Wilson@Sun.COM 	int fd;
25809816SGeorge.Wilson@Sun.COM 
25819816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
25829816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
25839816SGeorge.Wilson@Sun.COM 
25849816SGeorge.Wilson@Sun.COM 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
25859816SGeorge.Wilson@Sun.COM 		return (vd);
25869816SGeorge.Wilson@Sun.COM 
25879816SGeorge.Wilson@Sun.COM 	fsize = lseek(fd, 0, SEEK_END);
25889816SGeorge.Wilson@Sun.COM 	(void) ftruncate(fd, *newsize);
25899816SGeorge.Wilson@Sun.COM 
25909816SGeorge.Wilson@Sun.COM 	if (zopt_verbose >= 6) {
25919816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %lu to %lu bytes\n",
25929816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
25939816SGeorge.Wilson@Sun.COM 	}
25949816SGeorge.Wilson@Sun.COM 	(void) close(fd);
25959816SGeorge.Wilson@Sun.COM 	return (NULL);
25969816SGeorge.Wilson@Sun.COM }
25979816SGeorge.Wilson@Sun.COM 
25989816SGeorge.Wilson@Sun.COM /*
25999816SGeorge.Wilson@Sun.COM  * Callback function which expands a given vdev by calling vdev_online().
26009816SGeorge.Wilson@Sun.COM  */
26019816SGeorge.Wilson@Sun.COM /* ARGSUSED */
26029816SGeorge.Wilson@Sun.COM vdev_t *
online_vdev(vdev_t * vd,void * arg)26039816SGeorge.Wilson@Sun.COM online_vdev(vdev_t *vd, void *arg)
26049816SGeorge.Wilson@Sun.COM {
26059816SGeorge.Wilson@Sun.COM 	spa_t *spa = vd->vdev_spa;
26069816SGeorge.Wilson@Sun.COM 	vdev_t *tvd = vd->vdev_top;
26079816SGeorge.Wilson@Sun.COM 	uint64_t guid = vd->vdev_guid;
260810685SGeorge.Wilson@Sun.COM 	uint64_t generation = spa->spa_config_generation + 1;
260910850SGeorge.Wilson@Sun.COM 	vdev_state_t newstate = VDEV_STATE_UNKNOWN;
261010850SGeorge.Wilson@Sun.COM 	int error;
26119816SGeorge.Wilson@Sun.COM 
26129816SGeorge.Wilson@Sun.COM 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
26139816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
26149816SGeorge.Wilson@Sun.COM 
26159816SGeorge.Wilson@Sun.COM 	/* Calling vdev_online will initialize the new metaslabs */
26169816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
261710850SGeorge.Wilson@Sun.COM 	error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
26189816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26199816SGeorge.Wilson@Sun.COM 
26209816SGeorge.Wilson@Sun.COM 	/*
262110850SGeorge.Wilson@Sun.COM 	 * If vdev_online returned an error or the underlying vdev_open
262210850SGeorge.Wilson@Sun.COM 	 * failed then we abort the expand. The only way to know that
262310850SGeorge.Wilson@Sun.COM 	 * vdev_open fails is by checking the returned newstate.
262410850SGeorge.Wilson@Sun.COM 	 */
262510850SGeorge.Wilson@Sun.COM 	if (error || newstate != VDEV_STATE_HEALTHY) {
262610850SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
262710850SGeorge.Wilson@Sun.COM 			(void) printf("Unable to expand vdev, state %llu, "
262810850SGeorge.Wilson@Sun.COM 			    "error %d\n", (u_longlong_t)newstate, error);
262910850SGeorge.Wilson@Sun.COM 		}
263010850SGeorge.Wilson@Sun.COM 		return (vd);
263110850SGeorge.Wilson@Sun.COM 	}
263210850SGeorge.Wilson@Sun.COM 	ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
263310850SGeorge.Wilson@Sun.COM 
263410850SGeorge.Wilson@Sun.COM 	/*
26359816SGeorge.Wilson@Sun.COM 	 * Since we dropped the lock we need to ensure that we're
26369816SGeorge.Wilson@Sun.COM 	 * still talking to the original vdev. It's possible this
26379816SGeorge.Wilson@Sun.COM 	 * vdev may have been detached/replaced while we were
26389816SGeorge.Wilson@Sun.COM 	 * trying to online it.
26399816SGeorge.Wilson@Sun.COM 	 */
264010685SGeorge.Wilson@Sun.COM 	if (generation != spa->spa_config_generation) {
264110685SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
264210685SGeorge.Wilson@Sun.COM 			(void) printf("vdev configuration has changed, "
264310685SGeorge.Wilson@Sun.COM 			    "guid %llu, state %llu, expected gen %llu, "
264410922SJeff.Bonwick@Sun.COM 			    "got gen %llu\n",
264510922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)guid,
264610685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)tvd->vdev_state,
264710685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)generation,
264810685SGeorge.Wilson@Sun.COM 			    (u_longlong_t)spa->spa_config_generation);
26499816SGeorge.Wilson@Sun.COM 		}
26509816SGeorge.Wilson@Sun.COM 		return (vd);
26519816SGeorge.Wilson@Sun.COM 	}
26529816SGeorge.Wilson@Sun.COM 	return (NULL);
26539816SGeorge.Wilson@Sun.COM }
26549816SGeorge.Wilson@Sun.COM 
26559816SGeorge.Wilson@Sun.COM /*
26569816SGeorge.Wilson@Sun.COM  * Traverse the vdev tree calling the supplied function.
26579816SGeorge.Wilson@Sun.COM  * We continue to walk the tree until we either have walked all
26589816SGeorge.Wilson@Sun.COM  * children or we receive a non-NULL return from the callback.
26599816SGeorge.Wilson@Sun.COM  * If a NULL callback is passed, then we just return back the first
26609816SGeorge.Wilson@Sun.COM  * leaf vdev we encounter.
26619816SGeorge.Wilson@Sun.COM  */
26629816SGeorge.Wilson@Sun.COM vdev_t *
vdev_walk_tree(vdev_t * vd,vdev_t * (* func)(vdev_t *,void *),void * arg)26639816SGeorge.Wilson@Sun.COM vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
26649816SGeorge.Wilson@Sun.COM {
26659816SGeorge.Wilson@Sun.COM 	if (vd->vdev_ops->vdev_op_leaf) {
26669816SGeorge.Wilson@Sun.COM 		if (func == NULL)
26679816SGeorge.Wilson@Sun.COM 			return (vd);
26689816SGeorge.Wilson@Sun.COM 		else
26699816SGeorge.Wilson@Sun.COM 			return (func(vd, arg));
26709816SGeorge.Wilson@Sun.COM 	}
26719816SGeorge.Wilson@Sun.COM 
26729816SGeorge.Wilson@Sun.COM 	for (uint_t c = 0; c < vd->vdev_children; c++) {
26739816SGeorge.Wilson@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
26749816SGeorge.Wilson@Sun.COM 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
26759816SGeorge.Wilson@Sun.COM 			return (cvd);
26769816SGeorge.Wilson@Sun.COM 	}
26779816SGeorge.Wilson@Sun.COM 	return (NULL);
26789816SGeorge.Wilson@Sun.COM }
26799816SGeorge.Wilson@Sun.COM 
26809816SGeorge.Wilson@Sun.COM /*
2681789Sahrens  * Verify that dynamic LUN growth works as expected.
2682789Sahrens  */
268310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2684789Sahrens void
ztest_vdev_LUN_growth(ztest_ds_t * zd,uint64_t id)268510922SJeff.Bonwick@Sun.COM ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2686789Sahrens {
268710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
268810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
268910922SJeff.Bonwick@Sun.COM 	vdev_t *vd, *tvd;
269010922SJeff.Bonwick@Sun.COM 	metaslab_class_t *mc;
269110922SJeff.Bonwick@Sun.COM 	metaslab_group_t *mg;
26929816SGeorge.Wilson@Sun.COM 	size_t psize, newsize;
269310922SJeff.Bonwick@Sun.COM 	uint64_t top;
269410922SJeff.Bonwick@Sun.COM 	uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
269510922SJeff.Bonwick@Sun.COM 
269610922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
26979816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
26989816SGeorge.Wilson@Sun.COM 
269910922SJeff.Bonwick@Sun.COM 	top = ztest_random_vdev_top(spa, B_TRUE);
270010922SJeff.Bonwick@Sun.COM 
270110922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
270210922SJeff.Bonwick@Sun.COM 	mg = tvd->vdev_mg;
270310922SJeff.Bonwick@Sun.COM 	mc = mg->mg_class;
270410922SJeff.Bonwick@Sun.COM 	old_ms_count = tvd->vdev_ms_count;
270510922SJeff.Bonwick@Sun.COM 	old_class_space = metaslab_class_get_space(mc);
27069816SGeorge.Wilson@Sun.COM 
27079816SGeorge.Wilson@Sun.COM 	/*
27089816SGeorge.Wilson@Sun.COM 	 * Determine the size of the first leaf vdev associated with
27099816SGeorge.Wilson@Sun.COM 	 * our top-level device.
27109816SGeorge.Wilson@Sun.COM 	 */
27119816SGeorge.Wilson@Sun.COM 	vd = vdev_walk_tree(tvd, NULL, NULL);
27129816SGeorge.Wilson@Sun.COM 	ASSERT3P(vd, !=, NULL);
27139816SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_ops->vdev_op_leaf);
27149816SGeorge.Wilson@Sun.COM 
27159816SGeorge.Wilson@Sun.COM 	psize = vd->vdev_psize;
27169816SGeorge.Wilson@Sun.COM 
27179816SGeorge.Wilson@Sun.COM 	/*
271810685SGeorge.Wilson@Sun.COM 	 * We only try to expand the vdev if it's healthy, less than 4x its
271910685SGeorge.Wilson@Sun.COM 	 * original size, and it has a valid psize.
27209816SGeorge.Wilson@Sun.COM 	 */
272110685SGeorge.Wilson@Sun.COM 	if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
272210685SGeorge.Wilson@Sun.COM 	    psize == 0 || psize >= 4 * zopt_vdev_size) {
27239816SGeorge.Wilson@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
272410922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27259816SGeorge.Wilson@Sun.COM 		return;
27269816SGeorge.Wilson@Sun.COM 	}
27279816SGeorge.Wilson@Sun.COM 	ASSERT(psize > 0);
27289816SGeorge.Wilson@Sun.COM 	newsize = psize + psize / 8;
27299816SGeorge.Wilson@Sun.COM 	ASSERT3U(newsize, >, psize);
27309816SGeorge.Wilson@Sun.COM 
273110922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6) {
273210922SJeff.Bonwick@Sun.COM 		(void) printf("Expanding LUN %s from %lu to %lu\n",
27339816SGeorge.Wilson@Sun.COM 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
27349816SGeorge.Wilson@Sun.COM 	}
27359816SGeorge.Wilson@Sun.COM 
2736789Sahrens 	/*
27379816SGeorge.Wilson@Sun.COM 	 * Growing the vdev is a two step process:
27389816SGeorge.Wilson@Sun.COM 	 *	1). expand the physical size (i.e. relabel)
27399816SGeorge.Wilson@Sun.COM 	 *	2). online the vdev to create the new metaslabs
27409816SGeorge.Wilson@Sun.COM 	 */
27419816SGeorge.Wilson@Sun.COM 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
27429816SGeorge.Wilson@Sun.COM 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
27439816SGeorge.Wilson@Sun.COM 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
27449816SGeorge.Wilson@Sun.COM 		if (zopt_verbose >= 5) {
27459816SGeorge.Wilson@Sun.COM 			(void) printf("Could not expand LUN because "
274610685SGeorge.Wilson@Sun.COM 			    "the vdev configuration changed.\n");
27479816SGeorge.Wilson@Sun.COM 		}
274810922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
274910922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
27509816SGeorge.Wilson@Sun.COM 		return;
27519816SGeorge.Wilson@Sun.COM 	}
27529816SGeorge.Wilson@Sun.COM 
275310922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
27549816SGeorge.Wilson@Sun.COM 
27559816SGeorge.Wilson@Sun.COM 	/*
27569816SGeorge.Wilson@Sun.COM 	 * Expanding the LUN will update the config asynchronously,
27579816SGeorge.Wilson@Sun.COM 	 * thus we must wait for the async thread to complete any
27589816SGeorge.Wilson@Sun.COM 	 * pending tasks before proceeding.
2759789Sahrens 	 */
276010922SJeff.Bonwick@Sun.COM 	for (;;) {
276110922SJeff.Bonwick@Sun.COM 		boolean_t done;
276210922SJeff.Bonwick@Sun.COM 		mutex_enter(&spa->spa_async_lock);
276310922SJeff.Bonwick@Sun.COM 		done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
276410922SJeff.Bonwick@Sun.COM 		mutex_exit(&spa->spa_async_lock);
276510922SJeff.Bonwick@Sun.COM 		if (done)
276610922SJeff.Bonwick@Sun.COM 			break;
276710922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
276810922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
276910922SJeff.Bonwick@Sun.COM 	}
27709816SGeorge.Wilson@Sun.COM 
27719816SGeorge.Wilson@Sun.COM 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
277210922SJeff.Bonwick@Sun.COM 
277310922SJeff.Bonwick@Sun.COM 	tvd = spa->spa_root_vdev->vdev_child[top];
277410922SJeff.Bonwick@Sun.COM 	new_ms_count = tvd->vdev_ms_count;
277510922SJeff.Bonwick@Sun.COM 	new_class_space = metaslab_class_get_space(mc);
277610922SJeff.Bonwick@Sun.COM 
277710922SJeff.Bonwick@Sun.COM 	if (tvd->vdev_mg != mg || mg->mg_class != mc) {
277810922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 5) {
277910922SJeff.Bonwick@Sun.COM 			(void) printf("Could not verify LUN expansion due to "
278010922SJeff.Bonwick@Sun.COM 			    "intervening vdev offline or remove.\n");
278110922SJeff.Bonwick@Sun.COM 		}
278210922SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_STATE, spa);
278310922SJeff.Bonwick@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
278410922SJeff.Bonwick@Sun.COM 		return;
278510922SJeff.Bonwick@Sun.COM 	}
278610922SJeff.Bonwick@Sun.COM 
278710922SJeff.Bonwick@Sun.COM 	/*
278810922SJeff.Bonwick@Sun.COM 	 * Make sure we were able to grow the vdev.
278910922SJeff.Bonwick@Sun.COM 	 */
279010922SJeff.Bonwick@Sun.COM 	if (new_ms_count <= old_ms_count)
279110922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
279210922SJeff.Bonwick@Sun.COM 		    old_ms_count, new_ms_count);
27939816SGeorge.Wilson@Sun.COM 
27949816SGeorge.Wilson@Sun.COM 	/*
27959816SGeorge.Wilson@Sun.COM 	 * Make sure we were able to grow the pool.
27969816SGeorge.Wilson@Sun.COM 	 */
279710922SJeff.Bonwick@Sun.COM 	if (new_class_space <= old_class_space)
279810922SJeff.Bonwick@Sun.COM 		fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
279910922SJeff.Bonwick@Sun.COM 		    old_class_space, new_class_space);
280010922SJeff.Bonwick@Sun.COM 
280110922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 5) {
28029816SGeorge.Wilson@Sun.COM 		char oldnumbuf[6], newnumbuf[6];
28039816SGeorge.Wilson@Sun.COM 
280410922SJeff.Bonwick@Sun.COM 		nicenum(old_class_space, oldnumbuf);
280510922SJeff.Bonwick@Sun.COM 		nicenum(new_class_space, newnumbuf);
28069816SGeorge.Wilson@Sun.COM 		(void) printf("%s grew from %s to %s\n",
28079816SGeorge.Wilson@Sun.COM 		    spa->spa_name, oldnumbuf, newnumbuf);
2808789Sahrens 	}
280910922SJeff.Bonwick@Sun.COM 
28109816SGeorge.Wilson@Sun.COM 	spa_config_exit(spa, SCL_STATE, spa);
281110922SJeff.Bonwick@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
281210922SJeff.Bonwick@Sun.COM }
281310922SJeff.Bonwick@Sun.COM 
281410922SJeff.Bonwick@Sun.COM /*
281510922SJeff.Bonwick@Sun.COM  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
281610922SJeff.Bonwick@Sun.COM  */
281710922SJeff.Bonwick@Sun.COM /* ARGSUSED */
281810922SJeff.Bonwick@Sun.COM static void
ztest_objset_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)281910922SJeff.Bonwick@Sun.COM ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
282010922SJeff.Bonwick@Sun.COM {
282110922SJeff.Bonwick@Sun.COM 	/*
282210922SJeff.Bonwick@Sun.COM 	 * Create the objects common to all ztest datasets.
282310922SJeff.Bonwick@Sun.COM 	 */
282410922SJeff.Bonwick@Sun.COM 	VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
282510922SJeff.Bonwick@Sun.COM 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2826789Sahrens }
2827789Sahrens 
282812294SMark.Musante@Sun.COM static int
ztest_dataset_create(char * dsname)282912294SMark.Musante@Sun.COM ztest_dataset_create(char *dsname)
283012294SMark.Musante@Sun.COM {
283112294SMark.Musante@Sun.COM 	uint64_t zilset = ztest_random(100);
283212294SMark.Musante@Sun.COM 	int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
283312294SMark.Musante@Sun.COM 	    ztest_objset_create_cb, NULL);
283412294SMark.Musante@Sun.COM 
283512294SMark.Musante@Sun.COM 	if (err || zilset < 80)
283612294SMark.Musante@Sun.COM 		return (err);
283712294SMark.Musante@Sun.COM 
283812294SMark.Musante@Sun.COM 	(void) printf("Setting dataset %s to sync always\n", dsname);
283912294SMark.Musante@Sun.COM 	return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
284012294SMark.Musante@Sun.COM 	    ZFS_SYNC_ALWAYS, B_FALSE));
284112294SMark.Musante@Sun.COM }
284212294SMark.Musante@Sun.COM 
2843789Sahrens /* ARGSUSED */
284410922SJeff.Bonwick@Sun.COM static int
ztest_objset_destroy_cb(const char * name,void * arg)284511209SMatthew.Ahrens@Sun.COM ztest_objset_destroy_cb(const char *name, void *arg)
2846789Sahrens {
2847789Sahrens 	objset_t *os;
284810922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
2849789Sahrens 	int error;
2850789Sahrens 
2851789Sahrens 	/*
2852789Sahrens 	 * Verify that the dataset contains a directory object.
2853789Sahrens 	 */
285410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
285510922SJeff.Bonwick@Sun.COM 	error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
28561731Sbonwick 	if (error != ENOENT) {
28571731Sbonwick 		/* We could have crashed in the middle of destroying it */
28581731Sbonwick 		ASSERT3U(error, ==, 0);
285910922SJeff.Bonwick@Sun.COM 		ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
286010922SJeff.Bonwick@Sun.COM 		ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
28611731Sbonwick 	}
286210298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os, FTAG);
2863789Sahrens 
2864789Sahrens 	/*
2865789Sahrens 	 * Destroy the dataset.
2866789Sahrens 	 */
286710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
28682199Sahrens 	return (0);
2869789Sahrens }
2870789Sahrens 
287110922SJeff.Bonwick@Sun.COM static boolean_t
ztest_snapshot_create(char * osname,uint64_t id)287210922SJeff.Bonwick@Sun.COM ztest_snapshot_create(char *osname, uint64_t id)
2873789Sahrens {
287410922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
287510922SJeff.Bonwick@Sun.COM 	int error;
287610922SJeff.Bonwick@Sun.COM 
287710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
287810922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
287910922SJeff.Bonwick@Sun.COM 
288010922SJeff.Bonwick@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2881*13043STim.Haley@Sun.COM 	    NULL, NULL, B_FALSE, B_FALSE, -1);
288210922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
288310922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
288410922SJeff.Bonwick@Sun.COM 		return (B_FALSE);
288510922SJeff.Bonwick@Sun.COM 	}
288610922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != EEXIST)
288710922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
288810922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
2889789Sahrens }
2890789Sahrens 
289110922SJeff.Bonwick@Sun.COM static boolean_t
ztest_snapshot_destroy(char * osname,uint64_t id)289210922SJeff.Bonwick@Sun.COM ztest_snapshot_destroy(char *osname, uint64_t id)
289310922SJeff.Bonwick@Sun.COM {
289410922SJeff.Bonwick@Sun.COM 	char snapname[MAXNAMELEN];
289510922SJeff.Bonwick@Sun.COM 	int error;
289610922SJeff.Bonwick@Sun.COM 
289710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
289810922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)id);
289910922SJeff.Bonwick@Sun.COM 
290010922SJeff.Bonwick@Sun.COM 	error = dmu_objset_destroy(snapname, B_FALSE);
290110922SJeff.Bonwick@Sun.COM 	if (error != 0 && error != ENOENT)
290210922SJeff.Bonwick@Sun.COM 		fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
290310922SJeff.Bonwick@Sun.COM 	return (B_TRUE);
290410922SJeff.Bonwick@Sun.COM }
290510922SJeff.Bonwick@Sun.COM 
290610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
2907789Sahrens void
ztest_dmu_objset_create_destroy(ztest_ds_t * zd,uint64_t id)290810922SJeff.Bonwick@Sun.COM ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2909789Sahrens {
291010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
291110922SJeff.Bonwick@Sun.COM 	ztest_ds_t zdtmp;
291210922SJeff.Bonwick@Sun.COM 	int iters;
2913789Sahrens 	int error;
29146689Smaybee 	objset_t *os, *os2;
291510922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
2916789Sahrens 	zilog_t *zilog;
291710922SJeff.Bonwick@Sun.COM 
291810922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
291910922SJeff.Bonwick@Sun.COM 
292010922SJeff.Bonwick@Sun.COM 	(void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
292110922SJeff.Bonwick@Sun.COM 	    zs->zs_pool, (u_longlong_t)id);
2922789Sahrens 
2923789Sahrens 	/*
2924789Sahrens 	 * If this dataset exists from a previous run, process its replay log
2925789Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
292610922SJeff.Bonwick@Sun.COM 	 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2927789Sahrens 	 */
2928789Sahrens 	if (ztest_random(2) == 0 &&
292910298SMatthew.Ahrens@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
293010922SJeff.Bonwick@Sun.COM 		ztest_zd_init(&zdtmp, os);
293110922SJeff.Bonwick@Sun.COM 		zil_replay(os, &zdtmp, ztest_replay_vector);
293210922SJeff.Bonwick@Sun.COM 		ztest_zd_fini(&zdtmp);
293310298SMatthew.Ahrens@Sun.COM 		dmu_objset_disown(os, FTAG);
2934789Sahrens 	}
2935789Sahrens 
2936789Sahrens 	/*
2937789Sahrens 	 * There may be an old instance of the dataset we're about to
2938789Sahrens 	 * create lying around from a previous run.  If so, destroy it
2939789Sahrens 	 * and all of its snapshots.
2940789Sahrens 	 */
294110922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
29422417Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2943789Sahrens 
2944789Sahrens 	/*
2945789Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
2946789Sahrens 	 */
294710922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2948789Sahrens 
2949789Sahrens 	/*
2950789Sahrens 	 * Verify that we can create a new dataset.
2951789Sahrens 	 */
295212294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
2953789Sahrens 	if (error) {
2954789Sahrens 		if (error == ENOSPC) {
295510922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
295610922SJeff.Bonwick@Sun.COM 			(void) rw_unlock(&zs->zs_name_lock);
2957789Sahrens 			return;
2958789Sahrens 		}
2959789Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
2960789Sahrens 	}
2961789Sahrens 
296210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==,
296310922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
296410922SJeff.Bonwick@Sun.COM 
296510922SJeff.Bonwick@Sun.COM 	ztest_zd_init(&zdtmp, os);
2966789Sahrens 
2967789Sahrens 	/*
2968789Sahrens 	 * Open the intent log for it.
2969789Sahrens 	 */
297010922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
2971789Sahrens 
2972789Sahrens 	/*
297310922SJeff.Bonwick@Sun.COM 	 * Put some objects in there, do a little I/O to them,
297410922SJeff.Bonwick@Sun.COM 	 * and randomly take a couple of snapshots along the way.
2975789Sahrens 	 */
297610922SJeff.Bonwick@Sun.COM 	iters = ztest_random(5);
297710922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < iters; i++) {
297810922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(&zdtmp, id);
297910922SJeff.Bonwick@Sun.COM 		if (ztest_random(iters) == 0)
298010922SJeff.Bonwick@Sun.COM 			(void) ztest_snapshot_create(name, i);
2981789Sahrens 	}
2982789Sahrens 
2983789Sahrens 	/*
2984789Sahrens 	 * Verify that we cannot create an existing dataset.
2985789Sahrens 	 */
298610922SJeff.Bonwick@Sun.COM 	VERIFY3U(EEXIST, ==,
298710922SJeff.Bonwick@Sun.COM 	    dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2988789Sahrens 
2989789Sahrens 	/*
299010298SMatthew.Ahrens@Sun.COM 	 * Verify that we can hold an objset that is also owned.
2991789Sahrens 	 */
299210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
299310298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(os2, FTAG);
299410298SMatthew.Ahrens@Sun.COM 
299510298SMatthew.Ahrens@Sun.COM 	/*
299610922SJeff.Bonwick@Sun.COM 	 * Verify that we cannot own an objset that is already owned.
299710298SMatthew.Ahrens@Sun.COM 	 */
299810922SJeff.Bonwick@Sun.COM 	VERIFY3U(EBUSY, ==,
299910922SJeff.Bonwick@Sun.COM 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3000789Sahrens 
3001789Sahrens 	zil_close(zilog);
300210298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
300310922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(&zdtmp);
300410922SJeff.Bonwick@Sun.COM 
300510922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3006789Sahrens }
3007789Sahrens 
3008789Sahrens /*
3009789Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3010789Sahrens  */
3011789Sahrens void
ztest_dmu_snapshot_create_destroy(ztest_ds_t * zd,uint64_t id)301210922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3013789Sahrens {
301410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
301510922SJeff.Bonwick@Sun.COM 
301610922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
301710922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_destroy(zd->zd_name, id);
301810922SJeff.Bonwick@Sun.COM 	(void) ztest_snapshot_create(zd->zd_name, id);
301910922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
3020789Sahrens }
3021789Sahrens 
3022789Sahrens /*
30239691SGeorge.Wilson@Sun.COM  * Cleanup non-standard snapshots and clones.
30249691SGeorge.Wilson@Sun.COM  */
30259691SGeorge.Wilson@Sun.COM void
ztest_dsl_dataset_cleanup(char * osname,uint64_t id)302610922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
30279691SGeorge.Wilson@Sun.COM {
302810922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
302910922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
303010922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
303110922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
303210922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
30339691SGeorge.Wilson@Sun.COM 	int error;
30349691SGeorge.Wilson@Sun.COM 
303510922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
303610922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
303710922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
303810922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
303910922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30409691SGeorge.Wilson@Sun.COM 
304110242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone2name, B_FALSE);
30429691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30439691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
304410242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap3name, B_FALSE);
30459691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30469691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
304710242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap2name, B_FALSE);
30489691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30499691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
305010242Schris.kirby@sun.com 	error = dmu_objset_destroy(clone1name, B_FALSE);
30519691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30529691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
305310242Schris.kirby@sun.com 	error = dmu_objset_destroy(snap1name, B_FALSE);
30549691SGeorge.Wilson@Sun.COM 	if (error && error != ENOENT)
30559691SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
30569691SGeorge.Wilson@Sun.COM }
30579691SGeorge.Wilson@Sun.COM 
30589691SGeorge.Wilson@Sun.COM /*
30598779SMark.Musante@Sun.COM  * Verify dsl_dataset_promote handles EBUSY
30608779SMark.Musante@Sun.COM  */
30618779SMark.Musante@Sun.COM void
ztest_dsl_dataset_promote_busy(ztest_ds_t * zd,uint64_t id)306210922SJeff.Bonwick@Sun.COM ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
30638779SMark.Musante@Sun.COM {
306410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
30658779SMark.Musante@Sun.COM 	objset_t *clone;
30668779SMark.Musante@Sun.COM 	dsl_dataset_t *ds;
306710922SJeff.Bonwick@Sun.COM 	char snap1name[MAXNAMELEN];
306810922SJeff.Bonwick@Sun.COM 	char clone1name[MAXNAMELEN];
306910922SJeff.Bonwick@Sun.COM 	char snap2name[MAXNAMELEN];
307010922SJeff.Bonwick@Sun.COM 	char clone2name[MAXNAMELEN];
307110922SJeff.Bonwick@Sun.COM 	char snap3name[MAXNAMELEN];
307210922SJeff.Bonwick@Sun.COM 	char *osname = zd->zd_name;
307310922SJeff.Bonwick@Sun.COM 	int error;
307410922SJeff.Bonwick@Sun.COM 
307510922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
307610922SJeff.Bonwick@Sun.COM 
307710922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
307810922SJeff.Bonwick@Sun.COM 
307910922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
308010922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
308110922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
308210922SJeff.Bonwick@Sun.COM 	(void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
308310922SJeff.Bonwick@Sun.COM 	(void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
30848837SMark.Musante@Sun.COM 
30859355SMatthew.Ahrens@Sun.COM 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3086*13043STim.Haley@Sun.COM 	    NULL, NULL, B_FALSE, B_FALSE, -1);
30879229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
30889229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
308910922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
30909229SGeorge.Wilson@Sun.COM 			goto out;
30919229SGeorge.Wilson@Sun.COM 		}
30929229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
30939229SGeorge.Wilson@Sun.COM 	}
30948779SMark.Musante@Sun.COM 
309510298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap1name, FTAG, &clone);
30968779SMark.Musante@Sun.COM 	if (error)
30978779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
30988779SMark.Musante@Sun.COM 
309910272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
310010298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31019229SGeorge.Wilson@Sun.COM 	if (error) {
31029229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
310310922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31049229SGeorge.Wilson@Sun.COM 			goto out;
31059229SGeorge.Wilson@Sun.COM 		}
31068779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
31079229SGeorge.Wilson@Sun.COM 	}
31088779SMark.Musante@Sun.COM 
31098779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3110*13043STim.Haley@Sun.COM 	    NULL, NULL, B_FALSE, B_FALSE, -1);
31119229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31129229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
311310922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31149229SGeorge.Wilson@Sun.COM 			goto out;
31159229SGeorge.Wilson@Sun.COM 		}
31169229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
31179229SGeorge.Wilson@Sun.COM 	}
31188779SMark.Musante@Sun.COM 
31198779SMark.Musante@Sun.COM 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3120*13043STim.Haley@Sun.COM 	    NULL, NULL, B_FALSE, B_FALSE, -1);
31219229SGeorge.Wilson@Sun.COM 	if (error && error != EEXIST) {
31229229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
312310922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31249229SGeorge.Wilson@Sun.COM 			goto out;
31259229SGeorge.Wilson@Sun.COM 		}
31269229SGeorge.Wilson@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31279229SGeorge.Wilson@Sun.COM 	}
31288779SMark.Musante@Sun.COM 
312910298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_hold(snap3name, FTAG, &clone);
31308779SMark.Musante@Sun.COM 	if (error)
31318779SMark.Musante@Sun.COM 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
31328779SMark.Musante@Sun.COM 
313310272SMatthew.Ahrens@Sun.COM 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
313410298SMatthew.Ahrens@Sun.COM 	dmu_objset_rele(clone, FTAG);
31359229SGeorge.Wilson@Sun.COM 	if (error) {
31369229SGeorge.Wilson@Sun.COM 		if (error == ENOSPC) {
313710922SJeff.Bonwick@Sun.COM 			ztest_record_enospc(FTAG);
31389229SGeorge.Wilson@Sun.COM 			goto out;
31399229SGeorge.Wilson@Sun.COM 		}
31408779SMark.Musante@Sun.COM 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
31419229SGeorge.Wilson@Sun.COM 	}
31428779SMark.Musante@Sun.COM 
314312470SMatthew.Ahrens@Sun.COM 	error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
31448779SMark.Musante@Sun.COM 	if (error)
314512470SMatthew.Ahrens@Sun.COM 		fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
314610588SEric.Taylor@Sun.COM 	error = dsl_dataset_promote(clone2name, NULL);
31478779SMark.Musante@Sun.COM 	if (error != EBUSY)
31488779SMark.Musante@Sun.COM 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
31498779SMark.Musante@Sun.COM 		    error);
31508779SMark.Musante@Sun.COM 	dsl_dataset_disown(ds, FTAG);
31518779SMark.Musante@Sun.COM 
31529229SGeorge.Wilson@Sun.COM out:
315310922SJeff.Bonwick@Sun.COM 	ztest_dsl_dataset_cleanup(osname, id);
315410922SJeff.Bonwick@Sun.COM 
315510922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
31568779SMark.Musante@Sun.COM }
31578779SMark.Musante@Sun.COM 
31588779SMark.Musante@Sun.COM /*
3159789Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
3160789Sahrens  */
3161789Sahrens void
ztest_dmu_object_alloc_free(ztest_ds_t * zd,uint64_t id)316210922SJeff.Bonwick@Sun.COM ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3163789Sahrens {
316410922SJeff.Bonwick@Sun.COM 	ztest_od_t od[4];
316510922SJeff.Bonwick@Sun.COM 	int batchsize = sizeof (od) / sizeof (od[0]);
316610922SJeff.Bonwick@Sun.COM 
316710922SJeff.Bonwick@Sun.COM 	for (int b = 0; b < batchsize; b++)
316810922SJeff.Bonwick@Sun.COM 		ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3169789Sahrens 
3170789Sahrens 	/*
317110922SJeff.Bonwick@Sun.COM 	 * Destroy the previous batch of objects, create a new batch,
317210922SJeff.Bonwick@Sun.COM 	 * and do some I/O on the new objects.
3173789Sahrens 	 */
317410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
317510922SJeff.Bonwick@Sun.COM 		return;
317610922SJeff.Bonwick@Sun.COM 
317710922SJeff.Bonwick@Sun.COM 	while (ztest_random(4 * batchsize) != 0)
317810922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[ztest_random(batchsize)].od_object,
317910922SJeff.Bonwick@Sun.COM 		    ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3180789Sahrens }
3181789Sahrens 
3182789Sahrens /*
3183789Sahrens  * Verify that dmu_{read,write} work as expected.
3184789Sahrens  */
3185789Sahrens void
ztest_dmu_read_write(ztest_ds_t * zd,uint64_t id)318610922SJeff.Bonwick@Sun.COM ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3187789Sahrens {
318810922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
318910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
3190789Sahrens 	dmu_tx_t *tx;
3191789Sahrens 	int i, freeit, error;
3192789Sahrens 	uint64_t n, s, txg;
3193789Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
319410922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
319510922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3196789Sahrens 	uint64_t regions = 997;
3197789Sahrens 	uint64_t stride = 123456789ULL;
3198789Sahrens 	uint64_t width = 40;
3199789Sahrens 	int free_percent = 5;
3200789Sahrens 
3201789Sahrens 	/*
3202789Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
3203789Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
3204789Sahrens 	 * in sync and can be compared.  Their contents relate to each other
3205789Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
3206789Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
3207789Sahrens 	 * for any index n, there are three bufwads that should be identical:
3208789Sahrens 	 *
3209789Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
3210789Sahrens 	 *	bigobj, at the head of the nth chunk
3211789Sahrens 	 *	bigobj, at the tail of the nth chunk
3212789Sahrens 	 *
3213789Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
3214789Sahrens 	 * and it doesn't have any relation to the object blocksize.
3215789Sahrens 	 * The only requirement is that it can hold at least two bufwads.
3216789Sahrens 	 *
3217789Sahrens 	 * Normally, we write the bufwad to each of these locations.
3218789Sahrens 	 * However, free_percent of the time we instead write zeroes to
3219789Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
3220789Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
3221789Sahrens 	 * tracking which parts of an object are allocated and free,
3222789Sahrens 	 * and that the contents of the allocated blocks are correct.
3223789Sahrens 	 */
3224789Sahrens 
3225789Sahrens 	/*
3226789Sahrens 	 * Read the directory info.  If it's the first time, set things up.
3227789Sahrens 	 */
322810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
322910922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
323010922SJeff.Bonwick@Sun.COM 
323110922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
323210922SJeff.Bonwick@Sun.COM 		return;
323310922SJeff.Bonwick@Sun.COM 
323410922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
323510922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
323610922SJeff.Bonwick@Sun.COM 	chunksize = od[0].od_gen;
323710922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
3238789Sahrens 
3239789Sahrens 	/*
3240789Sahrens 	 * Prefetch a random chunk of the big object.
3241789Sahrens 	 * Our aim here is to get some async reads in flight
3242789Sahrens 	 * for blocks that we may free below; the DMU should
3243789Sahrens 	 * handle this race correctly.
3244789Sahrens 	 */
3245789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3246789Sahrens 	s = 1 + ztest_random(2 * width - 1);
324710922SJeff.Bonwick@Sun.COM 	dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3248789Sahrens 
3249789Sahrens 	/*
3250789Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
3251789Sahrens 	 */
3252789Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
3253789Sahrens 	s = 1 + ztest_random(width - 1);
3254789Sahrens 
3255789Sahrens 	packoff = n * sizeof (bufwad_t);
3256789Sahrens 	packsize = s * sizeof (bufwad_t);
3257789Sahrens 
325810922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
325910922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
3260789Sahrens 
3261789Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3262789Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3263789Sahrens 
3264789Sahrens 	/*
3265789Sahrens 	 * free_percent of the time, free a range of bigobj rather than
3266789Sahrens 	 * overwriting it.
3267789Sahrens 	 */
3268789Sahrens 	freeit = (ztest_random(100) < free_percent);
3269789Sahrens 
3270789Sahrens 	/*
3271789Sahrens 	 * Read the current contents of our objects.
3272789Sahrens 	 */
327310922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, packobj, packoff, packsize, packbuf,
32749512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32751544Seschrock 	ASSERT3U(error, ==, 0);
327610922SJeff.Bonwick@Sun.COM 	error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
32779512SNeil.Perrin@Sun.COM 	    DMU_READ_PREFETCH);
32781544Seschrock 	ASSERT3U(error, ==, 0);
3279789Sahrens 
3280789Sahrens 	/*
3281789Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
3282789Sahrens 	 */
3283789Sahrens 	tx = dmu_tx_create(os);
3284789Sahrens 
328510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, packobj, packoff, packsize);
3286789Sahrens 
3287789Sahrens 	if (freeit)
328810922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3289789Sahrens 	else
329010922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
329110922SJeff.Bonwick@Sun.COM 
329210922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
329310922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
3294789Sahrens 		umem_free(packbuf, packsize);
3295789Sahrens 		umem_free(bigbuf, bigsize);
3296789Sahrens 		return;
3297789Sahrens 	}
3298789Sahrens 
329910922SJeff.Bonwick@Sun.COM 	dmu_object_set_checksum(os, bigobj,
330010922SJeff.Bonwick@Sun.COM 	    (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
330110922SJeff.Bonwick@Sun.COM 
330210922SJeff.Bonwick@Sun.COM 	dmu_object_set_compress(os, bigobj,
330310922SJeff.Bonwick@Sun.COM 	    (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3304789Sahrens 
3305789Sahrens 	/*
3306789Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
3307789Sahrens 	 * in packobj matches the bufwads at the head and tail of the
3308789Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
3309789Sahrens 	 * with the new values we want to write out.
3310789Sahrens 	 */
3311789Sahrens 	for (i = 0; i < s; i++) {
3312789Sahrens 		/* LINTED */
3313789Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3314789Sahrens 		/* LINTED */
331510922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3316789Sahrens 		/* LINTED */
331710922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3318789Sahrens 
3319789Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3320789Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3321789Sahrens 
3322789Sahrens 		if (pack->bw_txg > txg)
3323789Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
3324789Sahrens 			    pack->bw_txg, txg);
3325789Sahrens 
3326789Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
3327789Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3328789Sahrens 			    pack->bw_index, n, i);
3329789Sahrens 
3330789Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3331789Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3332789Sahrens 
3333789Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3334789Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3335789Sahrens 
3336789Sahrens 		if (freeit) {
3337789Sahrens 			bzero(pack, sizeof (bufwad_t));
3338789Sahrens 		} else {
3339789Sahrens 			pack->bw_index = n + i;
3340789Sahrens 			pack->bw_txg = txg;
3341789Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
3342789Sahrens 		}
3343789Sahrens 		*bigH = *pack;
3344789Sahrens 		*bigT = *pack;
3345789Sahrens 	}
3346789Sahrens 
3347789Sahrens 	/*
3348789Sahrens 	 * We've verified all the old bufwads, and made new ones.
3349789Sahrens 	 * Now write them out.
3350789Sahrens 	 */
335110922SJeff.Bonwick@Sun.COM 	dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3352789Sahrens 
3353789Sahrens 	if (freeit) {
335410922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3355789Sahrens 			(void) printf("freeing offset %llx size %llx"
3356789Sahrens 			    " txg %llx\n",
3357789Sahrens 			    (u_longlong_t)bigoff,
3358789Sahrens 			    (u_longlong_t)bigsize,
3359789Sahrens 			    (u_longlong_t)txg);
3360789Sahrens 		}
336110922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3362789Sahrens 	} else {
336310922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
3364789Sahrens 			(void) printf("writing offset %llx size %llx"
3365789Sahrens 			    " txg %llx\n",
3366789Sahrens 			    (u_longlong_t)bigoff,
3367789Sahrens 			    (u_longlong_t)bigsize,
3368789Sahrens 			    (u_longlong_t)txg);
3369789Sahrens 		}
337010922SJeff.Bonwick@Sun.COM 		dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3371789Sahrens 	}
3372789Sahrens 
3373789Sahrens 	dmu_tx_commit(tx);
3374789Sahrens 
3375789Sahrens 	/*
3376789Sahrens 	 * Sanity check the stuff we just wrote.
3377789Sahrens 	 */
3378789Sahrens 	{
3379789Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3380789Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3381789Sahrens 
338210922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, packobj, packoff,
33839512SNeil.Perrin@Sun.COM 		    packsize, packcheck, DMU_READ_PREFETCH));
338410922SJeff.Bonwick@Sun.COM 		VERIFY(0 == dmu_read(os, bigobj, bigoff,
33859512SNeil.Perrin@Sun.COM 		    bigsize, bigcheck, DMU_READ_PREFETCH));
3386789Sahrens 
3387789Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3388789Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3389789Sahrens 
3390789Sahrens 		umem_free(packcheck, packsize);
3391789Sahrens 		umem_free(bigcheck, bigsize);
3392789Sahrens 	}
3393789Sahrens 
3394789Sahrens 	umem_free(packbuf, packsize);
3395789Sahrens 	umem_free(bigbuf, bigsize);
3396789Sahrens }
3397789Sahrens 
3398789Sahrens void
compare_and_update_pbbufs(uint64_t s,bufwad_t * packbuf,bufwad_t * bigbuf,uint64_t bigsize,uint64_t n,uint64_t chunksize,uint64_t txg)33999412SAleksandr.Guzovskiy@Sun.COM compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
340010922SJeff.Bonwick@Sun.COM     uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
34019412SAleksandr.Guzovskiy@Sun.COM {
34029412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34039412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *pack;
34049412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigH;
34059412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *bigT;
34069412SAleksandr.Guzovskiy@Sun.COM 
34079412SAleksandr.Guzovskiy@Sun.COM 	/*
34089412SAleksandr.Guzovskiy@Sun.COM 	 * For each index from n to n + s, verify that the existing bufwad
34099412SAleksandr.Guzovskiy@Sun.COM 	 * in packobj matches the bufwads at the head and tail of the
34109412SAleksandr.Guzovskiy@Sun.COM 	 * corresponding chunk in bigobj.  Then update all three bufwads
34119412SAleksandr.Guzovskiy@Sun.COM 	 * with the new values we want to write out.
34129412SAleksandr.Guzovskiy@Sun.COM 	 */
34139412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < s; i++) {
34149412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
34159412SAleksandr.Guzovskiy@Sun.COM 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
34169412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341710922SJeff.Bonwick@Sun.COM 		bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34189412SAleksandr.Guzovskiy@Sun.COM 		/* LINTED */
341910922SJeff.Bonwick@Sun.COM 		bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34209412SAleksandr.Guzovskiy@Sun.COM 
34219412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
34229412SAleksandr.Guzovskiy@Sun.COM 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
34239412SAleksandr.Guzovskiy@Sun.COM 
34249412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_txg > txg)
34259412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "future leak: got %llx, open txg is %llx",
34269412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_txg, txg);
34279412SAleksandr.Guzovskiy@Sun.COM 
34289412SAleksandr.Guzovskiy@Sun.COM 		if (pack->bw_data != 0 && pack->bw_index != n + i)
34299412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
34309412SAleksandr.Guzovskiy@Sun.COM 			    pack->bw_index, n, i);
34319412SAleksandr.Guzovskiy@Sun.COM 
34329412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
34339412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
34349412SAleksandr.Guzovskiy@Sun.COM 
34359412SAleksandr.Guzovskiy@Sun.COM 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
34369412SAleksandr.Guzovskiy@Sun.COM 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
34379412SAleksandr.Guzovskiy@Sun.COM 
34389412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_index = n + i;
34399412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_txg = txg;
34409412SAleksandr.Guzovskiy@Sun.COM 		pack->bw_data = 1 + ztest_random(-2ULL);
34419412SAleksandr.Guzovskiy@Sun.COM 
34429412SAleksandr.Guzovskiy@Sun.COM 		*bigH = *pack;
34439412SAleksandr.Guzovskiy@Sun.COM 		*bigT = *pack;
34449412SAleksandr.Guzovskiy@Sun.COM 	}
34459412SAleksandr.Guzovskiy@Sun.COM }
34469412SAleksandr.Guzovskiy@Sun.COM 
34479412SAleksandr.Guzovskiy@Sun.COM void
ztest_dmu_read_write_zcopy(ztest_ds_t * zd,uint64_t id)344810922SJeff.Bonwick@Sun.COM ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
34499412SAleksandr.Guzovskiy@Sun.COM {
345010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
345110922SJeff.Bonwick@Sun.COM 	ztest_od_t od[2];
34529412SAleksandr.Guzovskiy@Sun.COM 	dmu_tx_t *tx;
34539412SAleksandr.Guzovskiy@Sun.COM 	uint64_t i;
34549412SAleksandr.Guzovskiy@Sun.COM 	int error;
34559412SAleksandr.Guzovskiy@Sun.COM 	uint64_t n, s, txg;
34569412SAleksandr.Guzovskiy@Sun.COM 	bufwad_t *packbuf, *bigbuf;
345710922SJeff.Bonwick@Sun.COM 	uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
345810922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
345910922SJeff.Bonwick@Sun.COM 	uint64_t chunksize = blocksize;
34609412SAleksandr.Guzovskiy@Sun.COM 	uint64_t regions = 997;
34619412SAleksandr.Guzovskiy@Sun.COM 	uint64_t stride = 123456789ULL;
34629412SAleksandr.Guzovskiy@Sun.COM 	uint64_t width = 9;
34639412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_t *bonus_db;
34649412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t **bigbuf_arcbufs;
346510922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
34669412SAleksandr.Guzovskiy@Sun.COM 
34679412SAleksandr.Guzovskiy@Sun.COM 	/*
34689412SAleksandr.Guzovskiy@Sun.COM 	 * This test uses two objects, packobj and bigobj, that are always
34699412SAleksandr.Guzovskiy@Sun.COM 	 * updated together (i.e. in the same tx) so that their contents are
34709412SAleksandr.Guzovskiy@Sun.COM 	 * in sync and can be compared.  Their contents relate to each other
34719412SAleksandr.Guzovskiy@Sun.COM 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
34729412SAleksandr.Guzovskiy@Sun.COM 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
34739412SAleksandr.Guzovskiy@Sun.COM 	 * for any index n, there are three bufwads that should be identical:
34749412SAleksandr.Guzovskiy@Sun.COM 	 *
34759412SAleksandr.Guzovskiy@Sun.COM 	 *	packobj, at offset n * sizeof (bufwad_t)
34769412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the head of the nth chunk
34779412SAleksandr.Guzovskiy@Sun.COM 	 *	bigobj, at the tail of the nth chunk
34789412SAleksandr.Guzovskiy@Sun.COM 	 *
34799412SAleksandr.Guzovskiy@Sun.COM 	 * The chunk size is set equal to bigobj block size so that
34809412SAleksandr.Guzovskiy@Sun.COM 	 * dmu_assign_arcbuf() can be tested for object updates.
34819412SAleksandr.Guzovskiy@Sun.COM 	 */
34829412SAleksandr.Guzovskiy@Sun.COM 
34839412SAleksandr.Guzovskiy@Sun.COM 	/*
34849412SAleksandr.Guzovskiy@Sun.COM 	 * Read the directory info.  If it's the first time, set things up.
34859412SAleksandr.Guzovskiy@Sun.COM 	 */
348610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
348710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
348810922SJeff.Bonwick@Sun.COM 
348910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
349010922SJeff.Bonwick@Sun.COM 		return;
349110922SJeff.Bonwick@Sun.COM 
349210922SJeff.Bonwick@Sun.COM 	bigobj = od[0].od_object;
349310922SJeff.Bonwick@Sun.COM 	packobj = od[1].od_object;
349410922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
349510922SJeff.Bonwick@Sun.COM 	chunksize = blocksize;
349610922SJeff.Bonwick@Sun.COM 	ASSERT(chunksize == od[1].od_gen);
349710922SJeff.Bonwick@Sun.COM 
349810922SJeff.Bonwick@Sun.COM 	VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
349910922SJeff.Bonwick@Sun.COM 	VERIFY(ISP2(doi.doi_data_block_size));
350010922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize == doi.doi_data_block_size);
350110922SJeff.Bonwick@Sun.COM 	VERIFY(chunksize >= 2 * sizeof (bufwad_t));
35029412SAleksandr.Guzovskiy@Sun.COM 
35039412SAleksandr.Guzovskiy@Sun.COM 	/*
35049412SAleksandr.Guzovskiy@Sun.COM 	 * Pick a random index and compute the offsets into packobj and bigobj.
35059412SAleksandr.Guzovskiy@Sun.COM 	 */
35069412SAleksandr.Guzovskiy@Sun.COM 	n = ztest_random(regions) * stride + ztest_random(width);
35079412SAleksandr.Guzovskiy@Sun.COM 	s = 1 + ztest_random(width - 1);
35089412SAleksandr.Guzovskiy@Sun.COM 
35099412SAleksandr.Guzovskiy@Sun.COM 	packoff = n * sizeof (bufwad_t);
35109412SAleksandr.Guzovskiy@Sun.COM 	packsize = s * sizeof (bufwad_t);
35119412SAleksandr.Guzovskiy@Sun.COM 
351210922SJeff.Bonwick@Sun.COM 	bigoff = n * chunksize;
351310922SJeff.Bonwick@Sun.COM 	bigsize = s * chunksize;
35149412SAleksandr.Guzovskiy@Sun.COM 
35159412SAleksandr.Guzovskiy@Sun.COM 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
35169412SAleksandr.Guzovskiy@Sun.COM 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
35179412SAleksandr.Guzovskiy@Sun.COM 
351810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
35199412SAleksandr.Guzovskiy@Sun.COM 
35209412SAleksandr.Guzovskiy@Sun.COM 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
35219412SAleksandr.Guzovskiy@Sun.COM 
35229412SAleksandr.Guzovskiy@Sun.COM 	/*
35239412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
35249412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 1 test zcopy to already referenced dbufs.
35259412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
35269412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
35279412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
35289412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 5 test zcopy when it can't be done.
35299412SAleksandr.Guzovskiy@Sun.COM 	 * Iteration 6 one more zcopy write.
35309412SAleksandr.Guzovskiy@Sun.COM 	 */
35319412SAleksandr.Guzovskiy@Sun.COM 	for (i = 0; i < 7; i++) {
35329412SAleksandr.Guzovskiy@Sun.COM 		uint64_t j;
35339412SAleksandr.Guzovskiy@Sun.COM 		uint64_t off;
35349412SAleksandr.Guzovskiy@Sun.COM 
35359412SAleksandr.Guzovskiy@Sun.COM 		/*
35369412SAleksandr.Guzovskiy@Sun.COM 		 * In iteration 5 (i == 5) use arcbufs
35379412SAleksandr.Guzovskiy@Sun.COM 		 * that don't match bigobj blksz to test
35389412SAleksandr.Guzovskiy@Sun.COM 		 * dmu_assign_arcbuf() when it can't directly
35399412SAleksandr.Guzovskiy@Sun.COM 		 * assign an arcbuf to a dbuf.
35409412SAleksandr.Guzovskiy@Sun.COM 		 */
35419412SAleksandr.Guzovskiy@Sun.COM 		for (j = 0; j < s; j++) {
35429412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
35439412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[j] =
354410922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize);
35459412SAleksandr.Guzovskiy@Sun.COM 			} else {
35469412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j] =
354710922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35489412SAleksandr.Guzovskiy@Sun.COM 				bigbuf_arcbufs[2 * j + 1] =
354910922SJeff.Bonwick@Sun.COM 				    dmu_request_arcbuf(bonus_db, chunksize / 2);
35509412SAleksandr.Guzovskiy@Sun.COM 			}
35519412SAleksandr.Guzovskiy@Sun.COM 		}
35529412SAleksandr.Guzovskiy@Sun.COM 
35539412SAleksandr.Guzovskiy@Sun.COM 		/*
35549412SAleksandr.Guzovskiy@Sun.COM 		 * Get a tx for the mods to both packobj and bigobj.
35559412SAleksandr.Guzovskiy@Sun.COM 		 */
35569412SAleksandr.Guzovskiy@Sun.COM 		tx = dmu_tx_create(os);
35579412SAleksandr.Guzovskiy@Sun.COM 
355810922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, packobj, packoff, packsize);
355910922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
356010922SJeff.Bonwick@Sun.COM 
356110922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
356210922SJeff.Bonwick@Sun.COM 		if (txg == 0) {
35639412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packbuf, packsize);
35649412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf, bigsize);
35659412SAleksandr.Guzovskiy@Sun.COM 			for (j = 0; j < s; j++) {
35669412SAleksandr.Guzovskiy@Sun.COM 				if (i != 5) {
35679412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
35689412SAleksandr.Guzovskiy@Sun.COM 				} else {
35699412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35709412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j]);
35719412SAleksandr.Guzovskiy@Sun.COM 					dmu_return_arcbuf(
35729412SAleksandr.Guzovskiy@Sun.COM 					    bigbuf_arcbufs[2 * j + 1]);
35739412SAleksandr.Guzovskiy@Sun.COM 				}
35749412SAleksandr.Guzovskiy@Sun.COM 			}
35759412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
35769412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_rele(bonus_db, FTAG);
35779412SAleksandr.Guzovskiy@Sun.COM 			return;
35789412SAleksandr.Guzovskiy@Sun.COM 		}
35799412SAleksandr.Guzovskiy@Sun.COM 
35809412SAleksandr.Guzovskiy@Sun.COM 		/*
35819412SAleksandr.Guzovskiy@Sun.COM 		 * 50% of the time don't read objects in the 1st iteration to
35829412SAleksandr.Guzovskiy@Sun.COM 		 * test dmu_assign_arcbuf() for the case when there're no
35839412SAleksandr.Guzovskiy@Sun.COM 		 * existing dbufs for the specified offsets.
35849412SAleksandr.Guzovskiy@Sun.COM 		 */
35859412SAleksandr.Guzovskiy@Sun.COM 		if (i != 0 || ztest_random(2) != 0) {
358610922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, packobj, packoff,
35879512SNeil.Perrin@Sun.COM 			    packsize, packbuf, DMU_READ_PREFETCH);
35889412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
358910922SJeff.Bonwick@Sun.COM 			error = dmu_read(os, bigobj, bigoff, bigsize,
35909512SNeil.Perrin@Sun.COM 			    bigbuf, DMU_READ_PREFETCH);
35919412SAleksandr.Guzovskiy@Sun.COM 			ASSERT3U(error, ==, 0);
35929412SAleksandr.Guzovskiy@Sun.COM 		}
35939412SAleksandr.Guzovskiy@Sun.COM 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
359410922SJeff.Bonwick@Sun.COM 		    n, chunksize, txg);
35959412SAleksandr.Guzovskiy@Sun.COM 
35969412SAleksandr.Guzovskiy@Sun.COM 		/*
35979412SAleksandr.Guzovskiy@Sun.COM 		 * We've verified all the old bufwads, and made new ones.
35989412SAleksandr.Guzovskiy@Sun.COM 		 * Now write them out.
35999412SAleksandr.Guzovskiy@Sun.COM 		 */
360010922SJeff.Bonwick@Sun.COM 		dmu_write(os, packobj, packoff, packsize, packbuf, tx);
360110922SJeff.Bonwick@Sun.COM 		if (zopt_verbose >= 7) {
36029412SAleksandr.Guzovskiy@Sun.COM 			(void) printf("writing offset %llx size %llx"
36039412SAleksandr.Guzovskiy@Sun.COM 			    " txg %llx\n",
36049412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigoff,
36059412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)bigsize,
36069412SAleksandr.Guzovskiy@Sun.COM 			    (u_longlong_t)txg);
36079412SAleksandr.Guzovskiy@Sun.COM 		}
360810922SJeff.Bonwick@Sun.COM 		for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
36099412SAleksandr.Guzovskiy@Sun.COM 			dmu_buf_t *dbt;
36109412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36119412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
361210922SJeff.Bonwick@Sun.COM 				    bigbuf_arcbufs[j]->b_data, chunksize);
36139412SAleksandr.Guzovskiy@Sun.COM 			} else {
36149412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff),
36159412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j]->b_data,
361610922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36179412SAleksandr.Guzovskiy@Sun.COM 				bcopy((caddr_t)bigbuf + (off - bigoff) +
361810922SJeff.Bonwick@Sun.COM 				    chunksize / 2,
36199412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1]->b_data,
362010922SJeff.Bonwick@Sun.COM 				    chunksize / 2);
36219412SAleksandr.Guzovskiy@Sun.COM 			}
36229412SAleksandr.Guzovskiy@Sun.COM 
36239412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
362410922SJeff.Bonwick@Sun.COM 				VERIFY(dmu_buf_hold(os, bigobj, off,
362512285SJeff.Bonwick@Sun.COM 				    FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
36269412SAleksandr.Guzovskiy@Sun.COM 			}
36279412SAleksandr.Guzovskiy@Sun.COM 			if (i != 5) {
36289412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36299412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[j], tx);
36309412SAleksandr.Guzovskiy@Sun.COM 			} else {
36319412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db, off,
36329412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j], tx);
36339412SAleksandr.Guzovskiy@Sun.COM 				dmu_assign_arcbuf(bonus_db,
363410922SJeff.Bonwick@Sun.COM 				    off + chunksize / 2,
36359412SAleksandr.Guzovskiy@Sun.COM 				    bigbuf_arcbufs[2 * j + 1], tx);
36369412SAleksandr.Guzovskiy@Sun.COM 			}
36379412SAleksandr.Guzovskiy@Sun.COM 			if (i == 1) {
36389412SAleksandr.Guzovskiy@Sun.COM 				dmu_buf_rele(dbt, FTAG);
36399412SAleksandr.Guzovskiy@Sun.COM 			}
36409412SAleksandr.Guzovskiy@Sun.COM 		}
36419412SAleksandr.Guzovskiy@Sun.COM 		dmu_tx_commit(tx);
36429412SAleksandr.Guzovskiy@Sun.COM 
36439412SAleksandr.Guzovskiy@Sun.COM 		/*
36449412SAleksandr.Guzovskiy@Sun.COM 		 * Sanity check the stuff we just wrote.
36459412SAleksandr.Guzovskiy@Sun.COM 		 */
36469412SAleksandr.Guzovskiy@Sun.COM 		{
36479412SAleksandr.Guzovskiy@Sun.COM 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
36489412SAleksandr.Guzovskiy@Sun.COM 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
36499412SAleksandr.Guzovskiy@Sun.COM 
365010922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, packobj, packoff,
36519512SNeil.Perrin@Sun.COM 			    packsize, packcheck, DMU_READ_PREFETCH));
365210922SJeff.Bonwick@Sun.COM 			VERIFY(0 == dmu_read(os, bigobj, bigoff,
36539512SNeil.Perrin@Sun.COM 			    bigsize, bigcheck, DMU_READ_PREFETCH));
36549412SAleksandr.Guzovskiy@Sun.COM 
36559412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
36569412SAleksandr.Guzovskiy@Sun.COM 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
36579412SAleksandr.Guzovskiy@Sun.COM 
36589412SAleksandr.Guzovskiy@Sun.COM 			umem_free(packcheck, packsize);
36599412SAleksandr.Guzovskiy@Sun.COM 			umem_free(bigcheck, bigsize);
36609412SAleksandr.Guzovskiy@Sun.COM 		}
36619412SAleksandr.Guzovskiy@Sun.COM 		if (i == 2) {
36629412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_open(dmu_objset_pool(os), 0);
36639412SAleksandr.Guzovskiy@Sun.COM 		} else if (i == 3) {
36649412SAleksandr.Guzovskiy@Sun.COM 			txg_wait_synced(dmu_objset_pool(os), 0);
36659412SAleksandr.Guzovskiy@Sun.COM 		}
36669412SAleksandr.Guzovskiy@Sun.COM 	}
36679412SAleksandr.Guzovskiy@Sun.COM 
36689412SAleksandr.Guzovskiy@Sun.COM 	dmu_buf_rele(bonus_db, FTAG);
36699412SAleksandr.Guzovskiy@Sun.COM 	umem_free(packbuf, packsize);
36709412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf, bigsize);
36719412SAleksandr.Guzovskiy@Sun.COM 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
36729412SAleksandr.Guzovskiy@Sun.COM }
36739412SAleksandr.Guzovskiy@Sun.COM 
367410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
36759412SAleksandr.Guzovskiy@Sun.COM void
ztest_dmu_write_parallel(ztest_ds_t * zd,uint64_t id)367610922SJeff.Bonwick@Sun.COM ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
36773711Smaybee {
367810922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
367910922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
368010922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
36813711Smaybee 
36823711Smaybee 	/*
368310922SJeff.Bonwick@Sun.COM 	 * Have multiple threads write to large offsets in an object
368410922SJeff.Bonwick@Sun.COM 	 * to verify that parallel writes to an object -- even to the
368510922SJeff.Bonwick@Sun.COM 	 * same blocks within the object -- doesn't cause any trouble.
36863711Smaybee 	 */
368710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
368810922SJeff.Bonwick@Sun.COM 
368910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
369010922SJeff.Bonwick@Sun.COM 		return;
369110922SJeff.Bonwick@Sun.COM 
369210922SJeff.Bonwick@Sun.COM 	while (ztest_random(10) != 0)
369310922SJeff.Bonwick@Sun.COM 		ztest_io(zd, od[0].od_object, offset);
36943711Smaybee }
36953711Smaybee 
36963711Smaybee void
ztest_dmu_prealloc(ztest_ds_t * zd,uint64_t id)369710922SJeff.Bonwick@Sun.COM ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3698789Sahrens {
369910922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
370010922SJeff.Bonwick@Sun.COM 	uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
370110922SJeff.Bonwick@Sun.COM 	    (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
370210922SJeff.Bonwick@Sun.COM 	uint64_t count = ztest_random(20) + 1;
370310922SJeff.Bonwick@Sun.COM 	uint64_t blocksize = ztest_random_blocksize();
370410922SJeff.Bonwick@Sun.COM 	void *data;
370510922SJeff.Bonwick@Sun.COM 
370610922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
370710922SJeff.Bonwick@Sun.COM 
370810922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
37095530Sbonwick 		return;
371010922SJeff.Bonwick@Sun.COM 
371110922SJeff.Bonwick@Sun.COM 	if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
371210922SJeff.Bonwick@Sun.COM 		return;
371310922SJeff.Bonwick@Sun.COM 
371410922SJeff.Bonwick@Sun.COM 	ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
371510922SJeff.Bonwick@Sun.COM 
371610922SJeff.Bonwick@Sun.COM 	data = umem_zalloc(blocksize, UMEM_NOFAIL);
371710922SJeff.Bonwick@Sun.COM 
371810922SJeff.Bonwick@Sun.COM 	while (ztest_random(count) != 0) {
371910922SJeff.Bonwick@Sun.COM 		uint64_t randoff = offset + (ztest_random(count) * blocksize);
372010922SJeff.Bonwick@Sun.COM 		if (ztest_write(zd, od[0].od_object, randoff, blocksize,
372110922SJeff.Bonwick@Sun.COM 		    data) != 0)
372210922SJeff.Bonwick@Sun.COM 			break;
372310922SJeff.Bonwick@Sun.COM 		while (ztest_random(4) != 0)
372410922SJeff.Bonwick@Sun.COM 			ztest_io(zd, od[0].od_object, randoff);
37255530Sbonwick 	}
372610922SJeff.Bonwick@Sun.COM 
372710922SJeff.Bonwick@Sun.COM 	umem_free(data, blocksize);
3728789Sahrens }
3729789Sahrens 
3730789Sahrens /*
3731789Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
3732789Sahrens  */
3733789Sahrens #define	ZTEST_ZAP_MIN_INTS	1
3734789Sahrens #define	ZTEST_ZAP_MAX_INTS	4
3735789Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
3736789Sahrens 
3737789Sahrens void
ztest_zap(ztest_ds_t * zd,uint64_t id)373810922SJeff.Bonwick@Sun.COM ztest_zap(ztest_ds_t *zd, uint64_t id)
3739789Sahrens {
374010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
374110922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3742789Sahrens 	uint64_t object;
3743789Sahrens 	uint64_t txg, last_txg;
3744789Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
3745789Sahrens 	uint64_t zl_ints, zl_intsize, prop;
3746789Sahrens 	int i, ints;
3747789Sahrens 	dmu_tx_t *tx;
3748789Sahrens 	char propname[100], txgname[100];
3749789Sahrens 	int error;
3750789Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3751789Sahrens 
375210922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
375310922SJeff.Bonwick@Sun.COM 
375410922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
375510922SJeff.Bonwick@Sun.COM 		return;
375610922SJeff.Bonwick@Sun.COM 
375710922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
3758789Sahrens 
3759789Sahrens 	/*
376010922SJeff.Bonwick@Sun.COM 	 * Generate a known hash collision, and verify that
376110922SJeff.Bonwick@Sun.COM 	 * we can lookup and remove both entries.
3762789Sahrens 	 */
376310922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
376410922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
376510922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
376610922SJeff.Bonwick@Sun.COM 	if (txg == 0)
376710922SJeff.Bonwick@Sun.COM 		return;
376810922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
376910922SJeff.Bonwick@Sun.COM 		value[i] = i;
377010922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
377110922SJeff.Bonwick@Sun.COM 		    1, &value[i], tx));
3772789Sahrens 	}
377310922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
377410922SJeff.Bonwick@Sun.COM 		VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
377510922SJeff.Bonwick@Sun.COM 		    sizeof (uint64_t), 1, &value[i], tx));
377610922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==,
377710922SJeff.Bonwick@Sun.COM 		    zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
377810922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
377910922SJeff.Bonwick@Sun.COM 		ASSERT3U(zl_ints, ==, 1);
378010922SJeff.Bonwick@Sun.COM 	}
378110922SJeff.Bonwick@Sun.COM 	for (i = 0; i < 2; i++) {
378210922SJeff.Bonwick@Sun.COM 		VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
378310922SJeff.Bonwick@Sun.COM 	}
378410922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
378510922SJeff.Bonwick@Sun.COM 
378610922SJeff.Bonwick@Sun.COM 	/*
378710922SJeff.Bonwick@Sun.COM 	 * Generate a buch of random entries.
378810922SJeff.Bonwick@Sun.COM 	 */
3789789Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3790789Sahrens 
37915530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
37925530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
37935530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
37945530Sbonwick 	bzero(value, sizeof (value));
37955530Sbonwick 	last_txg = 0;
37965530Sbonwick 
37975530Sbonwick 	/*
37985530Sbonwick 	 * If these zap entries already exist, validate their contents.
37995530Sbonwick 	 */
38005530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38015530Sbonwick 	if (error == 0) {
38025530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
38035530Sbonwick 		ASSERT3U(zl_ints, ==, 1);
38045530Sbonwick 
38055530Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
38065530Sbonwick 		    zl_ints, &last_txg) == 0);
38075530Sbonwick 
38085530Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
38095530Sbonwick 		    &zl_ints) == 0);
38105530Sbonwick 
38115530Sbonwick 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
38125530Sbonwick 		ASSERT3U(zl_ints, ==, ints);
38135530Sbonwick 
38145530Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
38155530Sbonwick 		    zl_ints, value) == 0);
38165530Sbonwick 
38175530Sbonwick 		for (i = 0; i < ints; i++) {
38185530Sbonwick 			ASSERT3U(value[i], ==, last_txg + object + i);
3819789Sahrens 		}
38205530Sbonwick 	} else {
38215530Sbonwick 		ASSERT3U(error, ==, ENOENT);
38225530Sbonwick 	}
38235530Sbonwick 
38245530Sbonwick 	/*
38255530Sbonwick 	 * Atomically update two entries in our zap object.
38265530Sbonwick 	 * The first is named txg_%llu, and contains the txg
38275530Sbonwick 	 * in which the property was last updated.  The second
38285530Sbonwick 	 * is named prop_%llu, and the nth element of its value
38295530Sbonwick 	 * should be txg + object + n.
38305530Sbonwick 	 */
38315530Sbonwick 	tx = dmu_tx_create(os);
383210922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
383310922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
383410922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38355530Sbonwick 		return;
38365530Sbonwick 
38375530Sbonwick 	if (last_txg > txg)
38385530Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
38395530Sbonwick 
38405530Sbonwick 	for (i = 0; i < ints; i++)
38415530Sbonwick 		value[i] = txg + object + i;
38425530Sbonwick 
384310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
384410922SJeff.Bonwick@Sun.COM 	    1, &txg, tx));
384510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
384610922SJeff.Bonwick@Sun.COM 	    ints, value, tx));
38475530Sbonwick 
38485530Sbonwick 	dmu_tx_commit(tx);
38495530Sbonwick 
38505530Sbonwick 	/*
38515530Sbonwick 	 * Remove a random pair of entries.
38525530Sbonwick 	 */
38535530Sbonwick 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
38545530Sbonwick 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
38555530Sbonwick 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
38565530Sbonwick 
38575530Sbonwick 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
38585530Sbonwick 
38595530Sbonwick 	if (error == ENOENT)
38605530Sbonwick 		return;
38615530Sbonwick 
38625530Sbonwick 	ASSERT3U(error, ==, 0);
38635530Sbonwick 
38645530Sbonwick 	tx = dmu_tx_create(os);
386510922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
386610922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
386710922SJeff.Bonwick@Sun.COM 	if (txg == 0)
38685530Sbonwick 		return;
386910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
387010922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3871789Sahrens 	dmu_tx_commit(tx);
3872789Sahrens }
3873789Sahrens 
387410431SSanjeev.Bagewadi@Sun.COM /*
387510431SSanjeev.Bagewadi@Sun.COM  * Testcase to test the upgrading of a microzap to fatzap.
387610431SSanjeev.Bagewadi@Sun.COM  */
387710431SSanjeev.Bagewadi@Sun.COM void
ztest_fzap(ztest_ds_t * zd,uint64_t id)387810922SJeff.Bonwick@Sun.COM ztest_fzap(ztest_ds_t *zd, uint64_t id)
387910431SSanjeev.Bagewadi@Sun.COM {
388010922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
388110922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
388210922SJeff.Bonwick@Sun.COM 	uint64_t object, txg;
388310922SJeff.Bonwick@Sun.COM 
388410922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
388510922SJeff.Bonwick@Sun.COM 
388610922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
388710922SJeff.Bonwick@Sun.COM 		return;
388810922SJeff.Bonwick@Sun.COM 
388910922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
389010431SSanjeev.Bagewadi@Sun.COM 
389110431SSanjeev.Bagewadi@Sun.COM 	/*
389210922SJeff.Bonwick@Sun.COM 	 * Add entries to this ZAP and make sure it spills over
389310922SJeff.Bonwick@Sun.COM 	 * and gets upgraded to a fatzap. Also, since we are adding
389410922SJeff.Bonwick@Sun.COM 	 * 2050 entries we should see ptrtbl growth and leaf-block split.
389510431SSanjeev.Bagewadi@Sun.COM 	 */
389610922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 2050; i++) {
389710922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
389810922SJeff.Bonwick@Sun.COM 		uint64_t value = i;
389910922SJeff.Bonwick@Sun.COM 		dmu_tx_t *tx;
390010922SJeff.Bonwick@Sun.COM 		int error;
390110922SJeff.Bonwick@Sun.COM 
390210922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
390310922SJeff.Bonwick@Sun.COM 		    id, value);
390410431SSanjeev.Bagewadi@Sun.COM 
390510431SSanjeev.Bagewadi@Sun.COM 		tx = dmu_tx_create(os);
390610922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, name);
390710922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
390810922SJeff.Bonwick@Sun.COM 		if (txg == 0)
390910431SSanjeev.Bagewadi@Sun.COM 			return;
391010922SJeff.Bonwick@Sun.COM 		error = zap_add(os, object, name, sizeof (uint64_t), 1,
391110922SJeff.Bonwick@Sun.COM 		    &value, tx);
391210431SSanjeev.Bagewadi@Sun.COM 		ASSERT(error == 0 || error == EEXIST);
391310431SSanjeev.Bagewadi@Sun.COM 		dmu_tx_commit(tx);
391410431SSanjeev.Bagewadi@Sun.COM 	}
391510431SSanjeev.Bagewadi@Sun.COM }
391610431SSanjeev.Bagewadi@Sun.COM 
391710922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3918789Sahrens void
ztest_zap_parallel(ztest_ds_t * zd,uint64_t id)391910922SJeff.Bonwick@Sun.COM ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3920789Sahrens {
392110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
392210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
3923789Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3924789Sahrens 	dmu_tx_t *tx;
3925789Sahrens 	int i, namelen, error;
392610922SJeff.Bonwick@Sun.COM 	int micro = ztest_random(2);
3927789Sahrens 	char name[20], string_value[20];
3928789Sahrens 	void *data;
3929789Sahrens 
393010922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
393110922SJeff.Bonwick@Sun.COM 
393210922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
393310922SJeff.Bonwick@Sun.COM 		return;
393410922SJeff.Bonwick@Sun.COM 
393510922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
393610922SJeff.Bonwick@Sun.COM 
39375530Sbonwick 	/*
39385530Sbonwick 	 * Generate a random name of the form 'xxx.....' where each
39395530Sbonwick 	 * x is a random printable character and the dots are dots.
39405530Sbonwick 	 * There are 94 such characters, and the name length goes from
39415530Sbonwick 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
39425530Sbonwick 	 */
39435530Sbonwick 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
39445530Sbonwick 
39455530Sbonwick 	for (i = 0; i < 3; i++)
39465530Sbonwick 		name[i] = '!' + ztest_random('~' - '!' + 1);
39475530Sbonwick 	for (; i < namelen - 1; i++)
39485530Sbonwick 		name[i] = '.';
39495530Sbonwick 	name[i] = '\0';
39505530Sbonwick 
395110922SJeff.Bonwick@Sun.COM 	if ((namelen & 1) || micro) {
39525530Sbonwick 		wsize = sizeof (txg);
39535530Sbonwick 		wc = 1;
39545530Sbonwick 		data = &txg;
39555530Sbonwick 	} else {
39565530Sbonwick 		wsize = 1;
39575530Sbonwick 		wc = namelen;
39585530Sbonwick 		data = string_value;
39595530Sbonwick 	}
39605530Sbonwick 
39615530Sbonwick 	count = -1ULL;
39625530Sbonwick 	VERIFY(zap_count(os, object, &count) == 0);
39635530Sbonwick 	ASSERT(count != -1ULL);
39645530Sbonwick 
39655530Sbonwick 	/*
39665530Sbonwick 	 * Select an operation: length, lookup, add, update, remove.
39675530Sbonwick 	 */
39685530Sbonwick 	i = ztest_random(5);
39695530Sbonwick 
39705530Sbonwick 	if (i >= 2) {
39715530Sbonwick 		tx = dmu_tx_create(os);
397210922SJeff.Bonwick@Sun.COM 		dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
397310922SJeff.Bonwick@Sun.COM 		txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
397410922SJeff.Bonwick@Sun.COM 		if (txg == 0)
39755530Sbonwick 			return;
39765530Sbonwick 		bcopy(name, string_value, namelen);
39775530Sbonwick 	} else {
39785530Sbonwick 		tx = NULL;
39795530Sbonwick 		txg = 0;
39805530Sbonwick 		bzero(string_value, namelen);
39815530Sbonwick 	}
39825530Sbonwick 
39835530Sbonwick 	switch (i) {
39845530Sbonwick 
39855530Sbonwick 	case 0:
39865530Sbonwick 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
39875530Sbonwick 		if (error == 0) {
39885530Sbonwick 			ASSERT3U(wsize, ==, zl_wsize);
39895530Sbonwick 			ASSERT3U(wc, ==, zl_wc);
3990789Sahrens 		} else {
39915530Sbonwick 			ASSERT3U(error, ==, ENOENT);
3992789Sahrens 		}
39935530Sbonwick 		break;
39945530Sbonwick 
39955530Sbonwick 	case 1:
39965530Sbonwick 		error = zap_lookup(os, object, name, wsize, wc, data);
39975530Sbonwick 		if (error == 0) {
39985530Sbonwick 			if (data == string_value &&
39995530Sbonwick 			    bcmp(name, data, namelen) != 0)
40005530Sbonwick 				fatal(0, "name '%s' != val '%s' len %d",
40015530Sbonwick 				    name, data, namelen);
40025530Sbonwick 		} else {
40035530Sbonwick 			ASSERT3U(error, ==, ENOENT);
4004789Sahrens 		}
40055530Sbonwick 		break;
40065530Sbonwick 
40075530Sbonwick 	case 2:
40085530Sbonwick 		error = zap_add(os, object, name, wsize, wc, data, tx);
40095530Sbonwick 		ASSERT(error == 0 || error == EEXIST);
40105530Sbonwick 		break;
40115530Sbonwick 
40125530Sbonwick 	case 3:
40135530Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
40145530Sbonwick 		break;
40155530Sbonwick 
40165530Sbonwick 	case 4:
40175530Sbonwick 		error = zap_remove(os, object, name, tx);
40185530Sbonwick 		ASSERT(error == 0 || error == ENOENT);
40195530Sbonwick 		break;
4020789Sahrens 	}
40215530Sbonwick 
40225530Sbonwick 	if (tx != NULL)
40235530Sbonwick 		dmu_tx_commit(tx);
4024789Sahrens }
4025789Sahrens 
402610612SRicardo.M.Correia@Sun.COM /*
402710612SRicardo.M.Correia@Sun.COM  * Commit callback data.
402810612SRicardo.M.Correia@Sun.COM  */
402910612SRicardo.M.Correia@Sun.COM typedef struct ztest_cb_data {
403010612SRicardo.M.Correia@Sun.COM 	list_node_t		zcd_node;
403110612SRicardo.M.Correia@Sun.COM 	uint64_t		zcd_txg;
403210612SRicardo.M.Correia@Sun.COM 	int			zcd_expected_err;
403310612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_added;
403410612SRicardo.M.Correia@Sun.COM 	boolean_t		zcd_called;
403510612SRicardo.M.Correia@Sun.COM 	spa_t			*zcd_spa;
403610612SRicardo.M.Correia@Sun.COM } ztest_cb_data_t;
403710612SRicardo.M.Correia@Sun.COM 
403810612SRicardo.M.Correia@Sun.COM /* This is the actual commit callback function */
403910612SRicardo.M.Correia@Sun.COM static void
ztest_commit_callback(void * arg,int error)404010612SRicardo.M.Correia@Sun.COM ztest_commit_callback(void *arg, int error)
404110612SRicardo.M.Correia@Sun.COM {
404210612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *data = arg;
404310612SRicardo.M.Correia@Sun.COM 	uint64_t synced_txg;
404410612SRicardo.M.Correia@Sun.COM 
404510612SRicardo.M.Correia@Sun.COM 	VERIFY(data != NULL);
404610612SRicardo.M.Correia@Sun.COM 	VERIFY3S(data->zcd_expected_err, ==, error);
404710612SRicardo.M.Correia@Sun.COM 	VERIFY(!data->zcd_called);
404810612SRicardo.M.Correia@Sun.COM 
404910612SRicardo.M.Correia@Sun.COM 	synced_txg = spa_last_synced_txg(data->zcd_spa);
405010612SRicardo.M.Correia@Sun.COM 	if (data->zcd_txg > synced_txg)
405110612SRicardo.M.Correia@Sun.COM 		fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
405210612SRicardo.M.Correia@Sun.COM 		    ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
405310612SRicardo.M.Correia@Sun.COM 		    synced_txg);
405410612SRicardo.M.Correia@Sun.COM 
405510612SRicardo.M.Correia@Sun.COM 	data->zcd_called = B_TRUE;
405610612SRicardo.M.Correia@Sun.COM 
405710612SRicardo.M.Correia@Sun.COM 	if (error == ECANCELED) {
405810612SRicardo.M.Correia@Sun.COM 		ASSERT3U(data->zcd_txg, ==, 0);
405910612SRicardo.M.Correia@Sun.COM 		ASSERT(!data->zcd_added);
406010612SRicardo.M.Correia@Sun.COM 
406110612SRicardo.M.Correia@Sun.COM 		/*
406210612SRicardo.M.Correia@Sun.COM 		 * The private callback data should be destroyed here, but
406310612SRicardo.M.Correia@Sun.COM 		 * since we are going to check the zcd_called field after
406410612SRicardo.M.Correia@Sun.COM 		 * dmu_tx_abort(), we will destroy it there.
406510612SRicardo.M.Correia@Sun.COM 		 */
406610612SRicardo.M.Correia@Sun.COM 		return;
406710612SRicardo.M.Correia@Sun.COM 	}
406810612SRicardo.M.Correia@Sun.COM 
406910612SRicardo.M.Correia@Sun.COM 	/* Was this callback added to the global callback list? */
407010612SRicardo.M.Correia@Sun.COM 	if (!data->zcd_added)
407110612SRicardo.M.Correia@Sun.COM 		goto out;
407210612SRicardo.M.Correia@Sun.COM 
407310612SRicardo.M.Correia@Sun.COM 	ASSERT3U(data->zcd_txg, !=, 0);
407410612SRicardo.M.Correia@Sun.COM 
407510612SRicardo.M.Correia@Sun.COM 	/* Remove our callback from the list */
407610612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
407710612SRicardo.M.Correia@Sun.COM 	list_remove(&zcl.zcl_callbacks, data);
407810612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
407910612SRicardo.M.Correia@Sun.COM 
408010612SRicardo.M.Correia@Sun.COM out:
408110612SRicardo.M.Correia@Sun.COM 	umem_free(data, sizeof (ztest_cb_data_t));
408210612SRicardo.M.Correia@Sun.COM }
408310612SRicardo.M.Correia@Sun.COM 
408410612SRicardo.M.Correia@Sun.COM /* Allocate and initialize callback data structure */
408510612SRicardo.M.Correia@Sun.COM static ztest_cb_data_t *
ztest_create_cb_data(objset_t * os,uint64_t txg)408610612SRicardo.M.Correia@Sun.COM ztest_create_cb_data(objset_t *os, uint64_t txg)
408710612SRicardo.M.Correia@Sun.COM {
408810612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data;
408910612SRicardo.M.Correia@Sun.COM 
409010612SRicardo.M.Correia@Sun.COM 	cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
409110612SRicardo.M.Correia@Sun.COM 
409210612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_txg = txg;
409310612SRicardo.M.Correia@Sun.COM 	cb_data->zcd_spa = dmu_objset_spa(os);
409410612SRicardo.M.Correia@Sun.COM 
409510612SRicardo.M.Correia@Sun.COM 	return (cb_data);
409610612SRicardo.M.Correia@Sun.COM }
409710612SRicardo.M.Correia@Sun.COM 
409810612SRicardo.M.Correia@Sun.COM /*
409910612SRicardo.M.Correia@Sun.COM  * If a number of txgs equal to this threshold have been created after a commit
410010612SRicardo.M.Correia@Sun.COM  * callback has been registered but not called, then we assume there is an
410110612SRicardo.M.Correia@Sun.COM  * implementation bug.
410210612SRicardo.M.Correia@Sun.COM  */
410310612SRicardo.M.Correia@Sun.COM #define	ZTEST_COMMIT_CALLBACK_THRESH	(TXG_CONCURRENT_STATES + 2)
410410612SRicardo.M.Correia@Sun.COM 
410510612SRicardo.M.Correia@Sun.COM /*
410610612SRicardo.M.Correia@Sun.COM  * Commit callback test.
410710612SRicardo.M.Correia@Sun.COM  */
410810612SRicardo.M.Correia@Sun.COM void
ztest_dmu_commit_callbacks(ztest_ds_t * zd,uint64_t id)410910922SJeff.Bonwick@Sun.COM ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
411010612SRicardo.M.Correia@Sun.COM {
411110922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
411210922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
411310612SRicardo.M.Correia@Sun.COM 	dmu_tx_t *tx;
411410612SRicardo.M.Correia@Sun.COM 	ztest_cb_data_t *cb_data[3], *tmp_cb;
411510612SRicardo.M.Correia@Sun.COM 	uint64_t old_txg, txg;
411610612SRicardo.M.Correia@Sun.COM 	int i, error;
411710612SRicardo.M.Correia@Sun.COM 
411810922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
411910922SJeff.Bonwick@Sun.COM 
412010922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
412110922SJeff.Bonwick@Sun.COM 		return;
412210922SJeff.Bonwick@Sun.COM 
412310612SRicardo.M.Correia@Sun.COM 	tx = dmu_tx_create(os);
412410612SRicardo.M.Correia@Sun.COM 
412510612SRicardo.M.Correia@Sun.COM 	cb_data[0] = ztest_create_cb_data(os, 0);
412610612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
412710612SRicardo.M.Correia@Sun.COM 
412810922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
412910612SRicardo.M.Correia@Sun.COM 
413010612SRicardo.M.Correia@Sun.COM 	/* Every once in a while, abort the transaction on purpose */
413110612SRicardo.M.Correia@Sun.COM 	if (ztest_random(100) == 0)
413210612SRicardo.M.Correia@Sun.COM 		error = -1;
413310612SRicardo.M.Correia@Sun.COM 
413410612SRicardo.M.Correia@Sun.COM 	if (!error)
413510612SRicardo.M.Correia@Sun.COM 		error = dmu_tx_assign(tx, TXG_NOWAIT);
413610612SRicardo.M.Correia@Sun.COM 
413710612SRicardo.M.Correia@Sun.COM 	txg = error ? 0 : dmu_tx_get_txg(tx);
413810612SRicardo.M.Correia@Sun.COM 
413910612SRicardo.M.Correia@Sun.COM 	cb_data[0]->zcd_txg = txg;
414010612SRicardo.M.Correia@Sun.COM 	cb_data[1] = ztest_create_cb_data(os, txg);
414110612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
414210612SRicardo.M.Correia@Sun.COM 
414310612SRicardo.M.Correia@Sun.COM 	if (error) {
414410612SRicardo.M.Correia@Sun.COM 		/*
414510612SRicardo.M.Correia@Sun.COM 		 * It's not a strict requirement to call the registered
414610612SRicardo.M.Correia@Sun.COM 		 * callbacks from inside dmu_tx_abort(), but that's what
414710612SRicardo.M.Correia@Sun.COM 		 * it's supposed to happen in the current implementation
414810612SRicardo.M.Correia@Sun.COM 		 * so we will check for that.
414910612SRicardo.M.Correia@Sun.COM 		 */
415010612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
415110612SRicardo.M.Correia@Sun.COM 			cb_data[i]->zcd_expected_err = ECANCELED;
415210612SRicardo.M.Correia@Sun.COM 			VERIFY(!cb_data[i]->zcd_called);
415310612SRicardo.M.Correia@Sun.COM 		}
415410612SRicardo.M.Correia@Sun.COM 
415510612SRicardo.M.Correia@Sun.COM 		dmu_tx_abort(tx);
415610612SRicardo.M.Correia@Sun.COM 
415710612SRicardo.M.Correia@Sun.COM 		for (i = 0; i < 2; i++) {
415810612SRicardo.M.Correia@Sun.COM 			VERIFY(cb_data[i]->zcd_called);
415910612SRicardo.M.Correia@Sun.COM 			umem_free(cb_data[i], sizeof (ztest_cb_data_t));
416010612SRicardo.M.Correia@Sun.COM 		}
416110612SRicardo.M.Correia@Sun.COM 
416210612SRicardo.M.Correia@Sun.COM 		return;
416310612SRicardo.M.Correia@Sun.COM 	}
416410612SRicardo.M.Correia@Sun.COM 
416510612SRicardo.M.Correia@Sun.COM 	cb_data[2] = ztest_create_cb_data(os, txg);
416610612SRicardo.M.Correia@Sun.COM 	dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
416710612SRicardo.M.Correia@Sun.COM 
416810612SRicardo.M.Correia@Sun.COM 	/*
416910612SRicardo.M.Correia@Sun.COM 	 * Read existing data to make sure there isn't a future leak.
417010612SRicardo.M.Correia@Sun.COM 	 */
417110922SJeff.Bonwick@Sun.COM 	VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
417210612SRicardo.M.Correia@Sun.COM 	    &old_txg, DMU_READ_PREFETCH));
417310612SRicardo.M.Correia@Sun.COM 
417410612SRicardo.M.Correia@Sun.COM 	if (old_txg > txg)
417510612SRicardo.M.Correia@Sun.COM 		fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
417610612SRicardo.M.Correia@Sun.COM 		    old_txg, txg);
417710612SRicardo.M.Correia@Sun.COM 
417810922SJeff.Bonwick@Sun.COM 	dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
417910612SRicardo.M.Correia@Sun.COM 
418010612SRicardo.M.Correia@Sun.COM 	(void) mutex_lock(&zcl.zcl_callbacks_lock);
418110612SRicardo.M.Correia@Sun.COM 
418210612SRicardo.M.Correia@Sun.COM 	/*
418310612SRicardo.M.Correia@Sun.COM 	 * Since commit callbacks don't have any ordering requirement and since
418410612SRicardo.M.Correia@Sun.COM 	 * it is theoretically possible for a commit callback to be called
418510612SRicardo.M.Correia@Sun.COM 	 * after an arbitrary amount of time has elapsed since its txg has been
418610612SRicardo.M.Correia@Sun.COM 	 * synced, it is difficult to reliably determine whether a commit
418710612SRicardo.M.Correia@Sun.COM 	 * callback hasn't been called due to high load or due to a flawed
418810612SRicardo.M.Correia@Sun.COM 	 * implementation.
418910612SRicardo.M.Correia@Sun.COM 	 *
419010612SRicardo.M.Correia@Sun.COM 	 * In practice, we will assume that if after a certain number of txgs a
419110612SRicardo.M.Correia@Sun.COM 	 * commit callback hasn't been called, then most likely there's an
419210612SRicardo.M.Correia@Sun.COM 	 * implementation bug..
419310612SRicardo.M.Correia@Sun.COM 	 */
419410612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_head(&zcl.zcl_callbacks);
419510612SRicardo.M.Correia@Sun.COM 	if (tmp_cb != NULL &&
419610612SRicardo.M.Correia@Sun.COM 	    tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
419710612SRicardo.M.Correia@Sun.COM 		fatal(0, "Commit callback threshold exceeded, oldest txg: %"
419810612SRicardo.M.Correia@Sun.COM 		    PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
419910612SRicardo.M.Correia@Sun.COM 	}
420010612SRicardo.M.Correia@Sun.COM 
420110612SRicardo.M.Correia@Sun.COM 	/*
420210612SRicardo.M.Correia@Sun.COM 	 * Let's find the place to insert our callbacks.
420310612SRicardo.M.Correia@Sun.COM 	 *
420410612SRicardo.M.Correia@Sun.COM 	 * Even though the list is ordered by txg, it is possible for the
420510612SRicardo.M.Correia@Sun.COM 	 * insertion point to not be the end because our txg may already be
420610612SRicardo.M.Correia@Sun.COM 	 * quiescing at this point and other callbacks in the open txg
420710612SRicardo.M.Correia@Sun.COM 	 * (from other objsets) may have sneaked in.
420810612SRicardo.M.Correia@Sun.COM 	 */
420910612SRicardo.M.Correia@Sun.COM 	tmp_cb = list_tail(&zcl.zcl_callbacks);
421010612SRicardo.M.Correia@Sun.COM 	while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
421110612SRicardo.M.Correia@Sun.COM 		tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
421210612SRicardo.M.Correia@Sun.COM 
421310612SRicardo.M.Correia@Sun.COM 	/* Add the 3 callbacks to the list */
421410612SRicardo.M.Correia@Sun.COM 	for (i = 0; i < 3; i++) {
421510612SRicardo.M.Correia@Sun.COM 		if (tmp_cb == NULL)
421610612SRicardo.M.Correia@Sun.COM 			list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
421710612SRicardo.M.Correia@Sun.COM 		else
421810612SRicardo.M.Correia@Sun.COM 			list_insert_after(&zcl.zcl_callbacks, tmp_cb,
421910612SRicardo.M.Correia@Sun.COM 			    cb_data[i]);
422010612SRicardo.M.Correia@Sun.COM 
422110612SRicardo.M.Correia@Sun.COM 		cb_data[i]->zcd_added = B_TRUE;
422210612SRicardo.M.Correia@Sun.COM 		VERIFY(!cb_data[i]->zcd_called);
422310612SRicardo.M.Correia@Sun.COM 
422410612SRicardo.M.Correia@Sun.COM 		tmp_cb = cb_data[i];
422510612SRicardo.M.Correia@Sun.COM 	}
422610612SRicardo.M.Correia@Sun.COM 
422710612SRicardo.M.Correia@Sun.COM 	(void) mutex_unlock(&zcl.zcl_callbacks_lock);
422810612SRicardo.M.Correia@Sun.COM 
422910612SRicardo.M.Correia@Sun.COM 	dmu_tx_commit(tx);
423010612SRicardo.M.Correia@Sun.COM }
423110612SRicardo.M.Correia@Sun.COM 
423210922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4233789Sahrens void
ztest_dsl_prop_get_set(ztest_ds_t * zd,uint64_t id)423410922SJeff.Bonwick@Sun.COM ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4235789Sahrens {
423610922SJeff.Bonwick@Sun.COM 	zfs_prop_t proplist[] = {
423710922SJeff.Bonwick@Sun.COM 		ZFS_PROP_CHECKSUM,
423810922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COMPRESSION,
423910922SJeff.Bonwick@Sun.COM 		ZFS_PROP_COPIES,
424010922SJeff.Bonwick@Sun.COM 		ZFS_PROP_DEDUP
424110922SJeff.Bonwick@Sun.COM 	};
424210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
424310922SJeff.Bonwick@Sun.COM 
424410922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
424510922SJeff.Bonwick@Sun.COM 
424610922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
424710922SJeff.Bonwick@Sun.COM 		(void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
424810922SJeff.Bonwick@Sun.COM 		    ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
424910922SJeff.Bonwick@Sun.COM 
425010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
425110922SJeff.Bonwick@Sun.COM }
425210922SJeff.Bonwick@Sun.COM 
425310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
425410922SJeff.Bonwick@Sun.COM void
ztest_spa_prop_get_set(ztest_ds_t * zd,uint64_t id)425510922SJeff.Bonwick@Sun.COM ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
425610922SJeff.Bonwick@Sun.COM {
425710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
425810922SJeff.Bonwick@Sun.COM 	nvlist_t *props = NULL;
425910922SJeff.Bonwick@Sun.COM 
426010922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
426110922SJeff.Bonwick@Sun.COM 
426210922SJeff.Bonwick@Sun.COM 	(void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
426310922SJeff.Bonwick@Sun.COM 	    ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
426410922SJeff.Bonwick@Sun.COM 
426510922SJeff.Bonwick@Sun.COM 	VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
426610922SJeff.Bonwick@Sun.COM 
426710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
426810922SJeff.Bonwick@Sun.COM 		dump_nvlist(props, 4);
426910922SJeff.Bonwick@Sun.COM 
427010922SJeff.Bonwick@Sun.COM 	nvlist_free(props);
427110922SJeff.Bonwick@Sun.COM 
427210922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4273789Sahrens }
4274789Sahrens 
4275789Sahrens /*
427610693Schris.kirby@sun.com  * Test snapshot hold/release and deferred destroy.
427710693Schris.kirby@sun.com  */
427810693Schris.kirby@sun.com void
ztest_dmu_snapshot_hold(ztest_ds_t * zd,uint64_t id)427910922SJeff.Bonwick@Sun.COM ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
428010693Schris.kirby@sun.com {
428110693Schris.kirby@sun.com 	int error;
428210922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
428310693Schris.kirby@sun.com 	objset_t *origin;
428410693Schris.kirby@sun.com 	char snapname[100];
428510693Schris.kirby@sun.com 	char fullname[100];
428610693Schris.kirby@sun.com 	char clonename[100];
428710693Schris.kirby@sun.com 	char tag[100];
428810693Schris.kirby@sun.com 	char osname[MAXNAMELEN];
428910693Schris.kirby@sun.com 
429010693Schris.kirby@sun.com 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
429110693Schris.kirby@sun.com 
429210693Schris.kirby@sun.com 	dmu_objset_name(os, osname);
429310693Schris.kirby@sun.com 
429410922SJeff.Bonwick@Sun.COM 	(void) snprintf(snapname, 100, "sh1_%llu", id);
429510693Schris.kirby@sun.com 	(void) snprintf(fullname, 100, "%s@%s", osname, snapname);
429610922SJeff.Bonwick@Sun.COM 	(void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
429710922SJeff.Bonwick@Sun.COM 	(void) snprintf(tag, 100, "%tag_%llu", id);
429810693Schris.kirby@sun.com 
429910693Schris.kirby@sun.com 	/*
430010693Schris.kirby@sun.com 	 * Clean up from any previous run.
430110693Schris.kirby@sun.com 	 */
430210693Schris.kirby@sun.com 	(void) dmu_objset_destroy(clonename, B_FALSE);
430310693Schris.kirby@sun.com 	(void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
430410693Schris.kirby@sun.com 	(void) dmu_objset_destroy(fullname, B_FALSE);
430510693Schris.kirby@sun.com 
430610693Schris.kirby@sun.com 	/*
430710693Schris.kirby@sun.com 	 * Create snapshot, clone it, mark snap for deferred destroy,
430810693Schris.kirby@sun.com 	 * destroy clone, verify snap was also destroyed.
430910693Schris.kirby@sun.com 	 */
4310*13043STim.Haley@Sun.COM 	error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4311*13043STim.Haley@Sun.COM 	    FALSE, -1);
431210693Schris.kirby@sun.com 	if (error) {
431310693Schris.kirby@sun.com 		if (error == ENOSPC) {
431410693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
431510693Schris.kirby@sun.com 			goto out;
431610693Schris.kirby@sun.com 		}
431710693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
431810693Schris.kirby@sun.com 	}
431910693Schris.kirby@sun.com 
432010693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
432110693Schris.kirby@sun.com 	if (error)
432210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
432310693Schris.kirby@sun.com 
432410693Schris.kirby@sun.com 	error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
432510693Schris.kirby@sun.com 	dmu_objset_rele(origin, FTAG);
432610693Schris.kirby@sun.com 	if (error) {
432710693Schris.kirby@sun.com 		if (error == ENOSPC) {
432810693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_clone");
432910693Schris.kirby@sun.com 			goto out;
433010693Schris.kirby@sun.com 		}
433110693Schris.kirby@sun.com 		fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
433210693Schris.kirby@sun.com 	}
433310693Schris.kirby@sun.com 
433410693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
433510693Schris.kirby@sun.com 	if (error) {
433610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
433710693Schris.kirby@sun.com 		    fullname, error);
433810693Schris.kirby@sun.com 	}
433910693Schris.kirby@sun.com 
434010693Schris.kirby@sun.com 	error = dmu_objset_destroy(clonename, B_FALSE);
434110693Schris.kirby@sun.com 	if (error)
434210693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
434310693Schris.kirby@sun.com 
434410693Schris.kirby@sun.com 	error = dmu_objset_hold(fullname, FTAG, &origin);
434510693Schris.kirby@sun.com 	if (error != ENOENT)
434610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
434710693Schris.kirby@sun.com 
434810693Schris.kirby@sun.com 	/*
434910693Schris.kirby@sun.com 	 * Create snapshot, add temporary hold, verify that we can't
435010693Schris.kirby@sun.com 	 * destroy a held snapshot, mark for deferred destroy,
435110693Schris.kirby@sun.com 	 * release hold, verify snapshot was destroyed.
435210693Schris.kirby@sun.com 	 */
4353*13043STim.Haley@Sun.COM 	error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4354*13043STim.Haley@Sun.COM 	    FALSE, -1);
435510693Schris.kirby@sun.com 	if (error) {
435610693Schris.kirby@sun.com 		if (error == ENOSPC) {
435710693Schris.kirby@sun.com 			ztest_record_enospc("dmu_objset_snapshot");
435810693Schris.kirby@sun.com 			goto out;
435910693Schris.kirby@sun.com 		}
436010693Schris.kirby@sun.com 		fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
436110693Schris.kirby@sun.com 	}
436210693Schris.kirby@sun.com 
436312527SChris.Kirby@oracle.com 	error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
436412527SChris.Kirby@oracle.com 	    B_TRUE, -1);
436510693Schris.kirby@sun.com 	if (error)
436610693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
436710693Schris.kirby@sun.com 
436810693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_FALSE);
436910693Schris.kirby@sun.com 	if (error != EBUSY) {
437010693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
437110693Schris.kirby@sun.com 		    fullname, error);
437210693Schris.kirby@sun.com 	}
437310693Schris.kirby@sun.com 
437410693Schris.kirby@sun.com 	error = dmu_objset_destroy(fullname, B_TRUE);
437510693Schris.kirby@sun.com 	if (error) {
437610693Schris.kirby@sun.com 		fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
437710693Schris.kirby@sun.com 		    fullname, error);
437810693Schris.kirby@sun.com 	}
437910693Schris.kirby@sun.com 
438010693Schris.kirby@sun.com 	error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
438110693Schris.kirby@sun.com 	if (error)
438210693Schris.kirby@sun.com 		fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
438310693Schris.kirby@sun.com 
438410693Schris.kirby@sun.com 	VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
438510693Schris.kirby@sun.com 
438610693Schris.kirby@sun.com out:
438710693Schris.kirby@sun.com 	(void) rw_unlock(&ztest_shared->zs_name_lock);
438810693Schris.kirby@sun.com }
438910693Schris.kirby@sun.com 
439010693Schris.kirby@sun.com /*
4391789Sahrens  * Inject random faults into the on-disk data.
4392789Sahrens  */
439310922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4394789Sahrens void
ztest_fault_inject(ztest_ds_t * zd,uint64_t id)439510922SJeff.Bonwick@Sun.COM ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4396789Sahrens {
439710922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
439810922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4399789Sahrens 	int fd;
4400789Sahrens 	uint64_t offset;
440111422SMark.Musante@Sun.COM 	uint64_t leaves;
4402789Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
4403789Sahrens 	uint64_t top, leaf;
4404789Sahrens 	char path0[MAXPATHLEN];
4405789Sahrens 	char pathrand[MAXPATHLEN];
4406789Sahrens 	size_t fsize;
4407789Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
4408789Sahrens 	int iters = 1000;
440911422SMark.Musante@Sun.COM 	int maxfaults;
441011422SMark.Musante@Sun.COM 	int mirror_save;
44117754SJeff.Bonwick@Sun.COM 	vdev_t *vd0 = NULL;
44121544Seschrock 	uint64_t guid0 = 0;
441310685SGeorge.Wilson@Sun.COM 	boolean_t islog = B_FALSE;
44141544Seschrock 
441511422SMark.Musante@Sun.COM 	VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
441611422SMark.Musante@Sun.COM 	maxfaults = MAXFAULTS();
441711422SMark.Musante@Sun.COM 	leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
441811422SMark.Musante@Sun.COM 	mirror_save = zs->zs_mirrors;
441911422SMark.Musante@Sun.COM 	VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
442011422SMark.Musante@Sun.COM 
44217754SJeff.Bonwick@Sun.COM 	ASSERT(leaves >= 1);
4422789Sahrens 
4423789Sahrens 	/*
44247754SJeff.Bonwick@Sun.COM 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4425789Sahrens 	 */
44267754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
44277754SJeff.Bonwick@Sun.COM 
44287754SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
44297754SJeff.Bonwick@Sun.COM 		/*
443010922SJeff.Bonwick@Sun.COM 		 * Inject errors on a normal data device or slog device.
44317754SJeff.Bonwick@Sun.COM 		 */
443210922SJeff.Bonwick@Sun.COM 		top = ztest_random_vdev_top(spa, B_TRUE);
443311422SMark.Musante@Sun.COM 		leaf = ztest_random(leaves) + zs->zs_splits;
44347754SJeff.Bonwick@Sun.COM 
44357754SJeff.Bonwick@Sun.COM 		/*
44367754SJeff.Bonwick@Sun.COM 		 * Generate paths to the first leaf in this top-level vdev,
44377754SJeff.Bonwick@Sun.COM 		 * and to the random leaf we selected.  We'll induce transient
44387754SJeff.Bonwick@Sun.COM 		 * write failures and random online/offline activity on leaf 0,
44397754SJeff.Bonwick@Sun.COM 		 * and we'll write random garbage to the randomly chosen leaf.
44407754SJeff.Bonwick@Sun.COM 		 */
44417754SJeff.Bonwick@Sun.COM 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
444211422SMark.Musante@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
44437754SJeff.Bonwick@Sun.COM 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
44447754SJeff.Bonwick@Sun.COM 		    zopt_dir, zopt_pool, top * leaves + leaf);
44457754SJeff.Bonwick@Sun.COM 
44467754SJeff.Bonwick@Sun.COM 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
444710685SGeorge.Wilson@Sun.COM 		if (vd0 != NULL && vd0->vdev_top->vdev_islog)
444810685SGeorge.Wilson@Sun.COM 			islog = B_TRUE;
444910685SGeorge.Wilson@Sun.COM 
44507754SJeff.Bonwick@Sun.COM 		if (vd0 != NULL && maxfaults != 1) {
44517754SJeff.Bonwick@Sun.COM 			/*
44527754SJeff.Bonwick@Sun.COM 			 * Make vd0 explicitly claim to be unreadable,
44537754SJeff.Bonwick@Sun.COM 			 * or unwriteable, or reach behind its back
44547754SJeff.Bonwick@Sun.COM 			 * and close the underlying fd.  We can do this if
44557754SJeff.Bonwick@Sun.COM 			 * maxfaults == 0 because we'll fail and reexecute,
44567754SJeff.Bonwick@Sun.COM 			 * and we can do it if maxfaults >= 2 because we'll
44577754SJeff.Bonwick@Sun.COM 			 * have enough redundancy.  If maxfaults == 1, the
44587754SJeff.Bonwick@Sun.COM 			 * combination of this with injection of random data
44597754SJeff.Bonwick@Sun.COM 			 * corruption below exceeds the pool's fault tolerance.
44607754SJeff.Bonwick@Sun.COM 			 */
44617754SJeff.Bonwick@Sun.COM 			vdev_file_t *vf = vd0->vdev_tsd;
44627754SJeff.Bonwick@Sun.COM 
44637754SJeff.Bonwick@Sun.COM 			if (vf != NULL && ztest_random(3) == 0) {
44647754SJeff.Bonwick@Sun.COM 				(void) close(vf->vf_vnode->v_fd);
44657754SJeff.Bonwick@Sun.COM 				vf->vf_vnode->v_fd = -1;
44667754SJeff.Bonwick@Sun.COM 			} else if (ztest_random(2) == 0) {
44677754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_read = B_TRUE;
44687754SJeff.Bonwick@Sun.COM 			} else {
44697754SJeff.Bonwick@Sun.COM 				vd0->vdev_cant_write = B_TRUE;
44707754SJeff.Bonwick@Sun.COM 			}
44717754SJeff.Bonwick@Sun.COM 			guid0 = vd0->vdev_guid;
44727754SJeff.Bonwick@Sun.COM 		}
44737754SJeff.Bonwick@Sun.COM 	} else {
44747754SJeff.Bonwick@Sun.COM 		/*
44757754SJeff.Bonwick@Sun.COM 		 * Inject errors on an l2cache device.
44767754SJeff.Bonwick@Sun.COM 		 */
44777754SJeff.Bonwick@Sun.COM 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
44787754SJeff.Bonwick@Sun.COM 
44797754SJeff.Bonwick@Sun.COM 		if (sav->sav_count == 0) {
44807754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_STATE, FTAG);
44817754SJeff.Bonwick@Sun.COM 			return;
44827754SJeff.Bonwick@Sun.COM 		}
44837754SJeff.Bonwick@Sun.COM 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
44847754SJeff.Bonwick@Sun.COM 		guid0 = vd0->vdev_guid;
44857754SJeff.Bonwick@Sun.COM 		(void) strcpy(path0, vd0->vdev_path);
44867754SJeff.Bonwick@Sun.COM 		(void) strcpy(pathrand, vd0->vdev_path);
44877754SJeff.Bonwick@Sun.COM 
44887754SJeff.Bonwick@Sun.COM 		leaf = 0;
44897754SJeff.Bonwick@Sun.COM 		leaves = 1;
44907754SJeff.Bonwick@Sun.COM 		maxfaults = INT_MAX;	/* no limit on cache devices */
44917754SJeff.Bonwick@Sun.COM 	}
4492789Sahrens 
44937754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
44947754SJeff.Bonwick@Sun.COM 
44951544Seschrock 	/*
449610685SGeorge.Wilson@Sun.COM 	 * If we can tolerate two or more faults, or we're dealing
449710685SGeorge.Wilson@Sun.COM 	 * with a slog, randomly online/offline vd0.
44981544Seschrock 	 */
449910685SGeorge.Wilson@Sun.COM 	if ((maxfaults >= 2 || islog) && guid0 != 0) {
45008241SJeff.Bonwick@Sun.COM 		if (ztest_random(10) < 6) {
45018241SJeff.Bonwick@Sun.COM 			int flags = (ztest_random(2) == 0 ?
45028241SJeff.Bonwick@Sun.COM 			    ZFS_OFFLINE_TEMPORARY : 0);
450310685SGeorge.Wilson@Sun.COM 
450410685SGeorge.Wilson@Sun.COM 			/*
450510685SGeorge.Wilson@Sun.COM 			 * We have to grab the zs_name_lock as writer to
450610685SGeorge.Wilson@Sun.COM 			 * prevent a race between offlining a slog and
450710685SGeorge.Wilson@Sun.COM 			 * destroying a dataset. Offlining the slog will
450810685SGeorge.Wilson@Sun.COM 			 * grab a reference on the dataset which may cause
450910685SGeorge.Wilson@Sun.COM 			 * dmu_objset_destroy() to fail with EBUSY thus
451010685SGeorge.Wilson@Sun.COM 			 * leaving the dataset in an inconsistent state.
451110685SGeorge.Wilson@Sun.COM 			 */
451210685SGeorge.Wilson@Sun.COM 			if (islog)
451310685SGeorge.Wilson@Sun.COM 				(void) rw_wrlock(&ztest_shared->zs_name_lock);
451410685SGeorge.Wilson@Sun.COM 
45158241SJeff.Bonwick@Sun.COM 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
451610685SGeorge.Wilson@Sun.COM 
451710685SGeorge.Wilson@Sun.COM 			if (islog)
451810685SGeorge.Wilson@Sun.COM 				(void) rw_unlock(&ztest_shared->zs_name_lock);
45198241SJeff.Bonwick@Sun.COM 		} else {
45208241SJeff.Bonwick@Sun.COM 			(void) vdev_online(spa, guid0, 0, NULL);
45218241SJeff.Bonwick@Sun.COM 		}
4522789Sahrens 	}
4523789Sahrens 
452410685SGeorge.Wilson@Sun.COM 	if (maxfaults == 0)
452510685SGeorge.Wilson@Sun.COM 		return;
452610685SGeorge.Wilson@Sun.COM 
4527789Sahrens 	/*
45281544Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
4529789Sahrens 	 */
4530789Sahrens 	fd = open(pathrand, O_RDWR);
4531789Sahrens 
4532789Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
4533789Sahrens 		return;
4534789Sahrens 
4535789Sahrens 	fsize = lseek(fd, 0, SEEK_END);
4536789Sahrens 
4537789Sahrens 	while (--iters != 0) {
4538789Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
4539789Sahrens 		    (leaves << bshift) + (leaf << bshift) +
4540789Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4541789Sahrens 
4542789Sahrens 		if (offset >= fsize)
4543789Sahrens 			continue;
4544789Sahrens 
454511422SMark.Musante@Sun.COM 		VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
454611422SMark.Musante@Sun.COM 		if (mirror_save != zs->zs_mirrors) {
454711422SMark.Musante@Sun.COM 			VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
454811422SMark.Musante@Sun.COM 			(void) close(fd);
454911422SMark.Musante@Sun.COM 			return;
455011422SMark.Musante@Sun.COM 		}
4551789Sahrens 
4552789Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4553789Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
4554789Sahrens 			    offset, pathrand);
455511422SMark.Musante@Sun.COM 
455611422SMark.Musante@Sun.COM 		VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
455711422SMark.Musante@Sun.COM 
455811422SMark.Musante@Sun.COM 		if (zopt_verbose >= 7)
455911422SMark.Musante@Sun.COM 			(void) printf("injected bad word into %s,"
456011422SMark.Musante@Sun.COM 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4561789Sahrens 	}
4562789Sahrens 
4563789Sahrens 	(void) close(fd);
4564789Sahrens }
4565789Sahrens 
4566789Sahrens /*
456710922SJeff.Bonwick@Sun.COM  * Verify that DDT repair works as expected.
4568789Sahrens  */
4569789Sahrens void
ztest_ddt_repair(ztest_ds_t * zd,uint64_t id)457010922SJeff.Bonwick@Sun.COM ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4571789Sahrens {
457210922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
457310922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
457410922SJeff.Bonwick@Sun.COM 	objset_t *os = zd->zd_os;
457510922SJeff.Bonwick@Sun.COM 	ztest_od_t od[1];
457610922SJeff.Bonwick@Sun.COM 	uint64_t object, blocksize, txg, pattern, psize;
457710922SJeff.Bonwick@Sun.COM 	enum zio_checksum checksum = spa_dedup_checksum(spa);
457810922SJeff.Bonwick@Sun.COM 	dmu_buf_t *db;
457910922SJeff.Bonwick@Sun.COM 	dmu_tx_t *tx;
458010922SJeff.Bonwick@Sun.COM 	void *buf;
458110922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
458210922SJeff.Bonwick@Sun.COM 	int copies = 2 * ZIO_DEDUPDITTO_MIN;
458310922SJeff.Bonwick@Sun.COM 
458410922SJeff.Bonwick@Sun.COM 	blocksize = ztest_random_blocksize();
458510922SJeff.Bonwick@Sun.COM 	blocksize = MIN(blocksize, 2048);	/* because we write so many */
458610922SJeff.Bonwick@Sun.COM 
458710922SJeff.Bonwick@Sun.COM 	ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
458810922SJeff.Bonwick@Sun.COM 
458910922SJeff.Bonwick@Sun.COM 	if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
459010922SJeff.Bonwick@Sun.COM 		return;
459110922SJeff.Bonwick@Sun.COM 
459210922SJeff.Bonwick@Sun.COM 	/*
459310922SJeff.Bonwick@Sun.COM 	 * Take the name lock as writer to prevent anyone else from changing
459410922SJeff.Bonwick@Sun.COM 	 * the pool and dataset properies we need to maintain during this test.
459510922SJeff.Bonwick@Sun.COM 	 */
459610922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
459710922SJeff.Bonwick@Sun.COM 
459810922SJeff.Bonwick@Sun.COM 	if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
459910922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0 ||
460010922SJeff.Bonwick@Sun.COM 	    ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
460110922SJeff.Bonwick@Sun.COM 	    B_FALSE) != 0) {
460210922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
460310922SJeff.Bonwick@Sun.COM 		return;
460410922SJeff.Bonwick@Sun.COM 	}
460510922SJeff.Bonwick@Sun.COM 
460610922SJeff.Bonwick@Sun.COM 	object = od[0].od_object;
460710922SJeff.Bonwick@Sun.COM 	blocksize = od[0].od_blocksize;
460810922SJeff.Bonwick@Sun.COM 	pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
460910922SJeff.Bonwick@Sun.COM 
461010922SJeff.Bonwick@Sun.COM 	ASSERT(object != 0);
461110922SJeff.Bonwick@Sun.COM 
461210922SJeff.Bonwick@Sun.COM 	tx = dmu_tx_create(os);
461310922SJeff.Bonwick@Sun.COM 	dmu_tx_hold_write(tx, object, 0, copies * blocksize);
461410922SJeff.Bonwick@Sun.COM 	txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
461510922SJeff.Bonwick@Sun.COM 	if (txg == 0) {
461610922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
461710922SJeff.Bonwick@Sun.COM 		return;
461810922SJeff.Bonwick@Sun.COM 	}
461910922SJeff.Bonwick@Sun.COM 
462010922SJeff.Bonwick@Sun.COM 	/*
462110922SJeff.Bonwick@Sun.COM 	 * Write all the copies of our block.
462210922SJeff.Bonwick@Sun.COM 	 */
462310922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < copies; i++) {
462410922SJeff.Bonwick@Sun.COM 		uint64_t offset = i * blocksize;
462512285SJeff.Bonwick@Sun.COM 		VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
462612285SJeff.Bonwick@Sun.COM 		    DMU_READ_NO_PREFETCH) == 0);
462710922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_offset == offset);
462810922SJeff.Bonwick@Sun.COM 		ASSERT(db->db_size == blocksize);
462910922SJeff.Bonwick@Sun.COM 		ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
463010922SJeff.Bonwick@Sun.COM 		    ztest_pattern_match(db->db_data, db->db_size, 0ULL));
463110922SJeff.Bonwick@Sun.COM 		dmu_buf_will_fill(db, tx);
463210922SJeff.Bonwick@Sun.COM 		ztest_pattern_set(db->db_data, db->db_size, pattern);
463310922SJeff.Bonwick@Sun.COM 		dmu_buf_rele(db, FTAG);
463410922SJeff.Bonwick@Sun.COM 	}
463510922SJeff.Bonwick@Sun.COM 
463610922SJeff.Bonwick@Sun.COM 	dmu_tx_commit(tx);
463710922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), txg);
463810922SJeff.Bonwick@Sun.COM 
463910922SJeff.Bonwick@Sun.COM 	/*
464010922SJeff.Bonwick@Sun.COM 	 * Find out what block we got.
464110922SJeff.Bonwick@Sun.COM 	 */
464212285SJeff.Bonwick@Sun.COM 	VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
464312285SJeff.Bonwick@Sun.COM 	    DMU_READ_NO_PREFETCH) == 0);
464410922SJeff.Bonwick@Sun.COM 	blk = *((dmu_buf_impl_t *)db)->db_blkptr;
464510922SJeff.Bonwick@Sun.COM 	dmu_buf_rele(db, FTAG);
464610922SJeff.Bonwick@Sun.COM 
464710922SJeff.Bonwick@Sun.COM 	/*
464810922SJeff.Bonwick@Sun.COM 	 * Damage the block.  Dedup-ditto will save us when we read it later.
464910922SJeff.Bonwick@Sun.COM 	 */
465010922SJeff.Bonwick@Sun.COM 	psize = BP_GET_PSIZE(&blk);
465110922SJeff.Bonwick@Sun.COM 	buf = zio_buf_alloc(psize);
465210922SJeff.Bonwick@Sun.COM 	ztest_pattern_set(buf, psize, ~pattern);
465310922SJeff.Bonwick@Sun.COM 
465410922SJeff.Bonwick@Sun.COM 	(void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
465510922SJeff.Bonwick@Sun.COM 	    buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
465610922SJeff.Bonwick@Sun.COM 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
465710922SJeff.Bonwick@Sun.COM 
465810922SJeff.Bonwick@Sun.COM 	zio_buf_free(buf, psize);
465910922SJeff.Bonwick@Sun.COM 
466010922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
466110922SJeff.Bonwick@Sun.COM }
466210922SJeff.Bonwick@Sun.COM 
466310922SJeff.Bonwick@Sun.COM /*
466410922SJeff.Bonwick@Sun.COM  * Scrub the pool.
466510922SJeff.Bonwick@Sun.COM  */
466610922SJeff.Bonwick@Sun.COM /* ARGSUSED */
466710922SJeff.Bonwick@Sun.COM void
ztest_scrub(ztest_ds_t * zd,uint64_t id)466810922SJeff.Bonwick@Sun.COM ztest_scrub(ztest_ds_t *zd, uint64_t id)
466910922SJeff.Bonwick@Sun.COM {
467010922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
467110922SJeff.Bonwick@Sun.COM 	spa_t *spa = zs->zs_spa;
4672789Sahrens 
467312296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
467410922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
467512296SLin.Ling@Sun.COM 	(void) spa_scan(spa, POOL_SCAN_SCRUB);
4676789Sahrens }
4677789Sahrens 
4678789Sahrens /*
4679789Sahrens  * Rename the pool to a different name and then rename it back.
4680789Sahrens  */
468110922SJeff.Bonwick@Sun.COM /* ARGSUSED */
4682789Sahrens void
ztest_spa_rename(ztest_ds_t * zd,uint64_t id)468310922SJeff.Bonwick@Sun.COM ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4684789Sahrens {
468510922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
4686789Sahrens 	char *oldname, *newname;
4687789Sahrens 	spa_t *spa;
4688789Sahrens 
468910922SJeff.Bonwick@Sun.COM 	(void) rw_wrlock(&zs->zs_name_lock);
469010922SJeff.Bonwick@Sun.COM 
469110922SJeff.Bonwick@Sun.COM 	oldname = zs->zs_pool;
4692789Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4693789Sahrens 	(void) strcpy(newname, oldname);
4694789Sahrens 	(void) strcat(newname, "_tmp");
4695789Sahrens 
4696789Sahrens 	/*
4697789Sahrens 	 * Do the rename
4698789Sahrens 	 */
469910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(oldname, newname));
4700789Sahrens 
4701789Sahrens 	/*
4702789Sahrens 	 * Try to open it under the old name, which shouldn't exist
4703789Sahrens 	 */
470410922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4705789Sahrens 
4706789Sahrens 	/*
4707789Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
4708789Sahrens 	 */
470910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
471010922SJeff.Bonwick@Sun.COM 
471110922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4712789Sahrens 	spa_close(spa, FTAG);
4713789Sahrens 
4714789Sahrens 	/*
4715789Sahrens 	 * Rename it back to the original
4716789Sahrens 	 */
471710922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_rename(newname, oldname));
4718789Sahrens 
4719789Sahrens 	/*
4720789Sahrens 	 * Make sure it can still be opened
4721789Sahrens 	 */
472210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
472310922SJeff.Bonwick@Sun.COM 
472410922SJeff.Bonwick@Sun.COM 	ASSERT(spa == zs->zs_spa);
4725789Sahrens 	spa_close(spa, FTAG);
4726789Sahrens 
4727789Sahrens 	umem_free(newname, strlen(newname) + 1);
4728789Sahrens 
472910922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
4730789Sahrens }
4731789Sahrens 
4732789Sahrens /*
473310922SJeff.Bonwick@Sun.COM  * Verify pool integrity by running zdb.
4734789Sahrens  */
4735789Sahrens static void
ztest_run_zdb(char * pool)473610922SJeff.Bonwick@Sun.COM ztest_run_zdb(char *pool)
4737789Sahrens {
4738789Sahrens 	int status;
4739789Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4740789Sahrens 	char zbuf[1024];
4741789Sahrens 	char *bin;
47424527Sperrin 	char *ztest;
47434527Sperrin 	char *isa;
47444527Sperrin 	int isalen;
4745789Sahrens 	FILE *fp;
4746789Sahrens 
4747789Sahrens 	(void) realpath(getexecname(), zdb);
4748789Sahrens 
4749789Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4750789Sahrens 	bin = strstr(zdb, "/usr/bin/");
47514527Sperrin 	ztest = strstr(bin, "/ztest");
47524527Sperrin 	isa = bin + 8;
47534527Sperrin 	isalen = ztest - isa;
47544527Sperrin 	isa = strdup(isa);
4755789Sahrens 	/* LINTED */
47565994Sck153898 	(void) sprintf(bin,
475711811SJeff.Bonwick@Sun.COM 	    "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
47584527Sperrin 	    isalen,
47594527Sperrin 	    isa,
4760789Sahrens 	    zopt_verbose >= 3 ? "s" : "",
4761789Sahrens 	    zopt_verbose >= 4 ? "v" : "",
476211811SJeff.Bonwick@Sun.COM 	    spa_config_path,
47637837SMatthew.Ahrens@Sun.COM 	    pool);
47644527Sperrin 	free(isa);
4765789Sahrens 
4766789Sahrens 	if (zopt_verbose >= 5)
4767789Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
4768789Sahrens 
4769789Sahrens 	fp = popen(zdb, "r");
4770789Sahrens 
4771789Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4772789Sahrens 		if (zopt_verbose >= 3)
4773789Sahrens 			(void) printf("%s", zbuf);
4774789Sahrens 
4775789Sahrens 	status = pclose(fp);
4776789Sahrens 
4777789Sahrens 	if (status == 0)
4778789Sahrens 		return;
4779789Sahrens 
4780789Sahrens 	ztest_dump_core = 0;
4781789Sahrens 	if (WIFEXITED(status))
4782789Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4783789Sahrens 	else
4784789Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4785789Sahrens }
4786789Sahrens 
4787789Sahrens static void
ztest_walk_pool_directory(char * header)4788789Sahrens ztest_walk_pool_directory(char *header)
4789789Sahrens {
4790789Sahrens 	spa_t *spa = NULL;
4791789Sahrens 
4792789Sahrens 	if (zopt_verbose >= 6)
4793789Sahrens 		(void) printf("%s\n", header);
4794789Sahrens 
4795789Sahrens 	mutex_enter(&spa_namespace_lock);
4796789Sahrens 	while ((spa = spa_next(spa)) != NULL)
4797789Sahrens 		if (zopt_verbose >= 6)
4798789Sahrens 			(void) printf("\t%s\n", spa_name(spa));
4799789Sahrens 	mutex_exit(&spa_namespace_lock);
4800789Sahrens }
4801789Sahrens 
4802789Sahrens static void
ztest_spa_import_export(char * oldname,char * newname)4803789Sahrens ztest_spa_import_export(char *oldname, char *newname)
4804789Sahrens {
48058241SJeff.Bonwick@Sun.COM 	nvlist_t *config, *newconfig;
4806789Sahrens 	uint64_t pool_guid;
4807789Sahrens 	spa_t *spa;
4808789Sahrens 
4809789Sahrens 	if (zopt_verbose >= 4) {
4810789Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
4811789Sahrens 		    oldname, newname);
4812789Sahrens 	}
4813789Sahrens 
4814789Sahrens 	/*
4815789Sahrens 	 * Clean up from previous runs.
4816789Sahrens 	 */
4817789Sahrens 	(void) spa_destroy(newname);
4818789Sahrens 
4819789Sahrens 	/*
4820789Sahrens 	 * Get the pool's configuration and guid.
4821789Sahrens 	 */
482210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4823789Sahrens 
48248241SJeff.Bonwick@Sun.COM 	/*
48258241SJeff.Bonwick@Sun.COM 	 * Kick off a scrub to tickle scrub/export races.
48268241SJeff.Bonwick@Sun.COM 	 */
48278241SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0)
482812296SLin.Ling@Sun.COM 		(void) spa_scan(spa, POOL_SCAN_SCRUB);
48298241SJeff.Bonwick@Sun.COM 
4830789Sahrens 	pool_guid = spa_guid(spa);
4831789Sahrens 	spa_close(spa, FTAG);
4832789Sahrens 
4833789Sahrens 	ztest_walk_pool_directory("pools before export");
4834789Sahrens 
4835789Sahrens 	/*
4836789Sahrens 	 * Export it.
4837789Sahrens 	 */
483810922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4839789Sahrens 
4840789Sahrens 	ztest_walk_pool_directory("pools after export");
4841789Sahrens 
4842789Sahrens 	/*
48438241SJeff.Bonwick@Sun.COM 	 * Try to import it.
48448241SJeff.Bonwick@Sun.COM 	 */
48458241SJeff.Bonwick@Sun.COM 	newconfig = spa_tryimport(config);
48468241SJeff.Bonwick@Sun.COM 	ASSERT(newconfig != NULL);
48478241SJeff.Bonwick@Sun.COM 	nvlist_free(newconfig);
48488241SJeff.Bonwick@Sun.COM 
48498241SJeff.Bonwick@Sun.COM 	/*
4850789Sahrens 	 * Import it under the new name.
4851789Sahrens 	 */
485212949SGeorge.Wilson@Sun.COM 	VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
4853789Sahrens 
4854789Sahrens 	ztest_walk_pool_directory("pools after import");
4855789Sahrens 
4856789Sahrens 	/*
4857789Sahrens 	 * Try to import it again -- should fail with EEXIST.
4858789Sahrens 	 */
485912949SGeorge.Wilson@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
4860789Sahrens 
4861789Sahrens 	/*
4862789Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
4863789Sahrens 	 */
486412949SGeorge.Wilson@Sun.COM 	VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
4865789Sahrens 
4866789Sahrens 	/*
4867789Sahrens 	 * Verify that the pool is no longer visible under the old name.
4868789Sahrens 	 */
486910922SJeff.Bonwick@Sun.COM 	VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4870789Sahrens 
4871789Sahrens 	/*
4872789Sahrens 	 * Verify that we can open and close the pool using the new name.
4873789Sahrens 	 */
487410922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4875789Sahrens 	ASSERT(pool_guid == spa_guid(spa));
4876789Sahrens 	spa_close(spa, FTAG);
4877789Sahrens 
4878789Sahrens 	nvlist_free(config);
4879789Sahrens }
4880789Sahrens 
48818241SJeff.Bonwick@Sun.COM static void
ztest_resume(spa_t * spa)48828241SJeff.Bonwick@Sun.COM ztest_resume(spa_t *spa)
48838241SJeff.Bonwick@Sun.COM {
488410922SJeff.Bonwick@Sun.COM 	if (spa_suspended(spa) && zopt_verbose >= 6)
488510922SJeff.Bonwick@Sun.COM 		(void) printf("resuming from suspended state\n");
488610922SJeff.Bonwick@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
488710922SJeff.Bonwick@Sun.COM 	vdev_clear(spa, NULL);
488810922SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
488910922SJeff.Bonwick@Sun.COM 	(void) zio_resume(spa);
48908241SJeff.Bonwick@Sun.COM }
48918241SJeff.Bonwick@Sun.COM 
48925329Sgw25295 static void *
ztest_resume_thread(void * arg)48938241SJeff.Bonwick@Sun.COM ztest_resume_thread(void *arg)
48945329Sgw25295 {
48957754SJeff.Bonwick@Sun.COM 	spa_t *spa = arg;
48965329Sgw25295 
48975329Sgw25295 	while (!ztest_exiting) {
489810922SJeff.Bonwick@Sun.COM 		if (spa_suspended(spa))
489910922SJeff.Bonwick@Sun.COM 			ztest_resume(spa);
490010922SJeff.Bonwick@Sun.COM 		(void) poll(NULL, 0, 100);
49015329Sgw25295 	}
49025329Sgw25295 	return (NULL);
49035329Sgw25295 }
49045329Sgw25295 
4905789Sahrens static void *
ztest_deadman_thread(void * arg)490610922SJeff.Bonwick@Sun.COM ztest_deadman_thread(void *arg)
490710922SJeff.Bonwick@Sun.COM {
490810922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = arg;
490910922SJeff.Bonwick@Sun.COM 	int grace = 300;
491010922SJeff.Bonwick@Sun.COM 	hrtime_t delta;
491110922SJeff.Bonwick@Sun.COM 
491210922SJeff.Bonwick@Sun.COM 	delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
491310922SJeff.Bonwick@Sun.COM 
491410922SJeff.Bonwick@Sun.COM 	(void) poll(NULL, 0, (int)(1000 * delta));
491510922SJeff.Bonwick@Sun.COM 
491610922SJeff.Bonwick@Sun.COM 	fatal(0, "failed to complete within %d seconds of deadline", grace);
491710922SJeff.Bonwick@Sun.COM 
491810922SJeff.Bonwick@Sun.COM 	return (NULL);
491910922SJeff.Bonwick@Sun.COM }
492010922SJeff.Bonwick@Sun.COM 
492110922SJeff.Bonwick@Sun.COM static void
ztest_execute(ztest_info_t * zi,uint64_t id)492210922SJeff.Bonwick@Sun.COM ztest_execute(ztest_info_t *zi, uint64_t id)
492310922SJeff.Bonwick@Sun.COM {
492410922SJeff.Bonwick@Sun.COM 	ztest_shared_t *zs = ztest_shared;
492510922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
492610922SJeff.Bonwick@Sun.COM 	hrtime_t functime = gethrtime();
492710922SJeff.Bonwick@Sun.COM 
492810922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < zi->zi_iters; i++)
492910922SJeff.Bonwick@Sun.COM 		zi->zi_func(zd, id);
493010922SJeff.Bonwick@Sun.COM 
493110922SJeff.Bonwick@Sun.COM 	functime = gethrtime() - functime;
493210922SJeff.Bonwick@Sun.COM 
493310922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_count, 1);
493410922SJeff.Bonwick@Sun.COM 	atomic_add_64(&zi->zi_call_time, functime);
493510922SJeff.Bonwick@Sun.COM 
493610922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 4) {
493710922SJeff.Bonwick@Sun.COM 		Dl_info dli;
493810922SJeff.Bonwick@Sun.COM 		(void) dladdr((void *)zi->zi_func, &dli);
493910922SJeff.Bonwick@Sun.COM 		(void) printf("%6.2f sec in %s\n",
494010922SJeff.Bonwick@Sun.COM 		    (double)functime / NANOSEC, dli.dli_sname);
494110922SJeff.Bonwick@Sun.COM 	}
494210922SJeff.Bonwick@Sun.COM }
494310922SJeff.Bonwick@Sun.COM 
494410922SJeff.Bonwick@Sun.COM static void *
ztest_thread(void * arg)4945789Sahrens ztest_thread(void *arg)
4946789Sahrens {
494710922SJeff.Bonwick@Sun.COM 	uint64_t id = (uintptr_t)arg;
4948789Sahrens 	ztest_shared_t *zs = ztest_shared;
494910922SJeff.Bonwick@Sun.COM 	uint64_t call_next;
495010922SJeff.Bonwick@Sun.COM 	hrtime_t now;
4951789Sahrens 	ztest_info_t *zi;
495210922SJeff.Bonwick@Sun.COM 
495310922SJeff.Bonwick@Sun.COM 	while ((now = gethrtime()) < zs->zs_thread_stop) {
4954789Sahrens 		/*
4955789Sahrens 		 * See if it's time to force a crash.
4956789Sahrens 		 */
495710922SJeff.Bonwick@Sun.COM 		if (now > zs->zs_thread_kill)
495810922SJeff.Bonwick@Sun.COM 			ztest_kill(zs);
4959789Sahrens 
4960789Sahrens 		/*
4961789Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
4962789Sahrens 		 */
4963789Sahrens 		if (zs->zs_enospc_count > 10)
4964789Sahrens 			break;
496510922SJeff.Bonwick@Sun.COM 
496610922SJeff.Bonwick@Sun.COM 		/*
496710922SJeff.Bonwick@Sun.COM 		 * Pick a random function to execute.
496810922SJeff.Bonwick@Sun.COM 		 */
496910922SJeff.Bonwick@Sun.COM 		zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
497010922SJeff.Bonwick@Sun.COM 		call_next = zi->zi_call_next;
497110922SJeff.Bonwick@Sun.COM 
497210922SJeff.Bonwick@Sun.COM 		if (now >= call_next &&
497310922SJeff.Bonwick@Sun.COM 		    atomic_cas_64(&zi->zi_call_next, call_next, call_next +
497410922SJeff.Bonwick@Sun.COM 		    ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
497510922SJeff.Bonwick@Sun.COM 			ztest_execute(zi, id);
4976789Sahrens 	}
4977789Sahrens 
4978789Sahrens 	return (NULL);
4979789Sahrens }
4980789Sahrens 
498110922SJeff.Bonwick@Sun.COM static void
ztest_dataset_name(char * dsname,char * pool,int d)498210922SJeff.Bonwick@Sun.COM ztest_dataset_name(char *dsname, char *pool, int d)
498310922SJeff.Bonwick@Sun.COM {
498410922SJeff.Bonwick@Sun.COM 	(void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
498510922SJeff.Bonwick@Sun.COM }
498610922SJeff.Bonwick@Sun.COM 
498710922SJeff.Bonwick@Sun.COM static void
ztest_dataset_destroy(ztest_shared_t * zs,int d)498810922SJeff.Bonwick@Sun.COM ztest_dataset_destroy(ztest_shared_t *zs, int d)
498910922SJeff.Bonwick@Sun.COM {
499010922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
499110922SJeff.Bonwick@Sun.COM 
499210922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
499310922SJeff.Bonwick@Sun.COM 
499410922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
499510922SJeff.Bonwick@Sun.COM 		(void) printf("Destroying %s to free up space\n", name);
499610922SJeff.Bonwick@Sun.COM 
499710922SJeff.Bonwick@Sun.COM 	/*
499810922SJeff.Bonwick@Sun.COM 	 * Cleanup any non-standard clones and snapshots.  In general,
499910922SJeff.Bonwick@Sun.COM 	 * ztest thread t operates on dataset (t % zopt_datasets),
500010922SJeff.Bonwick@Sun.COM 	 * so there may be more than one thing to clean up.
500110922SJeff.Bonwick@Sun.COM 	 */
500210922SJeff.Bonwick@Sun.COM 	for (int t = d; t < zopt_threads; t += zopt_datasets)
500310922SJeff.Bonwick@Sun.COM 		ztest_dsl_dataset_cleanup(name, t);
500410922SJeff.Bonwick@Sun.COM 
500510922SJeff.Bonwick@Sun.COM 	(void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
500610922SJeff.Bonwick@Sun.COM 	    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
500710922SJeff.Bonwick@Sun.COM }
500810922SJeff.Bonwick@Sun.COM 
500910922SJeff.Bonwick@Sun.COM static void
ztest_dataset_dirobj_verify(ztest_ds_t * zd)501010922SJeff.Bonwick@Sun.COM ztest_dataset_dirobj_verify(ztest_ds_t *zd)
501110922SJeff.Bonwick@Sun.COM {
501210922SJeff.Bonwick@Sun.COM 	uint64_t usedobjs, dirobjs, scratch;
501310922SJeff.Bonwick@Sun.COM 
501410922SJeff.Bonwick@Sun.COM 	/*
501510922SJeff.Bonwick@Sun.COM 	 * ZTEST_DIROBJ is the object directory for the entire dataset.
501610922SJeff.Bonwick@Sun.COM 	 * Therefore, the number of objects in use should equal the
501710922SJeff.Bonwick@Sun.COM 	 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
501810922SJeff.Bonwick@Sun.COM 	 * If not, we have an object leak.
501910922SJeff.Bonwick@Sun.COM 	 *
502010922SJeff.Bonwick@Sun.COM 	 * Note that we can only check this in ztest_dataset_open(),
502110922SJeff.Bonwick@Sun.COM 	 * when the open-context and syncing-context values agree.
502210922SJeff.Bonwick@Sun.COM 	 * That's because zap_count() returns the open-context value,
502310922SJeff.Bonwick@Sun.COM 	 * while dmu_objset_space() returns the rootbp fill count.
502410922SJeff.Bonwick@Sun.COM 	 */
502510922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
502610922SJeff.Bonwick@Sun.COM 	dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
502710922SJeff.Bonwick@Sun.COM 	ASSERT3U(dirobjs + 1, ==, usedobjs);
502810922SJeff.Bonwick@Sun.COM }
502910922SJeff.Bonwick@Sun.COM 
503010922SJeff.Bonwick@Sun.COM static int
ztest_dataset_open(ztest_shared_t * zs,int d)503110922SJeff.Bonwick@Sun.COM ztest_dataset_open(ztest_shared_t *zs, int d)
503210922SJeff.Bonwick@Sun.COM {
503310922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
503410922SJeff.Bonwick@Sun.COM 	uint64_t committed_seq = zd->zd_seq;
503510922SJeff.Bonwick@Sun.COM 	objset_t *os;
503610922SJeff.Bonwick@Sun.COM 	zilog_t *zilog;
503710922SJeff.Bonwick@Sun.COM 	char name[MAXNAMELEN];
503810922SJeff.Bonwick@Sun.COM 	int error;
503910922SJeff.Bonwick@Sun.COM 
504010922SJeff.Bonwick@Sun.COM 	ztest_dataset_name(name, zs->zs_pool, d);
504110922SJeff.Bonwick@Sun.COM 
504210922SJeff.Bonwick@Sun.COM 	(void) rw_rdlock(&zs->zs_name_lock);
504310922SJeff.Bonwick@Sun.COM 
504412294SMark.Musante@Sun.COM 	error = ztest_dataset_create(name);
504510922SJeff.Bonwick@Sun.COM 	if (error == ENOSPC) {
504610922SJeff.Bonwick@Sun.COM 		(void) rw_unlock(&zs->zs_name_lock);
504710922SJeff.Bonwick@Sun.COM 		ztest_record_enospc(FTAG);
504810922SJeff.Bonwick@Sun.COM 		return (error);
504910922SJeff.Bonwick@Sun.COM 	}
505010922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0 || error == EEXIST);
505110922SJeff.Bonwick@Sun.COM 
505210922SJeff.Bonwick@Sun.COM 	VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
505310922SJeff.Bonwick@Sun.COM 	(void) rw_unlock(&zs->zs_name_lock);
505410922SJeff.Bonwick@Sun.COM 
505510922SJeff.Bonwick@Sun.COM 	ztest_zd_init(zd, os);
505610922SJeff.Bonwick@Sun.COM 
505710922SJeff.Bonwick@Sun.COM 	zilog = zd->zd_zilog;
505810922SJeff.Bonwick@Sun.COM 
505910922SJeff.Bonwick@Sun.COM 	if (zilog->zl_header->zh_claim_lr_seq != 0 &&
506010922SJeff.Bonwick@Sun.COM 	    zilog->zl_header->zh_claim_lr_seq < committed_seq)
506110922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: claimed %llu < committed %llu",
506210922SJeff.Bonwick@Sun.COM 		    zilog->zl_header->zh_claim_lr_seq, committed_seq);
506310922SJeff.Bonwick@Sun.COM 
506410922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
506510922SJeff.Bonwick@Sun.COM 
506610922SJeff.Bonwick@Sun.COM 	zil_replay(os, zd, ztest_replay_vector);
506710922SJeff.Bonwick@Sun.COM 
506810922SJeff.Bonwick@Sun.COM 	ztest_dataset_dirobj_verify(zd);
506910922SJeff.Bonwick@Sun.COM 
507010922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 6)
507110922SJeff.Bonwick@Sun.COM 		(void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
507210922SJeff.Bonwick@Sun.COM 		    zd->zd_name,
507310922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_blk_count,
507410922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_parse_lr_count,
507510922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zilog->zl_replaying_seq);
507610922SJeff.Bonwick@Sun.COM 
507710922SJeff.Bonwick@Sun.COM 	zilog = zil_open(os, ztest_get_data);
507810922SJeff.Bonwick@Sun.COM 
507910922SJeff.Bonwick@Sun.COM 	if (zilog->zl_replaying_seq != 0 &&
508010922SJeff.Bonwick@Sun.COM 	    zilog->zl_replaying_seq < committed_seq)
508110922SJeff.Bonwick@Sun.COM 		fatal(0, "missing log records: replayed %llu < committed %llu",
508210922SJeff.Bonwick@Sun.COM 		    zilog->zl_replaying_seq, committed_seq);
508310922SJeff.Bonwick@Sun.COM 
508410922SJeff.Bonwick@Sun.COM 	return (0);
508510922SJeff.Bonwick@Sun.COM }
508610922SJeff.Bonwick@Sun.COM 
508710922SJeff.Bonwick@Sun.COM static void
ztest_dataset_close(ztest_shared_t * zs,int d)508810922SJeff.Bonwick@Sun.COM ztest_dataset_close(ztest_shared_t *zs, int d)
508910922SJeff.Bonwick@Sun.COM {
509010922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[d];
509110922SJeff.Bonwick@Sun.COM 
509210922SJeff.Bonwick@Sun.COM 	zil_close(zd->zd_zilog);
509310922SJeff.Bonwick@Sun.COM 	dmu_objset_rele(zd->zd_os, zd);
509410922SJeff.Bonwick@Sun.COM 
509510922SJeff.Bonwick@Sun.COM 	ztest_zd_fini(zd);
509610922SJeff.Bonwick@Sun.COM }
509710922SJeff.Bonwick@Sun.COM 
5098789Sahrens /*
5099789Sahrens  * Kick off threads to run tests on all datasets in parallel.
5100789Sahrens  */
5101789Sahrens static void
ztest_run(ztest_shared_t * zs)510210922SJeff.Bonwick@Sun.COM ztest_run(ztest_shared_t *zs)
5103789Sahrens {
510410922SJeff.Bonwick@Sun.COM 	thread_t *tid;
5105789Sahrens 	spa_t *spa;
51067754SJeff.Bonwick@Sun.COM 	thread_t resume_tid;
510710922SJeff.Bonwick@Sun.COM 	int error;
51087754SJeff.Bonwick@Sun.COM 
51097754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_FALSE;
5110789Sahrens 
511110922SJeff.Bonwick@Sun.COM 	/*
511210922SJeff.Bonwick@Sun.COM 	 * Initialize parent/child shared state.
511310922SJeff.Bonwick@Sun.COM 	 */
511410922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
511510922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
511610922SJeff.Bonwick@Sun.COM 
511710922SJeff.Bonwick@Sun.COM 	zs->zs_thread_start = gethrtime();
511810922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
511910922SJeff.Bonwick@Sun.COM 	zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
512010922SJeff.Bonwick@Sun.COM 	zs->zs_thread_kill = zs->zs_thread_stop;
512110922SJeff.Bonwick@Sun.COM 	if (ztest_random(100) < zopt_killrate)
512210922SJeff.Bonwick@Sun.COM 		zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
512310922SJeff.Bonwick@Sun.COM 
512410922SJeff.Bonwick@Sun.COM 	(void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
512510612SRicardo.M.Correia@Sun.COM 
512610612SRicardo.M.Correia@Sun.COM 	list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
512710612SRicardo.M.Correia@Sun.COM 	    offsetof(ztest_cb_data_t, zcd_node));
512810612SRicardo.M.Correia@Sun.COM 
5129789Sahrens 	/*
51307754SJeff.Bonwick@Sun.COM 	 * Open our pool.
51315329Sgw25295 	 */
513210922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
513310922SJeff.Bonwick@Sun.COM 	VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
513410922SJeff.Bonwick@Sun.COM 	zs->zs_spa = spa;
513510922SJeff.Bonwick@Sun.COM 
513610922SJeff.Bonwick@Sun.COM 	spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
51375329Sgw25295 
51385329Sgw25295 	/*
51398241SJeff.Bonwick@Sun.COM 	 * We don't expect the pool to suspend unless maxfaults == 0,
51408241SJeff.Bonwick@Sun.COM 	 * in which case ztest_fault_inject() temporarily takes away
51418241SJeff.Bonwick@Sun.COM 	 * the only valid replica.
51428241SJeff.Bonwick@Sun.COM 	 */
514311422SMark.Musante@Sun.COM 	if (MAXFAULTS() == 0)
51448241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
51458241SJeff.Bonwick@Sun.COM 	else
51468241SJeff.Bonwick@Sun.COM 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
51478241SJeff.Bonwick@Sun.COM 
51488241SJeff.Bonwick@Sun.COM 	/*
51497754SJeff.Bonwick@Sun.COM 	 * Create a thread to periodically resume suspended I/O.
5150789Sahrens 	 */
51518241SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
51527754SJeff.Bonwick@Sun.COM 	    &resume_tid) == 0);
5153789Sahrens 
5154789Sahrens 	/*
515510922SJeff.Bonwick@Sun.COM 	 * Create a deadman thread to abort() if we hang.
515610922SJeff.Bonwick@Sun.COM 	 */
515710922SJeff.Bonwick@Sun.COM 	VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
515810922SJeff.Bonwick@Sun.COM 	    NULL) == 0);
515910922SJeff.Bonwick@Sun.COM 
516010922SJeff.Bonwick@Sun.COM 	/*
5161789Sahrens 	 * Verify that we can safely inquire about about any object,
5162789Sahrens 	 * whether it's allocated or not.  To make it interesting,
5163789Sahrens 	 * we probe a 5-wide window around each power of two.
5164789Sahrens 	 * This hits all edge cases, including zero and the max.
5165789Sahrens 	 */
516610922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < 64; t++) {
516710922SJeff.Bonwick@Sun.COM 		for (int d = -5; d <= 5; d++) {
5168789Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
5169789Sahrens 			    (1ULL << t) + d, NULL);
51701544Seschrock 			ASSERT(error == 0 || error == ENOENT ||
51711544Seschrock 			    error == EINVAL);
5172789Sahrens 		}
5173789Sahrens 	}
5174789Sahrens 
5175789Sahrens 	/*
517610922SJeff.Bonwick@Sun.COM 	 * If we got any ENOSPC errors on the previous run, destroy something.
5177789Sahrens 	 */
517810922SJeff.Bonwick@Sun.COM 	if (zs->zs_enospc_count != 0) {
517910922SJeff.Bonwick@Sun.COM 		int d = ztest_random(zopt_datasets);
518010922SJeff.Bonwick@Sun.COM 		ztest_dataset_destroy(zs, d);
518110922SJeff.Bonwick@Sun.COM 	}
5182789Sahrens 	zs->zs_enospc_count = 0;
5183789Sahrens 
518410922SJeff.Bonwick@Sun.COM 	tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5185789Sahrens 
5186789Sahrens 	if (zopt_verbose >= 4)
5187789Sahrens 		(void) printf("starting main threads...\n");
5188789Sahrens 
518910922SJeff.Bonwick@Sun.COM 	/*
519010922SJeff.Bonwick@Sun.COM 	 * Kick off all the tests that run in parallel.
519110922SJeff.Bonwick@Sun.COM 	 */
519210922SJeff.Bonwick@Sun.COM 	for (int t = 0; t < zopt_threads; t++) {
519310922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
519410922SJeff.Bonwick@Sun.COM 			return;
519510922SJeff.Bonwick@Sun.COM 		VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
519610922SJeff.Bonwick@Sun.COM 		    THR_BOUND, &tid[t]) == 0);
5197789Sahrens 	}
5198789Sahrens 
5199789Sahrens 	/*
520010922SJeff.Bonwick@Sun.COM 	 * Wait for all of the tests to complete.  We go in reverse order
520110922SJeff.Bonwick@Sun.COM 	 * so we don't close datasets while threads are still using them.
5202789Sahrens 	 */
520310922SJeff.Bonwick@Sun.COM 	for (int t = zopt_threads - 1; t >= 0; t--) {
520410922SJeff.Bonwick@Sun.COM 		VERIFY(thr_join(tid[t], NULL, NULL) == 0);
520510922SJeff.Bonwick@Sun.COM 		if (t < zopt_datasets)
520610922SJeff.Bonwick@Sun.COM 			ztest_dataset_close(zs, t);
5207789Sahrens 	}
5208789Sahrens 
52091544Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
5210789Sahrens 
521110922SJeff.Bonwick@Sun.COM 	zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
521210922SJeff.Bonwick@Sun.COM 	zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
521310922SJeff.Bonwick@Sun.COM 
521410922SJeff.Bonwick@Sun.COM 	umem_free(tid, zopt_threads * sizeof (thread_t));
52157754SJeff.Bonwick@Sun.COM 
52167754SJeff.Bonwick@Sun.COM 	/* Kill the resume thread */
52177754SJeff.Bonwick@Sun.COM 	ztest_exiting = B_TRUE;
52187754SJeff.Bonwick@Sun.COM 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
52198241SJeff.Bonwick@Sun.COM 	ztest_resume(spa);
52207754SJeff.Bonwick@Sun.COM 
5221789Sahrens 	/*
5222789Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
5223789Sahrens 	 * spa_close() should wait for it to complete.
5224789Sahrens 	 */
522510922SJeff.Bonwick@Sun.COM 	for (uint64_t object = 1; object < 50; object++)
522610922SJeff.Bonwick@Sun.COM 		dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5227789Sahrens 
52285530Sbonwick 	spa_close(spa, FTAG);
52295530Sbonwick 
523010922SJeff.Bonwick@Sun.COM 	/*
523110922SJeff.Bonwick@Sun.COM 	 * Verify that we can loop over all pools.
523210922SJeff.Bonwick@Sun.COM 	 */
523310922SJeff.Bonwick@Sun.COM 	mutex_enter(&spa_namespace_lock);
523410922SJeff.Bonwick@Sun.COM 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
523510922SJeff.Bonwick@Sun.COM 		if (zopt_verbose > 3)
523610922SJeff.Bonwick@Sun.COM 			(void) printf("spa_next: found %s\n", spa_name(spa));
523710922SJeff.Bonwick@Sun.COM 	mutex_exit(&spa_namespace_lock);
523810922SJeff.Bonwick@Sun.COM 
523910922SJeff.Bonwick@Sun.COM 	/*
524010922SJeff.Bonwick@Sun.COM 	 * Verify that we can export the pool and reimport it under a
524110922SJeff.Bonwick@Sun.COM 	 * different name.
524210922SJeff.Bonwick@Sun.COM 	 */
524310922SJeff.Bonwick@Sun.COM 	if (ztest_random(2) == 0) {
524410922SJeff.Bonwick@Sun.COM 		char name[MAXNAMELEN];
524510922SJeff.Bonwick@Sun.COM 		(void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
524610922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(zs->zs_pool, name);
524710922SJeff.Bonwick@Sun.COM 		ztest_spa_import_export(name, zs->zs_pool);
524810922SJeff.Bonwick@Sun.COM 	}
524910922SJeff.Bonwick@Sun.COM 
525010922SJeff.Bonwick@Sun.COM 	kernel_fini();
525112754SGeorge.Wilson@Sun.COM 
525212754SGeorge.Wilson@Sun.COM 	list_destroy(&zcl.zcl_callbacks);
525312754SGeorge.Wilson@Sun.COM 
525412754SGeorge.Wilson@Sun.COM 	(void) _mutex_destroy(&zcl.zcl_callbacks_lock);
525512754SGeorge.Wilson@Sun.COM 
525612754SGeorge.Wilson@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
525712754SGeorge.Wilson@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
525810922SJeff.Bonwick@Sun.COM }
525910922SJeff.Bonwick@Sun.COM 
526010922SJeff.Bonwick@Sun.COM static void
ztest_freeze(ztest_shared_t * zs)526110922SJeff.Bonwick@Sun.COM ztest_freeze(ztest_shared_t *zs)
526210922SJeff.Bonwick@Sun.COM {
526310922SJeff.Bonwick@Sun.COM 	ztest_ds_t *zd = &zs->zs_zd[0];
526410922SJeff.Bonwick@Sun.COM 	spa_t *spa;
526511818SMark.Musante@Sun.COM 	int numloops = 0;
526610922SJeff.Bonwick@Sun.COM 
526710922SJeff.Bonwick@Sun.COM 	if (zopt_verbose >= 3)
526810922SJeff.Bonwick@Sun.COM 		(void) printf("testing spa_freeze()...\n");
526910922SJeff.Bonwick@Sun.COM 
527010922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
527110922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
527210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
527310922SJeff.Bonwick@Sun.COM 
527410922SJeff.Bonwick@Sun.COM 	/*
527510922SJeff.Bonwick@Sun.COM 	 * Force the first log block to be transactionally allocated.
527610922SJeff.Bonwick@Sun.COM 	 * We have to do this before we freeze the pool -- otherwise
527710922SJeff.Bonwick@Sun.COM 	 * the log chain won't be anchored.
527810922SJeff.Bonwick@Sun.COM 	 */
527910922SJeff.Bonwick@Sun.COM 	while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
528010922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
528112699SNeil.Perrin@Sun.COM 		zil_commit(zd->zd_zilog, 0);
528210922SJeff.Bonwick@Sun.COM 	}
528310922SJeff.Bonwick@Sun.COM 
528410922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
528510922SJeff.Bonwick@Sun.COM 
528610922SJeff.Bonwick@Sun.COM 	/*
528710922SJeff.Bonwick@Sun.COM 	 * Freeze the pool.  This stops spa_sync() from doing anything,
528810922SJeff.Bonwick@Sun.COM 	 * so that the only way to record changes from now on is the ZIL.
528910922SJeff.Bonwick@Sun.COM 	 */
529010922SJeff.Bonwick@Sun.COM 	spa_freeze(spa);
529110922SJeff.Bonwick@Sun.COM 
529210922SJeff.Bonwick@Sun.COM 	/*
529310922SJeff.Bonwick@Sun.COM 	 * Run tests that generate log records but don't alter the pool config
529410922SJeff.Bonwick@Sun.COM 	 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
529510922SJeff.Bonwick@Sun.COM 	 * We do a txg_wait_synced() after each iteration to force the txg
529610922SJeff.Bonwick@Sun.COM 	 * to increase well beyond the last synced value in the uberblock.
529710922SJeff.Bonwick@Sun.COM 	 * The ZIL should be OK with that.
529810922SJeff.Bonwick@Sun.COM 	 */
529911818SMark.Musante@Sun.COM 	while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
530010922SJeff.Bonwick@Sun.COM 		ztest_dmu_write_parallel(zd, 0);
530110922SJeff.Bonwick@Sun.COM 		ztest_dmu_object_alloc_free(zd, 0);
530210922SJeff.Bonwick@Sun.COM 		txg_wait_synced(spa_get_dsl(spa), 0);
530310922SJeff.Bonwick@Sun.COM 	}
530410922SJeff.Bonwick@Sun.COM 
530510922SJeff.Bonwick@Sun.COM 	/*
530610922SJeff.Bonwick@Sun.COM 	 * Commit all of the changes we just generated.
530710922SJeff.Bonwick@Sun.COM 	 */
530812699SNeil.Perrin@Sun.COM 	zil_commit(zd->zd_zilog, 0);
530910922SJeff.Bonwick@Sun.COM 	txg_wait_synced(spa_get_dsl(spa), 0);
531010922SJeff.Bonwick@Sun.COM 
531110922SJeff.Bonwick@Sun.COM 	/*
531210922SJeff.Bonwick@Sun.COM 	 * Close our dataset and close the pool.
531310922SJeff.Bonwick@Sun.COM 	 */
531410922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
531510922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
531610922SJeff.Bonwick@Sun.COM 	kernel_fini();
531710922SJeff.Bonwick@Sun.COM 
531810922SJeff.Bonwick@Sun.COM 	/*
531910922SJeff.Bonwick@Sun.COM 	 * Open and close the pool and dataset to induce log replay.
532010922SJeff.Bonwick@Sun.COM 	 */
532110922SJeff.Bonwick@Sun.COM 	kernel_init(FREAD | FWRITE);
532210922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
532310922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
532410922SJeff.Bonwick@Sun.COM 	ztest_dataset_close(zs, 0);
532510922SJeff.Bonwick@Sun.COM 	spa_close(spa, FTAG);
5326789Sahrens 	kernel_fini();
5327789Sahrens }
5328789Sahrens 
5329789Sahrens void
print_time(hrtime_t t,char * timebuf)5330789Sahrens print_time(hrtime_t t, char *timebuf)
5331789Sahrens {
5332789Sahrens 	hrtime_t s = t / NANOSEC;
5333789Sahrens 	hrtime_t m = s / 60;
5334789Sahrens 	hrtime_t h = m / 60;
5335789Sahrens 	hrtime_t d = h / 24;
5336789Sahrens 
5337789Sahrens 	s -= m * 60;
5338789Sahrens 	m -= h * 60;
5339789Sahrens 	h -= d * 24;
5340789Sahrens 
5341789Sahrens 	timebuf[0] = '\0';
5342789Sahrens 
5343789Sahrens 	if (d)
5344789Sahrens 		(void) sprintf(timebuf,
5345789Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
5346789Sahrens 	else if (h)
5347789Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5348789Sahrens 	else if (m)
5349789Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
5350789Sahrens 	else
5351789Sahrens 		(void) sprintf(timebuf, "%llus", s);
5352789Sahrens }
5353789Sahrens 
535411422SMark.Musante@Sun.COM static nvlist_t *
make_random_props()535511422SMark.Musante@Sun.COM make_random_props()
535611422SMark.Musante@Sun.COM {
535711422SMark.Musante@Sun.COM 	nvlist_t *props;
535811422SMark.Musante@Sun.COM 
535911422SMark.Musante@Sun.COM 	if (ztest_random(2) == 0)
536011422SMark.Musante@Sun.COM 		return (NULL);
536111422SMark.Musante@Sun.COM 
536211422SMark.Musante@Sun.COM 	VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
536311422SMark.Musante@Sun.COM 	VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
536411422SMark.Musante@Sun.COM 
536511422SMark.Musante@Sun.COM 	(void) printf("props:\n");
536611422SMark.Musante@Sun.COM 	dump_nvlist(props, 4);
536711422SMark.Musante@Sun.COM 
536811422SMark.Musante@Sun.COM 	return (props);
536911422SMark.Musante@Sun.COM }
537011422SMark.Musante@Sun.COM 
5371789Sahrens /*
5372789Sahrens  * Create a storage pool with the given name and initial vdev size.
537310922SJeff.Bonwick@Sun.COM  * Then test spa_freeze() functionality.
5374789Sahrens  */
5375789Sahrens static void
ztest_init(ztest_shared_t * zs)537610922SJeff.Bonwick@Sun.COM ztest_init(ztest_shared_t *zs)
5377789Sahrens {
5378789Sahrens 	spa_t *spa;
537911422SMark.Musante@Sun.COM 	nvlist_t *nvroot, *props;
5380789Sahrens 
538110922SJeff.Bonwick@Sun.COM 	VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
538210922SJeff.Bonwick@Sun.COM 	VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
538310922SJeff.Bonwick@Sun.COM 
5384789Sahrens 	kernel_init(FREAD | FWRITE);
5385789Sahrens 
5386789Sahrens 	/*
5387789Sahrens 	 * Create the storage pool.
5388789Sahrens 	 */
538910922SJeff.Bonwick@Sun.COM 	(void) spa_destroy(zs->zs_pool);
539010594SGeorge.Wilson@Sun.COM 	ztest_shared->zs_vdev_next_leaf = 0;
539111422SMark.Musante@Sun.COM 	zs->zs_splits = 0;
539211422SMark.Musante@Sun.COM 	zs->zs_mirrors = zopt_mirrors;
53937754SJeff.Bonwick@Sun.COM 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
539411422SMark.Musante@Sun.COM 	    0, zopt_raidz, zs->zs_mirrors, 1);
539511422SMark.Musante@Sun.COM 	props = make_random_props();
539611422SMark.Musante@Sun.COM 	VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5397789Sahrens 	nvlist_free(nvroot);
5398789Sahrens 
539910922SJeff.Bonwick@Sun.COM 	VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
54009480SGeorge.Wilson@Sun.COM 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5401789Sahrens 	spa_close(spa, FTAG);
5402789Sahrens 
5403789Sahrens 	kernel_fini();
540410922SJeff.Bonwick@Sun.COM 
540510922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
540610922SJeff.Bonwick@Sun.COM 
540710922SJeff.Bonwick@Sun.COM 	ztest_freeze(zs);
540810922SJeff.Bonwick@Sun.COM 
540910922SJeff.Bonwick@Sun.COM 	ztest_run_zdb(zs->zs_pool);
541012754SGeorge.Wilson@Sun.COM 
541112754SGeorge.Wilson@Sun.COM 	(void) rwlock_destroy(&zs->zs_name_lock);
541212754SGeorge.Wilson@Sun.COM 	(void) _mutex_destroy(&zs->zs_vdev_lock);
5413789Sahrens }
5414789Sahrens 
5415789Sahrens int
main(int argc,char ** argv)5416789Sahrens main(int argc, char **argv)
5417789Sahrens {
5418789Sahrens 	int kills = 0;
5419789Sahrens 	int iters = 0;
5420789Sahrens 	ztest_shared_t *zs;
542110922SJeff.Bonwick@Sun.COM 	size_t shared_size;
5422789Sahrens 	ztest_info_t *zi;
5423789Sahrens 	char timebuf[100];
5424789Sahrens 	char numbuf[6];
542510922SJeff.Bonwick@Sun.COM 	spa_t *spa;
5426789Sahrens 
5427789Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
5428789Sahrens 
5429789Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
5430789Sahrens 
5431789Sahrens 	process_options(argc, argv);
5432789Sahrens 
543311811SJeff.Bonwick@Sun.COM 	/* Override location of zpool.cache */
543411811SJeff.Bonwick@Sun.COM 	(void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
543511811SJeff.Bonwick@Sun.COM 
54361544Seschrock 	/*
54371544Seschrock 	 * Blow away any existing copy of zpool.cache
54381544Seschrock 	 */
54391544Seschrock 	if (zopt_init != 0)
544011811SJeff.Bonwick@Sun.COM 		(void) remove(spa_config_path);
54411544Seschrock 
544210922SJeff.Bonwick@Sun.COM 	shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
544310922SJeff.Bonwick@Sun.COM 
5444789Sahrens 	zs = ztest_shared = (void *)mmap(0,
544510922SJeff.Bonwick@Sun.COM 	    P2ROUNDUP(shared_size, getpagesize()),
5446789Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5447789Sahrens 
5448789Sahrens 	if (zopt_verbose >= 1) {
5449789Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
5450789Sahrens 		    " %llu seconds...\n",
54511732Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5452789Sahrens 		    (u_longlong_t)zopt_time);
5453789Sahrens 	}
5454789Sahrens 
5455789Sahrens 	/*
5456789Sahrens 	 * Create and initialize our storage pool.
5457789Sahrens 	 */
545810922SJeff.Bonwick@Sun.COM 	for (int i = 1; i <= zopt_init; i++) {
5459789Sahrens 		bzero(zs, sizeof (ztest_shared_t));
5460789Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
5461789Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
546210922SJeff.Bonwick@Sun.COM 		zs->zs_pool = zopt_pool;
546310922SJeff.Bonwick@Sun.COM 		ztest_init(zs);
5464789Sahrens 	}
5465789Sahrens 
546610922SJeff.Bonwick@Sun.COM 	zs->zs_pool = zopt_pool;
546710922SJeff.Bonwick@Sun.COM 	zs->zs_proc_start = gethrtime();
546810922SJeff.Bonwick@Sun.COM 	zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
546910922SJeff.Bonwick@Sun.COM 
547010922SJeff.Bonwick@Sun.COM 	for (int f = 0; f < ZTEST_FUNCS; f++) {
5471789Sahrens 		zi = &zs->zs_info[f];
5472789Sahrens 		*zi = ztest_info[f];
547310922SJeff.Bonwick@Sun.COM 		if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
547410922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = UINT64_MAX;
5475789Sahrens 		else
547610922SJeff.Bonwick@Sun.COM 			zi->zi_call_next = zs->zs_proc_start +
547710922SJeff.Bonwick@Sun.COM 			    ztest_random(2 * zi->zi_interval[0] + 1);
5478789Sahrens 	}
5479789Sahrens 
5480789Sahrens 	/*
5481789Sahrens 	 * Run the tests in a loop.  These tests include fault injection
5482789Sahrens 	 * to verify that self-healing data works, and forced crashes
5483789Sahrens 	 * to verify that we never lose on-disk consistency.
5484789Sahrens 	 */
548510922SJeff.Bonwick@Sun.COM 	while (gethrtime() < zs->zs_proc_stop) {
5486789Sahrens 		int status;
5487789Sahrens 		pid_t pid;
5488789Sahrens 
5489789Sahrens 		/*
5490789Sahrens 		 * Initialize the workload counters for each function.
5491789Sahrens 		 */
549210922SJeff.Bonwick@Sun.COM 		for (int f = 0; f < ZTEST_FUNCS; f++) {
5493789Sahrens 			zi = &zs->zs_info[f];
549410922SJeff.Bonwick@Sun.COM 			zi->zi_call_count = 0;
5495789Sahrens 			zi->zi_call_time = 0;
5496789Sahrens 		}
5497789Sahrens 
54989480SGeorge.Wilson@Sun.COM 		/* Set the allocation switch size */
54999480SGeorge.Wilson@Sun.COM 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
55009480SGeorge.Wilson@Sun.COM 
5501789Sahrens 		pid = fork();
5502789Sahrens 
5503789Sahrens 		if (pid == -1)
5504789Sahrens 			fatal(1, "fork failed");
5505789Sahrens 
5506789Sahrens 		if (pid == 0) {	/* child */
5507789Sahrens 			struct rlimit rl = { 1024, 1024 };
5508789Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
55091914Scasper 			(void) enable_extended_FILE_stdio(-1, -1);
551010922SJeff.Bonwick@Sun.COM 			ztest_run(zs);
5511789Sahrens 			exit(0);
5512789Sahrens 		}
5513789Sahrens 
55142856Snd150628 		while (waitpid(pid, &status, 0) != pid)
5515789Sahrens 			continue;
5516789Sahrens 
5517789Sahrens 		if (WIFEXITED(status)) {
5518789Sahrens 			if (WEXITSTATUS(status) != 0) {
5519789Sahrens 				(void) fprintf(stderr,
5520789Sahrens 				    "child exited with code %d\n",
5521789Sahrens 				    WEXITSTATUS(status));
5522789Sahrens 				exit(2);
5523789Sahrens 			}
55242856Snd150628 		} else if (WIFSIGNALED(status)) {
5525789Sahrens 			if (WTERMSIG(status) != SIGKILL) {
5526789Sahrens 				(void) fprintf(stderr,
5527789Sahrens 				    "child died with signal %d\n",
5528789Sahrens 				    WTERMSIG(status));
5529789Sahrens 				exit(3);
5530789Sahrens 			}
5531789Sahrens 			kills++;
55322856Snd150628 		} else {
55332856Snd150628 			(void) fprintf(stderr, "something strange happened "
55342856Snd150628 			    "to child\n");
55352856Snd150628 			exit(4);
5536789Sahrens 		}
5537789Sahrens 
5538789Sahrens 		iters++;
5539789Sahrens 
5540789Sahrens 		if (zopt_verbose >= 1) {
5541789Sahrens 			hrtime_t now = gethrtime();
5542789Sahrens 
554310922SJeff.Bonwick@Sun.COM 			now = MIN(now, zs->zs_proc_stop);
554410922SJeff.Bonwick@Sun.COM 			print_time(zs->zs_proc_stop - now, timebuf);
5545789Sahrens 			nicenum(zs->zs_space, numbuf);
5546789Sahrens 
5547789Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5548789Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5549789Sahrens 			    iters,
5550789Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
5551789Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
5552789Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
5553789Sahrens 			    numbuf,
555410922SJeff.Bonwick@Sun.COM 			    100.0 * (now - zs->zs_proc_start) /
5555789Sahrens 			    (zopt_time * NANOSEC), timebuf);
5556789Sahrens 		}
5557789Sahrens 
5558789Sahrens 		if (zopt_verbose >= 2) {
5559789Sahrens 			(void) printf("\nWorkload summary:\n\n");
5560789Sahrens 			(void) printf("%7s %9s   %s\n",
5561789Sahrens 			    "Calls", "Time", "Function");
5562789Sahrens 			(void) printf("%7s %9s   %s\n",
5563789Sahrens 			    "-----", "----", "--------");
556410922SJeff.Bonwick@Sun.COM 			for (int f = 0; f < ZTEST_FUNCS; f++) {
5565789Sahrens 				Dl_info dli;
5566789Sahrens 
5567789Sahrens 				zi = &zs->zs_info[f];
5568789Sahrens 				print_time(zi->zi_call_time, timebuf);
5569789Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
5570789Sahrens 				(void) printf("%7llu %9s   %s\n",
557110922SJeff.Bonwick@Sun.COM 				    (u_longlong_t)zi->zi_call_count, timebuf,
5572789Sahrens 				    dli.dli_sname);
5573789Sahrens 			}
5574789Sahrens 			(void) printf("\n");
5575789Sahrens 		}
5576789Sahrens 
5577789Sahrens 		/*
557810922SJeff.Bonwick@Sun.COM 		 * It's possible that we killed a child during a rename test,
557910922SJeff.Bonwick@Sun.COM 		 * in which case we'll have a 'ztest_tmp' pool lying around
558010922SJeff.Bonwick@Sun.COM 		 * instead of 'ztest'.  Do a blind rename in case this happened.
5581789Sahrens 		 */
558210922SJeff.Bonwick@Sun.COM 		kernel_init(FREAD);
558310922SJeff.Bonwick@Sun.COM 		if (spa_open(zopt_pool, &spa, FTAG) == 0) {
558410922SJeff.Bonwick@Sun.COM 			spa_close(spa, FTAG);
558510922SJeff.Bonwick@Sun.COM 		} else {
558610922SJeff.Bonwick@Sun.COM 			char tmpname[MAXNAMELEN];
558710922SJeff.Bonwick@Sun.COM 			kernel_fini();
558810922SJeff.Bonwick@Sun.COM 			kernel_init(FREAD | FWRITE);
558910922SJeff.Bonwick@Sun.COM 			(void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
559010922SJeff.Bonwick@Sun.COM 			    zopt_pool);
559110922SJeff.Bonwick@Sun.COM 			(void) spa_rename(tmpname, zopt_pool);
559210922SJeff.Bonwick@Sun.COM 		}
5593789Sahrens 		kernel_fini();
559410922SJeff.Bonwick@Sun.COM 
559510922SJeff.Bonwick@Sun.COM 		ztest_run_zdb(zopt_pool);
5596789Sahrens 	}
5597789Sahrens 
5598789Sahrens 	if (zopt_verbose >= 1) {
5599789Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5600789Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
5601789Sahrens 	}
5602789Sahrens 
5603789Sahrens 	return (0);
5604789Sahrens }
5605