]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/ztest/ztest.c
Fix "--enable-code-coverage" debug build
[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 2147 RL_READER);
f763c3d1 2148 zgd->zgd_rl = zgd_private->z_rl->z_rl;
428870ff
BB
2149
2150 error = dmu_read(os, object, offset, size, buf,
2151 DMU_READ_NO_PREFETCH);
2152 ASSERT(error == 0);
2153 } else {
2154 size = doi.doi_data_block_size;
2155 if (ISP2(size)) {
2156 offset = P2ALIGN(offset, size);
2157 } else {
2158 ASSERT(offset < size);
2159 offset = 0;
2160 }
2161
e3a07cd0 2162 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
428870ff 2163 RL_READER);
f763c3d1 2164 zgd->zgd_rl = zgd_private->z_rl->z_rl;
428870ff
BB
2165
2166 error = dmu_buf_hold(os, object, offset, zgd, &db,
2167 DMU_READ_NO_PREFETCH);
2168
2169 if (error == 0) {
02dc43bc 2170 blkptr_t *bp = &lr->lr_blkptr;
03c6040b 2171
428870ff
BB
2172 zgd->zgd_db = db;
2173 zgd->zgd_bp = bp;
2174
2175 ASSERT(db->db_offset == offset);
2176 ASSERT(db->db_size == size);
2177
2178 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2179 ztest_get_done, zgd);
2180
2181 if (error == 0)
2182 return (0);
2183 }
2184 }
2185
2186 ztest_get_done(zgd, error);
2187
2188 return (error);
2189}
2190
2191static void *
2192ztest_lr_alloc(size_t lrsize, char *name)
2193{
2194 char *lr;
2195 size_t namesize = name ? strlen(name) + 1 : 0;
2196
2197 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2198
2199 if (name)
2200 bcopy(name, lr + lrsize, namesize);
2201
2202 return (lr);
2203}
2204
2205void
2206ztest_lr_free(void *lr, size_t lrsize, char *name)
2207{
2208 size_t namesize = name ? strlen(name) + 1 : 0;
2209
2210 umem_free(lr, lrsize + namesize);
2211}
2212
2213/*
2214 * Lookup a bunch of objects. Returns the number of objects not found.
2215 */
2216static int
2217ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2218{
2219 int missing = 0;
2220 int error;
d6320ddb 2221 int i;
428870ff 2222
c25b8f99 2223 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
428870ff 2224
d6320ddb 2225 for (i = 0; i < count; i++, od++) {
428870ff
BB
2226 od->od_object = 0;
2227 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2228 sizeof (uint64_t), 1, &od->od_object);
2229 if (error) {
2230 ASSERT(error == ENOENT);
2231 ASSERT(od->od_object == 0);
2232 missing++;
2233 } else {
2234 dmu_buf_t *db;
2235 ztest_block_tag_t *bbt;
2236 dmu_object_info_t doi;
2237
2238 ASSERT(od->od_object != 0);
2239 ASSERT(missing == 0); /* there should be no gaps */
2240
2241 ztest_object_lock(zd, od->od_object, RL_READER);
2242 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2243 od->od_object, FTAG, &db));
2244 dmu_object_info_from_db(db, &doi);
2245 bbt = ztest_bt_bonus(db);
2246 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2247 od->od_type = doi.doi_type;
2248 od->od_blocksize = doi.doi_data_block_size;
2249 od->od_gen = bbt->bt_gen;
2250 dmu_buf_rele(db, FTAG);
2251 ztest_object_unlock(zd, od->od_object);
2252 }
2253 }
2254
2255 return (missing);
2256}
2257
2258static int
2259ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2260{
2261 int missing = 0;
d6320ddb 2262 int i;
428870ff 2263
c25b8f99 2264 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
428870ff 2265
d6320ddb 2266 for (i = 0; i < count; i++, od++) {
428870ff
BB
2267 if (missing) {
2268 od->od_object = 0;
2269 missing++;
2270 continue;
2271 }
2272
2273 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2274
2275 lr->lr_doid = od->od_dir;
2276 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2277 lr->lrz_type = od->od_crtype;
2278 lr->lrz_blocksize = od->od_crblocksize;
2279 lr->lrz_ibshift = ztest_random_ibshift();
2280 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
50c957f7 2281 lr->lrz_dnodesize = od->od_crdnodesize;
428870ff
BB
2282 lr->lr_gen = od->od_crgen;
2283 lr->lr_crtime[0] = time(NULL);
2284
2285 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2286 ASSERT(missing == 0);
2287 od->od_object = 0;
2288 missing++;
2289 } else {
2290 od->od_object = lr->lr_foid;
2291 od->od_type = od->od_crtype;
2292 od->od_blocksize = od->od_crblocksize;
2293 od->od_gen = od->od_crgen;
2294 ASSERT(od->od_object != 0);
2295 }
2296
2297 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2298 }
2299
2300 return (missing);
2301}
2302
2303static int
2304ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2305{
2306 int missing = 0;
2307 int error;
d6320ddb 2308 int i;
428870ff 2309
c25b8f99 2310 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
428870ff
BB
2311
2312 od += count - 1;
2313
d6320ddb 2314 for (i = count - 1; i >= 0; i--, od--) {
428870ff
BB
2315 if (missing) {
2316 missing++;
2317 continue;
2318 }
2319
03c6040b
GW
2320 /*
2321 * No object was found.
2322 */
428870ff
BB
2323 if (od->od_object == 0)
2324 continue;
2325
2326 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2327
2328 lr->lr_doid = od->od_dir;
2329
2330 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2331 ASSERT3U(error, ==, ENOSPC);
2332 missing++;
2333 } else {
2334 od->od_object = 0;
2335 }
2336 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2337 }
2338
2339 return (missing);
2340}
2341
2342static int
2343ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2344 void *data)
2345{
2346 lr_write_t *lr;
2347 int error;
2348
2349 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2350
2351 lr->lr_foid = object;
2352 lr->lr_offset = offset;
2353 lr->lr_length = size;
2354 lr->lr_blkoff = 0;
2355 BP_ZERO(&lr->lr_blkptr);
2356
2357 bcopy(data, lr + 1, size);
2358
2359 error = ztest_replay_write(zd, lr, B_FALSE);
2360
2361 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2362
2363 return (error);
2364}
2365
2366static int
2367ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2368{
2369 lr_truncate_t *lr;
2370 int error;
2371
2372 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2373
2374 lr->lr_foid = object;
2375 lr->lr_offset = offset;
2376 lr->lr_length = size;
2377
2378 error = ztest_replay_truncate(zd, lr, B_FALSE);
2379
2380 ztest_lr_free(lr, sizeof (*lr), NULL);
2381
2382 return (error);
2383}
2384
2385static int
2386ztest_setattr(ztest_ds_t *zd, uint64_t object)
2387{
2388 lr_setattr_t *lr;
2389 int error;
2390
2391 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2392
2393 lr->lr_foid = object;
2394 lr->lr_size = 0;
2395 lr->lr_mode = 0;
2396
2397 error = ztest_replay_setattr(zd, lr, B_FALSE);
2398
2399 ztest_lr_free(lr, sizeof (*lr), NULL);
2400
2401 return (error);
2402}
2403
2404static void
2405ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2406{
2407 objset_t *os = zd->zd_os;
2408 dmu_tx_t *tx;
2409 uint64_t txg;
e3a07cd0 2410 ztest_zrl_t *rl;
428870ff
BB
2411
2412 txg_wait_synced(dmu_objset_pool(os), 0);
2413
2414 ztest_object_lock(zd, object, RL_READER);
2415 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2416
2417 tx = dmu_tx_create(os);
2418
2419 dmu_tx_hold_write(tx, object, offset, size);
2420
2421 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2422
2423 if (txg != 0) {
2424 dmu_prealloc(os, object, offset, size, tx);
2425 dmu_tx_commit(tx);
2426 txg_wait_synced(dmu_objset_pool(os), txg);
2427 } else {
2428 (void) dmu_free_long_range(os, object, offset, size);
2429 }
2430
e3a07cd0 2431 ztest_range_unlock(zd, rl);
428870ff
BB
2432 ztest_object_unlock(zd, object);
2433}
2434
2435static void
2436ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2437{
03c6040b 2438 int err;
428870ff
BB
2439 ztest_block_tag_t wbt;
2440 dmu_object_info_t doi;
2441 enum ztest_io_type io_type;
2442 uint64_t blocksize;
2443 void *data;
2444
2445 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2446 blocksize = doi.doi_data_block_size;
2447 data = umem_alloc(blocksize, UMEM_NOFAIL);
2448
2449 /*
2450 * Pick an i/o type at random, biased toward writing block tags.
2451 */
2452 io_type = ztest_random(ZTEST_IO_TYPES);
2453 if (ztest_random(2) == 0)
2454 io_type = ZTEST_IO_WRITE_TAG;
2455
7809eb8b 2456 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2457
428870ff
BB
2458 switch (io_type) {
2459
2460 case ZTEST_IO_WRITE_TAG:
50c957f7
NB
2461 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2462 offset, 0, 0, 0);
428870ff
BB
2463 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2464 break;
2465
2466 case ZTEST_IO_WRITE_PATTERN:
2467 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2468 if (ztest_random(2) == 0) {
2469 /*
2470 * Induce fletcher2 collisions to ensure that
2471 * zio_ddt_collision() detects and resolves them
2472 * when using fletcher2-verify for deduplication.
2473 */
2474 ((uint64_t *)data)[0] ^= 1ULL << 63;
2475 ((uint64_t *)data)[4] ^= 1ULL << 63;
2476 }
2477 (void) ztest_write(zd, object, offset, blocksize, data);
2478 break;
2479
2480 case ZTEST_IO_WRITE_ZEROES:
2481 bzero(data, blocksize);
2482 (void) ztest_write(zd, object, offset, blocksize, data);
2483 break;
2484
2485 case ZTEST_IO_TRUNCATE:
2486 (void) ztest_truncate(zd, object, offset, blocksize);
2487 break;
2488
2489 case ZTEST_IO_SETATTR:
2490 (void) ztest_setattr(zd, object);
2491 break;
e75c13c3
BB
2492 default:
2493 break;
03c6040b
GW
2494
2495 case ZTEST_IO_REWRITE:
7809eb8b 2496 (void) rw_rdlock(&ztest_name_lock);
03c6040b
GW
2497 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2498 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2499 B_FALSE);
2500 VERIFY(err == 0 || err == ENOSPC);
2501 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2502 ZFS_PROP_COMPRESSION,
2503 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2504 B_FALSE);
2505 VERIFY(err == 0 || err == ENOSPC);
7809eb8b 2506 (void) rw_unlock(&ztest_name_lock);
03c6040b
GW
2507
2508 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2509 DMU_READ_NO_PREFETCH));
2510
2511 (void) ztest_write(zd, object, offset, blocksize, data);
2512 break;
428870ff
BB
2513 }
2514
7809eb8b 2515 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0 2516
428870ff
BB
2517 umem_free(data, blocksize);
2518}
2519
2520/*
2521 * Initialize an object description template.
2522 */
2523static void
2524ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
50c957f7
NB
2525 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2526 uint64_t gen)
428870ff
BB
2527{
2528 od->od_dir = ZTEST_DIROBJ;
2529 od->od_object = 0;
2530
2531 od->od_crtype = type;
2532 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
50c957f7 2533 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
428870ff
BB
2534 od->od_crgen = gen;
2535
2536 od->od_type = DMU_OT_NONE;
2537 od->od_blocksize = 0;
2538 od->od_gen = 0;
2539
2540 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
b8864a23 2541 tag, (longlong_t)id, (u_longlong_t)index);
428870ff
BB
2542}
2543
2544/*
2545 * Lookup or create the objects for a test using the od template.
2546 * If the objects do not all exist, or if 'remove' is specified,
2547 * remove any existing objects and create new ones. Otherwise,
2548 * use the existing objects.
2549 */
2550static int
2551ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2552{
2553 int count = size / sizeof (*od);
2554 int rv = 0;
2555
1e33ac1e 2556 mutex_enter(&zd->zd_dirobj_lock);
428870ff
BB
2557 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2558 (ztest_remove(zd, od, count) != 0 ||
2559 ztest_create(zd, od, count) != 0))
2560 rv = -1;
2561 zd->zd_od = od;
1e33ac1e 2562 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2563
2564 return (rv);
2565}
2566
2567/* ARGSUSED */
2568void
2569ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2570{
2571 zilog_t *zilog = zd->zd_zilog;
2572
7809eb8b 2573 (void) rw_rdlock(&zd->zd_zilog_lock);
3e31d2b0 2574
572e2857 2575 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
428870ff
BB
2576
2577 /*
2578 * Remember the committed values in zd, which is in parent/child
2579 * shared memory. If we die, the next iteration of ztest_run()
2580 * will verify that the log really does contain this record.
2581 */
2582 mutex_enter(&zilog->zl_lock);
c242c188
CS
2583 ASSERT(zd->zd_shared != NULL);
2584 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2585 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
428870ff 2586 mutex_exit(&zilog->zl_lock);
3e31d2b0 2587
7809eb8b 2588 (void) rw_unlock(&zd->zd_zilog_lock);
3e31d2b0
ES
2589}
2590
2591/*
2592 * This function is designed to simulate the operations that occur during a
2593 * mount/unmount operation. We hold the dataset across these operations in an
2594 * attempt to expose any implicit assumptions about ZIL management.
2595 */
2596/* ARGSUSED */
2597void
2598ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2599{
2600 objset_t *os = zd->zd_os;
2601
03c6040b
GW
2602 /*
2603 * We grab the zd_dirobj_lock to ensure that no other thread is
2604 * updating the zil (i.e. adding in-memory log records) and the
2605 * zd_zilog_lock to block any I/O.
2606 */
29809a6c 2607 mutex_enter(&zd->zd_dirobj_lock);
7809eb8b 2608 (void) rw_wrlock(&zd->zd_zilog_lock);
3e31d2b0 2609
f298b24d 2610 /* zfsvfs_teardown() */
3e31d2b0
ES
2611 zil_close(zd->zd_zilog);
2612
2613 /* zfsvfs_setup() */
2614 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2615 zil_replay(os, zd, ztest_replay_vector);
2616
7809eb8b 2617 (void) rw_unlock(&zd->zd_zilog_lock);
29809a6c 2618 mutex_exit(&zd->zd_dirobj_lock);
428870ff
BB
2619}
2620
2621/*
2622 * Verify that we can't destroy an active pool, create an existing pool,
2623 * or create a pool with a bad vdev spec.
2624 */
2625/* ARGSUSED */
2626void
2627ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2628{
c242c188 2629 ztest_shared_opts_t *zo = &ztest_opts;
428870ff
BB
2630 spa_t *spa;
2631 nvlist_t *nvroot;
2632
379ca9cf
OF
2633 if (zo->zo_mmp_test)
2634 return;
2635
428870ff
BB
2636 /*
2637 * Attempt to create using a bad file.
2638 */
ea0b2538 2639 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
428870ff 2640 VERIFY3U(ENOENT, ==,
b5256303 2641 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
428870ff
BB
2642 nvlist_free(nvroot);
2643
2644 /*
2645 * Attempt to create using a bad mirror.
2646 */
ea0b2538 2647 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
428870ff 2648 VERIFY3U(ENOENT, ==,
b5256303 2649 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
428870ff
BB
2650 nvlist_free(nvroot);
2651
2652 /*
2653 * Attempt to create an existing pool. It shouldn't matter
2654 * what's in the nvroot; we should fail with EEXIST.
2655 */
7809eb8b 2656 (void) rw_rdlock(&ztest_name_lock);
ea0b2538 2657 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
b5256303
TC
2658 VERIFY3U(EEXIST, ==,
2659 spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
428870ff 2660 nvlist_free(nvroot);
c242c188
CS
2661 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2662 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
428870ff
BB
2663 spa_close(spa, FTAG);
2664
7809eb8b 2665 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
2666}
2667
0582e403
OF
2668/*
2669 * Start and then stop the MMP threads to ensure the startup and shutdown code
2670 * works properly. Actual protection and property-related code tested via ZTS.
2671 */
2672/* ARGSUSED */
2673void
2674ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2675{
2676 ztest_shared_opts_t *zo = &ztest_opts;
2677 spa_t *spa = ztest_spa;
2678
2679 if (zo->zo_mmp_test)
2680 return;
2681
2682 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2683 mutex_enter(&spa->spa_props_lock);
2684
2685 if (!spa_multihost(spa)) {
2686 spa->spa_multihost = B_TRUE;
2687 mmp_thread_start(spa);
2688 }
2689
2690 mutex_exit(&spa->spa_props_lock);
2691 spa_config_exit(spa, SCL_CONFIG, FTAG);
2692
2693 txg_wait_synced(spa_get_dsl(spa), 0);
2694 mmp_signal_all_threads();
2695 txg_wait_synced(spa_get_dsl(spa), 0);
2696
2697 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2698 mutex_enter(&spa->spa_props_lock);
2699
2700 if (spa_multihost(spa)) {
2701 mmp_thread_stop(spa);
2702 spa->spa_multihost = B_FALSE;
2703 }
2704
2705 mutex_exit(&spa->spa_props_lock);
2706 spa_config_exit(spa, SCL_CONFIG, FTAG);
2707}
2708
ea0b2538
GW
2709/* ARGSUSED */
2710void
2711ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2712{
2713 spa_t *spa;
2714 uint64_t initial_version = SPA_VERSION_INITIAL;
2715 uint64_t version, newversion;
2716 nvlist_t *nvroot, *props;
2717 char *name;
2718
379ca9cf
OF
2719 if (ztest_opts.zo_mmp_test)
2720 return;
2721
ea0b2538
GW
2722 mutex_enter(&ztest_vdev_lock);
2723 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2724
2725 /*
2726 * Clean up from previous runs.
2727 */
2728 (void) spa_destroy(name);
2729
2730 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2731 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2732
2733 /*
2734 * If we're configuring a RAIDZ device then make sure that the
2735 * the initial version is capable of supporting that feature.
2736 */
2737 switch (ztest_opts.zo_raidz_parity) {
2738 case 0:
2739 case 1:
2740 initial_version = SPA_VERSION_INITIAL;
2741 break;
2742 case 2:
2743 initial_version = SPA_VERSION_RAIDZ2;
2744 break;
2745 case 3:
2746 initial_version = SPA_VERSION_RAIDZ3;
2747 break;
2748 }
2749
2750 /*
2751 * Create a pool with a spa version that can be upgraded. Pick
2752 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2753 */
2754 do {
2755 version = ztest_random_spa_version(initial_version);
2756 } while (version > SPA_VERSION_BEFORE_FEATURES);
2757
2758 props = fnvlist_alloc();
2759 fnvlist_add_uint64(props,
2760 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
b5256303 2761 VERIFY3S(spa_create(name, nvroot, props, NULL, NULL), ==, 0);
ea0b2538
GW
2762 fnvlist_free(nvroot);
2763 fnvlist_free(props);
2764
2765 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2766 VERIFY3U(spa_version(spa), ==, version);
2767 newversion = ztest_random_spa_version(version + 1);
2768
2769 if (ztest_opts.zo_verbose >= 4) {
2770 (void) printf("upgrading spa version from %llu to %llu\n",
2771 (u_longlong_t)version, (u_longlong_t)newversion);
2772 }
2773
2774 spa_upgrade(spa, newversion);
2775 VERIFY3U(spa_version(spa), >, version);
2776 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2777 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2778 spa_close(spa, FTAG);
2779
2780 strfree(name);
2781 mutex_exit(&ztest_vdev_lock);
2782}
2783
428870ff
BB
2784static vdev_t *
2785vdev_lookup_by_path(vdev_t *vd, const char *path)
2786{
2787 vdev_t *mvd;
d6320ddb 2788 int c;
428870ff
BB
2789
2790 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2791 return (vd);
2792
d6320ddb 2793 for (c = 0; c < vd->vdev_children; c++)
428870ff
BB
2794 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2795 NULL)
2796 return (mvd);
2797
2798 return (NULL);
2799}
2800
2801/*
2802 * Find the first available hole which can be used as a top-level.
2803 */
2804int
2805find_vdev_hole(spa_t *spa)
2806{
2807 vdev_t *rvd = spa->spa_root_vdev;
2808 int c;
2809
2810 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2811
2812 for (c = 0; c < rvd->vdev_children; c++) {
2813 vdev_t *cvd = rvd->vdev_child[c];
2814
2815 if (cvd->vdev_ishole)
2816 break;
2817 }
2818 return (c);
2819}
2820
2821/*
2822 * Verify that vdev_add() works as expected.
2823 */
2824/* ARGSUSED */
2825void
2826ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2827{
2828 ztest_shared_t *zs = ztest_shared;
c242c188 2829 spa_t *spa = ztest_spa;
428870ff
BB
2830 uint64_t leaves;
2831 uint64_t guid;
2832 nvlist_t *nvroot;
2833 int error;
2834
379ca9cf
OF
2835 if (ztest_opts.zo_mmp_test)
2836 return;
2837
c242c188 2838 mutex_enter(&ztest_vdev_lock);
13fe0198 2839 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
428870ff
BB
2840
2841 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2842
2843 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2844
2845 /*
2846 * If we have slogs then remove them 1/4 of the time.
2847 */
2848 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2849 /*
2850 * Grab the guid from the head of the log class rotor.
2851 */
2852 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2853
2854 spa_config_exit(spa, SCL_VDEV, FTAG);
2855
2856 /*
2857 * We have to grab the zs_name_lock as writer to
2858 * prevent a race between removing a slog (dmu_objset_find)
2859 * and destroying a dataset. Removing the slog will
2860 * grab a reference on the dataset which may cause
13fe0198 2861 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
2862 * leaving the dataset in an inconsistent state.
2863 */
7809eb8b 2864 rw_wrlock(&ztest_name_lock);
428870ff 2865 error = spa_vdev_remove(spa, guid, B_FALSE);
7809eb8b 2866 rw_unlock(&ztest_name_lock);
428870ff
BB
2867
2868 if (error && error != EEXIST)
2869 fatal(0, "spa_vdev_remove() = %d", error);
2870 } else {
2871 spa_config_exit(spa, SCL_VDEV, FTAG);
2872
2873 /*
2874 * Make 1/4 of the devices be log devices.
2875 */
ea0b2538 2876 nvroot = make_vdev_root(NULL, NULL, NULL,
c242c188
CS
2877 ztest_opts.zo_vdev_size, 0,
2878 ztest_random(4) == 0, ztest_opts.zo_raidz,
2879 zs->zs_mirrors, 1);
428870ff
BB
2880
2881 error = spa_vdev_add(spa, nvroot);
2882 nvlist_free(nvroot);
2883
2884 if (error == ENOSPC)
2885 ztest_record_enospc("spa_vdev_add");
2886 else if (error != 0)
2887 fatal(0, "spa_vdev_add() = %d", error);
2888 }
2889
c242c188 2890 mutex_exit(&ztest_vdev_lock);
428870ff
BB
2891}
2892
2893/*
2894 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2895 */
2896/* ARGSUSED */
2897void
2898ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2899{
2900 ztest_shared_t *zs = ztest_shared;
c242c188 2901 spa_t *spa = ztest_spa;
428870ff
BB
2902 vdev_t *rvd = spa->spa_root_vdev;
2903 spa_aux_vdev_t *sav;
2904 char *aux;
40b84e7a 2905 char *path;
428870ff
BB
2906 uint64_t guid = 0;
2907 int error;
2908
379ca9cf
OF
2909 if (ztest_opts.zo_mmp_test)
2910 return;
2911
40b84e7a
BB
2912 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2913
428870ff
BB
2914 if (ztest_random(2) == 0) {
2915 sav = &spa->spa_spares;
2916 aux = ZPOOL_CONFIG_SPARES;
2917 } else {
2918 sav = &spa->spa_l2cache;
2919 aux = ZPOOL_CONFIG_L2CACHE;
2920 }
2921
c242c188 2922 mutex_enter(&ztest_vdev_lock);
428870ff
BB
2923
2924 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2925
2926 if (sav->sav_count != 0 && ztest_random(4) == 0) {
b128c09f
BB
2927 /*
2928 * Pick a random device to remove.
2929 */
2930 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2931 } else {
2932 /*
2933 * Find an unused device we can add.
2934 */
428870ff 2935 zs->zs_vdev_aux = 0;
b128c09f 2936 for (;;) {
b128c09f 2937 int c;
3db3ff4a 2938 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
c242c188
CS
2939 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2940 zs->zs_vdev_aux);
b128c09f
BB
2941 for (c = 0; c < sav->sav_count; c++)
2942 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2943 path) == 0)
2944 break;
2945 if (c == sav->sav_count &&
2946 vdev_lookup_by_path(rvd, path) == NULL)
2947 break;
428870ff 2948 zs->zs_vdev_aux++;
34dc7c2f
BB
2949 }
2950 }
2951
b128c09f 2952 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f 2953
b128c09f
BB
2954 if (guid == 0) {
2955 /*
2956 * Add a new device.
2957 */
ea0b2538 2958 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
c242c188 2959 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
b128c09f
BB
2960 error = spa_vdev_add(spa, nvroot);
2961 if (error != 0)
2962 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2963 nvlist_free(nvroot);
2964 } else {
2965 /*
2966 * Remove an existing device. Sometimes, dirty its
2967 * vdev state first to make sure we handle removal
2968 * of devices that have pending state changes.
2969 */
2970 if (ztest_random(2) == 0)
9babb374 2971 (void) vdev_online(spa, guid, 0, NULL);
b128c09f
BB
2972
2973 error = spa_vdev_remove(spa, guid, B_FALSE);
2974 if (error != 0 && error != EBUSY)
2975 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2976 }
2977
c242c188 2978 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
2979
2980 umem_free(path, MAXPATHLEN);
428870ff
BB
2981}
2982
2983/*
2984 * split a pool if it has mirror tlvdevs
2985 */
2986/* ARGSUSED */
2987void
2988ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2989{
2990 ztest_shared_t *zs = ztest_shared;
c242c188 2991 spa_t *spa = ztest_spa;
428870ff
BB
2992 vdev_t *rvd = spa->spa_root_vdev;
2993 nvlist_t *tree, **child, *config, *split, **schild;
2994 uint_t c, children, schildren = 0, lastlogid = 0;
2995 int error = 0;
2996
379ca9cf
OF
2997 if (ztest_opts.zo_mmp_test)
2998 return;
2999
c242c188 3000 mutex_enter(&ztest_vdev_lock);
428870ff
BB
3001
3002 /* ensure we have a useable config; mirrors of raidz aren't supported */
c242c188
CS
3003 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3004 mutex_exit(&ztest_vdev_lock);
428870ff
BB
3005 return;
3006 }
3007
3008 /* clean up the old pool, if any */
3009 (void) spa_destroy("splitp");
3010
3011 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3012
3013 /* generate a config from the existing config */
3014 mutex_enter(&spa->spa_props_lock);
3015 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3016 &tree) == 0);
3017 mutex_exit(&spa->spa_props_lock);
3018
3019 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3020 &children) == 0);
3021
3022 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3023 for (c = 0; c < children; c++) {
3024 vdev_t *tvd = rvd->vdev_child[c];
3025 nvlist_t **mchild;
3026 uint_t mchildren;
3027
3028 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3029 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3030 0) == 0);
3031 VERIFY(nvlist_add_string(schild[schildren],
3032 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3033 VERIFY(nvlist_add_uint64(schild[schildren],
3034 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3035 if (lastlogid == 0)
3036 lastlogid = schildren;
3037 ++schildren;
3038 continue;
3039 }
3040 lastlogid = 0;
3041 VERIFY(nvlist_lookup_nvlist_array(child[c],
3042 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3043 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3044 }
3045
3046 /* OK, create a config that can be used to split */
3047 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3048 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3049 VDEV_TYPE_ROOT) == 0);
3050 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3051 lastlogid != 0 ? lastlogid : schildren) == 0);
3052
3053 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3054 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3055
3056 for (c = 0; c < schildren; c++)
3057 nvlist_free(schild[c]);
3058 free(schild);
3059 nvlist_free(split);
3060
3061 spa_config_exit(spa, SCL_VDEV, FTAG);
3062
7809eb8b 3063 (void) rw_wrlock(&ztest_name_lock);
428870ff 3064 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
7809eb8b 3065 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
3066
3067 nvlist_free(config);
3068
3069 if (error == 0) {
3070 (void) printf("successful split - results:\n");
3071 mutex_enter(&spa_namespace_lock);
3072 show_pool_stats(spa);
3073 show_pool_stats(spa_lookup("splitp"));
3074 mutex_exit(&spa_namespace_lock);
3075 ++zs->zs_splits;
3076 --zs->zs_mirrors;
3077 }
c242c188 3078 mutex_exit(&ztest_vdev_lock);
428870ff 3079
34dc7c2f
BB
3080}
3081
3082/*
3083 * Verify that we can attach and detach devices.
3084 */
428870ff 3085/* ARGSUSED */
34dc7c2f 3086void
428870ff 3087ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3088{
428870ff 3089 ztest_shared_t *zs = ztest_shared;
c242c188 3090 spa_t *spa = ztest_spa;
b128c09f 3091 spa_aux_vdev_t *sav = &spa->spa_spares;
34dc7c2f
BB
3092 vdev_t *rvd = spa->spa_root_vdev;
3093 vdev_t *oldvd, *newvd, *pvd;
b128c09f 3094 nvlist_t *root;
428870ff 3095 uint64_t leaves;
34dc7c2f
BB
3096 uint64_t leaf, top;
3097 uint64_t ashift = ztest_get_ashift();
fb5f0bc8 3098 uint64_t oldguid, pguid;
5d1f7fb6 3099 uint64_t oldsize, newsize;
40b84e7a 3100 char *oldpath, *newpath;
34dc7c2f 3101 int replacing;
b128c09f
BB
3102 int oldvd_has_siblings = B_FALSE;
3103 int newvd_is_spare = B_FALSE;
3104 int oldvd_is_log;
34dc7c2f 3105 int error, expected_error;
34dc7c2f 3106
379ca9cf
OF
3107 if (ztest_opts.zo_mmp_test)
3108 return;
3109
40b84e7a
BB
3110 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3111 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3112
c242c188
CS
3113 mutex_enter(&ztest_vdev_lock);
3114 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
34dc7c2f 3115
b128c09f 3116 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
34dc7c2f
BB
3117
3118 /*
3119 * Decide whether to do an attach or a replace.
3120 */
3121 replacing = ztest_random(2);
3122
3123 /*
3124 * Pick a random top-level vdev.
3125 */
428870ff 3126 top = ztest_random_vdev_top(spa, B_TRUE);
34dc7c2f
BB
3127
3128 /*
3129 * Pick a random leaf within it.
3130 */
3131 leaf = ztest_random(leaves);
3132
3133 /*
b128c09f 3134 * Locate this vdev.
34dc7c2f 3135 */
b128c09f 3136 oldvd = rvd->vdev_child[top];
428870ff 3137 if (zs->zs_mirrors >= 1) {
fb5f0bc8 3138 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
428870ff 3139 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
c242c188 3140 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
fb5f0bc8 3141 }
c242c188 3142 if (ztest_opts.zo_raidz > 1) {
fb5f0bc8 3143 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
c242c188
CS
3144 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3145 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
fb5f0bc8 3146 }
34dc7c2f
BB
3147
3148 /*
b128c09f
BB
3149 * If we're already doing an attach or replace, oldvd may be a
3150 * mirror vdev -- in which case, pick a random child.
34dc7c2f 3151 */
b128c09f
BB
3152 while (oldvd->vdev_children != 0) {
3153 oldvd_has_siblings = B_TRUE;
fb5f0bc8
BB
3154 ASSERT(oldvd->vdev_children >= 2);
3155 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
b128c09f
BB
3156 }
3157
3158 oldguid = oldvd->vdev_guid;
9babb374 3159 oldsize = vdev_get_min_asize(oldvd);
b128c09f
BB
3160 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3161 (void) strcpy(oldpath, oldvd->vdev_path);
3162 pvd = oldvd->vdev_parent;
fb5f0bc8 3163 pguid = pvd->vdev_guid;
34dc7c2f
BB
3164
3165 /*
b128c09f 3166 * If oldvd has siblings, then half of the time, detach it.
34dc7c2f 3167 */
b128c09f
BB
3168 if (oldvd_has_siblings && ztest_random(2) == 0) {
3169 spa_config_exit(spa, SCL_VDEV, FTAG);
fb5f0bc8
BB
3170 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3171 if (error != 0 && error != ENODEV && error != EBUSY &&
3172 error != ENOTSUP)
3173 fatal(0, "detach (%s) returned %d", oldpath, error);
40b84e7a 3174 goto out;
b128c09f 3175 }
34dc7c2f
BB
3176
3177 /*
b128c09f
BB
3178 * For the new vdev, choose with equal probability between the two
3179 * standard paths (ending in either 'a' or 'b') or a random hot spare.
34dc7c2f 3180 */
b128c09f
BB
3181 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3182 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3183 newvd_is_spare = B_TRUE;
3184 (void) strcpy(newpath, newvd->vdev_path);
3185 } else {
6aec1cd5 3186 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
c242c188
CS
3187 ztest_opts.zo_dir, ztest_opts.zo_pool,
3188 top * leaves + leaf);
b128c09f
BB
3189 if (ztest_random(2) == 0)
3190 newpath[strlen(newpath) - 1] = 'b';
3191 newvd = vdev_lookup_by_path(rvd, newpath);
3192 }
3193
3194 if (newvd) {
9babb374 3195 newsize = vdev_get_min_asize(newvd);
b128c09f
BB
3196 } else {
3197 /*
3198 * Make newsize a little bigger or smaller than oldsize.
3199 * If it's smaller, the attach should fail.
3200 * If it's larger, and we're doing a replace,
3201 * we should get dynamic LUN growth when we're done.
3202 */
3203 newsize = 10 * oldsize / (9 + ztest_random(3));
3204 }
34dc7c2f
BB
3205
3206 /*
3207 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3208 * unless it's a replace; in that case any non-replacing parent is OK.
3209 *
3210 * If newvd is already part of the pool, it should fail with EBUSY.
3211 *
3212 * If newvd is too small, it should fail with EOVERFLOW.
3213 */
b128c09f
BB
3214 if (pvd->vdev_ops != &vdev_mirror_ops &&
3215 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3216 pvd->vdev_ops == &vdev_replacing_ops ||
3217 pvd->vdev_ops == &vdev_spare_ops))
34dc7c2f 3218 expected_error = ENOTSUP;
b128c09f
BB
3219 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3220 expected_error = ENOTSUP;
3221 else if (newvd == oldvd)
3222 expected_error = replacing ? 0 : EBUSY;
3223 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3224 expected_error = EBUSY;
34dc7c2f
BB
3225 else if (newsize < oldsize)
3226 expected_error = EOVERFLOW;
3227 else if (ashift > oldvd->vdev_top->vdev_ashift)
3228 expected_error = EDOM;
3229 else
3230 expected_error = 0;
3231
b128c09f 3232 spa_config_exit(spa, SCL_VDEV, FTAG);
34dc7c2f
BB
3233
3234 /*
3235 * Build the nvlist describing newpath.
3236 */
ea0b2538 3237 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
b128c09f 3238 ashift, 0, 0, 0, 1);
34dc7c2f 3239
b128c09f 3240 error = spa_vdev_attach(spa, oldguid, root, replacing);
34dc7c2f 3241
34dc7c2f
BB
3242 nvlist_free(root);
3243
3244 /*
3245 * If our parent was the replacing vdev, but the replace completed,
3246 * then instead of failing with ENOTSUP we may either succeed,
3247 * fail with ENODEV, or fail with EOVERFLOW.
3248 */
3249 if (expected_error == ENOTSUP &&
3250 (error == 0 || error == ENODEV || error == EOVERFLOW))
3251 expected_error = error;
3252
3253 /*
3254 * If someone grew the LUN, the replacement may be too small.
3255 */
b128c09f 3256 if (error == EOVERFLOW || error == EBUSY)
34dc7c2f
BB
3257 expected_error = error;
3258
b128c09f
BB
3259 /* XXX workaround 6690467 */
3260 if (error != expected_error && expected_error != EBUSY) {
3261 fatal(0, "attach (%s %llu, %s %llu, %d) "
3262 "returned %d, expected %d",
5d1f7fb6
GW
3263 oldpath, oldsize, newpath,
3264 newsize, replacing, error, expected_error);
34dc7c2f 3265 }
40b84e7a 3266out:
c242c188 3267 mutex_exit(&ztest_vdev_lock);
40b84e7a
BB
3268
3269 umem_free(oldpath, MAXPATHLEN);
3270 umem_free(newpath, MAXPATHLEN);
34dc7c2f
BB
3271}
3272
9babb374
BB
3273/*
3274 * Callback function which expands the physical size of the vdev.
3275 */
3276vdev_t *
3277grow_vdev(vdev_t *vd, void *arg)
3278{
1fde1e37 3279 ASSERTV(spa_t *spa = vd->vdev_spa);
9babb374
BB
3280 size_t *newsize = arg;
3281 size_t fsize;
3282 int fd;
3283
3284 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3285 ASSERT(vd->vdev_ops->vdev_op_leaf);
3286
3287 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3288 return (vd);
3289
3290 fsize = lseek(fd, 0, SEEK_END);
0e5b68e0 3291 VERIFY(ftruncate(fd, *newsize) == 0);
9babb374 3292
c242c188 3293 if (ztest_opts.zo_verbose >= 6) {
9babb374
BB
3294 (void) printf("%s grew from %lu to %lu bytes\n",
3295 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3296 }
3297 (void) close(fd);
3298 return (NULL);
3299}
3300
3301/*
3302 * Callback function which expands a given vdev by calling vdev_online().
3303 */
3304/* ARGSUSED */
3305vdev_t *
3306online_vdev(vdev_t *vd, void *arg)
3307{
3308 spa_t *spa = vd->vdev_spa;
3309 vdev_t *tvd = vd->vdev_top;
9babb374 3310 uint64_t guid = vd->vdev_guid;
428870ff
BB
3311 uint64_t generation = spa->spa_config_generation + 1;
3312 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3313 int error;
9babb374
BB
3314
3315 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3316 ASSERT(vd->vdev_ops->vdev_op_leaf);
3317
3318 /* Calling vdev_online will initialize the new metaslabs */
3319 spa_config_exit(spa, SCL_STATE, spa);
428870ff 3320 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
9babb374
BB
3321 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3322
428870ff
BB
3323 /*
3324 * If vdev_online returned an error or the underlying vdev_open
3325 * failed then we abort the expand. The only way to know that
3326 * vdev_open fails is by checking the returned newstate.
3327 */
3328 if (error || newstate != VDEV_STATE_HEALTHY) {
c242c188 3329 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3330 (void) printf("Unable to expand vdev, state %llu, "
3331 "error %d\n", (u_longlong_t)newstate, error);
3332 }
3333 return (vd);
3334 }
3335 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3336
9babb374
BB
3337 /*
3338 * Since we dropped the lock we need to ensure that we're
3339 * still talking to the original vdev. It's possible this
3340 * vdev may have been detached/replaced while we were
3341 * trying to online it.
3342 */
428870ff 3343 if (generation != spa->spa_config_generation) {
c242c188 3344 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3345 (void) printf("vdev configuration has changed, "
3346 "guid %llu, state %llu, expected gen %llu, "
3347 "got gen %llu\n",
3348 (u_longlong_t)guid,
3349 (u_longlong_t)tvd->vdev_state,
3350 (u_longlong_t)generation,
3351 (u_longlong_t)spa->spa_config_generation);
9babb374
BB
3352 }
3353 return (vd);
3354 }
3355 return (NULL);
3356}
3357
3358/*
3359 * Traverse the vdev tree calling the supplied function.
3360 * We continue to walk the tree until we either have walked all
3361 * children or we receive a non-NULL return from the callback.
3362 * If a NULL callback is passed, then we just return back the first
3363 * leaf vdev we encounter.
3364 */
3365vdev_t *
3366vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3367{
d6320ddb
BB
3368 uint_t c;
3369
9babb374
BB
3370 if (vd->vdev_ops->vdev_op_leaf) {
3371 if (func == NULL)
3372 return (vd);
3373 else
3374 return (func(vd, arg));
3375 }
3376
d6320ddb 3377 for (c = 0; c < vd->vdev_children; c++) {
9babb374
BB
3378 vdev_t *cvd = vd->vdev_child[c];
3379 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3380 return (cvd);
3381 }
3382 return (NULL);
3383}
3384
34dc7c2f
BB
3385/*
3386 * Verify that dynamic LUN growth works as expected.
3387 */
428870ff 3388/* ARGSUSED */
34dc7c2f 3389void
428870ff 3390ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3391{
c242c188 3392 spa_t *spa = ztest_spa;
428870ff
BB
3393 vdev_t *vd, *tvd;
3394 metaslab_class_t *mc;
3395 metaslab_group_t *mg;
9babb374 3396 size_t psize, newsize;
428870ff
BB
3397 uint64_t top;
3398 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
34dc7c2f 3399
c242c188 3400 mutex_enter(&ztest_vdev_lock);
9babb374
BB
3401 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3402
428870ff 3403 top = ztest_random_vdev_top(spa, B_TRUE);
9babb374 3404
428870ff
BB
3405 tvd = spa->spa_root_vdev->vdev_child[top];
3406 mg = tvd->vdev_mg;
3407 mc = mg->mg_class;
3408 old_ms_count = tvd->vdev_ms_count;
3409 old_class_space = metaslab_class_get_space(mc);
34dc7c2f
BB
3410
3411 /*
9babb374
BB
3412 * Determine the size of the first leaf vdev associated with
3413 * our top-level device.
34dc7c2f 3414 */
9babb374
BB
3415 vd = vdev_walk_tree(tvd, NULL, NULL);
3416 ASSERT3P(vd, !=, NULL);
3417 ASSERT(vd->vdev_ops->vdev_op_leaf);
34dc7c2f 3418
9babb374 3419 psize = vd->vdev_psize;
34dc7c2f 3420
9babb374 3421 /*
428870ff
BB
3422 * We only try to expand the vdev if it's healthy, less than 4x its
3423 * original size, and it has a valid psize.
9babb374 3424 */
428870ff 3425 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
c242c188 3426 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
9babb374 3427 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3428 mutex_exit(&ztest_vdev_lock);
9babb374
BB
3429 return;
3430 }
3431 ASSERT(psize > 0);
3432 newsize = psize + psize / 8;
3433 ASSERT3U(newsize, >, psize);
34dc7c2f 3434
c242c188 3435 if (ztest_opts.zo_verbose >= 6) {
428870ff 3436 (void) printf("Expanding LUN %s from %lu to %lu\n",
9babb374
BB
3437 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3438 }
3439
9babb374
BB
3440 /*
3441 * Growing the vdev is a two step process:
3442 * 1). expand the physical size (i.e. relabel)
3443 * 2). online the vdev to create the new metaslabs
3444 */
3445 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3446 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3447 tvd->vdev_state != VDEV_STATE_HEALTHY) {
c242c188 3448 if (ztest_opts.zo_verbose >= 5) {
9babb374 3449 (void) printf("Could not expand LUN because "
428870ff 3450 "the vdev configuration changed.\n");
34dc7c2f 3451 }
428870ff 3452 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3453 mutex_exit(&ztest_vdev_lock);
9babb374 3454 return;
34dc7c2f
BB
3455 }
3456
428870ff 3457 spa_config_exit(spa, SCL_STATE, spa);
9babb374
BB
3458
3459 /*
3460 * Expanding the LUN will update the config asynchronously,
3461 * thus we must wait for the async thread to complete any
3462 * pending tasks before proceeding.
3463 */
428870ff
BB
3464 for (;;) {
3465 boolean_t done;
3466 mutex_enter(&spa->spa_async_lock);
3467 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3468 mutex_exit(&spa->spa_async_lock);
3469 if (done)
3470 break;
3471 txg_wait_synced(spa_get_dsl(spa), 0);
3472 (void) poll(NULL, 0, 100);
3473 }
9babb374
BB
3474
3475 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
428870ff
BB
3476
3477 tvd = spa->spa_root_vdev->vdev_child[top];
3478 new_ms_count = tvd->vdev_ms_count;
3479 new_class_space = metaslab_class_get_space(mc);
3480
3481 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
c242c188 3482 if (ztest_opts.zo_verbose >= 5) {
428870ff
BB
3483 (void) printf("Could not verify LUN expansion due to "
3484 "intervening vdev offline or remove.\n");
3485 }
3486 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3487 mutex_exit(&ztest_vdev_lock);
428870ff
BB
3488 return;
3489 }
3490
3491 /*
3492 * Make sure we were able to grow the vdev.
3493 */
3494 if (new_ms_count <= old_ms_count)
3495 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3496 old_ms_count, new_ms_count);
9babb374
BB
3497
3498 /*
3499 * Make sure we were able to grow the pool.
3500 */
428870ff
BB
3501 if (new_class_space <= old_class_space)
3502 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3503 old_class_space, new_class_space);
3504
c242c188 3505 if (ztest_opts.zo_verbose >= 5) {
9babb374
BB
3506 char oldnumbuf[6], newnumbuf[6];
3507
428870ff
BB
3508 nicenum(old_class_space, oldnumbuf);
3509 nicenum(new_class_space, newnumbuf);
9babb374
BB
3510 (void) printf("%s grew from %s to %s\n",
3511 spa->spa_name, oldnumbuf, newnumbuf);
3512 }
428870ff 3513
9babb374 3514 spa_config_exit(spa, SCL_STATE, spa);
c242c188 3515 mutex_exit(&ztest_vdev_lock);
34dc7c2f
BB
3516}
3517
428870ff
BB
3518/*
3519 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3520 */
34dc7c2f
BB
3521/* ARGSUSED */
3522static void
428870ff 3523ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
34dc7c2f
BB
3524{
3525 /*
428870ff 3526 * Create the objects common to all ztest datasets.
34dc7c2f 3527 */
428870ff 3528 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
34dc7c2f 3529 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
428870ff 3530}
34dc7c2f 3531
428870ff
BB
3532static int
3533ztest_dataset_create(char *dsname)
3534{
3535 uint64_t zilset = ztest_random(100);
b5256303 3536 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, NULL,
428870ff
BB
3537 ztest_objset_create_cb, NULL);
3538
3539 if (err || zilset < 80)
3540 return (err);
3541
c242c188 3542 if (ztest_opts.zo_verbose >= 5)
b815ff9a 3543 (void) printf("Setting dataset %s to sync always\n", dsname);
428870ff
BB
3544 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3545 ZFS_SYNC_ALWAYS, B_FALSE));
34dc7c2f
BB
3546}
3547
428870ff 3548/* ARGSUSED */
34dc7c2f 3549static int
428870ff 3550ztest_objset_destroy_cb(const char *name, void *arg)
34dc7c2f 3551{
34dc7c2f 3552 objset_t *os;
428870ff 3553 dmu_object_info_t doi;
34dc7c2f
BB
3554 int error;
3555
3556 /*
3557 * Verify that the dataset contains a directory object.
3558 */
b5256303 3559 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, B_TRUE, FTAG, &os));
428870ff 3560 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
34dc7c2f
BB
3561 if (error != ENOENT) {
3562 /* We could have crashed in the middle of destroying it */
c99c9001 3563 ASSERT0(error);
428870ff
BB
3564 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3565 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
34dc7c2f 3566 }
b5256303 3567 dmu_objset_disown(os, B_TRUE, FTAG);
34dc7c2f
BB
3568
3569 /*
3570 * Destroy the dataset.
3571 */
13fe0198 3572 if (strchr(name, '@') != NULL) {
dc1fbc43 3573 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
13fe0198 3574 } else {
dc1fbc43
GM
3575 error = dsl_destroy_head(name);
3576 /* There could be a hold on this dataset */
3577 if (error != EBUSY)
3578 ASSERT0(error);
13fe0198 3579 }
34dc7c2f
BB
3580 return (0);
3581}
3582
428870ff
BB
3583static boolean_t
3584ztest_snapshot_create(char *osname, uint64_t id)
34dc7c2f 3585{
eca7b760 3586 char snapname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3587 int error;
3588
13fe0198 3589 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
428870ff 3590
13fe0198 3591 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
3592 if (error == ENOSPC) {
3593 ztest_record_enospc(FTAG);
3594 return (B_FALSE);
3595 }
13fe0198
MA
3596 if (error != 0 && error != EEXIST) {
3597 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3598 snapname, error);
3599 }
428870ff
BB
3600 return (B_TRUE);
3601}
3602
3603static boolean_t
3604ztest_snapshot_destroy(char *osname, uint64_t id)
3605{
eca7b760 3606 char snapname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3607 int error;
3608
eca7b760 3609 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
428870ff
BB
3610 (u_longlong_t)id);
3611
13fe0198 3612 error = dsl_destroy_snapshot(snapname, B_FALSE);
428870ff
BB
3613 if (error != 0 && error != ENOENT)
3614 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3615 return (B_TRUE);
34dc7c2f
BB
3616}
3617
428870ff 3618/* ARGSUSED */
34dc7c2f 3619void
428870ff 3620ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3621{
40b84e7a 3622 ztest_ds_t *zdtmp;
428870ff 3623 int iters;
34dc7c2f 3624 int error;
b128c09f 3625 objset_t *os, *os2;
eca7b760 3626 char name[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 3627 zilog_t *zilog;
d6320ddb 3628 int i;
34dc7c2f 3629
40b84e7a 3630 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
40b84e7a 3631
7809eb8b 3632 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f 3633
eca7b760 3634 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
c242c188 3635 ztest_opts.zo_pool, (u_longlong_t)id);
34dc7c2f
BB
3636
3637 /*
3638 * If this dataset exists from a previous run, process its replay log
13fe0198 3639 * half of the time. If we don't replay it, then dsl_destroy_head()
428870ff 3640 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
34dc7c2f
BB
3641 */
3642 if (ztest_random(2) == 0 &&
b5256303
TC
3643 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3644 B_TRUE, FTAG, &os) == 0) {
c242c188 3645 ztest_zd_init(zdtmp, NULL, os);
40b84e7a
BB
3646 zil_replay(os, zdtmp, ztest_replay_vector);
3647 ztest_zd_fini(zdtmp);
b5256303 3648 dmu_objset_disown(os, B_TRUE, FTAG);
34dc7c2f
BB
3649 }
3650
3651 /*
3652 * There may be an old instance of the dataset we're about to
3653 * create lying around from a previous run. If so, destroy it
3654 * and all of its snapshots.
3655 */
428870ff 3656 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
34dc7c2f
BB
3657 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3658
3659 /*
3660 * Verify that the destroyed dataset is no longer in the namespace.
3661 */
b5256303 3662 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, B_TRUE,
13fe0198 3663 FTAG, &os));
34dc7c2f
BB
3664
3665 /*
3666 * Verify that we can create a new dataset.
3667 */
428870ff 3668 error = ztest_dataset_create(name);
34dc7c2f
BB
3669 if (error) {
3670 if (error == ENOSPC) {
428870ff 3671 ztest_record_enospc(FTAG);
40b84e7a 3672 goto out;
34dc7c2f
BB
3673 }
3674 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3675 }
3676
b5256303
TC
3677 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3678 FTAG, &os));
428870ff 3679
c242c188 3680 ztest_zd_init(zdtmp, NULL, os);
34dc7c2f
BB
3681
3682 /*
3683 * Open the intent log for it.
3684 */
428870ff 3685 zilog = zil_open(os, ztest_get_data);
34dc7c2f
BB
3686
3687 /*
428870ff
BB
3688 * Put some objects in there, do a little I/O to them,
3689 * and randomly take a couple of snapshots along the way.
34dc7c2f 3690 */
428870ff 3691 iters = ztest_random(5);
d6320ddb 3692 for (i = 0; i < iters; i++) {
40b84e7a 3693 ztest_dmu_object_alloc_free(zdtmp, id);
428870ff
BB
3694 if (ztest_random(iters) == 0)
3695 (void) ztest_snapshot_create(name, i);
34dc7c2f
BB
3696 }
3697
3698 /*
3699 * Verify that we cannot create an existing dataset.
3700 */
428870ff 3701 VERIFY3U(EEXIST, ==,
b5256303 3702 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
34dc7c2f
BB
3703
3704 /*
428870ff 3705 * Verify that we can hold an objset that is also owned.
b128c09f 3706 */
428870ff
BB
3707 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3708 dmu_objset_rele(os2, FTAG);
34dc7c2f 3709
428870ff
BB
3710 /*
3711 * Verify that we cannot own an objset that is already owned.
3712 */
3713 VERIFY3U(EBUSY, ==,
b5256303 3714 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE, FTAG, &os2));
34dc7c2f 3715
428870ff 3716 zil_close(zilog);
b5256303 3717 dmu_objset_disown(os, B_TRUE, FTAG);
40b84e7a
BB
3718 ztest_zd_fini(zdtmp);
3719out:
7809eb8b 3720 (void) rw_unlock(&ztest_name_lock);
40b84e7a 3721
40b84e7a 3722 umem_free(zdtmp, sizeof (ztest_ds_t));
34dc7c2f
BB
3723}
3724
3725/*
3726 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3727 */
3728void
428870ff 3729ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3730{
7809eb8b 3731 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
3732 (void) ztest_snapshot_destroy(zd->zd_name, id);
3733 (void) ztest_snapshot_create(zd->zd_name, id);
7809eb8b 3734 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
3735}
3736
9babb374
BB
3737/*
3738 * Cleanup non-standard snapshots and clones.
3739 */
3740void
428870ff 3741ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
9babb374 3742{
40b84e7a
BB
3743 char *snap1name;
3744 char *clone1name;
3745 char *snap2name;
3746 char *clone2name;
3747 char *snap3name;
9babb374
BB
3748 int error;
3749
eca7b760
IK
3750 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3751 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3752 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3753 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3754 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3755
3756 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3757 "%s@s1_%llu", osname, (u_longlong_t)id);
3758 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3759 "%s/c1_%llu", osname, (u_longlong_t)id);
3760 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3761 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3762 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3763 "%s/c2_%llu", osname, (u_longlong_t)id);
3764 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3765 "%s@s3_%llu", clone1name, (u_longlong_t)id);
9babb374 3766
13fe0198 3767 error = dsl_destroy_head(clone2name);
9babb374 3768 if (error && error != ENOENT)
13fe0198
MA
3769 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3770 error = dsl_destroy_snapshot(snap3name, B_FALSE);
9babb374 3771 if (error && error != ENOENT)
13fe0198
MA
3772 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3773 error = dsl_destroy_snapshot(snap2name, B_FALSE);
9babb374 3774 if (error && error != ENOENT)
13fe0198
MA
3775 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3776 error = dsl_destroy_head(clone1name);
9babb374 3777 if (error && error != ENOENT)
13fe0198
MA
3778 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3779 error = dsl_destroy_snapshot(snap1name, B_FALSE);
9babb374 3780 if (error && error != ENOENT)
13fe0198 3781 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
40b84e7a 3782
eca7b760
IK
3783 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3784 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3785 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3786 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3787 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
9babb374
BB
3788}
3789
3790/*
3791 * Verify dsl_dataset_promote handles EBUSY
3792 */
3793void
428870ff 3794ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
9babb374 3795{
13fe0198 3796 objset_t *os;
40b84e7a
BB
3797 char *snap1name;
3798 char *clone1name;
3799 char *snap2name;
3800 char *clone2name;
3801 char *snap3name;
428870ff
BB
3802 char *osname = zd->zd_name;
3803 int error;
9babb374 3804
eca7b760
IK
3805 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3806 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3807 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3808 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3809 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
40b84e7a 3810
7809eb8b 3811 (void) rw_rdlock(&ztest_name_lock);
9babb374 3812
428870ff 3813 ztest_dsl_dataset_cleanup(osname, id);
9babb374 3814
eca7b760
IK
3815 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3816 "%s@s1_%llu", osname, (u_longlong_t)id);
3817 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3818 "%s/c1_%llu", osname, (u_longlong_t)id);
3819 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3820 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3821 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3822 "%s/c2_%llu", osname, (u_longlong_t)id);
3823 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3824 "%s@s3_%llu", clone1name, (u_longlong_t)id);
9babb374 3825
6f1ffb06 3826 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
9babb374
BB
3827 if (error && error != EEXIST) {
3828 if (error == ENOSPC) {
428870ff 3829 ztest_record_enospc(FTAG);
9babb374
BB
3830 goto out;
3831 }
3832 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3833 }
3834
13fe0198 3835 error = dmu_objset_clone(clone1name, snap1name);
9babb374
BB
3836 if (error) {
3837 if (error == ENOSPC) {
428870ff 3838 ztest_record_enospc(FTAG);
9babb374
BB
3839 goto out;
3840 }
3841 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3842 }
3843
6f1ffb06 3844 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
428870ff
BB
3845 if (error && error != EEXIST) {
3846 if (error == ENOSPC) {
3847 ztest_record_enospc(FTAG);
3848 goto out;
34dc7c2f 3849 }
428870ff
BB
3850 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3851 }
34dc7c2f 3852
6f1ffb06 3853 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
428870ff
BB
3854 if (error && error != EEXIST) {
3855 if (error == ENOSPC) {
3856 ztest_record_enospc(FTAG);
3857 goto out;
3858 }
3859 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3860 }
34dc7c2f 3861
13fe0198 3862 error = dmu_objset_clone(clone2name, snap3name);
428870ff
BB
3863 if (error) {
3864 if (error == ENOSPC) {
3865 ztest_record_enospc(FTAG);
3866 goto out;
3867 }
3868 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3869 }
34dc7c2f 3870
b5256303
TC
3871 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
3872 FTAG, &os);
428870ff 3873 if (error)
13fe0198 3874 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
428870ff 3875 error = dsl_dataset_promote(clone2name, NULL);
9b67f605 3876 if (error == ENOSPC) {
b5256303 3877 dmu_objset_disown(os, B_TRUE, FTAG);
9b67f605
MA
3878 ztest_record_enospc(FTAG);
3879 goto out;
3880 }
428870ff
BB
3881 if (error != EBUSY)
3882 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3883 error);
b5256303 3884 dmu_objset_disown(os, B_TRUE, FTAG);
34dc7c2f 3885
428870ff
BB
3886out:
3887 ztest_dsl_dataset_cleanup(osname, id);
34dc7c2f 3888
7809eb8b 3889 (void) rw_unlock(&ztest_name_lock);
40b84e7a 3890
eca7b760
IK
3891 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3892 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3893 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3894 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3895 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
428870ff 3896}
34dc7c2f 3897
40b84e7a 3898#undef OD_ARRAY_SIZE
d1d7e268 3899#define OD_ARRAY_SIZE 4
40b84e7a 3900
428870ff
BB
3901/*
3902 * Verify that dmu_object_{alloc,free} work as expected.
3903 */
3904void
3905ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3906{
40b84e7a
BB
3907 ztest_od_t *od;
3908 int batchsize;
3909 int size;
d6320ddb 3910 int b;
34dc7c2f 3911
d1d7e268 3912 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
3913 od = umem_alloc(size, UMEM_NOFAIL);
3914 batchsize = OD_ARRAY_SIZE;
3915
d6320ddb 3916 for (b = 0; b < batchsize; b++)
50c957f7
NB
3917 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3918 0, 0, 0);
34dc7c2f 3919
428870ff
BB
3920 /*
3921 * Destroy the previous batch of objects, create a new batch,
3922 * and do some I/O on the new objects.
3923 */
40b84e7a 3924 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
428870ff 3925 return;
34dc7c2f 3926
428870ff
BB
3927 while (ztest_random(4 * batchsize) != 0)
3928 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3929 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
40b84e7a
BB
3930
3931 umem_free(od, size);
34dc7c2f
BB
3932}
3933
40b84e7a 3934#undef OD_ARRAY_SIZE
d1d7e268 3935#define OD_ARRAY_SIZE 2
40b84e7a 3936
34dc7c2f
BB
3937/*
3938 * Verify that dmu_{read,write} work as expected.
3939 */
34dc7c2f 3940void
428870ff 3941ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
34dc7c2f 3942{
40b84e7a
BB
3943 int size;
3944 ztest_od_t *od;
3945
428870ff 3946 objset_t *os = zd->zd_os;
d1d7e268 3947 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a 3948 od = umem_alloc(size, UMEM_NOFAIL);
34dc7c2f
BB
3949 dmu_tx_t *tx;
3950 int i, freeit, error;
3951 uint64_t n, s, txg;
3952 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
428870ff
BB
3953 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3954 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
34dc7c2f
BB
3955 uint64_t regions = 997;
3956 uint64_t stride = 123456789ULL;
3957 uint64_t width = 40;
3958 int free_percent = 5;
3959
3960 /*
3961 * This test uses two objects, packobj and bigobj, that are always
3962 * updated together (i.e. in the same tx) so that their contents are
3963 * in sync and can be compared. Their contents relate to each other
3964 * in a simple way: packobj is a dense array of 'bufwad' structures,
3965 * while bigobj is a sparse array of the same bufwads. Specifically,
3966 * for any index n, there are three bufwads that should be identical:
3967 *
3968 * packobj, at offset n * sizeof (bufwad_t)
3969 * bigobj, at the head of the nth chunk
3970 * bigobj, at the tail of the nth chunk
3971 *
3972 * The chunk size is arbitrary. It doesn't have to be a power of two,
3973 * and it doesn't have any relation to the object blocksize.
3974 * The only requirement is that it can hold at least two bufwads.
3975 *
3976 * Normally, we write the bufwad to each of these locations.
3977 * However, free_percent of the time we instead write zeroes to
3978 * packobj and perform a dmu_free_range() on bigobj. By comparing
3979 * bigobj to packobj, we can verify that the DMU is correctly
3980 * tracking which parts of an object are allocated and free,
3981 * and that the contents of the allocated blocks are correct.
3982 */
3983
3984 /*
3985 * Read the directory info. If it's the first time, set things up.
3986 */
50c957f7
NB
3987 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3988 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3989 chunksize);
34dc7c2f 3990
40b84e7a
BB
3991 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3992 umem_free(od, size);
428870ff 3993 return;
40b84e7a 3994 }
34dc7c2f 3995
428870ff
BB
3996 bigobj = od[0].od_object;
3997 packobj = od[1].od_object;
3998 chunksize = od[0].od_gen;
3999 ASSERT(chunksize == od[1].od_gen);
34dc7c2f
BB
4000
4001 /*
4002 * Prefetch a random chunk of the big object.
4003 * Our aim here is to get some async reads in flight
4004 * for blocks that we may free below; the DMU should
4005 * handle this race correctly.
4006 */
4007 n = ztest_random(regions) * stride + ztest_random(width);
4008 s = 1 + ztest_random(2 * width - 1);
fcff0f35
PD
4009 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4010 ZIO_PRIORITY_SYNC_READ);
34dc7c2f
BB
4011
4012 /*
4013 * Pick a random index and compute the offsets into packobj and bigobj.
4014 */
4015 n = ztest_random(regions) * stride + ztest_random(width);
4016 s = 1 + ztest_random(width - 1);
4017
4018 packoff = n * sizeof (bufwad_t);
4019 packsize = s * sizeof (bufwad_t);
4020
428870ff
BB
4021 bigoff = n * chunksize;
4022 bigsize = s * chunksize;
34dc7c2f
BB
4023
4024 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4025 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4026
4027 /*
4028 * free_percent of the time, free a range of bigobj rather than
4029 * overwriting it.
4030 */
4031 freeit = (ztest_random(100) < free_percent);
4032
4033 /*
4034 * Read the current contents of our objects.
4035 */
428870ff 4036 error = dmu_read(os, packobj, packoff, packsize, packbuf,
9babb374 4037 DMU_READ_PREFETCH);
c99c9001 4038 ASSERT0(error);
428870ff 4039 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
9babb374 4040 DMU_READ_PREFETCH);
c99c9001 4041 ASSERT0(error);
34dc7c2f
BB
4042
4043 /*
4044 * Get a tx for the mods to both packobj and bigobj.
4045 */
4046 tx = dmu_tx_create(os);
4047
428870ff 4048 dmu_tx_hold_write(tx, packobj, packoff, packsize);
34dc7c2f
BB
4049
4050 if (freeit)
428870ff 4051 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
34dc7c2f 4052 else
428870ff 4053 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
34dc7c2f 4054
383fc4a9
MA
4055 /* This accounts for setting the checksum/compression. */
4056 dmu_tx_hold_bonus(tx, bigobj);
4057
428870ff
BB
4058 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4059 if (txg == 0) {
34dc7c2f
BB
4060 umem_free(packbuf, packsize);
4061 umem_free(bigbuf, bigsize);
40b84e7a 4062 umem_free(od, size);
34dc7c2f
BB
4063 return;
4064 }
4065
9b67f605
MA
4066 enum zio_checksum cksum;
4067 do {
4068 cksum = (enum zio_checksum)
4069 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4070 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4071 dmu_object_set_checksum(os, bigobj, cksum, tx);
428870ff 4072
9b67f605
MA
4073 enum zio_compress comp;
4074 do {
4075 comp = (enum zio_compress)
4076 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4077 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4078 dmu_object_set_compress(os, bigobj, comp, tx);
34dc7c2f
BB
4079
4080 /*
4081 * For each index from n to n + s, verify that the existing bufwad
4082 * in packobj matches the bufwads at the head and tail of the
4083 * corresponding chunk in bigobj. Then update all three bufwads
4084 * with the new values we want to write out.
4085 */
4086 for (i = 0; i < s; i++) {
4087 /* LINTED */
4088 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4089 /* LINTED */
428870ff 4090 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
34dc7c2f 4091 /* LINTED */
428870ff 4092 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
34dc7c2f
BB
4093
4094 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4095 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4096
4097 if (pack->bw_txg > txg)
4098 fatal(0, "future leak: got %llx, open txg is %llx",
4099 pack->bw_txg, txg);
4100
4101 if (pack->bw_data != 0 && pack->bw_index != n + i)
4102 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4103 pack->bw_index, n, i);
4104
4105 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4106 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4107
4108 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4109 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4110
4111 if (freeit) {
4112 bzero(pack, sizeof (bufwad_t));
4113 } else {
4114 pack->bw_index = n + i;
4115 pack->bw_txg = txg;
4116 pack->bw_data = 1 + ztest_random(-2ULL);
4117 }
4118 *bigH = *pack;
4119 *bigT = *pack;
4120 }
4121
4122 /*
4123 * We've verified all the old bufwads, and made new ones.
4124 * Now write them out.
4125 */
428870ff 4126 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
34dc7c2f
BB
4127
4128 if (freeit) {
c242c188 4129 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4130 (void) printf("freeing offset %llx size %llx"
4131 " txg %llx\n",
4132 (u_longlong_t)bigoff,
4133 (u_longlong_t)bigsize,
4134 (u_longlong_t)txg);
4135 }
428870ff 4136 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
34dc7c2f 4137 } else {
c242c188 4138 if (ztest_opts.zo_verbose >= 7) {
34dc7c2f
BB
4139 (void) printf("writing offset %llx size %llx"
4140 " txg %llx\n",
4141 (u_longlong_t)bigoff,
4142 (u_longlong_t)bigsize,
4143 (u_longlong_t)txg);
4144 }
428870ff 4145 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
34dc7c2f
BB
4146 }
4147
4148 dmu_tx_commit(tx);
4149
4150 /*
4151 * Sanity check the stuff we just wrote.
4152 */
4153 {
4154 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4155 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4156
428870ff 4157 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4158 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4159 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374 4160 bigsize, bigcheck, DMU_READ_PREFETCH));
34dc7c2f
BB
4161
4162 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4163 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4164
4165 umem_free(packcheck, packsize);
4166 umem_free(bigcheck, bigsize);
4167 }
4168
4169 umem_free(packbuf, packsize);
4170 umem_free(bigbuf, bigsize);
40b84e7a 4171 umem_free(od, size);
34dc7c2f
BB
4172}
4173
9babb374
BB
4174void
4175compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
428870ff 4176 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
9babb374
BB
4177{
4178 uint64_t i;
4179 bufwad_t *pack;
4180 bufwad_t *bigH;
4181 bufwad_t *bigT;
4182
4183 /*
4184 * For each index from n to n + s, verify that the existing bufwad
4185 * in packobj matches the bufwads at the head and tail of the
4186 * corresponding chunk in bigobj. Then update all three bufwads
4187 * with the new values we want to write out.
4188 */
4189 for (i = 0; i < s; i++) {
4190 /* LINTED */
4191 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4192 /* LINTED */
428870ff 4193 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
9babb374 4194 /* LINTED */
428870ff 4195 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
9babb374
BB
4196
4197 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4198 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4199
4200 if (pack->bw_txg > txg)
4201 fatal(0, "future leak: got %llx, open txg is %llx",
4202 pack->bw_txg, txg);
4203
4204 if (pack->bw_data != 0 && pack->bw_index != n + i)
4205 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4206 pack->bw_index, n, i);
4207
4208 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4209 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4210
4211 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4212 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4213
4214 pack->bw_index = n + i;
4215 pack->bw_txg = txg;
4216 pack->bw_data = 1 + ztest_random(-2ULL);
4217
4218 *bigH = *pack;
4219 *bigT = *pack;
4220 }
4221}
4222
40b84e7a 4223#undef OD_ARRAY_SIZE
d1d7e268 4224#define OD_ARRAY_SIZE 2
40b84e7a 4225
9babb374 4226void
428870ff 4227ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
9babb374 4228{
428870ff 4229 objset_t *os = zd->zd_os;
40b84e7a 4230 ztest_od_t *od;
9babb374
BB
4231 dmu_tx_t *tx;
4232 uint64_t i;
4233 int error;
40b84e7a 4234 int size;
9babb374
BB
4235 uint64_t n, s, txg;
4236 bufwad_t *packbuf, *bigbuf;
428870ff
BB
4237 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4238 uint64_t blocksize = ztest_random_blocksize();
4239 uint64_t chunksize = blocksize;
9babb374
BB
4240 uint64_t regions = 997;
4241 uint64_t stride = 123456789ULL;
4242 uint64_t width = 9;
4243 dmu_buf_t *bonus_db;
4244 arc_buf_t **bigbuf_arcbufs;
428870ff 4245 dmu_object_info_t doi;
9babb374 4246
d1d7e268 4247 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
40b84e7a
BB
4248 od = umem_alloc(size, UMEM_NOFAIL);
4249
9babb374
BB
4250 /*
4251 * This test uses two objects, packobj and bigobj, that are always
4252 * updated together (i.e. in the same tx) so that their contents are
4253 * in sync and can be compared. Their contents relate to each other
4254 * in a simple way: packobj is a dense array of 'bufwad' structures,
4255 * while bigobj is a sparse array of the same bufwads. Specifically,
4256 * for any index n, there are three bufwads that should be identical:
4257 *
4258 * packobj, at offset n * sizeof (bufwad_t)
4259 * bigobj, at the head of the nth chunk
4260 * bigobj, at the tail of the nth chunk
4261 *
4262 * The chunk size is set equal to bigobj block size so that
4263 * dmu_assign_arcbuf() can be tested for object updates.
4264 */
4265
4266 /*
4267 * Read the directory info. If it's the first time, set things up.
4268 */
50c957f7
NB
4269 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4270 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4271 chunksize);
40b84e7a 4272
9babb374 4273
40b84e7a
BB
4274 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4275 umem_free(od, size);
428870ff 4276 return;
40b84e7a 4277 }
9babb374 4278
428870ff
BB
4279 bigobj = od[0].od_object;
4280 packobj = od[1].od_object;
4281 blocksize = od[0].od_blocksize;
4282 chunksize = blocksize;
4283 ASSERT(chunksize == od[1].od_gen);
9babb374 4284
428870ff
BB
4285 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4286 VERIFY(ISP2(doi.doi_data_block_size));
4287 VERIFY(chunksize == doi.doi_data_block_size);
4288 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
9babb374
BB
4289
4290 /*
4291 * Pick a random index and compute the offsets into packobj and bigobj.
4292 */
4293 n = ztest_random(regions) * stride + ztest_random(width);
4294 s = 1 + ztest_random(width - 1);
4295
4296 packoff = n * sizeof (bufwad_t);
4297 packsize = s * sizeof (bufwad_t);
4298
428870ff
BB
4299 bigoff = n * chunksize;
4300 bigsize = s * chunksize;
9babb374
BB
4301
4302 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4303 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4304
428870ff 4305 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
9babb374
BB
4306
4307 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4308
4309 /*
4310 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4311 * Iteration 1 test zcopy to already referenced dbufs.
4312 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4313 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4314 * Iteration 4 test zcopy when dbuf is no longer dirty.
4315 * Iteration 5 test zcopy when it can't be done.
4316 * Iteration 6 one more zcopy write.
4317 */
4318 for (i = 0; i < 7; i++) {
4319 uint64_t j;
4320 uint64_t off;
4321
4322 /*
4323 * In iteration 5 (i == 5) use arcbufs
4324 * that don't match bigobj blksz to test
4325 * dmu_assign_arcbuf() when it can't directly
4326 * assign an arcbuf to a dbuf.
4327 */
4328 for (j = 0; j < s; j++) {
b9541d6b 4329 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4330 bigbuf_arcbufs[j] =
428870ff 4331 dmu_request_arcbuf(bonus_db, chunksize);
9babb374
BB
4332 } else {
4333 bigbuf_arcbufs[2 * j] =
428870ff 4334 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374 4335 bigbuf_arcbufs[2 * j + 1] =
428870ff 4336 dmu_request_arcbuf(bonus_db, chunksize / 2);
9babb374
BB
4337 }
4338 }
4339
4340 /*
4341 * Get a tx for the mods to both packobj and bigobj.
4342 */
4343 tx = dmu_tx_create(os);
4344
428870ff
BB
4345 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4346 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
9babb374 4347
428870ff
BB
4348 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4349 if (txg == 0) {
9babb374
BB
4350 umem_free(packbuf, packsize);
4351 umem_free(bigbuf, bigsize);
4352 for (j = 0; j < s; j++) {
b9541d6b
CW
4353 if (i != 5 ||
4354 chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4355 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4356 } else {
4357 dmu_return_arcbuf(
4358 bigbuf_arcbufs[2 * j]);
4359 dmu_return_arcbuf(
4360 bigbuf_arcbufs[2 * j + 1]);
4361 }
4362 }
4363 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4364 umem_free(od, size);
9babb374
BB
4365 dmu_buf_rele(bonus_db, FTAG);
4366 return;
4367 }
4368
9babb374
BB
4369 /*
4370 * 50% of the time don't read objects in the 1st iteration to
4371 * test dmu_assign_arcbuf() for the case when there're no
4372 * existing dbufs for the specified offsets.
4373 */
4374 if (i != 0 || ztest_random(2) != 0) {
428870ff 4375 error = dmu_read(os, packobj, packoff,
9babb374 4376 packsize, packbuf, DMU_READ_PREFETCH);
c99c9001 4377 ASSERT0(error);
428870ff 4378 error = dmu_read(os, bigobj, bigoff, bigsize,
9babb374 4379 bigbuf, DMU_READ_PREFETCH);
c99c9001 4380 ASSERT0(error);
9babb374
BB
4381 }
4382 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
428870ff 4383 n, chunksize, txg);
9babb374
BB
4384
4385 /*
4386 * We've verified all the old bufwads, and made new ones.
4387 * Now write them out.
4388 */
428870ff 4389 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
c242c188 4390 if (ztest_opts.zo_verbose >= 7) {
9babb374
BB
4391 (void) printf("writing offset %llx size %llx"
4392 " txg %llx\n",
4393 (u_longlong_t)bigoff,
4394 (u_longlong_t)bigsize,
4395 (u_longlong_t)txg);
4396 }
428870ff 4397 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
9babb374 4398 dmu_buf_t *dbt;
b9541d6b 4399 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374 4400 bcopy((caddr_t)bigbuf + (off - bigoff),
428870ff 4401 bigbuf_arcbufs[j]->b_data, chunksize);
9babb374
BB
4402 } else {
4403 bcopy((caddr_t)bigbuf + (off - bigoff),
4404 bigbuf_arcbufs[2 * j]->b_data,
428870ff 4405 chunksize / 2);
9babb374 4406 bcopy((caddr_t)bigbuf + (off - bigoff) +
428870ff 4407 chunksize / 2,
9babb374 4408 bigbuf_arcbufs[2 * j + 1]->b_data,
428870ff 4409 chunksize / 2);
9babb374
BB
4410 }
4411
4412 if (i == 1) {
428870ff
BB
4413 VERIFY(dmu_buf_hold(os, bigobj, off,
4414 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
9babb374 4415 }
b9541d6b 4416 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
9babb374
BB
4417 dmu_assign_arcbuf(bonus_db, off,
4418 bigbuf_arcbufs[j], tx);
4419 } else {
4420 dmu_assign_arcbuf(bonus_db, off,
4421 bigbuf_arcbufs[2 * j], tx);
4422 dmu_assign_arcbuf(bonus_db,
428870ff 4423 off + chunksize / 2,
9babb374
BB
4424 bigbuf_arcbufs[2 * j + 1], tx);
4425 }
4426 if (i == 1) {
4427 dmu_buf_rele(dbt, FTAG);
4428 }
4429 }
4430 dmu_tx_commit(tx);
4431
4432 /*
4433 * Sanity check the stuff we just wrote.
4434 */
4435 {
4436 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4437 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4438
428870ff 4439 VERIFY(0 == dmu_read(os, packobj, packoff,
9babb374 4440 packsize, packcheck, DMU_READ_PREFETCH));
428870ff 4441 VERIFY(0 == dmu_read(os, bigobj, bigoff,
9babb374
BB
4442 bigsize, bigcheck, DMU_READ_PREFETCH));
4443
4444 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4445 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4446
4447 umem_free(packcheck, packsize);
4448 umem_free(bigcheck, bigsize);
4449 }
4450 if (i == 2) {
4451 txg_wait_open(dmu_objset_pool(os), 0);
4452 } else if (i == 3) {
4453 txg_wait_synced(dmu_objset_pool(os), 0);
4454 }
4455 }
4456
4457 dmu_buf_rele(bonus_db, FTAG);
4458 umem_free(packbuf, packsize);
4459 umem_free(bigbuf, bigsize);
4460 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
40b84e7a 4461 umem_free(od, size);
9babb374
BB
4462}
4463
428870ff 4464/* ARGSUSED */
34dc7c2f 4465void
428870ff 4466ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4467{
40b84e7a
BB
4468 ztest_od_t *od;
4469
d1d7e268 4470 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
428870ff
BB
4471 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4472 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
34dc7c2f
BB
4473
4474 /*
428870ff
BB
4475 * Have multiple threads write to large offsets in an object
4476 * to verify that parallel writes to an object -- even to the
4477 * same blocks within the object -- doesn't cause any trouble.
34dc7c2f 4478 */
50c957f7 4479 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
9babb374 4480
40b84e7a 4481 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
34dc7c2f 4482 return;
34dc7c2f 4483
428870ff 4484 while (ztest_random(10) != 0)
40b84e7a
BB
4485 ztest_io(zd, od->od_object, offset);
4486
d1d7e268 4487 umem_free(od, sizeof (ztest_od_t));
428870ff 4488}
34dc7c2f 4489
428870ff
BB
4490void
4491ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4492{
40b84e7a 4493 ztest_od_t *od;
428870ff
BB
4494 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4495 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4496 uint64_t count = ztest_random(20) + 1;
4497 uint64_t blocksize = ztest_random_blocksize();
4498 void *data;
34dc7c2f 4499
d1d7e268 4500 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
34dc7c2f 4501
50c957f7 4502 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
40b84e7a 4503
d1d7e268
MK
4504 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4505 !ztest_random(2)) != 0) {
4506 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4507 return;
40b84e7a 4508 }
34dc7c2f 4509
40b84e7a 4510 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
d1d7e268 4511 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4512 return;
40b84e7a 4513 }
34dc7c2f 4514
40b84e7a 4515 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
34dc7c2f 4516
428870ff 4517 data = umem_zalloc(blocksize, UMEM_NOFAIL);
34dc7c2f 4518
428870ff
BB
4519 while (ztest_random(count) != 0) {
4520 uint64_t randoff = offset + (ztest_random(count) * blocksize);
40b84e7a 4521 if (ztest_write(zd, od->od_object, randoff, blocksize,
428870ff
BB
4522 data) != 0)
4523 break;
4524 while (ztest_random(4) != 0)
40b84e7a 4525 ztest_io(zd, od->od_object, randoff);
9babb374 4526 }
34dc7c2f 4527
428870ff 4528 umem_free(data, blocksize);
d1d7e268 4529 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4530}
4531
4532/*
4533 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4534 */
4535#define ZTEST_ZAP_MIN_INTS 1
4536#define ZTEST_ZAP_MAX_INTS 4
4537#define ZTEST_ZAP_MAX_PROPS 1000
4538
4539void
428870ff 4540ztest_zap(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4541{
428870ff 4542 objset_t *os = zd->zd_os;
40b84e7a 4543 ztest_od_t *od;
34dc7c2f
BB
4544 uint64_t object;
4545 uint64_t txg, last_txg;
4546 uint64_t value[ZTEST_ZAP_MAX_INTS];
4547 uint64_t zl_ints, zl_intsize, prop;
4548 int i, ints;
4549 dmu_tx_t *tx;
4550 char propname[100], txgname[100];
4551 int error;
34dc7c2f
BB
4552 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4553
d1d7e268 4554 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4555 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
34dc7c2f 4556
40b84e7a 4557 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
02730c33 4558 !ztest_random(2)) != 0)
40b84e7a 4559 goto out;
34dc7c2f 4560
40b84e7a 4561 object = od->od_object;
34dc7c2f 4562
428870ff
BB
4563 /*
4564 * Generate a known hash collision, and verify that
4565 * we can lookup and remove both entries.
4566 */
4567 tx = dmu_tx_create(os);
4568 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4569 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4570 if (txg == 0)
40b84e7a 4571 goto out;
428870ff
BB
4572 for (i = 0; i < 2; i++) {
4573 value[i] = i;
4574 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4575 1, &value[i], tx));
4576 }
4577 for (i = 0; i < 2; i++) {
4578 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4579 sizeof (uint64_t), 1, &value[i], tx));
4580 VERIFY3U(0, ==,
4581 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4582 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4583 ASSERT3U(zl_ints, ==, 1);
4584 }
4585 for (i = 0; i < 2; i++) {
4586 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
34dc7c2f 4587 }
428870ff 4588 dmu_tx_commit(tx);
34dc7c2f 4589
428870ff
BB
4590 /*
4591 * Generate a buch of random entries.
4592 */
34dc7c2f
BB
4593 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4594
4595 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4596 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4597 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4598 bzero(value, sizeof (value));
4599 last_txg = 0;
4600
4601 /*
4602 * If these zap entries already exist, validate their contents.
4603 */
4604 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4605 if (error == 0) {
4606 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4607 ASSERT3U(zl_ints, ==, 1);
4608
4609 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4610 zl_ints, &last_txg) == 0);
4611
4612 VERIFY(zap_length(os, object, propname, &zl_intsize,
4613 &zl_ints) == 0);
4614
4615 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4616 ASSERT3U(zl_ints, ==, ints);
4617
4618 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4619 zl_ints, value) == 0);
4620
4621 for (i = 0; i < ints; i++) {
4622 ASSERT3U(value[i], ==, last_txg + object + i);
4623 }
4624 } else {
4625 ASSERT3U(error, ==, ENOENT);
4626 }
4627
4628 /*
4629 * Atomically update two entries in our zap object.
4630 * The first is named txg_%llu, and contains the txg
4631 * in which the property was last updated. The second
4632 * is named prop_%llu, and the nth element of its value
4633 * should be txg + object + n.
4634 */
4635 tx = dmu_tx_create(os);
428870ff
BB
4636 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4637 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4638 if (txg == 0)
40b84e7a 4639 goto out;
34dc7c2f
BB
4640
4641 if (last_txg > txg)
4642 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4643
4644 for (i = 0; i < ints; i++)
4645 value[i] = txg + object + i;
4646
428870ff
BB
4647 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4648 1, &txg, tx));
4649 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4650 ints, value, tx));
34dc7c2f
BB
4651
4652 dmu_tx_commit(tx);
4653
4654 /*
4655 * Remove a random pair of entries.
4656 */
4657 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4658 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4659 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4660
4661 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4662
4663 if (error == ENOENT)
40b84e7a 4664 goto out;
34dc7c2f 4665
c99c9001 4666 ASSERT0(error);
34dc7c2f
BB
4667
4668 tx = dmu_tx_create(os);
428870ff
BB
4669 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4670 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4671 if (txg == 0)
40b84e7a 4672 goto out;
428870ff
BB
4673 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4674 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4675 dmu_tx_commit(tx);
40b84e7a 4676out:
d1d7e268 4677 umem_free(od, sizeof (ztest_od_t));
428870ff 4678}
34dc7c2f 4679
428870ff
BB
4680/*
4681 * Testcase to test the upgrading of a microzap to fatzap.
4682 */
4683void
4684ztest_fzap(ztest_ds_t *zd, uint64_t id)
4685{
4686 objset_t *os = zd->zd_os;
40b84e7a 4687 ztest_od_t *od;
428870ff 4688 uint64_t object, txg;
d6320ddb 4689 int i;
34dc7c2f 4690
d1d7e268 4691 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4692 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4693
40b84e7a 4694 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
02730c33 4695 !ztest_random(2)) != 0)
40b84e7a
BB
4696 goto out;
4697 object = od->od_object;
34dc7c2f
BB
4698
4699 /*
428870ff
BB
4700 * Add entries to this ZAP and make sure it spills over
4701 * and gets upgraded to a fatzap. Also, since we are adding
4702 * 2050 entries we should see ptrtbl growth and leaf-block split.
34dc7c2f 4703 */
d6320ddb 4704 for (i = 0; i < 2050; i++) {
eca7b760 4705 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
4706 uint64_t value = i;
4707 dmu_tx_t *tx;
4708 int error;
34dc7c2f 4709
428870ff 4710 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
b8864a23 4711 (u_longlong_t)id, (u_longlong_t)value);
428870ff
BB
4712
4713 tx = dmu_tx_create(os);
4714 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4715 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4716 if (txg == 0)
40b84e7a 4717 goto out;
428870ff
BB
4718 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4719 &value, tx);
4720 ASSERT(error == 0 || error == EEXIST);
4721 dmu_tx_commit(tx);
34dc7c2f 4722 }
40b84e7a 4723out:
d1d7e268 4724 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4725}
4726
428870ff 4727/* ARGSUSED */
34dc7c2f 4728void
428870ff 4729ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
34dc7c2f 4730{
428870ff 4731 objset_t *os = zd->zd_os;
40b84e7a 4732 ztest_od_t *od;
34dc7c2f
BB
4733 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4734 dmu_tx_t *tx;
4735 int i, namelen, error;
428870ff 4736 int micro = ztest_random(2);
34dc7c2f
BB
4737 char name[20], string_value[20];
4738 void *data;
4739
d1d7e268 4740 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4741 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
428870ff 4742
40b84e7a 4743 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 4744 umem_free(od, sizeof (ztest_od_t));
428870ff 4745 return;
40b84e7a 4746 }
428870ff 4747
40b84e7a 4748 object = od->od_object;
428870ff 4749
34dc7c2f
BB
4750 /*
4751 * Generate a random name of the form 'xxx.....' where each
4752 * x is a random printable character and the dots are dots.
4753 * There are 94 such characters, and the name length goes from
4754 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4755 */
4756 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4757
4758 for (i = 0; i < 3; i++)
4759 name[i] = '!' + ztest_random('~' - '!' + 1);
4760 for (; i < namelen - 1; i++)
4761 name[i] = '.';
4762 name[i] = '\0';
4763
428870ff 4764 if ((namelen & 1) || micro) {
34dc7c2f
BB
4765 wsize = sizeof (txg);
4766 wc = 1;
4767 data = &txg;
4768 } else {
4769 wsize = 1;
4770 wc = namelen;
4771 data = string_value;
4772 }
4773
4774 count = -1ULL;
13fe0198 4775 VERIFY0(zap_count(os, object, &count));
34dc7c2f
BB
4776 ASSERT(count != -1ULL);
4777
4778 /*
4779 * Select an operation: length, lookup, add, update, remove.
4780 */
4781 i = ztest_random(5);
4782
4783 if (i >= 2) {
4784 tx = dmu_tx_create(os);
428870ff
BB
4785 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4786 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
df053d67
GN
4787 if (txg == 0) {
4788 umem_free(od, sizeof (ztest_od_t));
34dc7c2f 4789 return;
df053d67 4790 }
34dc7c2f
BB
4791 bcopy(name, string_value, namelen);
4792 } else {
4793 tx = NULL;
4794 txg = 0;
4795 bzero(string_value, namelen);
4796 }
4797
4798 switch (i) {
4799
4800 case 0:
4801 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4802 if (error == 0) {
4803 ASSERT3U(wsize, ==, zl_wsize);
4804 ASSERT3U(wc, ==, zl_wc);
4805 } else {
4806 ASSERT3U(error, ==, ENOENT);
4807 }
4808 break;
4809
4810 case 1:
4811 error = zap_lookup(os, object, name, wsize, wc, data);
4812 if (error == 0) {
4813 if (data == string_value &&
4814 bcmp(name, data, namelen) != 0)
4815 fatal(0, "name '%s' != val '%s' len %d",
4816 name, data, namelen);
4817 } else {
4818 ASSERT3U(error, ==, ENOENT);
4819 }
4820 break;
4821
4822 case 2:
4823 error = zap_add(os, object, name, wsize, wc, data, tx);
4824 ASSERT(error == 0 || error == EEXIST);
4825 break;
4826
4827 case 3:
4828 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4829 break;
4830
4831 case 4:
4832 error = zap_remove(os, object, name, tx);
4833 ASSERT(error == 0 || error == ENOENT);
4834 break;
4835 }
4836
4837 if (tx != NULL)
4838 dmu_tx_commit(tx);
40b84e7a 4839
d1d7e268 4840 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
4841}
4842
428870ff
BB
4843/*
4844 * Commit callback data.
4845 */
4846typedef struct ztest_cb_data {
4847 list_node_t zcd_node;
4848 uint64_t zcd_txg;
4849 int zcd_expected_err;
4850 boolean_t zcd_added;
4851 boolean_t zcd_called;
4852 spa_t *zcd_spa;
4853} ztest_cb_data_t;
4854
4855/* This is the actual commit callback function */
4856static void
4857ztest_commit_callback(void *arg, int error)
4858{
4859 ztest_cb_data_t *data = arg;
4860 uint64_t synced_txg;
4861
4862 VERIFY(data != NULL);
4863 VERIFY3S(data->zcd_expected_err, ==, error);
4864 VERIFY(!data->zcd_called);
4865
4866 synced_txg = spa_last_synced_txg(data->zcd_spa);
4867 if (data->zcd_txg > synced_txg)
4868 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4869 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4870 synced_txg);
4871
4872 data->zcd_called = B_TRUE;
4873
4874 if (error == ECANCELED) {
c99c9001 4875 ASSERT0(data->zcd_txg);
428870ff
BB
4876 ASSERT(!data->zcd_added);
4877
4878 /*
4879 * The private callback data should be destroyed here, but
4880 * since we are going to check the zcd_called field after
4881 * dmu_tx_abort(), we will destroy it there.
4882 */
4883 return;
4884 }
4885
090ff092 4886 ASSERT(data->zcd_added);
428870ff
BB
4887 ASSERT3U(data->zcd_txg, !=, 0);
4888
1e33ac1e 4889 (void) mutex_enter(&zcl.zcl_callbacks_lock);
090ff092
RC
4890
4891 /* See if this cb was called more quickly */
4892 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4893 zc_min_txg_delay = synced_txg - data->zcd_txg;
4894
4895 /* Remove our callback from the list */
428870ff 4896 list_remove(&zcl.zcl_callbacks, data);
090ff092 4897
1e33ac1e 4898 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff 4899
428870ff
BB
4900 umem_free(data, sizeof (ztest_cb_data_t));
4901}
4902
4903/* Allocate and initialize callback data structure */
4904static ztest_cb_data_t *
4905ztest_create_cb_data(objset_t *os, uint64_t txg)
4906{
4907 ztest_cb_data_t *cb_data;
4908
4909 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4910
4911 cb_data->zcd_txg = txg;
4912 cb_data->zcd_spa = dmu_objset_spa(os);
1e33ac1e 4913 list_link_init(&cb_data->zcd_node);
428870ff
BB
4914
4915 return (cb_data);
4916}
4917
428870ff
BB
4918/*
4919 * Commit callback test.
4920 */
34dc7c2f 4921void
428870ff
BB
4922ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4923{
4924 objset_t *os = zd->zd_os;
40b84e7a 4925 ztest_od_t *od;
428870ff
BB
4926 dmu_tx_t *tx;
4927 ztest_cb_data_t *cb_data[3], *tmp_cb;
4928 uint64_t old_txg, txg;
090ff092 4929 int i, error = 0;
428870ff 4930
d1d7e268 4931 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 4932 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
428870ff 4933
40b84e7a 4934 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 4935 umem_free(od, sizeof (ztest_od_t));
428870ff 4936 return;
40b84e7a 4937 }
428870ff
BB
4938
4939 tx = dmu_tx_create(os);
4940
4941 cb_data[0] = ztest_create_cb_data(os, 0);
4942 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4943
40b84e7a 4944 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
428870ff
BB
4945
4946 /* Every once in a while, abort the transaction on purpose */
4947 if (ztest_random(100) == 0)
4948 error = -1;
4949
4950 if (!error)
4951 error = dmu_tx_assign(tx, TXG_NOWAIT);
4952
4953 txg = error ? 0 : dmu_tx_get_txg(tx);
4954
4955 cb_data[0]->zcd_txg = txg;
4956 cb_data[1] = ztest_create_cb_data(os, txg);
4957 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4958
4959 if (error) {
4960 /*
4961 * It's not a strict requirement to call the registered
4962 * callbacks from inside dmu_tx_abort(), but that's what
4963 * it's supposed to happen in the current implementation
4964 * so we will check for that.
4965 */
4966 for (i = 0; i < 2; i++) {
4967 cb_data[i]->zcd_expected_err = ECANCELED;
4968 VERIFY(!cb_data[i]->zcd_called);
4969 }
4970
4971 dmu_tx_abort(tx);
4972
4973 for (i = 0; i < 2; i++) {
4974 VERIFY(cb_data[i]->zcd_called);
4975 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4976 }
4977
d1d7e268 4978 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
4979 return;
4980 }
4981
4982 cb_data[2] = ztest_create_cb_data(os, txg);
4983 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4984
4985 /*
4986 * Read existing data to make sure there isn't a future leak.
4987 */
40b84e7a 4988 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
428870ff
BB
4989 &old_txg, DMU_READ_PREFETCH));
4990
4991 if (old_txg > txg)
4992 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4993 old_txg, txg);
4994
40b84e7a 4995 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
428870ff 4996
1e33ac1e 4997 (void) mutex_enter(&zcl.zcl_callbacks_lock);
428870ff
BB
4998
4999 /*
5000 * Since commit callbacks don't have any ordering requirement and since
5001 * it is theoretically possible for a commit callback to be called
5002 * after an arbitrary amount of time has elapsed since its txg has been
5003 * synced, it is difficult to reliably determine whether a commit
5004 * callback hasn't been called due to high load or due to a flawed
5005 * implementation.
5006 *
5007 * In practice, we will assume that if after a certain number of txgs a
5008 * commit callback hasn't been called, then most likely there's an
5009 * implementation bug..
5010 */
5011 tmp_cb = list_head(&zcl.zcl_callbacks);
5012 if (tmp_cb != NULL &&
090ff092 5013 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
428870ff
BB
5014 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5015 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5016 }
5017
5018 /*
5019 * Let's find the place to insert our callbacks.
5020 *
5021 * Even though the list is ordered by txg, it is possible for the
5022 * insertion point to not be the end because our txg may already be
5023 * quiescing at this point and other callbacks in the open txg
5024 * (from other objsets) may have sneaked in.
5025 */
5026 tmp_cb = list_tail(&zcl.zcl_callbacks);
5027 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5028 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5029
5030 /* Add the 3 callbacks to the list */
5031 for (i = 0; i < 3; i++) {
5032 if (tmp_cb == NULL)
5033 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5034 else
5035 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5036 cb_data[i]);
5037
5038 cb_data[i]->zcd_added = B_TRUE;
5039 VERIFY(!cb_data[i]->zcd_called);
5040
5041 tmp_cb = cb_data[i];
5042 }
5043
090ff092
RC
5044 zc_cb_counter += 3;
5045
1e33ac1e 5046 (void) mutex_exit(&zcl.zcl_callbacks_lock);
428870ff
BB
5047
5048 dmu_tx_commit(tx);
40b84e7a 5049
d1d7e268 5050 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5051}
5052
50c957f7
NB
5053/*
5054 * Visit each object in the dataset. Verify that its properties
5055 * are consistent what was stored in the block tag when it was created,
5056 * and that its unused bonus buffer space has not been overwritten.
5057 */
817b1b6e 5058/* ARGSUSED */
50c957f7
NB
5059void
5060ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5061{
5062 objset_t *os = zd->zd_os;
5063 uint64_t obj;
5064 int err = 0;
5065
5066 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5067 ztest_block_tag_t *bt = NULL;
5068 dmu_object_info_t doi;
5069 dmu_buf_t *db;
5070
5071 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
5072 continue;
5073
5074 dmu_object_info_from_db(db, &doi);
5075 if (doi.doi_bonus_size >= sizeof (*bt))
5076 bt = ztest_bt_bonus(db);
5077
5078 if (bt && bt->bt_magic == BT_MAGIC) {
5079 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5080 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5081 bt->bt_crtxg);
5082 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5083 }
5084
5085 dmu_buf_rele(db, FTAG);
5086 }
5087}
5088
428870ff
BB
5089/* ARGSUSED */
5090void
5091ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5092{
5093 zfs_prop_t proplist[] = {
5094 ZFS_PROP_CHECKSUM,
5095 ZFS_PROP_COMPRESSION,
5096 ZFS_PROP_COPIES,
5097 ZFS_PROP_DEDUP
5098 };
d6320ddb 5099 int p;
428870ff 5100
7809eb8b 5101 (void) rw_rdlock(&ztest_name_lock);
428870ff 5102
d6320ddb 5103 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
428870ff
BB
5104 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5105 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5106
76d52067
BB
5107 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5108 ztest_random_blocksize(), (int)ztest_random(2)));
5109
7809eb8b 5110 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5111}
5112
5113/* ARGSUSED */
5114void
5115ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5116{
428870ff
BB
5117 nvlist_t *props = NULL;
5118
7809eb8b 5119 (void) rw_rdlock(&ztest_name_lock);
428870ff 5120
c242c188 5121 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
428870ff
BB
5122 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5123
c99c9001 5124 VERIFY0(spa_prop_get(ztest_spa, &props));
428870ff 5125
c242c188 5126 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
5127 dump_nvlist(props, 4);
5128
5129 nvlist_free(props);
5130
7809eb8b 5131 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
5132}
5133
13fe0198
MA
5134static int
5135user_release_one(const char *snapname, const char *holdname)
5136{
5137 nvlist_t *snaps, *holds;
5138 int error;
5139
5140 snaps = fnvlist_alloc();
5141 holds = fnvlist_alloc();
5142 fnvlist_add_boolean(holds, holdname);
5143 fnvlist_add_nvlist(snaps, snapname, holds);
5144 fnvlist_free(holds);
5145 error = dsl_dataset_user_release(snaps, NULL);
5146 fnvlist_free(snaps);
5147 return (error);
5148}
5149
428870ff
BB
5150/*
5151 * Test snapshot hold/release and deferred destroy.
5152 */
5153void
5154ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5155{
34dc7c2f 5156 int error;
428870ff
BB
5157 objset_t *os = zd->zd_os;
5158 objset_t *origin;
5159 char snapname[100];
5160 char fullname[100];
5161 char clonename[100];
5162 char tag[100];
eca7b760 5163 char osname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198 5164 nvlist_t *holds;
34dc7c2f 5165
7809eb8b 5166 (void) rw_rdlock(&ztest_name_lock);
34dc7c2f
BB
5167
5168 dmu_objset_name(os, osname);
5169
d1d7e268
MK
5170 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5171 (u_longlong_t)id);
13fe0198
MA
5172 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5173 (void) snprintf(clonename, sizeof (clonename),
d1d7e268
MK
5174 "%s/ch1_%llu", osname, (u_longlong_t)id);
5175 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
428870ff
BB
5176
5177 /*
5178 * Clean up from any previous run.
5179 */
13fe0198
MA
5180 error = dsl_destroy_head(clonename);
5181 if (error != ENOENT)
5182 ASSERT0(error);
5183 error = user_release_one(fullname, tag);
5184 if (error != ESRCH && error != ENOENT)
5185 ASSERT0(error);
5186 error = dsl_destroy_snapshot(fullname, B_FALSE);
5187 if (error != ENOENT)
5188 ASSERT0(error);
428870ff
BB
5189
5190 /*
5191 * Create snapshot, clone it, mark snap for deferred destroy,
5192 * destroy clone, verify snap was also destroyed.
5193 */
6f1ffb06 5194 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5195 if (error) {
5196 if (error == ENOSPC) {
5197 ztest_record_enospc("dmu_objset_snapshot");
5198 goto out;
34dc7c2f 5199 }
428870ff
BB
5200 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5201 }
34dc7c2f 5202
13fe0198 5203 error = dmu_objset_clone(clonename, fullname);
428870ff 5204 if (error) {
34dc7c2f 5205 if (error == ENOSPC) {
428870ff
BB
5206 ztest_record_enospc("dmu_objset_clone");
5207 goto out;
34dc7c2f 5208 }
428870ff
BB
5209 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5210 }
34dc7c2f 5211
13fe0198 5212 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5213 if (error) {
13fe0198 5214 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff
BB
5215 fullname, error);
5216 }
34dc7c2f 5217
13fe0198 5218 error = dsl_destroy_head(clonename);
428870ff 5219 if (error)
13fe0198 5220 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
34dc7c2f 5221
428870ff
BB
5222 error = dmu_objset_hold(fullname, FTAG, &origin);
5223 if (error != ENOENT)
5224 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
34dc7c2f 5225
428870ff
BB
5226 /*
5227 * Create snapshot, add temporary hold, verify that we can't
5228 * destroy a held snapshot, mark for deferred destroy,
5229 * release hold, verify snapshot was destroyed.
5230 */
6f1ffb06 5231 error = dmu_objset_snapshot_one(osname, snapname);
428870ff
BB
5232 if (error) {
5233 if (error == ENOSPC) {
5234 ztest_record_enospc("dmu_objset_snapshot");
5235 goto out;
34dc7c2f 5236 }
428870ff
BB
5237 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5238 }
5239
13fe0198
MA
5240 holds = fnvlist_alloc();
5241 fnvlist_add_string(holds, fullname, tag);
5242 error = dsl_dataset_user_hold(holds, 0, NULL);
5243 fnvlist_free(holds);
5244
9b67f605
MA
5245 if (error == ENOSPC) {
5246 ztest_record_enospc("dsl_dataset_user_hold");
5247 goto out;
5248 } else if (error) {
5249 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5250 fullname, tag, error);
5251 }
428870ff 5252
13fe0198 5253 error = dsl_destroy_snapshot(fullname, B_FALSE);
428870ff 5254 if (error != EBUSY) {
13fe0198 5255 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
428870ff
BB
5256 fullname, error);
5257 }
5258
13fe0198 5259 error = dsl_destroy_snapshot(fullname, B_TRUE);
428870ff 5260 if (error) {
13fe0198 5261 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
428870ff 5262 fullname, error);
34dc7c2f
BB
5263 }
5264
13fe0198 5265 error = user_release_one(fullname, tag);
428870ff 5266 if (error)
95fd54a1 5267 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
428870ff 5268
13fe0198 5269 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
428870ff
BB
5270
5271out:
7809eb8b 5272 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5273}
5274
34dc7c2f
BB
5275/*
5276 * Inject random faults into the on-disk data.
5277 */
428870ff 5278/* ARGSUSED */
34dc7c2f 5279void
428870ff 5280ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5281{
428870ff 5282 ztest_shared_t *zs = ztest_shared;
c242c188 5283 spa_t *spa = ztest_spa;
34dc7c2f
BB
5284 int fd;
5285 uint64_t offset;
428870ff 5286 uint64_t leaves;
c5b3a7bb 5287 uint64_t bad = 0x1990c0ffeedecadeull;
34dc7c2f 5288 uint64_t top, leaf;
40b84e7a
BB
5289 char *path0;
5290 char *pathrand;
34dc7c2f 5291 size_t fsize;
2014c09f 5292 int bshift = SPA_MAXBLOCKSHIFT + 2;
34dc7c2f 5293 int iters = 1000;
428870ff
BB
5294 int maxfaults;
5295 int mirror_save;
b128c09f 5296 vdev_t *vd0 = NULL;
34dc7c2f 5297 uint64_t guid0 = 0;
428870ff
BB
5298 boolean_t islog = B_FALSE;
5299
40b84e7a
BB
5300 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5301 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5302
c242c188 5303 mutex_enter(&ztest_vdev_lock);
428870ff 5304 maxfaults = MAXFAULTS();
c242c188 5305 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
428870ff 5306 mirror_save = zs->zs_mirrors;
c242c188 5307 mutex_exit(&ztest_vdev_lock);
34dc7c2f 5308
b128c09f 5309 ASSERT(leaves >= 1);
34dc7c2f 5310
621dd7bb
GW
5311 /*
5312 * Grab the name lock as reader. There are some operations
5313 * which don't like to have their vdevs changed while
5314 * they are in progress (i.e. spa_change_guid). Those
5315 * operations will have grabbed the name lock as writer.
5316 */
7809eb8b 5317 (void) rw_rdlock(&ztest_name_lock);
621dd7bb 5318
34dc7c2f 5319 /*
b128c09f 5320 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
34dc7c2f 5321 */
b128c09f 5322 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 5323
b128c09f
BB
5324 if (ztest_random(2) == 0) {
5325 /*
428870ff 5326 * Inject errors on a normal data device or slog device.
b128c09f 5327 */
428870ff
BB
5328 top = ztest_random_vdev_top(spa, B_TRUE);
5329 leaf = ztest_random(leaves) + zs->zs_splits;
34dc7c2f 5330
b128c09f
BB
5331 /*
5332 * Generate paths to the first leaf in this top-level vdev,
5333 * and to the random leaf we selected. We'll induce transient
5334 * write failures and random online/offline activity on leaf 0,
5335 * and we'll write random garbage to the randomly chosen leaf.
5336 */
6aec1cd5 5337 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5338 ztest_opts.zo_dir, ztest_opts.zo_pool,
5339 top * leaves + zs->zs_splits);
6aec1cd5 5340 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
c242c188
CS
5341 ztest_opts.zo_dir, ztest_opts.zo_pool,
5342 top * leaves + leaf);
34dc7c2f 5343
b128c09f 5344 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
428870ff
BB
5345 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5346 islog = B_TRUE;
5347
621dd7bb
GW
5348 /*
5349 * If the top-level vdev needs to be resilvered
5350 * then we only allow faults on the device that is
5351 * resilvering.
5352 */
5353 if (vd0 != NULL && maxfaults != 1 &&
5354 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5d1f7fb6 5355 vd0->vdev_resilver_txg != 0)) {
b128c09f
BB
5356 /*
5357 * Make vd0 explicitly claim to be unreadable,
5358 * or unwriteable, or reach behind its back
5359 * and close the underlying fd. We can do this if
5360 * maxfaults == 0 because we'll fail and reexecute,
5361 * and we can do it if maxfaults >= 2 because we'll
5362 * have enough redundancy. If maxfaults == 1, the
5363 * combination of this with injection of random data
5364 * corruption below exceeds the pool's fault tolerance.
5365 */
5366 vdev_file_t *vf = vd0->vdev_tsd;
5367
5368 if (vf != NULL && ztest_random(3) == 0) {
5369 (void) close(vf->vf_vnode->v_fd);
5370 vf->vf_vnode->v_fd = -1;
5371 } else if (ztest_random(2) == 0) {
5372 vd0->vdev_cant_read = B_TRUE;
5373 } else {
5374 vd0->vdev_cant_write = B_TRUE;
5375 }
5376 guid0 = vd0->vdev_guid;
5377 }
5378 } else {
5379 /*
5380 * Inject errors on an l2cache device.
5381 */
5382 spa_aux_vdev_t *sav = &spa->spa_l2cache;
34dc7c2f 5383
b128c09f
BB
5384 if (sav->sav_count == 0) {
5385 spa_config_exit(spa, SCL_STATE, FTAG);
7809eb8b 5386 (void) rw_unlock(&ztest_name_lock);
40b84e7a 5387 goto out;
b128c09f
BB
5388 }
5389 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
34dc7c2f 5390 guid0 = vd0->vdev_guid;
b128c09f
BB
5391 (void) strcpy(path0, vd0->vdev_path);
5392 (void) strcpy(pathrand, vd0->vdev_path);
5393
5394 leaf = 0;
5395 leaves = 1;
5396 maxfaults = INT_MAX; /* no limit on cache devices */
34dc7c2f
BB
5397 }
5398
b128c09f 5399 spa_config_exit(spa, SCL_STATE, FTAG);
7809eb8b 5400 (void) rw_unlock(&ztest_name_lock);
b128c09f 5401
34dc7c2f 5402 /*
428870ff
BB
5403 * If we can tolerate two or more faults, or we're dealing
5404 * with a slog, randomly online/offline vd0.
34dc7c2f 5405 */
428870ff 5406 if ((maxfaults >= 2 || islog) && guid0 != 0) {
fb5f0bc8
BB
5407 if (ztest_random(10) < 6) {
5408 int flags = (ztest_random(2) == 0 ?
5409 ZFS_OFFLINE_TEMPORARY : 0);
428870ff
BB
5410
5411 /*
5412 * We have to grab the zs_name_lock as writer to
5413 * prevent a race between offlining a slog and
5414 * destroying a dataset. Offlining the slog will
5415 * grab a reference on the dataset which may cause
13fe0198 5416 * dsl_destroy_head() to fail with EBUSY thus
428870ff
BB
5417 * leaving the dataset in an inconsistent state.
5418 */
5419 if (islog)
7809eb8b 5420 (void) rw_wrlock(&ztest_name_lock);
428870ff 5421
fb5f0bc8 5422 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
428870ff
BB
5423
5424 if (islog)
7809eb8b 5425 (void) rw_unlock(&ztest_name_lock);
fb5f0bc8 5426 } else {
1eb5bfa3
GW
5427 /*
5428 * Ideally we would like to be able to randomly
5429 * call vdev_[on|off]line without holding locks
5430 * to force unpredictable failures but the side
5431 * effects of vdev_[on|off]line prevent us from
5432 * doing so. We grab the ztest_vdev_lock here to
5433 * prevent a race between injection testing and
5434 * aux_vdev removal.
5435 */
5436 mutex_enter(&ztest_vdev_lock);
fb5f0bc8 5437 (void) vdev_online(spa, guid0, 0, NULL);
1eb5bfa3 5438 mutex_exit(&ztest_vdev_lock);
fb5f0bc8 5439 }
34dc7c2f
BB
5440 }
5441
428870ff 5442 if (maxfaults == 0)
40b84e7a 5443 goto out;
428870ff 5444
34dc7c2f
BB
5445 /*
5446 * We have at least single-fault tolerance, so inject data corruption.
5447 */
5448 fd = open(pathrand, O_RDWR);
5449
5450 if (fd == -1) /* we hit a gap in the device namespace */
40b84e7a 5451 goto out;
34dc7c2f
BB
5452
5453 fsize = lseek(fd, 0, SEEK_END);
5454
5455 while (--iters != 0) {
91d88843
MA
5456 /*
5457 * The offset must be chosen carefully to ensure that
5458 * we do not inject a given logical block with errors
5459 * on two different leaf devices, because ZFS can not
5460 * tolerate that (if maxfaults==1).
5461 *
5462 * We divide each leaf into chunks of size
5463 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5464 * there is a series of ranges to which we can inject errors.
5465 * Each range can accept errors on only a single leaf vdev.
5466 * The error injection ranges are separated by ranges
5467 * which we will not inject errors on any device (DMZs).
5468 * Each DMZ must be large enough such that a single block
5469 * can not straddle it, so that a single block can not be
5470 * a target in two different injection ranges (on different
5471 * leaf vdevs).
5472 *
5473 * For example, with 3 leaves, each chunk looks like:
5474 * 0 to 32M: injection range for leaf 0
5475 * 32M to 64M: DMZ - no injection allowed
5476 * 64M to 96M: injection range for leaf 1
5477 * 96M to 128M: DMZ - no injection allowed
5478 * 128M to 160M: injection range for leaf 2
5479 * 160M to 192M: DMZ - no injection allowed
5480 */
34dc7c2f
BB
5481 offset = ztest_random(fsize / (leaves << bshift)) *
5482 (leaves << bshift) + (leaf << bshift) +
5483 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5484
2014c09f
GM
5485 /*
5486 * Only allow damage to the labels at one end of the vdev.
5487 *
5488 * If all labels are damaged, the device will be totally
5489 * inaccessible, which will result in loss of data,
5490 * because we also damage (parts of) the other side of
5491 * the mirror/raidz.
5492 *
5493 * Additionally, we will always have both an even and an
5494 * odd label, so that we can handle crashes in the
5495 * middle of vdev_config_sync().
5496 */
5497 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5498 continue;
5499
5500 /*
5501 * The two end labels are stored at the "end" of the disk, but
5502 * the end of the disk (vdev_psize) is aligned to
5503 * sizeof (vdev_label_t).
5504 */
5505 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5506 if ((leaf & 1) == 1 &&
5507 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
34dc7c2f
BB
5508 continue;
5509
c242c188 5510 mutex_enter(&ztest_vdev_lock);
428870ff 5511 if (mirror_save != zs->zs_mirrors) {
c242c188 5512 mutex_exit(&ztest_vdev_lock);
428870ff 5513 (void) close(fd);
40b84e7a 5514 goto out;
428870ff 5515 }
34dc7c2f
BB
5516
5517 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5518 fatal(1, "can't inject bad word at 0x%llx in %s",
5519 offset, pathrand);
428870ff 5520
c242c188 5521 mutex_exit(&ztest_vdev_lock);
428870ff 5522
c242c188 5523 if (ztest_opts.zo_verbose >= 7)
428870ff
BB
5524 (void) printf("injected bad word into %s,"
5525 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
34dc7c2f
BB
5526 }
5527
5528 (void) close(fd);
40b84e7a
BB
5529out:
5530 umem_free(path0, MAXPATHLEN);
5531 umem_free(pathrand, MAXPATHLEN);
34dc7c2f
BB
5532}
5533
5534/*
428870ff 5535 * Verify that DDT repair works as expected.
34dc7c2f
BB
5536 */
5537void
428870ff 5538ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5539{
428870ff 5540 ztest_shared_t *zs = ztest_shared;
c242c188 5541 spa_t *spa = ztest_spa;
428870ff 5542 objset_t *os = zd->zd_os;
40b84e7a 5543 ztest_od_t *od;
428870ff
BB
5544 uint64_t object, blocksize, txg, pattern, psize;
5545 enum zio_checksum checksum = spa_dedup_checksum(spa);
5546 dmu_buf_t *db;
5547 dmu_tx_t *tx;
a6255b7f 5548 abd_t *abd;
428870ff
BB
5549 blkptr_t blk;
5550 int copies = 2 * ZIO_DEDUPDITTO_MIN;
d6320ddb 5551 int i;
34dc7c2f 5552
428870ff
BB
5553 blocksize = ztest_random_blocksize();
5554 blocksize = MIN(blocksize, 2048); /* because we write so many */
34dc7c2f 5555
d1d7e268 5556 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
50c957f7 5557 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
34dc7c2f 5558
40b84e7a 5559 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
d1d7e268 5560 umem_free(od, sizeof (ztest_od_t));
428870ff 5561 return;
40b84e7a 5562 }
34dc7c2f
BB
5563
5564 /*
428870ff
BB
5565 * Take the name lock as writer to prevent anyone else from changing
5566 * the pool and dataset properies we need to maintain during this test.
34dc7c2f 5567 */
7809eb8b 5568 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5569
428870ff
BB
5570 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5571 B_FALSE) != 0 ||
5572 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5573 B_FALSE) != 0) {
7809eb8b 5574 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5575 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5576 return;
5577 }
5578
546d32ca
BB
5579 dmu_objset_stats_t dds;
5580 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5581 dmu_objset_fast_stat(os, &dds);
5582 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5583
428870ff
BB
5584 object = od[0].od_object;
5585 blocksize = od[0].od_blocksize;
546d32ca 5586 pattern = zs->zs_guid ^ dds.dds_guid;
428870ff
BB
5587
5588 ASSERT(object != 0);
5589
5590 tx = dmu_tx_create(os);
5591 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5592 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5593 if (txg == 0) {
7809eb8b 5594 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5595 umem_free(od, sizeof (ztest_od_t));
428870ff
BB
5596 return;
5597 }
34dc7c2f
BB
5598
5599 /*
428870ff 5600 * Write all the copies of our block.
34dc7c2f 5601 */
d6320ddb 5602 for (i = 0; i < copies; i++) {
428870ff 5603 uint64_t offset = i * blocksize;
13fe0198
MA
5604 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5605 DMU_READ_NO_PREFETCH);
5606 if (error != 0) {
5607 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5608 os, (long long)object, (long long) offset, error);
5609 }
428870ff
BB
5610 ASSERT(db->db_offset == offset);
5611 ASSERT(db->db_size == blocksize);
5612 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5613 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5614 dmu_buf_will_fill(db, tx);
5615 ztest_pattern_set(db->db_data, db->db_size, pattern);
5616 dmu_buf_rele(db, FTAG);
5617 }
34dc7c2f 5618
428870ff
BB
5619 dmu_tx_commit(tx);
5620 txg_wait_synced(spa_get_dsl(spa), txg);
34dc7c2f
BB
5621
5622 /*
428870ff 5623 * Find out what block we got.
34dc7c2f 5624 */
03c6040b
GW
5625 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5626 DMU_READ_NO_PREFETCH));
428870ff
BB
5627 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5628 dmu_buf_rele(db, FTAG);
34dc7c2f
BB
5629
5630 /*
428870ff 5631 * Damage the block. Dedup-ditto will save us when we read it later.
34dc7c2f 5632 */
428870ff 5633 psize = BP_GET_PSIZE(&blk);
a6255b7f
DQ
5634 abd = abd_alloc_linear(psize, B_TRUE);
5635 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
34dc7c2f 5636
428870ff 5637 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
a6255b7f 5638 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
428870ff 5639 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
34dc7c2f 5640
a6255b7f 5641 abd_free(abd);
34dc7c2f 5642
7809eb8b 5643 (void) rw_unlock(&ztest_name_lock);
d1d7e268 5644 umem_free(od, sizeof (ztest_od_t));
34dc7c2f
BB
5645}
5646
34dc7c2f 5647/*
428870ff 5648 * Scrub the pool.
34dc7c2f 5649 */
428870ff
BB
5650/* ARGSUSED */
5651void
5652ztest_scrub(ztest_ds_t *zd, uint64_t id)
34dc7c2f 5653{
c242c188 5654 spa_t *spa = ztest_spa;
34dc7c2f 5655
428870ff
BB
5656 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5657 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5658 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5659}
34dc7c2f 5660
3541dc6d
GA
5661/*
5662 * Change the guid for the pool.
5663 */
5664/* ARGSUSED */
5665void
5666ztest_reguid(ztest_ds_t *zd, uint64_t id)
5667{
c242c188 5668 spa_t *spa = ztest_spa;
3541dc6d 5669 uint64_t orig, load;
3bc7e0fb 5670 int error;
3541dc6d
GA
5671
5672 orig = spa_guid(spa);
5673 load = spa_load_guid(spa);
3bc7e0fb 5674
7809eb8b 5675 (void) rw_wrlock(&ztest_name_lock);
3bc7e0fb 5676 error = spa_change_guid(spa);
7809eb8b 5677 (void) rw_unlock(&ztest_name_lock);
3bc7e0fb
GW
5678
5679 if (error != 0)
3541dc6d
GA
5680 return;
5681
ea0b2538 5682 if (ztest_opts.zo_verbose >= 4) {
3541dc6d
GA
5683 (void) printf("Changed guid old %llu -> %llu\n",
5684 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5685 }
5686
5687 VERIFY3U(orig, !=, spa_guid(spa));
5688 VERIFY3U(load, ==, spa_load_guid(spa));
5689}
5690
428870ff
BB
5691/*
5692 * Rename the pool to a different name and then rename it back.
5693 */
5694/* ARGSUSED */
5695void
5696ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5697{
428870ff
BB
5698 char *oldname, *newname;
5699 spa_t *spa;
34dc7c2f 5700
379ca9cf
OF
5701 if (ztest_opts.zo_mmp_test)
5702 return;
5703
7809eb8b 5704 (void) rw_wrlock(&ztest_name_lock);
34dc7c2f 5705
c242c188 5706 oldname = ztest_opts.zo_pool;
428870ff
BB
5707 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5708 (void) strcpy(newname, oldname);
5709 (void) strcat(newname, "_tmp");
34dc7c2f
BB
5710
5711 /*
428870ff 5712 * Do the rename
34dc7c2f 5713 */
428870ff 5714 VERIFY3U(0, ==, spa_rename(oldname, newname));
34dc7c2f
BB
5715
5716 /*
428870ff 5717 * Try to open it under the old name, which shouldn't exist
34dc7c2f 5718 */
428870ff 5719 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
5720
5721 /*
428870ff
BB
5722 * Open it under the new name and make sure it's still the same spa_t.
5723 */
5724 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f 5725
c242c188 5726 ASSERT(spa == ztest_spa);
428870ff 5727 spa_close(spa, FTAG);
34dc7c2f
BB
5728
5729 /*
428870ff 5730 * Rename it back to the original
34dc7c2f 5731 */
428870ff 5732 VERIFY3U(0, ==, spa_rename(newname, oldname));
34dc7c2f 5733
428870ff
BB
5734 /*
5735 * Make sure it can still be opened
5736 */
5737 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 5738
c242c188 5739 ASSERT(spa == ztest_spa);
428870ff
BB
5740 spa_close(spa, FTAG);
5741
5742 umem_free(newname, strlen(newname) + 1);
5743
7809eb8b 5744 (void) rw_unlock(&ztest_name_lock);
34dc7c2f
BB
5745}
5746
1eeb4562
JX
5747void
5748ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5749{
5750 hrtime_t end = gethrtime() + NANOSEC;
5751
5752 while (gethrtime() <= end) {
5753 int run_count = 100;
5754 void *buf;
2fe36b0b 5755 struct abd *abd_data, *abd_meta;
1eeb4562
JX
5756 uint32_t size;
5757 int *ptr;
5758 int i;
5759 zio_cksum_t zc_ref;
5760 zio_cksum_t zc_ref_byteswap;
5761
5762 size = ztest_random_blocksize();
2fe36b0b 5763
1eeb4562 5764 buf = umem_alloc(size, UMEM_NOFAIL);
2fe36b0b
DQ
5765 abd_data = abd_alloc(size, B_FALSE);
5766 abd_meta = abd_alloc(size, B_TRUE);
1eeb4562
JX
5767
5768 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5769 *ptr = ztest_random(UINT_MAX);
5770
2fe36b0b
DQ
5771 abd_copy_from_buf_off(abd_data, buf, 0, size);
5772 abd_copy_from_buf_off(abd_meta, buf, 0, size);
5773
1eeb4562 5774 VERIFY0(fletcher_4_impl_set("scalar"));
3c67d83a
TH
5775 fletcher_4_native(buf, size, NULL, &zc_ref);
5776 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
1eeb4562
JX
5777
5778 VERIFY0(fletcher_4_impl_set("cycle"));
5779 while (run_count-- > 0) {
5780 zio_cksum_t zc;
5781 zio_cksum_t zc_byteswap;
5782
3c67d83a
TH
5783 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5784 fletcher_4_native(buf, size, NULL, &zc);
1eeb4562
JX
5785
5786 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5787 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5788 sizeof (zc_byteswap)));
2fe36b0b
DQ
5789
5790 /* Test ABD - data */
5791 abd_fletcher_4_byteswap(abd_data, size, NULL,
5792 &zc_byteswap);
5793 abd_fletcher_4_native(abd_data, size, NULL, &zc);
5794
5795 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5796 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5797 sizeof (zc_byteswap)));
5798
5799 /* Test ABD - metadata */
5800 abd_fletcher_4_byteswap(abd_meta, size, NULL,
5801 &zc_byteswap);
5802 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
5803
5804 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5805 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5806 sizeof (zc_byteswap)));
5807
1eeb4562
JX
5808 }
5809
5810 umem_free(buf, size);
2fe36b0b
DQ
5811 abd_free(abd_data);
5812 abd_free(abd_meta);
1eeb4562
JX
5813 }
5814}
5815
37f520db
GN
5816void
5817ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5818{
5819 void *buf;
5820 size_t size;
5821 int *ptr;
5822 int i;
5823 zio_cksum_t zc_ref;
5824 zio_cksum_t zc_ref_bswap;
5825
5826 hrtime_t end = gethrtime() + NANOSEC;
5827
5828 while (gethrtime() <= end) {
5829 int run_count = 100;
5830
5831 size = ztest_random_blocksize();
5832 buf = umem_alloc(size, UMEM_NOFAIL);
5833
5834 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5835 *ptr = ztest_random(UINT_MAX);
5836
5837 VERIFY0(fletcher_4_impl_set("scalar"));
5838 fletcher_4_native(buf, size, NULL, &zc_ref);
5839 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5840
5841 VERIFY0(fletcher_4_impl_set("cycle"));
5842
5843 while (run_count-- > 0) {
5844 zio_cksum_t zc;
5845 zio_cksum_t zc_bswap;
5846 size_t pos = 0;
5847
5848 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5849 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5850
5851 while (pos < size) {
5852 size_t inc = 64 * ztest_random(size / 67);
5853 /* sometimes add few bytes to test non-simd */
5854 if (ztest_random(100) < 10)
5855 inc += P2ALIGN(ztest_random(64),
5856 sizeof (uint32_t));
5857
5858 if (inc > (size - pos))
5859 inc = size - pos;
5860
5861 fletcher_4_incremental_native(buf + pos, inc,
5862 &zc);
5863 fletcher_4_incremental_byteswap(buf + pos, inc,
5864 &zc_bswap);
5865
5866 pos += inc;
5867 }
5868
5869 VERIFY3U(pos, ==, size);
5870
5871 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5872 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5873
5874 /*
5875 * verify if incremental on the whole buffer is
5876 * equivalent to non-incremental version
5877 */
5878 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5879 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5880
5881 fletcher_4_incremental_native(buf, size, &zc);
5882 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
5883
5884 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5885 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5886 }
5887
5888 umem_free(buf, size);
5889 }
5890}
5891
90aa094d
CC
5892static int
5893ztest_check_path(char *path)
5894{
5895 struct stat s;
5896 /* return true on success */
5897 return (!stat(path, &s));
5898}
5899
5900static void
5901ztest_get_zdb_bin(char *bin, int len)
5902{
5903 char *zdb_path;
5904 /*
5905 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5906 * let popen to search through PATH.
5907 */
5908 if ((zdb_path = getenv("ZDB_PATH"))) {
5909 strlcpy(bin, zdb_path, len); /* In env */
5910 if (!ztest_check_path(bin)) {
5911 ztest_dump_core = 0;
5912 fatal(1, "invalid ZDB_PATH '%s'", bin);
5913 }
5914 return;
5915 }
5916
5917 VERIFY(realpath(getexecname(), bin) != NULL);
5918 if (strstr(bin, "/ztest/")) {
5919 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5920 strcat(bin, "/zdb/zdb");
5921 if (ztest_check_path(bin))
5922 return;
5923 }
5924 strcpy(bin, "zdb");
5925}
5926
428870ff
BB
5927/*
5928 * Verify pool integrity by running zdb.
5929 */
34dc7c2f 5930static void
428870ff 5931ztest_run_zdb(char *pool)
34dc7c2f
BB
5932{
5933 int status;
34dc7c2f 5934 char *bin;
0e8d1b2d
BB
5935 char *zdb;
5936 char *zbuf;
90aa094d 5937 const int len = MAXPATHLEN + MAXNAMELEN + 20;
34dc7c2f
BB
5938 FILE *fp;
5939
90aa094d
CC
5940 bin = umem_alloc(len, UMEM_NOFAIL);
5941 zdb = umem_alloc(len, UMEM_NOFAIL);
0e8d1b2d 5942 zbuf = umem_alloc(1024, UMEM_NOFAIL);
34dc7c2f 5943
90aa094d 5944 ztest_get_zdb_bin(bin, len);
0e8d1b2d
BB
5945
5946 (void) sprintf(zdb,
456079d4 5947 "%s -bcc%s%s -G -d -U %s %s",
0e8d1b2d 5948 bin,
c242c188
CS
5949 ztest_opts.zo_verbose >= 3 ? "s" : "",
5950 ztest_opts.zo_verbose >= 4 ? "v" : "",
428870ff 5951 spa_config_path,
b128c09f 5952 pool);
34dc7c2f 5953
c242c188 5954 if (ztest_opts.zo_verbose >= 5)
34dc7c2f
BB
5955 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5956
5957 fp = popen(zdb, "r");
5958
6aec1cd5 5959 while (fgets(zbuf, 1024, fp) != NULL)
c242c188 5960 if (ztest_opts.zo_verbose >= 3)
34dc7c2f
BB
5961 (void) printf("%s", zbuf);
5962
5963 status = pclose(fp);
5964
5965 if (status == 0)
0e8d1b2d 5966 goto out;
34dc7c2f
BB
5967
5968 ztest_dump_core = 0;
5969 if (WIFEXITED(status))
5970 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5971 else
5972 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
0e8d1b2d 5973out:
90aa094d
CC
5974 umem_free(bin, len);
5975 umem_free(zdb, len);
0e8d1b2d 5976 umem_free(zbuf, 1024);
34dc7c2f
BB
5977}
5978
5979static void
5980ztest_walk_pool_directory(char *header)
5981{
5982 spa_t *spa = NULL;
5983
c242c188 5984 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5985 (void) printf("%s\n", header);
5986
5987 mutex_enter(&spa_namespace_lock);
5988 while ((spa = spa_next(spa)) != NULL)
c242c188 5989 if (ztest_opts.zo_verbose >= 6)
34dc7c2f
BB
5990 (void) printf("\t%s\n", spa_name(spa));
5991 mutex_exit(&spa_namespace_lock);
5992}
5993
5994static void
5995ztest_spa_import_export(char *oldname, char *newname)
5996{
fb5f0bc8 5997 nvlist_t *config, *newconfig;
34dc7c2f
BB
5998 uint64_t pool_guid;
5999 spa_t *spa;
13fe0198 6000 int error;
34dc7c2f 6001
c242c188 6002 if (ztest_opts.zo_verbose >= 4) {
34dc7c2f
BB
6003 (void) printf("import/export: old = %s, new = %s\n",
6004 oldname, newname);
6005 }
6006
6007 /*
6008 * Clean up from previous runs.
6009 */
6010 (void) spa_destroy(newname);
6011
6012 /*
6013 * Get the pool's configuration and guid.
6014 */
428870ff 6015 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f 6016
fb5f0bc8
BB
6017 /*
6018 * Kick off a scrub to tickle scrub/export races.
6019 */
6020 if (ztest_random(2) == 0)
428870ff 6021 (void) spa_scan(spa, POOL_SCAN_SCRUB);
fb5f0bc8 6022
34dc7c2f
BB
6023 pool_guid = spa_guid(spa);
6024 spa_close(spa, FTAG);
6025
6026 ztest_walk_pool_directory("pools before export");
6027
6028 /*
6029 * Export it.
6030 */
428870ff 6031 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
34dc7c2f
BB
6032
6033 ztest_walk_pool_directory("pools after export");
6034
fb5f0bc8
BB
6035 /*
6036 * Try to import it.
6037 */
6038 newconfig = spa_tryimport(config);
6039 ASSERT(newconfig != NULL);
6040 nvlist_free(newconfig);
6041
34dc7c2f
BB
6042 /*
6043 * Import it under the new name.
6044 */
13fe0198
MA
6045 error = spa_import(newname, config, NULL, 0);
6046 if (error != 0) {
6047 dump_nvlist(config, 0);
6048 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6049 oldname, newname, error);
6050 }
34dc7c2f
BB
6051
6052 ztest_walk_pool_directory("pools after import");
6053
6054 /*
6055 * Try to import it again -- should fail with EEXIST.
6056 */
572e2857 6057 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
34dc7c2f
BB
6058
6059 /*
6060 * Try to import it under a different name -- should fail with EEXIST.
6061 */
572e2857 6062 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
34dc7c2f
BB
6063
6064 /*
6065 * Verify that the pool is no longer visible under the old name.
6066 */
428870ff 6067 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
34dc7c2f
BB
6068
6069 /*
6070 * Verify that we can open and close the pool using the new name.
6071 */
428870ff 6072 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
34dc7c2f
BB
6073 ASSERT(pool_guid == spa_guid(spa));
6074 spa_close(spa, FTAG);
6075
6076 nvlist_free(config);
6077}
6078
fb5f0bc8
BB
6079static void
6080ztest_resume(spa_t *spa)
6081{
c242c188 6082 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
428870ff
BB
6083 (void) printf("resuming from suspended state\n");
6084 spa_vdev_state_enter(spa, SCL_NONE);
6085 vdev_clear(spa, NULL);
6086 (void) spa_vdev_state_exit(spa, NULL, 0);
6087 (void) zio_resume(spa);
fb5f0bc8
BB
6088}
6089
c25b8f99 6090static void
fb5f0bc8 6091ztest_resume_thread(void *arg)
34dc7c2f 6092{
b128c09f 6093 spa_t *spa = arg;
34dc7c2f
BB
6094
6095 while (!ztest_exiting) {
428870ff
BB
6096 if (spa_suspended(spa))
6097 ztest_resume(spa);
6098 (void) poll(NULL, 0, 100);
d3c2ae1c
GW
6099
6100 /*
6101 * Periodically change the zfs_compressed_arc_enabled setting.
6102 */
6103 if (ztest_random(10) == 0)
6104 zfs_compressed_arc_enabled = ztest_random(2);
a6255b7f
DQ
6105
6106 /*
6107 * Periodically change the zfs_abd_scatter_enabled setting.
6108 */
6109 if (ztest_random(10) == 0)
6110 zfs_abd_scatter_enabled = ztest_random(2);
34dc7c2f 6111 }
34dc7c2f 6112
1e33ac1e 6113 thread_exit();
1e33ac1e 6114}
428870ff 6115
d1d7e268 6116#define GRACE 300
428870ff 6117
26099167 6118#if 0
1e33ac1e
BB
6119static void
6120ztest_deadman_alarm(int sig)
6121{
6122 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
428870ff 6123}
26099167 6124#endif
428870ff
BB
6125
6126static void
c242c188 6127ztest_execute(int test, ztest_info_t *zi, uint64_t id)
428870ff 6128{
c242c188
CS
6129 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6130 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
428870ff 6131 hrtime_t functime = gethrtime();
d6320ddb 6132 int i;
428870ff 6133
d6320ddb 6134 for (i = 0; i < zi->zi_iters; i++)
428870ff
BB
6135 zi->zi_func(zd, id);
6136
6137 functime = gethrtime() - functime;
6138
c242c188
CS
6139 atomic_add_64(&zc->zc_count, 1);
6140 atomic_add_64(&zc->zc_time, functime);
428870ff 6141
fdc5d982 6142 if (ztest_opts.zo_verbose >= 4)
428870ff 6143 (void) printf("%6.2f sec in %s\n",
fdc5d982 6144 (double)functime / NANOSEC, zi->zi_funcname);
428870ff
BB
6145}
6146
c25b8f99 6147static void
34dc7c2f
BB
6148ztest_thread(void *arg)
6149{
c242c188 6150 int rand;
428870ff 6151 uint64_t id = (uintptr_t)arg;
34dc7c2f 6152 ztest_shared_t *zs = ztest_shared;
428870ff
BB
6153 uint64_t call_next;
6154 hrtime_t now;
34dc7c2f 6155 ztest_info_t *zi;
c242c188 6156 ztest_shared_callstate_t *zc;
34dc7c2f 6157
428870ff 6158 while ((now = gethrtime()) < zs->zs_thread_stop) {
34dc7c2f
BB
6159 /*
6160 * See if it's time to force a crash.
6161 */
428870ff
BB
6162 if (now > zs->zs_thread_kill)
6163 ztest_kill(zs);
34dc7c2f
BB
6164
6165 /*
428870ff 6166 * If we're getting ENOSPC with some regularity, stop.
34dc7c2f 6167 */
428870ff
BB
6168 if (zs->zs_enospc_count > 10)
6169 break;
34dc7c2f
BB
6170
6171 /*
428870ff 6172 * Pick a random function to execute.
34dc7c2f 6173 */
c242c188
CS
6174 rand = ztest_random(ZTEST_FUNCS);
6175 zi = &ztest_info[rand];
6176 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6177 call_next = zc->zc_next;
34dc7c2f 6178
428870ff 6179 if (now >= call_next &&
c242c188
CS
6180 atomic_cas_64(&zc->zc_next, call_next, call_next +
6181 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6182 ztest_execute(rand, zi, id);
6183 }
428870ff 6184 }
34dc7c2f 6185
1e33ac1e 6186 thread_exit();
428870ff 6187}
34dc7c2f 6188
428870ff
BB
6189static void
6190ztest_dataset_name(char *dsname, char *pool, int d)
6191{
eca7b760 6192 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
428870ff 6193}
34dc7c2f 6194
428870ff 6195static void
c242c188 6196ztest_dataset_destroy(int d)
428870ff 6197{
eca7b760 6198 char name[ZFS_MAX_DATASET_NAME_LEN];
d6320ddb 6199 int t;
34dc7c2f 6200
c242c188 6201 ztest_dataset_name(name, ztest_opts.zo_pool, d);
34dc7c2f 6202
c242c188 6203 if (ztest_opts.zo_verbose >= 3)
428870ff 6204 (void) printf("Destroying %s to free up space\n", name);
34dc7c2f 6205
428870ff
BB
6206 /*
6207 * Cleanup any non-standard clones and snapshots. In general,
6208 * ztest thread t operates on dataset (t % zopt_datasets),
6209 * so there may be more than one thing to clean up.
6210 */
c242c188
CS
6211 for (t = d; t < ztest_opts.zo_threads;
6212 t += ztest_opts.zo_datasets)
428870ff
BB
6213 ztest_dsl_dataset_cleanup(name, t);
6214
6215 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6216 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6217}
6218
6219static void
6220ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6221{
6222 uint64_t usedobjs, dirobjs, scratch;
6223
6224 /*
6225 * ZTEST_DIROBJ is the object directory for the entire dataset.
6226 * Therefore, the number of objects in use should equal the
6227 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6228 * If not, we have an object leak.
6229 *
6230 * Note that we can only check this in ztest_dataset_open(),
6231 * when the open-context and syncing-context values agree.
6232 * That's because zap_count() returns the open-context value,
6233 * while dmu_objset_space() returns the rootbp fill count.
6234 */
6235 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6236 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6237 ASSERT3U(dirobjs + 1, ==, usedobjs);
6238}
6239
6240static int
c242c188 6241ztest_dataset_open(int d)
428870ff 6242{
c242c188
CS
6243 ztest_ds_t *zd = &ztest_ds[d];
6244 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
428870ff
BB
6245 objset_t *os;
6246 zilog_t *zilog;
eca7b760 6247 char name[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
6248 int error;
6249
c242c188 6250 ztest_dataset_name(name, ztest_opts.zo_pool, d);
428870ff 6251
7809eb8b 6252 (void) rw_rdlock(&ztest_name_lock);
428870ff
BB
6253
6254 error = ztest_dataset_create(name);
6255 if (error == ENOSPC) {
7809eb8b 6256 (void) rw_unlock(&ztest_name_lock);
428870ff
BB
6257 ztest_record_enospc(FTAG);
6258 return (error);
34dc7c2f 6259 }
428870ff 6260 ASSERT(error == 0 || error == EEXIST);
34dc7c2f 6261
b5256303 6262 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE, zd, &os));
7809eb8b 6263 (void) rw_unlock(&ztest_name_lock);
428870ff 6264
c242c188 6265 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
428870ff
BB
6266
6267 zilog = zd->zd_zilog;
6268
6269 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6270 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6271 fatal(0, "missing log records: claimed %llu < committed %llu",
6272 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6273
6274 ztest_dataset_dirobj_verify(zd);
6275
6276 zil_replay(os, zd, ztest_replay_vector);
6277
6278 ztest_dataset_dirobj_verify(zd);
6279
c242c188 6280 if (ztest_opts.zo_verbose >= 6)
428870ff
BB
6281 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6282 zd->zd_name,
6283 (u_longlong_t)zilog->zl_parse_blk_count,
6284 (u_longlong_t)zilog->zl_parse_lr_count,
6285 (u_longlong_t)zilog->zl_replaying_seq);
6286
6287 zilog = zil_open(os, ztest_get_data);
6288
6289 if (zilog->zl_replaying_seq != 0 &&
6290 zilog->zl_replaying_seq < committed_seq)
6291 fatal(0, "missing log records: replayed %llu < committed %llu",
6292 zilog->zl_replaying_seq, committed_seq);
6293
6294 return (0);
6295}
6296
6297static void
c242c188 6298ztest_dataset_close(int d)
428870ff 6299{
c242c188 6300 ztest_ds_t *zd = &ztest_ds[d];
428870ff
BB
6301
6302 zil_close(zd->zd_zilog);
b5256303 6303 dmu_objset_disown(zd->zd_os, B_TRUE, zd);
428870ff
BB
6304
6305 ztest_zd_fini(zd);
34dc7c2f
BB
6306}
6307
6308/*
6309 * Kick off threads to run tests on all datasets in parallel.
6310 */
6311static void
428870ff 6312ztest_run(ztest_shared_t *zs)
34dc7c2f 6313{
34dc7c2f 6314 spa_t *spa;
3541dc6d 6315 objset_t *os;
1e33ac1e 6316 kthread_t *resume_thread;
c25b8f99 6317 kthread_t **run_threads;
1e33ac1e 6318 uint64_t object;
428870ff 6319 int error;
d6320ddb 6320 int t, d;
b128c09f
BB
6321
6322 ztest_exiting = B_FALSE;
34dc7c2f 6323
34dc7c2f 6324 /*
428870ff 6325 * Initialize parent/child shared state.
34dc7c2f 6326 */
c242c188 6327 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7809eb8b 6328 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f 6329
428870ff 6330 zs->zs_thread_start = gethrtime();
c242c188
CS
6331 zs->zs_thread_stop =
6332 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
428870ff
BB
6333 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6334 zs->zs_thread_kill = zs->zs_thread_stop;
c242c188
CS
6335 if (ztest_random(100) < ztest_opts.zo_killrate) {
6336 zs->zs_thread_kill -=
6337 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6338 }
34dc7c2f 6339
1e33ac1e 6340 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f 6341
428870ff
BB
6342 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6343 offsetof(ztest_cb_data_t, zcd_node));
34dc7c2f
BB
6344
6345 /*
b128c09f 6346 * Open our pool.
34dc7c2f 6347 */
428870ff 6348 kernel_init(FREAD | FWRITE);
13fe0198 6349 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6d974228 6350 spa->spa_debug = B_TRUE;
080b3100 6351 metaslab_preload_limit = ztest_random(20) + 1;
c242c188 6352 ztest_spa = spa;
428870ff 6353
546d32ca 6354 dmu_objset_stats_t dds;
13fe0198 6355 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
b5256303 6356 DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
546d32ca
BB
6357 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6358 dmu_objset_fast_stat(os, &dds);
6359 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6360 zs->zs_guid = dds.dds_guid;
b5256303 6361 dmu_objset_disown(os, B_TRUE, FTAG);
3541dc6d 6362
428870ff 6363 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
34dc7c2f 6364
fb5f0bc8
BB
6365 /*
6366 * We don't expect the pool to suspend unless maxfaults == 0,
6367 * in which case ztest_fault_inject() temporarily takes away
6368 * the only valid replica.
6369 */
428870ff 6370 if (MAXFAULTS() == 0)
fb5f0bc8
BB
6371 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6372 else
6373 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6374
34dc7c2f 6375 /*
b128c09f 6376 * Create a thread to periodically resume suspended I/O.
34dc7c2f 6377 */
c25b8f99
BB
6378 resume_thread = thread_create(NULL, 0, ztest_resume_thread,
6379 spa, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
34dc7c2f 6380
26099167 6381#if 0
428870ff 6382 /*
1e33ac1e 6383 * Set a deadman alarm to abort() if we hang.
428870ff 6384 */
1e33ac1e
BB
6385 signal(SIGALRM, ztest_deadman_alarm);
6386 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
26099167 6387#endif
428870ff 6388
34dc7c2f
BB
6389 /*
6390 * Verify that we can safely inquire about about any object,
6391 * whether it's allocated or not. To make it interesting,
6392 * we probe a 5-wide window around each power of two.
6393 * This hits all edge cases, including zero and the max.
6394 */
d6320ddb
BB
6395 for (t = 0; t < 64; t++) {
6396 for (d = -5; d <= 5; d++) {
34dc7c2f
BB
6397 error = dmu_object_info(spa->spa_meta_objset,
6398 (1ULL << t) + d, NULL);
6399 ASSERT(error == 0 || error == ENOENT ||
6400 error == EINVAL);
6401 }
6402 }
6403
6404 /*
428870ff 6405 * If we got any ENOSPC errors on the previous run, destroy something.
34dc7c2f 6406 */
428870ff 6407 if (zs->zs_enospc_count != 0) {
c242c188
CS
6408 int d = ztest_random(ztest_opts.zo_datasets);
6409 ztest_dataset_destroy(d);
428870ff 6410 }
34dc7c2f
BB
6411 zs->zs_enospc_count = 0;
6412
c25b8f99 6413 run_threads = umem_zalloc(ztest_opts.zo_threads * sizeof (kthread_t *),
c242c188 6414 UMEM_NOFAIL);
34dc7c2f 6415
c242c188 6416 if (ztest_opts.zo_verbose >= 4)
34dc7c2f
BB
6417 (void) printf("starting main threads...\n");
6418
428870ff
BB
6419 /*
6420 * Kick off all the tests that run in parallel.
6421 */
c242c188 6422 for (t = 0; t < ztest_opts.zo_threads; t++) {
c25b8f99
BB
6423 if (t < ztest_opts.zo_datasets && ztest_dataset_open(t) != 0) {
6424 umem_free(run_threads, ztest_opts.zo_threads *
6425 sizeof (kthread_t *));
428870ff 6426 return;
06cf4d98 6427 }
1e33ac1e 6428
c25b8f99
BB
6429 run_threads[t] = thread_create(NULL, 0, ztest_thread,
6430 (void *)(uintptr_t)t, 0, NULL, TS_RUN | TS_JOINABLE,
6431 defclsyspri);
34dc7c2f
BB
6432 }
6433
428870ff
BB
6434 /*
6435 * Wait for all of the tests to complete. We go in reverse order
6436 * so we don't close datasets while threads are still using them.
6437 */
c242c188 6438 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
c25b8f99 6439 VERIFY0(thread_join(run_threads[t]));
c242c188
CS
6440 if (t < ztest_opts.zo_datasets)
6441 ztest_dataset_close(t);
34dc7c2f
BB
6442 }
6443
34dc7c2f
BB
6444 txg_wait_synced(spa_get_dsl(spa), 0);
6445
428870ff
BB
6446 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6447 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6448
c25b8f99 6449 umem_free(run_threads, ztest_opts.zo_threads * sizeof (kthread_t *));
428870ff
BB
6450
6451 /* Kill the resume thread */
6452 ztest_exiting = B_TRUE;
c25b8f99 6453 VERIFY0(thread_join(resume_thread));
428870ff 6454 ztest_resume(spa);
34dc7c2f
BB
6455
6456 /*
428870ff
BB
6457 * Right before closing the pool, kick off a bunch of async I/O;
6458 * spa_close() should wait for it to complete.
34dc7c2f 6459 */
fcff0f35
PD
6460 for (object = 1; object < 50; object++) {
6461 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6462 ZIO_PRIORITY_SYNC_READ);
6463 }
428870ff 6464
090ff092
RC
6465 /* Verify that at least one commit cb was called in a timely fashion */
6466 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
c99c9001 6467 VERIFY0(zc_min_txg_delay);
090ff092 6468
428870ff
BB
6469 spa_close(spa, FTAG);
6470
6471 /*
6472 * Verify that we can loop over all pools.
6473 */
6474 mutex_enter(&spa_namespace_lock);
6475 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
c242c188 6476 if (ztest_opts.zo_verbose > 3)
428870ff
BB
6477 (void) printf("spa_next: found %s\n", spa_name(spa));
6478 mutex_exit(&spa_namespace_lock);
6479
6480 /*
6481 * Verify that we can export the pool and reimport it under a
6482 * different name.
6483 */
379ca9cf 6484 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
eca7b760
IK
6485 char name[ZFS_MAX_DATASET_NAME_LEN];
6486 (void) snprintf(name, sizeof (name), "%s_import",
c242c188
CS
6487 ztest_opts.zo_pool);
6488 ztest_spa_import_export(ztest_opts.zo_pool, name);
6489 ztest_spa_import_export(name, ztest_opts.zo_pool);
428870ff
BB
6490 }
6491
6492 kernel_fini();
572e2857
BB
6493
6494 list_destroy(&zcl.zcl_callbacks);
0e8d1b2d 6495 mutex_destroy(&zcl.zcl_callbacks_lock);
7809eb8b 6496 (void) rwlock_destroy(&ztest_name_lock);
c242c188 6497 mutex_destroy(&ztest_vdev_lock);
428870ff
BB
6498}
6499
6500static void
c242c188 6501ztest_freeze(void)
428870ff 6502{
c242c188 6503 ztest_ds_t *zd = &ztest_ds[0];
428870ff
BB
6504 spa_t *spa;
6505 int numloops = 0;
6506
c242c188 6507 if (ztest_opts.zo_verbose >= 3)
428870ff 6508 (void) printf("testing spa_freeze()...\n");
9babb374 6509
428870ff 6510 kernel_init(FREAD | FWRITE);
c242c188
CS
6511 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6512 VERIFY3U(0, ==, ztest_dataset_open(0));
03c6040b
GW
6513 spa->spa_debug = B_TRUE;
6514 ztest_spa = spa;
9babb374 6515
428870ff
BB
6516 /*
6517 * Force the first log block to be transactionally allocated.
6518 * We have to do this before we freeze the pool -- otherwise
6519 * the log chain won't be anchored.
6520 */
6521 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6522 ztest_dmu_object_alloc_free(zd, 0);
572e2857 6523 zil_commit(zd->zd_zilog, 0);
34dc7c2f
BB
6524 }
6525
6526 txg_wait_synced(spa_get_dsl(spa), 0);
6527
428870ff
BB
6528 /*
6529 * Freeze the pool. This stops spa_sync() from doing anything,
6530 * so that the only way to record changes from now on is the ZIL.
6531 */
6532 spa_freeze(spa);
b128c09f 6533
b02fe35d
AR
6534 /*
6535 * Because it is hard to predict how much space a write will actually
6536 * require beforehand, we leave ourselves some fudge space to write over
6537 * capacity.
6538 */
6539 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6540
428870ff
BB
6541 /*
6542 * Run tests that generate log records but don't alter the pool config
6543 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6544 * We do a txg_wait_synced() after each iteration to force the txg
6545 * to increase well beyond the last synced value in the uberblock.
6546 * The ZIL should be OK with that.
b02fe35d
AR
6547 *
6548 * Run a random number of times less than zo_maxloops and ensure we do
6549 * not run out of space on the pool.
428870ff 6550 */
c242c188 6551 while (ztest_random(10) != 0 &&
b02fe35d
AR
6552 numloops++ < ztest_opts.zo_maxloops &&
6553 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6554 ztest_od_t od;
50c957f7 6555 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
b02fe35d
AR
6556 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6557 ztest_io(zd, od.od_object,
6558 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
428870ff
BB
6559 txg_wait_synced(spa_get_dsl(spa), 0);
6560 }
b128c09f 6561
34dc7c2f 6562 /*
428870ff 6563 * Commit all of the changes we just generated.
34dc7c2f 6564 */
572e2857 6565 zil_commit(zd->zd_zilog, 0);
428870ff 6566 txg_wait_synced(spa_get_dsl(spa), 0);
34dc7c2f 6567
428870ff
BB
6568 /*
6569 * Close our dataset and close the pool.
6570 */
c242c188 6571 ztest_dataset_close(0);
34dc7c2f 6572 spa_close(spa, FTAG);
428870ff 6573 kernel_fini();
34dc7c2f 6574
428870ff
BB
6575 /*
6576 * Open and close the pool and dataset to induce log replay.
6577 */
6578 kernel_init(FREAD | FWRITE);
c242c188 6579 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
29809a6c 6580 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
c242c188
CS
6581 VERIFY3U(0, ==, ztest_dataset_open(0));
6582 ztest_dataset_close(0);
3bc7e0fb
GW
6583
6584 spa->spa_debug = B_TRUE;
6585 ztest_spa = spa;
6586 txg_wait_synced(spa_get_dsl(spa), 0);
6587 ztest_reguid(NULL, 0);
6588
428870ff 6589 spa_close(spa, FTAG);
34dc7c2f
BB
6590 kernel_fini();
6591}
6592
6593void
6594print_time(hrtime_t t, char *timebuf)
6595{
6596 hrtime_t s = t / NANOSEC;
6597 hrtime_t m = s / 60;
6598 hrtime_t h = m / 60;
6599 hrtime_t d = h / 24;
6600
6601 s -= m * 60;
6602 m -= h * 60;
6603 h -= d * 24;
6604
6605 timebuf[0] = '\0';
6606
6607 if (d)
6608 (void) sprintf(timebuf,
6609 "%llud%02lluh%02llum%02llus", d, h, m, s);
6610 else if (h)
6611 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6612 else if (m)
6613 (void) sprintf(timebuf, "%llum%02llus", m, s);
6614 else
6615 (void) sprintf(timebuf, "%llus", s);
6616}
6617
428870ff 6618static nvlist_t *
0bc8fd78 6619make_random_props(void)
428870ff
BB
6620{
6621 nvlist_t *props;
6622
428870ff 6623 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
9ae529ec
CS
6624 if (ztest_random(2) == 0)
6625 return (props);
428870ff
BB
6626 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6627
428870ff
BB
6628 return (props);
6629}
6630
379ca9cf
OF
6631/*
6632 * Import a storage pool with the given name.
6633 */
6634static void
6635ztest_import(ztest_shared_t *zs)
6636{
6637 libzfs_handle_t *hdl;
6638 importargs_t args = { 0 };
6639 spa_t *spa;
6640 nvlist_t *cfg = NULL;
6641 int nsearch = 1;
6642 char *searchdirs[nsearch];
6643 char *name = ztest_opts.zo_pool;
6644 int flags = ZFS_IMPORT_MISSING_LOG;
6645 int error;
6646
6647 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6648 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6649
6650 kernel_init(FREAD | FWRITE);
6651 hdl = libzfs_init();
6652
6653 searchdirs[0] = ztest_opts.zo_dir;
6654 args.paths = nsearch;
6655 args.path = searchdirs;
6656 args.can_be_active = B_FALSE;
6657
6658 error = zpool_tryimport(hdl, name, &cfg, &args);
6659 if (error)
6660 (void) fatal(0, "No pools found\n");
6661
6662 VERIFY0(spa_import(name, cfg, NULL, flags));
6663 VERIFY0(spa_open(name, &spa, FTAG));
6664 zs->zs_metaslab_sz =
6665 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6666 spa_close(spa, FTAG);
6667
6668 libzfs_fini(hdl);
6669 kernel_fini();
6670
6671 if (!ztest_opts.zo_mmp_test) {
6672 ztest_run_zdb(ztest_opts.zo_pool);
6673 ztest_freeze();
6674 ztest_run_zdb(ztest_opts.zo_pool);
6675 }
6676
6677 (void) rwlock_destroy(&ztest_name_lock);
6678 mutex_destroy(&ztest_vdev_lock);
6679}
6680
34dc7c2f
BB
6681/*
6682 * Create a storage pool with the given name and initial vdev size.
428870ff 6683 * Then test spa_freeze() functionality.
34dc7c2f
BB
6684 */
6685static void
428870ff 6686ztest_init(ztest_shared_t *zs)
34dc7c2f
BB
6687{
6688 spa_t *spa;
428870ff 6689 nvlist_t *nvroot, *props;
9ae529ec 6690 int i;
428870ff 6691
c242c188 6692 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
7809eb8b 6693 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
34dc7c2f
BB
6694
6695 kernel_init(FREAD | FWRITE);
6696
6697 /*
6698 * Create the storage pool.
6699 */
c242c188 6700 (void) spa_destroy(ztest_opts.zo_pool);
428870ff
BB
6701 ztest_shared->zs_vdev_next_leaf = 0;
6702 zs->zs_splits = 0;
c242c188 6703 zs->zs_mirrors = ztest_opts.zo_mirrors;
ea0b2538 6704 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
c242c188 6705 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
428870ff 6706 props = make_random_props();
9ae529ec
CS
6707 for (i = 0; i < SPA_FEATURES; i++) {
6708 char *buf;
6709 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6710 spa_feature_table[i].fi_uname));
6711 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6712 free(buf);
6713 }
b5256303
TC
6714 VERIFY3U(0, ==,
6715 spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
34dc7c2f 6716 nvlist_free(nvroot);
85802aa4 6717 nvlist_free(props);
34dc7c2f 6718
c242c188
CS
6719 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6720 zs->zs_metaslab_sz =
6721 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
34dc7c2f
BB
6722 spa_close(spa, FTAG);
6723
6724 kernel_fini();
428870ff 6725
379ca9cf
OF
6726 if (!ztest_opts.zo_mmp_test) {
6727 ztest_run_zdb(ztest_opts.zo_pool);
6728 ztest_freeze();
6729 ztest_run_zdb(ztest_opts.zo_pool);
6730 }
c242c188 6731
7809eb8b 6732 (void) rwlock_destroy(&ztest_name_lock);
c242c188
CS
6733 mutex_destroy(&ztest_vdev_lock);
6734}
6735
6736static void
9d81146b 6737setup_data_fd(void)
c242c188 6738{
facbbe43
BB
6739 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6740
6741 ztest_fd_data = mkstemp(ztest_name_data);
9d81146b 6742 ASSERT3S(ztest_fd_data, >=, 0);
facbbe43 6743 (void) unlink(ztest_name_data);
c242c188
CS
6744}
6745
22257dc0
CS
6746static int
6747shared_data_size(ztest_shared_hdr_t *hdr)
6748{
6749 int size;
6750
6751 size = hdr->zh_hdr_size;
6752 size += hdr->zh_opts_size;
6753 size += hdr->zh_size;
6754 size += hdr->zh_stats_size * hdr->zh_stats_count;
6755 size += hdr->zh_ds_size * hdr->zh_ds_count;
6756
6757 return (size);
6758}
6759
c242c188
CS
6760static void
6761setup_hdr(void)
6762{
22257dc0 6763 int size;
c242c188
CS
6764 ztest_shared_hdr_t *hdr;
6765
6766 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6767 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
799402d8 6768 ASSERT(hdr != MAP_FAILED);
c242c188 6769
9d81146b 6770 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
22257dc0 6771
c242c188
CS
6772 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6773 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6774 hdr->zh_size = sizeof (ztest_shared_t);
6775 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6776 hdr->zh_stats_count = ZTEST_FUNCS;
6777 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6778 hdr->zh_ds_count = ztest_opts.zo_datasets;
6779
22257dc0 6780 size = shared_data_size(hdr);
9d81146b 6781 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
22257dc0 6782
c242c188
CS
6783 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6784}
6785
6786static void
6787setup_data(void)
6788{
6789 int size, offset;
6790 ztest_shared_hdr_t *hdr;
6791 uint8_t *buf;
6792
6793 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
9d81146b 6794 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
799402d8 6795 ASSERT(hdr != MAP_FAILED);
c242c188 6796
22257dc0 6797 size = shared_data_size(hdr);
c242c188
CS
6798
6799 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6800 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
9d81146b 6801 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
799402d8 6802 ASSERT(hdr != MAP_FAILED);
c242c188
CS
6803 buf = (uint8_t *)hdr;
6804
6805 offset = hdr->zh_hdr_size;
6806 ztest_shared_opts = (void *)&buf[offset];
6807 offset += hdr->zh_opts_size;
6808 ztest_shared = (void *)&buf[offset];
6809 offset += hdr->zh_size;
6810 ztest_shared_callstate = (void *)&buf[offset];
6811 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6812 ztest_shared_ds = (void *)&buf[offset];
6813}
6814
6815static boolean_t
6816exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6817{
6818 pid_t pid;
6819 int status;
483106eb 6820 char *cmdbuf = NULL;
c242c188
CS
6821
6822 pid = fork();
6823
6824 if (cmd == NULL) {
483106eb
BB
6825 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6826 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
c242c188
CS
6827 cmd = cmdbuf;
6828 }
6829
6830 if (pid == -1)
6831 fatal(1, "fork failed");
6832
6833 if (pid == 0) { /* child */
6834 char *emptyargv[2] = { cmd, NULL };
9d81146b 6835 char fd_data_str[12];
c242c188
CS
6836
6837 struct rlimit rl = { 1024, 1024 };
6838 (void) setrlimit(RLIMIT_NOFILE, &rl);
9d81146b
ED
6839
6840 (void) close(ztest_fd_rand);
6841 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6842 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6843
c242c188
CS
6844 (void) enable_extended_FILE_stdio(-1, -1);
6845 if (libpath != NULL)
6846 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6847 (void) execv(cmd, emptyargv);
6848 ztest_dump_core = B_FALSE;
6849 fatal(B_TRUE, "exec failed: %s", cmd);
6850 }
6851
483106eb
BB
6852 if (cmdbuf != NULL) {
6853 umem_free(cmdbuf, MAXPATHLEN);
6854 cmd = NULL;
6855 }
6856
c242c188
CS
6857 while (waitpid(pid, &status, 0) != pid)
6858 continue;
6859 if (statusp != NULL)
6860 *statusp = status;
6861
6862 if (WIFEXITED(status)) {
6863 if (WEXITSTATUS(status) != 0) {
6864 (void) fprintf(stderr, "child exited with code %d\n",
6865 WEXITSTATUS(status));
6866 exit(2);
6867 }
6868 return (B_FALSE);
6869 } else if (WIFSIGNALED(status)) {
6870 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6871 (void) fprintf(stderr, "child died with signal %d\n",
6872 WTERMSIG(status));
6873 exit(3);
6874 }
6875 return (B_TRUE);
6876 } else {
6877 (void) fprintf(stderr, "something strange happened to child\n");
6878 exit(4);
6879 /* NOTREACHED */
6880 }
6881}
6882
6883static void
6884ztest_run_init(void)
6885{
6886 int i;
428870ff 6887
c242c188 6888 ztest_shared_t *zs = ztest_shared;
428870ff 6889
c242c188
CS
6890 /*
6891 * Blow away any existing copy of zpool.cache
6892 */
6893 (void) remove(spa_config_path);
6894
379ca9cf
OF
6895 if (ztest_opts.zo_init == 0) {
6896 if (ztest_opts.zo_verbose >= 1)
6897 (void) printf("Importing pool %s\n",
6898 ztest_opts.zo_pool);
6899 ztest_import(zs);
6900 return;
6901 }
6902
c242c188
CS
6903 /*
6904 * Create and initialize our storage pool.
6905 */
6906 for (i = 1; i <= ztest_opts.zo_init; i++) {
6907 bzero(zs, sizeof (ztest_shared_t));
6908 if (ztest_opts.zo_verbose >= 3 &&
6909 ztest_opts.zo_init != 1) {
6910 (void) printf("ztest_init(), pass %d\n", i);
6911 }
6912 ztest_init(zs);
6913 }
34dc7c2f
BB
6914}
6915
6916int
6917main(int argc, char **argv)
6918{
6919 int kills = 0;
6920 int iters = 0;
c242c188
CS
6921 int older = 0;
6922 int newer = 0;
34dc7c2f
BB
6923 ztest_shared_t *zs;
6924 ztest_info_t *zi;
c242c188 6925 ztest_shared_callstate_t *zc;
34dc7c2f
BB
6926 char timebuf[100];
6927 char numbuf[6];
428870ff 6928 spa_t *spa;
483106eb 6929 char *cmd;
c242c188
CS
6930 boolean_t hasalt;
6931 int f;
9d81146b 6932 char *fd_data_str = getenv("ZTEST_FD_DATA");
e82cdc3a 6933 struct sigaction action;
34dc7c2f
BB
6934
6935 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6936
498877ba
MA
6937 dprintf_setup(&argc, argv);
6938
e82cdc3a
NB
6939 action.sa_handler = sig_handler;
6940 sigemptyset(&action.sa_mask);
6941 action.sa_flags = 0;
6942
6943 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6944 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6945 strerror(errno));
6946 exit(EXIT_FAILURE);
6947 }
6948
6949 if (sigaction(SIGABRT, &action, NULL) < 0) {
6950 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6951 strerror(errno));
6952 exit(EXIT_FAILURE);
6953 }
6954
9d81146b
ED
6955 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6956 ASSERT3S(ztest_fd_rand, >=, 0);
6957
6958 if (!fd_data_str) {
c242c188 6959 process_options(argc, argv);
34dc7c2f 6960
9d81146b 6961 setup_data_fd();
c242c188
CS
6962 setup_hdr();
6963 setup_data();
6964 bcopy(&ztest_opts, ztest_shared_opts,
6965 sizeof (*ztest_shared_opts));
6966 } else {
9d81146b 6967 ztest_fd_data = atoi(fd_data_str);
c242c188
CS
6968 setup_data();
6969 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6970 }
6971 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
34dc7c2f 6972
428870ff 6973 /* Override location of zpool.cache */
5be98cfe
BB
6974 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6975 ztest_opts.zo_dir) != -1);
9d81146b 6976
c242c188
CS
6977 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6978 UMEM_NOFAIL);
6979 zs = ztest_shared;
6980
9d81146b 6981 if (fd_data_str) {
c242c188
CS
6982 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6983 metaslab_df_alloc_threshold =
6984 zs->zs_metaslab_df_alloc_threshold;
6985
6986 if (zs->zs_do_init)
6987 ztest_run_init();
6988 else
6989 ztest_run(zs);
6990 exit(0);
6991 }
34dc7c2f 6992
c242c188 6993 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
34dc7c2f 6994
c242c188 6995 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
6996 (void) printf("%llu vdevs, %d datasets, %d threads,"
6997 " %llu seconds...\n",
c242c188
CS
6998 (u_longlong_t)ztest_opts.zo_vdevs,
6999 ztest_opts.zo_datasets,
7000 ztest_opts.zo_threads,
7001 (u_longlong_t)ztest_opts.zo_time);
34dc7c2f
BB
7002 }
7003
483106eb
BB
7004 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7005 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
c242c188
CS
7006
7007 zs->zs_do_init = B_TRUE;
7008 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7009 if (ztest_opts.zo_verbose >= 1) {
7010 (void) printf("Executing older ztest for "
7011 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7012 }
7013 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7014 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7015 } else {
7016 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
34dc7c2f 7017 }
c242c188 7018 zs->zs_do_init = B_FALSE;
34dc7c2f 7019
428870ff 7020 zs->zs_proc_start = gethrtime();
c242c188 7021 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
34dc7c2f 7022
d6320ddb 7023 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7024 zi = &ztest_info[f];
7025 zc = ZTEST_GET_SHARED_CALLSTATE(f);
428870ff 7026 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
c242c188 7027 zc->zc_next = UINT64_MAX;
34dc7c2f 7028 else
c242c188 7029 zc->zc_next = zs->zs_proc_start +
428870ff 7030 ztest_random(2 * zi->zi_interval[0] + 1);
34dc7c2f
BB
7031 }
7032
34dc7c2f
BB
7033 /*
7034 * Run the tests in a loop. These tests include fault injection
7035 * to verify that self-healing data works, and forced crashes
7036 * to verify that we never lose on-disk consistency.
7037 */
428870ff 7038 while (gethrtime() < zs->zs_proc_stop) {
34dc7c2f 7039 int status;
c242c188 7040 boolean_t killed;
34dc7c2f
BB
7041
7042 /*
7043 * Initialize the workload counters for each function.
7044 */
d6320ddb 7045 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7046 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7047 zc->zc_count = 0;
7048 zc->zc_time = 0;
34dc7c2f
BB
7049 }
7050
9babb374 7051 /* Set the allocation switch size */
c242c188
CS
7052 zs->zs_metaslab_df_alloc_threshold =
7053 ztest_random(zs->zs_metaslab_sz / 4) + 1;
9babb374 7054
c242c188
CS
7055 if (!hasalt || ztest_random(2) == 0) {
7056 if (hasalt && ztest_opts.zo_verbose >= 1) {
7057 (void) printf("Executing newer ztest: %s\n",
7058 cmd);
34dc7c2f 7059 }
c242c188
CS
7060 newer++;
7061 killed = exec_child(cmd, NULL, B_TRUE, &status);
34dc7c2f 7062 } else {
c242c188
CS
7063 if (hasalt && ztest_opts.zo_verbose >= 1) {
7064 (void) printf("Executing older ztest: %s\n",
7065 ztest_opts.zo_alt_ztest);
7066 }
7067 older++;
7068 killed = exec_child(ztest_opts.zo_alt_ztest,
7069 ztest_opts.zo_alt_libpath, B_TRUE, &status);
34dc7c2f
BB
7070 }
7071
c242c188
CS
7072 if (killed)
7073 kills++;
34dc7c2f
BB
7074 iters++;
7075
c242c188 7076 if (ztest_opts.zo_verbose >= 1) {
34dc7c2f
BB
7077 hrtime_t now = gethrtime();
7078
428870ff
BB
7079 now = MIN(now, zs->zs_proc_stop);
7080 print_time(zs->zs_proc_stop - now, timebuf);
34dc7c2f
BB
7081 nicenum(zs->zs_space, numbuf);
7082
7083 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7084 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7085 iters,
7086 WIFEXITED(status) ? "Complete" : "SIGKILL",
7087 (u_longlong_t)zs->zs_enospc_count,
7088 100.0 * zs->zs_alloc / zs->zs_space,
7089 numbuf,
428870ff 7090 100.0 * (now - zs->zs_proc_start) /
c242c188 7091 (ztest_opts.zo_time * NANOSEC), timebuf);
34dc7c2f
BB
7092 }
7093
c242c188 7094 if (ztest_opts.zo_verbose >= 2) {
34dc7c2f
BB
7095 (void) printf("\nWorkload summary:\n\n");
7096 (void) printf("%7s %9s %s\n",
7097 "Calls", "Time", "Function");
7098 (void) printf("%7s %9s %s\n",
7099 "-----", "----", "--------");
d6320ddb 7100 for (f = 0; f < ZTEST_FUNCS; f++) {
c242c188
CS
7101 zi = &ztest_info[f];
7102 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7103 print_time(zc->zc_time, timebuf);
34dc7c2f 7104 (void) printf("%7llu %9s %s\n",
c242c188 7105 (u_longlong_t)zc->zc_count, timebuf,
fdc5d982 7106 zi->zi_funcname);
34dc7c2f
BB
7107 }
7108 (void) printf("\n");
7109 }
7110
7111 /*
428870ff
BB
7112 * It's possible that we killed a child during a rename test,
7113 * in which case we'll have a 'ztest_tmp' pool lying around
7114 * instead of 'ztest'. Do a blind rename in case this happened.
34dc7c2f 7115 */
428870ff 7116 kernel_init(FREAD);
c242c188 7117 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
428870ff
BB
7118 spa_close(spa, FTAG);
7119 } else {
eca7b760 7120 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
7121 kernel_fini();
7122 kernel_init(FREAD | FWRITE);
7123 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
c242c188
CS
7124 ztest_opts.zo_pool);
7125 (void) spa_rename(tmpname, ztest_opts.zo_pool);
428870ff 7126 }
34dc7c2f 7127 kernel_fini();
34dc7c2f 7128
379ca9cf
OF
7129 if (!ztest_opts.zo_mmp_test)
7130 ztest_run_zdb(ztest_opts.zo_pool);
428870ff 7131 }
34dc7c2f 7132
c242c188
CS
7133 if (ztest_opts.zo_verbose >= 1) {
7134 if (hasalt) {
7135 (void) printf("%d runs of older ztest: %s\n", older,
7136 ztest_opts.zo_alt_ztest);
7137 (void) printf("%d runs of newer ztest: %s\n", newer,
7138 cmd);
7139 }
34dc7c2f
BB
7140 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7141 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7142 }
7143
483106eb
BB
7144 umem_free(cmd, MAXNAMELEN);
7145
34dc7c2f
BB
7146 return (0);
7147}