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