]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/ztest/ztest.c
Add dpkg-dev to Depends of zfs-dkms (Closes: #900714)
[mirror_zfs-debian.git] / cmd / ztest / ztest.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
cae5b340 23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
3541dc6d 24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
a08ee875 25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
28/*
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
31 *
32 * The overall design of the ztest program is as follows:
33 *
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
38 *
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
42 *
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
45 *
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
49 *
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
ea04106b 55 * storage pool, as many times as desired. If backwards compatibility
c242c188
CS
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
34dc7c2f
BB
58 *
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
64 *
1e33ac1e
BB
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
67 *
34dc7c2f
BB
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
71 *
72 * To turn this into an overnight stress test, use -T to specify run time.
73 *
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
76 *
c242c188
CS
77 * Use the -k option to set the desired frequency of kills.
78 *
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
34dc7c2f
BB
86 */
87
88#include <sys/zfs_context.h>
89#include <sys/spa.h>
90#include <sys/dmu.h>
91#include <sys/txg.h>
9babb374 92#include <sys/dbuf.h>
34dc7c2f 93#include <sys/zap.h>
34dc7c2f
BB
94#include <sys/dmu_objset.h>
95#include <sys/poll.h>
96#include <sys/stat.h>
97#include <sys/time.h>
98#include <sys/wait.h>
99#include <sys/mman.h>
100#include <sys/resource.h>
101#include <sys/zio.h>
34dc7c2f 102#include <sys/zil.h>
428870ff 103#include <sys/zil_impl.h>
cae5b340 104#include <sys/zfs_rlock.h>
34dc7c2f 105#include <sys/vdev_impl.h>
b128c09f 106#include <sys/vdev_file.h>
34dc7c2f 107#include <sys/spa_impl.h>
428870ff 108#include <sys/metaslab_impl.h>
34dc7c2f 109#include <sys/dsl_prop.h>
9babb374 110#include <sys/dsl_dataset.h>
a08ee875 111#include <sys/dsl_destroy.h>
428870ff
BB
112#include <sys/dsl_scan.h>
113#include <sys/zio_checksum.h>
34dc7c2f 114#include <sys/refcount.h>
9ae529ec 115#include <sys/zfeature.h>
a08ee875 116#include <sys/dsl_userhold.h>
cae5b340 117#include <sys/abd.h>
34dc7c2f
BB
118#include <stdio.h>
119#include <stdio_ext.h>
120#include <stdlib.h>
121#include <unistd.h>
122#include <signal.h>
123#include <umem.h>
34dc7c2f
BB
124#include <ctype.h>
125#include <math.h>
126#include <sys/fs/zfs.h>
cae5b340 127#include <zfs_fletcher.h>
428870ff 128#include <libnvpair.h>
cae5b340
AX
129#include <libzfs.h>
130#ifdef __GLIBC__
ea04106b
AX
131#include <execinfo.h> /* for backtrace() */
132#endif
34dc7c2f 133
9d81146b
ED
134static int ztest_fd_data = -1;
135static int ztest_fd_rand = -1;
c242c188
CS
136
137typedef struct ztest_shared_hdr {
138 uint64_t zh_hdr_size;
139 uint64_t zh_opts_size;
140 uint64_t zh_size;
141 uint64_t zh_stats_size;
142 uint64_t zh_stats_count;
143 uint64_t zh_ds_size;
144 uint64_t zh_ds_count;
145} ztest_shared_hdr_t;
146
147static ztest_shared_hdr_t *ztest_shared_hdr;
148
149typedef struct ztest_shared_opts {
cae5b340
AX
150 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
151 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
c242c188
CS
152 char zo_alt_ztest[MAXNAMELEN];
153 char zo_alt_libpath[MAXNAMELEN];
154 uint64_t zo_vdevs;
155 uint64_t zo_vdevtime;
156 size_t zo_vdev_size;
157 int zo_ashift;
158 int zo_mirrors;
159 int zo_raidz;
160 int zo_raidz_parity;
161 int zo_datasets;
162 int zo_threads;
163 uint64_t zo_passtime;
164 uint64_t zo_killrate;
165 int zo_verbose;
166 int zo_init;
167 uint64_t zo_time;
168 uint64_t zo_maxloops;
169 uint64_t zo_metaslab_gang_bang;
cae5b340 170 int zo_mmp_test;
c242c188
CS
171} ztest_shared_opts_t;
172
173static const ztest_shared_opts_t ztest_opts_defaults = {
174 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
175 .zo_dir = { '/', 't', 'm', 'p', '\0' },
176 .zo_alt_ztest = { '\0' },
177 .zo_alt_libpath = { '\0' },
178 .zo_vdevs = 5,
179 .zo_ashift = SPA_MINBLOCKSHIFT,
180 .zo_mirrors = 2,
181 .zo_raidz = 4,
182 .zo_raidz_parity = 1,
cae5b340 183 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
c242c188
CS
184 .zo_datasets = 7,
185 .zo_threads = 23,
186 .zo_passtime = 60, /* 60 seconds */
187 .zo_killrate = 70, /* 70% kill rate */
188 .zo_verbose = 0,
cae5b340 189 .zo_mmp_test = 0,
c242c188
CS
190 .zo_init = 1,
191 .zo_time = 300, /* 5 minutes */
192 .zo_maxloops = 50, /* max loops during spa_freeze() */
193 .zo_metaslab_gang_bang = 32 << 10
194};
195
196extern uint64_t metaslab_gang_bang;
197extern uint64_t metaslab_df_alloc_threshold;
ea04106b 198extern int metaslab_preload_limit;
cae5b340
AX
199extern boolean_t zfs_compressed_arc_enabled;
200extern int zfs_abd_scatter_enabled;
c242c188
CS
201
202static ztest_shared_opts_t *ztest_shared_opts;
203static ztest_shared_opts_t ztest_opts;
204
205typedef struct ztest_shared_ds {
206 uint64_t zd_seq;
207} ztest_shared_ds_t;
208
209static ztest_shared_ds_t *ztest_shared_ds;
210#define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
428870ff
BB
211
212#define BT_MAGIC 0x123456789abcdefULL
c242c188
CS
213#define MAXFAULTS() \
214 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
428870ff
BB
215
216enum ztest_io_type {
217 ZTEST_IO_WRITE_TAG,
218 ZTEST_IO_WRITE_PATTERN,
219 ZTEST_IO_WRITE_ZEROES,
220 ZTEST_IO_TRUNCATE,
221 ZTEST_IO_SETATTR,
a08ee875 222 ZTEST_IO_REWRITE,
428870ff
BB
223 ZTEST_IO_TYPES
224};
34dc7c2f
BB
225
226typedef struct ztest_block_tag {
428870ff 227 uint64_t bt_magic;
34dc7c2f
BB
228 uint64_t bt_objset;
229 uint64_t bt_object;
cae5b340 230 uint64_t bt_dnodesize;
34dc7c2f 231 uint64_t bt_offset;
428870ff 232 uint64_t bt_gen;
34dc7c2f 233 uint64_t bt_txg;
428870ff 234 uint64_t bt_crtxg;
34dc7c2f
BB
235} ztest_block_tag_t;
236
428870ff
BB
237typedef struct bufwad {
238 uint64_t bw_index;
239 uint64_t bw_txg;
240 uint64_t bw_data;
241} bufwad_t;
242
428870ff
BB
243typedef struct rll {
244 void *rll_writer;
245 int rll_readers;
1e33ac1e
BB
246 kmutex_t rll_lock;
247 kcondvar_t rll_cv;
428870ff
BB
248} rll_t;
249
cae5b340
AX
250typedef struct zll {
251 list_t z_list;
252 kmutex_t z_lock;
253} zll_t;
428870ff
BB
254
255#define ZTEST_RANGE_LOCKS 64
256#define ZTEST_OBJECT_LOCKS 64
257
258/*
259 * Object descriptor. Used as a template for object lookup/create/remove.
260 */
261typedef struct ztest_od {
262 uint64_t od_dir;
263 uint64_t od_object;
264 dmu_object_type_t od_type;
265 dmu_object_type_t od_crtype;
266 uint64_t od_blocksize;
267 uint64_t od_crblocksize;
cae5b340 268 uint64_t od_crdnodesize;
428870ff
BB
269 uint64_t od_gen;
270 uint64_t od_crgen;
cae5b340 271 char od_name[ZFS_MAX_DATASET_NAME_LEN];
428870ff 272} ztest_od_t;
34dc7c2f 273
428870ff
BB
274/*
275 * Per-dataset state.
276 */
277typedef struct ztest_ds {
c242c188 278 ztest_shared_ds_t *zd_shared;
428870ff 279 objset_t *zd_os;
ea04106b 280 rwlock_t zd_zilog_lock;
428870ff 281 zilog_t *zd_zilog;
428870ff 282 ztest_od_t *zd_od; /* debugging aid */
cae5b340 283 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
1e33ac1e 284 kmutex_t zd_dirobj_lock;
428870ff 285 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
cae5b340 286 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
428870ff
BB
287} ztest_ds_t;
288
289/*
290 * Per-iteration state.
291 */
292typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
293
294typedef struct ztest_info {
295 ztest_func_t *zi_func; /* test function */
296 uint64_t zi_iters; /* iterations per execution */
297 uint64_t *zi_interval; /* execute every <interval> seconds */
ea04106b 298 const char *zi_funcname; /* name of test function */
428870ff 299} ztest_info_t;
34dc7c2f 300
c242c188
CS
301typedef struct ztest_shared_callstate {
302 uint64_t zc_count; /* per-pass count */
303 uint64_t zc_time; /* per-pass time */
304 uint64_t zc_next; /* next time to call this function */
305} ztest_shared_callstate_t;
306
307static ztest_shared_callstate_t *ztest_shared_callstate;
308#define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
309
34dc7c2f
BB
310ztest_func_t ztest_dmu_read_write;
311ztest_func_t ztest_dmu_write_parallel;
312ztest_func_t ztest_dmu_object_alloc_free;
428870ff 313ztest_func_t ztest_dmu_commit_callbacks;
34dc7c2f
BB
314ztest_func_t ztest_zap;
315ztest_func_t ztest_zap_parallel;
428870ff 316ztest_func_t ztest_zil_commit;
3e31d2b0 317ztest_func_t ztest_zil_remount;
428870ff 318ztest_func_t ztest_dmu_read_write_zcopy;
34dc7c2f 319ztest_func_t ztest_dmu_objset_create_destroy;
428870ff
BB
320ztest_func_t ztest_dmu_prealloc;
321ztest_func_t ztest_fzap;
34dc7c2f 322ztest_func_t ztest_dmu_snapshot_create_destroy;
428870ff
BB
323ztest_func_t ztest_dsl_prop_get_set;
324ztest_func_t ztest_spa_prop_get_set;
34dc7c2f
BB
325ztest_func_t ztest_spa_create_destroy;
326ztest_func_t ztest_fault_inject;
428870ff
BB
327ztest_func_t ztest_ddt_repair;
328ztest_func_t ztest_dmu_snapshot_hold;
cae5b340 329ztest_func_t ztest_mmp_enable_disable;
b128c09f 330ztest_func_t ztest_spa_rename;
428870ff
BB
331ztest_func_t ztest_scrub;
332ztest_func_t ztest_dsl_dataset_promote_busy;
34dc7c2f
BB
333ztest_func_t ztest_vdev_attach_detach;
334ztest_func_t ztest_vdev_LUN_growth;
335ztest_func_t ztest_vdev_add_remove;
b128c09f 336ztest_func_t ztest_vdev_aux_add_remove;
428870ff 337ztest_func_t ztest_split_pool;
3541dc6d 338ztest_func_t ztest_reguid;
ea0b2538 339ztest_func_t ztest_spa_upgrade;
cae5b340
AX
340ztest_func_t ztest_fletcher;
341ztest_func_t ztest_fletcher_incr;
342ztest_func_t ztest_verify_dnode_bt;
34dc7c2f 343
428870ff
BB
344uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
345uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
346uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
347uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
348uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
34dc7c2f 349
ea04106b
AX
350#define ZTI_INIT(func, iters, interval) \
351 { .zi_func = (func), \
352 .zi_iters = (iters), \
353 .zi_interval = (interval), \
354 .zi_funcname = # func }
355
34dc7c2f 356ztest_info_t ztest_info[] = {
ea04106b
AX
357 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
358 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
359 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
360 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
361 ZTI_INIT(ztest_zap, 30, &zopt_always),
362 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
363 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
364 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
365 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
367 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
368 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
369 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
428870ff 370#if 0
ea04106b 371 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
428870ff 372#endif
ea04106b
AX
373 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
374 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
376 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
378 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
cae5b340 379 ZTI_INIT(ztest_mmp_enable_disable, 1, &zopt_sometimes),
ea04106b
AX
380 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
381 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
382 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
383 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
384 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
385 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
386 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
387 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
388 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
cae5b340
AX
389 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
390 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
391 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
34dc7c2f
BB
392};
393
394#define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
395
428870ff
BB
396/*
397 * The following struct is used to hold a list of uncalled commit callbacks.
398 * The callbacks are ordered by txg number.
399 */
400typedef struct ztest_cb_list {
1e33ac1e
BB
401 kmutex_t zcl_callbacks_lock;
402 list_t zcl_callbacks;
428870ff 403} ztest_cb_list_t;
34dc7c2f
BB
404
405/*
406 * Stuff we need to share writably between parent and child.
407 */
408typedef struct ztest_shared {
c242c188 409 boolean_t zs_do_init;
428870ff
BB
410 hrtime_t zs_proc_start;
411 hrtime_t zs_proc_stop;
412 hrtime_t zs_thread_start;
413 hrtime_t zs_thread_stop;
414 hrtime_t zs_thread_kill;
34dc7c2f 415 uint64_t zs_enospc_count;
428870ff
BB
416 uint64_t zs_vdev_next_leaf;
417 uint64_t zs_vdev_aux;
34dc7c2f
BB
418 uint64_t zs_alloc;
419 uint64_t zs_space;
428870ff
BB
420 uint64_t zs_splits;
421 uint64_t zs_mirrors;
c242c188
CS
422 uint64_t zs_metaslab_sz;
423 uint64_t zs_metaslab_df_alloc_threshold;
424 uint64_t zs_guid;
34dc7c2f
BB
425} ztest_shared_t;
426
428870ff
BB
427#define ID_PARALLEL -1ULL
428
34dc7c2f 429static char ztest_dev_template[] = "%s/%s.%llua";
b128c09f 430static char ztest_aux_template[] = "%s/%s.%s.%llu";
428870ff 431ztest_shared_t *ztest_shared;
34dc7c2f 432
c242c188
CS
433static spa_t *ztest_spa = NULL;
434static ztest_ds_t *ztest_ds;
435
436static kmutex_t ztest_vdev_lock;
3bc7e0fb
GW
437
438/*
439 * The ztest_name_lock protects the pool and dataset namespace used by
440 * the individual tests. To modify the namespace, consumers must grab
441 * this lock as writer. Grabbing the lock as reader will ensure that the
442 * namespace does not change while the lock is held.
443 */
ea04106b 444static rwlock_t ztest_name_lock;
34dc7c2f 445
c242c188 446static boolean_t ztest_dump_core = B_TRUE;
b128c09f 447static boolean_t ztest_exiting;
34dc7c2f 448
428870ff
BB
449/* Global commit callback list */
450static ztest_cb_list_t zcl;
090ff092
RC
451/* Commit cb delay */
452static uint64_t zc_min_txg_delay = UINT64_MAX;
453static int zc_cb_counter = 0;
454
455/*
456 * Minimum number of commit callbacks that need to be registered for us to check
457 * whether the minimum txg delay is acceptable.
458 */
459#define ZTEST_COMMIT_CB_MIN_REG 100
460
461/*
462 * If a number of txgs equal to this threshold have been created after a commit
463 * callback has been registered but not called, then we assume there is an
464 * implementation bug.
465 */
466#define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
428870ff 467
34dc7c2f 468extern uint64_t metaslab_gang_bang;
9babb374 469extern uint64_t metaslab_df_alloc_threshold;
34dc7c2f 470
428870ff
BB
471enum ztest_object {
472 ZTEST_META_DNODE = 0,
473 ZTEST_DIROBJ,
474 ZTEST_OBJECTS
475};
34dc7c2f
BB
476
477static void usage(boolean_t) __NORETURN;
478
479/*
480 * These libumem hooks provide a reasonable set of defaults for the allocator's
481 * debugging facilities.
482 */
483const char *
0bc8fd78 484_umem_debug_init(void)
34dc7c2f
BB
485{
486 return ("default,verbose"); /* $UMEM_DEBUG setting */
487}
488
489const char *
490_umem_logging_init(void)
491{
492 return ("fail,contents"); /* $UMEM_LOGGING setting */
493}
494
ea04106b
AX
495#define BACKTRACE_SZ 100
496
497static void sig_handler(int signo)
498{
499 struct sigaction action;
cae5b340 500#ifdef __GLIBC__ /* backtrace() is a GNU extension */
ea04106b
AX
501 int nptrs;
502 void *buffer[BACKTRACE_SZ];
503
504 nptrs = backtrace(buffer, BACKTRACE_SZ);
505 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
506#endif
507
508 /*
509 * Restore default action and re-raise signal so SIGSEGV and
510 * SIGABRT can trigger a core dump.
511 */
512 action.sa_handler = SIG_DFL;
513 sigemptyset(&action.sa_mask);
514 action.sa_flags = 0;
515 (void) sigaction(signo, &action, NULL);
516 raise(signo);
517}
518
34dc7c2f
BB
519#define FATAL_MSG_SZ 1024
520
521char *fatal_msg;
522
523static void
524fatal(int do_perror, char *message, ...)
525{
526 va_list args;
527 int save_errno = errno;
40b84e7a 528 char *buf;
34dc7c2f
BB
529
530 (void) fflush(stdout);
40b84e7a 531 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
34dc7c2f
BB
532
533 va_start(args, message);
534 (void) sprintf(buf, "ztest: ");
535 /* LINTED */
536 (void) vsprintf(buf + strlen(buf), message, args);
537 va_end(args);
538 if (do_perror) {
539 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
540 ": %s", strerror(save_errno));
541 }
542 (void) fprintf(stderr, "%s\n", buf);
543 fatal_msg = buf; /* to ease debugging */
544 if (ztest_dump_core)
545 abort();
546 exit(3);
547}
548
549static int
550str2shift(const char *buf)
551{
552 const char *ends = "BKMGTPEZ";
553 int i;
554
555 if (buf[0] == '\0')
556 return (0);
557 for (i = 0; i < strlen(ends); i++) {
558 if (toupper(buf[0]) == ends[i])
559 break;
560 }
561 if (i == strlen(ends)) {
562 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
563 buf);
564 usage(B_FALSE);
565 }
566 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
567 return (10*i);
568 }
569 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
570 usage(B_FALSE);
571 /* NOTREACHED */
572}
573
574static uint64_t
575nicenumtoull(const char *buf)
576{
577 char *end;
578 uint64_t val;
579
580 val = strtoull(buf, &end, 0);
581 if (end == buf) {
582 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
583 usage(B_FALSE);
584 } else if (end[0] == '.') {
585 double fval = strtod(buf, &end);
586 fval *= pow(2, str2shift(end));
587 if (fval > UINT64_MAX) {
588 (void) fprintf(stderr, "ztest: value too large: %s\n",
589 buf);
590 usage(B_FALSE);
591 }
592 val = (uint64_t)fval;
593 } else {
594 int shift = str2shift(end);
595 if (shift >= 64 || (val << shift) >> shift != val) {
596 (void) fprintf(stderr, "ztest: value too large: %s\n",
597 buf);
598 usage(B_FALSE);
599 }
600 val <<= shift;
601 }
602 return (val);
603}
604
605static void
606usage(boolean_t requested)
607{
c242c188
CS
608 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
609
34dc7c2f
BB
610 char nice_vdev_size[10];
611 char nice_gang_bang[10];
612 FILE *fp = requested ? stdout : stderr;
613
c242c188
CS
614 nicenum(zo->zo_vdev_size, nice_vdev_size);
615 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
34dc7c2f
BB
616
617 (void) fprintf(fp, "Usage: %s\n"
618 "\t[-v vdevs (default: %llu)]\n"
619 "\t[-s size_of_each_vdev (default: %s)]\n"
428870ff 620 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
34dc7c2f
BB
621 "\t[-m mirror_copies (default: %d)]\n"
622 "\t[-r raidz_disks (default: %d)]\n"
623 "\t[-R raidz_parity (default: %d)]\n"
624 "\t[-d datasets (default: %d)]\n"
625 "\t[-t threads (default: %d)]\n"
626 "\t[-g gang_block_threshold (default: %s)]\n"
428870ff
BB
627 "\t[-i init_count (default: %d)] initialize pool i times\n"
628 "\t[-k kill_percentage (default: %llu%%)]\n"
34dc7c2f 629 "\t[-p pool_name (default: %s)]\n"
428870ff 630 "\t[-f dir (default: %s)] file directory for vdev files\n"
cae5b340 631 "\t[-M] Multi-host simulate pool imported on remote host\n"
428870ff
BB
632 "\t[-V] verbose (use multiple times for ever more blather)\n"
633 "\t[-E] use existing pool instead of creating new one\n"
634 "\t[-T time (default: %llu sec)] total run time\n"
635 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
636 "\t[-P passtime (default: %llu sec)] time per pass\n"
c242c188 637 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
cae5b340
AX
638 "\t[-o variable=value] ... set global variable to an unsigned\n"
639 "\t 32-bit integer value\n"
34dc7c2f
BB
640 "\t[-h] (print help)\n"
641 "",
c242c188
CS
642 zo->zo_pool,
643 (u_longlong_t)zo->zo_vdevs, /* -v */
34dc7c2f 644 nice_vdev_size, /* -s */
c242c188
CS
645 zo->zo_ashift, /* -a */
646 zo->zo_mirrors, /* -m */
647 zo->zo_raidz, /* -r */
648 zo->zo_raidz_parity, /* -R */
649 zo->zo_datasets, /* -d */
650 zo->zo_threads, /* -t */
34dc7c2f 651 nice_gang_bang, /* -g */
c242c188
CS
652 zo->zo_init, /* -i */
653 (u_longlong_t)zo->zo_killrate, /* -k */
654 zo->zo_pool, /* -p */
655 zo->zo_dir, /* -f */
656 (u_longlong_t)zo->zo_time, /* -T */
657 (u_longlong_t)zo->zo_maxloops, /* -F */
658 (u_longlong_t)zo->zo_passtime);
34dc7c2f
BB
659 exit(requested ? 0 : 1);
660}
661
34dc7c2f
BB
662static void
663process_options(int argc, char **argv)
664{
c242c188
CS
665 char *path;
666 ztest_shared_opts_t *zo = &ztest_opts;
667
34dc7c2f
BB
668 int opt;
669 uint64_t value;
c242c188 670 char altdir[MAXNAMELEN] = { 0 };
34dc7c2f 671
c242c188 672 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
34dc7c2f 673
34dc7c2f 674 while ((opt = getopt(argc, argv,
cae5b340 675 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:")) != EOF) {
34dc7c2f
BB
676 value = 0;
677 switch (opt) {
678 case 'v':
679 case 's':
680 case 'a':
681 case 'm':
682 case 'r':
683 case 'R':
684 case 'd':
685 case 't':
686 case 'g':
687 case 'i':
688 case 'k':
689 case 'T':
690 case 'P':
428870ff 691 case 'F':
34dc7c2f
BB
692 value = nicenumtoull(optarg);
693 }
694 switch (opt) {
695 case 'v':
c242c188 696 zo->zo_vdevs = value;
34dc7c2f
BB
697 break;
698 case 's':
c242c188 699 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
34dc7c2f
BB
700 break;
701 case 'a':
c242c188 702 zo->zo_ashift = value;
34dc7c2f
BB
703 break;
704 case 'm':
c242c188 705 zo->zo_mirrors = value;
34dc7c2f
BB
706 break;
707 case 'r':
c242c188 708 zo->zo_raidz = MAX(1, value);
34dc7c2f
BB
709 break;
710 case 'R':
c242c188 711 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
34dc7c2f
BB
712 break;
713 case 'd':
c242c188 714 zo->zo_datasets = MAX(1, value);
34dc7c2f
BB
715 break;
716 case 't':
c242c188 717 zo->zo_threads = MAX(1, value);
34dc7c2f
BB
718 break;
719 case 'g':
c242c188
CS
720 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
721 value);
34dc7c2f
BB
722 break;
723 case 'i':
c242c188 724 zo->zo_init = value;
34dc7c2f
BB
725 break;
726 case 'k':
c242c188 727 zo->zo_killrate = value;
34dc7c2f
BB
728 break;
729 case 'p':
c242c188
CS
730 (void) strlcpy(zo->zo_pool, optarg,
731 sizeof (zo->zo_pool));
34dc7c2f
BB
732 break;
733 case 'f':
c242c188
CS
734 path = realpath(optarg, NULL);
735 if (path == NULL) {
736 (void) fprintf(stderr, "error: %s: %s\n",
737 optarg, strerror(errno));
738 usage(B_FALSE);
739 } else {
740 (void) strlcpy(zo->zo_dir, path,
741 sizeof (zo->zo_dir));
cae5b340 742 free(path);
c242c188 743 }
34dc7c2f 744 break;
cae5b340
AX
745 case 'M':
746 zo->zo_mmp_test = 1;
747 break;
34dc7c2f 748 case 'V':
c242c188 749 zo->zo_verbose++;
34dc7c2f
BB
750 break;
751 case 'E':
c242c188 752 zo->zo_init = 0;
34dc7c2f
BB
753 break;
754 case 'T':
c242c188 755 zo->zo_time = value;
34dc7c2f
BB
756 break;
757 case 'P':
c242c188 758 zo->zo_passtime = MAX(1, value);
34dc7c2f 759 break;
428870ff 760 case 'F':
c242c188
CS
761 zo->zo_maxloops = MAX(1, value);
762 break;
763 case 'B':
764 (void) strlcpy(altdir, optarg, sizeof (altdir));
428870ff 765 break;
cae5b340
AX
766 case 'o':
767 if (set_global_var(optarg) != 0)
768 usage(B_FALSE);
769 break;
34dc7c2f
BB
770 case 'h':
771 usage(B_TRUE);
772 break;
773 case '?':
774 default:
775 usage(B_FALSE);
776 break;
777 }
778 }
779
c242c188 780 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
34dc7c2f 781
c242c188
CS
782 zo->zo_vdevtime =
783 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
428870ff 784 UINT64_MAX >> 2);
c242c188
CS
785
786 if (strlen(altdir) > 0) {
ae380cfa
BB
787 char *cmd;
788 char *realaltdir;
c242c188
CS
789 char *bin;
790 char *ztest;
791 char *isa;
792 int isalen;
793
ae380cfa
BB
794 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
795 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
796
5be98cfe 797 VERIFY(NULL != realpath(getexecname(), cmd));
c242c188
CS
798 if (0 != access(altdir, F_OK)) {
799 ztest_dump_core = B_FALSE;
800 fatal(B_TRUE, "invalid alternate ztest path: %s",
801 altdir);
802 }
803 VERIFY(NULL != realpath(altdir, realaltdir));
804
805 /*
806 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
807 * We want to extract <isa> to determine if we should use
808 * 32 or 64 bit binaries.
809 */
810 bin = strstr(cmd, "/usr/bin/");
811 ztest = strstr(bin, "/ztest");
812 isa = bin + 9;
813 isalen = ztest - isa;
814 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
815 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
816 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
817 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
818
819 if (0 != access(zo->zo_alt_ztest, X_OK)) {
820 ztest_dump_core = B_FALSE;
821 fatal(B_TRUE, "invalid alternate ztest: %s",
822 zo->zo_alt_ztest);
823 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
824 ztest_dump_core = B_FALSE;
825 fatal(B_TRUE, "invalid alternate lib directory %s",
826 zo->zo_alt_libpath);
827 }
ae380cfa
BB
828
829 umem_free(cmd, MAXPATHLEN);
830 umem_free(realaltdir, MAXPATHLEN);
c242c188 831 }
428870ff
BB
832}
833
834static void
835ztest_kill(ztest_shared_t *zs)
836{
c242c188
CS
837 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
838 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
a08ee875
LG
839
840 /*
841 * Before we kill off ztest, make sure that the config is updated.
842 * See comment above spa_config_sync().
843 */
844 mutex_enter(&spa_namespace_lock);
845 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
846 mutex_exit(&spa_namespace_lock);
847
428870ff
BB
848 (void) kill(getpid(), SIGKILL);
849}
850
851static uint64_t
852ztest_random(uint64_t range)
853{
854 uint64_t r;
855
9d81146b
ED
856 ASSERT3S(ztest_fd_rand, >=, 0);
857
428870ff
BB
858 if (range == 0)
859 return (0);
860
9d81146b 861 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
428870ff
BB
862 fatal(1, "short read from /dev/urandom");
863
864 return (r % range);
865}
866
867/* ARGSUSED */
868static void
869ztest_record_enospc(const char *s)
870{
871 ztest_shared->zs_enospc_count++;
34dc7c2f
BB
872}
873
874static uint64_t
875ztest_get_ashift(void)
876{
c242c188 877 if (ztest_opts.zo_ashift == 0)
ea04106b 878 return (SPA_MINBLOCKSHIFT + ztest_random(5));
c242c188 879 return (ztest_opts.zo_ashift);
34dc7c2f
BB
880}
881
882static nvlist_t *
ea0b2538 883make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
34dc7c2f 884{
40b84e7a 885 char *pathbuf;
34dc7c2f 886 uint64_t vdev;
34dc7c2f
BB
887 nvlist_t *file;
888
40b84e7a
BB
889 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
890
b128c09f
BB
891 if (ashift == 0)
892 ashift = ztest_get_ashift();
893
894 if (path == NULL) {
895 path = pathbuf;
896
897 if (aux != NULL) {
898 vdev = ztest_shared->zs_vdev_aux;
c242c188
CS
899 (void) snprintf(path, MAXPATHLEN,
900 ztest_aux_template, ztest_opts.zo_dir,
ea0b2538
GW
901 pool == NULL ? ztest_opts.zo_pool : pool,
902 aux, vdev);
b128c09f 903 } else {
428870ff 904 vdev = ztest_shared->zs_vdev_next_leaf++;
c242c188
CS
905 (void) snprintf(path, MAXPATHLEN,
906 ztest_dev_template, ztest_opts.zo_dir,
ea0b2538 907 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
b128c09f
BB
908 }
909 }
34dc7c2f 910
b128c09f
BB
911 if (size != 0) {
912 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
34dc7c2f 913 if (fd == -1)
b128c09f 914 fatal(1, "can't open %s", path);
34dc7c2f 915 if (ftruncate(fd, size) != 0)
b128c09f 916 fatal(1, "can't ftruncate %s", path);
34dc7c2f
BB
917 (void) close(fd);
918 }
919
920 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
921 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
b128c09f 922 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
34dc7c2f 923 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
40b84e7a 924 umem_free(pathbuf, MAXPATHLEN);
34dc7c2f
BB
925
926 return (file);
927}
928
929static nvlist_t *
ea0b2538
GW
930make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
931 uint64_t ashift, int r)
34dc7c2f
BB
932{
933 nvlist_t *raidz, **child;
934 int c;
935
936 if (r < 2)
ea0b2538 937 return (make_vdev_file(path, aux, pool, size, ashift));
34dc7c2f
BB
938 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
939
940 for (c = 0; c < r; c++)
ea0b2538 941 child[c] = make_vdev_file(path, aux, pool, size, ashift);
34dc7c2f
BB
942
943 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
944 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
945 VDEV_TYPE_RAIDZ) == 0);
946 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
c242c188 947 ztest_opts.zo_raidz_parity) == 0);
34dc7c2f
BB
948 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
949 child, r) == 0);
950
951 for (c = 0; c < r; c++)
952 nvlist_free(child[c]);
953
954 umem_free(child, r * sizeof (nvlist_t *));
955
956 return (raidz);
957}
958
959static nvlist_t *
ea0b2538
GW
960make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
961 uint64_t ashift, int r, int m)
34dc7c2f
BB
962{
963 nvlist_t *mirror, **child;
964 int c;
965
966 if (m < 1)
ea0b2538 967 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
34dc7c2f
BB
968
969 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
970
971 for (c = 0; c < m; c++)
ea0b2538 972 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
34dc7c2f
BB
973
974 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
975 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
976 VDEV_TYPE_MIRROR) == 0);
977 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
978 child, m) == 0);
34dc7c2f
BB
979
980 for (c = 0; c < m; c++)
981 nvlist_free(child[c]);
982
983 umem_free(child, m * sizeof (nvlist_t *));
984
985 return (mirror);
986}
987
988static nvlist_t *
ea0b2538
GW
989make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
990 int log, int r, int m, int t)
34dc7c2f
BB
991{
992 nvlist_t *root, **child;
993 int c;
994
995 ASSERT(t > 0);
996
997 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
998
b128c09f 999 for (c = 0; c < t; c++) {
ea0b2538
GW
1000 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1001 r, m);
b128c09f
BB
1002 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1003 log) == 0);
1004 }
34dc7c2f
BB
1005
1006 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1007 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
b128c09f 1008 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
34dc7c2f
BB
1009 child, t) == 0);
1010
1011 for (c = 0; c < t; c++)
1012 nvlist_free(child[c]);
1013
1014 umem_free(child, t * sizeof (nvlist_t *));
1015
1016 return (root);
1017}
1018
ea0b2538
GW
1019/*
1020 * Find a random spa version. Returns back a random spa version in the
1021 * range [initial_version, SPA_VERSION_FEATURES].
1022 */
1023static uint64_t
1024ztest_random_spa_version(uint64_t initial_version)
1025{
1026 uint64_t version = initial_version;
1027
1028 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1029 version = version +
1030 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1031 }
1032
1033 if (version > SPA_VERSION_BEFORE_FEATURES)
1034 version = SPA_VERSION_FEATURES;
1035
1036 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1037 return (version);
1038}
1039
428870ff
BB
1040static int
1041ztest_random_blocksize(void)
34dc7c2f 1042{
e10b0808
AX
1043 /*
1044 * Choose a block size >= the ashift.
1045 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1046 */
1047 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1048 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1049 maxbs = 20;
ea04106b 1050 uint64_t block_shift =
e10b0808 1051 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
ea04106b 1052 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
428870ff 1053}
34dc7c2f 1054
cae5b340
AX
1055static int
1056ztest_random_dnodesize(void)
1057{
1058 int slots;
1059 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1060
1061 if (max_slots == DNODE_MIN_SLOTS)
1062 return (DNODE_MIN_SIZE);
1063
1064 /*
1065 * Weight the random distribution more heavily toward smaller
1066 * dnode sizes since that is more likely to reflect real-world
1067 * usage.
1068 */
1069 ASSERT3U(max_slots, >, 4);
1070 switch (ztest_random(10)) {
1071 case 0:
1072 slots = 5 + ztest_random(max_slots - 4);
1073 break;
1074 case 1 ... 4:
1075 slots = 2 + ztest_random(3);
1076 break;
1077 default:
1078 slots = 1;
1079 break;
1080 }
1081
1082 return (slots << DNODE_SHIFT);
1083}
1084
428870ff
BB
1085static int
1086ztest_random_ibshift(void)
1087{
1088 return (DN_MIN_INDBLKSHIFT +
1089 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
34dc7c2f
BB
1090}
1091
428870ff
BB
1092static uint64_t
1093ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
34dc7c2f 1094{
428870ff
BB
1095 uint64_t top;
1096 vdev_t *rvd = spa->spa_root_vdev;
1097 vdev_t *tvd;
34dc7c2f 1098
428870ff 1099 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
34dc7c2f 1100
428870ff
BB
1101 do {
1102 top = ztest_random(rvd->vdev_children);
1103 tvd = rvd->vdev_child[top];
1104 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1105 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
34dc7c2f 1106
428870ff 1107 return (top);
34dc7c2f
BB
1108}
1109
428870ff
BB
1110static uint64_t
1111ztest_random_dsl_prop(zfs_prop_t prop)
34dc7c2f 1112{
428870ff
BB
1113 uint64_t value;
1114
1115 do {
1116 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1117 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1118
1119 return (value);
34dc7c2f
BB
1120}
1121
34dc7c2f 1122static int
428870ff
BB
1123ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1124 boolean_t inherit)
34dc7c2f 1125{
428870ff
BB
1126 const char *propname = zfs_prop_to_name(prop);
1127 const char *valname;
40b84e7a 1128 char *setpoint;
428870ff 1129 uint64_t curval;
34dc7c2f
BB
1130 int error;
1131
a08ee875
LG
1132 error = dsl_prop_set_int(osname, propname,
1133 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
34dc7c2f 1134
428870ff
BB
1135 if (error == ENOSPC) {
1136 ztest_record_enospc(FTAG);
34dc7c2f
BB
1137 return (error);
1138 }
c06d4368 1139 ASSERT0(error);
34dc7c2f 1140
40b84e7a 1141 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
a08ee875 1142 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
428870ff 1143
c242c188 1144 if (ztest_opts.zo_verbose >= 6) {
e10b0808
AX
1145 int err;
1146
1147 err = zfs_prop_index_to_string(prop, curval, &valname);
1148 if (err)
cae5b340
AX
1149 (void) printf("%s %s = %llu at '%s'\n", osname,
1150 propname, (unsigned long long)curval, setpoint);
e10b0808
AX
1151 else
1152 (void) printf("%s %s = %s at '%s'\n",
1153 osname, propname, valname, setpoint);
34dc7c2f 1154 }
40b84e7a 1155 umem_free(setpoint, MAXPATHLEN);
34dc7c2f
BB
1156
1157 return (error);
1158}
1159
1160static int
c242c188 1161ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
34dc7c2f 1162{
c242c188 1163 spa_t *spa = ztest_spa;
428870ff 1164 nvlist_t *props = NULL;
34dc7c2f
BB
1165 int error;
1166
428870ff
BB
1167 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1168 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
34dc7c2f 1169
428870ff
BB
1170 error = spa_prop_set(spa, props);
1171
1172 nvlist_free(props);
1173
1174 if (error == ENOSPC) {
1175 ztest_record_enospc(FTAG);
34dc7c2f
BB
1176 return (error);
1177 }
c06d4368 1178 ASSERT0(error);
34dc7c2f
BB
1179
1180 return (error);
1181}
1182
cae5b340
AX
1183
1184/*
1185 * Object and range lock mechanics
1186 */
1187typedef struct {
1188 list_node_t z_lnode;
1189 refcount_t z_refcnt;
1190 uint64_t z_object;
1191 zfs_rlock_t z_range_lock;
1192} ztest_znode_t;
1193
1194typedef struct {
1195 rl_t *z_rl;
1196 ztest_znode_t *z_ztznode;
1197} ztest_zrl_t;
1198
1199static ztest_znode_t *
1200ztest_znode_init(uint64_t object)
1201{
1202 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1203
1204 list_link_init(&zp->z_lnode);
1205 refcount_create(&zp->z_refcnt);
1206 zp->z_object = object;
1207 zfs_rlock_init(&zp->z_range_lock);
1208
1209 return (zp);
1210}
1211
1212static void
1213ztest_znode_fini(ztest_znode_t *zp)
1214{
1215 ASSERT(refcount_is_zero(&zp->z_refcnt));
1216 zfs_rlock_destroy(&zp->z_range_lock);
1217 zp->z_object = 0;
1218 refcount_destroy(&zp->z_refcnt);
1219 list_link_init(&zp->z_lnode);
1220 umem_free(zp, sizeof (*zp));
1221}
1222
1223static void
1224ztest_zll_init(zll_t *zll)
1225{
1226 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1227 list_create(&zll->z_list, sizeof (ztest_znode_t),
1228 offsetof(ztest_znode_t, z_lnode));
1229}
1230
1231static void
1232ztest_zll_destroy(zll_t *zll)
1233{
1234 list_destroy(&zll->z_list);
1235 mutex_destroy(&zll->z_lock);
1236}
1237
1238#define RL_TAG "range_lock"
1239static ztest_znode_t *
1240ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1241{
1242 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1243 ztest_znode_t *zp = NULL;
1244 mutex_enter(&zll->z_lock);
1245 for (zp = list_head(&zll->z_list); (zp);
1246 zp = list_next(&zll->z_list, zp)) {
1247 if (zp->z_object == object) {
1248 refcount_add(&zp->z_refcnt, RL_TAG);
1249 break;
1250 }
1251 }
1252 if (zp == NULL) {
1253 zp = ztest_znode_init(object);
1254 refcount_add(&zp->z_refcnt, RL_TAG);
1255 list_insert_head(&zll->z_list, zp);
1256 }
1257 mutex_exit(&zll->z_lock);
1258 return (zp);
1259}
1260
1261static void
1262ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1263{
1264 zll_t *zll = NULL;
1265 ASSERT3U(zp->z_object, !=, 0);
1266 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1267 mutex_enter(&zll->z_lock);
1268 refcount_remove(&zp->z_refcnt, RL_TAG);
1269 if (refcount_is_zero(&zp->z_refcnt)) {
1270 list_remove(&zll->z_list, zp);
1271 ztest_znode_fini(zp);
1272 }
1273 mutex_exit(&zll->z_lock);
1274}
1275
1276
428870ff
BB
1277static void
1278ztest_rll_init(rll_t *rll)
1279{
1280 rll->rll_writer = NULL;
1281 rll->rll_readers = 0;
1e33ac1e
BB
1282 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1283 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
428870ff 1284}
34dc7c2f 1285
428870ff
BB
1286static void
1287ztest_rll_destroy(rll_t *rll)
34dc7c2f 1288{
428870ff
BB
1289 ASSERT(rll->rll_writer == NULL);
1290 ASSERT(rll->rll_readers == 0);
1e33ac1e
BB
1291 mutex_destroy(&rll->rll_lock);
1292 cv_destroy(&rll->rll_cv);
428870ff 1293}
34dc7c2f 1294
428870ff
BB
1295static void
1296ztest_rll_lock(rll_t *rll, rl_type_t type)
1297{
1e33ac1e 1298 mutex_enter(&rll->rll_lock);
34dc7c2f 1299
428870ff
BB
1300 if (type == RL_READER) {
1301 while (rll->rll_writer != NULL)
1e33ac1e 1302 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
428870ff
BB
1303 rll->rll_readers++;
1304 } else {
1305 while (rll->rll_writer != NULL || rll->rll_readers)
1e33ac1e 1306 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
428870ff
BB
1307 rll->rll_writer = curthread;
1308 }
34dc7c2f 1309
1e33ac1e 1310 mutex_exit(&rll->rll_lock);
428870ff 1311}
34dc7c2f 1312
428870ff
BB
1313static void
1314ztest_rll_unlock(rll_t *rll)
1315{
1e33ac1e 1316 mutex_enter(&rll->rll_lock);
34dc7c2f 1317
428870ff
BB
1318 if (rll->rll_writer) {
1319 ASSERT(rll->rll_readers == 0);
1320 rll->rll_writer = NULL;
1321 } else {
1322 ASSERT(rll->rll_readers != 0);
1323 ASSERT(rll->rll_writer == NULL);
1324 rll->rll_readers--;
1325 }
34dc7c2f 1326
428870ff 1327 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1e33ac1e 1328 cv_broadcast(&rll->rll_cv);
428870ff 1329
1e33ac1e 1330 mutex_exit(&rll->rll_lock);
34dc7c2f
BB
1331}
1332
428870ff
BB
1333static void
1334ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
b128c09f 1335{
428870ff 1336 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 1337
428870ff
BB
1338 ztest_rll_lock(rll, type);
1339}
b128c09f 1340
428870ff
BB
1341static void
1342ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1343{
1344 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
b128c09f 1345
428870ff 1346 ztest_rll_unlock(rll);
b128c09f
BB
1347}
1348
cae5b340
AX
1349static ztest_zrl_t *
1350ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
34dc7c2f 1351{
cae5b340
AX
1352 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1353 zrl->z_rl = rl;
1354 zrl->z_ztznode = zp;
1355 return (zrl);
428870ff 1356}
34dc7c2f 1357
428870ff 1358static void
cae5b340 1359ztest_zrl_fini(ztest_zrl_t *zrl)
428870ff 1360{
cae5b340
AX
1361 umem_free(zrl, sizeof (*zrl));
1362}
34dc7c2f 1363
cae5b340
AX
1364static ztest_zrl_t *
1365ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1366 uint64_t size, rl_type_t type)
1367{
1368 ztest_znode_t *zp = ztest_znode_get(zd, object);
1369 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1370 size, type);
1371 return (ztest_zrl_init(rl, zp));
1372}
34dc7c2f 1373
cae5b340
AX
1374static void
1375ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1376{
1377 zfs_range_unlock(zrl->z_rl);
1378 ztest_znode_put(zd, zrl->z_ztznode);
1379 ztest_zrl_fini(zrl);
428870ff 1380}
34dc7c2f 1381
428870ff 1382static void
c242c188 1383ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
428870ff
BB
1384{
1385 zd->zd_os = os;
1386 zd->zd_zilog = dmu_objset_zil(os);
c242c188 1387 zd->zd_shared = szd;
428870ff 1388 dmu_objset_name(os, zd->zd_name);
d6320ddb 1389 int l;
428870ff 1390
c242c188
CS
1391 if (zd->zd_shared != NULL)
1392 zd->zd_shared->zd_seq = 0;
1393
ea04106b 1394 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1e33ac1e 1395 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 1396
d6320ddb 1397 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 1398 ztest_rll_init(&zd->zd_object_lock[l]);
34dc7c2f 1399
d6320ddb 1400 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
cae5b340 1401 ztest_zll_init(&zd->zd_range_lock[l]);
34dc7c2f
BB
1402}
1403
428870ff
BB
1404static void
1405ztest_zd_fini(ztest_ds_t *zd)
34dc7c2f 1406{
d6320ddb
BB
1407 int l;
1408
1e33ac1e 1409 mutex_destroy(&zd->zd_dirobj_lock);
ea04106b 1410 (void) rwlock_destroy(&zd->zd_zilog_lock);
34dc7c2f 1411
d6320ddb 1412 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
428870ff 1413 ztest_rll_destroy(&zd->zd_object_lock[l]);
b128c09f 1414
d6320ddb 1415 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
cae5b340 1416 ztest_zll_destroy(&zd->zd_range_lock[l]);
428870ff 1417}
b128c09f 1418
428870ff 1419#define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
b128c09f 1420
428870ff
BB
1421static uint64_t
1422ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1423{
1424 uint64_t txg;
1425 int error;
1426
1427 /*
1428 * Attempt to assign tx to some transaction group.
1429 */
1430 error = dmu_tx_assign(tx, txg_how);
1431 if (error) {
1432 if (error == ERESTART) {
1433 ASSERT(txg_how == TXG_NOWAIT);
1434 dmu_tx_wait(tx);
1435 } else {
1436 ASSERT3U(error, ==, ENOSPC);
1437 ztest_record_enospc(tag);
1438 }
1439 dmu_tx_abort(tx);
1440 return (0);
1441 }
1442 txg = dmu_tx_get_txg(tx);
1443 ASSERT(txg != 0);
1444 return (txg);
1445}
1446
1447static void
1448ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1449{
1450 uint64_t *ip = buf;
1451 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1452
1453 while (ip < ip_end)
1454 *ip++ = value;
1455}
1456
1fde1e37 1457#ifndef NDEBUG
428870ff
BB
1458static boolean_t
1459ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1460{
1461 uint64_t *ip = buf;
1462 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1463 uint64_t diff = 0;
1464
1465 while (ip < ip_end)
1466 diff |= (value - *ip++);
1467
1468 return (diff == 0);
1469}
1fde1e37 1470#endif
428870ff
BB
1471
1472static void
1473ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
cae5b340
AX
1474 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1475 uint64_t crtxg)
428870ff
BB
1476{
1477 bt->bt_magic = BT_MAGIC;
1478 bt->bt_objset = dmu_objset_id(os);
1479 bt->bt_object = object;
cae5b340 1480 bt->bt_dnodesize = dnodesize;
428870ff
BB
1481 bt->bt_offset = offset;
1482 bt->bt_gen = gen;
1483 bt->bt_txg = txg;
1484 bt->bt_crtxg = crtxg;
1485}
1486
1487static void
1488ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
cae5b340
AX
1489 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1490 uint64_t crtxg)
428870ff 1491{
ea04106b
AX
1492 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1493 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1494 ASSERT3U(bt->bt_object, ==, object);
cae5b340 1495 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
ea04106b
AX
1496 ASSERT3U(bt->bt_offset, ==, offset);
1497 ASSERT3U(bt->bt_gen, <=, gen);
1498 ASSERT3U(bt->bt_txg, <=, txg);
1499 ASSERT3U(bt->bt_crtxg, ==, crtxg);
428870ff
BB
1500}
1501
1502static ztest_block_tag_t *
1503ztest_bt_bonus(dmu_buf_t *db)
1504{
1505 dmu_object_info_t doi;
1506 ztest_block_tag_t *bt;
1507
1508 dmu_object_info_from_db(db, &doi);
1509 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1510 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1511 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1512
1513 return (bt);
1514}
1515
cae5b340
AX
1516/*
1517 * Generate a token to fill up unused bonus buffer space. Try to make
1518 * it unique to the object, generation, and offset to verify that data
1519 * is not getting overwritten by data from other dnodes.
1520 */
1521#define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1522 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1523
1524/*
1525 * Fill up the unused bonus buffer region before the block tag with a
1526 * verifiable pattern. Filling the whole bonus area with non-zero data
1527 * helps ensure that all dnode traversal code properly skips the
1528 * interior regions of large dnodes.
1529 */
1530void
1531ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1532 objset_t *os, uint64_t gen)
1533{
1534 uint64_t *bonusp;
1535
1536 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1537
1538 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1539 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1540 gen, bonusp - (uint64_t *)db->db_data);
1541 *bonusp = token;
1542 }
1543}
1544
1545/*
1546 * Verify that the unused area of a bonus buffer is filled with the
1547 * expected tokens.
1548 */
1549void
1550ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1551 objset_t *os, uint64_t gen)
1552{
1553 uint64_t *bonusp;
1554
1555 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1556 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1557 gen, bonusp - (uint64_t *)db->db_data);
1558 VERIFY3U(*bonusp, ==, token);
1559 }
1560}
1561
428870ff
BB
1562/*
1563 * ZIL logging ops
1564 */
1565
1566#define lrz_type lr_mode
1567#define lrz_blocksize lr_uid
1568#define lrz_ibshift lr_gid
1569#define lrz_bonustype lr_rdev
cae5b340 1570#define lrz_dnodesize lr_crtime[1]
428870ff 1571
572e2857 1572static void
428870ff
BB
1573ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1574{
1575 char *name = (void *)(lr + 1); /* name follows lr */
1576 size_t namesize = strlen(name) + 1;
1577 itx_t *itx;
1578
1579 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1580 return;
428870ff
BB
1581
1582 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1583 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1584 sizeof (*lr) + namesize - sizeof (lr_t));
1585
572e2857 1586 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1587}
1588
572e2857
BB
1589static void
1590ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
428870ff
BB
1591{
1592 char *name = (void *)(lr + 1); /* name follows lr */
1593 size_t namesize = strlen(name) + 1;
1594 itx_t *itx;
1595
1596 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1597 return;
428870ff
BB
1598
1599 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1600 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1601 sizeof (*lr) + namesize - sizeof (lr_t));
1602
572e2857
BB
1603 itx->itx_oid = object;
1604 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1605}
1606
572e2857 1607static void
428870ff
BB
1608ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1609{
1610 itx_t *itx;
1611 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1612
1613 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1614 return;
428870ff
BB
1615
1616 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1617 write_state = WR_INDIRECT;
1618
1619 itx = zil_itx_create(TX_WRITE,
1620 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1621
1622 if (write_state == WR_COPIED &&
1623 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1624 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1625 zil_itx_destroy(itx);
1626 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1627 write_state = WR_NEED_COPY;
1628 }
1629 itx->itx_private = zd;
1630 itx->itx_wr_state = write_state;
1631 itx->itx_sync = (ztest_random(8) == 0);
428870ff
BB
1632
1633 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1634 sizeof (*lr) - sizeof (lr_t));
1635
572e2857 1636 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1637}
1638
572e2857 1639static void
428870ff
BB
1640ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1641{
1642 itx_t *itx;
1643
1644 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1645 return;
428870ff
BB
1646
1647 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1648 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1649 sizeof (*lr) - sizeof (lr_t));
1650
572e2857
BB
1651 itx->itx_sync = B_FALSE;
1652 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1653}
1654
572e2857 1655static void
428870ff
BB
1656ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1657{
1658 itx_t *itx;
1659
1660 if (zil_replaying(zd->zd_zilog, tx))
572e2857 1661 return;
428870ff
BB
1662
1663 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1664 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1665 sizeof (*lr) - sizeof (lr_t));
1666
572e2857
BB
1667 itx->itx_sync = B_FALSE;
1668 zil_itx_assign(zd->zd_zilog, itx, tx);
428870ff
BB
1669}
1670
1671/*
1672 * ZIL replay ops
1673 */
1674static int
1675ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1676{
1677 char *name = (void *)(lr + 1); /* name follows lr */
1678 objset_t *os = zd->zd_os;
1679 ztest_block_tag_t *bbt;
1680 dmu_buf_t *db;
1681 dmu_tx_t *tx;
1682 uint64_t txg;
1683 int error = 0;
cae5b340 1684 int bonuslen;
428870ff
BB
1685
1686 if (byteswap)
1687 byteswap_uint64_array(lr, sizeof (*lr));
1688
1689 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1690 ASSERT(name[0] != '\0');
1691
1692 tx = dmu_tx_create(os);
1693
1694 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1695
1696 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1697 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1698 } else {
1699 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1700 }
1701
1702 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1703 if (txg == 0)
1704 return (ENOSPC);
1705
1706 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
cae5b340 1707 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
428870ff
BB
1708
1709 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1710 if (lr->lr_foid == 0) {
cae5b340 1711 lr->lr_foid = zap_create_dnsize(os,
428870ff 1712 lr->lrz_type, lr->lrz_bonustype,
cae5b340 1713 bonuslen, lr->lrz_dnodesize, tx);
428870ff 1714 } else {
cae5b340 1715 error = zap_create_claim_dnsize(os, lr->lr_foid,
428870ff 1716 lr->lrz_type, lr->lrz_bonustype,
cae5b340 1717 bonuslen, lr->lrz_dnodesize, tx);
428870ff
BB
1718 }
1719 } else {
1720 if (lr->lr_foid == 0) {
cae5b340 1721 lr->lr_foid = dmu_object_alloc_dnsize(os,
428870ff 1722 lr->lrz_type, 0, lr->lrz_bonustype,
cae5b340 1723 bonuslen, lr->lrz_dnodesize, tx);
428870ff 1724 } else {
cae5b340 1725 error = dmu_object_claim_dnsize(os, lr->lr_foid,
428870ff 1726 lr->lrz_type, 0, lr->lrz_bonustype,
cae5b340 1727 bonuslen, lr->lrz_dnodesize, tx);
428870ff
BB
1728 }
1729 }
1730
1731 if (error) {
1732 ASSERT3U(error, ==, EEXIST);
1733 ASSERT(zd->zd_zilog->zl_replay);
1734 dmu_tx_commit(tx);
1735 return (error);
1736 }
1737
1738 ASSERT(lr->lr_foid != 0);
1739
1740 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1741 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1742 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1743
1744 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1745 bbt = ztest_bt_bonus(db);
1746 dmu_buf_will_dirty(db, tx);
cae5b340
AX
1747 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1748 lr->lr_gen, txg, txg);
1749 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
428870ff
BB
1750 dmu_buf_rele(db, FTAG);
1751
1752 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1753 &lr->lr_foid, tx));
1754
1755 (void) ztest_log_create(zd, tx, lr);
1756
1757 dmu_tx_commit(tx);
1758
1759 return (0);
1760}
1761
1762static int
1763ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1764{
1765 char *name = (void *)(lr + 1); /* name follows lr */
1766 objset_t *os = zd->zd_os;
1767 dmu_object_info_t doi;
1768 dmu_tx_t *tx;
1769 uint64_t object, txg;
1770
1771 if (byteswap)
1772 byteswap_uint64_array(lr, sizeof (*lr));
1773
1774 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1775 ASSERT(name[0] != '\0');
1776
1777 VERIFY3U(0, ==,
1778 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1779 ASSERT(object != 0);
1780
1781 ztest_object_lock(zd, object, RL_WRITER);
1782
1783 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1784
1785 tx = dmu_tx_create(os);
1786
1787 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1788 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1789
1790 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1791 if (txg == 0) {
1792 ztest_object_unlock(zd, object);
1793 return (ENOSPC);
1794 }
1795
1796 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1797 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1798 } else {
1799 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1800 }
1801
1802 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1803
572e2857 1804 (void) ztest_log_remove(zd, tx, lr, object);
428870ff
BB
1805
1806 dmu_tx_commit(tx);
1807
1808 ztest_object_unlock(zd, object);
1809
1810 return (0);
1811}
1812
1813static int
1814ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1815{
1816 objset_t *os = zd->zd_os;
1817 void *data = lr + 1; /* data follows lr */
1818 uint64_t offset, length;
1819 ztest_block_tag_t *bt = data;
1820 ztest_block_tag_t *bbt;
1821 uint64_t gen, txg, lrtxg, crtxg;
1822 dmu_object_info_t doi;
1823 dmu_tx_t *tx;
1824 dmu_buf_t *db;
1825 arc_buf_t *abuf = NULL;
cae5b340 1826 ztest_zrl_t *rl;
428870ff
BB
1827
1828 if (byteswap)
1829 byteswap_uint64_array(lr, sizeof (*lr));
1830
1831 offset = lr->lr_offset;
1832 length = lr->lr_length;
1833
1834 /* If it's a dmu_sync() block, write the whole block */
1835 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1836 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1837 if (length < blocksize) {
1838 offset -= offset % blocksize;
1839 length = blocksize;
1840 }
1841 }
1842
1843 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1844 byteswap_uint64_array(bt, sizeof (*bt));
1845
1846 if (bt->bt_magic != BT_MAGIC)
1847 bt = NULL;
1848
1849 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1850 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1851
1852 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1853
1854 dmu_object_info_from_db(db, &doi);
1855
1856 bbt = ztest_bt_bonus(db);
1857 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1858 gen = bbt->bt_gen;
1859 crtxg = bbt->bt_crtxg;
1860 lrtxg = lr->lr_common.lrc_txg;
1861
1862 tx = dmu_tx_create(os);
1863
1864 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1865
1866 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1867 P2PHASE(offset, length) == 0)
1868 abuf = dmu_request_arcbuf(db, length);
1869
1870 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1871 if (txg == 0) {
1872 if (abuf != NULL)
1873 dmu_return_arcbuf(abuf);
1874 dmu_buf_rele(db, FTAG);
cae5b340 1875 ztest_range_unlock(zd, rl);
428870ff
BB
1876 ztest_object_unlock(zd, lr->lr_foid);
1877 return (ENOSPC);
1878 }
1879
1880 if (bt != NULL) {
1881 /*
1882 * Usually, verify the old data before writing new data --
1883 * but not always, because we also want to verify correct
1884 * behavior when the data was not recently read into cache.
1885 */
1886 ASSERT(offset % doi.doi_data_block_size == 0);
1887 if (ztest_random(4) != 0) {
1888 int prefetch = ztest_random(2) ?
1889 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1890 ztest_block_tag_t rbt;
1891
1892 VERIFY(dmu_read(os, lr->lr_foid, offset,
1893 sizeof (rbt), &rbt, prefetch) == 0);
1894 if (rbt.bt_magic == BT_MAGIC) {
cae5b340 1895 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
428870ff
BB
1896 offset, gen, txg, crtxg);
1897 }
1898 }
1899
1900 /*
1901 * Writes can appear to be newer than the bonus buffer because
1902 * the ztest_get_data() callback does a dmu_read() of the
1903 * open-context data, which may be different than the data
1904 * as it was when the write was generated.
1905 */
1906 if (zd->zd_zilog->zl_replay) {
cae5b340 1907 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
428870ff
BB
1908 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1909 bt->bt_crtxg);
1910 }
1911
1912 /*
1913 * Set the bt's gen/txg to the bonus buffer's gen/txg
1914 * so that all of the usual ASSERTs will work.
1915 */
cae5b340
AX
1916 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1917 crtxg);
428870ff
BB
1918 }
1919
1920 if (abuf == NULL) {
1921 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1922 } else {
1923 bcopy(data, abuf->b_data, length);
1924 dmu_assign_arcbuf(db, offset, abuf, tx);
1925 }
1926
1927 (void) ztest_log_write(zd, tx, lr);
1928
1929 dmu_buf_rele(db, FTAG);
1930
1931 dmu_tx_commit(tx);
1932
cae5b340 1933 ztest_range_unlock(zd, rl);
428870ff
BB
1934 ztest_object_unlock(zd, lr->lr_foid);
1935
1936 return (0);
1937}
1938
1939static int
1940ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1941{
1942 objset_t *os = zd->zd_os;
1943 dmu_tx_t *tx;
1944 uint64_t txg;
cae5b340 1945 ztest_zrl_t *rl;
428870ff
BB
1946
1947 if (byteswap)
1948 byteswap_uint64_array(lr, sizeof (*lr));
1949
1950 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1951 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1952 RL_WRITER);
1953
1954 tx = dmu_tx_create(os);
1955
1956 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1957
1958 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1959 if (txg == 0) {
cae5b340 1960 ztest_range_unlock(zd, rl);
428870ff
BB
1961 ztest_object_unlock(zd, lr->lr_foid);
1962 return (ENOSPC);
1963 }
1964
1965 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1966 lr->lr_length, tx) == 0);
1967
1968 (void) ztest_log_truncate(zd, tx, lr);
1969
1970 dmu_tx_commit(tx);
1971
cae5b340 1972 ztest_range_unlock(zd, rl);
428870ff
BB
1973 ztest_object_unlock(zd, lr->lr_foid);
1974
1975 return (0);
1976}
1977
1978static int
1979ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1980{
1981 objset_t *os = zd->zd_os;
1982 dmu_tx_t *tx;
1983 dmu_buf_t *db;
1984 ztest_block_tag_t *bbt;
cae5b340 1985 uint64_t txg, lrtxg, crtxg, dnodesize;
428870ff
BB
1986
1987 if (byteswap)
1988 byteswap_uint64_array(lr, sizeof (*lr));
1989
1990 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1991
1992 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1993
1994 tx = dmu_tx_create(os);
1995 dmu_tx_hold_bonus(tx, lr->lr_foid);
1996
1997 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1998 if (txg == 0) {
1999 dmu_buf_rele(db, FTAG);
2000 ztest_object_unlock(zd, lr->lr_foid);
2001 return (ENOSPC);
2002 }
2003
2004 bbt = ztest_bt_bonus(db);
2005 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2006 crtxg = bbt->bt_crtxg;
2007 lrtxg = lr->lr_common.lrc_txg;
cae5b340 2008 dnodesize = bbt->bt_dnodesize;
428870ff
BB
2009
2010 if (zd->zd_zilog->zl_replay) {
2011 ASSERT(lr->lr_size != 0);
2012 ASSERT(lr->lr_mode != 0);
2013 ASSERT(lrtxg != 0);
2014 } else {
2015 /*
2016 * Randomly change the size and increment the generation.
2017 */
2018 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2019 sizeof (*bbt);
2020 lr->lr_mode = bbt->bt_gen + 1;
2021 ASSERT(lrtxg == 0);
2022 }
2023
2024 /*
2025 * Verify that the current bonus buffer is not newer than our txg.
2026 */
cae5b340 2027 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
428870ff
BB
2028 MAX(txg, lrtxg), crtxg);
2029
2030 dmu_buf_will_dirty(db, tx);
2031
2032 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2033 ASSERT3U(lr->lr_size, <=, db->db_size);
c06d4368 2034 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
428870ff
BB
2035 bbt = ztest_bt_bonus(db);
2036
cae5b340
AX
2037 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2038 txg, crtxg);
2039 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
428870ff
BB
2040 dmu_buf_rele(db, FTAG);
2041
2042 (void) ztest_log_setattr(zd, tx, lr);
2043
2044 dmu_tx_commit(tx);
2045
2046 ztest_object_unlock(zd, lr->lr_foid);
2047
2048 return (0);
2049}
2050
b01615d5 2051zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
0bc8fd78 2052 NULL, /* 0 no such transaction type */
b01615d5 2053 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
0bc8fd78
BB
2054 NULL, /* TX_MKDIR */
2055 NULL, /* TX_MKXATTR */
2056 NULL, /* TX_SYMLINK */
b01615d5 2057 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
0bc8fd78
BB
2058 NULL, /* TX_RMDIR */
2059 NULL, /* TX_LINK */
2060 NULL, /* TX_RENAME */
b01615d5
RY
2061 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2062 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2063 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
0bc8fd78
BB
2064 NULL, /* TX_ACL */
2065 NULL, /* TX_CREATE_ACL */
2066 NULL, /* TX_CREATE_ATTR */
2067 NULL, /* TX_CREATE_ACL_ATTR */
2068 NULL, /* TX_MKDIR_ACL */
2069 NULL, /* TX_MKDIR_ATTR */
2070 NULL, /* TX_MKDIR_ACL_ATTR */
2071 NULL, /* TX_WRITE2 */
428870ff
BB
2072};
2073
2074/*
2075 * ZIL get_data callbacks
2076 */
cae5b340
AX
2077typedef struct ztest_zgd_private {
2078 ztest_ds_t *z_zd;
2079 ztest_zrl_t *z_rl;
2080 uint64_t z_object;
2081} ztest_zgd_private_t;
428870ff
BB
2082
2083static void
2084ztest_get_done(zgd_t *zgd, int error)
2085{
cae5b340
AX
2086 ztest_zgd_private_t *zzp = zgd->zgd_private;
2087 ztest_ds_t *zd = zzp->z_zd;
2088 uint64_t object = zzp->z_object;
428870ff
BB
2089
2090 if (zgd->zgd_db)
2091 dmu_buf_rele(zgd->zgd_db, zgd);
2092
cae5b340 2093 ztest_range_unlock(zd, zzp->z_rl);
428870ff
BB
2094 ztest_object_unlock(zd, object);
2095
2096 if (error == 0 && zgd->zgd_bp)
2097 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2098
2099 umem_free(zgd, sizeof (*zgd));
cae5b340 2100 umem_free(zzp, sizeof (*zzp));
428870ff
BB
2101}
2102
2103static int
2104ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2105{
2106 ztest_ds_t *zd = arg;
2107 objset_t *os = zd->zd_os;
2108 uint64_t object = lr->lr_foid;
2109 uint64_t offset = lr->lr_offset;
2110 uint64_t size = lr->lr_length;
428870ff
BB
2111 uint64_t txg = lr->lr_common.lrc_txg;
2112 uint64_t crtxg;
2113 dmu_object_info_t doi;
2114 dmu_buf_t *db;
2115 zgd_t *zgd;
2116 int error;
cae5b340 2117 ztest_zgd_private_t *zgd_private;
428870ff
BB
2118
2119 ztest_object_lock(zd, object, RL_READER);
2120 error = dmu_bonus_hold(os, object, FTAG, &db);
2121 if (error) {
2122 ztest_object_unlock(zd, object);
2123 return (error);
2124 }
2125
2126 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2127
2128 if (crtxg == 0 || crtxg > txg) {
2129 dmu_buf_rele(db, FTAG);
2130 ztest_object_unlock(zd, object);
2131 return (ENOENT);
2132 }
2133
2134 dmu_object_info_from_db(db, &doi);
2135 dmu_buf_rele(db, FTAG);
2136 db = NULL;
2137
2138 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2139 zgd->zgd_zilog = zd->zd_zilog;
cae5b340
AX
2140 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2141 zgd_private->z_zd = zd;
2142 zgd_private->z_object = object;
2143 zgd->zgd_private = zgd_private;
428870ff
BB
2144
2145 if (buf != NULL) { /* immediate write */
cae5b340 2146 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
428870ff 2147 RL_READER);
cae5b340 2148 zgd->zgd_rl = zgd_private->z_rl->z_rl;
428870ff
BB
2149
2150 error = dmu_read(os, object, offset, size, buf,
2151 DMU_READ_NO_PREFETCH);
2152 ASSERT(error == 0);
2153 } else {
2154 size = doi.doi_data_block_size;
2155 if (ISP2(size)) {
2156 offset = P2ALIGN(offset, size);
2157 } else {
2158 ASSERT(offset < size);
2159 offset = 0;
2160 }
2161
cae5b340 2162 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
428870ff 2163 RL_READER);
cae5b340 2164 zgd->zgd_rl = zgd_private->z_rl->z_rl;
428870ff
BB
2165
2166 error = dmu_buf_hold(os, object, offset, zgd, &db,
2167 DMU_READ_NO_PREFETCH);
2168
2169 if (error == 0) {
cae5b340 2170 blkptr_t *bp = &lr->lr_blkptr;
a08ee875 2171
428870ff
BB
2172 zgd->zgd_db = db;
2173 zgd->zgd_bp = bp;
2174
2175 ASSERT(db->db_offset == offset);
2176 ASSERT(db->db_size == size);
2177
2178 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2179 ztest_get_done, zgd);
2180
2181 if (error == 0)
2182 return (0);
2183 }
2184 }
2185
2186 ztest_get_done(zgd, error);
2187
2188 return (error);
2189}
2190
2191static void *
2192ztest_lr_alloc(size_t lrsize, char *name)
2193{
2194 char *lr;
2195 size_t namesize = name ? strlen(name) + 1 : 0;
2196
2197 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2198
2199 if (name)
2200 bcopy(name, lr + lrsize, namesize);
2201
2202 return (lr);
2203}
2204
2205void
2206ztest_lr_free(void *lr, size_t lrsize, char *name)
2207{
2208 size_t namesize = name ? strlen(name) + 1 : 0;
2209
2210 umem_free(lr, lrsize + namesize);
2211}
2212
2213/*
2214 * Lookup a bunch of objects. Returns the number of objects not found.
2215 */
2216static int
2217ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2218{
2219 int missing = 0;
2220 int error;
d6320ddb 2221 int i;
428870ff 2222
1e33ac1e 2223 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff 2224
d6320ddb 2225 for (i = 0; i < count; i++, od++) {
428870ff
BB
2226 od->od_object = 0;
2227 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2228 sizeof (uint64_t), 1, &od->od_object);
2229 if (error) {
2230 ASSERT(error == ENOENT);
2231 ASSERT(od->od_object == 0);
2232 missing++;
2233 } else {
2234 dmu_buf_t *db;
2235 ztest_block_tag_t *bbt;
2236 dmu_object_info_t doi;
2237
2238 ASSERT(od->od_object != 0);
2239 ASSERT(missing == 0); /* there should be no gaps */
2240
2241 ztest_object_lock(zd, od->od_object, RL_READER);
2242 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2243 od->od_object, FTAG, &db));
2244 dmu_object_info_from_db(db, &doi);
2245 bbt = ztest_bt_bonus(db);
2246 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2247 od->od_type = doi.doi_type;
2248 od->od_blocksize = doi.doi_data_block_size;
2249 od->od_gen = bbt->bt_gen;
2250 dmu_buf_rele(db, FTAG);
2251 ztest_object_unlock(zd, od->od_object);
2252 }
2253 }
2254
2255 return (missing);
2256}
2257
2258static int
2259ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2260{
2261 int missing = 0;
d6320ddb 2262 int i;
428870ff 2263
1e33ac1e 2264 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff 2265
d6320ddb 2266 for (i = 0; i < count; i++, od++) {
428870ff
BB
2267 if (missing) {
2268 od->od_object = 0;
2269 missing++;
2270 continue;
2271 }
2272
2273 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2274
2275 lr->lr_doid = od->od_dir;
2276 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2277 lr->lrz_type = od->od_crtype;
2278 lr->lrz_blocksize = od->od_crblocksize;
2279 lr->lrz_ibshift = ztest_random_ibshift();
2280 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
cae5b340 2281 lr->lrz_dnodesize = od->od_crdnodesize;
428870ff
BB
2282 lr->lr_gen = od->od_crgen;
2283 lr->lr_crtime[0] = time(NULL);
2284
2285 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2286 ASSERT(missing == 0);
2287 od->od_object = 0;
2288 missing++;
2289 } else {
2290 od->od_object = lr->lr_foid;
2291 od->od_type = od->od_crtype;
2292 od->od_blocksize = od->od_crblocksize;
2293 od->od_gen = od->od_crgen;
2294 ASSERT(od->od_object != 0);
2295 }
2296
2297 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2298 }
2299
2300 return (missing);
2301}
2302
2303static int
2304ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2305{
2306 int missing = 0;
2307 int error;
d6320ddb 2308 int i;
428870ff 2309
1e33ac1e 2310 ASSERT(mutex_held(&zd->zd_dirobj_lock));
428870ff
BB
2311
2312 od += count - 1;
2313
d6320ddb 2314 for (i = count - 1; i >= 0; i--, od--) {
428870ff
BB
2315 if (missing) {
2316 missing++;
2317 continue;
2318 }
2319
a08ee875
LG
2320 /*
2321 * No object was found.
2322 */
428870ff
BB
2323 if (od->od_object == 0)
2324 continue;
2325
2326 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2327
2328 lr->lr_doid = od->od_dir;
2329
2330 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2331 ASSERT3U(error, ==, ENOSPC);
2332 missing++;
2333 } else {
2334 od->od_object = 0;
2335 }
2336 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2337 }
2338
2339 return (missing);
2340}
2341
2342static int
2343ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2344 void *data)
2345{
2346 lr_write_t *lr;
2347 int error;
2348
2349 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2350
2351 lr->lr_foid = object;
2352 lr->lr_offset = offset;
2353 lr->lr_length = size;
2354 lr->lr_blkoff = 0;
2355 BP_ZERO(&lr->lr_blkptr);
2356
2357 bcopy(data, lr + 1, size);
2358
2359 error = ztest_replay_write(zd, lr, B_FALSE);
2360
2361 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2362
2363 return (error);
2364}
2365
2366static int
2367ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2368{
2369 lr_truncate_t *lr;
2370 int error;
2371
2372 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2373
2374 lr->lr_foid = object;
2375 lr->lr_offset = offset;
2376 lr->lr_length = size;
2377
2378 error = ztest_replay_truncate(zd, lr, B_FALSE);
2379
2380 ztest_lr_free(lr, sizeof (*lr), NULL);
2381
2382 return (error);
2383}
2384
2385static int
2386ztest_setattr(ztest_ds_t *zd, uint64_t object)
2387{
2388 lr_setattr_t *lr;
2389 int error;
2390
2391 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2392
2393 lr->lr_foid = object;
2394 lr->lr_size = 0;
2395 lr->lr_mode = 0;
2396
2397 error = ztest_replay_setattr(zd, lr, B_FALSE);
2398
2399 ztest_lr_free(lr, sizeof (*lr), NULL);
2400
2401 return (error);
2402}
2403
2404static void
2405ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2406{
2407 objset_t *os = zd->zd_os;
2408 dmu_tx_t *tx;
2409 uint64_t txg;
cae5b340 2410 ztest_zrl_t *rl;
428870ff
BB
2411
2412 txg_wait_synced(dmu_objset_pool(os), 0);
2413
2414 ztest_object_lock(zd, object, RL_READER);
2415 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2416
2417 tx = dmu_tx_create(os);
2418
2419 dmu_tx_hold_write(tx, object, offset, size);
2420
2421 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2422
2423 if (txg != 0) {
2424 dmu_prealloc(os, object, offset, size, tx);
2425 dmu_tx_commit(tx);
2426 txg_wait_synced(dmu_objset_pool(os), txg);
2427 } else {
2428 (void) dmu_free_long_range(os, object, offset, size);
2429 }
2430
cae5b340 2431 ztest_range_unlock(zd, rl);
428870ff
BB
2432 ztest_object_unlock(zd, object);
2433}
2434
2435static void
2436ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2437{
a08ee875 2438 int err;
428870ff
BB
2439 ztest_block_tag_t wbt;
2440 dmu_object_info_t doi;
2441 enum ztest_io_type io_type;
2442 uint64_t blocksize;
2443 void *data;
2444
2445 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2446 blocksize = doi.doi_data_block_size;
2447 data = umem_alloc(blocksize, UMEM_NOFAIL);
2448
2449 /*
2450 * Pick an i/o type at random, biased toward writing block tags.
2451 */
2452 io_type = ztest_random(ZTEST_IO_TYPES);
2453 if (ztest_random(2) == 0)
2454 io_type = ZTEST_IO_WRITE_TAG;
2455
ea04106b 2456 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2457
428870ff
BB
2458 switch (io_type) {
2459
2460 case ZTEST_IO_WRITE_TAG:
cae5b340
AX
2461 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2462 offset, 0, 0, 0);
428870ff
BB
2463 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2464 break;
2465
2466 case ZTEST_IO_WRITE_PATTERN:
2467 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2468 if (ztest_random(2) == 0) {
2469 /*
2470 * Induce fletcher2 collisions to ensure that
2471 * zio_ddt_collision() detects and resolves them
2472 * when using fletcher2-verify for deduplication.
2473 */
2474 ((uint64_t *)data)[0] ^= 1ULL << 63;
2475 ((uint64_t *)data)[4] ^= 1ULL << 63;
2476 }
2477 (void) ztest_write(zd, object, offset, blocksize, data);
2478 break;
2479
2480 case ZTEST_IO_WRITE_ZEROES:
2481 bzero(data, blocksize);
2482 (void) ztest_write(zd, object, offset, blocksize, data);
2483 break;
2484
2485 case ZTEST_IO_TRUNCATE:
2486 (void) ztest_truncate(zd, object, offset, blocksize);
2487 break;
2488
2489 case ZTEST_IO_SETATTR:
2490 (void) ztest_setattr(zd, object);
2491 break;
e75c13c3
BB
2492 default:
2493 break;
a08ee875
LG
2494
2495 case ZTEST_IO_REWRITE:
ea04106b 2496 (void) rw_rdlock(&ztest_name_lock);
a08ee875
LG
2497 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2498 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2499 B_FALSE);
2500 VERIFY(err == 0 || err == ENOSPC);
2501 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2502 ZFS_PROP_COMPRESSION,
2503 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2504 B_FALSE);
2505 VERIFY(err == 0 || err == ENOSPC);
ea04106b 2506 (void) rw_unlock(&ztest_name_lock);
a08ee875
LG
2507
2508 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2509 DMU_READ_NO_PREFETCH));
2510
2511 (void) ztest_write(zd, object, offset, blocksize, data);
2512 break;
428870ff
BB
2513 }
2514
ea04106b 2515 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0 2516
428870ff
BB
2517 umem_free(data, blocksize);
2518}
2519
2520/*
2521 * Initialize an object description template.
2522 */
2523static void
2524ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
cae5b340
AX
2525 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2526 uint64_t gen)
428870ff
BB
2527{
2528 od->od_dir = ZTEST_DIROBJ;
2529 od->od_object = 0;
2530
2531 od->od_crtype = type;
2532 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
cae5b340 2533 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
428870ff
BB
2534 od->od_crgen = gen;
2535
2536 od->od_type = DMU_OT_NONE;
2537 od->od_blocksize = 0;
2538 od->od_gen = 0;
2539
2540 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
b8864a23 2541 tag, (longlong_t)id, (u_longlong_t)index);
428870ff
BB
2542}
2543
2544/*
2545 * Lookup or create the objects for a test using the od template.
2546 * If the objects do not all exist, or if 'remove' is specified,
2547 * remove any existing objects and create new ones. Otherwise,
2548 * use the existing objects.
2549 */
2550static int
2551ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2552{
2553 int count = size / sizeof (*od);
2554 int rv = 0;
2555
1e33ac1e 2556 mutex_enter(&zd->zd_dirobj_lock);
428870ff
BB
2557 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2558 (ztest_remove(zd, od, count) != 0 ||
2559 ztest_create(zd, od, count) != 0))
2560 rv = -1;
2561 zd->zd_od = od;
1e33ac1e 2562 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2563
2564 return (rv);
2565}
2566
2567/* ARGSUSED */
2568void
2569ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2570{
2571 zilog_t *zilog = zd->zd_zilog;
2572
ea04106b 2573 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2574
572e2857 2575 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
428870ff
BB
2576
2577 /*
2578 * Remember the committed values in zd, which is in parent/child
2579 * shared memory. If we die, the next iteration of ztest_run()
2580 * will verify that the log really does contain this record.
2581 */
2582 mutex_enter(&zilog->zl_lock);
c242c188
CS
2583 ASSERT(zd->zd_shared != NULL);
2584 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2585 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
428870ff 2586 mutex_exit(&zilog->zl_lock);
3e31d2b0 2587
ea04106b 2588 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0
ES
2589}
2590
2591/*
2592 * This function is designed to simulate the operations that occur during a
2593 * mount/unmount operation. We hold the dataset across these operations in an
2594 * attempt to expose any implicit assumptions about ZIL management.
2595 */
2596/* ARGSUSED */
2597void
2598ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2599{
2600 objset_t *os = zd->zd_os;
2601
a08ee875
LG
2602 /*
2603 * We grab the zd_dirobj_lock to ensure that no other thread is
2604 * updating the zil (i.e. adding in-memory log records) and the
2605 * zd_zilog_lock to block any I/O.
2606 */
29809a6c 2607 mutex_enter(&zd->zd_dirobj_lock);
ea04106b 2608 (void) rw_wrlock(&zd->zd_zilog_lock);
3e31d2b0 2609
cae5b340 2610 /* zfsvfs_teardown() */
3e31d2b0
ES
2611 zil_close(zd->zd_zilog);
2612
2613 /* zfsvfs_setup() */
2614 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2615 zil_replay(os, zd, ztest_replay_vector);
2616
ea04106b 2617 (void) rw_unlock(&zd->zd_zilog_lock);
29809a6c 2618 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2619}
2620
2621/*
2622 * Verify that we can't destroy an active pool, create an existing pool,
2623 * or create a pool with a bad vdev spec.
2624 */
2625/* ARGSUSED */
2626void
2627ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2628{
c242c188 2629 ztest_shared_opts_t *zo = &ztest_opts;
428870ff
BB
2630 spa_t *spa;
2631 nvlist_t *nvroot;
2632
cae5b340
AX
2633 if (zo->zo_mmp_test)
2634 return;
2635
428870ff
BB
2636 /*
2637 * Attempt to create using a bad file.
2638 */
ea0b2538 2639 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
428870ff 2640 VERIFY3U(ENOENT, ==,
a08ee875 2641 spa_create("ztest_bad_file", nvroot, NULL, NULL));
428870ff
BB
2642 nvlist_free(nvroot);
2643
2644 /*
2645 * Attempt to create using a bad mirror.
2646 */
ea0b2538 2647 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
428870ff 2648 VERIFY3U(ENOENT, ==,
a08ee875 2649 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
428870ff
BB
2650 nvlist_free(nvroot);
2651
2652 /*
2653 * Attempt to create an existing pool. It shouldn't matter
2654 * what's in the nvroot; we should fail with EEXIST.
2655 */
ea04106b 2656 (void) rw_rdlock(&ztest_name_lock);
ea0b2538 2657 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
a08ee875 2658 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
428870ff 2659 nvlist_free(nvroot);
c242c188
CS
2660 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2661 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
428870ff
BB
2662 spa_close(spa, FTAG);
2663
ea04106b 2664 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
2665}
2666
cae5b340
AX
2667/*
2668 * Start and then stop the MMP threads to ensure the startup and shutdown code
2669 * works properly. Actual protection and property-related code tested via ZTS.
2670 */
2671/* ARGSUSED */
2672void
2673ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2674{
2675 ztest_shared_opts_t *zo = &ztest_opts;
2676 spa_t *spa = ztest_spa;
2677
2678 if (zo->zo_mmp_test)
2679 return;
2680
2681 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2682 mutex_enter(&spa->spa_props_lock);
2683
2684 if (!spa_multihost(spa)) {
2685 spa->spa_multihost = B_TRUE;
2686 mmp_thread_start(spa);
2687 }
2688
2689 mutex_exit(&spa->spa_props_lock);
2690 spa_config_exit(spa, SCL_CONFIG, FTAG);
2691
2692 txg_wait_synced(spa_get_dsl(spa), 0);
2693 mmp_signal_all_threads();
2694 txg_wait_synced(spa_get_dsl(spa), 0);
2695
2696 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2697 mutex_enter(&spa->spa_props_lock);
2698
2699 if (spa_multihost(spa)) {
2700 mmp_thread_stop(spa);
2701 spa->spa_multihost = B_FALSE;
2702 }
2703
2704 mutex_exit(&spa->spa_props_lock);
2705 spa_config_exit(spa, SCL_CONFIG, FTAG);
2706}
2707
ea0b2538
GW
2708/* ARGSUSED */
2709void
2710ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2711{
2712 spa_t *spa;
2713 uint64_t initial_version = SPA_VERSION_INITIAL;
2714 uint64_t version, newversion;
2715 nvlist_t *nvroot, *props;
2716 char *name;
2717
cae5b340
AX
2718 if (ztest_opts.zo_mmp_test)
2719 return;
2720
ea0b2538
GW
2721 mutex_enter(&ztest_vdev_lock);
2722 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2723
2724 /*
2725 * Clean up from previous runs.
2726 */
2727 (void) spa_destroy(name);
2728
2729 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2730 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2731
2732 /*
2733 * If we're configuring a RAIDZ device then make sure that the
2734 * the initial version is capable of supporting that feature.
2735 */
2736 switch (ztest_opts.zo_raidz_parity) {
2737 case 0:
2738 case 1:
2739 initial_version = SPA_VERSION_INITIAL;
2740 break;
2741 case 2:
2742 initial_version = SPA_VERSION_RAIDZ2;
2743 break;
2744 case 3:
2745 initial_version = SPA_VERSION_RAIDZ3;
2746 break;
2747 }
2748
2749 /*
2750 * Create a pool with a spa version that can be upgraded. Pick
2751 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2752 */
2753 do {
2754 version = ztest_random_spa_version(initial_version);
2755 } while (version > SPA_VERSION_BEFORE_FEATURES);
2756
2757 props = fnvlist_alloc();
2758 fnvlist_add_uint64(props,
2759 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
a08ee875 2760 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
ea0b2538
GW
2761 fnvlist_free(nvroot);
2762 fnvlist_free(props);
2763
2764 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2765 VERIFY3U(spa_version(spa), ==, version);
2766 newversion = ztest_random_spa_version(version + 1);
2767
2768 if (ztest_opts.zo_verbose >= 4) {
2769 (void) printf("upgrading spa version from %llu to %llu\n",
2770 (u_longlong_t)version, (u_longlong_t)newversion);
2771 }
2772
2773 spa_upgrade(spa, newversion);
2774 VERIFY3U(spa_version(spa), >, version);
2775 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2776 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2777 spa_close(spa, FTAG);
2778
2779 strfree(name);
2780 mutex_exit(&ztest_vdev_lock);
2781}
2782
428870ff
BB
2783static vdev_t *
2784vdev_lookup_by_path(vdev_t *vd, const char *path)
2785{
2786 vdev_t *mvd;
d6320ddb 2787 int c;
428870ff
BB
2788
2789 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2790 return (vd);
2791
d6320ddb 2792 for (c = 0; c < vd->vdev_children; c++)
428870ff
BB
2793 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2794 NULL)
2795 return (mvd);
2796
2797 return (NULL);
2798}
2799
2800/*
2801 * Find the first available hole which can be used as a top-level.
2802 */
2803int
2804find_vdev_hole(spa_t *spa)
2805{
2806 vdev_t *rvd = spa->spa_root_vdev;
2807 int c;
2808
2809 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2810
2811 for (c = 0; c < rvd->vdev_children; c++) {
2812 vdev_t *cvd = rvd->vdev_child[c];
2813
2814 if (cvd->vdev_ishole)
2815 break;
2816 }
2817 return (c);
2818}
2819
2820/*
2821 * Verify that vdev_add() works as expected.
2822 */
2823/* ARGSUSED */
2824void
2825ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2826{
2827 ztest_shared_t *zs = ztest_shared;
c242c188 2828 spa_t *spa = ztest_spa;
428870ff
BB
2829 uint64_t leaves;
2830 uint64_t guid;
2831 nvlist_t *nvroot;
2832 int error;
2833
cae5b340
AX
2834 if (ztest_opts.zo_mmp_test)
2835 return;
2836
c242c188 2837 mutex_enter(&ztest_vdev_lock);
a08ee875 2838 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
428870ff
BB
2839
2840 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2841
2842 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2843
2844 /*
2845 * If we have slogs then remove them 1/4 of the time.
2846 */
2847 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2848 /*
2849 * Grab the guid from the head of the log class rotor.
2850 */
2851 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2852
2853 spa_config_exit(spa, SCL_VDEV, FTAG);
2854
2855 /*
2856 * We have to grab the zs_name_lock as writer to
2857 * prevent a race between removing a slog (dmu_objset_find)
2858 * and destroying a dataset. Removing the slog will
2859 * grab a reference on the dataset which may cause
a08ee875 2860 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
2861 * leaving the dataset in an inconsistent state.
2862 */
ea04106b 2863 rw_wrlock(&ztest_name_lock);
428870ff 2864 error = spa_vdev_remove(spa, guid, B_FALSE);
ea04106b 2865 rw_unlock(&ztest_name_lock);
428870ff
BB
2866
2867 if (error && error != EEXIST)
2868 fatal(0, "spa_vdev_remove() = %d", error);
2869 } else {
2870 spa_config_exit(spa, SCL_VDEV, FTAG);
2871
2872 /*
2873 * Make 1/4 of the devices be log devices.
2874 */
ea0b2538 2875 nvroot = make_vdev_root(NULL, NULL, NULL,
c242c188
CS
2876 ztest_opts.zo_vdev_size, 0,
2877 ztest_random(4) == 0, ztest_opts.zo_raidz,
2878 zs->zs_mirrors, 1);
428870ff
BB
2879
2880 error = spa_vdev_add(spa, nvroot);
2881 nvlist_free(nvroot);
2882
2883 if (error == ENOSPC)
2884 ztest_record_enospc("spa_vdev_add");
2885 else if (error != 0)
2886 fatal(0, "spa_vdev_add() = %d", error);
2887 }
2888
c242c188 2889 mutex_exit(&ztest_vdev_lock);
428870ff
BB
2890}
2891
2892/*
2893 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2894 */
2895/* ARGSUSED */
2896void
2897ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2898{
2899 ztest_shared_t *zs = ztest_shared;
c242c188 2900 spa_t *spa = ztest_spa;
428870ff
BB
2901 vdev_t *rvd = spa->spa_root_vdev;
2902 spa_aux_vdev_t *sav;
2903 char *aux;
40b84e7a 2904 char *path;
428870ff
BB
2905 uint64_t guid = 0;
2906 int error;
2907
cae5b340
AX
2908 if (ztest_opts.zo_mmp_test)
2909 return;
2910
40b84e7a
BB
2911 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2912
428870ff
BB
2913 if (ztest_random(2) == 0) {
2914 sav = &spa->spa_spares;
2915 aux = ZPOOL_CONFIG_SPARES;
2916 } else {
2917 sav = &spa->spa_l2cache;
2918 aux = ZPOOL_CONFIG_L2CACHE;
2919 }
2920
c242c188 2921 mutex_enter(&ztest_vdev_lock);
428870ff
BB
2922
2923 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2924
2925 if (sav->sav_count != 0 && ztest_random(4) == 0) {
b128c09f
BB
2926 /*
2927 * Pick a random device to remove.
2928 */
2929 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2930 } else {
2931 /*
2932 * Find an unused device we can add.
2933 */
428870ff 2934 zs->zs_vdev_aux = 0;
b128c09f 2935 for (;;) {
b128c09f 2936 int c;
c06d4368 2937 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
c242c188
CS
2938 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2939 zs->zs_vdev_aux);
b128c09f
BB
2940 for (c = 0; c < sav->sav_count; c++)
2941 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2942 path) == 0)
2943 break;
2944 if (c == sav->sav_count &&
2945 vdev_lookup_by_path(rvd, path) == NULL)
2946 break;
428870ff 2947 zs->zs_vdev_aux++;
34dc7c2f
BB
2948 }
2949 }
2950
b128c09f 2951 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f 2952
b128c09f
BB
2953 if (guid == 0) {
2954 /*
2955 * Add a new device.
2956 */
ea0b2538 2957 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
c242c188 2958 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
b128c09f
BB
2959 error = spa_vdev_add(spa, nvroot);
2960 if (error != 0)
2961 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2962 nvlist_free(nvroot);
2963 } else {
2964 /*
2965 * Remove an existing device. Sometimes, dirty its
2966 * vdev state first to make sure we handle removal
2967 * of devices that have pending state changes.
2968 */
2969 if (ztest_random(2) == 0)
9babb374 2970 (void) vdev_online(spa, guid, 0, NULL);
b128c09f
BB
2971
2972 error = spa_vdev_remove(spa, guid, B_FALSE);
2973 if (error != 0 && error != EBUSY)
2974 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2975 }
2976
c242c188 2977 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
2978
2979 umem_free(path, MAXPATHLEN);
428870ff
BB
2980}
2981
2982/*
2983 * split a pool if it has mirror tlvdevs
2984 */
2985/* ARGSUSED */
2986void
2987ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2988{
2989 ztest_shared_t *zs = ztest_shared;
c242c188 2990 spa_t *spa = ztest_spa;
428870ff
BB
2991 vdev_t *rvd = spa->spa_root_vdev;
2992 nvlist_t *tree, **child, *config, *split, **schild;
2993 uint_t c, children, schildren = 0, lastlogid = 0;
2994 int error = 0;
2995
cae5b340
AX
2996 if (ztest_opts.zo_mmp_test)
2997 return;
2998
c242c188 2999 mutex_enter(&ztest_vdev_lock);
428870ff
BB
3000
3001 /* ensure we have a useable config; mirrors of raidz aren't supported */
c242c188
CS
3002 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3003 mutex_exit(&ztest_vdev_lock);
428870ff
BB
3004 return;
3005 }
3006
3007 /* clean up the old pool, if any */
3008 (void) spa_destroy("splitp");
3009
3010 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3011
3012 /* generate a config from the existing config */
3013 mutex_enter(&spa->spa_props_lock);
3014 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3015 &tree) == 0);
3016 mutex_exit(&spa->spa_props_lock);
3017
3018 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3019 &children) == 0);
3020
3021 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3022 for (c = 0; c < children; c++) {
3023 vdev_t *tvd = rvd->vdev_child[c];
3024 nvlist_t **mchild;
3025 uint_t mchildren;
3026
3027 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3028 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3029 0) == 0);
3030 VERIFY(nvlist_add_string(schild[schildren],
3031 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3032 VERIFY(nvlist_add_uint64(schild[schildren],
3033 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3034 if (lastlogid == 0)
3035 lastlogid = schildren;
3036 ++schildren;
3037 continue;
3038 }
3039 lastlogid = 0;
3040 VERIFY(nvlist_lookup_nvlist_array(child[c],
3041 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3042 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3043 }
3044
3045 /* OK, create a config that can be used to split */
3046 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3047 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3048 VDEV_TYPE_ROOT) == 0);
3049 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3050 lastlogid != 0 ? lastlogid : schildren) == 0);
3051
3052 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3053 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3054
3055 for (c = 0; c < schildren; c++)
3056 nvlist_free(schild[c]);
3057 free(schild);
3058 nvlist_free(split);
3059
3060 spa_config_exit(spa, SCL_VDEV, FTAG);
3061
ea04106b 3062 (void) rw_wrlock(&ztest_name_lock);
428870ff 3063 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
ea04106b 3064 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
3065
3066 nvlist_free(config);
3067
3068 if (error == 0) {
3069 (void) printf("successful split - results:\n");
3070 mutex_enter(&spa_namespace_lock);
3071 show_pool_stats(spa);
3072 show_pool_stats(spa_lookup("splitp"));
3073 mutex_exit(&spa_namespace_lock);
3074 ++zs->zs_splits;
3075 --zs->zs_mirrors;
3076 }
c242c188 3077 mutex_exit(&ztest_vdev_lock);
428870ff 3078
34dc7c2f
BB
3079}
3080
3081/*
3082 * Verify that we can attach and detach devices.
3083 */
428870ff 3084/* ARGSUSED */
34dc7c2f 3085void
428870ff 3086ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3087{
428870ff 3088 ztest_shared_t *zs = ztest_shared;
c242c188 3089 spa_t *spa = ztest_spa;
b128c09f 3090 spa_aux_vdev_t *sav = &spa->spa_spares;
34dc7c2f
BB
3091 vdev_t *rvd = spa->spa_root_vdev;
3092 vdev_t *oldvd, *newvd, *pvd;
b128c09f 3093 nvlist_t *root;
428870ff 3094 uint64_t leaves;
34dc7c2f
BB
3095 uint64_t leaf, top;
3096 uint64_t ashift = ztest_get_ashift();
fb5f0bc8 3097 uint64_t oldguid, pguid;
a08ee875 3098 uint64_t oldsize, newsize;
40b84e7a 3099 char *oldpath, *newpath;
34dc7c2f 3100 int replacing;
b128c09f
BB
3101 int oldvd_has_siblings = B_FALSE;
3102 int newvd_is_spare = B_FALSE;
3103 int oldvd_is_log;
34dc7c2f 3104 int error, expected_error;
34dc7c2f 3105
cae5b340
AX
3106 if (ztest_opts.zo_mmp_test)
3107 return;
3108
40b84e7a
BB
3109 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3110 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3111
c242c188
CS
3112 mutex_enter(&ztest_vdev_lock);
3113 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
34dc7c2f 3114
b128c09f 3115 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34dc7c2f
BB
3116
3117 /*
3118 * Decide whether to do an attach or a replace.
3119 */
3120 replacing = ztest_random(2);
3121
3122 /*
3123 * Pick a random top-level vdev.
3124 */
428870ff 3125 top = ztest_random_vdev_top(spa, B_TRUE);
34dc7c2f
BB
3126
3127 /*
3128 * Pick a random leaf within it.
3129 */
3130 leaf = ztest_random(leaves);
3131
3132 /*
b128c09f 3133 * Locate this vdev.
34dc7c2f 3134 */
b128c09f 3135 oldvd = rvd->vdev_child[top];
428870ff 3136 if (zs->zs_mirrors >= 1) {
fb5f0bc8 3137 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
428870ff 3138 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
c242c188 3139 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
fb5f0bc8 3140 }
c242c188 3141 if (ztest_opts.zo_raidz > 1) {
fb5f0bc8 3142 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
c242c188
CS
3143 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3144 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
fb5f0bc8 3145 }
34dc7c2f
BB
3146
3147 /*
b128c09f
BB
3148 * If we're already doing an attach or replace, oldvd may be a
3149 * mirror vdev -- in which case, pick a random child.
34dc7c2f 3150 */
b128c09f
BB
3151 while (oldvd->vdev_children != 0) {
3152 oldvd_has_siblings = B_TRUE;
fb5f0bc8
BB
3153 ASSERT(oldvd->vdev_children >= 2);
3154 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
b128c09f
BB
3155 }
3156
3157 oldguid = oldvd->vdev_guid;
9babb374 3158 oldsize = vdev_get_min_asize(oldvd);
b128c09f
BB
3159 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3160 (void) strcpy(oldpath, oldvd->vdev_path);
3161 pvd = oldvd->vdev_parent;
fb5f0bc8 3162 pguid = pvd->vdev_guid;
34dc7c2f
BB
3163
3164 /*
b128c09f 3165 * If oldvd has siblings, then half of the time, detach it.
34dc7c2f 3166 */
b128c09f
BB
3167 if (oldvd_has_siblings && ztest_random(2) == 0) {
3168 spa_config_exit(spa, SCL_VDEV, FTAG);
fb5f0bc8
BB
3169 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3170 if (error != 0 && error != ENODEV && error != EBUSY &&
3171 error != ENOTSUP)
3172 fatal(0, "detach (%s) returned %d", oldpath, error);
40b84e7a 3173 goto out;
b128c09f 3174 }
34dc7c2f
BB
3175
3176 /*
b128c09f
BB
3177 * For the new vdev, choose with equal probability between the two
3178 * standard paths (ending in either 'a' or 'b') or a random hot spare.
34dc7c2f 3179 */
b128c09f
BB
3180 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3181 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3182 newvd_is_spare = B_TRUE;
3183 (void) strcpy(newpath, newvd->vdev_path);
3184 } else {
6aec1cd5 3185 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
c242c188
CS
3186 ztest_opts.zo_dir, ztest_opts.zo_pool,
3187 top * leaves + leaf);
b128c09f
BB
3188 if (ztest_random(2) == 0)
3189 newpath[strlen(newpath) - 1] = 'b';
3190 newvd = vdev_lookup_by_path(rvd, newpath);
3191 }
3192
3193 if (newvd) {
9babb374 3194 newsize = vdev_get_min_asize(newvd);
b128c09f
BB
3195 } else {
3196 /*
3197 * Make newsize a little bigger or smaller than oldsize.
3198 * If it's smaller, the attach should fail.
3199 * If it's larger, and we're doing a replace,
3200 * we should get dynamic LUN growth when we're done.
3201 */
3202 newsize = 10 * oldsize / (9 + ztest_random(3));
3203 }
34dc7c2f
BB
3204
3205 /*
3206 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3207 * unless it's a replace; in that case any non-replacing parent is OK.
3208 *
3209 * If newvd is already part of the pool, it should fail with EBUSY.
3210 *
3211 * If newvd is too small, it should fail with EOVERFLOW.
3212 */
b128c09f
BB
3213 if (pvd->vdev_ops != &vdev_mirror_ops &&
3214 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3215 pvd->vdev_ops == &vdev_replacing_ops ||
3216 pvd->vdev_ops == &vdev_spare_ops))
34dc7c2f 3217 expected_error = ENOTSUP;
b128c09f
BB
3218 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3219 expected_error = ENOTSUP;
3220 else if (newvd == oldvd)
3221 expected_error = replacing ? 0 : EBUSY;
3222 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3223 expected_error = EBUSY;
34dc7c2f
BB
3224 else if (newsize < oldsize)
3225 expected_error = EOVERFLOW;
3226 else if (ashift > oldvd->vdev_top->vdev_ashift)
3227 expected_error = EDOM;
3228 else
3229 expected_error = 0;
3230
b128c09f 3231 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f
BB
3232
3233 /*
3234 * Build the nvlist describing newpath.
3235 */
ea0b2538 3236 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
b128c09f 3237 ashift, 0, 0, 0, 1);
34dc7c2f 3238
b128c09f 3239 error = spa_vdev_attach(spa, oldguid, root, replacing);
34dc7c2f 3240
34dc7c2f
BB
3241 nvlist_free(root);
3242
3243 /*
3244 * If our parent was the replacing vdev, but the replace completed,
3245 * then instead of failing with ENOTSUP we may either succeed,
3246 * fail with ENODEV, or fail with EOVERFLOW.
3247 */
3248 if (expected_error == ENOTSUP &&
3249 (error == 0 || error == ENODEV || error == EOVERFLOW))
3250 expected_error = error;
3251
3252 /*
3253 * If someone grew the LUN, the replacement may be too small.
3254 */
b128c09f 3255 if (error == EOVERFLOW || error == EBUSY)
34dc7c2f
BB
3256 expected_error = error;
3257
b128c09f
BB
3258 /* XXX workaround 6690467 */
3259 if (error != expected_error && expected_error != EBUSY) {
3260 fatal(0, "attach (%s %llu, %s %llu, %d) "
3261 "returned %d, expected %d",
a08ee875
LG
3262 oldpath, oldsize, newpath,
3263 newsize, replacing, error, expected_error);
34dc7c2f 3264 }
40b84e7a 3265out:
c242c188 3266 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
3267
3268 umem_free(oldpath, MAXPATHLEN);
3269 umem_free(newpath, MAXPATHLEN);
34dc7c2f
BB
3270}
3271
9babb374
BB
3272/*
3273 * Callback function which expands the physical size of the vdev.
3274 */
3275vdev_t *
3276grow_vdev(vdev_t *vd, void *arg)
3277{
1fde1e37 3278 ASSERTV(spa_t *spa = vd->vdev_spa);
9babb374
BB
3279 size_t *newsize = arg;
3280 size_t fsize;
3281 int fd;
3282
3283 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3284 ASSERT(vd->vdev_ops->vdev_op_leaf);
3285
3286 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3287 return (vd);
3288
3289 fsize = lseek(fd, 0, SEEK_END);
0e5b68e0 3290 VERIFY(ftruncate(fd, *newsize) == 0);
9babb374 3291
c242c188 3292 if (ztest_opts.zo_verbose >= 6) {
9babb374
BB
3293 (void) printf("%s grew from %lu to %lu bytes\n",
3294 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3295 }
3296 (void) close(fd);
3297 return (NULL);
3298}
3299
3300/*
3301 * Callback function which expands a given vdev by calling vdev_online().
3302 */
3303/* ARGSUSED */
3304vdev_t *
3305online_vdev(vdev_t *vd, void *arg)
3306{
3307 spa_t *spa = vd->vdev_spa;
3308 vdev_t *tvd = vd->vdev_top;
9babb374 3309 uint64_t guid = vd->vdev_guid;
428870ff
BB
3310 uint64_t generation = spa->spa_config_generation + 1;
3311 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3312 int error;
9babb374
BB
3313
3314 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3315 ASSERT(vd->vdev_ops->vdev_op_leaf);
3316
3317 /* Calling vdev_online will initialize the new metaslabs */
3318 spa_config_exit(spa, SCL_STATE, spa);
428870ff 3319 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
9babb374
BB
3320 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3321
428870ff
BB
3322 /*
3323 * If vdev_online returned an error or the underlying vdev_open
3324 * failed then we abort the expand. The only way to know that
3325 * vdev_open fails is by checking the returned newstate.
3326 */
3327 if (error || newstate != VDEV_STATE_HEALTHY) {
c242c188 3328 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3329 (void) printf("Unable to expand vdev, state %llu, "
3330 "error %d\n", (u_longlong_t)newstate, error);
3331 }
3332 return (vd);
3333 }
3334 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3335
9babb374
BB
3336 /*
3337 * Since we dropped the lock we need to ensure that we're
3338 * still talking to the original vdev. It's possible this
3339 * vdev may have been detached/replaced while we were
3340 * trying to online it.
3341 */
428870ff 3342 if (generation != spa->spa_config_generation) {
c242c188 3343 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3344 (void) printf("vdev configuration has changed, "
3345 "guid %llu, state %llu, expected gen %llu, "
3346 "got gen %llu\n",
3347 (u_longlong_t)guid,
3348 (u_longlong_t)tvd->vdev_state,
3349 (u_longlong_t)generation,
3350 (u_longlong_t)spa->spa_config_generation);
9babb374
BB
3351 }
3352 return (vd);
3353 }
3354 return (NULL);
3355}
3356
3357/*
3358 * Traverse the vdev tree calling the supplied function.
3359 * We continue to walk the tree until we either have walked all
3360 * children or we receive a non-NULL return from the callback.
3361 * If a NULL callback is passed, then we just return back the first
3362 * leaf vdev we encounter.
3363 */
3364vdev_t *
3365vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3366{
d6320ddb
BB
3367 uint_t c;
3368
9babb374
BB
3369 if (vd->vdev_ops->vdev_op_leaf) {
3370 if (func == NULL)
3371 return (vd);
3372 else
3373 return (func(vd, arg));
3374 }
3375
d6320ddb 3376 for (c = 0; c < vd->vdev_children; c++) {
9babb374
BB
3377 vdev_t *cvd = vd->vdev_child[c];
3378 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3379 return (cvd);
3380 }
3381 return (NULL);
3382}
3383
34dc7c2f
BB
3384/*
3385 * Verify that dynamic LUN growth works as expected.
3386 */
428870ff 3387/* ARGSUSED */
34dc7c2f 3388void
428870ff 3389ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3390{
c242c188 3391 spa_t *spa = ztest_spa;
428870ff
BB
3392 vdev_t *vd, *tvd;
3393 metaslab_class_t *mc;
3394 metaslab_group_t *mg;
9babb374 3395 size_t psize, newsize;
428870ff
BB
3396 uint64_t top;
3397 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
34dc7c2f 3398
c242c188 3399 mutex_enter(&ztest_vdev_lock);
9babb374
BB
3400 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3401
428870ff 3402 top = ztest_random_vdev_top(spa, B_TRUE);
9babb374 3403
428870ff
BB
3404 tvd = spa->spa_root_vdev->vdev_child[top];
3405 mg = tvd->vdev_mg;
3406 mc = mg->mg_class;
3407 old_ms_count = tvd->vdev_ms_count;
3408 old_class_space = metaslab_class_get_space(mc);
34dc7c2f
BB
3409
3410 /*
9babb374
BB
3411 * Determine the size of the first leaf vdev associated with
3412 * our top-level device.
34dc7c2f 3413 */
9babb374
BB
3414 vd = vdev_walk_tree(tvd, NULL, NULL);
3415 ASSERT3P(vd, !=, NULL);
3416 ASSERT(vd->vdev_ops->vdev_op_leaf);
34dc7c2f 3417
9babb374 3418 psize = vd->vdev_psize;
34dc7c2f 3419
9babb374 3420 /*
428870ff
BB
3421 * We only try to expand the vdev if it's healthy, less than 4x its
3422 * original size, and it has a valid psize.
9babb374 3423 */
428870ff 3424 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
c242c188 3425 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
9babb374 3426 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3427 mutex_exit(&ztest_vdev_lock);
9babb374
BB
3428 return;
3429 }
3430 ASSERT(psize > 0);
3431 newsize = psize + psize / 8;
3432 ASSERT3U(newsize, >, psize);
34dc7c2f 3433
c242c188 3434 if (ztest_opts.zo_verbose >= 6) {
428870ff 3435 (void) printf("Expanding LUN %s from %lu to %lu\n",
9babb374
BB
3436 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3437 }
3438
9babb374
BB
3439 /*
3440 * Growing the vdev is a two step process:
3441 * 1). expand the physical size (i.e. relabel)
3442 * 2). online the vdev to create the new metaslabs
3443 */
3444 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3445 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3446 tvd->vdev_state != VDEV_STATE_HEALTHY) {
c242c188 3447 if (ztest_opts.zo_verbose >= 5) {
9babb374 3448 (void) printf("Could not expand LUN because "
428870ff 3449 "the vdev configuration changed.\n");
34dc7c2f 3450 }
428870ff 3451 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3452 mutex_exit(&ztest_vdev_lock);
9babb374 3453 return;
34dc7c2f
BB
3454 }
3455
428870ff 3456 spa_config_exit(spa, SCL_STATE, spa);
9babb374
BB
3457
3458 /*
3459 * Expanding the LUN will update the config asynchronously,
3460 * thus we must wait for the async thread to complete any
3461 * pending tasks before proceeding.
3462 */
428870ff
BB
3463 for (;;) {
3464 boolean_t done;
3465 mutex_enter(&spa->spa_async_lock);
3466 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3467 mutex_exit(&spa->spa_async_lock);
3468 if (done)
3469 break;
3470 txg_wait_synced(spa_get_dsl(spa), 0);
3471 (void) poll(NULL, 0, 100);
3472 }
9babb374
BB
3473
3474 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
428870ff
BB
3475
3476 tvd = spa->spa_root_vdev->vdev_child[top];
3477 new_ms_count = tvd->vdev_ms_count;
3478 new_class_space = metaslab_class_get_space(mc);
3479
3480 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
c242c188 3481 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3482 (void) printf("Could not verify LUN expansion due to "
3483 "intervening vdev offline or remove.\n");
3484 }
3485 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3486 mutex_exit(&ztest_vdev_lock);
428870ff
BB
3487 return;
3488 }
3489
3490 /*
3491 * Make sure we were able to grow the vdev.
3492 */
3493 if (new_ms_count <= old_ms_count)
3494 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3495 old_ms_count, new_ms_count);
9babb374
BB
3496
3497 /*
3498 * Make sure we were able to grow the pool.
3499 */
428870ff
BB
3500 if (new_class_space <= old_class_space)
3501 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3502 old_class_space, new_class_space);
3503
c242c188 3504 if (ztest_opts.zo_verbose >= 5) {
9babb374
BB
3505 char oldnumbuf[6], newnumbuf[6];
3506
428870ff
BB
3507 nicenum(old_class_space, oldnumbuf);
3508 nicenum(new_class_space, newnumbuf);
9babb374
BB
3509 (void) printf("%s grew from %s to %s\n",
3510 spa->spa_name, oldnumbuf, newnumbuf);
3511 }
428870ff 3512
9babb374 3513 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3514 mutex_exit(&ztest_vdev_lock);
34dc7c2f
BB
3515}
3516
428870ff
BB
3517/*
3518 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3519 */
34dc7c2f
BB
3520/* ARGSUSED */
3521static void
428870ff 3522ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
34dc7c2f
BB
3523{
3524 /*
428870ff 3525 * Create the objects common to all ztest datasets.
34dc7c2f 3526 */
428870ff 3527 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
34dc7c2f 3528 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
428870ff 3529}
34dc7c2f 3530
428870ff
BB
3531static int
3532ztest_dataset_create(char *dsname)
3533{
3534 uint64_t zilset = ztest_random(100);
3535 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3536 ztest_objset_create_cb, NULL);
3537
3538 if (err || zilset < 80)
3539 return (err);
3540
c242c188 3541 if (ztest_opts.zo_verbose >= 5)
b815ff9a 3542 (void) printf("Setting dataset %s to sync always\n", dsname);
428870ff
BB
3543 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3544 ZFS_SYNC_ALWAYS, B_FALSE));
34dc7c2f
BB
3545}
3546
428870ff 3547/* ARGSUSED */
34dc7c2f 3548static int
428870ff 3549ztest_objset_destroy_cb(const char *name, void *arg)
34dc7c2f 3550{
34dc7c2f 3551 objset_t *os;
428870ff 3552 dmu_object_info_t doi;
34dc7c2f
BB
3553 int error;
3554
3555 /*
3556 * Verify that the dataset contains a directory object.
3557 */
a08ee875 3558 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
428870ff 3559 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
34dc7c2f
BB
3560 if (error != ENOENT) {
3561 /* We could have crashed in the middle of destroying it */
c06d4368 3562 ASSERT0(error);
428870ff
BB
3563 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3564 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
34dc7c2f 3565 }
a08ee875 3566 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
3567
3568 /*
3569 * Destroy the dataset.
3570 */
a08ee875 3571 if (strchr(name, '@') != NULL) {
cae5b340 3572 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
a08ee875 3573 } else {
cae5b340
AX
3574 error = dsl_destroy_head(name);
3575 /* There could be a hold on this dataset */
3576 if (error != EBUSY)
3577 ASSERT0(error);
a08ee875 3578 }
34dc7c2f
BB
3579 return (0);
3580}
3581
428870ff
BB
3582static boolean_t
3583ztest_snapshot_create(char *osname, uint64_t id)
34dc7c2f 3584{
cae5b340 3585 char snapname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3586 int error;
3587
a08ee875 3588 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
428870ff 3589
a08ee875 3590 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
3591 if (error == ENOSPC) {
3592 ztest_record_enospc(FTAG);
3593 return (B_FALSE);
3594 }
a08ee875
LG
3595 if (error != 0 && error != EEXIST) {
3596 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3597 snapname, error);
3598 }
428870ff
BB
3599 return (B_TRUE);
3600}
3601
3602static boolean_t
3603ztest_snapshot_destroy(char *osname, uint64_t id)
3604{
cae5b340 3605 char snapname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3606 int error;
3607
cae5b340 3608 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
428870ff
BB
3609 (u_longlong_t)id);
3610
a08ee875 3611 error = dsl_destroy_snapshot(snapname, B_FALSE);
428870ff
BB
3612 if (error != 0 && error != ENOENT)
3613 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3614 return (B_TRUE);
34dc7c2f
BB
3615}
3616
428870ff 3617/* ARGSUSED */
34dc7c2f 3618void
428870ff 3619ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3620{
40b84e7a 3621 ztest_ds_t *zdtmp;
428870ff 3622 int iters;
34dc7c2f 3623 int error;
b128c09f 3624 objset_t *os, *os2;
cae5b340 3625 char name[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 3626 zilog_t *zilog;
d6320ddb 3627 int i;
34dc7c2f 3628
40b84e7a 3629 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
40b84e7a 3630
ea04106b 3631 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f 3632
cae5b340 3633 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
c242c188 3634 ztest_opts.zo_pool, (u_longlong_t)id);
34dc7c2f
BB
3635
3636 /*
3637 * If this dataset exists from a previous run, process its replay log
a08ee875 3638 * half of the time. If we don't replay it, then dsl_destroy_head()
428870ff 3639 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
34dc7c2f
BB
3640 */
3641 if (ztest_random(2) == 0 &&
428870ff 3642 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
c242c188 3643 ztest_zd_init(zdtmp, NULL, os);
40b84e7a
BB
3644 zil_replay(os, zdtmp, ztest_replay_vector);
3645 ztest_zd_fini(zdtmp);
428870ff 3646 dmu_objset_disown(os, FTAG);
34dc7c2f
BB
3647 }
3648
3649 /*
3650 * There may be an old instance of the dataset we're about to
3651 * create lying around from a previous run. If so, destroy it
3652 * and all of its snapshots.
3653 */
428870ff 3654 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
34dc7c2f
BB
3655 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3656
3657 /*
3658 * Verify that the destroyed dataset is no longer in the namespace.
3659 */
a08ee875
LG
3660 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3661 FTAG, &os));
34dc7c2f
BB
3662
3663 /*
3664 * Verify that we can create a new dataset.
3665 */
428870ff 3666 error = ztest_dataset_create(name);
34dc7c2f
BB
3667 if (error) {
3668 if (error == ENOSPC) {
428870ff 3669 ztest_record_enospc(FTAG);
40b84e7a 3670 goto out;
34dc7c2f
BB
3671 }
3672 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3673 }
3674
a08ee875 3675 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
428870ff 3676
c242c188 3677 ztest_zd_init(zdtmp, NULL, os);
34dc7c2f
BB
3678
3679 /*
3680 * Open the intent log for it.
3681 */
428870ff 3682 zilog = zil_open(os, ztest_get_data);
34dc7c2f
BB
3683
3684 /*
428870ff
BB
3685 * Put some objects in there, do a little I/O to them,
3686 * and randomly take a couple of snapshots along the way.
34dc7c2f 3687 */
428870ff 3688 iters = ztest_random(5);
d6320ddb 3689 for (i = 0; i < iters; i++) {
40b84e7a 3690 ztest_dmu_object_alloc_free(zdtmp, id);
428870ff
BB
3691 if (ztest_random(iters) == 0)
3692 (void) ztest_snapshot_create(name, i);
34dc7c2f
BB
3693 }
3694
3695 /*
3696 * Verify that we cannot create an existing dataset.
3697 */
428870ff
BB
3698 VERIFY3U(EEXIST, ==,
3699 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
34dc7c2f
BB
3700
3701 /*
428870ff 3702 * Verify that we can hold an objset that is also owned.
b128c09f 3703 */
428870ff
BB
3704 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3705 dmu_objset_rele(os2, FTAG);
34dc7c2f 3706
428870ff
BB
3707 /*
3708 * Verify that we cannot own an objset that is already owned.
3709 */
3710 VERIFY3U(EBUSY, ==,
3711 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
34dc7c2f 3712
428870ff
BB
3713 zil_close(zilog);
3714 dmu_objset_disown(os, FTAG);
40b84e7a
BB
3715 ztest_zd_fini(zdtmp);
3716out:
ea04106b 3717 (void) rw_unlock(&ztest_name_lock);
40b84e7a 3718
40b84e7a 3719 umem_free(zdtmp, sizeof (ztest_ds_t));
34dc7c2f
BB
3720}
3721
3722/*
3723 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3724 */
3725void
428870ff 3726ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3727{
ea04106b 3728 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
3729 (void) ztest_snapshot_destroy(zd->zd_name, id);
3730 (void) ztest_snapshot_create(zd->zd_name, id);
ea04106b 3731 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
3732}
3733
9babb374
BB
3734/*
3735 * Cleanup non-standard snapshots and clones.
3736 */
3737void
428870ff 3738ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
9babb374 3739{
40b84e7a
BB
3740 char *snap1name;
3741 char *clone1name;
3742 char *snap2name;
3743 char *clone2name;
3744 char *snap3name;
9babb374
BB
3745 int error;
3746
cae5b340
AX
3747 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3748 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3749 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3750 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3751 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3752
3753 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3754 "%s@s1_%llu", osname, (u_longlong_t)id);
3755 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3756 "%s/c1_%llu", osname, (u_longlong_t)id);
3757 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3758 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3759 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3760 "%s/c2_%llu", osname, (u_longlong_t)id);
3761 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3762 "%s@s3_%llu", clone1name, (u_longlong_t)id);
9babb374 3763
a08ee875 3764 error = dsl_destroy_head(clone2name);
9babb374 3765 if (error && error != ENOENT)
a08ee875
LG
3766 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3767 error = dsl_destroy_snapshot(snap3name, B_FALSE);
9babb374 3768 if (error && error != ENOENT)
a08ee875
LG
3769 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3770 error = dsl_destroy_snapshot(snap2name, B_FALSE);
9babb374 3771 if (error && error != ENOENT)
a08ee875
LG
3772 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3773 error = dsl_destroy_head(clone1name);
9babb374 3774 if (error && error != ENOENT)
a08ee875
LG
3775 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3776 error = dsl_destroy_snapshot(snap1name, B_FALSE);
9babb374 3777 if (error && error != ENOENT)
a08ee875 3778 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
40b84e7a 3779
cae5b340
AX
3780 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3781 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3782 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3783 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3784 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
9babb374
BB
3785}
3786
3787/*
3788 * Verify dsl_dataset_promote handles EBUSY
3789 */
3790void
428870ff 3791ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
9babb374 3792{
a08ee875 3793 objset_t *os;
40b84e7a
BB
3794 char *snap1name;
3795 char *clone1name;
3796 char *snap2name;
3797 char *clone2name;
3798 char *snap3name;
428870ff
BB
3799 char *osname = zd->zd_name;
3800 int error;
9babb374 3801
cae5b340
AX
3802 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3803 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3804 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3805 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3806 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
40b84e7a 3807
ea04106b 3808 (void) rw_rdlock(&ztest_name_lock);
9babb374 3809
428870ff 3810 ztest_dsl_dataset_cleanup(osname, id);
9babb374 3811
cae5b340
AX
3812 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3813 "%s@s1_%llu", osname, (u_longlong_t)id);
3814 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3815 "%s/c1_%llu", osname, (u_longlong_t)id);
3816 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3817 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3818 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3819 "%s/c2_%llu", osname, (u_longlong_t)id);
3820 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3821 "%s@s3_%llu", clone1name, (u_longlong_t)id);
9babb374 3822
a08ee875 3823 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
9babb374
BB
3824 if (error && error != EEXIST) {
3825 if (error == ENOSPC) {
428870ff 3826 ztest_record_enospc(FTAG);
9babb374
BB
3827 goto out;
3828 }
3829 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3830 }
3831
a08ee875 3832 error = dmu_objset_clone(clone1name, snap1name);
9babb374
BB
3833 if (error) {
3834 if (error == ENOSPC) {
428870ff 3835 ztest_record_enospc(FTAG);
9babb374
BB
3836 goto out;
3837 }
3838 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3839 }
3840
a08ee875 3841 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
428870ff
BB
3842 if (error && error != EEXIST) {
3843 if (error == ENOSPC) {
3844 ztest_record_enospc(FTAG);
3845 goto out;
34dc7c2f 3846 }
428870ff
BB
3847 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3848 }
34dc7c2f 3849
a08ee875 3850 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
428870ff
BB
3851 if (error && error != EEXIST) {
3852 if (error == ENOSPC) {
3853 ztest_record_enospc(FTAG);
3854 goto out;
3855 }
3856 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3857 }
34dc7c2f 3858
a08ee875 3859 error = dmu_objset_clone(clone2name, snap3name);
428870ff
BB
3860 if (error) {
3861 if (error == ENOSPC) {
3862 ztest_record_enospc(FTAG);
3863 goto out;
3864 }
3865 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3866 }
34dc7c2f 3867
a08ee875 3868 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
428870ff 3869 if (error)
a08ee875 3870 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
428870ff 3871 error = dsl_dataset_promote(clone2name, NULL);
ea04106b
AX
3872 if (error == ENOSPC) {
3873 dmu_objset_disown(os, FTAG);
3874 ztest_record_enospc(FTAG);
3875 goto out;
3876 }
428870ff
BB
3877 if (error != EBUSY)
3878 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3879 error);
a08ee875 3880 dmu_objset_disown(os, FTAG);
34dc7c2f 3881
428870ff
BB
3882out:
3883 ztest_dsl_dataset_cleanup(osname, id);
34dc7c2f 3884
ea04106b 3885 (void) rw_unlock(&ztest_name_lock);
40b84e7a 3886
cae5b340
AX
3887 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3888 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3889 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3890 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3891 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
428870ff 3892}
34dc7c2f 3893
40b84e7a 3894#undef OD_ARRAY_SIZE
a08ee875 3895#define OD_ARRAY_SIZE 4
40b84e7a 3896
428870ff
BB
3897/*
3898 * Verify that dmu_object_{alloc,free} work as expected.
3899 */
3900void
3901ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3902{
40b84e7a
BB
3903 ztest_od_t *od;
3904 int batchsize;
3905 int size;
d6320ddb 3906 int b;
34dc7c2f 3907
a08ee875 3908 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
3909 od = umem_alloc(size, UMEM_NOFAIL);
3910 batchsize = OD_ARRAY_SIZE;
3911
d6320ddb 3912 for (b = 0; b < batchsize; b++)
cae5b340
AX
3913 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3914 0, 0, 0);
34dc7c2f 3915
428870ff
BB
3916 /*
3917 * Destroy the previous batch of objects, create a new batch,
3918 * and do some I/O on the new objects.
3919 */
40b84e7a 3920 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
428870ff 3921 return;
34dc7c2f 3922
428870ff
BB
3923 while (ztest_random(4 * batchsize) != 0)
3924 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3925 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
40b84e7a
BB
3926
3927 umem_free(od, size);
34dc7c2f
BB
3928}
3929
40b84e7a 3930#undef OD_ARRAY_SIZE
a08ee875 3931#define OD_ARRAY_SIZE 2
40b84e7a 3932
34dc7c2f
BB
3933/*
3934 * Verify that dmu_{read,write} work as expected.
3935 */
34dc7c2f 3936void
428870ff 3937ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3938{
40b84e7a
BB
3939 int size;
3940 ztest_od_t *od;
3941
428870ff 3942 objset_t *os = zd->zd_os;
a08ee875 3943 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a 3944 od = umem_alloc(size, UMEM_NOFAIL);
34dc7c2f
BB
3945 dmu_tx_t *tx;
3946 int i, freeit, error;
3947 uint64_t n, s, txg;
3948 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
428870ff
BB
3949 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3950 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
34dc7c2f
BB
3951 uint64_t regions = 997;
3952 uint64_t stride = 123456789ULL;
3953 uint64_t width = 40;
3954 int free_percent = 5;
3955
3956 /*
3957 * This test uses two objects, packobj and bigobj, that are always
3958 * updated together (i.e. in the same tx) so that their contents are
3959 * in sync and can be compared. Their contents relate to each other
3960 * in a simple way: packobj is a dense array of 'bufwad' structures,
3961 * while bigobj is a sparse array of the same bufwads. Specifically,
3962 * for any index n, there are three bufwads that should be identical:
3963 *
3964 * packobj, at offset n * sizeof (bufwad_t)
3965 * bigobj, at the head of the nth chunk
3966 * bigobj, at the tail of the nth chunk
3967 *
3968 * The chunk size is arbitrary. It doesn't have to be a power of two,
3969 * and it doesn't have any relation to the object blocksize.
3970 * The only requirement is that it can hold at least two bufwads.
3971 *
3972 * Normally, we write the bufwad to each of these locations.
3973 * However, free_percent of the time we instead write zeroes to
3974 * packobj and perform a dmu_free_range() on bigobj. By comparing
3975 * bigobj to packobj, we can verify that the DMU is correctly
3976 * tracking which parts of an object are allocated and free,
3977 * and that the contents of the allocated blocks are correct.
3978 */
3979
3980 /*
3981 * Read the directory info. If it's the first time, set things up.
3982 */
cae5b340
AX
3983 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3984 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3985 chunksize);
34dc7c2f 3986
40b84e7a
BB
3987 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3988 umem_free(od, size);
428870ff 3989 return;
40b84e7a 3990 }
34dc7c2f 3991
428870ff
BB
3992 bigobj = od[0].od_object;
3993 packobj = od[1].od_object;
3994 chunksize = od[0].od_gen;
3995 ASSERT(chunksize == od[1].od_gen);
34dc7c2f
BB
3996
3997 /*
3998 * Prefetch a random chunk of the big object.
3999 * Our aim here is to get some async reads in flight
4000 * for blocks that we may free below; the DMU should
4001 * handle this race correctly.
4002 */
4003 n = ztest_random(regions) * stride + ztest_random(width);
4004 s = 1 + ztest_random(2 * width - 1);
cae5b340
AX
4005 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4006 ZIO_PRIORITY_SYNC_READ);
34dc7c2f
BB
4007
4008 /*
4009 * Pick a random index and compute the offsets into packobj and bigobj.
4010 */
4011 n = ztest_random(regions) * stride + ztest_random(width);
4012 s = 1 + ztest_random(width - 1);
4013
4014 packoff = n * sizeof (bufwad_t);
4015 packsize = s * sizeof (bufwad_t);
4016
428870ff
BB
4017 bigoff = n * chunksize;
4018 bigsize = s * chunksize;
34dc7c2f
BB
4019
4020 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4021 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4022
4023 /*
4024 * free_percent of the time, free a range of bigobj rather than
4025 * overwriting it.
4026 */
4027 freeit = (ztest_random(100) < free_percent);
4028
4029 /*
4030 * Read the current contents of our objects.
4031 */
428870ff 4032 error = dmu_read(os, packobj, packoff, packsize, packbuf,
9babb374 4033 DMU_READ_PREFETCH);
c06d4368 4034 ASSERT0(error);
428870ff 4035 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
9babb374 4036 DMU_READ_PREFETCH);
c06d4368 4037 ASSERT0(error);
34dc7c2f
BB
4038
4039 /*
4040 * Get a tx for the mods to both packobj and bigobj.
4041 */
4042 tx = dmu_tx_create(os);
4043
428870ff 4044 dmu_tx_hold_write(tx, packobj, packoff, packsize);
34dc7c2f
BB
4045
4046 if (freeit)
428870ff 4047 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
34dc7c2f 4048 else
428870ff 4049 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
34dc7c2f 4050
a08ee875
LG
4051 /* This accounts for setting the checksum/compression. */
4052 dmu_tx_hold_bonus(tx, bigobj);
4053
428870ff
BB
4054 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4055 if (txg == 0) {
34dc7c2f
BB
4056 umem_free(packbuf, packsize);
4057 umem_free(bigbuf, bigsize);
40b84e7a 4058 umem_free(od, size);
34dc7c2f
BB
4059 return;
4060 }
4061
ea04106b
AX
4062 enum zio_checksum cksum;
4063 do {
4064 cksum = (enum zio_checksum)
4065 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4066 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4067 dmu_object_set_checksum(os, bigobj, cksum, tx);
428870ff 4068
ea04106b
AX
4069 enum zio_compress comp;
4070 do {
4071 comp = (enum zio_compress)
4072 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4073 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4074 dmu_object_set_compress(os, bigobj, comp, tx);
34dc7c2f
BB
4075
4076 /*
4077 * For each index from n to n + s, verify that the existing bufwad
4078 * in packobj matches the bufwads at the head and tail of the
4079 * corresponding chunk in bigobj. Then update all three bufwads
4080 * with the new values we want to write out.
4081 */
4082 for (i = 0; i < s; i++) {
4083 /* LINTED */
4084 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4085 /* LINTED */
428870ff 4086 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34dc7c2f 4087 /* LINTED */
428870ff 4088 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34dc7c2f
BB
4089
4090 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4091 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4092
4093 if (pack->bw_txg > txg)
4094 fatal(0, "future leak: got %llx, open txg is %llx",
4095 pack->bw_txg, txg);
4096
4097 if (pack->bw_data != 0 && pack->bw_index != n + i)
4098 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4099 pack->bw_index, n, i);
4100
4101 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4102 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4103
4104 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4105 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4106
4107 if (freeit) {
4108 bzero(pack, sizeof (bufwad_t));
4109 } else {
4110 pack->bw_index = n + i;
4111 pack->bw_txg = txg;
4112 pack->bw_data = 1 + ztest_random(-2ULL);
4113 }
4114 *bigH = *pack;
4115 *bigT = *pack;
4116 }
4117
4118 /*
4119 * We've verified all the old bufwads, and made new ones.
4120 * Now write them out.
4121 */
428870ff 4122 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
34dc7c2f
BB
4123
4124 if (freeit) {
c242c188 4125 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4126 (void) printf("freeing offset %llx size %llx"
4127 " txg %llx\n",
4128 (u_longlong_t)bigoff,
4129 (u_longlong_t)bigsize,
4130 (u_longlong_t)txg);
4131 }
428870ff 4132 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
34dc7c2f 4133 } else {
c242c188 4134 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4135 (void) printf("writing offset %llx size %llx"
4136 " txg %llx\n",
4137 (u_longlong_t)bigoff,
4138 (u_longlong_t)bigsize,
4139 (u_longlong_t)txg);
4140 }
428870ff 4141 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
34dc7c2f
BB
4142 }
4143
4144 dmu_tx_commit(tx);
4145
4146 /*
4147 * Sanity check the stuff we just wrote.
4148 */
4149 {
4150 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4151 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4152
428870ff 4153 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4154 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4155 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374 4156 bigsize, bigcheck, DMU_READ_PREFETCH));
34dc7c2f
BB
4157
4158 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4159 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4160
4161 umem_free(packcheck, packsize);
4162 umem_free(bigcheck, bigsize);
4163 }
4164
4165 umem_free(packbuf, packsize);
4166 umem_free(bigbuf, bigsize);
40b84e7a 4167 umem_free(od, size);
34dc7c2f
BB
4168}
4169
9babb374
BB
4170void
4171compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
428870ff 4172 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
9babb374
BB
4173{
4174 uint64_t i;
4175 bufwad_t *pack;
4176 bufwad_t *bigH;
4177 bufwad_t *bigT;
4178
4179 /*
4180 * For each index from n to n + s, verify that the existing bufwad
4181 * in packobj matches the bufwads at the head and tail of the
4182 * corresponding chunk in bigobj. Then update all three bufwads
4183 * with the new values we want to write out.
4184 */
4185 for (i = 0; i < s; i++) {
4186 /* LINTED */
4187 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4188 /* LINTED */
428870ff 4189 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
9babb374 4190 /* LINTED */
428870ff 4191 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
9babb374
BB
4192
4193 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4194 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4195
4196 if (pack->bw_txg > txg)
4197 fatal(0, "future leak: got %llx, open txg is %llx",
4198 pack->bw_txg, txg);
4199
4200 if (pack->bw_data != 0 && pack->bw_index != n + i)
4201 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4202 pack->bw_index, n, i);
4203
4204 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4205 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4206
4207 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4208 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4209
4210 pack->bw_index = n + i;
4211 pack->bw_txg = txg;
4212 pack->bw_data = 1 + ztest_random(-2ULL);
4213
4214 *bigH = *pack;
4215 *bigT = *pack;
4216 }
4217}
4218
40b84e7a 4219#undef OD_ARRAY_SIZE
a08ee875 4220#define OD_ARRAY_SIZE 2
40b84e7a 4221
9babb374 4222void
428870ff 4223ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
9babb374 4224{
428870ff 4225 objset_t *os = zd->zd_os;
40b84e7a 4226 ztest_od_t *od;
9babb374
BB
4227 dmu_tx_t *tx;
4228 uint64_t i;
4229 int error;
40b84e7a 4230 int size;
9babb374
BB
4231 uint64_t n, s, txg;
4232 bufwad_t *packbuf, *bigbuf;
428870ff
BB
4233 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4234 uint64_t blocksize = ztest_random_blocksize();
4235 uint64_t chunksize = blocksize;
9babb374
BB
4236 uint64_t regions = 997;
4237 uint64_t stride = 123456789ULL;
4238 uint64_t width = 9;
4239 dmu_buf_t *bonus_db;
4240 arc_buf_t **bigbuf_arcbufs;
428870ff 4241 dmu_object_info_t doi;
9babb374 4242
a08ee875 4243 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
4244 od = umem_alloc(size, UMEM_NOFAIL);
4245
9babb374
BB
4246 /*
4247 * This test uses two objects, packobj and bigobj, that are always
4248 * updated together (i.e. in the same tx) so that their contents are
4249 * in sync and can be compared. Their contents relate to each other
4250 * in a simple way: packobj is a dense array of 'bufwad' structures,
4251 * while bigobj is a sparse array of the same bufwads. Specifically,
4252 * for any index n, there are three bufwads that should be identical:
4253 *
4254 * packobj, at offset n * sizeof (bufwad_t)
4255 * bigobj, at the head of the nth chunk
4256 * bigobj, at the tail of the nth chunk
4257 *
4258 * The chunk size is set equal to bigobj block size so that
4259 * dmu_assign_arcbuf() can be tested for object updates.
4260 */
4261
4262 /*
4263 * Read the directory info. If it's the first time, set things up.
4264 */
cae5b340
AX
4265 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4266 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4267 chunksize);
40b84e7a 4268
9babb374 4269
40b84e7a
BB
4270 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4271 umem_free(od, size);
428870ff 4272 return;
40b84e7a 4273 }
9babb374 4274
428870ff
BB
4275 bigobj = od[0].od_object;
4276 packobj = od[1].od_object;
4277 blocksize = od[0].od_blocksize;
4278 chunksize = blocksize;
4279 ASSERT(chunksize == od[1].od_gen);
9babb374 4280
428870ff
BB
4281 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4282 VERIFY(ISP2(doi.doi_data_block_size));
4283 VERIFY(chunksize == doi.doi_data_block_size);
4284 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
9babb374
BB
4285
4286 /*
4287 * Pick a random index and compute the offsets into packobj and bigobj.
4288 */
4289 n = ztest_random(regions) * stride + ztest_random(width);
4290 s = 1 + ztest_random(width - 1);
4291
4292 packoff = n * sizeof (bufwad_t);
4293 packsize = s * sizeof (bufwad_t);
4294
428870ff
BB
4295 bigoff = n * chunksize;
4296 bigsize = s * chunksize;
9babb374
BB
4297
4298 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4299 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4300
428870ff 4301 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
9babb374
BB
4302
4303 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4304
4305 /*
4306 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4307 * Iteration 1 test zcopy to already referenced dbufs.
4308 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4309 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4310 * Iteration 4 test zcopy when dbuf is no longer dirty.
4311 * Iteration 5 test zcopy when it can't be done.
4312 * Iteration 6 one more zcopy write.
4313 */
4314 for (i = 0; i < 7; i++) {
4315 uint64_t j;
4316 uint64_t off;
4317
4318 /*
4319 * In iteration 5 (i == 5) use arcbufs
4320 * that don't match bigobj blksz to test
4321 * dmu_assign_arcbuf() when it can't directly
4322 * assign an arcbuf to a dbuf.
4323 */
4324 for (j = 0; j < s; j++) {
e10b0808 4325 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4326 bigbuf_arcbufs[j] =
428870ff 4327 dmu_request_arcbuf(bonus_db, chunksize);
9babb374
BB
4328 } else {
4329 bigbuf_arcbufs[2 * j] =
428870ff 4330 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374 4331 bigbuf_arcbufs[2 * j + 1] =
428870ff 4332 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374
BB
4333 }
4334 }
4335
4336 /*
4337 * Get a tx for the mods to both packobj and bigobj.
4338 */
4339 tx = dmu_tx_create(os);
4340
428870ff
BB
4341 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4342 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
9babb374 4343
428870ff
BB
4344 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4345 if (txg == 0) {
9babb374
BB
4346 umem_free(packbuf, packsize);
4347 umem_free(bigbuf, bigsize);
4348 for (j = 0; j < s; j++) {
e10b0808
AX
4349 if (i != 5 ||
4350 chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4351 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4352 } else {
4353 dmu_return_arcbuf(
4354 bigbuf_arcbufs[2 * j]);
4355 dmu_return_arcbuf(
4356 bigbuf_arcbufs[2 * j + 1]);
4357 }
4358 }
4359 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4360 umem_free(od, size);
9babb374
BB
4361 dmu_buf_rele(bonus_db, FTAG);
4362 return;
4363 }
4364
9babb374
BB
4365 /*
4366 * 50% of the time don't read objects in the 1st iteration to
4367 * test dmu_assign_arcbuf() for the case when there're no
4368 * existing dbufs for the specified offsets.
4369 */
4370 if (i != 0 || ztest_random(2) != 0) {
428870ff 4371 error = dmu_read(os, packobj, packoff,
9babb374 4372 packsize, packbuf, DMU_READ_PREFETCH);
c06d4368 4373 ASSERT0(error);
428870ff 4374 error = dmu_read(os, bigobj, bigoff, bigsize,
9babb374 4375 bigbuf, DMU_READ_PREFETCH);
c06d4368 4376 ASSERT0(error);
9babb374
BB
4377 }
4378 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
428870ff 4379 n, chunksize, txg);
9babb374
BB
4380
4381 /*
4382 * We've verified all the old bufwads, and made new ones.
4383 * Now write them out.
4384 */
428870ff 4385 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
c242c188 4386 if (ztest_opts.zo_verbose >= 7) {
9babb374
BB
4387 (void) printf("writing offset %llx size %llx"
4388 " txg %llx\n",
4389 (u_longlong_t)bigoff,
4390 (u_longlong_t)bigsize,
4391 (u_longlong_t)txg);
4392 }
428870ff 4393 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
9babb374 4394 dmu_buf_t *dbt;
e10b0808 4395 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4396 bcopy((caddr_t)bigbuf + (off - bigoff),
428870ff 4397 bigbuf_arcbufs[j]->b_data, chunksize);
9babb374
BB
4398 } else {
4399 bcopy((caddr_t)bigbuf + (off - bigoff),
4400 bigbuf_arcbufs[2 * j]->b_data,
428870ff 4401 chunksize / 2);
9babb374 4402 bcopy((caddr_t)bigbuf + (off - bigoff) +
428870ff 4403 chunksize / 2,
9babb374 4404 bigbuf_arcbufs[2 * j + 1]->b_data,
428870ff 4405 chunksize / 2);
9babb374
BB
4406 }
4407
4408 if (i == 1) {
428870ff
BB
4409 VERIFY(dmu_buf_hold(os, bigobj, off,
4410 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
9babb374 4411 }
e10b0808 4412 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4413 dmu_assign_arcbuf(bonus_db, off,
4414 bigbuf_arcbufs[j], tx);
4415 } else {
4416 dmu_assign_arcbuf(bonus_db, off,
4417 bigbuf_arcbufs[2 * j], tx);
4418 dmu_assign_arcbuf(bonus_db,
428870ff 4419 off + chunksize / 2,
9babb374
BB
4420 bigbuf_arcbufs[2 * j + 1], tx);
4421 }
4422 if (i == 1) {
4423 dmu_buf_rele(dbt, FTAG);
4424 }
4425 }
4426 dmu_tx_commit(tx);
4427
4428 /*
4429 * Sanity check the stuff we just wrote.
4430 */
4431 {
4432 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4433 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4434
428870ff 4435 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4436 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4437 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374
BB
4438 bigsize, bigcheck, DMU_READ_PREFETCH));
4439
4440 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4441 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4442
4443 umem_free(packcheck, packsize);
4444 umem_free(bigcheck, bigsize);
4445 }
4446 if (i == 2) {
4447 txg_wait_open(dmu_objset_pool(os), 0);
4448 } else if (i == 3) {
4449 txg_wait_synced(dmu_objset_pool(os), 0);
4450 }
4451 }
4452
4453 dmu_buf_rele(bonus_db, FTAG);
4454 umem_free(packbuf, packsize);
4455 umem_free(bigbuf, bigsize);
4456 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4457 umem_free(od, size);
9babb374
BB
4458}
4459
428870ff 4460/* ARGSUSED */
34dc7c2f 4461void
428870ff 4462ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4463{
40b84e7a
BB
4464 ztest_od_t *od;
4465
a08ee875 4466 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
428870ff
BB
4467 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4468 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
34dc7c2f
BB
4469
4470 /*
428870ff
BB
4471 * Have multiple threads write to large offsets in an object
4472 * to verify that parallel writes to an object -- even to the
4473 * same blocks within the object -- doesn't cause any trouble.
34dc7c2f 4474 */
cae5b340 4475 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
9babb374 4476
40b84e7a 4477 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
34dc7c2f 4478 return;
34dc7c2f 4479
428870ff 4480 while (ztest_random(10) != 0)
40b84e7a
BB
4481 ztest_io(zd, od->od_object, offset);
4482
a08ee875 4483 umem_free(od, sizeof (ztest_od_t));
428870ff 4484}
34dc7c2f 4485
428870ff
BB
4486void
4487ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4488{
40b84e7a 4489 ztest_od_t *od;
428870ff
BB
4490 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4491 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4492 uint64_t count = ztest_random(20) + 1;
4493 uint64_t blocksize = ztest_random_blocksize();
4494 void *data;
34dc7c2f 4495
a08ee875 4496 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
34dc7c2f 4497
cae5b340 4498 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
40b84e7a 4499
a08ee875
LG
4500 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4501 !ztest_random(2)) != 0) {
4502 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4503 return;
40b84e7a 4504 }
34dc7c2f 4505
40b84e7a 4506 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
a08ee875 4507 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4508 return;
40b84e7a 4509 }
34dc7c2f 4510
40b84e7a 4511 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
34dc7c2f 4512
428870ff 4513 data = umem_zalloc(blocksize, UMEM_NOFAIL);
34dc7c2f 4514
428870ff
BB
4515 while (ztest_random(count) != 0) {
4516 uint64_t randoff = offset + (ztest_random(count) * blocksize);
40b84e7a 4517 if (ztest_write(zd, od->od_object, randoff, blocksize,
428870ff
BB
4518 data) != 0)
4519 break;
4520 while (ztest_random(4) != 0)
40b84e7a 4521 ztest_io(zd, od->od_object, randoff);
9babb374 4522 }
34dc7c2f 4523
428870ff 4524 umem_free(data, blocksize);
a08ee875 4525 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4526}
4527
4528/*
4529 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4530 */
4531#define ZTEST_ZAP_MIN_INTS 1
4532#define ZTEST_ZAP_MAX_INTS 4
4533#define ZTEST_ZAP_MAX_PROPS 1000
4534
4535void
428870ff 4536ztest_zap(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4537{
428870ff 4538 objset_t *os = zd->zd_os;
40b84e7a 4539 ztest_od_t *od;
34dc7c2f
BB
4540 uint64_t object;
4541 uint64_t txg, last_txg;
4542 uint64_t value[ZTEST_ZAP_MAX_INTS];
4543 uint64_t zl_ints, zl_intsize, prop;
4544 int i, ints;
4545 dmu_tx_t *tx;
4546 char propname[100], txgname[100];
4547 int error;
34dc7c2f
BB
4548 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4549
a08ee875 4550 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
cae5b340 4551 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
34dc7c2f 4552
40b84e7a 4553 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
cae5b340 4554 !ztest_random(2)) != 0)
40b84e7a 4555 goto out;
34dc7c2f 4556
40b84e7a 4557 object = od->od_object;
34dc7c2f 4558
428870ff
BB
4559 /*
4560 * Generate a known hash collision, and verify that
4561 * we can lookup and remove both entries.
4562 */
4563 tx = dmu_tx_create(os);
4564 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4565 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4566 if (txg == 0)
40b84e7a 4567 goto out;
428870ff
BB
4568 for (i = 0; i < 2; i++) {
4569 value[i] = i;
4570 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4571 1, &value[i], tx));
4572 }
4573 for (i = 0; i < 2; i++) {
4574 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4575 sizeof (uint64_t), 1, &value[i], tx));
4576 VERIFY3U(0, ==,
4577 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4578 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4579 ASSERT3U(zl_ints, ==, 1);
4580 }
4581 for (i = 0; i < 2; i++) {
4582 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
34dc7c2f 4583 }
428870ff 4584 dmu_tx_commit(tx);
34dc7c2f 4585
428870ff
BB
4586 /*
4587 * Generate a buch of random entries.
4588 */
34dc7c2f
BB
4589 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4590
4591 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4592 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4593 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4594 bzero(value, sizeof (value));
4595 last_txg = 0;
4596
4597 /*
4598 * If these zap entries already exist, validate their contents.
4599 */
4600 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4601 if (error == 0) {
4602 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4603 ASSERT3U(zl_ints, ==, 1);
4604
4605 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4606 zl_ints, &last_txg) == 0);
4607
4608 VERIFY(zap_length(os, object, propname, &zl_intsize,
4609 &zl_ints) == 0);
4610
4611 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4612 ASSERT3U(zl_ints, ==, ints);
4613
4614 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4615 zl_ints, value) == 0);
4616
4617 for (i = 0; i < ints; i++) {
4618 ASSERT3U(value[i], ==, last_txg + object + i);
4619 }
4620 } else {
4621 ASSERT3U(error, ==, ENOENT);
4622 }
4623
4624 /*
4625 * Atomically update two entries in our zap object.
4626 * The first is named txg_%llu, and contains the txg
4627 * in which the property was last updated. The second
4628 * is named prop_%llu, and the nth element of its value
4629 * should be txg + object + n.
4630 */
4631 tx = dmu_tx_create(os);
428870ff
BB
4632 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4633 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4634 if (txg == 0)
40b84e7a 4635 goto out;
34dc7c2f
BB
4636
4637 if (last_txg > txg)
4638 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4639
4640 for (i = 0; i < ints; i++)
4641 value[i] = txg + object + i;
4642
428870ff
BB
4643 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4644 1, &txg, tx));
4645 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4646 ints, value, tx));
34dc7c2f
BB
4647
4648 dmu_tx_commit(tx);
4649
4650 /*
4651 * Remove a random pair of entries.
4652 */
4653 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4654 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4655 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4656
4657 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4658
4659 if (error == ENOENT)
40b84e7a 4660 goto out;
34dc7c2f 4661
c06d4368 4662 ASSERT0(error);
34dc7c2f
BB
4663
4664 tx = dmu_tx_create(os);
428870ff
BB
4665 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4666 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4667 if (txg == 0)
40b84e7a 4668 goto out;
428870ff
BB
4669 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4670 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4671 dmu_tx_commit(tx);
40b84e7a 4672out:
a08ee875 4673 umem_free(od, sizeof (ztest_od_t));
428870ff 4674}
34dc7c2f 4675
428870ff
BB
4676/*
4677 * Testcase to test the upgrading of a microzap to fatzap.
4678 */
4679void
4680ztest_fzap(ztest_ds_t *zd, uint64_t id)
4681{
4682 objset_t *os = zd->zd_os;
40b84e7a 4683 ztest_od_t *od;
428870ff 4684 uint64_t object, txg;
d6320ddb 4685 int i;
34dc7c2f 4686
a08ee875 4687 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
cae5b340 4688 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4689
40b84e7a 4690 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
cae5b340 4691 !ztest_random(2)) != 0)
40b84e7a
BB
4692 goto out;
4693 object = od->od_object;
34dc7c2f
BB
4694
4695 /*
428870ff
BB
4696 * Add entries to this ZAP and make sure it spills over
4697 * and gets upgraded to a fatzap. Also, since we are adding
4698 * 2050 entries we should see ptrtbl growth and leaf-block split.
34dc7c2f 4699 */
d6320ddb 4700 for (i = 0; i < 2050; i++) {
cae5b340 4701 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
4702 uint64_t value = i;
4703 dmu_tx_t *tx;
4704 int error;
34dc7c2f 4705
428870ff 4706 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
b8864a23 4707 (u_longlong_t)id, (u_longlong_t)value);
428870ff
BB
4708
4709 tx = dmu_tx_create(os);
4710 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4711 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4712 if (txg == 0)
40b84e7a 4713 goto out;
428870ff
BB
4714 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4715 &value, tx);
4716 ASSERT(error == 0 || error == EEXIST);
4717 dmu_tx_commit(tx);
34dc7c2f 4718 }
40b84e7a 4719out:
a08ee875 4720 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4721}
4722
428870ff 4723/* ARGSUSED */
34dc7c2f 4724void
428870ff 4725ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4726{
428870ff 4727 objset_t *os = zd->zd_os;
40b84e7a 4728 ztest_od_t *od;
34dc7c2f
BB
4729 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4730 dmu_tx_t *tx;
4731 int i, namelen, error;
428870ff 4732 int micro = ztest_random(2);
34dc7c2f
BB
4733 char name[20], string_value[20];
4734 void *data;
4735
a08ee875 4736 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
cae5b340 4737 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4738
40b84e7a 4739 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
a08ee875 4740 umem_free(od, sizeof (ztest_od_t));
428870ff 4741 return;
40b84e7a 4742 }
428870ff 4743
40b84e7a 4744 object = od->od_object;
428870ff 4745
34dc7c2f
BB
4746 /*
4747 * Generate a random name of the form 'xxx.....' where each
4748 * x is a random printable character and the dots are dots.
4749 * There are 94 such characters, and the name length goes from
4750 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4751 */
4752 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4753
4754 for (i = 0; i < 3; i++)
4755 name[i] = '!' + ztest_random('~' - '!' + 1);
4756 for (; i < namelen - 1; i++)
4757 name[i] = '.';
4758 name[i] = '\0';
4759
428870ff 4760 if ((namelen & 1) || micro) {
34dc7c2f
BB
4761 wsize = sizeof (txg);
4762 wc = 1;
4763 data = &txg;
4764 } else {
4765 wsize = 1;
4766 wc = namelen;
4767 data = string_value;
4768 }
4769
4770 count = -1ULL;
a08ee875 4771 VERIFY0(zap_count(os, object, &count));
34dc7c2f
BB
4772 ASSERT(count != -1ULL);
4773
4774 /*
4775 * Select an operation: length, lookup, add, update, remove.
4776 */
4777 i = ztest_random(5);
4778
4779 if (i >= 2) {
4780 tx = dmu_tx_create(os);
428870ff
BB
4781 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4782 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
cae5b340
AX
4783 if (txg == 0) {
4784 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4785 return;
cae5b340 4786 }
34dc7c2f
BB
4787 bcopy(name, string_value, namelen);
4788 } else {
4789 tx = NULL;
4790 txg = 0;
4791 bzero(string_value, namelen);
4792 }
4793
4794 switch (i) {
4795
4796 case 0:
4797 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4798 if (error == 0) {
4799 ASSERT3U(wsize, ==, zl_wsize);
4800 ASSERT3U(wc, ==, zl_wc);
4801 } else {
4802 ASSERT3U(error, ==, ENOENT);
4803 }
4804 break;
4805
4806 case 1:
4807 error = zap_lookup(os, object, name, wsize, wc, data);
4808 if (error == 0) {
4809 if (data == string_value &&
4810 bcmp(name, data, namelen) != 0)
4811 fatal(0, "name '%s' != val '%s' len %d",
4812 name, data, namelen);
4813 } else {
4814 ASSERT3U(error, ==, ENOENT);
4815 }
4816 break;
4817
4818 case 2:
4819 error = zap_add(os, object, name, wsize, wc, data, tx);
4820 ASSERT(error == 0 || error == EEXIST);
4821 break;
4822
4823 case 3:
4824 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4825 break;
4826
4827 case 4:
4828 error = zap_remove(os, object, name, tx);
4829 ASSERT(error == 0 || error == ENOENT);
4830 break;
4831 }
4832
4833 if (tx != NULL)
4834 dmu_tx_commit(tx);
40b84e7a 4835
a08ee875 4836 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4837}
4838
428870ff
BB
4839/*
4840 * Commit callback data.
4841 */
4842typedef struct ztest_cb_data {
4843 list_node_t zcd_node;
4844 uint64_t zcd_txg;
4845 int zcd_expected_err;
4846 boolean_t zcd_added;
4847 boolean_t zcd_called;
4848 spa_t *zcd_spa;
4849} ztest_cb_data_t;
4850
4851/* This is the actual commit callback function */
4852static void
4853ztest_commit_callback(void *arg, int error)
4854{
4855 ztest_cb_data_t *data = arg;
4856 uint64_t synced_txg;
4857
4858 VERIFY(data != NULL);
4859 VERIFY3S(data->zcd_expected_err, ==, error);
4860 VERIFY(!data->zcd_called);
4861
4862 synced_txg = spa_last_synced_txg(data->zcd_spa);
4863 if (data->zcd_txg > synced_txg)
4864 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4865 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4866 synced_txg);
4867
4868 data->zcd_called = B_TRUE;
4869
4870 if (error == ECANCELED) {
c06d4368 4871 ASSERT0(data->zcd_txg);
428870ff
BB
4872 ASSERT(!data->zcd_added);
4873
4874 /*
4875 * The private callback data should be destroyed here, but
4876 * since we are going to check the zcd_called field after
4877 * dmu_tx_abort(), we will destroy it there.
4878 */
4879 return;
4880 }
4881
090ff092 4882 ASSERT(data->zcd_added);
428870ff
BB
4883 ASSERT3U(data->zcd_txg, !=, 0);
4884
1e33ac1e 4885 (void) mutex_enter(&zcl.zcl_callbacks_lock);
090ff092
RC
4886
4887 /* See if this cb was called more quickly */
4888 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4889 zc_min_txg_delay = synced_txg - data->zcd_txg;
4890
4891 /* Remove our callback from the list */
428870ff 4892 list_remove(&zcl.zcl_callbacks, data);
090ff092 4893
1e33ac1e 4894 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff 4895
428870ff
BB
4896 umem_free(data, sizeof (ztest_cb_data_t));
4897}
4898
4899/* Allocate and initialize callback data structure */
4900static ztest_cb_data_t *
4901ztest_create_cb_data(objset_t *os, uint64_t txg)
4902{
4903 ztest_cb_data_t *cb_data;
4904
4905 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4906
4907 cb_data->zcd_txg = txg;
4908 cb_data->zcd_spa = dmu_objset_spa(os);
1e33ac1e 4909 list_link_init(&cb_data->zcd_node);
428870ff
BB
4910
4911 return (cb_data);
4912}
4913
428870ff
BB
4914/*
4915 * Commit callback test.
4916 */
34dc7c2f 4917void
428870ff
BB
4918ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4919{
4920 objset_t *os = zd->zd_os;
40b84e7a 4921 ztest_od_t *od;
428870ff
BB
4922 dmu_tx_t *tx;
4923 ztest_cb_data_t *cb_data[3], *tmp_cb;
4924 uint64_t old_txg, txg;
090ff092 4925 int i, error = 0;
428870ff 4926
a08ee875 4927 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
cae5b340 4928 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
428870ff 4929
40b84e7a 4930 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
a08ee875 4931 umem_free(od, sizeof (ztest_od_t));
428870ff 4932 return;
40b84e7a 4933 }
428870ff
BB
4934
4935 tx = dmu_tx_create(os);
4936
4937 cb_data[0] = ztest_create_cb_data(os, 0);
4938 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4939
40b84e7a 4940 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
428870ff
BB
4941
4942 /* Every once in a while, abort the transaction on purpose */
4943 if (ztest_random(100) == 0)
4944 error = -1;
4945
4946 if (!error)
4947 error = dmu_tx_assign(tx, TXG_NOWAIT);
4948
4949 txg = error ? 0 : dmu_tx_get_txg(tx);
4950
4951 cb_data[0]->zcd_txg = txg;
4952 cb_data[1] = ztest_create_cb_data(os, txg);
4953 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4954
4955 if (error) {
4956 /*
4957 * It's not a strict requirement to call the registered
4958 * callbacks from inside dmu_tx_abort(), but that's what
4959 * it's supposed to happen in the current implementation
4960 * so we will check for that.
4961 */
4962 for (i = 0; i < 2; i++) {
4963 cb_data[i]->zcd_expected_err = ECANCELED;
4964 VERIFY(!cb_data[i]->zcd_called);
4965 }
4966
4967 dmu_tx_abort(tx);
4968
4969 for (i = 0; i < 2; i++) {
4970 VERIFY(cb_data[i]->zcd_called);
4971 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4972 }
4973
a08ee875 4974 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
4975 return;
4976 }
4977
4978 cb_data[2] = ztest_create_cb_data(os, txg);
4979 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4980
4981 /*
4982 * Read existing data to make sure there isn't a future leak.
4983 */
40b84e7a 4984 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
428870ff
BB
4985 &old_txg, DMU_READ_PREFETCH));
4986
4987 if (old_txg > txg)
4988 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4989 old_txg, txg);
4990
40b84e7a 4991 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
428870ff 4992
1e33ac1e 4993 (void) mutex_enter(&zcl.zcl_callbacks_lock);
428870ff
BB
4994
4995 /*
4996 * Since commit callbacks don't have any ordering requirement and since
4997 * it is theoretically possible for a commit callback to be called
4998 * after an arbitrary amount of time has elapsed since its txg has been
4999 * synced, it is difficult to reliably determine whether a commit
5000 * callback hasn't been called due to high load or due to a flawed
5001 * implementation.
5002 *
5003 * In practice, we will assume that if after a certain number of txgs a
5004 * commit callback hasn't been called, then most likely there's an
5005 * implementation bug..
5006 */
5007 tmp_cb = list_head(&zcl.zcl_callbacks);
5008 if (tmp_cb != NULL &&
090ff092 5009 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
428870ff
BB
5010 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5011 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5012 }
5013
5014 /*
5015 * Let's find the place to insert our callbacks.
5016 *
5017 * Even though the list is ordered by txg, it is possible for the
5018 * insertion point to not be the end because our txg may already be
5019 * quiescing at this point and other callbacks in the open txg
5020 * (from other objsets) may have sneaked in.
5021 */
5022 tmp_cb = list_tail(&zcl.zcl_callbacks);
5023 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5024 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5025
5026 /* Add the 3 callbacks to the list */
5027 for (i = 0; i < 3; i++) {
5028 if (tmp_cb == NULL)
5029 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5030 else
5031 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5032 cb_data[i]);
5033
5034 cb_data[i]->zcd_added = B_TRUE;
5035 VERIFY(!cb_data[i]->zcd_called);
5036
5037 tmp_cb = cb_data[i];
5038 }
5039
090ff092
RC
5040 zc_cb_counter += 3;
5041
1e33ac1e 5042 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff
BB
5043
5044 dmu_tx_commit(tx);
40b84e7a 5045
a08ee875 5046 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5047}
5048
cae5b340
AX
5049/*
5050 * Visit each object in the dataset. Verify that its properties
5051 * are consistent what was stored in the block tag when it was created,
5052 * and that its unused bonus buffer space has not been overwritten.
5053 */
5054/* ARGSUSED */
5055void
5056ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5057{
5058 objset_t *os = zd->zd_os;
5059 uint64_t obj;
5060 int err = 0;
5061
5062 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5063 ztest_block_tag_t *bt = NULL;
5064 dmu_object_info_t doi;
5065 dmu_buf_t *db;
5066
5067 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
5068 continue;
5069
5070 dmu_object_info_from_db(db, &doi);
5071 if (doi.doi_bonus_size >= sizeof (*bt))
5072 bt = ztest_bt_bonus(db);
5073
5074 if (bt && bt->bt_magic == BT_MAGIC) {
5075 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5076 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5077 bt->bt_crtxg);
5078 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5079 }
5080
5081 dmu_buf_rele(db, FTAG);
5082 }
5083}
5084
428870ff
BB
5085/* ARGSUSED */
5086void
5087ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5088{
5089 zfs_prop_t proplist[] = {
5090 ZFS_PROP_CHECKSUM,
5091 ZFS_PROP_COMPRESSION,
5092 ZFS_PROP_COPIES,
5093 ZFS_PROP_DEDUP
5094 };
d6320ddb 5095 int p;
428870ff 5096
ea04106b 5097 (void) rw_rdlock(&ztest_name_lock);
428870ff 5098
d6320ddb 5099 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
428870ff
BB
5100 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5101 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5102
e10b0808
AX
5103 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5104 ztest_random_blocksize(), (int)ztest_random(2)));
5105
ea04106b 5106 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5107}
5108
5109/* ARGSUSED */
5110void
5111ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5112{
428870ff
BB
5113 nvlist_t *props = NULL;
5114
ea04106b 5115 (void) rw_rdlock(&ztest_name_lock);
428870ff 5116
c242c188 5117 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
428870ff
BB
5118 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5119
c06d4368 5120 VERIFY0(spa_prop_get(ztest_spa, &props));
428870ff 5121
c242c188 5122 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
5123 dump_nvlist(props, 4);
5124
5125 nvlist_free(props);
5126
ea04106b 5127 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5128}
5129
a08ee875
LG
5130static int
5131user_release_one(const char *snapname, const char *holdname)
5132{
5133 nvlist_t *snaps, *holds;
5134 int error;
5135
5136 snaps = fnvlist_alloc();
5137 holds = fnvlist_alloc();
5138 fnvlist_add_boolean(holds, holdname);
5139 fnvlist_add_nvlist(snaps, snapname, holds);
5140 fnvlist_free(holds);
5141 error = dsl_dataset_user_release(snaps, NULL);
5142 fnvlist_free(snaps);
5143 return (error);
5144}
5145
428870ff
BB
5146/*
5147 * Test snapshot hold/release and deferred destroy.
5148 */
5149void
5150ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5151{
34dc7c2f 5152 int error;
428870ff
BB
5153 objset_t *os = zd->zd_os;
5154 objset_t *origin;
5155 char snapname[100];
5156 char fullname[100];
5157 char clonename[100];
5158 char tag[100];
cae5b340 5159 char osname[ZFS_MAX_DATASET_NAME_LEN];
a08ee875 5160 nvlist_t *holds;
34dc7c2f 5161
ea04106b 5162 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f
BB
5163
5164 dmu_objset_name(os, osname);
5165
a08ee875
LG
5166 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5167 (u_longlong_t)id);
5168 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5169 (void) snprintf(clonename, sizeof (clonename),
5170 "%s/ch1_%llu", osname, (u_longlong_t)id);
5171 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
428870ff
BB
5172
5173 /*
5174 * Clean up from any previous run.
5175 */
a08ee875
LG
5176 error = dsl_destroy_head(clonename);
5177 if (error != ENOENT)
5178 ASSERT0(error);
5179 error = user_release_one(fullname, tag);
5180 if (error != ESRCH && error != ENOENT)
5181 ASSERT0(error);
5182 error = dsl_destroy_snapshot(fullname, B_FALSE);
5183 if (error != ENOENT)
5184 ASSERT0(error);
428870ff
BB
5185
5186 /*
5187 * Create snapshot, clone it, mark snap for deferred destroy,
5188 * destroy clone, verify snap was also destroyed.
5189 */
a08ee875 5190 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5191 if (error) {
5192 if (error == ENOSPC) {
5193 ztest_record_enospc("dmu_objset_snapshot");
5194 goto out;
34dc7c2f 5195 }
428870ff
BB
5196 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5197 }
34dc7c2f 5198
a08ee875 5199 error = dmu_objset_clone(clonename, fullname);
428870ff 5200 if (error) {
34dc7c2f 5201 if (error == ENOSPC) {
428870ff
BB
5202 ztest_record_enospc("dmu_objset_clone");
5203 goto out;
34dc7c2f 5204 }
428870ff
BB
5205 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5206 }
34dc7c2f 5207
a08ee875 5208 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5209 if (error) {
a08ee875 5210 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff
BB
5211 fullname, error);
5212 }
34dc7c2f 5213
a08ee875 5214 error = dsl_destroy_head(clonename);
428870ff 5215 if (error)
a08ee875 5216 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
34dc7c2f 5217
428870ff
BB
5218 error = dmu_objset_hold(fullname, FTAG, &origin);
5219 if (error != ENOENT)
5220 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
34dc7c2f 5221
428870ff
BB
5222 /*
5223 * Create snapshot, add temporary hold, verify that we can't
5224 * destroy a held snapshot, mark for deferred destroy,
5225 * release hold, verify snapshot was destroyed.
5226 */
a08ee875 5227 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5228 if (error) {
5229 if (error == ENOSPC) {
5230 ztest_record_enospc("dmu_objset_snapshot");
5231 goto out;
34dc7c2f 5232 }
428870ff
BB
5233 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5234 }
5235
a08ee875
LG
5236 holds = fnvlist_alloc();
5237 fnvlist_add_string(holds, fullname, tag);
5238 error = dsl_dataset_user_hold(holds, 0, NULL);
5239 fnvlist_free(holds);
5240
ea04106b
AX
5241 if (error == ENOSPC) {
5242 ztest_record_enospc("dsl_dataset_user_hold");
5243 goto out;
5244 } else if (error) {
5245 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5246 fullname, tag, error);
5247 }
428870ff 5248
a08ee875 5249 error = dsl_destroy_snapshot(fullname, B_FALSE);
428870ff 5250 if (error != EBUSY) {
a08ee875 5251 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
428870ff
BB
5252 fullname, error);
5253 }
5254
a08ee875 5255 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5256 if (error) {
a08ee875 5257 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff 5258 fullname, error);
34dc7c2f
BB
5259 }
5260
a08ee875 5261 error = user_release_one(fullname, tag);
428870ff 5262 if (error)
a08ee875 5263 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
428870ff 5264
a08ee875 5265 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
428870ff
BB
5266
5267out:
ea04106b 5268 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5269}
5270
34dc7c2f
BB
5271/*
5272 * Inject random faults into the on-disk data.
5273 */
428870ff 5274/* ARGSUSED */
34dc7c2f 5275void
428870ff 5276ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5277{
428870ff 5278 ztest_shared_t *zs = ztest_shared;
c242c188 5279 spa_t *spa = ztest_spa;
34dc7c2f
BB
5280 int fd;
5281 uint64_t offset;
428870ff 5282 uint64_t leaves;
c5b3a7bb 5283 uint64_t bad = 0x1990c0ffeedecadeull;
34dc7c2f 5284 uint64_t top, leaf;
40b84e7a
BB
5285 char *path0;
5286 char *pathrand;
34dc7c2f 5287 size_t fsize;
cae5b340 5288 int bshift = SPA_MAXBLOCKSHIFT + 2;
34dc7c2f 5289 int iters = 1000;
428870ff
BB
5290 int maxfaults;
5291 int mirror_save;
b128c09f 5292 vdev_t *vd0 = NULL;
34dc7c2f 5293 uint64_t guid0 = 0;
428870ff
BB
5294 boolean_t islog = B_FALSE;
5295
40b84e7a
BB
5296 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5297 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5298
c242c188 5299 mutex_enter(&ztest_vdev_lock);
428870ff 5300 maxfaults = MAXFAULTS();
c242c188 5301 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
428870ff 5302 mirror_save = zs->zs_mirrors;
c242c188 5303 mutex_exit(&ztest_vdev_lock);
34dc7c2f 5304
b128c09f 5305 ASSERT(leaves >= 1);
34dc7c2f 5306
a08ee875
LG
5307 /*
5308 * Grab the name lock as reader. There are some operations
5309 * which don't like to have their vdevs changed while
5310 * they are in progress (i.e. spa_change_guid). Those
5311 * operations will have grabbed the name lock as writer.
5312 */
ea04106b 5313 (void) rw_rdlock(&ztest_name_lock);
a08ee875 5314
34dc7c2f 5315 /*
b128c09f 5316 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
34dc7c2f 5317 */
b128c09f 5318 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 5319
b128c09f
BB
5320 if (ztest_random(2) == 0) {
5321 /*
428870ff 5322 * Inject errors on a normal data device or slog device.
b128c09f 5323 */
428870ff
BB
5324 top = ztest_random_vdev_top(spa, B_TRUE);
5325 leaf = ztest_random(leaves) + zs->zs_splits;
34dc7c2f 5326
b128c09f
BB
5327 /*
5328 * Generate paths to the first leaf in this top-level vdev,
5329 * and to the random leaf we selected. We'll induce transient
5330 * write failures and random online/offline activity on leaf 0,
5331 * and we'll write random garbage to the randomly chosen leaf.
5332 */
6aec1cd5 5333 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5334 ztest_opts.zo_dir, ztest_opts.zo_pool,
5335 top * leaves + zs->zs_splits);
6aec1cd5 5336 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5337 ztest_opts.zo_dir, ztest_opts.zo_pool,
5338 top * leaves + leaf);
34dc7c2f 5339
b128c09f 5340 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
428870ff
BB
5341 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5342 islog = B_TRUE;
5343
a08ee875
LG
5344 /*
5345 * If the top-level vdev needs to be resilvered
5346 * then we only allow faults on the device that is
5347 * resilvering.
5348 */
5349 if (vd0 != NULL && maxfaults != 1 &&
5350 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5351 vd0->vdev_resilver_txg != 0)) {
b128c09f
BB
5352 /*
5353 * Make vd0 explicitly claim to be unreadable,
5354 * or unwriteable, or reach behind its back
5355 * and close the underlying fd. We can do this if
5356 * maxfaults == 0 because we'll fail and reexecute,
5357 * and we can do it if maxfaults >= 2 because we'll
5358 * have enough redundancy. If maxfaults == 1, the
5359 * combination of this with injection of random data
5360 * corruption below exceeds the pool's fault tolerance.
5361 */
5362 vdev_file_t *vf = vd0->vdev_tsd;
5363
5364 if (vf != NULL && ztest_random(3) == 0) {
5365 (void) close(vf->vf_vnode->v_fd);
5366 vf->vf_vnode->v_fd = -1;
5367 } else if (ztest_random(2) == 0) {
5368 vd0->vdev_cant_read = B_TRUE;
5369 } else {
5370 vd0->vdev_cant_write = B_TRUE;
5371 }
5372 guid0 = vd0->vdev_guid;
5373 }
5374 } else {
5375 /*
5376 * Inject errors on an l2cache device.
5377 */
5378 spa_aux_vdev_t *sav = &spa->spa_l2cache;
34dc7c2f 5379
b128c09f
BB
5380 if (sav->sav_count == 0) {
5381 spa_config_exit(spa, SCL_STATE, FTAG);
ea04106b 5382 (void) rw_unlock(&ztest_name_lock);
40b84e7a 5383 goto out;
b128c09f
BB
5384 }
5385 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
34dc7c2f 5386 guid0 = vd0->vdev_guid;
b128c09f
BB
5387 (void) strcpy(path0, vd0->vdev_path);
5388 (void) strcpy(pathrand, vd0->vdev_path);
5389
5390 leaf = 0;
5391 leaves = 1;
5392 maxfaults = INT_MAX; /* no limit on cache devices */
34dc7c2f
BB
5393 }
5394
b128c09f 5395 spa_config_exit(spa, SCL_STATE, FTAG);
ea04106b 5396 (void) rw_unlock(&ztest_name_lock);
b128c09f 5397
34dc7c2f 5398 /*
428870ff
BB
5399 * If we can tolerate two or more faults, or we're dealing
5400 * with a slog, randomly online/offline vd0.
34dc7c2f 5401 */
428870ff 5402 if ((maxfaults >= 2 || islog) && guid0 != 0) {
fb5f0bc8
BB
5403 if (ztest_random(10) < 6) {
5404 int flags = (ztest_random(2) == 0 ?
5405 ZFS_OFFLINE_TEMPORARY : 0);
428870ff
BB
5406
5407 /*
5408 * We have to grab the zs_name_lock as writer to
5409 * prevent a race between offlining a slog and
5410 * destroying a dataset. Offlining the slog will
5411 * grab a reference on the dataset which may cause
a08ee875 5412 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
5413 * leaving the dataset in an inconsistent state.
5414 */
5415 if (islog)
ea04106b 5416 (void) rw_wrlock(&ztest_name_lock);
428870ff 5417
fb5f0bc8 5418 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
428870ff
BB
5419
5420 if (islog)
ea04106b 5421 (void) rw_unlock(&ztest_name_lock);
fb5f0bc8 5422 } else {
1eb5bfa3
GW
5423 /*
5424 * Ideally we would like to be able to randomly
5425 * call vdev_[on|off]line without holding locks
5426 * to force unpredictable failures but the side
5427 * effects of vdev_[on|off]line prevent us from
5428 * doing so. We grab the ztest_vdev_lock here to
5429 * prevent a race between injection testing and
5430 * aux_vdev removal.
5431 */
5432 mutex_enter(&ztest_vdev_lock);
fb5f0bc8 5433 (void) vdev_online(spa, guid0, 0, NULL);
1eb5bfa3 5434 mutex_exit(&ztest_vdev_lock);
fb5f0bc8 5435 }
34dc7c2f
BB
5436 }
5437
428870ff 5438 if (maxfaults == 0)
40b84e7a 5439 goto out;
428870ff 5440
34dc7c2f
BB
5441 /*
5442 * We have at least single-fault tolerance, so inject data corruption.
5443 */
5444 fd = open(pathrand, O_RDWR);
5445
5446 if (fd == -1) /* we hit a gap in the device namespace */
40b84e7a 5447 goto out;
34dc7c2f
BB
5448
5449 fsize = lseek(fd, 0, SEEK_END);
5450
5451 while (--iters != 0) {
cae5b340
AX
5452 /*
5453 * The offset must be chosen carefully to ensure that
5454 * we do not inject a given logical block with errors
5455 * on two different leaf devices, because ZFS can not
5456 * tolerate that (if maxfaults==1).
5457 *
5458 * We divide each leaf into chunks of size
5459 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5460 * there is a series of ranges to which we can inject errors.
5461 * Each range can accept errors on only a single leaf vdev.
5462 * The error injection ranges are separated by ranges
5463 * which we will not inject errors on any device (DMZs).
5464 * Each DMZ must be large enough such that a single block
5465 * can not straddle it, so that a single block can not be
5466 * a target in two different injection ranges (on different
5467 * leaf vdevs).
5468 *
5469 * For example, with 3 leaves, each chunk looks like:
5470 * 0 to 32M: injection range for leaf 0
5471 * 32M to 64M: DMZ - no injection allowed
5472 * 64M to 96M: injection range for leaf 1
5473 * 96M to 128M: DMZ - no injection allowed
5474 * 128M to 160M: injection range for leaf 2
5475 * 160M to 192M: DMZ - no injection allowed
5476 */
34dc7c2f
BB
5477 offset = ztest_random(fsize / (leaves << bshift)) *
5478 (leaves << bshift) + (leaf << bshift) +
5479 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5480
cae5b340
AX
5481 /*
5482 * Only allow damage to the labels at one end of the vdev.
5483 *
5484 * If all labels are damaged, the device will be totally
5485 * inaccessible, which will result in loss of data,
5486 * because we also damage (parts of) the other side of
5487 * the mirror/raidz.
5488 *
5489 * Additionally, we will always have both an even and an
5490 * odd label, so that we can handle crashes in the
5491 * middle of vdev_config_sync().
5492 */
5493 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5494 continue;
5495
5496 /*
5497 * The two end labels are stored at the "end" of the disk, but
5498 * the end of the disk (vdev_psize) is aligned to
5499 * sizeof (vdev_label_t).
5500 */
5501 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5502 if ((leaf & 1) == 1 &&
5503 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
34dc7c2f
BB
5504 continue;
5505
c242c188 5506 mutex_enter(&ztest_vdev_lock);
428870ff 5507 if (mirror_save != zs->zs_mirrors) {
c242c188 5508 mutex_exit(&ztest_vdev_lock);
428870ff 5509 (void) close(fd);
40b84e7a 5510 goto out;
428870ff 5511 }
34dc7c2f
BB
5512
5513 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5514 fatal(1, "can't inject bad word at 0x%llx in %s",
5515 offset, pathrand);
428870ff 5516
c242c188 5517 mutex_exit(&ztest_vdev_lock);
428870ff 5518
c242c188 5519 if (ztest_opts.zo_verbose >= 7)
428870ff
BB
5520 (void) printf("injected bad word into %s,"
5521 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
34dc7c2f
BB
5522 }
5523
5524 (void) close(fd);
40b84e7a
BB
5525out:
5526 umem_free(path0, MAXPATHLEN);
5527 umem_free(pathrand, MAXPATHLEN);
34dc7c2f
BB
5528}
5529
5530/*
428870ff 5531 * Verify that DDT repair works as expected.
34dc7c2f
BB
5532 */
5533void
428870ff 5534ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5535{
428870ff 5536 ztest_shared_t *zs = ztest_shared;
c242c188 5537 spa_t *spa = ztest_spa;
428870ff 5538 objset_t *os = zd->zd_os;
40b84e7a 5539 ztest_od_t *od;
428870ff
BB
5540 uint64_t object, blocksize, txg, pattern, psize;
5541 enum zio_checksum checksum = spa_dedup_checksum(spa);
5542 dmu_buf_t *db;
5543 dmu_tx_t *tx;
cae5b340 5544 abd_t *abd;
428870ff
BB
5545 blkptr_t blk;
5546 int copies = 2 * ZIO_DEDUPDITTO_MIN;
d6320ddb 5547 int i;
34dc7c2f 5548
428870ff
BB
5549 blocksize = ztest_random_blocksize();
5550 blocksize = MIN(blocksize, 2048); /* because we write so many */
34dc7c2f 5551
a08ee875 5552 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
cae5b340 5553 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
34dc7c2f 5554
40b84e7a 5555 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
a08ee875 5556 umem_free(od, sizeof (ztest_od_t));
428870ff 5557 return;
40b84e7a 5558 }
34dc7c2f
BB
5559
5560 /*
428870ff
BB
5561 * Take the name lock as writer to prevent anyone else from changing
5562 * the pool and dataset properies we need to maintain during this test.
34dc7c2f 5563 */
ea04106b 5564 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5565
428870ff
BB
5566 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5567 B_FALSE) != 0 ||
5568 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5569 B_FALSE) != 0) {
ea04106b 5570 (void) rw_unlock(&ztest_name_lock);
a08ee875 5571 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5572 return;
5573 }
5574
cae5b340
AX
5575 dmu_objset_stats_t dds;
5576 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5577 dmu_objset_fast_stat(os, &dds);
5578 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5579
428870ff
BB
5580 object = od[0].od_object;
5581 blocksize = od[0].od_blocksize;
cae5b340 5582 pattern = zs->zs_guid ^ dds.dds_guid;
428870ff
BB
5583
5584 ASSERT(object != 0);
5585
5586 tx = dmu_tx_create(os);
5587 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5588 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5589 if (txg == 0) {
ea04106b 5590 (void) rw_unlock(&ztest_name_lock);
a08ee875 5591 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5592 return;
5593 }
34dc7c2f
BB
5594
5595 /*
428870ff 5596 * Write all the copies of our block.
34dc7c2f 5597 */
d6320ddb 5598 for (i = 0; i < copies; i++) {
428870ff 5599 uint64_t offset = i * blocksize;
a08ee875
LG
5600 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5601 DMU_READ_NO_PREFETCH);
5602 if (error != 0) {
5603 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5604 os, (long long)object, (long long) offset, error);
5605 }
428870ff
BB
5606 ASSERT(db->db_offset == offset);
5607 ASSERT(db->db_size == blocksize);
5608 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5609 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5610 dmu_buf_will_fill(db, tx);
5611 ztest_pattern_set(db->db_data, db->db_size, pattern);
5612 dmu_buf_rele(db, FTAG);
5613 }
34dc7c2f 5614
428870ff
BB
5615 dmu_tx_commit(tx);
5616 txg_wait_synced(spa_get_dsl(spa), txg);
34dc7c2f
BB
5617
5618 /*
428870ff 5619 * Find out what block we got.
34dc7c2f 5620 */
a08ee875
LG
5621 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5622 DMU_READ_NO_PREFETCH));
428870ff
BB
5623 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5624 dmu_buf_rele(db, FTAG);
34dc7c2f
BB
5625
5626 /*
428870ff 5627 * Damage the block. Dedup-ditto will save us when we read it later.
34dc7c2f 5628 */
428870ff 5629 psize = BP_GET_PSIZE(&blk);
cae5b340
AX
5630 abd = abd_alloc_linear(psize, B_TRUE);
5631 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
34dc7c2f 5632
428870ff 5633 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
cae5b340 5634 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
428870ff 5635 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
34dc7c2f 5636
cae5b340 5637 abd_free(abd);
34dc7c2f 5638
ea04106b 5639 (void) rw_unlock(&ztest_name_lock);
a08ee875 5640 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
5641}
5642
34dc7c2f 5643/*
428870ff 5644 * Scrub the pool.
34dc7c2f 5645 */
428870ff
BB
5646/* ARGSUSED */
5647void
5648ztest_scrub(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5649{
c242c188 5650 spa_t *spa = ztest_spa;
34dc7c2f 5651
428870ff
BB
5652 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5653 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5654 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5655}
34dc7c2f 5656
3541dc6d
GA
5657/*
5658 * Change the guid for the pool.
5659 */
5660/* ARGSUSED */
5661void
5662ztest_reguid(ztest_ds_t *zd, uint64_t id)
5663{
c242c188 5664 spa_t *spa = ztest_spa;
3541dc6d 5665 uint64_t orig, load;
3bc7e0fb 5666 int error;
3541dc6d 5667
6b763916
AX
5668 if (ztest_opts.zo_mmp_test)
5669 return;
5670
3541dc6d
GA
5671 orig = spa_guid(spa);
5672 load = spa_load_guid(spa);
3bc7e0fb 5673
ea04106b 5674 (void) rw_wrlock(&ztest_name_lock);
3bc7e0fb 5675 error = spa_change_guid(spa);
ea04106b 5676 (void) rw_unlock(&ztest_name_lock);
3bc7e0fb
GW
5677
5678 if (error != 0)
3541dc6d
GA
5679 return;
5680
ea0b2538 5681 if (ztest_opts.zo_verbose >= 4) {
3541dc6d
GA
5682 (void) printf("Changed guid old %llu -> %llu\n",
5683 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5684 }
5685
5686 VERIFY3U(orig, !=, spa_guid(spa));
5687 VERIFY3U(load, ==, spa_load_guid(spa));
5688}
5689
428870ff
BB
5690/*
5691 * Rename the pool to a different name and then rename it back.
5692 */
5693/* ARGSUSED */
5694void
5695ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5696{
428870ff
BB
5697 char *oldname, *newname;
5698 spa_t *spa;
34dc7c2f 5699
cae5b340
AX
5700 if (ztest_opts.zo_mmp_test)
5701 return;
5702
ea04106b 5703 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5704
c242c188 5705 oldname = ztest_opts.zo_pool;
428870ff
BB
5706 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5707 (void) strcpy(newname, oldname);
5708 (void) strcat(newname, "_tmp");
34dc7c2f
BB
5709
5710 /*
428870ff 5711 * Do the rename
34dc7c2f 5712 */
428870ff 5713 VERIFY3U(0, ==, spa_rename(oldname, newname));
34dc7c2f
BB
5714
5715 /*
428870ff 5716 * Try to open it under the old name, which shouldn't exist
34dc7c2f 5717 */
428870ff 5718 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
5719
5720 /*
428870ff
BB
5721 * Open it under the new name and make sure it's still the same spa_t.
5722 */
5723 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f 5724
c242c188 5725 ASSERT(spa == ztest_spa);
428870ff 5726 spa_close(spa, FTAG);
34dc7c2f
BB
5727
5728 /*
428870ff 5729 * Rename it back to the original
34dc7c2f 5730 */
428870ff 5731 VERIFY3U(0, ==, spa_rename(newname, oldname));
34dc7c2f 5732
428870ff
BB
5733 /*
5734 * Make sure it can still be opened
5735 */
5736 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 5737
c242c188 5738 ASSERT(spa == ztest_spa);
428870ff
BB
5739 spa_close(spa, FTAG);
5740
5741 umem_free(newname, strlen(newname) + 1);
5742
ea04106b 5743 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5744}
5745
cae5b340
AX
5746void
5747ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5748{
5749 hrtime_t end = gethrtime() + NANOSEC;
5750
5751 while (gethrtime() <= end) {
5752 int run_count = 100;
5753 void *buf;
5754 struct abd *abd_data, *abd_meta;
5755 uint32_t size;
5756 int *ptr;
5757 int i;
5758 zio_cksum_t zc_ref;
5759 zio_cksum_t zc_ref_byteswap;
5760
5761 size = ztest_random_blocksize();
5762
5763 buf = umem_alloc(size, UMEM_NOFAIL);
5764 abd_data = abd_alloc(size, B_FALSE);
5765 abd_meta = abd_alloc(size, B_TRUE);
5766
5767 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5768 *ptr = ztest_random(UINT_MAX);
5769
5770 abd_copy_from_buf_off(abd_data, buf, 0, size);
5771 abd_copy_from_buf_off(abd_meta, buf, 0, size);
5772
5773 VERIFY0(fletcher_4_impl_set("scalar"));
5774 fletcher_4_native(buf, size, NULL, &zc_ref);
5775 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
5776
5777 VERIFY0(fletcher_4_impl_set("cycle"));
5778 while (run_count-- > 0) {
5779 zio_cksum_t zc;
5780 zio_cksum_t zc_byteswap;
5781
5782 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5783 fletcher_4_native(buf, size, NULL, &zc);
5784
5785 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5786 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5787 sizeof (zc_byteswap)));
5788
5789 /* Test ABD - data */
5790 abd_fletcher_4_byteswap(abd_data, size, NULL,
5791 &zc_byteswap);
5792 abd_fletcher_4_native(abd_data, size, NULL, &zc);
5793
5794 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5795 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5796 sizeof (zc_byteswap)));
5797
5798 /* Test ABD - metadata */
5799 abd_fletcher_4_byteswap(abd_meta, size, NULL,
5800 &zc_byteswap);
5801 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
5802
5803 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5804 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5805 sizeof (zc_byteswap)));
5806
5807 }
5808
5809 umem_free(buf, size);
5810 abd_free(abd_data);
5811 abd_free(abd_meta);
5812 }
5813}
5814
5815void
5816ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5817{
5818 void *buf;
5819 size_t size;
5820 int *ptr;
5821 int i;
5822 zio_cksum_t zc_ref;
5823 zio_cksum_t zc_ref_bswap;
5824
5825 hrtime_t end = gethrtime() + NANOSEC;
5826
5827 while (gethrtime() <= end) {
5828 int run_count = 100;
5829
5830 size = ztest_random_blocksize();
5831 buf = umem_alloc(size, UMEM_NOFAIL);
5832
5833 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5834 *ptr = ztest_random(UINT_MAX);
5835
5836 VERIFY0(fletcher_4_impl_set("scalar"));
5837 fletcher_4_native(buf, size, NULL, &zc_ref);
5838 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5839
5840 VERIFY0(fletcher_4_impl_set("cycle"));
5841
5842 while (run_count-- > 0) {
5843 zio_cksum_t zc;
5844 zio_cksum_t zc_bswap;
5845 size_t pos = 0;
5846
5847 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5848 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5849
5850 while (pos < size) {
5851 size_t inc = 64 * ztest_random(size / 67);
5852 /* sometimes add few bytes to test non-simd */
5853 if (ztest_random(100) < 10)
5854 inc += P2ALIGN(ztest_random(64),
5855 sizeof (uint32_t));
5856
5857 if (inc > (size - pos))
5858 inc = size - pos;
5859
5860 fletcher_4_incremental_native(buf + pos, inc,
5861 &zc);
5862 fletcher_4_incremental_byteswap(buf + pos, inc,
5863 &zc_bswap);
5864
5865 pos += inc;
5866 }
5867
5868 VERIFY3U(pos, ==, size);
5869
5870 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5871 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5872
5873 /*
5874 * verify if incremental on the whole buffer is
5875 * equivalent to non-incremental version
5876 */
5877 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5878 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5879
5880 fletcher_4_incremental_native(buf, size, &zc);
5881 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
5882
5883 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5884 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5885 }
5886
5887 umem_free(buf, size);
5888 }
5889}
5890
5891static int
5892ztest_check_path(char *path)
5893{
5894 struct stat s;
5895 /* return true on success */
5896 return (!stat(path, &s));
5897}
5898
5899static void
5900ztest_get_zdb_bin(char *bin, int len)
5901{
5902 char *zdb_path;
5903 /*
5904 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5905 * let popen to search through PATH.
5906 */
5907 if ((zdb_path = getenv("ZDB_PATH"))) {
5908 strlcpy(bin, zdb_path, len); /* In env */
5909 if (!ztest_check_path(bin)) {
5910 ztest_dump_core = 0;
5911 fatal(1, "invalid ZDB_PATH '%s'", bin);
5912 }
5913 return;
5914 }
5915
5916 VERIFY(realpath(getexecname(), bin) != NULL);
5917 if (strstr(bin, "/ztest/")) {
5918 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5919 strcat(bin, "/zdb/zdb");
5920 if (ztest_check_path(bin))
5921 return;
5922 }
5923 strcpy(bin, "zdb");
5924}
5925
428870ff
BB
5926/*
5927 * Verify pool integrity by running zdb.
5928 */
34dc7c2f 5929static void
428870ff 5930ztest_run_zdb(char *pool)
34dc7c2f
BB
5931{
5932 int status;
34dc7c2f 5933 char *bin;
0e8d1b2d
BB
5934 char *zdb;
5935 char *zbuf;
cae5b340 5936 const int len = MAXPATHLEN + MAXNAMELEN + 20;
34dc7c2f
BB
5937 FILE *fp;
5938
cae5b340
AX
5939 bin = umem_alloc(len, UMEM_NOFAIL);
5940 zdb = umem_alloc(len, UMEM_NOFAIL);
0e8d1b2d 5941 zbuf = umem_alloc(1024, UMEM_NOFAIL);
34dc7c2f 5942
cae5b340 5943 ztest_get_zdb_bin(bin, len);
0e8d1b2d
BB
5944
5945 (void) sprintf(zdb,
cae5b340 5946 "%s -bcc%s%s -G -d -U %s %s",
0e8d1b2d 5947 bin,
c242c188
CS
5948 ztest_opts.zo_verbose >= 3 ? "s" : "",
5949 ztest_opts.zo_verbose >= 4 ? "v" : "",
428870ff 5950 spa_config_path,
b128c09f 5951 pool);
34dc7c2f 5952
c242c188 5953 if (ztest_opts.zo_verbose >= 5)
34dc7c2f
BB
5954 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5955
5956 fp = popen(zdb, "r");
5957
6aec1cd5 5958 while (fgets(zbuf, 1024, fp) != NULL)
c242c188 5959 if (ztest_opts.zo_verbose >= 3)
34dc7c2f
BB
5960 (void) printf("%s", zbuf);
5961
5962 status = pclose(fp);
5963
5964 if (status == 0)
0e8d1b2d 5965 goto out;
34dc7c2f
BB
5966
5967 ztest_dump_core = 0;
5968 if (WIFEXITED(status))
5969 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5970 else
5971 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
0e8d1b2d 5972out:
cae5b340
AX
5973 umem_free(bin, len);
5974 umem_free(zdb, len);
0e8d1b2d 5975 umem_free(zbuf, 1024);
34dc7c2f
BB
5976}
5977
5978static void
5979ztest_walk_pool_directory(char *header)
5980{
5981 spa_t *spa = NULL;
5982
c242c188 5983 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5984 (void) printf("%s\n", header);
5985
5986 mutex_enter(&spa_namespace_lock);
5987 while ((spa = spa_next(spa)) != NULL)
c242c188 5988 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5989 (void) printf("\t%s\n", spa_name(spa));
5990 mutex_exit(&spa_namespace_lock);
5991}
5992
5993static void
5994ztest_spa_import_export(char *oldname, char *newname)
5995{
fb5f0bc8 5996 nvlist_t *config, *newconfig;
34dc7c2f
BB
5997 uint64_t pool_guid;
5998 spa_t *spa;
a08ee875 5999 int error;
34dc7c2f 6000
c242c188 6001 if (ztest_opts.zo_verbose >= 4) {
34dc7c2f
BB
6002 (void) printf("import/export: old = %s, new = %s\n",
6003 oldname, newname);
6004 }
6005
6006 /*
6007 * Clean up from previous runs.
6008 */
6009 (void) spa_destroy(newname);
6010
6011 /*
6012 * Get the pool's configuration and guid.
6013 */
428870ff 6014 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 6015
fb5f0bc8
BB
6016 /*
6017 * Kick off a scrub to tickle scrub/export races.
6018 */
6019 if (ztest_random(2) == 0)
428870ff 6020 (void) spa_scan(spa, POOL_SCAN_SCRUB);
fb5f0bc8 6021
34dc7c2f
BB
6022 pool_guid = spa_guid(spa);
6023 spa_close(spa, FTAG);
6024
6025 ztest_walk_pool_directory("pools before export");
6026
6027 /*
6028 * Export it.
6029 */
428870ff 6030 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
34dc7c2f
BB
6031
6032 ztest_walk_pool_directory("pools after export");
6033
fb5f0bc8
BB
6034 /*
6035 * Try to import it.
6036 */
6037 newconfig = spa_tryimport(config);
6038 ASSERT(newconfig != NULL);
6039 nvlist_free(newconfig);
6040
34dc7c2f
BB
6041 /*
6042 * Import it under the new name.
6043 */
a08ee875
LG
6044 error = spa_import(newname, config, NULL, 0);
6045 if (error != 0) {
6046 dump_nvlist(config, 0);
6047 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6048 oldname, newname, error);
6049 }
34dc7c2f
BB
6050
6051 ztest_walk_pool_directory("pools after import");
6052
6053 /*
6054 * Try to import it again -- should fail with EEXIST.
6055 */
572e2857 6056 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
34dc7c2f
BB
6057
6058 /*
6059 * Try to import it under a different name -- should fail with EEXIST.
6060 */
572e2857 6061 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
34dc7c2f
BB
6062
6063 /*
6064 * Verify that the pool is no longer visible under the old name.
6065 */
428870ff 6066 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
6067
6068 /*
6069 * Verify that we can open and close the pool using the new name.
6070 */
428870ff 6071 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f
BB
6072 ASSERT(pool_guid == spa_guid(spa));
6073 spa_close(spa, FTAG);
6074
6075 nvlist_free(config);
6076}
6077
fb5f0bc8
BB
6078static void
6079ztest_resume(spa_t *spa)
6080{
c242c188 6081 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
428870ff
BB
6082 (void) printf("resuming from suspended state\n");
6083 spa_vdev_state_enter(spa, SCL_NONE);
6084 vdev_clear(spa, NULL);
6085 (void) spa_vdev_state_exit(spa, NULL, 0);
6086 (void) zio_resume(spa);
fb5f0bc8
BB
6087}
6088
34dc7c2f 6089static void *
fb5f0bc8 6090ztest_resume_thread(void *arg)
34dc7c2f 6091{
b128c09f 6092 spa_t *spa = arg;
34dc7c2f
BB
6093
6094 while (!ztest_exiting) {
428870ff
BB
6095 if (spa_suspended(spa))
6096 ztest_resume(spa);
6097 (void) poll(NULL, 0, 100);
cae5b340
AX
6098
6099 /*
6100 * Periodically change the zfs_compressed_arc_enabled setting.
6101 */
6102 if (ztest_random(10) == 0)
6103 zfs_compressed_arc_enabled = ztest_random(2);
6104
6105 /*
6106 * Periodically change the zfs_abd_scatter_enabled setting.
6107 */
6108 if (ztest_random(10) == 0)
6109 zfs_abd_scatter_enabled = ztest_random(2);
34dc7c2f 6110 }
34dc7c2f 6111
1e33ac1e 6112 thread_exit();
428870ff 6113
1e33ac1e
BB
6114 return (NULL);
6115}
428870ff 6116
a08ee875 6117#define GRACE 300
428870ff 6118
26099167 6119#if 0
1e33ac1e
BB
6120static void
6121ztest_deadman_alarm(int sig)
6122{
6123 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
428870ff 6124}
26099167 6125#endif
428870ff
BB
6126
6127static void
c242c188 6128ztest_execute(int test, ztest_info_t *zi, uint64_t id)
428870ff 6129{
c242c188
CS
6130 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6131 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
428870ff 6132 hrtime_t functime = gethrtime();
d6320ddb 6133 int i;
428870ff 6134
d6320ddb 6135 for (i = 0; i < zi->zi_iters; i++)
428870ff
BB
6136 zi->zi_func(zd, id);
6137
6138 functime = gethrtime() - functime;
6139
c242c188
CS
6140 atomic_add_64(&zc->zc_count, 1);
6141 atomic_add_64(&zc->zc_time, functime);
428870ff 6142
ea04106b 6143 if (ztest_opts.zo_verbose >= 4)
428870ff 6144 (void) printf("%6.2f sec in %s\n",
ea04106b 6145 (double)functime / NANOSEC, zi->zi_funcname);
428870ff
BB
6146}
6147
34dc7c2f
BB
6148static void *
6149ztest_thread(void *arg)
6150{
c242c188 6151 int rand;
428870ff 6152 uint64_t id = (uintptr_t)arg;
34dc7c2f 6153 ztest_shared_t *zs = ztest_shared;
428870ff
BB
6154 uint64_t call_next;
6155 hrtime_t now;
34dc7c2f 6156 ztest_info_t *zi;
c242c188 6157 ztest_shared_callstate_t *zc;
34dc7c2f 6158
428870ff 6159 while ((now = gethrtime()) < zs->zs_thread_stop) {
34dc7c2f
BB
6160 /*
6161 * See if it's time to force a crash.
6162 */
428870ff
BB
6163 if (now > zs->zs_thread_kill)
6164 ztest_kill(zs);
34dc7c2f
BB
6165
6166 /*
428870ff 6167 * If we're getting ENOSPC with some regularity, stop.
34dc7c2f 6168 */
428870ff
BB
6169 if (zs->zs_enospc_count > 10)
6170 break;
34dc7c2f
BB
6171
6172 /*
428870ff 6173 * Pick a random function to execute.
34dc7c2f 6174 */
c242c188
CS
6175 rand = ztest_random(ZTEST_FUNCS);
6176 zi = &ztest_info[rand];
6177 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6178 call_next = zc->zc_next;
34dc7c2f 6179
428870ff 6180 if (now >= call_next &&
c242c188
CS
6181 atomic_cas_64(&zc->zc_next, call_next, call_next +
6182 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6183 ztest_execute(rand, zi, id);
6184 }
428870ff 6185 }
34dc7c2f 6186
1e33ac1e
BB
6187 thread_exit();
6188
428870ff
BB
6189 return (NULL);
6190}
34dc7c2f 6191
428870ff
BB
6192static void
6193ztest_dataset_name(char *dsname, char *pool, int d)
6194{
cae5b340 6195 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
428870ff 6196}
34dc7c2f 6197
428870ff 6198static void
c242c188 6199ztest_dataset_destroy(int d)
428870ff 6200{
cae5b340 6201 char name[ZFS_MAX_DATASET_NAME_LEN];
d6320ddb 6202 int t;
34dc7c2f 6203
c242c188 6204 ztest_dataset_name(name, ztest_opts.zo_pool, d);
34dc7c2f 6205
c242c188 6206 if (ztest_opts.zo_verbose >= 3)
428870ff 6207 (void) printf("Destroying %s to free up space\n", name);
34dc7c2f 6208
428870ff
BB
6209 /*
6210 * Cleanup any non-standard clones and snapshots. In general,
6211 * ztest thread t operates on dataset (t % zopt_datasets),
6212 * so there may be more than one thing to clean up.
6213 */
c242c188
CS
6214 for (t = d; t < ztest_opts.zo_threads;
6215 t += ztest_opts.zo_datasets)
428870ff
BB
6216 ztest_dsl_dataset_cleanup(name, t);
6217
6218 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6219 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6220}
6221
6222static void
6223ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6224{
6225 uint64_t usedobjs, dirobjs, scratch;
6226
6227 /*
6228 * ZTEST_DIROBJ is the object directory for the entire dataset.
6229 * Therefore, the number of objects in use should equal the
6230 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6231 * If not, we have an object leak.
6232 *
6233 * Note that we can only check this in ztest_dataset_open(),
6234 * when the open-context and syncing-context values agree.
6235 * That's because zap_count() returns the open-context value,
6236 * while dmu_objset_space() returns the rootbp fill count.
6237 */
6238 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6239 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6240 ASSERT3U(dirobjs + 1, ==, usedobjs);
6241}
6242
6243static int
c242c188 6244ztest_dataset_open(int d)
428870ff 6245{
c242c188
CS
6246 ztest_ds_t *zd = &ztest_ds[d];
6247 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
428870ff
BB
6248 objset_t *os;
6249 zilog_t *zilog;
cae5b340 6250 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
6251 int error;
6252
c242c188 6253 ztest_dataset_name(name, ztest_opts.zo_pool, d);
428870ff 6254
ea04106b 6255 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
6256
6257 error = ztest_dataset_create(name);
6258 if (error == ENOSPC) {
ea04106b 6259 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
6260 ztest_record_enospc(FTAG);
6261 return (error);
34dc7c2f 6262 }
428870ff 6263 ASSERT(error == 0 || error == EEXIST);
34dc7c2f 6264
a08ee875 6265 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
ea04106b 6266 (void) rw_unlock(&ztest_name_lock);
428870ff 6267
c242c188 6268 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
428870ff
BB
6269
6270 zilog = zd->zd_zilog;
6271
6272 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6273 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6274 fatal(0, "missing log records: claimed %llu < committed %llu",
6275 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6276
6277 ztest_dataset_dirobj_verify(zd);
6278
6279 zil_replay(os, zd, ztest_replay_vector);
6280
6281 ztest_dataset_dirobj_verify(zd);
6282
c242c188 6283 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
6284 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6285 zd->zd_name,
6286 (u_longlong_t)zilog->zl_parse_blk_count,
6287 (u_longlong_t)zilog->zl_parse_lr_count,
6288 (u_longlong_t)zilog->zl_replaying_seq);
6289
6290 zilog = zil_open(os, ztest_get_data);
6291
6292 if (zilog->zl_replaying_seq != 0 &&
6293 zilog->zl_replaying_seq < committed_seq)
6294 fatal(0, "missing log records: replayed %llu < committed %llu",
6295 zilog->zl_replaying_seq, committed_seq);
6296
6297 return (0);
6298}
6299
6300static void
c242c188 6301ztest_dataset_close(int d)
428870ff 6302{
c242c188 6303 ztest_ds_t *zd = &ztest_ds[d];
428870ff
BB
6304
6305 zil_close(zd->zd_zilog);
a08ee875 6306 dmu_objset_disown(zd->zd_os, zd);
428870ff
BB
6307
6308 ztest_zd_fini(zd);
34dc7c2f
BB
6309}
6310
6311/*
6312 * Kick off threads to run tests on all datasets in parallel.
6313 */
6314static void
428870ff 6315ztest_run(ztest_shared_t *zs)
34dc7c2f 6316{
1e33ac1e 6317 kt_did_t *tid;
34dc7c2f 6318 spa_t *spa;
3541dc6d 6319 objset_t *os;
1e33ac1e
BB
6320 kthread_t *resume_thread;
6321 uint64_t object;
428870ff 6322 int error;
d6320ddb 6323 int t, d;
b128c09f
BB
6324
6325 ztest_exiting = B_FALSE;
34dc7c2f 6326
34dc7c2f 6327 /*
428870ff 6328 * Initialize parent/child shared state.
34dc7c2f 6329 */
c242c188 6330 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
ea04106b 6331 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f 6332
428870ff 6333 zs->zs_thread_start = gethrtime();
c242c188
CS
6334 zs->zs_thread_stop =
6335 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
428870ff
BB
6336 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6337 zs->zs_thread_kill = zs->zs_thread_stop;
c242c188
CS
6338 if (ztest_random(100) < ztest_opts.zo_killrate) {
6339 zs->zs_thread_kill -=
6340 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6341 }
34dc7c2f 6342
1e33ac1e 6343 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 6344
428870ff
BB
6345 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6346 offsetof(ztest_cb_data_t, zcd_node));
34dc7c2f
BB
6347
6348 /*
b128c09f 6349 * Open our pool.
34dc7c2f 6350 */
428870ff 6351 kernel_init(FREAD | FWRITE);
a08ee875 6352 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6d974228 6353 spa->spa_debug = B_TRUE;
ea04106b 6354 metaslab_preload_limit = ztest_random(20) + 1;
c242c188 6355 ztest_spa = spa;
428870ff 6356
cae5b340 6357 dmu_objset_stats_t dds;
a08ee875
LG
6358 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6359 DMU_OST_ANY, B_TRUE, FTAG, &os));
cae5b340
AX
6360 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6361 dmu_objset_fast_stat(os, &dds);
6362 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6363 zs->zs_guid = dds.dds_guid;
a08ee875 6364 dmu_objset_disown(os, FTAG);
3541dc6d 6365
428870ff 6366 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
34dc7c2f 6367
fb5f0bc8
BB
6368 /*
6369 * We don't expect the pool to suspend unless maxfaults == 0,
6370 * in which case ztest_fault_inject() temporarily takes away
6371 * the only valid replica.
6372 */
428870ff 6373 if (MAXFAULTS() == 0)
fb5f0bc8
BB
6374 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6375 else
6376 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6377
34dc7c2f 6378 /*
b128c09f 6379 * Create a thread to periodically resume suspended I/O.
34dc7c2f 6380 */
0aebd4f9 6381 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
cae5b340 6382 (thread_func_t)ztest_resume_thread, spa, 0, NULL, TS_RUN, 0,
0aebd4f9 6383 PTHREAD_CREATE_JOINABLE)), !=, NULL);
34dc7c2f 6384
26099167 6385#if 0
428870ff 6386 /*
1e33ac1e 6387 * Set a deadman alarm to abort() if we hang.
428870ff 6388 */
1e33ac1e
BB
6389 signal(SIGALRM, ztest_deadman_alarm);
6390 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
26099167 6391#endif
428870ff 6392
34dc7c2f
BB
6393 /*
6394 * Verify that we can safely inquire about about any object,
6395 * whether it's allocated or not. To make it interesting,
6396 * we probe a 5-wide window around each power of two.
6397 * This hits all edge cases, including zero and the max.
6398 */
d6320ddb
BB
6399 for (t = 0; t < 64; t++) {
6400 for (d = -5; d <= 5; d++) {
34dc7c2f
BB
6401 error = dmu_object_info(spa->spa_meta_objset,
6402 (1ULL << t) + d, NULL);
6403 ASSERT(error == 0 || error == ENOENT ||
6404 error == EINVAL);
6405 }
6406 }
6407
6408 /*
428870ff 6409 * If we got any ENOSPC errors on the previous run, destroy something.
34dc7c2f 6410 */
428870ff 6411 if (zs->zs_enospc_count != 0) {
c242c188
CS
6412 int d = ztest_random(ztest_opts.zo_datasets);
6413 ztest_dataset_destroy(d);
428870ff 6414 }
34dc7c2f
BB
6415 zs->zs_enospc_count = 0;
6416
c242c188
CS
6417 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6418 UMEM_NOFAIL);
34dc7c2f 6419
c242c188 6420 if (ztest_opts.zo_verbose >= 4)
34dc7c2f
BB
6421 (void) printf("starting main threads...\n");
6422
428870ff
BB
6423 /*
6424 * Kick off all the tests that run in parallel.
6425 */
c242c188 6426 for (t = 0; t < ztest_opts.zo_threads; t++) {
1e33ac1e
BB
6427 kthread_t *thread;
6428
c242c188 6429 if (t < ztest_opts.zo_datasets &&
cae5b340
AX
6430 ztest_dataset_open(t) != 0) {
6431 umem_free(tid,
6432 ztest_opts.zo_threads * sizeof (kt_did_t));
428870ff 6433 return;
cae5b340 6434 }
1e33ac1e 6435
0aebd4f9
ED
6436 VERIFY3P(thread = zk_thread_create(NULL, 0,
6437 (thread_func_t)ztest_thread,
cae5b340 6438 (void *)(uintptr_t)t, 0, NULL, TS_RUN, 0,
0aebd4f9 6439 PTHREAD_CREATE_JOINABLE), !=, NULL);
1e33ac1e 6440 tid[t] = thread->t_tid;
34dc7c2f
BB
6441 }
6442
428870ff
BB
6443 /*
6444 * Wait for all of the tests to complete. We go in reverse order
6445 * so we don't close datasets while threads are still using them.
6446 */
c242c188 6447 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
1e33ac1e 6448 thread_join(tid[t]);
c242c188
CS
6449 if (t < ztest_opts.zo_datasets)
6450 ztest_dataset_close(t);
34dc7c2f
BB
6451 }
6452
34dc7c2f
BB
6453 txg_wait_synced(spa_get_dsl(spa), 0);
6454
428870ff
BB
6455 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6456 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6457
c242c188 6458 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
428870ff
BB
6459
6460 /* Kill the resume thread */
6461 ztest_exiting = B_TRUE;
1e33ac1e 6462 thread_join(resume_thread->t_tid);
428870ff 6463 ztest_resume(spa);
34dc7c2f
BB
6464
6465 /*
428870ff
BB
6466 * Right before closing the pool, kick off a bunch of async I/O;
6467 * spa_close() should wait for it to complete.
34dc7c2f 6468 */
cae5b340
AX
6469 for (object = 1; object < 50; object++) {
6470 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6471 ZIO_PRIORITY_SYNC_READ);
6472 }
428870ff 6473
090ff092
RC
6474 /* Verify that at least one commit cb was called in a timely fashion */
6475 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
c06d4368 6476 VERIFY0(zc_min_txg_delay);
090ff092 6477
428870ff
BB
6478 spa_close(spa, FTAG);
6479
6480 /*
6481 * Verify that we can loop over all pools.
6482 */
6483 mutex_enter(&spa_namespace_lock);
6484 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
c242c188 6485 if (ztest_opts.zo_verbose > 3)
428870ff
BB
6486 (void) printf("spa_next: found %s\n", spa_name(spa));
6487 mutex_exit(&spa_namespace_lock);
6488
6489 /*
6490 * Verify that we can export the pool and reimport it under a
6491 * different name.
6492 */
cae5b340
AX
6493 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6494 char name[ZFS_MAX_DATASET_NAME_LEN];
6495 (void) snprintf(name, sizeof (name), "%s_import",
c242c188
CS
6496 ztest_opts.zo_pool);
6497 ztest_spa_import_export(ztest_opts.zo_pool, name);
6498 ztest_spa_import_export(name, ztest_opts.zo_pool);
428870ff
BB
6499 }
6500
6501 kernel_fini();
572e2857
BB
6502
6503 list_destroy(&zcl.zcl_callbacks);
0e8d1b2d 6504 mutex_destroy(&zcl.zcl_callbacks_lock);
ea04106b 6505 (void) rwlock_destroy(&ztest_name_lock);
c242c188 6506 mutex_destroy(&ztest_vdev_lock);
428870ff
BB
6507}
6508
6509static void
c242c188 6510ztest_freeze(void)
428870ff 6511{
c242c188 6512 ztest_ds_t *zd = &ztest_ds[0];
428870ff
BB
6513 spa_t *spa;
6514 int numloops = 0;
6515
c242c188 6516 if (ztest_opts.zo_verbose >= 3)
428870ff 6517 (void) printf("testing spa_freeze()...\n");
9babb374 6518
428870ff 6519 kernel_init(FREAD | FWRITE);
c242c188
CS
6520 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6521 VERIFY3U(0, ==, ztest_dataset_open(0));
a08ee875
LG
6522 spa->spa_debug = B_TRUE;
6523 ztest_spa = spa;
9babb374 6524
428870ff
BB
6525 /*
6526 * Force the first log block to be transactionally allocated.
6527 * We have to do this before we freeze the pool -- otherwise
6528 * the log chain won't be anchored.
6529 */
6530 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6531 ztest_dmu_object_alloc_free(zd, 0);
572e2857 6532 zil_commit(zd->zd_zilog, 0);
34dc7c2f
BB
6533 }
6534
6535 txg_wait_synced(spa_get_dsl(spa), 0);
6536
428870ff
BB
6537 /*
6538 * Freeze the pool. This stops spa_sync() from doing anything,
6539 * so that the only way to record changes from now on is the ZIL.
6540 */
6541 spa_freeze(spa);
b128c09f 6542
ea04106b
AX
6543 /*
6544 * Because it is hard to predict how much space a write will actually
6545 * require beforehand, we leave ourselves some fudge space to write over
6546 * capacity.
6547 */
6548 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6549
428870ff
BB
6550 /*
6551 * Run tests that generate log records but don't alter the pool config
6552 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6553 * We do a txg_wait_synced() after each iteration to force the txg
6554 * to increase well beyond the last synced value in the uberblock.
6555 * The ZIL should be OK with that.
ea04106b
AX
6556 *
6557 * Run a random number of times less than zo_maxloops and ensure we do
6558 * not run out of space on the pool.
428870ff 6559 */
c242c188 6560 while (ztest_random(10) != 0 &&
ea04106b
AX
6561 numloops++ < ztest_opts.zo_maxloops &&
6562 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6563 ztest_od_t od;
cae5b340 6564 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
ea04106b
AX
6565 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6566 ztest_io(zd, od.od_object,
6567 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
428870ff
BB
6568 txg_wait_synced(spa_get_dsl(spa), 0);
6569 }
b128c09f 6570
34dc7c2f 6571 /*
428870ff 6572 * Commit all of the changes we just generated.
34dc7c2f 6573 */
572e2857 6574 zil_commit(zd->zd_zilog, 0);
428870ff 6575 txg_wait_synced(spa_get_dsl(spa), 0);
34dc7c2f 6576
428870ff
BB
6577 /*
6578 * Close our dataset and close the pool.
6579 */
c242c188 6580 ztest_dataset_close(0);
34dc7c2f 6581 spa_close(spa, FTAG);
428870ff 6582 kernel_fini();
34dc7c2f 6583
428870ff
BB
6584 /*
6585 * Open and close the pool and dataset to induce log replay.
6586 */
6587 kernel_init(FREAD | FWRITE);
c242c188 6588 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
29809a6c 6589 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
c242c188
CS
6590 VERIFY3U(0, ==, ztest_dataset_open(0));
6591 ztest_dataset_close(0);
3bc7e0fb
GW
6592
6593 spa->spa_debug = B_TRUE;
6594 ztest_spa = spa;
6595 txg_wait_synced(spa_get_dsl(spa), 0);
6596 ztest_reguid(NULL, 0);
6597
428870ff 6598 spa_close(spa, FTAG);
34dc7c2f
BB
6599 kernel_fini();
6600}
6601
6602void
6603print_time(hrtime_t t, char *timebuf)
6604{
6605 hrtime_t s = t / NANOSEC;
6606 hrtime_t m = s / 60;
6607 hrtime_t h = m / 60;
6608 hrtime_t d = h / 24;
6609
6610 s -= m * 60;
6611 m -= h * 60;
6612 h -= d * 24;
6613
6614 timebuf[0] = '\0';
6615
6616 if (d)
6617 (void) sprintf(timebuf,
6618 "%llud%02lluh%02llum%02llus", d, h, m, s);
6619 else if (h)
6620 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6621 else if (m)
6622 (void) sprintf(timebuf, "%llum%02llus", m, s);
6623 else
6624 (void) sprintf(timebuf, "%llus", s);
6625}
6626
428870ff 6627static nvlist_t *
0bc8fd78 6628make_random_props(void)
428870ff
BB
6629{
6630 nvlist_t *props;
6631
428870ff 6632 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
9ae529ec
CS
6633 if (ztest_random(2) == 0)
6634 return (props);
428870ff
BB
6635 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6636
428870ff
BB
6637 return (props);
6638}
6639
cae5b340
AX
6640/*
6641 * Import a storage pool with the given name.
6642 */
6643static void
6644ztest_import(ztest_shared_t *zs)
6645{
6646 libzfs_handle_t *hdl;
6647 importargs_t args = { 0 };
6648 spa_t *spa;
6649 nvlist_t *cfg = NULL;
6650 int nsearch = 1;
6651 char *searchdirs[nsearch];
6652 char *name = ztest_opts.zo_pool;
6653 int flags = ZFS_IMPORT_MISSING_LOG;
6654 int error;
6655
6656 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6657 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6658
6659 kernel_init(FREAD | FWRITE);
6660 hdl = libzfs_init();
6661
6662 searchdirs[0] = ztest_opts.zo_dir;
6663 args.paths = nsearch;
6664 args.path = searchdirs;
6665 args.can_be_active = B_FALSE;
6666
6667 error = zpool_tryimport(hdl, name, &cfg, &args);
6668 if (error)
6669 (void) fatal(0, "No pools found\n");
6670
6671 VERIFY0(spa_import(name, cfg, NULL, flags));
6672 VERIFY0(spa_open(name, &spa, FTAG));
6673 zs->zs_metaslab_sz =
6674 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6675 spa_close(spa, FTAG);
6676
6677 libzfs_fini(hdl);
6678 kernel_fini();
6679
6680 if (!ztest_opts.zo_mmp_test) {
6681 ztest_run_zdb(ztest_opts.zo_pool);
6682 ztest_freeze();
6683 ztest_run_zdb(ztest_opts.zo_pool);
6684 }
6685
6686 (void) rwlock_destroy(&ztest_name_lock);
6687 mutex_destroy(&ztest_vdev_lock);
6688}
6689
34dc7c2f
BB
6690/*
6691 * Create a storage pool with the given name and initial vdev size.
428870ff 6692 * Then test spa_freeze() functionality.
34dc7c2f
BB
6693 */
6694static void
428870ff 6695ztest_init(ztest_shared_t *zs)
34dc7c2f
BB
6696{
6697 spa_t *spa;
428870ff 6698 nvlist_t *nvroot, *props;
9ae529ec 6699 int i;
428870ff 6700
c242c188 6701 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
ea04106b 6702 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f
BB
6703
6704 kernel_init(FREAD | FWRITE);
6705
6706 /*
6707 * Create the storage pool.
6708 */
c242c188 6709 (void) spa_destroy(ztest_opts.zo_pool);
428870ff
BB
6710 ztest_shared->zs_vdev_next_leaf = 0;
6711 zs->zs_splits = 0;
c242c188 6712 zs->zs_mirrors = ztest_opts.zo_mirrors;
ea0b2538 6713 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
c242c188 6714 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
428870ff 6715 props = make_random_props();
9ae529ec
CS
6716 for (i = 0; i < SPA_FEATURES; i++) {
6717 char *buf;
6718 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6719 spa_feature_table[i].fi_uname));
6720 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6721 free(buf);
6722 }
a08ee875 6723 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
34dc7c2f 6724 nvlist_free(nvroot);
ea04106b 6725 nvlist_free(props);
34dc7c2f 6726
c242c188
CS
6727 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6728 zs->zs_metaslab_sz =
6729 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
34dc7c2f
BB
6730 spa_close(spa, FTAG);
6731
6732 kernel_fini();
428870ff 6733
cae5b340
AX
6734 if (!ztest_opts.zo_mmp_test) {
6735 ztest_run_zdb(ztest_opts.zo_pool);
6736 ztest_freeze();
6737 ztest_run_zdb(ztest_opts.zo_pool);
6738 }
c242c188 6739
ea04106b 6740 (void) rwlock_destroy(&ztest_name_lock);
c242c188
CS
6741 mutex_destroy(&ztest_vdev_lock);
6742}
6743
6744static void
9d81146b 6745setup_data_fd(void)
c242c188 6746{
facbbe43
BB
6747 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6748
6749 ztest_fd_data = mkstemp(ztest_name_data);
9d81146b 6750 ASSERT3S(ztest_fd_data, >=, 0);
facbbe43 6751 (void) unlink(ztest_name_data);
c242c188
CS
6752}
6753
22257dc0
CS
6754static int
6755shared_data_size(ztest_shared_hdr_t *hdr)
6756{
6757 int size;
6758
6759 size = hdr->zh_hdr_size;
6760 size += hdr->zh_opts_size;
6761 size += hdr->zh_size;
6762 size += hdr->zh_stats_size * hdr->zh_stats_count;
6763 size += hdr->zh_ds_size * hdr->zh_ds_count;
6764
6765 return (size);
6766}
6767
c242c188
CS
6768static void
6769setup_hdr(void)
6770{
22257dc0 6771 int size;
c242c188
CS
6772 ztest_shared_hdr_t *hdr;
6773
6774 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6775 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
cae5b340 6776 ASSERT(hdr != MAP_FAILED);
c242c188 6777
9d81146b 6778 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
22257dc0 6779
c242c188
CS
6780 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6781 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6782 hdr->zh_size = sizeof (ztest_shared_t);
6783 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6784 hdr->zh_stats_count = ZTEST_FUNCS;
6785 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6786 hdr->zh_ds_count = ztest_opts.zo_datasets;
6787
22257dc0 6788 size = shared_data_size(hdr);
9d81146b 6789 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
22257dc0 6790
c242c188
CS
6791 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6792}
6793
6794static void
6795setup_data(void)
6796{
6797 int size, offset;
6798 ztest_shared_hdr_t *hdr;
6799 uint8_t *buf;
6800
6801 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6802 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
cae5b340 6803 ASSERT(hdr != MAP_FAILED);
c242c188 6804
22257dc0 6805 size = shared_data_size(hdr);
c242c188
CS
6806
6807 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6808 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
9d81146b 6809 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
cae5b340 6810 ASSERT(hdr != MAP_FAILED);
c242c188
CS
6811 buf = (uint8_t *)hdr;
6812
6813 offset = hdr->zh_hdr_size;
6814 ztest_shared_opts = (void *)&buf[offset];
6815 offset += hdr->zh_opts_size;
6816 ztest_shared = (void *)&buf[offset];
6817 offset += hdr->zh_size;
6818 ztest_shared_callstate = (void *)&buf[offset];
6819 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6820 ztest_shared_ds = (void *)&buf[offset];
6821}
6822
6823static boolean_t
6824exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6825{
6826 pid_t pid;
6827 int status;
483106eb 6828 char *cmdbuf = NULL;
c242c188
CS
6829
6830 pid = fork();
6831
6832 if (cmd == NULL) {
483106eb
BB
6833 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6834 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
c242c188
CS
6835 cmd = cmdbuf;
6836 }
6837
6838 if (pid == -1)
6839 fatal(1, "fork failed");
6840
6841 if (pid == 0) { /* child */
6842 char *emptyargv[2] = { cmd, NULL };
9d81146b 6843 char fd_data_str[12];
c242c188
CS
6844
6845 struct rlimit rl = { 1024, 1024 };
6846 (void) setrlimit(RLIMIT_NOFILE, &rl);
9d81146b
ED
6847
6848 (void) close(ztest_fd_rand);
6849 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6850 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6851
c242c188
CS
6852 (void) enable_extended_FILE_stdio(-1, -1);
6853 if (libpath != NULL)
6854 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6855 (void) execv(cmd, emptyargv);
6856 ztest_dump_core = B_FALSE;
6857 fatal(B_TRUE, "exec failed: %s", cmd);
6858 }
6859
483106eb
BB
6860 if (cmdbuf != NULL) {
6861 umem_free(cmdbuf, MAXPATHLEN);
6862 cmd = NULL;
6863 }
6864
c242c188
CS
6865 while (waitpid(pid, &status, 0) != pid)
6866 continue;
6867 if (statusp != NULL)
6868 *statusp = status;
6869
6870 if (WIFEXITED(status)) {
6871 if (WEXITSTATUS(status) != 0) {
6872 (void) fprintf(stderr, "child exited with code %d\n",
6873 WEXITSTATUS(status));
6874 exit(2);
6875 }
6876 return (B_FALSE);
6877 } else if (WIFSIGNALED(status)) {
6878 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6879 (void) fprintf(stderr, "child died with signal %d\n",
6880 WTERMSIG(status));
6881 exit(3);
6882 }
6883 return (B_TRUE);
6884 } else {
6885 (void) fprintf(stderr, "something strange happened to child\n");
6886 exit(4);
6887 /* NOTREACHED */
6888 }
6889}
6890
6891static void
6892ztest_run_init(void)
6893{
6894 int i;
428870ff 6895
c242c188 6896 ztest_shared_t *zs = ztest_shared;
428870ff 6897
c242c188
CS
6898 /*
6899 * Blow away any existing copy of zpool.cache
6900 */
6901 (void) remove(spa_config_path);
6902
cae5b340
AX
6903 if (ztest_opts.zo_init == 0) {
6904 if (ztest_opts.zo_verbose >= 1)
6905 (void) printf("Importing pool %s\n",
6906 ztest_opts.zo_pool);
6907 ztest_import(zs);
6908 return;
6909 }
6910
c242c188
CS
6911 /*
6912 * Create and initialize our storage pool.
6913 */
6914 for (i = 1; i <= ztest_opts.zo_init; i++) {
6915 bzero(zs, sizeof (ztest_shared_t));
6916 if (ztest_opts.zo_verbose >= 3 &&
6917 ztest_opts.zo_init != 1) {
6918 (void) printf("ztest_init(), pass %d\n", i);
6919 }
6920 ztest_init(zs);
6921 }
34dc7c2f
BB
6922}
6923
6924int
6925main(int argc, char **argv)
6926{
6927 int kills = 0;
6928 int iters = 0;
c242c188
CS
6929 int older = 0;
6930 int newer = 0;
34dc7c2f
BB
6931 ztest_shared_t *zs;
6932 ztest_info_t *zi;
c242c188 6933 ztest_shared_callstate_t *zc;
34dc7c2f
BB
6934 char timebuf[100];
6935 char numbuf[6];
428870ff 6936 spa_t *spa;
483106eb 6937 char *cmd;
c242c188
CS
6938 boolean_t hasalt;
6939 int f;
9d81146b 6940 char *fd_data_str = getenv("ZTEST_FD_DATA");
ea04106b 6941 struct sigaction action;
34dc7c2f
BB
6942
6943 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6944
a08ee875
LG
6945 dprintf_setup(&argc, argv);
6946
ea04106b
AX
6947 action.sa_handler = sig_handler;
6948 sigemptyset(&action.sa_mask);
6949 action.sa_flags = 0;
6950
6951 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6952 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6953 strerror(errno));
6954 exit(EXIT_FAILURE);
6955 }
6956
6957 if (sigaction(SIGABRT, &action, NULL) < 0) {
6958 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6959 strerror(errno));
6960 exit(EXIT_FAILURE);
6961 }
6962
9d81146b
ED
6963 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6964 ASSERT3S(ztest_fd_rand, >=, 0);
6965
6966 if (!fd_data_str) {
c242c188 6967 process_options(argc, argv);
34dc7c2f 6968
9d81146b 6969 setup_data_fd();
c242c188
CS
6970 setup_hdr();
6971 setup_data();
6972 bcopy(&ztest_opts, ztest_shared_opts,
6973 sizeof (*ztest_shared_opts));
6974 } else {
9d81146b 6975 ztest_fd_data = atoi(fd_data_str);
c242c188
CS
6976 setup_data();
6977 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6978 }
6979 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
34dc7c2f 6980
428870ff 6981 /* Override location of zpool.cache */
5be98cfe
BB
6982 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6983 ztest_opts.zo_dir) != -1);
9d81146b 6984
c242c188
CS
6985 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6986 UMEM_NOFAIL);
6987 zs = ztest_shared;
6988
9d81146b 6989 if (fd_data_str) {
c242c188
CS
6990 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6991 metaslab_df_alloc_threshold =
6992 zs->zs_metaslab_df_alloc_threshold;
6993
6994 if (zs->zs_do_init)
6995 ztest_run_init();
6996 else
6997 ztest_run(zs);
6998 exit(0);
6999 }
34dc7c2f 7000
c242c188 7001 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
34dc7c2f 7002
c242c188 7003 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
7004 (void) printf("%llu vdevs, %d datasets, %d threads,"
7005 " %llu seconds...\n",
c242c188
CS
7006 (u_longlong_t)ztest_opts.zo_vdevs,
7007 ztest_opts.zo_datasets,
7008 ztest_opts.zo_threads,
7009 (u_longlong_t)ztest_opts.zo_time);
34dc7c2f
BB
7010 }
7011
483106eb
BB
7012 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7013 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
c242c188
CS
7014
7015 zs->zs_do_init = B_TRUE;
7016 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7017 if (ztest_opts.zo_verbose >= 1) {
7018 (void) printf("Executing older ztest for "
7019 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7020 }
7021 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7022 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7023 } else {
7024 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
34dc7c2f 7025 }
c242c188 7026 zs->zs_do_init = B_FALSE;
34dc7c2f 7027
428870ff 7028 zs->zs_proc_start = gethrtime();
c242c188 7029 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
34dc7c2f 7030
d6320ddb 7031 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7032 zi = &ztest_info[f];
7033 zc = ZTEST_GET_SHARED_CALLSTATE(f);
428870ff 7034 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
c242c188 7035 zc->zc_next = UINT64_MAX;
34dc7c2f 7036 else
c242c188 7037 zc->zc_next = zs->zs_proc_start +
428870ff 7038 ztest_random(2 * zi->zi_interval[0] + 1);
34dc7c2f
BB
7039 }
7040
34dc7c2f
BB
7041 /*
7042 * Run the tests in a loop. These tests include fault injection
7043 * to verify that self-healing data works, and forced crashes
7044 * to verify that we never lose on-disk consistency.
7045 */
428870ff 7046 while (gethrtime() < zs->zs_proc_stop) {
34dc7c2f 7047 int status;
c242c188 7048 boolean_t killed;
34dc7c2f
BB
7049
7050 /*
7051 * Initialize the workload counters for each function.
7052 */
d6320ddb 7053 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7054 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7055 zc->zc_count = 0;
7056 zc->zc_time = 0;
34dc7c2f
BB
7057 }
7058
9babb374 7059 /* Set the allocation switch size */
c242c188
CS
7060 zs->zs_metaslab_df_alloc_threshold =
7061 ztest_random(zs->zs_metaslab_sz / 4) + 1;
9babb374 7062
c242c188
CS
7063 if (!hasalt || ztest_random(2) == 0) {
7064 if (hasalt && ztest_opts.zo_verbose >= 1) {
7065 (void) printf("Executing newer ztest: %s\n",
7066 cmd);
34dc7c2f 7067 }
c242c188
CS
7068 newer++;
7069 killed = exec_child(cmd, NULL, B_TRUE, &status);
34dc7c2f 7070 } else {
c242c188
CS
7071 if (hasalt && ztest_opts.zo_verbose >= 1) {
7072 (void) printf("Executing older ztest: %s\n",
7073 ztest_opts.zo_alt_ztest);
7074 }
7075 older++;
7076 killed = exec_child(ztest_opts.zo_alt_ztest,
7077 ztest_opts.zo_alt_libpath, B_TRUE, &status);
34dc7c2f
BB
7078 }
7079
c242c188
CS
7080 if (killed)
7081 kills++;
34dc7c2f
BB
7082 iters++;
7083
c242c188 7084 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
7085 hrtime_t now = gethrtime();
7086
428870ff
BB
7087 now = MIN(now, zs->zs_proc_stop);
7088 print_time(zs->zs_proc_stop - now, timebuf);
34dc7c2f
BB
7089 nicenum(zs->zs_space, numbuf);
7090
7091 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7092 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7093 iters,
7094 WIFEXITED(status) ? "Complete" : "SIGKILL",
7095 (u_longlong_t)zs->zs_enospc_count,
7096 100.0 * zs->zs_alloc / zs->zs_space,
7097 numbuf,
428870ff 7098 100.0 * (now - zs->zs_proc_start) /
c242c188 7099 (ztest_opts.zo_time * NANOSEC), timebuf);
34dc7c2f
BB
7100 }
7101
c242c188 7102 if (ztest_opts.zo_verbose >= 2) {
34dc7c2f
BB
7103 (void) printf("\nWorkload summary:\n\n");
7104 (void) printf("%7s %9s %s\n",
7105 "Calls", "Time", "Function");
7106 (void) printf("%7s %9s %s\n",
7107 "-----", "----", "--------");
d6320ddb 7108 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7109 zi = &ztest_info[f];
7110 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7111 print_time(zc->zc_time, timebuf);
34dc7c2f 7112 (void) printf("%7llu %9s %s\n",
c242c188 7113 (u_longlong_t)zc->zc_count, timebuf,
ea04106b 7114 zi->zi_funcname);
34dc7c2f
BB
7115 }
7116 (void) printf("\n");
7117 }
7118
7119 /*
428870ff
BB
7120 * It's possible that we killed a child during a rename test,
7121 * in which case we'll have a 'ztest_tmp' pool lying around
7122 * instead of 'ztest'. Do a blind rename in case this happened.
34dc7c2f 7123 */
428870ff 7124 kernel_init(FREAD);
c242c188 7125 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
428870ff
BB
7126 spa_close(spa, FTAG);
7127 } else {
cae5b340 7128 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
7129 kernel_fini();
7130 kernel_init(FREAD | FWRITE);
7131 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
c242c188
CS
7132 ztest_opts.zo_pool);
7133 (void) spa_rename(tmpname, ztest_opts.zo_pool);
428870ff 7134 }
34dc7c2f 7135 kernel_fini();
34dc7c2f 7136
cae5b340
AX
7137 if (!ztest_opts.zo_mmp_test)
7138 ztest_run_zdb(ztest_opts.zo_pool);
428870ff 7139 }
34dc7c2f 7140
c242c188
CS
7141 if (ztest_opts.zo_verbose >= 1) {
7142 if (hasalt) {
7143 (void) printf("%d runs of older ztest: %s\n", older,
7144 ztest_opts.zo_alt_ztest);
7145 (void) printf("%d runs of newer ztest: %s\n", newer,
7146 cmd);
7147 }
34dc7c2f
BB
7148 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7149 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7150 }
7151
483106eb
BB
7152 umem_free(cmd, MAXNAMELEN);
7153
34dc7c2f
BB
7154 return (0);
7155}