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, 2015 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatibility
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/vdev_impl.h>
105 #include <sys/vdev_file.h>
106 #include <sys/spa_impl.h>
107 #include <sys/metaslab_impl.h>
108 #include <sys/dsl_prop.h>
109 #include <sys/dsl_dataset.h>
110 #include <sys/dsl_destroy.h>
111 #include <sys/dsl_scan.h>
112 #include <sys/zio_checksum.h>
113 #include <sys/refcount.h>
114 #include <sys/zfeature.h>
115 #include <sys/dsl_userhold.h>
117 #include <stdio_ext.h>
124 #include <sys/fs/zfs.h>
125 #include <libnvpair.h>
127 #include <execinfo.h> /* for backtrace() */
130 static int ztest_fd_data = -1;
131 static int ztest_fd_rand = -1;
133 typedef struct ztest_shared_hdr {
134 uint64_t zh_hdr_size;
135 uint64_t zh_opts_size;
137 uint64_t zh_stats_size;
138 uint64_t zh_stats_count;
140 uint64_t zh_ds_count;
141 } ztest_shared_hdr_t;
143 static ztest_shared_hdr_t *ztest_shared_hdr;
145 typedef struct ztest_shared_opts {
146 char zo_pool[MAXNAMELEN];
147 char zo_dir[MAXNAMELEN];
148 char zo_alt_ztest[MAXNAMELEN];
149 char zo_alt_libpath[MAXNAMELEN];
151 uint64_t zo_vdevtime;
159 uint64_t zo_passtime;
160 uint64_t zo_killrate;
164 uint64_t zo_maxloops;
165 uint64_t zo_metaslab_gang_bang;
166 } ztest_shared_opts_t;
168 static const ztest_shared_opts_t ztest_opts_defaults = {
169 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
170 .zo_dir = { '/', 't', 'm', 'p', '\0' },
171 .zo_alt_ztest = { '\0' },
172 .zo_alt_libpath = { '\0' },
174 .zo_ashift = SPA_MINBLOCKSHIFT,
177 .zo_raidz_parity = 1,
178 .zo_vdev_size = SPA_MINDEVSIZE,
181 .zo_passtime = 60, /* 60 seconds */
182 .zo_killrate = 70, /* 70% kill rate */
185 .zo_time = 300, /* 5 minutes */
186 .zo_maxloops = 50, /* max loops during spa_freeze() */
187 .zo_metaslab_gang_bang = 32 << 10
190 extern uint64_t metaslab_gang_bang;
191 extern uint64_t metaslab_df_alloc_threshold;
192 extern int metaslab_preload_limit;
194 static ztest_shared_opts_t *ztest_shared_opts;
195 static ztest_shared_opts_t ztest_opts;
197 typedef struct ztest_shared_ds {
201 static ztest_shared_ds_t *ztest_shared_ds;
202 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
204 #define BT_MAGIC 0x123456789abcdefULL
205 #define MAXFAULTS() \
206 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
210 ZTEST_IO_WRITE_PATTERN,
211 ZTEST_IO_WRITE_ZEROES,
218 typedef struct ztest_block_tag {
228 typedef struct bufwad {
235 * XXX -- fix zfs range locks to be generic so we can use them here.
257 #define ZTEST_RANGE_LOCKS 64
258 #define ZTEST_OBJECT_LOCKS 64
261 * Object descriptor. Used as a template for object lookup/create/remove.
263 typedef struct ztest_od {
266 dmu_object_type_t od_type;
267 dmu_object_type_t od_crtype;
268 uint64_t od_blocksize;
269 uint64_t od_crblocksize;
272 char od_name[MAXNAMELEN];
278 typedef struct ztest_ds {
279 ztest_shared_ds_t *zd_shared;
281 rwlock_t zd_zilog_lock;
283 ztest_od_t *zd_od; /* debugging aid */
284 char zd_name[MAXNAMELEN];
285 kmutex_t zd_dirobj_lock;
286 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
287 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
291 * Per-iteration state.
293 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
295 typedef struct ztest_info {
296 ztest_func_t *zi_func; /* test function */
297 uint64_t zi_iters; /* iterations per execution */
298 uint64_t *zi_interval; /* execute every <interval> seconds */
299 const char *zi_funcname; /* name of test function */
302 typedef struct ztest_shared_callstate {
303 uint64_t zc_count; /* per-pass count */
304 uint64_t zc_time; /* per-pass time */
305 uint64_t zc_next; /* next time to call this function */
306 } ztest_shared_callstate_t;
308 static ztest_shared_callstate_t *ztest_shared_callstate;
309 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
311 ztest_func_t ztest_dmu_read_write;
312 ztest_func_t ztest_dmu_write_parallel;
313 ztest_func_t ztest_dmu_object_alloc_free;
314 ztest_func_t ztest_dmu_commit_callbacks;
315 ztest_func_t ztest_zap;
316 ztest_func_t ztest_zap_parallel;
317 ztest_func_t ztest_zil_commit;
318 ztest_func_t ztest_zil_remount;
319 ztest_func_t ztest_dmu_read_write_zcopy;
320 ztest_func_t ztest_dmu_objset_create_destroy;
321 ztest_func_t ztest_dmu_prealloc;
322 ztest_func_t ztest_fzap;
323 ztest_func_t ztest_dmu_snapshot_create_destroy;
324 ztest_func_t ztest_dsl_prop_get_set;
325 ztest_func_t ztest_spa_prop_get_set;
326 ztest_func_t ztest_spa_create_destroy;
327 ztest_func_t ztest_fault_inject;
328 ztest_func_t ztest_ddt_repair;
329 ztest_func_t ztest_dmu_snapshot_hold;
330 ztest_func_t ztest_spa_rename;
331 ztest_func_t ztest_scrub;
332 ztest_func_t ztest_dsl_dataset_promote_busy;
333 ztest_func_t ztest_vdev_attach_detach;
334 ztest_func_t ztest_vdev_LUN_growth;
335 ztest_func_t ztest_vdev_add_remove;
336 ztest_func_t ztest_vdev_aux_add_remove;
337 ztest_func_t ztest_split_pool;
338 ztest_func_t ztest_reguid;
339 ztest_func_t ztest_spa_upgrade;
341 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
342 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
343 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
344 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
345 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
347 #define ZTI_INIT(func, iters, interval) \
348 { .zi_func = (func), \
349 .zi_iters = (iters), \
350 .zi_interval = (interval), \
351 .zi_funcname = # func }
353 ztest_info_t ztest_info[] = {
354 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
355 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
356 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
357 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
358 ZTI_INIT(ztest_zap, 30, &zopt_always),
359 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
360 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
361 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
362 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
363 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
364 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
365 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
366 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
371 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
372 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
373 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
374 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
376 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
377 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
378 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
379 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
380 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
381 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
382 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
383 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
384 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
387 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
390 * The following struct is used to hold a list of uncalled commit callbacks.
391 * The callbacks are ordered by txg number.
393 typedef struct ztest_cb_list {
394 kmutex_t zcl_callbacks_lock;
395 list_t zcl_callbacks;
399 * Stuff we need to share writably between parent and child.
401 typedef struct ztest_shared {
402 boolean_t zs_do_init;
403 hrtime_t zs_proc_start;
404 hrtime_t zs_proc_stop;
405 hrtime_t zs_thread_start;
406 hrtime_t zs_thread_stop;
407 hrtime_t zs_thread_kill;
408 uint64_t zs_enospc_count;
409 uint64_t zs_vdev_next_leaf;
410 uint64_t zs_vdev_aux;
415 uint64_t zs_metaslab_sz;
416 uint64_t zs_metaslab_df_alloc_threshold;
420 #define ID_PARALLEL -1ULL
422 static char ztest_dev_template[] = "%s/%s.%llua";
423 static char ztest_aux_template[] = "%s/%s.%s.%llu";
424 ztest_shared_t *ztest_shared;
426 static spa_t *ztest_spa = NULL;
427 static ztest_ds_t *ztest_ds;
429 static kmutex_t ztest_vdev_lock;
432 * The ztest_name_lock protects the pool and dataset namespace used by
433 * the individual tests. To modify the namespace, consumers must grab
434 * this lock as writer. Grabbing the lock as reader will ensure that the
435 * namespace does not change while the lock is held.
437 static rwlock_t ztest_name_lock;
439 static boolean_t ztest_dump_core = B_TRUE;
440 static boolean_t ztest_exiting;
442 /* Global commit callback list */
443 static ztest_cb_list_t zcl;
444 /* Commit cb delay */
445 static uint64_t zc_min_txg_delay = UINT64_MAX;
446 static int zc_cb_counter = 0;
449 * Minimum number of commit callbacks that need to be registered for us to check
450 * whether the minimum txg delay is acceptable.
452 #define ZTEST_COMMIT_CB_MIN_REG 100
455 * If a number of txgs equal to this threshold have been created after a commit
456 * callback has been registered but not called, then we assume there is an
457 * implementation bug.
459 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
461 extern uint64_t metaslab_gang_bang;
462 extern uint64_t metaslab_df_alloc_threshold;
465 ZTEST_META_DNODE = 0,
470 static void usage(boolean_t) __NORETURN;
473 * These libumem hooks provide a reasonable set of defaults for the allocator's
474 * debugging facilities.
477 _umem_debug_init(void)
479 return ("default,verbose"); /* $UMEM_DEBUG setting */
483 _umem_logging_init(void)
485 return ("fail,contents"); /* $UMEM_LOGGING setting */
488 #define BACKTRACE_SZ 100
490 static void sig_handler(int signo)
492 struct sigaction action;
493 #ifdef __GNUC__ /* backtrace() is a GNU extension */
495 void *buffer[BACKTRACE_SZ];
497 nptrs = backtrace(buffer, BACKTRACE_SZ);
498 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
502 * Restore default action and re-raise signal so SIGSEGV and
503 * SIGABRT can trigger a core dump.
505 action.sa_handler = SIG_DFL;
506 sigemptyset(&action.sa_mask);
508 (void) sigaction(signo, &action, NULL);
512 #define FATAL_MSG_SZ 1024
517 fatal(int do_perror, char *message, ...)
520 int save_errno = errno;
523 (void) fflush(stdout);
524 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
526 va_start(args, message);
527 (void) sprintf(buf, "ztest: ");
529 (void) vsprintf(buf + strlen(buf), message, args);
532 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
533 ": %s", strerror(save_errno));
535 (void) fprintf(stderr, "%s\n", buf);
536 fatal_msg = buf; /* to ease debugging */
543 str2shift(const char *buf)
545 const char *ends = "BKMGTPEZ";
550 for (i = 0; i < strlen(ends); i++) {
551 if (toupper(buf[0]) == ends[i])
554 if (i == strlen(ends)) {
555 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
559 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
562 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
568 nicenumtoull(const char *buf)
573 val = strtoull(buf, &end, 0);
575 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
577 } else if (end[0] == '.') {
578 double fval = strtod(buf, &end);
579 fval *= pow(2, str2shift(end));
580 if (fval > UINT64_MAX) {
581 (void) fprintf(stderr, "ztest: value too large: %s\n",
585 val = (uint64_t)fval;
587 int shift = str2shift(end);
588 if (shift >= 64 || (val << shift) >> shift != val) {
589 (void) fprintf(stderr, "ztest: value too large: %s\n",
599 usage(boolean_t requested)
601 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
603 char nice_vdev_size[10];
604 char nice_gang_bang[10];
605 FILE *fp = requested ? stdout : stderr;
607 nicenum(zo->zo_vdev_size, nice_vdev_size);
608 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
610 (void) fprintf(fp, "Usage: %s\n"
611 "\t[-v vdevs (default: %llu)]\n"
612 "\t[-s size_of_each_vdev (default: %s)]\n"
613 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
614 "\t[-m mirror_copies (default: %d)]\n"
615 "\t[-r raidz_disks (default: %d)]\n"
616 "\t[-R raidz_parity (default: %d)]\n"
617 "\t[-d datasets (default: %d)]\n"
618 "\t[-t threads (default: %d)]\n"
619 "\t[-g gang_block_threshold (default: %s)]\n"
620 "\t[-i init_count (default: %d)] initialize pool i times\n"
621 "\t[-k kill_percentage (default: %llu%%)]\n"
622 "\t[-p pool_name (default: %s)]\n"
623 "\t[-f dir (default: %s)] file directory for vdev files\n"
624 "\t[-V] verbose (use multiple times for ever more blather)\n"
625 "\t[-E] use existing pool instead of creating new one\n"
626 "\t[-T time (default: %llu sec)] total run time\n"
627 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
628 "\t[-P passtime (default: %llu sec)] time per pass\n"
629 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
630 "\t[-h] (print help)\n"
633 (u_longlong_t)zo->zo_vdevs, /* -v */
634 nice_vdev_size, /* -s */
635 zo->zo_ashift, /* -a */
636 zo->zo_mirrors, /* -m */
637 zo->zo_raidz, /* -r */
638 zo->zo_raidz_parity, /* -R */
639 zo->zo_datasets, /* -d */
640 zo->zo_threads, /* -t */
641 nice_gang_bang, /* -g */
642 zo->zo_init, /* -i */
643 (u_longlong_t)zo->zo_killrate, /* -k */
644 zo->zo_pool, /* -p */
646 (u_longlong_t)zo->zo_time, /* -T */
647 (u_longlong_t)zo->zo_maxloops, /* -F */
648 (u_longlong_t)zo->zo_passtime);
649 exit(requested ? 0 : 1);
653 process_options(int argc, char **argv)
656 ztest_shared_opts_t *zo = &ztest_opts;
660 char altdir[MAXNAMELEN] = { 0 };
662 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
664 while ((opt = getopt(argc, argv,
665 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
682 value = nicenumtoull(optarg);
686 zo->zo_vdevs = value;
689 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
692 zo->zo_ashift = value;
695 zo->zo_mirrors = value;
698 zo->zo_raidz = MAX(1, value);
701 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
704 zo->zo_datasets = MAX(1, value);
707 zo->zo_threads = MAX(1, value);
710 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
717 zo->zo_killrate = value;
720 (void) strlcpy(zo->zo_pool, optarg,
721 sizeof (zo->zo_pool));
724 path = realpath(optarg, NULL);
726 (void) fprintf(stderr, "error: %s: %s\n",
727 optarg, strerror(errno));
730 (void) strlcpy(zo->zo_dir, path,
731 sizeof (zo->zo_dir));
744 zo->zo_passtime = MAX(1, value);
747 zo->zo_maxloops = MAX(1, value);
750 (void) strlcpy(altdir, optarg, sizeof (altdir));
762 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
765 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
768 if (strlen(altdir) > 0) {
776 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
777 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
779 VERIFY(NULL != realpath(getexecname(), cmd));
780 if (0 != access(altdir, F_OK)) {
781 ztest_dump_core = B_FALSE;
782 fatal(B_TRUE, "invalid alternate ztest path: %s",
785 VERIFY(NULL != realpath(altdir, realaltdir));
788 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
789 * We want to extract <isa> to determine if we should use
790 * 32 or 64 bit binaries.
792 bin = strstr(cmd, "/usr/bin/");
793 ztest = strstr(bin, "/ztest");
795 isalen = ztest - isa;
796 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
797 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
798 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
799 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
801 if (0 != access(zo->zo_alt_ztest, X_OK)) {
802 ztest_dump_core = B_FALSE;
803 fatal(B_TRUE, "invalid alternate ztest: %s",
805 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
806 ztest_dump_core = B_FALSE;
807 fatal(B_TRUE, "invalid alternate lib directory %s",
811 umem_free(cmd, MAXPATHLEN);
812 umem_free(realaltdir, MAXPATHLEN);
817 ztest_kill(ztest_shared_t *zs)
819 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
820 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
823 * Before we kill off ztest, make sure that the config is updated.
824 * See comment above spa_config_sync().
826 mutex_enter(&spa_namespace_lock);
827 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
828 mutex_exit(&spa_namespace_lock);
830 if (ztest_opts.zo_verbose >= 3)
831 zfs_dbgmsg_print(FTAG);
833 (void) kill(getpid(), SIGKILL);
837 ztest_random(uint64_t range)
841 ASSERT3S(ztest_fd_rand, >=, 0);
846 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
847 fatal(1, "short read from /dev/urandom");
854 ztest_record_enospc(const char *s)
856 ztest_shared->zs_enospc_count++;
860 ztest_get_ashift(void)
862 if (ztest_opts.zo_ashift == 0)
863 return (SPA_MINBLOCKSHIFT + ztest_random(5));
864 return (ztest_opts.zo_ashift);
868 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
874 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
877 ashift = ztest_get_ashift();
883 vdev = ztest_shared->zs_vdev_aux;
884 (void) snprintf(path, MAXPATHLEN,
885 ztest_aux_template, ztest_opts.zo_dir,
886 pool == NULL ? ztest_opts.zo_pool : pool,
889 vdev = ztest_shared->zs_vdev_next_leaf++;
890 (void) snprintf(path, MAXPATHLEN,
891 ztest_dev_template, ztest_opts.zo_dir,
892 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
897 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
899 fatal(1, "can't open %s", path);
900 if (ftruncate(fd, size) != 0)
901 fatal(1, "can't ftruncate %s", path);
905 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
906 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
907 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
908 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
909 umem_free(pathbuf, MAXPATHLEN);
915 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
916 uint64_t ashift, int r)
918 nvlist_t *raidz, **child;
922 return (make_vdev_file(path, aux, pool, size, ashift));
923 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
925 for (c = 0; c < r; c++)
926 child[c] = make_vdev_file(path, aux, pool, size, ashift);
928 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
929 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
930 VDEV_TYPE_RAIDZ) == 0);
931 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
932 ztest_opts.zo_raidz_parity) == 0);
933 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
936 for (c = 0; c < r; c++)
937 nvlist_free(child[c]);
939 umem_free(child, r * sizeof (nvlist_t *));
945 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
946 uint64_t ashift, int r, int m)
948 nvlist_t *mirror, **child;
952 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
954 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
956 for (c = 0; c < m; c++)
957 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
959 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
960 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
961 VDEV_TYPE_MIRROR) == 0);
962 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
965 for (c = 0; c < m; c++)
966 nvlist_free(child[c]);
968 umem_free(child, m * sizeof (nvlist_t *));
974 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
975 int log, int r, int m, int t)
977 nvlist_t *root, **child;
982 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
984 for (c = 0; c < t; c++) {
985 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
987 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
991 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
992 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
993 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
996 for (c = 0; c < t; c++)
997 nvlist_free(child[c]);
999 umem_free(child, t * sizeof (nvlist_t *));
1005 * Find a random spa version. Returns back a random spa version in the
1006 * range [initial_version, SPA_VERSION_FEATURES].
1009 ztest_random_spa_version(uint64_t initial_version)
1011 uint64_t version = initial_version;
1013 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1015 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1018 if (version > SPA_VERSION_BEFORE_FEATURES)
1019 version = SPA_VERSION_FEATURES;
1021 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1026 ztest_random_blocksize(void)
1029 * Choose a block size >= the ashift.
1030 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1032 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1033 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1035 uint64_t block_shift =
1036 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1037 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1041 ztest_random_ibshift(void)
1043 return (DN_MIN_INDBLKSHIFT +
1044 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1048 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1051 vdev_t *rvd = spa->spa_root_vdev;
1054 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1057 top = ztest_random(rvd->vdev_children);
1058 tvd = rvd->vdev_child[top];
1059 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1060 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1066 ztest_random_dsl_prop(zfs_prop_t prop)
1071 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1072 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1078 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1081 const char *propname = zfs_prop_to_name(prop);
1082 const char *valname;
1087 error = dsl_prop_set_int(osname, propname,
1088 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1090 if (error == ENOSPC) {
1091 ztest_record_enospc(FTAG);
1096 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1097 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1099 if (ztest_opts.zo_verbose >= 6) {
1100 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1101 (void) printf("%s %s = %s at '%s'\n",
1102 osname, propname, valname, setpoint);
1104 umem_free(setpoint, MAXPATHLEN);
1110 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1112 spa_t *spa = ztest_spa;
1113 nvlist_t *props = NULL;
1116 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1117 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1119 error = spa_prop_set(spa, props);
1123 if (error == ENOSPC) {
1124 ztest_record_enospc(FTAG);
1133 ztest_rll_init(rll_t *rll)
1135 rll->rll_writer = NULL;
1136 rll->rll_readers = 0;
1137 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1138 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1142 ztest_rll_destroy(rll_t *rll)
1144 ASSERT(rll->rll_writer == NULL);
1145 ASSERT(rll->rll_readers == 0);
1146 mutex_destroy(&rll->rll_lock);
1147 cv_destroy(&rll->rll_cv);
1151 ztest_rll_lock(rll_t *rll, rl_type_t type)
1153 mutex_enter(&rll->rll_lock);
1155 if (type == RL_READER) {
1156 while (rll->rll_writer != NULL)
1157 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1160 while (rll->rll_writer != NULL || rll->rll_readers)
1161 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1162 rll->rll_writer = curthread;
1165 mutex_exit(&rll->rll_lock);
1169 ztest_rll_unlock(rll_t *rll)
1171 mutex_enter(&rll->rll_lock);
1173 if (rll->rll_writer) {
1174 ASSERT(rll->rll_readers == 0);
1175 rll->rll_writer = NULL;
1177 ASSERT(rll->rll_readers != 0);
1178 ASSERT(rll->rll_writer == NULL);
1182 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1183 cv_broadcast(&rll->rll_cv);
1185 mutex_exit(&rll->rll_lock);
1189 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1191 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1193 ztest_rll_lock(rll, type);
1197 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1199 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1201 ztest_rll_unlock(rll);
1205 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1206 uint64_t size, rl_type_t type)
1208 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1209 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1212 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1213 rl->rl_object = object;
1214 rl->rl_offset = offset;
1218 ztest_rll_lock(rll, type);
1224 ztest_range_unlock(rl_t *rl)
1226 rll_t *rll = rl->rl_lock;
1228 ztest_rll_unlock(rll);
1230 umem_free(rl, sizeof (*rl));
1234 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1237 zd->zd_zilog = dmu_objset_zil(os);
1238 zd->zd_shared = szd;
1239 dmu_objset_name(os, zd->zd_name);
1242 if (zd->zd_shared != NULL)
1243 zd->zd_shared->zd_seq = 0;
1245 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1246 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1248 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1249 ztest_rll_init(&zd->zd_object_lock[l]);
1251 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1252 ztest_rll_init(&zd->zd_range_lock[l]);
1256 ztest_zd_fini(ztest_ds_t *zd)
1260 mutex_destroy(&zd->zd_dirobj_lock);
1261 (void) rwlock_destroy(&zd->zd_zilog_lock);
1263 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1264 ztest_rll_destroy(&zd->zd_object_lock[l]);
1266 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1267 ztest_rll_destroy(&zd->zd_range_lock[l]);
1270 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1273 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1279 * Attempt to assign tx to some transaction group.
1281 error = dmu_tx_assign(tx, txg_how);
1283 if (error == ERESTART) {
1284 ASSERT(txg_how == TXG_NOWAIT);
1287 ASSERT3U(error, ==, ENOSPC);
1288 ztest_record_enospc(tag);
1293 txg = dmu_tx_get_txg(tx);
1299 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1302 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1310 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1313 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1317 diff |= (value - *ip++);
1324 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1325 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1327 bt->bt_magic = BT_MAGIC;
1328 bt->bt_objset = dmu_objset_id(os);
1329 bt->bt_object = object;
1330 bt->bt_offset = offset;
1333 bt->bt_crtxg = crtxg;
1337 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1338 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1340 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1341 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1342 ASSERT3U(bt->bt_object, ==, object);
1343 ASSERT3U(bt->bt_offset, ==, offset);
1344 ASSERT3U(bt->bt_gen, <=, gen);
1345 ASSERT3U(bt->bt_txg, <=, txg);
1346 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1349 static ztest_block_tag_t *
1350 ztest_bt_bonus(dmu_buf_t *db)
1352 dmu_object_info_t doi;
1353 ztest_block_tag_t *bt;
1355 dmu_object_info_from_db(db, &doi);
1356 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1357 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1358 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1367 #define lrz_type lr_mode
1368 #define lrz_blocksize lr_uid
1369 #define lrz_ibshift lr_gid
1370 #define lrz_bonustype lr_rdev
1371 #define lrz_bonuslen lr_crtime[1]
1374 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1376 char *name = (void *)(lr + 1); /* name follows lr */
1377 size_t namesize = strlen(name) + 1;
1380 if (zil_replaying(zd->zd_zilog, tx))
1383 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1384 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1385 sizeof (*lr) + namesize - sizeof (lr_t));
1387 zil_itx_assign(zd->zd_zilog, itx, tx);
1391 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1393 char *name = (void *)(lr + 1); /* name follows lr */
1394 size_t namesize = strlen(name) + 1;
1397 if (zil_replaying(zd->zd_zilog, tx))
1400 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1401 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1402 sizeof (*lr) + namesize - sizeof (lr_t));
1404 itx->itx_oid = object;
1405 zil_itx_assign(zd->zd_zilog, itx, tx);
1409 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1412 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1414 if (zil_replaying(zd->zd_zilog, tx))
1417 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1418 write_state = WR_INDIRECT;
1420 itx = zil_itx_create(TX_WRITE,
1421 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1423 if (write_state == WR_COPIED &&
1424 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1425 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1426 zil_itx_destroy(itx);
1427 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1428 write_state = WR_NEED_COPY;
1430 itx->itx_private = zd;
1431 itx->itx_wr_state = write_state;
1432 itx->itx_sync = (ztest_random(8) == 0);
1433 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1435 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1436 sizeof (*lr) - sizeof (lr_t));
1438 zil_itx_assign(zd->zd_zilog, itx, tx);
1442 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1446 if (zil_replaying(zd->zd_zilog, tx))
1449 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1450 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1451 sizeof (*lr) - sizeof (lr_t));
1453 itx->itx_sync = B_FALSE;
1454 zil_itx_assign(zd->zd_zilog, itx, tx);
1458 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1462 if (zil_replaying(zd->zd_zilog, tx))
1465 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1466 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1467 sizeof (*lr) - sizeof (lr_t));
1469 itx->itx_sync = B_FALSE;
1470 zil_itx_assign(zd->zd_zilog, itx, tx);
1477 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1479 char *name = (void *)(lr + 1); /* name follows lr */
1480 objset_t *os = zd->zd_os;
1481 ztest_block_tag_t *bbt;
1488 byteswap_uint64_array(lr, sizeof (*lr));
1490 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1491 ASSERT(name[0] != '\0');
1493 tx = dmu_tx_create(os);
1495 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1497 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1498 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1500 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1503 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1507 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1509 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1510 if (lr->lr_foid == 0) {
1511 lr->lr_foid = zap_create(os,
1512 lr->lrz_type, lr->lrz_bonustype,
1513 lr->lrz_bonuslen, tx);
1515 error = zap_create_claim(os, lr->lr_foid,
1516 lr->lrz_type, lr->lrz_bonustype,
1517 lr->lrz_bonuslen, tx);
1520 if (lr->lr_foid == 0) {
1521 lr->lr_foid = dmu_object_alloc(os,
1522 lr->lrz_type, 0, lr->lrz_bonustype,
1523 lr->lrz_bonuslen, tx);
1525 error = dmu_object_claim(os, lr->lr_foid,
1526 lr->lrz_type, 0, lr->lrz_bonustype,
1527 lr->lrz_bonuslen, tx);
1532 ASSERT3U(error, ==, EEXIST);
1533 ASSERT(zd->zd_zilog->zl_replay);
1538 ASSERT(lr->lr_foid != 0);
1540 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1541 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1542 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1544 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1545 bbt = ztest_bt_bonus(db);
1546 dmu_buf_will_dirty(db, tx);
1547 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1548 dmu_buf_rele(db, FTAG);
1550 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1553 (void) ztest_log_create(zd, tx, lr);
1561 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1563 char *name = (void *)(lr + 1); /* name follows lr */
1564 objset_t *os = zd->zd_os;
1565 dmu_object_info_t doi;
1567 uint64_t object, txg;
1570 byteswap_uint64_array(lr, sizeof (*lr));
1572 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1573 ASSERT(name[0] != '\0');
1576 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1577 ASSERT(object != 0);
1579 ztest_object_lock(zd, object, RL_WRITER);
1581 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1583 tx = dmu_tx_create(os);
1585 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1586 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1588 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1590 ztest_object_unlock(zd, object);
1594 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1595 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1597 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1600 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1602 (void) ztest_log_remove(zd, tx, lr, object);
1606 ztest_object_unlock(zd, object);
1612 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1614 objset_t *os = zd->zd_os;
1615 void *data = lr + 1; /* data follows lr */
1616 uint64_t offset, length;
1617 ztest_block_tag_t *bt = data;
1618 ztest_block_tag_t *bbt;
1619 uint64_t gen, txg, lrtxg, crtxg;
1620 dmu_object_info_t doi;
1623 arc_buf_t *abuf = NULL;
1627 byteswap_uint64_array(lr, sizeof (*lr));
1629 offset = lr->lr_offset;
1630 length = lr->lr_length;
1632 /* If it's a dmu_sync() block, write the whole block */
1633 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1634 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1635 if (length < blocksize) {
1636 offset -= offset % blocksize;
1641 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1642 byteswap_uint64_array(bt, sizeof (*bt));
1644 if (bt->bt_magic != BT_MAGIC)
1647 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1648 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1650 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1652 dmu_object_info_from_db(db, &doi);
1654 bbt = ztest_bt_bonus(db);
1655 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1657 crtxg = bbt->bt_crtxg;
1658 lrtxg = lr->lr_common.lrc_txg;
1660 tx = dmu_tx_create(os);
1662 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1664 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1665 P2PHASE(offset, length) == 0)
1666 abuf = dmu_request_arcbuf(db, length);
1668 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1671 dmu_return_arcbuf(abuf);
1672 dmu_buf_rele(db, FTAG);
1673 ztest_range_unlock(rl);
1674 ztest_object_unlock(zd, lr->lr_foid);
1680 * Usually, verify the old data before writing new data --
1681 * but not always, because we also want to verify correct
1682 * behavior when the data was not recently read into cache.
1684 ASSERT(offset % doi.doi_data_block_size == 0);
1685 if (ztest_random(4) != 0) {
1686 int prefetch = ztest_random(2) ?
1687 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1688 ztest_block_tag_t rbt;
1690 VERIFY(dmu_read(os, lr->lr_foid, offset,
1691 sizeof (rbt), &rbt, prefetch) == 0);
1692 if (rbt.bt_magic == BT_MAGIC) {
1693 ztest_bt_verify(&rbt, os, lr->lr_foid,
1694 offset, gen, txg, crtxg);
1699 * Writes can appear to be newer than the bonus buffer because
1700 * the ztest_get_data() callback does a dmu_read() of the
1701 * open-context data, which may be different than the data
1702 * as it was when the write was generated.
1704 if (zd->zd_zilog->zl_replay) {
1705 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1706 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1711 * Set the bt's gen/txg to the bonus buffer's gen/txg
1712 * so that all of the usual ASSERTs will work.
1714 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1718 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1720 bcopy(data, abuf->b_data, length);
1721 dmu_assign_arcbuf(db, offset, abuf, tx);
1724 (void) ztest_log_write(zd, tx, lr);
1726 dmu_buf_rele(db, FTAG);
1730 ztest_range_unlock(rl);
1731 ztest_object_unlock(zd, lr->lr_foid);
1737 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1739 objset_t *os = zd->zd_os;
1745 byteswap_uint64_array(lr, sizeof (*lr));
1747 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1748 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1751 tx = dmu_tx_create(os);
1753 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1755 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1757 ztest_range_unlock(rl);
1758 ztest_object_unlock(zd, lr->lr_foid);
1762 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1763 lr->lr_length, tx) == 0);
1765 (void) ztest_log_truncate(zd, tx, lr);
1769 ztest_range_unlock(rl);
1770 ztest_object_unlock(zd, lr->lr_foid);
1776 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1778 objset_t *os = zd->zd_os;
1781 ztest_block_tag_t *bbt;
1782 uint64_t txg, lrtxg, crtxg;
1785 byteswap_uint64_array(lr, sizeof (*lr));
1787 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1789 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1791 tx = dmu_tx_create(os);
1792 dmu_tx_hold_bonus(tx, lr->lr_foid);
1794 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1796 dmu_buf_rele(db, FTAG);
1797 ztest_object_unlock(zd, lr->lr_foid);
1801 bbt = ztest_bt_bonus(db);
1802 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1803 crtxg = bbt->bt_crtxg;
1804 lrtxg = lr->lr_common.lrc_txg;
1806 if (zd->zd_zilog->zl_replay) {
1807 ASSERT(lr->lr_size != 0);
1808 ASSERT(lr->lr_mode != 0);
1812 * Randomly change the size and increment the generation.
1814 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1816 lr->lr_mode = bbt->bt_gen + 1;
1821 * Verify that the current bonus buffer is not newer than our txg.
1823 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1824 MAX(txg, lrtxg), crtxg);
1826 dmu_buf_will_dirty(db, tx);
1828 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1829 ASSERT3U(lr->lr_size, <=, db->db_size);
1830 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1831 bbt = ztest_bt_bonus(db);
1833 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1835 dmu_buf_rele(db, FTAG);
1837 (void) ztest_log_setattr(zd, tx, lr);
1841 ztest_object_unlock(zd, lr->lr_foid);
1846 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1847 NULL, /* 0 no such transaction type */
1848 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1849 NULL, /* TX_MKDIR */
1850 NULL, /* TX_MKXATTR */
1851 NULL, /* TX_SYMLINK */
1852 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1853 NULL, /* TX_RMDIR */
1855 NULL, /* TX_RENAME */
1856 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1857 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1858 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1860 NULL, /* TX_CREATE_ACL */
1861 NULL, /* TX_CREATE_ATTR */
1862 NULL, /* TX_CREATE_ACL_ATTR */
1863 NULL, /* TX_MKDIR_ACL */
1864 NULL, /* TX_MKDIR_ATTR */
1865 NULL, /* TX_MKDIR_ACL_ATTR */
1866 NULL, /* TX_WRITE2 */
1870 * ZIL get_data callbacks
1874 ztest_get_done(zgd_t *zgd, int error)
1876 ztest_ds_t *zd = zgd->zgd_private;
1877 uint64_t object = zgd->zgd_rl->rl_object;
1880 dmu_buf_rele(zgd->zgd_db, zgd);
1882 ztest_range_unlock(zgd->zgd_rl);
1883 ztest_object_unlock(zd, object);
1885 if (error == 0 && zgd->zgd_bp)
1886 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1888 umem_free(zgd, sizeof (*zgd));
1892 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1894 ztest_ds_t *zd = arg;
1895 objset_t *os = zd->zd_os;
1896 uint64_t object = lr->lr_foid;
1897 uint64_t offset = lr->lr_offset;
1898 uint64_t size = lr->lr_length;
1899 blkptr_t *bp = &lr->lr_blkptr;
1900 uint64_t txg = lr->lr_common.lrc_txg;
1902 dmu_object_info_t doi;
1907 ztest_object_lock(zd, object, RL_READER);
1908 error = dmu_bonus_hold(os, object, FTAG, &db);
1910 ztest_object_unlock(zd, object);
1914 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1916 if (crtxg == 0 || crtxg > txg) {
1917 dmu_buf_rele(db, FTAG);
1918 ztest_object_unlock(zd, object);
1922 dmu_object_info_from_db(db, &doi);
1923 dmu_buf_rele(db, FTAG);
1926 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1927 zgd->zgd_zilog = zd->zd_zilog;
1928 zgd->zgd_private = zd;
1930 if (buf != NULL) { /* immediate write */
1931 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1934 error = dmu_read(os, object, offset, size, buf,
1935 DMU_READ_NO_PREFETCH);
1938 size = doi.doi_data_block_size;
1940 offset = P2ALIGN(offset, size);
1942 ASSERT(offset < size);
1946 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1949 error = dmu_buf_hold(os, object, offset, zgd, &db,
1950 DMU_READ_NO_PREFETCH);
1953 blkptr_t *obp = dmu_buf_get_blkptr(db);
1955 ASSERT(BP_IS_HOLE(bp));
1962 ASSERT(db->db_offset == offset);
1963 ASSERT(db->db_size == size);
1965 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1966 ztest_get_done, zgd);
1973 ztest_get_done(zgd, error);
1979 ztest_lr_alloc(size_t lrsize, char *name)
1982 size_t namesize = name ? strlen(name) + 1 : 0;
1984 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1987 bcopy(name, lr + lrsize, namesize);
1993 ztest_lr_free(void *lr, size_t lrsize, char *name)
1995 size_t namesize = name ? strlen(name) + 1 : 0;
1997 umem_free(lr, lrsize + namesize);
2001 * Lookup a bunch of objects. Returns the number of objects not found.
2004 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2010 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2012 for (i = 0; i < count; i++, od++) {
2014 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2015 sizeof (uint64_t), 1, &od->od_object);
2017 ASSERT(error == ENOENT);
2018 ASSERT(od->od_object == 0);
2022 ztest_block_tag_t *bbt;
2023 dmu_object_info_t doi;
2025 ASSERT(od->od_object != 0);
2026 ASSERT(missing == 0); /* there should be no gaps */
2028 ztest_object_lock(zd, od->od_object, RL_READER);
2029 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2030 od->od_object, FTAG, &db));
2031 dmu_object_info_from_db(db, &doi);
2032 bbt = ztest_bt_bonus(db);
2033 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2034 od->od_type = doi.doi_type;
2035 od->od_blocksize = doi.doi_data_block_size;
2036 od->od_gen = bbt->bt_gen;
2037 dmu_buf_rele(db, FTAG);
2038 ztest_object_unlock(zd, od->od_object);
2046 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2051 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2053 for (i = 0; i < count; i++, od++) {
2060 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2062 lr->lr_doid = od->od_dir;
2063 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2064 lr->lrz_type = od->od_crtype;
2065 lr->lrz_blocksize = od->od_crblocksize;
2066 lr->lrz_ibshift = ztest_random_ibshift();
2067 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2068 lr->lrz_bonuslen = dmu_bonus_max();
2069 lr->lr_gen = od->od_crgen;
2070 lr->lr_crtime[0] = time(NULL);
2072 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2073 ASSERT(missing == 0);
2077 od->od_object = lr->lr_foid;
2078 od->od_type = od->od_crtype;
2079 od->od_blocksize = od->od_crblocksize;
2080 od->od_gen = od->od_crgen;
2081 ASSERT(od->od_object != 0);
2084 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2091 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2097 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2101 for (i = count - 1; i >= 0; i--, od--) {
2108 * No object was found.
2110 if (od->od_object == 0)
2113 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2115 lr->lr_doid = od->od_dir;
2117 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2118 ASSERT3U(error, ==, ENOSPC);
2123 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2130 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2136 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2138 lr->lr_foid = object;
2139 lr->lr_offset = offset;
2140 lr->lr_length = size;
2142 BP_ZERO(&lr->lr_blkptr);
2144 bcopy(data, lr + 1, size);
2146 error = ztest_replay_write(zd, lr, B_FALSE);
2148 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2154 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2159 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2161 lr->lr_foid = object;
2162 lr->lr_offset = offset;
2163 lr->lr_length = size;
2165 error = ztest_replay_truncate(zd, lr, B_FALSE);
2167 ztest_lr_free(lr, sizeof (*lr), NULL);
2173 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2178 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2180 lr->lr_foid = object;
2184 error = ztest_replay_setattr(zd, lr, B_FALSE);
2186 ztest_lr_free(lr, sizeof (*lr), NULL);
2192 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2194 objset_t *os = zd->zd_os;
2199 txg_wait_synced(dmu_objset_pool(os), 0);
2201 ztest_object_lock(zd, object, RL_READER);
2202 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2204 tx = dmu_tx_create(os);
2206 dmu_tx_hold_write(tx, object, offset, size);
2208 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2211 dmu_prealloc(os, object, offset, size, tx);
2213 txg_wait_synced(dmu_objset_pool(os), txg);
2215 (void) dmu_free_long_range(os, object, offset, size);
2218 ztest_range_unlock(rl);
2219 ztest_object_unlock(zd, object);
2223 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2226 ztest_block_tag_t wbt;
2227 dmu_object_info_t doi;
2228 enum ztest_io_type io_type;
2232 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2233 blocksize = doi.doi_data_block_size;
2234 data = umem_alloc(blocksize, UMEM_NOFAIL);
2237 * Pick an i/o type at random, biased toward writing block tags.
2239 io_type = ztest_random(ZTEST_IO_TYPES);
2240 if (ztest_random(2) == 0)
2241 io_type = ZTEST_IO_WRITE_TAG;
2243 (void) rw_rdlock(&zd->zd_zilog_lock);
2247 case ZTEST_IO_WRITE_TAG:
2248 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2249 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2252 case ZTEST_IO_WRITE_PATTERN:
2253 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2254 if (ztest_random(2) == 0) {
2256 * Induce fletcher2 collisions to ensure that
2257 * zio_ddt_collision() detects and resolves them
2258 * when using fletcher2-verify for deduplication.
2260 ((uint64_t *)data)[0] ^= 1ULL << 63;
2261 ((uint64_t *)data)[4] ^= 1ULL << 63;
2263 (void) ztest_write(zd, object, offset, blocksize, data);
2266 case ZTEST_IO_WRITE_ZEROES:
2267 bzero(data, blocksize);
2268 (void) ztest_write(zd, object, offset, blocksize, data);
2271 case ZTEST_IO_TRUNCATE:
2272 (void) ztest_truncate(zd, object, offset, blocksize);
2275 case ZTEST_IO_SETATTR:
2276 (void) ztest_setattr(zd, object);
2281 case ZTEST_IO_REWRITE:
2282 (void) rw_rdlock(&ztest_name_lock);
2283 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2284 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2286 VERIFY(err == 0 || err == ENOSPC);
2287 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2288 ZFS_PROP_COMPRESSION,
2289 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2291 VERIFY(err == 0 || err == ENOSPC);
2292 (void) rw_unlock(&ztest_name_lock);
2294 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2295 DMU_READ_NO_PREFETCH));
2297 (void) ztest_write(zd, object, offset, blocksize, data);
2301 (void) rw_unlock(&zd->zd_zilog_lock);
2303 umem_free(data, blocksize);
2307 * Initialize an object description template.
2310 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2311 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2313 od->od_dir = ZTEST_DIROBJ;
2316 od->od_crtype = type;
2317 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2320 od->od_type = DMU_OT_NONE;
2321 od->od_blocksize = 0;
2324 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2325 tag, (longlong_t)id, (u_longlong_t)index);
2329 * Lookup or create the objects for a test using the od template.
2330 * If the objects do not all exist, or if 'remove' is specified,
2331 * remove any existing objects and create new ones. Otherwise,
2332 * use the existing objects.
2335 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2337 int count = size / sizeof (*od);
2340 mutex_enter(&zd->zd_dirobj_lock);
2341 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2342 (ztest_remove(zd, od, count) != 0 ||
2343 ztest_create(zd, od, count) != 0))
2346 mutex_exit(&zd->zd_dirobj_lock);
2353 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2355 zilog_t *zilog = zd->zd_zilog;
2357 (void) rw_rdlock(&zd->zd_zilog_lock);
2359 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2362 * Remember the committed values in zd, which is in parent/child
2363 * shared memory. If we die, the next iteration of ztest_run()
2364 * will verify that the log really does contain this record.
2366 mutex_enter(&zilog->zl_lock);
2367 ASSERT(zd->zd_shared != NULL);
2368 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2369 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2370 mutex_exit(&zilog->zl_lock);
2372 (void) rw_unlock(&zd->zd_zilog_lock);
2376 * This function is designed to simulate the operations that occur during a
2377 * mount/unmount operation. We hold the dataset across these operations in an
2378 * attempt to expose any implicit assumptions about ZIL management.
2382 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2384 objset_t *os = zd->zd_os;
2387 * We grab the zd_dirobj_lock to ensure that no other thread is
2388 * updating the zil (i.e. adding in-memory log records) and the
2389 * zd_zilog_lock to block any I/O.
2391 mutex_enter(&zd->zd_dirobj_lock);
2392 (void) rw_wrlock(&zd->zd_zilog_lock);
2394 /* zfs_sb_teardown() */
2395 zil_close(zd->zd_zilog);
2397 /* zfsvfs_setup() */
2398 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2399 zil_replay(os, zd, ztest_replay_vector);
2401 (void) rw_unlock(&zd->zd_zilog_lock);
2402 mutex_exit(&zd->zd_dirobj_lock);
2406 * Verify that we can't destroy an active pool, create an existing pool,
2407 * or create a pool with a bad vdev spec.
2411 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2413 ztest_shared_opts_t *zo = &ztest_opts;
2418 * Attempt to create using a bad file.
2420 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2421 VERIFY3U(ENOENT, ==,
2422 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2423 nvlist_free(nvroot);
2426 * Attempt to create using a bad mirror.
2428 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2429 VERIFY3U(ENOENT, ==,
2430 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2431 nvlist_free(nvroot);
2434 * Attempt to create an existing pool. It shouldn't matter
2435 * what's in the nvroot; we should fail with EEXIST.
2437 (void) rw_rdlock(&ztest_name_lock);
2438 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2439 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2440 nvlist_free(nvroot);
2441 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2442 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2443 spa_close(spa, FTAG);
2445 (void) rw_unlock(&ztest_name_lock);
2450 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2453 uint64_t initial_version = SPA_VERSION_INITIAL;
2454 uint64_t version, newversion;
2455 nvlist_t *nvroot, *props;
2458 mutex_enter(&ztest_vdev_lock);
2459 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2462 * Clean up from previous runs.
2464 (void) spa_destroy(name);
2466 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2467 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2470 * If we're configuring a RAIDZ device then make sure that the
2471 * the initial version is capable of supporting that feature.
2473 switch (ztest_opts.zo_raidz_parity) {
2476 initial_version = SPA_VERSION_INITIAL;
2479 initial_version = SPA_VERSION_RAIDZ2;
2482 initial_version = SPA_VERSION_RAIDZ3;
2487 * Create a pool with a spa version that can be upgraded. Pick
2488 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2491 version = ztest_random_spa_version(initial_version);
2492 } while (version > SPA_VERSION_BEFORE_FEATURES);
2494 props = fnvlist_alloc();
2495 fnvlist_add_uint64(props,
2496 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2497 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2498 fnvlist_free(nvroot);
2499 fnvlist_free(props);
2501 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2502 VERIFY3U(spa_version(spa), ==, version);
2503 newversion = ztest_random_spa_version(version + 1);
2505 if (ztest_opts.zo_verbose >= 4) {
2506 (void) printf("upgrading spa version from %llu to %llu\n",
2507 (u_longlong_t)version, (u_longlong_t)newversion);
2510 spa_upgrade(spa, newversion);
2511 VERIFY3U(spa_version(spa), >, version);
2512 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2513 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2514 spa_close(spa, FTAG);
2517 mutex_exit(&ztest_vdev_lock);
2521 vdev_lookup_by_path(vdev_t *vd, const char *path)
2526 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2529 for (c = 0; c < vd->vdev_children; c++)
2530 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2538 * Find the first available hole which can be used as a top-level.
2541 find_vdev_hole(spa_t *spa)
2543 vdev_t *rvd = spa->spa_root_vdev;
2546 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2548 for (c = 0; c < rvd->vdev_children; c++) {
2549 vdev_t *cvd = rvd->vdev_child[c];
2551 if (cvd->vdev_ishole)
2558 * Verify that vdev_add() works as expected.
2562 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2564 ztest_shared_t *zs = ztest_shared;
2565 spa_t *spa = ztest_spa;
2571 mutex_enter(&ztest_vdev_lock);
2572 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2574 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2576 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2579 * If we have slogs then remove them 1/4 of the time.
2581 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2583 * Grab the guid from the head of the log class rotor.
2585 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2587 spa_config_exit(spa, SCL_VDEV, FTAG);
2590 * We have to grab the zs_name_lock as writer to
2591 * prevent a race between removing a slog (dmu_objset_find)
2592 * and destroying a dataset. Removing the slog will
2593 * grab a reference on the dataset which may cause
2594 * dsl_destroy_head() to fail with EBUSY thus
2595 * leaving the dataset in an inconsistent state.
2597 rw_wrlock(&ztest_name_lock);
2598 error = spa_vdev_remove(spa, guid, B_FALSE);
2599 rw_unlock(&ztest_name_lock);
2601 if (error && error != EEXIST)
2602 fatal(0, "spa_vdev_remove() = %d", error);
2604 spa_config_exit(spa, SCL_VDEV, FTAG);
2607 * Make 1/4 of the devices be log devices.
2609 nvroot = make_vdev_root(NULL, NULL, NULL,
2610 ztest_opts.zo_vdev_size, 0,
2611 ztest_random(4) == 0, ztest_opts.zo_raidz,
2614 error = spa_vdev_add(spa, nvroot);
2615 nvlist_free(nvroot);
2617 if (error == ENOSPC)
2618 ztest_record_enospc("spa_vdev_add");
2619 else if (error != 0)
2620 fatal(0, "spa_vdev_add() = %d", error);
2623 mutex_exit(&ztest_vdev_lock);
2627 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2631 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2633 ztest_shared_t *zs = ztest_shared;
2634 spa_t *spa = ztest_spa;
2635 vdev_t *rvd = spa->spa_root_vdev;
2636 spa_aux_vdev_t *sav;
2642 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2644 if (ztest_random(2) == 0) {
2645 sav = &spa->spa_spares;
2646 aux = ZPOOL_CONFIG_SPARES;
2648 sav = &spa->spa_l2cache;
2649 aux = ZPOOL_CONFIG_L2CACHE;
2652 mutex_enter(&ztest_vdev_lock);
2654 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2656 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2658 * Pick a random device to remove.
2660 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2663 * Find an unused device we can add.
2665 zs->zs_vdev_aux = 0;
2668 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2669 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2671 for (c = 0; c < sav->sav_count; c++)
2672 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2675 if (c == sav->sav_count &&
2676 vdev_lookup_by_path(rvd, path) == NULL)
2682 spa_config_exit(spa, SCL_VDEV, FTAG);
2688 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2689 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2690 error = spa_vdev_add(spa, nvroot);
2692 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2693 nvlist_free(nvroot);
2696 * Remove an existing device. Sometimes, dirty its
2697 * vdev state first to make sure we handle removal
2698 * of devices that have pending state changes.
2700 if (ztest_random(2) == 0)
2701 (void) vdev_online(spa, guid, 0, NULL);
2703 error = spa_vdev_remove(spa, guid, B_FALSE);
2704 if (error != 0 && error != EBUSY)
2705 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2708 mutex_exit(&ztest_vdev_lock);
2710 umem_free(path, MAXPATHLEN);
2714 * split a pool if it has mirror tlvdevs
2718 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2720 ztest_shared_t *zs = ztest_shared;
2721 spa_t *spa = ztest_spa;
2722 vdev_t *rvd = spa->spa_root_vdev;
2723 nvlist_t *tree, **child, *config, *split, **schild;
2724 uint_t c, children, schildren = 0, lastlogid = 0;
2727 mutex_enter(&ztest_vdev_lock);
2729 /* ensure we have a useable config; mirrors of raidz aren't supported */
2730 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2731 mutex_exit(&ztest_vdev_lock);
2735 /* clean up the old pool, if any */
2736 (void) spa_destroy("splitp");
2738 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2740 /* generate a config from the existing config */
2741 mutex_enter(&spa->spa_props_lock);
2742 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2744 mutex_exit(&spa->spa_props_lock);
2746 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2749 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2750 for (c = 0; c < children; c++) {
2751 vdev_t *tvd = rvd->vdev_child[c];
2755 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2756 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2758 VERIFY(nvlist_add_string(schild[schildren],
2759 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2760 VERIFY(nvlist_add_uint64(schild[schildren],
2761 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2763 lastlogid = schildren;
2768 VERIFY(nvlist_lookup_nvlist_array(child[c],
2769 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2770 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2773 /* OK, create a config that can be used to split */
2774 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2775 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2776 VDEV_TYPE_ROOT) == 0);
2777 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2778 lastlogid != 0 ? lastlogid : schildren) == 0);
2780 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2781 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2783 for (c = 0; c < schildren; c++)
2784 nvlist_free(schild[c]);
2788 spa_config_exit(spa, SCL_VDEV, FTAG);
2790 (void) rw_wrlock(&ztest_name_lock);
2791 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2792 (void) rw_unlock(&ztest_name_lock);
2794 nvlist_free(config);
2797 (void) printf("successful split - results:\n");
2798 mutex_enter(&spa_namespace_lock);
2799 show_pool_stats(spa);
2800 show_pool_stats(spa_lookup("splitp"));
2801 mutex_exit(&spa_namespace_lock);
2805 mutex_exit(&ztest_vdev_lock);
2810 * Verify that we can attach and detach devices.
2814 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2816 ztest_shared_t *zs = ztest_shared;
2817 spa_t *spa = ztest_spa;
2818 spa_aux_vdev_t *sav = &spa->spa_spares;
2819 vdev_t *rvd = spa->spa_root_vdev;
2820 vdev_t *oldvd, *newvd, *pvd;
2824 uint64_t ashift = ztest_get_ashift();
2825 uint64_t oldguid, pguid;
2826 uint64_t oldsize, newsize;
2827 char *oldpath, *newpath;
2829 int oldvd_has_siblings = B_FALSE;
2830 int newvd_is_spare = B_FALSE;
2832 int error, expected_error;
2834 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2835 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2837 mutex_enter(&ztest_vdev_lock);
2838 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2840 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2843 * Decide whether to do an attach or a replace.
2845 replacing = ztest_random(2);
2848 * Pick a random top-level vdev.
2850 top = ztest_random_vdev_top(spa, B_TRUE);
2853 * Pick a random leaf within it.
2855 leaf = ztest_random(leaves);
2860 oldvd = rvd->vdev_child[top];
2861 if (zs->zs_mirrors >= 1) {
2862 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2863 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2864 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2866 if (ztest_opts.zo_raidz > 1) {
2867 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2868 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2869 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2873 * If we're already doing an attach or replace, oldvd may be a
2874 * mirror vdev -- in which case, pick a random child.
2876 while (oldvd->vdev_children != 0) {
2877 oldvd_has_siblings = B_TRUE;
2878 ASSERT(oldvd->vdev_children >= 2);
2879 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2882 oldguid = oldvd->vdev_guid;
2883 oldsize = vdev_get_min_asize(oldvd);
2884 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2885 (void) strcpy(oldpath, oldvd->vdev_path);
2886 pvd = oldvd->vdev_parent;
2887 pguid = pvd->vdev_guid;
2890 * If oldvd has siblings, then half of the time, detach it.
2892 if (oldvd_has_siblings && ztest_random(2) == 0) {
2893 spa_config_exit(spa, SCL_VDEV, FTAG);
2894 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2895 if (error != 0 && error != ENODEV && error != EBUSY &&
2897 fatal(0, "detach (%s) returned %d", oldpath, error);
2902 * For the new vdev, choose with equal probability between the two
2903 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2905 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2906 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2907 newvd_is_spare = B_TRUE;
2908 (void) strcpy(newpath, newvd->vdev_path);
2910 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2911 ztest_opts.zo_dir, ztest_opts.zo_pool,
2912 top * leaves + leaf);
2913 if (ztest_random(2) == 0)
2914 newpath[strlen(newpath) - 1] = 'b';
2915 newvd = vdev_lookup_by_path(rvd, newpath);
2919 newsize = vdev_get_min_asize(newvd);
2922 * Make newsize a little bigger or smaller than oldsize.
2923 * If it's smaller, the attach should fail.
2924 * If it's larger, and we're doing a replace,
2925 * we should get dynamic LUN growth when we're done.
2927 newsize = 10 * oldsize / (9 + ztest_random(3));
2931 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2932 * unless it's a replace; in that case any non-replacing parent is OK.
2934 * If newvd is already part of the pool, it should fail with EBUSY.
2936 * If newvd is too small, it should fail with EOVERFLOW.
2938 if (pvd->vdev_ops != &vdev_mirror_ops &&
2939 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2940 pvd->vdev_ops == &vdev_replacing_ops ||
2941 pvd->vdev_ops == &vdev_spare_ops))
2942 expected_error = ENOTSUP;
2943 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2944 expected_error = ENOTSUP;
2945 else if (newvd == oldvd)
2946 expected_error = replacing ? 0 : EBUSY;
2947 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2948 expected_error = EBUSY;
2949 else if (newsize < oldsize)
2950 expected_error = EOVERFLOW;
2951 else if (ashift > oldvd->vdev_top->vdev_ashift)
2952 expected_error = EDOM;
2956 spa_config_exit(spa, SCL_VDEV, FTAG);
2959 * Build the nvlist describing newpath.
2961 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2962 ashift, 0, 0, 0, 1);
2964 error = spa_vdev_attach(spa, oldguid, root, replacing);
2969 * If our parent was the replacing vdev, but the replace completed,
2970 * then instead of failing with ENOTSUP we may either succeed,
2971 * fail with ENODEV, or fail with EOVERFLOW.
2973 if (expected_error == ENOTSUP &&
2974 (error == 0 || error == ENODEV || error == EOVERFLOW))
2975 expected_error = error;
2978 * If someone grew the LUN, the replacement may be too small.
2980 if (error == EOVERFLOW || error == EBUSY)
2981 expected_error = error;
2983 /* XXX workaround 6690467 */
2984 if (error != expected_error && expected_error != EBUSY) {
2985 fatal(0, "attach (%s %llu, %s %llu, %d) "
2986 "returned %d, expected %d",
2987 oldpath, oldsize, newpath,
2988 newsize, replacing, error, expected_error);
2991 mutex_exit(&ztest_vdev_lock);
2993 umem_free(oldpath, MAXPATHLEN);
2994 umem_free(newpath, MAXPATHLEN);
2998 * Callback function which expands the physical size of the vdev.
3001 grow_vdev(vdev_t *vd, void *arg)
3003 ASSERTV(spa_t *spa = vd->vdev_spa);
3004 size_t *newsize = arg;
3008 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3009 ASSERT(vd->vdev_ops->vdev_op_leaf);
3011 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3014 fsize = lseek(fd, 0, SEEK_END);
3015 VERIFY(ftruncate(fd, *newsize) == 0);
3017 if (ztest_opts.zo_verbose >= 6) {
3018 (void) printf("%s grew from %lu to %lu bytes\n",
3019 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3026 * Callback function which expands a given vdev by calling vdev_online().
3030 online_vdev(vdev_t *vd, void *arg)
3032 spa_t *spa = vd->vdev_spa;
3033 vdev_t *tvd = vd->vdev_top;
3034 uint64_t guid = vd->vdev_guid;
3035 uint64_t generation = spa->spa_config_generation + 1;
3036 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3039 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3040 ASSERT(vd->vdev_ops->vdev_op_leaf);
3042 /* Calling vdev_online will initialize the new metaslabs */
3043 spa_config_exit(spa, SCL_STATE, spa);
3044 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3045 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3048 * If vdev_online returned an error or the underlying vdev_open
3049 * failed then we abort the expand. The only way to know that
3050 * vdev_open fails is by checking the returned newstate.
3052 if (error || newstate != VDEV_STATE_HEALTHY) {
3053 if (ztest_opts.zo_verbose >= 5) {
3054 (void) printf("Unable to expand vdev, state %llu, "
3055 "error %d\n", (u_longlong_t)newstate, error);
3059 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3062 * Since we dropped the lock we need to ensure that we're
3063 * still talking to the original vdev. It's possible this
3064 * vdev may have been detached/replaced while we were
3065 * trying to online it.
3067 if (generation != spa->spa_config_generation) {
3068 if (ztest_opts.zo_verbose >= 5) {
3069 (void) printf("vdev configuration has changed, "
3070 "guid %llu, state %llu, expected gen %llu, "
3073 (u_longlong_t)tvd->vdev_state,
3074 (u_longlong_t)generation,
3075 (u_longlong_t)spa->spa_config_generation);
3083 * Traverse the vdev tree calling the supplied function.
3084 * We continue to walk the tree until we either have walked all
3085 * children or we receive a non-NULL return from the callback.
3086 * If a NULL callback is passed, then we just return back the first
3087 * leaf vdev we encounter.
3090 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3094 if (vd->vdev_ops->vdev_op_leaf) {
3098 return (func(vd, arg));
3101 for (c = 0; c < vd->vdev_children; c++) {
3102 vdev_t *cvd = vd->vdev_child[c];
3103 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3110 * Verify that dynamic LUN growth works as expected.
3114 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3116 spa_t *spa = ztest_spa;
3118 metaslab_class_t *mc;
3119 metaslab_group_t *mg;
3120 size_t psize, newsize;
3122 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3124 mutex_enter(&ztest_vdev_lock);
3125 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3127 top = ztest_random_vdev_top(spa, B_TRUE);
3129 tvd = spa->spa_root_vdev->vdev_child[top];
3132 old_ms_count = tvd->vdev_ms_count;
3133 old_class_space = metaslab_class_get_space(mc);
3136 * Determine the size of the first leaf vdev associated with
3137 * our top-level device.
3139 vd = vdev_walk_tree(tvd, NULL, NULL);
3140 ASSERT3P(vd, !=, NULL);
3141 ASSERT(vd->vdev_ops->vdev_op_leaf);
3143 psize = vd->vdev_psize;
3146 * We only try to expand the vdev if it's healthy, less than 4x its
3147 * original size, and it has a valid psize.
3149 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3150 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3151 spa_config_exit(spa, SCL_STATE, spa);
3152 mutex_exit(&ztest_vdev_lock);
3156 newsize = psize + psize / 8;
3157 ASSERT3U(newsize, >, psize);
3159 if (ztest_opts.zo_verbose >= 6) {
3160 (void) printf("Expanding LUN %s from %lu to %lu\n",
3161 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3165 * Growing the vdev is a two step process:
3166 * 1). expand the physical size (i.e. relabel)
3167 * 2). online the vdev to create the new metaslabs
3169 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3170 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3171 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3172 if (ztest_opts.zo_verbose >= 5) {
3173 (void) printf("Could not expand LUN because "
3174 "the vdev configuration changed.\n");
3176 spa_config_exit(spa, SCL_STATE, spa);
3177 mutex_exit(&ztest_vdev_lock);
3181 spa_config_exit(spa, SCL_STATE, spa);
3184 * Expanding the LUN will update the config asynchronously,
3185 * thus we must wait for the async thread to complete any
3186 * pending tasks before proceeding.
3190 mutex_enter(&spa->spa_async_lock);
3191 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3192 mutex_exit(&spa->spa_async_lock);
3195 txg_wait_synced(spa_get_dsl(spa), 0);
3196 (void) poll(NULL, 0, 100);
3199 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3201 tvd = spa->spa_root_vdev->vdev_child[top];
3202 new_ms_count = tvd->vdev_ms_count;
3203 new_class_space = metaslab_class_get_space(mc);
3205 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3206 if (ztest_opts.zo_verbose >= 5) {
3207 (void) printf("Could not verify LUN expansion due to "
3208 "intervening vdev offline or remove.\n");
3210 spa_config_exit(spa, SCL_STATE, spa);
3211 mutex_exit(&ztest_vdev_lock);
3216 * Make sure we were able to grow the vdev.
3218 if (new_ms_count <= old_ms_count)
3219 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3220 old_ms_count, new_ms_count);
3223 * Make sure we were able to grow the pool.
3225 if (new_class_space <= old_class_space)
3226 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3227 old_class_space, new_class_space);
3229 if (ztest_opts.zo_verbose >= 5) {
3230 char oldnumbuf[6], newnumbuf[6];
3232 nicenum(old_class_space, oldnumbuf);
3233 nicenum(new_class_space, newnumbuf);
3234 (void) printf("%s grew from %s to %s\n",
3235 spa->spa_name, oldnumbuf, newnumbuf);
3238 spa_config_exit(spa, SCL_STATE, spa);
3239 mutex_exit(&ztest_vdev_lock);
3243 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3247 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3250 * Create the objects common to all ztest datasets.
3252 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3253 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3257 ztest_dataset_create(char *dsname)
3259 uint64_t zilset = ztest_random(100);
3260 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3261 ztest_objset_create_cb, NULL);
3263 if (err || zilset < 80)
3266 if (ztest_opts.zo_verbose >= 5)
3267 (void) printf("Setting dataset %s to sync always\n", dsname);
3268 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3269 ZFS_SYNC_ALWAYS, B_FALSE));
3274 ztest_objset_destroy_cb(const char *name, void *arg)
3277 dmu_object_info_t doi;
3281 * Verify that the dataset contains a directory object.
3283 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3284 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3285 if (error != ENOENT) {
3286 /* We could have crashed in the middle of destroying it */
3288 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3289 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3291 dmu_objset_disown(os, FTAG);
3294 * Destroy the dataset.
3296 if (strchr(name, '@') != NULL) {
3297 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3299 VERIFY0(dsl_destroy_head(name));
3305 ztest_snapshot_create(char *osname, uint64_t id)
3307 char snapname[MAXNAMELEN];
3310 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3312 error = dmu_objset_snapshot_one(osname, snapname);
3313 if (error == ENOSPC) {
3314 ztest_record_enospc(FTAG);
3317 if (error != 0 && error != EEXIST) {
3318 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3325 ztest_snapshot_destroy(char *osname, uint64_t id)
3327 char snapname[MAXNAMELEN];
3330 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3333 error = dsl_destroy_snapshot(snapname, B_FALSE);
3334 if (error != 0 && error != ENOENT)
3335 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3341 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3351 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3352 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3354 (void) rw_rdlock(&ztest_name_lock);
3356 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3357 ztest_opts.zo_pool, (u_longlong_t)id);
3360 * If this dataset exists from a previous run, process its replay log
3361 * half of the time. If we don't replay it, then dsl_destroy_head()
3362 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3364 if (ztest_random(2) == 0 &&
3365 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3366 ztest_zd_init(zdtmp, NULL, os);
3367 zil_replay(os, zdtmp, ztest_replay_vector);
3368 ztest_zd_fini(zdtmp);
3369 dmu_objset_disown(os, FTAG);
3373 * There may be an old instance of the dataset we're about to
3374 * create lying around from a previous run. If so, destroy it
3375 * and all of its snapshots.
3377 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3378 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3381 * Verify that the destroyed dataset is no longer in the namespace.
3383 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3387 * Verify that we can create a new dataset.
3389 error = ztest_dataset_create(name);
3391 if (error == ENOSPC) {
3392 ztest_record_enospc(FTAG);
3395 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3398 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3400 ztest_zd_init(zdtmp, NULL, os);
3403 * Open the intent log for it.
3405 zilog = zil_open(os, ztest_get_data);
3408 * Put some objects in there, do a little I/O to them,
3409 * and randomly take a couple of snapshots along the way.
3411 iters = ztest_random(5);
3412 for (i = 0; i < iters; i++) {
3413 ztest_dmu_object_alloc_free(zdtmp, id);
3414 if (ztest_random(iters) == 0)
3415 (void) ztest_snapshot_create(name, i);
3419 * Verify that we cannot create an existing dataset.
3421 VERIFY3U(EEXIST, ==,
3422 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3425 * Verify that we can hold an objset that is also owned.
3427 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3428 dmu_objset_rele(os2, FTAG);
3431 * Verify that we cannot own an objset that is already owned.
3434 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3437 dmu_objset_disown(os, FTAG);
3438 ztest_zd_fini(zdtmp);
3440 (void) rw_unlock(&ztest_name_lock);
3442 umem_free(name, MAXNAMELEN);
3443 umem_free(zdtmp, sizeof (ztest_ds_t));
3447 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3450 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3452 (void) rw_rdlock(&ztest_name_lock);
3453 (void) ztest_snapshot_destroy(zd->zd_name, id);
3454 (void) ztest_snapshot_create(zd->zd_name, id);
3455 (void) rw_unlock(&ztest_name_lock);
3459 * Cleanup non-standard snapshots and clones.
3462 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3471 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3472 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3473 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3474 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3475 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3477 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3478 osname, (u_longlong_t)id);
3479 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3480 osname, (u_longlong_t)id);
3481 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3482 clone1name, (u_longlong_t)id);
3483 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3484 osname, (u_longlong_t)id);
3485 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3486 clone1name, (u_longlong_t)id);
3488 error = dsl_destroy_head(clone2name);
3489 if (error && error != ENOENT)
3490 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3491 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3492 if (error && error != ENOENT)
3493 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3494 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3495 if (error && error != ENOENT)
3496 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3497 error = dsl_destroy_head(clone1name);
3498 if (error && error != ENOENT)
3499 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3500 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3501 if (error && error != ENOENT)
3502 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3504 umem_free(snap1name, MAXNAMELEN);
3505 umem_free(clone1name, MAXNAMELEN);
3506 umem_free(snap2name, MAXNAMELEN);
3507 umem_free(clone2name, MAXNAMELEN);
3508 umem_free(snap3name, MAXNAMELEN);
3512 * Verify dsl_dataset_promote handles EBUSY
3515 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3523 char *osname = zd->zd_name;
3526 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3527 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3528 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3529 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3530 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3532 (void) rw_rdlock(&ztest_name_lock);
3534 ztest_dsl_dataset_cleanup(osname, id);
3536 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3537 osname, (u_longlong_t)id);
3538 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3539 osname, (u_longlong_t)id);
3540 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3541 clone1name, (u_longlong_t)id);
3542 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3543 osname, (u_longlong_t)id);
3544 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3545 clone1name, (u_longlong_t)id);
3547 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3548 if (error && error != EEXIST) {
3549 if (error == ENOSPC) {
3550 ztest_record_enospc(FTAG);
3553 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3556 error = dmu_objset_clone(clone1name, snap1name);
3558 if (error == ENOSPC) {
3559 ztest_record_enospc(FTAG);
3562 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3565 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3566 if (error && error != EEXIST) {
3567 if (error == ENOSPC) {
3568 ztest_record_enospc(FTAG);
3571 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3574 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3575 if (error && error != EEXIST) {
3576 if (error == ENOSPC) {
3577 ztest_record_enospc(FTAG);
3580 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3583 error = dmu_objset_clone(clone2name, snap3name);
3585 if (error == ENOSPC) {
3586 ztest_record_enospc(FTAG);
3589 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3592 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3594 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3595 error = dsl_dataset_promote(clone2name, NULL);
3596 if (error == ENOSPC) {
3597 dmu_objset_disown(os, FTAG);
3598 ztest_record_enospc(FTAG);
3602 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3604 dmu_objset_disown(os, FTAG);
3607 ztest_dsl_dataset_cleanup(osname, id);
3609 (void) rw_unlock(&ztest_name_lock);
3611 umem_free(snap1name, MAXNAMELEN);
3612 umem_free(clone1name, MAXNAMELEN);
3613 umem_free(snap2name, MAXNAMELEN);
3614 umem_free(clone2name, MAXNAMELEN);
3615 umem_free(snap3name, MAXNAMELEN);
3618 #undef OD_ARRAY_SIZE
3619 #define OD_ARRAY_SIZE 4
3622 * Verify that dmu_object_{alloc,free} work as expected.
3625 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3632 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3633 od = umem_alloc(size, UMEM_NOFAIL);
3634 batchsize = OD_ARRAY_SIZE;
3636 for (b = 0; b < batchsize; b++)
3637 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3640 * Destroy the previous batch of objects, create a new batch,
3641 * and do some I/O on the new objects.
3643 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3646 while (ztest_random(4 * batchsize) != 0)
3647 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3648 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3650 umem_free(od, size);
3653 #undef OD_ARRAY_SIZE
3654 #define OD_ARRAY_SIZE 2
3657 * Verify that dmu_{read,write} work as expected.
3660 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3665 objset_t *os = zd->zd_os;
3666 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3667 od = umem_alloc(size, UMEM_NOFAIL);
3669 int i, freeit, error;
3671 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3672 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3673 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3674 uint64_t regions = 997;
3675 uint64_t stride = 123456789ULL;
3676 uint64_t width = 40;
3677 int free_percent = 5;
3680 * This test uses two objects, packobj and bigobj, that are always
3681 * updated together (i.e. in the same tx) so that their contents are
3682 * in sync and can be compared. Their contents relate to each other
3683 * in a simple way: packobj is a dense array of 'bufwad' structures,
3684 * while bigobj is a sparse array of the same bufwads. Specifically,
3685 * for any index n, there are three bufwads that should be identical:
3687 * packobj, at offset n * sizeof (bufwad_t)
3688 * bigobj, at the head of the nth chunk
3689 * bigobj, at the tail of the nth chunk
3691 * The chunk size is arbitrary. It doesn't have to be a power of two,
3692 * and it doesn't have any relation to the object blocksize.
3693 * The only requirement is that it can hold at least two bufwads.
3695 * Normally, we write the bufwad to each of these locations.
3696 * However, free_percent of the time we instead write zeroes to
3697 * packobj and perform a dmu_free_range() on bigobj. By comparing
3698 * bigobj to packobj, we can verify that the DMU is correctly
3699 * tracking which parts of an object are allocated and free,
3700 * and that the contents of the allocated blocks are correct.
3704 * Read the directory info. If it's the first time, set things up.
3706 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3707 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3709 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3710 umem_free(od, size);
3714 bigobj = od[0].od_object;
3715 packobj = od[1].od_object;
3716 chunksize = od[0].od_gen;
3717 ASSERT(chunksize == od[1].od_gen);
3720 * Prefetch a random chunk of the big object.
3721 * Our aim here is to get some async reads in flight
3722 * for blocks that we may free below; the DMU should
3723 * handle this race correctly.
3725 n = ztest_random(regions) * stride + ztest_random(width);
3726 s = 1 + ztest_random(2 * width - 1);
3727 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3730 * Pick a random index and compute the offsets into packobj and bigobj.
3732 n = ztest_random(regions) * stride + ztest_random(width);
3733 s = 1 + ztest_random(width - 1);
3735 packoff = n * sizeof (bufwad_t);
3736 packsize = s * sizeof (bufwad_t);
3738 bigoff = n * chunksize;
3739 bigsize = s * chunksize;
3741 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3742 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3745 * free_percent of the time, free a range of bigobj rather than
3748 freeit = (ztest_random(100) < free_percent);
3751 * Read the current contents of our objects.
3753 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3756 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3761 * Get a tx for the mods to both packobj and bigobj.
3763 tx = dmu_tx_create(os);
3765 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3768 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3770 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3772 /* This accounts for setting the checksum/compression. */
3773 dmu_tx_hold_bonus(tx, bigobj);
3775 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3777 umem_free(packbuf, packsize);
3778 umem_free(bigbuf, bigsize);
3779 umem_free(od, size);
3783 enum zio_checksum cksum;
3785 cksum = (enum zio_checksum)
3786 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3787 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3788 dmu_object_set_checksum(os, bigobj, cksum, tx);
3790 enum zio_compress comp;
3792 comp = (enum zio_compress)
3793 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3794 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3795 dmu_object_set_compress(os, bigobj, comp, tx);
3798 * For each index from n to n + s, verify that the existing bufwad
3799 * in packobj matches the bufwads at the head and tail of the
3800 * corresponding chunk in bigobj. Then update all three bufwads
3801 * with the new values we want to write out.
3803 for (i = 0; i < s; i++) {
3805 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3807 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3809 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3811 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3812 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3814 if (pack->bw_txg > txg)
3815 fatal(0, "future leak: got %llx, open txg is %llx",
3818 if (pack->bw_data != 0 && pack->bw_index != n + i)
3819 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3820 pack->bw_index, n, i);
3822 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3823 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3825 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3826 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3829 bzero(pack, sizeof (bufwad_t));
3831 pack->bw_index = n + i;
3833 pack->bw_data = 1 + ztest_random(-2ULL);
3840 * We've verified all the old bufwads, and made new ones.
3841 * Now write them out.
3843 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3846 if (ztest_opts.zo_verbose >= 7) {
3847 (void) printf("freeing offset %llx size %llx"
3849 (u_longlong_t)bigoff,
3850 (u_longlong_t)bigsize,
3853 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3855 if (ztest_opts.zo_verbose >= 7) {
3856 (void) printf("writing offset %llx size %llx"
3858 (u_longlong_t)bigoff,
3859 (u_longlong_t)bigsize,
3862 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3868 * Sanity check the stuff we just wrote.
3871 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3872 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3874 VERIFY(0 == dmu_read(os, packobj, packoff,
3875 packsize, packcheck, DMU_READ_PREFETCH));
3876 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3877 bigsize, bigcheck, DMU_READ_PREFETCH));
3879 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3880 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3882 umem_free(packcheck, packsize);
3883 umem_free(bigcheck, bigsize);
3886 umem_free(packbuf, packsize);
3887 umem_free(bigbuf, bigsize);
3888 umem_free(od, size);
3892 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3893 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3901 * For each index from n to n + s, verify that the existing bufwad
3902 * in packobj matches the bufwads at the head and tail of the
3903 * corresponding chunk in bigobj. Then update all three bufwads
3904 * with the new values we want to write out.
3906 for (i = 0; i < s; i++) {
3908 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3910 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3912 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3914 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3915 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3917 if (pack->bw_txg > txg)
3918 fatal(0, "future leak: got %llx, open txg is %llx",
3921 if (pack->bw_data != 0 && pack->bw_index != n + i)
3922 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3923 pack->bw_index, n, i);
3925 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3926 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3928 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3929 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3931 pack->bw_index = n + i;
3933 pack->bw_data = 1 + ztest_random(-2ULL);
3940 #undef OD_ARRAY_SIZE
3941 #define OD_ARRAY_SIZE 2
3944 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3946 objset_t *os = zd->zd_os;
3953 bufwad_t *packbuf, *bigbuf;
3954 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3955 uint64_t blocksize = ztest_random_blocksize();
3956 uint64_t chunksize = blocksize;
3957 uint64_t regions = 997;
3958 uint64_t stride = 123456789ULL;
3960 dmu_buf_t *bonus_db;
3961 arc_buf_t **bigbuf_arcbufs;
3962 dmu_object_info_t doi;
3964 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3965 od = umem_alloc(size, UMEM_NOFAIL);
3968 * This test uses two objects, packobj and bigobj, that are always
3969 * updated together (i.e. in the same tx) so that their contents are
3970 * in sync and can be compared. Their contents relate to each other
3971 * in a simple way: packobj is a dense array of 'bufwad' structures,
3972 * while bigobj is a sparse array of the same bufwads. Specifically,
3973 * for any index n, there are three bufwads that should be identical:
3975 * packobj, at offset n * sizeof (bufwad_t)
3976 * bigobj, at the head of the nth chunk
3977 * bigobj, at the tail of the nth chunk
3979 * The chunk size is set equal to bigobj block size so that
3980 * dmu_assign_arcbuf() can be tested for object updates.
3984 * Read the directory info. If it's the first time, set things up.
3986 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3987 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3990 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3991 umem_free(od, size);
3995 bigobj = od[0].od_object;
3996 packobj = od[1].od_object;
3997 blocksize = od[0].od_blocksize;
3998 chunksize = blocksize;
3999 ASSERT(chunksize == od[1].od_gen);
4001 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4002 VERIFY(ISP2(doi.doi_data_block_size));
4003 VERIFY(chunksize == doi.doi_data_block_size);
4004 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4007 * Pick a random index and compute the offsets into packobj and bigobj.
4009 n = ztest_random(regions) * stride + ztest_random(width);
4010 s = 1 + ztest_random(width - 1);
4012 packoff = n * sizeof (bufwad_t);
4013 packsize = s * sizeof (bufwad_t);
4015 bigoff = n * chunksize;
4016 bigsize = s * chunksize;
4018 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4019 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4021 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4023 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4026 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4027 * Iteration 1 test zcopy to already referenced dbufs.
4028 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4029 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4030 * Iteration 4 test zcopy when dbuf is no longer dirty.
4031 * Iteration 5 test zcopy when it can't be done.
4032 * Iteration 6 one more zcopy write.
4034 for (i = 0; i < 7; i++) {
4039 * In iteration 5 (i == 5) use arcbufs
4040 * that don't match bigobj blksz to test
4041 * dmu_assign_arcbuf() when it can't directly
4042 * assign an arcbuf to a dbuf.
4044 for (j = 0; j < s; j++) {
4045 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4047 dmu_request_arcbuf(bonus_db, chunksize);
4049 bigbuf_arcbufs[2 * j] =
4050 dmu_request_arcbuf(bonus_db, chunksize / 2);
4051 bigbuf_arcbufs[2 * j + 1] =
4052 dmu_request_arcbuf(bonus_db, chunksize / 2);
4057 * Get a tx for the mods to both packobj and bigobj.
4059 tx = dmu_tx_create(os);
4061 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4062 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4064 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4066 umem_free(packbuf, packsize);
4067 umem_free(bigbuf, bigsize);
4068 for (j = 0; j < s; j++) {
4070 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4071 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4074 bigbuf_arcbufs[2 * j]);
4076 bigbuf_arcbufs[2 * j + 1]);
4079 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4080 umem_free(od, size);
4081 dmu_buf_rele(bonus_db, FTAG);
4086 * 50% of the time don't read objects in the 1st iteration to
4087 * test dmu_assign_arcbuf() for the case when there're no
4088 * existing dbufs for the specified offsets.
4090 if (i != 0 || ztest_random(2) != 0) {
4091 error = dmu_read(os, packobj, packoff,
4092 packsize, packbuf, DMU_READ_PREFETCH);
4094 error = dmu_read(os, bigobj, bigoff, bigsize,
4095 bigbuf, DMU_READ_PREFETCH);
4098 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4102 * We've verified all the old bufwads, and made new ones.
4103 * Now write them out.
4105 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4106 if (ztest_opts.zo_verbose >= 7) {
4107 (void) printf("writing offset %llx size %llx"
4109 (u_longlong_t)bigoff,
4110 (u_longlong_t)bigsize,
4113 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4115 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4116 bcopy((caddr_t)bigbuf + (off - bigoff),
4117 bigbuf_arcbufs[j]->b_data, chunksize);
4119 bcopy((caddr_t)bigbuf + (off - bigoff),
4120 bigbuf_arcbufs[2 * j]->b_data,
4122 bcopy((caddr_t)bigbuf + (off - bigoff) +
4124 bigbuf_arcbufs[2 * j + 1]->b_data,
4129 VERIFY(dmu_buf_hold(os, bigobj, off,
4130 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4132 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4133 dmu_assign_arcbuf(bonus_db, off,
4134 bigbuf_arcbufs[j], tx);
4136 dmu_assign_arcbuf(bonus_db, off,
4137 bigbuf_arcbufs[2 * j], tx);
4138 dmu_assign_arcbuf(bonus_db,
4139 off + chunksize / 2,
4140 bigbuf_arcbufs[2 * j + 1], tx);
4143 dmu_buf_rele(dbt, FTAG);
4149 * Sanity check the stuff we just wrote.
4152 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4153 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4155 VERIFY(0 == dmu_read(os, packobj, packoff,
4156 packsize, packcheck, DMU_READ_PREFETCH));
4157 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4158 bigsize, bigcheck, DMU_READ_PREFETCH));
4160 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4161 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4163 umem_free(packcheck, packsize);
4164 umem_free(bigcheck, bigsize);
4167 txg_wait_open(dmu_objset_pool(os), 0);
4168 } else if (i == 3) {
4169 txg_wait_synced(dmu_objset_pool(os), 0);
4173 dmu_buf_rele(bonus_db, FTAG);
4174 umem_free(packbuf, packsize);
4175 umem_free(bigbuf, bigsize);
4176 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4177 umem_free(od, size);
4182 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4186 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4187 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4188 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4191 * Have multiple threads write to large offsets in an object
4192 * to verify that parallel writes to an object -- even to the
4193 * same blocks within the object -- doesn't cause any trouble.
4195 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4197 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4200 while (ztest_random(10) != 0)
4201 ztest_io(zd, od->od_object, offset);
4203 umem_free(od, sizeof (ztest_od_t));
4207 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4210 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4211 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4212 uint64_t count = ztest_random(20) + 1;
4213 uint64_t blocksize = ztest_random_blocksize();
4216 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4218 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4220 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4221 !ztest_random(2)) != 0) {
4222 umem_free(od, sizeof (ztest_od_t));
4226 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4227 umem_free(od, sizeof (ztest_od_t));
4231 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4233 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4235 while (ztest_random(count) != 0) {
4236 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4237 if (ztest_write(zd, od->od_object, randoff, blocksize,
4240 while (ztest_random(4) != 0)
4241 ztest_io(zd, od->od_object, randoff);
4244 umem_free(data, blocksize);
4245 umem_free(od, sizeof (ztest_od_t));
4249 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4251 #define ZTEST_ZAP_MIN_INTS 1
4252 #define ZTEST_ZAP_MAX_INTS 4
4253 #define ZTEST_ZAP_MAX_PROPS 1000
4256 ztest_zap(ztest_ds_t *zd, uint64_t id)
4258 objset_t *os = zd->zd_os;
4261 uint64_t txg, last_txg;
4262 uint64_t value[ZTEST_ZAP_MAX_INTS];
4263 uint64_t zl_ints, zl_intsize, prop;
4266 char propname[100], txgname[100];
4268 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4270 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4271 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4273 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4274 !ztest_random(2)) != 0)
4277 object = od->od_object;
4280 * Generate a known hash collision, and verify that
4281 * we can lookup and remove both entries.
4283 tx = dmu_tx_create(os);
4284 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4285 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4288 for (i = 0; i < 2; i++) {
4290 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4293 for (i = 0; i < 2; i++) {
4294 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4295 sizeof (uint64_t), 1, &value[i], tx));
4297 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4298 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4299 ASSERT3U(zl_ints, ==, 1);
4301 for (i = 0; i < 2; i++) {
4302 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4307 * Generate a buch of random entries.
4309 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4311 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4312 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4313 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4314 bzero(value, sizeof (value));
4318 * If these zap entries already exist, validate their contents.
4320 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4322 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4323 ASSERT3U(zl_ints, ==, 1);
4325 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4326 zl_ints, &last_txg) == 0);
4328 VERIFY(zap_length(os, object, propname, &zl_intsize,
4331 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4332 ASSERT3U(zl_ints, ==, ints);
4334 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4335 zl_ints, value) == 0);
4337 for (i = 0; i < ints; i++) {
4338 ASSERT3U(value[i], ==, last_txg + object + i);
4341 ASSERT3U(error, ==, ENOENT);
4345 * Atomically update two entries in our zap object.
4346 * The first is named txg_%llu, and contains the txg
4347 * in which the property was last updated. The second
4348 * is named prop_%llu, and the nth element of its value
4349 * should be txg + object + n.
4351 tx = dmu_tx_create(os);
4352 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4353 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4358 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4360 for (i = 0; i < ints; i++)
4361 value[i] = txg + object + i;
4363 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4365 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4371 * Remove a random pair of entries.
4373 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4374 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4375 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4377 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4379 if (error == ENOENT)
4384 tx = dmu_tx_create(os);
4385 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4386 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4389 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4390 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4393 umem_free(od, sizeof (ztest_od_t));
4397 * Testcase to test the upgrading of a microzap to fatzap.
4400 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4402 objset_t *os = zd->zd_os;
4404 uint64_t object, txg;
4407 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4408 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4410 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4411 !ztest_random(2)) != 0)
4413 object = od->od_object;
4416 * Add entries to this ZAP and make sure it spills over
4417 * and gets upgraded to a fatzap. Also, since we are adding
4418 * 2050 entries we should see ptrtbl growth and leaf-block split.
4420 for (i = 0; i < 2050; i++) {
4421 char name[MAXNAMELEN];
4426 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4427 (u_longlong_t)id, (u_longlong_t)value);
4429 tx = dmu_tx_create(os);
4430 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4431 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4434 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4436 ASSERT(error == 0 || error == EEXIST);
4440 umem_free(od, sizeof (ztest_od_t));
4445 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4447 objset_t *os = zd->zd_os;
4449 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4451 int i, namelen, error;
4452 int micro = ztest_random(2);
4453 char name[20], string_value[20];
4456 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4457 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4459 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4460 umem_free(od, sizeof (ztest_od_t));
4464 object = od->od_object;
4467 * Generate a random name of the form 'xxx.....' where each
4468 * x is a random printable character and the dots are dots.
4469 * There are 94 such characters, and the name length goes from
4470 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4472 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4474 for (i = 0; i < 3; i++)
4475 name[i] = '!' + ztest_random('~' - '!' + 1);
4476 for (; i < namelen - 1; i++)
4480 if ((namelen & 1) || micro) {
4481 wsize = sizeof (txg);
4487 data = string_value;
4491 VERIFY0(zap_count(os, object, &count));
4492 ASSERT(count != -1ULL);
4495 * Select an operation: length, lookup, add, update, remove.
4497 i = ztest_random(5);
4500 tx = dmu_tx_create(os);
4501 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4502 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4505 bcopy(name, string_value, namelen);
4509 bzero(string_value, namelen);
4515 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4517 ASSERT3U(wsize, ==, zl_wsize);
4518 ASSERT3U(wc, ==, zl_wc);
4520 ASSERT3U(error, ==, ENOENT);
4525 error = zap_lookup(os, object, name, wsize, wc, data);
4527 if (data == string_value &&
4528 bcmp(name, data, namelen) != 0)
4529 fatal(0, "name '%s' != val '%s' len %d",
4530 name, data, namelen);
4532 ASSERT3U(error, ==, ENOENT);
4537 error = zap_add(os, object, name, wsize, wc, data, tx);
4538 ASSERT(error == 0 || error == EEXIST);
4542 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4546 error = zap_remove(os, object, name, tx);
4547 ASSERT(error == 0 || error == ENOENT);
4554 umem_free(od, sizeof (ztest_od_t));
4558 * Commit callback data.
4560 typedef struct ztest_cb_data {
4561 list_node_t zcd_node;
4563 int zcd_expected_err;
4564 boolean_t zcd_added;
4565 boolean_t zcd_called;
4569 /* This is the actual commit callback function */
4571 ztest_commit_callback(void *arg, int error)
4573 ztest_cb_data_t *data = arg;
4574 uint64_t synced_txg;
4576 VERIFY(data != NULL);
4577 VERIFY3S(data->zcd_expected_err, ==, error);
4578 VERIFY(!data->zcd_called);
4580 synced_txg = spa_last_synced_txg(data->zcd_spa);
4581 if (data->zcd_txg > synced_txg)
4582 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4583 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4586 data->zcd_called = B_TRUE;
4588 if (error == ECANCELED) {
4589 ASSERT0(data->zcd_txg);
4590 ASSERT(!data->zcd_added);
4593 * The private callback data should be destroyed here, but
4594 * since we are going to check the zcd_called field after
4595 * dmu_tx_abort(), we will destroy it there.
4600 ASSERT(data->zcd_added);
4601 ASSERT3U(data->zcd_txg, !=, 0);
4603 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4605 /* See if this cb was called more quickly */
4606 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4607 zc_min_txg_delay = synced_txg - data->zcd_txg;
4609 /* Remove our callback from the list */
4610 list_remove(&zcl.zcl_callbacks, data);
4612 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4614 umem_free(data, sizeof (ztest_cb_data_t));
4617 /* Allocate and initialize callback data structure */
4618 static ztest_cb_data_t *
4619 ztest_create_cb_data(objset_t *os, uint64_t txg)
4621 ztest_cb_data_t *cb_data;
4623 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4625 cb_data->zcd_txg = txg;
4626 cb_data->zcd_spa = dmu_objset_spa(os);
4627 list_link_init(&cb_data->zcd_node);
4633 * Commit callback test.
4636 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4638 objset_t *os = zd->zd_os;
4641 ztest_cb_data_t *cb_data[3], *tmp_cb;
4642 uint64_t old_txg, txg;
4645 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4646 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4648 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4649 umem_free(od, sizeof (ztest_od_t));
4653 tx = dmu_tx_create(os);
4655 cb_data[0] = ztest_create_cb_data(os, 0);
4656 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4658 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4660 /* Every once in a while, abort the transaction on purpose */
4661 if (ztest_random(100) == 0)
4665 error = dmu_tx_assign(tx, TXG_NOWAIT);
4667 txg = error ? 0 : dmu_tx_get_txg(tx);
4669 cb_data[0]->zcd_txg = txg;
4670 cb_data[1] = ztest_create_cb_data(os, txg);
4671 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4675 * It's not a strict requirement to call the registered
4676 * callbacks from inside dmu_tx_abort(), but that's what
4677 * it's supposed to happen in the current implementation
4678 * so we will check for that.
4680 for (i = 0; i < 2; i++) {
4681 cb_data[i]->zcd_expected_err = ECANCELED;
4682 VERIFY(!cb_data[i]->zcd_called);
4687 for (i = 0; i < 2; i++) {
4688 VERIFY(cb_data[i]->zcd_called);
4689 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4692 umem_free(od, sizeof (ztest_od_t));
4696 cb_data[2] = ztest_create_cb_data(os, txg);
4697 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4700 * Read existing data to make sure there isn't a future leak.
4702 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4703 &old_txg, DMU_READ_PREFETCH));
4706 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4709 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4711 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4714 * Since commit callbacks don't have any ordering requirement and since
4715 * it is theoretically possible for a commit callback to be called
4716 * after an arbitrary amount of time has elapsed since its txg has been
4717 * synced, it is difficult to reliably determine whether a commit
4718 * callback hasn't been called due to high load or due to a flawed
4721 * In practice, we will assume that if after a certain number of txgs a
4722 * commit callback hasn't been called, then most likely there's an
4723 * implementation bug..
4725 tmp_cb = list_head(&zcl.zcl_callbacks);
4726 if (tmp_cb != NULL &&
4727 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4728 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4729 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4733 * Let's find the place to insert our callbacks.
4735 * Even though the list is ordered by txg, it is possible for the
4736 * insertion point to not be the end because our txg may already be
4737 * quiescing at this point and other callbacks in the open txg
4738 * (from other objsets) may have sneaked in.
4740 tmp_cb = list_tail(&zcl.zcl_callbacks);
4741 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4742 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4744 /* Add the 3 callbacks to the list */
4745 for (i = 0; i < 3; i++) {
4747 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4749 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4752 cb_data[i]->zcd_added = B_TRUE;
4753 VERIFY(!cb_data[i]->zcd_called);
4755 tmp_cb = cb_data[i];
4760 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4764 umem_free(od, sizeof (ztest_od_t));
4769 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4771 zfs_prop_t proplist[] = {
4773 ZFS_PROP_COMPRESSION,
4779 (void) rw_rdlock(&ztest_name_lock);
4781 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4782 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4783 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4785 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
4786 ztest_random_blocksize(), (int)ztest_random(2)));
4788 (void) rw_unlock(&ztest_name_lock);
4793 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4795 nvlist_t *props = NULL;
4797 (void) rw_rdlock(&ztest_name_lock);
4799 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4800 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4802 VERIFY0(spa_prop_get(ztest_spa, &props));
4804 if (ztest_opts.zo_verbose >= 6)
4805 dump_nvlist(props, 4);
4809 (void) rw_unlock(&ztest_name_lock);
4813 user_release_one(const char *snapname, const char *holdname)
4815 nvlist_t *snaps, *holds;
4818 snaps = fnvlist_alloc();
4819 holds = fnvlist_alloc();
4820 fnvlist_add_boolean(holds, holdname);
4821 fnvlist_add_nvlist(snaps, snapname, holds);
4822 fnvlist_free(holds);
4823 error = dsl_dataset_user_release(snaps, NULL);
4824 fnvlist_free(snaps);
4829 * Test snapshot hold/release and deferred destroy.
4832 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4835 objset_t *os = zd->zd_os;
4839 char clonename[100];
4841 char osname[MAXNAMELEN];
4844 (void) rw_rdlock(&ztest_name_lock);
4846 dmu_objset_name(os, osname);
4848 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4850 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4851 (void) snprintf(clonename, sizeof (clonename),
4852 "%s/ch1_%llu", osname, (u_longlong_t)id);
4853 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4856 * Clean up from any previous run.
4858 error = dsl_destroy_head(clonename);
4859 if (error != ENOENT)
4861 error = user_release_one(fullname, tag);
4862 if (error != ESRCH && error != ENOENT)
4864 error = dsl_destroy_snapshot(fullname, B_FALSE);
4865 if (error != ENOENT)
4869 * Create snapshot, clone it, mark snap for deferred destroy,
4870 * destroy clone, verify snap was also destroyed.
4872 error = dmu_objset_snapshot_one(osname, snapname);
4874 if (error == ENOSPC) {
4875 ztest_record_enospc("dmu_objset_snapshot");
4878 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4881 error = dmu_objset_clone(clonename, fullname);
4883 if (error == ENOSPC) {
4884 ztest_record_enospc("dmu_objset_clone");
4887 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4890 error = dsl_destroy_snapshot(fullname, B_TRUE);
4892 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4896 error = dsl_destroy_head(clonename);
4898 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4900 error = dmu_objset_hold(fullname, FTAG, &origin);
4901 if (error != ENOENT)
4902 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4905 * Create snapshot, add temporary hold, verify that we can't
4906 * destroy a held snapshot, mark for deferred destroy,
4907 * release hold, verify snapshot was destroyed.
4909 error = dmu_objset_snapshot_one(osname, snapname);
4911 if (error == ENOSPC) {
4912 ztest_record_enospc("dmu_objset_snapshot");
4915 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4918 holds = fnvlist_alloc();
4919 fnvlist_add_string(holds, fullname, tag);
4920 error = dsl_dataset_user_hold(holds, 0, NULL);
4921 fnvlist_free(holds);
4923 if (error == ENOSPC) {
4924 ztest_record_enospc("dsl_dataset_user_hold");
4927 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
4928 fullname, tag, error);
4931 error = dsl_destroy_snapshot(fullname, B_FALSE);
4932 if (error != EBUSY) {
4933 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4937 error = dsl_destroy_snapshot(fullname, B_TRUE);
4939 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4943 error = user_release_one(fullname, tag);
4945 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4947 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4950 (void) rw_unlock(&ztest_name_lock);
4954 * Inject random faults into the on-disk data.
4958 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4960 ztest_shared_t *zs = ztest_shared;
4961 spa_t *spa = ztest_spa;
4965 uint64_t bad = 0x1990c0ffeedecadeull;
4970 int bshift = SPA_OLD_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4976 boolean_t islog = B_FALSE;
4978 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4979 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4981 mutex_enter(&ztest_vdev_lock);
4982 maxfaults = MAXFAULTS();
4983 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4984 mirror_save = zs->zs_mirrors;
4985 mutex_exit(&ztest_vdev_lock);
4987 ASSERT(leaves >= 1);
4990 * Grab the name lock as reader. There are some operations
4991 * which don't like to have their vdevs changed while
4992 * they are in progress (i.e. spa_change_guid). Those
4993 * operations will have grabbed the name lock as writer.
4995 (void) rw_rdlock(&ztest_name_lock);
4998 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5000 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5002 if (ztest_random(2) == 0) {
5004 * Inject errors on a normal data device or slog device.
5006 top = ztest_random_vdev_top(spa, B_TRUE);
5007 leaf = ztest_random(leaves) + zs->zs_splits;
5010 * Generate paths to the first leaf in this top-level vdev,
5011 * and to the random leaf we selected. We'll induce transient
5012 * write failures and random online/offline activity on leaf 0,
5013 * and we'll write random garbage to the randomly chosen leaf.
5015 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5016 ztest_opts.zo_dir, ztest_opts.zo_pool,
5017 top * leaves + zs->zs_splits);
5018 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5019 ztest_opts.zo_dir, ztest_opts.zo_pool,
5020 top * leaves + leaf);
5022 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5023 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5027 * If the top-level vdev needs to be resilvered
5028 * then we only allow faults on the device that is
5031 if (vd0 != NULL && maxfaults != 1 &&
5032 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5033 vd0->vdev_resilver_txg != 0)) {
5035 * Make vd0 explicitly claim to be unreadable,
5036 * or unwriteable, or reach behind its back
5037 * and close the underlying fd. We can do this if
5038 * maxfaults == 0 because we'll fail and reexecute,
5039 * and we can do it if maxfaults >= 2 because we'll
5040 * have enough redundancy. If maxfaults == 1, the
5041 * combination of this with injection of random data
5042 * corruption below exceeds the pool's fault tolerance.
5044 vdev_file_t *vf = vd0->vdev_tsd;
5046 if (vf != NULL && ztest_random(3) == 0) {
5047 (void) close(vf->vf_vnode->v_fd);
5048 vf->vf_vnode->v_fd = -1;
5049 } else if (ztest_random(2) == 0) {
5050 vd0->vdev_cant_read = B_TRUE;
5052 vd0->vdev_cant_write = B_TRUE;
5054 guid0 = vd0->vdev_guid;
5058 * Inject errors on an l2cache device.
5060 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5062 if (sav->sav_count == 0) {
5063 spa_config_exit(spa, SCL_STATE, FTAG);
5064 (void) rw_unlock(&ztest_name_lock);
5067 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5068 guid0 = vd0->vdev_guid;
5069 (void) strcpy(path0, vd0->vdev_path);
5070 (void) strcpy(pathrand, vd0->vdev_path);
5074 maxfaults = INT_MAX; /* no limit on cache devices */
5077 spa_config_exit(spa, SCL_STATE, FTAG);
5078 (void) rw_unlock(&ztest_name_lock);
5081 * If we can tolerate two or more faults, or we're dealing
5082 * with a slog, randomly online/offline vd0.
5084 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5085 if (ztest_random(10) < 6) {
5086 int flags = (ztest_random(2) == 0 ?
5087 ZFS_OFFLINE_TEMPORARY : 0);
5090 * We have to grab the zs_name_lock as writer to
5091 * prevent a race between offlining a slog and
5092 * destroying a dataset. Offlining the slog will
5093 * grab a reference on the dataset which may cause
5094 * dsl_destroy_head() to fail with EBUSY thus
5095 * leaving the dataset in an inconsistent state.
5098 (void) rw_wrlock(&ztest_name_lock);
5100 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5103 (void) rw_unlock(&ztest_name_lock);
5106 * Ideally we would like to be able to randomly
5107 * call vdev_[on|off]line without holding locks
5108 * to force unpredictable failures but the side
5109 * effects of vdev_[on|off]line prevent us from
5110 * doing so. We grab the ztest_vdev_lock here to
5111 * prevent a race between injection testing and
5114 mutex_enter(&ztest_vdev_lock);
5115 (void) vdev_online(spa, guid0, 0, NULL);
5116 mutex_exit(&ztest_vdev_lock);
5124 * We have at least single-fault tolerance, so inject data corruption.
5126 fd = open(pathrand, O_RDWR);
5128 if (fd == -1) /* we hit a gap in the device namespace */
5131 fsize = lseek(fd, 0, SEEK_END);
5133 while (--iters != 0) {
5134 offset = ztest_random(fsize / (leaves << bshift)) *
5135 (leaves << bshift) + (leaf << bshift) +
5136 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5138 if (offset >= fsize)
5141 mutex_enter(&ztest_vdev_lock);
5142 if (mirror_save != zs->zs_mirrors) {
5143 mutex_exit(&ztest_vdev_lock);
5148 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5149 fatal(1, "can't inject bad word at 0x%llx in %s",
5152 mutex_exit(&ztest_vdev_lock);
5154 if (ztest_opts.zo_verbose >= 7)
5155 (void) printf("injected bad word into %s,"
5156 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5161 umem_free(path0, MAXPATHLEN);
5162 umem_free(pathrand, MAXPATHLEN);
5166 * Verify that DDT repair works as expected.
5169 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5171 ztest_shared_t *zs = ztest_shared;
5172 spa_t *spa = ztest_spa;
5173 objset_t *os = zd->zd_os;
5175 uint64_t object, blocksize, txg, pattern, psize;
5176 enum zio_checksum checksum = spa_dedup_checksum(spa);
5181 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5184 blocksize = ztest_random_blocksize();
5185 blocksize = MIN(blocksize, 2048); /* because we write so many */
5187 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5188 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5190 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5191 umem_free(od, sizeof (ztest_od_t));
5196 * Take the name lock as writer to prevent anyone else from changing
5197 * the pool and dataset properies we need to maintain during this test.
5199 (void) rw_wrlock(&ztest_name_lock);
5201 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5203 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5205 (void) rw_unlock(&ztest_name_lock);
5206 umem_free(od, sizeof (ztest_od_t));
5210 object = od[0].od_object;
5211 blocksize = od[0].od_blocksize;
5212 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5214 ASSERT(object != 0);
5216 tx = dmu_tx_create(os);
5217 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5218 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5220 (void) rw_unlock(&ztest_name_lock);
5221 umem_free(od, sizeof (ztest_od_t));
5226 * Write all the copies of our block.
5228 for (i = 0; i < copies; i++) {
5229 uint64_t offset = i * blocksize;
5230 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5231 DMU_READ_NO_PREFETCH);
5233 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5234 os, (long long)object, (long long) offset, error);
5236 ASSERT(db->db_offset == offset);
5237 ASSERT(db->db_size == blocksize);
5238 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5239 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5240 dmu_buf_will_fill(db, tx);
5241 ztest_pattern_set(db->db_data, db->db_size, pattern);
5242 dmu_buf_rele(db, FTAG);
5246 txg_wait_synced(spa_get_dsl(spa), txg);
5249 * Find out what block we got.
5251 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5252 DMU_READ_NO_PREFETCH));
5253 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5254 dmu_buf_rele(db, FTAG);
5257 * Damage the block. Dedup-ditto will save us when we read it later.
5259 psize = BP_GET_PSIZE(&blk);
5260 buf = zio_buf_alloc(psize);
5261 ztest_pattern_set(buf, psize, ~pattern);
5263 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5264 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5265 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5267 zio_buf_free(buf, psize);
5269 (void) rw_unlock(&ztest_name_lock);
5270 umem_free(od, sizeof (ztest_od_t));
5278 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5280 spa_t *spa = ztest_spa;
5282 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5283 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5284 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5288 * Change the guid for the pool.
5292 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5294 spa_t *spa = ztest_spa;
5295 uint64_t orig, load;
5298 orig = spa_guid(spa);
5299 load = spa_load_guid(spa);
5301 (void) rw_wrlock(&ztest_name_lock);
5302 error = spa_change_guid(spa);
5303 (void) rw_unlock(&ztest_name_lock);
5308 if (ztest_opts.zo_verbose >= 4) {
5309 (void) printf("Changed guid old %llu -> %llu\n",
5310 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5313 VERIFY3U(orig, !=, spa_guid(spa));
5314 VERIFY3U(load, ==, spa_load_guid(spa));
5318 * Rename the pool to a different name and then rename it back.
5322 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5324 char *oldname, *newname;
5327 (void) rw_wrlock(&ztest_name_lock);
5329 oldname = ztest_opts.zo_pool;
5330 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5331 (void) strcpy(newname, oldname);
5332 (void) strcat(newname, "_tmp");
5337 VERIFY3U(0, ==, spa_rename(oldname, newname));
5340 * Try to open it under the old name, which shouldn't exist
5342 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5345 * Open it under the new name and make sure it's still the same spa_t.
5347 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5349 ASSERT(spa == ztest_spa);
5350 spa_close(spa, FTAG);
5353 * Rename it back to the original
5355 VERIFY3U(0, ==, spa_rename(newname, oldname));
5358 * Make sure it can still be opened
5360 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5362 ASSERT(spa == ztest_spa);
5363 spa_close(spa, FTAG);
5365 umem_free(newname, strlen(newname) + 1);
5367 (void) rw_unlock(&ztest_name_lock);
5371 * Verify pool integrity by running zdb.
5374 ztest_run_zdb(char *pool)
5382 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5383 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5384 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5386 VERIFY(realpath(getexecname(), bin) != NULL);
5387 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5388 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5389 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5390 strcpy(bin, "/sbin/zdb"); /* Installed */
5392 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5393 strcat(bin, "/zdb/zdb");
5397 "%s -bcc%s%s -d -U %s %s",
5399 ztest_opts.zo_verbose >= 3 ? "s" : "",
5400 ztest_opts.zo_verbose >= 4 ? "v" : "",
5404 if (ztest_opts.zo_verbose >= 5)
5405 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5407 fp = popen(zdb, "r");
5409 while (fgets(zbuf, 1024, fp) != NULL)
5410 if (ztest_opts.zo_verbose >= 3)
5411 (void) printf("%s", zbuf);
5413 status = pclose(fp);
5418 ztest_dump_core = 0;
5419 if (WIFEXITED(status))
5420 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5422 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5424 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5425 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5426 umem_free(zbuf, 1024);
5430 ztest_walk_pool_directory(char *header)
5434 if (ztest_opts.zo_verbose >= 6)
5435 (void) printf("%s\n", header);
5437 mutex_enter(&spa_namespace_lock);
5438 while ((spa = spa_next(spa)) != NULL)
5439 if (ztest_opts.zo_verbose >= 6)
5440 (void) printf("\t%s\n", spa_name(spa));
5441 mutex_exit(&spa_namespace_lock);
5445 ztest_spa_import_export(char *oldname, char *newname)
5447 nvlist_t *config, *newconfig;
5452 if (ztest_opts.zo_verbose >= 4) {
5453 (void) printf("import/export: old = %s, new = %s\n",
5458 * Clean up from previous runs.
5460 (void) spa_destroy(newname);
5463 * Get the pool's configuration and guid.
5465 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5468 * Kick off a scrub to tickle scrub/export races.
5470 if (ztest_random(2) == 0)
5471 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5473 pool_guid = spa_guid(spa);
5474 spa_close(spa, FTAG);
5476 ztest_walk_pool_directory("pools before export");
5481 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5483 ztest_walk_pool_directory("pools after export");
5488 newconfig = spa_tryimport(config);
5489 ASSERT(newconfig != NULL);
5490 nvlist_free(newconfig);
5493 * Import it under the new name.
5495 error = spa_import(newname, config, NULL, 0);
5497 dump_nvlist(config, 0);
5498 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5499 oldname, newname, error);
5502 ztest_walk_pool_directory("pools after import");
5505 * Try to import it again -- should fail with EEXIST.
5507 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5510 * Try to import it under a different name -- should fail with EEXIST.
5512 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5515 * Verify that the pool is no longer visible under the old name.
5517 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5520 * Verify that we can open and close the pool using the new name.
5522 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5523 ASSERT(pool_guid == spa_guid(spa));
5524 spa_close(spa, FTAG);
5526 nvlist_free(config);
5530 ztest_resume(spa_t *spa)
5532 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5533 (void) printf("resuming from suspended state\n");
5534 spa_vdev_state_enter(spa, SCL_NONE);
5535 vdev_clear(spa, NULL);
5536 (void) spa_vdev_state_exit(spa, NULL, 0);
5537 (void) zio_resume(spa);
5541 ztest_resume_thread(void *arg)
5545 while (!ztest_exiting) {
5546 if (spa_suspended(spa))
5548 (void) poll(NULL, 0, 100);
5560 ztest_deadman_alarm(int sig)
5562 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5567 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5569 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5570 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5571 hrtime_t functime = gethrtime();
5574 for (i = 0; i < zi->zi_iters; i++)
5575 zi->zi_func(zd, id);
5577 functime = gethrtime() - functime;
5579 atomic_add_64(&zc->zc_count, 1);
5580 atomic_add_64(&zc->zc_time, functime);
5582 if (ztest_opts.zo_verbose >= 4)
5583 (void) printf("%6.2f sec in %s\n",
5584 (double)functime / NANOSEC, zi->zi_funcname);
5588 ztest_thread(void *arg)
5591 uint64_t id = (uintptr_t)arg;
5592 ztest_shared_t *zs = ztest_shared;
5596 ztest_shared_callstate_t *zc;
5598 while ((now = gethrtime()) < zs->zs_thread_stop) {
5600 * See if it's time to force a crash.
5602 if (now > zs->zs_thread_kill)
5606 * If we're getting ENOSPC with some regularity, stop.
5608 if (zs->zs_enospc_count > 10)
5612 * Pick a random function to execute.
5614 rand = ztest_random(ZTEST_FUNCS);
5615 zi = &ztest_info[rand];
5616 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5617 call_next = zc->zc_next;
5619 if (now >= call_next &&
5620 atomic_cas_64(&zc->zc_next, call_next, call_next +
5621 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5622 ztest_execute(rand, zi, id);
5632 ztest_dataset_name(char *dsname, char *pool, int d)
5634 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5638 ztest_dataset_destroy(int d)
5640 char name[MAXNAMELEN];
5643 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5645 if (ztest_opts.zo_verbose >= 3)
5646 (void) printf("Destroying %s to free up space\n", name);
5649 * Cleanup any non-standard clones and snapshots. In general,
5650 * ztest thread t operates on dataset (t % zopt_datasets),
5651 * so there may be more than one thing to clean up.
5653 for (t = d; t < ztest_opts.zo_threads;
5654 t += ztest_opts.zo_datasets)
5655 ztest_dsl_dataset_cleanup(name, t);
5657 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5658 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5662 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5664 uint64_t usedobjs, dirobjs, scratch;
5667 * ZTEST_DIROBJ is the object directory for the entire dataset.
5668 * Therefore, the number of objects in use should equal the
5669 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5670 * If not, we have an object leak.
5672 * Note that we can only check this in ztest_dataset_open(),
5673 * when the open-context and syncing-context values agree.
5674 * That's because zap_count() returns the open-context value,
5675 * while dmu_objset_space() returns the rootbp fill count.
5677 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5678 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5679 ASSERT3U(dirobjs + 1, ==, usedobjs);
5683 ztest_dataset_open(int d)
5685 ztest_ds_t *zd = &ztest_ds[d];
5686 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5689 char name[MAXNAMELEN];
5692 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5694 (void) rw_rdlock(&ztest_name_lock);
5696 error = ztest_dataset_create(name);
5697 if (error == ENOSPC) {
5698 (void) rw_unlock(&ztest_name_lock);
5699 ztest_record_enospc(FTAG);
5702 ASSERT(error == 0 || error == EEXIST);
5704 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5705 (void) rw_unlock(&ztest_name_lock);
5707 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5709 zilog = zd->zd_zilog;
5711 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5712 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5713 fatal(0, "missing log records: claimed %llu < committed %llu",
5714 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5716 ztest_dataset_dirobj_verify(zd);
5718 zil_replay(os, zd, ztest_replay_vector);
5720 ztest_dataset_dirobj_verify(zd);
5722 if (ztest_opts.zo_verbose >= 6)
5723 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5725 (u_longlong_t)zilog->zl_parse_blk_count,
5726 (u_longlong_t)zilog->zl_parse_lr_count,
5727 (u_longlong_t)zilog->zl_replaying_seq);
5729 zilog = zil_open(os, ztest_get_data);
5731 if (zilog->zl_replaying_seq != 0 &&
5732 zilog->zl_replaying_seq < committed_seq)
5733 fatal(0, "missing log records: replayed %llu < committed %llu",
5734 zilog->zl_replaying_seq, committed_seq);
5740 ztest_dataset_close(int d)
5742 ztest_ds_t *zd = &ztest_ds[d];
5744 zil_close(zd->zd_zilog);
5745 dmu_objset_disown(zd->zd_os, zd);
5751 * Kick off threads to run tests on all datasets in parallel.
5754 ztest_run(ztest_shared_t *zs)
5759 kthread_t *resume_thread;
5764 ztest_exiting = B_FALSE;
5767 * Initialize parent/child shared state.
5769 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5770 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5772 zs->zs_thread_start = gethrtime();
5773 zs->zs_thread_stop =
5774 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5775 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5776 zs->zs_thread_kill = zs->zs_thread_stop;
5777 if (ztest_random(100) < ztest_opts.zo_killrate) {
5778 zs->zs_thread_kill -=
5779 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5782 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5784 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5785 offsetof(ztest_cb_data_t, zcd_node));
5790 kernel_init(FREAD | FWRITE);
5791 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5792 spa->spa_debug = B_TRUE;
5793 metaslab_preload_limit = ztest_random(20) + 1;
5796 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5797 DMU_OST_ANY, B_TRUE, FTAG, &os));
5798 zs->zs_guid = dmu_objset_fsid_guid(os);
5799 dmu_objset_disown(os, FTAG);
5801 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5804 * We don't expect the pool to suspend unless maxfaults == 0,
5805 * in which case ztest_fault_inject() temporarily takes away
5806 * the only valid replica.
5808 if (MAXFAULTS() == 0)
5809 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5811 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5814 * Create a thread to periodically resume suspended I/O.
5816 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5817 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5818 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5822 * Set a deadman alarm to abort() if we hang.
5824 signal(SIGALRM, ztest_deadman_alarm);
5825 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5829 * Verify that we can safely inquire about about any object,
5830 * whether it's allocated or not. To make it interesting,
5831 * we probe a 5-wide window around each power of two.
5832 * This hits all edge cases, including zero and the max.
5834 for (t = 0; t < 64; t++) {
5835 for (d = -5; d <= 5; d++) {
5836 error = dmu_object_info(spa->spa_meta_objset,
5837 (1ULL << t) + d, NULL);
5838 ASSERT(error == 0 || error == ENOENT ||
5844 * If we got any ENOSPC errors on the previous run, destroy something.
5846 if (zs->zs_enospc_count != 0) {
5847 int d = ztest_random(ztest_opts.zo_datasets);
5848 ztest_dataset_destroy(d);
5850 zs->zs_enospc_count = 0;
5852 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5855 if (ztest_opts.zo_verbose >= 4)
5856 (void) printf("starting main threads...\n");
5859 * Kick off all the tests that run in parallel.
5861 for (t = 0; t < ztest_opts.zo_threads; t++) {
5864 if (t < ztest_opts.zo_datasets &&
5865 ztest_dataset_open(t) != 0)
5868 VERIFY3P(thread = zk_thread_create(NULL, 0,
5869 (thread_func_t)ztest_thread,
5870 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5871 PTHREAD_CREATE_JOINABLE), !=, NULL);
5872 tid[t] = thread->t_tid;
5876 * Wait for all of the tests to complete. We go in reverse order
5877 * so we don't close datasets while threads are still using them.
5879 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5880 thread_join(tid[t]);
5881 if (t < ztest_opts.zo_datasets)
5882 ztest_dataset_close(t);
5885 txg_wait_synced(spa_get_dsl(spa), 0);
5887 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5888 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5890 if (ztest_opts.zo_verbose >= 3)
5891 zfs_dbgmsg_print(FTAG);
5893 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5895 /* Kill the resume thread */
5896 ztest_exiting = B_TRUE;
5897 thread_join(resume_thread->t_tid);
5901 * Right before closing the pool, kick off a bunch of async I/O;
5902 * spa_close() should wait for it to complete.
5904 for (object = 1; object < 50; object++)
5905 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5907 /* Verify that at least one commit cb was called in a timely fashion */
5908 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5909 VERIFY0(zc_min_txg_delay);
5911 spa_close(spa, FTAG);
5914 * Verify that we can loop over all pools.
5916 mutex_enter(&spa_namespace_lock);
5917 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5918 if (ztest_opts.zo_verbose > 3)
5919 (void) printf("spa_next: found %s\n", spa_name(spa));
5920 mutex_exit(&spa_namespace_lock);
5923 * Verify that we can export the pool and reimport it under a
5926 if (ztest_random(2) == 0) {
5927 char name[MAXNAMELEN];
5928 (void) snprintf(name, MAXNAMELEN, "%s_import",
5929 ztest_opts.zo_pool);
5930 ztest_spa_import_export(ztest_opts.zo_pool, name);
5931 ztest_spa_import_export(name, ztest_opts.zo_pool);
5936 list_destroy(&zcl.zcl_callbacks);
5937 mutex_destroy(&zcl.zcl_callbacks_lock);
5938 (void) rwlock_destroy(&ztest_name_lock);
5939 mutex_destroy(&ztest_vdev_lock);
5945 ztest_ds_t *zd = &ztest_ds[0];
5949 if (ztest_opts.zo_verbose >= 3)
5950 (void) printf("testing spa_freeze()...\n");
5952 kernel_init(FREAD | FWRITE);
5953 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5954 VERIFY3U(0, ==, ztest_dataset_open(0));
5955 spa->spa_debug = B_TRUE;
5959 * Force the first log block to be transactionally allocated.
5960 * We have to do this before we freeze the pool -- otherwise
5961 * the log chain won't be anchored.
5963 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5964 ztest_dmu_object_alloc_free(zd, 0);
5965 zil_commit(zd->zd_zilog, 0);
5968 txg_wait_synced(spa_get_dsl(spa), 0);
5971 * Freeze the pool. This stops spa_sync() from doing anything,
5972 * so that the only way to record changes from now on is the ZIL.
5977 * Because it is hard to predict how much space a write will actually
5978 * require beforehand, we leave ourselves some fudge space to write over
5981 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
5984 * Run tests that generate log records but don't alter the pool config
5985 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5986 * We do a txg_wait_synced() after each iteration to force the txg
5987 * to increase well beyond the last synced value in the uberblock.
5988 * The ZIL should be OK with that.
5990 * Run a random number of times less than zo_maxloops and ensure we do
5991 * not run out of space on the pool.
5993 while (ztest_random(10) != 0 &&
5994 numloops++ < ztest_opts.zo_maxloops &&
5995 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
5997 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
5998 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
5999 ztest_io(zd, od.od_object,
6000 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6001 txg_wait_synced(spa_get_dsl(spa), 0);
6005 * Commit all of the changes we just generated.
6007 zil_commit(zd->zd_zilog, 0);
6008 txg_wait_synced(spa_get_dsl(spa), 0);
6011 * Close our dataset and close the pool.
6013 ztest_dataset_close(0);
6014 spa_close(spa, FTAG);
6018 * Open and close the pool and dataset to induce log replay.
6020 kernel_init(FREAD | FWRITE);
6021 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6022 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6023 VERIFY3U(0, ==, ztest_dataset_open(0));
6024 ztest_dataset_close(0);
6026 spa->spa_debug = B_TRUE;
6028 txg_wait_synced(spa_get_dsl(spa), 0);
6029 ztest_reguid(NULL, 0);
6031 spa_close(spa, FTAG);
6036 print_time(hrtime_t t, char *timebuf)
6038 hrtime_t s = t / NANOSEC;
6039 hrtime_t m = s / 60;
6040 hrtime_t h = m / 60;
6041 hrtime_t d = h / 24;
6050 (void) sprintf(timebuf,
6051 "%llud%02lluh%02llum%02llus", d, h, m, s);
6053 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6055 (void) sprintf(timebuf, "%llum%02llus", m, s);
6057 (void) sprintf(timebuf, "%llus", s);
6061 make_random_props(void)
6065 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6066 if (ztest_random(2) == 0)
6068 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6074 * Create a storage pool with the given name and initial vdev size.
6075 * Then test spa_freeze() functionality.
6078 ztest_init(ztest_shared_t *zs)
6081 nvlist_t *nvroot, *props;
6084 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6085 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6087 kernel_init(FREAD | FWRITE);
6090 * Create the storage pool.
6092 (void) spa_destroy(ztest_opts.zo_pool);
6093 ztest_shared->zs_vdev_next_leaf = 0;
6095 zs->zs_mirrors = ztest_opts.zo_mirrors;
6096 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6097 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6098 props = make_random_props();
6099 for (i = 0; i < SPA_FEATURES; i++) {
6101 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6102 spa_feature_table[i].fi_uname));
6103 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6106 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6107 nvlist_free(nvroot);
6110 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6111 zs->zs_metaslab_sz =
6112 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6113 spa_close(spa, FTAG);
6117 ztest_run_zdb(ztest_opts.zo_pool);
6121 ztest_run_zdb(ztest_opts.zo_pool);
6123 (void) rwlock_destroy(&ztest_name_lock);
6124 mutex_destroy(&ztest_vdev_lock);
6130 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6132 ztest_fd_data = mkstemp(ztest_name_data);
6133 ASSERT3S(ztest_fd_data, >=, 0);
6134 (void) unlink(ztest_name_data);
6138 shared_data_size(ztest_shared_hdr_t *hdr)
6142 size = hdr->zh_hdr_size;
6143 size += hdr->zh_opts_size;
6144 size += hdr->zh_size;
6145 size += hdr->zh_stats_size * hdr->zh_stats_count;
6146 size += hdr->zh_ds_size * hdr->zh_ds_count;
6155 ztest_shared_hdr_t *hdr;
6157 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6158 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6159 VERIFY3P(hdr, !=, MAP_FAILED);
6161 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6163 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6164 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6165 hdr->zh_size = sizeof (ztest_shared_t);
6166 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6167 hdr->zh_stats_count = ZTEST_FUNCS;
6168 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6169 hdr->zh_ds_count = ztest_opts.zo_datasets;
6171 size = shared_data_size(hdr);
6172 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6174 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6181 ztest_shared_hdr_t *hdr;
6184 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6185 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6186 VERIFY3P(hdr, !=, MAP_FAILED);
6188 size = shared_data_size(hdr);
6190 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6191 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6192 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6193 VERIFY3P(hdr, !=, MAP_FAILED);
6194 buf = (uint8_t *)hdr;
6196 offset = hdr->zh_hdr_size;
6197 ztest_shared_opts = (void *)&buf[offset];
6198 offset += hdr->zh_opts_size;
6199 ztest_shared = (void *)&buf[offset];
6200 offset += hdr->zh_size;
6201 ztest_shared_callstate = (void *)&buf[offset];
6202 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6203 ztest_shared_ds = (void *)&buf[offset];
6207 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6211 char *cmdbuf = NULL;
6216 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6217 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6222 fatal(1, "fork failed");
6224 if (pid == 0) { /* child */
6225 char *emptyargv[2] = { cmd, NULL };
6226 char fd_data_str[12];
6228 struct rlimit rl = { 1024, 1024 };
6229 (void) setrlimit(RLIMIT_NOFILE, &rl);
6231 (void) close(ztest_fd_rand);
6232 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6233 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6235 (void) enable_extended_FILE_stdio(-1, -1);
6236 if (libpath != NULL)
6237 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6238 (void) execv(cmd, emptyargv);
6239 ztest_dump_core = B_FALSE;
6240 fatal(B_TRUE, "exec failed: %s", cmd);
6243 if (cmdbuf != NULL) {
6244 umem_free(cmdbuf, MAXPATHLEN);
6248 while (waitpid(pid, &status, 0) != pid)
6250 if (statusp != NULL)
6253 if (WIFEXITED(status)) {
6254 if (WEXITSTATUS(status) != 0) {
6255 (void) fprintf(stderr, "child exited with code %d\n",
6256 WEXITSTATUS(status));
6260 } else if (WIFSIGNALED(status)) {
6261 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6262 (void) fprintf(stderr, "child died with signal %d\n",
6268 (void) fprintf(stderr, "something strange happened to child\n");
6275 ztest_run_init(void)
6279 ztest_shared_t *zs = ztest_shared;
6281 ASSERT(ztest_opts.zo_init != 0);
6284 * Blow away any existing copy of zpool.cache
6286 (void) remove(spa_config_path);
6289 * Create and initialize our storage pool.
6291 for (i = 1; i <= ztest_opts.zo_init; i++) {
6292 bzero(zs, sizeof (ztest_shared_t));
6293 if (ztest_opts.zo_verbose >= 3 &&
6294 ztest_opts.zo_init != 1) {
6295 (void) printf("ztest_init(), pass %d\n", i);
6302 main(int argc, char **argv)
6310 ztest_shared_callstate_t *zc;
6317 char *fd_data_str = getenv("ZTEST_FD_DATA");
6318 struct sigaction action;
6320 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6322 dprintf_setup(&argc, argv);
6324 action.sa_handler = sig_handler;
6325 sigemptyset(&action.sa_mask);
6326 action.sa_flags = 0;
6328 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6329 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6334 if (sigaction(SIGABRT, &action, NULL) < 0) {
6335 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6340 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6341 ASSERT3S(ztest_fd_rand, >=, 0);
6344 process_options(argc, argv);
6349 bcopy(&ztest_opts, ztest_shared_opts,
6350 sizeof (*ztest_shared_opts));
6352 ztest_fd_data = atoi(fd_data_str);
6354 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6356 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6358 /* Override location of zpool.cache */
6359 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6360 ztest_opts.zo_dir) != -1);
6362 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6367 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6368 metaslab_df_alloc_threshold =
6369 zs->zs_metaslab_df_alloc_threshold;
6378 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6380 if (ztest_opts.zo_verbose >= 1) {
6381 (void) printf("%llu vdevs, %d datasets, %d threads,"
6382 " %llu seconds...\n",
6383 (u_longlong_t)ztest_opts.zo_vdevs,
6384 ztest_opts.zo_datasets,
6385 ztest_opts.zo_threads,
6386 (u_longlong_t)ztest_opts.zo_time);
6389 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6390 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6392 zs->zs_do_init = B_TRUE;
6393 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6394 if (ztest_opts.zo_verbose >= 1) {
6395 (void) printf("Executing older ztest for "
6396 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6398 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6399 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6401 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6403 zs->zs_do_init = B_FALSE;
6405 zs->zs_proc_start = gethrtime();
6406 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6408 for (f = 0; f < ZTEST_FUNCS; f++) {
6409 zi = &ztest_info[f];
6410 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6411 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6412 zc->zc_next = UINT64_MAX;
6414 zc->zc_next = zs->zs_proc_start +
6415 ztest_random(2 * zi->zi_interval[0] + 1);
6419 * Run the tests in a loop. These tests include fault injection
6420 * to verify that self-healing data works, and forced crashes
6421 * to verify that we never lose on-disk consistency.
6423 while (gethrtime() < zs->zs_proc_stop) {
6428 * Initialize the workload counters for each function.
6430 for (f = 0; f < ZTEST_FUNCS; f++) {
6431 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6436 /* Set the allocation switch size */
6437 zs->zs_metaslab_df_alloc_threshold =
6438 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6440 if (!hasalt || ztest_random(2) == 0) {
6441 if (hasalt && ztest_opts.zo_verbose >= 1) {
6442 (void) printf("Executing newer ztest: %s\n",
6446 killed = exec_child(cmd, NULL, B_TRUE, &status);
6448 if (hasalt && ztest_opts.zo_verbose >= 1) {
6449 (void) printf("Executing older ztest: %s\n",
6450 ztest_opts.zo_alt_ztest);
6453 killed = exec_child(ztest_opts.zo_alt_ztest,
6454 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6461 if (ztest_opts.zo_verbose >= 1) {
6462 hrtime_t now = gethrtime();
6464 now = MIN(now, zs->zs_proc_stop);
6465 print_time(zs->zs_proc_stop - now, timebuf);
6466 nicenum(zs->zs_space, numbuf);
6468 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6469 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6471 WIFEXITED(status) ? "Complete" : "SIGKILL",
6472 (u_longlong_t)zs->zs_enospc_count,
6473 100.0 * zs->zs_alloc / zs->zs_space,
6475 100.0 * (now - zs->zs_proc_start) /
6476 (ztest_opts.zo_time * NANOSEC), timebuf);
6479 if (ztest_opts.zo_verbose >= 2) {
6480 (void) printf("\nWorkload summary:\n\n");
6481 (void) printf("%7s %9s %s\n",
6482 "Calls", "Time", "Function");
6483 (void) printf("%7s %9s %s\n",
6484 "-----", "----", "--------");
6485 for (f = 0; f < ZTEST_FUNCS; f++) {
6486 zi = &ztest_info[f];
6487 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6488 print_time(zc->zc_time, timebuf);
6489 (void) printf("%7llu %9s %s\n",
6490 (u_longlong_t)zc->zc_count, timebuf,
6493 (void) printf("\n");
6497 * It's possible that we killed a child during a rename test,
6498 * in which case we'll have a 'ztest_tmp' pool lying around
6499 * instead of 'ztest'. Do a blind rename in case this happened.
6502 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6503 spa_close(spa, FTAG);
6505 char tmpname[MAXNAMELEN];
6507 kernel_init(FREAD | FWRITE);
6508 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6509 ztest_opts.zo_pool);
6510 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6514 ztest_run_zdb(ztest_opts.zo_pool);
6517 if (ztest_opts.zo_verbose >= 1) {
6519 (void) printf("%d runs of older ztest: %s\n", older,
6520 ztest_opts.zo_alt_ztest);
6521 (void) printf("%d runs of newer ztest: %s\n", newer,
6524 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6525 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6528 umem_free(cmd, MAXNAMELEN);