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 <zfs_fletcher.h>
127 #include <libnvpair.h>
129 #include <execinfo.h> /* for backtrace() */
132 static int ztest_fd_data = -1;
133 static int ztest_fd_rand = -1;
135 typedef struct ztest_shared_hdr {
136 uint64_t zh_hdr_size;
137 uint64_t zh_opts_size;
139 uint64_t zh_stats_size;
140 uint64_t zh_stats_count;
142 uint64_t zh_ds_count;
143 } ztest_shared_hdr_t;
145 static ztest_shared_hdr_t *ztest_shared_hdr;
147 typedef struct ztest_shared_opts {
148 char zo_pool[MAXNAMELEN];
149 char zo_dir[MAXNAMELEN];
150 char zo_alt_ztest[MAXNAMELEN];
151 char zo_alt_libpath[MAXNAMELEN];
153 uint64_t zo_vdevtime;
161 uint64_t zo_passtime;
162 uint64_t zo_killrate;
166 uint64_t zo_maxloops;
167 uint64_t zo_metaslab_gang_bang;
168 } ztest_shared_opts_t;
170 static const ztest_shared_opts_t ztest_opts_defaults = {
171 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
172 .zo_dir = { '/', 't', 'm', 'p', '\0' },
173 .zo_alt_ztest = { '\0' },
174 .zo_alt_libpath = { '\0' },
176 .zo_ashift = SPA_MINBLOCKSHIFT,
179 .zo_raidz_parity = 1,
180 .zo_vdev_size = SPA_MINDEVSIZE * 2,
183 .zo_passtime = 60, /* 60 seconds */
184 .zo_killrate = 70, /* 70% kill rate */
187 .zo_time = 300, /* 5 minutes */
188 .zo_maxloops = 50, /* max loops during spa_freeze() */
189 .zo_metaslab_gang_bang = 32 << 10
192 extern uint64_t metaslab_gang_bang;
193 extern uint64_t metaslab_df_alloc_threshold;
194 extern int metaslab_preload_limit;
196 static ztest_shared_opts_t *ztest_shared_opts;
197 static ztest_shared_opts_t ztest_opts;
199 typedef struct ztest_shared_ds {
203 static ztest_shared_ds_t *ztest_shared_ds;
204 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
206 #define BT_MAGIC 0x123456789abcdefULL
207 #define MAXFAULTS() \
208 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
212 ZTEST_IO_WRITE_PATTERN,
213 ZTEST_IO_WRITE_ZEROES,
220 typedef struct ztest_block_tag {
230 typedef struct bufwad {
248 #define ZTEST_RANGE_LOCKS 64
249 #define ZTEST_OBJECT_LOCKS 64
252 * Object descriptor. Used as a template for object lookup/create/remove.
254 typedef struct ztest_od {
257 dmu_object_type_t od_type;
258 dmu_object_type_t od_crtype;
259 uint64_t od_blocksize;
260 uint64_t od_crblocksize;
263 char od_name[MAXNAMELEN];
269 typedef struct ztest_ds {
270 ztest_shared_ds_t *zd_shared;
272 rwlock_t zd_zilog_lock;
274 ztest_od_t *zd_od; /* debugging aid */
275 char zd_name[MAXNAMELEN];
276 kmutex_t zd_dirobj_lock;
277 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
278 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
282 * Per-iteration state.
284 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
286 typedef struct ztest_info {
287 ztest_func_t *zi_func; /* test function */
288 uint64_t zi_iters; /* iterations per execution */
289 uint64_t *zi_interval; /* execute every <interval> seconds */
290 const char *zi_funcname; /* name of test function */
293 typedef struct ztest_shared_callstate {
294 uint64_t zc_count; /* per-pass count */
295 uint64_t zc_time; /* per-pass time */
296 uint64_t zc_next; /* next time to call this function */
297 } ztest_shared_callstate_t;
299 static ztest_shared_callstate_t *ztest_shared_callstate;
300 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
302 ztest_func_t ztest_dmu_read_write;
303 ztest_func_t ztest_dmu_write_parallel;
304 ztest_func_t ztest_dmu_object_alloc_free;
305 ztest_func_t ztest_dmu_commit_callbacks;
306 ztest_func_t ztest_zap;
307 ztest_func_t ztest_zap_parallel;
308 ztest_func_t ztest_zil_commit;
309 ztest_func_t ztest_zil_remount;
310 ztest_func_t ztest_dmu_read_write_zcopy;
311 ztest_func_t ztest_dmu_objset_create_destroy;
312 ztest_func_t ztest_dmu_prealloc;
313 ztest_func_t ztest_fzap;
314 ztest_func_t ztest_dmu_snapshot_create_destroy;
315 ztest_func_t ztest_dsl_prop_get_set;
316 ztest_func_t ztest_spa_prop_get_set;
317 ztest_func_t ztest_spa_create_destroy;
318 ztest_func_t ztest_fault_inject;
319 ztest_func_t ztest_ddt_repair;
320 ztest_func_t ztest_dmu_snapshot_hold;
321 ztest_func_t ztest_spa_rename;
322 ztest_func_t ztest_scrub;
323 ztest_func_t ztest_dsl_dataset_promote_busy;
324 ztest_func_t ztest_vdev_attach_detach;
325 ztest_func_t ztest_vdev_LUN_growth;
326 ztest_func_t ztest_vdev_add_remove;
327 ztest_func_t ztest_vdev_aux_add_remove;
328 ztest_func_t ztest_split_pool;
329 ztest_func_t ztest_reguid;
330 ztest_func_t ztest_spa_upgrade;
331 ztest_func_t ztest_fletcher;
333 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
334 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
335 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
336 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
337 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
339 #define ZTI_INIT(func, iters, interval) \
340 { .zi_func = (func), \
341 .zi_iters = (iters), \
342 .zi_interval = (interval), \
343 .zi_funcname = # func }
345 ztest_info_t ztest_info[] = {
346 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
347 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
348 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
349 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
350 ZTI_INIT(ztest_zap, 30, &zopt_always),
351 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
352 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
353 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
354 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
355 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
356 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
357 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
358 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
360 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
362 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
363 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
364 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
367 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
368 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
369 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
370 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
371 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
372 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
373 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
374 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
375 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
376 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
377 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
380 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
383 * The following struct is used to hold a list of uncalled commit callbacks.
384 * The callbacks are ordered by txg number.
386 typedef struct ztest_cb_list {
387 kmutex_t zcl_callbacks_lock;
388 list_t zcl_callbacks;
392 * Stuff we need to share writably between parent and child.
394 typedef struct ztest_shared {
395 boolean_t zs_do_init;
396 hrtime_t zs_proc_start;
397 hrtime_t zs_proc_stop;
398 hrtime_t zs_thread_start;
399 hrtime_t zs_thread_stop;
400 hrtime_t zs_thread_kill;
401 uint64_t zs_enospc_count;
402 uint64_t zs_vdev_next_leaf;
403 uint64_t zs_vdev_aux;
408 uint64_t zs_metaslab_sz;
409 uint64_t zs_metaslab_df_alloc_threshold;
413 #define ID_PARALLEL -1ULL
415 static char ztest_dev_template[] = "%s/%s.%llua";
416 static char ztest_aux_template[] = "%s/%s.%s.%llu";
417 ztest_shared_t *ztest_shared;
419 static spa_t *ztest_spa = NULL;
420 static ztest_ds_t *ztest_ds;
422 static kmutex_t ztest_vdev_lock;
425 * The ztest_name_lock protects the pool and dataset namespace used by
426 * the individual tests. To modify the namespace, consumers must grab
427 * this lock as writer. Grabbing the lock as reader will ensure that the
428 * namespace does not change while the lock is held.
430 static rwlock_t ztest_name_lock;
432 static boolean_t ztest_dump_core = B_TRUE;
433 static boolean_t ztest_exiting;
435 /* Global commit callback list */
436 static ztest_cb_list_t zcl;
437 /* Commit cb delay */
438 static uint64_t zc_min_txg_delay = UINT64_MAX;
439 static int zc_cb_counter = 0;
442 * Minimum number of commit callbacks that need to be registered for us to check
443 * whether the minimum txg delay is acceptable.
445 #define ZTEST_COMMIT_CB_MIN_REG 100
448 * If a number of txgs equal to this threshold have been created after a commit
449 * callback has been registered but not called, then we assume there is an
450 * implementation bug.
452 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
454 extern uint64_t metaslab_gang_bang;
455 extern uint64_t metaslab_df_alloc_threshold;
458 ZTEST_META_DNODE = 0,
463 static void usage(boolean_t) __NORETURN;
466 * These libumem hooks provide a reasonable set of defaults for the allocator's
467 * debugging facilities.
470 _umem_debug_init(void)
472 return ("default,verbose"); /* $UMEM_DEBUG setting */
476 _umem_logging_init(void)
478 return ("fail,contents"); /* $UMEM_LOGGING setting */
481 #define BACKTRACE_SZ 100
483 static void sig_handler(int signo)
485 struct sigaction action;
486 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
488 void *buffer[BACKTRACE_SZ];
490 nptrs = backtrace(buffer, BACKTRACE_SZ);
491 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
495 * Restore default action and re-raise signal so SIGSEGV and
496 * SIGABRT can trigger a core dump.
498 action.sa_handler = SIG_DFL;
499 sigemptyset(&action.sa_mask);
501 (void) sigaction(signo, &action, NULL);
505 #define FATAL_MSG_SZ 1024
510 fatal(int do_perror, char *message, ...)
513 int save_errno = errno;
516 (void) fflush(stdout);
517 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
519 va_start(args, message);
520 (void) sprintf(buf, "ztest: ");
522 (void) vsprintf(buf + strlen(buf), message, args);
525 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
526 ": %s", strerror(save_errno));
528 (void) fprintf(stderr, "%s\n", buf);
529 fatal_msg = buf; /* to ease debugging */
536 str2shift(const char *buf)
538 const char *ends = "BKMGTPEZ";
543 for (i = 0; i < strlen(ends); i++) {
544 if (toupper(buf[0]) == ends[i])
547 if (i == strlen(ends)) {
548 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
552 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
555 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
561 nicenumtoull(const char *buf)
566 val = strtoull(buf, &end, 0);
568 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
570 } else if (end[0] == '.') {
571 double fval = strtod(buf, &end);
572 fval *= pow(2, str2shift(end));
573 if (fval > UINT64_MAX) {
574 (void) fprintf(stderr, "ztest: value too large: %s\n",
578 val = (uint64_t)fval;
580 int shift = str2shift(end);
581 if (shift >= 64 || (val << shift) >> shift != val) {
582 (void) fprintf(stderr, "ztest: value too large: %s\n",
592 usage(boolean_t requested)
594 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
596 char nice_vdev_size[10];
597 char nice_gang_bang[10];
598 FILE *fp = requested ? stdout : stderr;
600 nicenum(zo->zo_vdev_size, nice_vdev_size);
601 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
603 (void) fprintf(fp, "Usage: %s\n"
604 "\t[-v vdevs (default: %llu)]\n"
605 "\t[-s size_of_each_vdev (default: %s)]\n"
606 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
607 "\t[-m mirror_copies (default: %d)]\n"
608 "\t[-r raidz_disks (default: %d)]\n"
609 "\t[-R raidz_parity (default: %d)]\n"
610 "\t[-d datasets (default: %d)]\n"
611 "\t[-t threads (default: %d)]\n"
612 "\t[-g gang_block_threshold (default: %s)]\n"
613 "\t[-i init_count (default: %d)] initialize pool i times\n"
614 "\t[-k kill_percentage (default: %llu%%)]\n"
615 "\t[-p pool_name (default: %s)]\n"
616 "\t[-f dir (default: %s)] file directory for vdev files\n"
617 "\t[-V] verbose (use multiple times for ever more blather)\n"
618 "\t[-E] use existing pool instead of creating new one\n"
619 "\t[-T time (default: %llu sec)] total run time\n"
620 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
621 "\t[-P passtime (default: %llu sec)] time per pass\n"
622 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
623 "\t[-h] (print help)\n"
626 (u_longlong_t)zo->zo_vdevs, /* -v */
627 nice_vdev_size, /* -s */
628 zo->zo_ashift, /* -a */
629 zo->zo_mirrors, /* -m */
630 zo->zo_raidz, /* -r */
631 zo->zo_raidz_parity, /* -R */
632 zo->zo_datasets, /* -d */
633 zo->zo_threads, /* -t */
634 nice_gang_bang, /* -g */
635 zo->zo_init, /* -i */
636 (u_longlong_t)zo->zo_killrate, /* -k */
637 zo->zo_pool, /* -p */
639 (u_longlong_t)zo->zo_time, /* -T */
640 (u_longlong_t)zo->zo_maxloops, /* -F */
641 (u_longlong_t)zo->zo_passtime);
642 exit(requested ? 0 : 1);
646 process_options(int argc, char **argv)
649 ztest_shared_opts_t *zo = &ztest_opts;
653 char altdir[MAXNAMELEN] = { 0 };
655 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
657 while ((opt = getopt(argc, argv,
658 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
675 value = nicenumtoull(optarg);
679 zo->zo_vdevs = value;
682 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
685 zo->zo_ashift = value;
688 zo->zo_mirrors = value;
691 zo->zo_raidz = MAX(1, value);
694 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
697 zo->zo_datasets = MAX(1, value);
700 zo->zo_threads = MAX(1, value);
703 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
710 zo->zo_killrate = value;
713 (void) strlcpy(zo->zo_pool, optarg,
714 sizeof (zo->zo_pool));
717 path = realpath(optarg, NULL);
719 (void) fprintf(stderr, "error: %s: %s\n",
720 optarg, strerror(errno));
723 (void) strlcpy(zo->zo_dir, path,
724 sizeof (zo->zo_dir));
737 zo->zo_passtime = MAX(1, value);
740 zo->zo_maxloops = MAX(1, value);
743 (void) strlcpy(altdir, optarg, sizeof (altdir));
755 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
758 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
761 if (strlen(altdir) > 0) {
769 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
770 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
772 VERIFY(NULL != realpath(getexecname(), cmd));
773 if (0 != access(altdir, F_OK)) {
774 ztest_dump_core = B_FALSE;
775 fatal(B_TRUE, "invalid alternate ztest path: %s",
778 VERIFY(NULL != realpath(altdir, realaltdir));
781 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
782 * We want to extract <isa> to determine if we should use
783 * 32 or 64 bit binaries.
785 bin = strstr(cmd, "/usr/bin/");
786 ztest = strstr(bin, "/ztest");
788 isalen = ztest - isa;
789 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
790 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
791 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
792 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
794 if (0 != access(zo->zo_alt_ztest, X_OK)) {
795 ztest_dump_core = B_FALSE;
796 fatal(B_TRUE, "invalid alternate ztest: %s",
798 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
799 ztest_dump_core = B_FALSE;
800 fatal(B_TRUE, "invalid alternate lib directory %s",
804 umem_free(cmd, MAXPATHLEN);
805 umem_free(realaltdir, MAXPATHLEN);
810 ztest_kill(ztest_shared_t *zs)
812 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
813 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
816 * Before we kill off ztest, make sure that the config is updated.
817 * See comment above spa_config_sync().
819 mutex_enter(&spa_namespace_lock);
820 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
821 mutex_exit(&spa_namespace_lock);
823 (void) kill(getpid(), SIGKILL);
827 ztest_random(uint64_t range)
831 ASSERT3S(ztest_fd_rand, >=, 0);
836 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
837 fatal(1, "short read from /dev/urandom");
844 ztest_record_enospc(const char *s)
846 ztest_shared->zs_enospc_count++;
850 ztest_get_ashift(void)
852 if (ztest_opts.zo_ashift == 0)
853 return (SPA_MINBLOCKSHIFT + ztest_random(5));
854 return (ztest_opts.zo_ashift);
858 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
864 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
867 ashift = ztest_get_ashift();
873 vdev = ztest_shared->zs_vdev_aux;
874 (void) snprintf(path, MAXPATHLEN,
875 ztest_aux_template, ztest_opts.zo_dir,
876 pool == NULL ? ztest_opts.zo_pool : pool,
879 vdev = ztest_shared->zs_vdev_next_leaf++;
880 (void) snprintf(path, MAXPATHLEN,
881 ztest_dev_template, ztest_opts.zo_dir,
882 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
887 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
889 fatal(1, "can't open %s", path);
890 if (ftruncate(fd, size) != 0)
891 fatal(1, "can't ftruncate %s", path);
895 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
896 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
897 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
898 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
899 umem_free(pathbuf, MAXPATHLEN);
905 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
906 uint64_t ashift, int r)
908 nvlist_t *raidz, **child;
912 return (make_vdev_file(path, aux, pool, size, ashift));
913 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
915 for (c = 0; c < r; c++)
916 child[c] = make_vdev_file(path, aux, pool, size, ashift);
918 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
919 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
920 VDEV_TYPE_RAIDZ) == 0);
921 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
922 ztest_opts.zo_raidz_parity) == 0);
923 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
926 for (c = 0; c < r; c++)
927 nvlist_free(child[c]);
929 umem_free(child, r * sizeof (nvlist_t *));
935 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
936 uint64_t ashift, int r, int m)
938 nvlist_t *mirror, **child;
942 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
944 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
946 for (c = 0; c < m; c++)
947 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
949 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
950 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
951 VDEV_TYPE_MIRROR) == 0);
952 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
955 for (c = 0; c < m; c++)
956 nvlist_free(child[c]);
958 umem_free(child, m * sizeof (nvlist_t *));
964 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
965 int log, int r, int m, int t)
967 nvlist_t *root, **child;
972 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
974 for (c = 0; c < t; c++) {
975 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
977 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
981 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
982 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
983 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
986 for (c = 0; c < t; c++)
987 nvlist_free(child[c]);
989 umem_free(child, t * sizeof (nvlist_t *));
995 * Find a random spa version. Returns back a random spa version in the
996 * range [initial_version, SPA_VERSION_FEATURES].
999 ztest_random_spa_version(uint64_t initial_version)
1001 uint64_t version = initial_version;
1003 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1005 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1008 if (version > SPA_VERSION_BEFORE_FEATURES)
1009 version = SPA_VERSION_FEATURES;
1011 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1016 ztest_random_blocksize(void)
1019 * Choose a block size >= the ashift.
1020 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1022 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1023 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1025 uint64_t block_shift =
1026 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1027 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1031 ztest_random_ibshift(void)
1033 return (DN_MIN_INDBLKSHIFT +
1034 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1038 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1041 vdev_t *rvd = spa->spa_root_vdev;
1044 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1047 top = ztest_random(rvd->vdev_children);
1048 tvd = rvd->vdev_child[top];
1049 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1050 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1056 ztest_random_dsl_prop(zfs_prop_t prop)
1061 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1062 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1068 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1071 const char *propname = zfs_prop_to_name(prop);
1072 const char *valname;
1077 error = dsl_prop_set_int(osname, propname,
1078 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1080 if (error == ENOSPC) {
1081 ztest_record_enospc(FTAG);
1086 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1087 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1089 if (ztest_opts.zo_verbose >= 6) {
1092 err = zfs_prop_index_to_string(prop, curval, &valname);
1094 (void) printf("%s %s = %llu at '%s'\n",
1095 osname, propname, (unsigned long long)curval,
1098 (void) printf("%s %s = %s at '%s'\n",
1099 osname, propname, valname, setpoint);
1101 umem_free(setpoint, MAXPATHLEN);
1107 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1109 spa_t *spa = ztest_spa;
1110 nvlist_t *props = NULL;
1113 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1114 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1116 error = spa_prop_set(spa, props);
1120 if (error == ENOSPC) {
1121 ztest_record_enospc(FTAG);
1131 * Object and range lock mechanics
1134 list_node_t z_lnode;
1135 refcount_t z_refcnt;
1137 zfs_rlock_t z_range_lock;
1142 ztest_znode_t *z_ztznode;
1145 static ztest_znode_t *
1146 ztest_znode_init(uint64_t object)
1148 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1150 list_link_init(&zp->z_lnode);
1151 refcount_create(&zp->z_refcnt);
1152 zp->z_object = object;
1153 zfs_rlock_init(&zp->z_range_lock);
1159 ztest_znode_fini(ztest_znode_t *zp)
1161 ASSERT(refcount_is_zero(&zp->z_refcnt));
1162 zfs_rlock_destroy(&zp->z_range_lock);
1164 refcount_destroy(&zp->z_refcnt);
1165 list_link_init(&zp->z_lnode);
1166 umem_free(zp, sizeof (*zp));
1170 ztest_zll_init(zll_t *zll)
1172 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1173 list_create(&zll->z_list, sizeof (ztest_znode_t),
1174 offsetof(ztest_znode_t, z_lnode));
1178 ztest_zll_destroy(zll_t *zll)
1180 list_destroy(&zll->z_list);
1181 mutex_destroy(&zll->z_lock);
1184 #define RL_TAG "range_lock"
1185 static ztest_znode_t *
1186 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1188 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1189 ztest_znode_t *zp = NULL;
1190 mutex_enter(&zll->z_lock);
1191 for (zp = list_head(&zll->z_list); (zp);
1192 zp = list_next(&zll->z_list, zp)) {
1193 if (zp->z_object == object) {
1194 refcount_add(&zp->z_refcnt, RL_TAG);
1199 zp = ztest_znode_init(object);
1200 refcount_add(&zp->z_refcnt, RL_TAG);
1201 list_insert_head(&zll->z_list, zp);
1203 mutex_exit(&zll->z_lock);
1208 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1211 ASSERT3U(zp->z_object, !=, 0);
1212 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1213 mutex_enter(&zll->z_lock);
1214 refcount_remove(&zp->z_refcnt, RL_TAG);
1215 if (refcount_is_zero(&zp->z_refcnt)) {
1216 list_remove(&zll->z_list, zp);
1217 ztest_znode_fini(zp);
1219 mutex_exit(&zll->z_lock);
1224 ztest_rll_init(rll_t *rll)
1226 rll->rll_writer = NULL;
1227 rll->rll_readers = 0;
1228 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1229 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1233 ztest_rll_destroy(rll_t *rll)
1235 ASSERT(rll->rll_writer == NULL);
1236 ASSERT(rll->rll_readers == 0);
1237 mutex_destroy(&rll->rll_lock);
1238 cv_destroy(&rll->rll_cv);
1242 ztest_rll_lock(rll_t *rll, rl_type_t type)
1244 mutex_enter(&rll->rll_lock);
1246 if (type == RL_READER) {
1247 while (rll->rll_writer != NULL)
1248 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1251 while (rll->rll_writer != NULL || rll->rll_readers)
1252 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1253 rll->rll_writer = curthread;
1256 mutex_exit(&rll->rll_lock);
1260 ztest_rll_unlock(rll_t *rll)
1262 mutex_enter(&rll->rll_lock);
1264 if (rll->rll_writer) {
1265 ASSERT(rll->rll_readers == 0);
1266 rll->rll_writer = NULL;
1268 ASSERT(rll->rll_readers != 0);
1269 ASSERT(rll->rll_writer == NULL);
1273 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1274 cv_broadcast(&rll->rll_cv);
1276 mutex_exit(&rll->rll_lock);
1280 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1282 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1284 ztest_rll_lock(rll, type);
1288 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1290 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1292 ztest_rll_unlock(rll);
1295 static ztest_zrl_t *
1296 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1298 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1300 zrl->z_ztznode = zp;
1305 ztest_zrl_fini(ztest_zrl_t *zrl)
1307 umem_free(zrl, sizeof (*zrl));
1310 static ztest_zrl_t *
1311 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1312 uint64_t size, rl_type_t type)
1314 ztest_znode_t *zp = ztest_znode_get(zd, object);
1315 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1317 return (ztest_zrl_init(rl, zp));
1321 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1323 zfs_range_unlock(zrl->z_rl);
1324 ztest_znode_put(zd, zrl->z_ztznode);
1325 ztest_zrl_fini(zrl);
1329 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1332 zd->zd_zilog = dmu_objset_zil(os);
1333 zd->zd_shared = szd;
1334 dmu_objset_name(os, zd->zd_name);
1337 if (zd->zd_shared != NULL)
1338 zd->zd_shared->zd_seq = 0;
1340 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1341 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1343 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1344 ztest_rll_init(&zd->zd_object_lock[l]);
1346 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1347 ztest_zll_init(&zd->zd_range_lock[l]);
1351 ztest_zd_fini(ztest_ds_t *zd)
1355 mutex_destroy(&zd->zd_dirobj_lock);
1356 (void) rwlock_destroy(&zd->zd_zilog_lock);
1358 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1359 ztest_rll_destroy(&zd->zd_object_lock[l]);
1361 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1362 ztest_zll_destroy(&zd->zd_range_lock[l]);
1365 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1368 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1374 * Attempt to assign tx to some transaction group.
1376 error = dmu_tx_assign(tx, txg_how);
1378 if (error == ERESTART) {
1379 ASSERT(txg_how == TXG_NOWAIT);
1382 ASSERT3U(error, ==, ENOSPC);
1383 ztest_record_enospc(tag);
1388 txg = dmu_tx_get_txg(tx);
1394 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1397 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1405 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1408 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1412 diff |= (value - *ip++);
1419 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1420 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1422 bt->bt_magic = BT_MAGIC;
1423 bt->bt_objset = dmu_objset_id(os);
1424 bt->bt_object = object;
1425 bt->bt_offset = offset;
1428 bt->bt_crtxg = crtxg;
1432 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1433 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1435 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1436 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1437 ASSERT3U(bt->bt_object, ==, object);
1438 ASSERT3U(bt->bt_offset, ==, offset);
1439 ASSERT3U(bt->bt_gen, <=, gen);
1440 ASSERT3U(bt->bt_txg, <=, txg);
1441 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1444 static ztest_block_tag_t *
1445 ztest_bt_bonus(dmu_buf_t *db)
1447 dmu_object_info_t doi;
1448 ztest_block_tag_t *bt;
1450 dmu_object_info_from_db(db, &doi);
1451 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1452 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1453 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1462 #define lrz_type lr_mode
1463 #define lrz_blocksize lr_uid
1464 #define lrz_ibshift lr_gid
1465 #define lrz_bonustype lr_rdev
1466 #define lrz_bonuslen lr_crtime[1]
1469 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1471 char *name = (void *)(lr + 1); /* name follows lr */
1472 size_t namesize = strlen(name) + 1;
1475 if (zil_replaying(zd->zd_zilog, tx))
1478 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1479 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1480 sizeof (*lr) + namesize - sizeof (lr_t));
1482 zil_itx_assign(zd->zd_zilog, itx, tx);
1486 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1488 char *name = (void *)(lr + 1); /* name follows lr */
1489 size_t namesize = strlen(name) + 1;
1492 if (zil_replaying(zd->zd_zilog, tx))
1495 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1496 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1497 sizeof (*lr) + namesize - sizeof (lr_t));
1499 itx->itx_oid = object;
1500 zil_itx_assign(zd->zd_zilog, itx, tx);
1504 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1507 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1509 if (zil_replaying(zd->zd_zilog, tx))
1512 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1513 write_state = WR_INDIRECT;
1515 itx = zil_itx_create(TX_WRITE,
1516 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1518 if (write_state == WR_COPIED &&
1519 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1520 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1521 zil_itx_destroy(itx);
1522 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1523 write_state = WR_NEED_COPY;
1525 itx->itx_private = zd;
1526 itx->itx_wr_state = write_state;
1527 itx->itx_sync = (ztest_random(8) == 0);
1528 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1530 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1531 sizeof (*lr) - sizeof (lr_t));
1533 zil_itx_assign(zd->zd_zilog, itx, tx);
1537 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1541 if (zil_replaying(zd->zd_zilog, tx))
1544 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1545 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1546 sizeof (*lr) - sizeof (lr_t));
1548 itx->itx_sync = B_FALSE;
1549 zil_itx_assign(zd->zd_zilog, itx, tx);
1553 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1557 if (zil_replaying(zd->zd_zilog, tx))
1560 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1561 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1562 sizeof (*lr) - sizeof (lr_t));
1564 itx->itx_sync = B_FALSE;
1565 zil_itx_assign(zd->zd_zilog, itx, tx);
1572 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1574 char *name = (void *)(lr + 1); /* name follows lr */
1575 objset_t *os = zd->zd_os;
1576 ztest_block_tag_t *bbt;
1583 byteswap_uint64_array(lr, sizeof (*lr));
1585 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1586 ASSERT(name[0] != '\0');
1588 tx = dmu_tx_create(os);
1590 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1592 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1593 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1595 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1598 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1602 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1604 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1605 if (lr->lr_foid == 0) {
1606 lr->lr_foid = zap_create(os,
1607 lr->lrz_type, lr->lrz_bonustype,
1608 lr->lrz_bonuslen, tx);
1610 error = zap_create_claim(os, lr->lr_foid,
1611 lr->lrz_type, lr->lrz_bonustype,
1612 lr->lrz_bonuslen, tx);
1615 if (lr->lr_foid == 0) {
1616 lr->lr_foid = dmu_object_alloc(os,
1617 lr->lrz_type, 0, lr->lrz_bonustype,
1618 lr->lrz_bonuslen, tx);
1620 error = dmu_object_claim(os, lr->lr_foid,
1621 lr->lrz_type, 0, lr->lrz_bonustype,
1622 lr->lrz_bonuslen, tx);
1627 ASSERT3U(error, ==, EEXIST);
1628 ASSERT(zd->zd_zilog->zl_replay);
1633 ASSERT(lr->lr_foid != 0);
1635 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1636 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1637 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1639 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1640 bbt = ztest_bt_bonus(db);
1641 dmu_buf_will_dirty(db, tx);
1642 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1643 dmu_buf_rele(db, FTAG);
1645 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1648 (void) ztest_log_create(zd, tx, lr);
1656 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1658 char *name = (void *)(lr + 1); /* name follows lr */
1659 objset_t *os = zd->zd_os;
1660 dmu_object_info_t doi;
1662 uint64_t object, txg;
1665 byteswap_uint64_array(lr, sizeof (*lr));
1667 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1668 ASSERT(name[0] != '\0');
1671 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1672 ASSERT(object != 0);
1674 ztest_object_lock(zd, object, RL_WRITER);
1676 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1678 tx = dmu_tx_create(os);
1680 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1681 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1683 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1685 ztest_object_unlock(zd, object);
1689 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1690 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1692 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1695 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1697 (void) ztest_log_remove(zd, tx, lr, object);
1701 ztest_object_unlock(zd, object);
1707 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1709 objset_t *os = zd->zd_os;
1710 void *data = lr + 1; /* data follows lr */
1711 uint64_t offset, length;
1712 ztest_block_tag_t *bt = data;
1713 ztest_block_tag_t *bbt;
1714 uint64_t gen, txg, lrtxg, crtxg;
1715 dmu_object_info_t doi;
1718 arc_buf_t *abuf = NULL;
1722 byteswap_uint64_array(lr, sizeof (*lr));
1724 offset = lr->lr_offset;
1725 length = lr->lr_length;
1727 /* If it's a dmu_sync() block, write the whole block */
1728 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1729 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1730 if (length < blocksize) {
1731 offset -= offset % blocksize;
1736 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1737 byteswap_uint64_array(bt, sizeof (*bt));
1739 if (bt->bt_magic != BT_MAGIC)
1742 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1743 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1745 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1747 dmu_object_info_from_db(db, &doi);
1749 bbt = ztest_bt_bonus(db);
1750 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1752 crtxg = bbt->bt_crtxg;
1753 lrtxg = lr->lr_common.lrc_txg;
1755 tx = dmu_tx_create(os);
1757 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1759 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1760 P2PHASE(offset, length) == 0)
1761 abuf = dmu_request_arcbuf(db, length);
1763 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1766 dmu_return_arcbuf(abuf);
1767 dmu_buf_rele(db, FTAG);
1768 ztest_range_unlock(zd, rl);
1769 ztest_object_unlock(zd, lr->lr_foid);
1775 * Usually, verify the old data before writing new data --
1776 * but not always, because we also want to verify correct
1777 * behavior when the data was not recently read into cache.
1779 ASSERT(offset % doi.doi_data_block_size == 0);
1780 if (ztest_random(4) != 0) {
1781 int prefetch = ztest_random(2) ?
1782 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1783 ztest_block_tag_t rbt;
1785 VERIFY(dmu_read(os, lr->lr_foid, offset,
1786 sizeof (rbt), &rbt, prefetch) == 0);
1787 if (rbt.bt_magic == BT_MAGIC) {
1788 ztest_bt_verify(&rbt, os, lr->lr_foid,
1789 offset, gen, txg, crtxg);
1794 * Writes can appear to be newer than the bonus buffer because
1795 * the ztest_get_data() callback does a dmu_read() of the
1796 * open-context data, which may be different than the data
1797 * as it was when the write was generated.
1799 if (zd->zd_zilog->zl_replay) {
1800 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1801 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1806 * Set the bt's gen/txg to the bonus buffer's gen/txg
1807 * so that all of the usual ASSERTs will work.
1809 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1813 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1815 bcopy(data, abuf->b_data, length);
1816 dmu_assign_arcbuf(db, offset, abuf, tx);
1819 (void) ztest_log_write(zd, tx, lr);
1821 dmu_buf_rele(db, FTAG);
1825 ztest_range_unlock(zd, rl);
1826 ztest_object_unlock(zd, lr->lr_foid);
1832 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1834 objset_t *os = zd->zd_os;
1840 byteswap_uint64_array(lr, sizeof (*lr));
1842 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1843 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1846 tx = dmu_tx_create(os);
1848 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1850 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1852 ztest_range_unlock(zd, rl);
1853 ztest_object_unlock(zd, lr->lr_foid);
1857 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1858 lr->lr_length, tx) == 0);
1860 (void) ztest_log_truncate(zd, tx, lr);
1864 ztest_range_unlock(zd, rl);
1865 ztest_object_unlock(zd, lr->lr_foid);
1871 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1873 objset_t *os = zd->zd_os;
1876 ztest_block_tag_t *bbt;
1877 uint64_t txg, lrtxg, crtxg;
1880 byteswap_uint64_array(lr, sizeof (*lr));
1882 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1884 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1886 tx = dmu_tx_create(os);
1887 dmu_tx_hold_bonus(tx, lr->lr_foid);
1889 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1891 dmu_buf_rele(db, FTAG);
1892 ztest_object_unlock(zd, lr->lr_foid);
1896 bbt = ztest_bt_bonus(db);
1897 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1898 crtxg = bbt->bt_crtxg;
1899 lrtxg = lr->lr_common.lrc_txg;
1901 if (zd->zd_zilog->zl_replay) {
1902 ASSERT(lr->lr_size != 0);
1903 ASSERT(lr->lr_mode != 0);
1907 * Randomly change the size and increment the generation.
1909 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1911 lr->lr_mode = bbt->bt_gen + 1;
1916 * Verify that the current bonus buffer is not newer than our txg.
1918 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1919 MAX(txg, lrtxg), crtxg);
1921 dmu_buf_will_dirty(db, tx);
1923 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1924 ASSERT3U(lr->lr_size, <=, db->db_size);
1925 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1926 bbt = ztest_bt_bonus(db);
1928 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1930 dmu_buf_rele(db, FTAG);
1932 (void) ztest_log_setattr(zd, tx, lr);
1936 ztest_object_unlock(zd, lr->lr_foid);
1941 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1942 NULL, /* 0 no such transaction type */
1943 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1944 NULL, /* TX_MKDIR */
1945 NULL, /* TX_MKXATTR */
1946 NULL, /* TX_SYMLINK */
1947 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1948 NULL, /* TX_RMDIR */
1950 NULL, /* TX_RENAME */
1951 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1952 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1953 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1955 NULL, /* TX_CREATE_ACL */
1956 NULL, /* TX_CREATE_ATTR */
1957 NULL, /* TX_CREATE_ACL_ATTR */
1958 NULL, /* TX_MKDIR_ACL */
1959 NULL, /* TX_MKDIR_ATTR */
1960 NULL, /* TX_MKDIR_ACL_ATTR */
1961 NULL, /* TX_WRITE2 */
1965 * ZIL get_data callbacks
1967 typedef struct ztest_zgd_private {
1971 } ztest_zgd_private_t;
1974 ztest_get_done(zgd_t *zgd, int error)
1976 ztest_zgd_private_t *zzp = zgd->zgd_private;
1977 ztest_ds_t *zd = zzp->z_zd;
1978 uint64_t object = zzp->z_object;
1981 dmu_buf_rele(zgd->zgd_db, zgd);
1983 ztest_range_unlock(zd, zzp->z_rl);
1984 ztest_object_unlock(zd, object);
1986 if (error == 0 && zgd->zgd_bp)
1987 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1989 umem_free(zgd, sizeof (*zgd));
1990 umem_free(zzp, sizeof (*zzp));
1994 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1996 ztest_ds_t *zd = arg;
1997 objset_t *os = zd->zd_os;
1998 uint64_t object = lr->lr_foid;
1999 uint64_t offset = lr->lr_offset;
2000 uint64_t size = lr->lr_length;
2001 blkptr_t *bp = &lr->lr_blkptr;
2002 uint64_t txg = lr->lr_common.lrc_txg;
2004 dmu_object_info_t doi;
2008 ztest_zgd_private_t *zgd_private;
2010 ztest_object_lock(zd, object, RL_READER);
2011 error = dmu_bonus_hold(os, object, FTAG, &db);
2013 ztest_object_unlock(zd, object);
2017 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2019 if (crtxg == 0 || crtxg > txg) {
2020 dmu_buf_rele(db, FTAG);
2021 ztest_object_unlock(zd, object);
2025 dmu_object_info_from_db(db, &doi);
2026 dmu_buf_rele(db, FTAG);
2029 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2030 zgd->zgd_zilog = zd->zd_zilog;
2031 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2032 zgd_private->z_zd = zd;
2033 zgd_private->z_object = object;
2034 zgd->zgd_private = zgd_private;
2036 if (buf != NULL) { /* immediate write */
2037 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2040 error = dmu_read(os, object, offset, size, buf,
2041 DMU_READ_NO_PREFETCH);
2044 size = doi.doi_data_block_size;
2046 offset = P2ALIGN(offset, size);
2048 ASSERT(offset < size);
2052 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2055 error = dmu_buf_hold(os, object, offset, zgd, &db,
2056 DMU_READ_NO_PREFETCH);
2059 blkptr_t *obp = dmu_buf_get_blkptr(db);
2061 ASSERT(BP_IS_HOLE(bp));
2068 ASSERT(db->db_offset == offset);
2069 ASSERT(db->db_size == size);
2071 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2072 ztest_get_done, zgd);
2079 ztest_get_done(zgd, error);
2085 ztest_lr_alloc(size_t lrsize, char *name)
2088 size_t namesize = name ? strlen(name) + 1 : 0;
2090 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2093 bcopy(name, lr + lrsize, namesize);
2099 ztest_lr_free(void *lr, size_t lrsize, char *name)
2101 size_t namesize = name ? strlen(name) + 1 : 0;
2103 umem_free(lr, lrsize + namesize);
2107 * Lookup a bunch of objects. Returns the number of objects not found.
2110 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2116 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2118 for (i = 0; i < count; i++, od++) {
2120 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2121 sizeof (uint64_t), 1, &od->od_object);
2123 ASSERT(error == ENOENT);
2124 ASSERT(od->od_object == 0);
2128 ztest_block_tag_t *bbt;
2129 dmu_object_info_t doi;
2131 ASSERT(od->od_object != 0);
2132 ASSERT(missing == 0); /* there should be no gaps */
2134 ztest_object_lock(zd, od->od_object, RL_READER);
2135 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2136 od->od_object, FTAG, &db));
2137 dmu_object_info_from_db(db, &doi);
2138 bbt = ztest_bt_bonus(db);
2139 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2140 od->od_type = doi.doi_type;
2141 od->od_blocksize = doi.doi_data_block_size;
2142 od->od_gen = bbt->bt_gen;
2143 dmu_buf_rele(db, FTAG);
2144 ztest_object_unlock(zd, od->od_object);
2152 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2157 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2159 for (i = 0; i < count; i++, od++) {
2166 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2168 lr->lr_doid = od->od_dir;
2169 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2170 lr->lrz_type = od->od_crtype;
2171 lr->lrz_blocksize = od->od_crblocksize;
2172 lr->lrz_ibshift = ztest_random_ibshift();
2173 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2174 lr->lrz_bonuslen = dmu_bonus_max();
2175 lr->lr_gen = od->od_crgen;
2176 lr->lr_crtime[0] = time(NULL);
2178 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2179 ASSERT(missing == 0);
2183 od->od_object = lr->lr_foid;
2184 od->od_type = od->od_crtype;
2185 od->od_blocksize = od->od_crblocksize;
2186 od->od_gen = od->od_crgen;
2187 ASSERT(od->od_object != 0);
2190 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2197 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2203 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2207 for (i = count - 1; i >= 0; i--, od--) {
2214 * No object was found.
2216 if (od->od_object == 0)
2219 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2221 lr->lr_doid = od->od_dir;
2223 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2224 ASSERT3U(error, ==, ENOSPC);
2229 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2236 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2242 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2244 lr->lr_foid = object;
2245 lr->lr_offset = offset;
2246 lr->lr_length = size;
2248 BP_ZERO(&lr->lr_blkptr);
2250 bcopy(data, lr + 1, size);
2252 error = ztest_replay_write(zd, lr, B_FALSE);
2254 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2260 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2265 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2267 lr->lr_foid = object;
2268 lr->lr_offset = offset;
2269 lr->lr_length = size;
2271 error = ztest_replay_truncate(zd, lr, B_FALSE);
2273 ztest_lr_free(lr, sizeof (*lr), NULL);
2279 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2284 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2286 lr->lr_foid = object;
2290 error = ztest_replay_setattr(zd, lr, B_FALSE);
2292 ztest_lr_free(lr, sizeof (*lr), NULL);
2298 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2300 objset_t *os = zd->zd_os;
2305 txg_wait_synced(dmu_objset_pool(os), 0);
2307 ztest_object_lock(zd, object, RL_READER);
2308 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2310 tx = dmu_tx_create(os);
2312 dmu_tx_hold_write(tx, object, offset, size);
2314 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2317 dmu_prealloc(os, object, offset, size, tx);
2319 txg_wait_synced(dmu_objset_pool(os), txg);
2321 (void) dmu_free_long_range(os, object, offset, size);
2324 ztest_range_unlock(zd, rl);
2325 ztest_object_unlock(zd, object);
2329 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2332 ztest_block_tag_t wbt;
2333 dmu_object_info_t doi;
2334 enum ztest_io_type io_type;
2338 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2339 blocksize = doi.doi_data_block_size;
2340 data = umem_alloc(blocksize, UMEM_NOFAIL);
2343 * Pick an i/o type at random, biased toward writing block tags.
2345 io_type = ztest_random(ZTEST_IO_TYPES);
2346 if (ztest_random(2) == 0)
2347 io_type = ZTEST_IO_WRITE_TAG;
2349 (void) rw_rdlock(&zd->zd_zilog_lock);
2353 case ZTEST_IO_WRITE_TAG:
2354 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2355 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2358 case ZTEST_IO_WRITE_PATTERN:
2359 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2360 if (ztest_random(2) == 0) {
2362 * Induce fletcher2 collisions to ensure that
2363 * zio_ddt_collision() detects and resolves them
2364 * when using fletcher2-verify for deduplication.
2366 ((uint64_t *)data)[0] ^= 1ULL << 63;
2367 ((uint64_t *)data)[4] ^= 1ULL << 63;
2369 (void) ztest_write(zd, object, offset, blocksize, data);
2372 case ZTEST_IO_WRITE_ZEROES:
2373 bzero(data, blocksize);
2374 (void) ztest_write(zd, object, offset, blocksize, data);
2377 case ZTEST_IO_TRUNCATE:
2378 (void) ztest_truncate(zd, object, offset, blocksize);
2381 case ZTEST_IO_SETATTR:
2382 (void) ztest_setattr(zd, object);
2387 case ZTEST_IO_REWRITE:
2388 (void) rw_rdlock(&ztest_name_lock);
2389 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2390 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2392 VERIFY(err == 0 || err == ENOSPC);
2393 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2394 ZFS_PROP_COMPRESSION,
2395 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2397 VERIFY(err == 0 || err == ENOSPC);
2398 (void) rw_unlock(&ztest_name_lock);
2400 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2401 DMU_READ_NO_PREFETCH));
2403 (void) ztest_write(zd, object, offset, blocksize, data);
2407 (void) rw_unlock(&zd->zd_zilog_lock);
2409 umem_free(data, blocksize);
2413 * Initialize an object description template.
2416 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2417 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2419 od->od_dir = ZTEST_DIROBJ;
2422 od->od_crtype = type;
2423 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2426 od->od_type = DMU_OT_NONE;
2427 od->od_blocksize = 0;
2430 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2431 tag, (longlong_t)id, (u_longlong_t)index);
2435 * Lookup or create the objects for a test using the od template.
2436 * If the objects do not all exist, or if 'remove' is specified,
2437 * remove any existing objects and create new ones. Otherwise,
2438 * use the existing objects.
2441 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2443 int count = size / sizeof (*od);
2446 mutex_enter(&zd->zd_dirobj_lock);
2447 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2448 (ztest_remove(zd, od, count) != 0 ||
2449 ztest_create(zd, od, count) != 0))
2452 mutex_exit(&zd->zd_dirobj_lock);
2459 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2461 zilog_t *zilog = zd->zd_zilog;
2463 (void) rw_rdlock(&zd->zd_zilog_lock);
2465 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2468 * Remember the committed values in zd, which is in parent/child
2469 * shared memory. If we die, the next iteration of ztest_run()
2470 * will verify that the log really does contain this record.
2472 mutex_enter(&zilog->zl_lock);
2473 ASSERT(zd->zd_shared != NULL);
2474 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2475 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2476 mutex_exit(&zilog->zl_lock);
2478 (void) rw_unlock(&zd->zd_zilog_lock);
2482 * This function is designed to simulate the operations that occur during a
2483 * mount/unmount operation. We hold the dataset across these operations in an
2484 * attempt to expose any implicit assumptions about ZIL management.
2488 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2490 objset_t *os = zd->zd_os;
2493 * We grab the zd_dirobj_lock to ensure that no other thread is
2494 * updating the zil (i.e. adding in-memory log records) and the
2495 * zd_zilog_lock to block any I/O.
2497 mutex_enter(&zd->zd_dirobj_lock);
2498 (void) rw_wrlock(&zd->zd_zilog_lock);
2500 /* zfs_sb_teardown() */
2501 zil_close(zd->zd_zilog);
2503 /* zfsvfs_setup() */
2504 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2505 zil_replay(os, zd, ztest_replay_vector);
2507 (void) rw_unlock(&zd->zd_zilog_lock);
2508 mutex_exit(&zd->zd_dirobj_lock);
2512 * Verify that we can't destroy an active pool, create an existing pool,
2513 * or create a pool with a bad vdev spec.
2517 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2519 ztest_shared_opts_t *zo = &ztest_opts;
2524 * Attempt to create using a bad file.
2526 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2527 VERIFY3U(ENOENT, ==,
2528 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2529 nvlist_free(nvroot);
2532 * Attempt to create using a bad mirror.
2534 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2535 VERIFY3U(ENOENT, ==,
2536 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2537 nvlist_free(nvroot);
2540 * Attempt to create an existing pool. It shouldn't matter
2541 * what's in the nvroot; we should fail with EEXIST.
2543 (void) rw_rdlock(&ztest_name_lock);
2544 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2545 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2546 nvlist_free(nvroot);
2547 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2548 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2549 spa_close(spa, FTAG);
2551 (void) rw_unlock(&ztest_name_lock);
2556 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2559 uint64_t initial_version = SPA_VERSION_INITIAL;
2560 uint64_t version, newversion;
2561 nvlist_t *nvroot, *props;
2564 mutex_enter(&ztest_vdev_lock);
2565 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2568 * Clean up from previous runs.
2570 (void) spa_destroy(name);
2572 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2573 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2576 * If we're configuring a RAIDZ device then make sure that the
2577 * the initial version is capable of supporting that feature.
2579 switch (ztest_opts.zo_raidz_parity) {
2582 initial_version = SPA_VERSION_INITIAL;
2585 initial_version = SPA_VERSION_RAIDZ2;
2588 initial_version = SPA_VERSION_RAIDZ3;
2593 * Create a pool with a spa version that can be upgraded. Pick
2594 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2597 version = ztest_random_spa_version(initial_version);
2598 } while (version > SPA_VERSION_BEFORE_FEATURES);
2600 props = fnvlist_alloc();
2601 fnvlist_add_uint64(props,
2602 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2603 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2604 fnvlist_free(nvroot);
2605 fnvlist_free(props);
2607 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2608 VERIFY3U(spa_version(spa), ==, version);
2609 newversion = ztest_random_spa_version(version + 1);
2611 if (ztest_opts.zo_verbose >= 4) {
2612 (void) printf("upgrading spa version from %llu to %llu\n",
2613 (u_longlong_t)version, (u_longlong_t)newversion);
2616 spa_upgrade(spa, newversion);
2617 VERIFY3U(spa_version(spa), >, version);
2618 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2619 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2620 spa_close(spa, FTAG);
2623 mutex_exit(&ztest_vdev_lock);
2627 vdev_lookup_by_path(vdev_t *vd, const char *path)
2632 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2635 for (c = 0; c < vd->vdev_children; c++)
2636 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2644 * Find the first available hole which can be used as a top-level.
2647 find_vdev_hole(spa_t *spa)
2649 vdev_t *rvd = spa->spa_root_vdev;
2652 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2654 for (c = 0; c < rvd->vdev_children; c++) {
2655 vdev_t *cvd = rvd->vdev_child[c];
2657 if (cvd->vdev_ishole)
2664 * Verify that vdev_add() works as expected.
2668 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2670 ztest_shared_t *zs = ztest_shared;
2671 spa_t *spa = ztest_spa;
2677 mutex_enter(&ztest_vdev_lock);
2678 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2680 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2682 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2685 * If we have slogs then remove them 1/4 of the time.
2687 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2689 * Grab the guid from the head of the log class rotor.
2691 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2693 spa_config_exit(spa, SCL_VDEV, FTAG);
2696 * We have to grab the zs_name_lock as writer to
2697 * prevent a race between removing a slog (dmu_objset_find)
2698 * and destroying a dataset. Removing the slog will
2699 * grab a reference on the dataset which may cause
2700 * dsl_destroy_head() to fail with EBUSY thus
2701 * leaving the dataset in an inconsistent state.
2703 rw_wrlock(&ztest_name_lock);
2704 error = spa_vdev_remove(spa, guid, B_FALSE);
2705 rw_unlock(&ztest_name_lock);
2707 if (error && error != EEXIST)
2708 fatal(0, "spa_vdev_remove() = %d", error);
2710 spa_config_exit(spa, SCL_VDEV, FTAG);
2713 * Make 1/4 of the devices be log devices.
2715 nvroot = make_vdev_root(NULL, NULL, NULL,
2716 ztest_opts.zo_vdev_size, 0,
2717 ztest_random(4) == 0, ztest_opts.zo_raidz,
2720 error = spa_vdev_add(spa, nvroot);
2721 nvlist_free(nvroot);
2723 if (error == ENOSPC)
2724 ztest_record_enospc("spa_vdev_add");
2725 else if (error != 0)
2726 fatal(0, "spa_vdev_add() = %d", error);
2729 mutex_exit(&ztest_vdev_lock);
2733 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2737 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2739 ztest_shared_t *zs = ztest_shared;
2740 spa_t *spa = ztest_spa;
2741 vdev_t *rvd = spa->spa_root_vdev;
2742 spa_aux_vdev_t *sav;
2748 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2750 if (ztest_random(2) == 0) {
2751 sav = &spa->spa_spares;
2752 aux = ZPOOL_CONFIG_SPARES;
2754 sav = &spa->spa_l2cache;
2755 aux = ZPOOL_CONFIG_L2CACHE;
2758 mutex_enter(&ztest_vdev_lock);
2760 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2762 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2764 * Pick a random device to remove.
2766 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2769 * Find an unused device we can add.
2771 zs->zs_vdev_aux = 0;
2774 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2775 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2777 for (c = 0; c < sav->sav_count; c++)
2778 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2781 if (c == sav->sav_count &&
2782 vdev_lookup_by_path(rvd, path) == NULL)
2788 spa_config_exit(spa, SCL_VDEV, FTAG);
2794 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2795 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2796 error = spa_vdev_add(spa, nvroot);
2798 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2799 nvlist_free(nvroot);
2802 * Remove an existing device. Sometimes, dirty its
2803 * vdev state first to make sure we handle removal
2804 * of devices that have pending state changes.
2806 if (ztest_random(2) == 0)
2807 (void) vdev_online(spa, guid, 0, NULL);
2809 error = spa_vdev_remove(spa, guid, B_FALSE);
2810 if (error != 0 && error != EBUSY)
2811 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2814 mutex_exit(&ztest_vdev_lock);
2816 umem_free(path, MAXPATHLEN);
2820 * split a pool if it has mirror tlvdevs
2824 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2826 ztest_shared_t *zs = ztest_shared;
2827 spa_t *spa = ztest_spa;
2828 vdev_t *rvd = spa->spa_root_vdev;
2829 nvlist_t *tree, **child, *config, *split, **schild;
2830 uint_t c, children, schildren = 0, lastlogid = 0;
2833 mutex_enter(&ztest_vdev_lock);
2835 /* ensure we have a useable config; mirrors of raidz aren't supported */
2836 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2837 mutex_exit(&ztest_vdev_lock);
2841 /* clean up the old pool, if any */
2842 (void) spa_destroy("splitp");
2844 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2846 /* generate a config from the existing config */
2847 mutex_enter(&spa->spa_props_lock);
2848 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2850 mutex_exit(&spa->spa_props_lock);
2852 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2855 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2856 for (c = 0; c < children; c++) {
2857 vdev_t *tvd = rvd->vdev_child[c];
2861 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2862 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2864 VERIFY(nvlist_add_string(schild[schildren],
2865 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2866 VERIFY(nvlist_add_uint64(schild[schildren],
2867 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2869 lastlogid = schildren;
2874 VERIFY(nvlist_lookup_nvlist_array(child[c],
2875 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2876 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2879 /* OK, create a config that can be used to split */
2880 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2881 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2882 VDEV_TYPE_ROOT) == 0);
2883 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2884 lastlogid != 0 ? lastlogid : schildren) == 0);
2886 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2887 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2889 for (c = 0; c < schildren; c++)
2890 nvlist_free(schild[c]);
2894 spa_config_exit(spa, SCL_VDEV, FTAG);
2896 (void) rw_wrlock(&ztest_name_lock);
2897 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2898 (void) rw_unlock(&ztest_name_lock);
2900 nvlist_free(config);
2903 (void) printf("successful split - results:\n");
2904 mutex_enter(&spa_namespace_lock);
2905 show_pool_stats(spa);
2906 show_pool_stats(spa_lookup("splitp"));
2907 mutex_exit(&spa_namespace_lock);
2911 mutex_exit(&ztest_vdev_lock);
2916 * Verify that we can attach and detach devices.
2920 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2922 ztest_shared_t *zs = ztest_shared;
2923 spa_t *spa = ztest_spa;
2924 spa_aux_vdev_t *sav = &spa->spa_spares;
2925 vdev_t *rvd = spa->spa_root_vdev;
2926 vdev_t *oldvd, *newvd, *pvd;
2930 uint64_t ashift = ztest_get_ashift();
2931 uint64_t oldguid, pguid;
2932 uint64_t oldsize, newsize;
2933 char *oldpath, *newpath;
2935 int oldvd_has_siblings = B_FALSE;
2936 int newvd_is_spare = B_FALSE;
2938 int error, expected_error;
2940 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2941 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2943 mutex_enter(&ztest_vdev_lock);
2944 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2946 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2949 * Decide whether to do an attach or a replace.
2951 replacing = ztest_random(2);
2954 * Pick a random top-level vdev.
2956 top = ztest_random_vdev_top(spa, B_TRUE);
2959 * Pick a random leaf within it.
2961 leaf = ztest_random(leaves);
2966 oldvd = rvd->vdev_child[top];
2967 if (zs->zs_mirrors >= 1) {
2968 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2969 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2970 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2972 if (ztest_opts.zo_raidz > 1) {
2973 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2974 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2975 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2979 * If we're already doing an attach or replace, oldvd may be a
2980 * mirror vdev -- in which case, pick a random child.
2982 while (oldvd->vdev_children != 0) {
2983 oldvd_has_siblings = B_TRUE;
2984 ASSERT(oldvd->vdev_children >= 2);
2985 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2988 oldguid = oldvd->vdev_guid;
2989 oldsize = vdev_get_min_asize(oldvd);
2990 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2991 (void) strcpy(oldpath, oldvd->vdev_path);
2992 pvd = oldvd->vdev_parent;
2993 pguid = pvd->vdev_guid;
2996 * If oldvd has siblings, then half of the time, detach it.
2998 if (oldvd_has_siblings && ztest_random(2) == 0) {
2999 spa_config_exit(spa, SCL_VDEV, FTAG);
3000 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3001 if (error != 0 && error != ENODEV && error != EBUSY &&
3003 fatal(0, "detach (%s) returned %d", oldpath, error);
3008 * For the new vdev, choose with equal probability between the two
3009 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3011 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3012 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3013 newvd_is_spare = B_TRUE;
3014 (void) strcpy(newpath, newvd->vdev_path);
3016 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3017 ztest_opts.zo_dir, ztest_opts.zo_pool,
3018 top * leaves + leaf);
3019 if (ztest_random(2) == 0)
3020 newpath[strlen(newpath) - 1] = 'b';
3021 newvd = vdev_lookup_by_path(rvd, newpath);
3025 newsize = vdev_get_min_asize(newvd);
3028 * Make newsize a little bigger or smaller than oldsize.
3029 * If it's smaller, the attach should fail.
3030 * If it's larger, and we're doing a replace,
3031 * we should get dynamic LUN growth when we're done.
3033 newsize = 10 * oldsize / (9 + ztest_random(3));
3037 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3038 * unless it's a replace; in that case any non-replacing parent is OK.
3040 * If newvd is already part of the pool, it should fail with EBUSY.
3042 * If newvd is too small, it should fail with EOVERFLOW.
3044 if (pvd->vdev_ops != &vdev_mirror_ops &&
3045 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3046 pvd->vdev_ops == &vdev_replacing_ops ||
3047 pvd->vdev_ops == &vdev_spare_ops))
3048 expected_error = ENOTSUP;
3049 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3050 expected_error = ENOTSUP;
3051 else if (newvd == oldvd)
3052 expected_error = replacing ? 0 : EBUSY;
3053 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3054 expected_error = EBUSY;
3055 else if (newsize < oldsize)
3056 expected_error = EOVERFLOW;
3057 else if (ashift > oldvd->vdev_top->vdev_ashift)
3058 expected_error = EDOM;
3062 spa_config_exit(spa, SCL_VDEV, FTAG);
3065 * Build the nvlist describing newpath.
3067 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3068 ashift, 0, 0, 0, 1);
3070 error = spa_vdev_attach(spa, oldguid, root, replacing);
3075 * If our parent was the replacing vdev, but the replace completed,
3076 * then instead of failing with ENOTSUP we may either succeed,
3077 * fail with ENODEV, or fail with EOVERFLOW.
3079 if (expected_error == ENOTSUP &&
3080 (error == 0 || error == ENODEV || error == EOVERFLOW))
3081 expected_error = error;
3084 * If someone grew the LUN, the replacement may be too small.
3086 if (error == EOVERFLOW || error == EBUSY)
3087 expected_error = error;
3089 /* XXX workaround 6690467 */
3090 if (error != expected_error && expected_error != EBUSY) {
3091 fatal(0, "attach (%s %llu, %s %llu, %d) "
3092 "returned %d, expected %d",
3093 oldpath, oldsize, newpath,
3094 newsize, replacing, error, expected_error);
3097 mutex_exit(&ztest_vdev_lock);
3099 umem_free(oldpath, MAXPATHLEN);
3100 umem_free(newpath, MAXPATHLEN);
3104 * Callback function which expands the physical size of the vdev.
3107 grow_vdev(vdev_t *vd, void *arg)
3109 ASSERTV(spa_t *spa = vd->vdev_spa);
3110 size_t *newsize = arg;
3114 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3115 ASSERT(vd->vdev_ops->vdev_op_leaf);
3117 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3120 fsize = lseek(fd, 0, SEEK_END);
3121 VERIFY(ftruncate(fd, *newsize) == 0);
3123 if (ztest_opts.zo_verbose >= 6) {
3124 (void) printf("%s grew from %lu to %lu bytes\n",
3125 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3132 * Callback function which expands a given vdev by calling vdev_online().
3136 online_vdev(vdev_t *vd, void *arg)
3138 spa_t *spa = vd->vdev_spa;
3139 vdev_t *tvd = vd->vdev_top;
3140 uint64_t guid = vd->vdev_guid;
3141 uint64_t generation = spa->spa_config_generation + 1;
3142 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3145 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3146 ASSERT(vd->vdev_ops->vdev_op_leaf);
3148 /* Calling vdev_online will initialize the new metaslabs */
3149 spa_config_exit(spa, SCL_STATE, spa);
3150 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3151 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3154 * If vdev_online returned an error or the underlying vdev_open
3155 * failed then we abort the expand. The only way to know that
3156 * vdev_open fails is by checking the returned newstate.
3158 if (error || newstate != VDEV_STATE_HEALTHY) {
3159 if (ztest_opts.zo_verbose >= 5) {
3160 (void) printf("Unable to expand vdev, state %llu, "
3161 "error %d\n", (u_longlong_t)newstate, error);
3165 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3168 * Since we dropped the lock we need to ensure that we're
3169 * still talking to the original vdev. It's possible this
3170 * vdev may have been detached/replaced while we were
3171 * trying to online it.
3173 if (generation != spa->spa_config_generation) {
3174 if (ztest_opts.zo_verbose >= 5) {
3175 (void) printf("vdev configuration has changed, "
3176 "guid %llu, state %llu, expected gen %llu, "
3179 (u_longlong_t)tvd->vdev_state,
3180 (u_longlong_t)generation,
3181 (u_longlong_t)spa->spa_config_generation);
3189 * Traverse the vdev tree calling the supplied function.
3190 * We continue to walk the tree until we either have walked all
3191 * children or we receive a non-NULL return from the callback.
3192 * If a NULL callback is passed, then we just return back the first
3193 * leaf vdev we encounter.
3196 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3200 if (vd->vdev_ops->vdev_op_leaf) {
3204 return (func(vd, arg));
3207 for (c = 0; c < vd->vdev_children; c++) {
3208 vdev_t *cvd = vd->vdev_child[c];
3209 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3216 * Verify that dynamic LUN growth works as expected.
3220 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3222 spa_t *spa = ztest_spa;
3224 metaslab_class_t *mc;
3225 metaslab_group_t *mg;
3226 size_t psize, newsize;
3228 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3230 mutex_enter(&ztest_vdev_lock);
3231 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3233 top = ztest_random_vdev_top(spa, B_TRUE);
3235 tvd = spa->spa_root_vdev->vdev_child[top];
3238 old_ms_count = tvd->vdev_ms_count;
3239 old_class_space = metaslab_class_get_space(mc);
3242 * Determine the size of the first leaf vdev associated with
3243 * our top-level device.
3245 vd = vdev_walk_tree(tvd, NULL, NULL);
3246 ASSERT3P(vd, !=, NULL);
3247 ASSERT(vd->vdev_ops->vdev_op_leaf);
3249 psize = vd->vdev_psize;
3252 * We only try to expand the vdev if it's healthy, less than 4x its
3253 * original size, and it has a valid psize.
3255 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3256 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3257 spa_config_exit(spa, SCL_STATE, spa);
3258 mutex_exit(&ztest_vdev_lock);
3262 newsize = psize + psize / 8;
3263 ASSERT3U(newsize, >, psize);
3265 if (ztest_opts.zo_verbose >= 6) {
3266 (void) printf("Expanding LUN %s from %lu to %lu\n",
3267 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3271 * Growing the vdev is a two step process:
3272 * 1). expand the physical size (i.e. relabel)
3273 * 2). online the vdev to create the new metaslabs
3275 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3276 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3277 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3278 if (ztest_opts.zo_verbose >= 5) {
3279 (void) printf("Could not expand LUN because "
3280 "the vdev configuration changed.\n");
3282 spa_config_exit(spa, SCL_STATE, spa);
3283 mutex_exit(&ztest_vdev_lock);
3287 spa_config_exit(spa, SCL_STATE, spa);
3290 * Expanding the LUN will update the config asynchronously,
3291 * thus we must wait for the async thread to complete any
3292 * pending tasks before proceeding.
3296 mutex_enter(&spa->spa_async_lock);
3297 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3298 mutex_exit(&spa->spa_async_lock);
3301 txg_wait_synced(spa_get_dsl(spa), 0);
3302 (void) poll(NULL, 0, 100);
3305 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3307 tvd = spa->spa_root_vdev->vdev_child[top];
3308 new_ms_count = tvd->vdev_ms_count;
3309 new_class_space = metaslab_class_get_space(mc);
3311 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3312 if (ztest_opts.zo_verbose >= 5) {
3313 (void) printf("Could not verify LUN expansion due to "
3314 "intervening vdev offline or remove.\n");
3316 spa_config_exit(spa, SCL_STATE, spa);
3317 mutex_exit(&ztest_vdev_lock);
3322 * Make sure we were able to grow the vdev.
3324 if (new_ms_count <= old_ms_count)
3325 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3326 old_ms_count, new_ms_count);
3329 * Make sure we were able to grow the pool.
3331 if (new_class_space <= old_class_space)
3332 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3333 old_class_space, new_class_space);
3335 if (ztest_opts.zo_verbose >= 5) {
3336 char oldnumbuf[6], newnumbuf[6];
3338 nicenum(old_class_space, oldnumbuf);
3339 nicenum(new_class_space, newnumbuf);
3340 (void) printf("%s grew from %s to %s\n",
3341 spa->spa_name, oldnumbuf, newnumbuf);
3344 spa_config_exit(spa, SCL_STATE, spa);
3345 mutex_exit(&ztest_vdev_lock);
3349 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3353 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3356 * Create the objects common to all ztest datasets.
3358 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3359 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3363 ztest_dataset_create(char *dsname)
3365 uint64_t zilset = ztest_random(100);
3366 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3367 ztest_objset_create_cb, NULL);
3369 if (err || zilset < 80)
3372 if (ztest_opts.zo_verbose >= 5)
3373 (void) printf("Setting dataset %s to sync always\n", dsname);
3374 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3375 ZFS_SYNC_ALWAYS, B_FALSE));
3380 ztest_objset_destroy_cb(const char *name, void *arg)
3383 dmu_object_info_t doi;
3387 * Verify that the dataset contains a directory object.
3389 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3390 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3391 if (error != ENOENT) {
3392 /* We could have crashed in the middle of destroying it */
3394 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3395 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3397 dmu_objset_disown(os, FTAG);
3400 * Destroy the dataset.
3402 if (strchr(name, '@') != NULL) {
3403 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3405 VERIFY0(dsl_destroy_head(name));
3411 ztest_snapshot_create(char *osname, uint64_t id)
3413 char snapname[MAXNAMELEN];
3416 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3418 error = dmu_objset_snapshot_one(osname, snapname);
3419 if (error == ENOSPC) {
3420 ztest_record_enospc(FTAG);
3423 if (error != 0 && error != EEXIST) {
3424 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3431 ztest_snapshot_destroy(char *osname, uint64_t id)
3433 char snapname[MAXNAMELEN];
3436 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3439 error = dsl_destroy_snapshot(snapname, B_FALSE);
3440 if (error != 0 && error != ENOENT)
3441 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3447 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3457 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3458 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3460 (void) rw_rdlock(&ztest_name_lock);
3462 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3463 ztest_opts.zo_pool, (u_longlong_t)id);
3466 * If this dataset exists from a previous run, process its replay log
3467 * half of the time. If we don't replay it, then dsl_destroy_head()
3468 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3470 if (ztest_random(2) == 0 &&
3471 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3472 ztest_zd_init(zdtmp, NULL, os);
3473 zil_replay(os, zdtmp, ztest_replay_vector);
3474 ztest_zd_fini(zdtmp);
3475 dmu_objset_disown(os, FTAG);
3479 * There may be an old instance of the dataset we're about to
3480 * create lying around from a previous run. If so, destroy it
3481 * and all of its snapshots.
3483 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3484 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3487 * Verify that the destroyed dataset is no longer in the namespace.
3489 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3493 * Verify that we can create a new dataset.
3495 error = ztest_dataset_create(name);
3497 if (error == ENOSPC) {
3498 ztest_record_enospc(FTAG);
3501 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3504 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3506 ztest_zd_init(zdtmp, NULL, os);
3509 * Open the intent log for it.
3511 zilog = zil_open(os, ztest_get_data);
3514 * Put some objects in there, do a little I/O to them,
3515 * and randomly take a couple of snapshots along the way.
3517 iters = ztest_random(5);
3518 for (i = 0; i < iters; i++) {
3519 ztest_dmu_object_alloc_free(zdtmp, id);
3520 if (ztest_random(iters) == 0)
3521 (void) ztest_snapshot_create(name, i);
3525 * Verify that we cannot create an existing dataset.
3527 VERIFY3U(EEXIST, ==,
3528 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3531 * Verify that we can hold an objset that is also owned.
3533 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3534 dmu_objset_rele(os2, FTAG);
3537 * Verify that we cannot own an objset that is already owned.
3540 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3543 dmu_objset_disown(os, FTAG);
3544 ztest_zd_fini(zdtmp);
3546 (void) rw_unlock(&ztest_name_lock);
3548 umem_free(name, MAXNAMELEN);
3549 umem_free(zdtmp, sizeof (ztest_ds_t));
3553 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3556 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3558 (void) rw_rdlock(&ztest_name_lock);
3559 (void) ztest_snapshot_destroy(zd->zd_name, id);
3560 (void) ztest_snapshot_create(zd->zd_name, id);
3561 (void) rw_unlock(&ztest_name_lock);
3565 * Cleanup non-standard snapshots and clones.
3568 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3577 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3578 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3579 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3580 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3581 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3583 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3584 osname, (u_longlong_t)id);
3585 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3586 osname, (u_longlong_t)id);
3587 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3588 clone1name, (u_longlong_t)id);
3589 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3590 osname, (u_longlong_t)id);
3591 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3592 clone1name, (u_longlong_t)id);
3594 error = dsl_destroy_head(clone2name);
3595 if (error && error != ENOENT)
3596 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3597 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3598 if (error && error != ENOENT)
3599 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3600 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3601 if (error && error != ENOENT)
3602 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3603 error = dsl_destroy_head(clone1name);
3604 if (error && error != ENOENT)
3605 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3606 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3607 if (error && error != ENOENT)
3608 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3610 umem_free(snap1name, MAXNAMELEN);
3611 umem_free(clone1name, MAXNAMELEN);
3612 umem_free(snap2name, MAXNAMELEN);
3613 umem_free(clone2name, MAXNAMELEN);
3614 umem_free(snap3name, MAXNAMELEN);
3618 * Verify dsl_dataset_promote handles EBUSY
3621 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3629 char *osname = zd->zd_name;
3632 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3633 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3634 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3635 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3636 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3638 (void) rw_rdlock(&ztest_name_lock);
3640 ztest_dsl_dataset_cleanup(osname, id);
3642 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3643 osname, (u_longlong_t)id);
3644 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3645 osname, (u_longlong_t)id);
3646 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3647 clone1name, (u_longlong_t)id);
3648 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3649 osname, (u_longlong_t)id);
3650 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3651 clone1name, (u_longlong_t)id);
3653 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3654 if (error && error != EEXIST) {
3655 if (error == ENOSPC) {
3656 ztest_record_enospc(FTAG);
3659 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3662 error = dmu_objset_clone(clone1name, snap1name);
3664 if (error == ENOSPC) {
3665 ztest_record_enospc(FTAG);
3668 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3671 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3672 if (error && error != EEXIST) {
3673 if (error == ENOSPC) {
3674 ztest_record_enospc(FTAG);
3677 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3680 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3681 if (error && error != EEXIST) {
3682 if (error == ENOSPC) {
3683 ztest_record_enospc(FTAG);
3686 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3689 error = dmu_objset_clone(clone2name, snap3name);
3691 if (error == ENOSPC) {
3692 ztest_record_enospc(FTAG);
3695 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3698 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3700 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3701 error = dsl_dataset_promote(clone2name, NULL);
3702 if (error == ENOSPC) {
3703 dmu_objset_disown(os, FTAG);
3704 ztest_record_enospc(FTAG);
3708 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3710 dmu_objset_disown(os, FTAG);
3713 ztest_dsl_dataset_cleanup(osname, id);
3715 (void) rw_unlock(&ztest_name_lock);
3717 umem_free(snap1name, MAXNAMELEN);
3718 umem_free(clone1name, MAXNAMELEN);
3719 umem_free(snap2name, MAXNAMELEN);
3720 umem_free(clone2name, MAXNAMELEN);
3721 umem_free(snap3name, MAXNAMELEN);
3724 #undef OD_ARRAY_SIZE
3725 #define OD_ARRAY_SIZE 4
3728 * Verify that dmu_object_{alloc,free} work as expected.
3731 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3738 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3739 od = umem_alloc(size, UMEM_NOFAIL);
3740 batchsize = OD_ARRAY_SIZE;
3742 for (b = 0; b < batchsize; b++)
3743 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3746 * Destroy the previous batch of objects, create a new batch,
3747 * and do some I/O on the new objects.
3749 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3752 while (ztest_random(4 * batchsize) != 0)
3753 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3754 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3756 umem_free(od, size);
3759 #undef OD_ARRAY_SIZE
3760 #define OD_ARRAY_SIZE 2
3763 * Verify that dmu_{read,write} work as expected.
3766 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3771 objset_t *os = zd->zd_os;
3772 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3773 od = umem_alloc(size, UMEM_NOFAIL);
3775 int i, freeit, error;
3777 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3778 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3779 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3780 uint64_t regions = 997;
3781 uint64_t stride = 123456789ULL;
3782 uint64_t width = 40;
3783 int free_percent = 5;
3786 * This test uses two objects, packobj and bigobj, that are always
3787 * updated together (i.e. in the same tx) so that their contents are
3788 * in sync and can be compared. Their contents relate to each other
3789 * in a simple way: packobj is a dense array of 'bufwad' structures,
3790 * while bigobj is a sparse array of the same bufwads. Specifically,
3791 * for any index n, there are three bufwads that should be identical:
3793 * packobj, at offset n * sizeof (bufwad_t)
3794 * bigobj, at the head of the nth chunk
3795 * bigobj, at the tail of the nth chunk
3797 * The chunk size is arbitrary. It doesn't have to be a power of two,
3798 * and it doesn't have any relation to the object blocksize.
3799 * The only requirement is that it can hold at least two bufwads.
3801 * Normally, we write the bufwad to each of these locations.
3802 * However, free_percent of the time we instead write zeroes to
3803 * packobj and perform a dmu_free_range() on bigobj. By comparing
3804 * bigobj to packobj, we can verify that the DMU is correctly
3805 * tracking which parts of an object are allocated and free,
3806 * and that the contents of the allocated blocks are correct.
3810 * Read the directory info. If it's the first time, set things up.
3812 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3813 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3815 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3816 umem_free(od, size);
3820 bigobj = od[0].od_object;
3821 packobj = od[1].od_object;
3822 chunksize = od[0].od_gen;
3823 ASSERT(chunksize == od[1].od_gen);
3826 * Prefetch a random chunk of the big object.
3827 * Our aim here is to get some async reads in flight
3828 * for blocks that we may free below; the DMU should
3829 * handle this race correctly.
3831 n = ztest_random(regions) * stride + ztest_random(width);
3832 s = 1 + ztest_random(2 * width - 1);
3833 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3834 ZIO_PRIORITY_SYNC_READ);
3837 * Pick a random index and compute the offsets into packobj and bigobj.
3839 n = ztest_random(regions) * stride + ztest_random(width);
3840 s = 1 + ztest_random(width - 1);
3842 packoff = n * sizeof (bufwad_t);
3843 packsize = s * sizeof (bufwad_t);
3845 bigoff = n * chunksize;
3846 bigsize = s * chunksize;
3848 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3849 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3852 * free_percent of the time, free a range of bigobj rather than
3855 freeit = (ztest_random(100) < free_percent);
3858 * Read the current contents of our objects.
3860 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3863 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3868 * Get a tx for the mods to both packobj and bigobj.
3870 tx = dmu_tx_create(os);
3872 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3875 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3877 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3879 /* This accounts for setting the checksum/compression. */
3880 dmu_tx_hold_bonus(tx, bigobj);
3882 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3884 umem_free(packbuf, packsize);
3885 umem_free(bigbuf, bigsize);
3886 umem_free(od, size);
3890 enum zio_checksum cksum;
3892 cksum = (enum zio_checksum)
3893 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3894 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3895 dmu_object_set_checksum(os, bigobj, cksum, tx);
3897 enum zio_compress comp;
3899 comp = (enum zio_compress)
3900 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3901 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3902 dmu_object_set_compress(os, bigobj, comp, tx);
3905 * For each index from n to n + s, verify that the existing bufwad
3906 * in packobj matches the bufwads at the head and tail of the
3907 * corresponding chunk in bigobj. Then update all three bufwads
3908 * with the new values we want to write out.
3910 for (i = 0; i < s; i++) {
3912 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3914 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3916 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3918 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3919 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3921 if (pack->bw_txg > txg)
3922 fatal(0, "future leak: got %llx, open txg is %llx",
3925 if (pack->bw_data != 0 && pack->bw_index != n + i)
3926 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3927 pack->bw_index, n, i);
3929 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3930 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3932 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3933 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3936 bzero(pack, sizeof (bufwad_t));
3938 pack->bw_index = n + i;
3940 pack->bw_data = 1 + ztest_random(-2ULL);
3947 * We've verified all the old bufwads, and made new ones.
3948 * Now write them out.
3950 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3953 if (ztest_opts.zo_verbose >= 7) {
3954 (void) printf("freeing offset %llx size %llx"
3956 (u_longlong_t)bigoff,
3957 (u_longlong_t)bigsize,
3960 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3962 if (ztest_opts.zo_verbose >= 7) {
3963 (void) printf("writing offset %llx size %llx"
3965 (u_longlong_t)bigoff,
3966 (u_longlong_t)bigsize,
3969 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3975 * Sanity check the stuff we just wrote.
3978 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3979 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3981 VERIFY(0 == dmu_read(os, packobj, packoff,
3982 packsize, packcheck, DMU_READ_PREFETCH));
3983 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3984 bigsize, bigcheck, DMU_READ_PREFETCH));
3986 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3987 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3989 umem_free(packcheck, packsize);
3990 umem_free(bigcheck, bigsize);
3993 umem_free(packbuf, packsize);
3994 umem_free(bigbuf, bigsize);
3995 umem_free(od, size);
3999 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4000 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4008 * For each index from n to n + s, verify that the existing bufwad
4009 * in packobj matches the bufwads at the head and tail of the
4010 * corresponding chunk in bigobj. Then update all three bufwads
4011 * with the new values we want to write out.
4013 for (i = 0; i < s; i++) {
4015 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4017 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4019 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4021 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4022 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4024 if (pack->bw_txg > txg)
4025 fatal(0, "future leak: got %llx, open txg is %llx",
4028 if (pack->bw_data != 0 && pack->bw_index != n + i)
4029 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4030 pack->bw_index, n, i);
4032 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4033 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4035 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4036 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4038 pack->bw_index = n + i;
4040 pack->bw_data = 1 + ztest_random(-2ULL);
4047 #undef OD_ARRAY_SIZE
4048 #define OD_ARRAY_SIZE 2
4051 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4053 objset_t *os = zd->zd_os;
4060 bufwad_t *packbuf, *bigbuf;
4061 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4062 uint64_t blocksize = ztest_random_blocksize();
4063 uint64_t chunksize = blocksize;
4064 uint64_t regions = 997;
4065 uint64_t stride = 123456789ULL;
4067 dmu_buf_t *bonus_db;
4068 arc_buf_t **bigbuf_arcbufs;
4069 dmu_object_info_t doi;
4071 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4072 od = umem_alloc(size, UMEM_NOFAIL);
4075 * This test uses two objects, packobj and bigobj, that are always
4076 * updated together (i.e. in the same tx) so that their contents are
4077 * in sync and can be compared. Their contents relate to each other
4078 * in a simple way: packobj is a dense array of 'bufwad' structures,
4079 * while bigobj is a sparse array of the same bufwads. Specifically,
4080 * for any index n, there are three bufwads that should be identical:
4082 * packobj, at offset n * sizeof (bufwad_t)
4083 * bigobj, at the head of the nth chunk
4084 * bigobj, at the tail of the nth chunk
4086 * The chunk size is set equal to bigobj block size so that
4087 * dmu_assign_arcbuf() can be tested for object updates.
4091 * Read the directory info. If it's the first time, set things up.
4093 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4094 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
4097 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4098 umem_free(od, size);
4102 bigobj = od[0].od_object;
4103 packobj = od[1].od_object;
4104 blocksize = od[0].od_blocksize;
4105 chunksize = blocksize;
4106 ASSERT(chunksize == od[1].od_gen);
4108 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4109 VERIFY(ISP2(doi.doi_data_block_size));
4110 VERIFY(chunksize == doi.doi_data_block_size);
4111 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4114 * Pick a random index and compute the offsets into packobj and bigobj.
4116 n = ztest_random(regions) * stride + ztest_random(width);
4117 s = 1 + ztest_random(width - 1);
4119 packoff = n * sizeof (bufwad_t);
4120 packsize = s * sizeof (bufwad_t);
4122 bigoff = n * chunksize;
4123 bigsize = s * chunksize;
4125 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4126 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4128 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4130 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4133 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4134 * Iteration 1 test zcopy to already referenced dbufs.
4135 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4136 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4137 * Iteration 4 test zcopy when dbuf is no longer dirty.
4138 * Iteration 5 test zcopy when it can't be done.
4139 * Iteration 6 one more zcopy write.
4141 for (i = 0; i < 7; i++) {
4146 * In iteration 5 (i == 5) use arcbufs
4147 * that don't match bigobj blksz to test
4148 * dmu_assign_arcbuf() when it can't directly
4149 * assign an arcbuf to a dbuf.
4151 for (j = 0; j < s; j++) {
4152 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4154 dmu_request_arcbuf(bonus_db, chunksize);
4156 bigbuf_arcbufs[2 * j] =
4157 dmu_request_arcbuf(bonus_db, chunksize / 2);
4158 bigbuf_arcbufs[2 * j + 1] =
4159 dmu_request_arcbuf(bonus_db, chunksize / 2);
4164 * Get a tx for the mods to both packobj and bigobj.
4166 tx = dmu_tx_create(os);
4168 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4169 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4171 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4173 umem_free(packbuf, packsize);
4174 umem_free(bigbuf, bigsize);
4175 for (j = 0; j < s; j++) {
4177 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4178 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4181 bigbuf_arcbufs[2 * j]);
4183 bigbuf_arcbufs[2 * j + 1]);
4186 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4187 umem_free(od, size);
4188 dmu_buf_rele(bonus_db, FTAG);
4193 * 50% of the time don't read objects in the 1st iteration to
4194 * test dmu_assign_arcbuf() for the case when there're no
4195 * existing dbufs for the specified offsets.
4197 if (i != 0 || ztest_random(2) != 0) {
4198 error = dmu_read(os, packobj, packoff,
4199 packsize, packbuf, DMU_READ_PREFETCH);
4201 error = dmu_read(os, bigobj, bigoff, bigsize,
4202 bigbuf, DMU_READ_PREFETCH);
4205 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4209 * We've verified all the old bufwads, and made new ones.
4210 * Now write them out.
4212 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4213 if (ztest_opts.zo_verbose >= 7) {
4214 (void) printf("writing offset %llx size %llx"
4216 (u_longlong_t)bigoff,
4217 (u_longlong_t)bigsize,
4220 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4222 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4223 bcopy((caddr_t)bigbuf + (off - bigoff),
4224 bigbuf_arcbufs[j]->b_data, chunksize);
4226 bcopy((caddr_t)bigbuf + (off - bigoff),
4227 bigbuf_arcbufs[2 * j]->b_data,
4229 bcopy((caddr_t)bigbuf + (off - bigoff) +
4231 bigbuf_arcbufs[2 * j + 1]->b_data,
4236 VERIFY(dmu_buf_hold(os, bigobj, off,
4237 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4239 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4240 dmu_assign_arcbuf(bonus_db, off,
4241 bigbuf_arcbufs[j], tx);
4243 dmu_assign_arcbuf(bonus_db, off,
4244 bigbuf_arcbufs[2 * j], tx);
4245 dmu_assign_arcbuf(bonus_db,
4246 off + chunksize / 2,
4247 bigbuf_arcbufs[2 * j + 1], tx);
4250 dmu_buf_rele(dbt, FTAG);
4256 * Sanity check the stuff we just wrote.
4259 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4260 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4262 VERIFY(0 == dmu_read(os, packobj, packoff,
4263 packsize, packcheck, DMU_READ_PREFETCH));
4264 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4265 bigsize, bigcheck, DMU_READ_PREFETCH));
4267 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4268 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4270 umem_free(packcheck, packsize);
4271 umem_free(bigcheck, bigsize);
4274 txg_wait_open(dmu_objset_pool(os), 0);
4275 } else if (i == 3) {
4276 txg_wait_synced(dmu_objset_pool(os), 0);
4280 dmu_buf_rele(bonus_db, FTAG);
4281 umem_free(packbuf, packsize);
4282 umem_free(bigbuf, bigsize);
4283 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4284 umem_free(od, size);
4289 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4293 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4294 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4295 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4298 * Have multiple threads write to large offsets in an object
4299 * to verify that parallel writes to an object -- even to the
4300 * same blocks within the object -- doesn't cause any trouble.
4302 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4304 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4307 while (ztest_random(10) != 0)
4308 ztest_io(zd, od->od_object, offset);
4310 umem_free(od, sizeof (ztest_od_t));
4314 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4317 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4318 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4319 uint64_t count = ztest_random(20) + 1;
4320 uint64_t blocksize = ztest_random_blocksize();
4323 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4325 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4327 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4328 !ztest_random(2)) != 0) {
4329 umem_free(od, sizeof (ztest_od_t));
4333 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4334 umem_free(od, sizeof (ztest_od_t));
4338 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4340 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4342 while (ztest_random(count) != 0) {
4343 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4344 if (ztest_write(zd, od->od_object, randoff, blocksize,
4347 while (ztest_random(4) != 0)
4348 ztest_io(zd, od->od_object, randoff);
4351 umem_free(data, blocksize);
4352 umem_free(od, sizeof (ztest_od_t));
4356 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4358 #define ZTEST_ZAP_MIN_INTS 1
4359 #define ZTEST_ZAP_MAX_INTS 4
4360 #define ZTEST_ZAP_MAX_PROPS 1000
4363 ztest_zap(ztest_ds_t *zd, uint64_t id)
4365 objset_t *os = zd->zd_os;
4368 uint64_t txg, last_txg;
4369 uint64_t value[ZTEST_ZAP_MAX_INTS];
4370 uint64_t zl_ints, zl_intsize, prop;
4373 char propname[100], txgname[100];
4375 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4377 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4378 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4380 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4381 !ztest_random(2)) != 0)
4384 object = od->od_object;
4387 * Generate a known hash collision, and verify that
4388 * we can lookup and remove both entries.
4390 tx = dmu_tx_create(os);
4391 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4392 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4395 for (i = 0; i < 2; i++) {
4397 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4400 for (i = 0; i < 2; i++) {
4401 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4402 sizeof (uint64_t), 1, &value[i], tx));
4404 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4405 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4406 ASSERT3U(zl_ints, ==, 1);
4408 for (i = 0; i < 2; i++) {
4409 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4414 * Generate a buch of random entries.
4416 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4418 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4419 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4420 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4421 bzero(value, sizeof (value));
4425 * If these zap entries already exist, validate their contents.
4427 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4429 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4430 ASSERT3U(zl_ints, ==, 1);
4432 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4433 zl_ints, &last_txg) == 0);
4435 VERIFY(zap_length(os, object, propname, &zl_intsize,
4438 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4439 ASSERT3U(zl_ints, ==, ints);
4441 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4442 zl_ints, value) == 0);
4444 for (i = 0; i < ints; i++) {
4445 ASSERT3U(value[i], ==, last_txg + object + i);
4448 ASSERT3U(error, ==, ENOENT);
4452 * Atomically update two entries in our zap object.
4453 * The first is named txg_%llu, and contains the txg
4454 * in which the property was last updated. The second
4455 * is named prop_%llu, and the nth element of its value
4456 * should be txg + object + n.
4458 tx = dmu_tx_create(os);
4459 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4460 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4465 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4467 for (i = 0; i < ints; i++)
4468 value[i] = txg + object + i;
4470 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4472 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4478 * Remove a random pair of entries.
4480 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4481 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4482 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4484 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4486 if (error == ENOENT)
4491 tx = dmu_tx_create(os);
4492 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4493 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4496 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4497 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4500 umem_free(od, sizeof (ztest_od_t));
4504 * Testcase to test the upgrading of a microzap to fatzap.
4507 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4509 objset_t *os = zd->zd_os;
4511 uint64_t object, txg;
4514 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4515 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4517 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4518 !ztest_random(2)) != 0)
4520 object = od->od_object;
4523 * Add entries to this ZAP and make sure it spills over
4524 * and gets upgraded to a fatzap. Also, since we are adding
4525 * 2050 entries we should see ptrtbl growth and leaf-block split.
4527 for (i = 0; i < 2050; i++) {
4528 char name[MAXNAMELEN];
4533 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4534 (u_longlong_t)id, (u_longlong_t)value);
4536 tx = dmu_tx_create(os);
4537 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4538 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4541 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4543 ASSERT(error == 0 || error == EEXIST);
4547 umem_free(od, sizeof (ztest_od_t));
4552 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4554 objset_t *os = zd->zd_os;
4556 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4558 int i, namelen, error;
4559 int micro = ztest_random(2);
4560 char name[20], string_value[20];
4563 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4564 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4566 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4567 umem_free(od, sizeof (ztest_od_t));
4571 object = od->od_object;
4574 * Generate a random name of the form 'xxx.....' where each
4575 * x is a random printable character and the dots are dots.
4576 * There are 94 such characters, and the name length goes from
4577 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4579 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4581 for (i = 0; i < 3; i++)
4582 name[i] = '!' + ztest_random('~' - '!' + 1);
4583 for (; i < namelen - 1; i++)
4587 if ((namelen & 1) || micro) {
4588 wsize = sizeof (txg);
4594 data = string_value;
4598 VERIFY0(zap_count(os, object, &count));
4599 ASSERT(count != -1ULL);
4602 * Select an operation: length, lookup, add, update, remove.
4604 i = ztest_random(5);
4607 tx = dmu_tx_create(os);
4608 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4609 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4612 bcopy(name, string_value, namelen);
4616 bzero(string_value, namelen);
4622 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4624 ASSERT3U(wsize, ==, zl_wsize);
4625 ASSERT3U(wc, ==, zl_wc);
4627 ASSERT3U(error, ==, ENOENT);
4632 error = zap_lookup(os, object, name, wsize, wc, data);
4634 if (data == string_value &&
4635 bcmp(name, data, namelen) != 0)
4636 fatal(0, "name '%s' != val '%s' len %d",
4637 name, data, namelen);
4639 ASSERT3U(error, ==, ENOENT);
4644 error = zap_add(os, object, name, wsize, wc, data, tx);
4645 ASSERT(error == 0 || error == EEXIST);
4649 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4653 error = zap_remove(os, object, name, tx);
4654 ASSERT(error == 0 || error == ENOENT);
4661 umem_free(od, sizeof (ztest_od_t));
4665 * Commit callback data.
4667 typedef struct ztest_cb_data {
4668 list_node_t zcd_node;
4670 int zcd_expected_err;
4671 boolean_t zcd_added;
4672 boolean_t zcd_called;
4676 /* This is the actual commit callback function */
4678 ztest_commit_callback(void *arg, int error)
4680 ztest_cb_data_t *data = arg;
4681 uint64_t synced_txg;
4683 VERIFY(data != NULL);
4684 VERIFY3S(data->zcd_expected_err, ==, error);
4685 VERIFY(!data->zcd_called);
4687 synced_txg = spa_last_synced_txg(data->zcd_spa);
4688 if (data->zcd_txg > synced_txg)
4689 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4690 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4693 data->zcd_called = B_TRUE;
4695 if (error == ECANCELED) {
4696 ASSERT0(data->zcd_txg);
4697 ASSERT(!data->zcd_added);
4700 * The private callback data should be destroyed here, but
4701 * since we are going to check the zcd_called field after
4702 * dmu_tx_abort(), we will destroy it there.
4707 ASSERT(data->zcd_added);
4708 ASSERT3U(data->zcd_txg, !=, 0);
4710 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4712 /* See if this cb was called more quickly */
4713 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4714 zc_min_txg_delay = synced_txg - data->zcd_txg;
4716 /* Remove our callback from the list */
4717 list_remove(&zcl.zcl_callbacks, data);
4719 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4721 umem_free(data, sizeof (ztest_cb_data_t));
4724 /* Allocate and initialize callback data structure */
4725 static ztest_cb_data_t *
4726 ztest_create_cb_data(objset_t *os, uint64_t txg)
4728 ztest_cb_data_t *cb_data;
4730 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4732 cb_data->zcd_txg = txg;
4733 cb_data->zcd_spa = dmu_objset_spa(os);
4734 list_link_init(&cb_data->zcd_node);
4740 * Commit callback test.
4743 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4745 objset_t *os = zd->zd_os;
4748 ztest_cb_data_t *cb_data[3], *tmp_cb;
4749 uint64_t old_txg, txg;
4752 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4753 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4755 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4756 umem_free(od, sizeof (ztest_od_t));
4760 tx = dmu_tx_create(os);
4762 cb_data[0] = ztest_create_cb_data(os, 0);
4763 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4765 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4767 /* Every once in a while, abort the transaction on purpose */
4768 if (ztest_random(100) == 0)
4772 error = dmu_tx_assign(tx, TXG_NOWAIT);
4774 txg = error ? 0 : dmu_tx_get_txg(tx);
4776 cb_data[0]->zcd_txg = txg;
4777 cb_data[1] = ztest_create_cb_data(os, txg);
4778 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4782 * It's not a strict requirement to call the registered
4783 * callbacks from inside dmu_tx_abort(), but that's what
4784 * it's supposed to happen in the current implementation
4785 * so we will check for that.
4787 for (i = 0; i < 2; i++) {
4788 cb_data[i]->zcd_expected_err = ECANCELED;
4789 VERIFY(!cb_data[i]->zcd_called);
4794 for (i = 0; i < 2; i++) {
4795 VERIFY(cb_data[i]->zcd_called);
4796 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4799 umem_free(od, sizeof (ztest_od_t));
4803 cb_data[2] = ztest_create_cb_data(os, txg);
4804 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4807 * Read existing data to make sure there isn't a future leak.
4809 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4810 &old_txg, DMU_READ_PREFETCH));
4813 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4816 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4818 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4821 * Since commit callbacks don't have any ordering requirement and since
4822 * it is theoretically possible for a commit callback to be called
4823 * after an arbitrary amount of time has elapsed since its txg has been
4824 * synced, it is difficult to reliably determine whether a commit
4825 * callback hasn't been called due to high load or due to a flawed
4828 * In practice, we will assume that if after a certain number of txgs a
4829 * commit callback hasn't been called, then most likely there's an
4830 * implementation bug..
4832 tmp_cb = list_head(&zcl.zcl_callbacks);
4833 if (tmp_cb != NULL &&
4834 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4835 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4836 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4840 * Let's find the place to insert our callbacks.
4842 * Even though the list is ordered by txg, it is possible for the
4843 * insertion point to not be the end because our txg may already be
4844 * quiescing at this point and other callbacks in the open txg
4845 * (from other objsets) may have sneaked in.
4847 tmp_cb = list_tail(&zcl.zcl_callbacks);
4848 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4849 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4851 /* Add the 3 callbacks to the list */
4852 for (i = 0; i < 3; i++) {
4854 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4856 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4859 cb_data[i]->zcd_added = B_TRUE;
4860 VERIFY(!cb_data[i]->zcd_called);
4862 tmp_cb = cb_data[i];
4867 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4871 umem_free(od, sizeof (ztest_od_t));
4876 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4878 zfs_prop_t proplist[] = {
4880 ZFS_PROP_COMPRESSION,
4886 (void) rw_rdlock(&ztest_name_lock);
4888 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4889 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4890 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4892 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
4893 ztest_random_blocksize(), (int)ztest_random(2)));
4895 (void) rw_unlock(&ztest_name_lock);
4900 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4902 nvlist_t *props = NULL;
4904 (void) rw_rdlock(&ztest_name_lock);
4906 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4907 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4909 VERIFY0(spa_prop_get(ztest_spa, &props));
4911 if (ztest_opts.zo_verbose >= 6)
4912 dump_nvlist(props, 4);
4916 (void) rw_unlock(&ztest_name_lock);
4920 user_release_one(const char *snapname, const char *holdname)
4922 nvlist_t *snaps, *holds;
4925 snaps = fnvlist_alloc();
4926 holds = fnvlist_alloc();
4927 fnvlist_add_boolean(holds, holdname);
4928 fnvlist_add_nvlist(snaps, snapname, holds);
4929 fnvlist_free(holds);
4930 error = dsl_dataset_user_release(snaps, NULL);
4931 fnvlist_free(snaps);
4936 * Test snapshot hold/release and deferred destroy.
4939 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4942 objset_t *os = zd->zd_os;
4946 char clonename[100];
4948 char osname[MAXNAMELEN];
4951 (void) rw_rdlock(&ztest_name_lock);
4953 dmu_objset_name(os, osname);
4955 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4957 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4958 (void) snprintf(clonename, sizeof (clonename),
4959 "%s/ch1_%llu", osname, (u_longlong_t)id);
4960 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4963 * Clean up from any previous run.
4965 error = dsl_destroy_head(clonename);
4966 if (error != ENOENT)
4968 error = user_release_one(fullname, tag);
4969 if (error != ESRCH && error != ENOENT)
4971 error = dsl_destroy_snapshot(fullname, B_FALSE);
4972 if (error != ENOENT)
4976 * Create snapshot, clone it, mark snap for deferred destroy,
4977 * destroy clone, verify snap was also destroyed.
4979 error = dmu_objset_snapshot_one(osname, snapname);
4981 if (error == ENOSPC) {
4982 ztest_record_enospc("dmu_objset_snapshot");
4985 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4988 error = dmu_objset_clone(clonename, fullname);
4990 if (error == ENOSPC) {
4991 ztest_record_enospc("dmu_objset_clone");
4994 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4997 error = dsl_destroy_snapshot(fullname, B_TRUE);
4999 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5003 error = dsl_destroy_head(clonename);
5005 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5007 error = dmu_objset_hold(fullname, FTAG, &origin);
5008 if (error != ENOENT)
5009 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5012 * Create snapshot, add temporary hold, verify that we can't
5013 * destroy a held snapshot, mark for deferred destroy,
5014 * release hold, verify snapshot was destroyed.
5016 error = dmu_objset_snapshot_one(osname, snapname);
5018 if (error == ENOSPC) {
5019 ztest_record_enospc("dmu_objset_snapshot");
5022 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5025 holds = fnvlist_alloc();
5026 fnvlist_add_string(holds, fullname, tag);
5027 error = dsl_dataset_user_hold(holds, 0, NULL);
5028 fnvlist_free(holds);
5030 if (error == ENOSPC) {
5031 ztest_record_enospc("dsl_dataset_user_hold");
5034 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5035 fullname, tag, error);
5038 error = dsl_destroy_snapshot(fullname, B_FALSE);
5039 if (error != EBUSY) {
5040 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5044 error = dsl_destroy_snapshot(fullname, B_TRUE);
5046 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5050 error = user_release_one(fullname, tag);
5052 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5054 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5057 (void) rw_unlock(&ztest_name_lock);
5061 * Inject random faults into the on-disk data.
5065 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5067 ztest_shared_t *zs = ztest_shared;
5068 spa_t *spa = ztest_spa;
5072 uint64_t bad = 0x1990c0ffeedecadeull;
5077 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
5083 boolean_t islog = B_FALSE;
5085 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5086 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5088 mutex_enter(&ztest_vdev_lock);
5089 maxfaults = MAXFAULTS();
5090 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5091 mirror_save = zs->zs_mirrors;
5092 mutex_exit(&ztest_vdev_lock);
5094 ASSERT(leaves >= 1);
5097 * Grab the name lock as reader. There are some operations
5098 * which don't like to have their vdevs changed while
5099 * they are in progress (i.e. spa_change_guid). Those
5100 * operations will have grabbed the name lock as writer.
5102 (void) rw_rdlock(&ztest_name_lock);
5105 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5107 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5109 if (ztest_random(2) == 0) {
5111 * Inject errors on a normal data device or slog device.
5113 top = ztest_random_vdev_top(spa, B_TRUE);
5114 leaf = ztest_random(leaves) + zs->zs_splits;
5117 * Generate paths to the first leaf in this top-level vdev,
5118 * and to the random leaf we selected. We'll induce transient
5119 * write failures and random online/offline activity on leaf 0,
5120 * and we'll write random garbage to the randomly chosen leaf.
5122 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5123 ztest_opts.zo_dir, ztest_opts.zo_pool,
5124 top * leaves + zs->zs_splits);
5125 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5126 ztest_opts.zo_dir, ztest_opts.zo_pool,
5127 top * leaves + leaf);
5129 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5130 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5134 * If the top-level vdev needs to be resilvered
5135 * then we only allow faults on the device that is
5138 if (vd0 != NULL && maxfaults != 1 &&
5139 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5140 vd0->vdev_resilver_txg != 0)) {
5142 * Make vd0 explicitly claim to be unreadable,
5143 * or unwriteable, or reach behind its back
5144 * and close the underlying fd. We can do this if
5145 * maxfaults == 0 because we'll fail and reexecute,
5146 * and we can do it if maxfaults >= 2 because we'll
5147 * have enough redundancy. If maxfaults == 1, the
5148 * combination of this with injection of random data
5149 * corruption below exceeds the pool's fault tolerance.
5151 vdev_file_t *vf = vd0->vdev_tsd;
5153 if (vf != NULL && ztest_random(3) == 0) {
5154 (void) close(vf->vf_vnode->v_fd);
5155 vf->vf_vnode->v_fd = -1;
5156 } else if (ztest_random(2) == 0) {
5157 vd0->vdev_cant_read = B_TRUE;
5159 vd0->vdev_cant_write = B_TRUE;
5161 guid0 = vd0->vdev_guid;
5165 * Inject errors on an l2cache device.
5167 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5169 if (sav->sav_count == 0) {
5170 spa_config_exit(spa, SCL_STATE, FTAG);
5171 (void) rw_unlock(&ztest_name_lock);
5174 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5175 guid0 = vd0->vdev_guid;
5176 (void) strcpy(path0, vd0->vdev_path);
5177 (void) strcpy(pathrand, vd0->vdev_path);
5181 maxfaults = INT_MAX; /* no limit on cache devices */
5184 spa_config_exit(spa, SCL_STATE, FTAG);
5185 (void) rw_unlock(&ztest_name_lock);
5188 * If we can tolerate two or more faults, or we're dealing
5189 * with a slog, randomly online/offline vd0.
5191 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5192 if (ztest_random(10) < 6) {
5193 int flags = (ztest_random(2) == 0 ?
5194 ZFS_OFFLINE_TEMPORARY : 0);
5197 * We have to grab the zs_name_lock as writer to
5198 * prevent a race between offlining a slog and
5199 * destroying a dataset. Offlining the slog will
5200 * grab a reference on the dataset which may cause
5201 * dsl_destroy_head() to fail with EBUSY thus
5202 * leaving the dataset in an inconsistent state.
5205 (void) rw_wrlock(&ztest_name_lock);
5207 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5210 (void) rw_unlock(&ztest_name_lock);
5213 * Ideally we would like to be able to randomly
5214 * call vdev_[on|off]line without holding locks
5215 * to force unpredictable failures but the side
5216 * effects of vdev_[on|off]line prevent us from
5217 * doing so. We grab the ztest_vdev_lock here to
5218 * prevent a race between injection testing and
5221 mutex_enter(&ztest_vdev_lock);
5222 (void) vdev_online(spa, guid0, 0, NULL);
5223 mutex_exit(&ztest_vdev_lock);
5231 * We have at least single-fault tolerance, so inject data corruption.
5233 fd = open(pathrand, O_RDWR);
5235 if (fd == -1) /* we hit a gap in the device namespace */
5238 fsize = lseek(fd, 0, SEEK_END);
5240 while (--iters != 0) {
5242 * The offset must be chosen carefully to ensure that
5243 * we do not inject a given logical block with errors
5244 * on two different leaf devices, because ZFS can not
5245 * tolerate that (if maxfaults==1).
5247 * We divide each leaf into chunks of size
5248 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5249 * there is a series of ranges to which we can inject errors.
5250 * Each range can accept errors on only a single leaf vdev.
5251 * The error injection ranges are separated by ranges
5252 * which we will not inject errors on any device (DMZs).
5253 * Each DMZ must be large enough such that a single block
5254 * can not straddle it, so that a single block can not be
5255 * a target in two different injection ranges (on different
5258 * For example, with 3 leaves, each chunk looks like:
5259 * 0 to 32M: injection range for leaf 0
5260 * 32M to 64M: DMZ - no injection allowed
5261 * 64M to 96M: injection range for leaf 1
5262 * 96M to 128M: DMZ - no injection allowed
5263 * 128M to 160M: injection range for leaf 2
5264 * 160M to 192M: DMZ - no injection allowed
5266 offset = ztest_random(fsize / (leaves << bshift)) *
5267 (leaves << bshift) + (leaf << bshift) +
5268 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5270 if (offset >= fsize)
5273 mutex_enter(&ztest_vdev_lock);
5274 if (mirror_save != zs->zs_mirrors) {
5275 mutex_exit(&ztest_vdev_lock);
5280 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5281 fatal(1, "can't inject bad word at 0x%llx in %s",
5284 mutex_exit(&ztest_vdev_lock);
5286 if (ztest_opts.zo_verbose >= 7)
5287 (void) printf("injected bad word into %s,"
5288 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5293 umem_free(path0, MAXPATHLEN);
5294 umem_free(pathrand, MAXPATHLEN);
5298 * Verify that DDT repair works as expected.
5301 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5303 ztest_shared_t *zs = ztest_shared;
5304 spa_t *spa = ztest_spa;
5305 objset_t *os = zd->zd_os;
5307 uint64_t object, blocksize, txg, pattern, psize;
5308 enum zio_checksum checksum = spa_dedup_checksum(spa);
5313 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5316 blocksize = ztest_random_blocksize();
5317 blocksize = MIN(blocksize, 2048); /* because we write so many */
5319 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5320 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5322 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5323 umem_free(od, sizeof (ztest_od_t));
5328 * Take the name lock as writer to prevent anyone else from changing
5329 * the pool and dataset properies we need to maintain during this test.
5331 (void) rw_wrlock(&ztest_name_lock);
5333 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5335 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5337 (void) rw_unlock(&ztest_name_lock);
5338 umem_free(od, sizeof (ztest_od_t));
5342 object = od[0].od_object;
5343 blocksize = od[0].od_blocksize;
5344 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5346 ASSERT(object != 0);
5348 tx = dmu_tx_create(os);
5349 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5350 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5352 (void) rw_unlock(&ztest_name_lock);
5353 umem_free(od, sizeof (ztest_od_t));
5358 * Write all the copies of our block.
5360 for (i = 0; i < copies; i++) {
5361 uint64_t offset = i * blocksize;
5362 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5363 DMU_READ_NO_PREFETCH);
5365 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5366 os, (long long)object, (long long) offset, error);
5368 ASSERT(db->db_offset == offset);
5369 ASSERT(db->db_size == blocksize);
5370 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5371 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5372 dmu_buf_will_fill(db, tx);
5373 ztest_pattern_set(db->db_data, db->db_size, pattern);
5374 dmu_buf_rele(db, FTAG);
5378 txg_wait_synced(spa_get_dsl(spa), txg);
5381 * Find out what block we got.
5383 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5384 DMU_READ_NO_PREFETCH));
5385 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5386 dmu_buf_rele(db, FTAG);
5389 * Damage the block. Dedup-ditto will save us when we read it later.
5391 psize = BP_GET_PSIZE(&blk);
5392 buf = zio_buf_alloc(psize);
5393 ztest_pattern_set(buf, psize, ~pattern);
5395 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5396 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5397 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5399 zio_buf_free(buf, psize);
5401 (void) rw_unlock(&ztest_name_lock);
5402 umem_free(od, sizeof (ztest_od_t));
5410 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5412 spa_t *spa = ztest_spa;
5414 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5415 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5416 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5420 * Change the guid for the pool.
5424 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5426 spa_t *spa = ztest_spa;
5427 uint64_t orig, load;
5430 orig = spa_guid(spa);
5431 load = spa_load_guid(spa);
5433 (void) rw_wrlock(&ztest_name_lock);
5434 error = spa_change_guid(spa);
5435 (void) rw_unlock(&ztest_name_lock);
5440 if (ztest_opts.zo_verbose >= 4) {
5441 (void) printf("Changed guid old %llu -> %llu\n",
5442 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5445 VERIFY3U(orig, !=, spa_guid(spa));
5446 VERIFY3U(load, ==, spa_load_guid(spa));
5450 * Rename the pool to a different name and then rename it back.
5454 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5456 char *oldname, *newname;
5459 (void) rw_wrlock(&ztest_name_lock);
5461 oldname = ztest_opts.zo_pool;
5462 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5463 (void) strcpy(newname, oldname);
5464 (void) strcat(newname, "_tmp");
5469 VERIFY3U(0, ==, spa_rename(oldname, newname));
5472 * Try to open it under the old name, which shouldn't exist
5474 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5477 * Open it under the new name and make sure it's still the same spa_t.
5479 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5481 ASSERT(spa == ztest_spa);
5482 spa_close(spa, FTAG);
5485 * Rename it back to the original
5487 VERIFY3U(0, ==, spa_rename(newname, oldname));
5490 * Make sure it can still be opened
5492 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5494 ASSERT(spa == ztest_spa);
5495 spa_close(spa, FTAG);
5497 umem_free(newname, strlen(newname) + 1);
5499 (void) rw_unlock(&ztest_name_lock);
5503 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5505 hrtime_t end = gethrtime() + NANOSEC;
5507 while (gethrtime() <= end) {
5508 int run_count = 100;
5514 zio_cksum_t zc_ref_byteswap;
5516 size = ztest_random_blocksize();
5517 buf = umem_alloc(size, UMEM_NOFAIL);
5519 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5520 *ptr = ztest_random(UINT_MAX);
5522 VERIFY0(fletcher_4_impl_set("scalar"));
5523 fletcher_4_native(buf, size, &zc_ref);
5524 fletcher_4_byteswap(buf, size, &zc_ref_byteswap);
5526 VERIFY0(fletcher_4_impl_set("cycle"));
5527 while (run_count-- > 0) {
5529 zio_cksum_t zc_byteswap;
5531 fletcher_4_byteswap(buf, size, &zc_byteswap);
5532 fletcher_4_native(buf, size, &zc);
5534 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5535 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5536 sizeof (zc_byteswap)));
5539 umem_free(buf, size);
5544 ztest_check_path(char *path)
5547 /* return true on success */
5548 return (!stat(path, &s));
5552 ztest_get_zdb_bin(char *bin, int len)
5556 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5557 * let popen to search through PATH.
5559 if ((zdb_path = getenv("ZDB_PATH"))) {
5560 strlcpy(bin, zdb_path, len); /* In env */
5561 if (!ztest_check_path(bin)) {
5562 ztest_dump_core = 0;
5563 fatal(1, "invalid ZDB_PATH '%s'", bin);
5568 VERIFY(realpath(getexecname(), bin) != NULL);
5569 if (strstr(bin, "/ztest/")) {
5570 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5571 strcat(bin, "/zdb/zdb");
5572 if (ztest_check_path(bin))
5579 * Verify pool integrity by running zdb.
5582 ztest_run_zdb(char *pool)
5588 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5591 bin = umem_alloc(len, UMEM_NOFAIL);
5592 zdb = umem_alloc(len, UMEM_NOFAIL);
5593 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5595 ztest_get_zdb_bin(bin, len);
5598 "%s -bcc%s%s -d -U %s %s",
5600 ztest_opts.zo_verbose >= 3 ? "s" : "",
5601 ztest_opts.zo_verbose >= 4 ? "v" : "",
5605 if (ztest_opts.zo_verbose >= 5)
5606 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5608 fp = popen(zdb, "r");
5610 while (fgets(zbuf, 1024, fp) != NULL)
5611 if (ztest_opts.zo_verbose >= 3)
5612 (void) printf("%s", zbuf);
5614 status = pclose(fp);
5619 ztest_dump_core = 0;
5620 if (WIFEXITED(status))
5621 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5623 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5625 umem_free(bin, len);
5626 umem_free(zdb, len);
5627 umem_free(zbuf, 1024);
5631 ztest_walk_pool_directory(char *header)
5635 if (ztest_opts.zo_verbose >= 6)
5636 (void) printf("%s\n", header);
5638 mutex_enter(&spa_namespace_lock);
5639 while ((spa = spa_next(spa)) != NULL)
5640 if (ztest_opts.zo_verbose >= 6)
5641 (void) printf("\t%s\n", spa_name(spa));
5642 mutex_exit(&spa_namespace_lock);
5646 ztest_spa_import_export(char *oldname, char *newname)
5648 nvlist_t *config, *newconfig;
5653 if (ztest_opts.zo_verbose >= 4) {
5654 (void) printf("import/export: old = %s, new = %s\n",
5659 * Clean up from previous runs.
5661 (void) spa_destroy(newname);
5664 * Get the pool's configuration and guid.
5666 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5669 * Kick off a scrub to tickle scrub/export races.
5671 if (ztest_random(2) == 0)
5672 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5674 pool_guid = spa_guid(spa);
5675 spa_close(spa, FTAG);
5677 ztest_walk_pool_directory("pools before export");
5682 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5684 ztest_walk_pool_directory("pools after export");
5689 newconfig = spa_tryimport(config);
5690 ASSERT(newconfig != NULL);
5691 nvlist_free(newconfig);
5694 * Import it under the new name.
5696 error = spa_import(newname, config, NULL, 0);
5698 dump_nvlist(config, 0);
5699 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5700 oldname, newname, error);
5703 ztest_walk_pool_directory("pools after import");
5706 * Try to import it again -- should fail with EEXIST.
5708 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5711 * Try to import it under a different name -- should fail with EEXIST.
5713 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5716 * Verify that the pool is no longer visible under the old name.
5718 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5721 * Verify that we can open and close the pool using the new name.
5723 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5724 ASSERT(pool_guid == spa_guid(spa));
5725 spa_close(spa, FTAG);
5727 nvlist_free(config);
5731 ztest_resume(spa_t *spa)
5733 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5734 (void) printf("resuming from suspended state\n");
5735 spa_vdev_state_enter(spa, SCL_NONE);
5736 vdev_clear(spa, NULL);
5737 (void) spa_vdev_state_exit(spa, NULL, 0);
5738 (void) zio_resume(spa);
5742 ztest_resume_thread(void *arg)
5746 while (!ztest_exiting) {
5747 if (spa_suspended(spa))
5749 (void) poll(NULL, 0, 100);
5761 ztest_deadman_alarm(int sig)
5763 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5768 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5770 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5771 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5772 hrtime_t functime = gethrtime();
5775 for (i = 0; i < zi->zi_iters; i++)
5776 zi->zi_func(zd, id);
5778 functime = gethrtime() - functime;
5780 atomic_add_64(&zc->zc_count, 1);
5781 atomic_add_64(&zc->zc_time, functime);
5783 if (ztest_opts.zo_verbose >= 4)
5784 (void) printf("%6.2f sec in %s\n",
5785 (double)functime / NANOSEC, zi->zi_funcname);
5789 ztest_thread(void *arg)
5792 uint64_t id = (uintptr_t)arg;
5793 ztest_shared_t *zs = ztest_shared;
5797 ztest_shared_callstate_t *zc;
5799 while ((now = gethrtime()) < zs->zs_thread_stop) {
5801 * See if it's time to force a crash.
5803 if (now > zs->zs_thread_kill)
5807 * If we're getting ENOSPC with some regularity, stop.
5809 if (zs->zs_enospc_count > 10)
5813 * Pick a random function to execute.
5815 rand = ztest_random(ZTEST_FUNCS);
5816 zi = &ztest_info[rand];
5817 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5818 call_next = zc->zc_next;
5820 if (now >= call_next &&
5821 atomic_cas_64(&zc->zc_next, call_next, call_next +
5822 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5823 ztest_execute(rand, zi, id);
5833 ztest_dataset_name(char *dsname, char *pool, int d)
5835 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5839 ztest_dataset_destroy(int d)
5841 char name[MAXNAMELEN];
5844 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5846 if (ztest_opts.zo_verbose >= 3)
5847 (void) printf("Destroying %s to free up space\n", name);
5850 * Cleanup any non-standard clones and snapshots. In general,
5851 * ztest thread t operates on dataset (t % zopt_datasets),
5852 * so there may be more than one thing to clean up.
5854 for (t = d; t < ztest_opts.zo_threads;
5855 t += ztest_opts.zo_datasets)
5856 ztest_dsl_dataset_cleanup(name, t);
5858 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5859 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5863 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5865 uint64_t usedobjs, dirobjs, scratch;
5868 * ZTEST_DIROBJ is the object directory for the entire dataset.
5869 * Therefore, the number of objects in use should equal the
5870 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5871 * If not, we have an object leak.
5873 * Note that we can only check this in ztest_dataset_open(),
5874 * when the open-context and syncing-context values agree.
5875 * That's because zap_count() returns the open-context value,
5876 * while dmu_objset_space() returns the rootbp fill count.
5878 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5879 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5880 ASSERT3U(dirobjs + 1, ==, usedobjs);
5884 ztest_dataset_open(int d)
5886 ztest_ds_t *zd = &ztest_ds[d];
5887 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5890 char name[MAXNAMELEN];
5893 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5895 (void) rw_rdlock(&ztest_name_lock);
5897 error = ztest_dataset_create(name);
5898 if (error == ENOSPC) {
5899 (void) rw_unlock(&ztest_name_lock);
5900 ztest_record_enospc(FTAG);
5903 ASSERT(error == 0 || error == EEXIST);
5905 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5906 (void) rw_unlock(&ztest_name_lock);
5908 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5910 zilog = zd->zd_zilog;
5912 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5913 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5914 fatal(0, "missing log records: claimed %llu < committed %llu",
5915 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5917 ztest_dataset_dirobj_verify(zd);
5919 zil_replay(os, zd, ztest_replay_vector);
5921 ztest_dataset_dirobj_verify(zd);
5923 if (ztest_opts.zo_verbose >= 6)
5924 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5926 (u_longlong_t)zilog->zl_parse_blk_count,
5927 (u_longlong_t)zilog->zl_parse_lr_count,
5928 (u_longlong_t)zilog->zl_replaying_seq);
5930 zilog = zil_open(os, ztest_get_data);
5932 if (zilog->zl_replaying_seq != 0 &&
5933 zilog->zl_replaying_seq < committed_seq)
5934 fatal(0, "missing log records: replayed %llu < committed %llu",
5935 zilog->zl_replaying_seq, committed_seq);
5941 ztest_dataset_close(int d)
5943 ztest_ds_t *zd = &ztest_ds[d];
5945 zil_close(zd->zd_zilog);
5946 dmu_objset_disown(zd->zd_os, zd);
5952 * Kick off threads to run tests on all datasets in parallel.
5955 ztest_run(ztest_shared_t *zs)
5960 kthread_t *resume_thread;
5965 ztest_exiting = B_FALSE;
5968 * Initialize parent/child shared state.
5970 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5971 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5973 zs->zs_thread_start = gethrtime();
5974 zs->zs_thread_stop =
5975 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5976 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5977 zs->zs_thread_kill = zs->zs_thread_stop;
5978 if (ztest_random(100) < ztest_opts.zo_killrate) {
5979 zs->zs_thread_kill -=
5980 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5983 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5985 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5986 offsetof(ztest_cb_data_t, zcd_node));
5991 kernel_init(FREAD | FWRITE);
5992 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5993 spa->spa_debug = B_TRUE;
5994 metaslab_preload_limit = ztest_random(20) + 1;
5997 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5998 DMU_OST_ANY, B_TRUE, FTAG, &os));
5999 zs->zs_guid = dmu_objset_fsid_guid(os);
6000 dmu_objset_disown(os, FTAG);
6002 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6005 * We don't expect the pool to suspend unless maxfaults == 0,
6006 * in which case ztest_fault_inject() temporarily takes away
6007 * the only valid replica.
6009 if (MAXFAULTS() == 0)
6010 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6012 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6015 * Create a thread to periodically resume suspended I/O.
6017 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6018 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
6019 PTHREAD_CREATE_JOINABLE)), !=, NULL);
6023 * Set a deadman alarm to abort() if we hang.
6025 signal(SIGALRM, ztest_deadman_alarm);
6026 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6030 * Verify that we can safely inquire about about any object,
6031 * whether it's allocated or not. To make it interesting,
6032 * we probe a 5-wide window around each power of two.
6033 * This hits all edge cases, including zero and the max.
6035 for (t = 0; t < 64; t++) {
6036 for (d = -5; d <= 5; d++) {
6037 error = dmu_object_info(spa->spa_meta_objset,
6038 (1ULL << t) + d, NULL);
6039 ASSERT(error == 0 || error == ENOENT ||
6045 * If we got any ENOSPC errors on the previous run, destroy something.
6047 if (zs->zs_enospc_count != 0) {
6048 int d = ztest_random(ztest_opts.zo_datasets);
6049 ztest_dataset_destroy(d);
6051 zs->zs_enospc_count = 0;
6053 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6056 if (ztest_opts.zo_verbose >= 4)
6057 (void) printf("starting main threads...\n");
6060 * Kick off all the tests that run in parallel.
6062 for (t = 0; t < ztest_opts.zo_threads; t++) {
6065 if (t < ztest_opts.zo_datasets &&
6066 ztest_dataset_open(t) != 0)
6069 VERIFY3P(thread = zk_thread_create(NULL, 0,
6070 (thread_func_t)ztest_thread,
6071 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
6072 PTHREAD_CREATE_JOINABLE), !=, NULL);
6073 tid[t] = thread->t_tid;
6077 * Wait for all of the tests to complete. We go in reverse order
6078 * so we don't close datasets while threads are still using them.
6080 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6081 thread_join(tid[t]);
6082 if (t < ztest_opts.zo_datasets)
6083 ztest_dataset_close(t);
6086 txg_wait_synced(spa_get_dsl(spa), 0);
6088 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6089 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6091 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6093 /* Kill the resume thread */
6094 ztest_exiting = B_TRUE;
6095 thread_join(resume_thread->t_tid);
6099 * Right before closing the pool, kick off a bunch of async I/O;
6100 * spa_close() should wait for it to complete.
6102 for (object = 1; object < 50; object++) {
6103 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6104 ZIO_PRIORITY_SYNC_READ);
6107 /* Verify that at least one commit cb was called in a timely fashion */
6108 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6109 VERIFY0(zc_min_txg_delay);
6111 spa_close(spa, FTAG);
6114 * Verify that we can loop over all pools.
6116 mutex_enter(&spa_namespace_lock);
6117 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6118 if (ztest_opts.zo_verbose > 3)
6119 (void) printf("spa_next: found %s\n", spa_name(spa));
6120 mutex_exit(&spa_namespace_lock);
6123 * Verify that we can export the pool and reimport it under a
6126 if (ztest_random(2) == 0) {
6127 char name[MAXNAMELEN];
6128 (void) snprintf(name, MAXNAMELEN, "%s_import",
6129 ztest_opts.zo_pool);
6130 ztest_spa_import_export(ztest_opts.zo_pool, name);
6131 ztest_spa_import_export(name, ztest_opts.zo_pool);
6136 list_destroy(&zcl.zcl_callbacks);
6137 mutex_destroy(&zcl.zcl_callbacks_lock);
6138 (void) rwlock_destroy(&ztest_name_lock);
6139 mutex_destroy(&ztest_vdev_lock);
6145 ztest_ds_t *zd = &ztest_ds[0];
6149 if (ztest_opts.zo_verbose >= 3)
6150 (void) printf("testing spa_freeze()...\n");
6152 kernel_init(FREAD | FWRITE);
6153 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6154 VERIFY3U(0, ==, ztest_dataset_open(0));
6155 spa->spa_debug = B_TRUE;
6159 * Force the first log block to be transactionally allocated.
6160 * We have to do this before we freeze the pool -- otherwise
6161 * the log chain won't be anchored.
6163 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6164 ztest_dmu_object_alloc_free(zd, 0);
6165 zil_commit(zd->zd_zilog, 0);
6168 txg_wait_synced(spa_get_dsl(spa), 0);
6171 * Freeze the pool. This stops spa_sync() from doing anything,
6172 * so that the only way to record changes from now on is the ZIL.
6177 * Because it is hard to predict how much space a write will actually
6178 * require beforehand, we leave ourselves some fudge space to write over
6181 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6184 * Run tests that generate log records but don't alter the pool config
6185 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6186 * We do a txg_wait_synced() after each iteration to force the txg
6187 * to increase well beyond the last synced value in the uberblock.
6188 * The ZIL should be OK with that.
6190 * Run a random number of times less than zo_maxloops and ensure we do
6191 * not run out of space on the pool.
6193 while (ztest_random(10) != 0 &&
6194 numloops++ < ztest_opts.zo_maxloops &&
6195 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6197 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
6198 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6199 ztest_io(zd, od.od_object,
6200 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6201 txg_wait_synced(spa_get_dsl(spa), 0);
6205 * Commit all of the changes we just generated.
6207 zil_commit(zd->zd_zilog, 0);
6208 txg_wait_synced(spa_get_dsl(spa), 0);
6211 * Close our dataset and close the pool.
6213 ztest_dataset_close(0);
6214 spa_close(spa, FTAG);
6218 * Open and close the pool and dataset to induce log replay.
6220 kernel_init(FREAD | FWRITE);
6221 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6222 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6223 VERIFY3U(0, ==, ztest_dataset_open(0));
6224 ztest_dataset_close(0);
6226 spa->spa_debug = B_TRUE;
6228 txg_wait_synced(spa_get_dsl(spa), 0);
6229 ztest_reguid(NULL, 0);
6231 spa_close(spa, FTAG);
6236 print_time(hrtime_t t, char *timebuf)
6238 hrtime_t s = t / NANOSEC;
6239 hrtime_t m = s / 60;
6240 hrtime_t h = m / 60;
6241 hrtime_t d = h / 24;
6250 (void) sprintf(timebuf,
6251 "%llud%02lluh%02llum%02llus", d, h, m, s);
6253 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6255 (void) sprintf(timebuf, "%llum%02llus", m, s);
6257 (void) sprintf(timebuf, "%llus", s);
6261 make_random_props(void)
6265 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6266 if (ztest_random(2) == 0)
6268 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6274 * Create a storage pool with the given name and initial vdev size.
6275 * Then test spa_freeze() functionality.
6278 ztest_init(ztest_shared_t *zs)
6281 nvlist_t *nvroot, *props;
6284 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6285 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6287 kernel_init(FREAD | FWRITE);
6290 * Create the storage pool.
6292 (void) spa_destroy(ztest_opts.zo_pool);
6293 ztest_shared->zs_vdev_next_leaf = 0;
6295 zs->zs_mirrors = ztest_opts.zo_mirrors;
6296 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6297 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6298 props = make_random_props();
6299 for (i = 0; i < SPA_FEATURES; i++) {
6301 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6302 spa_feature_table[i].fi_uname));
6303 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6306 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6307 nvlist_free(nvroot);
6310 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6311 zs->zs_metaslab_sz =
6312 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6313 spa_close(spa, FTAG);
6317 ztest_run_zdb(ztest_opts.zo_pool);
6321 ztest_run_zdb(ztest_opts.zo_pool);
6323 (void) rwlock_destroy(&ztest_name_lock);
6324 mutex_destroy(&ztest_vdev_lock);
6330 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6332 ztest_fd_data = mkstemp(ztest_name_data);
6333 ASSERT3S(ztest_fd_data, >=, 0);
6334 (void) unlink(ztest_name_data);
6338 shared_data_size(ztest_shared_hdr_t *hdr)
6342 size = hdr->zh_hdr_size;
6343 size += hdr->zh_opts_size;
6344 size += hdr->zh_size;
6345 size += hdr->zh_stats_size * hdr->zh_stats_count;
6346 size += hdr->zh_ds_size * hdr->zh_ds_count;
6355 ztest_shared_hdr_t *hdr;
6357 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6358 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6359 ASSERT(hdr != MAP_FAILED);
6361 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6363 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6364 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6365 hdr->zh_size = sizeof (ztest_shared_t);
6366 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6367 hdr->zh_stats_count = ZTEST_FUNCS;
6368 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6369 hdr->zh_ds_count = ztest_opts.zo_datasets;
6371 size = shared_data_size(hdr);
6372 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6374 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6381 ztest_shared_hdr_t *hdr;
6384 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6385 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6386 ASSERT(hdr != MAP_FAILED);
6388 size = shared_data_size(hdr);
6390 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6391 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6392 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6393 ASSERT(hdr != MAP_FAILED);
6394 buf = (uint8_t *)hdr;
6396 offset = hdr->zh_hdr_size;
6397 ztest_shared_opts = (void *)&buf[offset];
6398 offset += hdr->zh_opts_size;
6399 ztest_shared = (void *)&buf[offset];
6400 offset += hdr->zh_size;
6401 ztest_shared_callstate = (void *)&buf[offset];
6402 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6403 ztest_shared_ds = (void *)&buf[offset];
6407 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6411 char *cmdbuf = NULL;
6416 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6417 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6422 fatal(1, "fork failed");
6424 if (pid == 0) { /* child */
6425 char *emptyargv[2] = { cmd, NULL };
6426 char fd_data_str[12];
6428 struct rlimit rl = { 1024, 1024 };
6429 (void) setrlimit(RLIMIT_NOFILE, &rl);
6431 (void) close(ztest_fd_rand);
6432 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6433 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6435 (void) enable_extended_FILE_stdio(-1, -1);
6436 if (libpath != NULL)
6437 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6438 (void) execv(cmd, emptyargv);
6439 ztest_dump_core = B_FALSE;
6440 fatal(B_TRUE, "exec failed: %s", cmd);
6443 if (cmdbuf != NULL) {
6444 umem_free(cmdbuf, MAXPATHLEN);
6448 while (waitpid(pid, &status, 0) != pid)
6450 if (statusp != NULL)
6453 if (WIFEXITED(status)) {
6454 if (WEXITSTATUS(status) != 0) {
6455 (void) fprintf(stderr, "child exited with code %d\n",
6456 WEXITSTATUS(status));
6460 } else if (WIFSIGNALED(status)) {
6461 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6462 (void) fprintf(stderr, "child died with signal %d\n",
6468 (void) fprintf(stderr, "something strange happened to child\n");
6475 ztest_run_init(void)
6479 ztest_shared_t *zs = ztest_shared;
6481 ASSERT(ztest_opts.zo_init != 0);
6484 * Blow away any existing copy of zpool.cache
6486 (void) remove(spa_config_path);
6489 * Create and initialize our storage pool.
6491 for (i = 1; i <= ztest_opts.zo_init; i++) {
6492 bzero(zs, sizeof (ztest_shared_t));
6493 if (ztest_opts.zo_verbose >= 3 &&
6494 ztest_opts.zo_init != 1) {
6495 (void) printf("ztest_init(), pass %d\n", i);
6502 main(int argc, char **argv)
6510 ztest_shared_callstate_t *zc;
6517 char *fd_data_str = getenv("ZTEST_FD_DATA");
6518 struct sigaction action;
6520 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6522 dprintf_setup(&argc, argv);
6524 action.sa_handler = sig_handler;
6525 sigemptyset(&action.sa_mask);
6526 action.sa_flags = 0;
6528 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6529 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6534 if (sigaction(SIGABRT, &action, NULL) < 0) {
6535 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6540 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6541 ASSERT3S(ztest_fd_rand, >=, 0);
6544 process_options(argc, argv);
6549 bcopy(&ztest_opts, ztest_shared_opts,
6550 sizeof (*ztest_shared_opts));
6552 ztest_fd_data = atoi(fd_data_str);
6554 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6556 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6558 /* Override location of zpool.cache */
6559 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6560 ztest_opts.zo_dir) != -1);
6562 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6567 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6568 metaslab_df_alloc_threshold =
6569 zs->zs_metaslab_df_alloc_threshold;
6578 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6580 if (ztest_opts.zo_verbose >= 1) {
6581 (void) printf("%llu vdevs, %d datasets, %d threads,"
6582 " %llu seconds...\n",
6583 (u_longlong_t)ztest_opts.zo_vdevs,
6584 ztest_opts.zo_datasets,
6585 ztest_opts.zo_threads,
6586 (u_longlong_t)ztest_opts.zo_time);
6589 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6590 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6592 zs->zs_do_init = B_TRUE;
6593 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6594 if (ztest_opts.zo_verbose >= 1) {
6595 (void) printf("Executing older ztest for "
6596 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6598 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6599 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6601 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6603 zs->zs_do_init = B_FALSE;
6605 zs->zs_proc_start = gethrtime();
6606 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6608 for (f = 0; f < ZTEST_FUNCS; f++) {
6609 zi = &ztest_info[f];
6610 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6611 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6612 zc->zc_next = UINT64_MAX;
6614 zc->zc_next = zs->zs_proc_start +
6615 ztest_random(2 * zi->zi_interval[0] + 1);
6619 * Run the tests in a loop. These tests include fault injection
6620 * to verify that self-healing data works, and forced crashes
6621 * to verify that we never lose on-disk consistency.
6623 while (gethrtime() < zs->zs_proc_stop) {
6628 * Initialize the workload counters for each function.
6630 for (f = 0; f < ZTEST_FUNCS; f++) {
6631 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6636 /* Set the allocation switch size */
6637 zs->zs_metaslab_df_alloc_threshold =
6638 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6640 if (!hasalt || ztest_random(2) == 0) {
6641 if (hasalt && ztest_opts.zo_verbose >= 1) {
6642 (void) printf("Executing newer ztest: %s\n",
6646 killed = exec_child(cmd, NULL, B_TRUE, &status);
6648 if (hasalt && ztest_opts.zo_verbose >= 1) {
6649 (void) printf("Executing older ztest: %s\n",
6650 ztest_opts.zo_alt_ztest);
6653 killed = exec_child(ztest_opts.zo_alt_ztest,
6654 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6661 if (ztest_opts.zo_verbose >= 1) {
6662 hrtime_t now = gethrtime();
6664 now = MIN(now, zs->zs_proc_stop);
6665 print_time(zs->zs_proc_stop - now, timebuf);
6666 nicenum(zs->zs_space, numbuf);
6668 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6669 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6671 WIFEXITED(status) ? "Complete" : "SIGKILL",
6672 (u_longlong_t)zs->zs_enospc_count,
6673 100.0 * zs->zs_alloc / zs->zs_space,
6675 100.0 * (now - zs->zs_proc_start) /
6676 (ztest_opts.zo_time * NANOSEC), timebuf);
6679 if (ztest_opts.zo_verbose >= 2) {
6680 (void) printf("\nWorkload summary:\n\n");
6681 (void) printf("%7s %9s %s\n",
6682 "Calls", "Time", "Function");
6683 (void) printf("%7s %9s %s\n",
6684 "-----", "----", "--------");
6685 for (f = 0; f < ZTEST_FUNCS; f++) {
6686 zi = &ztest_info[f];
6687 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6688 print_time(zc->zc_time, timebuf);
6689 (void) printf("%7llu %9s %s\n",
6690 (u_longlong_t)zc->zc_count, timebuf,
6693 (void) printf("\n");
6697 * It's possible that we killed a child during a rename test,
6698 * in which case we'll have a 'ztest_tmp' pool lying around
6699 * instead of 'ztest'. Do a blind rename in case this happened.
6702 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6703 spa_close(spa, FTAG);
6705 char tmpname[MAXNAMELEN];
6707 kernel_init(FREAD | FWRITE);
6708 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6709 ztest_opts.zo_pool);
6710 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6714 ztest_run_zdb(ztest_opts.zo_pool);
6717 if (ztest_opts.zo_verbose >= 1) {
6719 (void) printf("%d runs of older ztest: %s\n", older,
6720 ztest_opts.zo_alt_ztest);
6721 (void) printf("%d runs of newer ztest: %s\n", newer,
6724 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6725 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6728 umem_free(cmd, MAXNAMELEN);