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.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
31 * The objective of this program is to provide a DMU/ZAP/SPA stress test
32 * that runs entirely in userland, is easy to use, and easy to extend.
34 * The overall design of the ztest program is as follows:
36 * (1) For each major functional area (e.g. adding vdevs to a pool,
37 * creating and destroying datasets, reading and writing objects, etc)
38 * we have a simple routine to test that functionality. These
39 * individual routines do not have to do anything "stressful".
41 * (2) We turn these simple functionality tests into a stress test by
42 * running them all in parallel, with as many threads as desired,
43 * and spread across as many datasets, objects, and vdevs as desired.
45 * (3) While all this is happening, we inject faults into the pool to
46 * verify that self-healing data really works.
48 * (4) Every time we open a dataset, we change its checksum and compression
49 * functions. Thus even individual objects vary from block to block
50 * in which checksum they use and whether they're compressed.
52 * (5) To verify that we never lose on-disk consistency after a crash,
53 * we run the entire test in a child of the main process.
54 * At random times, the child self-immolates with a SIGKILL.
55 * This is the software equivalent of pulling the power cord.
56 * The parent then runs the test again, using the existing
57 * storage pool, as many times as desired. If backwards compatibility
58 * testing is enabled ztest will sometimes run the "older" version
59 * of ztest after a SIGKILL.
61 * (6) To verify that we don't have future leaks or temporal incursions,
62 * many of the functional tests record the transaction group number
63 * as part of their data. When reading old data, they verify that
64 * the transaction group number is less than the current, open txg.
65 * If you add a new test, please do this if applicable.
67 * (7) Threads are created with a reduced stack size, for sanity checking.
68 * Therefore, it's important not to allocate huge buffers on the stack.
70 * When run with no arguments, ztest runs for about five minutes and
71 * produces no output if successful. To get a little bit of information,
72 * specify -V. To get more information, specify -VV, and so on.
74 * To turn this into an overnight stress test, use -T to specify run time.
76 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
77 * to increase the pool capacity, fanout, and overall stress level.
79 * Use the -k option to set the desired frequency of kills.
81 * When ztest invokes itself it passes all relevant information through a
82 * temporary file which is mmap-ed in the child process. This allows shared
83 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
84 * stored at offset 0 of this file and contains information on the size and
85 * number of shared structures in the file. The information stored in this file
86 * must remain backwards compatible with older versions of ztest so that
87 * ztest can invoke them during backwards compatibility testing (-B).
90 #include <sys/zfs_context.h>
96 #include <sys/dmu_objset.h>
100 #include <sys/wait.h>
101 #include <sys/mman.h>
102 #include <sys/resource.h>
105 #include <sys/zil_impl.h>
106 #include <sys/zfs_rlock.h>
107 #include <sys/vdev_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/spa_impl.h>
110 #include <sys/metaslab_impl.h>
111 #include <sys/dsl_prop.h>
112 #include <sys/dsl_dataset.h>
113 #include <sys/dsl_destroy.h>
114 #include <sys/dsl_scan.h>
115 #include <sys/zio_checksum.h>
116 #include <sys/refcount.h>
117 #include <sys/zfeature.h>
118 #include <sys/dsl_userhold.h>
121 #include <stdio_ext.h>
128 #include <sys/fs/zfs.h>
129 #include <zfs_fletcher.h>
130 #include <libnvpair.h>
133 #include <execinfo.h> /* for backtrace() */
136 static int ztest_fd_data
= -1;
137 static int ztest_fd_rand
= -1;
139 typedef struct ztest_shared_hdr
{
140 uint64_t zh_hdr_size
;
141 uint64_t zh_opts_size
;
143 uint64_t zh_stats_size
;
144 uint64_t zh_stats_count
;
146 uint64_t zh_ds_count
;
147 } ztest_shared_hdr_t
;
149 static ztest_shared_hdr_t
*ztest_shared_hdr
;
151 typedef struct ztest_shared_opts
{
152 char zo_pool
[ZFS_MAX_DATASET_NAME_LEN
];
153 char zo_dir
[ZFS_MAX_DATASET_NAME_LEN
];
154 char zo_alt_ztest
[MAXNAMELEN
];
155 char zo_alt_libpath
[MAXNAMELEN
];
157 uint64_t zo_vdevtime
;
165 uint64_t zo_passtime
;
166 uint64_t zo_killrate
;
170 uint64_t zo_maxloops
;
171 uint64_t zo_metaslab_gang_bang
;
173 } ztest_shared_opts_t
;
175 static const ztest_shared_opts_t ztest_opts_defaults
= {
176 .zo_pool
= { 'z', 't', 'e', 's', 't', '\0' },
177 .zo_dir
= { '/', 't', 'm', 'p', '\0' },
178 .zo_alt_ztest
= { '\0' },
179 .zo_alt_libpath
= { '\0' },
181 .zo_ashift
= SPA_MINBLOCKSHIFT
,
184 .zo_raidz_parity
= 1,
185 .zo_vdev_size
= SPA_MINDEVSIZE
* 4, /* 256m default size */
188 .zo_passtime
= 60, /* 60 seconds */
189 .zo_killrate
= 70, /* 70% kill rate */
193 .zo_time
= 300, /* 5 minutes */
194 .zo_maxloops
= 50, /* max loops during spa_freeze() */
195 .zo_metaslab_gang_bang
= 32 << 10
198 extern uint64_t metaslab_gang_bang
;
199 extern uint64_t metaslab_df_alloc_threshold
;
200 extern unsigned long zfs_deadman_synctime_ms
;
201 extern int metaslab_preload_limit
;
202 extern boolean_t zfs_compressed_arc_enabled
;
203 extern int zfs_abd_scatter_enabled
;
204 extern int dmu_object_alloc_chunk_shift
;
206 static ztest_shared_opts_t
*ztest_shared_opts
;
207 static ztest_shared_opts_t ztest_opts
;
208 static char *ztest_wkeydata
= "abcdefghijklmnopqrstuvwxyz012345";
210 typedef struct ztest_shared_ds
{
214 static ztest_shared_ds_t
*ztest_shared_ds
;
215 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
217 #define BT_MAGIC 0x123456789abcdefULL
218 #define MAXFAULTS(zs) \
219 (MAX((zs)->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
223 ZTEST_IO_WRITE_PATTERN
,
224 ZTEST_IO_WRITE_ZEROES
,
231 typedef struct ztest_block_tag
{
235 uint64_t bt_dnodesize
;
242 typedef struct bufwad
{
260 #define ZTEST_RANGE_LOCKS 64
261 #define ZTEST_OBJECT_LOCKS 64
264 * Object descriptor. Used as a template for object lookup/create/remove.
266 typedef struct ztest_od
{
269 dmu_object_type_t od_type
;
270 dmu_object_type_t od_crtype
;
271 uint64_t od_blocksize
;
272 uint64_t od_crblocksize
;
273 uint64_t od_crdnodesize
;
276 char od_name
[ZFS_MAX_DATASET_NAME_LEN
];
282 typedef struct ztest_ds
{
283 ztest_shared_ds_t
*zd_shared
;
285 rwlock_t zd_zilog_lock
;
287 ztest_od_t
*zd_od
; /* debugging aid */
288 char zd_name
[ZFS_MAX_DATASET_NAME_LEN
];
289 kmutex_t zd_dirobj_lock
;
290 rll_t zd_object_lock
[ZTEST_OBJECT_LOCKS
];
291 zll_t zd_range_lock
[ZTEST_RANGE_LOCKS
];
295 * Per-iteration state.
297 typedef void ztest_func_t(ztest_ds_t
*zd
, uint64_t id
);
299 typedef struct ztest_info
{
300 ztest_func_t
*zi_func
; /* test function */
301 uint64_t zi_iters
; /* iterations per execution */
302 uint64_t *zi_interval
; /* execute every <interval> seconds */
303 const char *zi_funcname
; /* name of test function */
306 typedef struct ztest_shared_callstate
{
307 uint64_t zc_count
; /* per-pass count */
308 uint64_t zc_time
; /* per-pass time */
309 uint64_t zc_next
; /* next time to call this function */
310 } ztest_shared_callstate_t
;
312 static ztest_shared_callstate_t
*ztest_shared_callstate
;
313 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
315 ztest_func_t ztest_dmu_read_write
;
316 ztest_func_t ztest_dmu_write_parallel
;
317 ztest_func_t ztest_dmu_object_alloc_free
;
318 ztest_func_t ztest_dmu_object_next_chunk
;
319 ztest_func_t ztest_dmu_commit_callbacks
;
320 ztest_func_t ztest_zap
;
321 ztest_func_t ztest_zap_parallel
;
322 ztest_func_t ztest_zil_commit
;
323 ztest_func_t ztest_zil_remount
;
324 ztest_func_t ztest_dmu_read_write_zcopy
;
325 ztest_func_t ztest_dmu_objset_create_destroy
;
326 ztest_func_t ztest_dmu_prealloc
;
327 ztest_func_t ztest_fzap
;
328 ztest_func_t ztest_dmu_snapshot_create_destroy
;
329 ztest_func_t ztest_dsl_prop_get_set
;
330 ztest_func_t ztest_spa_prop_get_set
;
331 ztest_func_t ztest_spa_create_destroy
;
332 ztest_func_t ztest_fault_inject
;
333 ztest_func_t ztest_ddt_repair
;
334 ztest_func_t ztest_dmu_snapshot_hold
;
335 ztest_func_t ztest_mmp_enable_disable
;
336 ztest_func_t ztest_spa_rename
;
337 ztest_func_t ztest_scrub
;
338 ztest_func_t ztest_dsl_dataset_promote_busy
;
339 ztest_func_t ztest_vdev_attach_detach
;
340 ztest_func_t ztest_vdev_LUN_growth
;
341 ztest_func_t ztest_vdev_add_remove
;
342 ztest_func_t ztest_vdev_aux_add_remove
;
343 ztest_func_t ztest_split_pool
;
344 ztest_func_t ztest_reguid
;
345 ztest_func_t ztest_spa_upgrade
;
346 ztest_func_t ztest_fletcher
;
347 ztest_func_t ztest_fletcher_incr
;
348 ztest_func_t ztest_verify_dnode_bt
;
350 uint64_t zopt_always
= 0ULL * NANOSEC
; /* all the time */
351 uint64_t zopt_incessant
= 1ULL * NANOSEC
/ 10; /* every 1/10 second */
352 uint64_t zopt_often
= 1ULL * NANOSEC
; /* every second */
353 uint64_t zopt_sometimes
= 10ULL * NANOSEC
; /* every 10 seconds */
354 uint64_t zopt_rarely
= 60ULL * NANOSEC
; /* every 60 seconds */
356 #define ZTI_INIT(func, iters, interval) \
357 { .zi_func = (func), \
358 .zi_iters = (iters), \
359 .zi_interval = (interval), \
360 .zi_funcname = # func }
362 ztest_info_t ztest_info
[] = {
363 ZTI_INIT(ztest_dmu_read_write
, 1, &zopt_always
),
364 ZTI_INIT(ztest_dmu_write_parallel
, 10, &zopt_always
),
365 ZTI_INIT(ztest_dmu_object_alloc_free
, 1, &zopt_always
),
366 ZTI_INIT(ztest_dmu_object_next_chunk
, 1, &zopt_sometimes
),
367 ZTI_INIT(ztest_dmu_commit_callbacks
, 1, &zopt_always
),
368 ZTI_INIT(ztest_zap
, 30, &zopt_always
),
369 ZTI_INIT(ztest_zap_parallel
, 100, &zopt_always
),
370 ZTI_INIT(ztest_split_pool
, 1, &zopt_always
),
371 ZTI_INIT(ztest_zil_commit
, 1, &zopt_incessant
),
372 ZTI_INIT(ztest_zil_remount
, 1, &zopt_sometimes
),
373 ZTI_INIT(ztest_dmu_read_write_zcopy
, 1, &zopt_often
),
374 ZTI_INIT(ztest_dmu_objset_create_destroy
, 1, &zopt_often
),
375 ZTI_INIT(ztest_dsl_prop_get_set
, 1, &zopt_often
),
376 ZTI_INIT(ztest_spa_prop_get_set
, 1, &zopt_sometimes
),
378 ZTI_INIT(ztest_dmu_prealloc
, 1, &zopt_sometimes
),
380 ZTI_INIT(ztest_fzap
, 1, &zopt_sometimes
),
381 ZTI_INIT(ztest_dmu_snapshot_create_destroy
, 1, &zopt_sometimes
),
382 ZTI_INIT(ztest_spa_create_destroy
, 1, &zopt_sometimes
),
383 ZTI_INIT(ztest_fault_inject
, 1, &zopt_sometimes
),
384 ZTI_INIT(ztest_ddt_repair
, 1, &zopt_sometimes
),
385 ZTI_INIT(ztest_dmu_snapshot_hold
, 1, &zopt_sometimes
),
386 ZTI_INIT(ztest_mmp_enable_disable
, 1, &zopt_sometimes
),
387 ZTI_INIT(ztest_reguid
, 1, &zopt_rarely
),
388 ZTI_INIT(ztest_spa_rename
, 1, &zopt_rarely
),
389 ZTI_INIT(ztest_scrub
, 1, &zopt_rarely
),
390 ZTI_INIT(ztest_spa_upgrade
, 1, &zopt_rarely
),
391 ZTI_INIT(ztest_dsl_dataset_promote_busy
, 1, &zopt_rarely
),
392 ZTI_INIT(ztest_vdev_attach_detach
, 1, &zopt_sometimes
),
393 ZTI_INIT(ztest_vdev_LUN_growth
, 1, &zopt_rarely
),
394 ZTI_INIT(ztest_vdev_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
395 ZTI_INIT(ztest_vdev_aux_add_remove
, 1, &ztest_opts
.zo_vdevtime
),
396 ZTI_INIT(ztest_fletcher
, 1, &zopt_rarely
),
397 ZTI_INIT(ztest_fletcher_incr
, 1, &zopt_rarely
),
398 ZTI_INIT(ztest_verify_dnode_bt
, 1, &zopt_sometimes
),
401 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
404 * The following struct is used to hold a list of uncalled commit callbacks.
405 * The callbacks are ordered by txg number.
407 typedef struct ztest_cb_list
{
408 kmutex_t zcl_callbacks_lock
;
409 list_t zcl_callbacks
;
413 * Stuff we need to share writably between parent and child.
415 typedef struct ztest_shared
{
416 boolean_t zs_do_init
;
417 hrtime_t zs_proc_start
;
418 hrtime_t zs_proc_stop
;
419 hrtime_t zs_thread_start
;
420 hrtime_t zs_thread_stop
;
421 hrtime_t zs_thread_kill
;
422 uint64_t zs_enospc_count
;
423 uint64_t zs_vdev_next_leaf
;
424 uint64_t zs_vdev_aux
;
429 uint64_t zs_metaslab_sz
;
430 uint64_t zs_metaslab_df_alloc_threshold
;
434 #define ID_PARALLEL -1ULL
436 static char ztest_dev_template
[] = "%s/%s.%llua";
437 static char ztest_aux_template
[] = "%s/%s.%s.%llu";
438 ztest_shared_t
*ztest_shared
;
440 static spa_t
*ztest_spa
= NULL
;
441 static ztest_ds_t
*ztest_ds
;
443 static kmutex_t ztest_vdev_lock
;
446 * The ztest_name_lock protects the pool and dataset namespace used by
447 * the individual tests. To modify the namespace, consumers must grab
448 * this lock as writer. Grabbing the lock as reader will ensure that the
449 * namespace does not change while the lock is held.
451 static rwlock_t ztest_name_lock
;
453 static boolean_t ztest_dump_core
= B_TRUE
;
454 static boolean_t ztest_dump_debug_buffer
= B_FALSE
;
455 static boolean_t ztest_exiting
;
457 /* Global commit callback list */
458 static ztest_cb_list_t zcl
;
459 /* Commit cb delay */
460 static uint64_t zc_min_txg_delay
= UINT64_MAX
;
461 static int zc_cb_counter
= 0;
464 * Minimum number of commit callbacks that need to be registered for us to check
465 * whether the minimum txg delay is acceptable.
467 #define ZTEST_COMMIT_CB_MIN_REG 100
470 * If a number of txgs equal to this threshold have been created after a commit
471 * callback has been registered but not called, then we assume there is an
472 * implementation bug.
474 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
476 extern uint64_t metaslab_gang_bang
;
477 extern uint64_t metaslab_df_alloc_threshold
;
480 ZTEST_META_DNODE
= 0,
485 static void usage(boolean_t
) __NORETURN
;
488 * These libumem hooks provide a reasonable set of defaults for the allocator's
489 * debugging facilities.
492 _umem_debug_init(void)
494 return ("default,verbose"); /* $UMEM_DEBUG setting */
498 _umem_logging_init(void)
500 return ("fail,contents"); /* $UMEM_LOGGING setting */
504 dump_debug_buffer(void)
506 if (!ztest_dump_debug_buffer
)
510 zfs_dbgmsg_print("ztest");
513 #define BACKTRACE_SZ 100
515 static void sig_handler(int signo
)
517 struct sigaction action
;
518 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
520 void *buffer
[BACKTRACE_SZ
];
522 nptrs
= backtrace(buffer
, BACKTRACE_SZ
);
523 backtrace_symbols_fd(buffer
, nptrs
, STDERR_FILENO
);
528 * Restore default action and re-raise signal so SIGSEGV and
529 * SIGABRT can trigger a core dump.
531 action
.sa_handler
= SIG_DFL
;
532 sigemptyset(&action
.sa_mask
);
534 (void) sigaction(signo
, &action
, NULL
);
538 #define FATAL_MSG_SZ 1024
543 fatal(int do_perror
, char *message
, ...)
546 int save_errno
= errno
;
549 (void) fflush(stdout
);
550 buf
= umem_alloc(FATAL_MSG_SZ
, UMEM_NOFAIL
);
552 va_start(args
, message
);
553 (void) sprintf(buf
, "ztest: ");
555 (void) vsprintf(buf
+ strlen(buf
), message
, args
);
558 (void) snprintf(buf
+ strlen(buf
), FATAL_MSG_SZ
- strlen(buf
),
559 ": %s", strerror(save_errno
));
561 (void) fprintf(stderr
, "%s\n", buf
);
562 fatal_msg
= buf
; /* to ease debugging */
572 str2shift(const char *buf
)
574 const char *ends
= "BKMGTPEZ";
579 for (i
= 0; i
< strlen(ends
); i
++) {
580 if (toupper(buf
[0]) == ends
[i
])
583 if (i
== strlen(ends
)) {
584 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n",
588 if (buf
[1] == '\0' || (toupper(buf
[1]) == 'B' && buf
[2] == '\0')) {
591 (void) fprintf(stderr
, "ztest: invalid bytes suffix: %s\n", buf
);
597 nicenumtoull(const char *buf
)
602 val
= strtoull(buf
, &end
, 0);
604 (void) fprintf(stderr
, "ztest: bad numeric value: %s\n", buf
);
606 } else if (end
[0] == '.') {
607 double fval
= strtod(buf
, &end
);
608 fval
*= pow(2, str2shift(end
));
609 if (fval
> UINT64_MAX
) {
610 (void) fprintf(stderr
, "ztest: value too large: %s\n",
614 val
= (uint64_t)fval
;
616 int shift
= str2shift(end
);
617 if (shift
>= 64 || (val
<< shift
) >> shift
!= val
) {
618 (void) fprintf(stderr
, "ztest: value too large: %s\n",
628 usage(boolean_t requested
)
630 const ztest_shared_opts_t
*zo
= &ztest_opts_defaults
;
632 char nice_vdev_size
[NN_NUMBUF_SZ
];
633 char nice_gang_bang
[NN_NUMBUF_SZ
];
634 FILE *fp
= requested
? stdout
: stderr
;
636 nicenum(zo
->zo_vdev_size
, nice_vdev_size
, sizeof (nice_vdev_size
));
637 nicenum(zo
->zo_metaslab_gang_bang
, nice_gang_bang
,
638 sizeof (nice_gang_bang
));
640 (void) fprintf(fp
, "Usage: %s\n"
641 "\t[-v vdevs (default: %llu)]\n"
642 "\t[-s size_of_each_vdev (default: %s)]\n"
643 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
644 "\t[-m mirror_copies (default: %d)]\n"
645 "\t[-r raidz_disks (default: %d)]\n"
646 "\t[-R raidz_parity (default: %d)]\n"
647 "\t[-d datasets (default: %d)]\n"
648 "\t[-t threads (default: %d)]\n"
649 "\t[-g gang_block_threshold (default: %s)]\n"
650 "\t[-i init_count (default: %d)] initialize pool i times\n"
651 "\t[-k kill_percentage (default: %llu%%)]\n"
652 "\t[-p pool_name (default: %s)]\n"
653 "\t[-f dir (default: %s)] file directory for vdev files\n"
654 "\t[-M] Multi-host simulate pool imported on remote host\n"
655 "\t[-V] verbose (use multiple times for ever more blather)\n"
656 "\t[-E] use existing pool instead of creating new one\n"
657 "\t[-T time (default: %llu sec)] total run time\n"
658 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
659 "\t[-P passtime (default: %llu sec)] time per pass\n"
660 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
661 "\t[-o variable=value] ... set global variable to an unsigned\n"
662 "\t 32-bit integer value\n"
663 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
664 "\t[-h] (print help)\n"
667 (u_longlong_t
)zo
->zo_vdevs
, /* -v */
668 nice_vdev_size
, /* -s */
669 zo
->zo_ashift
, /* -a */
670 zo
->zo_mirrors
, /* -m */
671 zo
->zo_raidz
, /* -r */
672 zo
->zo_raidz_parity
, /* -R */
673 zo
->zo_datasets
, /* -d */
674 zo
->zo_threads
, /* -t */
675 nice_gang_bang
, /* -g */
676 zo
->zo_init
, /* -i */
677 (u_longlong_t
)zo
->zo_killrate
, /* -k */
678 zo
->zo_pool
, /* -p */
680 (u_longlong_t
)zo
->zo_time
, /* -T */
681 (u_longlong_t
)zo
->zo_maxloops
, /* -F */
682 (u_longlong_t
)zo
->zo_passtime
);
683 exit(requested
? 0 : 1);
687 process_options(int argc
, char **argv
)
690 ztest_shared_opts_t
*zo
= &ztest_opts
;
694 char altdir
[MAXNAMELEN
] = { 0 };
696 bcopy(&ztest_opts_defaults
, zo
, sizeof (*zo
));
698 while ((opt
= getopt(argc
, argv
,
699 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:G")) != EOF
) {
716 value
= nicenumtoull(optarg
);
720 zo
->zo_vdevs
= value
;
723 zo
->zo_vdev_size
= MAX(SPA_MINDEVSIZE
, value
);
726 zo
->zo_ashift
= value
;
729 zo
->zo_mirrors
= value
;
732 zo
->zo_raidz
= MAX(1, value
);
735 zo
->zo_raidz_parity
= MIN(MAX(value
, 1), 3);
738 zo
->zo_datasets
= MAX(1, value
);
741 zo
->zo_threads
= MAX(1, value
);
744 zo
->zo_metaslab_gang_bang
= MAX(SPA_MINBLOCKSIZE
<< 1,
751 zo
->zo_killrate
= value
;
754 (void) strlcpy(zo
->zo_pool
, optarg
,
755 sizeof (zo
->zo_pool
));
758 path
= realpath(optarg
, NULL
);
760 (void) fprintf(stderr
, "error: %s: %s\n",
761 optarg
, strerror(errno
));
764 (void) strlcpy(zo
->zo_dir
, path
,
765 sizeof (zo
->zo_dir
));
782 zo
->zo_passtime
= MAX(1, value
);
785 zo
->zo_maxloops
= MAX(1, value
);
788 (void) strlcpy(altdir
, optarg
, sizeof (altdir
));
791 if (set_global_var(optarg
) != 0)
795 ztest_dump_debug_buffer
= B_TRUE
;
807 zo
->zo_raidz_parity
= MIN(zo
->zo_raidz_parity
, zo
->zo_raidz
- 1);
810 (zo
->zo_vdevs
> 0 ? zo
->zo_time
* NANOSEC
/ zo
->zo_vdevs
:
813 if (strlen(altdir
) > 0) {
821 cmd
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
822 realaltdir
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
824 VERIFY(NULL
!= realpath(getexecname(), cmd
));
825 if (0 != access(altdir
, F_OK
)) {
826 ztest_dump_core
= B_FALSE
;
827 fatal(B_TRUE
, "invalid alternate ztest path: %s",
830 VERIFY(NULL
!= realpath(altdir
, realaltdir
));
833 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
834 * We want to extract <isa> to determine if we should use
835 * 32 or 64 bit binaries.
837 bin
= strstr(cmd
, "/usr/bin/");
838 ztest
= strstr(bin
, "/ztest");
840 isalen
= ztest
- isa
;
841 (void) snprintf(zo
->zo_alt_ztest
, sizeof (zo
->zo_alt_ztest
),
842 "%s/usr/bin/%.*s/ztest", realaltdir
, isalen
, isa
);
843 (void) snprintf(zo
->zo_alt_libpath
, sizeof (zo
->zo_alt_libpath
),
844 "%s/usr/lib/%.*s", realaltdir
, isalen
, isa
);
846 if (0 != access(zo
->zo_alt_ztest
, X_OK
)) {
847 ztest_dump_core
= B_FALSE
;
848 fatal(B_TRUE
, "invalid alternate ztest: %s",
850 } else if (0 != access(zo
->zo_alt_libpath
, X_OK
)) {
851 ztest_dump_core
= B_FALSE
;
852 fatal(B_TRUE
, "invalid alternate lib directory %s",
856 umem_free(cmd
, MAXPATHLEN
);
857 umem_free(realaltdir
, MAXPATHLEN
);
862 ztest_kill(ztest_shared_t
*zs
)
864 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(ztest_spa
));
865 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(ztest_spa
));
868 * Before we kill off ztest, make sure that the config is updated.
869 * See comment above spa_config_sync().
871 mutex_enter(&spa_namespace_lock
);
872 spa_config_sync(ztest_spa
, B_FALSE
, B_FALSE
);
873 mutex_exit(&spa_namespace_lock
);
875 (void) kill(getpid(), SIGKILL
);
879 ztest_random(uint64_t range
)
883 ASSERT3S(ztest_fd_rand
, >=, 0);
888 if (read(ztest_fd_rand
, &r
, sizeof (r
)) != sizeof (r
))
889 fatal(1, "short read from /dev/urandom");
896 ztest_record_enospc(const char *s
)
898 ztest_shared
->zs_enospc_count
++;
902 ztest_get_ashift(void)
904 if (ztest_opts
.zo_ashift
== 0)
905 return (SPA_MINBLOCKSHIFT
+ ztest_random(5));
906 return (ztest_opts
.zo_ashift
);
910 make_vdev_file(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
)
916 pathbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
919 ashift
= ztest_get_ashift();
925 vdev
= ztest_shared
->zs_vdev_aux
;
926 (void) snprintf(path
, MAXPATHLEN
,
927 ztest_aux_template
, ztest_opts
.zo_dir
,
928 pool
== NULL
? ztest_opts
.zo_pool
: pool
,
931 vdev
= ztest_shared
->zs_vdev_next_leaf
++;
932 (void) snprintf(path
, MAXPATHLEN
,
933 ztest_dev_template
, ztest_opts
.zo_dir
,
934 pool
== NULL
? ztest_opts
.zo_pool
: pool
, vdev
);
939 int fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
941 fatal(1, "can't open %s", path
);
942 if (ftruncate(fd
, size
) != 0)
943 fatal(1, "can't ftruncate %s", path
);
947 VERIFY(nvlist_alloc(&file
, NV_UNIQUE_NAME
, 0) == 0);
948 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_FILE
) == 0);
949 VERIFY(nvlist_add_string(file
, ZPOOL_CONFIG_PATH
, path
) == 0);
950 VERIFY(nvlist_add_uint64(file
, ZPOOL_CONFIG_ASHIFT
, ashift
) == 0);
951 umem_free(pathbuf
, MAXPATHLEN
);
957 make_vdev_raidz(char *path
, char *aux
, char *pool
, size_t size
,
958 uint64_t ashift
, int r
)
960 nvlist_t
*raidz
, **child
;
964 return (make_vdev_file(path
, aux
, pool
, size
, ashift
));
965 child
= umem_alloc(r
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
967 for (c
= 0; c
< r
; c
++)
968 child
[c
] = make_vdev_file(path
, aux
, pool
, size
, ashift
);
970 VERIFY(nvlist_alloc(&raidz
, NV_UNIQUE_NAME
, 0) == 0);
971 VERIFY(nvlist_add_string(raidz
, ZPOOL_CONFIG_TYPE
,
972 VDEV_TYPE_RAIDZ
) == 0);
973 VERIFY(nvlist_add_uint64(raidz
, ZPOOL_CONFIG_NPARITY
,
974 ztest_opts
.zo_raidz_parity
) == 0);
975 VERIFY(nvlist_add_nvlist_array(raidz
, ZPOOL_CONFIG_CHILDREN
,
978 for (c
= 0; c
< r
; c
++)
979 nvlist_free(child
[c
]);
981 umem_free(child
, r
* sizeof (nvlist_t
*));
987 make_vdev_mirror(char *path
, char *aux
, char *pool
, size_t size
,
988 uint64_t ashift
, int r
, int m
)
990 nvlist_t
*mirror
, **child
;
994 return (make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
));
996 child
= umem_alloc(m
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
998 for (c
= 0; c
< m
; c
++)
999 child
[c
] = make_vdev_raidz(path
, aux
, pool
, size
, ashift
, r
);
1001 VERIFY(nvlist_alloc(&mirror
, NV_UNIQUE_NAME
, 0) == 0);
1002 VERIFY(nvlist_add_string(mirror
, ZPOOL_CONFIG_TYPE
,
1003 VDEV_TYPE_MIRROR
) == 0);
1004 VERIFY(nvlist_add_nvlist_array(mirror
, ZPOOL_CONFIG_CHILDREN
,
1007 for (c
= 0; c
< m
; c
++)
1008 nvlist_free(child
[c
]);
1010 umem_free(child
, m
* sizeof (nvlist_t
*));
1016 make_vdev_root(char *path
, char *aux
, char *pool
, size_t size
, uint64_t ashift
,
1017 int log
, int r
, int m
, int t
)
1019 nvlist_t
*root
, **child
;
1024 child
= umem_alloc(t
* sizeof (nvlist_t
*), UMEM_NOFAIL
);
1026 for (c
= 0; c
< t
; c
++) {
1027 child
[c
] = make_vdev_mirror(path
, aux
, pool
, size
, ashift
,
1029 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
1033 VERIFY(nvlist_alloc(&root
, NV_UNIQUE_NAME
, 0) == 0);
1034 VERIFY(nvlist_add_string(root
, ZPOOL_CONFIG_TYPE
, VDEV_TYPE_ROOT
) == 0);
1035 VERIFY(nvlist_add_nvlist_array(root
, aux
? aux
: ZPOOL_CONFIG_CHILDREN
,
1038 for (c
= 0; c
< t
; c
++)
1039 nvlist_free(child
[c
]);
1041 umem_free(child
, t
* sizeof (nvlist_t
*));
1047 * Find a random spa version. Returns back a random spa version in the
1048 * range [initial_version, SPA_VERSION_FEATURES].
1051 ztest_random_spa_version(uint64_t initial_version
)
1053 uint64_t version
= initial_version
;
1055 if (version
<= SPA_VERSION_BEFORE_FEATURES
) {
1057 ztest_random(SPA_VERSION_BEFORE_FEATURES
- version
+ 1);
1060 if (version
> SPA_VERSION_BEFORE_FEATURES
)
1061 version
= SPA_VERSION_FEATURES
;
1063 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
1068 ztest_random_blocksize(void)
1071 * Choose a block size >= the ashift.
1072 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1074 int maxbs
= SPA_OLD_MAXBLOCKSHIFT
;
1075 if (spa_maxblocksize(ztest_spa
) == SPA_MAXBLOCKSIZE
)
1077 uint64_t block_shift
=
1078 ztest_random(maxbs
- ztest_spa
->spa_max_ashift
+ 1);
1079 return (1 << (SPA_MINBLOCKSHIFT
+ block_shift
));
1083 ztest_random_dnodesize(void)
1086 int max_slots
= spa_maxdnodesize(ztest_spa
) >> DNODE_SHIFT
;
1088 if (max_slots
== DNODE_MIN_SLOTS
)
1089 return (DNODE_MIN_SIZE
);
1092 * Weight the random distribution more heavily toward smaller
1093 * dnode sizes since that is more likely to reflect real-world
1096 ASSERT3U(max_slots
, >, 4);
1097 switch (ztest_random(10)) {
1099 slots
= 5 + ztest_random(max_slots
- 4);
1102 slots
= 2 + ztest_random(3);
1109 return (slots
<< DNODE_SHIFT
);
1113 ztest_random_ibshift(void)
1115 return (DN_MIN_INDBLKSHIFT
+
1116 ztest_random(DN_MAX_INDBLKSHIFT
- DN_MIN_INDBLKSHIFT
+ 1));
1120 ztest_random_vdev_top(spa_t
*spa
, boolean_t log_ok
)
1123 vdev_t
*rvd
= spa
->spa_root_vdev
;
1126 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_READER
) != 0);
1129 top
= ztest_random(rvd
->vdev_children
);
1130 tvd
= rvd
->vdev_child
[top
];
1131 } while (tvd
->vdev_ishole
|| (tvd
->vdev_islog
&& !log_ok
) ||
1132 tvd
->vdev_mg
== NULL
|| tvd
->vdev_mg
->mg_class
== NULL
);
1138 ztest_random_dsl_prop(zfs_prop_t prop
)
1143 value
= zfs_prop_random_value(prop
, ztest_random(-1ULL));
1144 } while (prop
== ZFS_PROP_CHECKSUM
&& value
== ZIO_CHECKSUM_OFF
);
1150 ztest_dsl_prop_set_uint64(char *osname
, zfs_prop_t prop
, uint64_t value
,
1153 const char *propname
= zfs_prop_to_name(prop
);
1154 const char *valname
;
1159 error
= dsl_prop_set_int(osname
, propname
,
1160 (inherit
? ZPROP_SRC_NONE
: ZPROP_SRC_LOCAL
), value
);
1162 if (error
== ENOSPC
) {
1163 ztest_record_enospc(FTAG
);
1168 setpoint
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
1169 VERIFY0(dsl_prop_get_integer(osname
, propname
, &curval
, setpoint
));
1171 if (ztest_opts
.zo_verbose
>= 6) {
1174 err
= zfs_prop_index_to_string(prop
, curval
, &valname
);
1176 (void) printf("%s %s = %llu at '%s'\n", osname
,
1177 propname
, (unsigned long long)curval
, setpoint
);
1179 (void) printf("%s %s = %s at '%s'\n",
1180 osname
, propname
, valname
, setpoint
);
1182 umem_free(setpoint
, MAXPATHLEN
);
1188 ztest_spa_prop_set_uint64(zpool_prop_t prop
, uint64_t value
)
1190 spa_t
*spa
= ztest_spa
;
1191 nvlist_t
*props
= NULL
;
1194 VERIFY(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) == 0);
1195 VERIFY(nvlist_add_uint64(props
, zpool_prop_to_name(prop
), value
) == 0);
1197 error
= spa_prop_set(spa
, props
);
1201 if (error
== ENOSPC
) {
1202 ztest_record_enospc(FTAG
);
1211 ztest_dmu_objset_own(const char *name
, dmu_objset_type_t type
,
1212 boolean_t readonly
, boolean_t decrypt
, void *tag
, objset_t
**osp
)
1216 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1217 if (decrypt
&& err
== EACCES
) {
1218 char ddname
[ZFS_MAX_DATASET_NAME_LEN
];
1219 dsl_crypto_params_t
*dcp
;
1220 nvlist_t
*crypto_args
= fnvlist_alloc();
1223 /* spa_keystore_load_wkey() expects a dsl dir name */
1224 strcpy(ddname
, name
);
1225 cp
= strchr(ddname
, '@');
1229 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
1230 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
1231 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, NULL
,
1232 crypto_args
, &dcp
));
1233 err
= spa_keystore_load_wkey(ddname
, dcp
, B_FALSE
);
1234 dsl_crypto_params_free(dcp
, B_FALSE
);
1235 fnvlist_free(crypto_args
);
1240 err
= dmu_objset_own(name
, type
, readonly
, decrypt
, tag
, osp
);
1248 * Object and range lock mechanics
1251 list_node_t z_lnode
;
1252 refcount_t z_refcnt
;
1254 zfs_rlock_t z_range_lock
;
1259 ztest_znode_t
*z_ztznode
;
1262 static ztest_znode_t
*
1263 ztest_znode_init(uint64_t object
)
1265 ztest_znode_t
*zp
= umem_alloc(sizeof (*zp
), UMEM_NOFAIL
);
1267 list_link_init(&zp
->z_lnode
);
1268 refcount_create(&zp
->z_refcnt
);
1269 zp
->z_object
= object
;
1270 zfs_rlock_init(&zp
->z_range_lock
);
1276 ztest_znode_fini(ztest_znode_t
*zp
)
1278 ASSERT(refcount_is_zero(&zp
->z_refcnt
));
1279 zfs_rlock_destroy(&zp
->z_range_lock
);
1281 refcount_destroy(&zp
->z_refcnt
);
1282 list_link_init(&zp
->z_lnode
);
1283 umem_free(zp
, sizeof (*zp
));
1287 ztest_zll_init(zll_t
*zll
)
1289 mutex_init(&zll
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1290 list_create(&zll
->z_list
, sizeof (ztest_znode_t
),
1291 offsetof(ztest_znode_t
, z_lnode
));
1295 ztest_zll_destroy(zll_t
*zll
)
1297 list_destroy(&zll
->z_list
);
1298 mutex_destroy(&zll
->z_lock
);
1301 #define RL_TAG "range_lock"
1302 static ztest_znode_t
*
1303 ztest_znode_get(ztest_ds_t
*zd
, uint64_t object
)
1305 zll_t
*zll
= &zd
->zd_range_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1306 ztest_znode_t
*zp
= NULL
;
1307 mutex_enter(&zll
->z_lock
);
1308 for (zp
= list_head(&zll
->z_list
); (zp
);
1309 zp
= list_next(&zll
->z_list
, zp
)) {
1310 if (zp
->z_object
== object
) {
1311 refcount_add(&zp
->z_refcnt
, RL_TAG
);
1316 zp
= ztest_znode_init(object
);
1317 refcount_add(&zp
->z_refcnt
, RL_TAG
);
1318 list_insert_head(&zll
->z_list
, zp
);
1320 mutex_exit(&zll
->z_lock
);
1325 ztest_znode_put(ztest_ds_t
*zd
, ztest_znode_t
*zp
)
1328 ASSERT3U(zp
->z_object
, !=, 0);
1329 zll
= &zd
->zd_range_lock
[zp
->z_object
& (ZTEST_OBJECT_LOCKS
- 1)];
1330 mutex_enter(&zll
->z_lock
);
1331 refcount_remove(&zp
->z_refcnt
, RL_TAG
);
1332 if (refcount_is_zero(&zp
->z_refcnt
)) {
1333 list_remove(&zll
->z_list
, zp
);
1334 ztest_znode_fini(zp
);
1336 mutex_exit(&zll
->z_lock
);
1341 ztest_rll_init(rll_t
*rll
)
1343 rll
->rll_writer
= NULL
;
1344 rll
->rll_readers
= 0;
1345 mutex_init(&rll
->rll_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1346 cv_init(&rll
->rll_cv
, NULL
, CV_DEFAULT
, NULL
);
1350 ztest_rll_destroy(rll_t
*rll
)
1352 ASSERT(rll
->rll_writer
== NULL
);
1353 ASSERT(rll
->rll_readers
== 0);
1354 mutex_destroy(&rll
->rll_lock
);
1355 cv_destroy(&rll
->rll_cv
);
1359 ztest_rll_lock(rll_t
*rll
, rl_type_t type
)
1361 mutex_enter(&rll
->rll_lock
);
1363 if (type
== RL_READER
) {
1364 while (rll
->rll_writer
!= NULL
)
1365 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1368 while (rll
->rll_writer
!= NULL
|| rll
->rll_readers
)
1369 (void) cv_wait(&rll
->rll_cv
, &rll
->rll_lock
);
1370 rll
->rll_writer
= curthread
;
1373 mutex_exit(&rll
->rll_lock
);
1377 ztest_rll_unlock(rll_t
*rll
)
1379 mutex_enter(&rll
->rll_lock
);
1381 if (rll
->rll_writer
) {
1382 ASSERT(rll
->rll_readers
== 0);
1383 rll
->rll_writer
= NULL
;
1385 ASSERT(rll
->rll_readers
!= 0);
1386 ASSERT(rll
->rll_writer
== NULL
);
1390 if (rll
->rll_writer
== NULL
&& rll
->rll_readers
== 0)
1391 cv_broadcast(&rll
->rll_cv
);
1393 mutex_exit(&rll
->rll_lock
);
1397 ztest_object_lock(ztest_ds_t
*zd
, uint64_t object
, rl_type_t type
)
1399 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1401 ztest_rll_lock(rll
, type
);
1405 ztest_object_unlock(ztest_ds_t
*zd
, uint64_t object
)
1407 rll_t
*rll
= &zd
->zd_object_lock
[object
& (ZTEST_OBJECT_LOCKS
- 1)];
1409 ztest_rll_unlock(rll
);
1412 static ztest_zrl_t
*
1413 ztest_zrl_init(rl_t
*rl
, ztest_znode_t
*zp
)
1415 ztest_zrl_t
*zrl
= umem_alloc(sizeof (*zrl
), UMEM_NOFAIL
);
1417 zrl
->z_ztznode
= zp
;
1422 ztest_zrl_fini(ztest_zrl_t
*zrl
)
1424 umem_free(zrl
, sizeof (*zrl
));
1427 static ztest_zrl_t
*
1428 ztest_range_lock(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
,
1429 uint64_t size
, rl_type_t type
)
1431 ztest_znode_t
*zp
= ztest_znode_get(zd
, object
);
1432 rl_t
*rl
= zfs_range_lock(&zp
->z_range_lock
, offset
,
1434 return (ztest_zrl_init(rl
, zp
));
1438 ztest_range_unlock(ztest_ds_t
*zd
, ztest_zrl_t
*zrl
)
1440 zfs_range_unlock(zrl
->z_rl
);
1441 ztest_znode_put(zd
, zrl
->z_ztznode
);
1442 ztest_zrl_fini(zrl
);
1446 ztest_zd_init(ztest_ds_t
*zd
, ztest_shared_ds_t
*szd
, objset_t
*os
)
1449 zd
->zd_zilog
= dmu_objset_zil(os
);
1450 zd
->zd_shared
= szd
;
1451 dmu_objset_name(os
, zd
->zd_name
);
1454 if (zd
->zd_shared
!= NULL
)
1455 zd
->zd_shared
->zd_seq
= 0;
1457 VERIFY(rwlock_init(&zd
->zd_zilog_lock
, USYNC_THREAD
, NULL
) == 0);
1458 mutex_init(&zd
->zd_dirobj_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1460 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1461 ztest_rll_init(&zd
->zd_object_lock
[l
]);
1463 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1464 ztest_zll_init(&zd
->zd_range_lock
[l
]);
1468 ztest_zd_fini(ztest_ds_t
*zd
)
1472 mutex_destroy(&zd
->zd_dirobj_lock
);
1473 (void) rwlock_destroy(&zd
->zd_zilog_lock
);
1475 for (l
= 0; l
< ZTEST_OBJECT_LOCKS
; l
++)
1476 ztest_rll_destroy(&zd
->zd_object_lock
[l
]);
1478 for (l
= 0; l
< ZTEST_RANGE_LOCKS
; l
++)
1479 ztest_zll_destroy(&zd
->zd_range_lock
[l
]);
1482 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1485 ztest_tx_assign(dmu_tx_t
*tx
, uint64_t txg_how
, const char *tag
)
1491 * Attempt to assign tx to some transaction group.
1493 error
= dmu_tx_assign(tx
, txg_how
);
1495 if (error
== ERESTART
) {
1496 ASSERT(txg_how
== TXG_NOWAIT
);
1499 ASSERT3U(error
, ==, ENOSPC
);
1500 ztest_record_enospc(tag
);
1505 txg
= dmu_tx_get_txg(tx
);
1511 ztest_pattern_set(void *buf
, uint64_t size
, uint64_t value
)
1514 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1522 ztest_pattern_match(void *buf
, uint64_t size
, uint64_t value
)
1525 uint64_t *ip_end
= (uint64_t *)((uintptr_t)buf
+ (uintptr_t)size
);
1529 diff
|= (value
- *ip
++);
1536 ztest_bt_generate(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1537 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1540 bt
->bt_magic
= BT_MAGIC
;
1541 bt
->bt_objset
= dmu_objset_id(os
);
1542 bt
->bt_object
= object
;
1543 bt
->bt_dnodesize
= dnodesize
;
1544 bt
->bt_offset
= offset
;
1547 bt
->bt_crtxg
= crtxg
;
1551 ztest_bt_verify(ztest_block_tag_t
*bt
, objset_t
*os
, uint64_t object
,
1552 uint64_t dnodesize
, uint64_t offset
, uint64_t gen
, uint64_t txg
,
1555 ASSERT3U(bt
->bt_magic
, ==, BT_MAGIC
);
1556 ASSERT3U(bt
->bt_objset
, ==, dmu_objset_id(os
));
1557 ASSERT3U(bt
->bt_object
, ==, object
);
1558 ASSERT3U(bt
->bt_dnodesize
, ==, dnodesize
);
1559 ASSERT3U(bt
->bt_offset
, ==, offset
);
1560 ASSERT3U(bt
->bt_gen
, <=, gen
);
1561 ASSERT3U(bt
->bt_txg
, <=, txg
);
1562 ASSERT3U(bt
->bt_crtxg
, ==, crtxg
);
1565 static ztest_block_tag_t
*
1566 ztest_bt_bonus(dmu_buf_t
*db
)
1568 dmu_object_info_t doi
;
1569 ztest_block_tag_t
*bt
;
1571 dmu_object_info_from_db(db
, &doi
);
1572 ASSERT3U(doi
.doi_bonus_size
, <=, db
->db_size
);
1573 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (*bt
));
1574 bt
= (void *)((char *)db
->db_data
+ doi
.doi_bonus_size
- sizeof (*bt
));
1580 * Generate a token to fill up unused bonus buffer space. Try to make
1581 * it unique to the object, generation, and offset to verify that data
1582 * is not getting overwritten by data from other dnodes.
1584 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1585 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1588 * Fill up the unused bonus buffer region before the block tag with a
1589 * verifiable pattern. Filling the whole bonus area with non-zero data
1590 * helps ensure that all dnode traversal code properly skips the
1591 * interior regions of large dnodes.
1594 ztest_fill_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1595 objset_t
*os
, uint64_t gen
)
1599 ASSERT(IS_P2ALIGNED((char *)end
- (char *)db
->db_data
, 8));
1601 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1602 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1603 gen
, bonusp
- (uint64_t *)db
->db_data
);
1609 * Verify that the unused area of a bonus buffer is filled with the
1613 ztest_verify_unused_bonus(dmu_buf_t
*db
, void *end
, uint64_t obj
,
1614 objset_t
*os
, uint64_t gen
)
1618 for (bonusp
= db
->db_data
; bonusp
< (uint64_t *)end
; bonusp
++) {
1619 uint64_t token
= ZTEST_BONUS_FILL_TOKEN(obj
, dmu_objset_id(os
),
1620 gen
, bonusp
- (uint64_t *)db
->db_data
);
1621 VERIFY3U(*bonusp
, ==, token
);
1629 #define lrz_type lr_mode
1630 #define lrz_blocksize lr_uid
1631 #define lrz_ibshift lr_gid
1632 #define lrz_bonustype lr_rdev
1633 #define lrz_dnodesize lr_crtime[1]
1636 ztest_log_create(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_create_t
*lr
)
1638 char *name
= (void *)(lr
+ 1); /* name follows lr */
1639 size_t namesize
= strlen(name
) + 1;
1642 if (zil_replaying(zd
->zd_zilog
, tx
))
1645 itx
= zil_itx_create(TX_CREATE
, sizeof (*lr
) + namesize
);
1646 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1647 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1649 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1653 ztest_log_remove(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_remove_t
*lr
, uint64_t object
)
1655 char *name
= (void *)(lr
+ 1); /* name follows lr */
1656 size_t namesize
= strlen(name
) + 1;
1659 if (zil_replaying(zd
->zd_zilog
, tx
))
1662 itx
= zil_itx_create(TX_REMOVE
, sizeof (*lr
) + namesize
);
1663 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1664 sizeof (*lr
) + namesize
- sizeof (lr_t
));
1666 itx
->itx_oid
= object
;
1667 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1671 ztest_log_write(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_write_t
*lr
)
1674 itx_wr_state_t write_state
= ztest_random(WR_NUM_STATES
);
1676 if (zil_replaying(zd
->zd_zilog
, tx
))
1679 if (lr
->lr_length
> ZIL_MAX_LOG_DATA
)
1680 write_state
= WR_INDIRECT
;
1682 itx
= zil_itx_create(TX_WRITE
,
1683 sizeof (*lr
) + (write_state
== WR_COPIED
? lr
->lr_length
: 0));
1685 if (write_state
== WR_COPIED
&&
1686 dmu_read(zd
->zd_os
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
1687 ((lr_write_t
*)&itx
->itx_lr
) + 1, DMU_READ_NO_PREFETCH
) != 0) {
1688 zil_itx_destroy(itx
);
1689 itx
= zil_itx_create(TX_WRITE
, sizeof (*lr
));
1690 write_state
= WR_NEED_COPY
;
1692 itx
->itx_private
= zd
;
1693 itx
->itx_wr_state
= write_state
;
1694 itx
->itx_sync
= (ztest_random(8) == 0);
1696 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1697 sizeof (*lr
) - sizeof (lr_t
));
1699 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1703 ztest_log_truncate(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_truncate_t
*lr
)
1707 if (zil_replaying(zd
->zd_zilog
, tx
))
1710 itx
= zil_itx_create(TX_TRUNCATE
, sizeof (*lr
));
1711 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1712 sizeof (*lr
) - sizeof (lr_t
));
1714 itx
->itx_sync
= B_FALSE
;
1715 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1719 ztest_log_setattr(ztest_ds_t
*zd
, dmu_tx_t
*tx
, lr_setattr_t
*lr
)
1723 if (zil_replaying(zd
->zd_zilog
, tx
))
1726 itx
= zil_itx_create(TX_SETATTR
, sizeof (*lr
));
1727 bcopy(&lr
->lr_common
+ 1, &itx
->itx_lr
+ 1,
1728 sizeof (*lr
) - sizeof (lr_t
));
1730 itx
->itx_sync
= B_FALSE
;
1731 zil_itx_assign(zd
->zd_zilog
, itx
, tx
);
1738 ztest_replay_create(void *arg1
, void *arg2
, boolean_t byteswap
)
1740 ztest_ds_t
*zd
= arg1
;
1741 lr_create_t
*lr
= arg2
;
1742 char *name
= (void *)(lr
+ 1); /* name follows lr */
1743 objset_t
*os
= zd
->zd_os
;
1744 ztest_block_tag_t
*bbt
;
1752 byteswap_uint64_array(lr
, sizeof (*lr
));
1754 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1755 ASSERT(name
[0] != '\0');
1757 tx
= dmu_tx_create(os
);
1759 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_TRUE
, name
);
1761 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1762 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, B_TRUE
, NULL
);
1764 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
1767 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1771 ASSERT(dmu_objset_zil(os
)->zl_replay
== !!lr
->lr_foid
);
1772 bonuslen
= DN_BONUS_SIZE(lr
->lrz_dnodesize
);
1774 if (lr
->lrz_type
== DMU_OT_ZAP_OTHER
) {
1775 if (lr
->lr_foid
== 0) {
1776 lr
->lr_foid
= zap_create_dnsize(os
,
1777 lr
->lrz_type
, lr
->lrz_bonustype
,
1778 bonuslen
, lr
->lrz_dnodesize
, tx
);
1780 error
= zap_create_claim_dnsize(os
, lr
->lr_foid
,
1781 lr
->lrz_type
, lr
->lrz_bonustype
,
1782 bonuslen
, lr
->lrz_dnodesize
, tx
);
1785 if (lr
->lr_foid
== 0) {
1786 lr
->lr_foid
= dmu_object_alloc_dnsize(os
,
1787 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1788 bonuslen
, lr
->lrz_dnodesize
, tx
);
1790 error
= dmu_object_claim_dnsize(os
, lr
->lr_foid
,
1791 lr
->lrz_type
, 0, lr
->lrz_bonustype
,
1792 bonuslen
, lr
->lrz_dnodesize
, tx
);
1797 ASSERT3U(error
, ==, EEXIST
);
1798 ASSERT(zd
->zd_zilog
->zl_replay
);
1803 ASSERT(lr
->lr_foid
!= 0);
1805 if (lr
->lrz_type
!= DMU_OT_ZAP_OTHER
)
1806 VERIFY3U(0, ==, dmu_object_set_blocksize(os
, lr
->lr_foid
,
1807 lr
->lrz_blocksize
, lr
->lrz_ibshift
, tx
));
1809 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1810 bbt
= ztest_bt_bonus(db
);
1811 dmu_buf_will_dirty(db
, tx
);
1812 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, lr
->lrz_dnodesize
, -1ULL,
1813 lr
->lr_gen
, txg
, txg
);
1814 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, lr
->lr_gen
);
1815 dmu_buf_rele(db
, FTAG
);
1817 VERIFY3U(0, ==, zap_add(os
, lr
->lr_doid
, name
, sizeof (uint64_t), 1,
1820 (void) ztest_log_create(zd
, tx
, lr
);
1828 ztest_replay_remove(void *arg1
, void *arg2
, boolean_t byteswap
)
1830 ztest_ds_t
*zd
= arg1
;
1831 lr_remove_t
*lr
= arg2
;
1832 char *name
= (void *)(lr
+ 1); /* name follows lr */
1833 objset_t
*os
= zd
->zd_os
;
1834 dmu_object_info_t doi
;
1836 uint64_t object
, txg
;
1839 byteswap_uint64_array(lr
, sizeof (*lr
));
1841 ASSERT(lr
->lr_doid
== ZTEST_DIROBJ
);
1842 ASSERT(name
[0] != '\0');
1845 zap_lookup(os
, lr
->lr_doid
, name
, sizeof (object
), 1, &object
));
1846 ASSERT(object
!= 0);
1848 ztest_object_lock(zd
, object
, RL_WRITER
);
1850 VERIFY3U(0, ==, dmu_object_info(os
, object
, &doi
));
1852 tx
= dmu_tx_create(os
);
1854 dmu_tx_hold_zap(tx
, lr
->lr_doid
, B_FALSE
, name
);
1855 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
1857 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1859 ztest_object_unlock(zd
, object
);
1863 if (doi
.doi_type
== DMU_OT_ZAP_OTHER
) {
1864 VERIFY3U(0, ==, zap_destroy(os
, object
, tx
));
1866 VERIFY3U(0, ==, dmu_object_free(os
, object
, tx
));
1869 VERIFY3U(0, ==, zap_remove(os
, lr
->lr_doid
, name
, tx
));
1871 (void) ztest_log_remove(zd
, tx
, lr
, object
);
1875 ztest_object_unlock(zd
, object
);
1881 ztest_replay_write(void *arg1
, void *arg2
, boolean_t byteswap
)
1883 ztest_ds_t
*zd
= arg1
;
1884 lr_write_t
*lr
= arg2
;
1885 objset_t
*os
= zd
->zd_os
;
1886 void *data
= lr
+ 1; /* data follows lr */
1887 uint64_t offset
, length
;
1888 ztest_block_tag_t
*bt
= data
;
1889 ztest_block_tag_t
*bbt
;
1890 uint64_t gen
, txg
, lrtxg
, crtxg
;
1891 dmu_object_info_t doi
;
1894 arc_buf_t
*abuf
= NULL
;
1898 byteswap_uint64_array(lr
, sizeof (*lr
));
1900 offset
= lr
->lr_offset
;
1901 length
= lr
->lr_length
;
1903 /* If it's a dmu_sync() block, write the whole block */
1904 if (lr
->lr_common
.lrc_reclen
== sizeof (lr_write_t
)) {
1905 uint64_t blocksize
= BP_GET_LSIZE(&lr
->lr_blkptr
);
1906 if (length
< blocksize
) {
1907 offset
-= offset
% blocksize
;
1912 if (bt
->bt_magic
== BSWAP_64(BT_MAGIC
))
1913 byteswap_uint64_array(bt
, sizeof (*bt
));
1915 if (bt
->bt_magic
!= BT_MAGIC
)
1918 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
1919 rl
= ztest_range_lock(zd
, lr
->lr_foid
, offset
, length
, RL_WRITER
);
1921 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
1923 dmu_object_info_from_db(db
, &doi
);
1925 bbt
= ztest_bt_bonus(db
);
1926 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
1928 crtxg
= bbt
->bt_crtxg
;
1929 lrtxg
= lr
->lr_common
.lrc_txg
;
1931 tx
= dmu_tx_create(os
);
1933 dmu_tx_hold_write(tx
, lr
->lr_foid
, offset
, length
);
1935 if (ztest_random(8) == 0 && length
== doi
.doi_data_block_size
&&
1936 P2PHASE(offset
, length
) == 0)
1937 abuf
= dmu_request_arcbuf(db
, length
);
1939 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
1942 dmu_return_arcbuf(abuf
);
1943 dmu_buf_rele(db
, FTAG
);
1944 ztest_range_unlock(zd
, rl
);
1945 ztest_object_unlock(zd
, lr
->lr_foid
);
1951 * Usually, verify the old data before writing new data --
1952 * but not always, because we also want to verify correct
1953 * behavior when the data was not recently read into cache.
1955 ASSERT(offset
% doi
.doi_data_block_size
== 0);
1956 if (ztest_random(4) != 0) {
1957 int prefetch
= ztest_random(2) ?
1958 DMU_READ_PREFETCH
: DMU_READ_NO_PREFETCH
;
1959 ztest_block_tag_t rbt
;
1961 VERIFY(dmu_read(os
, lr
->lr_foid
, offset
,
1962 sizeof (rbt
), &rbt
, prefetch
) == 0);
1963 if (rbt
.bt_magic
== BT_MAGIC
) {
1964 ztest_bt_verify(&rbt
, os
, lr
->lr_foid
, 0,
1965 offset
, gen
, txg
, crtxg
);
1970 * Writes can appear to be newer than the bonus buffer because
1971 * the ztest_get_data() callback does a dmu_read() of the
1972 * open-context data, which may be different than the data
1973 * as it was when the write was generated.
1975 if (zd
->zd_zilog
->zl_replay
) {
1976 ztest_bt_verify(bt
, os
, lr
->lr_foid
, 0, offset
,
1977 MAX(gen
, bt
->bt_gen
), MAX(txg
, lrtxg
),
1982 * Set the bt's gen/txg to the bonus buffer's gen/txg
1983 * so that all of the usual ASSERTs will work.
1985 ztest_bt_generate(bt
, os
, lr
->lr_foid
, 0, offset
, gen
, txg
,
1990 dmu_write(os
, lr
->lr_foid
, offset
, length
, data
, tx
);
1992 bcopy(data
, abuf
->b_data
, length
);
1993 dmu_assign_arcbuf_by_dbuf(db
, offset
, abuf
, tx
);
1996 (void) ztest_log_write(zd
, tx
, lr
);
1998 dmu_buf_rele(db
, FTAG
);
2002 ztest_range_unlock(zd
, rl
);
2003 ztest_object_unlock(zd
, lr
->lr_foid
);
2009 ztest_replay_truncate(void *arg1
, void *arg2
, boolean_t byteswap
)
2011 ztest_ds_t
*zd
= arg1
;
2012 lr_truncate_t
*lr
= arg2
;
2013 objset_t
*os
= zd
->zd_os
;
2019 byteswap_uint64_array(lr
, sizeof (*lr
));
2021 ztest_object_lock(zd
, lr
->lr_foid
, RL_READER
);
2022 rl
= ztest_range_lock(zd
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
,
2025 tx
= dmu_tx_create(os
);
2027 dmu_tx_hold_free(tx
, lr
->lr_foid
, lr
->lr_offset
, lr
->lr_length
);
2029 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2031 ztest_range_unlock(zd
, rl
);
2032 ztest_object_unlock(zd
, lr
->lr_foid
);
2036 VERIFY(dmu_free_range(os
, lr
->lr_foid
, lr
->lr_offset
,
2037 lr
->lr_length
, tx
) == 0);
2039 (void) ztest_log_truncate(zd
, tx
, lr
);
2043 ztest_range_unlock(zd
, rl
);
2044 ztest_object_unlock(zd
, lr
->lr_foid
);
2050 ztest_replay_setattr(void *arg1
, void *arg2
, boolean_t byteswap
)
2052 ztest_ds_t
*zd
= arg1
;
2053 lr_setattr_t
*lr
= arg2
;
2054 objset_t
*os
= zd
->zd_os
;
2057 ztest_block_tag_t
*bbt
;
2058 uint64_t txg
, lrtxg
, crtxg
, dnodesize
;
2061 byteswap_uint64_array(lr
, sizeof (*lr
));
2063 ztest_object_lock(zd
, lr
->lr_foid
, RL_WRITER
);
2065 VERIFY3U(0, ==, dmu_bonus_hold(os
, lr
->lr_foid
, FTAG
, &db
));
2067 tx
= dmu_tx_create(os
);
2068 dmu_tx_hold_bonus(tx
, lr
->lr_foid
);
2070 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2072 dmu_buf_rele(db
, FTAG
);
2073 ztest_object_unlock(zd
, lr
->lr_foid
);
2077 bbt
= ztest_bt_bonus(db
);
2078 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2079 crtxg
= bbt
->bt_crtxg
;
2080 lrtxg
= lr
->lr_common
.lrc_txg
;
2081 dnodesize
= bbt
->bt_dnodesize
;
2083 if (zd
->zd_zilog
->zl_replay
) {
2084 ASSERT(lr
->lr_size
!= 0);
2085 ASSERT(lr
->lr_mode
!= 0);
2089 * Randomly change the size and increment the generation.
2091 lr
->lr_size
= (ztest_random(db
->db_size
/ sizeof (*bbt
)) + 1) *
2093 lr
->lr_mode
= bbt
->bt_gen
+ 1;
2098 * Verify that the current bonus buffer is not newer than our txg.
2100 ztest_bt_verify(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2101 MAX(txg
, lrtxg
), crtxg
);
2103 dmu_buf_will_dirty(db
, tx
);
2105 ASSERT3U(lr
->lr_size
, >=, sizeof (*bbt
));
2106 ASSERT3U(lr
->lr_size
, <=, db
->db_size
);
2107 VERIFY0(dmu_set_bonus(db
, lr
->lr_size
, tx
));
2108 bbt
= ztest_bt_bonus(db
);
2110 ztest_bt_generate(bbt
, os
, lr
->lr_foid
, dnodesize
, -1ULL, lr
->lr_mode
,
2112 ztest_fill_unused_bonus(db
, bbt
, lr
->lr_foid
, os
, bbt
->bt_gen
);
2113 dmu_buf_rele(db
, FTAG
);
2115 (void) ztest_log_setattr(zd
, tx
, lr
);
2119 ztest_object_unlock(zd
, lr
->lr_foid
);
2124 zil_replay_func_t
*ztest_replay_vector
[TX_MAX_TYPE
] = {
2125 NULL
, /* 0 no such transaction type */
2126 ztest_replay_create
, /* TX_CREATE */
2127 NULL
, /* TX_MKDIR */
2128 NULL
, /* TX_MKXATTR */
2129 NULL
, /* TX_SYMLINK */
2130 ztest_replay_remove
, /* TX_REMOVE */
2131 NULL
, /* TX_RMDIR */
2133 NULL
, /* TX_RENAME */
2134 ztest_replay_write
, /* TX_WRITE */
2135 ztest_replay_truncate
, /* TX_TRUNCATE */
2136 ztest_replay_setattr
, /* TX_SETATTR */
2138 NULL
, /* TX_CREATE_ACL */
2139 NULL
, /* TX_CREATE_ATTR */
2140 NULL
, /* TX_CREATE_ACL_ATTR */
2141 NULL
, /* TX_MKDIR_ACL */
2142 NULL
, /* TX_MKDIR_ATTR */
2143 NULL
, /* TX_MKDIR_ACL_ATTR */
2144 NULL
, /* TX_WRITE2 */
2148 * ZIL get_data callbacks
2150 typedef struct ztest_zgd_private
{
2154 } ztest_zgd_private_t
;
2157 ztest_get_done(zgd_t
*zgd
, int error
)
2159 ztest_zgd_private_t
*zzp
= zgd
->zgd_private
;
2160 ztest_ds_t
*zd
= zzp
->z_zd
;
2161 uint64_t object
= zzp
->z_object
;
2164 dmu_buf_rele(zgd
->zgd_db
, zgd
);
2166 ztest_range_unlock(zd
, zzp
->z_rl
);
2167 ztest_object_unlock(zd
, object
);
2169 if (error
== 0 && zgd
->zgd_bp
)
2170 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
2172 umem_free(zgd
, sizeof (*zgd
));
2173 umem_free(zzp
, sizeof (*zzp
));
2177 ztest_get_data(void *arg
, lr_write_t
*lr
, char *buf
, struct lwb
*lwb
,
2180 ztest_ds_t
*zd
= arg
;
2181 objset_t
*os
= zd
->zd_os
;
2182 uint64_t object
= lr
->lr_foid
;
2183 uint64_t offset
= lr
->lr_offset
;
2184 uint64_t size
= lr
->lr_length
;
2185 uint64_t txg
= lr
->lr_common
.lrc_txg
;
2187 dmu_object_info_t doi
;
2191 ztest_zgd_private_t
*zgd_private
;
2193 ASSERT3P(lwb
, !=, NULL
);
2194 ASSERT3P(zio
, !=, NULL
);
2195 ASSERT3U(size
, !=, 0);
2197 ztest_object_lock(zd
, object
, RL_READER
);
2198 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
2200 ztest_object_unlock(zd
, object
);
2204 crtxg
= ztest_bt_bonus(db
)->bt_crtxg
;
2206 if (crtxg
== 0 || crtxg
> txg
) {
2207 dmu_buf_rele(db
, FTAG
);
2208 ztest_object_unlock(zd
, object
);
2212 dmu_object_info_from_db(db
, &doi
);
2213 dmu_buf_rele(db
, FTAG
);
2216 zgd
= umem_zalloc(sizeof (*zgd
), UMEM_NOFAIL
);
2218 zgd_private
= umem_zalloc(sizeof (ztest_zgd_private_t
), UMEM_NOFAIL
);
2219 zgd_private
->z_zd
= zd
;
2220 zgd_private
->z_object
= object
;
2221 zgd
->zgd_private
= zgd_private
;
2223 if (buf
!= NULL
) { /* immediate write */
2224 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2226 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2228 error
= dmu_read(os
, object
, offset
, size
, buf
,
2229 DMU_READ_NO_PREFETCH
);
2232 size
= doi
.doi_data_block_size
;
2234 offset
= P2ALIGN(offset
, size
);
2236 ASSERT(offset
< size
);
2240 zgd_private
->z_rl
= ztest_range_lock(zd
, object
, offset
, size
,
2242 zgd
->zgd_rl
= zgd_private
->z_rl
->z_rl
;
2244 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
2245 DMU_READ_NO_PREFETCH
);
2248 blkptr_t
*bp
= &lr
->lr_blkptr
;
2253 ASSERT(db
->db_offset
== offset
);
2254 ASSERT(db
->db_size
== size
);
2256 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
2257 ztest_get_done
, zgd
);
2264 ztest_get_done(zgd
, error
);
2270 ztest_lr_alloc(size_t lrsize
, char *name
)
2273 size_t namesize
= name
? strlen(name
) + 1 : 0;
2275 lr
= umem_zalloc(lrsize
+ namesize
, UMEM_NOFAIL
);
2278 bcopy(name
, lr
+ lrsize
, namesize
);
2284 ztest_lr_free(void *lr
, size_t lrsize
, char *name
)
2286 size_t namesize
= name
? strlen(name
) + 1 : 0;
2288 umem_free(lr
, lrsize
+ namesize
);
2292 * Lookup a bunch of objects. Returns the number of objects not found.
2295 ztest_lookup(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2301 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2303 for (i
= 0; i
< count
; i
++, od
++) {
2305 error
= zap_lookup(zd
->zd_os
, od
->od_dir
, od
->od_name
,
2306 sizeof (uint64_t), 1, &od
->od_object
);
2308 ASSERT(error
== ENOENT
);
2309 ASSERT(od
->od_object
== 0);
2313 ztest_block_tag_t
*bbt
;
2314 dmu_object_info_t doi
;
2316 ASSERT(od
->od_object
!= 0);
2317 ASSERT(missing
== 0); /* there should be no gaps */
2319 ztest_object_lock(zd
, od
->od_object
, RL_READER
);
2320 VERIFY3U(0, ==, dmu_bonus_hold(zd
->zd_os
,
2321 od
->od_object
, FTAG
, &db
));
2322 dmu_object_info_from_db(db
, &doi
);
2323 bbt
= ztest_bt_bonus(db
);
2324 ASSERT3U(bbt
->bt_magic
, ==, BT_MAGIC
);
2325 od
->od_type
= doi
.doi_type
;
2326 od
->od_blocksize
= doi
.doi_data_block_size
;
2327 od
->od_gen
= bbt
->bt_gen
;
2328 dmu_buf_rele(db
, FTAG
);
2329 ztest_object_unlock(zd
, od
->od_object
);
2337 ztest_create(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2342 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2344 for (i
= 0; i
< count
; i
++, od
++) {
2351 lr_create_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2353 lr
->lr_doid
= od
->od_dir
;
2354 lr
->lr_foid
= 0; /* 0 to allocate, > 0 to claim */
2355 lr
->lrz_type
= od
->od_crtype
;
2356 lr
->lrz_blocksize
= od
->od_crblocksize
;
2357 lr
->lrz_ibshift
= ztest_random_ibshift();
2358 lr
->lrz_bonustype
= DMU_OT_UINT64_OTHER
;
2359 lr
->lrz_dnodesize
= od
->od_crdnodesize
;
2360 lr
->lr_gen
= od
->od_crgen
;
2361 lr
->lr_crtime
[0] = time(NULL
);
2363 if (ztest_replay_create(zd
, lr
, B_FALSE
) != 0) {
2364 ASSERT(missing
== 0);
2368 od
->od_object
= lr
->lr_foid
;
2369 od
->od_type
= od
->od_crtype
;
2370 od
->od_blocksize
= od
->od_crblocksize
;
2371 od
->od_gen
= od
->od_crgen
;
2372 ASSERT(od
->od_object
!= 0);
2375 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2382 ztest_remove(ztest_ds_t
*zd
, ztest_od_t
*od
, int count
)
2388 ASSERT(MUTEX_HELD(&zd
->zd_dirobj_lock
));
2392 for (i
= count
- 1; i
>= 0; i
--, od
--) {
2399 * No object was found.
2401 if (od
->od_object
== 0)
2404 lr_remove_t
*lr
= ztest_lr_alloc(sizeof (*lr
), od
->od_name
);
2406 lr
->lr_doid
= od
->od_dir
;
2408 if ((error
= ztest_replay_remove(zd
, lr
, B_FALSE
)) != 0) {
2409 ASSERT3U(error
, ==, ENOSPC
);
2414 ztest_lr_free(lr
, sizeof (*lr
), od
->od_name
);
2421 ztest_write(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
,
2427 lr
= ztest_lr_alloc(sizeof (*lr
) + size
, NULL
);
2429 lr
->lr_foid
= object
;
2430 lr
->lr_offset
= offset
;
2431 lr
->lr_length
= size
;
2433 BP_ZERO(&lr
->lr_blkptr
);
2435 bcopy(data
, lr
+ 1, size
);
2437 error
= ztest_replay_write(zd
, lr
, B_FALSE
);
2439 ztest_lr_free(lr
, sizeof (*lr
) + size
, NULL
);
2445 ztest_truncate(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2450 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2452 lr
->lr_foid
= object
;
2453 lr
->lr_offset
= offset
;
2454 lr
->lr_length
= size
;
2456 error
= ztest_replay_truncate(zd
, lr
, B_FALSE
);
2458 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2464 ztest_setattr(ztest_ds_t
*zd
, uint64_t object
)
2469 lr
= ztest_lr_alloc(sizeof (*lr
), NULL
);
2471 lr
->lr_foid
= object
;
2475 error
= ztest_replay_setattr(zd
, lr
, B_FALSE
);
2477 ztest_lr_free(lr
, sizeof (*lr
), NULL
);
2483 ztest_prealloc(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
, uint64_t size
)
2485 objset_t
*os
= zd
->zd_os
;
2490 txg_wait_synced(dmu_objset_pool(os
), 0);
2492 ztest_object_lock(zd
, object
, RL_READER
);
2493 rl
= ztest_range_lock(zd
, object
, offset
, size
, RL_WRITER
);
2495 tx
= dmu_tx_create(os
);
2497 dmu_tx_hold_write(tx
, object
, offset
, size
);
2499 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
2502 dmu_prealloc(os
, object
, offset
, size
, tx
);
2504 txg_wait_synced(dmu_objset_pool(os
), txg
);
2506 (void) dmu_free_long_range(os
, object
, offset
, size
);
2509 ztest_range_unlock(zd
, rl
);
2510 ztest_object_unlock(zd
, object
);
2514 ztest_io(ztest_ds_t
*zd
, uint64_t object
, uint64_t offset
)
2517 ztest_block_tag_t wbt
;
2518 dmu_object_info_t doi
;
2519 enum ztest_io_type io_type
;
2523 VERIFY(dmu_object_info(zd
->zd_os
, object
, &doi
) == 0);
2524 blocksize
= doi
.doi_data_block_size
;
2525 data
= umem_alloc(blocksize
, UMEM_NOFAIL
);
2528 * Pick an i/o type at random, biased toward writing block tags.
2530 io_type
= ztest_random(ZTEST_IO_TYPES
);
2531 if (ztest_random(2) == 0)
2532 io_type
= ZTEST_IO_WRITE_TAG
;
2534 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2538 case ZTEST_IO_WRITE_TAG
:
2539 ztest_bt_generate(&wbt
, zd
->zd_os
, object
, doi
.doi_dnodesize
,
2541 (void) ztest_write(zd
, object
, offset
, sizeof (wbt
), &wbt
);
2544 case ZTEST_IO_WRITE_PATTERN
:
2545 (void) memset(data
, 'a' + (object
+ offset
) % 5, blocksize
);
2546 if (ztest_random(2) == 0) {
2548 * Induce fletcher2 collisions to ensure that
2549 * zio_ddt_collision() detects and resolves them
2550 * when using fletcher2-verify for deduplication.
2552 ((uint64_t *)data
)[0] ^= 1ULL << 63;
2553 ((uint64_t *)data
)[4] ^= 1ULL << 63;
2555 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2558 case ZTEST_IO_WRITE_ZEROES
:
2559 bzero(data
, blocksize
);
2560 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2563 case ZTEST_IO_TRUNCATE
:
2564 (void) ztest_truncate(zd
, object
, offset
, blocksize
);
2567 case ZTEST_IO_SETATTR
:
2568 (void) ztest_setattr(zd
, object
);
2573 case ZTEST_IO_REWRITE
:
2574 (void) rw_rdlock(&ztest_name_lock
);
2575 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2576 ZFS_PROP_CHECKSUM
, spa_dedup_checksum(ztest_spa
),
2578 VERIFY(err
== 0 || err
== ENOSPC
);
2579 err
= ztest_dsl_prop_set_uint64(zd
->zd_name
,
2580 ZFS_PROP_COMPRESSION
,
2581 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
),
2583 VERIFY(err
== 0 || err
== ENOSPC
);
2584 (void) rw_unlock(&ztest_name_lock
);
2586 VERIFY0(dmu_read(zd
->zd_os
, object
, offset
, blocksize
, data
,
2587 DMU_READ_NO_PREFETCH
));
2589 (void) ztest_write(zd
, object
, offset
, blocksize
, data
);
2593 (void) rw_unlock(&zd
->zd_zilog_lock
);
2595 umem_free(data
, blocksize
);
2599 * Initialize an object description template.
2602 ztest_od_init(ztest_od_t
*od
, uint64_t id
, char *tag
, uint64_t index
,
2603 dmu_object_type_t type
, uint64_t blocksize
, uint64_t dnodesize
,
2606 od
->od_dir
= ZTEST_DIROBJ
;
2609 od
->od_crtype
= type
;
2610 od
->od_crblocksize
= blocksize
? blocksize
: ztest_random_blocksize();
2611 od
->od_crdnodesize
= dnodesize
? dnodesize
: ztest_random_dnodesize();
2614 od
->od_type
= DMU_OT_NONE
;
2615 od
->od_blocksize
= 0;
2618 (void) snprintf(od
->od_name
, sizeof (od
->od_name
), "%s(%lld)[%llu]",
2619 tag
, (longlong_t
)id
, (u_longlong_t
)index
);
2623 * Lookup or create the objects for a test using the od template.
2624 * If the objects do not all exist, or if 'remove' is specified,
2625 * remove any existing objects and create new ones. Otherwise,
2626 * use the existing objects.
2629 ztest_object_init(ztest_ds_t
*zd
, ztest_od_t
*od
, size_t size
, boolean_t remove
)
2631 int count
= size
/ sizeof (*od
);
2634 mutex_enter(&zd
->zd_dirobj_lock
);
2635 if ((ztest_lookup(zd
, od
, count
) != 0 || remove
) &&
2636 (ztest_remove(zd
, od
, count
) != 0 ||
2637 ztest_create(zd
, od
, count
) != 0))
2640 mutex_exit(&zd
->zd_dirobj_lock
);
2647 ztest_zil_commit(ztest_ds_t
*zd
, uint64_t id
)
2649 zilog_t
*zilog
= zd
->zd_zilog
;
2651 (void) rw_rdlock(&zd
->zd_zilog_lock
);
2653 zil_commit(zilog
, ztest_random(ZTEST_OBJECTS
));
2656 * Remember the committed values in zd, which is in parent/child
2657 * shared memory. If we die, the next iteration of ztest_run()
2658 * will verify that the log really does contain this record.
2660 mutex_enter(&zilog
->zl_lock
);
2661 ASSERT(zd
->zd_shared
!= NULL
);
2662 ASSERT3U(zd
->zd_shared
->zd_seq
, <=, zilog
->zl_commit_lr_seq
);
2663 zd
->zd_shared
->zd_seq
= zilog
->zl_commit_lr_seq
;
2664 mutex_exit(&zilog
->zl_lock
);
2666 (void) rw_unlock(&zd
->zd_zilog_lock
);
2670 * This function is designed to simulate the operations that occur during a
2671 * mount/unmount operation. We hold the dataset across these operations in an
2672 * attempt to expose any implicit assumptions about ZIL management.
2676 ztest_zil_remount(ztest_ds_t
*zd
, uint64_t id
)
2678 objset_t
*os
= zd
->zd_os
;
2681 * We grab the zd_dirobj_lock to ensure that no other thread is
2682 * updating the zil (i.e. adding in-memory log records) and the
2683 * zd_zilog_lock to block any I/O.
2685 mutex_enter(&zd
->zd_dirobj_lock
);
2686 (void) rw_wrlock(&zd
->zd_zilog_lock
);
2688 /* zfsvfs_teardown() */
2689 zil_close(zd
->zd_zilog
);
2691 /* zfsvfs_setup() */
2692 VERIFY(zil_open(os
, ztest_get_data
) == zd
->zd_zilog
);
2693 zil_replay(os
, zd
, ztest_replay_vector
);
2695 (void) rw_unlock(&zd
->zd_zilog_lock
);
2696 mutex_exit(&zd
->zd_dirobj_lock
);
2700 * Verify that we can't destroy an active pool, create an existing pool,
2701 * or create a pool with a bad vdev spec.
2705 ztest_spa_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
2707 ztest_shared_opts_t
*zo
= &ztest_opts
;
2711 if (zo
->zo_mmp_test
)
2715 * Attempt to create using a bad file.
2717 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2718 VERIFY3U(ENOENT
, ==,
2719 spa_create("ztest_bad_file", nvroot
, NULL
, NULL
, NULL
));
2720 nvlist_free(nvroot
);
2723 * Attempt to create using a bad mirror.
2725 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 2, 1);
2726 VERIFY3U(ENOENT
, ==,
2727 spa_create("ztest_bad_mirror", nvroot
, NULL
, NULL
, NULL
));
2728 nvlist_free(nvroot
);
2731 * Attempt to create an existing pool. It shouldn't matter
2732 * what's in the nvroot; we should fail with EEXIST.
2734 (void) rw_rdlock(&ztest_name_lock
);
2735 nvroot
= make_vdev_root("/dev/bogus", NULL
, NULL
, 0, 0, 0, 0, 0, 1);
2736 VERIFY3U(EEXIST
, ==,
2737 spa_create(zo
->zo_pool
, nvroot
, NULL
, NULL
, NULL
));
2738 nvlist_free(nvroot
);
2739 VERIFY3U(0, ==, spa_open(zo
->zo_pool
, &spa
, FTAG
));
2740 VERIFY3U(EBUSY
, ==, spa_destroy(zo
->zo_pool
));
2741 spa_close(spa
, FTAG
);
2743 (void) rw_unlock(&ztest_name_lock
);
2747 * Start and then stop the MMP threads to ensure the startup and shutdown code
2748 * works properly. Actual protection and property-related code tested via ZTS.
2752 ztest_mmp_enable_disable(ztest_ds_t
*zd
, uint64_t id
)
2754 ztest_shared_opts_t
*zo
= &ztest_opts
;
2755 spa_t
*spa
= ztest_spa
;
2757 if (zo
->zo_mmp_test
)
2760 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2761 mutex_enter(&spa
->spa_props_lock
);
2763 if (!spa_multihost(spa
)) {
2764 spa
->spa_multihost
= B_TRUE
;
2765 mmp_thread_start(spa
);
2768 mutex_exit(&spa
->spa_props_lock
);
2769 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2771 txg_wait_synced(spa_get_dsl(spa
), 0);
2772 mmp_signal_all_threads();
2773 txg_wait_synced(spa_get_dsl(spa
), 0);
2775 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
2776 mutex_enter(&spa
->spa_props_lock
);
2778 if (spa_multihost(spa
)) {
2779 mmp_thread_stop(spa
);
2780 spa
->spa_multihost
= B_FALSE
;
2783 mutex_exit(&spa
->spa_props_lock
);
2784 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
2789 ztest_spa_upgrade(ztest_ds_t
*zd
, uint64_t id
)
2792 uint64_t initial_version
= SPA_VERSION_INITIAL
;
2793 uint64_t version
, newversion
;
2794 nvlist_t
*nvroot
, *props
;
2797 if (ztest_opts
.zo_mmp_test
)
2800 mutex_enter(&ztest_vdev_lock
);
2801 name
= kmem_asprintf("%s_upgrade", ztest_opts
.zo_pool
);
2804 * Clean up from previous runs.
2806 (void) spa_destroy(name
);
2808 nvroot
= make_vdev_root(NULL
, NULL
, name
, ztest_opts
.zo_vdev_size
, 0,
2809 0, ztest_opts
.zo_raidz
, ztest_opts
.zo_mirrors
, 1);
2812 * If we're configuring a RAIDZ device then make sure that the
2813 * the initial version is capable of supporting that feature.
2815 switch (ztest_opts
.zo_raidz_parity
) {
2818 initial_version
= SPA_VERSION_INITIAL
;
2821 initial_version
= SPA_VERSION_RAIDZ2
;
2824 initial_version
= SPA_VERSION_RAIDZ3
;
2829 * Create a pool with a spa version that can be upgraded. Pick
2830 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2833 version
= ztest_random_spa_version(initial_version
);
2834 } while (version
> SPA_VERSION_BEFORE_FEATURES
);
2836 props
= fnvlist_alloc();
2837 fnvlist_add_uint64(props
,
2838 zpool_prop_to_name(ZPOOL_PROP_VERSION
), version
);
2839 VERIFY3S(spa_create(name
, nvroot
, props
, NULL
, NULL
), ==, 0);
2840 fnvlist_free(nvroot
);
2841 fnvlist_free(props
);
2843 VERIFY3S(spa_open(name
, &spa
, FTAG
), ==, 0);
2844 VERIFY3U(spa_version(spa
), ==, version
);
2845 newversion
= ztest_random_spa_version(version
+ 1);
2847 if (ztest_opts
.zo_verbose
>= 4) {
2848 (void) printf("upgrading spa version from %llu to %llu\n",
2849 (u_longlong_t
)version
, (u_longlong_t
)newversion
);
2852 spa_upgrade(spa
, newversion
);
2853 VERIFY3U(spa_version(spa
), >, version
);
2854 VERIFY3U(spa_version(spa
), ==, fnvlist_lookup_uint64(spa
->spa_config
,
2855 zpool_prop_to_name(ZPOOL_PROP_VERSION
)));
2856 spa_close(spa
, FTAG
);
2859 mutex_exit(&ztest_vdev_lock
);
2863 vdev_lookup_by_path(vdev_t
*vd
, const char *path
)
2868 if (vd
->vdev_path
!= NULL
&& strcmp(path
, vd
->vdev_path
) == 0)
2871 for (c
= 0; c
< vd
->vdev_children
; c
++)
2872 if ((mvd
= vdev_lookup_by_path(vd
->vdev_child
[c
], path
)) !=
2880 * Find the first available hole which can be used as a top-level.
2883 find_vdev_hole(spa_t
*spa
)
2885 vdev_t
*rvd
= spa
->spa_root_vdev
;
2888 ASSERT(spa_config_held(spa
, SCL_VDEV
, RW_READER
) == SCL_VDEV
);
2890 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
2891 vdev_t
*cvd
= rvd
->vdev_child
[c
];
2893 if (cvd
->vdev_ishole
)
2900 * Verify that vdev_add() works as expected.
2904 ztest_vdev_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2906 ztest_shared_t
*zs
= ztest_shared
;
2907 spa_t
*spa
= ztest_spa
;
2913 if (ztest_opts
.zo_mmp_test
)
2916 mutex_enter(&ztest_vdev_lock
);
2917 leaves
= MAX(zs
->zs_mirrors
+ zs
->zs_splits
, 1) * ztest_opts
.zo_raidz
;
2919 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
2921 ztest_shared
->zs_vdev_next_leaf
= find_vdev_hole(spa
) * leaves
;
2924 * If we have slogs then remove them 1/4 of the time.
2926 if (spa_has_slogs(spa
) && ztest_random(4) == 0) {
2928 * Grab the guid from the head of the log class rotor.
2930 guid
= spa_log_class(spa
)->mc_rotor
->mg_vd
->vdev_guid
;
2932 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2935 * We have to grab the zs_name_lock as writer to
2936 * prevent a race between removing a slog (dmu_objset_find)
2937 * and destroying a dataset. Removing the slog will
2938 * grab a reference on the dataset which may cause
2939 * dsl_destroy_head() to fail with EBUSY thus
2940 * leaving the dataset in an inconsistent state.
2942 rw_wrlock(&ztest_name_lock
);
2943 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
2944 rw_unlock(&ztest_name_lock
);
2946 if (error
&& error
!= EEXIST
)
2947 fatal(0, "spa_vdev_remove() = %d", error
);
2949 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
2952 * Make 1/4 of the devices be log devices.
2954 nvroot
= make_vdev_root(NULL
, NULL
, NULL
,
2955 ztest_opts
.zo_vdev_size
, 0,
2956 ztest_random(4) == 0, ztest_opts
.zo_raidz
,
2959 error
= spa_vdev_add(spa
, nvroot
);
2960 nvlist_free(nvroot
);
2962 if (error
== ENOSPC
)
2963 ztest_record_enospc("spa_vdev_add");
2964 else if (error
!= 0)
2965 fatal(0, "spa_vdev_add() = %d", error
);
2968 mutex_exit(&ztest_vdev_lock
);
2972 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2976 ztest_vdev_aux_add_remove(ztest_ds_t
*zd
, uint64_t id
)
2978 ztest_shared_t
*zs
= ztest_shared
;
2979 spa_t
*spa
= ztest_spa
;
2980 vdev_t
*rvd
= spa
->spa_root_vdev
;
2981 spa_aux_vdev_t
*sav
;
2987 if (ztest_opts
.zo_mmp_test
)
2990 path
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
2992 if (ztest_random(2) == 0) {
2993 sav
= &spa
->spa_spares
;
2994 aux
= ZPOOL_CONFIG_SPARES
;
2996 sav
= &spa
->spa_l2cache
;
2997 aux
= ZPOOL_CONFIG_L2CACHE
;
3000 mutex_enter(&ztest_vdev_lock
);
3002 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3004 if (sav
->sav_count
!= 0 && ztest_random(4) == 0) {
3006 * Pick a random device to remove.
3008 guid
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)]->vdev_guid
;
3011 * Find an unused device we can add.
3013 zs
->zs_vdev_aux
= 0;
3016 (void) snprintf(path
, MAXPATHLEN
, ztest_aux_template
,
3017 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
, aux
,
3019 for (c
= 0; c
< sav
->sav_count
; c
++)
3020 if (strcmp(sav
->sav_vdevs
[c
]->vdev_path
,
3023 if (c
== sav
->sav_count
&&
3024 vdev_lookup_by_path(rvd
, path
) == NULL
)
3030 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3036 nvlist_t
*nvroot
= make_vdev_root(NULL
, aux
, NULL
,
3037 (ztest_opts
.zo_vdev_size
* 5) / 4, 0, 0, 0, 0, 1);
3038 error
= spa_vdev_add(spa
, nvroot
);
3040 fatal(0, "spa_vdev_add(%p) = %d", nvroot
, error
);
3041 nvlist_free(nvroot
);
3044 * Remove an existing device. Sometimes, dirty its
3045 * vdev state first to make sure we handle removal
3046 * of devices that have pending state changes.
3048 if (ztest_random(2) == 0)
3049 (void) vdev_online(spa
, guid
, 0, NULL
);
3051 error
= spa_vdev_remove(spa
, guid
, B_FALSE
);
3052 if (error
!= 0 && error
!= EBUSY
)
3053 fatal(0, "spa_vdev_remove(%llu) = %d", guid
, error
);
3056 mutex_exit(&ztest_vdev_lock
);
3058 umem_free(path
, MAXPATHLEN
);
3062 * split a pool if it has mirror tlvdevs
3066 ztest_split_pool(ztest_ds_t
*zd
, uint64_t id
)
3068 ztest_shared_t
*zs
= ztest_shared
;
3069 spa_t
*spa
= ztest_spa
;
3070 vdev_t
*rvd
= spa
->spa_root_vdev
;
3071 nvlist_t
*tree
, **child
, *config
, *split
, **schild
;
3072 uint_t c
, children
, schildren
= 0, lastlogid
= 0;
3075 if (ztest_opts
.zo_mmp_test
)
3078 mutex_enter(&ztest_vdev_lock
);
3080 /* ensure we have a useable config; mirrors of raidz aren't supported */
3081 if (zs
->zs_mirrors
< 3 || ztest_opts
.zo_raidz
> 1) {
3082 mutex_exit(&ztest_vdev_lock
);
3086 /* clean up the old pool, if any */
3087 (void) spa_destroy("splitp");
3089 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3091 /* generate a config from the existing config */
3092 mutex_enter(&spa
->spa_props_lock
);
3093 VERIFY(nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_CONFIG_VDEV_TREE
,
3095 mutex_exit(&spa
->spa_props_lock
);
3097 VERIFY(nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3100 schild
= malloc(rvd
->vdev_children
* sizeof (nvlist_t
*));
3101 for (c
= 0; c
< children
; c
++) {
3102 vdev_t
*tvd
= rvd
->vdev_child
[c
];
3106 if (tvd
->vdev_islog
|| tvd
->vdev_ops
== &vdev_hole_ops
) {
3107 VERIFY(nvlist_alloc(&schild
[schildren
], NV_UNIQUE_NAME
,
3109 VERIFY(nvlist_add_string(schild
[schildren
],
3110 ZPOOL_CONFIG_TYPE
, VDEV_TYPE_HOLE
) == 0);
3111 VERIFY(nvlist_add_uint64(schild
[schildren
],
3112 ZPOOL_CONFIG_IS_HOLE
, 1) == 0);
3114 lastlogid
= schildren
;
3119 VERIFY(nvlist_lookup_nvlist_array(child
[c
],
3120 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3121 VERIFY(nvlist_dup(mchild
[0], &schild
[schildren
++], 0) == 0);
3124 /* OK, create a config that can be used to split */
3125 VERIFY(nvlist_alloc(&split
, NV_UNIQUE_NAME
, 0) == 0);
3126 VERIFY(nvlist_add_string(split
, ZPOOL_CONFIG_TYPE
,
3127 VDEV_TYPE_ROOT
) == 0);
3128 VERIFY(nvlist_add_nvlist_array(split
, ZPOOL_CONFIG_CHILDREN
, schild
,
3129 lastlogid
!= 0 ? lastlogid
: schildren
) == 0);
3131 VERIFY(nvlist_alloc(&config
, NV_UNIQUE_NAME
, 0) == 0);
3132 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, split
) == 0);
3134 for (c
= 0; c
< schildren
; c
++)
3135 nvlist_free(schild
[c
]);
3139 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3141 (void) rw_wrlock(&ztest_name_lock
);
3142 error
= spa_vdev_split_mirror(spa
, "splitp", config
, NULL
, B_FALSE
);
3143 (void) rw_unlock(&ztest_name_lock
);
3145 nvlist_free(config
);
3148 (void) printf("successful split - results:\n");
3149 mutex_enter(&spa_namespace_lock
);
3150 show_pool_stats(spa
);
3151 show_pool_stats(spa_lookup("splitp"));
3152 mutex_exit(&spa_namespace_lock
);
3156 mutex_exit(&ztest_vdev_lock
);
3161 * Verify that we can attach and detach devices.
3165 ztest_vdev_attach_detach(ztest_ds_t
*zd
, uint64_t id
)
3167 ztest_shared_t
*zs
= ztest_shared
;
3168 spa_t
*spa
= ztest_spa
;
3169 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
3170 vdev_t
*rvd
= spa
->spa_root_vdev
;
3171 vdev_t
*oldvd
, *newvd
, *pvd
;
3175 uint64_t ashift
= ztest_get_ashift();
3176 uint64_t oldguid
, pguid
;
3177 uint64_t oldsize
, newsize
;
3178 char *oldpath
, *newpath
;
3180 int oldvd_has_siblings
= B_FALSE
;
3181 int newvd_is_spare
= B_FALSE
;
3183 int error
, expected_error
;
3185 if (ztest_opts
.zo_mmp_test
)
3188 oldpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3189 newpath
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
3191 mutex_enter(&ztest_vdev_lock
);
3192 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
3194 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3197 * Decide whether to do an attach or a replace.
3199 replacing
= ztest_random(2);
3202 * Pick a random top-level vdev.
3204 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3207 * Pick a random leaf within it.
3209 leaf
= ztest_random(leaves
);
3214 oldvd
= rvd
->vdev_child
[top
];
3215 if (zs
->zs_mirrors
>= 1) {
3216 ASSERT(oldvd
->vdev_ops
== &vdev_mirror_ops
);
3217 ASSERT(oldvd
->vdev_children
>= zs
->zs_mirrors
);
3218 oldvd
= oldvd
->vdev_child
[leaf
/ ztest_opts
.zo_raidz
];
3220 if (ztest_opts
.zo_raidz
> 1) {
3221 ASSERT(oldvd
->vdev_ops
== &vdev_raidz_ops
);
3222 ASSERT(oldvd
->vdev_children
== ztest_opts
.zo_raidz
);
3223 oldvd
= oldvd
->vdev_child
[leaf
% ztest_opts
.zo_raidz
];
3227 * If we're already doing an attach or replace, oldvd may be a
3228 * mirror vdev -- in which case, pick a random child.
3230 while (oldvd
->vdev_children
!= 0) {
3231 oldvd_has_siblings
= B_TRUE
;
3232 ASSERT(oldvd
->vdev_children
>= 2);
3233 oldvd
= oldvd
->vdev_child
[ztest_random(oldvd
->vdev_children
)];
3236 oldguid
= oldvd
->vdev_guid
;
3237 oldsize
= vdev_get_min_asize(oldvd
);
3238 oldvd_is_log
= oldvd
->vdev_top
->vdev_islog
;
3239 (void) strcpy(oldpath
, oldvd
->vdev_path
);
3240 pvd
= oldvd
->vdev_parent
;
3241 pguid
= pvd
->vdev_guid
;
3244 * If oldvd has siblings, then half of the time, detach it.
3246 if (oldvd_has_siblings
&& ztest_random(2) == 0) {
3247 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3248 error
= spa_vdev_detach(spa
, oldguid
, pguid
, B_FALSE
);
3249 if (error
!= 0 && error
!= ENODEV
&& error
!= EBUSY
&&
3251 fatal(0, "detach (%s) returned %d", oldpath
, error
);
3256 * For the new vdev, choose with equal probability between the two
3257 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3259 if (sav
->sav_count
!= 0 && ztest_random(3) == 0) {
3260 newvd
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
3261 newvd_is_spare
= B_TRUE
;
3262 (void) strcpy(newpath
, newvd
->vdev_path
);
3264 (void) snprintf(newpath
, MAXPATHLEN
, ztest_dev_template
,
3265 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
3266 top
* leaves
+ leaf
);
3267 if (ztest_random(2) == 0)
3268 newpath
[strlen(newpath
) - 1] = 'b';
3269 newvd
= vdev_lookup_by_path(rvd
, newpath
);
3273 newsize
= vdev_get_min_asize(newvd
);
3276 * Make newsize a little bigger or smaller than oldsize.
3277 * If it's smaller, the attach should fail.
3278 * If it's larger, and we're doing a replace,
3279 * we should get dynamic LUN growth when we're done.
3281 newsize
= 10 * oldsize
/ (9 + ztest_random(3));
3285 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3286 * unless it's a replace; in that case any non-replacing parent is OK.
3288 * If newvd is already part of the pool, it should fail with EBUSY.
3290 * If newvd is too small, it should fail with EOVERFLOW.
3292 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
3293 pvd
->vdev_ops
!= &vdev_root_ops
&& (!replacing
||
3294 pvd
->vdev_ops
== &vdev_replacing_ops
||
3295 pvd
->vdev_ops
== &vdev_spare_ops
))
3296 expected_error
= ENOTSUP
;
3297 else if (newvd_is_spare
&& (!replacing
|| oldvd_is_log
))
3298 expected_error
= ENOTSUP
;
3299 else if (newvd
== oldvd
)
3300 expected_error
= replacing
? 0 : EBUSY
;
3301 else if (vdev_lookup_by_path(rvd
, newpath
) != NULL
)
3302 expected_error
= EBUSY
;
3303 else if (newsize
< oldsize
)
3304 expected_error
= EOVERFLOW
;
3305 else if (ashift
> oldvd
->vdev_top
->vdev_ashift
)
3306 expected_error
= EDOM
;
3310 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3313 * Build the nvlist describing newpath.
3315 root
= make_vdev_root(newpath
, NULL
, NULL
, newvd
== NULL
? newsize
: 0,
3316 ashift
, 0, 0, 0, 1);
3318 error
= spa_vdev_attach(spa
, oldguid
, root
, replacing
);
3323 * If our parent was the replacing vdev, but the replace completed,
3324 * then instead of failing with ENOTSUP we may either succeed,
3325 * fail with ENODEV, or fail with EOVERFLOW.
3327 if (expected_error
== ENOTSUP
&&
3328 (error
== 0 || error
== ENODEV
|| error
== EOVERFLOW
))
3329 expected_error
= error
;
3332 * If someone grew the LUN, the replacement may be too small.
3334 if (error
== EOVERFLOW
|| error
== EBUSY
)
3335 expected_error
= error
;
3337 /* XXX workaround 6690467 */
3338 if (error
!= expected_error
&& expected_error
!= EBUSY
) {
3339 fatal(0, "attach (%s %llu, %s %llu, %d) "
3340 "returned %d, expected %d",
3341 oldpath
, oldsize
, newpath
,
3342 newsize
, replacing
, error
, expected_error
);
3345 mutex_exit(&ztest_vdev_lock
);
3347 umem_free(oldpath
, MAXPATHLEN
);
3348 umem_free(newpath
, MAXPATHLEN
);
3352 * Callback function which expands the physical size of the vdev.
3355 grow_vdev(vdev_t
*vd
, void *arg
)
3357 ASSERTV(spa_t
*spa
= vd
->vdev_spa
);
3358 size_t *newsize
= arg
;
3362 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3363 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3365 if ((fd
= open(vd
->vdev_path
, O_RDWR
)) == -1)
3368 fsize
= lseek(fd
, 0, SEEK_END
);
3369 VERIFY(ftruncate(fd
, *newsize
) == 0);
3371 if (ztest_opts
.zo_verbose
>= 6) {
3372 (void) printf("%s grew from %lu to %lu bytes\n",
3373 vd
->vdev_path
, (ulong_t
)fsize
, (ulong_t
)*newsize
);
3380 * Callback function which expands a given vdev by calling vdev_online().
3384 online_vdev(vdev_t
*vd
, void *arg
)
3386 spa_t
*spa
= vd
->vdev_spa
;
3387 vdev_t
*tvd
= vd
->vdev_top
;
3388 uint64_t guid
= vd
->vdev_guid
;
3389 uint64_t generation
= spa
->spa_config_generation
+ 1;
3390 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
3393 ASSERT(spa_config_held(spa
, SCL_STATE
, RW_READER
) == SCL_STATE
);
3394 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3396 /* Calling vdev_online will initialize the new metaslabs */
3397 spa_config_exit(spa
, SCL_STATE
, spa
);
3398 error
= vdev_online(spa
, guid
, ZFS_ONLINE_EXPAND
, &newstate
);
3399 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3402 * If vdev_online returned an error or the underlying vdev_open
3403 * failed then we abort the expand. The only way to know that
3404 * vdev_open fails is by checking the returned newstate.
3406 if (error
|| newstate
!= VDEV_STATE_HEALTHY
) {
3407 if (ztest_opts
.zo_verbose
>= 5) {
3408 (void) printf("Unable to expand vdev, state %llu, "
3409 "error %d\n", (u_longlong_t
)newstate
, error
);
3413 ASSERT3U(newstate
, ==, VDEV_STATE_HEALTHY
);
3416 * Since we dropped the lock we need to ensure that we're
3417 * still talking to the original vdev. It's possible this
3418 * vdev may have been detached/replaced while we were
3419 * trying to online it.
3421 if (generation
!= spa
->spa_config_generation
) {
3422 if (ztest_opts
.zo_verbose
>= 5) {
3423 (void) printf("vdev configuration has changed, "
3424 "guid %llu, state %llu, expected gen %llu, "
3427 (u_longlong_t
)tvd
->vdev_state
,
3428 (u_longlong_t
)generation
,
3429 (u_longlong_t
)spa
->spa_config_generation
);
3437 * Traverse the vdev tree calling the supplied function.
3438 * We continue to walk the tree until we either have walked all
3439 * children or we receive a non-NULL return from the callback.
3440 * If a NULL callback is passed, then we just return back the first
3441 * leaf vdev we encounter.
3444 vdev_walk_tree(vdev_t
*vd
, vdev_t
*(*func
)(vdev_t
*, void *), void *arg
)
3448 if (vd
->vdev_ops
->vdev_op_leaf
) {
3452 return (func(vd
, arg
));
3455 for (c
= 0; c
< vd
->vdev_children
; c
++) {
3456 vdev_t
*cvd
= vd
->vdev_child
[c
];
3457 if ((cvd
= vdev_walk_tree(cvd
, func
, arg
)) != NULL
)
3464 * Verify that dynamic LUN growth works as expected.
3468 ztest_vdev_LUN_growth(ztest_ds_t
*zd
, uint64_t id
)
3470 spa_t
*spa
= ztest_spa
;
3472 metaslab_class_t
*mc
;
3473 metaslab_group_t
*mg
;
3474 size_t psize
, newsize
;
3476 uint64_t old_class_space
, new_class_space
, old_ms_count
, new_ms_count
;
3478 mutex_enter(&ztest_vdev_lock
);
3479 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3481 top
= ztest_random_vdev_top(spa
, B_TRUE
);
3483 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3486 old_ms_count
= tvd
->vdev_ms_count
;
3487 old_class_space
= metaslab_class_get_space(mc
);
3490 * Determine the size of the first leaf vdev associated with
3491 * our top-level device.
3493 vd
= vdev_walk_tree(tvd
, NULL
, NULL
);
3494 ASSERT3P(vd
, !=, NULL
);
3495 ASSERT(vd
->vdev_ops
->vdev_op_leaf
);
3497 psize
= vd
->vdev_psize
;
3500 * We only try to expand the vdev if it's healthy, less than 4x its
3501 * original size, and it has a valid psize.
3503 if (tvd
->vdev_state
!= VDEV_STATE_HEALTHY
||
3504 psize
== 0 || psize
>= 4 * ztest_opts
.zo_vdev_size
) {
3505 spa_config_exit(spa
, SCL_STATE
, spa
);
3506 mutex_exit(&ztest_vdev_lock
);
3510 newsize
= psize
+ psize
/ 8;
3511 ASSERT3U(newsize
, >, psize
);
3513 if (ztest_opts
.zo_verbose
>= 6) {
3514 (void) printf("Expanding LUN %s from %lu to %lu\n",
3515 vd
->vdev_path
, (ulong_t
)psize
, (ulong_t
)newsize
);
3519 * Growing the vdev is a two step process:
3520 * 1). expand the physical size (i.e. relabel)
3521 * 2). online the vdev to create the new metaslabs
3523 if (vdev_walk_tree(tvd
, grow_vdev
, &newsize
) != NULL
||
3524 vdev_walk_tree(tvd
, online_vdev
, NULL
) != NULL
||
3525 tvd
->vdev_state
!= VDEV_STATE_HEALTHY
) {
3526 if (ztest_opts
.zo_verbose
>= 5) {
3527 (void) printf("Could not expand LUN because "
3528 "the vdev configuration changed.\n");
3530 spa_config_exit(spa
, SCL_STATE
, spa
);
3531 mutex_exit(&ztest_vdev_lock
);
3535 spa_config_exit(spa
, SCL_STATE
, spa
);
3538 * Expanding the LUN will update the config asynchronously,
3539 * thus we must wait for the async thread to complete any
3540 * pending tasks before proceeding.
3544 mutex_enter(&spa
->spa_async_lock
);
3545 done
= (spa
->spa_async_thread
== NULL
&& !spa
->spa_async_tasks
);
3546 mutex_exit(&spa
->spa_async_lock
);
3549 txg_wait_synced(spa_get_dsl(spa
), 0);
3550 (void) poll(NULL
, 0, 100);
3553 spa_config_enter(spa
, SCL_STATE
, spa
, RW_READER
);
3555 tvd
= spa
->spa_root_vdev
->vdev_child
[top
];
3556 new_ms_count
= tvd
->vdev_ms_count
;
3557 new_class_space
= metaslab_class_get_space(mc
);
3559 if (tvd
->vdev_mg
!= mg
|| mg
->mg_class
!= mc
) {
3560 if (ztest_opts
.zo_verbose
>= 5) {
3561 (void) printf("Could not verify LUN expansion due to "
3562 "intervening vdev offline or remove.\n");
3564 spa_config_exit(spa
, SCL_STATE
, spa
);
3565 mutex_exit(&ztest_vdev_lock
);
3570 * Make sure we were able to grow the vdev.
3572 if (new_ms_count
<= old_ms_count
)
3573 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3574 old_ms_count
, new_ms_count
);
3577 * Make sure we were able to grow the pool.
3579 if (new_class_space
<= old_class_space
)
3580 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3581 old_class_space
, new_class_space
);
3583 if (ztest_opts
.zo_verbose
>= 5) {
3584 char oldnumbuf
[NN_NUMBUF_SZ
], newnumbuf
[NN_NUMBUF_SZ
];
3586 nicenum(old_class_space
, oldnumbuf
, sizeof (oldnumbuf
));
3587 nicenum(new_class_space
, newnumbuf
, sizeof (newnumbuf
));
3588 (void) printf("%s grew from %s to %s\n",
3589 spa
->spa_name
, oldnumbuf
, newnumbuf
);
3592 spa_config_exit(spa
, SCL_STATE
, spa
);
3593 mutex_exit(&ztest_vdev_lock
);
3597 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3601 ztest_objset_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
3604 * Create the objects common to all ztest datasets.
3606 VERIFY(zap_create_claim(os
, ZTEST_DIROBJ
,
3607 DMU_OT_ZAP_OTHER
, DMU_OT_NONE
, 0, tx
) == 0);
3611 ztest_dataset_create(char *dsname
)
3615 dsl_crypto_params_t
*dcp
= NULL
;
3618 * 50% of the time, we create encrypted datasets
3619 * using a random cipher suite and a hard-coded
3622 rand
= ztest_random(2);
3624 nvlist_t
*crypto_args
= fnvlist_alloc();
3625 nvlist_t
*props
= fnvlist_alloc();
3627 /* slight bias towards the default cipher suite */
3628 rand
= ztest_random(ZIO_CRYPT_FUNCTIONS
);
3629 if (rand
< ZIO_CRYPT_AES_128_CCM
)
3630 rand
= ZIO_CRYPT_ON
;
3632 fnvlist_add_uint64(props
,
3633 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), rand
);
3634 fnvlist_add_uint8_array(crypto_args
, "wkeydata",
3635 (uint8_t *)ztest_wkeydata
, WRAPPING_KEY_LEN
);
3638 * These parameters aren't really used by the kernel. They
3639 * are simply stored so that userspace knows how to load
3642 fnvlist_add_uint64(props
,
3643 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), ZFS_KEYFORMAT_RAW
);
3644 fnvlist_add_string(props
,
3645 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), "prompt");
3646 fnvlist_add_uint64(props
,
3647 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 0ULL);
3648 fnvlist_add_uint64(props
,
3649 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 0ULL);
3651 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE
, props
,
3652 crypto_args
, &dcp
));
3654 fnvlist_free(crypto_args
);
3655 fnvlist_free(props
);
3658 err
= dmu_objset_create(dsname
, DMU_OST_OTHER
, 0, dcp
,
3659 ztest_objset_create_cb
, NULL
);
3660 dsl_crypto_params_free(dcp
, !!err
);
3662 rand
= ztest_random(100);
3663 if (err
|| rand
< 80)
3666 if (ztest_opts
.zo_verbose
>= 5)
3667 (void) printf("Setting dataset %s to sync always\n", dsname
);
3668 return (ztest_dsl_prop_set_uint64(dsname
, ZFS_PROP_SYNC
,
3669 ZFS_SYNC_ALWAYS
, B_FALSE
));
3674 ztest_objset_destroy_cb(const char *name
, void *arg
)
3677 dmu_object_info_t doi
;
3681 * Verify that the dataset contains a directory object.
3683 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3684 B_TRUE
, FTAG
, &os
));
3685 error
= dmu_object_info(os
, ZTEST_DIROBJ
, &doi
);
3686 if (error
!= ENOENT
) {
3687 /* We could have crashed in the middle of destroying it */
3689 ASSERT3U(doi
.doi_type
, ==, DMU_OT_ZAP_OTHER
);
3690 ASSERT3S(doi
.doi_physical_blocks_512
, >=, 0);
3692 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3695 * Destroy the dataset.
3697 if (strchr(name
, '@') != NULL
) {
3698 VERIFY0(dsl_destroy_snapshot(name
, B_TRUE
));
3700 error
= dsl_destroy_head(name
);
3701 /* There could be a hold on this dataset */
3709 ztest_snapshot_create(char *osname
, uint64_t id
)
3711 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3714 (void) snprintf(snapname
, sizeof (snapname
), "%llu", (u_longlong_t
)id
);
3716 error
= dmu_objset_snapshot_one(osname
, snapname
);
3717 if (error
== ENOSPC
) {
3718 ztest_record_enospc(FTAG
);
3721 if (error
!= 0 && error
!= EEXIST
) {
3722 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname
,
3729 ztest_snapshot_destroy(char *osname
, uint64_t id
)
3731 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3734 (void) snprintf(snapname
, sizeof (snapname
), "%s@%llu", osname
,
3737 error
= dsl_destroy_snapshot(snapname
, B_FALSE
);
3738 if (error
!= 0 && error
!= ENOENT
)
3739 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname
, error
);
3745 ztest_dmu_objset_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3751 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3755 zdtmp
= umem_alloc(sizeof (ztest_ds_t
), UMEM_NOFAIL
);
3757 (void) rw_rdlock(&ztest_name_lock
);
3759 (void) snprintf(name
, sizeof (name
), "%s/temp_%llu",
3760 ztest_opts
.zo_pool
, (u_longlong_t
)id
);
3763 * If this dataset exists from a previous run, process its replay log
3764 * half of the time. If we don't replay it, then dsl_destroy_head()
3765 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3767 if (ztest_random(2) == 0 &&
3768 ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
3769 B_TRUE
, FTAG
, &os
) == 0) {
3770 ztest_zd_init(zdtmp
, NULL
, os
);
3771 zil_replay(os
, zdtmp
, ztest_replay_vector
);
3772 ztest_zd_fini(zdtmp
);
3773 txg_wait_synced(dmu_objset_pool(os
), 0);
3774 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3778 * There may be an old instance of the dataset we're about to
3779 * create lying around from a previous run. If so, destroy it
3780 * and all of its snapshots.
3782 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
3783 DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
3786 * Verify that the destroyed dataset is no longer in the namespace.
3788 VERIFY3U(ENOENT
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_TRUE
,
3789 B_TRUE
, FTAG
, &os
));
3792 * Verify that we can create a new dataset.
3794 error
= ztest_dataset_create(name
);
3796 if (error
== ENOSPC
) {
3797 ztest_record_enospc(FTAG
);
3800 fatal(0, "dmu_objset_create(%s) = %d", name
, error
);
3803 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
, B_TRUE
,
3806 ztest_zd_init(zdtmp
, NULL
, os
);
3809 * Open the intent log for it.
3811 zilog
= zil_open(os
, ztest_get_data
);
3814 * Put some objects in there, do a little I/O to them,
3815 * and randomly take a couple of snapshots along the way.
3817 iters
= ztest_random(5);
3818 for (i
= 0; i
< iters
; i
++) {
3819 ztest_dmu_object_alloc_free(zdtmp
, id
);
3820 if (ztest_random(iters
) == 0)
3821 (void) ztest_snapshot_create(name
, i
);
3825 * Verify that we cannot create an existing dataset.
3827 VERIFY3U(EEXIST
, ==,
3828 dmu_objset_create(name
, DMU_OST_OTHER
, 0, NULL
, NULL
, NULL
));
3831 * Verify that we can hold an objset that is also owned.
3833 VERIFY3U(0, ==, dmu_objset_hold(name
, FTAG
, &os2
));
3834 dmu_objset_rele(os2
, FTAG
);
3837 * Verify that we cannot own an objset that is already owned.
3839 VERIFY3U(EBUSY
, ==, ztest_dmu_objset_own(name
, DMU_OST_OTHER
,
3840 B_FALSE
, B_TRUE
, FTAG
, &os2
));
3843 txg_wait_synced(spa_get_dsl(os
->os_spa
), 0);
3844 dmu_objset_disown(os
, B_TRUE
, FTAG
);
3845 ztest_zd_fini(zdtmp
);
3847 (void) rw_unlock(&ztest_name_lock
);
3849 umem_free(zdtmp
, sizeof (ztest_ds_t
));
3853 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3856 ztest_dmu_snapshot_create_destroy(ztest_ds_t
*zd
, uint64_t id
)
3858 (void) rw_rdlock(&ztest_name_lock
);
3859 (void) ztest_snapshot_destroy(zd
->zd_name
, id
);
3860 (void) ztest_snapshot_create(zd
->zd_name
, id
);
3861 (void) rw_unlock(&ztest_name_lock
);
3865 * Cleanup non-standard snapshots and clones.
3868 ztest_dsl_dataset_cleanup(char *osname
, uint64_t id
)
3877 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3878 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3879 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3880 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3881 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3883 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3884 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3885 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3886 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3887 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3888 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3889 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3890 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3891 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3892 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3894 error
= dsl_destroy_head(clone2name
);
3895 if (error
&& error
!= ENOENT
)
3896 fatal(0, "dsl_destroy_head(%s) = %d", clone2name
, error
);
3897 error
= dsl_destroy_snapshot(snap3name
, B_FALSE
);
3898 if (error
&& error
!= ENOENT
)
3899 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name
, error
);
3900 error
= dsl_destroy_snapshot(snap2name
, B_FALSE
);
3901 if (error
&& error
!= ENOENT
)
3902 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name
, error
);
3903 error
= dsl_destroy_head(clone1name
);
3904 if (error
&& error
!= ENOENT
)
3905 fatal(0, "dsl_destroy_head(%s) = %d", clone1name
, error
);
3906 error
= dsl_destroy_snapshot(snap1name
, B_FALSE
);
3907 if (error
&& error
!= ENOENT
)
3908 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name
, error
);
3910 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
3911 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
3912 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
3913 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
3914 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
3918 * Verify dsl_dataset_promote handles EBUSY
3921 ztest_dsl_dataset_promote_busy(ztest_ds_t
*zd
, uint64_t id
)
3929 char *osname
= zd
->zd_name
;
3932 snap1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3933 clone1name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3934 snap2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3935 clone2name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3936 snap3name
= umem_alloc(ZFS_MAX_DATASET_NAME_LEN
, UMEM_NOFAIL
);
3938 (void) rw_rdlock(&ztest_name_lock
);
3940 ztest_dsl_dataset_cleanup(osname
, id
);
3942 (void) snprintf(snap1name
, ZFS_MAX_DATASET_NAME_LEN
,
3943 "%s@s1_%llu", osname
, (u_longlong_t
)id
);
3944 (void) snprintf(clone1name
, ZFS_MAX_DATASET_NAME_LEN
,
3945 "%s/c1_%llu", osname
, (u_longlong_t
)id
);
3946 (void) snprintf(snap2name
, ZFS_MAX_DATASET_NAME_LEN
,
3947 "%s@s2_%llu", clone1name
, (u_longlong_t
)id
);
3948 (void) snprintf(clone2name
, ZFS_MAX_DATASET_NAME_LEN
,
3949 "%s/c2_%llu", osname
, (u_longlong_t
)id
);
3950 (void) snprintf(snap3name
, ZFS_MAX_DATASET_NAME_LEN
,
3951 "%s@s3_%llu", clone1name
, (u_longlong_t
)id
);
3953 error
= dmu_objset_snapshot_one(osname
, strchr(snap1name
, '@') + 1);
3954 if (error
&& error
!= EEXIST
) {
3955 if (error
== ENOSPC
) {
3956 ztest_record_enospc(FTAG
);
3959 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name
, error
);
3962 error
= dmu_objset_clone(clone1name
, snap1name
);
3964 if (error
== ENOSPC
) {
3965 ztest_record_enospc(FTAG
);
3968 fatal(0, "dmu_objset_create(%s) = %d", clone1name
, error
);
3971 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap2name
, '@') + 1);
3972 if (error
&& error
!= EEXIST
) {
3973 if (error
== ENOSPC
) {
3974 ztest_record_enospc(FTAG
);
3977 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name
, error
);
3980 error
= dmu_objset_snapshot_one(clone1name
, strchr(snap3name
, '@') + 1);
3981 if (error
&& error
!= EEXIST
) {
3982 if (error
== ENOSPC
) {
3983 ztest_record_enospc(FTAG
);
3986 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name
, error
);
3989 error
= dmu_objset_clone(clone2name
, snap3name
);
3991 if (error
== ENOSPC
) {
3992 ztest_record_enospc(FTAG
);
3995 fatal(0, "dmu_objset_create(%s) = %d", clone2name
, error
);
3998 error
= ztest_dmu_objset_own(snap2name
, DMU_OST_ANY
, B_TRUE
, B_TRUE
,
4001 fatal(0, "dmu_objset_own(%s) = %d", snap2name
, error
);
4002 error
= dsl_dataset_promote(clone2name
, NULL
);
4003 if (error
== ENOSPC
) {
4004 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4005 ztest_record_enospc(FTAG
);
4009 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name
,
4011 dmu_objset_disown(os
, B_TRUE
, FTAG
);
4014 ztest_dsl_dataset_cleanup(osname
, id
);
4016 (void) rw_unlock(&ztest_name_lock
);
4018 umem_free(snap1name
, ZFS_MAX_DATASET_NAME_LEN
);
4019 umem_free(clone1name
, ZFS_MAX_DATASET_NAME_LEN
);
4020 umem_free(snap2name
, ZFS_MAX_DATASET_NAME_LEN
);
4021 umem_free(clone2name
, ZFS_MAX_DATASET_NAME_LEN
);
4022 umem_free(snap3name
, ZFS_MAX_DATASET_NAME_LEN
);
4025 #undef OD_ARRAY_SIZE
4026 #define OD_ARRAY_SIZE 4
4029 * Verify that dmu_object_{alloc,free} work as expected.
4032 ztest_dmu_object_alloc_free(ztest_ds_t
*zd
, uint64_t id
)
4039 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4040 od
= umem_alloc(size
, UMEM_NOFAIL
);
4041 batchsize
= OD_ARRAY_SIZE
;
4043 for (b
= 0; b
< batchsize
; b
++)
4044 ztest_od_init(od
+ b
, id
, FTAG
, b
, DMU_OT_UINT64_OTHER
,
4048 * Destroy the previous batch of objects, create a new batch,
4049 * and do some I/O on the new objects.
4051 if (ztest_object_init(zd
, od
, size
, B_TRUE
) != 0)
4054 while (ztest_random(4 * batchsize
) != 0)
4055 ztest_io(zd
, od
[ztest_random(batchsize
)].od_object
,
4056 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4058 umem_free(od
, size
);
4062 * Rewind the global allocator to verify object allocation backfilling.
4065 ztest_dmu_object_next_chunk(ztest_ds_t
*zd
, uint64_t id
)
4067 objset_t
*os
= zd
->zd_os
;
4068 int dnodes_per_chunk
= 1 << dmu_object_alloc_chunk_shift
;
4072 * Rewind the global allocator randomly back to a lower object number
4073 * to force backfilling and reclamation of recently freed dnodes.
4075 mutex_enter(&os
->os_obj_lock
);
4076 object
= ztest_random(os
->os_obj_next_chunk
);
4077 os
->os_obj_next_chunk
= P2ALIGN(object
, dnodes_per_chunk
);
4078 mutex_exit(&os
->os_obj_lock
);
4081 #undef OD_ARRAY_SIZE
4082 #define OD_ARRAY_SIZE 2
4085 * Verify that dmu_{read,write} work as expected.
4088 ztest_dmu_read_write(ztest_ds_t
*zd
, uint64_t id
)
4093 objset_t
*os
= zd
->zd_os
;
4094 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4095 od
= umem_alloc(size
, UMEM_NOFAIL
);
4097 int i
, freeit
, error
;
4099 bufwad_t
*packbuf
, *bigbuf
, *pack
, *bigH
, *bigT
;
4100 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4101 uint64_t chunksize
= (1000 + ztest_random(1000)) * sizeof (uint64_t);
4102 uint64_t regions
= 997;
4103 uint64_t stride
= 123456789ULL;
4104 uint64_t width
= 40;
4105 int free_percent
= 5;
4108 * This test uses two objects, packobj and bigobj, that are always
4109 * updated together (i.e. in the same tx) so that their contents are
4110 * in sync and can be compared. Their contents relate to each other
4111 * in a simple way: packobj is a dense array of 'bufwad' structures,
4112 * while bigobj is a sparse array of the same bufwads. Specifically,
4113 * for any index n, there are three bufwads that should be identical:
4115 * packobj, at offset n * sizeof (bufwad_t)
4116 * bigobj, at the head of the nth chunk
4117 * bigobj, at the tail of the nth chunk
4119 * The chunk size is arbitrary. It doesn't have to be a power of two,
4120 * and it doesn't have any relation to the object blocksize.
4121 * The only requirement is that it can hold at least two bufwads.
4123 * Normally, we write the bufwad to each of these locations.
4124 * However, free_percent of the time we instead write zeroes to
4125 * packobj and perform a dmu_free_range() on bigobj. By comparing
4126 * bigobj to packobj, we can verify that the DMU is correctly
4127 * tracking which parts of an object are allocated and free,
4128 * and that the contents of the allocated blocks are correct.
4132 * Read the directory info. If it's the first time, set things up.
4134 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, chunksize
);
4135 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4138 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4139 umem_free(od
, size
);
4143 bigobj
= od
[0].od_object
;
4144 packobj
= od
[1].od_object
;
4145 chunksize
= od
[0].od_gen
;
4146 ASSERT(chunksize
== od
[1].od_gen
);
4149 * Prefetch a random chunk of the big object.
4150 * Our aim here is to get some async reads in flight
4151 * for blocks that we may free below; the DMU should
4152 * handle this race correctly.
4154 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4155 s
= 1 + ztest_random(2 * width
- 1);
4156 dmu_prefetch(os
, bigobj
, 0, n
* chunksize
, s
* chunksize
,
4157 ZIO_PRIORITY_SYNC_READ
);
4160 * Pick a random index and compute the offsets into packobj and bigobj.
4162 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4163 s
= 1 + ztest_random(width
- 1);
4165 packoff
= n
* sizeof (bufwad_t
);
4166 packsize
= s
* sizeof (bufwad_t
);
4168 bigoff
= n
* chunksize
;
4169 bigsize
= s
* chunksize
;
4171 packbuf
= umem_alloc(packsize
, UMEM_NOFAIL
);
4172 bigbuf
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4175 * free_percent of the time, free a range of bigobj rather than
4178 freeit
= (ztest_random(100) < free_percent
);
4181 * Read the current contents of our objects.
4183 error
= dmu_read(os
, packobj
, packoff
, packsize
, packbuf
,
4186 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
, bigbuf
,
4191 * Get a tx for the mods to both packobj and bigobj.
4193 tx
= dmu_tx_create(os
);
4195 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4198 dmu_tx_hold_free(tx
, bigobj
, bigoff
, bigsize
);
4200 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4202 /* This accounts for setting the checksum/compression. */
4203 dmu_tx_hold_bonus(tx
, bigobj
);
4205 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4207 umem_free(packbuf
, packsize
);
4208 umem_free(bigbuf
, bigsize
);
4209 umem_free(od
, size
);
4213 enum zio_checksum cksum
;
4215 cksum
= (enum zio_checksum
)
4216 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM
);
4217 } while (cksum
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
4218 dmu_object_set_checksum(os
, bigobj
, cksum
, tx
);
4220 enum zio_compress comp
;
4222 comp
= (enum zio_compress
)
4223 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION
);
4224 } while (comp
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
);
4225 dmu_object_set_compress(os
, bigobj
, comp
, tx
);
4228 * For each index from n to n + s, verify that the existing bufwad
4229 * in packobj matches the bufwads at the head and tail of the
4230 * corresponding chunk in bigobj. Then update all three bufwads
4231 * with the new values we want to write out.
4233 for (i
= 0; i
< s
; i
++) {
4235 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4237 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4239 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4241 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4242 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4244 if (pack
->bw_txg
> txg
)
4245 fatal(0, "future leak: got %llx, open txg is %llx",
4248 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4249 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4250 pack
->bw_index
, n
, i
);
4252 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4253 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4255 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4256 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4259 bzero(pack
, sizeof (bufwad_t
));
4261 pack
->bw_index
= n
+ i
;
4263 pack
->bw_data
= 1 + ztest_random(-2ULL);
4270 * We've verified all the old bufwads, and made new ones.
4271 * Now write them out.
4273 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4276 if (ztest_opts
.zo_verbose
>= 7) {
4277 (void) printf("freeing offset %llx size %llx"
4279 (u_longlong_t
)bigoff
,
4280 (u_longlong_t
)bigsize
,
4283 VERIFY(0 == dmu_free_range(os
, bigobj
, bigoff
, bigsize
, tx
));
4285 if (ztest_opts
.zo_verbose
>= 7) {
4286 (void) printf("writing offset %llx size %llx"
4288 (u_longlong_t
)bigoff
,
4289 (u_longlong_t
)bigsize
,
4292 dmu_write(os
, bigobj
, bigoff
, bigsize
, bigbuf
, tx
);
4298 * Sanity check the stuff we just wrote.
4301 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4302 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4304 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4305 packsize
, packcheck
, DMU_READ_PREFETCH
));
4306 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4307 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4309 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4310 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4312 umem_free(packcheck
, packsize
);
4313 umem_free(bigcheck
, bigsize
);
4316 umem_free(packbuf
, packsize
);
4317 umem_free(bigbuf
, bigsize
);
4318 umem_free(od
, size
);
4322 compare_and_update_pbbufs(uint64_t s
, bufwad_t
*packbuf
, bufwad_t
*bigbuf
,
4323 uint64_t bigsize
, uint64_t n
, uint64_t chunksize
, uint64_t txg
)
4331 * For each index from n to n + s, verify that the existing bufwad
4332 * in packobj matches the bufwads at the head and tail of the
4333 * corresponding chunk in bigobj. Then update all three bufwads
4334 * with the new values we want to write out.
4336 for (i
= 0; i
< s
; i
++) {
4338 pack
= (bufwad_t
*)((char *)packbuf
+ i
* sizeof (bufwad_t
));
4340 bigH
= (bufwad_t
*)((char *)bigbuf
+ i
* chunksize
);
4342 bigT
= (bufwad_t
*)((char *)bigH
+ chunksize
) - 1;
4344 ASSERT((uintptr_t)bigH
- (uintptr_t)bigbuf
< bigsize
);
4345 ASSERT((uintptr_t)bigT
- (uintptr_t)bigbuf
< bigsize
);
4347 if (pack
->bw_txg
> txg
)
4348 fatal(0, "future leak: got %llx, open txg is %llx",
4351 if (pack
->bw_data
!= 0 && pack
->bw_index
!= n
+ i
)
4352 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4353 pack
->bw_index
, n
, i
);
4355 if (bcmp(pack
, bigH
, sizeof (bufwad_t
)) != 0)
4356 fatal(0, "pack/bigH mismatch in %p/%p", pack
, bigH
);
4358 if (bcmp(pack
, bigT
, sizeof (bufwad_t
)) != 0)
4359 fatal(0, "pack/bigT mismatch in %p/%p", pack
, bigT
);
4361 pack
->bw_index
= n
+ i
;
4363 pack
->bw_data
= 1 + ztest_random(-2ULL);
4370 #undef OD_ARRAY_SIZE
4371 #define OD_ARRAY_SIZE 2
4374 ztest_dmu_read_write_zcopy(ztest_ds_t
*zd
, uint64_t id
)
4376 objset_t
*os
= zd
->zd_os
;
4383 bufwad_t
*packbuf
, *bigbuf
;
4384 uint64_t packobj
, packoff
, packsize
, bigobj
, bigoff
, bigsize
;
4385 uint64_t blocksize
= ztest_random_blocksize();
4386 uint64_t chunksize
= blocksize
;
4387 uint64_t regions
= 997;
4388 uint64_t stride
= 123456789ULL;
4390 dmu_buf_t
*bonus_db
;
4391 arc_buf_t
**bigbuf_arcbufs
;
4392 dmu_object_info_t doi
;
4394 size
= sizeof (ztest_od_t
) * OD_ARRAY_SIZE
;
4395 od
= umem_alloc(size
, UMEM_NOFAIL
);
4398 * This test uses two objects, packobj and bigobj, that are always
4399 * updated together (i.e. in the same tx) so that their contents are
4400 * in sync and can be compared. Their contents relate to each other
4401 * in a simple way: packobj is a dense array of 'bufwad' structures,
4402 * while bigobj is a sparse array of the same bufwads. Specifically,
4403 * for any index n, there are three bufwads that should be identical:
4405 * packobj, at offset n * sizeof (bufwad_t)
4406 * bigobj, at the head of the nth chunk
4407 * bigobj, at the tail of the nth chunk
4409 * The chunk size is set equal to bigobj block size so that
4410 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4414 * Read the directory info. If it's the first time, set things up.
4416 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4417 ztest_od_init(od
+ 1, id
, FTAG
, 1, DMU_OT_UINT64_OTHER
, 0, 0,
4421 if (ztest_object_init(zd
, od
, size
, B_FALSE
) != 0) {
4422 umem_free(od
, size
);
4426 bigobj
= od
[0].od_object
;
4427 packobj
= od
[1].od_object
;
4428 blocksize
= od
[0].od_blocksize
;
4429 chunksize
= blocksize
;
4430 ASSERT(chunksize
== od
[1].od_gen
);
4432 VERIFY(dmu_object_info(os
, bigobj
, &doi
) == 0);
4433 VERIFY(ISP2(doi
.doi_data_block_size
));
4434 VERIFY(chunksize
== doi
.doi_data_block_size
);
4435 VERIFY(chunksize
>= 2 * sizeof (bufwad_t
));
4438 * Pick a random index and compute the offsets into packobj and bigobj.
4440 n
= ztest_random(regions
) * stride
+ ztest_random(width
);
4441 s
= 1 + ztest_random(width
- 1);
4443 packoff
= n
* sizeof (bufwad_t
);
4444 packsize
= s
* sizeof (bufwad_t
);
4446 bigoff
= n
* chunksize
;
4447 bigsize
= s
* chunksize
;
4449 packbuf
= umem_zalloc(packsize
, UMEM_NOFAIL
);
4450 bigbuf
= umem_zalloc(bigsize
, UMEM_NOFAIL
);
4452 VERIFY3U(0, ==, dmu_bonus_hold(os
, bigobj
, FTAG
, &bonus_db
));
4454 bigbuf_arcbufs
= umem_zalloc(2 * s
* sizeof (arc_buf_t
*), UMEM_NOFAIL
);
4457 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4458 * Iteration 1 test zcopy to already referenced dbufs.
4459 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4460 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4461 * Iteration 4 test zcopy when dbuf is no longer dirty.
4462 * Iteration 5 test zcopy when it can't be done.
4463 * Iteration 6 one more zcopy write.
4465 for (i
= 0; i
< 7; i
++) {
4470 * In iteration 5 (i == 5) use arcbufs
4471 * that don't match bigobj blksz to test
4472 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4473 * assign an arcbuf to a dbuf.
4475 for (j
= 0; j
< s
; j
++) {
4476 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4478 dmu_request_arcbuf(bonus_db
, chunksize
);
4480 bigbuf_arcbufs
[2 * j
] =
4481 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4482 bigbuf_arcbufs
[2 * j
+ 1] =
4483 dmu_request_arcbuf(bonus_db
, chunksize
/ 2);
4488 * Get a tx for the mods to both packobj and bigobj.
4490 tx
= dmu_tx_create(os
);
4492 dmu_tx_hold_write(tx
, packobj
, packoff
, packsize
);
4493 dmu_tx_hold_write(tx
, bigobj
, bigoff
, bigsize
);
4495 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4497 umem_free(packbuf
, packsize
);
4498 umem_free(bigbuf
, bigsize
);
4499 for (j
= 0; j
< s
; j
++) {
4501 chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4502 dmu_return_arcbuf(bigbuf_arcbufs
[j
]);
4505 bigbuf_arcbufs
[2 * j
]);
4507 bigbuf_arcbufs
[2 * j
+ 1]);
4510 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4511 umem_free(od
, size
);
4512 dmu_buf_rele(bonus_db
, FTAG
);
4517 * 50% of the time don't read objects in the 1st iteration to
4518 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4519 * no existing dbufs for the specified offsets.
4521 if (i
!= 0 || ztest_random(2) != 0) {
4522 error
= dmu_read(os
, packobj
, packoff
,
4523 packsize
, packbuf
, DMU_READ_PREFETCH
);
4525 error
= dmu_read(os
, bigobj
, bigoff
, bigsize
,
4526 bigbuf
, DMU_READ_PREFETCH
);
4529 compare_and_update_pbbufs(s
, packbuf
, bigbuf
, bigsize
,
4533 * We've verified all the old bufwads, and made new ones.
4534 * Now write them out.
4536 dmu_write(os
, packobj
, packoff
, packsize
, packbuf
, tx
);
4537 if (ztest_opts
.zo_verbose
>= 7) {
4538 (void) printf("writing offset %llx size %llx"
4540 (u_longlong_t
)bigoff
,
4541 (u_longlong_t
)bigsize
,
4544 for (off
= bigoff
, j
= 0; j
< s
; j
++, off
+= chunksize
) {
4546 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4547 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4548 bigbuf_arcbufs
[j
]->b_data
, chunksize
);
4550 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
),
4551 bigbuf_arcbufs
[2 * j
]->b_data
,
4553 bcopy((caddr_t
)bigbuf
+ (off
- bigoff
) +
4555 bigbuf_arcbufs
[2 * j
+ 1]->b_data
,
4560 VERIFY(dmu_buf_hold(os
, bigobj
, off
,
4561 FTAG
, &dbt
, DMU_READ_NO_PREFETCH
) == 0);
4563 if (i
!= 5 || chunksize
< (SPA_MINBLOCKSIZE
* 2)) {
4564 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4565 bigbuf_arcbufs
[j
], tx
);
4567 dmu_assign_arcbuf_by_dbuf(bonus_db
, off
,
4568 bigbuf_arcbufs
[2 * j
], tx
);
4569 dmu_assign_arcbuf_by_dbuf(bonus_db
,
4570 off
+ chunksize
/ 2,
4571 bigbuf_arcbufs
[2 * j
+ 1], tx
);
4574 dmu_buf_rele(dbt
, FTAG
);
4580 * Sanity check the stuff we just wrote.
4583 void *packcheck
= umem_alloc(packsize
, UMEM_NOFAIL
);
4584 void *bigcheck
= umem_alloc(bigsize
, UMEM_NOFAIL
);
4586 VERIFY(0 == dmu_read(os
, packobj
, packoff
,
4587 packsize
, packcheck
, DMU_READ_PREFETCH
));
4588 VERIFY(0 == dmu_read(os
, bigobj
, bigoff
,
4589 bigsize
, bigcheck
, DMU_READ_PREFETCH
));
4591 ASSERT(bcmp(packbuf
, packcheck
, packsize
) == 0);
4592 ASSERT(bcmp(bigbuf
, bigcheck
, bigsize
) == 0);
4594 umem_free(packcheck
, packsize
);
4595 umem_free(bigcheck
, bigsize
);
4598 txg_wait_open(dmu_objset_pool(os
), 0);
4599 } else if (i
== 3) {
4600 txg_wait_synced(dmu_objset_pool(os
), 0);
4604 dmu_buf_rele(bonus_db
, FTAG
);
4605 umem_free(packbuf
, packsize
);
4606 umem_free(bigbuf
, bigsize
);
4607 umem_free(bigbuf_arcbufs
, 2 * s
* sizeof (arc_buf_t
*));
4608 umem_free(od
, size
);
4613 ztest_dmu_write_parallel(ztest_ds_t
*zd
, uint64_t id
)
4617 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4618 uint64_t offset
= (1ULL << (ztest_random(20) + 43)) +
4619 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4622 * Have multiple threads write to large offsets in an object
4623 * to verify that parallel writes to an object -- even to the
4624 * same blocks within the object -- doesn't cause any trouble.
4626 ztest_od_init(od
, ID_PARALLEL
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
4628 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0)
4631 while (ztest_random(10) != 0)
4632 ztest_io(zd
, od
->od_object
, offset
);
4634 umem_free(od
, sizeof (ztest_od_t
));
4638 ztest_dmu_prealloc(ztest_ds_t
*zd
, uint64_t id
)
4641 uint64_t offset
= (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT
)) +
4642 (ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
4643 uint64_t count
= ztest_random(20) + 1;
4644 uint64_t blocksize
= ztest_random_blocksize();
4647 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4649 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
4651 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4652 !ztest_random(2)) != 0) {
4653 umem_free(od
, sizeof (ztest_od_t
));
4657 if (ztest_truncate(zd
, od
->od_object
, offset
, count
* blocksize
) != 0) {
4658 umem_free(od
, sizeof (ztest_od_t
));
4662 ztest_prealloc(zd
, od
->od_object
, offset
, count
* blocksize
);
4664 data
= umem_zalloc(blocksize
, UMEM_NOFAIL
);
4666 while (ztest_random(count
) != 0) {
4667 uint64_t randoff
= offset
+ (ztest_random(count
) * blocksize
);
4668 if (ztest_write(zd
, od
->od_object
, randoff
, blocksize
,
4671 while (ztest_random(4) != 0)
4672 ztest_io(zd
, od
->od_object
, randoff
);
4675 umem_free(data
, blocksize
);
4676 umem_free(od
, sizeof (ztest_od_t
));
4680 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4682 #define ZTEST_ZAP_MIN_INTS 1
4683 #define ZTEST_ZAP_MAX_INTS 4
4684 #define ZTEST_ZAP_MAX_PROPS 1000
4687 ztest_zap(ztest_ds_t
*zd
, uint64_t id
)
4689 objset_t
*os
= zd
->zd_os
;
4692 uint64_t txg
, last_txg
;
4693 uint64_t value
[ZTEST_ZAP_MAX_INTS
];
4694 uint64_t zl_ints
, zl_intsize
, prop
;
4697 char propname
[100], txgname
[100];
4699 char *hc
[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4701 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4702 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4704 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4705 !ztest_random(2)) != 0)
4708 object
= od
->od_object
;
4711 * Generate a known hash collision, and verify that
4712 * we can lookup and remove both entries.
4714 tx
= dmu_tx_create(os
);
4715 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4716 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4719 for (i
= 0; i
< 2; i
++) {
4721 VERIFY3U(0, ==, zap_add(os
, object
, hc
[i
], sizeof (uint64_t),
4724 for (i
= 0; i
< 2; i
++) {
4725 VERIFY3U(EEXIST
, ==, zap_add(os
, object
, hc
[i
],
4726 sizeof (uint64_t), 1, &value
[i
], tx
));
4728 zap_length(os
, object
, hc
[i
], &zl_intsize
, &zl_ints
));
4729 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4730 ASSERT3U(zl_ints
, ==, 1);
4732 for (i
= 0; i
< 2; i
++) {
4733 VERIFY3U(0, ==, zap_remove(os
, object
, hc
[i
], tx
));
4738 * Generate a buch of random entries.
4740 ints
= MAX(ZTEST_ZAP_MIN_INTS
, object
% ZTEST_ZAP_MAX_INTS
);
4742 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4743 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4744 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4745 bzero(value
, sizeof (value
));
4749 * If these zap entries already exist, validate their contents.
4751 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4753 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4754 ASSERT3U(zl_ints
, ==, 1);
4756 VERIFY(zap_lookup(os
, object
, txgname
, zl_intsize
,
4757 zl_ints
, &last_txg
) == 0);
4759 VERIFY(zap_length(os
, object
, propname
, &zl_intsize
,
4762 ASSERT3U(zl_intsize
, ==, sizeof (uint64_t));
4763 ASSERT3U(zl_ints
, ==, ints
);
4765 VERIFY(zap_lookup(os
, object
, propname
, zl_intsize
,
4766 zl_ints
, value
) == 0);
4768 for (i
= 0; i
< ints
; i
++) {
4769 ASSERT3U(value
[i
], ==, last_txg
+ object
+ i
);
4772 ASSERT3U(error
, ==, ENOENT
);
4776 * Atomically update two entries in our zap object.
4777 * The first is named txg_%llu, and contains the txg
4778 * in which the property was last updated. The second
4779 * is named prop_%llu, and the nth element of its value
4780 * should be txg + object + n.
4782 tx
= dmu_tx_create(os
);
4783 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4784 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4789 fatal(0, "zap future leak: old %llu new %llu", last_txg
, txg
);
4791 for (i
= 0; i
< ints
; i
++)
4792 value
[i
] = txg
+ object
+ i
;
4794 VERIFY3U(0, ==, zap_update(os
, object
, txgname
, sizeof (uint64_t),
4796 VERIFY3U(0, ==, zap_update(os
, object
, propname
, sizeof (uint64_t),
4802 * Remove a random pair of entries.
4804 prop
= ztest_random(ZTEST_ZAP_MAX_PROPS
);
4805 (void) sprintf(propname
, "prop_%llu", (u_longlong_t
)prop
);
4806 (void) sprintf(txgname
, "txg_%llu", (u_longlong_t
)prop
);
4808 error
= zap_length(os
, object
, txgname
, &zl_intsize
, &zl_ints
);
4810 if (error
== ENOENT
)
4815 tx
= dmu_tx_create(os
);
4816 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4817 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4820 VERIFY3U(0, ==, zap_remove(os
, object
, txgname
, tx
));
4821 VERIFY3U(0, ==, zap_remove(os
, object
, propname
, tx
));
4824 umem_free(od
, sizeof (ztest_od_t
));
4828 * Testcase to test the upgrading of a microzap to fatzap.
4831 ztest_fzap(ztest_ds_t
*zd
, uint64_t id
)
4833 objset_t
*os
= zd
->zd_os
;
4835 uint64_t object
, txg
;
4838 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4839 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4841 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
),
4842 !ztest_random(2)) != 0)
4844 object
= od
->od_object
;
4847 * Add entries to this ZAP and make sure it spills over
4848 * and gets upgraded to a fatzap. Also, since we are adding
4849 * 2050 entries we should see ptrtbl growth and leaf-block split.
4851 for (i
= 0; i
< 2050; i
++) {
4852 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4857 (void) snprintf(name
, sizeof (name
), "fzap-%llu-%llu",
4858 (u_longlong_t
)id
, (u_longlong_t
)value
);
4860 tx
= dmu_tx_create(os
);
4861 dmu_tx_hold_zap(tx
, object
, B_TRUE
, name
);
4862 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4865 error
= zap_add(os
, object
, name
, sizeof (uint64_t), 1,
4867 ASSERT(error
== 0 || error
== EEXIST
);
4871 umem_free(od
, sizeof (ztest_od_t
));
4876 ztest_zap_parallel(ztest_ds_t
*zd
, uint64_t id
)
4878 objset_t
*os
= zd
->zd_os
;
4880 uint64_t txg
, object
, count
, wsize
, wc
, zl_wsize
, zl_wc
;
4882 int i
, namelen
, error
;
4883 int micro
= ztest_random(2);
4884 char name
[20], string_value
[20];
4887 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
4888 ztest_od_init(od
, ID_PARALLEL
, FTAG
, micro
, DMU_OT_ZAP_OTHER
, 0, 0, 0);
4890 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
4891 umem_free(od
, sizeof (ztest_od_t
));
4895 object
= od
->od_object
;
4898 * Generate a random name of the form 'xxx.....' where each
4899 * x is a random printable character and the dots are dots.
4900 * There are 94 such characters, and the name length goes from
4901 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4903 namelen
= ztest_random(sizeof (name
) - 5) + 5 + 1;
4905 for (i
= 0; i
< 3; i
++)
4906 name
[i
] = '!' + ztest_random('~' - '!' + 1);
4907 for (; i
< namelen
- 1; i
++)
4911 if ((namelen
& 1) || micro
) {
4912 wsize
= sizeof (txg
);
4918 data
= string_value
;
4922 VERIFY0(zap_count(os
, object
, &count
));
4923 ASSERT(count
!= -1ULL);
4926 * Select an operation: length, lookup, add, update, remove.
4928 i
= ztest_random(5);
4931 tx
= dmu_tx_create(os
);
4932 dmu_tx_hold_zap(tx
, object
, B_TRUE
, NULL
);
4933 txg
= ztest_tx_assign(tx
, TXG_MIGHTWAIT
, FTAG
);
4935 umem_free(od
, sizeof (ztest_od_t
));
4938 bcopy(name
, string_value
, namelen
);
4942 bzero(string_value
, namelen
);
4948 error
= zap_length(os
, object
, name
, &zl_wsize
, &zl_wc
);
4950 ASSERT3U(wsize
, ==, zl_wsize
);
4951 ASSERT3U(wc
, ==, zl_wc
);
4953 ASSERT3U(error
, ==, ENOENT
);
4958 error
= zap_lookup(os
, object
, name
, wsize
, wc
, data
);
4960 if (data
== string_value
&&
4961 bcmp(name
, data
, namelen
) != 0)
4962 fatal(0, "name '%s' != val '%s' len %d",
4963 name
, data
, namelen
);
4965 ASSERT3U(error
, ==, ENOENT
);
4970 error
= zap_add(os
, object
, name
, wsize
, wc
, data
, tx
);
4971 ASSERT(error
== 0 || error
== EEXIST
);
4975 VERIFY(zap_update(os
, object
, name
, wsize
, wc
, data
, tx
) == 0);
4979 error
= zap_remove(os
, object
, name
, tx
);
4980 ASSERT(error
== 0 || error
== ENOENT
);
4987 umem_free(od
, sizeof (ztest_od_t
));
4991 * Commit callback data.
4993 typedef struct ztest_cb_data
{
4994 list_node_t zcd_node
;
4996 int zcd_expected_err
;
4997 boolean_t zcd_added
;
4998 boolean_t zcd_called
;
5002 /* This is the actual commit callback function */
5004 ztest_commit_callback(void *arg
, int error
)
5006 ztest_cb_data_t
*data
= arg
;
5007 uint64_t synced_txg
;
5009 VERIFY(data
!= NULL
);
5010 VERIFY3S(data
->zcd_expected_err
, ==, error
);
5011 VERIFY(!data
->zcd_called
);
5013 synced_txg
= spa_last_synced_txg(data
->zcd_spa
);
5014 if (data
->zcd_txg
> synced_txg
)
5015 fatal(0, "commit callback of txg %" PRIu64
" called prematurely"
5016 ", last synced txg = %" PRIu64
"\n", data
->zcd_txg
,
5019 data
->zcd_called
= B_TRUE
;
5021 if (error
== ECANCELED
) {
5022 ASSERT0(data
->zcd_txg
);
5023 ASSERT(!data
->zcd_added
);
5026 * The private callback data should be destroyed here, but
5027 * since we are going to check the zcd_called field after
5028 * dmu_tx_abort(), we will destroy it there.
5033 ASSERT(data
->zcd_added
);
5034 ASSERT3U(data
->zcd_txg
, !=, 0);
5036 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5038 /* See if this cb was called more quickly */
5039 if ((synced_txg
- data
->zcd_txg
) < zc_min_txg_delay
)
5040 zc_min_txg_delay
= synced_txg
- data
->zcd_txg
;
5042 /* Remove our callback from the list */
5043 list_remove(&zcl
.zcl_callbacks
, data
);
5045 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5047 umem_free(data
, sizeof (ztest_cb_data_t
));
5050 /* Allocate and initialize callback data structure */
5051 static ztest_cb_data_t
*
5052 ztest_create_cb_data(objset_t
*os
, uint64_t txg
)
5054 ztest_cb_data_t
*cb_data
;
5056 cb_data
= umem_zalloc(sizeof (ztest_cb_data_t
), UMEM_NOFAIL
);
5058 cb_data
->zcd_txg
= txg
;
5059 cb_data
->zcd_spa
= dmu_objset_spa(os
);
5060 list_link_init(&cb_data
->zcd_node
);
5066 * Commit callback test.
5069 ztest_dmu_commit_callbacks(ztest_ds_t
*zd
, uint64_t id
)
5071 objset_t
*os
= zd
->zd_os
;
5074 ztest_cb_data_t
*cb_data
[3], *tmp_cb
;
5075 uint64_t old_txg
, txg
;
5078 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5079 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
5081 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5082 umem_free(od
, sizeof (ztest_od_t
));
5086 tx
= dmu_tx_create(os
);
5088 cb_data
[0] = ztest_create_cb_data(os
, 0);
5089 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[0]);
5091 dmu_tx_hold_write(tx
, od
->od_object
, 0, sizeof (uint64_t));
5093 /* Every once in a while, abort the transaction on purpose */
5094 if (ztest_random(100) == 0)
5098 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
5100 txg
= error
? 0 : dmu_tx_get_txg(tx
);
5102 cb_data
[0]->zcd_txg
= txg
;
5103 cb_data
[1] = ztest_create_cb_data(os
, txg
);
5104 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[1]);
5108 * It's not a strict requirement to call the registered
5109 * callbacks from inside dmu_tx_abort(), but that's what
5110 * it's supposed to happen in the current implementation
5111 * so we will check for that.
5113 for (i
= 0; i
< 2; i
++) {
5114 cb_data
[i
]->zcd_expected_err
= ECANCELED
;
5115 VERIFY(!cb_data
[i
]->zcd_called
);
5120 for (i
= 0; i
< 2; i
++) {
5121 VERIFY(cb_data
[i
]->zcd_called
);
5122 umem_free(cb_data
[i
], sizeof (ztest_cb_data_t
));
5125 umem_free(od
, sizeof (ztest_od_t
));
5129 cb_data
[2] = ztest_create_cb_data(os
, txg
);
5130 dmu_tx_callback_register(tx
, ztest_commit_callback
, cb_data
[2]);
5133 * Read existing data to make sure there isn't a future leak.
5135 VERIFY(0 == dmu_read(os
, od
->od_object
, 0, sizeof (uint64_t),
5136 &old_txg
, DMU_READ_PREFETCH
));
5139 fatal(0, "future leak: got %" PRIu64
", open txg is %" PRIu64
,
5142 dmu_write(os
, od
->od_object
, 0, sizeof (uint64_t), &txg
, tx
);
5144 (void) mutex_enter(&zcl
.zcl_callbacks_lock
);
5147 * Since commit callbacks don't have any ordering requirement and since
5148 * it is theoretically possible for a commit callback to be called
5149 * after an arbitrary amount of time has elapsed since its txg has been
5150 * synced, it is difficult to reliably determine whether a commit
5151 * callback hasn't been called due to high load or due to a flawed
5154 * In practice, we will assume that if after a certain number of txgs a
5155 * commit callback hasn't been called, then most likely there's an
5156 * implementation bug..
5158 tmp_cb
= list_head(&zcl
.zcl_callbacks
);
5159 if (tmp_cb
!= NULL
&&
5160 tmp_cb
->zcd_txg
+ ZTEST_COMMIT_CB_THRESH
< txg
) {
5161 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5162 PRIu64
", open txg: %" PRIu64
"\n", tmp_cb
->zcd_txg
, txg
);
5166 * Let's find the place to insert our callbacks.
5168 * Even though the list is ordered by txg, it is possible for the
5169 * insertion point to not be the end because our txg may already be
5170 * quiescing at this point and other callbacks in the open txg
5171 * (from other objsets) may have sneaked in.
5173 tmp_cb
= list_tail(&zcl
.zcl_callbacks
);
5174 while (tmp_cb
!= NULL
&& tmp_cb
->zcd_txg
> txg
)
5175 tmp_cb
= list_prev(&zcl
.zcl_callbacks
, tmp_cb
);
5177 /* Add the 3 callbacks to the list */
5178 for (i
= 0; i
< 3; i
++) {
5180 list_insert_head(&zcl
.zcl_callbacks
, cb_data
[i
]);
5182 list_insert_after(&zcl
.zcl_callbacks
, tmp_cb
,
5185 cb_data
[i
]->zcd_added
= B_TRUE
;
5186 VERIFY(!cb_data
[i
]->zcd_called
);
5188 tmp_cb
= cb_data
[i
];
5193 (void) mutex_exit(&zcl
.zcl_callbacks_lock
);
5197 umem_free(od
, sizeof (ztest_od_t
));
5201 * Visit each object in the dataset. Verify that its properties
5202 * are consistent what was stored in the block tag when it was created,
5203 * and that its unused bonus buffer space has not been overwritten.
5207 ztest_verify_dnode_bt(ztest_ds_t
*zd
, uint64_t id
)
5209 objset_t
*os
= zd
->zd_os
;
5213 for (obj
= 0; err
== 0; err
= dmu_object_next(os
, &obj
, FALSE
, 0)) {
5214 ztest_block_tag_t
*bt
= NULL
;
5215 dmu_object_info_t doi
;
5218 ztest_object_lock(zd
, obj
, RL_READER
);
5219 if (dmu_bonus_hold(os
, obj
, FTAG
, &db
) != 0) {
5220 ztest_object_unlock(zd
, obj
);
5224 dmu_object_info_from_db(db
, &doi
);
5225 if (doi
.doi_bonus_size
>= sizeof (*bt
))
5226 bt
= ztest_bt_bonus(db
);
5228 if (bt
&& bt
->bt_magic
== BT_MAGIC
) {
5229 ztest_bt_verify(bt
, os
, obj
, doi
.doi_dnodesize
,
5230 bt
->bt_offset
, bt
->bt_gen
, bt
->bt_txg
,
5232 ztest_verify_unused_bonus(db
, bt
, obj
, os
, bt
->bt_gen
);
5235 dmu_buf_rele(db
, FTAG
);
5236 ztest_object_unlock(zd
, obj
);
5242 ztest_dsl_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5244 zfs_prop_t proplist
[] = {
5246 ZFS_PROP_COMPRESSION
,
5252 (void) rw_rdlock(&ztest_name_lock
);
5254 for (p
= 0; p
< sizeof (proplist
) / sizeof (proplist
[0]); p
++)
5255 (void) ztest_dsl_prop_set_uint64(zd
->zd_name
, proplist
[p
],
5256 ztest_random_dsl_prop(proplist
[p
]), (int)ztest_random(2));
5258 VERIFY0(ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_RECORDSIZE
,
5259 ztest_random_blocksize(), (int)ztest_random(2)));
5261 (void) rw_unlock(&ztest_name_lock
);
5266 ztest_spa_prop_get_set(ztest_ds_t
*zd
, uint64_t id
)
5268 nvlist_t
*props
= NULL
;
5270 (void) rw_rdlock(&ztest_name_lock
);
5272 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO
,
5273 ZIO_DEDUPDITTO_MIN
+ ztest_random(ZIO_DEDUPDITTO_MIN
));
5275 VERIFY0(spa_prop_get(ztest_spa
, &props
));
5277 if (ztest_opts
.zo_verbose
>= 6)
5278 dump_nvlist(props
, 4);
5282 (void) rw_unlock(&ztest_name_lock
);
5286 user_release_one(const char *snapname
, const char *holdname
)
5288 nvlist_t
*snaps
, *holds
;
5291 snaps
= fnvlist_alloc();
5292 holds
= fnvlist_alloc();
5293 fnvlist_add_boolean(holds
, holdname
);
5294 fnvlist_add_nvlist(snaps
, snapname
, holds
);
5295 fnvlist_free(holds
);
5296 error
= dsl_dataset_user_release(snaps
, NULL
);
5297 fnvlist_free(snaps
);
5302 * Test snapshot hold/release and deferred destroy.
5305 ztest_dmu_snapshot_hold(ztest_ds_t
*zd
, uint64_t id
)
5308 objset_t
*os
= zd
->zd_os
;
5312 char clonename
[100];
5314 char osname
[ZFS_MAX_DATASET_NAME_LEN
];
5317 (void) rw_rdlock(&ztest_name_lock
);
5319 dmu_objset_name(os
, osname
);
5321 (void) snprintf(snapname
, sizeof (snapname
), "sh1_%llu",
5323 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", osname
, snapname
);
5324 (void) snprintf(clonename
, sizeof (clonename
),
5325 "%s/ch1_%llu", osname
, (u_longlong_t
)id
);
5326 (void) snprintf(tag
, sizeof (tag
), "tag_%llu", (u_longlong_t
)id
);
5329 * Clean up from any previous run.
5331 error
= dsl_destroy_head(clonename
);
5332 if (error
!= ENOENT
)
5334 error
= user_release_one(fullname
, tag
);
5335 if (error
!= ESRCH
&& error
!= ENOENT
)
5337 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5338 if (error
!= ENOENT
)
5342 * Create snapshot, clone it, mark snap for deferred destroy,
5343 * destroy clone, verify snap was also destroyed.
5345 error
= dmu_objset_snapshot_one(osname
, snapname
);
5347 if (error
== ENOSPC
) {
5348 ztest_record_enospc("dmu_objset_snapshot");
5351 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5354 error
= dmu_objset_clone(clonename
, fullname
);
5356 if (error
== ENOSPC
) {
5357 ztest_record_enospc("dmu_objset_clone");
5360 fatal(0, "dmu_objset_clone(%s) = %d", clonename
, error
);
5363 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5365 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5369 error
= dsl_destroy_head(clonename
);
5371 fatal(0, "dsl_destroy_head(%s) = %d", clonename
, error
);
5373 error
= dmu_objset_hold(fullname
, FTAG
, &origin
);
5374 if (error
!= ENOENT
)
5375 fatal(0, "dmu_objset_hold(%s) = %d", fullname
, error
);
5378 * Create snapshot, add temporary hold, verify that we can't
5379 * destroy a held snapshot, mark for deferred destroy,
5380 * release hold, verify snapshot was destroyed.
5382 error
= dmu_objset_snapshot_one(osname
, snapname
);
5384 if (error
== ENOSPC
) {
5385 ztest_record_enospc("dmu_objset_snapshot");
5388 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname
, error
);
5391 holds
= fnvlist_alloc();
5392 fnvlist_add_string(holds
, fullname
, tag
);
5393 error
= dsl_dataset_user_hold(holds
, 0, NULL
);
5394 fnvlist_free(holds
);
5396 if (error
== ENOSPC
) {
5397 ztest_record_enospc("dsl_dataset_user_hold");
5400 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5401 fullname
, tag
, error
);
5404 error
= dsl_destroy_snapshot(fullname
, B_FALSE
);
5405 if (error
!= EBUSY
) {
5406 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5410 error
= dsl_destroy_snapshot(fullname
, B_TRUE
);
5412 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5416 error
= user_release_one(fullname
, tag
);
5418 fatal(0, "user_release_one(%s, %s) = %d", fullname
, tag
, error
);
5420 VERIFY3U(dmu_objset_hold(fullname
, FTAG
, &origin
), ==, ENOENT
);
5423 (void) rw_unlock(&ztest_name_lock
);
5427 * Inject random faults into the on-disk data.
5431 ztest_fault_inject(ztest_ds_t
*zd
, uint64_t id
)
5433 ztest_shared_t
*zs
= ztest_shared
;
5434 spa_t
*spa
= ztest_spa
;
5438 uint64_t bad
= 0x1990c0ffeedecadeull
;
5443 int bshift
= SPA_MAXBLOCKSHIFT
+ 2;
5449 boolean_t islog
= B_FALSE
;
5451 path0
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5452 pathrand
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
5454 mutex_enter(&ztest_vdev_lock
);
5455 maxfaults
= MAXFAULTS(zs
);
5456 leaves
= MAX(zs
->zs_mirrors
, 1) * ztest_opts
.zo_raidz
;
5457 mirror_save
= zs
->zs_mirrors
;
5458 mutex_exit(&ztest_vdev_lock
);
5460 ASSERT(leaves
>= 1);
5463 * Grab the name lock as reader. There are some operations
5464 * which don't like to have their vdevs changed while
5465 * they are in progress (i.e. spa_change_guid). Those
5466 * operations will have grabbed the name lock as writer.
5468 (void) rw_rdlock(&ztest_name_lock
);
5471 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5473 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5475 if (ztest_random(2) == 0) {
5477 * Inject errors on a normal data device or slog device.
5479 top
= ztest_random_vdev_top(spa
, B_TRUE
);
5480 leaf
= ztest_random(leaves
) + zs
->zs_splits
;
5483 * Generate paths to the first leaf in this top-level vdev,
5484 * and to the random leaf we selected. We'll induce transient
5485 * write failures and random online/offline activity on leaf 0,
5486 * and we'll write random garbage to the randomly chosen leaf.
5488 (void) snprintf(path0
, MAXPATHLEN
, ztest_dev_template
,
5489 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5490 top
* leaves
+ zs
->zs_splits
);
5491 (void) snprintf(pathrand
, MAXPATHLEN
, ztest_dev_template
,
5492 ztest_opts
.zo_dir
, ztest_opts
.zo_pool
,
5493 top
* leaves
+ leaf
);
5495 vd0
= vdev_lookup_by_path(spa
->spa_root_vdev
, path0
);
5496 if (vd0
!= NULL
&& vd0
->vdev_top
->vdev_islog
)
5500 * If the top-level vdev needs to be resilvered
5501 * then we only allow faults on the device that is
5504 if (vd0
!= NULL
&& maxfaults
!= 1 &&
5505 (!vdev_resilver_needed(vd0
->vdev_top
, NULL
, NULL
) ||
5506 vd0
->vdev_resilver_txg
!= 0)) {
5508 * Make vd0 explicitly claim to be unreadable,
5509 * or unwriteable, or reach behind its back
5510 * and close the underlying fd. We can do this if
5511 * maxfaults == 0 because we'll fail and reexecute,
5512 * and we can do it if maxfaults >= 2 because we'll
5513 * have enough redundancy. If maxfaults == 1, the
5514 * combination of this with injection of random data
5515 * corruption below exceeds the pool's fault tolerance.
5517 vdev_file_t
*vf
= vd0
->vdev_tsd
;
5519 if (vf
!= NULL
&& ztest_random(3) == 0) {
5520 (void) close(vf
->vf_vnode
->v_fd
);
5521 vf
->vf_vnode
->v_fd
= -1;
5522 } else if (ztest_random(2) == 0) {
5523 vd0
->vdev_cant_read
= B_TRUE
;
5525 vd0
->vdev_cant_write
= B_TRUE
;
5527 guid0
= vd0
->vdev_guid
;
5531 * Inject errors on an l2cache device.
5533 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5535 if (sav
->sav_count
== 0) {
5536 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5537 (void) rw_unlock(&ztest_name_lock
);
5540 vd0
= sav
->sav_vdevs
[ztest_random(sav
->sav_count
)];
5541 guid0
= vd0
->vdev_guid
;
5542 (void) strcpy(path0
, vd0
->vdev_path
);
5543 (void) strcpy(pathrand
, vd0
->vdev_path
);
5547 maxfaults
= INT_MAX
; /* no limit on cache devices */
5550 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5551 (void) rw_unlock(&ztest_name_lock
);
5554 * If we can tolerate two or more faults, or we're dealing
5555 * with a slog, randomly online/offline vd0.
5557 if ((maxfaults
>= 2 || islog
) && guid0
!= 0) {
5558 if (ztest_random(10) < 6) {
5559 int flags
= (ztest_random(2) == 0 ?
5560 ZFS_OFFLINE_TEMPORARY
: 0);
5563 * We have to grab the zs_name_lock as writer to
5564 * prevent a race between offlining a slog and
5565 * destroying a dataset. Offlining the slog will
5566 * grab a reference on the dataset which may cause
5567 * dsl_destroy_head() to fail with EBUSY thus
5568 * leaving the dataset in an inconsistent state.
5571 (void) rw_wrlock(&ztest_name_lock
);
5573 VERIFY(vdev_offline(spa
, guid0
, flags
) != EBUSY
);
5576 (void) rw_unlock(&ztest_name_lock
);
5579 * Ideally we would like to be able to randomly
5580 * call vdev_[on|off]line without holding locks
5581 * to force unpredictable failures but the side
5582 * effects of vdev_[on|off]line prevent us from
5583 * doing so. We grab the ztest_vdev_lock here to
5584 * prevent a race between injection testing and
5587 mutex_enter(&ztest_vdev_lock
);
5588 (void) vdev_online(spa
, guid0
, 0, NULL
);
5589 mutex_exit(&ztest_vdev_lock
);
5597 * We have at least single-fault tolerance, so inject data corruption.
5599 fd
= open(pathrand
, O_RDWR
);
5601 if (fd
== -1) /* we hit a gap in the device namespace */
5604 fsize
= lseek(fd
, 0, SEEK_END
);
5606 while (--iters
!= 0) {
5608 * The offset must be chosen carefully to ensure that
5609 * we do not inject a given logical block with errors
5610 * on two different leaf devices, because ZFS can not
5611 * tolerate that (if maxfaults==1).
5613 * We divide each leaf into chunks of size
5614 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5615 * there is a series of ranges to which we can inject errors.
5616 * Each range can accept errors on only a single leaf vdev.
5617 * The error injection ranges are separated by ranges
5618 * which we will not inject errors on any device (DMZs).
5619 * Each DMZ must be large enough such that a single block
5620 * can not straddle it, so that a single block can not be
5621 * a target in two different injection ranges (on different
5624 * For example, with 3 leaves, each chunk looks like:
5625 * 0 to 32M: injection range for leaf 0
5626 * 32M to 64M: DMZ - no injection allowed
5627 * 64M to 96M: injection range for leaf 1
5628 * 96M to 128M: DMZ - no injection allowed
5629 * 128M to 160M: injection range for leaf 2
5630 * 160M to 192M: DMZ - no injection allowed
5632 offset
= ztest_random(fsize
/ (leaves
<< bshift
)) *
5633 (leaves
<< bshift
) + (leaf
<< bshift
) +
5634 (ztest_random(1ULL << (bshift
- 1)) & -8ULL);
5637 * Only allow damage to the labels at one end of the vdev.
5639 * If all labels are damaged, the device will be totally
5640 * inaccessible, which will result in loss of data,
5641 * because we also damage (parts of) the other side of
5644 * Additionally, we will always have both an even and an
5645 * odd label, so that we can handle crashes in the
5646 * middle of vdev_config_sync().
5648 if ((leaf
& 1) == 0 && offset
< VDEV_LABEL_START_SIZE
)
5652 * The two end labels are stored at the "end" of the disk, but
5653 * the end of the disk (vdev_psize) is aligned to
5654 * sizeof (vdev_label_t).
5656 uint64_t psize
= P2ALIGN(fsize
, sizeof (vdev_label_t
));
5657 if ((leaf
& 1) == 1 &&
5658 offset
+ sizeof (bad
) > psize
- VDEV_LABEL_END_SIZE
)
5661 mutex_enter(&ztest_vdev_lock
);
5662 if (mirror_save
!= zs
->zs_mirrors
) {
5663 mutex_exit(&ztest_vdev_lock
);
5668 if (pwrite(fd
, &bad
, sizeof (bad
), offset
) != sizeof (bad
))
5669 fatal(1, "can't inject bad word at 0x%llx in %s",
5672 mutex_exit(&ztest_vdev_lock
);
5674 if (ztest_opts
.zo_verbose
>= 7)
5675 (void) printf("injected bad word into %s,"
5676 " offset 0x%llx\n", pathrand
, (u_longlong_t
)offset
);
5681 umem_free(path0
, MAXPATHLEN
);
5682 umem_free(pathrand
, MAXPATHLEN
);
5686 * Verify that DDT repair works as expected.
5689 ztest_ddt_repair(ztest_ds_t
*zd
, uint64_t id
)
5691 ztest_shared_t
*zs
= ztest_shared
;
5692 spa_t
*spa
= ztest_spa
;
5693 objset_t
*os
= zd
->zd_os
;
5695 uint64_t object
, blocksize
, txg
, pattern
, psize
;
5696 enum zio_checksum checksum
= spa_dedup_checksum(spa
);
5701 int copies
= 2 * ZIO_DEDUPDITTO_MIN
;
5704 blocksize
= ztest_random_blocksize();
5705 blocksize
= MIN(blocksize
, 2048); /* because we write so many */
5707 od
= umem_alloc(sizeof (ztest_od_t
), UMEM_NOFAIL
);
5708 ztest_od_init(od
, id
, FTAG
, 0, DMU_OT_UINT64_OTHER
, blocksize
, 0, 0);
5710 if (ztest_object_init(zd
, od
, sizeof (ztest_od_t
), B_FALSE
) != 0) {
5711 umem_free(od
, sizeof (ztest_od_t
));
5716 * Take the name lock as writer to prevent anyone else from changing
5717 * the pool and dataset properies we need to maintain during this test.
5719 (void) rw_wrlock(&ztest_name_lock
);
5721 if (ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_DEDUP
, checksum
,
5723 ztest_dsl_prop_set_uint64(zd
->zd_name
, ZFS_PROP_COPIES
, 1,
5725 (void) rw_unlock(&ztest_name_lock
);
5726 umem_free(od
, sizeof (ztest_od_t
));
5730 dmu_objset_stats_t dds
;
5731 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
5732 dmu_objset_fast_stat(os
, &dds
);
5733 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
5735 object
= od
[0].od_object
;
5736 blocksize
= od
[0].od_blocksize
;
5737 pattern
= zs
->zs_guid
^ dds
.dds_guid
;
5739 ASSERT(object
!= 0);
5741 tx
= dmu_tx_create(os
);
5742 dmu_tx_hold_write(tx
, object
, 0, copies
* blocksize
);
5743 txg
= ztest_tx_assign(tx
, TXG_WAIT
, FTAG
);
5745 (void) rw_unlock(&ztest_name_lock
);
5746 umem_free(od
, sizeof (ztest_od_t
));
5751 * Write all the copies of our block.
5753 for (i
= 0; i
< copies
; i
++) {
5754 uint64_t offset
= i
* blocksize
;
5755 int error
= dmu_buf_hold(os
, object
, offset
, FTAG
, &db
,
5756 DMU_READ_NO_PREFETCH
);
5758 fatal(B_FALSE
, "dmu_buf_hold(%p, %llu, %llu) = %u",
5759 os
, (long long)object
, (long long) offset
, error
);
5761 ASSERT(db
->db_offset
== offset
);
5762 ASSERT(db
->db_size
== blocksize
);
5763 ASSERT(ztest_pattern_match(db
->db_data
, db
->db_size
, pattern
) ||
5764 ztest_pattern_match(db
->db_data
, db
->db_size
, 0ULL));
5765 dmu_buf_will_fill(db
, tx
);
5766 ztest_pattern_set(db
->db_data
, db
->db_size
, pattern
);
5767 dmu_buf_rele(db
, FTAG
);
5771 txg_wait_synced(spa_get_dsl(spa
), txg
);
5774 * Find out what block we got.
5776 VERIFY0(dmu_buf_hold(os
, object
, 0, FTAG
, &db
,
5777 DMU_READ_NO_PREFETCH
));
5778 blk
= *((dmu_buf_impl_t
*)db
)->db_blkptr
;
5779 dmu_buf_rele(db
, FTAG
);
5782 * Damage the block. Dedup-ditto will save us when we read it later.
5784 psize
= BP_GET_PSIZE(&blk
);
5785 abd
= abd_alloc_linear(psize
, B_TRUE
);
5786 ztest_pattern_set(abd_to_buf(abd
), psize
, ~pattern
);
5788 (void) zio_wait(zio_rewrite(NULL
, spa
, 0, &blk
,
5789 abd
, psize
, NULL
, NULL
, ZIO_PRIORITY_SYNC_WRITE
,
5790 ZIO_FLAG_CANFAIL
| ZIO_FLAG_INDUCE_DAMAGE
, NULL
));
5794 (void) rw_unlock(&ztest_name_lock
);
5795 umem_free(od
, sizeof (ztest_od_t
));
5803 ztest_scrub(ztest_ds_t
*zd
, uint64_t id
)
5805 spa_t
*spa
= ztest_spa
;
5807 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5808 (void) poll(NULL
, 0, 100); /* wait a moment, then force a restart */
5809 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
5813 * Change the guid for the pool.
5817 ztest_reguid(ztest_ds_t
*zd
, uint64_t id
)
5819 spa_t
*spa
= ztest_spa
;
5820 uint64_t orig
, load
;
5823 if (ztest_opts
.zo_mmp_test
)
5826 orig
= spa_guid(spa
);
5827 load
= spa_load_guid(spa
);
5829 (void) rw_wrlock(&ztest_name_lock
);
5830 error
= spa_change_guid(spa
);
5831 (void) rw_unlock(&ztest_name_lock
);
5836 if (ztest_opts
.zo_verbose
>= 4) {
5837 (void) printf("Changed guid old %llu -> %llu\n",
5838 (u_longlong_t
)orig
, (u_longlong_t
)spa_guid(spa
));
5841 VERIFY3U(orig
, !=, spa_guid(spa
));
5842 VERIFY3U(load
, ==, spa_load_guid(spa
));
5846 * Rename the pool to a different name and then rename it back.
5850 ztest_spa_rename(ztest_ds_t
*zd
, uint64_t id
)
5852 char *oldname
, *newname
;
5855 if (ztest_opts
.zo_mmp_test
)
5858 (void) rw_wrlock(&ztest_name_lock
);
5860 oldname
= ztest_opts
.zo_pool
;
5861 newname
= umem_alloc(strlen(oldname
) + 5, UMEM_NOFAIL
);
5862 (void) strcpy(newname
, oldname
);
5863 (void) strcat(newname
, "_tmp");
5868 VERIFY3U(0, ==, spa_rename(oldname
, newname
));
5871 * Try to open it under the old name, which shouldn't exist
5873 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
5876 * Open it under the new name and make sure it's still the same spa_t.
5878 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
5880 ASSERT(spa
== ztest_spa
);
5881 spa_close(spa
, FTAG
);
5884 * Rename it back to the original
5886 VERIFY3U(0, ==, spa_rename(newname
, oldname
));
5889 * Make sure it can still be opened
5891 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
5893 ASSERT(spa
== ztest_spa
);
5894 spa_close(spa
, FTAG
);
5896 umem_free(newname
, strlen(newname
) + 1);
5898 (void) rw_unlock(&ztest_name_lock
);
5902 ztest_fletcher(ztest_ds_t
*zd
, uint64_t id
)
5904 hrtime_t end
= gethrtime() + NANOSEC
;
5906 while (gethrtime() <= end
) {
5907 int run_count
= 100;
5909 struct abd
*abd_data
, *abd_meta
;
5914 zio_cksum_t zc_ref_byteswap
;
5916 size
= ztest_random_blocksize();
5918 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5919 abd_data
= abd_alloc(size
, B_FALSE
);
5920 abd_meta
= abd_alloc(size
, B_TRUE
);
5922 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5923 *ptr
= ztest_random(UINT_MAX
);
5925 abd_copy_from_buf_off(abd_data
, buf
, 0, size
);
5926 abd_copy_from_buf_off(abd_meta
, buf
, 0, size
);
5928 VERIFY0(fletcher_4_impl_set("scalar"));
5929 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5930 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_byteswap
);
5932 VERIFY0(fletcher_4_impl_set("cycle"));
5933 while (run_count
-- > 0) {
5935 zio_cksum_t zc_byteswap
;
5937 fletcher_4_byteswap(buf
, size
, NULL
, &zc_byteswap
);
5938 fletcher_4_native(buf
, size
, NULL
, &zc
);
5940 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5941 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5942 sizeof (zc_byteswap
)));
5944 /* Test ABD - data */
5945 abd_fletcher_4_byteswap(abd_data
, size
, NULL
,
5947 abd_fletcher_4_native(abd_data
, size
, NULL
, &zc
);
5949 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5950 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5951 sizeof (zc_byteswap
)));
5953 /* Test ABD - metadata */
5954 abd_fletcher_4_byteswap(abd_meta
, size
, NULL
,
5956 abd_fletcher_4_native(abd_meta
, size
, NULL
, &zc
);
5958 VERIFY0(bcmp(&zc
, &zc_ref
, sizeof (zc
)));
5959 VERIFY0(bcmp(&zc_byteswap
, &zc_ref_byteswap
,
5960 sizeof (zc_byteswap
)));
5964 umem_free(buf
, size
);
5971 ztest_fletcher_incr(ztest_ds_t
*zd
, uint64_t id
)
5978 zio_cksum_t zc_ref_bswap
;
5980 hrtime_t end
= gethrtime() + NANOSEC
;
5982 while (gethrtime() <= end
) {
5983 int run_count
= 100;
5985 size
= ztest_random_blocksize();
5986 buf
= umem_alloc(size
, UMEM_NOFAIL
);
5988 for (i
= 0, ptr
= buf
; i
< size
/ sizeof (*ptr
); i
++, ptr
++)
5989 *ptr
= ztest_random(UINT_MAX
);
5991 VERIFY0(fletcher_4_impl_set("scalar"));
5992 fletcher_4_native(buf
, size
, NULL
, &zc_ref
);
5993 fletcher_4_byteswap(buf
, size
, NULL
, &zc_ref_bswap
);
5995 VERIFY0(fletcher_4_impl_set("cycle"));
5997 while (run_count
-- > 0) {
5999 zio_cksum_t zc_bswap
;
6002 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6003 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6005 while (pos
< size
) {
6006 size_t inc
= 64 * ztest_random(size
/ 67);
6007 /* sometimes add few bytes to test non-simd */
6008 if (ztest_random(100) < 10)
6009 inc
+= P2ALIGN(ztest_random(64),
6012 if (inc
> (size
- pos
))
6015 fletcher_4_incremental_native(buf
+ pos
, inc
,
6017 fletcher_4_incremental_byteswap(buf
+ pos
, inc
,
6023 VERIFY3U(pos
, ==, size
);
6025 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6026 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6029 * verify if incremental on the whole buffer is
6030 * equivalent to non-incremental version
6032 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
6033 ZIO_SET_CHECKSUM(&zc_bswap
, 0, 0, 0, 0);
6035 fletcher_4_incremental_native(buf
, size
, &zc
);
6036 fletcher_4_incremental_byteswap(buf
, size
, &zc_bswap
);
6038 VERIFY(ZIO_CHECKSUM_EQUAL(zc
, zc_ref
));
6039 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap
, zc_ref_bswap
));
6042 umem_free(buf
, size
);
6047 ztest_check_path(char *path
)
6050 /* return true on success */
6051 return (!stat(path
, &s
));
6055 ztest_get_zdb_bin(char *bin
, int len
)
6059 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6060 * let popen to search through PATH.
6062 if ((zdb_path
= getenv("ZDB_PATH"))) {
6063 strlcpy(bin
, zdb_path
, len
); /* In env */
6064 if (!ztest_check_path(bin
)) {
6065 ztest_dump_core
= 0;
6066 fatal(1, "invalid ZDB_PATH '%s'", bin
);
6071 VERIFY(realpath(getexecname(), bin
) != NULL
);
6072 if (strstr(bin
, "/ztest/")) {
6073 strstr(bin
, "/ztest/")[0] = '\0'; /* In-tree */
6074 strcat(bin
, "/zdb/zdb");
6075 if (ztest_check_path(bin
))
6082 * Verify pool integrity by running zdb.
6085 ztest_run_zdb(char *pool
)
6091 const int len
= MAXPATHLEN
+ MAXNAMELEN
+ 20;
6094 bin
= umem_alloc(len
, UMEM_NOFAIL
);
6095 zdb
= umem_alloc(len
, UMEM_NOFAIL
);
6096 zbuf
= umem_alloc(1024, UMEM_NOFAIL
);
6098 ztest_get_zdb_bin(bin
, len
);
6101 "%s -bcc%s%s -G -d -U %s %s",
6103 ztest_opts
.zo_verbose
>= 3 ? "s" : "",
6104 ztest_opts
.zo_verbose
>= 4 ? "v" : "",
6108 if (ztest_opts
.zo_verbose
>= 5)
6109 (void) printf("Executing %s\n", strstr(zdb
, "zdb "));
6111 fp
= popen(zdb
, "r");
6113 while (fgets(zbuf
, 1024, fp
) != NULL
)
6114 if (ztest_opts
.zo_verbose
>= 3)
6115 (void) printf("%s", zbuf
);
6117 status
= pclose(fp
);
6122 ztest_dump_core
= 0;
6123 if (WIFEXITED(status
))
6124 fatal(0, "'%s' exit code %d", zdb
, WEXITSTATUS(status
));
6126 fatal(0, "'%s' died with signal %d", zdb
, WTERMSIG(status
));
6128 umem_free(bin
, len
);
6129 umem_free(zdb
, len
);
6130 umem_free(zbuf
, 1024);
6134 ztest_walk_pool_directory(char *header
)
6138 if (ztest_opts
.zo_verbose
>= 6)
6139 (void) printf("%s\n", header
);
6141 mutex_enter(&spa_namespace_lock
);
6142 while ((spa
= spa_next(spa
)) != NULL
)
6143 if (ztest_opts
.zo_verbose
>= 6)
6144 (void) printf("\t%s\n", spa_name(spa
));
6145 mutex_exit(&spa_namespace_lock
);
6149 ztest_spa_import_export(char *oldname
, char *newname
)
6151 nvlist_t
*config
, *newconfig
;
6156 if (ztest_opts
.zo_verbose
>= 4) {
6157 (void) printf("import/export: old = %s, new = %s\n",
6162 * Clean up from previous runs.
6164 (void) spa_destroy(newname
);
6167 * Get the pool's configuration and guid.
6169 VERIFY3U(0, ==, spa_open(oldname
, &spa
, FTAG
));
6172 * Kick off a scrub to tickle scrub/export races.
6174 if (ztest_random(2) == 0)
6175 (void) spa_scan(spa
, POOL_SCAN_SCRUB
);
6177 pool_guid
= spa_guid(spa
);
6178 spa_close(spa
, FTAG
);
6180 ztest_walk_pool_directory("pools before export");
6185 VERIFY3U(0, ==, spa_export(oldname
, &config
, B_FALSE
, B_FALSE
));
6187 ztest_walk_pool_directory("pools after export");
6192 newconfig
= spa_tryimport(config
);
6193 ASSERT(newconfig
!= NULL
);
6194 nvlist_free(newconfig
);
6197 * Import it under the new name.
6199 error
= spa_import(newname
, config
, NULL
, 0);
6201 dump_nvlist(config
, 0);
6202 fatal(B_FALSE
, "couldn't import pool %s as %s: error %u",
6203 oldname
, newname
, error
);
6206 ztest_walk_pool_directory("pools after import");
6209 * Try to import it again -- should fail with EEXIST.
6211 VERIFY3U(EEXIST
, ==, spa_import(newname
, config
, NULL
, 0));
6214 * Try to import it under a different name -- should fail with EEXIST.
6216 VERIFY3U(EEXIST
, ==, spa_import(oldname
, config
, NULL
, 0));
6219 * Verify that the pool is no longer visible under the old name.
6221 VERIFY3U(ENOENT
, ==, spa_open(oldname
, &spa
, FTAG
));
6224 * Verify that we can open and close the pool using the new name.
6226 VERIFY3U(0, ==, spa_open(newname
, &spa
, FTAG
));
6227 ASSERT(pool_guid
== spa_guid(spa
));
6228 spa_close(spa
, FTAG
);
6230 nvlist_free(config
);
6234 ztest_resume(spa_t
*spa
)
6236 if (spa_suspended(spa
) && ztest_opts
.zo_verbose
>= 6)
6237 (void) printf("resuming from suspended state\n");
6238 spa_vdev_state_enter(spa
, SCL_NONE
);
6239 vdev_clear(spa
, NULL
);
6240 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6241 (void) zio_resume(spa
);
6245 ztest_resume_thread(void *arg
)
6249 while (!ztest_exiting
) {
6250 if (spa_suspended(spa
))
6252 (void) poll(NULL
, 0, 100);
6255 * Periodically change the zfs_compressed_arc_enabled setting.
6257 if (ztest_random(10) == 0)
6258 zfs_compressed_arc_enabled
= ztest_random(2);
6261 * Periodically change the zfs_abd_scatter_enabled setting.
6263 if (ztest_random(10) == 0)
6264 zfs_abd_scatter_enabled
= ztest_random(2);
6271 ztest_deadman_thread(void *arg
)
6273 ztest_shared_t
*zs
= arg
;
6274 spa_t
*spa
= ztest_spa
;
6275 hrtime_t delta
, overdue
, total
= 0;
6278 delta
= zs
->zs_thread_stop
- zs
->zs_thread_start
+
6279 MSEC2NSEC(zfs_deadman_synctime_ms
);
6281 (void) poll(NULL
, 0, (int)NSEC2MSEC(delta
));
6284 * If the pool is suspended then fail immediately. Otherwise,
6285 * check to see if the pool is making any progress. If
6286 * vdev_deadman() discovers that there hasn't been any recent
6287 * I/Os then it will end up aborting the tests.
6289 if (spa_suspended(spa
) || spa
->spa_root_vdev
== NULL
) {
6290 fatal(0, "aborting test after %llu seconds because "
6291 "pool has transitioned to a suspended state.",
6292 zfs_deadman_synctime_ms
/ 1000);
6294 vdev_deadman(spa
->spa_root_vdev
, FTAG
);
6297 * If the process doesn't complete within a grace period of
6298 * zfs_deadman_synctime_ms over the expected finish time,
6299 * then it may be hung and is terminated.
6301 overdue
= zs
->zs_proc_stop
+ MSEC2NSEC(zfs_deadman_synctime_ms
);
6302 total
+= zfs_deadman_synctime_ms
/ 1000;
6303 if (gethrtime() > overdue
) {
6304 fatal(0, "aborting test after %llu seconds because "
6305 "the process is overdue for termination.", total
);
6308 (void) printf("ztest has been running for %lld seconds\n",
6314 ztest_execute(int test
, ztest_info_t
*zi
, uint64_t id
)
6316 ztest_ds_t
*zd
= &ztest_ds
[id
% ztest_opts
.zo_datasets
];
6317 ztest_shared_callstate_t
*zc
= ZTEST_GET_SHARED_CALLSTATE(test
);
6318 hrtime_t functime
= gethrtime();
6321 for (i
= 0; i
< zi
->zi_iters
; i
++)
6322 zi
->zi_func(zd
, id
);
6324 functime
= gethrtime() - functime
;
6326 atomic_add_64(&zc
->zc_count
, 1);
6327 atomic_add_64(&zc
->zc_time
, functime
);
6329 if (ztest_opts
.zo_verbose
>= 4)
6330 (void) printf("%6.2f sec in %s\n",
6331 (double)functime
/ NANOSEC
, zi
->zi_funcname
);
6335 ztest_thread(void *arg
)
6338 uint64_t id
= (uintptr_t)arg
;
6339 ztest_shared_t
*zs
= ztest_shared
;
6343 ztest_shared_callstate_t
*zc
;
6345 while ((now
= gethrtime()) < zs
->zs_thread_stop
) {
6347 * See if it's time to force a crash.
6349 if (now
> zs
->zs_thread_kill
)
6353 * If we're getting ENOSPC with some regularity, stop.
6355 if (zs
->zs_enospc_count
> 10)
6359 * Pick a random function to execute.
6361 rand
= ztest_random(ZTEST_FUNCS
);
6362 zi
= &ztest_info
[rand
];
6363 zc
= ZTEST_GET_SHARED_CALLSTATE(rand
);
6364 call_next
= zc
->zc_next
;
6366 if (now
>= call_next
&&
6367 atomic_cas_64(&zc
->zc_next
, call_next
, call_next
+
6368 ztest_random(2 * zi
->zi_interval
[0] + 1)) == call_next
) {
6369 ztest_execute(rand
, zi
, id
);
6377 ztest_dataset_name(char *dsname
, char *pool
, int d
)
6379 (void) snprintf(dsname
, ZFS_MAX_DATASET_NAME_LEN
, "%s/ds_%d", pool
, d
);
6383 ztest_dataset_destroy(int d
)
6385 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6388 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6390 if (ztest_opts
.zo_verbose
>= 3)
6391 (void) printf("Destroying %s to free up space\n", name
);
6394 * Cleanup any non-standard clones and snapshots. In general,
6395 * ztest thread t operates on dataset (t % zopt_datasets),
6396 * so there may be more than one thing to clean up.
6398 for (t
= d
; t
< ztest_opts
.zo_threads
;
6399 t
+= ztest_opts
.zo_datasets
)
6400 ztest_dsl_dataset_cleanup(name
, t
);
6402 (void) dmu_objset_find(name
, ztest_objset_destroy_cb
, NULL
,
6403 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
6407 ztest_dataset_dirobj_verify(ztest_ds_t
*zd
)
6409 uint64_t usedobjs
, dirobjs
, scratch
;
6412 * ZTEST_DIROBJ is the object directory for the entire dataset.
6413 * Therefore, the number of objects in use should equal the
6414 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6415 * If not, we have an object leak.
6417 * Note that we can only check this in ztest_dataset_open(),
6418 * when the open-context and syncing-context values agree.
6419 * That's because zap_count() returns the open-context value,
6420 * while dmu_objset_space() returns the rootbp fill count.
6422 VERIFY3U(0, ==, zap_count(zd
->zd_os
, ZTEST_DIROBJ
, &dirobjs
));
6423 dmu_objset_space(zd
->zd_os
, &scratch
, &scratch
, &usedobjs
, &scratch
);
6424 ASSERT3U(dirobjs
+ 1, ==, usedobjs
);
6428 ztest_dataset_open(int d
)
6430 ztest_ds_t
*zd
= &ztest_ds
[d
];
6431 uint64_t committed_seq
= ZTEST_GET_SHARED_DS(d
)->zd_seq
;
6434 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6437 ztest_dataset_name(name
, ztest_opts
.zo_pool
, d
);
6439 (void) rw_rdlock(&ztest_name_lock
);
6441 error
= ztest_dataset_create(name
);
6442 if (error
== ENOSPC
) {
6443 (void) rw_unlock(&ztest_name_lock
);
6444 ztest_record_enospc(FTAG
);
6447 ASSERT(error
== 0 || error
== EEXIST
);
6449 VERIFY0(ztest_dmu_objset_own(name
, DMU_OST_OTHER
, B_FALSE
,
6451 (void) rw_unlock(&ztest_name_lock
);
6453 ztest_zd_init(zd
, ZTEST_GET_SHARED_DS(d
), os
);
6455 zilog
= zd
->zd_zilog
;
6457 if (zilog
->zl_header
->zh_claim_lr_seq
!= 0 &&
6458 zilog
->zl_header
->zh_claim_lr_seq
< committed_seq
)
6459 fatal(0, "missing log records: claimed %llu < committed %llu",
6460 zilog
->zl_header
->zh_claim_lr_seq
, committed_seq
);
6462 ztest_dataset_dirobj_verify(zd
);
6464 zil_replay(os
, zd
, ztest_replay_vector
);
6466 ztest_dataset_dirobj_verify(zd
);
6468 if (ztest_opts
.zo_verbose
>= 6)
6469 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6471 (u_longlong_t
)zilog
->zl_parse_blk_count
,
6472 (u_longlong_t
)zilog
->zl_parse_lr_count
,
6473 (u_longlong_t
)zilog
->zl_replaying_seq
);
6475 zilog
= zil_open(os
, ztest_get_data
);
6477 if (zilog
->zl_replaying_seq
!= 0 &&
6478 zilog
->zl_replaying_seq
< committed_seq
)
6479 fatal(0, "missing log records: replayed %llu < committed %llu",
6480 zilog
->zl_replaying_seq
, committed_seq
);
6486 ztest_dataset_close(int d
)
6488 ztest_ds_t
*zd
= &ztest_ds
[d
];
6490 zil_close(zd
->zd_zilog
);
6491 txg_wait_synced(spa_get_dsl(zd
->zd_os
->os_spa
), 0);
6492 dmu_objset_disown(zd
->zd_os
, B_TRUE
, zd
);
6498 * Kick off threads to run tests on all datasets in parallel.
6501 ztest_run(ztest_shared_t
*zs
)
6505 kthread_t
*resume_thread
;
6506 kthread_t
**run_threads
;
6511 ztest_exiting
= B_FALSE
;
6514 * Initialize parent/child shared state.
6516 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6517 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6519 zs
->zs_thread_start
= gethrtime();
6520 zs
->zs_thread_stop
=
6521 zs
->zs_thread_start
+ ztest_opts
.zo_passtime
* NANOSEC
;
6522 zs
->zs_thread_stop
= MIN(zs
->zs_thread_stop
, zs
->zs_proc_stop
);
6523 zs
->zs_thread_kill
= zs
->zs_thread_stop
;
6524 if (ztest_random(100) < ztest_opts
.zo_killrate
) {
6525 zs
->zs_thread_kill
-=
6526 ztest_random(ztest_opts
.zo_passtime
* NANOSEC
);
6529 mutex_init(&zcl
.zcl_callbacks_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6531 list_create(&zcl
.zcl_callbacks
, sizeof (ztest_cb_data_t
),
6532 offsetof(ztest_cb_data_t
, zcd_node
));
6537 kernel_init(FREAD
| FWRITE
);
6538 VERIFY0(spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6539 spa
->spa_debug
= B_TRUE
;
6540 metaslab_preload_limit
= ztest_random(20) + 1;
6543 dmu_objset_stats_t dds
;
6544 VERIFY0(ztest_dmu_objset_own(ztest_opts
.zo_pool
,
6545 DMU_OST_ANY
, B_TRUE
, B_TRUE
, FTAG
, &os
));
6546 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
6547 dmu_objset_fast_stat(os
, &dds
);
6548 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
6549 zs
->zs_guid
= dds
.dds_guid
;
6550 dmu_objset_disown(os
, B_TRUE
, FTAG
);
6552 spa
->spa_dedup_ditto
= 2 * ZIO_DEDUPDITTO_MIN
;
6555 * Create a thread to periodically resume suspended I/O.
6557 resume_thread
= thread_create(NULL
, 0, ztest_resume_thread
,
6558 spa
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6561 * Create a deadman thread and set to panic if we hang.
6563 (void) thread_create(NULL
, 0, ztest_deadman_thread
,
6564 zs
, 0, NULL
, TS_RUN
| TS_JOINABLE
, defclsyspri
);
6566 spa
->spa_deadman_failmode
= ZIO_FAILURE_MODE_PANIC
;
6569 * Verify that we can safely inquire about about any object,
6570 * whether it's allocated or not. To make it interesting,
6571 * we probe a 5-wide window around each power of two.
6572 * This hits all edge cases, including zero and the max.
6574 for (t
= 0; t
< 64; t
++) {
6575 for (d
= -5; d
<= 5; d
++) {
6576 error
= dmu_object_info(spa
->spa_meta_objset
,
6577 (1ULL << t
) + d
, NULL
);
6578 ASSERT(error
== 0 || error
== ENOENT
||
6584 * If we got any ENOSPC errors on the previous run, destroy something.
6586 if (zs
->zs_enospc_count
!= 0) {
6587 int d
= ztest_random(ztest_opts
.zo_datasets
);
6588 ztest_dataset_destroy(d
);
6590 zs
->zs_enospc_count
= 0;
6592 run_threads
= umem_zalloc(ztest_opts
.zo_threads
* sizeof (kthread_t
*),
6595 if (ztest_opts
.zo_verbose
>= 4)
6596 (void) printf("starting main threads...\n");
6599 * Kick off all the tests that run in parallel.
6601 for (t
= 0; t
< ztest_opts
.zo_threads
; t
++) {
6602 if (t
< ztest_opts
.zo_datasets
&& ztest_dataset_open(t
) != 0) {
6603 umem_free(run_threads
, ztest_opts
.zo_threads
*
6604 sizeof (kthread_t
*));
6608 run_threads
[t
] = thread_create(NULL
, 0, ztest_thread
,
6609 (void *)(uintptr_t)t
, 0, NULL
, TS_RUN
| TS_JOINABLE
,
6614 * Wait for all of the tests to complete. We go in reverse order
6615 * so we don't close datasets while threads are still using them.
6617 for (t
= ztest_opts
.zo_threads
- 1; t
>= 0; t
--) {
6618 VERIFY0(thread_join(run_threads
[t
]));
6619 if (t
< ztest_opts
.zo_datasets
)
6620 ztest_dataset_close(t
);
6623 txg_wait_synced(spa_get_dsl(spa
), 0);
6625 zs
->zs_alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
6626 zs
->zs_space
= metaslab_class_get_space(spa_normal_class(spa
));
6628 umem_free(run_threads
, ztest_opts
.zo_threads
* sizeof (kthread_t
*));
6630 /* Kill the resume thread */
6631 ztest_exiting
= B_TRUE
;
6632 VERIFY0(thread_join(resume_thread
));
6636 * Right before closing the pool, kick off a bunch of async I/O;
6637 * spa_close() should wait for it to complete.
6639 for (object
= 1; object
< 50; object
++) {
6640 dmu_prefetch(spa
->spa_meta_objset
, object
, 0, 0, 1ULL << 20,
6641 ZIO_PRIORITY_SYNC_READ
);
6644 /* Verify that at least one commit cb was called in a timely fashion */
6645 if (zc_cb_counter
>= ZTEST_COMMIT_CB_MIN_REG
)
6646 VERIFY0(zc_min_txg_delay
);
6648 spa_close(spa
, FTAG
);
6651 * Verify that we can loop over all pools.
6653 mutex_enter(&spa_namespace_lock
);
6654 for (spa
= spa_next(NULL
); spa
!= NULL
; spa
= spa_next(spa
))
6655 if (ztest_opts
.zo_verbose
> 3)
6656 (void) printf("spa_next: found %s\n", spa_name(spa
));
6657 mutex_exit(&spa_namespace_lock
);
6660 * Verify that we can export the pool and reimport it under a
6663 if ((ztest_random(2) == 0) && !ztest_opts
.zo_mmp_test
) {
6664 char name
[ZFS_MAX_DATASET_NAME_LEN
];
6665 (void) snprintf(name
, sizeof (name
), "%s_import",
6666 ztest_opts
.zo_pool
);
6667 ztest_spa_import_export(ztest_opts
.zo_pool
, name
);
6668 ztest_spa_import_export(name
, ztest_opts
.zo_pool
);
6673 list_destroy(&zcl
.zcl_callbacks
);
6674 mutex_destroy(&zcl
.zcl_callbacks_lock
);
6675 (void) rwlock_destroy(&ztest_name_lock
);
6676 mutex_destroy(&ztest_vdev_lock
);
6682 ztest_ds_t
*zd
= &ztest_ds
[0];
6686 if (ztest_opts
.zo_verbose
>= 3)
6687 (void) printf("testing spa_freeze()...\n");
6689 kernel_init(FREAD
| FWRITE
);
6690 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6691 VERIFY3U(0, ==, ztest_dataset_open(0));
6692 spa
->spa_debug
= B_TRUE
;
6696 * Force the first log block to be transactionally allocated.
6697 * We have to do this before we freeze the pool -- otherwise
6698 * the log chain won't be anchored.
6700 while (BP_IS_HOLE(&zd
->zd_zilog
->zl_header
->zh_log
)) {
6701 ztest_dmu_object_alloc_free(zd
, 0);
6702 zil_commit(zd
->zd_zilog
, 0);
6705 txg_wait_synced(spa_get_dsl(spa
), 0);
6708 * Freeze the pool. This stops spa_sync() from doing anything,
6709 * so that the only way to record changes from now on is the ZIL.
6714 * Because it is hard to predict how much space a write will actually
6715 * require beforehand, we leave ourselves some fudge space to write over
6718 uint64_t capacity
= metaslab_class_get_space(spa_normal_class(spa
)) / 2;
6721 * Run tests that generate log records but don't alter the pool config
6722 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6723 * We do a txg_wait_synced() after each iteration to force the txg
6724 * to increase well beyond the last synced value in the uberblock.
6725 * The ZIL should be OK with that.
6727 * Run a random number of times less than zo_maxloops and ensure we do
6728 * not run out of space on the pool.
6730 while (ztest_random(10) != 0 &&
6731 numloops
++ < ztest_opts
.zo_maxloops
&&
6732 metaslab_class_get_alloc(spa_normal_class(spa
)) < capacity
) {
6734 ztest_od_init(&od
, 0, FTAG
, 0, DMU_OT_UINT64_OTHER
, 0, 0, 0);
6735 VERIFY0(ztest_object_init(zd
, &od
, sizeof (od
), B_FALSE
));
6736 ztest_io(zd
, od
.od_object
,
6737 ztest_random(ZTEST_RANGE_LOCKS
) << SPA_MAXBLOCKSHIFT
);
6738 txg_wait_synced(spa_get_dsl(spa
), 0);
6742 * Commit all of the changes we just generated.
6744 zil_commit(zd
->zd_zilog
, 0);
6745 txg_wait_synced(spa_get_dsl(spa
), 0);
6748 * Close our dataset and close the pool.
6750 ztest_dataset_close(0);
6751 spa_close(spa
, FTAG
);
6755 * Open and close the pool and dataset to induce log replay.
6757 kernel_init(FREAD
| FWRITE
);
6758 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6759 ASSERT(spa_freeze_txg(spa
) == UINT64_MAX
);
6760 VERIFY3U(0, ==, ztest_dataset_open(0));
6761 spa
->spa_debug
= B_TRUE
;
6763 txg_wait_synced(spa_get_dsl(spa
), 0);
6764 ztest_dataset_close(0);
6765 ztest_reguid(NULL
, 0);
6767 spa_close(spa
, FTAG
);
6772 print_time(hrtime_t t
, char *timebuf
)
6774 hrtime_t s
= t
/ NANOSEC
;
6775 hrtime_t m
= s
/ 60;
6776 hrtime_t h
= m
/ 60;
6777 hrtime_t d
= h
/ 24;
6786 (void) sprintf(timebuf
,
6787 "%llud%02lluh%02llum%02llus", d
, h
, m
, s
);
6789 (void) sprintf(timebuf
, "%lluh%02llum%02llus", h
, m
, s
);
6791 (void) sprintf(timebuf
, "%llum%02llus", m
, s
);
6793 (void) sprintf(timebuf
, "%llus", s
);
6797 make_random_props(void)
6801 VERIFY0(nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
6802 if (ztest_random(2) == 0)
6805 VERIFY0(nvlist_add_uint64(props
,
6806 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE
), 1));
6812 * Import a storage pool with the given name.
6815 ztest_import(ztest_shared_t
*zs
)
6817 libzfs_handle_t
*hdl
;
6818 importargs_t args
= { 0 };
6820 nvlist_t
*cfg
= NULL
;
6822 char *searchdirs
[nsearch
];
6823 char *name
= ztest_opts
.zo_pool
;
6824 int flags
= ZFS_IMPORT_MISSING_LOG
;
6827 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6828 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6830 kernel_init(FREAD
| FWRITE
);
6831 hdl
= libzfs_init();
6833 searchdirs
[0] = ztest_opts
.zo_dir
;
6834 args
.paths
= nsearch
;
6835 args
.path
= searchdirs
;
6836 args
.can_be_active
= B_FALSE
;
6838 error
= zpool_tryimport(hdl
, name
, &cfg
, &args
);
6840 (void) fatal(0, "No pools found\n");
6842 VERIFY0(spa_import(name
, cfg
, NULL
, flags
));
6843 VERIFY0(spa_open(name
, &spa
, FTAG
));
6844 zs
->zs_metaslab_sz
=
6845 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6846 spa_close(spa
, FTAG
);
6851 if (!ztest_opts
.zo_mmp_test
) {
6852 ztest_run_zdb(ztest_opts
.zo_pool
);
6854 ztest_run_zdb(ztest_opts
.zo_pool
);
6857 (void) rwlock_destroy(&ztest_name_lock
);
6858 mutex_destroy(&ztest_vdev_lock
);
6862 * Create a storage pool with the given name and initial vdev size.
6863 * Then test spa_freeze() functionality.
6866 ztest_init(ztest_shared_t
*zs
)
6869 nvlist_t
*nvroot
, *props
;
6872 mutex_init(&ztest_vdev_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6873 VERIFY(rwlock_init(&ztest_name_lock
, USYNC_THREAD
, NULL
) == 0);
6875 kernel_init(FREAD
| FWRITE
);
6878 * Create the storage pool.
6880 (void) spa_destroy(ztest_opts
.zo_pool
);
6881 ztest_shared
->zs_vdev_next_leaf
= 0;
6883 zs
->zs_mirrors
= ztest_opts
.zo_mirrors
;
6884 nvroot
= make_vdev_root(NULL
, NULL
, NULL
, ztest_opts
.zo_vdev_size
, 0,
6885 0, ztest_opts
.zo_raidz
, zs
->zs_mirrors
, 1);
6886 props
= make_random_props();
6889 * We don't expect the pool to suspend unless maxfaults == 0,
6890 * in which case ztest_fault_inject() temporarily takes away
6891 * the only valid replica.
6893 VERIFY0(nvlist_add_uint64(props
,
6894 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE
),
6895 MAXFAULTS(zs
) ? ZIO_FAILURE_MODE_PANIC
: ZIO_FAILURE_MODE_WAIT
));
6897 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6899 VERIFY3S(-1, !=, asprintf(&buf
, "feature@%s",
6900 spa_feature_table
[i
].fi_uname
));
6901 VERIFY3U(0, ==, nvlist_add_uint64(props
, buf
, 0));
6905 VERIFY0(spa_create(ztest_opts
.zo_pool
, nvroot
, props
, NULL
, NULL
));
6906 nvlist_free(nvroot
);
6909 VERIFY3U(0, ==, spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
));
6910 zs
->zs_metaslab_sz
=
6911 1ULL << spa
->spa_root_vdev
->vdev_child
[0]->vdev_ms_shift
;
6912 spa_close(spa
, FTAG
);
6916 if (!ztest_opts
.zo_mmp_test
) {
6917 ztest_run_zdb(ztest_opts
.zo_pool
);
6919 ztest_run_zdb(ztest_opts
.zo_pool
);
6922 (void) rwlock_destroy(&ztest_name_lock
);
6923 mutex_destroy(&ztest_vdev_lock
);
6929 static char ztest_name_data
[] = "/tmp/ztest.data.XXXXXX";
6931 ztest_fd_data
= mkstemp(ztest_name_data
);
6932 ASSERT3S(ztest_fd_data
, >=, 0);
6933 (void) unlink(ztest_name_data
);
6937 shared_data_size(ztest_shared_hdr_t
*hdr
)
6941 size
= hdr
->zh_hdr_size
;
6942 size
+= hdr
->zh_opts_size
;
6943 size
+= hdr
->zh_size
;
6944 size
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
6945 size
+= hdr
->zh_ds_size
* hdr
->zh_ds_count
;
6954 ztest_shared_hdr_t
*hdr
;
6956 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6957 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6958 ASSERT(hdr
!= MAP_FAILED
);
6960 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, sizeof (ztest_shared_hdr_t
)));
6962 hdr
->zh_hdr_size
= sizeof (ztest_shared_hdr_t
);
6963 hdr
->zh_opts_size
= sizeof (ztest_shared_opts_t
);
6964 hdr
->zh_size
= sizeof (ztest_shared_t
);
6965 hdr
->zh_stats_size
= sizeof (ztest_shared_callstate_t
);
6966 hdr
->zh_stats_count
= ZTEST_FUNCS
;
6967 hdr
->zh_ds_size
= sizeof (ztest_shared_ds_t
);
6968 hdr
->zh_ds_count
= ztest_opts
.zo_datasets
;
6970 size
= shared_data_size(hdr
);
6971 VERIFY3U(0, ==, ftruncate(ztest_fd_data
, size
));
6973 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6980 ztest_shared_hdr_t
*hdr
;
6983 hdr
= (void *)mmap(0, P2ROUNDUP(sizeof (*hdr
), getpagesize()),
6984 PROT_READ
, MAP_SHARED
, ztest_fd_data
, 0);
6985 ASSERT(hdr
!= MAP_FAILED
);
6987 size
= shared_data_size(hdr
);
6989 (void) munmap((caddr_t
)hdr
, P2ROUNDUP(sizeof (*hdr
), getpagesize()));
6990 hdr
= ztest_shared_hdr
= (void *)mmap(0, P2ROUNDUP(size
, getpagesize()),
6991 PROT_READ
| PROT_WRITE
, MAP_SHARED
, ztest_fd_data
, 0);
6992 ASSERT(hdr
!= MAP_FAILED
);
6993 buf
= (uint8_t *)hdr
;
6995 offset
= hdr
->zh_hdr_size
;
6996 ztest_shared_opts
= (void *)&buf
[offset
];
6997 offset
+= hdr
->zh_opts_size
;
6998 ztest_shared
= (void *)&buf
[offset
];
6999 offset
+= hdr
->zh_size
;
7000 ztest_shared_callstate
= (void *)&buf
[offset
];
7001 offset
+= hdr
->zh_stats_size
* hdr
->zh_stats_count
;
7002 ztest_shared_ds
= (void *)&buf
[offset
];
7006 exec_child(char *cmd
, char *libpath
, boolean_t ignorekill
, int *statusp
)
7010 char *cmdbuf
= NULL
;
7015 cmdbuf
= umem_alloc(MAXPATHLEN
, UMEM_NOFAIL
);
7016 (void) strlcpy(cmdbuf
, getexecname(), MAXPATHLEN
);
7021 fatal(1, "fork failed");
7023 if (pid
== 0) { /* child */
7024 char *emptyargv
[2] = { cmd
, NULL
};
7025 char fd_data_str
[12];
7027 struct rlimit rl
= { 1024, 1024 };
7028 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
7030 (void) close(ztest_fd_rand
);
7031 VERIFY(11 >= snprintf(fd_data_str
, 12, "%d", ztest_fd_data
));
7032 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str
, 1));
7034 (void) enable_extended_FILE_stdio(-1, -1);
7035 if (libpath
!= NULL
)
7036 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath
, 1));
7037 (void) execv(cmd
, emptyargv
);
7038 ztest_dump_core
= B_FALSE
;
7039 fatal(B_TRUE
, "exec failed: %s", cmd
);
7042 if (cmdbuf
!= NULL
) {
7043 umem_free(cmdbuf
, MAXPATHLEN
);
7047 while (waitpid(pid
, &status
, 0) != pid
)
7049 if (statusp
!= NULL
)
7052 if (WIFEXITED(status
)) {
7053 if (WEXITSTATUS(status
) != 0) {
7054 (void) fprintf(stderr
, "child exited with code %d\n",
7055 WEXITSTATUS(status
));
7059 } else if (WIFSIGNALED(status
)) {
7060 if (!ignorekill
|| WTERMSIG(status
) != SIGKILL
) {
7061 (void) fprintf(stderr
, "child died with signal %d\n",
7067 (void) fprintf(stderr
, "something strange happened to child\n");
7074 ztest_run_init(void)
7078 ztest_shared_t
*zs
= ztest_shared
;
7081 * Blow away any existing copy of zpool.cache
7083 (void) remove(spa_config_path
);
7085 if (ztest_opts
.zo_init
== 0) {
7086 if (ztest_opts
.zo_verbose
>= 1)
7087 (void) printf("Importing pool %s\n",
7088 ztest_opts
.zo_pool
);
7094 * Create and initialize our storage pool.
7096 for (i
= 1; i
<= ztest_opts
.zo_init
; i
++) {
7097 bzero(zs
, sizeof (ztest_shared_t
));
7098 if (ztest_opts
.zo_verbose
>= 3 &&
7099 ztest_opts
.zo_init
!= 1) {
7100 (void) printf("ztest_init(), pass %d\n", i
);
7107 main(int argc
, char **argv
)
7115 ztest_shared_callstate_t
*zc
;
7117 char numbuf
[NN_NUMBUF_SZ
];
7122 char *fd_data_str
= getenv("ZTEST_FD_DATA");
7123 struct sigaction action
;
7125 (void) setvbuf(stdout
, NULL
, _IOLBF
, 0);
7127 dprintf_setup(&argc
, argv
);
7128 zfs_deadman_synctime_ms
= 300000;
7130 action
.sa_handler
= sig_handler
;
7131 sigemptyset(&action
.sa_mask
);
7132 action
.sa_flags
= 0;
7134 if (sigaction(SIGSEGV
, &action
, NULL
) < 0) {
7135 (void) fprintf(stderr
, "ztest: cannot catch SIGSEGV: %s.\n",
7140 if (sigaction(SIGABRT
, &action
, NULL
) < 0) {
7141 (void) fprintf(stderr
, "ztest: cannot catch SIGABRT: %s.\n",
7147 * Force random_get_bytes() to use /dev/urandom in order to prevent
7148 * ztest from needlessly depleting the system entropy pool.
7150 random_path
= "/dev/urandom";
7151 ztest_fd_rand
= open(random_path
, O_RDONLY
);
7152 ASSERT3S(ztest_fd_rand
, >=, 0);
7155 process_options(argc
, argv
);
7160 bcopy(&ztest_opts
, ztest_shared_opts
,
7161 sizeof (*ztest_shared_opts
));
7163 ztest_fd_data
= atoi(fd_data_str
);
7165 bcopy(ztest_shared_opts
, &ztest_opts
, sizeof (ztest_opts
));
7167 ASSERT3U(ztest_opts
.zo_datasets
, ==, ztest_shared_hdr
->zh_ds_count
);
7169 /* Override location of zpool.cache */
7170 VERIFY(asprintf((char **)&spa_config_path
, "%s/zpool.cache",
7171 ztest_opts
.zo_dir
) != -1);
7173 ztest_ds
= umem_alloc(ztest_opts
.zo_datasets
* sizeof (ztest_ds_t
),
7178 metaslab_gang_bang
= ztest_opts
.zo_metaslab_gang_bang
;
7179 metaslab_df_alloc_threshold
=
7180 zs
->zs_metaslab_df_alloc_threshold
;
7189 hasalt
= (strlen(ztest_opts
.zo_alt_ztest
) != 0);
7191 if (ztest_opts
.zo_verbose
>= 1) {
7192 (void) printf("%llu vdevs, %d datasets, %d threads,"
7193 " %llu seconds...\n",
7194 (u_longlong_t
)ztest_opts
.zo_vdevs
,
7195 ztest_opts
.zo_datasets
,
7196 ztest_opts
.zo_threads
,
7197 (u_longlong_t
)ztest_opts
.zo_time
);
7200 cmd
= umem_alloc(MAXNAMELEN
, UMEM_NOFAIL
);
7201 (void) strlcpy(cmd
, getexecname(), MAXNAMELEN
);
7203 zs
->zs_do_init
= B_TRUE
;
7204 if (strlen(ztest_opts
.zo_alt_ztest
) != 0) {
7205 if (ztest_opts
.zo_verbose
>= 1) {
7206 (void) printf("Executing older ztest for "
7207 "initialization: %s\n", ztest_opts
.zo_alt_ztest
);
7209 VERIFY(!exec_child(ztest_opts
.zo_alt_ztest
,
7210 ztest_opts
.zo_alt_libpath
, B_FALSE
, NULL
));
7212 VERIFY(!exec_child(NULL
, NULL
, B_FALSE
, NULL
));
7214 zs
->zs_do_init
= B_FALSE
;
7216 zs
->zs_proc_start
= gethrtime();
7217 zs
->zs_proc_stop
= zs
->zs_proc_start
+ ztest_opts
.zo_time
* NANOSEC
;
7219 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7220 zi
= &ztest_info
[f
];
7221 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7222 if (zs
->zs_proc_start
+ zi
->zi_interval
[0] > zs
->zs_proc_stop
)
7223 zc
->zc_next
= UINT64_MAX
;
7225 zc
->zc_next
= zs
->zs_proc_start
+
7226 ztest_random(2 * zi
->zi_interval
[0] + 1);
7230 * Run the tests in a loop. These tests include fault injection
7231 * to verify that self-healing data works, and forced crashes
7232 * to verify that we never lose on-disk consistency.
7234 while (gethrtime() < zs
->zs_proc_stop
) {
7239 * Initialize the workload counters for each function.
7241 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7242 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7247 /* Set the allocation switch size */
7248 zs
->zs_metaslab_df_alloc_threshold
=
7249 ztest_random(zs
->zs_metaslab_sz
/ 4) + 1;
7251 if (!hasalt
|| ztest_random(2) == 0) {
7252 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7253 (void) printf("Executing newer ztest: %s\n",
7257 killed
= exec_child(cmd
, NULL
, B_TRUE
, &status
);
7259 if (hasalt
&& ztest_opts
.zo_verbose
>= 1) {
7260 (void) printf("Executing older ztest: %s\n",
7261 ztest_opts
.zo_alt_ztest
);
7264 killed
= exec_child(ztest_opts
.zo_alt_ztest
,
7265 ztest_opts
.zo_alt_libpath
, B_TRUE
, &status
);
7272 if (ztest_opts
.zo_verbose
>= 1) {
7273 hrtime_t now
= gethrtime();
7275 now
= MIN(now
, zs
->zs_proc_stop
);
7276 print_time(zs
->zs_proc_stop
- now
, timebuf
);
7277 nicenum(zs
->zs_space
, numbuf
, sizeof (numbuf
));
7279 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7280 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7282 WIFEXITED(status
) ? "Complete" : "SIGKILL",
7283 (u_longlong_t
)zs
->zs_enospc_count
,
7284 100.0 * zs
->zs_alloc
/ zs
->zs_space
,
7286 100.0 * (now
- zs
->zs_proc_start
) /
7287 (ztest_opts
.zo_time
* NANOSEC
), timebuf
);
7290 if (ztest_opts
.zo_verbose
>= 2) {
7291 (void) printf("\nWorkload summary:\n\n");
7292 (void) printf("%7s %9s %s\n",
7293 "Calls", "Time", "Function");
7294 (void) printf("%7s %9s %s\n",
7295 "-----", "----", "--------");
7296 for (f
= 0; f
< ZTEST_FUNCS
; f
++) {
7297 zi
= &ztest_info
[f
];
7298 zc
= ZTEST_GET_SHARED_CALLSTATE(f
);
7299 print_time(zc
->zc_time
, timebuf
);
7300 (void) printf("%7llu %9s %s\n",
7301 (u_longlong_t
)zc
->zc_count
, timebuf
,
7304 (void) printf("\n");
7308 * It's possible that we killed a child during a rename test,
7309 * in which case we'll have a 'ztest_tmp' pool lying around
7310 * instead of 'ztest'. Do a blind rename in case this happened.
7313 if (spa_open(ztest_opts
.zo_pool
, &spa
, FTAG
) == 0) {
7314 spa_close(spa
, FTAG
);
7316 char tmpname
[ZFS_MAX_DATASET_NAME_LEN
];
7318 kernel_init(FREAD
| FWRITE
);
7319 (void) snprintf(tmpname
, sizeof (tmpname
), "%s_tmp",
7320 ztest_opts
.zo_pool
);
7321 (void) spa_rename(tmpname
, ztest_opts
.zo_pool
);
7325 if (!ztest_opts
.zo_mmp_test
)
7326 ztest_run_zdb(ztest_opts
.zo_pool
);
7329 if (ztest_opts
.zo_verbose
>= 1) {
7331 (void) printf("%d runs of older ztest: %s\n", older
,
7332 ztest_opts
.zo_alt_ztest
);
7333 (void) printf("%d runs of newer ztest: %s\n", newer
,
7336 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7337 kills
, iters
- kills
, (100.0 * kills
) / MAX(1, iters
));
7340 umem_free(cmd
, MAXNAMELEN
);