4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatibility
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/zfs_rlock.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/spa_impl.h>
108 #include <sys/metaslab_impl.h>
109 #include <sys/dsl_prop.h>
110 #include <sys/dsl_dataset.h>
111 #include <sys/dsl_destroy.h>
112 #include <sys/dsl_scan.h>
113 #include <sys/zio_checksum.h>
114 #include <sys/refcount.h>
115 #include <sys/zfeature.h>
116 #include <sys/dsl_userhold.h>
118 #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 * 2,
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 {
247 #define ZTEST_RANGE_LOCKS 64
248 #define ZTEST_OBJECT_LOCKS 64
251 * Object descriptor. Used as a template for object lookup/create/remove.
253 typedef struct ztest_od {
256 dmu_object_type_t od_type;
257 dmu_object_type_t od_crtype;
258 uint64_t od_blocksize;
259 uint64_t od_crblocksize;
262 char od_name[MAXNAMELEN];
268 typedef struct ztest_ds {
269 ztest_shared_ds_t *zd_shared;
271 rwlock_t zd_zilog_lock;
273 ztest_od_t *zd_od; /* debugging aid */
274 char zd_name[MAXNAMELEN];
275 kmutex_t zd_dirobj_lock;
276 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
277 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
281 * Per-iteration state.
283 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
285 typedef struct ztest_info {
286 ztest_func_t *zi_func; /* test function */
287 uint64_t zi_iters; /* iterations per execution */
288 uint64_t *zi_interval; /* execute every <interval> seconds */
289 const char *zi_funcname; /* name of test function */
292 typedef struct ztest_shared_callstate {
293 uint64_t zc_count; /* per-pass count */
294 uint64_t zc_time; /* per-pass time */
295 uint64_t zc_next; /* next time to call this function */
296 } ztest_shared_callstate_t;
298 static ztest_shared_callstate_t *ztest_shared_callstate;
299 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
301 ztest_func_t ztest_dmu_read_write;
302 ztest_func_t ztest_dmu_write_parallel;
303 ztest_func_t ztest_dmu_object_alloc_free;
304 ztest_func_t ztest_dmu_commit_callbacks;
305 ztest_func_t ztest_zap;
306 ztest_func_t ztest_zap_parallel;
307 ztest_func_t ztest_zil_commit;
308 ztest_func_t ztest_zil_remount;
309 ztest_func_t ztest_dmu_read_write_zcopy;
310 ztest_func_t ztest_dmu_objset_create_destroy;
311 ztest_func_t ztest_dmu_prealloc;
312 ztest_func_t ztest_fzap;
313 ztest_func_t ztest_dmu_snapshot_create_destroy;
314 ztest_func_t ztest_dsl_prop_get_set;
315 ztest_func_t ztest_spa_prop_get_set;
316 ztest_func_t ztest_spa_create_destroy;
317 ztest_func_t ztest_fault_inject;
318 ztest_func_t ztest_ddt_repair;
319 ztest_func_t ztest_dmu_snapshot_hold;
320 ztest_func_t ztest_spa_rename;
321 ztest_func_t ztest_scrub;
322 ztest_func_t ztest_dsl_dataset_promote_busy;
323 ztest_func_t ztest_vdev_attach_detach;
324 ztest_func_t ztest_vdev_LUN_growth;
325 ztest_func_t ztest_vdev_add_remove;
326 ztest_func_t ztest_vdev_aux_add_remove;
327 ztest_func_t ztest_split_pool;
328 ztest_func_t ztest_reguid;
329 ztest_func_t ztest_spa_upgrade;
331 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
332 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
333 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
334 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
335 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
337 #define ZTI_INIT(func, iters, interval) \
338 { .zi_func = (func), \
339 .zi_iters = (iters), \
340 .zi_interval = (interval), \
341 .zi_funcname = # func }
343 ztest_info_t ztest_info[] = {
344 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
345 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
346 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
347 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
348 ZTI_INIT(ztest_zap, 30, &zopt_always),
349 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
350 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
351 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
352 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
353 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
354 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
355 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
356 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
358 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
360 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
361 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
362 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
363 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
364 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
367 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
368 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
369 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
370 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
371 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
372 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
373 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
374 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
377 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
380 * The following struct is used to hold a list of uncalled commit callbacks.
381 * The callbacks are ordered by txg number.
383 typedef struct ztest_cb_list {
384 kmutex_t zcl_callbacks_lock;
385 list_t zcl_callbacks;
389 * Stuff we need to share writably between parent and child.
391 typedef struct ztest_shared {
392 boolean_t zs_do_init;
393 hrtime_t zs_proc_start;
394 hrtime_t zs_proc_stop;
395 hrtime_t zs_thread_start;
396 hrtime_t zs_thread_stop;
397 hrtime_t zs_thread_kill;
398 uint64_t zs_enospc_count;
399 uint64_t zs_vdev_next_leaf;
400 uint64_t zs_vdev_aux;
405 uint64_t zs_metaslab_sz;
406 uint64_t zs_metaslab_df_alloc_threshold;
410 #define ID_PARALLEL -1ULL
412 static char ztest_dev_template[] = "%s/%s.%llua";
413 static char ztest_aux_template[] = "%s/%s.%s.%llu";
414 ztest_shared_t *ztest_shared;
416 static spa_t *ztest_spa = NULL;
417 static ztest_ds_t *ztest_ds;
419 static kmutex_t ztest_vdev_lock;
422 * The ztest_name_lock protects the pool and dataset namespace used by
423 * the individual tests. To modify the namespace, consumers must grab
424 * this lock as writer. Grabbing the lock as reader will ensure that the
425 * namespace does not change while the lock is held.
427 static rwlock_t ztest_name_lock;
429 static boolean_t ztest_dump_core = B_TRUE;
430 static boolean_t ztest_exiting;
432 /* Global commit callback list */
433 static ztest_cb_list_t zcl;
434 /* Commit cb delay */
435 static uint64_t zc_min_txg_delay = UINT64_MAX;
436 static int zc_cb_counter = 0;
439 * Minimum number of commit callbacks that need to be registered for us to check
440 * whether the minimum txg delay is acceptable.
442 #define ZTEST_COMMIT_CB_MIN_REG 100
445 * If a number of txgs equal to this threshold have been created after a commit
446 * callback has been registered but not called, then we assume there is an
447 * implementation bug.
449 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
451 extern uint64_t metaslab_gang_bang;
452 extern uint64_t metaslab_df_alloc_threshold;
455 ZTEST_META_DNODE = 0,
460 static void usage(boolean_t) __NORETURN;
463 * These libumem hooks provide a reasonable set of defaults for the allocator's
464 * debugging facilities.
467 _umem_debug_init(void)
469 return ("default,verbose"); /* $UMEM_DEBUG setting */
473 _umem_logging_init(void)
475 return ("fail,contents"); /* $UMEM_LOGGING setting */
478 #define BACKTRACE_SZ 100
480 static void sig_handler(int signo)
482 struct sigaction action;
483 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
485 void *buffer[BACKTRACE_SZ];
487 nptrs = backtrace(buffer, BACKTRACE_SZ);
488 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
492 * Restore default action and re-raise signal so SIGSEGV and
493 * SIGABRT can trigger a core dump.
495 action.sa_handler = SIG_DFL;
496 sigemptyset(&action.sa_mask);
498 (void) sigaction(signo, &action, NULL);
502 #define FATAL_MSG_SZ 1024
507 fatal(int do_perror, char *message, ...)
510 int save_errno = errno;
513 (void) fflush(stdout);
514 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
516 va_start(args, message);
517 (void) sprintf(buf, "ztest: ");
519 (void) vsprintf(buf + strlen(buf), message, args);
522 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
523 ": %s", strerror(save_errno));
525 (void) fprintf(stderr, "%s\n", buf);
526 fatal_msg = buf; /* to ease debugging */
533 str2shift(const char *buf)
535 const char *ends = "BKMGTPEZ";
540 for (i = 0; i < strlen(ends); i++) {
541 if (toupper(buf[0]) == ends[i])
544 if (i == strlen(ends)) {
545 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
549 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
552 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
558 nicenumtoull(const char *buf)
563 val = strtoull(buf, &end, 0);
565 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
567 } else if (end[0] == '.') {
568 double fval = strtod(buf, &end);
569 fval *= pow(2, str2shift(end));
570 if (fval > UINT64_MAX) {
571 (void) fprintf(stderr, "ztest: value too large: %s\n",
575 val = (uint64_t)fval;
577 int shift = str2shift(end);
578 if (shift >= 64 || (val << shift) >> shift != val) {
579 (void) fprintf(stderr, "ztest: value too large: %s\n",
589 usage(boolean_t requested)
591 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
593 char nice_vdev_size[10];
594 char nice_gang_bang[10];
595 FILE *fp = requested ? stdout : stderr;
597 nicenum(zo->zo_vdev_size, nice_vdev_size);
598 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
600 (void) fprintf(fp, "Usage: %s\n"
601 "\t[-v vdevs (default: %llu)]\n"
602 "\t[-s size_of_each_vdev (default: %s)]\n"
603 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
604 "\t[-m mirror_copies (default: %d)]\n"
605 "\t[-r raidz_disks (default: %d)]\n"
606 "\t[-R raidz_parity (default: %d)]\n"
607 "\t[-d datasets (default: %d)]\n"
608 "\t[-t threads (default: %d)]\n"
609 "\t[-g gang_block_threshold (default: %s)]\n"
610 "\t[-i init_count (default: %d)] initialize pool i times\n"
611 "\t[-k kill_percentage (default: %llu%%)]\n"
612 "\t[-p pool_name (default: %s)]\n"
613 "\t[-f dir (default: %s)] file directory for vdev files\n"
614 "\t[-V] verbose (use multiple times for ever more blather)\n"
615 "\t[-E] use existing pool instead of creating new one\n"
616 "\t[-T time (default: %llu sec)] total run time\n"
617 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
618 "\t[-P passtime (default: %llu sec)] time per pass\n"
619 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
620 "\t[-h] (print help)\n"
623 (u_longlong_t)zo->zo_vdevs, /* -v */
624 nice_vdev_size, /* -s */
625 zo->zo_ashift, /* -a */
626 zo->zo_mirrors, /* -m */
627 zo->zo_raidz, /* -r */
628 zo->zo_raidz_parity, /* -R */
629 zo->zo_datasets, /* -d */
630 zo->zo_threads, /* -t */
631 nice_gang_bang, /* -g */
632 zo->zo_init, /* -i */
633 (u_longlong_t)zo->zo_killrate, /* -k */
634 zo->zo_pool, /* -p */
636 (u_longlong_t)zo->zo_time, /* -T */
637 (u_longlong_t)zo->zo_maxloops, /* -F */
638 (u_longlong_t)zo->zo_passtime);
639 exit(requested ? 0 : 1);
643 process_options(int argc, char **argv)
646 ztest_shared_opts_t *zo = &ztest_opts;
650 char altdir[MAXNAMELEN] = { 0 };
652 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
654 while ((opt = getopt(argc, argv,
655 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
672 value = nicenumtoull(optarg);
676 zo->zo_vdevs = value;
679 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
682 zo->zo_ashift = value;
685 zo->zo_mirrors = value;
688 zo->zo_raidz = MAX(1, value);
691 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
694 zo->zo_datasets = MAX(1, value);
697 zo->zo_threads = MAX(1, value);
700 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
707 zo->zo_killrate = value;
710 (void) strlcpy(zo->zo_pool, optarg,
711 sizeof (zo->zo_pool));
714 path = realpath(optarg, NULL);
716 (void) fprintf(stderr, "error: %s: %s\n",
717 optarg, strerror(errno));
720 (void) strlcpy(zo->zo_dir, path,
721 sizeof (zo->zo_dir));
734 zo->zo_passtime = MAX(1, value);
737 zo->zo_maxloops = MAX(1, value);
740 (void) strlcpy(altdir, optarg, sizeof (altdir));
752 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
755 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
758 if (strlen(altdir) > 0) {
766 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
767 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
769 VERIFY(NULL != realpath(getexecname(), cmd));
770 if (0 != access(altdir, F_OK)) {
771 ztest_dump_core = B_FALSE;
772 fatal(B_TRUE, "invalid alternate ztest path: %s",
775 VERIFY(NULL != realpath(altdir, realaltdir));
778 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
779 * We want to extract <isa> to determine if we should use
780 * 32 or 64 bit binaries.
782 bin = strstr(cmd, "/usr/bin/");
783 ztest = strstr(bin, "/ztest");
785 isalen = ztest - isa;
786 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
787 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
788 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
789 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
791 if (0 != access(zo->zo_alt_ztest, X_OK)) {
792 ztest_dump_core = B_FALSE;
793 fatal(B_TRUE, "invalid alternate ztest: %s",
795 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
796 ztest_dump_core = B_FALSE;
797 fatal(B_TRUE, "invalid alternate lib directory %s",
801 umem_free(cmd, MAXPATHLEN);
802 umem_free(realaltdir, MAXPATHLEN);
807 ztest_kill(ztest_shared_t *zs)
809 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
810 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
813 * Before we kill off ztest, make sure that the config is updated.
814 * See comment above spa_config_sync().
816 mutex_enter(&spa_namespace_lock);
817 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
818 mutex_exit(&spa_namespace_lock);
820 (void) kill(getpid(), SIGKILL);
824 ztest_random(uint64_t range)
828 ASSERT3S(ztest_fd_rand, >=, 0);
833 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
834 fatal(1, "short read from /dev/urandom");
841 ztest_record_enospc(const char *s)
843 ztest_shared->zs_enospc_count++;
847 ztest_get_ashift(void)
849 if (ztest_opts.zo_ashift == 0)
850 return (SPA_MINBLOCKSHIFT + ztest_random(5));
851 return (ztest_opts.zo_ashift);
855 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
861 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
864 ashift = ztest_get_ashift();
870 vdev = ztest_shared->zs_vdev_aux;
871 (void) snprintf(path, MAXPATHLEN,
872 ztest_aux_template, ztest_opts.zo_dir,
873 pool == NULL ? ztest_opts.zo_pool : pool,
876 vdev = ztest_shared->zs_vdev_next_leaf++;
877 (void) snprintf(path, MAXPATHLEN,
878 ztest_dev_template, ztest_opts.zo_dir,
879 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
884 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
886 fatal(1, "can't open %s", path);
887 if (ftruncate(fd, size) != 0)
888 fatal(1, "can't ftruncate %s", path);
892 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
893 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
894 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
895 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
896 umem_free(pathbuf, MAXPATHLEN);
902 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
903 uint64_t ashift, int r)
905 nvlist_t *raidz, **child;
909 return (make_vdev_file(path, aux, pool, size, ashift));
910 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
912 for (c = 0; c < r; c++)
913 child[c] = make_vdev_file(path, aux, pool, size, ashift);
915 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
916 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
917 VDEV_TYPE_RAIDZ) == 0);
918 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
919 ztest_opts.zo_raidz_parity) == 0);
920 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
923 for (c = 0; c < r; c++)
924 nvlist_free(child[c]);
926 umem_free(child, r * sizeof (nvlist_t *));
932 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
933 uint64_t ashift, int r, int m)
935 nvlist_t *mirror, **child;
939 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
941 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
943 for (c = 0; c < m; c++)
944 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
946 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
947 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
948 VDEV_TYPE_MIRROR) == 0);
949 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
952 for (c = 0; c < m; c++)
953 nvlist_free(child[c]);
955 umem_free(child, m * sizeof (nvlist_t *));
961 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
962 int log, int r, int m, int t)
964 nvlist_t *root, **child;
969 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
971 for (c = 0; c < t; c++) {
972 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
974 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
978 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
979 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
980 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
983 for (c = 0; c < t; c++)
984 nvlist_free(child[c]);
986 umem_free(child, t * sizeof (nvlist_t *));
992 * Find a random spa version. Returns back a random spa version in the
993 * range [initial_version, SPA_VERSION_FEATURES].
996 ztest_random_spa_version(uint64_t initial_version)
998 uint64_t version = initial_version;
1000 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1002 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1005 if (version > SPA_VERSION_BEFORE_FEATURES)
1006 version = SPA_VERSION_FEATURES;
1008 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1013 ztest_random_blocksize(void)
1016 * Choose a block size >= the ashift.
1017 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1019 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1020 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1022 uint64_t block_shift =
1023 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1024 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1028 ztest_random_ibshift(void)
1030 return (DN_MIN_INDBLKSHIFT +
1031 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1035 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1038 vdev_t *rvd = spa->spa_root_vdev;
1041 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1044 top = ztest_random(rvd->vdev_children);
1045 tvd = rvd->vdev_child[top];
1046 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1047 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1053 ztest_random_dsl_prop(zfs_prop_t prop)
1058 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1059 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1065 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1068 const char *propname = zfs_prop_to_name(prop);
1069 const char *valname;
1074 error = dsl_prop_set_int(osname, propname,
1075 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1077 if (error == ENOSPC) {
1078 ztest_record_enospc(FTAG);
1083 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1084 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1086 if (ztest_opts.zo_verbose >= 6) {
1089 err = zfs_prop_index_to_string(prop, curval, &valname);
1091 (void) printf("%s %s = %llu at '%s'\n",
1092 osname, propname, (unsigned long long)curval,
1095 (void) printf("%s %s = %s at '%s'\n",
1096 osname, propname, valname, setpoint);
1098 umem_free(setpoint, MAXPATHLEN);
1104 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1106 spa_t *spa = ztest_spa;
1107 nvlist_t *props = NULL;
1110 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1111 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1113 error = spa_prop_set(spa, props);
1117 if (error == ENOSPC) {
1118 ztest_record_enospc(FTAG);
1128 * Object and range lock mechanics
1131 list_node_t z_lnode;
1132 refcount_t z_refcnt;
1134 zfs_rlock_t z_range_lock;
1139 ztest_znode_t *z_ztznode;
1142 static ztest_znode_t *
1143 ztest_znode_init(uint64_t object)
1145 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1147 list_link_init(&zp->z_lnode);
1148 refcount_create(&zp->z_refcnt);
1149 zp->z_object = object;
1150 zfs_rlock_init(&zp->z_range_lock);
1156 ztest_znode_fini(ztest_znode_t *zp)
1158 ASSERT(refcount_is_zero(&zp->z_refcnt));
1159 zfs_rlock_destroy(&zp->z_range_lock);
1161 refcount_destroy(&zp->z_refcnt);
1162 list_link_init(&zp->z_lnode);
1163 umem_free(zp, sizeof (*zp));
1167 ztest_zll_init(zll_t *zll)
1169 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1170 list_create(&zll->z_list, sizeof (ztest_znode_t),
1171 offsetof(ztest_znode_t, z_lnode));
1175 ztest_zll_destroy(zll_t *zll)
1177 list_destroy(&zll->z_list);
1178 mutex_destroy(&zll->z_lock);
1181 #define RL_TAG "range_lock"
1182 static ztest_znode_t *
1183 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1185 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1186 ztest_znode_t *zp = NULL;
1187 mutex_enter(&zll->z_lock);
1188 for (zp = list_head(&zll->z_list); (zp);
1189 zp = list_next(&zll->z_list, zp)) {
1190 if (zp->z_object == object) {
1191 refcount_add(&zp->z_refcnt, RL_TAG);
1196 zp = ztest_znode_init(object);
1197 refcount_add(&zp->z_refcnt, RL_TAG);
1198 list_insert_head(&zll->z_list, zp);
1200 mutex_exit(&zll->z_lock);
1205 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1208 ASSERT3U(zp->z_object, !=, 0);
1209 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1210 mutex_enter(&zll->z_lock);
1211 refcount_remove(&zp->z_refcnt, RL_TAG);
1212 if (refcount_is_zero(&zp->z_refcnt)) {
1213 list_remove(&zll->z_list, zp);
1214 ztest_znode_fini(zp);
1216 mutex_exit(&zll->z_lock);
1221 ztest_rll_init(rll_t *rll)
1223 rll->rll_writer = NULL;
1224 rll->rll_readers = 0;
1225 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1226 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1230 ztest_rll_destroy(rll_t *rll)
1232 ASSERT(rll->rll_writer == NULL);
1233 ASSERT(rll->rll_readers == 0);
1234 mutex_destroy(&rll->rll_lock);
1235 cv_destroy(&rll->rll_cv);
1239 ztest_rll_lock(rll_t *rll, rl_type_t type)
1241 mutex_enter(&rll->rll_lock);
1243 if (type == RL_READER) {
1244 while (rll->rll_writer != NULL)
1245 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1248 while (rll->rll_writer != NULL || rll->rll_readers)
1249 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1250 rll->rll_writer = curthread;
1253 mutex_exit(&rll->rll_lock);
1257 ztest_rll_unlock(rll_t *rll)
1259 mutex_enter(&rll->rll_lock);
1261 if (rll->rll_writer) {
1262 ASSERT(rll->rll_readers == 0);
1263 rll->rll_writer = NULL;
1265 ASSERT(rll->rll_readers != 0);
1266 ASSERT(rll->rll_writer == NULL);
1270 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1271 cv_broadcast(&rll->rll_cv);
1273 mutex_exit(&rll->rll_lock);
1277 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1279 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1281 ztest_rll_lock(rll, type);
1285 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1287 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1289 ztest_rll_unlock(rll);
1292 static ztest_zrl_t *
1293 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1295 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1297 zrl->z_ztznode = zp;
1302 ztest_zrl_fini(ztest_zrl_t *zrl)
1304 umem_free(zrl, sizeof (*zrl));
1307 static ztest_zrl_t *
1308 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1309 uint64_t size, rl_type_t type)
1311 ztest_znode_t *zp = ztest_znode_get(zd, object);
1312 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1314 return (ztest_zrl_init(rl, zp));
1318 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1320 zfs_range_unlock(zrl->z_rl);
1321 ztest_znode_put(zd, zrl->z_ztznode);
1322 ztest_zrl_fini(zrl);
1326 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1329 zd->zd_zilog = dmu_objset_zil(os);
1330 zd->zd_shared = szd;
1331 dmu_objset_name(os, zd->zd_name);
1334 if (zd->zd_shared != NULL)
1335 zd->zd_shared->zd_seq = 0;
1337 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1338 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1340 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1341 ztest_rll_init(&zd->zd_object_lock[l]);
1343 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1344 ztest_zll_init(&zd->zd_range_lock[l]);
1348 ztest_zd_fini(ztest_ds_t *zd)
1352 mutex_destroy(&zd->zd_dirobj_lock);
1353 (void) rwlock_destroy(&zd->zd_zilog_lock);
1355 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1356 ztest_rll_destroy(&zd->zd_object_lock[l]);
1358 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1359 ztest_zll_destroy(&zd->zd_range_lock[l]);
1362 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1365 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1371 * Attempt to assign tx to some transaction group.
1373 error = dmu_tx_assign(tx, txg_how);
1375 if (error == ERESTART) {
1376 ASSERT(txg_how == TXG_NOWAIT);
1379 ASSERT3U(error, ==, ENOSPC);
1380 ztest_record_enospc(tag);
1385 txg = dmu_tx_get_txg(tx);
1391 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1394 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1402 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1405 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1409 diff |= (value - *ip++);
1416 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1417 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1419 bt->bt_magic = BT_MAGIC;
1420 bt->bt_objset = dmu_objset_id(os);
1421 bt->bt_object = object;
1422 bt->bt_offset = offset;
1425 bt->bt_crtxg = crtxg;
1429 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1430 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1432 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1433 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1434 ASSERT3U(bt->bt_object, ==, object);
1435 ASSERT3U(bt->bt_offset, ==, offset);
1436 ASSERT3U(bt->bt_gen, <=, gen);
1437 ASSERT3U(bt->bt_txg, <=, txg);
1438 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1441 static ztest_block_tag_t *
1442 ztest_bt_bonus(dmu_buf_t *db)
1444 dmu_object_info_t doi;
1445 ztest_block_tag_t *bt;
1447 dmu_object_info_from_db(db, &doi);
1448 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1449 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1450 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1459 #define lrz_type lr_mode
1460 #define lrz_blocksize lr_uid
1461 #define lrz_ibshift lr_gid
1462 #define lrz_bonustype lr_rdev
1463 #define lrz_bonuslen lr_crtime[1]
1466 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1468 char *name = (void *)(lr + 1); /* name follows lr */
1469 size_t namesize = strlen(name) + 1;
1472 if (zil_replaying(zd->zd_zilog, tx))
1475 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1476 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1477 sizeof (*lr) + namesize - sizeof (lr_t));
1479 zil_itx_assign(zd->zd_zilog, itx, tx);
1483 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1485 char *name = (void *)(lr + 1); /* name follows lr */
1486 size_t namesize = strlen(name) + 1;
1489 if (zil_replaying(zd->zd_zilog, tx))
1492 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1493 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1494 sizeof (*lr) + namesize - sizeof (lr_t));
1496 itx->itx_oid = object;
1497 zil_itx_assign(zd->zd_zilog, itx, tx);
1501 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1504 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1506 if (zil_replaying(zd->zd_zilog, tx))
1509 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1510 write_state = WR_INDIRECT;
1512 itx = zil_itx_create(TX_WRITE,
1513 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1515 if (write_state == WR_COPIED &&
1516 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1517 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1518 zil_itx_destroy(itx);
1519 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1520 write_state = WR_NEED_COPY;
1522 itx->itx_private = zd;
1523 itx->itx_wr_state = write_state;
1524 itx->itx_sync = (ztest_random(8) == 0);
1525 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1527 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1528 sizeof (*lr) - sizeof (lr_t));
1530 zil_itx_assign(zd->zd_zilog, itx, tx);
1534 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1538 if (zil_replaying(zd->zd_zilog, tx))
1541 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1542 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1543 sizeof (*lr) - sizeof (lr_t));
1545 itx->itx_sync = B_FALSE;
1546 zil_itx_assign(zd->zd_zilog, itx, tx);
1550 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1554 if (zil_replaying(zd->zd_zilog, tx))
1557 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1558 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1559 sizeof (*lr) - sizeof (lr_t));
1561 itx->itx_sync = B_FALSE;
1562 zil_itx_assign(zd->zd_zilog, itx, tx);
1569 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1571 char *name = (void *)(lr + 1); /* name follows lr */
1572 objset_t *os = zd->zd_os;
1573 ztest_block_tag_t *bbt;
1580 byteswap_uint64_array(lr, sizeof (*lr));
1582 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1583 ASSERT(name[0] != '\0');
1585 tx = dmu_tx_create(os);
1587 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1589 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1590 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1592 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1595 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1599 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1601 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1602 if (lr->lr_foid == 0) {
1603 lr->lr_foid = zap_create(os,
1604 lr->lrz_type, lr->lrz_bonustype,
1605 lr->lrz_bonuslen, tx);
1607 error = zap_create_claim(os, lr->lr_foid,
1608 lr->lrz_type, lr->lrz_bonustype,
1609 lr->lrz_bonuslen, tx);
1612 if (lr->lr_foid == 0) {
1613 lr->lr_foid = dmu_object_alloc(os,
1614 lr->lrz_type, 0, lr->lrz_bonustype,
1615 lr->lrz_bonuslen, tx);
1617 error = dmu_object_claim(os, lr->lr_foid,
1618 lr->lrz_type, 0, lr->lrz_bonustype,
1619 lr->lrz_bonuslen, tx);
1624 ASSERT3U(error, ==, EEXIST);
1625 ASSERT(zd->zd_zilog->zl_replay);
1630 ASSERT(lr->lr_foid != 0);
1632 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1633 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1634 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1636 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1637 bbt = ztest_bt_bonus(db);
1638 dmu_buf_will_dirty(db, tx);
1639 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1640 dmu_buf_rele(db, FTAG);
1642 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1645 (void) ztest_log_create(zd, tx, lr);
1653 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1655 char *name = (void *)(lr + 1); /* name follows lr */
1656 objset_t *os = zd->zd_os;
1657 dmu_object_info_t doi;
1659 uint64_t object, txg;
1662 byteswap_uint64_array(lr, sizeof (*lr));
1664 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1665 ASSERT(name[0] != '\0');
1668 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1669 ASSERT(object != 0);
1671 ztest_object_lock(zd, object, RL_WRITER);
1673 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1675 tx = dmu_tx_create(os);
1677 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1678 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1680 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1682 ztest_object_unlock(zd, object);
1686 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1687 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1689 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1692 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1694 (void) ztest_log_remove(zd, tx, lr, object);
1698 ztest_object_unlock(zd, object);
1704 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1706 objset_t *os = zd->zd_os;
1707 void *data = lr + 1; /* data follows lr */
1708 uint64_t offset, length;
1709 ztest_block_tag_t *bt = data;
1710 ztest_block_tag_t *bbt;
1711 uint64_t gen, txg, lrtxg, crtxg;
1712 dmu_object_info_t doi;
1715 arc_buf_t *abuf = NULL;
1719 byteswap_uint64_array(lr, sizeof (*lr));
1721 offset = lr->lr_offset;
1722 length = lr->lr_length;
1724 /* If it's a dmu_sync() block, write the whole block */
1725 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1726 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1727 if (length < blocksize) {
1728 offset -= offset % blocksize;
1733 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1734 byteswap_uint64_array(bt, sizeof (*bt));
1736 if (bt->bt_magic != BT_MAGIC)
1739 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1740 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1742 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1744 dmu_object_info_from_db(db, &doi);
1746 bbt = ztest_bt_bonus(db);
1747 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1749 crtxg = bbt->bt_crtxg;
1750 lrtxg = lr->lr_common.lrc_txg;
1752 tx = dmu_tx_create(os);
1754 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1756 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1757 P2PHASE(offset, length) == 0)
1758 abuf = dmu_request_arcbuf(db, length);
1760 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1763 dmu_return_arcbuf(abuf);
1764 dmu_buf_rele(db, FTAG);
1765 ztest_range_unlock(zd, rl);
1766 ztest_object_unlock(zd, lr->lr_foid);
1772 * Usually, verify the old data before writing new data --
1773 * but not always, because we also want to verify correct
1774 * behavior when the data was not recently read into cache.
1776 ASSERT(offset % doi.doi_data_block_size == 0);
1777 if (ztest_random(4) != 0) {
1778 int prefetch = ztest_random(2) ?
1779 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1780 ztest_block_tag_t rbt;
1782 VERIFY(dmu_read(os, lr->lr_foid, offset,
1783 sizeof (rbt), &rbt, prefetch) == 0);
1784 if (rbt.bt_magic == BT_MAGIC) {
1785 ztest_bt_verify(&rbt, os, lr->lr_foid,
1786 offset, gen, txg, crtxg);
1791 * Writes can appear to be newer than the bonus buffer because
1792 * the ztest_get_data() callback does a dmu_read() of the
1793 * open-context data, which may be different than the data
1794 * as it was when the write was generated.
1796 if (zd->zd_zilog->zl_replay) {
1797 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1798 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1803 * Set the bt's gen/txg to the bonus buffer's gen/txg
1804 * so that all of the usual ASSERTs will work.
1806 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1810 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1812 bcopy(data, abuf->b_data, length);
1813 dmu_assign_arcbuf(db, offset, abuf, tx);
1816 (void) ztest_log_write(zd, tx, lr);
1818 dmu_buf_rele(db, FTAG);
1822 ztest_range_unlock(zd, rl);
1823 ztest_object_unlock(zd, lr->lr_foid);
1829 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1831 objset_t *os = zd->zd_os;
1837 byteswap_uint64_array(lr, sizeof (*lr));
1839 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1840 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1843 tx = dmu_tx_create(os);
1845 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1847 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1849 ztest_range_unlock(zd, rl);
1850 ztest_object_unlock(zd, lr->lr_foid);
1854 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1855 lr->lr_length, tx) == 0);
1857 (void) ztest_log_truncate(zd, tx, lr);
1861 ztest_range_unlock(zd, rl);
1862 ztest_object_unlock(zd, lr->lr_foid);
1868 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1870 objset_t *os = zd->zd_os;
1873 ztest_block_tag_t *bbt;
1874 uint64_t txg, lrtxg, crtxg;
1877 byteswap_uint64_array(lr, sizeof (*lr));
1879 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1881 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1883 tx = dmu_tx_create(os);
1884 dmu_tx_hold_bonus(tx, lr->lr_foid);
1886 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1888 dmu_buf_rele(db, FTAG);
1889 ztest_object_unlock(zd, lr->lr_foid);
1893 bbt = ztest_bt_bonus(db);
1894 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1895 crtxg = bbt->bt_crtxg;
1896 lrtxg = lr->lr_common.lrc_txg;
1898 if (zd->zd_zilog->zl_replay) {
1899 ASSERT(lr->lr_size != 0);
1900 ASSERT(lr->lr_mode != 0);
1904 * Randomly change the size and increment the generation.
1906 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1908 lr->lr_mode = bbt->bt_gen + 1;
1913 * Verify that the current bonus buffer is not newer than our txg.
1915 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1916 MAX(txg, lrtxg), crtxg);
1918 dmu_buf_will_dirty(db, tx);
1920 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1921 ASSERT3U(lr->lr_size, <=, db->db_size);
1922 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1923 bbt = ztest_bt_bonus(db);
1925 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1927 dmu_buf_rele(db, FTAG);
1929 (void) ztest_log_setattr(zd, tx, lr);
1933 ztest_object_unlock(zd, lr->lr_foid);
1938 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1939 NULL, /* 0 no such transaction type */
1940 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1941 NULL, /* TX_MKDIR */
1942 NULL, /* TX_MKXATTR */
1943 NULL, /* TX_SYMLINK */
1944 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1945 NULL, /* TX_RMDIR */
1947 NULL, /* TX_RENAME */
1948 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1949 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1950 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1952 NULL, /* TX_CREATE_ACL */
1953 NULL, /* TX_CREATE_ATTR */
1954 NULL, /* TX_CREATE_ACL_ATTR */
1955 NULL, /* TX_MKDIR_ACL */
1956 NULL, /* TX_MKDIR_ATTR */
1957 NULL, /* TX_MKDIR_ACL_ATTR */
1958 NULL, /* TX_WRITE2 */
1962 * ZIL get_data callbacks
1964 typedef struct ztest_zgd_private {
1968 } ztest_zgd_private_t;
1971 ztest_get_done(zgd_t *zgd, int error)
1973 ztest_zgd_private_t *zzp = zgd->zgd_private;
1974 ztest_ds_t *zd = zzp->z_zd;
1975 uint64_t object = zzp->z_object;
1978 dmu_buf_rele(zgd->zgd_db, zgd);
1980 ztest_range_unlock(zd, zzp->z_rl);
1981 ztest_object_unlock(zd, object);
1983 if (error == 0 && zgd->zgd_bp)
1984 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1986 umem_free(zgd, sizeof (*zgd));
1987 umem_free(zzp, sizeof (*zzp));
1991 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1993 ztest_ds_t *zd = arg;
1994 objset_t *os = zd->zd_os;
1995 uint64_t object = lr->lr_foid;
1996 uint64_t offset = lr->lr_offset;
1997 uint64_t size = lr->lr_length;
1998 blkptr_t *bp = &lr->lr_blkptr;
1999 uint64_t txg = lr->lr_common.lrc_txg;
2001 dmu_object_info_t doi;
2005 ztest_zgd_private_t *zgd_private;
2007 ztest_object_lock(zd, object, RL_READER);
2008 error = dmu_bonus_hold(os, object, FTAG, &db);
2010 ztest_object_unlock(zd, object);
2014 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2016 if (crtxg == 0 || crtxg > txg) {
2017 dmu_buf_rele(db, FTAG);
2018 ztest_object_unlock(zd, object);
2022 dmu_object_info_from_db(db, &doi);
2023 dmu_buf_rele(db, FTAG);
2026 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2027 zgd->zgd_zilog = zd->zd_zilog;
2028 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2029 zgd_private->z_zd = zd;
2030 zgd_private->z_object = object;
2031 zgd->zgd_private = zgd_private;
2033 if (buf != NULL) { /* immediate write */
2034 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2037 error = dmu_read(os, object, offset, size, buf,
2038 DMU_READ_NO_PREFETCH);
2041 size = doi.doi_data_block_size;
2043 offset = P2ALIGN(offset, size);
2045 ASSERT(offset < size);
2049 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2052 error = dmu_buf_hold(os, object, offset, zgd, &db,
2053 DMU_READ_NO_PREFETCH);
2056 blkptr_t *obp = dmu_buf_get_blkptr(db);
2058 ASSERT(BP_IS_HOLE(bp));
2065 ASSERT(db->db_offset == offset);
2066 ASSERT(db->db_size == size);
2068 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2069 ztest_get_done, zgd);
2076 ztest_get_done(zgd, error);
2082 ztest_lr_alloc(size_t lrsize, char *name)
2085 size_t namesize = name ? strlen(name) + 1 : 0;
2087 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2090 bcopy(name, lr + lrsize, namesize);
2096 ztest_lr_free(void *lr, size_t lrsize, char *name)
2098 size_t namesize = name ? strlen(name) + 1 : 0;
2100 umem_free(lr, lrsize + namesize);
2104 * Lookup a bunch of objects. Returns the number of objects not found.
2107 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2113 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2115 for (i = 0; i < count; i++, od++) {
2117 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2118 sizeof (uint64_t), 1, &od->od_object);
2120 ASSERT(error == ENOENT);
2121 ASSERT(od->od_object == 0);
2125 ztest_block_tag_t *bbt;
2126 dmu_object_info_t doi;
2128 ASSERT(od->od_object != 0);
2129 ASSERT(missing == 0); /* there should be no gaps */
2131 ztest_object_lock(zd, od->od_object, RL_READER);
2132 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2133 od->od_object, FTAG, &db));
2134 dmu_object_info_from_db(db, &doi);
2135 bbt = ztest_bt_bonus(db);
2136 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2137 od->od_type = doi.doi_type;
2138 od->od_blocksize = doi.doi_data_block_size;
2139 od->od_gen = bbt->bt_gen;
2140 dmu_buf_rele(db, FTAG);
2141 ztest_object_unlock(zd, od->od_object);
2149 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2154 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2156 for (i = 0; i < count; i++, od++) {
2163 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2165 lr->lr_doid = od->od_dir;
2166 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2167 lr->lrz_type = od->od_crtype;
2168 lr->lrz_blocksize = od->od_crblocksize;
2169 lr->lrz_ibshift = ztest_random_ibshift();
2170 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2171 lr->lrz_bonuslen = dmu_bonus_max();
2172 lr->lr_gen = od->od_crgen;
2173 lr->lr_crtime[0] = time(NULL);
2175 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2176 ASSERT(missing == 0);
2180 od->od_object = lr->lr_foid;
2181 od->od_type = od->od_crtype;
2182 od->od_blocksize = od->od_crblocksize;
2183 od->od_gen = od->od_crgen;
2184 ASSERT(od->od_object != 0);
2187 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2194 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2200 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2204 for (i = count - 1; i >= 0; i--, od--) {
2211 * No object was found.
2213 if (od->od_object == 0)
2216 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2218 lr->lr_doid = od->od_dir;
2220 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2221 ASSERT3U(error, ==, ENOSPC);
2226 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2233 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2239 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2241 lr->lr_foid = object;
2242 lr->lr_offset = offset;
2243 lr->lr_length = size;
2245 BP_ZERO(&lr->lr_blkptr);
2247 bcopy(data, lr + 1, size);
2249 error = ztest_replay_write(zd, lr, B_FALSE);
2251 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2257 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2262 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2264 lr->lr_foid = object;
2265 lr->lr_offset = offset;
2266 lr->lr_length = size;
2268 error = ztest_replay_truncate(zd, lr, B_FALSE);
2270 ztest_lr_free(lr, sizeof (*lr), NULL);
2276 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2281 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2283 lr->lr_foid = object;
2287 error = ztest_replay_setattr(zd, lr, B_FALSE);
2289 ztest_lr_free(lr, sizeof (*lr), NULL);
2295 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2297 objset_t *os = zd->zd_os;
2302 txg_wait_synced(dmu_objset_pool(os), 0);
2304 ztest_object_lock(zd, object, RL_READER);
2305 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2307 tx = dmu_tx_create(os);
2309 dmu_tx_hold_write(tx, object, offset, size);
2311 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2314 dmu_prealloc(os, object, offset, size, tx);
2316 txg_wait_synced(dmu_objset_pool(os), txg);
2318 (void) dmu_free_long_range(os, object, offset, size);
2321 ztest_range_unlock(zd, rl);
2322 ztest_object_unlock(zd, object);
2326 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2329 ztest_block_tag_t wbt;
2330 dmu_object_info_t doi;
2331 enum ztest_io_type io_type;
2335 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2336 blocksize = doi.doi_data_block_size;
2337 data = umem_alloc(blocksize, UMEM_NOFAIL);
2340 * Pick an i/o type at random, biased toward writing block tags.
2342 io_type = ztest_random(ZTEST_IO_TYPES);
2343 if (ztest_random(2) == 0)
2344 io_type = ZTEST_IO_WRITE_TAG;
2346 (void) rw_rdlock(&zd->zd_zilog_lock);
2350 case ZTEST_IO_WRITE_TAG:
2351 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2352 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2355 case ZTEST_IO_WRITE_PATTERN:
2356 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2357 if (ztest_random(2) == 0) {
2359 * Induce fletcher2 collisions to ensure that
2360 * zio_ddt_collision() detects and resolves them
2361 * when using fletcher2-verify for deduplication.
2363 ((uint64_t *)data)[0] ^= 1ULL << 63;
2364 ((uint64_t *)data)[4] ^= 1ULL << 63;
2366 (void) ztest_write(zd, object, offset, blocksize, data);
2369 case ZTEST_IO_WRITE_ZEROES:
2370 bzero(data, blocksize);
2371 (void) ztest_write(zd, object, offset, blocksize, data);
2374 case ZTEST_IO_TRUNCATE:
2375 (void) ztest_truncate(zd, object, offset, blocksize);
2378 case ZTEST_IO_SETATTR:
2379 (void) ztest_setattr(zd, object);
2384 case ZTEST_IO_REWRITE:
2385 (void) rw_rdlock(&ztest_name_lock);
2386 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2387 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2389 VERIFY(err == 0 || err == ENOSPC);
2390 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2391 ZFS_PROP_COMPRESSION,
2392 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2394 VERIFY(err == 0 || err == ENOSPC);
2395 (void) rw_unlock(&ztest_name_lock);
2397 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2398 DMU_READ_NO_PREFETCH));
2400 (void) ztest_write(zd, object, offset, blocksize, data);
2404 (void) rw_unlock(&zd->zd_zilog_lock);
2406 umem_free(data, blocksize);
2410 * Initialize an object description template.
2413 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2414 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2416 od->od_dir = ZTEST_DIROBJ;
2419 od->od_crtype = type;
2420 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2423 od->od_type = DMU_OT_NONE;
2424 od->od_blocksize = 0;
2427 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2428 tag, (longlong_t)id, (u_longlong_t)index);
2432 * Lookup or create the objects for a test using the od template.
2433 * If the objects do not all exist, or if 'remove' is specified,
2434 * remove any existing objects and create new ones. Otherwise,
2435 * use the existing objects.
2438 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2440 int count = size / sizeof (*od);
2443 mutex_enter(&zd->zd_dirobj_lock);
2444 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2445 (ztest_remove(zd, od, count) != 0 ||
2446 ztest_create(zd, od, count) != 0))
2449 mutex_exit(&zd->zd_dirobj_lock);
2456 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2458 zilog_t *zilog = zd->zd_zilog;
2460 (void) rw_rdlock(&zd->zd_zilog_lock);
2462 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2465 * Remember the committed values in zd, which is in parent/child
2466 * shared memory. If we die, the next iteration of ztest_run()
2467 * will verify that the log really does contain this record.
2469 mutex_enter(&zilog->zl_lock);
2470 ASSERT(zd->zd_shared != NULL);
2471 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2472 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2473 mutex_exit(&zilog->zl_lock);
2475 (void) rw_unlock(&zd->zd_zilog_lock);
2479 * This function is designed to simulate the operations that occur during a
2480 * mount/unmount operation. We hold the dataset across these operations in an
2481 * attempt to expose any implicit assumptions about ZIL management.
2485 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2487 objset_t *os = zd->zd_os;
2490 * We grab the zd_dirobj_lock to ensure that no other thread is
2491 * updating the zil (i.e. adding in-memory log records) and the
2492 * zd_zilog_lock to block any I/O.
2494 mutex_enter(&zd->zd_dirobj_lock);
2495 (void) rw_wrlock(&zd->zd_zilog_lock);
2497 /* zfs_sb_teardown() */
2498 zil_close(zd->zd_zilog);
2500 /* zfsvfs_setup() */
2501 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2502 zil_replay(os, zd, ztest_replay_vector);
2504 (void) rw_unlock(&zd->zd_zilog_lock);
2505 mutex_exit(&zd->zd_dirobj_lock);
2509 * Verify that we can't destroy an active pool, create an existing pool,
2510 * or create a pool with a bad vdev spec.
2514 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2516 ztest_shared_opts_t *zo = &ztest_opts;
2521 * Attempt to create using a bad file.
2523 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2524 VERIFY3U(ENOENT, ==,
2525 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2526 nvlist_free(nvroot);
2529 * Attempt to create using a bad mirror.
2531 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2532 VERIFY3U(ENOENT, ==,
2533 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2534 nvlist_free(nvroot);
2537 * Attempt to create an existing pool. It shouldn't matter
2538 * what's in the nvroot; we should fail with EEXIST.
2540 (void) rw_rdlock(&ztest_name_lock);
2541 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2542 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2543 nvlist_free(nvroot);
2544 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2545 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2546 spa_close(spa, FTAG);
2548 (void) rw_unlock(&ztest_name_lock);
2553 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2556 uint64_t initial_version = SPA_VERSION_INITIAL;
2557 uint64_t version, newversion;
2558 nvlist_t *nvroot, *props;
2561 mutex_enter(&ztest_vdev_lock);
2562 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2565 * Clean up from previous runs.
2567 (void) spa_destroy(name);
2569 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2570 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2573 * If we're configuring a RAIDZ device then make sure that the
2574 * the initial version is capable of supporting that feature.
2576 switch (ztest_opts.zo_raidz_parity) {
2579 initial_version = SPA_VERSION_INITIAL;
2582 initial_version = SPA_VERSION_RAIDZ2;
2585 initial_version = SPA_VERSION_RAIDZ3;
2590 * Create a pool with a spa version that can be upgraded. Pick
2591 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2594 version = ztest_random_spa_version(initial_version);
2595 } while (version > SPA_VERSION_BEFORE_FEATURES);
2597 props = fnvlist_alloc();
2598 fnvlist_add_uint64(props,
2599 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2600 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2601 fnvlist_free(nvroot);
2602 fnvlist_free(props);
2604 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2605 VERIFY3U(spa_version(spa), ==, version);
2606 newversion = ztest_random_spa_version(version + 1);
2608 if (ztest_opts.zo_verbose >= 4) {
2609 (void) printf("upgrading spa version from %llu to %llu\n",
2610 (u_longlong_t)version, (u_longlong_t)newversion);
2613 spa_upgrade(spa, newversion);
2614 VERIFY3U(spa_version(spa), >, version);
2615 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2616 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2617 spa_close(spa, FTAG);
2620 mutex_exit(&ztest_vdev_lock);
2624 vdev_lookup_by_path(vdev_t *vd, const char *path)
2629 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2632 for (c = 0; c < vd->vdev_children; c++)
2633 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2641 * Find the first available hole which can be used as a top-level.
2644 find_vdev_hole(spa_t *spa)
2646 vdev_t *rvd = spa->spa_root_vdev;
2649 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2651 for (c = 0; c < rvd->vdev_children; c++) {
2652 vdev_t *cvd = rvd->vdev_child[c];
2654 if (cvd->vdev_ishole)
2661 * Verify that vdev_add() works as expected.
2665 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2667 ztest_shared_t *zs = ztest_shared;
2668 spa_t *spa = ztest_spa;
2674 mutex_enter(&ztest_vdev_lock);
2675 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2677 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2679 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2682 * If we have slogs then remove them 1/4 of the time.
2684 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2686 * Grab the guid from the head of the log class rotor.
2688 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2690 spa_config_exit(spa, SCL_VDEV, FTAG);
2693 * We have to grab the zs_name_lock as writer to
2694 * prevent a race between removing a slog (dmu_objset_find)
2695 * and destroying a dataset. Removing the slog will
2696 * grab a reference on the dataset which may cause
2697 * dsl_destroy_head() to fail with EBUSY thus
2698 * leaving the dataset in an inconsistent state.
2700 rw_wrlock(&ztest_name_lock);
2701 error = spa_vdev_remove(spa, guid, B_FALSE);
2702 rw_unlock(&ztest_name_lock);
2704 if (error && error != EEXIST)
2705 fatal(0, "spa_vdev_remove() = %d", error);
2707 spa_config_exit(spa, SCL_VDEV, FTAG);
2710 * Make 1/4 of the devices be log devices.
2712 nvroot = make_vdev_root(NULL, NULL, NULL,
2713 ztest_opts.zo_vdev_size, 0,
2714 ztest_random(4) == 0, ztest_opts.zo_raidz,
2717 error = spa_vdev_add(spa, nvroot);
2718 nvlist_free(nvroot);
2720 if (error == ENOSPC)
2721 ztest_record_enospc("spa_vdev_add");
2722 else if (error != 0)
2723 fatal(0, "spa_vdev_add() = %d", error);
2726 mutex_exit(&ztest_vdev_lock);
2730 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2734 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2736 ztest_shared_t *zs = ztest_shared;
2737 spa_t *spa = ztest_spa;
2738 vdev_t *rvd = spa->spa_root_vdev;
2739 spa_aux_vdev_t *sav;
2745 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2747 if (ztest_random(2) == 0) {
2748 sav = &spa->spa_spares;
2749 aux = ZPOOL_CONFIG_SPARES;
2751 sav = &spa->spa_l2cache;
2752 aux = ZPOOL_CONFIG_L2CACHE;
2755 mutex_enter(&ztest_vdev_lock);
2757 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2759 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2761 * Pick a random device to remove.
2763 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2766 * Find an unused device we can add.
2768 zs->zs_vdev_aux = 0;
2771 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2772 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2774 for (c = 0; c < sav->sav_count; c++)
2775 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2778 if (c == sav->sav_count &&
2779 vdev_lookup_by_path(rvd, path) == NULL)
2785 spa_config_exit(spa, SCL_VDEV, FTAG);
2791 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2792 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2793 error = spa_vdev_add(spa, nvroot);
2795 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2796 nvlist_free(nvroot);
2799 * Remove an existing device. Sometimes, dirty its
2800 * vdev state first to make sure we handle removal
2801 * of devices that have pending state changes.
2803 if (ztest_random(2) == 0)
2804 (void) vdev_online(spa, guid, 0, NULL);
2806 error = spa_vdev_remove(spa, guid, B_FALSE);
2807 if (error != 0 && error != EBUSY)
2808 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2811 mutex_exit(&ztest_vdev_lock);
2813 umem_free(path, MAXPATHLEN);
2817 * split a pool if it has mirror tlvdevs
2821 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2823 ztest_shared_t *zs = ztest_shared;
2824 spa_t *spa = ztest_spa;
2825 vdev_t *rvd = spa->spa_root_vdev;
2826 nvlist_t *tree, **child, *config, *split, **schild;
2827 uint_t c, children, schildren = 0, lastlogid = 0;
2830 mutex_enter(&ztest_vdev_lock);
2832 /* ensure we have a useable config; mirrors of raidz aren't supported */
2833 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2834 mutex_exit(&ztest_vdev_lock);
2838 /* clean up the old pool, if any */
2839 (void) spa_destroy("splitp");
2841 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2843 /* generate a config from the existing config */
2844 mutex_enter(&spa->spa_props_lock);
2845 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2847 mutex_exit(&spa->spa_props_lock);
2849 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2852 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2853 for (c = 0; c < children; c++) {
2854 vdev_t *tvd = rvd->vdev_child[c];
2858 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2859 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2861 VERIFY(nvlist_add_string(schild[schildren],
2862 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2863 VERIFY(nvlist_add_uint64(schild[schildren],
2864 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2866 lastlogid = schildren;
2871 VERIFY(nvlist_lookup_nvlist_array(child[c],
2872 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2873 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2876 /* OK, create a config that can be used to split */
2877 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2878 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2879 VDEV_TYPE_ROOT) == 0);
2880 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2881 lastlogid != 0 ? lastlogid : schildren) == 0);
2883 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2884 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2886 for (c = 0; c < schildren; c++)
2887 nvlist_free(schild[c]);
2891 spa_config_exit(spa, SCL_VDEV, FTAG);
2893 (void) rw_wrlock(&ztest_name_lock);
2894 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2895 (void) rw_unlock(&ztest_name_lock);
2897 nvlist_free(config);
2900 (void) printf("successful split - results:\n");
2901 mutex_enter(&spa_namespace_lock);
2902 show_pool_stats(spa);
2903 show_pool_stats(spa_lookup("splitp"));
2904 mutex_exit(&spa_namespace_lock);
2908 mutex_exit(&ztest_vdev_lock);
2913 * Verify that we can attach and detach devices.
2917 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2919 ztest_shared_t *zs = ztest_shared;
2920 spa_t *spa = ztest_spa;
2921 spa_aux_vdev_t *sav = &spa->spa_spares;
2922 vdev_t *rvd = spa->spa_root_vdev;
2923 vdev_t *oldvd, *newvd, *pvd;
2927 uint64_t ashift = ztest_get_ashift();
2928 uint64_t oldguid, pguid;
2929 uint64_t oldsize, newsize;
2930 char *oldpath, *newpath;
2932 int oldvd_has_siblings = B_FALSE;
2933 int newvd_is_spare = B_FALSE;
2935 int error, expected_error;
2937 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2938 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2940 mutex_enter(&ztest_vdev_lock);
2941 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2943 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2946 * Decide whether to do an attach or a replace.
2948 replacing = ztest_random(2);
2951 * Pick a random top-level vdev.
2953 top = ztest_random_vdev_top(spa, B_TRUE);
2956 * Pick a random leaf within it.
2958 leaf = ztest_random(leaves);
2963 oldvd = rvd->vdev_child[top];
2964 if (zs->zs_mirrors >= 1) {
2965 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2966 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2967 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2969 if (ztest_opts.zo_raidz > 1) {
2970 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2971 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2972 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2976 * If we're already doing an attach or replace, oldvd may be a
2977 * mirror vdev -- in which case, pick a random child.
2979 while (oldvd->vdev_children != 0) {
2980 oldvd_has_siblings = B_TRUE;
2981 ASSERT(oldvd->vdev_children >= 2);
2982 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2985 oldguid = oldvd->vdev_guid;
2986 oldsize = vdev_get_min_asize(oldvd);
2987 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2988 (void) strcpy(oldpath, oldvd->vdev_path);
2989 pvd = oldvd->vdev_parent;
2990 pguid = pvd->vdev_guid;
2993 * If oldvd has siblings, then half of the time, detach it.
2995 if (oldvd_has_siblings && ztest_random(2) == 0) {
2996 spa_config_exit(spa, SCL_VDEV, FTAG);
2997 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2998 if (error != 0 && error != ENODEV && error != EBUSY &&
3000 fatal(0, "detach (%s) returned %d", oldpath, error);
3005 * For the new vdev, choose with equal probability between the two
3006 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3008 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3009 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3010 newvd_is_spare = B_TRUE;
3011 (void) strcpy(newpath, newvd->vdev_path);
3013 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3014 ztest_opts.zo_dir, ztest_opts.zo_pool,
3015 top * leaves + leaf);
3016 if (ztest_random(2) == 0)
3017 newpath[strlen(newpath) - 1] = 'b';
3018 newvd = vdev_lookup_by_path(rvd, newpath);
3022 newsize = vdev_get_min_asize(newvd);
3025 * Make newsize a little bigger or smaller than oldsize.
3026 * If it's smaller, the attach should fail.
3027 * If it's larger, and we're doing a replace,
3028 * we should get dynamic LUN growth when we're done.
3030 newsize = 10 * oldsize / (9 + ztest_random(3));
3034 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3035 * unless it's a replace; in that case any non-replacing parent is OK.
3037 * If newvd is already part of the pool, it should fail with EBUSY.
3039 * If newvd is too small, it should fail with EOVERFLOW.
3041 if (pvd->vdev_ops != &vdev_mirror_ops &&
3042 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3043 pvd->vdev_ops == &vdev_replacing_ops ||
3044 pvd->vdev_ops == &vdev_spare_ops))
3045 expected_error = ENOTSUP;
3046 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3047 expected_error = ENOTSUP;
3048 else if (newvd == oldvd)
3049 expected_error = replacing ? 0 : EBUSY;
3050 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3051 expected_error = EBUSY;
3052 else if (newsize < oldsize)
3053 expected_error = EOVERFLOW;
3054 else if (ashift > oldvd->vdev_top->vdev_ashift)
3055 expected_error = EDOM;
3059 spa_config_exit(spa, SCL_VDEV, FTAG);
3062 * Build the nvlist describing newpath.
3064 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3065 ashift, 0, 0, 0, 1);
3067 error = spa_vdev_attach(spa, oldguid, root, replacing);
3072 * If our parent was the replacing vdev, but the replace completed,
3073 * then instead of failing with ENOTSUP we may either succeed,
3074 * fail with ENODEV, or fail with EOVERFLOW.
3076 if (expected_error == ENOTSUP &&
3077 (error == 0 || error == ENODEV || error == EOVERFLOW))
3078 expected_error = error;
3081 * If someone grew the LUN, the replacement may be too small.
3083 if (error == EOVERFLOW || error == EBUSY)
3084 expected_error = error;
3086 /* XXX workaround 6690467 */
3087 if (error != expected_error && expected_error != EBUSY) {
3088 fatal(0, "attach (%s %llu, %s %llu, %d) "
3089 "returned %d, expected %d",
3090 oldpath, oldsize, newpath,
3091 newsize, replacing, error, expected_error);
3094 mutex_exit(&ztest_vdev_lock);
3096 umem_free(oldpath, MAXPATHLEN);
3097 umem_free(newpath, MAXPATHLEN);
3101 * Callback function which expands the physical size of the vdev.
3104 grow_vdev(vdev_t *vd, void *arg)
3106 ASSERTV(spa_t *spa = vd->vdev_spa);
3107 size_t *newsize = arg;
3111 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3112 ASSERT(vd->vdev_ops->vdev_op_leaf);
3114 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3117 fsize = lseek(fd, 0, SEEK_END);
3118 VERIFY(ftruncate(fd, *newsize) == 0);
3120 if (ztest_opts.zo_verbose >= 6) {
3121 (void) printf("%s grew from %lu to %lu bytes\n",
3122 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3129 * Callback function which expands a given vdev by calling vdev_online().
3133 online_vdev(vdev_t *vd, void *arg)
3135 spa_t *spa = vd->vdev_spa;
3136 vdev_t *tvd = vd->vdev_top;
3137 uint64_t guid = vd->vdev_guid;
3138 uint64_t generation = spa->spa_config_generation + 1;
3139 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3142 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3143 ASSERT(vd->vdev_ops->vdev_op_leaf);
3145 /* Calling vdev_online will initialize the new metaslabs */
3146 spa_config_exit(spa, SCL_STATE, spa);
3147 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3148 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3151 * If vdev_online returned an error or the underlying vdev_open
3152 * failed then we abort the expand. The only way to know that
3153 * vdev_open fails is by checking the returned newstate.
3155 if (error || newstate != VDEV_STATE_HEALTHY) {
3156 if (ztest_opts.zo_verbose >= 5) {
3157 (void) printf("Unable to expand vdev, state %llu, "
3158 "error %d\n", (u_longlong_t)newstate, error);
3162 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3165 * Since we dropped the lock we need to ensure that we're
3166 * still talking to the original vdev. It's possible this
3167 * vdev may have been detached/replaced while we were
3168 * trying to online it.
3170 if (generation != spa->spa_config_generation) {
3171 if (ztest_opts.zo_verbose >= 5) {
3172 (void) printf("vdev configuration has changed, "
3173 "guid %llu, state %llu, expected gen %llu, "
3176 (u_longlong_t)tvd->vdev_state,
3177 (u_longlong_t)generation,
3178 (u_longlong_t)spa->spa_config_generation);
3186 * Traverse the vdev tree calling the supplied function.
3187 * We continue to walk the tree until we either have walked all
3188 * children or we receive a non-NULL return from the callback.
3189 * If a NULL callback is passed, then we just return back the first
3190 * leaf vdev we encounter.
3193 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3197 if (vd->vdev_ops->vdev_op_leaf) {
3201 return (func(vd, arg));
3204 for (c = 0; c < vd->vdev_children; c++) {
3205 vdev_t *cvd = vd->vdev_child[c];
3206 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3213 * Verify that dynamic LUN growth works as expected.
3217 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3219 spa_t *spa = ztest_spa;
3221 metaslab_class_t *mc;
3222 metaslab_group_t *mg;
3223 size_t psize, newsize;
3225 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3227 mutex_enter(&ztest_vdev_lock);
3228 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3230 top = ztest_random_vdev_top(spa, B_TRUE);
3232 tvd = spa->spa_root_vdev->vdev_child[top];
3235 old_ms_count = tvd->vdev_ms_count;
3236 old_class_space = metaslab_class_get_space(mc);
3239 * Determine the size of the first leaf vdev associated with
3240 * our top-level device.
3242 vd = vdev_walk_tree(tvd, NULL, NULL);
3243 ASSERT3P(vd, !=, NULL);
3244 ASSERT(vd->vdev_ops->vdev_op_leaf);
3246 psize = vd->vdev_psize;
3249 * We only try to expand the vdev if it's healthy, less than 4x its
3250 * original size, and it has a valid psize.
3252 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3253 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3254 spa_config_exit(spa, SCL_STATE, spa);
3255 mutex_exit(&ztest_vdev_lock);
3259 newsize = psize + psize / 8;
3260 ASSERT3U(newsize, >, psize);
3262 if (ztest_opts.zo_verbose >= 6) {
3263 (void) printf("Expanding LUN %s from %lu to %lu\n",
3264 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3268 * Growing the vdev is a two step process:
3269 * 1). expand the physical size (i.e. relabel)
3270 * 2). online the vdev to create the new metaslabs
3272 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3273 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3274 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3275 if (ztest_opts.zo_verbose >= 5) {
3276 (void) printf("Could not expand LUN because "
3277 "the vdev configuration changed.\n");
3279 spa_config_exit(spa, SCL_STATE, spa);
3280 mutex_exit(&ztest_vdev_lock);
3284 spa_config_exit(spa, SCL_STATE, spa);
3287 * Expanding the LUN will update the config asynchronously,
3288 * thus we must wait for the async thread to complete any
3289 * pending tasks before proceeding.
3293 mutex_enter(&spa->spa_async_lock);
3294 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3295 mutex_exit(&spa->spa_async_lock);
3298 txg_wait_synced(spa_get_dsl(spa), 0);
3299 (void) poll(NULL, 0, 100);
3302 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3304 tvd = spa->spa_root_vdev->vdev_child[top];
3305 new_ms_count = tvd->vdev_ms_count;
3306 new_class_space = metaslab_class_get_space(mc);
3308 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3309 if (ztest_opts.zo_verbose >= 5) {
3310 (void) printf("Could not verify LUN expansion due to "
3311 "intervening vdev offline or remove.\n");
3313 spa_config_exit(spa, SCL_STATE, spa);
3314 mutex_exit(&ztest_vdev_lock);
3319 * Make sure we were able to grow the vdev.
3321 if (new_ms_count <= old_ms_count)
3322 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3323 old_ms_count, new_ms_count);
3326 * Make sure we were able to grow the pool.
3328 if (new_class_space <= old_class_space)
3329 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3330 old_class_space, new_class_space);
3332 if (ztest_opts.zo_verbose >= 5) {
3333 char oldnumbuf[6], newnumbuf[6];
3335 nicenum(old_class_space, oldnumbuf);
3336 nicenum(new_class_space, newnumbuf);
3337 (void) printf("%s grew from %s to %s\n",
3338 spa->spa_name, oldnumbuf, newnumbuf);
3341 spa_config_exit(spa, SCL_STATE, spa);
3342 mutex_exit(&ztest_vdev_lock);
3346 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3350 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3353 * Create the objects common to all ztest datasets.
3355 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3356 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3360 ztest_dataset_create(char *dsname)
3362 uint64_t zilset = ztest_random(100);
3363 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3364 ztest_objset_create_cb, NULL);
3366 if (err || zilset < 80)
3369 if (ztest_opts.zo_verbose >= 5)
3370 (void) printf("Setting dataset %s to sync always\n", dsname);
3371 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3372 ZFS_SYNC_ALWAYS, B_FALSE));
3377 ztest_objset_destroy_cb(const char *name, void *arg)
3380 dmu_object_info_t doi;
3384 * Verify that the dataset contains a directory object.
3386 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3387 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3388 if (error != ENOENT) {
3389 /* We could have crashed in the middle of destroying it */
3391 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3392 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3394 dmu_objset_disown(os, FTAG);
3397 * Destroy the dataset.
3399 if (strchr(name, '@') != NULL) {
3400 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3402 VERIFY0(dsl_destroy_head(name));
3408 ztest_snapshot_create(char *osname, uint64_t id)
3410 char snapname[MAXNAMELEN];
3413 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3415 error = dmu_objset_snapshot_one(osname, snapname);
3416 if (error == ENOSPC) {
3417 ztest_record_enospc(FTAG);
3420 if (error != 0 && error != EEXIST) {
3421 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3428 ztest_snapshot_destroy(char *osname, uint64_t id)
3430 char snapname[MAXNAMELEN];
3433 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3436 error = dsl_destroy_snapshot(snapname, B_FALSE);
3437 if (error != 0 && error != ENOENT)
3438 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3444 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3454 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3455 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3457 (void) rw_rdlock(&ztest_name_lock);
3459 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3460 ztest_opts.zo_pool, (u_longlong_t)id);
3463 * If this dataset exists from a previous run, process its replay log
3464 * half of the time. If we don't replay it, then dsl_destroy_head()
3465 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3467 if (ztest_random(2) == 0 &&
3468 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3469 ztest_zd_init(zdtmp, NULL, os);
3470 zil_replay(os, zdtmp, ztest_replay_vector);
3471 ztest_zd_fini(zdtmp);
3472 dmu_objset_disown(os, FTAG);
3476 * There may be an old instance of the dataset we're about to
3477 * create lying around from a previous run. If so, destroy it
3478 * and all of its snapshots.
3480 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3481 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3484 * Verify that the destroyed dataset is no longer in the namespace.
3486 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3490 * Verify that we can create a new dataset.
3492 error = ztest_dataset_create(name);
3494 if (error == ENOSPC) {
3495 ztest_record_enospc(FTAG);
3498 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3501 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3503 ztest_zd_init(zdtmp, NULL, os);
3506 * Open the intent log for it.
3508 zilog = zil_open(os, ztest_get_data);
3511 * Put some objects in there, do a little I/O to them,
3512 * and randomly take a couple of snapshots along the way.
3514 iters = ztest_random(5);
3515 for (i = 0; i < iters; i++) {
3516 ztest_dmu_object_alloc_free(zdtmp, id);
3517 if (ztest_random(iters) == 0)
3518 (void) ztest_snapshot_create(name, i);
3522 * Verify that we cannot create an existing dataset.
3524 VERIFY3U(EEXIST, ==,
3525 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3528 * Verify that we can hold an objset that is also owned.
3530 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3531 dmu_objset_rele(os2, FTAG);
3534 * Verify that we cannot own an objset that is already owned.
3537 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3540 dmu_objset_disown(os, FTAG);
3541 ztest_zd_fini(zdtmp);
3543 (void) rw_unlock(&ztest_name_lock);
3545 umem_free(name, MAXNAMELEN);
3546 umem_free(zdtmp, sizeof (ztest_ds_t));
3550 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3553 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3555 (void) rw_rdlock(&ztest_name_lock);
3556 (void) ztest_snapshot_destroy(zd->zd_name, id);
3557 (void) ztest_snapshot_create(zd->zd_name, id);
3558 (void) rw_unlock(&ztest_name_lock);
3562 * Cleanup non-standard snapshots and clones.
3565 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3574 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3575 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3576 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3577 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3578 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3580 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3581 osname, (u_longlong_t)id);
3582 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3583 osname, (u_longlong_t)id);
3584 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3585 clone1name, (u_longlong_t)id);
3586 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3587 osname, (u_longlong_t)id);
3588 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3589 clone1name, (u_longlong_t)id);
3591 error = dsl_destroy_head(clone2name);
3592 if (error && error != ENOENT)
3593 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3594 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3595 if (error && error != ENOENT)
3596 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3597 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3598 if (error && error != ENOENT)
3599 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3600 error = dsl_destroy_head(clone1name);
3601 if (error && error != ENOENT)
3602 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3603 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3604 if (error && error != ENOENT)
3605 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3607 umem_free(snap1name, MAXNAMELEN);
3608 umem_free(clone1name, MAXNAMELEN);
3609 umem_free(snap2name, MAXNAMELEN);
3610 umem_free(clone2name, MAXNAMELEN);
3611 umem_free(snap3name, MAXNAMELEN);
3615 * Verify dsl_dataset_promote handles EBUSY
3618 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3626 char *osname = zd->zd_name;
3629 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3630 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3631 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3632 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3633 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3635 (void) rw_rdlock(&ztest_name_lock);
3637 ztest_dsl_dataset_cleanup(osname, id);
3639 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3640 osname, (u_longlong_t)id);
3641 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3642 osname, (u_longlong_t)id);
3643 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3644 clone1name, (u_longlong_t)id);
3645 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3646 osname, (u_longlong_t)id);
3647 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3648 clone1name, (u_longlong_t)id);
3650 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3651 if (error && error != EEXIST) {
3652 if (error == ENOSPC) {
3653 ztest_record_enospc(FTAG);
3656 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3659 error = dmu_objset_clone(clone1name, snap1name);
3661 if (error == ENOSPC) {
3662 ztest_record_enospc(FTAG);
3665 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3668 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3669 if (error && error != EEXIST) {
3670 if (error == ENOSPC) {
3671 ztest_record_enospc(FTAG);
3674 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3677 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3678 if (error && error != EEXIST) {
3679 if (error == ENOSPC) {
3680 ztest_record_enospc(FTAG);
3683 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3686 error = dmu_objset_clone(clone2name, snap3name);
3688 if (error == ENOSPC) {
3689 ztest_record_enospc(FTAG);
3692 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3695 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3697 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3698 error = dsl_dataset_promote(clone2name, NULL);
3699 if (error == ENOSPC) {
3700 dmu_objset_disown(os, FTAG);
3701 ztest_record_enospc(FTAG);
3705 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3707 dmu_objset_disown(os, FTAG);
3710 ztest_dsl_dataset_cleanup(osname, id);
3712 (void) rw_unlock(&ztest_name_lock);
3714 umem_free(snap1name, MAXNAMELEN);
3715 umem_free(clone1name, MAXNAMELEN);
3716 umem_free(snap2name, MAXNAMELEN);
3717 umem_free(clone2name, MAXNAMELEN);
3718 umem_free(snap3name, MAXNAMELEN);
3721 #undef OD_ARRAY_SIZE
3722 #define OD_ARRAY_SIZE 4
3725 * Verify that dmu_object_{alloc,free} work as expected.
3728 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3735 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3736 od = umem_alloc(size, UMEM_NOFAIL);
3737 batchsize = OD_ARRAY_SIZE;
3739 for (b = 0; b < batchsize; b++)
3740 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3743 * Destroy the previous batch of objects, create a new batch,
3744 * and do some I/O on the new objects.
3746 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3749 while (ztest_random(4 * batchsize) != 0)
3750 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3751 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3753 umem_free(od, size);
3756 #undef OD_ARRAY_SIZE
3757 #define OD_ARRAY_SIZE 2
3760 * Verify that dmu_{read,write} work as expected.
3763 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3768 objset_t *os = zd->zd_os;
3769 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3770 od = umem_alloc(size, UMEM_NOFAIL);
3772 int i, freeit, error;
3774 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3775 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3776 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3777 uint64_t regions = 997;
3778 uint64_t stride = 123456789ULL;
3779 uint64_t width = 40;
3780 int free_percent = 5;
3783 * This test uses two objects, packobj and bigobj, that are always
3784 * updated together (i.e. in the same tx) so that their contents are
3785 * in sync and can be compared. Their contents relate to each other
3786 * in a simple way: packobj is a dense array of 'bufwad' structures,
3787 * while bigobj is a sparse array of the same bufwads. Specifically,
3788 * for any index n, there are three bufwads that should be identical:
3790 * packobj, at offset n * sizeof (bufwad_t)
3791 * bigobj, at the head of the nth chunk
3792 * bigobj, at the tail of the nth chunk
3794 * The chunk size is arbitrary. It doesn't have to be a power of two,
3795 * and it doesn't have any relation to the object blocksize.
3796 * The only requirement is that it can hold at least two bufwads.
3798 * Normally, we write the bufwad to each of these locations.
3799 * However, free_percent of the time we instead write zeroes to
3800 * packobj and perform a dmu_free_range() on bigobj. By comparing
3801 * bigobj to packobj, we can verify that the DMU is correctly
3802 * tracking which parts of an object are allocated and free,
3803 * and that the contents of the allocated blocks are correct.
3807 * Read the directory info. If it's the first time, set things up.
3809 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3810 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3812 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3813 umem_free(od, size);
3817 bigobj = od[0].od_object;
3818 packobj = od[1].od_object;
3819 chunksize = od[0].od_gen;
3820 ASSERT(chunksize == od[1].od_gen);
3823 * Prefetch a random chunk of the big object.
3824 * Our aim here is to get some async reads in flight
3825 * for blocks that we may free below; the DMU should
3826 * handle this race correctly.
3828 n = ztest_random(regions) * stride + ztest_random(width);
3829 s = 1 + ztest_random(2 * width - 1);
3830 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3831 ZIO_PRIORITY_SYNC_READ);
3834 * Pick a random index and compute the offsets into packobj and bigobj.
3836 n = ztest_random(regions) * stride + ztest_random(width);
3837 s = 1 + ztest_random(width - 1);
3839 packoff = n * sizeof (bufwad_t);
3840 packsize = s * sizeof (bufwad_t);
3842 bigoff = n * chunksize;
3843 bigsize = s * chunksize;
3845 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3846 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3849 * free_percent of the time, free a range of bigobj rather than
3852 freeit = (ztest_random(100) < free_percent);
3855 * Read the current contents of our objects.
3857 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3860 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3865 * Get a tx for the mods to both packobj and bigobj.
3867 tx = dmu_tx_create(os);
3869 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3872 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3874 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3876 /* This accounts for setting the checksum/compression. */
3877 dmu_tx_hold_bonus(tx, bigobj);
3879 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3881 umem_free(packbuf, packsize);
3882 umem_free(bigbuf, bigsize);
3883 umem_free(od, size);
3887 enum zio_checksum cksum;
3889 cksum = (enum zio_checksum)
3890 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3891 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3892 dmu_object_set_checksum(os, bigobj, cksum, tx);
3894 enum zio_compress comp;
3896 comp = (enum zio_compress)
3897 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3898 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3899 dmu_object_set_compress(os, bigobj, comp, tx);
3902 * For each index from n to n + s, verify that the existing bufwad
3903 * in packobj matches the bufwads at the head and tail of the
3904 * corresponding chunk in bigobj. Then update all three bufwads
3905 * with the new values we want to write out.
3907 for (i = 0; i < s; i++) {
3909 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3911 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3913 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3915 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3916 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3918 if (pack->bw_txg > txg)
3919 fatal(0, "future leak: got %llx, open txg is %llx",
3922 if (pack->bw_data != 0 && pack->bw_index != n + i)
3923 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3924 pack->bw_index, n, i);
3926 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3927 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3929 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3930 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3933 bzero(pack, sizeof (bufwad_t));
3935 pack->bw_index = n + i;
3937 pack->bw_data = 1 + ztest_random(-2ULL);
3944 * We've verified all the old bufwads, and made new ones.
3945 * Now write them out.
3947 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3950 if (ztest_opts.zo_verbose >= 7) {
3951 (void) printf("freeing offset %llx size %llx"
3953 (u_longlong_t)bigoff,
3954 (u_longlong_t)bigsize,
3957 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3959 if (ztest_opts.zo_verbose >= 7) {
3960 (void) printf("writing offset %llx size %llx"
3962 (u_longlong_t)bigoff,
3963 (u_longlong_t)bigsize,
3966 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3972 * Sanity check the stuff we just wrote.
3975 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3976 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3978 VERIFY(0 == dmu_read(os, packobj, packoff,
3979 packsize, packcheck, DMU_READ_PREFETCH));
3980 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3981 bigsize, bigcheck, DMU_READ_PREFETCH));
3983 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3984 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3986 umem_free(packcheck, packsize);
3987 umem_free(bigcheck, bigsize);
3990 umem_free(packbuf, packsize);
3991 umem_free(bigbuf, bigsize);
3992 umem_free(od, size);
3996 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3997 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4005 * For each index from n to n + s, verify that the existing bufwad
4006 * in packobj matches the bufwads at the head and tail of the
4007 * corresponding chunk in bigobj. Then update all three bufwads
4008 * with the new values we want to write out.
4010 for (i = 0; i < s; i++) {
4012 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4014 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4016 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4018 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4019 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4021 if (pack->bw_txg > txg)
4022 fatal(0, "future leak: got %llx, open txg is %llx",
4025 if (pack->bw_data != 0 && pack->bw_index != n + i)
4026 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4027 pack->bw_index, n, i);
4029 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4030 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4032 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4033 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4035 pack->bw_index = n + i;
4037 pack->bw_data = 1 + ztest_random(-2ULL);
4044 #undef OD_ARRAY_SIZE
4045 #define OD_ARRAY_SIZE 2
4048 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4050 objset_t *os = zd->zd_os;
4057 bufwad_t *packbuf, *bigbuf;
4058 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4059 uint64_t blocksize = ztest_random_blocksize();
4060 uint64_t chunksize = blocksize;
4061 uint64_t regions = 997;
4062 uint64_t stride = 123456789ULL;
4064 dmu_buf_t *bonus_db;
4065 arc_buf_t **bigbuf_arcbufs;
4066 dmu_object_info_t doi;
4068 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4069 od = umem_alloc(size, UMEM_NOFAIL);
4072 * This test uses two objects, packobj and bigobj, that are always
4073 * updated together (i.e. in the same tx) so that their contents are
4074 * in sync and can be compared. Their contents relate to each other
4075 * in a simple way: packobj is a dense array of 'bufwad' structures,
4076 * while bigobj is a sparse array of the same bufwads. Specifically,
4077 * for any index n, there are three bufwads that should be identical:
4079 * packobj, at offset n * sizeof (bufwad_t)
4080 * bigobj, at the head of the nth chunk
4081 * bigobj, at the tail of the nth chunk
4083 * The chunk size is set equal to bigobj block size so that
4084 * dmu_assign_arcbuf() can be tested for object updates.
4088 * Read the directory info. If it's the first time, set things up.
4090 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4091 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
4094 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4095 umem_free(od, size);
4099 bigobj = od[0].od_object;
4100 packobj = od[1].od_object;
4101 blocksize = od[0].od_blocksize;
4102 chunksize = blocksize;
4103 ASSERT(chunksize == od[1].od_gen);
4105 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4106 VERIFY(ISP2(doi.doi_data_block_size));
4107 VERIFY(chunksize == doi.doi_data_block_size);
4108 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4111 * Pick a random index and compute the offsets into packobj and bigobj.
4113 n = ztest_random(regions) * stride + ztest_random(width);
4114 s = 1 + ztest_random(width - 1);
4116 packoff = n * sizeof (bufwad_t);
4117 packsize = s * sizeof (bufwad_t);
4119 bigoff = n * chunksize;
4120 bigsize = s * chunksize;
4122 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4123 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4125 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4127 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4130 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4131 * Iteration 1 test zcopy to already referenced dbufs.
4132 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4133 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4134 * Iteration 4 test zcopy when dbuf is no longer dirty.
4135 * Iteration 5 test zcopy when it can't be done.
4136 * Iteration 6 one more zcopy write.
4138 for (i = 0; i < 7; i++) {
4143 * In iteration 5 (i == 5) use arcbufs
4144 * that don't match bigobj blksz to test
4145 * dmu_assign_arcbuf() when it can't directly
4146 * assign an arcbuf to a dbuf.
4148 for (j = 0; j < s; j++) {
4149 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4151 dmu_request_arcbuf(bonus_db, chunksize);
4153 bigbuf_arcbufs[2 * j] =
4154 dmu_request_arcbuf(bonus_db, chunksize / 2);
4155 bigbuf_arcbufs[2 * j + 1] =
4156 dmu_request_arcbuf(bonus_db, chunksize / 2);
4161 * Get a tx for the mods to both packobj and bigobj.
4163 tx = dmu_tx_create(os);
4165 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4166 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4168 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4170 umem_free(packbuf, packsize);
4171 umem_free(bigbuf, bigsize);
4172 for (j = 0; j < s; j++) {
4174 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4175 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4178 bigbuf_arcbufs[2 * j]);
4180 bigbuf_arcbufs[2 * j + 1]);
4183 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4184 umem_free(od, size);
4185 dmu_buf_rele(bonus_db, FTAG);
4190 * 50% of the time don't read objects in the 1st iteration to
4191 * test dmu_assign_arcbuf() for the case when there're no
4192 * existing dbufs for the specified offsets.
4194 if (i != 0 || ztest_random(2) != 0) {
4195 error = dmu_read(os, packobj, packoff,
4196 packsize, packbuf, DMU_READ_PREFETCH);
4198 error = dmu_read(os, bigobj, bigoff, bigsize,
4199 bigbuf, DMU_READ_PREFETCH);
4202 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4206 * We've verified all the old bufwads, and made new ones.
4207 * Now write them out.
4209 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4210 if (ztest_opts.zo_verbose >= 7) {
4211 (void) printf("writing offset %llx size %llx"
4213 (u_longlong_t)bigoff,
4214 (u_longlong_t)bigsize,
4217 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4219 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4220 bcopy((caddr_t)bigbuf + (off - bigoff),
4221 bigbuf_arcbufs[j]->b_data, chunksize);
4223 bcopy((caddr_t)bigbuf + (off - bigoff),
4224 bigbuf_arcbufs[2 * j]->b_data,
4226 bcopy((caddr_t)bigbuf + (off - bigoff) +
4228 bigbuf_arcbufs[2 * j + 1]->b_data,
4233 VERIFY(dmu_buf_hold(os, bigobj, off,
4234 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4236 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4237 dmu_assign_arcbuf(bonus_db, off,
4238 bigbuf_arcbufs[j], tx);
4240 dmu_assign_arcbuf(bonus_db, off,
4241 bigbuf_arcbufs[2 * j], tx);
4242 dmu_assign_arcbuf(bonus_db,
4243 off + chunksize / 2,
4244 bigbuf_arcbufs[2 * j + 1], tx);
4247 dmu_buf_rele(dbt, FTAG);
4253 * Sanity check the stuff we just wrote.
4256 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4257 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4259 VERIFY(0 == dmu_read(os, packobj, packoff,
4260 packsize, packcheck, DMU_READ_PREFETCH));
4261 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4262 bigsize, bigcheck, DMU_READ_PREFETCH));
4264 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4265 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4267 umem_free(packcheck, packsize);
4268 umem_free(bigcheck, bigsize);
4271 txg_wait_open(dmu_objset_pool(os), 0);
4272 } else if (i == 3) {
4273 txg_wait_synced(dmu_objset_pool(os), 0);
4277 dmu_buf_rele(bonus_db, FTAG);
4278 umem_free(packbuf, packsize);
4279 umem_free(bigbuf, bigsize);
4280 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4281 umem_free(od, size);
4286 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4290 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4291 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4292 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4295 * Have multiple threads write to large offsets in an object
4296 * to verify that parallel writes to an object -- even to the
4297 * same blocks within the object -- doesn't cause any trouble.
4299 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4301 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4304 while (ztest_random(10) != 0)
4305 ztest_io(zd, od->od_object, offset);
4307 umem_free(od, sizeof (ztest_od_t));
4311 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4314 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4315 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4316 uint64_t count = ztest_random(20) + 1;
4317 uint64_t blocksize = ztest_random_blocksize();
4320 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4322 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4324 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4325 !ztest_random(2)) != 0) {
4326 umem_free(od, sizeof (ztest_od_t));
4330 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4331 umem_free(od, sizeof (ztest_od_t));
4335 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4337 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4339 while (ztest_random(count) != 0) {
4340 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4341 if (ztest_write(zd, od->od_object, randoff, blocksize,
4344 while (ztest_random(4) != 0)
4345 ztest_io(zd, od->od_object, randoff);
4348 umem_free(data, blocksize);
4349 umem_free(od, sizeof (ztest_od_t));
4353 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4355 #define ZTEST_ZAP_MIN_INTS 1
4356 #define ZTEST_ZAP_MAX_INTS 4
4357 #define ZTEST_ZAP_MAX_PROPS 1000
4360 ztest_zap(ztest_ds_t *zd, uint64_t id)
4362 objset_t *os = zd->zd_os;
4365 uint64_t txg, last_txg;
4366 uint64_t value[ZTEST_ZAP_MAX_INTS];
4367 uint64_t zl_ints, zl_intsize, prop;
4370 char propname[100], txgname[100];
4372 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4374 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4375 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4377 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4378 !ztest_random(2)) != 0)
4381 object = od->od_object;
4384 * Generate a known hash collision, and verify that
4385 * we can lookup and remove both entries.
4387 tx = dmu_tx_create(os);
4388 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4389 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4392 for (i = 0; i < 2; i++) {
4394 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4397 for (i = 0; i < 2; i++) {
4398 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4399 sizeof (uint64_t), 1, &value[i], tx));
4401 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4402 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4403 ASSERT3U(zl_ints, ==, 1);
4405 for (i = 0; i < 2; i++) {
4406 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4411 * Generate a buch of random entries.
4413 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4415 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4416 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4417 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4418 bzero(value, sizeof (value));
4422 * If these zap entries already exist, validate their contents.
4424 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4426 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4427 ASSERT3U(zl_ints, ==, 1);
4429 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4430 zl_ints, &last_txg) == 0);
4432 VERIFY(zap_length(os, object, propname, &zl_intsize,
4435 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4436 ASSERT3U(zl_ints, ==, ints);
4438 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4439 zl_ints, value) == 0);
4441 for (i = 0; i < ints; i++) {
4442 ASSERT3U(value[i], ==, last_txg + object + i);
4445 ASSERT3U(error, ==, ENOENT);
4449 * Atomically update two entries in our zap object.
4450 * The first is named txg_%llu, and contains the txg
4451 * in which the property was last updated. The second
4452 * is named prop_%llu, and the nth element of its value
4453 * should be txg + object + n.
4455 tx = dmu_tx_create(os);
4456 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4457 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4462 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4464 for (i = 0; i < ints; i++)
4465 value[i] = txg + object + i;
4467 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4469 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4475 * Remove a random pair of entries.
4477 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4478 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4479 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4481 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4483 if (error == ENOENT)
4488 tx = dmu_tx_create(os);
4489 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4490 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4493 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4494 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4497 umem_free(od, sizeof (ztest_od_t));
4501 * Testcase to test the upgrading of a microzap to fatzap.
4504 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4506 objset_t *os = zd->zd_os;
4508 uint64_t object, txg;
4511 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4512 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4514 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4515 !ztest_random(2)) != 0)
4517 object = od->od_object;
4520 * Add entries to this ZAP and make sure it spills over
4521 * and gets upgraded to a fatzap. Also, since we are adding
4522 * 2050 entries we should see ptrtbl growth and leaf-block split.
4524 for (i = 0; i < 2050; i++) {
4525 char name[MAXNAMELEN];
4530 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4531 (u_longlong_t)id, (u_longlong_t)value);
4533 tx = dmu_tx_create(os);
4534 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4535 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4538 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4540 ASSERT(error == 0 || error == EEXIST);
4544 umem_free(od, sizeof (ztest_od_t));
4549 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4551 objset_t *os = zd->zd_os;
4553 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4555 int i, namelen, error;
4556 int micro = ztest_random(2);
4557 char name[20], string_value[20];
4560 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4561 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4563 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4564 umem_free(od, sizeof (ztest_od_t));
4568 object = od->od_object;
4571 * Generate a random name of the form 'xxx.....' where each
4572 * x is a random printable character and the dots are dots.
4573 * There are 94 such characters, and the name length goes from
4574 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4576 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4578 for (i = 0; i < 3; i++)
4579 name[i] = '!' + ztest_random('~' - '!' + 1);
4580 for (; i < namelen - 1; i++)
4584 if ((namelen & 1) || micro) {
4585 wsize = sizeof (txg);
4591 data = string_value;
4595 VERIFY0(zap_count(os, object, &count));
4596 ASSERT(count != -1ULL);
4599 * Select an operation: length, lookup, add, update, remove.
4601 i = ztest_random(5);
4604 tx = dmu_tx_create(os);
4605 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4606 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4609 bcopy(name, string_value, namelen);
4613 bzero(string_value, namelen);
4619 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4621 ASSERT3U(wsize, ==, zl_wsize);
4622 ASSERT3U(wc, ==, zl_wc);
4624 ASSERT3U(error, ==, ENOENT);
4629 error = zap_lookup(os, object, name, wsize, wc, data);
4631 if (data == string_value &&
4632 bcmp(name, data, namelen) != 0)
4633 fatal(0, "name '%s' != val '%s' len %d",
4634 name, data, namelen);
4636 ASSERT3U(error, ==, ENOENT);
4641 error = zap_add(os, object, name, wsize, wc, data, tx);
4642 ASSERT(error == 0 || error == EEXIST);
4646 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4650 error = zap_remove(os, object, name, tx);
4651 ASSERT(error == 0 || error == ENOENT);
4658 umem_free(od, sizeof (ztest_od_t));
4662 * Commit callback data.
4664 typedef struct ztest_cb_data {
4665 list_node_t zcd_node;
4667 int zcd_expected_err;
4668 boolean_t zcd_added;
4669 boolean_t zcd_called;
4673 /* This is the actual commit callback function */
4675 ztest_commit_callback(void *arg, int error)
4677 ztest_cb_data_t *data = arg;
4678 uint64_t synced_txg;
4680 VERIFY(data != NULL);
4681 VERIFY3S(data->zcd_expected_err, ==, error);
4682 VERIFY(!data->zcd_called);
4684 synced_txg = spa_last_synced_txg(data->zcd_spa);
4685 if (data->zcd_txg > synced_txg)
4686 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4687 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4690 data->zcd_called = B_TRUE;
4692 if (error == ECANCELED) {
4693 ASSERT0(data->zcd_txg);
4694 ASSERT(!data->zcd_added);
4697 * The private callback data should be destroyed here, but
4698 * since we are going to check the zcd_called field after
4699 * dmu_tx_abort(), we will destroy it there.
4704 ASSERT(data->zcd_added);
4705 ASSERT3U(data->zcd_txg, !=, 0);
4707 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4709 /* See if this cb was called more quickly */
4710 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4711 zc_min_txg_delay = synced_txg - data->zcd_txg;
4713 /* Remove our callback from the list */
4714 list_remove(&zcl.zcl_callbacks, data);
4716 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4718 umem_free(data, sizeof (ztest_cb_data_t));
4721 /* Allocate and initialize callback data structure */
4722 static ztest_cb_data_t *
4723 ztest_create_cb_data(objset_t *os, uint64_t txg)
4725 ztest_cb_data_t *cb_data;
4727 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4729 cb_data->zcd_txg = txg;
4730 cb_data->zcd_spa = dmu_objset_spa(os);
4731 list_link_init(&cb_data->zcd_node);
4737 * Commit callback test.
4740 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4742 objset_t *os = zd->zd_os;
4745 ztest_cb_data_t *cb_data[3], *tmp_cb;
4746 uint64_t old_txg, txg;
4749 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4750 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4752 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4753 umem_free(od, sizeof (ztest_od_t));
4757 tx = dmu_tx_create(os);
4759 cb_data[0] = ztest_create_cb_data(os, 0);
4760 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4762 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4764 /* Every once in a while, abort the transaction on purpose */
4765 if (ztest_random(100) == 0)
4769 error = dmu_tx_assign(tx, TXG_NOWAIT);
4771 txg = error ? 0 : dmu_tx_get_txg(tx);
4773 cb_data[0]->zcd_txg = txg;
4774 cb_data[1] = ztest_create_cb_data(os, txg);
4775 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4779 * It's not a strict requirement to call the registered
4780 * callbacks from inside dmu_tx_abort(), but that's what
4781 * it's supposed to happen in the current implementation
4782 * so we will check for that.
4784 for (i = 0; i < 2; i++) {
4785 cb_data[i]->zcd_expected_err = ECANCELED;
4786 VERIFY(!cb_data[i]->zcd_called);
4791 for (i = 0; i < 2; i++) {
4792 VERIFY(cb_data[i]->zcd_called);
4793 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4796 umem_free(od, sizeof (ztest_od_t));
4800 cb_data[2] = ztest_create_cb_data(os, txg);
4801 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4804 * Read existing data to make sure there isn't a future leak.
4806 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4807 &old_txg, DMU_READ_PREFETCH));
4810 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4813 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4815 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4818 * Since commit callbacks don't have any ordering requirement and since
4819 * it is theoretically possible for a commit callback to be called
4820 * after an arbitrary amount of time has elapsed since its txg has been
4821 * synced, it is difficult to reliably determine whether a commit
4822 * callback hasn't been called due to high load or due to a flawed
4825 * In practice, we will assume that if after a certain number of txgs a
4826 * commit callback hasn't been called, then most likely there's an
4827 * implementation bug..
4829 tmp_cb = list_head(&zcl.zcl_callbacks);
4830 if (tmp_cb != NULL &&
4831 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4832 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4833 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4837 * Let's find the place to insert our callbacks.
4839 * Even though the list is ordered by txg, it is possible for the
4840 * insertion point to not be the end because our txg may already be
4841 * quiescing at this point and other callbacks in the open txg
4842 * (from other objsets) may have sneaked in.
4844 tmp_cb = list_tail(&zcl.zcl_callbacks);
4845 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4846 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4848 /* Add the 3 callbacks to the list */
4849 for (i = 0; i < 3; i++) {
4851 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4853 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4856 cb_data[i]->zcd_added = B_TRUE;
4857 VERIFY(!cb_data[i]->zcd_called);
4859 tmp_cb = cb_data[i];
4864 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4868 umem_free(od, sizeof (ztest_od_t));
4873 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4875 zfs_prop_t proplist[] = {
4877 ZFS_PROP_COMPRESSION,
4883 (void) rw_rdlock(&ztest_name_lock);
4885 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4886 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4887 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4889 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
4890 ztest_random_blocksize(), (int)ztest_random(2)));
4892 (void) rw_unlock(&ztest_name_lock);
4897 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4899 nvlist_t *props = NULL;
4901 (void) rw_rdlock(&ztest_name_lock);
4903 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4904 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4906 VERIFY0(spa_prop_get(ztest_spa, &props));
4908 if (ztest_opts.zo_verbose >= 6)
4909 dump_nvlist(props, 4);
4913 (void) rw_unlock(&ztest_name_lock);
4917 user_release_one(const char *snapname, const char *holdname)
4919 nvlist_t *snaps, *holds;
4922 snaps = fnvlist_alloc();
4923 holds = fnvlist_alloc();
4924 fnvlist_add_boolean(holds, holdname);
4925 fnvlist_add_nvlist(snaps, snapname, holds);
4926 fnvlist_free(holds);
4927 error = dsl_dataset_user_release(snaps, NULL);
4928 fnvlist_free(snaps);
4933 * Test snapshot hold/release and deferred destroy.
4936 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4939 objset_t *os = zd->zd_os;
4943 char clonename[100];
4945 char osname[MAXNAMELEN];
4948 (void) rw_rdlock(&ztest_name_lock);
4950 dmu_objset_name(os, osname);
4952 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4954 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4955 (void) snprintf(clonename, sizeof (clonename),
4956 "%s/ch1_%llu", osname, (u_longlong_t)id);
4957 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4960 * Clean up from any previous run.
4962 error = dsl_destroy_head(clonename);
4963 if (error != ENOENT)
4965 error = user_release_one(fullname, tag);
4966 if (error != ESRCH && error != ENOENT)
4968 error = dsl_destroy_snapshot(fullname, B_FALSE);
4969 if (error != ENOENT)
4973 * Create snapshot, clone it, mark snap for deferred destroy,
4974 * destroy clone, verify snap was also destroyed.
4976 error = dmu_objset_snapshot_one(osname, snapname);
4978 if (error == ENOSPC) {
4979 ztest_record_enospc("dmu_objset_snapshot");
4982 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4985 error = dmu_objset_clone(clonename, fullname);
4987 if (error == ENOSPC) {
4988 ztest_record_enospc("dmu_objset_clone");
4991 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4994 error = dsl_destroy_snapshot(fullname, B_TRUE);
4996 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5000 error = dsl_destroy_head(clonename);
5002 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5004 error = dmu_objset_hold(fullname, FTAG, &origin);
5005 if (error != ENOENT)
5006 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5009 * Create snapshot, add temporary hold, verify that we can't
5010 * destroy a held snapshot, mark for deferred destroy,
5011 * release hold, verify snapshot was destroyed.
5013 error = dmu_objset_snapshot_one(osname, snapname);
5015 if (error == ENOSPC) {
5016 ztest_record_enospc("dmu_objset_snapshot");
5019 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5022 holds = fnvlist_alloc();
5023 fnvlist_add_string(holds, fullname, tag);
5024 error = dsl_dataset_user_hold(holds, 0, NULL);
5025 fnvlist_free(holds);
5027 if (error == ENOSPC) {
5028 ztest_record_enospc("dsl_dataset_user_hold");
5031 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5032 fullname, tag, error);
5035 error = dsl_destroy_snapshot(fullname, B_FALSE);
5036 if (error != EBUSY) {
5037 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5041 error = dsl_destroy_snapshot(fullname, B_TRUE);
5043 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5047 error = user_release_one(fullname, tag);
5049 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5051 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5054 (void) rw_unlock(&ztest_name_lock);
5058 * Inject random faults into the on-disk data.
5062 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5064 ztest_shared_t *zs = ztest_shared;
5065 spa_t *spa = ztest_spa;
5069 uint64_t bad = 0x1990c0ffeedecadeull;
5074 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
5080 boolean_t islog = B_FALSE;
5082 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5083 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5085 mutex_enter(&ztest_vdev_lock);
5086 maxfaults = MAXFAULTS();
5087 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5088 mirror_save = zs->zs_mirrors;
5089 mutex_exit(&ztest_vdev_lock);
5091 ASSERT(leaves >= 1);
5094 * Grab the name lock as reader. There are some operations
5095 * which don't like to have their vdevs changed while
5096 * they are in progress (i.e. spa_change_guid). Those
5097 * operations will have grabbed the name lock as writer.
5099 (void) rw_rdlock(&ztest_name_lock);
5102 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5104 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5106 if (ztest_random(2) == 0) {
5108 * Inject errors on a normal data device or slog device.
5110 top = ztest_random_vdev_top(spa, B_TRUE);
5111 leaf = ztest_random(leaves) + zs->zs_splits;
5114 * Generate paths to the first leaf in this top-level vdev,
5115 * and to the random leaf we selected. We'll induce transient
5116 * write failures and random online/offline activity on leaf 0,
5117 * and we'll write random garbage to the randomly chosen leaf.
5119 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5120 ztest_opts.zo_dir, ztest_opts.zo_pool,
5121 top * leaves + zs->zs_splits);
5122 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5123 ztest_opts.zo_dir, ztest_opts.zo_pool,
5124 top * leaves + leaf);
5126 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5127 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5131 * If the top-level vdev needs to be resilvered
5132 * then we only allow faults on the device that is
5135 if (vd0 != NULL && maxfaults != 1 &&
5136 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5137 vd0->vdev_resilver_txg != 0)) {
5139 * Make vd0 explicitly claim to be unreadable,
5140 * or unwriteable, or reach behind its back
5141 * and close the underlying fd. We can do this if
5142 * maxfaults == 0 because we'll fail and reexecute,
5143 * and we can do it if maxfaults >= 2 because we'll
5144 * have enough redundancy. If maxfaults == 1, the
5145 * combination of this with injection of random data
5146 * corruption below exceeds the pool's fault tolerance.
5148 vdev_file_t *vf = vd0->vdev_tsd;
5150 if (vf != NULL && ztest_random(3) == 0) {
5151 (void) close(vf->vf_vnode->v_fd);
5152 vf->vf_vnode->v_fd = -1;
5153 } else if (ztest_random(2) == 0) {
5154 vd0->vdev_cant_read = B_TRUE;
5156 vd0->vdev_cant_write = B_TRUE;
5158 guid0 = vd0->vdev_guid;
5162 * Inject errors on an l2cache device.
5164 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5166 if (sav->sav_count == 0) {
5167 spa_config_exit(spa, SCL_STATE, FTAG);
5168 (void) rw_unlock(&ztest_name_lock);
5171 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5172 guid0 = vd0->vdev_guid;
5173 (void) strcpy(path0, vd0->vdev_path);
5174 (void) strcpy(pathrand, vd0->vdev_path);
5178 maxfaults = INT_MAX; /* no limit on cache devices */
5181 spa_config_exit(spa, SCL_STATE, FTAG);
5182 (void) rw_unlock(&ztest_name_lock);
5185 * If we can tolerate two or more faults, or we're dealing
5186 * with a slog, randomly online/offline vd0.
5188 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5189 if (ztest_random(10) < 6) {
5190 int flags = (ztest_random(2) == 0 ?
5191 ZFS_OFFLINE_TEMPORARY : 0);
5194 * We have to grab the zs_name_lock as writer to
5195 * prevent a race between offlining a slog and
5196 * destroying a dataset. Offlining the slog will
5197 * grab a reference on the dataset which may cause
5198 * dsl_destroy_head() to fail with EBUSY thus
5199 * leaving the dataset in an inconsistent state.
5202 (void) rw_wrlock(&ztest_name_lock);
5204 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5207 (void) rw_unlock(&ztest_name_lock);
5210 * Ideally we would like to be able to randomly
5211 * call vdev_[on|off]line without holding locks
5212 * to force unpredictable failures but the side
5213 * effects of vdev_[on|off]line prevent us from
5214 * doing so. We grab the ztest_vdev_lock here to
5215 * prevent a race between injection testing and
5218 mutex_enter(&ztest_vdev_lock);
5219 (void) vdev_online(spa, guid0, 0, NULL);
5220 mutex_exit(&ztest_vdev_lock);
5228 * We have at least single-fault tolerance, so inject data corruption.
5230 fd = open(pathrand, O_RDWR);
5232 if (fd == -1) /* we hit a gap in the device namespace */
5235 fsize = lseek(fd, 0, SEEK_END);
5237 while (--iters != 0) {
5239 * The offset must be chosen carefully to ensure that
5240 * we do not inject a given logical block with errors
5241 * on two different leaf devices, because ZFS can not
5242 * tolerate that (if maxfaults==1).
5244 * We divide each leaf into chunks of size
5245 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5246 * there is a series of ranges to which we can inject errors.
5247 * Each range can accept errors on only a single leaf vdev.
5248 * The error injection ranges are separated by ranges
5249 * which we will not inject errors on any device (DMZs).
5250 * Each DMZ must be large enough such that a single block
5251 * can not straddle it, so that a single block can not be
5252 * a target in two different injection ranges (on different
5255 * For example, with 3 leaves, each chunk looks like:
5256 * 0 to 32M: injection range for leaf 0
5257 * 32M to 64M: DMZ - no injection allowed
5258 * 64M to 96M: injection range for leaf 1
5259 * 96M to 128M: DMZ - no injection allowed
5260 * 128M to 160M: injection range for leaf 2
5261 * 160M to 192M: DMZ - no injection allowed
5263 offset = ztest_random(fsize / (leaves << bshift)) *
5264 (leaves << bshift) + (leaf << bshift) +
5265 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5267 if (offset >= fsize)
5270 mutex_enter(&ztest_vdev_lock);
5271 if (mirror_save != zs->zs_mirrors) {
5272 mutex_exit(&ztest_vdev_lock);
5277 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5278 fatal(1, "can't inject bad word at 0x%llx in %s",
5281 mutex_exit(&ztest_vdev_lock);
5283 if (ztest_opts.zo_verbose >= 7)
5284 (void) printf("injected bad word into %s,"
5285 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5290 umem_free(path0, MAXPATHLEN);
5291 umem_free(pathrand, MAXPATHLEN);
5295 * Verify that DDT repair works as expected.
5298 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5300 ztest_shared_t *zs = ztest_shared;
5301 spa_t *spa = ztest_spa;
5302 objset_t *os = zd->zd_os;
5304 uint64_t object, blocksize, txg, pattern, psize;
5305 enum zio_checksum checksum = spa_dedup_checksum(spa);
5310 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5313 blocksize = ztest_random_blocksize();
5314 blocksize = MIN(blocksize, 2048); /* because we write so many */
5316 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5317 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5319 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5320 umem_free(od, sizeof (ztest_od_t));
5325 * Take the name lock as writer to prevent anyone else from changing
5326 * the pool and dataset properies we need to maintain during this test.
5328 (void) rw_wrlock(&ztest_name_lock);
5330 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5332 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5334 (void) rw_unlock(&ztest_name_lock);
5335 umem_free(od, sizeof (ztest_od_t));
5339 object = od[0].od_object;
5340 blocksize = od[0].od_blocksize;
5341 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5343 ASSERT(object != 0);
5345 tx = dmu_tx_create(os);
5346 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5347 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5349 (void) rw_unlock(&ztest_name_lock);
5350 umem_free(od, sizeof (ztest_od_t));
5355 * Write all the copies of our block.
5357 for (i = 0; i < copies; i++) {
5358 uint64_t offset = i * blocksize;
5359 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5360 DMU_READ_NO_PREFETCH);
5362 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5363 os, (long long)object, (long long) offset, error);
5365 ASSERT(db->db_offset == offset);
5366 ASSERT(db->db_size == blocksize);
5367 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5368 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5369 dmu_buf_will_fill(db, tx);
5370 ztest_pattern_set(db->db_data, db->db_size, pattern);
5371 dmu_buf_rele(db, FTAG);
5375 txg_wait_synced(spa_get_dsl(spa), txg);
5378 * Find out what block we got.
5380 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5381 DMU_READ_NO_PREFETCH));
5382 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5383 dmu_buf_rele(db, FTAG);
5386 * Damage the block. Dedup-ditto will save us when we read it later.
5388 psize = BP_GET_PSIZE(&blk);
5389 buf = zio_buf_alloc(psize);
5390 ztest_pattern_set(buf, psize, ~pattern);
5392 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5393 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5394 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5396 zio_buf_free(buf, psize);
5398 (void) rw_unlock(&ztest_name_lock);
5399 umem_free(od, sizeof (ztest_od_t));
5407 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5409 spa_t *spa = ztest_spa;
5411 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5412 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5413 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5417 * Change the guid for the pool.
5421 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5423 spa_t *spa = ztest_spa;
5424 uint64_t orig, load;
5427 orig = spa_guid(spa);
5428 load = spa_load_guid(spa);
5430 (void) rw_wrlock(&ztest_name_lock);
5431 error = spa_change_guid(spa);
5432 (void) rw_unlock(&ztest_name_lock);
5437 if (ztest_opts.zo_verbose >= 4) {
5438 (void) printf("Changed guid old %llu -> %llu\n",
5439 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5442 VERIFY3U(orig, !=, spa_guid(spa));
5443 VERIFY3U(load, ==, spa_load_guid(spa));
5447 * Rename the pool to a different name and then rename it back.
5451 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5453 char *oldname, *newname;
5456 (void) rw_wrlock(&ztest_name_lock);
5458 oldname = ztest_opts.zo_pool;
5459 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5460 (void) strcpy(newname, oldname);
5461 (void) strcat(newname, "_tmp");
5466 VERIFY3U(0, ==, spa_rename(oldname, newname));
5469 * Try to open it under the old name, which shouldn't exist
5471 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5474 * Open it under the new name and make sure it's still the same spa_t.
5476 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5478 ASSERT(spa == ztest_spa);
5479 spa_close(spa, FTAG);
5482 * Rename it back to the original
5484 VERIFY3U(0, ==, spa_rename(newname, oldname));
5487 * Make sure it can still be opened
5489 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5491 ASSERT(spa == ztest_spa);
5492 spa_close(spa, FTAG);
5494 umem_free(newname, strlen(newname) + 1);
5496 (void) rw_unlock(&ztest_name_lock);
5500 ztest_check_path(char *path)
5503 /* return true on success */
5504 return (!stat(path, &s));
5508 ztest_get_zdb_bin(char *bin, int len)
5512 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5513 * let popen to search through PATH.
5515 if ((zdb_path = getenv("ZDB_PATH"))) {
5516 strlcpy(bin, zdb_path, len); /* In env */
5517 if (!ztest_check_path(bin)) {
5518 ztest_dump_core = 0;
5519 fatal(1, "invalid ZDB_PATH '%s'", bin);
5524 VERIFY(realpath(getexecname(), bin) != NULL);
5525 if (strstr(bin, "/ztest/")) {
5526 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5527 strcat(bin, "/zdb/zdb");
5528 if (ztest_check_path(bin))
5535 * Verify pool integrity by running zdb.
5538 ztest_run_zdb(char *pool)
5544 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5547 bin = umem_alloc(len, UMEM_NOFAIL);
5548 zdb = umem_alloc(len, UMEM_NOFAIL);
5549 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5551 ztest_get_zdb_bin(bin, len);
5554 "%s -bcc%s%s -d -U %s %s",
5556 ztest_opts.zo_verbose >= 3 ? "s" : "",
5557 ztest_opts.zo_verbose >= 4 ? "v" : "",
5561 if (ztest_opts.zo_verbose >= 5)
5562 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5564 fp = popen(zdb, "r");
5566 while (fgets(zbuf, 1024, fp) != NULL)
5567 if (ztest_opts.zo_verbose >= 3)
5568 (void) printf("%s", zbuf);
5570 status = pclose(fp);
5575 ztest_dump_core = 0;
5576 if (WIFEXITED(status))
5577 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5579 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5581 umem_free(bin, len);
5582 umem_free(zdb, len);
5583 umem_free(zbuf, 1024);
5587 ztest_walk_pool_directory(char *header)
5591 if (ztest_opts.zo_verbose >= 6)
5592 (void) printf("%s\n", header);
5594 mutex_enter(&spa_namespace_lock);
5595 while ((spa = spa_next(spa)) != NULL)
5596 if (ztest_opts.zo_verbose >= 6)
5597 (void) printf("\t%s\n", spa_name(spa));
5598 mutex_exit(&spa_namespace_lock);
5602 ztest_spa_import_export(char *oldname, char *newname)
5604 nvlist_t *config, *newconfig;
5609 if (ztest_opts.zo_verbose >= 4) {
5610 (void) printf("import/export: old = %s, new = %s\n",
5615 * Clean up from previous runs.
5617 (void) spa_destroy(newname);
5620 * Get the pool's configuration and guid.
5622 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5625 * Kick off a scrub to tickle scrub/export races.
5627 if (ztest_random(2) == 0)
5628 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5630 pool_guid = spa_guid(spa);
5631 spa_close(spa, FTAG);
5633 ztest_walk_pool_directory("pools before export");
5638 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5640 ztest_walk_pool_directory("pools after export");
5645 newconfig = spa_tryimport(config);
5646 ASSERT(newconfig != NULL);
5647 nvlist_free(newconfig);
5650 * Import it under the new name.
5652 error = spa_import(newname, config, NULL, 0);
5654 dump_nvlist(config, 0);
5655 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5656 oldname, newname, error);
5659 ztest_walk_pool_directory("pools after import");
5662 * Try to import it again -- should fail with EEXIST.
5664 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5667 * Try to import it under a different name -- should fail with EEXIST.
5669 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5672 * Verify that the pool is no longer visible under the old name.
5674 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5677 * Verify that we can open and close the pool using the new name.
5679 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5680 ASSERT(pool_guid == spa_guid(spa));
5681 spa_close(spa, FTAG);
5683 nvlist_free(config);
5687 ztest_resume(spa_t *spa)
5689 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5690 (void) printf("resuming from suspended state\n");
5691 spa_vdev_state_enter(spa, SCL_NONE);
5692 vdev_clear(spa, NULL);
5693 (void) spa_vdev_state_exit(spa, NULL, 0);
5694 (void) zio_resume(spa);
5698 ztest_resume_thread(void *arg)
5702 while (!ztest_exiting) {
5703 if (spa_suspended(spa))
5705 (void) poll(NULL, 0, 100);
5717 ztest_deadman_alarm(int sig)
5719 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5724 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5726 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5727 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5728 hrtime_t functime = gethrtime();
5731 for (i = 0; i < zi->zi_iters; i++)
5732 zi->zi_func(zd, id);
5734 functime = gethrtime() - functime;
5736 atomic_add_64(&zc->zc_count, 1);
5737 atomic_add_64(&zc->zc_time, functime);
5739 if (ztest_opts.zo_verbose >= 4)
5740 (void) printf("%6.2f sec in %s\n",
5741 (double)functime / NANOSEC, zi->zi_funcname);
5745 ztest_thread(void *arg)
5748 uint64_t id = (uintptr_t)arg;
5749 ztest_shared_t *zs = ztest_shared;
5753 ztest_shared_callstate_t *zc;
5755 while ((now = gethrtime()) < zs->zs_thread_stop) {
5757 * See if it's time to force a crash.
5759 if (now > zs->zs_thread_kill)
5763 * If we're getting ENOSPC with some regularity, stop.
5765 if (zs->zs_enospc_count > 10)
5769 * Pick a random function to execute.
5771 rand = ztest_random(ZTEST_FUNCS);
5772 zi = &ztest_info[rand];
5773 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5774 call_next = zc->zc_next;
5776 if (now >= call_next &&
5777 atomic_cas_64(&zc->zc_next, call_next, call_next +
5778 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5779 ztest_execute(rand, zi, id);
5789 ztest_dataset_name(char *dsname, char *pool, int d)
5791 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5795 ztest_dataset_destroy(int d)
5797 char name[MAXNAMELEN];
5800 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5802 if (ztest_opts.zo_verbose >= 3)
5803 (void) printf("Destroying %s to free up space\n", name);
5806 * Cleanup any non-standard clones and snapshots. In general,
5807 * ztest thread t operates on dataset (t % zopt_datasets),
5808 * so there may be more than one thing to clean up.
5810 for (t = d; t < ztest_opts.zo_threads;
5811 t += ztest_opts.zo_datasets)
5812 ztest_dsl_dataset_cleanup(name, t);
5814 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5815 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5819 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5821 uint64_t usedobjs, dirobjs, scratch;
5824 * ZTEST_DIROBJ is the object directory for the entire dataset.
5825 * Therefore, the number of objects in use should equal the
5826 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5827 * If not, we have an object leak.
5829 * Note that we can only check this in ztest_dataset_open(),
5830 * when the open-context and syncing-context values agree.
5831 * That's because zap_count() returns the open-context value,
5832 * while dmu_objset_space() returns the rootbp fill count.
5834 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5835 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5836 ASSERT3U(dirobjs + 1, ==, usedobjs);
5840 ztest_dataset_open(int d)
5842 ztest_ds_t *zd = &ztest_ds[d];
5843 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5846 char name[MAXNAMELEN];
5849 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5851 (void) rw_rdlock(&ztest_name_lock);
5853 error = ztest_dataset_create(name);
5854 if (error == ENOSPC) {
5855 (void) rw_unlock(&ztest_name_lock);
5856 ztest_record_enospc(FTAG);
5859 ASSERT(error == 0 || error == EEXIST);
5861 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5862 (void) rw_unlock(&ztest_name_lock);
5864 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5866 zilog = zd->zd_zilog;
5868 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5869 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5870 fatal(0, "missing log records: claimed %llu < committed %llu",
5871 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5873 ztest_dataset_dirobj_verify(zd);
5875 zil_replay(os, zd, ztest_replay_vector);
5877 ztest_dataset_dirobj_verify(zd);
5879 if (ztest_opts.zo_verbose >= 6)
5880 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5882 (u_longlong_t)zilog->zl_parse_blk_count,
5883 (u_longlong_t)zilog->zl_parse_lr_count,
5884 (u_longlong_t)zilog->zl_replaying_seq);
5886 zilog = zil_open(os, ztest_get_data);
5888 if (zilog->zl_replaying_seq != 0 &&
5889 zilog->zl_replaying_seq < committed_seq)
5890 fatal(0, "missing log records: replayed %llu < committed %llu",
5891 zilog->zl_replaying_seq, committed_seq);
5897 ztest_dataset_close(int d)
5899 ztest_ds_t *zd = &ztest_ds[d];
5901 zil_close(zd->zd_zilog);
5902 dmu_objset_disown(zd->zd_os, zd);
5908 * Kick off threads to run tests on all datasets in parallel.
5911 ztest_run(ztest_shared_t *zs)
5916 kthread_t *resume_thread;
5921 ztest_exiting = B_FALSE;
5924 * Initialize parent/child shared state.
5926 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5927 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5929 zs->zs_thread_start = gethrtime();
5930 zs->zs_thread_stop =
5931 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5932 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5933 zs->zs_thread_kill = zs->zs_thread_stop;
5934 if (ztest_random(100) < ztest_opts.zo_killrate) {
5935 zs->zs_thread_kill -=
5936 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5939 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5941 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5942 offsetof(ztest_cb_data_t, zcd_node));
5947 kernel_init(FREAD | FWRITE);
5948 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5949 spa->spa_debug = B_TRUE;
5950 metaslab_preload_limit = ztest_random(20) + 1;
5953 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5954 DMU_OST_ANY, B_TRUE, FTAG, &os));
5955 zs->zs_guid = dmu_objset_fsid_guid(os);
5956 dmu_objset_disown(os, FTAG);
5958 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5961 * We don't expect the pool to suspend unless maxfaults == 0,
5962 * in which case ztest_fault_inject() temporarily takes away
5963 * the only valid replica.
5965 if (MAXFAULTS() == 0)
5966 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5968 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5971 * Create a thread to periodically resume suspended I/O.
5973 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5974 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5975 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5979 * Set a deadman alarm to abort() if we hang.
5981 signal(SIGALRM, ztest_deadman_alarm);
5982 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5986 * Verify that we can safely inquire about about any object,
5987 * whether it's allocated or not. To make it interesting,
5988 * we probe a 5-wide window around each power of two.
5989 * This hits all edge cases, including zero and the max.
5991 for (t = 0; t < 64; t++) {
5992 for (d = -5; d <= 5; d++) {
5993 error = dmu_object_info(spa->spa_meta_objset,
5994 (1ULL << t) + d, NULL);
5995 ASSERT(error == 0 || error == ENOENT ||
6001 * If we got any ENOSPC errors on the previous run, destroy something.
6003 if (zs->zs_enospc_count != 0) {
6004 int d = ztest_random(ztest_opts.zo_datasets);
6005 ztest_dataset_destroy(d);
6007 zs->zs_enospc_count = 0;
6009 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6012 if (ztest_opts.zo_verbose >= 4)
6013 (void) printf("starting main threads...\n");
6016 * Kick off all the tests that run in parallel.
6018 for (t = 0; t < ztest_opts.zo_threads; t++) {
6021 if (t < ztest_opts.zo_datasets &&
6022 ztest_dataset_open(t) != 0)
6025 VERIFY3P(thread = zk_thread_create(NULL, 0,
6026 (thread_func_t)ztest_thread,
6027 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6028 PTHREAD_CREATE_JOINABLE), !=, NULL);
6029 tid[t] = thread->t_tid;
6033 * Wait for all of the tests to complete. We go in reverse order
6034 * so we don't close datasets while threads are still using them.
6036 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6037 thread_join(tid[t]);
6038 if (t < ztest_opts.zo_datasets)
6039 ztest_dataset_close(t);
6042 txg_wait_synced(spa_get_dsl(spa), 0);
6044 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6045 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6047 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6049 /* Kill the resume thread */
6050 ztest_exiting = B_TRUE;
6051 thread_join(resume_thread->t_tid);
6055 * Right before closing the pool, kick off a bunch of async I/O;
6056 * spa_close() should wait for it to complete.
6058 for (object = 1; object < 50; object++) {
6059 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6060 ZIO_PRIORITY_SYNC_READ);
6063 /* Verify that at least one commit cb was called in a timely fashion */
6064 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6065 VERIFY0(zc_min_txg_delay);
6067 spa_close(spa, FTAG);
6070 * Verify that we can loop over all pools.
6072 mutex_enter(&spa_namespace_lock);
6073 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6074 if (ztest_opts.zo_verbose > 3)
6075 (void) printf("spa_next: found %s\n", spa_name(spa));
6076 mutex_exit(&spa_namespace_lock);
6079 * Verify that we can export the pool and reimport it under a
6082 if (ztest_random(2) == 0) {
6083 char name[MAXNAMELEN];
6084 (void) snprintf(name, MAXNAMELEN, "%s_import",
6085 ztest_opts.zo_pool);
6086 ztest_spa_import_export(ztest_opts.zo_pool, name);
6087 ztest_spa_import_export(name, ztest_opts.zo_pool);
6092 list_destroy(&zcl.zcl_callbacks);
6093 mutex_destroy(&zcl.zcl_callbacks_lock);
6094 (void) rwlock_destroy(&ztest_name_lock);
6095 mutex_destroy(&ztest_vdev_lock);
6101 ztest_ds_t *zd = &ztest_ds[0];
6105 if (ztest_opts.zo_verbose >= 3)
6106 (void) printf("testing spa_freeze()...\n");
6108 kernel_init(FREAD | FWRITE);
6109 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6110 VERIFY3U(0, ==, ztest_dataset_open(0));
6111 spa->spa_debug = B_TRUE;
6115 * Force the first log block to be transactionally allocated.
6116 * We have to do this before we freeze the pool -- otherwise
6117 * the log chain won't be anchored.
6119 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6120 ztest_dmu_object_alloc_free(zd, 0);
6121 zil_commit(zd->zd_zilog, 0);
6124 txg_wait_synced(spa_get_dsl(spa), 0);
6127 * Freeze the pool. This stops spa_sync() from doing anything,
6128 * so that the only way to record changes from now on is the ZIL.
6133 * Because it is hard to predict how much space a write will actually
6134 * require beforehand, we leave ourselves some fudge space to write over
6137 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6140 * Run tests that generate log records but don't alter the pool config
6141 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6142 * We do a txg_wait_synced() after each iteration to force the txg
6143 * to increase well beyond the last synced value in the uberblock.
6144 * The ZIL should be OK with that.
6146 * Run a random number of times less than zo_maxloops and ensure we do
6147 * not run out of space on the pool.
6149 while (ztest_random(10) != 0 &&
6150 numloops++ < ztest_opts.zo_maxloops &&
6151 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6153 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
6154 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6155 ztest_io(zd, od.od_object,
6156 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6157 txg_wait_synced(spa_get_dsl(spa), 0);
6161 * Commit all of the changes we just generated.
6163 zil_commit(zd->zd_zilog, 0);
6164 txg_wait_synced(spa_get_dsl(spa), 0);
6167 * Close our dataset and close the pool.
6169 ztest_dataset_close(0);
6170 spa_close(spa, FTAG);
6174 * Open and close the pool and dataset to induce log replay.
6176 kernel_init(FREAD | FWRITE);
6177 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6178 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6179 VERIFY3U(0, ==, ztest_dataset_open(0));
6180 ztest_dataset_close(0);
6182 spa->spa_debug = B_TRUE;
6184 txg_wait_synced(spa_get_dsl(spa), 0);
6185 ztest_reguid(NULL, 0);
6187 spa_close(spa, FTAG);
6192 print_time(hrtime_t t, char *timebuf)
6194 hrtime_t s = t / NANOSEC;
6195 hrtime_t m = s / 60;
6196 hrtime_t h = m / 60;
6197 hrtime_t d = h / 24;
6206 (void) sprintf(timebuf,
6207 "%llud%02lluh%02llum%02llus", d, h, m, s);
6209 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6211 (void) sprintf(timebuf, "%llum%02llus", m, s);
6213 (void) sprintf(timebuf, "%llus", s);
6217 make_random_props(void)
6221 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6222 if (ztest_random(2) == 0)
6224 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6230 * Create a storage pool with the given name and initial vdev size.
6231 * Then test spa_freeze() functionality.
6234 ztest_init(ztest_shared_t *zs)
6237 nvlist_t *nvroot, *props;
6240 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6241 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6243 kernel_init(FREAD | FWRITE);
6246 * Create the storage pool.
6248 (void) spa_destroy(ztest_opts.zo_pool);
6249 ztest_shared->zs_vdev_next_leaf = 0;
6251 zs->zs_mirrors = ztest_opts.zo_mirrors;
6252 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6253 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6254 props = make_random_props();
6255 for (i = 0; i < SPA_FEATURES; i++) {
6257 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6258 spa_feature_table[i].fi_uname));
6259 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6262 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6263 nvlist_free(nvroot);
6266 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6267 zs->zs_metaslab_sz =
6268 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6269 spa_close(spa, FTAG);
6273 ztest_run_zdb(ztest_opts.zo_pool);
6277 ztest_run_zdb(ztest_opts.zo_pool);
6279 (void) rwlock_destroy(&ztest_name_lock);
6280 mutex_destroy(&ztest_vdev_lock);
6286 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6288 ztest_fd_data = mkstemp(ztest_name_data);
6289 ASSERT3S(ztest_fd_data, >=, 0);
6290 (void) unlink(ztest_name_data);
6294 shared_data_size(ztest_shared_hdr_t *hdr)
6298 size = hdr->zh_hdr_size;
6299 size += hdr->zh_opts_size;
6300 size += hdr->zh_size;
6301 size += hdr->zh_stats_size * hdr->zh_stats_count;
6302 size += hdr->zh_ds_size * hdr->zh_ds_count;
6311 ztest_shared_hdr_t *hdr;
6313 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6314 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6315 ASSERT(hdr != MAP_FAILED);
6317 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6319 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6320 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6321 hdr->zh_size = sizeof (ztest_shared_t);
6322 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6323 hdr->zh_stats_count = ZTEST_FUNCS;
6324 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6325 hdr->zh_ds_count = ztest_opts.zo_datasets;
6327 size = shared_data_size(hdr);
6328 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6330 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6337 ztest_shared_hdr_t *hdr;
6340 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6341 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6342 ASSERT(hdr != MAP_FAILED);
6344 size = shared_data_size(hdr);
6346 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6347 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6348 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6349 ASSERT(hdr != MAP_FAILED);
6350 buf = (uint8_t *)hdr;
6352 offset = hdr->zh_hdr_size;
6353 ztest_shared_opts = (void *)&buf[offset];
6354 offset += hdr->zh_opts_size;
6355 ztest_shared = (void *)&buf[offset];
6356 offset += hdr->zh_size;
6357 ztest_shared_callstate = (void *)&buf[offset];
6358 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6359 ztest_shared_ds = (void *)&buf[offset];
6363 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6367 char *cmdbuf = NULL;
6372 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6373 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6378 fatal(1, "fork failed");
6380 if (pid == 0) { /* child */
6381 char *emptyargv[2] = { cmd, NULL };
6382 char fd_data_str[12];
6384 struct rlimit rl = { 1024, 1024 };
6385 (void) setrlimit(RLIMIT_NOFILE, &rl);
6387 (void) close(ztest_fd_rand);
6388 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6389 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6391 (void) enable_extended_FILE_stdio(-1, -1);
6392 if (libpath != NULL)
6393 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6394 (void) execv(cmd, emptyargv);
6395 ztest_dump_core = B_FALSE;
6396 fatal(B_TRUE, "exec failed: %s", cmd);
6399 if (cmdbuf != NULL) {
6400 umem_free(cmdbuf, MAXPATHLEN);
6404 while (waitpid(pid, &status, 0) != pid)
6406 if (statusp != NULL)
6409 if (WIFEXITED(status)) {
6410 if (WEXITSTATUS(status) != 0) {
6411 (void) fprintf(stderr, "child exited with code %d\n",
6412 WEXITSTATUS(status));
6416 } else if (WIFSIGNALED(status)) {
6417 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6418 (void) fprintf(stderr, "child died with signal %d\n",
6424 (void) fprintf(stderr, "something strange happened to child\n");
6431 ztest_run_init(void)
6435 ztest_shared_t *zs = ztest_shared;
6437 ASSERT(ztest_opts.zo_init != 0);
6440 * Blow away any existing copy of zpool.cache
6442 (void) remove(spa_config_path);
6445 * Create and initialize our storage pool.
6447 for (i = 1; i <= ztest_opts.zo_init; i++) {
6448 bzero(zs, sizeof (ztest_shared_t));
6449 if (ztest_opts.zo_verbose >= 3 &&
6450 ztest_opts.zo_init != 1) {
6451 (void) printf("ztest_init(), pass %d\n", i);
6458 main(int argc, char **argv)
6466 ztest_shared_callstate_t *zc;
6473 char *fd_data_str = getenv("ZTEST_FD_DATA");
6474 struct sigaction action;
6476 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6478 dprintf_setup(&argc, argv);
6480 action.sa_handler = sig_handler;
6481 sigemptyset(&action.sa_mask);
6482 action.sa_flags = 0;
6484 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6485 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6490 if (sigaction(SIGABRT, &action, NULL) < 0) {
6491 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6496 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6497 ASSERT3S(ztest_fd_rand, >=, 0);
6500 process_options(argc, argv);
6505 bcopy(&ztest_opts, ztest_shared_opts,
6506 sizeof (*ztest_shared_opts));
6508 ztest_fd_data = atoi(fd_data_str);
6510 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6512 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6514 /* Override location of zpool.cache */
6515 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6516 ztest_opts.zo_dir) != -1);
6518 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6523 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6524 metaslab_df_alloc_threshold =
6525 zs->zs_metaslab_df_alloc_threshold;
6534 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6536 if (ztest_opts.zo_verbose >= 1) {
6537 (void) printf("%llu vdevs, %d datasets, %d threads,"
6538 " %llu seconds...\n",
6539 (u_longlong_t)ztest_opts.zo_vdevs,
6540 ztest_opts.zo_datasets,
6541 ztest_opts.zo_threads,
6542 (u_longlong_t)ztest_opts.zo_time);
6545 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6546 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6548 zs->zs_do_init = B_TRUE;
6549 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6550 if (ztest_opts.zo_verbose >= 1) {
6551 (void) printf("Executing older ztest for "
6552 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6554 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6555 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6557 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6559 zs->zs_do_init = B_FALSE;
6561 zs->zs_proc_start = gethrtime();
6562 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6564 for (f = 0; f < ZTEST_FUNCS; f++) {
6565 zi = &ztest_info[f];
6566 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6567 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6568 zc->zc_next = UINT64_MAX;
6570 zc->zc_next = zs->zs_proc_start +
6571 ztest_random(2 * zi->zi_interval[0] + 1);
6575 * Run the tests in a loop. These tests include fault injection
6576 * to verify that self-healing data works, and forced crashes
6577 * to verify that we never lose on-disk consistency.
6579 while (gethrtime() < zs->zs_proc_stop) {
6584 * Initialize the workload counters for each function.
6586 for (f = 0; f < ZTEST_FUNCS; f++) {
6587 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6592 /* Set the allocation switch size */
6593 zs->zs_metaslab_df_alloc_threshold =
6594 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6596 if (!hasalt || ztest_random(2) == 0) {
6597 if (hasalt && ztest_opts.zo_verbose >= 1) {
6598 (void) printf("Executing newer ztest: %s\n",
6602 killed = exec_child(cmd, NULL, B_TRUE, &status);
6604 if (hasalt && ztest_opts.zo_verbose >= 1) {
6605 (void) printf("Executing older ztest: %s\n",
6606 ztest_opts.zo_alt_ztest);
6609 killed = exec_child(ztest_opts.zo_alt_ztest,
6610 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6617 if (ztest_opts.zo_verbose >= 1) {
6618 hrtime_t now = gethrtime();
6620 now = MIN(now, zs->zs_proc_stop);
6621 print_time(zs->zs_proc_stop - now, timebuf);
6622 nicenum(zs->zs_space, numbuf);
6624 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6625 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6627 WIFEXITED(status) ? "Complete" : "SIGKILL",
6628 (u_longlong_t)zs->zs_enospc_count,
6629 100.0 * zs->zs_alloc / zs->zs_space,
6631 100.0 * (now - zs->zs_proc_start) /
6632 (ztest_opts.zo_time * NANOSEC), timebuf);
6635 if (ztest_opts.zo_verbose >= 2) {
6636 (void) printf("\nWorkload summary:\n\n");
6637 (void) printf("%7s %9s %s\n",
6638 "Calls", "Time", "Function");
6639 (void) printf("%7s %9s %s\n",
6640 "-----", "----", "--------");
6641 for (f = 0; f < ZTEST_FUNCS; f++) {
6642 zi = &ztest_info[f];
6643 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6644 print_time(zc->zc_time, timebuf);
6645 (void) printf("%7llu %9s %s\n",
6646 (u_longlong_t)zc->zc_count, timebuf,
6649 (void) printf("\n");
6653 * It's possible that we killed a child during a rename test,
6654 * in which case we'll have a 'ztest_tmp' pool lying around
6655 * instead of 'ztest'. Do a blind rename in case this happened.
6658 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6659 spa_close(spa, FTAG);
6661 char tmpname[MAXNAMELEN];
6663 kernel_init(FREAD | FWRITE);
6664 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6665 ztest_opts.zo_pool);
6666 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6670 ztest_run_zdb(ztest_opts.zo_pool);
6673 if (ztest_opts.zo_verbose >= 1) {
6675 (void) printf("%d runs of older ztest: %s\n", older,
6676 ztest_opts.zo_alt_ztest);
6677 (void) printf("%d runs of newer ztest: %s\n", newer,
6680 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6681 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6684 umem_free(cmd, MAXNAMELEN);