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