4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2017 Joyent, Inc.
31 * The objective of this program is to provide a DMU/ZAP/SPA stress test
32 * that runs entirely in userland, is easy to use, and easy to extend.
34 * The overall design of the ztest program is as follows:
36 * (1) For each major functional area (e.g. adding vdevs to a pool,
37 * creating and destroying datasets, reading and writing objects, etc)
38 * we have a simple routine to test that functionality. These
39 * individual routines do not have to do anything "stressful".
41 * (2) We turn these simple functionality tests into a stress test by
42 * running them all in parallel, with as many threads as desired,
43 * and spread across as many datasets, objects, and vdevs as desired.
45 * (3) While all this is happening, we inject faults into the pool to
46 * verify that self-healing data really works.
48 * (4) Every time we open a dataset, we change its checksum and compression
49 * functions. Thus even individual objects vary from block to block
50 * in which checksum they use and whether they're compressed.
52 * (5) To verify that we never lose on-disk consistency after a crash,
53 * we run the entire test in a child of the main process.
54 * At random times, the child self-immolates with a SIGKILL.
55 * This is the software equivalent of pulling the power cord.
56 * The parent then runs the test again, using the existing
57 * storage pool, as many times as desired. If backwards compatibility
58 * testing is enabled ztest will sometimes run the "older" version
59 * of ztest after a SIGKILL.
61 * (6) To verify that we don't have future leaks or temporal incursions,
62 * many of the functional tests record the transaction group number
63 * as part of their data. When reading old data, they verify that
64 * the transaction group number is less than the current, open txg.
65 * If you add a new test, please do this if applicable.
67 * (7) Threads are created with a reduced stack size, for sanity checking.
68 * Therefore, it's important not to allocate huge buffers on the stack.
70 * When run with no arguments, ztest runs for about five minutes and
71 * produces no output if successful. To get a little bit of information,
72 * specify -V. To get more information, specify -VV, and so on.
74 * To turn this into an overnight stress test, use -T to specify run time.
76 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
77 * to increase the pool capacity, fanout, and overall stress level.
79 * Use the -k option to set the desired frequency of kills.
81 * When ztest invokes itself it passes all relevant information through a
82 * temporary file which is mmap-ed in the child process. This allows shared
83 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
84 * stored at offset 0 of this file and contains information on the size and
85 * number of shared structures in the file. The information stored in this file
86 * must remain backwards compatible with older versions of ztest so that
87 * ztest can invoke them during backwards compatibility testing (-B).
90 #include <sys/zfs_context.h>
96 #include <sys/dmu_objset.h>
100 #include <sys/wait.h>
101 #include <sys/mman.h>
102 #include <sys/resource.h>
105 #include <sys/zil_impl.h>
106 #include <sys/zfs_rlock.h>
107 #include <sys/vdev_impl.h>
108 #include <sys/vdev_file.h>
109 #include <sys/spa_impl.h>
110 #include <sys/metaslab_impl.h>
111 #include <sys/dsl_prop.h>
112 #include <sys/dsl_dataset.h>
113 #include <sys/dsl_destroy.h>
114 #include <sys/dsl_scan.h>
115 #include <sys/zio_checksum.h>
116 #include <sys/refcount.h>
117 #include <sys/zfeature.h>
118 #include <sys/dsl_userhold.h>
121 #include <stdio_ext.h>
128 #include <sys/fs/zfs.h>
129 #include <zfs_fletcher.h>
130 #include <libnvpair.h>
133 #include <execinfo.h> /* for backtrace() */
136 static int ztest_fd_data = -1;
137 static int ztest_fd_rand = -1;
139 typedef struct ztest_shared_hdr {
140 uint64_t zh_hdr_size;
141 uint64_t zh_opts_size;
143 uint64_t zh_stats_size;
144 uint64_t zh_stats_count;
146 uint64_t zh_ds_count;
147 } ztest_shared_hdr_t;
149 static ztest_shared_hdr_t *ztest_shared_hdr;
151 typedef struct ztest_shared_opts {
152 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
153 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
154 char zo_alt_ztest[MAXNAMELEN];
155 char zo_alt_libpath[MAXNAMELEN];
157 uint64_t zo_vdevtime;
165 uint64_t zo_passtime;
166 uint64_t zo_killrate;
170 uint64_t zo_maxloops;
171 uint64_t zo_metaslab_gang_bang;
173 } ztest_shared_opts_t;
175 static const ztest_shared_opts_t ztest_opts_defaults = {
176 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
177 .zo_dir = { '/', 't', 'm', 'p', '\0' },
178 .zo_alt_ztest = { '\0' },
179 .zo_alt_libpath = { '\0' },
181 .zo_ashift = SPA_MINBLOCKSHIFT,
184 .zo_raidz_parity = 1,
185 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
188 .zo_passtime = 60, /* 60 seconds */
189 .zo_killrate = 70, /* 70% kill rate */
193 .zo_time = 300, /* 5 minutes */
194 .zo_maxloops = 50, /* max loops during spa_freeze() */
195 .zo_metaslab_gang_bang = 32 << 10
198 extern uint64_t metaslab_gang_bang;
199 extern uint64_t metaslab_df_alloc_threshold;
200 extern unsigned long zfs_deadman_synctime_ms;
201 extern int metaslab_preload_limit;
202 extern boolean_t zfs_compressed_arc_enabled;
203 extern int zfs_abd_scatter_enabled;
205 static ztest_shared_opts_t *ztest_shared_opts;
206 static ztest_shared_opts_t ztest_opts;
207 static char *ztest_wkeydata = "abcdefghijklmnopqrstuvwxyz012345";
209 typedef struct ztest_shared_ds {
213 static ztest_shared_ds_t *ztest_shared_ds;
214 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
216 #define BT_MAGIC 0x123456789abcdefULL
217 #define MAXFAULTS() \
218 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
222 ZTEST_IO_WRITE_PATTERN,
223 ZTEST_IO_WRITE_ZEROES,
230 typedef struct ztest_block_tag {
234 uint64_t bt_dnodesize;
241 typedef struct bufwad {
259 #define ZTEST_RANGE_LOCKS 64
260 #define ZTEST_OBJECT_LOCKS 64
263 * Object descriptor. Used as a template for object lookup/create/remove.
265 typedef struct ztest_od {
268 dmu_object_type_t od_type;
269 dmu_object_type_t od_crtype;
270 uint64_t od_blocksize;
271 uint64_t od_crblocksize;
272 uint64_t od_crdnodesize;
275 char od_name[ZFS_MAX_DATASET_NAME_LEN];
281 typedef struct ztest_ds {
282 ztest_shared_ds_t *zd_shared;
284 rwlock_t zd_zilog_lock;
286 ztest_od_t *zd_od; /* debugging aid */
287 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
288 kmutex_t zd_dirobj_lock;
289 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
290 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
294 * Per-iteration state.
296 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
298 typedef struct ztest_info {
299 ztest_func_t *zi_func; /* test function */
300 uint64_t zi_iters; /* iterations per execution */
301 uint64_t *zi_interval; /* execute every <interval> seconds */
302 const char *zi_funcname; /* name of test function */
305 typedef struct ztest_shared_callstate {
306 uint64_t zc_count; /* per-pass count */
307 uint64_t zc_time; /* per-pass time */
308 uint64_t zc_next; /* next time to call this function */
309 } ztest_shared_callstate_t;
311 static ztest_shared_callstate_t *ztest_shared_callstate;
312 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
314 ztest_func_t ztest_dmu_read_write;
315 ztest_func_t ztest_dmu_write_parallel;
316 ztest_func_t ztest_dmu_object_alloc_free;
317 ztest_func_t ztest_dmu_commit_callbacks;
318 ztest_func_t ztest_zap;
319 ztest_func_t ztest_zap_parallel;
320 ztest_func_t ztest_zil_commit;
321 ztest_func_t ztest_zil_remount;
322 ztest_func_t ztest_dmu_read_write_zcopy;
323 ztest_func_t ztest_dmu_objset_create_destroy;
324 ztest_func_t ztest_dmu_prealloc;
325 ztest_func_t ztest_fzap;
326 ztest_func_t ztest_dmu_snapshot_create_destroy;
327 ztest_func_t ztest_dsl_prop_get_set;
328 ztest_func_t ztest_spa_prop_get_set;
329 ztest_func_t ztest_spa_create_destroy;
330 ztest_func_t ztest_fault_inject;
331 ztest_func_t ztest_ddt_repair;
332 ztest_func_t ztest_dmu_snapshot_hold;
333 ztest_func_t ztest_mmp_enable_disable;
334 ztest_func_t ztest_spa_rename;
335 ztest_func_t ztest_scrub;
336 ztest_func_t ztest_dsl_dataset_promote_busy;
337 ztest_func_t ztest_vdev_attach_detach;
338 ztest_func_t ztest_vdev_LUN_growth;
339 ztest_func_t ztest_vdev_add_remove;
340 ztest_func_t ztest_vdev_aux_add_remove;
341 ztest_func_t ztest_split_pool;
342 ztest_func_t ztest_reguid;
343 ztest_func_t ztest_spa_upgrade;
344 ztest_func_t ztest_fletcher;
345 ztest_func_t ztest_fletcher_incr;
346 ztest_func_t ztest_verify_dnode_bt;
348 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
349 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
350 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
351 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
352 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
354 #define ZTI_INIT(func, iters, interval) \
355 { .zi_func = (func), \
356 .zi_iters = (iters), \
357 .zi_interval = (interval), \
358 .zi_funcname = # func }
360 ztest_info_t ztest_info[] = {
361 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
362 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
363 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
364 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
365 ZTI_INIT(ztest_zap, 30, &zopt_always),
366 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
367 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
368 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
369 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
371 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
372 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
373 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
378 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
379 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
380 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
381 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
382 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
383 ZTI_INIT(ztest_mmp_enable_disable, 1, &zopt_sometimes),
384 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
385 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
386 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
387 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
388 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
389 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
390 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
391 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
392 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
393 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
394 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
395 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
398 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
401 * The following struct is used to hold a list of uncalled commit callbacks.
402 * The callbacks are ordered by txg number.
404 typedef struct ztest_cb_list {
405 kmutex_t zcl_callbacks_lock;
406 list_t zcl_callbacks;
410 * Stuff we need to share writably between parent and child.
412 typedef struct ztest_shared {
413 boolean_t zs_do_init;
414 hrtime_t zs_proc_start;
415 hrtime_t zs_proc_stop;
416 hrtime_t zs_thread_start;
417 hrtime_t zs_thread_stop;
418 hrtime_t zs_thread_kill;
419 uint64_t zs_enospc_count;
420 uint64_t zs_vdev_next_leaf;
421 uint64_t zs_vdev_aux;
426 uint64_t zs_metaslab_sz;
427 uint64_t zs_metaslab_df_alloc_threshold;
431 #define ID_PARALLEL -1ULL
433 static char ztest_dev_template[] = "%s/%s.%llua";
434 static char ztest_aux_template[] = "%s/%s.%s.%llu";
435 ztest_shared_t *ztest_shared;
437 static spa_t *ztest_spa = NULL;
438 static ztest_ds_t *ztest_ds;
440 static kmutex_t ztest_vdev_lock;
443 * The ztest_name_lock protects the pool and dataset namespace used by
444 * the individual tests. To modify the namespace, consumers must grab
445 * this lock as writer. Grabbing the lock as reader will ensure that the
446 * namespace does not change while the lock is held.
448 static rwlock_t ztest_name_lock;
450 static boolean_t ztest_dump_core = B_TRUE;
451 static boolean_t ztest_dump_debug_buffer = B_FALSE;
452 static boolean_t ztest_exiting;
454 /* Global commit callback list */
455 static ztest_cb_list_t zcl;
456 /* Commit cb delay */
457 static uint64_t zc_min_txg_delay = UINT64_MAX;
458 static int zc_cb_counter = 0;
461 * Minimum number of commit callbacks that need to be registered for us to check
462 * whether the minimum txg delay is acceptable.
464 #define ZTEST_COMMIT_CB_MIN_REG 100
467 * If a number of txgs equal to this threshold have been created after a commit
468 * callback has been registered but not called, then we assume there is an
469 * implementation bug.
471 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
473 extern uint64_t metaslab_gang_bang;
474 extern uint64_t metaslab_df_alloc_threshold;
477 ZTEST_META_DNODE = 0,
482 static void usage(boolean_t) __NORETURN;
485 * These libumem hooks provide a reasonable set of defaults for the allocator's
486 * debugging facilities.
489 _umem_debug_init(void)
491 return ("default,verbose"); /* $UMEM_DEBUG setting */
495 _umem_logging_init(void)
497 return ("fail,contents"); /* $UMEM_LOGGING setting */
501 dump_debug_buffer(void)
503 if (!ztest_dump_debug_buffer)
507 zfs_dbgmsg_print("ztest");
510 #define BACKTRACE_SZ 100
512 static void sig_handler(int signo)
514 struct sigaction action;
515 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
517 void *buffer[BACKTRACE_SZ];
519 nptrs = backtrace(buffer, BACKTRACE_SZ);
520 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
525 * Restore default action and re-raise signal so SIGSEGV and
526 * SIGABRT can trigger a core dump.
528 action.sa_handler = SIG_DFL;
529 sigemptyset(&action.sa_mask);
531 (void) sigaction(signo, &action, NULL);
535 #define FATAL_MSG_SZ 1024
540 fatal(int do_perror, char *message, ...)
543 int save_errno = errno;
546 (void) fflush(stdout);
547 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
549 va_start(args, message);
550 (void) sprintf(buf, "ztest: ");
552 (void) vsprintf(buf + strlen(buf), message, args);
555 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
556 ": %s", strerror(save_errno));
558 (void) fprintf(stderr, "%s\n", buf);
559 fatal_msg = buf; /* to ease debugging */
569 str2shift(const char *buf)
571 const char *ends = "BKMGTPEZ";
576 for (i = 0; i < strlen(ends); i++) {
577 if (toupper(buf[0]) == ends[i])
580 if (i == strlen(ends)) {
581 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
585 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
588 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
594 nicenumtoull(const char *buf)
599 val = strtoull(buf, &end, 0);
601 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
603 } else if (end[0] == '.') {
604 double fval = strtod(buf, &end);
605 fval *= pow(2, str2shift(end));
606 if (fval > UINT64_MAX) {
607 (void) fprintf(stderr, "ztest: value too large: %s\n",
611 val = (uint64_t)fval;
613 int shift = str2shift(end);
614 if (shift >= 64 || (val << shift) >> shift != val) {
615 (void) fprintf(stderr, "ztest: value too large: %s\n",
625 usage(boolean_t requested)
627 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
629 char nice_vdev_size[NN_NUMBUF_SZ];
630 char nice_gang_bang[NN_NUMBUF_SZ];
631 FILE *fp = requested ? stdout : stderr;
633 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
634 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang,
635 sizeof (nice_gang_bang));
637 (void) fprintf(fp, "Usage: %s\n"
638 "\t[-v vdevs (default: %llu)]\n"
639 "\t[-s size_of_each_vdev (default: %s)]\n"
640 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
641 "\t[-m mirror_copies (default: %d)]\n"
642 "\t[-r raidz_disks (default: %d)]\n"
643 "\t[-R raidz_parity (default: %d)]\n"
644 "\t[-d datasets (default: %d)]\n"
645 "\t[-t threads (default: %d)]\n"
646 "\t[-g gang_block_threshold (default: %s)]\n"
647 "\t[-i init_count (default: %d)] initialize pool i times\n"
648 "\t[-k kill_percentage (default: %llu%%)]\n"
649 "\t[-p pool_name (default: %s)]\n"
650 "\t[-f dir (default: %s)] file directory for vdev files\n"
651 "\t[-M] Multi-host simulate pool imported on remote host\n"
652 "\t[-V] verbose (use multiple times for ever more blather)\n"
653 "\t[-E] use existing pool instead of creating new one\n"
654 "\t[-T time (default: %llu sec)] total run time\n"
655 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
656 "\t[-P passtime (default: %llu sec)] time per pass\n"
657 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
658 "\t[-o variable=value] ... set global variable to an unsigned\n"
659 "\t 32-bit integer value\n"
660 "\t[-G dump zfs_dbgmsg buffer before exiting due to an error\n"
661 "\t[-h] (print help)\n"
664 (u_longlong_t)zo->zo_vdevs, /* -v */
665 nice_vdev_size, /* -s */
666 zo->zo_ashift, /* -a */
667 zo->zo_mirrors, /* -m */
668 zo->zo_raidz, /* -r */
669 zo->zo_raidz_parity, /* -R */
670 zo->zo_datasets, /* -d */
671 zo->zo_threads, /* -t */
672 nice_gang_bang, /* -g */
673 zo->zo_init, /* -i */
674 (u_longlong_t)zo->zo_killrate, /* -k */
675 zo->zo_pool, /* -p */
677 (u_longlong_t)zo->zo_time, /* -T */
678 (u_longlong_t)zo->zo_maxloops, /* -F */
679 (u_longlong_t)zo->zo_passtime);
680 exit(requested ? 0 : 1);
684 process_options(int argc, char **argv)
687 ztest_shared_opts_t *zo = &ztest_opts;
691 char altdir[MAXNAMELEN] = { 0 };
693 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
695 while ((opt = getopt(argc, argv,
696 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:G")) != EOF) {
713 value = nicenumtoull(optarg);
717 zo->zo_vdevs = value;
720 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
723 zo->zo_ashift = value;
726 zo->zo_mirrors = value;
729 zo->zo_raidz = MAX(1, value);
732 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
735 zo->zo_datasets = MAX(1, value);
738 zo->zo_threads = MAX(1, value);
741 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
748 zo->zo_killrate = value;
751 (void) strlcpy(zo->zo_pool, optarg,
752 sizeof (zo->zo_pool));
755 path = realpath(optarg, NULL);
757 (void) fprintf(stderr, "error: %s: %s\n",
758 optarg, strerror(errno));
761 (void) strlcpy(zo->zo_dir, path,
762 sizeof (zo->zo_dir));
779 zo->zo_passtime = MAX(1, value);
782 zo->zo_maxloops = MAX(1, value);
785 (void) strlcpy(altdir, optarg, sizeof (altdir));
788 if (set_global_var(optarg) != 0)
792 ztest_dump_debug_buffer = B_TRUE;
804 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
807 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
810 if (strlen(altdir) > 0) {
818 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
819 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
821 VERIFY(NULL != realpath(getexecname(), cmd));
822 if (0 != access(altdir, F_OK)) {
823 ztest_dump_core = B_FALSE;
824 fatal(B_TRUE, "invalid alternate ztest path: %s",
827 VERIFY(NULL != realpath(altdir, realaltdir));
830 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
831 * We want to extract <isa> to determine if we should use
832 * 32 or 64 bit binaries.
834 bin = strstr(cmd, "/usr/bin/");
835 ztest = strstr(bin, "/ztest");
837 isalen = ztest - isa;
838 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
839 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
840 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
841 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
843 if (0 != access(zo->zo_alt_ztest, X_OK)) {
844 ztest_dump_core = B_FALSE;
845 fatal(B_TRUE, "invalid alternate ztest: %s",
847 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
848 ztest_dump_core = B_FALSE;
849 fatal(B_TRUE, "invalid alternate lib directory %s",
853 umem_free(cmd, MAXPATHLEN);
854 umem_free(realaltdir, MAXPATHLEN);
859 ztest_kill(ztest_shared_t *zs)
861 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
862 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
865 * Before we kill off ztest, make sure that the config is updated.
866 * See comment above spa_config_sync().
868 mutex_enter(&spa_namespace_lock);
869 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
870 mutex_exit(&spa_namespace_lock);
872 (void) kill(getpid(), SIGKILL);
876 ztest_random(uint64_t range)
880 ASSERT3S(ztest_fd_rand, >=, 0);
885 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
886 fatal(1, "short read from /dev/urandom");
893 ztest_record_enospc(const char *s)
895 ztest_shared->zs_enospc_count++;
899 ztest_get_ashift(void)
901 if (ztest_opts.zo_ashift == 0)
902 return (SPA_MINBLOCKSHIFT + ztest_random(5));
903 return (ztest_opts.zo_ashift);
907 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
913 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
916 ashift = ztest_get_ashift();
922 vdev = ztest_shared->zs_vdev_aux;
923 (void) snprintf(path, MAXPATHLEN,
924 ztest_aux_template, ztest_opts.zo_dir,
925 pool == NULL ? ztest_opts.zo_pool : pool,
928 vdev = ztest_shared->zs_vdev_next_leaf++;
929 (void) snprintf(path, MAXPATHLEN,
930 ztest_dev_template, ztest_opts.zo_dir,
931 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
936 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
938 fatal(1, "can't open %s", path);
939 if (ftruncate(fd, size) != 0)
940 fatal(1, "can't ftruncate %s", path);
944 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
945 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
946 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
947 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
948 umem_free(pathbuf, MAXPATHLEN);
954 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
955 uint64_t ashift, int r)
957 nvlist_t *raidz, **child;
961 return (make_vdev_file(path, aux, pool, size, ashift));
962 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
964 for (c = 0; c < r; c++)
965 child[c] = make_vdev_file(path, aux, pool, size, ashift);
967 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
968 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
969 VDEV_TYPE_RAIDZ) == 0);
970 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
971 ztest_opts.zo_raidz_parity) == 0);
972 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
975 for (c = 0; c < r; c++)
976 nvlist_free(child[c]);
978 umem_free(child, r * sizeof (nvlist_t *));
984 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
985 uint64_t ashift, int r, int m)
987 nvlist_t *mirror, **child;
991 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
993 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
995 for (c = 0; c < m; c++)
996 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
998 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
999 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1000 VDEV_TYPE_MIRROR) == 0);
1001 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1004 for (c = 0; c < m; c++)
1005 nvlist_free(child[c]);
1007 umem_free(child, m * sizeof (nvlist_t *));
1013 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1014 int log, int r, int m, int t)
1016 nvlist_t *root, **child;
1021 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1023 for (c = 0; c < t; c++) {
1024 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1026 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1030 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1031 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1032 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1035 for (c = 0; c < t; c++)
1036 nvlist_free(child[c]);
1038 umem_free(child, t * sizeof (nvlist_t *));
1044 * Find a random spa version. Returns back a random spa version in the
1045 * range [initial_version, SPA_VERSION_FEATURES].
1048 ztest_random_spa_version(uint64_t initial_version)
1050 uint64_t version = initial_version;
1052 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1054 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1057 if (version > SPA_VERSION_BEFORE_FEATURES)
1058 version = SPA_VERSION_FEATURES;
1060 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1065 ztest_random_blocksize(void)
1068 * Choose a block size >= the ashift.
1069 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1071 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1072 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1074 uint64_t block_shift =
1075 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1076 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1080 ztest_random_dnodesize(void)
1083 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1085 if (max_slots == DNODE_MIN_SLOTS)
1086 return (DNODE_MIN_SIZE);
1089 * Weight the random distribution more heavily toward smaller
1090 * dnode sizes since that is more likely to reflect real-world
1093 ASSERT3U(max_slots, >, 4);
1094 switch (ztest_random(10)) {
1096 slots = 5 + ztest_random(max_slots - 4);
1099 slots = 2 + ztest_random(3);
1106 return (slots << DNODE_SHIFT);
1110 ztest_random_ibshift(void)
1112 return (DN_MIN_INDBLKSHIFT +
1113 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1117 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1120 vdev_t *rvd = spa->spa_root_vdev;
1123 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1126 top = ztest_random(rvd->vdev_children);
1127 tvd = rvd->vdev_child[top];
1128 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1129 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1135 ztest_random_dsl_prop(zfs_prop_t prop)
1140 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1141 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1147 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1150 const char *propname = zfs_prop_to_name(prop);
1151 const char *valname;
1156 error = dsl_prop_set_int(osname, propname,
1157 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1159 if (error == ENOSPC) {
1160 ztest_record_enospc(FTAG);
1165 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1166 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1168 if (ztest_opts.zo_verbose >= 6) {
1171 err = zfs_prop_index_to_string(prop, curval, &valname);
1173 (void) printf("%s %s = %llu at '%s'\n", osname,
1174 propname, (unsigned long long)curval, setpoint);
1176 (void) printf("%s %s = %s at '%s'\n",
1177 osname, propname, valname, setpoint);
1179 umem_free(setpoint, MAXPATHLEN);
1185 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1187 spa_t *spa = ztest_spa;
1188 nvlist_t *props = NULL;
1191 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1192 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1194 error = spa_prop_set(spa, props);
1198 if (error == ENOSPC) {
1199 ztest_record_enospc(FTAG);
1208 ztest_dmu_objset_own(const char *name, dmu_objset_type_t type,
1209 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
1213 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1214 if (decrypt && err == EACCES) {
1215 char ddname[ZFS_MAX_DATASET_NAME_LEN];
1216 dsl_crypto_params_t *dcp;
1217 nvlist_t *crypto_args = fnvlist_alloc();
1220 /* spa_keystore_load_wkey() expects a dsl dir name */
1221 strcpy(ddname, name);
1222 cp = strchr(ddname, '@');
1226 fnvlist_add_uint8_array(crypto_args, "wkeydata",
1227 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
1228 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
1229 crypto_args, &dcp));
1230 err = spa_keystore_load_wkey(ddname, dcp, B_FALSE);
1231 dsl_crypto_params_free(dcp, B_FALSE);
1232 fnvlist_free(crypto_args);
1237 err = dmu_objset_own(name, type, readonly, decrypt, tag, osp);
1245 * Object and range lock mechanics
1248 list_node_t z_lnode;
1249 refcount_t z_refcnt;
1251 zfs_rlock_t z_range_lock;
1256 ztest_znode_t *z_ztznode;
1259 static ztest_znode_t *
1260 ztest_znode_init(uint64_t object)
1262 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1264 list_link_init(&zp->z_lnode);
1265 refcount_create(&zp->z_refcnt);
1266 zp->z_object = object;
1267 zfs_rlock_init(&zp->z_range_lock);
1273 ztest_znode_fini(ztest_znode_t *zp)
1275 ASSERT(refcount_is_zero(&zp->z_refcnt));
1276 zfs_rlock_destroy(&zp->z_range_lock);
1278 refcount_destroy(&zp->z_refcnt);
1279 list_link_init(&zp->z_lnode);
1280 umem_free(zp, sizeof (*zp));
1284 ztest_zll_init(zll_t *zll)
1286 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1287 list_create(&zll->z_list, sizeof (ztest_znode_t),
1288 offsetof(ztest_znode_t, z_lnode));
1292 ztest_zll_destroy(zll_t *zll)
1294 list_destroy(&zll->z_list);
1295 mutex_destroy(&zll->z_lock);
1298 #define RL_TAG "range_lock"
1299 static ztest_znode_t *
1300 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1302 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1303 ztest_znode_t *zp = NULL;
1304 mutex_enter(&zll->z_lock);
1305 for (zp = list_head(&zll->z_list); (zp);
1306 zp = list_next(&zll->z_list, zp)) {
1307 if (zp->z_object == object) {
1308 refcount_add(&zp->z_refcnt, RL_TAG);
1313 zp = ztest_znode_init(object);
1314 refcount_add(&zp->z_refcnt, RL_TAG);
1315 list_insert_head(&zll->z_list, zp);
1317 mutex_exit(&zll->z_lock);
1322 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1325 ASSERT3U(zp->z_object, !=, 0);
1326 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1327 mutex_enter(&zll->z_lock);
1328 refcount_remove(&zp->z_refcnt, RL_TAG);
1329 if (refcount_is_zero(&zp->z_refcnt)) {
1330 list_remove(&zll->z_list, zp);
1331 ztest_znode_fini(zp);
1333 mutex_exit(&zll->z_lock);
1338 ztest_rll_init(rll_t *rll)
1340 rll->rll_writer = NULL;
1341 rll->rll_readers = 0;
1342 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1343 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1347 ztest_rll_destroy(rll_t *rll)
1349 ASSERT(rll->rll_writer == NULL);
1350 ASSERT(rll->rll_readers == 0);
1351 mutex_destroy(&rll->rll_lock);
1352 cv_destroy(&rll->rll_cv);
1356 ztest_rll_lock(rll_t *rll, rl_type_t type)
1358 mutex_enter(&rll->rll_lock);
1360 if (type == RL_READER) {
1361 while (rll->rll_writer != NULL)
1362 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1365 while (rll->rll_writer != NULL || rll->rll_readers)
1366 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1367 rll->rll_writer = curthread;
1370 mutex_exit(&rll->rll_lock);
1374 ztest_rll_unlock(rll_t *rll)
1376 mutex_enter(&rll->rll_lock);
1378 if (rll->rll_writer) {
1379 ASSERT(rll->rll_readers == 0);
1380 rll->rll_writer = NULL;
1382 ASSERT(rll->rll_readers != 0);
1383 ASSERT(rll->rll_writer == NULL);
1387 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1388 cv_broadcast(&rll->rll_cv);
1390 mutex_exit(&rll->rll_lock);
1394 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1396 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1398 ztest_rll_lock(rll, type);
1402 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1404 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1406 ztest_rll_unlock(rll);
1409 static ztest_zrl_t *
1410 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1412 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1414 zrl->z_ztznode = zp;
1419 ztest_zrl_fini(ztest_zrl_t *zrl)
1421 umem_free(zrl, sizeof (*zrl));
1424 static ztest_zrl_t *
1425 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1426 uint64_t size, rl_type_t type)
1428 ztest_znode_t *zp = ztest_znode_get(zd, object);
1429 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1431 return (ztest_zrl_init(rl, zp));
1435 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1437 zfs_range_unlock(zrl->z_rl);
1438 ztest_znode_put(zd, zrl->z_ztznode);
1439 ztest_zrl_fini(zrl);
1443 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1446 zd->zd_zilog = dmu_objset_zil(os);
1447 zd->zd_shared = szd;
1448 dmu_objset_name(os, zd->zd_name);
1451 if (zd->zd_shared != NULL)
1452 zd->zd_shared->zd_seq = 0;
1454 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1455 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1457 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1458 ztest_rll_init(&zd->zd_object_lock[l]);
1460 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1461 ztest_zll_init(&zd->zd_range_lock[l]);
1465 ztest_zd_fini(ztest_ds_t *zd)
1469 mutex_destroy(&zd->zd_dirobj_lock);
1470 (void) rwlock_destroy(&zd->zd_zilog_lock);
1472 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1473 ztest_rll_destroy(&zd->zd_object_lock[l]);
1475 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1476 ztest_zll_destroy(&zd->zd_range_lock[l]);
1479 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1482 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1488 * Attempt to assign tx to some transaction group.
1490 error = dmu_tx_assign(tx, txg_how);
1492 if (error == ERESTART) {
1493 ASSERT(txg_how == TXG_NOWAIT);
1496 ASSERT3U(error, ==, ENOSPC);
1497 ztest_record_enospc(tag);
1502 txg = dmu_tx_get_txg(tx);
1508 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1511 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1519 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1522 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1526 diff |= (value - *ip++);
1533 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1534 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1537 bt->bt_magic = BT_MAGIC;
1538 bt->bt_objset = dmu_objset_id(os);
1539 bt->bt_object = object;
1540 bt->bt_dnodesize = dnodesize;
1541 bt->bt_offset = offset;
1544 bt->bt_crtxg = crtxg;
1548 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1549 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1552 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1553 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1554 ASSERT3U(bt->bt_object, ==, object);
1555 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1556 ASSERT3U(bt->bt_offset, ==, offset);
1557 ASSERT3U(bt->bt_gen, <=, gen);
1558 ASSERT3U(bt->bt_txg, <=, txg);
1559 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1562 static ztest_block_tag_t *
1563 ztest_bt_bonus(dmu_buf_t *db)
1565 dmu_object_info_t doi;
1566 ztest_block_tag_t *bt;
1568 dmu_object_info_from_db(db, &doi);
1569 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1570 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1571 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1577 * Generate a token to fill up unused bonus buffer space. Try to make
1578 * it unique to the object, generation, and offset to verify that data
1579 * is not getting overwritten by data from other dnodes.
1581 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1582 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1585 * Fill up the unused bonus buffer region before the block tag with a
1586 * verifiable pattern. Filling the whole bonus area with non-zero data
1587 * helps ensure that all dnode traversal code properly skips the
1588 * interior regions of large dnodes.
1591 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1592 objset_t *os, uint64_t gen)
1596 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1598 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1599 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1600 gen, bonusp - (uint64_t *)db->db_data);
1606 * Verify that the unused area of a bonus buffer is filled with the
1610 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1611 objset_t *os, uint64_t gen)
1615 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1616 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1617 gen, bonusp - (uint64_t *)db->db_data);
1618 VERIFY3U(*bonusp, ==, token);
1626 #define lrz_type lr_mode
1627 #define lrz_blocksize lr_uid
1628 #define lrz_ibshift lr_gid
1629 #define lrz_bonustype lr_rdev
1630 #define lrz_dnodesize lr_crtime[1]
1633 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1635 char *name = (void *)(lr + 1); /* name follows lr */
1636 size_t namesize = strlen(name) + 1;
1639 if (zil_replaying(zd->zd_zilog, tx))
1642 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1643 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1644 sizeof (*lr) + namesize - sizeof (lr_t));
1646 zil_itx_assign(zd->zd_zilog, itx, tx);
1650 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1652 char *name = (void *)(lr + 1); /* name follows lr */
1653 size_t namesize = strlen(name) + 1;
1656 if (zil_replaying(zd->zd_zilog, tx))
1659 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1660 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1661 sizeof (*lr) + namesize - sizeof (lr_t));
1663 itx->itx_oid = object;
1664 zil_itx_assign(zd->zd_zilog, itx, tx);
1668 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1671 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1673 if (zil_replaying(zd->zd_zilog, tx))
1676 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1677 write_state = WR_INDIRECT;
1679 itx = zil_itx_create(TX_WRITE,
1680 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1682 if (write_state == WR_COPIED &&
1683 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1684 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1685 zil_itx_destroy(itx);
1686 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1687 write_state = WR_NEED_COPY;
1689 itx->itx_private = zd;
1690 itx->itx_wr_state = write_state;
1691 itx->itx_sync = (ztest_random(8) == 0);
1693 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1694 sizeof (*lr) - sizeof (lr_t));
1696 zil_itx_assign(zd->zd_zilog, itx, tx);
1700 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1704 if (zil_replaying(zd->zd_zilog, tx))
1707 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1708 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1709 sizeof (*lr) - sizeof (lr_t));
1711 itx->itx_sync = B_FALSE;
1712 zil_itx_assign(zd->zd_zilog, itx, tx);
1716 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1720 if (zil_replaying(zd->zd_zilog, tx))
1723 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1724 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1725 sizeof (*lr) - sizeof (lr_t));
1727 itx->itx_sync = B_FALSE;
1728 zil_itx_assign(zd->zd_zilog, itx, tx);
1735 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1737 ztest_ds_t *zd = arg1;
1738 lr_create_t *lr = arg2;
1739 char *name = (void *)(lr + 1); /* name follows lr */
1740 objset_t *os = zd->zd_os;
1741 ztest_block_tag_t *bbt;
1749 byteswap_uint64_array(lr, sizeof (*lr));
1751 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1752 ASSERT(name[0] != '\0');
1754 tx = dmu_tx_create(os);
1756 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1758 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1759 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1761 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1764 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1768 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1769 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1771 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1772 if (lr->lr_foid == 0) {
1773 lr->lr_foid = zap_create_dnsize(os,
1774 lr->lrz_type, lr->lrz_bonustype,
1775 bonuslen, lr->lrz_dnodesize, tx);
1777 error = zap_create_claim_dnsize(os, lr->lr_foid,
1778 lr->lrz_type, lr->lrz_bonustype,
1779 bonuslen, lr->lrz_dnodesize, tx);
1782 if (lr->lr_foid == 0) {
1783 lr->lr_foid = dmu_object_alloc_dnsize(os,
1784 lr->lrz_type, 0, lr->lrz_bonustype,
1785 bonuslen, lr->lrz_dnodesize, tx);
1787 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1788 lr->lrz_type, 0, lr->lrz_bonustype,
1789 bonuslen, lr->lrz_dnodesize, tx);
1794 ASSERT3U(error, ==, EEXIST);
1795 ASSERT(zd->zd_zilog->zl_replay);
1800 ASSERT(lr->lr_foid != 0);
1802 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1803 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1804 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1806 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1807 bbt = ztest_bt_bonus(db);
1808 dmu_buf_will_dirty(db, tx);
1809 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1810 lr->lr_gen, txg, txg);
1811 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1812 dmu_buf_rele(db, FTAG);
1814 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1817 (void) ztest_log_create(zd, tx, lr);
1825 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1827 ztest_ds_t *zd = arg1;
1828 lr_remove_t *lr = arg2;
1829 char *name = (void *)(lr + 1); /* name follows lr */
1830 objset_t *os = zd->zd_os;
1831 dmu_object_info_t doi;
1833 uint64_t object, txg;
1836 byteswap_uint64_array(lr, sizeof (*lr));
1838 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1839 ASSERT(name[0] != '\0');
1842 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1843 ASSERT(object != 0);
1845 ztest_object_lock(zd, object, RL_WRITER);
1847 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1849 tx = dmu_tx_create(os);
1851 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1852 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1854 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1856 ztest_object_unlock(zd, object);
1860 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1861 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1863 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1866 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1868 (void) ztest_log_remove(zd, tx, lr, object);
1872 ztest_object_unlock(zd, object);
1878 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1880 ztest_ds_t *zd = arg1;
1881 lr_write_t *lr = arg2;
1882 objset_t *os = zd->zd_os;
1883 void *data = lr + 1; /* data follows lr */
1884 uint64_t offset, length;
1885 ztest_block_tag_t *bt = data;
1886 ztest_block_tag_t *bbt;
1887 uint64_t gen, txg, lrtxg, crtxg;
1888 dmu_object_info_t doi;
1891 arc_buf_t *abuf = NULL;
1895 byteswap_uint64_array(lr, sizeof (*lr));
1897 offset = lr->lr_offset;
1898 length = lr->lr_length;
1900 /* If it's a dmu_sync() block, write the whole block */
1901 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1902 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1903 if (length < blocksize) {
1904 offset -= offset % blocksize;
1909 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1910 byteswap_uint64_array(bt, sizeof (*bt));
1912 if (bt->bt_magic != BT_MAGIC)
1915 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1916 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1918 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1920 dmu_object_info_from_db(db, &doi);
1922 bbt = ztest_bt_bonus(db);
1923 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1925 crtxg = bbt->bt_crtxg;
1926 lrtxg = lr->lr_common.lrc_txg;
1928 tx = dmu_tx_create(os);
1930 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1932 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1933 P2PHASE(offset, length) == 0)
1934 abuf = dmu_request_arcbuf(db, length);
1936 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1939 dmu_return_arcbuf(abuf);
1940 dmu_buf_rele(db, FTAG);
1941 ztest_range_unlock(zd, rl);
1942 ztest_object_unlock(zd, lr->lr_foid);
1948 * Usually, verify the old data before writing new data --
1949 * but not always, because we also want to verify correct
1950 * behavior when the data was not recently read into cache.
1952 ASSERT(offset % doi.doi_data_block_size == 0);
1953 if (ztest_random(4) != 0) {
1954 int prefetch = ztest_random(2) ?
1955 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1956 ztest_block_tag_t rbt;
1958 VERIFY(dmu_read(os, lr->lr_foid, offset,
1959 sizeof (rbt), &rbt, prefetch) == 0);
1960 if (rbt.bt_magic == BT_MAGIC) {
1961 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1962 offset, gen, txg, crtxg);
1967 * Writes can appear to be newer than the bonus buffer because
1968 * the ztest_get_data() callback does a dmu_read() of the
1969 * open-context data, which may be different than the data
1970 * as it was when the write was generated.
1972 if (zd->zd_zilog->zl_replay) {
1973 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1974 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1979 * Set the bt's gen/txg to the bonus buffer's gen/txg
1980 * so that all of the usual ASSERTs will work.
1982 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1987 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1989 bcopy(data, abuf->b_data, length);
1990 dmu_assign_arcbuf_by_dbuf(db, offset, abuf, tx);
1993 (void) ztest_log_write(zd, tx, lr);
1995 dmu_buf_rele(db, FTAG);
1999 ztest_range_unlock(zd, rl);
2000 ztest_object_unlock(zd, lr->lr_foid);
2006 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
2008 ztest_ds_t *zd = arg1;
2009 lr_truncate_t *lr = arg2;
2010 objset_t *os = zd->zd_os;
2016 byteswap_uint64_array(lr, sizeof (*lr));
2018 ztest_object_lock(zd, lr->lr_foid, RL_READER);
2019 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
2022 tx = dmu_tx_create(os);
2024 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
2026 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2028 ztest_range_unlock(zd, rl);
2029 ztest_object_unlock(zd, lr->lr_foid);
2033 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
2034 lr->lr_length, tx) == 0);
2036 (void) ztest_log_truncate(zd, tx, lr);
2040 ztest_range_unlock(zd, rl);
2041 ztest_object_unlock(zd, lr->lr_foid);
2047 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
2049 ztest_ds_t *zd = arg1;
2050 lr_setattr_t *lr = arg2;
2051 objset_t *os = zd->zd_os;
2054 ztest_block_tag_t *bbt;
2055 uint64_t txg, lrtxg, crtxg, dnodesize;
2058 byteswap_uint64_array(lr, sizeof (*lr));
2060 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
2062 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
2064 tx = dmu_tx_create(os);
2065 dmu_tx_hold_bonus(tx, lr->lr_foid);
2067 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2069 dmu_buf_rele(db, FTAG);
2070 ztest_object_unlock(zd, lr->lr_foid);
2074 bbt = ztest_bt_bonus(db);
2075 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2076 crtxg = bbt->bt_crtxg;
2077 lrtxg = lr->lr_common.lrc_txg;
2078 dnodesize = bbt->bt_dnodesize;
2080 if (zd->zd_zilog->zl_replay) {
2081 ASSERT(lr->lr_size != 0);
2082 ASSERT(lr->lr_mode != 0);
2086 * Randomly change the size and increment the generation.
2088 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2090 lr->lr_mode = bbt->bt_gen + 1;
2095 * Verify that the current bonus buffer is not newer than our txg.
2097 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2098 MAX(txg, lrtxg), crtxg);
2100 dmu_buf_will_dirty(db, tx);
2102 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2103 ASSERT3U(lr->lr_size, <=, db->db_size);
2104 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2105 bbt = ztest_bt_bonus(db);
2107 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2109 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2110 dmu_buf_rele(db, FTAG);
2112 (void) ztest_log_setattr(zd, tx, lr);
2116 ztest_object_unlock(zd, lr->lr_foid);
2121 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
2122 NULL, /* 0 no such transaction type */
2123 ztest_replay_create, /* TX_CREATE */
2124 NULL, /* TX_MKDIR */
2125 NULL, /* TX_MKXATTR */
2126 NULL, /* TX_SYMLINK */
2127 ztest_replay_remove, /* TX_REMOVE */
2128 NULL, /* TX_RMDIR */
2130 NULL, /* TX_RENAME */
2131 ztest_replay_write, /* TX_WRITE */
2132 ztest_replay_truncate, /* TX_TRUNCATE */
2133 ztest_replay_setattr, /* TX_SETATTR */
2135 NULL, /* TX_CREATE_ACL */
2136 NULL, /* TX_CREATE_ATTR */
2137 NULL, /* TX_CREATE_ACL_ATTR */
2138 NULL, /* TX_MKDIR_ACL */
2139 NULL, /* TX_MKDIR_ATTR */
2140 NULL, /* TX_MKDIR_ACL_ATTR */
2141 NULL, /* TX_WRITE2 */
2145 * ZIL get_data callbacks
2147 typedef struct ztest_zgd_private {
2151 } ztest_zgd_private_t;
2154 ztest_get_done(zgd_t *zgd, int error)
2156 ztest_zgd_private_t *zzp = zgd->zgd_private;
2157 ztest_ds_t *zd = zzp->z_zd;
2158 uint64_t object = zzp->z_object;
2161 dmu_buf_rele(zgd->zgd_db, zgd);
2163 ztest_range_unlock(zd, zzp->z_rl);
2164 ztest_object_unlock(zd, object);
2166 if (error == 0 && zgd->zgd_bp)
2167 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
2169 umem_free(zgd, sizeof (*zgd));
2170 umem_free(zzp, sizeof (*zzp));
2174 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
2177 ztest_ds_t *zd = arg;
2178 objset_t *os = zd->zd_os;
2179 uint64_t object = lr->lr_foid;
2180 uint64_t offset = lr->lr_offset;
2181 uint64_t size = lr->lr_length;
2182 uint64_t txg = lr->lr_common.lrc_txg;
2184 dmu_object_info_t doi;
2188 ztest_zgd_private_t *zgd_private;
2190 ASSERT3P(lwb, !=, NULL);
2191 ASSERT3P(zio, !=, NULL);
2192 ASSERT3U(size, !=, 0);
2194 ztest_object_lock(zd, object, RL_READER);
2195 error = dmu_bonus_hold(os, object, FTAG, &db);
2197 ztest_object_unlock(zd, object);
2201 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2203 if (crtxg == 0 || crtxg > txg) {
2204 dmu_buf_rele(db, FTAG);
2205 ztest_object_unlock(zd, object);
2209 dmu_object_info_from_db(db, &doi);
2210 dmu_buf_rele(db, FTAG);
2213 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2215 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2216 zgd_private->z_zd = zd;
2217 zgd_private->z_object = object;
2218 zgd->zgd_private = zgd_private;
2220 if (buf != NULL) { /* immediate write */
2221 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2223 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2225 error = dmu_read(os, object, offset, size, buf,
2226 DMU_READ_NO_PREFETCH);
2229 size = doi.doi_data_block_size;
2231 offset = P2ALIGN(offset, size);
2233 ASSERT(offset < size);
2237 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2239 zgd->zgd_rl = zgd_private->z_rl->z_rl;
2241 error = dmu_buf_hold(os, object, offset, zgd, &db,
2242 DMU_READ_NO_PREFETCH);
2245 blkptr_t *bp = &lr->lr_blkptr;
2250 ASSERT(db->db_offset == offset);
2251 ASSERT(db->db_size == size);
2253 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2254 ztest_get_done, zgd);
2261 ztest_get_done(zgd, error);
2267 ztest_lr_alloc(size_t lrsize, char *name)
2270 size_t namesize = name ? strlen(name) + 1 : 0;
2272 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2275 bcopy(name, lr + lrsize, namesize);
2281 ztest_lr_free(void *lr, size_t lrsize, char *name)
2283 size_t namesize = name ? strlen(name) + 1 : 0;
2285 umem_free(lr, lrsize + namesize);
2289 * Lookup a bunch of objects. Returns the number of objects not found.
2292 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2298 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2300 for (i = 0; i < count; i++, od++) {
2302 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2303 sizeof (uint64_t), 1, &od->od_object);
2305 ASSERT(error == ENOENT);
2306 ASSERT(od->od_object == 0);
2310 ztest_block_tag_t *bbt;
2311 dmu_object_info_t doi;
2313 ASSERT(od->od_object != 0);
2314 ASSERT(missing == 0); /* there should be no gaps */
2316 ztest_object_lock(zd, od->od_object, RL_READER);
2317 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2318 od->od_object, FTAG, &db));
2319 dmu_object_info_from_db(db, &doi);
2320 bbt = ztest_bt_bonus(db);
2321 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2322 od->od_type = doi.doi_type;
2323 od->od_blocksize = doi.doi_data_block_size;
2324 od->od_gen = bbt->bt_gen;
2325 dmu_buf_rele(db, FTAG);
2326 ztest_object_unlock(zd, od->od_object);
2334 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2339 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2341 for (i = 0; i < count; i++, od++) {
2348 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2350 lr->lr_doid = od->od_dir;
2351 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2352 lr->lrz_type = od->od_crtype;
2353 lr->lrz_blocksize = od->od_crblocksize;
2354 lr->lrz_ibshift = ztest_random_ibshift();
2355 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2356 lr->lrz_dnodesize = od->od_crdnodesize;
2357 lr->lr_gen = od->od_crgen;
2358 lr->lr_crtime[0] = time(NULL);
2360 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2361 ASSERT(missing == 0);
2365 od->od_object = lr->lr_foid;
2366 od->od_type = od->od_crtype;
2367 od->od_blocksize = od->od_crblocksize;
2368 od->od_gen = od->od_crgen;
2369 ASSERT(od->od_object != 0);
2372 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2379 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2385 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2389 for (i = count - 1; i >= 0; i--, od--) {
2396 * No object was found.
2398 if (od->od_object == 0)
2401 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2403 lr->lr_doid = od->od_dir;
2405 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2406 ASSERT3U(error, ==, ENOSPC);
2411 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2418 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2424 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2426 lr->lr_foid = object;
2427 lr->lr_offset = offset;
2428 lr->lr_length = size;
2430 BP_ZERO(&lr->lr_blkptr);
2432 bcopy(data, lr + 1, size);
2434 error = ztest_replay_write(zd, lr, B_FALSE);
2436 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2442 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2447 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2449 lr->lr_foid = object;
2450 lr->lr_offset = offset;
2451 lr->lr_length = size;
2453 error = ztest_replay_truncate(zd, lr, B_FALSE);
2455 ztest_lr_free(lr, sizeof (*lr), NULL);
2461 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2466 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2468 lr->lr_foid = object;
2472 error = ztest_replay_setattr(zd, lr, B_FALSE);
2474 ztest_lr_free(lr, sizeof (*lr), NULL);
2480 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2482 objset_t *os = zd->zd_os;
2487 txg_wait_synced(dmu_objset_pool(os), 0);
2489 ztest_object_lock(zd, object, RL_READER);
2490 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2492 tx = dmu_tx_create(os);
2494 dmu_tx_hold_write(tx, object, offset, size);
2496 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2499 dmu_prealloc(os, object, offset, size, tx);
2501 txg_wait_synced(dmu_objset_pool(os), txg);
2503 (void) dmu_free_long_range(os, object, offset, size);
2506 ztest_range_unlock(zd, rl);
2507 ztest_object_unlock(zd, object);
2511 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2514 ztest_block_tag_t wbt;
2515 dmu_object_info_t doi;
2516 enum ztest_io_type io_type;
2520 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2521 blocksize = doi.doi_data_block_size;
2522 data = umem_alloc(blocksize, UMEM_NOFAIL);
2525 * Pick an i/o type at random, biased toward writing block tags.
2527 io_type = ztest_random(ZTEST_IO_TYPES);
2528 if (ztest_random(2) == 0)
2529 io_type = ZTEST_IO_WRITE_TAG;
2531 (void) rw_rdlock(&zd->zd_zilog_lock);
2535 case ZTEST_IO_WRITE_TAG:
2536 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2538 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2541 case ZTEST_IO_WRITE_PATTERN:
2542 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2543 if (ztest_random(2) == 0) {
2545 * Induce fletcher2 collisions to ensure that
2546 * zio_ddt_collision() detects and resolves them
2547 * when using fletcher2-verify for deduplication.
2549 ((uint64_t *)data)[0] ^= 1ULL << 63;
2550 ((uint64_t *)data)[4] ^= 1ULL << 63;
2552 (void) ztest_write(zd, object, offset, blocksize, data);
2555 case ZTEST_IO_WRITE_ZEROES:
2556 bzero(data, blocksize);
2557 (void) ztest_write(zd, object, offset, blocksize, data);
2560 case ZTEST_IO_TRUNCATE:
2561 (void) ztest_truncate(zd, object, offset, blocksize);
2564 case ZTEST_IO_SETATTR:
2565 (void) ztest_setattr(zd, object);
2570 case ZTEST_IO_REWRITE:
2571 (void) rw_rdlock(&ztest_name_lock);
2572 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2573 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2575 VERIFY(err == 0 || err == ENOSPC);
2576 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2577 ZFS_PROP_COMPRESSION,
2578 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2580 VERIFY(err == 0 || err == ENOSPC);
2581 (void) rw_unlock(&ztest_name_lock);
2583 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2584 DMU_READ_NO_PREFETCH));
2586 (void) ztest_write(zd, object, offset, blocksize, data);
2590 (void) rw_unlock(&zd->zd_zilog_lock);
2592 umem_free(data, blocksize);
2596 * Initialize an object description template.
2599 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2600 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2603 od->od_dir = ZTEST_DIROBJ;
2606 od->od_crtype = type;
2607 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2608 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2611 od->od_type = DMU_OT_NONE;
2612 od->od_blocksize = 0;
2615 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2616 tag, (longlong_t)id, (u_longlong_t)index);
2620 * Lookup or create the objects for a test using the od template.
2621 * If the objects do not all exist, or if 'remove' is specified,
2622 * remove any existing objects and create new ones. Otherwise,
2623 * use the existing objects.
2626 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2628 int count = size / sizeof (*od);
2631 mutex_enter(&zd->zd_dirobj_lock);
2632 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2633 (ztest_remove(zd, od, count) != 0 ||
2634 ztest_create(zd, od, count) != 0))
2637 mutex_exit(&zd->zd_dirobj_lock);
2644 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2646 zilog_t *zilog = zd->zd_zilog;
2648 (void) rw_rdlock(&zd->zd_zilog_lock);
2650 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2653 * Remember the committed values in zd, which is in parent/child
2654 * shared memory. If we die, the next iteration of ztest_run()
2655 * will verify that the log really does contain this record.
2657 mutex_enter(&zilog->zl_lock);
2658 ASSERT(zd->zd_shared != NULL);
2659 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2660 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2661 mutex_exit(&zilog->zl_lock);
2663 (void) rw_unlock(&zd->zd_zilog_lock);
2667 * This function is designed to simulate the operations that occur during a
2668 * mount/unmount operation. We hold the dataset across these operations in an
2669 * attempt to expose any implicit assumptions about ZIL management.
2673 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2675 objset_t *os = zd->zd_os;
2678 * We grab the zd_dirobj_lock to ensure that no other thread is
2679 * updating the zil (i.e. adding in-memory log records) and the
2680 * zd_zilog_lock to block any I/O.
2682 mutex_enter(&zd->zd_dirobj_lock);
2683 (void) rw_wrlock(&zd->zd_zilog_lock);
2685 /* zfsvfs_teardown() */
2686 zil_close(zd->zd_zilog);
2688 /* zfsvfs_setup() */
2689 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2690 zil_replay(os, zd, ztest_replay_vector);
2692 (void) rw_unlock(&zd->zd_zilog_lock);
2693 mutex_exit(&zd->zd_dirobj_lock);
2697 * Verify that we can't destroy an active pool, create an existing pool,
2698 * or create a pool with a bad vdev spec.
2702 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2704 ztest_shared_opts_t *zo = &ztest_opts;
2708 if (zo->zo_mmp_test)
2712 * Attempt to create using a bad file.
2714 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2715 VERIFY3U(ENOENT, ==,
2716 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2717 nvlist_free(nvroot);
2720 * Attempt to create using a bad mirror.
2722 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2723 VERIFY3U(ENOENT, ==,
2724 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2725 nvlist_free(nvroot);
2728 * Attempt to create an existing pool. It shouldn't matter
2729 * what's in the nvroot; we should fail with EEXIST.
2731 (void) rw_rdlock(&ztest_name_lock);
2732 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2733 VERIFY3U(EEXIST, ==,
2734 spa_create(zo->zo_pool, nvroot, NULL, NULL, NULL));
2735 nvlist_free(nvroot);
2736 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2737 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2738 spa_close(spa, FTAG);
2740 (void) rw_unlock(&ztest_name_lock);
2744 * Start and then stop the MMP threads to ensure the startup and shutdown code
2745 * works properly. Actual protection and property-related code tested via ZTS.
2749 ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2751 ztest_shared_opts_t *zo = &ztest_opts;
2752 spa_t *spa = ztest_spa;
2754 if (zo->zo_mmp_test)
2757 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2758 mutex_enter(&spa->spa_props_lock);
2760 if (!spa_multihost(spa)) {
2761 spa->spa_multihost = B_TRUE;
2762 mmp_thread_start(spa);
2765 mutex_exit(&spa->spa_props_lock);
2766 spa_config_exit(spa, SCL_CONFIG, FTAG);
2768 txg_wait_synced(spa_get_dsl(spa), 0);
2769 mmp_signal_all_threads();
2770 txg_wait_synced(spa_get_dsl(spa), 0);
2772 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2773 mutex_enter(&spa->spa_props_lock);
2775 if (spa_multihost(spa)) {
2776 mmp_thread_stop(spa);
2777 spa->spa_multihost = B_FALSE;
2780 mutex_exit(&spa->spa_props_lock);
2781 spa_config_exit(spa, SCL_CONFIG, FTAG);
2786 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2789 uint64_t initial_version = SPA_VERSION_INITIAL;
2790 uint64_t version, newversion;
2791 nvlist_t *nvroot, *props;
2794 if (ztest_opts.zo_mmp_test)
2797 mutex_enter(&ztest_vdev_lock);
2798 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2801 * Clean up from previous runs.
2803 (void) spa_destroy(name);
2805 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2806 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2809 * If we're configuring a RAIDZ device then make sure that the
2810 * the initial version is capable of supporting that feature.
2812 switch (ztest_opts.zo_raidz_parity) {
2815 initial_version = SPA_VERSION_INITIAL;
2818 initial_version = SPA_VERSION_RAIDZ2;
2821 initial_version = SPA_VERSION_RAIDZ3;
2826 * Create a pool with a spa version that can be upgraded. Pick
2827 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2830 version = ztest_random_spa_version(initial_version);
2831 } while (version > SPA_VERSION_BEFORE_FEATURES);
2833 props = fnvlist_alloc();
2834 fnvlist_add_uint64(props,
2835 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2836 VERIFY3S(spa_create(name, nvroot, props, NULL, NULL), ==, 0);
2837 fnvlist_free(nvroot);
2838 fnvlist_free(props);
2840 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2841 VERIFY3U(spa_version(spa), ==, version);
2842 newversion = ztest_random_spa_version(version + 1);
2844 if (ztest_opts.zo_verbose >= 4) {
2845 (void) printf("upgrading spa version from %llu to %llu\n",
2846 (u_longlong_t)version, (u_longlong_t)newversion);
2849 spa_upgrade(spa, newversion);
2850 VERIFY3U(spa_version(spa), >, version);
2851 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2852 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2853 spa_close(spa, FTAG);
2856 mutex_exit(&ztest_vdev_lock);
2860 vdev_lookup_by_path(vdev_t *vd, const char *path)
2865 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2868 for (c = 0; c < vd->vdev_children; c++)
2869 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2877 * Find the first available hole which can be used as a top-level.
2880 find_vdev_hole(spa_t *spa)
2882 vdev_t *rvd = spa->spa_root_vdev;
2885 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2887 for (c = 0; c < rvd->vdev_children; c++) {
2888 vdev_t *cvd = rvd->vdev_child[c];
2890 if (cvd->vdev_ishole)
2897 * Verify that vdev_add() works as expected.
2901 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2903 ztest_shared_t *zs = ztest_shared;
2904 spa_t *spa = ztest_spa;
2910 if (ztest_opts.zo_mmp_test)
2913 mutex_enter(&ztest_vdev_lock);
2914 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2916 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2918 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2921 * If we have slogs then remove them 1/4 of the time.
2923 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2925 * Grab the guid from the head of the log class rotor.
2927 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2929 spa_config_exit(spa, SCL_VDEV, FTAG);
2932 * We have to grab the zs_name_lock as writer to
2933 * prevent a race between removing a slog (dmu_objset_find)
2934 * and destroying a dataset. Removing the slog will
2935 * grab a reference on the dataset which may cause
2936 * dsl_destroy_head() to fail with EBUSY thus
2937 * leaving the dataset in an inconsistent state.
2939 rw_wrlock(&ztest_name_lock);
2940 error = spa_vdev_remove(spa, guid, B_FALSE);
2941 rw_unlock(&ztest_name_lock);
2943 if (error && error != EEXIST)
2944 fatal(0, "spa_vdev_remove() = %d", error);
2946 spa_config_exit(spa, SCL_VDEV, FTAG);
2949 * Make 1/4 of the devices be log devices.
2951 nvroot = make_vdev_root(NULL, NULL, NULL,
2952 ztest_opts.zo_vdev_size, 0,
2953 ztest_random(4) == 0, ztest_opts.zo_raidz,
2956 error = spa_vdev_add(spa, nvroot);
2957 nvlist_free(nvroot);
2959 if (error == ENOSPC)
2960 ztest_record_enospc("spa_vdev_add");
2961 else if (error != 0)
2962 fatal(0, "spa_vdev_add() = %d", error);
2965 mutex_exit(&ztest_vdev_lock);
2969 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2973 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2975 ztest_shared_t *zs = ztest_shared;
2976 spa_t *spa = ztest_spa;
2977 vdev_t *rvd = spa->spa_root_vdev;
2978 spa_aux_vdev_t *sav;
2984 if (ztest_opts.zo_mmp_test)
2987 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2989 if (ztest_random(2) == 0) {
2990 sav = &spa->spa_spares;
2991 aux = ZPOOL_CONFIG_SPARES;
2993 sav = &spa->spa_l2cache;
2994 aux = ZPOOL_CONFIG_L2CACHE;
2997 mutex_enter(&ztest_vdev_lock);
2999 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3001 if (sav->sav_count != 0 && ztest_random(4) == 0) {
3003 * Pick a random device to remove.
3005 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3008 * Find an unused device we can add.
3010 zs->zs_vdev_aux = 0;
3013 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
3014 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3016 for (c = 0; c < sav->sav_count; c++)
3017 if (strcmp(sav->sav_vdevs[c]->vdev_path,
3020 if (c == sav->sav_count &&
3021 vdev_lookup_by_path(rvd, path) == NULL)
3027 spa_config_exit(spa, SCL_VDEV, FTAG);
3033 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3034 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
3035 error = spa_vdev_add(spa, nvroot);
3037 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
3038 nvlist_free(nvroot);
3041 * Remove an existing device. Sometimes, dirty its
3042 * vdev state first to make sure we handle removal
3043 * of devices that have pending state changes.
3045 if (ztest_random(2) == 0)
3046 (void) vdev_online(spa, guid, 0, NULL);
3048 error = spa_vdev_remove(spa, guid, B_FALSE);
3049 if (error != 0 && error != EBUSY)
3050 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
3053 mutex_exit(&ztest_vdev_lock);
3055 umem_free(path, MAXPATHLEN);
3059 * split a pool if it has mirror tlvdevs
3063 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
3065 ztest_shared_t *zs = ztest_shared;
3066 spa_t *spa = ztest_spa;
3067 vdev_t *rvd = spa->spa_root_vdev;
3068 nvlist_t *tree, **child, *config, *split, **schild;
3069 uint_t c, children, schildren = 0, lastlogid = 0;
3072 if (ztest_opts.zo_mmp_test)
3075 mutex_enter(&ztest_vdev_lock);
3077 /* ensure we have a useable config; mirrors of raidz aren't supported */
3078 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3079 mutex_exit(&ztest_vdev_lock);
3083 /* clean up the old pool, if any */
3084 (void) spa_destroy("splitp");
3086 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3088 /* generate a config from the existing config */
3089 mutex_enter(&spa->spa_props_lock);
3090 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3092 mutex_exit(&spa->spa_props_lock);
3094 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3097 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3098 for (c = 0; c < children; c++) {
3099 vdev_t *tvd = rvd->vdev_child[c];
3103 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3104 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3106 VERIFY(nvlist_add_string(schild[schildren],
3107 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3108 VERIFY(nvlist_add_uint64(schild[schildren],
3109 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3111 lastlogid = schildren;
3116 VERIFY(nvlist_lookup_nvlist_array(child[c],
3117 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3118 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3121 /* OK, create a config that can be used to split */
3122 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3123 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3124 VDEV_TYPE_ROOT) == 0);
3125 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3126 lastlogid != 0 ? lastlogid : schildren) == 0);
3128 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3129 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3131 for (c = 0; c < schildren; c++)
3132 nvlist_free(schild[c]);
3136 spa_config_exit(spa, SCL_VDEV, FTAG);
3138 (void) rw_wrlock(&ztest_name_lock);
3139 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3140 (void) rw_unlock(&ztest_name_lock);
3142 nvlist_free(config);
3145 (void) printf("successful split - results:\n");
3146 mutex_enter(&spa_namespace_lock);
3147 show_pool_stats(spa);
3148 show_pool_stats(spa_lookup("splitp"));
3149 mutex_exit(&spa_namespace_lock);
3153 mutex_exit(&ztest_vdev_lock);
3158 * Verify that we can attach and detach devices.
3162 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3164 ztest_shared_t *zs = ztest_shared;
3165 spa_t *spa = ztest_spa;
3166 spa_aux_vdev_t *sav = &spa->spa_spares;
3167 vdev_t *rvd = spa->spa_root_vdev;
3168 vdev_t *oldvd, *newvd, *pvd;
3172 uint64_t ashift = ztest_get_ashift();
3173 uint64_t oldguid, pguid;
3174 uint64_t oldsize, newsize;
3175 char *oldpath, *newpath;
3177 int oldvd_has_siblings = B_FALSE;
3178 int newvd_is_spare = B_FALSE;
3180 int error, expected_error;
3182 if (ztest_opts.zo_mmp_test)
3185 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3186 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3188 mutex_enter(&ztest_vdev_lock);
3189 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3191 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3194 * Decide whether to do an attach or a replace.
3196 replacing = ztest_random(2);
3199 * Pick a random top-level vdev.
3201 top = ztest_random_vdev_top(spa, B_TRUE);
3204 * Pick a random leaf within it.
3206 leaf = ztest_random(leaves);
3211 oldvd = rvd->vdev_child[top];
3212 if (zs->zs_mirrors >= 1) {
3213 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3214 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3215 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3217 if (ztest_opts.zo_raidz > 1) {
3218 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3219 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3220 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3224 * If we're already doing an attach or replace, oldvd may be a
3225 * mirror vdev -- in which case, pick a random child.
3227 while (oldvd->vdev_children != 0) {
3228 oldvd_has_siblings = B_TRUE;
3229 ASSERT(oldvd->vdev_children >= 2);
3230 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3233 oldguid = oldvd->vdev_guid;
3234 oldsize = vdev_get_min_asize(oldvd);
3235 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3236 (void) strcpy(oldpath, oldvd->vdev_path);
3237 pvd = oldvd->vdev_parent;
3238 pguid = pvd->vdev_guid;
3241 * If oldvd has siblings, then half of the time, detach it.
3243 if (oldvd_has_siblings && ztest_random(2) == 0) {
3244 spa_config_exit(spa, SCL_VDEV, FTAG);
3245 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3246 if (error != 0 && error != ENODEV && error != EBUSY &&
3248 fatal(0, "detach (%s) returned %d", oldpath, error);
3253 * For the new vdev, choose with equal probability between the two
3254 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3256 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3257 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3258 newvd_is_spare = B_TRUE;
3259 (void) strcpy(newpath, newvd->vdev_path);
3261 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3262 ztest_opts.zo_dir, ztest_opts.zo_pool,
3263 top * leaves + leaf);
3264 if (ztest_random(2) == 0)
3265 newpath[strlen(newpath) - 1] = 'b';
3266 newvd = vdev_lookup_by_path(rvd, newpath);
3270 newsize = vdev_get_min_asize(newvd);
3273 * Make newsize a little bigger or smaller than oldsize.
3274 * If it's smaller, the attach should fail.
3275 * If it's larger, and we're doing a replace,
3276 * we should get dynamic LUN growth when we're done.
3278 newsize = 10 * oldsize / (9 + ztest_random(3));
3282 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3283 * unless it's a replace; in that case any non-replacing parent is OK.
3285 * If newvd is already part of the pool, it should fail with EBUSY.
3287 * If newvd is too small, it should fail with EOVERFLOW.
3289 if (pvd->vdev_ops != &vdev_mirror_ops &&
3290 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3291 pvd->vdev_ops == &vdev_replacing_ops ||
3292 pvd->vdev_ops == &vdev_spare_ops))
3293 expected_error = ENOTSUP;
3294 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3295 expected_error = ENOTSUP;
3296 else if (newvd == oldvd)
3297 expected_error = replacing ? 0 : EBUSY;
3298 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3299 expected_error = EBUSY;
3300 else if (newsize < oldsize)
3301 expected_error = EOVERFLOW;
3302 else if (ashift > oldvd->vdev_top->vdev_ashift)
3303 expected_error = EDOM;
3307 spa_config_exit(spa, SCL_VDEV, FTAG);
3310 * Build the nvlist describing newpath.
3312 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3313 ashift, 0, 0, 0, 1);
3315 error = spa_vdev_attach(spa, oldguid, root, replacing);
3320 * If our parent was the replacing vdev, but the replace completed,
3321 * then instead of failing with ENOTSUP we may either succeed,
3322 * fail with ENODEV, or fail with EOVERFLOW.
3324 if (expected_error == ENOTSUP &&
3325 (error == 0 || error == ENODEV || error == EOVERFLOW))
3326 expected_error = error;
3329 * If someone grew the LUN, the replacement may be too small.
3331 if (error == EOVERFLOW || error == EBUSY)
3332 expected_error = error;
3334 /* XXX workaround 6690467 */
3335 if (error != expected_error && expected_error != EBUSY) {
3336 fatal(0, "attach (%s %llu, %s %llu, %d) "
3337 "returned %d, expected %d",
3338 oldpath, oldsize, newpath,
3339 newsize, replacing, error, expected_error);
3342 mutex_exit(&ztest_vdev_lock);
3344 umem_free(oldpath, MAXPATHLEN);
3345 umem_free(newpath, MAXPATHLEN);
3349 * Callback function which expands the physical size of the vdev.
3352 grow_vdev(vdev_t *vd, void *arg)
3354 ASSERTV(spa_t *spa = vd->vdev_spa);
3355 size_t *newsize = arg;
3359 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3360 ASSERT(vd->vdev_ops->vdev_op_leaf);
3362 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3365 fsize = lseek(fd, 0, SEEK_END);
3366 VERIFY(ftruncate(fd, *newsize) == 0);
3368 if (ztest_opts.zo_verbose >= 6) {
3369 (void) printf("%s grew from %lu to %lu bytes\n",
3370 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3377 * Callback function which expands a given vdev by calling vdev_online().
3381 online_vdev(vdev_t *vd, void *arg)
3383 spa_t *spa = vd->vdev_spa;
3384 vdev_t *tvd = vd->vdev_top;
3385 uint64_t guid = vd->vdev_guid;
3386 uint64_t generation = spa->spa_config_generation + 1;
3387 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3390 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3391 ASSERT(vd->vdev_ops->vdev_op_leaf);
3393 /* Calling vdev_online will initialize the new metaslabs */
3394 spa_config_exit(spa, SCL_STATE, spa);
3395 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3396 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3399 * If vdev_online returned an error or the underlying vdev_open
3400 * failed then we abort the expand. The only way to know that
3401 * vdev_open fails is by checking the returned newstate.
3403 if (error || newstate != VDEV_STATE_HEALTHY) {
3404 if (ztest_opts.zo_verbose >= 5) {
3405 (void) printf("Unable to expand vdev, state %llu, "
3406 "error %d\n", (u_longlong_t)newstate, error);
3410 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3413 * Since we dropped the lock we need to ensure that we're
3414 * still talking to the original vdev. It's possible this
3415 * vdev may have been detached/replaced while we were
3416 * trying to online it.
3418 if (generation != spa->spa_config_generation) {
3419 if (ztest_opts.zo_verbose >= 5) {
3420 (void) printf("vdev configuration has changed, "
3421 "guid %llu, state %llu, expected gen %llu, "
3424 (u_longlong_t)tvd->vdev_state,
3425 (u_longlong_t)generation,
3426 (u_longlong_t)spa->spa_config_generation);
3434 * Traverse the vdev tree calling the supplied function.
3435 * We continue to walk the tree until we either have walked all
3436 * children or we receive a non-NULL return from the callback.
3437 * If a NULL callback is passed, then we just return back the first
3438 * leaf vdev we encounter.
3441 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3445 if (vd->vdev_ops->vdev_op_leaf) {
3449 return (func(vd, arg));
3452 for (c = 0; c < vd->vdev_children; c++) {
3453 vdev_t *cvd = vd->vdev_child[c];
3454 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3461 * Verify that dynamic LUN growth works as expected.
3465 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3467 spa_t *spa = ztest_spa;
3469 metaslab_class_t *mc;
3470 metaslab_group_t *mg;
3471 size_t psize, newsize;
3473 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3475 mutex_enter(&ztest_vdev_lock);
3476 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3478 top = ztest_random_vdev_top(spa, B_TRUE);
3480 tvd = spa->spa_root_vdev->vdev_child[top];
3483 old_ms_count = tvd->vdev_ms_count;
3484 old_class_space = metaslab_class_get_space(mc);
3487 * Determine the size of the first leaf vdev associated with
3488 * our top-level device.
3490 vd = vdev_walk_tree(tvd, NULL, NULL);
3491 ASSERT3P(vd, !=, NULL);
3492 ASSERT(vd->vdev_ops->vdev_op_leaf);
3494 psize = vd->vdev_psize;
3497 * We only try to expand the vdev if it's healthy, less than 4x its
3498 * original size, and it has a valid psize.
3500 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3501 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3502 spa_config_exit(spa, SCL_STATE, spa);
3503 mutex_exit(&ztest_vdev_lock);
3507 newsize = psize + psize / 8;
3508 ASSERT3U(newsize, >, psize);
3510 if (ztest_opts.zo_verbose >= 6) {
3511 (void) printf("Expanding LUN %s from %lu to %lu\n",
3512 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3516 * Growing the vdev is a two step process:
3517 * 1). expand the physical size (i.e. relabel)
3518 * 2). online the vdev to create the new metaslabs
3520 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3521 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3522 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3523 if (ztest_opts.zo_verbose >= 5) {
3524 (void) printf("Could not expand LUN because "
3525 "the vdev configuration changed.\n");
3527 spa_config_exit(spa, SCL_STATE, spa);
3528 mutex_exit(&ztest_vdev_lock);
3532 spa_config_exit(spa, SCL_STATE, spa);
3535 * Expanding the LUN will update the config asynchronously,
3536 * thus we must wait for the async thread to complete any
3537 * pending tasks before proceeding.
3541 mutex_enter(&spa->spa_async_lock);
3542 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3543 mutex_exit(&spa->spa_async_lock);
3546 txg_wait_synced(spa_get_dsl(spa), 0);
3547 (void) poll(NULL, 0, 100);
3550 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3552 tvd = spa->spa_root_vdev->vdev_child[top];
3553 new_ms_count = tvd->vdev_ms_count;
3554 new_class_space = metaslab_class_get_space(mc);
3556 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3557 if (ztest_opts.zo_verbose >= 5) {
3558 (void) printf("Could not verify LUN expansion due to "
3559 "intervening vdev offline or remove.\n");
3561 spa_config_exit(spa, SCL_STATE, spa);
3562 mutex_exit(&ztest_vdev_lock);
3567 * Make sure we were able to grow the vdev.
3569 if (new_ms_count <= old_ms_count)
3570 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3571 old_ms_count, new_ms_count);
3574 * Make sure we were able to grow the pool.
3576 if (new_class_space <= old_class_space)
3577 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3578 old_class_space, new_class_space);
3580 if (ztest_opts.zo_verbose >= 5) {
3581 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3583 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3584 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3585 (void) printf("%s grew from %s to %s\n",
3586 spa->spa_name, oldnumbuf, newnumbuf);
3589 spa_config_exit(spa, SCL_STATE, spa);
3590 mutex_exit(&ztest_vdev_lock);
3594 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3598 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3601 * Create the objects common to all ztest datasets.
3603 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3604 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3608 ztest_dataset_create(char *dsname)
3612 dsl_crypto_params_t *dcp = NULL;
3615 * 50% of the time, we create encrypted datasets
3616 * using a random cipher suite and a hard-coded
3619 rand = ztest_random(2);
3621 nvlist_t *crypto_args = fnvlist_alloc();
3622 nvlist_t *props = fnvlist_alloc();
3624 /* slight bias towards the default cipher suite */
3625 rand = ztest_random(ZIO_CRYPT_FUNCTIONS);
3626 if (rand < ZIO_CRYPT_AES_128_CCM)
3627 rand = ZIO_CRYPT_ON;
3629 fnvlist_add_uint64(props,
3630 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), rand);
3631 fnvlist_add_uint8_array(crypto_args, "wkeydata",
3632 (uint8_t *)ztest_wkeydata, WRAPPING_KEY_LEN);
3635 * These parameters aren't really used by the kernel. They
3636 * are simply stored so that userspace knows how to load
3639 fnvlist_add_uint64(props,
3640 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), ZFS_KEYFORMAT_RAW);
3641 fnvlist_add_string(props,
3642 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), "prompt");
3643 fnvlist_add_uint64(props,
3644 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 0ULL);
3645 fnvlist_add_uint64(props,
3646 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 0ULL);
3648 VERIFY0(dsl_crypto_params_create_nvlist(DCP_CMD_NONE, props,
3649 crypto_args, &dcp));
3651 fnvlist_free(crypto_args);
3652 fnvlist_free(props);
3655 err = dmu_objset_create(dsname, DMU_OST_OTHER, 0, dcp,
3656 ztest_objset_create_cb, NULL);
3657 dsl_crypto_params_free(dcp, !!err);
3659 rand = ztest_random(100);
3660 if (err || rand < 80)
3663 if (ztest_opts.zo_verbose >= 5)
3664 (void) printf("Setting dataset %s to sync always\n", dsname);
3665 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3666 ZFS_SYNC_ALWAYS, B_FALSE));
3671 ztest_objset_destroy_cb(const char *name, void *arg)
3674 dmu_object_info_t doi;
3678 * Verify that the dataset contains a directory object.
3680 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3681 B_TRUE, FTAG, &os));
3682 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3683 if (error != ENOENT) {
3684 /* We could have crashed in the middle of destroying it */
3686 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3687 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3689 dmu_objset_disown(os, B_TRUE, FTAG);
3692 * Destroy the dataset.
3694 if (strchr(name, '@') != NULL) {
3695 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
3697 error = dsl_destroy_head(name);
3698 /* There could be a hold on this dataset */
3706 ztest_snapshot_create(char *osname, uint64_t id)
3708 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3711 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3713 error = dmu_objset_snapshot_one(osname, snapname);
3714 if (error == ENOSPC) {
3715 ztest_record_enospc(FTAG);
3718 if (error != 0 && error != EEXIST) {
3719 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3726 ztest_snapshot_destroy(char *osname, uint64_t id)
3728 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3731 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3734 error = dsl_destroy_snapshot(snapname, B_FALSE);
3735 if (error != 0 && error != ENOENT)
3736 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3742 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3748 char name[ZFS_MAX_DATASET_NAME_LEN];
3752 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3754 (void) rw_rdlock(&ztest_name_lock);
3756 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3757 ztest_opts.zo_pool, (u_longlong_t)id);
3760 * If this dataset exists from a previous run, process its replay log
3761 * half of the time. If we don't replay it, then dsl_destroy_head()
3762 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3764 if (ztest_random(2) == 0 &&
3765 ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
3766 B_TRUE, FTAG, &os) == 0) {
3767 ztest_zd_init(zdtmp, NULL, os);
3768 zil_replay(os, zdtmp, ztest_replay_vector);
3769 ztest_zd_fini(zdtmp);
3770 txg_wait_synced(dmu_objset_pool(os), 0);
3771 dmu_objset_disown(os, B_TRUE, FTAG);
3775 * There may be an old instance of the dataset we're about to
3776 * create lying around from a previous run. If so, destroy it
3777 * and all of its snapshots.
3779 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3780 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3783 * Verify that the destroyed dataset is no longer in the namespace.
3785 VERIFY3U(ENOENT, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3786 B_TRUE, FTAG, &os));
3789 * Verify that we can create a new dataset.
3791 error = ztest_dataset_create(name);
3793 if (error == ENOSPC) {
3794 ztest_record_enospc(FTAG);
3797 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3800 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, B_TRUE,
3803 ztest_zd_init(zdtmp, NULL, os);
3806 * Open the intent log for it.
3808 zilog = zil_open(os, ztest_get_data);
3811 * Put some objects in there, do a little I/O to them,
3812 * and randomly take a couple of snapshots along the way.
3814 iters = ztest_random(5);
3815 for (i = 0; i < iters; i++) {
3816 ztest_dmu_object_alloc_free(zdtmp, id);
3817 if (ztest_random(iters) == 0)
3818 (void) ztest_snapshot_create(name, i);
3822 * Verify that we cannot create an existing dataset.
3824 VERIFY3U(EEXIST, ==,
3825 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL, NULL));
3828 * Verify that we can hold an objset that is also owned.
3830 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3831 dmu_objset_rele(os2, FTAG);
3834 * Verify that we cannot own an objset that is already owned.
3836 VERIFY3U(EBUSY, ==, ztest_dmu_objset_own(name, DMU_OST_OTHER,
3837 B_FALSE, B_TRUE, FTAG, &os2));
3840 txg_wait_synced(spa_get_dsl(os->os_spa), 0);
3841 dmu_objset_disown(os, B_TRUE, FTAG);
3842 ztest_zd_fini(zdtmp);
3844 (void) rw_unlock(&ztest_name_lock);
3846 umem_free(zdtmp, sizeof (ztest_ds_t));
3850 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3853 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3855 (void) rw_rdlock(&ztest_name_lock);
3856 (void) ztest_snapshot_destroy(zd->zd_name, id);
3857 (void) ztest_snapshot_create(zd->zd_name, id);
3858 (void) rw_unlock(&ztest_name_lock);
3862 * Cleanup non-standard snapshots and clones.
3865 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3874 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3875 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3876 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3877 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3878 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3880 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3881 "%s@s1_%llu", osname, (u_longlong_t)id);
3882 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3883 "%s/c1_%llu", osname, (u_longlong_t)id);
3884 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3885 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3886 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3887 "%s/c2_%llu", osname, (u_longlong_t)id);
3888 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3889 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3891 error = dsl_destroy_head(clone2name);
3892 if (error && error != ENOENT)
3893 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3894 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3895 if (error && error != ENOENT)
3896 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3897 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3898 if (error && error != ENOENT)
3899 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3900 error = dsl_destroy_head(clone1name);
3901 if (error && error != ENOENT)
3902 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3903 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3904 if (error && error != ENOENT)
3905 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3907 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3908 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3909 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3910 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3911 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3915 * Verify dsl_dataset_promote handles EBUSY
3918 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3926 char *osname = zd->zd_name;
3929 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3930 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3931 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3932 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3933 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3935 (void) rw_rdlock(&ztest_name_lock);
3937 ztest_dsl_dataset_cleanup(osname, id);
3939 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3940 "%s@s1_%llu", osname, (u_longlong_t)id);
3941 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3942 "%s/c1_%llu", osname, (u_longlong_t)id);
3943 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3944 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3945 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3946 "%s/c2_%llu", osname, (u_longlong_t)id);
3947 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3948 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3950 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3951 if (error && error != EEXIST) {
3952 if (error == ENOSPC) {
3953 ztest_record_enospc(FTAG);
3956 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3959 error = dmu_objset_clone(clone1name, snap1name);
3961 if (error == ENOSPC) {
3962 ztest_record_enospc(FTAG);
3965 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3968 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3969 if (error && error != EEXIST) {
3970 if (error == ENOSPC) {
3971 ztest_record_enospc(FTAG);
3974 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3977 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3978 if (error && error != EEXIST) {
3979 if (error == ENOSPC) {
3980 ztest_record_enospc(FTAG);
3983 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3986 error = dmu_objset_clone(clone2name, snap3name);
3988 if (error == ENOSPC) {
3989 ztest_record_enospc(FTAG);
3992 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3995 error = ztest_dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, B_TRUE,
3998 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3999 error = dsl_dataset_promote(clone2name, NULL);
4000 if (error == ENOSPC) {
4001 dmu_objset_disown(os, B_TRUE, FTAG);
4002 ztest_record_enospc(FTAG);
4006 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
4008 dmu_objset_disown(os, B_TRUE, FTAG);
4011 ztest_dsl_dataset_cleanup(osname, id);
4013 (void) rw_unlock(&ztest_name_lock);
4015 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
4016 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
4017 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
4018 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
4019 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
4022 #undef OD_ARRAY_SIZE
4023 #define OD_ARRAY_SIZE 4
4026 * Verify that dmu_object_{alloc,free} work as expected.
4029 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4036 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4037 od = umem_alloc(size, UMEM_NOFAIL);
4038 batchsize = OD_ARRAY_SIZE;
4040 for (b = 0; b < batchsize; b++)
4041 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
4045 * Destroy the previous batch of objects, create a new batch,
4046 * and do some I/O on the new objects.
4048 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
4051 while (ztest_random(4 * batchsize) != 0)
4052 ztest_io(zd, od[ztest_random(batchsize)].od_object,
4053 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4055 umem_free(od, size);
4058 #undef OD_ARRAY_SIZE
4059 #define OD_ARRAY_SIZE 2
4062 * Verify that dmu_{read,write} work as expected.
4065 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4070 objset_t *os = zd->zd_os;
4071 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4072 od = umem_alloc(size, UMEM_NOFAIL);
4074 int i, freeit, error;
4076 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4077 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4078 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4079 uint64_t regions = 997;
4080 uint64_t stride = 123456789ULL;
4081 uint64_t width = 40;
4082 int free_percent = 5;
4085 * This test uses two objects, packobj and bigobj, that are always
4086 * updated together (i.e. in the same tx) so that their contents are
4087 * in sync and can be compared. Their contents relate to each other
4088 * in a simple way: packobj is a dense array of 'bufwad' structures,
4089 * while bigobj is a sparse array of the same bufwads. Specifically,
4090 * for any index n, there are three bufwads that should be identical:
4092 * packobj, at offset n * sizeof (bufwad_t)
4093 * bigobj, at the head of the nth chunk
4094 * bigobj, at the tail of the nth chunk
4096 * The chunk size is arbitrary. It doesn't have to be a power of two,
4097 * and it doesn't have any relation to the object blocksize.
4098 * The only requirement is that it can hold at least two bufwads.
4100 * Normally, we write the bufwad to each of these locations.
4101 * However, free_percent of the time we instead write zeroes to
4102 * packobj and perform a dmu_free_range() on bigobj. By comparing
4103 * bigobj to packobj, we can verify that the DMU is correctly
4104 * tracking which parts of an object are allocated and free,
4105 * and that the contents of the allocated blocks are correct.
4109 * Read the directory info. If it's the first time, set things up.
4111 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
4112 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4115 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4116 umem_free(od, size);
4120 bigobj = od[0].od_object;
4121 packobj = od[1].od_object;
4122 chunksize = od[0].od_gen;
4123 ASSERT(chunksize == od[1].od_gen);
4126 * Prefetch a random chunk of the big object.
4127 * Our aim here is to get some async reads in flight
4128 * for blocks that we may free below; the DMU should
4129 * handle this race correctly.
4131 n = ztest_random(regions) * stride + ztest_random(width);
4132 s = 1 + ztest_random(2 * width - 1);
4133 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4134 ZIO_PRIORITY_SYNC_READ);
4137 * Pick a random index and compute the offsets into packobj and bigobj.
4139 n = ztest_random(regions) * stride + ztest_random(width);
4140 s = 1 + ztest_random(width - 1);
4142 packoff = n * sizeof (bufwad_t);
4143 packsize = s * sizeof (bufwad_t);
4145 bigoff = n * chunksize;
4146 bigsize = s * chunksize;
4148 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4149 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4152 * free_percent of the time, free a range of bigobj rather than
4155 freeit = (ztest_random(100) < free_percent);
4158 * Read the current contents of our objects.
4160 error = dmu_read(os, packobj, packoff, packsize, packbuf,
4163 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
4168 * Get a tx for the mods to both packobj and bigobj.
4170 tx = dmu_tx_create(os);
4172 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4175 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4177 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4179 /* This accounts for setting the checksum/compression. */
4180 dmu_tx_hold_bonus(tx, bigobj);
4182 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4184 umem_free(packbuf, packsize);
4185 umem_free(bigbuf, bigsize);
4186 umem_free(od, size);
4190 enum zio_checksum cksum;
4192 cksum = (enum zio_checksum)
4193 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4194 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4195 dmu_object_set_checksum(os, bigobj, cksum, tx);
4197 enum zio_compress comp;
4199 comp = (enum zio_compress)
4200 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4201 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4202 dmu_object_set_compress(os, bigobj, comp, tx);
4205 * For each index from n to n + s, verify that the existing bufwad
4206 * in packobj matches the bufwads at the head and tail of the
4207 * corresponding chunk in bigobj. Then update all three bufwads
4208 * with the new values we want to write out.
4210 for (i = 0; i < s; i++) {
4212 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4214 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4216 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4218 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4219 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4221 if (pack->bw_txg > txg)
4222 fatal(0, "future leak: got %llx, open txg is %llx",
4225 if (pack->bw_data != 0 && pack->bw_index != n + i)
4226 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4227 pack->bw_index, n, i);
4229 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4230 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4232 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4233 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4236 bzero(pack, sizeof (bufwad_t));
4238 pack->bw_index = n + i;
4240 pack->bw_data = 1 + ztest_random(-2ULL);
4247 * We've verified all the old bufwads, and made new ones.
4248 * Now write them out.
4250 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4253 if (ztest_opts.zo_verbose >= 7) {
4254 (void) printf("freeing offset %llx size %llx"
4256 (u_longlong_t)bigoff,
4257 (u_longlong_t)bigsize,
4260 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4262 if (ztest_opts.zo_verbose >= 7) {
4263 (void) printf("writing offset %llx size %llx"
4265 (u_longlong_t)bigoff,
4266 (u_longlong_t)bigsize,
4269 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4275 * Sanity check the stuff we just wrote.
4278 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4279 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4281 VERIFY(0 == dmu_read(os, packobj, packoff,
4282 packsize, packcheck, DMU_READ_PREFETCH));
4283 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4284 bigsize, bigcheck, DMU_READ_PREFETCH));
4286 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4287 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4289 umem_free(packcheck, packsize);
4290 umem_free(bigcheck, bigsize);
4293 umem_free(packbuf, packsize);
4294 umem_free(bigbuf, bigsize);
4295 umem_free(od, size);
4299 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4300 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4308 * For each index from n to n + s, verify that the existing bufwad
4309 * in packobj matches the bufwads at the head and tail of the
4310 * corresponding chunk in bigobj. Then update all three bufwads
4311 * with the new values we want to write out.
4313 for (i = 0; i < s; i++) {
4315 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4317 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4319 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4321 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4322 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4324 if (pack->bw_txg > txg)
4325 fatal(0, "future leak: got %llx, open txg is %llx",
4328 if (pack->bw_data != 0 && pack->bw_index != n + i)
4329 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4330 pack->bw_index, n, i);
4332 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4333 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4335 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4336 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4338 pack->bw_index = n + i;
4340 pack->bw_data = 1 + ztest_random(-2ULL);
4347 #undef OD_ARRAY_SIZE
4348 #define OD_ARRAY_SIZE 2
4351 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4353 objset_t *os = zd->zd_os;
4360 bufwad_t *packbuf, *bigbuf;
4361 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4362 uint64_t blocksize = ztest_random_blocksize();
4363 uint64_t chunksize = blocksize;
4364 uint64_t regions = 997;
4365 uint64_t stride = 123456789ULL;
4367 dmu_buf_t *bonus_db;
4368 arc_buf_t **bigbuf_arcbufs;
4369 dmu_object_info_t doi;
4371 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4372 od = umem_alloc(size, UMEM_NOFAIL);
4375 * This test uses two objects, packobj and bigobj, that are always
4376 * updated together (i.e. in the same tx) so that their contents are
4377 * in sync and can be compared. Their contents relate to each other
4378 * in a simple way: packobj is a dense array of 'bufwad' structures,
4379 * while bigobj is a sparse array of the same bufwads. Specifically,
4380 * for any index n, there are three bufwads that should be identical:
4382 * packobj, at offset n * sizeof (bufwad_t)
4383 * bigobj, at the head of the nth chunk
4384 * bigobj, at the tail of the nth chunk
4386 * The chunk size is set equal to bigobj block size so that
4387 * dmu_assign_arcbuf_by_dbuf() can be tested for object updates.
4391 * Read the directory info. If it's the first time, set things up.
4393 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4394 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4398 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4399 umem_free(od, size);
4403 bigobj = od[0].od_object;
4404 packobj = od[1].od_object;
4405 blocksize = od[0].od_blocksize;
4406 chunksize = blocksize;
4407 ASSERT(chunksize == od[1].od_gen);
4409 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4410 VERIFY(ISP2(doi.doi_data_block_size));
4411 VERIFY(chunksize == doi.doi_data_block_size);
4412 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4415 * Pick a random index and compute the offsets into packobj and bigobj.
4417 n = ztest_random(regions) * stride + ztest_random(width);
4418 s = 1 + ztest_random(width - 1);
4420 packoff = n * sizeof (bufwad_t);
4421 packsize = s * sizeof (bufwad_t);
4423 bigoff = n * chunksize;
4424 bigsize = s * chunksize;
4426 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4427 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4429 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4431 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4434 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4435 * Iteration 1 test zcopy to already referenced dbufs.
4436 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4437 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4438 * Iteration 4 test zcopy when dbuf is no longer dirty.
4439 * Iteration 5 test zcopy when it can't be done.
4440 * Iteration 6 one more zcopy write.
4442 for (i = 0; i < 7; i++) {
4447 * In iteration 5 (i == 5) use arcbufs
4448 * that don't match bigobj blksz to test
4449 * dmu_assign_arcbuf_by_dbuf() when it can't directly
4450 * assign an arcbuf to a dbuf.
4452 for (j = 0; j < s; j++) {
4453 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4455 dmu_request_arcbuf(bonus_db, chunksize);
4457 bigbuf_arcbufs[2 * j] =
4458 dmu_request_arcbuf(bonus_db, chunksize / 2);
4459 bigbuf_arcbufs[2 * j + 1] =
4460 dmu_request_arcbuf(bonus_db, chunksize / 2);
4465 * Get a tx for the mods to both packobj and bigobj.
4467 tx = dmu_tx_create(os);
4469 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4470 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4472 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4474 umem_free(packbuf, packsize);
4475 umem_free(bigbuf, bigsize);
4476 for (j = 0; j < s; j++) {
4478 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4479 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4482 bigbuf_arcbufs[2 * j]);
4484 bigbuf_arcbufs[2 * j + 1]);
4487 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4488 umem_free(od, size);
4489 dmu_buf_rele(bonus_db, FTAG);
4494 * 50% of the time don't read objects in the 1st iteration to
4495 * test dmu_assign_arcbuf_by_dbuf() for the case when there are
4496 * no existing dbufs for the specified offsets.
4498 if (i != 0 || ztest_random(2) != 0) {
4499 error = dmu_read(os, packobj, packoff,
4500 packsize, packbuf, DMU_READ_PREFETCH);
4502 error = dmu_read(os, bigobj, bigoff, bigsize,
4503 bigbuf, DMU_READ_PREFETCH);
4506 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4510 * We've verified all the old bufwads, and made new ones.
4511 * Now write them out.
4513 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4514 if (ztest_opts.zo_verbose >= 7) {
4515 (void) printf("writing offset %llx size %llx"
4517 (u_longlong_t)bigoff,
4518 (u_longlong_t)bigsize,
4521 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4523 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4524 bcopy((caddr_t)bigbuf + (off - bigoff),
4525 bigbuf_arcbufs[j]->b_data, chunksize);
4527 bcopy((caddr_t)bigbuf + (off - bigoff),
4528 bigbuf_arcbufs[2 * j]->b_data,
4530 bcopy((caddr_t)bigbuf + (off - bigoff) +
4532 bigbuf_arcbufs[2 * j + 1]->b_data,
4537 VERIFY(dmu_buf_hold(os, bigobj, off,
4538 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4540 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4541 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4542 bigbuf_arcbufs[j], tx);
4544 dmu_assign_arcbuf_by_dbuf(bonus_db, off,
4545 bigbuf_arcbufs[2 * j], tx);
4546 dmu_assign_arcbuf_by_dbuf(bonus_db,
4547 off + chunksize / 2,
4548 bigbuf_arcbufs[2 * j + 1], tx);
4551 dmu_buf_rele(dbt, FTAG);
4557 * Sanity check the stuff we just wrote.
4560 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4561 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4563 VERIFY(0 == dmu_read(os, packobj, packoff,
4564 packsize, packcheck, DMU_READ_PREFETCH));
4565 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4566 bigsize, bigcheck, DMU_READ_PREFETCH));
4568 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4569 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4571 umem_free(packcheck, packsize);
4572 umem_free(bigcheck, bigsize);
4575 txg_wait_open(dmu_objset_pool(os), 0);
4576 } else if (i == 3) {
4577 txg_wait_synced(dmu_objset_pool(os), 0);
4581 dmu_buf_rele(bonus_db, FTAG);
4582 umem_free(packbuf, packsize);
4583 umem_free(bigbuf, bigsize);
4584 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4585 umem_free(od, size);
4590 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4594 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4595 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4596 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4599 * Have multiple threads write to large offsets in an object
4600 * to verify that parallel writes to an object -- even to the
4601 * same blocks within the object -- doesn't cause any trouble.
4603 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4605 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4608 while (ztest_random(10) != 0)
4609 ztest_io(zd, od->od_object, offset);
4611 umem_free(od, sizeof (ztest_od_t));
4615 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4618 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4619 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4620 uint64_t count = ztest_random(20) + 1;
4621 uint64_t blocksize = ztest_random_blocksize();
4624 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4626 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4628 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4629 !ztest_random(2)) != 0) {
4630 umem_free(od, sizeof (ztest_od_t));
4634 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4635 umem_free(od, sizeof (ztest_od_t));
4639 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4641 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4643 while (ztest_random(count) != 0) {
4644 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4645 if (ztest_write(zd, od->od_object, randoff, blocksize,
4648 while (ztest_random(4) != 0)
4649 ztest_io(zd, od->od_object, randoff);
4652 umem_free(data, blocksize);
4653 umem_free(od, sizeof (ztest_od_t));
4657 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4659 #define ZTEST_ZAP_MIN_INTS 1
4660 #define ZTEST_ZAP_MAX_INTS 4
4661 #define ZTEST_ZAP_MAX_PROPS 1000
4664 ztest_zap(ztest_ds_t *zd, uint64_t id)
4666 objset_t *os = zd->zd_os;
4669 uint64_t txg, last_txg;
4670 uint64_t value[ZTEST_ZAP_MAX_INTS];
4671 uint64_t zl_ints, zl_intsize, prop;
4674 char propname[100], txgname[100];
4676 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4678 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4679 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4681 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4682 !ztest_random(2)) != 0)
4685 object = od->od_object;
4688 * Generate a known hash collision, and verify that
4689 * we can lookup and remove both entries.
4691 tx = dmu_tx_create(os);
4692 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4693 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4696 for (i = 0; i < 2; i++) {
4698 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4701 for (i = 0; i < 2; i++) {
4702 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4703 sizeof (uint64_t), 1, &value[i], tx));
4705 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4706 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4707 ASSERT3U(zl_ints, ==, 1);
4709 for (i = 0; i < 2; i++) {
4710 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4715 * Generate a buch of random entries.
4717 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4719 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4720 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4721 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4722 bzero(value, sizeof (value));
4726 * If these zap entries already exist, validate their contents.
4728 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4730 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4731 ASSERT3U(zl_ints, ==, 1);
4733 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4734 zl_ints, &last_txg) == 0);
4736 VERIFY(zap_length(os, object, propname, &zl_intsize,
4739 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4740 ASSERT3U(zl_ints, ==, ints);
4742 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4743 zl_ints, value) == 0);
4745 for (i = 0; i < ints; i++) {
4746 ASSERT3U(value[i], ==, last_txg + object + i);
4749 ASSERT3U(error, ==, ENOENT);
4753 * Atomically update two entries in our zap object.
4754 * The first is named txg_%llu, and contains the txg
4755 * in which the property was last updated. The second
4756 * is named prop_%llu, and the nth element of its value
4757 * should be txg + object + n.
4759 tx = dmu_tx_create(os);
4760 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4761 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4766 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4768 for (i = 0; i < ints; i++)
4769 value[i] = txg + object + i;
4771 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4773 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4779 * Remove a random pair of entries.
4781 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4782 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4783 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4785 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4787 if (error == ENOENT)
4792 tx = dmu_tx_create(os);
4793 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4794 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4797 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4798 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4801 umem_free(od, sizeof (ztest_od_t));
4805 * Testcase to test the upgrading of a microzap to fatzap.
4808 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4810 objset_t *os = zd->zd_os;
4812 uint64_t object, txg;
4815 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4816 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4818 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4819 !ztest_random(2)) != 0)
4821 object = od->od_object;
4824 * Add entries to this ZAP and make sure it spills over
4825 * and gets upgraded to a fatzap. Also, since we are adding
4826 * 2050 entries we should see ptrtbl growth and leaf-block split.
4828 for (i = 0; i < 2050; i++) {
4829 char name[ZFS_MAX_DATASET_NAME_LEN];
4834 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4835 (u_longlong_t)id, (u_longlong_t)value);
4837 tx = dmu_tx_create(os);
4838 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4839 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4842 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4844 ASSERT(error == 0 || error == EEXIST);
4848 umem_free(od, sizeof (ztest_od_t));
4853 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4855 objset_t *os = zd->zd_os;
4857 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4859 int i, namelen, error;
4860 int micro = ztest_random(2);
4861 char name[20], string_value[20];
4864 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4865 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4867 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4868 umem_free(od, sizeof (ztest_od_t));
4872 object = od->od_object;
4875 * Generate a random name of the form 'xxx.....' where each
4876 * x is a random printable character and the dots are dots.
4877 * There are 94 such characters, and the name length goes from
4878 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4880 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4882 for (i = 0; i < 3; i++)
4883 name[i] = '!' + ztest_random('~' - '!' + 1);
4884 for (; i < namelen - 1; i++)
4888 if ((namelen & 1) || micro) {
4889 wsize = sizeof (txg);
4895 data = string_value;
4899 VERIFY0(zap_count(os, object, &count));
4900 ASSERT(count != -1ULL);
4903 * Select an operation: length, lookup, add, update, remove.
4905 i = ztest_random(5);
4908 tx = dmu_tx_create(os);
4909 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4910 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4912 umem_free(od, sizeof (ztest_od_t));
4915 bcopy(name, string_value, namelen);
4919 bzero(string_value, namelen);
4925 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4927 ASSERT3U(wsize, ==, zl_wsize);
4928 ASSERT3U(wc, ==, zl_wc);
4930 ASSERT3U(error, ==, ENOENT);
4935 error = zap_lookup(os, object, name, wsize, wc, data);
4937 if (data == string_value &&
4938 bcmp(name, data, namelen) != 0)
4939 fatal(0, "name '%s' != val '%s' len %d",
4940 name, data, namelen);
4942 ASSERT3U(error, ==, ENOENT);
4947 error = zap_add(os, object, name, wsize, wc, data, tx);
4948 ASSERT(error == 0 || error == EEXIST);
4952 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4956 error = zap_remove(os, object, name, tx);
4957 ASSERT(error == 0 || error == ENOENT);
4964 umem_free(od, sizeof (ztest_od_t));
4968 * Commit callback data.
4970 typedef struct ztest_cb_data {
4971 list_node_t zcd_node;
4973 int zcd_expected_err;
4974 boolean_t zcd_added;
4975 boolean_t zcd_called;
4979 /* This is the actual commit callback function */
4981 ztest_commit_callback(void *arg, int error)
4983 ztest_cb_data_t *data = arg;
4984 uint64_t synced_txg;
4986 VERIFY(data != NULL);
4987 VERIFY3S(data->zcd_expected_err, ==, error);
4988 VERIFY(!data->zcd_called);
4990 synced_txg = spa_last_synced_txg(data->zcd_spa);
4991 if (data->zcd_txg > synced_txg)
4992 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4993 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4996 data->zcd_called = B_TRUE;
4998 if (error == ECANCELED) {
4999 ASSERT0(data->zcd_txg);
5000 ASSERT(!data->zcd_added);
5003 * The private callback data should be destroyed here, but
5004 * since we are going to check the zcd_called field after
5005 * dmu_tx_abort(), we will destroy it there.
5010 ASSERT(data->zcd_added);
5011 ASSERT3U(data->zcd_txg, !=, 0);
5013 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5015 /* See if this cb was called more quickly */
5016 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
5017 zc_min_txg_delay = synced_txg - data->zcd_txg;
5019 /* Remove our callback from the list */
5020 list_remove(&zcl.zcl_callbacks, data);
5022 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5024 umem_free(data, sizeof (ztest_cb_data_t));
5027 /* Allocate and initialize callback data structure */
5028 static ztest_cb_data_t *
5029 ztest_create_cb_data(objset_t *os, uint64_t txg)
5031 ztest_cb_data_t *cb_data;
5033 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5035 cb_data->zcd_txg = txg;
5036 cb_data->zcd_spa = dmu_objset_spa(os);
5037 list_link_init(&cb_data->zcd_node);
5043 * Commit callback test.
5046 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5048 objset_t *os = zd->zd_os;
5051 ztest_cb_data_t *cb_data[3], *tmp_cb;
5052 uint64_t old_txg, txg;
5055 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5056 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5058 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5059 umem_free(od, sizeof (ztest_od_t));
5063 tx = dmu_tx_create(os);
5065 cb_data[0] = ztest_create_cb_data(os, 0);
5066 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5068 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
5070 /* Every once in a while, abort the transaction on purpose */
5071 if (ztest_random(100) == 0)
5075 error = dmu_tx_assign(tx, TXG_NOWAIT);
5077 txg = error ? 0 : dmu_tx_get_txg(tx);
5079 cb_data[0]->zcd_txg = txg;
5080 cb_data[1] = ztest_create_cb_data(os, txg);
5081 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5085 * It's not a strict requirement to call the registered
5086 * callbacks from inside dmu_tx_abort(), but that's what
5087 * it's supposed to happen in the current implementation
5088 * so we will check for that.
5090 for (i = 0; i < 2; i++) {
5091 cb_data[i]->zcd_expected_err = ECANCELED;
5092 VERIFY(!cb_data[i]->zcd_called);
5097 for (i = 0; i < 2; i++) {
5098 VERIFY(cb_data[i]->zcd_called);
5099 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5102 umem_free(od, sizeof (ztest_od_t));
5106 cb_data[2] = ztest_create_cb_data(os, txg);
5107 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5110 * Read existing data to make sure there isn't a future leak.
5112 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
5113 &old_txg, DMU_READ_PREFETCH));
5116 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5119 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
5121 (void) mutex_enter(&zcl.zcl_callbacks_lock);
5124 * Since commit callbacks don't have any ordering requirement and since
5125 * it is theoretically possible for a commit callback to be called
5126 * after an arbitrary amount of time has elapsed since its txg has been
5127 * synced, it is difficult to reliably determine whether a commit
5128 * callback hasn't been called due to high load or due to a flawed
5131 * In practice, we will assume that if after a certain number of txgs a
5132 * commit callback hasn't been called, then most likely there's an
5133 * implementation bug..
5135 tmp_cb = list_head(&zcl.zcl_callbacks);
5136 if (tmp_cb != NULL &&
5137 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
5138 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5139 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5143 * Let's find the place to insert our callbacks.
5145 * Even though the list is ordered by txg, it is possible for the
5146 * insertion point to not be the end because our txg may already be
5147 * quiescing at this point and other callbacks in the open txg
5148 * (from other objsets) may have sneaked in.
5150 tmp_cb = list_tail(&zcl.zcl_callbacks);
5151 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5152 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5154 /* Add the 3 callbacks to the list */
5155 for (i = 0; i < 3; i++) {
5157 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5159 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5162 cb_data[i]->zcd_added = B_TRUE;
5163 VERIFY(!cb_data[i]->zcd_called);
5165 tmp_cb = cb_data[i];
5170 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5174 umem_free(od, sizeof (ztest_od_t));
5178 * Visit each object in the dataset. Verify that its properties
5179 * are consistent what was stored in the block tag when it was created,
5180 * and that its unused bonus buffer space has not been overwritten.
5184 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5186 objset_t *os = zd->zd_os;
5190 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5191 ztest_block_tag_t *bt = NULL;
5192 dmu_object_info_t doi;
5195 ztest_object_lock(zd, obj, RL_READER);
5196 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0) {
5197 ztest_object_unlock(zd, obj);
5201 dmu_object_info_from_db(db, &doi);
5202 if (doi.doi_bonus_size >= sizeof (*bt))
5203 bt = ztest_bt_bonus(db);
5205 if (bt && bt->bt_magic == BT_MAGIC) {
5206 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5207 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5209 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5212 dmu_buf_rele(db, FTAG);
5213 ztest_object_unlock(zd, obj);
5219 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5221 zfs_prop_t proplist[] = {
5223 ZFS_PROP_COMPRESSION,
5229 (void) rw_rdlock(&ztest_name_lock);
5231 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5232 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5233 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5235 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5236 ztest_random_blocksize(), (int)ztest_random(2)));
5238 (void) rw_unlock(&ztest_name_lock);
5243 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5245 nvlist_t *props = NULL;
5247 (void) rw_rdlock(&ztest_name_lock);
5249 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5250 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5252 VERIFY0(spa_prop_get(ztest_spa, &props));
5254 if (ztest_opts.zo_verbose >= 6)
5255 dump_nvlist(props, 4);
5259 (void) rw_unlock(&ztest_name_lock);
5263 user_release_one(const char *snapname, const char *holdname)
5265 nvlist_t *snaps, *holds;
5268 snaps = fnvlist_alloc();
5269 holds = fnvlist_alloc();
5270 fnvlist_add_boolean(holds, holdname);
5271 fnvlist_add_nvlist(snaps, snapname, holds);
5272 fnvlist_free(holds);
5273 error = dsl_dataset_user_release(snaps, NULL);
5274 fnvlist_free(snaps);
5279 * Test snapshot hold/release and deferred destroy.
5282 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5285 objset_t *os = zd->zd_os;
5289 char clonename[100];
5291 char osname[ZFS_MAX_DATASET_NAME_LEN];
5294 (void) rw_rdlock(&ztest_name_lock);
5296 dmu_objset_name(os, osname);
5298 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5300 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5301 (void) snprintf(clonename, sizeof (clonename),
5302 "%s/ch1_%llu", osname, (u_longlong_t)id);
5303 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5306 * Clean up from any previous run.
5308 error = dsl_destroy_head(clonename);
5309 if (error != ENOENT)
5311 error = user_release_one(fullname, tag);
5312 if (error != ESRCH && error != ENOENT)
5314 error = dsl_destroy_snapshot(fullname, B_FALSE);
5315 if (error != ENOENT)
5319 * Create snapshot, clone it, mark snap for deferred destroy,
5320 * destroy clone, verify snap was also destroyed.
5322 error = dmu_objset_snapshot_one(osname, snapname);
5324 if (error == ENOSPC) {
5325 ztest_record_enospc("dmu_objset_snapshot");
5328 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5331 error = dmu_objset_clone(clonename, fullname);
5333 if (error == ENOSPC) {
5334 ztest_record_enospc("dmu_objset_clone");
5337 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5340 error = dsl_destroy_snapshot(fullname, B_TRUE);
5342 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5346 error = dsl_destroy_head(clonename);
5348 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5350 error = dmu_objset_hold(fullname, FTAG, &origin);
5351 if (error != ENOENT)
5352 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5355 * Create snapshot, add temporary hold, verify that we can't
5356 * destroy a held snapshot, mark for deferred destroy,
5357 * release hold, verify snapshot was destroyed.
5359 error = dmu_objset_snapshot_one(osname, snapname);
5361 if (error == ENOSPC) {
5362 ztest_record_enospc("dmu_objset_snapshot");
5365 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5368 holds = fnvlist_alloc();
5369 fnvlist_add_string(holds, fullname, tag);
5370 error = dsl_dataset_user_hold(holds, 0, NULL);
5371 fnvlist_free(holds);
5373 if (error == ENOSPC) {
5374 ztest_record_enospc("dsl_dataset_user_hold");
5377 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5378 fullname, tag, error);
5381 error = dsl_destroy_snapshot(fullname, B_FALSE);
5382 if (error != EBUSY) {
5383 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5387 error = dsl_destroy_snapshot(fullname, B_TRUE);
5389 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5393 error = user_release_one(fullname, tag);
5395 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5397 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5400 (void) rw_unlock(&ztest_name_lock);
5404 * Inject random faults into the on-disk data.
5408 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5410 ztest_shared_t *zs = ztest_shared;
5411 spa_t *spa = ztest_spa;
5415 uint64_t bad = 0x1990c0ffeedecadeull;
5420 int bshift = SPA_MAXBLOCKSHIFT + 2;
5426 boolean_t islog = B_FALSE;
5428 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5429 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5431 mutex_enter(&ztest_vdev_lock);
5432 maxfaults = MAXFAULTS();
5433 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5434 mirror_save = zs->zs_mirrors;
5435 mutex_exit(&ztest_vdev_lock);
5437 ASSERT(leaves >= 1);
5440 * Grab the name lock as reader. There are some operations
5441 * which don't like to have their vdevs changed while
5442 * they are in progress (i.e. spa_change_guid). Those
5443 * operations will have grabbed the name lock as writer.
5445 (void) rw_rdlock(&ztest_name_lock);
5448 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5450 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5452 if (ztest_random(2) == 0) {
5454 * Inject errors on a normal data device or slog device.
5456 top = ztest_random_vdev_top(spa, B_TRUE);
5457 leaf = ztest_random(leaves) + zs->zs_splits;
5460 * Generate paths to the first leaf in this top-level vdev,
5461 * and to the random leaf we selected. We'll induce transient
5462 * write failures and random online/offline activity on leaf 0,
5463 * and we'll write random garbage to the randomly chosen leaf.
5465 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5466 ztest_opts.zo_dir, ztest_opts.zo_pool,
5467 top * leaves + zs->zs_splits);
5468 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5469 ztest_opts.zo_dir, ztest_opts.zo_pool,
5470 top * leaves + leaf);
5472 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5473 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5477 * If the top-level vdev needs to be resilvered
5478 * then we only allow faults on the device that is
5481 if (vd0 != NULL && maxfaults != 1 &&
5482 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5483 vd0->vdev_resilver_txg != 0)) {
5485 * Make vd0 explicitly claim to be unreadable,
5486 * or unwriteable, or reach behind its back
5487 * and close the underlying fd. We can do this if
5488 * maxfaults == 0 because we'll fail and reexecute,
5489 * and we can do it if maxfaults >= 2 because we'll
5490 * have enough redundancy. If maxfaults == 1, the
5491 * combination of this with injection of random data
5492 * corruption below exceeds the pool's fault tolerance.
5494 vdev_file_t *vf = vd0->vdev_tsd;
5496 if (vf != NULL && ztest_random(3) == 0) {
5497 (void) close(vf->vf_vnode->v_fd);
5498 vf->vf_vnode->v_fd = -1;
5499 } else if (ztest_random(2) == 0) {
5500 vd0->vdev_cant_read = B_TRUE;
5502 vd0->vdev_cant_write = B_TRUE;
5504 guid0 = vd0->vdev_guid;
5508 * Inject errors on an l2cache device.
5510 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5512 if (sav->sav_count == 0) {
5513 spa_config_exit(spa, SCL_STATE, FTAG);
5514 (void) rw_unlock(&ztest_name_lock);
5517 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5518 guid0 = vd0->vdev_guid;
5519 (void) strcpy(path0, vd0->vdev_path);
5520 (void) strcpy(pathrand, vd0->vdev_path);
5524 maxfaults = INT_MAX; /* no limit on cache devices */
5527 spa_config_exit(spa, SCL_STATE, FTAG);
5528 (void) rw_unlock(&ztest_name_lock);
5531 * If we can tolerate two or more faults, or we're dealing
5532 * with a slog, randomly online/offline vd0.
5534 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5535 if (ztest_random(10) < 6) {
5536 int flags = (ztest_random(2) == 0 ?
5537 ZFS_OFFLINE_TEMPORARY : 0);
5540 * We have to grab the zs_name_lock as writer to
5541 * prevent a race between offlining a slog and
5542 * destroying a dataset. Offlining the slog will
5543 * grab a reference on the dataset which may cause
5544 * dsl_destroy_head() to fail with EBUSY thus
5545 * leaving the dataset in an inconsistent state.
5548 (void) rw_wrlock(&ztest_name_lock);
5550 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5553 (void) rw_unlock(&ztest_name_lock);
5556 * Ideally we would like to be able to randomly
5557 * call vdev_[on|off]line without holding locks
5558 * to force unpredictable failures but the side
5559 * effects of vdev_[on|off]line prevent us from
5560 * doing so. We grab the ztest_vdev_lock here to
5561 * prevent a race between injection testing and
5564 mutex_enter(&ztest_vdev_lock);
5565 (void) vdev_online(spa, guid0, 0, NULL);
5566 mutex_exit(&ztest_vdev_lock);
5574 * We have at least single-fault tolerance, so inject data corruption.
5576 fd = open(pathrand, O_RDWR);
5578 if (fd == -1) /* we hit a gap in the device namespace */
5581 fsize = lseek(fd, 0, SEEK_END);
5583 while (--iters != 0) {
5585 * The offset must be chosen carefully to ensure that
5586 * we do not inject a given logical block with errors
5587 * on two different leaf devices, because ZFS can not
5588 * tolerate that (if maxfaults==1).
5590 * We divide each leaf into chunks of size
5591 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5592 * there is a series of ranges to which we can inject errors.
5593 * Each range can accept errors on only a single leaf vdev.
5594 * The error injection ranges are separated by ranges
5595 * which we will not inject errors on any device (DMZs).
5596 * Each DMZ must be large enough such that a single block
5597 * can not straddle it, so that a single block can not be
5598 * a target in two different injection ranges (on different
5601 * For example, with 3 leaves, each chunk looks like:
5602 * 0 to 32M: injection range for leaf 0
5603 * 32M to 64M: DMZ - no injection allowed
5604 * 64M to 96M: injection range for leaf 1
5605 * 96M to 128M: DMZ - no injection allowed
5606 * 128M to 160M: injection range for leaf 2
5607 * 160M to 192M: DMZ - no injection allowed
5609 offset = ztest_random(fsize / (leaves << bshift)) *
5610 (leaves << bshift) + (leaf << bshift) +
5611 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5614 * Only allow damage to the labels at one end of the vdev.
5616 * If all labels are damaged, the device will be totally
5617 * inaccessible, which will result in loss of data,
5618 * because we also damage (parts of) the other side of
5621 * Additionally, we will always have both an even and an
5622 * odd label, so that we can handle crashes in the
5623 * middle of vdev_config_sync().
5625 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5629 * The two end labels are stored at the "end" of the disk, but
5630 * the end of the disk (vdev_psize) is aligned to
5631 * sizeof (vdev_label_t).
5633 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5634 if ((leaf & 1) == 1 &&
5635 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5638 mutex_enter(&ztest_vdev_lock);
5639 if (mirror_save != zs->zs_mirrors) {
5640 mutex_exit(&ztest_vdev_lock);
5645 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5646 fatal(1, "can't inject bad word at 0x%llx in %s",
5649 mutex_exit(&ztest_vdev_lock);
5651 if (ztest_opts.zo_verbose >= 7)
5652 (void) printf("injected bad word into %s,"
5653 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5658 umem_free(path0, MAXPATHLEN);
5659 umem_free(pathrand, MAXPATHLEN);
5663 * Verify that DDT repair works as expected.
5666 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5668 ztest_shared_t *zs = ztest_shared;
5669 spa_t *spa = ztest_spa;
5670 objset_t *os = zd->zd_os;
5672 uint64_t object, blocksize, txg, pattern, psize;
5673 enum zio_checksum checksum = spa_dedup_checksum(spa);
5678 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5681 blocksize = ztest_random_blocksize();
5682 blocksize = MIN(blocksize, 2048); /* because we write so many */
5684 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5685 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5687 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5688 umem_free(od, sizeof (ztest_od_t));
5693 * Take the name lock as writer to prevent anyone else from changing
5694 * the pool and dataset properies we need to maintain during this test.
5696 (void) rw_wrlock(&ztest_name_lock);
5698 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5700 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5702 (void) rw_unlock(&ztest_name_lock);
5703 umem_free(od, sizeof (ztest_od_t));
5707 dmu_objset_stats_t dds;
5708 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5709 dmu_objset_fast_stat(os, &dds);
5710 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5712 object = od[0].od_object;
5713 blocksize = od[0].od_blocksize;
5714 pattern = zs->zs_guid ^ dds.dds_guid;
5716 ASSERT(object != 0);
5718 tx = dmu_tx_create(os);
5719 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5720 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5722 (void) rw_unlock(&ztest_name_lock);
5723 umem_free(od, sizeof (ztest_od_t));
5728 * Write all the copies of our block.
5730 for (i = 0; i < copies; i++) {
5731 uint64_t offset = i * blocksize;
5732 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5733 DMU_READ_NO_PREFETCH);
5735 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5736 os, (long long)object, (long long) offset, error);
5738 ASSERT(db->db_offset == offset);
5739 ASSERT(db->db_size == blocksize);
5740 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5741 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5742 dmu_buf_will_fill(db, tx);
5743 ztest_pattern_set(db->db_data, db->db_size, pattern);
5744 dmu_buf_rele(db, FTAG);
5748 txg_wait_synced(spa_get_dsl(spa), txg);
5751 * Find out what block we got.
5753 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5754 DMU_READ_NO_PREFETCH));
5755 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5756 dmu_buf_rele(db, FTAG);
5759 * Damage the block. Dedup-ditto will save us when we read it later.
5761 psize = BP_GET_PSIZE(&blk);
5762 abd = abd_alloc_linear(psize, B_TRUE);
5763 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5765 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5766 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5767 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5771 (void) rw_unlock(&ztest_name_lock);
5772 umem_free(od, sizeof (ztest_od_t));
5780 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5782 spa_t *spa = ztest_spa;
5784 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5785 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5786 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5790 * Change the guid for the pool.
5794 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5796 spa_t *spa = ztest_spa;
5797 uint64_t orig, load;
5800 if (ztest_opts.zo_mmp_test)
5803 orig = spa_guid(spa);
5804 load = spa_load_guid(spa);
5806 (void) rw_wrlock(&ztest_name_lock);
5807 error = spa_change_guid(spa);
5808 (void) rw_unlock(&ztest_name_lock);
5813 if (ztest_opts.zo_verbose >= 4) {
5814 (void) printf("Changed guid old %llu -> %llu\n",
5815 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5818 VERIFY3U(orig, !=, spa_guid(spa));
5819 VERIFY3U(load, ==, spa_load_guid(spa));
5823 * Rename the pool to a different name and then rename it back.
5827 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5829 char *oldname, *newname;
5832 if (ztest_opts.zo_mmp_test)
5835 (void) rw_wrlock(&ztest_name_lock);
5837 oldname = ztest_opts.zo_pool;
5838 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5839 (void) strcpy(newname, oldname);
5840 (void) strcat(newname, "_tmp");
5845 VERIFY3U(0, ==, spa_rename(oldname, newname));
5848 * Try to open it under the old name, which shouldn't exist
5850 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5853 * Open it under the new name and make sure it's still the same spa_t.
5855 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5857 ASSERT(spa == ztest_spa);
5858 spa_close(spa, FTAG);
5861 * Rename it back to the original
5863 VERIFY3U(0, ==, spa_rename(newname, oldname));
5866 * Make sure it can still be opened
5868 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5870 ASSERT(spa == ztest_spa);
5871 spa_close(spa, FTAG);
5873 umem_free(newname, strlen(newname) + 1);
5875 (void) rw_unlock(&ztest_name_lock);
5879 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5881 hrtime_t end = gethrtime() + NANOSEC;
5883 while (gethrtime() <= end) {
5884 int run_count = 100;
5886 struct abd *abd_data, *abd_meta;
5891 zio_cksum_t zc_ref_byteswap;
5893 size = ztest_random_blocksize();
5895 buf = umem_alloc(size, UMEM_NOFAIL);
5896 abd_data = abd_alloc(size, B_FALSE);
5897 abd_meta = abd_alloc(size, B_TRUE);
5899 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5900 *ptr = ztest_random(UINT_MAX);
5902 abd_copy_from_buf_off(abd_data, buf, 0, size);
5903 abd_copy_from_buf_off(abd_meta, buf, 0, size);
5905 VERIFY0(fletcher_4_impl_set("scalar"));
5906 fletcher_4_native(buf, size, NULL, &zc_ref);
5907 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
5909 VERIFY0(fletcher_4_impl_set("cycle"));
5910 while (run_count-- > 0) {
5912 zio_cksum_t zc_byteswap;
5914 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5915 fletcher_4_native(buf, size, NULL, &zc);
5917 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5918 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5919 sizeof (zc_byteswap)));
5921 /* Test ABD - data */
5922 abd_fletcher_4_byteswap(abd_data, size, NULL,
5924 abd_fletcher_4_native(abd_data, size, NULL, &zc);
5926 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5927 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5928 sizeof (zc_byteswap)));
5930 /* Test ABD - metadata */
5931 abd_fletcher_4_byteswap(abd_meta, size, NULL,
5933 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
5935 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5936 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5937 sizeof (zc_byteswap)));
5941 umem_free(buf, size);
5948 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5955 zio_cksum_t zc_ref_bswap;
5957 hrtime_t end = gethrtime() + NANOSEC;
5959 while (gethrtime() <= end) {
5960 int run_count = 100;
5962 size = ztest_random_blocksize();
5963 buf = umem_alloc(size, UMEM_NOFAIL);
5965 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5966 *ptr = ztest_random(UINT_MAX);
5968 VERIFY0(fletcher_4_impl_set("scalar"));
5969 fletcher_4_native(buf, size, NULL, &zc_ref);
5970 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5972 VERIFY0(fletcher_4_impl_set("cycle"));
5974 while (run_count-- > 0) {
5976 zio_cksum_t zc_bswap;
5979 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5980 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5982 while (pos < size) {
5983 size_t inc = 64 * ztest_random(size / 67);
5984 /* sometimes add few bytes to test non-simd */
5985 if (ztest_random(100) < 10)
5986 inc += P2ALIGN(ztest_random(64),
5989 if (inc > (size - pos))
5992 fletcher_4_incremental_native(buf + pos, inc,
5994 fletcher_4_incremental_byteswap(buf + pos, inc,
6000 VERIFY3U(pos, ==, size);
6002 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6003 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6006 * verify if incremental on the whole buffer is
6007 * equivalent to non-incremental version
6009 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
6010 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
6012 fletcher_4_incremental_native(buf, size, &zc);
6013 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
6015 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
6016 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
6019 umem_free(buf, size);
6024 ztest_check_path(char *path)
6027 /* return true on success */
6028 return (!stat(path, &s));
6032 ztest_get_zdb_bin(char *bin, int len)
6036 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
6037 * let popen to search through PATH.
6039 if ((zdb_path = getenv("ZDB_PATH"))) {
6040 strlcpy(bin, zdb_path, len); /* In env */
6041 if (!ztest_check_path(bin)) {
6042 ztest_dump_core = 0;
6043 fatal(1, "invalid ZDB_PATH '%s'", bin);
6048 VERIFY(realpath(getexecname(), bin) != NULL);
6049 if (strstr(bin, "/ztest/")) {
6050 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
6051 strcat(bin, "/zdb/zdb");
6052 if (ztest_check_path(bin))
6059 * Verify pool integrity by running zdb.
6062 ztest_run_zdb(char *pool)
6068 const int len = MAXPATHLEN + MAXNAMELEN + 20;
6071 bin = umem_alloc(len, UMEM_NOFAIL);
6072 zdb = umem_alloc(len, UMEM_NOFAIL);
6073 zbuf = umem_alloc(1024, UMEM_NOFAIL);
6075 ztest_get_zdb_bin(bin, len);
6078 "%s -bcc%s%s -G -d -U %s %s",
6080 ztest_opts.zo_verbose >= 3 ? "s" : "",
6081 ztest_opts.zo_verbose >= 4 ? "v" : "",
6085 if (ztest_opts.zo_verbose >= 5)
6086 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
6088 fp = popen(zdb, "r");
6090 while (fgets(zbuf, 1024, fp) != NULL)
6091 if (ztest_opts.zo_verbose >= 3)
6092 (void) printf("%s", zbuf);
6094 status = pclose(fp);
6099 ztest_dump_core = 0;
6100 if (WIFEXITED(status))
6101 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
6103 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
6105 umem_free(bin, len);
6106 umem_free(zdb, len);
6107 umem_free(zbuf, 1024);
6111 ztest_walk_pool_directory(char *header)
6115 if (ztest_opts.zo_verbose >= 6)
6116 (void) printf("%s\n", header);
6118 mutex_enter(&spa_namespace_lock);
6119 while ((spa = spa_next(spa)) != NULL)
6120 if (ztest_opts.zo_verbose >= 6)
6121 (void) printf("\t%s\n", spa_name(spa));
6122 mutex_exit(&spa_namespace_lock);
6126 ztest_spa_import_export(char *oldname, char *newname)
6128 nvlist_t *config, *newconfig;
6133 if (ztest_opts.zo_verbose >= 4) {
6134 (void) printf("import/export: old = %s, new = %s\n",
6139 * Clean up from previous runs.
6141 (void) spa_destroy(newname);
6144 * Get the pool's configuration and guid.
6146 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6149 * Kick off a scrub to tickle scrub/export races.
6151 if (ztest_random(2) == 0)
6152 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6154 pool_guid = spa_guid(spa);
6155 spa_close(spa, FTAG);
6157 ztest_walk_pool_directory("pools before export");
6162 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6164 ztest_walk_pool_directory("pools after export");
6169 newconfig = spa_tryimport(config);
6170 ASSERT(newconfig != NULL);
6171 nvlist_free(newconfig);
6174 * Import it under the new name.
6176 error = spa_import(newname, config, NULL, 0);
6178 dump_nvlist(config, 0);
6179 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6180 oldname, newname, error);
6183 ztest_walk_pool_directory("pools after import");
6186 * Try to import it again -- should fail with EEXIST.
6188 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6191 * Try to import it under a different name -- should fail with EEXIST.
6193 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6196 * Verify that the pool is no longer visible under the old name.
6198 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6201 * Verify that we can open and close the pool using the new name.
6203 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6204 ASSERT(pool_guid == spa_guid(spa));
6205 spa_close(spa, FTAG);
6207 nvlist_free(config);
6211 ztest_resume(spa_t *spa)
6213 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6214 (void) printf("resuming from suspended state\n");
6215 spa_vdev_state_enter(spa, SCL_NONE);
6216 vdev_clear(spa, NULL);
6217 (void) spa_vdev_state_exit(spa, NULL, 0);
6218 (void) zio_resume(spa);
6222 ztest_resume_thread(void *arg)
6226 while (!ztest_exiting) {
6227 if (spa_suspended(spa))
6229 (void) poll(NULL, 0, 100);
6232 * Periodically change the zfs_compressed_arc_enabled setting.
6234 if (ztest_random(10) == 0)
6235 zfs_compressed_arc_enabled = ztest_random(2);
6238 * Periodically change the zfs_abd_scatter_enabled setting.
6240 if (ztest_random(10) == 0)
6241 zfs_abd_scatter_enabled = ztest_random(2);
6248 ztest_deadman_thread(void *arg)
6250 ztest_shared_t *zs = arg;
6251 spa_t *spa = ztest_spa;
6252 hrtime_t delta, overdue, total = 0;
6255 delta = zs->zs_thread_stop - zs->zs_thread_start +
6256 MSEC2NSEC(zfs_deadman_synctime_ms);
6258 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6261 * If the pool is suspended then fail immediately. Otherwise,
6262 * check to see if the pool is making any progress. If
6263 * vdev_deadman() discovers that there hasn't been any recent
6264 * I/Os then it will end up aborting the tests.
6266 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
6267 fatal(0, "aborting test after %llu seconds because "
6268 "pool has transitioned to a suspended state.",
6269 zfs_deadman_synctime_ms / 1000);
6271 vdev_deadman(spa->spa_root_vdev, FTAG);
6274 * If the process doesn't complete within a grace period of
6275 * zfs_deadman_synctime_ms over the expected finish time,
6276 * then it may be hung and is terminated.
6278 overdue = zs->zs_proc_stop + MSEC2NSEC(zfs_deadman_synctime_ms);
6279 total += zfs_deadman_synctime_ms / 1000;
6280 if (gethrtime() > overdue) {
6281 fatal(0, "aborting test after %llu seconds because "
6282 "the process is overdue for termination.", total);
6285 (void) printf("ztest has been running for %lld seconds\n",
6291 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6293 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6294 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6295 hrtime_t functime = gethrtime();
6298 for (i = 0; i < zi->zi_iters; i++)
6299 zi->zi_func(zd, id);
6301 functime = gethrtime() - functime;
6303 atomic_add_64(&zc->zc_count, 1);
6304 atomic_add_64(&zc->zc_time, functime);
6306 if (ztest_opts.zo_verbose >= 4)
6307 (void) printf("%6.2f sec in %s\n",
6308 (double)functime / NANOSEC, zi->zi_funcname);
6312 ztest_thread(void *arg)
6315 uint64_t id = (uintptr_t)arg;
6316 ztest_shared_t *zs = ztest_shared;
6320 ztest_shared_callstate_t *zc;
6322 while ((now = gethrtime()) < zs->zs_thread_stop) {
6324 * See if it's time to force a crash.
6326 if (now > zs->zs_thread_kill)
6330 * If we're getting ENOSPC with some regularity, stop.
6332 if (zs->zs_enospc_count > 10)
6336 * Pick a random function to execute.
6338 rand = ztest_random(ZTEST_FUNCS);
6339 zi = &ztest_info[rand];
6340 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6341 call_next = zc->zc_next;
6343 if (now >= call_next &&
6344 atomic_cas_64(&zc->zc_next, call_next, call_next +
6345 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6346 ztest_execute(rand, zi, id);
6354 ztest_dataset_name(char *dsname, char *pool, int d)
6356 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6360 ztest_dataset_destroy(int d)
6362 char name[ZFS_MAX_DATASET_NAME_LEN];
6365 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6367 if (ztest_opts.zo_verbose >= 3)
6368 (void) printf("Destroying %s to free up space\n", name);
6371 * Cleanup any non-standard clones and snapshots. In general,
6372 * ztest thread t operates on dataset (t % zopt_datasets),
6373 * so there may be more than one thing to clean up.
6375 for (t = d; t < ztest_opts.zo_threads;
6376 t += ztest_opts.zo_datasets)
6377 ztest_dsl_dataset_cleanup(name, t);
6379 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6380 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6384 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6386 uint64_t usedobjs, dirobjs, scratch;
6389 * ZTEST_DIROBJ is the object directory for the entire dataset.
6390 * Therefore, the number of objects in use should equal the
6391 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6392 * If not, we have an object leak.
6394 * Note that we can only check this in ztest_dataset_open(),
6395 * when the open-context and syncing-context values agree.
6396 * That's because zap_count() returns the open-context value,
6397 * while dmu_objset_space() returns the rootbp fill count.
6399 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6400 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6401 ASSERT3U(dirobjs + 1, ==, usedobjs);
6405 ztest_dataset_open(int d)
6407 ztest_ds_t *zd = &ztest_ds[d];
6408 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6411 char name[ZFS_MAX_DATASET_NAME_LEN];
6414 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6416 (void) rw_rdlock(&ztest_name_lock);
6418 error = ztest_dataset_create(name);
6419 if (error == ENOSPC) {
6420 (void) rw_unlock(&ztest_name_lock);
6421 ztest_record_enospc(FTAG);
6424 ASSERT(error == 0 || error == EEXIST);
6426 VERIFY0(ztest_dmu_objset_own(name, DMU_OST_OTHER, B_FALSE,
6428 (void) rw_unlock(&ztest_name_lock);
6430 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6432 zilog = zd->zd_zilog;
6434 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6435 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6436 fatal(0, "missing log records: claimed %llu < committed %llu",
6437 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6439 ztest_dataset_dirobj_verify(zd);
6441 zil_replay(os, zd, ztest_replay_vector);
6443 ztest_dataset_dirobj_verify(zd);
6445 if (ztest_opts.zo_verbose >= 6)
6446 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6448 (u_longlong_t)zilog->zl_parse_blk_count,
6449 (u_longlong_t)zilog->zl_parse_lr_count,
6450 (u_longlong_t)zilog->zl_replaying_seq);
6452 zilog = zil_open(os, ztest_get_data);
6454 if (zilog->zl_replaying_seq != 0 &&
6455 zilog->zl_replaying_seq < committed_seq)
6456 fatal(0, "missing log records: replayed %llu < committed %llu",
6457 zilog->zl_replaying_seq, committed_seq);
6463 ztest_dataset_close(int d)
6465 ztest_ds_t *zd = &ztest_ds[d];
6467 zil_close(zd->zd_zilog);
6468 txg_wait_synced(spa_get_dsl(zd->zd_os->os_spa), 0);
6469 dmu_objset_disown(zd->zd_os, B_TRUE, zd);
6475 * Kick off threads to run tests on all datasets in parallel.
6478 ztest_run(ztest_shared_t *zs)
6482 kthread_t *resume_thread;
6483 kthread_t **run_threads;
6488 ztest_exiting = B_FALSE;
6491 * Initialize parent/child shared state.
6493 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6494 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6496 zs->zs_thread_start = gethrtime();
6497 zs->zs_thread_stop =
6498 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6499 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6500 zs->zs_thread_kill = zs->zs_thread_stop;
6501 if (ztest_random(100) < ztest_opts.zo_killrate) {
6502 zs->zs_thread_kill -=
6503 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6506 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6508 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6509 offsetof(ztest_cb_data_t, zcd_node));
6514 kernel_init(FREAD | FWRITE);
6515 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6516 spa->spa_debug = B_TRUE;
6517 metaslab_preload_limit = ztest_random(20) + 1;
6520 dmu_objset_stats_t dds;
6521 VERIFY0(ztest_dmu_objset_own(ztest_opts.zo_pool,
6522 DMU_OST_ANY, B_TRUE, B_TRUE, FTAG, &os));
6523 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6524 dmu_objset_fast_stat(os, &dds);
6525 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6526 zs->zs_guid = dds.dds_guid;
6527 dmu_objset_disown(os, B_TRUE, FTAG);
6529 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6532 * We don't expect the pool to suspend unless maxfaults == 0,
6533 * in which case ztest_fault_inject() temporarily takes away
6534 * the only valid replica.
6536 if (MAXFAULTS() == 0)
6537 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6539 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6542 * Create a thread to periodically resume suspended I/O.
6544 resume_thread = thread_create(NULL, 0, ztest_resume_thread,
6545 spa, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6548 * Create a deadman thread and set to panic if we hang.
6550 (void) thread_create(NULL, 0, ztest_deadman_thread,
6551 zs, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6553 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC;
6556 * Verify that we can safely inquire about about any object,
6557 * whether it's allocated or not. To make it interesting,
6558 * we probe a 5-wide window around each power of two.
6559 * This hits all edge cases, including zero and the max.
6561 for (t = 0; t < 64; t++) {
6562 for (d = -5; d <= 5; d++) {
6563 error = dmu_object_info(spa->spa_meta_objset,
6564 (1ULL << t) + d, NULL);
6565 ASSERT(error == 0 || error == ENOENT ||
6571 * If we got any ENOSPC errors on the previous run, destroy something.
6573 if (zs->zs_enospc_count != 0) {
6574 int d = ztest_random(ztest_opts.zo_datasets);
6575 ztest_dataset_destroy(d);
6577 zs->zs_enospc_count = 0;
6579 run_threads = umem_zalloc(ztest_opts.zo_threads * sizeof (kthread_t *),
6582 if (ztest_opts.zo_verbose >= 4)
6583 (void) printf("starting main threads...\n");
6586 * Kick off all the tests that run in parallel.
6588 for (t = 0; t < ztest_opts.zo_threads; t++) {
6589 if (t < ztest_opts.zo_datasets && ztest_dataset_open(t) != 0) {
6590 umem_free(run_threads, ztest_opts.zo_threads *
6591 sizeof (kthread_t *));
6595 run_threads[t] = thread_create(NULL, 0, ztest_thread,
6596 (void *)(uintptr_t)t, 0, NULL, TS_RUN | TS_JOINABLE,
6601 * Wait for all of the tests to complete. We go in reverse order
6602 * so we don't close datasets while threads are still using them.
6604 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6605 VERIFY0(thread_join(run_threads[t]));
6606 if (t < ztest_opts.zo_datasets)
6607 ztest_dataset_close(t);
6610 txg_wait_synced(spa_get_dsl(spa), 0);
6612 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6613 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6615 umem_free(run_threads, ztest_opts.zo_threads * sizeof (kthread_t *));
6617 /* Kill the resume thread */
6618 ztest_exiting = B_TRUE;
6619 VERIFY0(thread_join(resume_thread));
6623 * Right before closing the pool, kick off a bunch of async I/O;
6624 * spa_close() should wait for it to complete.
6626 for (object = 1; object < 50; object++) {
6627 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6628 ZIO_PRIORITY_SYNC_READ);
6631 /* Verify that at least one commit cb was called in a timely fashion */
6632 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6633 VERIFY0(zc_min_txg_delay);
6635 spa_close(spa, FTAG);
6638 * Verify that we can loop over all pools.
6640 mutex_enter(&spa_namespace_lock);
6641 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6642 if (ztest_opts.zo_verbose > 3)
6643 (void) printf("spa_next: found %s\n", spa_name(spa));
6644 mutex_exit(&spa_namespace_lock);
6647 * Verify that we can export the pool and reimport it under a
6650 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6651 char name[ZFS_MAX_DATASET_NAME_LEN];
6652 (void) snprintf(name, sizeof (name), "%s_import",
6653 ztest_opts.zo_pool);
6654 ztest_spa_import_export(ztest_opts.zo_pool, name);
6655 ztest_spa_import_export(name, ztest_opts.zo_pool);
6660 list_destroy(&zcl.zcl_callbacks);
6661 mutex_destroy(&zcl.zcl_callbacks_lock);
6662 (void) rwlock_destroy(&ztest_name_lock);
6663 mutex_destroy(&ztest_vdev_lock);
6669 ztest_ds_t *zd = &ztest_ds[0];
6673 if (ztest_opts.zo_verbose >= 3)
6674 (void) printf("testing spa_freeze()...\n");
6676 kernel_init(FREAD | FWRITE);
6677 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6678 VERIFY3U(0, ==, ztest_dataset_open(0));
6679 spa->spa_debug = B_TRUE;
6683 * Force the first log block to be transactionally allocated.
6684 * We have to do this before we freeze the pool -- otherwise
6685 * the log chain won't be anchored.
6687 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6688 ztest_dmu_object_alloc_free(zd, 0);
6689 zil_commit(zd->zd_zilog, 0);
6692 txg_wait_synced(spa_get_dsl(spa), 0);
6695 * Freeze the pool. This stops spa_sync() from doing anything,
6696 * so that the only way to record changes from now on is the ZIL.
6701 * Because it is hard to predict how much space a write will actually
6702 * require beforehand, we leave ourselves some fudge space to write over
6705 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6708 * Run tests that generate log records but don't alter the pool config
6709 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6710 * We do a txg_wait_synced() after each iteration to force the txg
6711 * to increase well beyond the last synced value in the uberblock.
6712 * The ZIL should be OK with that.
6714 * Run a random number of times less than zo_maxloops and ensure we do
6715 * not run out of space on the pool.
6717 while (ztest_random(10) != 0 &&
6718 numloops++ < ztest_opts.zo_maxloops &&
6719 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6721 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6722 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6723 ztest_io(zd, od.od_object,
6724 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6725 txg_wait_synced(spa_get_dsl(spa), 0);
6729 * Commit all of the changes we just generated.
6731 zil_commit(zd->zd_zilog, 0);
6732 txg_wait_synced(spa_get_dsl(spa), 0);
6735 * Close our dataset and close the pool.
6737 ztest_dataset_close(0);
6738 spa_close(spa, FTAG);
6742 * Open and close the pool and dataset to induce log replay.
6744 kernel_init(FREAD | FWRITE);
6745 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6746 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6747 VERIFY3U(0, ==, ztest_dataset_open(0));
6748 spa->spa_debug = B_TRUE;
6750 txg_wait_synced(spa_get_dsl(spa), 0);
6751 ztest_dataset_close(0);
6752 ztest_reguid(NULL, 0);
6754 spa_close(spa, FTAG);
6759 print_time(hrtime_t t, char *timebuf)
6761 hrtime_t s = t / NANOSEC;
6762 hrtime_t m = s / 60;
6763 hrtime_t h = m / 60;
6764 hrtime_t d = h / 24;
6773 (void) sprintf(timebuf,
6774 "%llud%02lluh%02llum%02llus", d, h, m, s);
6776 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6778 (void) sprintf(timebuf, "%llum%02llus", m, s);
6780 (void) sprintf(timebuf, "%llus", s);
6784 make_random_props(void)
6788 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6789 if (ztest_random(2) == 0)
6791 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6797 * Import a storage pool with the given name.
6800 ztest_import(ztest_shared_t *zs)
6802 libzfs_handle_t *hdl;
6803 importargs_t args = { 0 };
6805 nvlist_t *cfg = NULL;
6807 char *searchdirs[nsearch];
6808 char *name = ztest_opts.zo_pool;
6809 int flags = ZFS_IMPORT_MISSING_LOG;
6812 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6813 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6815 kernel_init(FREAD | FWRITE);
6816 hdl = libzfs_init();
6818 searchdirs[0] = ztest_opts.zo_dir;
6819 args.paths = nsearch;
6820 args.path = searchdirs;
6821 args.can_be_active = B_FALSE;
6823 error = zpool_tryimport(hdl, name, &cfg, &args);
6825 (void) fatal(0, "No pools found\n");
6827 VERIFY0(spa_import(name, cfg, NULL, flags));
6828 VERIFY0(spa_open(name, &spa, FTAG));
6829 zs->zs_metaslab_sz =
6830 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6831 spa_close(spa, FTAG);
6836 if (!ztest_opts.zo_mmp_test) {
6837 ztest_run_zdb(ztest_opts.zo_pool);
6839 ztest_run_zdb(ztest_opts.zo_pool);
6842 (void) rwlock_destroy(&ztest_name_lock);
6843 mutex_destroy(&ztest_vdev_lock);
6847 * Create a storage pool with the given name and initial vdev size.
6848 * Then test spa_freeze() functionality.
6851 ztest_init(ztest_shared_t *zs)
6854 nvlist_t *nvroot, *props;
6857 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6858 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6860 kernel_init(FREAD | FWRITE);
6863 * Create the storage pool.
6865 (void) spa_destroy(ztest_opts.zo_pool);
6866 ztest_shared->zs_vdev_next_leaf = 0;
6868 zs->zs_mirrors = ztest_opts.zo_mirrors;
6869 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6870 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6871 props = make_random_props();
6872 for (i = 0; i < SPA_FEATURES; i++) {
6874 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6875 spa_feature_table[i].fi_uname));
6876 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6880 spa_create(ztest_opts.zo_pool, nvroot, props, NULL, NULL));
6881 nvlist_free(nvroot);
6884 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6885 zs->zs_metaslab_sz =
6886 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6887 spa_close(spa, FTAG);
6891 if (!ztest_opts.zo_mmp_test) {
6892 ztest_run_zdb(ztest_opts.zo_pool);
6894 ztest_run_zdb(ztest_opts.zo_pool);
6897 (void) rwlock_destroy(&ztest_name_lock);
6898 mutex_destroy(&ztest_vdev_lock);
6904 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6906 ztest_fd_data = mkstemp(ztest_name_data);
6907 ASSERT3S(ztest_fd_data, >=, 0);
6908 (void) unlink(ztest_name_data);
6912 shared_data_size(ztest_shared_hdr_t *hdr)
6916 size = hdr->zh_hdr_size;
6917 size += hdr->zh_opts_size;
6918 size += hdr->zh_size;
6919 size += hdr->zh_stats_size * hdr->zh_stats_count;
6920 size += hdr->zh_ds_size * hdr->zh_ds_count;
6929 ztest_shared_hdr_t *hdr;
6931 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6932 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6933 ASSERT(hdr != MAP_FAILED);
6935 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6937 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6938 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6939 hdr->zh_size = sizeof (ztest_shared_t);
6940 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6941 hdr->zh_stats_count = ZTEST_FUNCS;
6942 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6943 hdr->zh_ds_count = ztest_opts.zo_datasets;
6945 size = shared_data_size(hdr);
6946 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6948 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6955 ztest_shared_hdr_t *hdr;
6958 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6959 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6960 ASSERT(hdr != MAP_FAILED);
6962 size = shared_data_size(hdr);
6964 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6965 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6966 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6967 ASSERT(hdr != MAP_FAILED);
6968 buf = (uint8_t *)hdr;
6970 offset = hdr->zh_hdr_size;
6971 ztest_shared_opts = (void *)&buf[offset];
6972 offset += hdr->zh_opts_size;
6973 ztest_shared = (void *)&buf[offset];
6974 offset += hdr->zh_size;
6975 ztest_shared_callstate = (void *)&buf[offset];
6976 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6977 ztest_shared_ds = (void *)&buf[offset];
6981 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6985 char *cmdbuf = NULL;
6990 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6991 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6996 fatal(1, "fork failed");
6998 if (pid == 0) { /* child */
6999 char *emptyargv[2] = { cmd, NULL };
7000 char fd_data_str[12];
7002 struct rlimit rl = { 1024, 1024 };
7003 (void) setrlimit(RLIMIT_NOFILE, &rl);
7005 (void) close(ztest_fd_rand);
7006 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
7007 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
7009 (void) enable_extended_FILE_stdio(-1, -1);
7010 if (libpath != NULL)
7011 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
7012 (void) execv(cmd, emptyargv);
7013 ztest_dump_core = B_FALSE;
7014 fatal(B_TRUE, "exec failed: %s", cmd);
7017 if (cmdbuf != NULL) {
7018 umem_free(cmdbuf, MAXPATHLEN);
7022 while (waitpid(pid, &status, 0) != pid)
7024 if (statusp != NULL)
7027 if (WIFEXITED(status)) {
7028 if (WEXITSTATUS(status) != 0) {
7029 (void) fprintf(stderr, "child exited with code %d\n",
7030 WEXITSTATUS(status));
7034 } else if (WIFSIGNALED(status)) {
7035 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
7036 (void) fprintf(stderr, "child died with signal %d\n",
7042 (void) fprintf(stderr, "something strange happened to child\n");
7049 ztest_run_init(void)
7053 ztest_shared_t *zs = ztest_shared;
7056 * Blow away any existing copy of zpool.cache
7058 (void) remove(spa_config_path);
7060 if (ztest_opts.zo_init == 0) {
7061 if (ztest_opts.zo_verbose >= 1)
7062 (void) printf("Importing pool %s\n",
7063 ztest_opts.zo_pool);
7069 * Create and initialize our storage pool.
7071 for (i = 1; i <= ztest_opts.zo_init; i++) {
7072 bzero(zs, sizeof (ztest_shared_t));
7073 if (ztest_opts.zo_verbose >= 3 &&
7074 ztest_opts.zo_init != 1) {
7075 (void) printf("ztest_init(), pass %d\n", i);
7082 main(int argc, char **argv)
7090 ztest_shared_callstate_t *zc;
7092 char numbuf[NN_NUMBUF_SZ];
7097 char *fd_data_str = getenv("ZTEST_FD_DATA");
7098 struct sigaction action;
7100 (void) setvbuf(stdout, NULL, _IOLBF, 0);
7102 dprintf_setup(&argc, argv);
7103 zfs_deadman_synctime_ms = 300000;
7105 action.sa_handler = sig_handler;
7106 sigemptyset(&action.sa_mask);
7107 action.sa_flags = 0;
7109 if (sigaction(SIGSEGV, &action, NULL) < 0) {
7110 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
7115 if (sigaction(SIGABRT, &action, NULL) < 0) {
7116 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
7122 * Force random_get_bytes() to use /dev/urandom in order to prevent
7123 * ztest from needlessly depleting the system entropy pool.
7125 random_path = "/dev/urandom";
7126 ztest_fd_rand = open(random_path, O_RDONLY);
7127 ASSERT3S(ztest_fd_rand, >=, 0);
7130 process_options(argc, argv);
7135 bcopy(&ztest_opts, ztest_shared_opts,
7136 sizeof (*ztest_shared_opts));
7138 ztest_fd_data = atoi(fd_data_str);
7140 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
7142 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
7144 /* Override location of zpool.cache */
7145 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
7146 ztest_opts.zo_dir) != -1);
7148 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
7153 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
7154 metaslab_df_alloc_threshold =
7155 zs->zs_metaslab_df_alloc_threshold;
7164 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
7166 if (ztest_opts.zo_verbose >= 1) {
7167 (void) printf("%llu vdevs, %d datasets, %d threads,"
7168 " %llu seconds...\n",
7169 (u_longlong_t)ztest_opts.zo_vdevs,
7170 ztest_opts.zo_datasets,
7171 ztest_opts.zo_threads,
7172 (u_longlong_t)ztest_opts.zo_time);
7175 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7176 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7178 zs->zs_do_init = B_TRUE;
7179 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7180 if (ztest_opts.zo_verbose >= 1) {
7181 (void) printf("Executing older ztest for "
7182 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7184 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7185 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7187 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7189 zs->zs_do_init = B_FALSE;
7191 zs->zs_proc_start = gethrtime();
7192 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7194 for (f = 0; f < ZTEST_FUNCS; f++) {
7195 zi = &ztest_info[f];
7196 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7197 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7198 zc->zc_next = UINT64_MAX;
7200 zc->zc_next = zs->zs_proc_start +
7201 ztest_random(2 * zi->zi_interval[0] + 1);
7205 * Run the tests in a loop. These tests include fault injection
7206 * to verify that self-healing data works, and forced crashes
7207 * to verify that we never lose on-disk consistency.
7209 while (gethrtime() < zs->zs_proc_stop) {
7214 * Initialize the workload counters for each function.
7216 for (f = 0; f < ZTEST_FUNCS; f++) {
7217 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7222 /* Set the allocation switch size */
7223 zs->zs_metaslab_df_alloc_threshold =
7224 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7226 if (!hasalt || ztest_random(2) == 0) {
7227 if (hasalt && ztest_opts.zo_verbose >= 1) {
7228 (void) printf("Executing newer ztest: %s\n",
7232 killed = exec_child(cmd, NULL, B_TRUE, &status);
7234 if (hasalt && ztest_opts.zo_verbose >= 1) {
7235 (void) printf("Executing older ztest: %s\n",
7236 ztest_opts.zo_alt_ztest);
7239 killed = exec_child(ztest_opts.zo_alt_ztest,
7240 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7247 if (ztest_opts.zo_verbose >= 1) {
7248 hrtime_t now = gethrtime();
7250 now = MIN(now, zs->zs_proc_stop);
7251 print_time(zs->zs_proc_stop - now, timebuf);
7252 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7254 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7255 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7257 WIFEXITED(status) ? "Complete" : "SIGKILL",
7258 (u_longlong_t)zs->zs_enospc_count,
7259 100.0 * zs->zs_alloc / zs->zs_space,
7261 100.0 * (now - zs->zs_proc_start) /
7262 (ztest_opts.zo_time * NANOSEC), timebuf);
7265 if (ztest_opts.zo_verbose >= 2) {
7266 (void) printf("\nWorkload summary:\n\n");
7267 (void) printf("%7s %9s %s\n",
7268 "Calls", "Time", "Function");
7269 (void) printf("%7s %9s %s\n",
7270 "-----", "----", "--------");
7271 for (f = 0; f < ZTEST_FUNCS; f++) {
7272 zi = &ztest_info[f];
7273 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7274 print_time(zc->zc_time, timebuf);
7275 (void) printf("%7llu %9s %s\n",
7276 (u_longlong_t)zc->zc_count, timebuf,
7279 (void) printf("\n");
7283 * It's possible that we killed a child during a rename test,
7284 * in which case we'll have a 'ztest_tmp' pool lying around
7285 * instead of 'ztest'. Do a blind rename in case this happened.
7288 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
7289 spa_close(spa, FTAG);
7291 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
7293 kernel_init(FREAD | FWRITE);
7294 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
7295 ztest_opts.zo_pool);
7296 (void) spa_rename(tmpname, ztest_opts.zo_pool);
7300 if (!ztest_opts.zo_mmp_test)
7301 ztest_run_zdb(ztest_opts.zo_pool);
7304 if (ztest_opts.zo_verbose >= 1) {
7306 (void) printf("%d runs of older ztest: %s\n", older,
7307 ztest_opts.zo_alt_ztest);
7308 (void) printf("%d runs of newer ztest: %s\n", newer,
7311 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7312 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7315 umem_free(cmd, MAXNAMELEN);