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
;
219 static ztest_shared_opts_t
*ztest_shared_opts
;
220 static ztest_shared_opts_t ztest_opts
;
221 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
223 typedef struct ztest_shared_ds
{
227 static ztest_shared_ds_t
*ztest_shared_ds
;
228 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
230 #define BT_MAGIC 0x123456789abcdefULL
231 #define MAXFAULTS(zs) \
232 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
236 ZTEST_IO_WRITE_PATTERN
,
237 ZTEST_IO_WRITE_ZEROES
,
244 typedef struct ztest_block_tag
{
248 uint64_t bt_dnodesize
;
255 typedef struct bufwad
{
262 * It would be better to use a rangelock_t per object. Unfortunately
263 * the rangelock_t is not a drop-in replacement for rl_t, because we
264 * still need to map from object ID to rangelock_t.
286 #define ZTEST_RANGE_LOCKS 64
287 #define ZTEST_OBJECT_LOCKS 64
290 * Object descriptor. Used as a template for object lookup/create/remove.
292 typedef struct ztest_od
{
295 dmu_object_type_t od_type
;
296 dmu_object_type_t od_crtype
;
297 uint64_t od_blocksize
;
298 uint64_t od_crblocksize
;
299 uint64_t od_crdnodesize
;
302 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
308 typedef struct ztest_ds
{
309 ztest_shared_ds_t
*zd_shared
;
311 pthread_rwlock_t zd_zilog_lock
;
313 ztest_od_t
*zd_od
; /* debugging aid */
314 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
315 kmutex_t zd_dirobj_lock
;
316 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
317 rll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
321 * Per-iteration state.
323 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
325 typedef struct ztest_info
{
326 ztest_func_t
*zi_func
; /* test function */
327 uint64_t zi_iters
; /* iterations per execution */
328 uint64_t *zi_interval
; /* execute every <interval> seconds */
329 const char *zi_funcname
; /* name of test function */
332 typedef struct ztest_shared_callstate
{
333 uint64_t zc_count
; /* per-pass count */
334 uint64_t zc_time
; /* per-pass time */
335 uint64_t zc_next
; /* next time to call this function */
336 } ztest_shared_callstate_t
;
338 static ztest_shared_callstate_t
*ztest_shared_callstate
;
339 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
341 ztest_func_t ztest_dmu_read_write
;
342 ztest_func_t ztest_dmu_write_parallel
;
343 ztest_func_t ztest_dmu_object_alloc_free
;
344 ztest_func_t ztest_dmu_object_next_chunk
;
345 ztest_func_t ztest_dmu_commit_callbacks
;
346 ztest_func_t ztest_zap
;
347 ztest_func_t ztest_zap_parallel
;
348 ztest_func_t ztest_zil_commit
;
349 ztest_func_t ztest_zil_remount
;
350 ztest_func_t ztest_dmu_read_write_zcopy
;
351 ztest_func_t ztest_dmu_objset_create_destroy
;
352 ztest_func_t ztest_dmu_prealloc
;
353 ztest_func_t ztest_fzap
;
354 ztest_func_t ztest_dmu_snapshot_create_destroy
;
355 ztest_func_t ztest_dsl_prop_get_set
;
356 ztest_func_t ztest_spa_prop_get_set
;
357 ztest_func_t ztest_spa_create_destroy
;
358 ztest_func_t ztest_fault_inject
;
359 ztest_func_t ztest_ddt_repair
;
360 ztest_func_t ztest_dmu_snapshot_hold
;
361 ztest_func_t ztest_mmp_enable_disable
;
362 ztest_func_t ztest_scrub
;
363 ztest_func_t ztest_dsl_dataset_promote_busy
;
364 ztest_func_t ztest_vdev_attach_detach
;
365 ztest_func_t ztest_vdev_LUN_growth
;
366 ztest_func_t ztest_vdev_add_remove
;
367 ztest_func_t ztest_vdev_class_add
;
368 ztest_func_t ztest_vdev_aux_add_remove
;
369 ztest_func_t ztest_split_pool
;
370 ztest_func_t ztest_reguid
;
371 ztest_func_t ztest_spa_upgrade
;
372 ztest_func_t ztest_device_removal
;
373 ztest_func_t ztest_remap_blocks
;
374 ztest_func_t ztest_spa_checkpoint_create_discard
;
375 ztest_func_t ztest_fletcher
;
376 ztest_func_t ztest_fletcher_incr
;
377 ztest_func_t ztest_verify_dnode_bt
;
379 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
380 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
381 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
382 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
383 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
385 #define ZTI_INIT(func, iters, interval) \
386 { .zi_func = (func), \
387 .zi_iters = (iters), \
388 .zi_interval = (interval), \
389 .zi_funcname = # func }
391 ztest_info_t ztest_info
[] = {
392 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
393 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
394 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
395 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
396 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
397 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
398 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
399 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
400 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
401 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
402 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
403 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
404 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
405 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
407 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
409 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
410 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
411 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
412 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
413 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
414 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
415 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
416 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
417 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
418 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
419 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
420 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
421 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
422 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
423 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
424 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
425 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
426 ZTI_INIT(ztest_remap_blocks
, 1, &zopt_sometimes
),
427 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
428 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
429 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
430 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
433 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
436 * The following struct is used to hold a list of uncalled commit callbacks.
437 * The callbacks are ordered by txg number.
439 typedef struct ztest_cb_list
{
440 kmutex_t zcl_callbacks_lock
;
441 list_t zcl_callbacks
;
445 * Stuff we need to share writably between parent and child.
447 typedef struct ztest_shared
{
448 boolean_t zs_do_init
;
449 hrtime_t zs_proc_start
;
450 hrtime_t zs_proc_stop
;
451 hrtime_t zs_thread_start
;
452 hrtime_t zs_thread_stop
;
453 hrtime_t zs_thread_kill
;
454 uint64_t zs_enospc_count
;
455 uint64_t zs_vdev_next_leaf
;
456 uint64_t zs_vdev_aux
;
461 uint64_t zs_metaslab_sz
;
462 uint64_t zs_metaslab_df_alloc_threshold
;
466 #define ID_PARALLEL -1ULL
468 static char ztest_dev_template
[] = "%s/%s.%llua";
469 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
470 ztest_shared_t
*ztest_shared
;
472 static spa_t
*ztest_spa
= NULL
;
473 static ztest_ds_t
*ztest_ds
;
475 static kmutex_t ztest_vdev_lock
;
476 static boolean_t ztest_device_removal_active
= B_FALSE
;
477 static kmutex_t ztest_checkpoint_lock
;
480 * The ztest_name_lock protects the pool and dataset namespace used by
481 * the individual tests. To modify the namespace, consumers must grab
482 * this lock as writer. Grabbing the lock as reader will ensure that the
483 * namespace does not change while the lock is held.
485 static pthread_rwlock_t ztest_name_lock
;
487 static boolean_t ztest_dump_core
= B_TRUE
;
488 static boolean_t ztest_exiting
;
490 /* Global commit callback list */
491 static ztest_cb_list_t zcl
;
492 /* Commit cb delay */
493 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
494 static int zc_cb_counter
= 0;
497 * Minimum number of commit callbacks that need to be registered for us to check
498 * whether the minimum txg delay is acceptable.
500 #define ZTEST_COMMIT_CB_MIN_REG 100
503 * If a number of txgs equal to this threshold have been created after a commit
504 * callback has been registered but not called, then we assume there is an
505 * implementation bug.
507 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
510 ZTEST_META_DNODE
= 0,
515 static void usage(boolean_t
) __NORETURN
;
518 * These libumem hooks provide a reasonable set of defaults for the allocator's
519 * debugging facilities.
522 _umem_debug_init(void)
524 return ("default,verbose"); /* $UMEM_DEBUG setting */
528 _umem_logging_init(void)
530 return ("fail,contents"); /* $UMEM_LOGGING setting */
534 dump_debug_buffer(void)
536 ssize_t ret
__attribute__((unused
));
538 if (!ztest_opts
.zo_dump_dbgmsg
)
542 * We use write() instead of printf() so that this function
543 * is safe to call from a signal handler.
545 ret
= write(STDOUT_FILENO
, "\n", 1);
546 zfs_dbgmsg_print("ztest");
549 #define BACKTRACE_SZ 100
551 static void sig_handler(int signo
)
553 struct sigaction action
;
554 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
556 void *buffer
[BACKTRACE_SZ
];
558 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
559 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
564 * Restore default action and re-raise signal so SIGSEGV and
565 * SIGABRT can trigger a core dump.
567 action
.sa_handler
= SIG_DFL
;
568 sigemptyset(&action
.sa_mask
);
570 (void) sigaction(signo
, &action
, NULL
);
574 #define FATAL_MSG_SZ 1024
579 fatal(int do_perror
, char *message
, ...)
582 int save_errno
= errno
;
585 (void) fflush(stdout
);
586 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
588 va_start(args
, message
);
589 (void) sprintf(buf
, "ztest: ");
591 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
594 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
595 ": %s", strerror(save_errno
));
597 (void) fprintf(stderr
, "%s\n", buf
);
598 fatal_msg
= buf
; /* to ease debugging */
609 str2shift(const char *buf
)
611 const char *ends
= "BKMGTPEZ";
616 for (i
= 0; i
< strlen(ends
); i
++) {
617 if (toupper(buf
[0]) == ends
[i
])
620 if (i
== strlen(ends
)) {
621 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
625 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
628 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
634 nicenumtoull(const char *buf
)
639 val
= strtoull(buf
, &end
, 0);
641 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
643 } else if (end
[0] == '.') {
644 double fval
= strtod(buf
, &end
);
645 fval
*= pow(2, str2shift(end
));
646 if (fval
> UINT64_MAX
) {
647 (void) fprintf(stderr
, "ztest: value too large: %s\n",
651 val
= (uint64_t)fval
;
653 int shift
= str2shift(end
);
654 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
655 (void) fprintf(stderr
, "ztest: value too large: %s\n",
665 usage(boolean_t requested
)
667 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
669 char nice_vdev_size
[NN_NUMBUF_SZ
];
670 char nice_force_ganging
[NN_NUMBUF_SZ
];
671 FILE *fp
= requested
? stdout
: stderr
;
673 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
674 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
675 sizeof (nice_force_ganging
));
677 (void) fprintf(fp
, "Usage: %s\n"
678 "\t[-v vdevs (default: %llu)]\n"
679 "\t[-s size_of_each_vdev (default: %s)]\n"
680 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
681 "\t[-m mirror_copies (default: %d)]\n"
682 "\t[-r raidz_disks (default: %d)]\n"
683 "\t[-R raidz_parity (default: %d)]\n"
684 "\t[-d datasets (default: %d)]\n"
685 "\t[-t threads (default: %d)]\n"
686 "\t[-g gang_block_threshold (default: %s)]\n"
687 "\t[-i init_count (default: %d)] initialize pool i times\n"
688 "\t[-k kill_percentage (default: %llu%%)]\n"
689 "\t[-p pool_name (default: %s)]\n"
690 "\t[-f dir (default: %s)] file directory for vdev files\n"
691 "\t[-M] Multi-host simulate pool imported on remote host\n"
692 "\t[-V] verbose (use multiple times for ever more blather)\n"
693 "\t[-E] use existing pool instead of creating new one\n"
694 "\t[-T time (default: %llu sec)] total run time\n"
695 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
696 "\t[-P passtime (default: %llu sec)] time per pass\n"
697 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
698 "\t[-C vdev class state (default: random)] special=on|off|random\n"
699 "\t[-o variable=value] ... set global variable to an unsigned\n"
700 "\t 32-bit integer value\n"
701 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
702 "\t[-h] (print help)\n"
705 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
706 nice_vdev_size
, /* -s */
707 zo
->zo_ashift
, /* -a */
708 zo
->zo_mirrors
, /* -m */
709 zo
->zo_raidz
, /* -r */
710 zo
->zo_raidz_parity
, /* -R */
711 zo
->zo_datasets
, /* -d */
712 zo
->zo_threads
, /* -t */
713 nice_force_ganging
, /* -g */
714 zo
->zo_init
, /* -i */
715 (u_longlong_t
)zo
->zo_killrate
, /* -k */
716 zo
->zo_pool
, /* -p */
718 (u_longlong_t
)zo
->zo_time
, /* -T */
719 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
720 (u_longlong_t
)zo
->zo_passtime
);
721 exit(requested
? 0 : 1);
726 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
730 int state
= ZTEST_VDEV_CLASS_RND
;
732 (void) strlcpy(name
, input
, sizeof (name
));
734 value
= strchr(name
, '=');
736 (void) fprintf(stderr
, "missing value in property=value "
737 "'-C' argument (%s)\n", input
);
743 if (strcmp(value
, "on") == 0) {
744 state
= ZTEST_VDEV_CLASS_ON
;
745 } else if (strcmp(value
, "off") == 0) {
746 state
= ZTEST_VDEV_CLASS_OFF
;
747 } else if (strcmp(value
, "random") == 0) {
748 state
= ZTEST_VDEV_CLASS_RND
;
750 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
754 if (strcmp(name
, "special") == 0) {
755 zo
->zo_special_vdevs
= state
;
757 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
760 if (zo
->zo_verbose
>= 3)
761 (void) printf("%s vdev state is '%s'\n", name
, value
);
765 process_options(int argc
, char **argv
)
768 ztest_shared_opts_t
*zo
= &ztest_opts
;
772 char altdir
[MAXNAMELEN
] = { 0 };
774 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
776 while ((opt
= getopt(argc
, argv
,
777 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
794 value
= nicenumtoull(optarg
);
798 zo
->zo_vdevs
= value
;
801 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
804 zo
->zo_ashift
= value
;
807 zo
->zo_mirrors
= value
;
810 zo
->zo_raidz
= MAX(1, value
);
813 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
816 zo
->zo_datasets
= MAX(1, value
);
819 zo
->zo_threads
= MAX(1, value
);
822 zo
->zo_metaslab_force_ganging
=
823 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
829 zo
->zo_killrate
= value
;
832 (void) strlcpy(zo
->zo_pool
, optarg
,
833 sizeof (zo
->zo_pool
));
836 path
= realpath(optarg
, NULL
);
838 (void) fprintf(stderr
, "error: %s: %s\n",
839 optarg
, strerror(errno
));
842 (void) strlcpy(zo
->zo_dir
, path
,
843 sizeof (zo
->zo_dir
));
860 zo
->zo_passtime
= MAX(1, value
);
863 zo
->zo_maxloops
= MAX(1, value
);
866 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
869 ztest_parse_name_value(optarg
, zo
);
872 if (set_global_var(optarg
) != 0)
876 zo
->zo_dump_dbgmsg
= 1;
888 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
891 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
894 if (strlen(altdir
) > 0) {
902 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
903 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
905 VERIFY(NULL
!= realpath(getexecname(), cmd
));
906 if (0 != access(altdir
, F_OK
)) {
907 ztest_dump_core
= B_FALSE
;
908 fatal(B_TRUE
, "invalid alternate ztest path: %s",
911 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
914 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
915 * We want to extract <isa> to determine if we should use
916 * 32 or 64 bit binaries.
918 bin
= strstr(cmd
, "/usr/bin/");
919 ztest
= strstr(bin
, "/ztest");
921 isalen
= ztest
- isa
;
922 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
923 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
924 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
925 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
927 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
928 ztest_dump_core
= B_FALSE
;
929 fatal(B_TRUE
, "invalid alternate ztest: %s",
931 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
932 ztest_dump_core
= B_FALSE
;
933 fatal(B_TRUE
, "invalid alternate lib directory %s",
937 umem_free(cmd
, MAXPATHLEN
);
938 umem_free(realaltdir
, MAXPATHLEN
);
943 ztest_kill(ztest_shared_t
*zs
)
945 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
946 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
949 * Before we kill off ztest, make sure that the config is updated.
950 * See comment above spa_write_cachefile().
952 mutex_enter(&spa_namespace_lock
);
953 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
954 mutex_exit(&spa_namespace_lock
);
956 (void) kill(getpid(), SIGKILL
);
960 ztest_random(uint64_t range
)
964 ASSERT3S(ztest_fd_rand
, >=, 0);
969 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
970 fatal(1, "short read from /dev/urandom");
977 ztest_record_enospc(const char *s
)
979 ztest_shared
->zs_enospc_count
++;
983 ztest_get_ashift(void)
985 if (ztest_opts
.zo_ashift
== 0)
986 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
987 return (ztest_opts
.zo_ashift
);
991 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
997 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1000 ashift
= ztest_get_ashift();
1006 vdev
= ztest_shared
->zs_vdev_aux
;
1007 (void) snprintf(path
, MAXPATHLEN
,
1008 ztest_aux_template
, ztest_opts
.zo_dir
,
1009 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
1012 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
1013 (void) snprintf(path
, MAXPATHLEN
,
1014 ztest_dev_template
, ztest_opts
.zo_dir
,
1015 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1020 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1022 fatal(1, "can't open %s", path
);
1023 if (ftruncate(fd
, size
) != 0)
1024 fatal(1, "can't ftruncate %s", path
);
1028 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
1029 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
1030 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
1031 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
1032 umem_free(pathbuf
, MAXPATHLEN
);
1038 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
1039 uint64_t ashift
, int r
)
1041 nvlist_t
*raidz
, **child
;
1045 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1046 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1048 for (c
= 0; c
< r
; c
++)
1049 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1051 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
1052 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
1053 VDEV_TYPE_RAIDZ
) == 0);
1054 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
1055 ztest_opts
.zo_raidz_parity
) == 0);
1056 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
1059 for (c
= 0; c
< r
; c
++)
1060 nvlist_free(child
[c
]);
1062 umem_free(child
, r
* sizeof (nvlist_t
*));
1068 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1069 uint64_t ashift
, int r
, int m
)
1071 nvlist_t
*mirror
, **child
;
1075 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
1077 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1079 for (c
= 0; c
< m
; c
++)
1080 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1082 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1083 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1084 VDEV_TYPE_MIRROR
) == 0);
1085 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1088 for (c
= 0; c
< m
; c
++)
1089 nvlist_free(child
[c
]);
1091 umem_free(child
, m
* sizeof (nvlist_t
*));
1097 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1098 const char *class, int r
, int m
, int t
)
1100 nvlist_t
*root
, **child
;
1106 log
= (class != NULL
&& strcmp(class, "log") == 0);
1108 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1110 for (c
= 0; c
< t
; c
++) {
1111 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1113 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1116 if (class != NULL
&& class[0] != '\0') {
1117 ASSERT(m
> 1 || log
); /* expecting a mirror */
1118 VERIFY(nvlist_add_string(child
[c
],
1119 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1123 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1124 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1125 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1128 for (c
= 0; c
< t
; c
++)
1129 nvlist_free(child
[c
]);
1131 umem_free(child
, t
* sizeof (nvlist_t
*));
1137 * Find a random spa version. Returns back a random spa version in the
1138 * range [initial_version, SPA_VERSION_FEATURES].
1141 ztest_random_spa_version(uint64_t initial_version
)
1143 uint64_t version
= initial_version
;
1145 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1147 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1150 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1151 version
= SPA_VERSION_FEATURES
;
1153 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1158 ztest_random_blocksize(void)
1160 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1163 * Choose a block size >= the ashift.
1164 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1166 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1167 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1169 uint64_t block_shift
=
1170 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1171 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1175 ztest_random_dnodesize(void)
1178 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1180 if (max_slots
== DNODE_MIN_SLOTS
)
1181 return (DNODE_MIN_SIZE
);
1184 * Weight the random distribution more heavily toward smaller
1185 * dnode sizes since that is more likely to reflect real-world
1188 ASSERT3U(max_slots
, >, 4);
1189 switch (ztest_random(10)) {
1191 slots
= 5 + ztest_random(max_slots
- 4);
1194 slots
= 2 + ztest_random(3);
1201 return (slots
<< DNODE_SHIFT
);
1205 ztest_random_ibshift(void)
1207 return (DN_MIN_INDBLKSHIFT
+
1208 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1212 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1215 vdev_t
*rvd
= spa
->spa_root_vdev
;
1218 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1221 top
= ztest_random(rvd
->vdev_children
);
1222 tvd
= rvd
->vdev_child
[top
];
1223 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1224 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1230 ztest_random_dsl_prop(zfs_prop_t prop
)
1235 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1236 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1242 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1245 const char *propname
= zfs_prop_to_name(prop
);
1246 const char *valname
;
1251 error
= dsl_prop_set_int(osname
, propname
,
1252 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1254 if (error
== ENOSPC
) {
1255 ztest_record_enospc(FTAG
);
1260 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1261 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1263 if (ztest_opts
.zo_verbose
>= 6) {
1266 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1268 (void) printf("%s %s = %llu at '%s'\n", osname
,
1269 propname
, (unsigned long long)curval
, setpoint
);
1271 (void) printf("%s %s = %s at '%s'\n",
1272 osname
, propname
, valname
, setpoint
);
1274 umem_free(setpoint
, MAXPATHLEN
);
1280 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1282 spa_t
*spa
= ztest_spa
;
1283 nvlist_t
*props
= NULL
;
1286 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1287 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1289 error
= spa_prop_set(spa
, props
);
1293 if (error
== ENOSPC
) {
1294 ztest_record_enospc(FTAG
);
1303 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1304 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1308 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1310 strcpy(ddname
, name
);
1311 cp
= strchr(ddname
, '@');
1315 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1316 while (decrypt
&& err
== EACCES
) {
1317 dsl_crypto_params_t
*dcp
;
1318 nvlist_t
*crypto_args
= fnvlist_alloc();
1320 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1321 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1322 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1323 crypto_args
, &dcp
));
1324 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1325 dsl_crypto_params_free(dcp
, B_FALSE
);
1326 fnvlist_free(crypto_args
);
1328 if (err
== EINVAL
) {
1330 * We couldn't load a key for this dataset so try
1331 * the parent. This loop will eventually hit the
1332 * encryption root since ztest only makes clones
1333 * as children of their origin datasets.
1335 cp
= strrchr(ddname
, '/');
1342 } else if (err
!= 0) {
1346 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1354 ztest_rll_init(rll_t
*rll
)
1356 rll
->rll_writer
= NULL
;
1357 rll
->rll_readers
= 0;
1358 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1359 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1363 ztest_rll_destroy(rll_t
*rll
)
1365 ASSERT(rll
->rll_writer
== NULL
);
1366 ASSERT(rll
->rll_readers
== 0);
1367 mutex_destroy(&rll
->rll_lock
);
1368 cv_destroy(&rll
->rll_cv
);
1372 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1374 mutex_enter(&rll
->rll_lock
);
1376 if (type
== RL_READER
) {
1377 while (rll
->rll_writer
!= NULL
)
1378 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1381 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1382 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1383 rll
->rll_writer
= curthread
;
1386 mutex_exit(&rll
->rll_lock
);
1390 ztest_rll_unlock(rll_t
*rll
)
1392 mutex_enter(&rll
->rll_lock
);
1394 if (rll
->rll_writer
) {
1395 ASSERT(rll
->rll_readers
== 0);
1396 rll
->rll_writer
= NULL
;
1398 ASSERT(rll
->rll_readers
!= 0);
1399 ASSERT(rll
->rll_writer
== NULL
);
1403 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1404 cv_broadcast(&rll
->rll_cv
);
1406 mutex_exit(&rll
->rll_lock
);
1410 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1412 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1414 ztest_rll_lock(rll
, type
);
1418 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1420 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1422 ztest_rll_unlock(rll
);
1426 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1427 uint64_t size
, rl_type_t type
)
1429 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1430 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1433 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1434 rl
->rl_object
= object
;
1435 rl
->rl_offset
= offset
;
1439 ztest_rll_lock(rll
, type
);
1445 ztest_range_unlock(rl_t
*rl
)
1447 rll_t
*rll
= rl
->rl_lock
;
1449 ztest_rll_unlock(rll
);
1451 umem_free(rl
, sizeof (*rl
));
1455 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1458 zd
->zd_zilog
= dmu_objset_zil(os
);
1459 zd
->zd_shared
= szd
;
1460 dmu_objset_name(os
, zd
->zd_name
);
1463 if (zd
->zd_shared
!= NULL
)
1464 zd
->zd_shared
->zd_seq
= 0;
1466 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1467 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1469 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1470 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1472 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1473 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1477 ztest_zd_fini(ztest_ds_t
*zd
)
1481 mutex_destroy(&zd
->zd_dirobj_lock
);
1482 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1484 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1485 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1487 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1488 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1491 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1494 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1500 * Attempt to assign tx to some transaction group.
1502 error
= dmu_tx_assign(tx
, txg_how
);
1504 if (error
== ERESTART
) {
1505 ASSERT(txg_how
== TXG_NOWAIT
);
1508 ASSERT3U(error
, ==, ENOSPC
);
1509 ztest_record_enospc(tag
);
1514 txg
= dmu_tx_get_txg(tx
);
1520 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1523 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1531 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1534 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1538 diff
|= (value
- *ip
++);
1545 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1546 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1549 bt
->bt_magic
= BT_MAGIC
;
1550 bt
->bt_objset
= dmu_objset_id(os
);
1551 bt
->bt_object
= object
;
1552 bt
->bt_dnodesize
= dnodesize
;
1553 bt
->bt_offset
= offset
;
1556 bt
->bt_crtxg
= crtxg
;
1560 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1561 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1564 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1565 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1566 ASSERT3U(bt
->bt_object
, ==, object
);
1567 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1568 ASSERT3U(bt
->bt_offset
, ==, offset
);
1569 ASSERT3U(bt
->bt_gen
, <=, gen
);
1570 ASSERT3U(bt
->bt_txg
, <=, txg
);
1571 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1574 static ztest_block_tag_t
*
1575 ztest_bt_bonus(dmu_buf_t
*db
)
1577 dmu_object_info_t doi
;
1578 ztest_block_tag_t
*bt
;
1580 dmu_object_info_from_db(db
, &doi
);
1581 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1582 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1583 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1589 * Generate a token to fill up unused bonus buffer space. Try to make
1590 * it unique to the object, generation, and offset to verify that data
1591 * is not getting overwritten by data from other dnodes.
1593 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1594 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1597 * Fill up the unused bonus buffer region before the block tag with a
1598 * verifiable pattern. Filling the whole bonus area with non-zero data
1599 * helps ensure that all dnode traversal code properly skips the
1600 * interior regions of large dnodes.
1603 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1604 objset_t
*os
, uint64_t gen
)
1608 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1610 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1611 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1612 gen
, bonusp
- (uint64_t *)db
->db_data
);
1618 * Verify that the unused area of a bonus buffer is filled with the
1622 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1623 objset_t
*os
, uint64_t gen
)
1627 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1628 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1629 gen
, bonusp
- (uint64_t *)db
->db_data
);
1630 VERIFY3U(*bonusp
, ==, token
);
1638 #define lrz_type lr_mode
1639 #define lrz_blocksize lr_uid
1640 #define lrz_ibshift lr_gid
1641 #define lrz_bonustype lr_rdev
1642 #define lrz_dnodesize lr_crtime[1]
1645 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1647 char *name
= (void *)(lr
+ 1); /* name follows lr */
1648 size_t namesize
= strlen(name
) + 1;
1651 if (zil_replaying(zd
->zd_zilog
, tx
))
1654 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1655 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1656 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1658 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1662 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1664 char *name
= (void *)(lr
+ 1); /* name follows lr */
1665 size_t namesize
= strlen(name
) + 1;
1668 if (zil_replaying(zd
->zd_zilog
, tx
))
1671 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1672 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1673 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1675 itx
->itx_oid
= object
;
1676 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1680 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1683 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1685 if (zil_replaying(zd
->zd_zilog
, tx
))
1688 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1689 write_state
= WR_INDIRECT
;
1691 itx
= zil_itx_create(TX_WRITE
,
1692 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1694 if (write_state
== WR_COPIED
&&
1695 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1696 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1697 zil_itx_destroy(itx
);
1698 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1699 write_state
= WR_NEED_COPY
;
1701 itx
->itx_private
= zd
;
1702 itx
->itx_wr_state
= write_state
;
1703 itx
->itx_sync
= (ztest_random(8) == 0);
1705 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1706 sizeof (*lr
) - sizeof (lr_t
));
1708 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1712 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1716 if (zil_replaying(zd
->zd_zilog
, tx
))
1719 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1720 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1721 sizeof (*lr
) - sizeof (lr_t
));
1723 itx
->itx_sync
= B_FALSE
;
1724 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1728 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1732 if (zil_replaying(zd
->zd_zilog
, tx
))
1735 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1736 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1737 sizeof (*lr
) - sizeof (lr_t
));
1739 itx
->itx_sync
= B_FALSE
;
1740 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1747 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1749 ztest_ds_t
*zd
= arg1
;
1750 lr_create_t
*lr
= arg2
;
1751 char *name
= (void *)(lr
+ 1); /* name follows lr */
1752 objset_t
*os
= zd
->zd_os
;
1753 ztest_block_tag_t
*bbt
;
1761 byteswap_uint64_array(lr
, sizeof (*lr
));
1763 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1764 ASSERT(name
[0] != '\0');
1766 tx
= dmu_tx_create(os
);
1768 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1770 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1771 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1773 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1776 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1780 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1781 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1783 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1784 if (lr
->lr_foid
== 0) {
1785 lr
->lr_foid
= zap_create_dnsize(os
,
1786 lr
->lrz_type
, lr
->lrz_bonustype
,
1787 bonuslen
, lr
->lrz_dnodesize
, tx
);
1789 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1790 lr
->lrz_type
, lr
->lrz_bonustype
,
1791 bonuslen
, lr
->lrz_dnodesize
, tx
);
1794 if (lr
->lr_foid
== 0) {
1795 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1796 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1797 bonuslen
, lr
->lrz_dnodesize
, tx
);
1799 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1800 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1801 bonuslen
, lr
->lrz_dnodesize
, tx
);
1806 ASSERT3U(error
, ==, EEXIST
);
1807 ASSERT(zd
->zd_zilog
->zl_replay
);
1812 ASSERT(lr
->lr_foid
!= 0);
1814 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1815 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1816 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1818 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1819 bbt
= ztest_bt_bonus(db
);
1820 dmu_buf_will_dirty(db
, tx
);
1821 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1822 lr
->lr_gen
, txg
, txg
);
1823 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1824 dmu_buf_rele(db
, FTAG
);
1826 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1829 (void) ztest_log_create(zd
, tx
, lr
);
1837 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1839 ztest_ds_t
*zd
= arg1
;
1840 lr_remove_t
*lr
= arg2
;
1841 char *name
= (void *)(lr
+ 1); /* name follows lr */
1842 objset_t
*os
= zd
->zd_os
;
1843 dmu_object_info_t doi
;
1845 uint64_t object
, txg
;
1848 byteswap_uint64_array(lr
, sizeof (*lr
));
1850 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1851 ASSERT(name
[0] != '\0');
1854 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1855 ASSERT(object
!= 0);
1857 ztest_object_lock(zd
, object
, RL_WRITER
);
1859 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1861 tx
= dmu_tx_create(os
);
1863 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1864 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1866 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1868 ztest_object_unlock(zd
, object
);
1872 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1873 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1875 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1878 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1880 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1884 ztest_object_unlock(zd
, object
);
1890 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1892 ztest_ds_t
*zd
= arg1
;
1893 lr_write_t
*lr
= arg2
;
1894 objset_t
*os
= zd
->zd_os
;
1895 void *data
= lr
+ 1; /* data follows lr */
1896 uint64_t offset
, length
;
1897 ztest_block_tag_t
*bt
= data
;
1898 ztest_block_tag_t
*bbt
;
1899 uint64_t gen
, txg
, lrtxg
, crtxg
;
1900 dmu_object_info_t doi
;
1903 arc_buf_t
*abuf
= NULL
;
1907 byteswap_uint64_array(lr
, sizeof (*lr
));
1909 offset
= lr
->lr_offset
;
1910 length
= lr
->lr_length
;
1912 /* If it's a dmu_sync() block, write the whole block */
1913 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1914 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1915 if (length
< blocksize
) {
1916 offset
-= offset
% blocksize
;
1921 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1922 byteswap_uint64_array(bt
, sizeof (*bt
));
1924 if (bt
->bt_magic
!= BT_MAGIC
)
1927 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1928 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1930 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1932 dmu_object_info_from_db(db
, &doi
);
1934 bbt
= ztest_bt_bonus(db
);
1935 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1937 crtxg
= bbt
->bt_crtxg
;
1938 lrtxg
= lr
->lr_common
.lrc_txg
;
1940 tx
= dmu_tx_create(os
);
1942 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1944 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1945 P2PHASE(offset
, length
) == 0)
1946 abuf
= dmu_request_arcbuf(db
, length
);
1948 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1951 dmu_return_arcbuf(abuf
);
1952 dmu_buf_rele(db
, FTAG
);
1953 ztest_range_unlock(rl
);
1954 ztest_object_unlock(zd
, lr
->lr_foid
);
1960 * Usually, verify the old data before writing new data --
1961 * but not always, because we also want to verify correct
1962 * behavior when the data was not recently read into cache.
1964 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1965 if (ztest_random(4) != 0) {
1966 int prefetch
= ztest_random(2) ?
1967 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1968 ztest_block_tag_t rbt
;
1970 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1971 sizeof (rbt
), &rbt
, prefetch
) == 0);
1972 if (rbt
.bt_magic
== BT_MAGIC
) {
1973 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1974 offset
, gen
, txg
, crtxg
);
1979 * Writes can appear to be newer than the bonus buffer because
1980 * the ztest_get_data() callback does a dmu_read() of the
1981 * open-context data, which may be different than the data
1982 * as it was when the write was generated.
1984 if (zd
->zd_zilog
->zl_replay
) {
1985 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1986 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1991 * Set the bt's gen/txg to the bonus buffer's gen/txg
1992 * so that all of the usual ASSERTs will work.
1994 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
1999 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
2001 bcopy(data
, abuf
->b_data
, length
);
2002 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
2005 (void) ztest_log_write(zd
, tx
, lr
);
2007 dmu_buf_rele(db
, FTAG
);
2011 ztest_range_unlock(rl
);
2012 ztest_object_unlock(zd
, lr
->lr_foid
);
2018 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2020 ztest_ds_t
*zd
= arg1
;
2021 lr_truncate_t
*lr
= arg2
;
2022 objset_t
*os
= zd
->zd_os
;
2028 byteswap_uint64_array(lr
, sizeof (*lr
));
2030 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2031 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2034 tx
= dmu_tx_create(os
);
2036 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2038 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2040 ztest_range_unlock(rl
);
2041 ztest_object_unlock(zd
, lr
->lr_foid
);
2045 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2046 lr
->lr_length
, tx
) == 0);
2048 (void) ztest_log_truncate(zd
, tx
, lr
);
2052 ztest_range_unlock(rl
);
2053 ztest_object_unlock(zd
, lr
->lr_foid
);
2059 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2061 ztest_ds_t
*zd
= arg1
;
2062 lr_setattr_t
*lr
= arg2
;
2063 objset_t
*os
= zd
->zd_os
;
2066 ztest_block_tag_t
*bbt
;
2067 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2070 byteswap_uint64_array(lr
, sizeof (*lr
));
2072 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2074 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2076 tx
= dmu_tx_create(os
);
2077 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2079 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2081 dmu_buf_rele(db
, FTAG
);
2082 ztest_object_unlock(zd
, lr
->lr_foid
);
2086 bbt
= ztest_bt_bonus(db
);
2087 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2088 crtxg
= bbt
->bt_crtxg
;
2089 lrtxg
= lr
->lr_common
.lrc_txg
;
2090 dnodesize
= bbt
->bt_dnodesize
;
2092 if (zd
->zd_zilog
->zl_replay
) {
2093 ASSERT(lr
->lr_size
!= 0);
2094 ASSERT(lr
->lr_mode
!= 0);
2098 * Randomly change the size and increment the generation.
2100 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2102 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2107 * Verify that the current bonus buffer is not newer than our txg.
2109 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2110 MAX(txg
, lrtxg
), crtxg
);
2112 dmu_buf_will_dirty(db
, tx
);
2114 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2115 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2116 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2117 bbt
= ztest_bt_bonus(db
);
2119 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2121 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2122 dmu_buf_rele(db
, FTAG
);
2124 (void) ztest_log_setattr(zd
, tx
, lr
);
2128 ztest_object_unlock(zd
, lr
->lr_foid
);
2133 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2134 NULL
, /* 0 no such transaction type */
2135 ztest_replay_create
, /* TX_CREATE */
2136 NULL
, /* TX_MKDIR */
2137 NULL
, /* TX_MKXATTR */
2138 NULL
, /* TX_SYMLINK */
2139 ztest_replay_remove
, /* TX_REMOVE */
2140 NULL
, /* TX_RMDIR */
2142 NULL
, /* TX_RENAME */
2143 ztest_replay_write
, /* TX_WRITE */
2144 ztest_replay_truncate
, /* TX_TRUNCATE */
2145 ztest_replay_setattr
, /* TX_SETATTR */
2147 NULL
, /* TX_CREATE_ACL */
2148 NULL
, /* TX_CREATE_ATTR */
2149 NULL
, /* TX_CREATE_ACL_ATTR */
2150 NULL
, /* TX_MKDIR_ACL */
2151 NULL
, /* TX_MKDIR_ATTR */
2152 NULL
, /* TX_MKDIR_ACL_ATTR */
2153 NULL
, /* TX_WRITE2 */
2157 * ZIL get_data callbacks
2162 ztest_get_done(zgd_t
*zgd
, int error
)
2164 ztest_ds_t
*zd
= zgd
->zgd_private
;
2165 uint64_t object
= ((rl_t
*)zgd
->zgd_lr
)->rl_object
;
2168 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2170 ztest_range_unlock((rl_t
*)zgd
->zgd_lr
);
2171 ztest_object_unlock(zd
, object
);
2173 umem_free(zgd
, sizeof (*zgd
));
2177 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2180 ztest_ds_t
*zd
= arg
;
2181 objset_t
*os
= zd
->zd_os
;
2182 uint64_t object
= lr
->lr_foid
;
2183 uint64_t offset
= lr
->lr_offset
;
2184 uint64_t size
= lr
->lr_length
;
2185 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2187 dmu_object_info_t doi
;
2192 ASSERT3P(lwb
, !=, NULL
);
2193 ASSERT3P(zio
, !=, NULL
);
2194 ASSERT3U(size
, !=, 0);
2196 ztest_object_lock(zd
, object
, RL_READER
);
2197 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2199 ztest_object_unlock(zd
, object
);
2203 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2205 if (crtxg
== 0 || crtxg
> txg
) {
2206 dmu_buf_rele(db
, FTAG
);
2207 ztest_object_unlock(zd
, object
);
2211 dmu_object_info_from_db(db
, &doi
);
2212 dmu_buf_rele(db
, FTAG
);
2215 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2217 zgd
->zgd_private
= zd
;
2219 if (buf
!= NULL
) { /* immediate write */
2220 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2221 object
, offset
, size
, RL_READER
);
2223 error
= dmu_read(os
, object
, offset
, size
, buf
,
2224 DMU_READ_NO_PREFETCH
);
2227 size
= doi
.doi_data_block_size
;
2229 offset
= P2ALIGN(offset
, size
);
2231 ASSERT(offset
< size
);
2235 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2236 object
, offset
, size
, RL_READER
);
2238 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2239 DMU_READ_NO_PREFETCH
);
2242 blkptr_t
*bp
= &lr
->lr_blkptr
;
2247 ASSERT(db
->db_offset
== offset
);
2248 ASSERT(db
->db_size
== size
);
2250 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2251 ztest_get_done
, zgd
);
2258 ztest_get_done(zgd
, error
);
2264 ztest_lr_alloc(size_t lrsize
, char *name
)
2267 size_t namesize
= name
? strlen(name
) + 1 : 0;
2269 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2272 bcopy(name
, lr
+ lrsize
, namesize
);
2278 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2280 size_t namesize
= name
? strlen(name
) + 1 : 0;
2282 umem_free(lr
, lrsize
+ namesize
);
2286 * Lookup a bunch of objects. Returns the number of objects not found.
2289 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2295 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2297 for (i
= 0; i
< count
; i
++, od
++) {
2299 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2300 sizeof (uint64_t), 1, &od
->od_object
);
2302 ASSERT(error
== ENOENT
);
2303 ASSERT(od
->od_object
== 0);
2307 ztest_block_tag_t
*bbt
;
2308 dmu_object_info_t doi
;
2310 ASSERT(od
->od_object
!= 0);
2311 ASSERT(missing
== 0); /* there should be no gaps */
2313 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2314 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2315 od
->od_object
, FTAG
, &db
));
2316 dmu_object_info_from_db(db
, &doi
);
2317 bbt
= ztest_bt_bonus(db
);
2318 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2319 od
->od_type
= doi
.doi_type
;
2320 od
->od_blocksize
= doi
.doi_data_block_size
;
2321 od
->od_gen
= bbt
->bt_gen
;
2322 dmu_buf_rele(db
, FTAG
);
2323 ztest_object_unlock(zd
, od
->od_object
);
2331 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2336 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2338 for (i
= 0; i
< count
; i
++, od
++) {
2345 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2347 lr
->lr_doid
= od
->od_dir
;
2348 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2349 lr
->lrz_type
= od
->od_crtype
;
2350 lr
->lrz_blocksize
= od
->od_crblocksize
;
2351 lr
->lrz_ibshift
= ztest_random_ibshift();
2352 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2353 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2354 lr
->lr_gen
= od
->od_crgen
;
2355 lr
->lr_crtime
[0] = time(NULL
);
2357 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2358 ASSERT(missing
== 0);
2362 od
->od_object
= lr
->lr_foid
;
2363 od
->od_type
= od
->od_crtype
;
2364 od
->od_blocksize
= od
->od_crblocksize
;
2365 od
->od_gen
= od
->od_crgen
;
2366 ASSERT(od
->od_object
!= 0);
2369 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2376 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2382 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2386 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2393 * No object was found.
2395 if (od
->od_object
== 0)
2398 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2400 lr
->lr_doid
= od
->od_dir
;
2402 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2403 ASSERT3U(error
, ==, ENOSPC
);
2408 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2415 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2421 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2423 lr
->lr_foid
= object
;
2424 lr
->lr_offset
= offset
;
2425 lr
->lr_length
= size
;
2427 BP_ZERO(&lr
->lr_blkptr
);
2429 bcopy(data
, lr
+ 1, size
);
2431 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2433 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2439 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2444 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2446 lr
->lr_foid
= object
;
2447 lr
->lr_offset
= offset
;
2448 lr
->lr_length
= size
;
2450 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2452 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2458 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2463 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2465 lr
->lr_foid
= object
;
2469 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2471 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2477 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2479 objset_t
*os
= zd
->zd_os
;
2484 txg_wait_synced(dmu_objset_pool(os
), 0);
2486 ztest_object_lock(zd
, object
, RL_READER
);
2487 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2489 tx
= dmu_tx_create(os
);
2491 dmu_tx_hold_write(tx
, object
, offset
, size
);
2493 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2496 dmu_prealloc(os
, object
, offset
, size
, tx
);
2498 txg_wait_synced(dmu_objset_pool(os
), txg
);
2500 (void) dmu_free_long_range(os
, object
, offset
, size
);
2503 ztest_range_unlock(rl
);
2504 ztest_object_unlock(zd
, object
);
2508 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2511 ztest_block_tag_t wbt
;
2512 dmu_object_info_t doi
;
2513 enum ztest_io_type io_type
;
2517 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2518 blocksize
= doi
.doi_data_block_size
;
2519 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2522 * Pick an i/o type at random, biased toward writing block tags.
2524 io_type
= ztest_random(ZTEST_IO_TYPES
);
2525 if (ztest_random(2) == 0)
2526 io_type
= ZTEST_IO_WRITE_TAG
;
2528 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2532 case ZTEST_IO_WRITE_TAG
:
2533 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2535 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2538 case ZTEST_IO_WRITE_PATTERN
:
2539 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2540 if (ztest_random(2) == 0) {
2542 * Induce fletcher2 collisions to ensure that
2543 * zio_ddt_collision() detects and resolves them
2544 * when using fletcher2-verify for deduplication.
2546 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2547 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2549 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2552 case ZTEST_IO_WRITE_ZEROES
:
2553 bzero(data
, blocksize
);
2554 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2557 case ZTEST_IO_TRUNCATE
:
2558 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2561 case ZTEST_IO_SETATTR
:
2562 (void) ztest_setattr(zd
, object
);
2567 case ZTEST_IO_REWRITE
:
2568 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2569 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2570 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2572 VERIFY(err
== 0 || err
== ENOSPC
);
2573 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2574 ZFS_PROP_COMPRESSION
,
2575 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2577 VERIFY(err
== 0 || err
== ENOSPC
);
2578 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2580 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2581 DMU_READ_NO_PREFETCH
));
2583 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2587 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2589 umem_free(data
, blocksize
);
2593 * Initialize an object description template.
2596 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2597 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2600 od
->od_dir
= ZTEST_DIROBJ
;
2603 od
->od_crtype
= type
;
2604 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2605 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2608 od
->od_type
= DMU_OT_NONE
;
2609 od
->od_blocksize
= 0;
2612 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2613 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2617 * Lookup or create the objects for a test using the od template.
2618 * If the objects do not all exist, or if 'remove' is specified,
2619 * remove any existing objects and create new ones. Otherwise,
2620 * use the existing objects.
2623 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2625 int count
= size
/ sizeof (*od
);
2628 mutex_enter(&zd
->zd_dirobj_lock
);
2629 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2630 (ztest_remove(zd
, od
, count
) != 0 ||
2631 ztest_create(zd
, od
, count
) != 0))
2634 mutex_exit(&zd
->zd_dirobj_lock
);
2641 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2643 zilog_t
*zilog
= zd
->zd_zilog
;
2645 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2647 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2650 * Remember the committed values in zd, which is in parent/child
2651 * shared memory. If we die, the next iteration of ztest_run()
2652 * will verify that the log really does contain this record.
2654 mutex_enter(&zilog
->zl_lock
);
2655 ASSERT(zd
->zd_shared
!= NULL
);
2656 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2657 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2658 mutex_exit(&zilog
->zl_lock
);
2660 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2664 * This function is designed to simulate the operations that occur during a
2665 * mount/unmount operation. We hold the dataset across these operations in an
2666 * attempt to expose any implicit assumptions about ZIL management.
2670 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2672 objset_t
*os
= zd
->zd_os
;
2675 * We hold the ztest_vdev_lock so we don't cause problems with
2676 * other threads that wish to remove a log device, such as
2677 * ztest_device_removal().
2679 mutex_enter(&ztest_vdev_lock
);
2682 * We grab the zd_dirobj_lock to ensure that no other thread is
2683 * updating the zil (i.e. adding in-memory log records) and the
2684 * zd_zilog_lock to block any I/O.
2686 mutex_enter(&zd
->zd_dirobj_lock
);
2687 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2689 /* zfsvfs_teardown() */
2690 zil_close(zd
->zd_zilog
);
2692 /* zfsvfs_setup() */
2693 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2694 zil_replay(os
, zd
, ztest_replay_vector
);
2696 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2697 mutex_exit(&zd
->zd_dirobj_lock
);
2698 mutex_exit(&ztest_vdev_lock
);
2702 * Verify that we can't destroy an active pool, create an existing pool,
2703 * or create a pool with a bad vdev spec.
2707 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2709 ztest_shared_opts_t
*zo
= &ztest_opts
;
2713 if (zo
->zo_mmp_test
)
2717 * Attempt to create using a bad file.
2719 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2720 VERIFY3U(ENOENT
, ==,
2721 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2722 nvlist_free(nvroot
);
2725 * Attempt to create using a bad mirror.
2727 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2728 VERIFY3U(ENOENT
, ==,
2729 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2730 nvlist_free(nvroot
);
2733 * Attempt to create an existing pool. It shouldn't matter
2734 * what's in the nvroot; we should fail with EEXIST.
2736 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2737 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2738 VERIFY3U(EEXIST
, ==,
2739 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2740 nvlist_free(nvroot
);
2741 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2742 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2743 spa_close(spa
, FTAG
);
2745 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2749 * Start and then stop the MMP threads to ensure the startup and shutdown code
2750 * works properly. Actual protection and property-related code tested via ZTS.
2754 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2756 ztest_shared_opts_t
*zo
= &ztest_opts
;
2757 spa_t
*spa
= ztest_spa
;
2759 if (zo
->zo_mmp_test
)
2762 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2763 mutex_enter(&spa
->spa_props_lock
);
2765 zfs_multihost_fail_intervals
= 0;
2767 if (!spa_multihost(spa
)) {
2768 spa
->spa_multihost
= B_TRUE
;
2769 mmp_thread_start(spa
);
2772 mutex_exit(&spa
->spa_props_lock
);
2773 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2775 txg_wait_synced(spa_get_dsl(spa
), 0);
2776 mmp_signal_all_threads();
2777 txg_wait_synced(spa_get_dsl(spa
), 0);
2779 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2780 mutex_enter(&spa
->spa_props_lock
);
2782 if (spa_multihost(spa
)) {
2783 mmp_thread_stop(spa
);
2784 spa
->spa_multihost
= B_FALSE
;
2787 mutex_exit(&spa
->spa_props_lock
);
2788 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2793 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2796 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2797 uint64_t version
, newversion
;
2798 nvlist_t
*nvroot
, *props
;
2801 if (ztest_opts
.zo_mmp_test
)
2804 mutex_enter(&ztest_vdev_lock
);
2805 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2808 * Clean up from previous runs.
2810 (void) spa_destroy(name
);
2812 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2813 NULL
, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2816 * If we're configuring a RAIDZ device then make sure that the
2817 * the initial version is capable of supporting that feature.
2819 switch (ztest_opts
.zo_raidz_parity
) {
2822 initial_version
= SPA_VERSION_INITIAL
;
2825 initial_version
= SPA_VERSION_RAIDZ2
;
2828 initial_version
= SPA_VERSION_RAIDZ3
;
2833 * Create a pool with a spa version that can be upgraded. Pick
2834 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2837 version
= ztest_random_spa_version(initial_version
);
2838 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2840 props
= fnvlist_alloc();
2841 fnvlist_add_uint64(props
,
2842 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2843 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2844 fnvlist_free(nvroot
);
2845 fnvlist_free(props
);
2847 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2848 VERIFY3U(spa_version(spa
), ==, version
);
2849 newversion
= ztest_random_spa_version(version
+ 1);
2851 if (ztest_opts
.zo_verbose
>= 4) {
2852 (void) printf("upgrading spa version from %llu to %llu\n",
2853 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2856 spa_upgrade(spa
, newversion
);
2857 VERIFY3U(spa_version(spa
), >, version
);
2858 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2859 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2860 spa_close(spa
, FTAG
);
2863 mutex_exit(&ztest_vdev_lock
);
2867 ztest_spa_checkpoint(spa_t
*spa
)
2869 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2871 int error
= spa_checkpoint(spa
->spa_name
);
2875 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2876 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2877 case ZFS_ERR_CHECKPOINT_EXISTS
:
2880 ztest_record_enospc(FTAG
);
2883 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
2888 ztest_spa_discard_checkpoint(spa_t
*spa
)
2890 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2892 int error
= spa_checkpoint_discard(spa
->spa_name
);
2896 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2897 case ZFS_ERR_NO_CHECKPOINT
:
2900 fatal(0, "spa_discard_checkpoint(%s) = %d",
2901 spa
->spa_name
, error
);
2908 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
2910 spa_t
*spa
= ztest_spa
;
2912 mutex_enter(&ztest_checkpoint_lock
);
2913 if (ztest_random(2) == 0) {
2914 ztest_spa_checkpoint(spa
);
2916 ztest_spa_discard_checkpoint(spa
);
2918 mutex_exit(&ztest_checkpoint_lock
);
2923 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2928 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2931 for (c
= 0; c
< vd
->vdev_children
; c
++)
2932 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2940 * Find the first available hole which can be used as a top-level.
2943 find_vdev_hole(spa_t
*spa
)
2945 vdev_t
*rvd
= spa
->spa_root_vdev
;
2948 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2950 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2951 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2953 if (cvd
->vdev_ishole
)
2960 * Verify that vdev_add() works as expected.
2964 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2966 ztest_shared_t
*zs
= ztest_shared
;
2967 spa_t
*spa
= ztest_spa
;
2973 if (ztest_opts
.zo_mmp_test
)
2976 mutex_enter(&ztest_vdev_lock
);
2977 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2979 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2981 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2984 * If we have slogs then remove them 1/4 of the time.
2986 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2987 metaslab_group_t
*mg
;
2990 * find the first real slog in log allocation class
2992 mg
= spa_log_class(spa
)->mc_rotor
;
2993 while (!mg
->mg_vd
->vdev_islog
)
2996 guid
= mg
->mg_vd
->vdev_guid
;
2998 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3001 * We have to grab the zs_name_lock as writer to
3002 * prevent a race between removing a slog (dmu_objset_find)
3003 * and destroying a dataset. Removing the slog will
3004 * grab a reference on the dataset which may cause
3005 * dsl_destroy_head() to fail with EBUSY thus
3006 * leaving the dataset in an inconsistent state.
3008 pthread_rwlock_wrlock(&ztest_name_lock
);
3009 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3010 pthread_rwlock_unlock(&ztest_name_lock
);
3014 case EEXIST
: /* Generic zil_reset() error */
3015 case EBUSY
: /* Replay required */
3016 case EACCES
: /* Crypto key not loaded */
3017 case ZFS_ERR_CHECKPOINT_EXISTS
:
3018 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3021 fatal(0, "spa_vdev_remove() = %d", error
);
3024 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3027 * Make 1/4 of the devices be log devices
3029 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3030 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3031 "log" : NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3033 error
= spa_vdev_add(spa
, nvroot
);
3034 nvlist_free(nvroot
);
3040 ztest_record_enospc("spa_vdev_add");
3043 fatal(0, "spa_vdev_add() = %d", error
);
3047 mutex_exit(&ztest_vdev_lock
);
3052 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3054 ztest_shared_t
*zs
= ztest_shared
;
3055 spa_t
*spa
= ztest_spa
;
3058 const char *class = (ztest_random(2) == 0) ?
3059 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3063 * By default add a special vdev 50% of the time
3065 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3066 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3067 ztest_random(2) == 0)) {
3071 mutex_enter(&ztest_vdev_lock
);
3073 /* Only test with mirrors */
3074 if (zs
->zs_mirrors
< 2) {
3075 mutex_exit(&ztest_vdev_lock
);
3079 /* requires feature@allocation_classes */
3080 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3081 mutex_exit(&ztest_vdev_lock
);
3085 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3087 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3088 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
3089 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3091 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3092 class, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3094 error
= spa_vdev_add(spa
, nvroot
);
3095 nvlist_free(nvroot
);
3097 if (error
== ENOSPC
)
3098 ztest_record_enospc("spa_vdev_add");
3099 else if (error
!= 0)
3100 fatal(0, "spa_vdev_add() = %d", error
);
3103 * 50% of the time allow small blocks in the special class
3106 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3107 if (ztest_opts
.zo_verbose
>= 3)
3108 (void) printf("Enabling special VDEV small blocks\n");
3109 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3110 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3113 mutex_exit(&ztest_vdev_lock
);
3115 if (ztest_opts
.zo_verbose
>= 3) {
3116 metaslab_class_t
*mc
;
3118 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3119 mc
= spa_special_class(spa
);
3121 mc
= spa_dedup_class(spa
);
3122 (void) printf("Added a %s mirrored vdev (of %d)\n",
3123 class, (int)mc
->mc_groups
);
3128 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3132 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3134 ztest_shared_t
*zs
= ztest_shared
;
3135 spa_t
*spa
= ztest_spa
;
3136 vdev_t
*rvd
= spa
->spa_root_vdev
;
3137 spa_aux_vdev_t
*sav
;
3143 if (ztest_opts
.zo_mmp_test
)
3146 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3148 if (ztest_random(2) == 0) {
3149 sav
= &spa
->spa_spares
;
3150 aux
= ZPOOL_CONFIG_SPARES
;
3152 sav
= &spa
->spa_l2cache
;
3153 aux
= ZPOOL_CONFIG_L2CACHE
;
3156 mutex_enter(&ztest_vdev_lock
);
3158 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3160 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3162 * Pick a random device to remove.
3164 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3167 * Find an unused device we can add.
3169 zs
->zs_vdev_aux
= 0;
3172 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3173 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3175 for (c
= 0; c
< sav
->sav_count
; c
++)
3176 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3179 if (c
== sav
->sav_count
&&
3180 vdev_lookup_by_path(rvd
, path
) == NULL
)
3186 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3192 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3193 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3194 error
= spa_vdev_add(spa
, nvroot
);
3200 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3202 nvlist_free(nvroot
);
3205 * Remove an existing device. Sometimes, dirty its
3206 * vdev state first to make sure we handle removal
3207 * of devices that have pending state changes.
3209 if (ztest_random(2) == 0)
3210 (void) vdev_online(spa
, guid
, 0, NULL
);
3212 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3217 case ZFS_ERR_CHECKPOINT_EXISTS
:
3218 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3221 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3225 mutex_exit(&ztest_vdev_lock
);
3227 umem_free(path
, MAXPATHLEN
);
3231 * split a pool if it has mirror tlvdevs
3235 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3237 ztest_shared_t
*zs
= ztest_shared
;
3238 spa_t
*spa
= ztest_spa
;
3239 vdev_t
*rvd
= spa
->spa_root_vdev
;
3240 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3241 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3244 if (ztest_opts
.zo_mmp_test
)
3247 mutex_enter(&ztest_vdev_lock
);
3249 /* ensure we have a useable config; mirrors of raidz aren't supported */
3250 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3251 mutex_exit(&ztest_vdev_lock
);
3255 /* clean up the old pool, if any */
3256 (void) spa_destroy("splitp");
3258 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3260 /* generate a config from the existing config */
3261 mutex_enter(&spa
->spa_props_lock
);
3262 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3264 mutex_exit(&spa
->spa_props_lock
);
3266 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3269 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3270 for (c
= 0; c
< children
; c
++) {
3271 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3275 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3276 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3278 VERIFY(nvlist_add_string(schild
[schildren
],
3279 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3280 VERIFY(nvlist_add_uint64(schild
[schildren
],
3281 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3283 lastlogid
= schildren
;
3288 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3289 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3290 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3293 /* OK, create a config that can be used to split */
3294 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3295 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3296 VDEV_TYPE_ROOT
) == 0);
3297 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3298 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3300 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3301 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3303 for (c
= 0; c
< schildren
; c
++)
3304 nvlist_free(schild
[c
]);
3308 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3310 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3311 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3312 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3314 nvlist_free(config
);
3317 (void) printf("successful split - results:\n");
3318 mutex_enter(&spa_namespace_lock
);
3319 show_pool_stats(spa
);
3320 show_pool_stats(spa_lookup("splitp"));
3321 mutex_exit(&spa_namespace_lock
);
3325 mutex_exit(&ztest_vdev_lock
);
3329 * Verify that we can attach and detach devices.
3333 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3335 ztest_shared_t
*zs
= ztest_shared
;
3336 spa_t
*spa
= ztest_spa
;
3337 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3338 vdev_t
*rvd
= spa
->spa_root_vdev
;
3339 vdev_t
*oldvd
, *newvd
, *pvd
;
3343 uint64_t ashift
= ztest_get_ashift();
3344 uint64_t oldguid
, pguid
;
3345 uint64_t oldsize
, newsize
;
3346 char *oldpath
, *newpath
;
3348 int oldvd_has_siblings
= B_FALSE
;
3349 int newvd_is_spare
= B_FALSE
;
3351 int error
, expected_error
;
3353 if (ztest_opts
.zo_mmp_test
)
3356 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3357 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3359 mutex_enter(&ztest_vdev_lock
);
3360 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3362 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3365 * If a vdev is in the process of being removed, its removal may
3366 * finish while we are in progress, leading to an unexpected error
3367 * value. Don't bother trying to attach while we are in the middle
3370 if (ztest_device_removal_active
) {
3371 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3372 mutex_exit(&ztest_vdev_lock
);
3377 * Decide whether to do an attach or a replace.
3379 replacing
= ztest_random(2);
3382 * Pick a random top-level vdev.
3384 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3387 * Pick a random leaf within it.
3389 leaf
= ztest_random(leaves
);
3394 oldvd
= rvd
->vdev_child
[top
];
3396 /* pick a child from the mirror */
3397 if (zs
->zs_mirrors
>= 1) {
3398 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3399 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3400 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3403 /* pick a child out of the raidz group */
3404 if (ztest_opts
.zo_raidz
> 1) {
3405 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3406 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3407 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3411 * If we're already doing an attach or replace, oldvd may be a
3412 * mirror vdev -- in which case, pick a random child.
3414 while (oldvd
->vdev_children
!= 0) {
3415 oldvd_has_siblings
= B_TRUE
;
3416 ASSERT(oldvd
->vdev_children
>= 2);
3417 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3420 oldguid
= oldvd
->vdev_guid
;
3421 oldsize
= vdev_get_min_asize(oldvd
);
3422 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3423 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3424 pvd
= oldvd
->vdev_parent
;
3425 pguid
= pvd
->vdev_guid
;
3428 * If oldvd has siblings, then half of the time, detach it.
3430 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3431 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3432 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3433 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3434 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3435 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3436 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3441 * For the new vdev, choose with equal probability between the two
3442 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3444 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3445 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3446 newvd_is_spare
= B_TRUE
;
3447 (void) strcpy(newpath
, newvd
->vdev_path
);
3449 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3450 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3451 top
* leaves
+ leaf
);
3452 if (ztest_random(2) == 0)
3453 newpath
[strlen(newpath
) - 1] = 'b';
3454 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3459 * Reopen to ensure the vdev's asize field isn't stale.
3462 newsize
= vdev_get_min_asize(newvd
);
3465 * Make newsize a little bigger or smaller than oldsize.
3466 * If it's smaller, the attach should fail.
3467 * If it's larger, and we're doing a replace,
3468 * we should get dynamic LUN growth when we're done.
3470 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3474 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3475 * unless it's a replace; in that case any non-replacing parent is OK.
3477 * If newvd is already part of the pool, it should fail with EBUSY.
3479 * If newvd is too small, it should fail with EOVERFLOW.
3481 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3482 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3483 pvd
->vdev_ops
== &vdev_replacing_ops
||
3484 pvd
->vdev_ops
== &vdev_spare_ops
))
3485 expected_error
= ENOTSUP
;
3486 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3487 expected_error
= ENOTSUP
;
3488 else if (newvd
== oldvd
)
3489 expected_error
= replacing
? 0 : EBUSY
;
3490 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3491 expected_error
= EBUSY
;
3492 else if (newsize
< oldsize
)
3493 expected_error
= EOVERFLOW
;
3494 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3495 expected_error
= EDOM
;
3499 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3502 * Build the nvlist describing newpath.
3504 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3505 ashift
, NULL
, 0, 0, 1);
3507 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3512 * If our parent was the replacing vdev, but the replace completed,
3513 * then instead of failing with ENOTSUP we may either succeed,
3514 * fail with ENODEV, or fail with EOVERFLOW.
3516 if (expected_error
== ENOTSUP
&&
3517 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3518 expected_error
= error
;
3521 * If someone grew the LUN, the replacement may be too small.
3523 if (error
== EOVERFLOW
|| error
== EBUSY
)
3524 expected_error
= error
;
3526 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3527 error
== ZFS_ERR_DISCARDING_CHECKPOINT
)
3528 expected_error
= error
;
3530 /* XXX workaround 6690467 */
3531 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3532 fatal(0, "attach (%s %llu, %s %llu, %d) "
3533 "returned %d, expected %d",
3534 oldpath
, oldsize
, newpath
,
3535 newsize
, replacing
, error
, expected_error
);
3538 mutex_exit(&ztest_vdev_lock
);
3540 umem_free(oldpath
, MAXPATHLEN
);
3541 umem_free(newpath
, MAXPATHLEN
);
3546 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3548 spa_t
*spa
= ztest_spa
;
3553 mutex_enter(&ztest_vdev_lock
);
3555 if (ztest_device_removal_active
) {
3556 mutex_exit(&ztest_vdev_lock
);
3561 * Remove a random top-level vdev and wait for removal to finish.
3563 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3564 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3565 guid
= vd
->vdev_guid
;
3566 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3568 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3570 ztest_device_removal_active
= B_TRUE
;
3571 mutex_exit(&ztest_vdev_lock
);
3574 * spa->spa_vdev_removal is created in a sync task that
3575 * is initiated via dsl_sync_task_nowait(). Since the
3576 * task may not run before spa_vdev_remove() returns, we
3577 * must wait at least 1 txg to ensure that the removal
3578 * struct has been created.
3580 txg_wait_synced(spa_get_dsl(spa
), 0);
3582 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
3583 txg_wait_synced(spa_get_dsl(spa
), 0);
3585 mutex_exit(&ztest_vdev_lock
);
3590 * The pool needs to be scrubbed after completing device removal.
3591 * Failure to do so may result in checksum errors due to the
3592 * strategy employed by ztest_fault_inject() when selecting which
3593 * offset are redundant and can be damaged.
3595 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3597 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3598 txg_wait_synced(spa_get_dsl(spa
), 0);
3601 mutex_enter(&ztest_vdev_lock
);
3602 ztest_device_removal_active
= B_FALSE
;
3603 mutex_exit(&ztest_vdev_lock
);
3607 * Callback function which expands the physical size of the vdev.
3610 grow_vdev(vdev_t
*vd
, void *arg
)
3612 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3613 size_t *newsize
= arg
;
3617 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3618 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3620 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3623 fsize
= lseek(fd
, 0, SEEK_END
);
3624 VERIFY(ftruncate(fd
, *newsize
) == 0);
3626 if (ztest_opts
.zo_verbose
>= 6) {
3627 (void) printf("%s grew from %lu to %lu bytes\n",
3628 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3635 * Callback function which expands a given vdev by calling vdev_online().
3639 online_vdev(vdev_t
*vd
, void *arg
)
3641 spa_t
*spa
= vd
->vdev_spa
;
3642 vdev_t
*tvd
= vd
->vdev_top
;
3643 uint64_t guid
= vd
->vdev_guid
;
3644 uint64_t generation
= spa
->spa_config_generation
+ 1;
3645 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3648 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3649 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3651 /* Calling vdev_online will initialize the new metaslabs */
3652 spa_config_exit(spa
, SCL_STATE
, spa
);
3653 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3654 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3657 * If vdev_online returned an error or the underlying vdev_open
3658 * failed then we abort the expand. The only way to know that
3659 * vdev_open fails is by checking the returned newstate.
3661 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3662 if (ztest_opts
.zo_verbose
>= 5) {
3663 (void) printf("Unable to expand vdev, state %llu, "
3664 "error %d\n", (u_longlong_t
)newstate
, error
);
3668 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3671 * Since we dropped the lock we need to ensure that we're
3672 * still talking to the original vdev. It's possible this
3673 * vdev may have been detached/replaced while we were
3674 * trying to online it.
3676 if (generation
!= spa
->spa_config_generation
) {
3677 if (ztest_opts
.zo_verbose
>= 5) {
3678 (void) printf("vdev configuration has changed, "
3679 "guid %llu, state %llu, expected gen %llu, "
3682 (u_longlong_t
)tvd
->vdev_state
,
3683 (u_longlong_t
)generation
,
3684 (u_longlong_t
)spa
->spa_config_generation
);
3692 * Traverse the vdev tree calling the supplied function.
3693 * We continue to walk the tree until we either have walked all
3694 * children or we receive a non-NULL return from the callback.
3695 * If a NULL callback is passed, then we just return back the first
3696 * leaf vdev we encounter.
3699 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3703 if (vd
->vdev_ops
->vdev_op_leaf
) {
3707 return (func(vd
, arg
));
3710 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3711 vdev_t
*cvd
= vd
->vdev_child
[c
];
3712 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3719 * Verify that dynamic LUN growth works as expected.
3723 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3725 spa_t
*spa
= ztest_spa
;
3727 metaslab_class_t
*mc
;
3728 metaslab_group_t
*mg
;
3729 size_t psize
, newsize
;
3731 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3733 mutex_enter(&ztest_checkpoint_lock
);
3734 mutex_enter(&ztest_vdev_lock
);
3735 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3738 * If there is a vdev removal in progress, it could complete while
3739 * we are running, in which case we would not be able to verify
3740 * that the metaslab_class space increased (because it decreases
3741 * when the device removal completes).
3743 if (ztest_device_removal_active
) {
3744 spa_config_exit(spa
, SCL_STATE
, spa
);
3745 mutex_exit(&ztest_vdev_lock
);
3746 mutex_exit(&ztest_checkpoint_lock
);
3750 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3752 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3755 old_ms_count
= tvd
->vdev_ms_count
;
3756 old_class_space
= metaslab_class_get_space(mc
);
3759 * Determine the size of the first leaf vdev associated with
3760 * our top-level device.
3762 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3763 ASSERT3P(vd
, !=, NULL
);
3764 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3766 psize
= vd
->vdev_psize
;
3769 * We only try to expand the vdev if it's healthy, less than 4x its
3770 * original size, and it has a valid psize.
3772 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3773 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3774 spa_config_exit(spa
, SCL_STATE
, spa
);
3775 mutex_exit(&ztest_vdev_lock
);
3776 mutex_exit(&ztest_checkpoint_lock
);
3780 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3781 ASSERT3U(newsize
, >, psize
);
3783 if (ztest_opts
.zo_verbose
>= 6) {
3784 (void) printf("Expanding LUN %s from %lu to %lu\n",
3785 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3789 * Growing the vdev is a two step process:
3790 * 1). expand the physical size (i.e. relabel)
3791 * 2). online the vdev to create the new metaslabs
3793 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3794 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3795 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3796 if (ztest_opts
.zo_verbose
>= 5) {
3797 (void) printf("Could not expand LUN because "
3798 "the vdev configuration changed.\n");
3800 spa_config_exit(spa
, SCL_STATE
, spa
);
3801 mutex_exit(&ztest_vdev_lock
);
3802 mutex_exit(&ztest_checkpoint_lock
);
3806 spa_config_exit(spa
, SCL_STATE
, spa
);
3809 * Expanding the LUN will update the config asynchronously,
3810 * thus we must wait for the async thread to complete any
3811 * pending tasks before proceeding.
3815 mutex_enter(&spa
->spa_async_lock
);
3816 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3817 mutex_exit(&spa
->spa_async_lock
);
3820 txg_wait_synced(spa_get_dsl(spa
), 0);
3821 (void) poll(NULL
, 0, 100);
3824 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3826 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3827 new_ms_count
= tvd
->vdev_ms_count
;
3828 new_class_space
= metaslab_class_get_space(mc
);
3830 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3831 if (ztest_opts
.zo_verbose
>= 5) {
3832 (void) printf("Could not verify LUN expansion due to "
3833 "intervening vdev offline or remove.\n");
3835 spa_config_exit(spa
, SCL_STATE
, spa
);
3836 mutex_exit(&ztest_vdev_lock
);
3837 mutex_exit(&ztest_checkpoint_lock
);
3842 * Make sure we were able to grow the vdev.
3844 if (new_ms_count
<= old_ms_count
) {
3845 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3846 old_ms_count
, new_ms_count
);
3850 * Make sure we were able to grow the pool.
3852 if (new_class_space
<= old_class_space
) {
3853 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3854 old_class_space
, new_class_space
);
3857 if (ztest_opts
.zo_verbose
>= 5) {
3858 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3860 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3861 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3862 (void) printf("%s grew from %s to %s\n",
3863 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3866 spa_config_exit(spa
, SCL_STATE
, spa
);
3867 mutex_exit(&ztest_vdev_lock
);
3868 mutex_exit(&ztest_checkpoint_lock
);
3872 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3876 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3879 * Create the objects common to all ztest datasets.
3881 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3882 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3886 ztest_dataset_create(char *dsname
)
3890 dsl_crypto_params_t
*dcp
= NULL
;
3893 * 50% of the time, we create encrypted datasets
3894 * using a random cipher suite and a hard-coded
3897 rand
= ztest_random(2);
3899 nvlist_t
*crypto_args
= fnvlist_alloc();
3900 nvlist_t
*props
= fnvlist_alloc();
3902 /* slight bias towards the default cipher suite */
3903 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3904 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3905 rand
= ZIO_CRYPT_ON
;
3907 fnvlist_add_uint64(props
,
3908 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3909 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3910 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3913 * These parameters aren't really used by the kernel. They
3914 * are simply stored so that userspace knows how to load
3917 fnvlist_add_uint64(props
,
3918 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3919 fnvlist_add_string(props
,
3920 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3921 fnvlist_add_uint64(props
,
3922 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3923 fnvlist_add_uint64(props
,
3924 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3926 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3927 crypto_args
, &dcp
));
3930 * Cycle through all available encryption implementations
3931 * to verify interoperability.
3933 VERIFY0(gcm_impl_set("cycle"));
3934 VERIFY0(aes_impl_set("cycle"));
3936 fnvlist_free(crypto_args
);
3937 fnvlist_free(props
);
3940 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
3941 ztest_objset_create_cb
, NULL
);
3942 dsl_crypto_params_free(dcp
, !!err
);
3944 rand
= ztest_random(100);
3945 if (err
|| rand
< 80)
3948 if (ztest_opts
.zo_verbose
>= 5)
3949 (void) printf("Setting dataset %s to sync always\n", dsname
);
3950 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3951 ZFS_SYNC_ALWAYS
, B_FALSE
));
3956 ztest_objset_destroy_cb(const char *name
, void *arg
)
3959 dmu_object_info_t doi
;
3963 * Verify that the dataset contains a directory object.
3965 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3966 B_TRUE
, FTAG
, &os
));
3967 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3968 if (error
!= ENOENT
) {
3969 /* We could have crashed in the middle of destroying it */
3971 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3972 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3974 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3977 * Destroy the dataset.
3979 if (strchr(name
, '@') != NULL
) {
3980 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3982 error
= dsl_destroy_head(name
);
3983 if (error
== ENOSPC
) {
3984 /* There could be checkpoint or insufficient slop */
3985 ztest_record_enospc(FTAG
);
3986 } else if (error
!= EBUSY
) {
3987 /* There could be a hold on this dataset */
3995 ztest_snapshot_create(char *osname
, uint64_t id
)
3997 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4000 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4002 error
= dmu_objset_snapshot_one(osname
, snapname
);
4003 if (error
== ENOSPC
) {
4004 ztest_record_enospc(FTAG
);
4007 if (error
!= 0 && error
!= EEXIST
) {
4008 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4015 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4017 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4020 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4023 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4024 if (error
!= 0 && error
!= ENOENT
)
4025 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4031 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4037 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4041 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4043 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4045 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4046 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4049 * If this dataset exists from a previous run, process its replay log
4050 * half of the time. If we don't replay it, then dsl_destroy_head()
4051 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4053 if (ztest_random(2) == 0 &&
4054 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4055 B_TRUE
, FTAG
, &os
) == 0) {
4056 ztest_zd_init(zdtmp
, NULL
, os
);
4057 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4058 ztest_zd_fini(zdtmp
);
4059 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4063 * There may be an old instance of the dataset we're about to
4064 * create lying around from a previous run. If so, destroy it
4065 * and all of its snapshots.
4067 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4068 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4071 * Verify that the destroyed dataset is no longer in the namespace.
4073 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4074 B_TRUE
, FTAG
, &os
));
4077 * Verify that we can create a new dataset.
4079 error
= ztest_dataset_create(name
);
4081 if (error
== ENOSPC
) {
4082 ztest_record_enospc(FTAG
);
4085 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4088 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4091 ztest_zd_init(zdtmp
, NULL
, os
);
4094 * Open the intent log for it.
4096 zilog
= zil_open(os
, ztest_get_data
);
4099 * Put some objects in there, do a little I/O to them,
4100 * and randomly take a couple of snapshots along the way.
4102 iters
= ztest_random(5);
4103 for (i
= 0; i
< iters
; i
++) {
4104 ztest_dmu_object_alloc_free(zdtmp
, id
);
4105 if (ztest_random(iters
) == 0)
4106 (void) ztest_snapshot_create(name
, i
);
4110 * Verify that we cannot create an existing dataset.
4112 VERIFY3U(EEXIST
, ==,
4113 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4116 * Verify that we can hold an objset that is also owned.
4118 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4119 dmu_objset_rele(os2
, FTAG
);
4122 * Verify that we cannot own an objset that is already owned.
4124 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4125 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4128 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4129 ztest_zd_fini(zdtmp
);
4131 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4133 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4137 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4140 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4142 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4143 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4144 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4145 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4149 * Cleanup non-standard snapshots and clones.
4152 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4161 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4162 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4163 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4164 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4165 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4167 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4168 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4169 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4170 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4171 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4172 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4173 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4174 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4175 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4176 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4178 error
= dsl_destroy_head(clone2name
);
4179 if (error
&& error
!= ENOENT
)
4180 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4181 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4182 if (error
&& error
!= ENOENT
)
4183 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4184 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4185 if (error
&& error
!= ENOENT
)
4186 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4187 error
= dsl_destroy_head(clone1name
);
4188 if (error
&& error
!= ENOENT
)
4189 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4190 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4191 if (error
&& error
!= ENOENT
)
4192 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4194 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4195 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4196 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4197 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4198 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4202 * Verify dsl_dataset_promote handles EBUSY
4205 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4213 char *osname
= zd
->zd_name
;
4216 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4217 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4218 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4219 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4220 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4222 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4224 ztest_dsl_dataset_cleanup(osname
, id
);
4226 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4227 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4228 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4229 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4230 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4231 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4232 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4233 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4234 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4235 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4237 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4238 if (error
&& error
!= EEXIST
) {
4239 if (error
== ENOSPC
) {
4240 ztest_record_enospc(FTAG
);
4243 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4246 error
= dmu_objset_clone(clone1name
, snap1name
);
4248 if (error
== ENOSPC
) {
4249 ztest_record_enospc(FTAG
);
4252 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4255 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4256 if (error
&& error
!= EEXIST
) {
4257 if (error
== ENOSPC
) {
4258 ztest_record_enospc(FTAG
);
4261 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4264 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4265 if (error
&& error
!= EEXIST
) {
4266 if (error
== ENOSPC
) {
4267 ztest_record_enospc(FTAG
);
4270 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4273 error
= dmu_objset_clone(clone2name
, snap3name
);
4275 if (error
== ENOSPC
) {
4276 ztest_record_enospc(FTAG
);
4279 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4282 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4285 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4286 error
= dsl_dataset_promote(clone2name
, NULL
);
4287 if (error
== ENOSPC
) {
4288 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4289 ztest_record_enospc(FTAG
);
4293 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4295 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4298 ztest_dsl_dataset_cleanup(osname
, id
);
4300 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4302 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4303 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4304 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4305 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4306 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4309 #undef OD_ARRAY_SIZE
4310 #define OD_ARRAY_SIZE 4
4313 * Verify that dmu_object_{alloc,free} work as expected.
4316 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4323 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4324 od
= umem_alloc(size
, UMEM_NOFAIL
);
4325 batchsize
= OD_ARRAY_SIZE
;
4327 for (b
= 0; b
< batchsize
; b
++)
4328 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4332 * Destroy the previous batch of objects, create a new batch,
4333 * and do some I/O on the new objects.
4335 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4338 while (ztest_random(4 * batchsize
) != 0)
4339 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4340 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4342 umem_free(od
, size
);
4346 * Rewind the global allocator to verify object allocation backfilling.
4349 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4351 objset_t
*os
= zd
->zd_os
;
4352 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4356 * Rewind the global allocator randomly back to a lower object number
4357 * to force backfilling and reclamation of recently freed dnodes.
4359 mutex_enter(&os
->os_obj_lock
);
4360 object
= ztest_random(os
->os_obj_next_chunk
);
4361 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4362 mutex_exit(&os
->os_obj_lock
);
4365 #undef OD_ARRAY_SIZE
4366 #define OD_ARRAY_SIZE 2
4369 * Verify that dmu_{read,write} work as expected.
4372 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4377 objset_t
*os
= zd
->zd_os
;
4378 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4379 od
= umem_alloc(size
, UMEM_NOFAIL
);
4381 int i
, freeit
, error
;
4383 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4384 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4385 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4386 uint64_t regions
= 997;
4387 uint64_t stride
= 123456789ULL;
4388 uint64_t width
= 40;
4389 int free_percent
= 5;
4392 * This test uses two objects, packobj and bigobj, that are always
4393 * updated together (i.e. in the same tx) so that their contents are
4394 * in sync and can be compared. Their contents relate to each other
4395 * in a simple way: packobj is a dense array of 'bufwad' structures,
4396 * while bigobj is a sparse array of the same bufwads. Specifically,
4397 * for any index n, there are three bufwads that should be identical:
4399 * packobj, at offset n * sizeof (bufwad_t)
4400 * bigobj, at the head of the nth chunk
4401 * bigobj, at the tail of the nth chunk
4403 * The chunk size is arbitrary. It doesn't have to be a power of two,
4404 * and it doesn't have any relation to the object blocksize.
4405 * The only requirement is that it can hold at least two bufwads.
4407 * Normally, we write the bufwad to each of these locations.
4408 * However, free_percent of the time we instead write zeroes to
4409 * packobj and perform a dmu_free_range() on bigobj. By comparing
4410 * bigobj to packobj, we can verify that the DMU is correctly
4411 * tracking which parts of an object are allocated and free,
4412 * and that the contents of the allocated blocks are correct.
4416 * Read the directory info. If it's the first time, set things up.
4418 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4419 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4422 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4423 umem_free(od
, size
);
4427 bigobj
= od
[0].od_object
;
4428 packobj
= od
[1].od_object
;
4429 chunksize
= od
[0].od_gen
;
4430 ASSERT(chunksize
== od
[1].od_gen
);
4433 * Prefetch a random chunk of the big object.
4434 * Our aim here is to get some async reads in flight
4435 * for blocks that we may free below; the DMU should
4436 * handle this race correctly.
4438 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4439 s
= 1 + ztest_random(2 * width
- 1);
4440 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4441 ZIO_PRIORITY_SYNC_READ
);
4444 * Pick a random index and compute the offsets into packobj and bigobj.
4446 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4447 s
= 1 + ztest_random(width
- 1);
4449 packoff
= n
* sizeof (bufwad_t
);
4450 packsize
= s
* sizeof (bufwad_t
);
4452 bigoff
= n
* chunksize
;
4453 bigsize
= s
* chunksize
;
4455 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4456 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4459 * free_percent of the time, free a range of bigobj rather than
4462 freeit
= (ztest_random(100) < free_percent
);
4465 * Read the current contents of our objects.
4467 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4470 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4475 * Get a tx for the mods to both packobj and bigobj.
4477 tx
= dmu_tx_create(os
);
4479 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4482 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4484 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4486 /* This accounts for setting the checksum/compression. */
4487 dmu_tx_hold_bonus(tx
, bigobj
);
4489 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4491 umem_free(packbuf
, packsize
);
4492 umem_free(bigbuf
, bigsize
);
4493 umem_free(od
, size
);
4497 enum zio_checksum cksum
;
4499 cksum
= (enum zio_checksum
)
4500 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4501 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4502 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4504 enum zio_compress comp
;
4506 comp
= (enum zio_compress
)
4507 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4508 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4509 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4512 * For each index from n to n + s, verify that the existing bufwad
4513 * in packobj matches the bufwads at the head and tail of the
4514 * corresponding chunk in bigobj. Then update all three bufwads
4515 * with the new values we want to write out.
4517 for (i
= 0; i
< s
; i
++) {
4519 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4521 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4523 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4525 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4526 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4528 if (pack
->bw_txg
> txg
)
4529 fatal(0, "future leak: got %llx, open txg is %llx",
4532 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4533 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4534 pack
->bw_index
, n
, i
);
4536 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4537 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4539 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4540 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4543 bzero(pack
, sizeof (bufwad_t
));
4545 pack
->bw_index
= n
+ i
;
4547 pack
->bw_data
= 1 + ztest_random(-2ULL);
4554 * We've verified all the old bufwads, and made new ones.
4555 * Now write them out.
4557 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4560 if (ztest_opts
.zo_verbose
>= 7) {
4561 (void) printf("freeing offset %llx size %llx"
4563 (u_longlong_t
)bigoff
,
4564 (u_longlong_t
)bigsize
,
4567 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4569 if (ztest_opts
.zo_verbose
>= 7) {
4570 (void) printf("writing offset %llx size %llx"
4572 (u_longlong_t
)bigoff
,
4573 (u_longlong_t
)bigsize
,
4576 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4582 * Sanity check the stuff we just wrote.
4585 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4586 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4588 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4589 packsize
, packcheck
, DMU_READ_PREFETCH
));
4590 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4591 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4593 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4594 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4596 umem_free(packcheck
, packsize
);
4597 umem_free(bigcheck
, bigsize
);
4600 umem_free(packbuf
, packsize
);
4601 umem_free(bigbuf
, bigsize
);
4602 umem_free(od
, size
);
4606 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4607 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4615 * For each index from n to n + s, verify that the existing bufwad
4616 * in packobj matches the bufwads at the head and tail of the
4617 * corresponding chunk in bigobj. Then update all three bufwads
4618 * with the new values we want to write out.
4620 for (i
= 0; i
< s
; i
++) {
4622 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4624 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4626 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4628 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4629 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4631 if (pack
->bw_txg
> txg
)
4632 fatal(0, "future leak: got %llx, open txg is %llx",
4635 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4636 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4637 pack
->bw_index
, n
, i
);
4639 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4640 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4642 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4643 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4645 pack
->bw_index
= n
+ i
;
4647 pack
->bw_data
= 1 + ztest_random(-2ULL);
4654 #undef OD_ARRAY_SIZE
4655 #define OD_ARRAY_SIZE 2
4658 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4660 objset_t
*os
= zd
->zd_os
;
4667 bufwad_t
*packbuf
, *bigbuf
;
4668 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4669 uint64_t blocksize
= ztest_random_blocksize();
4670 uint64_t chunksize
= blocksize
;
4671 uint64_t regions
= 997;
4672 uint64_t stride
= 123456789ULL;
4674 dmu_buf_t
*bonus_db
;
4675 arc_buf_t
**bigbuf_arcbufs
;
4676 dmu_object_info_t doi
;
4678 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4679 od
= umem_alloc(size
, UMEM_NOFAIL
);
4682 * This test uses two objects, packobj and bigobj, that are always
4683 * updated together (i.e. in the same tx) so that their contents are
4684 * in sync and can be compared. Their contents relate to each other
4685 * in a simple way: packobj is a dense array of 'bufwad' structures,
4686 * while bigobj is a sparse array of the same bufwads. Specifically,
4687 * for any index n, there are three bufwads that should be identical:
4689 * packobj, at offset n * sizeof (bufwad_t)
4690 * bigobj, at the head of the nth chunk
4691 * bigobj, at the tail of the nth chunk
4693 * The chunk size is set equal to bigobj block size so that
4694 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4698 * Read the directory info. If it's the first time, set things up.
4700 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4701 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4705 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4706 umem_free(od
, size
);
4710 bigobj
= od
[0].od_object
;
4711 packobj
= od
[1].od_object
;
4712 blocksize
= od
[0].od_blocksize
;
4713 chunksize
= blocksize
;
4714 ASSERT(chunksize
== od
[1].od_gen
);
4716 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4717 VERIFY(ISP2(doi
.doi_data_block_size
));
4718 VERIFY(chunksize
== doi
.doi_data_block_size
);
4719 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4722 * Pick a random index and compute the offsets into packobj and bigobj.
4724 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4725 s
= 1 + ztest_random(width
- 1);
4727 packoff
= n
* sizeof (bufwad_t
);
4728 packsize
= s
* sizeof (bufwad_t
);
4730 bigoff
= n
* chunksize
;
4731 bigsize
= s
* chunksize
;
4733 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4734 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4736 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4738 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4741 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4742 * Iteration 1 test zcopy to already referenced dbufs.
4743 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4744 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4745 * Iteration 4 test zcopy when dbuf is no longer dirty.
4746 * Iteration 5 test zcopy when it can't be done.
4747 * Iteration 6 one more zcopy write.
4749 for (i
= 0; i
< 7; i
++) {
4754 * In iteration 5 (i == 5) use arcbufs
4755 * that don't match bigobj blksz to test
4756 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4757 * assign an arcbuf to a dbuf.
4759 for (j
= 0; j
< s
; j
++) {
4760 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4762 dmu_request_arcbuf(bonus_db
, chunksize
);
4764 bigbuf_arcbufs
[2 * j
] =
4765 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4766 bigbuf_arcbufs
[2 * j
+ 1] =
4767 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4772 * Get a tx for the mods to both packobj and bigobj.
4774 tx
= dmu_tx_create(os
);
4776 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4777 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4779 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4781 umem_free(packbuf
, packsize
);
4782 umem_free(bigbuf
, bigsize
);
4783 for (j
= 0; j
< s
; j
++) {
4785 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4786 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4789 bigbuf_arcbufs
[2 * j
]);
4791 bigbuf_arcbufs
[2 * j
+ 1]);
4794 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4795 umem_free(od
, size
);
4796 dmu_buf_rele(bonus_db
, FTAG
);
4801 * 50% of the time don't read objects in the 1st iteration to
4802 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4803 * no existing dbufs for the specified offsets.
4805 if (i
!= 0 || ztest_random(2) != 0) {
4806 error
= dmu_read(os
, packobj
, packoff
,
4807 packsize
, packbuf
, DMU_READ_PREFETCH
);
4809 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4810 bigbuf
, DMU_READ_PREFETCH
);
4813 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4817 * We've verified all the old bufwads, and made new ones.
4818 * Now write them out.
4820 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4821 if (ztest_opts
.zo_verbose
>= 7) {
4822 (void) printf("writing offset %llx size %llx"
4824 (u_longlong_t
)bigoff
,
4825 (u_longlong_t
)bigsize
,
4828 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4830 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4831 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4832 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4834 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4835 bigbuf_arcbufs
[2 * j
]->b_data
,
4837 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4839 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4844 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4845 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4847 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4848 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4849 bigbuf_arcbufs
[j
], tx
);
4851 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4852 bigbuf_arcbufs
[2 * j
], tx
);
4853 dmu_assign_arcbuf_by_dbuf(bonus_db
,
4854 off
+ chunksize
/ 2,
4855 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4858 dmu_buf_rele(dbt
, FTAG
);
4864 * Sanity check the stuff we just wrote.
4867 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4868 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4870 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4871 packsize
, packcheck
, DMU_READ_PREFETCH
));
4872 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4873 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4875 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4876 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4878 umem_free(packcheck
, packsize
);
4879 umem_free(bigcheck
, bigsize
);
4882 txg_wait_open(dmu_objset_pool(os
), 0);
4883 } else if (i
== 3) {
4884 txg_wait_synced(dmu_objset_pool(os
), 0);
4888 dmu_buf_rele(bonus_db
, FTAG
);
4889 umem_free(packbuf
, packsize
);
4890 umem_free(bigbuf
, bigsize
);
4891 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4892 umem_free(od
, size
);
4897 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4901 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4902 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4903 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4906 * Have multiple threads write to large offsets in an object
4907 * to verify that parallel writes to an object -- even to the
4908 * same blocks within the object -- doesn't cause any trouble.
4910 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4912 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4915 while (ztest_random(10) != 0)
4916 ztest_io(zd
, od
->od_object
, offset
);
4918 umem_free(od
, sizeof (ztest_od_t
));
4922 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4925 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4926 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4927 uint64_t count
= ztest_random(20) + 1;
4928 uint64_t blocksize
= ztest_random_blocksize();
4931 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4933 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4935 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4936 !ztest_random(2)) != 0) {
4937 umem_free(od
, sizeof (ztest_od_t
));
4941 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4942 umem_free(od
, sizeof (ztest_od_t
));
4946 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4948 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4950 while (ztest_random(count
) != 0) {
4951 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4952 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4955 while (ztest_random(4) != 0)
4956 ztest_io(zd
, od
->od_object
, randoff
);
4959 umem_free(data
, blocksize
);
4960 umem_free(od
, sizeof (ztest_od_t
));
4964 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4966 #define ZTEST_ZAP_MIN_INTS 1
4967 #define ZTEST_ZAP_MAX_INTS 4
4968 #define ZTEST_ZAP_MAX_PROPS 1000
4971 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4973 objset_t
*os
= zd
->zd_os
;
4976 uint64_t txg
, last_txg
;
4977 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4978 uint64_t zl_ints
, zl_intsize
, prop
;
4981 char propname
[100], txgname
[100];
4983 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4985 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4986 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4988 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4989 !ztest_random(2)) != 0)
4992 object
= od
->od_object
;
4995 * Generate a known hash collision, and verify that
4996 * we can lookup and remove both entries.
4998 tx
= dmu_tx_create(os
);
4999 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5000 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5003 for (i
= 0; i
< 2; i
++) {
5005 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5008 for (i
= 0; i
< 2; i
++) {
5009 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5010 sizeof (uint64_t), 1, &value
[i
], tx
));
5012 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5013 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5014 ASSERT3U(zl_ints
, ==, 1);
5016 for (i
= 0; i
< 2; i
++) {
5017 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5022 * Generate a buch of random entries.
5024 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5026 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5027 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5028 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5029 bzero(value
, sizeof (value
));
5033 * If these zap entries already exist, validate their contents.
5035 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5037 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5038 ASSERT3U(zl_ints
, ==, 1);
5040 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5041 zl_ints
, &last_txg
) == 0);
5043 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5046 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5047 ASSERT3U(zl_ints
, ==, ints
);
5049 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5050 zl_ints
, value
) == 0);
5052 for (i
= 0; i
< ints
; i
++) {
5053 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5056 ASSERT3U(error
, ==, ENOENT
);
5060 * Atomically update two entries in our zap object.
5061 * The first is named txg_%llu, and contains the txg
5062 * in which the property was last updated. The second
5063 * is named prop_%llu, and the nth element of its value
5064 * should be txg + object + n.
5066 tx
= dmu_tx_create(os
);
5067 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5068 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5073 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5075 for (i
= 0; i
< ints
; i
++)
5076 value
[i
] = txg
+ object
+ i
;
5078 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5080 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5086 * Remove a random pair of entries.
5088 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5089 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5090 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5092 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5094 if (error
== ENOENT
)
5099 tx
= dmu_tx_create(os
);
5100 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5101 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5104 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5105 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5108 umem_free(od
, sizeof (ztest_od_t
));
5112 * Testcase to test the upgrading of a microzap to fatzap.
5115 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5117 objset_t
*os
= zd
->zd_os
;
5119 uint64_t object
, txg
;
5122 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5123 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5125 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5126 !ztest_random(2)) != 0)
5128 object
= od
->od_object
;
5131 * Add entries to this ZAP and make sure it spills over
5132 * and gets upgraded to a fatzap. Also, since we are adding
5133 * 2050 entries we should see ptrtbl growth and leaf-block split.
5135 for (i
= 0; i
< 2050; i
++) {
5136 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5141 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5142 (u_longlong_t
)id
, (u_longlong_t
)value
);
5144 tx
= dmu_tx_create(os
);
5145 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5146 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5149 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5151 ASSERT(error
== 0 || error
== EEXIST
);
5155 umem_free(od
, sizeof (ztest_od_t
));
5160 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5162 objset_t
*os
= zd
->zd_os
;
5164 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5166 int i
, namelen
, error
;
5167 int micro
= ztest_random(2);
5168 char name
[20], string_value
[20];
5171 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5172 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5174 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5175 umem_free(od
, sizeof (ztest_od_t
));
5179 object
= od
->od_object
;
5182 * Generate a random name of the form 'xxx.....' where each
5183 * x is a random printable character and the dots are dots.
5184 * There are 94 such characters, and the name length goes from
5185 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5187 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5189 for (i
= 0; i
< 3; i
++)
5190 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5191 for (; i
< namelen
- 1; i
++)
5195 if ((namelen
& 1) || micro
) {
5196 wsize
= sizeof (txg
);
5202 data
= string_value
;
5206 VERIFY0(zap_count(os
, object
, &count
));
5207 ASSERT(count
!= -1ULL);
5210 * Select an operation: length, lookup, add, update, remove.
5212 i
= ztest_random(5);
5215 tx
= dmu_tx_create(os
);
5216 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5217 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5219 umem_free(od
, sizeof (ztest_od_t
));
5222 bcopy(name
, string_value
, namelen
);
5226 bzero(string_value
, namelen
);
5232 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5234 ASSERT3U(wsize
, ==, zl_wsize
);
5235 ASSERT3U(wc
, ==, zl_wc
);
5237 ASSERT3U(error
, ==, ENOENT
);
5242 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5244 if (data
== string_value
&&
5245 bcmp(name
, data
, namelen
) != 0)
5246 fatal(0, "name '%s' != val '%s' len %d",
5247 name
, data
, namelen
);
5249 ASSERT3U(error
, ==, ENOENT
);
5254 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5255 ASSERT(error
== 0 || error
== EEXIST
);
5259 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5263 error
= zap_remove(os
, object
, name
, tx
);
5264 ASSERT(error
== 0 || error
== ENOENT
);
5271 umem_free(od
, sizeof (ztest_od_t
));
5275 * Commit callback data.
5277 typedef struct ztest_cb_data
{
5278 list_node_t zcd_node
;
5280 int zcd_expected_err
;
5281 boolean_t zcd_added
;
5282 boolean_t zcd_called
;
5286 /* This is the actual commit callback function */
5288 ztest_commit_callback(void *arg
, int error
)
5290 ztest_cb_data_t
*data
= arg
;
5291 uint64_t synced_txg
;
5293 VERIFY(data
!= NULL
);
5294 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5295 VERIFY(!data
->zcd_called
);
5297 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5298 if (data
->zcd_txg
> synced_txg
)
5299 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5300 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5303 data
->zcd_called
= B_TRUE
;
5305 if (error
== ECANCELED
) {
5306 ASSERT0(data
->zcd_txg
);
5307 ASSERT(!data
->zcd_added
);
5310 * The private callback data should be destroyed here, but
5311 * since we are going to check the zcd_called field after
5312 * dmu_tx_abort(), we will destroy it there.
5317 ASSERT(data
->zcd_added
);
5318 ASSERT3U(data
->zcd_txg
, !=, 0);
5320 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5322 /* See if this cb was called more quickly */
5323 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5324 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5326 /* Remove our callback from the list */
5327 list_remove(&zcl
.zcl_callbacks
, data
);
5329 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5331 umem_free(data
, sizeof (ztest_cb_data_t
));
5334 /* Allocate and initialize callback data structure */
5335 static ztest_cb_data_t
*
5336 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5338 ztest_cb_data_t
*cb_data
;
5340 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5342 cb_data
->zcd_txg
= txg
;
5343 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5344 list_link_init(&cb_data
->zcd_node
);
5350 * Commit callback test.
5353 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5355 objset_t
*os
= zd
->zd_os
;
5358 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5359 uint64_t old_txg
, txg
;
5362 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5363 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5365 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5366 umem_free(od
, sizeof (ztest_od_t
));
5370 tx
= dmu_tx_create(os
);
5372 cb_data
[0] = ztest_create_cb_data(os
, 0);
5373 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5375 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5377 /* Every once in a while, abort the transaction on purpose */
5378 if (ztest_random(100) == 0)
5382 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5384 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5386 cb_data
[0]->zcd_txg
= txg
;
5387 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5388 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5392 * It's not a strict requirement to call the registered
5393 * callbacks from inside dmu_tx_abort(), but that's what
5394 * it's supposed to happen in the current implementation
5395 * so we will check for that.
5397 for (i
= 0; i
< 2; i
++) {
5398 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5399 VERIFY(!cb_data
[i
]->zcd_called
);
5404 for (i
= 0; i
< 2; i
++) {
5405 VERIFY(cb_data
[i
]->zcd_called
);
5406 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5409 umem_free(od
, sizeof (ztest_od_t
));
5413 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5414 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5417 * Read existing data to make sure there isn't a future leak.
5419 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5420 &old_txg
, DMU_READ_PREFETCH
));
5423 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5426 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5428 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5431 * Since commit callbacks don't have any ordering requirement and since
5432 * it is theoretically possible for a commit callback to be called
5433 * after an arbitrary amount of time has elapsed since its txg has been
5434 * synced, it is difficult to reliably determine whether a commit
5435 * callback hasn't been called due to high load or due to a flawed
5438 * In practice, we will assume that if after a certain number of txgs a
5439 * commit callback hasn't been called, then most likely there's an
5440 * implementation bug..
5442 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5443 if (tmp_cb
!= NULL
&&
5444 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5445 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5446 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5450 * Let's find the place to insert our callbacks.
5452 * Even though the list is ordered by txg, it is possible for the
5453 * insertion point to not be the end because our txg may already be
5454 * quiescing at this point and other callbacks in the open txg
5455 * (from other objsets) may have sneaked in.
5457 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5458 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5459 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5461 /* Add the 3 callbacks to the list */
5462 for (i
= 0; i
< 3; i
++) {
5464 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5466 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5469 cb_data
[i
]->zcd_added
= B_TRUE
;
5470 VERIFY(!cb_data
[i
]->zcd_called
);
5472 tmp_cb
= cb_data
[i
];
5477 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5481 umem_free(od
, sizeof (ztest_od_t
));
5485 * Visit each object in the dataset. Verify that its properties
5486 * are consistent what was stored in the block tag when it was created,
5487 * and that its unused bonus buffer space has not been overwritten.
5491 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5493 objset_t
*os
= zd
->zd_os
;
5497 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5498 ztest_block_tag_t
*bt
= NULL
;
5499 dmu_object_info_t doi
;
5502 ztest_object_lock(zd
, obj
, RL_READER
);
5503 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5504 ztest_object_unlock(zd
, obj
);
5508 dmu_object_info_from_db(db
, &doi
);
5509 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5510 bt
= ztest_bt_bonus(db
);
5512 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5513 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5514 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5516 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5519 dmu_buf_rele(db
, FTAG
);
5520 ztest_object_unlock(zd
, obj
);
5526 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5528 zfs_prop_t proplist
[] = {
5530 ZFS_PROP_COMPRESSION
,
5536 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5538 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5539 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5540 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5542 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5543 ztest_random_blocksize(), (int)ztest_random(2)));
5545 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5550 ztest_remap_blocks(ztest_ds_t
*zd
, uint64_t id
)
5552 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5554 int error
= dmu_objset_remap_indirects(zd
->zd_name
);
5555 if (error
== ENOSPC
)
5559 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5564 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5566 nvlist_t
*props
= NULL
;
5568 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5570 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5571 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5573 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5575 if (ztest_opts
.zo_verbose
>= 6)
5576 dump_nvlist(props
, 4);
5580 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5584 user_release_one(const char *snapname
, const char *holdname
)
5586 nvlist_t
*snaps
, *holds
;
5589 snaps
= fnvlist_alloc();
5590 holds
= fnvlist_alloc();
5591 fnvlist_add_boolean(holds
, holdname
);
5592 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5593 fnvlist_free(holds
);
5594 error
= dsl_dataset_user_release(snaps
, NULL
);
5595 fnvlist_free(snaps
);
5600 * Test snapshot hold/release and deferred destroy.
5603 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5606 objset_t
*os
= zd
->zd_os
;
5610 char clonename
[100];
5612 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5615 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5617 dmu_objset_name(os
, osname
);
5619 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5621 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5622 (void) snprintf(clonename
, sizeof (clonename
),
5623 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5624 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5627 * Clean up from any previous run.
5629 error
= dsl_destroy_head(clonename
);
5630 if (error
!= ENOENT
)
5632 error
= user_release_one(fullname
, tag
);
5633 if (error
!= ESRCH
&& error
!= ENOENT
)
5635 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5636 if (error
!= ENOENT
)
5640 * Create snapshot, clone it, mark snap for deferred destroy,
5641 * destroy clone, verify snap was also destroyed.
5643 error
= dmu_objset_snapshot_one(osname
, snapname
);
5645 if (error
== ENOSPC
) {
5646 ztest_record_enospc("dmu_objset_snapshot");
5649 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5652 error
= dmu_objset_clone(clonename
, fullname
);
5654 if (error
== ENOSPC
) {
5655 ztest_record_enospc("dmu_objset_clone");
5658 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5661 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5663 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5667 error
= dsl_destroy_head(clonename
);
5669 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5671 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5672 if (error
!= ENOENT
)
5673 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5676 * Create snapshot, add temporary hold, verify that we can't
5677 * destroy a held snapshot, mark for deferred destroy,
5678 * release hold, verify snapshot was destroyed.
5680 error
= dmu_objset_snapshot_one(osname
, snapname
);
5682 if (error
== ENOSPC
) {
5683 ztest_record_enospc("dmu_objset_snapshot");
5686 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5689 holds
= fnvlist_alloc();
5690 fnvlist_add_string(holds
, fullname
, tag
);
5691 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5692 fnvlist_free(holds
);
5694 if (error
== ENOSPC
) {
5695 ztest_record_enospc("dsl_dataset_user_hold");
5698 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5699 fullname
, tag
, error
);
5702 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5703 if (error
!= EBUSY
) {
5704 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5708 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5710 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5714 error
= user_release_one(fullname
, tag
);
5716 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5718 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5721 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5725 * Inject random faults into the on-disk data.
5729 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5731 ztest_shared_t
*zs
= ztest_shared
;
5732 spa_t
*spa
= ztest_spa
;
5736 uint64_t bad
= 0x1990c0ffeedecadeull
;
5741 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5747 boolean_t islog
= B_FALSE
;
5749 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5750 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5752 mutex_enter(&ztest_vdev_lock
);
5755 * Device removal is in progress, fault injection must be disabled
5756 * until it completes and the pool is scrubbed. The fault injection
5757 * strategy for damaging blocks does not take in to account evacuated
5758 * blocks which may have already been damaged.
5760 if (ztest_device_removal_active
) {
5761 mutex_exit(&ztest_vdev_lock
);
5765 maxfaults
= MAXFAULTS(zs
);
5766 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5767 mirror_save
= zs
->zs_mirrors
;
5768 mutex_exit(&ztest_vdev_lock
);
5770 ASSERT(leaves
>= 1);
5773 * Grab the name lock as reader. There are some operations
5774 * which don't like to have their vdevs changed while
5775 * they are in progress (i.e. spa_change_guid). Those
5776 * operations will have grabbed the name lock as writer.
5778 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5781 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5783 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5785 if (ztest_random(2) == 0) {
5787 * Inject errors on a normal data device or slog device.
5789 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5790 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5793 * Generate paths to the first leaf in this top-level vdev,
5794 * and to the random leaf we selected. We'll induce transient
5795 * write failures and random online/offline activity on leaf 0,
5796 * and we'll write random garbage to the randomly chosen leaf.
5798 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5799 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5800 top
* leaves
+ zs
->zs_splits
);
5801 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5802 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5803 top
* leaves
+ leaf
);
5805 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5806 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5810 * If the top-level vdev needs to be resilvered
5811 * then we only allow faults on the device that is
5814 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5815 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5816 vd0
->vdev_resilver_txg
!= 0)) {
5818 * Make vd0 explicitly claim to be unreadable,
5819 * or unwriteable, or reach behind its back
5820 * and close the underlying fd. We can do this if
5821 * maxfaults == 0 because we'll fail and reexecute,
5822 * and we can do it if maxfaults >= 2 because we'll
5823 * have enough redundancy. If maxfaults == 1, the
5824 * combination of this with injection of random data
5825 * corruption below exceeds the pool's fault tolerance.
5827 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5829 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5830 (long long)vd0
->vdev_id
, (int)maxfaults
);
5832 if (vf
!= NULL
&& ztest_random(3) == 0) {
5833 (void) close(vf
->vf_vnode
->v_fd
);
5834 vf
->vf_vnode
->v_fd
= -1;
5835 } else if (ztest_random(2) == 0) {
5836 vd0
->vdev_cant_read
= B_TRUE
;
5838 vd0
->vdev_cant_write
= B_TRUE
;
5840 guid0
= vd0
->vdev_guid
;
5844 * Inject errors on an l2cache device.
5846 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5848 if (sav
->sav_count
== 0) {
5849 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5850 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5853 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5854 guid0
= vd0
->vdev_guid
;
5855 (void) strcpy(path0
, vd0
->vdev_path
);
5856 (void) strcpy(pathrand
, vd0
->vdev_path
);
5860 maxfaults
= INT_MAX
; /* no limit on cache devices */
5863 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5864 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5867 * If we can tolerate two or more faults, or we're dealing
5868 * with a slog, randomly online/offline vd0.
5870 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5871 if (ztest_random(10) < 6) {
5872 int flags
= (ztest_random(2) == 0 ?
5873 ZFS_OFFLINE_TEMPORARY
: 0);
5876 * We have to grab the zs_name_lock as writer to
5877 * prevent a race between offlining a slog and
5878 * destroying a dataset. Offlining the slog will
5879 * grab a reference on the dataset which may cause
5880 * dsl_destroy_head() to fail with EBUSY thus
5881 * leaving the dataset in an inconsistent state.
5884 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
5886 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5889 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5892 * Ideally we would like to be able to randomly
5893 * call vdev_[on|off]line without holding locks
5894 * to force unpredictable failures but the side
5895 * effects of vdev_[on|off]line prevent us from
5896 * doing so. We grab the ztest_vdev_lock here to
5897 * prevent a race between injection testing and
5900 mutex_enter(&ztest_vdev_lock
);
5901 (void) vdev_online(spa
, guid0
, 0, NULL
);
5902 mutex_exit(&ztest_vdev_lock
);
5910 * We have at least single-fault tolerance, so inject data corruption.
5912 fd
= open(pathrand
, O_RDWR
);
5914 if (fd
== -1) /* we hit a gap in the device namespace */
5917 fsize
= lseek(fd
, 0, SEEK_END
);
5919 while (--iters
!= 0) {
5921 * The offset must be chosen carefully to ensure that
5922 * we do not inject a given logical block with errors
5923 * on two different leaf devices, because ZFS can not
5924 * tolerate that (if maxfaults==1).
5926 * We divide each leaf into chunks of size
5927 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5928 * there is a series of ranges to which we can inject errors.
5929 * Each range can accept errors on only a single leaf vdev.
5930 * The error injection ranges are separated by ranges
5931 * which we will not inject errors on any device (DMZs).
5932 * Each DMZ must be large enough such that a single block
5933 * can not straddle it, so that a single block can not be
5934 * a target in two different injection ranges (on different
5937 * For example, with 3 leaves, each chunk looks like:
5938 * 0 to 32M: injection range for leaf 0
5939 * 32M to 64M: DMZ - no injection allowed
5940 * 64M to 96M: injection range for leaf 1
5941 * 96M to 128M: DMZ - no injection allowed
5942 * 128M to 160M: injection range for leaf 2
5943 * 160M to 192M: DMZ - no injection allowed
5945 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5946 (leaves
<< bshift
) + (leaf
<< bshift
) +
5947 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5950 * Only allow damage to the labels at one end of the vdev.
5952 * If all labels are damaged, the device will be totally
5953 * inaccessible, which will result in loss of data,
5954 * because we also damage (parts of) the other side of
5957 * Additionally, we will always have both an even and an
5958 * odd label, so that we can handle crashes in the
5959 * middle of vdev_config_sync().
5961 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5965 * The two end labels are stored at the "end" of the disk, but
5966 * the end of the disk (vdev_psize) is aligned to
5967 * sizeof (vdev_label_t).
5969 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5970 if ((leaf
& 1) == 1 &&
5971 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5974 mutex_enter(&ztest_vdev_lock
);
5975 if (mirror_save
!= zs
->zs_mirrors
) {
5976 mutex_exit(&ztest_vdev_lock
);
5981 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5982 fatal(1, "can't inject bad word at 0x%llx in %s",
5985 mutex_exit(&ztest_vdev_lock
);
5987 if (ztest_opts
.zo_verbose
>= 7)
5988 (void) printf("injected bad word into %s,"
5989 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5994 umem_free(path0
, MAXPATHLEN
);
5995 umem_free(pathrand
, MAXPATHLEN
);
5999 * Verify that DDT repair works as expected.
6002 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
6004 ztest_shared_t
*zs
= ztest_shared
;
6005 spa_t
*spa
= ztest_spa
;
6006 objset_t
*os
= zd
->zd_os
;
6008 uint64_t object
, blocksize
, txg
, pattern
, psize
;
6009 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
6014 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
6017 blocksize
= ztest_random_blocksize();
6018 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
6020 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
6021 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
6023 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
6024 umem_free(od
, sizeof (ztest_od_t
));
6029 * Take the name lock as writer to prevent anyone else from changing
6030 * the pool and dataset properies we need to maintain during this test.
6032 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6034 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
6036 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
6038 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6039 umem_free(od
, sizeof (ztest_od_t
));
6043 dmu_objset_stats_t dds
;
6044 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6045 dmu_objset_fast_stat(os
, &dds
);
6046 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6048 object
= od
[0].od_object
;
6049 blocksize
= od
[0].od_blocksize
;
6050 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
6052 ASSERT(object
!= 0);
6054 tx
= dmu_tx_create(os
);
6055 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
6056 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
6058 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6059 umem_free(od
, sizeof (ztest_od_t
));
6064 * Write all the copies of our block.
6066 for (i
= 0; i
< copies
; i
++) {
6067 uint64_t offset
= i
* blocksize
;
6068 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
6069 DMU_READ_NO_PREFETCH
);
6071 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
6072 os
, (long long)object
, (long long) offset
, error
);
6074 ASSERT(db
->db_offset
== offset
);
6075 ASSERT(db
->db_size
== blocksize
);
6076 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
6077 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
6078 dmu_buf_will_fill(db
, tx
);
6079 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
6080 dmu_buf_rele(db
, FTAG
);
6084 txg_wait_synced(spa_get_dsl(spa
), txg
);
6087 * Find out what block we got.
6089 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
6090 DMU_READ_NO_PREFETCH
));
6091 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
6092 dmu_buf_rele(db
, FTAG
);
6095 * Damage the block. Dedup-ditto will save us when we read it later.
6097 psize
= BP_GET_PSIZE(&blk
);
6098 abd
= abd_alloc_linear(psize
, B_TRUE
);
6099 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
6101 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
6102 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
6103 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
6107 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6108 umem_free(od
, sizeof (ztest_od_t
));
6116 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6118 spa_t
*spa
= ztest_spa
;
6121 * Scrub in progress by device removal.
6123 if (ztest_device_removal_active
)
6126 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6127 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
6128 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6132 * Change the guid for the pool.
6136 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6138 spa_t
*spa
= ztest_spa
;
6139 uint64_t orig
, load
;
6142 if (ztest_opts
.zo_mmp_test
)
6145 orig
= spa_guid(spa
);
6146 load
= spa_load_guid(spa
);
6148 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6149 error
= spa_change_guid(spa
);
6150 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6155 if (ztest_opts
.zo_verbose
>= 4) {
6156 (void) printf("Changed guid old %llu -> %llu\n",
6157 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6160 VERIFY3U(orig
, !=, spa_guid(spa
));
6161 VERIFY3U(load
, ==, spa_load_guid(spa
));
6165 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6167 hrtime_t end
= gethrtime() + NANOSEC
;
6169 while (gethrtime() <= end
) {
6170 int run_count
= 100;
6172 struct abd
*abd_data
, *abd_meta
;
6177 zio_cksum_t zc_ref_byteswap
;
6179 size
= ztest_random_blocksize();
6181 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6182 abd_data
= abd_alloc(size
, B_FALSE
);
6183 abd_meta
= abd_alloc(size
, B_TRUE
);
6185 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6186 *ptr
= ztest_random(UINT_MAX
);
6188 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6189 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6191 VERIFY0(fletcher_4_impl_set("scalar"));
6192 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6193 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6195 VERIFY0(fletcher_4_impl_set("cycle"));
6196 while (run_count
-- > 0) {
6198 zio_cksum_t zc_byteswap
;
6200 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6201 fletcher_4_native(buf
, size
, NULL
, &zc
);
6203 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6204 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6205 sizeof (zc_byteswap
)));
6207 /* Test ABD - data */
6208 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6210 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6212 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6213 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6214 sizeof (zc_byteswap
)));
6216 /* Test ABD - metadata */
6217 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6219 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6221 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6222 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6223 sizeof (zc_byteswap
)));
6227 umem_free(buf
, size
);
6234 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6241 zio_cksum_t zc_ref_bswap
;
6243 hrtime_t end
= gethrtime() + NANOSEC
;
6245 while (gethrtime() <= end
) {
6246 int run_count
= 100;
6248 size
= ztest_random_blocksize();
6249 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6251 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6252 *ptr
= ztest_random(UINT_MAX
);
6254 VERIFY0(fletcher_4_impl_set("scalar"));
6255 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6256 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6258 VERIFY0(fletcher_4_impl_set("cycle"));
6260 while (run_count
-- > 0) {
6262 zio_cksum_t zc_bswap
;
6265 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6266 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6268 while (pos
< size
) {
6269 size_t inc
= 64 * ztest_random(size
/ 67);
6270 /* sometimes add few bytes to test non-simd */
6271 if (ztest_random(100) < 10)
6272 inc
+= P2ALIGN(ztest_random(64),
6275 if (inc
> (size
- pos
))
6278 fletcher_4_incremental_native(buf
+ pos
, inc
,
6280 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6286 VERIFY3U(pos
, ==, size
);
6288 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6289 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6292 * verify if incremental on the whole buffer is
6293 * equivalent to non-incremental version
6295 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6296 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6298 fletcher_4_incremental_native(buf
, size
, &zc
);
6299 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6301 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6302 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6305 umem_free(buf
, size
);
6310 ztest_check_path(char *path
)
6313 /* return true on success */
6314 return (!stat(path
, &s
));
6318 ztest_get_zdb_bin(char *bin
, int len
)
6322 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6323 * let popen to search through PATH.
6325 if ((zdb_path
= getenv("ZDB_PATH"))) {
6326 strlcpy(bin
, zdb_path
, len
); /* In env */
6327 if (!ztest_check_path(bin
)) {
6328 ztest_dump_core
= 0;
6329 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6334 VERIFY(realpath(getexecname(), bin
) != NULL
);
6335 if (strstr(bin
, "/ztest/")) {
6336 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6337 strcat(bin
, "/zdb/zdb");
6338 if (ztest_check_path(bin
))
6345 * Verify pool integrity by running zdb.
6348 ztest_run_zdb(char *pool
)
6354 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6357 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6358 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6359 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6361 ztest_get_zdb_bin(bin
, len
);
6364 "%s -bcc%s%s -G -d -U %s "
6365 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
6367 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6368 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6372 if (ztest_opts
.zo_verbose
>= 5)
6373 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6375 fp
= popen(zdb
, "r");
6377 while (fgets(zbuf
, 1024, fp
) != NULL
)
6378 if (ztest_opts
.zo_verbose
>= 3)
6379 (void) printf("%s", zbuf
);
6381 status
= pclose(fp
);
6386 ztest_dump_core
= 0;
6387 if (WIFEXITED(status
))
6388 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6390 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6392 umem_free(bin
, len
);
6393 umem_free(zdb
, len
);
6394 umem_free(zbuf
, 1024);
6398 ztest_walk_pool_directory(char *header
)
6402 if (ztest_opts
.zo_verbose
>= 6)
6403 (void) printf("%s\n", header
);
6405 mutex_enter(&spa_namespace_lock
);
6406 while ((spa
= spa_next(spa
)) != NULL
)
6407 if (ztest_opts
.zo_verbose
>= 6)
6408 (void) printf("\t%s\n", spa_name(spa
));
6409 mutex_exit(&spa_namespace_lock
);
6413 ztest_spa_import_export(char *oldname
, char *newname
)
6415 nvlist_t
*config
, *newconfig
;
6420 if (ztest_opts
.zo_verbose
>= 4) {
6421 (void) printf("import/export: old = %s, new = %s\n",
6426 * Clean up from previous runs.
6428 (void) spa_destroy(newname
);
6431 * Get the pool's configuration and guid.
6433 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6436 * Kick off a scrub to tickle scrub/export races.
6438 if (ztest_random(2) == 0)
6439 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6441 pool_guid
= spa_guid(spa
);
6442 spa_close(spa
, FTAG
);
6444 ztest_walk_pool_directory("pools before export");
6449 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6451 ztest_walk_pool_directory("pools after export");
6456 newconfig
= spa_tryimport(config
);
6457 ASSERT(newconfig
!= NULL
);
6458 nvlist_free(newconfig
);
6461 * Import it under the new name.
6463 error
= spa_import(newname
, config
, NULL
, 0);
6465 dump_nvlist(config
, 0);
6466 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6467 oldname
, newname
, error
);
6470 ztest_walk_pool_directory("pools after import");
6473 * Try to import it again -- should fail with EEXIST.
6475 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6478 * Try to import it under a different name -- should fail with EEXIST.
6480 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6483 * Verify that the pool is no longer visible under the old name.
6485 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6488 * Verify that we can open and close the pool using the new name.
6490 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6491 ASSERT(pool_guid
== spa_guid(spa
));
6492 spa_close(spa
, FTAG
);
6494 nvlist_free(config
);
6498 ztest_resume(spa_t
*spa
)
6500 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6501 (void) printf("resuming from suspended state\n");
6502 spa_vdev_state_enter(spa
, SCL_NONE
);
6503 vdev_clear(spa
, NULL
);
6504 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6505 (void) zio_resume(spa
);
6509 ztest_resume_thread(void *arg
)
6513 while (!ztest_exiting
) {
6514 if (spa_suspended(spa
))
6516 (void) poll(NULL
, 0, 100);
6519 * Periodically change the zfs_compressed_arc_enabled setting.
6521 if (ztest_random(10) == 0)
6522 zfs_compressed_arc_enabled
= ztest_random(2);
6525 * Periodically change the zfs_abd_scatter_enabled setting.
6527 if (ztest_random(10) == 0)
6528 zfs_abd_scatter_enabled
= ztest_random(2);
6535 ztest_deadman_thread(void *arg
)
6537 ztest_shared_t
*zs
= arg
;
6538 spa_t
*spa
= ztest_spa
;
6539 hrtime_t delay
, overdue
, last_run
= gethrtime();
6541 delay
= (zs
->zs_thread_stop
- zs
->zs_thread_start
) +
6542 MSEC2NSEC(zfs_deadman_synctime_ms
);
6544 while (!ztest_exiting
) {
6546 * Wait for the delay timer while checking occasionally
6547 * if we should stop.
6549 if (gethrtime() < last_run
+ delay
) {
6550 (void) poll(NULL
, 0, 1000);
6555 * If the pool is suspended then fail immediately. Otherwise,
6556 * check to see if the pool is making any progress. If
6557 * vdev_deadman() discovers that there hasn't been any recent
6558 * I/Os then it will end up aborting the tests.
6560 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6561 fatal(0, "aborting test after %llu seconds because "
6562 "pool has transitioned to a suspended state.",
6563 zfs_deadman_synctime_ms
/ 1000);
6565 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6568 * If the process doesn't complete within a grace period of
6569 * zfs_deadman_synctime_ms over the expected finish time,
6570 * then it may be hung and is terminated.
6572 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6573 if (gethrtime() > overdue
) {
6574 fatal(0, "aborting test after %llu seconds because "
6575 "the process is overdue for termination.",
6576 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6579 (void) printf("ztest has been running for %lld seconds\n",
6580 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6582 last_run
= gethrtime();
6583 delay
= MSEC2NSEC(zfs_deadman_checktime_ms
);
6590 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6592 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6593 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6594 hrtime_t functime
= gethrtime();
6597 for (i
= 0; i
< zi
->zi_iters
; i
++)
6598 zi
->zi_func(zd
, id
);
6600 functime
= gethrtime() - functime
;
6602 atomic_add_64(&zc
->zc_count
, 1);
6603 atomic_add_64(&zc
->zc_time
, functime
);
6605 if (ztest_opts
.zo_verbose
>= 4)
6606 (void) printf("%6.2f sec in %s\n",
6607 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6611 ztest_thread(void *arg
)
6614 uint64_t id
= (uintptr_t)arg
;
6615 ztest_shared_t
*zs
= ztest_shared
;
6619 ztest_shared_callstate_t
*zc
;
6621 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6623 * See if it's time to force a crash.
6625 if (now
> zs
->zs_thread_kill
)
6629 * If we're getting ENOSPC with some regularity, stop.
6631 if (zs
->zs_enospc_count
> 10)
6635 * Pick a random function to execute.
6637 rand
= ztest_random(ZTEST_FUNCS
);
6638 zi
= &ztest_info
[rand
];
6639 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6640 call_next
= zc
->zc_next
;
6642 if (now
>= call_next
&&
6643 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6644 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6645 ztest_execute(rand
, zi
, id
);
6653 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6655 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6659 ztest_dataset_destroy(int d
)
6661 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6664 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6666 if (ztest_opts
.zo_verbose
>= 3)
6667 (void) printf("Destroying %s to free up space\n", name
);
6670 * Cleanup any non-standard clones and snapshots. In general,
6671 * ztest thread t operates on dataset (t % zopt_datasets),
6672 * so there may be more than one thing to clean up.
6674 for (t
= d
; t
< ztest_opts
.zo_threads
;
6675 t
+= ztest_opts
.zo_datasets
)
6676 ztest_dsl_dataset_cleanup(name
, t
);
6678 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6679 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6683 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6685 uint64_t usedobjs
, dirobjs
, scratch
;
6688 * ZTEST_DIROBJ is the object directory for the entire dataset.
6689 * Therefore, the number of objects in use should equal the
6690 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6691 * If not, we have an object leak.
6693 * Note that we can only check this in ztest_dataset_open(),
6694 * when the open-context and syncing-context values agree.
6695 * That's because zap_count() returns the open-context value,
6696 * while dmu_objset_space() returns the rootbp fill count.
6698 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6699 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6700 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6704 ztest_dataset_open(int d
)
6706 ztest_ds_t
*zd
= &ztest_ds
[d
];
6707 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6710 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6713 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6715 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6717 error
= ztest_dataset_create(name
);
6718 if (error
== ENOSPC
) {
6719 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6720 ztest_record_enospc(FTAG
);
6723 ASSERT(error
== 0 || error
== EEXIST
);
6725 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6727 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6729 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6731 zilog
= zd
->zd_zilog
;
6733 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6734 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6735 fatal(0, "missing log records: claimed %llu < committed %llu",
6736 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6738 ztest_dataset_dirobj_verify(zd
);
6740 zil_replay(os
, zd
, ztest_replay_vector
);
6742 ztest_dataset_dirobj_verify(zd
);
6744 if (ztest_opts
.zo_verbose
>= 6)
6745 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6747 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6748 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6749 (u_longlong_t
)zilog
->zl_replaying_seq
);
6751 zilog
= zil_open(os
, ztest_get_data
);
6753 if (zilog
->zl_replaying_seq
!= 0 &&
6754 zilog
->zl_replaying_seq
< committed_seq
)
6755 fatal(0, "missing log records: replayed %llu < committed %llu",
6756 zilog
->zl_replaying_seq
, committed_seq
);
6762 ztest_dataset_close(int d
)
6764 ztest_ds_t
*zd
= &ztest_ds
[d
];
6766 zil_close(zd
->zd_zilog
);
6767 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6774 ztest_replay_zil_cb(const char *name
, void *arg
)
6779 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_ANY
, B_TRUE
,
6780 B_TRUE
, FTAG
, &os
));
6782 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
6784 ztest_zd_init(zdtmp
, NULL
, os
);
6785 zil_replay(os
, zdtmp
, ztest_replay_vector
);
6786 ztest_zd_fini(zdtmp
);
6788 if (dmu_objset_zil(os
)->zl_parse_lr_count
!= 0 &&
6789 ztest_opts
.zo_verbose
>= 6) {
6790 zilog_t
*zilog
= dmu_objset_zil(os
);
6792 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6794 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6795 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6796 (u_longlong_t
)zilog
->zl_replaying_seq
);
6799 umem_free(zdtmp
, sizeof (ztest_ds_t
));
6801 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6806 * Kick off threads to run tests on all datasets in parallel.
6809 ztest_run(ztest_shared_t
*zs
)
6813 kthread_t
*resume_thread
, *deadman_thread
;
6814 kthread_t
**run_threads
;
6819 ztest_exiting
= B_FALSE
;
6822 * Initialize parent/child shared state.
6824 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6825 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6826 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
6828 zs
->zs_thread_start
= gethrtime();
6829 zs
->zs_thread_stop
=
6830 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6831 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6832 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6833 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6834 zs
->zs_thread_kill
-=
6835 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6838 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6840 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6841 offsetof(ztest_cb_data_t
, zcd_node
));
6846 kernel_init(FREAD
| FWRITE
);
6847 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6848 metaslab_preload_limit
= ztest_random(20) + 1;
6851 dmu_objset_stats_t dds
;
6852 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
6853 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
6854 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6855 dmu_objset_fast_stat(os
, &dds
);
6856 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6857 zs
->zs_guid
= dds
.dds_guid
;
6858 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6860 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6863 * Create a thread to periodically resume suspended I/O.
6865 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
6866 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6869 * Create a deadman thread and set to panic if we hang.
6871 deadman_thread
= thread_create(NULL
, 0, ztest_deadman_thread
,
6872 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6874 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
6877 * Verify that we can safely inquire about any object,
6878 * whether it's allocated or not. To make it interesting,
6879 * we probe a 5-wide window around each power of two.
6880 * This hits all edge cases, including zero and the max.
6882 for (t
= 0; t
< 64; t
++) {
6883 for (d
= -5; d
<= 5; d
++) {
6884 error
= dmu_object_info(spa
->spa_meta_objset
,
6885 (1ULL << t
) + d
, NULL
);
6886 ASSERT(error
== 0 || error
== ENOENT
||
6892 * If we got any ENOSPC errors on the previous run, destroy something.
6894 if (zs
->zs_enospc_count
!= 0) {
6895 int d
= ztest_random(ztest_opts
.zo_datasets
);
6896 ztest_dataset_destroy(d
);
6898 zs
->zs_enospc_count
= 0;
6901 * If we were in the middle of ztest_device_removal() and were killed
6902 * we need to ensure the removal and scrub complete before running
6903 * any tests that check ztest_device_removal_active. The removal will
6904 * be restarted automatically when the spa is opened, but we need to
6905 * initate the scrub manually if it is not already in progress. Note
6906 * that we always run the scrub whenever an indirect vdev exists
6907 * because we have no way of knowing for sure if ztest_device_removal()
6908 * fully completed its scrub before the pool was reimported.
6910 if (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
||
6911 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6912 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
6913 txg_wait_synced(spa_get_dsl(spa
), 0);
6915 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6916 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
6917 txg_wait_synced(spa_get_dsl(spa
), 0);
6920 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
6923 if (ztest_opts
.zo_verbose
>= 4)
6924 (void) printf("starting main threads...\n");
6927 * Replay all logs of all datasets in the pool. This is primarily for
6928 * temporary datasets which wouldn't otherwise get replayed, which
6929 * can trigger failures when attempting to offline a SLOG in
6930 * ztest_fault_inject().
6932 (void) dmu_objset_find(ztest_opts
.zo_pool
, ztest_replay_zil_cb
,
6933 NULL
, DS_FIND_CHILDREN
);
6936 * Kick off all the tests that run in parallel.
6938 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6939 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
6940 umem_free(run_threads
, ztest_opts
.zo_threads
*
6941 sizeof (kthread_t
*));
6945 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
6946 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
6951 * Wait for all of the tests to complete.
6953 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++)
6954 VERIFY0(thread_join(run_threads
[t
]));
6957 * Close all datasets. This must be done after all the threads
6958 * are joined so we can be sure none of the datasets are in-use
6959 * by any of the threads.
6961 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6962 if (t
< ztest_opts
.zo_datasets
)
6963 ztest_dataset_close(t
);
6966 txg_wait_synced(spa_get_dsl(spa
), 0);
6968 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
6969 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
6971 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
6973 /* Kill the resume and deadman threads */
6974 ztest_exiting
= B_TRUE
;
6975 VERIFY0(thread_join(resume_thread
));
6976 VERIFY0(thread_join(deadman_thread
));
6980 * Right before closing the pool, kick off a bunch of async I/O;
6981 * spa_close() should wait for it to complete.
6983 for (object
= 1; object
< 50; object
++) {
6984 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
6985 ZIO_PRIORITY_SYNC_READ
);
6988 /* Verify that at least one commit cb was called in a timely fashion */
6989 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
6990 VERIFY0(zc_min_txg_delay
);
6992 spa_close(spa
, FTAG
);
6995 * Verify that we can loop over all pools.
6997 mutex_enter(&spa_namespace_lock
);
6998 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
6999 if (ztest_opts
.zo_verbose
> 3)
7000 (void) printf("spa_next: found %s\n", spa_name(spa
));
7001 mutex_exit(&spa_namespace_lock
);
7004 * Verify that we can export the pool and reimport it under a
7007 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7008 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7009 (void) snprintf(name
, sizeof (name
), "%s_import",
7010 ztest_opts
.zo_pool
);
7011 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7012 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7017 list_destroy(&zcl
.zcl_callbacks
);
7018 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7019 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7020 mutex_destroy(&ztest_vdev_lock
);
7021 mutex_destroy(&ztest_checkpoint_lock
);
7027 ztest_ds_t
*zd
= &ztest_ds
[0];
7031 if (ztest_opts
.zo_verbose
>= 3)
7032 (void) printf("testing spa_freeze()...\n");
7034 kernel_init(FREAD
| FWRITE
);
7035 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7036 VERIFY3U(0, ==, ztest_dataset_open(0));
7040 * Force the first log block to be transactionally allocated.
7041 * We have to do this before we freeze the pool -- otherwise
7042 * the log chain won't be anchored.
7044 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
7045 ztest_dmu_object_alloc_free(zd
, 0);
7046 zil_commit(zd
->zd_zilog
, 0);
7049 txg_wait_synced(spa_get_dsl(spa
), 0);
7052 * Freeze the pool. This stops spa_sync() from doing anything,
7053 * so that the only way to record changes from now on is the ZIL.
7058 * Because it is hard to predict how much space a write will actually
7059 * require beforehand, we leave ourselves some fudge space to write over
7062 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
7065 * Run tests that generate log records but don't alter the pool config
7066 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
7067 * We do a txg_wait_synced() after each iteration to force the txg
7068 * to increase well beyond the last synced value in the uberblock.
7069 * The ZIL should be OK with that.
7071 * Run a random number of times less than zo_maxloops and ensure we do
7072 * not run out of space on the pool.
7074 while (ztest_random(10) != 0 &&
7075 numloops
++ < ztest_opts
.zo_maxloops
&&
7076 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
7078 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
7079 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
7080 ztest_io(zd
, od
.od_object
,
7081 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
7082 txg_wait_synced(spa_get_dsl(spa
), 0);
7086 * Commit all of the changes we just generated.
7088 zil_commit(zd
->zd_zilog
, 0);
7089 txg_wait_synced(spa_get_dsl(spa
), 0);
7092 * Close our dataset and close the pool.
7094 ztest_dataset_close(0);
7095 spa_close(spa
, FTAG
);
7099 * Open and close the pool and dataset to induce log replay.
7101 kernel_init(FREAD
| FWRITE
);
7102 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7103 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
7104 VERIFY3U(0, ==, ztest_dataset_open(0));
7106 txg_wait_synced(spa_get_dsl(spa
), 0);
7107 ztest_dataset_close(0);
7108 ztest_reguid(NULL
, 0);
7110 spa_close(spa
, FTAG
);
7115 print_time(hrtime_t t
, char *timebuf
)
7117 hrtime_t s
= t
/ NANOSEC
;
7118 hrtime_t m
= s
/ 60;
7119 hrtime_t h
= m
/ 60;
7120 hrtime_t d
= h
/ 24;
7129 (void) sprintf(timebuf
,
7130 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7132 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7134 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7136 (void) sprintf(timebuf
, "%llus", s
);
7140 make_random_props(void)
7144 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7146 if (ztest_random(2) == 0)
7149 VERIFY0(nvlist_add_uint64(props
,
7150 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7156 * Import a storage pool with the given name.
7159 ztest_import(ztest_shared_t
*zs
)
7161 importargs_t args
= { 0 };
7163 nvlist_t
*cfg
= NULL
;
7165 char *searchdirs
[nsearch
];
7166 char *name
= ztest_opts
.zo_pool
;
7167 int flags
= ZFS_IMPORT_MISSING_LOG
;
7170 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7171 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7172 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7174 kernel_init(FREAD
| FWRITE
);
7176 searchdirs
[0] = ztest_opts
.zo_dir
;
7177 args
.paths
= nsearch
;
7178 args
.path
= searchdirs
;
7179 args
.can_be_active
= B_FALSE
;
7181 error
= zpool_find_config(NULL
, name
, &cfg
, &args
,
7182 &libzpool_config_ops
);
7184 (void) fatal(0, "No pools found\n");
7186 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
7187 VERIFY0(spa_open(name
, &spa
, FTAG
));
7188 zs
->zs_metaslab_sz
=
7189 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7190 spa_close(spa
, FTAG
);
7194 if (!ztest_opts
.zo_mmp_test
) {
7195 ztest_run_zdb(ztest_opts
.zo_pool
);
7197 ztest_run_zdb(ztest_opts
.zo_pool
);
7200 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7201 mutex_destroy(&ztest_vdev_lock
);
7202 mutex_destroy(&ztest_checkpoint_lock
);
7206 * Create a storage pool with the given name and initial vdev size.
7207 * Then test spa_freeze() functionality.
7210 ztest_init(ztest_shared_t
*zs
)
7213 nvlist_t
*nvroot
, *props
;
7216 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7217 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7218 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7220 kernel_init(FREAD
| FWRITE
);
7223 * Create the storage pool.
7225 (void) spa_destroy(ztest_opts
.zo_pool
);
7226 ztest_shared
->zs_vdev_next_leaf
= 0;
7228 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7229 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7230 NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
7231 props
= make_random_props();
7234 * We don't expect the pool to suspend unless maxfaults == 0,
7235 * in which case ztest_fault_inject() temporarily takes away
7236 * the only valid replica.
7238 VERIFY0(nvlist_add_uint64(props
,
7239 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7240 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7242 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7244 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7245 spa_feature_table
[i
].fi_uname
));
7246 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7250 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7251 nvlist_free(nvroot
);
7254 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7255 zs
->zs_metaslab_sz
=
7256 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7257 spa_close(spa
, FTAG
);
7261 if (!ztest_opts
.zo_mmp_test
) {
7262 ztest_run_zdb(ztest_opts
.zo_pool
);
7264 ztest_run_zdb(ztest_opts
.zo_pool
);
7267 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7268 mutex_destroy(&ztest_vdev_lock
);
7269 mutex_destroy(&ztest_checkpoint_lock
);
7275 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7277 ztest_fd_data
= mkstemp(ztest_name_data
);
7278 ASSERT3S(ztest_fd_data
, >=, 0);
7279 (void) unlink(ztest_name_data
);
7283 shared_data_size(ztest_shared_hdr_t
*hdr
)
7287 size
= hdr
->zh_hdr_size
;
7288 size
+= hdr
->zh_opts_size
;
7289 size
+= hdr
->zh_size
;
7290 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7291 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7300 ztest_shared_hdr_t
*hdr
;
7302 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7303 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7304 ASSERT(hdr
!= MAP_FAILED
);
7306 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7308 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7309 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7310 hdr
->zh_size
= sizeof (ztest_shared_t
);
7311 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7312 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7313 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7314 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7316 size
= shared_data_size(hdr
);
7317 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7319 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7326 ztest_shared_hdr_t
*hdr
;
7329 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7330 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7331 ASSERT(hdr
!= MAP_FAILED
);
7333 size
= shared_data_size(hdr
);
7335 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7336 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7337 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7338 ASSERT(hdr
!= MAP_FAILED
);
7339 buf
= (uint8_t *)hdr
;
7341 offset
= hdr
->zh_hdr_size
;
7342 ztest_shared_opts
= (void *)&buf
[offset
];
7343 offset
+= hdr
->zh_opts_size
;
7344 ztest_shared
= (void *)&buf
[offset
];
7345 offset
+= hdr
->zh_size
;
7346 ztest_shared_callstate
= (void *)&buf
[offset
];
7347 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7348 ztest_shared_ds
= (void *)&buf
[offset
];
7352 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7356 char *cmdbuf
= NULL
;
7361 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7362 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7367 fatal(1, "fork failed");
7369 if (pid
== 0) { /* child */
7370 char *emptyargv
[2] = { cmd
, NULL
};
7371 char fd_data_str
[12];
7373 struct rlimit rl
= { 1024, 1024 };
7374 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7376 (void) close(ztest_fd_rand
);
7377 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7378 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7380 (void) enable_extended_FILE_stdio(-1, -1);
7381 if (libpath
!= NULL
)
7382 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7383 (void) execv(cmd
, emptyargv
);
7384 ztest_dump_core
= B_FALSE
;
7385 fatal(B_TRUE
, "exec failed: %s", cmd
);
7388 if (cmdbuf
!= NULL
) {
7389 umem_free(cmdbuf
, MAXPATHLEN
);
7393 while (waitpid(pid
, &status
, 0) != pid
)
7395 if (statusp
!= NULL
)
7398 if (WIFEXITED(status
)) {
7399 if (WEXITSTATUS(status
) != 0) {
7400 (void) fprintf(stderr
, "child exited with code %d\n",
7401 WEXITSTATUS(status
));
7405 } else if (WIFSIGNALED(status
)) {
7406 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7407 (void) fprintf(stderr
, "child died with signal %d\n",
7413 (void) fprintf(stderr
, "something strange happened to child\n");
7420 ztest_run_init(void)
7424 ztest_shared_t
*zs
= ztest_shared
;
7427 * Blow away any existing copy of zpool.cache
7429 (void) remove(spa_config_path
);
7431 if (ztest_opts
.zo_init
== 0) {
7432 if (ztest_opts
.zo_verbose
>= 1)
7433 (void) printf("Importing pool %s\n",
7434 ztest_opts
.zo_pool
);
7440 * Create and initialize our storage pool.
7442 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7443 bzero(zs
, sizeof (ztest_shared_t
));
7444 if (ztest_opts
.zo_verbose
>= 3 &&
7445 ztest_opts
.zo_init
!= 1) {
7446 (void) printf("ztest_init(), pass %d\n", i
);
7453 main(int argc
, char **argv
)
7461 ztest_shared_callstate_t
*zc
;
7463 char numbuf
[NN_NUMBUF_SZ
];
7467 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7468 struct sigaction action
;
7470 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7472 dprintf_setup(&argc
, argv
);
7473 zfs_deadman_synctime_ms
= 300000;
7474 zfs_deadman_checktime_ms
= 30000;
7476 * As two-word space map entries may not come up often (especially
7477 * if pool and vdev sizes are small) we want to force at least some
7478 * of them so the feature get tested.
7480 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7483 * Verify that even extensively damaged split blocks with many
7484 * segments can be reconstructed in a reasonable amount of time
7485 * when reconstruction is known to be possible.
7487 * Note: the lower this value is, the more damage we inflict, and
7488 * the more time ztest spends in recovering that damage. We chose
7489 * to induce damage 1/100th of the time so recovery is tested but
7490 * not so frequently that ztest doesn't get to test other code paths.
7492 zfs_reconstruct_indirect_damage_fraction
= 100;
7494 action
.sa_handler
= sig_handler
;
7495 sigemptyset(&action
.sa_mask
);
7496 action
.sa_flags
= 0;
7498 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7499 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7504 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7505 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7511 * Force random_get_bytes() to use /dev/urandom in order to prevent
7512 * ztest from needlessly depleting the system entropy pool.
7514 random_path
= "/dev/urandom";
7515 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7516 ASSERT3S(ztest_fd_rand
, >=, 0);
7519 process_options(argc
, argv
);
7524 bcopy(&ztest_opts
, ztest_shared_opts
,
7525 sizeof (*ztest_shared_opts
));
7527 ztest_fd_data
= atoi(fd_data_str
);
7529 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7531 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7533 /* Override location of zpool.cache */
7534 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7535 ztest_opts
.zo_dir
) != -1);
7537 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7542 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7543 metaslab_df_alloc_threshold
=
7544 zs
->zs_metaslab_df_alloc_threshold
;
7553 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7555 if (ztest_opts
.zo_verbose
>= 1) {
7556 (void) printf("%llu vdevs, %d datasets, %d threads,"
7557 " %llu seconds...\n",
7558 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7559 ztest_opts
.zo_datasets
,
7560 ztest_opts
.zo_threads
,
7561 (u_longlong_t
)ztest_opts
.zo_time
);
7564 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7565 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7567 zs
->zs_do_init
= B_TRUE
;
7568 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7569 if (ztest_opts
.zo_verbose
>= 1) {
7570 (void) printf("Executing older ztest for "
7571 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7573 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7574 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7576 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7578 zs
->zs_do_init
= B_FALSE
;
7580 zs
->zs_proc_start
= gethrtime();
7581 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7583 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7584 zi
= &ztest_info
[f
];
7585 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7586 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7587 zc
->zc_next
= UINT64_MAX
;
7589 zc
->zc_next
= zs
->zs_proc_start
+
7590 ztest_random(2 * zi
->zi_interval
[0] + 1);
7594 * Run the tests in a loop. These tests include fault injection
7595 * to verify that self-healing data works, and forced crashes
7596 * to verify that we never lose on-disk consistency.
7598 while (gethrtime() < zs
->zs_proc_stop
) {
7603 * Initialize the workload counters for each function.
7605 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7606 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7611 /* Set the allocation switch size */
7612 zs
->zs_metaslab_df_alloc_threshold
=
7613 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7615 if (!hasalt
|| ztest_random(2) == 0) {
7616 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7617 (void) printf("Executing newer ztest: %s\n",
7621 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7623 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7624 (void) printf("Executing older ztest: %s\n",
7625 ztest_opts
.zo_alt_ztest
);
7628 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7629 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7636 if (ztest_opts
.zo_verbose
>= 1) {
7637 hrtime_t now
= gethrtime();
7639 now
= MIN(now
, zs
->zs_proc_stop
);
7640 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7641 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7643 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7644 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7646 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7647 (u_longlong_t
)zs
->zs_enospc_count
,
7648 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7650 100.0 * (now
- zs
->zs_proc_start
) /
7651 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7654 if (ztest_opts
.zo_verbose
>= 2) {
7655 (void) printf("\nWorkload summary:\n\n");
7656 (void) printf("%7s %9s %s\n",
7657 "Calls", "Time", "Function");
7658 (void) printf("%7s %9s %s\n",
7659 "-----", "----", "--------");
7660 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7661 zi
= &ztest_info
[f
];
7662 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7663 print_time(zc
->zc_time
, timebuf
);
7664 (void) printf("%7llu %9s %s\n",
7665 (u_longlong_t
)zc
->zc_count
, timebuf
,
7668 (void) printf("\n");
7671 if (!ztest_opts
.zo_mmp_test
)
7672 ztest_run_zdb(ztest_opts
.zo_pool
);
7675 if (ztest_opts
.zo_verbose
>= 1) {
7677 (void) printf("%d runs of older ztest: %s\n", older
,
7678 ztest_opts
.zo_alt_ztest
);
7679 (void) printf("%d runs of newer ztest: %s\n", newer
,
7682 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7683 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7686 umem_free(cmd
, MAXNAMELEN
);