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 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_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/spa_impl.h>
110 #include <sys/metaslab_impl.h>
111 #include <sys/dsl_prop.h>
112 #include <sys/dsl_dataset.h>
113 #include <sys/dsl_destroy.h>
114 #include <sys/dsl_scan.h>
115 #include <sys/zio_checksum.h>
116 #include <sys/refcount.h>
117 #include <sys/zfeature.h>
118 #include <sys/dsl_userhold.h>
121 #include <stdio_ext.h>
128 #include <sys/fs/zfs.h>
129 #include <zfs_fletcher.h>
130 #include <libnvpair.h>
131 #include <libzutil.h>
132 #include <sys/crypto/icp.h>
134 #include <execinfo.h> /* for backtrace() */
137 static int ztest_fd_data
= -1;
138 static int ztest_fd_rand
= -1;
140 typedef struct ztest_shared_hdr
{
141 uint64_t zh_hdr_size
;
142 uint64_t zh_opts_size
;
144 uint64_t zh_stats_size
;
145 uint64_t zh_stats_count
;
147 uint64_t zh_ds_count
;
148 } ztest_shared_hdr_t
;
150 static ztest_shared_hdr_t
*ztest_shared_hdr
;
152 enum ztest_class_state
{
153 ZTEST_VDEV_CLASS_OFF
,
158 typedef struct ztest_shared_opts
{
159 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
160 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
161 char zo_alt_ztest
[MAXNAMELEN
];
162 char zo_alt_libpath
[MAXNAMELEN
];
164 uint64_t zo_vdevtime
;
172 uint64_t zo_passtime
;
173 uint64_t zo_killrate
;
177 uint64_t zo_maxloops
;
178 uint64_t zo_metaslab_force_ganging
;
180 int zo_special_vdevs
;
182 } ztest_shared_opts_t
;
184 static const ztest_shared_opts_t ztest_opts_defaults
= {
185 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
186 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
187 .zo_alt_ztest
= { '\0' },
188 .zo_alt_libpath
= { '\0' },
190 .zo_ashift
= SPA_MINBLOCKSHIFT
,
193 .zo_raidz_parity
= 1,
194 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
197 .zo_passtime
= 60, /* 60 seconds */
198 .zo_killrate
= 70, /* 70% kill rate */
202 .zo_time
= 300, /* 5 minutes */
203 .zo_maxloops
= 50, /* max loops during spa_freeze() */
204 .zo_metaslab_force_ganging
= 64 << 10,
205 .zo_special_vdevs
= ZTEST_VDEV_CLASS_RND
,
208 extern uint64_t metaslab_force_ganging
;
209 extern uint64_t metaslab_df_alloc_threshold
;
210 extern unsigned long zfs_deadman_synctime_ms
;
211 extern int metaslab_preload_limit
;
212 extern boolean_t zfs_compressed_arc_enabled
;
213 extern int zfs_abd_scatter_enabled
;
214 extern int dmu_object_alloc_chunk_shift
;
215 extern boolean_t zfs_force_some_double_word_sm_entries
;
216 extern unsigned long zio_decompress_fail_fraction
;
217 extern unsigned long zfs_reconstruct_indirect_damage_fraction
;
218 extern int zfs_object_remap_one_indirect_delay_ms
;
221 static ztest_shared_opts_t
*ztest_shared_opts
;
222 static ztest_shared_opts_t ztest_opts
;
223 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
225 typedef struct ztest_shared_ds
{
229 static ztest_shared_ds_t
*ztest_shared_ds
;
230 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
232 #define BT_MAGIC 0x123456789abcdefULL
233 #define MAXFAULTS(zs) \
234 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
238 ZTEST_IO_WRITE_PATTERN
,
239 ZTEST_IO_WRITE_ZEROES
,
246 typedef struct ztest_block_tag
{
250 uint64_t bt_dnodesize
;
257 typedef struct bufwad
{
264 * It would be better to use a rangelock_t per object. Unfortunately
265 * the rangelock_t is not a drop-in replacement for rl_t, because we
266 * still need to map from object ID to rangelock_t.
288 #define ZTEST_RANGE_LOCKS 64
289 #define ZTEST_OBJECT_LOCKS 64
292 * Object descriptor. Used as a template for object lookup/create/remove.
294 typedef struct ztest_od
{
297 dmu_object_type_t od_type
;
298 dmu_object_type_t od_crtype
;
299 uint64_t od_blocksize
;
300 uint64_t od_crblocksize
;
301 uint64_t od_crdnodesize
;
304 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
310 typedef struct ztest_ds
{
311 ztest_shared_ds_t
*zd_shared
;
313 pthread_rwlock_t zd_zilog_lock
;
315 ztest_od_t
*zd_od
; /* debugging aid */
316 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
317 kmutex_t zd_dirobj_lock
;
318 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
319 rll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
323 * Per-iteration state.
325 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
327 typedef struct ztest_info
{
328 ztest_func_t
*zi_func
; /* test function */
329 uint64_t zi_iters
; /* iterations per execution */
330 uint64_t *zi_interval
; /* execute every <interval> seconds */
331 const char *zi_funcname
; /* name of test function */
334 typedef struct ztest_shared_callstate
{
335 uint64_t zc_count
; /* per-pass count */
336 uint64_t zc_time
; /* per-pass time */
337 uint64_t zc_next
; /* next time to call this function */
338 } ztest_shared_callstate_t
;
340 static ztest_shared_callstate_t
*ztest_shared_callstate
;
341 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
343 ztest_func_t ztest_dmu_read_write
;
344 ztest_func_t ztest_dmu_write_parallel
;
345 ztest_func_t ztest_dmu_object_alloc_free
;
346 ztest_func_t ztest_dmu_object_next_chunk
;
347 ztest_func_t ztest_dmu_commit_callbacks
;
348 ztest_func_t ztest_zap
;
349 ztest_func_t ztest_zap_parallel
;
350 ztest_func_t ztest_zil_commit
;
351 ztest_func_t ztest_zil_remount
;
352 ztest_func_t ztest_dmu_read_write_zcopy
;
353 ztest_func_t ztest_dmu_objset_create_destroy
;
354 ztest_func_t ztest_dmu_prealloc
;
355 ztest_func_t ztest_fzap
;
356 ztest_func_t ztest_dmu_snapshot_create_destroy
;
357 ztest_func_t ztest_dsl_prop_get_set
;
358 ztest_func_t ztest_spa_prop_get_set
;
359 ztest_func_t ztest_spa_create_destroy
;
360 ztest_func_t ztest_fault_inject
;
361 ztest_func_t ztest_ddt_repair
;
362 ztest_func_t ztest_dmu_snapshot_hold
;
363 ztest_func_t ztest_mmp_enable_disable
;
364 ztest_func_t ztest_scrub
;
365 ztest_func_t ztest_dsl_dataset_promote_busy
;
366 ztest_func_t ztest_vdev_attach_detach
;
367 ztest_func_t ztest_vdev_LUN_growth
;
368 ztest_func_t ztest_vdev_add_remove
;
369 ztest_func_t ztest_vdev_class_add
;
370 ztest_func_t ztest_vdev_aux_add_remove
;
371 ztest_func_t ztest_split_pool
;
372 ztest_func_t ztest_reguid
;
373 ztest_func_t ztest_spa_upgrade
;
374 ztest_func_t ztest_device_removal
;
375 ztest_func_t ztest_remap_blocks
;
376 ztest_func_t ztest_spa_checkpoint_create_discard
;
377 ztest_func_t ztest_fletcher
;
378 ztest_func_t ztest_fletcher_incr
;
379 ztest_func_t ztest_verify_dnode_bt
;
381 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
382 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
383 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
384 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
385 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
387 #define ZTI_INIT(func, iters, interval) \
388 { .zi_func = (func), \
389 .zi_iters = (iters), \
390 .zi_interval = (interval), \
391 .zi_funcname = # func }
393 ztest_info_t ztest_info
[] = {
394 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
395 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
396 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
397 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
398 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
399 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
400 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
401 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
402 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
403 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
404 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
405 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
406 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
407 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
409 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
411 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
412 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
413 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
414 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
415 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
416 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
417 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
418 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
419 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
420 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
421 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
422 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
423 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
424 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
425 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
426 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
427 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
428 ZTI_INIT(ztest_remap_blocks
, 1, &zopt_sometimes
),
429 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
430 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
431 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
432 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
435 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
438 * The following struct is used to hold a list of uncalled commit callbacks.
439 * The callbacks are ordered by txg number.
441 typedef struct ztest_cb_list
{
442 kmutex_t zcl_callbacks_lock
;
443 list_t zcl_callbacks
;
447 * Stuff we need to share writably between parent and child.
449 typedef struct ztest_shared
{
450 boolean_t zs_do_init
;
451 hrtime_t zs_proc_start
;
452 hrtime_t zs_proc_stop
;
453 hrtime_t zs_thread_start
;
454 hrtime_t zs_thread_stop
;
455 hrtime_t zs_thread_kill
;
456 uint64_t zs_enospc_count
;
457 uint64_t zs_vdev_next_leaf
;
458 uint64_t zs_vdev_aux
;
463 uint64_t zs_metaslab_sz
;
464 uint64_t zs_metaslab_df_alloc_threshold
;
468 #define ID_PARALLEL -1ULL
470 static char ztest_dev_template
[] = "%s/%s.%llua";
471 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
472 ztest_shared_t
*ztest_shared
;
474 static spa_t
*ztest_spa
= NULL
;
475 static ztest_ds_t
*ztest_ds
;
477 static kmutex_t ztest_vdev_lock
;
478 static boolean_t ztest_device_removal_active
= B_FALSE
;
479 static kmutex_t ztest_checkpoint_lock
;
482 * The ztest_name_lock protects the pool and dataset namespace used by
483 * the individual tests. To modify the namespace, consumers must grab
484 * this lock as writer. Grabbing the lock as reader will ensure that the
485 * namespace does not change while the lock is held.
487 static pthread_rwlock_t ztest_name_lock
;
489 static boolean_t ztest_dump_core
= B_TRUE
;
490 static boolean_t ztest_exiting
;
492 /* Global commit callback list */
493 static ztest_cb_list_t zcl
;
494 /* Commit cb delay */
495 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
496 static int zc_cb_counter
= 0;
499 * Minimum number of commit callbacks that need to be registered for us to check
500 * whether the minimum txg delay is acceptable.
502 #define ZTEST_COMMIT_CB_MIN_REG 100
505 * If a number of txgs equal to this threshold have been created after a commit
506 * callback has been registered but not called, then we assume there is an
507 * implementation bug.
509 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
512 ZTEST_META_DNODE
= 0,
517 static void usage(boolean_t
) __NORETURN
;
520 * These libumem hooks provide a reasonable set of defaults for the allocator's
521 * debugging facilities.
524 _umem_debug_init(void)
526 return ("default,verbose"); /* $UMEM_DEBUG setting */
530 _umem_logging_init(void)
532 return ("fail,contents"); /* $UMEM_LOGGING setting */
536 dump_debug_buffer(void)
538 ssize_t ret
__attribute__((unused
));
540 if (!ztest_opts
.zo_dump_dbgmsg
)
544 * We use write() instead of printf() so that this function
545 * is safe to call from a signal handler.
547 ret
= write(STDOUT_FILENO
, "\n", 1);
548 zfs_dbgmsg_print("ztest");
551 #define BACKTRACE_SZ 100
553 static void sig_handler(int signo
)
555 struct sigaction action
;
556 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
558 void *buffer
[BACKTRACE_SZ
];
560 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
561 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
566 * Restore default action and re-raise signal so SIGSEGV and
567 * SIGABRT can trigger a core dump.
569 action
.sa_handler
= SIG_DFL
;
570 sigemptyset(&action
.sa_mask
);
572 (void) sigaction(signo
, &action
, NULL
);
576 #define FATAL_MSG_SZ 1024
581 fatal(int do_perror
, char *message
, ...)
584 int save_errno
= errno
;
587 (void) fflush(stdout
);
588 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
590 va_start(args
, message
);
591 (void) sprintf(buf
, "ztest: ");
593 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
596 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
597 ": %s", strerror(save_errno
));
599 (void) fprintf(stderr
, "%s\n", buf
);
600 fatal_msg
= buf
; /* to ease debugging */
611 str2shift(const char *buf
)
613 const char *ends
= "BKMGTPEZ";
618 for (i
= 0; i
< strlen(ends
); i
++) {
619 if (toupper(buf
[0]) == ends
[i
])
622 if (i
== strlen(ends
)) {
623 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
627 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
630 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
636 nicenumtoull(const char *buf
)
641 val
= strtoull(buf
, &end
, 0);
643 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
645 } else if (end
[0] == '.') {
646 double fval
= strtod(buf
, &end
);
647 fval
*= pow(2, str2shift(end
));
648 if (fval
> UINT64_MAX
) {
649 (void) fprintf(stderr
, "ztest: value too large: %s\n",
653 val
= (uint64_t)fval
;
655 int shift
= str2shift(end
);
656 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
657 (void) fprintf(stderr
, "ztest: value too large: %s\n",
667 usage(boolean_t requested
)
669 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
671 char nice_vdev_size
[NN_NUMBUF_SZ
];
672 char nice_force_ganging
[NN_NUMBUF_SZ
];
673 FILE *fp
= requested
? stdout
: stderr
;
675 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
676 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
677 sizeof (nice_force_ganging
));
679 (void) fprintf(fp
, "Usage: %s\n"
680 "\t[-v vdevs (default: %llu)]\n"
681 "\t[-s size_of_each_vdev (default: %s)]\n"
682 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
683 "\t[-m mirror_copies (default: %d)]\n"
684 "\t[-r raidz_disks (default: %d)]\n"
685 "\t[-R raidz_parity (default: %d)]\n"
686 "\t[-d datasets (default: %d)]\n"
687 "\t[-t threads (default: %d)]\n"
688 "\t[-g gang_block_threshold (default: %s)]\n"
689 "\t[-i init_count (default: %d)] initialize pool i times\n"
690 "\t[-k kill_percentage (default: %llu%%)]\n"
691 "\t[-p pool_name (default: %s)]\n"
692 "\t[-f dir (default: %s)] file directory for vdev files\n"
693 "\t[-M] Multi-host simulate pool imported on remote host\n"
694 "\t[-V] verbose (use multiple times for ever more blather)\n"
695 "\t[-E] use existing pool instead of creating new one\n"
696 "\t[-T time (default: %llu sec)] total run time\n"
697 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
698 "\t[-P passtime (default: %llu sec)] time per pass\n"
699 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
700 "\t[-C vdev class state (default: random)] special=on|off|random\n"
701 "\t[-o variable=value] ... set global variable to an unsigned\n"
702 "\t 32-bit integer value\n"
703 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
704 "\t[-h] (print help)\n"
707 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
708 nice_vdev_size
, /* -s */
709 zo
->zo_ashift
, /* -a */
710 zo
->zo_mirrors
, /* -m */
711 zo
->zo_raidz
, /* -r */
712 zo
->zo_raidz_parity
, /* -R */
713 zo
->zo_datasets
, /* -d */
714 zo
->zo_threads
, /* -t */
715 nice_force_ganging
, /* -g */
716 zo
->zo_init
, /* -i */
717 (u_longlong_t
)zo
->zo_killrate
, /* -k */
718 zo
->zo_pool
, /* -p */
720 (u_longlong_t
)zo
->zo_time
, /* -T */
721 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
722 (u_longlong_t
)zo
->zo_passtime
);
723 exit(requested
? 0 : 1);
728 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
732 int state
= ZTEST_VDEV_CLASS_RND
;
734 (void) strlcpy(name
, input
, sizeof (name
));
736 value
= strchr(name
, '=');
738 (void) fprintf(stderr
, "missing value in property=value "
739 "'-C' argument (%s)\n", input
);
745 if (strcmp(value
, "on") == 0) {
746 state
= ZTEST_VDEV_CLASS_ON
;
747 } else if (strcmp(value
, "off") == 0) {
748 state
= ZTEST_VDEV_CLASS_OFF
;
749 } else if (strcmp(value
, "random") == 0) {
750 state
= ZTEST_VDEV_CLASS_RND
;
752 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
756 if (strcmp(name
, "special") == 0) {
757 zo
->zo_special_vdevs
= state
;
759 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
762 if (zo
->zo_verbose
>= 3)
763 (void) printf("%s vdev state is '%s'\n", name
, value
);
767 process_options(int argc
, char **argv
)
770 ztest_shared_opts_t
*zo
= &ztest_opts
;
774 char altdir
[MAXNAMELEN
] = { 0 };
776 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
778 while ((opt
= getopt(argc
, argv
,
779 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
796 value
= nicenumtoull(optarg
);
800 zo
->zo_vdevs
= value
;
803 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
806 zo
->zo_ashift
= value
;
809 zo
->zo_mirrors
= value
;
812 zo
->zo_raidz
= MAX(1, value
);
815 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
818 zo
->zo_datasets
= MAX(1, value
);
821 zo
->zo_threads
= MAX(1, value
);
824 zo
->zo_metaslab_force_ganging
=
825 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
831 zo
->zo_killrate
= value
;
834 (void) strlcpy(zo
->zo_pool
, optarg
,
835 sizeof (zo
->zo_pool
));
838 path
= realpath(optarg
, NULL
);
840 (void) fprintf(stderr
, "error: %s: %s\n",
841 optarg
, strerror(errno
));
844 (void) strlcpy(zo
->zo_dir
, path
,
845 sizeof (zo
->zo_dir
));
862 zo
->zo_passtime
= MAX(1, value
);
865 zo
->zo_maxloops
= MAX(1, value
);
868 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
871 ztest_parse_name_value(optarg
, zo
);
874 if (set_global_var(optarg
) != 0)
878 zo
->zo_dump_dbgmsg
= 1;
890 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
893 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
896 if (strlen(altdir
) > 0) {
904 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
905 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
907 VERIFY(NULL
!= realpath(getexecname(), cmd
));
908 if (0 != access(altdir
, F_OK
)) {
909 ztest_dump_core
= B_FALSE
;
910 fatal(B_TRUE
, "invalid alternate ztest path: %s",
913 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
916 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
917 * We want to extract <isa> to determine if we should use
918 * 32 or 64 bit binaries.
920 bin
= strstr(cmd
, "/usr/bin/");
921 ztest
= strstr(bin
, "/ztest");
923 isalen
= ztest
- isa
;
924 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
925 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
926 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
927 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
929 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
930 ztest_dump_core
= B_FALSE
;
931 fatal(B_TRUE
, "invalid alternate ztest: %s",
933 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
934 ztest_dump_core
= B_FALSE
;
935 fatal(B_TRUE
, "invalid alternate lib directory %s",
939 umem_free(cmd
, MAXPATHLEN
);
940 umem_free(realaltdir
, MAXPATHLEN
);
945 ztest_kill(ztest_shared_t
*zs
)
947 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
948 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
951 * Before we kill off ztest, make sure that the config is updated.
952 * See comment above spa_write_cachefile().
954 mutex_enter(&spa_namespace_lock
);
955 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
956 mutex_exit(&spa_namespace_lock
);
958 (void) kill(getpid(), SIGKILL
);
962 ztest_random(uint64_t range
)
966 ASSERT3S(ztest_fd_rand
, >=, 0);
971 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
972 fatal(1, "short read from /dev/urandom");
979 ztest_record_enospc(const char *s
)
981 ztest_shared
->zs_enospc_count
++;
985 ztest_get_ashift(void)
987 if (ztest_opts
.zo_ashift
== 0)
988 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
989 return (ztest_opts
.zo_ashift
);
993 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
999 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1002 ashift
= ztest_get_ashift();
1008 vdev
= ztest_shared
->zs_vdev_aux
;
1009 (void) snprintf(path
, MAXPATHLEN
,
1010 ztest_aux_template
, ztest_opts
.zo_dir
,
1011 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
1014 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
1015 (void) snprintf(path
, MAXPATHLEN
,
1016 ztest_dev_template
, ztest_opts
.zo_dir
,
1017 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1022 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1024 fatal(1, "can't open %s", path
);
1025 if (ftruncate(fd
, size
) != 0)
1026 fatal(1, "can't ftruncate %s", path
);
1030 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
1031 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
1032 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
1033 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
1034 umem_free(pathbuf
, MAXPATHLEN
);
1040 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
1041 uint64_t ashift
, int r
)
1043 nvlist_t
*raidz
, **child
;
1047 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1048 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1050 for (c
= 0; c
< r
; c
++)
1051 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1053 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
1054 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
1055 VDEV_TYPE_RAIDZ
) == 0);
1056 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
1057 ztest_opts
.zo_raidz_parity
) == 0);
1058 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
1061 for (c
= 0; c
< r
; c
++)
1062 nvlist_free(child
[c
]);
1064 umem_free(child
, r
* sizeof (nvlist_t
*));
1070 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1071 uint64_t ashift
, int r
, int m
)
1073 nvlist_t
*mirror
, **child
;
1077 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
1079 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1081 for (c
= 0; c
< m
; c
++)
1082 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1084 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1085 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1086 VDEV_TYPE_MIRROR
) == 0);
1087 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1090 for (c
= 0; c
< m
; c
++)
1091 nvlist_free(child
[c
]);
1093 umem_free(child
, m
* sizeof (nvlist_t
*));
1099 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1100 const char *class, int r
, int m
, int t
)
1102 nvlist_t
*root
, **child
;
1108 log
= (class != NULL
&& strcmp(class, "log") == 0);
1110 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1112 for (c
= 0; c
< t
; c
++) {
1113 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1115 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1118 if (class != NULL
&& class[0] != '\0') {
1119 ASSERT(m
> 1 || log
); /* expecting a mirror */
1120 VERIFY(nvlist_add_string(child
[c
],
1121 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1125 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1126 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1127 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1130 for (c
= 0; c
< t
; c
++)
1131 nvlist_free(child
[c
]);
1133 umem_free(child
, t
* sizeof (nvlist_t
*));
1139 * Find a random spa version. Returns back a random spa version in the
1140 * range [initial_version, SPA_VERSION_FEATURES].
1143 ztest_random_spa_version(uint64_t initial_version
)
1145 uint64_t version
= initial_version
;
1147 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1149 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1152 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1153 version
= SPA_VERSION_FEATURES
;
1155 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1160 ztest_random_blocksize(void)
1162 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1165 * Choose a block size >= the ashift.
1166 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1168 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1169 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1171 uint64_t block_shift
=
1172 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1173 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1177 ztest_random_dnodesize(void)
1180 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1182 if (max_slots
== DNODE_MIN_SLOTS
)
1183 return (DNODE_MIN_SIZE
);
1186 * Weight the random distribution more heavily toward smaller
1187 * dnode sizes since that is more likely to reflect real-world
1190 ASSERT3U(max_slots
, >, 4);
1191 switch (ztest_random(10)) {
1193 slots
= 5 + ztest_random(max_slots
- 4);
1196 slots
= 2 + ztest_random(3);
1203 return (slots
<< DNODE_SHIFT
);
1207 ztest_random_ibshift(void)
1209 return (DN_MIN_INDBLKSHIFT
+
1210 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1214 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1217 vdev_t
*rvd
= spa
->spa_root_vdev
;
1220 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1223 top
= ztest_random(rvd
->vdev_children
);
1224 tvd
= rvd
->vdev_child
[top
];
1225 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1226 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1232 ztest_random_dsl_prop(zfs_prop_t prop
)
1237 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1238 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1244 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1247 const char *propname
= zfs_prop_to_name(prop
);
1248 const char *valname
;
1253 error
= dsl_prop_set_int(osname
, propname
,
1254 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1256 if (error
== ENOSPC
) {
1257 ztest_record_enospc(FTAG
);
1262 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1263 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1265 if (ztest_opts
.zo_verbose
>= 6) {
1268 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1270 (void) printf("%s %s = %llu at '%s'\n", osname
,
1271 propname
, (unsigned long long)curval
, setpoint
);
1273 (void) printf("%s %s = %s at '%s'\n",
1274 osname
, propname
, valname
, setpoint
);
1276 umem_free(setpoint
, MAXPATHLEN
);
1282 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1284 spa_t
*spa
= ztest_spa
;
1285 nvlist_t
*props
= NULL
;
1288 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1289 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1291 error
= spa_prop_set(spa
, props
);
1295 if (error
== ENOSPC
) {
1296 ztest_record_enospc(FTAG
);
1305 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1306 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1310 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1312 strcpy(ddname
, name
);
1313 cp
= strchr(ddname
, '@');
1317 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1318 while (decrypt
&& err
== EACCES
) {
1319 dsl_crypto_params_t
*dcp
;
1320 nvlist_t
*crypto_args
= fnvlist_alloc();
1322 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1323 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1324 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1325 crypto_args
, &dcp
));
1326 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1327 dsl_crypto_params_free(dcp
, B_FALSE
);
1328 fnvlist_free(crypto_args
);
1330 if (err
== EINVAL
) {
1332 * We couldn't load a key for this dataset so try
1333 * the parent. This loop will eventually hit the
1334 * encryption root since ztest only makes clones
1335 * as children of their origin datasets.
1337 cp
= strrchr(ddname
, '/');
1344 } else if (err
!= 0) {
1348 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1356 ztest_rll_init(rll_t
*rll
)
1358 rll
->rll_writer
= NULL
;
1359 rll
->rll_readers
= 0;
1360 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1361 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1365 ztest_rll_destroy(rll_t
*rll
)
1367 ASSERT(rll
->rll_writer
== NULL
);
1368 ASSERT(rll
->rll_readers
== 0);
1369 mutex_destroy(&rll
->rll_lock
);
1370 cv_destroy(&rll
->rll_cv
);
1374 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1376 mutex_enter(&rll
->rll_lock
);
1378 if (type
== RL_READER
) {
1379 while (rll
->rll_writer
!= NULL
)
1380 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1383 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1384 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1385 rll
->rll_writer
= curthread
;
1388 mutex_exit(&rll
->rll_lock
);
1392 ztest_rll_unlock(rll_t
*rll
)
1394 mutex_enter(&rll
->rll_lock
);
1396 if (rll
->rll_writer
) {
1397 ASSERT(rll
->rll_readers
== 0);
1398 rll
->rll_writer
= NULL
;
1400 ASSERT(rll
->rll_readers
!= 0);
1401 ASSERT(rll
->rll_writer
== NULL
);
1405 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1406 cv_broadcast(&rll
->rll_cv
);
1408 mutex_exit(&rll
->rll_lock
);
1412 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1414 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1416 ztest_rll_lock(rll
, type
);
1420 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1422 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1424 ztest_rll_unlock(rll
);
1428 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1429 uint64_t size
, rl_type_t type
)
1431 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1432 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1435 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1436 rl
->rl_object
= object
;
1437 rl
->rl_offset
= offset
;
1441 ztest_rll_lock(rll
, type
);
1447 ztest_range_unlock(rl_t
*rl
)
1449 rll_t
*rll
= rl
->rl_lock
;
1451 ztest_rll_unlock(rll
);
1453 umem_free(rl
, sizeof (*rl
));
1457 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1460 zd
->zd_zilog
= dmu_objset_zil(os
);
1461 zd
->zd_shared
= szd
;
1462 dmu_objset_name(os
, zd
->zd_name
);
1465 if (zd
->zd_shared
!= NULL
)
1466 zd
->zd_shared
->zd_seq
= 0;
1468 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1469 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1471 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1472 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1474 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1475 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1479 ztest_zd_fini(ztest_ds_t
*zd
)
1483 mutex_destroy(&zd
->zd_dirobj_lock
);
1484 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1486 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1487 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1489 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1490 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1493 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1496 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1502 * Attempt to assign tx to some transaction group.
1504 error
= dmu_tx_assign(tx
, txg_how
);
1506 if (error
== ERESTART
) {
1507 ASSERT(txg_how
== TXG_NOWAIT
);
1510 ASSERT3U(error
, ==, ENOSPC
);
1511 ztest_record_enospc(tag
);
1516 txg
= dmu_tx_get_txg(tx
);
1522 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1525 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1533 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1536 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1540 diff
|= (value
- *ip
++);
1547 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1548 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1551 bt
->bt_magic
= BT_MAGIC
;
1552 bt
->bt_objset
= dmu_objset_id(os
);
1553 bt
->bt_object
= object
;
1554 bt
->bt_dnodesize
= dnodesize
;
1555 bt
->bt_offset
= offset
;
1558 bt
->bt_crtxg
= crtxg
;
1562 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1563 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1566 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1567 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1568 ASSERT3U(bt
->bt_object
, ==, object
);
1569 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1570 ASSERT3U(bt
->bt_offset
, ==, offset
);
1571 ASSERT3U(bt
->bt_gen
, <=, gen
);
1572 ASSERT3U(bt
->bt_txg
, <=, txg
);
1573 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1576 static ztest_block_tag_t
*
1577 ztest_bt_bonus(dmu_buf_t
*db
)
1579 dmu_object_info_t doi
;
1580 ztest_block_tag_t
*bt
;
1582 dmu_object_info_from_db(db
, &doi
);
1583 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1584 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1585 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1591 * Generate a token to fill up unused bonus buffer space. Try to make
1592 * it unique to the object, generation, and offset to verify that data
1593 * is not getting overwritten by data from other dnodes.
1595 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1596 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1599 * Fill up the unused bonus buffer region before the block tag with a
1600 * verifiable pattern. Filling the whole bonus area with non-zero data
1601 * helps ensure that all dnode traversal code properly skips the
1602 * interior regions of large dnodes.
1605 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1606 objset_t
*os
, uint64_t gen
)
1610 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1612 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1613 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1614 gen
, bonusp
- (uint64_t *)db
->db_data
);
1620 * Verify that the unused area of a bonus buffer is filled with the
1624 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1625 objset_t
*os
, uint64_t gen
)
1629 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1630 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1631 gen
, bonusp
- (uint64_t *)db
->db_data
);
1632 VERIFY3U(*bonusp
, ==, token
);
1640 #define lrz_type lr_mode
1641 #define lrz_blocksize lr_uid
1642 #define lrz_ibshift lr_gid
1643 #define lrz_bonustype lr_rdev
1644 #define lrz_dnodesize lr_crtime[1]
1647 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1649 char *name
= (void *)(lr
+ 1); /* name follows lr */
1650 size_t namesize
= strlen(name
) + 1;
1653 if (zil_replaying(zd
->zd_zilog
, tx
))
1656 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1657 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1658 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1660 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1664 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1666 char *name
= (void *)(lr
+ 1); /* name follows lr */
1667 size_t namesize
= strlen(name
) + 1;
1670 if (zil_replaying(zd
->zd_zilog
, tx
))
1673 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1674 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1675 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1677 itx
->itx_oid
= object
;
1678 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1682 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1685 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1687 if (zil_replaying(zd
->zd_zilog
, tx
))
1690 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1691 write_state
= WR_INDIRECT
;
1693 itx
= zil_itx_create(TX_WRITE
,
1694 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1696 if (write_state
== WR_COPIED
&&
1697 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1698 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1699 zil_itx_destroy(itx
);
1700 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1701 write_state
= WR_NEED_COPY
;
1703 itx
->itx_private
= zd
;
1704 itx
->itx_wr_state
= write_state
;
1705 itx
->itx_sync
= (ztest_random(8) == 0);
1707 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1708 sizeof (*lr
) - sizeof (lr_t
));
1710 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1714 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1718 if (zil_replaying(zd
->zd_zilog
, tx
))
1721 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1722 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1723 sizeof (*lr
) - sizeof (lr_t
));
1725 itx
->itx_sync
= B_FALSE
;
1726 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1730 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1734 if (zil_replaying(zd
->zd_zilog
, tx
))
1737 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1738 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1739 sizeof (*lr
) - sizeof (lr_t
));
1741 itx
->itx_sync
= B_FALSE
;
1742 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1749 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1751 ztest_ds_t
*zd
= arg1
;
1752 lr_create_t
*lr
= arg2
;
1753 char *name
= (void *)(lr
+ 1); /* name follows lr */
1754 objset_t
*os
= zd
->zd_os
;
1755 ztest_block_tag_t
*bbt
;
1763 byteswap_uint64_array(lr
, sizeof (*lr
));
1765 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1766 ASSERT(name
[0] != '\0');
1768 tx
= dmu_tx_create(os
);
1770 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1772 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1773 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1775 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1778 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1782 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1783 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1785 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1786 if (lr
->lr_foid
== 0) {
1787 lr
->lr_foid
= zap_create_dnsize(os
,
1788 lr
->lrz_type
, lr
->lrz_bonustype
,
1789 bonuslen
, lr
->lrz_dnodesize
, tx
);
1791 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1792 lr
->lrz_type
, lr
->lrz_bonustype
,
1793 bonuslen
, lr
->lrz_dnodesize
, tx
);
1796 if (lr
->lr_foid
== 0) {
1797 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1798 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1799 bonuslen
, lr
->lrz_dnodesize
, tx
);
1801 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1802 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1803 bonuslen
, lr
->lrz_dnodesize
, tx
);
1808 ASSERT3U(error
, ==, EEXIST
);
1809 ASSERT(zd
->zd_zilog
->zl_replay
);
1814 ASSERT(lr
->lr_foid
!= 0);
1816 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1817 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1818 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1820 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1821 bbt
= ztest_bt_bonus(db
);
1822 dmu_buf_will_dirty(db
, tx
);
1823 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1824 lr
->lr_gen
, txg
, txg
);
1825 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1826 dmu_buf_rele(db
, FTAG
);
1828 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1831 (void) ztest_log_create(zd
, tx
, lr
);
1839 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1841 ztest_ds_t
*zd
= arg1
;
1842 lr_remove_t
*lr
= arg2
;
1843 char *name
= (void *)(lr
+ 1); /* name follows lr */
1844 objset_t
*os
= zd
->zd_os
;
1845 dmu_object_info_t doi
;
1847 uint64_t object
, txg
;
1850 byteswap_uint64_array(lr
, sizeof (*lr
));
1852 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1853 ASSERT(name
[0] != '\0');
1856 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1857 ASSERT(object
!= 0);
1859 ztest_object_lock(zd
, object
, RL_WRITER
);
1861 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1863 tx
= dmu_tx_create(os
);
1865 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1866 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1868 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1870 ztest_object_unlock(zd
, object
);
1874 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1875 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1877 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1880 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1882 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1886 ztest_object_unlock(zd
, object
);
1892 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1894 ztest_ds_t
*zd
= arg1
;
1895 lr_write_t
*lr
= arg2
;
1896 objset_t
*os
= zd
->zd_os
;
1897 void *data
= lr
+ 1; /* data follows lr */
1898 uint64_t offset
, length
;
1899 ztest_block_tag_t
*bt
= data
;
1900 ztest_block_tag_t
*bbt
;
1901 uint64_t gen
, txg
, lrtxg
, crtxg
;
1902 dmu_object_info_t doi
;
1905 arc_buf_t
*abuf
= NULL
;
1909 byteswap_uint64_array(lr
, sizeof (*lr
));
1911 offset
= lr
->lr_offset
;
1912 length
= lr
->lr_length
;
1914 /* If it's a dmu_sync() block, write the whole block */
1915 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1916 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1917 if (length
< blocksize
) {
1918 offset
-= offset
% blocksize
;
1923 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1924 byteswap_uint64_array(bt
, sizeof (*bt
));
1926 if (bt
->bt_magic
!= BT_MAGIC
)
1929 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1930 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1932 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1934 dmu_object_info_from_db(db
, &doi
);
1936 bbt
= ztest_bt_bonus(db
);
1937 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1939 crtxg
= bbt
->bt_crtxg
;
1940 lrtxg
= lr
->lr_common
.lrc_txg
;
1942 tx
= dmu_tx_create(os
);
1944 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1946 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1947 P2PHASE(offset
, length
) == 0)
1948 abuf
= dmu_request_arcbuf(db
, length
);
1950 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1953 dmu_return_arcbuf(abuf
);
1954 dmu_buf_rele(db
, FTAG
);
1955 ztest_range_unlock(rl
);
1956 ztest_object_unlock(zd
, lr
->lr_foid
);
1962 * Usually, verify the old data before writing new data --
1963 * but not always, because we also want to verify correct
1964 * behavior when the data was not recently read into cache.
1966 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1967 if (ztest_random(4) != 0) {
1968 int prefetch
= ztest_random(2) ?
1969 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1970 ztest_block_tag_t rbt
;
1972 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1973 sizeof (rbt
), &rbt
, prefetch
) == 0);
1974 if (rbt
.bt_magic
== BT_MAGIC
) {
1975 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1976 offset
, gen
, txg
, crtxg
);
1981 * Writes can appear to be newer than the bonus buffer because
1982 * the ztest_get_data() callback does a dmu_read() of the
1983 * open-context data, which may be different than the data
1984 * as it was when the write was generated.
1986 if (zd
->zd_zilog
->zl_replay
) {
1987 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1988 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1993 * Set the bt's gen/txg to the bonus buffer's gen/txg
1994 * so that all of the usual ASSERTs will work.
1996 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
2001 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
2003 bcopy(data
, abuf
->b_data
, length
);
2004 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
2007 (void) ztest_log_write(zd
, tx
, lr
);
2009 dmu_buf_rele(db
, FTAG
);
2013 ztest_range_unlock(rl
);
2014 ztest_object_unlock(zd
, lr
->lr_foid
);
2020 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2022 ztest_ds_t
*zd
= arg1
;
2023 lr_truncate_t
*lr
= arg2
;
2024 objset_t
*os
= zd
->zd_os
;
2030 byteswap_uint64_array(lr
, sizeof (*lr
));
2032 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2033 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2036 tx
= dmu_tx_create(os
);
2038 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2040 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2042 ztest_range_unlock(rl
);
2043 ztest_object_unlock(zd
, lr
->lr_foid
);
2047 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2048 lr
->lr_length
, tx
) == 0);
2050 (void) ztest_log_truncate(zd
, tx
, lr
);
2054 ztest_range_unlock(rl
);
2055 ztest_object_unlock(zd
, lr
->lr_foid
);
2061 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2063 ztest_ds_t
*zd
= arg1
;
2064 lr_setattr_t
*lr
= arg2
;
2065 objset_t
*os
= zd
->zd_os
;
2068 ztest_block_tag_t
*bbt
;
2069 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2072 byteswap_uint64_array(lr
, sizeof (*lr
));
2074 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2076 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2078 tx
= dmu_tx_create(os
);
2079 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2081 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2083 dmu_buf_rele(db
, FTAG
);
2084 ztest_object_unlock(zd
, lr
->lr_foid
);
2088 bbt
= ztest_bt_bonus(db
);
2089 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2090 crtxg
= bbt
->bt_crtxg
;
2091 lrtxg
= lr
->lr_common
.lrc_txg
;
2092 dnodesize
= bbt
->bt_dnodesize
;
2094 if (zd
->zd_zilog
->zl_replay
) {
2095 ASSERT(lr
->lr_size
!= 0);
2096 ASSERT(lr
->lr_mode
!= 0);
2100 * Randomly change the size and increment the generation.
2102 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2104 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2109 * Verify that the current bonus buffer is not newer than our txg.
2111 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2112 MAX(txg
, lrtxg
), crtxg
);
2114 dmu_buf_will_dirty(db
, tx
);
2116 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2117 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2118 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2119 bbt
= ztest_bt_bonus(db
);
2121 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2123 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2124 dmu_buf_rele(db
, FTAG
);
2126 (void) ztest_log_setattr(zd
, tx
, lr
);
2130 ztest_object_unlock(zd
, lr
->lr_foid
);
2135 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2136 NULL
, /* 0 no such transaction type */
2137 ztest_replay_create
, /* TX_CREATE */
2138 NULL
, /* TX_MKDIR */
2139 NULL
, /* TX_MKXATTR */
2140 NULL
, /* TX_SYMLINK */
2141 ztest_replay_remove
, /* TX_REMOVE */
2142 NULL
, /* TX_RMDIR */
2144 NULL
, /* TX_RENAME */
2145 ztest_replay_write
, /* TX_WRITE */
2146 ztest_replay_truncate
, /* TX_TRUNCATE */
2147 ztest_replay_setattr
, /* TX_SETATTR */
2149 NULL
, /* TX_CREATE_ACL */
2150 NULL
, /* TX_CREATE_ATTR */
2151 NULL
, /* TX_CREATE_ACL_ATTR */
2152 NULL
, /* TX_MKDIR_ACL */
2153 NULL
, /* TX_MKDIR_ATTR */
2154 NULL
, /* TX_MKDIR_ACL_ATTR */
2155 NULL
, /* TX_WRITE2 */
2159 * ZIL get_data callbacks
2164 ztest_get_done(zgd_t
*zgd
, int error
)
2166 ztest_ds_t
*zd
= zgd
->zgd_private
;
2167 uint64_t object
= ((rl_t
*)zgd
->zgd_lr
)->rl_object
;
2170 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2172 ztest_range_unlock((rl_t
*)zgd
->zgd_lr
);
2173 ztest_object_unlock(zd
, object
);
2175 umem_free(zgd
, sizeof (*zgd
));
2179 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2182 ztest_ds_t
*zd
= arg
;
2183 objset_t
*os
= zd
->zd_os
;
2184 uint64_t object
= lr
->lr_foid
;
2185 uint64_t offset
= lr
->lr_offset
;
2186 uint64_t size
= lr
->lr_length
;
2187 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2189 dmu_object_info_t doi
;
2194 ASSERT3P(lwb
, !=, NULL
);
2195 ASSERT3P(zio
, !=, NULL
);
2196 ASSERT3U(size
, !=, 0);
2198 ztest_object_lock(zd
, object
, RL_READER
);
2199 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2201 ztest_object_unlock(zd
, object
);
2205 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2207 if (crtxg
== 0 || crtxg
> txg
) {
2208 dmu_buf_rele(db
, FTAG
);
2209 ztest_object_unlock(zd
, object
);
2213 dmu_object_info_from_db(db
, &doi
);
2214 dmu_buf_rele(db
, FTAG
);
2217 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2219 zgd
->zgd_private
= zd
;
2221 if (buf
!= NULL
) { /* immediate write */
2222 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2223 object
, offset
, size
, RL_READER
);
2225 error
= dmu_read(os
, object
, offset
, size
, buf
,
2226 DMU_READ_NO_PREFETCH
);
2229 size
= doi
.doi_data_block_size
;
2231 offset
= P2ALIGN(offset
, size
);
2233 ASSERT(offset
< size
);
2237 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2238 object
, offset
, size
, RL_READER
);
2240 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2241 DMU_READ_NO_PREFETCH
);
2244 blkptr_t
*bp
= &lr
->lr_blkptr
;
2249 ASSERT(db
->db_offset
== offset
);
2250 ASSERT(db
->db_size
== size
);
2252 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2253 ztest_get_done
, zgd
);
2260 ztest_get_done(zgd
, error
);
2266 ztest_lr_alloc(size_t lrsize
, char *name
)
2269 size_t namesize
= name
? strlen(name
) + 1 : 0;
2271 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2274 bcopy(name
, lr
+ lrsize
, namesize
);
2280 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2282 size_t namesize
= name
? strlen(name
) + 1 : 0;
2284 umem_free(lr
, lrsize
+ namesize
);
2288 * Lookup a bunch of objects. Returns the number of objects not found.
2291 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2297 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2299 for (i
= 0; i
< count
; i
++, od
++) {
2301 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2302 sizeof (uint64_t), 1, &od
->od_object
);
2304 ASSERT(error
== ENOENT
);
2305 ASSERT(od
->od_object
== 0);
2309 ztest_block_tag_t
*bbt
;
2310 dmu_object_info_t doi
;
2312 ASSERT(od
->od_object
!= 0);
2313 ASSERT(missing
== 0); /* there should be no gaps */
2315 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2316 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2317 od
->od_object
, FTAG
, &db
));
2318 dmu_object_info_from_db(db
, &doi
);
2319 bbt
= ztest_bt_bonus(db
);
2320 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2321 od
->od_type
= doi
.doi_type
;
2322 od
->od_blocksize
= doi
.doi_data_block_size
;
2323 od
->od_gen
= bbt
->bt_gen
;
2324 dmu_buf_rele(db
, FTAG
);
2325 ztest_object_unlock(zd
, od
->od_object
);
2333 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2338 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2340 for (i
= 0; i
< count
; i
++, od
++) {
2347 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2349 lr
->lr_doid
= od
->od_dir
;
2350 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2351 lr
->lrz_type
= od
->od_crtype
;
2352 lr
->lrz_blocksize
= od
->od_crblocksize
;
2353 lr
->lrz_ibshift
= ztest_random_ibshift();
2354 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2355 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2356 lr
->lr_gen
= od
->od_crgen
;
2357 lr
->lr_crtime
[0] = time(NULL
);
2359 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2360 ASSERT(missing
== 0);
2364 od
->od_object
= lr
->lr_foid
;
2365 od
->od_type
= od
->od_crtype
;
2366 od
->od_blocksize
= od
->od_crblocksize
;
2367 od
->od_gen
= od
->od_crgen
;
2368 ASSERT(od
->od_object
!= 0);
2371 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2378 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2384 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2388 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2395 * No object was found.
2397 if (od
->od_object
== 0)
2400 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2402 lr
->lr_doid
= od
->od_dir
;
2404 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2405 ASSERT3U(error
, ==, ENOSPC
);
2410 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2417 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2423 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2425 lr
->lr_foid
= object
;
2426 lr
->lr_offset
= offset
;
2427 lr
->lr_length
= size
;
2429 BP_ZERO(&lr
->lr_blkptr
);
2431 bcopy(data
, lr
+ 1, size
);
2433 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2435 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2441 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2446 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2448 lr
->lr_foid
= object
;
2449 lr
->lr_offset
= offset
;
2450 lr
->lr_length
= size
;
2452 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2454 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2460 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2465 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2467 lr
->lr_foid
= object
;
2471 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2473 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2479 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2481 objset_t
*os
= zd
->zd_os
;
2486 txg_wait_synced(dmu_objset_pool(os
), 0);
2488 ztest_object_lock(zd
, object
, RL_READER
);
2489 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2491 tx
= dmu_tx_create(os
);
2493 dmu_tx_hold_write(tx
, object
, offset
, size
);
2495 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2498 dmu_prealloc(os
, object
, offset
, size
, tx
);
2500 txg_wait_synced(dmu_objset_pool(os
), txg
);
2502 (void) dmu_free_long_range(os
, object
, offset
, size
);
2505 ztest_range_unlock(rl
);
2506 ztest_object_unlock(zd
, object
);
2510 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2513 ztest_block_tag_t wbt
;
2514 dmu_object_info_t doi
;
2515 enum ztest_io_type io_type
;
2519 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2520 blocksize
= doi
.doi_data_block_size
;
2521 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2524 * Pick an i/o type at random, biased toward writing block tags.
2526 io_type
= ztest_random(ZTEST_IO_TYPES
);
2527 if (ztest_random(2) == 0)
2528 io_type
= ZTEST_IO_WRITE_TAG
;
2530 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2534 case ZTEST_IO_WRITE_TAG
:
2535 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2537 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2540 case ZTEST_IO_WRITE_PATTERN
:
2541 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2542 if (ztest_random(2) == 0) {
2544 * Induce fletcher2 collisions to ensure that
2545 * zio_ddt_collision() detects and resolves them
2546 * when using fletcher2-verify for deduplication.
2548 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2549 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2551 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2554 case ZTEST_IO_WRITE_ZEROES
:
2555 bzero(data
, blocksize
);
2556 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2559 case ZTEST_IO_TRUNCATE
:
2560 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2563 case ZTEST_IO_SETATTR
:
2564 (void) ztest_setattr(zd
, object
);
2569 case ZTEST_IO_REWRITE
:
2570 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2571 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2572 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2574 VERIFY(err
== 0 || err
== ENOSPC
);
2575 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2576 ZFS_PROP_COMPRESSION
,
2577 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2579 VERIFY(err
== 0 || err
== ENOSPC
);
2580 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2582 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2583 DMU_READ_NO_PREFETCH
));
2585 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2589 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2591 umem_free(data
, blocksize
);
2595 * Initialize an object description template.
2598 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2599 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2602 od
->od_dir
= ZTEST_DIROBJ
;
2605 od
->od_crtype
= type
;
2606 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2607 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2610 od
->od_type
= DMU_OT_NONE
;
2611 od
->od_blocksize
= 0;
2614 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2615 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2619 * Lookup or create the objects for a test using the od template.
2620 * If the objects do not all exist, or if 'remove' is specified,
2621 * remove any existing objects and create new ones. Otherwise,
2622 * use the existing objects.
2625 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2627 int count
= size
/ sizeof (*od
);
2630 mutex_enter(&zd
->zd_dirobj_lock
);
2631 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2632 (ztest_remove(zd
, od
, count
) != 0 ||
2633 ztest_create(zd
, od
, count
) != 0))
2636 mutex_exit(&zd
->zd_dirobj_lock
);
2643 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2645 zilog_t
*zilog
= zd
->zd_zilog
;
2647 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2649 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2652 * Remember the committed values in zd, which is in parent/child
2653 * shared memory. If we die, the next iteration of ztest_run()
2654 * will verify that the log really does contain this record.
2656 mutex_enter(&zilog
->zl_lock
);
2657 ASSERT(zd
->zd_shared
!= NULL
);
2658 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2659 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2660 mutex_exit(&zilog
->zl_lock
);
2662 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2666 * This function is designed to simulate the operations that occur during a
2667 * mount/unmount operation. We hold the dataset across these operations in an
2668 * attempt to expose any implicit assumptions about ZIL management.
2672 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2674 objset_t
*os
= zd
->zd_os
;
2677 * We hold the ztest_vdev_lock so we don't cause problems with
2678 * other threads that wish to remove a log device, such as
2679 * ztest_device_removal().
2681 mutex_enter(&ztest_vdev_lock
);
2684 * We grab the zd_dirobj_lock to ensure that no other thread is
2685 * updating the zil (i.e. adding in-memory log records) and the
2686 * zd_zilog_lock to block any I/O.
2688 mutex_enter(&zd
->zd_dirobj_lock
);
2689 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2691 /* zfsvfs_teardown() */
2692 zil_close(zd
->zd_zilog
);
2694 /* zfsvfs_setup() */
2695 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2696 zil_replay(os
, zd
, ztest_replay_vector
);
2698 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2699 mutex_exit(&zd
->zd_dirobj_lock
);
2700 mutex_exit(&ztest_vdev_lock
);
2704 * Verify that we can't destroy an active pool, create an existing pool,
2705 * or create a pool with a bad vdev spec.
2709 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2711 ztest_shared_opts_t
*zo
= &ztest_opts
;
2715 if (zo
->zo_mmp_test
)
2719 * Attempt to create using a bad file.
2721 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2722 VERIFY3U(ENOENT
, ==,
2723 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2724 nvlist_free(nvroot
);
2727 * Attempt to create using a bad mirror.
2729 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2730 VERIFY3U(ENOENT
, ==,
2731 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2732 nvlist_free(nvroot
);
2735 * Attempt to create an existing pool. It shouldn't matter
2736 * what's in the nvroot; we should fail with EEXIST.
2738 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2739 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2740 VERIFY3U(EEXIST
, ==,
2741 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2742 nvlist_free(nvroot
);
2743 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2744 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2745 spa_close(spa
, FTAG
);
2747 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2751 * Start and then stop the MMP threads to ensure the startup and shutdown code
2752 * works properly. Actual protection and property-related code tested via ZTS.
2756 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2758 ztest_shared_opts_t
*zo
= &ztest_opts
;
2759 spa_t
*spa
= ztest_spa
;
2761 if (zo
->zo_mmp_test
)
2764 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2765 mutex_enter(&spa
->spa_props_lock
);
2767 zfs_multihost_fail_intervals
= 0;
2769 if (!spa_multihost(spa
)) {
2770 spa
->spa_multihost
= B_TRUE
;
2771 mmp_thread_start(spa
);
2774 mutex_exit(&spa
->spa_props_lock
);
2775 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2777 txg_wait_synced(spa_get_dsl(spa
), 0);
2778 mmp_signal_all_threads();
2779 txg_wait_synced(spa_get_dsl(spa
), 0);
2781 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2782 mutex_enter(&spa
->spa_props_lock
);
2784 if (spa_multihost(spa
)) {
2785 mmp_thread_stop(spa
);
2786 spa
->spa_multihost
= B_FALSE
;
2789 mutex_exit(&spa
->spa_props_lock
);
2790 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2795 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2798 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2799 uint64_t version
, newversion
;
2800 nvlist_t
*nvroot
, *props
;
2803 if (ztest_opts
.zo_mmp_test
)
2806 mutex_enter(&ztest_vdev_lock
);
2807 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2810 * Clean up from previous runs.
2812 (void) spa_destroy(name
);
2814 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2815 NULL
, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2818 * If we're configuring a RAIDZ device then make sure that the
2819 * the initial version is capable of supporting that feature.
2821 switch (ztest_opts
.zo_raidz_parity
) {
2824 initial_version
= SPA_VERSION_INITIAL
;
2827 initial_version
= SPA_VERSION_RAIDZ2
;
2830 initial_version
= SPA_VERSION_RAIDZ3
;
2835 * Create a pool with a spa version that can be upgraded. Pick
2836 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2839 version
= ztest_random_spa_version(initial_version
);
2840 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2842 props
= fnvlist_alloc();
2843 fnvlist_add_uint64(props
,
2844 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2845 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2846 fnvlist_free(nvroot
);
2847 fnvlist_free(props
);
2849 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2850 VERIFY3U(spa_version(spa
), ==, version
);
2851 newversion
= ztest_random_spa_version(version
+ 1);
2853 if (ztest_opts
.zo_verbose
>= 4) {
2854 (void) printf("upgrading spa version from %llu to %llu\n",
2855 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2858 spa_upgrade(spa
, newversion
);
2859 VERIFY3U(spa_version(spa
), >, version
);
2860 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2861 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2862 spa_close(spa
, FTAG
);
2865 mutex_exit(&ztest_vdev_lock
);
2869 ztest_spa_checkpoint(spa_t
*spa
)
2871 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2873 int error
= spa_checkpoint(spa
->spa_name
);
2877 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2878 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2879 case ZFS_ERR_CHECKPOINT_EXISTS
:
2882 ztest_record_enospc(FTAG
);
2885 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
2890 ztest_spa_discard_checkpoint(spa_t
*spa
)
2892 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2894 int error
= spa_checkpoint_discard(spa
->spa_name
);
2898 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2899 case ZFS_ERR_NO_CHECKPOINT
:
2902 fatal(0, "spa_discard_checkpoint(%s) = %d",
2903 spa
->spa_name
, error
);
2910 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
2912 spa_t
*spa
= ztest_spa
;
2914 mutex_enter(&ztest_checkpoint_lock
);
2915 if (ztest_random(2) == 0) {
2916 ztest_spa_checkpoint(spa
);
2918 ztest_spa_discard_checkpoint(spa
);
2920 mutex_exit(&ztest_checkpoint_lock
);
2925 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2930 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2933 for (c
= 0; c
< vd
->vdev_children
; c
++)
2934 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2942 * Find the first available hole which can be used as a top-level.
2945 find_vdev_hole(spa_t
*spa
)
2947 vdev_t
*rvd
= spa
->spa_root_vdev
;
2950 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2952 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2953 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2955 if (cvd
->vdev_ishole
)
2962 * Verify that vdev_add() works as expected.
2966 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2968 ztest_shared_t
*zs
= ztest_shared
;
2969 spa_t
*spa
= ztest_spa
;
2975 if (ztest_opts
.zo_mmp_test
)
2978 mutex_enter(&ztest_vdev_lock
);
2979 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2981 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2983 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2986 * If we have slogs then remove them 1/4 of the time.
2988 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2989 metaslab_group_t
*mg
;
2992 * find the first real slog in log allocation class
2994 mg
= spa_log_class(spa
)->mc_rotor
;
2995 while (!mg
->mg_vd
->vdev_islog
)
2998 guid
= mg
->mg_vd
->vdev_guid
;
3000 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3003 * We have to grab the zs_name_lock as writer to
3004 * prevent a race between removing a slog (dmu_objset_find)
3005 * and destroying a dataset. Removing the slog will
3006 * grab a reference on the dataset which may cause
3007 * dsl_destroy_head() to fail with EBUSY thus
3008 * leaving the dataset in an inconsistent state.
3010 pthread_rwlock_wrlock(&ztest_name_lock
);
3011 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3012 pthread_rwlock_unlock(&ztest_name_lock
);
3016 case EEXIST
: /* Generic zil_reset() error */
3017 case EBUSY
: /* Replay required */
3018 case EACCES
: /* Crypto key not loaded */
3019 case ZFS_ERR_CHECKPOINT_EXISTS
:
3020 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3023 fatal(0, "spa_vdev_remove() = %d", error
);
3026 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3029 * Make 1/4 of the devices be log devices
3031 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3032 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3033 "log" : NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3035 error
= spa_vdev_add(spa
, nvroot
);
3036 nvlist_free(nvroot
);
3042 ztest_record_enospc("spa_vdev_add");
3045 fatal(0, "spa_vdev_add() = %d", error
);
3049 mutex_exit(&ztest_vdev_lock
);
3054 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3056 ztest_shared_t
*zs
= ztest_shared
;
3057 spa_t
*spa
= ztest_spa
;
3060 const char *class = (ztest_random(2) == 0) ?
3061 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3065 * By default add a special vdev 50% of the time
3067 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3068 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3069 ztest_random(2) == 0)) {
3073 mutex_enter(&ztest_vdev_lock
);
3075 /* Only test with mirrors */
3076 if (zs
->zs_mirrors
< 2) {
3077 mutex_exit(&ztest_vdev_lock
);
3081 /* requires feature@allocation_classes */
3082 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3083 mutex_exit(&ztest_vdev_lock
);
3087 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3089 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3090 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
3091 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3093 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3094 class, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3096 error
= spa_vdev_add(spa
, nvroot
);
3097 nvlist_free(nvroot
);
3099 if (error
== ENOSPC
)
3100 ztest_record_enospc("spa_vdev_add");
3101 else if (error
!= 0)
3102 fatal(0, "spa_vdev_add() = %d", error
);
3105 * 50% of the time allow small blocks in the special class
3108 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3109 if (ztest_opts
.zo_verbose
>= 3)
3110 (void) printf("Enabling special VDEV small blocks\n");
3111 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3112 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3115 mutex_exit(&ztest_vdev_lock
);
3117 if (ztest_opts
.zo_verbose
>= 3) {
3118 metaslab_class_t
*mc
;
3120 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3121 mc
= spa_special_class(spa
);
3123 mc
= spa_dedup_class(spa
);
3124 (void) printf("Added a %s mirrored vdev (of %d)\n",
3125 class, (int)mc
->mc_groups
);
3130 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3134 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3136 ztest_shared_t
*zs
= ztest_shared
;
3137 spa_t
*spa
= ztest_spa
;
3138 vdev_t
*rvd
= spa
->spa_root_vdev
;
3139 spa_aux_vdev_t
*sav
;
3145 if (ztest_opts
.zo_mmp_test
)
3148 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3150 if (ztest_random(2) == 0) {
3151 sav
= &spa
->spa_spares
;
3152 aux
= ZPOOL_CONFIG_SPARES
;
3154 sav
= &spa
->spa_l2cache
;
3155 aux
= ZPOOL_CONFIG_L2CACHE
;
3158 mutex_enter(&ztest_vdev_lock
);
3160 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3162 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3164 * Pick a random device to remove.
3166 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3169 * Find an unused device we can add.
3171 zs
->zs_vdev_aux
= 0;
3174 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3175 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3177 for (c
= 0; c
< sav
->sav_count
; c
++)
3178 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3181 if (c
== sav
->sav_count
&&
3182 vdev_lookup_by_path(rvd
, path
) == NULL
)
3188 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3194 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3195 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3196 error
= spa_vdev_add(spa
, nvroot
);
3202 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3204 nvlist_free(nvroot
);
3207 * Remove an existing device. Sometimes, dirty its
3208 * vdev state first to make sure we handle removal
3209 * of devices that have pending state changes.
3211 if (ztest_random(2) == 0)
3212 (void) vdev_online(spa
, guid
, 0, NULL
);
3214 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3219 case ZFS_ERR_CHECKPOINT_EXISTS
:
3220 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3223 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3227 mutex_exit(&ztest_vdev_lock
);
3229 umem_free(path
, MAXPATHLEN
);
3233 * split a pool if it has mirror tlvdevs
3237 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3239 ztest_shared_t
*zs
= ztest_shared
;
3240 spa_t
*spa
= ztest_spa
;
3241 vdev_t
*rvd
= spa
->spa_root_vdev
;
3242 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3243 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3246 if (ztest_opts
.zo_mmp_test
)
3249 mutex_enter(&ztest_vdev_lock
);
3251 /* ensure we have a useable config; mirrors of raidz aren't supported */
3252 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3253 mutex_exit(&ztest_vdev_lock
);
3257 /* clean up the old pool, if any */
3258 (void) spa_destroy("splitp");
3260 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3262 /* generate a config from the existing config */
3263 mutex_enter(&spa
->spa_props_lock
);
3264 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3266 mutex_exit(&spa
->spa_props_lock
);
3268 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3271 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3272 for (c
= 0; c
< children
; c
++) {
3273 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3277 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3278 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3280 VERIFY(nvlist_add_string(schild
[schildren
],
3281 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3282 VERIFY(nvlist_add_uint64(schild
[schildren
],
3283 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3285 lastlogid
= schildren
;
3290 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3291 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3292 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3295 /* OK, create a config that can be used to split */
3296 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3297 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3298 VDEV_TYPE_ROOT
) == 0);
3299 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3300 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3302 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3303 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3305 for (c
= 0; c
< schildren
; c
++)
3306 nvlist_free(schild
[c
]);
3310 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3312 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3313 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3314 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3316 nvlist_free(config
);
3319 (void) printf("successful split - results:\n");
3320 mutex_enter(&spa_namespace_lock
);
3321 show_pool_stats(spa
);
3322 show_pool_stats(spa_lookup("splitp"));
3323 mutex_exit(&spa_namespace_lock
);
3327 mutex_exit(&ztest_vdev_lock
);
3331 * Verify that we can attach and detach devices.
3335 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3337 ztest_shared_t
*zs
= ztest_shared
;
3338 spa_t
*spa
= ztest_spa
;
3339 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3340 vdev_t
*rvd
= spa
->spa_root_vdev
;
3341 vdev_t
*oldvd
, *newvd
, *pvd
;
3345 uint64_t ashift
= ztest_get_ashift();
3346 uint64_t oldguid
, pguid
;
3347 uint64_t oldsize
, newsize
;
3348 char *oldpath
, *newpath
;
3350 int oldvd_has_siblings
= B_FALSE
;
3351 int newvd_is_spare
= B_FALSE
;
3353 int error
, expected_error
;
3355 if (ztest_opts
.zo_mmp_test
)
3358 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3359 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3361 mutex_enter(&ztest_vdev_lock
);
3362 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3364 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3367 * If a vdev is in the process of being removed, its removal may
3368 * finish while we are in progress, leading to an unexpected error
3369 * value. Don't bother trying to attach while we are in the middle
3372 if (ztest_device_removal_active
) {
3373 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3374 mutex_exit(&ztest_vdev_lock
);
3379 * Decide whether to do an attach or a replace.
3381 replacing
= ztest_random(2);
3384 * Pick a random top-level vdev.
3386 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3389 * Pick a random leaf within it.
3391 leaf
= ztest_random(leaves
);
3396 oldvd
= rvd
->vdev_child
[top
];
3398 /* pick a child from the mirror */
3399 if (zs
->zs_mirrors
>= 1) {
3400 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3401 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3402 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3405 /* pick a child out of the raidz group */
3406 if (ztest_opts
.zo_raidz
> 1) {
3407 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3408 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3409 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3413 * If we're already doing an attach or replace, oldvd may be a
3414 * mirror vdev -- in which case, pick a random child.
3416 while (oldvd
->vdev_children
!= 0) {
3417 oldvd_has_siblings
= B_TRUE
;
3418 ASSERT(oldvd
->vdev_children
>= 2);
3419 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3422 oldguid
= oldvd
->vdev_guid
;
3423 oldsize
= vdev_get_min_asize(oldvd
);
3424 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3425 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3426 pvd
= oldvd
->vdev_parent
;
3427 pguid
= pvd
->vdev_guid
;
3430 * If oldvd has siblings, then half of the time, detach it.
3432 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3433 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3434 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3435 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3436 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3437 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3438 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3443 * For the new vdev, choose with equal probability between the two
3444 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3446 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3447 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3448 newvd_is_spare
= B_TRUE
;
3449 (void) strcpy(newpath
, newvd
->vdev_path
);
3451 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3452 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3453 top
* leaves
+ leaf
);
3454 if (ztest_random(2) == 0)
3455 newpath
[strlen(newpath
) - 1] = 'b';
3456 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3461 * Reopen to ensure the vdev's asize field isn't stale.
3464 newsize
= vdev_get_min_asize(newvd
);
3467 * Make newsize a little bigger or smaller than oldsize.
3468 * If it's smaller, the attach should fail.
3469 * If it's larger, and we're doing a replace,
3470 * we should get dynamic LUN growth when we're done.
3472 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3476 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3477 * unless it's a replace; in that case any non-replacing parent is OK.
3479 * If newvd is already part of the pool, it should fail with EBUSY.
3481 * If newvd is too small, it should fail with EOVERFLOW.
3483 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3484 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3485 pvd
->vdev_ops
== &vdev_replacing_ops
||
3486 pvd
->vdev_ops
== &vdev_spare_ops
))
3487 expected_error
= ENOTSUP
;
3488 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3489 expected_error
= ENOTSUP
;
3490 else if (newvd
== oldvd
)
3491 expected_error
= replacing
? 0 : EBUSY
;
3492 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3493 expected_error
= EBUSY
;
3494 else if (newsize
< oldsize
)
3495 expected_error
= EOVERFLOW
;
3496 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3497 expected_error
= EDOM
;
3501 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3504 * Build the nvlist describing newpath.
3506 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3507 ashift
, NULL
, 0, 0, 1);
3509 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3514 * If our parent was the replacing vdev, but the replace completed,
3515 * then instead of failing with ENOTSUP we may either succeed,
3516 * fail with ENODEV, or fail with EOVERFLOW.
3518 if (expected_error
== ENOTSUP
&&
3519 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3520 expected_error
= error
;
3523 * If someone grew the LUN, the replacement may be too small.
3525 if (error
== EOVERFLOW
|| error
== EBUSY
)
3526 expected_error
= error
;
3528 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3529 error
== ZFS_ERR_DISCARDING_CHECKPOINT
)
3530 expected_error
= error
;
3532 /* XXX workaround 6690467 */
3533 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3534 fatal(0, "attach (%s %llu, %s %llu, %d) "
3535 "returned %d, expected %d",
3536 oldpath
, oldsize
, newpath
,
3537 newsize
, replacing
, error
, expected_error
);
3540 mutex_exit(&ztest_vdev_lock
);
3542 umem_free(oldpath
, MAXPATHLEN
);
3543 umem_free(newpath
, MAXPATHLEN
);
3548 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3550 spa_t
*spa
= ztest_spa
;
3555 mutex_enter(&ztest_vdev_lock
);
3557 if (ztest_device_removal_active
) {
3558 mutex_exit(&ztest_vdev_lock
);
3563 * Remove a random top-level vdev and wait for removal to finish.
3565 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3566 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3567 guid
= vd
->vdev_guid
;
3568 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3570 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3572 ztest_device_removal_active
= B_TRUE
;
3573 mutex_exit(&ztest_vdev_lock
);
3576 * spa->spa_vdev_removal is created in a sync task that
3577 * is initiated via dsl_sync_task_nowait(). Since the
3578 * task may not run before spa_vdev_remove() returns, we
3579 * must wait at least 1 txg to ensure that the removal
3580 * struct has been created.
3582 txg_wait_synced(spa_get_dsl(spa
), 0);
3584 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
3585 txg_wait_synced(spa_get_dsl(spa
), 0);
3587 mutex_exit(&ztest_vdev_lock
);
3592 * The pool needs to be scrubbed after completing device removal.
3593 * Failure to do so may result in checksum errors due to the
3594 * strategy employed by ztest_fault_inject() when selecting which
3595 * offset are redundant and can be damaged.
3597 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3599 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3600 txg_wait_synced(spa_get_dsl(spa
), 0);
3603 mutex_enter(&ztest_vdev_lock
);
3604 ztest_device_removal_active
= B_FALSE
;
3605 mutex_exit(&ztest_vdev_lock
);
3609 * Callback function which expands the physical size of the vdev.
3612 grow_vdev(vdev_t
*vd
, void *arg
)
3614 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3615 size_t *newsize
= arg
;
3619 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3620 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3622 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3625 fsize
= lseek(fd
, 0, SEEK_END
);
3626 VERIFY(ftruncate(fd
, *newsize
) == 0);
3628 if (ztest_opts
.zo_verbose
>= 6) {
3629 (void) printf("%s grew from %lu to %lu bytes\n",
3630 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3637 * Callback function which expands a given vdev by calling vdev_online().
3641 online_vdev(vdev_t
*vd
, void *arg
)
3643 spa_t
*spa
= vd
->vdev_spa
;
3644 vdev_t
*tvd
= vd
->vdev_top
;
3645 uint64_t guid
= vd
->vdev_guid
;
3646 uint64_t generation
= spa
->spa_config_generation
+ 1;
3647 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3650 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3651 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3653 /* Calling vdev_online will initialize the new metaslabs */
3654 spa_config_exit(spa
, SCL_STATE
, spa
);
3655 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3656 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3659 * If vdev_online returned an error or the underlying vdev_open
3660 * failed then we abort the expand. The only way to know that
3661 * vdev_open fails is by checking the returned newstate.
3663 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3664 if (ztest_opts
.zo_verbose
>= 5) {
3665 (void) printf("Unable to expand vdev, state %llu, "
3666 "error %d\n", (u_longlong_t
)newstate
, error
);
3670 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3673 * Since we dropped the lock we need to ensure that we're
3674 * still talking to the original vdev. It's possible this
3675 * vdev may have been detached/replaced while we were
3676 * trying to online it.
3678 if (generation
!= spa
->spa_config_generation
) {
3679 if (ztest_opts
.zo_verbose
>= 5) {
3680 (void) printf("vdev configuration has changed, "
3681 "guid %llu, state %llu, expected gen %llu, "
3684 (u_longlong_t
)tvd
->vdev_state
,
3685 (u_longlong_t
)generation
,
3686 (u_longlong_t
)spa
->spa_config_generation
);
3694 * Traverse the vdev tree calling the supplied function.
3695 * We continue to walk the tree until we either have walked all
3696 * children or we receive a non-NULL return from the callback.
3697 * If a NULL callback is passed, then we just return back the first
3698 * leaf vdev we encounter.
3701 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3705 if (vd
->vdev_ops
->vdev_op_leaf
) {
3709 return (func(vd
, arg
));
3712 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3713 vdev_t
*cvd
= vd
->vdev_child
[c
];
3714 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3721 * Verify that dynamic LUN growth works as expected.
3725 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3727 spa_t
*spa
= ztest_spa
;
3729 metaslab_class_t
*mc
;
3730 metaslab_group_t
*mg
;
3731 size_t psize
, newsize
;
3733 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3735 mutex_enter(&ztest_checkpoint_lock
);
3736 mutex_enter(&ztest_vdev_lock
);
3737 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3740 * If there is a vdev removal in progress, it could complete while
3741 * we are running, in which case we would not be able to verify
3742 * that the metaslab_class space increased (because it decreases
3743 * when the device removal completes).
3745 if (ztest_device_removal_active
) {
3746 spa_config_exit(spa
, SCL_STATE
, spa
);
3747 mutex_exit(&ztest_vdev_lock
);
3748 mutex_exit(&ztest_checkpoint_lock
);
3752 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3754 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3757 old_ms_count
= tvd
->vdev_ms_count
;
3758 old_class_space
= metaslab_class_get_space(mc
);
3761 * Determine the size of the first leaf vdev associated with
3762 * our top-level device.
3764 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3765 ASSERT3P(vd
, !=, NULL
);
3766 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3768 psize
= vd
->vdev_psize
;
3771 * We only try to expand the vdev if it's healthy, less than 4x its
3772 * original size, and it has a valid psize.
3774 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3775 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3776 spa_config_exit(spa
, SCL_STATE
, spa
);
3777 mutex_exit(&ztest_vdev_lock
);
3778 mutex_exit(&ztest_checkpoint_lock
);
3782 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3783 ASSERT3U(newsize
, >, psize
);
3785 if (ztest_opts
.zo_verbose
>= 6) {
3786 (void) printf("Expanding LUN %s from %lu to %lu\n",
3787 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3791 * Growing the vdev is a two step process:
3792 * 1). expand the physical size (i.e. relabel)
3793 * 2). online the vdev to create the new metaslabs
3795 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3796 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3797 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3798 if (ztest_opts
.zo_verbose
>= 5) {
3799 (void) printf("Could not expand LUN because "
3800 "the vdev configuration changed.\n");
3802 spa_config_exit(spa
, SCL_STATE
, spa
);
3803 mutex_exit(&ztest_vdev_lock
);
3804 mutex_exit(&ztest_checkpoint_lock
);
3808 spa_config_exit(spa
, SCL_STATE
, spa
);
3811 * Expanding the LUN will update the config asynchronously,
3812 * thus we must wait for the async thread to complete any
3813 * pending tasks before proceeding.
3817 mutex_enter(&spa
->spa_async_lock
);
3818 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3819 mutex_exit(&spa
->spa_async_lock
);
3822 txg_wait_synced(spa_get_dsl(spa
), 0);
3823 (void) poll(NULL
, 0, 100);
3826 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3828 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3829 new_ms_count
= tvd
->vdev_ms_count
;
3830 new_class_space
= metaslab_class_get_space(mc
);
3832 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3833 if (ztest_opts
.zo_verbose
>= 5) {
3834 (void) printf("Could not verify LUN expansion due to "
3835 "intervening vdev offline or remove.\n");
3837 spa_config_exit(spa
, SCL_STATE
, spa
);
3838 mutex_exit(&ztest_vdev_lock
);
3839 mutex_exit(&ztest_checkpoint_lock
);
3844 * Make sure we were able to grow the vdev.
3846 if (new_ms_count
<= old_ms_count
) {
3847 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3848 old_ms_count
, new_ms_count
);
3852 * Make sure we were able to grow the pool.
3854 if (new_class_space
<= old_class_space
) {
3855 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3856 old_class_space
, new_class_space
);
3859 if (ztest_opts
.zo_verbose
>= 5) {
3860 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3862 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3863 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3864 (void) printf("%s grew from %s to %s\n",
3865 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3868 spa_config_exit(spa
, SCL_STATE
, spa
);
3869 mutex_exit(&ztest_vdev_lock
);
3870 mutex_exit(&ztest_checkpoint_lock
);
3874 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3878 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3881 * Create the objects common to all ztest datasets.
3883 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3884 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3888 ztest_dataset_create(char *dsname
)
3892 dsl_crypto_params_t
*dcp
= NULL
;
3895 * 50% of the time, we create encrypted datasets
3896 * using a random cipher suite and a hard-coded
3899 rand
= ztest_random(2);
3901 nvlist_t
*crypto_args
= fnvlist_alloc();
3902 nvlist_t
*props
= fnvlist_alloc();
3904 /* slight bias towards the default cipher suite */
3905 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3906 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3907 rand
= ZIO_CRYPT_ON
;
3909 fnvlist_add_uint64(props
,
3910 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3911 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3912 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3915 * These parameters aren't really used by the kernel. They
3916 * are simply stored so that userspace knows how to load
3919 fnvlist_add_uint64(props
,
3920 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3921 fnvlist_add_string(props
,
3922 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3923 fnvlist_add_uint64(props
,
3924 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3925 fnvlist_add_uint64(props
,
3926 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3928 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3929 crypto_args
, &dcp
));
3932 * Cycle through all available encryption implementations
3933 * to verify interoperability.
3935 VERIFY0(gcm_impl_set("cycle"));
3936 VERIFY0(aes_impl_set("cycle"));
3938 fnvlist_free(crypto_args
);
3939 fnvlist_free(props
);
3942 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
3943 ztest_objset_create_cb
, NULL
);
3944 dsl_crypto_params_free(dcp
, !!err
);
3946 rand
= ztest_random(100);
3947 if (err
|| rand
< 80)
3950 if (ztest_opts
.zo_verbose
>= 5)
3951 (void) printf("Setting dataset %s to sync always\n", dsname
);
3952 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3953 ZFS_SYNC_ALWAYS
, B_FALSE
));
3958 ztest_objset_destroy_cb(const char *name
, void *arg
)
3961 dmu_object_info_t doi
;
3965 * Verify that the dataset contains a directory object.
3967 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3968 B_TRUE
, FTAG
, &os
));
3969 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3970 if (error
!= ENOENT
) {
3971 /* We could have crashed in the middle of destroying it */
3973 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3974 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3976 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3979 * Destroy the dataset.
3981 if (strchr(name
, '@') != NULL
) {
3982 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3984 error
= dsl_destroy_head(name
);
3985 if (error
== ENOSPC
) {
3986 /* There could be checkpoint or insufficient slop */
3987 ztest_record_enospc(FTAG
);
3988 } else if (error
!= EBUSY
) {
3989 /* There could be a hold on this dataset */
3997 ztest_snapshot_create(char *osname
, uint64_t id
)
3999 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4002 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4004 error
= dmu_objset_snapshot_one(osname
, snapname
);
4005 if (error
== ENOSPC
) {
4006 ztest_record_enospc(FTAG
);
4009 if (error
!= 0 && error
!= EEXIST
) {
4010 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4017 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4019 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4022 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4025 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4026 if (error
!= 0 && error
!= ENOENT
)
4027 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4033 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4039 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4043 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4045 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4047 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4048 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4051 * If this dataset exists from a previous run, process its replay log
4052 * half of the time. If we don't replay it, then dsl_destroy_head()
4053 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4055 if (ztest_random(2) == 0 &&
4056 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4057 B_TRUE
, FTAG
, &os
) == 0) {
4058 ztest_zd_init(zdtmp
, NULL
, os
);
4059 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4060 ztest_zd_fini(zdtmp
);
4061 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4065 * There may be an old instance of the dataset we're about to
4066 * create lying around from a previous run. If so, destroy it
4067 * and all of its snapshots.
4069 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4070 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4073 * Verify that the destroyed dataset is no longer in the namespace.
4075 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4076 B_TRUE
, FTAG
, &os
));
4079 * Verify that we can create a new dataset.
4081 error
= ztest_dataset_create(name
);
4083 if (error
== ENOSPC
) {
4084 ztest_record_enospc(FTAG
);
4087 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4090 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4093 ztest_zd_init(zdtmp
, NULL
, os
);
4096 * Open the intent log for it.
4098 zilog
= zil_open(os
, ztest_get_data
);
4101 * Put some objects in there, do a little I/O to them,
4102 * and randomly take a couple of snapshots along the way.
4104 iters
= ztest_random(5);
4105 for (i
= 0; i
< iters
; i
++) {
4106 ztest_dmu_object_alloc_free(zdtmp
, id
);
4107 if (ztest_random(iters
) == 0)
4108 (void) ztest_snapshot_create(name
, i
);
4112 * Verify that we cannot create an existing dataset.
4114 VERIFY3U(EEXIST
, ==,
4115 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4118 * Verify that we can hold an objset that is also owned.
4120 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4121 dmu_objset_rele(os2
, FTAG
);
4124 * Verify that we cannot own an objset that is already owned.
4126 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4127 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4130 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4131 ztest_zd_fini(zdtmp
);
4133 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4135 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4139 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4142 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4144 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4145 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4146 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4147 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4151 * Cleanup non-standard snapshots and clones.
4154 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4163 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4164 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4165 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4166 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4167 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4169 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4170 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4171 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4172 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4173 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4174 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4175 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4176 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4177 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4178 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4180 error
= dsl_destroy_head(clone2name
);
4181 if (error
&& error
!= ENOENT
)
4182 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4183 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4184 if (error
&& error
!= ENOENT
)
4185 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4186 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4187 if (error
&& error
!= ENOENT
)
4188 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4189 error
= dsl_destroy_head(clone1name
);
4190 if (error
&& error
!= ENOENT
)
4191 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4192 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4193 if (error
&& error
!= ENOENT
)
4194 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4196 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4197 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4198 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4199 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4200 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4204 * Verify dsl_dataset_promote handles EBUSY
4207 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4215 char *osname
= zd
->zd_name
;
4218 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4219 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4220 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4221 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4222 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4224 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4226 ztest_dsl_dataset_cleanup(osname
, id
);
4228 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4229 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4230 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4231 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4232 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4233 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4234 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4235 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4236 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4237 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4239 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4240 if (error
&& error
!= EEXIST
) {
4241 if (error
== ENOSPC
) {
4242 ztest_record_enospc(FTAG
);
4245 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4248 error
= dmu_objset_clone(clone1name
, snap1name
);
4250 if (error
== ENOSPC
) {
4251 ztest_record_enospc(FTAG
);
4254 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4257 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4258 if (error
&& error
!= EEXIST
) {
4259 if (error
== ENOSPC
) {
4260 ztest_record_enospc(FTAG
);
4263 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4266 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4267 if (error
&& error
!= EEXIST
) {
4268 if (error
== ENOSPC
) {
4269 ztest_record_enospc(FTAG
);
4272 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4275 error
= dmu_objset_clone(clone2name
, snap3name
);
4277 if (error
== ENOSPC
) {
4278 ztest_record_enospc(FTAG
);
4281 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4284 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4287 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4288 error
= dsl_dataset_promote(clone2name
, NULL
);
4289 if (error
== ENOSPC
) {
4290 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4291 ztest_record_enospc(FTAG
);
4295 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4297 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4300 ztest_dsl_dataset_cleanup(osname
, id
);
4302 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4304 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4305 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4306 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4307 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4308 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4311 #undef OD_ARRAY_SIZE
4312 #define OD_ARRAY_SIZE 4
4315 * Verify that dmu_object_{alloc,free} work as expected.
4318 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4325 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4326 od
= umem_alloc(size
, UMEM_NOFAIL
);
4327 batchsize
= OD_ARRAY_SIZE
;
4329 for (b
= 0; b
< batchsize
; b
++)
4330 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4334 * Destroy the previous batch of objects, create a new batch,
4335 * and do some I/O on the new objects.
4337 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4340 while (ztest_random(4 * batchsize
) != 0)
4341 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4342 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4344 umem_free(od
, size
);
4348 * Rewind the global allocator to verify object allocation backfilling.
4351 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4353 objset_t
*os
= zd
->zd_os
;
4354 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4358 * Rewind the global allocator randomly back to a lower object number
4359 * to force backfilling and reclamation of recently freed dnodes.
4361 mutex_enter(&os
->os_obj_lock
);
4362 object
= ztest_random(os
->os_obj_next_chunk
);
4363 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4364 mutex_exit(&os
->os_obj_lock
);
4367 #undef OD_ARRAY_SIZE
4368 #define OD_ARRAY_SIZE 2
4371 * Verify that dmu_{read,write} work as expected.
4374 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4379 objset_t
*os
= zd
->zd_os
;
4380 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4381 od
= umem_alloc(size
, UMEM_NOFAIL
);
4383 int i
, freeit
, error
;
4385 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4386 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4387 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4388 uint64_t regions
= 997;
4389 uint64_t stride
= 123456789ULL;
4390 uint64_t width
= 40;
4391 int free_percent
= 5;
4394 * This test uses two objects, packobj and bigobj, that are always
4395 * updated together (i.e. in the same tx) so that their contents are
4396 * in sync and can be compared. Their contents relate to each other
4397 * in a simple way: packobj is a dense array of 'bufwad' structures,
4398 * while bigobj is a sparse array of the same bufwads. Specifically,
4399 * for any index n, there are three bufwads that should be identical:
4401 * packobj, at offset n * sizeof (bufwad_t)
4402 * bigobj, at the head of the nth chunk
4403 * bigobj, at the tail of the nth chunk
4405 * The chunk size is arbitrary. It doesn't have to be a power of two,
4406 * and it doesn't have any relation to the object blocksize.
4407 * The only requirement is that it can hold at least two bufwads.
4409 * Normally, we write the bufwad to each of these locations.
4410 * However, free_percent of the time we instead write zeroes to
4411 * packobj and perform a dmu_free_range() on bigobj. By comparing
4412 * bigobj to packobj, we can verify that the DMU is correctly
4413 * tracking which parts of an object are allocated and free,
4414 * and that the contents of the allocated blocks are correct.
4418 * Read the directory info. If it's the first time, set things up.
4420 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4421 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4424 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4425 umem_free(od
, size
);
4429 bigobj
= od
[0].od_object
;
4430 packobj
= od
[1].od_object
;
4431 chunksize
= od
[0].od_gen
;
4432 ASSERT(chunksize
== od
[1].od_gen
);
4435 * Prefetch a random chunk of the big object.
4436 * Our aim here is to get some async reads in flight
4437 * for blocks that we may free below; the DMU should
4438 * handle this race correctly.
4440 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4441 s
= 1 + ztest_random(2 * width
- 1);
4442 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4443 ZIO_PRIORITY_SYNC_READ
);
4446 * Pick a random index and compute the offsets into packobj and bigobj.
4448 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4449 s
= 1 + ztest_random(width
- 1);
4451 packoff
= n
* sizeof (bufwad_t
);
4452 packsize
= s
* sizeof (bufwad_t
);
4454 bigoff
= n
* chunksize
;
4455 bigsize
= s
* chunksize
;
4457 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4458 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4461 * free_percent of the time, free a range of bigobj rather than
4464 freeit
= (ztest_random(100) < free_percent
);
4467 * Read the current contents of our objects.
4469 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4472 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4477 * Get a tx for the mods to both packobj and bigobj.
4479 tx
= dmu_tx_create(os
);
4481 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4484 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4486 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4488 /* This accounts for setting the checksum/compression. */
4489 dmu_tx_hold_bonus(tx
, bigobj
);
4491 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4493 umem_free(packbuf
, packsize
);
4494 umem_free(bigbuf
, bigsize
);
4495 umem_free(od
, size
);
4499 enum zio_checksum cksum
;
4501 cksum
= (enum zio_checksum
)
4502 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4503 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4504 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4506 enum zio_compress comp
;
4508 comp
= (enum zio_compress
)
4509 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4510 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4511 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4514 * For each index from n to n + s, verify that the existing bufwad
4515 * in packobj matches the bufwads at the head and tail of the
4516 * corresponding chunk in bigobj. Then update all three bufwads
4517 * with the new values we want to write out.
4519 for (i
= 0; i
< s
; i
++) {
4521 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4523 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4525 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4527 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4528 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4530 if (pack
->bw_txg
> txg
)
4531 fatal(0, "future leak: got %llx, open txg is %llx",
4534 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4535 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4536 pack
->bw_index
, n
, i
);
4538 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4539 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4541 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4542 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4545 bzero(pack
, sizeof (bufwad_t
));
4547 pack
->bw_index
= n
+ i
;
4549 pack
->bw_data
= 1 + ztest_random(-2ULL);
4556 * We've verified all the old bufwads, and made new ones.
4557 * Now write them out.
4559 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4562 if (ztest_opts
.zo_verbose
>= 7) {
4563 (void) printf("freeing offset %llx size %llx"
4565 (u_longlong_t
)bigoff
,
4566 (u_longlong_t
)bigsize
,
4569 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4571 if (ztest_opts
.zo_verbose
>= 7) {
4572 (void) printf("writing offset %llx size %llx"
4574 (u_longlong_t
)bigoff
,
4575 (u_longlong_t
)bigsize
,
4578 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4584 * Sanity check the stuff we just wrote.
4587 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4588 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4590 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4591 packsize
, packcheck
, DMU_READ_PREFETCH
));
4592 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4593 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4595 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4596 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4598 umem_free(packcheck
, packsize
);
4599 umem_free(bigcheck
, bigsize
);
4602 umem_free(packbuf
, packsize
);
4603 umem_free(bigbuf
, bigsize
);
4604 umem_free(od
, size
);
4608 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4609 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4617 * For each index from n to n + s, verify that the existing bufwad
4618 * in packobj matches the bufwads at the head and tail of the
4619 * corresponding chunk in bigobj. Then update all three bufwads
4620 * with the new values we want to write out.
4622 for (i
= 0; i
< s
; i
++) {
4624 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4626 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4628 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4630 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4631 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4633 if (pack
->bw_txg
> txg
)
4634 fatal(0, "future leak: got %llx, open txg is %llx",
4637 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4638 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4639 pack
->bw_index
, n
, i
);
4641 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4642 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4644 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4645 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4647 pack
->bw_index
= n
+ i
;
4649 pack
->bw_data
= 1 + ztest_random(-2ULL);
4656 #undef OD_ARRAY_SIZE
4657 #define OD_ARRAY_SIZE 2
4660 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4662 objset_t
*os
= zd
->zd_os
;
4669 bufwad_t
*packbuf
, *bigbuf
;
4670 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4671 uint64_t blocksize
= ztest_random_blocksize();
4672 uint64_t chunksize
= blocksize
;
4673 uint64_t regions
= 997;
4674 uint64_t stride
= 123456789ULL;
4676 dmu_buf_t
*bonus_db
;
4677 arc_buf_t
**bigbuf_arcbufs
;
4678 dmu_object_info_t doi
;
4680 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4681 od
= umem_alloc(size
, UMEM_NOFAIL
);
4684 * This test uses two objects, packobj and bigobj, that are always
4685 * updated together (i.e. in the same tx) so that their contents are
4686 * in sync and can be compared. Their contents relate to each other
4687 * in a simple way: packobj is a dense array of 'bufwad' structures,
4688 * while bigobj is a sparse array of the same bufwads. Specifically,
4689 * for any index n, there are three bufwads that should be identical:
4691 * packobj, at offset n * sizeof (bufwad_t)
4692 * bigobj, at the head of the nth chunk
4693 * bigobj, at the tail of the nth chunk
4695 * The chunk size is set equal to bigobj block size so that
4696 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4700 * Read the directory info. If it's the first time, set things up.
4702 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4703 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4707 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4708 umem_free(od
, size
);
4712 bigobj
= od
[0].od_object
;
4713 packobj
= od
[1].od_object
;
4714 blocksize
= od
[0].od_blocksize
;
4715 chunksize
= blocksize
;
4716 ASSERT(chunksize
== od
[1].od_gen
);
4718 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4719 VERIFY(ISP2(doi
.doi_data_block_size
));
4720 VERIFY(chunksize
== doi
.doi_data_block_size
);
4721 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4724 * Pick a random index and compute the offsets into packobj and bigobj.
4726 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4727 s
= 1 + ztest_random(width
- 1);
4729 packoff
= n
* sizeof (bufwad_t
);
4730 packsize
= s
* sizeof (bufwad_t
);
4732 bigoff
= n
* chunksize
;
4733 bigsize
= s
* chunksize
;
4735 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4736 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4738 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4740 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4743 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4744 * Iteration 1 test zcopy to already referenced dbufs.
4745 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4746 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4747 * Iteration 4 test zcopy when dbuf is no longer dirty.
4748 * Iteration 5 test zcopy when it can't be done.
4749 * Iteration 6 one more zcopy write.
4751 for (i
= 0; i
< 7; i
++) {
4756 * In iteration 5 (i == 5) use arcbufs
4757 * that don't match bigobj blksz to test
4758 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4759 * assign an arcbuf to a dbuf.
4761 for (j
= 0; j
< s
; j
++) {
4762 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4764 dmu_request_arcbuf(bonus_db
, chunksize
);
4766 bigbuf_arcbufs
[2 * j
] =
4767 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4768 bigbuf_arcbufs
[2 * j
+ 1] =
4769 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4774 * Get a tx for the mods to both packobj and bigobj.
4776 tx
= dmu_tx_create(os
);
4778 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4779 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4781 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4783 umem_free(packbuf
, packsize
);
4784 umem_free(bigbuf
, bigsize
);
4785 for (j
= 0; j
< s
; j
++) {
4787 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4788 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4791 bigbuf_arcbufs
[2 * j
]);
4793 bigbuf_arcbufs
[2 * j
+ 1]);
4796 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4797 umem_free(od
, size
);
4798 dmu_buf_rele(bonus_db
, FTAG
);
4803 * 50% of the time don't read objects in the 1st iteration to
4804 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4805 * no existing dbufs for the specified offsets.
4807 if (i
!= 0 || ztest_random(2) != 0) {
4808 error
= dmu_read(os
, packobj
, packoff
,
4809 packsize
, packbuf
, DMU_READ_PREFETCH
);
4811 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4812 bigbuf
, DMU_READ_PREFETCH
);
4815 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4819 * We've verified all the old bufwads, and made new ones.
4820 * Now write them out.
4822 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4823 if (ztest_opts
.zo_verbose
>= 7) {
4824 (void) printf("writing offset %llx size %llx"
4826 (u_longlong_t
)bigoff
,
4827 (u_longlong_t
)bigsize
,
4830 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4832 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4833 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4834 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4836 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4837 bigbuf_arcbufs
[2 * j
]->b_data
,
4839 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4841 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4846 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4847 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4849 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4850 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4851 bigbuf_arcbufs
[j
], tx
);
4853 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4854 bigbuf_arcbufs
[2 * j
], tx
);
4855 dmu_assign_arcbuf_by_dbuf(bonus_db
,
4856 off
+ chunksize
/ 2,
4857 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4860 dmu_buf_rele(dbt
, FTAG
);
4866 * Sanity check the stuff we just wrote.
4869 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4870 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4872 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4873 packsize
, packcheck
, DMU_READ_PREFETCH
));
4874 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4875 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4877 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4878 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4880 umem_free(packcheck
, packsize
);
4881 umem_free(bigcheck
, bigsize
);
4884 txg_wait_open(dmu_objset_pool(os
), 0);
4885 } else if (i
== 3) {
4886 txg_wait_synced(dmu_objset_pool(os
), 0);
4890 dmu_buf_rele(bonus_db
, FTAG
);
4891 umem_free(packbuf
, packsize
);
4892 umem_free(bigbuf
, bigsize
);
4893 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4894 umem_free(od
, size
);
4899 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4903 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4904 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4905 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4908 * Have multiple threads write to large offsets in an object
4909 * to verify that parallel writes to an object -- even to the
4910 * same blocks within the object -- doesn't cause any trouble.
4912 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4914 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4917 while (ztest_random(10) != 0)
4918 ztest_io(zd
, od
->od_object
, offset
);
4920 umem_free(od
, sizeof (ztest_od_t
));
4924 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4927 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4928 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4929 uint64_t count
= ztest_random(20) + 1;
4930 uint64_t blocksize
= ztest_random_blocksize();
4933 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4935 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4937 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4938 !ztest_random(2)) != 0) {
4939 umem_free(od
, sizeof (ztest_od_t
));
4943 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4944 umem_free(od
, sizeof (ztest_od_t
));
4948 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4950 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4952 while (ztest_random(count
) != 0) {
4953 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4954 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4957 while (ztest_random(4) != 0)
4958 ztest_io(zd
, od
->od_object
, randoff
);
4961 umem_free(data
, blocksize
);
4962 umem_free(od
, sizeof (ztest_od_t
));
4966 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4968 #define ZTEST_ZAP_MIN_INTS 1
4969 #define ZTEST_ZAP_MAX_INTS 4
4970 #define ZTEST_ZAP_MAX_PROPS 1000
4973 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4975 objset_t
*os
= zd
->zd_os
;
4978 uint64_t txg
, last_txg
;
4979 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4980 uint64_t zl_ints
, zl_intsize
, prop
;
4983 char propname
[100], txgname
[100];
4985 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4987 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4988 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4990 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4991 !ztest_random(2)) != 0)
4994 object
= od
->od_object
;
4997 * Generate a known hash collision, and verify that
4998 * we can lookup and remove both entries.
5000 tx
= dmu_tx_create(os
);
5001 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5002 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5005 for (i
= 0; i
< 2; i
++) {
5007 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5010 for (i
= 0; i
< 2; i
++) {
5011 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5012 sizeof (uint64_t), 1, &value
[i
], tx
));
5014 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5015 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5016 ASSERT3U(zl_ints
, ==, 1);
5018 for (i
= 0; i
< 2; i
++) {
5019 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5024 * Generate a buch of random entries.
5026 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5028 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5029 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5030 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5031 bzero(value
, sizeof (value
));
5035 * If these zap entries already exist, validate their contents.
5037 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5039 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5040 ASSERT3U(zl_ints
, ==, 1);
5042 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5043 zl_ints
, &last_txg
) == 0);
5045 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5048 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5049 ASSERT3U(zl_ints
, ==, ints
);
5051 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5052 zl_ints
, value
) == 0);
5054 for (i
= 0; i
< ints
; i
++) {
5055 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5058 ASSERT3U(error
, ==, ENOENT
);
5062 * Atomically update two entries in our zap object.
5063 * The first is named txg_%llu, and contains the txg
5064 * in which the property was last updated. The second
5065 * is named prop_%llu, and the nth element of its value
5066 * should be txg + object + n.
5068 tx
= dmu_tx_create(os
);
5069 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5070 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5075 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5077 for (i
= 0; i
< ints
; i
++)
5078 value
[i
] = txg
+ object
+ i
;
5080 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5082 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5088 * Remove a random pair of entries.
5090 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5091 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5092 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5094 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5096 if (error
== ENOENT
)
5101 tx
= dmu_tx_create(os
);
5102 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5103 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5106 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5107 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5110 umem_free(od
, sizeof (ztest_od_t
));
5114 * Testcase to test the upgrading of a microzap to fatzap.
5117 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5119 objset_t
*os
= zd
->zd_os
;
5121 uint64_t object
, txg
;
5124 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5125 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5127 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5128 !ztest_random(2)) != 0)
5130 object
= od
->od_object
;
5133 * Add entries to this ZAP and make sure it spills over
5134 * and gets upgraded to a fatzap. Also, since we are adding
5135 * 2050 entries we should see ptrtbl growth and leaf-block split.
5137 for (i
= 0; i
< 2050; i
++) {
5138 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5143 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5144 (u_longlong_t
)id
, (u_longlong_t
)value
);
5146 tx
= dmu_tx_create(os
);
5147 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5148 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5151 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5153 ASSERT(error
== 0 || error
== EEXIST
);
5157 umem_free(od
, sizeof (ztest_od_t
));
5162 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5164 objset_t
*os
= zd
->zd_os
;
5166 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5168 int i
, namelen
, error
;
5169 int micro
= ztest_random(2);
5170 char name
[20], string_value
[20];
5173 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5174 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5176 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5177 umem_free(od
, sizeof (ztest_od_t
));
5181 object
= od
->od_object
;
5184 * Generate a random name of the form 'xxx.....' where each
5185 * x is a random printable character and the dots are dots.
5186 * There are 94 such characters, and the name length goes from
5187 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5189 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5191 for (i
= 0; i
< 3; i
++)
5192 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5193 for (; i
< namelen
- 1; i
++)
5197 if ((namelen
& 1) || micro
) {
5198 wsize
= sizeof (txg
);
5204 data
= string_value
;
5208 VERIFY0(zap_count(os
, object
, &count
));
5209 ASSERT(count
!= -1ULL);
5212 * Select an operation: length, lookup, add, update, remove.
5214 i
= ztest_random(5);
5217 tx
= dmu_tx_create(os
);
5218 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5219 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5221 umem_free(od
, sizeof (ztest_od_t
));
5224 bcopy(name
, string_value
, namelen
);
5228 bzero(string_value
, namelen
);
5234 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5236 ASSERT3U(wsize
, ==, zl_wsize
);
5237 ASSERT3U(wc
, ==, zl_wc
);
5239 ASSERT3U(error
, ==, ENOENT
);
5244 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5246 if (data
== string_value
&&
5247 bcmp(name
, data
, namelen
) != 0)
5248 fatal(0, "name '%s' != val '%s' len %d",
5249 name
, data
, namelen
);
5251 ASSERT3U(error
, ==, ENOENT
);
5256 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5257 ASSERT(error
== 0 || error
== EEXIST
);
5261 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5265 error
= zap_remove(os
, object
, name
, tx
);
5266 ASSERT(error
== 0 || error
== ENOENT
);
5273 umem_free(od
, sizeof (ztest_od_t
));
5277 * Commit callback data.
5279 typedef struct ztest_cb_data
{
5280 list_node_t zcd_node
;
5282 int zcd_expected_err
;
5283 boolean_t zcd_added
;
5284 boolean_t zcd_called
;
5288 /* This is the actual commit callback function */
5290 ztest_commit_callback(void *arg
, int error
)
5292 ztest_cb_data_t
*data
= arg
;
5293 uint64_t synced_txg
;
5295 VERIFY(data
!= NULL
);
5296 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5297 VERIFY(!data
->zcd_called
);
5299 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5300 if (data
->zcd_txg
> synced_txg
)
5301 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5302 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5305 data
->zcd_called
= B_TRUE
;
5307 if (error
== ECANCELED
) {
5308 ASSERT0(data
->zcd_txg
);
5309 ASSERT(!data
->zcd_added
);
5312 * The private callback data should be destroyed here, but
5313 * since we are going to check the zcd_called field after
5314 * dmu_tx_abort(), we will destroy it there.
5319 ASSERT(data
->zcd_added
);
5320 ASSERT3U(data
->zcd_txg
, !=, 0);
5322 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5324 /* See if this cb was called more quickly */
5325 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5326 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5328 /* Remove our callback from the list */
5329 list_remove(&zcl
.zcl_callbacks
, data
);
5331 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5333 umem_free(data
, sizeof (ztest_cb_data_t
));
5336 /* Allocate and initialize callback data structure */
5337 static ztest_cb_data_t
*
5338 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5340 ztest_cb_data_t
*cb_data
;
5342 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5344 cb_data
->zcd_txg
= txg
;
5345 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5346 list_link_init(&cb_data
->zcd_node
);
5352 * Commit callback test.
5355 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5357 objset_t
*os
= zd
->zd_os
;
5360 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5361 uint64_t old_txg
, txg
;
5364 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5365 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5367 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5368 umem_free(od
, sizeof (ztest_od_t
));
5372 tx
= dmu_tx_create(os
);
5374 cb_data
[0] = ztest_create_cb_data(os
, 0);
5375 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5377 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5379 /* Every once in a while, abort the transaction on purpose */
5380 if (ztest_random(100) == 0)
5384 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5386 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5388 cb_data
[0]->zcd_txg
= txg
;
5389 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5390 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5394 * It's not a strict requirement to call the registered
5395 * callbacks from inside dmu_tx_abort(), but that's what
5396 * it's supposed to happen in the current implementation
5397 * so we will check for that.
5399 for (i
= 0; i
< 2; i
++) {
5400 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5401 VERIFY(!cb_data
[i
]->zcd_called
);
5406 for (i
= 0; i
< 2; i
++) {
5407 VERIFY(cb_data
[i
]->zcd_called
);
5408 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5411 umem_free(od
, sizeof (ztest_od_t
));
5415 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5416 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5419 * Read existing data to make sure there isn't a future leak.
5421 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5422 &old_txg
, DMU_READ_PREFETCH
));
5425 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5428 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5430 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5433 * Since commit callbacks don't have any ordering requirement and since
5434 * it is theoretically possible for a commit callback to be called
5435 * after an arbitrary amount of time has elapsed since its txg has been
5436 * synced, it is difficult to reliably determine whether a commit
5437 * callback hasn't been called due to high load or due to a flawed
5440 * In practice, we will assume that if after a certain number of txgs a
5441 * commit callback hasn't been called, then most likely there's an
5442 * implementation bug..
5444 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5445 if (tmp_cb
!= NULL
&&
5446 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5447 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5448 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5452 * Let's find the place to insert our callbacks.
5454 * Even though the list is ordered by txg, it is possible for the
5455 * insertion point to not be the end because our txg may already be
5456 * quiescing at this point and other callbacks in the open txg
5457 * (from other objsets) may have sneaked in.
5459 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5460 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5461 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5463 /* Add the 3 callbacks to the list */
5464 for (i
= 0; i
< 3; i
++) {
5466 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5468 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5471 cb_data
[i
]->zcd_added
= B_TRUE
;
5472 VERIFY(!cb_data
[i
]->zcd_called
);
5474 tmp_cb
= cb_data
[i
];
5479 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5483 umem_free(od
, sizeof (ztest_od_t
));
5487 * Visit each object in the dataset. Verify that its properties
5488 * are consistent what was stored in the block tag when it was created,
5489 * and that its unused bonus buffer space has not been overwritten.
5493 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5495 objset_t
*os
= zd
->zd_os
;
5499 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5500 ztest_block_tag_t
*bt
= NULL
;
5501 dmu_object_info_t doi
;
5504 ztest_object_lock(zd
, obj
, RL_READER
);
5505 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5506 ztest_object_unlock(zd
, obj
);
5510 dmu_object_info_from_db(db
, &doi
);
5511 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5512 bt
= ztest_bt_bonus(db
);
5514 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5515 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5516 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5518 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5521 dmu_buf_rele(db
, FTAG
);
5522 ztest_object_unlock(zd
, obj
);
5528 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5530 zfs_prop_t proplist
[] = {
5532 ZFS_PROP_COMPRESSION
,
5538 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5540 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5541 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5542 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5544 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5545 ztest_random_blocksize(), (int)ztest_random(2)));
5547 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5552 ztest_remap_blocks(ztest_ds_t
*zd
, uint64_t id
)
5554 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5556 int error
= dmu_objset_remap_indirects(zd
->zd_name
);
5557 if (error
== ENOSPC
)
5561 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5566 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5568 nvlist_t
*props
= NULL
;
5570 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5572 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5573 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5575 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5577 if (ztest_opts
.zo_verbose
>= 6)
5578 dump_nvlist(props
, 4);
5582 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5586 user_release_one(const char *snapname
, const char *holdname
)
5588 nvlist_t
*snaps
, *holds
;
5591 snaps
= fnvlist_alloc();
5592 holds
= fnvlist_alloc();
5593 fnvlist_add_boolean(holds
, holdname
);
5594 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5595 fnvlist_free(holds
);
5596 error
= dsl_dataset_user_release(snaps
, NULL
);
5597 fnvlist_free(snaps
);
5602 * Test snapshot hold/release and deferred destroy.
5605 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5608 objset_t
*os
= zd
->zd_os
;
5612 char clonename
[100];
5614 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5617 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5619 dmu_objset_name(os
, osname
);
5621 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5623 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5624 (void) snprintf(clonename
, sizeof (clonename
),
5625 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5626 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5629 * Clean up from any previous run.
5631 error
= dsl_destroy_head(clonename
);
5632 if (error
!= ENOENT
)
5634 error
= user_release_one(fullname
, tag
);
5635 if (error
!= ESRCH
&& error
!= ENOENT
)
5637 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5638 if (error
!= ENOENT
)
5642 * Create snapshot, clone it, mark snap for deferred destroy,
5643 * destroy clone, verify snap was also destroyed.
5645 error
= dmu_objset_snapshot_one(osname
, snapname
);
5647 if (error
== ENOSPC
) {
5648 ztest_record_enospc("dmu_objset_snapshot");
5651 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5654 error
= dmu_objset_clone(clonename
, fullname
);
5656 if (error
== ENOSPC
) {
5657 ztest_record_enospc("dmu_objset_clone");
5660 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5663 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5665 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5669 error
= dsl_destroy_head(clonename
);
5671 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5673 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5674 if (error
!= ENOENT
)
5675 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5678 * Create snapshot, add temporary hold, verify that we can't
5679 * destroy a held snapshot, mark for deferred destroy,
5680 * release hold, verify snapshot was destroyed.
5682 error
= dmu_objset_snapshot_one(osname
, snapname
);
5684 if (error
== ENOSPC
) {
5685 ztest_record_enospc("dmu_objset_snapshot");
5688 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5691 holds
= fnvlist_alloc();
5692 fnvlist_add_string(holds
, fullname
, tag
);
5693 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5694 fnvlist_free(holds
);
5696 if (error
== ENOSPC
) {
5697 ztest_record_enospc("dsl_dataset_user_hold");
5700 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5701 fullname
, tag
, error
);
5704 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5705 if (error
!= EBUSY
) {
5706 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5710 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5712 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5716 error
= user_release_one(fullname
, tag
);
5718 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5720 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5723 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5727 * Inject random faults into the on-disk data.
5731 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5733 ztest_shared_t
*zs
= ztest_shared
;
5734 spa_t
*spa
= ztest_spa
;
5738 uint64_t bad
= 0x1990c0ffeedecadeull
;
5743 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5749 boolean_t islog
= B_FALSE
;
5751 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5752 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5754 mutex_enter(&ztest_vdev_lock
);
5757 * Device removal is in progress, fault injection must be disabled
5758 * until it completes and the pool is scrubbed. The fault injection
5759 * strategy for damaging blocks does not take in to account evacuated
5760 * blocks which may have already been damaged.
5762 if (ztest_device_removal_active
) {
5763 mutex_exit(&ztest_vdev_lock
);
5767 maxfaults
= MAXFAULTS(zs
);
5768 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5769 mirror_save
= zs
->zs_mirrors
;
5770 mutex_exit(&ztest_vdev_lock
);
5772 ASSERT(leaves
>= 1);
5775 * Grab the name lock as reader. There are some operations
5776 * which don't like to have their vdevs changed while
5777 * they are in progress (i.e. spa_change_guid). Those
5778 * operations will have grabbed the name lock as writer.
5780 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5783 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5785 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5787 if (ztest_random(2) == 0) {
5789 * Inject errors on a normal data device or slog device.
5791 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5792 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5795 * Generate paths to the first leaf in this top-level vdev,
5796 * and to the random leaf we selected. We'll induce transient
5797 * write failures and random online/offline activity on leaf 0,
5798 * and we'll write random garbage to the randomly chosen leaf.
5800 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5801 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5802 top
* leaves
+ zs
->zs_splits
);
5803 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5804 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5805 top
* leaves
+ leaf
);
5807 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5808 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5812 * If the top-level vdev needs to be resilvered
5813 * then we only allow faults on the device that is
5816 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5817 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5818 vd0
->vdev_resilver_txg
!= 0)) {
5820 * Make vd0 explicitly claim to be unreadable,
5821 * or unwriteable, or reach behind its back
5822 * and close the underlying fd. We can do this if
5823 * maxfaults == 0 because we'll fail and reexecute,
5824 * and we can do it if maxfaults >= 2 because we'll
5825 * have enough redundancy. If maxfaults == 1, the
5826 * combination of this with injection of random data
5827 * corruption below exceeds the pool's fault tolerance.
5829 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5831 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5832 (long long)vd0
->vdev_id
, (int)maxfaults
);
5834 if (vf
!= NULL
&& ztest_random(3) == 0) {
5835 (void) close(vf
->vf_vnode
->v_fd
);
5836 vf
->vf_vnode
->v_fd
= -1;
5837 } else if (ztest_random(2) == 0) {
5838 vd0
->vdev_cant_read
= B_TRUE
;
5840 vd0
->vdev_cant_write
= B_TRUE
;
5842 guid0
= vd0
->vdev_guid
;
5846 * Inject errors on an l2cache device.
5848 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5850 if (sav
->sav_count
== 0) {
5851 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5852 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5855 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5856 guid0
= vd0
->vdev_guid
;
5857 (void) strcpy(path0
, vd0
->vdev_path
);
5858 (void) strcpy(pathrand
, vd0
->vdev_path
);
5862 maxfaults
= INT_MAX
; /* no limit on cache devices */
5865 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5866 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5869 * If we can tolerate two or more faults, or we're dealing
5870 * with a slog, randomly online/offline vd0.
5872 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5873 if (ztest_random(10) < 6) {
5874 int flags
= (ztest_random(2) == 0 ?
5875 ZFS_OFFLINE_TEMPORARY
: 0);
5878 * We have to grab the zs_name_lock as writer to
5879 * prevent a race between offlining a slog and
5880 * destroying a dataset. Offlining the slog will
5881 * grab a reference on the dataset which may cause
5882 * dsl_destroy_head() to fail with EBUSY thus
5883 * leaving the dataset in an inconsistent state.
5886 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
5888 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5891 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5894 * Ideally we would like to be able to randomly
5895 * call vdev_[on|off]line without holding locks
5896 * to force unpredictable failures but the side
5897 * effects of vdev_[on|off]line prevent us from
5898 * doing so. We grab the ztest_vdev_lock here to
5899 * prevent a race between injection testing and
5902 mutex_enter(&ztest_vdev_lock
);
5903 (void) vdev_online(spa
, guid0
, 0, NULL
);
5904 mutex_exit(&ztest_vdev_lock
);
5912 * We have at least single-fault tolerance, so inject data corruption.
5914 fd
= open(pathrand
, O_RDWR
);
5916 if (fd
== -1) /* we hit a gap in the device namespace */
5919 fsize
= lseek(fd
, 0, SEEK_END
);
5921 while (--iters
!= 0) {
5923 * The offset must be chosen carefully to ensure that
5924 * we do not inject a given logical block with errors
5925 * on two different leaf devices, because ZFS can not
5926 * tolerate that (if maxfaults==1).
5928 * We divide each leaf into chunks of size
5929 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5930 * there is a series of ranges to which we can inject errors.
5931 * Each range can accept errors on only a single leaf vdev.
5932 * The error injection ranges are separated by ranges
5933 * which we will not inject errors on any device (DMZs).
5934 * Each DMZ must be large enough such that a single block
5935 * can not straddle it, so that a single block can not be
5936 * a target in two different injection ranges (on different
5939 * For example, with 3 leaves, each chunk looks like:
5940 * 0 to 32M: injection range for leaf 0
5941 * 32M to 64M: DMZ - no injection allowed
5942 * 64M to 96M: injection range for leaf 1
5943 * 96M to 128M: DMZ - no injection allowed
5944 * 128M to 160M: injection range for leaf 2
5945 * 160M to 192M: DMZ - no injection allowed
5947 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5948 (leaves
<< bshift
) + (leaf
<< bshift
) +
5949 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5952 * Only allow damage to the labels at one end of the vdev.
5954 * If all labels are damaged, the device will be totally
5955 * inaccessible, which will result in loss of data,
5956 * because we also damage (parts of) the other side of
5959 * Additionally, we will always have both an even and an
5960 * odd label, so that we can handle crashes in the
5961 * middle of vdev_config_sync().
5963 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5967 * The two end labels are stored at the "end" of the disk, but
5968 * the end of the disk (vdev_psize) is aligned to
5969 * sizeof (vdev_label_t).
5971 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5972 if ((leaf
& 1) == 1 &&
5973 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5976 mutex_enter(&ztest_vdev_lock
);
5977 if (mirror_save
!= zs
->zs_mirrors
) {
5978 mutex_exit(&ztest_vdev_lock
);
5983 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5984 fatal(1, "can't inject bad word at 0x%llx in %s",
5987 mutex_exit(&ztest_vdev_lock
);
5989 if (ztest_opts
.zo_verbose
>= 7)
5990 (void) printf("injected bad word into %s,"
5991 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5996 umem_free(path0
, MAXPATHLEN
);
5997 umem_free(pathrand
, MAXPATHLEN
);
6001 * Verify that DDT repair works as expected.
6004 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
6006 ztest_shared_t
*zs
= ztest_shared
;
6007 spa_t
*spa
= ztest_spa
;
6008 objset_t
*os
= zd
->zd_os
;
6010 uint64_t object
, blocksize
, txg
, pattern
, psize
;
6011 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
6016 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
6019 blocksize
= ztest_random_blocksize();
6020 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
6022 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
6023 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
6025 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
6026 umem_free(od
, sizeof (ztest_od_t
));
6031 * Take the name lock as writer to prevent anyone else from changing
6032 * the pool and dataset properies we need to maintain during this test.
6034 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6036 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
6038 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
6040 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6041 umem_free(od
, sizeof (ztest_od_t
));
6045 dmu_objset_stats_t dds
;
6046 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6047 dmu_objset_fast_stat(os
, &dds
);
6048 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6050 object
= od
[0].od_object
;
6051 blocksize
= od
[0].od_blocksize
;
6052 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
6054 ASSERT(object
!= 0);
6056 tx
= dmu_tx_create(os
);
6057 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
6058 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
6060 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6061 umem_free(od
, sizeof (ztest_od_t
));
6066 * Write all the copies of our block.
6068 for (i
= 0; i
< copies
; i
++) {
6069 uint64_t offset
= i
* blocksize
;
6070 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
6071 DMU_READ_NO_PREFETCH
);
6073 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
6074 os
, (long long)object
, (long long) offset
, error
);
6076 ASSERT(db
->db_offset
== offset
);
6077 ASSERT(db
->db_size
== blocksize
);
6078 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
6079 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
6080 dmu_buf_will_fill(db
, tx
);
6081 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
6082 dmu_buf_rele(db
, FTAG
);
6086 txg_wait_synced(spa_get_dsl(spa
), txg
);
6089 * Find out what block we got.
6091 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
6092 DMU_READ_NO_PREFETCH
));
6093 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
6094 dmu_buf_rele(db
, FTAG
);
6097 * Damage the block. Dedup-ditto will save us when we read it later.
6099 psize
= BP_GET_PSIZE(&blk
);
6100 abd
= abd_alloc_linear(psize
, B_TRUE
);
6101 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
6103 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
6104 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
6105 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
6109 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6110 umem_free(od
, sizeof (ztest_od_t
));
6118 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6120 spa_t
*spa
= ztest_spa
;
6123 * Scrub in progress by device removal.
6125 if (ztest_device_removal_active
)
6128 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6129 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
6130 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6134 * Change the guid for the pool.
6138 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6140 spa_t
*spa
= ztest_spa
;
6141 uint64_t orig
, load
;
6144 if (ztest_opts
.zo_mmp_test
)
6147 orig
= spa_guid(spa
);
6148 load
= spa_load_guid(spa
);
6150 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6151 error
= spa_change_guid(spa
);
6152 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6157 if (ztest_opts
.zo_verbose
>= 4) {
6158 (void) printf("Changed guid old %llu -> %llu\n",
6159 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6162 VERIFY3U(orig
, !=, spa_guid(spa
));
6163 VERIFY3U(load
, ==, spa_load_guid(spa
));
6167 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6169 hrtime_t end
= gethrtime() + NANOSEC
;
6171 while (gethrtime() <= end
) {
6172 int run_count
= 100;
6174 struct abd
*abd_data
, *abd_meta
;
6179 zio_cksum_t zc_ref_byteswap
;
6181 size
= ztest_random_blocksize();
6183 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6184 abd_data
= abd_alloc(size
, B_FALSE
);
6185 abd_meta
= abd_alloc(size
, B_TRUE
);
6187 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6188 *ptr
= ztest_random(UINT_MAX
);
6190 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6191 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6193 VERIFY0(fletcher_4_impl_set("scalar"));
6194 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6195 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6197 VERIFY0(fletcher_4_impl_set("cycle"));
6198 while (run_count
-- > 0) {
6200 zio_cksum_t zc_byteswap
;
6202 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6203 fletcher_4_native(buf
, size
, NULL
, &zc
);
6205 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6206 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6207 sizeof (zc_byteswap
)));
6209 /* Test ABD - data */
6210 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6212 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6214 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6215 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6216 sizeof (zc_byteswap
)));
6218 /* Test ABD - metadata */
6219 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6221 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6223 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6224 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6225 sizeof (zc_byteswap
)));
6229 umem_free(buf
, size
);
6236 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6243 zio_cksum_t zc_ref_bswap
;
6245 hrtime_t end
= gethrtime() + NANOSEC
;
6247 while (gethrtime() <= end
) {
6248 int run_count
= 100;
6250 size
= ztest_random_blocksize();
6251 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6253 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6254 *ptr
= ztest_random(UINT_MAX
);
6256 VERIFY0(fletcher_4_impl_set("scalar"));
6257 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6258 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6260 VERIFY0(fletcher_4_impl_set("cycle"));
6262 while (run_count
-- > 0) {
6264 zio_cksum_t zc_bswap
;
6267 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6268 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6270 while (pos
< size
) {
6271 size_t inc
= 64 * ztest_random(size
/ 67);
6272 /* sometimes add few bytes to test non-simd */
6273 if (ztest_random(100) < 10)
6274 inc
+= P2ALIGN(ztest_random(64),
6277 if (inc
> (size
- pos
))
6280 fletcher_4_incremental_native(buf
+ pos
, inc
,
6282 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6288 VERIFY3U(pos
, ==, size
);
6290 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6291 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6294 * verify if incremental on the whole buffer is
6295 * equivalent to non-incremental version
6297 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6298 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6300 fletcher_4_incremental_native(buf
, size
, &zc
);
6301 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6303 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6304 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6307 umem_free(buf
, size
);
6312 ztest_check_path(char *path
)
6315 /* return true on success */
6316 return (!stat(path
, &s
));
6320 ztest_get_zdb_bin(char *bin
, int len
)
6324 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6325 * let popen to search through PATH.
6327 if ((zdb_path
= getenv("ZDB_PATH"))) {
6328 strlcpy(bin
, zdb_path
, len
); /* In env */
6329 if (!ztest_check_path(bin
)) {
6330 ztest_dump_core
= 0;
6331 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6336 VERIFY(realpath(getexecname(), bin
) != NULL
);
6337 if (strstr(bin
, "/ztest/")) {
6338 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6339 strcat(bin
, "/zdb/zdb");
6340 if (ztest_check_path(bin
))
6347 * Verify pool integrity by running zdb.
6350 ztest_run_zdb(char *pool
)
6356 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6359 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6360 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6361 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6363 ztest_get_zdb_bin(bin
, len
);
6366 "%s -bcc%s%s -G -d -U %s "
6367 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
6369 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6370 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6374 if (ztest_opts
.zo_verbose
>= 5)
6375 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6377 fp
= popen(zdb
, "r");
6379 while (fgets(zbuf
, 1024, fp
) != NULL
)
6380 if (ztest_opts
.zo_verbose
>= 3)
6381 (void) printf("%s", zbuf
);
6383 status
= pclose(fp
);
6388 ztest_dump_core
= 0;
6389 if (WIFEXITED(status
))
6390 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6392 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6394 umem_free(bin
, len
);
6395 umem_free(zdb
, len
);
6396 umem_free(zbuf
, 1024);
6400 ztest_walk_pool_directory(char *header
)
6404 if (ztest_opts
.zo_verbose
>= 6)
6405 (void) printf("%s\n", header
);
6407 mutex_enter(&spa_namespace_lock
);
6408 while ((spa
= spa_next(spa
)) != NULL
)
6409 if (ztest_opts
.zo_verbose
>= 6)
6410 (void) printf("\t%s\n", spa_name(spa
));
6411 mutex_exit(&spa_namespace_lock
);
6415 ztest_spa_import_export(char *oldname
, char *newname
)
6417 nvlist_t
*config
, *newconfig
;
6422 if (ztest_opts
.zo_verbose
>= 4) {
6423 (void) printf("import/export: old = %s, new = %s\n",
6428 * Clean up from previous runs.
6430 (void) spa_destroy(newname
);
6433 * Get the pool's configuration and guid.
6435 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6438 * Kick off a scrub to tickle scrub/export races.
6440 if (ztest_random(2) == 0)
6441 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6443 pool_guid
= spa_guid(spa
);
6444 spa_close(spa
, FTAG
);
6446 ztest_walk_pool_directory("pools before export");
6451 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6453 ztest_walk_pool_directory("pools after export");
6458 newconfig
= spa_tryimport(config
);
6459 ASSERT(newconfig
!= NULL
);
6460 nvlist_free(newconfig
);
6463 * Import it under the new name.
6465 error
= spa_import(newname
, config
, NULL
, 0);
6467 dump_nvlist(config
, 0);
6468 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6469 oldname
, newname
, error
);
6472 ztest_walk_pool_directory("pools after import");
6475 * Try to import it again -- should fail with EEXIST.
6477 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6480 * Try to import it under a different name -- should fail with EEXIST.
6482 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6485 * Verify that the pool is no longer visible under the old name.
6487 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6490 * Verify that we can open and close the pool using the new name.
6492 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6493 ASSERT(pool_guid
== spa_guid(spa
));
6494 spa_close(spa
, FTAG
);
6496 nvlist_free(config
);
6500 ztest_resume(spa_t
*spa
)
6502 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6503 (void) printf("resuming from suspended state\n");
6504 spa_vdev_state_enter(spa
, SCL_NONE
);
6505 vdev_clear(spa
, NULL
);
6506 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6507 (void) zio_resume(spa
);
6511 ztest_resume_thread(void *arg
)
6515 while (!ztest_exiting
) {
6516 if (spa_suspended(spa
))
6518 (void) poll(NULL
, 0, 100);
6521 * Periodically change the zfs_compressed_arc_enabled setting.
6523 if (ztest_random(10) == 0)
6524 zfs_compressed_arc_enabled
= ztest_random(2);
6527 * Periodically change the zfs_abd_scatter_enabled setting.
6529 if (ztest_random(10) == 0)
6530 zfs_abd_scatter_enabled
= ztest_random(2);
6533 * Periodically inject remapping delays (10% of the time).
6535 zfs_object_remap_one_indirect_delay_ms
=
6536 ztest_random(10) == 0 ? ztest_random(1000) + 1 : 0;
6543 ztest_deadman_thread(void *arg
)
6545 ztest_shared_t
*zs
= arg
;
6546 spa_t
*spa
= ztest_spa
;
6547 hrtime_t delay
, overdue
, last_run
= gethrtime();
6549 delay
= (zs
->zs_thread_stop
- zs
->zs_thread_start
) +
6550 MSEC2NSEC(zfs_deadman_synctime_ms
);
6552 while (!ztest_exiting
) {
6554 * Wait for the delay timer while checking occasionally
6555 * if we should stop.
6557 if (gethrtime() < last_run
+ delay
) {
6558 (void) poll(NULL
, 0, 1000);
6563 * If the pool is suspended then fail immediately. Otherwise,
6564 * check to see if the pool is making any progress. If
6565 * vdev_deadman() discovers that there hasn't been any recent
6566 * I/Os then it will end up aborting the tests.
6568 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6569 fatal(0, "aborting test after %llu seconds because "
6570 "pool has transitioned to a suspended state.",
6571 zfs_deadman_synctime_ms
/ 1000);
6573 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6576 * If the process doesn't complete within a grace period of
6577 * zfs_deadman_synctime_ms over the expected finish time,
6578 * then it may be hung and is terminated.
6580 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6581 if (gethrtime() > overdue
) {
6582 fatal(0, "aborting test after %llu seconds because "
6583 "the process is overdue for termination.",
6584 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6587 (void) printf("ztest has been running for %lld seconds\n",
6588 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6590 last_run
= gethrtime();
6591 delay
= MSEC2NSEC(zfs_deadman_checktime_ms
);
6598 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6600 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6601 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6602 hrtime_t functime
= gethrtime();
6605 for (i
= 0; i
< zi
->zi_iters
; i
++)
6606 zi
->zi_func(zd
, id
);
6608 functime
= gethrtime() - functime
;
6610 atomic_add_64(&zc
->zc_count
, 1);
6611 atomic_add_64(&zc
->zc_time
, functime
);
6613 if (ztest_opts
.zo_verbose
>= 4)
6614 (void) printf("%6.2f sec in %s\n",
6615 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6619 ztest_thread(void *arg
)
6622 uint64_t id
= (uintptr_t)arg
;
6623 ztest_shared_t
*zs
= ztest_shared
;
6627 ztest_shared_callstate_t
*zc
;
6629 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6631 * See if it's time to force a crash.
6633 if (now
> zs
->zs_thread_kill
)
6637 * If we're getting ENOSPC with some regularity, stop.
6639 if (zs
->zs_enospc_count
> 10)
6643 * Pick a random function to execute.
6645 rand
= ztest_random(ZTEST_FUNCS
);
6646 zi
= &ztest_info
[rand
];
6647 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6648 call_next
= zc
->zc_next
;
6650 if (now
>= call_next
&&
6651 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6652 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6653 ztest_execute(rand
, zi
, id
);
6661 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6663 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6667 ztest_dataset_destroy(int d
)
6669 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6672 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6674 if (ztest_opts
.zo_verbose
>= 3)
6675 (void) printf("Destroying %s to free up space\n", name
);
6678 * Cleanup any non-standard clones and snapshots. In general,
6679 * ztest thread t operates on dataset (t % zopt_datasets),
6680 * so there may be more than one thing to clean up.
6682 for (t
= d
; t
< ztest_opts
.zo_threads
;
6683 t
+= ztest_opts
.zo_datasets
)
6684 ztest_dsl_dataset_cleanup(name
, t
);
6686 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6687 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6691 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6693 uint64_t usedobjs
, dirobjs
, scratch
;
6696 * ZTEST_DIROBJ is the object directory for the entire dataset.
6697 * Therefore, the number of objects in use should equal the
6698 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6699 * If not, we have an object leak.
6701 * Note that we can only check this in ztest_dataset_open(),
6702 * when the open-context and syncing-context values agree.
6703 * That's because zap_count() returns the open-context value,
6704 * while dmu_objset_space() returns the rootbp fill count.
6706 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6707 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6708 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6712 ztest_dataset_open(int d
)
6714 ztest_ds_t
*zd
= &ztest_ds
[d
];
6715 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6718 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6721 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6723 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6725 error
= ztest_dataset_create(name
);
6726 if (error
== ENOSPC
) {
6727 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6728 ztest_record_enospc(FTAG
);
6731 ASSERT(error
== 0 || error
== EEXIST
);
6733 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6735 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6737 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6739 zilog
= zd
->zd_zilog
;
6741 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6742 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6743 fatal(0, "missing log records: claimed %llu < committed %llu",
6744 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6746 ztest_dataset_dirobj_verify(zd
);
6748 zil_replay(os
, zd
, ztest_replay_vector
);
6750 ztest_dataset_dirobj_verify(zd
);
6752 if (ztest_opts
.zo_verbose
>= 6)
6753 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6755 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6756 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6757 (u_longlong_t
)zilog
->zl_replaying_seq
);
6759 zilog
= zil_open(os
, ztest_get_data
);
6761 if (zilog
->zl_replaying_seq
!= 0 &&
6762 zilog
->zl_replaying_seq
< committed_seq
)
6763 fatal(0, "missing log records: replayed %llu < committed %llu",
6764 zilog
->zl_replaying_seq
, committed_seq
);
6770 ztest_dataset_close(int d
)
6772 ztest_ds_t
*zd
= &ztest_ds
[d
];
6774 zil_close(zd
->zd_zilog
);
6775 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6782 ztest_replay_zil_cb(const char *name
, void *arg
)
6787 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_ANY
, B_TRUE
,
6788 B_TRUE
, FTAG
, &os
));
6790 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
6792 ztest_zd_init(zdtmp
, NULL
, os
);
6793 zil_replay(os
, zdtmp
, ztest_replay_vector
);
6794 ztest_zd_fini(zdtmp
);
6796 if (dmu_objset_zil(os
)->zl_parse_lr_count
!= 0 &&
6797 ztest_opts
.zo_verbose
>= 6) {
6798 zilog_t
*zilog
= dmu_objset_zil(os
);
6800 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6802 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6803 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6804 (u_longlong_t
)zilog
->zl_replaying_seq
);
6807 umem_free(zdtmp
, sizeof (ztest_ds_t
));
6809 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6814 * Kick off threads to run tests on all datasets in parallel.
6817 ztest_run(ztest_shared_t
*zs
)
6821 kthread_t
*resume_thread
, *deadman_thread
;
6822 kthread_t
**run_threads
;
6827 ztest_exiting
= B_FALSE
;
6830 * Initialize parent/child shared state.
6832 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6833 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6834 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
6836 zs
->zs_thread_start
= gethrtime();
6837 zs
->zs_thread_stop
=
6838 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6839 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6840 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6841 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6842 zs
->zs_thread_kill
-=
6843 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6846 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6848 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6849 offsetof(ztest_cb_data_t
, zcd_node
));
6854 kernel_init(FREAD
| FWRITE
);
6855 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6856 metaslab_preload_limit
= ztest_random(20) + 1;
6859 dmu_objset_stats_t dds
;
6860 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
6861 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
6862 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6863 dmu_objset_fast_stat(os
, &dds
);
6864 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6865 zs
->zs_guid
= dds
.dds_guid
;
6866 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6868 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6871 * Create a thread to periodically resume suspended I/O.
6873 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
6874 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6877 * Create a deadman thread and set to panic if we hang.
6879 deadman_thread
= thread_create(NULL
, 0, ztest_deadman_thread
,
6880 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6882 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
6885 * Verify that we can safely inquire about any object,
6886 * whether it's allocated or not. To make it interesting,
6887 * we probe a 5-wide window around each power of two.
6888 * This hits all edge cases, including zero and the max.
6890 for (t
= 0; t
< 64; t
++) {
6891 for (d
= -5; d
<= 5; d
++) {
6892 error
= dmu_object_info(spa
->spa_meta_objset
,
6893 (1ULL << t
) + d
, NULL
);
6894 ASSERT(error
== 0 || error
== ENOENT
||
6900 * If we got any ENOSPC errors on the previous run, destroy something.
6902 if (zs
->zs_enospc_count
!= 0) {
6903 int d
= ztest_random(ztest_opts
.zo_datasets
);
6904 ztest_dataset_destroy(d
);
6906 zs
->zs_enospc_count
= 0;
6909 * If we were in the middle of ztest_device_removal() and were killed
6910 * we need to ensure the removal and scrub complete before running
6911 * any tests that check ztest_device_removal_active. The removal will
6912 * be restarted automatically when the spa is opened, but we need to
6913 * initate the scrub manually if it is not already in progress. Note
6914 * that we always run the scrub whenever an indirect vdev exists
6915 * because we have no way of knowing for sure if ztest_device_removal()
6916 * fully completed its scrub before the pool was reimported.
6918 if (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
||
6919 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6920 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
6921 txg_wait_synced(spa_get_dsl(spa
), 0);
6923 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6924 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
6925 txg_wait_synced(spa_get_dsl(spa
), 0);
6928 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
6931 if (ztest_opts
.zo_verbose
>= 4)
6932 (void) printf("starting main threads...\n");
6935 * Replay all logs of all datasets in the pool. This is primarily for
6936 * temporary datasets which wouldn't otherwise get replayed, which
6937 * can trigger failures when attempting to offline a SLOG in
6938 * ztest_fault_inject().
6940 (void) dmu_objset_find(ztest_opts
.zo_pool
, ztest_replay_zil_cb
,
6941 NULL
, DS_FIND_CHILDREN
);
6944 * Kick off all the tests that run in parallel.
6946 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6947 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
6948 umem_free(run_threads
, ztest_opts
.zo_threads
*
6949 sizeof (kthread_t
*));
6953 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
6954 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
6959 * Wait for all of the tests to complete.
6961 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++)
6962 VERIFY0(thread_join(run_threads
[t
]));
6965 * Close all datasets. This must be done after all the threads
6966 * are joined so we can be sure none of the datasets are in-use
6967 * by any of the threads.
6969 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6970 if (t
< ztest_opts
.zo_datasets
)
6971 ztest_dataset_close(t
);
6974 txg_wait_synced(spa_get_dsl(spa
), 0);
6976 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
6977 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
6979 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
6981 /* Kill the resume and deadman threads */
6982 ztest_exiting
= B_TRUE
;
6983 VERIFY0(thread_join(resume_thread
));
6984 VERIFY0(thread_join(deadman_thread
));
6988 * Right before closing the pool, kick off a bunch of async I/O;
6989 * spa_close() should wait for it to complete.
6991 for (object
= 1; object
< 50; object
++) {
6992 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
6993 ZIO_PRIORITY_SYNC_READ
);
6996 /* Verify that at least one commit cb was called in a timely fashion */
6997 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
6998 VERIFY0(zc_min_txg_delay
);
7000 spa_close(spa
, FTAG
);
7003 * Verify that we can loop over all pools.
7005 mutex_enter(&spa_namespace_lock
);
7006 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
7007 if (ztest_opts
.zo_verbose
> 3)
7008 (void) printf("spa_next: found %s\n", spa_name(spa
));
7009 mutex_exit(&spa_namespace_lock
);
7012 * Verify that we can export the pool and reimport it under a
7015 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7016 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7017 (void) snprintf(name
, sizeof (name
), "%s_import",
7018 ztest_opts
.zo_pool
);
7019 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7020 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7025 list_destroy(&zcl
.zcl_callbacks
);
7026 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7027 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7028 mutex_destroy(&ztest_vdev_lock
);
7029 mutex_destroy(&ztest_checkpoint_lock
);
7035 ztest_ds_t
*zd
= &ztest_ds
[0];
7039 if (ztest_opts
.zo_verbose
>= 3)
7040 (void) printf("testing spa_freeze()...\n");
7042 kernel_init(FREAD
| FWRITE
);
7043 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7044 VERIFY3U(0, ==, ztest_dataset_open(0));
7048 * Force the first log block to be transactionally allocated.
7049 * We have to do this before we freeze the pool -- otherwise
7050 * the log chain won't be anchored.
7052 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
7053 ztest_dmu_object_alloc_free(zd
, 0);
7054 zil_commit(zd
->zd_zilog
, 0);
7057 txg_wait_synced(spa_get_dsl(spa
), 0);
7060 * Freeze the pool. This stops spa_sync() from doing anything,
7061 * so that the only way to record changes from now on is the ZIL.
7066 * Because it is hard to predict how much space a write will actually
7067 * require beforehand, we leave ourselves some fudge space to write over
7070 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
7073 * Run tests that generate log records but don't alter the pool config
7074 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
7075 * We do a txg_wait_synced() after each iteration to force the txg
7076 * to increase well beyond the last synced value in the uberblock.
7077 * The ZIL should be OK with that.
7079 * Run a random number of times less than zo_maxloops and ensure we do
7080 * not run out of space on the pool.
7082 while (ztest_random(10) != 0 &&
7083 numloops
++ < ztest_opts
.zo_maxloops
&&
7084 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
7086 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
7087 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
7088 ztest_io(zd
, od
.od_object
,
7089 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
7090 txg_wait_synced(spa_get_dsl(spa
), 0);
7094 * Commit all of the changes we just generated.
7096 zil_commit(zd
->zd_zilog
, 0);
7097 txg_wait_synced(spa_get_dsl(spa
), 0);
7100 * Close our dataset and close the pool.
7102 ztest_dataset_close(0);
7103 spa_close(spa
, FTAG
);
7107 * Open and close the pool and dataset to induce log replay.
7109 kernel_init(FREAD
| FWRITE
);
7110 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7111 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
7112 VERIFY3U(0, ==, ztest_dataset_open(0));
7114 txg_wait_synced(spa_get_dsl(spa
), 0);
7115 ztest_dataset_close(0);
7116 ztest_reguid(NULL
, 0);
7118 spa_close(spa
, FTAG
);
7123 print_time(hrtime_t t
, char *timebuf
)
7125 hrtime_t s
= t
/ NANOSEC
;
7126 hrtime_t m
= s
/ 60;
7127 hrtime_t h
= m
/ 60;
7128 hrtime_t d
= h
/ 24;
7137 (void) sprintf(timebuf
,
7138 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7140 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7142 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7144 (void) sprintf(timebuf
, "%llus", s
);
7148 make_random_props(void)
7152 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7154 if (ztest_random(2) == 0)
7157 VERIFY0(nvlist_add_uint64(props
,
7158 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7164 * Import a storage pool with the given name.
7167 ztest_import(ztest_shared_t
*zs
)
7169 importargs_t args
= { 0 };
7171 nvlist_t
*cfg
= NULL
;
7173 char *searchdirs
[nsearch
];
7174 char *name
= ztest_opts
.zo_pool
;
7175 int flags
= ZFS_IMPORT_MISSING_LOG
;
7178 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7179 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7180 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7182 kernel_init(FREAD
| FWRITE
);
7184 searchdirs
[0] = ztest_opts
.zo_dir
;
7185 args
.paths
= nsearch
;
7186 args
.path
= searchdirs
;
7187 args
.can_be_active
= B_FALSE
;
7189 error
= zpool_find_config(NULL
, name
, &cfg
, &args
,
7190 &libzpool_config_ops
);
7192 (void) fatal(0, "No pools found\n");
7194 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
7195 VERIFY0(spa_open(name
, &spa
, FTAG
));
7196 zs
->zs_metaslab_sz
=
7197 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7198 spa_close(spa
, FTAG
);
7202 if (!ztest_opts
.zo_mmp_test
) {
7203 ztest_run_zdb(ztest_opts
.zo_pool
);
7205 ztest_run_zdb(ztest_opts
.zo_pool
);
7208 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7209 mutex_destroy(&ztest_vdev_lock
);
7210 mutex_destroy(&ztest_checkpoint_lock
);
7214 * Create a storage pool with the given name and initial vdev size.
7215 * Then test spa_freeze() functionality.
7218 ztest_init(ztest_shared_t
*zs
)
7221 nvlist_t
*nvroot
, *props
;
7224 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7225 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7226 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7228 kernel_init(FREAD
| FWRITE
);
7231 * Create the storage pool.
7233 (void) spa_destroy(ztest_opts
.zo_pool
);
7234 ztest_shared
->zs_vdev_next_leaf
= 0;
7236 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7237 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7238 NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
7239 props
= make_random_props();
7242 * We don't expect the pool to suspend unless maxfaults == 0,
7243 * in which case ztest_fault_inject() temporarily takes away
7244 * the only valid replica.
7246 VERIFY0(nvlist_add_uint64(props
,
7247 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7248 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7250 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7252 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7253 spa_feature_table
[i
].fi_uname
));
7254 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7258 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7259 nvlist_free(nvroot
);
7262 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7263 zs
->zs_metaslab_sz
=
7264 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7265 spa_close(spa
, FTAG
);
7269 if (!ztest_opts
.zo_mmp_test
) {
7270 ztest_run_zdb(ztest_opts
.zo_pool
);
7272 ztest_run_zdb(ztest_opts
.zo_pool
);
7275 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7276 mutex_destroy(&ztest_vdev_lock
);
7277 mutex_destroy(&ztest_checkpoint_lock
);
7283 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7285 ztest_fd_data
= mkstemp(ztest_name_data
);
7286 ASSERT3S(ztest_fd_data
, >=, 0);
7287 (void) unlink(ztest_name_data
);
7291 shared_data_size(ztest_shared_hdr_t
*hdr
)
7295 size
= hdr
->zh_hdr_size
;
7296 size
+= hdr
->zh_opts_size
;
7297 size
+= hdr
->zh_size
;
7298 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7299 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7308 ztest_shared_hdr_t
*hdr
;
7310 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7311 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7312 ASSERT(hdr
!= MAP_FAILED
);
7314 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7316 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7317 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7318 hdr
->zh_size
= sizeof (ztest_shared_t
);
7319 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7320 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7321 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7322 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7324 size
= shared_data_size(hdr
);
7325 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7327 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7334 ztest_shared_hdr_t
*hdr
;
7337 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7338 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7339 ASSERT(hdr
!= MAP_FAILED
);
7341 size
= shared_data_size(hdr
);
7343 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7344 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7345 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7346 ASSERT(hdr
!= MAP_FAILED
);
7347 buf
= (uint8_t *)hdr
;
7349 offset
= hdr
->zh_hdr_size
;
7350 ztest_shared_opts
= (void *)&buf
[offset
];
7351 offset
+= hdr
->zh_opts_size
;
7352 ztest_shared
= (void *)&buf
[offset
];
7353 offset
+= hdr
->zh_size
;
7354 ztest_shared_callstate
= (void *)&buf
[offset
];
7355 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7356 ztest_shared_ds
= (void *)&buf
[offset
];
7360 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7364 char *cmdbuf
= NULL
;
7369 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7370 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7375 fatal(1, "fork failed");
7377 if (pid
== 0) { /* child */
7378 char *emptyargv
[2] = { cmd
, NULL
};
7379 char fd_data_str
[12];
7381 struct rlimit rl
= { 1024, 1024 };
7382 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7384 (void) close(ztest_fd_rand
);
7385 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7386 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7388 (void) enable_extended_FILE_stdio(-1, -1);
7389 if (libpath
!= NULL
)
7390 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7391 (void) execv(cmd
, emptyargv
);
7392 ztest_dump_core
= B_FALSE
;
7393 fatal(B_TRUE
, "exec failed: %s", cmd
);
7396 if (cmdbuf
!= NULL
) {
7397 umem_free(cmdbuf
, MAXPATHLEN
);
7401 while (waitpid(pid
, &status
, 0) != pid
)
7403 if (statusp
!= NULL
)
7406 if (WIFEXITED(status
)) {
7407 if (WEXITSTATUS(status
) != 0) {
7408 (void) fprintf(stderr
, "child exited with code %d\n",
7409 WEXITSTATUS(status
));
7413 } else if (WIFSIGNALED(status
)) {
7414 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7415 (void) fprintf(stderr
, "child died with signal %d\n",
7421 (void) fprintf(stderr
, "something strange happened to child\n");
7428 ztest_run_init(void)
7432 ztest_shared_t
*zs
= ztest_shared
;
7435 * Blow away any existing copy of zpool.cache
7437 (void) remove(spa_config_path
);
7439 if (ztest_opts
.zo_init
== 0) {
7440 if (ztest_opts
.zo_verbose
>= 1)
7441 (void) printf("Importing pool %s\n",
7442 ztest_opts
.zo_pool
);
7448 * Create and initialize our storage pool.
7450 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7451 bzero(zs
, sizeof (ztest_shared_t
));
7452 if (ztest_opts
.zo_verbose
>= 3 &&
7453 ztest_opts
.zo_init
!= 1) {
7454 (void) printf("ztest_init(), pass %d\n", i
);
7461 main(int argc
, char **argv
)
7469 ztest_shared_callstate_t
*zc
;
7471 char numbuf
[NN_NUMBUF_SZ
];
7475 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7476 struct sigaction action
;
7478 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7480 dprintf_setup(&argc
, argv
);
7481 zfs_deadman_synctime_ms
= 300000;
7482 zfs_deadman_checktime_ms
= 30000;
7484 * As two-word space map entries may not come up often (especially
7485 * if pool and vdev sizes are small) we want to force at least some
7486 * of them so the feature get tested.
7488 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7491 * Verify that even extensively damaged split blocks with many
7492 * segments can be reconstructed in a reasonable amount of time
7493 * when reconstruction is known to be possible.
7495 * Note: the lower this value is, the more damage we inflict, and
7496 * the more time ztest spends in recovering that damage. We chose
7497 * to induce damage 1/100th of the time so recovery is tested but
7498 * not so frequently that ztest doesn't get to test other code paths.
7500 zfs_reconstruct_indirect_damage_fraction
= 100;
7502 action
.sa_handler
= sig_handler
;
7503 sigemptyset(&action
.sa_mask
);
7504 action
.sa_flags
= 0;
7506 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7507 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7512 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7513 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7519 * Force random_get_bytes() to use /dev/urandom in order to prevent
7520 * ztest from needlessly depleting the system entropy pool.
7522 random_path
= "/dev/urandom";
7523 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7524 ASSERT3S(ztest_fd_rand
, >=, 0);
7527 process_options(argc
, argv
);
7532 bcopy(&ztest_opts
, ztest_shared_opts
,
7533 sizeof (*ztest_shared_opts
));
7535 ztest_fd_data
= atoi(fd_data_str
);
7537 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7539 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7541 /* Override location of zpool.cache */
7542 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7543 ztest_opts
.zo_dir
) != -1);
7545 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7550 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7551 metaslab_df_alloc_threshold
=
7552 zs
->zs_metaslab_df_alloc_threshold
;
7561 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7563 if (ztest_opts
.zo_verbose
>= 1) {
7564 (void) printf("%llu vdevs, %d datasets, %d threads,"
7565 " %llu seconds...\n",
7566 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7567 ztest_opts
.zo_datasets
,
7568 ztest_opts
.zo_threads
,
7569 (u_longlong_t
)ztest_opts
.zo_time
);
7572 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7573 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7575 zs
->zs_do_init
= B_TRUE
;
7576 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7577 if (ztest_opts
.zo_verbose
>= 1) {
7578 (void) printf("Executing older ztest for "
7579 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7581 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7582 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7584 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7586 zs
->zs_do_init
= B_FALSE
;
7588 zs
->zs_proc_start
= gethrtime();
7589 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7591 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7592 zi
= &ztest_info
[f
];
7593 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7594 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7595 zc
->zc_next
= UINT64_MAX
;
7597 zc
->zc_next
= zs
->zs_proc_start
+
7598 ztest_random(2 * zi
->zi_interval
[0] + 1);
7602 * Run the tests in a loop. These tests include fault injection
7603 * to verify that self-healing data works, and forced crashes
7604 * to verify that we never lose on-disk consistency.
7606 while (gethrtime() < zs
->zs_proc_stop
) {
7611 * Initialize the workload counters for each function.
7613 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7614 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7619 /* Set the allocation switch size */
7620 zs
->zs_metaslab_df_alloc_threshold
=
7621 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7623 if (!hasalt
|| ztest_random(2) == 0) {
7624 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7625 (void) printf("Executing newer ztest: %s\n",
7629 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7631 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7632 (void) printf("Executing older ztest: %s\n",
7633 ztest_opts
.zo_alt_ztest
);
7636 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7637 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7644 if (ztest_opts
.zo_verbose
>= 1) {
7645 hrtime_t now
= gethrtime();
7647 now
= MIN(now
, zs
->zs_proc_stop
);
7648 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7649 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7651 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7652 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7654 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7655 (u_longlong_t
)zs
->zs_enospc_count
,
7656 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7658 100.0 * (now
- zs
->zs_proc_start
) /
7659 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7662 if (ztest_opts
.zo_verbose
>= 2) {
7663 (void) printf("\nWorkload summary:\n\n");
7664 (void) printf("%7s %9s %s\n",
7665 "Calls", "Time", "Function");
7666 (void) printf("%7s %9s %s\n",
7667 "-----", "----", "--------");
7668 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7669 zi
= &ztest_info
[f
];
7670 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7671 print_time(zc
->zc_time
, timebuf
);
7672 (void) printf("%7llu %9s %s\n",
7673 (u_longlong_t
)zc
->zc_count
, timebuf
,
7676 (void) printf("\n");
7679 if (!ztest_opts
.zo_mmp_test
)
7680 ztest_run_zdb(ztest_opts
.zo_pool
);
7683 if (ztest_opts
.zo_verbose
>= 1) {
7685 (void) printf("%d runs of older ztest: %s\n", older
,
7686 ztest_opts
.zo_alt_ztest
);
7687 (void) printf("%d runs of newer ztest: %s\n", newer
,
7690 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7691 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7694 umem_free(cmd
, MAXNAMELEN
);