xref: /titanic_52/usr/src/cmd/ztest/ztest.c (revision 503ad85c168c7992ccc310af845a581cff3c72b5)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5441d80aaSlling  * Common Development and Distribution License (the "License").
6441d80aaSlling  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
224f5064b7SMark J Musante  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
27fa9e4066Sahrens  * The objective of this program is to provide a DMU/ZAP/SPA stress test
28fa9e4066Sahrens  * that runs entirely in userland, is easy to use, and easy to extend.
29fa9e4066Sahrens  *
30fa9e4066Sahrens  * The overall design of the ztest program is as follows:
31fa9e4066Sahrens  *
32fa9e4066Sahrens  * (1) For each major functional area (e.g. adding vdevs to a pool,
33fa9e4066Sahrens  *     creating and destroying datasets, reading and writing objects, etc)
34fa9e4066Sahrens  *     we have a simple routine to test that functionality.  These
35fa9e4066Sahrens  *     individual routines do not have to do anything "stressful".
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * (2) We turn these simple functionality tests into a stress test by
38fa9e4066Sahrens  *     running them all in parallel, with as many threads as desired,
39fa9e4066Sahrens  *     and spread across as many datasets, objects, and vdevs as desired.
40fa9e4066Sahrens  *
41fa9e4066Sahrens  * (3) While all this is happening, we inject faults into the pool to
42fa9e4066Sahrens  *     verify that self-healing data really works.
43fa9e4066Sahrens  *
44fa9e4066Sahrens  * (4) Every time we open a dataset, we change its checksum and compression
45fa9e4066Sahrens  *     functions.  Thus even individual objects vary from block to block
46fa9e4066Sahrens  *     in which checksum they use and whether they're compressed.
47fa9e4066Sahrens  *
48fa9e4066Sahrens  * (5) To verify that we never lose on-disk consistency after a crash,
49fa9e4066Sahrens  *     we run the entire test in a child of the main process.
50fa9e4066Sahrens  *     At random times, the child self-immolates with a SIGKILL.
51fa9e4066Sahrens  *     This is the software equivalent of pulling the power cord.
52fa9e4066Sahrens  *     The parent then runs the test again, using the existing
53fa9e4066Sahrens  *     storage pool, as many times as desired.
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * (6) To verify that we don't have future leaks or temporal incursions,
56fa9e4066Sahrens  *     many of the functional tests record the transaction group number
57fa9e4066Sahrens  *     as part of their data.  When reading old data, they verify that
58fa9e4066Sahrens  *     the transaction group number is less than the current, open txg.
59fa9e4066Sahrens  *     If you add a new test, please do this if applicable.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * When run with no arguments, ztest runs for about five minutes and
62fa9e4066Sahrens  * produces no output if successful.  To get a little bit of information,
63fa9e4066Sahrens  * specify -V.  To get more information, specify -VV, and so on.
64fa9e4066Sahrens  *
65fa9e4066Sahrens  * To turn this into an overnight stress test, use -T to specify run time.
66fa9e4066Sahrens  *
67fa9e4066Sahrens  * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68fa9e4066Sahrens  * to increase the pool capacity, fanout, and overall stress level.
69fa9e4066Sahrens  *
70fa9e4066Sahrens  * The -N(okill) option will suppress kills, so each child runs to completion.
71fa9e4066Sahrens  * This can be useful when you're trying to distinguish temporal incursions
72fa9e4066Sahrens  * from plain old race conditions.
73fa9e4066Sahrens  */
74fa9e4066Sahrens 
75fa9e4066Sahrens #include <sys/zfs_context.h>
76fa9e4066Sahrens #include <sys/spa.h>
77fa9e4066Sahrens #include <sys/dmu.h>
78fa9e4066Sahrens #include <sys/txg.h>
792fdbea25SAleksandr Guzovskiy #include <sys/dbuf.h>
80fa9e4066Sahrens #include <sys/zap.h>
81fa9e4066Sahrens #include <sys/dmu_objset.h>
82fa9e4066Sahrens #include <sys/poll.h>
83fa9e4066Sahrens #include <sys/stat.h>
84fa9e4066Sahrens #include <sys/time.h>
85fa9e4066Sahrens #include <sys/wait.h>
86fa9e4066Sahrens #include <sys/mman.h>
87fa9e4066Sahrens #include <sys/resource.h>
88fa9e4066Sahrens #include <sys/zio.h>
89fa9e4066Sahrens #include <sys/zio_checksum.h>
90fa9e4066Sahrens #include <sys/zio_compress.h>
91fa9e4066Sahrens #include <sys/zil.h>
92fa9e4066Sahrens #include <sys/vdev_impl.h>
93e14bb325SJeff Bonwick #include <sys/vdev_file.h>
94fa9e4066Sahrens #include <sys/spa_impl.h>
95fa9e4066Sahrens #include <sys/dsl_prop.h>
964f5064b7SMark J Musante #include <sys/dsl_dataset.h>
97fa9e4066Sahrens #include <sys/refcount.h>
98fa9e4066Sahrens #include <stdio.h>
99004388ebScasper #include <stdio_ext.h>
100fa9e4066Sahrens #include <stdlib.h>
101fa9e4066Sahrens #include <unistd.h>
102fa9e4066Sahrens #include <signal.h>
103fa9e4066Sahrens #include <umem.h>
104fa9e4066Sahrens #include <dlfcn.h>
105fa9e4066Sahrens #include <ctype.h>
106fa9e4066Sahrens #include <math.h>
107fa9e4066Sahrens #include <sys/fs/zfs.h>
108fa9e4066Sahrens 
109fa9e4066Sahrens static char cmdname[] = "ztest";
110fa9e4066Sahrens static char *zopt_pool = cmdname;
111fa9e4066Sahrens 
112fa9e4066Sahrens static uint64_t zopt_vdevs = 5;
113fa9e4066Sahrens static uint64_t zopt_vdevtime;
114ecc2d604Sbonwick static int zopt_ashift = SPA_MINBLOCKSHIFT;
115fa9e4066Sahrens static int zopt_mirrors = 2;
116fa9e4066Sahrens static int zopt_raidz = 4;
11799653d4eSeschrock static int zopt_raidz_parity = 1;
118fa9e4066Sahrens static size_t zopt_vdev_size = SPA_MINDEVSIZE;
119ecc2d604Sbonwick static int zopt_datasets = 7;
120fa9e4066Sahrens static int zopt_threads = 23;
121fa9e4066Sahrens static uint64_t zopt_passtime = 60;	/* 60 seconds */
122fa9e4066Sahrens static uint64_t zopt_killrate = 70;	/* 70% kill rate */
123fa9e4066Sahrens static int zopt_verbose = 0;
124fa9e4066Sahrens static int zopt_init = 1;
125fa9e4066Sahrens static char *zopt_dir = "/tmp";
126fa9e4066Sahrens static uint64_t zopt_time = 300;	/* 5 minutes */
127fa9e4066Sahrens static int zopt_maxfaults;
128fa9e4066Sahrens 
129e05725b1Sbonwick typedef struct ztest_block_tag {
130e05725b1Sbonwick 	uint64_t	bt_objset;
131e05725b1Sbonwick 	uint64_t	bt_object;
132e05725b1Sbonwick 	uint64_t	bt_offset;
133e05725b1Sbonwick 	uint64_t	bt_txg;
134e05725b1Sbonwick 	uint64_t	bt_thread;
135e05725b1Sbonwick 	uint64_t	bt_seq;
136e05725b1Sbonwick } ztest_block_tag_t;
137e05725b1Sbonwick 
138fa9e4066Sahrens typedef struct ztest_args {
139e05725b1Sbonwick 	char		za_pool[MAXNAMELEN];
140e05725b1Sbonwick 	spa_t		*za_spa;
141fa9e4066Sahrens 	objset_t	*za_os;
142fa9e4066Sahrens 	zilog_t		*za_zilog;
143fa9e4066Sahrens 	thread_t	za_thread;
144fa9e4066Sahrens 	uint64_t	za_instance;
145fa9e4066Sahrens 	uint64_t	za_random;
146fa9e4066Sahrens 	uint64_t	za_diroff;
147fa9e4066Sahrens 	uint64_t	za_diroff_shared;
148ea8dc4b6Seschrock 	uint64_t	za_zil_seq;
149fa9e4066Sahrens 	hrtime_t	za_start;
150fa9e4066Sahrens 	hrtime_t	za_stop;
151fa9e4066Sahrens 	hrtime_t	za_kill;
152e05725b1Sbonwick 	/*
153e05725b1Sbonwick 	 * Thread-local variables can go here to aid debugging.
154e05725b1Sbonwick 	 */
155e05725b1Sbonwick 	ztest_block_tag_t za_rbt;
156e05725b1Sbonwick 	ztest_block_tag_t za_wbt;
157e05725b1Sbonwick 	dmu_object_info_t za_doi;
158e05725b1Sbonwick 	dmu_buf_t	*za_dbuf;
159fa9e4066Sahrens } ztest_args_t;
160fa9e4066Sahrens 
161fa9e4066Sahrens typedef void ztest_func_t(ztest_args_t *);
162fa9e4066Sahrens 
163fa9e4066Sahrens /*
164fa9e4066Sahrens  * Note: these aren't static because we want dladdr() to work.
165fa9e4066Sahrens  */
166fa9e4066Sahrens ztest_func_t ztest_dmu_read_write;
1672fdbea25SAleksandr Guzovskiy ztest_func_t ztest_dmu_read_write_zcopy;
168fa9e4066Sahrens ztest_func_t ztest_dmu_write_parallel;
169fa9e4066Sahrens ztest_func_t ztest_dmu_object_alloc_free;
170fa9e4066Sahrens ztest_func_t ztest_zap;
171fa9e4066Sahrens ztest_func_t ztest_zap_parallel;
172fa9e4066Sahrens ztest_func_t ztest_traverse;
173fa9e4066Sahrens ztest_func_t ztest_dsl_prop_get_set;
174fa9e4066Sahrens ztest_func_t ztest_dmu_objset_create_destroy;
175fa9e4066Sahrens ztest_func_t ztest_dmu_snapshot_create_destroy;
1764f5064b7SMark J Musante ztest_func_t ztest_dsl_dataset_promote_busy;
177fa9e4066Sahrens ztest_func_t ztest_spa_create_destroy;
178fa9e4066Sahrens ztest_func_t ztest_fault_inject;
179e14bb325SJeff Bonwick ztest_func_t ztest_spa_rename;
180fa9e4066Sahrens ztest_func_t ztest_vdev_attach_detach;
181fa9e4066Sahrens ztest_func_t ztest_vdev_LUN_growth;
182fa9e4066Sahrens ztest_func_t ztest_vdev_add_remove;
183e14bb325SJeff Bonwick ztest_func_t ztest_vdev_aux_add_remove;
184fa9e4066Sahrens ztest_func_t ztest_scrub;
185fa9e4066Sahrens 
186fa9e4066Sahrens typedef struct ztest_info {
187fa9e4066Sahrens 	ztest_func_t	*zi_func;	/* test function */
188e05725b1Sbonwick 	uint64_t	zi_iters;	/* iterations per execution */
189fa9e4066Sahrens 	uint64_t	*zi_interval;	/* execute every <interval> seconds */
190fa9e4066Sahrens 	uint64_t	zi_calls;	/* per-pass count */
191fa9e4066Sahrens 	uint64_t	zi_call_time;	/* per-pass time */
192fa9e4066Sahrens 	uint64_t	zi_call_total;	/* cumulative total */
193fa9e4066Sahrens 	uint64_t	zi_call_target;	/* target cumulative total */
194fa9e4066Sahrens } ztest_info_t;
195fa9e4066Sahrens 
196fa9e4066Sahrens uint64_t zopt_always = 0;		/* all the time */
197fa9e4066Sahrens uint64_t zopt_often = 1;		/* every second */
198fa9e4066Sahrens uint64_t zopt_sometimes = 10;		/* every 10 seconds */
199fa9e4066Sahrens uint64_t zopt_rarely = 60;		/* every 60 seconds */
200fa9e4066Sahrens 
201fa9e4066Sahrens ztest_info_t ztest_info[] = {
202e05725b1Sbonwick 	{ ztest_dmu_read_write,			1,	&zopt_always	},
2032fdbea25SAleksandr Guzovskiy 	{ ztest_dmu_read_write_zcopy,		1,	&zopt_always	},
204e05725b1Sbonwick 	{ ztest_dmu_write_parallel,		30,	&zopt_always	},
205e05725b1Sbonwick 	{ ztest_dmu_object_alloc_free,		1,	&zopt_always	},
206e05725b1Sbonwick 	{ ztest_zap,				30,	&zopt_always	},
207e05725b1Sbonwick 	{ ztest_zap_parallel,			100,	&zopt_always	},
208e05725b1Sbonwick 	{ ztest_dsl_prop_get_set,		1,	&zopt_sometimes	},
209e05725b1Sbonwick 	{ ztest_dmu_objset_create_destroy,	1,	&zopt_sometimes },
210e14bb325SJeff Bonwick 	{ ztest_dmu_snapshot_create_destroy,	1,	&zopt_sometimes },
211e05725b1Sbonwick 	{ ztest_spa_create_destroy,		1,	&zopt_sometimes },
212e05725b1Sbonwick 	{ ztest_fault_inject,			1,	&zopt_sometimes	},
213e05725b1Sbonwick 	{ ztest_spa_rename,			1,	&zopt_rarely	},
214e05725b1Sbonwick 	{ ztest_vdev_attach_detach,		1,	&zopt_rarely	},
215e05725b1Sbonwick 	{ ztest_vdev_LUN_growth,		1,	&zopt_rarely	},
216b2d22b66SGeorge Wilson 	{ ztest_dsl_dataset_promote_busy,	1,	&zopt_rarely	},
217e05725b1Sbonwick 	{ ztest_vdev_add_remove,		1,	&zopt_vdevtime	},
218e14bb325SJeff Bonwick 	{ ztest_vdev_aux_add_remove,		1,	&zopt_vdevtime	},
219e05725b1Sbonwick 	{ ztest_scrub,				1,	&zopt_vdevtime	},
220fa9e4066Sahrens };
221fa9e4066Sahrens 
222fa9e4066Sahrens #define	ZTEST_FUNCS	(sizeof (ztest_info) / sizeof (ztest_info_t))
223fa9e4066Sahrens 
224fa9e4066Sahrens #define	ZTEST_SYNC_LOCKS	16
225fa9e4066Sahrens 
226fa9e4066Sahrens /*
227fa9e4066Sahrens  * Stuff we need to share writably between parent and child.
228fa9e4066Sahrens  */
229fa9e4066Sahrens typedef struct ztest_shared {
230fa9e4066Sahrens 	mutex_t		zs_vdev_lock;
231fa9e4066Sahrens 	rwlock_t	zs_name_lock;
232fa9e4066Sahrens 	uint64_t	zs_vdev_primaries;
233e14bb325SJeff Bonwick 	uint64_t	zs_vdev_aux;
234fa9e4066Sahrens 	uint64_t	zs_enospc_count;
235fa9e4066Sahrens 	hrtime_t	zs_start_time;
236fa9e4066Sahrens 	hrtime_t	zs_stop_time;
237fa9e4066Sahrens 	uint64_t	zs_alloc;
238fa9e4066Sahrens 	uint64_t	zs_space;
239fa9e4066Sahrens 	ztest_info_t	zs_info[ZTEST_FUNCS];
240fa9e4066Sahrens 	mutex_t		zs_sync_lock[ZTEST_SYNC_LOCKS];
241fa9e4066Sahrens 	uint64_t	zs_seq[ZTEST_SYNC_LOCKS];
242fa9e4066Sahrens } ztest_shared_t;
243fa9e4066Sahrens 
244fa9e4066Sahrens static char ztest_dev_template[] = "%s/%s.%llua";
245e14bb325SJeff Bonwick static char ztest_aux_template[] = "%s/%s.%s.%llu";
246fa9e4066Sahrens static ztest_shared_t *ztest_shared;
247fa9e4066Sahrens 
248fa9e4066Sahrens static int ztest_random_fd;
249fa9e4066Sahrens static int ztest_dump_core = 1;
250fa9e4066Sahrens 
251d6e555bdSGeorge Wilson static uint64_t metaslab_sz;
252e14bb325SJeff Bonwick static boolean_t ztest_exiting;
2530a4e9518Sgw25295 
254e05725b1Sbonwick extern uint64_t metaslab_gang_bang;
255d6e555bdSGeorge Wilson extern uint64_t metaslab_df_alloc_threshold;
256fa9e4066Sahrens 
257fa9e4066Sahrens #define	ZTEST_DIROBJ		1
258fa9e4066Sahrens #define	ZTEST_MICROZAP_OBJ	2
259fa9e4066Sahrens #define	ZTEST_FATZAP_OBJ	3
260fa9e4066Sahrens 
261fa9e4066Sahrens #define	ZTEST_DIROBJ_BLOCKSIZE	(1 << 10)
262fa9e4066Sahrens #define	ZTEST_DIRSIZE		256
263fa9e4066Sahrens 
2641ce825d8Sraf static void usage(boolean_t) __NORETURN;
265f1b4288bSvb160487 
266fa9e4066Sahrens /*
267fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
268fa9e4066Sahrens  * debugging facilities.
269fa9e4066Sahrens  */
270fa9e4066Sahrens const char *
271fa9e4066Sahrens _umem_debug_init()
272fa9e4066Sahrens {
273fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
274fa9e4066Sahrens }
275fa9e4066Sahrens 
276fa9e4066Sahrens const char *
277fa9e4066Sahrens _umem_logging_init(void)
278fa9e4066Sahrens {
279fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
280fa9e4066Sahrens }
281fa9e4066Sahrens 
282fa9e4066Sahrens #define	FATAL_MSG_SZ	1024
283fa9e4066Sahrens 
284fa9e4066Sahrens char *fatal_msg;
285fa9e4066Sahrens 
286fa9e4066Sahrens static void
287fa9e4066Sahrens fatal(int do_perror, char *message, ...)
288fa9e4066Sahrens {
289fa9e4066Sahrens 	va_list args;
290fa9e4066Sahrens 	int save_errno = errno;
291fa9e4066Sahrens 	char buf[FATAL_MSG_SZ];
292fa9e4066Sahrens 
293fa9e4066Sahrens 	(void) fflush(stdout);
294fa9e4066Sahrens 
295fa9e4066Sahrens 	va_start(args, message);
296fa9e4066Sahrens 	(void) sprintf(buf, "ztest: ");
297fa9e4066Sahrens 	/* LINTED */
298fa9e4066Sahrens 	(void) vsprintf(buf + strlen(buf), message, args);
299fa9e4066Sahrens 	va_end(args);
300fa9e4066Sahrens 	if (do_perror) {
301fa9e4066Sahrens 		(void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
302fa9e4066Sahrens 		    ": %s", strerror(save_errno));
303fa9e4066Sahrens 	}
304fa9e4066Sahrens 	(void) fprintf(stderr, "%s\n", buf);
305fa9e4066Sahrens 	fatal_msg = buf;			/* to ease debugging */
306fa9e4066Sahrens 	if (ztest_dump_core)
307fa9e4066Sahrens 		abort();
308fa9e4066Sahrens 	exit(3);
309fa9e4066Sahrens }
310fa9e4066Sahrens 
311fa9e4066Sahrens static int
312fa9e4066Sahrens str2shift(const char *buf)
313fa9e4066Sahrens {
314fa9e4066Sahrens 	const char *ends = "BKMGTPEZ";
315fa9e4066Sahrens 	int i;
316fa9e4066Sahrens 
317fa9e4066Sahrens 	if (buf[0] == '\0')
318fa9e4066Sahrens 		return (0);
319fa9e4066Sahrens 	for (i = 0; i < strlen(ends); i++) {
320fa9e4066Sahrens 		if (toupper(buf[0]) == ends[i])
321fa9e4066Sahrens 			break;
322fa9e4066Sahrens 	}
323f1b4288bSvb160487 	if (i == strlen(ends)) {
324f1b4288bSvb160487 		(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
325f1b4288bSvb160487 		    buf);
326f1b4288bSvb160487 		usage(B_FALSE);
327f1b4288bSvb160487 	}
328fa9e4066Sahrens 	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
329fa9e4066Sahrens 		return (10*i);
330fa9e4066Sahrens 	}
331f1b4288bSvb160487 	(void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
332f1b4288bSvb160487 	usage(B_FALSE);
333f1b4288bSvb160487 	/* NOTREACHED */
334fa9e4066Sahrens }
335fa9e4066Sahrens 
336fa9e4066Sahrens static uint64_t
337fa9e4066Sahrens nicenumtoull(const char *buf)
338fa9e4066Sahrens {
339fa9e4066Sahrens 	char *end;
340fa9e4066Sahrens 	uint64_t val;
341fa9e4066Sahrens 
342fa9e4066Sahrens 	val = strtoull(buf, &end, 0);
343fa9e4066Sahrens 	if (end == buf) {
344f1b4288bSvb160487 		(void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
345f1b4288bSvb160487 		usage(B_FALSE);
346fa9e4066Sahrens 	} else if (end[0] == '.') {
347fa9e4066Sahrens 		double fval = strtod(buf, &end);
348fa9e4066Sahrens 		fval *= pow(2, str2shift(end));
349f1b4288bSvb160487 		if (fval > UINT64_MAX) {
350f1b4288bSvb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
351f1b4288bSvb160487 			    buf);
352f1b4288bSvb160487 			usage(B_FALSE);
353f1b4288bSvb160487 		}
354fa9e4066Sahrens 		val = (uint64_t)fval;
355fa9e4066Sahrens 	} else {
356fa9e4066Sahrens 		int shift = str2shift(end);
357f1b4288bSvb160487 		if (shift >= 64 || (val << shift) >> shift != val) {
358f1b4288bSvb160487 			(void) fprintf(stderr, "ztest: value too large: %s\n",
359f1b4288bSvb160487 			    buf);
360f1b4288bSvb160487 			usage(B_FALSE);
361f1b4288bSvb160487 		}
362fa9e4066Sahrens 		val <<= shift;
363fa9e4066Sahrens 	}
364fa9e4066Sahrens 	return (val);
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens static void
368f1b4288bSvb160487 usage(boolean_t requested)
369fa9e4066Sahrens {
370fa9e4066Sahrens 	char nice_vdev_size[10];
371fa9e4066Sahrens 	char nice_gang_bang[10];
372f1b4288bSvb160487 	FILE *fp = requested ? stdout : stderr;
373fa9e4066Sahrens 
374fa9e4066Sahrens 	nicenum(zopt_vdev_size, nice_vdev_size);
375e05725b1Sbonwick 	nicenum(metaslab_gang_bang, nice_gang_bang);
376fa9e4066Sahrens 
377f1b4288bSvb160487 	(void) fprintf(fp, "Usage: %s\n"
378fa9e4066Sahrens 	    "\t[-v vdevs (default: %llu)]\n"
379fa9e4066Sahrens 	    "\t[-s size_of_each_vdev (default: %s)]\n"
380ecc2d604Sbonwick 	    "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
381fa9e4066Sahrens 	    "\t[-m mirror_copies (default: %d)]\n"
382fa9e4066Sahrens 	    "\t[-r raidz_disks (default: %d)]\n"
38399653d4eSeschrock 	    "\t[-R raidz_parity (default: %d)]\n"
384fa9e4066Sahrens 	    "\t[-d datasets (default: %d)]\n"
385fa9e4066Sahrens 	    "\t[-t threads (default: %d)]\n"
386fa9e4066Sahrens 	    "\t[-g gang_block_threshold (default: %s)]\n"
387fa9e4066Sahrens 	    "\t[-i initialize pool i times (default: %d)]\n"
388fa9e4066Sahrens 	    "\t[-k kill percentage (default: %llu%%)]\n"
389fa9e4066Sahrens 	    "\t[-p pool_name (default: %s)]\n"
390fa9e4066Sahrens 	    "\t[-f file directory for vdev files (default: %s)]\n"
391fa9e4066Sahrens 	    "\t[-V(erbose)] (use multiple times for ever more blather)\n"
392ecc2d604Sbonwick 	    "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
393fa9e4066Sahrens 	    "\t[-T time] total run time (default: %llu sec)\n"
394fa9e4066Sahrens 	    "\t[-P passtime] time per pass (default: %llu sec)\n"
395f1b4288bSvb160487 	    "\t[-h] (print help)\n"
396fa9e4066Sahrens 	    "",
397fa9e4066Sahrens 	    cmdname,
398fa9e4066Sahrens 	    (u_longlong_t)zopt_vdevs,			/* -v */
399fa9e4066Sahrens 	    nice_vdev_size,				/* -s */
400ecc2d604Sbonwick 	    zopt_ashift,				/* -a */
401fa9e4066Sahrens 	    zopt_mirrors,				/* -m */
402fa9e4066Sahrens 	    zopt_raidz,					/* -r */
40399653d4eSeschrock 	    zopt_raidz_parity,				/* -R */
404ecc2d604Sbonwick 	    zopt_datasets,				/* -d */
405fa9e4066Sahrens 	    zopt_threads,				/* -t */
406fa9e4066Sahrens 	    nice_gang_bang,				/* -g */
407fa9e4066Sahrens 	    zopt_init,					/* -i */
408fa9e4066Sahrens 	    (u_longlong_t)zopt_killrate,		/* -k */
409fa9e4066Sahrens 	    zopt_pool,					/* -p */
410fa9e4066Sahrens 	    zopt_dir,					/* -f */
411fa9e4066Sahrens 	    (u_longlong_t)zopt_time,			/* -T */
412e14bb325SJeff Bonwick 	    (u_longlong_t)zopt_passtime);		/* -P */
413f1b4288bSvb160487 	exit(requested ? 0 : 1);
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416fa9e4066Sahrens static uint64_t
417fa9e4066Sahrens ztest_random(uint64_t range)
418fa9e4066Sahrens {
419fa9e4066Sahrens 	uint64_t r;
420fa9e4066Sahrens 
421fa9e4066Sahrens 	if (range == 0)
422fa9e4066Sahrens 		return (0);
423fa9e4066Sahrens 
424fa9e4066Sahrens 	if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
425fa9e4066Sahrens 		fatal(1, "short read from /dev/urandom");
426fa9e4066Sahrens 
427fa9e4066Sahrens 	return (r % range);
428fa9e4066Sahrens }
429fa9e4066Sahrens 
4308ad4d6ddSJeff Bonwick /* ARGSUSED */
431fa9e4066Sahrens static void
432fa9e4066Sahrens ztest_record_enospc(char *s)
433fa9e4066Sahrens {
434fa9e4066Sahrens 	ztest_shared->zs_enospc_count++;
435fa9e4066Sahrens }
436fa9e4066Sahrens 
437fa9e4066Sahrens static void
438fa9e4066Sahrens process_options(int argc, char **argv)
439fa9e4066Sahrens {
440fa9e4066Sahrens 	int opt;
441fa9e4066Sahrens 	uint64_t value;
442fa9e4066Sahrens 
443fa9e4066Sahrens 	/* By default, test gang blocks for blocks 32K and greater */
444e05725b1Sbonwick 	metaslab_gang_bang = 32 << 10;
445fa9e4066Sahrens 
446fa9e4066Sahrens 	while ((opt = getopt(argc, argv,
447e14bb325SJeff Bonwick 	    "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
448fa9e4066Sahrens 		value = 0;
449fa9e4066Sahrens 		switch (opt) {
450fa9e4066Sahrens 		case 'v':
451fa9e4066Sahrens 		case 's':
452ecc2d604Sbonwick 		case 'a':
453fa9e4066Sahrens 		case 'm':
454fa9e4066Sahrens 		case 'r':
45599653d4eSeschrock 		case 'R':
456fa9e4066Sahrens 		case 'd':
457fa9e4066Sahrens 		case 't':
458fa9e4066Sahrens 		case 'g':
459fa9e4066Sahrens 		case 'i':
460fa9e4066Sahrens 		case 'k':
461fa9e4066Sahrens 		case 'T':
462fa9e4066Sahrens 		case 'P':
463fa9e4066Sahrens 			value = nicenumtoull(optarg);
464fa9e4066Sahrens 		}
465fa9e4066Sahrens 		switch (opt) {
466fa9e4066Sahrens 		case 'v':
467fa9e4066Sahrens 			zopt_vdevs = value;
468fa9e4066Sahrens 			break;
469fa9e4066Sahrens 		case 's':
470fa9e4066Sahrens 			zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
471fa9e4066Sahrens 			break;
472ecc2d604Sbonwick 		case 'a':
473ecc2d604Sbonwick 			zopt_ashift = value;
474ecc2d604Sbonwick 			break;
475fa9e4066Sahrens 		case 'm':
476fa9e4066Sahrens 			zopt_mirrors = value;
477fa9e4066Sahrens 			break;
478fa9e4066Sahrens 		case 'r':
479fa9e4066Sahrens 			zopt_raidz = MAX(1, value);
480fa9e4066Sahrens 			break;
48199653d4eSeschrock 		case 'R':
482f94275ceSAdam Leventhal 			zopt_raidz_parity = MIN(MAX(value, 1), 3);
48399653d4eSeschrock 			break;
484fa9e4066Sahrens 		case 'd':
485ecc2d604Sbonwick 			zopt_datasets = MAX(1, value);
486fa9e4066Sahrens 			break;
487fa9e4066Sahrens 		case 't':
488fa9e4066Sahrens 			zopt_threads = MAX(1, value);
489fa9e4066Sahrens 			break;
490fa9e4066Sahrens 		case 'g':
491e05725b1Sbonwick 			metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
492fa9e4066Sahrens 			break;
493fa9e4066Sahrens 		case 'i':
494fa9e4066Sahrens 			zopt_init = value;
495fa9e4066Sahrens 			break;
496fa9e4066Sahrens 		case 'k':
497fa9e4066Sahrens 			zopt_killrate = value;
498fa9e4066Sahrens 			break;
499fa9e4066Sahrens 		case 'p':
500fa9e4066Sahrens 			zopt_pool = strdup(optarg);
501fa9e4066Sahrens 			break;
502fa9e4066Sahrens 		case 'f':
503fa9e4066Sahrens 			zopt_dir = strdup(optarg);
504fa9e4066Sahrens 			break;
505fa9e4066Sahrens 		case 'V':
506fa9e4066Sahrens 			zopt_verbose++;
507fa9e4066Sahrens 			break;
508fa9e4066Sahrens 		case 'E':
509fa9e4066Sahrens 			zopt_init = 0;
510fa9e4066Sahrens 			break;
511fa9e4066Sahrens 		case 'T':
512fa9e4066Sahrens 			zopt_time = value;
513fa9e4066Sahrens 			break;
514fa9e4066Sahrens 		case 'P':
515fa9e4066Sahrens 			zopt_passtime = MAX(1, value);
516fa9e4066Sahrens 			break;
517f1b4288bSvb160487 		case 'h':
518f1b4288bSvb160487 			usage(B_TRUE);
519f1b4288bSvb160487 			break;
520fa9e4066Sahrens 		case '?':
521fa9e4066Sahrens 		default:
522f1b4288bSvb160487 			usage(B_FALSE);
523fa9e4066Sahrens 			break;
524fa9e4066Sahrens 		}
525fa9e4066Sahrens 	}
526fa9e4066Sahrens 
52799653d4eSeschrock 	zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
52899653d4eSeschrock 
529fa9e4066Sahrens 	zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
53099653d4eSeschrock 	zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
531fa9e4066Sahrens }
532fa9e4066Sahrens 
533ecc2d604Sbonwick static uint64_t
534ecc2d604Sbonwick ztest_get_ashift(void)
535ecc2d604Sbonwick {
536ecc2d604Sbonwick 	if (zopt_ashift == 0)
537ecc2d604Sbonwick 		return (SPA_MINBLOCKSHIFT + ztest_random(3));
538ecc2d604Sbonwick 	return (zopt_ashift);
539ecc2d604Sbonwick }
540ecc2d604Sbonwick 
541fa9e4066Sahrens static nvlist_t *
542e14bb325SJeff Bonwick make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
543fa9e4066Sahrens {
544e14bb325SJeff Bonwick 	char pathbuf[MAXPATHLEN];
545fa9e4066Sahrens 	uint64_t vdev;
546fa9e4066Sahrens 	nvlist_t *file;
547fa9e4066Sahrens 
548e14bb325SJeff Bonwick 	if (ashift == 0)
549e14bb325SJeff Bonwick 		ashift = ztest_get_ashift();
550e14bb325SJeff Bonwick 
551e14bb325SJeff Bonwick 	if (path == NULL) {
552e14bb325SJeff Bonwick 		path = pathbuf;
553e14bb325SJeff Bonwick 
554e14bb325SJeff Bonwick 		if (aux != NULL) {
555e14bb325SJeff Bonwick 			vdev = ztest_shared->zs_vdev_aux;
556e14bb325SJeff Bonwick 			(void) sprintf(path, ztest_aux_template,
557e14bb325SJeff Bonwick 			    zopt_dir, zopt_pool, aux, vdev);
558fa9e4066Sahrens 		} else {
559fa9e4066Sahrens 			vdev = ztest_shared->zs_vdev_primaries++;
560e14bb325SJeff Bonwick 			(void) sprintf(path, ztest_dev_template,
561fa9e4066Sahrens 			    zopt_dir, zopt_pool, vdev);
562e14bb325SJeff Bonwick 		}
563e14bb325SJeff Bonwick 	}
564fa9e4066Sahrens 
565e14bb325SJeff Bonwick 	if (size != 0) {
566e14bb325SJeff Bonwick 		int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
567fa9e4066Sahrens 		if (fd == -1)
568e14bb325SJeff Bonwick 			fatal(1, "can't open %s", path);
569fa9e4066Sahrens 		if (ftruncate(fd, size) != 0)
570e14bb325SJeff Bonwick 			fatal(1, "can't ftruncate %s", path);
571fa9e4066Sahrens 		(void) close(fd);
572fa9e4066Sahrens 	}
573fa9e4066Sahrens 
574fa9e4066Sahrens 	VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
575fa9e4066Sahrens 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
576e14bb325SJeff Bonwick 	VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
577ecc2d604Sbonwick 	VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
578fa9e4066Sahrens 
579fa9e4066Sahrens 	return (file);
580fa9e4066Sahrens }
581fa9e4066Sahrens 
582fa9e4066Sahrens static nvlist_t *
583e14bb325SJeff Bonwick make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
584fa9e4066Sahrens {
585fa9e4066Sahrens 	nvlist_t *raidz, **child;
586fa9e4066Sahrens 	int c;
587fa9e4066Sahrens 
588fa9e4066Sahrens 	if (r < 2)
589e14bb325SJeff Bonwick 		return (make_vdev_file(path, aux, size, ashift));
590fa9e4066Sahrens 	child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
591fa9e4066Sahrens 
592fa9e4066Sahrens 	for (c = 0; c < r; c++)
593e14bb325SJeff Bonwick 		child[c] = make_vdev_file(path, aux, size, ashift);
594fa9e4066Sahrens 
595fa9e4066Sahrens 	VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
596fa9e4066Sahrens 	VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
597fa9e4066Sahrens 	    VDEV_TYPE_RAIDZ) == 0);
59899653d4eSeschrock 	VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
59999653d4eSeschrock 	    zopt_raidz_parity) == 0);
600fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
601fa9e4066Sahrens 	    child, r) == 0);
602fa9e4066Sahrens 
603fa9e4066Sahrens 	for (c = 0; c < r; c++)
604fa9e4066Sahrens 		nvlist_free(child[c]);
605fa9e4066Sahrens 
606fa9e4066Sahrens 	umem_free(child, r * sizeof (nvlist_t *));
607fa9e4066Sahrens 
608fa9e4066Sahrens 	return (raidz);
609fa9e4066Sahrens }
610fa9e4066Sahrens 
611fa9e4066Sahrens static nvlist_t *
612e14bb325SJeff Bonwick make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
61331157203SJeff Bonwick 	int r, int m)
614fa9e4066Sahrens {
615fa9e4066Sahrens 	nvlist_t *mirror, **child;
616fa9e4066Sahrens 	int c;
617fa9e4066Sahrens 
618fa9e4066Sahrens 	if (m < 1)
619e14bb325SJeff Bonwick 		return (make_vdev_raidz(path, aux, size, ashift, r));
620fa9e4066Sahrens 
621fa9e4066Sahrens 	child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
622fa9e4066Sahrens 
623fa9e4066Sahrens 	for (c = 0; c < m; c++)
624e14bb325SJeff Bonwick 		child[c] = make_vdev_raidz(path, aux, size, ashift, r);
625fa9e4066Sahrens 
626fa9e4066Sahrens 	VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
627fa9e4066Sahrens 	VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
628fa9e4066Sahrens 	    VDEV_TYPE_MIRROR) == 0);
629fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
630fa9e4066Sahrens 	    child, m) == 0);
631fa9e4066Sahrens 
632fa9e4066Sahrens 	for (c = 0; c < m; c++)
633fa9e4066Sahrens 		nvlist_free(child[c]);
634fa9e4066Sahrens 
635fa9e4066Sahrens 	umem_free(child, m * sizeof (nvlist_t *));
636fa9e4066Sahrens 
637fa9e4066Sahrens 	return (mirror);
638fa9e4066Sahrens }
639fa9e4066Sahrens 
640fa9e4066Sahrens static nvlist_t *
641e14bb325SJeff Bonwick make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
642e14bb325SJeff Bonwick 	int log, int r, int m, int t)
643fa9e4066Sahrens {
644fa9e4066Sahrens 	nvlist_t *root, **child;
645fa9e4066Sahrens 	int c;
646fa9e4066Sahrens 
647fa9e4066Sahrens 	ASSERT(t > 0);
648fa9e4066Sahrens 
649fa9e4066Sahrens 	child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
650fa9e4066Sahrens 
65131157203SJeff Bonwick 	for (c = 0; c < t; c++) {
65231157203SJeff Bonwick 		child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
65331157203SJeff Bonwick 		VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
65431157203SJeff Bonwick 		    log) == 0);
65531157203SJeff Bonwick 	}
656fa9e4066Sahrens 
657fa9e4066Sahrens 	VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
658fa9e4066Sahrens 	VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
659e14bb325SJeff Bonwick 	VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
660fa9e4066Sahrens 	    child, t) == 0);
661fa9e4066Sahrens 
662fa9e4066Sahrens 	for (c = 0; c < t; c++)
663fa9e4066Sahrens 		nvlist_free(child[c]);
664fa9e4066Sahrens 
665fa9e4066Sahrens 	umem_free(child, t * sizeof (nvlist_t *));
666fa9e4066Sahrens 
667fa9e4066Sahrens 	return (root);
668fa9e4066Sahrens }
669fa9e4066Sahrens 
670fa9e4066Sahrens static void
671fa9e4066Sahrens ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
672fa9e4066Sahrens {
673fa9e4066Sahrens 	int bs = SPA_MINBLOCKSHIFT +
674fa9e4066Sahrens 	    ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
675fa9e4066Sahrens 	int ibs = DN_MIN_INDBLKSHIFT +
676fa9e4066Sahrens 	    ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
677fa9e4066Sahrens 	int error;
678fa9e4066Sahrens 
679fa9e4066Sahrens 	error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
680fa9e4066Sahrens 	if (error) {
681fa9e4066Sahrens 		char osname[300];
682fa9e4066Sahrens 		dmu_objset_name(os, osname);
683fa9e4066Sahrens 		fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
684fa9e4066Sahrens 		    osname, object, 1 << bs, ibs, error);
685fa9e4066Sahrens 	}
686fa9e4066Sahrens }
687fa9e4066Sahrens 
688fa9e4066Sahrens static uint8_t
689fa9e4066Sahrens ztest_random_checksum(void)
690fa9e4066Sahrens {
691fa9e4066Sahrens 	uint8_t checksum;
692fa9e4066Sahrens 
693fa9e4066Sahrens 	do {
694fa9e4066Sahrens 		checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
695fa9e4066Sahrens 	} while (zio_checksum_table[checksum].ci_zbt);
696fa9e4066Sahrens 
697fa9e4066Sahrens 	if (checksum == ZIO_CHECKSUM_OFF)
698fa9e4066Sahrens 		checksum = ZIO_CHECKSUM_ON;
699fa9e4066Sahrens 
700fa9e4066Sahrens 	return (checksum);
701fa9e4066Sahrens }
702fa9e4066Sahrens 
703fa9e4066Sahrens static uint8_t
704fa9e4066Sahrens ztest_random_compress(void)
705fa9e4066Sahrens {
706fa9e4066Sahrens 	return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
707fa9e4066Sahrens }
708fa9e4066Sahrens 
709fa9e4066Sahrens static int
7101209a471SNeil Perrin ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap)
711fa9e4066Sahrens {
712fa9e4066Sahrens 	dmu_tx_t *tx;
713fa9e4066Sahrens 	int error;
714fa9e4066Sahrens 
715fa9e4066Sahrens 	if (byteswap)
716fa9e4066Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
717fa9e4066Sahrens 
718fa9e4066Sahrens 	tx = dmu_tx_create(os);
719fa9e4066Sahrens 	dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
7201209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
721fa9e4066Sahrens 	if (error) {
722fa9e4066Sahrens 		dmu_tx_abort(tx);
723fa9e4066Sahrens 		return (error);
724fa9e4066Sahrens 	}
725fa9e4066Sahrens 
726fa9e4066Sahrens 	error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
727fa9e4066Sahrens 	    DMU_OT_NONE, 0, tx);
72899653d4eSeschrock 	ASSERT3U(error, ==, 0);
729fa9e4066Sahrens 	dmu_tx_commit(tx);
730fa9e4066Sahrens 
731fa9e4066Sahrens 	if (zopt_verbose >= 5) {
732fa9e4066Sahrens 		char osname[MAXNAMELEN];
733fa9e4066Sahrens 		dmu_objset_name(os, osname);
734fa9e4066Sahrens 		(void) printf("replay create of %s object %llu"
735fa9e4066Sahrens 		    " in txg %llu = %d\n",
736fa9e4066Sahrens 		    osname, (u_longlong_t)lr->lr_doid,
7371209a471SNeil Perrin 		    (u_longlong_t)dmu_tx_get_txg(tx), error);
738fa9e4066Sahrens 	}
739fa9e4066Sahrens 
740fa9e4066Sahrens 	return (error);
741fa9e4066Sahrens }
742fa9e4066Sahrens 
743fa9e4066Sahrens static int
7441209a471SNeil Perrin ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap)
745fa9e4066Sahrens {
746fa9e4066Sahrens 	dmu_tx_t *tx;
747fa9e4066Sahrens 	int error;
748fa9e4066Sahrens 
749fa9e4066Sahrens 	if (byteswap)
750fa9e4066Sahrens 		byteswap_uint64_array(lr, sizeof (*lr));
751fa9e4066Sahrens 
752fa9e4066Sahrens 	tx = dmu_tx_create(os);
753fa9e4066Sahrens 	dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
7541209a471SNeil Perrin 	error = dmu_tx_assign(tx, TXG_WAIT);
755fa9e4066Sahrens 	if (error) {
756fa9e4066Sahrens 		dmu_tx_abort(tx);
757fa9e4066Sahrens 		return (error);
758fa9e4066Sahrens 	}
759fa9e4066Sahrens 
760fa9e4066Sahrens 	error = dmu_object_free(os, lr->lr_doid, tx);
761fa9e4066Sahrens 	dmu_tx_commit(tx);
762fa9e4066Sahrens 
763fa9e4066Sahrens 	return (error);
764fa9e4066Sahrens }
765fa9e4066Sahrens 
766fa9e4066Sahrens zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
767fa9e4066Sahrens 	NULL,			/* 0 no such transaction type */
768fa9e4066Sahrens 	ztest_replay_create,	/* TX_CREATE */
769fa9e4066Sahrens 	NULL,			/* TX_MKDIR */
770fa9e4066Sahrens 	NULL,			/* TX_MKXATTR */
771fa9e4066Sahrens 	NULL,			/* TX_SYMLINK */
772fa9e4066Sahrens 	ztest_replay_remove,	/* TX_REMOVE */
773fa9e4066Sahrens 	NULL,			/* TX_RMDIR */
774fa9e4066Sahrens 	NULL,			/* TX_LINK */
775fa9e4066Sahrens 	NULL,			/* TX_RENAME */
776fa9e4066Sahrens 	NULL,			/* TX_WRITE */
777fa9e4066Sahrens 	NULL,			/* TX_TRUNCATE */
778fa9e4066Sahrens 	NULL,			/* TX_SETATTR */
779fa9e4066Sahrens 	NULL,			/* TX_ACL */
780fa9e4066Sahrens };
781fa9e4066Sahrens 
782fa9e4066Sahrens /*
783fa9e4066Sahrens  * Verify that we can't destroy an active pool, create an existing pool,
784fa9e4066Sahrens  * or create a pool with a bad vdev spec.
785fa9e4066Sahrens  */
786fa9e4066Sahrens void
787fa9e4066Sahrens ztest_spa_create_destroy(ztest_args_t *za)
788fa9e4066Sahrens {
789fa9e4066Sahrens 	int error;
790fa9e4066Sahrens 	spa_t *spa;
791fa9e4066Sahrens 	nvlist_t *nvroot;
792fa9e4066Sahrens 
793fa9e4066Sahrens 	/*
794fa9e4066Sahrens 	 * Attempt to create using a bad file.
795fa9e4066Sahrens 	 */
796e14bb325SJeff Bonwick 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
7970a48a24eStimh 	error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
798fa9e4066Sahrens 	nvlist_free(nvroot);
799fa9e4066Sahrens 	if (error != ENOENT)
800fa9e4066Sahrens 		fatal(0, "spa_create(bad_file) = %d", error);
801fa9e4066Sahrens 
802fa9e4066Sahrens 	/*
803fa9e4066Sahrens 	 * Attempt to create using a bad mirror.
804fa9e4066Sahrens 	 */
805e14bb325SJeff Bonwick 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
8060a48a24eStimh 	error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
807fa9e4066Sahrens 	nvlist_free(nvroot);
808fa9e4066Sahrens 	if (error != ENOENT)
809fa9e4066Sahrens 		fatal(0, "spa_create(bad_mirror) = %d", error);
810fa9e4066Sahrens 
811fa9e4066Sahrens 	/*
812fa9e4066Sahrens 	 * Attempt to create an existing pool.  It shouldn't matter
813fa9e4066Sahrens 	 * what's in the nvroot; we should fail with EEXIST.
814fa9e4066Sahrens 	 */
815fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
816e14bb325SJeff Bonwick 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
8170a48a24eStimh 	error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
818fa9e4066Sahrens 	nvlist_free(nvroot);
819fa9e4066Sahrens 	if (error != EEXIST)
820fa9e4066Sahrens 		fatal(0, "spa_create(whatever) = %d", error);
821fa9e4066Sahrens 
822fa9e4066Sahrens 	error = spa_open(za->za_pool, &spa, FTAG);
823fa9e4066Sahrens 	if (error)
824fa9e4066Sahrens 		fatal(0, "spa_open() = %d", error);
825fa9e4066Sahrens 
826fa9e4066Sahrens 	error = spa_destroy(za->za_pool);
827fa9e4066Sahrens 	if (error != EBUSY)
828fa9e4066Sahrens 		fatal(0, "spa_destroy() = %d", error);
829fa9e4066Sahrens 
830fa9e4066Sahrens 	spa_close(spa, FTAG);
831fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
832fa9e4066Sahrens }
833fa9e4066Sahrens 
83431157203SJeff Bonwick static vdev_t *
83531157203SJeff Bonwick vdev_lookup_by_path(vdev_t *vd, const char *path)
83631157203SJeff Bonwick {
83731157203SJeff Bonwick 	vdev_t *mvd;
83831157203SJeff Bonwick 
83931157203SJeff Bonwick 	if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
84031157203SJeff Bonwick 		return (vd);
84131157203SJeff Bonwick 
84231157203SJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
84331157203SJeff Bonwick 		if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
84431157203SJeff Bonwick 		    NULL)
84531157203SJeff Bonwick 			return (mvd);
84631157203SJeff Bonwick 
84731157203SJeff Bonwick 	return (NULL);
84831157203SJeff Bonwick }
84931157203SJeff Bonwick 
850fa9e4066Sahrens /*
851fa9e4066Sahrens  * Verify that vdev_add() works as expected.
852fa9e4066Sahrens  */
853fa9e4066Sahrens void
854fa9e4066Sahrens ztest_vdev_add_remove(ztest_args_t *za)
855fa9e4066Sahrens {
856e05725b1Sbonwick 	spa_t *spa = za->za_spa;
857fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
858fa9e4066Sahrens 	nvlist_t *nvroot;
859fa9e4066Sahrens 	int error;
860fa9e4066Sahrens 
861fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
862fa9e4066Sahrens 
863e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
864fa9e4066Sahrens 
865fa9e4066Sahrens 	ztest_shared->zs_vdev_primaries =
866fa9e4066Sahrens 	    spa->spa_root_vdev->vdev_children * leaves;
867fa9e4066Sahrens 
868e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
869fa9e4066Sahrens 
8708654d025Sperrin 	/*
8718654d025Sperrin 	 * Make 1/4 of the devices be log devices.
8728654d025Sperrin 	 */
873e14bb325SJeff Bonwick 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
8748654d025Sperrin 	    ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
8758654d025Sperrin 
876fa9e4066Sahrens 	error = spa_vdev_add(spa, nvroot);
877fa9e4066Sahrens 	nvlist_free(nvroot);
878fa9e4066Sahrens 
879fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
880fa9e4066Sahrens 
881fa9e4066Sahrens 	if (error == ENOSPC)
882fa9e4066Sahrens 		ztest_record_enospc("spa_vdev_add");
883fa9e4066Sahrens 	else if (error != 0)
884fa9e4066Sahrens 		fatal(0, "spa_vdev_add() = %d", error);
885e14bb325SJeff Bonwick }
886fa9e4066Sahrens 
887e14bb325SJeff Bonwick /*
888e14bb325SJeff Bonwick  * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
889e14bb325SJeff Bonwick  */
890e14bb325SJeff Bonwick void
891e14bb325SJeff Bonwick ztest_vdev_aux_add_remove(ztest_args_t *za)
892e14bb325SJeff Bonwick {
893e14bb325SJeff Bonwick 	spa_t *spa = za->za_spa;
89431157203SJeff Bonwick 	vdev_t *rvd = spa->spa_root_vdev;
895e14bb325SJeff Bonwick 	spa_aux_vdev_t *sav;
896e14bb325SJeff Bonwick 	char *aux;
897e14bb325SJeff Bonwick 	uint64_t guid = 0;
898e14bb325SJeff Bonwick 	int error;
899e14bb325SJeff Bonwick 
90031157203SJeff Bonwick 	if (ztest_random(2) == 0) {
901e14bb325SJeff Bonwick 		sav = &spa->spa_spares;
902e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_SPARES;
903e14bb325SJeff Bonwick 	} else {
904e14bb325SJeff Bonwick 		sav = &spa->spa_l2cache;
905e14bb325SJeff Bonwick 		aux = ZPOOL_CONFIG_L2CACHE;
906e14bb325SJeff Bonwick 	}
907e14bb325SJeff Bonwick 
908e14bb325SJeff Bonwick 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
909e14bb325SJeff Bonwick 
910e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
911e14bb325SJeff Bonwick 
912e14bb325SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(4) == 0) {
913e14bb325SJeff Bonwick 		/*
914e14bb325SJeff Bonwick 		 * Pick a random device to remove.
915e14bb325SJeff Bonwick 		 */
916e14bb325SJeff Bonwick 		guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
917e14bb325SJeff Bonwick 	} else {
918e14bb325SJeff Bonwick 		/*
919e14bb325SJeff Bonwick 		 * Find an unused device we can add.
920e14bb325SJeff Bonwick 		 */
921e14bb325SJeff Bonwick 		ztest_shared->zs_vdev_aux = 0;
922e14bb325SJeff Bonwick 		for (;;) {
923e14bb325SJeff Bonwick 			char path[MAXPATHLEN];
924e14bb325SJeff Bonwick 			int c;
925e14bb325SJeff Bonwick 			(void) sprintf(path, ztest_aux_template, zopt_dir,
926e14bb325SJeff Bonwick 			    zopt_pool, aux, ztest_shared->zs_vdev_aux);
927e14bb325SJeff Bonwick 			for (c = 0; c < sav->sav_count; c++)
928e14bb325SJeff Bonwick 				if (strcmp(sav->sav_vdevs[c]->vdev_path,
929e14bb325SJeff Bonwick 				    path) == 0)
930e14bb325SJeff Bonwick 					break;
93131157203SJeff Bonwick 			if (c == sav->sav_count &&
93231157203SJeff Bonwick 			    vdev_lookup_by_path(rvd, path) == NULL)
933e14bb325SJeff Bonwick 				break;
934e14bb325SJeff Bonwick 			ztest_shared->zs_vdev_aux++;
935e14bb325SJeff Bonwick 		}
936e14bb325SJeff Bonwick 	}
937e14bb325SJeff Bonwick 
938e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
939e14bb325SJeff Bonwick 
940e14bb325SJeff Bonwick 	if (guid == 0) {
941e14bb325SJeff Bonwick 		/*
942e14bb325SJeff Bonwick 		 * Add a new device.
943e14bb325SJeff Bonwick 		 */
94431157203SJeff Bonwick 		nvlist_t *nvroot = make_vdev_root(NULL, aux,
94531157203SJeff Bonwick 		    (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
946e14bb325SJeff Bonwick 		error = spa_vdev_add(spa, nvroot);
947e14bb325SJeff Bonwick 		if (error != 0)
948e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
949e14bb325SJeff Bonwick 		nvlist_free(nvroot);
950e14bb325SJeff Bonwick 	} else {
951e14bb325SJeff Bonwick 		/*
952e14bb325SJeff Bonwick 		 * Remove an existing device.  Sometimes, dirty its
953e14bb325SJeff Bonwick 		 * vdev state first to make sure we handle removal
954e14bb325SJeff Bonwick 		 * of devices that have pending state changes.
955e14bb325SJeff Bonwick 		 */
956e14bb325SJeff Bonwick 		if (ztest_random(2) == 0)
957573ca77eSGeorge Wilson 			(void) vdev_online(spa, guid, 0, NULL);
958e14bb325SJeff Bonwick 
959e14bb325SJeff Bonwick 		error = spa_vdev_remove(spa, guid, B_FALSE);
960e14bb325SJeff Bonwick 		if (error != 0 && error != EBUSY)
961e14bb325SJeff Bonwick 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
962e14bb325SJeff Bonwick 	}
963e14bb325SJeff Bonwick 
964e14bb325SJeff Bonwick 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
965fa9e4066Sahrens }
966fa9e4066Sahrens 
967fa9e4066Sahrens /*
968fa9e4066Sahrens  * Verify that we can attach and detach devices.
969fa9e4066Sahrens  */
970fa9e4066Sahrens void
971fa9e4066Sahrens ztest_vdev_attach_detach(ztest_args_t *za)
972fa9e4066Sahrens {
973e05725b1Sbonwick 	spa_t *spa = za->za_spa;
97431157203SJeff Bonwick 	spa_aux_vdev_t *sav = &spa->spa_spares;
975fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
976ea8dc4b6Seschrock 	vdev_t *oldvd, *newvd, *pvd;
977e14bb325SJeff Bonwick 	nvlist_t *root;
978fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
979fa9e4066Sahrens 	uint64_t leaf, top;
980ecc2d604Sbonwick 	uint64_t ashift = ztest_get_ashift();
9818ad4d6ddSJeff Bonwick 	uint64_t oldguid, pguid;
982ea8dc4b6Seschrock 	size_t oldsize, newsize;
983ea8dc4b6Seschrock 	char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
984fa9e4066Sahrens 	int replacing;
9859af0a4dfSJeff Bonwick 	int oldvd_has_siblings = B_FALSE;
98631157203SJeff Bonwick 	int newvd_is_spare = B_FALSE;
98731157203SJeff Bonwick 	int oldvd_is_log;
988fa9e4066Sahrens 	int error, expected_error;
989fa9e4066Sahrens 
990fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
991fa9e4066Sahrens 
992e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
993fa9e4066Sahrens 
994fa9e4066Sahrens 	/*
995fa9e4066Sahrens 	 * Decide whether to do an attach or a replace.
996fa9e4066Sahrens 	 */
997fa9e4066Sahrens 	replacing = ztest_random(2);
998fa9e4066Sahrens 
999fa9e4066Sahrens 	/*
1000fa9e4066Sahrens 	 * Pick a random top-level vdev.
1001fa9e4066Sahrens 	 */
1002fa9e4066Sahrens 	top = ztest_random(rvd->vdev_children);
1003fa9e4066Sahrens 
1004fa9e4066Sahrens 	/*
1005fa9e4066Sahrens 	 * Pick a random leaf within it.
1006fa9e4066Sahrens 	 */
1007fa9e4066Sahrens 	leaf = ztest_random(leaves);
1008fa9e4066Sahrens 
1009fa9e4066Sahrens 	/*
101031157203SJeff Bonwick 	 * Locate this vdev.
1011fa9e4066Sahrens 	 */
101231157203SJeff Bonwick 	oldvd = rvd->vdev_child[top];
10138ad4d6ddSJeff Bonwick 	if (zopt_mirrors >= 1) {
10148ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
10158ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= zopt_mirrors);
101631157203SJeff Bonwick 		oldvd = oldvd->vdev_child[leaf / zopt_raidz];
10178ad4d6ddSJeff Bonwick 	}
10188ad4d6ddSJeff Bonwick 	if (zopt_raidz > 1) {
10198ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
10208ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children == zopt_raidz);
102131157203SJeff Bonwick 		oldvd = oldvd->vdev_child[leaf % zopt_raidz];
10228ad4d6ddSJeff Bonwick 	}
1023fa9e4066Sahrens 
1024fa9e4066Sahrens 	/*
102531157203SJeff Bonwick 	 * If we're already doing an attach or replace, oldvd may be a
102631157203SJeff Bonwick 	 * mirror vdev -- in which case, pick a random child.
1027fa9e4066Sahrens 	 */
102831157203SJeff Bonwick 	while (oldvd->vdev_children != 0) {
10299af0a4dfSJeff Bonwick 		oldvd_has_siblings = B_TRUE;
10308ad4d6ddSJeff Bonwick 		ASSERT(oldvd->vdev_children >= 2);
10318ad4d6ddSJeff Bonwick 		oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
103231157203SJeff Bonwick 	}
1033fa9e4066Sahrens 
103431157203SJeff Bonwick 	oldguid = oldvd->vdev_guid;
1035573ca77eSGeorge Wilson 	oldsize = vdev_get_min_asize(oldvd);
103631157203SJeff Bonwick 	oldvd_is_log = oldvd->vdev_top->vdev_islog;
103731157203SJeff Bonwick 	(void) strcpy(oldpath, oldvd->vdev_path);
1038ea8dc4b6Seschrock 	pvd = oldvd->vdev_parent;
10398ad4d6ddSJeff Bonwick 	pguid = pvd->vdev_guid;
1040fa9e4066Sahrens 
1041fa9e4066Sahrens 	/*
10429af0a4dfSJeff Bonwick 	 * If oldvd has siblings, then half of the time, detach it.
10439af0a4dfSJeff Bonwick 	 */
10449af0a4dfSJeff Bonwick 	if (oldvd_has_siblings && ztest_random(2) == 0) {
10459af0a4dfSJeff Bonwick 		spa_config_exit(spa, SCL_VDEV, FTAG);
10468ad4d6ddSJeff Bonwick 		error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
10478ad4d6ddSJeff Bonwick 		if (error != 0 && error != ENODEV && error != EBUSY &&
10488ad4d6ddSJeff Bonwick 		    error != ENOTSUP)
10498ad4d6ddSJeff Bonwick 			fatal(0, "detach (%s) returned %d", oldpath, error);
10509af0a4dfSJeff Bonwick 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
10519af0a4dfSJeff Bonwick 		return;
10529af0a4dfSJeff Bonwick 	}
10539af0a4dfSJeff Bonwick 
10549af0a4dfSJeff Bonwick 	/*
105531157203SJeff Bonwick 	 * For the new vdev, choose with equal probability between the two
105631157203SJeff Bonwick 	 * standard paths (ending in either 'a' or 'b') or a random hot spare.
105731157203SJeff Bonwick 	 */
105831157203SJeff Bonwick 	if (sav->sav_count != 0 && ztest_random(3) == 0) {
105931157203SJeff Bonwick 		newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
106031157203SJeff Bonwick 		newvd_is_spare = B_TRUE;
106131157203SJeff Bonwick 		(void) strcpy(newpath, newvd->vdev_path);
106231157203SJeff Bonwick 	} else {
106331157203SJeff Bonwick 		(void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
106431157203SJeff Bonwick 		    zopt_dir, zopt_pool, top * leaves + leaf);
106531157203SJeff Bonwick 		if (ztest_random(2) == 0)
106631157203SJeff Bonwick 			newpath[strlen(newpath) - 1] = 'b';
106731157203SJeff Bonwick 		newvd = vdev_lookup_by_path(rvd, newpath);
106831157203SJeff Bonwick 	}
106931157203SJeff Bonwick 
107031157203SJeff Bonwick 	if (newvd) {
1071573ca77eSGeorge Wilson 		newsize = vdev_get_min_asize(newvd);
107231157203SJeff Bonwick 	} else {
107331157203SJeff Bonwick 		/*
1074ea8dc4b6Seschrock 		 * Make newsize a little bigger or smaller than oldsize.
1075fa9e4066Sahrens 		 * If it's smaller, the attach should fail.
1076fa9e4066Sahrens 		 * If it's larger, and we're doing a replace,
1077fa9e4066Sahrens 		 * we should get dynamic LUN growth when we're done.
1078fa9e4066Sahrens 		 */
1079ea8dc4b6Seschrock 		newsize = 10 * oldsize / (9 + ztest_random(3));
108031157203SJeff Bonwick 	}
1081fa9e4066Sahrens 
1082fa9e4066Sahrens 	/*
1083fa9e4066Sahrens 	 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1084fa9e4066Sahrens 	 * unless it's a replace; in that case any non-replacing parent is OK.
1085fa9e4066Sahrens 	 *
1086ea8dc4b6Seschrock 	 * If newvd is already part of the pool, it should fail with EBUSY.
1087fa9e4066Sahrens 	 *
1088ea8dc4b6Seschrock 	 * If newvd is too small, it should fail with EOVERFLOW.
1089fa9e4066Sahrens 	 */
109031157203SJeff Bonwick 	if (pvd->vdev_ops != &vdev_mirror_ops &&
109131157203SJeff Bonwick 	    pvd->vdev_ops != &vdev_root_ops && (!replacing ||
109231157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_replacing_ops ||
109331157203SJeff Bonwick 	    pvd->vdev_ops == &vdev_spare_ops))
1094fa9e4066Sahrens 		expected_error = ENOTSUP;
109531157203SJeff Bonwick 	else if (newvd_is_spare && (!replacing || oldvd_is_log))
109631157203SJeff Bonwick 		expected_error = ENOTSUP;
109731157203SJeff Bonwick 	else if (newvd == oldvd)
109831157203SJeff Bonwick 		expected_error = replacing ? 0 : EBUSY;
109931157203SJeff Bonwick 	else if (vdev_lookup_by_path(rvd, newpath) != NULL)
110031157203SJeff Bonwick 		expected_error = EBUSY;
1101ea8dc4b6Seschrock 	else if (newsize < oldsize)
1102fa9e4066Sahrens 		expected_error = EOVERFLOW;
1103ecc2d604Sbonwick 	else if (ashift > oldvd->vdev_top->vdev_ashift)
1104ecc2d604Sbonwick 		expected_error = EDOM;
1105fa9e4066Sahrens 	else
1106fa9e4066Sahrens 		expected_error = 0;
1107fa9e4066Sahrens 
1108e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
1109fa9e4066Sahrens 
1110fa9e4066Sahrens 	/*
1111ea8dc4b6Seschrock 	 * Build the nvlist describing newpath.
1112fa9e4066Sahrens 	 */
1113e14bb325SJeff Bonwick 	root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1114e14bb325SJeff Bonwick 	    ashift, 0, 0, 0, 1);
1115fa9e4066Sahrens 
111631157203SJeff Bonwick 	error = spa_vdev_attach(spa, oldguid, root, replacing);
1117fa9e4066Sahrens 
1118fa9e4066Sahrens 	nvlist_free(root);
1119fa9e4066Sahrens 
1120fa9e4066Sahrens 	/*
1121fa9e4066Sahrens 	 * If our parent was the replacing vdev, but the replace completed,
1122fa9e4066Sahrens 	 * then instead of failing with ENOTSUP we may either succeed,
1123fa9e4066Sahrens 	 * fail with ENODEV, or fail with EOVERFLOW.
1124fa9e4066Sahrens 	 */
1125fa9e4066Sahrens 	if (expected_error == ENOTSUP &&
1126fa9e4066Sahrens 	    (error == 0 || error == ENODEV || error == EOVERFLOW))
1127fa9e4066Sahrens 		expected_error = error;
1128fa9e4066Sahrens 
1129f0aa80d4Sbonwick 	/*
1130f0aa80d4Sbonwick 	 * If someone grew the LUN, the replacement may be too small.
1131f0aa80d4Sbonwick 	 */
1132088f3894Sahrens 	if (error == EOVERFLOW || error == EBUSY)
1133f0aa80d4Sbonwick 		expected_error = error;
1134f0aa80d4Sbonwick 
1135088f3894Sahrens 	/* XXX workaround 6690467 */
1136088f3894Sahrens 	if (error != expected_error && expected_error != EBUSY) {
1137088f3894Sahrens 		fatal(0, "attach (%s %llu, %s %llu, %d) "
1138088f3894Sahrens 		    "returned %d, expected %d",
1139088f3894Sahrens 		    oldpath, (longlong_t)oldsize, newpath,
1140088f3894Sahrens 		    (longlong_t)newsize, replacing, error, expected_error);
1141fa9e4066Sahrens 	}
1142fa9e4066Sahrens 
1143fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1144fa9e4066Sahrens }
1145fa9e4066Sahrens 
1146fa9e4066Sahrens /*
1147573ca77eSGeorge Wilson  * Callback function which expands the physical size of the vdev.
1148573ca77eSGeorge Wilson  */
1149573ca77eSGeorge Wilson vdev_t *
1150573ca77eSGeorge Wilson grow_vdev(vdev_t *vd, void *arg)
1151573ca77eSGeorge Wilson {
1152573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1153573ca77eSGeorge Wilson 	size_t *newsize = arg;
1154573ca77eSGeorge Wilson 	size_t fsize;
1155573ca77eSGeorge Wilson 	int fd;
1156573ca77eSGeorge Wilson 
1157573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
1158573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1159573ca77eSGeorge Wilson 
1160573ca77eSGeorge Wilson 	if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
1161573ca77eSGeorge Wilson 		return (vd);
1162573ca77eSGeorge Wilson 
1163573ca77eSGeorge Wilson 	fsize = lseek(fd, 0, SEEK_END);
1164573ca77eSGeorge Wilson 	(void) ftruncate(fd, *newsize);
1165573ca77eSGeorge Wilson 
1166573ca77eSGeorge Wilson 	if (zopt_verbose >= 6) {
1167573ca77eSGeorge Wilson 		(void) printf("%s grew from %lu to %lu bytes\n",
1168573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
1169573ca77eSGeorge Wilson 	}
1170573ca77eSGeorge Wilson 	(void) close(fd);
1171573ca77eSGeorge Wilson 	return (NULL);
1172573ca77eSGeorge Wilson }
1173573ca77eSGeorge Wilson 
1174573ca77eSGeorge Wilson /*
1175573ca77eSGeorge Wilson  * Callback function which expands a given vdev by calling vdev_online().
1176573ca77eSGeorge Wilson  */
1177573ca77eSGeorge Wilson /* ARGSUSED */
1178573ca77eSGeorge Wilson vdev_t *
1179573ca77eSGeorge Wilson online_vdev(vdev_t *vd, void *arg)
1180573ca77eSGeorge Wilson {
1181573ca77eSGeorge Wilson 	spa_t *spa = vd->vdev_spa;
1182573ca77eSGeorge Wilson 	vdev_t *tvd = vd->vdev_top;
1183573ca77eSGeorge Wilson 	vdev_t *pvd = vd->vdev_parent;
1184573ca77eSGeorge Wilson 	uint64_t guid = vd->vdev_guid;
1185573ca77eSGeorge Wilson 
1186573ca77eSGeorge Wilson 	ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
1187573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1188573ca77eSGeorge Wilson 
1189573ca77eSGeorge Wilson 	/* Calling vdev_online will initialize the new metaslabs */
1190573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
1191573ca77eSGeorge Wilson 	(void) vdev_online(spa, guid, ZFS_ONLINE_EXPAND, NULL);
1192573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1193573ca77eSGeorge Wilson 
1194573ca77eSGeorge Wilson 	/*
1195573ca77eSGeorge Wilson 	 * Since we dropped the lock we need to ensure that we're
1196573ca77eSGeorge Wilson 	 * still talking to the original vdev. It's possible this
1197573ca77eSGeorge Wilson 	 * vdev may have been detached/replaced while we were
1198573ca77eSGeorge Wilson 	 * trying to online it.
1199573ca77eSGeorge Wilson 	 */
1200573ca77eSGeorge Wilson 	if (vd != vdev_lookup_by_guid(tvd, guid) || vd->vdev_parent != pvd) {
1201573ca77eSGeorge Wilson 		if (zopt_verbose >= 6) {
1202573ca77eSGeorge Wilson 			(void) printf("vdev %p has disappeared, was "
1203573ca77eSGeorge Wilson 			    "guid %llu\n", (void *)vd, (u_longlong_t)guid);
1204573ca77eSGeorge Wilson 		}
1205573ca77eSGeorge Wilson 		return (vd);
1206573ca77eSGeorge Wilson 	}
1207573ca77eSGeorge Wilson 	return (NULL);
1208573ca77eSGeorge Wilson }
1209573ca77eSGeorge Wilson 
1210573ca77eSGeorge Wilson /*
1211573ca77eSGeorge Wilson  * Traverse the vdev tree calling the supplied function.
1212573ca77eSGeorge Wilson  * We continue to walk the tree until we either have walked all
1213573ca77eSGeorge Wilson  * children or we receive a non-NULL return from the callback.
1214573ca77eSGeorge Wilson  * If a NULL callback is passed, then we just return back the first
1215573ca77eSGeorge Wilson  * leaf vdev we encounter.
1216573ca77eSGeorge Wilson  */
1217573ca77eSGeorge Wilson vdev_t *
1218573ca77eSGeorge Wilson vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
1219573ca77eSGeorge Wilson {
1220573ca77eSGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
1221573ca77eSGeorge Wilson 		if (func == NULL)
1222573ca77eSGeorge Wilson 			return (vd);
1223573ca77eSGeorge Wilson 		else
1224573ca77eSGeorge Wilson 			return (func(vd, arg));
1225573ca77eSGeorge Wilson 	}
1226573ca77eSGeorge Wilson 
1227573ca77eSGeorge Wilson 	for (uint_t c = 0; c < vd->vdev_children; c++) {
1228573ca77eSGeorge Wilson 		vdev_t *cvd = vd->vdev_child[c];
1229573ca77eSGeorge Wilson 		if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
1230573ca77eSGeorge Wilson 			return (cvd);
1231573ca77eSGeorge Wilson 	}
1232573ca77eSGeorge Wilson 	return (NULL);
1233573ca77eSGeorge Wilson }
1234573ca77eSGeorge Wilson 
1235573ca77eSGeorge Wilson /*
1236fa9e4066Sahrens  * Verify that dynamic LUN growth works as expected.
1237fa9e4066Sahrens  */
1238fa9e4066Sahrens void
1239fa9e4066Sahrens ztest_vdev_LUN_growth(ztest_args_t *za)
1240fa9e4066Sahrens {
1241e05725b1Sbonwick 	spa_t *spa = za->za_spa;
1242573ca77eSGeorge Wilson 	vdev_t *vd, *tvd = NULL;
1243573ca77eSGeorge Wilson 	size_t psize, newsize;
1244573ca77eSGeorge Wilson 	uint64_t spa_newsize, spa_cursize, ms_count;
1245fa9e4066Sahrens 
1246fa9e4066Sahrens 	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
1247573ca77eSGeorge Wilson 	mutex_enter(&spa_namespace_lock);
1248573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1249573ca77eSGeorge Wilson 
1250573ca77eSGeorge Wilson 	while (tvd == NULL || tvd->vdev_islog) {
1251573ca77eSGeorge Wilson 		uint64_t vdev;
1252573ca77eSGeorge Wilson 
1253573ca77eSGeorge Wilson 		vdev = ztest_random(spa->spa_root_vdev->vdev_children);
1254573ca77eSGeorge Wilson 		tvd = spa->spa_root_vdev->vdev_child[vdev];
1255573ca77eSGeorge Wilson 	}
1256fa9e4066Sahrens 
1257fa9e4066Sahrens 	/*
1258573ca77eSGeorge Wilson 	 * Determine the size of the first leaf vdev associated with
1259573ca77eSGeorge Wilson 	 * our top-level device.
1260fa9e4066Sahrens 	 */
1261573ca77eSGeorge Wilson 	vd = vdev_walk_tree(tvd, NULL, NULL);
1262573ca77eSGeorge Wilson 	ASSERT3P(vd, !=, NULL);
1263573ca77eSGeorge Wilson 	ASSERT(vd->vdev_ops->vdev_op_leaf);
1264fa9e4066Sahrens 
1265573ca77eSGeorge Wilson 	psize = vd->vdev_psize;
1266fa9e4066Sahrens 
1267fa9e4066Sahrens 	/*
1268573ca77eSGeorge Wilson 	 * We only try to expand the vdev if it's less than 4x its
1269573ca77eSGeorge Wilson 	 * original size and it has a valid psize.
1270fa9e4066Sahrens 	 */
1271573ca77eSGeorge Wilson 	if (psize == 0 || psize >= 4 * zopt_vdev_size) {
1272573ca77eSGeorge Wilson 		spa_config_exit(spa, SCL_STATE, spa);
1273573ca77eSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1274573ca77eSGeorge Wilson 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1275573ca77eSGeorge Wilson 		return;
1276573ca77eSGeorge Wilson 	}
1277573ca77eSGeorge Wilson 	ASSERT(psize > 0);
1278573ca77eSGeorge Wilson 	newsize = psize + psize / 8;
1279573ca77eSGeorge Wilson 	ASSERT3U(newsize, >, psize);
1280573ca77eSGeorge Wilson 
1281fa9e4066Sahrens 	if (zopt_verbose >= 6) {
1282573ca77eSGeorge Wilson 		(void) printf("Expanding vdev %s from %lu to %lu\n",
1283573ca77eSGeorge Wilson 		    vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
1284fa9e4066Sahrens 	}
1285fa9e4066Sahrens 
1286573ca77eSGeorge Wilson 	spa_cursize = spa_get_space(spa);
1287573ca77eSGeorge Wilson 	ms_count = tvd->vdev_ms_count;
1288573ca77eSGeorge Wilson 
1289573ca77eSGeorge Wilson 	/*
1290573ca77eSGeorge Wilson 	 * Growing the vdev is a two step process:
1291573ca77eSGeorge Wilson 	 *	1). expand the physical size (i.e. relabel)
1292573ca77eSGeorge Wilson 	 *	2). online the vdev to create the new metaslabs
1293573ca77eSGeorge Wilson 	 */
1294573ca77eSGeorge Wilson 	if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
1295573ca77eSGeorge Wilson 	    vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
1296573ca77eSGeorge Wilson 	    tvd->vdev_state != VDEV_STATE_HEALTHY) {
1297573ca77eSGeorge Wilson 		if (zopt_verbose >= 5) {
1298573ca77eSGeorge Wilson 			(void) printf("Could not expand LUN because "
1299573ca77eSGeorge Wilson 			    "some vdevs were not healthy\n");
1300573ca77eSGeorge Wilson 		}
1301573ca77eSGeorge Wilson 		(void) spa_config_exit(spa, SCL_STATE, spa);
1302573ca77eSGeorge Wilson 		mutex_exit(&spa_namespace_lock);
1303573ca77eSGeorge Wilson 		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1304573ca77eSGeorge Wilson 		return;
1305573ca77eSGeorge Wilson 	}
1306573ca77eSGeorge Wilson 
1307573ca77eSGeorge Wilson 	(void) spa_config_exit(spa, SCL_STATE, spa);
1308573ca77eSGeorge Wilson 	mutex_exit(&spa_namespace_lock);
1309573ca77eSGeorge Wilson 
1310573ca77eSGeorge Wilson 	/*
1311573ca77eSGeorge Wilson 	 * Expanding the LUN will update the config asynchronously,
1312573ca77eSGeorge Wilson 	 * thus we must wait for the async thread to complete any
1313573ca77eSGeorge Wilson 	 * pending tasks before proceeding.
1314573ca77eSGeorge Wilson 	 */
1315573ca77eSGeorge Wilson 	mutex_enter(&spa->spa_async_lock);
1316573ca77eSGeorge Wilson 	while (spa->spa_async_thread != NULL || spa->spa_async_tasks)
1317573ca77eSGeorge Wilson 		cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
1318573ca77eSGeorge Wilson 	mutex_exit(&spa->spa_async_lock);
1319573ca77eSGeorge Wilson 
1320573ca77eSGeorge Wilson 	spa_config_enter(spa, SCL_STATE, spa, RW_READER);
1321573ca77eSGeorge Wilson 	spa_newsize = spa_get_space(spa);
1322573ca77eSGeorge Wilson 
1323573ca77eSGeorge Wilson 	/*
1324573ca77eSGeorge Wilson 	 * Make sure we were able to grow the pool.
1325573ca77eSGeorge Wilson 	 */
1326573ca77eSGeorge Wilson 	if (ms_count >= tvd->vdev_ms_count ||
1327573ca77eSGeorge Wilson 	    spa_cursize >= spa_newsize) {
1328573ca77eSGeorge Wilson 		(void) printf("Top-level vdev metaslab count: "
1329573ca77eSGeorge Wilson 		    "before %llu, after %llu\n",
1330573ca77eSGeorge Wilson 		    (u_longlong_t)ms_count,
1331573ca77eSGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
1332573ca77eSGeorge Wilson 		fatal(0, "LUN expansion failed: before %llu, "
1333573ca77eSGeorge Wilson 		    "after %llu\n", spa_cursize, spa_newsize);
1334573ca77eSGeorge Wilson 	} else if (zopt_verbose >= 5) {
1335573ca77eSGeorge Wilson 		char oldnumbuf[6], newnumbuf[6];
1336573ca77eSGeorge Wilson 
1337573ca77eSGeorge Wilson 		nicenum(spa_cursize, oldnumbuf);
1338573ca77eSGeorge Wilson 		nicenum(spa_newsize, newnumbuf);
1339573ca77eSGeorge Wilson 		(void) printf("%s grew from %s to %s\n",
1340573ca77eSGeorge Wilson 		    spa->spa_name, oldnumbuf, newnumbuf);
1341573ca77eSGeorge Wilson 	}
1342573ca77eSGeorge Wilson 	spa_config_exit(spa, SCL_STATE, spa);
1343fa9e4066Sahrens 	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1344fa9e4066Sahrens }
1345fa9e4066Sahrens 
1346fa9e4066Sahrens /* ARGSUSED */
1347fa9e4066Sahrens static void
1348ecd6cf80Smarks ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1349fa9e4066Sahrens {
1350fa9e4066Sahrens 	/*
1351fa9e4066Sahrens 	 * Create the directory object.
1352fa9e4066Sahrens 	 */
1353fa9e4066Sahrens 	VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1354fa9e4066Sahrens 	    DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1355e05725b1Sbonwick 	    DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1356fa9e4066Sahrens 
1357fa9e4066Sahrens 	VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1358fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1359fa9e4066Sahrens 
1360fa9e4066Sahrens 	VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1361fa9e4066Sahrens 	    DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1362fa9e4066Sahrens }
1363fa9e4066Sahrens 
13641d452cf5Sahrens static int
1365fa9e4066Sahrens ztest_destroy_cb(char *name, void *arg)
1366fa9e4066Sahrens {
1367e05725b1Sbonwick 	ztest_args_t *za = arg;
1368fa9e4066Sahrens 	objset_t *os;
1369e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
1370fa9e4066Sahrens 	int error;
1371fa9e4066Sahrens 
1372fa9e4066Sahrens 	/*
1373fa9e4066Sahrens 	 * Verify that the dataset contains a directory object.
1374fa9e4066Sahrens 	 */
1375*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
1376fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
1377e05725b1Sbonwick 	error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1378e1930233Sbonwick 	if (error != ENOENT) {
1379e1930233Sbonwick 		/* We could have crashed in the middle of destroying it */
1380fa9e4066Sahrens 		ASSERT3U(error, ==, 0);
1381e05725b1Sbonwick 		ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1382e05725b1Sbonwick 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1383e1930233Sbonwick 	}
1384*503ad85cSMatthew Ahrens 	dmu_objset_rele(os, FTAG);
1385fa9e4066Sahrens 
1386fa9e4066Sahrens 	/*
1387fa9e4066Sahrens 	 * Destroy the dataset.
1388fa9e4066Sahrens 	 */
1389842727c2SChris Kirby 	error = dmu_objset_destroy(name, B_FALSE);
1390745cd3c5Smaybee 	if (error) {
1391*503ad85cSMatthew Ahrens 		(void) dmu_objset_hold(name, FTAG, &os);
1392*503ad85cSMatthew Ahrens 		fatal(0, "dmu_objset_destroy(os=%p) = %d\n", os, error);
1393745cd3c5Smaybee 	}
13941d452cf5Sahrens 	return (0);
1395fa9e4066Sahrens }
1396fa9e4066Sahrens 
1397fa9e4066Sahrens /*
1398fa9e4066Sahrens  * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1399fa9e4066Sahrens  */
1400fa9e4066Sahrens static uint64_t
1401fa9e4066Sahrens ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1402fa9e4066Sahrens {
1403fa9e4066Sahrens 	itx_t *itx;
1404fa9e4066Sahrens 	lr_create_t *lr;
1405fa9e4066Sahrens 	size_t namesize;
1406fa9e4066Sahrens 	char name[24];
1407fa9e4066Sahrens 
1408fa9e4066Sahrens 	(void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1409fa9e4066Sahrens 	namesize = strlen(name) + 1;
1410fa9e4066Sahrens 
1411fa9e4066Sahrens 	itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1412fa9e4066Sahrens 	    ztest_random(ZIL_MAX_BLKSZ));
1413fa9e4066Sahrens 	lr = (lr_create_t *)&itx->itx_lr;
1414fa9e4066Sahrens 	bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1415fa9e4066Sahrens 	lr->lr_doid = object;
1416fa9e4066Sahrens 	lr->lr_foid = 0;
1417fa9e4066Sahrens 	lr->lr_mode = mode;
1418fa9e4066Sahrens 	lr->lr_uid = 0;
1419fa9e4066Sahrens 	lr->lr_gid = 0;
1420fa9e4066Sahrens 	lr->lr_gen = dmu_tx_get_txg(tx);
1421fa9e4066Sahrens 	lr->lr_crtime[0] = time(NULL);
1422fa9e4066Sahrens 	lr->lr_crtime[1] = 0;
1423fa9e4066Sahrens 	lr->lr_rdev = 0;
1424fa9e4066Sahrens 	bcopy(name, (char *)(lr + 1), namesize);
1425fa9e4066Sahrens 
1426fa9e4066Sahrens 	return (zil_itx_assign(zilog, itx, tx));
1427fa9e4066Sahrens }
1428fa9e4066Sahrens 
1429fa9e4066Sahrens void
1430fa9e4066Sahrens ztest_dmu_objset_create_destroy(ztest_args_t *za)
1431fa9e4066Sahrens {
1432fa9e4066Sahrens 	int error;
1433745cd3c5Smaybee 	objset_t *os, *os2;
1434fa9e4066Sahrens 	char name[100];
1435fa9e4066Sahrens 	zilog_t *zilog;
1436fa9e4066Sahrens 	uint64_t seq;
1437fa9e4066Sahrens 	uint64_t objects;
1438fa9e4066Sahrens 
1439fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1440fa9e4066Sahrens 	(void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1441fa9e4066Sahrens 	    (u_longlong_t)za->za_instance);
1442fa9e4066Sahrens 
1443fa9e4066Sahrens 	/*
1444fa9e4066Sahrens 	 * If this dataset exists from a previous run, process its replay log
1445fa9e4066Sahrens 	 * half of the time.  If we don't replay it, then dmu_objset_destroy()
1446fa9e4066Sahrens 	 * (invoked from ztest_destroy_cb() below) should just throw it away.
1447fa9e4066Sahrens 	 */
1448fa9e4066Sahrens 	if (ztest_random(2) == 0 &&
1449*503ad85cSMatthew Ahrens 	    dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
14501209a471SNeil Perrin 		zil_replay(os, os, ztest_replay_vector);
1451*503ad85cSMatthew Ahrens 		dmu_objset_disown(os, FTAG);
1452fa9e4066Sahrens 	}
1453fa9e4066Sahrens 
1454fa9e4066Sahrens 	/*
1455fa9e4066Sahrens 	 * There may be an old instance of the dataset we're about to
1456fa9e4066Sahrens 	 * create lying around from a previous run.  If so, destroy it
1457fa9e4066Sahrens 	 * and all of its snapshots.
1458fa9e4066Sahrens 	 */
1459e05725b1Sbonwick 	(void) dmu_objset_find(name, ztest_destroy_cb, za,
14600b69c2f0Sahrens 	    DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1461fa9e4066Sahrens 
1462fa9e4066Sahrens 	/*
1463fa9e4066Sahrens 	 * Verify that the destroyed dataset is no longer in the namespace.
1464fa9e4066Sahrens 	 */
1465*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
1466fa9e4066Sahrens 	if (error != ENOENT)
1467fa9e4066Sahrens 		fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1468fa9e4066Sahrens 		    name, os);
1469fa9e4066Sahrens 
1470fa9e4066Sahrens 	/*
1471fa9e4066Sahrens 	 * Verify that we can create a new dataset.
1472fa9e4066Sahrens 	 */
1473ae46e4c7SMatthew Ahrens 	error = dmu_objset_create(name, DMU_OST_OTHER, 0,
1474ab04eb8eStimh 	    ztest_create_cb, NULL);
1475fa9e4066Sahrens 	if (error) {
1476fa9e4066Sahrens 		if (error == ENOSPC) {
1477fa9e4066Sahrens 			ztest_record_enospc("dmu_objset_create");
1478fa9e4066Sahrens 			(void) rw_unlock(&ztest_shared->zs_name_lock);
1479fa9e4066Sahrens 			return;
1480fa9e4066Sahrens 		}
1481fa9e4066Sahrens 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
1482fa9e4066Sahrens 	}
1483fa9e4066Sahrens 
1484*503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os);
1485fa9e4066Sahrens 	if (error) {
1486fa9e4066Sahrens 		fatal(0, "dmu_objset_open(%s) = %d", name, error);
1487fa9e4066Sahrens 	}
1488fa9e4066Sahrens 
1489fa9e4066Sahrens 	/*
1490fa9e4066Sahrens 	 * Open the intent log for it.
1491fa9e4066Sahrens 	 */
1492fa9e4066Sahrens 	zilog = zil_open(os, NULL);
1493fa9e4066Sahrens 
1494fa9e4066Sahrens 	/*
1495fa9e4066Sahrens 	 * Put a random number of objects in there.
1496fa9e4066Sahrens 	 */
1497d80c45e0Sbonwick 	objects = ztest_random(20);
1498fa9e4066Sahrens 	seq = 0;
1499fa9e4066Sahrens 	while (objects-- != 0) {
1500fa9e4066Sahrens 		uint64_t object;
1501fa9e4066Sahrens 		dmu_tx_t *tx = dmu_tx_create(os);
1502fa9e4066Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1503fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1504fa9e4066Sahrens 		if (error) {
1505fa9e4066Sahrens 			dmu_tx_abort(tx);
1506fa9e4066Sahrens 		} else {
1507fa9e4066Sahrens 			object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1508fa9e4066Sahrens 			    DMU_OT_NONE, 0, tx);
1509fa9e4066Sahrens 			ztest_set_random_blocksize(os, object, tx);
1510fa9e4066Sahrens 			seq = ztest_log_create(zilog, tx, object,
1511fa9e4066Sahrens 			    DMU_OT_UINT64_OTHER);
1512fa9e4066Sahrens 			dmu_write(os, object, 0, sizeof (name), name, tx);
1513fa9e4066Sahrens 			dmu_tx_commit(tx);
1514fa9e4066Sahrens 		}
1515fa9e4066Sahrens 		if (ztest_random(5) == 0) {
1516b19a79ecSperrin 			zil_commit(zilog, seq, object);
1517fa9e4066Sahrens 		}
1518d80c45e0Sbonwick 		if (ztest_random(100) == 0) {
1519fa9e4066Sahrens 			error = zil_suspend(zilog);
1520fa9e4066Sahrens 			if (error == 0) {
1521fa9e4066Sahrens 				zil_resume(zilog);
1522fa9e4066Sahrens 			}
1523fa9e4066Sahrens 		}
1524fa9e4066Sahrens 	}
1525fa9e4066Sahrens 
1526fa9e4066Sahrens 	/*
1527fa9e4066Sahrens 	 * Verify that we cannot create an existing dataset.
1528fa9e4066Sahrens 	 */
1529ae46e4c7SMatthew Ahrens 	error = dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL);
1530fa9e4066Sahrens 	if (error != EEXIST)
1531fa9e4066Sahrens 		fatal(0, "created existing dataset, error = %d", error);
1532fa9e4066Sahrens 
1533fa9e4066Sahrens 	/*
1534*503ad85cSMatthew Ahrens 	 * Verify that we can hold an objset that is also owned.
1535fa9e4066Sahrens 	 */
1536*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os2);
1537745cd3c5Smaybee 	if (error)
1538745cd3c5Smaybee 		fatal(0, "dmu_objset_open('%s') = %d", name, error);
1539*503ad85cSMatthew Ahrens 	dmu_objset_rele(os2, FTAG);
1540*503ad85cSMatthew Ahrens 
1541*503ad85cSMatthew Ahrens 	/*
1542*503ad85cSMatthew Ahrens 	 * Verify that we can not own an objset that is already owned.
1543*503ad85cSMatthew Ahrens 	 */
1544*503ad85cSMatthew Ahrens 	error = dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2);
1545*503ad85cSMatthew Ahrens 	if (error != EBUSY)
1546*503ad85cSMatthew Ahrens 		fatal(0, "dmu_objset_open('%s') = %d, expected EBUSY",
1547*503ad85cSMatthew Ahrens 		    name, error);
1548fa9e4066Sahrens 
1549fa9e4066Sahrens 	zil_close(zilog);
1550*503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
1551fa9e4066Sahrens 
1552842727c2SChris Kirby 	error = dmu_objset_destroy(name, B_FALSE);
1553fa9e4066Sahrens 	if (error)
1554fa9e4066Sahrens 		fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
1559fa9e4066Sahrens /*
1560fa9e4066Sahrens  * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1561fa9e4066Sahrens  */
1562fa9e4066Sahrens void
1563fa9e4066Sahrens ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1564fa9e4066Sahrens {
1565fa9e4066Sahrens 	int error;
1566fa9e4066Sahrens 	objset_t *os = za->za_os;
1567fa9e4066Sahrens 	char snapname[100];
1568fa9e4066Sahrens 	char osname[MAXNAMELEN];
1569fa9e4066Sahrens 
1570fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
1571fa9e4066Sahrens 	dmu_objset_name(os, osname);
1572fa9e4066Sahrens 	(void) snprintf(snapname, 100, "%s@%llu", osname,
1573fa9e4066Sahrens 	    (u_longlong_t)za->za_instance);
1574fa9e4066Sahrens 
1575842727c2SChris Kirby 	error = dmu_objset_destroy(snapname, B_FALSE);
1576fa9e4066Sahrens 	if (error != 0 && error != ENOENT)
1577fa9e4066Sahrens 		fatal(0, "dmu_objset_destroy() = %d", error);
1578ea2f5b9eSMatthew Ahrens 	error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1,
1579ea2f5b9eSMatthew Ahrens 	    NULL, FALSE);
1580fa9e4066Sahrens 	if (error == ENOSPC)
1581fa9e4066Sahrens 		ztest_record_enospc("dmu_take_snapshot");
1582fa9e4066Sahrens 	else if (error != 0 && error != EEXIST)
1583fa9e4066Sahrens 		fatal(0, "dmu_take_snapshot() = %d", error);
1584fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
1585fa9e4066Sahrens }
1586fa9e4066Sahrens 
1587fa9e4066Sahrens /*
1588718d718aSGeorge Wilson  * Cleanup non-standard snapshots and clones.
1589718d718aSGeorge Wilson  */
1590718d718aSGeorge Wilson void
1591718d718aSGeorge Wilson ztest_dsl_dataset_cleanup(char *osname, uint64_t curval)
1592718d718aSGeorge Wilson {
1593718d718aSGeorge Wilson 	char snap1name[100];
1594718d718aSGeorge Wilson 	char clone1name[100];
1595718d718aSGeorge Wilson 	char snap2name[100];
1596718d718aSGeorge Wilson 	char clone2name[100];
1597718d718aSGeorge Wilson 	char snap3name[100];
1598718d718aSGeorge Wilson 	int error;
1599718d718aSGeorge Wilson 
1600718d718aSGeorge Wilson 	(void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval);
1601718d718aSGeorge Wilson 	(void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval);
1602718d718aSGeorge Wilson 	(void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval);
1603718d718aSGeorge Wilson 	(void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval);
1604718d718aSGeorge Wilson 	(void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval);
1605718d718aSGeorge Wilson 
1606842727c2SChris Kirby 	error = dmu_objset_destroy(clone2name, B_FALSE);
1607718d718aSGeorge Wilson 	if (error && error != ENOENT)
1608718d718aSGeorge Wilson 		fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
1609842727c2SChris Kirby 	error = dmu_objset_destroy(snap3name, B_FALSE);
1610718d718aSGeorge Wilson 	if (error && error != ENOENT)
1611718d718aSGeorge Wilson 		fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
1612842727c2SChris Kirby 	error = dmu_objset_destroy(snap2name, B_FALSE);
1613718d718aSGeorge Wilson 	if (error && error != ENOENT)
1614718d718aSGeorge Wilson 		fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
1615842727c2SChris Kirby 	error = dmu_objset_destroy(clone1name, B_FALSE);
1616718d718aSGeorge Wilson 	if (error && error != ENOENT)
1617718d718aSGeorge Wilson 		fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
1618842727c2SChris Kirby 	error = dmu_objset_destroy(snap1name, B_FALSE);
1619718d718aSGeorge Wilson 	if (error && error != ENOENT)
1620718d718aSGeorge Wilson 		fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
1621718d718aSGeorge Wilson }
1622718d718aSGeorge Wilson 
1623718d718aSGeorge Wilson /*
16244f5064b7SMark J Musante  * Verify dsl_dataset_promote handles EBUSY
16254f5064b7SMark J Musante  */
16264f5064b7SMark J Musante void
16274f5064b7SMark J Musante ztest_dsl_dataset_promote_busy(ztest_args_t *za)
16284f5064b7SMark J Musante {
16294f5064b7SMark J Musante 	int error;
16304f5064b7SMark J Musante 	objset_t *os = za->za_os;
16314f5064b7SMark J Musante 	objset_t *clone;
16324f5064b7SMark J Musante 	dsl_dataset_t *ds;
16334f5064b7SMark J Musante 	char snap1name[100];
16344f5064b7SMark J Musante 	char clone1name[100];
16354f5064b7SMark J Musante 	char snap2name[100];
16364f5064b7SMark J Musante 	char clone2name[100];
16374f5064b7SMark J Musante 	char snap3name[100];
16384f5064b7SMark J Musante 	char osname[MAXNAMELEN];
1639718d718aSGeorge Wilson 	uint64_t curval = za->za_instance;
16404f5064b7SMark J Musante 
16414f5064b7SMark J Musante 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
16424f5064b7SMark J Musante 
16434f5064b7SMark J Musante 	dmu_objset_name(os, osname);
1644718d718aSGeorge Wilson 	ztest_dsl_dataset_cleanup(osname, curval);
1645718d718aSGeorge Wilson 
164605312e2cSMark J Musante 	(void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval);
164705312e2cSMark J Musante 	(void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval);
164805312e2cSMark J Musante 	(void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval);
164905312e2cSMark J Musante 	(void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval);
165005312e2cSMark J Musante 	(void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval);
165105312e2cSMark J Musante 
1652ea2f5b9eSMatthew Ahrens 	error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
1653ea2f5b9eSMatthew Ahrens 	    NULL, FALSE);
1654b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
1655b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
16564f5064b7SMark J Musante 			ztest_record_enospc("dmu_take_snapshot");
1657b2d22b66SGeorge Wilson 			goto out;
1658b2d22b66SGeorge Wilson 		}
1659b2d22b66SGeorge Wilson 		fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
1660b2d22b66SGeorge Wilson 	}
16614f5064b7SMark J Musante 
1662*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(snap1name, FTAG, &clone);
16634f5064b7SMark J Musante 	if (error)
16644f5064b7SMark J Musante 		fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
16654f5064b7SMark J Musante 
1666ae46e4c7SMatthew Ahrens 	error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
1667*503ad85cSMatthew Ahrens 	dmu_objset_rele(clone, FTAG);
1668b2d22b66SGeorge Wilson 	if (error) {
1669b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
1670b2d22b66SGeorge Wilson 			ztest_record_enospc("dmu_objset_create");
1671b2d22b66SGeorge Wilson 			goto out;
1672b2d22b66SGeorge Wilson 		}
1673b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
1674b2d22b66SGeorge Wilson 	}
16754f5064b7SMark J Musante 
16764f5064b7SMark J Musante 	error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
1677ea2f5b9eSMatthew Ahrens 	    NULL, FALSE);
1678b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
1679b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
16804f5064b7SMark J Musante 			ztest_record_enospc("dmu_take_snapshot");
1681b2d22b66SGeorge Wilson 			goto out;
1682b2d22b66SGeorge Wilson 		}
1683b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
1684b2d22b66SGeorge Wilson 	}
16854f5064b7SMark J Musante 
16864f5064b7SMark J Musante 	error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
1687ea2f5b9eSMatthew Ahrens 	    NULL, FALSE);
1688b2d22b66SGeorge Wilson 	if (error && error != EEXIST) {
1689b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
16904f5064b7SMark J Musante 			ztest_record_enospc("dmu_take_snapshot");
1691b2d22b66SGeorge Wilson 			goto out;
1692b2d22b66SGeorge Wilson 		}
1693b2d22b66SGeorge Wilson 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
1694b2d22b66SGeorge Wilson 	}
16954f5064b7SMark J Musante 
1696*503ad85cSMatthew Ahrens 	error = dmu_objset_hold(snap3name, FTAG, &clone);
16974f5064b7SMark J Musante 	if (error)
16984f5064b7SMark J Musante 		fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
16994f5064b7SMark J Musante 
1700ae46e4c7SMatthew Ahrens 	error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
1701*503ad85cSMatthew Ahrens 	dmu_objset_rele(clone, FTAG);
1702b2d22b66SGeorge Wilson 	if (error) {
1703b2d22b66SGeorge Wilson 		if (error == ENOSPC) {
1704b2d22b66SGeorge Wilson 			ztest_record_enospc("dmu_objset_create");
1705b2d22b66SGeorge Wilson 			goto out;
1706b2d22b66SGeorge Wilson 		}
1707b2d22b66SGeorge Wilson 		fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
1708b2d22b66SGeorge Wilson 	}
17094f5064b7SMark J Musante 
1710*503ad85cSMatthew Ahrens 	error = dsl_dataset_own(snap1name, B_FALSE, FTAG, &ds);
17114f5064b7SMark J Musante 	if (error)
17124f5064b7SMark J Musante 		fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
17134f5064b7SMark J Musante 	error = dsl_dataset_promote(clone2name);
17144f5064b7SMark J Musante 	if (error != EBUSY)
17154f5064b7SMark J Musante 		fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
17164f5064b7SMark J Musante 		    error);
17174f5064b7SMark J Musante 	dsl_dataset_disown(ds, FTAG);
17184f5064b7SMark J Musante 
1719b2d22b66SGeorge Wilson out:
1720718d718aSGeorge Wilson 	ztest_dsl_dataset_cleanup(osname, curval);
17214f5064b7SMark J Musante 
17224f5064b7SMark J Musante 	(void) rw_unlock(&ztest_shared->zs_name_lock);
17234f5064b7SMark J Musante }
17244f5064b7SMark J Musante 
17254f5064b7SMark J Musante /*
1726fa9e4066Sahrens  * Verify that dmu_object_{alloc,free} work as expected.
1727fa9e4066Sahrens  */
1728fa9e4066Sahrens void
1729fa9e4066Sahrens ztest_dmu_object_alloc_free(ztest_args_t *za)
1730fa9e4066Sahrens {
1731fa9e4066Sahrens 	objset_t *os = za->za_os;
1732fa9e4066Sahrens 	dmu_buf_t *db;
1733fa9e4066Sahrens 	dmu_tx_t *tx;
1734fa9e4066Sahrens 	uint64_t batchobj, object, batchsize, endoff, temp;
1735fa9e4066Sahrens 	int b, c, error, bonuslen;
1736e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
1737fa9e4066Sahrens 	char osname[MAXNAMELEN];
1738fa9e4066Sahrens 
1739fa9e4066Sahrens 	dmu_objset_name(os, osname);
1740fa9e4066Sahrens 
1741fa9e4066Sahrens 	endoff = -8ULL;
1742fa9e4066Sahrens 	batchsize = 2;
1743fa9e4066Sahrens 
1744fa9e4066Sahrens 	/*
1745fa9e4066Sahrens 	 * Create a batch object if necessary, and record it in the directory.
1746fa9e4066Sahrens 	 */
1747e14bb325SJeff Bonwick 	VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
17487bfdf011SNeil Perrin 	    sizeof (uint64_t), &batchobj, DMU_READ_PREFETCH));
1749fa9e4066Sahrens 	if (batchobj == 0) {
1750fa9e4066Sahrens 		tx = dmu_tx_create(os);
1751fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1752fa9e4066Sahrens 		    sizeof (uint64_t));
1753fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1754fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1755fa9e4066Sahrens 		if (error) {
1756fa9e4066Sahrens 			ztest_record_enospc("create a batch object");
1757fa9e4066Sahrens 			dmu_tx_abort(tx);
1758fa9e4066Sahrens 			return;
1759fa9e4066Sahrens 		}
1760fa9e4066Sahrens 		batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1761fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1762fa9e4066Sahrens 		ztest_set_random_blocksize(os, batchobj, tx);
1763fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1764fa9e4066Sahrens 		    sizeof (uint64_t), &batchobj, tx);
1765fa9e4066Sahrens 		dmu_tx_commit(tx);
1766fa9e4066Sahrens 	}
1767fa9e4066Sahrens 
1768fa9e4066Sahrens 	/*
1769fa9e4066Sahrens 	 * Destroy the previous batch of objects.
1770fa9e4066Sahrens 	 */
1771fa9e4066Sahrens 	for (b = 0; b < batchsize; b++) {
1772088f3894Sahrens 		VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
17737bfdf011SNeil Perrin 		    sizeof (uint64_t), &object, DMU_READ_PREFETCH));
1774fa9e4066Sahrens 		if (object == 0)
1775fa9e4066Sahrens 			continue;
1776fa9e4066Sahrens 		/*
1777fa9e4066Sahrens 		 * Read and validate contents.
1778fa9e4066Sahrens 		 * We expect the nth byte of the bonus buffer to be n.
1779fa9e4066Sahrens 		 */
1780ea8dc4b6Seschrock 		VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1781e05725b1Sbonwick 		za->za_dbuf = db;
1782fa9e4066Sahrens 
1783e05725b1Sbonwick 		dmu_object_info_from_db(db, doi);
1784e05725b1Sbonwick 		ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1785e05725b1Sbonwick 		ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1786e05725b1Sbonwick 		ASSERT3S(doi->doi_physical_blks, >=, 0);
1787fa9e4066Sahrens 
1788e05725b1Sbonwick 		bonuslen = doi->doi_bonus_size;
1789fa9e4066Sahrens 
1790fa9e4066Sahrens 		for (c = 0; c < bonuslen; c++) {
1791fa9e4066Sahrens 			if (((uint8_t *)db->db_data)[c] !=
1792fa9e4066Sahrens 			    (uint8_t)(c + bonuslen)) {
1793fa9e4066Sahrens 				fatal(0,
1794fa9e4066Sahrens 				    "bad bonus: %s, obj %llu, off %d: %u != %u",
1795fa9e4066Sahrens 				    osname, object, c,
1796fa9e4066Sahrens 				    ((uint8_t *)db->db_data)[c],
1797fa9e4066Sahrens 				    (uint8_t)(c + bonuslen));
1798fa9e4066Sahrens 			}
1799fa9e4066Sahrens 		}
1800fa9e4066Sahrens 
1801ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1802e05725b1Sbonwick 		za->za_dbuf = NULL;
1803fa9e4066Sahrens 
1804fa9e4066Sahrens 		/*
1805fa9e4066Sahrens 		 * We expect the word at endoff to be our object number.
1806fa9e4066Sahrens 		 */
1807ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, object, endoff,
18087bfdf011SNeil Perrin 		    sizeof (uint64_t), &temp, DMU_READ_PREFETCH));
1809fa9e4066Sahrens 
1810fa9e4066Sahrens 		if (temp != object) {
1811fa9e4066Sahrens 			fatal(0, "bad data in %s, got %llu, expected %llu",
1812fa9e4066Sahrens 			    osname, temp, object);
1813fa9e4066Sahrens 		}
1814fa9e4066Sahrens 
1815fa9e4066Sahrens 		/*
1816fa9e4066Sahrens 		 * Destroy old object and clear batch entry.
1817fa9e4066Sahrens 		 */
1818fa9e4066Sahrens 		tx = dmu_tx_create(os);
1819fa9e4066Sahrens 		dmu_tx_hold_write(tx, batchobj,
1820fa9e4066Sahrens 		    b * sizeof (uint64_t), sizeof (uint64_t));
1821fa9e4066Sahrens 		dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1822fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1823fa9e4066Sahrens 		if (error) {
1824fa9e4066Sahrens 			ztest_record_enospc("free object");
1825fa9e4066Sahrens 			dmu_tx_abort(tx);
1826fa9e4066Sahrens 			return;
1827fa9e4066Sahrens 		}
1828fa9e4066Sahrens 		error = dmu_object_free(os, object, tx);
1829fa9e4066Sahrens 		if (error) {
1830fa9e4066Sahrens 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1831fa9e4066Sahrens 			    osname, object, error);
1832fa9e4066Sahrens 		}
1833fa9e4066Sahrens 		object = 0;
1834fa9e4066Sahrens 
1835fa9e4066Sahrens 		dmu_object_set_checksum(os, batchobj,
1836fa9e4066Sahrens 		    ztest_random_checksum(), tx);
1837fa9e4066Sahrens 		dmu_object_set_compress(os, batchobj,
1838fa9e4066Sahrens 		    ztest_random_compress(), tx);
1839fa9e4066Sahrens 
1840fa9e4066Sahrens 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1841fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
1842fa9e4066Sahrens 
1843fa9e4066Sahrens 		dmu_tx_commit(tx);
1844fa9e4066Sahrens 	}
1845fa9e4066Sahrens 
1846fa9e4066Sahrens 	/*
1847fa9e4066Sahrens 	 * Before creating the new batch of objects, generate a bunch of churn.
1848fa9e4066Sahrens 	 */
1849fa9e4066Sahrens 	for (b = ztest_random(100); b > 0; b--) {
1850fa9e4066Sahrens 		tx = dmu_tx_create(os);
1851fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1852fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1853fa9e4066Sahrens 		if (error) {
1854fa9e4066Sahrens 			ztest_record_enospc("churn objects");
1855fa9e4066Sahrens 			dmu_tx_abort(tx);
1856fa9e4066Sahrens 			return;
1857fa9e4066Sahrens 		}
1858fa9e4066Sahrens 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1859fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
1860fa9e4066Sahrens 		ztest_set_random_blocksize(os, object, tx);
1861fa9e4066Sahrens 		error = dmu_object_free(os, object, tx);
1862fa9e4066Sahrens 		if (error) {
1863fa9e4066Sahrens 			fatal(0, "dmu_object_free('%s', %llu) = %d",
1864fa9e4066Sahrens 			    osname, object, error);
1865fa9e4066Sahrens 		}
1866fa9e4066Sahrens 		dmu_tx_commit(tx);
1867fa9e4066Sahrens 	}
1868fa9e4066Sahrens 
1869fa9e4066Sahrens 	/*
1870fa9e4066Sahrens 	 * Create a new batch of objects with randomly chosen
1871fa9e4066Sahrens 	 * blocksizes and record them in the batch directory.
1872fa9e4066Sahrens 	 */
1873fa9e4066Sahrens 	for (b = 0; b < batchsize; b++) {
1874fa9e4066Sahrens 		uint32_t va_blksize;
1875fa9e4066Sahrens 		u_longlong_t va_nblocks;
1876fa9e4066Sahrens 
1877fa9e4066Sahrens 		tx = dmu_tx_create(os);
1878fa9e4066Sahrens 		dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1879fa9e4066Sahrens 		    sizeof (uint64_t));
1880fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1881fa9e4066Sahrens 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1882fa9e4066Sahrens 		    sizeof (uint64_t));
1883fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
1884fa9e4066Sahrens 		if (error) {
1885fa9e4066Sahrens 			ztest_record_enospc("create batchobj");
1886fa9e4066Sahrens 			dmu_tx_abort(tx);
1887fa9e4066Sahrens 			return;
1888fa9e4066Sahrens 		}
1889fa9e4066Sahrens 		bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1890fa9e4066Sahrens 
1891fa9e4066Sahrens 		object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1892fa9e4066Sahrens 		    DMU_OT_PLAIN_OTHER, bonuslen, tx);
1893fa9e4066Sahrens 
1894fa9e4066Sahrens 		ztest_set_random_blocksize(os, object, tx);
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 		dmu_object_set_checksum(os, object,
1897fa9e4066Sahrens 		    ztest_random_checksum(), tx);
1898fa9e4066Sahrens 		dmu_object_set_compress(os, object,
1899fa9e4066Sahrens 		    ztest_random_compress(), tx);
1900fa9e4066Sahrens 
1901fa9e4066Sahrens 		dmu_write(os, batchobj, b * sizeof (uint64_t),
1902fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
1903fa9e4066Sahrens 
1904fa9e4066Sahrens 		/*
1905fa9e4066Sahrens 		 * Write to both the bonus buffer and the regular data.
1906fa9e4066Sahrens 		 */
1907e05725b1Sbonwick 		VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1908e05725b1Sbonwick 		za->za_dbuf = db;
19091934e92fSmaybee 		ASSERT3U(bonuslen, <=, db->db_size);
1910fa9e4066Sahrens 
1911fa9e4066Sahrens 		dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1912fa9e4066Sahrens 		ASSERT3S(va_nblocks, >=, 0);
1913fa9e4066Sahrens 
1914fa9e4066Sahrens 		dmu_buf_will_dirty(db, tx);
1915fa9e4066Sahrens 
1916fa9e4066Sahrens 		/*
1917fa9e4066Sahrens 		 * See comments above regarding the contents of
1918fa9e4066Sahrens 		 * the bonus buffer and the word at endoff.
1919fa9e4066Sahrens 		 */
19201934e92fSmaybee 		for (c = 0; c < bonuslen; c++)
1921fa9e4066Sahrens 			((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1922fa9e4066Sahrens 
1923ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1924e05725b1Sbonwick 		za->za_dbuf = NULL;
1925fa9e4066Sahrens 
1926fa9e4066Sahrens 		/*
1927fa9e4066Sahrens 		 * Write to a large offset to increase indirection.
1928fa9e4066Sahrens 		 */
1929fa9e4066Sahrens 		dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1930fa9e4066Sahrens 
1931fa9e4066Sahrens 		dmu_tx_commit(tx);
1932fa9e4066Sahrens 	}
1933fa9e4066Sahrens }
1934fa9e4066Sahrens 
1935fa9e4066Sahrens /*
1936fa9e4066Sahrens  * Verify that dmu_{read,write} work as expected.
1937fa9e4066Sahrens  */
1938fa9e4066Sahrens typedef struct bufwad {
1939fa9e4066Sahrens 	uint64_t	bw_index;
1940fa9e4066Sahrens 	uint64_t	bw_txg;
1941fa9e4066Sahrens 	uint64_t	bw_data;
1942fa9e4066Sahrens } bufwad_t;
1943fa9e4066Sahrens 
1944fa9e4066Sahrens typedef struct dmu_read_write_dir {
1945fa9e4066Sahrens 	uint64_t	dd_packobj;
1946fa9e4066Sahrens 	uint64_t	dd_bigobj;
1947fa9e4066Sahrens 	uint64_t	dd_chunk;
1948fa9e4066Sahrens } dmu_read_write_dir_t;
1949fa9e4066Sahrens 
1950fa9e4066Sahrens void
1951fa9e4066Sahrens ztest_dmu_read_write(ztest_args_t *za)
1952fa9e4066Sahrens {
1953fa9e4066Sahrens 	objset_t *os = za->za_os;
1954fa9e4066Sahrens 	dmu_read_write_dir_t dd;
1955fa9e4066Sahrens 	dmu_tx_t *tx;
1956fa9e4066Sahrens 	int i, freeit, error;
1957fa9e4066Sahrens 	uint64_t n, s, txg;
1958fa9e4066Sahrens 	bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1959fa9e4066Sahrens 	uint64_t packoff, packsize, bigoff, bigsize;
1960fa9e4066Sahrens 	uint64_t regions = 997;
1961fa9e4066Sahrens 	uint64_t stride = 123456789ULL;
1962fa9e4066Sahrens 	uint64_t width = 40;
1963fa9e4066Sahrens 	int free_percent = 5;
1964fa9e4066Sahrens 
1965fa9e4066Sahrens 	/*
1966fa9e4066Sahrens 	 * This test uses two objects, packobj and bigobj, that are always
1967fa9e4066Sahrens 	 * updated together (i.e. in the same tx) so that their contents are
1968fa9e4066Sahrens 	 * in sync and can be compared.  Their contents relate to each other
1969fa9e4066Sahrens 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
1970fa9e4066Sahrens 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
1971fa9e4066Sahrens 	 * for any index n, there are three bufwads that should be identical:
1972fa9e4066Sahrens 	 *
1973fa9e4066Sahrens 	 *	packobj, at offset n * sizeof (bufwad_t)
1974fa9e4066Sahrens 	 *	bigobj, at the head of the nth chunk
1975fa9e4066Sahrens 	 *	bigobj, at the tail of the nth chunk
1976fa9e4066Sahrens 	 *
1977fa9e4066Sahrens 	 * The chunk size is arbitrary. It doesn't have to be a power of two,
1978fa9e4066Sahrens 	 * and it doesn't have any relation to the object blocksize.
1979fa9e4066Sahrens 	 * The only requirement is that it can hold at least two bufwads.
1980fa9e4066Sahrens 	 *
1981fa9e4066Sahrens 	 * Normally, we write the bufwad to each of these locations.
1982fa9e4066Sahrens 	 * However, free_percent of the time we instead write zeroes to
1983fa9e4066Sahrens 	 * packobj and perform a dmu_free_range() on bigobj.  By comparing
1984fa9e4066Sahrens 	 * bigobj to packobj, we can verify that the DMU is correctly
1985fa9e4066Sahrens 	 * tracking which parts of an object are allocated and free,
1986fa9e4066Sahrens 	 * and that the contents of the allocated blocks are correct.
1987fa9e4066Sahrens 	 */
1988fa9e4066Sahrens 
1989fa9e4066Sahrens 	/*
1990fa9e4066Sahrens 	 * Read the directory info.  If it's the first time, set things up.
1991fa9e4066Sahrens 	 */
1992ea8dc4b6Seschrock 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
19937bfdf011SNeil Perrin 	    sizeof (dd), &dd, DMU_READ_PREFETCH));
1994fa9e4066Sahrens 	if (dd.dd_chunk == 0) {
1995fa9e4066Sahrens 		ASSERT(dd.dd_packobj == 0);
1996fa9e4066Sahrens 		ASSERT(dd.dd_bigobj == 0);
1997fa9e4066Sahrens 		tx = dmu_tx_create(os);
1998fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1999fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2000fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2001fa9e4066Sahrens 		if (error) {
2002fa9e4066Sahrens 			ztest_record_enospc("create r/w directory");
2003fa9e4066Sahrens 			dmu_tx_abort(tx);
2004fa9e4066Sahrens 			return;
2005fa9e4066Sahrens 		}
2006fa9e4066Sahrens 
2007fa9e4066Sahrens 		dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2008fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
2009fa9e4066Sahrens 		dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
2010fa9e4066Sahrens 		    DMU_OT_NONE, 0, tx);
2011fa9e4066Sahrens 		dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
2012fa9e4066Sahrens 
2013fa9e4066Sahrens 		ztest_set_random_blocksize(os, dd.dd_packobj, tx);
2014fa9e4066Sahrens 		ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
2015fa9e4066Sahrens 
2016fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
2017fa9e4066Sahrens 		    tx);
2018fa9e4066Sahrens 		dmu_tx_commit(tx);
2019fa9e4066Sahrens 	}
2020fa9e4066Sahrens 
2021fa9e4066Sahrens 	/*
2022fa9e4066Sahrens 	 * Prefetch a random chunk of the big object.
2023fa9e4066Sahrens 	 * Our aim here is to get some async reads in flight
2024fa9e4066Sahrens 	 * for blocks that we may free below; the DMU should
2025fa9e4066Sahrens 	 * handle this race correctly.
2026fa9e4066Sahrens 	 */
2027fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
2028fa9e4066Sahrens 	s = 1 + ztest_random(2 * width - 1);
2029fa9e4066Sahrens 	dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
2030fa9e4066Sahrens 
2031fa9e4066Sahrens 	/*
2032fa9e4066Sahrens 	 * Pick a random index and compute the offsets into packobj and bigobj.
2033fa9e4066Sahrens 	 */
2034fa9e4066Sahrens 	n = ztest_random(regions) * stride + ztest_random(width);
2035fa9e4066Sahrens 	s = 1 + ztest_random(width - 1);
2036fa9e4066Sahrens 
2037fa9e4066Sahrens 	packoff = n * sizeof (bufwad_t);
2038fa9e4066Sahrens 	packsize = s * sizeof (bufwad_t);
2039fa9e4066Sahrens 
2040fa9e4066Sahrens 	bigoff = n * dd.dd_chunk;
2041fa9e4066Sahrens 	bigsize = s * dd.dd_chunk;
2042fa9e4066Sahrens 
2043fa9e4066Sahrens 	packbuf = umem_alloc(packsize, UMEM_NOFAIL);
2044fa9e4066Sahrens 	bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
2045fa9e4066Sahrens 
2046fa9e4066Sahrens 	/*
2047fa9e4066Sahrens 	 * free_percent of the time, free a range of bigobj rather than
2048fa9e4066Sahrens 	 * overwriting it.
2049fa9e4066Sahrens 	 */
2050fa9e4066Sahrens 	freeit = (ztest_random(100) < free_percent);
2051fa9e4066Sahrens 
2052fa9e4066Sahrens 	/*
2053fa9e4066Sahrens 	 * Read the current contents of our objects.
2054fa9e4066Sahrens 	 */
20557bfdf011SNeil Perrin 	error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf,
20567bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
2057ea8dc4b6Seschrock 	ASSERT3U(error, ==, 0);
20587bfdf011SNeil Perrin 	error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf,
20597bfdf011SNeil Perrin 	    DMU_READ_PREFETCH);
2060ea8dc4b6Seschrock 	ASSERT3U(error, ==, 0);
2061fa9e4066Sahrens 
2062fa9e4066Sahrens 	/*
2063fa9e4066Sahrens 	 * Get a tx for the mods to both packobj and bigobj.
2064fa9e4066Sahrens 	 */
2065fa9e4066Sahrens 	tx = dmu_tx_create(os);
2066fa9e4066Sahrens 
2067fa9e4066Sahrens 	dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
2068fa9e4066Sahrens 
2069fa9e4066Sahrens 	if (freeit)
2070fa9e4066Sahrens 		dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
2071fa9e4066Sahrens 	else
2072fa9e4066Sahrens 		dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
2073fa9e4066Sahrens 
2074fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2075fa9e4066Sahrens 
2076fa9e4066Sahrens 	if (error) {
2077fa9e4066Sahrens 		ztest_record_enospc("dmu r/w range");
2078fa9e4066Sahrens 		dmu_tx_abort(tx);
2079fa9e4066Sahrens 		umem_free(packbuf, packsize);
2080fa9e4066Sahrens 		umem_free(bigbuf, bigsize);
2081fa9e4066Sahrens 		return;
2082fa9e4066Sahrens 	}
2083fa9e4066Sahrens 
2084fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
2085fa9e4066Sahrens 
2086fa9e4066Sahrens 	/*
2087fa9e4066Sahrens 	 * For each index from n to n + s, verify that the existing bufwad
2088fa9e4066Sahrens 	 * in packobj matches the bufwads at the head and tail of the
2089fa9e4066Sahrens 	 * corresponding chunk in bigobj.  Then update all three bufwads
2090fa9e4066Sahrens 	 * with the new values we want to write out.
2091fa9e4066Sahrens 	 */
2092fa9e4066Sahrens 	for (i = 0; i < s; i++) {
2093fa9e4066Sahrens 		/* LINTED */
2094fa9e4066Sahrens 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
2095fa9e4066Sahrens 		/* LINTED */
2096fa9e4066Sahrens 		bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
2097fa9e4066Sahrens 		/* LINTED */
2098fa9e4066Sahrens 		bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
2099fa9e4066Sahrens 
2100fa9e4066Sahrens 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
2101fa9e4066Sahrens 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
2102fa9e4066Sahrens 
2103fa9e4066Sahrens 		if (pack->bw_txg > txg)
2104fa9e4066Sahrens 			fatal(0, "future leak: got %llx, open txg is %llx",
2105fa9e4066Sahrens 			    pack->bw_txg, txg);
2106fa9e4066Sahrens 
2107fa9e4066Sahrens 		if (pack->bw_data != 0 && pack->bw_index != n + i)
2108fa9e4066Sahrens 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
2109fa9e4066Sahrens 			    pack->bw_index, n, i);
2110fa9e4066Sahrens 
2111fa9e4066Sahrens 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
2112fa9e4066Sahrens 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
2113fa9e4066Sahrens 
2114fa9e4066Sahrens 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
2115fa9e4066Sahrens 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
2116fa9e4066Sahrens 
2117fa9e4066Sahrens 		if (freeit) {
2118fa9e4066Sahrens 			bzero(pack, sizeof (bufwad_t));
2119fa9e4066Sahrens 		} else {
2120fa9e4066Sahrens 			pack->bw_index = n + i;
2121fa9e4066Sahrens 			pack->bw_txg = txg;
2122fa9e4066Sahrens 			pack->bw_data = 1 + ztest_random(-2ULL);
2123fa9e4066Sahrens 		}
2124fa9e4066Sahrens 		*bigH = *pack;
2125fa9e4066Sahrens 		*bigT = *pack;
2126fa9e4066Sahrens 	}
2127fa9e4066Sahrens 
2128fa9e4066Sahrens 	/*
2129fa9e4066Sahrens 	 * We've verified all the old bufwads, and made new ones.
2130fa9e4066Sahrens 	 * Now write them out.
2131fa9e4066Sahrens 	 */
2132fa9e4066Sahrens 	dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
2133fa9e4066Sahrens 
2134fa9e4066Sahrens 	if (freeit) {
2135fa9e4066Sahrens 		if (zopt_verbose >= 6) {
2136fa9e4066Sahrens 			(void) printf("freeing offset %llx size %llx"
2137fa9e4066Sahrens 			    " txg %llx\n",
2138fa9e4066Sahrens 			    (u_longlong_t)bigoff,
2139fa9e4066Sahrens 			    (u_longlong_t)bigsize,
2140fa9e4066Sahrens 			    (u_longlong_t)txg);
2141fa9e4066Sahrens 		}
2142ea8dc4b6Seschrock 		VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
2143ea8dc4b6Seschrock 		    bigsize, tx));
2144fa9e4066Sahrens 	} else {
2145fa9e4066Sahrens 		if (zopt_verbose >= 6) {
2146fa9e4066Sahrens 			(void) printf("writing offset %llx size %llx"
2147fa9e4066Sahrens 			    " txg %llx\n",
2148fa9e4066Sahrens 			    (u_longlong_t)bigoff,
2149fa9e4066Sahrens 			    (u_longlong_t)bigsize,
2150fa9e4066Sahrens 			    (u_longlong_t)txg);
2151fa9e4066Sahrens 		}
2152fa9e4066Sahrens 		dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
2153fa9e4066Sahrens 	}
2154fa9e4066Sahrens 
2155fa9e4066Sahrens 	dmu_tx_commit(tx);
2156fa9e4066Sahrens 
2157fa9e4066Sahrens 	/*
2158fa9e4066Sahrens 	 * Sanity check the stuff we just wrote.
2159fa9e4066Sahrens 	 */
2160fa9e4066Sahrens 	{
2161fa9e4066Sahrens 		void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
2162fa9e4066Sahrens 		void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
2163fa9e4066Sahrens 
2164ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
21657bfdf011SNeil Perrin 		    packsize, packcheck, DMU_READ_PREFETCH));
2166ea8dc4b6Seschrock 		VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
21677bfdf011SNeil Perrin 		    bigsize, bigcheck, DMU_READ_PREFETCH));
2168fa9e4066Sahrens 
2169fa9e4066Sahrens 		ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
2170fa9e4066Sahrens 		ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
2171fa9e4066Sahrens 
2172fa9e4066Sahrens 		umem_free(packcheck, packsize);
2173fa9e4066Sahrens 		umem_free(bigcheck, bigsize);
2174fa9e4066Sahrens 	}
2175fa9e4066Sahrens 
2176fa9e4066Sahrens 	umem_free(packbuf, packsize);
2177fa9e4066Sahrens 	umem_free(bigbuf, bigsize);
2178fa9e4066Sahrens }
2179fa9e4066Sahrens 
2180fa9e4066Sahrens void
21812fdbea25SAleksandr Guzovskiy compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
21822fdbea25SAleksandr Guzovskiy     uint64_t bigsize, uint64_t n, dmu_read_write_dir_t dd, uint64_t txg)
21832fdbea25SAleksandr Guzovskiy {
21842fdbea25SAleksandr Guzovskiy 	uint64_t i;
21852fdbea25SAleksandr Guzovskiy 	bufwad_t *pack;
21862fdbea25SAleksandr Guzovskiy 	bufwad_t *bigH;
21872fdbea25SAleksandr Guzovskiy 	bufwad_t *bigT;
21882fdbea25SAleksandr Guzovskiy 
21892fdbea25SAleksandr Guzovskiy 	/*
21902fdbea25SAleksandr Guzovskiy 	 * For each index from n to n + s, verify that the existing bufwad
21912fdbea25SAleksandr Guzovskiy 	 * in packobj matches the bufwads at the head and tail of the
21922fdbea25SAleksandr Guzovskiy 	 * corresponding chunk in bigobj.  Then update all three bufwads
21932fdbea25SAleksandr Guzovskiy 	 * with the new values we want to write out.
21942fdbea25SAleksandr Guzovskiy 	 */
21952fdbea25SAleksandr Guzovskiy 	for (i = 0; i < s; i++) {
21962fdbea25SAleksandr Guzovskiy 		/* LINTED */
21972fdbea25SAleksandr Guzovskiy 		pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
21982fdbea25SAleksandr Guzovskiy 		/* LINTED */
21992fdbea25SAleksandr Guzovskiy 		bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
22002fdbea25SAleksandr Guzovskiy 		/* LINTED */
22012fdbea25SAleksandr Guzovskiy 		bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
22022fdbea25SAleksandr Guzovskiy 
22032fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
22042fdbea25SAleksandr Guzovskiy 		ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
22052fdbea25SAleksandr Guzovskiy 
22062fdbea25SAleksandr Guzovskiy 		if (pack->bw_txg > txg)
22072fdbea25SAleksandr Guzovskiy 			fatal(0, "future leak: got %llx, open txg is %llx",
22082fdbea25SAleksandr Guzovskiy 			    pack->bw_txg, txg);
22092fdbea25SAleksandr Guzovskiy 
22102fdbea25SAleksandr Guzovskiy 		if (pack->bw_data != 0 && pack->bw_index != n + i)
22112fdbea25SAleksandr Guzovskiy 			fatal(0, "wrong index: got %llx, wanted %llx+%llx",
22122fdbea25SAleksandr Guzovskiy 			    pack->bw_index, n, i);
22132fdbea25SAleksandr Guzovskiy 
22142fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
22152fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
22162fdbea25SAleksandr Guzovskiy 
22172fdbea25SAleksandr Guzovskiy 		if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
22182fdbea25SAleksandr Guzovskiy 			fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
22192fdbea25SAleksandr Guzovskiy 
22202fdbea25SAleksandr Guzovskiy 		pack->bw_index = n + i;
22212fdbea25SAleksandr Guzovskiy 		pack->bw_txg = txg;
22222fdbea25SAleksandr Guzovskiy 		pack->bw_data = 1 + ztest_random(-2ULL);
22232fdbea25SAleksandr Guzovskiy 
22242fdbea25SAleksandr Guzovskiy 		*bigH = *pack;
22252fdbea25SAleksandr Guzovskiy 		*bigT = *pack;
22262fdbea25SAleksandr Guzovskiy 	}
22272fdbea25SAleksandr Guzovskiy }
22282fdbea25SAleksandr Guzovskiy 
22292fdbea25SAleksandr Guzovskiy void
22302fdbea25SAleksandr Guzovskiy ztest_dmu_read_write_zcopy(ztest_args_t *za)
22312fdbea25SAleksandr Guzovskiy {
22322fdbea25SAleksandr Guzovskiy 	objset_t *os = za->za_os;
22332fdbea25SAleksandr Guzovskiy 	dmu_read_write_dir_t dd;
22342fdbea25SAleksandr Guzovskiy 	dmu_tx_t *tx;
22352fdbea25SAleksandr Guzovskiy 	uint64_t i;
22362fdbea25SAleksandr Guzovskiy 	int error;
22372fdbea25SAleksandr Guzovskiy 	uint64_t n, s, txg;
22382fdbea25SAleksandr Guzovskiy 	bufwad_t *packbuf, *bigbuf;
22392fdbea25SAleksandr Guzovskiy 	uint64_t packoff, packsize, bigoff, bigsize;
22402fdbea25SAleksandr Guzovskiy 	uint64_t regions = 997;
22412fdbea25SAleksandr Guzovskiy 	uint64_t stride = 123456789ULL;
22422fdbea25SAleksandr Guzovskiy 	uint64_t width = 9;
22432fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
22442fdbea25SAleksandr Guzovskiy 	arc_buf_t **bigbuf_arcbufs;
22452fdbea25SAleksandr Guzovskiy 	dmu_object_info_t *doi = &za->za_doi;
22462fdbea25SAleksandr Guzovskiy 
22472fdbea25SAleksandr Guzovskiy 	/*
22482fdbea25SAleksandr Guzovskiy 	 * This test uses two objects, packobj and bigobj, that are always
22492fdbea25SAleksandr Guzovskiy 	 * updated together (i.e. in the same tx) so that their contents are
22502fdbea25SAleksandr Guzovskiy 	 * in sync and can be compared.  Their contents relate to each other
22512fdbea25SAleksandr Guzovskiy 	 * in a simple way: packobj is a dense array of 'bufwad' structures,
22522fdbea25SAleksandr Guzovskiy 	 * while bigobj is a sparse array of the same bufwads.  Specifically,
22532fdbea25SAleksandr Guzovskiy 	 * for any index n, there are three bufwads that should be identical:
22542fdbea25SAleksandr Guzovskiy 	 *
22552fdbea25SAleksandr Guzovskiy 	 *	packobj, at offset n * sizeof (bufwad_t)
22562fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the head of the nth chunk
22572fdbea25SAleksandr Guzovskiy 	 *	bigobj, at the tail of the nth chunk
22582fdbea25SAleksandr Guzovskiy 	 *
22592fdbea25SAleksandr Guzovskiy 	 * The chunk size is set equal to bigobj block size so that
22602fdbea25SAleksandr Guzovskiy 	 * dmu_assign_arcbuf() can be tested for object updates.
22612fdbea25SAleksandr Guzovskiy 	 */
22622fdbea25SAleksandr Guzovskiy 
22632fdbea25SAleksandr Guzovskiy 	/*
22642fdbea25SAleksandr Guzovskiy 	 * Read the directory info.  If it's the first time, set things up.
22652fdbea25SAleksandr Guzovskiy 	 */
22662fdbea25SAleksandr Guzovskiy 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
22677bfdf011SNeil Perrin 	    sizeof (dd), &dd, DMU_READ_PREFETCH));
22682fdbea25SAleksandr Guzovskiy 	if (dd.dd_chunk == 0) {
22692fdbea25SAleksandr Guzovskiy 		ASSERT(dd.dd_packobj == 0);
22702fdbea25SAleksandr Guzovskiy 		ASSERT(dd.dd_bigobj == 0);
22712fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
22722fdbea25SAleksandr Guzovskiy 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
22732fdbea25SAleksandr Guzovskiy 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
22742fdbea25SAleksandr Guzovskiy 		error = dmu_tx_assign(tx, TXG_WAIT);
22752fdbea25SAleksandr Guzovskiy 		if (error) {
22762fdbea25SAleksandr Guzovskiy 			ztest_record_enospc("create r/w directory");
22772fdbea25SAleksandr Guzovskiy 			dmu_tx_abort(tx);
22782fdbea25SAleksandr Guzovskiy 			return;
22792fdbea25SAleksandr Guzovskiy 		}
22802fdbea25SAleksandr Guzovskiy 
22812fdbea25SAleksandr Guzovskiy 		dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
22822fdbea25SAleksandr Guzovskiy 		    DMU_OT_NONE, 0, tx);
22832fdbea25SAleksandr Guzovskiy 		dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
22842fdbea25SAleksandr Guzovskiy 		    DMU_OT_NONE, 0, tx);
22852fdbea25SAleksandr Guzovskiy 		ztest_set_random_blocksize(os, dd.dd_packobj, tx);
22862fdbea25SAleksandr Guzovskiy 		ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
22872fdbea25SAleksandr Guzovskiy 
22882fdbea25SAleksandr Guzovskiy 		VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
22892fdbea25SAleksandr Guzovskiy 		ASSERT(doi->doi_data_block_size >= 2 * sizeof (bufwad_t));
22902fdbea25SAleksandr Guzovskiy 		ASSERT(ISP2(doi->doi_data_block_size));
22912fdbea25SAleksandr Guzovskiy 		dd.dd_chunk = doi->doi_data_block_size;
22922fdbea25SAleksandr Guzovskiy 
22932fdbea25SAleksandr Guzovskiy 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
22942fdbea25SAleksandr Guzovskiy 		    tx);
22952fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
22962fdbea25SAleksandr Guzovskiy 	} else {
22972fdbea25SAleksandr Guzovskiy 		VERIFY(dmu_object_info(os, dd.dd_bigobj, doi) == 0);
22982fdbea25SAleksandr Guzovskiy 		VERIFY(ISP2(doi->doi_data_block_size));
22992fdbea25SAleksandr Guzovskiy 		VERIFY(dd.dd_chunk == doi->doi_data_block_size);
23002fdbea25SAleksandr Guzovskiy 		VERIFY(dd.dd_chunk >= 2 * sizeof (bufwad_t));
23012fdbea25SAleksandr Guzovskiy 	}
23022fdbea25SAleksandr Guzovskiy 
23032fdbea25SAleksandr Guzovskiy 	/*
23042fdbea25SAleksandr Guzovskiy 	 * Pick a random index and compute the offsets into packobj and bigobj.
23052fdbea25SAleksandr Guzovskiy 	 */
23062fdbea25SAleksandr Guzovskiy 	n = ztest_random(regions) * stride + ztest_random(width);
23072fdbea25SAleksandr Guzovskiy 	s = 1 + ztest_random(width - 1);
23082fdbea25SAleksandr Guzovskiy 
23092fdbea25SAleksandr Guzovskiy 	packoff = n * sizeof (bufwad_t);
23102fdbea25SAleksandr Guzovskiy 	packsize = s * sizeof (bufwad_t);
23112fdbea25SAleksandr Guzovskiy 
23122fdbea25SAleksandr Guzovskiy 	bigoff = n * dd.dd_chunk;
23132fdbea25SAleksandr Guzovskiy 	bigsize = s * dd.dd_chunk;
23142fdbea25SAleksandr Guzovskiy 
23152fdbea25SAleksandr Guzovskiy 	packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
23162fdbea25SAleksandr Guzovskiy 	bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
23172fdbea25SAleksandr Guzovskiy 
23182fdbea25SAleksandr Guzovskiy 	VERIFY(dmu_bonus_hold(os, dd.dd_bigobj, FTAG, &bonus_db) == 0);
23192fdbea25SAleksandr Guzovskiy 
23202fdbea25SAleksandr Guzovskiy 	bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
23212fdbea25SAleksandr Guzovskiy 
23222fdbea25SAleksandr Guzovskiy 	/*
23232fdbea25SAleksandr Guzovskiy 	 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
23242fdbea25SAleksandr Guzovskiy 	 * Iteration 1 test zcopy to already referenced dbufs.
23252fdbea25SAleksandr Guzovskiy 	 * Iteration 2 test zcopy to dirty dbuf in the same txg.
23262fdbea25SAleksandr Guzovskiy 	 * Iteration 3 test zcopy to dbuf dirty in previous txg.
23272fdbea25SAleksandr Guzovskiy 	 * Iteration 4 test zcopy when dbuf is no longer dirty.
23282fdbea25SAleksandr Guzovskiy 	 * Iteration 5 test zcopy when it can't be done.
23292fdbea25SAleksandr Guzovskiy 	 * Iteration 6 one more zcopy write.
23302fdbea25SAleksandr Guzovskiy 	 */
23312fdbea25SAleksandr Guzovskiy 	for (i = 0; i < 7; i++) {
23322fdbea25SAleksandr Guzovskiy 		uint64_t j;
23332fdbea25SAleksandr Guzovskiy 		uint64_t off;
23342fdbea25SAleksandr Guzovskiy 
23352fdbea25SAleksandr Guzovskiy 		/*
23362fdbea25SAleksandr Guzovskiy 		 * In iteration 5 (i == 5) use arcbufs
23372fdbea25SAleksandr Guzovskiy 		 * that don't match bigobj blksz to test
23382fdbea25SAleksandr Guzovskiy 		 * dmu_assign_arcbuf() when it can't directly
23392fdbea25SAleksandr Guzovskiy 		 * assign an arcbuf to a dbuf.
23402fdbea25SAleksandr Guzovskiy 		 */
23412fdbea25SAleksandr Guzovskiy 		for (j = 0; j < s; j++) {
23422fdbea25SAleksandr Guzovskiy 			if (i != 5) {
23432fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[j] =
23442fdbea25SAleksandr Guzovskiy 				    dmu_request_arcbuf(bonus_db,
23452fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk);
23462fdbea25SAleksandr Guzovskiy 			} else {
23472fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j] =
23482fdbea25SAleksandr Guzovskiy 				    dmu_request_arcbuf(bonus_db,
23492fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk / 2);
23502fdbea25SAleksandr Guzovskiy 				bigbuf_arcbufs[2 * j + 1] =
23512fdbea25SAleksandr Guzovskiy 				    dmu_request_arcbuf(bonus_db,
23522fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk / 2);
23532fdbea25SAleksandr Guzovskiy 			}
23542fdbea25SAleksandr Guzovskiy 		}
23552fdbea25SAleksandr Guzovskiy 
23562fdbea25SAleksandr Guzovskiy 		/*
23572fdbea25SAleksandr Guzovskiy 		 * Get a tx for the mods to both packobj and bigobj.
23582fdbea25SAleksandr Guzovskiy 		 */
23592fdbea25SAleksandr Guzovskiy 		tx = dmu_tx_create(os);
23602fdbea25SAleksandr Guzovskiy 
23612fdbea25SAleksandr Guzovskiy 		dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
23622fdbea25SAleksandr Guzovskiy 		dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
23632fdbea25SAleksandr Guzovskiy 
23642fdbea25SAleksandr Guzovskiy 		if (ztest_random(100) == 0) {
23652fdbea25SAleksandr Guzovskiy 			error = -1;
23662fdbea25SAleksandr Guzovskiy 		} else {
23672fdbea25SAleksandr Guzovskiy 			error = dmu_tx_assign(tx, TXG_WAIT);
23682fdbea25SAleksandr Guzovskiy 		}
23692fdbea25SAleksandr Guzovskiy 
23702fdbea25SAleksandr Guzovskiy 		if (error) {
23712fdbea25SAleksandr Guzovskiy 			if (error != -1) {
23722fdbea25SAleksandr Guzovskiy 				ztest_record_enospc("dmu r/w range");
23732fdbea25SAleksandr Guzovskiy 			}
23742fdbea25SAleksandr Guzovskiy 			dmu_tx_abort(tx);
23752fdbea25SAleksandr Guzovskiy 			umem_free(packbuf, packsize);
23762fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf, bigsize);
23772fdbea25SAleksandr Guzovskiy 			for (j = 0; j < s; j++) {
23782fdbea25SAleksandr Guzovskiy 				if (i != 5) {
23792fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(bigbuf_arcbufs[j]);
23802fdbea25SAleksandr Guzovskiy 				} else {
23812fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
23822fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j]);
23832fdbea25SAleksandr Guzovskiy 					dmu_return_arcbuf(
23842fdbea25SAleksandr Guzovskiy 					    bigbuf_arcbufs[2 * j + 1]);
23852fdbea25SAleksandr Guzovskiy 				}
23862fdbea25SAleksandr Guzovskiy 			}
23872fdbea25SAleksandr Guzovskiy 			umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
23882fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
23892fdbea25SAleksandr Guzovskiy 			return;
23902fdbea25SAleksandr Guzovskiy 		}
23912fdbea25SAleksandr Guzovskiy 
23922fdbea25SAleksandr Guzovskiy 		txg = dmu_tx_get_txg(tx);
23932fdbea25SAleksandr Guzovskiy 
23942fdbea25SAleksandr Guzovskiy 		/*
23952fdbea25SAleksandr Guzovskiy 		 * 50% of the time don't read objects in the 1st iteration to
23962fdbea25SAleksandr Guzovskiy 		 * test dmu_assign_arcbuf() for the case when there're no
23972fdbea25SAleksandr Guzovskiy 		 * existing dbufs for the specified offsets.
23982fdbea25SAleksandr Guzovskiy 		 */
23992fdbea25SAleksandr Guzovskiy 		if (i != 0 || ztest_random(2) != 0) {
24002fdbea25SAleksandr Guzovskiy 			error = dmu_read(os, dd.dd_packobj, packoff,
24017bfdf011SNeil Perrin 			    packsize, packbuf, DMU_READ_PREFETCH);
24022fdbea25SAleksandr Guzovskiy 			ASSERT3U(error, ==, 0);
24032fdbea25SAleksandr Guzovskiy 			error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize,
24047bfdf011SNeil Perrin 			    bigbuf, DMU_READ_PREFETCH);
24052fdbea25SAleksandr Guzovskiy 			ASSERT3U(error, ==, 0);
24062fdbea25SAleksandr Guzovskiy 		}
24072fdbea25SAleksandr Guzovskiy 		compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
24082fdbea25SAleksandr Guzovskiy 		    n, dd, txg);
24092fdbea25SAleksandr Guzovskiy 
24102fdbea25SAleksandr Guzovskiy 		/*
24112fdbea25SAleksandr Guzovskiy 		 * We've verified all the old bufwads, and made new ones.
24122fdbea25SAleksandr Guzovskiy 		 * Now write them out.
24132fdbea25SAleksandr Guzovskiy 		 */
24142fdbea25SAleksandr Guzovskiy 		dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
24152fdbea25SAleksandr Guzovskiy 		if (zopt_verbose >= 6) {
24162fdbea25SAleksandr Guzovskiy 			(void) printf("writing offset %llx size %llx"
24172fdbea25SAleksandr Guzovskiy 			    " txg %llx\n",
24182fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigoff,
24192fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)bigsize,
24202fdbea25SAleksandr Guzovskiy 			    (u_longlong_t)txg);
24212fdbea25SAleksandr Guzovskiy 		}
24222fdbea25SAleksandr Guzovskiy 		for (off = bigoff, j = 0; j < s; j++, off += dd.dd_chunk) {
24232fdbea25SAleksandr Guzovskiy 			dmu_buf_t *dbt;
24242fdbea25SAleksandr Guzovskiy 			if (i != 5) {
24252fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
24262fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j]->b_data, dd.dd_chunk);
24272fdbea25SAleksandr Guzovskiy 			} else {
24282fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff),
24292fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j]->b_data,
24302fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk / 2);
24312fdbea25SAleksandr Guzovskiy 				bcopy((caddr_t)bigbuf + (off - bigoff) +
24322fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk / 2,
24332fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1]->b_data,
24342fdbea25SAleksandr Guzovskiy 				    dd.dd_chunk / 2);
24352fdbea25SAleksandr Guzovskiy 			}
24362fdbea25SAleksandr Guzovskiy 
24372fdbea25SAleksandr Guzovskiy 			if (i == 1) {
24382fdbea25SAleksandr Guzovskiy 				VERIFY(dmu_buf_hold(os, dd.dd_bigobj, off,
24392fdbea25SAleksandr Guzovskiy 				    FTAG, &dbt) == 0);
24402fdbea25SAleksandr Guzovskiy 			}
24412fdbea25SAleksandr Guzovskiy 			if (i != 5) {
24422fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
24432fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[j], tx);
24442fdbea25SAleksandr Guzovskiy 			} else {
24452fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db, off,
24462fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j], tx);
24472fdbea25SAleksandr Guzovskiy 				dmu_assign_arcbuf(bonus_db,
24482fdbea25SAleksandr Guzovskiy 				    off + dd.dd_chunk / 2,
24492fdbea25SAleksandr Guzovskiy 				    bigbuf_arcbufs[2 * j + 1], tx);
24502fdbea25SAleksandr Guzovskiy 			}
24512fdbea25SAleksandr Guzovskiy 			if (i == 1) {
24522fdbea25SAleksandr Guzovskiy 				dmu_buf_rele(dbt, FTAG);
24532fdbea25SAleksandr Guzovskiy 			}
24542fdbea25SAleksandr Guzovskiy 		}
24552fdbea25SAleksandr Guzovskiy 		dmu_tx_commit(tx);
24562fdbea25SAleksandr Guzovskiy 
24572fdbea25SAleksandr Guzovskiy 		/*
24582fdbea25SAleksandr Guzovskiy 		 * Sanity check the stuff we just wrote.
24592fdbea25SAleksandr Guzovskiy 		 */
24602fdbea25SAleksandr Guzovskiy 		{
24612fdbea25SAleksandr Guzovskiy 			void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
24622fdbea25SAleksandr Guzovskiy 			void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
24632fdbea25SAleksandr Guzovskiy 
24642fdbea25SAleksandr Guzovskiy 			VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
24657bfdf011SNeil Perrin 			    packsize, packcheck, DMU_READ_PREFETCH));
24662fdbea25SAleksandr Guzovskiy 			VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
24677bfdf011SNeil Perrin 			    bigsize, bigcheck, DMU_READ_PREFETCH));
24682fdbea25SAleksandr Guzovskiy 
24692fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
24702fdbea25SAleksandr Guzovskiy 			ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
24712fdbea25SAleksandr Guzovskiy 
24722fdbea25SAleksandr Guzovskiy 			umem_free(packcheck, packsize);
24732fdbea25SAleksandr Guzovskiy 			umem_free(bigcheck, bigsize);
24742fdbea25SAleksandr Guzovskiy 		}
24752fdbea25SAleksandr Guzovskiy 		if (i == 2) {
24762fdbea25SAleksandr Guzovskiy 			txg_wait_open(dmu_objset_pool(os), 0);
24772fdbea25SAleksandr Guzovskiy 		} else if (i == 3) {
24782fdbea25SAleksandr Guzovskiy 			txg_wait_synced(dmu_objset_pool(os), 0);
24792fdbea25SAleksandr Guzovskiy 		}
24802fdbea25SAleksandr Guzovskiy 	}
24812fdbea25SAleksandr Guzovskiy 
24822fdbea25SAleksandr Guzovskiy 	dmu_buf_rele(bonus_db, FTAG);
24832fdbea25SAleksandr Guzovskiy 	umem_free(packbuf, packsize);
24842fdbea25SAleksandr Guzovskiy 	umem_free(bigbuf, bigsize);
24852fdbea25SAleksandr Guzovskiy 	umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
24862fdbea25SAleksandr Guzovskiy }
24872fdbea25SAleksandr Guzovskiy 
24882fdbea25SAleksandr Guzovskiy void
2489e05725b1Sbonwick ztest_dmu_check_future_leak(ztest_args_t *za)
24904d31c452Smaybee {
2491e05725b1Sbonwick 	objset_t *os = za->za_os;
24924d31c452Smaybee 	dmu_buf_t *db;
2493e05725b1Sbonwick 	ztest_block_tag_t *bt;
2494e05725b1Sbonwick 	dmu_object_info_t *doi = &za->za_doi;
24954d31c452Smaybee 
24964d31c452Smaybee 	/*
24974d31c452Smaybee 	 * Make sure that, if there is a write record in the bonus buffer
24984d31c452Smaybee 	 * of the ZTEST_DIROBJ, that the txg for this record is <= the
24994d31c452Smaybee 	 * last synced txg of the pool.
25004d31c452Smaybee 	 */
2501e05725b1Sbonwick 	VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2502e05725b1Sbonwick 	za->za_dbuf = db;
2503e05725b1Sbonwick 	VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
2504e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
2505e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2506e05725b1Sbonwick 	ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
2507e05725b1Sbonwick 	bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
2508e05725b1Sbonwick 	if (bt->bt_objset != 0) {
2509e05725b1Sbonwick 		ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
2510e05725b1Sbonwick 		ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
2511e05725b1Sbonwick 		ASSERT3U(bt->bt_offset, ==, -1ULL);
2512e05725b1Sbonwick 		ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
25134d31c452Smaybee 	}
25144d31c452Smaybee 	dmu_buf_rele(db, FTAG);
2515e05725b1Sbonwick 	za->za_dbuf = NULL;
25164d31c452Smaybee }
25174d31c452Smaybee 
25184d31c452Smaybee void
2519fa9e4066Sahrens ztest_dmu_write_parallel(ztest_args_t *za)
2520fa9e4066Sahrens {
2521fa9e4066Sahrens 	objset_t *os = za->za_os;
2522e05725b1Sbonwick 	ztest_block_tag_t *rbt = &za->za_rbt;
2523e05725b1Sbonwick 	ztest_block_tag_t *wbt = &za->za_wbt;
2524e05725b1Sbonwick 	const size_t btsize = sizeof (ztest_block_tag_t);
2525fa9e4066Sahrens 	dmu_buf_t *db;
2526e05725b1Sbonwick 	int b, error;
2527e05725b1Sbonwick 	int bs = ZTEST_DIROBJ_BLOCKSIZE;
2528e05725b1Sbonwick 	int do_free = 0;
2529f4a72450SJeff Bonwick 	uint64_t off, txg, txg_how;
2530fa9e4066Sahrens 	mutex_t *lp;
2531fa9e4066Sahrens 	char osname[MAXNAMELEN];
2532fa9e4066Sahrens 	char iobuf[SPA_MAXBLOCKSIZE];
2533e05725b1Sbonwick 	blkptr_t blk = { 0 };
2534e05725b1Sbonwick 	uint64_t blkoff;
2535e05725b1Sbonwick 	zbookmark_t zb;
2536e05725b1Sbonwick 	dmu_tx_t *tx = dmu_tx_create(os);
25372fdbea25SAleksandr Guzovskiy 	dmu_buf_t *bonus_db;
25382fdbea25SAleksandr Guzovskiy 	arc_buf_t *abuf = NULL;
2539fa9e4066Sahrens 
2540fa9e4066Sahrens 	dmu_objset_name(os, osname);
2541fa9e4066Sahrens 
2542fa9e4066Sahrens 	/*
2543fa9e4066Sahrens 	 * Have multiple threads write to large offsets in ZTEST_DIROBJ
2544fa9e4066Sahrens 	 * to verify that having multiple threads writing to the same object
2545fa9e4066Sahrens 	 * in parallel doesn't cause any trouble.
2546fa9e4066Sahrens 	 */
2547fa9e4066Sahrens 	if (ztest_random(4) == 0) {
2548fa9e4066Sahrens 		/*
2549fa9e4066Sahrens 		 * Do the bonus buffer instead of a regular block.
2550e05725b1Sbonwick 		 * We need a lock to serialize resize vs. others,
2551e05725b1Sbonwick 		 * so we hash on the objset ID.
2552fa9e4066Sahrens 		 */
2553e05725b1Sbonwick 		b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
2554fa9e4066Sahrens 		off = -1ULL;
2555fa9e4066Sahrens 		dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
2556e05725b1Sbonwick 	} else {
2557e05725b1Sbonwick 		b = ztest_random(ZTEST_SYNC_LOCKS);
2558e05725b1Sbonwick 		off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
2559e05725b1Sbonwick 		if (ztest_random(4) == 0) {
2560e05725b1Sbonwick 			do_free = 1;
2561fa9e4066Sahrens 			dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
2562e05725b1Sbonwick 		} else {
2563fa9e4066Sahrens 			dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
2564e05725b1Sbonwick 		}
2565e05725b1Sbonwick 	}
2566fa9e4066Sahrens 
25672fdbea25SAleksandr Guzovskiy 	if (off != -1ULL && P2PHASE(off, bs) == 0 && !do_free &&
25682fdbea25SAleksandr Guzovskiy 	    ztest_random(8) == 0) {
25692fdbea25SAleksandr Guzovskiy 		VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &bonus_db) == 0);
25702fdbea25SAleksandr Guzovskiy 		abuf = dmu_request_arcbuf(bonus_db, bs);
25712fdbea25SAleksandr Guzovskiy 	}
25722fdbea25SAleksandr Guzovskiy 
2573fa9e4066Sahrens 	txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
2574fa9e4066Sahrens 	error = dmu_tx_assign(tx, txg_how);
2575fa9e4066Sahrens 	if (error) {
2576fa9e4066Sahrens 		if (error == ERESTART) {
2577fa9e4066Sahrens 			ASSERT(txg_how == TXG_NOWAIT);
25788a2f1b91Sahrens 			dmu_tx_wait(tx);
2579e05725b1Sbonwick 		} else {
2580e05725b1Sbonwick 			ztest_record_enospc("dmu write parallel");
2581fa9e4066Sahrens 		}
25828a2f1b91Sahrens 		dmu_tx_abort(tx);
25832fdbea25SAleksandr Guzovskiy 		if (abuf != NULL) {
25842fdbea25SAleksandr Guzovskiy 			dmu_return_arcbuf(abuf);
25852fdbea25SAleksandr Guzovskiy 			dmu_buf_rele(bonus_db, FTAG);
25862fdbea25SAleksandr Guzovskiy 		}
2587fa9e4066Sahrens 		return;
2588fa9e4066Sahrens 	}
2589f4a72450SJeff Bonwick 	txg = dmu_tx_get_txg(tx);
2590fa9e4066Sahrens 
2591e05725b1Sbonwick 	lp = &ztest_shared->zs_sync_lock[b];
2592fa9e4066Sahrens 	(void) mutex_lock(lp);
2593fa9e4066Sahrens 
2594e05725b1Sbonwick 	wbt->bt_objset = dmu_objset_id(os);
2595e05725b1Sbonwick 	wbt->bt_object = ZTEST_DIROBJ;
2596e05725b1Sbonwick 	wbt->bt_offset = off;
2597f4a72450SJeff Bonwick 	wbt->bt_txg = txg;
2598e05725b1Sbonwick 	wbt->bt_thread = za->za_instance;
2599e05725b1Sbonwick 	wbt->bt_seq = ztest_shared->zs_seq[b]++;	/* protected by lp */
2600fa9e4066Sahrens 
2601f4a72450SJeff Bonwick 	/*
2602f4a72450SJeff Bonwick 	 * Occasionally, write an all-zero block to test the behavior
2603f4a72450SJeff Bonwick 	 * of blocks that compress into holes.
2604f4a72450SJeff Bonwick 	 */
2605f4a72450SJeff Bonwick 	if (off != -1ULL && ztest_random(8) == 0)
2606f4a72450SJeff Bonwick 		bzero(wbt, btsize);
2607f4a72450SJeff Bonwick 
2608fa9e4066Sahrens 	if (off == -1ULL) {
2609e05725b1Sbonwick 		dmu_object_info_t *doi = &za->za_doi;
2610e05725b1Sbonwick 		char *dboff;
26111934e92fSmaybee 
2612e05725b1Sbonwick 		VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2613e05725b1Sbonwick 		za->za_dbuf = db;
2614e05725b1Sbonwick 		dmu_object_info_from_db(db, doi);
2615e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2616e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size, >=, btsize);
2617e05725b1Sbonwick 		ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2618e05725b1Sbonwick 		dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2619e05725b1Sbonwick 		bcopy(dboff, rbt, btsize);
2620e05725b1Sbonwick 		if (rbt->bt_objset != 0) {
2621e05725b1Sbonwick 			ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2622e05725b1Sbonwick 			ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2623e05725b1Sbonwick 			ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2624e05725b1Sbonwick 			ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2625fa9e4066Sahrens 		}
26261934e92fSmaybee 		if (ztest_random(10) == 0) {
2627e05725b1Sbonwick 			int newsize = (ztest_random(db->db_size /
2628e05725b1Sbonwick 			    btsize) + 1) * btsize;
26291934e92fSmaybee 
2630e05725b1Sbonwick 			ASSERT3U(newsize, >=, btsize);
26311934e92fSmaybee 			ASSERT3U(newsize, <=, db->db_size);
2632e05725b1Sbonwick 			VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2633e05725b1Sbonwick 			dboff = (char *)db->db_data + newsize - btsize;
26341934e92fSmaybee 		}
2635fa9e4066Sahrens 		dmu_buf_will_dirty(db, tx);
2636e05725b1Sbonwick 		bcopy(wbt, dboff, btsize);
2637ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
2638e05725b1Sbonwick 		za->za_dbuf = NULL;
2639e05725b1Sbonwick 	} else if (do_free) {
2640e05725b1Sbonwick 		VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
26412fdbea25SAleksandr Guzovskiy 	} else if (abuf == NULL) {
2642e05725b1Sbonwick 		dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
26432fdbea25SAleksandr Guzovskiy 	} else {
26442fdbea25SAleksandr Guzovskiy 		bcopy(wbt, abuf->b_data, btsize);
26452fdbea25SAleksandr Guzovskiy 		dmu_assign_arcbuf(bonus_db, off, abuf, tx);
26462fdbea25SAleksandr Guzovskiy 		dmu_buf_rele(bonus_db, FTAG);
2647fa9e4066Sahrens 	}
2648fa9e4066Sahrens 
2649fa9e4066Sahrens 	(void) mutex_unlock(lp);
2650fa9e4066Sahrens 
2651e05725b1Sbonwick 	if (ztest_random(1000) == 0)
2652fa9e4066Sahrens 		(void) poll(NULL, 0, 1); /* open dn_notxholds window */
2653fa9e4066Sahrens 
2654fa9e4066Sahrens 	dmu_tx_commit(tx);
2655fa9e4066Sahrens 
2656e05725b1Sbonwick 	if (ztest_random(10000) == 0)
2657f4a72450SJeff Bonwick 		txg_wait_synced(dmu_objset_pool(os), txg);
2658fa9e4066Sahrens 
2659e14bb325SJeff Bonwick 	if (off == -1ULL || do_free)
2660e05725b1Sbonwick 		return;
2661fa9e4066Sahrens 
2662e05725b1Sbonwick 	if (ztest_random(2) != 0)
2663e05725b1Sbonwick 		return;
2664e05725b1Sbonwick 
2665e05725b1Sbonwick 	/*
2666e05725b1Sbonwick 	 * dmu_sync() the block we just wrote.
2667e05725b1Sbonwick 	 */
2668fa9e4066Sahrens 	(void) mutex_lock(lp);
2669e05725b1Sbonwick 
2670c5c6ffa0Smaybee 	blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2671e05725b1Sbonwick 	error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2672e05725b1Sbonwick 	za->za_dbuf = db;
2673c5c6ffa0Smaybee 	if (error) {
2674c5c6ffa0Smaybee 		(void) mutex_unlock(lp);
2675e05725b1Sbonwick 		return;
2676c5c6ffa0Smaybee 	}
2677c5c6ffa0Smaybee 	blkoff = off - blkoff;
2678f4a72450SJeff Bonwick 	error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2679c5c6ffa0Smaybee 	dmu_buf_rele(db, FTAG);
2680e05725b1Sbonwick 	za->za_dbuf = NULL;
2681e05725b1Sbonwick 
26822fdbea25SAleksandr Guzovskiy 	if (error) {
2683fa9e4066Sahrens 		(void) mutex_unlock(lp);
2684e05725b1Sbonwick 		return;
26852fdbea25SAleksandr Guzovskiy 	}
2686fa9e4066Sahrens 
26872fdbea25SAleksandr Guzovskiy 	if (blk.blk_birth == 0)	{	/* concurrent free */
26882fdbea25SAleksandr Guzovskiy 		(void) mutex_unlock(lp);
2689e05725b1Sbonwick 		return;
26902fdbea25SAleksandr Guzovskiy 	}
2691e05725b1Sbonwick 
2692c5c6ffa0Smaybee 	txg_suspend(dmu_objset_pool(os));
2693fa9e4066Sahrens 
26942fdbea25SAleksandr Guzovskiy 	(void) mutex_unlock(lp);
26952fdbea25SAleksandr Guzovskiy 
2696fa9e4066Sahrens 	ASSERT(blk.blk_fill == 1);
2697fa9e4066Sahrens 	ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2698fa9e4066Sahrens 	ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2699fa9e4066Sahrens 	ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2700fa9e4066Sahrens 
2701fa9e4066Sahrens 	/*
2702e05725b1Sbonwick 	 * Read the block that dmu_sync() returned to make sure its contents
2703e05725b1Sbonwick 	 * match what we wrote.  We do this while still txg_suspend()ed
2704e05725b1Sbonwick 	 * to ensure that the block can't be reused before we read it.
2705fa9e4066Sahrens 	 */
2706ea8dc4b6Seschrock 	zb.zb_objset = dmu_objset_id(os);
2707ea8dc4b6Seschrock 	zb.zb_object = ZTEST_DIROBJ;
2708ea8dc4b6Seschrock 	zb.zb_level = 0;
2709ea8dc4b6Seschrock 	zb.zb_blkid = off / bs;
2710e05725b1Sbonwick 	error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2711e05725b1Sbonwick 	    NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2712e05725b1Sbonwick 	ASSERT3U(error, ==, 0);
2713fa9e4066Sahrens 
2714fa9e4066Sahrens 	txg_resume(dmu_objset_pool(os));
2715fa9e4066Sahrens 
2716e05725b1Sbonwick 	bcopy(&iobuf[blkoff], rbt, btsize);
2717fa9e4066Sahrens 
2718e05725b1Sbonwick 	if (rbt->bt_objset == 0)		/* concurrent free */
2719e05725b1Sbonwick 		return;
27200e34b6a7Sbonwick 
2721f4a72450SJeff Bonwick 	if (wbt->bt_objset == 0)		/* all-zero overwrite */
2722f4a72450SJeff Bonwick 		return;
2723f4a72450SJeff Bonwick 
2724e05725b1Sbonwick 	ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2725e05725b1Sbonwick 	ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2726e05725b1Sbonwick 	ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2727fa9e4066Sahrens 
2728fa9e4066Sahrens 	/*
2729e05725b1Sbonwick 	 * The semantic of dmu_sync() is that we always push the most recent
2730e05725b1Sbonwick 	 * version of the data, so in the face of concurrent updates we may
2731fa9e4066Sahrens 	 * see a newer version of the block.  That's OK.
2732fa9e4066Sahrens 	 */
2733e05725b1Sbonwick 	ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2734e05725b1Sbonwick 	if (rbt->bt_thread == wbt->bt_thread)
2735e05725b1Sbonwick 		ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2736fa9e4066Sahrens 	else
2737e05725b1Sbonwick 		ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2738fa9e4066Sahrens }
2739fa9e4066Sahrens 
2740fa9e4066Sahrens /*
2741fa9e4066Sahrens  * Verify that zap_{create,destroy,add,remove,update} work as expected.
2742fa9e4066Sahrens  */
2743fa9e4066Sahrens #define	ZTEST_ZAP_MIN_INTS	1
2744fa9e4066Sahrens #define	ZTEST_ZAP_MAX_INTS	4
2745fa9e4066Sahrens #define	ZTEST_ZAP_MAX_PROPS	1000
2746fa9e4066Sahrens 
2747fa9e4066Sahrens void
2748fa9e4066Sahrens ztest_zap(ztest_args_t *za)
2749fa9e4066Sahrens {
2750fa9e4066Sahrens 	objset_t *os = za->za_os;
2751fa9e4066Sahrens 	uint64_t object;
2752fa9e4066Sahrens 	uint64_t txg, last_txg;
2753fa9e4066Sahrens 	uint64_t value[ZTEST_ZAP_MAX_INTS];
2754fa9e4066Sahrens 	uint64_t zl_ints, zl_intsize, prop;
2755fa9e4066Sahrens 	int i, ints;
2756fa9e4066Sahrens 	dmu_tx_t *tx;
2757fa9e4066Sahrens 	char propname[100], txgname[100];
2758fa9e4066Sahrens 	int error;
2759fa9e4066Sahrens 	char osname[MAXNAMELEN];
2760fa9e4066Sahrens 	char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2761fa9e4066Sahrens 
2762fa9e4066Sahrens 	dmu_objset_name(os, osname);
2763fa9e4066Sahrens 
2764fa9e4066Sahrens 	/*
2765fa9e4066Sahrens 	 * Create a new object if necessary, and record it in the directory.
2766fa9e4066Sahrens 	 */
2767ea8dc4b6Seschrock 	VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
27687bfdf011SNeil Perrin 	    sizeof (uint64_t), &object, DMU_READ_PREFETCH));
2769fa9e4066Sahrens 
2770fa9e4066Sahrens 	if (object == 0) {
2771fa9e4066Sahrens 		tx = dmu_tx_create(os);
2772fa9e4066Sahrens 		dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2773fa9e4066Sahrens 		    sizeof (uint64_t));
2774ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2775fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2776fa9e4066Sahrens 		if (error) {
2777fa9e4066Sahrens 			ztest_record_enospc("create zap test obj");
2778fa9e4066Sahrens 			dmu_tx_abort(tx);
2779fa9e4066Sahrens 			return;
2780fa9e4066Sahrens 		}
2781fa9e4066Sahrens 		object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2782fa9e4066Sahrens 		if (error) {
2783fa9e4066Sahrens 			fatal(0, "zap_create('%s', %llu) = %d",
2784fa9e4066Sahrens 			    osname, object, error);
2785fa9e4066Sahrens 		}
2786fa9e4066Sahrens 		ASSERT(object != 0);
2787fa9e4066Sahrens 		dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2788fa9e4066Sahrens 		    sizeof (uint64_t), &object, tx);
2789fa9e4066Sahrens 		/*
2790fa9e4066Sahrens 		 * Generate a known hash collision, and verify that
2791fa9e4066Sahrens 		 * we can lookup and remove both entries.
2792fa9e4066Sahrens 		 */
2793fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2794fa9e4066Sahrens 			value[i] = i;
2795fa9e4066Sahrens 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2796fa9e4066Sahrens 			    1, &value[i], tx);
2797fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2798fa9e4066Sahrens 		}
2799fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2800fa9e4066Sahrens 			error = zap_add(os, object, hc[i], sizeof (uint64_t),
2801fa9e4066Sahrens 			    1, &value[i], tx);
2802fa9e4066Sahrens 			ASSERT3U(error, ==, EEXIST);
2803fa9e4066Sahrens 			error = zap_length(os, object, hc[i],
2804fa9e4066Sahrens 			    &zl_intsize, &zl_ints);
2805fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2806fa9e4066Sahrens 			ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2807fa9e4066Sahrens 			ASSERT3U(zl_ints, ==, 1);
2808fa9e4066Sahrens 		}
2809fa9e4066Sahrens 		for (i = 0; i < 2; i++) {
2810fa9e4066Sahrens 			error = zap_remove(os, object, hc[i], tx);
2811fa9e4066Sahrens 			ASSERT3U(error, ==, 0);
2812fa9e4066Sahrens 		}
2813fa9e4066Sahrens 
2814fa9e4066Sahrens 		dmu_tx_commit(tx);
2815fa9e4066Sahrens 	}
2816fa9e4066Sahrens 
2817fa9e4066Sahrens 	ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2818fa9e4066Sahrens 
2819fa9e4066Sahrens 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2820fa9e4066Sahrens 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2821fa9e4066Sahrens 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2822fa9e4066Sahrens 	bzero(value, sizeof (value));
2823fa9e4066Sahrens 	last_txg = 0;
2824fa9e4066Sahrens 
2825fa9e4066Sahrens 	/*
2826fa9e4066Sahrens 	 * If these zap entries already exist, validate their contents.
2827fa9e4066Sahrens 	 */
2828fa9e4066Sahrens 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2829fa9e4066Sahrens 	if (error == 0) {
2830fa9e4066Sahrens 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2831fa9e4066Sahrens 		ASSERT3U(zl_ints, ==, 1);
2832fa9e4066Sahrens 
2833e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2834e05725b1Sbonwick 		    zl_ints, &last_txg) == 0);
2835fa9e4066Sahrens 
2836e05725b1Sbonwick 		VERIFY(zap_length(os, object, propname, &zl_intsize,
2837e05725b1Sbonwick 		    &zl_ints) == 0);
2838fa9e4066Sahrens 
2839fa9e4066Sahrens 		ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2840fa9e4066Sahrens 		ASSERT3U(zl_ints, ==, ints);
2841fa9e4066Sahrens 
2842e05725b1Sbonwick 		VERIFY(zap_lookup(os, object, propname, zl_intsize,
2843e05725b1Sbonwick 		    zl_ints, value) == 0);
2844fa9e4066Sahrens 
2845fa9e4066Sahrens 		for (i = 0; i < ints; i++) {
2846fa9e4066Sahrens 			ASSERT3U(value[i], ==, last_txg + object + i);
2847fa9e4066Sahrens 		}
2848fa9e4066Sahrens 	} else {
2849fa9e4066Sahrens 		ASSERT3U(error, ==, ENOENT);
2850fa9e4066Sahrens 	}
2851fa9e4066Sahrens 
2852fa9e4066Sahrens 	/*
2853fa9e4066Sahrens 	 * Atomically update two entries in our zap object.
2854fa9e4066Sahrens 	 * The first is named txg_%llu, and contains the txg
2855fa9e4066Sahrens 	 * in which the property was last updated.  The second
2856fa9e4066Sahrens 	 * is named prop_%llu, and the nth element of its value
2857fa9e4066Sahrens 	 * should be txg + object + n.
2858fa9e4066Sahrens 	 */
2859fa9e4066Sahrens 	tx = dmu_tx_create(os);
2860ea8dc4b6Seschrock 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2861fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2862fa9e4066Sahrens 	if (error) {
2863fa9e4066Sahrens 		ztest_record_enospc("create zap entry");
2864fa9e4066Sahrens 		dmu_tx_abort(tx);
2865fa9e4066Sahrens 		return;
2866fa9e4066Sahrens 	}
2867fa9e4066Sahrens 	txg = dmu_tx_get_txg(tx);
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 	if (last_txg > txg)
2870e05725b1Sbonwick 		fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2871fa9e4066Sahrens 
2872fa9e4066Sahrens 	for (i = 0; i < ints; i++)
2873fa9e4066Sahrens 		value[i] = txg + object + i;
2874fa9e4066Sahrens 
2875e05725b1Sbonwick 	error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2876fa9e4066Sahrens 	if (error)
2877fa9e4066Sahrens 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2878fa9e4066Sahrens 		    osname, object, txgname, error);
2879fa9e4066Sahrens 
2880fa9e4066Sahrens 	error = zap_update(os, object, propname, sizeof (uint64_t),
2881fa9e4066Sahrens 	    ints, value, tx);
2882fa9e4066Sahrens 	if (error)
2883fa9e4066Sahrens 		fatal(0, "zap_update('%s', %llu, '%s') = %d",
2884fa9e4066Sahrens 		    osname, object, propname, error);
2885fa9e4066Sahrens 
2886fa9e4066Sahrens 	dmu_tx_commit(tx);
2887fa9e4066Sahrens 
2888fa9e4066Sahrens 	/*
2889fa9e4066Sahrens 	 * Remove a random pair of entries.
2890fa9e4066Sahrens 	 */
2891fa9e4066Sahrens 	prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2892fa9e4066Sahrens 	(void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2893fa9e4066Sahrens 	(void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2894fa9e4066Sahrens 
2895fa9e4066Sahrens 	error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2896fa9e4066Sahrens 
2897fa9e4066Sahrens 	if (error == ENOENT)
2898e05725b1Sbonwick 		return;
2899fa9e4066Sahrens 
2900fa9e4066Sahrens 	ASSERT3U(error, ==, 0);
2901fa9e4066Sahrens 
2902fa9e4066Sahrens 	tx = dmu_tx_create(os);
2903ea8dc4b6Seschrock 	dmu_tx_hold_zap(tx, object, TRUE, NULL);
2904fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2905fa9e4066Sahrens 	if (error) {
2906fa9e4066Sahrens 		ztest_record_enospc("remove zap entry");
2907fa9e4066Sahrens 		dmu_tx_abort(tx);
2908fa9e4066Sahrens 		return;
2909fa9e4066Sahrens 	}
2910fa9e4066Sahrens 	error = zap_remove(os, object, txgname, tx);
2911fa9e4066Sahrens 	if (error)
2912fa9e4066Sahrens 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2913fa9e4066Sahrens 		    osname, object, txgname, error);
2914fa9e4066Sahrens 
2915fa9e4066Sahrens 	error = zap_remove(os, object, propname, tx);
2916fa9e4066Sahrens 	if (error)
2917fa9e4066Sahrens 		fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2918fa9e4066Sahrens 		    osname, object, propname, error);
2919fa9e4066Sahrens 
2920fa9e4066Sahrens 	dmu_tx_commit(tx);
2921fa9e4066Sahrens 
2922fa9e4066Sahrens 	/*
2923fa9e4066Sahrens 	 * Once in a while, destroy the object.
2924fa9e4066Sahrens 	 */
2925e05725b1Sbonwick 	if (ztest_random(1000) != 0)
2926fa9e4066Sahrens 		return;
2927fa9e4066Sahrens 
2928fa9e4066Sahrens 	tx = dmu_tx_create(os);
2929fa9e4066Sahrens 	dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2930fa9e4066Sahrens 	dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2931fa9e4066Sahrens 	error = dmu_tx_assign(tx, TXG_WAIT);
2932fa9e4066Sahrens 	if (error) {
2933fa9e4066Sahrens 		ztest_record_enospc("destroy zap object");
2934fa9e4066Sahrens 		dmu_tx_abort(tx);
2935fa9e4066Sahrens 		return;
2936fa9e4066Sahrens 	}
2937fa9e4066Sahrens 	error = zap_destroy(os, object, tx);
2938fa9e4066Sahrens 	if (error)
2939fa9e4066Sahrens 		fatal(0, "zap_destroy('%s', %llu) = %d",
2940fa9e4066Sahrens 		    osname, object, error);
2941fa9e4066Sahrens 	object = 0;
2942fa9e4066Sahrens 	dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2943fa9e4066Sahrens 	    &object, tx);
2944fa9e4066Sahrens 	dmu_tx_commit(tx);
2945fa9e4066Sahrens }
2946fa9e4066Sahrens 
2947fa9e4066Sahrens void
2948fa9e4066Sahrens ztest_zap_parallel(ztest_args_t *za)
2949fa9e4066Sahrens {
2950fa9e4066Sahrens 	objset_t *os = za->za_os;
2951fa9e4066Sahrens 	uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2952fa9e4066Sahrens 	dmu_tx_t *tx;
2953fa9e4066Sahrens 	int i, namelen, error;
2954fa9e4066Sahrens 	char name[20], string_value[20];
2955fa9e4066Sahrens 	void *data;
2956fa9e4066Sahrens 
2957fa9e4066Sahrens 	/*
2958fa9e4066Sahrens 	 * Generate a random name of the form 'xxx.....' where each
2959fa9e4066Sahrens 	 * x is a random printable character and the dots are dots.
2960fa9e4066Sahrens 	 * There are 94 such characters, and the name length goes from
2961fa9e4066Sahrens 	 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2962fa9e4066Sahrens 	 */
2963fa9e4066Sahrens 	namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2964fa9e4066Sahrens 
2965fa9e4066Sahrens 	for (i = 0; i < 3; i++)
2966fa9e4066Sahrens 		name[i] = '!' + ztest_random('~' - '!' + 1);
2967fa9e4066Sahrens 	for (; i < namelen - 1; i++)
2968fa9e4066Sahrens 		name[i] = '.';
2969fa9e4066Sahrens 	name[i] = '\0';
2970fa9e4066Sahrens 
2971fa9e4066Sahrens 	if (ztest_random(2) == 0)
2972fa9e4066Sahrens 		object = ZTEST_MICROZAP_OBJ;
2973fa9e4066Sahrens 	else
2974fa9e4066Sahrens 		object = ZTEST_FATZAP_OBJ;
2975fa9e4066Sahrens 
2976fa9e4066Sahrens 	if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2977fa9e4066Sahrens 		wsize = sizeof (txg);
2978fa9e4066Sahrens 		wc = 1;
2979fa9e4066Sahrens 		data = &txg;
2980fa9e4066Sahrens 	} else {
2981fa9e4066Sahrens 		wsize = 1;
2982fa9e4066Sahrens 		wc = namelen;
2983fa9e4066Sahrens 		data = string_value;
2984fa9e4066Sahrens 	}
2985fa9e4066Sahrens 
2986fa9e4066Sahrens 	count = -1ULL;
2987fa9e4066Sahrens 	VERIFY(zap_count(os, object, &count) == 0);
2988fa9e4066Sahrens 	ASSERT(count != -1ULL);
2989fa9e4066Sahrens 
2990fa9e4066Sahrens 	/*
2991fa9e4066Sahrens 	 * Select an operation: length, lookup, add, update, remove.
2992fa9e4066Sahrens 	 */
2993fa9e4066Sahrens 	i = ztest_random(5);
2994fa9e4066Sahrens 
2995fa9e4066Sahrens 	if (i >= 2) {
2996fa9e4066Sahrens 		tx = dmu_tx_create(os);
2997ea8dc4b6Seschrock 		dmu_tx_hold_zap(tx, object, TRUE, NULL);
2998fa9e4066Sahrens 		error = dmu_tx_assign(tx, TXG_WAIT);
2999fa9e4066Sahrens 		if (error) {
3000fa9e4066Sahrens 			ztest_record_enospc("zap parallel");
3001fa9e4066Sahrens 			dmu_tx_abort(tx);
3002fa9e4066Sahrens 			return;
3003fa9e4066Sahrens 		}
3004fa9e4066Sahrens 		txg = dmu_tx_get_txg(tx);
3005fa9e4066Sahrens 		bcopy(name, string_value, namelen);
3006fa9e4066Sahrens 	} else {
3007fa9e4066Sahrens 		tx = NULL;
3008fa9e4066Sahrens 		txg = 0;
3009fa9e4066Sahrens 		bzero(string_value, namelen);
3010fa9e4066Sahrens 	}
3011fa9e4066Sahrens 
3012fa9e4066Sahrens 	switch (i) {
3013fa9e4066Sahrens 
3014fa9e4066Sahrens 	case 0:
3015fa9e4066Sahrens 		error = zap_length(os, object, name, &zl_wsize, &zl_wc);
3016fa9e4066Sahrens 		if (error == 0) {
3017fa9e4066Sahrens 			ASSERT3U(wsize, ==, zl_wsize);
3018fa9e4066Sahrens 			ASSERT3U(wc, ==, zl_wc);
3019fa9e4066Sahrens 		} else {
3020fa9e4066Sahrens 			ASSERT3U(error, ==, ENOENT);
3021fa9e4066Sahrens 		}
3022fa9e4066Sahrens 		break;
3023fa9e4066Sahrens 
3024fa9e4066Sahrens 	case 1:
3025fa9e4066Sahrens 		error = zap_lookup(os, object, name, wsize, wc, data);
3026fa9e4066Sahrens 		if (error == 0) {
3027fa9e4066Sahrens 			if (data == string_value &&
3028fa9e4066Sahrens 			    bcmp(name, data, namelen) != 0)
3029fa9e4066Sahrens 				fatal(0, "name '%s' != val '%s' len %d",
3030fa9e4066Sahrens 				    name, data, namelen);
3031fa9e4066Sahrens 		} else {
3032fa9e4066Sahrens 			ASSERT3U(error, ==, ENOENT);
3033fa9e4066Sahrens 		}
3034fa9e4066Sahrens 		break;
3035fa9e4066Sahrens 
3036fa9e4066Sahrens 	case 2:
3037fa9e4066Sahrens 		error = zap_add(os, object, name, wsize, wc, data, tx);
3038fa9e4066Sahrens 		ASSERT(error == 0 || error == EEXIST);
3039fa9e4066Sahrens 		break;
3040fa9e4066Sahrens 
3041fa9e4066Sahrens 	case 3:
3042e05725b1Sbonwick 		VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
3043fa9e4066Sahrens 		break;
3044fa9e4066Sahrens 
3045fa9e4066Sahrens 	case 4:
3046fa9e4066Sahrens 		error = zap_remove(os, object, name, tx);
3047fa9e4066Sahrens 		ASSERT(error == 0 || error == ENOENT);
3048fa9e4066Sahrens 		break;
3049fa9e4066Sahrens 	}
3050fa9e4066Sahrens 
3051fa9e4066Sahrens 	if (tx != NULL)
3052fa9e4066Sahrens 		dmu_tx_commit(tx);
3053fa9e4066Sahrens }
3054fa9e4066Sahrens 
3055fa9e4066Sahrens void
3056fa9e4066Sahrens ztest_dsl_prop_get_set(ztest_args_t *za)
3057fa9e4066Sahrens {
3058fa9e4066Sahrens 	objset_t *os = za->za_os;
3059fa9e4066Sahrens 	int i, inherit;
3060fa9e4066Sahrens 	uint64_t value;
3061fa9e4066Sahrens 	const char *prop, *valname;
3062fa9e4066Sahrens 	char setpoint[MAXPATHLEN];
3063fa9e4066Sahrens 	char osname[MAXNAMELEN];
3064ea8dc4b6Seschrock 	int error;
3065fa9e4066Sahrens 
3066fa9e4066Sahrens 	(void) rw_rdlock(&ztest_shared->zs_name_lock);
3067fa9e4066Sahrens 
3068fa9e4066Sahrens 	dmu_objset_name(os, osname);
3069fa9e4066Sahrens 
3070fa9e4066Sahrens 	for (i = 0; i < 2; i++) {
3071fa9e4066Sahrens 		if (i == 0) {
3072fa9e4066Sahrens 			prop = "checksum";
3073fa9e4066Sahrens 			value = ztest_random_checksum();
3074fa9e4066Sahrens 			inherit = (value == ZIO_CHECKSUM_INHERIT);
3075fa9e4066Sahrens 		} else {
3076fa9e4066Sahrens 			prop = "compression";
3077fa9e4066Sahrens 			value = ztest_random_compress();
3078fa9e4066Sahrens 			inherit = (value == ZIO_COMPRESS_INHERIT);
3079fa9e4066Sahrens 		}
3080fa9e4066Sahrens 
3081ea8dc4b6Seschrock 		error = dsl_prop_set(osname, prop, sizeof (value),
3082ea8dc4b6Seschrock 		    !inherit, &value);
3083ea8dc4b6Seschrock 
3084ea8dc4b6Seschrock 		if (error == ENOSPC) {
3085ea8dc4b6Seschrock 			ztest_record_enospc("dsl_prop_set");
3086ea8dc4b6Seschrock 			break;
3087ea8dc4b6Seschrock 		}
3088ea8dc4b6Seschrock 
3089ea8dc4b6Seschrock 		ASSERT3U(error, ==, 0);
3090fa9e4066Sahrens 
3091fa9e4066Sahrens 		VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
3092fa9e4066Sahrens 		    1, &value, setpoint), ==, 0);
3093fa9e4066Sahrens 
3094fa9e4066Sahrens 		if (i == 0)
3095fa9e4066Sahrens 			valname = zio_checksum_table[value].ci_name;
3096fa9e4066Sahrens 		else
3097fa9e4066Sahrens 			valname = zio_compress_table[value].ci_name;
3098fa9e4066Sahrens 
3099fa9e4066Sahrens 		if (zopt_verbose >= 6) {
3100fa9e4066Sahrens 			(void) printf("%s %s = %s for '%s'\n",
3101fa9e4066Sahrens 			    osname, prop, valname, setpoint);
3102fa9e4066Sahrens 		}
3103fa9e4066Sahrens 	}
3104fa9e4066Sahrens 
3105fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
3106fa9e4066Sahrens }
3107fa9e4066Sahrens 
3108fa9e4066Sahrens /*
3109fa9e4066Sahrens  * Inject random faults into the on-disk data.
3110fa9e4066Sahrens  */
3111fa9e4066Sahrens void
3112fa9e4066Sahrens ztest_fault_inject(ztest_args_t *za)
3113fa9e4066Sahrens {
3114fa9e4066Sahrens 	int fd;
3115fa9e4066Sahrens 	uint64_t offset;
3116fa9e4066Sahrens 	uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
3117fa9e4066Sahrens 	uint64_t bad = 0x1990c0ffeedecade;
3118fa9e4066Sahrens 	uint64_t top, leaf;
3119fa9e4066Sahrens 	char path0[MAXPATHLEN];
3120fa9e4066Sahrens 	char pathrand[MAXPATHLEN];
3121fa9e4066Sahrens 	size_t fsize;
3122e05725b1Sbonwick 	spa_t *spa = za->za_spa;
3123fa9e4066Sahrens 	int bshift = SPA_MAXBLOCKSHIFT + 2;	/* don't scrog all labels */
3124fa9e4066Sahrens 	int iters = 1000;
3125e14bb325SJeff Bonwick 	int maxfaults = zopt_maxfaults;
3126e14bb325SJeff Bonwick 	vdev_t *vd0 = NULL;
3127ea8dc4b6Seschrock 	uint64_t guid0 = 0;
3128ea8dc4b6Seschrock 
3129e14bb325SJeff Bonwick 	ASSERT(leaves >= 1);
3130fa9e4066Sahrens 
3131fa9e4066Sahrens 	/*
3132e14bb325SJeff Bonwick 	 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
3133fa9e4066Sahrens 	 */
3134e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3135e14bb325SJeff Bonwick 
3136e14bb325SJeff Bonwick 	if (ztest_random(2) == 0) {
3137e14bb325SJeff Bonwick 		/*
3138e14bb325SJeff Bonwick 		 * Inject errors on a normal data device.
3139e14bb325SJeff Bonwick 		 */
3140fa9e4066Sahrens 		top = ztest_random(spa->spa_root_vdev->vdev_children);
3141fa9e4066Sahrens 		leaf = ztest_random(leaves);
3142fa9e4066Sahrens 
3143fa9e4066Sahrens 		/*
3144e14bb325SJeff Bonwick 		 * Generate paths to the first leaf in this top-level vdev,
3145fa9e4066Sahrens 		 * and to the random leaf we selected.  We'll induce transient
3146e14bb325SJeff Bonwick 		 * write failures and random online/offline activity on leaf 0,
3147fa9e4066Sahrens 		 * and we'll write random garbage to the randomly chosen leaf.
3148fa9e4066Sahrens 		 */
3149e14bb325SJeff Bonwick 		(void) snprintf(path0, sizeof (path0), ztest_dev_template,
3150e14bb325SJeff Bonwick 		    zopt_dir, zopt_pool, top * leaves + 0);
3151e14bb325SJeff Bonwick 		(void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
3152e14bb325SJeff Bonwick 		    zopt_dir, zopt_pool, top * leaves + leaf);
3153e14bb325SJeff Bonwick 
3154e14bb325SJeff Bonwick 		vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
3155e14bb325SJeff Bonwick 		if (vd0 != NULL && maxfaults != 1) {
3156e14bb325SJeff Bonwick 			/*
3157e14bb325SJeff Bonwick 			 * Make vd0 explicitly claim to be unreadable,
3158e14bb325SJeff Bonwick 			 * or unwriteable, or reach behind its back
3159e14bb325SJeff Bonwick 			 * and close the underlying fd.  We can do this if
3160e14bb325SJeff Bonwick 			 * maxfaults == 0 because we'll fail and reexecute,
3161e14bb325SJeff Bonwick 			 * and we can do it if maxfaults >= 2 because we'll
3162e14bb325SJeff Bonwick 			 * have enough redundancy.  If maxfaults == 1, the
3163e14bb325SJeff Bonwick 			 * combination of this with injection of random data
3164e14bb325SJeff Bonwick 			 * corruption below exceeds the pool's fault tolerance.
3165e14bb325SJeff Bonwick 			 */
3166e14bb325SJeff Bonwick 			vdev_file_t *vf = vd0->vdev_tsd;
3167e14bb325SJeff Bonwick 
3168e14bb325SJeff Bonwick 			if (vf != NULL && ztest_random(3) == 0) {
3169e14bb325SJeff Bonwick 				(void) close(vf->vf_vnode->v_fd);
3170e14bb325SJeff Bonwick 				vf->vf_vnode->v_fd = -1;
3171e14bb325SJeff Bonwick 			} else if (ztest_random(2) == 0) {
3172e14bb325SJeff Bonwick 				vd0->vdev_cant_read = B_TRUE;
3173e14bb325SJeff Bonwick 			} else {
3174e14bb325SJeff Bonwick 				vd0->vdev_cant_write = B_TRUE;
3175e14bb325SJeff Bonwick 			}
3176e14bb325SJeff Bonwick 			guid0 = vd0->vdev_guid;
3177e14bb325SJeff Bonwick 		}
3178e14bb325SJeff Bonwick 	} else {
3179e14bb325SJeff Bonwick 		/*
3180e14bb325SJeff Bonwick 		 * Inject errors on an l2cache device.
3181e14bb325SJeff Bonwick 		 */
3182e14bb325SJeff Bonwick 		spa_aux_vdev_t *sav = &spa->spa_l2cache;
3183e14bb325SJeff Bonwick 
3184e14bb325SJeff Bonwick 		if (sav->sav_count == 0) {
3185e14bb325SJeff Bonwick 			spa_config_exit(spa, SCL_STATE, FTAG);
3186e14bb325SJeff Bonwick 			return;
3187e14bb325SJeff Bonwick 		}
3188e14bb325SJeff Bonwick 		vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
3189e14bb325SJeff Bonwick 		guid0 = vd0->vdev_guid;
3190e14bb325SJeff Bonwick 		(void) strcpy(path0, vd0->vdev_path);
3191e14bb325SJeff Bonwick 		(void) strcpy(pathrand, vd0->vdev_path);
3192e14bb325SJeff Bonwick 
3193e14bb325SJeff Bonwick 		leaf = 0;
3194e14bb325SJeff Bonwick 		leaves = 1;
3195e14bb325SJeff Bonwick 		maxfaults = INT_MAX;	/* no limit on cache devices */
3196e14bb325SJeff Bonwick 	}
3197fa9e4066Sahrens 
3198e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
3199fa9e4066Sahrens 
3200e14bb325SJeff Bonwick 	if (maxfaults == 0)
3201e14bb325SJeff Bonwick 		return;
3202ea8dc4b6Seschrock 
3203ea8dc4b6Seschrock 	/*
3204ea8dc4b6Seschrock 	 * If we can tolerate two or more faults, randomly online/offline vd0.
3205ea8dc4b6Seschrock 	 */
3206e14bb325SJeff Bonwick 	if (maxfaults >= 2 && guid0 != 0) {
32078ad4d6ddSJeff Bonwick 		if (ztest_random(10) < 6) {
32088ad4d6ddSJeff Bonwick 			int flags = (ztest_random(2) == 0 ?
32098ad4d6ddSJeff Bonwick 			    ZFS_OFFLINE_TEMPORARY : 0);
32108ad4d6ddSJeff Bonwick 			VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
32118ad4d6ddSJeff Bonwick 		} else {
32128ad4d6ddSJeff Bonwick 			(void) vdev_online(spa, guid0, 0, NULL);
32138ad4d6ddSJeff Bonwick 		}
3214fa9e4066Sahrens 	}
3215fa9e4066Sahrens 
3216fa9e4066Sahrens 	/*
3217ea8dc4b6Seschrock 	 * We have at least single-fault tolerance, so inject data corruption.
3218fa9e4066Sahrens 	 */
3219fa9e4066Sahrens 	fd = open(pathrand, O_RDWR);
3220fa9e4066Sahrens 
3221fa9e4066Sahrens 	if (fd == -1)	/* we hit a gap in the device namespace */
3222fa9e4066Sahrens 		return;
3223fa9e4066Sahrens 
3224fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
3225fa9e4066Sahrens 
3226fa9e4066Sahrens 	while (--iters != 0) {
3227fa9e4066Sahrens 		offset = ztest_random(fsize / (leaves << bshift)) *
3228fa9e4066Sahrens 		    (leaves << bshift) + (leaf << bshift) +
3229fa9e4066Sahrens 		    (ztest_random(1ULL << (bshift - 1)) & -8ULL);
3230fa9e4066Sahrens 
3231fa9e4066Sahrens 		if (offset >= fsize)
3232fa9e4066Sahrens 			continue;
3233fa9e4066Sahrens 
3234fa9e4066Sahrens 		if (zopt_verbose >= 6)
3235fa9e4066Sahrens 			(void) printf("injecting bad word into %s,"
3236fa9e4066Sahrens 			    " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
3237fa9e4066Sahrens 
3238fa9e4066Sahrens 		if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
3239fa9e4066Sahrens 			fatal(1, "can't inject bad word at 0x%llx in %s",
3240fa9e4066Sahrens 			    offset, pathrand);
3241fa9e4066Sahrens 	}
3242fa9e4066Sahrens 
3243fa9e4066Sahrens 	(void) close(fd);
3244fa9e4066Sahrens }
3245fa9e4066Sahrens 
3246fa9e4066Sahrens /*
3247fa9e4066Sahrens  * Scrub the pool.
3248fa9e4066Sahrens  */
3249fa9e4066Sahrens void
3250fa9e4066Sahrens ztest_scrub(ztest_args_t *za)
3251fa9e4066Sahrens {
3252e05725b1Sbonwick 	spa_t *spa = za->za_spa;
3253fa9e4066Sahrens 
3254088f3894Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
3255fa9e4066Sahrens 	(void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
3256088f3894Sahrens 	(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
3257fa9e4066Sahrens }
3258fa9e4066Sahrens 
3259fa9e4066Sahrens /*
3260fa9e4066Sahrens  * Rename the pool to a different name and then rename it back.
3261fa9e4066Sahrens  */
3262fa9e4066Sahrens void
3263fa9e4066Sahrens ztest_spa_rename(ztest_args_t *za)
3264fa9e4066Sahrens {
3265fa9e4066Sahrens 	char *oldname, *newname;
3266fa9e4066Sahrens 	int error;
3267fa9e4066Sahrens 	spa_t *spa;
3268fa9e4066Sahrens 
3269fa9e4066Sahrens 	(void) rw_wrlock(&ztest_shared->zs_name_lock);
3270fa9e4066Sahrens 
3271fa9e4066Sahrens 	oldname = za->za_pool;
3272fa9e4066Sahrens 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
3273fa9e4066Sahrens 	(void) strcpy(newname, oldname);
3274fa9e4066Sahrens 	(void) strcat(newname, "_tmp");
3275fa9e4066Sahrens 
3276fa9e4066Sahrens 	/*
3277fa9e4066Sahrens 	 * Do the rename
3278fa9e4066Sahrens 	 */
3279fa9e4066Sahrens 	error = spa_rename(oldname, newname);
3280fa9e4066Sahrens 	if (error)
3281fa9e4066Sahrens 		fatal(0, "spa_rename('%s', '%s') = %d", oldname,
3282fa9e4066Sahrens 		    newname, error);
3283fa9e4066Sahrens 
3284fa9e4066Sahrens 	/*
3285fa9e4066Sahrens 	 * Try to open it under the old name, which shouldn't exist
3286fa9e4066Sahrens 	 */
3287fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
3288fa9e4066Sahrens 	if (error != ENOENT)
3289fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
3290fa9e4066Sahrens 
3291fa9e4066Sahrens 	/*
3292fa9e4066Sahrens 	 * Open it under the new name and make sure it's still the same spa_t.
3293fa9e4066Sahrens 	 */
3294fa9e4066Sahrens 	error = spa_open(newname, &spa, FTAG);
3295fa9e4066Sahrens 	if (error != 0)
3296fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
3297fa9e4066Sahrens 
3298e05725b1Sbonwick 	ASSERT(spa == za->za_spa);
3299fa9e4066Sahrens 	spa_close(spa, FTAG);
3300fa9e4066Sahrens 
3301fa9e4066Sahrens 	/*
3302fa9e4066Sahrens 	 * Rename it back to the original
3303fa9e4066Sahrens 	 */
3304fa9e4066Sahrens 	error = spa_rename(newname, oldname);
3305fa9e4066Sahrens 	if (error)
3306fa9e4066Sahrens 		fatal(0, "spa_rename('%s', '%s') = %d", newname,
3307fa9e4066Sahrens 		    oldname, error);
3308fa9e4066Sahrens 
3309fa9e4066Sahrens 	/*
3310fa9e4066Sahrens 	 * Make sure it can still be opened
3311fa9e4066Sahrens 	 */
3312fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
3313fa9e4066Sahrens 	if (error != 0)
3314fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
3315fa9e4066Sahrens 
3316e05725b1Sbonwick 	ASSERT(spa == za->za_spa);
3317fa9e4066Sahrens 	spa_close(spa, FTAG);
3318fa9e4066Sahrens 
3319fa9e4066Sahrens 	umem_free(newname, strlen(newname) + 1);
3320fa9e4066Sahrens 
3321fa9e4066Sahrens 	(void) rw_unlock(&ztest_shared->zs_name_lock);
3322fa9e4066Sahrens }
3323fa9e4066Sahrens 
3324fa9e4066Sahrens 
3325fa9e4066Sahrens /*
3326fa9e4066Sahrens  * Completely obliterate one disk.
3327fa9e4066Sahrens  */
3328fa9e4066Sahrens static void
3329fa9e4066Sahrens ztest_obliterate_one_disk(uint64_t vdev)
3330fa9e4066Sahrens {
3331fa9e4066Sahrens 	int fd;
3332d80c45e0Sbonwick 	char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
3333fa9e4066Sahrens 	size_t fsize;
3334fa9e4066Sahrens 
3335fa9e4066Sahrens 	if (zopt_maxfaults < 2)
3336fa9e4066Sahrens 		return;
3337fa9e4066Sahrens 
3338fa9e4066Sahrens 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
3339d80c45e0Sbonwick 	(void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
3340fa9e4066Sahrens 
3341fa9e4066Sahrens 	fd = open(dev_name, O_RDWR);
3342fa9e4066Sahrens 
3343fa9e4066Sahrens 	if (fd == -1)
3344fa9e4066Sahrens 		fatal(1, "can't open %s", dev_name);
3345fa9e4066Sahrens 
3346fa9e4066Sahrens 	/*
3347fa9e4066Sahrens 	 * Determine the size.
3348fa9e4066Sahrens 	 */
3349fa9e4066Sahrens 	fsize = lseek(fd, 0, SEEK_END);
3350d80c45e0Sbonwick 
3351fa9e4066Sahrens 	(void) close(fd);
3352fa9e4066Sahrens 
3353fa9e4066Sahrens 	/*
3354d80c45e0Sbonwick 	 * Rename the old device to dev_name.old (useful for debugging).
3355fa9e4066Sahrens 	 */
3356d80c45e0Sbonwick 	VERIFY(rename(dev_name, copy_name) == 0);
3357fa9e4066Sahrens 
3358fa9e4066Sahrens 	/*
3359fa9e4066Sahrens 	 * Create a new one.
3360fa9e4066Sahrens 	 */
3361fa9e4066Sahrens 	VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
3362fa9e4066Sahrens 	VERIFY(ftruncate(fd, fsize) == 0);
3363fa9e4066Sahrens 	(void) close(fd);
3364fa9e4066Sahrens }
3365fa9e4066Sahrens 
3366fa9e4066Sahrens static void
3367fa9e4066Sahrens ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
3368fa9e4066Sahrens {
3369fa9e4066Sahrens 	char dev_name[MAXPATHLEN];
3370e14bb325SJeff Bonwick 	nvlist_t *root;
3371fa9e4066Sahrens 	int error;
3372ea8dc4b6Seschrock 	uint64_t guid;
3373ea8dc4b6Seschrock 	vdev_t *vd;
3374fa9e4066Sahrens 
3375fa9e4066Sahrens 	(void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
3376fa9e4066Sahrens 
3377fa9e4066Sahrens 	/*
3378fa9e4066Sahrens 	 * Build the nvlist describing dev_name.
3379fa9e4066Sahrens 	 */
3380e14bb325SJeff Bonwick 	root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
3381fa9e4066Sahrens 
3382e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3383ea8dc4b6Seschrock 	if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
3384ea8dc4b6Seschrock 		guid = 0;
3385ea8dc4b6Seschrock 	else
3386ea8dc4b6Seschrock 		guid = vd->vdev_guid;
3387e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_VDEV, FTAG);
3388ea8dc4b6Seschrock 	error = spa_vdev_attach(spa, guid, root, B_TRUE);
3389ecc2d604Sbonwick 	if (error != 0 &&
3390ecc2d604Sbonwick 	    error != EBUSY &&
3391ecc2d604Sbonwick 	    error != ENOTSUP &&
3392ecc2d604Sbonwick 	    error != ENODEV &&
3393ecc2d604Sbonwick 	    error != EDOM)
3394fa9e4066Sahrens 		fatal(0, "spa_vdev_attach(in-place) = %d", error);
3395fa9e4066Sahrens 
3396fa9e4066Sahrens 	nvlist_free(root);
3397fa9e4066Sahrens }
3398fa9e4066Sahrens 
3399fa9e4066Sahrens static void
3400fa9e4066Sahrens ztest_verify_blocks(char *pool)
3401fa9e4066Sahrens {
3402fa9e4066Sahrens 	int status;
3403fa9e4066Sahrens 	char zdb[MAXPATHLEN + MAXNAMELEN + 20];
3404fa9e4066Sahrens 	char zbuf[1024];
3405fa9e4066Sahrens 	char *bin;
34068654d025Sperrin 	char *ztest;
34078654d025Sperrin 	char *isa;
34088654d025Sperrin 	int isalen;
3409fa9e4066Sahrens 	FILE *fp;
3410fa9e4066Sahrens 
3411fa9e4066Sahrens 	(void) realpath(getexecname(), zdb);
3412fa9e4066Sahrens 
3413fa9e4066Sahrens 	/* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
3414fa9e4066Sahrens 	bin = strstr(zdb, "/usr/bin/");
34158654d025Sperrin 	ztest = strstr(bin, "/ztest");
34168654d025Sperrin 	isa = bin + 8;
34178654d025Sperrin 	isalen = ztest - isa;
34188654d025Sperrin 	isa = strdup(isa);
3419fa9e4066Sahrens 	/* LINTED */
34203a57275aSck153898 	(void) sprintf(bin,
34216365109dSVictor Latushkin 	    "/usr/sbin%.*s/zdb -bcc%s%s -U /tmp/zpool.cache %s",
34228654d025Sperrin 	    isalen,
34238654d025Sperrin 	    isa,
3424fa9e4066Sahrens 	    zopt_verbose >= 3 ? "s" : "",
3425fa9e4066Sahrens 	    zopt_verbose >= 4 ? "v" : "",
342688b7b0f2SMatthew Ahrens 	    pool);
34278654d025Sperrin 	free(isa);
3428fa9e4066Sahrens 
3429fa9e4066Sahrens 	if (zopt_verbose >= 5)
3430fa9e4066Sahrens 		(void) printf("Executing %s\n", strstr(zdb, "zdb "));
3431fa9e4066Sahrens 
3432fa9e4066Sahrens 	fp = popen(zdb, "r");
3433fa9e4066Sahrens 
3434fa9e4066Sahrens 	while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
3435fa9e4066Sahrens 		if (zopt_verbose >= 3)
3436fa9e4066Sahrens 			(void) printf("%s", zbuf);
3437fa9e4066Sahrens 
3438fa9e4066Sahrens 	status = pclose(fp);
3439fa9e4066Sahrens 
3440fa9e4066Sahrens 	if (status == 0)
3441fa9e4066Sahrens 		return;
3442fa9e4066Sahrens 
3443fa9e4066Sahrens 	ztest_dump_core = 0;
3444fa9e4066Sahrens 	if (WIFEXITED(status))
3445fa9e4066Sahrens 		fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
3446fa9e4066Sahrens 	else
3447fa9e4066Sahrens 		fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
3448fa9e4066Sahrens }
3449fa9e4066Sahrens 
3450fa9e4066Sahrens static void
3451fa9e4066Sahrens ztest_walk_pool_directory(char *header)
3452fa9e4066Sahrens {
3453fa9e4066Sahrens 	spa_t *spa = NULL;
3454fa9e4066Sahrens 
3455fa9e4066Sahrens 	if (zopt_verbose >= 6)
3456fa9e4066Sahrens 		(void) printf("%s\n", header);
3457fa9e4066Sahrens 
3458fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3459fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL)
3460fa9e4066Sahrens 		if (zopt_verbose >= 6)
3461fa9e4066Sahrens 			(void) printf("\t%s\n", spa_name(spa));
3462fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
3463fa9e4066Sahrens }
3464fa9e4066Sahrens 
3465fa9e4066Sahrens static void
3466fa9e4066Sahrens ztest_spa_import_export(char *oldname, char *newname)
3467fa9e4066Sahrens {
34688ad4d6ddSJeff Bonwick 	nvlist_t *config, *newconfig;
3469fa9e4066Sahrens 	uint64_t pool_guid;
3470fa9e4066Sahrens 	spa_t *spa;
3471fa9e4066Sahrens 	int error;
3472fa9e4066Sahrens 
3473fa9e4066Sahrens 	if (zopt_verbose >= 4) {
3474fa9e4066Sahrens 		(void) printf("import/export: old = %s, new = %s\n",
3475fa9e4066Sahrens 		    oldname, newname);
3476fa9e4066Sahrens 	}
3477fa9e4066Sahrens 
3478fa9e4066Sahrens 	/*
3479fa9e4066Sahrens 	 * Clean up from previous runs.
3480fa9e4066Sahrens 	 */
3481fa9e4066Sahrens 	(void) spa_destroy(newname);
3482fa9e4066Sahrens 
3483fa9e4066Sahrens 	/*
3484fa9e4066Sahrens 	 * Get the pool's configuration and guid.
3485fa9e4066Sahrens 	 */
3486fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
3487fa9e4066Sahrens 	if (error)
3488fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", oldname, error);
3489fa9e4066Sahrens 
34908ad4d6ddSJeff Bonwick 	/*
34918ad4d6ddSJeff Bonwick 	 * Kick off a scrub to tickle scrub/export races.
34928ad4d6ddSJeff Bonwick 	 */
34938ad4d6ddSJeff Bonwick 	if (ztest_random(2) == 0)
34948ad4d6ddSJeff Bonwick 		(void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
34958ad4d6ddSJeff Bonwick 
3496fa9e4066Sahrens 	pool_guid = spa_guid(spa);
3497fa9e4066Sahrens 	spa_close(spa, FTAG);
3498fa9e4066Sahrens 
3499fa9e4066Sahrens 	ztest_walk_pool_directory("pools before export");
3500fa9e4066Sahrens 
3501fa9e4066Sahrens 	/*
3502fa9e4066Sahrens 	 * Export it.
3503fa9e4066Sahrens 	 */
3504394ab0cbSGeorge Wilson 	error = spa_export(oldname, &config, B_FALSE, B_FALSE);
3505fa9e4066Sahrens 	if (error)
3506fa9e4066Sahrens 		fatal(0, "spa_export('%s') = %d", oldname, error);
3507fa9e4066Sahrens 
3508fa9e4066Sahrens 	ztest_walk_pool_directory("pools after export");
3509fa9e4066Sahrens 
3510fa9e4066Sahrens 	/*
35118ad4d6ddSJeff Bonwick 	 * Try to import it.
35128ad4d6ddSJeff Bonwick 	 */
35138ad4d6ddSJeff Bonwick 	newconfig = spa_tryimport(config);
35148ad4d6ddSJeff Bonwick 	ASSERT(newconfig != NULL);
35158ad4d6ddSJeff Bonwick 	nvlist_free(newconfig);
35168ad4d6ddSJeff Bonwick 
35178ad4d6ddSJeff Bonwick 	/*
3518fa9e4066Sahrens 	 * Import it under the new name.
3519fa9e4066Sahrens 	 */
3520fa9e4066Sahrens 	error = spa_import(newname, config, NULL);
3521fa9e4066Sahrens 	if (error)
3522fa9e4066Sahrens 		fatal(0, "spa_import('%s') = %d", newname, error);
3523fa9e4066Sahrens 
3524fa9e4066Sahrens 	ztest_walk_pool_directory("pools after import");
3525fa9e4066Sahrens 
3526fa9e4066Sahrens 	/*
3527fa9e4066Sahrens 	 * Try to import it again -- should fail with EEXIST.
3528fa9e4066Sahrens 	 */
3529fa9e4066Sahrens 	error = spa_import(newname, config, NULL);
3530fa9e4066Sahrens 	if (error != EEXIST)
3531fa9e4066Sahrens 		fatal(0, "spa_import('%s') twice", newname);
3532fa9e4066Sahrens 
3533fa9e4066Sahrens 	/*
3534fa9e4066Sahrens 	 * Try to import it under a different name -- should fail with EEXIST.
3535fa9e4066Sahrens 	 */
3536fa9e4066Sahrens 	error = spa_import(oldname, config, NULL);
3537fa9e4066Sahrens 	if (error != EEXIST)
3538fa9e4066Sahrens 		fatal(0, "spa_import('%s') under multiple names", newname);
3539fa9e4066Sahrens 
3540fa9e4066Sahrens 	/*
3541fa9e4066Sahrens 	 * Verify that the pool is no longer visible under the old name.
3542fa9e4066Sahrens 	 */
3543fa9e4066Sahrens 	error = spa_open(oldname, &spa, FTAG);
3544fa9e4066Sahrens 	if (error != ENOENT)
3545fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
3546fa9e4066Sahrens 
3547fa9e4066Sahrens 	/*
3548fa9e4066Sahrens 	 * Verify that we can open and close the pool using the new name.
3549fa9e4066Sahrens 	 */
3550fa9e4066Sahrens 	error = spa_open(newname, &spa, FTAG);
3551fa9e4066Sahrens 	if (error)
3552fa9e4066Sahrens 		fatal(0, "spa_open('%s') = %d", newname, error);
3553fa9e4066Sahrens 	ASSERT(pool_guid == spa_guid(spa));
3554fa9e4066Sahrens 	spa_close(spa, FTAG);
3555fa9e4066Sahrens 
3556fa9e4066Sahrens 	nvlist_free(config);
3557fa9e4066Sahrens }
3558fa9e4066Sahrens 
35598ad4d6ddSJeff Bonwick static void
35608ad4d6ddSJeff Bonwick ztest_resume(spa_t *spa)
35618ad4d6ddSJeff Bonwick {
35628ad4d6ddSJeff Bonwick 	if (spa_suspended(spa)) {
35638ad4d6ddSJeff Bonwick 		spa_vdev_state_enter(spa);
35648ad4d6ddSJeff Bonwick 		vdev_clear(spa, NULL);
35658ad4d6ddSJeff Bonwick 		(void) spa_vdev_state_exit(spa, NULL, 0);
356654d692b7SGeorge Wilson 		(void) zio_resume(spa);
35678ad4d6ddSJeff Bonwick 	}
35688ad4d6ddSJeff Bonwick }
35698ad4d6ddSJeff Bonwick 
35700a4e9518Sgw25295 static void *
35718ad4d6ddSJeff Bonwick ztest_resume_thread(void *arg)
35720a4e9518Sgw25295 {
3573e14bb325SJeff Bonwick 	spa_t *spa = arg;
35740a4e9518Sgw25295 
35750a4e9518Sgw25295 	while (!ztest_exiting) {
3576e14bb325SJeff Bonwick 		(void) poll(NULL, 0, 1000);
35778ad4d6ddSJeff Bonwick 		ztest_resume(spa);
35780a4e9518Sgw25295 	}
35790a4e9518Sgw25295 	return (NULL);
35800a4e9518Sgw25295 }
35810a4e9518Sgw25295 
3582fa9e4066Sahrens static void *
3583fa9e4066Sahrens ztest_thread(void *arg)
3584fa9e4066Sahrens {
3585fa9e4066Sahrens 	ztest_args_t *za = arg;
3586fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
3587fa9e4066Sahrens 	hrtime_t now, functime;
3588fa9e4066Sahrens 	ztest_info_t *zi;
3589e05725b1Sbonwick 	int f, i;
3590fa9e4066Sahrens 
3591fa9e4066Sahrens 	while ((now = gethrtime()) < za->za_stop) {
3592fa9e4066Sahrens 		/*
3593fa9e4066Sahrens 		 * See if it's time to force a crash.
3594fa9e4066Sahrens 		 */
3595fa9e4066Sahrens 		if (now > za->za_kill) {
3596e05725b1Sbonwick 			zs->zs_alloc = spa_get_alloc(za->za_spa);
3597e05725b1Sbonwick 			zs->zs_space = spa_get_space(za->za_spa);
3598fa9e4066Sahrens 			(void) kill(getpid(), SIGKILL);
3599fa9e4066Sahrens 		}
3600fa9e4066Sahrens 
3601fa9e4066Sahrens 		/*
3602fa9e4066Sahrens 		 * Pick a random function.
3603fa9e4066Sahrens 		 */
3604fa9e4066Sahrens 		f = ztest_random(ZTEST_FUNCS);
3605fa9e4066Sahrens 		zi = &zs->zs_info[f];
3606fa9e4066Sahrens 
3607fa9e4066Sahrens 		/*
3608fa9e4066Sahrens 		 * Decide whether to call it, based on the requested frequency.
3609fa9e4066Sahrens 		 */
3610fa9e4066Sahrens 		if (zi->zi_call_target == 0 ||
3611fa9e4066Sahrens 		    (double)zi->zi_call_total / zi->zi_call_target >
3612fa9e4066Sahrens 		    (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3613fa9e4066Sahrens 			continue;
3614fa9e4066Sahrens 
3615fa9e4066Sahrens 		atomic_add_64(&zi->zi_calls, 1);
3616fa9e4066Sahrens 		atomic_add_64(&zi->zi_call_total, 1);
3617fa9e4066Sahrens 
3618fa9e4066Sahrens 		za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3619fa9e4066Sahrens 		    ZTEST_DIRSIZE;
3620fa9e4066Sahrens 		za->za_diroff_shared = (1ULL << 63);
3621fa9e4066Sahrens 
3622e05725b1Sbonwick 		for (i = 0; i < zi->zi_iters; i++)
3623fa9e4066Sahrens 			zi->zi_func(za);
3624fa9e4066Sahrens 
3625fa9e4066Sahrens 		functime = gethrtime() - now;
3626fa9e4066Sahrens 
3627fa9e4066Sahrens 		atomic_add_64(&zi->zi_call_time, functime);
3628fa9e4066Sahrens 
3629fa9e4066Sahrens 		if (zopt_verbose >= 4) {
3630fa9e4066Sahrens 			Dl_info dli;
3631fa9e4066Sahrens 			(void) dladdr((void *)zi->zi_func, &dli);
3632fa9e4066Sahrens 			(void) printf("%6.2f sec in %s\n",
3633fa9e4066Sahrens 			    (double)functime / NANOSEC, dli.dli_sname);
3634fa9e4066Sahrens 		}
3635fa9e4066Sahrens 
3636fa9e4066Sahrens 		/*
3637fa9e4066Sahrens 		 * If we're getting ENOSPC with some regularity, stop.
3638fa9e4066Sahrens 		 */
3639fa9e4066Sahrens 		if (zs->zs_enospc_count > 10)
3640fa9e4066Sahrens 			break;
3641fa9e4066Sahrens 	}
3642fa9e4066Sahrens 
3643fa9e4066Sahrens 	return (NULL);
3644fa9e4066Sahrens }
3645fa9e4066Sahrens 
3646fa9e4066Sahrens /*
3647fa9e4066Sahrens  * Kick off threads to run tests on all datasets in parallel.
3648fa9e4066Sahrens  */
3649fa9e4066Sahrens static void
3650fa9e4066Sahrens ztest_run(char *pool)
3651fa9e4066Sahrens {
3652fa9e4066Sahrens 	int t, d, error;
3653fa9e4066Sahrens 	ztest_shared_t *zs = ztest_shared;
3654fa9e4066Sahrens 	ztest_args_t *za;
3655fa9e4066Sahrens 	spa_t *spa;
3656fa9e4066Sahrens 	char name[100];
3657e14bb325SJeff Bonwick 	thread_t resume_tid;
3658e14bb325SJeff Bonwick 
3659e14bb325SJeff Bonwick 	ztest_exiting = B_FALSE;
3660fa9e4066Sahrens 
3661fa9e4066Sahrens 	(void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3662fa9e4066Sahrens 	(void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3663fa9e4066Sahrens 
3664fa9e4066Sahrens 	for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3665fa9e4066Sahrens 		(void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3666fa9e4066Sahrens 
3667fa9e4066Sahrens 	/*
3668fa9e4066Sahrens 	 * Destroy one disk before we even start.
3669fa9e4066Sahrens 	 * It's mirrored, so everything should work just fine.
3670fa9e4066Sahrens 	 * This makes us exercise fault handling very early in spa_load().
3671fa9e4066Sahrens 	 */
3672fa9e4066Sahrens 	ztest_obliterate_one_disk(0);
3673fa9e4066Sahrens 
3674fa9e4066Sahrens 	/*
3675fa9e4066Sahrens 	 * Verify that the sum of the sizes of all blocks in the pool
3676fa9e4066Sahrens 	 * equals the SPA's allocated space total.
3677fa9e4066Sahrens 	 */
3678fa9e4066Sahrens 	ztest_verify_blocks(pool);
3679fa9e4066Sahrens 
3680fa9e4066Sahrens 	/*
3681fa9e4066Sahrens 	 * Kick off a replacement of the disk we just obliterated.
3682fa9e4066Sahrens 	 */
3683fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3684e14bb325SJeff Bonwick 	VERIFY(spa_open(pool, &spa, FTAG) == 0);
3685fa9e4066Sahrens 	ztest_replace_one_disk(spa, 0);
3686fa9e4066Sahrens 	if (zopt_verbose >= 5)
3687fa9e4066Sahrens 		show_pool_stats(spa);
3688fa9e4066Sahrens 	spa_close(spa, FTAG);
3689fa9e4066Sahrens 	kernel_fini();
3690fa9e4066Sahrens 
3691fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3692fa9e4066Sahrens 
3693fa9e4066Sahrens 	/*
3694fa9e4066Sahrens 	 * Verify that we can export the pool and reimport it under a
3695fa9e4066Sahrens 	 * different name.
3696fa9e4066Sahrens 	 */
36970e34b6a7Sbonwick 	if (ztest_random(2) == 0) {
3698fa9e4066Sahrens 		(void) snprintf(name, 100, "%s_import", pool);
3699fa9e4066Sahrens 		ztest_spa_import_export(pool, name);
3700fa9e4066Sahrens 		ztest_spa_import_export(name, pool);
37010e34b6a7Sbonwick 	}
3702fa9e4066Sahrens 
3703fa9e4066Sahrens 	/*
3704fa9e4066Sahrens 	 * Verify that we can loop over all pools.
3705fa9e4066Sahrens 	 */
3706fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
3707fa9e4066Sahrens 	for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3708fa9e4066Sahrens 		if (zopt_verbose > 3) {
3709fa9e4066Sahrens 			(void) printf("spa_next: found %s\n", spa_name(spa));
3710fa9e4066Sahrens 		}
3711fa9e4066Sahrens 	}
3712fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
3713fa9e4066Sahrens 
3714fa9e4066Sahrens 	/*
3715fa9e4066Sahrens 	 * Open our pool.
3716fa9e4066Sahrens 	 */
3717e14bb325SJeff Bonwick 	VERIFY(spa_open(pool, &spa, FTAG) == 0);
3718e14bb325SJeff Bonwick 
3719e14bb325SJeff Bonwick 	/*
37208ad4d6ddSJeff Bonwick 	 * We don't expect the pool to suspend unless maxfaults == 0,
37218ad4d6ddSJeff Bonwick 	 * in which case ztest_fault_inject() temporarily takes away
37228ad4d6ddSJeff Bonwick 	 * the only valid replica.
37238ad4d6ddSJeff Bonwick 	 */
37248ad4d6ddSJeff Bonwick 	if (zopt_maxfaults == 0)
37258ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
37268ad4d6ddSJeff Bonwick 	else
37278ad4d6ddSJeff Bonwick 		spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
37288ad4d6ddSJeff Bonwick 
37298ad4d6ddSJeff Bonwick 	/*
3730e14bb325SJeff Bonwick 	 * Create a thread to periodically resume suspended I/O.
3731e14bb325SJeff Bonwick 	 */
37328ad4d6ddSJeff Bonwick 	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
3733e14bb325SJeff Bonwick 	    &resume_tid) == 0);
3734fa9e4066Sahrens 
3735fa9e4066Sahrens 	/*
3736fa9e4066Sahrens 	 * Verify that we can safely inquire about about any object,
3737fa9e4066Sahrens 	 * whether it's allocated or not.  To make it interesting,
3738fa9e4066Sahrens 	 * we probe a 5-wide window around each power of two.
3739fa9e4066Sahrens 	 * This hits all edge cases, including zero and the max.
3740fa9e4066Sahrens 	 */
3741fa9e4066Sahrens 	for (t = 0; t < 64; t++) {
3742fa9e4066Sahrens 		for (d = -5; d <= 5; d++) {
3743fa9e4066Sahrens 			error = dmu_object_info(spa->spa_meta_objset,
3744fa9e4066Sahrens 			    (1ULL << t) + d, NULL);
3745ea8dc4b6Seschrock 			ASSERT(error == 0 || error == ENOENT ||
3746ea8dc4b6Seschrock 			    error == EINVAL);
3747fa9e4066Sahrens 		}
3748fa9e4066Sahrens 	}
3749fa9e4066Sahrens 
3750fa9e4066Sahrens 	/*
3751fa9e4066Sahrens 	 * Now kick off all the tests that run in parallel.
3752fa9e4066Sahrens 	 */
3753fa9e4066Sahrens 	zs->zs_enospc_count = 0;
3754fa9e4066Sahrens 
3755fa9e4066Sahrens 	za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3756fa9e4066Sahrens 
3757fa9e4066Sahrens 	if (zopt_verbose >= 4)
3758fa9e4066Sahrens 		(void) printf("starting main threads...\n");
3759fa9e4066Sahrens 
3760fa9e4066Sahrens 	za[0].za_start = gethrtime();
3761fa9e4066Sahrens 	za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3762fa9e4066Sahrens 	za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3763fa9e4066Sahrens 	za[0].za_kill = za[0].za_stop;
3764fa9e4066Sahrens 	if (ztest_random(100) < zopt_killrate)
3765fa9e4066Sahrens 		za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3766fa9e4066Sahrens 
3767fa9e4066Sahrens 	for (t = 0; t < zopt_threads; t++) {
3768ecc2d604Sbonwick 		d = t % zopt_datasets;
3769e05725b1Sbonwick 
3770e05725b1Sbonwick 		(void) strcpy(za[t].za_pool, pool);
3771e05725b1Sbonwick 		za[t].za_os = za[d].za_os;
3772e05725b1Sbonwick 		za[t].za_spa = spa;
3773e05725b1Sbonwick 		za[t].za_zilog = za[d].za_zilog;
3774e05725b1Sbonwick 		za[t].za_instance = t;
3775e05725b1Sbonwick 		za[t].za_random = ztest_random(-1ULL);
3776e05725b1Sbonwick 		za[t].za_start = za[0].za_start;
3777e05725b1Sbonwick 		za[t].za_stop = za[0].za_stop;
3778e05725b1Sbonwick 		za[t].za_kill = za[0].za_kill;
3779e05725b1Sbonwick 
3780ecc2d604Sbonwick 		if (t < zopt_datasets) {
37814d31c452Smaybee 			int test_future = FALSE;
3782fa9e4066Sahrens 			(void) rw_rdlock(&ztest_shared->zs_name_lock);
3783fa9e4066Sahrens 			(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3784ae46e4c7SMatthew Ahrens 			error = dmu_objset_create(name, DMU_OST_OTHER, 0,
3785fa9e4066Sahrens 			    ztest_create_cb, NULL);
37864d31c452Smaybee 			if (error == EEXIST) {
37874d31c452Smaybee 				test_future = TRUE;
3788e05725b1Sbonwick 			} else if (error == ENOSPC) {
3789fa9e4066Sahrens 				zs->zs_enospc_count++;
3790e05725b1Sbonwick 				(void) rw_unlock(&ztest_shared->zs_name_lock);
3791fa9e4066Sahrens 				break;
3792e05725b1Sbonwick 			} else if (error != 0) {
3793fa9e4066Sahrens 				fatal(0, "dmu_objset_create(%s) = %d",
3794fa9e4066Sahrens 				    name, error);
3795fa9e4066Sahrens 			}
3796*503ad85cSMatthew Ahrens 			error = dmu_objset_hold(name, FTAG, &za[d].za_os);
3797fa9e4066Sahrens 			if (error)
3798fa9e4066Sahrens 				fatal(0, "dmu_objset_open('%s') = %d",
3799fa9e4066Sahrens 				    name, error);
3800fa9e4066Sahrens 			(void) rw_unlock(&ztest_shared->zs_name_lock);
3801e05725b1Sbonwick 			if (test_future)
3802e05725b1Sbonwick 				ztest_dmu_check_future_leak(&za[t]);
38031209a471SNeil Perrin 			zil_replay(za[d].za_os, za[d].za_os,
38041209a471SNeil Perrin 			    ztest_replay_vector);
3805fa9e4066Sahrens 			za[d].za_zilog = zil_open(za[d].za_os, NULL);
3806fa9e4066Sahrens 		}
3807fa9e4066Sahrens 
3808e14bb325SJeff Bonwick 		VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3809e14bb325SJeff Bonwick 		    &za[t].za_thread) == 0);
3810fa9e4066Sahrens 	}
3811fa9e4066Sahrens 
3812fa9e4066Sahrens 	while (--t >= 0) {
3813e14bb325SJeff Bonwick 		VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3814ecc2d604Sbonwick 		if (t < zopt_datasets) {
3815fa9e4066Sahrens 			zil_close(za[t].za_zilog);
3816*503ad85cSMatthew Ahrens 			dmu_objset_rele(za[t].za_os, FTAG);
3817fa9e4066Sahrens 		}
3818fa9e4066Sahrens 	}
3819fa9e4066Sahrens 
3820fa9e4066Sahrens 	if (zopt_verbose >= 3)
3821fa9e4066Sahrens 		show_pool_stats(spa);
3822fa9e4066Sahrens 
3823fa9e4066Sahrens 	txg_wait_synced(spa_get_dsl(spa), 0);
3824fa9e4066Sahrens 
3825fa9e4066Sahrens 	zs->zs_alloc = spa_get_alloc(spa);
3826fa9e4066Sahrens 	zs->zs_space = spa_get_space(spa);
3827fa9e4066Sahrens 
3828fa9e4066Sahrens 	/*
3829e05725b1Sbonwick 	 * If we had out-of-space errors, destroy a random objset.
3830fa9e4066Sahrens 	 */
3831fa9e4066Sahrens 	if (zs->zs_enospc_count != 0) {
3832fa9e4066Sahrens 		(void) rw_rdlock(&ztest_shared->zs_name_lock);
3833e05725b1Sbonwick 		d = (int)ztest_random(zopt_datasets);
3834e05725b1Sbonwick 		(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3835fa9e4066Sahrens 		if (zopt_verbose >= 3)
3836fa9e4066Sahrens 			(void) printf("Destroying %s to free up space\n", name);
3837718d718aSGeorge Wilson 
3838718d718aSGeorge Wilson 		/* Cleanup any non-standard clones and snapshots */
3839718d718aSGeorge Wilson 		ztest_dsl_dataset_cleanup(name, za[d].za_instance);
3840718d718aSGeorge Wilson 
3841e05725b1Sbonwick 		(void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
38420b69c2f0Sahrens 		    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3843fa9e4066Sahrens 		(void) rw_unlock(&ztest_shared->zs_name_lock);
3844fa9e4066Sahrens 	}
3845fa9e4066Sahrens 
3846ea8dc4b6Seschrock 	txg_wait_synced(spa_get_dsl(spa), 0);
3847fa9e4066Sahrens 
3848e14bb325SJeff Bonwick 	umem_free(za, zopt_threads * sizeof (ztest_args_t));
3849e14bb325SJeff Bonwick 
3850e14bb325SJeff Bonwick 	/* Kill the resume thread */
3851e14bb325SJeff Bonwick 	ztest_exiting = B_TRUE;
3852e14bb325SJeff Bonwick 	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
38538ad4d6ddSJeff Bonwick 	ztest_resume(spa);
3854e14bb325SJeff Bonwick 
3855fa9e4066Sahrens 	/*
3856fa9e4066Sahrens 	 * Right before closing the pool, kick off a bunch of async I/O;
3857fa9e4066Sahrens 	 * spa_close() should wait for it to complete.
3858fa9e4066Sahrens 	 */
3859fa9e4066Sahrens 	for (t = 1; t < 50; t++)
3860fa9e4066Sahrens 		dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3861fa9e4066Sahrens 
3862e05725b1Sbonwick 	spa_close(spa, FTAG);
3863e05725b1Sbonwick 
3864fa9e4066Sahrens 	kernel_fini();
3865fa9e4066Sahrens }
3866fa9e4066Sahrens 
3867fa9e4066Sahrens void
3868fa9e4066Sahrens print_time(hrtime_t t, char *timebuf)
3869fa9e4066Sahrens {
3870fa9e4066Sahrens 	hrtime_t s = t / NANOSEC;
3871fa9e4066Sahrens 	hrtime_t m = s / 60;
3872fa9e4066Sahrens 	hrtime_t h = m / 60;
3873fa9e4066Sahrens 	hrtime_t d = h / 24;
3874fa9e4066Sahrens 
3875fa9e4066Sahrens 	s -= m * 60;
3876fa9e4066Sahrens 	m -= h * 60;
3877fa9e4066Sahrens 	h -= d * 24;
3878fa9e4066Sahrens 
3879fa9e4066Sahrens 	timebuf[0] = '\0';
3880fa9e4066Sahrens 
3881fa9e4066Sahrens 	if (d)
3882fa9e4066Sahrens 		(void) sprintf(timebuf,
3883fa9e4066Sahrens 		    "%llud%02lluh%02llum%02llus", d, h, m, s);
3884fa9e4066Sahrens 	else if (h)
3885fa9e4066Sahrens 		(void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3886fa9e4066Sahrens 	else if (m)
3887fa9e4066Sahrens 		(void) sprintf(timebuf, "%llum%02llus", m, s);
3888fa9e4066Sahrens 	else
3889fa9e4066Sahrens 		(void) sprintf(timebuf, "%llus", s);
3890fa9e4066Sahrens }
3891fa9e4066Sahrens 
3892fa9e4066Sahrens /*
3893fa9e4066Sahrens  * Create a storage pool with the given name and initial vdev size.
3894fa9e4066Sahrens  * Then create the specified number of datasets in the pool.
3895fa9e4066Sahrens  */
3896fa9e4066Sahrens static void
3897fa9e4066Sahrens ztest_init(char *pool)
3898fa9e4066Sahrens {
3899fa9e4066Sahrens 	spa_t *spa;
3900fa9e4066Sahrens 	int error;
3901fa9e4066Sahrens 	nvlist_t *nvroot;
3902fa9e4066Sahrens 
3903fa9e4066Sahrens 	kernel_init(FREAD | FWRITE);
3904fa9e4066Sahrens 
3905fa9e4066Sahrens 	/*
3906fa9e4066Sahrens 	 * Create the storage pool.
3907fa9e4066Sahrens 	 */
3908fa9e4066Sahrens 	(void) spa_destroy(pool);
3909fa9e4066Sahrens 	ztest_shared->zs_vdev_primaries = 0;
3910e14bb325SJeff Bonwick 	nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
3911e14bb325SJeff Bonwick 	    0, zopt_raidz, zopt_mirrors, 1);
39120a48a24eStimh 	error = spa_create(pool, nvroot, NULL, NULL, NULL);
3913fa9e4066Sahrens 	nvlist_free(nvroot);
3914fa9e4066Sahrens 
3915fa9e4066Sahrens 	if (error)
3916fa9e4066Sahrens 		fatal(0, "spa_create() = %d", error);
3917fa9e4066Sahrens 	error = spa_open(pool, &spa, FTAG);
3918fa9e4066Sahrens 	if (error)
3919fa9e4066Sahrens 		fatal(0, "spa_open() = %d", error);
3920fa9e4066Sahrens 
3921d6e555bdSGeorge Wilson 	metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
3922d6e555bdSGeorge Wilson 
3923fa9e4066Sahrens 	if (zopt_verbose >= 3)
3924fa9e4066Sahrens 		show_pool_stats(spa);
3925fa9e4066Sahrens 
3926fa9e4066Sahrens 	spa_close(spa, FTAG);
3927fa9e4066Sahrens 
3928fa9e4066Sahrens 	kernel_fini();
3929fa9e4066Sahrens }
3930fa9e4066Sahrens 
3931fa9e4066Sahrens int
3932fa9e4066Sahrens main(int argc, char **argv)
3933fa9e4066Sahrens {
3934fa9e4066Sahrens 	int kills = 0;
3935fa9e4066Sahrens 	int iters = 0;
3936fa9e4066Sahrens 	int i, f;
3937fa9e4066Sahrens 	ztest_shared_t *zs;
3938fa9e4066Sahrens 	ztest_info_t *zi;
3939fa9e4066Sahrens 	char timebuf[100];
3940fa9e4066Sahrens 	char numbuf[6];
3941fa9e4066Sahrens 
3942fa9e4066Sahrens 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
3943fa9e4066Sahrens 
3944fa9e4066Sahrens 	/* Override location of zpool.cache */
3945c5904d13Seschrock 	spa_config_path = "/tmp/zpool.cache";
3946fa9e4066Sahrens 
3947fa9e4066Sahrens 	ztest_random_fd = open("/dev/urandom", O_RDONLY);
3948fa9e4066Sahrens 
3949fa9e4066Sahrens 	process_options(argc, argv);
3950fa9e4066Sahrens 
3951ea8dc4b6Seschrock 	/*
3952ea8dc4b6Seschrock 	 * Blow away any existing copy of zpool.cache
3953ea8dc4b6Seschrock 	 */
3954ea8dc4b6Seschrock 	if (zopt_init != 0)
3955ea8dc4b6Seschrock 		(void) remove("/tmp/zpool.cache");
3956ea8dc4b6Seschrock 
3957fa9e4066Sahrens 	zs = ztest_shared = (void *)mmap(0,
3958fa9e4066Sahrens 	    P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3959fa9e4066Sahrens 	    PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3960fa9e4066Sahrens 
3961fa9e4066Sahrens 	if (zopt_verbose >= 1) {
3962fa9e4066Sahrens 		(void) printf("%llu vdevs, %d datasets, %d threads,"
3963fa9e4066Sahrens 		    " %llu seconds...\n",
3964ecc2d604Sbonwick 		    (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3965fa9e4066Sahrens 		    (u_longlong_t)zopt_time);
3966fa9e4066Sahrens 	}
3967fa9e4066Sahrens 
3968fa9e4066Sahrens 	/*
3969fa9e4066Sahrens 	 * Create and initialize our storage pool.
3970fa9e4066Sahrens 	 */
3971fa9e4066Sahrens 	for (i = 1; i <= zopt_init; i++) {
3972fa9e4066Sahrens 		bzero(zs, sizeof (ztest_shared_t));
3973fa9e4066Sahrens 		if (zopt_verbose >= 3 && zopt_init != 1)
3974fa9e4066Sahrens 			(void) printf("ztest_init(), pass %d\n", i);
3975fa9e4066Sahrens 		ztest_init(zopt_pool);
3976fa9e4066Sahrens 	}
3977fa9e4066Sahrens 
3978fa9e4066Sahrens 	/*
3979fa9e4066Sahrens 	 * Initialize the call targets for each function.
3980fa9e4066Sahrens 	 */
3981fa9e4066Sahrens 	for (f = 0; f < ZTEST_FUNCS; f++) {
3982fa9e4066Sahrens 		zi = &zs->zs_info[f];
3983fa9e4066Sahrens 
3984fa9e4066Sahrens 		*zi = ztest_info[f];
3985fa9e4066Sahrens 
3986fa9e4066Sahrens 		if (*zi->zi_interval == 0)
3987fa9e4066Sahrens 			zi->zi_call_target = UINT64_MAX;
3988fa9e4066Sahrens 		else
3989fa9e4066Sahrens 			zi->zi_call_target = zopt_time / *zi->zi_interval;
3990fa9e4066Sahrens 	}
3991fa9e4066Sahrens 
3992fa9e4066Sahrens 	zs->zs_start_time = gethrtime();
3993fa9e4066Sahrens 	zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3994fa9e4066Sahrens 
3995fa9e4066Sahrens 	/*
3996fa9e4066Sahrens 	 * Run the tests in a loop.  These tests include fault injection
3997fa9e4066Sahrens 	 * to verify that self-healing data works, and forced crashes
3998fa9e4066Sahrens 	 * to verify that we never lose on-disk consistency.
3999fa9e4066Sahrens 	 */
4000fa9e4066Sahrens 	while (gethrtime() < zs->zs_stop_time) {
4001fa9e4066Sahrens 		int status;
4002fa9e4066Sahrens 		pid_t pid;
4003fa9e4066Sahrens 		char *tmp;
4004fa9e4066Sahrens 
4005fa9e4066Sahrens 		/*
4006fa9e4066Sahrens 		 * Initialize the workload counters for each function.
4007fa9e4066Sahrens 		 */
4008fa9e4066Sahrens 		for (f = 0; f < ZTEST_FUNCS; f++) {
4009fa9e4066Sahrens 			zi = &zs->zs_info[f];
4010fa9e4066Sahrens 			zi->zi_calls = 0;
4011fa9e4066Sahrens 			zi->zi_call_time = 0;
4012fa9e4066Sahrens 		}
4013fa9e4066Sahrens 
4014d6e555bdSGeorge Wilson 		/* Set the allocation switch size */
4015d6e555bdSGeorge Wilson 		metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
4016d6e555bdSGeorge Wilson 
4017fa9e4066Sahrens 		pid = fork();
4018fa9e4066Sahrens 
4019fa9e4066Sahrens 		if (pid == -1)
4020fa9e4066Sahrens 			fatal(1, "fork failed");
4021fa9e4066Sahrens 
4022fa9e4066Sahrens 		if (pid == 0) {	/* child */
4023fa9e4066Sahrens 			struct rlimit rl = { 1024, 1024 };
4024fa9e4066Sahrens 			(void) setrlimit(RLIMIT_NOFILE, &rl);
4025004388ebScasper 			(void) enable_extended_FILE_stdio(-1, -1);
4026fa9e4066Sahrens 			ztest_run(zopt_pool);
4027fa9e4066Sahrens 			exit(0);
4028fa9e4066Sahrens 		}
4029fa9e4066Sahrens 
40305ad82045Snd150628 		while (waitpid(pid, &status, 0) != pid)
4031fa9e4066Sahrens 			continue;
4032fa9e4066Sahrens 
4033fa9e4066Sahrens 		if (WIFEXITED(status)) {
4034fa9e4066Sahrens 			if (WEXITSTATUS(status) != 0) {
4035fa9e4066Sahrens 				(void) fprintf(stderr,
4036fa9e4066Sahrens 				    "child exited with code %d\n",
4037fa9e4066Sahrens 				    WEXITSTATUS(status));
4038fa9e4066Sahrens 				exit(2);
4039fa9e4066Sahrens 			}
40405ad82045Snd150628 		} else if (WIFSIGNALED(status)) {
4041fa9e4066Sahrens 			if (WTERMSIG(status) != SIGKILL) {
4042fa9e4066Sahrens 				(void) fprintf(stderr,
4043fa9e4066Sahrens 				    "child died with signal %d\n",
4044fa9e4066Sahrens 				    WTERMSIG(status));
4045fa9e4066Sahrens 				exit(3);
4046fa9e4066Sahrens 			}
4047fa9e4066Sahrens 			kills++;
40485ad82045Snd150628 		} else {
40495ad82045Snd150628 			(void) fprintf(stderr, "something strange happened "
40505ad82045Snd150628 			    "to child\n");
40515ad82045Snd150628 			exit(4);
4052fa9e4066Sahrens 		}
4053fa9e4066Sahrens 
4054fa9e4066Sahrens 		iters++;
4055fa9e4066Sahrens 
4056fa9e4066Sahrens 		if (zopt_verbose >= 1) {
4057fa9e4066Sahrens 			hrtime_t now = gethrtime();
4058fa9e4066Sahrens 
4059fa9e4066Sahrens 			now = MIN(now, zs->zs_stop_time);
4060fa9e4066Sahrens 			print_time(zs->zs_stop_time - now, timebuf);
4061fa9e4066Sahrens 			nicenum(zs->zs_space, numbuf);
4062fa9e4066Sahrens 
4063fa9e4066Sahrens 			(void) printf("Pass %3d, %8s, %3llu ENOSPC, "
4064fa9e4066Sahrens 			    "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
4065fa9e4066Sahrens 			    iters,
4066fa9e4066Sahrens 			    WIFEXITED(status) ? "Complete" : "SIGKILL",
4067fa9e4066Sahrens 			    (u_longlong_t)zs->zs_enospc_count,
4068fa9e4066Sahrens 			    100.0 * zs->zs_alloc / zs->zs_space,
4069fa9e4066Sahrens 			    numbuf,
4070fa9e4066Sahrens 			    100.0 * (now - zs->zs_start_time) /
4071fa9e4066Sahrens 			    (zopt_time * NANOSEC), timebuf);
4072fa9e4066Sahrens 		}
4073fa9e4066Sahrens 
4074fa9e4066Sahrens 		if (zopt_verbose >= 2) {
4075fa9e4066Sahrens 			(void) printf("\nWorkload summary:\n\n");
4076fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
4077fa9e4066Sahrens 			    "Calls", "Time", "Function");
4078fa9e4066Sahrens 			(void) printf("%7s %9s   %s\n",
4079fa9e4066Sahrens 			    "-----", "----", "--------");
4080fa9e4066Sahrens 			for (f = 0; f < ZTEST_FUNCS; f++) {
4081fa9e4066Sahrens 				Dl_info dli;
4082fa9e4066Sahrens 
4083fa9e4066Sahrens 				zi = &zs->zs_info[f];
4084fa9e4066Sahrens 				print_time(zi->zi_call_time, timebuf);
4085fa9e4066Sahrens 				(void) dladdr((void *)zi->zi_func, &dli);
4086fa9e4066Sahrens 				(void) printf("%7llu %9s   %s\n",
4087fa9e4066Sahrens 				    (u_longlong_t)zi->zi_calls, timebuf,
4088fa9e4066Sahrens 				    dli.dli_sname);
4089fa9e4066Sahrens 			}
4090fa9e4066Sahrens 			(void) printf("\n");
4091fa9e4066Sahrens 		}
4092fa9e4066Sahrens 
4093fa9e4066Sahrens 		/*
4094fa9e4066Sahrens 		 * It's possible that we killed a child during a rename test, in
4095fa9e4066Sahrens 		 * which case we'll have a 'ztest_tmp' pool lying around instead
4096fa9e4066Sahrens 		 * of 'ztest'.  Do a blind rename in case this happened.
4097fa9e4066Sahrens 		 */
4098fa9e4066Sahrens 		tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
4099fa9e4066Sahrens 		(void) strcpy(tmp, zopt_pool);
4100fa9e4066Sahrens 		(void) strcat(tmp, "_tmp");
4101fa9e4066Sahrens 		kernel_init(FREAD | FWRITE);
4102fa9e4066Sahrens 		(void) spa_rename(tmp, zopt_pool);
4103fa9e4066Sahrens 		kernel_fini();
4104fa9e4066Sahrens 		umem_free(tmp, strlen(tmp) + 1);
4105fa9e4066Sahrens 	}
4106fa9e4066Sahrens 
4107fa9e4066Sahrens 	ztest_verify_blocks(zopt_pool);
4108fa9e4066Sahrens 
4109fa9e4066Sahrens 	if (zopt_verbose >= 1) {
4110fa9e4066Sahrens 		(void) printf("%d killed, %d completed, %.0f%% kill rate\n",
4111fa9e4066Sahrens 		    kills, iters - kills, (100.0 * kills) / MAX(1, iters));
4112fa9e4066Sahrens 	}
4113fa9e4066Sahrens 
4114fa9e4066Sahrens 	return (0);
4115fa9e4066Sahrens }
4116