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