4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
28 * Copyright (c) 2017, Intel Corporation.
32 * The objective of this program is to provide a DMU/ZAP/SPA stress test
33 * that runs entirely in userland, is easy to use, and easy to extend.
35 * The overall design of the ztest program is as follows:
37 * (1) For each major functional area (e.g. adding vdevs to a pool,
38 * creating and destroying datasets, reading and writing objects, etc)
39 * we have a simple routine to test that functionality. These
40 * individual routines do not have to do anything "stressful".
42 * (2) We turn these simple functionality tests into a stress test by
43 * running them all in parallel, with as many threads as desired,
44 * and spread across as many datasets, objects, and vdevs as desired.
46 * (3) While all this is happening, we inject faults into the pool to
47 * verify that self-healing data really works.
49 * (4) Every time we open a dataset, we change its checksum and compression
50 * functions. Thus even individual objects vary from block to block
51 * in which checksum they use and whether they're compressed.
53 * (5) To verify that we never lose on-disk consistency after a crash,
54 * we run the entire test in a child of the main process.
55 * At random times, the child self-immolates with a SIGKILL.
56 * This is the software equivalent of pulling the power cord.
57 * The parent then runs the test again, using the existing
58 * storage pool, as many times as desired. If backwards compatibility
59 * testing is enabled ztest will sometimes run the "older" version
60 * of ztest after a SIGKILL.
62 * (6) To verify that we don't have future leaks or temporal incursions,
63 * many of the functional tests record the transaction group number
64 * as part of their data. When reading old data, they verify that
65 * the transaction group number is less than the current, open txg.
66 * If you add a new test, please do this if applicable.
68 * (7) Threads are created with a reduced stack size, for sanity checking.
69 * Therefore, it's important not to allocate huge buffers on the stack.
71 * When run with no arguments, ztest runs for about five minutes and
72 * produces no output if successful. To get a little bit of information,
73 * specify -V. To get more information, specify -VV, and so on.
75 * To turn this into an overnight stress test, use -T to specify run time.
77 * You can ask more vdevs [-v], datasets [-d], or threads [-t]
78 * to increase the pool capacity, fanout, and overall stress level.
80 * Use the -k option to set the desired frequency of kills.
82 * When ztest invokes itself it passes all relevant information through a
83 * temporary file which is mmap-ed in the child process. This allows shared
84 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
85 * stored at offset 0 of this file and contains information on the size and
86 * number of shared structures in the file. The information stored in this file
87 * must remain backwards compatible with older versions of ztest so that
88 * ztest can invoke them during backwards compatibility testing (-B).
91 #include <sys/zfs_context.h>
97 #include <sys/dmu_objset.h>
100 #include <sys/time.h>
101 #include <sys/wait.h>
102 #include <sys/mman.h>
103 #include <sys/resource.h>
106 #include <sys/zil_impl.h>
107 #include <sys/vdev_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/vdev_initialize.h>
110 #include <sys/vdev_raidz.h>
111 #include <sys/vdev_trim.h>
112 #include <sys/spa_impl.h>
113 #include <sys/metaslab_impl.h>
114 #include <sys/dsl_prop.h>
115 #include <sys/dsl_dataset.h>
116 #include <sys/dsl_destroy.h>
117 #include <sys/dsl_scan.h>
118 #include <sys/zio_checksum.h>
119 #include <sys/refcount.h>
120 #include <sys/zfeature.h>
121 #include <sys/dsl_userhold.h>
130 #include <sys/fs/zfs.h>
131 #include <zfs_fletcher.h>
132 #include <libnvpair.h>
133 #include <libzutil.h>
134 #include <sys/crypto/icp.h>
136 #include <execinfo.h> /* for backtrace() */
139 static int ztest_fd_data
= -1;
140 static int ztest_fd_rand
= -1;
142 typedef struct ztest_shared_hdr
{
143 uint64_t zh_hdr_size
;
144 uint64_t zh_opts_size
;
146 uint64_t zh_stats_size
;
147 uint64_t zh_stats_count
;
149 uint64_t zh_ds_count
;
150 } ztest_shared_hdr_t
;
152 static ztest_shared_hdr_t
*ztest_shared_hdr
;
154 enum ztest_class_state
{
155 ZTEST_VDEV_CLASS_OFF
,
160 typedef struct ztest_shared_opts
{
161 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
162 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
163 char zo_alt_ztest
[MAXNAMELEN
];
164 char zo_alt_libpath
[MAXNAMELEN
];
166 uint64_t zo_vdevtime
;
174 uint64_t zo_passtime
;
175 uint64_t zo_killrate
;
179 uint64_t zo_maxloops
;
180 uint64_t zo_metaslab_force_ganging
;
182 int zo_special_vdevs
;
184 } ztest_shared_opts_t
;
186 static const ztest_shared_opts_t ztest_opts_defaults
= {
189 .zo_alt_ztest
= { '\0' },
190 .zo_alt_libpath
= { '\0' },
192 .zo_ashift
= SPA_MINBLOCKSHIFT
,
195 .zo_raidz_parity
= 1,
196 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
199 .zo_passtime
= 60, /* 60 seconds */
200 .zo_killrate
= 70, /* 70% kill rate */
204 .zo_time
= 300, /* 5 minutes */
205 .zo_maxloops
= 50, /* max loops during spa_freeze() */
206 .zo_metaslab_force_ganging
= 64 << 10,
207 .zo_special_vdevs
= ZTEST_VDEV_CLASS_RND
,
210 extern uint64_t metaslab_force_ganging
;
211 extern uint64_t metaslab_df_alloc_threshold
;
212 extern unsigned long zfs_deadman_synctime_ms
;
213 extern int metaslab_preload_limit
;
214 extern boolean_t zfs_compressed_arc_enabled
;
215 extern int zfs_abd_scatter_enabled
;
216 extern int dmu_object_alloc_chunk_shift
;
217 extern boolean_t zfs_force_some_double_word_sm_entries
;
218 extern unsigned long zio_decompress_fail_fraction
;
219 extern unsigned long zfs_reconstruct_indirect_damage_fraction
;
222 static ztest_shared_opts_t
*ztest_shared_opts
;
223 static ztest_shared_opts_t ztest_opts
;
224 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
226 typedef struct ztest_shared_ds
{
230 static ztest_shared_ds_t
*ztest_shared_ds
;
231 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
233 #define BT_MAGIC 0x123456789abcdefULL
234 #define MAXFAULTS(zs) \
235 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
239 ZTEST_IO_WRITE_PATTERN
,
240 ZTEST_IO_WRITE_ZEROES
,
247 typedef struct ztest_block_tag
{
251 uint64_t bt_dnodesize
;
258 typedef struct bufwad
{
265 * It would be better to use a rangelock_t per object. Unfortunately
266 * the rangelock_t is not a drop-in replacement for rl_t, because we
267 * still need to map from object ID to rangelock_t.
289 #define ZTEST_RANGE_LOCKS 64
290 #define ZTEST_OBJECT_LOCKS 64
293 * Object descriptor. Used as a template for object lookup/create/remove.
295 typedef struct ztest_od
{
298 dmu_object_type_t od_type
;
299 dmu_object_type_t od_crtype
;
300 uint64_t od_blocksize
;
301 uint64_t od_crblocksize
;
302 uint64_t od_crdnodesize
;
305 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
311 typedef struct ztest_ds
{
312 ztest_shared_ds_t
*zd_shared
;
314 pthread_rwlock_t zd_zilog_lock
;
316 ztest_od_t
*zd_od
; /* debugging aid */
317 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
318 kmutex_t zd_dirobj_lock
;
319 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
320 rll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
324 * Per-iteration state.
326 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
328 typedef struct ztest_info
{
329 ztest_func_t
*zi_func
; /* test function */
330 uint64_t zi_iters
; /* iterations per execution */
331 uint64_t *zi_interval
; /* execute every <interval> seconds */
332 const char *zi_funcname
; /* name of test function */
335 typedef struct ztest_shared_callstate
{
336 uint64_t zc_count
; /* per-pass count */
337 uint64_t zc_time
; /* per-pass time */
338 uint64_t zc_next
; /* next time to call this function */
339 } ztest_shared_callstate_t
;
341 static ztest_shared_callstate_t
*ztest_shared_callstate
;
342 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
344 ztest_func_t ztest_dmu_read_write
;
345 ztest_func_t ztest_dmu_write_parallel
;
346 ztest_func_t ztest_dmu_object_alloc_free
;
347 ztest_func_t ztest_dmu_object_next_chunk
;
348 ztest_func_t ztest_dmu_commit_callbacks
;
349 ztest_func_t ztest_zap
;
350 ztest_func_t ztest_zap_parallel
;
351 ztest_func_t ztest_zil_commit
;
352 ztest_func_t ztest_zil_remount
;
353 ztest_func_t ztest_dmu_read_write_zcopy
;
354 ztest_func_t ztest_dmu_objset_create_destroy
;
355 ztest_func_t ztest_dmu_prealloc
;
356 ztest_func_t ztest_fzap
;
357 ztest_func_t ztest_dmu_snapshot_create_destroy
;
358 ztest_func_t ztest_dsl_prop_get_set
;
359 ztest_func_t ztest_spa_prop_get_set
;
360 ztest_func_t ztest_spa_create_destroy
;
361 ztest_func_t ztest_fault_inject
;
362 ztest_func_t ztest_dmu_snapshot_hold
;
363 ztest_func_t ztest_mmp_enable_disable
;
364 ztest_func_t ztest_scrub
;
365 ztest_func_t ztest_dsl_dataset_promote_busy
;
366 ztest_func_t ztest_vdev_attach_detach
;
367 ztest_func_t ztest_vdev_LUN_growth
;
368 ztest_func_t ztest_vdev_add_remove
;
369 ztest_func_t ztest_vdev_class_add
;
370 ztest_func_t ztest_vdev_aux_add_remove
;
371 ztest_func_t ztest_split_pool
;
372 ztest_func_t ztest_reguid
;
373 ztest_func_t ztest_spa_upgrade
;
374 ztest_func_t ztest_device_removal
;
375 ztest_func_t ztest_spa_checkpoint_create_discard
;
376 ztest_func_t ztest_initialize
;
377 ztest_func_t ztest_trim
;
378 ztest_func_t ztest_fletcher
;
379 ztest_func_t ztest_fletcher_incr
;
380 ztest_func_t ztest_verify_dnode_bt
;
382 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
383 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
384 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
385 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
386 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
388 #define ZTI_INIT(func, iters, interval) \
389 { .zi_func = (func), \
390 .zi_iters = (iters), \
391 .zi_interval = (interval), \
392 .zi_funcname = # func }
394 ztest_info_t ztest_info
[] = {
395 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
396 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
397 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
398 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
399 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
400 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
401 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
402 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
403 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
404 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
405 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
406 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
407 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
408 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
410 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
412 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
413 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
414 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
415 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
416 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
417 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
418 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
419 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
420 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
421 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
422 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
423 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
424 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
425 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
426 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
427 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
428 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
429 ZTI_INIT(ztest_initialize
, 1, &zopt_sometimes
),
430 ZTI_INIT(ztest_trim
, 1, &zopt_sometimes
),
431 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
432 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
433 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
436 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
439 * The following struct is used to hold a list of uncalled commit callbacks.
440 * The callbacks are ordered by txg number.
442 typedef struct ztest_cb_list
{
443 kmutex_t zcl_callbacks_lock
;
444 list_t zcl_callbacks
;
448 * Stuff we need to share writably between parent and child.
450 typedef struct ztest_shared
{
451 boolean_t zs_do_init
;
452 hrtime_t zs_proc_start
;
453 hrtime_t zs_proc_stop
;
454 hrtime_t zs_thread_start
;
455 hrtime_t zs_thread_stop
;
456 hrtime_t zs_thread_kill
;
457 uint64_t zs_enospc_count
;
458 uint64_t zs_vdev_next_leaf
;
459 uint64_t zs_vdev_aux
;
464 uint64_t zs_metaslab_sz
;
465 uint64_t zs_metaslab_df_alloc_threshold
;
469 #define ID_PARALLEL -1ULL
471 static char ztest_dev_template
[] = "%s/%s.%llua";
472 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
473 ztest_shared_t
*ztest_shared
;
475 static spa_t
*ztest_spa
= NULL
;
476 static ztest_ds_t
*ztest_ds
;
478 static kmutex_t ztest_vdev_lock
;
479 static boolean_t ztest_device_removal_active
= B_FALSE
;
480 static boolean_t ztest_pool_scrubbed
= B_FALSE
;
481 static kmutex_t ztest_checkpoint_lock
;
484 * The ztest_name_lock protects the pool and dataset namespace used by
485 * the individual tests. To modify the namespace, consumers must grab
486 * this lock as writer. Grabbing the lock as reader will ensure that the
487 * namespace does not change while the lock is held.
489 static pthread_rwlock_t ztest_name_lock
;
491 static boolean_t ztest_dump_core
= B_TRUE
;
492 static boolean_t ztest_exiting
;
494 /* Global commit callback list */
495 static ztest_cb_list_t zcl
;
496 /* Commit cb delay */
497 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
498 static int zc_cb_counter
= 0;
501 * Minimum number of commit callbacks that need to be registered for us to check
502 * whether the minimum txg delay is acceptable.
504 #define ZTEST_COMMIT_CB_MIN_REG 100
507 * If a number of txgs equal to this threshold have been created after a commit
508 * callback has been registered but not called, then we assume there is an
509 * implementation bug.
511 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
514 ZTEST_META_DNODE
= 0,
519 static void usage(boolean_t
) __NORETURN
;
520 static int ztest_scrub_impl(spa_t
*spa
);
523 * These libumem hooks provide a reasonable set of defaults for the allocator's
524 * debugging facilities.
527 _umem_debug_init(void)
529 return ("default,verbose"); /* $UMEM_DEBUG setting */
533 _umem_logging_init(void)
535 return ("fail,contents"); /* $UMEM_LOGGING setting */
539 dump_debug_buffer(void)
541 ssize_t ret
__attribute__((unused
));
543 if (!ztest_opts
.zo_dump_dbgmsg
)
547 * We use write() instead of printf() so that this function
548 * is safe to call from a signal handler.
550 ret
= write(STDOUT_FILENO
, "\n", 1);
551 zfs_dbgmsg_print("ztest");
554 #define BACKTRACE_SZ 100
556 static void sig_handler(int signo
)
558 struct sigaction action
;
559 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
561 void *buffer
[BACKTRACE_SZ
];
563 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
564 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
569 * Restore default action and re-raise signal so SIGSEGV and
570 * SIGABRT can trigger a core dump.
572 action
.sa_handler
= SIG_DFL
;
573 sigemptyset(&action
.sa_mask
);
575 (void) sigaction(signo
, &action
, NULL
);
579 #define FATAL_MSG_SZ 1024
584 fatal(int do_perror
, char *message
, ...)
587 int save_errno
= errno
;
590 (void) fflush(stdout
);
591 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
593 va_start(args
, message
);
594 (void) sprintf(buf
, "ztest: ");
596 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
599 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
600 ": %s", strerror(save_errno
));
602 (void) fprintf(stderr
, "%s\n", buf
);
603 fatal_msg
= buf
; /* to ease debugging */
614 str2shift(const char *buf
)
616 const char *ends
= "BKMGTPEZ";
621 for (i
= 0; i
< strlen(ends
); i
++) {
622 if (toupper(buf
[0]) == ends
[i
])
625 if (i
== strlen(ends
)) {
626 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
630 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
633 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
639 nicenumtoull(const char *buf
)
644 val
= strtoull(buf
, &end
, 0);
646 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
648 } else if (end
[0] == '.') {
649 double fval
= strtod(buf
, &end
);
650 fval
*= pow(2, str2shift(end
));
652 * UINT64_MAX is not exactly representable as a double.
653 * The closest representation is UINT64_MAX + 1, so we
654 * use a >= comparison instead of > for the bounds check.
656 if (fval
>= (double)UINT64_MAX
) {
657 (void) fprintf(stderr
, "ztest: value too large: %s\n",
661 val
= (uint64_t)fval
;
663 int shift
= str2shift(end
);
664 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
665 (void) fprintf(stderr
, "ztest: value too large: %s\n",
675 usage(boolean_t requested
)
677 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
679 char nice_vdev_size
[NN_NUMBUF_SZ
];
680 char nice_force_ganging
[NN_NUMBUF_SZ
];
681 FILE *fp
= requested
? stdout
: stderr
;
683 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
684 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
685 sizeof (nice_force_ganging
));
687 (void) fprintf(fp
, "Usage: %s\n"
688 "\t[-v vdevs (default: %llu)]\n"
689 "\t[-s size_of_each_vdev (default: %s)]\n"
690 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
691 "\t[-m mirror_copies (default: %d)]\n"
692 "\t[-r raidz_disks (default: %d)]\n"
693 "\t[-R raidz_parity (default: %d)]\n"
694 "\t[-d datasets (default: %d)]\n"
695 "\t[-t threads (default: %d)]\n"
696 "\t[-g gang_block_threshold (default: %s)]\n"
697 "\t[-i init_count (default: %d)] initialize pool i times\n"
698 "\t[-k kill_percentage (default: %llu%%)]\n"
699 "\t[-p pool_name (default: %s)]\n"
700 "\t[-f dir (default: %s)] file directory for vdev files\n"
701 "\t[-M] Multi-host simulate pool imported on remote host\n"
702 "\t[-V] verbose (use multiple times for ever more blather)\n"
703 "\t[-E] use existing pool instead of creating new one\n"
704 "\t[-T time (default: %llu sec)] total run time\n"
705 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
706 "\t[-P passtime (default: %llu sec)] time per pass\n"
707 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
708 "\t[-C vdev class state (default: random)] special=on|off|random\n"
709 "\t[-o variable=value] ... set global variable to an unsigned\n"
710 "\t 32-bit integer value\n"
711 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
712 "\t[-h] (print help)\n"
715 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
716 nice_vdev_size
, /* -s */
717 zo
->zo_ashift
, /* -a */
718 zo
->zo_mirrors
, /* -m */
719 zo
->zo_raidz
, /* -r */
720 zo
->zo_raidz_parity
, /* -R */
721 zo
->zo_datasets
, /* -d */
722 zo
->zo_threads
, /* -t */
723 nice_force_ganging
, /* -g */
724 zo
->zo_init
, /* -i */
725 (u_longlong_t
)zo
->zo_killrate
, /* -k */
726 zo
->zo_pool
, /* -p */
728 (u_longlong_t
)zo
->zo_time
, /* -T */
729 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
730 (u_longlong_t
)zo
->zo_passtime
);
731 exit(requested
? 0 : 1);
736 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
740 int state
= ZTEST_VDEV_CLASS_RND
;
742 (void) strlcpy(name
, input
, sizeof (name
));
744 value
= strchr(name
, '=');
746 (void) fprintf(stderr
, "missing value in property=value "
747 "'-C' argument (%s)\n", input
);
753 if (strcmp(value
, "on") == 0) {
754 state
= ZTEST_VDEV_CLASS_ON
;
755 } else if (strcmp(value
, "off") == 0) {
756 state
= ZTEST_VDEV_CLASS_OFF
;
757 } else if (strcmp(value
, "random") == 0) {
758 state
= ZTEST_VDEV_CLASS_RND
;
760 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
764 if (strcmp(name
, "special") == 0) {
765 zo
->zo_special_vdevs
= state
;
767 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
770 if (zo
->zo_verbose
>= 3)
771 (void) printf("%s vdev state is '%s'\n", name
, value
);
775 process_options(int argc
, char **argv
)
778 ztest_shared_opts_t
*zo
= &ztest_opts
;
782 char altdir
[MAXNAMELEN
] = { 0 };
784 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
786 while ((opt
= getopt(argc
, argv
,
787 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
804 value
= nicenumtoull(optarg
);
808 zo
->zo_vdevs
= value
;
811 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
814 zo
->zo_ashift
= value
;
817 zo
->zo_mirrors
= value
;
820 zo
->zo_raidz
= MAX(1, value
);
823 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
826 zo
->zo_datasets
= MAX(1, value
);
829 zo
->zo_threads
= MAX(1, value
);
832 zo
->zo_metaslab_force_ganging
=
833 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
839 zo
->zo_killrate
= value
;
842 (void) strlcpy(zo
->zo_pool
, optarg
,
843 sizeof (zo
->zo_pool
));
846 path
= realpath(optarg
, NULL
);
848 (void) fprintf(stderr
, "error: %s: %s\n",
849 optarg
, strerror(errno
));
852 (void) strlcpy(zo
->zo_dir
, path
,
853 sizeof (zo
->zo_dir
));
870 zo
->zo_passtime
= MAX(1, value
);
873 zo
->zo_maxloops
= MAX(1, value
);
876 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
879 ztest_parse_name_value(optarg
, zo
);
882 if (set_global_var(optarg
) != 0)
886 zo
->zo_dump_dbgmsg
= 1;
898 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
901 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
904 if (strlen(altdir
) > 0) {
912 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
913 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
915 VERIFY(NULL
!= realpath(getexecname(), cmd
));
916 if (0 != access(altdir
, F_OK
)) {
917 ztest_dump_core
= B_FALSE
;
918 fatal(B_TRUE
, "invalid alternate ztest path: %s",
921 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
924 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
925 * We want to extract <isa> to determine if we should use
926 * 32 or 64 bit binaries.
928 bin
= strstr(cmd
, "/usr/bin/");
929 ztest
= strstr(bin
, "/ztest");
931 isalen
= ztest
- isa
;
932 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
933 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
934 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
935 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
937 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
938 ztest_dump_core
= B_FALSE
;
939 fatal(B_TRUE
, "invalid alternate ztest: %s",
941 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
942 ztest_dump_core
= B_FALSE
;
943 fatal(B_TRUE
, "invalid alternate lib directory %s",
947 umem_free(cmd
, MAXPATHLEN
);
948 umem_free(realaltdir
, MAXPATHLEN
);
953 ztest_kill(ztest_shared_t
*zs
)
955 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
956 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
959 * Before we kill off ztest, make sure that the config is updated.
960 * See comment above spa_write_cachefile().
962 mutex_enter(&spa_namespace_lock
);
963 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
964 mutex_exit(&spa_namespace_lock
);
966 (void) kill(getpid(), SIGKILL
);
970 ztest_random(uint64_t range
)
974 ASSERT3S(ztest_fd_rand
, >=, 0);
979 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
980 fatal(1, "short read from /dev/urandom");
987 ztest_record_enospc(const char *s
)
989 ztest_shared
->zs_enospc_count
++;
993 ztest_get_ashift(void)
995 if (ztest_opts
.zo_ashift
== 0)
996 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
997 return (ztest_opts
.zo_ashift
);
1001 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
1007 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1010 ashift
= ztest_get_ashift();
1016 vdev
= ztest_shared
->zs_vdev_aux
;
1017 (void) snprintf(path
, MAXPATHLEN
,
1018 ztest_aux_template
, ztest_opts
.zo_dir
,
1019 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
1022 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
1023 (void) snprintf(path
, MAXPATHLEN
,
1024 ztest_dev_template
, ztest_opts
.zo_dir
,
1025 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1030 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1032 fatal(1, "can't open %s", path
);
1033 if (ftruncate(fd
, size
) != 0)
1034 fatal(1, "can't ftruncate %s", path
);
1038 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
1039 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
1040 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
1041 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
1042 umem_free(pathbuf
, MAXPATHLEN
);
1048 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
1049 uint64_t ashift
, int r
)
1051 nvlist_t
*raidz
, **child
;
1055 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1056 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1058 for (c
= 0; c
< r
; c
++)
1059 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1061 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
1062 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
1063 VDEV_TYPE_RAIDZ
) == 0);
1064 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
1065 ztest_opts
.zo_raidz_parity
) == 0);
1066 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
1069 for (c
= 0; c
< r
; c
++)
1070 nvlist_free(child
[c
]);
1072 umem_free(child
, r
* sizeof (nvlist_t
*));
1078 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1079 uint64_t ashift
, int r
, int m
)
1081 nvlist_t
*mirror
, **child
;
1085 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
1087 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1089 for (c
= 0; c
< m
; c
++)
1090 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1092 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1093 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1094 VDEV_TYPE_MIRROR
) == 0);
1095 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1098 for (c
= 0; c
< m
; c
++)
1099 nvlist_free(child
[c
]);
1101 umem_free(child
, m
* sizeof (nvlist_t
*));
1107 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1108 const char *class, int r
, int m
, int t
)
1110 nvlist_t
*root
, **child
;
1116 log
= (class != NULL
&& strcmp(class, "log") == 0);
1118 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1120 for (c
= 0; c
< t
; c
++) {
1121 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1123 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1126 if (class != NULL
&& class[0] != '\0') {
1127 ASSERT(m
> 1 || log
); /* expecting a mirror */
1128 VERIFY(nvlist_add_string(child
[c
],
1129 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1133 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1134 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1135 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1138 for (c
= 0; c
< t
; c
++)
1139 nvlist_free(child
[c
]);
1141 umem_free(child
, t
* sizeof (nvlist_t
*));
1147 * Find a random spa version. Returns back a random spa version in the
1148 * range [initial_version, SPA_VERSION_FEATURES].
1151 ztest_random_spa_version(uint64_t initial_version
)
1153 uint64_t version
= initial_version
;
1155 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1157 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1160 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1161 version
= SPA_VERSION_FEATURES
;
1163 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1168 ztest_random_blocksize(void)
1170 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1173 * Choose a block size >= the ashift.
1174 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1176 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1177 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1179 uint64_t block_shift
=
1180 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1181 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1185 ztest_random_dnodesize(void)
1188 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1190 if (max_slots
== DNODE_MIN_SLOTS
)
1191 return (DNODE_MIN_SIZE
);
1194 * Weight the random distribution more heavily toward smaller
1195 * dnode sizes since that is more likely to reflect real-world
1198 ASSERT3U(max_slots
, >, 4);
1199 switch (ztest_random(10)) {
1201 slots
= 5 + ztest_random(max_slots
- 4);
1204 slots
= 2 + ztest_random(3);
1211 return (slots
<< DNODE_SHIFT
);
1215 ztest_random_ibshift(void)
1217 return (DN_MIN_INDBLKSHIFT
+
1218 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1222 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1225 vdev_t
*rvd
= spa
->spa_root_vdev
;
1228 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1231 top
= ztest_random(rvd
->vdev_children
);
1232 tvd
= rvd
->vdev_child
[top
];
1233 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1234 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1240 ztest_random_dsl_prop(zfs_prop_t prop
)
1245 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1246 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1252 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1255 const char *propname
= zfs_prop_to_name(prop
);
1256 const char *valname
;
1261 error
= dsl_prop_set_int(osname
, propname
,
1262 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1264 if (error
== ENOSPC
) {
1265 ztest_record_enospc(FTAG
);
1270 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1271 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1273 if (ztest_opts
.zo_verbose
>= 6) {
1276 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1278 (void) printf("%s %s = %llu at '%s'\n", osname
,
1279 propname
, (unsigned long long)curval
, setpoint
);
1281 (void) printf("%s %s = %s at '%s'\n",
1282 osname
, propname
, valname
, setpoint
);
1284 umem_free(setpoint
, MAXPATHLEN
);
1290 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1292 spa_t
*spa
= ztest_spa
;
1293 nvlist_t
*props
= NULL
;
1296 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1297 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1299 error
= spa_prop_set(spa
, props
);
1303 if (error
== ENOSPC
) {
1304 ztest_record_enospc(FTAG
);
1313 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1314 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1318 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1320 strcpy(ddname
, name
);
1321 cp
= strchr(ddname
, '@');
1325 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1326 while (decrypt
&& err
== EACCES
) {
1327 dsl_crypto_params_t
*dcp
;
1328 nvlist_t
*crypto_args
= fnvlist_alloc();
1330 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1331 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1332 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1333 crypto_args
, &dcp
));
1334 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1335 dsl_crypto_params_free(dcp
, B_FALSE
);
1336 fnvlist_free(crypto_args
);
1338 if (err
== EINVAL
) {
1340 * We couldn't load a key for this dataset so try
1341 * the parent. This loop will eventually hit the
1342 * encryption root since ztest only makes clones
1343 * as children of their origin datasets.
1345 cp
= strrchr(ddname
, '/');
1352 } else if (err
!= 0) {
1356 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1364 ztest_rll_init(rll_t
*rll
)
1366 rll
->rll_writer
= NULL
;
1367 rll
->rll_readers
= 0;
1368 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1369 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1373 ztest_rll_destroy(rll_t
*rll
)
1375 ASSERT(rll
->rll_writer
== NULL
);
1376 ASSERT(rll
->rll_readers
== 0);
1377 mutex_destroy(&rll
->rll_lock
);
1378 cv_destroy(&rll
->rll_cv
);
1382 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1384 mutex_enter(&rll
->rll_lock
);
1386 if (type
== RL_READER
) {
1387 while (rll
->rll_writer
!= NULL
)
1388 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1391 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1392 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1393 rll
->rll_writer
= curthread
;
1396 mutex_exit(&rll
->rll_lock
);
1400 ztest_rll_unlock(rll_t
*rll
)
1402 mutex_enter(&rll
->rll_lock
);
1404 if (rll
->rll_writer
) {
1405 ASSERT(rll
->rll_readers
== 0);
1406 rll
->rll_writer
= NULL
;
1408 ASSERT(rll
->rll_readers
!= 0);
1409 ASSERT(rll
->rll_writer
== NULL
);
1413 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1414 cv_broadcast(&rll
->rll_cv
);
1416 mutex_exit(&rll
->rll_lock
);
1420 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1422 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1424 ztest_rll_lock(rll
, type
);
1428 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1430 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1432 ztest_rll_unlock(rll
);
1436 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1437 uint64_t size
, rl_type_t type
)
1439 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1440 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1443 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1444 rl
->rl_object
= object
;
1445 rl
->rl_offset
= offset
;
1449 ztest_rll_lock(rll
, type
);
1455 ztest_range_unlock(rl_t
*rl
)
1457 rll_t
*rll
= rl
->rl_lock
;
1459 ztest_rll_unlock(rll
);
1461 umem_free(rl
, sizeof (*rl
));
1465 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1468 zd
->zd_zilog
= dmu_objset_zil(os
);
1469 zd
->zd_shared
= szd
;
1470 dmu_objset_name(os
, zd
->zd_name
);
1473 if (zd
->zd_shared
!= NULL
)
1474 zd
->zd_shared
->zd_seq
= 0;
1476 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1477 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1479 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1480 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1482 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1483 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1487 ztest_zd_fini(ztest_ds_t
*zd
)
1491 mutex_destroy(&zd
->zd_dirobj_lock
);
1492 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1494 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1495 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1497 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1498 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1501 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1504 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1510 * Attempt to assign tx to some transaction group.
1512 error
= dmu_tx_assign(tx
, txg_how
);
1514 if (error
== ERESTART
) {
1515 ASSERT(txg_how
== TXG_NOWAIT
);
1518 ASSERT3U(error
, ==, ENOSPC
);
1519 ztest_record_enospc(tag
);
1524 txg
= dmu_tx_get_txg(tx
);
1530 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1531 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1534 bt
->bt_magic
= BT_MAGIC
;
1535 bt
->bt_objset
= dmu_objset_id(os
);
1536 bt
->bt_object
= object
;
1537 bt
->bt_dnodesize
= dnodesize
;
1538 bt
->bt_offset
= offset
;
1541 bt
->bt_crtxg
= crtxg
;
1545 ztest_bt_verify(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 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1550 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1551 ASSERT3U(bt
->bt_object
, ==, object
);
1552 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1553 ASSERT3U(bt
->bt_offset
, ==, offset
);
1554 ASSERT3U(bt
->bt_gen
, <=, gen
);
1555 ASSERT3U(bt
->bt_txg
, <=, txg
);
1556 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1559 static ztest_block_tag_t
*
1560 ztest_bt_bonus(dmu_buf_t
*db
)
1562 dmu_object_info_t doi
;
1563 ztest_block_tag_t
*bt
;
1565 dmu_object_info_from_db(db
, &doi
);
1566 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1567 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1568 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1574 * Generate a token to fill up unused bonus buffer space. Try to make
1575 * it unique to the object, generation, and offset to verify that data
1576 * is not getting overwritten by data from other dnodes.
1578 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1579 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1582 * Fill up the unused bonus buffer region before the block tag with a
1583 * verifiable pattern. Filling the whole bonus area with non-zero data
1584 * helps ensure that all dnode traversal code properly skips the
1585 * interior regions of large dnodes.
1588 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1589 objset_t
*os
, uint64_t gen
)
1593 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1595 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1596 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1597 gen
, bonusp
- (uint64_t *)db
->db_data
);
1603 * Verify that the unused area of a bonus buffer is filled with the
1607 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1608 objset_t
*os
, uint64_t gen
)
1612 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1613 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1614 gen
, bonusp
- (uint64_t *)db
->db_data
);
1615 VERIFY3U(*bonusp
, ==, token
);
1623 #define lrz_type lr_mode
1624 #define lrz_blocksize lr_uid
1625 #define lrz_ibshift lr_gid
1626 #define lrz_bonustype lr_rdev
1627 #define lrz_dnodesize lr_crtime[1]
1630 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1632 char *name
= (void *)(lr
+ 1); /* name follows lr */
1633 size_t namesize
= strlen(name
) + 1;
1636 if (zil_replaying(zd
->zd_zilog
, tx
))
1639 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1640 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1641 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1643 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1647 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1649 char *name
= (void *)(lr
+ 1); /* name follows lr */
1650 size_t namesize
= strlen(name
) + 1;
1653 if (zil_replaying(zd
->zd_zilog
, tx
))
1656 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1657 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1658 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1660 itx
->itx_oid
= object
;
1661 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1665 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1668 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1670 if (zil_replaying(zd
->zd_zilog
, tx
))
1673 if (lr
->lr_length
> zil_max_log_data(zd
->zd_zilog
))
1674 write_state
= WR_INDIRECT
;
1676 itx
= zil_itx_create(TX_WRITE
,
1677 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1679 if (write_state
== WR_COPIED
&&
1680 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1681 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1682 zil_itx_destroy(itx
);
1683 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1684 write_state
= WR_NEED_COPY
;
1686 itx
->itx_private
= zd
;
1687 itx
->itx_wr_state
= write_state
;
1688 itx
->itx_sync
= (ztest_random(8) == 0);
1690 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1691 sizeof (*lr
) - sizeof (lr_t
));
1693 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1697 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1701 if (zil_replaying(zd
->zd_zilog
, tx
))
1704 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1705 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1706 sizeof (*lr
) - sizeof (lr_t
));
1708 itx
->itx_sync
= B_FALSE
;
1709 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1713 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1717 if (zil_replaying(zd
->zd_zilog
, tx
))
1720 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1721 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1722 sizeof (*lr
) - sizeof (lr_t
));
1724 itx
->itx_sync
= B_FALSE
;
1725 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1732 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1734 ztest_ds_t
*zd
= arg1
;
1735 lr_create_t
*lr
= arg2
;
1736 char *name
= (void *)(lr
+ 1); /* name follows lr */
1737 objset_t
*os
= zd
->zd_os
;
1738 ztest_block_tag_t
*bbt
;
1746 byteswap_uint64_array(lr
, sizeof (*lr
));
1748 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1749 ASSERT(name
[0] != '\0');
1751 tx
= dmu_tx_create(os
);
1753 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1755 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1756 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1758 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1761 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1765 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1766 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1768 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1769 if (lr
->lr_foid
== 0) {
1770 lr
->lr_foid
= zap_create_dnsize(os
,
1771 lr
->lrz_type
, lr
->lrz_bonustype
,
1772 bonuslen
, lr
->lrz_dnodesize
, tx
);
1774 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1775 lr
->lrz_type
, lr
->lrz_bonustype
,
1776 bonuslen
, lr
->lrz_dnodesize
, tx
);
1779 if (lr
->lr_foid
== 0) {
1780 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1781 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1782 bonuslen
, lr
->lrz_dnodesize
, tx
);
1784 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1785 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1786 bonuslen
, lr
->lrz_dnodesize
, tx
);
1791 ASSERT3U(error
, ==, EEXIST
);
1792 ASSERT(zd
->zd_zilog
->zl_replay
);
1797 ASSERT(lr
->lr_foid
!= 0);
1799 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1800 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1801 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1803 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1804 bbt
= ztest_bt_bonus(db
);
1805 dmu_buf_will_dirty(db
, tx
);
1806 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1807 lr
->lr_gen
, txg
, txg
);
1808 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1809 dmu_buf_rele(db
, FTAG
);
1811 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1814 (void) ztest_log_create(zd
, tx
, lr
);
1822 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1824 ztest_ds_t
*zd
= arg1
;
1825 lr_remove_t
*lr
= arg2
;
1826 char *name
= (void *)(lr
+ 1); /* name follows lr */
1827 objset_t
*os
= zd
->zd_os
;
1828 dmu_object_info_t doi
;
1830 uint64_t object
, txg
;
1833 byteswap_uint64_array(lr
, sizeof (*lr
));
1835 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1836 ASSERT(name
[0] != '\0');
1839 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1840 ASSERT(object
!= 0);
1842 ztest_object_lock(zd
, object
, RL_WRITER
);
1844 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1846 tx
= dmu_tx_create(os
);
1848 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1849 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1851 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1853 ztest_object_unlock(zd
, object
);
1857 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1858 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1860 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1863 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1865 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1869 ztest_object_unlock(zd
, object
);
1875 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1877 ztest_ds_t
*zd
= arg1
;
1878 lr_write_t
*lr
= arg2
;
1879 objset_t
*os
= zd
->zd_os
;
1880 void *data
= lr
+ 1; /* data follows lr */
1881 uint64_t offset
, length
;
1882 ztest_block_tag_t
*bt
= data
;
1883 ztest_block_tag_t
*bbt
;
1884 uint64_t gen
, txg
, lrtxg
, crtxg
;
1885 dmu_object_info_t doi
;
1888 arc_buf_t
*abuf
= NULL
;
1892 byteswap_uint64_array(lr
, sizeof (*lr
));
1894 offset
= lr
->lr_offset
;
1895 length
= lr
->lr_length
;
1897 /* If it's a dmu_sync() block, write the whole block */
1898 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1899 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1900 if (length
< blocksize
) {
1901 offset
-= offset
% blocksize
;
1906 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1907 byteswap_uint64_array(bt
, sizeof (*bt
));
1909 if (bt
->bt_magic
!= BT_MAGIC
)
1912 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1913 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1915 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1917 dmu_object_info_from_db(db
, &doi
);
1919 bbt
= ztest_bt_bonus(db
);
1920 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1922 crtxg
= bbt
->bt_crtxg
;
1923 lrtxg
= lr
->lr_common
.lrc_txg
;
1925 tx
= dmu_tx_create(os
);
1927 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1929 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1930 P2PHASE(offset
, length
) == 0)
1931 abuf
= dmu_request_arcbuf(db
, length
);
1933 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1936 dmu_return_arcbuf(abuf
);
1937 dmu_buf_rele(db
, FTAG
);
1938 ztest_range_unlock(rl
);
1939 ztest_object_unlock(zd
, lr
->lr_foid
);
1945 * Usually, verify the old data before writing new data --
1946 * but not always, because we also want to verify correct
1947 * behavior when the data was not recently read into cache.
1949 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1950 if (ztest_random(4) != 0) {
1951 int prefetch
= ztest_random(2) ?
1952 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1953 ztest_block_tag_t rbt
;
1955 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1956 sizeof (rbt
), &rbt
, prefetch
) == 0);
1957 if (rbt
.bt_magic
== BT_MAGIC
) {
1958 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1959 offset
, gen
, txg
, crtxg
);
1964 * Writes can appear to be newer than the bonus buffer because
1965 * the ztest_get_data() callback does a dmu_read() of the
1966 * open-context data, which may be different than the data
1967 * as it was when the write was generated.
1969 if (zd
->zd_zilog
->zl_replay
) {
1970 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1971 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1976 * Set the bt's gen/txg to the bonus buffer's gen/txg
1977 * so that all of the usual ASSERTs will work.
1979 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
1984 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
1986 bcopy(data
, abuf
->b_data
, length
);
1987 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
1990 (void) ztest_log_write(zd
, tx
, lr
);
1992 dmu_buf_rele(db
, FTAG
);
1996 ztest_range_unlock(rl
);
1997 ztest_object_unlock(zd
, lr
->lr_foid
);
2003 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2005 ztest_ds_t
*zd
= arg1
;
2006 lr_truncate_t
*lr
= arg2
;
2007 objset_t
*os
= zd
->zd_os
;
2013 byteswap_uint64_array(lr
, sizeof (*lr
));
2015 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2016 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2019 tx
= dmu_tx_create(os
);
2021 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2023 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2025 ztest_range_unlock(rl
);
2026 ztest_object_unlock(zd
, lr
->lr_foid
);
2030 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2031 lr
->lr_length
, tx
) == 0);
2033 (void) ztest_log_truncate(zd
, tx
, lr
);
2037 ztest_range_unlock(rl
);
2038 ztest_object_unlock(zd
, lr
->lr_foid
);
2044 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2046 ztest_ds_t
*zd
= arg1
;
2047 lr_setattr_t
*lr
= arg2
;
2048 objset_t
*os
= zd
->zd_os
;
2051 ztest_block_tag_t
*bbt
;
2052 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2055 byteswap_uint64_array(lr
, sizeof (*lr
));
2057 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2059 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2061 tx
= dmu_tx_create(os
);
2062 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2064 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2066 dmu_buf_rele(db
, FTAG
);
2067 ztest_object_unlock(zd
, lr
->lr_foid
);
2071 bbt
= ztest_bt_bonus(db
);
2072 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2073 crtxg
= bbt
->bt_crtxg
;
2074 lrtxg
= lr
->lr_common
.lrc_txg
;
2075 dnodesize
= bbt
->bt_dnodesize
;
2077 if (zd
->zd_zilog
->zl_replay
) {
2078 ASSERT(lr
->lr_size
!= 0);
2079 ASSERT(lr
->lr_mode
!= 0);
2083 * Randomly change the size and increment the generation.
2085 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2087 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2092 * Verify that the current bonus buffer is not newer than our txg.
2094 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2095 MAX(txg
, lrtxg
), crtxg
);
2097 dmu_buf_will_dirty(db
, tx
);
2099 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2100 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2101 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2102 bbt
= ztest_bt_bonus(db
);
2104 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2106 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2107 dmu_buf_rele(db
, FTAG
);
2109 (void) ztest_log_setattr(zd
, tx
, lr
);
2113 ztest_object_unlock(zd
, lr
->lr_foid
);
2118 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2119 NULL
, /* 0 no such transaction type */
2120 ztest_replay_create
, /* TX_CREATE */
2121 NULL
, /* TX_MKDIR */
2122 NULL
, /* TX_MKXATTR */
2123 NULL
, /* TX_SYMLINK */
2124 ztest_replay_remove
, /* TX_REMOVE */
2125 NULL
, /* TX_RMDIR */
2127 NULL
, /* TX_RENAME */
2128 ztest_replay_write
, /* TX_WRITE */
2129 ztest_replay_truncate
, /* TX_TRUNCATE */
2130 ztest_replay_setattr
, /* TX_SETATTR */
2132 NULL
, /* TX_CREATE_ACL */
2133 NULL
, /* TX_CREATE_ATTR */
2134 NULL
, /* TX_CREATE_ACL_ATTR */
2135 NULL
, /* TX_MKDIR_ACL */
2136 NULL
, /* TX_MKDIR_ATTR */
2137 NULL
, /* TX_MKDIR_ACL_ATTR */
2138 NULL
, /* TX_WRITE2 */
2142 * ZIL get_data callbacks
2147 ztest_get_done(zgd_t
*zgd
, int error
)
2149 ztest_ds_t
*zd
= zgd
->zgd_private
;
2150 uint64_t object
= ((rl_t
*)zgd
->zgd_lr
)->rl_object
;
2153 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2155 ztest_range_unlock((rl_t
*)zgd
->zgd_lr
);
2156 ztest_object_unlock(zd
, object
);
2158 umem_free(zgd
, sizeof (*zgd
));
2162 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2165 ztest_ds_t
*zd
= arg
;
2166 objset_t
*os
= zd
->zd_os
;
2167 uint64_t object
= lr
->lr_foid
;
2168 uint64_t offset
= lr
->lr_offset
;
2169 uint64_t size
= lr
->lr_length
;
2170 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2172 dmu_object_info_t doi
;
2177 ASSERT3P(lwb
, !=, NULL
);
2178 ASSERT3P(zio
, !=, NULL
);
2179 ASSERT3U(size
, !=, 0);
2181 ztest_object_lock(zd
, object
, RL_READER
);
2182 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2184 ztest_object_unlock(zd
, object
);
2188 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2190 if (crtxg
== 0 || crtxg
> txg
) {
2191 dmu_buf_rele(db
, FTAG
);
2192 ztest_object_unlock(zd
, object
);
2196 dmu_object_info_from_db(db
, &doi
);
2197 dmu_buf_rele(db
, FTAG
);
2200 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2202 zgd
->zgd_private
= zd
;
2204 if (buf
!= NULL
) { /* immediate write */
2205 zgd
->zgd_lr
= (struct zfs_locked_range
*)ztest_range_lock(zd
,
2206 object
, offset
, size
, RL_READER
);
2208 error
= dmu_read(os
, object
, offset
, size
, buf
,
2209 DMU_READ_NO_PREFETCH
);
2212 size
= doi
.doi_data_block_size
;
2214 offset
= P2ALIGN(offset
, size
);
2216 ASSERT(offset
< size
);
2220 zgd
->zgd_lr
= (struct zfs_locked_range
*)ztest_range_lock(zd
,
2221 object
, offset
, size
, RL_READER
);
2223 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2224 DMU_READ_NO_PREFETCH
);
2227 blkptr_t
*bp
= &lr
->lr_blkptr
;
2232 ASSERT(db
->db_offset
== offset
);
2233 ASSERT(db
->db_size
== size
);
2235 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2236 ztest_get_done
, zgd
);
2243 ztest_get_done(zgd
, error
);
2249 ztest_lr_alloc(size_t lrsize
, char *name
)
2252 size_t namesize
= name
? strlen(name
) + 1 : 0;
2254 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2257 bcopy(name
, lr
+ lrsize
, namesize
);
2263 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2265 size_t namesize
= name
? strlen(name
) + 1 : 0;
2267 umem_free(lr
, lrsize
+ namesize
);
2271 * Lookup a bunch of objects. Returns the number of objects not found.
2274 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2280 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2282 for (i
= 0; i
< count
; i
++, od
++) {
2284 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2285 sizeof (uint64_t), 1, &od
->od_object
);
2287 ASSERT(error
== ENOENT
);
2288 ASSERT(od
->od_object
== 0);
2292 ztest_block_tag_t
*bbt
;
2293 dmu_object_info_t doi
;
2295 ASSERT(od
->od_object
!= 0);
2296 ASSERT(missing
== 0); /* there should be no gaps */
2298 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2299 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2300 od
->od_object
, FTAG
, &db
));
2301 dmu_object_info_from_db(db
, &doi
);
2302 bbt
= ztest_bt_bonus(db
);
2303 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2304 od
->od_type
= doi
.doi_type
;
2305 od
->od_blocksize
= doi
.doi_data_block_size
;
2306 od
->od_gen
= bbt
->bt_gen
;
2307 dmu_buf_rele(db
, FTAG
);
2308 ztest_object_unlock(zd
, od
->od_object
);
2316 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2321 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2323 for (i
= 0; i
< count
; i
++, od
++) {
2330 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2332 lr
->lr_doid
= od
->od_dir
;
2333 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2334 lr
->lrz_type
= od
->od_crtype
;
2335 lr
->lrz_blocksize
= od
->od_crblocksize
;
2336 lr
->lrz_ibshift
= ztest_random_ibshift();
2337 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2338 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2339 lr
->lr_gen
= od
->od_crgen
;
2340 lr
->lr_crtime
[0] = time(NULL
);
2342 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2343 ASSERT(missing
== 0);
2347 od
->od_object
= lr
->lr_foid
;
2348 od
->od_type
= od
->od_crtype
;
2349 od
->od_blocksize
= od
->od_crblocksize
;
2350 od
->od_gen
= od
->od_crgen
;
2351 ASSERT(od
->od_object
!= 0);
2354 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2361 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2367 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2371 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2378 * No object was found.
2380 if (od
->od_object
== 0)
2383 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2385 lr
->lr_doid
= od
->od_dir
;
2387 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2388 ASSERT3U(error
, ==, ENOSPC
);
2393 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2400 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2406 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2408 lr
->lr_foid
= object
;
2409 lr
->lr_offset
= offset
;
2410 lr
->lr_length
= size
;
2412 BP_ZERO(&lr
->lr_blkptr
);
2414 bcopy(data
, lr
+ 1, size
);
2416 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2418 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2424 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2429 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2431 lr
->lr_foid
= object
;
2432 lr
->lr_offset
= offset
;
2433 lr
->lr_length
= size
;
2435 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2437 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2443 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2448 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2450 lr
->lr_foid
= object
;
2454 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2456 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2462 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2464 objset_t
*os
= zd
->zd_os
;
2469 txg_wait_synced(dmu_objset_pool(os
), 0);
2471 ztest_object_lock(zd
, object
, RL_READER
);
2472 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2474 tx
= dmu_tx_create(os
);
2476 dmu_tx_hold_write(tx
, object
, offset
, size
);
2478 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2481 dmu_prealloc(os
, object
, offset
, size
, tx
);
2483 txg_wait_synced(dmu_objset_pool(os
), txg
);
2485 (void) dmu_free_long_range(os
, object
, offset
, size
);
2488 ztest_range_unlock(rl
);
2489 ztest_object_unlock(zd
, object
);
2493 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2496 ztest_block_tag_t wbt
;
2497 dmu_object_info_t doi
;
2498 enum ztest_io_type io_type
;
2502 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2503 blocksize
= doi
.doi_data_block_size
;
2504 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2507 * Pick an i/o type at random, biased toward writing block tags.
2509 io_type
= ztest_random(ZTEST_IO_TYPES
);
2510 if (ztest_random(2) == 0)
2511 io_type
= ZTEST_IO_WRITE_TAG
;
2513 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2517 case ZTEST_IO_WRITE_TAG
:
2518 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2520 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2523 case ZTEST_IO_WRITE_PATTERN
:
2524 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2525 if (ztest_random(2) == 0) {
2527 * Induce fletcher2 collisions to ensure that
2528 * zio_ddt_collision() detects and resolves them
2529 * when using fletcher2-verify for deduplication.
2531 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2532 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2534 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2537 case ZTEST_IO_WRITE_ZEROES
:
2538 bzero(data
, blocksize
);
2539 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2542 case ZTEST_IO_TRUNCATE
:
2543 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2546 case ZTEST_IO_SETATTR
:
2547 (void) ztest_setattr(zd
, object
);
2552 case ZTEST_IO_REWRITE
:
2553 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2554 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2555 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2557 VERIFY(err
== 0 || err
== ENOSPC
);
2558 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2559 ZFS_PROP_COMPRESSION
,
2560 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2562 VERIFY(err
== 0 || err
== ENOSPC
);
2563 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2565 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2566 DMU_READ_NO_PREFETCH
));
2568 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2572 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2574 umem_free(data
, blocksize
);
2578 * Initialize an object description template.
2581 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2582 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2585 od
->od_dir
= ZTEST_DIROBJ
;
2588 od
->od_crtype
= type
;
2589 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2590 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2593 od
->od_type
= DMU_OT_NONE
;
2594 od
->od_blocksize
= 0;
2597 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2598 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2602 * Lookup or create the objects for a test using the od template.
2603 * If the objects do not all exist, or if 'remove' is specified,
2604 * remove any existing objects and create new ones. Otherwise,
2605 * use the existing objects.
2608 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2610 int count
= size
/ sizeof (*od
);
2613 mutex_enter(&zd
->zd_dirobj_lock
);
2614 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2615 (ztest_remove(zd
, od
, count
) != 0 ||
2616 ztest_create(zd
, od
, count
) != 0))
2619 mutex_exit(&zd
->zd_dirobj_lock
);
2626 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2628 zilog_t
*zilog
= zd
->zd_zilog
;
2630 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2632 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2635 * Remember the committed values in zd, which is in parent/child
2636 * shared memory. If we die, the next iteration of ztest_run()
2637 * will verify that the log really does contain this record.
2639 mutex_enter(&zilog
->zl_lock
);
2640 ASSERT(zd
->zd_shared
!= NULL
);
2641 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2642 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2643 mutex_exit(&zilog
->zl_lock
);
2645 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2649 * This function is designed to simulate the operations that occur during a
2650 * mount/unmount operation. We hold the dataset across these operations in an
2651 * attempt to expose any implicit assumptions about ZIL management.
2655 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2657 objset_t
*os
= zd
->zd_os
;
2660 * We hold the ztest_vdev_lock so we don't cause problems with
2661 * other threads that wish to remove a log device, such as
2662 * ztest_device_removal().
2664 mutex_enter(&ztest_vdev_lock
);
2667 * We grab the zd_dirobj_lock to ensure that no other thread is
2668 * updating the zil (i.e. adding in-memory log records) and the
2669 * zd_zilog_lock to block any I/O.
2671 mutex_enter(&zd
->zd_dirobj_lock
);
2672 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2674 /* zfsvfs_teardown() */
2675 zil_close(zd
->zd_zilog
);
2677 /* zfsvfs_setup() */
2678 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2679 zil_replay(os
, zd
, ztest_replay_vector
);
2681 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2682 mutex_exit(&zd
->zd_dirobj_lock
);
2683 mutex_exit(&ztest_vdev_lock
);
2687 * Verify that we can't destroy an active pool, create an existing pool,
2688 * or create a pool with a bad vdev spec.
2692 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2694 ztest_shared_opts_t
*zo
= &ztest_opts
;
2698 if (zo
->zo_mmp_test
)
2702 * Attempt to create using a bad file.
2704 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2705 VERIFY3U(ENOENT
, ==,
2706 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2707 nvlist_free(nvroot
);
2710 * Attempt to create using a bad mirror.
2712 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2713 VERIFY3U(ENOENT
, ==,
2714 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2715 nvlist_free(nvroot
);
2718 * Attempt to create an existing pool. It shouldn't matter
2719 * what's in the nvroot; we should fail with EEXIST.
2721 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2722 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2723 VERIFY3U(EEXIST
, ==,
2724 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2725 nvlist_free(nvroot
);
2728 * We open a reference to the spa and then we try to export it
2729 * expecting one of the following errors:
2732 * Because of the reference we just opened.
2734 * ZFS_ERR_EXPORT_IN_PROGRESS
2735 * For the case that there is another ztest thread doing
2736 * an export concurrently.
2738 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2739 int error
= spa_destroy(zo
->zo_pool
);
2740 if (error
!= EBUSY
&& error
!= ZFS_ERR_EXPORT_IN_PROGRESS
) {
2741 fatal(0, "spa_destroy(%s) returned unexpected value %d",
2742 spa
->spa_name
, error
);
2744 spa_close(spa
, FTAG
);
2746 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2750 * Start and then stop the MMP threads to ensure the startup and shutdown code
2751 * works properly. Actual protection and property-related code tested via ZTS.
2755 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2757 ztest_shared_opts_t
*zo
= &ztest_opts
;
2758 spa_t
*spa
= ztest_spa
;
2760 if (zo
->zo_mmp_test
)
2764 * Since enabling MMP involves setting a property, it could not be done
2765 * while the pool is suspended.
2767 if (spa_suspended(spa
))
2770 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2771 mutex_enter(&spa
->spa_props_lock
);
2773 zfs_multihost_fail_intervals
= 0;
2775 if (!spa_multihost(spa
)) {
2776 spa
->spa_multihost
= B_TRUE
;
2777 mmp_thread_start(spa
);
2780 mutex_exit(&spa
->spa_props_lock
);
2781 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2783 txg_wait_synced(spa_get_dsl(spa
), 0);
2784 mmp_signal_all_threads();
2785 txg_wait_synced(spa_get_dsl(spa
), 0);
2787 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2788 mutex_enter(&spa
->spa_props_lock
);
2790 if (spa_multihost(spa
)) {
2791 mmp_thread_stop(spa
);
2792 spa
->spa_multihost
= B_FALSE
;
2795 mutex_exit(&spa
->spa_props_lock
);
2796 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2801 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2804 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2805 uint64_t version
, newversion
;
2806 nvlist_t
*nvroot
, *props
;
2809 if (ztest_opts
.zo_mmp_test
)
2812 mutex_enter(&ztest_vdev_lock
);
2813 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2816 * Clean up from previous runs.
2818 (void) spa_destroy(name
);
2820 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2821 NULL
, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2824 * If we're configuring a RAIDZ device then make sure that the
2825 * initial version is capable of supporting that feature.
2827 switch (ztest_opts
.zo_raidz_parity
) {
2830 initial_version
= SPA_VERSION_INITIAL
;
2833 initial_version
= SPA_VERSION_RAIDZ2
;
2836 initial_version
= SPA_VERSION_RAIDZ3
;
2841 * Create a pool with a spa version that can be upgraded. Pick
2842 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2845 version
= ztest_random_spa_version(initial_version
);
2846 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2848 props
= fnvlist_alloc();
2849 fnvlist_add_uint64(props
,
2850 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2851 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2852 fnvlist_free(nvroot
);
2853 fnvlist_free(props
);
2855 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2856 VERIFY3U(spa_version(spa
), ==, version
);
2857 newversion
= ztest_random_spa_version(version
+ 1);
2859 if (ztest_opts
.zo_verbose
>= 4) {
2860 (void) printf("upgrading spa version from %llu to %llu\n",
2861 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2864 spa_upgrade(spa
, newversion
);
2865 VERIFY3U(spa_version(spa
), >, version
);
2866 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2867 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2868 spa_close(spa
, FTAG
);
2871 mutex_exit(&ztest_vdev_lock
);
2875 ztest_spa_checkpoint(spa_t
*spa
)
2877 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2879 int error
= spa_checkpoint(spa
->spa_name
);
2883 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2884 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2885 case ZFS_ERR_CHECKPOINT_EXISTS
:
2888 ztest_record_enospc(FTAG
);
2891 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
2896 ztest_spa_discard_checkpoint(spa_t
*spa
)
2898 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2900 int error
= spa_checkpoint_discard(spa
->spa_name
);
2904 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2905 case ZFS_ERR_NO_CHECKPOINT
:
2908 fatal(0, "spa_discard_checkpoint(%s) = %d",
2909 spa
->spa_name
, error
);
2916 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
2918 spa_t
*spa
= ztest_spa
;
2920 mutex_enter(&ztest_checkpoint_lock
);
2921 if (ztest_random(2) == 0) {
2922 ztest_spa_checkpoint(spa
);
2924 ztest_spa_discard_checkpoint(spa
);
2926 mutex_exit(&ztest_checkpoint_lock
);
2931 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2936 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2939 for (c
= 0; c
< vd
->vdev_children
; c
++)
2940 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2948 spa_num_top_vdevs(spa_t
*spa
)
2950 vdev_t
*rvd
= spa
->spa_root_vdev
;
2951 ASSERT3U(spa_config_held(spa
, SCL_VDEV
, RW_READER
), ==, SCL_VDEV
);
2952 return (rvd
->vdev_children
);
2956 * Verify that vdev_add() works as expected.
2960 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2962 ztest_shared_t
*zs
= ztest_shared
;
2963 spa_t
*spa
= ztest_spa
;
2969 if (ztest_opts
.zo_mmp_test
)
2972 mutex_enter(&ztest_vdev_lock
);
2973 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2975 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2977 ztest_shared
->zs_vdev_next_leaf
= spa_num_top_vdevs(spa
) * leaves
;
2980 * If we have slogs then remove them 1/4 of the time.
2982 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2983 metaslab_group_t
*mg
;
2986 * find the first real slog in log allocation class
2988 mg
= spa_log_class(spa
)->mc_rotor
;
2989 while (!mg
->mg_vd
->vdev_islog
)
2992 guid
= mg
->mg_vd
->vdev_guid
;
2994 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2997 * We have to grab the zs_name_lock as writer to
2998 * prevent a race between removing a slog (dmu_objset_find)
2999 * and destroying a dataset. Removing the slog will
3000 * grab a reference on the dataset which may cause
3001 * dsl_destroy_head() to fail with EBUSY thus
3002 * leaving the dataset in an inconsistent state.
3004 pthread_rwlock_wrlock(&ztest_name_lock
);
3005 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3006 pthread_rwlock_unlock(&ztest_name_lock
);
3010 case EEXIST
: /* Generic zil_reset() error */
3011 case EBUSY
: /* Replay required */
3012 case EACCES
: /* Crypto key not loaded */
3013 case ZFS_ERR_CHECKPOINT_EXISTS
:
3014 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3017 fatal(0, "spa_vdev_remove() = %d", error
);
3020 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3023 * Make 1/4 of the devices be log devices
3025 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3026 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3027 "log" : NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3029 error
= spa_vdev_add(spa
, nvroot
);
3030 nvlist_free(nvroot
);
3036 ztest_record_enospc("spa_vdev_add");
3039 fatal(0, "spa_vdev_add() = %d", error
);
3043 mutex_exit(&ztest_vdev_lock
);
3048 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3050 ztest_shared_t
*zs
= ztest_shared
;
3051 spa_t
*spa
= ztest_spa
;
3054 const char *class = (ztest_random(2) == 0) ?
3055 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3059 * By default add a special vdev 50% of the time
3061 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3062 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3063 ztest_random(2) == 0)) {
3067 mutex_enter(&ztest_vdev_lock
);
3069 /* Only test with mirrors */
3070 if (zs
->zs_mirrors
< 2) {
3071 mutex_exit(&ztest_vdev_lock
);
3075 /* requires feature@allocation_classes */
3076 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3077 mutex_exit(&ztest_vdev_lock
);
3081 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3083 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3084 ztest_shared
->zs_vdev_next_leaf
= spa_num_top_vdevs(spa
) * leaves
;
3085 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3087 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3088 class, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3090 error
= spa_vdev_add(spa
, nvroot
);
3091 nvlist_free(nvroot
);
3093 if (error
== ENOSPC
)
3094 ztest_record_enospc("spa_vdev_add");
3095 else if (error
!= 0)
3096 fatal(0, "spa_vdev_add() = %d", error
);
3099 * 50% of the time allow small blocks in the special class
3102 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3103 if (ztest_opts
.zo_verbose
>= 3)
3104 (void) printf("Enabling special VDEV small blocks\n");
3105 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3106 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3109 mutex_exit(&ztest_vdev_lock
);
3111 if (ztest_opts
.zo_verbose
>= 3) {
3112 metaslab_class_t
*mc
;
3114 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3115 mc
= spa_special_class(spa
);
3117 mc
= spa_dedup_class(spa
);
3118 (void) printf("Added a %s mirrored vdev (of %d)\n",
3119 class, (int)mc
->mc_groups
);
3124 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3128 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3130 ztest_shared_t
*zs
= ztest_shared
;
3131 spa_t
*spa
= ztest_spa
;
3132 vdev_t
*rvd
= spa
->spa_root_vdev
;
3133 spa_aux_vdev_t
*sav
;
3139 if (ztest_opts
.zo_mmp_test
)
3142 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3144 if (ztest_random(2) == 0) {
3145 sav
= &spa
->spa_spares
;
3146 aux
= ZPOOL_CONFIG_SPARES
;
3148 sav
= &spa
->spa_l2cache
;
3149 aux
= ZPOOL_CONFIG_L2CACHE
;
3152 mutex_enter(&ztest_vdev_lock
);
3154 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3156 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3158 * Pick a random device to remove.
3160 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3163 * Find an unused device we can add.
3165 zs
->zs_vdev_aux
= 0;
3168 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3169 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3171 for (c
= 0; c
< sav
->sav_count
; c
++)
3172 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3175 if (c
== sav
->sav_count
&&
3176 vdev_lookup_by_path(rvd
, path
) == NULL
)
3182 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3188 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3189 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3190 error
= spa_vdev_add(spa
, nvroot
);
3196 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3198 nvlist_free(nvroot
);
3201 * Remove an existing device. Sometimes, dirty its
3202 * vdev state first to make sure we handle removal
3203 * of devices that have pending state changes.
3205 if (ztest_random(2) == 0)
3206 (void) vdev_online(spa
, guid
, 0, NULL
);
3208 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3213 case ZFS_ERR_CHECKPOINT_EXISTS
:
3214 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3217 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3221 mutex_exit(&ztest_vdev_lock
);
3223 umem_free(path
, MAXPATHLEN
);
3227 * split a pool if it has mirror tlvdevs
3231 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3233 ztest_shared_t
*zs
= ztest_shared
;
3234 spa_t
*spa
= ztest_spa
;
3235 vdev_t
*rvd
= spa
->spa_root_vdev
;
3236 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3237 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3240 if (ztest_opts
.zo_mmp_test
)
3243 mutex_enter(&ztest_vdev_lock
);
3245 /* ensure we have a usable config; mirrors of raidz aren't supported */
3246 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3247 mutex_exit(&ztest_vdev_lock
);
3251 /* clean up the old pool, if any */
3252 (void) spa_destroy("splitp");
3254 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3256 /* generate a config from the existing config */
3257 mutex_enter(&spa
->spa_props_lock
);
3258 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3260 mutex_exit(&spa
->spa_props_lock
);
3262 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3265 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3266 for (c
= 0; c
< children
; c
++) {
3267 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3271 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3272 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3274 VERIFY(nvlist_add_string(schild
[schildren
],
3275 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3276 VERIFY(nvlist_add_uint64(schild
[schildren
],
3277 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3279 lastlogid
= schildren
;
3284 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3285 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3286 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3289 /* OK, create a config that can be used to split */
3290 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3291 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3292 VDEV_TYPE_ROOT
) == 0);
3293 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3294 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3296 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3297 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3299 for (c
= 0; c
< schildren
; c
++)
3300 nvlist_free(schild
[c
]);
3304 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3306 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3307 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3308 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3310 nvlist_free(config
);
3313 (void) printf("successful split - results:\n");
3314 mutex_enter(&spa_namespace_lock
);
3315 show_pool_stats(spa
);
3316 show_pool_stats(spa_lookup("splitp"));
3317 mutex_exit(&spa_namespace_lock
);
3321 mutex_exit(&ztest_vdev_lock
);
3325 * Verify that we can attach and detach devices.
3329 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3331 ztest_shared_t
*zs
= ztest_shared
;
3332 spa_t
*spa
= ztest_spa
;
3333 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3334 vdev_t
*rvd
= spa
->spa_root_vdev
;
3335 vdev_t
*oldvd
, *newvd
, *pvd
;
3339 uint64_t ashift
= ztest_get_ashift();
3340 uint64_t oldguid
, pguid
;
3341 uint64_t oldsize
, newsize
;
3342 char *oldpath
, *newpath
;
3344 int oldvd_has_siblings
= B_FALSE
;
3345 int newvd_is_spare
= B_FALSE
;
3347 int error
, expected_error
;
3349 if (ztest_opts
.zo_mmp_test
)
3352 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3353 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3355 mutex_enter(&ztest_vdev_lock
);
3356 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3358 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3361 * If a vdev is in the process of being removed, its removal may
3362 * finish while we are in progress, leading to an unexpected error
3363 * value. Don't bother trying to attach while we are in the middle
3366 if (ztest_device_removal_active
) {
3367 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3368 mutex_exit(&ztest_vdev_lock
);
3373 * Decide whether to do an attach or a replace.
3375 replacing
= ztest_random(2);
3378 * Pick a random top-level vdev.
3380 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3383 * Pick a random leaf within it.
3385 leaf
= ztest_random(leaves
);
3390 oldvd
= rvd
->vdev_child
[top
];
3392 /* pick a child from the mirror */
3393 if (zs
->zs_mirrors
>= 1) {
3394 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3395 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3396 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3399 /* pick a child out of the raidz group */
3400 if (ztest_opts
.zo_raidz
> 1) {
3401 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3402 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3403 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3407 * If we're already doing an attach or replace, oldvd may be a
3408 * mirror vdev -- in which case, pick a random child.
3410 while (oldvd
->vdev_children
!= 0) {
3411 oldvd_has_siblings
= B_TRUE
;
3412 ASSERT(oldvd
->vdev_children
>= 2);
3413 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3416 oldguid
= oldvd
->vdev_guid
;
3417 oldsize
= vdev_get_min_asize(oldvd
);
3418 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3419 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3420 pvd
= oldvd
->vdev_parent
;
3421 pguid
= pvd
->vdev_guid
;
3424 * If oldvd has siblings, then half of the time, detach it. Prior
3425 * to the detach the pool is scrubbed in order to prevent creating
3426 * unrepairable blocks as a result of the data corruption injection.
3428 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3429 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3431 error
= ztest_scrub_impl(spa
);
3435 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3436 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3437 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3438 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3439 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3444 * For the new vdev, choose with equal probability between the two
3445 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3447 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3448 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3449 newvd_is_spare
= B_TRUE
;
3450 (void) strcpy(newpath
, newvd
->vdev_path
);
3452 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3453 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3454 top
* leaves
+ leaf
);
3455 if (ztest_random(2) == 0)
3456 newpath
[strlen(newpath
) - 1] = 'b';
3457 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3462 * Reopen to ensure the vdev's asize field isn't stale.
3465 newsize
= vdev_get_min_asize(newvd
);
3468 * Make newsize a little bigger or smaller than oldsize.
3469 * If it's smaller, the attach should fail.
3470 * If it's larger, and we're doing a replace,
3471 * we should get dynamic LUN growth when we're done.
3473 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3477 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3478 * unless it's a replace; in that case any non-replacing parent is OK.
3480 * If newvd is already part of the pool, it should fail with EBUSY.
3482 * If newvd is too small, it should fail with EOVERFLOW.
3484 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3485 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3486 pvd
->vdev_ops
== &vdev_replacing_ops
||
3487 pvd
->vdev_ops
== &vdev_spare_ops
))
3488 expected_error
= ENOTSUP
;
3489 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3490 expected_error
= ENOTSUP
;
3491 else if (newvd
== oldvd
)
3492 expected_error
= replacing
? 0 : EBUSY
;
3493 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3494 expected_error
= EBUSY
;
3495 else if (newsize
< oldsize
)
3496 expected_error
= EOVERFLOW
;
3497 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3498 expected_error
= EDOM
;
3502 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3505 * Build the nvlist describing newpath.
3507 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3508 ashift
, NULL
, 0, 0, 1);
3511 * When supported select either a healing or sequential resilver.
3513 boolean_t rebuilding
= B_FALSE
;
3514 if (pvd
->vdev_ops
== &vdev_mirror_ops
||
3515 pvd
->vdev_ops
== &vdev_root_ops
) {
3516 rebuilding
= !!ztest_random(2);
3519 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
, rebuilding
);
3524 * If our parent was the replacing vdev, but the replace completed,
3525 * then instead of failing with ENOTSUP we may either succeed,
3526 * fail with ENODEV, or fail with EOVERFLOW.
3528 if (expected_error
== ENOTSUP
&&
3529 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3530 expected_error
= error
;
3533 * If someone grew the LUN, the replacement may be too small.
3535 if (error
== EOVERFLOW
|| error
== EBUSY
)
3536 expected_error
= error
;
3538 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3539 error
== ZFS_ERR_DISCARDING_CHECKPOINT
||
3540 error
== ZFS_ERR_RESILVER_IN_PROGRESS
||
3541 error
== ZFS_ERR_REBUILD_IN_PROGRESS
)
3542 expected_error
= error
;
3544 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3545 fatal(0, "attach (%s %llu, %s %llu, %d) "
3546 "returned %d, expected %d",
3547 oldpath
, oldsize
, newpath
,
3548 newsize
, replacing
, error
, expected_error
);
3551 mutex_exit(&ztest_vdev_lock
);
3553 umem_free(oldpath
, MAXPATHLEN
);
3554 umem_free(newpath
, MAXPATHLEN
);
3559 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3561 spa_t
*spa
= ztest_spa
;
3566 mutex_enter(&ztest_vdev_lock
);
3568 if (ztest_device_removal_active
) {
3569 mutex_exit(&ztest_vdev_lock
);
3574 * Remove a random top-level vdev and wait for removal to finish.
3576 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3577 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3578 guid
= vd
->vdev_guid
;
3579 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3581 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3583 ztest_device_removal_active
= B_TRUE
;
3584 mutex_exit(&ztest_vdev_lock
);
3587 * spa->spa_vdev_removal is created in a sync task that
3588 * is initiated via dsl_sync_task_nowait(). Since the
3589 * task may not run before spa_vdev_remove() returns, we
3590 * must wait at least 1 txg to ensure that the removal
3591 * struct has been created.
3593 txg_wait_synced(spa_get_dsl(spa
), 0);
3595 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
3596 txg_wait_synced(spa_get_dsl(spa
), 0);
3598 mutex_exit(&ztest_vdev_lock
);
3603 * The pool needs to be scrubbed after completing device removal.
3604 * Failure to do so may result in checksum errors due to the
3605 * strategy employed by ztest_fault_inject() when selecting which
3606 * offset are redundant and can be damaged.
3608 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3610 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3611 txg_wait_synced(spa_get_dsl(spa
), 0);
3614 mutex_enter(&ztest_vdev_lock
);
3615 ztest_device_removal_active
= B_FALSE
;
3616 mutex_exit(&ztest_vdev_lock
);
3620 * Callback function which expands the physical size of the vdev.
3623 grow_vdev(vdev_t
*vd
, void *arg
)
3625 spa_t
*spa __maybe_unused
= vd
->vdev_spa
;
3626 size_t *newsize
= arg
;
3630 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3631 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3633 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3636 fsize
= lseek(fd
, 0, SEEK_END
);
3637 VERIFY(ftruncate(fd
, *newsize
) == 0);
3639 if (ztest_opts
.zo_verbose
>= 6) {
3640 (void) printf("%s grew from %lu to %lu bytes\n",
3641 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3648 * Callback function which expands a given vdev by calling vdev_online().
3652 online_vdev(vdev_t
*vd
, void *arg
)
3654 spa_t
*spa
= vd
->vdev_spa
;
3655 vdev_t
*tvd
= vd
->vdev_top
;
3656 uint64_t guid
= vd
->vdev_guid
;
3657 uint64_t generation
= spa
->spa_config_generation
+ 1;
3658 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3661 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3662 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3664 /* Calling vdev_online will initialize the new metaslabs */
3665 spa_config_exit(spa
, SCL_STATE
, spa
);
3666 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3667 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3670 * If vdev_online returned an error or the underlying vdev_open
3671 * failed then we abort the expand. The only way to know that
3672 * vdev_open fails is by checking the returned newstate.
3674 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3675 if (ztest_opts
.zo_verbose
>= 5) {
3676 (void) printf("Unable to expand vdev, state %llu, "
3677 "error %d\n", (u_longlong_t
)newstate
, error
);
3681 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3684 * Since we dropped the lock we need to ensure that we're
3685 * still talking to the original vdev. It's possible this
3686 * vdev may have been detached/replaced while we were
3687 * trying to online it.
3689 if (generation
!= spa
->spa_config_generation
) {
3690 if (ztest_opts
.zo_verbose
>= 5) {
3691 (void) printf("vdev configuration has changed, "
3692 "guid %llu, state %llu, expected gen %llu, "
3695 (u_longlong_t
)tvd
->vdev_state
,
3696 (u_longlong_t
)generation
,
3697 (u_longlong_t
)spa
->spa_config_generation
);
3705 * Traverse the vdev tree calling the supplied function.
3706 * We continue to walk the tree until we either have walked all
3707 * children or we receive a non-NULL return from the callback.
3708 * If a NULL callback is passed, then we just return back the first
3709 * leaf vdev we encounter.
3712 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3716 if (vd
->vdev_ops
->vdev_op_leaf
) {
3720 return (func(vd
, arg
));
3723 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3724 vdev_t
*cvd
= vd
->vdev_child
[c
];
3725 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3732 * Verify that dynamic LUN growth works as expected.
3736 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3738 spa_t
*spa
= ztest_spa
;
3740 metaslab_class_t
*mc
;
3741 metaslab_group_t
*mg
;
3742 size_t psize
, newsize
;
3744 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3746 mutex_enter(&ztest_checkpoint_lock
);
3747 mutex_enter(&ztest_vdev_lock
);
3748 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3751 * If there is a vdev removal in progress, it could complete while
3752 * we are running, in which case we would not be able to verify
3753 * that the metaslab_class space increased (because it decreases
3754 * when the device removal completes).
3756 if (ztest_device_removal_active
) {
3757 spa_config_exit(spa
, SCL_STATE
, spa
);
3758 mutex_exit(&ztest_vdev_lock
);
3759 mutex_exit(&ztest_checkpoint_lock
);
3763 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3765 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3768 old_ms_count
= tvd
->vdev_ms_count
;
3769 old_class_space
= metaslab_class_get_space(mc
);
3772 * Determine the size of the first leaf vdev associated with
3773 * our top-level device.
3775 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3776 ASSERT3P(vd
, !=, NULL
);
3777 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3779 psize
= vd
->vdev_psize
;
3782 * We only try to expand the vdev if it's healthy, less than 4x its
3783 * original size, and it has a valid psize.
3785 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3786 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3787 spa_config_exit(spa
, SCL_STATE
, spa
);
3788 mutex_exit(&ztest_vdev_lock
);
3789 mutex_exit(&ztest_checkpoint_lock
);
3793 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3794 ASSERT3U(newsize
, >, psize
);
3796 if (ztest_opts
.zo_verbose
>= 6) {
3797 (void) printf("Expanding LUN %s from %lu to %lu\n",
3798 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3802 * Growing the vdev is a two step process:
3803 * 1). expand the physical size (i.e. relabel)
3804 * 2). online the vdev to create the new metaslabs
3806 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3807 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3808 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3809 if (ztest_opts
.zo_verbose
>= 5) {
3810 (void) printf("Could not expand LUN because "
3811 "the vdev configuration changed.\n");
3813 spa_config_exit(spa
, SCL_STATE
, spa
);
3814 mutex_exit(&ztest_vdev_lock
);
3815 mutex_exit(&ztest_checkpoint_lock
);
3819 spa_config_exit(spa
, SCL_STATE
, spa
);
3822 * Expanding the LUN will update the config asynchronously,
3823 * thus we must wait for the async thread to complete any
3824 * pending tasks before proceeding.
3828 mutex_enter(&spa
->spa_async_lock
);
3829 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3830 mutex_exit(&spa
->spa_async_lock
);
3833 txg_wait_synced(spa_get_dsl(spa
), 0);
3834 (void) poll(NULL
, 0, 100);
3837 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3839 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3840 new_ms_count
= tvd
->vdev_ms_count
;
3841 new_class_space
= metaslab_class_get_space(mc
);
3843 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3844 if (ztest_opts
.zo_verbose
>= 5) {
3845 (void) printf("Could not verify LUN expansion due to "
3846 "intervening vdev offline or remove.\n");
3848 spa_config_exit(spa
, SCL_STATE
, spa
);
3849 mutex_exit(&ztest_vdev_lock
);
3850 mutex_exit(&ztest_checkpoint_lock
);
3855 * Make sure we were able to grow the vdev.
3857 if (new_ms_count
<= old_ms_count
) {
3858 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3859 old_ms_count
, new_ms_count
);
3863 * Make sure we were able to grow the pool.
3865 if (new_class_space
<= old_class_space
) {
3866 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3867 old_class_space
, new_class_space
);
3870 if (ztest_opts
.zo_verbose
>= 5) {
3871 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3873 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3874 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3875 (void) printf("%s grew from %s to %s\n",
3876 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3879 spa_config_exit(spa
, SCL_STATE
, spa
);
3880 mutex_exit(&ztest_vdev_lock
);
3881 mutex_exit(&ztest_checkpoint_lock
);
3885 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3889 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3892 * Create the objects common to all ztest datasets.
3894 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3895 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3899 ztest_dataset_create(char *dsname
)
3903 dsl_crypto_params_t
*dcp
= NULL
;
3906 * 50% of the time, we create encrypted datasets
3907 * using a random cipher suite and a hard-coded
3910 rand
= ztest_random(2);
3912 nvlist_t
*crypto_args
= fnvlist_alloc();
3913 nvlist_t
*props
= fnvlist_alloc();
3915 /* slight bias towards the default cipher suite */
3916 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3917 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3918 rand
= ZIO_CRYPT_ON
;
3920 fnvlist_add_uint64(props
,
3921 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3922 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3923 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3926 * These parameters aren't really used by the kernel. They
3927 * are simply stored so that userspace knows how to load
3930 fnvlist_add_uint64(props
,
3931 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3932 fnvlist_add_string(props
,
3933 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3934 fnvlist_add_uint64(props
,
3935 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3936 fnvlist_add_uint64(props
,
3937 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3939 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3940 crypto_args
, &dcp
));
3943 * Cycle through all available encryption implementations
3944 * to verify interoperability.
3946 VERIFY0(gcm_impl_set("cycle"));
3947 VERIFY0(aes_impl_set("cycle"));
3949 fnvlist_free(crypto_args
);
3950 fnvlist_free(props
);
3953 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
3954 ztest_objset_create_cb
, NULL
);
3955 dsl_crypto_params_free(dcp
, !!err
);
3957 rand
= ztest_random(100);
3958 if (err
|| rand
< 80)
3961 if (ztest_opts
.zo_verbose
>= 5)
3962 (void) printf("Setting dataset %s to sync always\n", dsname
);
3963 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3964 ZFS_SYNC_ALWAYS
, B_FALSE
));
3969 ztest_objset_destroy_cb(const char *name
, void *arg
)
3972 dmu_object_info_t doi
;
3976 * Verify that the dataset contains a directory object.
3978 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3979 B_TRUE
, FTAG
, &os
));
3980 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3981 if (error
!= ENOENT
) {
3982 /* We could have crashed in the middle of destroying it */
3984 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3985 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3987 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3990 * Destroy the dataset.
3992 if (strchr(name
, '@') != NULL
) {
3993 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3995 error
= dsl_destroy_head(name
);
3996 if (error
== ENOSPC
) {
3997 /* There could be checkpoint or insufficient slop */
3998 ztest_record_enospc(FTAG
);
3999 } else if (error
!= EBUSY
) {
4000 /* There could be a hold on this dataset */
4008 ztest_snapshot_create(char *osname
, uint64_t id
)
4010 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4013 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4015 error
= dmu_objset_snapshot_one(osname
, snapname
);
4016 if (error
== ENOSPC
) {
4017 ztest_record_enospc(FTAG
);
4020 if (error
!= 0 && error
!= EEXIST
) {
4021 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4028 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4030 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4033 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4036 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4037 if (error
!= 0 && error
!= ENOENT
)
4038 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4044 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4050 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4054 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4056 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4058 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4059 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4062 * If this dataset exists from a previous run, process its replay log
4063 * half of the time. If we don't replay it, then dsl_destroy_head()
4064 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4066 if (ztest_random(2) == 0 &&
4067 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4068 B_TRUE
, FTAG
, &os
) == 0) {
4069 ztest_zd_init(zdtmp
, NULL
, os
);
4070 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4071 ztest_zd_fini(zdtmp
);
4072 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4076 * There may be an old instance of the dataset we're about to
4077 * create lying around from a previous run. If so, destroy it
4078 * and all of its snapshots.
4080 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4081 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4084 * Verify that the destroyed dataset is no longer in the namespace.
4086 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4087 B_TRUE
, FTAG
, &os
));
4090 * Verify that we can create a new dataset.
4092 error
= ztest_dataset_create(name
);
4094 if (error
== ENOSPC
) {
4095 ztest_record_enospc(FTAG
);
4098 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4101 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4104 ztest_zd_init(zdtmp
, NULL
, os
);
4107 * Open the intent log for it.
4109 zilog
= zil_open(os
, ztest_get_data
);
4112 * Put some objects in there, do a little I/O to them,
4113 * and randomly take a couple of snapshots along the way.
4115 iters
= ztest_random(5);
4116 for (i
= 0; i
< iters
; i
++) {
4117 ztest_dmu_object_alloc_free(zdtmp
, id
);
4118 if (ztest_random(iters
) == 0)
4119 (void) ztest_snapshot_create(name
, i
);
4123 * Verify that we cannot create an existing dataset.
4125 VERIFY3U(EEXIST
, ==,
4126 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4129 * Verify that we can hold an objset that is also owned.
4131 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4132 dmu_objset_rele(os2
, FTAG
);
4135 * Verify that we cannot own an objset that is already owned.
4137 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4138 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4141 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4142 ztest_zd_fini(zdtmp
);
4144 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4146 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4150 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4153 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4155 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4156 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4157 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4158 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4162 * Cleanup non-standard snapshots and clones.
4165 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4174 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4175 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4176 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4177 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4178 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4180 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4181 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4182 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4183 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4184 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4185 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4186 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4187 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4188 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4189 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4191 error
= dsl_destroy_head(clone2name
);
4192 if (error
&& error
!= ENOENT
)
4193 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4194 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4195 if (error
&& error
!= ENOENT
)
4196 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4197 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4198 if (error
&& error
!= ENOENT
)
4199 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4200 error
= dsl_destroy_head(clone1name
);
4201 if (error
&& error
!= ENOENT
)
4202 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4203 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4204 if (error
&& error
!= ENOENT
)
4205 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4207 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4208 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4209 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4210 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4211 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4215 * Verify dsl_dataset_promote handles EBUSY
4218 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4226 char *osname
= zd
->zd_name
;
4229 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4230 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4231 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4232 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4233 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4235 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4237 ztest_dsl_dataset_cleanup(osname
, id
);
4239 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4240 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4241 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4242 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4243 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4244 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4245 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4246 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4247 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4248 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4250 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4251 if (error
&& error
!= EEXIST
) {
4252 if (error
== ENOSPC
) {
4253 ztest_record_enospc(FTAG
);
4256 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4259 error
= dmu_objset_clone(clone1name
, snap1name
);
4261 if (error
== ENOSPC
) {
4262 ztest_record_enospc(FTAG
);
4265 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4268 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4269 if (error
&& error
!= EEXIST
) {
4270 if (error
== ENOSPC
) {
4271 ztest_record_enospc(FTAG
);
4274 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4277 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4278 if (error
&& error
!= EEXIST
) {
4279 if (error
== ENOSPC
) {
4280 ztest_record_enospc(FTAG
);
4283 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4286 error
= dmu_objset_clone(clone2name
, snap3name
);
4288 if (error
== ENOSPC
) {
4289 ztest_record_enospc(FTAG
);
4292 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4295 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4298 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4299 error
= dsl_dataset_promote(clone2name
, NULL
);
4300 if (error
== ENOSPC
) {
4301 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4302 ztest_record_enospc(FTAG
);
4306 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4308 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4311 ztest_dsl_dataset_cleanup(osname
, id
);
4313 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4315 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4316 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4317 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4318 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4319 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4322 #undef OD_ARRAY_SIZE
4323 #define OD_ARRAY_SIZE 4
4326 * Verify that dmu_object_{alloc,free} work as expected.
4329 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4336 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4337 od
= umem_alloc(size
, UMEM_NOFAIL
);
4338 batchsize
= OD_ARRAY_SIZE
;
4340 for (b
= 0; b
< batchsize
; b
++)
4341 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4345 * Destroy the previous batch of objects, create a new batch,
4346 * and do some I/O on the new objects.
4348 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4351 while (ztest_random(4 * batchsize
) != 0)
4352 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4353 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4355 umem_free(od
, size
);
4359 * Rewind the global allocator to verify object allocation backfilling.
4362 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4364 objset_t
*os
= zd
->zd_os
;
4365 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4369 * Rewind the global allocator randomly back to a lower object number
4370 * to force backfilling and reclamation of recently freed dnodes.
4372 mutex_enter(&os
->os_obj_lock
);
4373 object
= ztest_random(os
->os_obj_next_chunk
);
4374 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4375 mutex_exit(&os
->os_obj_lock
);
4378 #undef OD_ARRAY_SIZE
4379 #define OD_ARRAY_SIZE 2
4382 * Verify that dmu_{read,write} work as expected.
4385 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4390 objset_t
*os
= zd
->zd_os
;
4391 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4392 od
= umem_alloc(size
, UMEM_NOFAIL
);
4394 int i
, freeit
, error
;
4396 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4397 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4398 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4399 uint64_t regions
= 997;
4400 uint64_t stride
= 123456789ULL;
4401 uint64_t width
= 40;
4402 int free_percent
= 5;
4405 * This test uses two objects, packobj and bigobj, that are always
4406 * updated together (i.e. in the same tx) so that their contents are
4407 * in sync and can be compared. Their contents relate to each other
4408 * in a simple way: packobj is a dense array of 'bufwad' structures,
4409 * while bigobj is a sparse array of the same bufwads. Specifically,
4410 * for any index n, there are three bufwads that should be identical:
4412 * packobj, at offset n * sizeof (bufwad_t)
4413 * bigobj, at the head of the nth chunk
4414 * bigobj, at the tail of the nth chunk
4416 * The chunk size is arbitrary. It doesn't have to be a power of two,
4417 * and it doesn't have any relation to the object blocksize.
4418 * The only requirement is that it can hold at least two bufwads.
4420 * Normally, we write the bufwad to each of these locations.
4421 * However, free_percent of the time we instead write zeroes to
4422 * packobj and perform a dmu_free_range() on bigobj. By comparing
4423 * bigobj to packobj, we can verify that the DMU is correctly
4424 * tracking which parts of an object are allocated and free,
4425 * and that the contents of the allocated blocks are correct.
4429 * Read the directory info. If it's the first time, set things up.
4431 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4432 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4435 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4436 umem_free(od
, size
);
4440 bigobj
= od
[0].od_object
;
4441 packobj
= od
[1].od_object
;
4442 chunksize
= od
[0].od_gen
;
4443 ASSERT(chunksize
== od
[1].od_gen
);
4446 * Prefetch a random chunk of the big object.
4447 * Our aim here is to get some async reads in flight
4448 * for blocks that we may free below; the DMU should
4449 * handle this race correctly.
4451 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4452 s
= 1 + ztest_random(2 * width
- 1);
4453 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4454 ZIO_PRIORITY_SYNC_READ
);
4457 * Pick a random index and compute the offsets into packobj and bigobj.
4459 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4460 s
= 1 + ztest_random(width
- 1);
4462 packoff
= n
* sizeof (bufwad_t
);
4463 packsize
= s
* sizeof (bufwad_t
);
4465 bigoff
= n
* chunksize
;
4466 bigsize
= s
* chunksize
;
4468 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4469 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4472 * free_percent of the time, free a range of bigobj rather than
4475 freeit
= (ztest_random(100) < free_percent
);
4478 * Read the current contents of our objects.
4480 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4483 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4488 * Get a tx for the mods to both packobj and bigobj.
4490 tx
= dmu_tx_create(os
);
4492 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4495 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4497 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4499 /* This accounts for setting the checksum/compression. */
4500 dmu_tx_hold_bonus(tx
, bigobj
);
4502 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4504 umem_free(packbuf
, packsize
);
4505 umem_free(bigbuf
, bigsize
);
4506 umem_free(od
, size
);
4510 enum zio_checksum cksum
;
4512 cksum
= (enum zio_checksum
)
4513 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4514 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4515 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4517 enum zio_compress comp
;
4519 comp
= (enum zio_compress
)
4520 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4521 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4522 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4525 * For each index from n to n + s, verify that the existing bufwad
4526 * in packobj matches the bufwads at the head and tail of the
4527 * corresponding chunk in bigobj. Then update all three bufwads
4528 * with the new values we want to write out.
4530 for (i
= 0; i
< s
; i
++) {
4532 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4534 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4536 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4538 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4539 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4541 if (pack
->bw_txg
> txg
)
4542 fatal(0, "future leak: got %llx, open txg is %llx",
4545 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4546 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4547 pack
->bw_index
, n
, i
);
4549 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4550 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4552 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4553 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4556 bzero(pack
, sizeof (bufwad_t
));
4558 pack
->bw_index
= n
+ i
;
4560 pack
->bw_data
= 1 + ztest_random(-2ULL);
4567 * We've verified all the old bufwads, and made new ones.
4568 * Now write them out.
4570 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4573 if (ztest_opts
.zo_verbose
>= 7) {
4574 (void) printf("freeing offset %llx size %llx"
4576 (u_longlong_t
)bigoff
,
4577 (u_longlong_t
)bigsize
,
4580 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4582 if (ztest_opts
.zo_verbose
>= 7) {
4583 (void) printf("writing offset %llx size %llx"
4585 (u_longlong_t
)bigoff
,
4586 (u_longlong_t
)bigsize
,
4589 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4595 * Sanity check the stuff we just wrote.
4598 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4599 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4601 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4602 packsize
, packcheck
, DMU_READ_PREFETCH
));
4603 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4604 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4606 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4607 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4609 umem_free(packcheck
, packsize
);
4610 umem_free(bigcheck
, bigsize
);
4613 umem_free(packbuf
, packsize
);
4614 umem_free(bigbuf
, bigsize
);
4615 umem_free(od
, size
);
4619 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4620 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4628 * For each index from n to n + s, verify that the existing bufwad
4629 * in packobj matches the bufwads at the head and tail of the
4630 * corresponding chunk in bigobj. Then update all three bufwads
4631 * with the new values we want to write out.
4633 for (i
= 0; i
< s
; i
++) {
4635 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4637 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4639 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4641 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4642 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4644 if (pack
->bw_txg
> txg
)
4645 fatal(0, "future leak: got %llx, open txg is %llx",
4648 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4649 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4650 pack
->bw_index
, n
, i
);
4652 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4653 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4655 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4656 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4658 pack
->bw_index
= n
+ i
;
4660 pack
->bw_data
= 1 + ztest_random(-2ULL);
4667 #undef OD_ARRAY_SIZE
4668 #define OD_ARRAY_SIZE 2
4671 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4673 objset_t
*os
= zd
->zd_os
;
4680 bufwad_t
*packbuf
, *bigbuf
;
4681 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4682 uint64_t blocksize
= ztest_random_blocksize();
4683 uint64_t chunksize
= blocksize
;
4684 uint64_t regions
= 997;
4685 uint64_t stride
= 123456789ULL;
4687 dmu_buf_t
*bonus_db
;
4688 arc_buf_t
**bigbuf_arcbufs
;
4689 dmu_object_info_t doi
;
4691 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4692 od
= umem_alloc(size
, UMEM_NOFAIL
);
4695 * This test uses two objects, packobj and bigobj, that are always
4696 * updated together (i.e. in the same tx) so that their contents are
4697 * in sync and can be compared. Their contents relate to each other
4698 * in a simple way: packobj is a dense array of 'bufwad' structures,
4699 * while bigobj is a sparse array of the same bufwads. Specifically,
4700 * for any index n, there are three bufwads that should be identical:
4702 * packobj, at offset n * sizeof (bufwad_t)
4703 * bigobj, at the head of the nth chunk
4704 * bigobj, at the tail of the nth chunk
4706 * The chunk size is set equal to bigobj block size so that
4707 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4711 * Read the directory info. If it's the first time, set things up.
4713 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4714 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4718 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4719 umem_free(od
, size
);
4723 bigobj
= od
[0].od_object
;
4724 packobj
= od
[1].od_object
;
4725 blocksize
= od
[0].od_blocksize
;
4726 chunksize
= blocksize
;
4727 ASSERT(chunksize
== od
[1].od_gen
);
4729 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4730 VERIFY(ISP2(doi
.doi_data_block_size
));
4731 VERIFY(chunksize
== doi
.doi_data_block_size
);
4732 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4735 * Pick a random index and compute the offsets into packobj and bigobj.
4737 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4738 s
= 1 + ztest_random(width
- 1);
4740 packoff
= n
* sizeof (bufwad_t
);
4741 packsize
= s
* sizeof (bufwad_t
);
4743 bigoff
= n
* chunksize
;
4744 bigsize
= s
* chunksize
;
4746 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4747 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4749 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4751 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4754 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4755 * Iteration 1 test zcopy to already referenced dbufs.
4756 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4757 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4758 * Iteration 4 test zcopy when dbuf is no longer dirty.
4759 * Iteration 5 test zcopy when it can't be done.
4760 * Iteration 6 one more zcopy write.
4762 for (i
= 0; i
< 7; i
++) {
4767 * In iteration 5 (i == 5) use arcbufs
4768 * that don't match bigobj blksz to test
4769 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4770 * assign an arcbuf to a dbuf.
4772 for (j
= 0; j
< s
; j
++) {
4773 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4775 dmu_request_arcbuf(bonus_db
, chunksize
);
4777 bigbuf_arcbufs
[2 * j
] =
4778 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4779 bigbuf_arcbufs
[2 * j
+ 1] =
4780 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4785 * Get a tx for the mods to both packobj and bigobj.
4787 tx
= dmu_tx_create(os
);
4789 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4790 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4792 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4794 umem_free(packbuf
, packsize
);
4795 umem_free(bigbuf
, bigsize
);
4796 for (j
= 0; j
< s
; j
++) {
4798 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4799 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4802 bigbuf_arcbufs
[2 * j
]);
4804 bigbuf_arcbufs
[2 * j
+ 1]);
4807 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4808 umem_free(od
, size
);
4809 dmu_buf_rele(bonus_db
, FTAG
);
4814 * 50% of the time don't read objects in the 1st iteration to
4815 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4816 * no existing dbufs for the specified offsets.
4818 if (i
!= 0 || ztest_random(2) != 0) {
4819 error
= dmu_read(os
, packobj
, packoff
,
4820 packsize
, packbuf
, DMU_READ_PREFETCH
);
4822 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4823 bigbuf
, DMU_READ_PREFETCH
);
4826 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4830 * We've verified all the old bufwads, and made new ones.
4831 * Now write them out.
4833 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4834 if (ztest_opts
.zo_verbose
>= 7) {
4835 (void) printf("writing offset %llx size %llx"
4837 (u_longlong_t
)bigoff
,
4838 (u_longlong_t
)bigsize
,
4841 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4843 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4844 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4845 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4847 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4848 bigbuf_arcbufs
[2 * j
]->b_data
,
4850 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4852 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4857 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4858 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4860 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4861 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
4862 off
, bigbuf_arcbufs
[j
], tx
));
4864 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
4865 off
, bigbuf_arcbufs
[2 * j
], tx
));
4866 VERIFY0(dmu_assign_arcbuf_by_dbuf(bonus_db
,
4867 off
+ chunksize
/ 2,
4868 bigbuf_arcbufs
[2 * j
+ 1], tx
));
4871 dmu_buf_rele(dbt
, FTAG
);
4877 * Sanity check the stuff we just wrote.
4880 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4881 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4883 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4884 packsize
, packcheck
, DMU_READ_PREFETCH
));
4885 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4886 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4888 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4889 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4891 umem_free(packcheck
, packsize
);
4892 umem_free(bigcheck
, bigsize
);
4895 txg_wait_open(dmu_objset_pool(os
), 0, B_TRUE
);
4896 } else if (i
== 3) {
4897 txg_wait_synced(dmu_objset_pool(os
), 0);
4901 dmu_buf_rele(bonus_db
, FTAG
);
4902 umem_free(packbuf
, packsize
);
4903 umem_free(bigbuf
, bigsize
);
4904 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4905 umem_free(od
, size
);
4910 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4914 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4915 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4916 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4919 * Have multiple threads write to large offsets in an object
4920 * to verify that parallel writes to an object -- even to the
4921 * same blocks within the object -- doesn't cause any trouble.
4923 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4925 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4928 while (ztest_random(10) != 0)
4929 ztest_io(zd
, od
->od_object
, offset
);
4931 umem_free(od
, sizeof (ztest_od_t
));
4935 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4938 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4939 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4940 uint64_t count
= ztest_random(20) + 1;
4941 uint64_t blocksize
= ztest_random_blocksize();
4944 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4946 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4948 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4949 !ztest_random(2)) != 0) {
4950 umem_free(od
, sizeof (ztest_od_t
));
4954 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4955 umem_free(od
, sizeof (ztest_od_t
));
4959 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4961 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4963 while (ztest_random(count
) != 0) {
4964 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4965 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4968 while (ztest_random(4) != 0)
4969 ztest_io(zd
, od
->od_object
, randoff
);
4972 umem_free(data
, blocksize
);
4973 umem_free(od
, sizeof (ztest_od_t
));
4977 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4979 #define ZTEST_ZAP_MIN_INTS 1
4980 #define ZTEST_ZAP_MAX_INTS 4
4981 #define ZTEST_ZAP_MAX_PROPS 1000
4984 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4986 objset_t
*os
= zd
->zd_os
;
4989 uint64_t txg
, last_txg
;
4990 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4991 uint64_t zl_ints
, zl_intsize
, prop
;
4994 char propname
[100], txgname
[100];
4996 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4998 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4999 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5001 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5002 !ztest_random(2)) != 0)
5005 object
= od
->od_object
;
5008 * Generate a known hash collision, and verify that
5009 * we can lookup and remove both entries.
5011 tx
= dmu_tx_create(os
);
5012 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5013 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5016 for (i
= 0; i
< 2; i
++) {
5018 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5021 for (i
= 0; i
< 2; i
++) {
5022 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5023 sizeof (uint64_t), 1, &value
[i
], tx
));
5025 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5026 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5027 ASSERT3U(zl_ints
, ==, 1);
5029 for (i
= 0; i
< 2; i
++) {
5030 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5035 * Generate a bunch of random entries.
5037 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5039 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5040 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5041 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5042 bzero(value
, sizeof (value
));
5046 * If these zap entries already exist, validate their contents.
5048 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5050 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5051 ASSERT3U(zl_ints
, ==, 1);
5053 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5054 zl_ints
, &last_txg
) == 0);
5056 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5059 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5060 ASSERT3U(zl_ints
, ==, ints
);
5062 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5063 zl_ints
, value
) == 0);
5065 for (i
= 0; i
< ints
; i
++) {
5066 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5069 ASSERT3U(error
, ==, ENOENT
);
5073 * Atomically update two entries in our zap object.
5074 * The first is named txg_%llu, and contains the txg
5075 * in which the property was last updated. The second
5076 * is named prop_%llu, and the nth element of its value
5077 * should be txg + object + n.
5079 tx
= dmu_tx_create(os
);
5080 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5081 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5086 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5088 for (i
= 0; i
< ints
; i
++)
5089 value
[i
] = txg
+ object
+ i
;
5091 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5093 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5099 * Remove a random pair of entries.
5101 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5102 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5103 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5105 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5107 if (error
== ENOENT
)
5112 tx
= dmu_tx_create(os
);
5113 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5114 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5117 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5118 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5121 umem_free(od
, sizeof (ztest_od_t
));
5125 * Test case to test the upgrading of a microzap to fatzap.
5128 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5130 objset_t
*os
= zd
->zd_os
;
5132 uint64_t object
, txg
;
5135 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5136 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5138 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5139 !ztest_random(2)) != 0)
5141 object
= od
->od_object
;
5144 * Add entries to this ZAP and make sure it spills over
5145 * and gets upgraded to a fatzap. Also, since we are adding
5146 * 2050 entries we should see ptrtbl growth and leaf-block split.
5148 for (i
= 0; i
< 2050; i
++) {
5149 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5154 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5155 (u_longlong_t
)id
, (u_longlong_t
)value
);
5157 tx
= dmu_tx_create(os
);
5158 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5159 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5162 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5164 ASSERT(error
== 0 || error
== EEXIST
);
5168 umem_free(od
, sizeof (ztest_od_t
));
5173 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5175 objset_t
*os
= zd
->zd_os
;
5177 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5179 int i
, namelen
, error
;
5180 int micro
= ztest_random(2);
5181 char name
[20], string_value
[20];
5184 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5185 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5187 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5188 umem_free(od
, sizeof (ztest_od_t
));
5192 object
= od
->od_object
;
5195 * Generate a random name of the form 'xxx.....' where each
5196 * x is a random printable character and the dots are dots.
5197 * There are 94 such characters, and the name length goes from
5198 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5200 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5202 for (i
= 0; i
< 3; i
++)
5203 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5204 for (; i
< namelen
- 1; i
++)
5208 if ((namelen
& 1) || micro
) {
5209 wsize
= sizeof (txg
);
5215 data
= string_value
;
5219 VERIFY0(zap_count(os
, object
, &count
));
5220 ASSERT(count
!= -1ULL);
5223 * Select an operation: length, lookup, add, update, remove.
5225 i
= ztest_random(5);
5228 tx
= dmu_tx_create(os
);
5229 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5230 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5232 umem_free(od
, sizeof (ztest_od_t
));
5235 bcopy(name
, string_value
, namelen
);
5239 bzero(string_value
, namelen
);
5245 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5247 ASSERT3U(wsize
, ==, zl_wsize
);
5248 ASSERT3U(wc
, ==, zl_wc
);
5250 ASSERT3U(error
, ==, ENOENT
);
5255 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5257 if (data
== string_value
&&
5258 bcmp(name
, data
, namelen
) != 0)
5259 fatal(0, "name '%s' != val '%s' len %d",
5260 name
, data
, namelen
);
5262 ASSERT3U(error
, ==, ENOENT
);
5267 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5268 ASSERT(error
== 0 || error
== EEXIST
);
5272 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5276 error
= zap_remove(os
, object
, name
, tx
);
5277 ASSERT(error
== 0 || error
== ENOENT
);
5284 umem_free(od
, sizeof (ztest_od_t
));
5288 * Commit callback data.
5290 typedef struct ztest_cb_data
{
5291 list_node_t zcd_node
;
5293 int zcd_expected_err
;
5294 boolean_t zcd_added
;
5295 boolean_t zcd_called
;
5299 /* This is the actual commit callback function */
5301 ztest_commit_callback(void *arg
, int error
)
5303 ztest_cb_data_t
*data
= arg
;
5304 uint64_t synced_txg
;
5306 VERIFY(data
!= NULL
);
5307 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5308 VERIFY(!data
->zcd_called
);
5310 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5311 if (data
->zcd_txg
> synced_txg
)
5312 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5313 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5316 data
->zcd_called
= B_TRUE
;
5318 if (error
== ECANCELED
) {
5319 ASSERT0(data
->zcd_txg
);
5320 ASSERT(!data
->zcd_added
);
5323 * The private callback data should be destroyed here, but
5324 * since we are going to check the zcd_called field after
5325 * dmu_tx_abort(), we will destroy it there.
5330 ASSERT(data
->zcd_added
);
5331 ASSERT3U(data
->zcd_txg
, !=, 0);
5333 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5335 /* See if this cb was called more quickly */
5336 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5337 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5339 /* Remove our callback from the list */
5340 list_remove(&zcl
.zcl_callbacks
, data
);
5342 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5344 umem_free(data
, sizeof (ztest_cb_data_t
));
5347 /* Allocate and initialize callback data structure */
5348 static ztest_cb_data_t
*
5349 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5351 ztest_cb_data_t
*cb_data
;
5353 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5355 cb_data
->zcd_txg
= txg
;
5356 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5357 list_link_init(&cb_data
->zcd_node
);
5363 * Commit callback test.
5366 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5368 objset_t
*os
= zd
->zd_os
;
5371 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5372 uint64_t old_txg
, txg
;
5375 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5376 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5378 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5379 umem_free(od
, sizeof (ztest_od_t
));
5383 tx
= dmu_tx_create(os
);
5385 cb_data
[0] = ztest_create_cb_data(os
, 0);
5386 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5388 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5390 /* Every once in a while, abort the transaction on purpose */
5391 if (ztest_random(100) == 0)
5395 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5397 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5399 cb_data
[0]->zcd_txg
= txg
;
5400 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5401 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5405 * It's not a strict requirement to call the registered
5406 * callbacks from inside dmu_tx_abort(), but that's what
5407 * it's supposed to happen in the current implementation
5408 * so we will check for that.
5410 for (i
= 0; i
< 2; i
++) {
5411 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5412 VERIFY(!cb_data
[i
]->zcd_called
);
5417 for (i
= 0; i
< 2; i
++) {
5418 VERIFY(cb_data
[i
]->zcd_called
);
5419 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5422 umem_free(od
, sizeof (ztest_od_t
));
5426 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5427 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5430 * Read existing data to make sure there isn't a future leak.
5432 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5433 &old_txg
, DMU_READ_PREFETCH
));
5436 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5439 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5441 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5444 * Since commit callbacks don't have any ordering requirement and since
5445 * it is theoretically possible for a commit callback to be called
5446 * after an arbitrary amount of time has elapsed since its txg has been
5447 * synced, it is difficult to reliably determine whether a commit
5448 * callback hasn't been called due to high load or due to a flawed
5451 * In practice, we will assume that if after a certain number of txgs a
5452 * commit callback hasn't been called, then most likely there's an
5453 * implementation bug..
5455 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5456 if (tmp_cb
!= NULL
&&
5457 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5458 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5459 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5463 * Let's find the place to insert our callbacks.
5465 * Even though the list is ordered by txg, it is possible for the
5466 * insertion point to not be the end because our txg may already be
5467 * quiescing at this point and other callbacks in the open txg
5468 * (from other objsets) may have sneaked in.
5470 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5471 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5472 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5474 /* Add the 3 callbacks to the list */
5475 for (i
= 0; i
< 3; i
++) {
5477 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5479 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5482 cb_data
[i
]->zcd_added
= B_TRUE
;
5483 VERIFY(!cb_data
[i
]->zcd_called
);
5485 tmp_cb
= cb_data
[i
];
5490 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5494 umem_free(od
, sizeof (ztest_od_t
));
5498 * Visit each object in the dataset. Verify that its properties
5499 * are consistent what was stored in the block tag when it was created,
5500 * and that its unused bonus buffer space has not been overwritten.
5504 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5506 objset_t
*os
= zd
->zd_os
;
5510 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5511 ztest_block_tag_t
*bt
= NULL
;
5512 dmu_object_info_t doi
;
5515 ztest_object_lock(zd
, obj
, RL_READER
);
5516 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5517 ztest_object_unlock(zd
, obj
);
5521 dmu_object_info_from_db(db
, &doi
);
5522 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5523 bt
= ztest_bt_bonus(db
);
5525 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5526 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5527 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5529 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5532 dmu_buf_rele(db
, FTAG
);
5533 ztest_object_unlock(zd
, obj
);
5539 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5541 zfs_prop_t proplist
[] = {
5543 ZFS_PROP_COMPRESSION
,
5549 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5551 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5552 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5553 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5555 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5556 ztest_random_blocksize(), (int)ztest_random(2)));
5558 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5563 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5565 nvlist_t
*props
= NULL
;
5567 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5569 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_AUTOTRIM
, ztest_random(2));
5571 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5573 if (ztest_opts
.zo_verbose
>= 6)
5574 dump_nvlist(props
, 4);
5578 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5582 user_release_one(const char *snapname
, const char *holdname
)
5584 nvlist_t
*snaps
, *holds
;
5587 snaps
= fnvlist_alloc();
5588 holds
= fnvlist_alloc();
5589 fnvlist_add_boolean(holds
, holdname
);
5590 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5591 fnvlist_free(holds
);
5592 error
= dsl_dataset_user_release(snaps
, NULL
);
5593 fnvlist_free(snaps
);
5598 * Test snapshot hold/release and deferred destroy.
5601 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5604 objset_t
*os
= zd
->zd_os
;
5608 char clonename
[100];
5610 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5613 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5615 dmu_objset_name(os
, osname
);
5617 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5619 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5620 (void) snprintf(clonename
, sizeof (clonename
),
5621 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5622 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5625 * Clean up from any previous run.
5627 error
= dsl_destroy_head(clonename
);
5628 if (error
!= ENOENT
)
5630 error
= user_release_one(fullname
, tag
);
5631 if (error
!= ESRCH
&& error
!= ENOENT
)
5633 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5634 if (error
!= ENOENT
)
5638 * Create snapshot, clone it, mark snap for deferred destroy,
5639 * destroy clone, verify snap was also destroyed.
5641 error
= dmu_objset_snapshot_one(osname
, snapname
);
5643 if (error
== ENOSPC
) {
5644 ztest_record_enospc("dmu_objset_snapshot");
5647 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5650 error
= dmu_objset_clone(clonename
, fullname
);
5652 if (error
== ENOSPC
) {
5653 ztest_record_enospc("dmu_objset_clone");
5656 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5659 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5661 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5665 error
= dsl_destroy_head(clonename
);
5667 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5669 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5670 if (error
!= ENOENT
)
5671 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5674 * Create snapshot, add temporary hold, verify that we can't
5675 * destroy a held snapshot, mark for deferred destroy,
5676 * release hold, verify snapshot was destroyed.
5678 error
= dmu_objset_snapshot_one(osname
, snapname
);
5680 if (error
== ENOSPC
) {
5681 ztest_record_enospc("dmu_objset_snapshot");
5684 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5687 holds
= fnvlist_alloc();
5688 fnvlist_add_string(holds
, fullname
, tag
);
5689 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5690 fnvlist_free(holds
);
5692 if (error
== ENOSPC
) {
5693 ztest_record_enospc("dsl_dataset_user_hold");
5696 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5697 fullname
, tag
, error
);
5700 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5701 if (error
!= EBUSY
) {
5702 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5706 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5708 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5712 error
= user_release_one(fullname
, tag
);
5714 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5716 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5719 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5723 * Inject random faults into the on-disk data.
5727 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5729 ztest_shared_t
*zs
= ztest_shared
;
5730 spa_t
*spa
= ztest_spa
;
5734 uint64_t bad
= 0x1990c0ffeedecadeull
;
5739 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5745 boolean_t islog
= B_FALSE
;
5747 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5748 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5750 mutex_enter(&ztest_vdev_lock
);
5753 * Device removal is in progress, fault injection must be disabled
5754 * until it completes and the pool is scrubbed. The fault injection
5755 * strategy for damaging blocks does not take in to account evacuated
5756 * blocks which may have already been damaged.
5758 if (ztest_device_removal_active
) {
5759 mutex_exit(&ztest_vdev_lock
);
5763 maxfaults
= MAXFAULTS(zs
);
5764 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5765 mirror_save
= zs
->zs_mirrors
;
5766 mutex_exit(&ztest_vdev_lock
);
5768 ASSERT(leaves
>= 1);
5771 * While ztest is running the number of leaves will not change. This
5772 * is critical for the fault injection logic as it determines where
5773 * errors can be safely injected such that they are always repairable.
5775 * When restarting ztest a different number of leaves may be requested
5776 * which will shift the regions to be damaged. This is fine as long
5777 * as the pool has been scrubbed prior to using the new mapping.
5778 * Failure to do can result in non-repairable damage being injected.
5780 if (ztest_pool_scrubbed
== B_FALSE
)
5784 * Grab the name lock as reader. There are some operations
5785 * which don't like to have their vdevs changed while
5786 * they are in progress (i.e. spa_change_guid). Those
5787 * operations will have grabbed the name lock as writer.
5789 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5792 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5794 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5796 if (ztest_random(2) == 0) {
5798 * Inject errors on a normal data device or slog device.
5800 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5801 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5804 * Generate paths to the first leaf in this top-level vdev,
5805 * and to the random leaf we selected. We'll induce transient
5806 * write failures and random online/offline activity on leaf 0,
5807 * and we'll write random garbage to the randomly chosen leaf.
5809 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5810 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5811 top
* leaves
+ zs
->zs_splits
);
5812 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5813 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5814 top
* leaves
+ leaf
);
5816 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5817 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5821 * If the top-level vdev needs to be resilvered
5822 * then we only allow faults on the device that is
5825 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5826 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5827 vd0
->vdev_resilver_txg
!= 0)) {
5829 * Make vd0 explicitly claim to be unreadable,
5830 * or unwriteable, or reach behind its back
5831 * and close the underlying fd. We can do this if
5832 * maxfaults == 0 because we'll fail and reexecute,
5833 * and we can do it if maxfaults >= 2 because we'll
5834 * have enough redundancy. If maxfaults == 1, the
5835 * combination of this with injection of random data
5836 * corruption below exceeds the pool's fault tolerance.
5838 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5840 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5841 (long long)vd0
->vdev_id
, (int)maxfaults
);
5843 if (vf
!= NULL
&& ztest_random(3) == 0) {
5844 (void) close(vf
->vf_file
->f_fd
);
5845 vf
->vf_file
->f_fd
= -1;
5846 } else if (ztest_random(2) == 0) {
5847 vd0
->vdev_cant_read
= B_TRUE
;
5849 vd0
->vdev_cant_write
= B_TRUE
;
5851 guid0
= vd0
->vdev_guid
;
5855 * Inject errors on an l2cache device.
5857 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5859 if (sav
->sav_count
== 0) {
5860 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5861 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5864 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5865 guid0
= vd0
->vdev_guid
;
5866 (void) strcpy(path0
, vd0
->vdev_path
);
5867 (void) strcpy(pathrand
, vd0
->vdev_path
);
5871 maxfaults
= INT_MAX
; /* no limit on cache devices */
5874 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5875 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5878 * If we can tolerate two or more faults, or we're dealing
5879 * with a slog, randomly online/offline vd0.
5881 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5882 if (ztest_random(10) < 6) {
5883 int flags
= (ztest_random(2) == 0 ?
5884 ZFS_OFFLINE_TEMPORARY
: 0);
5887 * We have to grab the zs_name_lock as writer to
5888 * prevent a race between offlining a slog and
5889 * destroying a dataset. Offlining the slog will
5890 * grab a reference on the dataset which may cause
5891 * dsl_destroy_head() to fail with EBUSY thus
5892 * leaving the dataset in an inconsistent state.
5895 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
5897 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5900 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5903 * Ideally we would like to be able to randomly
5904 * call vdev_[on|off]line without holding locks
5905 * to force unpredictable failures but the side
5906 * effects of vdev_[on|off]line prevent us from
5907 * doing so. We grab the ztest_vdev_lock here to
5908 * prevent a race between injection testing and
5911 mutex_enter(&ztest_vdev_lock
);
5912 (void) vdev_online(spa
, guid0
, 0, NULL
);
5913 mutex_exit(&ztest_vdev_lock
);
5921 * We have at least single-fault tolerance, so inject data corruption.
5923 fd
= open(pathrand
, O_RDWR
);
5925 if (fd
== -1) /* we hit a gap in the device namespace */
5928 fsize
= lseek(fd
, 0, SEEK_END
);
5930 while (--iters
!= 0) {
5932 * The offset must be chosen carefully to ensure that
5933 * we do not inject a given logical block with errors
5934 * on two different leaf devices, because ZFS can not
5935 * tolerate that (if maxfaults==1).
5937 * To achieve this we divide each leaf device into
5938 * chunks of size (# leaves * SPA_MAXBLOCKSIZE * 4).
5939 * Each chunk is further divided into error-injection
5940 * ranges (can accept errors) and clear ranges (we do
5941 * not inject errors in those). Each error-injection
5942 * range can accept errors only for a single leaf vdev.
5943 * Error-injection ranges are separated by clear ranges.
5945 * For example, with 3 leaves, each chunk looks like:
5946 * 0 to 32M: injection range for leaf 0
5947 * 32M to 64M: clear range - no injection allowed
5948 * 64M to 96M: injection range for leaf 1
5949 * 96M to 128M: clear range - no injection allowed
5950 * 128M to 160M: injection range for leaf 2
5951 * 160M to 192M: clear range - no injection allowed
5953 * Each clear range must be large enough such that a
5954 * single block cannot straddle it. This way a block
5955 * can't be a target in two different injection ranges
5956 * (on different leaf vdevs).
5958 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5959 (leaves
<< bshift
) + (leaf
<< bshift
) +
5960 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5963 * Only allow damage to the labels at one end of the vdev.
5965 * If all labels are damaged, the device will be totally
5966 * inaccessible, which will result in loss of data,
5967 * because we also damage (parts of) the other side of
5970 * Additionally, we will always have both an even and an
5971 * odd label, so that we can handle crashes in the
5972 * middle of vdev_config_sync().
5974 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5978 * The two end labels are stored at the "end" of the disk, but
5979 * the end of the disk (vdev_psize) is aligned to
5980 * sizeof (vdev_label_t).
5982 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5983 if ((leaf
& 1) == 1 &&
5984 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5987 mutex_enter(&ztest_vdev_lock
);
5988 if (mirror_save
!= zs
->zs_mirrors
) {
5989 mutex_exit(&ztest_vdev_lock
);
5994 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5995 fatal(1, "can't inject bad word at 0x%llx in %s",
5998 mutex_exit(&ztest_vdev_lock
);
6000 if (ztest_opts
.zo_verbose
>= 7)
6001 (void) printf("injected bad word into %s,"
6002 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
6007 umem_free(path0
, MAXPATHLEN
);
6008 umem_free(pathrand
, MAXPATHLEN
);
6012 * By design ztest will never inject uncorrectable damage in to the pool.
6013 * Issue a scrub, wait for it to complete, and verify there is never any
6014 * any persistent damage.
6016 * Only after a full scrub has been completed is it safe to start injecting
6017 * data corruption. See the comment in zfs_fault_inject().
6020 ztest_scrub_impl(spa_t
*spa
)
6022 int error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
6026 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
6027 txg_wait_synced(spa_get_dsl(spa
), 0);
6029 if (spa_get_errlog_size(spa
) > 0)
6032 ztest_pool_scrubbed
= B_TRUE
;
6042 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6044 spa_t
*spa
= ztest_spa
;
6048 * Scrub in progress by device removal.
6050 if (ztest_device_removal_active
)
6054 * Start a scrub, wait a moment, then force a restart.
6056 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6057 (void) poll(NULL
, 0, 100);
6059 error
= ztest_scrub_impl(spa
);
6066 * Change the guid for the pool.
6070 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6072 spa_t
*spa
= ztest_spa
;
6073 uint64_t orig
, load
;
6076 if (ztest_opts
.zo_mmp_test
)
6079 orig
= spa_guid(spa
);
6080 load
= spa_load_guid(spa
);
6082 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6083 error
= spa_change_guid(spa
);
6084 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6089 if (ztest_opts
.zo_verbose
>= 4) {
6090 (void) printf("Changed guid old %llu -> %llu\n",
6091 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6094 VERIFY3U(orig
, !=, spa_guid(spa
));
6095 VERIFY3U(load
, ==, spa_load_guid(spa
));
6099 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6101 hrtime_t end
= gethrtime() + NANOSEC
;
6103 while (gethrtime() <= end
) {
6104 int run_count
= 100;
6106 struct abd
*abd_data
, *abd_meta
;
6111 zio_cksum_t zc_ref_byteswap
;
6113 size
= ztest_random_blocksize();
6115 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6116 abd_data
= abd_alloc(size
, B_FALSE
);
6117 abd_meta
= abd_alloc(size
, B_TRUE
);
6119 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6120 *ptr
= ztest_random(UINT_MAX
);
6122 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6123 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6125 VERIFY0(fletcher_4_impl_set("scalar"));
6126 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6127 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6129 VERIFY0(fletcher_4_impl_set("cycle"));
6130 while (run_count
-- > 0) {
6132 zio_cksum_t zc_byteswap
;
6134 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6135 fletcher_4_native(buf
, size
, NULL
, &zc
);
6137 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6138 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6139 sizeof (zc_byteswap
)));
6141 /* Test ABD - data */
6142 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6144 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6146 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6147 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6148 sizeof (zc_byteswap
)));
6150 /* Test ABD - metadata */
6151 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6153 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6155 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6156 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6157 sizeof (zc_byteswap
)));
6161 umem_free(buf
, size
);
6168 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6175 zio_cksum_t zc_ref_bswap
;
6177 hrtime_t end
= gethrtime() + NANOSEC
;
6179 while (gethrtime() <= end
) {
6180 int run_count
= 100;
6182 size
= ztest_random_blocksize();
6183 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6185 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6186 *ptr
= ztest_random(UINT_MAX
);
6188 VERIFY0(fletcher_4_impl_set("scalar"));
6189 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6190 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6192 VERIFY0(fletcher_4_impl_set("cycle"));
6194 while (run_count
-- > 0) {
6196 zio_cksum_t zc_bswap
;
6199 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6200 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6202 while (pos
< size
) {
6203 size_t inc
= 64 * ztest_random(size
/ 67);
6204 /* sometimes add few bytes to test non-simd */
6205 if (ztest_random(100) < 10)
6206 inc
+= P2ALIGN(ztest_random(64),
6209 if (inc
> (size
- pos
))
6212 fletcher_4_incremental_native(buf
+ pos
, inc
,
6214 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6220 VERIFY3U(pos
, ==, size
);
6222 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6223 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6226 * verify if incremental on the whole buffer is
6227 * equivalent to non-incremental version
6229 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6230 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6232 fletcher_4_incremental_native(buf
, size
, &zc
);
6233 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6235 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6236 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6239 umem_free(buf
, size
);
6244 ztest_check_path(char *path
)
6247 /* return true on success */
6248 return (!stat(path
, &s
));
6252 ztest_get_zdb_bin(char *bin
, int len
)
6256 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6257 * let popen to search through PATH.
6259 if ((zdb_path
= getenv("ZDB_PATH"))) {
6260 strlcpy(bin
, zdb_path
, len
); /* In env */
6261 if (!ztest_check_path(bin
)) {
6262 ztest_dump_core
= 0;
6263 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6268 VERIFY(realpath(getexecname(), bin
) != NULL
);
6269 if (strstr(bin
, "/ztest/")) {
6270 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6271 strcat(bin
, "/zdb/zdb");
6272 if (ztest_check_path(bin
))
6279 ztest_random_concrete_vdev_leaf(vdev_t
*vd
)
6284 if (vd
->vdev_children
== 0)
6287 vdev_t
*eligible
[vd
->vdev_children
];
6288 int eligible_idx
= 0, i
;
6289 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6290 vdev_t
*cvd
= vd
->vdev_child
[i
];
6291 if (cvd
->vdev_top
->vdev_removing
)
6293 if (cvd
->vdev_children
> 0 ||
6294 (vdev_is_concrete(cvd
) && !cvd
->vdev_detached
)) {
6295 eligible
[eligible_idx
++] = cvd
;
6298 VERIFY(eligible_idx
> 0);
6300 uint64_t child_no
= ztest_random(eligible_idx
);
6301 return (ztest_random_concrete_vdev_leaf(eligible
[child_no
]));
6306 ztest_initialize(ztest_ds_t
*zd
, uint64_t id
)
6308 spa_t
*spa
= ztest_spa
;
6311 mutex_enter(&ztest_vdev_lock
);
6313 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
6315 /* Random leaf vdev */
6316 vdev_t
*rand_vd
= ztest_random_concrete_vdev_leaf(spa
->spa_root_vdev
);
6317 if (rand_vd
== NULL
) {
6318 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6319 mutex_exit(&ztest_vdev_lock
);
6324 * The random vdev we've selected may change as soon as we
6325 * drop the spa_config_lock. We create local copies of things
6326 * we're interested in.
6328 uint64_t guid
= rand_vd
->vdev_guid
;
6329 char *path
= strdup(rand_vd
->vdev_path
);
6330 boolean_t active
= rand_vd
->vdev_initialize_thread
!= NULL
;
6332 zfs_dbgmsg("vd %px, guid %llu", rand_vd
, guid
);
6333 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6335 uint64_t cmd
= ztest_random(POOL_INITIALIZE_FUNCS
);
6337 nvlist_t
*vdev_guids
= fnvlist_alloc();
6338 nvlist_t
*vdev_errlist
= fnvlist_alloc();
6339 fnvlist_add_uint64(vdev_guids
, path
, guid
);
6340 error
= spa_vdev_initialize(spa
, vdev_guids
, cmd
, vdev_errlist
);
6341 fnvlist_free(vdev_guids
);
6342 fnvlist_free(vdev_errlist
);
6345 case POOL_INITIALIZE_CANCEL
:
6346 if (ztest_opts
.zo_verbose
>= 4) {
6347 (void) printf("Cancel initialize %s", path
);
6349 (void) printf(" failed (no initialize active)");
6350 (void) printf("\n");
6353 case POOL_INITIALIZE_START
:
6354 if (ztest_opts
.zo_verbose
>= 4) {
6355 (void) printf("Start initialize %s", path
);
6356 if (active
&& error
== 0)
6357 (void) printf(" failed (already active)");
6358 else if (error
!= 0)
6359 (void) printf(" failed (error %d)", error
);
6360 (void) printf("\n");
6363 case POOL_INITIALIZE_SUSPEND
:
6364 if (ztest_opts
.zo_verbose
>= 4) {
6365 (void) printf("Suspend initialize %s", path
);
6367 (void) printf(" failed (no initialize active)");
6368 (void) printf("\n");
6373 mutex_exit(&ztest_vdev_lock
);
6378 ztest_trim(ztest_ds_t
*zd
, uint64_t id
)
6380 spa_t
*spa
= ztest_spa
;
6383 mutex_enter(&ztest_vdev_lock
);
6385 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
6387 /* Random leaf vdev */
6388 vdev_t
*rand_vd
= ztest_random_concrete_vdev_leaf(spa
->spa_root_vdev
);
6389 if (rand_vd
== NULL
) {
6390 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6391 mutex_exit(&ztest_vdev_lock
);
6396 * The random vdev we've selected may change as soon as we
6397 * drop the spa_config_lock. We create local copies of things
6398 * we're interested in.
6400 uint64_t guid
= rand_vd
->vdev_guid
;
6401 char *path
= strdup(rand_vd
->vdev_path
);
6402 boolean_t active
= rand_vd
->vdev_trim_thread
!= NULL
;
6404 zfs_dbgmsg("vd %p, guid %llu", rand_vd
, guid
);
6405 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6407 uint64_t cmd
= ztest_random(POOL_TRIM_FUNCS
);
6408 uint64_t rate
= 1 << ztest_random(30);
6409 boolean_t partial
= (ztest_random(5) > 0);
6410 boolean_t secure
= (ztest_random(5) > 0);
6412 nvlist_t
*vdev_guids
= fnvlist_alloc();
6413 nvlist_t
*vdev_errlist
= fnvlist_alloc();
6414 fnvlist_add_uint64(vdev_guids
, path
, guid
);
6415 error
= spa_vdev_trim(spa
, vdev_guids
, cmd
, rate
, partial
,
6416 secure
, vdev_errlist
);
6417 fnvlist_free(vdev_guids
);
6418 fnvlist_free(vdev_errlist
);
6421 case POOL_TRIM_CANCEL
:
6422 if (ztest_opts
.zo_verbose
>= 4) {
6423 (void) printf("Cancel TRIM %s", path
);
6425 (void) printf(" failed (no TRIM active)");
6426 (void) printf("\n");
6429 case POOL_TRIM_START
:
6430 if (ztest_opts
.zo_verbose
>= 4) {
6431 (void) printf("Start TRIM %s", path
);
6432 if (active
&& error
== 0)
6433 (void) printf(" failed (already active)");
6434 else if (error
!= 0)
6435 (void) printf(" failed (error %d)", error
);
6436 (void) printf("\n");
6439 case POOL_TRIM_SUSPEND
:
6440 if (ztest_opts
.zo_verbose
>= 4) {
6441 (void) printf("Suspend TRIM %s", path
);
6443 (void) printf(" failed (no TRIM active)");
6444 (void) printf("\n");
6449 mutex_exit(&ztest_vdev_lock
);
6453 * Verify pool integrity by running zdb.
6456 ztest_run_zdb(char *pool
)
6462 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6465 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6466 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6467 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6469 ztest_get_zdb_bin(bin
, len
);
6472 "%s -bcc%s%s -G -d -Y -e -p %s %s",
6474 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6475 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6479 if (ztest_opts
.zo_verbose
>= 5)
6480 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6482 fp
= popen(zdb
, "r");
6484 while (fgets(zbuf
, 1024, fp
) != NULL
)
6485 if (ztest_opts
.zo_verbose
>= 3)
6486 (void) printf("%s", zbuf
);
6488 status
= pclose(fp
);
6493 ztest_dump_core
= 0;
6494 if (WIFEXITED(status
))
6495 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6497 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6499 umem_free(bin
, len
);
6500 umem_free(zdb
, len
);
6501 umem_free(zbuf
, 1024);
6505 ztest_walk_pool_directory(char *header
)
6509 if (ztest_opts
.zo_verbose
>= 6)
6510 (void) printf("%s\n", header
);
6512 mutex_enter(&spa_namespace_lock
);
6513 while ((spa
= spa_next(spa
)) != NULL
)
6514 if (ztest_opts
.zo_verbose
>= 6)
6515 (void) printf("\t%s\n", spa_name(spa
));
6516 mutex_exit(&spa_namespace_lock
);
6520 ztest_spa_import_export(char *oldname
, char *newname
)
6522 nvlist_t
*config
, *newconfig
;
6527 if (ztest_opts
.zo_verbose
>= 4) {
6528 (void) printf("import/export: old = %s, new = %s\n",
6533 * Clean up from previous runs.
6535 (void) spa_destroy(newname
);
6538 * Get the pool's configuration and guid.
6540 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6543 * Kick off a scrub to tickle scrub/export races.
6545 if (ztest_random(2) == 0)
6546 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6548 pool_guid
= spa_guid(spa
);
6549 spa_close(spa
, FTAG
);
6551 ztest_walk_pool_directory("pools before export");
6556 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6558 ztest_walk_pool_directory("pools after export");
6563 newconfig
= spa_tryimport(config
);
6564 ASSERT(newconfig
!= NULL
);
6565 nvlist_free(newconfig
);
6568 * Import it under the new name.
6570 error
= spa_import(newname
, config
, NULL
, 0);
6572 dump_nvlist(config
, 0);
6573 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6574 oldname
, newname
, error
);
6577 ztest_walk_pool_directory("pools after import");
6580 * Try to import it again -- should fail with EEXIST.
6582 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6585 * Try to import it under a different name -- should fail with EEXIST.
6587 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6590 * Verify that the pool is no longer visible under the old name.
6592 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6595 * Verify that we can open and close the pool using the new name.
6597 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6598 ASSERT(pool_guid
== spa_guid(spa
));
6599 spa_close(spa
, FTAG
);
6601 nvlist_free(config
);
6605 ztest_resume(spa_t
*spa
)
6607 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6608 (void) printf("resuming from suspended state\n");
6609 spa_vdev_state_enter(spa
, SCL_NONE
);
6610 vdev_clear(spa
, NULL
);
6611 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6612 (void) zio_resume(spa
);
6616 ztest_resume_thread(void *arg
)
6620 while (!ztest_exiting
) {
6621 if (spa_suspended(spa
))
6623 (void) poll(NULL
, 0, 100);
6626 * Periodically change the zfs_compressed_arc_enabled setting.
6628 if (ztest_random(10) == 0)
6629 zfs_compressed_arc_enabled
= ztest_random(2);
6632 * Periodically change the zfs_abd_scatter_enabled setting.
6634 if (ztest_random(10) == 0)
6635 zfs_abd_scatter_enabled
= ztest_random(2);
6642 ztest_deadman_thread(void *arg
)
6644 ztest_shared_t
*zs
= arg
;
6645 spa_t
*spa
= ztest_spa
;
6646 hrtime_t delay
, overdue
, last_run
= gethrtime();
6648 delay
= (zs
->zs_thread_stop
- zs
->zs_thread_start
) +
6649 MSEC2NSEC(zfs_deadman_synctime_ms
);
6651 while (!ztest_exiting
) {
6653 * Wait for the delay timer while checking occasionally
6654 * if we should stop.
6656 if (gethrtime() < last_run
+ delay
) {
6657 (void) poll(NULL
, 0, 1000);
6662 * If the pool is suspended then fail immediately. Otherwise,
6663 * check to see if the pool is making any progress. If
6664 * vdev_deadman() discovers that there hasn't been any recent
6665 * I/Os then it will end up aborting the tests.
6667 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6668 fatal(0, "aborting test after %llu seconds because "
6669 "pool has transitioned to a suspended state.",
6670 zfs_deadman_synctime_ms
/ 1000);
6672 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6675 * If the process doesn't complete within a grace period of
6676 * zfs_deadman_synctime_ms over the expected finish time,
6677 * then it may be hung and is terminated.
6679 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6680 if (gethrtime() > overdue
) {
6681 fatal(0, "aborting test after %llu seconds because "
6682 "the process is overdue for termination.",
6683 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6686 (void) printf("ztest has been running for %lld seconds\n",
6687 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6689 last_run
= gethrtime();
6690 delay
= MSEC2NSEC(zfs_deadman_checktime_ms
);
6697 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6699 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6700 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6701 hrtime_t functime
= gethrtime();
6704 for (i
= 0; i
< zi
->zi_iters
; i
++)
6705 zi
->zi_func(zd
, id
);
6707 functime
= gethrtime() - functime
;
6709 atomic_add_64(&zc
->zc_count
, 1);
6710 atomic_add_64(&zc
->zc_time
, functime
);
6712 if (ztest_opts
.zo_verbose
>= 4)
6713 (void) printf("%6.2f sec in %s\n",
6714 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6718 ztest_thread(void *arg
)
6721 uint64_t id
= (uintptr_t)arg
;
6722 ztest_shared_t
*zs
= ztest_shared
;
6726 ztest_shared_callstate_t
*zc
;
6728 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6730 * See if it's time to force a crash.
6732 if (now
> zs
->zs_thread_kill
)
6736 * If we're getting ENOSPC with some regularity, stop.
6738 if (zs
->zs_enospc_count
> 10)
6742 * Pick a random function to execute.
6744 rand
= ztest_random(ZTEST_FUNCS
);
6745 zi
= &ztest_info
[rand
];
6746 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6747 call_next
= zc
->zc_next
;
6749 if (now
>= call_next
&&
6750 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6751 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6752 ztest_execute(rand
, zi
, id
);
6760 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6762 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6766 ztest_dataset_destroy(int d
)
6768 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6771 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6773 if (ztest_opts
.zo_verbose
>= 3)
6774 (void) printf("Destroying %s to free up space\n", name
);
6777 * Cleanup any non-standard clones and snapshots. In general,
6778 * ztest thread t operates on dataset (t % zopt_datasets),
6779 * so there may be more than one thing to clean up.
6781 for (t
= d
; t
< ztest_opts
.zo_threads
;
6782 t
+= ztest_opts
.zo_datasets
)
6783 ztest_dsl_dataset_cleanup(name
, t
);
6785 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6786 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6790 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6792 uint64_t usedobjs
, dirobjs
, scratch
;
6795 * ZTEST_DIROBJ is the object directory for the entire dataset.
6796 * Therefore, the number of objects in use should equal the
6797 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6798 * If not, we have an object leak.
6800 * Note that we can only check this in ztest_dataset_open(),
6801 * when the open-context and syncing-context values agree.
6802 * That's because zap_count() returns the open-context value,
6803 * while dmu_objset_space() returns the rootbp fill count.
6805 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6806 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6807 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6811 ztest_dataset_open(int d
)
6813 ztest_ds_t
*zd
= &ztest_ds
[d
];
6814 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6817 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6820 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6822 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6824 error
= ztest_dataset_create(name
);
6825 if (error
== ENOSPC
) {
6826 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6827 ztest_record_enospc(FTAG
);
6830 ASSERT(error
== 0 || error
== EEXIST
);
6832 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6834 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6836 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6838 zilog
= zd
->zd_zilog
;
6840 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6841 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6842 fatal(0, "missing log records: claimed %llu < committed %llu",
6843 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6845 ztest_dataset_dirobj_verify(zd
);
6847 zil_replay(os
, zd
, ztest_replay_vector
);
6849 ztest_dataset_dirobj_verify(zd
);
6851 if (ztest_opts
.zo_verbose
>= 6)
6852 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6854 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6855 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6856 (u_longlong_t
)zilog
->zl_replaying_seq
);
6858 zilog
= zil_open(os
, ztest_get_data
);
6860 if (zilog
->zl_replaying_seq
!= 0 &&
6861 zilog
->zl_replaying_seq
< committed_seq
)
6862 fatal(0, "missing log records: replayed %llu < committed %llu",
6863 zilog
->zl_replaying_seq
, committed_seq
);
6869 ztest_dataset_close(int d
)
6871 ztest_ds_t
*zd
= &ztest_ds
[d
];
6873 zil_close(zd
->zd_zilog
);
6874 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6881 ztest_replay_zil_cb(const char *name
, void *arg
)
6886 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_ANY
, B_TRUE
,
6887 B_TRUE
, FTAG
, &os
));
6889 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
6891 ztest_zd_init(zdtmp
, NULL
, os
);
6892 zil_replay(os
, zdtmp
, ztest_replay_vector
);
6893 ztest_zd_fini(zdtmp
);
6895 if (dmu_objset_zil(os
)->zl_parse_lr_count
!= 0 &&
6896 ztest_opts
.zo_verbose
>= 6) {
6897 zilog_t
*zilog
= dmu_objset_zil(os
);
6899 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6901 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6902 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6903 (u_longlong_t
)zilog
->zl_replaying_seq
);
6906 umem_free(zdtmp
, sizeof (ztest_ds_t
));
6908 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6915 ztest_ds_t
*zd
= &ztest_ds
[0];
6919 if (ztest_opts
.zo_verbose
>= 3)
6920 (void) printf("testing spa_freeze()...\n");
6922 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
6923 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6924 VERIFY3U(0, ==, ztest_dataset_open(0));
6928 * Force the first log block to be transactionally allocated.
6929 * We have to do this before we freeze the pool -- otherwise
6930 * the log chain won't be anchored.
6932 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
6933 ztest_dmu_object_alloc_free(zd
, 0);
6934 zil_commit(zd
->zd_zilog
, 0);
6937 txg_wait_synced(spa_get_dsl(spa
), 0);
6940 * Freeze the pool. This stops spa_sync() from doing anything,
6941 * so that the only way to record changes from now on is the ZIL.
6946 * Because it is hard to predict how much space a write will actually
6947 * require beforehand, we leave ourselves some fudge space to write over
6950 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
6953 * Run tests that generate log records but don't alter the pool config
6954 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6955 * We do a txg_wait_synced() after each iteration to force the txg
6956 * to increase well beyond the last synced value in the uberblock.
6957 * The ZIL should be OK with that.
6959 * Run a random number of times less than zo_maxloops and ensure we do
6960 * not run out of space on the pool.
6962 while (ztest_random(10) != 0 &&
6963 numloops
++ < ztest_opts
.zo_maxloops
&&
6964 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
6966 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
6967 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
6968 ztest_io(zd
, od
.od_object
,
6969 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
6970 txg_wait_synced(spa_get_dsl(spa
), 0);
6974 * Commit all of the changes we just generated.
6976 zil_commit(zd
->zd_zilog
, 0);
6977 txg_wait_synced(spa_get_dsl(spa
), 0);
6980 * Close our dataset and close the pool.
6982 ztest_dataset_close(0);
6983 spa_close(spa
, FTAG
);
6987 * Open and close the pool and dataset to induce log replay.
6989 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
6990 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6991 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
6992 VERIFY3U(0, ==, ztest_dataset_open(0));
6994 txg_wait_synced(spa_get_dsl(spa
), 0);
6995 ztest_dataset_close(0);
6996 ztest_reguid(NULL
, 0);
6998 spa_close(spa
, FTAG
);
7003 ztest_import_impl(ztest_shared_t
*zs
)
7005 importargs_t args
= { 0 };
7006 nvlist_t
*cfg
= NULL
;
7008 char *searchdirs
[nsearch
];
7009 int flags
= ZFS_IMPORT_MISSING_LOG
;
7011 searchdirs
[0] = ztest_opts
.zo_dir
;
7012 args
.paths
= nsearch
;
7013 args
.path
= searchdirs
;
7014 args
.can_be_active
= B_FALSE
;
7016 VERIFY0(zpool_find_config(NULL
, ztest_opts
.zo_pool
, &cfg
, &args
,
7017 &libzpool_config_ops
));
7018 VERIFY0(spa_import(ztest_opts
.zo_pool
, cfg
, NULL
, flags
));
7022 * Import a storage pool with the given name.
7025 ztest_import(ztest_shared_t
*zs
)
7029 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7030 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7031 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7033 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7035 ztest_import_impl(zs
);
7037 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7038 zs
->zs_metaslab_sz
=
7039 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7040 spa_close(spa
, FTAG
);
7044 if (!ztest_opts
.zo_mmp_test
) {
7045 ztest_run_zdb(ztest_opts
.zo_pool
);
7047 ztest_run_zdb(ztest_opts
.zo_pool
);
7050 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7051 mutex_destroy(&ztest_vdev_lock
);
7052 mutex_destroy(&ztest_checkpoint_lock
);
7056 * Kick off threads to run tests on all datasets in parallel.
7059 ztest_run(ztest_shared_t
*zs
)
7063 kthread_t
*resume_thread
, *deadman_thread
;
7064 kthread_t
**run_threads
;
7069 ztest_exiting
= B_FALSE
;
7072 * Initialize parent/child shared state.
7074 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7075 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7076 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7078 zs
->zs_thread_start
= gethrtime();
7079 zs
->zs_thread_stop
=
7080 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
7081 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
7082 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
7083 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
7084 zs
->zs_thread_kill
-=
7085 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
7088 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7090 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
7091 offsetof(ztest_cb_data_t
, zcd_node
));
7094 * Open our pool. It may need to be imported first depending on
7095 * what tests were running when the previous pass was terminated.
7097 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7098 error
= spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
);
7100 VERIFY3S(error
, ==, ENOENT
);
7101 ztest_import_impl(zs
);
7102 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7103 zs
->zs_metaslab_sz
=
7104 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7107 metaslab_preload_limit
= ztest_random(20) + 1;
7110 VERIFY0(vdev_raidz_impl_set("cycle"));
7112 dmu_objset_stats_t dds
;
7113 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
7114 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
7115 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
7116 dmu_objset_fast_stat(os
, &dds
);
7117 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
7118 zs
->zs_guid
= dds
.dds_guid
;
7119 dmu_objset_disown(os
, B_TRUE
, FTAG
);
7122 * Create a thread to periodically resume suspended I/O.
7124 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
7125 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
7128 * Create a deadman thread and set to panic if we hang.
7130 deadman_thread
= thread_create(NULL
, 0, ztest_deadman_thread
,
7131 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
7133 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
7136 * Verify that we can safely inquire about any object,
7137 * whether it's allocated or not. To make it interesting,
7138 * we probe a 5-wide window around each power of two.
7139 * This hits all edge cases, including zero and the max.
7141 for (t
= 0; t
< 64; t
++) {
7142 for (d
= -5; d
<= 5; d
++) {
7143 error
= dmu_object_info(spa
->spa_meta_objset
,
7144 (1ULL << t
) + d
, NULL
);
7145 ASSERT(error
== 0 || error
== ENOENT
||
7151 * If we got any ENOSPC errors on the previous run, destroy something.
7153 if (zs
->zs_enospc_count
!= 0) {
7154 int d
= ztest_random(ztest_opts
.zo_datasets
);
7155 ztest_dataset_destroy(d
);
7157 zs
->zs_enospc_count
= 0;
7160 * If we were in the middle of ztest_device_removal() and were killed
7161 * we need to ensure the removal and scrub complete before running
7162 * any tests that check ztest_device_removal_active. The removal will
7163 * be restarted automatically when the spa is opened, but we need to
7164 * initiate the scrub manually if it is not already in progress. Note
7165 * that we always run the scrub whenever an indirect vdev exists
7166 * because we have no way of knowing for sure if ztest_device_removal()
7167 * fully completed its scrub before the pool was reimported.
7169 if (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
||
7170 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7171 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
7172 txg_wait_synced(spa_get_dsl(spa
), 0);
7174 error
= ztest_scrub_impl(spa
);
7180 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
7183 if (ztest_opts
.zo_verbose
>= 4)
7184 (void) printf("starting main threads...\n");
7187 * Replay all logs of all datasets in the pool. This is primarily for
7188 * temporary datasets which wouldn't otherwise get replayed, which
7189 * can trigger failures when attempting to offline a SLOG in
7190 * ztest_fault_inject().
7192 (void) dmu_objset_find(ztest_opts
.zo_pool
, ztest_replay_zil_cb
,
7193 NULL
, DS_FIND_CHILDREN
);
7196 * Kick off all the tests that run in parallel.
7198 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7199 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
7200 umem_free(run_threads
, ztest_opts
.zo_threads
*
7201 sizeof (kthread_t
*));
7205 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
7206 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
7211 * Wait for all of the tests to complete.
7213 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++)
7214 VERIFY0(thread_join(run_threads
[t
]));
7217 * Close all datasets. This must be done after all the threads
7218 * are joined so we can be sure none of the datasets are in-use
7219 * by any of the threads.
7221 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7222 if (t
< ztest_opts
.zo_datasets
)
7223 ztest_dataset_close(t
);
7226 txg_wait_synced(spa_get_dsl(spa
), 0);
7228 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
7229 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
7231 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
7233 /* Kill the resume and deadman threads */
7234 ztest_exiting
= B_TRUE
;
7235 VERIFY0(thread_join(resume_thread
));
7236 VERIFY0(thread_join(deadman_thread
));
7240 * Right before closing the pool, kick off a bunch of async I/O;
7241 * spa_close() should wait for it to complete.
7243 for (object
= 1; object
< 50; object
++) {
7244 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
7245 ZIO_PRIORITY_SYNC_READ
);
7248 /* Verify that at least one commit cb was called in a timely fashion */
7249 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
7250 VERIFY0(zc_min_txg_delay
);
7252 spa_close(spa
, FTAG
);
7255 * Verify that we can loop over all pools.
7257 mutex_enter(&spa_namespace_lock
);
7258 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
7259 if (ztest_opts
.zo_verbose
> 3)
7260 (void) printf("spa_next: found %s\n", spa_name(spa
));
7261 mutex_exit(&spa_namespace_lock
);
7264 * Verify that we can export the pool and reimport it under a
7267 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7268 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7269 (void) snprintf(name
, sizeof (name
), "%s_import",
7270 ztest_opts
.zo_pool
);
7271 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7272 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7277 list_destroy(&zcl
.zcl_callbacks
);
7278 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7279 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7280 mutex_destroy(&ztest_vdev_lock
);
7281 mutex_destroy(&ztest_checkpoint_lock
);
7285 print_time(hrtime_t t
, char *timebuf
)
7287 hrtime_t s
= t
/ NANOSEC
;
7288 hrtime_t m
= s
/ 60;
7289 hrtime_t h
= m
/ 60;
7290 hrtime_t d
= h
/ 24;
7299 (void) sprintf(timebuf
,
7300 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7302 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7304 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7306 (void) sprintf(timebuf
, "%llus", s
);
7310 make_random_props(void)
7314 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7316 if (ztest_random(2) == 0)
7319 VERIFY0(nvlist_add_uint64(props
,
7320 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7326 * Create a storage pool with the given name and initial vdev size.
7327 * Then test spa_freeze() functionality.
7330 ztest_init(ztest_shared_t
*zs
)
7333 nvlist_t
*nvroot
, *props
;
7336 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7337 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7338 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7340 kernel_init(SPA_MODE_READ
| SPA_MODE_WRITE
);
7343 * Create the storage pool.
7345 (void) spa_destroy(ztest_opts
.zo_pool
);
7346 ztest_shared
->zs_vdev_next_leaf
= 0;
7348 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7349 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7350 NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
7351 props
= make_random_props();
7354 * We don't expect the pool to suspend unless maxfaults == 0,
7355 * in which case ztest_fault_inject() temporarily takes away
7356 * the only valid replica.
7358 VERIFY0(nvlist_add_uint64(props
,
7359 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7360 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7362 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7366 * 75% chance of using the log space map feature. We want ztest
7367 * to exercise both the code paths that use the log space map
7368 * feature and the ones that don't.
7370 if (i
== SPA_FEATURE_LOG_SPACEMAP
&& ztest_random(4) == 0)
7373 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7374 spa_feature_table
[i
].fi_uname
));
7375 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7379 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7380 nvlist_free(nvroot
);
7383 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7384 zs
->zs_metaslab_sz
=
7385 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7386 spa_close(spa
, FTAG
);
7390 if (!ztest_opts
.zo_mmp_test
) {
7391 ztest_run_zdb(ztest_opts
.zo_pool
);
7393 ztest_run_zdb(ztest_opts
.zo_pool
);
7396 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7397 mutex_destroy(&ztest_vdev_lock
);
7398 mutex_destroy(&ztest_checkpoint_lock
);
7404 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7406 ztest_fd_data
= mkstemp(ztest_name_data
);
7407 ASSERT3S(ztest_fd_data
, >=, 0);
7408 (void) unlink(ztest_name_data
);
7412 shared_data_size(ztest_shared_hdr_t
*hdr
)
7416 size
= hdr
->zh_hdr_size
;
7417 size
+= hdr
->zh_opts_size
;
7418 size
+= hdr
->zh_size
;
7419 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7420 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7429 ztest_shared_hdr_t
*hdr
;
7431 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7432 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7433 ASSERT(hdr
!= MAP_FAILED
);
7435 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7437 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7438 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7439 hdr
->zh_size
= sizeof (ztest_shared_t
);
7440 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7441 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7442 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7443 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7445 size
= shared_data_size(hdr
);
7446 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7448 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7455 ztest_shared_hdr_t
*hdr
;
7458 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7459 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7460 ASSERT(hdr
!= MAP_FAILED
);
7462 size
= shared_data_size(hdr
);
7464 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7465 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7466 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7467 ASSERT(hdr
!= MAP_FAILED
);
7468 buf
= (uint8_t *)hdr
;
7470 offset
= hdr
->zh_hdr_size
;
7471 ztest_shared_opts
= (void *)&buf
[offset
];
7472 offset
+= hdr
->zh_opts_size
;
7473 ztest_shared
= (void *)&buf
[offset
];
7474 offset
+= hdr
->zh_size
;
7475 ztest_shared_callstate
= (void *)&buf
[offset
];
7476 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7477 ztest_shared_ds
= (void *)&buf
[offset
];
7481 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7485 char *cmdbuf
= NULL
;
7490 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7491 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7496 fatal(1, "fork failed");
7498 if (pid
== 0) { /* child */
7499 char *emptyargv
[2] = { cmd
, NULL
};
7500 char fd_data_str
[12];
7502 struct rlimit rl
= { 1024, 1024 };
7503 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7505 (void) close(ztest_fd_rand
);
7506 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7507 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7509 (void) enable_extended_FILE_stdio(-1, -1);
7510 if (libpath
!= NULL
)
7511 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7512 (void) execv(cmd
, emptyargv
);
7513 ztest_dump_core
= B_FALSE
;
7514 fatal(B_TRUE
, "exec failed: %s", cmd
);
7517 if (cmdbuf
!= NULL
) {
7518 umem_free(cmdbuf
, MAXPATHLEN
);
7522 while (waitpid(pid
, &status
, 0) != pid
)
7524 if (statusp
!= NULL
)
7527 if (WIFEXITED(status
)) {
7528 if (WEXITSTATUS(status
) != 0) {
7529 (void) fprintf(stderr
, "child exited with code %d\n",
7530 WEXITSTATUS(status
));
7534 } else if (WIFSIGNALED(status
)) {
7535 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7536 (void) fprintf(stderr
, "child died with signal %d\n",
7542 (void) fprintf(stderr
, "something strange happened to child\n");
7549 ztest_run_init(void)
7553 ztest_shared_t
*zs
= ztest_shared
;
7556 * Blow away any existing copy of zpool.cache
7558 (void) remove(spa_config_path
);
7560 if (ztest_opts
.zo_init
== 0) {
7561 if (ztest_opts
.zo_verbose
>= 1)
7562 (void) printf("Importing pool %s\n",
7563 ztest_opts
.zo_pool
);
7569 * Create and initialize our storage pool.
7571 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7572 bzero(zs
, sizeof (ztest_shared_t
));
7573 if (ztest_opts
.zo_verbose
>= 3 &&
7574 ztest_opts
.zo_init
!= 1) {
7575 (void) printf("ztest_init(), pass %d\n", i
);
7582 main(int argc
, char **argv
)
7590 ztest_shared_callstate_t
*zc
;
7592 char numbuf
[NN_NUMBUF_SZ
];
7596 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7597 struct sigaction action
;
7599 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7601 dprintf_setup(&argc
, argv
);
7602 zfs_deadman_synctime_ms
= 300000;
7603 zfs_deadman_checktime_ms
= 30000;
7605 * As two-word space map entries may not come up often (especially
7606 * if pool and vdev sizes are small) we want to force at least some
7607 * of them so the feature get tested.
7609 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7612 * Verify that even extensively damaged split blocks with many
7613 * segments can be reconstructed in a reasonable amount of time
7614 * when reconstruction is known to be possible.
7616 * Note: the lower this value is, the more damage we inflict, and
7617 * the more time ztest spends in recovering that damage. We chose
7618 * to induce damage 1/100th of the time so recovery is tested but
7619 * not so frequently that ztest doesn't get to test other code paths.
7621 zfs_reconstruct_indirect_damage_fraction
= 100;
7623 action
.sa_handler
= sig_handler
;
7624 sigemptyset(&action
.sa_mask
);
7625 action
.sa_flags
= 0;
7627 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7628 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7633 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7634 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7640 * Force random_get_bytes() to use /dev/urandom in order to prevent
7641 * ztest from needlessly depleting the system entropy pool.
7643 random_path
= "/dev/urandom";
7644 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7645 ASSERT3S(ztest_fd_rand
, >=, 0);
7648 process_options(argc
, argv
);
7653 bcopy(&ztest_opts
, ztest_shared_opts
,
7654 sizeof (*ztest_shared_opts
));
7656 ztest_fd_data
= atoi(fd_data_str
);
7658 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7660 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7662 /* Override location of zpool.cache */
7663 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7664 ztest_opts
.zo_dir
) != -1);
7666 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7671 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7672 metaslab_df_alloc_threshold
=
7673 zs
->zs_metaslab_df_alloc_threshold
;
7682 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7684 if (ztest_opts
.zo_verbose
>= 1) {
7685 (void) printf("%llu vdevs, %d datasets, %d threads,"
7686 " %llu seconds...\n",
7687 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7688 ztest_opts
.zo_datasets
,
7689 ztest_opts
.zo_threads
,
7690 (u_longlong_t
)ztest_opts
.zo_time
);
7693 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7694 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7696 zs
->zs_do_init
= B_TRUE
;
7697 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7698 if (ztest_opts
.zo_verbose
>= 1) {
7699 (void) printf("Executing older ztest for "
7700 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7702 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7703 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7705 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7707 zs
->zs_do_init
= B_FALSE
;
7709 zs
->zs_proc_start
= gethrtime();
7710 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7712 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7713 zi
= &ztest_info
[f
];
7714 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7715 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7716 zc
->zc_next
= UINT64_MAX
;
7718 zc
->zc_next
= zs
->zs_proc_start
+
7719 ztest_random(2 * zi
->zi_interval
[0] + 1);
7723 * Run the tests in a loop. These tests include fault injection
7724 * to verify that self-healing data works, and forced crashes
7725 * to verify that we never lose on-disk consistency.
7727 while (gethrtime() < zs
->zs_proc_stop
) {
7732 * Initialize the workload counters for each function.
7734 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7735 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7740 /* Set the allocation switch size */
7741 zs
->zs_metaslab_df_alloc_threshold
=
7742 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7744 if (!hasalt
|| ztest_random(2) == 0) {
7745 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7746 (void) printf("Executing newer ztest: %s\n",
7750 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7752 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7753 (void) printf("Executing older ztest: %s\n",
7754 ztest_opts
.zo_alt_ztest
);
7757 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7758 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7765 if (ztest_opts
.zo_verbose
>= 1) {
7766 hrtime_t now
= gethrtime();
7768 now
= MIN(now
, zs
->zs_proc_stop
);
7769 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7770 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7772 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7773 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7775 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7776 (u_longlong_t
)zs
->zs_enospc_count
,
7777 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7779 100.0 * (now
- zs
->zs_proc_start
) /
7780 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7783 if (ztest_opts
.zo_verbose
>= 2) {
7784 (void) printf("\nWorkload summary:\n\n");
7785 (void) printf("%7s %9s %s\n",
7786 "Calls", "Time", "Function");
7787 (void) printf("%7s %9s %s\n",
7788 "-----", "----", "--------");
7789 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7790 zi
= &ztest_info
[f
];
7791 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7792 print_time(zc
->zc_time
, timebuf
);
7793 (void) printf("%7llu %9s %s\n",
7794 (u_longlong_t
)zc
->zc_count
, timebuf
,
7797 (void) printf("\n");
7800 if (!ztest_opts
.zo_mmp_test
)
7801 ztest_run_zdb(ztest_opts
.zo_pool
);
7804 if (ztest_opts
.zo_verbose
>= 1) {
7806 (void) printf("%d runs of older ztest: %s\n", older
,
7807 ztest_opts
.zo_alt_ztest
);
7808 (void) printf("%d runs of newer ztest: %s\n", newer
,
7811 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7812 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7815 umem_free(cmd
, MAXNAMELEN
);