4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
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>
119 #include <stdio_ext.h>
126 #include <sys/fs/zfs.h>
127 #include <zfs_fletcher.h>
128 #include <libnvpair.h>
131 #include <execinfo.h> /* for backtrace() */
134 static int ztest_fd_data = -1;
135 static int ztest_fd_rand = -1;
137 typedef struct ztest_shared_hdr {
138 uint64_t zh_hdr_size;
139 uint64_t zh_opts_size;
141 uint64_t zh_stats_size;
142 uint64_t zh_stats_count;
144 uint64_t zh_ds_count;
145 } ztest_shared_hdr_t;
147 static ztest_shared_hdr_t *ztest_shared_hdr;
149 typedef struct ztest_shared_opts {
150 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
151 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
152 char zo_alt_ztest[MAXNAMELEN];
153 char zo_alt_libpath[MAXNAMELEN];
155 uint64_t zo_vdevtime;
163 uint64_t zo_passtime;
164 uint64_t zo_killrate;
168 uint64_t zo_maxloops;
169 uint64_t zo_metaslab_gang_bang;
171 } ztest_shared_opts_t;
173 static const ztest_shared_opts_t ztest_opts_defaults = {
174 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
175 .zo_dir = { '/', 't', 'm', 'p', '\0' },
176 .zo_alt_ztest = { '\0' },
177 .zo_alt_libpath = { '\0' },
179 .zo_ashift = SPA_MINBLOCKSHIFT,
182 .zo_raidz_parity = 1,
183 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
186 .zo_passtime = 60, /* 60 seconds */
187 .zo_killrate = 70, /* 70% kill rate */
191 .zo_time = 300, /* 5 minutes */
192 .zo_maxloops = 50, /* max loops during spa_freeze() */
193 .zo_metaslab_gang_bang = 32 << 10
196 extern uint64_t metaslab_gang_bang;
197 extern uint64_t metaslab_df_alloc_threshold;
198 extern int metaslab_preload_limit;
199 extern boolean_t zfs_compressed_arc_enabled;
200 extern int zfs_abd_scatter_enabled;
202 static ztest_shared_opts_t *ztest_shared_opts;
203 static ztest_shared_opts_t ztest_opts;
205 typedef struct ztest_shared_ds {
209 static ztest_shared_ds_t *ztest_shared_ds;
210 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
212 #define BT_MAGIC 0x123456789abcdefULL
213 #define MAXFAULTS() \
214 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
218 ZTEST_IO_WRITE_PATTERN,
219 ZTEST_IO_WRITE_ZEROES,
226 typedef struct ztest_block_tag {
230 uint64_t bt_dnodesize;
237 typedef struct bufwad {
255 #define ZTEST_RANGE_LOCKS 64
256 #define ZTEST_OBJECT_LOCKS 64
259 * Object descriptor. Used as a template for object lookup/create/remove.
261 typedef struct ztest_od {
264 dmu_object_type_t od_type;
265 dmu_object_type_t od_crtype;
266 uint64_t od_blocksize;
267 uint64_t od_crblocksize;
268 uint64_t od_crdnodesize;
271 char od_name[ZFS_MAX_DATASET_NAME_LEN];
277 typedef struct ztest_ds {
278 ztest_shared_ds_t *zd_shared;
280 rwlock_t zd_zilog_lock;
282 ztest_od_t *zd_od; /* debugging aid */
283 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
284 kmutex_t zd_dirobj_lock;
285 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
286 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
290 * Per-iteration state.
292 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
294 typedef struct ztest_info {
295 ztest_func_t *zi_func; /* test function */
296 uint64_t zi_iters; /* iterations per execution */
297 uint64_t *zi_interval; /* execute every <interval> seconds */
298 const char *zi_funcname; /* name of test function */
301 typedef struct ztest_shared_callstate {
302 uint64_t zc_count; /* per-pass count */
303 uint64_t zc_time; /* per-pass time */
304 uint64_t zc_next; /* next time to call this function */
305 } ztest_shared_callstate_t;
307 static ztest_shared_callstate_t *ztest_shared_callstate;
308 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
310 ztest_func_t ztest_dmu_read_write;
311 ztest_func_t ztest_dmu_write_parallel;
312 ztest_func_t ztest_dmu_object_alloc_free;
313 ztest_func_t ztest_dmu_commit_callbacks;
314 ztest_func_t ztest_zap;
315 ztest_func_t ztest_zap_parallel;
316 ztest_func_t ztest_zil_commit;
317 ztest_func_t ztest_zil_remount;
318 ztest_func_t ztest_dmu_read_write_zcopy;
319 ztest_func_t ztest_dmu_objset_create_destroy;
320 ztest_func_t ztest_dmu_prealloc;
321 ztest_func_t ztest_fzap;
322 ztest_func_t ztest_dmu_snapshot_create_destroy;
323 ztest_func_t ztest_dsl_prop_get_set;
324 ztest_func_t ztest_spa_prop_get_set;
325 ztest_func_t ztest_spa_create_destroy;
326 ztest_func_t ztest_fault_inject;
327 ztest_func_t ztest_ddt_repair;
328 ztest_func_t ztest_dmu_snapshot_hold;
329 ztest_func_t ztest_mmp_enable_disable;
330 ztest_func_t ztest_spa_rename;
331 ztest_func_t ztest_scrub;
332 ztest_func_t ztest_dsl_dataset_promote_busy;
333 ztest_func_t ztest_vdev_attach_detach;
334 ztest_func_t ztest_vdev_LUN_growth;
335 ztest_func_t ztest_vdev_add_remove;
336 ztest_func_t ztest_vdev_aux_add_remove;
337 ztest_func_t ztest_split_pool;
338 ztest_func_t ztest_reguid;
339 ztest_func_t ztest_spa_upgrade;
340 ztest_func_t ztest_fletcher;
341 ztest_func_t ztest_fletcher_incr;
342 ztest_func_t ztest_verify_dnode_bt;
344 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
345 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
346 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
347 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
348 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
350 #define ZTI_INIT(func, iters, interval) \
351 { .zi_func = (func), \
352 .zi_iters = (iters), \
353 .zi_interval = (interval), \
354 .zi_funcname = # func }
356 ztest_info_t ztest_info[] = {
357 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
358 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
359 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
360 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
361 ZTI_INIT(ztest_zap, 30, &zopt_always),
362 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
363 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
364 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
365 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
366 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
367 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
368 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
369 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
371 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
373 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
374 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
376 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
378 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
379 ZTI_INIT(ztest_mmp_enable_disable, 1, &zopt_sometimes),
380 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
381 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
382 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
383 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
384 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
385 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
386 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
387 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
388 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
389 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
390 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
391 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
394 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
397 * The following struct is used to hold a list of uncalled commit callbacks.
398 * The callbacks are ordered by txg number.
400 typedef struct ztest_cb_list {
401 kmutex_t zcl_callbacks_lock;
402 list_t zcl_callbacks;
406 * Stuff we need to share writably between parent and child.
408 typedef struct ztest_shared {
409 boolean_t zs_do_init;
410 hrtime_t zs_proc_start;
411 hrtime_t zs_proc_stop;
412 hrtime_t zs_thread_start;
413 hrtime_t zs_thread_stop;
414 hrtime_t zs_thread_kill;
415 uint64_t zs_enospc_count;
416 uint64_t zs_vdev_next_leaf;
417 uint64_t zs_vdev_aux;
422 uint64_t zs_metaslab_sz;
423 uint64_t zs_metaslab_df_alloc_threshold;
427 #define ID_PARALLEL -1ULL
429 static char ztest_dev_template[] = "%s/%s.%llua";
430 static char ztest_aux_template[] = "%s/%s.%s.%llu";
431 ztest_shared_t *ztest_shared;
433 static spa_t *ztest_spa = NULL;
434 static ztest_ds_t *ztest_ds;
436 static kmutex_t ztest_vdev_lock;
439 * The ztest_name_lock protects the pool and dataset namespace used by
440 * the individual tests. To modify the namespace, consumers must grab
441 * this lock as writer. Grabbing the lock as reader will ensure that the
442 * namespace does not change while the lock is held.
444 static rwlock_t ztest_name_lock;
446 static boolean_t ztest_dump_core = B_TRUE;
447 static boolean_t ztest_exiting;
449 /* Global commit callback list */
450 static ztest_cb_list_t zcl;
451 /* Commit cb delay */
452 static uint64_t zc_min_txg_delay = UINT64_MAX;
453 static int zc_cb_counter = 0;
456 * Minimum number of commit callbacks that need to be registered for us to check
457 * whether the minimum txg delay is acceptable.
459 #define ZTEST_COMMIT_CB_MIN_REG 100
462 * If a number of txgs equal to this threshold have been created after a commit
463 * callback has been registered but not called, then we assume there is an
464 * implementation bug.
466 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
468 extern uint64_t metaslab_gang_bang;
469 extern uint64_t metaslab_df_alloc_threshold;
472 ZTEST_META_DNODE = 0,
477 static void usage(boolean_t) __NORETURN;
480 * These libumem hooks provide a reasonable set of defaults for the allocator's
481 * debugging facilities.
484 _umem_debug_init(void)
486 return ("default,verbose"); /* $UMEM_DEBUG setting */
490 _umem_logging_init(void)
492 return ("fail,contents"); /* $UMEM_LOGGING setting */
495 #define BACKTRACE_SZ 100
497 static void sig_handler(int signo)
499 struct sigaction action;
500 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
502 void *buffer[BACKTRACE_SZ];
504 nptrs = backtrace(buffer, BACKTRACE_SZ);
505 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
509 * Restore default action and re-raise signal so SIGSEGV and
510 * SIGABRT can trigger a core dump.
512 action.sa_handler = SIG_DFL;
513 sigemptyset(&action.sa_mask);
515 (void) sigaction(signo, &action, NULL);
519 #define FATAL_MSG_SZ 1024
524 fatal(int do_perror, char *message, ...)
527 int save_errno = errno;
530 (void) fflush(stdout);
531 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
533 va_start(args, message);
534 (void) sprintf(buf, "ztest: ");
536 (void) vsprintf(buf + strlen(buf), message, args);
539 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
540 ": %s", strerror(save_errno));
542 (void) fprintf(stderr, "%s\n", buf);
543 fatal_msg = buf; /* to ease debugging */
550 str2shift(const char *buf)
552 const char *ends = "BKMGTPEZ";
557 for (i = 0; i < strlen(ends); i++) {
558 if (toupper(buf[0]) == ends[i])
561 if (i == strlen(ends)) {
562 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
566 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
569 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
575 nicenumtoull(const char *buf)
580 val = strtoull(buf, &end, 0);
582 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
584 } else if (end[0] == '.') {
585 double fval = strtod(buf, &end);
586 fval *= pow(2, str2shift(end));
587 if (fval > UINT64_MAX) {
588 (void) fprintf(stderr, "ztest: value too large: %s\n",
592 val = (uint64_t)fval;
594 int shift = str2shift(end);
595 if (shift >= 64 || (val << shift) >> shift != val) {
596 (void) fprintf(stderr, "ztest: value too large: %s\n",
606 usage(boolean_t requested)
608 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
610 char nice_vdev_size[10];
611 char nice_gang_bang[10];
612 FILE *fp = requested ? stdout : stderr;
614 nicenum(zo->zo_vdev_size, nice_vdev_size);
615 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
617 (void) fprintf(fp, "Usage: %s\n"
618 "\t[-v vdevs (default: %llu)]\n"
619 "\t[-s size_of_each_vdev (default: %s)]\n"
620 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
621 "\t[-m mirror_copies (default: %d)]\n"
622 "\t[-r raidz_disks (default: %d)]\n"
623 "\t[-R raidz_parity (default: %d)]\n"
624 "\t[-d datasets (default: %d)]\n"
625 "\t[-t threads (default: %d)]\n"
626 "\t[-g gang_block_threshold (default: %s)]\n"
627 "\t[-i init_count (default: %d)] initialize pool i times\n"
628 "\t[-k kill_percentage (default: %llu%%)]\n"
629 "\t[-p pool_name (default: %s)]\n"
630 "\t[-f dir (default: %s)] file directory for vdev files\n"
631 "\t[-M] Multi-host simulate pool imported on remote host\n"
632 "\t[-V] verbose (use multiple times for ever more blather)\n"
633 "\t[-E] use existing pool instead of creating new one\n"
634 "\t[-T time (default: %llu sec)] total run time\n"
635 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
636 "\t[-P passtime (default: %llu sec)] time per pass\n"
637 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
638 "\t[-o variable=value] ... set global variable to an unsigned\n"
639 "\t 32-bit integer value\n"
640 "\t[-h] (print help)\n"
643 (u_longlong_t)zo->zo_vdevs, /* -v */
644 nice_vdev_size, /* -s */
645 zo->zo_ashift, /* -a */
646 zo->zo_mirrors, /* -m */
647 zo->zo_raidz, /* -r */
648 zo->zo_raidz_parity, /* -R */
649 zo->zo_datasets, /* -d */
650 zo->zo_threads, /* -t */
651 nice_gang_bang, /* -g */
652 zo->zo_init, /* -i */
653 (u_longlong_t)zo->zo_killrate, /* -k */
654 zo->zo_pool, /* -p */
656 (u_longlong_t)zo->zo_time, /* -T */
657 (u_longlong_t)zo->zo_maxloops, /* -F */
658 (u_longlong_t)zo->zo_passtime);
659 exit(requested ? 0 : 1);
663 process_options(int argc, char **argv)
666 ztest_shared_opts_t *zo = &ztest_opts;
670 char altdir[MAXNAMELEN] = { 0 };
672 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
674 while ((opt = getopt(argc, argv,
675 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:")) != EOF) {
692 value = nicenumtoull(optarg);
696 zo->zo_vdevs = value;
699 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
702 zo->zo_ashift = value;
705 zo->zo_mirrors = value;
708 zo->zo_raidz = MAX(1, value);
711 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
714 zo->zo_datasets = MAX(1, value);
717 zo->zo_threads = MAX(1, value);
720 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
727 zo->zo_killrate = value;
730 (void) strlcpy(zo->zo_pool, optarg,
731 sizeof (zo->zo_pool));
734 path = realpath(optarg, NULL);
736 (void) fprintf(stderr, "error: %s: %s\n",
737 optarg, strerror(errno));
740 (void) strlcpy(zo->zo_dir, path,
741 sizeof (zo->zo_dir));
758 zo->zo_passtime = MAX(1, value);
761 zo->zo_maxloops = MAX(1, value);
764 (void) strlcpy(altdir, optarg, sizeof (altdir));
767 if (set_global_var(optarg) != 0)
780 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
783 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
786 if (strlen(altdir) > 0) {
794 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
795 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
797 VERIFY(NULL != realpath(getexecname(), cmd));
798 if (0 != access(altdir, F_OK)) {
799 ztest_dump_core = B_FALSE;
800 fatal(B_TRUE, "invalid alternate ztest path: %s",
803 VERIFY(NULL != realpath(altdir, realaltdir));
806 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
807 * We want to extract <isa> to determine if we should use
808 * 32 or 64 bit binaries.
810 bin = strstr(cmd, "/usr/bin/");
811 ztest = strstr(bin, "/ztest");
813 isalen = ztest - isa;
814 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
815 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
816 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
817 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
819 if (0 != access(zo->zo_alt_ztest, X_OK)) {
820 ztest_dump_core = B_FALSE;
821 fatal(B_TRUE, "invalid alternate ztest: %s",
823 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
824 ztest_dump_core = B_FALSE;
825 fatal(B_TRUE, "invalid alternate lib directory %s",
829 umem_free(cmd, MAXPATHLEN);
830 umem_free(realaltdir, MAXPATHLEN);
835 ztest_kill(ztest_shared_t *zs)
837 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
838 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
841 * Before we kill off ztest, make sure that the config is updated.
842 * See comment above spa_config_sync().
844 mutex_enter(&spa_namespace_lock);
845 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
846 mutex_exit(&spa_namespace_lock);
848 (void) kill(getpid(), SIGKILL);
852 ztest_random(uint64_t range)
856 ASSERT3S(ztest_fd_rand, >=, 0);
861 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
862 fatal(1, "short read from /dev/urandom");
869 ztest_record_enospc(const char *s)
871 ztest_shared->zs_enospc_count++;
875 ztest_get_ashift(void)
877 if (ztest_opts.zo_ashift == 0)
878 return (SPA_MINBLOCKSHIFT + ztest_random(5));
879 return (ztest_opts.zo_ashift);
883 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
889 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
892 ashift = ztest_get_ashift();
898 vdev = ztest_shared->zs_vdev_aux;
899 (void) snprintf(path, MAXPATHLEN,
900 ztest_aux_template, ztest_opts.zo_dir,
901 pool == NULL ? ztest_opts.zo_pool : pool,
904 vdev = ztest_shared->zs_vdev_next_leaf++;
905 (void) snprintf(path, MAXPATHLEN,
906 ztest_dev_template, ztest_opts.zo_dir,
907 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
912 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
914 fatal(1, "can't open %s", path);
915 if (ftruncate(fd, size) != 0)
916 fatal(1, "can't ftruncate %s", path);
920 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
921 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
922 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
923 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
924 umem_free(pathbuf, MAXPATHLEN);
930 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
931 uint64_t ashift, int r)
933 nvlist_t *raidz, **child;
937 return (make_vdev_file(path, aux, pool, size, ashift));
938 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
940 for (c = 0; c < r; c++)
941 child[c] = make_vdev_file(path, aux, pool, size, ashift);
943 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
944 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
945 VDEV_TYPE_RAIDZ) == 0);
946 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
947 ztest_opts.zo_raidz_parity) == 0);
948 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
951 for (c = 0; c < r; c++)
952 nvlist_free(child[c]);
954 umem_free(child, r * sizeof (nvlist_t *));
960 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
961 uint64_t ashift, int r, int m)
963 nvlist_t *mirror, **child;
967 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
969 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
971 for (c = 0; c < m; c++)
972 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
974 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
975 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
976 VDEV_TYPE_MIRROR) == 0);
977 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
980 for (c = 0; c < m; c++)
981 nvlist_free(child[c]);
983 umem_free(child, m * sizeof (nvlist_t *));
989 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
990 int log, int r, int m, int t)
992 nvlist_t *root, **child;
997 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
999 for (c = 0; c < t; c++) {
1000 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1002 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1006 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1007 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1008 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1011 for (c = 0; c < t; c++)
1012 nvlist_free(child[c]);
1014 umem_free(child, t * sizeof (nvlist_t *));
1020 * Find a random spa version. Returns back a random spa version in the
1021 * range [initial_version, SPA_VERSION_FEATURES].
1024 ztest_random_spa_version(uint64_t initial_version)
1026 uint64_t version = initial_version;
1028 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1030 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1033 if (version > SPA_VERSION_BEFORE_FEATURES)
1034 version = SPA_VERSION_FEATURES;
1036 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1041 ztest_random_blocksize(void)
1044 * Choose a block size >= the ashift.
1045 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1047 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1048 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1050 uint64_t block_shift =
1051 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1052 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1056 ztest_random_dnodesize(void)
1059 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1061 if (max_slots == DNODE_MIN_SLOTS)
1062 return (DNODE_MIN_SIZE);
1065 * Weight the random distribution more heavily toward smaller
1066 * dnode sizes since that is more likely to reflect real-world
1069 ASSERT3U(max_slots, >, 4);
1070 switch (ztest_random(10)) {
1072 slots = 5 + ztest_random(max_slots - 4);
1075 slots = 2 + ztest_random(3);
1082 return (slots << DNODE_SHIFT);
1086 ztest_random_ibshift(void)
1088 return (DN_MIN_INDBLKSHIFT +
1089 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1093 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1096 vdev_t *rvd = spa->spa_root_vdev;
1099 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1102 top = ztest_random(rvd->vdev_children);
1103 tvd = rvd->vdev_child[top];
1104 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1105 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1111 ztest_random_dsl_prop(zfs_prop_t prop)
1116 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1117 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1123 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1126 const char *propname = zfs_prop_to_name(prop);
1127 const char *valname;
1132 error = dsl_prop_set_int(osname, propname,
1133 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1135 if (error == ENOSPC) {
1136 ztest_record_enospc(FTAG);
1141 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1142 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1144 if (ztest_opts.zo_verbose >= 6) {
1147 err = zfs_prop_index_to_string(prop, curval, &valname);
1149 (void) printf("%s %s = %llu at '%s'\n", osname,
1150 propname, (unsigned long long)curval, setpoint);
1152 (void) printf("%s %s = %s at '%s'\n",
1153 osname, propname, valname, setpoint);
1155 umem_free(setpoint, MAXPATHLEN);
1161 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1163 spa_t *spa = ztest_spa;
1164 nvlist_t *props = NULL;
1167 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1168 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1170 error = spa_prop_set(spa, props);
1174 if (error == ENOSPC) {
1175 ztest_record_enospc(FTAG);
1185 * Object and range lock mechanics
1188 list_node_t z_lnode;
1189 refcount_t z_refcnt;
1191 zfs_rlock_t z_range_lock;
1196 ztest_znode_t *z_ztznode;
1199 static ztest_znode_t *
1200 ztest_znode_init(uint64_t object)
1202 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1204 list_link_init(&zp->z_lnode);
1205 refcount_create(&zp->z_refcnt);
1206 zp->z_object = object;
1207 zfs_rlock_init(&zp->z_range_lock);
1213 ztest_znode_fini(ztest_znode_t *zp)
1215 ASSERT(refcount_is_zero(&zp->z_refcnt));
1216 zfs_rlock_destroy(&zp->z_range_lock);
1218 refcount_destroy(&zp->z_refcnt);
1219 list_link_init(&zp->z_lnode);
1220 umem_free(zp, sizeof (*zp));
1224 ztest_zll_init(zll_t *zll)
1226 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1227 list_create(&zll->z_list, sizeof (ztest_znode_t),
1228 offsetof(ztest_znode_t, z_lnode));
1232 ztest_zll_destroy(zll_t *zll)
1234 list_destroy(&zll->z_list);
1235 mutex_destroy(&zll->z_lock);
1238 #define RL_TAG "range_lock"
1239 static ztest_znode_t *
1240 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1242 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1243 ztest_znode_t *zp = NULL;
1244 mutex_enter(&zll->z_lock);
1245 for (zp = list_head(&zll->z_list); (zp);
1246 zp = list_next(&zll->z_list, zp)) {
1247 if (zp->z_object == object) {
1248 refcount_add(&zp->z_refcnt, RL_TAG);
1253 zp = ztest_znode_init(object);
1254 refcount_add(&zp->z_refcnt, RL_TAG);
1255 list_insert_head(&zll->z_list, zp);
1257 mutex_exit(&zll->z_lock);
1262 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1265 ASSERT3U(zp->z_object, !=, 0);
1266 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1267 mutex_enter(&zll->z_lock);
1268 refcount_remove(&zp->z_refcnt, RL_TAG);
1269 if (refcount_is_zero(&zp->z_refcnt)) {
1270 list_remove(&zll->z_list, zp);
1271 ztest_znode_fini(zp);
1273 mutex_exit(&zll->z_lock);
1278 ztest_rll_init(rll_t *rll)
1280 rll->rll_writer = NULL;
1281 rll->rll_readers = 0;
1282 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1283 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1287 ztest_rll_destroy(rll_t *rll)
1289 ASSERT(rll->rll_writer == NULL);
1290 ASSERT(rll->rll_readers == 0);
1291 mutex_destroy(&rll->rll_lock);
1292 cv_destroy(&rll->rll_cv);
1296 ztest_rll_lock(rll_t *rll, rl_type_t type)
1298 mutex_enter(&rll->rll_lock);
1300 if (type == RL_READER) {
1301 while (rll->rll_writer != NULL)
1302 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1305 while (rll->rll_writer != NULL || rll->rll_readers)
1306 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1307 rll->rll_writer = curthread;
1310 mutex_exit(&rll->rll_lock);
1314 ztest_rll_unlock(rll_t *rll)
1316 mutex_enter(&rll->rll_lock);
1318 if (rll->rll_writer) {
1319 ASSERT(rll->rll_readers == 0);
1320 rll->rll_writer = NULL;
1322 ASSERT(rll->rll_readers != 0);
1323 ASSERT(rll->rll_writer == NULL);
1327 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1328 cv_broadcast(&rll->rll_cv);
1330 mutex_exit(&rll->rll_lock);
1334 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1336 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1338 ztest_rll_lock(rll, type);
1342 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1344 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1346 ztest_rll_unlock(rll);
1349 static ztest_zrl_t *
1350 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1352 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1354 zrl->z_ztznode = zp;
1359 ztest_zrl_fini(ztest_zrl_t *zrl)
1361 umem_free(zrl, sizeof (*zrl));
1364 static ztest_zrl_t *
1365 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1366 uint64_t size, rl_type_t type)
1368 ztest_znode_t *zp = ztest_znode_get(zd, object);
1369 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1371 return (ztest_zrl_init(rl, zp));
1375 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1377 zfs_range_unlock(zrl->z_rl);
1378 ztest_znode_put(zd, zrl->z_ztznode);
1379 ztest_zrl_fini(zrl);
1383 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1386 zd->zd_zilog = dmu_objset_zil(os);
1387 zd->zd_shared = szd;
1388 dmu_objset_name(os, zd->zd_name);
1391 if (zd->zd_shared != NULL)
1392 zd->zd_shared->zd_seq = 0;
1394 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1395 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1397 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1398 ztest_rll_init(&zd->zd_object_lock[l]);
1400 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1401 ztest_zll_init(&zd->zd_range_lock[l]);
1405 ztest_zd_fini(ztest_ds_t *zd)
1409 mutex_destroy(&zd->zd_dirobj_lock);
1410 (void) rwlock_destroy(&zd->zd_zilog_lock);
1412 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1413 ztest_rll_destroy(&zd->zd_object_lock[l]);
1415 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1416 ztest_zll_destroy(&zd->zd_range_lock[l]);
1419 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1422 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1428 * Attempt to assign tx to some transaction group.
1430 error = dmu_tx_assign(tx, txg_how);
1432 if (error == ERESTART) {
1433 ASSERT(txg_how == TXG_NOWAIT);
1436 ASSERT3U(error, ==, ENOSPC);
1437 ztest_record_enospc(tag);
1442 txg = dmu_tx_get_txg(tx);
1448 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1451 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1459 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1462 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1466 diff |= (value - *ip++);
1473 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1474 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1477 bt->bt_magic = BT_MAGIC;
1478 bt->bt_objset = dmu_objset_id(os);
1479 bt->bt_object = object;
1480 bt->bt_dnodesize = dnodesize;
1481 bt->bt_offset = offset;
1484 bt->bt_crtxg = crtxg;
1488 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1489 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1492 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1493 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1494 ASSERT3U(bt->bt_object, ==, object);
1495 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1496 ASSERT3U(bt->bt_offset, ==, offset);
1497 ASSERT3U(bt->bt_gen, <=, gen);
1498 ASSERT3U(bt->bt_txg, <=, txg);
1499 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1502 static ztest_block_tag_t *
1503 ztest_bt_bonus(dmu_buf_t *db)
1505 dmu_object_info_t doi;
1506 ztest_block_tag_t *bt;
1508 dmu_object_info_from_db(db, &doi);
1509 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1510 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1511 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1517 * Generate a token to fill up unused bonus buffer space. Try to make
1518 * it unique to the object, generation, and offset to verify that data
1519 * is not getting overwritten by data from other dnodes.
1521 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1522 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1525 * Fill up the unused bonus buffer region before the block tag with a
1526 * verifiable pattern. Filling the whole bonus area with non-zero data
1527 * helps ensure that all dnode traversal code properly skips the
1528 * interior regions of large dnodes.
1531 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1532 objset_t *os, uint64_t gen)
1536 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1538 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1539 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1540 gen, bonusp - (uint64_t *)db->db_data);
1546 * Verify that the unused area of a bonus buffer is filled with the
1550 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1551 objset_t *os, uint64_t gen)
1555 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1556 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1557 gen, bonusp - (uint64_t *)db->db_data);
1558 VERIFY3U(*bonusp, ==, token);
1566 #define lrz_type lr_mode
1567 #define lrz_blocksize lr_uid
1568 #define lrz_ibshift lr_gid
1569 #define lrz_bonustype lr_rdev
1570 #define lrz_dnodesize lr_crtime[1]
1573 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1575 char *name = (void *)(lr + 1); /* name follows lr */
1576 size_t namesize = strlen(name) + 1;
1579 if (zil_replaying(zd->zd_zilog, tx))
1582 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1583 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1584 sizeof (*lr) + namesize - sizeof (lr_t));
1586 zil_itx_assign(zd->zd_zilog, itx, tx);
1590 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1592 char *name = (void *)(lr + 1); /* name follows lr */
1593 size_t namesize = strlen(name) + 1;
1596 if (zil_replaying(zd->zd_zilog, tx))
1599 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1600 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1601 sizeof (*lr) + namesize - sizeof (lr_t));
1603 itx->itx_oid = object;
1604 zil_itx_assign(zd->zd_zilog, itx, tx);
1608 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1611 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1613 if (zil_replaying(zd->zd_zilog, tx))
1616 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1617 write_state = WR_INDIRECT;
1619 itx = zil_itx_create(TX_WRITE,
1620 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1622 if (write_state == WR_COPIED &&
1623 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1624 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1625 zil_itx_destroy(itx);
1626 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1627 write_state = WR_NEED_COPY;
1629 itx->itx_private = zd;
1630 itx->itx_wr_state = write_state;
1631 itx->itx_sync = (ztest_random(8) == 0);
1633 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1634 sizeof (*lr) - sizeof (lr_t));
1636 zil_itx_assign(zd->zd_zilog, itx, tx);
1640 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1644 if (zil_replaying(zd->zd_zilog, tx))
1647 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1648 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1649 sizeof (*lr) - sizeof (lr_t));
1651 itx->itx_sync = B_FALSE;
1652 zil_itx_assign(zd->zd_zilog, itx, tx);
1656 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1660 if (zil_replaying(zd->zd_zilog, tx))
1663 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1664 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1665 sizeof (*lr) - sizeof (lr_t));
1667 itx->itx_sync = B_FALSE;
1668 zil_itx_assign(zd->zd_zilog, itx, tx);
1675 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1677 char *name = (void *)(lr + 1); /* name follows lr */
1678 objset_t *os = zd->zd_os;
1679 ztest_block_tag_t *bbt;
1687 byteswap_uint64_array(lr, sizeof (*lr));
1689 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1690 ASSERT(name[0] != '\0');
1692 tx = dmu_tx_create(os);
1694 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1696 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1697 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1699 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1702 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1706 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1707 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1709 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1710 if (lr->lr_foid == 0) {
1711 lr->lr_foid = zap_create_dnsize(os,
1712 lr->lrz_type, lr->lrz_bonustype,
1713 bonuslen, lr->lrz_dnodesize, tx);
1715 error = zap_create_claim_dnsize(os, lr->lr_foid,
1716 lr->lrz_type, lr->lrz_bonustype,
1717 bonuslen, lr->lrz_dnodesize, tx);
1720 if (lr->lr_foid == 0) {
1721 lr->lr_foid = dmu_object_alloc_dnsize(os,
1722 lr->lrz_type, 0, lr->lrz_bonustype,
1723 bonuslen, lr->lrz_dnodesize, tx);
1725 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1726 lr->lrz_type, 0, lr->lrz_bonustype,
1727 bonuslen, lr->lrz_dnodesize, tx);
1732 ASSERT3U(error, ==, EEXIST);
1733 ASSERT(zd->zd_zilog->zl_replay);
1738 ASSERT(lr->lr_foid != 0);
1740 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1741 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1742 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1744 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1745 bbt = ztest_bt_bonus(db);
1746 dmu_buf_will_dirty(db, tx);
1747 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1748 lr->lr_gen, txg, txg);
1749 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1750 dmu_buf_rele(db, FTAG);
1752 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1755 (void) ztest_log_create(zd, tx, lr);
1763 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1765 char *name = (void *)(lr + 1); /* name follows lr */
1766 objset_t *os = zd->zd_os;
1767 dmu_object_info_t doi;
1769 uint64_t object, txg;
1772 byteswap_uint64_array(lr, sizeof (*lr));
1774 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1775 ASSERT(name[0] != '\0');
1778 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1779 ASSERT(object != 0);
1781 ztest_object_lock(zd, object, RL_WRITER);
1783 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1785 tx = dmu_tx_create(os);
1787 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1788 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1790 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1792 ztest_object_unlock(zd, object);
1796 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1797 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1799 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1802 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1804 (void) ztest_log_remove(zd, tx, lr, object);
1808 ztest_object_unlock(zd, object);
1814 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1816 objset_t *os = zd->zd_os;
1817 void *data = lr + 1; /* data follows lr */
1818 uint64_t offset, length;
1819 ztest_block_tag_t *bt = data;
1820 ztest_block_tag_t *bbt;
1821 uint64_t gen, txg, lrtxg, crtxg;
1822 dmu_object_info_t doi;
1825 arc_buf_t *abuf = NULL;
1829 byteswap_uint64_array(lr, sizeof (*lr));
1831 offset = lr->lr_offset;
1832 length = lr->lr_length;
1834 /* If it's a dmu_sync() block, write the whole block */
1835 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1836 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1837 if (length < blocksize) {
1838 offset -= offset % blocksize;
1843 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1844 byteswap_uint64_array(bt, sizeof (*bt));
1846 if (bt->bt_magic != BT_MAGIC)
1849 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1850 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1852 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1854 dmu_object_info_from_db(db, &doi);
1856 bbt = ztest_bt_bonus(db);
1857 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1859 crtxg = bbt->bt_crtxg;
1860 lrtxg = lr->lr_common.lrc_txg;
1862 tx = dmu_tx_create(os);
1864 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1866 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1867 P2PHASE(offset, length) == 0)
1868 abuf = dmu_request_arcbuf(db, length);
1870 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1873 dmu_return_arcbuf(abuf);
1874 dmu_buf_rele(db, FTAG);
1875 ztest_range_unlock(zd, rl);
1876 ztest_object_unlock(zd, lr->lr_foid);
1882 * Usually, verify the old data before writing new data --
1883 * but not always, because we also want to verify correct
1884 * behavior when the data was not recently read into cache.
1886 ASSERT(offset % doi.doi_data_block_size == 0);
1887 if (ztest_random(4) != 0) {
1888 int prefetch = ztest_random(2) ?
1889 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1890 ztest_block_tag_t rbt;
1892 VERIFY(dmu_read(os, lr->lr_foid, offset,
1893 sizeof (rbt), &rbt, prefetch) == 0);
1894 if (rbt.bt_magic == BT_MAGIC) {
1895 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1896 offset, gen, txg, crtxg);
1901 * Writes can appear to be newer than the bonus buffer because
1902 * the ztest_get_data() callback does a dmu_read() of the
1903 * open-context data, which may be different than the data
1904 * as it was when the write was generated.
1906 if (zd->zd_zilog->zl_replay) {
1907 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1908 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1913 * Set the bt's gen/txg to the bonus buffer's gen/txg
1914 * so that all of the usual ASSERTs will work.
1916 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1921 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1923 bcopy(data, abuf->b_data, length);
1924 dmu_assign_arcbuf(db, offset, abuf, tx);
1927 (void) ztest_log_write(zd, tx, lr);
1929 dmu_buf_rele(db, FTAG);
1933 ztest_range_unlock(zd, rl);
1934 ztest_object_unlock(zd, lr->lr_foid);
1940 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1942 objset_t *os = zd->zd_os;
1948 byteswap_uint64_array(lr, sizeof (*lr));
1950 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1951 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1954 tx = dmu_tx_create(os);
1956 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1958 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1960 ztest_range_unlock(zd, rl);
1961 ztest_object_unlock(zd, lr->lr_foid);
1965 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1966 lr->lr_length, tx) == 0);
1968 (void) ztest_log_truncate(zd, tx, lr);
1972 ztest_range_unlock(zd, rl);
1973 ztest_object_unlock(zd, lr->lr_foid);
1979 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1981 objset_t *os = zd->zd_os;
1984 ztest_block_tag_t *bbt;
1985 uint64_t txg, lrtxg, crtxg, dnodesize;
1988 byteswap_uint64_array(lr, sizeof (*lr));
1990 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1992 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1994 tx = dmu_tx_create(os);
1995 dmu_tx_hold_bonus(tx, lr->lr_foid);
1997 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1999 dmu_buf_rele(db, FTAG);
2000 ztest_object_unlock(zd, lr->lr_foid);
2004 bbt = ztest_bt_bonus(db);
2005 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2006 crtxg = bbt->bt_crtxg;
2007 lrtxg = lr->lr_common.lrc_txg;
2008 dnodesize = bbt->bt_dnodesize;
2010 if (zd->zd_zilog->zl_replay) {
2011 ASSERT(lr->lr_size != 0);
2012 ASSERT(lr->lr_mode != 0);
2016 * Randomly change the size and increment the generation.
2018 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2020 lr->lr_mode = bbt->bt_gen + 1;
2025 * Verify that the current bonus buffer is not newer than our txg.
2027 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2028 MAX(txg, lrtxg), crtxg);
2030 dmu_buf_will_dirty(db, tx);
2032 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2033 ASSERT3U(lr->lr_size, <=, db->db_size);
2034 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2035 bbt = ztest_bt_bonus(db);
2037 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2039 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2040 dmu_buf_rele(db, FTAG);
2042 (void) ztest_log_setattr(zd, tx, lr);
2046 ztest_object_unlock(zd, lr->lr_foid);
2051 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
2052 NULL, /* 0 no such transaction type */
2053 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
2054 NULL, /* TX_MKDIR */
2055 NULL, /* TX_MKXATTR */
2056 NULL, /* TX_SYMLINK */
2057 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
2058 NULL, /* TX_RMDIR */
2060 NULL, /* TX_RENAME */
2061 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2062 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2063 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
2065 NULL, /* TX_CREATE_ACL */
2066 NULL, /* TX_CREATE_ATTR */
2067 NULL, /* TX_CREATE_ACL_ATTR */
2068 NULL, /* TX_MKDIR_ACL */
2069 NULL, /* TX_MKDIR_ATTR */
2070 NULL, /* TX_MKDIR_ACL_ATTR */
2071 NULL, /* TX_WRITE2 */
2075 * ZIL get_data callbacks
2077 typedef struct ztest_zgd_private {
2081 } ztest_zgd_private_t;
2084 ztest_get_done(zgd_t *zgd, int error)
2086 ztest_zgd_private_t *zzp = zgd->zgd_private;
2087 ztest_ds_t *zd = zzp->z_zd;
2088 uint64_t object = zzp->z_object;
2091 dmu_buf_rele(zgd->zgd_db, zgd);
2093 ztest_range_unlock(zd, zzp->z_rl);
2094 ztest_object_unlock(zd, object);
2096 if (error == 0 && zgd->zgd_bp)
2097 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2099 umem_free(zgd, sizeof (*zgd));
2100 umem_free(zzp, sizeof (*zzp));
2104 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2106 ztest_ds_t *zd = arg;
2107 objset_t *os = zd->zd_os;
2108 uint64_t object = lr->lr_foid;
2109 uint64_t offset = lr->lr_offset;
2110 uint64_t size = lr->lr_length;
2111 uint64_t txg = lr->lr_common.lrc_txg;
2113 dmu_object_info_t doi;
2117 ztest_zgd_private_t *zgd_private;
2119 ztest_object_lock(zd, object, RL_READER);
2120 error = dmu_bonus_hold(os, object, FTAG, &db);
2122 ztest_object_unlock(zd, object);
2126 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2128 if (crtxg == 0 || crtxg > txg) {
2129 dmu_buf_rele(db, FTAG);
2130 ztest_object_unlock(zd, object);
2134 dmu_object_info_from_db(db, &doi);
2135 dmu_buf_rele(db, FTAG);
2138 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2139 zgd->zgd_zilog = zd->zd_zilog;
2140 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2141 zgd_private->z_zd = zd;
2142 zgd_private->z_object = object;
2143 zgd->zgd_private = zgd_private;
2145 if (buf != NULL) { /* immediate write */
2146 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2149 error = dmu_read(os, object, offset, size, buf,
2150 DMU_READ_NO_PREFETCH);
2153 size = doi.doi_data_block_size;
2155 offset = P2ALIGN(offset, size);
2157 ASSERT(offset < size);
2161 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2164 error = dmu_buf_hold(os, object, offset, zgd, &db,
2165 DMU_READ_NO_PREFETCH);
2168 blkptr_t *bp = &lr->lr_blkptr;
2173 ASSERT(db->db_offset == offset);
2174 ASSERT(db->db_size == size);
2176 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2177 ztest_get_done, zgd);
2184 ztest_get_done(zgd, error);
2190 ztest_lr_alloc(size_t lrsize, char *name)
2193 size_t namesize = name ? strlen(name) + 1 : 0;
2195 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2198 bcopy(name, lr + lrsize, namesize);
2204 ztest_lr_free(void *lr, size_t lrsize, char *name)
2206 size_t namesize = name ? strlen(name) + 1 : 0;
2208 umem_free(lr, lrsize + namesize);
2212 * Lookup a bunch of objects. Returns the number of objects not found.
2215 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2221 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2223 for (i = 0; i < count; i++, od++) {
2225 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2226 sizeof (uint64_t), 1, &od->od_object);
2228 ASSERT(error == ENOENT);
2229 ASSERT(od->od_object == 0);
2233 ztest_block_tag_t *bbt;
2234 dmu_object_info_t doi;
2236 ASSERT(od->od_object != 0);
2237 ASSERT(missing == 0); /* there should be no gaps */
2239 ztest_object_lock(zd, od->od_object, RL_READER);
2240 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2241 od->od_object, FTAG, &db));
2242 dmu_object_info_from_db(db, &doi);
2243 bbt = ztest_bt_bonus(db);
2244 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2245 od->od_type = doi.doi_type;
2246 od->od_blocksize = doi.doi_data_block_size;
2247 od->od_gen = bbt->bt_gen;
2248 dmu_buf_rele(db, FTAG);
2249 ztest_object_unlock(zd, od->od_object);
2257 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2262 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2264 for (i = 0; i < count; i++, od++) {
2271 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2273 lr->lr_doid = od->od_dir;
2274 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2275 lr->lrz_type = od->od_crtype;
2276 lr->lrz_blocksize = od->od_crblocksize;
2277 lr->lrz_ibshift = ztest_random_ibshift();
2278 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2279 lr->lrz_dnodesize = od->od_crdnodesize;
2280 lr->lr_gen = od->od_crgen;
2281 lr->lr_crtime[0] = time(NULL);
2283 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2284 ASSERT(missing == 0);
2288 od->od_object = lr->lr_foid;
2289 od->od_type = od->od_crtype;
2290 od->od_blocksize = od->od_crblocksize;
2291 od->od_gen = od->od_crgen;
2292 ASSERT(od->od_object != 0);
2295 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2302 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2308 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2312 for (i = count - 1; i >= 0; i--, od--) {
2319 * No object was found.
2321 if (od->od_object == 0)
2324 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2326 lr->lr_doid = od->od_dir;
2328 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2329 ASSERT3U(error, ==, ENOSPC);
2334 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2341 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2347 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2349 lr->lr_foid = object;
2350 lr->lr_offset = offset;
2351 lr->lr_length = size;
2353 BP_ZERO(&lr->lr_blkptr);
2355 bcopy(data, lr + 1, size);
2357 error = ztest_replay_write(zd, lr, B_FALSE);
2359 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2365 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2370 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2372 lr->lr_foid = object;
2373 lr->lr_offset = offset;
2374 lr->lr_length = size;
2376 error = ztest_replay_truncate(zd, lr, B_FALSE);
2378 ztest_lr_free(lr, sizeof (*lr), NULL);
2384 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2389 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2391 lr->lr_foid = object;
2395 error = ztest_replay_setattr(zd, lr, B_FALSE);
2397 ztest_lr_free(lr, sizeof (*lr), NULL);
2403 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2405 objset_t *os = zd->zd_os;
2410 txg_wait_synced(dmu_objset_pool(os), 0);
2412 ztest_object_lock(zd, object, RL_READER);
2413 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2415 tx = dmu_tx_create(os);
2417 dmu_tx_hold_write(tx, object, offset, size);
2419 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2422 dmu_prealloc(os, object, offset, size, tx);
2424 txg_wait_synced(dmu_objset_pool(os), txg);
2426 (void) dmu_free_long_range(os, object, offset, size);
2429 ztest_range_unlock(zd, rl);
2430 ztest_object_unlock(zd, object);
2434 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2437 ztest_block_tag_t wbt;
2438 dmu_object_info_t doi;
2439 enum ztest_io_type io_type;
2443 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2444 blocksize = doi.doi_data_block_size;
2445 data = umem_alloc(blocksize, UMEM_NOFAIL);
2448 * Pick an i/o type at random, biased toward writing block tags.
2450 io_type = ztest_random(ZTEST_IO_TYPES);
2451 if (ztest_random(2) == 0)
2452 io_type = ZTEST_IO_WRITE_TAG;
2454 (void) rw_rdlock(&zd->zd_zilog_lock);
2458 case ZTEST_IO_WRITE_TAG:
2459 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2461 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2464 case ZTEST_IO_WRITE_PATTERN:
2465 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2466 if (ztest_random(2) == 0) {
2468 * Induce fletcher2 collisions to ensure that
2469 * zio_ddt_collision() detects and resolves them
2470 * when using fletcher2-verify for deduplication.
2472 ((uint64_t *)data)[0] ^= 1ULL << 63;
2473 ((uint64_t *)data)[4] ^= 1ULL << 63;
2475 (void) ztest_write(zd, object, offset, blocksize, data);
2478 case ZTEST_IO_WRITE_ZEROES:
2479 bzero(data, blocksize);
2480 (void) ztest_write(zd, object, offset, blocksize, data);
2483 case ZTEST_IO_TRUNCATE:
2484 (void) ztest_truncate(zd, object, offset, blocksize);
2487 case ZTEST_IO_SETATTR:
2488 (void) ztest_setattr(zd, object);
2493 case ZTEST_IO_REWRITE:
2494 (void) rw_rdlock(&ztest_name_lock);
2495 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2496 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2498 VERIFY(err == 0 || err == ENOSPC);
2499 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2500 ZFS_PROP_COMPRESSION,
2501 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2503 VERIFY(err == 0 || err == ENOSPC);
2504 (void) rw_unlock(&ztest_name_lock);
2506 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2507 DMU_READ_NO_PREFETCH));
2509 (void) ztest_write(zd, object, offset, blocksize, data);
2513 (void) rw_unlock(&zd->zd_zilog_lock);
2515 umem_free(data, blocksize);
2519 * Initialize an object description template.
2522 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2523 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2526 od->od_dir = ZTEST_DIROBJ;
2529 od->od_crtype = type;
2530 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2531 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2534 od->od_type = DMU_OT_NONE;
2535 od->od_blocksize = 0;
2538 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2539 tag, (longlong_t)id, (u_longlong_t)index);
2543 * Lookup or create the objects for a test using the od template.
2544 * If the objects do not all exist, or if 'remove' is specified,
2545 * remove any existing objects and create new ones. Otherwise,
2546 * use the existing objects.
2549 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2551 int count = size / sizeof (*od);
2554 mutex_enter(&zd->zd_dirobj_lock);
2555 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2556 (ztest_remove(zd, od, count) != 0 ||
2557 ztest_create(zd, od, count) != 0))
2560 mutex_exit(&zd->zd_dirobj_lock);
2567 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2569 zilog_t *zilog = zd->zd_zilog;
2571 (void) rw_rdlock(&zd->zd_zilog_lock);
2573 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2576 * Remember the committed values in zd, which is in parent/child
2577 * shared memory. If we die, the next iteration of ztest_run()
2578 * will verify that the log really does contain this record.
2580 mutex_enter(&zilog->zl_lock);
2581 ASSERT(zd->zd_shared != NULL);
2582 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2583 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2584 mutex_exit(&zilog->zl_lock);
2586 (void) rw_unlock(&zd->zd_zilog_lock);
2590 * This function is designed to simulate the operations that occur during a
2591 * mount/unmount operation. We hold the dataset across these operations in an
2592 * attempt to expose any implicit assumptions about ZIL management.
2596 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2598 objset_t *os = zd->zd_os;
2601 * We grab the zd_dirobj_lock to ensure that no other thread is
2602 * updating the zil (i.e. adding in-memory log records) and the
2603 * zd_zilog_lock to block any I/O.
2605 mutex_enter(&zd->zd_dirobj_lock);
2606 (void) rw_wrlock(&zd->zd_zilog_lock);
2608 /* zfsvfs_teardown() */
2609 zil_close(zd->zd_zilog);
2611 /* zfsvfs_setup() */
2612 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2613 zil_replay(os, zd, ztest_replay_vector);
2615 (void) rw_unlock(&zd->zd_zilog_lock);
2616 mutex_exit(&zd->zd_dirobj_lock);
2620 * Verify that we can't destroy an active pool, create an existing pool,
2621 * or create a pool with a bad vdev spec.
2625 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2627 ztest_shared_opts_t *zo = &ztest_opts;
2631 if (zo->zo_mmp_test)
2635 * Attempt to create using a bad file.
2637 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2638 VERIFY3U(ENOENT, ==,
2639 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2640 nvlist_free(nvroot);
2643 * Attempt to create using a bad mirror.
2645 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2646 VERIFY3U(ENOENT, ==,
2647 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2648 nvlist_free(nvroot);
2651 * Attempt to create an existing pool. It shouldn't matter
2652 * what's in the nvroot; we should fail with EEXIST.
2654 (void) rw_rdlock(&ztest_name_lock);
2655 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2656 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2657 nvlist_free(nvroot);
2658 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2659 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2660 spa_close(spa, FTAG);
2662 (void) rw_unlock(&ztest_name_lock);
2666 * Start and then stop the MMP threads to ensure the startup and shutdown code
2667 * works properly. Actual protection and property-related code tested via ZTS.
2671 ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2673 ztest_shared_opts_t *zo = &ztest_opts;
2674 spa_t *spa = ztest_spa;
2676 if (zo->zo_mmp_test)
2679 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2680 mutex_enter(&spa->spa_props_lock);
2682 if (!spa_multihost(spa)) {
2683 spa->spa_multihost = B_TRUE;
2684 mmp_thread_start(spa);
2687 mutex_exit(&spa->spa_props_lock);
2688 spa_config_exit(spa, SCL_CONFIG, FTAG);
2690 txg_wait_synced(spa_get_dsl(spa), 0);
2691 mmp_signal_all_threads();
2692 txg_wait_synced(spa_get_dsl(spa), 0);
2694 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2695 mutex_enter(&spa->spa_props_lock);
2697 if (spa_multihost(spa)) {
2698 mmp_thread_stop(spa);
2699 spa->spa_multihost = B_FALSE;
2702 mutex_exit(&spa->spa_props_lock);
2703 spa_config_exit(spa, SCL_CONFIG, FTAG);
2708 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2711 uint64_t initial_version = SPA_VERSION_INITIAL;
2712 uint64_t version, newversion;
2713 nvlist_t *nvroot, *props;
2716 if (ztest_opts.zo_mmp_test)
2719 mutex_enter(&ztest_vdev_lock);
2720 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2723 * Clean up from previous runs.
2725 (void) spa_destroy(name);
2727 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2728 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2731 * If we're configuring a RAIDZ device then make sure that the
2732 * the initial version is capable of supporting that feature.
2734 switch (ztest_opts.zo_raidz_parity) {
2737 initial_version = SPA_VERSION_INITIAL;
2740 initial_version = SPA_VERSION_RAIDZ2;
2743 initial_version = SPA_VERSION_RAIDZ3;
2748 * Create a pool with a spa version that can be upgraded. Pick
2749 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2752 version = ztest_random_spa_version(initial_version);
2753 } while (version > SPA_VERSION_BEFORE_FEATURES);
2755 props = fnvlist_alloc();
2756 fnvlist_add_uint64(props,
2757 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2758 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2759 fnvlist_free(nvroot);
2760 fnvlist_free(props);
2762 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2763 VERIFY3U(spa_version(spa), ==, version);
2764 newversion = ztest_random_spa_version(version + 1);
2766 if (ztest_opts.zo_verbose >= 4) {
2767 (void) printf("upgrading spa version from %llu to %llu\n",
2768 (u_longlong_t)version, (u_longlong_t)newversion);
2771 spa_upgrade(spa, newversion);
2772 VERIFY3U(spa_version(spa), >, version);
2773 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2774 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2775 spa_close(spa, FTAG);
2778 mutex_exit(&ztest_vdev_lock);
2782 vdev_lookup_by_path(vdev_t *vd, const char *path)
2787 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2790 for (c = 0; c < vd->vdev_children; c++)
2791 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2799 * Find the first available hole which can be used as a top-level.
2802 find_vdev_hole(spa_t *spa)
2804 vdev_t *rvd = spa->spa_root_vdev;
2807 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2809 for (c = 0; c < rvd->vdev_children; c++) {
2810 vdev_t *cvd = rvd->vdev_child[c];
2812 if (cvd->vdev_ishole)
2819 * Verify that vdev_add() works as expected.
2823 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2825 ztest_shared_t *zs = ztest_shared;
2826 spa_t *spa = ztest_spa;
2832 if (ztest_opts.zo_mmp_test)
2835 mutex_enter(&ztest_vdev_lock);
2836 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2838 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2840 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2843 * If we have slogs then remove them 1/4 of the time.
2845 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2847 * Grab the guid from the head of the log class rotor.
2849 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2851 spa_config_exit(spa, SCL_VDEV, FTAG);
2854 * We have to grab the zs_name_lock as writer to
2855 * prevent a race between removing a slog (dmu_objset_find)
2856 * and destroying a dataset. Removing the slog will
2857 * grab a reference on the dataset which may cause
2858 * dsl_destroy_head() to fail with EBUSY thus
2859 * leaving the dataset in an inconsistent state.
2861 rw_wrlock(&ztest_name_lock);
2862 error = spa_vdev_remove(spa, guid, B_FALSE);
2863 rw_unlock(&ztest_name_lock);
2865 if (error && error != EEXIST)
2866 fatal(0, "spa_vdev_remove() = %d", error);
2868 spa_config_exit(spa, SCL_VDEV, FTAG);
2871 * Make 1/4 of the devices be log devices.
2873 nvroot = make_vdev_root(NULL, NULL, NULL,
2874 ztest_opts.zo_vdev_size, 0,
2875 ztest_random(4) == 0, ztest_opts.zo_raidz,
2878 error = spa_vdev_add(spa, nvroot);
2879 nvlist_free(nvroot);
2881 if (error == ENOSPC)
2882 ztest_record_enospc("spa_vdev_add");
2883 else if (error != 0)
2884 fatal(0, "spa_vdev_add() = %d", error);
2887 mutex_exit(&ztest_vdev_lock);
2891 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2895 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2897 ztest_shared_t *zs = ztest_shared;
2898 spa_t *spa = ztest_spa;
2899 vdev_t *rvd = spa->spa_root_vdev;
2900 spa_aux_vdev_t *sav;
2906 if (ztest_opts.zo_mmp_test)
2909 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2911 if (ztest_random(2) == 0) {
2912 sav = &spa->spa_spares;
2913 aux = ZPOOL_CONFIG_SPARES;
2915 sav = &spa->spa_l2cache;
2916 aux = ZPOOL_CONFIG_L2CACHE;
2919 mutex_enter(&ztest_vdev_lock);
2921 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2923 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2925 * Pick a random device to remove.
2927 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2930 * Find an unused device we can add.
2932 zs->zs_vdev_aux = 0;
2935 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2936 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2938 for (c = 0; c < sav->sav_count; c++)
2939 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2942 if (c == sav->sav_count &&
2943 vdev_lookup_by_path(rvd, path) == NULL)
2949 spa_config_exit(spa, SCL_VDEV, FTAG);
2955 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2956 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2957 error = spa_vdev_add(spa, nvroot);
2959 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2960 nvlist_free(nvroot);
2963 * Remove an existing device. Sometimes, dirty its
2964 * vdev state first to make sure we handle removal
2965 * of devices that have pending state changes.
2967 if (ztest_random(2) == 0)
2968 (void) vdev_online(spa, guid, 0, NULL);
2970 error = spa_vdev_remove(spa, guid, B_FALSE);
2971 if (error != 0 && error != EBUSY)
2972 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2975 mutex_exit(&ztest_vdev_lock);
2977 umem_free(path, MAXPATHLEN);
2981 * split a pool if it has mirror tlvdevs
2985 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2987 ztest_shared_t *zs = ztest_shared;
2988 spa_t *spa = ztest_spa;
2989 vdev_t *rvd = spa->spa_root_vdev;
2990 nvlist_t *tree, **child, *config, *split, **schild;
2991 uint_t c, children, schildren = 0, lastlogid = 0;
2994 if (ztest_opts.zo_mmp_test)
2997 mutex_enter(&ztest_vdev_lock);
2999 /* ensure we have a useable config; mirrors of raidz aren't supported */
3000 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3001 mutex_exit(&ztest_vdev_lock);
3005 /* clean up the old pool, if any */
3006 (void) spa_destroy("splitp");
3008 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3010 /* generate a config from the existing config */
3011 mutex_enter(&spa->spa_props_lock);
3012 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3014 mutex_exit(&spa->spa_props_lock);
3016 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3019 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3020 for (c = 0; c < children; c++) {
3021 vdev_t *tvd = rvd->vdev_child[c];
3025 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3026 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3028 VERIFY(nvlist_add_string(schild[schildren],
3029 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3030 VERIFY(nvlist_add_uint64(schild[schildren],
3031 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3033 lastlogid = schildren;
3038 VERIFY(nvlist_lookup_nvlist_array(child[c],
3039 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3040 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3043 /* OK, create a config that can be used to split */
3044 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3045 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3046 VDEV_TYPE_ROOT) == 0);
3047 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3048 lastlogid != 0 ? lastlogid : schildren) == 0);
3050 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3051 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3053 for (c = 0; c < schildren; c++)
3054 nvlist_free(schild[c]);
3058 spa_config_exit(spa, SCL_VDEV, FTAG);
3060 (void) rw_wrlock(&ztest_name_lock);
3061 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3062 (void) rw_unlock(&ztest_name_lock);
3064 nvlist_free(config);
3067 (void) printf("successful split - results:\n");
3068 mutex_enter(&spa_namespace_lock);
3069 show_pool_stats(spa);
3070 show_pool_stats(spa_lookup("splitp"));
3071 mutex_exit(&spa_namespace_lock);
3075 mutex_exit(&ztest_vdev_lock);
3080 * Verify that we can attach and detach devices.
3084 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3086 ztest_shared_t *zs = ztest_shared;
3087 spa_t *spa = ztest_spa;
3088 spa_aux_vdev_t *sav = &spa->spa_spares;
3089 vdev_t *rvd = spa->spa_root_vdev;
3090 vdev_t *oldvd, *newvd, *pvd;
3094 uint64_t ashift = ztest_get_ashift();
3095 uint64_t oldguid, pguid;
3096 uint64_t oldsize, newsize;
3097 char *oldpath, *newpath;
3099 int oldvd_has_siblings = B_FALSE;
3100 int newvd_is_spare = B_FALSE;
3102 int error, expected_error;
3104 if (ztest_opts.zo_mmp_test)
3107 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3108 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3110 mutex_enter(&ztest_vdev_lock);
3111 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3113 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3116 * Decide whether to do an attach or a replace.
3118 replacing = ztest_random(2);
3121 * Pick a random top-level vdev.
3123 top = ztest_random_vdev_top(spa, B_TRUE);
3126 * Pick a random leaf within it.
3128 leaf = ztest_random(leaves);
3133 oldvd = rvd->vdev_child[top];
3134 if (zs->zs_mirrors >= 1) {
3135 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3136 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3137 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3139 if (ztest_opts.zo_raidz > 1) {
3140 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3141 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3142 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3146 * If we're already doing an attach or replace, oldvd may be a
3147 * mirror vdev -- in which case, pick a random child.
3149 while (oldvd->vdev_children != 0) {
3150 oldvd_has_siblings = B_TRUE;
3151 ASSERT(oldvd->vdev_children >= 2);
3152 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3155 oldguid = oldvd->vdev_guid;
3156 oldsize = vdev_get_min_asize(oldvd);
3157 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3158 (void) strcpy(oldpath, oldvd->vdev_path);
3159 pvd = oldvd->vdev_parent;
3160 pguid = pvd->vdev_guid;
3163 * If oldvd has siblings, then half of the time, detach it.
3165 if (oldvd_has_siblings && ztest_random(2) == 0) {
3166 spa_config_exit(spa, SCL_VDEV, FTAG);
3167 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3168 if (error != 0 && error != ENODEV && error != EBUSY &&
3170 fatal(0, "detach (%s) returned %d", oldpath, error);
3175 * For the new vdev, choose with equal probability between the two
3176 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3178 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3179 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3180 newvd_is_spare = B_TRUE;
3181 (void) strcpy(newpath, newvd->vdev_path);
3183 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3184 ztest_opts.zo_dir, ztest_opts.zo_pool,
3185 top * leaves + leaf);
3186 if (ztest_random(2) == 0)
3187 newpath[strlen(newpath) - 1] = 'b';
3188 newvd = vdev_lookup_by_path(rvd, newpath);
3192 newsize = vdev_get_min_asize(newvd);
3195 * Make newsize a little bigger or smaller than oldsize.
3196 * If it's smaller, the attach should fail.
3197 * If it's larger, and we're doing a replace,
3198 * we should get dynamic LUN growth when we're done.
3200 newsize = 10 * oldsize / (9 + ztest_random(3));
3204 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3205 * unless it's a replace; in that case any non-replacing parent is OK.
3207 * If newvd is already part of the pool, it should fail with EBUSY.
3209 * If newvd is too small, it should fail with EOVERFLOW.
3211 if (pvd->vdev_ops != &vdev_mirror_ops &&
3212 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3213 pvd->vdev_ops == &vdev_replacing_ops ||
3214 pvd->vdev_ops == &vdev_spare_ops))
3215 expected_error = ENOTSUP;
3216 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3217 expected_error = ENOTSUP;
3218 else if (newvd == oldvd)
3219 expected_error = replacing ? 0 : EBUSY;
3220 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3221 expected_error = EBUSY;
3222 else if (newsize < oldsize)
3223 expected_error = EOVERFLOW;
3224 else if (ashift > oldvd->vdev_top->vdev_ashift)
3225 expected_error = EDOM;
3229 spa_config_exit(spa, SCL_VDEV, FTAG);
3232 * Build the nvlist describing newpath.
3234 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3235 ashift, 0, 0, 0, 1);
3237 error = spa_vdev_attach(spa, oldguid, root, replacing);
3242 * If our parent was the replacing vdev, but the replace completed,
3243 * then instead of failing with ENOTSUP we may either succeed,
3244 * fail with ENODEV, or fail with EOVERFLOW.
3246 if (expected_error == ENOTSUP &&
3247 (error == 0 || error == ENODEV || error == EOVERFLOW))
3248 expected_error = error;
3251 * If someone grew the LUN, the replacement may be too small.
3253 if (error == EOVERFLOW || error == EBUSY)
3254 expected_error = error;
3256 /* XXX workaround 6690467 */
3257 if (error != expected_error && expected_error != EBUSY) {
3258 fatal(0, "attach (%s %llu, %s %llu, %d) "
3259 "returned %d, expected %d",
3260 oldpath, oldsize, newpath,
3261 newsize, replacing, error, expected_error);
3264 mutex_exit(&ztest_vdev_lock);
3266 umem_free(oldpath, MAXPATHLEN);
3267 umem_free(newpath, MAXPATHLEN);
3271 * Callback function which expands the physical size of the vdev.
3274 grow_vdev(vdev_t *vd, void *arg)
3276 ASSERTV(spa_t *spa = vd->vdev_spa);
3277 size_t *newsize = arg;
3281 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3282 ASSERT(vd->vdev_ops->vdev_op_leaf);
3284 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3287 fsize = lseek(fd, 0, SEEK_END);
3288 VERIFY(ftruncate(fd, *newsize) == 0);
3290 if (ztest_opts.zo_verbose >= 6) {
3291 (void) printf("%s grew from %lu to %lu bytes\n",
3292 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3299 * Callback function which expands a given vdev by calling vdev_online().
3303 online_vdev(vdev_t *vd, void *arg)
3305 spa_t *spa = vd->vdev_spa;
3306 vdev_t *tvd = vd->vdev_top;
3307 uint64_t guid = vd->vdev_guid;
3308 uint64_t generation = spa->spa_config_generation + 1;
3309 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3312 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3313 ASSERT(vd->vdev_ops->vdev_op_leaf);
3315 /* Calling vdev_online will initialize the new metaslabs */
3316 spa_config_exit(spa, SCL_STATE, spa);
3317 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3318 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3321 * If vdev_online returned an error or the underlying vdev_open
3322 * failed then we abort the expand. The only way to know that
3323 * vdev_open fails is by checking the returned newstate.
3325 if (error || newstate != VDEV_STATE_HEALTHY) {
3326 if (ztest_opts.zo_verbose >= 5) {
3327 (void) printf("Unable to expand vdev, state %llu, "
3328 "error %d\n", (u_longlong_t)newstate, error);
3332 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3335 * Since we dropped the lock we need to ensure that we're
3336 * still talking to the original vdev. It's possible this
3337 * vdev may have been detached/replaced while we were
3338 * trying to online it.
3340 if (generation != spa->spa_config_generation) {
3341 if (ztest_opts.zo_verbose >= 5) {
3342 (void) printf("vdev configuration has changed, "
3343 "guid %llu, state %llu, expected gen %llu, "
3346 (u_longlong_t)tvd->vdev_state,
3347 (u_longlong_t)generation,
3348 (u_longlong_t)spa->spa_config_generation);
3356 * Traverse the vdev tree calling the supplied function.
3357 * We continue to walk the tree until we either have walked all
3358 * children or we receive a non-NULL return from the callback.
3359 * If a NULL callback is passed, then we just return back the first
3360 * leaf vdev we encounter.
3363 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3367 if (vd->vdev_ops->vdev_op_leaf) {
3371 return (func(vd, arg));
3374 for (c = 0; c < vd->vdev_children; c++) {
3375 vdev_t *cvd = vd->vdev_child[c];
3376 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3383 * Verify that dynamic LUN growth works as expected.
3387 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3389 spa_t *spa = ztest_spa;
3391 metaslab_class_t *mc;
3392 metaslab_group_t *mg;
3393 size_t psize, newsize;
3395 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3397 mutex_enter(&ztest_vdev_lock);
3398 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3400 top = ztest_random_vdev_top(spa, B_TRUE);
3402 tvd = spa->spa_root_vdev->vdev_child[top];
3405 old_ms_count = tvd->vdev_ms_count;
3406 old_class_space = metaslab_class_get_space(mc);
3409 * Determine the size of the first leaf vdev associated with
3410 * our top-level device.
3412 vd = vdev_walk_tree(tvd, NULL, NULL);
3413 ASSERT3P(vd, !=, NULL);
3414 ASSERT(vd->vdev_ops->vdev_op_leaf);
3416 psize = vd->vdev_psize;
3419 * We only try to expand the vdev if it's healthy, less than 4x its
3420 * original size, and it has a valid psize.
3422 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3423 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3424 spa_config_exit(spa, SCL_STATE, spa);
3425 mutex_exit(&ztest_vdev_lock);
3429 newsize = psize + psize / 8;
3430 ASSERT3U(newsize, >, psize);
3432 if (ztest_opts.zo_verbose >= 6) {
3433 (void) printf("Expanding LUN %s from %lu to %lu\n",
3434 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3438 * Growing the vdev is a two step process:
3439 * 1). expand the physical size (i.e. relabel)
3440 * 2). online the vdev to create the new metaslabs
3442 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3443 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3444 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3445 if (ztest_opts.zo_verbose >= 5) {
3446 (void) printf("Could not expand LUN because "
3447 "the vdev configuration changed.\n");
3449 spa_config_exit(spa, SCL_STATE, spa);
3450 mutex_exit(&ztest_vdev_lock);
3454 spa_config_exit(spa, SCL_STATE, spa);
3457 * Expanding the LUN will update the config asynchronously,
3458 * thus we must wait for the async thread to complete any
3459 * pending tasks before proceeding.
3463 mutex_enter(&spa->spa_async_lock);
3464 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3465 mutex_exit(&spa->spa_async_lock);
3468 txg_wait_synced(spa_get_dsl(spa), 0);
3469 (void) poll(NULL, 0, 100);
3472 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3474 tvd = spa->spa_root_vdev->vdev_child[top];
3475 new_ms_count = tvd->vdev_ms_count;
3476 new_class_space = metaslab_class_get_space(mc);
3478 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3479 if (ztest_opts.zo_verbose >= 5) {
3480 (void) printf("Could not verify LUN expansion due to "
3481 "intervening vdev offline or remove.\n");
3483 spa_config_exit(spa, SCL_STATE, spa);
3484 mutex_exit(&ztest_vdev_lock);
3489 * Make sure we were able to grow the vdev.
3491 if (new_ms_count <= old_ms_count)
3492 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3493 old_ms_count, new_ms_count);
3496 * Make sure we were able to grow the pool.
3498 if (new_class_space <= old_class_space)
3499 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3500 old_class_space, new_class_space);
3502 if (ztest_opts.zo_verbose >= 5) {
3503 char oldnumbuf[6], newnumbuf[6];
3505 nicenum(old_class_space, oldnumbuf);
3506 nicenum(new_class_space, newnumbuf);
3507 (void) printf("%s grew from %s to %s\n",
3508 spa->spa_name, oldnumbuf, newnumbuf);
3511 spa_config_exit(spa, SCL_STATE, spa);
3512 mutex_exit(&ztest_vdev_lock);
3516 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3520 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3523 * Create the objects common to all ztest datasets.
3525 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3526 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3530 ztest_dataset_create(char *dsname)
3532 uint64_t zilset = ztest_random(100);
3533 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3534 ztest_objset_create_cb, NULL);
3536 if (err || zilset < 80)
3539 if (ztest_opts.zo_verbose >= 5)
3540 (void) printf("Setting dataset %s to sync always\n", dsname);
3541 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3542 ZFS_SYNC_ALWAYS, B_FALSE));
3547 ztest_objset_destroy_cb(const char *name, void *arg)
3550 dmu_object_info_t doi;
3554 * Verify that the dataset contains a directory object.
3556 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3557 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3558 if (error != ENOENT) {
3559 /* We could have crashed in the middle of destroying it */
3561 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3562 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3564 dmu_objset_disown(os, FTAG);
3567 * Destroy the dataset.
3569 if (strchr(name, '@') != NULL) {
3570 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
3572 error = dsl_destroy_head(name);
3573 /* There could be a hold on this dataset */
3581 ztest_snapshot_create(char *osname, uint64_t id)
3583 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3586 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3588 error = dmu_objset_snapshot_one(osname, snapname);
3589 if (error == ENOSPC) {
3590 ztest_record_enospc(FTAG);
3593 if (error != 0 && error != EEXIST) {
3594 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3601 ztest_snapshot_destroy(char *osname, uint64_t id)
3603 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3606 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3609 error = dsl_destroy_snapshot(snapname, B_FALSE);
3610 if (error != 0 && error != ENOENT)
3611 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3617 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3623 char name[ZFS_MAX_DATASET_NAME_LEN];
3627 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3629 (void) rw_rdlock(&ztest_name_lock);
3631 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3632 ztest_opts.zo_pool, (u_longlong_t)id);
3635 * If this dataset exists from a previous run, process its replay log
3636 * half of the time. If we don't replay it, then dsl_destroy_head()
3637 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3639 if (ztest_random(2) == 0 &&
3640 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3641 ztest_zd_init(zdtmp, NULL, os);
3642 zil_replay(os, zdtmp, ztest_replay_vector);
3643 ztest_zd_fini(zdtmp);
3644 dmu_objset_disown(os, FTAG);
3648 * There may be an old instance of the dataset we're about to
3649 * create lying around from a previous run. If so, destroy it
3650 * and all of its snapshots.
3652 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3653 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3656 * Verify that the destroyed dataset is no longer in the namespace.
3658 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3662 * Verify that we can create a new dataset.
3664 error = ztest_dataset_create(name);
3666 if (error == ENOSPC) {
3667 ztest_record_enospc(FTAG);
3670 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3673 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3675 ztest_zd_init(zdtmp, NULL, os);
3678 * Open the intent log for it.
3680 zilog = zil_open(os, ztest_get_data);
3683 * Put some objects in there, do a little I/O to them,
3684 * and randomly take a couple of snapshots along the way.
3686 iters = ztest_random(5);
3687 for (i = 0; i < iters; i++) {
3688 ztest_dmu_object_alloc_free(zdtmp, id);
3689 if (ztest_random(iters) == 0)
3690 (void) ztest_snapshot_create(name, i);
3694 * Verify that we cannot create an existing dataset.
3696 VERIFY3U(EEXIST, ==,
3697 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3700 * Verify that we can hold an objset that is also owned.
3702 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3703 dmu_objset_rele(os2, FTAG);
3706 * Verify that we cannot own an objset that is already owned.
3709 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3712 dmu_objset_disown(os, FTAG);
3713 ztest_zd_fini(zdtmp);
3715 (void) rw_unlock(&ztest_name_lock);
3717 umem_free(zdtmp, sizeof (ztest_ds_t));
3721 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3724 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3726 (void) rw_rdlock(&ztest_name_lock);
3727 (void) ztest_snapshot_destroy(zd->zd_name, id);
3728 (void) ztest_snapshot_create(zd->zd_name, id);
3729 (void) rw_unlock(&ztest_name_lock);
3733 * Cleanup non-standard snapshots and clones.
3736 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3745 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3746 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3747 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3748 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3749 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3751 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3752 "%s@s1_%llu", osname, (u_longlong_t)id);
3753 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3754 "%s/c1_%llu", osname, (u_longlong_t)id);
3755 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3756 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3757 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3758 "%s/c2_%llu", osname, (u_longlong_t)id);
3759 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3760 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3762 error = dsl_destroy_head(clone2name);
3763 if (error && error != ENOENT)
3764 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3765 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3766 if (error && error != ENOENT)
3767 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3768 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3769 if (error && error != ENOENT)
3770 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3771 error = dsl_destroy_head(clone1name);
3772 if (error && error != ENOENT)
3773 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3774 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3775 if (error && error != ENOENT)
3776 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3778 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3779 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3780 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3781 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3782 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3786 * Verify dsl_dataset_promote handles EBUSY
3789 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3797 char *osname = zd->zd_name;
3800 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3801 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3802 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3803 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3804 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3806 (void) rw_rdlock(&ztest_name_lock);
3808 ztest_dsl_dataset_cleanup(osname, id);
3810 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3811 "%s@s1_%llu", osname, (u_longlong_t)id);
3812 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3813 "%s/c1_%llu", osname, (u_longlong_t)id);
3814 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3815 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3816 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3817 "%s/c2_%llu", osname, (u_longlong_t)id);
3818 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3819 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3821 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3822 if (error && error != EEXIST) {
3823 if (error == ENOSPC) {
3824 ztest_record_enospc(FTAG);
3827 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3830 error = dmu_objset_clone(clone1name, snap1name);
3832 if (error == ENOSPC) {
3833 ztest_record_enospc(FTAG);
3836 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3839 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3840 if (error && error != EEXIST) {
3841 if (error == ENOSPC) {
3842 ztest_record_enospc(FTAG);
3845 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3848 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3849 if (error && error != EEXIST) {
3850 if (error == ENOSPC) {
3851 ztest_record_enospc(FTAG);
3854 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3857 error = dmu_objset_clone(clone2name, snap3name);
3859 if (error == ENOSPC) {
3860 ztest_record_enospc(FTAG);
3863 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3866 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3868 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3869 error = dsl_dataset_promote(clone2name, NULL);
3870 if (error == ENOSPC) {
3871 dmu_objset_disown(os, FTAG);
3872 ztest_record_enospc(FTAG);
3876 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3878 dmu_objset_disown(os, FTAG);
3881 ztest_dsl_dataset_cleanup(osname, id);
3883 (void) rw_unlock(&ztest_name_lock);
3885 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3886 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3887 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3888 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3889 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3892 #undef OD_ARRAY_SIZE
3893 #define OD_ARRAY_SIZE 4
3896 * Verify that dmu_object_{alloc,free} work as expected.
3899 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3906 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3907 od = umem_alloc(size, UMEM_NOFAIL);
3908 batchsize = OD_ARRAY_SIZE;
3910 for (b = 0; b < batchsize; b++)
3911 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3915 * Destroy the previous batch of objects, create a new batch,
3916 * and do some I/O on the new objects.
3918 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3921 while (ztest_random(4 * batchsize) != 0)
3922 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3923 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3925 umem_free(od, size);
3928 #undef OD_ARRAY_SIZE
3929 #define OD_ARRAY_SIZE 2
3932 * Verify that dmu_{read,write} work as expected.
3935 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3940 objset_t *os = zd->zd_os;
3941 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3942 od = umem_alloc(size, UMEM_NOFAIL);
3944 int i, freeit, error;
3946 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3947 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3948 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3949 uint64_t regions = 997;
3950 uint64_t stride = 123456789ULL;
3951 uint64_t width = 40;
3952 int free_percent = 5;
3955 * This test uses two objects, packobj and bigobj, that are always
3956 * updated together (i.e. in the same tx) so that their contents are
3957 * in sync and can be compared. Their contents relate to each other
3958 * in a simple way: packobj is a dense array of 'bufwad' structures,
3959 * while bigobj is a sparse array of the same bufwads. Specifically,
3960 * for any index n, there are three bufwads that should be identical:
3962 * packobj, at offset n * sizeof (bufwad_t)
3963 * bigobj, at the head of the nth chunk
3964 * bigobj, at the tail of the nth chunk
3966 * The chunk size is arbitrary. It doesn't have to be a power of two,
3967 * and it doesn't have any relation to the object blocksize.
3968 * The only requirement is that it can hold at least two bufwads.
3970 * Normally, we write the bufwad to each of these locations.
3971 * However, free_percent of the time we instead write zeroes to
3972 * packobj and perform a dmu_free_range() on bigobj. By comparing
3973 * bigobj to packobj, we can verify that the DMU is correctly
3974 * tracking which parts of an object are allocated and free,
3975 * and that the contents of the allocated blocks are correct.
3979 * Read the directory info. If it's the first time, set things up.
3981 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3982 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3985 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3986 umem_free(od, size);
3990 bigobj = od[0].od_object;
3991 packobj = od[1].od_object;
3992 chunksize = od[0].od_gen;
3993 ASSERT(chunksize == od[1].od_gen);
3996 * Prefetch a random chunk of the big object.
3997 * Our aim here is to get some async reads in flight
3998 * for blocks that we may free below; the DMU should
3999 * handle this race correctly.
4001 n = ztest_random(regions) * stride + ztest_random(width);
4002 s = 1 + ztest_random(2 * width - 1);
4003 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4004 ZIO_PRIORITY_SYNC_READ);
4007 * Pick a random index and compute the offsets into packobj and bigobj.
4009 n = ztest_random(regions) * stride + ztest_random(width);
4010 s = 1 + ztest_random(width - 1);
4012 packoff = n * sizeof (bufwad_t);
4013 packsize = s * sizeof (bufwad_t);
4015 bigoff = n * chunksize;
4016 bigsize = s * chunksize;
4018 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4019 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4022 * free_percent of the time, free a range of bigobj rather than
4025 freeit = (ztest_random(100) < free_percent);
4028 * Read the current contents of our objects.
4030 error = dmu_read(os, packobj, packoff, packsize, packbuf,
4033 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
4038 * Get a tx for the mods to both packobj and bigobj.
4040 tx = dmu_tx_create(os);
4042 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4045 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4047 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4049 /* This accounts for setting the checksum/compression. */
4050 dmu_tx_hold_bonus(tx, bigobj);
4052 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4054 umem_free(packbuf, packsize);
4055 umem_free(bigbuf, bigsize);
4056 umem_free(od, size);
4060 enum zio_checksum cksum;
4062 cksum = (enum zio_checksum)
4063 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4064 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4065 dmu_object_set_checksum(os, bigobj, cksum, tx);
4067 enum zio_compress comp;
4069 comp = (enum zio_compress)
4070 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4071 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4072 dmu_object_set_compress(os, bigobj, comp, tx);
4075 * For each index from n to n + s, verify that the existing bufwad
4076 * in packobj matches the bufwads at the head and tail of the
4077 * corresponding chunk in bigobj. Then update all three bufwads
4078 * with the new values we want to write out.
4080 for (i = 0; i < s; i++) {
4082 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4084 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4086 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4088 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4089 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4091 if (pack->bw_txg > txg)
4092 fatal(0, "future leak: got %llx, open txg is %llx",
4095 if (pack->bw_data != 0 && pack->bw_index != n + i)
4096 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4097 pack->bw_index, n, i);
4099 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4100 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4102 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4103 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4106 bzero(pack, sizeof (bufwad_t));
4108 pack->bw_index = n + i;
4110 pack->bw_data = 1 + ztest_random(-2ULL);
4117 * We've verified all the old bufwads, and made new ones.
4118 * Now write them out.
4120 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4123 if (ztest_opts.zo_verbose >= 7) {
4124 (void) printf("freeing offset %llx size %llx"
4126 (u_longlong_t)bigoff,
4127 (u_longlong_t)bigsize,
4130 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4132 if (ztest_opts.zo_verbose >= 7) {
4133 (void) printf("writing offset %llx size %llx"
4135 (u_longlong_t)bigoff,
4136 (u_longlong_t)bigsize,
4139 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4145 * Sanity check the stuff we just wrote.
4148 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4149 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4151 VERIFY(0 == dmu_read(os, packobj, packoff,
4152 packsize, packcheck, DMU_READ_PREFETCH));
4153 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4154 bigsize, bigcheck, DMU_READ_PREFETCH));
4156 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4157 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4159 umem_free(packcheck, packsize);
4160 umem_free(bigcheck, bigsize);
4163 umem_free(packbuf, packsize);
4164 umem_free(bigbuf, bigsize);
4165 umem_free(od, size);
4169 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4170 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4178 * For each index from n to n + s, verify that the existing bufwad
4179 * in packobj matches the bufwads at the head and tail of the
4180 * corresponding chunk in bigobj. Then update all three bufwads
4181 * with the new values we want to write out.
4183 for (i = 0; i < s; i++) {
4185 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4187 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4189 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4191 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4192 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4194 if (pack->bw_txg > txg)
4195 fatal(0, "future leak: got %llx, open txg is %llx",
4198 if (pack->bw_data != 0 && pack->bw_index != n + i)
4199 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4200 pack->bw_index, n, i);
4202 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4203 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4205 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4206 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4208 pack->bw_index = n + i;
4210 pack->bw_data = 1 + ztest_random(-2ULL);
4217 #undef OD_ARRAY_SIZE
4218 #define OD_ARRAY_SIZE 2
4221 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4223 objset_t *os = zd->zd_os;
4230 bufwad_t *packbuf, *bigbuf;
4231 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4232 uint64_t blocksize = ztest_random_blocksize();
4233 uint64_t chunksize = blocksize;
4234 uint64_t regions = 997;
4235 uint64_t stride = 123456789ULL;
4237 dmu_buf_t *bonus_db;
4238 arc_buf_t **bigbuf_arcbufs;
4239 dmu_object_info_t doi;
4241 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4242 od = umem_alloc(size, UMEM_NOFAIL);
4245 * This test uses two objects, packobj and bigobj, that are always
4246 * updated together (i.e. in the same tx) so that their contents are
4247 * in sync and can be compared. Their contents relate to each other
4248 * in a simple way: packobj is a dense array of 'bufwad' structures,
4249 * while bigobj is a sparse array of the same bufwads. Specifically,
4250 * for any index n, there are three bufwads that should be identical:
4252 * packobj, at offset n * sizeof (bufwad_t)
4253 * bigobj, at the head of the nth chunk
4254 * bigobj, at the tail of the nth chunk
4256 * The chunk size is set equal to bigobj block size so that
4257 * dmu_assign_arcbuf() can be tested for object updates.
4261 * Read the directory info. If it's the first time, set things up.
4263 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4264 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4268 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4269 umem_free(od, size);
4273 bigobj = od[0].od_object;
4274 packobj = od[1].od_object;
4275 blocksize = od[0].od_blocksize;
4276 chunksize = blocksize;
4277 ASSERT(chunksize == od[1].od_gen);
4279 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4280 VERIFY(ISP2(doi.doi_data_block_size));
4281 VERIFY(chunksize == doi.doi_data_block_size);
4282 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4285 * Pick a random index and compute the offsets into packobj and bigobj.
4287 n = ztest_random(regions) * stride + ztest_random(width);
4288 s = 1 + ztest_random(width - 1);
4290 packoff = n * sizeof (bufwad_t);
4291 packsize = s * sizeof (bufwad_t);
4293 bigoff = n * chunksize;
4294 bigsize = s * chunksize;
4296 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4297 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4299 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4301 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4304 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4305 * Iteration 1 test zcopy to already referenced dbufs.
4306 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4307 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4308 * Iteration 4 test zcopy when dbuf is no longer dirty.
4309 * Iteration 5 test zcopy when it can't be done.
4310 * Iteration 6 one more zcopy write.
4312 for (i = 0; i < 7; i++) {
4317 * In iteration 5 (i == 5) use arcbufs
4318 * that don't match bigobj blksz to test
4319 * dmu_assign_arcbuf() when it can't directly
4320 * assign an arcbuf to a dbuf.
4322 for (j = 0; j < s; j++) {
4323 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4325 dmu_request_arcbuf(bonus_db, chunksize);
4327 bigbuf_arcbufs[2 * j] =
4328 dmu_request_arcbuf(bonus_db, chunksize / 2);
4329 bigbuf_arcbufs[2 * j + 1] =
4330 dmu_request_arcbuf(bonus_db, chunksize / 2);
4335 * Get a tx for the mods to both packobj and bigobj.
4337 tx = dmu_tx_create(os);
4339 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4340 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4342 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4344 umem_free(packbuf, packsize);
4345 umem_free(bigbuf, bigsize);
4346 for (j = 0; j < s; j++) {
4348 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4349 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4352 bigbuf_arcbufs[2 * j]);
4354 bigbuf_arcbufs[2 * j + 1]);
4357 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4358 umem_free(od, size);
4359 dmu_buf_rele(bonus_db, FTAG);
4364 * 50% of the time don't read objects in the 1st iteration to
4365 * test dmu_assign_arcbuf() for the case when there're no
4366 * existing dbufs for the specified offsets.
4368 if (i != 0 || ztest_random(2) != 0) {
4369 error = dmu_read(os, packobj, packoff,
4370 packsize, packbuf, DMU_READ_PREFETCH);
4372 error = dmu_read(os, bigobj, bigoff, bigsize,
4373 bigbuf, DMU_READ_PREFETCH);
4376 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4380 * We've verified all the old bufwads, and made new ones.
4381 * Now write them out.
4383 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4384 if (ztest_opts.zo_verbose >= 7) {
4385 (void) printf("writing offset %llx size %llx"
4387 (u_longlong_t)bigoff,
4388 (u_longlong_t)bigsize,
4391 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4393 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4394 bcopy((caddr_t)bigbuf + (off - bigoff),
4395 bigbuf_arcbufs[j]->b_data, chunksize);
4397 bcopy((caddr_t)bigbuf + (off - bigoff),
4398 bigbuf_arcbufs[2 * j]->b_data,
4400 bcopy((caddr_t)bigbuf + (off - bigoff) +
4402 bigbuf_arcbufs[2 * j + 1]->b_data,
4407 VERIFY(dmu_buf_hold(os, bigobj, off,
4408 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4410 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4411 dmu_assign_arcbuf(bonus_db, off,
4412 bigbuf_arcbufs[j], tx);
4414 dmu_assign_arcbuf(bonus_db, off,
4415 bigbuf_arcbufs[2 * j], tx);
4416 dmu_assign_arcbuf(bonus_db,
4417 off + chunksize / 2,
4418 bigbuf_arcbufs[2 * j + 1], tx);
4421 dmu_buf_rele(dbt, FTAG);
4427 * Sanity check the stuff we just wrote.
4430 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4431 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4433 VERIFY(0 == dmu_read(os, packobj, packoff,
4434 packsize, packcheck, DMU_READ_PREFETCH));
4435 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4436 bigsize, bigcheck, DMU_READ_PREFETCH));
4438 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4439 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4441 umem_free(packcheck, packsize);
4442 umem_free(bigcheck, bigsize);
4445 txg_wait_open(dmu_objset_pool(os), 0);
4446 } else if (i == 3) {
4447 txg_wait_synced(dmu_objset_pool(os), 0);
4451 dmu_buf_rele(bonus_db, FTAG);
4452 umem_free(packbuf, packsize);
4453 umem_free(bigbuf, bigsize);
4454 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4455 umem_free(od, size);
4460 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4464 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4465 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4466 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4469 * Have multiple threads write to large offsets in an object
4470 * to verify that parallel writes to an object -- even to the
4471 * same blocks within the object -- doesn't cause any trouble.
4473 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4475 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4478 while (ztest_random(10) != 0)
4479 ztest_io(zd, od->od_object, offset);
4481 umem_free(od, sizeof (ztest_od_t));
4485 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4488 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4489 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4490 uint64_t count = ztest_random(20) + 1;
4491 uint64_t blocksize = ztest_random_blocksize();
4494 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4496 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4498 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4499 !ztest_random(2)) != 0) {
4500 umem_free(od, sizeof (ztest_od_t));
4504 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4505 umem_free(od, sizeof (ztest_od_t));
4509 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4511 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4513 while (ztest_random(count) != 0) {
4514 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4515 if (ztest_write(zd, od->od_object, randoff, blocksize,
4518 while (ztest_random(4) != 0)
4519 ztest_io(zd, od->od_object, randoff);
4522 umem_free(data, blocksize);
4523 umem_free(od, sizeof (ztest_od_t));
4527 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4529 #define ZTEST_ZAP_MIN_INTS 1
4530 #define ZTEST_ZAP_MAX_INTS 4
4531 #define ZTEST_ZAP_MAX_PROPS 1000
4534 ztest_zap(ztest_ds_t *zd, uint64_t id)
4536 objset_t *os = zd->zd_os;
4539 uint64_t txg, last_txg;
4540 uint64_t value[ZTEST_ZAP_MAX_INTS];
4541 uint64_t zl_ints, zl_intsize, prop;
4544 char propname[100], txgname[100];
4546 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4548 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4549 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4551 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4552 !ztest_random(2)) != 0)
4555 object = od->od_object;
4558 * Generate a known hash collision, and verify that
4559 * we can lookup and remove both entries.
4561 tx = dmu_tx_create(os);
4562 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4563 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4566 for (i = 0; i < 2; i++) {
4568 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4571 for (i = 0; i < 2; i++) {
4572 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4573 sizeof (uint64_t), 1, &value[i], tx));
4575 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4576 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4577 ASSERT3U(zl_ints, ==, 1);
4579 for (i = 0; i < 2; i++) {
4580 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4585 * Generate a buch of random entries.
4587 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4589 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4590 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4591 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4592 bzero(value, sizeof (value));
4596 * If these zap entries already exist, validate their contents.
4598 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4600 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4601 ASSERT3U(zl_ints, ==, 1);
4603 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4604 zl_ints, &last_txg) == 0);
4606 VERIFY(zap_length(os, object, propname, &zl_intsize,
4609 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4610 ASSERT3U(zl_ints, ==, ints);
4612 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4613 zl_ints, value) == 0);
4615 for (i = 0; i < ints; i++) {
4616 ASSERT3U(value[i], ==, last_txg + object + i);
4619 ASSERT3U(error, ==, ENOENT);
4623 * Atomically update two entries in our zap object.
4624 * The first is named txg_%llu, and contains the txg
4625 * in which the property was last updated. The second
4626 * is named prop_%llu, and the nth element of its value
4627 * should be txg + object + n.
4629 tx = dmu_tx_create(os);
4630 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4631 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4636 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4638 for (i = 0; i < ints; i++)
4639 value[i] = txg + object + i;
4641 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4643 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4649 * Remove a random pair of entries.
4651 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4652 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4653 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4655 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4657 if (error == ENOENT)
4662 tx = dmu_tx_create(os);
4663 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4664 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4667 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4668 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4671 umem_free(od, sizeof (ztest_od_t));
4675 * Testcase to test the upgrading of a microzap to fatzap.
4678 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4680 objset_t *os = zd->zd_os;
4682 uint64_t object, txg;
4685 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4686 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4688 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4689 !ztest_random(2)) != 0)
4691 object = od->od_object;
4694 * Add entries to this ZAP and make sure it spills over
4695 * and gets upgraded to a fatzap. Also, since we are adding
4696 * 2050 entries we should see ptrtbl growth and leaf-block split.
4698 for (i = 0; i < 2050; i++) {
4699 char name[ZFS_MAX_DATASET_NAME_LEN];
4704 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4705 (u_longlong_t)id, (u_longlong_t)value);
4707 tx = dmu_tx_create(os);
4708 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4709 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4712 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4714 ASSERT(error == 0 || error == EEXIST);
4718 umem_free(od, sizeof (ztest_od_t));
4723 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4725 objset_t *os = zd->zd_os;
4727 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4729 int i, namelen, error;
4730 int micro = ztest_random(2);
4731 char name[20], string_value[20];
4734 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4735 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4737 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4738 umem_free(od, sizeof (ztest_od_t));
4742 object = od->od_object;
4745 * Generate a random name of the form 'xxx.....' where each
4746 * x is a random printable character and the dots are dots.
4747 * There are 94 such characters, and the name length goes from
4748 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4750 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4752 for (i = 0; i < 3; i++)
4753 name[i] = '!' + ztest_random('~' - '!' + 1);
4754 for (; i < namelen - 1; i++)
4758 if ((namelen & 1) || micro) {
4759 wsize = sizeof (txg);
4765 data = string_value;
4769 VERIFY0(zap_count(os, object, &count));
4770 ASSERT(count != -1ULL);
4773 * Select an operation: length, lookup, add, update, remove.
4775 i = ztest_random(5);
4778 tx = dmu_tx_create(os);
4779 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4780 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4782 umem_free(od, sizeof (ztest_od_t));
4785 bcopy(name, string_value, namelen);
4789 bzero(string_value, namelen);
4795 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4797 ASSERT3U(wsize, ==, zl_wsize);
4798 ASSERT3U(wc, ==, zl_wc);
4800 ASSERT3U(error, ==, ENOENT);
4805 error = zap_lookup(os, object, name, wsize, wc, data);
4807 if (data == string_value &&
4808 bcmp(name, data, namelen) != 0)
4809 fatal(0, "name '%s' != val '%s' len %d",
4810 name, data, namelen);
4812 ASSERT3U(error, ==, ENOENT);
4817 error = zap_add(os, object, name, wsize, wc, data, tx);
4818 ASSERT(error == 0 || error == EEXIST);
4822 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4826 error = zap_remove(os, object, name, tx);
4827 ASSERT(error == 0 || error == ENOENT);
4834 umem_free(od, sizeof (ztest_od_t));
4838 * Commit callback data.
4840 typedef struct ztest_cb_data {
4841 list_node_t zcd_node;
4843 int zcd_expected_err;
4844 boolean_t zcd_added;
4845 boolean_t zcd_called;
4849 /* This is the actual commit callback function */
4851 ztest_commit_callback(void *arg, int error)
4853 ztest_cb_data_t *data = arg;
4854 uint64_t synced_txg;
4856 VERIFY(data != NULL);
4857 VERIFY3S(data->zcd_expected_err, ==, error);
4858 VERIFY(!data->zcd_called);
4860 synced_txg = spa_last_synced_txg(data->zcd_spa);
4861 if (data->zcd_txg > synced_txg)
4862 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4863 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4866 data->zcd_called = B_TRUE;
4868 if (error == ECANCELED) {
4869 ASSERT0(data->zcd_txg);
4870 ASSERT(!data->zcd_added);
4873 * The private callback data should be destroyed here, but
4874 * since we are going to check the zcd_called field after
4875 * dmu_tx_abort(), we will destroy it there.
4880 ASSERT(data->zcd_added);
4881 ASSERT3U(data->zcd_txg, !=, 0);
4883 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4885 /* See if this cb was called more quickly */
4886 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4887 zc_min_txg_delay = synced_txg - data->zcd_txg;
4889 /* Remove our callback from the list */
4890 list_remove(&zcl.zcl_callbacks, data);
4892 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4894 umem_free(data, sizeof (ztest_cb_data_t));
4897 /* Allocate and initialize callback data structure */
4898 static ztest_cb_data_t *
4899 ztest_create_cb_data(objset_t *os, uint64_t txg)
4901 ztest_cb_data_t *cb_data;
4903 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4905 cb_data->zcd_txg = txg;
4906 cb_data->zcd_spa = dmu_objset_spa(os);
4907 list_link_init(&cb_data->zcd_node);
4913 * Commit callback test.
4916 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4918 objset_t *os = zd->zd_os;
4921 ztest_cb_data_t *cb_data[3], *tmp_cb;
4922 uint64_t old_txg, txg;
4925 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4926 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4928 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4929 umem_free(od, sizeof (ztest_od_t));
4933 tx = dmu_tx_create(os);
4935 cb_data[0] = ztest_create_cb_data(os, 0);
4936 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4938 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4940 /* Every once in a while, abort the transaction on purpose */
4941 if (ztest_random(100) == 0)
4945 error = dmu_tx_assign(tx, TXG_NOWAIT);
4947 txg = error ? 0 : dmu_tx_get_txg(tx);
4949 cb_data[0]->zcd_txg = txg;
4950 cb_data[1] = ztest_create_cb_data(os, txg);
4951 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4955 * It's not a strict requirement to call the registered
4956 * callbacks from inside dmu_tx_abort(), but that's what
4957 * it's supposed to happen in the current implementation
4958 * so we will check for that.
4960 for (i = 0; i < 2; i++) {
4961 cb_data[i]->zcd_expected_err = ECANCELED;
4962 VERIFY(!cb_data[i]->zcd_called);
4967 for (i = 0; i < 2; i++) {
4968 VERIFY(cb_data[i]->zcd_called);
4969 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4972 umem_free(od, sizeof (ztest_od_t));
4976 cb_data[2] = ztest_create_cb_data(os, txg);
4977 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4980 * Read existing data to make sure there isn't a future leak.
4982 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4983 &old_txg, DMU_READ_PREFETCH));
4986 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4989 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4991 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4994 * Since commit callbacks don't have any ordering requirement and since
4995 * it is theoretically possible for a commit callback to be called
4996 * after an arbitrary amount of time has elapsed since its txg has been
4997 * synced, it is difficult to reliably determine whether a commit
4998 * callback hasn't been called due to high load or due to a flawed
5001 * In practice, we will assume that if after a certain number of txgs a
5002 * commit callback hasn't been called, then most likely there's an
5003 * implementation bug..
5005 tmp_cb = list_head(&zcl.zcl_callbacks);
5006 if (tmp_cb != NULL &&
5007 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
5008 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5009 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5013 * Let's find the place to insert our callbacks.
5015 * Even though the list is ordered by txg, it is possible for the
5016 * insertion point to not be the end because our txg may already be
5017 * quiescing at this point and other callbacks in the open txg
5018 * (from other objsets) may have sneaked in.
5020 tmp_cb = list_tail(&zcl.zcl_callbacks);
5021 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5022 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5024 /* Add the 3 callbacks to the list */
5025 for (i = 0; i < 3; i++) {
5027 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5029 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5032 cb_data[i]->zcd_added = B_TRUE;
5033 VERIFY(!cb_data[i]->zcd_called);
5035 tmp_cb = cb_data[i];
5040 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5044 umem_free(od, sizeof (ztest_od_t));
5048 * Visit each object in the dataset. Verify that its properties
5049 * are consistent what was stored in the block tag when it was created,
5050 * and that its unused bonus buffer space has not been overwritten.
5054 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5056 objset_t *os = zd->zd_os;
5060 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5061 ztest_block_tag_t *bt = NULL;
5062 dmu_object_info_t doi;
5065 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
5068 dmu_object_info_from_db(db, &doi);
5069 if (doi.doi_bonus_size >= sizeof (*bt))
5070 bt = ztest_bt_bonus(db);
5072 if (bt && bt->bt_magic == BT_MAGIC) {
5073 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5074 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5076 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5079 dmu_buf_rele(db, FTAG);
5085 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5087 zfs_prop_t proplist[] = {
5089 ZFS_PROP_COMPRESSION,
5095 (void) rw_rdlock(&ztest_name_lock);
5097 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5098 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5099 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5101 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5102 ztest_random_blocksize(), (int)ztest_random(2)));
5104 (void) rw_unlock(&ztest_name_lock);
5109 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5111 nvlist_t *props = NULL;
5113 (void) rw_rdlock(&ztest_name_lock);
5115 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5116 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5118 VERIFY0(spa_prop_get(ztest_spa, &props));
5120 if (ztest_opts.zo_verbose >= 6)
5121 dump_nvlist(props, 4);
5125 (void) rw_unlock(&ztest_name_lock);
5129 user_release_one(const char *snapname, const char *holdname)
5131 nvlist_t *snaps, *holds;
5134 snaps = fnvlist_alloc();
5135 holds = fnvlist_alloc();
5136 fnvlist_add_boolean(holds, holdname);
5137 fnvlist_add_nvlist(snaps, snapname, holds);
5138 fnvlist_free(holds);
5139 error = dsl_dataset_user_release(snaps, NULL);
5140 fnvlist_free(snaps);
5145 * Test snapshot hold/release and deferred destroy.
5148 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5151 objset_t *os = zd->zd_os;
5155 char clonename[100];
5157 char osname[ZFS_MAX_DATASET_NAME_LEN];
5160 (void) rw_rdlock(&ztest_name_lock);
5162 dmu_objset_name(os, osname);
5164 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5166 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5167 (void) snprintf(clonename, sizeof (clonename),
5168 "%s/ch1_%llu", osname, (u_longlong_t)id);
5169 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5172 * Clean up from any previous run.
5174 error = dsl_destroy_head(clonename);
5175 if (error != ENOENT)
5177 error = user_release_one(fullname, tag);
5178 if (error != ESRCH && error != ENOENT)
5180 error = dsl_destroy_snapshot(fullname, B_FALSE);
5181 if (error != ENOENT)
5185 * Create snapshot, clone it, mark snap for deferred destroy,
5186 * destroy clone, verify snap was also destroyed.
5188 error = dmu_objset_snapshot_one(osname, snapname);
5190 if (error == ENOSPC) {
5191 ztest_record_enospc("dmu_objset_snapshot");
5194 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5197 error = dmu_objset_clone(clonename, fullname);
5199 if (error == ENOSPC) {
5200 ztest_record_enospc("dmu_objset_clone");
5203 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5206 error = dsl_destroy_snapshot(fullname, B_TRUE);
5208 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5212 error = dsl_destroy_head(clonename);
5214 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5216 error = dmu_objset_hold(fullname, FTAG, &origin);
5217 if (error != ENOENT)
5218 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5221 * Create snapshot, add temporary hold, verify that we can't
5222 * destroy a held snapshot, mark for deferred destroy,
5223 * release hold, verify snapshot was destroyed.
5225 error = dmu_objset_snapshot_one(osname, snapname);
5227 if (error == ENOSPC) {
5228 ztest_record_enospc("dmu_objset_snapshot");
5231 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5234 holds = fnvlist_alloc();
5235 fnvlist_add_string(holds, fullname, tag);
5236 error = dsl_dataset_user_hold(holds, 0, NULL);
5237 fnvlist_free(holds);
5239 if (error == ENOSPC) {
5240 ztest_record_enospc("dsl_dataset_user_hold");
5243 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5244 fullname, tag, error);
5247 error = dsl_destroy_snapshot(fullname, B_FALSE);
5248 if (error != EBUSY) {
5249 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5253 error = dsl_destroy_snapshot(fullname, B_TRUE);
5255 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5259 error = user_release_one(fullname, tag);
5261 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5263 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5266 (void) rw_unlock(&ztest_name_lock);
5270 * Inject random faults into the on-disk data.
5274 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5276 ztest_shared_t *zs = ztest_shared;
5277 spa_t *spa = ztest_spa;
5281 uint64_t bad = 0x1990c0ffeedecadeull;
5286 int bshift = SPA_MAXBLOCKSHIFT + 2;
5292 boolean_t islog = B_FALSE;
5294 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5295 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5297 mutex_enter(&ztest_vdev_lock);
5298 maxfaults = MAXFAULTS();
5299 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5300 mirror_save = zs->zs_mirrors;
5301 mutex_exit(&ztest_vdev_lock);
5303 ASSERT(leaves >= 1);
5306 * Grab the name lock as reader. There are some operations
5307 * which don't like to have their vdevs changed while
5308 * they are in progress (i.e. spa_change_guid). Those
5309 * operations will have grabbed the name lock as writer.
5311 (void) rw_rdlock(&ztest_name_lock);
5314 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5316 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5318 if (ztest_random(2) == 0) {
5320 * Inject errors on a normal data device or slog device.
5322 top = ztest_random_vdev_top(spa, B_TRUE);
5323 leaf = ztest_random(leaves) + zs->zs_splits;
5326 * Generate paths to the first leaf in this top-level vdev,
5327 * and to the random leaf we selected. We'll induce transient
5328 * write failures and random online/offline activity on leaf 0,
5329 * and we'll write random garbage to the randomly chosen leaf.
5331 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5332 ztest_opts.zo_dir, ztest_opts.zo_pool,
5333 top * leaves + zs->zs_splits);
5334 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5335 ztest_opts.zo_dir, ztest_opts.zo_pool,
5336 top * leaves + leaf);
5338 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5339 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5343 * If the top-level vdev needs to be resilvered
5344 * then we only allow faults on the device that is
5347 if (vd0 != NULL && maxfaults != 1 &&
5348 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5349 vd0->vdev_resilver_txg != 0)) {
5351 * Make vd0 explicitly claim to be unreadable,
5352 * or unwriteable, or reach behind its back
5353 * and close the underlying fd. We can do this if
5354 * maxfaults == 0 because we'll fail and reexecute,
5355 * and we can do it if maxfaults >= 2 because we'll
5356 * have enough redundancy. If maxfaults == 1, the
5357 * combination of this with injection of random data
5358 * corruption below exceeds the pool's fault tolerance.
5360 vdev_file_t *vf = vd0->vdev_tsd;
5362 if (vf != NULL && ztest_random(3) == 0) {
5363 (void) close(vf->vf_vnode->v_fd);
5364 vf->vf_vnode->v_fd = -1;
5365 } else if (ztest_random(2) == 0) {
5366 vd0->vdev_cant_read = B_TRUE;
5368 vd0->vdev_cant_write = B_TRUE;
5370 guid0 = vd0->vdev_guid;
5374 * Inject errors on an l2cache device.
5376 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5378 if (sav->sav_count == 0) {
5379 spa_config_exit(spa, SCL_STATE, FTAG);
5380 (void) rw_unlock(&ztest_name_lock);
5383 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5384 guid0 = vd0->vdev_guid;
5385 (void) strcpy(path0, vd0->vdev_path);
5386 (void) strcpy(pathrand, vd0->vdev_path);
5390 maxfaults = INT_MAX; /* no limit on cache devices */
5393 spa_config_exit(spa, SCL_STATE, FTAG);
5394 (void) rw_unlock(&ztest_name_lock);
5397 * If we can tolerate two or more faults, or we're dealing
5398 * with a slog, randomly online/offline vd0.
5400 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5401 if (ztest_random(10) < 6) {
5402 int flags = (ztest_random(2) == 0 ?
5403 ZFS_OFFLINE_TEMPORARY : 0);
5406 * We have to grab the zs_name_lock as writer to
5407 * prevent a race between offlining a slog and
5408 * destroying a dataset. Offlining the slog will
5409 * grab a reference on the dataset which may cause
5410 * dsl_destroy_head() to fail with EBUSY thus
5411 * leaving the dataset in an inconsistent state.
5414 (void) rw_wrlock(&ztest_name_lock);
5416 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5419 (void) rw_unlock(&ztest_name_lock);
5422 * Ideally we would like to be able to randomly
5423 * call vdev_[on|off]line without holding locks
5424 * to force unpredictable failures but the side
5425 * effects of vdev_[on|off]line prevent us from
5426 * doing so. We grab the ztest_vdev_lock here to
5427 * prevent a race between injection testing and
5430 mutex_enter(&ztest_vdev_lock);
5431 (void) vdev_online(spa, guid0, 0, NULL);
5432 mutex_exit(&ztest_vdev_lock);
5440 * We have at least single-fault tolerance, so inject data corruption.
5442 fd = open(pathrand, O_RDWR);
5444 if (fd == -1) /* we hit a gap in the device namespace */
5447 fsize = lseek(fd, 0, SEEK_END);
5449 while (--iters != 0) {
5451 * The offset must be chosen carefully to ensure that
5452 * we do not inject a given logical block with errors
5453 * on two different leaf devices, because ZFS can not
5454 * tolerate that (if maxfaults==1).
5456 * We divide each leaf into chunks of size
5457 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5458 * there is a series of ranges to which we can inject errors.
5459 * Each range can accept errors on only a single leaf vdev.
5460 * The error injection ranges are separated by ranges
5461 * which we will not inject errors on any device (DMZs).
5462 * Each DMZ must be large enough such that a single block
5463 * can not straddle it, so that a single block can not be
5464 * a target in two different injection ranges (on different
5467 * For example, with 3 leaves, each chunk looks like:
5468 * 0 to 32M: injection range for leaf 0
5469 * 32M to 64M: DMZ - no injection allowed
5470 * 64M to 96M: injection range for leaf 1
5471 * 96M to 128M: DMZ - no injection allowed
5472 * 128M to 160M: injection range for leaf 2
5473 * 160M to 192M: DMZ - no injection allowed
5475 offset = ztest_random(fsize / (leaves << bshift)) *
5476 (leaves << bshift) + (leaf << bshift) +
5477 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5480 * Only allow damage to the labels at one end of the vdev.
5482 * If all labels are damaged, the device will be totally
5483 * inaccessible, which will result in loss of data,
5484 * because we also damage (parts of) the other side of
5487 * Additionally, we will always have both an even and an
5488 * odd label, so that we can handle crashes in the
5489 * middle of vdev_config_sync().
5491 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5495 * The two end labels are stored at the "end" of the disk, but
5496 * the end of the disk (vdev_psize) is aligned to
5497 * sizeof (vdev_label_t).
5499 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5500 if ((leaf & 1) == 1 &&
5501 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5504 mutex_enter(&ztest_vdev_lock);
5505 if (mirror_save != zs->zs_mirrors) {
5506 mutex_exit(&ztest_vdev_lock);
5511 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5512 fatal(1, "can't inject bad word at 0x%llx in %s",
5515 mutex_exit(&ztest_vdev_lock);
5517 if (ztest_opts.zo_verbose >= 7)
5518 (void) printf("injected bad word into %s,"
5519 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5524 umem_free(path0, MAXPATHLEN);
5525 umem_free(pathrand, MAXPATHLEN);
5529 * Verify that DDT repair works as expected.
5532 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5534 ztest_shared_t *zs = ztest_shared;
5535 spa_t *spa = ztest_spa;
5536 objset_t *os = zd->zd_os;
5538 uint64_t object, blocksize, txg, pattern, psize;
5539 enum zio_checksum checksum = spa_dedup_checksum(spa);
5544 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5547 blocksize = ztest_random_blocksize();
5548 blocksize = MIN(blocksize, 2048); /* because we write so many */
5550 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5551 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5553 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5554 umem_free(od, sizeof (ztest_od_t));
5559 * Take the name lock as writer to prevent anyone else from changing
5560 * the pool and dataset properies we need to maintain during this test.
5562 (void) rw_wrlock(&ztest_name_lock);
5564 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5566 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5568 (void) rw_unlock(&ztest_name_lock);
5569 umem_free(od, sizeof (ztest_od_t));
5573 dmu_objset_stats_t dds;
5574 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5575 dmu_objset_fast_stat(os, &dds);
5576 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5578 object = od[0].od_object;
5579 blocksize = od[0].od_blocksize;
5580 pattern = zs->zs_guid ^ dds.dds_guid;
5582 ASSERT(object != 0);
5584 tx = dmu_tx_create(os);
5585 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5586 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5588 (void) rw_unlock(&ztest_name_lock);
5589 umem_free(od, sizeof (ztest_od_t));
5594 * Write all the copies of our block.
5596 for (i = 0; i < copies; i++) {
5597 uint64_t offset = i * blocksize;
5598 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5599 DMU_READ_NO_PREFETCH);
5601 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5602 os, (long long)object, (long long) offset, error);
5604 ASSERT(db->db_offset == offset);
5605 ASSERT(db->db_size == blocksize);
5606 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5607 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5608 dmu_buf_will_fill(db, tx);
5609 ztest_pattern_set(db->db_data, db->db_size, pattern);
5610 dmu_buf_rele(db, FTAG);
5614 txg_wait_synced(spa_get_dsl(spa), txg);
5617 * Find out what block we got.
5619 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5620 DMU_READ_NO_PREFETCH));
5621 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5622 dmu_buf_rele(db, FTAG);
5625 * Damage the block. Dedup-ditto will save us when we read it later.
5627 psize = BP_GET_PSIZE(&blk);
5628 abd = abd_alloc_linear(psize, B_TRUE);
5629 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5631 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5632 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5633 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5637 (void) rw_unlock(&ztest_name_lock);
5638 umem_free(od, sizeof (ztest_od_t));
5646 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5648 spa_t *spa = ztest_spa;
5650 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5651 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5652 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5656 * Change the guid for the pool.
5660 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5662 spa_t *spa = ztest_spa;
5663 uint64_t orig, load;
5666 orig = spa_guid(spa);
5667 load = spa_load_guid(spa);
5669 (void) rw_wrlock(&ztest_name_lock);
5670 error = spa_change_guid(spa);
5671 (void) rw_unlock(&ztest_name_lock);
5676 if (ztest_opts.zo_verbose >= 4) {
5677 (void) printf("Changed guid old %llu -> %llu\n",
5678 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5681 VERIFY3U(orig, !=, spa_guid(spa));
5682 VERIFY3U(load, ==, spa_load_guid(spa));
5686 * Rename the pool to a different name and then rename it back.
5690 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5692 char *oldname, *newname;
5695 if (ztest_opts.zo_mmp_test)
5698 (void) rw_wrlock(&ztest_name_lock);
5700 oldname = ztest_opts.zo_pool;
5701 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5702 (void) strcpy(newname, oldname);
5703 (void) strcat(newname, "_tmp");
5708 VERIFY3U(0, ==, spa_rename(oldname, newname));
5711 * Try to open it under the old name, which shouldn't exist
5713 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5716 * Open it under the new name and make sure it's still the same spa_t.
5718 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5720 ASSERT(spa == ztest_spa);
5721 spa_close(spa, FTAG);
5724 * Rename it back to the original
5726 VERIFY3U(0, ==, spa_rename(newname, oldname));
5729 * Make sure it can still be opened
5731 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5733 ASSERT(spa == ztest_spa);
5734 spa_close(spa, FTAG);
5736 umem_free(newname, strlen(newname) + 1);
5738 (void) rw_unlock(&ztest_name_lock);
5742 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5744 hrtime_t end = gethrtime() + NANOSEC;
5746 while (gethrtime() <= end) {
5747 int run_count = 100;
5749 struct abd *abd_data, *abd_meta;
5754 zio_cksum_t zc_ref_byteswap;
5756 size = ztest_random_blocksize();
5758 buf = umem_alloc(size, UMEM_NOFAIL);
5759 abd_data = abd_alloc(size, B_FALSE);
5760 abd_meta = abd_alloc(size, B_TRUE);
5762 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5763 *ptr = ztest_random(UINT_MAX);
5765 abd_copy_from_buf_off(abd_data, buf, 0, size);
5766 abd_copy_from_buf_off(abd_meta, buf, 0, size);
5768 VERIFY0(fletcher_4_impl_set("scalar"));
5769 fletcher_4_native(buf, size, NULL, &zc_ref);
5770 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
5772 VERIFY0(fletcher_4_impl_set("cycle"));
5773 while (run_count-- > 0) {
5775 zio_cksum_t zc_byteswap;
5777 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5778 fletcher_4_native(buf, size, NULL, &zc);
5780 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5781 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5782 sizeof (zc_byteswap)));
5784 /* Test ABD - data */
5785 abd_fletcher_4_byteswap(abd_data, size, NULL,
5787 abd_fletcher_4_native(abd_data, size, NULL, &zc);
5789 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5790 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5791 sizeof (zc_byteswap)));
5793 /* Test ABD - metadata */
5794 abd_fletcher_4_byteswap(abd_meta, size, NULL,
5796 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
5798 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5799 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5800 sizeof (zc_byteswap)));
5804 umem_free(buf, size);
5811 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5818 zio_cksum_t zc_ref_bswap;
5820 hrtime_t end = gethrtime() + NANOSEC;
5822 while (gethrtime() <= end) {
5823 int run_count = 100;
5825 size = ztest_random_blocksize();
5826 buf = umem_alloc(size, UMEM_NOFAIL);
5828 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5829 *ptr = ztest_random(UINT_MAX);
5831 VERIFY0(fletcher_4_impl_set("scalar"));
5832 fletcher_4_native(buf, size, NULL, &zc_ref);
5833 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5835 VERIFY0(fletcher_4_impl_set("cycle"));
5837 while (run_count-- > 0) {
5839 zio_cksum_t zc_bswap;
5842 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5843 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5845 while (pos < size) {
5846 size_t inc = 64 * ztest_random(size / 67);
5847 /* sometimes add few bytes to test non-simd */
5848 if (ztest_random(100) < 10)
5849 inc += P2ALIGN(ztest_random(64),
5852 if (inc > (size - pos))
5855 fletcher_4_incremental_native(buf + pos, inc,
5857 fletcher_4_incremental_byteswap(buf + pos, inc,
5863 VERIFY3U(pos, ==, size);
5865 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5866 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5869 * verify if incremental on the whole buffer is
5870 * equivalent to non-incremental version
5872 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5873 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5875 fletcher_4_incremental_native(buf, size, &zc);
5876 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
5878 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5879 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5882 umem_free(buf, size);
5887 ztest_check_path(char *path)
5890 /* return true on success */
5891 return (!stat(path, &s));
5895 ztest_get_zdb_bin(char *bin, int len)
5899 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5900 * let popen to search through PATH.
5902 if ((zdb_path = getenv("ZDB_PATH"))) {
5903 strlcpy(bin, zdb_path, len); /* In env */
5904 if (!ztest_check_path(bin)) {
5905 ztest_dump_core = 0;
5906 fatal(1, "invalid ZDB_PATH '%s'", bin);
5911 VERIFY(realpath(getexecname(), bin) != NULL);
5912 if (strstr(bin, "/ztest/")) {
5913 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5914 strcat(bin, "/zdb/zdb");
5915 if (ztest_check_path(bin))
5922 * Verify pool integrity by running zdb.
5925 ztest_run_zdb(char *pool)
5931 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5934 bin = umem_alloc(len, UMEM_NOFAIL);
5935 zdb = umem_alloc(len, UMEM_NOFAIL);
5936 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5938 ztest_get_zdb_bin(bin, len);
5941 "%s -bcc%s%s -G -d -U %s %s",
5943 ztest_opts.zo_verbose >= 3 ? "s" : "",
5944 ztest_opts.zo_verbose >= 4 ? "v" : "",
5948 if (ztest_opts.zo_verbose >= 5)
5949 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5951 fp = popen(zdb, "r");
5953 while (fgets(zbuf, 1024, fp) != NULL)
5954 if (ztest_opts.zo_verbose >= 3)
5955 (void) printf("%s", zbuf);
5957 status = pclose(fp);
5962 ztest_dump_core = 0;
5963 if (WIFEXITED(status))
5964 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5966 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5968 umem_free(bin, len);
5969 umem_free(zdb, len);
5970 umem_free(zbuf, 1024);
5974 ztest_walk_pool_directory(char *header)
5978 if (ztest_opts.zo_verbose >= 6)
5979 (void) printf("%s\n", header);
5981 mutex_enter(&spa_namespace_lock);
5982 while ((spa = spa_next(spa)) != NULL)
5983 if (ztest_opts.zo_verbose >= 6)
5984 (void) printf("\t%s\n", spa_name(spa));
5985 mutex_exit(&spa_namespace_lock);
5989 ztest_spa_import_export(char *oldname, char *newname)
5991 nvlist_t *config, *newconfig;
5996 if (ztest_opts.zo_verbose >= 4) {
5997 (void) printf("import/export: old = %s, new = %s\n",
6002 * Clean up from previous runs.
6004 (void) spa_destroy(newname);
6007 * Get the pool's configuration and guid.
6009 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6012 * Kick off a scrub to tickle scrub/export races.
6014 if (ztest_random(2) == 0)
6015 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6017 pool_guid = spa_guid(spa);
6018 spa_close(spa, FTAG);
6020 ztest_walk_pool_directory("pools before export");
6025 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6027 ztest_walk_pool_directory("pools after export");
6032 newconfig = spa_tryimport(config);
6033 ASSERT(newconfig != NULL);
6034 nvlist_free(newconfig);
6037 * Import it under the new name.
6039 error = spa_import(newname, config, NULL, 0);
6041 dump_nvlist(config, 0);
6042 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6043 oldname, newname, error);
6046 ztest_walk_pool_directory("pools after import");
6049 * Try to import it again -- should fail with EEXIST.
6051 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6054 * Try to import it under a different name -- should fail with EEXIST.
6056 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6059 * Verify that the pool is no longer visible under the old name.
6061 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6064 * Verify that we can open and close the pool using the new name.
6066 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6067 ASSERT(pool_guid == spa_guid(spa));
6068 spa_close(spa, FTAG);
6070 nvlist_free(config);
6074 ztest_resume(spa_t *spa)
6076 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6077 (void) printf("resuming from suspended state\n");
6078 spa_vdev_state_enter(spa, SCL_NONE);
6079 vdev_clear(spa, NULL);
6080 (void) spa_vdev_state_exit(spa, NULL, 0);
6081 (void) zio_resume(spa);
6085 ztest_resume_thread(void *arg)
6089 while (!ztest_exiting) {
6090 if (spa_suspended(spa))
6092 (void) poll(NULL, 0, 100);
6095 * Periodically change the zfs_compressed_arc_enabled setting.
6097 if (ztest_random(10) == 0)
6098 zfs_compressed_arc_enabled = ztest_random(2);
6101 * Periodically change the zfs_abd_scatter_enabled setting.
6103 if (ztest_random(10) == 0)
6104 zfs_abd_scatter_enabled = ztest_random(2);
6114 ztest_deadman_alarm(int sig)
6116 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
6121 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6123 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6124 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6125 hrtime_t functime = gethrtime();
6128 for (i = 0; i < zi->zi_iters; i++)
6129 zi->zi_func(zd, id);
6131 functime = gethrtime() - functime;
6133 atomic_add_64(&zc->zc_count, 1);
6134 atomic_add_64(&zc->zc_time, functime);
6136 if (ztest_opts.zo_verbose >= 4)
6137 (void) printf("%6.2f sec in %s\n",
6138 (double)functime / NANOSEC, zi->zi_funcname);
6142 ztest_thread(void *arg)
6145 uint64_t id = (uintptr_t)arg;
6146 ztest_shared_t *zs = ztest_shared;
6150 ztest_shared_callstate_t *zc;
6152 while ((now = gethrtime()) < zs->zs_thread_stop) {
6154 * See if it's time to force a crash.
6156 if (now > zs->zs_thread_kill)
6160 * If we're getting ENOSPC with some regularity, stop.
6162 if (zs->zs_enospc_count > 10)
6166 * Pick a random function to execute.
6168 rand = ztest_random(ZTEST_FUNCS);
6169 zi = &ztest_info[rand];
6170 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6171 call_next = zc->zc_next;
6173 if (now >= call_next &&
6174 atomic_cas_64(&zc->zc_next, call_next, call_next +
6175 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6176 ztest_execute(rand, zi, id);
6184 ztest_dataset_name(char *dsname, char *pool, int d)
6186 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6190 ztest_dataset_destroy(int d)
6192 char name[ZFS_MAX_DATASET_NAME_LEN];
6195 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6197 if (ztest_opts.zo_verbose >= 3)
6198 (void) printf("Destroying %s to free up space\n", name);
6201 * Cleanup any non-standard clones and snapshots. In general,
6202 * ztest thread t operates on dataset (t % zopt_datasets),
6203 * so there may be more than one thing to clean up.
6205 for (t = d; t < ztest_opts.zo_threads;
6206 t += ztest_opts.zo_datasets)
6207 ztest_dsl_dataset_cleanup(name, t);
6209 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6210 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6214 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6216 uint64_t usedobjs, dirobjs, scratch;
6219 * ZTEST_DIROBJ is the object directory for the entire dataset.
6220 * Therefore, the number of objects in use should equal the
6221 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6222 * If not, we have an object leak.
6224 * Note that we can only check this in ztest_dataset_open(),
6225 * when the open-context and syncing-context values agree.
6226 * That's because zap_count() returns the open-context value,
6227 * while dmu_objset_space() returns the rootbp fill count.
6229 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6230 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6231 ASSERT3U(dirobjs + 1, ==, usedobjs);
6235 ztest_dataset_open(int d)
6237 ztest_ds_t *zd = &ztest_ds[d];
6238 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6241 char name[ZFS_MAX_DATASET_NAME_LEN];
6244 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6246 (void) rw_rdlock(&ztest_name_lock);
6248 error = ztest_dataset_create(name);
6249 if (error == ENOSPC) {
6250 (void) rw_unlock(&ztest_name_lock);
6251 ztest_record_enospc(FTAG);
6254 ASSERT(error == 0 || error == EEXIST);
6256 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6257 (void) rw_unlock(&ztest_name_lock);
6259 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6261 zilog = zd->zd_zilog;
6263 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6264 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6265 fatal(0, "missing log records: claimed %llu < committed %llu",
6266 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6268 ztest_dataset_dirobj_verify(zd);
6270 zil_replay(os, zd, ztest_replay_vector);
6272 ztest_dataset_dirobj_verify(zd);
6274 if (ztest_opts.zo_verbose >= 6)
6275 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6277 (u_longlong_t)zilog->zl_parse_blk_count,
6278 (u_longlong_t)zilog->zl_parse_lr_count,
6279 (u_longlong_t)zilog->zl_replaying_seq);
6281 zilog = zil_open(os, ztest_get_data);
6283 if (zilog->zl_replaying_seq != 0 &&
6284 zilog->zl_replaying_seq < committed_seq)
6285 fatal(0, "missing log records: replayed %llu < committed %llu",
6286 zilog->zl_replaying_seq, committed_seq);
6292 ztest_dataset_close(int d)
6294 ztest_ds_t *zd = &ztest_ds[d];
6296 zil_close(zd->zd_zilog);
6297 dmu_objset_disown(zd->zd_os, zd);
6303 * Kick off threads to run tests on all datasets in parallel.
6306 ztest_run(ztest_shared_t *zs)
6310 kthread_t *resume_thread;
6311 kthread_t **run_threads;
6316 ztest_exiting = B_FALSE;
6319 * Initialize parent/child shared state.
6321 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6322 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6324 zs->zs_thread_start = gethrtime();
6325 zs->zs_thread_stop =
6326 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6327 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6328 zs->zs_thread_kill = zs->zs_thread_stop;
6329 if (ztest_random(100) < ztest_opts.zo_killrate) {
6330 zs->zs_thread_kill -=
6331 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6334 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6336 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6337 offsetof(ztest_cb_data_t, zcd_node));
6342 kernel_init(FREAD | FWRITE);
6343 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6344 spa->spa_debug = B_TRUE;
6345 metaslab_preload_limit = ztest_random(20) + 1;
6348 dmu_objset_stats_t dds;
6349 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6350 DMU_OST_ANY, B_TRUE, FTAG, &os));
6351 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6352 dmu_objset_fast_stat(os, &dds);
6353 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6354 zs->zs_guid = dds.dds_guid;
6355 dmu_objset_disown(os, FTAG);
6357 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6360 * We don't expect the pool to suspend unless maxfaults == 0,
6361 * in which case ztest_fault_inject() temporarily takes away
6362 * the only valid replica.
6364 if (MAXFAULTS() == 0)
6365 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6367 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6370 * Create a thread to periodically resume suspended I/O.
6372 resume_thread = thread_create(NULL, 0, ztest_resume_thread,
6373 spa, 0, NULL, TS_RUN | TS_JOINABLE, defclsyspri);
6377 * Set a deadman alarm to abort() if we hang.
6379 signal(SIGALRM, ztest_deadman_alarm);
6380 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6384 * Verify that we can safely inquire about about any object,
6385 * whether it's allocated or not. To make it interesting,
6386 * we probe a 5-wide window around each power of two.
6387 * This hits all edge cases, including zero and the max.
6389 for (t = 0; t < 64; t++) {
6390 for (d = -5; d <= 5; d++) {
6391 error = dmu_object_info(spa->spa_meta_objset,
6392 (1ULL << t) + d, NULL);
6393 ASSERT(error == 0 || error == ENOENT ||
6399 * If we got any ENOSPC errors on the previous run, destroy something.
6401 if (zs->zs_enospc_count != 0) {
6402 int d = ztest_random(ztest_opts.zo_datasets);
6403 ztest_dataset_destroy(d);
6405 zs->zs_enospc_count = 0;
6407 run_threads = umem_zalloc(ztest_opts.zo_threads * sizeof (kthread_t *),
6410 if (ztest_opts.zo_verbose >= 4)
6411 (void) printf("starting main threads...\n");
6414 * Kick off all the tests that run in parallel.
6416 for (t = 0; t < ztest_opts.zo_threads; t++) {
6417 if (t < ztest_opts.zo_datasets && ztest_dataset_open(t) != 0) {
6418 umem_free(run_threads, ztest_opts.zo_threads *
6419 sizeof (kthread_t *));
6423 run_threads[t] = thread_create(NULL, 0, ztest_thread,
6424 (void *)(uintptr_t)t, 0, NULL, TS_RUN | TS_JOINABLE,
6429 * Wait for all of the tests to complete. We go in reverse order
6430 * so we don't close datasets while threads are still using them.
6432 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6433 VERIFY0(thread_join(run_threads[t]));
6434 if (t < ztest_opts.zo_datasets)
6435 ztest_dataset_close(t);
6438 txg_wait_synced(spa_get_dsl(spa), 0);
6440 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6441 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6443 umem_free(run_threads, ztest_opts.zo_threads * sizeof (kthread_t *));
6445 /* Kill the resume thread */
6446 ztest_exiting = B_TRUE;
6447 VERIFY0(thread_join(resume_thread));
6451 * Right before closing the pool, kick off a bunch of async I/O;
6452 * spa_close() should wait for it to complete.
6454 for (object = 1; object < 50; object++) {
6455 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6456 ZIO_PRIORITY_SYNC_READ);
6459 /* Verify that at least one commit cb was called in a timely fashion */
6460 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6461 VERIFY0(zc_min_txg_delay);
6463 spa_close(spa, FTAG);
6466 * Verify that we can loop over all pools.
6468 mutex_enter(&spa_namespace_lock);
6469 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6470 if (ztest_opts.zo_verbose > 3)
6471 (void) printf("spa_next: found %s\n", spa_name(spa));
6472 mutex_exit(&spa_namespace_lock);
6475 * Verify that we can export the pool and reimport it under a
6478 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6479 char name[ZFS_MAX_DATASET_NAME_LEN];
6480 (void) snprintf(name, sizeof (name), "%s_import",
6481 ztest_opts.zo_pool);
6482 ztest_spa_import_export(ztest_opts.zo_pool, name);
6483 ztest_spa_import_export(name, ztest_opts.zo_pool);
6488 list_destroy(&zcl.zcl_callbacks);
6489 mutex_destroy(&zcl.zcl_callbacks_lock);
6490 (void) rwlock_destroy(&ztest_name_lock);
6491 mutex_destroy(&ztest_vdev_lock);
6497 ztest_ds_t *zd = &ztest_ds[0];
6501 if (ztest_opts.zo_verbose >= 3)
6502 (void) printf("testing spa_freeze()...\n");
6504 kernel_init(FREAD | FWRITE);
6505 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6506 VERIFY3U(0, ==, ztest_dataset_open(0));
6507 spa->spa_debug = B_TRUE;
6511 * Force the first log block to be transactionally allocated.
6512 * We have to do this before we freeze the pool -- otherwise
6513 * the log chain won't be anchored.
6515 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6516 ztest_dmu_object_alloc_free(zd, 0);
6517 zil_commit(zd->zd_zilog, 0);
6520 txg_wait_synced(spa_get_dsl(spa), 0);
6523 * Freeze the pool. This stops spa_sync() from doing anything,
6524 * so that the only way to record changes from now on is the ZIL.
6529 * Because it is hard to predict how much space a write will actually
6530 * require beforehand, we leave ourselves some fudge space to write over
6533 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6536 * Run tests that generate log records but don't alter the pool config
6537 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6538 * We do a txg_wait_synced() after each iteration to force the txg
6539 * to increase well beyond the last synced value in the uberblock.
6540 * The ZIL should be OK with that.
6542 * Run a random number of times less than zo_maxloops and ensure we do
6543 * not run out of space on the pool.
6545 while (ztest_random(10) != 0 &&
6546 numloops++ < ztest_opts.zo_maxloops &&
6547 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6549 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6550 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6551 ztest_io(zd, od.od_object,
6552 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6553 txg_wait_synced(spa_get_dsl(spa), 0);
6557 * Commit all of the changes we just generated.
6559 zil_commit(zd->zd_zilog, 0);
6560 txg_wait_synced(spa_get_dsl(spa), 0);
6563 * Close our dataset and close the pool.
6565 ztest_dataset_close(0);
6566 spa_close(spa, FTAG);
6570 * Open and close the pool and dataset to induce log replay.
6572 kernel_init(FREAD | FWRITE);
6573 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6574 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6575 VERIFY3U(0, ==, ztest_dataset_open(0));
6576 ztest_dataset_close(0);
6578 spa->spa_debug = B_TRUE;
6580 txg_wait_synced(spa_get_dsl(spa), 0);
6581 ztest_reguid(NULL, 0);
6583 spa_close(spa, FTAG);
6588 print_time(hrtime_t t, char *timebuf)
6590 hrtime_t s = t / NANOSEC;
6591 hrtime_t m = s / 60;
6592 hrtime_t h = m / 60;
6593 hrtime_t d = h / 24;
6602 (void) sprintf(timebuf,
6603 "%llud%02lluh%02llum%02llus", d, h, m, s);
6605 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6607 (void) sprintf(timebuf, "%llum%02llus", m, s);
6609 (void) sprintf(timebuf, "%llus", s);
6613 make_random_props(void)
6617 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6618 if (ztest_random(2) == 0)
6620 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6626 * Import a storage pool with the given name.
6629 ztest_import(ztest_shared_t *zs)
6631 libzfs_handle_t *hdl;
6632 importargs_t args = { 0 };
6634 nvlist_t *cfg = NULL;
6636 char *searchdirs[nsearch];
6637 char *name = ztest_opts.zo_pool;
6638 int flags = ZFS_IMPORT_MISSING_LOG;
6641 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6642 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6644 kernel_init(FREAD | FWRITE);
6645 hdl = libzfs_init();
6647 searchdirs[0] = ztest_opts.zo_dir;
6648 args.paths = nsearch;
6649 args.path = searchdirs;
6650 args.can_be_active = B_FALSE;
6652 error = zpool_tryimport(hdl, name, &cfg, &args);
6654 (void) fatal(0, "No pools found\n");
6656 VERIFY0(spa_import(name, cfg, NULL, flags));
6657 VERIFY0(spa_open(name, &spa, FTAG));
6658 zs->zs_metaslab_sz =
6659 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6660 spa_close(spa, FTAG);
6665 if (!ztest_opts.zo_mmp_test) {
6666 ztest_run_zdb(ztest_opts.zo_pool);
6668 ztest_run_zdb(ztest_opts.zo_pool);
6671 (void) rwlock_destroy(&ztest_name_lock);
6672 mutex_destroy(&ztest_vdev_lock);
6676 * Create a storage pool with the given name and initial vdev size.
6677 * Then test spa_freeze() functionality.
6680 ztest_init(ztest_shared_t *zs)
6683 nvlist_t *nvroot, *props;
6686 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6687 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6689 kernel_init(FREAD | FWRITE);
6692 * Create the storage pool.
6694 (void) spa_destroy(ztest_opts.zo_pool);
6695 ztest_shared->zs_vdev_next_leaf = 0;
6697 zs->zs_mirrors = ztest_opts.zo_mirrors;
6698 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6699 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6700 props = make_random_props();
6701 for (i = 0; i < SPA_FEATURES; i++) {
6703 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6704 spa_feature_table[i].fi_uname));
6705 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6708 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6709 nvlist_free(nvroot);
6712 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6713 zs->zs_metaslab_sz =
6714 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6715 spa_close(spa, FTAG);
6719 if (!ztest_opts.zo_mmp_test) {
6720 ztest_run_zdb(ztest_opts.zo_pool);
6722 ztest_run_zdb(ztest_opts.zo_pool);
6725 (void) rwlock_destroy(&ztest_name_lock);
6726 mutex_destroy(&ztest_vdev_lock);
6732 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6734 ztest_fd_data = mkstemp(ztest_name_data);
6735 ASSERT3S(ztest_fd_data, >=, 0);
6736 (void) unlink(ztest_name_data);
6740 shared_data_size(ztest_shared_hdr_t *hdr)
6744 size = hdr->zh_hdr_size;
6745 size += hdr->zh_opts_size;
6746 size += hdr->zh_size;
6747 size += hdr->zh_stats_size * hdr->zh_stats_count;
6748 size += hdr->zh_ds_size * hdr->zh_ds_count;
6757 ztest_shared_hdr_t *hdr;
6759 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6760 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6761 ASSERT(hdr != MAP_FAILED);
6763 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6765 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6766 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6767 hdr->zh_size = sizeof (ztest_shared_t);
6768 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6769 hdr->zh_stats_count = ZTEST_FUNCS;
6770 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6771 hdr->zh_ds_count = ztest_opts.zo_datasets;
6773 size = shared_data_size(hdr);
6774 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6776 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6783 ztest_shared_hdr_t *hdr;
6786 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6787 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6788 ASSERT(hdr != MAP_FAILED);
6790 size = shared_data_size(hdr);
6792 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6793 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6794 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6795 ASSERT(hdr != MAP_FAILED);
6796 buf = (uint8_t *)hdr;
6798 offset = hdr->zh_hdr_size;
6799 ztest_shared_opts = (void *)&buf[offset];
6800 offset += hdr->zh_opts_size;
6801 ztest_shared = (void *)&buf[offset];
6802 offset += hdr->zh_size;
6803 ztest_shared_callstate = (void *)&buf[offset];
6804 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6805 ztest_shared_ds = (void *)&buf[offset];
6809 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6813 char *cmdbuf = NULL;
6818 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6819 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6824 fatal(1, "fork failed");
6826 if (pid == 0) { /* child */
6827 char *emptyargv[2] = { cmd, NULL };
6828 char fd_data_str[12];
6830 struct rlimit rl = { 1024, 1024 };
6831 (void) setrlimit(RLIMIT_NOFILE, &rl);
6833 (void) close(ztest_fd_rand);
6834 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6835 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6837 (void) enable_extended_FILE_stdio(-1, -1);
6838 if (libpath != NULL)
6839 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6840 (void) execv(cmd, emptyargv);
6841 ztest_dump_core = B_FALSE;
6842 fatal(B_TRUE, "exec failed: %s", cmd);
6845 if (cmdbuf != NULL) {
6846 umem_free(cmdbuf, MAXPATHLEN);
6850 while (waitpid(pid, &status, 0) != pid)
6852 if (statusp != NULL)
6855 if (WIFEXITED(status)) {
6856 if (WEXITSTATUS(status) != 0) {
6857 (void) fprintf(stderr, "child exited with code %d\n",
6858 WEXITSTATUS(status));
6862 } else if (WIFSIGNALED(status)) {
6863 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6864 (void) fprintf(stderr, "child died with signal %d\n",
6870 (void) fprintf(stderr, "something strange happened to child\n");
6877 ztest_run_init(void)
6881 ztest_shared_t *zs = ztest_shared;
6884 * Blow away any existing copy of zpool.cache
6886 (void) remove(spa_config_path);
6888 if (ztest_opts.zo_init == 0) {
6889 if (ztest_opts.zo_verbose >= 1)
6890 (void) printf("Importing pool %s\n",
6891 ztest_opts.zo_pool);
6897 * Create and initialize our storage pool.
6899 for (i = 1; i <= ztest_opts.zo_init; i++) {
6900 bzero(zs, sizeof (ztest_shared_t));
6901 if (ztest_opts.zo_verbose >= 3 &&
6902 ztest_opts.zo_init != 1) {
6903 (void) printf("ztest_init(), pass %d\n", i);
6910 main(int argc, char **argv)
6918 ztest_shared_callstate_t *zc;
6925 char *fd_data_str = getenv("ZTEST_FD_DATA");
6926 struct sigaction action;
6928 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6930 dprintf_setup(&argc, argv);
6932 action.sa_handler = sig_handler;
6933 sigemptyset(&action.sa_mask);
6934 action.sa_flags = 0;
6936 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6937 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6942 if (sigaction(SIGABRT, &action, NULL) < 0) {
6943 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6948 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6949 ASSERT3S(ztest_fd_rand, >=, 0);
6952 process_options(argc, argv);
6957 bcopy(&ztest_opts, ztest_shared_opts,
6958 sizeof (*ztest_shared_opts));
6960 ztest_fd_data = atoi(fd_data_str);
6962 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6964 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6966 /* Override location of zpool.cache */
6967 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6968 ztest_opts.zo_dir) != -1);
6970 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6975 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6976 metaslab_df_alloc_threshold =
6977 zs->zs_metaslab_df_alloc_threshold;
6986 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6988 if (ztest_opts.zo_verbose >= 1) {
6989 (void) printf("%llu vdevs, %d datasets, %d threads,"
6990 " %llu seconds...\n",
6991 (u_longlong_t)ztest_opts.zo_vdevs,
6992 ztest_opts.zo_datasets,
6993 ztest_opts.zo_threads,
6994 (u_longlong_t)ztest_opts.zo_time);
6997 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6998 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7000 zs->zs_do_init = B_TRUE;
7001 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7002 if (ztest_opts.zo_verbose >= 1) {
7003 (void) printf("Executing older ztest for "
7004 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7006 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7007 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7009 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7011 zs->zs_do_init = B_FALSE;
7013 zs->zs_proc_start = gethrtime();
7014 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7016 for (f = 0; f < ZTEST_FUNCS; f++) {
7017 zi = &ztest_info[f];
7018 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7019 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7020 zc->zc_next = UINT64_MAX;
7022 zc->zc_next = zs->zs_proc_start +
7023 ztest_random(2 * zi->zi_interval[0] + 1);
7027 * Run the tests in a loop. These tests include fault injection
7028 * to verify that self-healing data works, and forced crashes
7029 * to verify that we never lose on-disk consistency.
7031 while (gethrtime() < zs->zs_proc_stop) {
7036 * Initialize the workload counters for each function.
7038 for (f = 0; f < ZTEST_FUNCS; f++) {
7039 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7044 /* Set the allocation switch size */
7045 zs->zs_metaslab_df_alloc_threshold =
7046 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7048 if (!hasalt || ztest_random(2) == 0) {
7049 if (hasalt && ztest_opts.zo_verbose >= 1) {
7050 (void) printf("Executing newer ztest: %s\n",
7054 killed = exec_child(cmd, NULL, B_TRUE, &status);
7056 if (hasalt && ztest_opts.zo_verbose >= 1) {
7057 (void) printf("Executing older ztest: %s\n",
7058 ztest_opts.zo_alt_ztest);
7061 killed = exec_child(ztest_opts.zo_alt_ztest,
7062 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7069 if (ztest_opts.zo_verbose >= 1) {
7070 hrtime_t now = gethrtime();
7072 now = MIN(now, zs->zs_proc_stop);
7073 print_time(zs->zs_proc_stop - now, timebuf);
7074 nicenum(zs->zs_space, numbuf);
7076 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7077 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7079 WIFEXITED(status) ? "Complete" : "SIGKILL",
7080 (u_longlong_t)zs->zs_enospc_count,
7081 100.0 * zs->zs_alloc / zs->zs_space,
7083 100.0 * (now - zs->zs_proc_start) /
7084 (ztest_opts.zo_time * NANOSEC), timebuf);
7087 if (ztest_opts.zo_verbose >= 2) {
7088 (void) printf("\nWorkload summary:\n\n");
7089 (void) printf("%7s %9s %s\n",
7090 "Calls", "Time", "Function");
7091 (void) printf("%7s %9s %s\n",
7092 "-----", "----", "--------");
7093 for (f = 0; f < ZTEST_FUNCS; f++) {
7094 zi = &ztest_info[f];
7095 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7096 print_time(zc->zc_time, timebuf);
7097 (void) printf("%7llu %9s %s\n",
7098 (u_longlong_t)zc->zc_count, timebuf,
7101 (void) printf("\n");
7105 * It's possible that we killed a child during a rename test,
7106 * in which case we'll have a 'ztest_tmp' pool lying around
7107 * instead of 'ztest'. Do a blind rename in case this happened.
7110 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
7111 spa_close(spa, FTAG);
7113 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
7115 kernel_init(FREAD | FWRITE);
7116 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
7117 ztest_opts.zo_pool);
7118 (void) spa_rename(tmpname, ztest_opts.zo_pool);
7122 if (!ztest_opts.zo_mmp_test)
7123 ztest_run_zdb(ztest_opts.zo_pool);
7126 if (ztest_opts.zo_verbose >= 1) {
7128 (void) printf("%d runs of older ztest: %s\n", older,
7129 ztest_opts.zo_alt_ztest);
7130 (void) printf("%d runs of newer ztest: %s\n", newer,
7133 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7134 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7137 umem_free(cmd, MAXNAMELEN);