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