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, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatibility
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/zfs_rlock.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/spa_impl.h>
108 #include <sys/metaslab_impl.h>
109 #include <sys/dsl_prop.h>
110 #include <sys/dsl_dataset.h>
111 #include <sys/dsl_destroy.h>
112 #include <sys/dsl_scan.h>
113 #include <sys/zio_checksum.h>
114 #include <sys/refcount.h>
115 #include <sys/zfeature.h>
116 #include <sys/dsl_userhold.h>
119 #include <stdio_ext.h>
126 #include <sys/fs/zfs.h>
127 #include <zfs_fletcher.h>
128 #include <libnvpair.h>
131 #include <execinfo.h> /* for backtrace() */
134 static int ztest_fd_data
= -1;
135 static int ztest_fd_rand
= -1;
137 typedef struct ztest_shared_hdr
{
138 uint64_t zh_hdr_size
;
139 uint64_t zh_opts_size
;
141 uint64_t zh_stats_size
;
142 uint64_t zh_stats_count
;
144 uint64_t zh_ds_count
;
145 } ztest_shared_hdr_t
;
147 static ztest_shared_hdr_t
*ztest_shared_hdr
;
149 typedef struct ztest_shared_opts
{
150 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
151 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
152 char zo_alt_ztest
[MAXNAMELEN
];
153 char zo_alt_libpath
[MAXNAMELEN
];
155 uint64_t zo_vdevtime
;
163 uint64_t zo_passtime
;
164 uint64_t zo_killrate
;
168 uint64_t zo_maxloops
;
169 uint64_t zo_metaslab_gang_bang
;
171 } ztest_shared_opts_t
;
173 static const ztest_shared_opts_t ztest_opts_defaults
= {
174 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
175 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
176 .zo_alt_ztest
= { '\0' },
177 .zo_alt_libpath
= { '\0' },
179 .zo_ashift
= SPA_MINBLOCKSHIFT
,
182 .zo_raidz_parity
= 1,
183 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
186 .zo_passtime
= 60, /* 60 seconds */
187 .zo_killrate
= 70, /* 70% kill rate */
191 .zo_time
= 300, /* 5 minutes */
192 .zo_maxloops
= 50, /* max loops during spa_freeze() */
193 .zo_metaslab_gang_bang
= 32 << 10
196 extern uint64_t metaslab_gang_bang
;
197 extern uint64_t metaslab_df_alloc_threshold
;
198 extern int metaslab_preload_limit
;
199 extern boolean_t zfs_compressed_arc_enabled
;
200 extern int zfs_abd_scatter_enabled
;
201 extern int dmu_object_alloc_chunk_shift
;
203 static ztest_shared_opts_t
*ztest_shared_opts
;
204 static ztest_shared_opts_t ztest_opts
;
206 typedef struct ztest_shared_ds
{
210 static ztest_shared_ds_t
*ztest_shared_ds
;
211 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
213 #define BT_MAGIC 0x123456789abcdefULL
214 #define MAXFAULTS() \
215 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
219 ZTEST_IO_WRITE_PATTERN
,
220 ZTEST_IO_WRITE_ZEROES
,
227 typedef struct ztest_block_tag
{
231 uint64_t bt_dnodesize
;
238 typedef struct bufwad
{
256 #define ZTEST_RANGE_LOCKS 64
257 #define ZTEST_OBJECT_LOCKS 64
260 * Object descriptor. Used as a template for object lookup/create/remove.
262 typedef struct ztest_od
{
265 dmu_object_type_t od_type
;
266 dmu_object_type_t od_crtype
;
267 uint64_t od_blocksize
;
268 uint64_t od_crblocksize
;
269 uint64_t od_crdnodesize
;
272 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
278 typedef struct ztest_ds
{
279 ztest_shared_ds_t
*zd_shared
;
281 rwlock_t zd_zilog_lock
;
283 ztest_od_t
*zd_od
; /* debugging aid */
284 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
285 kmutex_t zd_dirobj_lock
;
286 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
287 zll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
291 * Per-iteration state.
293 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
295 typedef struct ztest_info
{
296 ztest_func_t
*zi_func
; /* test function */
297 uint64_t zi_iters
; /* iterations per execution */
298 uint64_t *zi_interval
; /* execute every <interval> seconds */
299 const char *zi_funcname
; /* name of test function */
302 typedef struct ztest_shared_callstate
{
303 uint64_t zc_count
; /* per-pass count */
304 uint64_t zc_time
; /* per-pass time */
305 uint64_t zc_next
; /* next time to call this function */
306 } ztest_shared_callstate_t
;
308 static ztest_shared_callstate_t
*ztest_shared_callstate
;
309 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
311 ztest_func_t ztest_dmu_read_write
;
312 ztest_func_t ztest_dmu_write_parallel
;
313 ztest_func_t ztest_dmu_object_alloc_free
;
314 ztest_func_t ztest_dmu_object_next_chunk
;
315 ztest_func_t ztest_dmu_commit_callbacks
;
316 ztest_func_t ztest_zap
;
317 ztest_func_t ztest_zap_parallel
;
318 ztest_func_t ztest_zil_commit
;
319 ztest_func_t ztest_zil_remount
;
320 ztest_func_t ztest_dmu_read_write_zcopy
;
321 ztest_func_t ztest_dmu_objset_create_destroy
;
322 ztest_func_t ztest_dmu_prealloc
;
323 ztest_func_t ztest_fzap
;
324 ztest_func_t ztest_dmu_snapshot_create_destroy
;
325 ztest_func_t ztest_dsl_prop_get_set
;
326 ztest_func_t ztest_spa_prop_get_set
;
327 ztest_func_t ztest_spa_create_destroy
;
328 ztest_func_t ztest_fault_inject
;
329 ztest_func_t ztest_ddt_repair
;
330 ztest_func_t ztest_dmu_snapshot_hold
;
331 ztest_func_t ztest_mmp_enable_disable
;
332 ztest_func_t ztest_spa_rename
;
333 ztest_func_t ztest_scrub
;
334 ztest_func_t ztest_dsl_dataset_promote_busy
;
335 ztest_func_t ztest_vdev_attach_detach
;
336 ztest_func_t ztest_vdev_LUN_growth
;
337 ztest_func_t ztest_vdev_add_remove
;
338 ztest_func_t ztest_vdev_aux_add_remove
;
339 ztest_func_t ztest_split_pool
;
340 ztest_func_t ztest_reguid
;
341 ztest_func_t ztest_spa_upgrade
;
342 ztest_func_t ztest_fletcher
;
343 ztest_func_t ztest_fletcher_incr
;
344 ztest_func_t ztest_verify_dnode_bt
;
346 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
347 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
348 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
349 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
350 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
352 #define ZTI_INIT(func, iters, interval) \
353 { .zi_func = (func), \
354 .zi_iters = (iters), \
355 .zi_interval = (interval), \
356 .zi_funcname = # func }
358 ztest_info_t ztest_info
[] = {
359 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
360 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
361 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
362 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
363 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
364 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
365 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
366 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
367 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
368 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
369 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
370 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
371 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
372 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
374 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
376 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
377 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
378 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
379 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
380 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
381 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
382 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
383 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
384 ZTI_INIT(ztest_spa_rename
, 1, &zopt_rarely
),
385 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
386 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
387 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
388 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
389 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
390 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
391 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
392 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
393 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
394 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
397 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
400 * The following struct is used to hold a list of uncalled commit callbacks.
401 * The callbacks are ordered by txg number.
403 typedef struct ztest_cb_list
{
404 kmutex_t zcl_callbacks_lock
;
405 list_t zcl_callbacks
;
409 * Stuff we need to share writably between parent and child.
411 typedef struct ztest_shared
{
412 boolean_t zs_do_init
;
413 hrtime_t zs_proc_start
;
414 hrtime_t zs_proc_stop
;
415 hrtime_t zs_thread_start
;
416 hrtime_t zs_thread_stop
;
417 hrtime_t zs_thread_kill
;
418 uint64_t zs_enospc_count
;
419 uint64_t zs_vdev_next_leaf
;
420 uint64_t zs_vdev_aux
;
425 uint64_t zs_metaslab_sz
;
426 uint64_t zs_metaslab_df_alloc_threshold
;
430 #define ID_PARALLEL -1ULL
432 static char ztest_dev_template
[] = "%s/%s.%llua";
433 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
434 ztest_shared_t
*ztest_shared
;
436 static spa_t
*ztest_spa
= NULL
;
437 static ztest_ds_t
*ztest_ds
;
439 static kmutex_t ztest_vdev_lock
;
442 * The ztest_name_lock protects the pool and dataset namespace used by
443 * the individual tests. To modify the namespace, consumers must grab
444 * this lock as writer. Grabbing the lock as reader will ensure that the
445 * namespace does not change while the lock is held.
447 static rwlock_t ztest_name_lock
;
449 static boolean_t ztest_dump_core
= B_TRUE
;
450 static boolean_t ztest_exiting
;
452 /* Global commit callback list */
453 static ztest_cb_list_t zcl
;
454 /* Commit cb delay */
455 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
456 static int zc_cb_counter
= 0;
459 * Minimum number of commit callbacks that need to be registered for us to check
460 * whether the minimum txg delay is acceptable.
462 #define ZTEST_COMMIT_CB_MIN_REG 100
465 * If a number of txgs equal to this threshold have been created after a commit
466 * callback has been registered but not called, then we assume there is an
467 * implementation bug.
469 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
471 extern uint64_t metaslab_gang_bang
;
472 extern uint64_t metaslab_df_alloc_threshold
;
475 ZTEST_META_DNODE
= 0,
480 static void usage(boolean_t
) __NORETURN
;
483 * These libumem hooks provide a reasonable set of defaults for the allocator's
484 * debugging facilities.
487 _umem_debug_init(void)
489 return ("default,verbose"); /* $UMEM_DEBUG setting */
493 _umem_logging_init(void)
495 return ("fail,contents"); /* $UMEM_LOGGING setting */
498 #define BACKTRACE_SZ 100
500 static void sig_handler(int signo
)
502 struct sigaction action
;
503 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
505 void *buffer
[BACKTRACE_SZ
];
507 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
508 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
512 * Restore default action and re-raise signal so SIGSEGV and
513 * SIGABRT can trigger a core dump.
515 action
.sa_handler
= SIG_DFL
;
516 sigemptyset(&action
.sa_mask
);
518 (void) sigaction(signo
, &action
, NULL
);
522 #define FATAL_MSG_SZ 1024
527 fatal(int do_perror
, char *message
, ...)
530 int save_errno
= errno
;
533 (void) fflush(stdout
);
534 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
536 va_start(args
, message
);
537 (void) sprintf(buf
, "ztest: ");
539 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
542 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
543 ": %s", strerror(save_errno
));
545 (void) fprintf(stderr
, "%s\n", buf
);
546 fatal_msg
= buf
; /* to ease debugging */
553 str2shift(const char *buf
)
555 const char *ends
= "BKMGTPEZ";
560 for (i
= 0; i
< strlen(ends
); i
++) {
561 if (toupper(buf
[0]) == ends
[i
])
564 if (i
== strlen(ends
)) {
565 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
569 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
572 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
578 nicenumtoull(const char *buf
)
583 val
= strtoull(buf
, &end
, 0);
585 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
587 } else if (end
[0] == '.') {
588 double fval
= strtod(buf
, &end
);
589 fval
*= pow(2, str2shift(end
));
590 if (fval
> UINT64_MAX
) {
591 (void) fprintf(stderr
, "ztest: value too large: %s\n",
595 val
= (uint64_t)fval
;
597 int shift
= str2shift(end
);
598 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
599 (void) fprintf(stderr
, "ztest: value too large: %s\n",
609 usage(boolean_t requested
)
611 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
613 char nice_vdev_size
[10];
614 char nice_gang_bang
[10];
615 FILE *fp
= requested
? stdout
: stderr
;
617 nicenum(zo
->zo_vdev_size
, nice_vdev_size
);
618 nicenum(zo
->zo_metaslab_gang_bang
, nice_gang_bang
);
620 (void) fprintf(fp
, "Usage: %s\n"
621 "\t[-v vdevs (default: %llu)]\n"
622 "\t[-s size_of_each_vdev (default: %s)]\n"
623 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
624 "\t[-m mirror_copies (default: %d)]\n"
625 "\t[-r raidz_disks (default: %d)]\n"
626 "\t[-R raidz_parity (default: %d)]\n"
627 "\t[-d datasets (default: %d)]\n"
628 "\t[-t threads (default: %d)]\n"
629 "\t[-g gang_block_threshold (default: %s)]\n"
630 "\t[-i init_count (default: %d)] initialize pool i times\n"
631 "\t[-k kill_percentage (default: %llu%%)]\n"
632 "\t[-p pool_name (default: %s)]\n"
633 "\t[-f dir (default: %s)] file directory for vdev files\n"
634 "\t[-M] Multi-host simulate pool imported on remote host\n"
635 "\t[-V] verbose (use multiple times for ever more blather)\n"
636 "\t[-E] use existing pool instead of creating new one\n"
637 "\t[-T time (default: %llu sec)] total run time\n"
638 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
639 "\t[-P passtime (default: %llu sec)] time per pass\n"
640 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
641 "\t[-o variable=value] ... set global variable to an unsigned\n"
642 "\t 32-bit integer value\n"
643 "\t[-h] (print help)\n"
646 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
647 nice_vdev_size
, /* -s */
648 zo
->zo_ashift
, /* -a */
649 zo
->zo_mirrors
, /* -m */
650 zo
->zo_raidz
, /* -r */
651 zo
->zo_raidz_parity
, /* -R */
652 zo
->zo_datasets
, /* -d */
653 zo
->zo_threads
, /* -t */
654 nice_gang_bang
, /* -g */
655 zo
->zo_init
, /* -i */
656 (u_longlong_t
)zo
->zo_killrate
, /* -k */
657 zo
->zo_pool
, /* -p */
659 (u_longlong_t
)zo
->zo_time
, /* -T */
660 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
661 (u_longlong_t
)zo
->zo_passtime
);
662 exit(requested
? 0 : 1);
666 process_options(int argc
, char **argv
)
669 ztest_shared_opts_t
*zo
= &ztest_opts
;
673 char altdir
[MAXNAMELEN
] = { 0 };
675 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
677 while ((opt
= getopt(argc
, argv
,
678 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:")) != EOF
) {
695 value
= nicenumtoull(optarg
);
699 zo
->zo_vdevs
= value
;
702 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
705 zo
->zo_ashift
= value
;
708 zo
->zo_mirrors
= value
;
711 zo
->zo_raidz
= MAX(1, value
);
714 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
717 zo
->zo_datasets
= MAX(1, value
);
720 zo
->zo_threads
= MAX(1, value
);
723 zo
->zo_metaslab_gang_bang
= MAX(SPA_MINBLOCKSIZE
<< 1,
730 zo
->zo_killrate
= value
;
733 (void) strlcpy(zo
->zo_pool
, optarg
,
734 sizeof (zo
->zo_pool
));
737 path
= realpath(optarg
, NULL
);
739 (void) fprintf(stderr
, "error: %s: %s\n",
740 optarg
, strerror(errno
));
743 (void) strlcpy(zo
->zo_dir
, path
,
744 sizeof (zo
->zo_dir
));
761 zo
->zo_passtime
= MAX(1, value
);
764 zo
->zo_maxloops
= MAX(1, value
);
767 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
770 if (set_global_var(optarg
) != 0)
783 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
786 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
789 if (strlen(altdir
) > 0) {
797 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
798 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
800 VERIFY(NULL
!= realpath(getexecname(), cmd
));
801 if (0 != access(altdir
, F_OK
)) {
802 ztest_dump_core
= B_FALSE
;
803 fatal(B_TRUE
, "invalid alternate ztest path: %s",
806 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
809 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
810 * We want to extract <isa> to determine if we should use
811 * 32 or 64 bit binaries.
813 bin
= strstr(cmd
, "/usr/bin/");
814 ztest
= strstr(bin
, "/ztest");
816 isalen
= ztest
- isa
;
817 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
818 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
819 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
820 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
822 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
823 ztest_dump_core
= B_FALSE
;
824 fatal(B_TRUE
, "invalid alternate ztest: %s",
826 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
827 ztest_dump_core
= B_FALSE
;
828 fatal(B_TRUE
, "invalid alternate lib directory %s",
832 umem_free(cmd
, MAXPATHLEN
);
833 umem_free(realaltdir
, MAXPATHLEN
);
838 ztest_kill(ztest_shared_t
*zs
)
840 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
841 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
844 * Before we kill off ztest, make sure that the config is updated.
845 * See comment above spa_config_sync().
847 mutex_enter(&spa_namespace_lock
);
848 spa_config_sync(ztest_spa
, B_FALSE
, B_FALSE
);
849 mutex_exit(&spa_namespace_lock
);
851 (void) kill(getpid(), SIGKILL
);
855 ztest_random(uint64_t range
)
859 ASSERT3S(ztest_fd_rand
, >=, 0);
864 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
865 fatal(1, "short read from /dev/urandom");
872 ztest_record_enospc(const char *s
)
874 ztest_shared
->zs_enospc_count
++;
878 ztest_get_ashift(void)
880 if (ztest_opts
.zo_ashift
== 0)
881 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
882 return (ztest_opts
.zo_ashift
);
886 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
892 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
895 ashift
= ztest_get_ashift();
901 vdev
= ztest_shared
->zs_vdev_aux
;
902 (void) snprintf(path
, MAXPATHLEN
,
903 ztest_aux_template
, ztest_opts
.zo_dir
,
904 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
907 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
908 (void) snprintf(path
, MAXPATHLEN
,
909 ztest_dev_template
, ztest_opts
.zo_dir
,
910 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
915 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
917 fatal(1, "can't open %s", path
);
918 if (ftruncate(fd
, size
) != 0)
919 fatal(1, "can't ftruncate %s", path
);
923 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
924 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
925 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
926 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
927 umem_free(pathbuf
, MAXPATHLEN
);
933 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
934 uint64_t ashift
, int r
)
936 nvlist_t
*raidz
, **child
;
940 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
941 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
943 for (c
= 0; c
< r
; c
++)
944 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
946 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
947 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
948 VDEV_TYPE_RAIDZ
) == 0);
949 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
950 ztest_opts
.zo_raidz_parity
) == 0);
951 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
954 for (c
= 0; c
< r
; c
++)
955 nvlist_free(child
[c
]);
957 umem_free(child
, r
* sizeof (nvlist_t
*));
963 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
964 uint64_t ashift
, int r
, int m
)
966 nvlist_t
*mirror
, **child
;
970 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
972 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
974 for (c
= 0; c
< m
; c
++)
975 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
977 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
978 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
979 VDEV_TYPE_MIRROR
) == 0);
980 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
983 for (c
= 0; c
< m
; c
++)
984 nvlist_free(child
[c
]);
986 umem_free(child
, m
* sizeof (nvlist_t
*));
992 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
993 int log
, int r
, int m
, int t
)
995 nvlist_t
*root
, **child
;
1000 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1002 for (c
= 0; c
< t
; c
++) {
1003 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1005 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1009 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1010 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1011 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1014 for (c
= 0; c
< t
; c
++)
1015 nvlist_free(child
[c
]);
1017 umem_free(child
, t
* sizeof (nvlist_t
*));
1023 * Find a random spa version. Returns back a random spa version in the
1024 * range [initial_version, SPA_VERSION_FEATURES].
1027 ztest_random_spa_version(uint64_t initial_version
)
1029 uint64_t version
= initial_version
;
1031 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1033 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1036 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1037 version
= SPA_VERSION_FEATURES
;
1039 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1044 ztest_random_blocksize(void)
1047 * Choose a block size >= the ashift.
1048 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1050 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1051 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1053 uint64_t block_shift
=
1054 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1055 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1059 ztest_random_dnodesize(void)
1062 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1064 if (max_slots
== DNODE_MIN_SLOTS
)
1065 return (DNODE_MIN_SIZE
);
1068 * Weight the random distribution more heavily toward smaller
1069 * dnode sizes since that is more likely to reflect real-world
1072 ASSERT3U(max_slots
, >, 4);
1073 switch (ztest_random(10)) {
1075 slots
= 5 + ztest_random(max_slots
- 4);
1078 slots
= 2 + ztest_random(3);
1085 return (slots
<< DNODE_SHIFT
);
1089 ztest_random_ibshift(void)
1091 return (DN_MIN_INDBLKSHIFT
+
1092 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1096 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1099 vdev_t
*rvd
= spa
->spa_root_vdev
;
1102 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1105 top
= ztest_random(rvd
->vdev_children
);
1106 tvd
= rvd
->vdev_child
[top
];
1107 } while (tvd
->vdev_ishole
|| (tvd
->vdev_islog
&& !log_ok
) ||
1108 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1114 ztest_random_dsl_prop(zfs_prop_t prop
)
1119 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1120 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1126 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1129 const char *propname
= zfs_prop_to_name(prop
);
1130 const char *valname
;
1135 error
= dsl_prop_set_int(osname
, propname
,
1136 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1138 if (error
== ENOSPC
) {
1139 ztest_record_enospc(FTAG
);
1144 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1145 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1147 if (ztest_opts
.zo_verbose
>= 6) {
1150 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1152 (void) printf("%s %s = %llu at '%s'\n", osname
,
1153 propname
, (unsigned long long)curval
, setpoint
);
1155 (void) printf("%s %s = %s at '%s'\n",
1156 osname
, propname
, valname
, setpoint
);
1158 umem_free(setpoint
, MAXPATHLEN
);
1164 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1166 spa_t
*spa
= ztest_spa
;
1167 nvlist_t
*props
= NULL
;
1170 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1171 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1173 error
= spa_prop_set(spa
, props
);
1177 if (error
== ENOSPC
) {
1178 ztest_record_enospc(FTAG
);
1188 * Object and range lock mechanics
1191 list_node_t z_lnode
;
1192 zfs_refcount_t z_refcnt
;
1194 zfs_rlock_t z_range_lock
;
1199 ztest_znode_t
*z_ztznode
;
1202 static ztest_znode_t
*
1203 ztest_znode_init(uint64_t object
)
1205 ztest_znode_t
*zp
= umem_alloc(sizeof (*zp
), UMEM_NOFAIL
);
1207 list_link_init(&zp
->z_lnode
);
1208 zfs_refcount_create(&zp
->z_refcnt
);
1209 zp
->z_object
= object
;
1210 zfs_rlock_init(&zp
->z_range_lock
);
1216 ztest_znode_fini(ztest_znode_t
*zp
)
1218 ASSERT(zfs_refcount_is_zero(&zp
->z_refcnt
));
1219 zfs_rlock_destroy(&zp
->z_range_lock
);
1221 zfs_refcount_destroy(&zp
->z_refcnt
);
1222 list_link_init(&zp
->z_lnode
);
1223 umem_free(zp
, sizeof (*zp
));
1227 ztest_zll_init(zll_t
*zll
)
1229 mutex_init(&zll
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1230 list_create(&zll
->z_list
, sizeof (ztest_znode_t
),
1231 offsetof(ztest_znode_t
, z_lnode
));
1235 ztest_zll_destroy(zll_t
*zll
)
1237 list_destroy(&zll
->z_list
);
1238 mutex_destroy(&zll
->z_lock
);
1241 #define RL_TAG "range_lock"
1242 static ztest_znode_t
*
1243 ztest_znode_get(ztest_ds_t
*zd
, uint64_t object
)
1245 zll_t
*zll
= &zd
->zd_range_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1246 ztest_znode_t
*zp
= NULL
;
1247 mutex_enter(&zll
->z_lock
);
1248 for (zp
= list_head(&zll
->z_list
); (zp
);
1249 zp
= list_next(&zll
->z_list
, zp
)) {
1250 if (zp
->z_object
== object
) {
1251 zfs_refcount_add(&zp
->z_refcnt
, RL_TAG
);
1256 zp
= ztest_znode_init(object
);
1257 zfs_refcount_add(&zp
->z_refcnt
, RL_TAG
);
1258 list_insert_head(&zll
->z_list
, zp
);
1260 mutex_exit(&zll
->z_lock
);
1265 ztest_znode_put(ztest_ds_t
*zd
, ztest_znode_t
*zp
)
1268 ASSERT3U(zp
->z_object
, !=, 0);
1269 zll
= &zd
->zd_range_lock
[zp
->z_object
& (ZTEST_OBJECT_LOCKS
- 1)];
1270 mutex_enter(&zll
->z_lock
);
1271 zfs_refcount_remove(&zp
->z_refcnt
, RL_TAG
);
1272 if (zfs_refcount_is_zero(&zp
->z_refcnt
)) {
1273 list_remove(&zll
->z_list
, zp
);
1274 ztest_znode_fini(zp
);
1276 mutex_exit(&zll
->z_lock
);
1281 ztest_rll_init(rll_t
*rll
)
1283 rll
->rll_writer
= NULL
;
1284 rll
->rll_readers
= 0;
1285 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1286 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1290 ztest_rll_destroy(rll_t
*rll
)
1292 ASSERT(rll
->rll_writer
== NULL
);
1293 ASSERT(rll
->rll_readers
== 0);
1294 mutex_destroy(&rll
->rll_lock
);
1295 cv_destroy(&rll
->rll_cv
);
1299 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1301 mutex_enter(&rll
->rll_lock
);
1303 if (type
== RL_READER
) {
1304 while (rll
->rll_writer
!= NULL
)
1305 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1308 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1309 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1310 rll
->rll_writer
= curthread
;
1313 mutex_exit(&rll
->rll_lock
);
1317 ztest_rll_unlock(rll_t
*rll
)
1319 mutex_enter(&rll
->rll_lock
);
1321 if (rll
->rll_writer
) {
1322 ASSERT(rll
->rll_readers
== 0);
1323 rll
->rll_writer
= NULL
;
1325 ASSERT(rll
->rll_readers
!= 0);
1326 ASSERT(rll
->rll_writer
== NULL
);
1330 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1331 cv_broadcast(&rll
->rll_cv
);
1333 mutex_exit(&rll
->rll_lock
);
1337 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1339 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1341 ztest_rll_lock(rll
, type
);
1345 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1347 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1349 ztest_rll_unlock(rll
);
1352 static ztest_zrl_t
*
1353 ztest_zrl_init(rl_t
*rl
, ztest_znode_t
*zp
)
1355 ztest_zrl_t
*zrl
= umem_alloc(sizeof (*zrl
), UMEM_NOFAIL
);
1357 zrl
->z_ztznode
= zp
;
1362 ztest_zrl_fini(ztest_zrl_t
*zrl
)
1364 umem_free(zrl
, sizeof (*zrl
));
1367 static ztest_zrl_t
*
1368 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1369 uint64_t size
, rl_type_t type
)
1371 ztest_znode_t
*zp
= ztest_znode_get(zd
, object
);
1372 rl_t
*rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1374 return (ztest_zrl_init(rl
, zp
));
1378 ztest_range_unlock(ztest_ds_t
*zd
, ztest_zrl_t
*zrl
)
1380 zfs_range_unlock(zrl
->z_rl
);
1381 ztest_znode_put(zd
, zrl
->z_ztznode
);
1382 ztest_zrl_fini(zrl
);
1386 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1389 zd
->zd_zilog
= dmu_objset_zil(os
);
1390 zd
->zd_shared
= szd
;
1391 dmu_objset_name(os
, zd
->zd_name
);
1394 if (zd
->zd_shared
!= NULL
)
1395 zd
->zd_shared
->zd_seq
= 0;
1397 VERIFY(rwlock_init(&zd
->zd_zilog_lock
, USYNC_THREAD
, NULL
) == 0);
1398 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1400 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1401 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1403 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1404 ztest_zll_init(&zd
->zd_range_lock
[l
]);
1408 ztest_zd_fini(ztest_ds_t
*zd
)
1412 mutex_destroy(&zd
->zd_dirobj_lock
);
1413 (void) rwlock_destroy(&zd
->zd_zilog_lock
);
1415 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1416 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1418 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1419 ztest_zll_destroy(&zd
->zd_range_lock
[l
]);
1422 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1425 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1431 * Attempt to assign tx to some transaction group.
1433 error
= dmu_tx_assign(tx
, txg_how
);
1435 if (error
== ERESTART
) {
1436 ASSERT(txg_how
== TXG_NOWAIT
);
1439 ASSERT3U(error
, ==, ENOSPC
);
1440 ztest_record_enospc(tag
);
1445 txg
= dmu_tx_get_txg(tx
);
1451 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1454 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1462 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1465 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1469 diff
|= (value
- *ip
++);
1476 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1477 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1480 bt
->bt_magic
= BT_MAGIC
;
1481 bt
->bt_objset
= dmu_objset_id(os
);
1482 bt
->bt_object
= object
;
1483 bt
->bt_dnodesize
= dnodesize
;
1484 bt
->bt_offset
= offset
;
1487 bt
->bt_crtxg
= crtxg
;
1491 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1492 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1495 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1496 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1497 ASSERT3U(bt
->bt_object
, ==, object
);
1498 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1499 ASSERT3U(bt
->bt_offset
, ==, offset
);
1500 ASSERT3U(bt
->bt_gen
, <=, gen
);
1501 ASSERT3U(bt
->bt_txg
, <=, txg
);
1502 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1505 static ztest_block_tag_t
*
1506 ztest_bt_bonus(dmu_buf_t
*db
)
1508 dmu_object_info_t doi
;
1509 ztest_block_tag_t
*bt
;
1511 dmu_object_info_from_db(db
, &doi
);
1512 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1513 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1514 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1520 * Generate a token to fill up unused bonus buffer space. Try to make
1521 * it unique to the object, generation, and offset to verify that data
1522 * is not getting overwritten by data from other dnodes.
1524 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1525 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1528 * Fill up the unused bonus buffer region before the block tag with a
1529 * verifiable pattern. Filling the whole bonus area with non-zero data
1530 * helps ensure that all dnode traversal code properly skips the
1531 * interior regions of large dnodes.
1534 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1535 objset_t
*os
, uint64_t gen
)
1539 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1541 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1542 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1543 gen
, bonusp
- (uint64_t *)db
->db_data
);
1549 * Verify that the unused area of a bonus buffer is filled with the
1553 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1554 objset_t
*os
, uint64_t gen
)
1558 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1559 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1560 gen
, bonusp
- (uint64_t *)db
->db_data
);
1561 VERIFY3U(*bonusp
, ==, token
);
1569 #define lrz_type lr_mode
1570 #define lrz_blocksize lr_uid
1571 #define lrz_ibshift lr_gid
1572 #define lrz_bonustype lr_rdev
1573 #define lrz_dnodesize lr_crtime[1]
1576 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1578 char *name
= (void *)(lr
+ 1); /* name follows lr */
1579 size_t namesize
= strlen(name
) + 1;
1582 if (zil_replaying(zd
->zd_zilog
, tx
))
1585 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1586 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1587 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1589 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1593 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1595 char *name
= (void *)(lr
+ 1); /* name follows lr */
1596 size_t namesize
= strlen(name
) + 1;
1599 if (zil_replaying(zd
->zd_zilog
, tx
))
1602 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1603 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1604 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1606 itx
->itx_oid
= object
;
1607 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1611 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1614 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1616 if (zil_replaying(zd
->zd_zilog
, tx
))
1619 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1620 write_state
= WR_INDIRECT
;
1622 itx
= zil_itx_create(TX_WRITE
,
1623 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1625 if (write_state
== WR_COPIED
&&
1626 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1627 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1628 zil_itx_destroy(itx
);
1629 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1630 write_state
= WR_NEED_COPY
;
1632 itx
->itx_private
= zd
;
1633 itx
->itx_wr_state
= write_state
;
1634 itx
->itx_sync
= (ztest_random(8) == 0);
1636 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1637 sizeof (*lr
) - sizeof (lr_t
));
1639 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1643 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1647 if (zil_replaying(zd
->zd_zilog
, tx
))
1650 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1651 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1652 sizeof (*lr
) - sizeof (lr_t
));
1654 itx
->itx_sync
= B_FALSE
;
1655 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1659 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1663 if (zil_replaying(zd
->zd_zilog
, tx
))
1666 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1667 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1668 sizeof (*lr
) - sizeof (lr_t
));
1670 itx
->itx_sync
= B_FALSE
;
1671 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1678 ztest_replay_create(ztest_ds_t
*zd
, lr_create_t
*lr
, boolean_t byteswap
)
1680 char *name
= (void *)(lr
+ 1); /* name follows lr */
1681 objset_t
*os
= zd
->zd_os
;
1682 ztest_block_tag_t
*bbt
;
1690 byteswap_uint64_array(lr
, sizeof (*lr
));
1692 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1693 ASSERT(name
[0] != '\0');
1695 tx
= dmu_tx_create(os
);
1697 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1699 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1700 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1702 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1705 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1709 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1710 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1712 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1713 if (lr
->lr_foid
== 0) {
1714 lr
->lr_foid
= zap_create_dnsize(os
,
1715 lr
->lrz_type
, lr
->lrz_bonustype
,
1716 bonuslen
, lr
->lrz_dnodesize
, tx
);
1718 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1719 lr
->lrz_type
, lr
->lrz_bonustype
,
1720 bonuslen
, lr
->lrz_dnodesize
, tx
);
1723 if (lr
->lr_foid
== 0) {
1724 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1725 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1726 bonuslen
, lr
->lrz_dnodesize
, tx
);
1728 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1729 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1730 bonuslen
, lr
->lrz_dnodesize
, tx
);
1735 ASSERT3U(error
, ==, EEXIST
);
1736 ASSERT(zd
->zd_zilog
->zl_replay
);
1741 ASSERT(lr
->lr_foid
!= 0);
1743 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1744 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1745 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1747 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1748 bbt
= ztest_bt_bonus(db
);
1749 dmu_buf_will_dirty(db
, tx
);
1750 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1751 lr
->lr_gen
, txg
, txg
);
1752 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1753 dmu_buf_rele(db
, FTAG
);
1755 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1758 (void) ztest_log_create(zd
, tx
, lr
);
1766 ztest_replay_remove(ztest_ds_t
*zd
, lr_remove_t
*lr
, boolean_t byteswap
)
1768 char *name
= (void *)(lr
+ 1); /* name follows lr */
1769 objset_t
*os
= zd
->zd_os
;
1770 dmu_object_info_t doi
;
1772 uint64_t object
, txg
;
1775 byteswap_uint64_array(lr
, sizeof (*lr
));
1777 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1778 ASSERT(name
[0] != '\0');
1781 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1782 ASSERT(object
!= 0);
1784 ztest_object_lock(zd
, object
, RL_WRITER
);
1786 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1788 tx
= dmu_tx_create(os
);
1790 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1791 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1793 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1795 ztest_object_unlock(zd
, object
);
1799 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1800 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1802 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1805 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1807 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1811 ztest_object_unlock(zd
, object
);
1817 ztest_replay_write(ztest_ds_t
*zd
, lr_write_t
*lr
, boolean_t byteswap
)
1819 objset_t
*os
= zd
->zd_os
;
1820 void *data
= lr
+ 1; /* data follows lr */
1821 uint64_t offset
, length
;
1822 ztest_block_tag_t
*bt
= data
;
1823 ztest_block_tag_t
*bbt
;
1824 uint64_t gen
, txg
, lrtxg
, crtxg
;
1825 dmu_object_info_t doi
;
1828 arc_buf_t
*abuf
= NULL
;
1832 byteswap_uint64_array(lr
, sizeof (*lr
));
1834 offset
= lr
->lr_offset
;
1835 length
= lr
->lr_length
;
1837 /* If it's a dmu_sync() block, write the whole block */
1838 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1839 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1840 if (length
< blocksize
) {
1841 offset
-= offset
% blocksize
;
1846 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1847 byteswap_uint64_array(bt
, sizeof (*bt
));
1849 if (bt
->bt_magic
!= BT_MAGIC
)
1852 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1853 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1855 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1857 dmu_object_info_from_db(db
, &doi
);
1859 bbt
= ztest_bt_bonus(db
);
1860 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1862 crtxg
= bbt
->bt_crtxg
;
1863 lrtxg
= lr
->lr_common
.lrc_txg
;
1865 tx
= dmu_tx_create(os
);
1867 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1869 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1870 P2PHASE(offset
, length
) == 0)
1871 abuf
= dmu_request_arcbuf(db
, length
);
1873 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1876 dmu_return_arcbuf(abuf
);
1877 dmu_buf_rele(db
, FTAG
);
1878 ztest_range_unlock(zd
, rl
);
1879 ztest_object_unlock(zd
, lr
->lr_foid
);
1885 * Usually, verify the old data before writing new data --
1886 * but not always, because we also want to verify correct
1887 * behavior when the data was not recently read into cache.
1889 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1890 if (ztest_random(4) != 0) {
1891 int prefetch
= ztest_random(2) ?
1892 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1893 ztest_block_tag_t rbt
;
1895 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1896 sizeof (rbt
), &rbt
, prefetch
) == 0);
1897 if (rbt
.bt_magic
== BT_MAGIC
) {
1898 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1899 offset
, gen
, txg
, crtxg
);
1904 * Writes can appear to be newer than the bonus buffer because
1905 * the ztest_get_data() callback does a dmu_read() of the
1906 * open-context data, which may be different than the data
1907 * as it was when the write was generated.
1909 if (zd
->zd_zilog
->zl_replay
) {
1910 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1911 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1916 * Set the bt's gen/txg to the bonus buffer's gen/txg
1917 * so that all of the usual ASSERTs will work.
1919 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
1924 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
1926 bcopy(data
, abuf
->b_data
, length
);
1927 dmu_assign_arcbuf(db
, offset
, abuf
, tx
);
1930 (void) ztest_log_write(zd
, tx
, lr
);
1932 dmu_buf_rele(db
, FTAG
);
1936 ztest_range_unlock(zd
, rl
);
1937 ztest_object_unlock(zd
, lr
->lr_foid
);
1943 ztest_replay_truncate(ztest_ds_t
*zd
, lr_truncate_t
*lr
, boolean_t byteswap
)
1945 objset_t
*os
= zd
->zd_os
;
1951 byteswap_uint64_array(lr
, sizeof (*lr
));
1953 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1954 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1957 tx
= dmu_tx_create(os
);
1959 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
1961 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1963 ztest_range_unlock(zd
, rl
);
1964 ztest_object_unlock(zd
, lr
->lr_foid
);
1968 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
1969 lr
->lr_length
, tx
) == 0);
1971 (void) ztest_log_truncate(zd
, tx
, lr
);
1975 ztest_range_unlock(zd
, rl
);
1976 ztest_object_unlock(zd
, lr
->lr_foid
);
1982 ztest_replay_setattr(ztest_ds_t
*zd
, lr_setattr_t
*lr
, boolean_t byteswap
)
1984 objset_t
*os
= zd
->zd_os
;
1987 ztest_block_tag_t
*bbt
;
1988 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
1991 byteswap_uint64_array(lr
, sizeof (*lr
));
1993 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
1995 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1997 tx
= dmu_tx_create(os
);
1998 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2000 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2002 dmu_buf_rele(db
, FTAG
);
2003 ztest_object_unlock(zd
, lr
->lr_foid
);
2007 bbt
= ztest_bt_bonus(db
);
2008 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2009 crtxg
= bbt
->bt_crtxg
;
2010 lrtxg
= lr
->lr_common
.lrc_txg
;
2011 dnodesize
= bbt
->bt_dnodesize
;
2013 if (zd
->zd_zilog
->zl_replay
) {
2014 ASSERT(lr
->lr_size
!= 0);
2015 ASSERT(lr
->lr_mode
!= 0);
2019 * Randomly change the size and increment the generation.
2021 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2023 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2028 * Verify that the current bonus buffer is not newer than our txg.
2030 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2031 MAX(txg
, lrtxg
), crtxg
);
2033 dmu_buf_will_dirty(db
, tx
);
2035 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2036 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2037 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2038 bbt
= ztest_bt_bonus(db
);
2040 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2042 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2043 dmu_buf_rele(db
, FTAG
);
2045 (void) ztest_log_setattr(zd
, tx
, lr
);
2049 ztest_object_unlock(zd
, lr
->lr_foid
);
2054 zil_replay_func_t ztest_replay_vector
[TX_MAX_TYPE
] = {
2055 NULL
, /* 0 no such transaction type */
2056 (zil_replay_func_t
)ztest_replay_create
, /* TX_CREATE */
2057 NULL
, /* TX_MKDIR */
2058 NULL
, /* TX_MKXATTR */
2059 NULL
, /* TX_SYMLINK */
2060 (zil_replay_func_t
)ztest_replay_remove
, /* TX_REMOVE */
2061 NULL
, /* TX_RMDIR */
2063 NULL
, /* TX_RENAME */
2064 (zil_replay_func_t
)ztest_replay_write
, /* TX_WRITE */
2065 (zil_replay_func_t
)ztest_replay_truncate
, /* TX_TRUNCATE */
2066 (zil_replay_func_t
)ztest_replay_setattr
, /* TX_SETATTR */
2068 NULL
, /* TX_CREATE_ACL */
2069 NULL
, /* TX_CREATE_ATTR */
2070 NULL
, /* TX_CREATE_ACL_ATTR */
2071 NULL
, /* TX_MKDIR_ACL */
2072 NULL
, /* TX_MKDIR_ATTR */
2073 NULL
, /* TX_MKDIR_ACL_ATTR */
2074 NULL
, /* TX_WRITE2 */
2078 * ZIL get_data callbacks
2080 typedef struct ztest_zgd_private
{
2084 } ztest_zgd_private_t
;
2087 ztest_get_done(zgd_t
*zgd
, int error
)
2089 ztest_zgd_private_t
*zzp
= zgd
->zgd_private
;
2090 ztest_ds_t
*zd
= zzp
->z_zd
;
2091 uint64_t object
= zzp
->z_object
;
2094 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2096 ztest_range_unlock(zd
, zzp
->z_rl
);
2097 ztest_object_unlock(zd
, object
);
2099 if (error
== 0 && zgd
->zgd_bp
)
2100 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
2102 umem_free(zgd
, sizeof (*zgd
));
2103 umem_free(zzp
, sizeof (*zzp
));
2107 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
2109 ztest_ds_t
*zd
= arg
;
2110 objset_t
*os
= zd
->zd_os
;
2111 uint64_t object
= lr
->lr_foid
;
2112 uint64_t offset
= lr
->lr_offset
;
2113 uint64_t size
= lr
->lr_length
;
2114 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2116 dmu_object_info_t doi
;
2120 ztest_zgd_private_t
*zgd_private
;
2122 ztest_object_lock(zd
, object
, RL_READER
);
2123 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2125 ztest_object_unlock(zd
, object
);
2129 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2131 if (crtxg
== 0 || crtxg
> txg
) {
2132 dmu_buf_rele(db
, FTAG
);
2133 ztest_object_unlock(zd
, object
);
2137 dmu_object_info_from_db(db
, &doi
);
2138 dmu_buf_rele(db
, FTAG
);
2141 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2142 zgd
->zgd_zilog
= zd
->zd_zilog
;
2143 zgd_private
= umem_zalloc(sizeof (ztest_zgd_private_t
), UMEM_NOFAIL
);
2144 zgd_private
->z_zd
= zd
;
2145 zgd_private
->z_object
= object
;
2146 zgd
->zgd_private
= zgd_private
;
2148 if (buf
!= NULL
) { /* immediate write */
2149 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2151 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2153 error
= dmu_read(os
, object
, offset
, size
, buf
,
2154 DMU_READ_NO_PREFETCH
);
2157 size
= doi
.doi_data_block_size
;
2159 offset
= P2ALIGN(offset
, size
);
2161 ASSERT(offset
< size
);
2165 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2167 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2169 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2170 DMU_READ_NO_PREFETCH
);
2173 blkptr_t
*bp
= &lr
->lr_blkptr
;
2178 ASSERT(db
->db_offset
== offset
);
2179 ASSERT(db
->db_size
== size
);
2181 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2182 ztest_get_done
, zgd
);
2189 ztest_get_done(zgd
, error
);
2195 ztest_lr_alloc(size_t lrsize
, char *name
)
2198 size_t namesize
= name
? strlen(name
) + 1 : 0;
2200 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2203 bcopy(name
, lr
+ lrsize
, namesize
);
2209 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2211 size_t namesize
= name
? strlen(name
) + 1 : 0;
2213 umem_free(lr
, lrsize
+ namesize
);
2217 * Lookup a bunch of objects. Returns the number of objects not found.
2220 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2226 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2228 for (i
= 0; i
< count
; i
++, od
++) {
2230 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2231 sizeof (uint64_t), 1, &od
->od_object
);
2233 ASSERT(error
== ENOENT
);
2234 ASSERT(od
->od_object
== 0);
2238 ztest_block_tag_t
*bbt
;
2239 dmu_object_info_t doi
;
2241 ASSERT(od
->od_object
!= 0);
2242 ASSERT(missing
== 0); /* there should be no gaps */
2244 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2245 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2246 od
->od_object
, FTAG
, &db
));
2247 dmu_object_info_from_db(db
, &doi
);
2248 bbt
= ztest_bt_bonus(db
);
2249 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2250 od
->od_type
= doi
.doi_type
;
2251 od
->od_blocksize
= doi
.doi_data_block_size
;
2252 od
->od_gen
= bbt
->bt_gen
;
2253 dmu_buf_rele(db
, FTAG
);
2254 ztest_object_unlock(zd
, od
->od_object
);
2262 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2267 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2269 for (i
= 0; i
< count
; i
++, od
++) {
2276 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2278 lr
->lr_doid
= od
->od_dir
;
2279 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2280 lr
->lrz_type
= od
->od_crtype
;
2281 lr
->lrz_blocksize
= od
->od_crblocksize
;
2282 lr
->lrz_ibshift
= ztest_random_ibshift();
2283 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2284 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2285 lr
->lr_gen
= od
->od_crgen
;
2286 lr
->lr_crtime
[0] = time(NULL
);
2288 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2289 ASSERT(missing
== 0);
2293 od
->od_object
= lr
->lr_foid
;
2294 od
->od_type
= od
->od_crtype
;
2295 od
->od_blocksize
= od
->od_crblocksize
;
2296 od
->od_gen
= od
->od_crgen
;
2297 ASSERT(od
->od_object
!= 0);
2300 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2307 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2313 ASSERT(mutex_held(&zd
->zd_dirobj_lock
));
2317 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2324 * No object was found.
2326 if (od
->od_object
== 0)
2329 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2331 lr
->lr_doid
= od
->od_dir
;
2333 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2334 ASSERT3U(error
, ==, ENOSPC
);
2339 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2346 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2352 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2354 lr
->lr_foid
= object
;
2355 lr
->lr_offset
= offset
;
2356 lr
->lr_length
= size
;
2358 BP_ZERO(&lr
->lr_blkptr
);
2360 bcopy(data
, lr
+ 1, size
);
2362 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2364 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2370 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2375 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2377 lr
->lr_foid
= object
;
2378 lr
->lr_offset
= offset
;
2379 lr
->lr_length
= size
;
2381 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2383 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2389 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2394 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2396 lr
->lr_foid
= object
;
2400 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2402 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2408 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2410 objset_t
*os
= zd
->zd_os
;
2415 txg_wait_synced(dmu_objset_pool(os
), 0);
2417 ztest_object_lock(zd
, object
, RL_READER
);
2418 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2420 tx
= dmu_tx_create(os
);
2422 dmu_tx_hold_write(tx
, object
, offset
, size
);
2424 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2427 dmu_prealloc(os
, object
, offset
, size
, tx
);
2429 txg_wait_synced(dmu_objset_pool(os
), txg
);
2431 (void) dmu_free_long_range(os
, object
, offset
, size
);
2434 ztest_range_unlock(zd
, rl
);
2435 ztest_object_unlock(zd
, object
);
2439 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2442 ztest_block_tag_t wbt
;
2443 dmu_object_info_t doi
;
2444 enum ztest_io_type io_type
;
2448 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2449 blocksize
= doi
.doi_data_block_size
;
2450 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2453 * Pick an i/o type at random, biased toward writing block tags.
2455 io_type
= ztest_random(ZTEST_IO_TYPES
);
2456 if (ztest_random(2) == 0)
2457 io_type
= ZTEST_IO_WRITE_TAG
;
2459 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2463 case ZTEST_IO_WRITE_TAG
:
2464 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2466 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2469 case ZTEST_IO_WRITE_PATTERN
:
2470 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2471 if (ztest_random(2) == 0) {
2473 * Induce fletcher2 collisions to ensure that
2474 * zio_ddt_collision() detects and resolves them
2475 * when using fletcher2-verify for deduplication.
2477 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2478 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2480 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2483 case ZTEST_IO_WRITE_ZEROES
:
2484 bzero(data
, blocksize
);
2485 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2488 case ZTEST_IO_TRUNCATE
:
2489 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2492 case ZTEST_IO_SETATTR
:
2493 (void) ztest_setattr(zd
, object
);
2498 case ZTEST_IO_REWRITE
:
2499 (void) rw_rdlock(&ztest_name_lock
);
2500 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2501 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2503 VERIFY(err
== 0 || err
== ENOSPC
);
2504 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2505 ZFS_PROP_COMPRESSION
,
2506 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2508 VERIFY(err
== 0 || err
== ENOSPC
);
2509 (void) rw_unlock(&ztest_name_lock
);
2511 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2512 DMU_READ_NO_PREFETCH
));
2514 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2518 (void) rw_unlock(&zd
->zd_zilog_lock
);
2520 umem_free(data
, blocksize
);
2524 * Initialize an object description template.
2527 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2528 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2531 od
->od_dir
= ZTEST_DIROBJ
;
2534 od
->od_crtype
= type
;
2535 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2536 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2539 od
->od_type
= DMU_OT_NONE
;
2540 od
->od_blocksize
= 0;
2543 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2544 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2548 * Lookup or create the objects for a test using the od template.
2549 * If the objects do not all exist, or if 'remove' is specified,
2550 * remove any existing objects and create new ones. Otherwise,
2551 * use the existing objects.
2554 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2556 int count
= size
/ sizeof (*od
);
2559 mutex_enter(&zd
->zd_dirobj_lock
);
2560 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2561 (ztest_remove(zd
, od
, count
) != 0 ||
2562 ztest_create(zd
, od
, count
) != 0))
2565 mutex_exit(&zd
->zd_dirobj_lock
);
2572 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2574 zilog_t
*zilog
= zd
->zd_zilog
;
2576 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2578 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2581 * Remember the committed values in zd, which is in parent/child
2582 * shared memory. If we die, the next iteration of ztest_run()
2583 * will verify that the log really does contain this record.
2585 mutex_enter(&zilog
->zl_lock
);
2586 ASSERT(zd
->zd_shared
!= NULL
);
2587 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2588 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2589 mutex_exit(&zilog
->zl_lock
);
2591 (void) rw_unlock(&zd
->zd_zilog_lock
);
2595 * This function is designed to simulate the operations that occur during a
2596 * mount/unmount operation. We hold the dataset across these operations in an
2597 * attempt to expose any implicit assumptions about ZIL management.
2601 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2603 objset_t
*os
= zd
->zd_os
;
2606 * We grab the zd_dirobj_lock to ensure that no other thread is
2607 * updating the zil (i.e. adding in-memory log records) and the
2608 * zd_zilog_lock to block any I/O.
2610 mutex_enter(&zd
->zd_dirobj_lock
);
2611 (void) rw_wrlock(&zd
->zd_zilog_lock
);
2613 /* zfsvfs_teardown() */
2614 zil_close(zd
->zd_zilog
);
2616 /* zfsvfs_setup() */
2617 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2618 zil_replay(os
, zd
, ztest_replay_vector
);
2620 (void) rw_unlock(&zd
->zd_zilog_lock
);
2621 mutex_exit(&zd
->zd_dirobj_lock
);
2625 * Verify that we can't destroy an active pool, create an existing pool,
2626 * or create a pool with a bad vdev spec.
2630 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2632 ztest_shared_opts_t
*zo
= &ztest_opts
;
2636 if (zo
->zo_mmp_test
)
2640 * Attempt to create using a bad file.
2642 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2643 VERIFY3U(ENOENT
, ==,
2644 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
));
2645 nvlist_free(nvroot
);
2648 * Attempt to create using a bad mirror.
2650 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 2, 1);
2651 VERIFY3U(ENOENT
, ==,
2652 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
));
2653 nvlist_free(nvroot
);
2656 * Attempt to create an existing pool. It shouldn't matter
2657 * what's in the nvroot; we should fail with EEXIST.
2659 (void) rw_rdlock(&ztest_name_lock
);
2660 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2661 VERIFY3U(EEXIST
, ==, spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
));
2662 nvlist_free(nvroot
);
2663 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2664 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2665 spa_close(spa
, FTAG
);
2667 (void) rw_unlock(&ztest_name_lock
);
2671 * Start and then stop the MMP threads to ensure the startup and shutdown code
2672 * works properly. Actual protection and property-related code tested via ZTS.
2676 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2678 ztest_shared_opts_t
*zo
= &ztest_opts
;
2679 spa_t
*spa
= ztest_spa
;
2681 if (zo
->zo_mmp_test
)
2684 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2685 mutex_enter(&spa
->spa_props_lock
);
2687 if (!spa_multihost(spa
)) {
2688 spa
->spa_multihost
= B_TRUE
;
2689 mmp_thread_start(spa
);
2692 mutex_exit(&spa
->spa_props_lock
);
2693 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2695 txg_wait_synced(spa_get_dsl(spa
), 0);
2696 mmp_signal_all_threads();
2697 txg_wait_synced(spa_get_dsl(spa
), 0);
2699 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2700 mutex_enter(&spa
->spa_props_lock
);
2702 if (spa_multihost(spa
)) {
2703 mmp_thread_stop(spa
);
2704 spa
->spa_multihost
= B_FALSE
;
2707 mutex_exit(&spa
->spa_props_lock
);
2708 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2713 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2716 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2717 uint64_t version
, newversion
;
2718 nvlist_t
*nvroot
, *props
;
2721 if (ztest_opts
.zo_mmp_test
)
2724 mutex_enter(&ztest_vdev_lock
);
2725 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2728 * Clean up from previous runs.
2730 (void) spa_destroy(name
);
2732 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2733 0, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2736 * If we're configuring a RAIDZ device then make sure that the
2737 * the initial version is capable of supporting that feature.
2739 switch (ztest_opts
.zo_raidz_parity
) {
2742 initial_version
= SPA_VERSION_INITIAL
;
2745 initial_version
= SPA_VERSION_RAIDZ2
;
2748 initial_version
= SPA_VERSION_RAIDZ3
;
2753 * Create a pool with a spa version that can be upgraded. Pick
2754 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2757 version
= ztest_random_spa_version(initial_version
);
2758 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2760 props
= fnvlist_alloc();
2761 fnvlist_add_uint64(props
,
2762 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2763 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
), ==, 0);
2764 fnvlist_free(nvroot
);
2765 fnvlist_free(props
);
2767 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2768 VERIFY3U(spa_version(spa
), ==, version
);
2769 newversion
= ztest_random_spa_version(version
+ 1);
2771 if (ztest_opts
.zo_verbose
>= 4) {
2772 (void) printf("upgrading spa version from %llu to %llu\n",
2773 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2776 spa_upgrade(spa
, newversion
);
2777 VERIFY3U(spa_version(spa
), >, version
);
2778 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2779 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2780 spa_close(spa
, FTAG
);
2783 mutex_exit(&ztest_vdev_lock
);
2787 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2792 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2795 for (c
= 0; c
< vd
->vdev_children
; c
++)
2796 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2804 * Find the first available hole which can be used as a top-level.
2807 find_vdev_hole(spa_t
*spa
)
2809 vdev_t
*rvd
= spa
->spa_root_vdev
;
2812 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2814 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2815 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2817 if (cvd
->vdev_ishole
)
2824 * Verify that vdev_add() works as expected.
2828 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2830 ztest_shared_t
*zs
= ztest_shared
;
2831 spa_t
*spa
= ztest_spa
;
2837 if (ztest_opts
.zo_mmp_test
)
2840 mutex_enter(&ztest_vdev_lock
);
2841 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2843 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2845 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2848 * If we have slogs then remove them 1/4 of the time.
2850 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2852 * Grab the guid from the head of the log class rotor.
2854 guid
= spa_log_class(spa
)->mc_rotor
->mg_vd
->vdev_guid
;
2856 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2859 * We have to grab the zs_name_lock as writer to
2860 * prevent a race between removing a slog (dmu_objset_find)
2861 * and destroying a dataset. Removing the slog will
2862 * grab a reference on the dataset which may cause
2863 * dsl_destroy_head() to fail with EBUSY thus
2864 * leaving the dataset in an inconsistent state.
2866 rw_wrlock(&ztest_name_lock
);
2867 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2868 rw_unlock(&ztest_name_lock
);
2870 if (error
&& error
!= EEXIST
)
2871 fatal(0, "spa_vdev_remove() = %d", error
);
2873 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2876 * Make 1/4 of the devices be log devices.
2878 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
2879 ztest_opts
.zo_vdev_size
, 0,
2880 ztest_random(4) == 0, ztest_opts
.zo_raidz
,
2883 error
= spa_vdev_add(spa
, nvroot
);
2884 nvlist_free(nvroot
);
2886 if (error
== ENOSPC
)
2887 ztest_record_enospc("spa_vdev_add");
2888 else if (error
!= 0)
2889 fatal(0, "spa_vdev_add() = %d", error
);
2892 mutex_exit(&ztest_vdev_lock
);
2896 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2900 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2902 ztest_shared_t
*zs
= ztest_shared
;
2903 spa_t
*spa
= ztest_spa
;
2904 vdev_t
*rvd
= spa
->spa_root_vdev
;
2905 spa_aux_vdev_t
*sav
;
2911 if (ztest_opts
.zo_mmp_test
)
2914 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
2916 if (ztest_random(2) == 0) {
2917 sav
= &spa
->spa_spares
;
2918 aux
= ZPOOL_CONFIG_SPARES
;
2920 sav
= &spa
->spa_l2cache
;
2921 aux
= ZPOOL_CONFIG_L2CACHE
;
2924 mutex_enter(&ztest_vdev_lock
);
2926 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2928 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
2930 * Pick a random device to remove.
2932 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
2935 * Find an unused device we can add.
2937 zs
->zs_vdev_aux
= 0;
2940 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
2941 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
2943 for (c
= 0; c
< sav
->sav_count
; c
++)
2944 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
2947 if (c
== sav
->sav_count
&&
2948 vdev_lookup_by_path(rvd
, path
) == NULL
)
2954 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2960 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
2961 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, 0, 0, 0, 1);
2962 error
= spa_vdev_add(spa
, nvroot
);
2964 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
2965 nvlist_free(nvroot
);
2968 * Remove an existing device. Sometimes, dirty its
2969 * vdev state first to make sure we handle removal
2970 * of devices that have pending state changes.
2972 if (ztest_random(2) == 0)
2973 (void) vdev_online(spa
, guid
, 0, NULL
);
2975 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2976 if (error
!= 0 && error
!= EBUSY
)
2977 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
2980 mutex_exit(&ztest_vdev_lock
);
2982 umem_free(path
, MAXPATHLEN
);
2986 * split a pool if it has mirror tlvdevs
2990 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
2992 ztest_shared_t
*zs
= ztest_shared
;
2993 spa_t
*spa
= ztest_spa
;
2994 vdev_t
*rvd
= spa
->spa_root_vdev
;
2995 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
2996 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
2999 if (ztest_opts
.zo_mmp_test
)
3002 mutex_enter(&ztest_vdev_lock
);
3004 /* ensure we have a useable config; mirrors of raidz aren't supported */
3005 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3006 mutex_exit(&ztest_vdev_lock
);
3010 /* clean up the old pool, if any */
3011 (void) spa_destroy("splitp");
3013 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3015 /* generate a config from the existing config */
3016 mutex_enter(&spa
->spa_props_lock
);
3017 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3019 mutex_exit(&spa
->spa_props_lock
);
3021 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3024 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3025 for (c
= 0; c
< children
; c
++) {
3026 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3030 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3031 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3033 VERIFY(nvlist_add_string(schild
[schildren
],
3034 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3035 VERIFY(nvlist_add_uint64(schild
[schildren
],
3036 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3038 lastlogid
= schildren
;
3043 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3044 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3045 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3048 /* OK, create a config that can be used to split */
3049 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3050 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3051 VDEV_TYPE_ROOT
) == 0);
3052 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3053 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3055 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3056 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3058 for (c
= 0; c
< schildren
; c
++)
3059 nvlist_free(schild
[c
]);
3063 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3065 (void) rw_wrlock(&ztest_name_lock
);
3066 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3067 (void) rw_unlock(&ztest_name_lock
);
3069 nvlist_free(config
);
3072 (void) printf("successful split - results:\n");
3073 mutex_enter(&spa_namespace_lock
);
3074 show_pool_stats(spa
);
3075 show_pool_stats(spa_lookup("splitp"));
3076 mutex_exit(&spa_namespace_lock
);
3080 mutex_exit(&ztest_vdev_lock
);
3085 * Verify that we can attach and detach devices.
3089 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3091 ztest_shared_t
*zs
= ztest_shared
;
3092 spa_t
*spa
= ztest_spa
;
3093 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3094 vdev_t
*rvd
= spa
->spa_root_vdev
;
3095 vdev_t
*oldvd
, *newvd
, *pvd
;
3099 uint64_t ashift
= ztest_get_ashift();
3100 uint64_t oldguid
, pguid
;
3101 uint64_t oldsize
, newsize
;
3102 char *oldpath
, *newpath
;
3104 int oldvd_has_siblings
= B_FALSE
;
3105 int newvd_is_spare
= B_FALSE
;
3107 int error
, expected_error
;
3109 if (ztest_opts
.zo_mmp_test
)
3112 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3113 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3115 mutex_enter(&ztest_vdev_lock
);
3116 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3118 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3121 * Decide whether to do an attach or a replace.
3123 replacing
= ztest_random(2);
3126 * Pick a random top-level vdev.
3128 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3131 * Pick a random leaf within it.
3133 leaf
= ztest_random(leaves
);
3138 oldvd
= rvd
->vdev_child
[top
];
3139 if (zs
->zs_mirrors
>= 1) {
3140 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3141 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3142 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3144 if (ztest_opts
.zo_raidz
> 1) {
3145 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3146 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3147 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3151 * If we're already doing an attach or replace, oldvd may be a
3152 * mirror vdev -- in which case, pick a random child.
3154 while (oldvd
->vdev_children
!= 0) {
3155 oldvd_has_siblings
= B_TRUE
;
3156 ASSERT(oldvd
->vdev_children
>= 2);
3157 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3160 oldguid
= oldvd
->vdev_guid
;
3161 oldsize
= vdev_get_min_asize(oldvd
);
3162 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3163 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3164 pvd
= oldvd
->vdev_parent
;
3165 pguid
= pvd
->vdev_guid
;
3168 * If oldvd has siblings, then half of the time, detach it.
3170 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3171 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3172 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3173 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3175 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3180 * For the new vdev, choose with equal probability between the two
3181 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3183 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3184 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3185 newvd_is_spare
= B_TRUE
;
3186 (void) strcpy(newpath
, newvd
->vdev_path
);
3188 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3189 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3190 top
* leaves
+ leaf
);
3191 if (ztest_random(2) == 0)
3192 newpath
[strlen(newpath
) - 1] = 'b';
3193 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3197 newsize
= vdev_get_min_asize(newvd
);
3200 * Make newsize a little bigger or smaller than oldsize.
3201 * If it's smaller, the attach should fail.
3202 * If it's larger, and we're doing a replace,
3203 * we should get dynamic LUN growth when we're done.
3205 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3209 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3210 * unless it's a replace; in that case any non-replacing parent is OK.
3212 * If newvd is already part of the pool, it should fail with EBUSY.
3214 * If newvd is too small, it should fail with EOVERFLOW.
3216 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3217 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3218 pvd
->vdev_ops
== &vdev_replacing_ops
||
3219 pvd
->vdev_ops
== &vdev_spare_ops
))
3220 expected_error
= ENOTSUP
;
3221 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3222 expected_error
= ENOTSUP
;
3223 else if (newvd
== oldvd
)
3224 expected_error
= replacing
? 0 : EBUSY
;
3225 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3226 expected_error
= EBUSY
;
3227 else if (newsize
< oldsize
)
3228 expected_error
= EOVERFLOW
;
3229 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3230 expected_error
= EDOM
;
3234 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3237 * Build the nvlist describing newpath.
3239 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3240 ashift
, 0, 0, 0, 1);
3242 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3247 * If our parent was the replacing vdev, but the replace completed,
3248 * then instead of failing with ENOTSUP we may either succeed,
3249 * fail with ENODEV, or fail with EOVERFLOW.
3251 if (expected_error
== ENOTSUP
&&
3252 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3253 expected_error
= error
;
3256 * If someone grew the LUN, the replacement may be too small.
3258 if (error
== EOVERFLOW
|| error
== EBUSY
)
3259 expected_error
= error
;
3261 /* XXX workaround 6690467 */
3262 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3263 fatal(0, "attach (%s %llu, %s %llu, %d) "
3264 "returned %d, expected %d",
3265 oldpath
, oldsize
, newpath
,
3266 newsize
, replacing
, error
, expected_error
);
3269 mutex_exit(&ztest_vdev_lock
);
3271 umem_free(oldpath
, MAXPATHLEN
);
3272 umem_free(newpath
, MAXPATHLEN
);
3276 * Callback function which expands the physical size of the vdev.
3279 grow_vdev(vdev_t
*vd
, void *arg
)
3281 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3282 size_t *newsize
= arg
;
3286 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3287 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3289 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3292 fsize
= lseek(fd
, 0, SEEK_END
);
3293 VERIFY(ftruncate(fd
, *newsize
) == 0);
3295 if (ztest_opts
.zo_verbose
>= 6) {
3296 (void) printf("%s grew from %lu to %lu bytes\n",
3297 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3304 * Callback function which expands a given vdev by calling vdev_online().
3308 online_vdev(vdev_t
*vd
, void *arg
)
3310 spa_t
*spa
= vd
->vdev_spa
;
3311 vdev_t
*tvd
= vd
->vdev_top
;
3312 uint64_t guid
= vd
->vdev_guid
;
3313 uint64_t generation
= spa
->spa_config_generation
+ 1;
3314 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3317 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3318 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3320 /* Calling vdev_online will initialize the new metaslabs */
3321 spa_config_exit(spa
, SCL_STATE
, spa
);
3322 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3323 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3326 * If vdev_online returned an error or the underlying vdev_open
3327 * failed then we abort the expand. The only way to know that
3328 * vdev_open fails is by checking the returned newstate.
3330 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3331 if (ztest_opts
.zo_verbose
>= 5) {
3332 (void) printf("Unable to expand vdev, state %llu, "
3333 "error %d\n", (u_longlong_t
)newstate
, error
);
3337 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3340 * Since we dropped the lock we need to ensure that we're
3341 * still talking to the original vdev. It's possible this
3342 * vdev may have been detached/replaced while we were
3343 * trying to online it.
3345 if (generation
!= spa
->spa_config_generation
) {
3346 if (ztest_opts
.zo_verbose
>= 5) {
3347 (void) printf("vdev configuration has changed, "
3348 "guid %llu, state %llu, expected gen %llu, "
3351 (u_longlong_t
)tvd
->vdev_state
,
3352 (u_longlong_t
)generation
,
3353 (u_longlong_t
)spa
->spa_config_generation
);
3361 * Traverse the vdev tree calling the supplied function.
3362 * We continue to walk the tree until we either have walked all
3363 * children or we receive a non-NULL return from the callback.
3364 * If a NULL callback is passed, then we just return back the first
3365 * leaf vdev we encounter.
3368 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3372 if (vd
->vdev_ops
->vdev_op_leaf
) {
3376 return (func(vd
, arg
));
3379 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3380 vdev_t
*cvd
= vd
->vdev_child
[c
];
3381 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3388 * Verify that dynamic LUN growth works as expected.
3392 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3394 spa_t
*spa
= ztest_spa
;
3396 metaslab_class_t
*mc
;
3397 metaslab_group_t
*mg
;
3398 size_t psize
, newsize
;
3400 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3402 mutex_enter(&ztest_vdev_lock
);
3403 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3405 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3407 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3410 old_ms_count
= tvd
->vdev_ms_count
;
3411 old_class_space
= metaslab_class_get_space(mc
);
3414 * Determine the size of the first leaf vdev associated with
3415 * our top-level device.
3417 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3418 ASSERT3P(vd
, !=, NULL
);
3419 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3421 psize
= vd
->vdev_psize
;
3424 * We only try to expand the vdev if it's healthy, less than 4x its
3425 * original size, and it has a valid psize.
3427 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3428 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3429 spa_config_exit(spa
, SCL_STATE
, spa
);
3430 mutex_exit(&ztest_vdev_lock
);
3434 newsize
= psize
+ psize
/ 8;
3435 ASSERT3U(newsize
, >, psize
);
3437 if (ztest_opts
.zo_verbose
>= 6) {
3438 (void) printf("Expanding LUN %s from %lu to %lu\n",
3439 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3443 * Growing the vdev is a two step process:
3444 * 1). expand the physical size (i.e. relabel)
3445 * 2). online the vdev to create the new metaslabs
3447 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3448 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3449 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3450 if (ztest_opts
.zo_verbose
>= 5) {
3451 (void) printf("Could not expand LUN because "
3452 "the vdev configuration changed.\n");
3454 spa_config_exit(spa
, SCL_STATE
, spa
);
3455 mutex_exit(&ztest_vdev_lock
);
3459 spa_config_exit(spa
, SCL_STATE
, spa
);
3462 * Expanding the LUN will update the config asynchronously,
3463 * thus we must wait for the async thread to complete any
3464 * pending tasks before proceeding.
3468 mutex_enter(&spa
->spa_async_lock
);
3469 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3470 mutex_exit(&spa
->spa_async_lock
);
3473 txg_wait_synced(spa_get_dsl(spa
), 0);
3474 (void) poll(NULL
, 0, 100);
3477 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3479 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3480 new_ms_count
= tvd
->vdev_ms_count
;
3481 new_class_space
= metaslab_class_get_space(mc
);
3483 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3484 if (ztest_opts
.zo_verbose
>= 5) {
3485 (void) printf("Could not verify LUN expansion due to "
3486 "intervening vdev offline or remove.\n");
3488 spa_config_exit(spa
, SCL_STATE
, spa
);
3489 mutex_exit(&ztest_vdev_lock
);
3494 * Make sure we were able to grow the vdev.
3496 if (new_ms_count
<= old_ms_count
)
3497 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3498 old_ms_count
, new_ms_count
);
3501 * Make sure we were able to grow the pool.
3503 if (new_class_space
<= old_class_space
)
3504 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3505 old_class_space
, new_class_space
);
3507 if (ztest_opts
.zo_verbose
>= 5) {
3508 char oldnumbuf
[6], newnumbuf
[6];
3510 nicenum(old_class_space
, oldnumbuf
);
3511 nicenum(new_class_space
, newnumbuf
);
3512 (void) printf("%s grew from %s to %s\n",
3513 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3516 spa_config_exit(spa
, SCL_STATE
, spa
);
3517 mutex_exit(&ztest_vdev_lock
);
3521 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3525 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3528 * Create the objects common to all ztest datasets.
3530 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3531 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3535 ztest_dataset_create(char *dsname
)
3537 uint64_t zilset
= ztest_random(100);
3538 int err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0,
3539 ztest_objset_create_cb
, NULL
);
3541 if (err
|| zilset
< 80)
3544 if (ztest_opts
.zo_verbose
>= 5)
3545 (void) printf("Setting dataset %s to sync always\n", dsname
);
3546 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3547 ZFS_SYNC_ALWAYS
, B_FALSE
));
3552 ztest_objset_destroy_cb(const char *name
, void *arg
)
3555 dmu_object_info_t doi
;
3559 * Verify that the dataset contains a directory object.
3561 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
, FTAG
, &os
));
3562 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3563 if (error
!= ENOENT
) {
3564 /* We could have crashed in the middle of destroying it */
3566 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3567 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3569 dmu_objset_disown(os
, FTAG
);
3572 * Destroy the dataset.
3574 if (strchr(name
, '@') != NULL
) {
3575 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3577 error
= dsl_destroy_head(name
);
3578 /* There could be a hold on this dataset */
3586 ztest_snapshot_create(char *osname
, uint64_t id
)
3588 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3591 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
3593 error
= dmu_objset_snapshot_one(osname
, snapname
);
3594 if (error
== ENOSPC
) {
3595 ztest_record_enospc(FTAG
);
3598 if (error
!= 0 && error
!= EEXIST
) {
3599 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
3606 ztest_snapshot_destroy(char *osname
, uint64_t id
)
3608 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3611 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
3614 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
3615 if (error
!= 0 && error
!= ENOENT
)
3616 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
3622 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3628 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3632 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
3634 (void) rw_rdlock(&ztest_name_lock
);
3636 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
3637 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
3640 * If this dataset exists from a previous run, process its replay log
3641 * half of the time. If we don't replay it, then dsl_destroy_head()
3642 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3644 if (ztest_random(2) == 0 &&
3645 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
) == 0) {
3646 ztest_zd_init(zdtmp
, NULL
, os
);
3647 zil_replay(os
, zdtmp
, ztest_replay_vector
);
3648 ztest_zd_fini(zdtmp
);
3649 dmu_objset_disown(os
, FTAG
);
3653 * There may be an old instance of the dataset we're about to
3654 * create lying around from a previous run. If so, destroy it
3655 * and all of its snapshots.
3657 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
3658 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
3661 * Verify that the destroyed dataset is no longer in the namespace.
3663 VERIFY3U(ENOENT
, ==, dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3667 * Verify that we can create a new dataset.
3669 error
= ztest_dataset_create(name
);
3671 if (error
== ENOSPC
) {
3672 ztest_record_enospc(FTAG
);
3675 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
3678 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os
));
3680 ztest_zd_init(zdtmp
, NULL
, os
);
3683 * Open the intent log for it.
3685 zilog
= zil_open(os
, ztest_get_data
);
3688 * Put some objects in there, do a little I/O to them,
3689 * and randomly take a couple of snapshots along the way.
3691 iters
= ztest_random(5);
3692 for (i
= 0; i
< iters
; i
++) {
3693 ztest_dmu_object_alloc_free(zdtmp
, id
);
3694 if (ztest_random(iters
) == 0)
3695 (void) ztest_snapshot_create(name
, i
);
3699 * Verify that we cannot create an existing dataset.
3701 VERIFY3U(EEXIST
, ==,
3702 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
));
3705 * Verify that we can hold an objset that is also owned.
3707 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
3708 dmu_objset_rele(os2
, FTAG
);
3711 * Verify that we cannot own an objset that is already owned.
3714 dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, FTAG
, &os2
));
3717 dmu_objset_disown(os
, FTAG
);
3718 ztest_zd_fini(zdtmp
);
3720 (void) rw_unlock(&ztest_name_lock
);
3722 umem_free(zdtmp
, sizeof (ztest_ds_t
));
3726 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3729 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3731 (void) rw_rdlock(&ztest_name_lock
);
3732 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
3733 (void) ztest_snapshot_create(zd
->zd_name
, id
);
3734 (void) rw_unlock(&ztest_name_lock
);
3738 * Cleanup non-standard snapshots and clones.
3741 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
3750 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3751 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3752 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3753 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3754 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3756 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3757 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3758 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3759 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3760 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3761 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3762 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3763 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3764 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3765 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3767 error
= dsl_destroy_head(clone2name
);
3768 if (error
&& error
!= ENOENT
)
3769 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
3770 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
3771 if (error
&& error
!= ENOENT
)
3772 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
3773 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
3774 if (error
&& error
!= ENOENT
)
3775 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
3776 error
= dsl_destroy_head(clone1name
);
3777 if (error
&& error
!= ENOENT
)
3778 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
3779 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
3780 if (error
&& error
!= ENOENT
)
3781 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
3783 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
3784 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
3785 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
3786 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
3787 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
3791 * Verify dsl_dataset_promote handles EBUSY
3794 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
3802 char *osname
= zd
->zd_name
;
3805 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3806 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3807 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3808 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3809 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3811 (void) rw_rdlock(&ztest_name_lock
);
3813 ztest_dsl_dataset_cleanup(osname
, id
);
3815 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3816 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3817 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3818 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3819 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3820 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3821 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3822 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3823 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3824 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3826 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
3827 if (error
&& error
!= EEXIST
) {
3828 if (error
== ENOSPC
) {
3829 ztest_record_enospc(FTAG
);
3832 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
3835 error
= dmu_objset_clone(clone1name
, snap1name
);
3837 if (error
== ENOSPC
) {
3838 ztest_record_enospc(FTAG
);
3841 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
3844 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
3845 if (error
&& error
!= EEXIST
) {
3846 if (error
== ENOSPC
) {
3847 ztest_record_enospc(FTAG
);
3850 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
3853 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
3854 if (error
&& error
!= EEXIST
) {
3855 if (error
== ENOSPC
) {
3856 ztest_record_enospc(FTAG
);
3859 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
3862 error
= dmu_objset_clone(clone2name
, snap3name
);
3864 if (error
== ENOSPC
) {
3865 ztest_record_enospc(FTAG
);
3868 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
3871 error
= dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, FTAG
, &os
);
3873 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
3874 error
= dsl_dataset_promote(clone2name
, NULL
);
3875 if (error
== ENOSPC
) {
3876 dmu_objset_disown(os
, FTAG
);
3877 ztest_record_enospc(FTAG
);
3881 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
3883 dmu_objset_disown(os
, FTAG
);
3886 ztest_dsl_dataset_cleanup(osname
, id
);
3888 (void) rw_unlock(&ztest_name_lock
);
3890 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
3891 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
3892 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
3893 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
3894 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
3897 #undef OD_ARRAY_SIZE
3898 #define OD_ARRAY_SIZE 4
3901 * Verify that dmu_object_{alloc,free} work as expected.
3904 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
3911 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
3912 od
= umem_alloc(size
, UMEM_NOFAIL
);
3913 batchsize
= OD_ARRAY_SIZE
;
3915 for (b
= 0; b
< batchsize
; b
++)
3916 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
3920 * Destroy the previous batch of objects, create a new batch,
3921 * and do some I/O on the new objects.
3923 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
3926 while (ztest_random(4 * batchsize
) != 0)
3927 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
3928 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
3930 umem_free(od
, size
);
3934 * Rewind the global allocator to verify object allocation backfilling.
3937 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
3939 objset_t
*os
= zd
->zd_os
;
3940 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
3944 * Rewind the global allocator randomly back to a lower object number
3945 * to force backfilling and reclamation of recently freed dnodes.
3947 mutex_enter(&os
->os_obj_lock
);
3948 object
= ztest_random(os
->os_obj_next_chunk
);
3949 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
3950 mutex_exit(&os
->os_obj_lock
);
3953 #undef OD_ARRAY_SIZE
3954 #define OD_ARRAY_SIZE 2
3957 * Verify that dmu_{read,write} work as expected.
3960 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
3965 objset_t
*os
= zd
->zd_os
;
3966 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
3967 od
= umem_alloc(size
, UMEM_NOFAIL
);
3969 int i
, freeit
, error
;
3971 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
3972 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
3973 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
3974 uint64_t regions
= 997;
3975 uint64_t stride
= 123456789ULL;
3976 uint64_t width
= 40;
3977 int free_percent
= 5;
3980 * This test uses two objects, packobj and bigobj, that are always
3981 * updated together (i.e. in the same tx) so that their contents are
3982 * in sync and can be compared. Their contents relate to each other
3983 * in a simple way: packobj is a dense array of 'bufwad' structures,
3984 * while bigobj is a sparse array of the same bufwads. Specifically,
3985 * for any index n, there are three bufwads that should be identical:
3987 * packobj, at offset n * sizeof (bufwad_t)
3988 * bigobj, at the head of the nth chunk
3989 * bigobj, at the tail of the nth chunk
3991 * The chunk size is arbitrary. It doesn't have to be a power of two,
3992 * and it doesn't have any relation to the object blocksize.
3993 * The only requirement is that it can hold at least two bufwads.
3995 * Normally, we write the bufwad to each of these locations.
3996 * However, free_percent of the time we instead write zeroes to
3997 * packobj and perform a dmu_free_range() on bigobj. By comparing
3998 * bigobj to packobj, we can verify that the DMU is correctly
3999 * tracking which parts of an object are allocated and free,
4000 * and that the contents of the allocated blocks are correct.
4004 * Read the directory info. If it's the first time, set things up.
4006 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4007 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4010 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4011 umem_free(od
, size
);
4015 bigobj
= od
[0].od_object
;
4016 packobj
= od
[1].od_object
;
4017 chunksize
= od
[0].od_gen
;
4018 ASSERT(chunksize
== od
[1].od_gen
);
4021 * Prefetch a random chunk of the big object.
4022 * Our aim here is to get some async reads in flight
4023 * for blocks that we may free below; the DMU should
4024 * handle this race correctly.
4026 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4027 s
= 1 + ztest_random(2 * width
- 1);
4028 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4029 ZIO_PRIORITY_SYNC_READ
);
4032 * Pick a random index and compute the offsets into packobj and bigobj.
4034 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4035 s
= 1 + ztest_random(width
- 1);
4037 packoff
= n
* sizeof (bufwad_t
);
4038 packsize
= s
* sizeof (bufwad_t
);
4040 bigoff
= n
* chunksize
;
4041 bigsize
= s
* chunksize
;
4043 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4044 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4047 * free_percent of the time, free a range of bigobj rather than
4050 freeit
= (ztest_random(100) < free_percent
);
4053 * Read the current contents of our objects.
4055 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4058 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4063 * Get a tx for the mods to both packobj and bigobj.
4065 tx
= dmu_tx_create(os
);
4067 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4070 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4072 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4074 /* This accounts for setting the checksum/compression. */
4075 dmu_tx_hold_bonus(tx
, bigobj
);
4077 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4079 umem_free(packbuf
, packsize
);
4080 umem_free(bigbuf
, bigsize
);
4081 umem_free(od
, size
);
4085 enum zio_checksum cksum
;
4087 cksum
= (enum zio_checksum
)
4088 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4089 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4090 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4092 enum zio_compress comp
;
4094 comp
= (enum zio_compress
)
4095 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4096 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4097 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4100 * For each index from n to n + s, verify that the existing bufwad
4101 * in packobj matches the bufwads at the head and tail of the
4102 * corresponding chunk in bigobj. Then update all three bufwads
4103 * with the new values we want to write out.
4105 for (i
= 0; i
< s
; i
++) {
4107 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4109 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4111 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4113 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4114 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4116 if (pack
->bw_txg
> txg
)
4117 fatal(0, "future leak: got %llx, open txg is %llx",
4120 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4121 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4122 pack
->bw_index
, n
, i
);
4124 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4125 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4127 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4128 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4131 bzero(pack
, sizeof (bufwad_t
));
4133 pack
->bw_index
= n
+ i
;
4135 pack
->bw_data
= 1 + ztest_random(-2ULL);
4142 * We've verified all the old bufwads, and made new ones.
4143 * Now write them out.
4145 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4148 if (ztest_opts
.zo_verbose
>= 7) {
4149 (void) printf("freeing offset %llx size %llx"
4151 (u_longlong_t
)bigoff
,
4152 (u_longlong_t
)bigsize
,
4155 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4157 if (ztest_opts
.zo_verbose
>= 7) {
4158 (void) printf("writing offset %llx size %llx"
4160 (u_longlong_t
)bigoff
,
4161 (u_longlong_t
)bigsize
,
4164 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4170 * Sanity check the stuff we just wrote.
4173 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4174 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4176 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4177 packsize
, packcheck
, DMU_READ_PREFETCH
));
4178 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4179 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4181 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4182 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4184 umem_free(packcheck
, packsize
);
4185 umem_free(bigcheck
, bigsize
);
4188 umem_free(packbuf
, packsize
);
4189 umem_free(bigbuf
, bigsize
);
4190 umem_free(od
, size
);
4194 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4195 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4203 * For each index from n to n + s, verify that the existing bufwad
4204 * in packobj matches the bufwads at the head and tail of the
4205 * corresponding chunk in bigobj. Then update all three bufwads
4206 * with the new values we want to write out.
4208 for (i
= 0; i
< s
; i
++) {
4210 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4212 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4214 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4216 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4217 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4219 if (pack
->bw_txg
> txg
)
4220 fatal(0, "future leak: got %llx, open txg is %llx",
4223 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4224 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4225 pack
->bw_index
, n
, i
);
4227 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4228 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4230 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4231 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4233 pack
->bw_index
= n
+ i
;
4235 pack
->bw_data
= 1 + ztest_random(-2ULL);
4242 #undef OD_ARRAY_SIZE
4243 #define OD_ARRAY_SIZE 2
4246 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4248 objset_t
*os
= zd
->zd_os
;
4255 bufwad_t
*packbuf
, *bigbuf
;
4256 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4257 uint64_t blocksize
= ztest_random_blocksize();
4258 uint64_t chunksize
= blocksize
;
4259 uint64_t regions
= 997;
4260 uint64_t stride
= 123456789ULL;
4262 dmu_buf_t
*bonus_db
;
4263 arc_buf_t
**bigbuf_arcbufs
;
4264 dmu_object_info_t doi
;
4266 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4267 od
= umem_alloc(size
, UMEM_NOFAIL
);
4270 * This test uses two objects, packobj and bigobj, that are always
4271 * updated together (i.e. in the same tx) so that their contents are
4272 * in sync and can be compared. Their contents relate to each other
4273 * in a simple way: packobj is a dense array of 'bufwad' structures,
4274 * while bigobj is a sparse array of the same bufwads. Specifically,
4275 * for any index n, there are three bufwads that should be identical:
4277 * packobj, at offset n * sizeof (bufwad_t)
4278 * bigobj, at the head of the nth chunk
4279 * bigobj, at the tail of the nth chunk
4281 * The chunk size is set equal to bigobj block size so that
4282 * dmu_assign_arcbuf() can be tested for object updates.
4286 * Read the directory info. If it's the first time, set things up.
4288 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4289 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4293 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4294 umem_free(od
, size
);
4298 bigobj
= od
[0].od_object
;
4299 packobj
= od
[1].od_object
;
4300 blocksize
= od
[0].od_blocksize
;
4301 chunksize
= blocksize
;
4302 ASSERT(chunksize
== od
[1].od_gen
);
4304 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4305 VERIFY(ISP2(doi
.doi_data_block_size
));
4306 VERIFY(chunksize
== doi
.doi_data_block_size
);
4307 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4310 * Pick a random index and compute the offsets into packobj and bigobj.
4312 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4313 s
= 1 + ztest_random(width
- 1);
4315 packoff
= n
* sizeof (bufwad_t
);
4316 packsize
= s
* sizeof (bufwad_t
);
4318 bigoff
= n
* chunksize
;
4319 bigsize
= s
* chunksize
;
4321 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4322 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4324 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4326 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4329 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4330 * Iteration 1 test zcopy to already referenced dbufs.
4331 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4332 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4333 * Iteration 4 test zcopy when dbuf is no longer dirty.
4334 * Iteration 5 test zcopy when it can't be done.
4335 * Iteration 6 one more zcopy write.
4337 for (i
= 0; i
< 7; i
++) {
4342 * In iteration 5 (i == 5) use arcbufs
4343 * that don't match bigobj blksz to test
4344 * dmu_assign_arcbuf() when it can't directly
4345 * assign an arcbuf to a dbuf.
4347 for (j
= 0; j
< s
; j
++) {
4348 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4350 dmu_request_arcbuf(bonus_db
, chunksize
);
4352 bigbuf_arcbufs
[2 * j
] =
4353 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4354 bigbuf_arcbufs
[2 * j
+ 1] =
4355 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4360 * Get a tx for the mods to both packobj and bigobj.
4362 tx
= dmu_tx_create(os
);
4364 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4365 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4367 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4369 umem_free(packbuf
, packsize
);
4370 umem_free(bigbuf
, bigsize
);
4371 for (j
= 0; j
< s
; j
++) {
4373 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4374 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4377 bigbuf_arcbufs
[2 * j
]);
4379 bigbuf_arcbufs
[2 * j
+ 1]);
4382 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4383 umem_free(od
, size
);
4384 dmu_buf_rele(bonus_db
, FTAG
);
4389 * 50% of the time don't read objects in the 1st iteration to
4390 * test dmu_assign_arcbuf() for the case when there're no
4391 * existing dbufs for the specified offsets.
4393 if (i
!= 0 || ztest_random(2) != 0) {
4394 error
= dmu_read(os
, packobj
, packoff
,
4395 packsize
, packbuf
, DMU_READ_PREFETCH
);
4397 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4398 bigbuf
, DMU_READ_PREFETCH
);
4401 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4405 * We've verified all the old bufwads, and made new ones.
4406 * Now write them out.
4408 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4409 if (ztest_opts
.zo_verbose
>= 7) {
4410 (void) printf("writing offset %llx size %llx"
4412 (u_longlong_t
)bigoff
,
4413 (u_longlong_t
)bigsize
,
4416 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4418 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4419 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4420 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4422 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4423 bigbuf_arcbufs
[2 * j
]->b_data
,
4425 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4427 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4432 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4433 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4435 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4436 dmu_assign_arcbuf(bonus_db
, off
,
4437 bigbuf_arcbufs
[j
], tx
);
4439 dmu_assign_arcbuf(bonus_db
, off
,
4440 bigbuf_arcbufs
[2 * j
], tx
);
4441 dmu_assign_arcbuf(bonus_db
,
4442 off
+ chunksize
/ 2,
4443 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4446 dmu_buf_rele(dbt
, FTAG
);
4452 * Sanity check the stuff we just wrote.
4455 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4456 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4458 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4459 packsize
, packcheck
, DMU_READ_PREFETCH
));
4460 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4461 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4463 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4464 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4466 umem_free(packcheck
, packsize
);
4467 umem_free(bigcheck
, bigsize
);
4470 txg_wait_open(dmu_objset_pool(os
), 0);
4471 } else if (i
== 3) {
4472 txg_wait_synced(dmu_objset_pool(os
), 0);
4476 dmu_buf_rele(bonus_db
, FTAG
);
4477 umem_free(packbuf
, packsize
);
4478 umem_free(bigbuf
, bigsize
);
4479 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4480 umem_free(od
, size
);
4485 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4489 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4490 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4491 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4494 * Have multiple threads write to large offsets in an object
4495 * to verify that parallel writes to an object -- even to the
4496 * same blocks within the object -- doesn't cause any trouble.
4498 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4500 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4503 while (ztest_random(10) != 0)
4504 ztest_io(zd
, od
->od_object
, offset
);
4506 umem_free(od
, sizeof (ztest_od_t
));
4510 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4513 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4514 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4515 uint64_t count
= ztest_random(20) + 1;
4516 uint64_t blocksize
= ztest_random_blocksize();
4519 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4521 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4523 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4524 !ztest_random(2)) != 0) {
4525 umem_free(od
, sizeof (ztest_od_t
));
4529 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4530 umem_free(od
, sizeof (ztest_od_t
));
4534 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4536 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4538 while (ztest_random(count
) != 0) {
4539 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4540 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4543 while (ztest_random(4) != 0)
4544 ztest_io(zd
, od
->od_object
, randoff
);
4547 umem_free(data
, blocksize
);
4548 umem_free(od
, sizeof (ztest_od_t
));
4552 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4554 #define ZTEST_ZAP_MIN_INTS 1
4555 #define ZTEST_ZAP_MAX_INTS 4
4556 #define ZTEST_ZAP_MAX_PROPS 1000
4559 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4561 objset_t
*os
= zd
->zd_os
;
4564 uint64_t txg
, last_txg
;
4565 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4566 uint64_t zl_ints
, zl_intsize
, prop
;
4569 char propname
[100], txgname
[100];
4571 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4573 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4574 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4576 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4577 !ztest_random(2)) != 0)
4580 object
= od
->od_object
;
4583 * Generate a known hash collision, and verify that
4584 * we can lookup and remove both entries.
4586 tx
= dmu_tx_create(os
);
4587 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4588 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4591 for (i
= 0; i
< 2; i
++) {
4593 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
4596 for (i
= 0; i
< 2; i
++) {
4597 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
4598 sizeof (uint64_t), 1, &value
[i
], tx
));
4600 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
4601 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4602 ASSERT3U(zl_ints
, ==, 1);
4604 for (i
= 0; i
< 2; i
++) {
4605 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
4610 * Generate a buch of random entries.
4612 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
4614 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4615 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4616 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4617 bzero(value
, sizeof (value
));
4621 * If these zap entries already exist, validate their contents.
4623 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4625 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4626 ASSERT3U(zl_ints
, ==, 1);
4628 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
4629 zl_ints
, &last_txg
) == 0);
4631 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
4634 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4635 ASSERT3U(zl_ints
, ==, ints
);
4637 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
4638 zl_ints
, value
) == 0);
4640 for (i
= 0; i
< ints
; i
++) {
4641 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
4644 ASSERT3U(error
, ==, ENOENT
);
4648 * Atomically update two entries in our zap object.
4649 * The first is named txg_%llu, and contains the txg
4650 * in which the property was last updated. The second
4651 * is named prop_%llu, and the nth element of its value
4652 * should be txg + object + n.
4654 tx
= dmu_tx_create(os
);
4655 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4656 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4661 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
4663 for (i
= 0; i
< ints
; i
++)
4664 value
[i
] = txg
+ object
+ i
;
4666 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
4668 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
4674 * Remove a random pair of entries.
4676 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4677 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4678 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4680 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4682 if (error
== ENOENT
)
4687 tx
= dmu_tx_create(os
);
4688 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4689 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4692 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
4693 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
4696 umem_free(od
, sizeof (ztest_od_t
));
4700 * Testcase to test the upgrading of a microzap to fatzap.
4703 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
4705 objset_t
*os
= zd
->zd_os
;
4707 uint64_t object
, txg
;
4710 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4711 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4713 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4714 !ztest_random(2)) != 0)
4716 object
= od
->od_object
;
4719 * Add entries to this ZAP and make sure it spills over
4720 * and gets upgraded to a fatzap. Also, since we are adding
4721 * 2050 entries we should see ptrtbl growth and leaf-block split.
4723 for (i
= 0; i
< 2050; i
++) {
4724 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4729 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
4730 (u_longlong_t
)id
, (u_longlong_t
)value
);
4732 tx
= dmu_tx_create(os
);
4733 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
4734 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4737 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
4739 ASSERT(error
== 0 || error
== EEXIST
);
4743 umem_free(od
, sizeof (ztest_od_t
));
4748 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
4750 objset_t
*os
= zd
->zd_os
;
4752 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
4754 int i
, namelen
, error
;
4755 int micro
= ztest_random(2);
4756 char name
[20], string_value
[20];
4759 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4760 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4762 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
4763 umem_free(od
, sizeof (ztest_od_t
));
4767 object
= od
->od_object
;
4770 * Generate a random name of the form 'xxx.....' where each
4771 * x is a random printable character and the dots are dots.
4772 * There are 94 such characters, and the name length goes from
4773 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4775 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
4777 for (i
= 0; i
< 3; i
++)
4778 name
[i
] = '!' + ztest_random('~' - '!' + 1);
4779 for (; i
< namelen
- 1; i
++)
4783 if ((namelen
& 1) || micro
) {
4784 wsize
= sizeof (txg
);
4790 data
= string_value
;
4794 VERIFY0(zap_count(os
, object
, &count
));
4795 ASSERT(count
!= -1ULL);
4798 * Select an operation: length, lookup, add, update, remove.
4800 i
= ztest_random(5);
4803 tx
= dmu_tx_create(os
);
4804 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4805 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4807 umem_free(od
, sizeof (ztest_od_t
));
4810 bcopy(name
, string_value
, namelen
);
4814 bzero(string_value
, namelen
);
4820 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
4822 ASSERT3U(wsize
, ==, zl_wsize
);
4823 ASSERT3U(wc
, ==, zl_wc
);
4825 ASSERT3U(error
, ==, ENOENT
);
4830 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
4832 if (data
== string_value
&&
4833 bcmp(name
, data
, namelen
) != 0)
4834 fatal(0, "name '%s' != val '%s' len %d",
4835 name
, data
, namelen
);
4837 ASSERT3U(error
, ==, ENOENT
);
4842 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
4843 ASSERT(error
== 0 || error
== EEXIST
);
4847 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
4851 error
= zap_remove(os
, object
, name
, tx
);
4852 ASSERT(error
== 0 || error
== ENOENT
);
4859 umem_free(od
, sizeof (ztest_od_t
));
4863 * Commit callback data.
4865 typedef struct ztest_cb_data
{
4866 list_node_t zcd_node
;
4868 int zcd_expected_err
;
4869 boolean_t zcd_added
;
4870 boolean_t zcd_called
;
4874 /* This is the actual commit callback function */
4876 ztest_commit_callback(void *arg
, int error
)
4878 ztest_cb_data_t
*data
= arg
;
4879 uint64_t synced_txg
;
4881 VERIFY(data
!= NULL
);
4882 VERIFY3S(data
->zcd_expected_err
, ==, error
);
4883 VERIFY(!data
->zcd_called
);
4885 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
4886 if (data
->zcd_txg
> synced_txg
)
4887 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
4888 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
4891 data
->zcd_called
= B_TRUE
;
4893 if (error
== ECANCELED
) {
4894 ASSERT0(data
->zcd_txg
);
4895 ASSERT(!data
->zcd_added
);
4898 * The private callback data should be destroyed here, but
4899 * since we are going to check the zcd_called field after
4900 * dmu_tx_abort(), we will destroy it there.
4905 ASSERT(data
->zcd_added
);
4906 ASSERT3U(data
->zcd_txg
, !=, 0);
4908 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
4910 /* See if this cb was called more quickly */
4911 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
4912 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
4914 /* Remove our callback from the list */
4915 list_remove(&zcl
.zcl_callbacks
, data
);
4917 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
4919 umem_free(data
, sizeof (ztest_cb_data_t
));
4922 /* Allocate and initialize callback data structure */
4923 static ztest_cb_data_t
*
4924 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
4926 ztest_cb_data_t
*cb_data
;
4928 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
4930 cb_data
->zcd_txg
= txg
;
4931 cb_data
->zcd_spa
= dmu_objset_spa(os
);
4932 list_link_init(&cb_data
->zcd_node
);
4938 * Commit callback test.
4941 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
4943 objset_t
*os
= zd
->zd_os
;
4946 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
4947 uint64_t old_txg
, txg
;
4950 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4951 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4953 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
4954 umem_free(od
, sizeof (ztest_od_t
));
4958 tx
= dmu_tx_create(os
);
4960 cb_data
[0] = ztest_create_cb_data(os
, 0);
4961 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
4963 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
4965 /* Every once in a while, abort the transaction on purpose */
4966 if (ztest_random(100) == 0)
4970 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
4972 txg
= error
? 0 : dmu_tx_get_txg(tx
);
4974 cb_data
[0]->zcd_txg
= txg
;
4975 cb_data
[1] = ztest_create_cb_data(os
, txg
);
4976 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
4980 * It's not a strict requirement to call the registered
4981 * callbacks from inside dmu_tx_abort(), but that's what
4982 * it's supposed to happen in the current implementation
4983 * so we will check for that.
4985 for (i
= 0; i
< 2; i
++) {
4986 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
4987 VERIFY(!cb_data
[i
]->zcd_called
);
4992 for (i
= 0; i
< 2; i
++) {
4993 VERIFY(cb_data
[i
]->zcd_called
);
4994 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
4997 umem_free(od
, sizeof (ztest_od_t
));
5001 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5002 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5005 * Read existing data to make sure there isn't a future leak.
5007 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5008 &old_txg
, DMU_READ_PREFETCH
));
5011 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5014 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5016 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5019 * Since commit callbacks don't have any ordering requirement and since
5020 * it is theoretically possible for a commit callback to be called
5021 * after an arbitrary amount of time has elapsed since its txg has been
5022 * synced, it is difficult to reliably determine whether a commit
5023 * callback hasn't been called due to high load or due to a flawed
5026 * In practice, we will assume that if after a certain number of txgs a
5027 * commit callback hasn't been called, then most likely there's an
5028 * implementation bug..
5030 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5031 if (tmp_cb
!= NULL
&&
5032 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5033 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5034 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5038 * Let's find the place to insert our callbacks.
5040 * Even though the list is ordered by txg, it is possible for the
5041 * insertion point to not be the end because our txg may already be
5042 * quiescing at this point and other callbacks in the open txg
5043 * (from other objsets) may have sneaked in.
5045 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5046 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5047 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5049 /* Add the 3 callbacks to the list */
5050 for (i
= 0; i
< 3; i
++) {
5052 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5054 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5057 cb_data
[i
]->zcd_added
= B_TRUE
;
5058 VERIFY(!cb_data
[i
]->zcd_called
);
5060 tmp_cb
= cb_data
[i
];
5065 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5069 umem_free(od
, sizeof (ztest_od_t
));
5073 * Visit each object in the dataset. Verify that its properties
5074 * are consistent what was stored in the block tag when it was created,
5075 * and that its unused bonus buffer space has not been overwritten.
5079 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5081 objset_t
*os
= zd
->zd_os
;
5085 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5086 ztest_block_tag_t
*bt
= NULL
;
5087 dmu_object_info_t doi
;
5090 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0)
5093 dmu_object_info_from_db(db
, &doi
);
5094 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5095 bt
= ztest_bt_bonus(db
);
5097 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5098 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5099 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5101 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5104 dmu_buf_rele(db
, FTAG
);
5110 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5112 zfs_prop_t proplist
[] = {
5114 ZFS_PROP_COMPRESSION
,
5120 (void) rw_rdlock(&ztest_name_lock
);
5122 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5123 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5124 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5126 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5127 ztest_random_blocksize(), (int)ztest_random(2)));
5129 (void) rw_unlock(&ztest_name_lock
);
5134 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5136 nvlist_t
*props
= NULL
;
5138 (void) rw_rdlock(&ztest_name_lock
);
5140 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5141 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5143 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5145 if (ztest_opts
.zo_verbose
>= 6)
5146 dump_nvlist(props
, 4);
5150 (void) rw_unlock(&ztest_name_lock
);
5154 user_release_one(const char *snapname
, const char *holdname
)
5156 nvlist_t
*snaps
, *holds
;
5159 snaps
= fnvlist_alloc();
5160 holds
= fnvlist_alloc();
5161 fnvlist_add_boolean(holds
, holdname
);
5162 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5163 fnvlist_free(holds
);
5164 error
= dsl_dataset_user_release(snaps
, NULL
);
5165 fnvlist_free(snaps
);
5170 * Test snapshot hold/release and deferred destroy.
5173 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5176 objset_t
*os
= zd
->zd_os
;
5180 char clonename
[100];
5182 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5185 (void) rw_rdlock(&ztest_name_lock
);
5187 dmu_objset_name(os
, osname
);
5189 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5191 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5192 (void) snprintf(clonename
, sizeof (clonename
),
5193 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5194 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5197 * Clean up from any previous run.
5199 error
= dsl_destroy_head(clonename
);
5200 if (error
!= ENOENT
)
5202 error
= user_release_one(fullname
, tag
);
5203 if (error
!= ESRCH
&& error
!= ENOENT
)
5205 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5206 if (error
!= ENOENT
)
5210 * Create snapshot, clone it, mark snap for deferred destroy,
5211 * destroy clone, verify snap was also destroyed.
5213 error
= dmu_objset_snapshot_one(osname
, snapname
);
5215 if (error
== ENOSPC
) {
5216 ztest_record_enospc("dmu_objset_snapshot");
5219 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5222 error
= dmu_objset_clone(clonename
, fullname
);
5224 if (error
== ENOSPC
) {
5225 ztest_record_enospc("dmu_objset_clone");
5228 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5231 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5233 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5237 error
= dsl_destroy_head(clonename
);
5239 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5241 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5242 if (error
!= ENOENT
)
5243 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5246 * Create snapshot, add temporary hold, verify that we can't
5247 * destroy a held snapshot, mark for deferred destroy,
5248 * release hold, verify snapshot was destroyed.
5250 error
= dmu_objset_snapshot_one(osname
, snapname
);
5252 if (error
== ENOSPC
) {
5253 ztest_record_enospc("dmu_objset_snapshot");
5256 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5259 holds
= fnvlist_alloc();
5260 fnvlist_add_string(holds
, fullname
, tag
);
5261 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5262 fnvlist_free(holds
);
5264 if (error
== ENOSPC
) {
5265 ztest_record_enospc("dsl_dataset_user_hold");
5268 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5269 fullname
, tag
, error
);
5272 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5273 if (error
!= EBUSY
) {
5274 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5278 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5280 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5284 error
= user_release_one(fullname
, tag
);
5286 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5288 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5291 (void) rw_unlock(&ztest_name_lock
);
5295 * Inject random faults into the on-disk data.
5299 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5301 ztest_shared_t
*zs
= ztest_shared
;
5302 spa_t
*spa
= ztest_spa
;
5306 uint64_t bad
= 0x1990c0ffeedecadeull
;
5311 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5317 boolean_t islog
= B_FALSE
;
5319 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5320 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5322 mutex_enter(&ztest_vdev_lock
);
5323 maxfaults
= MAXFAULTS();
5324 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5325 mirror_save
= zs
->zs_mirrors
;
5326 mutex_exit(&ztest_vdev_lock
);
5328 ASSERT(leaves
>= 1);
5331 * Grab the name lock as reader. There are some operations
5332 * which don't like to have their vdevs changed while
5333 * they are in progress (i.e. spa_change_guid). Those
5334 * operations will have grabbed the name lock as writer.
5336 (void) rw_rdlock(&ztest_name_lock
);
5339 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5341 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5343 if (ztest_random(2) == 0) {
5345 * Inject errors on a normal data device or slog device.
5347 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5348 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5351 * Generate paths to the first leaf in this top-level vdev,
5352 * and to the random leaf we selected. We'll induce transient
5353 * write failures and random online/offline activity on leaf 0,
5354 * and we'll write random garbage to the randomly chosen leaf.
5356 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5357 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5358 top
* leaves
+ zs
->zs_splits
);
5359 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5360 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5361 top
* leaves
+ leaf
);
5363 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5364 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5368 * If the top-level vdev needs to be resilvered
5369 * then we only allow faults on the device that is
5372 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5373 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5374 vd0
->vdev_resilver_txg
!= 0)) {
5376 * Make vd0 explicitly claim to be unreadable,
5377 * or unwriteable, or reach behind its back
5378 * and close the underlying fd. We can do this if
5379 * maxfaults == 0 because we'll fail and reexecute,
5380 * and we can do it if maxfaults >= 2 because we'll
5381 * have enough redundancy. If maxfaults == 1, the
5382 * combination of this with injection of random data
5383 * corruption below exceeds the pool's fault tolerance.
5385 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5387 if (vf
!= NULL
&& ztest_random(3) == 0) {
5388 (void) close(vf
->vf_vnode
->v_fd
);
5389 vf
->vf_vnode
->v_fd
= -1;
5390 } else if (ztest_random(2) == 0) {
5391 vd0
->vdev_cant_read
= B_TRUE
;
5393 vd0
->vdev_cant_write
= B_TRUE
;
5395 guid0
= vd0
->vdev_guid
;
5399 * Inject errors on an l2cache device.
5401 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5403 if (sav
->sav_count
== 0) {
5404 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5405 (void) rw_unlock(&ztest_name_lock
);
5408 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5409 guid0
= vd0
->vdev_guid
;
5410 (void) strcpy(path0
, vd0
->vdev_path
);
5411 (void) strcpy(pathrand
, vd0
->vdev_path
);
5415 maxfaults
= INT_MAX
; /* no limit on cache devices */
5418 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5419 (void) rw_unlock(&ztest_name_lock
);
5422 * If we can tolerate two or more faults, or we're dealing
5423 * with a slog, randomly online/offline vd0.
5425 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5426 if (ztest_random(10) < 6) {
5427 int flags
= (ztest_random(2) == 0 ?
5428 ZFS_OFFLINE_TEMPORARY
: 0);
5431 * We have to grab the zs_name_lock as writer to
5432 * prevent a race between offlining a slog and
5433 * destroying a dataset. Offlining the slog will
5434 * grab a reference on the dataset which may cause
5435 * dsl_destroy_head() to fail with EBUSY thus
5436 * leaving the dataset in an inconsistent state.
5439 (void) rw_wrlock(&ztest_name_lock
);
5441 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5444 (void) rw_unlock(&ztest_name_lock
);
5447 * Ideally we would like to be able to randomly
5448 * call vdev_[on|off]line without holding locks
5449 * to force unpredictable failures but the side
5450 * effects of vdev_[on|off]line prevent us from
5451 * doing so. We grab the ztest_vdev_lock here to
5452 * prevent a race between injection testing and
5455 mutex_enter(&ztest_vdev_lock
);
5456 (void) vdev_online(spa
, guid0
, 0, NULL
);
5457 mutex_exit(&ztest_vdev_lock
);
5465 * We have at least single-fault tolerance, so inject data corruption.
5467 fd
= open(pathrand
, O_RDWR
);
5469 if (fd
== -1) /* we hit a gap in the device namespace */
5472 fsize
= lseek(fd
, 0, SEEK_END
);
5474 while (--iters
!= 0) {
5476 * The offset must be chosen carefully to ensure that
5477 * we do not inject a given logical block with errors
5478 * on two different leaf devices, because ZFS can not
5479 * tolerate that (if maxfaults==1).
5481 * We divide each leaf into chunks of size
5482 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5483 * there is a series of ranges to which we can inject errors.
5484 * Each range can accept errors on only a single leaf vdev.
5485 * The error injection ranges are separated by ranges
5486 * which we will not inject errors on any device (DMZs).
5487 * Each DMZ must be large enough such that a single block
5488 * can not straddle it, so that a single block can not be
5489 * a target in two different injection ranges (on different
5492 * For example, with 3 leaves, each chunk looks like:
5493 * 0 to 32M: injection range for leaf 0
5494 * 32M to 64M: DMZ - no injection allowed
5495 * 64M to 96M: injection range for leaf 1
5496 * 96M to 128M: DMZ - no injection allowed
5497 * 128M to 160M: injection range for leaf 2
5498 * 160M to 192M: DMZ - no injection allowed
5500 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5501 (leaves
<< bshift
) + (leaf
<< bshift
) +
5502 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5505 * Only allow damage to the labels at one end of the vdev.
5507 * If all labels are damaged, the device will be totally
5508 * inaccessible, which will result in loss of data,
5509 * because we also damage (parts of) the other side of
5512 * Additionally, we will always have both an even and an
5513 * odd label, so that we can handle crashes in the
5514 * middle of vdev_config_sync().
5516 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5520 * The two end labels are stored at the "end" of the disk, but
5521 * the end of the disk (vdev_psize) is aligned to
5522 * sizeof (vdev_label_t).
5524 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5525 if ((leaf
& 1) == 1 &&
5526 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5529 mutex_enter(&ztest_vdev_lock
);
5530 if (mirror_save
!= zs
->zs_mirrors
) {
5531 mutex_exit(&ztest_vdev_lock
);
5536 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5537 fatal(1, "can't inject bad word at 0x%llx in %s",
5540 mutex_exit(&ztest_vdev_lock
);
5542 if (ztest_opts
.zo_verbose
>= 7)
5543 (void) printf("injected bad word into %s,"
5544 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5549 umem_free(path0
, MAXPATHLEN
);
5550 umem_free(pathrand
, MAXPATHLEN
);
5554 * Verify that DDT repair works as expected.
5557 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
5559 ztest_shared_t
*zs
= ztest_shared
;
5560 spa_t
*spa
= ztest_spa
;
5561 objset_t
*os
= zd
->zd_os
;
5563 uint64_t object
, blocksize
, txg
, pattern
, psize
;
5564 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
5569 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
5572 blocksize
= ztest_random_blocksize();
5573 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
5575 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5576 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
5578 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5579 umem_free(od
, sizeof (ztest_od_t
));
5584 * Take the name lock as writer to prevent anyone else from changing
5585 * the pool and dataset properies we need to maintain during this test.
5587 (void) rw_wrlock(&ztest_name_lock
);
5589 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
5591 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
5593 (void) rw_unlock(&ztest_name_lock
);
5594 umem_free(od
, sizeof (ztest_od_t
));
5598 dmu_objset_stats_t dds
;
5599 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
5600 dmu_objset_fast_stat(os
, &dds
);
5601 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
5603 object
= od
[0].od_object
;
5604 blocksize
= od
[0].od_blocksize
;
5605 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
5607 ASSERT(object
!= 0);
5609 tx
= dmu_tx_create(os
);
5610 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
5611 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
5613 (void) rw_unlock(&ztest_name_lock
);
5614 umem_free(od
, sizeof (ztest_od_t
));
5619 * Write all the copies of our block.
5621 for (i
= 0; i
< copies
; i
++) {
5622 uint64_t offset
= i
* blocksize
;
5623 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
5624 DMU_READ_NO_PREFETCH
);
5626 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
5627 os
, (long long)object
, (long long) offset
, error
);
5629 ASSERT(db
->db_offset
== offset
);
5630 ASSERT(db
->db_size
== blocksize
);
5631 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
5632 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
5633 dmu_buf_will_fill(db
, tx
);
5634 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
5635 dmu_buf_rele(db
, FTAG
);
5639 txg_wait_synced(spa_get_dsl(spa
), txg
);
5642 * Find out what block we got.
5644 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
5645 DMU_READ_NO_PREFETCH
));
5646 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
5647 dmu_buf_rele(db
, FTAG
);
5650 * Damage the block. Dedup-ditto will save us when we read it later.
5652 psize
= BP_GET_PSIZE(&blk
);
5653 abd
= abd_alloc_linear(psize
, B_TRUE
);
5654 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
5656 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
5657 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
5658 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
5662 (void) rw_unlock(&ztest_name_lock
);
5663 umem_free(od
, sizeof (ztest_od_t
));
5671 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
5673 spa_t
*spa
= ztest_spa
;
5675 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5676 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
5677 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5681 * Change the guid for the pool.
5685 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
5687 spa_t
*spa
= ztest_spa
;
5688 uint64_t orig
, load
;
5691 if (ztest_opts
.zo_mmp_test
)
5694 orig
= spa_guid(spa
);
5695 load
= spa_load_guid(spa
);
5697 (void) rw_wrlock(&ztest_name_lock
);
5698 error
= spa_change_guid(spa
);
5699 (void) rw_unlock(&ztest_name_lock
);
5704 if (ztest_opts
.zo_verbose
>= 4) {
5705 (void) printf("Changed guid old %llu -> %llu\n",
5706 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
5709 VERIFY3U(orig
, !=, spa_guid(spa
));
5710 VERIFY3U(load
, ==, spa_load_guid(spa
));
5714 * Rename the pool to a different name and then rename it back.
5718 ztest_spa_rename(ztest_ds_t
*zd
, uint64_t id
)
5720 char *oldname
, *newname
;
5723 if (ztest_opts
.zo_mmp_test
)
5726 (void) rw_wrlock(&ztest_name_lock
);
5728 oldname
= ztest_opts
.zo_pool
;
5729 newname
= umem_alloc(strlen(oldname
) + 5, UMEM_NOFAIL
);
5730 (void) strcpy(newname
, oldname
);
5731 (void) strcat(newname
, "_tmp");
5736 VERIFY3U(0, ==, spa_rename(oldname
, newname
));
5739 * Try to open it under the old name, which shouldn't exist
5741 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
5744 * Open it under the new name and make sure it's still the same spa_t.
5746 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
5748 ASSERT(spa
== ztest_spa
);
5749 spa_close(spa
, FTAG
);
5752 * Rename it back to the original
5754 VERIFY3U(0, ==, spa_rename(newname
, oldname
));
5757 * Make sure it can still be opened
5759 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
5761 ASSERT(spa
== ztest_spa
);
5762 spa_close(spa
, FTAG
);
5764 umem_free(newname
, strlen(newname
) + 1);
5766 (void) rw_unlock(&ztest_name_lock
);
5770 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
5772 hrtime_t end
= gethrtime() + NANOSEC
;
5774 while (gethrtime() <= end
) {
5775 int run_count
= 100;
5777 struct abd
*abd_data
, *abd_meta
;
5782 zio_cksum_t zc_ref_byteswap
;
5784 size
= ztest_random_blocksize();
5786 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5787 abd_data
= abd_alloc(size
, B_FALSE
);
5788 abd_meta
= abd_alloc(size
, B_TRUE
);
5790 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5791 *ptr
= ztest_random(UINT_MAX
);
5793 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
5794 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
5796 VERIFY0(fletcher_4_impl_set("scalar"));
5797 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5798 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
5800 VERIFY0(fletcher_4_impl_set("cycle"));
5801 while (run_count
-- > 0) {
5803 zio_cksum_t zc_byteswap
;
5805 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
5806 fletcher_4_native(buf
, size
, NULL
, &zc
);
5808 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5809 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5810 sizeof (zc_byteswap
)));
5812 /* Test ABD - data */
5813 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
5815 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
5817 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5818 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5819 sizeof (zc_byteswap
)));
5821 /* Test ABD - metadata */
5822 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
5824 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
5826 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5827 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5828 sizeof (zc_byteswap
)));
5832 umem_free(buf
, size
);
5839 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
5846 zio_cksum_t zc_ref_bswap
;
5848 hrtime_t end
= gethrtime() + NANOSEC
;
5850 while (gethrtime() <= end
) {
5851 int run_count
= 100;
5853 size
= ztest_random_blocksize();
5854 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5856 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5857 *ptr
= ztest_random(UINT_MAX
);
5859 VERIFY0(fletcher_4_impl_set("scalar"));
5860 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5861 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
5863 VERIFY0(fletcher_4_impl_set("cycle"));
5865 while (run_count
-- > 0) {
5867 zio_cksum_t zc_bswap
;
5870 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
5871 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
5873 while (pos
< size
) {
5874 size_t inc
= 64 * ztest_random(size
/ 67);
5875 /* sometimes add few bytes to test non-simd */
5876 if (ztest_random(100) < 10)
5877 inc
+= P2ALIGN(ztest_random(64),
5880 if (inc
> (size
- pos
))
5883 fletcher_4_incremental_native(buf
+ pos
, inc
,
5885 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
5891 VERIFY3U(pos
, ==, size
);
5893 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
5894 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
5897 * verify if incremental on the whole buffer is
5898 * equivalent to non-incremental version
5900 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
5901 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
5903 fletcher_4_incremental_native(buf
, size
, &zc
);
5904 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
5906 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
5907 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
5910 umem_free(buf
, size
);
5915 ztest_check_path(char *path
)
5918 /* return true on success */
5919 return (!stat(path
, &s
));
5923 ztest_get_zdb_bin(char *bin
, int len
)
5927 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5928 * let popen to search through PATH.
5930 if ((zdb_path
= getenv("ZDB_PATH"))) {
5931 strlcpy(bin
, zdb_path
, len
); /* In env */
5932 if (!ztest_check_path(bin
)) {
5933 ztest_dump_core
= 0;
5934 fatal(1, "invalid ZDB_PATH '%s'", bin
);
5939 VERIFY(realpath(getexecname(), bin
) != NULL
);
5940 if (strstr(bin
, "/ztest/")) {
5941 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
5942 strcat(bin
, "/zdb/zdb");
5943 if (ztest_check_path(bin
))
5950 * Verify pool integrity by running zdb.
5953 ztest_run_zdb(char *pool
)
5959 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
5962 bin
= umem_alloc(len
, UMEM_NOFAIL
);
5963 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
5964 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
5966 ztest_get_zdb_bin(bin
, len
);
5969 "%s -bcc%s%s -G -d -U %s %s",
5971 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
5972 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
5976 if (ztest_opts
.zo_verbose
>= 5)
5977 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
5979 fp
= popen(zdb
, "r");
5981 while (fgets(zbuf
, 1024, fp
) != NULL
)
5982 if (ztest_opts
.zo_verbose
>= 3)
5983 (void) printf("%s", zbuf
);
5985 status
= pclose(fp
);
5990 ztest_dump_core
= 0;
5991 if (WIFEXITED(status
))
5992 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
5994 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
5996 umem_free(bin
, len
);
5997 umem_free(zdb
, len
);
5998 umem_free(zbuf
, 1024);
6002 ztest_walk_pool_directory(char *header
)
6006 if (ztest_opts
.zo_verbose
>= 6)
6007 (void) printf("%s\n", header
);
6009 mutex_enter(&spa_namespace_lock
);
6010 while ((spa
= spa_next(spa
)) != NULL
)
6011 if (ztest_opts
.zo_verbose
>= 6)
6012 (void) printf("\t%s\n", spa_name(spa
));
6013 mutex_exit(&spa_namespace_lock
);
6017 ztest_spa_import_export(char *oldname
, char *newname
)
6019 nvlist_t
*config
, *newconfig
;
6024 if (ztest_opts
.zo_verbose
>= 4) {
6025 (void) printf("import/export: old = %s, new = %s\n",
6030 * Clean up from previous runs.
6032 (void) spa_destroy(newname
);
6035 * Get the pool's configuration and guid.
6037 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6040 * Kick off a scrub to tickle scrub/export races.
6042 if (ztest_random(2) == 0)
6043 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6045 pool_guid
= spa_guid(spa
);
6046 spa_close(spa
, FTAG
);
6048 ztest_walk_pool_directory("pools before export");
6053 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6055 ztest_walk_pool_directory("pools after export");
6060 newconfig
= spa_tryimport(config
);
6061 ASSERT(newconfig
!= NULL
);
6062 nvlist_free(newconfig
);
6065 * Import it under the new name.
6067 error
= spa_import(newname
, config
, NULL
, 0);
6069 dump_nvlist(config
, 0);
6070 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6071 oldname
, newname
, error
);
6074 ztest_walk_pool_directory("pools after import");
6077 * Try to import it again -- should fail with EEXIST.
6079 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6082 * Try to import it under a different name -- should fail with EEXIST.
6084 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6087 * Verify that the pool is no longer visible under the old name.
6089 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6092 * Verify that we can open and close the pool using the new name.
6094 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6095 ASSERT(pool_guid
== spa_guid(spa
));
6096 spa_close(spa
, FTAG
);
6098 nvlist_free(config
);
6102 ztest_resume(spa_t
*spa
)
6104 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6105 (void) printf("resuming from suspended state\n");
6106 spa_vdev_state_enter(spa
, SCL_NONE
);
6107 vdev_clear(spa
, NULL
);
6108 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6109 (void) zio_resume(spa
);
6113 ztest_resume_thread(void *arg
)
6117 while (!ztest_exiting
) {
6118 if (spa_suspended(spa
))
6120 (void) poll(NULL
, 0, 100);
6123 * Periodically change the zfs_compressed_arc_enabled setting.
6125 if (ztest_random(10) == 0)
6126 zfs_compressed_arc_enabled
= ztest_random(2);
6129 * Periodically change the zfs_abd_scatter_enabled setting.
6131 if (ztest_random(10) == 0)
6132 zfs_abd_scatter_enabled
= ztest_random(2);
6144 ztest_deadman_alarm(int sig
)
6146 fatal(0, "failed to complete within %d seconds of deadline", GRACE
);
6151 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6153 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6154 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6155 hrtime_t functime
= gethrtime();
6158 for (i
= 0; i
< zi
->zi_iters
; i
++)
6159 zi
->zi_func(zd
, id
);
6161 functime
= gethrtime() - functime
;
6163 atomic_add_64(&zc
->zc_count
, 1);
6164 atomic_add_64(&zc
->zc_time
, functime
);
6166 if (ztest_opts
.zo_verbose
>= 4)
6167 (void) printf("%6.2f sec in %s\n",
6168 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6172 ztest_thread(void *arg
)
6175 uint64_t id
= (uintptr_t)arg
;
6176 ztest_shared_t
*zs
= ztest_shared
;
6180 ztest_shared_callstate_t
*zc
;
6182 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6184 * See if it's time to force a crash.
6186 if (now
> zs
->zs_thread_kill
)
6190 * If we're getting ENOSPC with some regularity, stop.
6192 if (zs
->zs_enospc_count
> 10)
6196 * Pick a random function to execute.
6198 rand
= ztest_random(ZTEST_FUNCS
);
6199 zi
= &ztest_info
[rand
];
6200 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6201 call_next
= zc
->zc_next
;
6203 if (now
>= call_next
&&
6204 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6205 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6206 ztest_execute(rand
, zi
, id
);
6216 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6218 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6222 ztest_dataset_destroy(int d
)
6224 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6227 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6229 if (ztest_opts
.zo_verbose
>= 3)
6230 (void) printf("Destroying %s to free up space\n", name
);
6233 * Cleanup any non-standard clones and snapshots. In general,
6234 * ztest thread t operates on dataset (t % zopt_datasets),
6235 * so there may be more than one thing to clean up.
6237 for (t
= d
; t
< ztest_opts
.zo_threads
;
6238 t
+= ztest_opts
.zo_datasets
)
6239 ztest_dsl_dataset_cleanup(name
, t
);
6241 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6242 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6246 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6248 uint64_t usedobjs
, dirobjs
, scratch
;
6251 * ZTEST_DIROBJ is the object directory for the entire dataset.
6252 * Therefore, the number of objects in use should equal the
6253 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6254 * If not, we have an object leak.
6256 * Note that we can only check this in ztest_dataset_open(),
6257 * when the open-context and syncing-context values agree.
6258 * That's because zap_count() returns the open-context value,
6259 * while dmu_objset_space() returns the rootbp fill count.
6261 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6262 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6263 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6267 ztest_dataset_open(int d
)
6269 ztest_ds_t
*zd
= &ztest_ds
[d
];
6270 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6273 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6276 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6278 (void) rw_rdlock(&ztest_name_lock
);
6280 error
= ztest_dataset_create(name
);
6281 if (error
== ENOSPC
) {
6282 (void) rw_unlock(&ztest_name_lock
);
6283 ztest_record_enospc(FTAG
);
6286 ASSERT(error
== 0 || error
== EEXIST
);
6288 VERIFY0(dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, zd
, &os
));
6289 (void) rw_unlock(&ztest_name_lock
);
6291 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6293 zilog
= zd
->zd_zilog
;
6295 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6296 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6297 fatal(0, "missing log records: claimed %llu < committed %llu",
6298 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6300 ztest_dataset_dirobj_verify(zd
);
6302 zil_replay(os
, zd
, ztest_replay_vector
);
6304 ztest_dataset_dirobj_verify(zd
);
6306 if (ztest_opts
.zo_verbose
>= 6)
6307 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6309 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6310 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6311 (u_longlong_t
)zilog
->zl_replaying_seq
);
6313 zilog
= zil_open(os
, ztest_get_data
);
6315 if (zilog
->zl_replaying_seq
!= 0 &&
6316 zilog
->zl_replaying_seq
< committed_seq
)
6317 fatal(0, "missing log records: replayed %llu < committed %llu",
6318 zilog
->zl_replaying_seq
, committed_seq
);
6324 ztest_dataset_close(int d
)
6326 ztest_ds_t
*zd
= &ztest_ds
[d
];
6328 zil_close(zd
->zd_zilog
);
6329 dmu_objset_disown(zd
->zd_os
, zd
);
6335 * Kick off threads to run tests on all datasets in parallel.
6338 ztest_run(ztest_shared_t
*zs
)
6343 kthread_t
*resume_thread
;
6348 ztest_exiting
= B_FALSE
;
6351 * Initialize parent/child shared state.
6353 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6354 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6356 zs
->zs_thread_start
= gethrtime();
6357 zs
->zs_thread_stop
=
6358 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6359 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6360 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6361 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6362 zs
->zs_thread_kill
-=
6363 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6366 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6368 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6369 offsetof(ztest_cb_data_t
, zcd_node
));
6374 kernel_init(FREAD
| FWRITE
);
6375 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6376 spa
->spa_debug
= B_TRUE
;
6377 metaslab_preload_limit
= ztest_random(20) + 1;
6380 dmu_objset_stats_t dds
;
6381 VERIFY0(dmu_objset_own(ztest_opts
.zo_pool
,
6382 DMU_OST_ANY
, B_TRUE
, FTAG
, &os
));
6383 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6384 dmu_objset_fast_stat(os
, &dds
);
6385 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6386 zs
->zs_guid
= dds
.dds_guid
;
6387 dmu_objset_disown(os
, FTAG
);
6389 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6392 * We don't expect the pool to suspend unless maxfaults == 0,
6393 * in which case ztest_fault_inject() temporarily takes away
6394 * the only valid replica.
6396 if (MAXFAULTS() == 0)
6397 spa
->spa_failmode
= ZIO_FAILURE_MODE_WAIT
;
6399 spa
->spa_failmode
= ZIO_FAILURE_MODE_PANIC
;
6402 * Create a thread to periodically resume suspended I/O.
6404 VERIFY3P((resume_thread
= zk_thread_create(NULL
, 0,
6405 (thread_func_t
)ztest_resume_thread
, spa
, 0, NULL
, TS_RUN
, 0,
6406 PTHREAD_CREATE_JOINABLE
)), !=, NULL
);
6410 * Set a deadman alarm to abort() if we hang.
6412 signal(SIGALRM
, ztest_deadman_alarm
);
6413 alarm((zs
->zs_thread_stop
- zs
->zs_thread_start
) / NANOSEC
+ GRACE
);
6417 * Verify that we can safely inquire about about any object,
6418 * whether it's allocated or not. To make it interesting,
6419 * we probe a 5-wide window around each power of two.
6420 * This hits all edge cases, including zero and the max.
6422 for (t
= 0; t
< 64; t
++) {
6423 for (d
= -5; d
<= 5; d
++) {
6424 error
= dmu_object_info(spa
->spa_meta_objset
,
6425 (1ULL << t
) + d
, NULL
);
6426 ASSERT(error
== 0 || error
== ENOENT
||
6432 * If we got any ENOSPC errors on the previous run, destroy something.
6434 if (zs
->zs_enospc_count
!= 0) {
6435 int d
= ztest_random(ztest_opts
.zo_datasets
);
6436 ztest_dataset_destroy(d
);
6438 zs
->zs_enospc_count
= 0;
6440 tid
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kt_did_t
),
6443 if (ztest_opts
.zo_verbose
>= 4)
6444 (void) printf("starting main threads...\n");
6447 * Kick off all the tests that run in parallel.
6449 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6452 if (t
< ztest_opts
.zo_datasets
&&
6453 ztest_dataset_open(t
) != 0) {
6455 ztest_opts
.zo_threads
* sizeof (kt_did_t
));
6459 VERIFY3P(thread
= zk_thread_create(NULL
, 0,
6460 (thread_func_t
)ztest_thread
,
6461 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
, 0,
6462 PTHREAD_CREATE_JOINABLE
), !=, NULL
);
6463 tid
[t
] = thread
->t_tid
;
6467 * Wait for all of the tests to complete. We go in reverse order
6468 * so we don't close datasets while threads are still using them.
6470 for (t
= ztest_opts
.zo_threads
- 1; t
>= 0; t
--) {
6471 thread_join(tid
[t
]);
6472 if (t
< ztest_opts
.zo_datasets
)
6473 ztest_dataset_close(t
);
6476 txg_wait_synced(spa_get_dsl(spa
), 0);
6478 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
6479 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
6481 umem_free(tid
, ztest_opts
.zo_threads
* sizeof (kt_did_t
));
6483 /* Kill the resume thread */
6484 ztest_exiting
= B_TRUE
;
6485 thread_join(resume_thread
->t_tid
);
6489 * Right before closing the pool, kick off a bunch of async I/O;
6490 * spa_close() should wait for it to complete.
6492 for (object
= 1; object
< 50; object
++) {
6493 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
6494 ZIO_PRIORITY_SYNC_READ
);
6497 /* Verify that at least one commit cb was called in a timely fashion */
6498 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
6499 VERIFY0(zc_min_txg_delay
);
6501 spa_close(spa
, FTAG
);
6504 * Verify that we can loop over all pools.
6506 mutex_enter(&spa_namespace_lock
);
6507 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
6508 if (ztest_opts
.zo_verbose
> 3)
6509 (void) printf("spa_next: found %s\n", spa_name(spa
));
6510 mutex_exit(&spa_namespace_lock
);
6513 * Verify that we can export the pool and reimport it under a
6516 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
6517 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6518 (void) snprintf(name
, sizeof (name
), "%s_import",
6519 ztest_opts
.zo_pool
);
6520 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
6521 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
6526 list_destroy(&zcl
.zcl_callbacks
);
6527 mutex_destroy(&zcl
.zcl_callbacks_lock
);
6528 (void) rwlock_destroy(&ztest_name_lock
);
6529 mutex_destroy(&ztest_vdev_lock
);
6535 ztest_ds_t
*zd
= &ztest_ds
[0];
6539 if (ztest_opts
.zo_verbose
>= 3)
6540 (void) printf("testing spa_freeze()...\n");
6542 kernel_init(FREAD
| FWRITE
);
6543 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6544 VERIFY3U(0, ==, ztest_dataset_open(0));
6545 spa
->spa_debug
= B_TRUE
;
6549 * Force the first log block to be transactionally allocated.
6550 * We have to do this before we freeze the pool -- otherwise
6551 * the log chain won't be anchored.
6553 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
6554 ztest_dmu_object_alloc_free(zd
, 0);
6555 zil_commit(zd
->zd_zilog
, 0);
6558 txg_wait_synced(spa_get_dsl(spa
), 0);
6561 * Freeze the pool. This stops spa_sync() from doing anything,
6562 * so that the only way to record changes from now on is the ZIL.
6567 * Because it is hard to predict how much space a write will actually
6568 * require beforehand, we leave ourselves some fudge space to write over
6571 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
6574 * Run tests that generate log records but don't alter the pool config
6575 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6576 * We do a txg_wait_synced() after each iteration to force the txg
6577 * to increase well beyond the last synced value in the uberblock.
6578 * The ZIL should be OK with that.
6580 * Run a random number of times less than zo_maxloops and ensure we do
6581 * not run out of space on the pool.
6583 while (ztest_random(10) != 0 &&
6584 numloops
++ < ztest_opts
.zo_maxloops
&&
6585 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
6587 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
6588 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
6589 ztest_io(zd
, od
.od_object
,
6590 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
6591 txg_wait_synced(spa_get_dsl(spa
), 0);
6595 * Commit all of the changes we just generated.
6597 zil_commit(zd
->zd_zilog
, 0);
6598 txg_wait_synced(spa_get_dsl(spa
), 0);
6601 * Close our dataset and close the pool.
6603 ztest_dataset_close(0);
6604 spa_close(spa
, FTAG
);
6608 * Open and close the pool and dataset to induce log replay.
6610 kernel_init(FREAD
| FWRITE
);
6611 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6612 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
6613 VERIFY3U(0, ==, ztest_dataset_open(0));
6614 ztest_dataset_close(0);
6616 spa
->spa_debug
= B_TRUE
;
6618 txg_wait_synced(spa_get_dsl(spa
), 0);
6619 ztest_reguid(NULL
, 0);
6621 spa_close(spa
, FTAG
);
6626 print_time(hrtime_t t
, char *timebuf
)
6628 hrtime_t s
= t
/ NANOSEC
;
6629 hrtime_t m
= s
/ 60;
6630 hrtime_t h
= m
/ 60;
6631 hrtime_t d
= h
/ 24;
6640 (void) sprintf(timebuf
,
6641 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
6643 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
6645 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
6647 (void) sprintf(timebuf
, "%llus", s
);
6651 make_random_props(void)
6655 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
6656 if (ztest_random(2) == 0)
6658 VERIFY(nvlist_add_uint64(props
, "autoreplace", 1) == 0);
6664 * Import a storage pool with the given name.
6667 ztest_import(ztest_shared_t
*zs
)
6669 libzfs_handle_t
*hdl
;
6670 importargs_t args
= { 0 };
6672 nvlist_t
*cfg
= NULL
;
6674 char *searchdirs
[nsearch
];
6675 char *name
= ztest_opts
.zo_pool
;
6676 int flags
= ZFS_IMPORT_MISSING_LOG
;
6679 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6680 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6682 kernel_init(FREAD
| FWRITE
);
6683 hdl
= libzfs_init();
6685 searchdirs
[0] = ztest_opts
.zo_dir
;
6686 args
.paths
= nsearch
;
6687 args
.path
= searchdirs
;
6688 args
.can_be_active
= B_FALSE
;
6690 error
= zpool_tryimport(hdl
, name
, &cfg
, &args
);
6692 (void) fatal(0, "No pools found\n");
6694 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
6695 VERIFY0(spa_open(name
, &spa
, FTAG
));
6696 zs
->zs_metaslab_sz
=
6697 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6698 spa_close(spa
, FTAG
);
6703 if (!ztest_opts
.zo_mmp_test
) {
6704 ztest_run_zdb(ztest_opts
.zo_pool
);
6706 ztest_run_zdb(ztest_opts
.zo_pool
);
6709 (void) rwlock_destroy(&ztest_name_lock
);
6710 mutex_destroy(&ztest_vdev_lock
);
6714 * Create a storage pool with the given name and initial vdev size.
6715 * Then test spa_freeze() functionality.
6718 ztest_init(ztest_shared_t
*zs
)
6721 nvlist_t
*nvroot
, *props
;
6724 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6725 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6727 kernel_init(FREAD
| FWRITE
);
6730 * Create the storage pool.
6732 (void) spa_destroy(ztest_opts
.zo_pool
);
6733 ztest_shared
->zs_vdev_next_leaf
= 0;
6735 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
6736 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
6737 0, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
6738 props
= make_random_props();
6739 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6741 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
6742 spa_feature_table
[i
].fi_uname
));
6743 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
6746 VERIFY3U(0, ==, spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
));
6747 nvlist_free(nvroot
);
6750 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6751 zs
->zs_metaslab_sz
=
6752 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6753 spa_close(spa
, FTAG
);
6757 if (!ztest_opts
.zo_mmp_test
) {
6758 ztest_run_zdb(ztest_opts
.zo_pool
);
6760 ztest_run_zdb(ztest_opts
.zo_pool
);
6763 (void) rwlock_destroy(&ztest_name_lock
);
6764 mutex_destroy(&ztest_vdev_lock
);
6770 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
6772 ztest_fd_data
= mkstemp(ztest_name_data
);
6773 ASSERT3S(ztest_fd_data
, >=, 0);
6774 (void) unlink(ztest_name_data
);
6778 shared_data_size(ztest_shared_hdr_t
*hdr
)
6782 size
= hdr
->zh_hdr_size
;
6783 size
+= hdr
->zh_opts_size
;
6784 size
+= hdr
->zh_size
;
6785 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6786 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
6795 ztest_shared_hdr_t
*hdr
;
6797 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6798 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6799 ASSERT(hdr
!= MAP_FAILED
);
6801 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
6803 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
6804 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
6805 hdr
->zh_size
= sizeof (ztest_shared_t
);
6806 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
6807 hdr
->zh_stats_count
= ZTEST_FUNCS
;
6808 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
6809 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
6811 size
= shared_data_size(hdr
);
6812 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
6814 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6821 ztest_shared_hdr_t
*hdr
;
6824 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6825 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
6826 ASSERT(hdr
!= MAP_FAILED
);
6828 size
= shared_data_size(hdr
);
6830 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6831 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
6832 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6833 ASSERT(hdr
!= MAP_FAILED
);
6834 buf
= (uint8_t *)hdr
;
6836 offset
= hdr
->zh_hdr_size
;
6837 ztest_shared_opts
= (void *)&buf
[offset
];
6838 offset
+= hdr
->zh_opts_size
;
6839 ztest_shared
= (void *)&buf
[offset
];
6840 offset
+= hdr
->zh_size
;
6841 ztest_shared_callstate
= (void *)&buf
[offset
];
6842 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6843 ztest_shared_ds
= (void *)&buf
[offset
];
6847 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
6851 char *cmdbuf
= NULL
;
6856 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
6857 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
6862 fatal(1, "fork failed");
6864 if (pid
== 0) { /* child */
6865 char *emptyargv
[2] = { cmd
, NULL
};
6866 char fd_data_str
[12];
6868 struct rlimit rl
= { 1024, 1024 };
6869 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
6871 (void) close(ztest_fd_rand
);
6872 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
6873 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
6875 (void) enable_extended_FILE_stdio(-1, -1);
6876 if (libpath
!= NULL
)
6877 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
6878 (void) execv(cmd
, emptyargv
);
6879 ztest_dump_core
= B_FALSE
;
6880 fatal(B_TRUE
, "exec failed: %s", cmd
);
6883 if (cmdbuf
!= NULL
) {
6884 umem_free(cmdbuf
, MAXPATHLEN
);
6888 while (waitpid(pid
, &status
, 0) != pid
)
6890 if (statusp
!= NULL
)
6893 if (WIFEXITED(status
)) {
6894 if (WEXITSTATUS(status
) != 0) {
6895 (void) fprintf(stderr
, "child exited with code %d\n",
6896 WEXITSTATUS(status
));
6900 } else if (WIFSIGNALED(status
)) {
6901 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
6902 (void) fprintf(stderr
, "child died with signal %d\n",
6908 (void) fprintf(stderr
, "something strange happened to child\n");
6915 ztest_run_init(void)
6919 ztest_shared_t
*zs
= ztest_shared
;
6922 * Blow away any existing copy of zpool.cache
6924 (void) remove(spa_config_path
);
6926 if (ztest_opts
.zo_init
== 0) {
6927 if (ztest_opts
.zo_verbose
>= 1)
6928 (void) printf("Importing pool %s\n",
6929 ztest_opts
.zo_pool
);
6935 * Create and initialize our storage pool.
6937 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
6938 bzero(zs
, sizeof (ztest_shared_t
));
6939 if (ztest_opts
.zo_verbose
>= 3 &&
6940 ztest_opts
.zo_init
!= 1) {
6941 (void) printf("ztest_init(), pass %d\n", i
);
6948 main(int argc
, char **argv
)
6956 ztest_shared_callstate_t
*zc
;
6963 char *fd_data_str
= getenv("ZTEST_FD_DATA");
6964 struct sigaction action
;
6966 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
6968 dprintf_setup(&argc
, argv
);
6970 action
.sa_handler
= sig_handler
;
6971 sigemptyset(&action
.sa_mask
);
6972 action
.sa_flags
= 0;
6974 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
6975 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
6980 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
6981 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
6986 ztest_fd_rand
= open("/dev/urandom", O_RDONLY
);
6987 ASSERT3S(ztest_fd_rand
, >=, 0);
6990 process_options(argc
, argv
);
6995 bcopy(&ztest_opts
, ztest_shared_opts
,
6996 sizeof (*ztest_shared_opts
));
6998 ztest_fd_data
= atoi(fd_data_str
);
7000 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7002 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7004 /* Override location of zpool.cache */
7005 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7006 ztest_opts
.zo_dir
) != -1);
7008 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7013 metaslab_gang_bang
= ztest_opts
.zo_metaslab_gang_bang
;
7014 metaslab_df_alloc_threshold
=
7015 zs
->zs_metaslab_df_alloc_threshold
;
7024 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7026 if (ztest_opts
.zo_verbose
>= 1) {
7027 (void) printf("%llu vdevs, %d datasets, %d threads,"
7028 " %llu seconds...\n",
7029 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7030 ztest_opts
.zo_datasets
,
7031 ztest_opts
.zo_threads
,
7032 (u_longlong_t
)ztest_opts
.zo_time
);
7035 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7036 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7038 zs
->zs_do_init
= B_TRUE
;
7039 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7040 if (ztest_opts
.zo_verbose
>= 1) {
7041 (void) printf("Executing older ztest for "
7042 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7044 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7045 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7047 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7049 zs
->zs_do_init
= B_FALSE
;
7051 zs
->zs_proc_start
= gethrtime();
7052 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7054 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7055 zi
= &ztest_info
[f
];
7056 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7057 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7058 zc
->zc_next
= UINT64_MAX
;
7060 zc
->zc_next
= zs
->zs_proc_start
+
7061 ztest_random(2 * zi
->zi_interval
[0] + 1);
7065 * Run the tests in a loop. These tests include fault injection
7066 * to verify that self-healing data works, and forced crashes
7067 * to verify that we never lose on-disk consistency.
7069 while (gethrtime() < zs
->zs_proc_stop
) {
7074 * Initialize the workload counters for each function.
7076 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7077 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7082 /* Set the allocation switch size */
7083 zs
->zs_metaslab_df_alloc_threshold
=
7084 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7086 if (!hasalt
|| ztest_random(2) == 0) {
7087 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7088 (void) printf("Executing newer ztest: %s\n",
7092 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7094 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7095 (void) printf("Executing older ztest: %s\n",
7096 ztest_opts
.zo_alt_ztest
);
7099 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7100 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7107 if (ztest_opts
.zo_verbose
>= 1) {
7108 hrtime_t now
= gethrtime();
7110 now
= MIN(now
, zs
->zs_proc_stop
);
7111 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7112 nicenum(zs
->zs_space
, numbuf
);
7114 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7115 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7117 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7118 (u_longlong_t
)zs
->zs_enospc_count
,
7119 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7121 100.0 * (now
- zs
->zs_proc_start
) /
7122 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7125 if (ztest_opts
.zo_verbose
>= 2) {
7126 (void) printf("\nWorkload summary:\n\n");
7127 (void) printf("%7s %9s %s\n",
7128 "Calls", "Time", "Function");
7129 (void) printf("%7s %9s %s\n",
7130 "-----", "----", "--------");
7131 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7132 zi
= &ztest_info
[f
];
7133 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7134 print_time(zc
->zc_time
, timebuf
);
7135 (void) printf("%7llu %9s %s\n",
7136 (u_longlong_t
)zc
->zc_count
, timebuf
,
7139 (void) printf("\n");
7143 * It's possible that we killed a child during a rename test,
7144 * in which case we'll have a 'ztest_tmp' pool lying around
7145 * instead of 'ztest'. Do a blind rename in case this happened.
7148 if (spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
) == 0) {
7149 spa_close(spa
, FTAG
);
7151 char tmpname
[ZFS_MAX_DATASET_NAME_LEN
];
7153 kernel_init(FREAD
| FWRITE
);
7154 (void) snprintf(tmpname
, sizeof (tmpname
), "%s_tmp",
7155 ztest_opts
.zo_pool
);
7156 (void) spa_rename(tmpname
, ztest_opts
.zo_pool
);
7160 if (!ztest_opts
.zo_mmp_test
)
7161 ztest_run_zdb(ztest_opts
.zo_pool
);
7164 if (ztest_opts
.zo_verbose
>= 1) {
7166 (void) printf("%d runs of older ztest: %s\n", older
,
7167 ztest_opts
.zo_alt_ztest
);
7168 (void) printf("%d runs of newer ztest: %s\n", newer
,
7171 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7172 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7175 umem_free(cmd
, MAXNAMELEN
);