xref: /titanic_52/usr/src/cmd/ztest/ztest.c (revision 3a57275a335306e90136ebd00a4689fe0ee72519)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*3a57275aSck153898  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
30fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * The overall design of the ztest program is as follows:
33fa9e4066Sahrens  *
34fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
35fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
36fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
37fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
38fa9e4066Sahrens  *
39fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
40fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
41fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
42fa9e4066Sahrens  *
43fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
44fa9e4066Sahrens  *     verify that self-healing data really works.
45fa9e4066Sahrens  *
46fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
47fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
48fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
49fa9e4066Sahrens  *
50fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
51fa9e4066Sahrens  *     we run the entire test in a child of the main process.
52fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
53fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
54fa9e4066Sahrens  *     The parent then runs the test again, using the existing
55fa9e4066Sahrens  *     storage pool, as many times as desired.
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
58fa9e4066Sahrens  *     many of the functional tests record the transaction group number
59fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
60fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
61fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
62fa9e4066Sahrens  *
63fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
64fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
65fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
66fa9e4066Sahrens  *
67fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
68fa9e4066Sahrens  *
69fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
70fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
71fa9e4066Sahrens  *
72fa9e4066Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
73fa9e4066Sahrens  * This can be useful when you're trying to distinguish temporal incursions
74fa9e4066Sahrens  * from plain old race conditions.
75fa9e4066Sahrens  */
76fa9e4066Sahrens 
77fa9e4066Sahrens #include <sys/zfs_context.h>
78fa9e4066Sahrens #include <sys/spa.h>
79fa9e4066Sahrens #include <sys/dmu.h>
80fa9e4066Sahrens #include <sys/txg.h>
81fa9e4066Sahrens #include <sys/zap.h>
82fa9e4066Sahrens #include <sys/dmu_traverse.h>
83fa9e4066Sahrens #include <sys/dmu_objset.h>
84fa9e4066Sahrens #include <sys/poll.h>
85fa9e4066Sahrens #include <sys/stat.h>
86fa9e4066Sahrens #include <sys/time.h>
87fa9e4066Sahrens #include <sys/wait.h>
88fa9e4066Sahrens #include <sys/mman.h>
89fa9e4066Sahrens #include <sys/resource.h>
90fa9e4066Sahrens #include <sys/zio.h>
91fa9e4066Sahrens #include <sys/zio_checksum.h>
92fa9e4066Sahrens #include <sys/zio_compress.h>
93fa9e4066Sahrens #include <sys/zil.h>
94fa9e4066Sahrens #include <sys/vdev_impl.h>
95fa9e4066Sahrens #include <sys/spa_impl.h>
96fa9e4066Sahrens #include <sys/dsl_prop.h>
97fa9e4066Sahrens #include <sys/refcount.h>
98fa9e4066Sahrens #include <stdio.h>
99004388ebScasper #include <stdio_ext.h>
100fa9e4066Sahrens #include <stdlib.h>
101fa9e4066Sahrens #include <unistd.h>
102fa9e4066Sahrens #include <signal.h>
103fa9e4066Sahrens #include <umem.h>
104fa9e4066Sahrens #include <dlfcn.h>
105fa9e4066Sahrens #include <ctype.h>
106fa9e4066Sahrens #include <math.h>
107fa9e4066Sahrens #include <sys/fs/zfs.h>
108fa9e4066Sahrens 
109fa9e4066Sahrens static char cmdname[] = "ztest";
110fa9e4066Sahrens static char *zopt_pool = cmdname;
111fa9e4066Sahrens 
112fa9e4066Sahrens static uint64_t zopt_vdevs = 5;
113fa9e4066Sahrens static uint64_t zopt_vdevtime;
114ecc2d604Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT;
115fa9e4066Sahrens static int zopt_mirrors = 2;
116fa9e4066Sahrens static int zopt_raidz = 4;
11799653d4eSeschrock static int zopt_raidz_parity = 1;
118fa9e4066Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE;
119ecc2d604Sbonwick static int zopt_datasets = 7;
120fa9e4066Sahrens static int zopt_threads = 23;
121fa9e4066Sahrens static uint64_t zopt_passtime = 60;	/* 60 seconds */
122fa9e4066Sahrens static uint64_t zopt_killrate = 70;	/* 70% kill rate */
123fa9e4066Sahrens static int zopt_verbose = 0;
124fa9e4066Sahrens static int zopt_init = 1;
125fa9e4066Sahrens static char *zopt_dir = "/tmp";
126fa9e4066Sahrens static uint64_t zopt_time = 300;	/* 5 minutes */
127fa9e4066Sahrens static int zopt_maxfaults;
1280a4e9518Sgw25295 static uint16_t zopt_write_fail_shift = 5;
129fa9e4066Sahrens 
130e05725b1Sbonwick typedef struct ztest_block_tag {
131e05725b1Sbonwick 	uint64_t	bt_objset;
132e05725b1Sbonwick 	uint64_t	bt_object;
133e05725b1Sbonwick 	uint64_t	bt_offset;
134e05725b1Sbonwick 	uint64_t	bt_txg;
135e05725b1Sbonwick 	uint64_t	bt_thread;
136e05725b1Sbonwick 	uint64_t	bt_seq;
137e05725b1Sbonwick } ztest_block_tag_t;
138e05725b1Sbonwick 
139fa9e4066Sahrens typedef struct ztest_args {
140e05725b1Sbonwick 	char		za_pool[MAXNAMELEN];
141e05725b1Sbonwick 	spa_t		*za_spa;
142fa9e4066Sahrens 	objset_t	*za_os;
143fa9e4066Sahrens 	zilog_t		*za_zilog;
144fa9e4066Sahrens 	thread_t	za_thread;
145fa9e4066Sahrens 	uint64_t	za_instance;
146fa9e4066Sahrens 	uint64_t	za_random;
147fa9e4066Sahrens 	uint64_t	za_diroff;
148fa9e4066Sahrens 	uint64_t	za_diroff_shared;
149ea8dc4b6Seschrock 	uint64_t	za_zil_seq;
150fa9e4066Sahrens 	hrtime_t	za_start;
151fa9e4066Sahrens 	hrtime_t	za_stop;
152fa9e4066Sahrens 	hrtime_t	za_kill;
153fa9e4066Sahrens 	traverse_handle_t *za_th;
154e05725b1Sbonwick 	/*
155e05725b1Sbonwick 	 * Thread-local variables can go here to aid debugging.
156e05725b1Sbonwick 	 */
157e05725b1Sbonwick 	ztest_block_tag_t za_rbt;
158e05725b1Sbonwick 	ztest_block_tag_t za_wbt;
159e05725b1Sbonwick 	dmu_object_info_t za_doi;
160e05725b1Sbonwick 	dmu_buf_t	*za_dbuf;
161fa9e4066Sahrens } ztest_args_t;
162fa9e4066Sahrens 
163fa9e4066Sahrens typedef void ztest_func_t(ztest_args_t *);
164fa9e4066Sahrens 
165fa9e4066Sahrens /*
166fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
167fa9e4066Sahrens  */
168fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
169fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
170fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
171fa9e4066Sahrens ztest_func_t ztest_zap;
172fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
173fa9e4066Sahrens ztest_func_t ztest_traverse;
174fa9e4066Sahrens ztest_func_t ztest_dsl_prop_get_set;
175fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
176fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
177fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
178fa9e4066Sahrens ztest_func_t ztest_fault_inject;
179fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
180fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
181fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
182fa9e4066Sahrens ztest_func_t ztest_scrub;
183fa9e4066Sahrens ztest_func_t ztest_spa_rename;
184fa9e4066Sahrens 
185fa9e4066Sahrens typedef struct ztest_info {
186fa9e4066Sahrens 	ztest_func_t	*zi_func;	/* test function */
187e05725b1Sbonwick 	uint64_t	zi_iters;	/* iterations per execution */
188fa9e4066Sahrens 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
189fa9e4066Sahrens 	uint64_t	zi_calls;	/* per-pass count */
190fa9e4066Sahrens 	uint64_t	zi_call_time;	/* per-pass time */
191fa9e4066Sahrens 	uint64_t	zi_call_total;	/* cumulative total */
192fa9e4066Sahrens 	uint64_t	zi_call_target;	/* target cumulative total */
193fa9e4066Sahrens } ztest_info_t;
194fa9e4066Sahrens 
195fa9e4066Sahrens uint64_t zopt_always = 0;		/* all the time */
196fa9e4066Sahrens uint64_t zopt_often = 1;		/* every second */
197fa9e4066Sahrens uint64_t zopt_sometimes = 10;		/* every 10 seconds */
198fa9e4066Sahrens uint64_t zopt_rarely = 60;		/* every 60 seconds */
199fa9e4066Sahrens 
200fa9e4066Sahrens ztest_info_t ztest_info[] = {
201e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
202e05725b1Sbonwick 	{ ztest_dmu_write_parallel,		30,	&zopt_always	},
203e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
204e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
205e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
206e05725b1Sbonwick 	{ ztest_traverse,			1,	&zopt_often	},
207e05725b1Sbonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_sometimes	},
208e05725b1Sbonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_sometimes	},
209e05725b1Sbonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_rarely	},
210e05725b1Sbonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes	},
211e05725b1Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
212e05725b1Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
213e05725b1Sbonwick 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
214e05725b1Sbonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
215e05725b1Sbonwick 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime	},
216e05725b1Sbonwick 	{ ztest_scrub,				1,	&zopt_vdevtime	},
217fa9e4066Sahrens };
218fa9e4066Sahrens 
219fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
220fa9e4066Sahrens 
221fa9e4066Sahrens #define	ZTEST_SYNC_LOCKS	16
222fa9e4066Sahrens 
223fa9e4066Sahrens /*
224fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
225fa9e4066Sahrens  */
226fa9e4066Sahrens typedef struct ztest_shared {
227fa9e4066Sahrens 	mutex_t		zs_vdev_lock;
228fa9e4066Sahrens 	rwlock_t	zs_name_lock;
229fa9e4066Sahrens 	uint64_t	zs_vdev_primaries;
230fa9e4066Sahrens 	uint64_t	zs_enospc_count;
231fa9e4066Sahrens 	hrtime_t	zs_start_time;
232fa9e4066Sahrens 	hrtime_t	zs_stop_time;
233fa9e4066Sahrens 	uint64_t	zs_alloc;
234fa9e4066Sahrens 	uint64_t	zs_space;
235fa9e4066Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
236fa9e4066Sahrens 	mutex_t		zs_sync_lock[ZTEST_SYNC_LOCKS];
237fa9e4066Sahrens 	uint64_t	zs_seq[ZTEST_SYNC_LOCKS];
238fa9e4066Sahrens } ztest_shared_t;
239fa9e4066Sahrens 
240fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
241fa9e4066Sahrens static ztest_shared_t *ztest_shared;
242fa9e4066Sahrens 
243fa9e4066Sahrens static int ztest_random_fd;
244fa9e4066Sahrens static int ztest_dump_core = 1;
245fa9e4066Sahrens 
2460a4e9518Sgw25295 static boolean_t ztest_exiting = B_FALSE;
2470a4e9518Sgw25295 
248e05725b1Sbonwick extern uint64_t metaslab_gang_bang;
249d63d470bSgw25295 extern uint16_t zio_zil_fail_shift;
2500a4e9518Sgw25295 extern uint16_t zio_io_fail_shift;
251fa9e4066Sahrens 
252fa9e4066Sahrens #define	ZTEST_DIROBJ		1
253fa9e4066Sahrens #define	ZTEST_MICROZAP_OBJ	2
254fa9e4066Sahrens #define	ZTEST_FATZAP_OBJ	3
255fa9e4066Sahrens 
256fa9e4066Sahrens #define	ZTEST_DIROBJ_BLOCKSIZE	(1 << 10)
257fa9e4066Sahrens #define	ZTEST_DIRSIZE		256
258fa9e4066Sahrens 
2591ce825d8Sraf static void usage(boolean_t) __NORETURN;
260f1b4288bSvb160487 
261fa9e4066Sahrens /*
262fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
263fa9e4066Sahrens  * debugging facilities.
264fa9e4066Sahrens  */
265fa9e4066Sahrens const char *
266fa9e4066Sahrens _umem_debug_init()
267fa9e4066Sahrens {
268fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
269fa9e4066Sahrens }
270fa9e4066Sahrens 
271fa9e4066Sahrens const char *
272fa9e4066Sahrens _umem_logging_init(void)
273fa9e4066Sahrens {
274fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
275fa9e4066Sahrens }
276fa9e4066Sahrens 
277fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
278fa9e4066Sahrens 
279fa9e4066Sahrens char *fatal_msg;
280fa9e4066Sahrens 
281fa9e4066Sahrens static void
282fa9e4066Sahrens fatal(int do_perror, char *message, ...)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	va_list args;
285fa9e4066Sahrens 	int save_errno = errno;
286fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
287fa9e4066Sahrens 
288fa9e4066Sahrens 	(void) fflush(stdout);
289fa9e4066Sahrens 
290fa9e4066Sahrens 	va_start(args, message);
291fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
292fa9e4066Sahrens 	/* LINTED */
293fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
294fa9e4066Sahrens 	va_end(args);
295fa9e4066Sahrens 	if (do_perror) {
296fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
297fa9e4066Sahrens 		    ": %s", strerror(save_errno));
298fa9e4066Sahrens 	}
299fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
300fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
301fa9e4066Sahrens 	if (ztest_dump_core)
302fa9e4066Sahrens 		abort();
303fa9e4066Sahrens 	exit(3);
304fa9e4066Sahrens }
305fa9e4066Sahrens 
306fa9e4066Sahrens static int
307fa9e4066Sahrens str2shift(const char *buf)
308fa9e4066Sahrens {
309fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
310fa9e4066Sahrens 	int i;
311fa9e4066Sahrens 
312fa9e4066Sahrens 	if (buf[0] == '\0')
313fa9e4066Sahrens 		return (0);
314fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
315fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
316fa9e4066Sahrens 			break;
317fa9e4066Sahrens 	}
318f1b4288bSvb160487 	if (i == strlen(ends)) {
319f1b4288bSvb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
320f1b4288bSvb160487 		    buf);
321f1b4288bSvb160487 		usage(B_FALSE);
322f1b4288bSvb160487 	}
323fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
324fa9e4066Sahrens 		return (10*i);
325fa9e4066Sahrens 	}
326f1b4288bSvb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
327f1b4288bSvb160487 	usage(B_FALSE);
328f1b4288bSvb160487 	/* NOTREACHED */
329fa9e4066Sahrens }
330fa9e4066Sahrens 
331fa9e4066Sahrens static uint64_t
332fa9e4066Sahrens nicenumtoull(const char *buf)
333fa9e4066Sahrens {
334fa9e4066Sahrens 	char *end;
335fa9e4066Sahrens 	uint64_t val;
336fa9e4066Sahrens 
337fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
338fa9e4066Sahrens 	if (end == buf) {
339f1b4288bSvb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
340f1b4288bSvb160487 		usage(B_FALSE);
341fa9e4066Sahrens 	} else if (end[0] == '.') {
342fa9e4066Sahrens 		double fval = strtod(buf, &end);
343fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
344f1b4288bSvb160487 		if (fval > UINT64_MAX) {
345f1b4288bSvb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
346f1b4288bSvb160487 			    buf);
347f1b4288bSvb160487 			usage(B_FALSE);
348f1b4288bSvb160487 		}
349fa9e4066Sahrens 		val = (uint64_t)fval;
350fa9e4066Sahrens 	} else {
351fa9e4066Sahrens 		int shift = str2shift(end);
352f1b4288bSvb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
353f1b4288bSvb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
354f1b4288bSvb160487 			    buf);
355f1b4288bSvb160487 			usage(B_FALSE);
356f1b4288bSvb160487 		}
357fa9e4066Sahrens 		val <<= shift;
358fa9e4066Sahrens 	}
359fa9e4066Sahrens 	return (val);
360fa9e4066Sahrens }
361fa9e4066Sahrens 
362fa9e4066Sahrens static void
363f1b4288bSvb160487 usage(boolean_t requested)
364fa9e4066Sahrens {
365fa9e4066Sahrens 	char nice_vdev_size[10];
366fa9e4066Sahrens 	char nice_gang_bang[10];
367f1b4288bSvb160487 	FILE *fp = requested ? stdout : stderr;
368fa9e4066Sahrens 
369fa9e4066Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
370e05725b1Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
371fa9e4066Sahrens 
372f1b4288bSvb160487 	(void) fprintf(fp, "Usage: %s\n"
373fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
374fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
375ecc2d604Sbonwick 	    "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
376fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
377fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
37899653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
379fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
380fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
381fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
382fa9e4066Sahrens 	    "\t[-i initialize pool i times (default: %d)]\n"
383fa9e4066Sahrens 	    "\t[-k kill percentage (default: %llu%%)]\n"
384fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
385fa9e4066Sahrens 	    "\t[-f file directory for vdev files (default: %s)]\n"
386fa9e4066Sahrens 	    "\t[-V(erbose)] (use multiple times for ever more blather)\n"
387ecc2d604Sbonwick 	    "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
388fa9e4066Sahrens 	    "\t[-T time] total run time (default: %llu sec)\n"
389fa9e4066Sahrens 	    "\t[-P passtime] time per pass (default: %llu sec)\n"
390d63d470bSgw25295 	    "\t[-z zil failure rate (default: fail every 2^%llu allocs)]\n"
3910a4e9518Sgw25295 	    "\t[-w write failure rate (default: fail every 2^%llu allocs)]\n"
392f1b4288bSvb160487 	    "\t[-h] (print help)\n"
393fa9e4066Sahrens 	    "",
394fa9e4066Sahrens 	    cmdname,
395fa9e4066Sahrens 	    (u_longlong_t)zopt_vdevs,			/* -v */
396fa9e4066Sahrens 	    nice_vdev_size,				/* -s */
397ecc2d604Sbonwick 	    zopt_ashift,				/* -a */
398fa9e4066Sahrens 	    zopt_mirrors,				/* -m */
399fa9e4066Sahrens 	    zopt_raidz,					/* -r */
40099653d4eSeschrock 	    zopt_raidz_parity,				/* -R */
401ecc2d604Sbonwick 	    zopt_datasets,				/* -d */
402fa9e4066Sahrens 	    zopt_threads,				/* -t */
403fa9e4066Sahrens 	    nice_gang_bang,				/* -g */
404fa9e4066Sahrens 	    zopt_init,					/* -i */
405fa9e4066Sahrens 	    (u_longlong_t)zopt_killrate,		/* -k */
406fa9e4066Sahrens 	    zopt_pool,					/* -p */
407fa9e4066Sahrens 	    zopt_dir,					/* -f */
408fa9e4066Sahrens 	    (u_longlong_t)zopt_time,			/* -T */
409d63d470bSgw25295 	    (u_longlong_t)zopt_passtime,		/* -P */
4100a4e9518Sgw25295 	    (u_longlong_t)zio_zil_fail_shift,		/* -z */
4110a4e9518Sgw25295 	    (u_longlong_t)zopt_write_fail_shift);	/* -w */
412f1b4288bSvb160487 	exit(requested ? 0 : 1);
413fa9e4066Sahrens }
414fa9e4066Sahrens 
415fa9e4066Sahrens static uint64_t
416fa9e4066Sahrens ztest_random(uint64_t range)
417fa9e4066Sahrens {
418fa9e4066Sahrens 	uint64_t r;
419fa9e4066Sahrens 
420fa9e4066Sahrens 	if (range == 0)
421fa9e4066Sahrens 		return (0);
422fa9e4066Sahrens 
423fa9e4066Sahrens 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
424fa9e4066Sahrens 		fatal(1, "short read from /dev/urandom");
425fa9e4066Sahrens 
426fa9e4066Sahrens 	return (r % range);
427fa9e4066Sahrens }
428fa9e4066Sahrens 
429fa9e4066Sahrens static void
430fa9e4066Sahrens ztest_record_enospc(char *s)
431fa9e4066Sahrens {
432fa9e4066Sahrens 	dprintf("ENOSPC doing: %s\n", s ? s : "<unknown>");
433fa9e4066Sahrens 	ztest_shared->zs_enospc_count++;
434fa9e4066Sahrens }
435fa9e4066Sahrens 
436fa9e4066Sahrens static void
437fa9e4066Sahrens process_options(int argc, char **argv)
438fa9e4066Sahrens {
439fa9e4066Sahrens 	int opt;
440fa9e4066Sahrens 	uint64_t value;
441fa9e4066Sahrens 
442fa9e4066Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
443e05725b1Sbonwick 	metaslab_gang_bang = 32 << 10;
444fa9e4066Sahrens 
445d63d470bSgw25295 	/* Default value, fail every 32nd allocation */
446d63d470bSgw25295 	zio_zil_fail_shift = 5;
447d63d470bSgw25295 
448fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
4490a4e9518Sgw25295 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:z:w:h")) != EOF) {
450fa9e4066Sahrens 		value = 0;
451fa9e4066Sahrens 		switch (opt) {
452fa9e4066Sahrens 		case 'v':
453fa9e4066Sahrens 		case 's':
454ecc2d604Sbonwick 		case 'a':
455fa9e4066Sahrens 		case 'm':
456fa9e4066Sahrens 		case 'r':
45799653d4eSeschrock 		case 'R':
458fa9e4066Sahrens 		case 'd':
459fa9e4066Sahrens 		case 't':
460fa9e4066Sahrens 		case 'g':
461fa9e4066Sahrens 		case 'i':
462fa9e4066Sahrens 		case 'k':
463fa9e4066Sahrens 		case 'T':
464fa9e4066Sahrens 		case 'P':
465d63d470bSgw25295 		case 'z':
4660a4e9518Sgw25295 		case 'w':
467fa9e4066Sahrens 			value = nicenumtoull(optarg);
468fa9e4066Sahrens 		}
469fa9e4066Sahrens 		switch (opt) {
470fa9e4066Sahrens 		case 'v':
471fa9e4066Sahrens 			zopt_vdevs = value;
472fa9e4066Sahrens 			break;
473fa9e4066Sahrens 		case 's':
474fa9e4066Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
475fa9e4066Sahrens 			break;
476ecc2d604Sbonwick 		case 'a':
477ecc2d604Sbonwick 			zopt_ashift = value;
478ecc2d604Sbonwick 			break;
479fa9e4066Sahrens 		case 'm':
480fa9e4066Sahrens 			zopt_mirrors = value;
481fa9e4066Sahrens 			break;
482fa9e4066Sahrens 		case 'r':
483fa9e4066Sahrens 			zopt_raidz = MAX(1, value);
484fa9e4066Sahrens 			break;
48599653d4eSeschrock 		case 'R':
48699653d4eSeschrock 			zopt_raidz_parity = MIN(MAX(value, 1), 2);
48799653d4eSeschrock 			break;
488fa9e4066Sahrens 		case 'd':
489ecc2d604Sbonwick 			zopt_datasets = MAX(1, value);
490fa9e4066Sahrens 			break;
491fa9e4066Sahrens 		case 't':
492fa9e4066Sahrens 			zopt_threads = MAX(1, value);
493fa9e4066Sahrens 			break;
494fa9e4066Sahrens 		case 'g':
495e05725b1Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
496fa9e4066Sahrens 			break;
497fa9e4066Sahrens 		case 'i':
498fa9e4066Sahrens 			zopt_init = value;
499fa9e4066Sahrens 			break;
500fa9e4066Sahrens 		case 'k':
501fa9e4066Sahrens 			zopt_killrate = value;
502fa9e4066Sahrens 			break;
503fa9e4066Sahrens 		case 'p':
504fa9e4066Sahrens 			zopt_pool = strdup(optarg);
505fa9e4066Sahrens 			break;
506fa9e4066Sahrens 		case 'f':
507fa9e4066Sahrens 			zopt_dir = strdup(optarg);
508fa9e4066Sahrens 			break;
509fa9e4066Sahrens 		case 'V':
510fa9e4066Sahrens 			zopt_verbose++;
511fa9e4066Sahrens 			break;
512fa9e4066Sahrens 		case 'E':
513fa9e4066Sahrens 			zopt_init = 0;
514fa9e4066Sahrens 			break;
515fa9e4066Sahrens 		case 'T':
516fa9e4066Sahrens 			zopt_time = value;
517fa9e4066Sahrens 			break;
518fa9e4066Sahrens 		case 'P':
519fa9e4066Sahrens 			zopt_passtime = MAX(1, value);
520fa9e4066Sahrens 			break;
521d63d470bSgw25295 		case 'z':
522d63d470bSgw25295 			zio_zil_fail_shift = MIN(value, 16);
523d63d470bSgw25295 			break;
5240a4e9518Sgw25295 		case 'w':
5250a4e9518Sgw25295 			zopt_write_fail_shift = MIN(value, 16);
5260a4e9518Sgw25295 			break;
527f1b4288bSvb160487 		case 'h':
528f1b4288bSvb160487 			usage(B_TRUE);
529f1b4288bSvb160487 			break;
530fa9e4066Sahrens 		case '?':
531fa9e4066Sahrens 		default:
532f1b4288bSvb160487 			usage(B_FALSE);
533fa9e4066Sahrens 			break;
534fa9e4066Sahrens 		}
535fa9e4066Sahrens 	}
536fa9e4066Sahrens 
53799653d4eSeschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
53899653d4eSeschrock 
539fa9e4066Sahrens 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
54099653d4eSeschrock 	zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
541fa9e4066Sahrens }
542fa9e4066Sahrens 
543ecc2d604Sbonwick static uint64_t
544ecc2d604Sbonwick ztest_get_ashift(void)
545ecc2d604Sbonwick {
546ecc2d604Sbonwick 	if (zopt_ashift == 0)
547ecc2d604Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
548ecc2d604Sbonwick 	return (zopt_ashift);
549ecc2d604Sbonwick }
550ecc2d604Sbonwick 
551fa9e4066Sahrens static nvlist_t *
552fa9e4066Sahrens make_vdev_file(size_t size)
553fa9e4066Sahrens {
554fa9e4066Sahrens 	char dev_name[MAXPATHLEN];
555fa9e4066Sahrens 	uint64_t vdev;
556ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
557fa9e4066Sahrens 	int fd;
558fa9e4066Sahrens 	nvlist_t *file;
559fa9e4066Sahrens 
560fa9e4066Sahrens 	if (size == 0) {
561fa9e4066Sahrens 		(void) snprintf(dev_name, sizeof (dev_name), "%s",
562fa9e4066Sahrens 		    "/dev/bogus");
563fa9e4066Sahrens 	} else {
564fa9e4066Sahrens 		vdev = ztest_shared->zs_vdev_primaries++;
565fa9e4066Sahrens 		(void) sprintf(dev_name, ztest_dev_template,
566fa9e4066Sahrens 		    zopt_dir, zopt_pool, vdev);
567fa9e4066Sahrens 
568fa9e4066Sahrens 		fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
569fa9e4066Sahrens 		if (fd == -1)
570fa9e4066Sahrens 			fatal(1, "can't open %s", dev_name);
571fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
572fa9e4066Sahrens 			fatal(1, "can't ftruncate %s", dev_name);
573fa9e4066Sahrens 		(void) close(fd);
574fa9e4066Sahrens 	}
575fa9e4066Sahrens 
576fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
577fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
578fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, dev_name) == 0);
579ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
580fa9e4066Sahrens 
581fa9e4066Sahrens 	return (file);
582fa9e4066Sahrens }
583fa9e4066Sahrens 
584fa9e4066Sahrens static nvlist_t *
585fa9e4066Sahrens make_vdev_raidz(size_t size, int r)
586fa9e4066Sahrens {
587fa9e4066Sahrens 	nvlist_t *raidz, **child;
588fa9e4066Sahrens 	int c;
589fa9e4066Sahrens 
590fa9e4066Sahrens 	if (r < 2)
591fa9e4066Sahrens 		return (make_vdev_file(size));
592fa9e4066Sahrens 
593fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
594fa9e4066Sahrens 
595fa9e4066Sahrens 	for (c = 0; c < r; c++)
596fa9e4066Sahrens 		child[c] = make_vdev_file(size);
597fa9e4066Sahrens 
598fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
599fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
600fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
60199653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
60299653d4eSeschrock 	    zopt_raidz_parity) == 0);
603fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
604fa9e4066Sahrens 	    child, r) == 0);
605fa9e4066Sahrens 
606fa9e4066Sahrens 	for (c = 0; c < r; c++)
607fa9e4066Sahrens 		nvlist_free(child[c]);
608fa9e4066Sahrens 
609fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
610fa9e4066Sahrens 
611fa9e4066Sahrens 	return (raidz);
612fa9e4066Sahrens }
613fa9e4066Sahrens 
614fa9e4066Sahrens static nvlist_t *
6158654d025Sperrin make_vdev_mirror(size_t size, int log, int r, int m)
616fa9e4066Sahrens {
617fa9e4066Sahrens 	nvlist_t *mirror, **child;
618fa9e4066Sahrens 	int c;
619fa9e4066Sahrens 
620fa9e4066Sahrens 	if (m < 1)
621fa9e4066Sahrens 		return (make_vdev_raidz(size, r));
622fa9e4066Sahrens 
623fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
624fa9e4066Sahrens 
625fa9e4066Sahrens 	for (c = 0; c < m; c++)
626fa9e4066Sahrens 		child[c] = make_vdev_raidz(size, r);
627fa9e4066Sahrens 
628fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
629fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
630fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
631fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
632fa9e4066Sahrens 	    child, m) == 0);
6338654d025Sperrin 	VERIFY(nvlist_add_uint64(mirror, ZPOOL_CONFIG_IS_LOG, log) == 0);
634fa9e4066Sahrens 
635fa9e4066Sahrens 	for (c = 0; c < m; c++)
636fa9e4066Sahrens 		nvlist_free(child[c]);
637fa9e4066Sahrens 
638fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
639fa9e4066Sahrens 
640fa9e4066Sahrens 	return (mirror);
641fa9e4066Sahrens }
642fa9e4066Sahrens 
643fa9e4066Sahrens static nvlist_t *
6448654d025Sperrin make_vdev_root(size_t size, int log, int r, int m, int t)
645fa9e4066Sahrens {
646fa9e4066Sahrens 	nvlist_t *root, **child;
647fa9e4066Sahrens 	int c;
648fa9e4066Sahrens 
649fa9e4066Sahrens 	ASSERT(t > 0);
650fa9e4066Sahrens 
651fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
652fa9e4066Sahrens 
653fa9e4066Sahrens 	for (c = 0; c < t; c++)
6548654d025Sperrin 		child[c] = make_vdev_mirror(size, log, r, m);
655fa9e4066Sahrens 
656fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
657fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
658fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN,
659fa9e4066Sahrens 	    child, t) == 0);
660fa9e4066Sahrens 
661fa9e4066Sahrens 	for (c = 0; c < t; c++)
662fa9e4066Sahrens 		nvlist_free(child[c]);
663fa9e4066Sahrens 
664fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
665fa9e4066Sahrens 
666fa9e4066Sahrens 	return (root);
667fa9e4066Sahrens }
668fa9e4066Sahrens 
669fa9e4066Sahrens static void
670fa9e4066Sahrens ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
671fa9e4066Sahrens {
672fa9e4066Sahrens 	int bs = SPA_MINBLOCKSHIFT +
673fa9e4066Sahrens 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
674fa9e4066Sahrens 	int ibs = DN_MIN_INDBLKSHIFT +
675fa9e4066Sahrens 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
676fa9e4066Sahrens 	int error;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
679fa9e4066Sahrens 	if (error) {
680fa9e4066Sahrens 		char osname[300];
681fa9e4066Sahrens 		dmu_objset_name(os, osname);
682fa9e4066Sahrens 		fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
683fa9e4066Sahrens 		    osname, object, 1 << bs, ibs, error);
684fa9e4066Sahrens 	}
685fa9e4066Sahrens }
686fa9e4066Sahrens 
687fa9e4066Sahrens static uint8_t
688fa9e4066Sahrens ztest_random_checksum(void)
689fa9e4066Sahrens {
690fa9e4066Sahrens 	uint8_t checksum;
691fa9e4066Sahrens 
692fa9e4066Sahrens 	do {
693fa9e4066Sahrens 		checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
694fa9e4066Sahrens 	} while (zio_checksum_table[checksum].ci_zbt);
695fa9e4066Sahrens 
696fa9e4066Sahrens 	if (checksum == ZIO_CHECKSUM_OFF)
697fa9e4066Sahrens 		checksum = ZIO_CHECKSUM_ON;
698fa9e4066Sahrens 
699fa9e4066Sahrens 	return (checksum);
700fa9e4066Sahrens }
701fa9e4066Sahrens 
702fa9e4066Sahrens static uint8_t
703fa9e4066Sahrens ztest_random_compress(void)
704fa9e4066Sahrens {
705fa9e4066Sahrens 	return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
706fa9e4066Sahrens }
707fa9e4066Sahrens 
708fa9e4066Sahrens typedef struct ztest_replay {
709fa9e4066Sahrens 	objset_t	*zr_os;
710fa9e4066Sahrens 	uint64_t	zr_assign;
711fa9e4066Sahrens } ztest_replay_t;
712fa9e4066Sahrens 
713fa9e4066Sahrens static int
714fa9e4066Sahrens ztest_replay_create(ztest_replay_t *zr, lr_create_t *lr, boolean_t byteswap)
715fa9e4066Sahrens {
716fa9e4066Sahrens 	objset_t *os = zr->zr_os;
717fa9e4066Sahrens 	dmu_tx_t *tx;
718fa9e4066Sahrens 	int error;
719fa9e4066Sahrens 
720fa9e4066Sahrens 	if (byteswap)
721fa9e4066Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
722fa9e4066Sahrens 
723fa9e4066Sahrens 	tx = dmu_tx_create(os);
724fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
725fa9e4066Sahrens 	error = dmu_tx_assign(tx, zr->zr_assign);
726fa9e4066Sahrens 	if (error) {
727fa9e4066Sahrens 		dmu_tx_abort(tx);
728fa9e4066Sahrens 		return (error);
729fa9e4066Sahrens 	}
730fa9e4066Sahrens 
731fa9e4066Sahrens 	error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
732fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
73399653d4eSeschrock 	ASSERT3U(error, ==, 0);
734fa9e4066Sahrens 	dmu_tx_commit(tx);
735fa9e4066Sahrens 
736fa9e4066Sahrens 	if (zopt_verbose >= 5) {
737fa9e4066Sahrens 		char osname[MAXNAMELEN];
738fa9e4066Sahrens 		dmu_objset_name(os, osname);
739fa9e4066Sahrens 		(void) printf("replay create of %s object %llu"
740fa9e4066Sahrens 		    " in txg %llu = %d\n",
741fa9e4066Sahrens 		    osname, (u_longlong_t)lr->lr_doid,
742fa9e4066Sahrens 		    (u_longlong_t)zr->zr_assign, error);
743fa9e4066Sahrens 	}
744fa9e4066Sahrens 
745fa9e4066Sahrens 	return (error);
746fa9e4066Sahrens }
747fa9e4066Sahrens 
748fa9e4066Sahrens static int
749fa9e4066Sahrens ztest_replay_remove(ztest_replay_t *zr, lr_remove_t *lr, boolean_t byteswap)
750fa9e4066Sahrens {
751fa9e4066Sahrens 	objset_t *os = zr->zr_os;
752fa9e4066Sahrens 	dmu_tx_t *tx;
753fa9e4066Sahrens 	int error;
754fa9e4066Sahrens 
755fa9e4066Sahrens 	if (byteswap)
756fa9e4066Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
757fa9e4066Sahrens 
758fa9e4066Sahrens 	tx = dmu_tx_create(os);
759fa9e4066Sahrens 	dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
760fa9e4066Sahrens 	error = dmu_tx_assign(tx, zr->zr_assign);
761fa9e4066Sahrens 	if (error) {
762fa9e4066Sahrens 		dmu_tx_abort(tx);
763fa9e4066Sahrens 		return (error);
764fa9e4066Sahrens 	}
765fa9e4066Sahrens 
766fa9e4066Sahrens 	error = dmu_object_free(os, lr->lr_doid, tx);
767fa9e4066Sahrens 	dmu_tx_commit(tx);
768fa9e4066Sahrens 
769fa9e4066Sahrens 	return (error);
770fa9e4066Sahrens }
771fa9e4066Sahrens 
772fa9e4066Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
773fa9e4066Sahrens 	NULL,			/* 0 no such transaction type */
774fa9e4066Sahrens 	ztest_replay_create,	/* TX_CREATE */
775fa9e4066Sahrens 	NULL,			/* TX_MKDIR */
776fa9e4066Sahrens 	NULL,			/* TX_MKXATTR */
777fa9e4066Sahrens 	NULL,			/* TX_SYMLINK */
778fa9e4066Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
779fa9e4066Sahrens 	NULL,			/* TX_RMDIR */
780fa9e4066Sahrens 	NULL,			/* TX_LINK */
781fa9e4066Sahrens 	NULL,			/* TX_RENAME */
782fa9e4066Sahrens 	NULL,			/* TX_WRITE */
783fa9e4066Sahrens 	NULL,			/* TX_TRUNCATE */
784fa9e4066Sahrens 	NULL,			/* TX_SETATTR */
785fa9e4066Sahrens 	NULL,			/* TX_ACL */
786fa9e4066Sahrens };
787fa9e4066Sahrens 
788fa9e4066Sahrens /*
789fa9e4066Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
790fa9e4066Sahrens  * or create a pool with a bad vdev spec.
791fa9e4066Sahrens  */
792fa9e4066Sahrens void
793fa9e4066Sahrens ztest_spa_create_destroy(ztest_args_t *za)
794fa9e4066Sahrens {
795fa9e4066Sahrens 	int error;
796fa9e4066Sahrens 	spa_t *spa;
797fa9e4066Sahrens 	nvlist_t *nvroot;
798fa9e4066Sahrens 
799fa9e4066Sahrens 	/*
800fa9e4066Sahrens 	 * Attempt to create using a bad file.
801fa9e4066Sahrens 	 */
8028654d025Sperrin 	nvroot = make_vdev_root(0, 0, 0, 0, 1);
803228975ccSek110237 	error = spa_create("ztest_bad_file", nvroot, NULL, NULL);
804fa9e4066Sahrens 	nvlist_free(nvroot);
805fa9e4066Sahrens 	if (error != ENOENT)
806fa9e4066Sahrens 		fatal(0, "spa_create(bad_file) = %d", error);
807fa9e4066Sahrens 
808fa9e4066Sahrens 	/*
809fa9e4066Sahrens 	 * Attempt to create using a bad mirror.
810fa9e4066Sahrens 	 */
8118654d025Sperrin 	nvroot = make_vdev_root(0, 0, 0, 2, 1);
812228975ccSek110237 	error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL);
813fa9e4066Sahrens 	nvlist_free(nvroot);
814fa9e4066Sahrens 	if (error != ENOENT)
815fa9e4066Sahrens 		fatal(0, "spa_create(bad_mirror) = %d", error);
816fa9e4066Sahrens 
817fa9e4066Sahrens 	/*
818fa9e4066Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
819fa9e4066Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
820fa9e4066Sahrens 	 */
821fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
8228654d025Sperrin 	nvroot = make_vdev_root(0, 0, 0, 0, 1);
823228975ccSek110237 	error = spa_create(za->za_pool, nvroot, NULL, NULL);
824fa9e4066Sahrens 	nvlist_free(nvroot);
825fa9e4066Sahrens 	if (error != EEXIST)
826fa9e4066Sahrens 		fatal(0, "spa_create(whatever) = %d", error);
827fa9e4066Sahrens 
828fa9e4066Sahrens 	error = spa_open(za->za_pool, &spa, FTAG);
829fa9e4066Sahrens 	if (error)
830fa9e4066Sahrens 		fatal(0, "spa_open() = %d", error);
831fa9e4066Sahrens 
832fa9e4066Sahrens 	error = spa_destroy(za->za_pool);
833fa9e4066Sahrens 	if (error != EBUSY)
834fa9e4066Sahrens 		fatal(0, "spa_destroy() = %d", error);
835fa9e4066Sahrens 
836fa9e4066Sahrens 	spa_close(spa, FTAG);
837fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
838fa9e4066Sahrens }
839fa9e4066Sahrens 
840fa9e4066Sahrens /*
841fa9e4066Sahrens  * Verify that vdev_add() works as expected.
842fa9e4066Sahrens  */
843fa9e4066Sahrens void
844fa9e4066Sahrens ztest_vdev_add_remove(ztest_args_t *za)
845fa9e4066Sahrens {
846e05725b1Sbonwick 	spa_t *spa = za->za_spa;
847fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
848fa9e4066Sahrens 	nvlist_t *nvroot;
849fa9e4066Sahrens 	int error;
850fa9e4066Sahrens 
851fa9e4066Sahrens 	if (zopt_verbose >= 6)
852fa9e4066Sahrens 		(void) printf("adding vdev\n");
853fa9e4066Sahrens 
854fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
855fa9e4066Sahrens 
856ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	ztest_shared->zs_vdev_primaries =
859fa9e4066Sahrens 	    spa->spa_root_vdev->vdev_children * leaves;
860fa9e4066Sahrens 
861ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
862fa9e4066Sahrens 
8638654d025Sperrin 	/*
8648654d025Sperrin 	 * Make 1/4 of the devices be log devices.
8658654d025Sperrin 	 */
8668654d025Sperrin 	nvroot = make_vdev_root(zopt_vdev_size,
8678654d025Sperrin 	    ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
8688654d025Sperrin 
869fa9e4066Sahrens 	error = spa_vdev_add(spa, nvroot);
870fa9e4066Sahrens 	nvlist_free(nvroot);
871fa9e4066Sahrens 
872fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
873fa9e4066Sahrens 
874fa9e4066Sahrens 	if (error == ENOSPC)
875fa9e4066Sahrens 		ztest_record_enospc("spa_vdev_add");
876fa9e4066Sahrens 	else if (error != 0)
877fa9e4066Sahrens 		fatal(0, "spa_vdev_add() = %d", error);
878fa9e4066Sahrens 
879fa9e4066Sahrens 	if (zopt_verbose >= 6)
880fa9e4066Sahrens 		(void) printf("spa_vdev_add = %d, as expected\n", error);
881fa9e4066Sahrens }
882fa9e4066Sahrens 
883ea8dc4b6Seschrock static vdev_t *
884ea8dc4b6Seschrock vdev_lookup_by_path(vdev_t *vd, const char *path)
885ea8dc4b6Seschrock {
886ea8dc4b6Seschrock 	int c;
887ea8dc4b6Seschrock 	vdev_t *mvd;
888ea8dc4b6Seschrock 
889ea8dc4b6Seschrock 	if (vd->vdev_path != NULL) {
890ea8dc4b6Seschrock 		if (vd->vdev_wholedisk == 1) {
891ea8dc4b6Seschrock 			/*
892ea8dc4b6Seschrock 			 * For whole disks, the internal path has 's0', but the
893ea8dc4b6Seschrock 			 * path passed in by the user doesn't.
894ea8dc4b6Seschrock 			 */
895ea8dc4b6Seschrock 			if (strlen(path) == strlen(vd->vdev_path) - 2 &&
896ea8dc4b6Seschrock 			    strncmp(path, vd->vdev_path, strlen(path)) == 0)
897ea8dc4b6Seschrock 				return (vd);
898ea8dc4b6Seschrock 		} else if (strcmp(path, vd->vdev_path) == 0) {
899ea8dc4b6Seschrock 			return (vd);
900ea8dc4b6Seschrock 		}
901ea8dc4b6Seschrock 	}
902ea8dc4b6Seschrock 
903ea8dc4b6Seschrock 	for (c = 0; c < vd->vdev_children; c++)
904ea8dc4b6Seschrock 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
905ea8dc4b6Seschrock 		    NULL)
906ea8dc4b6Seschrock 			return (mvd);
907ea8dc4b6Seschrock 
908ea8dc4b6Seschrock 	return (NULL);
909ea8dc4b6Seschrock }
910ea8dc4b6Seschrock 
911fa9e4066Sahrens /*
912fa9e4066Sahrens  * Verify that we can attach and detach devices.
913fa9e4066Sahrens  */
914fa9e4066Sahrens void
915fa9e4066Sahrens ztest_vdev_attach_detach(ztest_args_t *za)
916fa9e4066Sahrens {
917e05725b1Sbonwick 	spa_t *spa = za->za_spa;
918fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
919ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
920fa9e4066Sahrens 	nvlist_t *root, *file;
921fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
922fa9e4066Sahrens 	uint64_t leaf, top;
923ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
924ea8dc4b6Seschrock 	size_t oldsize, newsize;
925ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
926fa9e4066Sahrens 	int replacing;
927fa9e4066Sahrens 	int error, expected_error;
928fa9e4066Sahrens 	int fd;
929fa9e4066Sahrens 
930fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
931fa9e4066Sahrens 
932ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
933fa9e4066Sahrens 
934fa9e4066Sahrens 	/*
935fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
936fa9e4066Sahrens 	 */
937fa9e4066Sahrens 	replacing = ztest_random(2);
938fa9e4066Sahrens 
939fa9e4066Sahrens 	/*
940fa9e4066Sahrens 	 * Pick a random top-level vdev.
941fa9e4066Sahrens 	 */
942fa9e4066Sahrens 	top = ztest_random(rvd->vdev_children);
943fa9e4066Sahrens 
944fa9e4066Sahrens 	/*
945fa9e4066Sahrens 	 * Pick a random leaf within it.
946fa9e4066Sahrens 	 */
947fa9e4066Sahrens 	leaf = ztest_random(leaves);
948fa9e4066Sahrens 
949fa9e4066Sahrens 	/*
950fa9e4066Sahrens 	 * Generate the path to this leaf.  The filename will end with 'a'.
951fa9e4066Sahrens 	 * We'll alternate replacements with a filename that ends with 'b'.
952fa9e4066Sahrens 	 */
953ea8dc4b6Seschrock 	(void) snprintf(oldpath, sizeof (oldpath),
954fa9e4066Sahrens 	    ztest_dev_template, zopt_dir, zopt_pool, top * leaves + leaf);
955fa9e4066Sahrens 
956ea8dc4b6Seschrock 	bcopy(oldpath, newpath, MAXPATHLEN);
957fa9e4066Sahrens 
958fa9e4066Sahrens 	/*
959fa9e4066Sahrens 	 * If the 'a' file isn't part of the pool, the 'b' file must be.
960fa9e4066Sahrens 	 */
961ea8dc4b6Seschrock 	if (vdev_lookup_by_path(rvd, oldpath) == NULL)
962ea8dc4b6Seschrock 		oldpath[strlen(oldpath) - 1] = 'b';
963fa9e4066Sahrens 	else
964ea8dc4b6Seschrock 		newpath[strlen(newpath) - 1] = 'b';
965fa9e4066Sahrens 
966fa9e4066Sahrens 	/*
967ea8dc4b6Seschrock 	 * Now oldpath represents something that's already in the pool,
968ea8dc4b6Seschrock 	 * and newpath is the thing we'll try to attach.
969fa9e4066Sahrens 	 */
970ea8dc4b6Seschrock 	oldvd = vdev_lookup_by_path(rvd, oldpath);
971ea8dc4b6Seschrock 	newvd = vdev_lookup_by_path(rvd, newpath);
972ea8dc4b6Seschrock 	ASSERT(oldvd != NULL);
973ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
974fa9e4066Sahrens 
975fa9e4066Sahrens 	/*
976ea8dc4b6Seschrock 	 * Make newsize a little bigger or smaller than oldsize.
977fa9e4066Sahrens 	 * If it's smaller, the attach should fail.
978fa9e4066Sahrens 	 * If it's larger, and we're doing a replace,
979fa9e4066Sahrens 	 * we should get dynamic LUN growth when we're done.
980fa9e4066Sahrens 	 */
981ea8dc4b6Seschrock 	oldsize = vdev_get_rsize(oldvd);
982ea8dc4b6Seschrock 	newsize = 10 * oldsize / (9 + ztest_random(3));
983fa9e4066Sahrens 
984fa9e4066Sahrens 	/*
985fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
986fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
987fa9e4066Sahrens 	 *
988ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
989fa9e4066Sahrens 	 *
990ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
991fa9e4066Sahrens 	 */
992b5989ec7Seschrock 	if (newvd != NULL)
993b5989ec7Seschrock 		expected_error = EBUSY;
994b5989ec7Seschrock 	else if (pvd->vdev_ops != &vdev_mirror_ops &&
995fa9e4066Sahrens 	    pvd->vdev_ops != &vdev_root_ops &&
996fa9e4066Sahrens 	    (!replacing || pvd->vdev_ops == &vdev_replacing_ops))
997fa9e4066Sahrens 		expected_error = ENOTSUP;
998ea8dc4b6Seschrock 	else if (newsize < oldsize)
999fa9e4066Sahrens 		expected_error = EOVERFLOW;
1000ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
1001ecc2d604Sbonwick 		expected_error = EDOM;
1002fa9e4066Sahrens 	else
1003fa9e4066Sahrens 		expected_error = 0;
1004fa9e4066Sahrens 
1005fa9e4066Sahrens 	/*
1006ea8dc4b6Seschrock 	 * If newvd isn't already part of the pool, create it.
1007fa9e4066Sahrens 	 */
1008ea8dc4b6Seschrock 	if (newvd == NULL) {
1009ea8dc4b6Seschrock 		fd = open(newpath, O_RDWR | O_CREAT | O_TRUNC, 0666);
1010fa9e4066Sahrens 		if (fd == -1)
1011ea8dc4b6Seschrock 			fatal(1, "can't open %s", newpath);
1012ea8dc4b6Seschrock 		if (ftruncate(fd, newsize) != 0)
1013ea8dc4b6Seschrock 			fatal(1, "can't ftruncate %s", newpath);
1014fa9e4066Sahrens 		(void) close(fd);
1015fa9e4066Sahrens 	}
1016fa9e4066Sahrens 
1017ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
1018fa9e4066Sahrens 
1019fa9e4066Sahrens 	/*
1020ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
1021fa9e4066Sahrens 	 */
1022fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
1023fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
1024ea8dc4b6Seschrock 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, newpath) == 0);
1025ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1028fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1029fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN,
1030fa9e4066Sahrens 	    &file, 1) == 0);
1031fa9e4066Sahrens 
1032ea8dc4b6Seschrock 	error = spa_vdev_attach(spa, oldvd->vdev_guid, root, replacing);
1033fa9e4066Sahrens 
1034fa9e4066Sahrens 	nvlist_free(file);
1035fa9e4066Sahrens 	nvlist_free(root);
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	/*
1038fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
1039fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
1040fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
1041fa9e4066Sahrens 	 */
1042fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
1043fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
1044fa9e4066Sahrens 		expected_error = error;
1045fa9e4066Sahrens 
1046f0aa80d4Sbonwick 	/*
1047f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
1048f0aa80d4Sbonwick 	 */
1049f0aa80d4Sbonwick 	if (error == EOVERFLOW)
1050f0aa80d4Sbonwick 		expected_error = error;
1051f0aa80d4Sbonwick 
1052fa9e4066Sahrens 	if (error != expected_error) {
1053fa9e4066Sahrens 		fatal(0, "attach (%s, %s, %d) returned %d, expected %d",
1054ea8dc4b6Seschrock 		    oldpath, newpath, replacing, error, expected_error);
1055fa9e4066Sahrens 	}
1056fa9e4066Sahrens 
1057fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1058fa9e4066Sahrens }
1059fa9e4066Sahrens 
1060fa9e4066Sahrens /*
1061fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
1062fa9e4066Sahrens  */
1063fa9e4066Sahrens /* ARGSUSED */
1064fa9e4066Sahrens void
1065fa9e4066Sahrens ztest_vdev_LUN_growth(ztest_args_t *za)
1066fa9e4066Sahrens {
1067e05725b1Sbonwick 	spa_t *spa = za->za_spa;
1068fa9e4066Sahrens 	char dev_name[MAXPATHLEN];
1069fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
1070fa9e4066Sahrens 	uint64_t vdev;
1071fa9e4066Sahrens 	size_t fsize;
1072fa9e4066Sahrens 	int fd;
1073fa9e4066Sahrens 
1074fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	/*
1077fa9e4066Sahrens 	 * Pick a random leaf vdev.
1078fa9e4066Sahrens 	 */
1079ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
1080fa9e4066Sahrens 	vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves);
1081ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
1084fa9e4066Sahrens 
1085fa9e4066Sahrens 	if ((fd = open(dev_name, O_RDWR)) != -1) {
1086fa9e4066Sahrens 		/*
1087fa9e4066Sahrens 		 * Determine the size.
1088fa9e4066Sahrens 		 */
1089fa9e4066Sahrens 		fsize = lseek(fd, 0, SEEK_END);
1090fa9e4066Sahrens 
1091fa9e4066Sahrens 		/*
1092fa9e4066Sahrens 		 * If it's less than 2x the original size, grow by around 3%.
1093fa9e4066Sahrens 		 */
1094fa9e4066Sahrens 		if (fsize < 2 * zopt_vdev_size) {
1095fa9e4066Sahrens 			size_t newsize = fsize + ztest_random(fsize / 32);
1096fa9e4066Sahrens 			(void) ftruncate(fd, newsize);
1097fa9e4066Sahrens 			if (zopt_verbose >= 6) {
1098fa9e4066Sahrens 				(void) printf("%s grew from %lu to %lu bytes\n",
1099fa9e4066Sahrens 				    dev_name, (ulong_t)fsize, (ulong_t)newsize);
1100fa9e4066Sahrens 			}
1101fa9e4066Sahrens 		}
1102fa9e4066Sahrens 		(void) close(fd);
1103fa9e4066Sahrens 	}
1104fa9e4066Sahrens 
1105fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1106fa9e4066Sahrens }
1107fa9e4066Sahrens 
1108fa9e4066Sahrens /* ARGSUSED */
1109fa9e4066Sahrens static void
1110ecd6cf80Smarks ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1111fa9e4066Sahrens {
1112fa9e4066Sahrens 	/*
1113fa9e4066Sahrens 	 * Create the directory object.
1114fa9e4066Sahrens 	 */
1115fa9e4066Sahrens 	VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1116fa9e4066Sahrens 	    DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1117e05725b1Sbonwick 	    DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1118fa9e4066Sahrens 
1119fa9e4066Sahrens 	VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1120fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1121fa9e4066Sahrens 
1122fa9e4066Sahrens 	VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1123fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1124fa9e4066Sahrens }
1125fa9e4066Sahrens 
11261d452cf5Sahrens static int
1127fa9e4066Sahrens ztest_destroy_cb(char *name, void *arg)
1128fa9e4066Sahrens {
1129e05725b1Sbonwick 	ztest_args_t *za = arg;
1130fa9e4066Sahrens 	objset_t *os;
1131e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
1132fa9e4066Sahrens 	int error;
1133fa9e4066Sahrens 
1134fa9e4066Sahrens 	/*
1135fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
1136fa9e4066Sahrens 	 */
1137fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_OTHER,
1138fa9e4066Sahrens 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1139fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1140e05725b1Sbonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1141e1930233Sbonwick 	if (error != ENOENT) {
1142e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
1143fa9e4066Sahrens 		ASSERT3U(error, ==, 0);
1144e05725b1Sbonwick 		ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1145e05725b1Sbonwick 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1146e1930233Sbonwick 	}
1147fa9e4066Sahrens 	dmu_objset_close(os);
1148fa9e4066Sahrens 
1149fa9e4066Sahrens 	/*
1150fa9e4066Sahrens 	 * Destroy the dataset.
1151fa9e4066Sahrens 	 */
1152fa9e4066Sahrens 	error = dmu_objset_destroy(name);
1153fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
11541d452cf5Sahrens 	return (0);
1155fa9e4066Sahrens }
1156fa9e4066Sahrens 
1157fa9e4066Sahrens /*
1158fa9e4066Sahrens  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1159fa9e4066Sahrens  */
1160fa9e4066Sahrens static uint64_t
1161fa9e4066Sahrens ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1162fa9e4066Sahrens {
1163fa9e4066Sahrens 	itx_t *itx;
1164fa9e4066Sahrens 	lr_create_t *lr;
1165fa9e4066Sahrens 	size_t namesize;
1166fa9e4066Sahrens 	char name[24];
1167fa9e4066Sahrens 
1168fa9e4066Sahrens 	(void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1169fa9e4066Sahrens 	namesize = strlen(name) + 1;
1170fa9e4066Sahrens 
1171fa9e4066Sahrens 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1172fa9e4066Sahrens 	    ztest_random(ZIL_MAX_BLKSZ));
1173fa9e4066Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
1174fa9e4066Sahrens 	bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1175fa9e4066Sahrens 	lr->lr_doid = object;
1176fa9e4066Sahrens 	lr->lr_foid = 0;
1177fa9e4066Sahrens 	lr->lr_mode = mode;
1178fa9e4066Sahrens 	lr->lr_uid = 0;
1179fa9e4066Sahrens 	lr->lr_gid = 0;
1180fa9e4066Sahrens 	lr->lr_gen = dmu_tx_get_txg(tx);
1181fa9e4066Sahrens 	lr->lr_crtime[0] = time(NULL);
1182fa9e4066Sahrens 	lr->lr_crtime[1] = 0;
1183fa9e4066Sahrens 	lr->lr_rdev = 0;
1184fa9e4066Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
1185fa9e4066Sahrens 
1186fa9e4066Sahrens 	return (zil_itx_assign(zilog, itx, tx));
1187fa9e4066Sahrens }
1188fa9e4066Sahrens 
1189fa9e4066Sahrens void
1190fa9e4066Sahrens ztest_dmu_objset_create_destroy(ztest_args_t *za)
1191fa9e4066Sahrens {
1192fa9e4066Sahrens 	int error;
1193fa9e4066Sahrens 	objset_t *os;
1194fa9e4066Sahrens 	char name[100];
1195fa9e4066Sahrens 	int mode, basemode, expected_error;
1196fa9e4066Sahrens 	zilog_t *zilog;
1197fa9e4066Sahrens 	uint64_t seq;
1198fa9e4066Sahrens 	uint64_t objects;
1199fa9e4066Sahrens 	ztest_replay_t zr;
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1202fa9e4066Sahrens 	(void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1203fa9e4066Sahrens 	    (u_longlong_t)za->za_instance);
1204fa9e4066Sahrens 
1205fa9e4066Sahrens 	basemode = DS_MODE_LEVEL(za->za_instance);
1206fa9e4066Sahrens 	if (basemode == DS_MODE_NONE)
1207fa9e4066Sahrens 		basemode++;
1208fa9e4066Sahrens 
1209fa9e4066Sahrens 	/*
1210fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
1211fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
1212fa9e4066Sahrens 	 * (invoked from ztest_destroy_cb() below) should just throw it away.
1213fa9e4066Sahrens 	 */
1214fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
1215fa9e4066Sahrens 	    dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_PRIMARY, &os) == 0) {
1216fa9e4066Sahrens 		zr.zr_os = os;
1217893a6d32Sahrens 		zil_replay(os, &zr, &zr.zr_assign, ztest_replay_vector);
1218fa9e4066Sahrens 		dmu_objset_close(os);
1219fa9e4066Sahrens 	}
1220fa9e4066Sahrens 
1221fa9e4066Sahrens 	/*
1222fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
1223fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
1224fa9e4066Sahrens 	 * and all of its snapshots.
1225fa9e4066Sahrens 	 */
1226e05725b1Sbonwick 	(void) dmu_objset_find(name, ztest_destroy_cb, za,
12270b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1228fa9e4066Sahrens 
1229fa9e4066Sahrens 	/*
1230fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
1231fa9e4066Sahrens 	 */
1232fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1233fa9e4066Sahrens 	if (error != ENOENT)
1234fa9e4066Sahrens 		fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1235fa9e4066Sahrens 		    name, os);
1236fa9e4066Sahrens 
1237fa9e4066Sahrens 	/*
1238fa9e4066Sahrens 	 * Verify that we can create a new dataset.
1239fa9e4066Sahrens 	 */
1240fa9e4066Sahrens 	error = dmu_objset_create(name, DMU_OST_OTHER, NULL, ztest_create_cb,
1241fa9e4066Sahrens 	    NULL);
1242fa9e4066Sahrens 	if (error) {
1243fa9e4066Sahrens 		if (error == ENOSPC) {
1244fa9e4066Sahrens 			ztest_record_enospc("dmu_objset_create");
1245fa9e4066Sahrens 			(void) rw_unlock(&ztest_shared->zs_name_lock);
1246fa9e4066Sahrens 			return;
1247fa9e4066Sahrens 		}
1248fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
1249fa9e4066Sahrens 	}
1250fa9e4066Sahrens 
1251fa9e4066Sahrens 	error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1252fa9e4066Sahrens 	if (error) {
1253fa9e4066Sahrens 		fatal(0, "dmu_objset_open(%s) = %d", name, error);
1254fa9e4066Sahrens 	}
1255fa9e4066Sahrens 
1256fa9e4066Sahrens 	/*
1257fa9e4066Sahrens 	 * Open the intent log for it.
1258fa9e4066Sahrens 	 */
1259fa9e4066Sahrens 	zilog = zil_open(os, NULL);
1260fa9e4066Sahrens 
1261fa9e4066Sahrens 	/*
1262fa9e4066Sahrens 	 * Put a random number of objects in there.
1263fa9e4066Sahrens 	 */
1264d80c45e0Sbonwick 	objects = ztest_random(20);
1265fa9e4066Sahrens 	seq = 0;
1266fa9e4066Sahrens 	while (objects-- != 0) {
1267fa9e4066Sahrens 		uint64_t object;
1268fa9e4066Sahrens 		dmu_tx_t *tx = dmu_tx_create(os);
1269fa9e4066Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1270fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1271fa9e4066Sahrens 		if (error) {
1272fa9e4066Sahrens 			dmu_tx_abort(tx);
1273fa9e4066Sahrens 		} else {
1274fa9e4066Sahrens 			object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1275fa9e4066Sahrens 			    DMU_OT_NONE, 0, tx);
1276fa9e4066Sahrens 			ztest_set_random_blocksize(os, object, tx);
1277fa9e4066Sahrens 			seq = ztest_log_create(zilog, tx, object,
1278fa9e4066Sahrens 			    DMU_OT_UINT64_OTHER);
1279fa9e4066Sahrens 			dmu_write(os, object, 0, sizeof (name), name, tx);
1280fa9e4066Sahrens 			dmu_tx_commit(tx);
1281fa9e4066Sahrens 		}
1282fa9e4066Sahrens 		if (ztest_random(5) == 0) {
1283b19a79ecSperrin 			zil_commit(zilog, seq, object);
1284fa9e4066Sahrens 		}
1285d80c45e0Sbonwick 		if (ztest_random(100) == 0) {
1286fa9e4066Sahrens 			error = zil_suspend(zilog);
1287fa9e4066Sahrens 			if (error == 0) {
1288fa9e4066Sahrens 				zil_resume(zilog);
1289fa9e4066Sahrens 			}
1290fa9e4066Sahrens 		}
1291fa9e4066Sahrens 	}
1292fa9e4066Sahrens 
1293fa9e4066Sahrens 	/*
1294fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
1295fa9e4066Sahrens 	 */
1296fa9e4066Sahrens 	error = dmu_objset_create(name, DMU_OST_OTHER, NULL, NULL, NULL);
1297fa9e4066Sahrens 	if (error != EEXIST)
1298fa9e4066Sahrens 		fatal(0, "created existing dataset, error = %d", error);
1299fa9e4066Sahrens 
1300fa9e4066Sahrens 	/*
1301fa9e4066Sahrens 	 * Verify that multiple dataset opens are allowed, but only when
1302fa9e4066Sahrens 	 * the new access mode is compatible with the base mode.
1303fa9e4066Sahrens 	 * We use a mixture of typed and typeless opens, and when the
1304fa9e4066Sahrens 	 * open succeeds, verify that the discovered type is correct.
1305fa9e4066Sahrens 	 */
1306fa9e4066Sahrens 	for (mode = DS_MODE_STANDARD; mode < DS_MODE_LEVELS; mode++) {
1307fa9e4066Sahrens 		objset_t *os2;
1308fa9e4066Sahrens 		error = dmu_objset_open(name, DMU_OST_OTHER, mode, &os2);
1309fa9e4066Sahrens 		expected_error = (basemode + mode < DS_MODE_LEVELS) ? 0 : EBUSY;
1310fa9e4066Sahrens 		if (error != expected_error)
1311fa9e4066Sahrens 			fatal(0, "dmu_objset_open('%s') = %d, expected %d",
1312fa9e4066Sahrens 			    name, error, expected_error);
1313fa9e4066Sahrens 		if (error == 0)
1314fa9e4066Sahrens 			dmu_objset_close(os2);
1315fa9e4066Sahrens 	}
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	zil_close(zilog);
1318fa9e4066Sahrens 	dmu_objset_close(os);
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	error = dmu_objset_destroy(name);
1321fa9e4066Sahrens 	if (error)
1322fa9e4066Sahrens 		fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1323fa9e4066Sahrens 
1324fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1325fa9e4066Sahrens }
1326fa9e4066Sahrens 
1327fa9e4066Sahrens /*
1328fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1329fa9e4066Sahrens  */
1330fa9e4066Sahrens void
1331fa9e4066Sahrens ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1332fa9e4066Sahrens {
1333fa9e4066Sahrens 	int error;
1334fa9e4066Sahrens 	objset_t *os = za->za_os;
1335fa9e4066Sahrens 	char snapname[100];
1336fa9e4066Sahrens 	char osname[MAXNAMELEN];
1337fa9e4066Sahrens 
1338fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1339fa9e4066Sahrens 	dmu_objset_name(os, osname);
1340fa9e4066Sahrens 	(void) snprintf(snapname, 100, "%s@%llu", osname,
1341fa9e4066Sahrens 	    (u_longlong_t)za->za_instance);
1342fa9e4066Sahrens 
1343fa9e4066Sahrens 	error = dmu_objset_destroy(snapname);
1344fa9e4066Sahrens 	if (error != 0 && error != ENOENT)
1345fa9e4066Sahrens 		fatal(0, "dmu_objset_destroy() = %d", error);
13461d452cf5Sahrens 	error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE);
1347fa9e4066Sahrens 	if (error == ENOSPC)
1348fa9e4066Sahrens 		ztest_record_enospc("dmu_take_snapshot");
1349fa9e4066Sahrens 	else if (error != 0 && error != EEXIST)
1350fa9e4066Sahrens 		fatal(0, "dmu_take_snapshot() = %d", error);
1351fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1352fa9e4066Sahrens }
1353fa9e4066Sahrens 
1354fa9e4066Sahrens #define	ZTEST_TRAVERSE_BLOCKS	1000
1355fa9e4066Sahrens 
1356fa9e4066Sahrens static int
1357fa9e4066Sahrens ztest_blk_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1358fa9e4066Sahrens {
1359fa9e4066Sahrens 	ztest_args_t *za = arg;
1360fa9e4066Sahrens 	zbookmark_t *zb = &bc->bc_bookmark;
1361fa9e4066Sahrens 	blkptr_t *bp = &bc->bc_blkptr;
1362fa9e4066Sahrens 	dnode_phys_t *dnp = bc->bc_dnode;
1363fa9e4066Sahrens 	traverse_handle_t *th = za->za_th;
1364fa9e4066Sahrens 	uint64_t size = BP_GET_LSIZE(bp);
1365fa9e4066Sahrens 
1366ea8dc4b6Seschrock 	/*
1367ea8dc4b6Seschrock 	 * Level -1 indicates the objset_phys_t or something in its intent log.
1368ea8dc4b6Seschrock 	 */
1369ea8dc4b6Seschrock 	if (zb->zb_level == -1) {
1370ea8dc4b6Seschrock 		if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
1371ea8dc4b6Seschrock 			ASSERT3U(zb->zb_object, ==, 0);
1372ea8dc4b6Seschrock 			ASSERT3U(zb->zb_blkid, ==, 0);
1373ea8dc4b6Seschrock 			ASSERT3U(size, ==, sizeof (objset_phys_t));
1374ea8dc4b6Seschrock 			za->za_zil_seq = 0;
1375ea8dc4b6Seschrock 		} else if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) {
1376ea8dc4b6Seschrock 			ASSERT3U(zb->zb_object, ==, 0);
1377ea8dc4b6Seschrock 			ASSERT3U(zb->zb_blkid, >, za->za_zil_seq);
1378ea8dc4b6Seschrock 			za->za_zil_seq = zb->zb_blkid;
1379ea8dc4b6Seschrock 		} else {
1380ea8dc4b6Seschrock 			ASSERT3U(zb->zb_object, !=, 0);	/* lr_write_t */
1381ea8dc4b6Seschrock 		}
1382ea8dc4b6Seschrock 
1383ea8dc4b6Seschrock 		return (0);
1384ea8dc4b6Seschrock 	}
1385ea8dc4b6Seschrock 
1386fa9e4066Sahrens 	ASSERT(dnp != NULL);
1387fa9e4066Sahrens 
1388fa9e4066Sahrens 	if (bc->bc_errno)
1389fa9e4066Sahrens 		return (ERESTART);
1390fa9e4066Sahrens 
1391fa9e4066Sahrens 	/*
1392fa9e4066Sahrens 	 * Once in a while, abort the traverse.   We only do this to odd
1393fa9e4066Sahrens 	 * instance numbers to ensure that even ones can run to completion.
1394fa9e4066Sahrens 	 */
1395fa9e4066Sahrens 	if ((za->za_instance & 1) && ztest_random(10000) == 0)
1396fa9e4066Sahrens 		return (EINTR);
1397fa9e4066Sahrens 
1398fa9e4066Sahrens 	if (bp->blk_birth == 0) {
1399fa9e4066Sahrens 		ASSERT(th->th_advance & ADVANCE_HOLES);
1400fa9e4066Sahrens 		return (0);
1401fa9e4066Sahrens 	}
1402fa9e4066Sahrens 
1403fa9e4066Sahrens 	if (zb->zb_level == 0 && !(th->th_advance & ADVANCE_DATA) &&
1404fa9e4066Sahrens 	    bc == &th->th_cache[ZB_DN_CACHE][0]) {
1405fa9e4066Sahrens 		ASSERT(bc->bc_data == NULL);
1406fa9e4066Sahrens 		return (0);
1407fa9e4066Sahrens 	}
1408fa9e4066Sahrens 
1409fa9e4066Sahrens 	ASSERT(bc->bc_data != NULL);
1410fa9e4066Sahrens 
1411fa9e4066Sahrens 	/*
1412fa9e4066Sahrens 	 * This is an expensive question, so don't ask it too often.
1413fa9e4066Sahrens 	 */
1414fa9e4066Sahrens 	if (((za->za_random ^ th->th_callbacks) & 0xff) == 0) {
1415fa9e4066Sahrens 		void *xbuf = umem_alloc(size, UMEM_NOFAIL);
1416fa9e4066Sahrens 		if (arc_tryread(spa, bp, xbuf) == 0) {
1417fa9e4066Sahrens 			ASSERT(bcmp(bc->bc_data, xbuf, size) == 0);
1418fa9e4066Sahrens 		}
1419fa9e4066Sahrens 		umem_free(xbuf, size);
1420fa9e4066Sahrens 	}
1421fa9e4066Sahrens 
1422fa9e4066Sahrens 	if (zb->zb_level > 0) {
1423fa9e4066Sahrens 		ASSERT3U(size, ==, 1ULL << dnp->dn_indblkshift);
1424fa9e4066Sahrens 		return (0);
1425fa9e4066Sahrens 	}
1426fa9e4066Sahrens 
1427fa9e4066Sahrens 	ASSERT(zb->zb_level == 0);
1428fa9e4066Sahrens 	ASSERT3U(size, ==, dnp->dn_datablkszsec << DEV_BSHIFT);
1429fa9e4066Sahrens 
1430fa9e4066Sahrens 	return (0);
1431fa9e4066Sahrens }
1432fa9e4066Sahrens 
1433fa9e4066Sahrens /*
1434fa9e4066Sahrens  * Verify that live pool traversal works.
1435fa9e4066Sahrens  */
1436fa9e4066Sahrens void
1437fa9e4066Sahrens ztest_traverse(ztest_args_t *za)
1438fa9e4066Sahrens {
1439e05725b1Sbonwick 	spa_t *spa = za->za_spa;
1440fa9e4066Sahrens 	traverse_handle_t *th = za->za_th;
1441fa9e4066Sahrens 	int rc, advance;
1442fa9e4066Sahrens 	uint64_t cbstart, cblimit;
1443fa9e4066Sahrens 
1444fa9e4066Sahrens 	if (th == NULL) {
1445fa9e4066Sahrens 		advance = 0;
1446fa9e4066Sahrens 
1447fa9e4066Sahrens 		if (ztest_random(2) == 0)
1448fa9e4066Sahrens 			advance |= ADVANCE_PRE;
1449fa9e4066Sahrens 
1450fa9e4066Sahrens 		if (ztest_random(2) == 0)
1451fa9e4066Sahrens 			advance |= ADVANCE_PRUNE;
1452fa9e4066Sahrens 
1453fa9e4066Sahrens 		if (ztest_random(2) == 0)
1454fa9e4066Sahrens 			advance |= ADVANCE_DATA;
1455fa9e4066Sahrens 
1456fa9e4066Sahrens 		if (ztest_random(2) == 0)
1457fa9e4066Sahrens 			advance |= ADVANCE_HOLES;
1458fa9e4066Sahrens 
1459ea8dc4b6Seschrock 		if (ztest_random(2) == 0)
1460ea8dc4b6Seschrock 			advance |= ADVANCE_ZIL;
1461ea8dc4b6Seschrock 
1462fa9e4066Sahrens 		th = za->za_th = traverse_init(spa, ztest_blk_cb, za, advance,
1463fa9e4066Sahrens 		    ZIO_FLAG_CANFAIL);
1464fa9e4066Sahrens 
1465fa9e4066Sahrens 		traverse_add_pool(th, 0, -1ULL);
1466fa9e4066Sahrens 	}
1467fa9e4066Sahrens 
1468fa9e4066Sahrens 	advance = th->th_advance;
1469fa9e4066Sahrens 	cbstart = th->th_callbacks;
1470fa9e4066Sahrens 	cblimit = cbstart + ((advance & ADVANCE_DATA) ? 100 : 1000);
1471fa9e4066Sahrens 
1472fa9e4066Sahrens 	while ((rc = traverse_more(th)) == EAGAIN && th->th_callbacks < cblimit)
1473fa9e4066Sahrens 		continue;
1474fa9e4066Sahrens 
1475fa9e4066Sahrens 	if (zopt_verbose >= 5)
1476fa9e4066Sahrens 		(void) printf("traverse %s%s%s%s %llu blocks to "
1477ea8dc4b6Seschrock 		    "<%llu, %llu, %lld, %llx>%s\n",
1478fa9e4066Sahrens 		    (advance & ADVANCE_PRE) ? "pre" : "post",
1479fa9e4066Sahrens 		    (advance & ADVANCE_PRUNE) ? "|prune" : "",
1480fa9e4066Sahrens 		    (advance & ADVANCE_DATA) ? "|data" : "",
1481fa9e4066Sahrens 		    (advance & ADVANCE_HOLES) ? "|holes" : "",
1482fa9e4066Sahrens 		    (u_longlong_t)(th->th_callbacks - cbstart),
1483fa9e4066Sahrens 		    (u_longlong_t)th->th_lastcb.zb_objset,
1484fa9e4066Sahrens 		    (u_longlong_t)th->th_lastcb.zb_object,
1485ea8dc4b6Seschrock 		    (u_longlong_t)th->th_lastcb.zb_level,
1486fa9e4066Sahrens 		    (u_longlong_t)th->th_lastcb.zb_blkid,
1487fa9e4066Sahrens 		    rc == 0 ? " [done]" :
1488fa9e4066Sahrens 		    rc == EINTR ? " [aborted]" :
1489fa9e4066Sahrens 		    rc == EAGAIN ? "" :
1490fa9e4066Sahrens 		    strerror(rc));
1491fa9e4066Sahrens 
1492fa9e4066Sahrens 	if (rc != EAGAIN) {
1493fa9e4066Sahrens 		if (rc != 0 && rc != EINTR)
1494fa9e4066Sahrens 			fatal(0, "traverse_more(%p) = %d", th, rc);
1495fa9e4066Sahrens 		traverse_fini(th);
1496fa9e4066Sahrens 		za->za_th = NULL;
1497fa9e4066Sahrens 	}
1498fa9e4066Sahrens }
1499fa9e4066Sahrens 
1500fa9e4066Sahrens /*
1501fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
1502fa9e4066Sahrens  */
1503fa9e4066Sahrens void
1504fa9e4066Sahrens ztest_dmu_object_alloc_free(ztest_args_t *za)
1505fa9e4066Sahrens {
1506fa9e4066Sahrens 	objset_t *os = za->za_os;
1507fa9e4066Sahrens 	dmu_buf_t *db;
1508fa9e4066Sahrens 	dmu_tx_t *tx;
1509fa9e4066Sahrens 	uint64_t batchobj, object, batchsize, endoff, temp;
1510fa9e4066Sahrens 	int b, c, error, bonuslen;
1511e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
1512fa9e4066Sahrens 	char osname[MAXNAMELEN];
1513fa9e4066Sahrens 
1514fa9e4066Sahrens 	dmu_objset_name(os, osname);
1515fa9e4066Sahrens 
1516fa9e4066Sahrens 	endoff = -8ULL;
1517fa9e4066Sahrens 	batchsize = 2;
1518fa9e4066Sahrens 
1519fa9e4066Sahrens 	/*
1520fa9e4066Sahrens 	 * Create a batch object if necessary, and record it in the directory.
1521fa9e4066Sahrens 	 */
1522ea8dc4b6Seschrock 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1523ea8dc4b6Seschrock 	    sizeof (uint64_t), &batchobj));
1524fa9e4066Sahrens 	if (batchobj == 0) {
1525fa9e4066Sahrens 		tx = dmu_tx_create(os);
1526fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1527fa9e4066Sahrens 		    sizeof (uint64_t));
1528fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1529fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1530fa9e4066Sahrens 		if (error) {
1531fa9e4066Sahrens 			ztest_record_enospc("create a batch object");
1532fa9e4066Sahrens 			dmu_tx_abort(tx);
1533fa9e4066Sahrens 			return;
1534fa9e4066Sahrens 		}
1535fa9e4066Sahrens 		batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1536fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1537fa9e4066Sahrens 		ztest_set_random_blocksize(os, batchobj, tx);
1538fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1539fa9e4066Sahrens 		    sizeof (uint64_t), &batchobj, tx);
1540fa9e4066Sahrens 		dmu_tx_commit(tx);
1541fa9e4066Sahrens 	}
1542fa9e4066Sahrens 
1543fa9e4066Sahrens 	/*
1544fa9e4066Sahrens 	 * Destroy the previous batch of objects.
1545fa9e4066Sahrens 	 */
1546fa9e4066Sahrens 	for (b = 0; b < batchsize; b++) {
1547ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, batchobj, b * sizeof (uint64_t),
1548ea8dc4b6Seschrock 		    sizeof (uint64_t), &object));
1549fa9e4066Sahrens 		if (object == 0)
1550fa9e4066Sahrens 			continue;
1551fa9e4066Sahrens 		/*
1552fa9e4066Sahrens 		 * Read and validate contents.
1553fa9e4066Sahrens 		 * We expect the nth byte of the bonus buffer to be n.
1554fa9e4066Sahrens 		 */
1555ea8dc4b6Seschrock 		VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1556e05725b1Sbonwick 		za->za_dbuf = db;
1557fa9e4066Sahrens 
1558e05725b1Sbonwick 		dmu_object_info_from_db(db, doi);
1559e05725b1Sbonwick 		ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1560e05725b1Sbonwick 		ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1561e05725b1Sbonwick 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1562fa9e4066Sahrens 
1563e05725b1Sbonwick 		bonuslen = doi->doi_bonus_size;
1564fa9e4066Sahrens 
1565fa9e4066Sahrens 		for (c = 0; c < bonuslen; c++) {
1566fa9e4066Sahrens 			if (((uint8_t *)db->db_data)[c] !=
1567fa9e4066Sahrens 			    (uint8_t)(c + bonuslen)) {
1568fa9e4066Sahrens 				fatal(0,
1569fa9e4066Sahrens 				    "bad bonus: %s, obj %llu, off %d: %u != %u",
1570fa9e4066Sahrens 				    osname, object, c,
1571fa9e4066Sahrens 				    ((uint8_t *)db->db_data)[c],
1572fa9e4066Sahrens 				    (uint8_t)(c + bonuslen));
1573fa9e4066Sahrens 			}
1574fa9e4066Sahrens 		}
1575fa9e4066Sahrens 
1576ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1577e05725b1Sbonwick 		za->za_dbuf = NULL;
1578fa9e4066Sahrens 
1579fa9e4066Sahrens 		/*
1580fa9e4066Sahrens 		 * We expect the word at endoff to be our object number.
1581fa9e4066Sahrens 		 */
1582ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, object, endoff,
1583ea8dc4b6Seschrock 		    sizeof (uint64_t), &temp));
1584fa9e4066Sahrens 
1585fa9e4066Sahrens 		if (temp != object) {
1586fa9e4066Sahrens 			fatal(0, "bad data in %s, got %llu, expected %llu",
1587fa9e4066Sahrens 			    osname, temp, object);
1588fa9e4066Sahrens 		}
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 		/*
1591fa9e4066Sahrens 		 * Destroy old object and clear batch entry.
1592fa9e4066Sahrens 		 */
1593fa9e4066Sahrens 		tx = dmu_tx_create(os);
1594fa9e4066Sahrens 		dmu_tx_hold_write(tx, batchobj,
1595fa9e4066Sahrens 		    b * sizeof (uint64_t), sizeof (uint64_t));
1596fa9e4066Sahrens 		dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1597fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1598fa9e4066Sahrens 		if (error) {
1599fa9e4066Sahrens 			ztest_record_enospc("free object");
1600fa9e4066Sahrens 			dmu_tx_abort(tx);
1601fa9e4066Sahrens 			return;
1602fa9e4066Sahrens 		}
1603fa9e4066Sahrens 		error = dmu_object_free(os, object, tx);
1604fa9e4066Sahrens 		if (error) {
1605fa9e4066Sahrens 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1606fa9e4066Sahrens 			    osname, object, error);
1607fa9e4066Sahrens 		}
1608fa9e4066Sahrens 		object = 0;
1609fa9e4066Sahrens 
1610fa9e4066Sahrens 		dmu_object_set_checksum(os, batchobj,
1611fa9e4066Sahrens 		    ztest_random_checksum(), tx);
1612fa9e4066Sahrens 		dmu_object_set_compress(os, batchobj,
1613fa9e4066Sahrens 		    ztest_random_compress(), tx);
1614fa9e4066Sahrens 
1615fa9e4066Sahrens 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1616fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
1617fa9e4066Sahrens 
1618fa9e4066Sahrens 		dmu_tx_commit(tx);
1619fa9e4066Sahrens 	}
1620fa9e4066Sahrens 
1621fa9e4066Sahrens 	/*
1622fa9e4066Sahrens 	 * Before creating the new batch of objects, generate a bunch of churn.
1623fa9e4066Sahrens 	 */
1624fa9e4066Sahrens 	for (b = ztest_random(100); b > 0; b--) {
1625fa9e4066Sahrens 		tx = dmu_tx_create(os);
1626fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1627fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1628fa9e4066Sahrens 		if (error) {
1629fa9e4066Sahrens 			ztest_record_enospc("churn objects");
1630fa9e4066Sahrens 			dmu_tx_abort(tx);
1631fa9e4066Sahrens 			return;
1632fa9e4066Sahrens 		}
1633fa9e4066Sahrens 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1634fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1635fa9e4066Sahrens 		ztest_set_random_blocksize(os, object, tx);
1636fa9e4066Sahrens 		error = dmu_object_free(os, object, tx);
1637fa9e4066Sahrens 		if (error) {
1638fa9e4066Sahrens 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1639fa9e4066Sahrens 			    osname, object, error);
1640fa9e4066Sahrens 		}
1641fa9e4066Sahrens 		dmu_tx_commit(tx);
1642fa9e4066Sahrens 	}
1643fa9e4066Sahrens 
1644fa9e4066Sahrens 	/*
1645fa9e4066Sahrens 	 * Create a new batch of objects with randomly chosen
1646fa9e4066Sahrens 	 * blocksizes and record them in the batch directory.
1647fa9e4066Sahrens 	 */
1648fa9e4066Sahrens 	for (b = 0; b < batchsize; b++) {
1649fa9e4066Sahrens 		uint32_t va_blksize;
1650fa9e4066Sahrens 		u_longlong_t va_nblocks;
1651fa9e4066Sahrens 
1652fa9e4066Sahrens 		tx = dmu_tx_create(os);
1653fa9e4066Sahrens 		dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1654fa9e4066Sahrens 		    sizeof (uint64_t));
1655fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1656fa9e4066Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1657fa9e4066Sahrens 		    sizeof (uint64_t));
1658fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1659fa9e4066Sahrens 		if (error) {
1660fa9e4066Sahrens 			ztest_record_enospc("create batchobj");
1661fa9e4066Sahrens 			dmu_tx_abort(tx);
1662fa9e4066Sahrens 			return;
1663fa9e4066Sahrens 		}
1664fa9e4066Sahrens 		bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1665fa9e4066Sahrens 
1666fa9e4066Sahrens 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1667fa9e4066Sahrens 		    DMU_OT_PLAIN_OTHER, bonuslen, tx);
1668fa9e4066Sahrens 
1669fa9e4066Sahrens 		ztest_set_random_blocksize(os, object, tx);
1670fa9e4066Sahrens 
1671fa9e4066Sahrens 		dmu_object_set_checksum(os, object,
1672fa9e4066Sahrens 		    ztest_random_checksum(), tx);
1673fa9e4066Sahrens 		dmu_object_set_compress(os, object,
1674fa9e4066Sahrens 		    ztest_random_compress(), tx);
1675fa9e4066Sahrens 
1676fa9e4066Sahrens 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1677fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
1678fa9e4066Sahrens 
1679fa9e4066Sahrens 		/*
1680fa9e4066Sahrens 		 * Write to both the bonus buffer and the regular data.
1681fa9e4066Sahrens 		 */
1682e05725b1Sbonwick 		VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1683e05725b1Sbonwick 		za->za_dbuf = db;
16841934e92fSmaybee 		ASSERT3U(bonuslen, <=, db->db_size);
1685fa9e4066Sahrens 
1686fa9e4066Sahrens 		dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1687fa9e4066Sahrens 		ASSERT3S(va_nblocks, >=, 0);
1688fa9e4066Sahrens 
1689fa9e4066Sahrens 		dmu_buf_will_dirty(db, tx);
1690fa9e4066Sahrens 
1691fa9e4066Sahrens 		/*
1692fa9e4066Sahrens 		 * See comments above regarding the contents of
1693fa9e4066Sahrens 		 * the bonus buffer and the word at endoff.
1694fa9e4066Sahrens 		 */
16951934e92fSmaybee 		for (c = 0; c < bonuslen; c++)
1696fa9e4066Sahrens 			((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1697fa9e4066Sahrens 
1698ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1699e05725b1Sbonwick 		za->za_dbuf = NULL;
1700fa9e4066Sahrens 
1701fa9e4066Sahrens 		/*
1702fa9e4066Sahrens 		 * Write to a large offset to increase indirection.
1703fa9e4066Sahrens 		 */
1704fa9e4066Sahrens 		dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1705fa9e4066Sahrens 
1706fa9e4066Sahrens 		dmu_tx_commit(tx);
1707fa9e4066Sahrens 	}
1708fa9e4066Sahrens }
1709fa9e4066Sahrens 
1710fa9e4066Sahrens /*
1711fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
1712fa9e4066Sahrens  */
1713fa9e4066Sahrens typedef struct bufwad {
1714fa9e4066Sahrens 	uint64_t	bw_index;
1715fa9e4066Sahrens 	uint64_t	bw_txg;
1716fa9e4066Sahrens 	uint64_t	bw_data;
1717fa9e4066Sahrens } bufwad_t;
1718fa9e4066Sahrens 
1719fa9e4066Sahrens typedef struct dmu_read_write_dir {
1720fa9e4066Sahrens 	uint64_t	dd_packobj;
1721fa9e4066Sahrens 	uint64_t	dd_bigobj;
1722fa9e4066Sahrens 	uint64_t	dd_chunk;
1723fa9e4066Sahrens } dmu_read_write_dir_t;
1724fa9e4066Sahrens 
1725fa9e4066Sahrens void
1726fa9e4066Sahrens ztest_dmu_read_write(ztest_args_t *za)
1727fa9e4066Sahrens {
1728fa9e4066Sahrens 	objset_t *os = za->za_os;
1729fa9e4066Sahrens 	dmu_read_write_dir_t dd;
1730fa9e4066Sahrens 	dmu_tx_t *tx;
1731fa9e4066Sahrens 	int i, freeit, error;
1732fa9e4066Sahrens 	uint64_t n, s, txg;
1733fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1734fa9e4066Sahrens 	uint64_t packoff, packsize, bigoff, bigsize;
1735fa9e4066Sahrens 	uint64_t regions = 997;
1736fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
1737fa9e4066Sahrens 	uint64_t width = 40;
1738fa9e4066Sahrens 	int free_percent = 5;
1739fa9e4066Sahrens 
1740fa9e4066Sahrens 	/*
1741fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
1742fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
1743fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
1744fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
1745fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
1746fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
1747fa9e4066Sahrens 	 *
1748fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
1749fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
1750fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
1751fa9e4066Sahrens 	 *
1752fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
1753fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
1754fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
1755fa9e4066Sahrens 	 *
1756fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
1757fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
1758fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
1759fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
1760fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
1761fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
1762fa9e4066Sahrens 	 */
1763fa9e4066Sahrens 
1764fa9e4066Sahrens 	/*
1765fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
1766fa9e4066Sahrens 	 */
1767ea8dc4b6Seschrock 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1768ea8dc4b6Seschrock 	    sizeof (dd), &dd));
1769fa9e4066Sahrens 	if (dd.dd_chunk == 0) {
1770fa9e4066Sahrens 		ASSERT(dd.dd_packobj == 0);
1771fa9e4066Sahrens 		ASSERT(dd.dd_bigobj == 0);
1772fa9e4066Sahrens 		tx = dmu_tx_create(os);
1773fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1774fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1775fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1776fa9e4066Sahrens 		if (error) {
1777fa9e4066Sahrens 			ztest_record_enospc("create r/w directory");
1778fa9e4066Sahrens 			dmu_tx_abort(tx);
1779fa9e4066Sahrens 			return;
1780fa9e4066Sahrens 		}
1781fa9e4066Sahrens 
1782fa9e4066Sahrens 		dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1783fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1784fa9e4066Sahrens 		dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1785fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1786fa9e4066Sahrens 		dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
1787fa9e4066Sahrens 
1788fa9e4066Sahrens 		ztest_set_random_blocksize(os, dd.dd_packobj, tx);
1789fa9e4066Sahrens 		ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
1790fa9e4066Sahrens 
1791fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
1792fa9e4066Sahrens 		    tx);
1793fa9e4066Sahrens 		dmu_tx_commit(tx);
1794fa9e4066Sahrens 	}
1795fa9e4066Sahrens 
1796fa9e4066Sahrens 	/*
1797fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
1798fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
1799fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
1800fa9e4066Sahrens 	 * handle this race correctly.
1801fa9e4066Sahrens 	 */
1802fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
1803fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
1804fa9e4066Sahrens 	dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
1805fa9e4066Sahrens 
1806fa9e4066Sahrens 	/*
1807fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
1808fa9e4066Sahrens 	 */
1809fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
1810fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
1811fa9e4066Sahrens 
1812fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
1813fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
1814fa9e4066Sahrens 
1815fa9e4066Sahrens 	bigoff = n * dd.dd_chunk;
1816fa9e4066Sahrens 	bigsize = s * dd.dd_chunk;
1817fa9e4066Sahrens 
1818fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
1819fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
1820fa9e4066Sahrens 
1821fa9e4066Sahrens 	/*
1822fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
1823fa9e4066Sahrens 	 * overwriting it.
1824fa9e4066Sahrens 	 */
1825fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
1826fa9e4066Sahrens 
1827fa9e4066Sahrens 	/*
1828fa9e4066Sahrens 	 * Read the current contents of our objects.
1829fa9e4066Sahrens 	 */
1830ea8dc4b6Seschrock 	error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf);
1831ea8dc4b6Seschrock 	ASSERT3U(error, ==, 0);
1832ea8dc4b6Seschrock 	error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf);
1833ea8dc4b6Seschrock 	ASSERT3U(error, ==, 0);
1834fa9e4066Sahrens 
1835fa9e4066Sahrens 	/*
1836fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
1837fa9e4066Sahrens 	 */
1838fa9e4066Sahrens 	tx = dmu_tx_create(os);
1839fa9e4066Sahrens 
1840fa9e4066Sahrens 	dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
1841fa9e4066Sahrens 
1842fa9e4066Sahrens 	if (freeit)
1843fa9e4066Sahrens 		dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
1844fa9e4066Sahrens 	else
1845fa9e4066Sahrens 		dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
1846fa9e4066Sahrens 
1847fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
1848fa9e4066Sahrens 
1849fa9e4066Sahrens 	if (error) {
1850fa9e4066Sahrens 		ztest_record_enospc("dmu r/w range");
1851fa9e4066Sahrens 		dmu_tx_abort(tx);
1852fa9e4066Sahrens 		umem_free(packbuf, packsize);
1853fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
1854fa9e4066Sahrens 		return;
1855fa9e4066Sahrens 	}
1856fa9e4066Sahrens 
1857fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
1858fa9e4066Sahrens 
1859fa9e4066Sahrens 	/*
1860fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
1861fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
1862fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
1863fa9e4066Sahrens 	 * with the new values we want to write out.
1864fa9e4066Sahrens 	 */
1865fa9e4066Sahrens 	for (i = 0; i < s; i++) {
1866fa9e4066Sahrens 		/* LINTED */
1867fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
1868fa9e4066Sahrens 		/* LINTED */
1869fa9e4066Sahrens 		bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
1870fa9e4066Sahrens 		/* LINTED */
1871fa9e4066Sahrens 		bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
1872fa9e4066Sahrens 
1873fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
1874fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
1875fa9e4066Sahrens 
1876fa9e4066Sahrens 		if (pack->bw_txg > txg)
1877fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
1878fa9e4066Sahrens 			    pack->bw_txg, txg);
1879fa9e4066Sahrens 
1880fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
1881fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
1882fa9e4066Sahrens 			    pack->bw_index, n, i);
1883fa9e4066Sahrens 
1884fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
1885fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
1886fa9e4066Sahrens 
1887fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
1888fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
1889fa9e4066Sahrens 
1890fa9e4066Sahrens 		if (freeit) {
1891fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
1892fa9e4066Sahrens 		} else {
1893fa9e4066Sahrens 			pack->bw_index = n + i;
1894fa9e4066Sahrens 			pack->bw_txg = txg;
1895fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
1896fa9e4066Sahrens 		}
1897fa9e4066Sahrens 		*bigH = *pack;
1898fa9e4066Sahrens 		*bigT = *pack;
1899fa9e4066Sahrens 	}
1900fa9e4066Sahrens 
1901fa9e4066Sahrens 	/*
1902fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
1903fa9e4066Sahrens 	 * Now write them out.
1904fa9e4066Sahrens 	 */
1905fa9e4066Sahrens 	dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
1906fa9e4066Sahrens 
1907fa9e4066Sahrens 	if (freeit) {
1908fa9e4066Sahrens 		if (zopt_verbose >= 6) {
1909fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
1910fa9e4066Sahrens 			    " txg %llx\n",
1911fa9e4066Sahrens 			    (u_longlong_t)bigoff,
1912fa9e4066Sahrens 			    (u_longlong_t)bigsize,
1913fa9e4066Sahrens 			    (u_longlong_t)txg);
1914fa9e4066Sahrens 		}
1915ea8dc4b6Seschrock 		VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
1916ea8dc4b6Seschrock 		    bigsize, tx));
1917fa9e4066Sahrens 	} else {
1918fa9e4066Sahrens 		if (zopt_verbose >= 6) {
1919fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
1920fa9e4066Sahrens 			    " txg %llx\n",
1921fa9e4066Sahrens 			    (u_longlong_t)bigoff,
1922fa9e4066Sahrens 			    (u_longlong_t)bigsize,
1923fa9e4066Sahrens 			    (u_longlong_t)txg);
1924fa9e4066Sahrens 		}
1925fa9e4066Sahrens 		dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
1926fa9e4066Sahrens 	}
1927fa9e4066Sahrens 
1928fa9e4066Sahrens 	dmu_tx_commit(tx);
1929fa9e4066Sahrens 
1930fa9e4066Sahrens 	/*
1931fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
1932fa9e4066Sahrens 	 */
1933fa9e4066Sahrens 	{
1934fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
1935fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
1936fa9e4066Sahrens 
1937ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
1938ea8dc4b6Seschrock 		    packsize, packcheck));
1939ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
1940ea8dc4b6Seschrock 		    bigsize, bigcheck));
1941fa9e4066Sahrens 
1942fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
1943fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
1944fa9e4066Sahrens 
1945fa9e4066Sahrens 		umem_free(packcheck, packsize);
1946fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
1947fa9e4066Sahrens 	}
1948fa9e4066Sahrens 
1949fa9e4066Sahrens 	umem_free(packbuf, packsize);
1950fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
1951fa9e4066Sahrens }
1952fa9e4066Sahrens 
1953fa9e4066Sahrens void
1954e05725b1Sbonwick ztest_dmu_check_future_leak(ztest_args_t *za)
19554d31c452Smaybee {
1956e05725b1Sbonwick 	objset_t *os = za->za_os;
19574d31c452Smaybee 	dmu_buf_t *db;
1958e05725b1Sbonwick 	ztest_block_tag_t *bt;
1959e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
19604d31c452Smaybee 
19614d31c452Smaybee 	/*
19624d31c452Smaybee 	 * Make sure that, if there is a write record in the bonus buffer
19634d31c452Smaybee 	 * of the ZTEST_DIROBJ, that the txg for this record is <= the
19644d31c452Smaybee 	 * last synced txg of the pool.
19654d31c452Smaybee 	 */
1966e05725b1Sbonwick 	VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
1967e05725b1Sbonwick 	za->za_dbuf = db;
1968e05725b1Sbonwick 	VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
1969e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
1970e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
1971e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
1972e05725b1Sbonwick 	bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
1973e05725b1Sbonwick 	if (bt->bt_objset != 0) {
1974e05725b1Sbonwick 		ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1975e05725b1Sbonwick 		ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
1976e05725b1Sbonwick 		ASSERT3U(bt->bt_offset, ==, -1ULL);
1977e05725b1Sbonwick 		ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
19784d31c452Smaybee 	}
19794d31c452Smaybee 	dmu_buf_rele(db, FTAG);
1980e05725b1Sbonwick 	za->za_dbuf = NULL;
19814d31c452Smaybee }
19824d31c452Smaybee 
19834d31c452Smaybee void
1984fa9e4066Sahrens ztest_dmu_write_parallel(ztest_args_t *za)
1985fa9e4066Sahrens {
1986fa9e4066Sahrens 	objset_t *os = za->za_os;
1987e05725b1Sbonwick 	ztest_block_tag_t *rbt = &za->za_rbt;
1988e05725b1Sbonwick 	ztest_block_tag_t *wbt = &za->za_wbt;
1989e05725b1Sbonwick 	const size_t btsize = sizeof (ztest_block_tag_t);
1990fa9e4066Sahrens 	dmu_buf_t *db;
1991e05725b1Sbonwick 	int b, error;
1992e05725b1Sbonwick 	int bs = ZTEST_DIROBJ_BLOCKSIZE;
1993e05725b1Sbonwick 	int do_free = 0;
1994e05725b1Sbonwick 	uint64_t off, txg_how;
1995fa9e4066Sahrens 	mutex_t *lp;
1996fa9e4066Sahrens 	char osname[MAXNAMELEN];
1997fa9e4066Sahrens 	char iobuf[SPA_MAXBLOCKSIZE];
1998e05725b1Sbonwick 	blkptr_t blk = { 0 };
1999e05725b1Sbonwick 	uint64_t blkoff;
2000e05725b1Sbonwick 	zbookmark_t zb;
2001e05725b1Sbonwick 	dmu_tx_t *tx = dmu_tx_create(os);
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	dmu_objset_name(os, osname);
2004fa9e4066Sahrens 
2005fa9e4066Sahrens 	/*
2006fa9e4066Sahrens 	 * Have multiple threads write to large offsets in ZTEST_DIROBJ
2007fa9e4066Sahrens 	 * to verify that having multiple threads writing to the same object
2008fa9e4066Sahrens 	 * in parallel doesn't cause any trouble.
2009fa9e4066Sahrens 	 */
2010fa9e4066Sahrens 	if (ztest_random(4) == 0) {
2011fa9e4066Sahrens 		/*
2012fa9e4066Sahrens 		 * Do the bonus buffer instead of a regular block.
2013e05725b1Sbonwick 		 * We need a lock to serialize resize vs. others,
2014e05725b1Sbonwick 		 * so we hash on the objset ID.
2015fa9e4066Sahrens 		 */
2016e05725b1Sbonwick 		b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
2017fa9e4066Sahrens 		off = -1ULL;
2018fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
2019e05725b1Sbonwick 	} else {
2020e05725b1Sbonwick 		b = ztest_random(ZTEST_SYNC_LOCKS);
2021e05725b1Sbonwick 		off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
2022e05725b1Sbonwick 		if (ztest_random(4) == 0) {
2023e05725b1Sbonwick 			do_free = 1;
2024fa9e4066Sahrens 			dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
2025e05725b1Sbonwick 		} else {
2026fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
2027e05725b1Sbonwick 		}
2028e05725b1Sbonwick 	}
2029fa9e4066Sahrens 
2030fa9e4066Sahrens 	txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
2031fa9e4066Sahrens 	error = dmu_tx_assign(tx, txg_how);
2032fa9e4066Sahrens 	if (error) {
2033fa9e4066Sahrens 		if (error == ERESTART) {
2034fa9e4066Sahrens 			ASSERT(txg_how == TXG_NOWAIT);
20358a2f1b91Sahrens 			dmu_tx_wait(tx);
2036e05725b1Sbonwick 		} else {
2037e05725b1Sbonwick 			ztest_record_enospc("dmu write parallel");
2038fa9e4066Sahrens 		}
20398a2f1b91Sahrens 		dmu_tx_abort(tx);
2040fa9e4066Sahrens 		return;
2041fa9e4066Sahrens 	}
2042fa9e4066Sahrens 
2043e05725b1Sbonwick 	lp = &ztest_shared->zs_sync_lock[b];
2044fa9e4066Sahrens 	(void) mutex_lock(lp);
2045fa9e4066Sahrens 
2046e05725b1Sbonwick 	wbt->bt_objset = dmu_objset_id(os);
2047e05725b1Sbonwick 	wbt->bt_object = ZTEST_DIROBJ;
2048e05725b1Sbonwick 	wbt->bt_offset = off;
2049e05725b1Sbonwick 	wbt->bt_txg = dmu_tx_get_txg(tx);
2050e05725b1Sbonwick 	wbt->bt_thread = za->za_instance;
2051e05725b1Sbonwick 	wbt->bt_seq = ztest_shared->zs_seq[b]++;	/* protected by lp */
2052fa9e4066Sahrens 
2053fa9e4066Sahrens 	if (off == -1ULL) {
2054e05725b1Sbonwick 		dmu_object_info_t *doi = &za->za_doi;
2055e05725b1Sbonwick 		char *dboff;
20561934e92fSmaybee 
2057e05725b1Sbonwick 		VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2058e05725b1Sbonwick 		za->za_dbuf = db;
2059e05725b1Sbonwick 		dmu_object_info_from_db(db, doi);
2060e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2061e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size, >=, btsize);
2062e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2063e05725b1Sbonwick 		dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2064e05725b1Sbonwick 		bcopy(dboff, rbt, btsize);
2065e05725b1Sbonwick 		if (rbt->bt_objset != 0) {
2066e05725b1Sbonwick 			ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2067e05725b1Sbonwick 			ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2068e05725b1Sbonwick 			ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2069e05725b1Sbonwick 			ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2070fa9e4066Sahrens 		}
20711934e92fSmaybee 		if (ztest_random(10) == 0) {
2072e05725b1Sbonwick 			int newsize = (ztest_random(db->db_size /
2073e05725b1Sbonwick 			    btsize) + 1) * btsize;
20741934e92fSmaybee 
2075e05725b1Sbonwick 			ASSERT3U(newsize, >=, btsize);
20761934e92fSmaybee 			ASSERT3U(newsize, <=, db->db_size);
2077e05725b1Sbonwick 			VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2078e05725b1Sbonwick 			dboff = (char *)db->db_data + newsize - btsize;
20791934e92fSmaybee 		}
2080fa9e4066Sahrens 		dmu_buf_will_dirty(db, tx);
2081e05725b1Sbonwick 		bcopy(wbt, dboff, btsize);
2082ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
2083e05725b1Sbonwick 		za->za_dbuf = NULL;
2084e05725b1Sbonwick 	} else if (do_free) {
2085e05725b1Sbonwick 		VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2086e05725b1Sbonwick 	} else {
2087e05725b1Sbonwick 		dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2088fa9e4066Sahrens 	}
2089fa9e4066Sahrens 
2090fa9e4066Sahrens 	(void) mutex_unlock(lp);
2091fa9e4066Sahrens 
2092e05725b1Sbonwick 	if (ztest_random(1000) == 0)
2093fa9e4066Sahrens 		(void) poll(NULL, 0, 1); /* open dn_notxholds window */
2094fa9e4066Sahrens 
2095fa9e4066Sahrens 	dmu_tx_commit(tx);
2096fa9e4066Sahrens 
2097e05725b1Sbonwick 	if (ztest_random(10000) == 0)
2098e05725b1Sbonwick 		txg_wait_synced(dmu_objset_pool(os), wbt->bt_txg);
2099fa9e4066Sahrens 
2100e05725b1Sbonwick 	if (off == -1 || do_free)
2101e05725b1Sbonwick 		return;
2102fa9e4066Sahrens 
2103e05725b1Sbonwick 	if (ztest_random(2) != 0)
2104e05725b1Sbonwick 		return;
2105e05725b1Sbonwick 
2106e05725b1Sbonwick 	/*
2107e05725b1Sbonwick 	 * dmu_sync() the block we just wrote.
2108e05725b1Sbonwick 	 */
2109fa9e4066Sahrens 	(void) mutex_lock(lp);
2110e05725b1Sbonwick 
2111c5c6ffa0Smaybee 	blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2112e05725b1Sbonwick 	error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2113e05725b1Sbonwick 	za->za_dbuf = db;
2114c5c6ffa0Smaybee 	if (error) {
2115c5c6ffa0Smaybee 		dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n",
2116c5c6ffa0Smaybee 		    osname, ZTEST_DIROBJ, blkoff, error);
2117c5c6ffa0Smaybee 		(void) mutex_unlock(lp);
2118e05725b1Sbonwick 		return;
2119c5c6ffa0Smaybee 	}
2120c5c6ffa0Smaybee 	blkoff = off - blkoff;
2121e05725b1Sbonwick 	error = dmu_sync(NULL, db, &blk, wbt->bt_txg, NULL, NULL);
2122c5c6ffa0Smaybee 	dmu_buf_rele(db, FTAG);
2123e05725b1Sbonwick 	za->za_dbuf = NULL;
2124e05725b1Sbonwick 
2125fa9e4066Sahrens 	(void) mutex_unlock(lp);
2126e05725b1Sbonwick 
2127fa9e4066Sahrens 	if (error) {
2128fa9e4066Sahrens 		dprintf("dmu_sync(%s, %d, %llx) = %d\n",
2129fa9e4066Sahrens 		    osname, ZTEST_DIROBJ, off, error);
2130e05725b1Sbonwick 		return;
2131fa9e4066Sahrens 	}
2132fa9e4066Sahrens 
2133e05725b1Sbonwick 	if (blk.blk_birth == 0)		/* concurrent free */
2134e05725b1Sbonwick 		return;
2135e05725b1Sbonwick 
2136c5c6ffa0Smaybee 	txg_suspend(dmu_objset_pool(os));
2137fa9e4066Sahrens 
2138fa9e4066Sahrens 	ASSERT(blk.blk_fill == 1);
2139fa9e4066Sahrens 	ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2140fa9e4066Sahrens 	ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2141fa9e4066Sahrens 	ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2142fa9e4066Sahrens 
2143fa9e4066Sahrens 	/*
2144e05725b1Sbonwick 	 * Read the block that dmu_sync() returned to make sure its contents
2145e05725b1Sbonwick 	 * match what we wrote.  We do this while still txg_suspend()ed
2146e05725b1Sbonwick 	 * to ensure that the block can't be reused before we read it.
2147fa9e4066Sahrens 	 */
2148ea8dc4b6Seschrock 	zb.zb_objset = dmu_objset_id(os);
2149ea8dc4b6Seschrock 	zb.zb_object = ZTEST_DIROBJ;
2150ea8dc4b6Seschrock 	zb.zb_level = 0;
2151ea8dc4b6Seschrock 	zb.zb_blkid = off / bs;
2152e05725b1Sbonwick 	error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2153e05725b1Sbonwick 	    NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2154e05725b1Sbonwick 	ASSERT3U(error, ==, 0);
2155fa9e4066Sahrens 
2156fa9e4066Sahrens 	txg_resume(dmu_objset_pool(os));
2157fa9e4066Sahrens 
2158e05725b1Sbonwick 	bcopy(&iobuf[blkoff], rbt, btsize);
2159fa9e4066Sahrens 
2160e05725b1Sbonwick 	if (rbt->bt_objset == 0)		/* concurrent free */
2161e05725b1Sbonwick 		return;
21620e34b6a7Sbonwick 
2163e05725b1Sbonwick 	ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2164e05725b1Sbonwick 	ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2165e05725b1Sbonwick 	ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2166fa9e4066Sahrens 
2167fa9e4066Sahrens 	/*
2168e05725b1Sbonwick 	 * The semantic of dmu_sync() is that we always push the most recent
2169e05725b1Sbonwick 	 * version of the data, so in the face of concurrent updates we may
2170fa9e4066Sahrens 	 * see a newer version of the block.  That's OK.
2171fa9e4066Sahrens 	 */
2172e05725b1Sbonwick 	ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2173e05725b1Sbonwick 	if (rbt->bt_thread == wbt->bt_thread)
2174e05725b1Sbonwick 		ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2175fa9e4066Sahrens 	else
2176e05725b1Sbonwick 		ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2177fa9e4066Sahrens }
2178fa9e4066Sahrens 
2179fa9e4066Sahrens /*
2180fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
2181fa9e4066Sahrens  */
2182fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
2183fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
2184fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
2185fa9e4066Sahrens 
2186fa9e4066Sahrens void
2187fa9e4066Sahrens ztest_zap(ztest_args_t *za)
2188fa9e4066Sahrens {
2189fa9e4066Sahrens 	objset_t *os = za->za_os;
2190fa9e4066Sahrens 	uint64_t object;
2191fa9e4066Sahrens 	uint64_t txg, last_txg;
2192fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
2193fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
2194fa9e4066Sahrens 	int i, ints;
2195fa9e4066Sahrens 	dmu_tx_t *tx;
2196fa9e4066Sahrens 	char propname[100], txgname[100];
2197fa9e4066Sahrens 	int error;
2198fa9e4066Sahrens 	char osname[MAXNAMELEN];
2199fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2200fa9e4066Sahrens 
2201fa9e4066Sahrens 	dmu_objset_name(os, osname);
2202fa9e4066Sahrens 
2203fa9e4066Sahrens 	/*
2204fa9e4066Sahrens 	 * Create a new object if necessary, and record it in the directory.
2205fa9e4066Sahrens 	 */
2206ea8dc4b6Seschrock 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2207ea8dc4b6Seschrock 	    sizeof (uint64_t), &object));
2208fa9e4066Sahrens 
2209fa9e4066Sahrens 	if (object == 0) {
2210fa9e4066Sahrens 		tx = dmu_tx_create(os);
2211fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2212fa9e4066Sahrens 		    sizeof (uint64_t));
2213ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2214fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2215fa9e4066Sahrens 		if (error) {
2216fa9e4066Sahrens 			ztest_record_enospc("create zap test obj");
2217fa9e4066Sahrens 			dmu_tx_abort(tx);
2218fa9e4066Sahrens 			return;
2219fa9e4066Sahrens 		}
2220fa9e4066Sahrens 		object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2221fa9e4066Sahrens 		if (error) {
2222fa9e4066Sahrens 			fatal(0, "zap_create('%s', %llu) = %d",
2223fa9e4066Sahrens 			    osname, object, error);
2224fa9e4066Sahrens 		}
2225fa9e4066Sahrens 		ASSERT(object != 0);
2226fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2227fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
2228fa9e4066Sahrens 		/*
2229fa9e4066Sahrens 		 * Generate a known hash collision, and verify that
2230fa9e4066Sahrens 		 * we can lookup and remove both entries.
2231fa9e4066Sahrens 		 */
2232fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2233fa9e4066Sahrens 			value[i] = i;
2234fa9e4066Sahrens 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2235fa9e4066Sahrens 			    1, &value[i], tx);
2236fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2237fa9e4066Sahrens 		}
2238fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2239fa9e4066Sahrens 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2240fa9e4066Sahrens 			    1, &value[i], tx);
2241fa9e4066Sahrens 			ASSERT3U(error, ==, EEXIST);
2242fa9e4066Sahrens 			error = zap_length(os, object, hc[i],
2243fa9e4066Sahrens 			    &zl_intsize, &zl_ints);
2244fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2245fa9e4066Sahrens 			ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2246fa9e4066Sahrens 			ASSERT3U(zl_ints, ==, 1);
2247fa9e4066Sahrens 		}
2248fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2249fa9e4066Sahrens 			error = zap_remove(os, object, hc[i], tx);
2250fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2251fa9e4066Sahrens 		}
2252fa9e4066Sahrens 
2253fa9e4066Sahrens 		dmu_tx_commit(tx);
2254fa9e4066Sahrens 	}
2255fa9e4066Sahrens 
2256fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2257fa9e4066Sahrens 
2258fa9e4066Sahrens 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2259fa9e4066Sahrens 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2260fa9e4066Sahrens 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2261fa9e4066Sahrens 	bzero(value, sizeof (value));
2262fa9e4066Sahrens 	last_txg = 0;
2263fa9e4066Sahrens 
2264fa9e4066Sahrens 	/*
2265fa9e4066Sahrens 	 * If these zap entries already exist, validate their contents.
2266fa9e4066Sahrens 	 */
2267fa9e4066Sahrens 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2268fa9e4066Sahrens 	if (error == 0) {
2269fa9e4066Sahrens 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2270fa9e4066Sahrens 		ASSERT3U(zl_ints, ==, 1);
2271fa9e4066Sahrens 
2272e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2273e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
2274fa9e4066Sahrens 
2275e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
2276e05725b1Sbonwick 		    &zl_ints) == 0);
2277fa9e4066Sahrens 
2278fa9e4066Sahrens 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2279fa9e4066Sahrens 		ASSERT3U(zl_ints, ==, ints);
2280fa9e4066Sahrens 
2281e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
2282e05725b1Sbonwick 		    zl_ints, value) == 0);
2283fa9e4066Sahrens 
2284fa9e4066Sahrens 		for (i = 0; i < ints; i++) {
2285fa9e4066Sahrens 			ASSERT3U(value[i], ==, last_txg + object + i);
2286fa9e4066Sahrens 		}
2287fa9e4066Sahrens 	} else {
2288fa9e4066Sahrens 		ASSERT3U(error, ==, ENOENT);
2289fa9e4066Sahrens 	}
2290fa9e4066Sahrens 
2291fa9e4066Sahrens 	/*
2292fa9e4066Sahrens 	 * Atomically update two entries in our zap object.
2293fa9e4066Sahrens 	 * The first is named txg_%llu, and contains the txg
2294fa9e4066Sahrens 	 * in which the property was last updated.  The second
2295fa9e4066Sahrens 	 * is named prop_%llu, and the nth element of its value
2296fa9e4066Sahrens 	 * should be txg + object + n.
2297fa9e4066Sahrens 	 */
2298fa9e4066Sahrens 	tx = dmu_tx_create(os);
2299ea8dc4b6Seschrock 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2300fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2301fa9e4066Sahrens 	if (error) {
2302fa9e4066Sahrens 		ztest_record_enospc("create zap entry");
2303fa9e4066Sahrens 		dmu_tx_abort(tx);
2304fa9e4066Sahrens 		return;
2305fa9e4066Sahrens 	}
2306fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
2307fa9e4066Sahrens 
2308fa9e4066Sahrens 	if (last_txg > txg)
2309e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2310fa9e4066Sahrens 
2311fa9e4066Sahrens 	for (i = 0; i < ints; i++)
2312fa9e4066Sahrens 		value[i] = txg + object + i;
2313fa9e4066Sahrens 
2314e05725b1Sbonwick 	error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2315fa9e4066Sahrens 	if (error)
2316fa9e4066Sahrens 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2317fa9e4066Sahrens 		    osname, object, txgname, error);
2318fa9e4066Sahrens 
2319fa9e4066Sahrens 	error = zap_update(os, object, propname, sizeof (uint64_t),
2320fa9e4066Sahrens 	    ints, value, tx);
2321fa9e4066Sahrens 	if (error)
2322fa9e4066Sahrens 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2323fa9e4066Sahrens 		    osname, object, propname, error);
2324fa9e4066Sahrens 
2325fa9e4066Sahrens 	dmu_tx_commit(tx);
2326fa9e4066Sahrens 
2327fa9e4066Sahrens 	/*
2328fa9e4066Sahrens 	 * Remove a random pair of entries.
2329fa9e4066Sahrens 	 */
2330fa9e4066Sahrens 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2331fa9e4066Sahrens 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2332fa9e4066Sahrens 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2333fa9e4066Sahrens 
2334fa9e4066Sahrens 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2335fa9e4066Sahrens 
2336fa9e4066Sahrens 	if (error == ENOENT)
2337e05725b1Sbonwick 		return;
2338fa9e4066Sahrens 
2339fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
2340fa9e4066Sahrens 
2341fa9e4066Sahrens 	tx = dmu_tx_create(os);
2342ea8dc4b6Seschrock 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2343fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2344fa9e4066Sahrens 	if (error) {
2345fa9e4066Sahrens 		ztest_record_enospc("remove zap entry");
2346fa9e4066Sahrens 		dmu_tx_abort(tx);
2347fa9e4066Sahrens 		return;
2348fa9e4066Sahrens 	}
2349fa9e4066Sahrens 	error = zap_remove(os, object, txgname, tx);
2350fa9e4066Sahrens 	if (error)
2351fa9e4066Sahrens 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2352fa9e4066Sahrens 		    osname, object, txgname, error);
2353fa9e4066Sahrens 
2354fa9e4066Sahrens 	error = zap_remove(os, object, propname, tx);
2355fa9e4066Sahrens 	if (error)
2356fa9e4066Sahrens 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2357fa9e4066Sahrens 		    osname, object, propname, error);
2358fa9e4066Sahrens 
2359fa9e4066Sahrens 	dmu_tx_commit(tx);
2360fa9e4066Sahrens 
2361fa9e4066Sahrens 	/*
2362fa9e4066Sahrens 	 * Once in a while, destroy the object.
2363fa9e4066Sahrens 	 */
2364e05725b1Sbonwick 	if (ztest_random(1000) != 0)
2365fa9e4066Sahrens 		return;
2366fa9e4066Sahrens 
2367fa9e4066Sahrens 	tx = dmu_tx_create(os);
2368fa9e4066Sahrens 	dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2369fa9e4066Sahrens 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2370fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2371fa9e4066Sahrens 	if (error) {
2372fa9e4066Sahrens 		ztest_record_enospc("destroy zap object");
2373fa9e4066Sahrens 		dmu_tx_abort(tx);
2374fa9e4066Sahrens 		return;
2375fa9e4066Sahrens 	}
2376fa9e4066Sahrens 	error = zap_destroy(os, object, tx);
2377fa9e4066Sahrens 	if (error)
2378fa9e4066Sahrens 		fatal(0, "zap_destroy('%s', %llu) = %d",
2379fa9e4066Sahrens 		    osname, object, error);
2380fa9e4066Sahrens 	object = 0;
2381fa9e4066Sahrens 	dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2382fa9e4066Sahrens 	    &object, tx);
2383fa9e4066Sahrens 	dmu_tx_commit(tx);
2384fa9e4066Sahrens }
2385fa9e4066Sahrens 
2386fa9e4066Sahrens void
2387fa9e4066Sahrens ztest_zap_parallel(ztest_args_t *za)
2388fa9e4066Sahrens {
2389fa9e4066Sahrens 	objset_t *os = za->za_os;
2390fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2391fa9e4066Sahrens 	dmu_tx_t *tx;
2392fa9e4066Sahrens 	int i, namelen, error;
2393fa9e4066Sahrens 	char name[20], string_value[20];
2394fa9e4066Sahrens 	void *data;
2395fa9e4066Sahrens 
2396fa9e4066Sahrens 	/*
2397fa9e4066Sahrens 	 * Generate a random name of the form 'xxx.....' where each
2398fa9e4066Sahrens 	 * x is a random printable character and the dots are dots.
2399fa9e4066Sahrens 	 * There are 94 such characters, and the name length goes from
2400fa9e4066Sahrens 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2401fa9e4066Sahrens 	 */
2402fa9e4066Sahrens 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2403fa9e4066Sahrens 
2404fa9e4066Sahrens 	for (i = 0; i < 3; i++)
2405fa9e4066Sahrens 		name[i] = '!' + ztest_random('~' - '!' + 1);
2406fa9e4066Sahrens 	for (; i < namelen - 1; i++)
2407fa9e4066Sahrens 		name[i] = '.';
2408fa9e4066Sahrens 	name[i] = '\0';
2409fa9e4066Sahrens 
2410fa9e4066Sahrens 	if (ztest_random(2) == 0)
2411fa9e4066Sahrens 		object = ZTEST_MICROZAP_OBJ;
2412fa9e4066Sahrens 	else
2413fa9e4066Sahrens 		object = ZTEST_FATZAP_OBJ;
2414fa9e4066Sahrens 
2415fa9e4066Sahrens 	if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2416fa9e4066Sahrens 		wsize = sizeof (txg);
2417fa9e4066Sahrens 		wc = 1;
2418fa9e4066Sahrens 		data = &txg;
2419fa9e4066Sahrens 	} else {
2420fa9e4066Sahrens 		wsize = 1;
2421fa9e4066Sahrens 		wc = namelen;
2422fa9e4066Sahrens 		data = string_value;
2423fa9e4066Sahrens 	}
2424fa9e4066Sahrens 
2425fa9e4066Sahrens 	count = -1ULL;
2426fa9e4066Sahrens 	VERIFY(zap_count(os, object, &count) == 0);
2427fa9e4066Sahrens 	ASSERT(count != -1ULL);
2428fa9e4066Sahrens 
2429fa9e4066Sahrens 	/*
2430fa9e4066Sahrens 	 * Select an operation: length, lookup, add, update, remove.
2431fa9e4066Sahrens 	 */
2432fa9e4066Sahrens 	i = ztest_random(5);
2433fa9e4066Sahrens 
2434fa9e4066Sahrens 	if (i >= 2) {
2435fa9e4066Sahrens 		tx = dmu_tx_create(os);
2436ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, object, TRUE, NULL);
2437fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2438fa9e4066Sahrens 		if (error) {
2439fa9e4066Sahrens 			ztest_record_enospc("zap parallel");
2440fa9e4066Sahrens 			dmu_tx_abort(tx);
2441fa9e4066Sahrens 			return;
2442fa9e4066Sahrens 		}
2443fa9e4066Sahrens 		txg = dmu_tx_get_txg(tx);
2444fa9e4066Sahrens 		bcopy(name, string_value, namelen);
2445fa9e4066Sahrens 	} else {
2446fa9e4066Sahrens 		tx = NULL;
2447fa9e4066Sahrens 		txg = 0;
2448fa9e4066Sahrens 		bzero(string_value, namelen);
2449fa9e4066Sahrens 	}
2450fa9e4066Sahrens 
2451fa9e4066Sahrens 	switch (i) {
2452fa9e4066Sahrens 
2453fa9e4066Sahrens 	case 0:
2454fa9e4066Sahrens 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
2455fa9e4066Sahrens 		if (error == 0) {
2456fa9e4066Sahrens 			ASSERT3U(wsize, ==, zl_wsize);
2457fa9e4066Sahrens 			ASSERT3U(wc, ==, zl_wc);
2458fa9e4066Sahrens 		} else {
2459fa9e4066Sahrens 			ASSERT3U(error, ==, ENOENT);
2460fa9e4066Sahrens 		}
2461fa9e4066Sahrens 		break;
2462fa9e4066Sahrens 
2463fa9e4066Sahrens 	case 1:
2464fa9e4066Sahrens 		error = zap_lookup(os, object, name, wsize, wc, data);
2465fa9e4066Sahrens 		if (error == 0) {
2466fa9e4066Sahrens 			if (data == string_value &&
2467fa9e4066Sahrens 			    bcmp(name, data, namelen) != 0)
2468fa9e4066Sahrens 				fatal(0, "name '%s' != val '%s' len %d",
2469fa9e4066Sahrens 				    name, data, namelen);
2470fa9e4066Sahrens 		} else {
2471fa9e4066Sahrens 			ASSERT3U(error, ==, ENOENT);
2472fa9e4066Sahrens 		}
2473fa9e4066Sahrens 		break;
2474fa9e4066Sahrens 
2475fa9e4066Sahrens 	case 2:
2476fa9e4066Sahrens 		error = zap_add(os, object, name, wsize, wc, data, tx);
2477fa9e4066Sahrens 		ASSERT(error == 0 || error == EEXIST);
2478fa9e4066Sahrens 		break;
2479fa9e4066Sahrens 
2480fa9e4066Sahrens 	case 3:
2481e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
2482fa9e4066Sahrens 		break;
2483fa9e4066Sahrens 
2484fa9e4066Sahrens 	case 4:
2485fa9e4066Sahrens 		error = zap_remove(os, object, name, tx);
2486fa9e4066Sahrens 		ASSERT(error == 0 || error == ENOENT);
2487fa9e4066Sahrens 		break;
2488fa9e4066Sahrens 	}
2489fa9e4066Sahrens 
2490fa9e4066Sahrens 	if (tx != NULL)
2491fa9e4066Sahrens 		dmu_tx_commit(tx);
2492fa9e4066Sahrens }
2493fa9e4066Sahrens 
2494fa9e4066Sahrens void
2495fa9e4066Sahrens ztest_dsl_prop_get_set(ztest_args_t *za)
2496fa9e4066Sahrens {
2497fa9e4066Sahrens 	objset_t *os = za->za_os;
2498fa9e4066Sahrens 	int i, inherit;
2499fa9e4066Sahrens 	uint64_t value;
2500fa9e4066Sahrens 	const char *prop, *valname;
2501fa9e4066Sahrens 	char setpoint[MAXPATHLEN];
2502fa9e4066Sahrens 	char osname[MAXNAMELEN];
2503ea8dc4b6Seschrock 	int error;
2504fa9e4066Sahrens 
2505fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
2506fa9e4066Sahrens 
2507fa9e4066Sahrens 	dmu_objset_name(os, osname);
2508fa9e4066Sahrens 
2509fa9e4066Sahrens 	for (i = 0; i < 2; i++) {
2510fa9e4066Sahrens 		if (i == 0) {
2511fa9e4066Sahrens 			prop = "checksum";
2512fa9e4066Sahrens 			value = ztest_random_checksum();
2513fa9e4066Sahrens 			inherit = (value == ZIO_CHECKSUM_INHERIT);
2514fa9e4066Sahrens 		} else {
2515fa9e4066Sahrens 			prop = "compression";
2516fa9e4066Sahrens 			value = ztest_random_compress();
2517fa9e4066Sahrens 			inherit = (value == ZIO_COMPRESS_INHERIT);
2518fa9e4066Sahrens 		}
2519fa9e4066Sahrens 
2520ea8dc4b6Seschrock 		error = dsl_prop_set(osname, prop, sizeof (value),
2521ea8dc4b6Seschrock 		    !inherit, &value);
2522ea8dc4b6Seschrock 
2523ea8dc4b6Seschrock 		if (error == ENOSPC) {
2524ea8dc4b6Seschrock 			ztest_record_enospc("dsl_prop_set");
2525ea8dc4b6Seschrock 			break;
2526ea8dc4b6Seschrock 		}
2527ea8dc4b6Seschrock 
2528ea8dc4b6Seschrock 		ASSERT3U(error, ==, 0);
2529fa9e4066Sahrens 
2530fa9e4066Sahrens 		VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
2531fa9e4066Sahrens 		    1, &value, setpoint), ==, 0);
2532fa9e4066Sahrens 
2533fa9e4066Sahrens 		if (i == 0)
2534fa9e4066Sahrens 			valname = zio_checksum_table[value].ci_name;
2535fa9e4066Sahrens 		else
2536fa9e4066Sahrens 			valname = zio_compress_table[value].ci_name;
2537fa9e4066Sahrens 
2538fa9e4066Sahrens 		if (zopt_verbose >= 6) {
2539fa9e4066Sahrens 			(void) printf("%s %s = %s for '%s'\n",
2540fa9e4066Sahrens 			    osname, prop, valname, setpoint);
2541fa9e4066Sahrens 		}
2542fa9e4066Sahrens 	}
2543fa9e4066Sahrens 
2544fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
2545fa9e4066Sahrens }
2546fa9e4066Sahrens 
2547ea8dc4b6Seschrock static void
2548ea8dc4b6Seschrock ztest_error_setup(vdev_t *vd, int mode, int mask, uint64_t arg)
2549ea8dc4b6Seschrock {
2550ea8dc4b6Seschrock 	int c;
2551ea8dc4b6Seschrock 
2552ea8dc4b6Seschrock 	for (c = 0; c < vd->vdev_children; c++)
2553ea8dc4b6Seschrock 		ztest_error_setup(vd->vdev_child[c], mode, mask, arg);
2554ea8dc4b6Seschrock 
2555ea8dc4b6Seschrock 	if (vd->vdev_path != NULL) {
2556ea8dc4b6Seschrock 		vd->vdev_fault_mode = mode;
2557ea8dc4b6Seschrock 		vd->vdev_fault_mask = mask;
2558ea8dc4b6Seschrock 		vd->vdev_fault_arg = arg;
2559ea8dc4b6Seschrock 	}
2560ea8dc4b6Seschrock }
2561ea8dc4b6Seschrock 
2562fa9e4066Sahrens /*
2563fa9e4066Sahrens  * Inject random faults into the on-disk data.
2564fa9e4066Sahrens  */
2565fa9e4066Sahrens void
2566fa9e4066Sahrens ztest_fault_inject(ztest_args_t *za)
2567fa9e4066Sahrens {
2568fa9e4066Sahrens 	int fd;
2569fa9e4066Sahrens 	uint64_t offset;
2570fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2571fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
2572fa9e4066Sahrens 	uint64_t top, leaf;
2573fa9e4066Sahrens 	char path0[MAXPATHLEN];
2574fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
2575fa9e4066Sahrens 	size_t fsize;
2576e05725b1Sbonwick 	spa_t *spa = za->za_spa;
2577fa9e4066Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
2578fa9e4066Sahrens 	int iters = 1000;
2579ea8dc4b6Seschrock 	vdev_t *vd0;
2580ea8dc4b6Seschrock 	uint64_t guid0 = 0;
2581ea8dc4b6Seschrock 
2582ea8dc4b6Seschrock 	/*
2583ea8dc4b6Seschrock 	 * We can't inject faults when we have no fault tolerance.
2584ea8dc4b6Seschrock 	 */
2585ea8dc4b6Seschrock 	if (zopt_maxfaults == 0)
2586ea8dc4b6Seschrock 		return;
2587ea8dc4b6Seschrock 
2588ea8dc4b6Seschrock 	ASSERT(leaves >= 2);
2589fa9e4066Sahrens 
2590fa9e4066Sahrens 	/*
2591fa9e4066Sahrens 	 * Pick a random top-level vdev.
2592fa9e4066Sahrens 	 */
2593ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2594fa9e4066Sahrens 	top = ztest_random(spa->spa_root_vdev->vdev_children);
2595ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2596fa9e4066Sahrens 
2597fa9e4066Sahrens 	/*
2598fa9e4066Sahrens 	 * Pick a random leaf.
2599fa9e4066Sahrens 	 */
2600fa9e4066Sahrens 	leaf = ztest_random(leaves);
2601fa9e4066Sahrens 
2602fa9e4066Sahrens 	/*
2603ea8dc4b6Seschrock 	 * Generate paths to the first two leaves in this top-level vdev,
2604fa9e4066Sahrens 	 * and to the random leaf we selected.  We'll induce transient
2605ea8dc4b6Seschrock 	 * I/O errors and random online/offline activity on leaf 0,
2606fa9e4066Sahrens 	 * and we'll write random garbage to the randomly chosen leaf.
2607fa9e4066Sahrens 	 */
2608fa9e4066Sahrens 	(void) snprintf(path0, sizeof (path0),
2609fa9e4066Sahrens 	    ztest_dev_template, zopt_dir, zopt_pool, top * leaves + 0);
2610fa9e4066Sahrens 	(void) snprintf(pathrand, sizeof (pathrand),
2611fa9e4066Sahrens 	    ztest_dev_template, zopt_dir, zopt_pool, top * leaves + leaf);
2612fa9e4066Sahrens 
2613ea8dc4b6Seschrock 	dprintf("damaging %s and %s\n", path0, pathrand);
2614fa9e4066Sahrens 
2615ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2616fa9e4066Sahrens 
2617fa9e4066Sahrens 	/*
2618ea8dc4b6Seschrock 	 * If we can tolerate two or more faults, make vd0 fail randomly.
2619fa9e4066Sahrens 	 */
2620ea8dc4b6Seschrock 	vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
2621ea8dc4b6Seschrock 	if (vd0 != NULL && zopt_maxfaults >= 2) {
2622ea8dc4b6Seschrock 		guid0 = vd0->vdev_guid;
2623ea8dc4b6Seschrock 		ztest_error_setup(vd0, VDEV_FAULT_COUNT,
2624ea8dc4b6Seschrock 		    (1U << ZIO_TYPE_READ) | (1U << ZIO_TYPE_WRITE), 100);
2625ea8dc4b6Seschrock 	}
2626ea8dc4b6Seschrock 
2627ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2628ea8dc4b6Seschrock 
2629ea8dc4b6Seschrock 	/*
2630ea8dc4b6Seschrock 	 * If we can tolerate two or more faults, randomly online/offline vd0.
2631ea8dc4b6Seschrock 	 */
2632ea8dc4b6Seschrock 	if (zopt_maxfaults >= 2 && guid0 != 0) {
2633fa9e4066Sahrens 		if (ztest_random(10) < 6)
2634ea8dc4b6Seschrock 			(void) vdev_offline(spa, guid0, B_TRUE);
2635fa9e4066Sahrens 		else
26363d7072f8Seschrock 			(void) vdev_online(spa, guid0, B_FALSE, NULL);
2637fa9e4066Sahrens 	}
2638fa9e4066Sahrens 
2639fa9e4066Sahrens 	/*
2640ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
2641fa9e4066Sahrens 	 */
2642fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
2643fa9e4066Sahrens 
2644fa9e4066Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
2645fa9e4066Sahrens 		return;
2646fa9e4066Sahrens 
2647fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
2648fa9e4066Sahrens 
2649fa9e4066Sahrens 	while (--iters != 0) {
2650fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
2651fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
2652fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
2653fa9e4066Sahrens 
2654fa9e4066Sahrens 		if (offset >= fsize)
2655fa9e4066Sahrens 			continue;
2656fa9e4066Sahrens 
2657fa9e4066Sahrens 		if (zopt_verbose >= 6)
2658fa9e4066Sahrens 			(void) printf("injecting bad word into %s,"
2659fa9e4066Sahrens 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
2660fa9e4066Sahrens 
2661fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
2662fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
2663fa9e4066Sahrens 			    offset, pathrand);
2664fa9e4066Sahrens 	}
2665fa9e4066Sahrens 
2666fa9e4066Sahrens 	(void) close(fd);
2667fa9e4066Sahrens }
2668fa9e4066Sahrens 
2669fa9e4066Sahrens /*
2670fa9e4066Sahrens  * Scrub the pool.
2671fa9e4066Sahrens  */
2672fa9e4066Sahrens void
2673fa9e4066Sahrens ztest_scrub(ztest_args_t *za)
2674fa9e4066Sahrens {
2675e05725b1Sbonwick 	spa_t *spa = za->za_spa;
2676fa9e4066Sahrens 
2677bb8b5132Sek110237 	mutex_enter(&spa_namespace_lock);
2678fa9e4066Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_FALSE);
2679bb8b5132Sek110237 	mutex_exit(&spa_namespace_lock);
2680fa9e4066Sahrens 	(void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
2681bb8b5132Sek110237 	mutex_enter(&spa_namespace_lock);
2682fa9e4066Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING, B_FALSE);
2683bb8b5132Sek110237 	mutex_exit(&spa_namespace_lock);
2684fa9e4066Sahrens }
2685fa9e4066Sahrens 
2686fa9e4066Sahrens /*
2687fa9e4066Sahrens  * Rename the pool to a different name and then rename it back.
2688fa9e4066Sahrens  */
2689fa9e4066Sahrens void
2690fa9e4066Sahrens ztest_spa_rename(ztest_args_t *za)
2691fa9e4066Sahrens {
2692fa9e4066Sahrens 	char *oldname, *newname;
2693fa9e4066Sahrens 	int error;
2694fa9e4066Sahrens 	spa_t *spa;
2695fa9e4066Sahrens 
2696fa9e4066Sahrens 	(void) rw_wrlock(&ztest_shared->zs_name_lock);
2697fa9e4066Sahrens 
2698fa9e4066Sahrens 	oldname = za->za_pool;
2699fa9e4066Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
2700fa9e4066Sahrens 	(void) strcpy(newname, oldname);
2701fa9e4066Sahrens 	(void) strcat(newname, "_tmp");
2702fa9e4066Sahrens 
2703fa9e4066Sahrens 	/*
2704fa9e4066Sahrens 	 * Do the rename
2705fa9e4066Sahrens 	 */
2706fa9e4066Sahrens 	error = spa_rename(oldname, newname);
2707fa9e4066Sahrens 	if (error)
2708fa9e4066Sahrens 		fatal(0, "spa_rename('%s', '%s') = %d", oldname,
2709fa9e4066Sahrens 		    newname, error);
2710fa9e4066Sahrens 
2711fa9e4066Sahrens 	/*
2712fa9e4066Sahrens 	 * Try to open it under the old name, which shouldn't exist
2713fa9e4066Sahrens 	 */
2714fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
2715fa9e4066Sahrens 	if (error != ENOENT)
2716fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
2717fa9e4066Sahrens 
2718fa9e4066Sahrens 	/*
2719fa9e4066Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
2720fa9e4066Sahrens 	 */
2721fa9e4066Sahrens 	error = spa_open(newname, &spa, FTAG);
2722fa9e4066Sahrens 	if (error != 0)
2723fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
2724fa9e4066Sahrens 
2725e05725b1Sbonwick 	ASSERT(spa == za->za_spa);
2726fa9e4066Sahrens 	spa_close(spa, FTAG);
2727fa9e4066Sahrens 
2728fa9e4066Sahrens 	/*
2729fa9e4066Sahrens 	 * Rename it back to the original
2730fa9e4066Sahrens 	 */
2731fa9e4066Sahrens 	error = spa_rename(newname, oldname);
2732fa9e4066Sahrens 	if (error)
2733fa9e4066Sahrens 		fatal(0, "spa_rename('%s', '%s') = %d", newname,
2734fa9e4066Sahrens 		    oldname, error);
2735fa9e4066Sahrens 
2736fa9e4066Sahrens 	/*
2737fa9e4066Sahrens 	 * Make sure it can still be opened
2738fa9e4066Sahrens 	 */
2739fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
2740fa9e4066Sahrens 	if (error != 0)
2741fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
2742fa9e4066Sahrens 
2743e05725b1Sbonwick 	ASSERT(spa == za->za_spa);
2744fa9e4066Sahrens 	spa_close(spa, FTAG);
2745fa9e4066Sahrens 
2746fa9e4066Sahrens 	umem_free(newname, strlen(newname) + 1);
2747fa9e4066Sahrens 
2748fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
2749fa9e4066Sahrens }
2750fa9e4066Sahrens 
2751fa9e4066Sahrens 
2752fa9e4066Sahrens /*
2753fa9e4066Sahrens  * Completely obliterate one disk.
2754fa9e4066Sahrens  */
2755fa9e4066Sahrens static void
2756fa9e4066Sahrens ztest_obliterate_one_disk(uint64_t vdev)
2757fa9e4066Sahrens {
2758fa9e4066Sahrens 	int fd;
2759d80c45e0Sbonwick 	char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
2760fa9e4066Sahrens 	size_t fsize;
2761fa9e4066Sahrens 
2762fa9e4066Sahrens 	if (zopt_maxfaults < 2)
2763fa9e4066Sahrens 		return;
2764fa9e4066Sahrens 
2765fa9e4066Sahrens 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2766d80c45e0Sbonwick 	(void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	fd = open(dev_name, O_RDWR);
2769fa9e4066Sahrens 
2770fa9e4066Sahrens 	if (fd == -1)
2771fa9e4066Sahrens 		fatal(1, "can't open %s", dev_name);
2772fa9e4066Sahrens 
2773fa9e4066Sahrens 	/*
2774fa9e4066Sahrens 	 * Determine the size.
2775fa9e4066Sahrens 	 */
2776fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
2777d80c45e0Sbonwick 
2778fa9e4066Sahrens 	(void) close(fd);
2779fa9e4066Sahrens 
2780fa9e4066Sahrens 	/*
2781d80c45e0Sbonwick 	 * Rename the old device to dev_name.old (useful for debugging).
2782fa9e4066Sahrens 	 */
2783d80c45e0Sbonwick 	VERIFY(rename(dev_name, copy_name) == 0);
2784fa9e4066Sahrens 
2785fa9e4066Sahrens 	/*
2786fa9e4066Sahrens 	 * Create a new one.
2787fa9e4066Sahrens 	 */
2788fa9e4066Sahrens 	VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
2789fa9e4066Sahrens 	VERIFY(ftruncate(fd, fsize) == 0);
2790fa9e4066Sahrens 	(void) close(fd);
2791fa9e4066Sahrens }
2792fa9e4066Sahrens 
2793fa9e4066Sahrens static void
2794fa9e4066Sahrens ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
2795fa9e4066Sahrens {
2796fa9e4066Sahrens 	char dev_name[MAXPATHLEN];
2797fa9e4066Sahrens 	nvlist_t *file, *root;
2798fa9e4066Sahrens 	int error;
2799ea8dc4b6Seschrock 	uint64_t guid;
2800ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
2801ea8dc4b6Seschrock 	vdev_t *vd;
2802fa9e4066Sahrens 
2803fa9e4066Sahrens 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2804fa9e4066Sahrens 
2805fa9e4066Sahrens 	/*
2806fa9e4066Sahrens 	 * Build the nvlist describing dev_name.
2807fa9e4066Sahrens 	 */
2808fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
2809fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
2810fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, dev_name) == 0);
2811ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
2812fa9e4066Sahrens 
2813fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
2814fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
2815fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(root, ZPOOL_CONFIG_CHILDREN,
2816fa9e4066Sahrens 	    &file, 1) == 0);
2817fa9e4066Sahrens 
2818ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
2819ea8dc4b6Seschrock 	if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
2820ea8dc4b6Seschrock 		guid = 0;
2821ea8dc4b6Seschrock 	else
2822ea8dc4b6Seschrock 		guid = vd->vdev_guid;
2823ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
2824ea8dc4b6Seschrock 	error = spa_vdev_attach(spa, guid, root, B_TRUE);
2825ecc2d604Sbonwick 	if (error != 0 &&
2826ecc2d604Sbonwick 	    error != EBUSY &&
2827ecc2d604Sbonwick 	    error != ENOTSUP &&
2828ecc2d604Sbonwick 	    error != ENODEV &&
2829ecc2d604Sbonwick 	    error != EDOM)
2830fa9e4066Sahrens 		fatal(0, "spa_vdev_attach(in-place) = %d", error);
2831fa9e4066Sahrens 
2832fa9e4066Sahrens 	nvlist_free(file);
2833fa9e4066Sahrens 	nvlist_free(root);
2834fa9e4066Sahrens }
2835fa9e4066Sahrens 
2836fa9e4066Sahrens static void
2837fa9e4066Sahrens ztest_verify_blocks(char *pool)
2838fa9e4066Sahrens {
2839fa9e4066Sahrens 	int status;
2840fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
2841fa9e4066Sahrens 	char zbuf[1024];
2842fa9e4066Sahrens 	char *bin;
28438654d025Sperrin 	char *ztest;
28448654d025Sperrin 	char *isa;
28458654d025Sperrin 	int isalen;
2846fa9e4066Sahrens 	FILE *fp;
2847fa9e4066Sahrens 
2848fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
2849fa9e4066Sahrens 
2850fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
2851fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
28528654d025Sperrin 	ztest = strstr(bin, "/ztest");
28538654d025Sperrin 	isa = bin + 8;
28548654d025Sperrin 	isalen = ztest - isa;
28558654d025Sperrin 	isa = strdup(isa);
2856fa9e4066Sahrens 	/* LINTED */
2857*3a57275aSck153898 	(void) sprintf(bin,
2858*3a57275aSck153898 	    "/usr/sbin%.*s/zdb -bc%s%s -U /tmp/zpool.cache -O %s %s",
28598654d025Sperrin 	    isalen,
28608654d025Sperrin 	    isa,
2861fa9e4066Sahrens 	    zopt_verbose >= 3 ? "s" : "",
2862fa9e4066Sahrens 	    zopt_verbose >= 4 ? "v" : "",
2863fa9e4066Sahrens 	    ztest_random(2) == 0 ? "pre" : "post", pool);
28648654d025Sperrin 	free(isa);
2865fa9e4066Sahrens 
2866fa9e4066Sahrens 	if (zopt_verbose >= 5)
2867fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 	fp = popen(zdb, "r");
2870fa9e4066Sahrens 
2871fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
2872fa9e4066Sahrens 		if (zopt_verbose >= 3)
2873fa9e4066Sahrens 			(void) printf("%s", zbuf);
2874fa9e4066Sahrens 
2875fa9e4066Sahrens 	status = pclose(fp);
2876fa9e4066Sahrens 
2877fa9e4066Sahrens 	if (status == 0)
2878fa9e4066Sahrens 		return;
2879fa9e4066Sahrens 
2880fa9e4066Sahrens 	ztest_dump_core = 0;
2881fa9e4066Sahrens 	if (WIFEXITED(status))
2882fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
2883fa9e4066Sahrens 	else
2884fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
2885fa9e4066Sahrens }
2886fa9e4066Sahrens 
2887fa9e4066Sahrens static void
2888fa9e4066Sahrens ztest_walk_pool_directory(char *header)
2889fa9e4066Sahrens {
2890fa9e4066Sahrens 	spa_t *spa = NULL;
2891fa9e4066Sahrens 
2892fa9e4066Sahrens 	if (zopt_verbose >= 6)
2893fa9e4066Sahrens 		(void) printf("%s\n", header);
2894fa9e4066Sahrens 
2895fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
2896fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
2897fa9e4066Sahrens 		if (zopt_verbose >= 6)
2898fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
2899fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
2900fa9e4066Sahrens }
2901fa9e4066Sahrens 
2902fa9e4066Sahrens static void
2903fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
2904fa9e4066Sahrens {
2905fa9e4066Sahrens 	nvlist_t *config;
2906fa9e4066Sahrens 	uint64_t pool_guid;
2907fa9e4066Sahrens 	spa_t *spa;
2908fa9e4066Sahrens 	int error;
2909fa9e4066Sahrens 
2910fa9e4066Sahrens 	if (zopt_verbose >= 4) {
2911fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
2912fa9e4066Sahrens 		    oldname, newname);
2913fa9e4066Sahrens 	}
2914fa9e4066Sahrens 
2915fa9e4066Sahrens 	/*
2916fa9e4066Sahrens 	 * Clean up from previous runs.
2917fa9e4066Sahrens 	 */
2918fa9e4066Sahrens 	(void) spa_destroy(newname);
2919fa9e4066Sahrens 
2920fa9e4066Sahrens 	/*
2921fa9e4066Sahrens 	 * Get the pool's configuration and guid.
2922fa9e4066Sahrens 	 */
2923fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
2924fa9e4066Sahrens 	if (error)
2925fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
2926fa9e4066Sahrens 
2927fa9e4066Sahrens 	pool_guid = spa_guid(spa);
2928fa9e4066Sahrens 	spa_close(spa, FTAG);
2929fa9e4066Sahrens 
2930fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
2931fa9e4066Sahrens 
2932fa9e4066Sahrens 	/*
2933fa9e4066Sahrens 	 * Export it.
2934fa9e4066Sahrens 	 */
293544cd46caSbillm 	error = spa_export(oldname, &config);
2936fa9e4066Sahrens 	if (error)
2937fa9e4066Sahrens 		fatal(0, "spa_export('%s') = %d", oldname, error);
2938fa9e4066Sahrens 
2939fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
2940fa9e4066Sahrens 
2941fa9e4066Sahrens 	/*
2942fa9e4066Sahrens 	 * Import it under the new name.
2943fa9e4066Sahrens 	 */
2944fa9e4066Sahrens 	error = spa_import(newname, config, NULL);
2945fa9e4066Sahrens 	if (error)
2946fa9e4066Sahrens 		fatal(0, "spa_import('%s') = %d", newname, error);
2947fa9e4066Sahrens 
2948fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
2949fa9e4066Sahrens 
2950fa9e4066Sahrens 	/*
2951fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
2952fa9e4066Sahrens 	 */
2953fa9e4066Sahrens 	error = spa_import(newname, config, NULL);
2954fa9e4066Sahrens 	if (error != EEXIST)
2955fa9e4066Sahrens 		fatal(0, "spa_import('%s') twice", newname);
2956fa9e4066Sahrens 
2957fa9e4066Sahrens 	/*
2958fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
2959fa9e4066Sahrens 	 */
2960fa9e4066Sahrens 	error = spa_import(oldname, config, NULL);
2961fa9e4066Sahrens 	if (error != EEXIST)
2962fa9e4066Sahrens 		fatal(0, "spa_import('%s') under multiple names", newname);
2963fa9e4066Sahrens 
2964fa9e4066Sahrens 	/*
2965fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
2966fa9e4066Sahrens 	 */
2967fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
2968fa9e4066Sahrens 	if (error != ENOENT)
2969fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
2970fa9e4066Sahrens 
2971fa9e4066Sahrens 	/*
2972fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
2973fa9e4066Sahrens 	 */
2974fa9e4066Sahrens 	error = spa_open(newname, &spa, FTAG);
2975fa9e4066Sahrens 	if (error)
2976fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
2977fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
2978fa9e4066Sahrens 	spa_close(spa, FTAG);
2979fa9e4066Sahrens 
2980fa9e4066Sahrens 	nvlist_free(config);
2981fa9e4066Sahrens }
2982fa9e4066Sahrens 
29830a4e9518Sgw25295 /* ARGSUSED */
29840a4e9518Sgw25295 static void *
29850a4e9518Sgw25295 ztest_suspend_monitor(void *arg)
29860a4e9518Sgw25295 {
29870a4e9518Sgw25295 	spa_t *spa;
29880a4e9518Sgw25295 	int error;
29890a4e9518Sgw25295 
29900a4e9518Sgw25295 	error = spa_open(zopt_pool, &spa, FTAG);
29910a4e9518Sgw25295 	if (error) {
29920a4e9518Sgw25295 		(void) printf("Unable to monitor pool '%s'\n", zopt_pool);
29930a4e9518Sgw25295 		return (NULL);
29940a4e9518Sgw25295 	}
29950a4e9518Sgw25295 
29960a4e9518Sgw25295 	while (!ztest_exiting) {
29970a4e9518Sgw25295 		mutex_enter(&spa->spa_zio_lock);
29980a4e9518Sgw25295 		while (!ztest_exiting && list_is_empty(&spa->spa_zio_list))
29990a4e9518Sgw25295 			cv_wait(&spa->spa_zio_cv, &spa->spa_zio_lock);
30000a4e9518Sgw25295 		mutex_exit(&spa->spa_zio_lock);
30010a4e9518Sgw25295 
30020a4e9518Sgw25295 		(void) sleep(3);
30030a4e9518Sgw25295 		/*
30040a4e9518Sgw25295 		 * We don't hold the spa_config_lock since the pool is in
30050a4e9518Sgw25295 		 * complete failure mode and there is no way for us to
30060a4e9518Sgw25295 		 * change the vdev config when we're in this state.
30070a4e9518Sgw25295 		 */
30080a4e9518Sgw25295 		while ((error = zio_vdev_resume_io(spa)) != 0) {
30090a4e9518Sgw25295 			(void) printf("I/O could not be resumed, %d\n", error);
30100a4e9518Sgw25295 			(void) sleep(1);
30110a4e9518Sgw25295 		}
30120a4e9518Sgw25295 		vdev_clear(spa, NULL, B_TRUE);
30130a4e9518Sgw25295 	}
30140a4e9518Sgw25295 	spa_close(spa, FTAG);
30150a4e9518Sgw25295 	return (NULL);
30160a4e9518Sgw25295 }
30170a4e9518Sgw25295 
3018fa9e4066Sahrens static void *
3019fa9e4066Sahrens ztest_thread(void *arg)
3020fa9e4066Sahrens {
3021fa9e4066Sahrens 	ztest_args_t *za = arg;
3022fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
3023fa9e4066Sahrens 	hrtime_t now, functime;
3024fa9e4066Sahrens 	ztest_info_t *zi;
3025e05725b1Sbonwick 	int f, i;
3026fa9e4066Sahrens 
3027fa9e4066Sahrens 	while ((now = gethrtime()) < za->za_stop) {
3028fa9e4066Sahrens 		/*
3029fa9e4066Sahrens 		 * See if it's time to force a crash.
3030fa9e4066Sahrens 		 */
3031fa9e4066Sahrens 		if (now > za->za_kill) {
3032e05725b1Sbonwick 			zs->zs_alloc = spa_get_alloc(za->za_spa);
3033e05725b1Sbonwick 			zs->zs_space = spa_get_space(za->za_spa);
3034fa9e4066Sahrens 			(void) kill(getpid(), SIGKILL);
3035fa9e4066Sahrens 		}
3036fa9e4066Sahrens 
3037fa9e4066Sahrens 		/*
3038fa9e4066Sahrens 		 * Pick a random function.
3039fa9e4066Sahrens 		 */
3040fa9e4066Sahrens 		f = ztest_random(ZTEST_FUNCS);
3041fa9e4066Sahrens 		zi = &zs->zs_info[f];
3042fa9e4066Sahrens 
3043fa9e4066Sahrens 		/*
3044fa9e4066Sahrens 		 * Decide whether to call it, based on the requested frequency.
3045fa9e4066Sahrens 		 */
3046fa9e4066Sahrens 		if (zi->zi_call_target == 0 ||
3047fa9e4066Sahrens 		    (double)zi->zi_call_total / zi->zi_call_target >
3048fa9e4066Sahrens 		    (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3049fa9e4066Sahrens 			continue;
3050fa9e4066Sahrens 
3051fa9e4066Sahrens 		atomic_add_64(&zi->zi_calls, 1);
3052fa9e4066Sahrens 		atomic_add_64(&zi->zi_call_total, 1);
3053fa9e4066Sahrens 
3054fa9e4066Sahrens 		za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3055fa9e4066Sahrens 		    ZTEST_DIRSIZE;
3056fa9e4066Sahrens 		za->za_diroff_shared = (1ULL << 63);
3057fa9e4066Sahrens 
3058e05725b1Sbonwick 		for (i = 0; i < zi->zi_iters; i++)
3059fa9e4066Sahrens 			zi->zi_func(za);
3060fa9e4066Sahrens 
3061fa9e4066Sahrens 		functime = gethrtime() - now;
3062fa9e4066Sahrens 
3063fa9e4066Sahrens 		atomic_add_64(&zi->zi_call_time, functime);
3064fa9e4066Sahrens 
3065fa9e4066Sahrens 		if (zopt_verbose >= 4) {
3066fa9e4066Sahrens 			Dl_info dli;
3067fa9e4066Sahrens 			(void) dladdr((void *)zi->zi_func, &dli);
3068fa9e4066Sahrens 			(void) printf("%6.2f sec in %s\n",
3069fa9e4066Sahrens 			    (double)functime / NANOSEC, dli.dli_sname);
3070fa9e4066Sahrens 		}
3071fa9e4066Sahrens 
3072fa9e4066Sahrens 		/*
3073fa9e4066Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
3074fa9e4066Sahrens 		 */
3075fa9e4066Sahrens 		if (zs->zs_enospc_count > 10)
3076fa9e4066Sahrens 			break;
3077fa9e4066Sahrens 	}
3078fa9e4066Sahrens 
3079fa9e4066Sahrens 	return (NULL);
3080fa9e4066Sahrens }
3081fa9e4066Sahrens 
3082fa9e4066Sahrens /*
3083fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
3084fa9e4066Sahrens  */
3085fa9e4066Sahrens static void
3086fa9e4066Sahrens ztest_run(char *pool)
3087fa9e4066Sahrens {
3088fa9e4066Sahrens 	int t, d, error;
3089fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
3090fa9e4066Sahrens 	ztest_args_t *za;
3091fa9e4066Sahrens 	spa_t *spa;
3092fa9e4066Sahrens 	char name[100];
30930a4e9518Sgw25295 	thread_t tid;
3094fa9e4066Sahrens 
3095fa9e4066Sahrens 	(void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3096fa9e4066Sahrens 	(void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3097fa9e4066Sahrens 
3098fa9e4066Sahrens 	for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3099fa9e4066Sahrens 		(void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3100fa9e4066Sahrens 
3101fa9e4066Sahrens 	/*
3102fa9e4066Sahrens 	 * Destroy one disk before we even start.
3103fa9e4066Sahrens 	 * It's mirrored, so everything should work just fine.
3104fa9e4066Sahrens 	 * This makes us exercise fault handling very early in spa_load().
3105fa9e4066Sahrens 	 */
3106fa9e4066Sahrens 	ztest_obliterate_one_disk(0);
3107fa9e4066Sahrens 
3108fa9e4066Sahrens 	/*
3109fa9e4066Sahrens 	 * Verify that the sum of the sizes of all blocks in the pool
3110fa9e4066Sahrens 	 * equals the SPA's allocated space total.
3111fa9e4066Sahrens 	 */
3112fa9e4066Sahrens 	ztest_verify_blocks(pool);
3113fa9e4066Sahrens 
3114fa9e4066Sahrens 	/*
3115fa9e4066Sahrens 	 * Kick off a replacement of the disk we just obliterated.
3116fa9e4066Sahrens 	 */
3117fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3118fa9e4066Sahrens 	error = spa_open(pool, &spa, FTAG);
3119fa9e4066Sahrens 	if (error)
3120fa9e4066Sahrens 		fatal(0, "spa_open(%s) = %d", pool, error);
3121fa9e4066Sahrens 	ztest_replace_one_disk(spa, 0);
3122fa9e4066Sahrens 	if (zopt_verbose >= 5)
3123fa9e4066Sahrens 		show_pool_stats(spa);
3124fa9e4066Sahrens 	spa_close(spa, FTAG);
3125fa9e4066Sahrens 	kernel_fini();
3126fa9e4066Sahrens 
3127fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3128fa9e4066Sahrens 
3129fa9e4066Sahrens 	/*
3130fa9e4066Sahrens 	 * Verify that we can export the pool and reimport it under a
3131fa9e4066Sahrens 	 * different name.
3132fa9e4066Sahrens 	 */
31330e34b6a7Sbonwick 	if (ztest_random(2) == 0) {
3134fa9e4066Sahrens 		(void) snprintf(name, 100, "%s_import", pool);
3135fa9e4066Sahrens 		ztest_spa_import_export(pool, name);
3136fa9e4066Sahrens 		ztest_spa_import_export(name, pool);
31370e34b6a7Sbonwick 	}
3138fa9e4066Sahrens 
3139fa9e4066Sahrens 	/*
3140fa9e4066Sahrens 	 * Verify that we can loop over all pools.
3141fa9e4066Sahrens 	 */
3142fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3143fa9e4066Sahrens 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3144fa9e4066Sahrens 		if (zopt_verbose > 3) {
3145fa9e4066Sahrens 			(void) printf("spa_next: found %s\n", spa_name(spa));
3146fa9e4066Sahrens 		}
3147fa9e4066Sahrens 	}
3148fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
3149fa9e4066Sahrens 
3150fa9e4066Sahrens 	/*
31510a4e9518Sgw25295 	 * Create a thread to handling complete pool failures. This
31520a4e9518Sgw25295 	 * thread will kickstart the I/Os when they suspend. We must
31530a4e9518Sgw25295 	 * start the thread before setting the zio_io_fail_shift, which
31540a4e9518Sgw25295 	 * will indicate our failure rate.
31550a4e9518Sgw25295 	 */
31560a4e9518Sgw25295 	error = thr_create(0, 0, ztest_suspend_monitor, NULL, THR_BOUND, &tid);
31570a4e9518Sgw25295 	if (error) {
31580a4e9518Sgw25295 		fatal(0, "can't create suspend monitor thread: error %d",
31590a4e9518Sgw25295 		    t, error);
31600a4e9518Sgw25295 	}
31610a4e9518Sgw25295 
31620a4e9518Sgw25295 	/*
3163fa9e4066Sahrens 	 * Open our pool.
3164fa9e4066Sahrens 	 */
3165fa9e4066Sahrens 	error = spa_open(pool, &spa, FTAG);
3166fa9e4066Sahrens 	if (error)
3167fa9e4066Sahrens 		fatal(0, "spa_open() = %d", error);
3168fa9e4066Sahrens 
3169fa9e4066Sahrens 	/*
3170fa9e4066Sahrens 	 * Verify that we can safely inquire about about any object,
3171fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
3172fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
3173fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
3174fa9e4066Sahrens 	 */
3175fa9e4066Sahrens 	for (t = 0; t < 64; t++) {
3176fa9e4066Sahrens 		for (d = -5; d <= 5; d++) {
3177fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
3178fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
3179ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
3180ea8dc4b6Seschrock 			    error == EINVAL);
3181fa9e4066Sahrens 		}
3182fa9e4066Sahrens 	}
3183fa9e4066Sahrens 
3184fa9e4066Sahrens 	/*
3185fa9e4066Sahrens 	 * Now kick off all the tests that run in parallel.
3186fa9e4066Sahrens 	 */
3187fa9e4066Sahrens 	zs->zs_enospc_count = 0;
3188fa9e4066Sahrens 
3189fa9e4066Sahrens 	za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3190fa9e4066Sahrens 
3191fa9e4066Sahrens 	if (zopt_verbose >= 4)
3192fa9e4066Sahrens 		(void) printf("starting main threads...\n");
3193fa9e4066Sahrens 
31940a4e9518Sgw25295 	/* Let failures begin */
31950a4e9518Sgw25295 	zio_io_fail_shift = zopt_write_fail_shift;
31960a4e9518Sgw25295 
3197fa9e4066Sahrens 	za[0].za_start = gethrtime();
3198fa9e4066Sahrens 	za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3199fa9e4066Sahrens 	za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3200fa9e4066Sahrens 	za[0].za_kill = za[0].za_stop;
3201fa9e4066Sahrens 	if (ztest_random(100) < zopt_killrate)
3202fa9e4066Sahrens 		za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3203fa9e4066Sahrens 
3204fa9e4066Sahrens 	for (t = 0; t < zopt_threads; t++) {
3205ecc2d604Sbonwick 		d = t % zopt_datasets;
3206e05725b1Sbonwick 
3207e05725b1Sbonwick 		(void) strcpy(za[t].za_pool, pool);
3208e05725b1Sbonwick 		za[t].za_os = za[d].za_os;
3209e05725b1Sbonwick 		za[t].za_spa = spa;
3210e05725b1Sbonwick 		za[t].za_zilog = za[d].za_zilog;
3211e05725b1Sbonwick 		za[t].za_instance = t;
3212e05725b1Sbonwick 		za[t].za_random = ztest_random(-1ULL);
3213e05725b1Sbonwick 		za[t].za_start = za[0].za_start;
3214e05725b1Sbonwick 		za[t].za_stop = za[0].za_stop;
3215e05725b1Sbonwick 		za[t].za_kill = za[0].za_kill;
3216e05725b1Sbonwick 
3217ecc2d604Sbonwick 		if (t < zopt_datasets) {
3218fa9e4066Sahrens 			ztest_replay_t zr;
32194d31c452Smaybee 			int test_future = FALSE;
3220fa9e4066Sahrens 			(void) rw_rdlock(&ztest_shared->zs_name_lock);
3221fa9e4066Sahrens 			(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3222fa9e4066Sahrens 			error = dmu_objset_create(name, DMU_OST_OTHER, NULL,
3223fa9e4066Sahrens 			    ztest_create_cb, NULL);
32244d31c452Smaybee 			if (error == EEXIST) {
32254d31c452Smaybee 				test_future = TRUE;
3226e05725b1Sbonwick 			} else if (error == ENOSPC) {
3227fa9e4066Sahrens 				zs->zs_enospc_count++;
3228e05725b1Sbonwick 				(void) rw_unlock(&ztest_shared->zs_name_lock);
3229fa9e4066Sahrens 				break;
3230e05725b1Sbonwick 			} else if (error != 0) {
3231fa9e4066Sahrens 				fatal(0, "dmu_objset_create(%s) = %d",
3232fa9e4066Sahrens 				    name, error);
3233fa9e4066Sahrens 			}
3234fa9e4066Sahrens 			error = dmu_objset_open(name, DMU_OST_OTHER,
3235fa9e4066Sahrens 			    DS_MODE_STANDARD, &za[d].za_os);
3236fa9e4066Sahrens 			if (error)
3237fa9e4066Sahrens 				fatal(0, "dmu_objset_open('%s') = %d",
3238fa9e4066Sahrens 				    name, error);
3239fa9e4066Sahrens 			(void) rw_unlock(&ztest_shared->zs_name_lock);
3240e05725b1Sbonwick 			if (test_future)
3241e05725b1Sbonwick 				ztest_dmu_check_future_leak(&za[t]);
3242fa9e4066Sahrens 			zr.zr_os = za[d].za_os;
3243fa9e4066Sahrens 			zil_replay(zr.zr_os, &zr, &zr.zr_assign,
3244893a6d32Sahrens 			    ztest_replay_vector);
3245fa9e4066Sahrens 			za[d].za_zilog = zil_open(za[d].za_os, NULL);
3246fa9e4066Sahrens 		}
3247fa9e4066Sahrens 
3248fa9e4066Sahrens 		error = thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3249fa9e4066Sahrens 		    &za[t].za_thread);
3250fa9e4066Sahrens 		if (error)
3251fa9e4066Sahrens 			fatal(0, "can't create thread %d: error %d",
3252fa9e4066Sahrens 			    t, error);
3253fa9e4066Sahrens 	}
3254fa9e4066Sahrens 
3255fa9e4066Sahrens 	while (--t >= 0) {
3256fa9e4066Sahrens 		error = thr_join(za[t].za_thread, NULL, NULL);
3257fa9e4066Sahrens 		if (error)
3258fa9e4066Sahrens 			fatal(0, "thr_join(%d) = %d", t, error);
3259fa9e4066Sahrens 		if (za[t].za_th)
3260fa9e4066Sahrens 			traverse_fini(za[t].za_th);
3261ecc2d604Sbonwick 		if (t < zopt_datasets) {
3262fa9e4066Sahrens 			zil_close(za[t].za_zilog);
3263fa9e4066Sahrens 			dmu_objset_close(za[t].za_os);
3264fa9e4066Sahrens 		}
3265fa9e4066Sahrens 	}
3266fa9e4066Sahrens 
3267fa9e4066Sahrens 	if (zopt_verbose >= 3)
3268fa9e4066Sahrens 		show_pool_stats(spa);
3269fa9e4066Sahrens 
3270fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
3271fa9e4066Sahrens 
3272fa9e4066Sahrens 	zs->zs_alloc = spa_get_alloc(spa);
3273fa9e4066Sahrens 	zs->zs_space = spa_get_space(spa);
3274fa9e4066Sahrens 
3275fa9e4066Sahrens 	/*
3276e05725b1Sbonwick 	 * If we had out-of-space errors, destroy a random objset.
3277fa9e4066Sahrens 	 */
3278fa9e4066Sahrens 	if (zs->zs_enospc_count != 0) {
3279fa9e4066Sahrens 		(void) rw_rdlock(&ztest_shared->zs_name_lock);
3280e05725b1Sbonwick 		d = (int)ztest_random(zopt_datasets);
3281e05725b1Sbonwick 		(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3282fa9e4066Sahrens 		if (zopt_verbose >= 3)
3283fa9e4066Sahrens 			(void) printf("Destroying %s to free up space\n", name);
3284e05725b1Sbonwick 		(void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
32850b69c2f0Sahrens 		    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3286fa9e4066Sahrens 		(void) rw_unlock(&ztest_shared->zs_name_lock);
3287fa9e4066Sahrens 	}
3288fa9e4066Sahrens 
3289ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
3290fa9e4066Sahrens 
3291fa9e4066Sahrens 	/*
3292fa9e4066Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
3293fa9e4066Sahrens 	 * spa_close() should wait for it to complete.
3294fa9e4066Sahrens 	 */
3295fa9e4066Sahrens 	for (t = 1; t < 50; t++)
3296fa9e4066Sahrens 		dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3297fa9e4066Sahrens 
32980a4e9518Sgw25295 	/* Shutdown the suspend monitor thread */
32990a4e9518Sgw25295 	zio_io_fail_shift = 0;
33000a4e9518Sgw25295 	ztest_exiting = B_TRUE;
33010a4e9518Sgw25295 	mutex_enter(&spa->spa_zio_lock);
33020a4e9518Sgw25295 	cv_broadcast(&spa->spa_zio_cv);
33030a4e9518Sgw25295 	mutex_exit(&spa->spa_zio_lock);
33040a4e9518Sgw25295 	error = thr_join(tid, NULL, NULL);
33050a4e9518Sgw25295 	if (error)
33060a4e9518Sgw25295 		fatal(0, "thr_join(%d) = %d", tid, error);
33070a4e9518Sgw25295 
3308e05725b1Sbonwick 	umem_free(za, zopt_threads * sizeof (ztest_args_t));
3309e05725b1Sbonwick 
3310e05725b1Sbonwick 	spa_close(spa, FTAG);
3311e05725b1Sbonwick 
3312fa9e4066Sahrens 	kernel_fini();
3313fa9e4066Sahrens }
3314fa9e4066Sahrens 
3315fa9e4066Sahrens void
3316fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
3317fa9e4066Sahrens {
3318fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
3319fa9e4066Sahrens 	hrtime_t m = s / 60;
3320fa9e4066Sahrens 	hrtime_t h = m / 60;
3321fa9e4066Sahrens 	hrtime_t d = h / 24;
3322fa9e4066Sahrens 
3323fa9e4066Sahrens 	s -= m * 60;
3324fa9e4066Sahrens 	m -= h * 60;
3325fa9e4066Sahrens 	h -= d * 24;
3326fa9e4066Sahrens 
3327fa9e4066Sahrens 	timebuf[0] = '\0';
3328fa9e4066Sahrens 
3329fa9e4066Sahrens 	if (d)
3330fa9e4066Sahrens 		(void) sprintf(timebuf,
3331fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
3332fa9e4066Sahrens 	else if (h)
3333fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3334fa9e4066Sahrens 	else if (m)
3335fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
3336fa9e4066Sahrens 	else
3337fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
3338fa9e4066Sahrens }
3339fa9e4066Sahrens 
3340fa9e4066Sahrens /*
3341fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
3342fa9e4066Sahrens  * Then create the specified number of datasets in the pool.
3343fa9e4066Sahrens  */
3344fa9e4066Sahrens static void
3345fa9e4066Sahrens ztest_init(char *pool)
3346fa9e4066Sahrens {
3347fa9e4066Sahrens 	spa_t *spa;
3348fa9e4066Sahrens 	int error;
3349fa9e4066Sahrens 	nvlist_t *nvroot;
3350fa9e4066Sahrens 
3351fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3352fa9e4066Sahrens 
3353fa9e4066Sahrens 	/*
3354fa9e4066Sahrens 	 * Create the storage pool.
3355fa9e4066Sahrens 	 */
3356fa9e4066Sahrens 	(void) spa_destroy(pool);
3357fa9e4066Sahrens 	ztest_shared->zs_vdev_primaries = 0;
33588654d025Sperrin 	nvroot = make_vdev_root(zopt_vdev_size, 0, zopt_raidz, zopt_mirrors, 1);
3359228975ccSek110237 	error = spa_create(pool, nvroot, NULL, NULL);
3360fa9e4066Sahrens 	nvlist_free(nvroot);
3361fa9e4066Sahrens 
3362fa9e4066Sahrens 	if (error)
3363fa9e4066Sahrens 		fatal(0, "spa_create() = %d", error);
3364fa9e4066Sahrens 	error = spa_open(pool, &spa, FTAG);
3365fa9e4066Sahrens 	if (error)
3366fa9e4066Sahrens 		fatal(0, "spa_open() = %d", error);
3367fa9e4066Sahrens 
3368fa9e4066Sahrens 	if (zopt_verbose >= 3)
3369fa9e4066Sahrens 		show_pool_stats(spa);
3370fa9e4066Sahrens 
3371fa9e4066Sahrens 	spa_close(spa, FTAG);
3372fa9e4066Sahrens 
3373fa9e4066Sahrens 	kernel_fini();
3374fa9e4066Sahrens }
3375fa9e4066Sahrens 
3376fa9e4066Sahrens int
3377fa9e4066Sahrens main(int argc, char **argv)
3378fa9e4066Sahrens {
3379fa9e4066Sahrens 	int kills = 0;
3380fa9e4066Sahrens 	int iters = 0;
3381fa9e4066Sahrens 	int i, f;
3382fa9e4066Sahrens 	ztest_shared_t *zs;
3383fa9e4066Sahrens 	ztest_info_t *zi;
3384fa9e4066Sahrens 	char timebuf[100];
3385fa9e4066Sahrens 	char numbuf[6];
3386fa9e4066Sahrens 
3387fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
3388fa9e4066Sahrens 
3389fa9e4066Sahrens 	/* Override location of zpool.cache */
3390fa9e4066Sahrens 	spa_config_dir = "/tmp";
3391fa9e4066Sahrens 
3392fa9e4066Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
3393fa9e4066Sahrens 
3394fa9e4066Sahrens 	process_options(argc, argv);
3395fa9e4066Sahrens 
3396fa9e4066Sahrens 	argc -= optind;
3397fa9e4066Sahrens 	argv += optind;
3398fa9e4066Sahrens 
3399fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3400fa9e4066Sahrens 
3401ea8dc4b6Seschrock 	/*
3402ea8dc4b6Seschrock 	 * Blow away any existing copy of zpool.cache
3403ea8dc4b6Seschrock 	 */
3404ea8dc4b6Seschrock 	if (zopt_init != 0)
3405ea8dc4b6Seschrock 		(void) remove("/tmp/zpool.cache");
3406ea8dc4b6Seschrock 
3407fa9e4066Sahrens 	zs = ztest_shared = (void *)mmap(0,
3408fa9e4066Sahrens 	    P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3409fa9e4066Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3410fa9e4066Sahrens 
3411fa9e4066Sahrens 	if (zopt_verbose >= 1) {
3412fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
3413fa9e4066Sahrens 		    " %llu seconds...\n",
3414ecc2d604Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3415fa9e4066Sahrens 		    (u_longlong_t)zopt_time);
3416fa9e4066Sahrens 	}
3417fa9e4066Sahrens 
3418fa9e4066Sahrens 	/*
3419fa9e4066Sahrens 	 * Create and initialize our storage pool.
3420fa9e4066Sahrens 	 */
3421fa9e4066Sahrens 	for (i = 1; i <= zopt_init; i++) {
3422fa9e4066Sahrens 		bzero(zs, sizeof (ztest_shared_t));
3423fa9e4066Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
3424fa9e4066Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
3425fa9e4066Sahrens 		ztest_init(zopt_pool);
3426fa9e4066Sahrens 	}
3427fa9e4066Sahrens 
3428fa9e4066Sahrens 	/*
3429fa9e4066Sahrens 	 * Initialize the call targets for each function.
3430fa9e4066Sahrens 	 */
3431fa9e4066Sahrens 	for (f = 0; f < ZTEST_FUNCS; f++) {
3432fa9e4066Sahrens 		zi = &zs->zs_info[f];
3433fa9e4066Sahrens 
3434fa9e4066Sahrens 		*zi = ztest_info[f];
3435fa9e4066Sahrens 
3436fa9e4066Sahrens 		if (*zi->zi_interval == 0)
3437fa9e4066Sahrens 			zi->zi_call_target = UINT64_MAX;
3438fa9e4066Sahrens 		else
3439fa9e4066Sahrens 			zi->zi_call_target = zopt_time / *zi->zi_interval;
3440fa9e4066Sahrens 	}
3441fa9e4066Sahrens 
3442fa9e4066Sahrens 	zs->zs_start_time = gethrtime();
3443fa9e4066Sahrens 	zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3444fa9e4066Sahrens 
3445fa9e4066Sahrens 	/*
3446fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
3447fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
3448fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
3449fa9e4066Sahrens 	 */
3450fa9e4066Sahrens 	while (gethrtime() < zs->zs_stop_time) {
3451fa9e4066Sahrens 		int status;
3452fa9e4066Sahrens 		pid_t pid;
3453fa9e4066Sahrens 		char *tmp;
3454fa9e4066Sahrens 
3455fa9e4066Sahrens 		/*
3456fa9e4066Sahrens 		 * Initialize the workload counters for each function.
3457fa9e4066Sahrens 		 */
3458fa9e4066Sahrens 		for (f = 0; f < ZTEST_FUNCS; f++) {
3459fa9e4066Sahrens 			zi = &zs->zs_info[f];
3460fa9e4066Sahrens 			zi->zi_calls = 0;
3461fa9e4066Sahrens 			zi->zi_call_time = 0;
3462fa9e4066Sahrens 		}
3463fa9e4066Sahrens 
3464fa9e4066Sahrens 		pid = fork();
3465fa9e4066Sahrens 
3466fa9e4066Sahrens 		if (pid == -1)
3467fa9e4066Sahrens 			fatal(1, "fork failed");
3468fa9e4066Sahrens 
3469fa9e4066Sahrens 		if (pid == 0) {	/* child */
3470fa9e4066Sahrens 			struct rlimit rl = { 1024, 1024 };
3471fa9e4066Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
3472004388ebScasper 			(void) enable_extended_FILE_stdio(-1, -1);
3473fa9e4066Sahrens 			ztest_run(zopt_pool);
3474fa9e4066Sahrens 			exit(0);
3475fa9e4066Sahrens 		}
3476fa9e4066Sahrens 
34775ad82045Snd150628 		while (waitpid(pid, &status, 0) != pid)
3478fa9e4066Sahrens 			continue;
3479fa9e4066Sahrens 
3480fa9e4066Sahrens 		if (WIFEXITED(status)) {
3481fa9e4066Sahrens 			if (WEXITSTATUS(status) != 0) {
3482fa9e4066Sahrens 				(void) fprintf(stderr,
3483fa9e4066Sahrens 				    "child exited with code %d\n",
3484fa9e4066Sahrens 				    WEXITSTATUS(status));
3485fa9e4066Sahrens 				exit(2);
3486fa9e4066Sahrens 			}
34875ad82045Snd150628 		} else if (WIFSIGNALED(status)) {
3488fa9e4066Sahrens 			if (WTERMSIG(status) != SIGKILL) {
3489fa9e4066Sahrens 				(void) fprintf(stderr,
3490fa9e4066Sahrens 				    "child died with signal %d\n",
3491fa9e4066Sahrens 				    WTERMSIG(status));
3492fa9e4066Sahrens 				exit(3);
3493fa9e4066Sahrens 			}
3494fa9e4066Sahrens 			kills++;
34955ad82045Snd150628 		} else {
34965ad82045Snd150628 			(void) fprintf(stderr, "something strange happened "
34975ad82045Snd150628 			    "to child\n");
34985ad82045Snd150628 			exit(4);
3499fa9e4066Sahrens 		}
3500fa9e4066Sahrens 
3501fa9e4066Sahrens 		iters++;
3502fa9e4066Sahrens 
3503fa9e4066Sahrens 		if (zopt_verbose >= 1) {
3504fa9e4066Sahrens 			hrtime_t now = gethrtime();
3505fa9e4066Sahrens 
3506fa9e4066Sahrens 			now = MIN(now, zs->zs_stop_time);
3507fa9e4066Sahrens 			print_time(zs->zs_stop_time - now, timebuf);
3508fa9e4066Sahrens 			nicenum(zs->zs_space, numbuf);
3509fa9e4066Sahrens 
3510fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
3511fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
3512fa9e4066Sahrens 			    iters,
3513fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
3514fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
3515fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
3516fa9e4066Sahrens 			    numbuf,
3517fa9e4066Sahrens 			    100.0 * (now - zs->zs_start_time) /
3518fa9e4066Sahrens 			    (zopt_time * NANOSEC), timebuf);
3519fa9e4066Sahrens 		}
3520fa9e4066Sahrens 
3521fa9e4066Sahrens 		if (zopt_verbose >= 2) {
3522fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
3523fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
3524fa9e4066Sahrens 			    "Calls", "Time", "Function");
3525fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
3526fa9e4066Sahrens 			    "-----", "----", "--------");
3527fa9e4066Sahrens 			for (f = 0; f < ZTEST_FUNCS; f++) {
3528fa9e4066Sahrens 				Dl_info dli;
3529fa9e4066Sahrens 
3530fa9e4066Sahrens 				zi = &zs->zs_info[f];
3531fa9e4066Sahrens 				print_time(zi->zi_call_time, timebuf);
3532fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
3533fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
3534fa9e4066Sahrens 				    (u_longlong_t)zi->zi_calls, timebuf,
3535fa9e4066Sahrens 				    dli.dli_sname);
3536fa9e4066Sahrens 			}
3537fa9e4066Sahrens 			(void) printf("\n");
3538fa9e4066Sahrens 		}
3539fa9e4066Sahrens 
3540fa9e4066Sahrens 		/*
3541fa9e4066Sahrens 		 * It's possible that we killed a child during a rename test, in
3542fa9e4066Sahrens 		 * which case we'll have a 'ztest_tmp' pool lying around instead
3543fa9e4066Sahrens 		 * of 'ztest'.  Do a blind rename in case this happened.
3544fa9e4066Sahrens 		 */
3545fa9e4066Sahrens 		tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
3546fa9e4066Sahrens 		(void) strcpy(tmp, zopt_pool);
3547fa9e4066Sahrens 		(void) strcat(tmp, "_tmp");
3548fa9e4066Sahrens 		kernel_init(FREAD | FWRITE);
3549fa9e4066Sahrens 		(void) spa_rename(tmp, zopt_pool);
3550fa9e4066Sahrens 		kernel_fini();
3551fa9e4066Sahrens 		umem_free(tmp, strlen(tmp) + 1);
3552fa9e4066Sahrens 	}
3553fa9e4066Sahrens 
3554fa9e4066Sahrens 	ztest_verify_blocks(zopt_pool);
3555fa9e4066Sahrens 
3556fa9e4066Sahrens 	if (zopt_verbose >= 1) {
3557fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
3558fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
3559fa9e4066Sahrens 	}
3560fa9e4066Sahrens 
3561fa9e4066Sahrens 	return (0);
3562fa9e4066Sahrens }
3563