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