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