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, 2014 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>
125 #include <sys/fs/zfs.h>
126 #include <libnvpair.h>
128 #include <execinfo.h> /* for backtrace() */
131 static int ztest_fd_data = -1;
132 static int ztest_fd_rand = -1;
134 typedef struct ztest_shared_hdr {
135 uint64_t zh_hdr_size;
136 uint64_t zh_opts_size;
138 uint64_t zh_stats_size;
139 uint64_t zh_stats_count;
141 uint64_t zh_ds_count;
142 } ztest_shared_hdr_t;
144 static ztest_shared_hdr_t *ztest_shared_hdr;
146 typedef struct ztest_shared_opts {
147 char zo_pool[MAXNAMELEN];
148 char zo_dir[MAXNAMELEN];
149 char zo_alt_ztest[MAXNAMELEN];
150 char zo_alt_libpath[MAXNAMELEN];
152 uint64_t zo_vdevtime;
160 uint64_t zo_passtime;
161 uint64_t zo_killrate;
165 uint64_t zo_maxloops;
166 uint64_t zo_metaslab_gang_bang;
167 } ztest_shared_opts_t;
169 static const ztest_shared_opts_t ztest_opts_defaults = {
170 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
171 .zo_dir = { '/', 't', 'm', 'p', '\0' },
172 .zo_alt_ztest = { '\0' },
173 .zo_alt_libpath = { '\0' },
175 .zo_ashift = SPA_MINBLOCKSHIFT,
178 .zo_raidz_parity = 1,
179 .zo_vdev_size = SPA_MINDEVSIZE,
182 .zo_passtime = 60, /* 60 seconds */
183 .zo_killrate = 70, /* 70% kill rate */
186 .zo_time = 300, /* 5 minutes */
187 .zo_maxloops = 50, /* max loops during spa_freeze() */
188 .zo_metaslab_gang_bang = 32 << 10
191 extern uint64_t metaslab_gang_bang;
192 extern uint64_t metaslab_df_alloc_threshold;
193 extern int metaslab_preload_limit;
195 static ztest_shared_opts_t *ztest_shared_opts;
196 static ztest_shared_opts_t ztest_opts;
198 typedef struct ztest_shared_ds {
202 static ztest_shared_ds_t *ztest_shared_ds;
203 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
205 #define BT_MAGIC 0x123456789abcdefULL
206 #define MAXFAULTS() \
207 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
211 ZTEST_IO_WRITE_PATTERN,
212 ZTEST_IO_WRITE_ZEROES,
219 typedef struct ztest_block_tag {
229 typedef struct bufwad {
236 * XXX -- fix zfs range locks to be generic so we can use them here.
258 #define ZTEST_RANGE_LOCKS 64
259 #define ZTEST_OBJECT_LOCKS 64
262 * Object descriptor. Used as a template for object lookup/create/remove.
264 typedef struct ztest_od {
267 dmu_object_type_t od_type;
268 dmu_object_type_t od_crtype;
269 uint64_t od_blocksize;
270 uint64_t od_crblocksize;
273 char od_name[MAXNAMELEN];
279 typedef struct ztest_ds {
280 ztest_shared_ds_t *zd_shared;
282 rwlock_t zd_zilog_lock;
284 ztest_od_t *zd_od; /* debugging aid */
285 char zd_name[MAXNAMELEN];
286 kmutex_t zd_dirobj_lock;
287 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
288 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
292 * Per-iteration state.
294 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
296 typedef struct ztest_info {
297 ztest_func_t *zi_func; /* test function */
298 uint64_t zi_iters; /* iterations per execution */
299 uint64_t *zi_interval; /* execute every <interval> seconds */
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])
312 * Note: these aren't static because we want dladdr() to work.
314 ztest_func_t ztest_dmu_read_write;
315 ztest_func_t ztest_dmu_write_parallel;
316 ztest_func_t ztest_dmu_object_alloc_free;
317 ztest_func_t ztest_dmu_commit_callbacks;
318 ztest_func_t ztest_zap;
319 ztest_func_t ztest_zap_parallel;
320 ztest_func_t ztest_zil_commit;
321 ztest_func_t ztest_zil_remount;
322 ztest_func_t ztest_dmu_read_write_zcopy;
323 ztest_func_t ztest_dmu_objset_create_destroy;
324 ztest_func_t ztest_dmu_prealloc;
325 ztest_func_t ztest_fzap;
326 ztest_func_t ztest_dmu_snapshot_create_destroy;
327 ztest_func_t ztest_dsl_prop_get_set;
328 ztest_func_t ztest_spa_prop_get_set;
329 ztest_func_t ztest_spa_create_destroy;
330 ztest_func_t ztest_fault_inject;
331 ztest_func_t ztest_ddt_repair;
332 ztest_func_t ztest_dmu_snapshot_hold;
333 ztest_func_t ztest_spa_rename;
334 ztest_func_t ztest_scrub;
335 ztest_func_t ztest_dsl_dataset_promote_busy;
336 ztest_func_t ztest_vdev_attach_detach;
337 ztest_func_t ztest_vdev_LUN_growth;
338 ztest_func_t ztest_vdev_add_remove;
339 ztest_func_t ztest_vdev_aux_add_remove;
340 ztest_func_t ztest_split_pool;
341 ztest_func_t ztest_reguid;
342 ztest_func_t ztest_spa_upgrade;
344 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
345 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
346 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
347 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
348 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
350 ztest_info_t ztest_info[] = {
351 { ztest_dmu_read_write, 1, &zopt_always },
352 { ztest_dmu_write_parallel, 10, &zopt_always },
353 { ztest_dmu_object_alloc_free, 1, &zopt_always },
354 { ztest_dmu_commit_callbacks, 1, &zopt_always },
355 { ztest_zap, 30, &zopt_always },
356 { ztest_zap_parallel, 100, &zopt_always },
357 { ztest_split_pool, 1, &zopt_always },
358 { ztest_zil_commit, 1, &zopt_incessant },
359 { ztest_zil_remount, 1, &zopt_sometimes },
360 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
361 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
362 { ztest_dsl_prop_get_set, 1, &zopt_often },
363 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
365 { ztest_dmu_prealloc, 1, &zopt_sometimes },
367 { ztest_fzap, 1, &zopt_sometimes },
368 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
369 { ztest_spa_create_destroy, 1, &zopt_sometimes },
370 { ztest_fault_inject, 1, &zopt_sometimes },
371 { ztest_ddt_repair, 1, &zopt_sometimes },
372 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
373 { ztest_reguid, 1, &zopt_rarely },
374 { ztest_spa_rename, 1, &zopt_rarely },
375 { ztest_scrub, 1, &zopt_rarely },
376 { ztest_spa_upgrade, 1, &zopt_rarely },
377 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
378 { ztest_vdev_attach_detach, 1, &zopt_sometimes },
379 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
380 { ztest_vdev_add_remove, 1,
381 &ztest_opts.zo_vdevtime },
382 { ztest_vdev_aux_add_remove, 1,
383 &ztest_opts.zo_vdevtime },
386 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
389 * The following struct is used to hold a list of uncalled commit callbacks.
390 * The callbacks are ordered by txg number.
392 typedef struct ztest_cb_list {
393 kmutex_t zcl_callbacks_lock;
394 list_t zcl_callbacks;
398 * Stuff we need to share writably between parent and child.
400 typedef struct ztest_shared {
401 boolean_t zs_do_init;
402 hrtime_t zs_proc_start;
403 hrtime_t zs_proc_stop;
404 hrtime_t zs_thread_start;
405 hrtime_t zs_thread_stop;
406 hrtime_t zs_thread_kill;
407 uint64_t zs_enospc_count;
408 uint64_t zs_vdev_next_leaf;
409 uint64_t zs_vdev_aux;
414 uint64_t zs_metaslab_sz;
415 uint64_t zs_metaslab_df_alloc_threshold;
419 #define ID_PARALLEL -1ULL
421 static char ztest_dev_template[] = "%s/%s.%llua";
422 static char ztest_aux_template[] = "%s/%s.%s.%llu";
423 ztest_shared_t *ztest_shared;
425 static spa_t *ztest_spa = NULL;
426 static ztest_ds_t *ztest_ds;
428 static kmutex_t ztest_vdev_lock;
431 * The ztest_name_lock protects the pool and dataset namespace used by
432 * the individual tests. To modify the namespace, consumers must grab
433 * this lock as writer. Grabbing the lock as reader will ensure that the
434 * namespace does not change while the lock is held.
436 static rwlock_t ztest_name_lock;
438 static boolean_t ztest_dump_core = B_TRUE;
439 static boolean_t ztest_exiting;
441 /* Global commit callback list */
442 static ztest_cb_list_t zcl;
443 /* Commit cb delay */
444 static uint64_t zc_min_txg_delay = UINT64_MAX;
445 static int zc_cb_counter = 0;
448 * Minimum number of commit callbacks that need to be registered for us to check
449 * whether the minimum txg delay is acceptable.
451 #define ZTEST_COMMIT_CB_MIN_REG 100
454 * If a number of txgs equal to this threshold have been created after a commit
455 * callback has been registered but not called, then we assume there is an
456 * implementation bug.
458 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
460 extern uint64_t metaslab_gang_bang;
461 extern uint64_t metaslab_df_alloc_threshold;
464 ZTEST_META_DNODE = 0,
469 static void usage(boolean_t) __NORETURN;
472 * These libumem hooks provide a reasonable set of defaults for the allocator's
473 * debugging facilities.
476 _umem_debug_init(void)
478 return ("default,verbose"); /* $UMEM_DEBUG setting */
482 _umem_logging_init(void)
484 return ("fail,contents"); /* $UMEM_LOGGING setting */
487 #define BACKTRACE_SZ 100
489 static void sig_handler(int signo)
491 struct sigaction action;
492 #ifdef __GNUC__ /* backtrace() is a GNU extension */
494 void *buffer[BACKTRACE_SZ];
496 nptrs = backtrace(buffer, BACKTRACE_SZ);
497 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
501 * Restore default action and re-raise signal so SIGSEGV and
502 * SIGABRT can trigger a core dump.
504 action.sa_handler = SIG_DFL;
505 sigemptyset(&action.sa_mask);
507 (void) sigaction(signo, &action, NULL);
511 #define FATAL_MSG_SZ 1024
516 fatal(int do_perror, char *message, ...)
519 int save_errno = errno;
522 (void) fflush(stdout);
523 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
525 va_start(args, message);
526 (void) sprintf(buf, "ztest: ");
528 (void) vsprintf(buf + strlen(buf), message, args);
531 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
532 ": %s", strerror(save_errno));
534 (void) fprintf(stderr, "%s\n", buf);
535 fatal_msg = buf; /* to ease debugging */
542 str2shift(const char *buf)
544 const char *ends = "BKMGTPEZ";
549 for (i = 0; i < strlen(ends); i++) {
550 if (toupper(buf[0]) == ends[i])
553 if (i == strlen(ends)) {
554 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
558 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
561 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
567 nicenumtoull(const char *buf)
572 val = strtoull(buf, &end, 0);
574 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
576 } else if (end[0] == '.') {
577 double fval = strtod(buf, &end);
578 fval *= pow(2, str2shift(end));
579 if (fval > UINT64_MAX) {
580 (void) fprintf(stderr, "ztest: value too large: %s\n",
584 val = (uint64_t)fval;
586 int shift = str2shift(end);
587 if (shift >= 64 || (val << shift) >> shift != val) {
588 (void) fprintf(stderr, "ztest: value too large: %s\n",
598 usage(boolean_t requested)
600 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
602 char nice_vdev_size[10];
603 char nice_gang_bang[10];
604 FILE *fp = requested ? stdout : stderr;
606 nicenum(zo->zo_vdev_size, nice_vdev_size);
607 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
609 (void) fprintf(fp, "Usage: %s\n"
610 "\t[-v vdevs (default: %llu)]\n"
611 "\t[-s size_of_each_vdev (default: %s)]\n"
612 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
613 "\t[-m mirror_copies (default: %d)]\n"
614 "\t[-r raidz_disks (default: %d)]\n"
615 "\t[-R raidz_parity (default: %d)]\n"
616 "\t[-d datasets (default: %d)]\n"
617 "\t[-t threads (default: %d)]\n"
618 "\t[-g gang_block_threshold (default: %s)]\n"
619 "\t[-i init_count (default: %d)] initialize pool i times\n"
620 "\t[-k kill_percentage (default: %llu%%)]\n"
621 "\t[-p pool_name (default: %s)]\n"
622 "\t[-f dir (default: %s)] file directory for vdev files\n"
623 "\t[-V] verbose (use multiple times for ever more blather)\n"
624 "\t[-E] use existing pool instead of creating new one\n"
625 "\t[-T time (default: %llu sec)] total run time\n"
626 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
627 "\t[-P passtime (default: %llu sec)] time per pass\n"
628 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
629 "\t[-h] (print help)\n"
632 (u_longlong_t)zo->zo_vdevs, /* -v */
633 nice_vdev_size, /* -s */
634 zo->zo_ashift, /* -a */
635 zo->zo_mirrors, /* -m */
636 zo->zo_raidz, /* -r */
637 zo->zo_raidz_parity, /* -R */
638 zo->zo_datasets, /* -d */
639 zo->zo_threads, /* -t */
640 nice_gang_bang, /* -g */
641 zo->zo_init, /* -i */
642 (u_longlong_t)zo->zo_killrate, /* -k */
643 zo->zo_pool, /* -p */
645 (u_longlong_t)zo->zo_time, /* -T */
646 (u_longlong_t)zo->zo_maxloops, /* -F */
647 (u_longlong_t)zo->zo_passtime);
648 exit(requested ? 0 : 1);
652 process_options(int argc, char **argv)
655 ztest_shared_opts_t *zo = &ztest_opts;
659 char altdir[MAXNAMELEN] = { 0 };
661 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
663 while ((opt = getopt(argc, argv,
664 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
681 value = nicenumtoull(optarg);
685 zo->zo_vdevs = value;
688 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
691 zo->zo_ashift = value;
694 zo->zo_mirrors = value;
697 zo->zo_raidz = MAX(1, value);
700 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
703 zo->zo_datasets = MAX(1, value);
706 zo->zo_threads = MAX(1, value);
709 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
716 zo->zo_killrate = value;
719 (void) strlcpy(zo->zo_pool, optarg,
720 sizeof (zo->zo_pool));
723 path = realpath(optarg, NULL);
725 (void) fprintf(stderr, "error: %s: %s\n",
726 optarg, strerror(errno));
729 (void) strlcpy(zo->zo_dir, path,
730 sizeof (zo->zo_dir));
743 zo->zo_passtime = MAX(1, value);
746 zo->zo_maxloops = MAX(1, value);
749 (void) strlcpy(altdir, optarg, sizeof (altdir));
761 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
764 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
767 if (strlen(altdir) > 0) {
775 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
776 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
778 VERIFY(NULL != realpath(getexecname(), cmd));
779 if (0 != access(altdir, F_OK)) {
780 ztest_dump_core = B_FALSE;
781 fatal(B_TRUE, "invalid alternate ztest path: %s",
784 VERIFY(NULL != realpath(altdir, realaltdir));
787 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
788 * We want to extract <isa> to determine if we should use
789 * 32 or 64 bit binaries.
791 bin = strstr(cmd, "/usr/bin/");
792 ztest = strstr(bin, "/ztest");
794 isalen = ztest - isa;
795 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
796 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
797 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
798 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
800 if (0 != access(zo->zo_alt_ztest, X_OK)) {
801 ztest_dump_core = B_FALSE;
802 fatal(B_TRUE, "invalid alternate ztest: %s",
804 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
805 ztest_dump_core = B_FALSE;
806 fatal(B_TRUE, "invalid alternate lib directory %s",
810 umem_free(cmd, MAXPATHLEN);
811 umem_free(realaltdir, MAXPATHLEN);
816 ztest_kill(ztest_shared_t *zs)
818 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
819 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
822 * Before we kill off ztest, make sure that the config is updated.
823 * See comment above spa_config_sync().
825 mutex_enter(&spa_namespace_lock);
826 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
827 mutex_exit(&spa_namespace_lock);
829 if (ztest_opts.zo_verbose >= 3)
830 zfs_dbgmsg_print(FTAG);
832 (void) kill(getpid(), SIGKILL);
836 ztest_random(uint64_t range)
840 ASSERT3S(ztest_fd_rand, >=, 0);
845 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
846 fatal(1, "short read from /dev/urandom");
853 ztest_record_enospc(const char *s)
855 ztest_shared->zs_enospc_count++;
859 ztest_get_ashift(void)
861 if (ztest_opts.zo_ashift == 0)
862 return (SPA_MINBLOCKSHIFT + ztest_random(3));
863 return (ztest_opts.zo_ashift);
867 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
873 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
876 ashift = ztest_get_ashift();
882 vdev = ztest_shared->zs_vdev_aux;
883 (void) snprintf(path, MAXPATHLEN,
884 ztest_aux_template, ztest_opts.zo_dir,
885 pool == NULL ? ztest_opts.zo_pool : pool,
888 vdev = ztest_shared->zs_vdev_next_leaf++;
889 (void) snprintf(path, MAXPATHLEN,
890 ztest_dev_template, ztest_opts.zo_dir,
891 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
896 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
898 fatal(1, "can't open %s", path);
899 if (ftruncate(fd, size) != 0)
900 fatal(1, "can't ftruncate %s", path);
904 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
905 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
906 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
907 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
908 umem_free(pathbuf, MAXPATHLEN);
914 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
915 uint64_t ashift, int r)
917 nvlist_t *raidz, **child;
921 return (make_vdev_file(path, aux, pool, size, ashift));
922 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
924 for (c = 0; c < r; c++)
925 child[c] = make_vdev_file(path, aux, pool, size, ashift);
927 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
928 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
929 VDEV_TYPE_RAIDZ) == 0);
930 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
931 ztest_opts.zo_raidz_parity) == 0);
932 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
935 for (c = 0; c < r; c++)
936 nvlist_free(child[c]);
938 umem_free(child, r * sizeof (nvlist_t *));
944 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
945 uint64_t ashift, int r, int m)
947 nvlist_t *mirror, **child;
951 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
953 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
955 for (c = 0; c < m; c++)
956 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
958 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
959 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
960 VDEV_TYPE_MIRROR) == 0);
961 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
964 for (c = 0; c < m; c++)
965 nvlist_free(child[c]);
967 umem_free(child, m * sizeof (nvlist_t *));
973 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
974 int log, int r, int m, int t)
976 nvlist_t *root, **child;
981 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
983 for (c = 0; c < t; c++) {
984 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
986 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
990 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
991 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
992 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
995 for (c = 0; c < t; c++)
996 nvlist_free(child[c]);
998 umem_free(child, t * sizeof (nvlist_t *));
1004 * Find a random spa version. Returns back a random spa version in the
1005 * range [initial_version, SPA_VERSION_FEATURES].
1008 ztest_random_spa_version(uint64_t initial_version)
1010 uint64_t version = initial_version;
1012 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1014 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1017 if (version > SPA_VERSION_BEFORE_FEATURES)
1018 version = SPA_VERSION_FEATURES;
1020 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1025 ztest_random_blocksize(void)
1027 return (1 << (SPA_MINBLOCKSHIFT +
1028 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
1032 ztest_random_ibshift(void)
1034 return (DN_MIN_INDBLKSHIFT +
1035 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1039 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1042 vdev_t *rvd = spa->spa_root_vdev;
1045 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1048 top = ztest_random(rvd->vdev_children);
1049 tvd = rvd->vdev_child[top];
1050 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1051 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1057 ztest_random_dsl_prop(zfs_prop_t prop)
1062 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1063 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1069 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1072 const char *propname = zfs_prop_to_name(prop);
1073 const char *valname;
1078 error = dsl_prop_set_int(osname, propname,
1079 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1081 if (error == ENOSPC) {
1082 ztest_record_enospc(FTAG);
1087 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1088 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1090 if (ztest_opts.zo_verbose >= 6) {
1091 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1092 (void) printf("%s %s = %s at '%s'\n",
1093 osname, propname, valname, setpoint);
1095 umem_free(setpoint, MAXPATHLEN);
1101 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1103 spa_t *spa = ztest_spa;
1104 nvlist_t *props = NULL;
1107 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1108 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1110 error = spa_prop_set(spa, props);
1114 if (error == ENOSPC) {
1115 ztest_record_enospc(FTAG);
1124 ztest_rll_init(rll_t *rll)
1126 rll->rll_writer = NULL;
1127 rll->rll_readers = 0;
1128 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1129 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1133 ztest_rll_destroy(rll_t *rll)
1135 ASSERT(rll->rll_writer == NULL);
1136 ASSERT(rll->rll_readers == 0);
1137 mutex_destroy(&rll->rll_lock);
1138 cv_destroy(&rll->rll_cv);
1142 ztest_rll_lock(rll_t *rll, rl_type_t type)
1144 mutex_enter(&rll->rll_lock);
1146 if (type == RL_READER) {
1147 while (rll->rll_writer != NULL)
1148 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1151 while (rll->rll_writer != NULL || rll->rll_readers)
1152 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1153 rll->rll_writer = curthread;
1156 mutex_exit(&rll->rll_lock);
1160 ztest_rll_unlock(rll_t *rll)
1162 mutex_enter(&rll->rll_lock);
1164 if (rll->rll_writer) {
1165 ASSERT(rll->rll_readers == 0);
1166 rll->rll_writer = NULL;
1168 ASSERT(rll->rll_readers != 0);
1169 ASSERT(rll->rll_writer == NULL);
1173 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1174 cv_broadcast(&rll->rll_cv);
1176 mutex_exit(&rll->rll_lock);
1180 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1182 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1184 ztest_rll_lock(rll, type);
1188 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1190 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1192 ztest_rll_unlock(rll);
1196 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1197 uint64_t size, rl_type_t type)
1199 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1200 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1203 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1204 rl->rl_object = object;
1205 rl->rl_offset = offset;
1209 ztest_rll_lock(rll, type);
1215 ztest_range_unlock(rl_t *rl)
1217 rll_t *rll = rl->rl_lock;
1219 ztest_rll_unlock(rll);
1221 umem_free(rl, sizeof (*rl));
1225 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1228 zd->zd_zilog = dmu_objset_zil(os);
1229 zd->zd_shared = szd;
1230 dmu_objset_name(os, zd->zd_name);
1233 if (zd->zd_shared != NULL)
1234 zd->zd_shared->zd_seq = 0;
1236 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1237 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1239 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1240 ztest_rll_init(&zd->zd_object_lock[l]);
1242 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1243 ztest_rll_init(&zd->zd_range_lock[l]);
1247 ztest_zd_fini(ztest_ds_t *zd)
1251 mutex_destroy(&zd->zd_dirobj_lock);
1252 (void) rwlock_destroy(&zd->zd_zilog_lock);
1254 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1255 ztest_rll_destroy(&zd->zd_object_lock[l]);
1257 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1258 ztest_rll_destroy(&zd->zd_range_lock[l]);
1261 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1264 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1270 * Attempt to assign tx to some transaction group.
1272 error = dmu_tx_assign(tx, txg_how);
1274 if (error == ERESTART) {
1275 ASSERT(txg_how == TXG_NOWAIT);
1278 ASSERT3U(error, ==, ENOSPC);
1279 ztest_record_enospc(tag);
1284 txg = dmu_tx_get_txg(tx);
1290 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1293 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1301 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1304 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1308 diff |= (value - *ip++);
1315 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1316 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1318 bt->bt_magic = BT_MAGIC;
1319 bt->bt_objset = dmu_objset_id(os);
1320 bt->bt_object = object;
1321 bt->bt_offset = offset;
1324 bt->bt_crtxg = crtxg;
1328 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1329 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1331 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1332 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1333 ASSERT3U(bt->bt_object, ==, object);
1334 ASSERT3U(bt->bt_offset, ==, offset);
1335 ASSERT3U(bt->bt_gen, <=, gen);
1336 ASSERT3U(bt->bt_txg, <=, txg);
1337 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1340 static ztest_block_tag_t *
1341 ztest_bt_bonus(dmu_buf_t *db)
1343 dmu_object_info_t doi;
1344 ztest_block_tag_t *bt;
1346 dmu_object_info_from_db(db, &doi);
1347 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1348 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1349 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1358 #define lrz_type lr_mode
1359 #define lrz_blocksize lr_uid
1360 #define lrz_ibshift lr_gid
1361 #define lrz_bonustype lr_rdev
1362 #define lrz_bonuslen lr_crtime[1]
1365 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1367 char *name = (void *)(lr + 1); /* name follows lr */
1368 size_t namesize = strlen(name) + 1;
1371 if (zil_replaying(zd->zd_zilog, tx))
1374 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1375 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1376 sizeof (*lr) + namesize - sizeof (lr_t));
1378 zil_itx_assign(zd->zd_zilog, itx, tx);
1382 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1384 char *name = (void *)(lr + 1); /* name follows lr */
1385 size_t namesize = strlen(name) + 1;
1388 if (zil_replaying(zd->zd_zilog, tx))
1391 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1392 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1393 sizeof (*lr) + namesize - sizeof (lr_t));
1395 itx->itx_oid = object;
1396 zil_itx_assign(zd->zd_zilog, itx, tx);
1400 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1403 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1405 if (zil_replaying(zd->zd_zilog, tx))
1408 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1409 write_state = WR_INDIRECT;
1411 itx = zil_itx_create(TX_WRITE,
1412 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1414 if (write_state == WR_COPIED &&
1415 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1416 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1417 zil_itx_destroy(itx);
1418 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1419 write_state = WR_NEED_COPY;
1421 itx->itx_private = zd;
1422 itx->itx_wr_state = write_state;
1423 itx->itx_sync = (ztest_random(8) == 0);
1424 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1426 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1427 sizeof (*lr) - sizeof (lr_t));
1429 zil_itx_assign(zd->zd_zilog, itx, tx);
1433 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1437 if (zil_replaying(zd->zd_zilog, tx))
1440 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1441 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1442 sizeof (*lr) - sizeof (lr_t));
1444 itx->itx_sync = B_FALSE;
1445 zil_itx_assign(zd->zd_zilog, itx, tx);
1449 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1453 if (zil_replaying(zd->zd_zilog, tx))
1456 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1457 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1458 sizeof (*lr) - sizeof (lr_t));
1460 itx->itx_sync = B_FALSE;
1461 zil_itx_assign(zd->zd_zilog, itx, tx);
1468 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1470 char *name = (void *)(lr + 1); /* name follows lr */
1471 objset_t *os = zd->zd_os;
1472 ztest_block_tag_t *bbt;
1479 byteswap_uint64_array(lr, sizeof (*lr));
1481 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1482 ASSERT(name[0] != '\0');
1484 tx = dmu_tx_create(os);
1486 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1488 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1489 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1491 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1494 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1498 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1500 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1501 if (lr->lr_foid == 0) {
1502 lr->lr_foid = zap_create(os,
1503 lr->lrz_type, lr->lrz_bonustype,
1504 lr->lrz_bonuslen, tx);
1506 error = zap_create_claim(os, lr->lr_foid,
1507 lr->lrz_type, lr->lrz_bonustype,
1508 lr->lrz_bonuslen, tx);
1511 if (lr->lr_foid == 0) {
1512 lr->lr_foid = dmu_object_alloc(os,
1513 lr->lrz_type, 0, lr->lrz_bonustype,
1514 lr->lrz_bonuslen, tx);
1516 error = dmu_object_claim(os, lr->lr_foid,
1517 lr->lrz_type, 0, lr->lrz_bonustype,
1518 lr->lrz_bonuslen, tx);
1523 ASSERT3U(error, ==, EEXIST);
1524 ASSERT(zd->zd_zilog->zl_replay);
1529 ASSERT(lr->lr_foid != 0);
1531 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1532 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1533 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1535 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1536 bbt = ztest_bt_bonus(db);
1537 dmu_buf_will_dirty(db, tx);
1538 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1539 dmu_buf_rele(db, FTAG);
1541 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1544 (void) ztest_log_create(zd, tx, lr);
1552 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1554 char *name = (void *)(lr + 1); /* name follows lr */
1555 objset_t *os = zd->zd_os;
1556 dmu_object_info_t doi;
1558 uint64_t object, txg;
1561 byteswap_uint64_array(lr, sizeof (*lr));
1563 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1564 ASSERT(name[0] != '\0');
1567 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1568 ASSERT(object != 0);
1570 ztest_object_lock(zd, object, RL_WRITER);
1572 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1574 tx = dmu_tx_create(os);
1576 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1577 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1579 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1581 ztest_object_unlock(zd, object);
1585 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1586 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1588 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1591 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1593 (void) ztest_log_remove(zd, tx, lr, object);
1597 ztest_object_unlock(zd, object);
1603 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1605 objset_t *os = zd->zd_os;
1606 void *data = lr + 1; /* data follows lr */
1607 uint64_t offset, length;
1608 ztest_block_tag_t *bt = data;
1609 ztest_block_tag_t *bbt;
1610 uint64_t gen, txg, lrtxg, crtxg;
1611 dmu_object_info_t doi;
1614 arc_buf_t *abuf = NULL;
1618 byteswap_uint64_array(lr, sizeof (*lr));
1620 offset = lr->lr_offset;
1621 length = lr->lr_length;
1623 /* If it's a dmu_sync() block, write the whole block */
1624 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1625 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1626 if (length < blocksize) {
1627 offset -= offset % blocksize;
1632 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1633 byteswap_uint64_array(bt, sizeof (*bt));
1635 if (bt->bt_magic != BT_MAGIC)
1638 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1639 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1641 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1643 dmu_object_info_from_db(db, &doi);
1645 bbt = ztest_bt_bonus(db);
1646 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1648 crtxg = bbt->bt_crtxg;
1649 lrtxg = lr->lr_common.lrc_txg;
1651 tx = dmu_tx_create(os);
1653 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1655 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1656 P2PHASE(offset, length) == 0)
1657 abuf = dmu_request_arcbuf(db, length);
1659 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1662 dmu_return_arcbuf(abuf);
1663 dmu_buf_rele(db, FTAG);
1664 ztest_range_unlock(rl);
1665 ztest_object_unlock(zd, lr->lr_foid);
1671 * Usually, verify the old data before writing new data --
1672 * but not always, because we also want to verify correct
1673 * behavior when the data was not recently read into cache.
1675 ASSERT(offset % doi.doi_data_block_size == 0);
1676 if (ztest_random(4) != 0) {
1677 int prefetch = ztest_random(2) ?
1678 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1679 ztest_block_tag_t rbt;
1681 VERIFY(dmu_read(os, lr->lr_foid, offset,
1682 sizeof (rbt), &rbt, prefetch) == 0);
1683 if (rbt.bt_magic == BT_MAGIC) {
1684 ztest_bt_verify(&rbt, os, lr->lr_foid,
1685 offset, gen, txg, crtxg);
1690 * Writes can appear to be newer than the bonus buffer because
1691 * the ztest_get_data() callback does a dmu_read() of the
1692 * open-context data, which may be different than the data
1693 * as it was when the write was generated.
1695 if (zd->zd_zilog->zl_replay) {
1696 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1697 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1702 * Set the bt's gen/txg to the bonus buffer's gen/txg
1703 * so that all of the usual ASSERTs will work.
1705 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1709 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1711 bcopy(data, abuf->b_data, length);
1712 dmu_assign_arcbuf(db, offset, abuf, tx);
1715 (void) ztest_log_write(zd, tx, lr);
1717 dmu_buf_rele(db, FTAG);
1721 ztest_range_unlock(rl);
1722 ztest_object_unlock(zd, lr->lr_foid);
1728 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1730 objset_t *os = zd->zd_os;
1736 byteswap_uint64_array(lr, sizeof (*lr));
1738 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1739 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1742 tx = dmu_tx_create(os);
1744 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1746 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1748 ztest_range_unlock(rl);
1749 ztest_object_unlock(zd, lr->lr_foid);
1753 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1754 lr->lr_length, tx) == 0);
1756 (void) ztest_log_truncate(zd, tx, lr);
1760 ztest_range_unlock(rl);
1761 ztest_object_unlock(zd, lr->lr_foid);
1767 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1769 objset_t *os = zd->zd_os;
1772 ztest_block_tag_t *bbt;
1773 uint64_t txg, lrtxg, crtxg;
1776 byteswap_uint64_array(lr, sizeof (*lr));
1778 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1780 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1782 tx = dmu_tx_create(os);
1783 dmu_tx_hold_bonus(tx, lr->lr_foid);
1785 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1787 dmu_buf_rele(db, FTAG);
1788 ztest_object_unlock(zd, lr->lr_foid);
1792 bbt = ztest_bt_bonus(db);
1793 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1794 crtxg = bbt->bt_crtxg;
1795 lrtxg = lr->lr_common.lrc_txg;
1797 if (zd->zd_zilog->zl_replay) {
1798 ASSERT(lr->lr_size != 0);
1799 ASSERT(lr->lr_mode != 0);
1803 * Randomly change the size and increment the generation.
1805 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1807 lr->lr_mode = bbt->bt_gen + 1;
1812 * Verify that the current bonus buffer is not newer than our txg.
1814 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1815 MAX(txg, lrtxg), crtxg);
1817 dmu_buf_will_dirty(db, tx);
1819 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1820 ASSERT3U(lr->lr_size, <=, db->db_size);
1821 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1822 bbt = ztest_bt_bonus(db);
1824 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1826 dmu_buf_rele(db, FTAG);
1828 (void) ztest_log_setattr(zd, tx, lr);
1832 ztest_object_unlock(zd, lr->lr_foid);
1837 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1838 NULL, /* 0 no such transaction type */
1839 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1840 NULL, /* TX_MKDIR */
1841 NULL, /* TX_MKXATTR */
1842 NULL, /* TX_SYMLINK */
1843 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1844 NULL, /* TX_RMDIR */
1846 NULL, /* TX_RENAME */
1847 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1848 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1849 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1851 NULL, /* TX_CREATE_ACL */
1852 NULL, /* TX_CREATE_ATTR */
1853 NULL, /* TX_CREATE_ACL_ATTR */
1854 NULL, /* TX_MKDIR_ACL */
1855 NULL, /* TX_MKDIR_ATTR */
1856 NULL, /* TX_MKDIR_ACL_ATTR */
1857 NULL, /* TX_WRITE2 */
1861 * ZIL get_data callbacks
1865 ztest_get_done(zgd_t *zgd, int error)
1867 ztest_ds_t *zd = zgd->zgd_private;
1868 uint64_t object = zgd->zgd_rl->rl_object;
1871 dmu_buf_rele(zgd->zgd_db, zgd);
1873 ztest_range_unlock(zgd->zgd_rl);
1874 ztest_object_unlock(zd, object);
1876 if (error == 0 && zgd->zgd_bp)
1877 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1879 umem_free(zgd, sizeof (*zgd));
1883 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1885 ztest_ds_t *zd = arg;
1886 objset_t *os = zd->zd_os;
1887 uint64_t object = lr->lr_foid;
1888 uint64_t offset = lr->lr_offset;
1889 uint64_t size = lr->lr_length;
1890 blkptr_t *bp = &lr->lr_blkptr;
1891 uint64_t txg = lr->lr_common.lrc_txg;
1893 dmu_object_info_t doi;
1898 ztest_object_lock(zd, object, RL_READER);
1899 error = dmu_bonus_hold(os, object, FTAG, &db);
1901 ztest_object_unlock(zd, object);
1905 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1907 if (crtxg == 0 || crtxg > txg) {
1908 dmu_buf_rele(db, FTAG);
1909 ztest_object_unlock(zd, object);
1913 dmu_object_info_from_db(db, &doi);
1914 dmu_buf_rele(db, FTAG);
1917 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1918 zgd->zgd_zilog = zd->zd_zilog;
1919 zgd->zgd_private = zd;
1921 if (buf != NULL) { /* immediate write */
1922 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1925 error = dmu_read(os, object, offset, size, buf,
1926 DMU_READ_NO_PREFETCH);
1929 size = doi.doi_data_block_size;
1931 offset = P2ALIGN(offset, size);
1933 ASSERT(offset < size);
1937 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1940 error = dmu_buf_hold(os, object, offset, zgd, &db,
1941 DMU_READ_NO_PREFETCH);
1944 blkptr_t *obp = dmu_buf_get_blkptr(db);
1946 ASSERT(BP_IS_HOLE(bp));
1953 ASSERT(db->db_offset == offset);
1954 ASSERT(db->db_size == size);
1956 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1957 ztest_get_done, zgd);
1964 ztest_get_done(zgd, error);
1970 ztest_lr_alloc(size_t lrsize, char *name)
1973 size_t namesize = name ? strlen(name) + 1 : 0;
1975 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1978 bcopy(name, lr + lrsize, namesize);
1984 ztest_lr_free(void *lr, size_t lrsize, char *name)
1986 size_t namesize = name ? strlen(name) + 1 : 0;
1988 umem_free(lr, lrsize + namesize);
1992 * Lookup a bunch of objects. Returns the number of objects not found.
1995 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2001 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2003 for (i = 0; i < count; i++, od++) {
2005 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2006 sizeof (uint64_t), 1, &od->od_object);
2008 ASSERT(error == ENOENT);
2009 ASSERT(od->od_object == 0);
2013 ztest_block_tag_t *bbt;
2014 dmu_object_info_t doi;
2016 ASSERT(od->od_object != 0);
2017 ASSERT(missing == 0); /* there should be no gaps */
2019 ztest_object_lock(zd, od->od_object, RL_READER);
2020 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2021 od->od_object, FTAG, &db));
2022 dmu_object_info_from_db(db, &doi);
2023 bbt = ztest_bt_bonus(db);
2024 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2025 od->od_type = doi.doi_type;
2026 od->od_blocksize = doi.doi_data_block_size;
2027 od->od_gen = bbt->bt_gen;
2028 dmu_buf_rele(db, FTAG);
2029 ztest_object_unlock(zd, od->od_object);
2037 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2042 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2044 for (i = 0; i < count; i++, od++) {
2051 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2053 lr->lr_doid = od->od_dir;
2054 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2055 lr->lrz_type = od->od_crtype;
2056 lr->lrz_blocksize = od->od_crblocksize;
2057 lr->lrz_ibshift = ztest_random_ibshift();
2058 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2059 lr->lrz_bonuslen = dmu_bonus_max();
2060 lr->lr_gen = od->od_crgen;
2061 lr->lr_crtime[0] = time(NULL);
2063 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2064 ASSERT(missing == 0);
2068 od->od_object = lr->lr_foid;
2069 od->od_type = od->od_crtype;
2070 od->od_blocksize = od->od_crblocksize;
2071 od->od_gen = od->od_crgen;
2072 ASSERT(od->od_object != 0);
2075 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2082 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2088 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2092 for (i = count - 1; i >= 0; i--, od--) {
2099 * No object was found.
2101 if (od->od_object == 0)
2104 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2106 lr->lr_doid = od->od_dir;
2108 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2109 ASSERT3U(error, ==, ENOSPC);
2114 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2121 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2127 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2129 lr->lr_foid = object;
2130 lr->lr_offset = offset;
2131 lr->lr_length = size;
2133 BP_ZERO(&lr->lr_blkptr);
2135 bcopy(data, lr + 1, size);
2137 error = ztest_replay_write(zd, lr, B_FALSE);
2139 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2145 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2150 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2152 lr->lr_foid = object;
2153 lr->lr_offset = offset;
2154 lr->lr_length = size;
2156 error = ztest_replay_truncate(zd, lr, B_FALSE);
2158 ztest_lr_free(lr, sizeof (*lr), NULL);
2164 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2169 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2171 lr->lr_foid = object;
2175 error = ztest_replay_setattr(zd, lr, B_FALSE);
2177 ztest_lr_free(lr, sizeof (*lr), NULL);
2183 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2185 objset_t *os = zd->zd_os;
2190 txg_wait_synced(dmu_objset_pool(os), 0);
2192 ztest_object_lock(zd, object, RL_READER);
2193 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2195 tx = dmu_tx_create(os);
2197 dmu_tx_hold_write(tx, object, offset, size);
2199 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2202 dmu_prealloc(os, object, offset, size, tx);
2204 txg_wait_synced(dmu_objset_pool(os), txg);
2206 (void) dmu_free_long_range(os, object, offset, size);
2209 ztest_range_unlock(rl);
2210 ztest_object_unlock(zd, object);
2214 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2217 ztest_block_tag_t wbt;
2218 dmu_object_info_t doi;
2219 enum ztest_io_type io_type;
2223 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2224 blocksize = doi.doi_data_block_size;
2225 data = umem_alloc(blocksize, UMEM_NOFAIL);
2228 * Pick an i/o type at random, biased toward writing block tags.
2230 io_type = ztest_random(ZTEST_IO_TYPES);
2231 if (ztest_random(2) == 0)
2232 io_type = ZTEST_IO_WRITE_TAG;
2234 (void) rw_rdlock(&zd->zd_zilog_lock);
2238 case ZTEST_IO_WRITE_TAG:
2239 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2240 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2243 case ZTEST_IO_WRITE_PATTERN:
2244 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2245 if (ztest_random(2) == 0) {
2247 * Induce fletcher2 collisions to ensure that
2248 * zio_ddt_collision() detects and resolves them
2249 * when using fletcher2-verify for deduplication.
2251 ((uint64_t *)data)[0] ^= 1ULL << 63;
2252 ((uint64_t *)data)[4] ^= 1ULL << 63;
2254 (void) ztest_write(zd, object, offset, blocksize, data);
2257 case ZTEST_IO_WRITE_ZEROES:
2258 bzero(data, blocksize);
2259 (void) ztest_write(zd, object, offset, blocksize, data);
2262 case ZTEST_IO_TRUNCATE:
2263 (void) ztest_truncate(zd, object, offset, blocksize);
2266 case ZTEST_IO_SETATTR:
2267 (void) ztest_setattr(zd, object);
2272 case ZTEST_IO_REWRITE:
2273 (void) rw_rdlock(&ztest_name_lock);
2274 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2275 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2277 VERIFY(err == 0 || err == ENOSPC);
2278 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2279 ZFS_PROP_COMPRESSION,
2280 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2282 VERIFY(err == 0 || err == ENOSPC);
2283 (void) rw_unlock(&ztest_name_lock);
2285 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2286 DMU_READ_NO_PREFETCH));
2288 (void) ztest_write(zd, object, offset, blocksize, data);
2292 (void) rw_unlock(&zd->zd_zilog_lock);
2294 umem_free(data, blocksize);
2298 * Initialize an object description template.
2301 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2302 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2304 od->od_dir = ZTEST_DIROBJ;
2307 od->od_crtype = type;
2308 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2311 od->od_type = DMU_OT_NONE;
2312 od->od_blocksize = 0;
2315 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2316 tag, (longlong_t)id, (u_longlong_t)index);
2320 * Lookup or create the objects for a test using the od template.
2321 * If the objects do not all exist, or if 'remove' is specified,
2322 * remove any existing objects and create new ones. Otherwise,
2323 * use the existing objects.
2326 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2328 int count = size / sizeof (*od);
2331 mutex_enter(&zd->zd_dirobj_lock);
2332 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2333 (ztest_remove(zd, od, count) != 0 ||
2334 ztest_create(zd, od, count) != 0))
2337 mutex_exit(&zd->zd_dirobj_lock);
2344 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2346 zilog_t *zilog = zd->zd_zilog;
2348 (void) rw_rdlock(&zd->zd_zilog_lock);
2350 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2353 * Remember the committed values in zd, which is in parent/child
2354 * shared memory. If we die, the next iteration of ztest_run()
2355 * will verify that the log really does contain this record.
2357 mutex_enter(&zilog->zl_lock);
2358 ASSERT(zd->zd_shared != NULL);
2359 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2360 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2361 mutex_exit(&zilog->zl_lock);
2363 (void) rw_unlock(&zd->zd_zilog_lock);
2367 * This function is designed to simulate the operations that occur during a
2368 * mount/unmount operation. We hold the dataset across these operations in an
2369 * attempt to expose any implicit assumptions about ZIL management.
2373 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2375 objset_t *os = zd->zd_os;
2378 * We grab the zd_dirobj_lock to ensure that no other thread is
2379 * updating the zil (i.e. adding in-memory log records) and the
2380 * zd_zilog_lock to block any I/O.
2382 mutex_enter(&zd->zd_dirobj_lock);
2383 (void) rw_wrlock(&zd->zd_zilog_lock);
2385 /* zfs_sb_teardown() */
2386 zil_close(zd->zd_zilog);
2388 /* zfsvfs_setup() */
2389 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2390 zil_replay(os, zd, ztest_replay_vector);
2392 (void) rw_unlock(&zd->zd_zilog_lock);
2393 mutex_exit(&zd->zd_dirobj_lock);
2397 * Verify that we can't destroy an active pool, create an existing pool,
2398 * or create a pool with a bad vdev spec.
2402 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2404 ztest_shared_opts_t *zo = &ztest_opts;
2409 * Attempt to create using a bad file.
2411 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2412 VERIFY3U(ENOENT, ==,
2413 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2414 nvlist_free(nvroot);
2417 * Attempt to create using a bad mirror.
2419 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2420 VERIFY3U(ENOENT, ==,
2421 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2422 nvlist_free(nvroot);
2425 * Attempt to create an existing pool. It shouldn't matter
2426 * what's in the nvroot; we should fail with EEXIST.
2428 (void) rw_rdlock(&ztest_name_lock);
2429 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2430 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2431 nvlist_free(nvroot);
2432 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2433 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2434 spa_close(spa, FTAG);
2436 (void) rw_unlock(&ztest_name_lock);
2441 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2444 uint64_t initial_version = SPA_VERSION_INITIAL;
2445 uint64_t version, newversion;
2446 nvlist_t *nvroot, *props;
2449 mutex_enter(&ztest_vdev_lock);
2450 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2453 * Clean up from previous runs.
2455 (void) spa_destroy(name);
2457 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2458 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2461 * If we're configuring a RAIDZ device then make sure that the
2462 * the initial version is capable of supporting that feature.
2464 switch (ztest_opts.zo_raidz_parity) {
2467 initial_version = SPA_VERSION_INITIAL;
2470 initial_version = SPA_VERSION_RAIDZ2;
2473 initial_version = SPA_VERSION_RAIDZ3;
2478 * Create a pool with a spa version that can be upgraded. Pick
2479 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2482 version = ztest_random_spa_version(initial_version);
2483 } while (version > SPA_VERSION_BEFORE_FEATURES);
2485 props = fnvlist_alloc();
2486 fnvlist_add_uint64(props,
2487 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2488 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2489 fnvlist_free(nvroot);
2490 fnvlist_free(props);
2492 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2493 VERIFY3U(spa_version(spa), ==, version);
2494 newversion = ztest_random_spa_version(version + 1);
2496 if (ztest_opts.zo_verbose >= 4) {
2497 (void) printf("upgrading spa version from %llu to %llu\n",
2498 (u_longlong_t)version, (u_longlong_t)newversion);
2501 spa_upgrade(spa, newversion);
2502 VERIFY3U(spa_version(spa), >, version);
2503 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2504 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2505 spa_close(spa, FTAG);
2508 mutex_exit(&ztest_vdev_lock);
2512 vdev_lookup_by_path(vdev_t *vd, const char *path)
2517 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2520 for (c = 0; c < vd->vdev_children; c++)
2521 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2529 * Find the first available hole which can be used as a top-level.
2532 find_vdev_hole(spa_t *spa)
2534 vdev_t *rvd = spa->spa_root_vdev;
2537 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2539 for (c = 0; c < rvd->vdev_children; c++) {
2540 vdev_t *cvd = rvd->vdev_child[c];
2542 if (cvd->vdev_ishole)
2549 * Verify that vdev_add() works as expected.
2553 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2555 ztest_shared_t *zs = ztest_shared;
2556 spa_t *spa = ztest_spa;
2562 mutex_enter(&ztest_vdev_lock);
2563 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2565 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2567 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2570 * If we have slogs then remove them 1/4 of the time.
2572 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2574 * Grab the guid from the head of the log class rotor.
2576 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2578 spa_config_exit(spa, SCL_VDEV, FTAG);
2581 * We have to grab the zs_name_lock as writer to
2582 * prevent a race between removing a slog (dmu_objset_find)
2583 * and destroying a dataset. Removing the slog will
2584 * grab a reference on the dataset which may cause
2585 * dsl_destroy_head() to fail with EBUSY thus
2586 * leaving the dataset in an inconsistent state.
2588 rw_wrlock(&ztest_name_lock);
2589 error = spa_vdev_remove(spa, guid, B_FALSE);
2590 rw_unlock(&ztest_name_lock);
2592 if (error && error != EEXIST)
2593 fatal(0, "spa_vdev_remove() = %d", error);
2595 spa_config_exit(spa, SCL_VDEV, FTAG);
2598 * Make 1/4 of the devices be log devices.
2600 nvroot = make_vdev_root(NULL, NULL, NULL,
2601 ztest_opts.zo_vdev_size, 0,
2602 ztest_random(4) == 0, ztest_opts.zo_raidz,
2605 error = spa_vdev_add(spa, nvroot);
2606 nvlist_free(nvroot);
2608 if (error == ENOSPC)
2609 ztest_record_enospc("spa_vdev_add");
2610 else if (error != 0)
2611 fatal(0, "spa_vdev_add() = %d", error);
2614 mutex_exit(&ztest_vdev_lock);
2618 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2622 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2624 ztest_shared_t *zs = ztest_shared;
2625 spa_t *spa = ztest_spa;
2626 vdev_t *rvd = spa->spa_root_vdev;
2627 spa_aux_vdev_t *sav;
2633 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2635 if (ztest_random(2) == 0) {
2636 sav = &spa->spa_spares;
2637 aux = ZPOOL_CONFIG_SPARES;
2639 sav = &spa->spa_l2cache;
2640 aux = ZPOOL_CONFIG_L2CACHE;
2643 mutex_enter(&ztest_vdev_lock);
2645 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2647 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2649 * Pick a random device to remove.
2651 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2654 * Find an unused device we can add.
2656 zs->zs_vdev_aux = 0;
2659 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2660 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2662 for (c = 0; c < sav->sav_count; c++)
2663 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2666 if (c == sav->sav_count &&
2667 vdev_lookup_by_path(rvd, path) == NULL)
2673 spa_config_exit(spa, SCL_VDEV, FTAG);
2679 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2680 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2681 error = spa_vdev_add(spa, nvroot);
2683 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2684 nvlist_free(nvroot);
2687 * Remove an existing device. Sometimes, dirty its
2688 * vdev state first to make sure we handle removal
2689 * of devices that have pending state changes.
2691 if (ztest_random(2) == 0)
2692 (void) vdev_online(spa, guid, 0, NULL);
2694 error = spa_vdev_remove(spa, guid, B_FALSE);
2695 if (error != 0 && error != EBUSY)
2696 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2699 mutex_exit(&ztest_vdev_lock);
2701 umem_free(path, MAXPATHLEN);
2705 * split a pool if it has mirror tlvdevs
2709 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2711 ztest_shared_t *zs = ztest_shared;
2712 spa_t *spa = ztest_spa;
2713 vdev_t *rvd = spa->spa_root_vdev;
2714 nvlist_t *tree, **child, *config, *split, **schild;
2715 uint_t c, children, schildren = 0, lastlogid = 0;
2718 mutex_enter(&ztest_vdev_lock);
2720 /* ensure we have a useable config; mirrors of raidz aren't supported */
2721 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2722 mutex_exit(&ztest_vdev_lock);
2726 /* clean up the old pool, if any */
2727 (void) spa_destroy("splitp");
2729 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2731 /* generate a config from the existing config */
2732 mutex_enter(&spa->spa_props_lock);
2733 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2735 mutex_exit(&spa->spa_props_lock);
2737 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2740 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2741 for (c = 0; c < children; c++) {
2742 vdev_t *tvd = rvd->vdev_child[c];
2746 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2747 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2749 VERIFY(nvlist_add_string(schild[schildren],
2750 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2751 VERIFY(nvlist_add_uint64(schild[schildren],
2752 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2754 lastlogid = schildren;
2759 VERIFY(nvlist_lookup_nvlist_array(child[c],
2760 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2761 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2764 /* OK, create a config that can be used to split */
2765 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2766 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2767 VDEV_TYPE_ROOT) == 0);
2768 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2769 lastlogid != 0 ? lastlogid : schildren) == 0);
2771 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2772 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2774 for (c = 0; c < schildren; c++)
2775 nvlist_free(schild[c]);
2779 spa_config_exit(spa, SCL_VDEV, FTAG);
2781 (void) rw_wrlock(&ztest_name_lock);
2782 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2783 (void) rw_unlock(&ztest_name_lock);
2785 nvlist_free(config);
2788 (void) printf("successful split - results:\n");
2789 mutex_enter(&spa_namespace_lock);
2790 show_pool_stats(spa);
2791 show_pool_stats(spa_lookup("splitp"));
2792 mutex_exit(&spa_namespace_lock);
2796 mutex_exit(&ztest_vdev_lock);
2801 * Verify that we can attach and detach devices.
2805 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2807 ztest_shared_t *zs = ztest_shared;
2808 spa_t *spa = ztest_spa;
2809 spa_aux_vdev_t *sav = &spa->spa_spares;
2810 vdev_t *rvd = spa->spa_root_vdev;
2811 vdev_t *oldvd, *newvd, *pvd;
2815 uint64_t ashift = ztest_get_ashift();
2816 uint64_t oldguid, pguid;
2817 uint64_t oldsize, newsize;
2818 char *oldpath, *newpath;
2820 int oldvd_has_siblings = B_FALSE;
2821 int newvd_is_spare = B_FALSE;
2823 int error, expected_error;
2825 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2826 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2828 mutex_enter(&ztest_vdev_lock);
2829 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2831 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2834 * Decide whether to do an attach or a replace.
2836 replacing = ztest_random(2);
2839 * Pick a random top-level vdev.
2841 top = ztest_random_vdev_top(spa, B_TRUE);
2844 * Pick a random leaf within it.
2846 leaf = ztest_random(leaves);
2851 oldvd = rvd->vdev_child[top];
2852 if (zs->zs_mirrors >= 1) {
2853 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2854 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2855 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2857 if (ztest_opts.zo_raidz > 1) {
2858 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2859 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2860 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2864 * If we're already doing an attach or replace, oldvd may be a
2865 * mirror vdev -- in which case, pick a random child.
2867 while (oldvd->vdev_children != 0) {
2868 oldvd_has_siblings = B_TRUE;
2869 ASSERT(oldvd->vdev_children >= 2);
2870 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2873 oldguid = oldvd->vdev_guid;
2874 oldsize = vdev_get_min_asize(oldvd);
2875 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2876 (void) strcpy(oldpath, oldvd->vdev_path);
2877 pvd = oldvd->vdev_parent;
2878 pguid = pvd->vdev_guid;
2881 * If oldvd has siblings, then half of the time, detach it.
2883 if (oldvd_has_siblings && ztest_random(2) == 0) {
2884 spa_config_exit(spa, SCL_VDEV, FTAG);
2885 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2886 if (error != 0 && error != ENODEV && error != EBUSY &&
2888 fatal(0, "detach (%s) returned %d", oldpath, error);
2893 * For the new vdev, choose with equal probability between the two
2894 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2896 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2897 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2898 newvd_is_spare = B_TRUE;
2899 (void) strcpy(newpath, newvd->vdev_path);
2901 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2902 ztest_opts.zo_dir, ztest_opts.zo_pool,
2903 top * leaves + leaf);
2904 if (ztest_random(2) == 0)
2905 newpath[strlen(newpath) - 1] = 'b';
2906 newvd = vdev_lookup_by_path(rvd, newpath);
2910 newsize = vdev_get_min_asize(newvd);
2913 * Make newsize a little bigger or smaller than oldsize.
2914 * If it's smaller, the attach should fail.
2915 * If it's larger, and we're doing a replace,
2916 * we should get dynamic LUN growth when we're done.
2918 newsize = 10 * oldsize / (9 + ztest_random(3));
2922 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2923 * unless it's a replace; in that case any non-replacing parent is OK.
2925 * If newvd is already part of the pool, it should fail with EBUSY.
2927 * If newvd is too small, it should fail with EOVERFLOW.
2929 if (pvd->vdev_ops != &vdev_mirror_ops &&
2930 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2931 pvd->vdev_ops == &vdev_replacing_ops ||
2932 pvd->vdev_ops == &vdev_spare_ops))
2933 expected_error = ENOTSUP;
2934 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2935 expected_error = ENOTSUP;
2936 else if (newvd == oldvd)
2937 expected_error = replacing ? 0 : EBUSY;
2938 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2939 expected_error = EBUSY;
2940 else if (newsize < oldsize)
2941 expected_error = EOVERFLOW;
2942 else if (ashift > oldvd->vdev_top->vdev_ashift)
2943 expected_error = EDOM;
2947 spa_config_exit(spa, SCL_VDEV, FTAG);
2950 * Build the nvlist describing newpath.
2952 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2953 ashift, 0, 0, 0, 1);
2955 error = spa_vdev_attach(spa, oldguid, root, replacing);
2960 * If our parent was the replacing vdev, but the replace completed,
2961 * then instead of failing with ENOTSUP we may either succeed,
2962 * fail with ENODEV, or fail with EOVERFLOW.
2964 if (expected_error == ENOTSUP &&
2965 (error == 0 || error == ENODEV || error == EOVERFLOW))
2966 expected_error = error;
2969 * If someone grew the LUN, the replacement may be too small.
2971 if (error == EOVERFLOW || error == EBUSY)
2972 expected_error = error;
2974 /* XXX workaround 6690467 */
2975 if (error != expected_error && expected_error != EBUSY) {
2976 fatal(0, "attach (%s %llu, %s %llu, %d) "
2977 "returned %d, expected %d",
2978 oldpath, oldsize, newpath,
2979 newsize, replacing, error, expected_error);
2982 mutex_exit(&ztest_vdev_lock);
2984 umem_free(oldpath, MAXPATHLEN);
2985 umem_free(newpath, MAXPATHLEN);
2989 * Callback function which expands the physical size of the vdev.
2992 grow_vdev(vdev_t *vd, void *arg)
2994 ASSERTV(spa_t *spa = vd->vdev_spa);
2995 size_t *newsize = arg;
2999 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3000 ASSERT(vd->vdev_ops->vdev_op_leaf);
3002 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3005 fsize = lseek(fd, 0, SEEK_END);
3006 VERIFY(ftruncate(fd, *newsize) == 0);
3008 if (ztest_opts.zo_verbose >= 6) {
3009 (void) printf("%s grew from %lu to %lu bytes\n",
3010 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3017 * Callback function which expands a given vdev by calling vdev_online().
3021 online_vdev(vdev_t *vd, void *arg)
3023 spa_t *spa = vd->vdev_spa;
3024 vdev_t *tvd = vd->vdev_top;
3025 uint64_t guid = vd->vdev_guid;
3026 uint64_t generation = spa->spa_config_generation + 1;
3027 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3030 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3031 ASSERT(vd->vdev_ops->vdev_op_leaf);
3033 /* Calling vdev_online will initialize the new metaslabs */
3034 spa_config_exit(spa, SCL_STATE, spa);
3035 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3036 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3039 * If vdev_online returned an error or the underlying vdev_open
3040 * failed then we abort the expand. The only way to know that
3041 * vdev_open fails is by checking the returned newstate.
3043 if (error || newstate != VDEV_STATE_HEALTHY) {
3044 if (ztest_opts.zo_verbose >= 5) {
3045 (void) printf("Unable to expand vdev, state %llu, "
3046 "error %d\n", (u_longlong_t)newstate, error);
3050 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3053 * Since we dropped the lock we need to ensure that we're
3054 * still talking to the original vdev. It's possible this
3055 * vdev may have been detached/replaced while we were
3056 * trying to online it.
3058 if (generation != spa->spa_config_generation) {
3059 if (ztest_opts.zo_verbose >= 5) {
3060 (void) printf("vdev configuration has changed, "
3061 "guid %llu, state %llu, expected gen %llu, "
3064 (u_longlong_t)tvd->vdev_state,
3065 (u_longlong_t)generation,
3066 (u_longlong_t)spa->spa_config_generation);
3074 * Traverse the vdev tree calling the supplied function.
3075 * We continue to walk the tree until we either have walked all
3076 * children or we receive a non-NULL return from the callback.
3077 * If a NULL callback is passed, then we just return back the first
3078 * leaf vdev we encounter.
3081 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3085 if (vd->vdev_ops->vdev_op_leaf) {
3089 return (func(vd, arg));
3092 for (c = 0; c < vd->vdev_children; c++) {
3093 vdev_t *cvd = vd->vdev_child[c];
3094 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3101 * Verify that dynamic LUN growth works as expected.
3105 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3107 spa_t *spa = ztest_spa;
3109 metaslab_class_t *mc;
3110 metaslab_group_t *mg;
3111 size_t psize, newsize;
3113 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3115 mutex_enter(&ztest_vdev_lock);
3116 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3118 top = ztest_random_vdev_top(spa, B_TRUE);
3120 tvd = spa->spa_root_vdev->vdev_child[top];
3123 old_ms_count = tvd->vdev_ms_count;
3124 old_class_space = metaslab_class_get_space(mc);
3127 * Determine the size of the first leaf vdev associated with
3128 * our top-level device.
3130 vd = vdev_walk_tree(tvd, NULL, NULL);
3131 ASSERT3P(vd, !=, NULL);
3132 ASSERT(vd->vdev_ops->vdev_op_leaf);
3134 psize = vd->vdev_psize;
3137 * We only try to expand the vdev if it's healthy, less than 4x its
3138 * original size, and it has a valid psize.
3140 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3141 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3142 spa_config_exit(spa, SCL_STATE, spa);
3143 mutex_exit(&ztest_vdev_lock);
3147 newsize = psize + psize / 8;
3148 ASSERT3U(newsize, >, psize);
3150 if (ztest_opts.zo_verbose >= 6) {
3151 (void) printf("Expanding LUN %s from %lu to %lu\n",
3152 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3156 * Growing the vdev is a two step process:
3157 * 1). expand the physical size (i.e. relabel)
3158 * 2). online the vdev to create the new metaslabs
3160 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3161 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3162 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3163 if (ztest_opts.zo_verbose >= 5) {
3164 (void) printf("Could not expand LUN because "
3165 "the vdev configuration changed.\n");
3167 spa_config_exit(spa, SCL_STATE, spa);
3168 mutex_exit(&ztest_vdev_lock);
3172 spa_config_exit(spa, SCL_STATE, spa);
3175 * Expanding the LUN will update the config asynchronously,
3176 * thus we must wait for the async thread to complete any
3177 * pending tasks before proceeding.
3181 mutex_enter(&spa->spa_async_lock);
3182 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3183 mutex_exit(&spa->spa_async_lock);
3186 txg_wait_synced(spa_get_dsl(spa), 0);
3187 (void) poll(NULL, 0, 100);
3190 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3192 tvd = spa->spa_root_vdev->vdev_child[top];
3193 new_ms_count = tvd->vdev_ms_count;
3194 new_class_space = metaslab_class_get_space(mc);
3196 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3197 if (ztest_opts.zo_verbose >= 5) {
3198 (void) printf("Could not verify LUN expansion due to "
3199 "intervening vdev offline or remove.\n");
3201 spa_config_exit(spa, SCL_STATE, spa);
3202 mutex_exit(&ztest_vdev_lock);
3207 * Make sure we were able to grow the vdev.
3209 if (new_ms_count <= old_ms_count)
3210 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3211 old_ms_count, new_ms_count);
3214 * Make sure we were able to grow the pool.
3216 if (new_class_space <= old_class_space)
3217 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3218 old_class_space, new_class_space);
3220 if (ztest_opts.zo_verbose >= 5) {
3221 char oldnumbuf[6], newnumbuf[6];
3223 nicenum(old_class_space, oldnumbuf);
3224 nicenum(new_class_space, newnumbuf);
3225 (void) printf("%s grew from %s to %s\n",
3226 spa->spa_name, oldnumbuf, newnumbuf);
3229 spa_config_exit(spa, SCL_STATE, spa);
3230 mutex_exit(&ztest_vdev_lock);
3234 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3238 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3241 * Create the objects common to all ztest datasets.
3243 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3244 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3248 ztest_dataset_create(char *dsname)
3250 uint64_t zilset = ztest_random(100);
3251 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3252 ztest_objset_create_cb, NULL);
3254 if (err || zilset < 80)
3257 if (ztest_opts.zo_verbose >= 5)
3258 (void) printf("Setting dataset %s to sync always\n", dsname);
3259 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3260 ZFS_SYNC_ALWAYS, B_FALSE));
3265 ztest_objset_destroy_cb(const char *name, void *arg)
3268 dmu_object_info_t doi;
3272 * Verify that the dataset contains a directory object.
3274 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3275 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3276 if (error != ENOENT) {
3277 /* We could have crashed in the middle of destroying it */
3279 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3280 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3282 dmu_objset_disown(os, FTAG);
3285 * Destroy the dataset.
3287 if (strchr(name, '@') != NULL) {
3288 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3290 VERIFY0(dsl_destroy_head(name));
3296 ztest_snapshot_create(char *osname, uint64_t id)
3298 char snapname[MAXNAMELEN];
3301 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3303 error = dmu_objset_snapshot_one(osname, snapname);
3304 if (error == ENOSPC) {
3305 ztest_record_enospc(FTAG);
3308 if (error != 0 && error != EEXIST) {
3309 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3316 ztest_snapshot_destroy(char *osname, uint64_t id)
3318 char snapname[MAXNAMELEN];
3321 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3324 error = dsl_destroy_snapshot(snapname, B_FALSE);
3325 if (error != 0 && error != ENOENT)
3326 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3332 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3342 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3343 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3345 (void) rw_rdlock(&ztest_name_lock);
3347 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3348 ztest_opts.zo_pool, (u_longlong_t)id);
3351 * If this dataset exists from a previous run, process its replay log
3352 * half of the time. If we don't replay it, then dsl_destroy_head()
3353 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3355 if (ztest_random(2) == 0 &&
3356 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3357 ztest_zd_init(zdtmp, NULL, os);
3358 zil_replay(os, zdtmp, ztest_replay_vector);
3359 ztest_zd_fini(zdtmp);
3360 dmu_objset_disown(os, FTAG);
3364 * There may be an old instance of the dataset we're about to
3365 * create lying around from a previous run. If so, destroy it
3366 * and all of its snapshots.
3368 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3369 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3372 * Verify that the destroyed dataset is no longer in the namespace.
3374 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3378 * Verify that we can create a new dataset.
3380 error = ztest_dataset_create(name);
3382 if (error == ENOSPC) {
3383 ztest_record_enospc(FTAG);
3386 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3389 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3391 ztest_zd_init(zdtmp, NULL, os);
3394 * Open the intent log for it.
3396 zilog = zil_open(os, ztest_get_data);
3399 * Put some objects in there, do a little I/O to them,
3400 * and randomly take a couple of snapshots along the way.
3402 iters = ztest_random(5);
3403 for (i = 0; i < iters; i++) {
3404 ztest_dmu_object_alloc_free(zdtmp, id);
3405 if (ztest_random(iters) == 0)
3406 (void) ztest_snapshot_create(name, i);
3410 * Verify that we cannot create an existing dataset.
3412 VERIFY3U(EEXIST, ==,
3413 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3416 * Verify that we can hold an objset that is also owned.
3418 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3419 dmu_objset_rele(os2, FTAG);
3422 * Verify that we cannot own an objset that is already owned.
3425 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3428 dmu_objset_disown(os, FTAG);
3429 ztest_zd_fini(zdtmp);
3431 (void) rw_unlock(&ztest_name_lock);
3433 umem_free(name, MAXNAMELEN);
3434 umem_free(zdtmp, sizeof (ztest_ds_t));
3438 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3441 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3443 (void) rw_rdlock(&ztest_name_lock);
3444 (void) ztest_snapshot_destroy(zd->zd_name, id);
3445 (void) ztest_snapshot_create(zd->zd_name, id);
3446 (void) rw_unlock(&ztest_name_lock);
3450 * Cleanup non-standard snapshots and clones.
3453 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3462 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3463 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3464 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3465 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3466 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3468 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3469 osname, (u_longlong_t)id);
3470 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3471 osname, (u_longlong_t)id);
3472 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3473 clone1name, (u_longlong_t)id);
3474 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3475 osname, (u_longlong_t)id);
3476 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3477 clone1name, (u_longlong_t)id);
3479 error = dsl_destroy_head(clone2name);
3480 if (error && error != ENOENT)
3481 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3482 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3483 if (error && error != ENOENT)
3484 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3485 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3486 if (error && error != ENOENT)
3487 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3488 error = dsl_destroy_head(clone1name);
3489 if (error && error != ENOENT)
3490 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3491 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3492 if (error && error != ENOENT)
3493 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3495 umem_free(snap1name, MAXNAMELEN);
3496 umem_free(clone1name, MAXNAMELEN);
3497 umem_free(snap2name, MAXNAMELEN);
3498 umem_free(clone2name, MAXNAMELEN);
3499 umem_free(snap3name, MAXNAMELEN);
3503 * Verify dsl_dataset_promote handles EBUSY
3506 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3514 char *osname = zd->zd_name;
3517 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3518 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3519 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3520 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3521 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3523 (void) rw_rdlock(&ztest_name_lock);
3525 ztest_dsl_dataset_cleanup(osname, id);
3527 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3528 osname, (u_longlong_t)id);
3529 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3530 osname, (u_longlong_t)id);
3531 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3532 clone1name, (u_longlong_t)id);
3533 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3534 osname, (u_longlong_t)id);
3535 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3536 clone1name, (u_longlong_t)id);
3538 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3539 if (error && error != EEXIST) {
3540 if (error == ENOSPC) {
3541 ztest_record_enospc(FTAG);
3544 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3547 error = dmu_objset_clone(clone1name, snap1name);
3549 if (error == ENOSPC) {
3550 ztest_record_enospc(FTAG);
3553 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3556 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3557 if (error && error != EEXIST) {
3558 if (error == ENOSPC) {
3559 ztest_record_enospc(FTAG);
3562 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3565 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3566 if (error && error != EEXIST) {
3567 if (error == ENOSPC) {
3568 ztest_record_enospc(FTAG);
3571 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3574 error = dmu_objset_clone(clone2name, snap3name);
3576 if (error == ENOSPC) {
3577 ztest_record_enospc(FTAG);
3580 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3583 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3585 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3586 error = dsl_dataset_promote(clone2name, NULL);
3587 if (error == ENOSPC) {
3588 dmu_objset_disown(os, FTAG);
3589 ztest_record_enospc(FTAG);
3593 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3595 dmu_objset_disown(os, FTAG);
3598 ztest_dsl_dataset_cleanup(osname, id);
3600 (void) rw_unlock(&ztest_name_lock);
3602 umem_free(snap1name, MAXNAMELEN);
3603 umem_free(clone1name, MAXNAMELEN);
3604 umem_free(snap2name, MAXNAMELEN);
3605 umem_free(clone2name, MAXNAMELEN);
3606 umem_free(snap3name, MAXNAMELEN);
3609 #undef OD_ARRAY_SIZE
3610 #define OD_ARRAY_SIZE 4
3613 * Verify that dmu_object_{alloc,free} work as expected.
3616 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3623 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3624 od = umem_alloc(size, UMEM_NOFAIL);
3625 batchsize = OD_ARRAY_SIZE;
3627 for (b = 0; b < batchsize; b++)
3628 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3631 * Destroy the previous batch of objects, create a new batch,
3632 * and do some I/O on the new objects.
3634 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3637 while (ztest_random(4 * batchsize) != 0)
3638 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3639 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3641 umem_free(od, size);
3644 #undef OD_ARRAY_SIZE
3645 #define OD_ARRAY_SIZE 2
3648 * Verify that dmu_{read,write} work as expected.
3651 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3656 objset_t *os = zd->zd_os;
3657 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3658 od = umem_alloc(size, UMEM_NOFAIL);
3660 int i, freeit, error;
3662 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3663 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3664 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3665 uint64_t regions = 997;
3666 uint64_t stride = 123456789ULL;
3667 uint64_t width = 40;
3668 int free_percent = 5;
3671 * This test uses two objects, packobj and bigobj, that are always
3672 * updated together (i.e. in the same tx) so that their contents are
3673 * in sync and can be compared. Their contents relate to each other
3674 * in a simple way: packobj is a dense array of 'bufwad' structures,
3675 * while bigobj is a sparse array of the same bufwads. Specifically,
3676 * for any index n, there are three bufwads that should be identical:
3678 * packobj, at offset n * sizeof (bufwad_t)
3679 * bigobj, at the head of the nth chunk
3680 * bigobj, at the tail of the nth chunk
3682 * The chunk size is arbitrary. It doesn't have to be a power of two,
3683 * and it doesn't have any relation to the object blocksize.
3684 * The only requirement is that it can hold at least two bufwads.
3686 * Normally, we write the bufwad to each of these locations.
3687 * However, free_percent of the time we instead write zeroes to
3688 * packobj and perform a dmu_free_range() on bigobj. By comparing
3689 * bigobj to packobj, we can verify that the DMU is correctly
3690 * tracking which parts of an object are allocated and free,
3691 * and that the contents of the allocated blocks are correct.
3695 * Read the directory info. If it's the first time, set things up.
3697 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3698 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3700 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3701 umem_free(od, size);
3705 bigobj = od[0].od_object;
3706 packobj = od[1].od_object;
3707 chunksize = od[0].od_gen;
3708 ASSERT(chunksize == od[1].od_gen);
3711 * Prefetch a random chunk of the big object.
3712 * Our aim here is to get some async reads in flight
3713 * for blocks that we may free below; the DMU should
3714 * handle this race correctly.
3716 n = ztest_random(regions) * stride + ztest_random(width);
3717 s = 1 + ztest_random(2 * width - 1);
3718 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3721 * Pick a random index and compute the offsets into packobj and bigobj.
3723 n = ztest_random(regions) * stride + ztest_random(width);
3724 s = 1 + ztest_random(width - 1);
3726 packoff = n * sizeof (bufwad_t);
3727 packsize = s * sizeof (bufwad_t);
3729 bigoff = n * chunksize;
3730 bigsize = s * chunksize;
3732 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3733 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3736 * free_percent of the time, free a range of bigobj rather than
3739 freeit = (ztest_random(100) < free_percent);
3742 * Read the current contents of our objects.
3744 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3747 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3752 * Get a tx for the mods to both packobj and bigobj.
3754 tx = dmu_tx_create(os);
3756 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3759 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3761 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3763 /* This accounts for setting the checksum/compression. */
3764 dmu_tx_hold_bonus(tx, bigobj);
3766 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3768 umem_free(packbuf, packsize);
3769 umem_free(bigbuf, bigsize);
3770 umem_free(od, size);
3774 enum zio_checksum cksum;
3776 cksum = (enum zio_checksum)
3777 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3778 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3779 dmu_object_set_checksum(os, bigobj, cksum, tx);
3781 enum zio_compress comp;
3783 comp = (enum zio_compress)
3784 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3785 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3786 dmu_object_set_compress(os, bigobj, comp, tx);
3789 * For each index from n to n + s, verify that the existing bufwad
3790 * in packobj matches the bufwads at the head and tail of the
3791 * corresponding chunk in bigobj. Then update all three bufwads
3792 * with the new values we want to write out.
3794 for (i = 0; i < s; i++) {
3796 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3798 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3800 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3802 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3803 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3805 if (pack->bw_txg > txg)
3806 fatal(0, "future leak: got %llx, open txg is %llx",
3809 if (pack->bw_data != 0 && pack->bw_index != n + i)
3810 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3811 pack->bw_index, n, i);
3813 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3814 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3816 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3817 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3820 bzero(pack, sizeof (bufwad_t));
3822 pack->bw_index = n + i;
3824 pack->bw_data = 1 + ztest_random(-2ULL);
3831 * We've verified all the old bufwads, and made new ones.
3832 * Now write them out.
3834 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3837 if (ztest_opts.zo_verbose >= 7) {
3838 (void) printf("freeing offset %llx size %llx"
3840 (u_longlong_t)bigoff,
3841 (u_longlong_t)bigsize,
3844 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3846 if (ztest_opts.zo_verbose >= 7) {
3847 (void) printf("writing offset %llx size %llx"
3849 (u_longlong_t)bigoff,
3850 (u_longlong_t)bigsize,
3853 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3859 * Sanity check the stuff we just wrote.
3862 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3863 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3865 VERIFY(0 == dmu_read(os, packobj, packoff,
3866 packsize, packcheck, DMU_READ_PREFETCH));
3867 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3868 bigsize, bigcheck, DMU_READ_PREFETCH));
3870 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3871 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3873 umem_free(packcheck, packsize);
3874 umem_free(bigcheck, bigsize);
3877 umem_free(packbuf, packsize);
3878 umem_free(bigbuf, bigsize);
3879 umem_free(od, size);
3883 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3884 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3892 * For each index from n to n + s, verify that the existing bufwad
3893 * in packobj matches the bufwads at the head and tail of the
3894 * corresponding chunk in bigobj. Then update all three bufwads
3895 * with the new values we want to write out.
3897 for (i = 0; i < s; i++) {
3899 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3901 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3903 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3905 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3906 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3908 if (pack->bw_txg > txg)
3909 fatal(0, "future leak: got %llx, open txg is %llx",
3912 if (pack->bw_data != 0 && pack->bw_index != n + i)
3913 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3914 pack->bw_index, n, i);
3916 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3917 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3919 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3920 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3922 pack->bw_index = n + i;
3924 pack->bw_data = 1 + ztest_random(-2ULL);
3931 #undef OD_ARRAY_SIZE
3932 #define OD_ARRAY_SIZE 2
3935 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3937 objset_t *os = zd->zd_os;
3944 bufwad_t *packbuf, *bigbuf;
3945 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3946 uint64_t blocksize = ztest_random_blocksize();
3947 uint64_t chunksize = blocksize;
3948 uint64_t regions = 997;
3949 uint64_t stride = 123456789ULL;
3951 dmu_buf_t *bonus_db;
3952 arc_buf_t **bigbuf_arcbufs;
3953 dmu_object_info_t doi;
3955 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3956 od = umem_alloc(size, UMEM_NOFAIL);
3959 * This test uses two objects, packobj and bigobj, that are always
3960 * updated together (i.e. in the same tx) so that their contents are
3961 * in sync and can be compared. Their contents relate to each other
3962 * in a simple way: packobj is a dense array of 'bufwad' structures,
3963 * while bigobj is a sparse array of the same bufwads. Specifically,
3964 * for any index n, there are three bufwads that should be identical:
3966 * packobj, at offset n * sizeof (bufwad_t)
3967 * bigobj, at the head of the nth chunk
3968 * bigobj, at the tail of the nth chunk
3970 * The chunk size is set equal to bigobj block size so that
3971 * dmu_assign_arcbuf() can be tested for object updates.
3975 * Read the directory info. If it's the first time, set things up.
3977 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3978 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3981 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3982 umem_free(od, size);
3986 bigobj = od[0].od_object;
3987 packobj = od[1].od_object;
3988 blocksize = od[0].od_blocksize;
3989 chunksize = blocksize;
3990 ASSERT(chunksize == od[1].od_gen);
3992 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3993 VERIFY(ISP2(doi.doi_data_block_size));
3994 VERIFY(chunksize == doi.doi_data_block_size);
3995 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3998 * Pick a random index and compute the offsets into packobj and bigobj.
4000 n = ztest_random(regions) * stride + ztest_random(width);
4001 s = 1 + ztest_random(width - 1);
4003 packoff = n * sizeof (bufwad_t);
4004 packsize = s * sizeof (bufwad_t);
4006 bigoff = n * chunksize;
4007 bigsize = s * chunksize;
4009 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4010 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4012 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4014 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4017 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4018 * Iteration 1 test zcopy to already referenced dbufs.
4019 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4020 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4021 * Iteration 4 test zcopy when dbuf is no longer dirty.
4022 * Iteration 5 test zcopy when it can't be done.
4023 * Iteration 6 one more zcopy write.
4025 for (i = 0; i < 7; i++) {
4030 * In iteration 5 (i == 5) use arcbufs
4031 * that don't match bigobj blksz to test
4032 * dmu_assign_arcbuf() when it can't directly
4033 * assign an arcbuf to a dbuf.
4035 for (j = 0; j < s; j++) {
4038 dmu_request_arcbuf(bonus_db, chunksize);
4040 bigbuf_arcbufs[2 * j] =
4041 dmu_request_arcbuf(bonus_db, chunksize / 2);
4042 bigbuf_arcbufs[2 * j + 1] =
4043 dmu_request_arcbuf(bonus_db, chunksize / 2);
4048 * Get a tx for the mods to both packobj and bigobj.
4050 tx = dmu_tx_create(os);
4052 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4053 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4055 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4057 umem_free(packbuf, packsize);
4058 umem_free(bigbuf, bigsize);
4059 for (j = 0; j < s; j++) {
4061 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4064 bigbuf_arcbufs[2 * j]);
4066 bigbuf_arcbufs[2 * j + 1]);
4069 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4070 umem_free(od, size);
4071 dmu_buf_rele(bonus_db, FTAG);
4076 * 50% of the time don't read objects in the 1st iteration to
4077 * test dmu_assign_arcbuf() for the case when there're no
4078 * existing dbufs for the specified offsets.
4080 if (i != 0 || ztest_random(2) != 0) {
4081 error = dmu_read(os, packobj, packoff,
4082 packsize, packbuf, DMU_READ_PREFETCH);
4084 error = dmu_read(os, bigobj, bigoff, bigsize,
4085 bigbuf, DMU_READ_PREFETCH);
4088 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4092 * We've verified all the old bufwads, and made new ones.
4093 * Now write them out.
4095 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4096 if (ztest_opts.zo_verbose >= 7) {
4097 (void) printf("writing offset %llx size %llx"
4099 (u_longlong_t)bigoff,
4100 (u_longlong_t)bigsize,
4103 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4106 bcopy((caddr_t)bigbuf + (off - bigoff),
4107 bigbuf_arcbufs[j]->b_data, chunksize);
4109 bcopy((caddr_t)bigbuf + (off - bigoff),
4110 bigbuf_arcbufs[2 * j]->b_data,
4112 bcopy((caddr_t)bigbuf + (off - bigoff) +
4114 bigbuf_arcbufs[2 * j + 1]->b_data,
4119 VERIFY(dmu_buf_hold(os, bigobj, off,
4120 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4123 dmu_assign_arcbuf(bonus_db, off,
4124 bigbuf_arcbufs[j], tx);
4126 dmu_assign_arcbuf(bonus_db, off,
4127 bigbuf_arcbufs[2 * j], tx);
4128 dmu_assign_arcbuf(bonus_db,
4129 off + chunksize / 2,
4130 bigbuf_arcbufs[2 * j + 1], tx);
4133 dmu_buf_rele(dbt, FTAG);
4139 * Sanity check the stuff we just wrote.
4142 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4143 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4145 VERIFY(0 == dmu_read(os, packobj, packoff,
4146 packsize, packcheck, DMU_READ_PREFETCH));
4147 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4148 bigsize, bigcheck, DMU_READ_PREFETCH));
4150 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4151 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4153 umem_free(packcheck, packsize);
4154 umem_free(bigcheck, bigsize);
4157 txg_wait_open(dmu_objset_pool(os), 0);
4158 } else if (i == 3) {
4159 txg_wait_synced(dmu_objset_pool(os), 0);
4163 dmu_buf_rele(bonus_db, FTAG);
4164 umem_free(packbuf, packsize);
4165 umem_free(bigbuf, bigsize);
4166 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4167 umem_free(od, size);
4172 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4176 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4177 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4178 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4181 * Have multiple threads write to large offsets in an object
4182 * to verify that parallel writes to an object -- even to the
4183 * same blocks within the object -- doesn't cause any trouble.
4185 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4187 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4190 while (ztest_random(10) != 0)
4191 ztest_io(zd, od->od_object, offset);
4193 umem_free(od, sizeof (ztest_od_t));
4197 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4200 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4201 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4202 uint64_t count = ztest_random(20) + 1;
4203 uint64_t blocksize = ztest_random_blocksize();
4206 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4208 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4210 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4211 !ztest_random(2)) != 0) {
4212 umem_free(od, sizeof (ztest_od_t));
4216 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4217 umem_free(od, sizeof (ztest_od_t));
4221 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4223 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4225 while (ztest_random(count) != 0) {
4226 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4227 if (ztest_write(zd, od->od_object, randoff, blocksize,
4230 while (ztest_random(4) != 0)
4231 ztest_io(zd, od->od_object, randoff);
4234 umem_free(data, blocksize);
4235 umem_free(od, sizeof (ztest_od_t));
4239 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4241 #define ZTEST_ZAP_MIN_INTS 1
4242 #define ZTEST_ZAP_MAX_INTS 4
4243 #define ZTEST_ZAP_MAX_PROPS 1000
4246 ztest_zap(ztest_ds_t *zd, uint64_t id)
4248 objset_t *os = zd->zd_os;
4251 uint64_t txg, last_txg;
4252 uint64_t value[ZTEST_ZAP_MAX_INTS];
4253 uint64_t zl_ints, zl_intsize, prop;
4256 char propname[100], txgname[100];
4258 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4260 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4261 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4263 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4264 !ztest_random(2)) != 0)
4267 object = od->od_object;
4270 * Generate a known hash collision, and verify that
4271 * we can lookup and remove both entries.
4273 tx = dmu_tx_create(os);
4274 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4275 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4278 for (i = 0; i < 2; i++) {
4280 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4283 for (i = 0; i < 2; i++) {
4284 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4285 sizeof (uint64_t), 1, &value[i], tx));
4287 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4288 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4289 ASSERT3U(zl_ints, ==, 1);
4291 for (i = 0; i < 2; i++) {
4292 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4297 * Generate a buch of random entries.
4299 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4301 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4302 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4303 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4304 bzero(value, sizeof (value));
4308 * If these zap entries already exist, validate their contents.
4310 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4312 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4313 ASSERT3U(zl_ints, ==, 1);
4315 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4316 zl_ints, &last_txg) == 0);
4318 VERIFY(zap_length(os, object, propname, &zl_intsize,
4321 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4322 ASSERT3U(zl_ints, ==, ints);
4324 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4325 zl_ints, value) == 0);
4327 for (i = 0; i < ints; i++) {
4328 ASSERT3U(value[i], ==, last_txg + object + i);
4331 ASSERT3U(error, ==, ENOENT);
4335 * Atomically update two entries in our zap object.
4336 * The first is named txg_%llu, and contains the txg
4337 * in which the property was last updated. The second
4338 * is named prop_%llu, and the nth element of its value
4339 * should be txg + object + n.
4341 tx = dmu_tx_create(os);
4342 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4343 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4348 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4350 for (i = 0; i < ints; i++)
4351 value[i] = txg + object + i;
4353 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4355 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4361 * Remove a random pair of entries.
4363 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4364 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4365 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4367 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4369 if (error == ENOENT)
4374 tx = dmu_tx_create(os);
4375 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4376 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4379 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4380 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4383 umem_free(od, sizeof (ztest_od_t));
4387 * Testcase to test the upgrading of a microzap to fatzap.
4390 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4392 objset_t *os = zd->zd_os;
4394 uint64_t object, txg;
4397 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4398 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4400 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4401 !ztest_random(2)) != 0)
4403 object = od->od_object;
4406 * Add entries to this ZAP and make sure it spills over
4407 * and gets upgraded to a fatzap. Also, since we are adding
4408 * 2050 entries we should see ptrtbl growth and leaf-block split.
4410 for (i = 0; i < 2050; i++) {
4411 char name[MAXNAMELEN];
4416 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4417 (u_longlong_t)id, (u_longlong_t)value);
4419 tx = dmu_tx_create(os);
4420 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4421 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4424 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4426 ASSERT(error == 0 || error == EEXIST);
4430 umem_free(od, sizeof (ztest_od_t));
4435 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4437 objset_t *os = zd->zd_os;
4439 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4441 int i, namelen, error;
4442 int micro = ztest_random(2);
4443 char name[20], string_value[20];
4446 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4447 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4449 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4450 umem_free(od, sizeof (ztest_od_t));
4454 object = od->od_object;
4457 * Generate a random name of the form 'xxx.....' where each
4458 * x is a random printable character and the dots are dots.
4459 * There are 94 such characters, and the name length goes from
4460 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4462 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4464 for (i = 0; i < 3; i++)
4465 name[i] = '!' + ztest_random('~' - '!' + 1);
4466 for (; i < namelen - 1; i++)
4470 if ((namelen & 1) || micro) {
4471 wsize = sizeof (txg);
4477 data = string_value;
4481 VERIFY0(zap_count(os, object, &count));
4482 ASSERT(count != -1ULL);
4485 * Select an operation: length, lookup, add, update, remove.
4487 i = ztest_random(5);
4490 tx = dmu_tx_create(os);
4491 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4492 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4495 bcopy(name, string_value, namelen);
4499 bzero(string_value, namelen);
4505 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4507 ASSERT3U(wsize, ==, zl_wsize);
4508 ASSERT3U(wc, ==, zl_wc);
4510 ASSERT3U(error, ==, ENOENT);
4515 error = zap_lookup(os, object, name, wsize, wc, data);
4517 if (data == string_value &&
4518 bcmp(name, data, namelen) != 0)
4519 fatal(0, "name '%s' != val '%s' len %d",
4520 name, data, namelen);
4522 ASSERT3U(error, ==, ENOENT);
4527 error = zap_add(os, object, name, wsize, wc, data, tx);
4528 ASSERT(error == 0 || error == EEXIST);
4532 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4536 error = zap_remove(os, object, name, tx);
4537 ASSERT(error == 0 || error == ENOENT);
4544 umem_free(od, sizeof (ztest_od_t));
4548 * Commit callback data.
4550 typedef struct ztest_cb_data {
4551 list_node_t zcd_node;
4553 int zcd_expected_err;
4554 boolean_t zcd_added;
4555 boolean_t zcd_called;
4559 /* This is the actual commit callback function */
4561 ztest_commit_callback(void *arg, int error)
4563 ztest_cb_data_t *data = arg;
4564 uint64_t synced_txg;
4566 VERIFY(data != NULL);
4567 VERIFY3S(data->zcd_expected_err, ==, error);
4568 VERIFY(!data->zcd_called);
4570 synced_txg = spa_last_synced_txg(data->zcd_spa);
4571 if (data->zcd_txg > synced_txg)
4572 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4573 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4576 data->zcd_called = B_TRUE;
4578 if (error == ECANCELED) {
4579 ASSERT0(data->zcd_txg);
4580 ASSERT(!data->zcd_added);
4583 * The private callback data should be destroyed here, but
4584 * since we are going to check the zcd_called field after
4585 * dmu_tx_abort(), we will destroy it there.
4590 ASSERT(data->zcd_added);
4591 ASSERT3U(data->zcd_txg, !=, 0);
4593 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4595 /* See if this cb was called more quickly */
4596 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4597 zc_min_txg_delay = synced_txg - data->zcd_txg;
4599 /* Remove our callback from the list */
4600 list_remove(&zcl.zcl_callbacks, data);
4602 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4604 umem_free(data, sizeof (ztest_cb_data_t));
4607 /* Allocate and initialize callback data structure */
4608 static ztest_cb_data_t *
4609 ztest_create_cb_data(objset_t *os, uint64_t txg)
4611 ztest_cb_data_t *cb_data;
4613 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4615 cb_data->zcd_txg = txg;
4616 cb_data->zcd_spa = dmu_objset_spa(os);
4617 list_link_init(&cb_data->zcd_node);
4623 * Commit callback test.
4626 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4628 objset_t *os = zd->zd_os;
4631 ztest_cb_data_t *cb_data[3], *tmp_cb;
4632 uint64_t old_txg, txg;
4635 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4636 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4638 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4639 umem_free(od, sizeof (ztest_od_t));
4643 tx = dmu_tx_create(os);
4645 cb_data[0] = ztest_create_cb_data(os, 0);
4646 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4648 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4650 /* Every once in a while, abort the transaction on purpose */
4651 if (ztest_random(100) == 0)
4655 error = dmu_tx_assign(tx, TXG_NOWAIT);
4657 txg = error ? 0 : dmu_tx_get_txg(tx);
4659 cb_data[0]->zcd_txg = txg;
4660 cb_data[1] = ztest_create_cb_data(os, txg);
4661 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4665 * It's not a strict requirement to call the registered
4666 * callbacks from inside dmu_tx_abort(), but that's what
4667 * it's supposed to happen in the current implementation
4668 * so we will check for that.
4670 for (i = 0; i < 2; i++) {
4671 cb_data[i]->zcd_expected_err = ECANCELED;
4672 VERIFY(!cb_data[i]->zcd_called);
4677 for (i = 0; i < 2; i++) {
4678 VERIFY(cb_data[i]->zcd_called);
4679 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4682 umem_free(od, sizeof (ztest_od_t));
4686 cb_data[2] = ztest_create_cb_data(os, txg);
4687 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4690 * Read existing data to make sure there isn't a future leak.
4692 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4693 &old_txg, DMU_READ_PREFETCH));
4696 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4699 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4701 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4704 * Since commit callbacks don't have any ordering requirement and since
4705 * it is theoretically possible for a commit callback to be called
4706 * after an arbitrary amount of time has elapsed since its txg has been
4707 * synced, it is difficult to reliably determine whether a commit
4708 * callback hasn't been called due to high load or due to a flawed
4711 * In practice, we will assume that if after a certain number of txgs a
4712 * commit callback hasn't been called, then most likely there's an
4713 * implementation bug..
4715 tmp_cb = list_head(&zcl.zcl_callbacks);
4716 if (tmp_cb != NULL &&
4717 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4718 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4719 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4723 * Let's find the place to insert our callbacks.
4725 * Even though the list is ordered by txg, it is possible for the
4726 * insertion point to not be the end because our txg may already be
4727 * quiescing at this point and other callbacks in the open txg
4728 * (from other objsets) may have sneaked in.
4730 tmp_cb = list_tail(&zcl.zcl_callbacks);
4731 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4732 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4734 /* Add the 3 callbacks to the list */
4735 for (i = 0; i < 3; i++) {
4737 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4739 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4742 cb_data[i]->zcd_added = B_TRUE;
4743 VERIFY(!cb_data[i]->zcd_called);
4745 tmp_cb = cb_data[i];
4750 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4754 umem_free(od, sizeof (ztest_od_t));
4759 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4761 zfs_prop_t proplist[] = {
4763 ZFS_PROP_COMPRESSION,
4769 (void) rw_rdlock(&ztest_name_lock);
4771 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4772 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4773 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4775 (void) rw_unlock(&ztest_name_lock);
4780 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4782 nvlist_t *props = NULL;
4784 (void) rw_rdlock(&ztest_name_lock);
4786 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4787 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4789 VERIFY0(spa_prop_get(ztest_spa, &props));
4791 if (ztest_opts.zo_verbose >= 6)
4792 dump_nvlist(props, 4);
4796 (void) rw_unlock(&ztest_name_lock);
4800 user_release_one(const char *snapname, const char *holdname)
4802 nvlist_t *snaps, *holds;
4805 snaps = fnvlist_alloc();
4806 holds = fnvlist_alloc();
4807 fnvlist_add_boolean(holds, holdname);
4808 fnvlist_add_nvlist(snaps, snapname, holds);
4809 fnvlist_free(holds);
4810 error = dsl_dataset_user_release(snaps, NULL);
4811 fnvlist_free(snaps);
4816 * Test snapshot hold/release and deferred destroy.
4819 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4822 objset_t *os = zd->zd_os;
4826 char clonename[100];
4828 char osname[MAXNAMELEN];
4831 (void) rw_rdlock(&ztest_name_lock);
4833 dmu_objset_name(os, osname);
4835 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4837 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4838 (void) snprintf(clonename, sizeof (clonename),
4839 "%s/ch1_%llu", osname, (u_longlong_t)id);
4840 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4843 * Clean up from any previous run.
4845 error = dsl_destroy_head(clonename);
4846 if (error != ENOENT)
4848 error = user_release_one(fullname, tag);
4849 if (error != ESRCH && error != ENOENT)
4851 error = dsl_destroy_snapshot(fullname, B_FALSE);
4852 if (error != ENOENT)
4856 * Create snapshot, clone it, mark snap for deferred destroy,
4857 * destroy clone, verify snap was also destroyed.
4859 error = dmu_objset_snapshot_one(osname, snapname);
4861 if (error == ENOSPC) {
4862 ztest_record_enospc("dmu_objset_snapshot");
4865 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4868 error = dmu_objset_clone(clonename, fullname);
4870 if (error == ENOSPC) {
4871 ztest_record_enospc("dmu_objset_clone");
4874 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4877 error = dsl_destroy_snapshot(fullname, B_TRUE);
4879 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4883 error = dsl_destroy_head(clonename);
4885 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4887 error = dmu_objset_hold(fullname, FTAG, &origin);
4888 if (error != ENOENT)
4889 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4892 * Create snapshot, add temporary hold, verify that we can't
4893 * destroy a held snapshot, mark for deferred destroy,
4894 * release hold, verify snapshot was destroyed.
4896 error = dmu_objset_snapshot_one(osname, snapname);
4898 if (error == ENOSPC) {
4899 ztest_record_enospc("dmu_objset_snapshot");
4902 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4905 holds = fnvlist_alloc();
4906 fnvlist_add_string(holds, fullname, tag);
4907 error = dsl_dataset_user_hold(holds, 0, NULL);
4908 fnvlist_free(holds);
4910 if (error == ENOSPC) {
4911 ztest_record_enospc("dsl_dataset_user_hold");
4914 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
4915 fullname, tag, error);
4918 error = dsl_destroy_snapshot(fullname, B_FALSE);
4919 if (error != EBUSY) {
4920 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4924 error = dsl_destroy_snapshot(fullname, B_TRUE);
4926 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4930 error = user_release_one(fullname, tag);
4932 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4934 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4937 (void) rw_unlock(&ztest_name_lock);
4941 * Inject random faults into the on-disk data.
4945 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4947 ztest_shared_t *zs = ztest_shared;
4948 spa_t *spa = ztest_spa;
4952 uint64_t bad = 0x1990c0ffeedecadeull;
4957 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4963 boolean_t islog = B_FALSE;
4965 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4966 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4968 mutex_enter(&ztest_vdev_lock);
4969 maxfaults = MAXFAULTS();
4970 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4971 mirror_save = zs->zs_mirrors;
4972 mutex_exit(&ztest_vdev_lock);
4974 ASSERT(leaves >= 1);
4977 * Grab the name lock as reader. There are some operations
4978 * which don't like to have their vdevs changed while
4979 * they are in progress (i.e. spa_change_guid). Those
4980 * operations will have grabbed the name lock as writer.
4982 (void) rw_rdlock(&ztest_name_lock);
4985 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4987 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4989 if (ztest_random(2) == 0) {
4991 * Inject errors on a normal data device or slog device.
4993 top = ztest_random_vdev_top(spa, B_TRUE);
4994 leaf = ztest_random(leaves) + zs->zs_splits;
4997 * Generate paths to the first leaf in this top-level vdev,
4998 * and to the random leaf we selected. We'll induce transient
4999 * write failures and random online/offline activity on leaf 0,
5000 * and we'll write random garbage to the randomly chosen leaf.
5002 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5003 ztest_opts.zo_dir, ztest_opts.zo_pool,
5004 top * leaves + zs->zs_splits);
5005 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5006 ztest_opts.zo_dir, ztest_opts.zo_pool,
5007 top * leaves + leaf);
5009 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5010 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5014 * If the top-level vdev needs to be resilvered
5015 * then we only allow faults on the device that is
5018 if (vd0 != NULL && maxfaults != 1 &&
5019 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5020 vd0->vdev_resilver_txg != 0)) {
5022 * Make vd0 explicitly claim to be unreadable,
5023 * or unwriteable, or reach behind its back
5024 * and close the underlying fd. We can do this if
5025 * maxfaults == 0 because we'll fail and reexecute,
5026 * and we can do it if maxfaults >= 2 because we'll
5027 * have enough redundancy. If maxfaults == 1, the
5028 * combination of this with injection of random data
5029 * corruption below exceeds the pool's fault tolerance.
5031 vdev_file_t *vf = vd0->vdev_tsd;
5033 if (vf != NULL && ztest_random(3) == 0) {
5034 (void) close(vf->vf_vnode->v_fd);
5035 vf->vf_vnode->v_fd = -1;
5036 } else if (ztest_random(2) == 0) {
5037 vd0->vdev_cant_read = B_TRUE;
5039 vd0->vdev_cant_write = B_TRUE;
5041 guid0 = vd0->vdev_guid;
5045 * Inject errors on an l2cache device.
5047 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5049 if (sav->sav_count == 0) {
5050 spa_config_exit(spa, SCL_STATE, FTAG);
5051 (void) rw_unlock(&ztest_name_lock);
5054 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5055 guid0 = vd0->vdev_guid;
5056 (void) strcpy(path0, vd0->vdev_path);
5057 (void) strcpy(pathrand, vd0->vdev_path);
5061 maxfaults = INT_MAX; /* no limit on cache devices */
5064 spa_config_exit(spa, SCL_STATE, FTAG);
5065 (void) rw_unlock(&ztest_name_lock);
5068 * If we can tolerate two or more faults, or we're dealing
5069 * with a slog, randomly online/offline vd0.
5071 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5072 if (ztest_random(10) < 6) {
5073 int flags = (ztest_random(2) == 0 ?
5074 ZFS_OFFLINE_TEMPORARY : 0);
5077 * We have to grab the zs_name_lock as writer to
5078 * prevent a race between offlining a slog and
5079 * destroying a dataset. Offlining the slog will
5080 * grab a reference on the dataset which may cause
5081 * dsl_destroy_head() to fail with EBUSY thus
5082 * leaving the dataset in an inconsistent state.
5085 (void) rw_wrlock(&ztest_name_lock);
5087 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5090 (void) rw_unlock(&ztest_name_lock);
5093 * Ideally we would like to be able to randomly
5094 * call vdev_[on|off]line without holding locks
5095 * to force unpredictable failures but the side
5096 * effects of vdev_[on|off]line prevent us from
5097 * doing so. We grab the ztest_vdev_lock here to
5098 * prevent a race between injection testing and
5101 mutex_enter(&ztest_vdev_lock);
5102 (void) vdev_online(spa, guid0, 0, NULL);
5103 mutex_exit(&ztest_vdev_lock);
5111 * We have at least single-fault tolerance, so inject data corruption.
5113 fd = open(pathrand, O_RDWR);
5115 if (fd == -1) /* we hit a gap in the device namespace */
5118 fsize = lseek(fd, 0, SEEK_END);
5120 while (--iters != 0) {
5121 offset = ztest_random(fsize / (leaves << bshift)) *
5122 (leaves << bshift) + (leaf << bshift) +
5123 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5125 if (offset >= fsize)
5128 mutex_enter(&ztest_vdev_lock);
5129 if (mirror_save != zs->zs_mirrors) {
5130 mutex_exit(&ztest_vdev_lock);
5135 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5136 fatal(1, "can't inject bad word at 0x%llx in %s",
5139 mutex_exit(&ztest_vdev_lock);
5141 if (ztest_opts.zo_verbose >= 7)
5142 (void) printf("injected bad word into %s,"
5143 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5148 umem_free(path0, MAXPATHLEN);
5149 umem_free(pathrand, MAXPATHLEN);
5153 * Verify that DDT repair works as expected.
5156 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5158 ztest_shared_t *zs = ztest_shared;
5159 spa_t *spa = ztest_spa;
5160 objset_t *os = zd->zd_os;
5162 uint64_t object, blocksize, txg, pattern, psize;
5163 enum zio_checksum checksum = spa_dedup_checksum(spa);
5168 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5171 blocksize = ztest_random_blocksize();
5172 blocksize = MIN(blocksize, 2048); /* because we write so many */
5174 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5175 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5177 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5178 umem_free(od, sizeof (ztest_od_t));
5183 * Take the name lock as writer to prevent anyone else from changing
5184 * the pool and dataset properies we need to maintain during this test.
5186 (void) rw_wrlock(&ztest_name_lock);
5188 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5190 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5192 (void) rw_unlock(&ztest_name_lock);
5193 umem_free(od, sizeof (ztest_od_t));
5197 object = od[0].od_object;
5198 blocksize = od[0].od_blocksize;
5199 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5201 ASSERT(object != 0);
5203 tx = dmu_tx_create(os);
5204 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5205 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5207 (void) rw_unlock(&ztest_name_lock);
5208 umem_free(od, sizeof (ztest_od_t));
5213 * Write all the copies of our block.
5215 for (i = 0; i < copies; i++) {
5216 uint64_t offset = i * blocksize;
5217 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5218 DMU_READ_NO_PREFETCH);
5220 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5221 os, (long long)object, (long long) offset, error);
5223 ASSERT(db->db_offset == offset);
5224 ASSERT(db->db_size == blocksize);
5225 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5226 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5227 dmu_buf_will_fill(db, tx);
5228 ztest_pattern_set(db->db_data, db->db_size, pattern);
5229 dmu_buf_rele(db, FTAG);
5233 txg_wait_synced(spa_get_dsl(spa), txg);
5236 * Find out what block we got.
5238 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5239 DMU_READ_NO_PREFETCH));
5240 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5241 dmu_buf_rele(db, FTAG);
5244 * Damage the block. Dedup-ditto will save us when we read it later.
5246 psize = BP_GET_PSIZE(&blk);
5247 buf = zio_buf_alloc(psize);
5248 ztest_pattern_set(buf, psize, ~pattern);
5250 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5251 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5252 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5254 zio_buf_free(buf, psize);
5256 (void) rw_unlock(&ztest_name_lock);
5257 umem_free(od, sizeof (ztest_od_t));
5265 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5267 spa_t *spa = ztest_spa;
5269 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5270 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5271 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5275 * Change the guid for the pool.
5279 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5281 spa_t *spa = ztest_spa;
5282 uint64_t orig, load;
5285 orig = spa_guid(spa);
5286 load = spa_load_guid(spa);
5288 (void) rw_wrlock(&ztest_name_lock);
5289 error = spa_change_guid(spa);
5290 (void) rw_unlock(&ztest_name_lock);
5295 if (ztest_opts.zo_verbose >= 4) {
5296 (void) printf("Changed guid old %llu -> %llu\n",
5297 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5300 VERIFY3U(orig, !=, spa_guid(spa));
5301 VERIFY3U(load, ==, spa_load_guid(spa));
5305 * Rename the pool to a different name and then rename it back.
5309 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5311 char *oldname, *newname;
5314 (void) rw_wrlock(&ztest_name_lock);
5316 oldname = ztest_opts.zo_pool;
5317 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5318 (void) strcpy(newname, oldname);
5319 (void) strcat(newname, "_tmp");
5324 VERIFY3U(0, ==, spa_rename(oldname, newname));
5327 * Try to open it under the old name, which shouldn't exist
5329 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5332 * Open it under the new name and make sure it's still the same spa_t.
5334 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5336 ASSERT(spa == ztest_spa);
5337 spa_close(spa, FTAG);
5340 * Rename it back to the original
5342 VERIFY3U(0, ==, spa_rename(newname, oldname));
5345 * Make sure it can still be opened
5347 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5349 ASSERT(spa == ztest_spa);
5350 spa_close(spa, FTAG);
5352 umem_free(newname, strlen(newname) + 1);
5354 (void) rw_unlock(&ztest_name_lock);
5358 * Verify pool integrity by running zdb.
5361 ztest_run_zdb(char *pool)
5369 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5370 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5371 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5373 VERIFY(realpath(getexecname(), bin) != NULL);
5374 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5375 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5376 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5377 strcpy(bin, "/sbin/zdb"); /* Installed */
5379 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5380 strcat(bin, "/zdb/zdb");
5384 "%s -bcc%s%s -d -U %s %s",
5386 ztest_opts.zo_verbose >= 3 ? "s" : "",
5387 ztest_opts.zo_verbose >= 4 ? "v" : "",
5391 if (ztest_opts.zo_verbose >= 5)
5392 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5394 fp = popen(zdb, "r");
5396 while (fgets(zbuf, 1024, fp) != NULL)
5397 if (ztest_opts.zo_verbose >= 3)
5398 (void) printf("%s", zbuf);
5400 status = pclose(fp);
5405 ztest_dump_core = 0;
5406 if (WIFEXITED(status))
5407 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5409 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5411 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5412 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5413 umem_free(zbuf, 1024);
5417 ztest_walk_pool_directory(char *header)
5421 if (ztest_opts.zo_verbose >= 6)
5422 (void) printf("%s\n", header);
5424 mutex_enter(&spa_namespace_lock);
5425 while ((spa = spa_next(spa)) != NULL)
5426 if (ztest_opts.zo_verbose >= 6)
5427 (void) printf("\t%s\n", spa_name(spa));
5428 mutex_exit(&spa_namespace_lock);
5432 ztest_spa_import_export(char *oldname, char *newname)
5434 nvlist_t *config, *newconfig;
5439 if (ztest_opts.zo_verbose >= 4) {
5440 (void) printf("import/export: old = %s, new = %s\n",
5445 * Clean up from previous runs.
5447 (void) spa_destroy(newname);
5450 * Get the pool's configuration and guid.
5452 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5455 * Kick off a scrub to tickle scrub/export races.
5457 if (ztest_random(2) == 0)
5458 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5460 pool_guid = spa_guid(spa);
5461 spa_close(spa, FTAG);
5463 ztest_walk_pool_directory("pools before export");
5468 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5470 ztest_walk_pool_directory("pools after export");
5475 newconfig = spa_tryimport(config);
5476 ASSERT(newconfig != NULL);
5477 nvlist_free(newconfig);
5480 * Import it under the new name.
5482 error = spa_import(newname, config, NULL, 0);
5484 dump_nvlist(config, 0);
5485 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5486 oldname, newname, error);
5489 ztest_walk_pool_directory("pools after import");
5492 * Try to import it again -- should fail with EEXIST.
5494 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5497 * Try to import it under a different name -- should fail with EEXIST.
5499 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5502 * Verify that the pool is no longer visible under the old name.
5504 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5507 * Verify that we can open and close the pool using the new name.
5509 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5510 ASSERT(pool_guid == spa_guid(spa));
5511 spa_close(spa, FTAG);
5513 nvlist_free(config);
5517 ztest_resume(spa_t *spa)
5519 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5520 (void) printf("resuming from suspended state\n");
5521 spa_vdev_state_enter(spa, SCL_NONE);
5522 vdev_clear(spa, NULL);
5523 (void) spa_vdev_state_exit(spa, NULL, 0);
5524 (void) zio_resume(spa);
5528 ztest_resume_thread(void *arg)
5532 while (!ztest_exiting) {
5533 if (spa_suspended(spa))
5535 (void) poll(NULL, 0, 100);
5547 ztest_deadman_alarm(int sig)
5549 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5554 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5556 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5557 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5558 hrtime_t functime = gethrtime();
5561 for (i = 0; i < zi->zi_iters; i++)
5562 zi->zi_func(zd, id);
5564 functime = gethrtime() - functime;
5566 atomic_add_64(&zc->zc_count, 1);
5567 atomic_add_64(&zc->zc_time, functime);
5569 if (ztest_opts.zo_verbose >= 4) {
5571 (void) dladdr((void *)zi->zi_func, &dli);
5572 (void) printf("%6.2f sec in %s\n",
5573 (double)functime / NANOSEC, dli.dli_sname);
5578 ztest_thread(void *arg)
5581 uint64_t id = (uintptr_t)arg;
5582 ztest_shared_t *zs = ztest_shared;
5586 ztest_shared_callstate_t *zc;
5588 while ((now = gethrtime()) < zs->zs_thread_stop) {
5590 * See if it's time to force a crash.
5592 if (now > zs->zs_thread_kill)
5596 * If we're getting ENOSPC with some regularity, stop.
5598 if (zs->zs_enospc_count > 10)
5602 * Pick a random function to execute.
5604 rand = ztest_random(ZTEST_FUNCS);
5605 zi = &ztest_info[rand];
5606 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5607 call_next = zc->zc_next;
5609 if (now >= call_next &&
5610 atomic_cas_64(&zc->zc_next, call_next, call_next +
5611 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5612 ztest_execute(rand, zi, id);
5622 ztest_dataset_name(char *dsname, char *pool, int d)
5624 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5628 ztest_dataset_destroy(int d)
5630 char name[MAXNAMELEN];
5633 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5635 if (ztest_opts.zo_verbose >= 3)
5636 (void) printf("Destroying %s to free up space\n", name);
5639 * Cleanup any non-standard clones and snapshots. In general,
5640 * ztest thread t operates on dataset (t % zopt_datasets),
5641 * so there may be more than one thing to clean up.
5643 for (t = d; t < ztest_opts.zo_threads;
5644 t += ztest_opts.zo_datasets)
5645 ztest_dsl_dataset_cleanup(name, t);
5647 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5648 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5652 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5654 uint64_t usedobjs, dirobjs, scratch;
5657 * ZTEST_DIROBJ is the object directory for the entire dataset.
5658 * Therefore, the number of objects in use should equal the
5659 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5660 * If not, we have an object leak.
5662 * Note that we can only check this in ztest_dataset_open(),
5663 * when the open-context and syncing-context values agree.
5664 * That's because zap_count() returns the open-context value,
5665 * while dmu_objset_space() returns the rootbp fill count.
5667 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5668 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5669 ASSERT3U(dirobjs + 1, ==, usedobjs);
5673 ztest_dataset_open(int d)
5675 ztest_ds_t *zd = &ztest_ds[d];
5676 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5679 char name[MAXNAMELEN];
5682 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5684 (void) rw_rdlock(&ztest_name_lock);
5686 error = ztest_dataset_create(name);
5687 if (error == ENOSPC) {
5688 (void) rw_unlock(&ztest_name_lock);
5689 ztest_record_enospc(FTAG);
5692 ASSERT(error == 0 || error == EEXIST);
5694 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5695 (void) rw_unlock(&ztest_name_lock);
5697 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5699 zilog = zd->zd_zilog;
5701 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5702 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5703 fatal(0, "missing log records: claimed %llu < committed %llu",
5704 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5706 ztest_dataset_dirobj_verify(zd);
5708 zil_replay(os, zd, ztest_replay_vector);
5710 ztest_dataset_dirobj_verify(zd);
5712 if (ztest_opts.zo_verbose >= 6)
5713 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5715 (u_longlong_t)zilog->zl_parse_blk_count,
5716 (u_longlong_t)zilog->zl_parse_lr_count,
5717 (u_longlong_t)zilog->zl_replaying_seq);
5719 zilog = zil_open(os, ztest_get_data);
5721 if (zilog->zl_replaying_seq != 0 &&
5722 zilog->zl_replaying_seq < committed_seq)
5723 fatal(0, "missing log records: replayed %llu < committed %llu",
5724 zilog->zl_replaying_seq, committed_seq);
5730 ztest_dataset_close(int d)
5732 ztest_ds_t *zd = &ztest_ds[d];
5734 zil_close(zd->zd_zilog);
5735 dmu_objset_disown(zd->zd_os, zd);
5741 * Kick off threads to run tests on all datasets in parallel.
5744 ztest_run(ztest_shared_t *zs)
5749 kthread_t *resume_thread;
5754 ztest_exiting = B_FALSE;
5757 * Initialize parent/child shared state.
5759 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5760 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5762 zs->zs_thread_start = gethrtime();
5763 zs->zs_thread_stop =
5764 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5765 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5766 zs->zs_thread_kill = zs->zs_thread_stop;
5767 if (ztest_random(100) < ztest_opts.zo_killrate) {
5768 zs->zs_thread_kill -=
5769 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5772 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5774 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5775 offsetof(ztest_cb_data_t, zcd_node));
5780 kernel_init(FREAD | FWRITE);
5781 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5782 spa->spa_debug = B_TRUE;
5783 metaslab_preload_limit = ztest_random(20) + 1;
5786 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5787 DMU_OST_ANY, B_TRUE, FTAG, &os));
5788 zs->zs_guid = dmu_objset_fsid_guid(os);
5789 dmu_objset_disown(os, FTAG);
5791 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5794 * We don't expect the pool to suspend unless maxfaults == 0,
5795 * in which case ztest_fault_inject() temporarily takes away
5796 * the only valid replica.
5798 if (MAXFAULTS() == 0)
5799 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5801 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5804 * Create a thread to periodically resume suspended I/O.
5806 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5807 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5808 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5812 * Set a deadman alarm to abort() if we hang.
5814 signal(SIGALRM, ztest_deadman_alarm);
5815 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5819 * Verify that we can safely inquire about about any object,
5820 * whether it's allocated or not. To make it interesting,
5821 * we probe a 5-wide window around each power of two.
5822 * This hits all edge cases, including zero and the max.
5824 for (t = 0; t < 64; t++) {
5825 for (d = -5; d <= 5; d++) {
5826 error = dmu_object_info(spa->spa_meta_objset,
5827 (1ULL << t) + d, NULL);
5828 ASSERT(error == 0 || error == ENOENT ||
5834 * If we got any ENOSPC errors on the previous run, destroy something.
5836 if (zs->zs_enospc_count != 0) {
5837 int d = ztest_random(ztest_opts.zo_datasets);
5838 ztest_dataset_destroy(d);
5840 zs->zs_enospc_count = 0;
5842 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5845 if (ztest_opts.zo_verbose >= 4)
5846 (void) printf("starting main threads...\n");
5849 * Kick off all the tests that run in parallel.
5851 for (t = 0; t < ztest_opts.zo_threads; t++) {
5854 if (t < ztest_opts.zo_datasets &&
5855 ztest_dataset_open(t) != 0)
5858 VERIFY3P(thread = zk_thread_create(NULL, 0,
5859 (thread_func_t)ztest_thread,
5860 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5861 PTHREAD_CREATE_JOINABLE), !=, NULL);
5862 tid[t] = thread->t_tid;
5866 * Wait for all of the tests to complete. We go in reverse order
5867 * so we don't close datasets while threads are still using them.
5869 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5870 thread_join(tid[t]);
5871 if (t < ztest_opts.zo_datasets)
5872 ztest_dataset_close(t);
5875 txg_wait_synced(spa_get_dsl(spa), 0);
5877 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5878 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5880 if (ztest_opts.zo_verbose >= 3)
5881 zfs_dbgmsg_print(FTAG);
5883 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5885 /* Kill the resume thread */
5886 ztest_exiting = B_TRUE;
5887 thread_join(resume_thread->t_tid);
5891 * Right before closing the pool, kick off a bunch of async I/O;
5892 * spa_close() should wait for it to complete.
5894 for (object = 1; object < 50; object++)
5895 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5897 /* Verify that at least one commit cb was called in a timely fashion */
5898 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5899 VERIFY0(zc_min_txg_delay);
5901 spa_close(spa, FTAG);
5904 * Verify that we can loop over all pools.
5906 mutex_enter(&spa_namespace_lock);
5907 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5908 if (ztest_opts.zo_verbose > 3)
5909 (void) printf("spa_next: found %s\n", spa_name(spa));
5910 mutex_exit(&spa_namespace_lock);
5913 * Verify that we can export the pool and reimport it under a
5916 if (ztest_random(2) == 0) {
5917 char name[MAXNAMELEN];
5918 (void) snprintf(name, MAXNAMELEN, "%s_import",
5919 ztest_opts.zo_pool);
5920 ztest_spa_import_export(ztest_opts.zo_pool, name);
5921 ztest_spa_import_export(name, ztest_opts.zo_pool);
5926 list_destroy(&zcl.zcl_callbacks);
5927 mutex_destroy(&zcl.zcl_callbacks_lock);
5928 (void) rwlock_destroy(&ztest_name_lock);
5929 mutex_destroy(&ztest_vdev_lock);
5935 ztest_ds_t *zd = &ztest_ds[0];
5939 if (ztest_opts.zo_verbose >= 3)
5940 (void) printf("testing spa_freeze()...\n");
5942 kernel_init(FREAD | FWRITE);
5943 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5944 VERIFY3U(0, ==, ztest_dataset_open(0));
5945 spa->spa_debug = B_TRUE;
5949 * Force the first log block to be transactionally allocated.
5950 * We have to do this before we freeze the pool -- otherwise
5951 * the log chain won't be anchored.
5953 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5954 ztest_dmu_object_alloc_free(zd, 0);
5955 zil_commit(zd->zd_zilog, 0);
5958 txg_wait_synced(spa_get_dsl(spa), 0);
5961 * Freeze the pool. This stops spa_sync() from doing anything,
5962 * so that the only way to record changes from now on is the ZIL.
5967 * Run tests that generate log records but don't alter the pool config
5968 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5969 * We do a txg_wait_synced() after each iteration to force the txg
5970 * to increase well beyond the last synced value in the uberblock.
5971 * The ZIL should be OK with that.
5973 while (ztest_random(10) != 0 &&
5974 numloops++ < ztest_opts.zo_maxloops) {
5975 ztest_dmu_write_parallel(zd, 0);
5976 ztest_dmu_object_alloc_free(zd, 0);
5977 txg_wait_synced(spa_get_dsl(spa), 0);
5981 * Commit all of the changes we just generated.
5983 zil_commit(zd->zd_zilog, 0);
5984 txg_wait_synced(spa_get_dsl(spa), 0);
5987 * Close our dataset and close the pool.
5989 ztest_dataset_close(0);
5990 spa_close(spa, FTAG);
5994 * Open and close the pool and dataset to induce log replay.
5996 kernel_init(FREAD | FWRITE);
5997 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5998 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
5999 VERIFY3U(0, ==, ztest_dataset_open(0));
6000 ztest_dataset_close(0);
6002 spa->spa_debug = B_TRUE;
6004 txg_wait_synced(spa_get_dsl(spa), 0);
6005 ztest_reguid(NULL, 0);
6007 spa_close(spa, FTAG);
6012 print_time(hrtime_t t, char *timebuf)
6014 hrtime_t s = t / NANOSEC;
6015 hrtime_t m = s / 60;
6016 hrtime_t h = m / 60;
6017 hrtime_t d = h / 24;
6026 (void) sprintf(timebuf,
6027 "%llud%02lluh%02llum%02llus", d, h, m, s);
6029 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6031 (void) sprintf(timebuf, "%llum%02llus", m, s);
6033 (void) sprintf(timebuf, "%llus", s);
6037 make_random_props(void)
6041 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6042 if (ztest_random(2) == 0)
6044 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6050 * Create a storage pool with the given name and initial vdev size.
6051 * Then test spa_freeze() functionality.
6054 ztest_init(ztest_shared_t *zs)
6057 nvlist_t *nvroot, *props;
6060 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6061 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6063 kernel_init(FREAD | FWRITE);
6066 * Create the storage pool.
6068 (void) spa_destroy(ztest_opts.zo_pool);
6069 ztest_shared->zs_vdev_next_leaf = 0;
6071 zs->zs_mirrors = ztest_opts.zo_mirrors;
6072 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6073 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6074 props = make_random_props();
6075 for (i = 0; i < SPA_FEATURES; i++) {
6077 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6078 spa_feature_table[i].fi_uname));
6079 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6082 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6083 nvlist_free(nvroot);
6086 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6087 zs->zs_metaslab_sz =
6088 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6089 spa_close(spa, FTAG);
6093 ztest_run_zdb(ztest_opts.zo_pool);
6097 ztest_run_zdb(ztest_opts.zo_pool);
6099 (void) rwlock_destroy(&ztest_name_lock);
6100 mutex_destroy(&ztest_vdev_lock);
6106 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6108 ztest_fd_data = mkstemp(ztest_name_data);
6109 ASSERT3S(ztest_fd_data, >=, 0);
6110 (void) unlink(ztest_name_data);
6114 shared_data_size(ztest_shared_hdr_t *hdr)
6118 size = hdr->zh_hdr_size;
6119 size += hdr->zh_opts_size;
6120 size += hdr->zh_size;
6121 size += hdr->zh_stats_size * hdr->zh_stats_count;
6122 size += hdr->zh_ds_size * hdr->zh_ds_count;
6131 ztest_shared_hdr_t *hdr;
6133 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6134 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6135 VERIFY3P(hdr, !=, MAP_FAILED);
6137 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6139 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6140 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6141 hdr->zh_size = sizeof (ztest_shared_t);
6142 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6143 hdr->zh_stats_count = ZTEST_FUNCS;
6144 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6145 hdr->zh_ds_count = ztest_opts.zo_datasets;
6147 size = shared_data_size(hdr);
6148 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6150 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6157 ztest_shared_hdr_t *hdr;
6160 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6161 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6162 VERIFY3P(hdr, !=, MAP_FAILED);
6164 size = shared_data_size(hdr);
6166 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6167 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6168 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6169 VERIFY3P(hdr, !=, MAP_FAILED);
6170 buf = (uint8_t *)hdr;
6172 offset = hdr->zh_hdr_size;
6173 ztest_shared_opts = (void *)&buf[offset];
6174 offset += hdr->zh_opts_size;
6175 ztest_shared = (void *)&buf[offset];
6176 offset += hdr->zh_size;
6177 ztest_shared_callstate = (void *)&buf[offset];
6178 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6179 ztest_shared_ds = (void *)&buf[offset];
6183 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6187 char *cmdbuf = NULL;
6192 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6193 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6198 fatal(1, "fork failed");
6200 if (pid == 0) { /* child */
6201 char *emptyargv[2] = { cmd, NULL };
6202 char fd_data_str[12];
6204 struct rlimit rl = { 1024, 1024 };
6205 (void) setrlimit(RLIMIT_NOFILE, &rl);
6207 (void) close(ztest_fd_rand);
6208 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6209 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6211 (void) enable_extended_FILE_stdio(-1, -1);
6212 if (libpath != NULL)
6213 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6214 (void) execv(cmd, emptyargv);
6215 ztest_dump_core = B_FALSE;
6216 fatal(B_TRUE, "exec failed: %s", cmd);
6219 if (cmdbuf != NULL) {
6220 umem_free(cmdbuf, MAXPATHLEN);
6224 while (waitpid(pid, &status, 0) != pid)
6226 if (statusp != NULL)
6229 if (WIFEXITED(status)) {
6230 if (WEXITSTATUS(status) != 0) {
6231 (void) fprintf(stderr, "child exited with code %d\n",
6232 WEXITSTATUS(status));
6236 } else if (WIFSIGNALED(status)) {
6237 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6238 (void) fprintf(stderr, "child died with signal %d\n",
6244 (void) fprintf(stderr, "something strange happened to child\n");
6251 ztest_run_init(void)
6255 ztest_shared_t *zs = ztest_shared;
6257 ASSERT(ztest_opts.zo_init != 0);
6260 * Blow away any existing copy of zpool.cache
6262 (void) remove(spa_config_path);
6265 * Create and initialize our storage pool.
6267 for (i = 1; i <= ztest_opts.zo_init; i++) {
6268 bzero(zs, sizeof (ztest_shared_t));
6269 if (ztest_opts.zo_verbose >= 3 &&
6270 ztest_opts.zo_init != 1) {
6271 (void) printf("ztest_init(), pass %d\n", i);
6278 main(int argc, char **argv)
6286 ztest_shared_callstate_t *zc;
6293 char *fd_data_str = getenv("ZTEST_FD_DATA");
6294 struct sigaction action;
6296 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6298 dprintf_setup(&argc, argv);
6300 action.sa_handler = sig_handler;
6301 sigemptyset(&action.sa_mask);
6302 action.sa_flags = 0;
6304 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6305 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6310 if (sigaction(SIGABRT, &action, NULL) < 0) {
6311 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6316 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6317 ASSERT3S(ztest_fd_rand, >=, 0);
6320 process_options(argc, argv);
6325 bcopy(&ztest_opts, ztest_shared_opts,
6326 sizeof (*ztest_shared_opts));
6328 ztest_fd_data = atoi(fd_data_str);
6330 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6332 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6334 /* Override location of zpool.cache */
6335 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6336 ztest_opts.zo_dir) != -1);
6338 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6343 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6344 metaslab_df_alloc_threshold =
6345 zs->zs_metaslab_df_alloc_threshold;
6354 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6356 if (ztest_opts.zo_verbose >= 1) {
6357 (void) printf("%llu vdevs, %d datasets, %d threads,"
6358 " %llu seconds...\n",
6359 (u_longlong_t)ztest_opts.zo_vdevs,
6360 ztest_opts.zo_datasets,
6361 ztest_opts.zo_threads,
6362 (u_longlong_t)ztest_opts.zo_time);
6365 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6366 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6368 zs->zs_do_init = B_TRUE;
6369 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6370 if (ztest_opts.zo_verbose >= 1) {
6371 (void) printf("Executing older ztest for "
6372 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6374 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6375 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6377 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6379 zs->zs_do_init = B_FALSE;
6381 zs->zs_proc_start = gethrtime();
6382 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6384 for (f = 0; f < ZTEST_FUNCS; f++) {
6385 zi = &ztest_info[f];
6386 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6387 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6388 zc->zc_next = UINT64_MAX;
6390 zc->zc_next = zs->zs_proc_start +
6391 ztest_random(2 * zi->zi_interval[0] + 1);
6395 * Run the tests in a loop. These tests include fault injection
6396 * to verify that self-healing data works, and forced crashes
6397 * to verify that we never lose on-disk consistency.
6399 while (gethrtime() < zs->zs_proc_stop) {
6404 * Initialize the workload counters for each function.
6406 for (f = 0; f < ZTEST_FUNCS; f++) {
6407 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6412 /* Set the allocation switch size */
6413 zs->zs_metaslab_df_alloc_threshold =
6414 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6416 if (!hasalt || ztest_random(2) == 0) {
6417 if (hasalt && ztest_opts.zo_verbose >= 1) {
6418 (void) printf("Executing newer ztest: %s\n",
6422 killed = exec_child(cmd, NULL, B_TRUE, &status);
6424 if (hasalt && ztest_opts.zo_verbose >= 1) {
6425 (void) printf("Executing older ztest: %s\n",
6426 ztest_opts.zo_alt_ztest);
6429 killed = exec_child(ztest_opts.zo_alt_ztest,
6430 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6437 if (ztest_opts.zo_verbose >= 1) {
6438 hrtime_t now = gethrtime();
6440 now = MIN(now, zs->zs_proc_stop);
6441 print_time(zs->zs_proc_stop - now, timebuf);
6442 nicenum(zs->zs_space, numbuf);
6444 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6445 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6447 WIFEXITED(status) ? "Complete" : "SIGKILL",
6448 (u_longlong_t)zs->zs_enospc_count,
6449 100.0 * zs->zs_alloc / zs->zs_space,
6451 100.0 * (now - zs->zs_proc_start) /
6452 (ztest_opts.zo_time * NANOSEC), timebuf);
6455 if (ztest_opts.zo_verbose >= 2) {
6456 (void) printf("\nWorkload summary:\n\n");
6457 (void) printf("%7s %9s %s\n",
6458 "Calls", "Time", "Function");
6459 (void) printf("%7s %9s %s\n",
6460 "-----", "----", "--------");
6461 for (f = 0; f < ZTEST_FUNCS; f++) {
6464 zi = &ztest_info[f];
6465 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6466 print_time(zc->zc_time, timebuf);
6467 (void) dladdr((void *)zi->zi_func, &dli);
6468 (void) printf("%7llu %9s %s\n",
6469 (u_longlong_t)zc->zc_count, timebuf,
6472 (void) printf("\n");
6476 * It's possible that we killed a child during a rename test,
6477 * in which case we'll have a 'ztest_tmp' pool lying around
6478 * instead of 'ztest'. Do a blind rename in case this happened.
6481 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6482 spa_close(spa, FTAG);
6484 char tmpname[MAXNAMELEN];
6486 kernel_init(FREAD | FWRITE);
6487 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6488 ztest_opts.zo_pool);
6489 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6493 ztest_run_zdb(ztest_opts.zo_pool);
6496 if (ztest_opts.zo_verbose >= 1) {
6498 (void) printf("%d runs of older ztest: %s\n", older,
6499 ztest_opts.zo_alt_ztest);
6500 (void) printf("%d runs of newer ztest: %s\n", newer,
6503 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6504 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6507 umem_free(cmd, MAXNAMELEN);