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