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_force_ganging;
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_force_ganging = 32 << 10
198 extern uint64_t metaslab_force_ganging;
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_device_removal;
347 ztest_func_t ztest_remap_blocks;
348 ztest_func_t ztest_fletcher;
349 ztest_func_t ztest_fletcher_incr;
350 ztest_func_t ztest_verify_dnode_bt;
352 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
353 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
354 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
355 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
356 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
358 #define ZTI_INIT(func, iters, interval) \
359 { .zi_func = (func), \
360 .zi_iters = (iters), \
361 .zi_interval = (interval), \
362 .zi_funcname = # func }
364 ztest_info_t ztest_info[] = {
365 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
366 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
367 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
368 ZTI_INIT(ztest_dmu_object_next_chunk, 1, &zopt_sometimes),
369 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
370 ZTI_INIT(ztest_zap, 30, &zopt_always),
371 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
372 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
373 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
374 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
376 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
377 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
378 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
380 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
382 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
383 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
384 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
385 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
386 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
387 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
388 ZTI_INIT(ztest_mmp_enable_disable, 1, &zopt_sometimes),
389 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
390 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
391 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
392 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
393 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
394 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
395 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
396 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
397 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
398 ZTI_INIT(ztest_device_removal, 1, &zopt_sometimes),
399 ZTI_INIT(ztest_remap_blocks, 1, &zopt_sometimes),
400 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
401 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
402 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
405 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
408 * The following struct is used to hold a list of uncalled commit callbacks.
409 * The callbacks are ordered by txg number.
411 typedef struct ztest_cb_list {
412 kmutex_t zcl_callbacks_lock;
413 list_t zcl_callbacks;
417 * Stuff we need to share writably between parent and child.
419 typedef struct ztest_shared {
420 boolean_t zs_do_init;
421 hrtime_t zs_proc_start;
422 hrtime_t zs_proc_stop;
423 hrtime_t zs_thread_start;
424 hrtime_t zs_thread_stop;
425 hrtime_t zs_thread_kill;
426 uint64_t zs_enospc_count;
427 uint64_t zs_vdev_next_leaf;
428 uint64_t zs_vdev_aux;
433 uint64_t zs_metaslab_sz;
434 uint64_t zs_metaslab_df_alloc_threshold;
438 #define ID_PARALLEL -1ULL
440 static char ztest_dev_template[] = "%s/%s.%llua";
441 static char ztest_aux_template[] = "%s/%s.%s.%llu";
442 ztest_shared_t *ztest_shared;
444 static spa_t *ztest_spa = NULL;
445 static ztest_ds_t *ztest_ds;
447 static kmutex_t ztest_vdev_lock;
448 static boolean_t ztest_device_removal_active = B_FALSE;
451 * The ztest_name_lock protects the pool and dataset namespace used by
452 * the individual tests. To modify the namespace, consumers must grab
453 * this lock as writer. Grabbing the lock as reader will ensure that the
454 * namespace does not change while the lock is held.
456 static rwlock_t ztest_name_lock;
458 static boolean_t ztest_dump_core = B_TRUE;
459 static boolean_t ztest_dump_debug_buffer = B_FALSE;
460 static boolean_t ztest_exiting;
462 /* Global commit callback list */
463 static ztest_cb_list_t zcl;
464 /* Commit cb delay */
465 static uint64_t zc_min_txg_delay = UINT64_MAX;
466 static int zc_cb_counter = 0;
469 * Minimum number of commit callbacks that need to be registered for us to check
470 * whether the minimum txg delay is acceptable.
472 #define ZTEST_COMMIT_CB_MIN_REG 100
475 * If a number of txgs equal to this threshold have been created after a commit
476 * callback has been registered but not called, then we assume there is an
477 * implementation bug.
479 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
481 extern uint64_t metaslab_gang_bang;
482 extern uint64_t metaslab_df_alloc_threshold;
485 ZTEST_META_DNODE = 0,
490 static void usage(boolean_t) __NORETURN;
493 * These libumem hooks provide a reasonable set of defaults for the allocator's
494 * debugging facilities.
497 _umem_debug_init(void)
499 return ("default,verbose"); /* $UMEM_DEBUG setting */
503 _umem_logging_init(void)
505 return ("fail,contents"); /* $UMEM_LOGGING setting */
509 dump_debug_buffer(void)
511 if (!ztest_dump_debug_buffer)
515 zfs_dbgmsg_print("ztest");
518 #define BACKTRACE_SZ 100
520 static void sig_handler(int signo)
522 struct sigaction action;
523 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
525 void *buffer[BACKTRACE_SZ];
527 nptrs = backtrace(buffer, BACKTRACE_SZ);
528 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
533 * Restore default action and re-raise signal so SIGSEGV and
534 * SIGABRT can trigger a core dump.
536 action.sa_handler = SIG_DFL;
537 sigemptyset(&action.sa_mask);
539 (void) sigaction(signo, &action, NULL);
543 #define FATAL_MSG_SZ 1024
548 fatal(int do_perror, char *message, ...)
551 int save_errno = errno;
554 (void) fflush(stdout);
555 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
557 va_start(args, message);
558 (void) sprintf(buf, "ztest: ");
560 (void) vsprintf(buf + strlen(buf), message, args);
563 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
564 ": %s", strerror(save_errno));
566 (void) fprintf(stderr, "%s\n", buf);
567 fatal_msg = buf; /* to ease debugging */
577 str2shift(const char *buf)
579 const char *ends = "BKMGTPEZ";
584 for (i = 0; i < strlen(ends); i++) {
585 if (toupper(buf[0]) == ends[i])
588 if (i == strlen(ends)) {
589 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
593 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
596 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
602 nicenumtoull(const char *buf)
607 val = strtoull(buf, &end, 0);
609 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
611 } else if (end[0] == '.') {
612 double fval = strtod(buf, &end);
613 fval *= pow(2, str2shift(end));
614 if (fval > UINT64_MAX) {
615 (void) fprintf(stderr, "ztest: value too large: %s\n",
619 val = (uint64_t)fval;
621 int shift = str2shift(end);
622 if (shift >= 64 || (val << shift) >> shift != val) {
623 (void) fprintf(stderr, "ztest: value too large: %s\n",
633 usage(boolean_t requested)
635 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
637 char nice_vdev_size[NN_NUMBUF_SZ];
638 char nice_force_ganging[NN_NUMBUF_SZ];
639 FILE *fp = requested ? stdout : stderr;
641 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
642 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
643 sizeof (nice_force_ganging));
645 (void) fprintf(fp, "Usage: %s\n"
646 "\t[-v vdevs (default: %llu)]\n"
647 "\t[-s size_of_each_vdev (default: %s)]\n"
648 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
649 "\t[-m mirror_copies (default: %d)]\n"
650 "\t[-r raidz_disks (default: %d)]\n"
651 "\t[-R raidz_parity (default: %d)]\n"
652 "\t[-d datasets (default: %d)]\n"
653 "\t[-t threads (default: %d)]\n"
654 "\t[-g gang_block_threshold (default: %s)]\n"
655 "\t[-i init_count (default: %d)] initialize pool i times\n"
656 "\t[-k kill_percentage (default: %llu%%)]\n"
657 "\t[-p pool_name (default: %s)]\n"
658 "\t[-f dir (default: %s)] file directory for vdev files\n"
659 "\t[-M] Multi-host simulate pool imported on remote host\n"
660 "\t[-V] verbose (use multiple times for ever more blather)\n"
661 "\t[-E] use existing pool instead of creating new one\n"
662 "\t[-T time (default: %llu sec)] total run time\n"
663 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
664 "\t[-P passtime (default: %llu sec)] time per pass\n"
665 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
666 "\t[-o variable=value] ... set global variable to an unsigned\n"
667 "\t 32-bit integer value\n"
668 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
669 "\t[-h] (print help)\n"
672 (u_longlong_t)zo->zo_vdevs, /* -v */
673 nice_vdev_size, /* -s */
674 zo->zo_ashift, /* -a */
675 zo->zo_mirrors, /* -m */
676 zo->zo_raidz, /* -r */
677 zo->zo_raidz_parity, /* -R */
678 zo->zo_datasets, /* -d */
679 zo->zo_threads, /* -t */
680 nice_force_ganging, /* -g */
681 zo->zo_init, /* -i */
682 (u_longlong_t)zo->zo_killrate, /* -k */
683 zo->zo_pool, /* -p */
685 (u_longlong_t)zo->zo_time, /* -T */
686 (u_longlong_t)zo->zo_maxloops, /* -F */
687 (u_longlong_t)zo->zo_passtime);
688 exit(requested ? 0 : 1);
692 process_options(int argc, char **argv)
695 ztest_shared_opts_t *zo = &ztest_opts;
699 char altdir[MAXNAMELEN] = { 0 };
701 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
703 while ((opt = getopt(argc, argv,
704 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:G")) != EOF) {
721 value = nicenumtoull(optarg);
725 zo->zo_vdevs = value;
728 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
731 zo->zo_ashift = value;
734 zo->zo_mirrors = value;
737 zo->zo_raidz = MAX(1, value);
740 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
743 zo->zo_datasets = MAX(1, value);
746 zo->zo_threads = MAX(1, value);
749 zo->zo_metaslab_force_ganging =
750 MAX(SPA_MINBLOCKSIZE << 1, value);
756 zo->zo_killrate = value;
759 (void) strlcpy(zo->zo_pool, optarg,
760 sizeof (zo->zo_pool));
763 path = realpath(optarg, NULL);
765 (void) fprintf(stderr, "error: %s: %s\n",
766 optarg, strerror(errno));
769 (void) strlcpy(zo->zo_dir, path,
770 sizeof (zo->zo_dir));
787 zo->zo_passtime = MAX(1, value);
790 zo->zo_maxloops = MAX(1, value);
793 (void) strlcpy(altdir, optarg, sizeof (altdir));
796 if (set_global_var(optarg) != 0)
800 ztest_dump_debug_buffer = B_TRUE;
812 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
815 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
818 if (strlen(altdir) > 0) {
826 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
827 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
829 VERIFY(NULL != realpath(getexecname(), cmd));
830 if (0 != access(altdir, F_OK)) {
831 ztest_dump_core = B_FALSE;
832 fatal(B_TRUE, "invalid alternate ztest path: %s",
835 VERIFY(NULL != realpath(altdir, realaltdir));
838 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
839 * We want to extract <isa> to determine if we should use
840 * 32 or 64 bit binaries.
842 bin = strstr(cmd, "/usr/bin/");
843 ztest = strstr(bin, "/ztest");
845 isalen = ztest - isa;
846 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
847 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
848 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
849 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
851 if (0 != access(zo->zo_alt_ztest, X_OK)) {
852 ztest_dump_core = B_FALSE;
853 fatal(B_TRUE, "invalid alternate ztest: %s",
855 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
856 ztest_dump_core = B_FALSE;
857 fatal(B_TRUE, "invalid alternate lib directory %s",
861 umem_free(cmd, MAXPATHLEN);
862 umem_free(realaltdir, MAXPATHLEN);
867 ztest_kill(ztest_shared_t *zs)
869 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
870 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
873 * Before we kill off ztest, make sure that the config is updated.
874 * See comment above spa_write_cachefile().
876 mutex_enter(&spa_namespace_lock);
877 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
878 mutex_exit(&spa_namespace_lock);
880 (void) kill(getpid(), SIGKILL);
884 ztest_random(uint64_t range)
888 ASSERT3S(ztest_fd_rand, >=, 0);
893 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
894 fatal(1, "short read from /dev/urandom");
901 ztest_record_enospc(const char *s)
903 ztest_shared->zs_enospc_count++;
907 ztest_get_ashift(void)
909 if (ztest_opts.zo_ashift == 0)
910 return (SPA_MINBLOCKSHIFT + ztest_random(5));
911 return (ztest_opts.zo_ashift);
915 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
921 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
924 ashift = ztest_get_ashift();
930 vdev = ztest_shared->zs_vdev_aux;
931 (void) snprintf(path, MAXPATHLEN,
932 ztest_aux_template, ztest_opts.zo_dir,
933 pool == NULL ? ztest_opts.zo_pool : pool,
936 vdev = ztest_shared->zs_vdev_next_leaf++;
937 (void) snprintf(path, MAXPATHLEN,
938 ztest_dev_template, ztest_opts.zo_dir,
939 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
944 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
946 fatal(1, "can't open %s", path);
947 if (ftruncate(fd, size) != 0)
948 fatal(1, "can't ftruncate %s", path);
952 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
953 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
954 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
955 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
956 umem_free(pathbuf, MAXPATHLEN);
962 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
963 uint64_t ashift, int r)
965 nvlist_t *raidz, **child;
969 return (make_vdev_file(path, aux, pool, size, ashift));
970 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
972 for (c = 0; c < r; c++)
973 child[c] = make_vdev_file(path, aux, pool, size, ashift);
975 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
976 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
977 VDEV_TYPE_RAIDZ) == 0);
978 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
979 ztest_opts.zo_raidz_parity) == 0);
980 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
983 for (c = 0; c < r; c++)
984 nvlist_free(child[c]);
986 umem_free(child, r * sizeof (nvlist_t *));
992 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
993 uint64_t ashift, int r, int m)
995 nvlist_t *mirror, **child;
999 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
1001 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
1003 for (c = 0; c < m; c++)
1004 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1006 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1007 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1008 VDEV_TYPE_MIRROR) == 0);
1009 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1012 for (c = 0; c < m; c++)
1013 nvlist_free(child[c]);
1015 umem_free(child, m * sizeof (nvlist_t *));
1021 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1022 int log, int r, int m, int t)
1024 nvlist_t *root, **child;
1029 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1031 for (c = 0; c < t; c++) {
1032 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1034 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1038 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1039 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1040 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1043 for (c = 0; c < t; c++)
1044 nvlist_free(child[c]);
1046 umem_free(child, t * sizeof (nvlist_t *));
1052 * Find a random spa version. Returns back a random spa version in the
1053 * range [initial_version, SPA_VERSION_FEATURES].
1056 ztest_random_spa_version(uint64_t initial_version)
1058 uint64_t version = initial_version;
1060 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1062 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1065 if (version > SPA_VERSION_BEFORE_FEATURES)
1066 version = SPA_VERSION_FEATURES;
1068 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1073 ztest_random_blocksize(void)
1076 * Choose a block size >= the ashift.
1077 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1079 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1080 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1082 uint64_t block_shift =
1083 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1084 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1088 ztest_random_dnodesize(void)
1091 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1093 if (max_slots == DNODE_MIN_SLOTS)
1094 return (DNODE_MIN_SIZE);
1097 * Weight the random distribution more heavily toward smaller
1098 * dnode sizes since that is more likely to reflect real-world
1101 ASSERT3U(max_slots, >, 4);
1102 switch (ztest_random(10)) {
1104 slots = 5 + ztest_random(max_slots - 4);
1107 slots = 2 + ztest_random(3);
1114 return (slots << DNODE_SHIFT);
1118 ztest_random_ibshift(void)
1120 return (DN_MIN_INDBLKSHIFT +
1121 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1125 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1128 vdev_t *rvd = spa->spa_root_vdev;
1131 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1134 top = ztest_random(rvd->vdev_children);
1135 tvd = rvd->vdev_child[top];
1136 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1137 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1143 ztest_random_dsl_prop(zfs_prop_t prop)
1148 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1149 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1155 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1158 const char *propname = zfs_prop_to_name(prop);
1159 const char *valname;
1164 error = dsl_prop_set_int(osname, propname,
1165 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1167 if (error == ENOSPC) {
1168 ztest_record_enospc(FTAG);
1173 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1174 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1176 if (ztest_opts.zo_verbose >= 6) {
1179 err = zfs_prop_index_to_string(prop, curval, &valname);
1181 (void) printf("%s %s = %llu at '%s'\n", osname,
1182 propname, (unsigned long long)curval, setpoint);
1184 (void) printf("%s %s = %s at '%s'\n",
1185 osname, propname, valname, setpoint);
1187 umem_free(setpoint, MAXPATHLEN);
1193 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1195 spa_t *spa = ztest_spa;
1196 nvlist_t *props = NULL;
1199 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1200 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1202 error = spa_prop_set(spa, props);
1206 if (error == ENOSPC) {
1207 ztest_record_enospc(FTAG);
1216 ztest_dmu_objset_own(const char *name, dmu_objset_type_t type,
1217 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
1221 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1222 if (decrypt && err == EACCES) {
1223 char ddname[ZFS_MAX_DATASET_NAME_LEN];
1224 dsl_crypto_params_t *dcp;
1225 nvlist_t *crypto_args = fnvlist_alloc();
1228 /* spa_keystore_load_wkey() expects a dsl dir name */
1229 strcpy(ddname, name);
1230 cp = strchr(ddname, '@');
1234 fnvlist_add_uint8_array(crypto_args, "wkeydata",
1235 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
1236 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
1237 crypto_args, &dcp));
1238 err = spa_keystore_load_wkey(ddname, dcp, B_FALSE);
1239 dsl_crypto_params_free(dcp, B_FALSE);
1240 fnvlist_free(crypto_args);
1245 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1253 * Object and range lock mechanics
1256 list_node_t z_lnode;
1257 refcount_t z_refcnt;
1259 zfs_rlock_t z_range_lock;
1264 ztest_znode_t *z_ztznode;
1267 static ztest_znode_t *
1268 ztest_znode_init(uint64_t object)
1270 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1272 list_link_init(&zp->z_lnode);
1273 refcount_create(&zp->z_refcnt);
1274 zp->z_object = object;
1275 zfs_rlock_init(&zp->z_range_lock);
1281 ztest_znode_fini(ztest_znode_t *zp)
1283 ASSERT(refcount_is_zero(&zp->z_refcnt));
1284 zfs_rlock_destroy(&zp->z_range_lock);
1286 refcount_destroy(&zp->z_refcnt);
1287 list_link_init(&zp->z_lnode);
1288 umem_free(zp, sizeof (*zp));
1292 ztest_zll_init(zll_t *zll)
1294 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1295 list_create(&zll->z_list, sizeof (ztest_znode_t),
1296 offsetof(ztest_znode_t, z_lnode));
1300 ztest_zll_destroy(zll_t *zll)
1302 list_destroy(&zll->z_list);
1303 mutex_destroy(&zll->z_lock);
1306 #define RL_TAG "range_lock"
1307 static ztest_znode_t *
1308 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1310 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1311 ztest_znode_t *zp = NULL;
1312 mutex_enter(&zll->z_lock);
1313 for (zp = list_head(&zll->z_list); (zp);
1314 zp = list_next(&zll->z_list, zp)) {
1315 if (zp->z_object == object) {
1316 refcount_add(&zp->z_refcnt, RL_TAG);
1321 zp = ztest_znode_init(object);
1322 refcount_add(&zp->z_refcnt, RL_TAG);
1323 list_insert_head(&zll->z_list, zp);
1325 mutex_exit(&zll->z_lock);
1330 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1333 ASSERT3U(zp->z_object, !=, 0);
1334 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1335 mutex_enter(&zll->z_lock);
1336 refcount_remove(&zp->z_refcnt, RL_TAG);
1337 if (refcount_is_zero(&zp->z_refcnt)) {
1338 list_remove(&zll->z_list, zp);
1339 ztest_znode_fini(zp);
1341 mutex_exit(&zll->z_lock);
1346 ztest_rll_init(rll_t *rll)
1348 rll->rll_writer = NULL;
1349 rll->rll_readers = 0;
1350 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1351 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1355 ztest_rll_destroy(rll_t *rll)
1357 ASSERT(rll->rll_writer == NULL);
1358 ASSERT(rll->rll_readers == 0);
1359 mutex_destroy(&rll->rll_lock);
1360 cv_destroy(&rll->rll_cv);
1364 ztest_rll_lock(rll_t *rll, rl_type_t type)
1366 mutex_enter(&rll->rll_lock);
1368 if (type == RL_READER) {
1369 while (rll->rll_writer != NULL)
1370 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1373 while (rll->rll_writer != NULL || rll->rll_readers)
1374 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1375 rll->rll_writer = curthread;
1378 mutex_exit(&rll->rll_lock);
1382 ztest_rll_unlock(rll_t *rll)
1384 mutex_enter(&rll->rll_lock);
1386 if (rll->rll_writer) {
1387 ASSERT(rll->rll_readers == 0);
1388 rll->rll_writer = NULL;
1390 ASSERT(rll->rll_readers != 0);
1391 ASSERT(rll->rll_writer == NULL);
1395 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1396 cv_broadcast(&rll->rll_cv);
1398 mutex_exit(&rll->rll_lock);
1402 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1404 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1406 ztest_rll_lock(rll, type);
1410 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1412 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1414 ztest_rll_unlock(rll);
1417 static ztest_zrl_t *
1418 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1420 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1422 zrl->z_ztznode = zp;
1427 ztest_zrl_fini(ztest_zrl_t *zrl)
1429 umem_free(zrl, sizeof (*zrl));
1432 static ztest_zrl_t *
1433 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1434 uint64_t size, rl_type_t type)
1436 ztest_znode_t *zp = ztest_znode_get(zd, object);
1437 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1439 return (ztest_zrl_init(rl, zp));
1443 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1445 zfs_range_unlock(zrl->z_rl);
1446 ztest_znode_put(zd, zrl->z_ztznode);
1447 ztest_zrl_fini(zrl);
1451 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1454 zd->zd_zilog = dmu_objset_zil(os);
1455 zd->zd_shared = szd;
1456 dmu_objset_name(os, zd->zd_name);
1459 if (zd->zd_shared != NULL)
1460 zd->zd_shared->zd_seq = 0;
1462 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1463 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1465 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1466 ztest_rll_init(&zd->zd_object_lock[l]);
1468 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1469 ztest_zll_init(&zd->zd_range_lock[l]);
1473 ztest_zd_fini(ztest_ds_t *zd)
1477 mutex_destroy(&zd->zd_dirobj_lock);
1478 (void) rwlock_destroy(&zd->zd_zilog_lock);
1480 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1481 ztest_rll_destroy(&zd->zd_object_lock[l]);
1483 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1484 ztest_zll_destroy(&zd->zd_range_lock[l]);
1487 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1490 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1496 * Attempt to assign tx to some transaction group.
1498 error = dmu_tx_assign(tx, txg_how);
1500 if (error == ERESTART) {
1501 ASSERT(txg_how == TXG_NOWAIT);
1504 ASSERT3U(error, ==, ENOSPC);
1505 ztest_record_enospc(tag);
1510 txg = dmu_tx_get_txg(tx);
1516 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1519 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1527 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1530 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1534 diff |= (value - *ip++);
1541 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1542 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1545 bt->bt_magic = BT_MAGIC;
1546 bt->bt_objset = dmu_objset_id(os);
1547 bt->bt_object = object;
1548 bt->bt_dnodesize = dnodesize;
1549 bt->bt_offset = offset;
1552 bt->bt_crtxg = crtxg;
1556 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1557 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1560 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1561 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1562 ASSERT3U(bt->bt_object, ==, object);
1563 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1564 ASSERT3U(bt->bt_offset, ==, offset);
1565 ASSERT3U(bt->bt_gen, <=, gen);
1566 ASSERT3U(bt->bt_txg, <=, txg);
1567 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1570 static ztest_block_tag_t *
1571 ztest_bt_bonus(dmu_buf_t *db)
1573 dmu_object_info_t doi;
1574 ztest_block_tag_t *bt;
1576 dmu_object_info_from_db(db, &doi);
1577 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1578 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1579 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1585 * Generate a token to fill up unused bonus buffer space. Try to make
1586 * it unique to the object, generation, and offset to verify that data
1587 * is not getting overwritten by data from other dnodes.
1589 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1590 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1593 * Fill up the unused bonus buffer region before the block tag with a
1594 * verifiable pattern. Filling the whole bonus area with non-zero data
1595 * helps ensure that all dnode traversal code properly skips the
1596 * interior regions of large dnodes.
1599 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1600 objset_t *os, uint64_t gen)
1604 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1606 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1607 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1608 gen, bonusp - (uint64_t *)db->db_data);
1614 * Verify that the unused area of a bonus buffer is filled with the
1618 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1619 objset_t *os, uint64_t gen)
1623 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1624 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1625 gen, bonusp - (uint64_t *)db->db_data);
1626 VERIFY3U(*bonusp, ==, token);
1634 #define lrz_type lr_mode
1635 #define lrz_blocksize lr_uid
1636 #define lrz_ibshift lr_gid
1637 #define lrz_bonustype lr_rdev
1638 #define lrz_dnodesize lr_crtime[1]
1641 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1643 char *name = (void *)(lr + 1); /* name follows lr */
1644 size_t namesize = strlen(name) + 1;
1647 if (zil_replaying(zd->zd_zilog, tx))
1650 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1651 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1652 sizeof (*lr) + namesize - sizeof (lr_t));
1654 zil_itx_assign(zd->zd_zilog, itx, tx);
1658 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1660 char *name = (void *)(lr + 1); /* name follows lr */
1661 size_t namesize = strlen(name) + 1;
1664 if (zil_replaying(zd->zd_zilog, tx))
1667 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1668 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1669 sizeof (*lr) + namesize - sizeof (lr_t));
1671 itx->itx_oid = object;
1672 zil_itx_assign(zd->zd_zilog, itx, tx);
1676 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1679 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1681 if (zil_replaying(zd->zd_zilog, tx))
1684 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1685 write_state = WR_INDIRECT;
1687 itx = zil_itx_create(TX_WRITE,
1688 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1690 if (write_state == WR_COPIED &&
1691 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1692 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1693 zil_itx_destroy(itx);
1694 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1695 write_state = WR_NEED_COPY;
1697 itx->itx_private = zd;
1698 itx->itx_wr_state = write_state;
1699 itx->itx_sync = (ztest_random(8) == 0);
1701 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1702 sizeof (*lr) - sizeof (lr_t));
1704 zil_itx_assign(zd->zd_zilog, itx, tx);
1708 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1712 if (zil_replaying(zd->zd_zilog, tx))
1715 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1716 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1717 sizeof (*lr) - sizeof (lr_t));
1719 itx->itx_sync = B_FALSE;
1720 zil_itx_assign(zd->zd_zilog, itx, tx);
1724 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1728 if (zil_replaying(zd->zd_zilog, tx))
1731 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1732 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1733 sizeof (*lr) - sizeof (lr_t));
1735 itx->itx_sync = B_FALSE;
1736 zil_itx_assign(zd->zd_zilog, itx, tx);
1743 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1745 ztest_ds_t *zd = arg1;
1746 lr_create_t *lr = arg2;
1747 char *name = (void *)(lr + 1); /* name follows lr */
1748 objset_t *os = zd->zd_os;
1749 ztest_block_tag_t *bbt;
1757 byteswap_uint64_array(lr, sizeof (*lr));
1759 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1760 ASSERT(name[0] != '\0');
1762 tx = dmu_tx_create(os);
1764 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1766 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1767 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1769 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1772 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1776 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1777 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1779 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1780 if (lr->lr_foid == 0) {
1781 lr->lr_foid = zap_create_dnsize(os,
1782 lr->lrz_type, lr->lrz_bonustype,
1783 bonuslen, lr->lrz_dnodesize, tx);
1785 error = zap_create_claim_dnsize(os, lr->lr_foid,
1786 lr->lrz_type, lr->lrz_bonustype,
1787 bonuslen, lr->lrz_dnodesize, tx);
1790 if (lr->lr_foid == 0) {
1791 lr->lr_foid = dmu_object_alloc_dnsize(os,
1792 lr->lrz_type, 0, lr->lrz_bonustype,
1793 bonuslen, lr->lrz_dnodesize, tx);
1795 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1796 lr->lrz_type, 0, lr->lrz_bonustype,
1797 bonuslen, lr->lrz_dnodesize, tx);
1802 ASSERT3U(error, ==, EEXIST);
1803 ASSERT(zd->zd_zilog->zl_replay);
1808 ASSERT(lr->lr_foid != 0);
1810 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1811 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1812 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1814 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1815 bbt = ztest_bt_bonus(db);
1816 dmu_buf_will_dirty(db, tx);
1817 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1818 lr->lr_gen, txg, txg);
1819 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1820 dmu_buf_rele(db, FTAG);
1822 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1825 (void) ztest_log_create(zd, tx, lr);
1833 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1835 ztest_ds_t *zd = arg1;
1836 lr_remove_t *lr = arg2;
1837 char *name = (void *)(lr + 1); /* name follows lr */
1838 objset_t *os = zd->zd_os;
1839 dmu_object_info_t doi;
1841 uint64_t object, txg;
1844 byteswap_uint64_array(lr, sizeof (*lr));
1846 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1847 ASSERT(name[0] != '\0');
1850 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1851 ASSERT(object != 0);
1853 ztest_object_lock(zd, object, RL_WRITER);
1855 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1857 tx = dmu_tx_create(os);
1859 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1860 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1862 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1864 ztest_object_unlock(zd, object);
1868 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1869 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1871 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1874 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1876 (void) ztest_log_remove(zd, tx, lr, object);
1880 ztest_object_unlock(zd, object);
1886 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1888 ztest_ds_t *zd = arg1;
1889 lr_write_t *lr = arg2;
1890 objset_t *os = zd->zd_os;
1891 void *data = lr + 1; /* data follows lr */
1892 uint64_t offset, length;
1893 ztest_block_tag_t *bt = data;
1894 ztest_block_tag_t *bbt;
1895 uint64_t gen, txg, lrtxg, crtxg;
1896 dmu_object_info_t doi;
1899 arc_buf_t *abuf = NULL;
1903 byteswap_uint64_array(lr, sizeof (*lr));
1905 offset = lr->lr_offset;
1906 length = lr->lr_length;
1908 /* If it's a dmu_sync() block, write the whole block */
1909 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1910 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1911 if (length < blocksize) {
1912 offset -= offset % blocksize;
1917 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1918 byteswap_uint64_array(bt, sizeof (*bt));
1920 if (bt->bt_magic != BT_MAGIC)
1923 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1924 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1926 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1928 dmu_object_info_from_db(db, &doi);
1930 bbt = ztest_bt_bonus(db);
1931 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1933 crtxg = bbt->bt_crtxg;
1934 lrtxg = lr->lr_common.lrc_txg;
1936 tx = dmu_tx_create(os);
1938 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1940 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1941 P2PHASE(offset, length) == 0)
1942 abuf = dmu_request_arcbuf(db, length);
1944 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1947 dmu_return_arcbuf(abuf);
1948 dmu_buf_rele(db, FTAG);
1949 ztest_range_unlock(zd, rl);
1950 ztest_object_unlock(zd, lr->lr_foid);
1956 * Usually, verify the old data before writing new data --
1957 * but not always, because we also want to verify correct
1958 * behavior when the data was not recently read into cache.
1960 ASSERT(offset % doi.doi_data_block_size == 0);
1961 if (ztest_random(4) != 0) {
1962 int prefetch = ztest_random(2) ?
1963 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1964 ztest_block_tag_t rbt;
1966 VERIFY(dmu_read(os, lr->lr_foid, offset,
1967 sizeof (rbt), &rbt, prefetch) == 0);
1968 if (rbt.bt_magic == BT_MAGIC) {
1969 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1970 offset, gen, txg, crtxg);
1975 * Writes can appear to be newer than the bonus buffer because
1976 * the ztest_get_data() callback does a dmu_read() of the
1977 * open-context data, which may be different than the data
1978 * as it was when the write was generated.
1980 if (zd->zd_zilog->zl_replay) {
1981 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1982 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1987 * Set the bt's gen/txg to the bonus buffer's gen/txg
1988 * so that all of the usual ASSERTs will work.
1990 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1995 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1997 bcopy(data, abuf->b_data, length);
1998 dmu_assign_arcbuf_by_dbuf(db, offset, abuf, tx);
2001 (void) ztest_log_write(zd, tx, lr);
2003 dmu_buf_rele(db, FTAG);
2007 ztest_range_unlock(zd, rl);
2008 ztest_object_unlock(zd, lr->lr_foid);
2014 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
2016 ztest_ds_t *zd = arg1;
2017 lr_truncate_t *lr = arg2;
2018 objset_t *os = zd->zd_os;
2024 byteswap_uint64_array(lr, sizeof (*lr));
2026 ztest_object_lock(zd, lr->lr_foid, RL_READER);
2027 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
2030 tx = dmu_tx_create(os);
2032 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
2034 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2036 ztest_range_unlock(zd, rl);
2037 ztest_object_unlock(zd, lr->lr_foid);
2041 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
2042 lr->lr_length, tx) == 0);
2044 (void) ztest_log_truncate(zd, tx, lr);
2048 ztest_range_unlock(zd, rl);
2049 ztest_object_unlock(zd, lr->lr_foid);
2055 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
2057 ztest_ds_t *zd = arg1;
2058 lr_setattr_t *lr = arg2;
2059 objset_t *os = zd->zd_os;
2062 ztest_block_tag_t *bbt;
2063 uint64_t txg, lrtxg, crtxg, dnodesize;
2066 byteswap_uint64_array(lr, sizeof (*lr));
2068 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
2070 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
2072 tx = dmu_tx_create(os);
2073 dmu_tx_hold_bonus(tx, lr->lr_foid);
2075 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2077 dmu_buf_rele(db, FTAG);
2078 ztest_object_unlock(zd, lr->lr_foid);
2082 bbt = ztest_bt_bonus(db);
2083 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2084 crtxg = bbt->bt_crtxg;
2085 lrtxg = lr->lr_common.lrc_txg;
2086 dnodesize = bbt->bt_dnodesize;
2088 if (zd->zd_zilog->zl_replay) {
2089 ASSERT(lr->lr_size != 0);
2090 ASSERT(lr->lr_mode != 0);
2094 * Randomly change the size and increment the generation.
2096 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2098 lr->lr_mode = bbt->bt_gen + 1;
2103 * Verify that the current bonus buffer is not newer than our txg.
2105 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2106 MAX(txg, lrtxg), crtxg);
2108 dmu_buf_will_dirty(db, tx);
2110 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2111 ASSERT3U(lr->lr_size, <=, db->db_size);
2112 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2113 bbt = ztest_bt_bonus(db);
2115 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2117 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2118 dmu_buf_rele(db, FTAG);
2120 (void) ztest_log_setattr(zd, tx, lr);
2124 ztest_object_unlock(zd, lr->lr_foid);
2129 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
2130 NULL, /* 0 no such transaction type */
2131 ztest_replay_create, /* TX_CREATE */
2132 NULL, /* TX_MKDIR */
2133 NULL, /* TX_MKXATTR */
2134 NULL, /* TX_SYMLINK */
2135 ztest_replay_remove, /* TX_REMOVE */
2136 NULL, /* TX_RMDIR */
2138 NULL, /* TX_RENAME */
2139 ztest_replay_write, /* TX_WRITE */
2140 ztest_replay_truncate, /* TX_TRUNCATE */
2141 ztest_replay_setattr, /* TX_SETATTR */
2143 NULL, /* TX_CREATE_ACL */
2144 NULL, /* TX_CREATE_ATTR */
2145 NULL, /* TX_CREATE_ACL_ATTR */
2146 NULL, /* TX_MKDIR_ACL */
2147 NULL, /* TX_MKDIR_ATTR */
2148 NULL, /* TX_MKDIR_ACL_ATTR */
2149 NULL, /* TX_WRITE2 */
2153 * ZIL get_data callbacks
2155 typedef struct ztest_zgd_private {
2159 } ztest_zgd_private_t;
2162 ztest_get_done(zgd_t *zgd, int error)
2164 ztest_zgd_private_t *zzp = zgd->zgd_private;
2165 ztest_ds_t *zd = zzp->z_zd;
2166 uint64_t object = zzp->z_object;
2169 dmu_buf_rele(zgd->zgd_db, zgd);
2171 ztest_range_unlock(zd, zzp->z_rl);
2172 ztest_object_unlock(zd, object);
2174 if (error == 0 && zgd->zgd_bp)
2175 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
2177 umem_free(zgd, sizeof (*zgd));
2178 umem_free(zzp, sizeof (*zzp));
2182 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
2185 ztest_ds_t *zd = arg;
2186 objset_t *os = zd->zd_os;
2187 uint64_t object = lr->lr_foid;
2188 uint64_t offset = lr->lr_offset;
2189 uint64_t size = lr->lr_length;
2190 uint64_t txg = lr->lr_common.lrc_txg;
2192 dmu_object_info_t doi;
2196 ztest_zgd_private_t *zgd_private;
2198 ASSERT3P(lwb, !=, NULL);
2199 ASSERT3P(zio, !=, NULL);
2200 ASSERT3U(size, !=, 0);
2202 ztest_object_lock(zd, object, RL_READER);
2203 error = dmu_bonus_hold(os, object, FTAG, &db);
2205 ztest_object_unlock(zd, object);
2209 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2211 if (crtxg == 0 || crtxg > txg) {
2212 dmu_buf_rele(db, FTAG);
2213 ztest_object_unlock(zd, object);
2217 dmu_object_info_from_db(db, &doi);
2218 dmu_buf_rele(db, FTAG);
2221 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2223 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2224 zgd_private->z_zd = zd;
2225 zgd_private->z_object = object;
2226 zgd->zgd_private = zgd_private;
2228 if (buf != NULL) { /* immediate write */
2229 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2231 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2233 error = dmu_read(os, object, offset, size, buf,
2234 DMU_READ_NO_PREFETCH);
2237 size = doi.doi_data_block_size;
2239 offset = P2ALIGN(offset, size);
2241 ASSERT(offset < size);
2245 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2247 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2249 error = dmu_buf_hold(os, object, offset, zgd, &db,
2250 DMU_READ_NO_PREFETCH);
2253 blkptr_t *bp = &lr->lr_blkptr;
2258 ASSERT(db->db_offset == offset);
2259 ASSERT(db->db_size == size);
2261 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2262 ztest_get_done, zgd);
2269 ztest_get_done(zgd, error);
2275 ztest_lr_alloc(size_t lrsize, char *name)
2278 size_t namesize = name ? strlen(name) + 1 : 0;
2280 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2283 bcopy(name, lr + lrsize, namesize);
2289 ztest_lr_free(void *lr, size_t lrsize, char *name)
2291 size_t namesize = name ? strlen(name) + 1 : 0;
2293 umem_free(lr, lrsize + namesize);
2297 * Lookup a bunch of objects. Returns the number of objects not found.
2300 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2306 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2308 for (i = 0; i < count; i++, od++) {
2310 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2311 sizeof (uint64_t), 1, &od->od_object);
2313 ASSERT(error == ENOENT);
2314 ASSERT(od->od_object == 0);
2318 ztest_block_tag_t *bbt;
2319 dmu_object_info_t doi;
2321 ASSERT(od->od_object != 0);
2322 ASSERT(missing == 0); /* there should be no gaps */
2324 ztest_object_lock(zd, od->od_object, RL_READER);
2325 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2326 od->od_object, FTAG, &db));
2327 dmu_object_info_from_db(db, &doi);
2328 bbt = ztest_bt_bonus(db);
2329 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2330 od->od_type = doi.doi_type;
2331 od->od_blocksize = doi.doi_data_block_size;
2332 od->od_gen = bbt->bt_gen;
2333 dmu_buf_rele(db, FTAG);
2334 ztest_object_unlock(zd, od->od_object);
2342 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2347 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2349 for (i = 0; i < count; i++, od++) {
2356 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2358 lr->lr_doid = od->od_dir;
2359 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2360 lr->lrz_type = od->od_crtype;
2361 lr->lrz_blocksize = od->od_crblocksize;
2362 lr->lrz_ibshift = ztest_random_ibshift();
2363 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2364 lr->lrz_dnodesize = od->od_crdnodesize;
2365 lr->lr_gen = od->od_crgen;
2366 lr->lr_crtime[0] = time(NULL);
2368 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2369 ASSERT(missing == 0);
2373 od->od_object = lr->lr_foid;
2374 od->od_type = od->od_crtype;
2375 od->od_blocksize = od->od_crblocksize;
2376 od->od_gen = od->od_crgen;
2377 ASSERT(od->od_object != 0);
2380 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2387 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2393 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2397 for (i = count - 1; i >= 0; i--, od--) {
2404 * No object was found.
2406 if (od->od_object == 0)
2409 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2411 lr->lr_doid = od->od_dir;
2413 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2414 ASSERT3U(error, ==, ENOSPC);
2419 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2426 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2432 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2434 lr->lr_foid = object;
2435 lr->lr_offset = offset;
2436 lr->lr_length = size;
2438 BP_ZERO(&lr->lr_blkptr);
2440 bcopy(data, lr + 1, size);
2442 error = ztest_replay_write(zd, lr, B_FALSE);
2444 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2450 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2455 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2457 lr->lr_foid = object;
2458 lr->lr_offset = offset;
2459 lr->lr_length = size;
2461 error = ztest_replay_truncate(zd, lr, B_FALSE);
2463 ztest_lr_free(lr, sizeof (*lr), NULL);
2469 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2474 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2476 lr->lr_foid = object;
2480 error = ztest_replay_setattr(zd, lr, B_FALSE);
2482 ztest_lr_free(lr, sizeof (*lr), NULL);
2488 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2490 objset_t *os = zd->zd_os;
2495 txg_wait_synced(dmu_objset_pool(os), 0);
2497 ztest_object_lock(zd, object, RL_READER);
2498 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2500 tx = dmu_tx_create(os);
2502 dmu_tx_hold_write(tx, object, offset, size);
2504 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2507 dmu_prealloc(os, object, offset, size, tx);
2509 txg_wait_synced(dmu_objset_pool(os), txg);
2511 (void) dmu_free_long_range(os, object, offset, size);
2514 ztest_range_unlock(zd, rl);
2515 ztest_object_unlock(zd, object);
2519 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2522 ztest_block_tag_t wbt;
2523 dmu_object_info_t doi;
2524 enum ztest_io_type io_type;
2528 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2529 blocksize = doi.doi_data_block_size;
2530 data = umem_alloc(blocksize, UMEM_NOFAIL);
2533 * Pick an i/o type at random, biased toward writing block tags.
2535 io_type = ztest_random(ZTEST_IO_TYPES);
2536 if (ztest_random(2) == 0)
2537 io_type = ZTEST_IO_WRITE_TAG;
2539 (void) rw_rdlock(&zd->zd_zilog_lock);
2543 case ZTEST_IO_WRITE_TAG:
2544 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2546 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2549 case ZTEST_IO_WRITE_PATTERN:
2550 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2551 if (ztest_random(2) == 0) {
2553 * Induce fletcher2 collisions to ensure that
2554 * zio_ddt_collision() detects and resolves them
2555 * when using fletcher2-verify for deduplication.
2557 ((uint64_t *)data)[0] ^= 1ULL << 63;
2558 ((uint64_t *)data)[4] ^= 1ULL << 63;
2560 (void) ztest_write(zd, object, offset, blocksize, data);
2563 case ZTEST_IO_WRITE_ZEROES:
2564 bzero(data, blocksize);
2565 (void) ztest_write(zd, object, offset, blocksize, data);
2568 case ZTEST_IO_TRUNCATE:
2569 (void) ztest_truncate(zd, object, offset, blocksize);
2572 case ZTEST_IO_SETATTR:
2573 (void) ztest_setattr(zd, object);
2578 case ZTEST_IO_REWRITE:
2579 (void) rw_rdlock(&ztest_name_lock);
2580 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2581 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2583 VERIFY(err == 0 || err == ENOSPC);
2584 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2585 ZFS_PROP_COMPRESSION,
2586 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2588 VERIFY(err == 0 || err == ENOSPC);
2589 (void) rw_unlock(&ztest_name_lock);
2591 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2592 DMU_READ_NO_PREFETCH));
2594 (void) ztest_write(zd, object, offset, blocksize, data);
2598 (void) rw_unlock(&zd->zd_zilog_lock);
2600 umem_free(data, blocksize);
2604 * Initialize an object description template.
2607 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2608 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2611 od->od_dir = ZTEST_DIROBJ;
2614 od->od_crtype = type;
2615 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2616 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2619 od->od_type = DMU_OT_NONE;
2620 od->od_blocksize = 0;
2623 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2624 tag, (longlong_t)id, (u_longlong_t)index);
2628 * Lookup or create the objects for a test using the od template.
2629 * If the objects do not all exist, or if 'remove' is specified,
2630 * remove any existing objects and create new ones. Otherwise,
2631 * use the existing objects.
2634 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2636 int count = size / sizeof (*od);
2639 mutex_enter(&zd->zd_dirobj_lock);
2640 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2641 (ztest_remove(zd, od, count) != 0 ||
2642 ztest_create(zd, od, count) != 0))
2645 mutex_exit(&zd->zd_dirobj_lock);
2652 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2654 zilog_t *zilog = zd->zd_zilog;
2656 (void) rw_rdlock(&zd->zd_zilog_lock);
2658 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2661 * Remember the committed values in zd, which is in parent/child
2662 * shared memory. If we die, the next iteration of ztest_run()
2663 * will verify that the log really does contain this record.
2665 mutex_enter(&zilog->zl_lock);
2666 ASSERT(zd->zd_shared != NULL);
2667 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2668 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2669 mutex_exit(&zilog->zl_lock);
2671 (void) rw_unlock(&zd->zd_zilog_lock);
2675 * This function is designed to simulate the operations that occur during a
2676 * mount/unmount operation. We hold the dataset across these operations in an
2677 * attempt to expose any implicit assumptions about ZIL management.
2681 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2683 objset_t *os = zd->zd_os;
2686 * We grab the zd_dirobj_lock to ensure that no other thread is
2687 * updating the zil (i.e. adding in-memory log records) and the
2688 * zd_zilog_lock to block any I/O.
2690 mutex_enter(&zd->zd_dirobj_lock);
2691 (void) rw_wrlock(&zd->zd_zilog_lock);
2693 /* zfsvfs_teardown() */
2694 zil_close(zd->zd_zilog);
2696 /* zfsvfs_setup() */
2697 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2698 zil_replay(os, zd, ztest_replay_vector);
2700 (void) rw_unlock(&zd->zd_zilog_lock);
2701 mutex_exit(&zd->zd_dirobj_lock);
2705 * Verify that we can't destroy an active pool, create an existing pool,
2706 * or create a pool with a bad vdev spec.
2710 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2712 ztest_shared_opts_t *zo = &ztest_opts;
2716 if (zo->zo_mmp_test)
2720 * Attempt to create using a bad file.
2722 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2723 VERIFY3U(ENOENT, ==,
2724 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2725 nvlist_free(nvroot);
2728 * Attempt to create using a bad mirror.
2730 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2731 VERIFY3U(ENOENT, ==,
2732 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2733 nvlist_free(nvroot);
2736 * Attempt to create an existing pool. It shouldn't matter
2737 * what's in the nvroot; we should fail with EEXIST.
2739 (void) rw_rdlock(&ztest_name_lock);
2740 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2741 VERIFY3U(EEXIST, ==,
2742 spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2743 nvlist_free(nvroot);
2744 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2745 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2746 spa_close(spa, FTAG);
2748 (void) rw_unlock(&ztest_name_lock);
2752 * Start and then stop the MMP threads to ensure the startup and shutdown code
2753 * works properly. Actual protection and property-related code tested via ZTS.
2757 ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2759 ztest_shared_opts_t *zo = &ztest_opts;
2760 spa_t *spa = ztest_spa;
2762 if (zo->zo_mmp_test)
2765 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2766 mutex_enter(&spa->spa_props_lock);
2768 if (!spa_multihost(spa)) {
2769 spa->spa_multihost = B_TRUE;
2770 mmp_thread_start(spa);
2773 mutex_exit(&spa->spa_props_lock);
2774 spa_config_exit(spa, SCL_CONFIG, FTAG);
2776 txg_wait_synced(spa_get_dsl(spa), 0);
2777 mmp_signal_all_threads();
2778 txg_wait_synced(spa_get_dsl(spa), 0);
2780 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2781 mutex_enter(&spa->spa_props_lock);
2783 if (spa_multihost(spa)) {
2784 mmp_thread_stop(spa);
2785 spa->spa_multihost = B_FALSE;
2788 mutex_exit(&spa->spa_props_lock);
2789 spa_config_exit(spa, SCL_CONFIG, FTAG);
2794 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2797 uint64_t initial_version = SPA_VERSION_INITIAL;
2798 uint64_t version, newversion;
2799 nvlist_t *nvroot, *props;
2802 if (ztest_opts.zo_mmp_test)
2805 mutex_enter(&ztest_vdev_lock);
2806 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2809 * Clean up from previous runs.
2811 (void) spa_destroy(name);
2813 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2814 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2817 * If we're configuring a RAIDZ device then make sure that the
2818 * the initial version is capable of supporting that feature.
2820 switch (ztest_opts.zo_raidz_parity) {
2823 initial_version = SPA_VERSION_INITIAL;
2826 initial_version = SPA_VERSION_RAIDZ2;
2829 initial_version = SPA_VERSION_RAIDZ3;
2834 * Create a pool with a spa version that can be upgraded. Pick
2835 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2838 version = ztest_random_spa_version(initial_version);
2839 } while (version > SPA_VERSION_BEFORE_FEATURES);
2841 props = fnvlist_alloc();
2842 fnvlist_add_uint64(props,
2843 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2844 VERIFY3S(spa_create(name, nvroot, props, NULL, NULL), ==, 0);
2845 fnvlist_free(nvroot);
2846 fnvlist_free(props);
2848 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2849 VERIFY3U(spa_version(spa), ==, version);
2850 newversion = ztest_random_spa_version(version + 1);
2852 if (ztest_opts.zo_verbose >= 4) {
2853 (void) printf("upgrading spa version from %llu to %llu\n",
2854 (u_longlong_t)version, (u_longlong_t)newversion);
2857 spa_upgrade(spa, newversion);
2858 VERIFY3U(spa_version(spa), >, version);
2859 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2860 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2861 spa_close(spa, FTAG);
2864 mutex_exit(&ztest_vdev_lock);
2868 vdev_lookup_by_path(vdev_t *vd, const char *path)
2873 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2876 for (c = 0; c < vd->vdev_children; c++)
2877 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2885 * Find the first available hole which can be used as a top-level.
2888 find_vdev_hole(spa_t *spa)
2890 vdev_t *rvd = spa->spa_root_vdev;
2893 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2895 for (c = 0; c < rvd->vdev_children; c++) {
2896 vdev_t *cvd = rvd->vdev_child[c];
2898 if (cvd->vdev_ishole)
2905 * Verify that vdev_add() works as expected.
2909 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2911 ztest_shared_t *zs = ztest_shared;
2912 spa_t *spa = ztest_spa;
2918 if (ztest_opts.zo_mmp_test)
2921 mutex_enter(&ztest_vdev_lock);
2922 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2924 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2926 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2929 * If we have slogs then remove them 1/4 of the time.
2931 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2933 * Grab the guid from the head of the log class rotor.
2935 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2937 spa_config_exit(spa, SCL_VDEV, FTAG);
2940 * We have to grab the zs_name_lock as writer to
2941 * prevent a race between removing a slog (dmu_objset_find)
2942 * and destroying a dataset. Removing the slog will
2943 * grab a reference on the dataset which may cause
2944 * dsl_destroy_head() to fail with EBUSY thus
2945 * leaving the dataset in an inconsistent state.
2947 rw_wrlock(&ztest_name_lock);
2948 error = spa_vdev_remove(spa, guid, B_FALSE);
2949 rw_unlock(&ztest_name_lock);
2951 if (error && error != EEXIST)
2952 fatal(0, "spa_vdev_remove() = %d", error);
2954 spa_config_exit(spa, SCL_VDEV, FTAG);
2957 * Make 1/4 of the devices be log devices.
2959 nvroot = make_vdev_root(NULL, NULL, NULL,
2960 ztest_opts.zo_vdev_size, 0,
2961 ztest_random(4) == 0, ztest_opts.zo_raidz,
2964 error = spa_vdev_add(spa, nvroot);
2965 nvlist_free(nvroot);
2967 if (error == ENOSPC)
2968 ztest_record_enospc("spa_vdev_add");
2969 else if (error != 0)
2970 fatal(0, "spa_vdev_add() = %d", error);
2973 mutex_exit(&ztest_vdev_lock);
2977 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2981 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2983 ztest_shared_t *zs = ztest_shared;
2984 spa_t *spa = ztest_spa;
2985 vdev_t *rvd = spa->spa_root_vdev;
2986 spa_aux_vdev_t *sav;
2992 if (ztest_opts.zo_mmp_test)
2995 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2997 if (ztest_random(2) == 0) {
2998 sav = &spa->spa_spares;
2999 aux = ZPOOL_CONFIG_SPARES;
3001 sav = &spa->spa_l2cache;
3002 aux = ZPOOL_CONFIG_L2CACHE;
3005 mutex_enter(&ztest_vdev_lock);
3007 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3009 if (sav->sav_count != 0 && ztest_random(4) == 0) {
3011 * Pick a random device to remove.
3013 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3016 * Find an unused device we can add.
3018 zs->zs_vdev_aux = 0;
3021 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
3022 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3024 for (c = 0; c < sav->sav_count; c++)
3025 if (strcmp(sav->sav_vdevs[c]->vdev_path,
3028 if (c == sav->sav_count &&
3029 vdev_lookup_by_path(rvd, path) == NULL)
3035 spa_config_exit(spa, SCL_VDEV, FTAG);
3041 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3042 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
3043 error = spa_vdev_add(spa, nvroot);
3045 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
3046 nvlist_free(nvroot);
3049 * Remove an existing device. Sometimes, dirty its
3050 * vdev state first to make sure we handle removal
3051 * of devices that have pending state changes.
3053 if (ztest_random(2) == 0)
3054 (void) vdev_online(spa, guid, 0, NULL);
3056 error = spa_vdev_remove(spa, guid, B_FALSE);
3057 if (error != 0 && error != EBUSY)
3058 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
3061 mutex_exit(&ztest_vdev_lock);
3063 umem_free(path, MAXPATHLEN);
3067 * split a pool if it has mirror tlvdevs
3071 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
3073 ztest_shared_t *zs = ztest_shared;
3074 spa_t *spa = ztest_spa;
3075 vdev_t *rvd = spa->spa_root_vdev;
3076 nvlist_t *tree, **child, *config, *split, **schild;
3077 uint_t c, children, schildren = 0, lastlogid = 0;
3080 if (ztest_opts.zo_mmp_test)
3083 mutex_enter(&ztest_vdev_lock);
3085 /* ensure we have a useable config; mirrors of raidz aren't supported */
3086 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3087 mutex_exit(&ztest_vdev_lock);
3091 /* clean up the old pool, if any */
3092 (void) spa_destroy("splitp");
3094 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3096 /* generate a config from the existing config */
3097 mutex_enter(&spa->spa_props_lock);
3098 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3100 mutex_exit(&spa->spa_props_lock);
3102 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3105 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3106 for (c = 0; c < children; c++) {
3107 vdev_t *tvd = rvd->vdev_child[c];
3111 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3112 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3114 VERIFY(nvlist_add_string(schild[schildren],
3115 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3116 VERIFY(nvlist_add_uint64(schild[schildren],
3117 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3119 lastlogid = schildren;
3124 VERIFY(nvlist_lookup_nvlist_array(child[c],
3125 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3126 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3129 /* OK, create a config that can be used to split */
3130 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3131 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3132 VDEV_TYPE_ROOT) == 0);
3133 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3134 lastlogid != 0 ? lastlogid : schildren) == 0);
3136 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3137 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3139 for (c = 0; c < schildren; c++)
3140 nvlist_free(schild[c]);
3144 spa_config_exit(spa, SCL_VDEV, FTAG);
3146 (void) rw_wrlock(&ztest_name_lock);
3147 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3148 (void) rw_unlock(&ztest_name_lock);
3150 nvlist_free(config);
3153 (void) printf("successful split - results:\n");
3154 mutex_enter(&spa_namespace_lock);
3155 show_pool_stats(spa);
3156 show_pool_stats(spa_lookup("splitp"));
3157 mutex_exit(&spa_namespace_lock);
3161 mutex_exit(&ztest_vdev_lock);
3166 * Verify that we can attach and detach devices.
3170 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3172 ztest_shared_t *zs = ztest_shared;
3173 spa_t *spa = ztest_spa;
3174 spa_aux_vdev_t *sav = &spa->spa_spares;
3175 vdev_t *rvd = spa->spa_root_vdev;
3176 vdev_t *oldvd, *newvd, *pvd;
3180 uint64_t ashift = ztest_get_ashift();
3181 uint64_t oldguid, pguid;
3182 uint64_t oldsize, newsize;
3183 char *oldpath, *newpath;
3185 int oldvd_has_siblings = B_FALSE;
3186 int newvd_is_spare = B_FALSE;
3188 int error, expected_error;
3190 if (ztest_opts.zo_mmp_test)
3193 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3194 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3196 mutex_enter(&ztest_vdev_lock);
3197 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3199 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3202 * If a vdev is in the process of being removed, its removal may
3203 * finish while we are in progress, leading to an unexpected error
3204 * value. Don't bother trying to attach while we are in the middle
3207 if (ztest_device_removal_active) {
3208 spa_config_exit(spa, SCL_ALL, FTAG);
3209 mutex_exit(&ztest_vdev_lock);
3214 * Decide whether to do an attach or a replace.
3216 replacing = ztest_random(2);
3219 * Pick a random top-level vdev.
3221 top = ztest_random_vdev_top(spa, B_TRUE);
3224 * Pick a random leaf within it.
3226 leaf = ztest_random(leaves);
3231 oldvd = rvd->vdev_child[top];
3232 if (zs->zs_mirrors >= 1) {
3233 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3234 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3235 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3237 if (ztest_opts.zo_raidz > 1) {
3238 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3239 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3240 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3244 * If we're already doing an attach or replace, oldvd may be a
3245 * mirror vdev -- in which case, pick a random child.
3247 while (oldvd->vdev_children != 0) {
3248 oldvd_has_siblings = B_TRUE;
3249 ASSERT(oldvd->vdev_children >= 2);
3250 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3253 oldguid = oldvd->vdev_guid;
3254 oldsize = vdev_get_min_asize(oldvd);
3255 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3256 (void) strcpy(oldpath, oldvd->vdev_path);
3257 pvd = oldvd->vdev_parent;
3258 pguid = pvd->vdev_guid;
3261 * If oldvd has siblings, then half of the time, detach it.
3263 if (oldvd_has_siblings && ztest_random(2) == 0) {
3264 spa_config_exit(spa, SCL_ALL, FTAG);
3265 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3266 if (error != 0 && error != ENODEV && error != EBUSY &&
3268 fatal(0, "detach (%s) returned %d", oldpath, error);
3273 * For the new vdev, choose with equal probability between the two
3274 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3276 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3277 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3278 newvd_is_spare = B_TRUE;
3279 (void) strcpy(newpath, newvd->vdev_path);
3281 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3282 ztest_opts.zo_dir, ztest_opts.zo_pool,
3283 top * leaves + leaf);
3284 if (ztest_random(2) == 0)
3285 newpath[strlen(newpath) - 1] = 'b';
3286 newvd = vdev_lookup_by_path(rvd, newpath);
3291 * Reopen to ensure the vdev's asize field isn't stale.
3294 newsize = vdev_get_min_asize(newvd);
3297 * Make newsize a little bigger or smaller than oldsize.
3298 * If it's smaller, the attach should fail.
3299 * If it's larger, and we're doing a replace,
3300 * we should get dynamic LUN growth when we're done.
3302 newsize = 10 * oldsize / (9 + ztest_random(3));
3306 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3307 * unless it's a replace; in that case any non-replacing parent is OK.
3309 * If newvd is already part of the pool, it should fail with EBUSY.
3311 * If newvd is too small, it should fail with EOVERFLOW.
3313 if (pvd->vdev_ops != &vdev_mirror_ops &&
3314 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3315 pvd->vdev_ops == &vdev_replacing_ops ||
3316 pvd->vdev_ops == &vdev_spare_ops))
3317 expected_error = ENOTSUP;
3318 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3319 expected_error = ENOTSUP;
3320 else if (newvd == oldvd)
3321 expected_error = replacing ? 0 : EBUSY;
3322 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3323 expected_error = EBUSY;
3324 else if (newsize < oldsize)
3325 expected_error = EOVERFLOW;
3326 else if (ashift > oldvd->vdev_top->vdev_ashift)
3327 expected_error = EDOM;
3331 spa_config_exit(spa, SCL_ALL, FTAG);
3334 * Build the nvlist describing newpath.
3336 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3337 ashift, 0, 0, 0, 1);
3339 error = spa_vdev_attach(spa, oldguid, root, replacing);
3344 * If our parent was the replacing vdev, but the replace completed,
3345 * then instead of failing with ENOTSUP we may either succeed,
3346 * fail with ENODEV, or fail with EOVERFLOW.
3348 if (expected_error == ENOTSUP &&
3349 (error == 0 || error == ENODEV || error == EOVERFLOW))
3350 expected_error = error;
3353 * If someone grew the LUN, the replacement may be too small.
3355 if (error == EOVERFLOW || error == EBUSY)
3356 expected_error = error;
3358 /* XXX workaround 6690467 */
3359 if (error != expected_error && expected_error != EBUSY) {
3360 fatal(0, "attach (%s %llu, %s %llu, %d) "
3361 "returned %d, expected %d",
3362 oldpath, oldsize, newpath,
3363 newsize, replacing, error, expected_error);
3366 mutex_exit(&ztest_vdev_lock);
3368 umem_free(oldpath, MAXPATHLEN);
3369 umem_free(newpath, MAXPATHLEN);
3374 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3376 spa_t *spa = ztest_spa;
3381 mutex_enter(&ztest_vdev_lock);
3383 if (ztest_device_removal_active) {
3384 mutex_exit(&ztest_vdev_lock);
3389 * Remove a random top-level vdev and wait for removal to finish.
3391 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3392 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3393 guid = vd->vdev_guid;
3394 spa_config_exit(spa, SCL_VDEV, FTAG);
3396 error = spa_vdev_remove(spa, guid, B_FALSE);
3398 ztest_device_removal_active = B_TRUE;
3399 mutex_exit(&ztest_vdev_lock);
3401 while (spa->spa_vdev_removal != NULL)
3402 txg_wait_synced(spa_get_dsl(spa), 0);
3404 mutex_exit(&ztest_vdev_lock);
3409 * The pool needs to be scrubbed after completing device removal.
3410 * Failure to do so may result in checksum errors due to the
3411 * strategy employed by ztest_fault_inject() when selecting which
3412 * offset are redundant and can be damaged.
3414 error = spa_scan(spa, POOL_SCAN_SCRUB);
3416 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3417 txg_wait_synced(spa_get_dsl(spa), 0);
3420 mutex_enter(&ztest_vdev_lock);
3421 ztest_device_removal_active = B_FALSE;
3422 mutex_exit(&ztest_vdev_lock);
3426 * Callback function which expands the physical size of the vdev.
3429 grow_vdev(vdev_t *vd, void *arg)
3431 ASSERTV(spa_t *spa = vd->vdev_spa);
3432 size_t *newsize = arg;
3436 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3437 ASSERT(vd->vdev_ops->vdev_op_leaf);
3439 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3442 fsize = lseek(fd, 0, SEEK_END);
3443 VERIFY(ftruncate(fd, *newsize) == 0);
3445 if (ztest_opts.zo_verbose >= 6) {
3446 (void) printf("%s grew from %lu to %lu bytes\n",
3447 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3454 * Callback function which expands a given vdev by calling vdev_online().
3458 online_vdev(vdev_t *vd, void *arg)
3460 spa_t *spa = vd->vdev_spa;
3461 vdev_t *tvd = vd->vdev_top;
3462 uint64_t guid = vd->vdev_guid;
3463 uint64_t generation = spa->spa_config_generation + 1;
3464 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3467 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3468 ASSERT(vd->vdev_ops->vdev_op_leaf);
3470 /* Calling vdev_online will initialize the new metaslabs */
3471 spa_config_exit(spa, SCL_STATE, spa);
3472 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3473 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3476 * If vdev_online returned an error or the underlying vdev_open
3477 * failed then we abort the expand. The only way to know that
3478 * vdev_open fails is by checking the returned newstate.
3480 if (error || newstate != VDEV_STATE_HEALTHY) {
3481 if (ztest_opts.zo_verbose >= 5) {
3482 (void) printf("Unable to expand vdev, state %llu, "
3483 "error %d\n", (u_longlong_t)newstate, error);
3487 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3490 * Since we dropped the lock we need to ensure that we're
3491 * still talking to the original vdev. It's possible this
3492 * vdev may have been detached/replaced while we were
3493 * trying to online it.
3495 if (generation != spa->spa_config_generation) {
3496 if (ztest_opts.zo_verbose >= 5) {
3497 (void) printf("vdev configuration has changed, "
3498 "guid %llu, state %llu, expected gen %llu, "
3501 (u_longlong_t)tvd->vdev_state,
3502 (u_longlong_t)generation,
3503 (u_longlong_t)spa->spa_config_generation);
3511 * Traverse the vdev tree calling the supplied function.
3512 * We continue to walk the tree until we either have walked all
3513 * children or we receive a non-NULL return from the callback.
3514 * If a NULL callback is passed, then we just return back the first
3515 * leaf vdev we encounter.
3518 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3522 if (vd->vdev_ops->vdev_op_leaf) {
3526 return (func(vd, arg));
3529 for (c = 0; c < vd->vdev_children; c++) {
3530 vdev_t *cvd = vd->vdev_child[c];
3531 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3538 * Verify that dynamic LUN growth works as expected.
3542 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3544 spa_t *spa = ztest_spa;
3546 metaslab_class_t *mc;
3547 metaslab_group_t *mg;
3548 size_t psize, newsize;
3550 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3552 mutex_enter(&ztest_vdev_lock);
3553 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3556 * If there is a vdev removal in progress, it could complete while
3557 * we are running, in which case we would not be able to verify
3558 * that the metaslab_class space increased (because it decreases
3559 * when the device removal completes).
3561 if (ztest_device_removal_active) {
3562 spa_config_exit(spa, SCL_STATE, FTAG);
3563 mutex_exit(&ztest_vdev_lock);
3567 top = ztest_random_vdev_top(spa, B_TRUE);
3569 tvd = spa->spa_root_vdev->vdev_child[top];
3572 old_ms_count = tvd->vdev_ms_count;
3573 old_class_space = metaslab_class_get_space(mc);
3576 * Determine the size of the first leaf vdev associated with
3577 * our top-level device.
3579 vd = vdev_walk_tree(tvd, NULL, NULL);
3580 ASSERT3P(vd, !=, NULL);
3581 ASSERT(vd->vdev_ops->vdev_op_leaf);
3583 psize = vd->vdev_psize;
3586 * We only try to expand the vdev if it's healthy, less than 4x its
3587 * original size, and it has a valid psize.
3589 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3590 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3591 spa_config_exit(spa, SCL_STATE, spa);
3592 mutex_exit(&ztest_vdev_lock);
3596 newsize = psize + psize / 8;
3597 ASSERT3U(newsize, >, psize);
3599 if (ztest_opts.zo_verbose >= 6) {
3600 (void) printf("Expanding LUN %s from %lu to %lu\n",
3601 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3605 * Growing the vdev is a two step process:
3606 * 1). expand the physical size (i.e. relabel)
3607 * 2). online the vdev to create the new metaslabs
3609 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3610 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3611 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3612 if (ztest_opts.zo_verbose >= 5) {
3613 (void) printf("Could not expand LUN because "
3614 "the vdev configuration changed.\n");
3616 spa_config_exit(spa, SCL_STATE, spa);
3617 mutex_exit(&ztest_vdev_lock);
3621 spa_config_exit(spa, SCL_STATE, spa);
3624 * Expanding the LUN will update the config asynchronously,
3625 * thus we must wait for the async thread to complete any
3626 * pending tasks before proceeding.
3630 mutex_enter(&spa->spa_async_lock);
3631 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3632 mutex_exit(&spa->spa_async_lock);
3635 txg_wait_synced(spa_get_dsl(spa), 0);
3636 (void) poll(NULL, 0, 100);
3639 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3641 tvd = spa->spa_root_vdev->vdev_child[top];
3642 new_ms_count = tvd->vdev_ms_count;
3643 new_class_space = metaslab_class_get_space(mc);
3645 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3646 if (ztest_opts.zo_verbose >= 5) {
3647 (void) printf("Could not verify LUN expansion due to "
3648 "intervening vdev offline or remove.\n");
3650 spa_config_exit(spa, SCL_STATE, spa);
3651 mutex_exit(&ztest_vdev_lock);
3656 * Make sure we were able to grow the vdev.
3658 if (new_ms_count <= old_ms_count) {
3659 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3660 old_ms_count, new_ms_count);
3664 * Make sure we were able to grow the pool.
3666 if (new_class_space <= old_class_space) {
3667 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3668 old_class_space, new_class_space);
3671 if (ztest_opts.zo_verbose >= 5) {
3672 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3674 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3675 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3676 (void) printf("%s grew from %s to %s\n",
3677 spa->spa_name, oldnumbuf, newnumbuf);
3680 spa_config_exit(spa, SCL_STATE, spa);
3681 mutex_exit(&ztest_vdev_lock);
3685 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3689 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3692 * Create the objects common to all ztest datasets.
3694 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3695 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3699 ztest_dataset_create(char *dsname)
3703 dsl_crypto_params_t *dcp = NULL;
3706 * 50% of the time, we create encrypted datasets
3707 * using a random cipher suite and a hard-coded
3710 rand = ztest_random(2);
3712 nvlist_t *crypto_args = fnvlist_alloc();
3713 nvlist_t *props = fnvlist_alloc();
3715 /* slight bias towards the default cipher suite */
3716 rand = ztest_random(ZIO_CRYPT_FUNCTIONS);
3717 if (rand < ZIO_CRYPT_AES_128_CCM)
3718 rand = ZIO_CRYPT_ON;
3720 fnvlist_add_uint64(props,
3721 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), rand);
3722 fnvlist_add_uint8_array(crypto_args, "wkeydata",
3723 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
3726 * These parameters aren't really used by the kernel. They
3727 * are simply stored so that userspace knows how to load
3730 fnvlist_add_uint64(props,
3731 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), ZFS_KEYFORMAT_RAW);
3732 fnvlist_add_string(props,
3733 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), "prompt");
3734 fnvlist_add_uint64(props,
3735 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 0ULL);
3736 fnvlist_add_uint64(props,
3737 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 0ULL);
3739 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, props,
3740 crypto_args, &dcp));
3742 fnvlist_free(crypto_args);
3743 fnvlist_free(props);
3746 err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, dcp,
3747 ztest_objset_create_cb, NULL);
3748 dsl_crypto_params_free(dcp, !!err);
3750 rand = ztest_random(100);
3751 if (err || rand < 80)
3754 if (ztest_opts.zo_verbose >= 5)
3755 (void) printf("Setting dataset %s to sync always\n", dsname);
3756 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3757 ZFS_SYNC_ALWAYS, B_FALSE));
3762 ztest_objset_destroy_cb(const char *name, void *arg)
3765 dmu_object_info_t doi;
3769 * Verify that the dataset contains a directory object.
3771 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3772 B_TRUE, FTAG, &os));
3773 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3774 if (error != ENOENT) {
3775 /* We could have crashed in the middle of destroying it */
3777 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3778 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3780 dmu_objset_disown(os, B_TRUE, FTAG);
3783 * Destroy the dataset.
3785 if (strchr(name, '@') != NULL) {
3786 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
3788 error = dsl_destroy_head(name);
3789 /* There could be a hold on this dataset */
3797 ztest_snapshot_create(char *osname, uint64_t id)
3799 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3802 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3804 error = dmu_objset_snapshot_one(osname, snapname);
3805 if (error == ENOSPC) {
3806 ztest_record_enospc(FTAG);
3809 if (error != 0 && error != EEXIST) {
3810 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3817 ztest_snapshot_destroy(char *osname, uint64_t id)
3819 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3822 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3825 error = dsl_destroy_snapshot(snapname, B_FALSE);
3826 if (error != 0 && error != ENOENT)
3827 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3833 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3839 char name[ZFS_MAX_DATASET_NAME_LEN];
3843 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3845 (void) rw_rdlock(&ztest_name_lock);
3847 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3848 ztest_opts.zo_pool, (u_longlong_t)id);
3851 * If this dataset exists from a previous run, process its replay log
3852 * half of the time. If we don't replay it, then dsl_destroy_head()
3853 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3855 if (ztest_random(2) == 0 &&
3856 ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3857 B_TRUE, FTAG, &os) == 0) {
3858 ztest_zd_init(zdtmp, NULL, os);
3859 zil_replay(os, zdtmp, ztest_replay_vector);
3860 ztest_zd_fini(zdtmp);
3861 txg_wait_synced(dmu_objset_pool(os), 0);
3862 dmu_objset_disown(os, B_TRUE, FTAG);
3866 * There may be an old instance of the dataset we're about to
3867 * create lying around from a previous run. If so, destroy it
3868 * and all of its snapshots.
3870 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3871 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3874 * Verify that the destroyed dataset is no longer in the namespace.
3876 VERIFY3U(ENOENT, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3877 B_TRUE, FTAG, &os));
3880 * Verify that we can create a new dataset.
3882 error = ztest_dataset_create(name);
3884 if (error == ENOSPC) {
3885 ztest_record_enospc(FTAG);
3888 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3891 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3894 ztest_zd_init(zdtmp, NULL, os);
3897 * Open the intent log for it.
3899 zilog = zil_open(os, ztest_get_data);
3902 * Put some objects in there, do a little I/O to them,
3903 * and randomly take a couple of snapshots along the way.
3905 iters = ztest_random(5);
3906 for (i = 0; i < iters; i++) {
3907 ztest_dmu_object_alloc_free(zdtmp, id);
3908 if (ztest_random(iters) == 0)
3909 (void) ztest_snapshot_create(name, i);
3913 * Verify that we cannot create an existing dataset.
3915 VERIFY3U(EEXIST, ==,
3916 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
3919 * Verify that we can hold an objset that is also owned.
3921 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3922 dmu_objset_rele(os2, FTAG);
3925 * Verify that we cannot own an objset that is already owned.
3927 VERIFY3U(EBUSY, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER,
3928 B_FALSE, B_TRUE, FTAG, &os2));
3931 txg_wait_synced(spa_get_dsl(os->os_spa), 0);
3932 dmu_objset_disown(os, B_TRUE, FTAG);
3933 ztest_zd_fini(zdtmp);
3935 (void) rw_unlock(&ztest_name_lock);
3937 umem_free(zdtmp, sizeof (ztest_ds_t));
3941 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3944 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3946 (void) rw_rdlock(&ztest_name_lock);
3947 (void) ztest_snapshot_destroy(zd->zd_name, id);
3948 (void) ztest_snapshot_create(zd->zd_name, id);
3949 (void) rw_unlock(&ztest_name_lock);
3953 * Cleanup non-standard snapshots and clones.
3956 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3965 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3966 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3967 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3968 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3969 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3971 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3972 "%s@s1_%llu", osname, (u_longlong_t)id);
3973 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3974 "%s/c1_%llu", osname, (u_longlong_t)id);
3975 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3976 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3977 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3978 "%s/c2_%llu", osname, (u_longlong_t)id);
3979 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3980 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3982 error = dsl_destroy_head(clone2name);
3983 if (error && error != ENOENT)
3984 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3985 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3986 if (error && error != ENOENT)
3987 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3988 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3989 if (error && error != ENOENT)
3990 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3991 error = dsl_destroy_head(clone1name);
3992 if (error && error != ENOENT)
3993 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3994 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3995 if (error && error != ENOENT)
3996 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3998 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3999 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
4000 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
4001 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
4002 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
4006 * Verify dsl_dataset_promote handles EBUSY
4009 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
4017 char *osname = zd->zd_name;
4020 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4021 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4022 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4023 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4024 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
4026 (void) rw_rdlock(&ztest_name_lock);
4028 ztest_dsl_dataset_cleanup(osname, id);
4030 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
4031 "%s@s1_%llu", osname, (u_longlong_t)id);
4032 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
4033 "%s/c1_%llu", osname, (u_longlong_t)id);
4034 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
4035 "%s@s2_%llu", clone1name, (u_longlong_t)id);
4036 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
4037 "%s/c2_%llu", osname, (u_longlong_t)id);
4038 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
4039 "%s@s3_%llu", clone1name, (u_longlong_t)id);
4041 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
4042 if (error && error != EEXIST) {
4043 if (error == ENOSPC) {
4044 ztest_record_enospc(FTAG);
4047 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
4050 error = dmu_objset_clone(clone1name, snap1name);
4052 if (error == ENOSPC) {
4053 ztest_record_enospc(FTAG);
4056 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
4059 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4060 if (error && error != EEXIST) {
4061 if (error == ENOSPC) {
4062 ztest_record_enospc(FTAG);
4065 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4068 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4069 if (error && error != EEXIST) {
4070 if (error == ENOSPC) {
4071 ztest_record_enospc(FTAG);
4074 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4077 error = dmu_objset_clone(clone2name, snap3name);
4079 if (error == ENOSPC) {
4080 ztest_record_enospc(FTAG);
4083 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4086 error = ztest_dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
4089 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4090 error = dsl_dataset_promote(clone2name, NULL);
4091 if (error == ENOSPC) {
4092 dmu_objset_disown(os, B_TRUE, FTAG);
4093 ztest_record_enospc(FTAG);
4097 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
4099 dmu_objset_disown(os, B_TRUE, FTAG);
4102 ztest_dsl_dataset_cleanup(osname, id);
4104 (void) rw_unlock(&ztest_name_lock);
4106 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
4107 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
4108 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
4109 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
4110 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
4113 #undef OD_ARRAY_SIZE
4114 #define OD_ARRAY_SIZE 4
4117 * Verify that dmu_object_{alloc,free} work as expected.
4120 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4127 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4128 od = umem_alloc(size, UMEM_NOFAIL);
4129 batchsize = OD_ARRAY_SIZE;
4131 for (b = 0; b < batchsize; b++)
4132 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
4136 * Destroy the previous batch of objects, create a new batch,
4137 * and do some I/O on the new objects.
4139 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
4142 while (ztest_random(4 * batchsize) != 0)
4143 ztest_io(zd, od[ztest_random(batchsize)].od_object,
4144 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4146 umem_free(od, size);
4150 * Rewind the global allocator to verify object allocation backfilling.
4153 ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
4155 objset_t *os = zd->zd_os;
4156 int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
4160 * Rewind the global allocator randomly back to a lower object number
4161 * to force backfilling and reclamation of recently freed dnodes.
4163 mutex_enter(&os->os_obj_lock);
4164 object = ztest_random(os->os_obj_next_chunk);
4165 os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
4166 mutex_exit(&os->os_obj_lock);
4169 #undef OD_ARRAY_SIZE
4170 #define OD_ARRAY_SIZE 2
4173 * Verify that dmu_{read,write} work as expected.
4176 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4181 objset_t *os = zd->zd_os;
4182 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4183 od = umem_alloc(size, UMEM_NOFAIL);
4185 int i, freeit, error;
4187 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4188 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4189 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4190 uint64_t regions = 997;
4191 uint64_t stride = 123456789ULL;
4192 uint64_t width = 40;
4193 int free_percent = 5;
4196 * This test uses two objects, packobj and bigobj, that are always
4197 * updated together (i.e. in the same tx) so that their contents are
4198 * in sync and can be compared. Their contents relate to each other
4199 * in a simple way: packobj is a dense array of 'bufwad' structures,
4200 * while bigobj is a sparse array of the same bufwads. Specifically,
4201 * for any index n, there are three bufwads that should be identical:
4203 * packobj, at offset n * sizeof (bufwad_t)
4204 * bigobj, at the head of the nth chunk
4205 * bigobj, at the tail of the nth chunk
4207 * The chunk size is arbitrary. It doesn't have to be a power of two,
4208 * and it doesn't have any relation to the object blocksize.
4209 * The only requirement is that it can hold at least two bufwads.
4211 * Normally, we write the bufwad to each of these locations.
4212 * However, free_percent of the time we instead write zeroes to
4213 * packobj and perform a dmu_free_range() on bigobj. By comparing
4214 * bigobj to packobj, we can verify that the DMU is correctly
4215 * tracking which parts of an object are allocated and free,
4216 * and that the contents of the allocated blocks are correct.
4220 * Read the directory info. If it's the first time, set things up.
4222 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
4223 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4226 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4227 umem_free(od, size);
4231 bigobj = od[0].od_object;
4232 packobj = od[1].od_object;
4233 chunksize = od[0].od_gen;
4234 ASSERT(chunksize == od[1].od_gen);
4237 * Prefetch a random chunk of the big object.
4238 * Our aim here is to get some async reads in flight
4239 * for blocks that we may free below; the DMU should
4240 * handle this race correctly.
4242 n = ztest_random(regions) * stride + ztest_random(width);
4243 s = 1 + ztest_random(2 * width - 1);
4244 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4245 ZIO_PRIORITY_SYNC_READ);
4248 * Pick a random index and compute the offsets into packobj and bigobj.
4250 n = ztest_random(regions) * stride + ztest_random(width);
4251 s = 1 + ztest_random(width - 1);
4253 packoff = n * sizeof (bufwad_t);
4254 packsize = s * sizeof (bufwad_t);
4256 bigoff = n * chunksize;
4257 bigsize = s * chunksize;
4259 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4260 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4263 * free_percent of the time, free a range of bigobj rather than
4266 freeit = (ztest_random(100) < free_percent);
4269 * Read the current contents of our objects.
4271 error = dmu_read(os, packobj, packoff, packsize, packbuf,
4274 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
4279 * Get a tx for the mods to both packobj and bigobj.
4281 tx = dmu_tx_create(os);
4283 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4286 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4288 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4290 /* This accounts for setting the checksum/compression. */
4291 dmu_tx_hold_bonus(tx, bigobj);
4293 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4295 umem_free(packbuf, packsize);
4296 umem_free(bigbuf, bigsize);
4297 umem_free(od, size);
4301 enum zio_checksum cksum;
4303 cksum = (enum zio_checksum)
4304 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4305 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4306 dmu_object_set_checksum(os, bigobj, cksum, tx);
4308 enum zio_compress comp;
4310 comp = (enum zio_compress)
4311 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4312 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4313 dmu_object_set_compress(os, bigobj, comp, tx);
4316 * For each index from n to n + s, verify that the existing bufwad
4317 * in packobj matches the bufwads at the head and tail of the
4318 * corresponding chunk in bigobj. Then update all three bufwads
4319 * with the new values we want to write out.
4321 for (i = 0; i < s; i++) {
4323 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4325 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4327 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4329 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4330 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4332 if (pack->bw_txg > txg)
4333 fatal(0, "future leak: got %llx, open txg is %llx",
4336 if (pack->bw_data != 0 && pack->bw_index != n + i)
4337 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4338 pack->bw_index, n, i);
4340 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4341 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4343 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4344 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4347 bzero(pack, sizeof (bufwad_t));
4349 pack->bw_index = n + i;
4351 pack->bw_data = 1 + ztest_random(-2ULL);
4358 * We've verified all the old bufwads, and made new ones.
4359 * Now write them out.
4361 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4364 if (ztest_opts.zo_verbose >= 7) {
4365 (void) printf("freeing offset %llx size %llx"
4367 (u_longlong_t)bigoff,
4368 (u_longlong_t)bigsize,
4371 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4373 if (ztest_opts.zo_verbose >= 7) {
4374 (void) printf("writing offset %llx size %llx"
4376 (u_longlong_t)bigoff,
4377 (u_longlong_t)bigsize,
4380 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4386 * Sanity check the stuff we just wrote.
4389 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4390 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4392 VERIFY(0 == dmu_read(os, packobj, packoff,
4393 packsize, packcheck, DMU_READ_PREFETCH));
4394 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4395 bigsize, bigcheck, DMU_READ_PREFETCH));
4397 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4398 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4400 umem_free(packcheck, packsize);
4401 umem_free(bigcheck, bigsize);
4404 umem_free(packbuf, packsize);
4405 umem_free(bigbuf, bigsize);
4406 umem_free(od, size);
4410 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4411 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4419 * For each index from n to n + s, verify that the existing bufwad
4420 * in packobj matches the bufwads at the head and tail of the
4421 * corresponding chunk in bigobj. Then update all three bufwads
4422 * with the new values we want to write out.
4424 for (i = 0; i < s; i++) {
4426 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4428 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4430 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4432 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4433 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4435 if (pack->bw_txg > txg)
4436 fatal(0, "future leak: got %llx, open txg is %llx",
4439 if (pack->bw_data != 0 && pack->bw_index != n + i)
4440 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4441 pack->bw_index, n, i);
4443 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4444 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4446 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4447 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4449 pack->bw_index = n + i;
4451 pack->bw_data = 1 + ztest_random(-2ULL);
4458 #undef OD_ARRAY_SIZE
4459 #define OD_ARRAY_SIZE 2
4462 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4464 objset_t *os = zd->zd_os;
4471 bufwad_t *packbuf, *bigbuf;
4472 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4473 uint64_t blocksize = ztest_random_blocksize();
4474 uint64_t chunksize = blocksize;
4475 uint64_t regions = 997;
4476 uint64_t stride = 123456789ULL;
4478 dmu_buf_t *bonus_db;
4479 arc_buf_t **bigbuf_arcbufs;
4480 dmu_object_info_t doi;
4482 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4483 od = umem_alloc(size, UMEM_NOFAIL);
4486 * This test uses two objects, packobj and bigobj, that are always
4487 * updated together (i.e. in the same tx) so that their contents are
4488 * in sync and can be compared. Their contents relate to each other
4489 * in a simple way: packobj is a dense array of 'bufwad' structures,
4490 * while bigobj is a sparse array of the same bufwads. Specifically,
4491 * for any index n, there are three bufwads that should be identical:
4493 * packobj, at offset n * sizeof (bufwad_t)
4494 * bigobj, at the head of the nth chunk
4495 * bigobj, at the tail of the nth chunk
4497 * The chunk size is set equal to bigobj block size so that
4498 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4502 * Read the directory info. If it's the first time, set things up.
4504 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4505 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4509 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4510 umem_free(od, size);
4514 bigobj = od[0].od_object;
4515 packobj = od[1].od_object;
4516 blocksize = od[0].od_blocksize;
4517 chunksize = blocksize;
4518 ASSERT(chunksize == od[1].od_gen);
4520 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4521 VERIFY(ISP2(doi.doi_data_block_size));
4522 VERIFY(chunksize == doi.doi_data_block_size);
4523 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4526 * Pick a random index and compute the offsets into packobj and bigobj.
4528 n = ztest_random(regions) * stride + ztest_random(width);
4529 s = 1 + ztest_random(width - 1);
4531 packoff = n * sizeof (bufwad_t);
4532 packsize = s * sizeof (bufwad_t);
4534 bigoff = n * chunksize;
4535 bigsize = s * chunksize;
4537 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4538 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4540 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4542 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4545 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4546 * Iteration 1 test zcopy to already referenced dbufs.
4547 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4548 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4549 * Iteration 4 test zcopy when dbuf is no longer dirty.
4550 * Iteration 5 test zcopy when it can't be done.
4551 * Iteration 6 one more zcopy write.
4553 for (i = 0; i < 7; i++) {
4558 * In iteration 5 (i == 5) use arcbufs
4559 * that don't match bigobj blksz to test
4560 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4561 * assign an arcbuf to a dbuf.
4563 for (j = 0; j < s; j++) {
4564 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4566 dmu_request_arcbuf(bonus_db, chunksize);
4568 bigbuf_arcbufs[2 * j] =
4569 dmu_request_arcbuf(bonus_db, chunksize / 2);
4570 bigbuf_arcbufs[2 * j + 1] =
4571 dmu_request_arcbuf(bonus_db, chunksize / 2);
4576 * Get a tx for the mods to both packobj and bigobj.
4578 tx = dmu_tx_create(os);
4580 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4581 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4583 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4585 umem_free(packbuf, packsize);
4586 umem_free(bigbuf, bigsize);
4587 for (j = 0; j < s; j++) {
4589 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4590 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4593 bigbuf_arcbufs[2 * j]);
4595 bigbuf_arcbufs[2 * j + 1]);
4598 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4599 umem_free(od, size);
4600 dmu_buf_rele(bonus_db, FTAG);
4605 * 50% of the time don't read objects in the 1st iteration to
4606 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4607 * no existing dbufs for the specified offsets.
4609 if (i != 0 || ztest_random(2) != 0) {
4610 error = dmu_read(os, packobj, packoff,
4611 packsize, packbuf, DMU_READ_PREFETCH);
4613 error = dmu_read(os, bigobj, bigoff, bigsize,
4614 bigbuf, DMU_READ_PREFETCH);
4617 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4621 * We've verified all the old bufwads, and made new ones.
4622 * Now write them out.
4624 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4625 if (ztest_opts.zo_verbose >= 7) {
4626 (void) printf("writing offset %llx size %llx"
4628 (u_longlong_t)bigoff,
4629 (u_longlong_t)bigsize,
4632 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4634 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4635 bcopy((caddr_t)bigbuf + (off - bigoff),
4636 bigbuf_arcbufs[j]->b_data, chunksize);
4638 bcopy((caddr_t)bigbuf + (off - bigoff),
4639 bigbuf_arcbufs[2 * j]->b_data,
4641 bcopy((caddr_t)bigbuf + (off - bigoff) +
4643 bigbuf_arcbufs[2 * j + 1]->b_data,
4648 VERIFY(dmu_buf_hold(os, bigobj, off,
4649 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4651 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4652 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4653 bigbuf_arcbufs[j], tx);
4655 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4656 bigbuf_arcbufs[2 * j], tx);
4657 dmu_assign_arcbuf_by_dbuf(bonus_db,
4658 off + chunksize / 2,
4659 bigbuf_arcbufs[2 * j + 1], tx);
4662 dmu_buf_rele(dbt, FTAG);
4668 * Sanity check the stuff we just wrote.
4671 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4672 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4674 VERIFY(0 == dmu_read(os, packobj, packoff,
4675 packsize, packcheck, DMU_READ_PREFETCH));
4676 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4677 bigsize, bigcheck, DMU_READ_PREFETCH));
4679 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4680 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4682 umem_free(packcheck, packsize);
4683 umem_free(bigcheck, bigsize);
4686 txg_wait_open(dmu_objset_pool(os), 0);
4687 } else if (i == 3) {
4688 txg_wait_synced(dmu_objset_pool(os), 0);
4692 dmu_buf_rele(bonus_db, FTAG);
4693 umem_free(packbuf, packsize);
4694 umem_free(bigbuf, bigsize);
4695 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4696 umem_free(od, size);
4701 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4705 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4706 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4707 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4710 * Have multiple threads write to large offsets in an object
4711 * to verify that parallel writes to an object -- even to the
4712 * same blocks within the object -- doesn't cause any trouble.
4714 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4716 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4719 while (ztest_random(10) != 0)
4720 ztest_io(zd, od->od_object, offset);
4722 umem_free(od, sizeof (ztest_od_t));
4726 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4729 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4730 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4731 uint64_t count = ztest_random(20) + 1;
4732 uint64_t blocksize = ztest_random_blocksize();
4735 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4737 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4739 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4740 !ztest_random(2)) != 0) {
4741 umem_free(od, sizeof (ztest_od_t));
4745 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4746 umem_free(od, sizeof (ztest_od_t));
4750 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4752 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4754 while (ztest_random(count) != 0) {
4755 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4756 if (ztest_write(zd, od->od_object, randoff, blocksize,
4759 while (ztest_random(4) != 0)
4760 ztest_io(zd, od->od_object, randoff);
4763 umem_free(data, blocksize);
4764 umem_free(od, sizeof (ztest_od_t));
4768 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4770 #define ZTEST_ZAP_MIN_INTS 1
4771 #define ZTEST_ZAP_MAX_INTS 4
4772 #define ZTEST_ZAP_MAX_PROPS 1000
4775 ztest_zap(ztest_ds_t *zd, uint64_t id)
4777 objset_t *os = zd->zd_os;
4780 uint64_t txg, last_txg;
4781 uint64_t value[ZTEST_ZAP_MAX_INTS];
4782 uint64_t zl_ints, zl_intsize, prop;
4785 char propname[100], txgname[100];
4787 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4789 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4790 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4792 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4793 !ztest_random(2)) != 0)
4796 object = od->od_object;
4799 * Generate a known hash collision, and verify that
4800 * we can lookup and remove both entries.
4802 tx = dmu_tx_create(os);
4803 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4804 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4807 for (i = 0; i < 2; i++) {
4809 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4812 for (i = 0; i < 2; i++) {
4813 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4814 sizeof (uint64_t), 1, &value[i], tx));
4816 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4817 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4818 ASSERT3U(zl_ints, ==, 1);
4820 for (i = 0; i < 2; i++) {
4821 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4826 * Generate a buch of random entries.
4828 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4830 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4831 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4832 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4833 bzero(value, sizeof (value));
4837 * If these zap entries already exist, validate their contents.
4839 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4841 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4842 ASSERT3U(zl_ints, ==, 1);
4844 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4845 zl_ints, &last_txg) == 0);
4847 VERIFY(zap_length(os, object, propname, &zl_intsize,
4850 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4851 ASSERT3U(zl_ints, ==, ints);
4853 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4854 zl_ints, value) == 0);
4856 for (i = 0; i < ints; i++) {
4857 ASSERT3U(value[i], ==, last_txg + object + i);
4860 ASSERT3U(error, ==, ENOENT);
4864 * Atomically update two entries in our zap object.
4865 * The first is named txg_%llu, and contains the txg
4866 * in which the property was last updated. The second
4867 * is named prop_%llu, and the nth element of its value
4868 * should be txg + object + n.
4870 tx = dmu_tx_create(os);
4871 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4872 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4877 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4879 for (i = 0; i < ints; i++)
4880 value[i] = txg + object + i;
4882 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4884 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4890 * Remove a random pair of entries.
4892 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4893 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4894 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4896 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4898 if (error == ENOENT)
4903 tx = dmu_tx_create(os);
4904 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4905 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4908 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4909 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4912 umem_free(od, sizeof (ztest_od_t));
4916 * Testcase to test the upgrading of a microzap to fatzap.
4919 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4921 objset_t *os = zd->zd_os;
4923 uint64_t object, txg;
4926 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4927 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4929 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4930 !ztest_random(2)) != 0)
4932 object = od->od_object;
4935 * Add entries to this ZAP and make sure it spills over
4936 * and gets upgraded to a fatzap. Also, since we are adding
4937 * 2050 entries we should see ptrtbl growth and leaf-block split.
4939 for (i = 0; i < 2050; i++) {
4940 char name[ZFS_MAX_DATASET_NAME_LEN];
4945 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4946 (u_longlong_t)id, (u_longlong_t)value);
4948 tx = dmu_tx_create(os);
4949 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4950 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4953 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4955 ASSERT(error == 0 || error == EEXIST);
4959 umem_free(od, sizeof (ztest_od_t));
4964 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4966 objset_t *os = zd->zd_os;
4968 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4970 int i, namelen, error;
4971 int micro = ztest_random(2);
4972 char name[20], string_value[20];
4975 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4976 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4978 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4979 umem_free(od, sizeof (ztest_od_t));
4983 object = od->od_object;
4986 * Generate a random name of the form 'xxx.....' where each
4987 * x is a random printable character and the dots are dots.
4988 * There are 94 such characters, and the name length goes from
4989 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4991 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4993 for (i = 0; i < 3; i++)
4994 name[i] = '!' + ztest_random('~' - '!' + 1);
4995 for (; i < namelen - 1; i++)
4999 if ((namelen & 1) || micro) {
5000 wsize = sizeof (txg);
5006 data = string_value;
5010 VERIFY0(zap_count(os, object, &count));
5011 ASSERT(count != -1ULL);
5014 * Select an operation: length, lookup, add, update, remove.
5016 i = ztest_random(5);
5019 tx = dmu_tx_create(os);
5020 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
5021 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
5023 umem_free(od, sizeof (ztest_od_t));
5026 bcopy(name, string_value, namelen);
5030 bzero(string_value, namelen);
5036 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
5038 ASSERT3U(wsize, ==, zl_wsize);
5039 ASSERT3U(wc, ==, zl_wc);
5041 ASSERT3U(error, ==, ENOENT);
5046 error = zap_lookup(os, object, name, wsize, wc, data);
5048 if (data == string_value &&
5049 bcmp(name, data, namelen) != 0)
5050 fatal(0, "name '%s' != val '%s' len %d",
5051 name, data, namelen);
5053 ASSERT3U(error, ==, ENOENT);
5058 error = zap_add(os, object, name, wsize, wc, data, tx);
5059 ASSERT(error == 0 || error == EEXIST);
5063 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
5067 error = zap_remove(os, object, name, tx);
5068 ASSERT(error == 0 || error == ENOENT);
5075 umem_free(od, sizeof (ztest_od_t));
5079 * Commit callback data.
5081 typedef struct ztest_cb_data {
5082 list_node_t zcd_node;
5084 int zcd_expected_err;
5085 boolean_t zcd_added;
5086 boolean_t zcd_called;
5090 /* This is the actual commit callback function */
5092 ztest_commit_callback(void *arg, int error)
5094 ztest_cb_data_t *data = arg;
5095 uint64_t synced_txg;
5097 VERIFY(data != NULL);
5098 VERIFY3S(data->zcd_expected_err, ==, error);
5099 VERIFY(!data->zcd_called);
5101 synced_txg = spa_last_synced_txg(data->zcd_spa);
5102 if (data->zcd_txg > synced_txg)
5103 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
5104 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
5107 data->zcd_called = B_TRUE;
5109 if (error == ECANCELED) {
5110 ASSERT0(data->zcd_txg);
5111 ASSERT(!data->zcd_added);
5114 * The private callback data should be destroyed here, but
5115 * since we are going to check the zcd_called field after
5116 * dmu_tx_abort(), we will destroy it there.
5121 ASSERT(data->zcd_added);
5122 ASSERT3U(data->zcd_txg, !=, 0);
5124 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5126 /* See if this cb was called more quickly */
5127 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
5128 zc_min_txg_delay = synced_txg - data->zcd_txg;
5130 /* Remove our callback from the list */
5131 list_remove(&zcl.zcl_callbacks, data);
5133 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5135 umem_free(data, sizeof (ztest_cb_data_t));
5138 /* Allocate and initialize callback data structure */
5139 static ztest_cb_data_t *
5140 ztest_create_cb_data(objset_t *os, uint64_t txg)
5142 ztest_cb_data_t *cb_data;
5144 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5146 cb_data->zcd_txg = txg;
5147 cb_data->zcd_spa = dmu_objset_spa(os);
5148 list_link_init(&cb_data->zcd_node);
5154 * Commit callback test.
5157 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5159 objset_t *os = zd->zd_os;
5162 ztest_cb_data_t *cb_data[3], *tmp_cb;
5163 uint64_t old_txg, txg;
5166 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5167 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5169 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5170 umem_free(od, sizeof (ztest_od_t));
5174 tx = dmu_tx_create(os);
5176 cb_data[0] = ztest_create_cb_data(os, 0);
5177 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5179 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
5181 /* Every once in a while, abort the transaction on purpose */
5182 if (ztest_random(100) == 0)
5186 error = dmu_tx_assign(tx, TXG_NOWAIT);
5188 txg = error ? 0 : dmu_tx_get_txg(tx);
5190 cb_data[0]->zcd_txg = txg;
5191 cb_data[1] = ztest_create_cb_data(os, txg);
5192 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5196 * It's not a strict requirement to call the registered
5197 * callbacks from inside dmu_tx_abort(), but that's what
5198 * it's supposed to happen in the current implementation
5199 * so we will check for that.
5201 for (i = 0; i < 2; i++) {
5202 cb_data[i]->zcd_expected_err = ECANCELED;
5203 VERIFY(!cb_data[i]->zcd_called);
5208 for (i = 0; i < 2; i++) {
5209 VERIFY(cb_data[i]->zcd_called);
5210 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5213 umem_free(od, sizeof (ztest_od_t));
5217 cb_data[2] = ztest_create_cb_data(os, txg);
5218 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5221 * Read existing data to make sure there isn't a future leak.
5223 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
5224 &old_txg, DMU_READ_PREFETCH));
5227 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5230 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
5232 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5235 * Since commit callbacks don't have any ordering requirement and since
5236 * it is theoretically possible for a commit callback to be called
5237 * after an arbitrary amount of time has elapsed since its txg has been
5238 * synced, it is difficult to reliably determine whether a commit
5239 * callback hasn't been called due to high load or due to a flawed
5242 * In practice, we will assume that if after a certain number of txgs a
5243 * commit callback hasn't been called, then most likely there's an
5244 * implementation bug..
5246 tmp_cb = list_head(&zcl.zcl_callbacks);
5247 if (tmp_cb != NULL &&
5248 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
5249 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5250 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5254 * Let's find the place to insert our callbacks.
5256 * Even though the list is ordered by txg, it is possible for the
5257 * insertion point to not be the end because our txg may already be
5258 * quiescing at this point and other callbacks in the open txg
5259 * (from other objsets) may have sneaked in.
5261 tmp_cb = list_tail(&zcl.zcl_callbacks);
5262 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5263 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5265 /* Add the 3 callbacks to the list */
5266 for (i = 0; i < 3; i++) {
5268 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5270 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5273 cb_data[i]->zcd_added = B_TRUE;
5274 VERIFY(!cb_data[i]->zcd_called);
5276 tmp_cb = cb_data[i];
5281 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5285 umem_free(od, sizeof (ztest_od_t));
5289 * Visit each object in the dataset. Verify that its properties
5290 * are consistent what was stored in the block tag when it was created,
5291 * and that its unused bonus buffer space has not been overwritten.
5295 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5297 objset_t *os = zd->zd_os;
5301 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5302 ztest_block_tag_t *bt = NULL;
5303 dmu_object_info_t doi;
5306 ztest_object_lock(zd, obj, RL_READER);
5307 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0) {
5308 ztest_object_unlock(zd, obj);
5312 dmu_object_info_from_db(db, &doi);
5313 if (doi.doi_bonus_size >= sizeof (*bt))
5314 bt = ztest_bt_bonus(db);
5316 if (bt && bt->bt_magic == BT_MAGIC) {
5317 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5318 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5320 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5323 dmu_buf_rele(db, FTAG);
5324 ztest_object_unlock(zd, obj);
5330 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5332 zfs_prop_t proplist[] = {
5334 ZFS_PROP_COMPRESSION,
5340 (void) rw_rdlock(&ztest_name_lock);
5342 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5343 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5344 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5346 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5347 ztest_random_blocksize(), (int)ztest_random(2)));
5349 (void) rw_unlock(&ztest_name_lock);
5354 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
5356 (void) rw_rdlock(&ztest_name_lock);
5358 int error = dmu_objset_remap_indirects(zd->zd_name);
5359 if (error == ENOSPC)
5363 (void) rw_unlock(&ztest_name_lock);
5368 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5370 nvlist_t *props = NULL;
5372 (void) rw_rdlock(&ztest_name_lock);
5374 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5375 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5377 VERIFY0(spa_prop_get(ztest_spa, &props));
5379 if (ztest_opts.zo_verbose >= 6)
5380 dump_nvlist(props, 4);
5384 (void) rw_unlock(&ztest_name_lock);
5388 user_release_one(const char *snapname, const char *holdname)
5390 nvlist_t *snaps, *holds;
5393 snaps = fnvlist_alloc();
5394 holds = fnvlist_alloc();
5395 fnvlist_add_boolean(holds, holdname);
5396 fnvlist_add_nvlist(snaps, snapname, holds);
5397 fnvlist_free(holds);
5398 error = dsl_dataset_user_release(snaps, NULL);
5399 fnvlist_free(snaps);
5404 * Test snapshot hold/release and deferred destroy.
5407 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5410 objset_t *os = zd->zd_os;
5414 char clonename[100];
5416 char osname[ZFS_MAX_DATASET_NAME_LEN];
5419 (void) rw_rdlock(&ztest_name_lock);
5421 dmu_objset_name(os, osname);
5423 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5425 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5426 (void) snprintf(clonename, sizeof (clonename),
5427 "%s/ch1_%llu", osname, (u_longlong_t)id);
5428 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5431 * Clean up from any previous run.
5433 error = dsl_destroy_head(clonename);
5434 if (error != ENOENT)
5436 error = user_release_one(fullname, tag);
5437 if (error != ESRCH && error != ENOENT)
5439 error = dsl_destroy_snapshot(fullname, B_FALSE);
5440 if (error != ENOENT)
5444 * Create snapshot, clone it, mark snap for deferred destroy,
5445 * destroy clone, verify snap was also destroyed.
5447 error = dmu_objset_snapshot_one(osname, snapname);
5449 if (error == ENOSPC) {
5450 ztest_record_enospc("dmu_objset_snapshot");
5453 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5456 error = dmu_objset_clone(clonename, fullname);
5458 if (error == ENOSPC) {
5459 ztest_record_enospc("dmu_objset_clone");
5462 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5465 error = dsl_destroy_snapshot(fullname, B_TRUE);
5467 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5471 error = dsl_destroy_head(clonename);
5473 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5475 error = dmu_objset_hold(fullname, FTAG, &origin);
5476 if (error != ENOENT)
5477 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5480 * Create snapshot, add temporary hold, verify that we can't
5481 * destroy a held snapshot, mark for deferred destroy,
5482 * release hold, verify snapshot was destroyed.
5484 error = dmu_objset_snapshot_one(osname, snapname);
5486 if (error == ENOSPC) {
5487 ztest_record_enospc("dmu_objset_snapshot");
5490 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5493 holds = fnvlist_alloc();
5494 fnvlist_add_string(holds, fullname, tag);
5495 error = dsl_dataset_user_hold(holds, 0, NULL);
5496 fnvlist_free(holds);
5498 if (error == ENOSPC) {
5499 ztest_record_enospc("dsl_dataset_user_hold");
5502 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5503 fullname, tag, error);
5506 error = dsl_destroy_snapshot(fullname, B_FALSE);
5507 if (error != EBUSY) {
5508 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5512 error = dsl_destroy_snapshot(fullname, B_TRUE);
5514 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5518 error = user_release_one(fullname, tag);
5520 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5522 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5525 (void) rw_unlock(&ztest_name_lock);
5529 * Inject random faults into the on-disk data.
5533 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5535 ztest_shared_t *zs = ztest_shared;
5536 spa_t *spa = ztest_spa;
5540 uint64_t bad = 0x1990c0ffeedecadeull;
5545 int bshift = SPA_MAXBLOCKSHIFT + 2;
5551 boolean_t islog = B_FALSE;
5553 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5554 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5556 mutex_enter(&ztest_vdev_lock);
5559 * Device removal is in progress, fault injection must be disabled
5560 * until it completes and the pool is scrubbed. The fault injection
5561 * strategy for damaging blocks does not take in to account evacuated
5562 * blocks which may have already been damaged.
5564 if (ztest_device_removal_active) {
5565 mutex_exit(&ztest_vdev_lock);
5569 maxfaults = MAXFAULTS(zs);
5570 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5571 mirror_save = zs->zs_mirrors;
5572 mutex_exit(&ztest_vdev_lock);
5574 ASSERT(leaves >= 1);
5577 * Grab the name lock as reader. There are some operations
5578 * which don't like to have their vdevs changed while
5579 * they are in progress (i.e. spa_change_guid). Those
5580 * operations will have grabbed the name lock as writer.
5582 (void) rw_rdlock(&ztest_name_lock);
5585 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5587 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5589 if (ztest_random(2) == 0) {
5591 * Inject errors on a normal data device or slog device.
5593 top = ztest_random_vdev_top(spa, B_TRUE);
5594 leaf = ztest_random(leaves) + zs->zs_splits;
5597 * Generate paths to the first leaf in this top-level vdev,
5598 * and to the random leaf we selected. We'll induce transient
5599 * write failures and random online/offline activity on leaf 0,
5600 * and we'll write random garbage to the randomly chosen leaf.
5602 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5603 ztest_opts.zo_dir, ztest_opts.zo_pool,
5604 top * leaves + zs->zs_splits);
5605 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5606 ztest_opts.zo_dir, ztest_opts.zo_pool,
5607 top * leaves + leaf);
5609 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5610 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5614 * If the top-level vdev needs to be resilvered
5615 * then we only allow faults on the device that is
5618 if (vd0 != NULL && maxfaults != 1 &&
5619 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5620 vd0->vdev_resilver_txg != 0)) {
5622 * Make vd0 explicitly claim to be unreadable,
5623 * or unwriteable, or reach behind its back
5624 * and close the underlying fd. We can do this if
5625 * maxfaults == 0 because we'll fail and reexecute,
5626 * and we can do it if maxfaults >= 2 because we'll
5627 * have enough redundancy. If maxfaults == 1, the
5628 * combination of this with injection of random data
5629 * corruption below exceeds the pool's fault tolerance.
5631 vdev_file_t *vf = vd0->vdev_tsd;
5633 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5634 (long long)vd0->vdev_id, (int)maxfaults);
5636 if (vf != NULL && ztest_random(3) == 0) {
5637 (void) close(vf->vf_vnode->v_fd);
5638 vf->vf_vnode->v_fd = -1;
5639 } else if (ztest_random(2) == 0) {
5640 vd0->vdev_cant_read = B_TRUE;
5642 vd0->vdev_cant_write = B_TRUE;
5644 guid0 = vd0->vdev_guid;
5648 * Inject errors on an l2cache device.
5650 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5652 if (sav->sav_count == 0) {
5653 spa_config_exit(spa, SCL_STATE, FTAG);
5654 (void) rw_unlock(&ztest_name_lock);
5657 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5658 guid0 = vd0->vdev_guid;
5659 (void) strcpy(path0, vd0->vdev_path);
5660 (void) strcpy(pathrand, vd0->vdev_path);
5664 maxfaults = INT_MAX; /* no limit on cache devices */
5667 spa_config_exit(spa, SCL_STATE, FTAG);
5668 (void) rw_unlock(&ztest_name_lock);
5671 * If we can tolerate two or more faults, or we're dealing
5672 * with a slog, randomly online/offline vd0.
5674 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5675 if (ztest_random(10) < 6) {
5676 int flags = (ztest_random(2) == 0 ?
5677 ZFS_OFFLINE_TEMPORARY : 0);
5680 * We have to grab the zs_name_lock as writer to
5681 * prevent a race between offlining a slog and
5682 * destroying a dataset. Offlining the slog will
5683 * grab a reference on the dataset which may cause
5684 * dsl_destroy_head() to fail with EBUSY thus
5685 * leaving the dataset in an inconsistent state.
5688 (void) rw_wrlock(&ztest_name_lock);
5690 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5693 (void) rw_unlock(&ztest_name_lock);
5696 * Ideally we would like to be able to randomly
5697 * call vdev_[on|off]line without holding locks
5698 * to force unpredictable failures but the side
5699 * effects of vdev_[on|off]line prevent us from
5700 * doing so. We grab the ztest_vdev_lock here to
5701 * prevent a race between injection testing and
5704 mutex_enter(&ztest_vdev_lock);
5705 (void) vdev_online(spa, guid0, 0, NULL);
5706 mutex_exit(&ztest_vdev_lock);
5714 * We have at least single-fault tolerance, so inject data corruption.
5716 fd = open(pathrand, O_RDWR);
5718 if (fd == -1) /* we hit a gap in the device namespace */
5721 fsize = lseek(fd, 0, SEEK_END);
5723 while (--iters != 0) {
5725 * The offset must be chosen carefully to ensure that
5726 * we do not inject a given logical block with errors
5727 * on two different leaf devices, because ZFS can not
5728 * tolerate that (if maxfaults==1).
5730 * We divide each leaf into chunks of size
5731 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5732 * there is a series of ranges to which we can inject errors.
5733 * Each range can accept errors on only a single leaf vdev.
5734 * The error injection ranges are separated by ranges
5735 * which we will not inject errors on any device (DMZs).
5736 * Each DMZ must be large enough such that a single block
5737 * can not straddle it, so that a single block can not be
5738 * a target in two different injection ranges (on different
5741 * For example, with 3 leaves, each chunk looks like:
5742 * 0 to 32M: injection range for leaf 0
5743 * 32M to 64M: DMZ - no injection allowed
5744 * 64M to 96M: injection range for leaf 1
5745 * 96M to 128M: DMZ - no injection allowed
5746 * 128M to 160M: injection range for leaf 2
5747 * 160M to 192M: DMZ - no injection allowed
5749 offset = ztest_random(fsize / (leaves << bshift)) *
5750 (leaves << bshift) + (leaf << bshift) +
5751 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5754 * Only allow damage to the labels at one end of the vdev.
5756 * If all labels are damaged, the device will be totally
5757 * inaccessible, which will result in loss of data,
5758 * because we also damage (parts of) the other side of
5761 * Additionally, we will always have both an even and an
5762 * odd label, so that we can handle crashes in the
5763 * middle of vdev_config_sync().
5765 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5769 * The two end labels are stored at the "end" of the disk, but
5770 * the end of the disk (vdev_psize) is aligned to
5771 * sizeof (vdev_label_t).
5773 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5774 if ((leaf & 1) == 1 &&
5775 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5778 mutex_enter(&ztest_vdev_lock);
5779 if (mirror_save != zs->zs_mirrors) {
5780 mutex_exit(&ztest_vdev_lock);
5785 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5786 fatal(1, "can't inject bad word at 0x%llx in %s",
5789 mutex_exit(&ztest_vdev_lock);
5791 if (ztest_opts.zo_verbose >= 7)
5792 (void) printf("injected bad word into %s,"
5793 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5798 umem_free(path0, MAXPATHLEN);
5799 umem_free(pathrand, MAXPATHLEN);
5803 * Verify that DDT repair works as expected.
5806 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5808 ztest_shared_t *zs = ztest_shared;
5809 spa_t *spa = ztest_spa;
5810 objset_t *os = zd->zd_os;
5812 uint64_t object, blocksize, txg, pattern, psize;
5813 enum zio_checksum checksum = spa_dedup_checksum(spa);
5818 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5821 blocksize = ztest_random_blocksize();
5822 blocksize = MIN(blocksize, 2048); /* because we write so many */
5824 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5825 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5827 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5828 umem_free(od, sizeof (ztest_od_t));
5833 * Take the name lock as writer to prevent anyone else from changing
5834 * the pool and dataset properies we need to maintain during this test.
5836 (void) rw_wrlock(&ztest_name_lock);
5838 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5840 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5842 (void) rw_unlock(&ztest_name_lock);
5843 umem_free(od, sizeof (ztest_od_t));
5847 dmu_objset_stats_t dds;
5848 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5849 dmu_objset_fast_stat(os, &dds);
5850 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5852 object = od[0].od_object;
5853 blocksize = od[0].od_blocksize;
5854 pattern = zs->zs_guid ^ dds.dds_guid;
5856 ASSERT(object != 0);
5858 tx = dmu_tx_create(os);
5859 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5860 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5862 (void) rw_unlock(&ztest_name_lock);
5863 umem_free(od, sizeof (ztest_od_t));
5868 * Write all the copies of our block.
5870 for (i = 0; i < copies; i++) {
5871 uint64_t offset = i * blocksize;
5872 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5873 DMU_READ_NO_PREFETCH);
5875 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5876 os, (long long)object, (long long) offset, error);
5878 ASSERT(db->db_offset == offset);
5879 ASSERT(db->db_size == blocksize);
5880 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5881 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5882 dmu_buf_will_fill(db, tx);
5883 ztest_pattern_set(db->db_data, db->db_size, pattern);
5884 dmu_buf_rele(db, FTAG);
5888 txg_wait_synced(spa_get_dsl(spa), txg);
5891 * Find out what block we got.
5893 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5894 DMU_READ_NO_PREFETCH));
5895 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5896 dmu_buf_rele(db, FTAG);
5899 * Damage the block. Dedup-ditto will save us when we read it later.
5901 psize = BP_GET_PSIZE(&blk);
5902 abd = abd_alloc_linear(psize, B_TRUE);
5903 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5905 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5906 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5907 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5911 (void) rw_unlock(&ztest_name_lock);
5912 umem_free(od, sizeof (ztest_od_t));
5920 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5922 spa_t *spa = ztest_spa;
5925 * Scrub in progress by device removal.
5927 if (ztest_device_removal_active)
5930 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5931 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5932 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5936 * Change the guid for the pool.
5940 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5942 spa_t *spa = ztest_spa;
5943 uint64_t orig, load;
5946 if (ztest_opts.zo_mmp_test)
5949 orig = spa_guid(spa);
5950 load = spa_load_guid(spa);
5952 (void) rw_wrlock(&ztest_name_lock);
5953 error = spa_change_guid(spa);
5954 (void) rw_unlock(&ztest_name_lock);
5959 if (ztest_opts.zo_verbose >= 4) {
5960 (void) printf("Changed guid old %llu -> %llu\n",
5961 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5964 VERIFY3U(orig, !=, spa_guid(spa));
5965 VERIFY3U(load, ==, spa_load_guid(spa));
5969 * Rename the pool to a different name and then rename it back.
5973 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5975 char *oldname, *newname;
5978 if (ztest_opts.zo_mmp_test)
5981 (void) rw_wrlock(&ztest_name_lock);
5983 oldname = ztest_opts.zo_pool;
5984 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5985 (void) strcpy(newname, oldname);
5986 (void) strcat(newname, "_tmp");
5991 VERIFY3U(0, ==, spa_rename(oldname, newname));
5994 * Try to open it under the old name, which shouldn't exist
5996 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5999 * Open it under the new name and make sure it's still the same spa_t.
6001 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6003 ASSERT(spa == ztest_spa);
6004 spa_close(spa, FTAG);
6007 * Rename it back to the original
6009 VERIFY3U(0, ==, spa_rename(newname, oldname));
6012 * Make sure it can still be opened
6014 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6016 ASSERT(spa == ztest_spa);
6017 spa_close(spa, FTAG);
6019 umem_free(newname, strlen(newname) + 1);
6021 (void) rw_unlock(&ztest_name_lock);
6025 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
6027 hrtime_t end = gethrtime() + NANOSEC;
6029 while (gethrtime() <= end) {
6030 int run_count = 100;
6032 struct abd *abd_data, *abd_meta;
6037 zio_cksum_t zc_ref_byteswap;
6039 size = ztest_random_blocksize();
6041 buf = umem_alloc(size, UMEM_NOFAIL);
6042 abd_data = abd_alloc(size, B_FALSE);
6043 abd_meta = abd_alloc(size, B_TRUE);
6045 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
6046 *ptr = ztest_random(UINT_MAX);
6048 abd_copy_from_buf_off(abd_data, buf, 0, size);
6049 abd_copy_from_buf_off(abd_meta, buf, 0, size);
6051 VERIFY0(fletcher_4_impl_set("scalar"));
6052 fletcher_4_native(buf, size, NULL, &zc_ref);
6053 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
6055 VERIFY0(fletcher_4_impl_set("cycle"));
6056 while (run_count-- > 0) {
6058 zio_cksum_t zc_byteswap;
6060 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
6061 fletcher_4_native(buf, size, NULL, &zc);
6063 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6064 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6065 sizeof (zc_byteswap)));
6067 /* Test ABD - data */
6068 abd_fletcher_4_byteswap(abd_data, size, NULL,
6070 abd_fletcher_4_native(abd_data, size, NULL, &zc);
6072 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6073 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6074 sizeof (zc_byteswap)));
6076 /* Test ABD - metadata */
6077 abd_fletcher_4_byteswap(abd_meta, size, NULL,
6079 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
6081 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
6082 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
6083 sizeof (zc_byteswap)));
6087 umem_free(buf, size);
6094 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
6101 zio_cksum_t zc_ref_bswap;
6103 hrtime_t end = gethrtime() + NANOSEC;
6105 while (gethrtime() <= end) {
6106 int run_count = 100;
6108 size = ztest_random_blocksize();
6109 buf = umem_alloc(size, UMEM_NOFAIL);
6111 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
6112 *ptr = ztest_random(UINT_MAX);
6114 VERIFY0(fletcher_4_impl_set("scalar"));
6115 fletcher_4_native(buf, size, NULL, &zc_ref);
6116 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
6118 VERIFY0(fletcher_4_impl_set("cycle"));
6120 while (run_count-- > 0) {
6122 zio_cksum_t zc_bswap;
6125 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
6126 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
6128 while (pos < size) {
6129 size_t inc = 64 * ztest_random(size / 67);
6130 /* sometimes add few bytes to test non-simd */
6131 if (ztest_random(100) < 10)
6132 inc += P2ALIGN(ztest_random(64),
6135 if (inc > (size - pos))
6138 fletcher_4_incremental_native(buf + pos, inc,
6140 fletcher_4_incremental_byteswap(buf + pos, inc,
6146 VERIFY3U(pos, ==, size);
6148 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6149 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6152 * verify if incremental on the whole buffer is
6153 * equivalent to non-incremental version
6155 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
6156 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
6158 fletcher_4_incremental_native(buf, size, &zc);
6159 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
6161 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6162 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6165 umem_free(buf, size);
6170 ztest_check_path(char *path)
6173 /* return true on success */
6174 return (!stat(path, &s));
6178 ztest_get_zdb_bin(char *bin, int len)
6182 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6183 * let popen to search through PATH.
6185 if ((zdb_path = getenv("ZDB_PATH"))) {
6186 strlcpy(bin, zdb_path, len); /* In env */
6187 if (!ztest_check_path(bin)) {
6188 ztest_dump_core = 0;
6189 fatal(1, "invalid ZDB_PATH '%s'", bin);
6194 VERIFY(realpath(getexecname(), bin) != NULL);
6195 if (strstr(bin, "/ztest/")) {
6196 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
6197 strcat(bin, "/zdb/zdb");
6198 if (ztest_check_path(bin))
6205 * Verify pool integrity by running zdb.
6208 ztest_run_zdb(char *pool)
6214 const int len = MAXPATHLEN + MAXNAMELEN + 20;
6217 bin = umem_alloc(len, UMEM_NOFAIL);
6218 zdb = umem_alloc(len, UMEM_NOFAIL);
6219 zbuf = umem_alloc(1024, UMEM_NOFAIL);
6221 ztest_get_zdb_bin(bin, len);
6224 "%s -bcc%s%s -G -d -U %s "
6225 "-o zfs_reconstruct_indirect_combinations_max=1000000 %s",
6227 ztest_opts.zo_verbose >= 3 ? "s" : "",
6228 ztest_opts.zo_verbose >= 4 ? "v" : "",
6232 if (ztest_opts.zo_verbose >= 5)
6233 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
6235 fp = popen(zdb, "r");
6237 while (fgets(zbuf, 1024, fp) != NULL)
6238 if (ztest_opts.zo_verbose >= 3)
6239 (void) printf("%s", zbuf);
6241 status = pclose(fp);
6246 ztest_dump_core = 0;
6247 if (WIFEXITED(status))
6248 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
6250 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
6252 umem_free(bin, len);
6253 umem_free(zdb, len);
6254 umem_free(zbuf, 1024);
6258 ztest_walk_pool_directory(char *header)
6262 if (ztest_opts.zo_verbose >= 6)
6263 (void) printf("%s\n", header);
6265 mutex_enter(&spa_namespace_lock);
6266 while ((spa = spa_next(spa)) != NULL)
6267 if (ztest_opts.zo_verbose >= 6)
6268 (void) printf("\t%s\n", spa_name(spa));
6269 mutex_exit(&spa_namespace_lock);
6273 ztest_spa_import_export(char *oldname, char *newname)
6275 nvlist_t *config, *newconfig;
6280 if (ztest_opts.zo_verbose >= 4) {
6281 (void) printf("import/export: old = %s, new = %s\n",
6286 * Clean up from previous runs.
6288 (void) spa_destroy(newname);
6291 * Get the pool's configuration and guid.
6293 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6296 * Kick off a scrub to tickle scrub/export races.
6298 if (ztest_random(2) == 0)
6299 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6301 pool_guid = spa_guid(spa);
6302 spa_close(spa, FTAG);
6304 ztest_walk_pool_directory("pools before export");
6309 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6311 ztest_walk_pool_directory("pools after export");
6316 newconfig = spa_tryimport(config);
6317 ASSERT(newconfig != NULL);
6318 nvlist_free(newconfig);
6321 * Import it under the new name.
6323 error = spa_import(newname, config, NULL, 0);
6325 dump_nvlist(config, 0);
6326 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6327 oldname, newname, error);
6330 ztest_walk_pool_directory("pools after import");
6333 * Try to import it again -- should fail with EEXIST.
6335 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6338 * Try to import it under a different name -- should fail with EEXIST.
6340 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6343 * Verify that the pool is no longer visible under the old name.
6345 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6348 * Verify that we can open and close the pool using the new name.
6350 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6351 ASSERT(pool_guid == spa_guid(spa));
6352 spa_close(spa, FTAG);
6354 nvlist_free(config);
6358 ztest_resume(spa_t *spa)
6360 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6361 (void) printf("resuming from suspended state\n");
6362 spa_vdev_state_enter(spa, SCL_NONE);
6363 vdev_clear(spa, NULL);
6364 (void) spa_vdev_state_exit(spa, NULL, 0);
6365 (void) zio_resume(spa);
6369 ztest_resume_thread(void *arg)
6373 while (!ztest_exiting) {
6374 if (spa_suspended(spa))
6376 (void) poll(NULL, 0, 100);
6379 * Periodically change the zfs_compressed_arc_enabled setting.
6381 if (ztest_random(10) == 0)
6382 zfs_compressed_arc_enabled = ztest_random(2);
6385 * Periodically change the zfs_abd_scatter_enabled setting.
6387 if (ztest_random(10) == 0)
6388 zfs_abd_scatter_enabled = ztest_random(2);
6395 ztest_deadman_thread(void *arg)
6397 ztest_shared_t *zs = arg;
6398 spa_t *spa = ztest_spa;
6399 hrtime_t delta, overdue, total = 0;
6402 delta = zs->zs_thread_stop - zs->zs_thread_start +
6403 MSEC2NSEC(zfs_deadman_synctime_ms);
6405 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6408 * If the pool is suspended then fail immediately. Otherwise,
6409 * check to see if the pool is making any progress. If
6410 * vdev_deadman() discovers that there hasn't been any recent
6411 * I/Os then it will end up aborting the tests.
6413 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
6414 fatal(0, "aborting test after %llu seconds because "
6415 "pool has transitioned to a suspended state.",
6416 zfs_deadman_synctime_ms / 1000);
6418 vdev_deadman(spa->spa_root_vdev, FTAG);
6421 * If the process doesn't complete within a grace period of
6422 * zfs_deadman_synctime_ms over the expected finish time,
6423 * then it may be hung and is terminated.
6425 overdue = zs->zs_proc_stop + MSEC2NSEC(zfs_deadman_synctime_ms);
6426 total += zfs_deadman_synctime_ms / 1000;
6427 if (gethrtime() > overdue) {
6428 fatal(0, "aborting test after %llu seconds because "
6429 "the process is overdue for termination.", total);
6432 (void) printf("ztest has been running for %lld seconds\n",
6438 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6440 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6441 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6442 hrtime_t functime = gethrtime();
6445 for (i = 0; i < zi->zi_iters; i++)
6446 zi->zi_func(zd, id);
6448 functime = gethrtime() - functime;
6450 atomic_add_64(&zc->zc_count, 1);
6451 atomic_add_64(&zc->zc_time, functime);
6453 if (ztest_opts.zo_verbose >= 4)
6454 (void) printf("%6.2f sec in %s\n",
6455 (double)functime / NANOSEC, zi->zi_funcname);
6459 ztest_thread(void *arg)
6462 uint64_t id = (uintptr_t)arg;
6463 ztest_shared_t *zs = ztest_shared;
6467 ztest_shared_callstate_t *zc;
6469 while ((now = gethrtime()) < zs->zs_thread_stop) {
6471 * See if it's time to force a crash.
6473 if (now > zs->zs_thread_kill)
6477 * If we're getting ENOSPC with some regularity, stop.
6479 if (zs->zs_enospc_count > 10)
6483 * Pick a random function to execute.
6485 rand = ztest_random(ZTEST_FUNCS);
6486 zi = &ztest_info[rand];
6487 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6488 call_next = zc->zc_next;
6490 if (now >= call_next &&
6491 atomic_cas_64(&zc->zc_next, call_next, call_next +
6492 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6493 ztest_execute(rand, zi, id);
6501 ztest_dataset_name(char *dsname, char *pool, int d)
6503 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6507 ztest_dataset_destroy(int d)
6509 char name[ZFS_MAX_DATASET_NAME_LEN];
6512 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6514 if (ztest_opts.zo_verbose >= 3)
6515 (void) printf("Destroying %s to free up space\n", name);
6518 * Cleanup any non-standard clones and snapshots. In general,
6519 * ztest thread t operates on dataset (t % zopt_datasets),
6520 * so there may be more than one thing to clean up.
6522 for (t = d; t < ztest_opts.zo_threads;
6523 t += ztest_opts.zo_datasets)
6524 ztest_dsl_dataset_cleanup(name, t);
6526 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6527 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6531 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6533 uint64_t usedobjs, dirobjs, scratch;
6536 * ZTEST_DIROBJ is the object directory for the entire dataset.
6537 * Therefore, the number of objects in use should equal the
6538 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6539 * If not, we have an object leak.
6541 * Note that we can only check this in ztest_dataset_open(),
6542 * when the open-context and syncing-context values agree.
6543 * That's because zap_count() returns the open-context value,
6544 * while dmu_objset_space() returns the rootbp fill count.
6546 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6547 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6548 ASSERT3U(dirobjs + 1, ==, usedobjs);
6552 ztest_dataset_open(int d)
6554 ztest_ds_t *zd = &ztest_ds[d];
6555 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6558 char name[ZFS_MAX_DATASET_NAME_LEN];
6561 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6563 (void) rw_rdlock(&ztest_name_lock);
6565 error = ztest_dataset_create(name);
6566 if (error == ENOSPC) {
6567 (void) rw_unlock(&ztest_name_lock);
6568 ztest_record_enospc(FTAG);
6571 ASSERT(error == 0 || error == EEXIST);
6573 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
6575 (void) rw_unlock(&ztest_name_lock);
6577 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6579 zilog = zd->zd_zilog;
6581 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6582 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6583 fatal(0, "missing log records: claimed %llu < committed %llu",
6584 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6586 ztest_dataset_dirobj_verify(zd);
6588 zil_replay(os, zd, ztest_replay_vector);
6590 ztest_dataset_dirobj_verify(zd);
6592 if (ztest_opts.zo_verbose >= 6)
6593 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6595 (u_longlong_t)zilog->zl_parse_blk_count,
6596 (u_longlong_t)zilog->zl_parse_lr_count,
6597 (u_longlong_t)zilog->zl_replaying_seq);
6599 zilog = zil_open(os, ztest_get_data);
6601 if (zilog->zl_replaying_seq != 0 &&
6602 zilog->zl_replaying_seq < committed_seq)
6603 fatal(0, "missing log records: replayed %llu < committed %llu",
6604 zilog->zl_replaying_seq, committed_seq);
6610 ztest_dataset_close(int d)
6612 ztest_ds_t *zd = &ztest_ds[d];
6614 zil_close(zd->zd_zilog);
6615 txg_wait_synced(spa_get_dsl(zd->zd_os->os_spa), 0);
6616 dmu_objset_disown(zd->zd_os, B_TRUE, zd);
6622 * Kick off threads to run tests on all datasets in parallel.
6625 ztest_run(ztest_shared_t *zs)
6629 kthread_t *resume_thread;
6630 kthread_t **run_threads;
6635 ztest_exiting = B_FALSE;
6638 * Initialize parent/child shared state.
6640 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6641 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6643 zs->zs_thread_start = gethrtime();
6644 zs->zs_thread_stop =
6645 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6646 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6647 zs->zs_thread_kill = zs->zs_thread_stop;
6648 if (ztest_random(100) < ztest_opts.zo_killrate) {
6649 zs->zs_thread_kill -=
6650 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6653 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6655 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6656 offsetof(ztest_cb_data_t, zcd_node));
6661 kernel_init(FREAD | FWRITE);
6662 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6663 spa->spa_debug = B_TRUE;
6664 metaslab_preload_limit = ztest_random(20) + 1;
6667 dmu_objset_stats_t dds;
6668 VERIFY0(ztest_dmu_objset_own(ztest_opts.zo_pool,
6669 DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
6670 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6671 dmu_objset_fast_stat(os, &dds);
6672 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6673 zs->zs_guid = dds.dds_guid;
6674 dmu_objset_disown(os, B_TRUE, FTAG);
6676 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6679 * Create a thread to periodically resume suspended I/O.
6681 resume_thread = thread_create(NULL, 0, ztest_resume_thread,
6682 spa, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6685 * Create a deadman thread and set to panic if we hang.
6687 (void) thread_create(NULL, 0, ztest_deadman_thread,
6688 zs, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6690 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
6693 * Verify that we can safely inquire about about any object,
6694 * whether it's allocated or not. To make it interesting,
6695 * we probe a 5-wide window around each power of two.
6696 * This hits all edge cases, including zero and the max.
6698 for (t = 0; t < 64; t++) {
6699 for (d = -5; d <= 5; d++) {
6700 error = dmu_object_info(spa->spa_meta_objset,
6701 (1ULL << t) + d, NULL);
6702 ASSERT(error == 0 || error == ENOENT ||
6708 * If we got any ENOSPC errors on the previous run, destroy something.
6710 if (zs->zs_enospc_count != 0) {
6711 int d = ztest_random(ztest_opts.zo_datasets);
6712 ztest_dataset_destroy(d);
6714 zs->zs_enospc_count = 0;
6716 run_threads = umem_zalloc(ztest_opts.zo_threads * sizeof (kthread_t *),
6719 if (ztest_opts.zo_verbose >= 4)
6720 (void) printf("starting main threads...\n");
6723 * Kick off all the tests that run in parallel.
6725 for (t = 0; t < ztest_opts.zo_threads; t++) {
6726 if (t < ztest_opts.zo_datasets && ztest_dataset_open(t) != 0) {
6727 umem_free(run_threads, ztest_opts.zo_threads *
6728 sizeof (kthread_t *));
6732 run_threads[t] = thread_create(NULL, 0, ztest_thread,
6733 (void *)(uintptr_t)t, 0, NULL, TS_RUN | TS_JOINABLE,
6738 * Wait for all of the tests to complete. We go in reverse order
6739 * so we don't close datasets while threads are still using them.
6741 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6742 VERIFY0(thread_join(run_threads[t]));
6743 if (t < ztest_opts.zo_datasets)
6744 ztest_dataset_close(t);
6747 txg_wait_synced(spa_get_dsl(spa), 0);
6749 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6750 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6752 umem_free(run_threads, ztest_opts.zo_threads * sizeof (kthread_t *));
6754 /* Kill the resume thread */
6755 ztest_exiting = B_TRUE;
6756 VERIFY0(thread_join(resume_thread));
6760 * Right before closing the pool, kick off a bunch of async I/O;
6761 * spa_close() should wait for it to complete.
6763 for (object = 1; object < 50; object++) {
6764 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6765 ZIO_PRIORITY_SYNC_READ);
6768 /* Verify that at least one commit cb was called in a timely fashion */
6769 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6770 VERIFY0(zc_min_txg_delay);
6772 spa_close(spa, FTAG);
6775 * Verify that we can loop over all pools.
6777 mutex_enter(&spa_namespace_lock);
6778 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6779 if (ztest_opts.zo_verbose > 3)
6780 (void) printf("spa_next: found %s\n", spa_name(spa));
6781 mutex_exit(&spa_namespace_lock);
6784 * Verify that we can export the pool and reimport it under a
6787 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6788 char name[ZFS_MAX_DATASET_NAME_LEN];
6789 (void) snprintf(name, sizeof (name), "%s_import",
6790 ztest_opts.zo_pool);
6791 ztest_spa_import_export(ztest_opts.zo_pool, name);
6792 ztest_spa_import_export(name, ztest_opts.zo_pool);
6797 list_destroy(&zcl.zcl_callbacks);
6798 mutex_destroy(&zcl.zcl_callbacks_lock);
6799 (void) rwlock_destroy(&ztest_name_lock);
6800 mutex_destroy(&ztest_vdev_lock);
6806 ztest_ds_t *zd = &ztest_ds[0];
6810 if (ztest_opts.zo_verbose >= 3)
6811 (void) printf("testing spa_freeze()...\n");
6813 kernel_init(FREAD | FWRITE);
6814 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6815 VERIFY3U(0, ==, ztest_dataset_open(0));
6816 spa->spa_debug = B_TRUE;
6820 * Force the first log block to be transactionally allocated.
6821 * We have to do this before we freeze the pool -- otherwise
6822 * the log chain won't be anchored.
6824 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6825 ztest_dmu_object_alloc_free(zd, 0);
6826 zil_commit(zd->zd_zilog, 0);
6829 txg_wait_synced(spa_get_dsl(spa), 0);
6832 * Freeze the pool. This stops spa_sync() from doing anything,
6833 * so that the only way to record changes from now on is the ZIL.
6838 * Because it is hard to predict how much space a write will actually
6839 * require beforehand, we leave ourselves some fudge space to write over
6842 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6845 * Run tests that generate log records but don't alter the pool config
6846 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6847 * We do a txg_wait_synced() after each iteration to force the txg
6848 * to increase well beyond the last synced value in the uberblock.
6849 * The ZIL should be OK with that.
6851 * Run a random number of times less than zo_maxloops and ensure we do
6852 * not run out of space on the pool.
6854 while (ztest_random(10) != 0 &&
6855 numloops++ < ztest_opts.zo_maxloops &&
6856 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6858 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6859 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6860 ztest_io(zd, od.od_object,
6861 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6862 txg_wait_synced(spa_get_dsl(spa), 0);
6866 * Commit all of the changes we just generated.
6868 zil_commit(zd->zd_zilog, 0);
6869 txg_wait_synced(spa_get_dsl(spa), 0);
6872 * Close our dataset and close the pool.
6874 ztest_dataset_close(0);
6875 spa_close(spa, FTAG);
6879 * Open and close the pool and dataset to induce log replay.
6881 kernel_init(FREAD | FWRITE);
6882 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6883 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6884 VERIFY3U(0, ==, ztest_dataset_open(0));
6885 spa->spa_debug = B_TRUE;
6887 txg_wait_synced(spa_get_dsl(spa), 0);
6888 ztest_dataset_close(0);
6889 ztest_reguid(NULL, 0);
6891 spa_close(spa, FTAG);
6896 print_time(hrtime_t t, char *timebuf)
6898 hrtime_t s = t / NANOSEC;
6899 hrtime_t m = s / 60;
6900 hrtime_t h = m / 60;
6901 hrtime_t d = h / 24;
6910 (void) sprintf(timebuf,
6911 "%llud%02lluh%02llum%02llus", d, h, m, s);
6913 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6915 (void) sprintf(timebuf, "%llum%02llus", m, s);
6917 (void) sprintf(timebuf, "%llus", s);
6921 make_random_props(void)
6925 VERIFY0(nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
6926 if (ztest_random(2) == 0)
6929 VERIFY0(nvlist_add_uint64(props,
6930 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE), 1));
6936 * Import a storage pool with the given name.
6939 ztest_import(ztest_shared_t *zs)
6941 libzfs_handle_t *hdl;
6942 importargs_t args = { 0 };
6944 nvlist_t *cfg = NULL;
6946 char *searchdirs[nsearch];
6947 char *name = ztest_opts.zo_pool;
6948 int flags = ZFS_IMPORT_MISSING_LOG;
6951 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6952 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6954 kernel_init(FREAD | FWRITE);
6955 hdl = libzfs_init();
6957 searchdirs[0] = ztest_opts.zo_dir;
6958 args.paths = nsearch;
6959 args.path = searchdirs;
6960 args.can_be_active = B_FALSE;
6962 error = zpool_tryimport(hdl, name, &cfg, &args);
6964 (void) fatal(0, "No pools found\n");
6966 VERIFY0(spa_import(name, cfg, NULL, flags));
6967 VERIFY0(spa_open(name, &spa, FTAG));
6968 zs->zs_metaslab_sz =
6969 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6970 spa_close(spa, FTAG);
6975 if (!ztest_opts.zo_mmp_test) {
6976 ztest_run_zdb(ztest_opts.zo_pool);
6978 ztest_run_zdb(ztest_opts.zo_pool);
6981 (void) rwlock_destroy(&ztest_name_lock);
6982 mutex_destroy(&ztest_vdev_lock);
6986 * Create a storage pool with the given name and initial vdev size.
6987 * Then test spa_freeze() functionality.
6990 ztest_init(ztest_shared_t *zs)
6993 nvlist_t *nvroot, *props;
6996 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6997 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6999 kernel_init(FREAD | FWRITE);
7002 * Create the storage pool.
7004 (void) spa_destroy(ztest_opts.zo_pool);
7005 ztest_shared->zs_vdev_next_leaf = 0;
7007 zs->zs_mirrors = ztest_opts.zo_mirrors;
7008 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
7009 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
7010 props = make_random_props();
7013 * We don't expect the pool to suspend unless maxfaults == 0,
7014 * in which case ztest_fault_inject() temporarily takes away
7015 * the only valid replica.
7017 VERIFY0(nvlist_add_uint64(props,
7018 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
7019 MAXFAULTS(zs) ? ZIO_FAILURE_MODE_PANIC : ZIO_FAILURE_MODE_WAIT));
7021 for (i = 0; i < SPA_FEATURES; i++) {
7023 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
7024 spa_feature_table[i].fi_uname));
7025 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
7029 VERIFY0(spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
7030 nvlist_free(nvroot);
7033 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
7034 zs->zs_metaslab_sz =
7035 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
7036 spa_close(spa, FTAG);
7040 if (!ztest_opts.zo_mmp_test) {
7041 ztest_run_zdb(ztest_opts.zo_pool);
7043 ztest_run_zdb(ztest_opts.zo_pool);
7046 (void) rwlock_destroy(&ztest_name_lock);
7047 mutex_destroy(&ztest_vdev_lock);
7053 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
7055 ztest_fd_data = mkstemp(ztest_name_data);
7056 ASSERT3S(ztest_fd_data, >=, 0);
7057 (void) unlink(ztest_name_data);
7061 shared_data_size(ztest_shared_hdr_t *hdr)
7065 size = hdr->zh_hdr_size;
7066 size += hdr->zh_opts_size;
7067 size += hdr->zh_size;
7068 size += hdr->zh_stats_size * hdr->zh_stats_count;
7069 size += hdr->zh_ds_size * hdr->zh_ds_count;
7078 ztest_shared_hdr_t *hdr;
7080 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
7081 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
7082 ASSERT(hdr != MAP_FAILED);
7084 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
7086 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
7087 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
7088 hdr->zh_size = sizeof (ztest_shared_t);
7089 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
7090 hdr->zh_stats_count = ZTEST_FUNCS;
7091 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
7092 hdr->zh_ds_count = ztest_opts.zo_datasets;
7094 size = shared_data_size(hdr);
7095 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
7097 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
7104 ztest_shared_hdr_t *hdr;
7107 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
7108 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
7109 ASSERT(hdr != MAP_FAILED);
7111 size = shared_data_size(hdr);
7113 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
7114 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
7115 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
7116 ASSERT(hdr != MAP_FAILED);
7117 buf = (uint8_t *)hdr;
7119 offset = hdr->zh_hdr_size;
7120 ztest_shared_opts = (void *)&buf[offset];
7121 offset += hdr->zh_opts_size;
7122 ztest_shared = (void *)&buf[offset];
7123 offset += hdr->zh_size;
7124 ztest_shared_callstate = (void *)&buf[offset];
7125 offset += hdr->zh_stats_size * hdr->zh_stats_count;
7126 ztest_shared_ds = (void *)&buf[offset];
7130 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
7134 char *cmdbuf = NULL;
7139 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
7140 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
7145 fatal(1, "fork failed");
7147 if (pid == 0) { /* child */
7148 char *emptyargv[2] = { cmd, NULL };
7149 char fd_data_str[12];
7151 struct rlimit rl = { 1024, 1024 };
7152 (void) setrlimit(RLIMIT_NOFILE, &rl);
7154 (void) close(ztest_fd_rand);
7155 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
7156 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
7158 (void) enable_extended_FILE_stdio(-1, -1);
7159 if (libpath != NULL)
7160 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
7161 (void) execv(cmd, emptyargv);
7162 ztest_dump_core = B_FALSE;
7163 fatal(B_TRUE, "exec failed: %s", cmd);
7166 if (cmdbuf != NULL) {
7167 umem_free(cmdbuf, MAXPATHLEN);
7171 while (waitpid(pid, &status, 0) != pid)
7173 if (statusp != NULL)
7176 if (WIFEXITED(status)) {
7177 if (WEXITSTATUS(status) != 0) {
7178 (void) fprintf(stderr, "child exited with code %d\n",
7179 WEXITSTATUS(status));
7183 } else if (WIFSIGNALED(status)) {
7184 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
7185 (void) fprintf(stderr, "child died with signal %d\n",
7191 (void) fprintf(stderr, "something strange happened to child\n");
7198 ztest_run_init(void)
7202 ztest_shared_t *zs = ztest_shared;
7205 * Blow away any existing copy of zpool.cache
7207 (void) remove(spa_config_path);
7209 if (ztest_opts.zo_init == 0) {
7210 if (ztest_opts.zo_verbose >= 1)
7211 (void) printf("Importing pool %s\n",
7212 ztest_opts.zo_pool);
7218 * Create and initialize our storage pool.
7220 for (i = 1; i <= ztest_opts.zo_init; i++) {
7221 bzero(zs, sizeof (ztest_shared_t));
7222 if (ztest_opts.zo_verbose >= 3 &&
7223 ztest_opts.zo_init != 1) {
7224 (void) printf("ztest_init(), pass %d\n", i);
7231 main(int argc, char **argv)
7239 ztest_shared_callstate_t *zc;
7241 char numbuf[NN_NUMBUF_SZ];
7246 char *fd_data_str = getenv("ZTEST_FD_DATA");
7247 struct sigaction action;
7249 (void) setvbuf(stdout, NULL, _IOLBF, 0);
7251 dprintf_setup(&argc, argv);
7252 zfs_deadman_synctime_ms = 300000;
7254 action.sa_handler = sig_handler;
7255 sigemptyset(&action.sa_mask);
7256 action.sa_flags = 0;
7258 if (sigaction(SIGSEGV, &action, NULL) < 0) {
7259 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
7264 if (sigaction(SIGABRT, &action, NULL) < 0) {
7265 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
7271 * Force random_get_bytes() to use /dev/urandom in order to prevent
7272 * ztest from needlessly depleting the system entropy pool.
7274 random_path = "/dev/urandom";
7275 ztest_fd_rand = open(random_path, O_RDONLY);
7276 ASSERT3S(ztest_fd_rand, >=, 0);
7279 process_options(argc, argv);
7284 bcopy(&ztest_opts, ztest_shared_opts,
7285 sizeof (*ztest_shared_opts));
7287 ztest_fd_data = atoi(fd_data_str);
7289 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
7291 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
7293 /* Override location of zpool.cache */
7294 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
7295 ztest_opts.zo_dir) != -1);
7297 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
7302 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
7303 metaslab_df_alloc_threshold =
7304 zs->zs_metaslab_df_alloc_threshold;
7313 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
7315 if (ztest_opts.zo_verbose >= 1) {
7316 (void) printf("%llu vdevs, %d datasets, %d threads,"
7317 " %llu seconds...\n",
7318 (u_longlong_t)ztest_opts.zo_vdevs,
7319 ztest_opts.zo_datasets,
7320 ztest_opts.zo_threads,
7321 (u_longlong_t)ztest_opts.zo_time);
7324 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7325 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7327 zs->zs_do_init = B_TRUE;
7328 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7329 if (ztest_opts.zo_verbose >= 1) {
7330 (void) printf("Executing older ztest for "
7331 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7333 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7334 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7336 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7338 zs->zs_do_init = B_FALSE;
7340 zs->zs_proc_start = gethrtime();
7341 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7343 for (f = 0; f < ZTEST_FUNCS; f++) {
7344 zi = &ztest_info[f];
7345 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7346 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7347 zc->zc_next = UINT64_MAX;
7349 zc->zc_next = zs->zs_proc_start +
7350 ztest_random(2 * zi->zi_interval[0] + 1);
7354 * Run the tests in a loop. These tests include fault injection
7355 * to verify that self-healing data works, and forced crashes
7356 * to verify that we never lose on-disk consistency.
7358 while (gethrtime() < zs->zs_proc_stop) {
7363 * Initialize the workload counters for each function.
7365 for (f = 0; f < ZTEST_FUNCS; f++) {
7366 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7371 /* Set the allocation switch size */
7372 zs->zs_metaslab_df_alloc_threshold =
7373 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7375 if (!hasalt || ztest_random(2) == 0) {
7376 if (hasalt && ztest_opts.zo_verbose >= 1) {
7377 (void) printf("Executing newer ztest: %s\n",
7381 killed = exec_child(cmd, NULL, B_TRUE, &status);
7383 if (hasalt && ztest_opts.zo_verbose >= 1) {
7384 (void) printf("Executing older ztest: %s\n",
7385 ztest_opts.zo_alt_ztest);
7388 killed = exec_child(ztest_opts.zo_alt_ztest,
7389 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7396 if (ztest_opts.zo_verbose >= 1) {
7397 hrtime_t now = gethrtime();
7399 now = MIN(now, zs->zs_proc_stop);
7400 print_time(zs->zs_proc_stop - now, timebuf);
7401 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7403 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7404 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7406 WIFEXITED(status) ? "Complete" : "SIGKILL",
7407 (u_longlong_t)zs->zs_enospc_count,
7408 100.0 * zs->zs_alloc / zs->zs_space,
7410 100.0 * (now - zs->zs_proc_start) /
7411 (ztest_opts.zo_time * NANOSEC), timebuf);
7414 if (ztest_opts.zo_verbose >= 2) {
7415 (void) printf("\nWorkload summary:\n\n");
7416 (void) printf("%7s %9s %s\n",
7417 "Calls", "Time", "Function");
7418 (void) printf("%7s %9s %s\n",
7419 "-----", "----", "--------");
7420 for (f = 0; f < ZTEST_FUNCS; f++) {
7421 zi = &ztest_info[f];
7422 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7423 print_time(zc->zc_time, timebuf);
7424 (void) printf("%7llu %9s %s\n",
7425 (u_longlong_t)zc->zc_count, timebuf,
7428 (void) printf("\n");
7432 * It's possible that we killed a child during a rename test,
7433 * in which case we'll have a 'ztest_tmp' pool lying around
7434 * instead of 'ztest'. Do a blind rename in case this happened.
7437 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
7438 spa_close(spa, FTAG);
7440 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
7442 kernel_init(FREAD | FWRITE);
7443 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
7444 ztest_opts.zo_pool);
7445 (void) spa_rename(tmpname, ztest_opts.zo_pool);
7449 if (!ztest_opts.zo_mmp_test)
7450 ztest_run_zdb(ztest_opts.zo_pool);
7453 if (ztest_opts.zo_verbose >= 1) {
7455 (void) printf("%d runs of older ztest: %s\n", older,
7456 ztest_opts.zo_alt_ztest);
7457 (void) printf("%d runs of newer ztest: %s\n", newer,
7460 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7461 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7464 umem_free(cmd, MAXNAMELEN);