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