4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
28 * Copyright (c) 2017, Intel Corporation.
32 * The objective of this program is to provide a DMU/ZAP/SPA stress test
33 * that runs entirely in userland, is easy to use, and easy to extend.
35 * The overall design of the ztest program is as follows:
37 * (1) For each major functional area (e.g. adding vdevs to a pool,
38 * creating and destroying datasets, reading and writing objects, etc)
39 * we have a simple routine to test that functionality. These
40 * individual routines do not have to do anything "stressful".
42 * (2) We turn these simple functionality tests into a stress test by
43 * running them all in parallel, with as many threads as desired,
44 * and spread across as many datasets, objects, and vdevs as desired.
46 * (3) While all this is happening, we inject faults into the pool to
47 * verify that self-healing data really works.
49 * (4) Every time we open a dataset, we change its checksum and compression
50 * functions. Thus even individual objects vary from block to block
51 * in which checksum they use and whether they're compressed.
53 * (5) To verify that we never lose on-disk consistency after a crash,
54 * we run the entire test in a child of the main process.
55 * At random times, the child self-immolates with a SIGKILL.
56 * This is the software equivalent of pulling the power cord.
57 * The parent then runs the test again, using the existing
58 * storage pool, as many times as desired. If backwards compatibility
59 * testing is enabled ztest will sometimes run the "older" version
60 * of ztest after a SIGKILL.
62 * (6) To verify that we don't have future leaks or temporal incursions,
63 * many of the functional tests record the transaction group number
64 * as part of their data. When reading old data, they verify that
65 * the transaction group number is less than the current, open txg.
66 * If you add a new test, please do this if applicable.
68 * (7) Threads are created with a reduced stack size, for sanity checking.
69 * Therefore, it's important not to allocate huge buffers on the stack.
71 * When run with no arguments, ztest runs for about five minutes and
72 * produces no output if successful. To get a little bit of information,
73 * specify -V. To get more information, specify -VV, and so on.
75 * To turn this into an overnight stress test, use -T to specify run time.
77 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
78 * to increase the pool capacity, fanout, and overall stress level.
80 * Use the -k option to set the desired frequency of kills.
82 * When ztest invokes itself it passes all relevant information through a
83 * temporary file which is mmap-ed in the child process. This allows shared
84 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
85 * stored at offset 0 of this file and contains information on the size and
86 * number of shared structures in the file. The information stored in this file
87 * must remain backwards compatible with older versions of ztest so that
88 * ztest can invoke them during backwards compatibility testing (-B).
91 #include <sys/zfs_context.h>
97 #include <sys/dmu_objset.h>
100 #include <sys/time.h>
101 #include <sys/wait.h>
102 #include <sys/mman.h>
103 #include <sys/resource.h>
106 #include <sys/zil_impl.h>
107 #include <sys/vdev_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/vdev_initialize.h>
110 #include <sys/spa_impl.h>
111 #include <sys/metaslab_impl.h>
112 #include <sys/dsl_prop.h>
113 #include <sys/dsl_dataset.h>
114 #include <sys/dsl_destroy.h>
115 #include <sys/dsl_scan.h>
116 #include <sys/zio_checksum.h>
117 #include <sys/refcount.h>
118 #include <sys/zfeature.h>
119 #include <sys/dsl_userhold.h>
122 #include <stdio_ext.h>
129 #include <sys/fs/zfs.h>
130 #include <zfs_fletcher.h>
131 #include <libnvpair.h>
132 #include <libzutil.h>
133 #include <sys/crypto/icp.h>
135 #include <execinfo.h> /* for backtrace() */
138 static int ztest_fd_data
= -1;
139 static int ztest_fd_rand
= -1;
141 typedef struct ztest_shared_hdr
{
142 uint64_t zh_hdr_size
;
143 uint64_t zh_opts_size
;
145 uint64_t zh_stats_size
;
146 uint64_t zh_stats_count
;
148 uint64_t zh_ds_count
;
149 } ztest_shared_hdr_t
;
151 static ztest_shared_hdr_t
*ztest_shared_hdr
;
153 enum ztest_class_state
{
154 ZTEST_VDEV_CLASS_OFF
,
159 typedef struct ztest_shared_opts
{
160 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
161 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
162 char zo_alt_ztest
[MAXNAMELEN
];
163 char zo_alt_libpath
[MAXNAMELEN
];
165 uint64_t zo_vdevtime
;
173 uint64_t zo_passtime
;
174 uint64_t zo_killrate
;
178 uint64_t zo_maxloops
;
179 uint64_t zo_metaslab_force_ganging
;
181 int zo_special_vdevs
;
183 } ztest_shared_opts_t
;
185 static const ztest_shared_opts_t ztest_opts_defaults
= {
186 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
187 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
188 .zo_alt_ztest
= { '\0' },
189 .zo_alt_libpath
= { '\0' },
191 .zo_ashift
= SPA_MINBLOCKSHIFT
,
194 .zo_raidz_parity
= 1,
195 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
198 .zo_passtime
= 60, /* 60 seconds */
199 .zo_killrate
= 70, /* 70% kill rate */
203 .zo_time
= 300, /* 5 minutes */
204 .zo_maxloops
= 50, /* max loops during spa_freeze() */
205 .zo_metaslab_force_ganging
= 64 << 10,
206 .zo_special_vdevs
= ZTEST_VDEV_CLASS_RND
,
209 extern uint64_t metaslab_force_ganging
;
210 extern uint64_t metaslab_df_alloc_threshold
;
211 extern unsigned long zfs_deadman_synctime_ms
;
212 extern int metaslab_preload_limit
;
213 extern boolean_t zfs_compressed_arc_enabled
;
214 extern int zfs_abd_scatter_enabled
;
215 extern int dmu_object_alloc_chunk_shift
;
216 extern boolean_t zfs_force_some_double_word_sm_entries
;
217 extern unsigned long zio_decompress_fail_fraction
;
218 extern unsigned long zfs_reconstruct_indirect_damage_fraction
;
219 extern int zfs_object_remap_one_indirect_delay_ms
;
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_ddt_repair
;
363 ztest_func_t ztest_dmu_snapshot_hold
;
364 ztest_func_t ztest_mmp_enable_disable
;
365 ztest_func_t ztest_scrub
;
366 ztest_func_t ztest_dsl_dataset_promote_busy
;
367 ztest_func_t ztest_vdev_attach_detach
;
368 ztest_func_t ztest_vdev_LUN_growth
;
369 ztest_func_t ztest_vdev_add_remove
;
370 ztest_func_t ztest_vdev_class_add
;
371 ztest_func_t ztest_vdev_aux_add_remove
;
372 ztest_func_t ztest_split_pool
;
373 ztest_func_t ztest_reguid
;
374 ztest_func_t ztest_spa_upgrade
;
375 ztest_func_t ztest_device_removal
;
376 ztest_func_t ztest_remap_blocks
;
377 ztest_func_t ztest_spa_checkpoint_create_discard
;
378 ztest_func_t ztest_initialize
;
379 ztest_func_t ztest_fletcher
;
380 ztest_func_t ztest_fletcher_incr
;
381 ztest_func_t ztest_verify_dnode_bt
;
383 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
384 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
385 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
386 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
387 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
389 #define ZTI_INIT(func, iters, interval) \
390 { .zi_func = (func), \
391 .zi_iters = (iters), \
392 .zi_interval = (interval), \
393 .zi_funcname = # func }
395 ztest_info_t ztest_info
[] = {
396 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
397 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
398 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
399 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
400 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
401 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
402 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
403 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
404 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
405 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
406 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
407 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
408 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
409 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
411 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
413 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
414 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
415 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
416 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
417 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
418 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
419 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
420 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
421 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
422 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
423 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
424 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
425 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
426 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
427 ZTI_INIT(ztest_vdev_class_add
, 1, &ztest_opts
.zo_vdevtime
),
428 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
429 ZTI_INIT(ztest_device_removal
, 1, &zopt_sometimes
),
430 ZTI_INIT(ztest_remap_blocks
, 1, &zopt_sometimes
),
431 ZTI_INIT(ztest_spa_checkpoint_create_discard
, 1, &zopt_rarely
),
432 ZTI_INIT(ztest_initialize
, 1, &zopt_sometimes
),
433 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
434 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
435 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
438 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
441 * The following struct is used to hold a list of uncalled commit callbacks.
442 * The callbacks are ordered by txg number.
444 typedef struct ztest_cb_list
{
445 kmutex_t zcl_callbacks_lock
;
446 list_t zcl_callbacks
;
450 * Stuff we need to share writably between parent and child.
452 typedef struct ztest_shared
{
453 boolean_t zs_do_init
;
454 hrtime_t zs_proc_start
;
455 hrtime_t zs_proc_stop
;
456 hrtime_t zs_thread_start
;
457 hrtime_t zs_thread_stop
;
458 hrtime_t zs_thread_kill
;
459 uint64_t zs_enospc_count
;
460 uint64_t zs_vdev_next_leaf
;
461 uint64_t zs_vdev_aux
;
466 uint64_t zs_metaslab_sz
;
467 uint64_t zs_metaslab_df_alloc_threshold
;
471 #define ID_PARALLEL -1ULL
473 static char ztest_dev_template
[] = "%s/%s.%llua";
474 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
475 ztest_shared_t
*ztest_shared
;
477 static spa_t
*ztest_spa
= NULL
;
478 static ztest_ds_t
*ztest_ds
;
480 static kmutex_t ztest_vdev_lock
;
481 static boolean_t ztest_device_removal_active
= B_FALSE
;
482 static kmutex_t ztest_checkpoint_lock
;
485 * The ztest_name_lock protects the pool and dataset namespace used by
486 * the individual tests. To modify the namespace, consumers must grab
487 * this lock as writer. Grabbing the lock as reader will ensure that the
488 * namespace does not change while the lock is held.
490 static pthread_rwlock_t ztest_name_lock
;
492 static boolean_t ztest_dump_core
= B_TRUE
;
493 static boolean_t ztest_exiting
;
495 /* Global commit callback list */
496 static ztest_cb_list_t zcl
;
497 /* Commit cb delay */
498 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
499 static int zc_cb_counter
= 0;
502 * Minimum number of commit callbacks that need to be registered for us to check
503 * whether the minimum txg delay is acceptable.
505 #define ZTEST_COMMIT_CB_MIN_REG 100
508 * If a number of txgs equal to this threshold have been created after a commit
509 * callback has been registered but not called, then we assume there is an
510 * implementation bug.
512 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
515 ZTEST_META_DNODE
= 0,
520 static void usage(boolean_t
) __NORETURN
;
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
));
651 if (fval
> UINT64_MAX
) {
652 (void) fprintf(stderr
, "ztest: value too large: %s\n",
656 val
= (uint64_t)fval
;
658 int shift
= str2shift(end
);
659 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
660 (void) fprintf(stderr
, "ztest: value too large: %s\n",
670 usage(boolean_t requested
)
672 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
674 char nice_vdev_size
[NN_NUMBUF_SZ
];
675 char nice_force_ganging
[NN_NUMBUF_SZ
];
676 FILE *fp
= requested
? stdout
: stderr
;
678 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
679 nicenum(zo
->zo_metaslab_force_ganging
, nice_force_ganging
,
680 sizeof (nice_force_ganging
));
682 (void) fprintf(fp
, "Usage: %s\n"
683 "\t[-v vdevs (default: %llu)]\n"
684 "\t[-s size_of_each_vdev (default: %s)]\n"
685 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
686 "\t[-m mirror_copies (default: %d)]\n"
687 "\t[-r raidz_disks (default: %d)]\n"
688 "\t[-R raidz_parity (default: %d)]\n"
689 "\t[-d datasets (default: %d)]\n"
690 "\t[-t threads (default: %d)]\n"
691 "\t[-g gang_block_threshold (default: %s)]\n"
692 "\t[-i init_count (default: %d)] initialize pool i times\n"
693 "\t[-k kill_percentage (default: %llu%%)]\n"
694 "\t[-p pool_name (default: %s)]\n"
695 "\t[-f dir (default: %s)] file directory for vdev files\n"
696 "\t[-M] Multi-host simulate pool imported on remote host\n"
697 "\t[-V] verbose (use multiple times for ever more blather)\n"
698 "\t[-E] use existing pool instead of creating new one\n"
699 "\t[-T time (default: %llu sec)] total run time\n"
700 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
701 "\t[-P passtime (default: %llu sec)] time per pass\n"
702 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
703 "\t[-C vdev class state (default: random)] special=on|off|random\n"
704 "\t[-o variable=value] ... set global variable to an unsigned\n"
705 "\t 32-bit integer value\n"
706 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
707 "\t[-h] (print help)\n"
710 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
711 nice_vdev_size
, /* -s */
712 zo
->zo_ashift
, /* -a */
713 zo
->zo_mirrors
, /* -m */
714 zo
->zo_raidz
, /* -r */
715 zo
->zo_raidz_parity
, /* -R */
716 zo
->zo_datasets
, /* -d */
717 zo
->zo_threads
, /* -t */
718 nice_force_ganging
, /* -g */
719 zo
->zo_init
, /* -i */
720 (u_longlong_t
)zo
->zo_killrate
, /* -k */
721 zo
->zo_pool
, /* -p */
723 (u_longlong_t
)zo
->zo_time
, /* -T */
724 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
725 (u_longlong_t
)zo
->zo_passtime
);
726 exit(requested
? 0 : 1);
731 ztest_parse_name_value(const char *input
, ztest_shared_opts_t
*zo
)
735 int state
= ZTEST_VDEV_CLASS_RND
;
737 (void) strlcpy(name
, input
, sizeof (name
));
739 value
= strchr(name
, '=');
741 (void) fprintf(stderr
, "missing value in property=value "
742 "'-C' argument (%s)\n", input
);
748 if (strcmp(value
, "on") == 0) {
749 state
= ZTEST_VDEV_CLASS_ON
;
750 } else if (strcmp(value
, "off") == 0) {
751 state
= ZTEST_VDEV_CLASS_OFF
;
752 } else if (strcmp(value
, "random") == 0) {
753 state
= ZTEST_VDEV_CLASS_RND
;
755 (void) fprintf(stderr
, "invalid property value '%s'\n", value
);
759 if (strcmp(name
, "special") == 0) {
760 zo
->zo_special_vdevs
= state
;
762 (void) fprintf(stderr
, "invalid property name '%s'\n", name
);
765 if (zo
->zo_verbose
>= 3)
766 (void) printf("%s vdev state is '%s'\n", name
, value
);
770 process_options(int argc
, char **argv
)
773 ztest_shared_opts_t
*zo
= &ztest_opts
;
777 char altdir
[MAXNAMELEN
] = { 0 };
779 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
781 while ((opt
= getopt(argc
, argv
,
782 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:G")) != EOF
) {
799 value
= nicenumtoull(optarg
);
803 zo
->zo_vdevs
= value
;
806 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
809 zo
->zo_ashift
= value
;
812 zo
->zo_mirrors
= value
;
815 zo
->zo_raidz
= MAX(1, value
);
818 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
821 zo
->zo_datasets
= MAX(1, value
);
824 zo
->zo_threads
= MAX(1, value
);
827 zo
->zo_metaslab_force_ganging
=
828 MAX(SPA_MINBLOCKSIZE
<< 1, value
);
834 zo
->zo_killrate
= value
;
837 (void) strlcpy(zo
->zo_pool
, optarg
,
838 sizeof (zo
->zo_pool
));
841 path
= realpath(optarg
, NULL
);
843 (void) fprintf(stderr
, "error: %s: %s\n",
844 optarg
, strerror(errno
));
847 (void) strlcpy(zo
->zo_dir
, path
,
848 sizeof (zo
->zo_dir
));
865 zo
->zo_passtime
= MAX(1, value
);
868 zo
->zo_maxloops
= MAX(1, value
);
871 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
874 ztest_parse_name_value(optarg
, zo
);
877 if (set_global_var(optarg
) != 0)
881 zo
->zo_dump_dbgmsg
= 1;
893 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
896 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
899 if (strlen(altdir
) > 0) {
907 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
908 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
910 VERIFY(NULL
!= realpath(getexecname(), cmd
));
911 if (0 != access(altdir
, F_OK
)) {
912 ztest_dump_core
= B_FALSE
;
913 fatal(B_TRUE
, "invalid alternate ztest path: %s",
916 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
919 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
920 * We want to extract <isa> to determine if we should use
921 * 32 or 64 bit binaries.
923 bin
= strstr(cmd
, "/usr/bin/");
924 ztest
= strstr(bin
, "/ztest");
926 isalen
= ztest
- isa
;
927 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
928 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
929 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
930 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
932 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
933 ztest_dump_core
= B_FALSE
;
934 fatal(B_TRUE
, "invalid alternate ztest: %s",
936 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
937 ztest_dump_core
= B_FALSE
;
938 fatal(B_TRUE
, "invalid alternate lib directory %s",
942 umem_free(cmd
, MAXPATHLEN
);
943 umem_free(realaltdir
, MAXPATHLEN
);
948 ztest_kill(ztest_shared_t
*zs
)
950 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
951 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
954 * Before we kill off ztest, make sure that the config is updated.
955 * See comment above spa_write_cachefile().
957 mutex_enter(&spa_namespace_lock
);
958 spa_write_cachefile(ztest_spa
, B_FALSE
, B_FALSE
);
959 mutex_exit(&spa_namespace_lock
);
961 (void) kill(getpid(), SIGKILL
);
965 ztest_random(uint64_t range
)
969 ASSERT3S(ztest_fd_rand
, >=, 0);
974 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
975 fatal(1, "short read from /dev/urandom");
982 ztest_record_enospc(const char *s
)
984 ztest_shared
->zs_enospc_count
++;
988 ztest_get_ashift(void)
990 if (ztest_opts
.zo_ashift
== 0)
991 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
992 return (ztest_opts
.zo_ashift
);
996 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
1002 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1005 ashift
= ztest_get_ashift();
1011 vdev
= ztest_shared
->zs_vdev_aux
;
1012 (void) snprintf(path
, MAXPATHLEN
,
1013 ztest_aux_template
, ztest_opts
.zo_dir
,
1014 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
1017 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
1018 (void) snprintf(path
, MAXPATHLEN
,
1019 ztest_dev_template
, ztest_opts
.zo_dir
,
1020 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
1025 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1027 fatal(1, "can't open %s", path
);
1028 if (ftruncate(fd
, size
) != 0)
1029 fatal(1, "can't ftruncate %s", path
);
1033 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
1034 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
1035 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
1036 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
1037 umem_free(pathbuf
, MAXPATHLEN
);
1043 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
1044 uint64_t ashift
, int r
)
1046 nvlist_t
*raidz
, **child
;
1050 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
1051 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1053 for (c
= 0; c
< r
; c
++)
1054 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
1056 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
1057 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
1058 VDEV_TYPE_RAIDZ
) == 0);
1059 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
1060 ztest_opts
.zo_raidz_parity
) == 0);
1061 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
1064 for (c
= 0; c
< r
; c
++)
1065 nvlist_free(child
[c
]);
1067 umem_free(child
, r
* sizeof (nvlist_t
*));
1073 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
1074 uint64_t ashift
, int r
, int m
)
1076 nvlist_t
*mirror
, **child
;
1080 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
1082 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1084 for (c
= 0; c
< m
; c
++)
1085 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1087 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1088 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1089 VDEV_TYPE_MIRROR
) == 0);
1090 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1093 for (c
= 0; c
< m
; c
++)
1094 nvlist_free(child
[c
]);
1096 umem_free(child
, m
* sizeof (nvlist_t
*));
1102 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1103 const char *class, int r
, int m
, int t
)
1105 nvlist_t
*root
, **child
;
1111 log
= (class != NULL
&& strcmp(class, "log") == 0);
1113 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1115 for (c
= 0; c
< t
; c
++) {
1116 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1118 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1121 if (class != NULL
&& class[0] != '\0') {
1122 ASSERT(m
> 1 || log
); /* expecting a mirror */
1123 VERIFY(nvlist_add_string(child
[c
],
1124 ZPOOL_CONFIG_ALLOCATION_BIAS
, class) == 0);
1128 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1129 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1130 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1133 for (c
= 0; c
< t
; c
++)
1134 nvlist_free(child
[c
]);
1136 umem_free(child
, t
* sizeof (nvlist_t
*));
1142 * Find a random spa version. Returns back a random spa version in the
1143 * range [initial_version, SPA_VERSION_FEATURES].
1146 ztest_random_spa_version(uint64_t initial_version
)
1148 uint64_t version
= initial_version
;
1150 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1152 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1155 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1156 version
= SPA_VERSION_FEATURES
;
1158 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1163 ztest_random_blocksize(void)
1165 ASSERT(ztest_spa
->spa_max_ashift
!= 0);
1168 * Choose a block size >= the ashift.
1169 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1171 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1172 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1174 uint64_t block_shift
=
1175 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1176 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1180 ztest_random_dnodesize(void)
1183 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1185 if (max_slots
== DNODE_MIN_SLOTS
)
1186 return (DNODE_MIN_SIZE
);
1189 * Weight the random distribution more heavily toward smaller
1190 * dnode sizes since that is more likely to reflect real-world
1193 ASSERT3U(max_slots
, >, 4);
1194 switch (ztest_random(10)) {
1196 slots
= 5 + ztest_random(max_slots
- 4);
1199 slots
= 2 + ztest_random(3);
1206 return (slots
<< DNODE_SHIFT
);
1210 ztest_random_ibshift(void)
1212 return (DN_MIN_INDBLKSHIFT
+
1213 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1217 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1220 vdev_t
*rvd
= spa
->spa_root_vdev
;
1223 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1226 top
= ztest_random(rvd
->vdev_children
);
1227 tvd
= rvd
->vdev_child
[top
];
1228 } while (!vdev_is_concrete(tvd
) || (tvd
->vdev_islog
&& !log_ok
) ||
1229 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1235 ztest_random_dsl_prop(zfs_prop_t prop
)
1240 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1241 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1247 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1250 const char *propname
= zfs_prop_to_name(prop
);
1251 const char *valname
;
1256 error
= dsl_prop_set_int(osname
, propname
,
1257 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1259 if (error
== ENOSPC
) {
1260 ztest_record_enospc(FTAG
);
1265 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1266 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1268 if (ztest_opts
.zo_verbose
>= 6) {
1271 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1273 (void) printf("%s %s = %llu at '%s'\n", osname
,
1274 propname
, (unsigned long long)curval
, setpoint
);
1276 (void) printf("%s %s = %s at '%s'\n",
1277 osname
, propname
, valname
, setpoint
);
1279 umem_free(setpoint
, MAXPATHLEN
);
1285 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1287 spa_t
*spa
= ztest_spa
;
1288 nvlist_t
*props
= NULL
;
1291 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1292 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1294 error
= spa_prop_set(spa
, props
);
1298 if (error
== ENOSPC
) {
1299 ztest_record_enospc(FTAG
);
1308 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1309 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1313 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1315 strcpy(ddname
, name
);
1316 cp
= strchr(ddname
, '@');
1320 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1321 while (decrypt
&& err
== EACCES
) {
1322 dsl_crypto_params_t
*dcp
;
1323 nvlist_t
*crypto_args
= fnvlist_alloc();
1325 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1326 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1327 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1328 crypto_args
, &dcp
));
1329 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1330 dsl_crypto_params_free(dcp
, B_FALSE
);
1331 fnvlist_free(crypto_args
);
1333 if (err
== EINVAL
) {
1335 * We couldn't load a key for this dataset so try
1336 * the parent. This loop will eventually hit the
1337 * encryption root since ztest only makes clones
1338 * as children of their origin datasets.
1340 cp
= strrchr(ddname
, '/');
1347 } else if (err
!= 0) {
1351 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1359 ztest_rll_init(rll_t
*rll
)
1361 rll
->rll_writer
= NULL
;
1362 rll
->rll_readers
= 0;
1363 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1364 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1368 ztest_rll_destroy(rll_t
*rll
)
1370 ASSERT(rll
->rll_writer
== NULL
);
1371 ASSERT(rll
->rll_readers
== 0);
1372 mutex_destroy(&rll
->rll_lock
);
1373 cv_destroy(&rll
->rll_cv
);
1377 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1379 mutex_enter(&rll
->rll_lock
);
1381 if (type
== RL_READER
) {
1382 while (rll
->rll_writer
!= NULL
)
1383 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1386 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1387 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1388 rll
->rll_writer
= curthread
;
1391 mutex_exit(&rll
->rll_lock
);
1395 ztest_rll_unlock(rll_t
*rll
)
1397 mutex_enter(&rll
->rll_lock
);
1399 if (rll
->rll_writer
) {
1400 ASSERT(rll
->rll_readers
== 0);
1401 rll
->rll_writer
= NULL
;
1403 ASSERT(rll
->rll_readers
!= 0);
1404 ASSERT(rll
->rll_writer
== NULL
);
1408 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1409 cv_broadcast(&rll
->rll_cv
);
1411 mutex_exit(&rll
->rll_lock
);
1415 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1417 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1419 ztest_rll_lock(rll
, type
);
1423 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1425 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1427 ztest_rll_unlock(rll
);
1431 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1432 uint64_t size
, rl_type_t type
)
1434 uint64_t hash
= object
^ (offset
% (ZTEST_RANGE_LOCKS
+ 1));
1435 rll_t
*rll
= &zd
->zd_range_lock
[hash
& (ZTEST_RANGE_LOCKS
- 1)];
1438 rl
= umem_alloc(sizeof (*rl
), UMEM_NOFAIL
);
1439 rl
->rl_object
= object
;
1440 rl
->rl_offset
= offset
;
1444 ztest_rll_lock(rll
, type
);
1450 ztest_range_unlock(rl_t
*rl
)
1452 rll_t
*rll
= rl
->rl_lock
;
1454 ztest_rll_unlock(rll
);
1456 umem_free(rl
, sizeof (*rl
));
1460 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1463 zd
->zd_zilog
= dmu_objset_zil(os
);
1464 zd
->zd_shared
= szd
;
1465 dmu_objset_name(os
, zd
->zd_name
);
1468 if (zd
->zd_shared
!= NULL
)
1469 zd
->zd_shared
->zd_seq
= 0;
1471 VERIFY0(pthread_rwlock_init(&zd
->zd_zilog_lock
, NULL
));
1472 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1474 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1475 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1477 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1478 ztest_rll_init(&zd
->zd_range_lock
[l
]);
1482 ztest_zd_fini(ztest_ds_t
*zd
)
1486 mutex_destroy(&zd
->zd_dirobj_lock
);
1487 (void) pthread_rwlock_destroy(&zd
->zd_zilog_lock
);
1489 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1490 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1492 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1493 ztest_rll_destroy(&zd
->zd_range_lock
[l
]);
1496 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1499 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1505 * Attempt to assign tx to some transaction group.
1507 error
= dmu_tx_assign(tx
, txg_how
);
1509 if (error
== ERESTART
) {
1510 ASSERT(txg_how
== TXG_NOWAIT
);
1513 ASSERT3U(error
, ==, ENOSPC
);
1514 ztest_record_enospc(tag
);
1519 txg
= dmu_tx_get_txg(tx
);
1525 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1528 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1536 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1539 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1543 diff
|= (value
- *ip
++);
1550 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1551 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1554 bt
->bt_magic
= BT_MAGIC
;
1555 bt
->bt_objset
= dmu_objset_id(os
);
1556 bt
->bt_object
= object
;
1557 bt
->bt_dnodesize
= dnodesize
;
1558 bt
->bt_offset
= offset
;
1561 bt
->bt_crtxg
= crtxg
;
1565 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1566 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1569 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1570 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1571 ASSERT3U(bt
->bt_object
, ==, object
);
1572 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1573 ASSERT3U(bt
->bt_offset
, ==, offset
);
1574 ASSERT3U(bt
->bt_gen
, <=, gen
);
1575 ASSERT3U(bt
->bt_txg
, <=, txg
);
1576 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1579 static ztest_block_tag_t
*
1580 ztest_bt_bonus(dmu_buf_t
*db
)
1582 dmu_object_info_t doi
;
1583 ztest_block_tag_t
*bt
;
1585 dmu_object_info_from_db(db
, &doi
);
1586 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1587 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1588 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1594 * Generate a token to fill up unused bonus buffer space. Try to make
1595 * it unique to the object, generation, and offset to verify that data
1596 * is not getting overwritten by data from other dnodes.
1598 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1599 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1602 * Fill up the unused bonus buffer region before the block tag with a
1603 * verifiable pattern. Filling the whole bonus area with non-zero data
1604 * helps ensure that all dnode traversal code properly skips the
1605 * interior regions of large dnodes.
1608 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1609 objset_t
*os
, uint64_t gen
)
1613 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1615 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1616 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1617 gen
, bonusp
- (uint64_t *)db
->db_data
);
1623 * Verify that the unused area of a bonus buffer is filled with the
1627 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1628 objset_t
*os
, uint64_t gen
)
1632 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1633 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1634 gen
, bonusp
- (uint64_t *)db
->db_data
);
1635 VERIFY3U(*bonusp
, ==, token
);
1643 #define lrz_type lr_mode
1644 #define lrz_blocksize lr_uid
1645 #define lrz_ibshift lr_gid
1646 #define lrz_bonustype lr_rdev
1647 #define lrz_dnodesize lr_crtime[1]
1650 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1652 char *name
= (void *)(lr
+ 1); /* name follows lr */
1653 size_t namesize
= strlen(name
) + 1;
1656 if (zil_replaying(zd
->zd_zilog
, tx
))
1659 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1660 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1661 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1663 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1667 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1669 char *name
= (void *)(lr
+ 1); /* name follows lr */
1670 size_t namesize
= strlen(name
) + 1;
1673 if (zil_replaying(zd
->zd_zilog
, tx
))
1676 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1677 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1678 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1680 itx
->itx_oid
= object
;
1681 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1685 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1688 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1690 if (zil_replaying(zd
->zd_zilog
, tx
))
1693 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1694 write_state
= WR_INDIRECT
;
1696 itx
= zil_itx_create(TX_WRITE
,
1697 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1699 if (write_state
== WR_COPIED
&&
1700 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1701 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1702 zil_itx_destroy(itx
);
1703 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1704 write_state
= WR_NEED_COPY
;
1706 itx
->itx_private
= zd
;
1707 itx
->itx_wr_state
= write_state
;
1708 itx
->itx_sync
= (ztest_random(8) == 0);
1710 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1711 sizeof (*lr
) - sizeof (lr_t
));
1713 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1717 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1721 if (zil_replaying(zd
->zd_zilog
, tx
))
1724 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1725 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1726 sizeof (*lr
) - sizeof (lr_t
));
1728 itx
->itx_sync
= B_FALSE
;
1729 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1733 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1737 if (zil_replaying(zd
->zd_zilog
, tx
))
1740 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1741 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1742 sizeof (*lr
) - sizeof (lr_t
));
1744 itx
->itx_sync
= B_FALSE
;
1745 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1752 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1754 ztest_ds_t
*zd
= arg1
;
1755 lr_create_t
*lr
= arg2
;
1756 char *name
= (void *)(lr
+ 1); /* name follows lr */
1757 objset_t
*os
= zd
->zd_os
;
1758 ztest_block_tag_t
*bbt
;
1766 byteswap_uint64_array(lr
, sizeof (*lr
));
1768 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1769 ASSERT(name
[0] != '\0');
1771 tx
= dmu_tx_create(os
);
1773 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1775 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1776 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1778 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1781 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1785 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1786 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1788 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1789 if (lr
->lr_foid
== 0) {
1790 lr
->lr_foid
= zap_create_dnsize(os
,
1791 lr
->lrz_type
, lr
->lrz_bonustype
,
1792 bonuslen
, lr
->lrz_dnodesize
, tx
);
1794 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1795 lr
->lrz_type
, lr
->lrz_bonustype
,
1796 bonuslen
, lr
->lrz_dnodesize
, tx
);
1799 if (lr
->lr_foid
== 0) {
1800 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1801 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1802 bonuslen
, lr
->lrz_dnodesize
, tx
);
1804 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1805 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1806 bonuslen
, lr
->lrz_dnodesize
, tx
);
1811 ASSERT3U(error
, ==, EEXIST
);
1812 ASSERT(zd
->zd_zilog
->zl_replay
);
1817 ASSERT(lr
->lr_foid
!= 0);
1819 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1820 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1821 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1823 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1824 bbt
= ztest_bt_bonus(db
);
1825 dmu_buf_will_dirty(db
, tx
);
1826 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1827 lr
->lr_gen
, txg
, txg
);
1828 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1829 dmu_buf_rele(db
, FTAG
);
1831 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1834 (void) ztest_log_create(zd
, tx
, lr
);
1842 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1844 ztest_ds_t
*zd
= arg1
;
1845 lr_remove_t
*lr
= arg2
;
1846 char *name
= (void *)(lr
+ 1); /* name follows lr */
1847 objset_t
*os
= zd
->zd_os
;
1848 dmu_object_info_t doi
;
1850 uint64_t object
, txg
;
1853 byteswap_uint64_array(lr
, sizeof (*lr
));
1855 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1856 ASSERT(name
[0] != '\0');
1859 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1860 ASSERT(object
!= 0);
1862 ztest_object_lock(zd
, object
, RL_WRITER
);
1864 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1866 tx
= dmu_tx_create(os
);
1868 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1869 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1871 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1873 ztest_object_unlock(zd
, object
);
1877 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1878 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1880 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1883 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1885 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1889 ztest_object_unlock(zd
, object
);
1895 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1897 ztest_ds_t
*zd
= arg1
;
1898 lr_write_t
*lr
= arg2
;
1899 objset_t
*os
= zd
->zd_os
;
1900 void *data
= lr
+ 1; /* data follows lr */
1901 uint64_t offset
, length
;
1902 ztest_block_tag_t
*bt
= data
;
1903 ztest_block_tag_t
*bbt
;
1904 uint64_t gen
, txg
, lrtxg
, crtxg
;
1905 dmu_object_info_t doi
;
1908 arc_buf_t
*abuf
= NULL
;
1912 byteswap_uint64_array(lr
, sizeof (*lr
));
1914 offset
= lr
->lr_offset
;
1915 length
= lr
->lr_length
;
1917 /* If it's a dmu_sync() block, write the whole block */
1918 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1919 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1920 if (length
< blocksize
) {
1921 offset
-= offset
% blocksize
;
1926 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1927 byteswap_uint64_array(bt
, sizeof (*bt
));
1929 if (bt
->bt_magic
!= BT_MAGIC
)
1932 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1933 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1935 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1937 dmu_object_info_from_db(db
, &doi
);
1939 bbt
= ztest_bt_bonus(db
);
1940 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1942 crtxg
= bbt
->bt_crtxg
;
1943 lrtxg
= lr
->lr_common
.lrc_txg
;
1945 tx
= dmu_tx_create(os
);
1947 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1949 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1950 P2PHASE(offset
, length
) == 0)
1951 abuf
= dmu_request_arcbuf(db
, length
);
1953 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1956 dmu_return_arcbuf(abuf
);
1957 dmu_buf_rele(db
, FTAG
);
1958 ztest_range_unlock(rl
);
1959 ztest_object_unlock(zd
, lr
->lr_foid
);
1965 * Usually, verify the old data before writing new data --
1966 * but not always, because we also want to verify correct
1967 * behavior when the data was not recently read into cache.
1969 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1970 if (ztest_random(4) != 0) {
1971 int prefetch
= ztest_random(2) ?
1972 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1973 ztest_block_tag_t rbt
;
1975 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1976 sizeof (rbt
), &rbt
, prefetch
) == 0);
1977 if (rbt
.bt_magic
== BT_MAGIC
) {
1978 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1979 offset
, gen
, txg
, crtxg
);
1984 * Writes can appear to be newer than the bonus buffer because
1985 * the ztest_get_data() callback does a dmu_read() of the
1986 * open-context data, which may be different than the data
1987 * as it was when the write was generated.
1989 if (zd
->zd_zilog
->zl_replay
) {
1990 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1991 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1996 * Set the bt's gen/txg to the bonus buffer's gen/txg
1997 * so that all of the usual ASSERTs will work.
1999 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
2004 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
2006 bcopy(data
, abuf
->b_data
, length
);
2007 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
2010 (void) ztest_log_write(zd
, tx
, lr
);
2012 dmu_buf_rele(db
, FTAG
);
2016 ztest_range_unlock(rl
);
2017 ztest_object_unlock(zd
, lr
->lr_foid
);
2023 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2025 ztest_ds_t
*zd
= arg1
;
2026 lr_truncate_t
*lr
= arg2
;
2027 objset_t
*os
= zd
->zd_os
;
2033 byteswap_uint64_array(lr
, sizeof (*lr
));
2035 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2036 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2039 tx
= dmu_tx_create(os
);
2041 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2043 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2045 ztest_range_unlock(rl
);
2046 ztest_object_unlock(zd
, lr
->lr_foid
);
2050 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2051 lr
->lr_length
, tx
) == 0);
2053 (void) ztest_log_truncate(zd
, tx
, lr
);
2057 ztest_range_unlock(rl
);
2058 ztest_object_unlock(zd
, lr
->lr_foid
);
2064 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2066 ztest_ds_t
*zd
= arg1
;
2067 lr_setattr_t
*lr
= arg2
;
2068 objset_t
*os
= zd
->zd_os
;
2071 ztest_block_tag_t
*bbt
;
2072 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2075 byteswap_uint64_array(lr
, sizeof (*lr
));
2077 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2079 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2081 tx
= dmu_tx_create(os
);
2082 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2084 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2086 dmu_buf_rele(db
, FTAG
);
2087 ztest_object_unlock(zd
, lr
->lr_foid
);
2091 bbt
= ztest_bt_bonus(db
);
2092 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2093 crtxg
= bbt
->bt_crtxg
;
2094 lrtxg
= lr
->lr_common
.lrc_txg
;
2095 dnodesize
= bbt
->bt_dnodesize
;
2097 if (zd
->zd_zilog
->zl_replay
) {
2098 ASSERT(lr
->lr_size
!= 0);
2099 ASSERT(lr
->lr_mode
!= 0);
2103 * Randomly change the size and increment the generation.
2105 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2107 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2112 * Verify that the current bonus buffer is not newer than our txg.
2114 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2115 MAX(txg
, lrtxg
), crtxg
);
2117 dmu_buf_will_dirty(db
, tx
);
2119 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2120 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2121 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2122 bbt
= ztest_bt_bonus(db
);
2124 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2126 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2127 dmu_buf_rele(db
, FTAG
);
2129 (void) ztest_log_setattr(zd
, tx
, lr
);
2133 ztest_object_unlock(zd
, lr
->lr_foid
);
2138 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2139 NULL
, /* 0 no such transaction type */
2140 ztest_replay_create
, /* TX_CREATE */
2141 NULL
, /* TX_MKDIR */
2142 NULL
, /* TX_MKXATTR */
2143 NULL
, /* TX_SYMLINK */
2144 ztest_replay_remove
, /* TX_REMOVE */
2145 NULL
, /* TX_RMDIR */
2147 NULL
, /* TX_RENAME */
2148 ztest_replay_write
, /* TX_WRITE */
2149 ztest_replay_truncate
, /* TX_TRUNCATE */
2150 ztest_replay_setattr
, /* TX_SETATTR */
2152 NULL
, /* TX_CREATE_ACL */
2153 NULL
, /* TX_CREATE_ATTR */
2154 NULL
, /* TX_CREATE_ACL_ATTR */
2155 NULL
, /* TX_MKDIR_ACL */
2156 NULL
, /* TX_MKDIR_ATTR */
2157 NULL
, /* TX_MKDIR_ACL_ATTR */
2158 NULL
, /* TX_WRITE2 */
2162 * ZIL get_data callbacks
2167 ztest_get_done(zgd_t
*zgd
, int error
)
2169 ztest_ds_t
*zd
= zgd
->zgd_private
;
2170 uint64_t object
= ((rl_t
*)zgd
->zgd_lr
)->rl_object
;
2173 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2175 ztest_range_unlock((rl_t
*)zgd
->zgd_lr
);
2176 ztest_object_unlock(zd
, object
);
2178 umem_free(zgd
, sizeof (*zgd
));
2182 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2185 ztest_ds_t
*zd
= arg
;
2186 objset_t
*os
= zd
->zd_os
;
2187 uint64_t object
= lr
->lr_foid
;
2188 uint64_t offset
= lr
->lr_offset
;
2189 uint64_t size
= lr
->lr_length
;
2190 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2192 dmu_object_info_t doi
;
2197 ASSERT3P(lwb
, !=, NULL
);
2198 ASSERT3P(zio
, !=, NULL
);
2199 ASSERT3U(size
, !=, 0);
2201 ztest_object_lock(zd
, object
, RL_READER
);
2202 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2204 ztest_object_unlock(zd
, object
);
2208 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2210 if (crtxg
== 0 || crtxg
> txg
) {
2211 dmu_buf_rele(db
, FTAG
);
2212 ztest_object_unlock(zd
, object
);
2216 dmu_object_info_from_db(db
, &doi
);
2217 dmu_buf_rele(db
, FTAG
);
2220 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2222 zgd
->zgd_private
= zd
;
2224 if (buf
!= NULL
) { /* immediate write */
2225 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2226 object
, offset
, size
, RL_READER
);
2228 error
= dmu_read(os
, object
, offset
, size
, buf
,
2229 DMU_READ_NO_PREFETCH
);
2232 size
= doi
.doi_data_block_size
;
2234 offset
= P2ALIGN(offset
, size
);
2236 ASSERT(offset
< size
);
2240 zgd
->zgd_lr
= (struct locked_range
*)ztest_range_lock(zd
,
2241 object
, offset
, size
, RL_READER
);
2243 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2244 DMU_READ_NO_PREFETCH
);
2247 blkptr_t
*bp
= &lr
->lr_blkptr
;
2252 ASSERT(db
->db_offset
== offset
);
2253 ASSERT(db
->db_size
== size
);
2255 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2256 ztest_get_done
, zgd
);
2263 ztest_get_done(zgd
, error
);
2269 ztest_lr_alloc(size_t lrsize
, char *name
)
2272 size_t namesize
= name
? strlen(name
) + 1 : 0;
2274 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2277 bcopy(name
, lr
+ lrsize
, namesize
);
2283 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2285 size_t namesize
= name
? strlen(name
) + 1 : 0;
2287 umem_free(lr
, lrsize
+ namesize
);
2291 * Lookup a bunch of objects. Returns the number of objects not found.
2294 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2300 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2302 for (i
= 0; i
< count
; i
++, od
++) {
2304 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2305 sizeof (uint64_t), 1, &od
->od_object
);
2307 ASSERT(error
== ENOENT
);
2308 ASSERT(od
->od_object
== 0);
2312 ztest_block_tag_t
*bbt
;
2313 dmu_object_info_t doi
;
2315 ASSERT(od
->od_object
!= 0);
2316 ASSERT(missing
== 0); /* there should be no gaps */
2318 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2319 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2320 od
->od_object
, FTAG
, &db
));
2321 dmu_object_info_from_db(db
, &doi
);
2322 bbt
= ztest_bt_bonus(db
);
2323 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2324 od
->od_type
= doi
.doi_type
;
2325 od
->od_blocksize
= doi
.doi_data_block_size
;
2326 od
->od_gen
= bbt
->bt_gen
;
2327 dmu_buf_rele(db
, FTAG
);
2328 ztest_object_unlock(zd
, od
->od_object
);
2336 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2341 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2343 for (i
= 0; i
< count
; i
++, od
++) {
2350 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2352 lr
->lr_doid
= od
->od_dir
;
2353 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2354 lr
->lrz_type
= od
->od_crtype
;
2355 lr
->lrz_blocksize
= od
->od_crblocksize
;
2356 lr
->lrz_ibshift
= ztest_random_ibshift();
2357 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2358 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2359 lr
->lr_gen
= od
->od_crgen
;
2360 lr
->lr_crtime
[0] = time(NULL
);
2362 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2363 ASSERT(missing
== 0);
2367 od
->od_object
= lr
->lr_foid
;
2368 od
->od_type
= od
->od_crtype
;
2369 od
->od_blocksize
= od
->od_crblocksize
;
2370 od
->od_gen
= od
->od_crgen
;
2371 ASSERT(od
->od_object
!= 0);
2374 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2381 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2387 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2391 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2398 * No object was found.
2400 if (od
->od_object
== 0)
2403 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2405 lr
->lr_doid
= od
->od_dir
;
2407 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2408 ASSERT3U(error
, ==, ENOSPC
);
2413 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2420 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2426 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2428 lr
->lr_foid
= object
;
2429 lr
->lr_offset
= offset
;
2430 lr
->lr_length
= size
;
2432 BP_ZERO(&lr
->lr_blkptr
);
2434 bcopy(data
, lr
+ 1, size
);
2436 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2438 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2444 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2449 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2451 lr
->lr_foid
= object
;
2452 lr
->lr_offset
= offset
;
2453 lr
->lr_length
= size
;
2455 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2457 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2463 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2468 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2470 lr
->lr_foid
= object
;
2474 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2476 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2482 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2484 objset_t
*os
= zd
->zd_os
;
2489 txg_wait_synced(dmu_objset_pool(os
), 0);
2491 ztest_object_lock(zd
, object
, RL_READER
);
2492 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2494 tx
= dmu_tx_create(os
);
2496 dmu_tx_hold_write(tx
, object
, offset
, size
);
2498 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2501 dmu_prealloc(os
, object
, offset
, size
, tx
);
2503 txg_wait_synced(dmu_objset_pool(os
), txg
);
2505 (void) dmu_free_long_range(os
, object
, offset
, size
);
2508 ztest_range_unlock(rl
);
2509 ztest_object_unlock(zd
, object
);
2513 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2516 ztest_block_tag_t wbt
;
2517 dmu_object_info_t doi
;
2518 enum ztest_io_type io_type
;
2522 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2523 blocksize
= doi
.doi_data_block_size
;
2524 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2527 * Pick an i/o type at random, biased toward writing block tags.
2529 io_type
= ztest_random(ZTEST_IO_TYPES
);
2530 if (ztest_random(2) == 0)
2531 io_type
= ZTEST_IO_WRITE_TAG
;
2533 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2537 case ZTEST_IO_WRITE_TAG
:
2538 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2540 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2543 case ZTEST_IO_WRITE_PATTERN
:
2544 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2545 if (ztest_random(2) == 0) {
2547 * Induce fletcher2 collisions to ensure that
2548 * zio_ddt_collision() detects and resolves them
2549 * when using fletcher2-verify for deduplication.
2551 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2552 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2554 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2557 case ZTEST_IO_WRITE_ZEROES
:
2558 bzero(data
, blocksize
);
2559 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2562 case ZTEST_IO_TRUNCATE
:
2563 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2566 case ZTEST_IO_SETATTR
:
2567 (void) ztest_setattr(zd
, object
);
2572 case ZTEST_IO_REWRITE
:
2573 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2574 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2575 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2577 VERIFY(err
== 0 || err
== ENOSPC
);
2578 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2579 ZFS_PROP_COMPRESSION
,
2580 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2582 VERIFY(err
== 0 || err
== ENOSPC
);
2583 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2585 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2586 DMU_READ_NO_PREFETCH
));
2588 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2592 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2594 umem_free(data
, blocksize
);
2598 * Initialize an object description template.
2601 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2602 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2605 od
->od_dir
= ZTEST_DIROBJ
;
2608 od
->od_crtype
= type
;
2609 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2610 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2613 od
->od_type
= DMU_OT_NONE
;
2614 od
->od_blocksize
= 0;
2617 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2618 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2622 * Lookup or create the objects for a test using the od template.
2623 * If the objects do not all exist, or if 'remove' is specified,
2624 * remove any existing objects and create new ones. Otherwise,
2625 * use the existing objects.
2628 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2630 int count
= size
/ sizeof (*od
);
2633 mutex_enter(&zd
->zd_dirobj_lock
);
2634 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2635 (ztest_remove(zd
, od
, count
) != 0 ||
2636 ztest_create(zd
, od
, count
) != 0))
2639 mutex_exit(&zd
->zd_dirobj_lock
);
2646 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2648 zilog_t
*zilog
= zd
->zd_zilog
;
2650 (void) pthread_rwlock_rdlock(&zd
->zd_zilog_lock
);
2652 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2655 * Remember the committed values in zd, which is in parent/child
2656 * shared memory. If we die, the next iteration of ztest_run()
2657 * will verify that the log really does contain this record.
2659 mutex_enter(&zilog
->zl_lock
);
2660 ASSERT(zd
->zd_shared
!= NULL
);
2661 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2662 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2663 mutex_exit(&zilog
->zl_lock
);
2665 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2669 * This function is designed to simulate the operations that occur during a
2670 * mount/unmount operation. We hold the dataset across these operations in an
2671 * attempt to expose any implicit assumptions about ZIL management.
2675 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2677 objset_t
*os
= zd
->zd_os
;
2680 * We hold the ztest_vdev_lock so we don't cause problems with
2681 * other threads that wish to remove a log device, such as
2682 * ztest_device_removal().
2684 mutex_enter(&ztest_vdev_lock
);
2687 * We grab the zd_dirobj_lock to ensure that no other thread is
2688 * updating the zil (i.e. adding in-memory log records) and the
2689 * zd_zilog_lock to block any I/O.
2691 mutex_enter(&zd
->zd_dirobj_lock
);
2692 (void) pthread_rwlock_wrlock(&zd
->zd_zilog_lock
);
2694 /* zfsvfs_teardown() */
2695 zil_close(zd
->zd_zilog
);
2697 /* zfsvfs_setup() */
2698 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2699 zil_replay(os
, zd
, ztest_replay_vector
);
2701 (void) pthread_rwlock_unlock(&zd
->zd_zilog_lock
);
2702 mutex_exit(&zd
->zd_dirobj_lock
);
2703 mutex_exit(&ztest_vdev_lock
);
2707 * Verify that we can't destroy an active pool, create an existing pool,
2708 * or create a pool with a bad vdev spec.
2712 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2714 ztest_shared_opts_t
*zo
= &ztest_opts
;
2718 if (zo
->zo_mmp_test
)
2722 * Attempt to create using a bad file.
2724 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2725 VERIFY3U(ENOENT
, ==,
2726 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2727 nvlist_free(nvroot
);
2730 * Attempt to create using a bad mirror.
2732 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 2, 1);
2733 VERIFY3U(ENOENT
, ==,
2734 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2735 nvlist_free(nvroot
);
2738 * Attempt to create an existing pool. It shouldn't matter
2739 * what's in the nvroot; we should fail with EEXIST.
2741 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
2742 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, NULL
, 0, 0, 1);
2743 VERIFY3U(EEXIST
, ==,
2744 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2745 nvlist_free(nvroot
);
2746 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2747 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2748 spa_close(spa
, FTAG
);
2750 (void) pthread_rwlock_unlock(&ztest_name_lock
);
2754 * Start and then stop the MMP threads to ensure the startup and shutdown code
2755 * works properly. Actual protection and property-related code tested via ZTS.
2759 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2761 ztest_shared_opts_t
*zo
= &ztest_opts
;
2762 spa_t
*spa
= ztest_spa
;
2764 if (zo
->zo_mmp_test
)
2767 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2768 mutex_enter(&spa
->spa_props_lock
);
2770 zfs_multihost_fail_intervals
= 0;
2772 if (!spa_multihost(spa
)) {
2773 spa
->spa_multihost
= B_TRUE
;
2774 mmp_thread_start(spa
);
2777 mutex_exit(&spa
->spa_props_lock
);
2778 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2780 txg_wait_synced(spa_get_dsl(spa
), 0);
2781 mmp_signal_all_threads();
2782 txg_wait_synced(spa_get_dsl(spa
), 0);
2784 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2785 mutex_enter(&spa
->spa_props_lock
);
2787 if (spa_multihost(spa
)) {
2788 mmp_thread_stop(spa
);
2789 spa
->spa_multihost
= B_FALSE
;
2792 mutex_exit(&spa
->spa_props_lock
);
2793 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2798 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2801 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2802 uint64_t version
, newversion
;
2803 nvlist_t
*nvroot
, *props
;
2806 if (ztest_opts
.zo_mmp_test
)
2809 mutex_enter(&ztest_vdev_lock
);
2810 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2813 * Clean up from previous runs.
2815 (void) spa_destroy(name
);
2817 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2818 NULL
, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2821 * If we're configuring a RAIDZ device then make sure that the
2822 * initial version is capable of supporting that feature.
2824 switch (ztest_opts
.zo_raidz_parity
) {
2827 initial_version
= SPA_VERSION_INITIAL
;
2830 initial_version
= SPA_VERSION_RAIDZ2
;
2833 initial_version
= SPA_VERSION_RAIDZ3
;
2838 * Create a pool with a spa version that can be upgraded. Pick
2839 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2842 version
= ztest_random_spa_version(initial_version
);
2843 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2845 props
= fnvlist_alloc();
2846 fnvlist_add_uint64(props
,
2847 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2848 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2849 fnvlist_free(nvroot
);
2850 fnvlist_free(props
);
2852 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2853 VERIFY3U(spa_version(spa
), ==, version
);
2854 newversion
= ztest_random_spa_version(version
+ 1);
2856 if (ztest_opts
.zo_verbose
>= 4) {
2857 (void) printf("upgrading spa version from %llu to %llu\n",
2858 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2861 spa_upgrade(spa
, newversion
);
2862 VERIFY3U(spa_version(spa
), >, version
);
2863 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2864 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2865 spa_close(spa
, FTAG
);
2868 mutex_exit(&ztest_vdev_lock
);
2872 ztest_spa_checkpoint(spa_t
*spa
)
2874 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2876 int error
= spa_checkpoint(spa
->spa_name
);
2880 case ZFS_ERR_DEVRM_IN_PROGRESS
:
2881 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2882 case ZFS_ERR_CHECKPOINT_EXISTS
:
2885 ztest_record_enospc(FTAG
);
2888 fatal(0, "spa_checkpoint(%s) = %d", spa
->spa_name
, error
);
2893 ztest_spa_discard_checkpoint(spa_t
*spa
)
2895 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock
));
2897 int error
= spa_checkpoint_discard(spa
->spa_name
);
2901 case ZFS_ERR_DISCARDING_CHECKPOINT
:
2902 case ZFS_ERR_NO_CHECKPOINT
:
2905 fatal(0, "spa_discard_checkpoint(%s) = %d",
2906 spa
->spa_name
, error
);
2913 ztest_spa_checkpoint_create_discard(ztest_ds_t
*zd
, uint64_t id
)
2915 spa_t
*spa
= ztest_spa
;
2917 mutex_enter(&ztest_checkpoint_lock
);
2918 if (ztest_random(2) == 0) {
2919 ztest_spa_checkpoint(spa
);
2921 ztest_spa_discard_checkpoint(spa
);
2923 mutex_exit(&ztest_checkpoint_lock
);
2928 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2933 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2936 for (c
= 0; c
< vd
->vdev_children
; c
++)
2937 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2945 * Find the first available hole which can be used as a top-level.
2948 find_vdev_hole(spa_t
*spa
)
2950 vdev_t
*rvd
= spa
->spa_root_vdev
;
2953 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2955 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2956 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2958 if (cvd
->vdev_ishole
)
2965 * Verify that vdev_add() works as expected.
2969 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2971 ztest_shared_t
*zs
= ztest_shared
;
2972 spa_t
*spa
= ztest_spa
;
2978 if (ztest_opts
.zo_mmp_test
)
2981 mutex_enter(&ztest_vdev_lock
);
2982 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2984 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2986 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2989 * If we have slogs then remove them 1/4 of the time.
2991 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2992 metaslab_group_t
*mg
;
2995 * find the first real slog in log allocation class
2997 mg
= spa_log_class(spa
)->mc_rotor
;
2998 while (!mg
->mg_vd
->vdev_islog
)
3001 guid
= mg
->mg_vd
->vdev_guid
;
3003 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3006 * We have to grab the zs_name_lock as writer to
3007 * prevent a race between removing a slog (dmu_objset_find)
3008 * and destroying a dataset. Removing the slog will
3009 * grab a reference on the dataset which may cause
3010 * dsl_destroy_head() to fail with EBUSY thus
3011 * leaving the dataset in an inconsistent state.
3013 pthread_rwlock_wrlock(&ztest_name_lock
);
3014 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3015 pthread_rwlock_unlock(&ztest_name_lock
);
3019 case EEXIST
: /* Generic zil_reset() error */
3020 case EBUSY
: /* Replay required */
3021 case EACCES
: /* Crypto key not loaded */
3022 case ZFS_ERR_CHECKPOINT_EXISTS
:
3023 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3026 fatal(0, "spa_vdev_remove() = %d", error
);
3029 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3032 * Make 1/4 of the devices be log devices
3034 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
3035 ztest_opts
.zo_vdev_size
, 0, (ztest_random(4) == 0) ?
3036 "log" : NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3038 error
= spa_vdev_add(spa
, nvroot
);
3039 nvlist_free(nvroot
);
3045 ztest_record_enospc("spa_vdev_add");
3048 fatal(0, "spa_vdev_add() = %d", error
);
3052 mutex_exit(&ztest_vdev_lock
);
3057 ztest_vdev_class_add(ztest_ds_t
*zd
, uint64_t id
)
3059 ztest_shared_t
*zs
= ztest_shared
;
3060 spa_t
*spa
= ztest_spa
;
3063 const char *class = (ztest_random(2) == 0) ?
3064 VDEV_ALLOC_BIAS_SPECIAL
: VDEV_ALLOC_BIAS_DEDUP
;
3068 * By default add a special vdev 50% of the time
3070 if ((ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_OFF
) ||
3071 (ztest_opts
.zo_special_vdevs
== ZTEST_VDEV_CLASS_RND
&&
3072 ztest_random(2) == 0)) {
3076 mutex_enter(&ztest_vdev_lock
);
3078 /* Only test with mirrors */
3079 if (zs
->zs_mirrors
< 2) {
3080 mutex_exit(&ztest_vdev_lock
);
3084 /* requires feature@allocation_classes */
3085 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_ALLOCATION_CLASSES
)) {
3086 mutex_exit(&ztest_vdev_lock
);
3090 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
3092 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3093 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
3094 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3096 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
3097 class, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
3099 error
= spa_vdev_add(spa
, nvroot
);
3100 nvlist_free(nvroot
);
3102 if (error
== ENOSPC
)
3103 ztest_record_enospc("spa_vdev_add");
3104 else if (error
!= 0)
3105 fatal(0, "spa_vdev_add() = %d", error
);
3108 * 50% of the time allow small blocks in the special class
3111 spa_special_class(spa
)->mc_groups
== 1 && ztest_random(2) == 0) {
3112 if (ztest_opts
.zo_verbose
>= 3)
3113 (void) printf("Enabling special VDEV small blocks\n");
3114 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
,
3115 ZFS_PROP_SPECIAL_SMALL_BLOCKS
, 32768, B_FALSE
);
3118 mutex_exit(&ztest_vdev_lock
);
3120 if (ztest_opts
.zo_verbose
>= 3) {
3121 metaslab_class_t
*mc
;
3123 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3124 mc
= spa_special_class(spa
);
3126 mc
= spa_dedup_class(spa
);
3127 (void) printf("Added a %s mirrored vdev (of %d)\n",
3128 class, (int)mc
->mc_groups
);
3133 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
3137 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
3139 ztest_shared_t
*zs
= ztest_shared
;
3140 spa_t
*spa
= ztest_spa
;
3141 vdev_t
*rvd
= spa
->spa_root_vdev
;
3142 spa_aux_vdev_t
*sav
;
3148 if (ztest_opts
.zo_mmp_test
)
3151 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3153 if (ztest_random(2) == 0) {
3154 sav
= &spa
->spa_spares
;
3155 aux
= ZPOOL_CONFIG_SPARES
;
3157 sav
= &spa
->spa_l2cache
;
3158 aux
= ZPOOL_CONFIG_L2CACHE
;
3161 mutex_enter(&ztest_vdev_lock
);
3163 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3165 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3167 * Pick a random device to remove.
3169 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3172 * Find an unused device we can add.
3174 zs
->zs_vdev_aux
= 0;
3177 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3178 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3180 for (c
= 0; c
< sav
->sav_count
; c
++)
3181 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3184 if (c
== sav
->sav_count
&&
3185 vdev_lookup_by_path(rvd
, path
) == NULL
)
3191 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3197 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3198 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, NULL
, 0, 0, 1);
3199 error
= spa_vdev_add(spa
, nvroot
);
3205 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3207 nvlist_free(nvroot
);
3210 * Remove an existing device. Sometimes, dirty its
3211 * vdev state first to make sure we handle removal
3212 * of devices that have pending state changes.
3214 if (ztest_random(2) == 0)
3215 (void) vdev_online(spa
, guid
, 0, NULL
);
3217 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3222 case ZFS_ERR_CHECKPOINT_EXISTS
:
3223 case ZFS_ERR_DISCARDING_CHECKPOINT
:
3226 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3230 mutex_exit(&ztest_vdev_lock
);
3232 umem_free(path
, MAXPATHLEN
);
3236 * split a pool if it has mirror tlvdevs
3240 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3242 ztest_shared_t
*zs
= ztest_shared
;
3243 spa_t
*spa
= ztest_spa
;
3244 vdev_t
*rvd
= spa
->spa_root_vdev
;
3245 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3246 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3249 if (ztest_opts
.zo_mmp_test
)
3252 mutex_enter(&ztest_vdev_lock
);
3254 /* ensure we have a usable config; mirrors of raidz aren't supported */
3255 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3256 mutex_exit(&ztest_vdev_lock
);
3260 /* clean up the old pool, if any */
3261 (void) spa_destroy("splitp");
3263 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3265 /* generate a config from the existing config */
3266 mutex_enter(&spa
->spa_props_lock
);
3267 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3269 mutex_exit(&spa
->spa_props_lock
);
3271 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3274 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3275 for (c
= 0; c
< children
; c
++) {
3276 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3280 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3281 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3283 VERIFY(nvlist_add_string(schild
[schildren
],
3284 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3285 VERIFY(nvlist_add_uint64(schild
[schildren
],
3286 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3288 lastlogid
= schildren
;
3293 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3294 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3295 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3298 /* OK, create a config that can be used to split */
3299 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3300 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3301 VDEV_TYPE_ROOT
) == 0);
3302 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3303 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3305 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3306 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3308 for (c
= 0; c
< schildren
; c
++)
3309 nvlist_free(schild
[c
]);
3313 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3315 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
3316 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3317 (void) pthread_rwlock_unlock(&ztest_name_lock
);
3319 nvlist_free(config
);
3322 (void) printf("successful split - results:\n");
3323 mutex_enter(&spa_namespace_lock
);
3324 show_pool_stats(spa
);
3325 show_pool_stats(spa_lookup("splitp"));
3326 mutex_exit(&spa_namespace_lock
);
3330 mutex_exit(&ztest_vdev_lock
);
3334 * Verify that we can attach and detach devices.
3338 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3340 ztest_shared_t
*zs
= ztest_shared
;
3341 spa_t
*spa
= ztest_spa
;
3342 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3343 vdev_t
*rvd
= spa
->spa_root_vdev
;
3344 vdev_t
*oldvd
, *newvd
, *pvd
;
3348 uint64_t ashift
= ztest_get_ashift();
3349 uint64_t oldguid
, pguid
;
3350 uint64_t oldsize
, newsize
;
3351 char *oldpath
, *newpath
;
3353 int oldvd_has_siblings
= B_FALSE
;
3354 int newvd_is_spare
= B_FALSE
;
3356 int error
, expected_error
;
3358 if (ztest_opts
.zo_mmp_test
)
3361 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3362 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3364 mutex_enter(&ztest_vdev_lock
);
3365 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3367 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3370 * If a vdev is in the process of being removed, its removal may
3371 * finish while we are in progress, leading to an unexpected error
3372 * value. Don't bother trying to attach while we are in the middle
3375 if (ztest_device_removal_active
) {
3376 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3377 mutex_exit(&ztest_vdev_lock
);
3382 * Decide whether to do an attach or a replace.
3384 replacing
= ztest_random(2);
3387 * Pick a random top-level vdev.
3389 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3392 * Pick a random leaf within it.
3394 leaf
= ztest_random(leaves
);
3399 oldvd
= rvd
->vdev_child
[top
];
3401 /* pick a child from the mirror */
3402 if (zs
->zs_mirrors
>= 1) {
3403 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3404 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3405 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3408 /* pick a child out of the raidz group */
3409 if (ztest_opts
.zo_raidz
> 1) {
3410 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3411 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3412 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3416 * If we're already doing an attach or replace, oldvd may be a
3417 * mirror vdev -- in which case, pick a random child.
3419 while (oldvd
->vdev_children
!= 0) {
3420 oldvd_has_siblings
= B_TRUE
;
3421 ASSERT(oldvd
->vdev_children
>= 2);
3422 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3425 oldguid
= oldvd
->vdev_guid
;
3426 oldsize
= vdev_get_min_asize(oldvd
);
3427 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3428 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3429 pvd
= oldvd
->vdev_parent
;
3430 pguid
= pvd
->vdev_guid
;
3433 * If oldvd has siblings, then half of the time, detach it.
3435 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3436 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3437 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3438 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3439 error
!= ENOTSUP
&& error
!= ZFS_ERR_CHECKPOINT_EXISTS
&&
3440 error
!= ZFS_ERR_DISCARDING_CHECKPOINT
)
3441 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3446 * For the new vdev, choose with equal probability between the two
3447 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3449 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3450 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3451 newvd_is_spare
= B_TRUE
;
3452 (void) strcpy(newpath
, newvd
->vdev_path
);
3454 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3455 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3456 top
* leaves
+ leaf
);
3457 if (ztest_random(2) == 0)
3458 newpath
[strlen(newpath
) - 1] = 'b';
3459 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3464 * Reopen to ensure the vdev's asize field isn't stale.
3467 newsize
= vdev_get_min_asize(newvd
);
3470 * Make newsize a little bigger or smaller than oldsize.
3471 * If it's smaller, the attach should fail.
3472 * If it's larger, and we're doing a replace,
3473 * we should get dynamic LUN growth when we're done.
3475 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3479 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3480 * unless it's a replace; in that case any non-replacing parent is OK.
3482 * If newvd is already part of the pool, it should fail with EBUSY.
3484 * If newvd is too small, it should fail with EOVERFLOW.
3486 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3487 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3488 pvd
->vdev_ops
== &vdev_replacing_ops
||
3489 pvd
->vdev_ops
== &vdev_spare_ops
))
3490 expected_error
= ENOTSUP
;
3491 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3492 expected_error
= ENOTSUP
;
3493 else if (newvd
== oldvd
)
3494 expected_error
= replacing
? 0 : EBUSY
;
3495 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3496 expected_error
= EBUSY
;
3497 else if (newsize
< oldsize
)
3498 expected_error
= EOVERFLOW
;
3499 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3500 expected_error
= EDOM
;
3504 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3507 * Build the nvlist describing newpath.
3509 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3510 ashift
, NULL
, 0, 0, 1);
3512 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3517 * If our parent was the replacing vdev, but the replace completed,
3518 * then instead of failing with ENOTSUP we may either succeed,
3519 * fail with ENODEV, or fail with EOVERFLOW.
3521 if (expected_error
== ENOTSUP
&&
3522 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3523 expected_error
= error
;
3526 * If someone grew the LUN, the replacement may be too small.
3528 if (error
== EOVERFLOW
|| error
== EBUSY
)
3529 expected_error
= error
;
3531 if (error
== ZFS_ERR_CHECKPOINT_EXISTS
||
3532 error
== ZFS_ERR_DISCARDING_CHECKPOINT
)
3533 expected_error
= error
;
3535 /* XXX workaround 6690467 */
3536 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3537 fatal(0, "attach (%s %llu, %s %llu, %d) "
3538 "returned %d, expected %d",
3539 oldpath
, oldsize
, newpath
,
3540 newsize
, replacing
, error
, expected_error
);
3543 mutex_exit(&ztest_vdev_lock
);
3545 umem_free(oldpath
, MAXPATHLEN
);
3546 umem_free(newpath
, MAXPATHLEN
);
3551 ztest_device_removal(ztest_ds_t
*zd
, uint64_t id
)
3553 spa_t
*spa
= ztest_spa
;
3558 mutex_enter(&ztest_vdev_lock
);
3560 if (ztest_device_removal_active
) {
3561 mutex_exit(&ztest_vdev_lock
);
3566 * Remove a random top-level vdev and wait for removal to finish.
3568 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3569 vd
= vdev_lookup_top(spa
, ztest_random_vdev_top(spa
, B_FALSE
));
3570 guid
= vd
->vdev_guid
;
3571 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3573 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3575 ztest_device_removal_active
= B_TRUE
;
3576 mutex_exit(&ztest_vdev_lock
);
3579 * spa->spa_vdev_removal is created in a sync task that
3580 * is initiated via dsl_sync_task_nowait(). Since the
3581 * task may not run before spa_vdev_remove() returns, we
3582 * must wait at least 1 txg to ensure that the removal
3583 * struct has been created.
3585 txg_wait_synced(spa_get_dsl(spa
), 0);
3587 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
3588 txg_wait_synced(spa_get_dsl(spa
), 0);
3590 mutex_exit(&ztest_vdev_lock
);
3595 * The pool needs to be scrubbed after completing device removal.
3596 * Failure to do so may result in checksum errors due to the
3597 * strategy employed by ztest_fault_inject() when selecting which
3598 * offset are redundant and can be damaged.
3600 error
= spa_scan(spa
, POOL_SCAN_SCRUB
);
3602 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
3603 txg_wait_synced(spa_get_dsl(spa
), 0);
3606 mutex_enter(&ztest_vdev_lock
);
3607 ztest_device_removal_active
= B_FALSE
;
3608 mutex_exit(&ztest_vdev_lock
);
3612 * Callback function which expands the physical size of the vdev.
3615 grow_vdev(vdev_t
*vd
, void *arg
)
3617 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3618 size_t *newsize
= arg
;
3622 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3623 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3625 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3628 fsize
= lseek(fd
, 0, SEEK_END
);
3629 VERIFY(ftruncate(fd
, *newsize
) == 0);
3631 if (ztest_opts
.zo_verbose
>= 6) {
3632 (void) printf("%s grew from %lu to %lu bytes\n",
3633 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3640 * Callback function which expands a given vdev by calling vdev_online().
3644 online_vdev(vdev_t
*vd
, void *arg
)
3646 spa_t
*spa
= vd
->vdev_spa
;
3647 vdev_t
*tvd
= vd
->vdev_top
;
3648 uint64_t guid
= vd
->vdev_guid
;
3649 uint64_t generation
= spa
->spa_config_generation
+ 1;
3650 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3653 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3654 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3656 /* Calling vdev_online will initialize the new metaslabs */
3657 spa_config_exit(spa
, SCL_STATE
, spa
);
3658 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3659 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3662 * If vdev_online returned an error or the underlying vdev_open
3663 * failed then we abort the expand. The only way to know that
3664 * vdev_open fails is by checking the returned newstate.
3666 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3667 if (ztest_opts
.zo_verbose
>= 5) {
3668 (void) printf("Unable to expand vdev, state %llu, "
3669 "error %d\n", (u_longlong_t
)newstate
, error
);
3673 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3676 * Since we dropped the lock we need to ensure that we're
3677 * still talking to the original vdev. It's possible this
3678 * vdev may have been detached/replaced while we were
3679 * trying to online it.
3681 if (generation
!= spa
->spa_config_generation
) {
3682 if (ztest_opts
.zo_verbose
>= 5) {
3683 (void) printf("vdev configuration has changed, "
3684 "guid %llu, state %llu, expected gen %llu, "
3687 (u_longlong_t
)tvd
->vdev_state
,
3688 (u_longlong_t
)generation
,
3689 (u_longlong_t
)spa
->spa_config_generation
);
3697 * Traverse the vdev tree calling the supplied function.
3698 * We continue to walk the tree until we either have walked all
3699 * children or we receive a non-NULL return from the callback.
3700 * If a NULL callback is passed, then we just return back the first
3701 * leaf vdev we encounter.
3704 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3708 if (vd
->vdev_ops
->vdev_op_leaf
) {
3712 return (func(vd
, arg
));
3715 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3716 vdev_t
*cvd
= vd
->vdev_child
[c
];
3717 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3724 * Verify that dynamic LUN growth works as expected.
3728 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3730 spa_t
*spa
= ztest_spa
;
3732 metaslab_class_t
*mc
;
3733 metaslab_group_t
*mg
;
3734 size_t psize
, newsize
;
3736 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3738 mutex_enter(&ztest_checkpoint_lock
);
3739 mutex_enter(&ztest_vdev_lock
);
3740 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3743 * If there is a vdev removal in progress, it could complete while
3744 * we are running, in which case we would not be able to verify
3745 * that the metaslab_class space increased (because it decreases
3746 * when the device removal completes).
3748 if (ztest_device_removal_active
) {
3749 spa_config_exit(spa
, SCL_STATE
, spa
);
3750 mutex_exit(&ztest_vdev_lock
);
3751 mutex_exit(&ztest_checkpoint_lock
);
3755 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3757 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3760 old_ms_count
= tvd
->vdev_ms_count
;
3761 old_class_space
= metaslab_class_get_space(mc
);
3764 * Determine the size of the first leaf vdev associated with
3765 * our top-level device.
3767 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3768 ASSERT3P(vd
, !=, NULL
);
3769 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3771 psize
= vd
->vdev_psize
;
3774 * We only try to expand the vdev if it's healthy, less than 4x its
3775 * original size, and it has a valid psize.
3777 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3778 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3779 spa_config_exit(spa
, SCL_STATE
, spa
);
3780 mutex_exit(&ztest_vdev_lock
);
3781 mutex_exit(&ztest_checkpoint_lock
);
3785 newsize
= psize
+ MAX(psize
/ 8, SPA_MAXBLOCKSIZE
);
3786 ASSERT3U(newsize
, >, psize
);
3788 if (ztest_opts
.zo_verbose
>= 6) {
3789 (void) printf("Expanding LUN %s from %lu to %lu\n",
3790 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3794 * Growing the vdev is a two step process:
3795 * 1). expand the physical size (i.e. relabel)
3796 * 2). online the vdev to create the new metaslabs
3798 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3799 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3800 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3801 if (ztest_opts
.zo_verbose
>= 5) {
3802 (void) printf("Could not expand LUN because "
3803 "the vdev configuration changed.\n");
3805 spa_config_exit(spa
, SCL_STATE
, spa
);
3806 mutex_exit(&ztest_vdev_lock
);
3807 mutex_exit(&ztest_checkpoint_lock
);
3811 spa_config_exit(spa
, SCL_STATE
, spa
);
3814 * Expanding the LUN will update the config asynchronously,
3815 * thus we must wait for the async thread to complete any
3816 * pending tasks before proceeding.
3820 mutex_enter(&spa
->spa_async_lock
);
3821 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3822 mutex_exit(&spa
->spa_async_lock
);
3825 txg_wait_synced(spa_get_dsl(spa
), 0);
3826 (void) poll(NULL
, 0, 100);
3829 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3831 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3832 new_ms_count
= tvd
->vdev_ms_count
;
3833 new_class_space
= metaslab_class_get_space(mc
);
3835 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3836 if (ztest_opts
.zo_verbose
>= 5) {
3837 (void) printf("Could not verify LUN expansion due to "
3838 "intervening vdev offline or remove.\n");
3840 spa_config_exit(spa
, SCL_STATE
, spa
);
3841 mutex_exit(&ztest_vdev_lock
);
3842 mutex_exit(&ztest_checkpoint_lock
);
3847 * Make sure we were able to grow the vdev.
3849 if (new_ms_count
<= old_ms_count
) {
3850 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3851 old_ms_count
, new_ms_count
);
3855 * Make sure we were able to grow the pool.
3857 if (new_class_space
<= old_class_space
) {
3858 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3859 old_class_space
, new_class_space
);
3862 if (ztest_opts
.zo_verbose
>= 5) {
3863 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3865 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3866 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3867 (void) printf("%s grew from %s to %s\n",
3868 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3871 spa_config_exit(spa
, SCL_STATE
, spa
);
3872 mutex_exit(&ztest_vdev_lock
);
3873 mutex_exit(&ztest_checkpoint_lock
);
3877 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3881 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3884 * Create the objects common to all ztest datasets.
3886 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3887 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3891 ztest_dataset_create(char *dsname
)
3895 dsl_crypto_params_t
*dcp
= NULL
;
3898 * 50% of the time, we create encrypted datasets
3899 * using a random cipher suite and a hard-coded
3902 rand
= ztest_random(2);
3904 nvlist_t
*crypto_args
= fnvlist_alloc();
3905 nvlist_t
*props
= fnvlist_alloc();
3907 /* slight bias towards the default cipher suite */
3908 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3909 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3910 rand
= ZIO_CRYPT_ON
;
3912 fnvlist_add_uint64(props
,
3913 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3914 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3915 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3918 * These parameters aren't really used by the kernel. They
3919 * are simply stored so that userspace knows how to load
3922 fnvlist_add_uint64(props
,
3923 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3924 fnvlist_add_string(props
,
3925 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3926 fnvlist_add_uint64(props
,
3927 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3928 fnvlist_add_uint64(props
,
3929 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3931 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3932 crypto_args
, &dcp
));
3935 * Cycle through all available encryption implementations
3936 * to verify interoperability.
3938 VERIFY0(gcm_impl_set("cycle"));
3939 VERIFY0(aes_impl_set("cycle"));
3941 fnvlist_free(crypto_args
);
3942 fnvlist_free(props
);
3945 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
3946 ztest_objset_create_cb
, NULL
);
3947 dsl_crypto_params_free(dcp
, !!err
);
3949 rand
= ztest_random(100);
3950 if (err
|| rand
< 80)
3953 if (ztest_opts
.zo_verbose
>= 5)
3954 (void) printf("Setting dataset %s to sync always\n", dsname
);
3955 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3956 ZFS_SYNC_ALWAYS
, B_FALSE
));
3961 ztest_objset_destroy_cb(const char *name
, void *arg
)
3964 dmu_object_info_t doi
;
3968 * Verify that the dataset contains a directory object.
3970 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3971 B_TRUE
, FTAG
, &os
));
3972 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3973 if (error
!= ENOENT
) {
3974 /* We could have crashed in the middle of destroying it */
3976 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3977 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3979 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3982 * Destroy the dataset.
3984 if (strchr(name
, '@') != NULL
) {
3985 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3987 error
= dsl_destroy_head(name
);
3988 if (error
== ENOSPC
) {
3989 /* There could be checkpoint or insufficient slop */
3990 ztest_record_enospc(FTAG
);
3991 } else if (error
!= EBUSY
) {
3992 /* There could be a hold on this dataset */
4000 ztest_snapshot_create(char *osname
, uint64_t id
)
4002 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4005 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
4007 error
= dmu_objset_snapshot_one(osname
, snapname
);
4008 if (error
== ENOSPC
) {
4009 ztest_record_enospc(FTAG
);
4012 if (error
!= 0 && error
!= EEXIST
) {
4013 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
4020 ztest_snapshot_destroy(char *osname
, uint64_t id
)
4022 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4025 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
4028 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
4029 if (error
!= 0 && error
!= ENOENT
)
4030 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
4036 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4042 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4046 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
4048 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4050 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
4051 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
4054 * If this dataset exists from a previous run, process its replay log
4055 * half of the time. If we don't replay it, then dsl_destroy_head()
4056 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
4058 if (ztest_random(2) == 0 &&
4059 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
4060 B_TRUE
, FTAG
, &os
) == 0) {
4061 ztest_zd_init(zdtmp
, NULL
, os
);
4062 zil_replay(os
, zdtmp
, ztest_replay_vector
);
4063 ztest_zd_fini(zdtmp
);
4064 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4068 * There may be an old instance of the dataset we're about to
4069 * create lying around from a previous run. If so, destroy it
4070 * and all of its snapshots.
4072 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
4073 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
4076 * Verify that the destroyed dataset is no longer in the namespace.
4078 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
4079 B_TRUE
, FTAG
, &os
));
4082 * Verify that we can create a new dataset.
4084 error
= ztest_dataset_create(name
);
4086 if (error
== ENOSPC
) {
4087 ztest_record_enospc(FTAG
);
4090 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
4093 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
4096 ztest_zd_init(zdtmp
, NULL
, os
);
4099 * Open the intent log for it.
4101 zilog
= zil_open(os
, ztest_get_data
);
4104 * Put some objects in there, do a little I/O to them,
4105 * and randomly take a couple of snapshots along the way.
4107 iters
= ztest_random(5);
4108 for (i
= 0; i
< iters
; i
++) {
4109 ztest_dmu_object_alloc_free(zdtmp
, id
);
4110 if (ztest_random(iters
) == 0)
4111 (void) ztest_snapshot_create(name
, i
);
4115 * Verify that we cannot create an existing dataset.
4117 VERIFY3U(EEXIST
, ==,
4118 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
4121 * Verify that we can hold an objset that is also owned.
4123 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
4124 dmu_objset_rele(os2
, FTAG
);
4127 * Verify that we cannot own an objset that is already owned.
4129 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
4130 B_FALSE
, B_TRUE
, FTAG
, &os2
));
4133 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4134 ztest_zd_fini(zdtmp
);
4136 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4138 umem_free(zdtmp
, sizeof (ztest_ds_t
));
4142 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
4145 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
4147 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4148 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
4149 (void) ztest_snapshot_create(zd
->zd_name
, id
);
4150 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4154 * Cleanup non-standard snapshots and clones.
4157 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
4166 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4167 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4168 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4169 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4170 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4172 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4173 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4174 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4175 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4176 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4177 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4178 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4179 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4180 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4181 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4183 error
= dsl_destroy_head(clone2name
);
4184 if (error
&& error
!= ENOENT
)
4185 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
4186 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
4187 if (error
&& error
!= ENOENT
)
4188 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
4189 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
4190 if (error
&& error
!= ENOENT
)
4191 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
4192 error
= dsl_destroy_head(clone1name
);
4193 if (error
&& error
!= ENOENT
)
4194 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
4195 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
4196 if (error
&& error
!= ENOENT
)
4197 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
4199 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4200 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4201 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4202 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4203 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4207 * Verify dsl_dataset_promote handles EBUSY
4210 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
4218 char *osname
= zd
->zd_name
;
4221 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4222 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4223 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4224 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4225 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
4227 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
4229 ztest_dsl_dataset_cleanup(osname
, id
);
4231 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
4232 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
4233 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
4234 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
4235 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
4236 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
4237 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
4238 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
4239 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
4240 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
4242 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
4243 if (error
&& error
!= EEXIST
) {
4244 if (error
== ENOSPC
) {
4245 ztest_record_enospc(FTAG
);
4248 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
4251 error
= dmu_objset_clone(clone1name
, snap1name
);
4253 if (error
== ENOSPC
) {
4254 ztest_record_enospc(FTAG
);
4257 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
4260 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
4261 if (error
&& error
!= EEXIST
) {
4262 if (error
== ENOSPC
) {
4263 ztest_record_enospc(FTAG
);
4266 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
4269 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
4270 if (error
&& error
!= EEXIST
) {
4271 if (error
== ENOSPC
) {
4272 ztest_record_enospc(FTAG
);
4275 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
4278 error
= dmu_objset_clone(clone2name
, snap3name
);
4280 if (error
== ENOSPC
) {
4281 ztest_record_enospc(FTAG
);
4284 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
4287 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4290 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4291 error
= dsl_dataset_promote(clone2name
, NULL
);
4292 if (error
== ENOSPC
) {
4293 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4294 ztest_record_enospc(FTAG
);
4298 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4300 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4303 ztest_dsl_dataset_cleanup(osname
, id
);
4305 (void) pthread_rwlock_unlock(&ztest_name_lock
);
4307 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4308 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4309 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4310 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4311 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4314 #undef OD_ARRAY_SIZE
4315 #define OD_ARRAY_SIZE 4
4318 * Verify that dmu_object_{alloc,free} work as expected.
4321 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4328 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4329 od
= umem_alloc(size
, UMEM_NOFAIL
);
4330 batchsize
= OD_ARRAY_SIZE
;
4332 for (b
= 0; b
< batchsize
; b
++)
4333 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4337 * Destroy the previous batch of objects, create a new batch,
4338 * and do some I/O on the new objects.
4340 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4343 while (ztest_random(4 * batchsize
) != 0)
4344 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4345 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4347 umem_free(od
, size
);
4351 * Rewind the global allocator to verify object allocation backfilling.
4354 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4356 objset_t
*os
= zd
->zd_os
;
4357 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4361 * Rewind the global allocator randomly back to a lower object number
4362 * to force backfilling and reclamation of recently freed dnodes.
4364 mutex_enter(&os
->os_obj_lock
);
4365 object
= ztest_random(os
->os_obj_next_chunk
);
4366 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4367 mutex_exit(&os
->os_obj_lock
);
4370 #undef OD_ARRAY_SIZE
4371 #define OD_ARRAY_SIZE 2
4374 * Verify that dmu_{read,write} work as expected.
4377 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4382 objset_t
*os
= zd
->zd_os
;
4383 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4384 od
= umem_alloc(size
, UMEM_NOFAIL
);
4386 int i
, freeit
, error
;
4388 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4389 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4390 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4391 uint64_t regions
= 997;
4392 uint64_t stride
= 123456789ULL;
4393 uint64_t width
= 40;
4394 int free_percent
= 5;
4397 * This test uses two objects, packobj and bigobj, that are always
4398 * updated together (i.e. in the same tx) so that their contents are
4399 * in sync and can be compared. Their contents relate to each other
4400 * in a simple way: packobj is a dense array of 'bufwad' structures,
4401 * while bigobj is a sparse array of the same bufwads. Specifically,
4402 * for any index n, there are three bufwads that should be identical:
4404 * packobj, at offset n * sizeof (bufwad_t)
4405 * bigobj, at the head of the nth chunk
4406 * bigobj, at the tail of the nth chunk
4408 * The chunk size is arbitrary. It doesn't have to be a power of two,
4409 * and it doesn't have any relation to the object blocksize.
4410 * The only requirement is that it can hold at least two bufwads.
4412 * Normally, we write the bufwad to each of these locations.
4413 * However, free_percent of the time we instead write zeroes to
4414 * packobj and perform a dmu_free_range() on bigobj. By comparing
4415 * bigobj to packobj, we can verify that the DMU is correctly
4416 * tracking which parts of an object are allocated and free,
4417 * and that the contents of the allocated blocks are correct.
4421 * Read the directory info. If it's the first time, set things up.
4423 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4424 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4427 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4428 umem_free(od
, size
);
4432 bigobj
= od
[0].od_object
;
4433 packobj
= od
[1].od_object
;
4434 chunksize
= od
[0].od_gen
;
4435 ASSERT(chunksize
== od
[1].od_gen
);
4438 * Prefetch a random chunk of the big object.
4439 * Our aim here is to get some async reads in flight
4440 * for blocks that we may free below; the DMU should
4441 * handle this race correctly.
4443 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4444 s
= 1 + ztest_random(2 * width
- 1);
4445 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4446 ZIO_PRIORITY_SYNC_READ
);
4449 * Pick a random index and compute the offsets into packobj and bigobj.
4451 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4452 s
= 1 + ztest_random(width
- 1);
4454 packoff
= n
* sizeof (bufwad_t
);
4455 packsize
= s
* sizeof (bufwad_t
);
4457 bigoff
= n
* chunksize
;
4458 bigsize
= s
* chunksize
;
4460 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4461 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4464 * free_percent of the time, free a range of bigobj rather than
4467 freeit
= (ztest_random(100) < free_percent
);
4470 * Read the current contents of our objects.
4472 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4475 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4480 * Get a tx for the mods to both packobj and bigobj.
4482 tx
= dmu_tx_create(os
);
4484 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4487 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4489 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4491 /* This accounts for setting the checksum/compression. */
4492 dmu_tx_hold_bonus(tx
, bigobj
);
4494 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4496 umem_free(packbuf
, packsize
);
4497 umem_free(bigbuf
, bigsize
);
4498 umem_free(od
, size
);
4502 enum zio_checksum cksum
;
4504 cksum
= (enum zio_checksum
)
4505 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4506 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4507 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4509 enum zio_compress comp
;
4511 comp
= (enum zio_compress
)
4512 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4513 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4514 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4517 * For each index from n to n + s, verify that the existing bufwad
4518 * in packobj matches the bufwads at the head and tail of the
4519 * corresponding chunk in bigobj. Then update all three bufwads
4520 * with the new values we want to write out.
4522 for (i
= 0; i
< s
; i
++) {
4524 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4526 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4528 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4530 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4531 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4533 if (pack
->bw_txg
> txg
)
4534 fatal(0, "future leak: got %llx, open txg is %llx",
4537 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4538 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4539 pack
->bw_index
, n
, i
);
4541 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4542 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4544 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4545 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4548 bzero(pack
, sizeof (bufwad_t
));
4550 pack
->bw_index
= n
+ i
;
4552 pack
->bw_data
= 1 + ztest_random(-2ULL);
4559 * We've verified all the old bufwads, and made new ones.
4560 * Now write them out.
4562 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4565 if (ztest_opts
.zo_verbose
>= 7) {
4566 (void) printf("freeing offset %llx size %llx"
4568 (u_longlong_t
)bigoff
,
4569 (u_longlong_t
)bigsize
,
4572 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4574 if (ztest_opts
.zo_verbose
>= 7) {
4575 (void) printf("writing offset %llx size %llx"
4577 (u_longlong_t
)bigoff
,
4578 (u_longlong_t
)bigsize
,
4581 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4587 * Sanity check the stuff we just wrote.
4590 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4591 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4593 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4594 packsize
, packcheck
, DMU_READ_PREFETCH
));
4595 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4596 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4598 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4599 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4601 umem_free(packcheck
, packsize
);
4602 umem_free(bigcheck
, bigsize
);
4605 umem_free(packbuf
, packsize
);
4606 umem_free(bigbuf
, bigsize
);
4607 umem_free(od
, size
);
4611 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4612 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4620 * For each index from n to n + s, verify that the existing bufwad
4621 * in packobj matches the bufwads at the head and tail of the
4622 * corresponding chunk in bigobj. Then update all three bufwads
4623 * with the new values we want to write out.
4625 for (i
= 0; i
< s
; i
++) {
4627 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4629 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4631 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4633 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4634 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4636 if (pack
->bw_txg
> txg
)
4637 fatal(0, "future leak: got %llx, open txg is %llx",
4640 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4641 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4642 pack
->bw_index
, n
, i
);
4644 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4645 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4647 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4648 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4650 pack
->bw_index
= n
+ i
;
4652 pack
->bw_data
= 1 + ztest_random(-2ULL);
4659 #undef OD_ARRAY_SIZE
4660 #define OD_ARRAY_SIZE 2
4663 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4665 objset_t
*os
= zd
->zd_os
;
4672 bufwad_t
*packbuf
, *bigbuf
;
4673 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4674 uint64_t blocksize
= ztest_random_blocksize();
4675 uint64_t chunksize
= blocksize
;
4676 uint64_t regions
= 997;
4677 uint64_t stride
= 123456789ULL;
4679 dmu_buf_t
*bonus_db
;
4680 arc_buf_t
**bigbuf_arcbufs
;
4681 dmu_object_info_t doi
;
4683 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4684 od
= umem_alloc(size
, UMEM_NOFAIL
);
4687 * This test uses two objects, packobj and bigobj, that are always
4688 * updated together (i.e. in the same tx) so that their contents are
4689 * in sync and can be compared. Their contents relate to each other
4690 * in a simple way: packobj is a dense array of 'bufwad' structures,
4691 * while bigobj is a sparse array of the same bufwads. Specifically,
4692 * for any index n, there are three bufwads that should be identical:
4694 * packobj, at offset n * sizeof (bufwad_t)
4695 * bigobj, at the head of the nth chunk
4696 * bigobj, at the tail of the nth chunk
4698 * The chunk size is set equal to bigobj block size so that
4699 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4703 * Read the directory info. If it's the first time, set things up.
4705 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4706 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4710 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4711 umem_free(od
, size
);
4715 bigobj
= od
[0].od_object
;
4716 packobj
= od
[1].od_object
;
4717 blocksize
= od
[0].od_blocksize
;
4718 chunksize
= blocksize
;
4719 ASSERT(chunksize
== od
[1].od_gen
);
4721 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4722 VERIFY(ISP2(doi
.doi_data_block_size
));
4723 VERIFY(chunksize
== doi
.doi_data_block_size
);
4724 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4727 * Pick a random index and compute the offsets into packobj and bigobj.
4729 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4730 s
= 1 + ztest_random(width
- 1);
4732 packoff
= n
* sizeof (bufwad_t
);
4733 packsize
= s
* sizeof (bufwad_t
);
4735 bigoff
= n
* chunksize
;
4736 bigsize
= s
* chunksize
;
4738 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4739 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4741 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4743 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4746 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4747 * Iteration 1 test zcopy to already referenced dbufs.
4748 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4749 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4750 * Iteration 4 test zcopy when dbuf is no longer dirty.
4751 * Iteration 5 test zcopy when it can't be done.
4752 * Iteration 6 one more zcopy write.
4754 for (i
= 0; i
< 7; i
++) {
4759 * In iteration 5 (i == 5) use arcbufs
4760 * that don't match bigobj blksz to test
4761 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4762 * assign an arcbuf to a dbuf.
4764 for (j
= 0; j
< s
; j
++) {
4765 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4767 dmu_request_arcbuf(bonus_db
, chunksize
);
4769 bigbuf_arcbufs
[2 * j
] =
4770 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4771 bigbuf_arcbufs
[2 * j
+ 1] =
4772 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4777 * Get a tx for the mods to both packobj and bigobj.
4779 tx
= dmu_tx_create(os
);
4781 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4782 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4784 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4786 umem_free(packbuf
, packsize
);
4787 umem_free(bigbuf
, bigsize
);
4788 for (j
= 0; j
< s
; j
++) {
4790 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4791 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4794 bigbuf_arcbufs
[2 * j
]);
4796 bigbuf_arcbufs
[2 * j
+ 1]);
4799 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4800 umem_free(od
, size
);
4801 dmu_buf_rele(bonus_db
, FTAG
);
4806 * 50% of the time don't read objects in the 1st iteration to
4807 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4808 * no existing dbufs for the specified offsets.
4810 if (i
!= 0 || ztest_random(2) != 0) {
4811 error
= dmu_read(os
, packobj
, packoff
,
4812 packsize
, packbuf
, DMU_READ_PREFETCH
);
4814 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4815 bigbuf
, DMU_READ_PREFETCH
);
4818 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4822 * We've verified all the old bufwads, and made new ones.
4823 * Now write them out.
4825 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4826 if (ztest_opts
.zo_verbose
>= 7) {
4827 (void) printf("writing offset %llx size %llx"
4829 (u_longlong_t
)bigoff
,
4830 (u_longlong_t
)bigsize
,
4833 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4835 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4836 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4837 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4839 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4840 bigbuf_arcbufs
[2 * j
]->b_data
,
4842 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4844 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4849 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4850 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4852 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4853 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4854 bigbuf_arcbufs
[j
], tx
);
4856 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4857 bigbuf_arcbufs
[2 * j
], tx
);
4858 dmu_assign_arcbuf_by_dbuf(bonus_db
,
4859 off
+ chunksize
/ 2,
4860 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4863 dmu_buf_rele(dbt
, FTAG
);
4869 * Sanity check the stuff we just wrote.
4872 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4873 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4875 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4876 packsize
, packcheck
, DMU_READ_PREFETCH
));
4877 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4878 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4880 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4881 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4883 umem_free(packcheck
, packsize
);
4884 umem_free(bigcheck
, bigsize
);
4887 txg_wait_open(dmu_objset_pool(os
), 0);
4888 } else if (i
== 3) {
4889 txg_wait_synced(dmu_objset_pool(os
), 0);
4893 dmu_buf_rele(bonus_db
, FTAG
);
4894 umem_free(packbuf
, packsize
);
4895 umem_free(bigbuf
, bigsize
);
4896 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4897 umem_free(od
, size
);
4902 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4906 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4907 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4908 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4911 * Have multiple threads write to large offsets in an object
4912 * to verify that parallel writes to an object -- even to the
4913 * same blocks within the object -- doesn't cause any trouble.
4915 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4917 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4920 while (ztest_random(10) != 0)
4921 ztest_io(zd
, od
->od_object
, offset
);
4923 umem_free(od
, sizeof (ztest_od_t
));
4927 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4930 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4931 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4932 uint64_t count
= ztest_random(20) + 1;
4933 uint64_t blocksize
= ztest_random_blocksize();
4936 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4938 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4940 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4941 !ztest_random(2)) != 0) {
4942 umem_free(od
, sizeof (ztest_od_t
));
4946 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4947 umem_free(od
, sizeof (ztest_od_t
));
4951 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4953 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4955 while (ztest_random(count
) != 0) {
4956 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4957 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4960 while (ztest_random(4) != 0)
4961 ztest_io(zd
, od
->od_object
, randoff
);
4964 umem_free(data
, blocksize
);
4965 umem_free(od
, sizeof (ztest_od_t
));
4969 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4971 #define ZTEST_ZAP_MIN_INTS 1
4972 #define ZTEST_ZAP_MAX_INTS 4
4973 #define ZTEST_ZAP_MAX_PROPS 1000
4976 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4978 objset_t
*os
= zd
->zd_os
;
4981 uint64_t txg
, last_txg
;
4982 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4983 uint64_t zl_ints
, zl_intsize
, prop
;
4986 char propname
[100], txgname
[100];
4988 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4990 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4991 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4993 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4994 !ztest_random(2)) != 0)
4997 object
= od
->od_object
;
5000 * Generate a known hash collision, and verify that
5001 * we can lookup and remove both entries.
5003 tx
= dmu_tx_create(os
);
5004 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5005 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5008 for (i
= 0; i
< 2; i
++) {
5010 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
5013 for (i
= 0; i
< 2; i
++) {
5014 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
5015 sizeof (uint64_t), 1, &value
[i
], tx
));
5017 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
5018 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5019 ASSERT3U(zl_ints
, ==, 1);
5021 for (i
= 0; i
< 2; i
++) {
5022 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
5027 * Generate a bunch of random entries.
5029 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
5031 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5032 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5033 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5034 bzero(value
, sizeof (value
));
5038 * If these zap entries already exist, validate their contents.
5040 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5042 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5043 ASSERT3U(zl_ints
, ==, 1);
5045 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
5046 zl_ints
, &last_txg
) == 0);
5048 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
5051 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
5052 ASSERT3U(zl_ints
, ==, ints
);
5054 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
5055 zl_ints
, value
) == 0);
5057 for (i
= 0; i
< ints
; i
++) {
5058 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
5061 ASSERT3U(error
, ==, ENOENT
);
5065 * Atomically update two entries in our zap object.
5066 * The first is named txg_%llu, and contains the txg
5067 * in which the property was last updated. The second
5068 * is named prop_%llu, and the nth element of its value
5069 * should be txg + object + n.
5071 tx
= dmu_tx_create(os
);
5072 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5073 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5078 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
5080 for (i
= 0; i
< ints
; i
++)
5081 value
[i
] = txg
+ object
+ i
;
5083 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
5085 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
5091 * Remove a random pair of entries.
5093 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
5094 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
5095 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
5097 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
5099 if (error
== ENOENT
)
5104 tx
= dmu_tx_create(os
);
5105 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5106 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5109 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
5110 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
5113 umem_free(od
, sizeof (ztest_od_t
));
5117 * Test case to test the upgrading of a microzap to fatzap.
5120 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
5122 objset_t
*os
= zd
->zd_os
;
5124 uint64_t object
, txg
;
5127 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5128 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5130 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
5131 !ztest_random(2)) != 0)
5133 object
= od
->od_object
;
5136 * Add entries to this ZAP and make sure it spills over
5137 * and gets upgraded to a fatzap. Also, since we are adding
5138 * 2050 entries we should see ptrtbl growth and leaf-block split.
5140 for (i
= 0; i
< 2050; i
++) {
5141 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5146 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
5147 (u_longlong_t
)id
, (u_longlong_t
)value
);
5149 tx
= dmu_tx_create(os
);
5150 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
5151 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5154 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
5156 ASSERT(error
== 0 || error
== EEXIST
);
5160 umem_free(od
, sizeof (ztest_od_t
));
5165 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
5167 objset_t
*os
= zd
->zd_os
;
5169 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
5171 int i
, namelen
, error
;
5172 int micro
= ztest_random(2);
5173 char name
[20], string_value
[20];
5176 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5177 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
5179 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5180 umem_free(od
, sizeof (ztest_od_t
));
5184 object
= od
->od_object
;
5187 * Generate a random name of the form 'xxx.....' where each
5188 * x is a random printable character and the dots are dots.
5189 * There are 94 such characters, and the name length goes from
5190 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
5192 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
5194 for (i
= 0; i
< 3; i
++)
5195 name
[i
] = '!' + ztest_random('~' - '!' + 1);
5196 for (; i
< namelen
- 1; i
++)
5200 if ((namelen
& 1) || micro
) {
5201 wsize
= sizeof (txg
);
5207 data
= string_value
;
5211 VERIFY0(zap_count(os
, object
, &count
));
5212 ASSERT(count
!= -1ULL);
5215 * Select an operation: length, lookup, add, update, remove.
5217 i
= ztest_random(5);
5220 tx
= dmu_tx_create(os
);
5221 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
5222 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
5224 umem_free(od
, sizeof (ztest_od_t
));
5227 bcopy(name
, string_value
, namelen
);
5231 bzero(string_value
, namelen
);
5237 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
5239 ASSERT3U(wsize
, ==, zl_wsize
);
5240 ASSERT3U(wc
, ==, zl_wc
);
5242 ASSERT3U(error
, ==, ENOENT
);
5247 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
5249 if (data
== string_value
&&
5250 bcmp(name
, data
, namelen
) != 0)
5251 fatal(0, "name '%s' != val '%s' len %d",
5252 name
, data
, namelen
);
5254 ASSERT3U(error
, ==, ENOENT
);
5259 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
5260 ASSERT(error
== 0 || error
== EEXIST
);
5264 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
5268 error
= zap_remove(os
, object
, name
, tx
);
5269 ASSERT(error
== 0 || error
== ENOENT
);
5276 umem_free(od
, sizeof (ztest_od_t
));
5280 * Commit callback data.
5282 typedef struct ztest_cb_data
{
5283 list_node_t zcd_node
;
5285 int zcd_expected_err
;
5286 boolean_t zcd_added
;
5287 boolean_t zcd_called
;
5291 /* This is the actual commit callback function */
5293 ztest_commit_callback(void *arg
, int error
)
5295 ztest_cb_data_t
*data
= arg
;
5296 uint64_t synced_txg
;
5298 VERIFY(data
!= NULL
);
5299 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5300 VERIFY(!data
->zcd_called
);
5302 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5303 if (data
->zcd_txg
> synced_txg
)
5304 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5305 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5308 data
->zcd_called
= B_TRUE
;
5310 if (error
== ECANCELED
) {
5311 ASSERT0(data
->zcd_txg
);
5312 ASSERT(!data
->zcd_added
);
5315 * The private callback data should be destroyed here, but
5316 * since we are going to check the zcd_called field after
5317 * dmu_tx_abort(), we will destroy it there.
5322 ASSERT(data
->zcd_added
);
5323 ASSERT3U(data
->zcd_txg
, !=, 0);
5325 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5327 /* See if this cb was called more quickly */
5328 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5329 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5331 /* Remove our callback from the list */
5332 list_remove(&zcl
.zcl_callbacks
, data
);
5334 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5336 umem_free(data
, sizeof (ztest_cb_data_t
));
5339 /* Allocate and initialize callback data structure */
5340 static ztest_cb_data_t
*
5341 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5343 ztest_cb_data_t
*cb_data
;
5345 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5347 cb_data
->zcd_txg
= txg
;
5348 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5349 list_link_init(&cb_data
->zcd_node
);
5355 * Commit callback test.
5358 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5360 objset_t
*os
= zd
->zd_os
;
5363 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5364 uint64_t old_txg
, txg
;
5367 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5368 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5370 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5371 umem_free(od
, sizeof (ztest_od_t
));
5375 tx
= dmu_tx_create(os
);
5377 cb_data
[0] = ztest_create_cb_data(os
, 0);
5378 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5380 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5382 /* Every once in a while, abort the transaction on purpose */
5383 if (ztest_random(100) == 0)
5387 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5389 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5391 cb_data
[0]->zcd_txg
= txg
;
5392 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5393 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5397 * It's not a strict requirement to call the registered
5398 * callbacks from inside dmu_tx_abort(), but that's what
5399 * it's supposed to happen in the current implementation
5400 * so we will check for that.
5402 for (i
= 0; i
< 2; i
++) {
5403 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5404 VERIFY(!cb_data
[i
]->zcd_called
);
5409 for (i
= 0; i
< 2; i
++) {
5410 VERIFY(cb_data
[i
]->zcd_called
);
5411 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5414 umem_free(od
, sizeof (ztest_od_t
));
5418 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5419 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5422 * Read existing data to make sure there isn't a future leak.
5424 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5425 &old_txg
, DMU_READ_PREFETCH
));
5428 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5431 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5433 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5436 * Since commit callbacks don't have any ordering requirement and since
5437 * it is theoretically possible for a commit callback to be called
5438 * after an arbitrary amount of time has elapsed since its txg has been
5439 * synced, it is difficult to reliably determine whether a commit
5440 * callback hasn't been called due to high load or due to a flawed
5443 * In practice, we will assume that if after a certain number of txgs a
5444 * commit callback hasn't been called, then most likely there's an
5445 * implementation bug..
5447 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5448 if (tmp_cb
!= NULL
&&
5449 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5450 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5451 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5455 * Let's find the place to insert our callbacks.
5457 * Even though the list is ordered by txg, it is possible for the
5458 * insertion point to not be the end because our txg may already be
5459 * quiescing at this point and other callbacks in the open txg
5460 * (from other objsets) may have sneaked in.
5462 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5463 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5464 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5466 /* Add the 3 callbacks to the list */
5467 for (i
= 0; i
< 3; i
++) {
5469 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5471 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5474 cb_data
[i
]->zcd_added
= B_TRUE
;
5475 VERIFY(!cb_data
[i
]->zcd_called
);
5477 tmp_cb
= cb_data
[i
];
5482 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5486 umem_free(od
, sizeof (ztest_od_t
));
5490 * Visit each object in the dataset. Verify that its properties
5491 * are consistent what was stored in the block tag when it was created,
5492 * and that its unused bonus buffer space has not been overwritten.
5496 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5498 objset_t
*os
= zd
->zd_os
;
5502 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5503 ztest_block_tag_t
*bt
= NULL
;
5504 dmu_object_info_t doi
;
5507 ztest_object_lock(zd
, obj
, RL_READER
);
5508 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5509 ztest_object_unlock(zd
, obj
);
5513 dmu_object_info_from_db(db
, &doi
);
5514 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5515 bt
= ztest_bt_bonus(db
);
5517 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5518 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5519 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5521 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5524 dmu_buf_rele(db
, FTAG
);
5525 ztest_object_unlock(zd
, obj
);
5531 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5533 zfs_prop_t proplist
[] = {
5535 ZFS_PROP_COMPRESSION
,
5541 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5543 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5544 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5545 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5547 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5548 ztest_random_blocksize(), (int)ztest_random(2)));
5550 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5555 ztest_remap_blocks(ztest_ds_t
*zd
, uint64_t id
)
5557 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5559 int error
= dmu_objset_remap_indirects(zd
->zd_name
);
5560 if (error
== ENOSPC
)
5564 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5569 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5571 nvlist_t
*props
= NULL
;
5573 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5575 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5576 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5578 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5580 if (ztest_opts
.zo_verbose
>= 6)
5581 dump_nvlist(props
, 4);
5585 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5589 user_release_one(const char *snapname
, const char *holdname
)
5591 nvlist_t
*snaps
, *holds
;
5594 snaps
= fnvlist_alloc();
5595 holds
= fnvlist_alloc();
5596 fnvlist_add_boolean(holds
, holdname
);
5597 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5598 fnvlist_free(holds
);
5599 error
= dsl_dataset_user_release(snaps
, NULL
);
5600 fnvlist_free(snaps
);
5605 * Test snapshot hold/release and deferred destroy.
5608 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5611 objset_t
*os
= zd
->zd_os
;
5615 char clonename
[100];
5617 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5620 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5622 dmu_objset_name(os
, osname
);
5624 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5626 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5627 (void) snprintf(clonename
, sizeof (clonename
),
5628 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5629 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5632 * Clean up from any previous run.
5634 error
= dsl_destroy_head(clonename
);
5635 if (error
!= ENOENT
)
5637 error
= user_release_one(fullname
, tag
);
5638 if (error
!= ESRCH
&& error
!= ENOENT
)
5640 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5641 if (error
!= ENOENT
)
5645 * Create snapshot, clone it, mark snap for deferred destroy,
5646 * destroy clone, verify snap was also destroyed.
5648 error
= dmu_objset_snapshot_one(osname
, snapname
);
5650 if (error
== ENOSPC
) {
5651 ztest_record_enospc("dmu_objset_snapshot");
5654 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5657 error
= dmu_objset_clone(clonename
, fullname
);
5659 if (error
== ENOSPC
) {
5660 ztest_record_enospc("dmu_objset_clone");
5663 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5666 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5668 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5672 error
= dsl_destroy_head(clonename
);
5674 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5676 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5677 if (error
!= ENOENT
)
5678 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5681 * Create snapshot, add temporary hold, verify that we can't
5682 * destroy a held snapshot, mark for deferred destroy,
5683 * release hold, verify snapshot was destroyed.
5685 error
= dmu_objset_snapshot_one(osname
, snapname
);
5687 if (error
== ENOSPC
) {
5688 ztest_record_enospc("dmu_objset_snapshot");
5691 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5694 holds
= fnvlist_alloc();
5695 fnvlist_add_string(holds
, fullname
, tag
);
5696 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5697 fnvlist_free(holds
);
5699 if (error
== ENOSPC
) {
5700 ztest_record_enospc("dsl_dataset_user_hold");
5703 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5704 fullname
, tag
, error
);
5707 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5708 if (error
!= EBUSY
) {
5709 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5713 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5715 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5719 error
= user_release_one(fullname
, tag
);
5721 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5723 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5726 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5730 * Inject random faults into the on-disk data.
5734 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5736 ztest_shared_t
*zs
= ztest_shared
;
5737 spa_t
*spa
= ztest_spa
;
5741 uint64_t bad
= 0x1990c0ffeedecadeull
;
5746 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5752 boolean_t islog
= B_FALSE
;
5754 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5755 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5757 mutex_enter(&ztest_vdev_lock
);
5760 * Device removal is in progress, fault injection must be disabled
5761 * until it completes and the pool is scrubbed. The fault injection
5762 * strategy for damaging blocks does not take in to account evacuated
5763 * blocks which may have already been damaged.
5765 if (ztest_device_removal_active
) {
5766 mutex_exit(&ztest_vdev_lock
);
5770 maxfaults
= MAXFAULTS(zs
);
5771 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5772 mirror_save
= zs
->zs_mirrors
;
5773 mutex_exit(&ztest_vdev_lock
);
5775 ASSERT(leaves
>= 1);
5778 * Grab the name lock as reader. There are some operations
5779 * which don't like to have their vdevs changed while
5780 * they are in progress (i.e. spa_change_guid). Those
5781 * operations will have grabbed the name lock as writer.
5783 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
5786 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5788 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5790 if (ztest_random(2) == 0) {
5792 * Inject errors on a normal data device or slog device.
5794 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5795 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5798 * Generate paths to the first leaf in this top-level vdev,
5799 * and to the random leaf we selected. We'll induce transient
5800 * write failures and random online/offline activity on leaf 0,
5801 * and we'll write random garbage to the randomly chosen leaf.
5803 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5804 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5805 top
* leaves
+ zs
->zs_splits
);
5806 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5807 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5808 top
* leaves
+ leaf
);
5810 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5811 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5815 * If the top-level vdev needs to be resilvered
5816 * then we only allow faults on the device that is
5819 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5820 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5821 vd0
->vdev_resilver_txg
!= 0)) {
5823 * Make vd0 explicitly claim to be unreadable,
5824 * or unwriteable, or reach behind its back
5825 * and close the underlying fd. We can do this if
5826 * maxfaults == 0 because we'll fail and reexecute,
5827 * and we can do it if maxfaults >= 2 because we'll
5828 * have enough redundancy. If maxfaults == 1, the
5829 * combination of this with injection of random data
5830 * corruption below exceeds the pool's fault tolerance.
5832 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5834 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5835 (long long)vd0
->vdev_id
, (int)maxfaults
);
5837 if (vf
!= NULL
&& ztest_random(3) == 0) {
5838 (void) close(vf
->vf_vnode
->v_fd
);
5839 vf
->vf_vnode
->v_fd
= -1;
5840 } else if (ztest_random(2) == 0) {
5841 vd0
->vdev_cant_read
= B_TRUE
;
5843 vd0
->vdev_cant_write
= B_TRUE
;
5845 guid0
= vd0
->vdev_guid
;
5849 * Inject errors on an l2cache device.
5851 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5853 if (sav
->sav_count
== 0) {
5854 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5855 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5858 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5859 guid0
= vd0
->vdev_guid
;
5860 (void) strcpy(path0
, vd0
->vdev_path
);
5861 (void) strcpy(pathrand
, vd0
->vdev_path
);
5865 maxfaults
= INT_MAX
; /* no limit on cache devices */
5868 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5869 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5872 * If we can tolerate two or more faults, or we're dealing
5873 * with a slog, randomly online/offline vd0.
5875 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5876 if (ztest_random(10) < 6) {
5877 int flags
= (ztest_random(2) == 0 ?
5878 ZFS_OFFLINE_TEMPORARY
: 0);
5881 * We have to grab the zs_name_lock as writer to
5882 * prevent a race between offlining a slog and
5883 * destroying a dataset. Offlining the slog will
5884 * grab a reference on the dataset which may cause
5885 * dsl_destroy_head() to fail with EBUSY thus
5886 * leaving the dataset in an inconsistent state.
5889 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
5891 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5894 (void) pthread_rwlock_unlock(&ztest_name_lock
);
5897 * Ideally we would like to be able to randomly
5898 * call vdev_[on|off]line without holding locks
5899 * to force unpredictable failures but the side
5900 * effects of vdev_[on|off]line prevent us from
5901 * doing so. We grab the ztest_vdev_lock here to
5902 * prevent a race between injection testing and
5905 mutex_enter(&ztest_vdev_lock
);
5906 (void) vdev_online(spa
, guid0
, 0, NULL
);
5907 mutex_exit(&ztest_vdev_lock
);
5915 * We have at least single-fault tolerance, so inject data corruption.
5917 fd
= open(pathrand
, O_RDWR
);
5919 if (fd
== -1) /* we hit a gap in the device namespace */
5922 fsize
= lseek(fd
, 0, SEEK_END
);
5924 while (--iters
!= 0) {
5926 * The offset must be chosen carefully to ensure that
5927 * we do not inject a given logical block with errors
5928 * on two different leaf devices, because ZFS can not
5929 * tolerate that (if maxfaults==1).
5931 * We divide each leaf into chunks of size
5932 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5933 * there is a series of ranges to which we can inject errors.
5934 * Each range can accept errors on only a single leaf vdev.
5935 * The error injection ranges are separated by ranges
5936 * which we will not inject errors on any device (DMZs).
5937 * Each DMZ must be large enough such that a single block
5938 * can not straddle it, so that a single block can not be
5939 * a target in two different injection ranges (on different
5942 * For example, with 3 leaves, each chunk looks like:
5943 * 0 to 32M: injection range for leaf 0
5944 * 32M to 64M: DMZ - no injection allowed
5945 * 64M to 96M: injection range for leaf 1
5946 * 96M to 128M: DMZ - no injection allowed
5947 * 128M to 160M: injection range for leaf 2
5948 * 160M to 192M: DMZ - no injection allowed
5950 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5951 (leaves
<< bshift
) + (leaf
<< bshift
) +
5952 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5955 * Only allow damage to the labels at one end of the vdev.
5957 * If all labels are damaged, the device will be totally
5958 * inaccessible, which will result in loss of data,
5959 * because we also damage (parts of) the other side of
5962 * Additionally, we will always have both an even and an
5963 * odd label, so that we can handle crashes in the
5964 * middle of vdev_config_sync().
5966 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5970 * The two end labels are stored at the "end" of the disk, but
5971 * the end of the disk (vdev_psize) is aligned to
5972 * sizeof (vdev_label_t).
5974 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5975 if ((leaf
& 1) == 1 &&
5976 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5979 mutex_enter(&ztest_vdev_lock
);
5980 if (mirror_save
!= zs
->zs_mirrors
) {
5981 mutex_exit(&ztest_vdev_lock
);
5986 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5987 fatal(1, "can't inject bad word at 0x%llx in %s",
5990 mutex_exit(&ztest_vdev_lock
);
5992 if (ztest_opts
.zo_verbose
>= 7)
5993 (void) printf("injected bad word into %s,"
5994 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5999 umem_free(path0
, MAXPATHLEN
);
6000 umem_free(pathrand
, MAXPATHLEN
);
6004 * Verify that DDT repair works as expected.
6007 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
6009 ztest_shared_t
*zs
= ztest_shared
;
6010 spa_t
*spa
= ztest_spa
;
6011 objset_t
*os
= zd
->zd_os
;
6013 uint64_t object
, blocksize
, txg
, pattern
, psize
;
6014 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
6019 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
6022 blocksize
= ztest_random_blocksize();
6023 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
6025 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
6026 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
6028 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
6029 umem_free(od
, sizeof (ztest_od_t
));
6034 * Take the name lock as writer to prevent anyone else from changing
6035 * the pool and dataset properies we need to maintain during this test.
6037 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6039 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
6041 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
6043 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6044 umem_free(od
, sizeof (ztest_od_t
));
6048 dmu_objset_stats_t dds
;
6049 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6050 dmu_objset_fast_stat(os
, &dds
);
6051 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6053 object
= od
[0].od_object
;
6054 blocksize
= od
[0].od_blocksize
;
6055 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
6057 ASSERT(object
!= 0);
6059 tx
= dmu_tx_create(os
);
6060 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
6061 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
6063 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6064 umem_free(od
, sizeof (ztest_od_t
));
6069 * Write all the copies of our block.
6071 for (i
= 0; i
< copies
; i
++) {
6072 uint64_t offset
= i
* blocksize
;
6073 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
6074 DMU_READ_NO_PREFETCH
);
6076 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
6077 os
, (long long)object
, (long long) offset
, error
);
6079 ASSERT(db
->db_offset
== offset
);
6080 ASSERT(db
->db_size
== blocksize
);
6081 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
6082 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
6083 dmu_buf_will_fill(db
, tx
);
6084 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
6085 dmu_buf_rele(db
, FTAG
);
6089 txg_wait_synced(spa_get_dsl(spa
), txg
);
6092 * Find out what block we got.
6094 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
6095 DMU_READ_NO_PREFETCH
));
6096 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
6097 dmu_buf_rele(db
, FTAG
);
6100 * Damage the block. Dedup-ditto will save us when we read it later.
6102 psize
= BP_GET_PSIZE(&blk
);
6103 abd
= abd_alloc_linear(psize
, B_TRUE
);
6104 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
6106 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
6107 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
6108 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
6112 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6113 umem_free(od
, sizeof (ztest_od_t
));
6121 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
6123 spa_t
*spa
= ztest_spa
;
6126 * Scrub in progress by device removal.
6128 if (ztest_device_removal_active
)
6131 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6132 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
6133 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6137 * Change the guid for the pool.
6141 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
6143 spa_t
*spa
= ztest_spa
;
6144 uint64_t orig
, load
;
6147 if (ztest_opts
.zo_mmp_test
)
6150 orig
= spa_guid(spa
);
6151 load
= spa_load_guid(spa
);
6153 (void) pthread_rwlock_wrlock(&ztest_name_lock
);
6154 error
= spa_change_guid(spa
);
6155 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6160 if (ztest_opts
.zo_verbose
>= 4) {
6161 (void) printf("Changed guid old %llu -> %llu\n",
6162 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
6165 VERIFY3U(orig
, !=, spa_guid(spa
));
6166 VERIFY3U(load
, ==, spa_load_guid(spa
));
6170 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
6172 hrtime_t end
= gethrtime() + NANOSEC
;
6174 while (gethrtime() <= end
) {
6175 int run_count
= 100;
6177 struct abd
*abd_data
, *abd_meta
;
6182 zio_cksum_t zc_ref_byteswap
;
6184 size
= ztest_random_blocksize();
6186 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6187 abd_data
= abd_alloc(size
, B_FALSE
);
6188 abd_meta
= abd_alloc(size
, B_TRUE
);
6190 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6191 *ptr
= ztest_random(UINT_MAX
);
6193 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
6194 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
6196 VERIFY0(fletcher_4_impl_set("scalar"));
6197 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6198 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
6200 VERIFY0(fletcher_4_impl_set("cycle"));
6201 while (run_count
-- > 0) {
6203 zio_cksum_t zc_byteswap
;
6205 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
6206 fletcher_4_native(buf
, size
, NULL
, &zc
);
6208 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6209 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6210 sizeof (zc_byteswap
)));
6212 /* Test ABD - data */
6213 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
6215 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
6217 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6218 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6219 sizeof (zc_byteswap
)));
6221 /* Test ABD - metadata */
6222 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
6224 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
6226 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
6227 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
6228 sizeof (zc_byteswap
)));
6232 umem_free(buf
, size
);
6239 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
6246 zio_cksum_t zc_ref_bswap
;
6248 hrtime_t end
= gethrtime() + NANOSEC
;
6250 while (gethrtime() <= end
) {
6251 int run_count
= 100;
6253 size
= ztest_random_blocksize();
6254 buf
= umem_alloc(size
, UMEM_NOFAIL
);
6256 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
6257 *ptr
= ztest_random(UINT_MAX
);
6259 VERIFY0(fletcher_4_impl_set("scalar"));
6260 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
6261 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
6263 VERIFY0(fletcher_4_impl_set("cycle"));
6265 while (run_count
-- > 0) {
6267 zio_cksum_t zc_bswap
;
6270 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6271 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6273 while (pos
< size
) {
6274 size_t inc
= 64 * ztest_random(size
/ 67);
6275 /* sometimes add few bytes to test non-simd */
6276 if (ztest_random(100) < 10)
6277 inc
+= P2ALIGN(ztest_random(64),
6280 if (inc
> (size
- pos
))
6283 fletcher_4_incremental_native(buf
+ pos
, inc
,
6285 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6291 VERIFY3U(pos
, ==, size
);
6293 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6294 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6297 * verify if incremental on the whole buffer is
6298 * equivalent to non-incremental version
6300 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6301 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6303 fletcher_4_incremental_native(buf
, size
, &zc
);
6304 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6306 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6307 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6310 umem_free(buf
, size
);
6315 ztest_check_path(char *path
)
6318 /* return true on success */
6319 return (!stat(path
, &s
));
6323 ztest_get_zdb_bin(char *bin
, int len
)
6327 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6328 * let popen to search through PATH.
6330 if ((zdb_path
= getenv("ZDB_PATH"))) {
6331 strlcpy(bin
, zdb_path
, len
); /* In env */
6332 if (!ztest_check_path(bin
)) {
6333 ztest_dump_core
= 0;
6334 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6339 VERIFY(realpath(getexecname(), bin
) != NULL
);
6340 if (strstr(bin
, "/ztest/")) {
6341 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6342 strcat(bin
, "/zdb/zdb");
6343 if (ztest_check_path(bin
))
6350 ztest_random_concrete_vdev_leaf(vdev_t
*vd
)
6355 if (vd
->vdev_children
== 0)
6358 vdev_t
*eligible
[vd
->vdev_children
];
6359 int eligible_idx
= 0, i
;
6360 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6361 vdev_t
*cvd
= vd
->vdev_child
[i
];
6362 if (cvd
->vdev_top
->vdev_removing
)
6364 if (cvd
->vdev_children
> 0 ||
6365 (vdev_is_concrete(cvd
) && !cvd
->vdev_detached
)) {
6366 eligible
[eligible_idx
++] = cvd
;
6369 VERIFY(eligible_idx
> 0);
6371 uint64_t child_no
= ztest_random(eligible_idx
);
6372 return (ztest_random_concrete_vdev_leaf(eligible
[child_no
]));
6377 ztest_initialize(ztest_ds_t
*zd
, uint64_t id
)
6379 spa_t
*spa
= ztest_spa
;
6382 mutex_enter(&ztest_vdev_lock
);
6384 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
6386 /* Random leaf vdev */
6387 vdev_t
*rand_vd
= ztest_random_concrete_vdev_leaf(spa
->spa_root_vdev
);
6388 if (rand_vd
== NULL
) {
6389 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6390 mutex_exit(&ztest_vdev_lock
);
6395 * The random vdev we've selected may change as soon as we
6396 * drop the spa_config_lock. We create local copies of things
6397 * we're interested in.
6399 uint64_t guid
= rand_vd
->vdev_guid
;
6400 char *path
= strdup(rand_vd
->vdev_path
);
6401 boolean_t active
= rand_vd
->vdev_initialize_thread
!= NULL
;
6403 zfs_dbgmsg("vd %p, guid %llu", rand_vd
, guid
);
6404 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
6406 uint64_t cmd
= ztest_random(POOL_INITIALIZE_FUNCS
);
6408 nvlist_t
*vdev_guids
= fnvlist_alloc();
6409 nvlist_t
*vdev_errlist
= fnvlist_alloc();
6410 fnvlist_add_uint64(vdev_guids
, path
, guid
);
6411 error
= spa_vdev_initialize(spa
, vdev_guids
, cmd
, vdev_errlist
);
6412 fnvlist_free(vdev_guids
);
6413 fnvlist_free(vdev_errlist
);
6416 case POOL_INITIALIZE_CANCEL
:
6417 if (ztest_opts
.zo_verbose
>= 4) {
6418 (void) printf("Cancel initialize %s", path
);
6420 (void) printf(" failed (no initialize active)");
6421 (void) printf("\n");
6424 case POOL_INITIALIZE_DO
:
6425 if (ztest_opts
.zo_verbose
>= 4) {
6426 (void) printf("Start initialize %s", path
);
6427 if (active
&& error
== 0)
6428 (void) printf(" failed (already active)");
6429 else if (error
!= 0)
6430 (void) printf(" failed (error %d)", error
);
6431 (void) printf("\n");
6434 case POOL_INITIALIZE_SUSPEND
:
6435 if (ztest_opts
.zo_verbose
>= 4) {
6436 (void) printf("Suspend initialize %s", path
);
6438 (void) printf(" failed (no initialize active)");
6439 (void) printf("\n");
6444 mutex_exit(&ztest_vdev_lock
);
6448 * Verify pool integrity by running zdb.
6451 ztest_run_zdb(char *pool
)
6457 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6460 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6461 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6462 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6464 ztest_get_zdb_bin(bin
, len
);
6467 "%s -bcc%s%s -G -d -U %s "
6468 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
6470 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6471 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6475 if (ztest_opts
.zo_verbose
>= 5)
6476 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6478 fp
= popen(zdb
, "r");
6480 while (fgets(zbuf
, 1024, fp
) != NULL
)
6481 if (ztest_opts
.zo_verbose
>= 3)
6482 (void) printf("%s", zbuf
);
6484 status
= pclose(fp
);
6489 ztest_dump_core
= 0;
6490 if (WIFEXITED(status
))
6491 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6493 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6495 umem_free(bin
, len
);
6496 umem_free(zdb
, len
);
6497 umem_free(zbuf
, 1024);
6501 ztest_walk_pool_directory(char *header
)
6505 if (ztest_opts
.zo_verbose
>= 6)
6506 (void) printf("%s\n", header
);
6508 mutex_enter(&spa_namespace_lock
);
6509 while ((spa
= spa_next(spa
)) != NULL
)
6510 if (ztest_opts
.zo_verbose
>= 6)
6511 (void) printf("\t%s\n", spa_name(spa
));
6512 mutex_exit(&spa_namespace_lock
);
6516 ztest_spa_import_export(char *oldname
, char *newname
)
6518 nvlist_t
*config
, *newconfig
;
6523 if (ztest_opts
.zo_verbose
>= 4) {
6524 (void) printf("import/export: old = %s, new = %s\n",
6529 * Clean up from previous runs.
6531 (void) spa_destroy(newname
);
6534 * Get the pool's configuration and guid.
6536 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6539 * Kick off a scrub to tickle scrub/export races.
6541 if (ztest_random(2) == 0)
6542 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6544 pool_guid
= spa_guid(spa
);
6545 spa_close(spa
, FTAG
);
6547 ztest_walk_pool_directory("pools before export");
6552 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6554 ztest_walk_pool_directory("pools after export");
6559 newconfig
= spa_tryimport(config
);
6560 ASSERT(newconfig
!= NULL
);
6561 nvlist_free(newconfig
);
6564 * Import it under the new name.
6566 error
= spa_import(newname
, config
, NULL
, 0);
6568 dump_nvlist(config
, 0);
6569 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6570 oldname
, newname
, error
);
6573 ztest_walk_pool_directory("pools after import");
6576 * Try to import it again -- should fail with EEXIST.
6578 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6581 * Try to import it under a different name -- should fail with EEXIST.
6583 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6586 * Verify that the pool is no longer visible under the old name.
6588 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6591 * Verify that we can open and close the pool using the new name.
6593 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6594 ASSERT(pool_guid
== spa_guid(spa
));
6595 spa_close(spa
, FTAG
);
6597 nvlist_free(config
);
6601 ztest_resume(spa_t
*spa
)
6603 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6604 (void) printf("resuming from suspended state\n");
6605 spa_vdev_state_enter(spa
, SCL_NONE
);
6606 vdev_clear(spa
, NULL
);
6607 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6608 (void) zio_resume(spa
);
6612 ztest_resume_thread(void *arg
)
6616 while (!ztest_exiting
) {
6617 if (spa_suspended(spa
))
6619 (void) poll(NULL
, 0, 100);
6622 * Periodically change the zfs_compressed_arc_enabled setting.
6624 if (ztest_random(10) == 0)
6625 zfs_compressed_arc_enabled
= ztest_random(2);
6628 * Periodically change the zfs_abd_scatter_enabled setting.
6630 if (ztest_random(10) == 0)
6631 zfs_abd_scatter_enabled
= ztest_random(2);
6634 * Periodically inject remapping delays (10% of the time).
6636 zfs_object_remap_one_indirect_delay_ms
=
6637 ztest_random(10) == 0 ? ztest_random(1000) + 1 : 0;
6644 ztest_deadman_thread(void *arg
)
6646 ztest_shared_t
*zs
= arg
;
6647 spa_t
*spa
= ztest_spa
;
6648 hrtime_t delay
, overdue
, last_run
= gethrtime();
6650 delay
= (zs
->zs_thread_stop
- zs
->zs_thread_start
) +
6651 MSEC2NSEC(zfs_deadman_synctime_ms
);
6653 while (!ztest_exiting
) {
6655 * Wait for the delay timer while checking occasionally
6656 * if we should stop.
6658 if (gethrtime() < last_run
+ delay
) {
6659 (void) poll(NULL
, 0, 1000);
6664 * If the pool is suspended then fail immediately. Otherwise,
6665 * check to see if the pool is making any progress. If
6666 * vdev_deadman() discovers that there hasn't been any recent
6667 * I/Os then it will end up aborting the tests.
6669 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6670 fatal(0, "aborting test after %llu seconds because "
6671 "pool has transitioned to a suspended state.",
6672 zfs_deadman_synctime_ms
/ 1000);
6674 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6677 * If the process doesn't complete within a grace period of
6678 * zfs_deadman_synctime_ms over the expected finish time,
6679 * then it may be hung and is terminated.
6681 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6682 if (gethrtime() > overdue
) {
6683 fatal(0, "aborting test after %llu seconds because "
6684 "the process is overdue for termination.",
6685 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6688 (void) printf("ztest has been running for %lld seconds\n",
6689 (gethrtime() - zs
->zs_proc_start
) / NANOSEC
);
6691 last_run
= gethrtime();
6692 delay
= MSEC2NSEC(zfs_deadman_checktime_ms
);
6699 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6701 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6702 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6703 hrtime_t functime
= gethrtime();
6706 for (i
= 0; i
< zi
->zi_iters
; i
++)
6707 zi
->zi_func(zd
, id
);
6709 functime
= gethrtime() - functime
;
6711 atomic_add_64(&zc
->zc_count
, 1);
6712 atomic_add_64(&zc
->zc_time
, functime
);
6714 if (ztest_opts
.zo_verbose
>= 4)
6715 (void) printf("%6.2f sec in %s\n",
6716 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6720 ztest_thread(void *arg
)
6723 uint64_t id
= (uintptr_t)arg
;
6724 ztest_shared_t
*zs
= ztest_shared
;
6728 ztest_shared_callstate_t
*zc
;
6730 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6732 * See if it's time to force a crash.
6734 if (now
> zs
->zs_thread_kill
)
6738 * If we're getting ENOSPC with some regularity, stop.
6740 if (zs
->zs_enospc_count
> 10)
6744 * Pick a random function to execute.
6746 rand
= ztest_random(ZTEST_FUNCS
);
6747 zi
= &ztest_info
[rand
];
6748 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6749 call_next
= zc
->zc_next
;
6751 if (now
>= call_next
&&
6752 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6753 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6754 ztest_execute(rand
, zi
, id
);
6762 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6764 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6768 ztest_dataset_destroy(int d
)
6770 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6773 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6775 if (ztest_opts
.zo_verbose
>= 3)
6776 (void) printf("Destroying %s to free up space\n", name
);
6779 * Cleanup any non-standard clones and snapshots. In general,
6780 * ztest thread t operates on dataset (t % zopt_datasets),
6781 * so there may be more than one thing to clean up.
6783 for (t
= d
; t
< ztest_opts
.zo_threads
;
6784 t
+= ztest_opts
.zo_datasets
)
6785 ztest_dsl_dataset_cleanup(name
, t
);
6787 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6788 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6792 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6794 uint64_t usedobjs
, dirobjs
, scratch
;
6797 * ZTEST_DIROBJ is the object directory for the entire dataset.
6798 * Therefore, the number of objects in use should equal the
6799 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6800 * If not, we have an object leak.
6802 * Note that we can only check this in ztest_dataset_open(),
6803 * when the open-context and syncing-context values agree.
6804 * That's because zap_count() returns the open-context value,
6805 * while dmu_objset_space() returns the rootbp fill count.
6807 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6808 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6809 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6813 ztest_dataset_open(int d
)
6815 ztest_ds_t
*zd
= &ztest_ds
[d
];
6816 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6819 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6822 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6824 (void) pthread_rwlock_rdlock(&ztest_name_lock
);
6826 error
= ztest_dataset_create(name
);
6827 if (error
== ENOSPC
) {
6828 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6829 ztest_record_enospc(FTAG
);
6832 ASSERT(error
== 0 || error
== EEXIST
);
6834 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6836 (void) pthread_rwlock_unlock(&ztest_name_lock
);
6838 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6840 zilog
= zd
->zd_zilog
;
6842 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6843 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6844 fatal(0, "missing log records: claimed %llu < committed %llu",
6845 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6847 ztest_dataset_dirobj_verify(zd
);
6849 zil_replay(os
, zd
, ztest_replay_vector
);
6851 ztest_dataset_dirobj_verify(zd
);
6853 if (ztest_opts
.zo_verbose
>= 6)
6854 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6856 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6857 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6858 (u_longlong_t
)zilog
->zl_replaying_seq
);
6860 zilog
= zil_open(os
, ztest_get_data
);
6862 if (zilog
->zl_replaying_seq
!= 0 &&
6863 zilog
->zl_replaying_seq
< committed_seq
)
6864 fatal(0, "missing log records: replayed %llu < committed %llu",
6865 zilog
->zl_replaying_seq
, committed_seq
);
6871 ztest_dataset_close(int d
)
6873 ztest_ds_t
*zd
= &ztest_ds
[d
];
6875 zil_close(zd
->zd_zilog
);
6876 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6883 ztest_replay_zil_cb(const char *name
, void *arg
)
6888 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_ANY
, B_TRUE
,
6889 B_TRUE
, FTAG
, &os
));
6891 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
6893 ztest_zd_init(zdtmp
, NULL
, os
);
6894 zil_replay(os
, zdtmp
, ztest_replay_vector
);
6895 ztest_zd_fini(zdtmp
);
6897 if (dmu_objset_zil(os
)->zl_parse_lr_count
!= 0 &&
6898 ztest_opts
.zo_verbose
>= 6) {
6899 zilog_t
*zilog
= dmu_objset_zil(os
);
6901 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6903 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6904 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6905 (u_longlong_t
)zilog
->zl_replaying_seq
);
6908 umem_free(zdtmp
, sizeof (ztest_ds_t
));
6910 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6915 * Kick off threads to run tests on all datasets in parallel.
6918 ztest_run(ztest_shared_t
*zs
)
6922 kthread_t
*resume_thread
, *deadman_thread
;
6923 kthread_t
**run_threads
;
6928 ztest_exiting
= B_FALSE
;
6931 * Initialize parent/child shared state.
6933 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6934 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6935 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
6937 zs
->zs_thread_start
= gethrtime();
6938 zs
->zs_thread_stop
=
6939 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6940 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6941 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6942 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6943 zs
->zs_thread_kill
-=
6944 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6947 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6949 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6950 offsetof(ztest_cb_data_t
, zcd_node
));
6955 kernel_init(FREAD
| FWRITE
);
6956 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6957 metaslab_preload_limit
= ztest_random(20) + 1;
6960 dmu_objset_stats_t dds
;
6961 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
6962 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
6963 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6964 dmu_objset_fast_stat(os
, &dds
);
6965 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6966 zs
->zs_guid
= dds
.dds_guid
;
6967 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6969 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6972 * Create a thread to periodically resume suspended I/O.
6974 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
6975 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6978 * Create a deadman thread and set to panic if we hang.
6980 deadman_thread
= thread_create(NULL
, 0, ztest_deadman_thread
,
6981 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6983 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
6986 * Verify that we can safely inquire about any object,
6987 * whether it's allocated or not. To make it interesting,
6988 * we probe a 5-wide window around each power of two.
6989 * This hits all edge cases, including zero and the max.
6991 for (t
= 0; t
< 64; t
++) {
6992 for (d
= -5; d
<= 5; d
++) {
6993 error
= dmu_object_info(spa
->spa_meta_objset
,
6994 (1ULL << t
) + d
, NULL
);
6995 ASSERT(error
== 0 || error
== ENOENT
||
7001 * If we got any ENOSPC errors on the previous run, destroy something.
7003 if (zs
->zs_enospc_count
!= 0) {
7004 int d
= ztest_random(ztest_opts
.zo_datasets
);
7005 ztest_dataset_destroy(d
);
7007 zs
->zs_enospc_count
= 0;
7010 * If we were in the middle of ztest_device_removal() and were killed
7011 * we need to ensure the removal and scrub complete before running
7012 * any tests that check ztest_device_removal_active. The removal will
7013 * be restarted automatically when the spa is opened, but we need to
7014 * initiate the scrub manually if it is not already in progress. Note
7015 * that we always run the scrub whenever an indirect vdev exists
7016 * because we have no way of knowing for sure if ztest_device_removal()
7017 * fully completed its scrub before the pool was reimported.
7019 if (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
||
7020 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7021 while (spa
->spa_removing_phys
.sr_state
== DSS_SCANNING
)
7022 txg_wait_synced(spa_get_dsl(spa
), 0);
7024 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
7025 while (dsl_scan_scrubbing(spa_get_dsl(spa
)))
7026 txg_wait_synced(spa_get_dsl(spa
), 0);
7029 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
7032 if (ztest_opts
.zo_verbose
>= 4)
7033 (void) printf("starting main threads...\n");
7036 * Replay all logs of all datasets in the pool. This is primarily for
7037 * temporary datasets which wouldn't otherwise get replayed, which
7038 * can trigger failures when attempting to offline a SLOG in
7039 * ztest_fault_inject().
7041 (void) dmu_objset_find(ztest_opts
.zo_pool
, ztest_replay_zil_cb
,
7042 NULL
, DS_FIND_CHILDREN
);
7045 * Kick off all the tests that run in parallel.
7047 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7048 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
7049 umem_free(run_threads
, ztest_opts
.zo_threads
*
7050 sizeof (kthread_t
*));
7054 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
7055 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
7060 * Wait for all of the tests to complete.
7062 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++)
7063 VERIFY0(thread_join(run_threads
[t
]));
7066 * Close all datasets. This must be done after all the threads
7067 * are joined so we can be sure none of the datasets are in-use
7068 * by any of the threads.
7070 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
7071 if (t
< ztest_opts
.zo_datasets
)
7072 ztest_dataset_close(t
);
7075 txg_wait_synced(spa_get_dsl(spa
), 0);
7077 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
7078 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
7080 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
7082 /* Kill the resume and deadman threads */
7083 ztest_exiting
= B_TRUE
;
7084 VERIFY0(thread_join(resume_thread
));
7085 VERIFY0(thread_join(deadman_thread
));
7089 * Right before closing the pool, kick off a bunch of async I/O;
7090 * spa_close() should wait for it to complete.
7092 for (object
= 1; object
< 50; object
++) {
7093 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
7094 ZIO_PRIORITY_SYNC_READ
);
7097 /* Verify that at least one commit cb was called in a timely fashion */
7098 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
7099 VERIFY0(zc_min_txg_delay
);
7101 spa_close(spa
, FTAG
);
7104 * Verify that we can loop over all pools.
7106 mutex_enter(&spa_namespace_lock
);
7107 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
7108 if (ztest_opts
.zo_verbose
> 3)
7109 (void) printf("spa_next: found %s\n", spa_name(spa
));
7110 mutex_exit(&spa_namespace_lock
);
7113 * Verify that we can export the pool and reimport it under a
7116 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
7117 char name
[ZFS_MAX_DATASET_NAME_LEN
];
7118 (void) snprintf(name
, sizeof (name
), "%s_import",
7119 ztest_opts
.zo_pool
);
7120 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
7121 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
7126 list_destroy(&zcl
.zcl_callbacks
);
7127 mutex_destroy(&zcl
.zcl_callbacks_lock
);
7128 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7129 mutex_destroy(&ztest_vdev_lock
);
7130 mutex_destroy(&ztest_checkpoint_lock
);
7136 ztest_ds_t
*zd
= &ztest_ds
[0];
7140 if (ztest_opts
.zo_verbose
>= 3)
7141 (void) printf("testing spa_freeze()...\n");
7143 kernel_init(FREAD
| FWRITE
);
7144 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7145 VERIFY3U(0, ==, ztest_dataset_open(0));
7149 * Force the first log block to be transactionally allocated.
7150 * We have to do this before we freeze the pool -- otherwise
7151 * the log chain won't be anchored.
7153 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
7154 ztest_dmu_object_alloc_free(zd
, 0);
7155 zil_commit(zd
->zd_zilog
, 0);
7158 txg_wait_synced(spa_get_dsl(spa
), 0);
7161 * Freeze the pool. This stops spa_sync() from doing anything,
7162 * so that the only way to record changes from now on is the ZIL.
7167 * Because it is hard to predict how much space a write will actually
7168 * require beforehand, we leave ourselves some fudge space to write over
7171 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
7174 * Run tests that generate log records but don't alter the pool config
7175 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
7176 * We do a txg_wait_synced() after each iteration to force the txg
7177 * to increase well beyond the last synced value in the uberblock.
7178 * The ZIL should be OK with that.
7180 * Run a random number of times less than zo_maxloops and ensure we do
7181 * not run out of space on the pool.
7183 while (ztest_random(10) != 0 &&
7184 numloops
++ < ztest_opts
.zo_maxloops
&&
7185 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
7187 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
7188 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
7189 ztest_io(zd
, od
.od_object
,
7190 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
7191 txg_wait_synced(spa_get_dsl(spa
), 0);
7195 * Commit all of the changes we just generated.
7197 zil_commit(zd
->zd_zilog
, 0);
7198 txg_wait_synced(spa_get_dsl(spa
), 0);
7201 * Close our dataset and close the pool.
7203 ztest_dataset_close(0);
7204 spa_close(spa
, FTAG
);
7208 * Open and close the pool and dataset to induce log replay.
7210 kernel_init(FREAD
| FWRITE
);
7211 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7212 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
7213 VERIFY3U(0, ==, ztest_dataset_open(0));
7215 txg_wait_synced(spa_get_dsl(spa
), 0);
7216 ztest_dataset_close(0);
7217 ztest_reguid(NULL
, 0);
7219 spa_close(spa
, FTAG
);
7224 print_time(hrtime_t t
, char *timebuf
)
7226 hrtime_t s
= t
/ NANOSEC
;
7227 hrtime_t m
= s
/ 60;
7228 hrtime_t h
= m
/ 60;
7229 hrtime_t d
= h
/ 24;
7238 (void) sprintf(timebuf
,
7239 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
7241 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
7243 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
7245 (void) sprintf(timebuf
, "%llus", s
);
7249 make_random_props(void)
7253 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
7255 if (ztest_random(2) == 0)
7258 VERIFY0(nvlist_add_uint64(props
,
7259 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
7265 * Import a storage pool with the given name.
7268 ztest_import(ztest_shared_t
*zs
)
7270 importargs_t args
= { 0 };
7272 nvlist_t
*cfg
= NULL
;
7274 char *searchdirs
[nsearch
];
7275 char *name
= ztest_opts
.zo_pool
;
7276 int flags
= ZFS_IMPORT_MISSING_LOG
;
7279 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7280 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7281 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7283 kernel_init(FREAD
| FWRITE
);
7285 searchdirs
[0] = ztest_opts
.zo_dir
;
7286 args
.paths
= nsearch
;
7287 args
.path
= searchdirs
;
7288 args
.can_be_active
= B_FALSE
;
7290 error
= zpool_find_config(NULL
, name
, &cfg
, &args
,
7291 &libzpool_config_ops
);
7293 (void) fatal(0, "No pools found\n");
7295 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
7296 VERIFY0(spa_open(name
, &spa
, FTAG
));
7297 zs
->zs_metaslab_sz
=
7298 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7299 spa_close(spa
, FTAG
);
7303 if (!ztest_opts
.zo_mmp_test
) {
7304 ztest_run_zdb(ztest_opts
.zo_pool
);
7306 ztest_run_zdb(ztest_opts
.zo_pool
);
7309 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7310 mutex_destroy(&ztest_vdev_lock
);
7311 mutex_destroy(&ztest_checkpoint_lock
);
7315 * Create a storage pool with the given name and initial vdev size.
7316 * Then test spa_freeze() functionality.
7319 ztest_init(ztest_shared_t
*zs
)
7322 nvlist_t
*nvroot
, *props
;
7325 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7326 mutex_init(&ztest_checkpoint_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7327 VERIFY0(pthread_rwlock_init(&ztest_name_lock
, NULL
));
7329 kernel_init(FREAD
| FWRITE
);
7332 * Create the storage pool.
7334 (void) spa_destroy(ztest_opts
.zo_pool
);
7335 ztest_shared
->zs_vdev_next_leaf
= 0;
7337 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
7338 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
7339 NULL
, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
7340 props
= make_random_props();
7343 * We don't expect the pool to suspend unless maxfaults == 0,
7344 * in which case ztest_fault_inject() temporarily takes away
7345 * the only valid replica.
7347 VERIFY0(nvlist_add_uint64(props
,
7348 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
7349 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
7351 for (i
= 0; i
< SPA_FEATURES
; i
++) {
7353 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
7354 spa_feature_table
[i
].fi_uname
));
7355 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
7359 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
7360 nvlist_free(nvroot
);
7363 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
7364 zs
->zs_metaslab_sz
=
7365 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
7366 spa_close(spa
, FTAG
);
7370 if (!ztest_opts
.zo_mmp_test
) {
7371 ztest_run_zdb(ztest_opts
.zo_pool
);
7373 ztest_run_zdb(ztest_opts
.zo_pool
);
7376 (void) pthread_rwlock_destroy(&ztest_name_lock
);
7377 mutex_destroy(&ztest_vdev_lock
);
7378 mutex_destroy(&ztest_checkpoint_lock
);
7384 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
7386 ztest_fd_data
= mkstemp(ztest_name_data
);
7387 ASSERT3S(ztest_fd_data
, >=, 0);
7388 (void) unlink(ztest_name_data
);
7392 shared_data_size(ztest_shared_hdr_t
*hdr
)
7396 size
= hdr
->zh_hdr_size
;
7397 size
+= hdr
->zh_opts_size
;
7398 size
+= hdr
->zh_size
;
7399 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7400 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
7409 ztest_shared_hdr_t
*hdr
;
7411 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7412 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7413 ASSERT(hdr
!= MAP_FAILED
);
7415 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
7417 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
7418 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
7419 hdr
->zh_size
= sizeof (ztest_shared_t
);
7420 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
7421 hdr
->zh_stats_count
= ZTEST_FUNCS
;
7422 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
7423 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
7425 size
= shared_data_size(hdr
);
7426 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
7428 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7435 ztest_shared_hdr_t
*hdr
;
7438 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
7439 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
7440 ASSERT(hdr
!= MAP_FAILED
);
7442 size
= shared_data_size(hdr
);
7444 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
7445 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
7446 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
7447 ASSERT(hdr
!= MAP_FAILED
);
7448 buf
= (uint8_t *)hdr
;
7450 offset
= hdr
->zh_hdr_size
;
7451 ztest_shared_opts
= (void *)&buf
[offset
];
7452 offset
+= hdr
->zh_opts_size
;
7453 ztest_shared
= (void *)&buf
[offset
];
7454 offset
+= hdr
->zh_size
;
7455 ztest_shared_callstate
= (void *)&buf
[offset
];
7456 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7457 ztest_shared_ds
= (void *)&buf
[offset
];
7461 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7465 char *cmdbuf
= NULL
;
7470 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7471 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7476 fatal(1, "fork failed");
7478 if (pid
== 0) { /* child */
7479 char *emptyargv
[2] = { cmd
, NULL
};
7480 char fd_data_str
[12];
7482 struct rlimit rl
= { 1024, 1024 };
7483 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7485 (void) close(ztest_fd_rand
);
7486 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7487 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7489 (void) enable_extended_FILE_stdio(-1, -1);
7490 if (libpath
!= NULL
)
7491 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7492 (void) execv(cmd
, emptyargv
);
7493 ztest_dump_core
= B_FALSE
;
7494 fatal(B_TRUE
, "exec failed: %s", cmd
);
7497 if (cmdbuf
!= NULL
) {
7498 umem_free(cmdbuf
, MAXPATHLEN
);
7502 while (waitpid(pid
, &status
, 0) != pid
)
7504 if (statusp
!= NULL
)
7507 if (WIFEXITED(status
)) {
7508 if (WEXITSTATUS(status
) != 0) {
7509 (void) fprintf(stderr
, "child exited with code %d\n",
7510 WEXITSTATUS(status
));
7514 } else if (WIFSIGNALED(status
)) {
7515 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7516 (void) fprintf(stderr
, "child died with signal %d\n",
7522 (void) fprintf(stderr
, "something strange happened to child\n");
7529 ztest_run_init(void)
7533 ztest_shared_t
*zs
= ztest_shared
;
7536 * Blow away any existing copy of zpool.cache
7538 (void) remove(spa_config_path
);
7540 if (ztest_opts
.zo_init
== 0) {
7541 if (ztest_opts
.zo_verbose
>= 1)
7542 (void) printf("Importing pool %s\n",
7543 ztest_opts
.zo_pool
);
7549 * Create and initialize our storage pool.
7551 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7552 bzero(zs
, sizeof (ztest_shared_t
));
7553 if (ztest_opts
.zo_verbose
>= 3 &&
7554 ztest_opts
.zo_init
!= 1) {
7555 (void) printf("ztest_init(), pass %d\n", i
);
7562 main(int argc
, char **argv
)
7570 ztest_shared_callstate_t
*zc
;
7572 char numbuf
[NN_NUMBUF_SZ
];
7576 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7577 struct sigaction action
;
7579 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7581 dprintf_setup(&argc
, argv
);
7582 zfs_deadman_synctime_ms
= 300000;
7583 zfs_deadman_checktime_ms
= 30000;
7585 * As two-word space map entries may not come up often (especially
7586 * if pool and vdev sizes are small) we want to force at least some
7587 * of them so the feature get tested.
7589 zfs_force_some_double_word_sm_entries
= B_TRUE
;
7592 * Verify that even extensively damaged split blocks with many
7593 * segments can be reconstructed in a reasonable amount of time
7594 * when reconstruction is known to be possible.
7596 * Note: the lower this value is, the more damage we inflict, and
7597 * the more time ztest spends in recovering that damage. We chose
7598 * to induce damage 1/100th of the time so recovery is tested but
7599 * not so frequently that ztest doesn't get to test other code paths.
7601 zfs_reconstruct_indirect_damage_fraction
= 100;
7603 action
.sa_handler
= sig_handler
;
7604 sigemptyset(&action
.sa_mask
);
7605 action
.sa_flags
= 0;
7607 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7608 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7613 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7614 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7620 * Force random_get_bytes() to use /dev/urandom in order to prevent
7621 * ztest from needlessly depleting the system entropy pool.
7623 random_path
= "/dev/urandom";
7624 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7625 ASSERT3S(ztest_fd_rand
, >=, 0);
7628 process_options(argc
, argv
);
7633 bcopy(&ztest_opts
, ztest_shared_opts
,
7634 sizeof (*ztest_shared_opts
));
7636 ztest_fd_data
= atoi(fd_data_str
);
7638 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7640 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7642 /* Override location of zpool.cache */
7643 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7644 ztest_opts
.zo_dir
) != -1);
7646 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7651 metaslab_force_ganging
= ztest_opts
.zo_metaslab_force_ganging
;
7652 metaslab_df_alloc_threshold
=
7653 zs
->zs_metaslab_df_alloc_threshold
;
7662 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7664 if (ztest_opts
.zo_verbose
>= 1) {
7665 (void) printf("%llu vdevs, %d datasets, %d threads,"
7666 " %llu seconds...\n",
7667 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7668 ztest_opts
.zo_datasets
,
7669 ztest_opts
.zo_threads
,
7670 (u_longlong_t
)ztest_opts
.zo_time
);
7673 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7674 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7676 zs
->zs_do_init
= B_TRUE
;
7677 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7678 if (ztest_opts
.zo_verbose
>= 1) {
7679 (void) printf("Executing older ztest for "
7680 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7682 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7683 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7685 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7687 zs
->zs_do_init
= B_FALSE
;
7689 zs
->zs_proc_start
= gethrtime();
7690 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7692 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7693 zi
= &ztest_info
[f
];
7694 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7695 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7696 zc
->zc_next
= UINT64_MAX
;
7698 zc
->zc_next
= zs
->zs_proc_start
+
7699 ztest_random(2 * zi
->zi_interval
[0] + 1);
7703 * Run the tests in a loop. These tests include fault injection
7704 * to verify that self-healing data works, and forced crashes
7705 * to verify that we never lose on-disk consistency.
7707 while (gethrtime() < zs
->zs_proc_stop
) {
7712 * Initialize the workload counters for each function.
7714 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7715 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7720 /* Set the allocation switch size */
7721 zs
->zs_metaslab_df_alloc_threshold
=
7722 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7724 if (!hasalt
|| ztest_random(2) == 0) {
7725 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7726 (void) printf("Executing newer ztest: %s\n",
7730 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7732 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7733 (void) printf("Executing older ztest: %s\n",
7734 ztest_opts
.zo_alt_ztest
);
7737 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7738 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7745 if (ztest_opts
.zo_verbose
>= 1) {
7746 hrtime_t now
= gethrtime();
7748 now
= MIN(now
, zs
->zs_proc_stop
);
7749 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7750 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7752 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7753 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7755 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7756 (u_longlong_t
)zs
->zs_enospc_count
,
7757 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7759 100.0 * (now
- zs
->zs_proc_start
) /
7760 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7763 if (ztest_opts
.zo_verbose
>= 2) {
7764 (void) printf("\nWorkload summary:\n\n");
7765 (void) printf("%7s %9s %s\n",
7766 "Calls", "Time", "Function");
7767 (void) printf("%7s %9s %s\n",
7768 "-----", "----", "--------");
7769 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7770 zi
= &ztest_info
[f
];
7771 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7772 print_time(zc
->zc_time
, timebuf
);
7773 (void) printf("%7llu %9s %s\n",
7774 (u_longlong_t
)zc
->zc_count
, timebuf
,
7777 (void) printf("\n");
7780 if (!ztest_opts
.zo_mmp_test
)
7781 ztest_run_zdb(ztest_opts
.zo_pool
);
7784 if (ztest_opts
.zo_verbose
>= 1) {
7786 (void) printf("%d runs of older ztest: %s\n", older
,
7787 ztest_opts
.zo_alt_ztest
);
7788 (void) printf("%d runs of newer ztest: %s\n", newer
,
7791 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7792 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7795 umem_free(cmd
, MAXNAMELEN
);