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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
28 * Copyright (c) 2017, Intel Corporation.
32 * The objective of this program is to provide a DMU/ZAP/SPA stress test
33 * that runs entirely in userland, is easy to use, and easy to extend.
35 * The overall design of the ztest program is as follows:
37 * (1) For each major functional area (e.g. adding vdevs to a pool,
38 * creating and destroying datasets, reading and writing objects, etc)
39 * we have a simple routine to test that functionality. These
40 * individual routines do not have to do anything "stressful".
42 * (2) We turn these simple functionality tests into a stress test by
43 * running them all in parallel, with as many threads as desired,
44 * and spread across as many datasets, objects, and vdevs as desired.
46 * (3) While all this is happening, we inject faults into the pool to
47 * verify that self-healing data really works.
49 * (4) Every time we open a dataset, we change its checksum and compression
50 * functions. Thus even individual objects vary from block to block
51 * in which checksum they use and whether they're compressed.
53 * (5) To verify that we never lose on-disk consistency after a crash,
54 * we run the entire test in a child of the main process.
55 * At random times, the child self-immolates with a SIGKILL.
56 * This is the software equivalent of pulling the power cord.
57 * The parent then runs the test again, using the existing
58 * storage pool, as many times as desired. If backwards compatibility
59 * testing is enabled ztest will sometimes run the "older" version
60 * of ztest after a SIGKILL.
62 * (6) To verify that we don't have future leaks or temporal incursions,
63 * many of the functional tests record the transaction group number
64 * as part of their data. When reading old data, they verify that
65 * the transaction group number is less than the current, open txg.
66 * If you add a new test, please do this if applicable.
68 * (7) Threads are created with a reduced stack size, for sanity checking.
69 * Therefore, it's important not to allocate huge buffers on the stack.
71 * When run with no arguments, ztest runs for about five minutes and
72 * produces no output if successful. To get a little bit of information,
73 * specify -V. To get more information, specify -VV, and so on.
75 * To turn this into an overnight stress test, use -T to specify run time.
77 * You can ask more vdevs [-v], datasets [-d], or threads [-t]
78 * to increase the pool capacity, fanout, and overall stress level.
80 * Use the -k option to set the desired frequency of kills.
82 * When ztest invokes itself it passes all relevant information through a
83 * temporary file which is mmap-ed in the child process. This allows shared
84 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
85 * stored at offset 0 of this file and contains information on the size and
86 * number of shared structures in the file. The information stored in this file
87 * must remain backwards compatible with older versions of ztest so that
88 * ztest can invoke them during backwards compatibility testing (-B).
91 #include <sys/zfs_context.h>
97 #include <sys/dmu_objset.h>
100 #include <sys/time.h>
101 #include <sys/wait.h>
102 #include <sys/mman.h>
103 #include <sys/resource.h>
106 #include <sys/zil_impl.h>
107 #include <sys/vdev_draid.h>
108 #include <sys/vdev_impl.h>
109 #include <sys/vdev_file.h>
110 #include <sys/vdev_initialize.h>
111 #include <sys/vdev_raidz.h>
112 #include <sys/vdev_trim.h>
113 #include <sys/spa_impl.h>
114 #include <sys/metaslab_impl.h>
115 #include <sys/dsl_prop.h>
116 #include <sys/dsl_dataset.h>
117 #include <sys/dsl_destroy.h>
118 #include <sys/dsl_scan.h>
119 #include <sys/zio_checksum.h>
120 #include <sys/zfs_refcount.h>
121 #include <sys/zfeature.h>
122 #include <sys/dsl_userhold.h>
131 #include <sys/fs/zfs.h>
132 #include <zfs_fletcher.h>
133 #include <libnvpair.h>
134 #include <libzutil.h>
135 #include <sys/crypto/icp.h>
137 #include <execinfo.h> /* for backtrace() */
140 static int ztest_fd_data
= -1;
141 static int ztest_fd_rand
= -1;
143 typedef struct ztest_shared_hdr
{
144 uint64_t zh_hdr_size
;
145 uint64_t zh_opts_size
;
147 uint64_t zh_stats_size
;
148 uint64_t zh_stats_count
;
150 uint64_t zh_ds_count
;
151 } ztest_shared_hdr_t
;
153 static ztest_shared_hdr_t
*ztest_shared_hdr
;
155 enum ztest_class_state
{
156 ZTEST_VDEV_CLASS_OFF
,
161 typedef struct ztest_shared_opts
{
162 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
163 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
164 char zo_alt_ztest
[MAXNAMELEN
];
165 char zo_alt_libpath
[MAXNAMELEN
];
167 uint64_t zo_vdevtime
;
171 int zo_raid_children
;
173 char zo_raid_type
[8];
178 uint64_t zo_passtime
;
179 uint64_t zo_killrate
;
183 uint64_t zo_maxloops
;
184 uint64_t zo_metaslab_force_ganging
;
186 int zo_special_vdevs
;
188 } ztest_shared_opts_t
;
190 static const ztest_shared_opts_t ztest_opts_defaults
= {
193 .zo_alt_ztest
= { '\0' },
194 .zo_alt_libpath
= { '\0' },
196 .zo_ashift
= SPA_MINBLOCKSHIFT
,
198 .zo_raid_children
= 4,
200 .zo_raid_type
= VDEV_TYPE_RAIDZ
,
201 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
202 .zo_draid_data
= 4, /* data drives */
203 .zo_draid_spares
= 1, /* distributed spares */
206 .zo_passtime
= 60, /* 60 seconds */
207 .zo_killrate
= 70, /* 70% kill rate */
211 .zo_time
= 300, /* 5 minutes */
212 .zo_maxloops
= 50, /* max loops during spa_freeze() */
213 .zo_metaslab_force_ganging
= 64 << 10,
214 .zo_special_vdevs
= ZTEST_VDEV_CLASS_RND
,
217 extern uint64_t metaslab_force_ganging
;
218 extern uint64_t metaslab_df_alloc_threshold
;
219 extern unsigned long zfs_deadman_synctime_ms
;
220 extern int metaslab_preload_limit
;
221 extern boolean_t zfs_compressed_arc_enabled
;
222 extern int zfs_abd_scatter_enabled
;
223 extern int dmu_object_alloc_chunk_shift
;
224 extern boolean_t zfs_force_some_double_word_sm_entries
;
225 extern unsigned long zio_decompress_fail_fraction
;
226 extern unsigned long zfs_reconstruct_indirect_damage_fraction
;
229 static ztest_shared_opts_t
*ztest_shared_opts
;
230 static ztest_shared_opts_t ztest_opts
;
231 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
233 typedef struct ztest_shared_ds
{
237 static ztest_shared_ds_t
*ztest_shared_ds
;
238 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
240 #define BT_MAGIC 0x123456789abcdefULL
241 #define MAXFAULTS(zs) \
242 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raid_parity + 1) - 1)
246 ZTEST_IO_WRITE_PATTERN
,
247 ZTEST_IO_WRITE_ZEROES
,
254 typedef struct ztest_block_tag
{
258 uint64_t bt_dnodesize
;
265 typedef struct bufwad
{
272 * It would be better to use a rangelock_t per object. Unfortunately
273 * the rangelock_t is not a drop-in replacement for rl_t, because we
274 * still need to map from object ID to rangelock_t.
296 #define ZTEST_RANGE_LOCKS 64
297 #define ZTEST_OBJECT_LOCKS 64
300 * Object descriptor. Used as a template for object lookup/create/remove.
302 typedef struct ztest_od
{
305 dmu_object_type_t od_type
;
306 dmu_object_type_t od_crtype
;
307 uint64_t od_blocksize
;
308 uint64_t od_crblocksize
;
309 uint64_t od_crdnodesize
;
312 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
318 typedef struct ztest_ds
{
319 ztest_shared_ds_t
*zd_shared
;
321 pthread_rwlock_t zd_zilog_lock
;
323 ztest_od_t
*zd_od
; /* debugging aid */
324 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
325 kmutex_t zd_dirobj_lock
;
326 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
327 rll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
331 * Per-iteration state.
333 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
335 typedef struct ztest_info
{
336 ztest_func_t
*zi_func
; /* test function */
337 uint64_t zi_iters
; /* iterations per execution */
338 uint64_t *zi_interval
; /* execute every <interval> seconds */
339 const char *zi_funcname
; /* name of test function */
342 typedef struct ztest_shared_callstate
{
343 uint64_t zc_count
; /* per-pass count */
344 uint64_t zc_time
; /* per-pass time */
345 uint64_t zc_next
; /* next time to call this function */
346 } ztest_shared_callstate_t
;
348 static ztest_shared_callstate_t
*ztest_shared_callstate
;
349 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
351 ztest_func_t ztest_dmu_read_write
;
352 ztest_func_t ztest_dmu_write_parallel
;
353 ztest_func_t ztest_dmu_object_alloc_free
;
354 ztest_func_t ztest_dmu_object_next_chunk
;
355 ztest_func_t ztest_dmu_commit_callbacks
;
356 ztest_func_t ztest_zap
;
357 ztest_func_t ztest_zap_parallel
;
358 ztest_func_t ztest_zil_commit
;
359 ztest_func_t ztest_zil_remount
;
360 ztest_func_t ztest_dmu_read_write_zcopy
;
361 ztest_func_t ztest_dmu_objset_create_destroy
;
362 ztest_func_t ztest_dmu_prealloc
;
363 ztest_func_t ztest_fzap
;
364 ztest_func_t ztest_dmu_snapshot_create_destroy
;
365 ztest_func_t ztest_dsl_prop_get_set
;
366 ztest_func_t ztest_spa_prop_get_set
;
367 ztest_func_t ztest_spa_create_destroy
;
368 ztest_func_t ztest_fault_inject
;
369 ztest_func_t ztest_dmu_snapshot_hold
;
370 ztest_func_t ztest_mmp_enable_disable
;
371 ztest_func_t ztest_scrub
;
372 ztest_func_t ztest_dsl_dataset_promote_busy
;
373 ztest_func_t ztest_vdev_attach_detach
;
374 ztest_func_t ztest_vdev_LUN_growth
;
375 ztest_func_t ztest_vdev_add_remove
;
376 ztest_func_t ztest_vdev_class_add
;
377 ztest_func_t ztest_vdev_aux_add_remove
;
378 ztest_func_t ztest_split_pool
;
379 ztest_func_t ztest_reguid
;
380 ztest_func_t ztest_spa_upgrade
;
381 ztest_func_t ztest_device_removal
;
382 ztest_func_t ztest_spa_checkpoint_create_discard
;
383 ztest_func_t ztest_initialize
;
384 ztest_func_t ztest_trim
;
385 ztest_func_t ztest_fletcher
;
386 ztest_func_t ztest_fletcher_incr
;
387 ztest_func_t ztest_verify_dnode_bt
;
389 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
390 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
391 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
392 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
393 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
395 #define ZTI_INIT(func, iters, interval) \
396 { .zi_func = (func), \
397 .zi_iters = (iters), \
398 .zi_interval = (interval), \
399 .zi_funcname = # func }
401 ztest_info_t ztest_info
[] = {
402 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
403 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
404 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
405 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
406 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
407 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
408 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
409 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
410 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
411 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
412 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
413 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
414 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
415 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
417 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
419 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
420 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
421 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
422 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
423 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
424 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
425 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
426 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
427 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
428 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
429 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
430 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
431 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
432 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
433 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
434 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
435 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
436 ZTI_INIT(ztest_initialize
, 1, &zopt_sometimes
),
437 ZTI_INIT(ztest_trim
, 1, &zopt_sometimes
),
438 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
439 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
440 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
443 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
446 * The following struct is used to hold a list of uncalled commit callbacks.
447 * The callbacks are ordered by txg number.
449 typedef struct ztest_cb_list
{
450 kmutex_t zcl_callbacks_lock
;
451 list_t zcl_callbacks
;
455 * Stuff we need to share writably between parent and child.
457 typedef struct ztest_shared
{
458 boolean_t zs_do_init
;
459 hrtime_t zs_proc_start
;
460 hrtime_t zs_proc_stop
;
461 hrtime_t zs_thread_start
;
462 hrtime_t zs_thread_stop
;
463 hrtime_t zs_thread_kill
;
464 uint64_t zs_enospc_count
;
465 uint64_t zs_vdev_next_leaf
;
466 uint64_t zs_vdev_aux
;
471 uint64_t zs_metaslab_sz
;
472 uint64_t zs_metaslab_df_alloc_threshold
;
476 #define ID_PARALLEL -1ULL
478 static char ztest_dev_template
[] = "%s/%s.%llua";
479 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
480 ztest_shared_t
*ztest_shared
;
482 static spa_t
*ztest_spa
= NULL
;
483 static ztest_ds_t
*ztest_ds
;
485 static kmutex_t ztest_vdev_lock
;
486 static boolean_t ztest_device_removal_active
= B_FALSE
;
487 static boolean_t ztest_pool_scrubbed
= B_FALSE
;
488 static kmutex_t ztest_checkpoint_lock
;
491 * The ztest_name_lock protects the pool and dataset namespace used by
492 * the individual tests. To modify the namespace, consumers must grab
493 * this lock as writer. Grabbing the lock as reader will ensure that the
494 * namespace does not change while the lock is held.
496 static pthread_rwlock_t ztest_name_lock
;
498 static boolean_t ztest_dump_core
= B_TRUE
;
499 static boolean_t ztest_exiting
;
501 /* Global commit callback list */
502 static ztest_cb_list_t zcl
;
503 /* Commit cb delay */
504 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
505 static int zc_cb_counter
= 0;
508 * Minimum number of commit callbacks that need to be registered for us to check
509 * whether the minimum txg delay is acceptable.
511 #define ZTEST_COMMIT_CB_MIN_REG 100
514 * If a number of txgs equal to this threshold have been created after a commit
515 * callback has been registered but not called, then we assume there is an
516 * implementation bug.
518 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
521 ZTEST_META_DNODE
= 0,
526 static void usage(boolean_t
) __NORETURN
;
527 static int ztest_scrub_impl(spa_t
*spa
);
530 * These libumem hooks provide a reasonable set of defaults for the allocator's
531 * debugging facilities.
534 _umem_debug_init(void)
536 return ("default,verbose"); /* $UMEM_DEBUG setting */
540 _umem_logging_init(void)
542 return ("fail,contents"); /* $UMEM_LOGGING setting */
546 dump_debug_buffer(void)
548 ssize_t ret
__attribute__((unused
));
550 if (!ztest_opts
.zo_dump_dbgmsg
)
554 * We use write() instead of printf() so that this function
555 * is safe to call from a signal handler.
557 ret
= write(STDOUT_FILENO
, "\n", 1);
558 zfs_dbgmsg_print("ztest");
561 #define BACKTRACE_SZ 100
563 static void sig_handler(int signo
)
565 struct sigaction action
;
566 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
568 void *buffer
[BACKTRACE_SZ
];
570 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
571 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
576 * Restore default action and re-raise signal so SIGSEGV and
577 * SIGABRT can trigger a core dump.
579 action
.sa_handler
= SIG_DFL
;
580 sigemptyset(&action
.sa_mask
);
582 (void) sigaction(signo
, &action
, NULL
);
586 #define FATAL_MSG_SZ 1024
591 fatal(int do_perror
, char *message
, ...)
594 int save_errno
= errno
;
597 (void) fflush(stdout
);
598 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
600 va_start(args
, message
);
601 (void) sprintf(buf
, "ztest: ");
603 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
606 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
607 ": %s", strerror(save_errno
));
609 (void) fprintf(stderr
, "%s\n", buf
);
610 fatal_msg
= buf
; /* to ease debugging */
621 str2shift(const char *buf
)
623 const char *ends
= "BKMGTPEZ";
628 for (i
= 0; i
< strlen(ends
); i
++) {
629 if (toupper(buf
[0]) == ends
[i
])
632 if (i
== strlen(ends
)) {
633 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
637 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
640 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
646 nicenumtoull(const char *buf
)
651 val
= strtoull(buf
, &end
, 0);
653 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
655 } else if (end
[0] == '.') {
656 double fval
= strtod(buf
, &end
);
657 fval
*= pow(2, str2shift(end
));
659 * UINT64_MAX is not exactly representable as a double.
660 * The closest representation is UINT64_MAX + 1, so we
661 * use a >= comparison instead of > for the bounds check.
663 if (fval
>= (double)UINT64_MAX
) {
664 (void) fprintf(stderr
, "ztest: value too large: %s\n",
668 val
= (uint64_t)fval
;
670 int shift
= str2shift(end
);
671 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
672 (void) fprintf(stderr
, "ztest: value too large: %s\n",
682 usage(boolean_t requested
)
684 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
686 char nice_vdev_size
[NN_NUMBUF_SZ
];
687 char nice_force_ganging
[NN_NUMBUF_SZ
];
688 FILE *fp
= requested
? stdout
: stderr
;
690 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
691 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
692 sizeof (nice_force_ganging
));
694 (void) fprintf(fp
, "Usage: %s\n"
695 "\t[-v vdevs (default: %llu)]\n"
696 "\t[-s size_of_each_vdev (default: %s)]\n"
697 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
698 "\t[-m mirror_copies (default: %d)]\n"
699 "\t[-r raidz_disks / draid_disks (default: %d)]\n"
700 "\t[-R raid_parity (default: %d)]\n"
701 "\t[-K raid_kind (default: random)] raidz|draid|random\n"
702 "\t[-D draid_data (default: %d)] in config\n"
703 "\t[-S draid_spares (default: %d)]\n"
704 "\t[-d datasets (default: %d)]\n"
705 "\t[-t threads (default: %d)]\n"
706 "\t[-g gang_block_threshold (default: %s)]\n"
707 "\t[-i init_count (default: %d)] initialize pool i times\n"
708 "\t[-k kill_percentage (default: %llu%%)]\n"
709 "\t[-p pool_name (default: %s)]\n"
710 "\t[-f dir (default: %s)] file directory for vdev files\n"
711 "\t[-M] Multi-host simulate pool imported on remote host\n"
712 "\t[-V] verbose (use multiple times for ever more blather)\n"
713 "\t[-E] use existing pool instead of creating new one\n"
714 "\t[-T time (default: %llu sec)] total run time\n"
715 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
716 "\t[-P passtime (default: %llu sec)] time per pass\n"
717 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
718 "\t[-C vdev class state (default: random)] special=on|off|random\n"
719 "\t[-o variable=value] ... set global variable to an unsigned\n"
720 "\t 32-bit integer value\n"
721 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
722 "\t[-h] (print help)\n"
725 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
726 nice_vdev_size
, /* -s */
727 zo
->zo_ashift
, /* -a */
728 zo
->zo_mirrors
, /* -m */
729 zo
->zo_raid_children
, /* -r */
730 zo
->zo_raid_parity
, /* -R */
731 zo
->zo_draid_data
, /* -D */
732 zo
->zo_draid_spares
, /* -S */
733 zo
->zo_datasets
, /* -d */
734 zo
->zo_threads
, /* -t */
735 nice_force_ganging
, /* -g */
736 zo
->zo_init
, /* -i */
737 (u_longlong_t
)zo
->zo_killrate
, /* -k */
738 zo
->zo_pool
, /* -p */
740 (u_longlong_t
)zo
->zo_time
, /* -T */
741 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
742 (u_longlong_t
)zo
->zo_passtime
);
743 exit(requested
? 0 : 1);
747 ztest_random(uint64_t range
)
751 ASSERT3S(ztest_fd_rand
, >=, 0);
756 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
757 fatal(1, "short read from /dev/urandom");
763 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
767 int state
= ZTEST_VDEV_CLASS_RND
;
769 (void) strlcpy(name
, input
, sizeof (name
));
771 value
= strchr(name
, '=');
773 (void) fprintf(stderr
, "missing value in property=value "
774 "'-C' argument (%s)\n", input
);
780 if (strcmp(value
, "on") == 0) {
781 state
= ZTEST_VDEV_CLASS_ON
;
782 } else if (strcmp(value
, "off") == 0) {
783 state
= ZTEST_VDEV_CLASS_OFF
;
784 } else if (strcmp(value
, "random") == 0) {
785 state
= ZTEST_VDEV_CLASS_RND
;
787 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
791 if (strcmp(name
, "special") == 0) {
792 zo
->zo_special_vdevs
= state
;
794 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
797 if (zo
->zo_verbose
>= 3)
798 (void) printf("%s vdev state is '%s'\n", name
, value
);
802 process_options(int argc
, char **argv
)
805 ztest_shared_opts_t
*zo
= &ztest_opts
;
809 char altdir
[MAXNAMELEN
] = { 0 };
810 char raid_kind
[8] = { "random" };
812 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
814 while ((opt
= getopt(argc
, argv
,
815 "v:s:a:m:r:R:K:D:S:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
834 value
= nicenumtoull(optarg
);
838 zo
->zo_vdevs
= value
;
841 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
844 zo
->zo_ashift
= value
;
847 zo
->zo_mirrors
= value
;
850 zo
->zo_raid_children
= MAX(1, value
);
853 zo
->zo_raid_parity
= MIN(MAX(value
, 1), 3);
856 (void) strlcpy(raid_kind
, optarg
, sizeof (raid_kind
));
859 zo
->zo_draid_data
= MAX(1, value
);
862 zo
->zo_draid_spares
= MAX(1, value
);
865 zo
->zo_datasets
= MAX(1, value
);
868 zo
->zo_threads
= MAX(1, value
);
871 zo
->zo_metaslab_force_ganging
=
872 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
878 zo
->zo_killrate
= value
;
881 (void) strlcpy(zo
->zo_pool
, optarg
,
882 sizeof (zo
->zo_pool
));
885 path
= realpath(optarg
, NULL
);
887 (void) fprintf(stderr
, "error: %s: %s\n",
888 optarg
, strerror(errno
));
891 (void) strlcpy(zo
->zo_dir
, path
,
892 sizeof (zo
->zo_dir
));
909 zo
->zo_passtime
= MAX(1, value
);
912 zo
->zo_maxloops
= MAX(1, value
);
915 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
918 ztest_parse_name_value(optarg
, zo
);
921 if (set_global_var(optarg
) != 0)
925 zo
->zo_dump_dbgmsg
= 1;
937 /* When raid choice is 'random' add a draid pool 50% of the time */
938 if (strcmp(raid_kind
, "random") == 0) {
939 (void) strlcpy(raid_kind
, (ztest_random(2) == 0) ?
940 "draid" : "raidz", sizeof (raid_kind
));
942 if (ztest_opts
.zo_verbose
>= 3)
943 (void) printf("choosing RAID type '%s'\n", raid_kind
);
946 if (strcmp(raid_kind
, "draid") == 0) {
947 uint64_t min_devsize
;
949 /* With fewer disk use 256M, otherwise 128M is OK */
950 min_devsize
= (ztest_opts
.zo_raid_children
< 16) ?
951 (256ULL << 20) : (128ULL << 20);
953 /* No top-level mirrors with dRAID for now */
956 /* Use more appropriate defaults for dRAID */
957 if (zo
->zo_vdevs
== ztest_opts_defaults
.zo_vdevs
)
959 if (zo
->zo_raid_children
==
960 ztest_opts_defaults
.zo_raid_children
)
961 zo
->zo_raid_children
= 16;
962 if (zo
->zo_ashift
< 12)
964 if (zo
->zo_vdev_size
< min_devsize
)
965 zo
->zo_vdev_size
= min_devsize
;
967 if (zo
->zo_draid_data
+ zo
->zo_raid_parity
>
968 zo
->zo_raid_children
- zo
->zo_draid_spares
) {
969 (void) fprintf(stderr
, "error: too few draid "
970 "children (%d) for stripe width (%d)\n",
971 zo
->zo_raid_children
,
972 zo
->zo_draid_data
+ zo
->zo_raid_parity
);
976 (void) strlcpy(zo
->zo_raid_type
, VDEV_TYPE_DRAID
,
977 sizeof (zo
->zo_raid_type
));
979 } else /* using raidz */ {
980 ASSERT0(strcmp(raid_kind
, "raidz"));
982 zo
->zo_raid_parity
= MIN(zo
->zo_raid_parity
,
983 zo
->zo_raid_children
- 1);
987 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
990 if (strlen(altdir
) > 0) {
998 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
999 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1001 VERIFY(NULL
!= realpath(getexecname(), cmd
));
1002 if (0 != access(altdir
, F_OK
)) {
1003 ztest_dump_core
= B_FALSE
;
1004 fatal(B_TRUE
, "invalid alternate ztest path: %s",
1007 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
1010 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
1011 * We want to extract <isa> to determine if we should use
1012 * 32 or 64 bit binaries.
1014 bin
= strstr(cmd
, "/usr/bin/");
1015 ztest
= strstr(bin
, "/ztest");
1017 isalen
= ztest
- isa
;
1018 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
1019 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
1020 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
1021 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
1023 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
1024 ztest_dump_core
= B_FALSE
;
1025 fatal(B_TRUE
, "invalid alternate ztest: %s",
1027 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
1028 ztest_dump_core
= B_FALSE
;
1029 fatal(B_TRUE
, "invalid alternate lib directory %s",
1030 zo
->zo_alt_libpath
);
1033 umem_free(cmd
, MAXPATHLEN
);
1034 umem_free(realaltdir
, MAXPATHLEN
);
1039 ztest_kill(ztest_shared_t
*zs
)
1041 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
1042 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
1045 * Before we kill off ztest, make sure that the config is updated.
1046 * See comment above spa_write_cachefile().
1048 mutex_enter(&spa_namespace_lock
);
1049 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
1050 mutex_exit(&spa_namespace_lock
);
1052 (void) kill(getpid(), SIGKILL
);
1057 ztest_record_enospc(const char *s
)
1059 ztest_shared
->zs_enospc_count
++;
1063 ztest_get_ashift(void)
1065 if (ztest_opts
.zo_ashift
== 0)
1066 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
1067 return (ztest_opts
.zo_ashift
);
1071 ztest_is_draid_spare(const char *name
)
1073 uint64_t spare_id
= 0, parity
= 0, vdev_id
= 0;
1075 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1076 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1077 (u_longlong_t
*)&spare_id
) == 3) {
1085 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
1090 boolean_t draid_spare
= B_FALSE
;
1092 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1095 ashift
= ztest_get_ashift();
1101 vdev
= ztest_shared
->zs_vdev_aux
;
1102 (void) snprintf(path
, MAXPATHLEN
,
1103 ztest_aux_template
, ztest_opts
.zo_dir
,
1104 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
1107 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
1108 (void) snprintf(path
, MAXPATHLEN
,
1109 ztest_dev_template
, ztest_opts
.zo_dir
,
1110 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1113 draid_spare
= ztest_is_draid_spare(path
);
1116 if (size
!= 0 && !draid_spare
) {
1117 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1119 fatal(1, "can't open %s", path
);
1120 if (ftruncate(fd
, size
) != 0)
1121 fatal(1, "can't ftruncate %s", path
);
1125 VERIFY0(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0));
1126 VERIFY0(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
,
1127 draid_spare
? VDEV_TYPE_DRAID_SPARE
: VDEV_TYPE_FILE
));
1128 VERIFY0(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
));
1129 VERIFY0(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
));
1130 umem_free(pathbuf
, MAXPATHLEN
);
1136 make_vdev_raid(char *path
, char *aux
, char *pool
, size_t size
,
1137 uint64_t ashift
, int r
)
1139 nvlist_t
*raid
, **child
;
1143 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1144 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1146 for (c
= 0; c
< r
; c
++)
1147 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1149 VERIFY0(nvlist_alloc(&raid
, NV_UNIQUE_NAME
, 0));
1150 VERIFY0(nvlist_add_string(raid
, ZPOOL_CONFIG_TYPE
,
1151 ztest_opts
.zo_raid_type
));
1152 VERIFY0(nvlist_add_uint64(raid
, ZPOOL_CONFIG_NPARITY
,
1153 ztest_opts
.zo_raid_parity
));
1154 VERIFY0(nvlist_add_nvlist_array(raid
, ZPOOL_CONFIG_CHILDREN
,
1157 if (strcmp(ztest_opts
.zo_raid_type
, VDEV_TYPE_DRAID
) == 0) {
1158 uint64_t ndata
= ztest_opts
.zo_draid_data
;
1159 uint64_t nparity
= ztest_opts
.zo_raid_parity
;
1160 uint64_t nspares
= ztest_opts
.zo_draid_spares
;
1161 uint64_t children
= ztest_opts
.zo_raid_children
;
1162 uint64_t ngroups
= 1;
1165 * Calculate the minimum number of groups required to fill a
1166 * slice. This is the LCM of the stripe width (data + parity)
1167 * and the number of data drives (children - spares).
1169 while (ngroups
* (ndata
+ nparity
) % (children
- nspares
) != 0)
1172 /* Store the basic dRAID configuration. */
1173 fnvlist_add_uint64(raid
, ZPOOL_CONFIG_DRAID_NDATA
, ndata
);
1174 fnvlist_add_uint64(raid
, ZPOOL_CONFIG_DRAID_NSPARES
, nspares
);
1175 fnvlist_add_uint64(raid
, ZPOOL_CONFIG_DRAID_NGROUPS
, ngroups
);
1178 for (c
= 0; c
< r
; c
++)
1179 nvlist_free(child
[c
]);
1181 umem_free(child
, r
* sizeof (nvlist_t
*));
1187 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1188 uint64_t ashift
, int r
, int m
)
1190 nvlist_t
*mirror
, **child
;
1194 return (make_vdev_raid(path
, aux
, pool
, size
, ashift
, r
));
1196 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1198 for (c
= 0; c
< m
; c
++)
1199 child
[c
] = make_vdev_raid(path
, aux
, pool
, size
, ashift
, r
);
1201 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1202 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1203 VDEV_TYPE_MIRROR
) == 0);
1204 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1207 for (c
= 0; c
< m
; c
++)
1208 nvlist_free(child
[c
]);
1210 umem_free(child
, m
* sizeof (nvlist_t
*));
1216 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1217 const char *class, int r
, int m
, int t
)
1219 nvlist_t
*root
, **child
;
1225 log
= (class != NULL
&& strcmp(class, "log") == 0);
1227 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1229 for (c
= 0; c
< t
; c
++) {
1230 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1232 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1235 if (class != NULL
&& class[0] != '\0') {
1236 ASSERT(m
> 1 || log
); /* expecting a mirror */
1237 VERIFY(nvlist_add_string(child
[c
],
1238 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1242 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1243 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1244 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1247 for (c
= 0; c
< t
; c
++)
1248 nvlist_free(child
[c
]);
1250 umem_free(child
, t
* sizeof (nvlist_t
*));
1256 * Find a random spa version. Returns back a random spa version in the
1257 * range [initial_version, SPA_VERSION_FEATURES].
1260 ztest_random_spa_version(uint64_t initial_version
)
1262 uint64_t version
= initial_version
;
1264 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1266 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1269 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1270 version
= SPA_VERSION_FEATURES
;
1272 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1277 ztest_random_blocksize(void)
1279 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1282 * Choose a block size >= the ashift.
1283 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1285 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1286 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1288 uint64_t block_shift
=
1289 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1290 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1294 ztest_random_dnodesize(void)
1297 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1299 if (max_slots
== DNODE_MIN_SLOTS
)
1300 return (DNODE_MIN_SIZE
);
1303 * Weight the random distribution more heavily toward smaller
1304 * dnode sizes since that is more likely to reflect real-world
1307 ASSERT3U(max_slots
, >, 4);
1308 switch (ztest_random(10)) {
1310 slots
= 5 + ztest_random(max_slots
- 4);
1313 slots
= 2 + ztest_random(3);
1320 return (slots
<< DNODE_SHIFT
);
1324 ztest_random_ibshift(void)
1326 return (DN_MIN_INDBLKSHIFT
+
1327 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1331 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1334 vdev_t
*rvd
= spa
->spa_root_vdev
;
1337 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1340 top
= ztest_random(rvd
->vdev_children
);
1341 tvd
= rvd
->vdev_child
[top
];
1342 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1343 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1349 ztest_random_dsl_prop(zfs_prop_t prop
)
1354 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1355 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1361 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1364 const char *propname
= zfs_prop_to_name(prop
);
1365 const char *valname
;
1370 error
= dsl_prop_set_int(osname
, propname
,
1371 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1373 if (error
== ENOSPC
) {
1374 ztest_record_enospc(FTAG
);
1379 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1380 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1382 if (ztest_opts
.zo_verbose
>= 6) {
1385 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1387 (void) printf("%s %s = %llu at '%s'\n", osname
,
1388 propname
, (unsigned long long)curval
, setpoint
);
1390 (void) printf("%s %s = %s at '%s'\n",
1391 osname
, propname
, valname
, setpoint
);
1393 umem_free(setpoint
, MAXPATHLEN
);
1399 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1401 spa_t
*spa
= ztest_spa
;
1402 nvlist_t
*props
= NULL
;
1405 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1406 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1408 error
= spa_prop_set(spa
, props
);
1412 if (error
== ENOSPC
) {
1413 ztest_record_enospc(FTAG
);
1422 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1423 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1427 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1429 strcpy(ddname
, name
);
1430 cp
= strchr(ddname
, '@');
1434 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1435 while (decrypt
&& err
== EACCES
) {
1436 dsl_crypto_params_t
*dcp
;
1437 nvlist_t
*crypto_args
= fnvlist_alloc();
1439 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1440 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1441 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1442 crypto_args
, &dcp
));
1443 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1444 dsl_crypto_params_free(dcp
, B_FALSE
);
1445 fnvlist_free(crypto_args
);
1447 if (err
== EINVAL
) {
1449 * We couldn't load a key for this dataset so try
1450 * the parent. This loop will eventually hit the
1451 * encryption root since ztest only makes clones
1452 * as children of their origin datasets.
1454 cp
= strrchr(ddname
, '/');
1461 } else if (err
!= 0) {
1465 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1473 ztest_rll_init(rll_t
*rll
)
1475 rll
->rll_writer
= NULL
;
1476 rll
->rll_readers
= 0;
1477 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1478 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1482 ztest_rll_destroy(rll_t
*rll
)
1484 ASSERT(rll
->rll_writer
== NULL
);
1485 ASSERT(rll
->rll_readers
== 0);
1486 mutex_destroy(&rll
->rll_lock
);
1487 cv_destroy(&rll
->rll_cv
);
1491 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1493 mutex_enter(&rll
->rll_lock
);
1495 if (type
== RL_READER
) {
1496 while (rll
->rll_writer
!= NULL
)
1497 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1500 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1501 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1502 rll
->rll_writer
= curthread
;
1505 mutex_exit(&rll
->rll_lock
);
1509 ztest_rll_unlock(rll_t
*rll
)
1511 mutex_enter(&rll
->rll_lock
);
1513 if (rll
->rll_writer
) {
1514 ASSERT(rll
->rll_readers
== 0);
1515 rll
->rll_writer
= NULL
;
1517 ASSERT(rll
->rll_readers
!= 0);
1518 ASSERT(rll
->rll_writer
== NULL
);
1522 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1523 cv_broadcast(&rll
->rll_cv
);
1525 mutex_exit(&rll
->rll_lock
);
1529 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1531 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1533 ztest_rll_lock(rll
, type
);
1537 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1539 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1541 ztest_rll_unlock(rll
);
1545 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1546 uint64_t size
, rl_type_t type
)
1548 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1549 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1552 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1553 rl
->rl_object
= object
;
1554 rl
->rl_offset
= offset
;
1558 ztest_rll_lock(rll
, type
);
1564 ztest_range_unlock(rl_t
*rl
)
1566 rll_t
*rll
= rl
->rl_lock
;
1568 ztest_rll_unlock(rll
);
1570 umem_free(rl
, sizeof (*rl
));
1574 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1577 zd
->zd_zilog
= dmu_objset_zil(os
);
1578 zd
->zd_shared
= szd
;
1579 dmu_objset_name(os
, zd
->zd_name
);
1582 if (zd
->zd_shared
!= NULL
)
1583 zd
->zd_shared
->zd_seq
= 0;
1585 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1586 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1588 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1589 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1591 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1592 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1596 ztest_zd_fini(ztest_ds_t
*zd
)
1600 mutex_destroy(&zd
->zd_dirobj_lock
);
1601 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1603 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1604 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1606 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1607 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1610 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1613 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1619 * Attempt to assign tx to some transaction group.
1621 error
= dmu_tx_assign(tx
, txg_how
);
1623 if (error
== ERESTART
) {
1624 ASSERT(txg_how
== TXG_NOWAIT
);
1627 ASSERT3U(error
, ==, ENOSPC
);
1628 ztest_record_enospc(tag
);
1633 txg
= dmu_tx_get_txg(tx
);
1639 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1640 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1643 bt
->bt_magic
= BT_MAGIC
;
1644 bt
->bt_objset
= dmu_objset_id(os
);
1645 bt
->bt_object
= object
;
1646 bt
->bt_dnodesize
= dnodesize
;
1647 bt
->bt_offset
= offset
;
1650 bt
->bt_crtxg
= crtxg
;
1654 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1655 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1658 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1659 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1660 ASSERT3U(bt
->bt_object
, ==, object
);
1661 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1662 ASSERT3U(bt
->bt_offset
, ==, offset
);
1663 ASSERT3U(bt
->bt_gen
, <=, gen
);
1664 ASSERT3U(bt
->bt_txg
, <=, txg
);
1665 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1668 static ztest_block_tag_t
*
1669 ztest_bt_bonus(dmu_buf_t
*db
)
1671 dmu_object_info_t doi
;
1672 ztest_block_tag_t
*bt
;
1674 dmu_object_info_from_db(db
, &doi
);
1675 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1676 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1677 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1683 * Generate a token to fill up unused bonus buffer space. Try to make
1684 * it unique to the object, generation, and offset to verify that data
1685 * is not getting overwritten by data from other dnodes.
1687 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1688 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1691 * Fill up the unused bonus buffer region before the block tag with a
1692 * verifiable pattern. Filling the whole bonus area with non-zero data
1693 * helps ensure that all dnode traversal code properly skips the
1694 * interior regions of large dnodes.
1697 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1698 objset_t
*os
, uint64_t gen
)
1702 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1704 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1705 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1706 gen
, bonusp
- (uint64_t *)db
->db_data
);
1712 * Verify that the unused area of a bonus buffer is filled with the
1716 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1717 objset_t
*os
, uint64_t gen
)
1721 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1722 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1723 gen
, bonusp
- (uint64_t *)db
->db_data
);
1724 VERIFY3U(*bonusp
, ==, token
);
1732 #define lrz_type lr_mode
1733 #define lrz_blocksize lr_uid
1734 #define lrz_ibshift lr_gid
1735 #define lrz_bonustype lr_rdev
1736 #define lrz_dnodesize lr_crtime[1]
1739 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1741 char *name
= (void *)(lr
+ 1); /* name follows lr */
1742 size_t namesize
= strlen(name
) + 1;
1745 if (zil_replaying(zd
->zd_zilog
, tx
))
1748 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1749 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1750 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1752 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1756 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1758 char *name
= (void *)(lr
+ 1); /* name follows lr */
1759 size_t namesize
= strlen(name
) + 1;
1762 if (zil_replaying(zd
->zd_zilog
, tx
))
1765 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1766 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1767 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1769 itx
->itx_oid
= object
;
1770 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1774 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1777 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1779 if (zil_replaying(zd
->zd_zilog
, tx
))
1782 if (lr
->lr_length
> zil_max_log_data(zd
->zd_zilog
))
1783 write_state
= WR_INDIRECT
;
1785 itx
= zil_itx_create(TX_WRITE
,
1786 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1788 if (write_state
== WR_COPIED
&&
1789 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1790 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1791 zil_itx_destroy(itx
);
1792 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1793 write_state
= WR_NEED_COPY
;
1795 itx
->itx_private
= zd
;
1796 itx
->itx_wr_state
= write_state
;
1797 itx
->itx_sync
= (ztest_random(8) == 0);
1799 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1800 sizeof (*lr
) - sizeof (lr_t
));
1802 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1806 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1810 if (zil_replaying(zd
->zd_zilog
, tx
))
1813 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1814 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1815 sizeof (*lr
) - sizeof (lr_t
));
1817 itx
->itx_sync
= B_FALSE
;
1818 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1822 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1826 if (zil_replaying(zd
->zd_zilog
, tx
))
1829 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1830 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1831 sizeof (*lr
) - sizeof (lr_t
));
1833 itx
->itx_sync
= B_FALSE
;
1834 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1841 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1843 ztest_ds_t
*zd
= arg1
;
1844 lr_create_t
*lr
= arg2
;
1845 char *name
= (void *)(lr
+ 1); /* name follows lr */
1846 objset_t
*os
= zd
->zd_os
;
1847 ztest_block_tag_t
*bbt
;
1855 byteswap_uint64_array(lr
, sizeof (*lr
));
1857 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1858 ASSERT(name
[0] != '\0');
1860 tx
= dmu_tx_create(os
);
1862 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1864 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1865 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1867 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1870 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1874 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1875 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1877 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1878 if (lr
->lr_foid
== 0) {
1879 lr
->lr_foid
= zap_create_dnsize(os
,
1880 lr
->lrz_type
, lr
->lrz_bonustype
,
1881 bonuslen
, lr
->lrz_dnodesize
, tx
);
1883 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1884 lr
->lrz_type
, lr
->lrz_bonustype
,
1885 bonuslen
, lr
->lrz_dnodesize
, tx
);
1888 if (lr
->lr_foid
== 0) {
1889 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1890 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1891 bonuslen
, lr
->lrz_dnodesize
, tx
);
1893 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1894 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1895 bonuslen
, lr
->lrz_dnodesize
, tx
);
1900 ASSERT3U(error
, ==, EEXIST
);
1901 ASSERT(zd
->zd_zilog
->zl_replay
);
1906 ASSERT(lr
->lr_foid
!= 0);
1908 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1909 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1910 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1912 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1913 bbt
= ztest_bt_bonus(db
);
1914 dmu_buf_will_dirty(db
, tx
);
1915 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1916 lr
->lr_gen
, txg
, txg
);
1917 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1918 dmu_buf_rele(db
, FTAG
);
1920 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1923 (void) ztest_log_create(zd
, tx
, lr
);
1931 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1933 ztest_ds_t
*zd
= arg1
;
1934 lr_remove_t
*lr
= arg2
;
1935 char *name
= (void *)(lr
+ 1); /* name follows lr */
1936 objset_t
*os
= zd
->zd_os
;
1937 dmu_object_info_t doi
;
1939 uint64_t object
, txg
;
1942 byteswap_uint64_array(lr
, sizeof (*lr
));
1944 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1945 ASSERT(name
[0] != '\0');
1948 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1949 ASSERT(object
!= 0);
1951 ztest_object_lock(zd
, object
, RL_WRITER
);
1953 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1955 tx
= dmu_tx_create(os
);
1957 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1958 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1960 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1962 ztest_object_unlock(zd
, object
);
1966 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1967 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1969 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1972 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1974 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1978 ztest_object_unlock(zd
, object
);
1984 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1986 ztest_ds_t
*zd
= arg1
;
1987 lr_write_t
*lr
= arg2
;
1988 objset_t
*os
= zd
->zd_os
;
1989 void *data
= lr
+ 1; /* data follows lr */
1990 uint64_t offset
, length
;
1991 ztest_block_tag_t
*bt
= data
;
1992 ztest_block_tag_t
*bbt
;
1993 uint64_t gen
, txg
, lrtxg
, crtxg
;
1994 dmu_object_info_t doi
;
1997 arc_buf_t
*abuf
= NULL
;
2001 byteswap_uint64_array(lr
, sizeof (*lr
));
2003 offset
= lr
->lr_offset
;
2004 length
= lr
->lr_length
;
2006 /* If it's a dmu_sync() block, write the whole block */
2007 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
2008 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
2009 if (length
< blocksize
) {
2010 offset
-= offset
% blocksize
;
2015 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
2016 byteswap_uint64_array(bt
, sizeof (*bt
));
2018 if (bt
->bt_magic
!= BT_MAGIC
)
2021 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2022 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
2024 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2026 dmu_object_info_from_db(db
, &doi
);
2028 bbt
= ztest_bt_bonus(db
);
2029 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2031 crtxg
= bbt
->bt_crtxg
;
2032 lrtxg
= lr
->lr_common
.lrc_txg
;
2034 tx
= dmu_tx_create(os
);
2036 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
2038 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
2039 P2PHASE(offset
, length
) == 0)
2040 abuf
= dmu_request_arcbuf(db
, length
);
2042 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2045 dmu_return_arcbuf(abuf
);
2046 dmu_buf_rele(db
, FTAG
);
2047 ztest_range_unlock(rl
);
2048 ztest_object_unlock(zd
, lr
->lr_foid
);
2054 * Usually, verify the old data before writing new data --
2055 * but not always, because we also want to verify correct
2056 * behavior when the data was not recently read into cache.
2058 ASSERT(offset
% doi
.doi_data_block_size
== 0);
2059 if (ztest_random(4) != 0) {
2060 int prefetch
= ztest_random(2) ?
2061 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
2062 ztest_block_tag_t rbt
;
2064 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
2065 sizeof (rbt
), &rbt
, prefetch
) == 0);
2066 if (rbt
.bt_magic
== BT_MAGIC
) {
2067 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
2068 offset
, gen
, txg
, crtxg
);
2073 * Writes can appear to be newer than the bonus buffer because
2074 * the ztest_get_data() callback does a dmu_read() of the
2075 * open-context data, which may be different than the data
2076 * as it was when the write was generated.
2078 if (zd
->zd_zilog
->zl_replay
) {
2079 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
2080 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
2085 * Set the bt's gen/txg to the bonus buffer's gen/txg
2086 * so that all of the usual ASSERTs will work.
2088 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
2093 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
2095 bcopy(data
, abuf
->b_data
, length
);
2096 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
2099 (void) ztest_log_write(zd
, tx
, lr
);
2101 dmu_buf_rele(db
, FTAG
);
2105 ztest_range_unlock(rl
);
2106 ztest_object_unlock(zd
, lr
->lr_foid
);
2112 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2114 ztest_ds_t
*zd
= arg1
;
2115 lr_truncate_t
*lr
= arg2
;
2116 objset_t
*os
= zd
->zd_os
;
2122 byteswap_uint64_array(lr
, sizeof (*lr
));
2124 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2125 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2128 tx
= dmu_tx_create(os
);
2130 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2132 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2134 ztest_range_unlock(rl
);
2135 ztest_object_unlock(zd
, lr
->lr_foid
);
2139 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2140 lr
->lr_length
, tx
) == 0);
2142 (void) ztest_log_truncate(zd
, tx
, lr
);
2146 ztest_range_unlock(rl
);
2147 ztest_object_unlock(zd
, lr
->lr_foid
);
2153 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2155 ztest_ds_t
*zd
= arg1
;
2156 lr_setattr_t
*lr
= arg2
;
2157 objset_t
*os
= zd
->zd_os
;
2160 ztest_block_tag_t
*bbt
;
2161 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2164 byteswap_uint64_array(lr
, sizeof (*lr
));
2166 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2168 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2170 tx
= dmu_tx_create(os
);
2171 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2173 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2175 dmu_buf_rele(db
, FTAG
);
2176 ztest_object_unlock(zd
, lr
->lr_foid
);
2180 bbt
= ztest_bt_bonus(db
);
2181 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2182 crtxg
= bbt
->bt_crtxg
;
2183 lrtxg
= lr
->lr_common
.lrc_txg
;
2184 dnodesize
= bbt
->bt_dnodesize
;
2186 if (zd
->zd_zilog
->zl_replay
) {
2187 ASSERT(lr
->lr_size
!= 0);
2188 ASSERT(lr
->lr_mode
!= 0);
2192 * Randomly change the size and increment the generation.
2194 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2196 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2201 * Verify that the current bonus buffer is not newer than our txg.
2203 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2204 MAX(txg
, lrtxg
), crtxg
);
2206 dmu_buf_will_dirty(db
, tx
);
2208 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2209 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2210 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2211 bbt
= ztest_bt_bonus(db
);
2213 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2215 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2216 dmu_buf_rele(db
, FTAG
);
2218 (void) ztest_log_setattr(zd
, tx
, lr
);
2222 ztest_object_unlock(zd
, lr
->lr_foid
);
2227 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2228 NULL
, /* 0 no such transaction type */
2229 ztest_replay_create
, /* TX_CREATE */
2230 NULL
, /* TX_MKDIR */
2231 NULL
, /* TX_MKXATTR */
2232 NULL
, /* TX_SYMLINK */
2233 ztest_replay_remove
, /* TX_REMOVE */
2234 NULL
, /* TX_RMDIR */
2236 NULL
, /* TX_RENAME */
2237 ztest_replay_write
, /* TX_WRITE */
2238 ztest_replay_truncate
, /* TX_TRUNCATE */
2239 ztest_replay_setattr
, /* TX_SETATTR */
2241 NULL
, /* TX_CREATE_ACL */
2242 NULL
, /* TX_CREATE_ATTR */
2243 NULL
, /* TX_CREATE_ACL_ATTR */
2244 NULL
, /* TX_MKDIR_ACL */
2245 NULL
, /* TX_MKDIR_ATTR */
2246 NULL
, /* TX_MKDIR_ACL_ATTR */
2247 NULL
, /* TX_WRITE2 */
2251 * ZIL get_data callbacks
2256 ztest_get_done(zgd_t
*zgd
, int error
)
2258 ztest_ds_t
*zd
= zgd
->zgd_private
;
2259 uint64_t object
= ((rl_t
*)zgd
->zgd_lr
)->rl_object
;
2262 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2264 ztest_range_unlock((rl_t
*)zgd
->zgd_lr
);
2265 ztest_object_unlock(zd
, object
);
2267 umem_free(zgd
, sizeof (*zgd
));
2271 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2274 ztest_ds_t
*zd
= arg
;
2275 objset_t
*os
= zd
->zd_os
;
2276 uint64_t object
= lr
->lr_foid
;
2277 uint64_t offset
= lr
->lr_offset
;
2278 uint64_t size
= lr
->lr_length
;
2279 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2281 dmu_object_info_t doi
;
2286 ASSERT3P(lwb
, !=, NULL
);
2287 ASSERT3P(zio
, !=, NULL
);
2288 ASSERT3U(size
, !=, 0);
2290 ztest_object_lock(zd
, object
, RL_READER
);
2291 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2293 ztest_object_unlock(zd
, object
);
2297 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2299 if (crtxg
== 0 || crtxg
> txg
) {
2300 dmu_buf_rele(db
, FTAG
);
2301 ztest_object_unlock(zd
, object
);
2305 dmu_object_info_from_db(db
, &doi
);
2306 dmu_buf_rele(db
, FTAG
);
2309 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2311 zgd
->zgd_private
= zd
;
2313 if (buf
!= NULL
) { /* immediate write */
2314 zgd
->zgd_lr
= (struct zfs_locked_range
*)ztest_range_lock(zd
,
2315 object
, offset
, size
, RL_READER
);
2317 error
= dmu_read(os
, object
, offset
, size
, buf
,
2318 DMU_READ_NO_PREFETCH
);
2321 size
= doi
.doi_data_block_size
;
2323 offset
= P2ALIGN(offset
, size
);
2325 ASSERT(offset
< size
);
2329 zgd
->zgd_lr
= (struct zfs_locked_range
*)ztest_range_lock(zd
,
2330 object
, offset
, size
, RL_READER
);
2332 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2333 DMU_READ_NO_PREFETCH
);
2336 blkptr_t
*bp
= &lr
->lr_blkptr
;
2341 ASSERT(db
->db_offset
== offset
);
2342 ASSERT(db
->db_size
== size
);
2344 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2345 ztest_get_done
, zgd
);
2352 ztest_get_done(zgd
, error
);
2358 ztest_lr_alloc(size_t lrsize
, char *name
)
2361 size_t namesize
= name
? strlen(name
) + 1 : 0;
2363 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2366 bcopy(name
, lr
+ lrsize
, namesize
);
2372 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2374 size_t namesize
= name
? strlen(name
) + 1 : 0;
2376 umem_free(lr
, lrsize
+ namesize
);
2380 * Lookup a bunch of objects. Returns the number of objects not found.
2383 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2389 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2391 for (i
= 0; i
< count
; i
++, od
++) {
2393 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2394 sizeof (uint64_t), 1, &od
->od_object
);
2396 ASSERT(error
== ENOENT
);
2397 ASSERT(od
->od_object
== 0);
2401 ztest_block_tag_t
*bbt
;
2402 dmu_object_info_t doi
;
2404 ASSERT(od
->od_object
!= 0);
2405 ASSERT(missing
== 0); /* there should be no gaps */
2407 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2408 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2409 od
->od_object
, FTAG
, &db
));
2410 dmu_object_info_from_db(db
, &doi
);
2411 bbt
= ztest_bt_bonus(db
);
2412 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2413 od
->od_type
= doi
.doi_type
;
2414 od
->od_blocksize
= doi
.doi_data_block_size
;
2415 od
->od_gen
= bbt
->bt_gen
;
2416 dmu_buf_rele(db
, FTAG
);
2417 ztest_object_unlock(zd
, od
->od_object
);
2425 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2430 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2432 for (i
= 0; i
< count
; i
++, od
++) {
2439 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2441 lr
->lr_doid
= od
->od_dir
;
2442 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2443 lr
->lrz_type
= od
->od_crtype
;
2444 lr
->lrz_blocksize
= od
->od_crblocksize
;
2445 lr
->lrz_ibshift
= ztest_random_ibshift();
2446 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2447 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2448 lr
->lr_gen
= od
->od_crgen
;
2449 lr
->lr_crtime
[0] = time(NULL
);
2451 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2452 ASSERT(missing
== 0);
2456 od
->od_object
= lr
->lr_foid
;
2457 od
->od_type
= od
->od_crtype
;
2458 od
->od_blocksize
= od
->od_crblocksize
;
2459 od
->od_gen
= od
->od_crgen
;
2460 ASSERT(od
->od_object
!= 0);
2463 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2470 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2476 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2480 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2487 * No object was found.
2489 if (od
->od_object
== 0)
2492 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2494 lr
->lr_doid
= od
->od_dir
;
2496 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2497 ASSERT3U(error
, ==, ENOSPC
);
2502 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2509 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2515 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2517 lr
->lr_foid
= object
;
2518 lr
->lr_offset
= offset
;
2519 lr
->lr_length
= size
;
2521 BP_ZERO(&lr
->lr_blkptr
);
2523 bcopy(data
, lr
+ 1, size
);
2525 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2527 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2533 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2538 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2540 lr
->lr_foid
= object
;
2541 lr
->lr_offset
= offset
;
2542 lr
->lr_length
= size
;
2544 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2546 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2552 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2557 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2559 lr
->lr_foid
= object
;
2563 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2565 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2571 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2573 objset_t
*os
= zd
->zd_os
;
2578 txg_wait_synced(dmu_objset_pool(os
), 0);
2580 ztest_object_lock(zd
, object
, RL_READER
);
2581 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2583 tx
= dmu_tx_create(os
);
2585 dmu_tx_hold_write(tx
, object
, offset
, size
);
2587 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2590 dmu_prealloc(os
, object
, offset
, size
, tx
);
2592 txg_wait_synced(dmu_objset_pool(os
), txg
);
2594 (void) dmu_free_long_range(os
, object
, offset
, size
);
2597 ztest_range_unlock(rl
);
2598 ztest_object_unlock(zd
, object
);
2602 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2605 ztest_block_tag_t wbt
;
2606 dmu_object_info_t doi
;
2607 enum ztest_io_type io_type
;
2611 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2612 blocksize
= doi
.doi_data_block_size
;
2613 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2616 * Pick an i/o type at random, biased toward writing block tags.
2618 io_type
= ztest_random(ZTEST_IO_TYPES
);
2619 if (ztest_random(2) == 0)
2620 io_type
= ZTEST_IO_WRITE_TAG
;
2622 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2626 case ZTEST_IO_WRITE_TAG
:
2627 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2629 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2632 case ZTEST_IO_WRITE_PATTERN
:
2633 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2634 if (ztest_random(2) == 0) {
2636 * Induce fletcher2 collisions to ensure that
2637 * zio_ddt_collision() detects and resolves them
2638 * when using fletcher2-verify for deduplication.
2640 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2641 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2643 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2646 case ZTEST_IO_WRITE_ZEROES
:
2647 bzero(data
, blocksize
);
2648 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2651 case ZTEST_IO_TRUNCATE
:
2652 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2655 case ZTEST_IO_SETATTR
:
2656 (void) ztest_setattr(zd
, object
);
2661 case ZTEST_IO_REWRITE
:
2662 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2663 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2664 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2666 VERIFY(err
== 0 || err
== ENOSPC
);
2667 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2668 ZFS_PROP_COMPRESSION
,
2669 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2671 VERIFY(err
== 0 || err
== ENOSPC
);
2672 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2674 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2675 DMU_READ_NO_PREFETCH
));
2677 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2681 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2683 umem_free(data
, blocksize
);
2687 * Initialize an object description template.
2690 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2691 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2694 od
->od_dir
= ZTEST_DIROBJ
;
2697 od
->od_crtype
= type
;
2698 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2699 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2702 od
->od_type
= DMU_OT_NONE
;
2703 od
->od_blocksize
= 0;
2706 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2707 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2711 * Lookup or create the objects for a test using the od template.
2712 * If the objects do not all exist, or if 'remove' is specified,
2713 * remove any existing objects and create new ones. Otherwise,
2714 * use the existing objects.
2717 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2719 int count
= size
/ sizeof (*od
);
2722 mutex_enter(&zd
->zd_dirobj_lock
);
2723 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2724 (ztest_remove(zd
, od
, count
) != 0 ||
2725 ztest_create(zd
, od
, count
) != 0))
2728 mutex_exit(&zd
->zd_dirobj_lock
);
2735 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2737 zilog_t
*zilog
= zd
->zd_zilog
;
2739 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2741 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2744 * Remember the committed values in zd, which is in parent/child
2745 * shared memory. If we die, the next iteration of ztest_run()
2746 * will verify that the log really does contain this record.
2748 mutex_enter(&zilog
->zl_lock
);
2749 ASSERT(zd
->zd_shared
!= NULL
);
2750 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2751 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2752 mutex_exit(&zilog
->zl_lock
);
2754 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2758 * This function is designed to simulate the operations that occur during a
2759 * mount/unmount operation. We hold the dataset across these operations in an
2760 * attempt to expose any implicit assumptions about ZIL management.
2764 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2766 objset_t
*os
= zd
->zd_os
;
2769 * We hold the ztest_vdev_lock so we don't cause problems with
2770 * other threads that wish to remove a log device, such as
2771 * ztest_device_removal().
2773 mutex_enter(&ztest_vdev_lock
);
2776 * We grab the zd_dirobj_lock to ensure that no other thread is
2777 * updating the zil (i.e. adding in-memory log records) and the
2778 * zd_zilog_lock to block any I/O.
2780 mutex_enter(&zd
->zd_dirobj_lock
);
2781 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2783 /* zfsvfs_teardown() */
2784 zil_close(zd
->zd_zilog
);
2786 /* zfsvfs_setup() */
2787 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2788 zil_replay(os
, zd
, ztest_replay_vector
);
2790 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2791 mutex_exit(&zd
->zd_dirobj_lock
);
2792 mutex_exit(&ztest_vdev_lock
);
2796 * Verify that we can't destroy an active pool, create an existing pool,
2797 * or create a pool with a bad vdev spec.
2801 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2803 ztest_shared_opts_t
*zo
= &ztest_opts
;
2807 if (zo
->zo_mmp_test
)
2811 * Attempt to create using a bad file.
2813 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2814 VERIFY3U(ENOENT
, ==,
2815 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2816 nvlist_free(nvroot
);
2819 * Attempt to create using a bad mirror.
2821 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2822 VERIFY3U(ENOENT
, ==,
2823 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2824 nvlist_free(nvroot
);
2827 * Attempt to create an existing pool. It shouldn't matter
2828 * what's in the nvroot; we should fail with EEXIST.
2830 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2831 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2832 VERIFY3U(EEXIST
, ==,
2833 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2834 nvlist_free(nvroot
);
2837 * We open a reference to the spa and then we try to export it
2838 * expecting one of the following errors:
2841 * Because of the reference we just opened.
2843 * ZFS_ERR_EXPORT_IN_PROGRESS
2844 * For the case that there is another ztest thread doing
2845 * an export concurrently.
2847 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2848 int error
= spa_destroy(zo
->zo_pool
);
2849 if (error
!= EBUSY
&& error
!= ZFS_ERR_EXPORT_IN_PROGRESS
) {
2850 fatal(0, "spa_destroy(%s) returned unexpected value %d",
2851 spa
->spa_name
, error
);
2853 spa_close(spa
, FTAG
);
2855 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2859 * Start and then stop the MMP threads to ensure the startup and shutdown code
2860 * works properly. Actual protection and property-related code tested via ZTS.
2864 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2866 ztest_shared_opts_t
*zo
= &ztest_opts
;
2867 spa_t
*spa
= ztest_spa
;
2869 if (zo
->zo_mmp_test
)
2873 * Since enabling MMP involves setting a property, it could not be done
2874 * while the pool is suspended.
2876 if (spa_suspended(spa
))
2879 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2880 mutex_enter(&spa
->spa_props_lock
);
2882 zfs_multihost_fail_intervals
= 0;
2884 if (!spa_multihost(spa
)) {
2885 spa
->spa_multihost
= B_TRUE
;
2886 mmp_thread_start(spa
);
2889 mutex_exit(&spa
->spa_props_lock
);
2890 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2892 txg_wait_synced(spa_get_dsl(spa
), 0);
2893 mmp_signal_all_threads();
2894 txg_wait_synced(spa_get_dsl(spa
), 0);
2896 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2897 mutex_enter(&spa
->spa_props_lock
);
2899 if (spa_multihost(spa
)) {
2900 mmp_thread_stop(spa
);
2901 spa
->spa_multihost
= B_FALSE
;
2904 mutex_exit(&spa
->spa_props_lock
);
2905 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2910 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2913 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2914 uint64_t version
, newversion
;
2915 nvlist_t
*nvroot
, *props
;
2918 if (ztest_opts
.zo_mmp_test
)
2921 /* dRAID added after feature flags, skip upgrade test. */
2922 if (strcmp(ztest_opts
.zo_raid_type
, VDEV_TYPE_DRAID
) == 0)
2925 mutex_enter(&ztest_vdev_lock
);
2926 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2929 * Clean up from previous runs.
2931 (void) spa_destroy(name
);
2933 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2934 NULL
, ztest_opts
.zo_raid_children
, ztest_opts
.zo_mirrors
, 1);
2937 * If we're configuring a RAIDZ device then make sure that the
2938 * initial version is capable of supporting that feature.
2940 switch (ztest_opts
.zo_raid_parity
) {
2943 initial_version
= SPA_VERSION_INITIAL
;
2946 initial_version
= SPA_VERSION_RAIDZ2
;
2949 initial_version
= SPA_VERSION_RAIDZ3
;
2954 * Create a pool with a spa version that can be upgraded. Pick
2955 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2958 version
= ztest_random_spa_version(initial_version
);
2959 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2961 props
= fnvlist_alloc();
2962 fnvlist_add_uint64(props
,
2963 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2964 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2965 fnvlist_free(nvroot
);
2966 fnvlist_free(props
);
2968 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2969 VERIFY3U(spa_version(spa
), ==, version
);
2970 newversion
= ztest_random_spa_version(version
+ 1);
2972 if (ztest_opts
.zo_verbose
>= 4) {
2973 (void) printf("upgrading spa version from %llu to %llu\n",
2974 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2977 spa_upgrade(spa
, newversion
);
2978 VERIFY3U(spa_version(spa
), >, version
);
2979 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2980 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2981 spa_close(spa
, FTAG
);
2984 mutex_exit(&ztest_vdev_lock
);
2988 ztest_spa_checkpoint(spa_t
*spa
)
2990 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2992 int error
= spa_checkpoint(spa
->spa_name
);
2996 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2997 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2998 case ZFS_ERR_CHECKPOINT_EXISTS
:
3001 ztest_record_enospc(FTAG
);
3004 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
3009 ztest_spa_discard_checkpoint(spa_t
*spa
)
3011 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
3013 int error
= spa_checkpoint_discard(spa
->spa_name
);
3017 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3018 case ZFS_ERR_NO_CHECKPOINT
:
3021 fatal(0, "spa_discard_checkpoint(%s) = %d",
3022 spa
->spa_name
, error
);
3029 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
3031 spa_t
*spa
= ztest_spa
;
3033 mutex_enter(&ztest_checkpoint_lock
);
3034 if (ztest_random(2) == 0) {
3035 ztest_spa_checkpoint(spa
);
3037 ztest_spa_discard_checkpoint(spa
);
3039 mutex_exit(&ztest_checkpoint_lock
);
3044 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
3049 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
3052 for (c
= 0; c
< vd
->vdev_children
; c
++)
3053 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
3061 spa_num_top_vdevs(spa_t
*spa
)
3063 vdev_t
*rvd
= spa
->spa_root_vdev
;
3064 ASSERT3U(spa_config_held(spa
, SCL_VDEV
, RW_READER
), ==, SCL_VDEV
);
3065 return (rvd
->vdev_children
);
3069 * Verify that vdev_add() works as expected.
3073 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3075 ztest_shared_t
*zs
= ztest_shared
;
3076 spa_t
*spa
= ztest_spa
;
3082 if (ztest_opts
.zo_mmp_test
)
3085 mutex_enter(&ztest_vdev_lock
);
3086 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) *
3087 ztest_opts
.zo_raid_children
;
3089 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3091 ztest_shared
->zs_vdev_next_leaf
= spa_num_top_vdevs(spa
) * leaves
;
3094 * If we have slogs then remove them 1/4 of the time.
3096 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
3097 metaslab_group_t
*mg
;
3100 * find the first real slog in log allocation class
3102 mg
= spa_log_class(spa
)->mc_rotor
;
3103 while (!mg
->mg_vd
->vdev_islog
)
3106 guid
= mg
->mg_vd
->vdev_guid
;
3108 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3111 * We have to grab the zs_name_lock as writer to
3112 * prevent a race between removing a slog (dmu_objset_find)
3113 * and destroying a dataset. Removing the slog will
3114 * grab a reference on the dataset which may cause
3115 * dsl_destroy_head() to fail with EBUSY thus
3116 * leaving the dataset in an inconsistent state.
3118 pthread_rwlock_wrlock(&ztest_name_lock
);
3119 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3120 pthread_rwlock_unlock(&ztest_name_lock
);
3124 case EEXIST
: /* Generic zil_reset() error */
3125 case EBUSY
: /* Replay required */
3126 case EACCES
: /* Crypto key not loaded */
3127 case ZFS_ERR_CHECKPOINT_EXISTS
:
3128 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3131 fatal(0, "spa_vdev_remove() = %d", error
);
3134 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3137 * Make 1/4 of the devices be log devices
3139 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3140 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3141 "log" : NULL
, ztest_opts
.zo_raid_children
, zs
->zs_mirrors
,
3144 error
= spa_vdev_add(spa
, nvroot
);
3145 nvlist_free(nvroot
);
3151 ztest_record_enospc("spa_vdev_add");
3154 fatal(0, "spa_vdev_add() = %d", error
);
3158 mutex_exit(&ztest_vdev_lock
);
3163 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3165 ztest_shared_t
*zs
= ztest_shared
;
3166 spa_t
*spa
= ztest_spa
;
3169 const char *class = (ztest_random(2) == 0) ?
3170 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3174 * By default add a special vdev 50% of the time
3176 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3177 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3178 ztest_random(2) == 0)) {
3182 mutex_enter(&ztest_vdev_lock
);
3184 /* Only test with mirrors */
3185 if (zs
->zs_mirrors
< 2) {
3186 mutex_exit(&ztest_vdev_lock
);
3190 /* requires feature@allocation_classes */
3191 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3192 mutex_exit(&ztest_vdev_lock
);
3196 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) *
3197 ztest_opts
.zo_raid_children
;
3199 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3200 ztest_shared
->zs_vdev_next_leaf
= spa_num_top_vdevs(spa
) * leaves
;
3201 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3203 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3204 class, ztest_opts
.zo_raid_children
, zs
->zs_mirrors
, 1);
3206 error
= spa_vdev_add(spa
, nvroot
);
3207 nvlist_free(nvroot
);
3209 if (error
== ENOSPC
)
3210 ztest_record_enospc("spa_vdev_add");
3211 else if (error
!= 0)
3212 fatal(0, "spa_vdev_add() = %d", error
);
3215 * 50% of the time allow small blocks in the special class
3218 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3219 if (ztest_opts
.zo_verbose
>= 3)
3220 (void) printf("Enabling special VDEV small blocks\n");
3221 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3222 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3225 mutex_exit(&ztest_vdev_lock
);
3227 if (ztest_opts
.zo_verbose
>= 3) {
3228 metaslab_class_t
*mc
;
3230 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3231 mc
= spa_special_class(spa
);
3233 mc
= spa_dedup_class(spa
);
3234 (void) printf("Added a %s mirrored vdev (of %d)\n",
3235 class, (int)mc
->mc_groups
);
3240 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3244 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3246 ztest_shared_t
*zs
= ztest_shared
;
3247 spa_t
*spa
= ztest_spa
;
3248 vdev_t
*rvd
= spa
->spa_root_vdev
;
3249 spa_aux_vdev_t
*sav
;
3253 int error
, ignore_err
= 0;
3255 if (ztest_opts
.zo_mmp_test
)
3258 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3260 if (ztest_random(2) == 0) {
3261 sav
= &spa
->spa_spares
;
3262 aux
= ZPOOL_CONFIG_SPARES
;
3264 sav
= &spa
->spa_l2cache
;
3265 aux
= ZPOOL_CONFIG_L2CACHE
;
3268 mutex_enter(&ztest_vdev_lock
);
3270 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3272 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3274 * Pick a random device to remove.
3276 vdev_t
*svd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3278 /* dRAID spares cannot be removed; try anyways to see ENOTSUP */
3279 if (strstr(svd
->vdev_path
, VDEV_TYPE_DRAID
) != NULL
)
3280 ignore_err
= ENOTSUP
;
3282 guid
= svd
->vdev_guid
;
3285 * Find an unused device we can add.
3287 zs
->zs_vdev_aux
= 0;
3290 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3291 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3293 for (c
= 0; c
< sav
->sav_count
; c
++)
3294 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3297 if (c
== sav
->sav_count
&&
3298 vdev_lookup_by_path(rvd
, path
) == NULL
)
3304 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3310 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3311 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3312 error
= spa_vdev_add(spa
, nvroot
);
3318 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3320 nvlist_free(nvroot
);
3323 * Remove an existing device. Sometimes, dirty its
3324 * vdev state first to make sure we handle removal
3325 * of devices that have pending state changes.
3327 if (ztest_random(2) == 0)
3328 (void) vdev_online(spa
, guid
, 0, NULL
);
3330 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3335 case ZFS_ERR_CHECKPOINT_EXISTS
:
3336 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3339 if (error
!= ignore_err
)
3340 fatal(0, "spa_vdev_remove(%llu) = %d", guid
,
3345 mutex_exit(&ztest_vdev_lock
);
3347 umem_free(path
, MAXPATHLEN
);
3351 * split a pool if it has mirror tlvdevs
3355 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3357 ztest_shared_t
*zs
= ztest_shared
;
3358 spa_t
*spa
= ztest_spa
;
3359 vdev_t
*rvd
= spa
->spa_root_vdev
;
3360 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3361 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3364 if (ztest_opts
.zo_mmp_test
)
3367 mutex_enter(&ztest_vdev_lock
);
3369 /* ensure we have a usable config; mirrors of raidz aren't supported */
3370 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raid_children
> 1) {
3371 mutex_exit(&ztest_vdev_lock
);
3375 /* clean up the old pool, if any */
3376 (void) spa_destroy("splitp");
3378 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3380 /* generate a config from the existing config */
3381 mutex_enter(&spa
->spa_props_lock
);
3382 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3384 mutex_exit(&spa
->spa_props_lock
);
3386 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3389 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3390 for (c
= 0; c
< children
; c
++) {
3391 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3395 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3396 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3398 VERIFY(nvlist_add_string(schild
[schildren
],
3399 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3400 VERIFY(nvlist_add_uint64(schild
[schildren
],
3401 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3403 lastlogid
= schildren
;
3408 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3409 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3410 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3413 /* OK, create a config that can be used to split */
3414 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3415 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3416 VDEV_TYPE_ROOT
) == 0);
3417 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3418 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3420 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3421 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3423 for (c
= 0; c
< schildren
; c
++)
3424 nvlist_free(schild
[c
]);
3428 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3430 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3431 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3432 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3434 nvlist_free(config
);
3437 (void) printf("successful split - results:\n");
3438 mutex_enter(&spa_namespace_lock
);
3439 show_pool_stats(spa
);
3440 show_pool_stats(spa_lookup("splitp"));
3441 mutex_exit(&spa_namespace_lock
);
3445 mutex_exit(&ztest_vdev_lock
);
3449 * Verify that we can attach and detach devices.
3453 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3455 ztest_shared_t
*zs
= ztest_shared
;
3456 spa_t
*spa
= ztest_spa
;
3457 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3458 vdev_t
*rvd
= spa
->spa_root_vdev
;
3459 vdev_t
*oldvd
, *newvd
, *pvd
;
3463 uint64_t ashift
= ztest_get_ashift();
3464 uint64_t oldguid
, pguid
;
3465 uint64_t oldsize
, newsize
;
3466 char *oldpath
, *newpath
;
3468 int oldvd_has_siblings
= B_FALSE
;
3469 int newvd_is_spare
= B_FALSE
;
3470 int newvd_is_dspare
= B_FALSE
;
3472 int error
, expected_error
;
3474 if (ztest_opts
.zo_mmp_test
)
3477 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3478 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3480 mutex_enter(&ztest_vdev_lock
);
3481 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raid_children
;
3483 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3486 * If a vdev is in the process of being removed, its removal may
3487 * finish while we are in progress, leading to an unexpected error
3488 * value. Don't bother trying to attach while we are in the middle
3491 if (ztest_device_removal_active
) {
3492 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3493 mutex_exit(&ztest_vdev_lock
);
3498 * Decide whether to do an attach or a replace.
3500 replacing
= ztest_random(2);
3503 * Pick a random top-level vdev.
3505 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3508 * Pick a random leaf within it.
3510 leaf
= ztest_random(leaves
);
3515 oldvd
= rvd
->vdev_child
[top
];
3517 /* pick a child from the mirror */
3518 if (zs
->zs_mirrors
>= 1) {
3519 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3520 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3521 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raid_children
];
3524 /* pick a child out of the raidz group */
3525 if (ztest_opts
.zo_raid_children
> 1) {
3526 if (strcmp(oldvd
->vdev_ops
->vdev_op_type
, "raidz") == 0)
3527 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3529 ASSERT(oldvd
->vdev_ops
== &vdev_draid_ops
);
3530 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raid_children
);
3531 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raid_children
];
3535 * If we're already doing an attach or replace, oldvd may be a
3536 * mirror vdev -- in which case, pick a random child.
3538 while (oldvd
->vdev_children
!= 0) {
3539 oldvd_has_siblings
= B_TRUE
;
3540 ASSERT(oldvd
->vdev_children
>= 2);
3541 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3544 oldguid
= oldvd
->vdev_guid
;
3545 oldsize
= vdev_get_min_asize(oldvd
);
3546 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3547 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3548 pvd
= oldvd
->vdev_parent
;
3549 pguid
= pvd
->vdev_guid
;
3552 * If oldvd has siblings, then half of the time, detach it. Prior
3553 * to the detach the pool is scrubbed in order to prevent creating
3554 * unrepairable blocks as a result of the data corruption injection.
3556 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3557 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3559 error
= ztest_scrub_impl(spa
);
3563 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3564 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3565 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3566 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3567 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3572 * For the new vdev, choose with equal probability between the two
3573 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3575 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3576 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3577 newvd_is_spare
= B_TRUE
;
3579 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
)
3580 newvd_is_dspare
= B_TRUE
;
3582 (void) strcpy(newpath
, newvd
->vdev_path
);
3584 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3585 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3586 top
* leaves
+ leaf
);
3587 if (ztest_random(2) == 0)
3588 newpath
[strlen(newpath
) - 1] = 'b';
3589 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3594 * Reopen to ensure the vdev's asize field isn't stale.
3597 newsize
= vdev_get_min_asize(newvd
);
3600 * Make newsize a little bigger or smaller than oldsize.
3601 * If it's smaller, the attach should fail.
3602 * If it's larger, and we're doing a replace,
3603 * we should get dynamic LUN growth when we're done.
3605 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3609 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3610 * unless it's a replace; in that case any non-replacing parent is OK.
3612 * If newvd is already part of the pool, it should fail with EBUSY.
3614 * If newvd is too small, it should fail with EOVERFLOW.
3616 * If newvd is a distributed spare and it's being attached to a
3617 * dRAID which is not its parent it should fail with EINVAL.
3619 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3620 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3621 pvd
->vdev_ops
== &vdev_replacing_ops
||
3622 pvd
->vdev_ops
== &vdev_spare_ops
))
3623 expected_error
= ENOTSUP
;
3624 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3625 expected_error
= ENOTSUP
;
3626 else if (newvd
== oldvd
)
3627 expected_error
= replacing
? 0 : EBUSY
;
3628 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3629 expected_error
= EBUSY
;
3630 else if (!newvd_is_dspare
&& newsize
< oldsize
)
3631 expected_error
= EOVERFLOW
;
3632 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3633 expected_error
= EDOM
;
3634 else if (newvd_is_dspare
&& pvd
!= vdev_draid_spare_get_parent(newvd
))
3635 expected_error
= ENOTSUP
;
3639 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3642 * Build the nvlist describing newpath.
3644 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3645 ashift
, NULL
, 0, 0, 1);
3648 * When supported select either a healing or sequential resilver.
3650 boolean_t rebuilding
= B_FALSE
;
3651 if (pvd
->vdev_ops
== &vdev_mirror_ops
||
3652 pvd
->vdev_ops
== &vdev_root_ops
) {
3653 rebuilding
= !!ztest_random(2);
3656 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
, rebuilding
);
3661 * If our parent was the replacing vdev, but the replace completed,
3662 * then instead of failing with ENOTSUP we may either succeed,
3663 * fail with ENODEV, or fail with EOVERFLOW.
3665 if (expected_error
== ENOTSUP
&&
3666 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3667 expected_error
= error
;
3670 * If someone grew the LUN, the replacement may be too small.
3672 if (error
== EOVERFLOW
|| error
== EBUSY
)
3673 expected_error
= error
;
3675 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3676 error
== ZFS_ERR_DISCARDING_CHECKPOINT
||
3677 error
== ZFS_ERR_RESILVER_IN_PROGRESS
||
3678 error
== ZFS_ERR_REBUILD_IN_PROGRESS
)
3679 expected_error
= error
;
3681 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3682 fatal(0, "attach (%s %llu, %s %llu, %d) "
3683 "returned %d, expected %d",
3684 oldpath
, oldsize
, newpath
,
3685 newsize
, replacing
, error
, expected_error
);
3688 mutex_exit(&ztest_vdev_lock
);
3690 umem_free(oldpath
, MAXPATHLEN
);
3691 umem_free(newpath
, MAXPATHLEN
);
3696 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3698 spa_t
*spa
= ztest_spa
;
3703 mutex_enter(&ztest_vdev_lock
);
3705 if (ztest_device_removal_active
) {
3706 mutex_exit(&ztest_vdev_lock
);
3711 * Remove a random top-level vdev and wait for removal to finish.
3713 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3714 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3715 guid
= vd
->vdev_guid
;
3716 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3718 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3720 ztest_device_removal_active
= B_TRUE
;
3721 mutex_exit(&ztest_vdev_lock
);
3724 * spa->spa_vdev_removal is created in a sync task that
3725 * is initiated via dsl_sync_task_nowait(). Since the
3726 * task may not run before spa_vdev_remove() returns, we
3727 * must wait at least 1 txg to ensure that the removal
3728 * struct has been created.
3730 txg_wait_synced(spa_get_dsl(spa
), 0);
3732 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
3733 txg_wait_synced(spa_get_dsl(spa
), 0);
3735 mutex_exit(&ztest_vdev_lock
);
3740 * The pool needs to be scrubbed after completing device removal.
3741 * Failure to do so may result in checksum errors due to the
3742 * strategy employed by ztest_fault_inject() when selecting which
3743 * offset are redundant and can be damaged.
3745 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3747 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3748 txg_wait_synced(spa_get_dsl(spa
), 0);
3751 mutex_enter(&ztest_vdev_lock
);
3752 ztest_device_removal_active
= B_FALSE
;
3753 mutex_exit(&ztest_vdev_lock
);
3757 * Callback function which expands the physical size of the vdev.
3760 grow_vdev(vdev_t
*vd
, void *arg
)
3762 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
3763 size_t *newsize
= arg
;
3767 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3768 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3770 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3773 fsize
= lseek(fd
, 0, SEEK_END
);
3774 VERIFY(ftruncate(fd
, *newsize
) == 0);
3776 if (ztest_opts
.zo_verbose
>= 6) {
3777 (void) printf("%s grew from %lu to %lu bytes\n",
3778 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3785 * Callback function which expands a given vdev by calling vdev_online().
3789 online_vdev(vdev_t
*vd
, void *arg
)
3791 spa_t
*spa
= vd
->vdev_spa
;
3792 vdev_t
*tvd
= vd
->vdev_top
;
3793 uint64_t guid
= vd
->vdev_guid
;
3794 uint64_t generation
= spa
->spa_config_generation
+ 1;
3795 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3798 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3799 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3801 /* Calling vdev_online will initialize the new metaslabs */
3802 spa_config_exit(spa
, SCL_STATE
, spa
);
3803 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3804 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3807 * If vdev_online returned an error or the underlying vdev_open
3808 * failed then we abort the expand. The only way to know that
3809 * vdev_open fails is by checking the returned newstate.
3811 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3812 if (ztest_opts
.zo_verbose
>= 5) {
3813 (void) printf("Unable to expand vdev, state %llu, "
3814 "error %d\n", (u_longlong_t
)newstate
, error
);
3818 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3821 * Since we dropped the lock we need to ensure that we're
3822 * still talking to the original vdev. It's possible this
3823 * vdev may have been detached/replaced while we were
3824 * trying to online it.
3826 if (generation
!= spa
->spa_config_generation
) {
3827 if (ztest_opts
.zo_verbose
>= 5) {
3828 (void) printf("vdev configuration has changed, "
3829 "guid %llu, state %llu, expected gen %llu, "
3832 (u_longlong_t
)tvd
->vdev_state
,
3833 (u_longlong_t
)generation
,
3834 (u_longlong_t
)spa
->spa_config_generation
);
3842 * Traverse the vdev tree calling the supplied function.
3843 * We continue to walk the tree until we either have walked all
3844 * children or we receive a non-NULL return from the callback.
3845 * If a NULL callback is passed, then we just return back the first
3846 * leaf vdev we encounter.
3849 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3853 if (vd
->vdev_ops
->vdev_op_leaf
) {
3857 return (func(vd
, arg
));
3860 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3861 vdev_t
*cvd
= vd
->vdev_child
[c
];
3862 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3869 * Verify that dynamic LUN growth works as expected.
3873 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3875 spa_t
*spa
= ztest_spa
;
3877 metaslab_class_t
*mc
;
3878 metaslab_group_t
*mg
;
3879 size_t psize
, newsize
;
3881 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3883 mutex_enter(&ztest_checkpoint_lock
);
3884 mutex_enter(&ztest_vdev_lock
);
3885 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3888 * If there is a vdev removal in progress, it could complete while
3889 * we are running, in which case we would not be able to verify
3890 * that the metaslab_class space increased (because it decreases
3891 * when the device removal completes).
3893 if (ztest_device_removal_active
) {
3894 spa_config_exit(spa
, SCL_STATE
, spa
);
3895 mutex_exit(&ztest_vdev_lock
);
3896 mutex_exit(&ztest_checkpoint_lock
);
3900 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3902 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3905 old_ms_count
= tvd
->vdev_ms_count
;
3906 old_class_space
= metaslab_class_get_space(mc
);
3909 * Determine the size of the first leaf vdev associated with
3910 * our top-level device.
3912 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3913 ASSERT3P(vd
, !=, NULL
);
3914 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3916 psize
= vd
->vdev_psize
;
3919 * We only try to expand the vdev if it's healthy, less than 4x its
3920 * original size, and it has a valid psize.
3922 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3923 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3924 spa_config_exit(spa
, SCL_STATE
, spa
);
3925 mutex_exit(&ztest_vdev_lock
);
3926 mutex_exit(&ztest_checkpoint_lock
);
3930 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3931 ASSERT3U(newsize
, >, psize
);
3933 if (ztest_opts
.zo_verbose
>= 6) {
3934 (void) printf("Expanding LUN %s from %lu to %lu\n",
3935 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3939 * Growing the vdev is a two step process:
3940 * 1). expand the physical size (i.e. relabel)
3941 * 2). online the vdev to create the new metaslabs
3943 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3944 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3945 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3946 if (ztest_opts
.zo_verbose
>= 5) {
3947 (void) printf("Could not expand LUN because "
3948 "the vdev configuration changed.\n");
3950 spa_config_exit(spa
, SCL_STATE
, spa
);
3951 mutex_exit(&ztest_vdev_lock
);
3952 mutex_exit(&ztest_checkpoint_lock
);
3956 spa_config_exit(spa
, SCL_STATE
, spa
);
3959 * Expanding the LUN will update the config asynchronously,
3960 * thus we must wait for the async thread to complete any
3961 * pending tasks before proceeding.
3965 mutex_enter(&spa
->spa_async_lock
);
3966 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3967 mutex_exit(&spa
->spa_async_lock
);
3970 txg_wait_synced(spa_get_dsl(spa
), 0);
3971 (void) poll(NULL
, 0, 100);
3974 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3976 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3977 new_ms_count
= tvd
->vdev_ms_count
;
3978 new_class_space
= metaslab_class_get_space(mc
);
3980 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3981 if (ztest_opts
.zo_verbose
>= 5) {
3982 (void) printf("Could not verify LUN expansion due to "
3983 "intervening vdev offline or remove.\n");
3985 spa_config_exit(spa
, SCL_STATE
, spa
);
3986 mutex_exit(&ztest_vdev_lock
);
3987 mutex_exit(&ztest_checkpoint_lock
);
3992 * Make sure we were able to grow the vdev.
3994 if (new_ms_count
<= old_ms_count
) {
3995 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3996 old_ms_count
, new_ms_count
);
4000 * Make sure we were able to grow the pool.
4002 if (new_class_space
<= old_class_space
) {
4003 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
4004 old_class_space
, new_class_space
);
4007 if (ztest_opts
.zo_verbose
>= 5) {
4008 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
4010 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
4011 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
4012 (void) printf("%s grew from %s to %s\n",
4013 spa
->spa_name
, oldnumbuf
, newnumbuf
);
4016 spa_config_exit(spa
, SCL_STATE
, spa
);
4017 mutex_exit(&ztest_vdev_lock
);
4018 mutex_exit(&ztest_checkpoint_lock
);
4022 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
4026 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
4029 * Create the objects common to all ztest datasets.
4031 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
4032 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
4036 ztest_dataset_create(char *dsname
)
4040 dsl_crypto_params_t
*dcp
= NULL
;
4043 * 50% of the time, we create encrypted datasets
4044 * using a random cipher suite and a hard-coded
4047 rand
= ztest_random(2);
4049 nvlist_t
*crypto_args
= fnvlist_alloc();
4050 nvlist_t
*props
= fnvlist_alloc();
4052 /* slight bias towards the default cipher suite */
4053 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
4054 if (rand
< ZIO_CRYPT_AES_128_CCM
)
4055 rand
= ZIO_CRYPT_ON
;
4057 fnvlist_add_uint64(props
,
4058 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
4059 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
4060 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
4063 * These parameters aren't really used by the kernel. They
4064 * are simply stored so that userspace knows how to load
4067 fnvlist_add_uint64(props
,
4068 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
4069 fnvlist_add_string(props
,
4070 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
4071 fnvlist_add_uint64(props
,
4072 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
4073 fnvlist_add_uint64(props
,
4074 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
4076 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
4077 crypto_args
, &dcp
));
4080 * Cycle through all available encryption implementations
4081 * to verify interoperability.
4083 VERIFY0(gcm_impl_set("cycle"));
4084 VERIFY0(aes_impl_set("cycle"));
4086 fnvlist_free(crypto_args
);
4087 fnvlist_free(props
);
4090 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
4091 ztest_objset_create_cb
, NULL
);
4092 dsl_crypto_params_free(dcp
, !!err
);
4094 rand
= ztest_random(100);
4095 if (err
|| rand
< 80)
4098 if (ztest_opts
.zo_verbose
>= 5)
4099 (void) printf("Setting dataset %s to sync always\n", dsname
);
4100 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
4101 ZFS_SYNC_ALWAYS
, B_FALSE
));
4106 ztest_objset_destroy_cb(const char *name
, void *arg
)
4109 dmu_object_info_t doi
;
4113 * Verify that the dataset contains a directory object.
4115 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4116 B_TRUE
, FTAG
, &os
));
4117 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
4118 if (error
!= ENOENT
) {
4119 /* We could have crashed in the middle of destroying it */
4121 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
4122 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
4124 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4127 * Destroy the dataset.
4129 if (strchr(name
, '@') != NULL
) {
4130 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
4132 error
= dsl_destroy_head(name
);
4133 if (error
== ENOSPC
) {
4134 /* There could be checkpoint or insufficient slop */
4135 ztest_record_enospc(FTAG
);
4136 } else if (error
!= EBUSY
) {
4137 /* There could be a hold on this dataset */
4145 ztest_snapshot_create(char *osname
, uint64_t id
)
4147 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4150 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4152 error
= dmu_objset_snapshot_one(osname
, snapname
);
4153 if (error
== ENOSPC
) {
4154 ztest_record_enospc(FTAG
);
4157 if (error
!= 0 && error
!= EEXIST
) {
4158 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4165 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4167 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4170 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4173 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4174 if (error
!= 0 && error
!= ENOENT
)
4175 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4181 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4187 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4191 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4193 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4195 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4196 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4199 * If this dataset exists from a previous run, process its replay log
4200 * half of the time. If we don't replay it, then dsl_destroy_head()
4201 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4203 if (ztest_random(2) == 0 &&
4204 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4205 B_TRUE
, FTAG
, &os
) == 0) {
4206 ztest_zd_init(zdtmp
, NULL
, os
);
4207 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4208 ztest_zd_fini(zdtmp
);
4209 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4213 * There may be an old instance of the dataset we're about to
4214 * create lying around from a previous run. If so, destroy it
4215 * and all of its snapshots.
4217 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4218 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4221 * Verify that the destroyed dataset is no longer in the namespace.
4223 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4224 B_TRUE
, FTAG
, &os
));
4227 * Verify that we can create a new dataset.
4229 error
= ztest_dataset_create(name
);
4231 if (error
== ENOSPC
) {
4232 ztest_record_enospc(FTAG
);
4235 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4238 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4241 ztest_zd_init(zdtmp
, NULL
, os
);
4244 * Open the intent log for it.
4246 zilog
= zil_open(os
, ztest_get_data
);
4249 * Put some objects in there, do a little I/O to them,
4250 * and randomly take a couple of snapshots along the way.
4252 iters
= ztest_random(5);
4253 for (i
= 0; i
< iters
; i
++) {
4254 ztest_dmu_object_alloc_free(zdtmp
, id
);
4255 if (ztest_random(iters
) == 0)
4256 (void) ztest_snapshot_create(name
, i
);
4260 * Verify that we cannot create an existing dataset.
4262 VERIFY3U(EEXIST
, ==,
4263 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4266 * Verify that we can hold an objset that is also owned.
4268 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4269 dmu_objset_rele(os2
, FTAG
);
4272 * Verify that we cannot own an objset that is already owned.
4274 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4275 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4278 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4279 ztest_zd_fini(zdtmp
);
4281 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4283 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4287 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4290 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4292 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4293 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4294 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4295 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4299 * Cleanup non-standard snapshots and clones.
4302 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4311 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4312 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4313 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4314 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4315 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4317 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4318 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4319 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4320 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4321 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4322 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4323 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4324 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4325 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4326 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4328 error
= dsl_destroy_head(clone2name
);
4329 if (error
&& error
!= ENOENT
)
4330 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4331 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4332 if (error
&& error
!= ENOENT
)
4333 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4334 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4335 if (error
&& error
!= ENOENT
)
4336 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4337 error
= dsl_destroy_head(clone1name
);
4338 if (error
&& error
!= ENOENT
)
4339 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4340 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4341 if (error
&& error
!= ENOENT
)
4342 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4344 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4345 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4346 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4347 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4348 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4352 * Verify dsl_dataset_promote handles EBUSY
4355 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4363 char *osname
= zd
->zd_name
;
4366 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4367 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4368 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4369 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4370 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4372 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4374 ztest_dsl_dataset_cleanup(osname
, id
);
4376 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4377 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4378 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4379 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4380 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4381 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4382 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4383 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4384 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4385 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4387 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4388 if (error
&& error
!= EEXIST
) {
4389 if (error
== ENOSPC
) {
4390 ztest_record_enospc(FTAG
);
4393 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4396 error
= dmu_objset_clone(clone1name
, snap1name
);
4398 if (error
== ENOSPC
) {
4399 ztest_record_enospc(FTAG
);
4402 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4405 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4406 if (error
&& error
!= EEXIST
) {
4407 if (error
== ENOSPC
) {
4408 ztest_record_enospc(FTAG
);
4411 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4414 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4415 if (error
&& error
!= EEXIST
) {
4416 if (error
== ENOSPC
) {
4417 ztest_record_enospc(FTAG
);
4420 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4423 error
= dmu_objset_clone(clone2name
, snap3name
);
4425 if (error
== ENOSPC
) {
4426 ztest_record_enospc(FTAG
);
4429 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4432 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4435 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4436 error
= dsl_dataset_promote(clone2name
, NULL
);
4437 if (error
== ENOSPC
) {
4438 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4439 ztest_record_enospc(FTAG
);
4443 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4445 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4448 ztest_dsl_dataset_cleanup(osname
, id
);
4450 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4452 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4453 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4454 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4455 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4456 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4459 #undef OD_ARRAY_SIZE
4460 #define OD_ARRAY_SIZE 4
4463 * Verify that dmu_object_{alloc,free} work as expected.
4466 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4473 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4474 od
= umem_alloc(size
, UMEM_NOFAIL
);
4475 batchsize
= OD_ARRAY_SIZE
;
4477 for (b
= 0; b
< batchsize
; b
++)
4478 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4482 * Destroy the previous batch of objects, create a new batch,
4483 * and do some I/O on the new objects.
4485 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4488 while (ztest_random(4 * batchsize
) != 0)
4489 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4490 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4492 umem_free(od
, size
);
4496 * Rewind the global allocator to verify object allocation backfilling.
4499 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4501 objset_t
*os
= zd
->zd_os
;
4502 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4506 * Rewind the global allocator randomly back to a lower object number
4507 * to force backfilling and reclamation of recently freed dnodes.
4509 mutex_enter(&os
->os_obj_lock
);
4510 object
= ztest_random(os
->os_obj_next_chunk
);
4511 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4512 mutex_exit(&os
->os_obj_lock
);
4515 #undef OD_ARRAY_SIZE
4516 #define OD_ARRAY_SIZE 2
4519 * Verify that dmu_{read,write} work as expected.
4522 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4527 objset_t
*os
= zd
->zd_os
;
4528 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4529 od
= umem_alloc(size
, UMEM_NOFAIL
);
4531 int i
, freeit
, error
;
4533 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4534 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4535 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4536 uint64_t regions
= 997;
4537 uint64_t stride
= 123456789ULL;
4538 uint64_t width
= 40;
4539 int free_percent
= 5;
4542 * This test uses two objects, packobj and bigobj, that are always
4543 * updated together (i.e. in the same tx) so that their contents are
4544 * in sync and can be compared. Their contents relate to each other
4545 * in a simple way: packobj is a dense array of 'bufwad' structures,
4546 * while bigobj is a sparse array of the same bufwads. Specifically,
4547 * for any index n, there are three bufwads that should be identical:
4549 * packobj, at offset n * sizeof (bufwad_t)
4550 * bigobj, at the head of the nth chunk
4551 * bigobj, at the tail of the nth chunk
4553 * The chunk size is arbitrary. It doesn't have to be a power of two,
4554 * and it doesn't have any relation to the object blocksize.
4555 * The only requirement is that it can hold at least two bufwads.
4557 * Normally, we write the bufwad to each of these locations.
4558 * However, free_percent of the time we instead write zeroes to
4559 * packobj and perform a dmu_free_range() on bigobj. By comparing
4560 * bigobj to packobj, we can verify that the DMU is correctly
4561 * tracking which parts of an object are allocated and free,
4562 * and that the contents of the allocated blocks are correct.
4566 * Read the directory info. If it's the first time, set things up.
4568 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4569 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4572 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4573 umem_free(od
, size
);
4577 bigobj
= od
[0].od_object
;
4578 packobj
= od
[1].od_object
;
4579 chunksize
= od
[0].od_gen
;
4580 ASSERT(chunksize
== od
[1].od_gen
);
4583 * Prefetch a random chunk of the big object.
4584 * Our aim here is to get some async reads in flight
4585 * for blocks that we may free below; the DMU should
4586 * handle this race correctly.
4588 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4589 s
= 1 + ztest_random(2 * width
- 1);
4590 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4591 ZIO_PRIORITY_SYNC_READ
);
4594 * Pick a random index and compute the offsets into packobj and bigobj.
4596 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4597 s
= 1 + ztest_random(width
- 1);
4599 packoff
= n
* sizeof (bufwad_t
);
4600 packsize
= s
* sizeof (bufwad_t
);
4602 bigoff
= n
* chunksize
;
4603 bigsize
= s
* chunksize
;
4605 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4606 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4609 * free_percent of the time, free a range of bigobj rather than
4612 freeit
= (ztest_random(100) < free_percent
);
4615 * Read the current contents of our objects.
4617 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4620 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4625 * Get a tx for the mods to both packobj and bigobj.
4627 tx
= dmu_tx_create(os
);
4629 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4632 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4634 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4636 /* This accounts for setting the checksum/compression. */
4637 dmu_tx_hold_bonus(tx
, bigobj
);
4639 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4641 umem_free(packbuf
, packsize
);
4642 umem_free(bigbuf
, bigsize
);
4643 umem_free(od
, size
);
4647 enum zio_checksum cksum
;
4649 cksum
= (enum zio_checksum
)
4650 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4651 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4652 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4654 enum zio_compress comp
;
4656 comp
= (enum zio_compress
)
4657 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4658 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4659 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4662 * For each index from n to n + s, verify that the existing bufwad
4663 * in packobj matches the bufwads at the head and tail of the
4664 * corresponding chunk in bigobj. Then update all three bufwads
4665 * with the new values we want to write out.
4667 for (i
= 0; i
< s
; i
++) {
4669 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4671 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4673 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4675 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4676 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4678 if (pack
->bw_txg
> txg
)
4679 fatal(0, "future leak: got %llx, open txg is %llx",
4682 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4683 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4684 pack
->bw_index
, n
, i
);
4686 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4687 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4689 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4690 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4693 bzero(pack
, sizeof (bufwad_t
));
4695 pack
->bw_index
= n
+ i
;
4697 pack
->bw_data
= 1 + ztest_random(-2ULL);
4704 * We've verified all the old bufwads, and made new ones.
4705 * Now write them out.
4707 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4710 if (ztest_opts
.zo_verbose
>= 7) {
4711 (void) printf("freeing offset %llx size %llx"
4713 (u_longlong_t
)bigoff
,
4714 (u_longlong_t
)bigsize
,
4717 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4719 if (ztest_opts
.zo_verbose
>= 7) {
4720 (void) printf("writing offset %llx size %llx"
4722 (u_longlong_t
)bigoff
,
4723 (u_longlong_t
)bigsize
,
4726 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4732 * Sanity check the stuff we just wrote.
4735 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4736 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4738 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4739 packsize
, packcheck
, DMU_READ_PREFETCH
));
4740 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4741 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4743 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4744 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4746 umem_free(packcheck
, packsize
);
4747 umem_free(bigcheck
, bigsize
);
4750 umem_free(packbuf
, packsize
);
4751 umem_free(bigbuf
, bigsize
);
4752 umem_free(od
, size
);
4756 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4757 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4765 * For each index from n to n + s, verify that the existing bufwad
4766 * in packobj matches the bufwads at the head and tail of the
4767 * corresponding chunk in bigobj. Then update all three bufwads
4768 * with the new values we want to write out.
4770 for (i
= 0; i
< s
; i
++) {
4772 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4774 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4776 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4778 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4779 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4781 if (pack
->bw_txg
> txg
)
4782 fatal(0, "future leak: got %llx, open txg is %llx",
4785 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4786 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4787 pack
->bw_index
, n
, i
);
4789 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4790 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4792 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4793 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4795 pack
->bw_index
= n
+ i
;
4797 pack
->bw_data
= 1 + ztest_random(-2ULL);
4804 #undef OD_ARRAY_SIZE
4805 #define OD_ARRAY_SIZE 2
4808 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4810 objset_t
*os
= zd
->zd_os
;
4817 bufwad_t
*packbuf
, *bigbuf
;
4818 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4819 uint64_t blocksize
= ztest_random_blocksize();
4820 uint64_t chunksize
= blocksize
;
4821 uint64_t regions
= 997;
4822 uint64_t stride
= 123456789ULL;
4824 dmu_buf_t
*bonus_db
;
4825 arc_buf_t
**bigbuf_arcbufs
;
4826 dmu_object_info_t doi
;
4828 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4829 od
= umem_alloc(size
, UMEM_NOFAIL
);
4832 * This test uses two objects, packobj and bigobj, that are always
4833 * updated together (i.e. in the same tx) so that their contents are
4834 * in sync and can be compared. Their contents relate to each other
4835 * in a simple way: packobj is a dense array of 'bufwad' structures,
4836 * while bigobj is a sparse array of the same bufwads. Specifically,
4837 * for any index n, there are three bufwads that should be identical:
4839 * packobj, at offset n * sizeof (bufwad_t)
4840 * bigobj, at the head of the nth chunk
4841 * bigobj, at the tail of the nth chunk
4843 * The chunk size is set equal to bigobj block size so that
4844 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4848 * Read the directory info. If it's the first time, set things up.
4850 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4851 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4855 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4856 umem_free(od
, size
);
4860 bigobj
= od
[0].od_object
;
4861 packobj
= od
[1].od_object
;
4862 blocksize
= od
[0].od_blocksize
;
4863 chunksize
= blocksize
;
4864 ASSERT(chunksize
== od
[1].od_gen
);
4866 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4867 VERIFY(ISP2(doi
.doi_data_block_size
));
4868 VERIFY(chunksize
== doi
.doi_data_block_size
);
4869 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4872 * Pick a random index and compute the offsets into packobj and bigobj.
4874 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4875 s
= 1 + ztest_random(width
- 1);
4877 packoff
= n
* sizeof (bufwad_t
);
4878 packsize
= s
* sizeof (bufwad_t
);
4880 bigoff
= n
* chunksize
;
4881 bigsize
= s
* chunksize
;
4883 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4884 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4886 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4888 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4891 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4892 * Iteration 1 test zcopy to already referenced dbufs.
4893 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4894 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4895 * Iteration 4 test zcopy when dbuf is no longer dirty.
4896 * Iteration 5 test zcopy when it can't be done.
4897 * Iteration 6 one more zcopy write.
4899 for (i
= 0; i
< 7; i
++) {
4904 * In iteration 5 (i == 5) use arcbufs
4905 * that don't match bigobj blksz to test
4906 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4907 * assign an arcbuf to a dbuf.
4909 for (j
= 0; j
< s
; j
++) {
4910 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4912 dmu_request_arcbuf(bonus_db
, chunksize
);
4914 bigbuf_arcbufs
[2 * j
] =
4915 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4916 bigbuf_arcbufs
[2 * j
+ 1] =
4917 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4922 * Get a tx for the mods to both packobj and bigobj.
4924 tx
= dmu_tx_create(os
);
4926 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4927 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4929 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4931 umem_free(packbuf
, packsize
);
4932 umem_free(bigbuf
, bigsize
);
4933 for (j
= 0; j
< s
; j
++) {
4935 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4936 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4939 bigbuf_arcbufs
[2 * j
]);
4941 bigbuf_arcbufs
[2 * j
+ 1]);
4944 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4945 umem_free(od
, size
);
4946 dmu_buf_rele(bonus_db
, FTAG
);
4951 * 50% of the time don't read objects in the 1st iteration to
4952 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4953 * no existing dbufs for the specified offsets.
4955 if (i
!= 0 || ztest_random(2) != 0) {
4956 error
= dmu_read(os
, packobj
, packoff
,
4957 packsize
, packbuf
, DMU_READ_PREFETCH
);
4959 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4960 bigbuf
, DMU_READ_PREFETCH
);
4963 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4967 * We've verified all the old bufwads, and made new ones.
4968 * Now write them out.
4970 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4971 if (ztest_opts
.zo_verbose
>= 7) {
4972 (void) printf("writing offset %llx size %llx"
4974 (u_longlong_t
)bigoff
,
4975 (u_longlong_t
)bigsize
,
4978 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4980 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4981 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4982 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4984 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4985 bigbuf_arcbufs
[2 * j
]->b_data
,
4987 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4989 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4994 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4995 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4997 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4998 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
4999 off
, bigbuf_arcbufs
[j
], tx
));
5001 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
5002 off
, bigbuf_arcbufs
[2 * j
], tx
));
5003 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
5004 off
+ chunksize
/ 2,
5005 bigbuf_arcbufs
[2 * j
+ 1], tx
));
5008 dmu_buf_rele(dbt
, FTAG
);
5014 * Sanity check the stuff we just wrote.
5017 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
5018 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
5020 VERIFY0(dmu_read(os
, packobj
, packoff
,
5021 packsize
, packcheck
, DMU_READ_PREFETCH
));
5022 VERIFY0(dmu_read(os
, bigobj
, bigoff
,
5023 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
5025 ASSERT0(bcmp(packbuf
, packcheck
, packsize
));
5026 ASSERT0(bcmp(bigbuf
, bigcheck
, bigsize
));
5028 umem_free(packcheck
, packsize
);
5029 umem_free(bigcheck
, bigsize
);
5032 txg_wait_open(dmu_objset_pool(os
), 0, B_TRUE
);
5033 } else if (i
== 3) {
5034 txg_wait_synced(dmu_objset_pool(os
), 0);
5038 dmu_buf_rele(bonus_db
, FTAG
);
5039 umem_free(packbuf
, packsize
);
5040 umem_free(bigbuf
, bigsize
);
5041 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
5042 umem_free(od
, size
);
5047 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
5051 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5052 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
5053 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
5056 * Have multiple threads write to large offsets in an object
5057 * to verify that parallel writes to an object -- even to the
5058 * same blocks within the object -- doesn't cause any trouble.
5060 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5062 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
5065 while (ztest_random(10) != 0)
5066 ztest_io(zd
, od
->od_object
, offset
);
5068 umem_free(od
, sizeof (ztest_od_t
));
5072 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
5075 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
5076 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
5077 uint64_t count
= ztest_random(20) + 1;
5078 uint64_t blocksize
= ztest_random_blocksize();
5081 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5083 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
5085 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5086 !ztest_random(2)) != 0) {
5087 umem_free(od
, sizeof (ztest_od_t
));
5091 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
5092 umem_free(od
, sizeof (ztest_od_t
));
5096 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
5098 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
5100 while (ztest_random(count
) != 0) {
5101 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
5102 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
5105 while (ztest_random(4) != 0)
5106 ztest_io(zd
, od
->od_object
, randoff
);
5109 umem_free(data
, blocksize
);
5110 umem_free(od
, sizeof (ztest_od_t
));
5114 * Verify that zap_{create,destroy,add,remove,update} work as expected.
5116 #define ZTEST_ZAP_MIN_INTS 1
5117 #define ZTEST_ZAP_MAX_INTS 4
5118 #define ZTEST_ZAP_MAX_PROPS 1000
5121 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
5123 objset_t
*os
= zd
->zd_os
;
5126 uint64_t txg
, last_txg
;
5127 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
5128 uint64_t zl_ints
, zl_intsize
, prop
;
5131 char propname
[100], txgname
[100];
5133 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
5135 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5136 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5138 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5139 !ztest_random(2)) != 0)
5142 object
= od
->od_object
;
5145 * Generate a known hash collision, and verify that
5146 * we can lookup and remove both entries.
5148 tx
= dmu_tx_create(os
);
5149 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5150 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5153 for (i
= 0; i
< 2; i
++) {
5155 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5158 for (i
= 0; i
< 2; i
++) {
5159 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5160 sizeof (uint64_t), 1, &value
[i
], tx
));
5162 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5163 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5164 ASSERT3U(zl_ints
, ==, 1);
5166 for (i
= 0; i
< 2; i
++) {
5167 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5172 * Generate a bunch of random entries.
5174 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5176 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5177 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5178 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5179 bzero(value
, sizeof (value
));
5183 * If these zap entries already exist, validate their contents.
5185 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5187 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5188 ASSERT3U(zl_ints
, ==, 1);
5190 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5191 zl_ints
, &last_txg
) == 0);
5193 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5196 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5197 ASSERT3U(zl_ints
, ==, ints
);
5199 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5200 zl_ints
, value
) == 0);
5202 for (i
= 0; i
< ints
; i
++) {
5203 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5206 ASSERT3U(error
, ==, ENOENT
);
5210 * Atomically update two entries in our zap object.
5211 * The first is named txg_%llu, and contains the txg
5212 * in which the property was last updated. The second
5213 * is named prop_%llu, and the nth element of its value
5214 * should be txg + object + n.
5216 tx
= dmu_tx_create(os
);
5217 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5218 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5223 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5225 for (i
= 0; i
< ints
; i
++)
5226 value
[i
] = txg
+ object
+ i
;
5228 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5230 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5236 * Remove a random pair of entries.
5238 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5239 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5240 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5242 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5244 if (error
== ENOENT
)
5249 tx
= dmu_tx_create(os
);
5250 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5251 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5254 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5255 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5258 umem_free(od
, sizeof (ztest_od_t
));
5262 * Test case to test the upgrading of a microzap to fatzap.
5265 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5267 objset_t
*os
= zd
->zd_os
;
5269 uint64_t object
, txg
;
5272 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5273 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5275 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5276 !ztest_random(2)) != 0)
5278 object
= od
->od_object
;
5281 * Add entries to this ZAP and make sure it spills over
5282 * and gets upgraded to a fatzap. Also, since we are adding
5283 * 2050 entries we should see ptrtbl growth and leaf-block split.
5285 for (i
= 0; i
< 2050; i
++) {
5286 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5291 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5292 (u_longlong_t
)id
, (u_longlong_t
)value
);
5294 tx
= dmu_tx_create(os
);
5295 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5296 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5299 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5301 ASSERT(error
== 0 || error
== EEXIST
);
5305 umem_free(od
, sizeof (ztest_od_t
));
5310 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5312 objset_t
*os
= zd
->zd_os
;
5314 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5316 int i
, namelen
, error
;
5317 int micro
= ztest_random(2);
5318 char name
[20], string_value
[20];
5321 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5322 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5324 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5325 umem_free(od
, sizeof (ztest_od_t
));
5329 object
= od
->od_object
;
5332 * Generate a random name of the form 'xxx.....' where each
5333 * x is a random printable character and the dots are dots.
5334 * There are 94 such characters, and the name length goes from
5335 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5337 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5339 for (i
= 0; i
< 3; i
++)
5340 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5341 for (; i
< namelen
- 1; i
++)
5345 if ((namelen
& 1) || micro
) {
5346 wsize
= sizeof (txg
);
5352 data
= string_value
;
5356 VERIFY0(zap_count(os
, object
, &count
));
5357 ASSERT(count
!= -1ULL);
5360 * Select an operation: length, lookup, add, update, remove.
5362 i
= ztest_random(5);
5365 tx
= dmu_tx_create(os
);
5366 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5367 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5369 umem_free(od
, sizeof (ztest_od_t
));
5372 bcopy(name
, string_value
, namelen
);
5376 bzero(string_value
, namelen
);
5382 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5384 ASSERT3U(wsize
, ==, zl_wsize
);
5385 ASSERT3U(wc
, ==, zl_wc
);
5387 ASSERT3U(error
, ==, ENOENT
);
5392 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5394 if (data
== string_value
&&
5395 bcmp(name
, data
, namelen
) != 0)
5396 fatal(0, "name '%s' != val '%s' len %d",
5397 name
, data
, namelen
);
5399 ASSERT3U(error
, ==, ENOENT
);
5404 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5405 ASSERT(error
== 0 || error
== EEXIST
);
5409 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5413 error
= zap_remove(os
, object
, name
, tx
);
5414 ASSERT(error
== 0 || error
== ENOENT
);
5421 umem_free(od
, sizeof (ztest_od_t
));
5425 * Commit callback data.
5427 typedef struct ztest_cb_data
{
5428 list_node_t zcd_node
;
5430 int zcd_expected_err
;
5431 boolean_t zcd_added
;
5432 boolean_t zcd_called
;
5436 /* This is the actual commit callback function */
5438 ztest_commit_callback(void *arg
, int error
)
5440 ztest_cb_data_t
*data
= arg
;
5441 uint64_t synced_txg
;
5443 VERIFY(data
!= NULL
);
5444 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5445 VERIFY(!data
->zcd_called
);
5447 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5448 if (data
->zcd_txg
> synced_txg
)
5449 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5450 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5453 data
->zcd_called
= B_TRUE
;
5455 if (error
== ECANCELED
) {
5456 ASSERT0(data
->zcd_txg
);
5457 ASSERT(!data
->zcd_added
);
5460 * The private callback data should be destroyed here, but
5461 * since we are going to check the zcd_called field after
5462 * dmu_tx_abort(), we will destroy it there.
5467 ASSERT(data
->zcd_added
);
5468 ASSERT3U(data
->zcd_txg
, !=, 0);
5470 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5472 /* See if this cb was called more quickly */
5473 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5474 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5476 /* Remove our callback from the list */
5477 list_remove(&zcl
.zcl_callbacks
, data
);
5479 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5481 umem_free(data
, sizeof (ztest_cb_data_t
));
5484 /* Allocate and initialize callback data structure */
5485 static ztest_cb_data_t
*
5486 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5488 ztest_cb_data_t
*cb_data
;
5490 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5492 cb_data
->zcd_txg
= txg
;
5493 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5494 list_link_init(&cb_data
->zcd_node
);
5500 * Commit callback test.
5503 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5505 objset_t
*os
= zd
->zd_os
;
5508 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5509 uint64_t old_txg
, txg
;
5512 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5513 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5515 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5516 umem_free(od
, sizeof (ztest_od_t
));
5520 tx
= dmu_tx_create(os
);
5522 cb_data
[0] = ztest_create_cb_data(os
, 0);
5523 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5525 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5527 /* Every once in a while, abort the transaction on purpose */
5528 if (ztest_random(100) == 0)
5532 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5534 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5536 cb_data
[0]->zcd_txg
= txg
;
5537 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5538 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5542 * It's not a strict requirement to call the registered
5543 * callbacks from inside dmu_tx_abort(), but that's what
5544 * it's supposed to happen in the current implementation
5545 * so we will check for that.
5547 for (i
= 0; i
< 2; i
++) {
5548 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5549 VERIFY(!cb_data
[i
]->zcd_called
);
5554 for (i
= 0; i
< 2; i
++) {
5555 VERIFY(cb_data
[i
]->zcd_called
);
5556 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5559 umem_free(od
, sizeof (ztest_od_t
));
5563 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5564 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5567 * Read existing data to make sure there isn't a future leak.
5569 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5570 &old_txg
, DMU_READ_PREFETCH
));
5573 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5576 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5578 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5581 * Since commit callbacks don't have any ordering requirement and since
5582 * it is theoretically possible for a commit callback to be called
5583 * after an arbitrary amount of time has elapsed since its txg has been
5584 * synced, it is difficult to reliably determine whether a commit
5585 * callback hasn't been called due to high load or due to a flawed
5588 * In practice, we will assume that if after a certain number of txgs a
5589 * commit callback hasn't been called, then most likely there's an
5590 * implementation bug..
5592 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5593 if (tmp_cb
!= NULL
&&
5594 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5595 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5596 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5600 * Let's find the place to insert our callbacks.
5602 * Even though the list is ordered by txg, it is possible for the
5603 * insertion point to not be the end because our txg may already be
5604 * quiescing at this point and other callbacks in the open txg
5605 * (from other objsets) may have sneaked in.
5607 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5608 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5609 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5611 /* Add the 3 callbacks to the list */
5612 for (i
= 0; i
< 3; i
++) {
5614 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5616 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5619 cb_data
[i
]->zcd_added
= B_TRUE
;
5620 VERIFY(!cb_data
[i
]->zcd_called
);
5622 tmp_cb
= cb_data
[i
];
5627 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5631 umem_free(od
, sizeof (ztest_od_t
));
5635 * Visit each object in the dataset. Verify that its properties
5636 * are consistent what was stored in the block tag when it was created,
5637 * and that its unused bonus buffer space has not been overwritten.
5641 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5643 objset_t
*os
= zd
->zd_os
;
5647 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5648 ztest_block_tag_t
*bt
= NULL
;
5649 dmu_object_info_t doi
;
5652 ztest_object_lock(zd
, obj
, RL_READER
);
5653 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5654 ztest_object_unlock(zd
, obj
);
5658 dmu_object_info_from_db(db
, &doi
);
5659 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5660 bt
= ztest_bt_bonus(db
);
5662 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5663 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5664 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5666 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5669 dmu_buf_rele(db
, FTAG
);
5670 ztest_object_unlock(zd
, obj
);
5676 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5678 zfs_prop_t proplist
[] = {
5680 ZFS_PROP_COMPRESSION
,
5686 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5688 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5689 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5690 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5692 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5693 ztest_random_blocksize(), (int)ztest_random(2)));
5695 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5700 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5702 nvlist_t
*props
= NULL
;
5704 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5706 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_AUTOTRIM
, ztest_random(2));
5708 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5710 if (ztest_opts
.zo_verbose
>= 6)
5711 dump_nvlist(props
, 4);
5715 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5719 user_release_one(const char *snapname
, const char *holdname
)
5721 nvlist_t
*snaps
, *holds
;
5724 snaps
= fnvlist_alloc();
5725 holds
= fnvlist_alloc();
5726 fnvlist_add_boolean(holds
, holdname
);
5727 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5728 fnvlist_free(holds
);
5729 error
= dsl_dataset_user_release(snaps
, NULL
);
5730 fnvlist_free(snaps
);
5735 * Test snapshot hold/release and deferred destroy.
5738 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5741 objset_t
*os
= zd
->zd_os
;
5745 char clonename
[100];
5747 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5750 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5752 dmu_objset_name(os
, osname
);
5754 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5756 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5757 (void) snprintf(clonename
, sizeof (clonename
),
5758 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5759 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5762 * Clean up from any previous run.
5764 error
= dsl_destroy_head(clonename
);
5765 if (error
!= ENOENT
)
5767 error
= user_release_one(fullname
, tag
);
5768 if (error
!= ESRCH
&& error
!= ENOENT
)
5770 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5771 if (error
!= ENOENT
)
5775 * Create snapshot, clone it, mark snap for deferred destroy,
5776 * destroy clone, verify snap was also destroyed.
5778 error
= dmu_objset_snapshot_one(osname
, snapname
);
5780 if (error
== ENOSPC
) {
5781 ztest_record_enospc("dmu_objset_snapshot");
5784 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5787 error
= dmu_objset_clone(clonename
, fullname
);
5789 if (error
== ENOSPC
) {
5790 ztest_record_enospc("dmu_objset_clone");
5793 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5796 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5798 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5802 error
= dsl_destroy_head(clonename
);
5804 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5806 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5807 if (error
!= ENOENT
)
5808 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5811 * Create snapshot, add temporary hold, verify that we can't
5812 * destroy a held snapshot, mark for deferred destroy,
5813 * release hold, verify snapshot was destroyed.
5815 error
= dmu_objset_snapshot_one(osname
, snapname
);
5817 if (error
== ENOSPC
) {
5818 ztest_record_enospc("dmu_objset_snapshot");
5821 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5824 holds
= fnvlist_alloc();
5825 fnvlist_add_string(holds
, fullname
, tag
);
5826 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5827 fnvlist_free(holds
);
5829 if (error
== ENOSPC
) {
5830 ztest_record_enospc("dsl_dataset_user_hold");
5833 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5834 fullname
, tag
, error
);
5837 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5838 if (error
!= EBUSY
) {
5839 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5843 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5845 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5849 error
= user_release_one(fullname
, tag
);
5851 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5853 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5856 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5860 * Inject random faults into the on-disk data.
5864 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5866 ztest_shared_t
*zs
= ztest_shared
;
5867 spa_t
*spa
= ztest_spa
;
5871 uint64_t bad
= 0x1990c0ffeedecadeull
;
5876 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5882 boolean_t islog
= B_FALSE
;
5884 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5885 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5887 mutex_enter(&ztest_vdev_lock
);
5890 * Device removal is in progress, fault injection must be disabled
5891 * until it completes and the pool is scrubbed. The fault injection
5892 * strategy for damaging blocks does not take in to account evacuated
5893 * blocks which may have already been damaged.
5895 if (ztest_device_removal_active
) {
5896 mutex_exit(&ztest_vdev_lock
);
5900 maxfaults
= MAXFAULTS(zs
);
5901 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raid_children
;
5902 mirror_save
= zs
->zs_mirrors
;
5903 mutex_exit(&ztest_vdev_lock
);
5905 ASSERT(leaves
>= 1);
5908 * While ztest is running the number of leaves will not change. This
5909 * is critical for the fault injection logic as it determines where
5910 * errors can be safely injected such that they are always repairable.
5912 * When restarting ztest a different number of leaves may be requested
5913 * which will shift the regions to be damaged. This is fine as long
5914 * as the pool has been scrubbed prior to using the new mapping.
5915 * Failure to do can result in non-repairable damage being injected.
5917 if (ztest_pool_scrubbed
== B_FALSE
)
5921 * Grab the name lock as reader. There are some operations
5922 * which don't like to have their vdevs changed while
5923 * they are in progress (i.e. spa_change_guid). Those
5924 * operations will have grabbed the name lock as writer.
5926 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5929 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5931 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5933 if (ztest_random(2) == 0) {
5935 * Inject errors on a normal data device or slog device.
5937 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5938 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5941 * Generate paths to the first leaf in this top-level vdev,
5942 * and to the random leaf we selected. We'll induce transient
5943 * write failures and random online/offline activity on leaf 0,
5944 * and we'll write random garbage to the randomly chosen leaf.
5946 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5947 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5948 top
* leaves
+ zs
->zs_splits
);
5949 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5950 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5951 top
* leaves
+ leaf
);
5953 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5954 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5958 * If the top-level vdev needs to be resilvered
5959 * then we only allow faults on the device that is
5962 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5963 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5964 vd0
->vdev_resilver_txg
!= 0)) {
5966 * Make vd0 explicitly claim to be unreadable,
5967 * or unwriteable, or reach behind its back
5968 * and close the underlying fd. We can do this if
5969 * maxfaults == 0 because we'll fail and reexecute,
5970 * and we can do it if maxfaults >= 2 because we'll
5971 * have enough redundancy. If maxfaults == 1, the
5972 * combination of this with injection of random data
5973 * corruption below exceeds the pool's fault tolerance.
5975 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5977 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5978 (long long)vd0
->vdev_id
, (int)maxfaults
);
5980 if (vf
!= NULL
&& ztest_random(3) == 0) {
5981 (void) close(vf
->vf_file
->f_fd
);
5982 vf
->vf_file
->f_fd
= -1;
5983 } else if (ztest_random(2) == 0) {
5984 vd0
->vdev_cant_read
= B_TRUE
;
5986 vd0
->vdev_cant_write
= B_TRUE
;
5988 guid0
= vd0
->vdev_guid
;
5992 * Inject errors on an l2cache device.
5994 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5996 if (sav
->sav_count
== 0) {
5997 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5998 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6001 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
6002 guid0
= vd0
->vdev_guid
;
6003 (void) strcpy(path0
, vd0
->vdev_path
);
6004 (void) strcpy(pathrand
, vd0
->vdev_path
);
6008 maxfaults
= INT_MAX
; /* no limit on cache devices */
6011 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6012 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6015 * If we can tolerate two or more faults, or we're dealing
6016 * with a slog, randomly online/offline vd0.
6018 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
6019 if (ztest_random(10) < 6) {
6020 int flags
= (ztest_random(2) == 0 ?
6021 ZFS_OFFLINE_TEMPORARY
: 0);
6024 * We have to grab the zs_name_lock as writer to
6025 * prevent a race between offlining a slog and
6026 * destroying a dataset. Offlining the slog will
6027 * grab a reference on the dataset which may cause
6028 * dsl_destroy_head() to fail with EBUSY thus
6029 * leaving the dataset in an inconsistent state.
6032 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6034 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
6037 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6040 * Ideally we would like to be able to randomly
6041 * call vdev_[on|off]line without holding locks
6042 * to force unpredictable failures but the side
6043 * effects of vdev_[on|off]line prevent us from
6044 * doing so. We grab the ztest_vdev_lock here to
6045 * prevent a race between injection testing and
6048 mutex_enter(&ztest_vdev_lock
);
6049 (void) vdev_online(spa
, guid0
, 0, NULL
);
6050 mutex_exit(&ztest_vdev_lock
);
6058 * We have at least single-fault tolerance, so inject data corruption.
6060 fd
= open(pathrand
, O_RDWR
);
6062 if (fd
== -1) /* we hit a gap in the device namespace */
6065 fsize
= lseek(fd
, 0, SEEK_END
);
6067 while (--iters
!= 0) {
6069 * The offset must be chosen carefully to ensure that
6070 * we do not inject a given logical block with errors
6071 * on two different leaf devices, because ZFS can not
6072 * tolerate that (if maxfaults==1).
6074 * To achieve this we divide each leaf device into
6075 * chunks of size (# leaves * SPA_MAXBLOCKSIZE * 4).
6076 * Each chunk is further divided into error-injection
6077 * ranges (can accept errors) and clear ranges (we do
6078 * not inject errors in those). Each error-injection
6079 * range can accept errors only for a single leaf vdev.
6080 * Error-injection ranges are separated by clear ranges.
6082 * For example, with 3 leaves, each chunk looks like:
6083 * 0 to 32M: injection range for leaf 0
6084 * 32M to 64M: clear range - no injection allowed
6085 * 64M to 96M: injection range for leaf 1
6086 * 96M to 128M: clear range - no injection allowed
6087 * 128M to 160M: injection range for leaf 2
6088 * 160M to 192M: clear range - no injection allowed
6090 * Each clear range must be large enough such that a
6091 * single block cannot straddle it. This way a block
6092 * can't be a target in two different injection ranges
6093 * (on different leaf vdevs).
6095 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
6096 (leaves
<< bshift
) + (leaf
<< bshift
) +
6097 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
6100 * Only allow damage to the labels at one end of the vdev.
6102 * If all labels are damaged, the device will be totally
6103 * inaccessible, which will result in loss of data,
6104 * because we also damage (parts of) the other side of
6107 * Additionally, we will always have both an even and an
6108 * odd label, so that we can handle crashes in the
6109 * middle of vdev_config_sync().
6111 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
6115 * The two end labels are stored at the "end" of the disk, but
6116 * the end of the disk (vdev_psize) is aligned to
6117 * sizeof (vdev_label_t).
6119 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
6120 if ((leaf
& 1) == 1 &&
6121 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
6124 mutex_enter(&ztest_vdev_lock
);
6125 if (mirror_save
!= zs
->zs_mirrors
) {
6126 mutex_exit(&ztest_vdev_lock
);
6131 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
6132 fatal(1, "can't inject bad word at 0x%llx in %s",
6135 mutex_exit(&ztest_vdev_lock
);
6137 if (ztest_opts
.zo_verbose
>= 7)
6138 (void) printf("injected bad word into %s,"
6139 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
6144 umem_free(path0
, MAXPATHLEN
);
6145 umem_free(pathrand
, MAXPATHLEN
);
6149 * By design ztest will never inject uncorrectable damage in to the pool.
6150 * Issue a scrub, wait for it to complete, and verify there is never any
6151 * persistent damage.
6153 * Only after a full scrub has been completed is it safe to start injecting
6154 * data corruption. See the comment in zfs_fault_inject().
6157 ztest_scrub_impl(spa_t
*spa
)
6159 int error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
6163 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
6164 txg_wait_synced(spa_get_dsl(spa
), 0);
6166 if (spa_get_errlog_size(spa
) > 0)
6169 ztest_pool_scrubbed
= B_TRUE
;
6179 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6181 spa_t
*spa
= ztest_spa
;
6185 * Scrub in progress by device removal.
6187 if (ztest_device_removal_active
)
6191 * Start a scrub, wait a moment, then force a restart.
6193 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6194 (void) poll(NULL
, 0, 100);
6196 error
= ztest_scrub_impl(spa
);
6203 * Change the guid for the pool.
6207 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6209 spa_t
*spa
= ztest_spa
;
6210 uint64_t orig
, load
;
6213 if (ztest_opts
.zo_mmp_test
)
6216 orig
= spa_guid(spa
);
6217 load
= spa_load_guid(spa
);
6219 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6220 error
= spa_change_guid(spa
);
6221 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6226 if (ztest_opts
.zo_verbose
>= 4) {
6227 (void) printf("Changed guid old %llu -> %llu\n",
6228 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6231 VERIFY3U(orig
, !=, spa_guid(spa
));
6232 VERIFY3U(load
, ==, spa_load_guid(spa
));
6236 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6238 hrtime_t end
= gethrtime() + NANOSEC
;
6240 while (gethrtime() <= end
) {
6241 int run_count
= 100;
6243 struct abd
*abd_data
, *abd_meta
;
6248 zio_cksum_t zc_ref_byteswap
;
6250 size
= ztest_random_blocksize();
6252 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6253 abd_data
= abd_alloc(size
, B_FALSE
);
6254 abd_meta
= abd_alloc(size
, B_TRUE
);
6256 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6257 *ptr
= ztest_random(UINT_MAX
);
6259 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6260 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6262 VERIFY0(fletcher_4_impl_set("scalar"));
6263 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6264 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6266 VERIFY0(fletcher_4_impl_set("cycle"));
6267 while (run_count
-- > 0) {
6269 zio_cksum_t zc_byteswap
;
6271 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6272 fletcher_4_native(buf
, size
, NULL
, &zc
);
6274 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6275 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6276 sizeof (zc_byteswap
)));
6278 /* Test ABD - data */
6279 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6281 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6283 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6284 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6285 sizeof (zc_byteswap
)));
6287 /* Test ABD - metadata */
6288 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6290 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6292 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6293 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6294 sizeof (zc_byteswap
)));
6298 umem_free(buf
, size
);
6305 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6312 zio_cksum_t zc_ref_bswap
;
6314 hrtime_t end
= gethrtime() + NANOSEC
;
6316 while (gethrtime() <= end
) {
6317 int run_count
= 100;
6319 size
= ztest_random_blocksize();
6320 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6322 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6323 *ptr
= ztest_random(UINT_MAX
);
6325 VERIFY0(fletcher_4_impl_set("scalar"));
6326 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6327 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6329 VERIFY0(fletcher_4_impl_set("cycle"));
6331 while (run_count
-- > 0) {
6333 zio_cksum_t zc_bswap
;
6336 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6337 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6339 while (pos
< size
) {
6340 size_t inc
= 64 * ztest_random(size
/ 67);
6341 /* sometimes add few bytes to test non-simd */
6342 if (ztest_random(100) < 10)
6343 inc
+= P2ALIGN(ztest_random(64),
6346 if (inc
> (size
- pos
))
6349 fletcher_4_incremental_native(buf
+ pos
, inc
,
6351 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6357 VERIFY3U(pos
, ==, size
);
6359 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6360 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6363 * verify if incremental on the whole buffer is
6364 * equivalent to non-incremental version
6366 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6367 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6369 fletcher_4_incremental_native(buf
, size
, &zc
);
6370 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6372 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6373 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6376 umem_free(buf
, size
);
6381 ztest_check_path(char *path
)
6384 /* return true on success */
6385 return (!stat(path
, &s
));
6389 ztest_get_zdb_bin(char *bin
, int len
)
6393 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6394 * let popen to search through PATH.
6396 if ((zdb_path
= getenv("ZDB_PATH"))) {
6397 strlcpy(bin
, zdb_path
, len
); /* In env */
6398 if (!ztest_check_path(bin
)) {
6399 ztest_dump_core
= 0;
6400 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6405 VERIFY(realpath(getexecname(), bin
) != NULL
);
6406 if (strstr(bin
, "/ztest/")) {
6407 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6408 strcat(bin
, "/zdb/zdb");
6409 if (ztest_check_path(bin
))
6416 ztest_random_concrete_vdev_leaf(vdev_t
*vd
)
6421 if (vd
->vdev_children
== 0)
6424 vdev_t
*eligible
[vd
->vdev_children
];
6425 int eligible_idx
= 0, i
;
6426 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6427 vdev_t
*cvd
= vd
->vdev_child
[i
];
6428 if (cvd
->vdev_top
->vdev_removing
)
6430 if (cvd
->vdev_children
> 0 ||
6431 (vdev_is_concrete(cvd
) && !cvd
->vdev_detached
)) {
6432 eligible
[eligible_idx
++] = cvd
;
6435 VERIFY(eligible_idx
> 0);
6437 uint64_t child_no
= ztest_random(eligible_idx
);
6438 return (ztest_random_concrete_vdev_leaf(eligible
[child_no
]));
6443 ztest_initialize(ztest_ds_t
*zd
, uint64_t id
)
6445 spa_t
*spa
= ztest_spa
;
6448 mutex_enter(&ztest_vdev_lock
);
6450 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
6452 /* Random leaf vdev */
6453 vdev_t
*rand_vd
= ztest_random_concrete_vdev_leaf(spa
->spa_root_vdev
);
6454 if (rand_vd
== NULL
) {
6455 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6456 mutex_exit(&ztest_vdev_lock
);
6461 * The random vdev we've selected may change as soon as we
6462 * drop the spa_config_lock. We create local copies of things
6463 * we're interested in.
6465 uint64_t guid
= rand_vd
->vdev_guid
;
6466 char *path
= strdup(rand_vd
->vdev_path
);
6467 boolean_t active
= rand_vd
->vdev_initialize_thread
!= NULL
;
6469 zfs_dbgmsg("vd %px, guid %llu", rand_vd
, guid
);
6470 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6472 uint64_t cmd
= ztest_random(POOL_INITIALIZE_FUNCS
);
6474 nvlist_t
*vdev_guids
= fnvlist_alloc();
6475 nvlist_t
*vdev_errlist
= fnvlist_alloc();
6476 fnvlist_add_uint64(vdev_guids
, path
, guid
);
6477 error
= spa_vdev_initialize(spa
, vdev_guids
, cmd
, vdev_errlist
);
6478 fnvlist_free(vdev_guids
);
6479 fnvlist_free(vdev_errlist
);
6482 case POOL_INITIALIZE_CANCEL
:
6483 if (ztest_opts
.zo_verbose
>= 4) {
6484 (void) printf("Cancel initialize %s", path
);
6486 (void) printf(" failed (no initialize active)");
6487 (void) printf("\n");
6490 case POOL_INITIALIZE_START
:
6491 if (ztest_opts
.zo_verbose
>= 4) {
6492 (void) printf("Start initialize %s", path
);
6493 if (active
&& error
== 0)
6494 (void) printf(" failed (already active)");
6495 else if (error
!= 0)
6496 (void) printf(" failed (error %d)", error
);
6497 (void) printf("\n");
6500 case POOL_INITIALIZE_SUSPEND
:
6501 if (ztest_opts
.zo_verbose
>= 4) {
6502 (void) printf("Suspend initialize %s", path
);
6504 (void) printf(" failed (no initialize active)");
6505 (void) printf("\n");
6510 mutex_exit(&ztest_vdev_lock
);
6515 ztest_trim(ztest_ds_t
*zd
, uint64_t id
)
6517 spa_t
*spa
= ztest_spa
;
6520 mutex_enter(&ztest_vdev_lock
);
6522 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
6524 /* Random leaf vdev */
6525 vdev_t
*rand_vd
= ztest_random_concrete_vdev_leaf(spa
->spa_root_vdev
);
6526 if (rand_vd
== NULL
) {
6527 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6528 mutex_exit(&ztest_vdev_lock
);
6533 * The random vdev we've selected may change as soon as we
6534 * drop the spa_config_lock. We create local copies of things
6535 * we're interested in.
6537 uint64_t guid
= rand_vd
->vdev_guid
;
6538 char *path
= strdup(rand_vd
->vdev_path
);
6539 boolean_t active
= rand_vd
->vdev_trim_thread
!= NULL
;
6541 zfs_dbgmsg("vd %p, guid %llu", rand_vd
, guid
);
6542 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6544 uint64_t cmd
= ztest_random(POOL_TRIM_FUNCS
);
6545 uint64_t rate
= 1 << ztest_random(30);
6546 boolean_t partial
= (ztest_random(5) > 0);
6547 boolean_t secure
= (ztest_random(5) > 0);
6549 nvlist_t
*vdev_guids
= fnvlist_alloc();
6550 nvlist_t
*vdev_errlist
= fnvlist_alloc();
6551 fnvlist_add_uint64(vdev_guids
, path
, guid
);
6552 error
= spa_vdev_trim(spa
, vdev_guids
, cmd
, rate
, partial
,
6553 secure
, vdev_errlist
);
6554 fnvlist_free(vdev_guids
);
6555 fnvlist_free(vdev_errlist
);
6558 case POOL_TRIM_CANCEL
:
6559 if (ztest_opts
.zo_verbose
>= 4) {
6560 (void) printf("Cancel TRIM %s", path
);
6562 (void) printf(" failed (no TRIM active)");
6563 (void) printf("\n");
6566 case POOL_TRIM_START
:
6567 if (ztest_opts
.zo_verbose
>= 4) {
6568 (void) printf("Start TRIM %s", path
);
6569 if (active
&& error
== 0)
6570 (void) printf(" failed (already active)");
6571 else if (error
!= 0)
6572 (void) printf(" failed (error %d)", error
);
6573 (void) printf("\n");
6576 case POOL_TRIM_SUSPEND
:
6577 if (ztest_opts
.zo_verbose
>= 4) {
6578 (void) printf("Suspend TRIM %s", path
);
6580 (void) printf(" failed (no TRIM active)");
6581 (void) printf("\n");
6586 mutex_exit(&ztest_vdev_lock
);
6590 * Verify pool integrity by running zdb.
6593 ztest_run_zdb(char *pool
)
6599 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6602 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6603 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6604 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6606 ztest_get_zdb_bin(bin
, len
);
6609 "%s -bcc%s%s -G -d -Y -e -y -p %s %s",
6611 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6612 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6616 if (ztest_opts
.zo_verbose
>= 5)
6617 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6619 fp
= popen(zdb
, "r");
6621 while (fgets(zbuf
, 1024, fp
) != NULL
)
6622 if (ztest_opts
.zo_verbose
>= 3)
6623 (void) printf("%s", zbuf
);
6625 status
= pclose(fp
);
6630 ztest_dump_core
= 0;
6631 if (WIFEXITED(status
))
6632 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6634 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6636 umem_free(bin
, len
);
6637 umem_free(zdb
, len
);
6638 umem_free(zbuf
, 1024);
6642 ztest_walk_pool_directory(char *header
)
6646 if (ztest_opts
.zo_verbose
>= 6)
6647 (void) printf("%s\n", header
);
6649 mutex_enter(&spa_namespace_lock
);
6650 while ((spa
= spa_next(spa
)) != NULL
)
6651 if (ztest_opts
.zo_verbose
>= 6)
6652 (void) printf("\t%s\n", spa_name(spa
));
6653 mutex_exit(&spa_namespace_lock
);
6657 ztest_spa_import_export(char *oldname
, char *newname
)
6659 nvlist_t
*config
, *newconfig
;
6664 if (ztest_opts
.zo_verbose
>= 4) {
6665 (void) printf("import/export: old = %s, new = %s\n",
6670 * Clean up from previous runs.
6672 (void) spa_destroy(newname
);
6675 * Get the pool's configuration and guid.
6677 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6680 * Kick off a scrub to tickle scrub/export races.
6682 if (ztest_random(2) == 0)
6683 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6685 pool_guid
= spa_guid(spa
);
6686 spa_close(spa
, FTAG
);
6688 ztest_walk_pool_directory("pools before export");
6693 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6695 ztest_walk_pool_directory("pools after export");
6700 newconfig
= spa_tryimport(config
);
6701 ASSERT(newconfig
!= NULL
);
6702 nvlist_free(newconfig
);
6705 * Import it under the new name.
6707 error
= spa_import(newname
, config
, NULL
, 0);
6709 dump_nvlist(config
, 0);
6710 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6711 oldname
, newname
, error
);
6714 ztest_walk_pool_directory("pools after import");
6717 * Try to import it again -- should fail with EEXIST.
6719 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6722 * Try to import it under a different name -- should fail with EEXIST.
6724 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6727 * Verify that the pool is no longer visible under the old name.
6729 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6732 * Verify that we can open and close the pool using the new name.
6734 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6735 ASSERT(pool_guid
== spa_guid(spa
));
6736 spa_close(spa
, FTAG
);
6738 nvlist_free(config
);
6742 ztest_resume(spa_t
*spa
)
6744 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6745 (void) printf("resuming from suspended state\n");
6746 spa_vdev_state_enter(spa
, SCL_NONE
);
6747 vdev_clear(spa
, NULL
);
6748 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6749 (void) zio_resume(spa
);
6753 ztest_resume_thread(void *arg
)
6757 while (!ztest_exiting
) {
6758 if (spa_suspended(spa
))
6760 (void) poll(NULL
, 0, 100);
6763 * Periodically change the zfs_compressed_arc_enabled setting.
6765 if (ztest_random(10) == 0)
6766 zfs_compressed_arc_enabled
= ztest_random(2);
6769 * Periodically change the zfs_abd_scatter_enabled setting.
6771 if (ztest_random(10) == 0)
6772 zfs_abd_scatter_enabled
= ztest_random(2);
6779 ztest_deadman_thread(void *arg
)
6781 ztest_shared_t
*zs
= arg
;
6782 spa_t
*spa
= ztest_spa
;
6783 hrtime_t delay
, overdue
, last_run
= gethrtime();
6785 delay
= (zs
->zs_thread_stop
- zs
->zs_thread_start
) +
6786 MSEC2NSEC(zfs_deadman_synctime_ms
);
6788 while (!ztest_exiting
) {
6790 * Wait for the delay timer while checking occasionally
6791 * if we should stop.
6793 if (gethrtime() < last_run
+ delay
) {
6794 (void) poll(NULL
, 0, 1000);
6799 * If the pool is suspended then fail immediately. Otherwise,
6800 * check to see if the pool is making any progress. If
6801 * vdev_deadman() discovers that there hasn't been any recent
6802 * I/Os then it will end up aborting the tests.
6804 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6805 fatal(0, "aborting test after %llu seconds because "
6806 "pool has transitioned to a suspended state.",
6807 zfs_deadman_synctime_ms
/ 1000);
6809 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6812 * If the process doesn't complete within a grace period of
6813 * zfs_deadman_synctime_ms over the expected finish time,
6814 * then it may be hung and is terminated.
6816 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6817 if (gethrtime() > overdue
) {
6818 fatal(0, "aborting test after %llu seconds because "
6819 "the process is overdue for termination.",
6820 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6823 (void) printf("ztest has been running for %lld seconds\n",
6824 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6826 last_run
= gethrtime();
6827 delay
= MSEC2NSEC(zfs_deadman_checktime_ms
);
6834 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6836 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6837 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6838 hrtime_t functime
= gethrtime();
6841 for (i
= 0; i
< zi
->zi_iters
; i
++)
6842 zi
->zi_func(zd
, id
);
6844 functime
= gethrtime() - functime
;
6846 atomic_add_64(&zc
->zc_count
, 1);
6847 atomic_add_64(&zc
->zc_time
, functime
);
6849 if (ztest_opts
.zo_verbose
>= 4)
6850 (void) printf("%6.2f sec in %s\n",
6851 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6855 ztest_thread(void *arg
)
6858 uint64_t id
= (uintptr_t)arg
;
6859 ztest_shared_t
*zs
= ztest_shared
;
6863 ztest_shared_callstate_t
*zc
;
6865 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6867 * See if it's time to force a crash.
6869 if (now
> zs
->zs_thread_kill
)
6873 * If we're getting ENOSPC with some regularity, stop.
6875 if (zs
->zs_enospc_count
> 10)
6879 * Pick a random function to execute.
6881 rand
= ztest_random(ZTEST_FUNCS
);
6882 zi
= &ztest_info
[rand
];
6883 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6884 call_next
= zc
->zc_next
;
6886 if (now
>= call_next
&&
6887 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6888 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6889 ztest_execute(rand
, zi
, id
);
6897 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6899 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6903 ztest_dataset_destroy(int d
)
6905 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6908 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6910 if (ztest_opts
.zo_verbose
>= 3)
6911 (void) printf("Destroying %s to free up space\n", name
);
6914 * Cleanup any non-standard clones and snapshots. In general,
6915 * ztest thread t operates on dataset (t % zopt_datasets),
6916 * so there may be more than one thing to clean up.
6918 for (t
= d
; t
< ztest_opts
.zo_threads
;
6919 t
+= ztest_opts
.zo_datasets
)
6920 ztest_dsl_dataset_cleanup(name
, t
);
6922 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6923 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6927 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6929 uint64_t usedobjs
, dirobjs
, scratch
;
6932 * ZTEST_DIROBJ is the object directory for the entire dataset.
6933 * Therefore, the number of objects in use should equal the
6934 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6935 * If not, we have an object leak.
6937 * Note that we can only check this in ztest_dataset_open(),
6938 * when the open-context and syncing-context values agree.
6939 * That's because zap_count() returns the open-context value,
6940 * while dmu_objset_space() returns the rootbp fill count.
6942 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6943 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6944 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6948 ztest_dataset_open(int d
)
6950 ztest_ds_t
*zd
= &ztest_ds
[d
];
6951 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6954 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6957 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6959 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6961 error
= ztest_dataset_create(name
);
6962 if (error
== ENOSPC
) {
6963 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6964 ztest_record_enospc(FTAG
);
6967 ASSERT(error
== 0 || error
== EEXIST
);
6969 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6971 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6973 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6975 zilog
= zd
->zd_zilog
;
6977 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6978 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6979 fatal(0, "missing log records: claimed %llu < committed %llu",
6980 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6982 ztest_dataset_dirobj_verify(zd
);
6984 zil_replay(os
, zd
, ztest_replay_vector
);
6986 ztest_dataset_dirobj_verify(zd
);
6988 if (ztest_opts
.zo_verbose
>= 6)
6989 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6991 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6992 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6993 (u_longlong_t
)zilog
->zl_replaying_seq
);
6995 zilog
= zil_open(os
, ztest_get_data
);
6997 if (zilog
->zl_replaying_seq
!= 0 &&
6998 zilog
->zl_replaying_seq
< committed_seq
)
6999 fatal(0, "missing log records: replayed %llu < committed %llu",
7000 zilog
->zl_replaying_seq
, committed_seq
);
7006 ztest_dataset_close(int d
)
7008 ztest_ds_t
*zd
= &ztest_ds
[d
];
7010 zil_close(zd
->zd_zilog
);
7011 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
7018 ztest_replay_zil_cb(const char *name
, void *arg
)
7023 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_ANY
, B_TRUE
,
7024 B_TRUE
, FTAG
, &os
));
7026 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
7028 ztest_zd_init(zdtmp
, NULL
, os
);
7029 zil_replay(os
, zdtmp
, ztest_replay_vector
);
7030 ztest_zd_fini(zdtmp
);
7032 if (dmu_objset_zil(os
)->zl_parse_lr_count
!= 0 &&
7033 ztest_opts
.zo_verbose
>= 6) {
7034 zilog_t
*zilog
= dmu_objset_zil(os
);
7036 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
7038 (u_longlong_t
)zilog
->zl_parse_blk_count
,
7039 (u_longlong_t
)zilog
->zl_parse_lr_count
,
7040 (u_longlong_t
)zilog
->zl_replaying_seq
);
7043 umem_free(zdtmp
, sizeof (ztest_ds_t
));
7045 dmu_objset_disown(os
, B_TRUE
, FTAG
);
7052 ztest_ds_t
*zd
= &ztest_ds
[0];
7056 if (ztest_opts
.zo_verbose
>= 3)
7057 (void) printf("testing spa_freeze()...\n");
7059 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7060 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7061 VERIFY3U(0, ==, ztest_dataset_open(0));
7065 * Force the first log block to be transactionally allocated.
7066 * We have to do this before we freeze the pool -- otherwise
7067 * the log chain won't be anchored.
7069 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
7070 ztest_dmu_object_alloc_free(zd
, 0);
7071 zil_commit(zd
->zd_zilog
, 0);
7074 txg_wait_synced(spa_get_dsl(spa
), 0);
7077 * Freeze the pool. This stops spa_sync() from doing anything,
7078 * so that the only way to record changes from now on is the ZIL.
7083 * Because it is hard to predict how much space a write will actually
7084 * require beforehand, we leave ourselves some fudge space to write over
7087 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
7090 * Run tests that generate log records but don't alter the pool config
7091 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
7092 * We do a txg_wait_synced() after each iteration to force the txg
7093 * to increase well beyond the last synced value in the uberblock.
7094 * The ZIL should be OK with that.
7096 * Run a random number of times less than zo_maxloops and ensure we do
7097 * not run out of space on the pool.
7099 while (ztest_random(10) != 0 &&
7100 numloops
++ < ztest_opts
.zo_maxloops
&&
7101 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
7103 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
7104 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
7105 ztest_io(zd
, od
.od_object
,
7106 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
7107 txg_wait_synced(spa_get_dsl(spa
), 0);
7111 * Commit all of the changes we just generated.
7113 zil_commit(zd
->zd_zilog
, 0);
7114 txg_wait_synced(spa_get_dsl(spa
), 0);
7117 * Close our dataset and close the pool.
7119 ztest_dataset_close(0);
7120 spa_close(spa
, FTAG
);
7124 * Open and close the pool and dataset to induce log replay.
7126 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7127 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7128 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
7129 VERIFY3U(0, ==, ztest_dataset_open(0));
7131 txg_wait_synced(spa_get_dsl(spa
), 0);
7132 ztest_dataset_close(0);
7133 ztest_reguid(NULL
, 0);
7135 spa_close(spa
, FTAG
);
7140 ztest_import_impl(ztest_shared_t
*zs
)
7142 importargs_t args
= { 0 };
7143 nvlist_t
*cfg
= NULL
;
7145 char *searchdirs
[nsearch
];
7146 int flags
= ZFS_IMPORT_MISSING_LOG
;
7148 searchdirs
[0] = ztest_opts
.zo_dir
;
7149 args
.paths
= nsearch
;
7150 args
.path
= searchdirs
;
7151 args
.can_be_active
= B_FALSE
;
7153 VERIFY0(zpool_find_config(NULL
, ztest_opts
.zo_pool
, &cfg
, &args
,
7154 &libzpool_config_ops
));
7155 VERIFY0(spa_import(ztest_opts
.zo_pool
, cfg
, NULL
, flags
));
7159 * Import a storage pool with the given name.
7162 ztest_import(ztest_shared_t
*zs
)
7166 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7167 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7168 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7170 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7172 ztest_import_impl(zs
);
7174 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7175 zs
->zs_metaslab_sz
=
7176 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7177 spa_close(spa
, FTAG
);
7181 if (!ztest_opts
.zo_mmp_test
) {
7182 ztest_run_zdb(ztest_opts
.zo_pool
);
7184 ztest_run_zdb(ztest_opts
.zo_pool
);
7187 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7188 mutex_destroy(&ztest_vdev_lock
);
7189 mutex_destroy(&ztest_checkpoint_lock
);
7193 * Kick off threads to run tests on all datasets in parallel.
7196 ztest_run(ztest_shared_t
*zs
)
7200 kthread_t
*resume_thread
, *deadman_thread
;
7201 kthread_t
**run_threads
;
7206 ztest_exiting
= B_FALSE
;
7209 * Initialize parent/child shared state.
7211 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7212 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7213 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7215 zs
->zs_thread_start
= gethrtime();
7216 zs
->zs_thread_stop
=
7217 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
7218 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
7219 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
7220 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
7221 zs
->zs_thread_kill
-=
7222 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
7225 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7227 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
7228 offsetof(ztest_cb_data_t
, zcd_node
));
7231 * Open our pool. It may need to be imported first depending on
7232 * what tests were running when the previous pass was terminated.
7234 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7235 error
= spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
);
7237 VERIFY3S(error
, ==, ENOENT
);
7238 ztest_import_impl(zs
);
7239 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7240 zs
->zs_metaslab_sz
=
7241 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7244 metaslab_preload_limit
= ztest_random(20) + 1;
7247 VERIFY0(vdev_raidz_impl_set("cycle"));
7249 dmu_objset_stats_t dds
;
7250 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
7251 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
7252 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
7253 dmu_objset_fast_stat(os
, &dds
);
7254 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
7255 zs
->zs_guid
= dds
.dds_guid
;
7256 dmu_objset_disown(os
, B_TRUE
, FTAG
);
7259 * Create a thread to periodically resume suspended I/O.
7261 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
7262 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
7265 * Create a deadman thread and set to panic if we hang.
7267 deadman_thread
= thread_create(NULL
, 0, ztest_deadman_thread
,
7268 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
7270 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
7273 * Verify that we can safely inquire about any object,
7274 * whether it's allocated or not. To make it interesting,
7275 * we probe a 5-wide window around each power of two.
7276 * This hits all edge cases, including zero and the max.
7278 for (t
= 0; t
< 64; t
++) {
7279 for (d
= -5; d
<= 5; d
++) {
7280 error
= dmu_object_info(spa
->spa_meta_objset
,
7281 (1ULL << t
) + d
, NULL
);
7282 ASSERT(error
== 0 || error
== ENOENT
||
7288 * If we got any ENOSPC errors on the previous run, destroy something.
7290 if (zs
->zs_enospc_count
!= 0) {
7291 int d
= ztest_random(ztest_opts
.zo_datasets
);
7292 ztest_dataset_destroy(d
);
7294 zs
->zs_enospc_count
= 0;
7297 * If we were in the middle of ztest_device_removal() and were killed
7298 * we need to ensure the removal and scrub complete before running
7299 * any tests that check ztest_device_removal_active. The removal will
7300 * be restarted automatically when the spa is opened, but we need to
7301 * initiate the scrub manually if it is not already in progress. Note
7302 * that we always run the scrub whenever an indirect vdev exists
7303 * because we have no way of knowing for sure if ztest_device_removal()
7304 * fully completed its scrub before the pool was reimported.
7306 if (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
||
7307 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7308 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
7309 txg_wait_synced(spa_get_dsl(spa
), 0);
7311 error
= ztest_scrub_impl(spa
);
7317 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
7320 if (ztest_opts
.zo_verbose
>= 4)
7321 (void) printf("starting main threads...\n");
7324 * Replay all logs of all datasets in the pool. This is primarily for
7325 * temporary datasets which wouldn't otherwise get replayed, which
7326 * can trigger failures when attempting to offline a SLOG in
7327 * ztest_fault_inject().
7329 (void) dmu_objset_find(ztest_opts
.zo_pool
, ztest_replay_zil_cb
,
7330 NULL
, DS_FIND_CHILDREN
);
7333 * Kick off all the tests that run in parallel.
7335 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7336 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
7337 umem_free(run_threads
, ztest_opts
.zo_threads
*
7338 sizeof (kthread_t
*));
7342 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
7343 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
7348 * Wait for all of the tests to complete.
7350 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++)
7351 VERIFY0(thread_join(run_threads
[t
]));
7354 * Close all datasets. This must be done after all the threads
7355 * are joined so we can be sure none of the datasets are in-use
7356 * by any of the threads.
7358 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7359 if (t
< ztest_opts
.zo_datasets
)
7360 ztest_dataset_close(t
);
7363 txg_wait_synced(spa_get_dsl(spa
), 0);
7365 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
7366 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
7368 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
7370 /* Kill the resume and deadman threads */
7371 ztest_exiting
= B_TRUE
;
7372 VERIFY0(thread_join(resume_thread
));
7373 VERIFY0(thread_join(deadman_thread
));
7377 * Right before closing the pool, kick off a bunch of async I/O;
7378 * spa_close() should wait for it to complete.
7380 for (object
= 1; object
< 50; object
++) {
7381 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
7382 ZIO_PRIORITY_SYNC_READ
);
7385 /* Verify that at least one commit cb was called in a timely fashion */
7386 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
7387 VERIFY0(zc_min_txg_delay
);
7389 spa_close(spa
, FTAG
);
7392 * Verify that we can loop over all pools.
7394 mutex_enter(&spa_namespace_lock
);
7395 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
7396 if (ztest_opts
.zo_verbose
> 3)
7397 (void) printf("spa_next: found %s\n", spa_name(spa
));
7398 mutex_exit(&spa_namespace_lock
);
7401 * Verify that we can export the pool and reimport it under a
7404 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7405 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7406 (void) snprintf(name
, sizeof (name
), "%s_import",
7407 ztest_opts
.zo_pool
);
7408 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7409 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7414 list_destroy(&zcl
.zcl_callbacks
);
7415 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7416 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7417 mutex_destroy(&ztest_vdev_lock
);
7418 mutex_destroy(&ztest_checkpoint_lock
);
7422 print_time(hrtime_t t
, char *timebuf
)
7424 hrtime_t s
= t
/ NANOSEC
;
7425 hrtime_t m
= s
/ 60;
7426 hrtime_t h
= m
/ 60;
7427 hrtime_t d
= h
/ 24;
7436 (void) sprintf(timebuf
,
7437 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7439 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7441 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7443 (void) sprintf(timebuf
, "%llus", s
);
7447 make_random_props(void)
7451 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7453 if (ztest_random(2) == 0)
7456 VERIFY0(nvlist_add_uint64(props
,
7457 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7463 * Create a storage pool with the given name and initial vdev size.
7464 * Then test spa_freeze() functionality.
7467 ztest_init(ztest_shared_t
*zs
)
7470 nvlist_t
*nvroot
, *props
;
7473 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7474 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7475 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7477 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7480 * Create the storage pool.
7482 (void) spa_destroy(ztest_opts
.zo_pool
);
7483 ztest_shared
->zs_vdev_next_leaf
= 0;
7485 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7486 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7487 NULL
, ztest_opts
.zo_raid_children
, zs
->zs_mirrors
, 1);
7488 props
= make_random_props();
7491 * We don't expect the pool to suspend unless maxfaults == 0,
7492 * in which case ztest_fault_inject() temporarily takes away
7493 * the only valid replica.
7495 VERIFY0(nvlist_add_uint64(props
,
7496 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7497 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7499 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7503 * 75% chance of using the log space map feature. We want ztest
7504 * to exercise both the code paths that use the log space map
7505 * feature and the ones that don't.
7507 if (i
== SPA_FEATURE_LOG_SPACEMAP
&& ztest_random(4) == 0)
7510 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7511 spa_feature_table
[i
].fi_uname
));
7512 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7516 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7517 nvlist_free(nvroot
);
7520 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7521 zs
->zs_metaslab_sz
=
7522 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7523 spa_close(spa
, FTAG
);
7527 if (!ztest_opts
.zo_mmp_test
) {
7528 ztest_run_zdb(ztest_opts
.zo_pool
);
7530 ztest_run_zdb(ztest_opts
.zo_pool
);
7533 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7534 mutex_destroy(&ztest_vdev_lock
);
7535 mutex_destroy(&ztest_checkpoint_lock
);
7541 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7543 ztest_fd_data
= mkstemp(ztest_name_data
);
7544 ASSERT3S(ztest_fd_data
, >=, 0);
7545 (void) unlink(ztest_name_data
);
7549 shared_data_size(ztest_shared_hdr_t
*hdr
)
7553 size
= hdr
->zh_hdr_size
;
7554 size
+= hdr
->zh_opts_size
;
7555 size
+= hdr
->zh_size
;
7556 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7557 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7566 ztest_shared_hdr_t
*hdr
;
7568 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7569 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7570 ASSERT(hdr
!= MAP_FAILED
);
7572 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7574 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7575 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7576 hdr
->zh_size
= sizeof (ztest_shared_t
);
7577 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7578 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7579 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7580 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7582 size
= shared_data_size(hdr
);
7583 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7585 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7592 ztest_shared_hdr_t
*hdr
;
7595 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7596 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7597 ASSERT(hdr
!= MAP_FAILED
);
7599 size
= shared_data_size(hdr
);
7601 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7602 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7603 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7604 ASSERT(hdr
!= MAP_FAILED
);
7605 buf
= (uint8_t *)hdr
;
7607 offset
= hdr
->zh_hdr_size
;
7608 ztest_shared_opts
= (void *)&buf
[offset
];
7609 offset
+= hdr
->zh_opts_size
;
7610 ztest_shared
= (void *)&buf
[offset
];
7611 offset
+= hdr
->zh_size
;
7612 ztest_shared_callstate
= (void *)&buf
[offset
];
7613 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7614 ztest_shared_ds
= (void *)&buf
[offset
];
7618 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7622 char *cmdbuf
= NULL
;
7627 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7628 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7633 fatal(1, "fork failed");
7635 if (pid
== 0) { /* child */
7636 char *emptyargv
[2] = { cmd
, NULL
};
7637 char fd_data_str
[12];
7639 struct rlimit rl
= { 1024, 1024 };
7640 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7642 (void) close(ztest_fd_rand
);
7643 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7644 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7646 (void) enable_extended_FILE_stdio(-1, -1);
7647 if (libpath
!= NULL
)
7648 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7649 (void) execv(cmd
, emptyargv
);
7650 ztest_dump_core
= B_FALSE
;
7651 fatal(B_TRUE
, "exec failed: %s", cmd
);
7654 if (cmdbuf
!= NULL
) {
7655 umem_free(cmdbuf
, MAXPATHLEN
);
7659 while (waitpid(pid
, &status
, 0) != pid
)
7661 if (statusp
!= NULL
)
7664 if (WIFEXITED(status
)) {
7665 if (WEXITSTATUS(status
) != 0) {
7666 (void) fprintf(stderr
, "child exited with code %d\n",
7667 WEXITSTATUS(status
));
7671 } else if (WIFSIGNALED(status
)) {
7672 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7673 (void) fprintf(stderr
, "child died with signal %d\n",
7679 (void) fprintf(stderr
, "something strange happened to child\n");
7686 ztest_run_init(void)
7690 ztest_shared_t
*zs
= ztest_shared
;
7693 * Blow away any existing copy of zpool.cache
7695 (void) remove(spa_config_path
);
7697 if (ztest_opts
.zo_init
== 0) {
7698 if (ztest_opts
.zo_verbose
>= 1)
7699 (void) printf("Importing pool %s\n",
7700 ztest_opts
.zo_pool
);
7706 * Create and initialize our storage pool.
7708 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7709 bzero(zs
, sizeof (ztest_shared_t
));
7710 if (ztest_opts
.zo_verbose
>= 3 &&
7711 ztest_opts
.zo_init
!= 1) {
7712 (void) printf("ztest_init(), pass %d\n", i
);
7719 main(int argc
, char **argv
)
7727 ztest_shared_callstate_t
*zc
;
7729 char numbuf
[NN_NUMBUF_SZ
];
7733 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7734 struct sigaction action
;
7736 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7738 dprintf_setup(&argc
, argv
);
7739 zfs_deadman_synctime_ms
= 300000;
7740 zfs_deadman_checktime_ms
= 30000;
7742 * As two-word space map entries may not come up often (especially
7743 * if pool and vdev sizes are small) we want to force at least some
7744 * of them so the feature get tested.
7746 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7749 * Verify that even extensively damaged split blocks with many
7750 * segments can be reconstructed in a reasonable amount of time
7751 * when reconstruction is known to be possible.
7753 * Note: the lower this value is, the more damage we inflict, and
7754 * the more time ztest spends in recovering that damage. We chose
7755 * to induce damage 1/100th of the time so recovery is tested but
7756 * not so frequently that ztest doesn't get to test other code paths.
7758 zfs_reconstruct_indirect_damage_fraction
= 100;
7760 action
.sa_handler
= sig_handler
;
7761 sigemptyset(&action
.sa_mask
);
7762 action
.sa_flags
= 0;
7764 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7765 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7770 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7771 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7777 * Force random_get_bytes() to use /dev/urandom in order to prevent
7778 * ztest from needlessly depleting the system entropy pool.
7780 random_path
= "/dev/urandom";
7781 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7782 ASSERT3S(ztest_fd_rand
, >=, 0);
7785 process_options(argc
, argv
);
7790 bcopy(&ztest_opts
, ztest_shared_opts
,
7791 sizeof (*ztest_shared_opts
));
7793 ztest_fd_data
= atoi(fd_data_str
);
7795 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7797 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7799 /* Override location of zpool.cache */
7800 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7801 ztest_opts
.zo_dir
) != -1);
7803 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7808 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7809 metaslab_df_alloc_threshold
=
7810 zs
->zs_metaslab_df_alloc_threshold
;
7819 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7821 if (ztest_opts
.zo_verbose
>= 1) {
7822 (void) printf("%llu vdevs, %d datasets, %d threads,"
7823 "%d %s disks, %llu seconds...\n\n",
7824 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7825 ztest_opts
.zo_datasets
,
7826 ztest_opts
.zo_threads
,
7827 ztest_opts
.zo_raid_children
,
7828 ztest_opts
.zo_raid_type
,
7829 (u_longlong_t
)ztest_opts
.zo_time
);
7832 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7833 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7835 zs
->zs_do_init
= B_TRUE
;
7836 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7837 if (ztest_opts
.zo_verbose
>= 1) {
7838 (void) printf("Executing older ztest for "
7839 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7841 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7842 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7844 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7846 zs
->zs_do_init
= B_FALSE
;
7848 zs
->zs_proc_start
= gethrtime();
7849 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7851 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7852 zi
= &ztest_info
[f
];
7853 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7854 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7855 zc
->zc_next
= UINT64_MAX
;
7857 zc
->zc_next
= zs
->zs_proc_start
+
7858 ztest_random(2 * zi
->zi_interval
[0] + 1);
7862 * Run the tests in a loop. These tests include fault injection
7863 * to verify that self-healing data works, and forced crashes
7864 * to verify that we never lose on-disk consistency.
7866 while (gethrtime() < zs
->zs_proc_stop
) {
7871 * Initialize the workload counters for each function.
7873 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7874 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7879 /* Set the allocation switch size */
7880 zs
->zs_metaslab_df_alloc_threshold
=
7881 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7883 if (!hasalt
|| ztest_random(2) == 0) {
7884 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7885 (void) printf("Executing newer ztest: %s\n",
7889 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7891 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7892 (void) printf("Executing older ztest: %s\n",
7893 ztest_opts
.zo_alt_ztest
);
7896 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7897 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7904 if (ztest_opts
.zo_verbose
>= 1) {
7905 hrtime_t now
= gethrtime();
7907 now
= MIN(now
, zs
->zs_proc_stop
);
7908 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7909 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7911 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7912 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7914 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7915 (u_longlong_t
)zs
->zs_enospc_count
,
7916 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7918 100.0 * (now
- zs
->zs_proc_start
) /
7919 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7922 if (ztest_opts
.zo_verbose
>= 2) {
7923 (void) printf("\nWorkload summary:\n\n");
7924 (void) printf("%7s %9s %s\n",
7925 "Calls", "Time", "Function");
7926 (void) printf("%7s %9s %s\n",
7927 "-----", "----", "--------");
7928 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7929 zi
= &ztest_info
[f
];
7930 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7931 print_time(zc
->zc_time
, timebuf
);
7932 (void) printf("%7llu %9s %s\n",
7933 (u_longlong_t
)zc
->zc_count
, timebuf
,
7936 (void) printf("\n");
7939 if (!ztest_opts
.zo_mmp_test
)
7940 ztest_run_zdb(ztest_opts
.zo_pool
);
7943 if (ztest_opts
.zo_verbose
>= 1) {
7945 (void) printf("%d runs of older ztest: %s\n", older
,
7946 ztest_opts
.zo_alt_ztest
);
7947 (void) printf("%d runs of newer ztest: %s\n", newer
,
7950 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7951 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7954 umem_free(cmd
, MAXNAMELEN
);