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, 2018 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 * Copyright (c) 2014 Integros [integros.com]
28 * Copyright 2017 Joyent, Inc.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright 2017 RackTop Systems.
34 * The objective of this program is to provide a DMU/ZAP/SPA stress test
35 * that runs entirely in userland, is easy to use, and easy to extend.
37 * The overall design of the ztest program is as follows:
39 * (1) For each major functional area (e.g. adding vdevs to a pool,
40 * creating and destroying datasets, reading and writing objects, etc)
41 * we have a simple routine to test that functionality. These
42 * individual routines do not have to do anything "stressful".
44 * (2) We turn these simple functionality tests into a stress test by
45 * running them all in parallel, with as many threads as desired,
46 * and spread across as many datasets, objects, and vdevs as desired.
48 * (3) While all this is happening, we inject faults into the pool to
49 * verify that self-healing data really works.
51 * (4) Every time we open a dataset, we change its checksum and compression
52 * functions. Thus even individual objects vary from block to block
53 * in which checksum they use and whether they're compressed.
55 * (5) To verify that we never lose on-disk consistency after a crash,
56 * we run the entire test in a child of the main process.
57 * At random times, the child self-immolates with a SIGKILL.
58 * This is the software equivalent of pulling the power cord.
59 * The parent then runs the test again, using the existing
60 * storage pool, as many times as desired. If backwards compatibility
61 * testing is enabled ztest will sometimes run the "older" version
62 * of ztest after a SIGKILL.
64 * (6) To verify that we don't have future leaks or temporal incursions,
65 * many of the functional tests record the transaction group number
66 * as part of their data. When reading old data, they verify that
67 * the transaction group number is less than the current, open txg.
68 * If you add a new test, please do this if applicable.
70 * When run with no arguments, ztest runs for about five minutes and
71 * produces no output if successful. To get a little bit of information,
72 * specify -V. To get more information, specify -VV, and so on.
74 * To turn this into an overnight stress test, use -T to specify run time.
76 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
77 * to increase the pool capacity, fanout, and overall stress level.
79 * Use the -k option to set the desired frequency of kills.
81 * When ztest invokes itself it passes all relevant information through a
82 * temporary file which is mmap-ed in the child process. This allows shared
83 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
84 * stored at offset 0 of this file and contains information on the size and
85 * number of shared structures in the file. The information stored in this file
86 * must remain backwards compatible with older versions of ztest so that
87 * ztest can invoke them during backwards compatibility testing (-B).
90 #include <sys/zfs_context.h>
96 #include <sys/dmu_objset.h>
100 #include <sys/wait.h>
101 #include <sys/mman.h>
102 #include <sys/resource.h>
105 #include <sys/zil_impl.h>
106 #include <sys/vdev_impl.h>
107 #include <sys/vdev_file.h>
108 #include <sys/vdev_initialize.h>
109 #include <sys/spa_impl.h>
110 #include <sys/metaslab_impl.h>
111 #include <sys/dsl_prop.h>
112 #include <sys/dsl_dataset.h>
113 #include <sys/dsl_destroy.h>
114 #include <sys/dsl_scan.h>
115 #include <sys/zio_checksum.h>
116 #include <sys/refcount.h>
117 #include <sys/zfeature.h>
118 #include <sys/dsl_userhold.h>
121 #include <stdio_ext.h>
130 #include <sys/fs/zfs.h>
131 #include <libnvpair.h>
133 #include <libcmdutils.h>
135 static int ztest_fd_data = -1;
136 static int ztest_fd_rand = -1;
138 typedef struct ztest_shared_hdr {
139 uint64_t zh_hdr_size;
140 uint64_t zh_opts_size;
142 uint64_t zh_stats_size;
143 uint64_t zh_stats_count;
145 uint64_t zh_ds_count;
146 } ztest_shared_hdr_t;
148 static ztest_shared_hdr_t *ztest_shared_hdr;
150 enum ztest_class_state {
151 ZTEST_VDEV_CLASS_OFF,
156 typedef struct ztest_shared_opts {
157 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
158 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
159 char zo_alt_ztest[MAXNAMELEN];
160 char zo_alt_libpath[MAXNAMELEN];
162 uint64_t zo_vdevtime;
170 uint64_t zo_passtime;
171 uint64_t zo_killrate;
175 uint64_t zo_maxloops;
176 uint64_t zo_metaslab_force_ganging;
178 int zo_special_vdevs;
179 } ztest_shared_opts_t;
181 static const ztest_shared_opts_t ztest_opts_defaults = {
182 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
183 .zo_dir = { '/', 't', 'm', 'p', '\0' },
184 .zo_alt_ztest = { '\0' },
185 .zo_alt_libpath = { '\0' },
187 .zo_ashift = SPA_MINBLOCKSHIFT,
190 .zo_raidz_parity = 1,
191 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
194 .zo_passtime = 60, /* 60 seconds */
195 .zo_killrate = 70, /* 70% kill rate */
199 .zo_time = 300, /* 5 minutes */
200 .zo_maxloops = 50, /* max loops during spa_freeze() */
201 .zo_metaslab_force_ganging = 32 << 10,
202 .zo_special_vdevs = ZTEST_VDEV_CLASS_RND,
205 extern uint64_t metaslab_force_ganging;
206 extern uint64_t metaslab_df_alloc_threshold;
207 extern uint64_t zfs_deadman_synctime_ms;
208 extern int metaslab_preload_limit;
209 extern boolean_t zfs_compressed_arc_enabled;
210 extern boolean_t zfs_abd_scatter_enabled;
211 extern int dmu_object_alloc_chunk_shift;
212 extern boolean_t zfs_force_some_double_word_sm_entries;
213 extern unsigned long zfs_reconstruct_indirect_damage_fraction;
215 static ztest_shared_opts_t *ztest_shared_opts;
216 static ztest_shared_opts_t ztest_opts;
218 typedef struct ztest_shared_ds {
222 static ztest_shared_ds_t *ztest_shared_ds;
223 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
225 #define BT_MAGIC 0x123456789abcdefULL
226 #define MAXFAULTS() \
227 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
231 ZTEST_IO_WRITE_PATTERN,
232 ZTEST_IO_WRITE_ZEROES,
239 typedef struct ztest_block_tag {
243 uint64_t bt_dnodesize;
250 typedef struct bufwad {
257 * It would be better to use a rangelock_t per object. Unfortunately
258 * the rangelock_t is not a drop-in replacement for rl_t, because we
259 * still need to map from object ID to rangelock_t.
281 #define ZTEST_RANGE_LOCKS 64
282 #define ZTEST_OBJECT_LOCKS 64
285 * Object descriptor. Used as a template for object lookup/create/remove.
287 typedef struct ztest_od {
290 dmu_object_type_t od_type;
291 dmu_object_type_t od_crtype;
292 uint64_t od_blocksize;
293 uint64_t od_crblocksize;
294 uint64_t od_crdnodesize;
297 char od_name[ZFS_MAX_DATASET_NAME_LEN];
303 typedef struct ztest_ds {
304 ztest_shared_ds_t *zd_shared;
306 krwlock_t zd_zilog_lock;
308 ztest_od_t *zd_od; /* debugging aid */
309 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
310 kmutex_t zd_dirobj_lock;
311 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
312 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
316 * Per-iteration state.
318 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
320 typedef struct ztest_info {
321 ztest_func_t *zi_func; /* test function */
322 uint64_t zi_iters; /* iterations per execution */
323 uint64_t *zi_interval; /* execute every <interval> seconds */
326 typedef struct ztest_shared_callstate {
327 uint64_t zc_count; /* per-pass count */
328 uint64_t zc_time; /* per-pass time */
329 uint64_t zc_next; /* next time to call this function */
330 } ztest_shared_callstate_t;
332 static ztest_shared_callstate_t *ztest_shared_callstate;
333 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
336 * Note: these aren't static because we want dladdr() to work.
338 ztest_func_t ztest_dmu_read_write;
339 ztest_func_t ztest_dmu_write_parallel;
340 ztest_func_t ztest_dmu_object_alloc_free;
341 ztest_func_t ztest_dmu_object_next_chunk;
342 ztest_func_t ztest_dmu_commit_callbacks;
343 ztest_func_t ztest_zap;
344 ztest_func_t ztest_zap_parallel;
345 ztest_func_t ztest_zil_commit;
346 ztest_func_t ztest_zil_remount;
347 ztest_func_t ztest_dmu_read_write_zcopy;
348 ztest_func_t ztest_dmu_objset_create_destroy;
349 ztest_func_t ztest_dmu_prealloc;
350 ztest_func_t ztest_fzap;
351 ztest_func_t ztest_dmu_snapshot_create_destroy;
352 ztest_func_t ztest_dsl_prop_get_set;
353 ztest_func_t ztest_spa_prop_get_set;
354 ztest_func_t ztest_spa_create_destroy;
355 ztest_func_t ztest_fault_inject;
356 ztest_func_t ztest_ddt_repair;
357 ztest_func_t ztest_dmu_snapshot_hold;
358 ztest_func_t ztest_mmp_enable_disable;
359 ztest_func_t ztest_scrub;
360 ztest_func_t ztest_dsl_dataset_promote_busy;
361 ztest_func_t ztest_vdev_attach_detach;
362 ztest_func_t ztest_vdev_LUN_growth;
363 ztest_func_t ztest_vdev_add_remove;
364 ztest_func_t ztest_vdev_class_add;
365 ztest_func_t ztest_vdev_aux_add_remove;
366 ztest_func_t ztest_split_pool;
367 ztest_func_t ztest_reguid;
368 ztest_func_t ztest_spa_upgrade;
369 ztest_func_t ztest_device_removal;
370 ztest_func_t ztest_remap_blocks;
371 ztest_func_t ztest_spa_checkpoint_create_discard;
372 ztest_func_t ztest_initialize;
373 ztest_func_t ztest_verify_dnode_bt;
375 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
376 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
377 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
378 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
379 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
381 ztest_info_t ztest_info[] = {
382 { ztest_dmu_read_write, 1, &zopt_always },
383 { ztest_dmu_write_parallel, 10, &zopt_always },
384 { ztest_dmu_object_alloc_free, 1, &zopt_always },
385 { ztest_dmu_object_next_chunk, 1, &zopt_sometimes },
386 { ztest_dmu_commit_callbacks, 1, &zopt_always },
387 { ztest_zap, 30, &zopt_always },
388 { ztest_zap_parallel, 100, &zopt_always },
389 { ztest_split_pool, 1, &zopt_always },
390 { ztest_zil_commit, 1, &zopt_incessant },
391 { ztest_zil_remount, 1, &zopt_sometimes },
392 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
393 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
394 { ztest_dsl_prop_get_set, 1, &zopt_often },
395 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
397 { ztest_dmu_prealloc, 1, &zopt_sometimes },
399 { ztest_fzap, 1, &zopt_sometimes },
400 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
401 { ztest_spa_create_destroy, 1, &zopt_sometimes },
402 { ztest_fault_inject, 1, &zopt_incessant },
403 { ztest_ddt_repair, 1, &zopt_sometimes },
404 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
405 { ztest_mmp_enable_disable, 1, &zopt_sometimes },
406 { ztest_reguid, 1, &zopt_rarely },
407 { ztest_scrub, 1, &zopt_often },
408 { ztest_spa_upgrade, 1, &zopt_rarely },
409 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
410 { ztest_vdev_attach_detach, 1, &zopt_incessant },
411 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
412 { ztest_vdev_add_remove, 1,
413 &ztest_opts.zo_vdevtime },
414 { ztest_vdev_class_add, 1,
415 &ztest_opts.zo_vdevtime },
416 { ztest_vdev_aux_add_remove, 1,
417 &ztest_opts.zo_vdevtime },
418 { ztest_device_removal, 1, &zopt_sometimes },
419 { ztest_remap_blocks, 1, &zopt_sometimes },
420 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
421 { ztest_initialize, 1, &zopt_sometimes },
422 { ztest_verify_dnode_bt, 1, &zopt_sometimes }
425 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
428 * The following struct is used to hold a list of uncalled commit callbacks.
429 * The callbacks are ordered by txg number.
431 typedef struct ztest_cb_list {
432 kmutex_t zcl_callbacks_lock;
433 list_t zcl_callbacks;
437 * Stuff we need to share writably between parent and child.
439 typedef struct ztest_shared {
440 boolean_t zs_do_init;
441 hrtime_t zs_proc_start;
442 hrtime_t zs_proc_stop;
443 hrtime_t zs_thread_start;
444 hrtime_t zs_thread_stop;
445 hrtime_t zs_thread_kill;
446 uint64_t zs_enospc_count;
447 uint64_t zs_vdev_next_leaf;
448 uint64_t zs_vdev_aux;
453 uint64_t zs_metaslab_sz;
454 uint64_t zs_metaslab_df_alloc_threshold;
458 #define ID_PARALLEL -1ULL
460 static char ztest_dev_template[] = "%s/%s.%llua";
461 static char ztest_aux_template[] = "%s/%s.%s.%llu";
462 ztest_shared_t *ztest_shared;
464 static spa_t *ztest_spa = NULL;
465 static ztest_ds_t *ztest_ds;
467 static kmutex_t ztest_vdev_lock;
468 static boolean_t ztest_device_removal_active = B_FALSE;
469 static kmutex_t ztest_checkpoint_lock;
472 * The ztest_name_lock protects the pool and dataset namespace used by
473 * the individual tests. To modify the namespace, consumers must grab
474 * this lock as writer. Grabbing the lock as reader will ensure that the
475 * namespace does not change while the lock is held.
477 static krwlock_t ztest_name_lock;
479 static boolean_t ztest_dump_core = B_TRUE;
480 static boolean_t ztest_exiting;
482 /* Global commit callback list */
483 static ztest_cb_list_t zcl;
486 ZTEST_META_DNODE = 0,
491 static void usage(boolean_t) __NORETURN;
494 * These libumem hooks provide a reasonable set of defaults for the allocator's
495 * debugging facilities.
500 return ("default,verbose"); /* $UMEM_DEBUG setting */
504 _umem_logging_init(void)
506 return ("fail,contents"); /* $UMEM_LOGGING setting */
509 #define FATAL_MSG_SZ 1024
514 fatal(int do_perror, char *message, ...)
517 int save_errno = errno;
518 char buf[FATAL_MSG_SZ];
520 (void) fflush(stdout);
522 va_start(args, message);
523 (void) sprintf(buf, "ztest: ");
525 (void) vsprintf(buf + strlen(buf), message, args);
528 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
529 ": %s", strerror(save_errno));
531 (void) fprintf(stderr, "%s\n", buf);
532 fatal_msg = buf; /* to ease debugging */
539 str2shift(const char *buf)
541 const char *ends = "BKMGTPEZ";
546 for (i = 0; i < strlen(ends); i++) {
547 if (toupper(buf[0]) == ends[i])
550 if (i == strlen(ends)) {
551 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
555 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
558 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
564 nicenumtoull(const char *buf)
569 val = strtoull(buf, &end, 0);
571 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
573 } else if (end[0] == '.') {
574 double fval = strtod(buf, &end);
575 fval *= pow(2, str2shift(end));
576 if (fval > UINT64_MAX) {
577 (void) fprintf(stderr, "ztest: value too large: %s\n",
581 val = (uint64_t)fval;
583 int shift = str2shift(end);
584 if (shift >= 64 || (val << shift) >> shift != val) {
585 (void) fprintf(stderr, "ztest: value too large: %s\n",
595 usage(boolean_t requested)
597 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
599 char nice_vdev_size[NN_NUMBUF_SZ];
600 char nice_force_ganging[NN_NUMBUF_SZ];
601 FILE *fp = requested ? stdout : stderr;
603 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
604 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
605 sizeof (nice_force_ganging));
607 (void) fprintf(fp, "Usage: %s\n"
608 "\t[-v vdevs (default: %llu)]\n"
609 "\t[-s size_of_each_vdev (default: %s)]\n"
610 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
611 "\t[-m mirror_copies (default: %d)]\n"
612 "\t[-r raidz_disks (default: %d)]\n"
613 "\t[-R raidz_parity (default: %d)]\n"
614 "\t[-d datasets (default: %d)]\n"
615 "\t[-t threads (default: %d)]\n"
616 "\t[-g gang_block_threshold (default: %s)]\n"
617 "\t[-i init_count (default: %d)] initialize pool i times\n"
618 "\t[-k kill_percentage (default: %llu%%)]\n"
619 "\t[-p pool_name (default: %s)]\n"
620 "\t[-f dir (default: %s)] file directory for vdev files\n"
621 "\t[-M] Multi-host simulate pool imported on remote host\n"
622 "\t[-V] verbose (use multiple times for ever more blather)\n"
623 "\t[-E] use existing pool instead of creating new one\n"
624 "\t[-T time (default: %llu sec)] total run time\n"
625 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
626 "\t[-P passtime (default: %llu sec)] time per pass\n"
627 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
628 "\t[-C vdev class state (default: random)] special=on|off|random\n"
629 "\t[-o variable=value] ... set global variable to an unsigned\n"
630 "\t 32-bit integer value\n"
631 "\t[-h] (print help)\n"
634 (u_longlong_t)zo->zo_vdevs, /* -v */
635 nice_vdev_size, /* -s */
636 zo->zo_ashift, /* -a */
637 zo->zo_mirrors, /* -m */
638 zo->zo_raidz, /* -r */
639 zo->zo_raidz_parity, /* -R */
640 zo->zo_datasets, /* -d */
641 zo->zo_threads, /* -t */
642 nice_force_ganging, /* -g */
643 zo->zo_init, /* -i */
644 (u_longlong_t)zo->zo_killrate, /* -k */
645 zo->zo_pool, /* -p */
647 (u_longlong_t)zo->zo_time, /* -T */
648 (u_longlong_t)zo->zo_maxloops, /* -F */
649 (u_longlong_t)zo->zo_passtime);
650 exit(requested ? 0 : 1);
655 ztest_parse_name_value(const char *input, ztest_shared_opts_t *zo)
659 int state = ZTEST_VDEV_CLASS_RND;
661 (void) strlcpy(name, input, sizeof (name));
663 value = strchr(name, '=');
665 (void) fprintf(stderr, "missing value in property=value "
666 "'-C' argument (%s)\n", input);
672 if (strcmp(value, "on") == 0) {
673 state = ZTEST_VDEV_CLASS_ON;
674 } else if (strcmp(value, "off") == 0) {
675 state = ZTEST_VDEV_CLASS_OFF;
676 } else if (strcmp(value, "random") == 0) {
677 state = ZTEST_VDEV_CLASS_RND;
679 (void) fprintf(stderr, "invalid property value '%s'\n", value);
683 if (strcmp(name, "special") == 0) {
684 zo->zo_special_vdevs = state;
686 (void) fprintf(stderr, "invalid property name '%s'\n", name);
689 if (zo->zo_verbose >= 3)
690 (void) printf("%s vdev state is '%s'\n", name, value);
694 process_options(int argc, char **argv)
697 ztest_shared_opts_t *zo = &ztest_opts;
701 char altdir[MAXNAMELEN] = { 0 };
703 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
705 while ((opt = getopt(argc, argv,
706 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:C:o:")) != EOF) {
723 value = nicenumtoull(optarg);
727 zo->zo_vdevs = value;
730 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
733 zo->zo_ashift = value;
736 zo->zo_mirrors = value;
739 zo->zo_raidz = MAX(1, value);
742 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
745 zo->zo_datasets = MAX(1, value);
748 zo->zo_threads = MAX(1, value);
751 zo->zo_metaslab_force_ganging =
752 MAX(SPA_MINBLOCKSIZE << 1, value);
758 zo->zo_killrate = value;
761 (void) strlcpy(zo->zo_pool, optarg,
762 sizeof (zo->zo_pool));
765 path = realpath(optarg, NULL);
767 (void) fprintf(stderr, "error: %s: %s\n",
768 optarg, strerror(errno));
771 (void) strlcpy(zo->zo_dir, path,
772 sizeof (zo->zo_dir));
788 zo->zo_passtime = MAX(1, value);
791 zo->zo_maxloops = MAX(1, value);
794 (void) strlcpy(altdir, optarg, sizeof (altdir));
797 ztest_parse_name_value(optarg, zo);
800 if (set_global_var(optarg) != 0)
813 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
816 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
819 if (strlen(altdir) > 0) {
827 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
828 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
830 VERIFY(NULL != realpath(getexecname(), cmd));
831 if (0 != access(altdir, F_OK)) {
832 ztest_dump_core = B_FALSE;
833 fatal(B_TRUE, "invalid alternate ztest path: %s",
836 VERIFY(NULL != realpath(altdir, realaltdir));
839 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
840 * We want to extract <isa> to determine if we should use
841 * 32 or 64 bit binaries.
843 bin = strstr(cmd, "/usr/bin/");
844 ztest = strstr(bin, "/ztest");
846 isalen = ztest - isa;
847 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
848 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
849 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
850 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
852 if (0 != access(zo->zo_alt_ztest, X_OK)) {
853 ztest_dump_core = B_FALSE;
854 fatal(B_TRUE, "invalid alternate ztest: %s",
856 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
857 ztest_dump_core = B_FALSE;
858 fatal(B_TRUE, "invalid alternate lib directory %s",
862 umem_free(cmd, MAXPATHLEN);
863 umem_free(realaltdir, MAXPATHLEN);
868 ztest_kill(ztest_shared_t *zs)
870 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
871 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
874 * Before we kill off ztest, make sure that the config is updated.
875 * See comment above spa_write_cachefile().
877 mutex_enter(&spa_namespace_lock);
878 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
879 mutex_exit(&spa_namespace_lock);
881 zfs_dbgmsg_print(FTAG);
882 (void) kill(getpid(), SIGKILL);
886 ztest_random(uint64_t range)
890 ASSERT3S(ztest_fd_rand, >=, 0);
895 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
896 fatal(1, "short read from /dev/urandom");
903 ztest_record_enospc(const char *s)
905 ztest_shared->zs_enospc_count++;
909 ztest_get_ashift(void)
911 if (ztest_opts.zo_ashift == 0)
912 return (SPA_MINBLOCKSHIFT + ztest_random(5));
913 return (ztest_opts.zo_ashift);
917 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
919 char pathbuf[MAXPATHLEN];
924 ashift = ztest_get_ashift();
930 vdev = ztest_shared->zs_vdev_aux;
931 (void) snprintf(path, sizeof (pathbuf),
932 ztest_aux_template, ztest_opts.zo_dir,
933 pool == NULL ? ztest_opts.zo_pool : pool,
936 vdev = ztest_shared->zs_vdev_next_leaf++;
937 (void) snprintf(path, sizeof (pathbuf),
938 ztest_dev_template, ztest_opts.zo_dir,
939 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
944 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
946 fatal(1, "can't open %s", path);
947 if (ftruncate(fd, size) != 0)
948 fatal(1, "can't ftruncate %s", path);
952 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
953 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
954 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
955 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
961 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
962 uint64_t ashift, int r)
964 nvlist_t *raidz, **child;
968 return (make_vdev_file(path, aux, pool, size, ashift));
969 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
971 for (c = 0; c < r; c++)
972 child[c] = make_vdev_file(path, aux, pool, size, ashift);
974 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
975 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
976 VDEV_TYPE_RAIDZ) == 0);
977 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
978 ztest_opts.zo_raidz_parity) == 0);
979 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
982 for (c = 0; c < r; c++)
983 nvlist_free(child[c]);
985 umem_free(child, r * sizeof (nvlist_t *));
991 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
992 uint64_t ashift, int r, int m)
994 nvlist_t *mirror, **child;
998 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
1000 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
1002 for (c = 0; c < m; c++)
1003 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
1005 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
1006 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
1007 VDEV_TYPE_MIRROR) == 0);
1008 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
1011 for (c = 0; c < m; c++)
1012 nvlist_free(child[c]);
1014 umem_free(child, m * sizeof (nvlist_t *));
1020 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
1021 const char *class, int r, int m, int t)
1023 nvlist_t *root, **child;
1029 log = (class != NULL && strcmp(class, "log") == 0);
1031 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
1033 for (c = 0; c < t; c++) {
1034 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1036 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1039 if (class != NULL && class[0] != '\0') {
1040 ASSERT(m > 1 || log); /* expecting a mirror */
1041 VERIFY(nvlist_add_string(child[c],
1042 ZPOOL_CONFIG_ALLOCATION_BIAS, class) == 0);
1046 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1047 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1048 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1051 for (c = 0; c < t; c++)
1052 nvlist_free(child[c]);
1054 umem_free(child, t * sizeof (nvlist_t *));
1060 * Find a random spa version. Returns back a random spa version in the
1061 * range [initial_version, SPA_VERSION_FEATURES].
1064 ztest_random_spa_version(uint64_t initial_version)
1066 uint64_t version = initial_version;
1068 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1070 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1073 if (version > SPA_VERSION_BEFORE_FEATURES)
1074 version = SPA_VERSION_FEATURES;
1076 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1081 ztest_random_blocksize(void)
1083 uint64_t block_shift;
1085 ASSERT(ztest_spa->spa_max_ashift != 0);
1088 * Choose a block size >= the ashift.
1089 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1091 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1092 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1094 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1095 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1099 ztest_random_dnodesize(void)
1102 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1104 if (max_slots == DNODE_MIN_SLOTS)
1105 return (DNODE_MIN_SIZE);
1108 * Weight the random distribution more heavily toward smaller
1109 * dnode sizes since that is more likely to reflect real-world
1112 ASSERT3U(max_slots, >, 4);
1113 switch (ztest_random(10)) {
1115 slots = 5 + ztest_random(max_slots - 4);
1118 slots = 2 + ztest_random(3);
1125 return (slots << DNODE_SHIFT);
1129 ztest_random_ibshift(void)
1131 return (DN_MIN_INDBLKSHIFT +
1132 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1136 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1139 vdev_t *rvd = spa->spa_root_vdev;
1142 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1145 top = ztest_random(rvd->vdev_children);
1146 tvd = rvd->vdev_child[top];
1147 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1148 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1154 ztest_random_dsl_prop(zfs_prop_t prop)
1159 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1160 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1166 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1169 const char *propname = zfs_prop_to_name(prop);
1170 const char *valname;
1171 char setpoint[MAXPATHLEN];
1175 error = dsl_prop_set_int(osname, propname,
1176 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1178 if (error == ENOSPC) {
1179 ztest_record_enospc(FTAG);
1184 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1186 if (ztest_opts.zo_verbose >= 6) {
1187 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1188 (void) printf("%s %s = %s at '%s'\n",
1189 osname, propname, valname, setpoint);
1196 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1198 spa_t *spa = ztest_spa;
1199 nvlist_t *props = NULL;
1202 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1203 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1205 error = spa_prop_set(spa, props);
1209 if (error == ENOSPC) {
1210 ztest_record_enospc(FTAG);
1219 ztest_rll_init(rll_t *rll)
1221 rll->rll_writer = NULL;
1222 rll->rll_readers = 0;
1223 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1224 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1228 ztest_rll_destroy(rll_t *rll)
1230 ASSERT(rll->rll_writer == NULL);
1231 ASSERT(rll->rll_readers == 0);
1232 mutex_destroy(&rll->rll_lock);
1233 cv_destroy(&rll->rll_cv);
1237 ztest_rll_lock(rll_t *rll, rl_type_t type)
1239 mutex_enter(&rll->rll_lock);
1241 if (type == RL_READER) {
1242 while (rll->rll_writer != NULL)
1243 cv_wait(&rll->rll_cv, &rll->rll_lock);
1246 while (rll->rll_writer != NULL || rll->rll_readers)
1247 cv_wait(&rll->rll_cv, &rll->rll_lock);
1248 rll->rll_writer = curthread;
1251 mutex_exit(&rll->rll_lock);
1255 ztest_rll_unlock(rll_t *rll)
1257 mutex_enter(&rll->rll_lock);
1259 if (rll->rll_writer) {
1260 ASSERT(rll->rll_readers == 0);
1261 rll->rll_writer = NULL;
1263 ASSERT(rll->rll_readers != 0);
1264 ASSERT(rll->rll_writer == NULL);
1268 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1269 cv_broadcast(&rll->rll_cv);
1271 mutex_exit(&rll->rll_lock);
1275 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1277 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1279 ztest_rll_lock(rll, type);
1283 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1285 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1287 ztest_rll_unlock(rll);
1291 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1292 uint64_t size, rl_type_t type)
1294 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1295 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1298 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1299 rl->rl_object = object;
1300 rl->rl_offset = offset;
1304 ztest_rll_lock(rll, type);
1310 ztest_range_unlock(rl_t *rl)
1312 rll_t *rll = rl->rl_lock;
1314 ztest_rll_unlock(rll);
1316 umem_free(rl, sizeof (*rl));
1320 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1323 zd->zd_zilog = dmu_objset_zil(os);
1324 zd->zd_shared = szd;
1325 dmu_objset_name(os, zd->zd_name);
1327 if (zd->zd_shared != NULL)
1328 zd->zd_shared->zd_seq = 0;
1330 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1331 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1333 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1334 ztest_rll_init(&zd->zd_object_lock[l]);
1336 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1337 ztest_rll_init(&zd->zd_range_lock[l]);
1341 ztest_zd_fini(ztest_ds_t *zd)
1343 mutex_destroy(&zd->zd_dirobj_lock);
1345 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1346 ztest_rll_destroy(&zd->zd_object_lock[l]);
1348 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1349 ztest_rll_destroy(&zd->zd_range_lock[l]);
1352 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1355 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1361 * Attempt to assign tx to some transaction group.
1363 error = dmu_tx_assign(tx, txg_how);
1365 if (error == ERESTART) {
1366 ASSERT(txg_how == TXG_NOWAIT);
1369 ASSERT3U(error, ==, ENOSPC);
1370 ztest_record_enospc(tag);
1375 txg = dmu_tx_get_txg(tx);
1381 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1384 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1391 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1394 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1398 diff |= (value - *ip++);
1404 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1405 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1408 bt->bt_magic = BT_MAGIC;
1409 bt->bt_objset = dmu_objset_id(os);
1410 bt->bt_object = object;
1411 bt->bt_dnodesize = dnodesize;
1412 bt->bt_offset = offset;
1415 bt->bt_crtxg = crtxg;
1419 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1420 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1423 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1424 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1425 ASSERT3U(bt->bt_object, ==, object);
1426 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1427 ASSERT3U(bt->bt_offset, ==, offset);
1428 ASSERT3U(bt->bt_gen, <=, gen);
1429 ASSERT3U(bt->bt_txg, <=, txg);
1430 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1433 static ztest_block_tag_t *
1434 ztest_bt_bonus(dmu_buf_t *db)
1436 dmu_object_info_t doi;
1437 ztest_block_tag_t *bt;
1439 dmu_object_info_from_db(db, &doi);
1440 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1441 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1442 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1448 * Generate a token to fill up unused bonus buffer space. Try to make
1449 * it unique to the object, generation, and offset to verify that data
1450 * is not getting overwritten by data from other dnodes.
1452 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1453 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1456 * Fill up the unused bonus buffer region before the block tag with a
1457 * verifiable pattern. Filling the whole bonus area with non-zero data
1458 * helps ensure that all dnode traversal code properly skips the
1459 * interior regions of large dnodes.
1462 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1463 objset_t *os, uint64_t gen)
1467 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1469 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1470 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1471 gen, bonusp - (uint64_t *)db->db_data);
1477 * Verify that the unused area of a bonus buffer is filled with the
1481 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1482 objset_t *os, uint64_t gen)
1486 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1487 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1488 gen, bonusp - (uint64_t *)db->db_data);
1489 VERIFY3U(*bonusp, ==, token);
1497 #define lrz_type lr_mode
1498 #define lrz_blocksize lr_uid
1499 #define lrz_ibshift lr_gid
1500 #define lrz_bonustype lr_rdev
1501 #define lrz_dnodesize lr_crtime[1]
1504 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1506 char *name = (void *)(lr + 1); /* name follows lr */
1507 size_t namesize = strlen(name) + 1;
1510 if (zil_replaying(zd->zd_zilog, tx))
1513 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1514 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1515 sizeof (*lr) + namesize - sizeof (lr_t));
1517 zil_itx_assign(zd->zd_zilog, itx, tx);
1521 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1523 char *name = (void *)(lr + 1); /* name follows lr */
1524 size_t namesize = strlen(name) + 1;
1527 if (zil_replaying(zd->zd_zilog, tx))
1530 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1531 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1532 sizeof (*lr) + namesize - sizeof (lr_t));
1534 itx->itx_oid = object;
1535 zil_itx_assign(zd->zd_zilog, itx, tx);
1539 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1542 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1544 if (zil_replaying(zd->zd_zilog, tx))
1547 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1548 write_state = WR_INDIRECT;
1550 itx = zil_itx_create(TX_WRITE,
1551 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1553 if (write_state == WR_COPIED &&
1554 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1555 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1556 zil_itx_destroy(itx);
1557 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1558 write_state = WR_NEED_COPY;
1560 itx->itx_private = zd;
1561 itx->itx_wr_state = write_state;
1562 itx->itx_sync = (ztest_random(8) == 0);
1564 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1565 sizeof (*lr) - sizeof (lr_t));
1567 zil_itx_assign(zd->zd_zilog, itx, tx);
1571 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1575 if (zil_replaying(zd->zd_zilog, tx))
1578 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1579 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1580 sizeof (*lr) - sizeof (lr_t));
1582 itx->itx_sync = B_FALSE;
1583 zil_itx_assign(zd->zd_zilog, itx, tx);
1587 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1591 if (zil_replaying(zd->zd_zilog, tx))
1594 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1595 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1596 sizeof (*lr) - sizeof (lr_t));
1598 itx->itx_sync = B_FALSE;
1599 zil_itx_assign(zd->zd_zilog, itx, tx);
1606 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1608 ztest_ds_t *zd = arg1;
1609 lr_create_t *lr = arg2;
1610 char *name = (void *)(lr + 1); /* name follows lr */
1611 objset_t *os = zd->zd_os;
1612 ztest_block_tag_t *bbt;
1620 byteswap_uint64_array(lr, sizeof (*lr));
1622 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1623 ASSERT(name[0] != '\0');
1625 tx = dmu_tx_create(os);
1627 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1629 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1630 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1632 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1635 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1639 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1640 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1642 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1643 if (lr->lr_foid == 0) {
1644 lr->lr_foid = zap_create_dnsize(os,
1645 lr->lrz_type, lr->lrz_bonustype,
1646 bonuslen, lr->lrz_dnodesize, tx);
1648 error = zap_create_claim_dnsize(os, lr->lr_foid,
1649 lr->lrz_type, lr->lrz_bonustype,
1650 bonuslen, lr->lrz_dnodesize, tx);
1653 if (lr->lr_foid == 0) {
1654 lr->lr_foid = dmu_object_alloc_dnsize(os,
1655 lr->lrz_type, 0, lr->lrz_bonustype,
1656 bonuslen, lr->lrz_dnodesize, tx);
1658 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1659 lr->lrz_type, 0, lr->lrz_bonustype,
1660 bonuslen, lr->lrz_dnodesize, tx);
1665 ASSERT3U(error, ==, EEXIST);
1666 ASSERT(zd->zd_zilog->zl_replay);
1671 ASSERT(lr->lr_foid != 0);
1673 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1674 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1675 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1677 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1678 bbt = ztest_bt_bonus(db);
1679 dmu_buf_will_dirty(db, tx);
1680 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1681 lr->lr_gen, txg, txg);
1682 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1683 dmu_buf_rele(db, FTAG);
1685 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1688 (void) ztest_log_create(zd, tx, lr);
1696 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1698 ztest_ds_t *zd = arg1;
1699 lr_remove_t *lr = arg2;
1700 char *name = (void *)(lr + 1); /* name follows lr */
1701 objset_t *os = zd->zd_os;
1702 dmu_object_info_t doi;
1704 uint64_t object, txg;
1707 byteswap_uint64_array(lr, sizeof (*lr));
1709 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1710 ASSERT(name[0] != '\0');
1713 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1714 ASSERT(object != 0);
1716 ztest_object_lock(zd, object, RL_WRITER);
1718 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1720 tx = dmu_tx_create(os);
1722 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1723 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1725 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1727 ztest_object_unlock(zd, object);
1731 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1732 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1734 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1737 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1739 (void) ztest_log_remove(zd, tx, lr, object);
1743 ztest_object_unlock(zd, object);
1749 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1751 ztest_ds_t *zd = arg1;
1752 lr_write_t *lr = arg2;
1753 objset_t *os = zd->zd_os;
1754 void *data = lr + 1; /* data follows lr */
1755 uint64_t offset, length;
1756 ztest_block_tag_t *bt = data;
1757 ztest_block_tag_t *bbt;
1758 uint64_t gen, txg, lrtxg, crtxg;
1759 dmu_object_info_t doi;
1762 arc_buf_t *abuf = NULL;
1766 byteswap_uint64_array(lr, sizeof (*lr));
1768 offset = lr->lr_offset;
1769 length = lr->lr_length;
1771 /* If it's a dmu_sync() block, write the whole block */
1772 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1773 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1774 if (length < blocksize) {
1775 offset -= offset % blocksize;
1780 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1781 byteswap_uint64_array(bt, sizeof (*bt));
1783 if (bt->bt_magic != BT_MAGIC)
1786 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1787 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1789 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1791 dmu_object_info_from_db(db, &doi);
1793 bbt = ztest_bt_bonus(db);
1794 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1796 crtxg = bbt->bt_crtxg;
1797 lrtxg = lr->lr_common.lrc_txg;
1799 tx = dmu_tx_create(os);
1801 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1803 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1804 P2PHASE(offset, length) == 0)
1805 abuf = dmu_request_arcbuf(db, length);
1807 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1810 dmu_return_arcbuf(abuf);
1811 dmu_buf_rele(db, FTAG);
1812 ztest_range_unlock(rl);
1813 ztest_object_unlock(zd, lr->lr_foid);
1819 * Usually, verify the old data before writing new data --
1820 * but not always, because we also want to verify correct
1821 * behavior when the data was not recently read into cache.
1823 ASSERT(offset % doi.doi_data_block_size == 0);
1824 if (ztest_random(4) != 0) {
1825 int prefetch = ztest_random(2) ?
1826 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1827 ztest_block_tag_t rbt;
1829 VERIFY(dmu_read(os, lr->lr_foid, offset,
1830 sizeof (rbt), &rbt, prefetch) == 0);
1831 if (rbt.bt_magic == BT_MAGIC) {
1832 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1833 offset, gen, txg, crtxg);
1838 * Writes can appear to be newer than the bonus buffer because
1839 * the ztest_get_data() callback does a dmu_read() of the
1840 * open-context data, which may be different than the data
1841 * as it was when the write was generated.
1843 if (zd->zd_zilog->zl_replay) {
1844 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1845 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1850 * Set the bt's gen/txg to the bonus buffer's gen/txg
1851 * so that all of the usual ASSERTs will work.
1853 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1858 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1860 bcopy(data, abuf->b_data, length);
1861 dmu_assign_arcbuf(db, offset, abuf, tx);
1864 (void) ztest_log_write(zd, tx, lr);
1866 dmu_buf_rele(db, FTAG);
1870 ztest_range_unlock(rl);
1871 ztest_object_unlock(zd, lr->lr_foid);
1877 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1879 ztest_ds_t *zd = arg1;
1880 lr_truncate_t *lr = arg2;
1881 objset_t *os = zd->zd_os;
1887 byteswap_uint64_array(lr, sizeof (*lr));
1889 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1890 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1893 tx = dmu_tx_create(os);
1895 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1897 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1899 ztest_range_unlock(rl);
1900 ztest_object_unlock(zd, lr->lr_foid);
1904 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1905 lr->lr_length, tx) == 0);
1907 (void) ztest_log_truncate(zd, tx, lr);
1911 ztest_range_unlock(rl);
1912 ztest_object_unlock(zd, lr->lr_foid);
1918 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1920 ztest_ds_t *zd = arg1;
1921 lr_setattr_t *lr = arg2;
1922 objset_t *os = zd->zd_os;
1925 ztest_block_tag_t *bbt;
1926 uint64_t txg, lrtxg, crtxg, dnodesize;
1929 byteswap_uint64_array(lr, sizeof (*lr));
1931 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1933 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1935 tx = dmu_tx_create(os);
1936 dmu_tx_hold_bonus(tx, lr->lr_foid);
1938 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1940 dmu_buf_rele(db, FTAG);
1941 ztest_object_unlock(zd, lr->lr_foid);
1945 bbt = ztest_bt_bonus(db);
1946 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1947 crtxg = bbt->bt_crtxg;
1948 lrtxg = lr->lr_common.lrc_txg;
1949 dnodesize = bbt->bt_dnodesize;
1951 if (zd->zd_zilog->zl_replay) {
1952 ASSERT(lr->lr_size != 0);
1953 ASSERT(lr->lr_mode != 0);
1957 * Randomly change the size and increment the generation.
1959 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1961 lr->lr_mode = bbt->bt_gen + 1;
1966 * Verify that the current bonus buffer is not newer than our txg.
1968 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1969 MAX(txg, lrtxg), crtxg);
1971 dmu_buf_will_dirty(db, tx);
1973 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1974 ASSERT3U(lr->lr_size, <=, db->db_size);
1975 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1976 bbt = ztest_bt_bonus(db);
1978 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
1980 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
1982 dmu_buf_rele(db, FTAG);
1984 (void) ztest_log_setattr(zd, tx, lr);
1988 ztest_object_unlock(zd, lr->lr_foid);
1993 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1994 NULL, /* 0 no such transaction type */
1995 ztest_replay_create, /* TX_CREATE */
1996 NULL, /* TX_MKDIR */
1997 NULL, /* TX_MKXATTR */
1998 NULL, /* TX_SYMLINK */
1999 ztest_replay_remove, /* TX_REMOVE */
2000 NULL, /* TX_RMDIR */
2002 NULL, /* TX_RENAME */
2003 ztest_replay_write, /* TX_WRITE */
2004 ztest_replay_truncate, /* TX_TRUNCATE */
2005 ztest_replay_setattr, /* TX_SETATTR */
2007 NULL, /* TX_CREATE_ACL */
2008 NULL, /* TX_CREATE_ATTR */
2009 NULL, /* TX_CREATE_ACL_ATTR */
2010 NULL, /* TX_MKDIR_ACL */
2011 NULL, /* TX_MKDIR_ATTR */
2012 NULL, /* TX_MKDIR_ACL_ATTR */
2013 NULL, /* TX_WRITE2 */
2017 * ZIL get_data callbacks
2022 ztest_get_done(zgd_t *zgd, int error)
2024 ztest_ds_t *zd = zgd->zgd_private;
2025 uint64_t object = ((rl_t *)zgd->zgd_lr)->rl_object;
2028 dmu_buf_rele(zgd->zgd_db, zgd);
2030 ztest_range_unlock((rl_t *)zgd->zgd_lr);
2031 ztest_object_unlock(zd, object);
2033 umem_free(zgd, sizeof (*zgd));
2037 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
2040 ztest_ds_t *zd = arg;
2041 objset_t *os = zd->zd_os;
2042 uint64_t object = lr->lr_foid;
2043 uint64_t offset = lr->lr_offset;
2044 uint64_t size = lr->lr_length;
2045 uint64_t txg = lr->lr_common.lrc_txg;
2047 dmu_object_info_t doi;
2052 ASSERT3P(lwb, !=, NULL);
2053 ASSERT3P(zio, !=, NULL);
2054 ASSERT3U(size, !=, 0);
2056 ztest_object_lock(zd, object, RL_READER);
2057 error = dmu_bonus_hold(os, object, FTAG, &db);
2059 ztest_object_unlock(zd, object);
2063 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2065 if (crtxg == 0 || crtxg > txg) {
2066 dmu_buf_rele(db, FTAG);
2067 ztest_object_unlock(zd, object);
2071 dmu_object_info_from_db(db, &doi);
2072 dmu_buf_rele(db, FTAG);
2075 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2077 zgd->zgd_private = zd;
2079 if (buf != NULL) { /* immediate write */
2080 zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
2081 object, offset, size, RL_READER);
2083 error = dmu_read(os, object, offset, size, buf,
2084 DMU_READ_NO_PREFETCH);
2087 size = doi.doi_data_block_size;
2089 offset = P2ALIGN(offset, size);
2091 ASSERT(offset < size);
2095 zgd->zgd_lr = (struct locked_range *)ztest_range_lock(zd,
2096 object, offset, size, RL_READER);
2098 error = dmu_buf_hold(os, object, offset, zgd, &db,
2099 DMU_READ_NO_PREFETCH);
2102 blkptr_t *bp = &lr->lr_blkptr;
2107 ASSERT(db->db_offset == offset);
2108 ASSERT(db->db_size == size);
2110 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2111 ztest_get_done, zgd);
2118 ztest_get_done(zgd, error);
2124 ztest_lr_alloc(size_t lrsize, char *name)
2127 size_t namesize = name ? strlen(name) + 1 : 0;
2129 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2132 bcopy(name, lr + lrsize, namesize);
2138 ztest_lr_free(void *lr, size_t lrsize, char *name)
2140 size_t namesize = name ? strlen(name) + 1 : 0;
2142 umem_free(lr, lrsize + namesize);
2146 * Lookup a bunch of objects. Returns the number of objects not found.
2149 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2154 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2156 for (int i = 0; i < count; i++, od++) {
2158 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2159 sizeof (uint64_t), 1, &od->od_object);
2161 ASSERT(error == ENOENT);
2162 ASSERT(od->od_object == 0);
2166 ztest_block_tag_t *bbt;
2167 dmu_object_info_t doi;
2169 ASSERT(od->od_object != 0);
2170 ASSERT(missing == 0); /* there should be no gaps */
2172 ztest_object_lock(zd, od->od_object, RL_READER);
2173 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2174 od->od_object, FTAG, &db));
2175 dmu_object_info_from_db(db, &doi);
2176 bbt = ztest_bt_bonus(db);
2177 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2178 od->od_type = doi.doi_type;
2179 od->od_blocksize = doi.doi_data_block_size;
2180 od->od_gen = bbt->bt_gen;
2181 dmu_buf_rele(db, FTAG);
2182 ztest_object_unlock(zd, od->od_object);
2190 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2194 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2196 for (int i = 0; i < count; i++, od++) {
2203 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2205 lr->lr_doid = od->od_dir;
2206 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2207 lr->lrz_type = od->od_crtype;
2208 lr->lrz_blocksize = od->od_crblocksize;
2209 lr->lrz_ibshift = ztest_random_ibshift();
2210 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2211 lr->lrz_dnodesize = od->od_crdnodesize;
2212 lr->lr_gen = od->od_crgen;
2213 lr->lr_crtime[0] = time(NULL);
2215 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2216 ASSERT(missing == 0);
2220 od->od_object = lr->lr_foid;
2221 od->od_type = od->od_crtype;
2222 od->od_blocksize = od->od_crblocksize;
2223 od->od_gen = od->od_crgen;
2224 ASSERT(od->od_object != 0);
2227 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2234 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2239 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2243 for (int i = count - 1; i >= 0; i--, od--) {
2250 * No object was found.
2252 if (od->od_object == 0)
2255 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2257 lr->lr_doid = od->od_dir;
2259 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2260 ASSERT3U(error, ==, ENOSPC);
2265 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2272 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2278 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2280 lr->lr_foid = object;
2281 lr->lr_offset = offset;
2282 lr->lr_length = size;
2284 BP_ZERO(&lr->lr_blkptr);
2286 bcopy(data, lr + 1, size);
2288 error = ztest_replay_write(zd, lr, B_FALSE);
2290 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2296 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2301 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2303 lr->lr_foid = object;
2304 lr->lr_offset = offset;
2305 lr->lr_length = size;
2307 error = ztest_replay_truncate(zd, lr, B_FALSE);
2309 ztest_lr_free(lr, sizeof (*lr), NULL);
2315 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2320 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2322 lr->lr_foid = object;
2326 error = ztest_replay_setattr(zd, lr, B_FALSE);
2328 ztest_lr_free(lr, sizeof (*lr), NULL);
2334 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2336 objset_t *os = zd->zd_os;
2341 txg_wait_synced(dmu_objset_pool(os), 0);
2343 ztest_object_lock(zd, object, RL_READER);
2344 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2346 tx = dmu_tx_create(os);
2348 dmu_tx_hold_write(tx, object, offset, size);
2350 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2353 dmu_prealloc(os, object, offset, size, tx);
2355 txg_wait_synced(dmu_objset_pool(os), txg);
2357 (void) dmu_free_long_range(os, object, offset, size);
2360 ztest_range_unlock(rl);
2361 ztest_object_unlock(zd, object);
2365 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2368 ztest_block_tag_t wbt;
2369 dmu_object_info_t doi;
2370 enum ztest_io_type io_type;
2374 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2375 blocksize = doi.doi_data_block_size;
2376 data = umem_alloc(blocksize, UMEM_NOFAIL);
2379 * Pick an i/o type at random, biased toward writing block tags.
2381 io_type = ztest_random(ZTEST_IO_TYPES);
2382 if (ztest_random(2) == 0)
2383 io_type = ZTEST_IO_WRITE_TAG;
2385 rw_enter(&zd->zd_zilog_lock, RW_READER);
2389 case ZTEST_IO_WRITE_TAG:
2390 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2392 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2395 case ZTEST_IO_WRITE_PATTERN:
2396 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2397 if (ztest_random(2) == 0) {
2399 * Induce fletcher2 collisions to ensure that
2400 * zio_ddt_collision() detects and resolves them
2401 * when using fletcher2-verify for deduplication.
2403 ((uint64_t *)data)[0] ^= 1ULL << 63;
2404 ((uint64_t *)data)[4] ^= 1ULL << 63;
2406 (void) ztest_write(zd, object, offset, blocksize, data);
2409 case ZTEST_IO_WRITE_ZEROES:
2410 bzero(data, blocksize);
2411 (void) ztest_write(zd, object, offset, blocksize, data);
2414 case ZTEST_IO_TRUNCATE:
2415 (void) ztest_truncate(zd, object, offset, blocksize);
2418 case ZTEST_IO_SETATTR:
2419 (void) ztest_setattr(zd, object);
2422 case ZTEST_IO_REWRITE:
2423 rw_enter(&ztest_name_lock, RW_READER);
2424 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2425 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2427 VERIFY(err == 0 || err == ENOSPC);
2428 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2429 ZFS_PROP_COMPRESSION,
2430 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2432 VERIFY(err == 0 || err == ENOSPC);
2433 rw_exit(&ztest_name_lock);
2435 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2436 DMU_READ_NO_PREFETCH));
2438 (void) ztest_write(zd, object, offset, blocksize, data);
2442 rw_exit(&zd->zd_zilog_lock);
2444 umem_free(data, blocksize);
2448 * Initialize an object description template.
2451 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2452 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2455 od->od_dir = ZTEST_DIROBJ;
2458 od->od_crtype = type;
2459 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2460 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2463 od->od_type = DMU_OT_NONE;
2464 od->od_blocksize = 0;
2467 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2468 tag, (int64_t)id, index);
2472 * Lookup or create the objects for a test using the od template.
2473 * If the objects do not all exist, or if 'remove' is specified,
2474 * remove any existing objects and create new ones. Otherwise,
2475 * use the existing objects.
2478 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2480 int count = size / sizeof (*od);
2483 mutex_enter(&zd->zd_dirobj_lock);
2484 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2485 (ztest_remove(zd, od, count) != 0 ||
2486 ztest_create(zd, od, count) != 0))
2489 mutex_exit(&zd->zd_dirobj_lock);
2496 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2498 zilog_t *zilog = zd->zd_zilog;
2500 rw_enter(&zd->zd_zilog_lock, RW_READER);
2502 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2505 * Remember the committed values in zd, which is in parent/child
2506 * shared memory. If we die, the next iteration of ztest_run()
2507 * will verify that the log really does contain this record.
2509 mutex_enter(&zilog->zl_lock);
2510 ASSERT(zd->zd_shared != NULL);
2511 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2512 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2513 mutex_exit(&zilog->zl_lock);
2515 rw_exit(&zd->zd_zilog_lock);
2519 * This function is designed to simulate the operations that occur during a
2520 * mount/unmount operation. We hold the dataset across these operations in an
2521 * attempt to expose any implicit assumptions about ZIL management.
2525 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2527 objset_t *os = zd->zd_os;
2530 * We grab the zd_dirobj_lock to ensure that no other thread is
2531 * updating the zil (i.e. adding in-memory log records) and the
2532 * zd_zilog_lock to block any I/O.
2534 mutex_enter(&zd->zd_dirobj_lock);
2535 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2537 /* zfsvfs_teardown() */
2538 zil_close(zd->zd_zilog);
2540 /* zfsvfs_setup() */
2541 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2542 zil_replay(os, zd, ztest_replay_vector);
2544 rw_exit(&zd->zd_zilog_lock);
2545 mutex_exit(&zd->zd_dirobj_lock);
2549 * Verify that we can't destroy an active pool, create an existing pool,
2550 * or create a pool with a bad vdev spec.
2554 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2556 ztest_shared_opts_t *zo = &ztest_opts;
2560 if (zo->zo_mmp_test)
2564 * Attempt to create using a bad file.
2566 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2567 VERIFY3U(ENOENT, ==,
2568 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2569 nvlist_free(nvroot);
2572 * Attempt to create using a bad mirror.
2574 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 2, 1);
2575 VERIFY3U(ENOENT, ==,
2576 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2577 nvlist_free(nvroot);
2580 * Attempt to create an existing pool. It shouldn't matter
2581 * what's in the nvroot; we should fail with EEXIST.
2583 rw_enter(&ztest_name_lock, RW_READER);
2584 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, NULL, 0, 0, 1);
2585 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2586 nvlist_free(nvroot);
2587 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2588 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2589 spa_close(spa, FTAG);
2591 rw_exit(&ztest_name_lock);
2595 * Start and then stop the MMP threads to ensure the startup and shutdown code
2596 * works properly. Actual protection and property-related code tested via ZTS.
2600 ztest_mmp_enable_disable(ztest_ds_t *zd, uint64_t id)
2602 ztest_shared_opts_t *zo = &ztest_opts;
2603 spa_t *spa = ztest_spa;
2605 if (zo->zo_mmp_test)
2609 * Since enabling MMP involves setting a property, it could not be done
2610 * while the pool is suspended.
2612 if (spa_suspended(spa))
2615 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2616 mutex_enter(&spa->spa_props_lock);
2618 zfs_multihost_fail_intervals = 0;
2620 if (!spa_multihost(spa)) {
2621 spa->spa_multihost = B_TRUE;
2622 mmp_thread_start(spa);
2625 mutex_exit(&spa->spa_props_lock);
2626 spa_config_exit(spa, SCL_CONFIG, FTAG);
2628 txg_wait_synced(spa_get_dsl(spa), 0);
2629 mmp_signal_all_threads();
2630 txg_wait_synced(spa_get_dsl(spa), 0);
2632 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2633 mutex_enter(&spa->spa_props_lock);
2635 if (spa_multihost(spa)) {
2636 mmp_thread_stop(spa);
2637 spa->spa_multihost = B_FALSE;
2640 mutex_exit(&spa->spa_props_lock);
2641 spa_config_exit(spa, SCL_CONFIG, FTAG);
2646 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2649 uint64_t initial_version = SPA_VERSION_INITIAL;
2650 uint64_t version, newversion;
2651 nvlist_t *nvroot, *props;
2654 if (ztest_opts.zo_mmp_test)
2657 mutex_enter(&ztest_vdev_lock);
2658 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2661 * Clean up from previous runs.
2663 (void) spa_destroy(name);
2665 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2666 NULL, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2669 * If we're configuring a RAIDZ device then make sure that the
2670 * the initial version is capable of supporting that feature.
2672 switch (ztest_opts.zo_raidz_parity) {
2675 initial_version = SPA_VERSION_INITIAL;
2678 initial_version = SPA_VERSION_RAIDZ2;
2681 initial_version = SPA_VERSION_RAIDZ3;
2686 * Create a pool with a spa version that can be upgraded. Pick
2687 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2690 version = ztest_random_spa_version(initial_version);
2691 } while (version > SPA_VERSION_BEFORE_FEATURES);
2693 props = fnvlist_alloc();
2694 fnvlist_add_uint64(props,
2695 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2696 VERIFY0(spa_create(name, nvroot, props, NULL));
2697 fnvlist_free(nvroot);
2698 fnvlist_free(props);
2700 VERIFY0(spa_open(name, &spa, FTAG));
2701 VERIFY3U(spa_version(spa), ==, version);
2702 newversion = ztest_random_spa_version(version + 1);
2704 if (ztest_opts.zo_verbose >= 4) {
2705 (void) printf("upgrading spa version from %llu to %llu\n",
2706 (u_longlong_t)version, (u_longlong_t)newversion);
2709 spa_upgrade(spa, newversion);
2710 VERIFY3U(spa_version(spa), >, version);
2711 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2712 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2713 spa_close(spa, FTAG);
2716 mutex_exit(&ztest_vdev_lock);
2720 ztest_spa_checkpoint(spa_t *spa)
2722 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2724 int error = spa_checkpoint(spa->spa_name);
2728 case ZFS_ERR_DEVRM_IN_PROGRESS:
2729 case ZFS_ERR_DISCARDING_CHECKPOINT:
2730 case ZFS_ERR_CHECKPOINT_EXISTS:
2733 ztest_record_enospc(FTAG);
2736 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2741 ztest_spa_discard_checkpoint(spa_t *spa)
2743 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2745 int error = spa_checkpoint_discard(spa->spa_name);
2749 case ZFS_ERR_DISCARDING_CHECKPOINT:
2750 case ZFS_ERR_NO_CHECKPOINT:
2753 fatal(0, "spa_discard_checkpoint(%s) = %d",
2754 spa->spa_name, error);
2761 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2763 spa_t *spa = ztest_spa;
2765 mutex_enter(&ztest_checkpoint_lock);
2766 if (ztest_random(2) == 0) {
2767 ztest_spa_checkpoint(spa);
2769 ztest_spa_discard_checkpoint(spa);
2771 mutex_exit(&ztest_checkpoint_lock);
2776 vdev_lookup_by_path(vdev_t *vd, const char *path)
2780 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2783 for (int c = 0; c < vd->vdev_children; c++)
2784 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2792 * Find the first available hole which can be used as a top-level.
2795 find_vdev_hole(spa_t *spa)
2797 vdev_t *rvd = spa->spa_root_vdev;
2800 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2802 for (c = 0; c < rvd->vdev_children; c++) {
2803 vdev_t *cvd = rvd->vdev_child[c];
2805 if (cvd->vdev_ishole)
2812 * Verify that vdev_add() works as expected.
2816 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2818 ztest_shared_t *zs = ztest_shared;
2819 spa_t *spa = ztest_spa;
2825 if (ztest_opts.zo_mmp_test)
2828 mutex_enter(&ztest_vdev_lock);
2829 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2831 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2833 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2836 * If we have slogs then remove them 1/4 of the time.
2838 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2839 metaslab_group_t *mg;
2842 * find the first real slog in log allocation class
2844 mg = spa_log_class(spa)->mc_rotor;
2845 while (!mg->mg_vd->vdev_islog)
2848 guid = mg->mg_vd->vdev_guid;
2850 spa_config_exit(spa, SCL_VDEV, FTAG);
2853 * We have to grab the zs_name_lock as writer to
2854 * prevent a race between removing a slog (dmu_objset_find)
2855 * and destroying a dataset. Removing the slog will
2856 * grab a reference on the dataset which may cause
2857 * dmu_objset_destroy() to fail with EBUSY thus
2858 * leaving the dataset in an inconsistent state.
2860 rw_enter(&ztest_name_lock, RW_WRITER);
2861 error = spa_vdev_remove(spa, guid, B_FALSE);
2862 rw_exit(&ztest_name_lock);
2867 case ZFS_ERR_CHECKPOINT_EXISTS:
2868 case ZFS_ERR_DISCARDING_CHECKPOINT:
2871 fatal(0, "spa_vdev_remove() = %d", error);
2874 spa_config_exit(spa, SCL_VDEV, FTAG);
2877 * Make 1/4 of the devices be log devices
2879 nvroot = make_vdev_root(NULL, NULL, NULL,
2880 ztest_opts.zo_vdev_size, 0, (ztest_random(4) == 0) ?
2881 "log" : NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
2883 error = spa_vdev_add(spa, nvroot);
2884 nvlist_free(nvroot);
2890 ztest_record_enospc("spa_vdev_add");
2893 fatal(0, "spa_vdev_add() = %d", error);
2897 mutex_exit(&ztest_vdev_lock);
2902 ztest_vdev_class_add(ztest_ds_t *zd, uint64_t id)
2904 ztest_shared_t *zs = ztest_shared;
2905 spa_t *spa = ztest_spa;
2908 const char *class = (ztest_random(2) == 0) ?
2909 VDEV_ALLOC_BIAS_SPECIAL : VDEV_ALLOC_BIAS_DEDUP;
2913 * By default add a special vdev 50% of the time
2915 if ((ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_OFF) ||
2916 (ztest_opts.zo_special_vdevs == ZTEST_VDEV_CLASS_RND &&
2917 ztest_random(2) == 0)) {
2921 mutex_enter(&ztest_vdev_lock);
2923 /* Only test with mirrors */
2924 if (zs->zs_mirrors < 2) {
2925 mutex_exit(&ztest_vdev_lock);
2929 /* requires feature@allocation_classes */
2930 if (!spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)) {
2931 mutex_exit(&ztest_vdev_lock);
2935 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2937 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2938 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2939 spa_config_exit(spa, SCL_VDEV, FTAG);
2941 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
2942 class, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
2944 error = spa_vdev_add(spa, nvroot);
2945 nvlist_free(nvroot);
2947 if (error == ENOSPC)
2948 ztest_record_enospc("spa_vdev_add");
2949 else if (error != 0)
2950 fatal(0, "spa_vdev_add() = %d", error);
2953 * 50% of the time allow small blocks in the special class
2956 spa_special_class(spa)->mc_groups == 1 && ztest_random(2) == 0) {
2957 if (ztest_opts.zo_verbose >= 3)
2958 (void) printf("Enabling special VDEV small blocks\n");
2959 (void) ztest_dsl_prop_set_uint64(zd->zd_name,
2960 ZFS_PROP_SPECIAL_SMALL_BLOCKS, 32768, B_FALSE);
2963 mutex_exit(&ztest_vdev_lock);
2965 if (ztest_opts.zo_verbose >= 3) {
2966 metaslab_class_t *mc;
2968 if (strcmp(class, VDEV_ALLOC_BIAS_SPECIAL) == 0)
2969 mc = spa_special_class(spa);
2971 mc = spa_dedup_class(spa);
2972 (void) printf("Added a %s mirrored vdev (of %d)\n",
2973 class, (int)mc->mc_groups);
2978 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2982 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2984 ztest_shared_t *zs = ztest_shared;
2985 spa_t *spa = ztest_spa;
2986 vdev_t *rvd = spa->spa_root_vdev;
2987 spa_aux_vdev_t *sav;
2992 if (ztest_opts.zo_mmp_test)
2995 if (ztest_random(2) == 0) {
2996 sav = &spa->spa_spares;
2997 aux = ZPOOL_CONFIG_SPARES;
2999 sav = &spa->spa_l2cache;
3000 aux = ZPOOL_CONFIG_L2CACHE;
3003 mutex_enter(&ztest_vdev_lock);
3005 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3007 if (sav->sav_count != 0 && ztest_random(4) == 0) {
3009 * Pick a random device to remove.
3011 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
3014 * Find an unused device we can add.
3016 zs->zs_vdev_aux = 0;
3018 char path[MAXPATHLEN];
3020 (void) snprintf(path, sizeof (path), ztest_aux_template,
3021 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
3023 for (c = 0; c < sav->sav_count; c++)
3024 if (strcmp(sav->sav_vdevs[c]->vdev_path,
3027 if (c == sav->sav_count &&
3028 vdev_lookup_by_path(rvd, path) == NULL)
3034 spa_config_exit(spa, SCL_VDEV, FTAG);
3040 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
3041 (ztest_opts.zo_vdev_size * 5) / 4, 0, NULL, 0, 0, 1);
3042 error = spa_vdev_add(spa, nvroot);
3048 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
3050 nvlist_free(nvroot);
3053 * Remove an existing device. Sometimes, dirty its
3054 * vdev state first to make sure we handle removal
3055 * of devices that have pending state changes.
3057 if (ztest_random(2) == 0)
3058 (void) vdev_online(spa, guid, 0, NULL);
3060 error = spa_vdev_remove(spa, guid, B_FALSE);
3065 case ZFS_ERR_CHECKPOINT_EXISTS:
3066 case ZFS_ERR_DISCARDING_CHECKPOINT:
3069 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
3073 mutex_exit(&ztest_vdev_lock);
3077 * split a pool if it has mirror tlvdevs
3081 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
3083 ztest_shared_t *zs = ztest_shared;
3084 spa_t *spa = ztest_spa;
3085 vdev_t *rvd = spa->spa_root_vdev;
3086 nvlist_t *tree, **child, *config, *split, **schild;
3087 uint_t c, children, schildren = 0, lastlogid = 0;
3090 if (ztest_opts.zo_mmp_test)
3093 mutex_enter(&ztest_vdev_lock);
3095 /* ensure we have a useable config; mirrors of raidz aren't supported */
3096 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
3097 mutex_exit(&ztest_vdev_lock);
3101 /* clean up the old pool, if any */
3102 (void) spa_destroy("splitp");
3104 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3106 /* generate a config from the existing config */
3107 mutex_enter(&spa->spa_props_lock);
3108 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
3110 mutex_exit(&spa->spa_props_lock);
3112 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3115 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
3116 for (c = 0; c < children; c++) {
3117 vdev_t *tvd = rvd->vdev_child[c];
3121 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
3122 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
3124 VERIFY(nvlist_add_string(schild[schildren],
3125 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
3126 VERIFY(nvlist_add_uint64(schild[schildren],
3127 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
3129 lastlogid = schildren;
3134 VERIFY(nvlist_lookup_nvlist_array(child[c],
3135 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3136 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3139 /* OK, create a config that can be used to split */
3140 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3141 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3142 VDEV_TYPE_ROOT) == 0);
3143 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3144 lastlogid != 0 ? lastlogid : schildren) == 0);
3146 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3147 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3149 for (c = 0; c < schildren; c++)
3150 nvlist_free(schild[c]);
3154 spa_config_exit(spa, SCL_VDEV, FTAG);
3156 rw_enter(&ztest_name_lock, RW_WRITER);
3157 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3158 rw_exit(&ztest_name_lock);
3160 nvlist_free(config);
3163 (void) printf("successful split - results:\n");
3164 mutex_enter(&spa_namespace_lock);
3165 show_pool_stats(spa);
3166 show_pool_stats(spa_lookup("splitp"));
3167 mutex_exit(&spa_namespace_lock);
3171 mutex_exit(&ztest_vdev_lock);
3175 * Verify that we can attach and detach devices.
3179 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3181 ztest_shared_t *zs = ztest_shared;
3182 spa_t *spa = ztest_spa;
3183 spa_aux_vdev_t *sav = &spa->spa_spares;
3184 vdev_t *rvd = spa->spa_root_vdev;
3185 vdev_t *oldvd, *newvd, *pvd;
3189 uint64_t ashift = ztest_get_ashift();
3190 uint64_t oldguid, pguid;
3191 uint64_t oldsize, newsize;
3192 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
3194 int oldvd_has_siblings = B_FALSE;
3195 int newvd_is_spare = B_FALSE;
3197 int error, expected_error;
3199 if (ztest_opts.zo_mmp_test)
3202 mutex_enter(&ztest_vdev_lock);
3203 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3205 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3208 * If a vdev is in the process of being removed, its removal may
3209 * finish while we are in progress, leading to an unexpected error
3210 * value. Don't bother trying to attach while we are in the middle
3213 if (ztest_device_removal_active) {
3214 spa_config_exit(spa, SCL_ALL, FTAG);
3215 mutex_exit(&ztest_vdev_lock);
3220 * Decide whether to do an attach or a replace.
3222 replacing = ztest_random(2);
3225 * Pick a random top-level vdev.
3227 top = ztest_random_vdev_top(spa, B_TRUE);
3230 * Pick a random leaf within it.
3232 leaf = ztest_random(leaves);
3237 oldvd = rvd->vdev_child[top];
3239 /* pick a child from the mirror */
3240 if (zs->zs_mirrors >= 1) {
3241 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3242 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3243 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3246 /* pick a child out of the raidz group */
3247 if (ztest_opts.zo_raidz > 1) {
3248 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3249 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3250 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3254 * If we're already doing an attach or replace, oldvd may be a
3255 * mirror vdev -- in which case, pick a random child.
3257 while (oldvd->vdev_children != 0) {
3258 oldvd_has_siblings = B_TRUE;
3259 ASSERT(oldvd->vdev_children >= 2);
3260 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3263 oldguid = oldvd->vdev_guid;
3264 oldsize = vdev_get_min_asize(oldvd);
3265 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3266 (void) strcpy(oldpath, oldvd->vdev_path);
3267 pvd = oldvd->vdev_parent;
3268 pguid = pvd->vdev_guid;
3271 * If oldvd has siblings, then half of the time, detach it.
3273 if (oldvd_has_siblings && ztest_random(2) == 0) {
3274 spa_config_exit(spa, SCL_ALL, FTAG);
3275 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3276 if (error != 0 && error != ENODEV && error != EBUSY &&
3277 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
3278 error != ZFS_ERR_DISCARDING_CHECKPOINT)
3279 fatal(0, "detach (%s) returned %d", oldpath, error);
3280 mutex_exit(&ztest_vdev_lock);
3285 * For the new vdev, choose with equal probability between the two
3286 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3288 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3289 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3290 newvd_is_spare = B_TRUE;
3291 (void) strcpy(newpath, newvd->vdev_path);
3293 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
3294 ztest_opts.zo_dir, ztest_opts.zo_pool,
3295 top * leaves + leaf);
3296 if (ztest_random(2) == 0)
3297 newpath[strlen(newpath) - 1] = 'b';
3298 newvd = vdev_lookup_by_path(rvd, newpath);
3303 * Reopen to ensure the vdev's asize field isn't stale.
3306 newsize = vdev_get_min_asize(newvd);
3309 * Make newsize a little bigger or smaller than oldsize.
3310 * If it's smaller, the attach should fail.
3311 * If it's larger, and we're doing a replace,
3312 * we should get dynamic LUN growth when we're done.
3314 newsize = 10 * oldsize / (9 + ztest_random(3));
3318 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3319 * unless it's a replace; in that case any non-replacing parent is OK.
3321 * If newvd is already part of the pool, it should fail with EBUSY.
3323 * If newvd is too small, it should fail with EOVERFLOW.
3325 if (pvd->vdev_ops != &vdev_mirror_ops &&
3326 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3327 pvd->vdev_ops == &vdev_replacing_ops ||
3328 pvd->vdev_ops == &vdev_spare_ops))
3329 expected_error = ENOTSUP;
3330 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3331 expected_error = ENOTSUP;
3332 else if (newvd == oldvd)
3333 expected_error = replacing ? 0 : EBUSY;
3334 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3335 expected_error = EBUSY;
3336 else if (newsize < oldsize)
3337 expected_error = EOVERFLOW;
3338 else if (ashift > oldvd->vdev_top->vdev_ashift)
3339 expected_error = EDOM;
3343 spa_config_exit(spa, SCL_ALL, FTAG);
3346 * Build the nvlist describing newpath.
3348 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3349 ashift, NULL, 0, 0, 1);
3351 error = spa_vdev_attach(spa, oldguid, root, replacing);
3356 * If our parent was the replacing vdev, but the replace completed,
3357 * then instead of failing with ENOTSUP we may either succeed,
3358 * fail with ENODEV, or fail with EOVERFLOW.
3360 if (expected_error == ENOTSUP &&
3361 (error == 0 || error == ENODEV || error == EOVERFLOW))
3362 expected_error = error;
3365 * If someone grew the LUN, the replacement may be too small.
3367 if (error == EOVERFLOW || error == EBUSY)
3368 expected_error = error;
3370 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3371 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3372 expected_error = error;
3374 /* XXX workaround 6690467 */
3375 if (error != expected_error && expected_error != EBUSY) {
3376 fatal(0, "attach (%s %llu, %s %llu, %d) "
3377 "returned %d, expected %d",
3378 oldpath, oldsize, newpath,
3379 newsize, replacing, error, expected_error);
3382 mutex_exit(&ztest_vdev_lock);
3387 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3389 spa_t *spa = ztest_spa;
3394 mutex_enter(&ztest_vdev_lock);
3396 if (ztest_device_removal_active) {
3397 mutex_exit(&ztest_vdev_lock);
3402 * Remove a random top-level vdev and wait for removal to finish.
3404 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3405 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3406 guid = vd->vdev_guid;
3407 spa_config_exit(spa, SCL_VDEV, FTAG);
3409 error = spa_vdev_remove(spa, guid, B_FALSE);
3411 ztest_device_removal_active = B_TRUE;
3412 mutex_exit(&ztest_vdev_lock);
3414 while (spa->spa_vdev_removal != NULL)
3415 txg_wait_synced(spa_get_dsl(spa), 0);
3417 mutex_exit(&ztest_vdev_lock);
3422 * The pool needs to be scrubbed after completing device removal.
3423 * Failure to do so may result in checksum errors due to the
3424 * strategy employed by ztest_fault_inject() when selecting which
3425 * offset are redundant and can be damaged.
3427 error = spa_scan(spa, POOL_SCAN_SCRUB);
3429 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3430 txg_wait_synced(spa_get_dsl(spa), 0);
3433 mutex_enter(&ztest_vdev_lock);
3434 ztest_device_removal_active = B_FALSE;
3435 mutex_exit(&ztest_vdev_lock);
3439 * Callback function which expands the physical size of the vdev.
3442 grow_vdev(vdev_t *vd, void *arg)
3444 spa_t *spa = vd->vdev_spa;
3445 size_t *newsize = arg;
3449 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3450 ASSERT(vd->vdev_ops->vdev_op_leaf);
3452 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3455 fsize = lseek(fd, 0, SEEK_END);
3456 (void) ftruncate(fd, *newsize);
3458 if (ztest_opts.zo_verbose >= 6) {
3459 (void) printf("%s grew from %lu to %lu bytes\n",
3460 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3467 * Callback function which expands a given vdev by calling vdev_online().
3471 online_vdev(vdev_t *vd, void *arg)
3473 spa_t *spa = vd->vdev_spa;
3474 vdev_t *tvd = vd->vdev_top;
3475 uint64_t guid = vd->vdev_guid;
3476 uint64_t generation = spa->spa_config_generation + 1;
3477 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3480 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3481 ASSERT(vd->vdev_ops->vdev_op_leaf);
3483 /* Calling vdev_online will initialize the new metaslabs */
3484 spa_config_exit(spa, SCL_STATE, spa);
3485 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3486 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3489 * If vdev_online returned an error or the underlying vdev_open
3490 * failed then we abort the expand. The only way to know that
3491 * vdev_open fails is by checking the returned newstate.
3493 if (error || newstate != VDEV_STATE_HEALTHY) {
3494 if (ztest_opts.zo_verbose >= 5) {
3495 (void) printf("Unable to expand vdev, state %llu, "
3496 "error %d\n", (u_longlong_t)newstate, error);
3500 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3503 * Since we dropped the lock we need to ensure that we're
3504 * still talking to the original vdev. It's possible this
3505 * vdev may have been detached/replaced while we were
3506 * trying to online it.
3508 if (generation != spa->spa_config_generation) {
3509 if (ztest_opts.zo_verbose >= 5) {
3510 (void) printf("vdev configuration has changed, "
3511 "guid %llu, state %llu, expected gen %llu, "
3514 (u_longlong_t)tvd->vdev_state,
3515 (u_longlong_t)generation,
3516 (u_longlong_t)spa->spa_config_generation);
3524 * Traverse the vdev tree calling the supplied function.
3525 * We continue to walk the tree until we either have walked all
3526 * children or we receive a non-NULL return from the callback.
3527 * If a NULL callback is passed, then we just return back the first
3528 * leaf vdev we encounter.
3531 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3533 if (vd->vdev_ops->vdev_op_leaf) {
3537 return (func(vd, arg));
3540 for (uint_t c = 0; c < vd->vdev_children; c++) {
3541 vdev_t *cvd = vd->vdev_child[c];
3542 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3549 * Verify that dynamic LUN growth works as expected.
3553 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3555 spa_t *spa = ztest_spa;
3557 metaslab_class_t *mc;
3558 metaslab_group_t *mg;
3559 size_t psize, newsize;
3561 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3563 mutex_enter(&ztest_checkpoint_lock);
3564 mutex_enter(&ztest_vdev_lock);
3565 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3568 * If there is a vdev removal in progress, it could complete while
3569 * we are running, in which case we would not be able to verify
3570 * that the metaslab_class space increased (because it decreases
3571 * when the device removal completes).
3573 if (ztest_device_removal_active) {
3574 spa_config_exit(spa, SCL_STATE, spa);
3575 mutex_exit(&ztest_vdev_lock);
3576 mutex_exit(&ztest_checkpoint_lock);
3580 top = ztest_random_vdev_top(spa, B_TRUE);
3582 tvd = spa->spa_root_vdev->vdev_child[top];
3585 old_ms_count = tvd->vdev_ms_count;
3586 old_class_space = metaslab_class_get_space(mc);
3589 * Determine the size of the first leaf vdev associated with
3590 * our top-level device.
3592 vd = vdev_walk_tree(tvd, NULL, NULL);
3593 ASSERT3P(vd, !=, NULL);
3594 ASSERT(vd->vdev_ops->vdev_op_leaf);
3596 psize = vd->vdev_psize;
3599 * We only try to expand the vdev if it's healthy, less than 4x its
3600 * original size, and it has a valid psize.
3602 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3603 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3604 spa_config_exit(spa, SCL_STATE, spa);
3605 mutex_exit(&ztest_vdev_lock);
3606 mutex_exit(&ztest_checkpoint_lock);
3610 newsize = psize + MAX(psize / 8, SPA_MAXBLOCKSIZE);
3611 ASSERT3U(newsize, >, psize);
3613 if (ztest_opts.zo_verbose >= 6) {
3614 (void) printf("Expanding LUN %s from %lu to %lu\n",
3615 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3619 * Growing the vdev is a two step process:
3620 * 1). expand the physical size (i.e. relabel)
3621 * 2). online the vdev to create the new metaslabs
3623 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3624 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3625 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3626 if (ztest_opts.zo_verbose >= 5) {
3627 (void) printf("Could not expand LUN because "
3628 "the vdev configuration changed.\n");
3630 spa_config_exit(spa, SCL_STATE, spa);
3631 mutex_exit(&ztest_vdev_lock);
3632 mutex_exit(&ztest_checkpoint_lock);
3636 spa_config_exit(spa, SCL_STATE, spa);
3639 * Expanding the LUN will update the config asynchronously,
3640 * thus we must wait for the async thread to complete any
3641 * pending tasks before proceeding.
3645 mutex_enter(&spa->spa_async_lock);
3646 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3647 mutex_exit(&spa->spa_async_lock);
3650 txg_wait_synced(spa_get_dsl(spa), 0);
3651 (void) poll(NULL, 0, 100);
3654 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3656 tvd = spa->spa_root_vdev->vdev_child[top];
3657 new_ms_count = tvd->vdev_ms_count;
3658 new_class_space = metaslab_class_get_space(mc);
3660 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3661 if (ztest_opts.zo_verbose >= 5) {
3662 (void) printf("Could not verify LUN expansion due to "
3663 "intervening vdev offline or remove.\n");
3665 spa_config_exit(spa, SCL_STATE, spa);
3666 mutex_exit(&ztest_vdev_lock);
3667 mutex_exit(&ztest_checkpoint_lock);
3672 * Make sure we were able to grow the vdev.
3674 if (new_ms_count <= old_ms_count) {
3675 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3676 old_ms_count, new_ms_count);
3680 * Make sure we were able to grow the pool.
3682 if (new_class_space <= old_class_space) {
3683 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3684 old_class_space, new_class_space);
3687 if (ztest_opts.zo_verbose >= 5) {
3688 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3690 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3691 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3692 (void) printf("%s grew from %s to %s\n",
3693 spa->spa_name, oldnumbuf, newnumbuf);
3696 spa_config_exit(spa, SCL_STATE, spa);
3697 mutex_exit(&ztest_vdev_lock);
3698 mutex_exit(&ztest_checkpoint_lock);
3702 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3706 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3709 * Create the objects common to all ztest datasets.
3711 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3712 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3716 ztest_dataset_create(char *dsname)
3718 uint64_t zilset = ztest_random(100);
3719 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3720 ztest_objset_create_cb, NULL);
3722 if (err || zilset < 80)
3725 if (ztest_opts.zo_verbose >= 6)
3726 (void) printf("Setting dataset %s to sync always\n", dsname);
3727 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3728 ZFS_SYNC_ALWAYS, B_FALSE));
3733 ztest_objset_destroy_cb(const char *name, void *arg)
3736 dmu_object_info_t doi;
3740 * Verify that the dataset contains a directory object.
3742 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3743 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3744 if (error != ENOENT) {
3745 /* We could have crashed in the middle of destroying it */
3747 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3748 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3750 dmu_objset_disown(os, FTAG);
3753 * Destroy the dataset.
3755 if (strchr(name, '@') != NULL) {
3756 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3758 VERIFY0(dsl_destroy_head(name));
3764 ztest_snapshot_create(char *osname, uint64_t id)
3766 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3769 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3771 error = dmu_objset_snapshot_one(osname, snapname);
3772 if (error == ENOSPC) {
3773 ztest_record_enospc(FTAG);
3776 if (error != 0 && error != EEXIST) {
3777 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3784 ztest_snapshot_destroy(char *osname, uint64_t id)
3786 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3789 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3792 error = dsl_destroy_snapshot(snapname, B_FALSE);
3793 if (error != 0 && error != ENOENT)
3794 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3800 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3806 char name[ZFS_MAX_DATASET_NAME_LEN];
3809 rw_enter(&ztest_name_lock, RW_READER);
3811 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3812 ztest_opts.zo_pool, (u_longlong_t)id);
3815 * If this dataset exists from a previous run, process its replay log
3816 * half of the time. If we don't replay it, then dmu_objset_destroy()
3817 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3819 if (ztest_random(2) == 0 &&
3820 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3821 ztest_zd_init(&zdtmp, NULL, os);
3822 zil_replay(os, &zdtmp, ztest_replay_vector);
3823 ztest_zd_fini(&zdtmp);
3824 dmu_objset_disown(os, FTAG);
3828 * There may be an old instance of the dataset we're about to
3829 * create lying around from a previous run. If so, destroy it
3830 * and all of its snapshots.
3832 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3833 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3836 * Verify that the destroyed dataset is no longer in the namespace.
3838 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3842 * Verify that we can create a new dataset.
3844 error = ztest_dataset_create(name);
3846 if (error == ENOSPC) {
3847 ztest_record_enospc(FTAG);
3848 rw_exit(&ztest_name_lock);
3851 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3854 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3856 ztest_zd_init(&zdtmp, NULL, os);
3859 * Open the intent log for it.
3861 zilog = zil_open(os, ztest_get_data);
3864 * Put some objects in there, do a little I/O to them,
3865 * and randomly take a couple of snapshots along the way.
3867 iters = ztest_random(5);
3868 for (int i = 0; i < iters; i++) {
3869 ztest_dmu_object_alloc_free(&zdtmp, id);
3870 if (ztest_random(iters) == 0)
3871 (void) ztest_snapshot_create(name, i);
3875 * Verify that we cannot create an existing dataset.
3877 VERIFY3U(EEXIST, ==,
3878 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3881 * Verify that we can hold an objset that is also owned.
3883 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3884 dmu_objset_rele(os2, FTAG);
3887 * Verify that we cannot own an objset that is already owned.
3890 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3893 dmu_objset_disown(os, FTAG);
3894 ztest_zd_fini(&zdtmp);
3896 rw_exit(&ztest_name_lock);
3900 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3903 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3905 rw_enter(&ztest_name_lock, RW_READER);
3906 (void) ztest_snapshot_destroy(zd->zd_name, id);
3907 (void) ztest_snapshot_create(zd->zd_name, id);
3908 rw_exit(&ztest_name_lock);
3912 * Cleanup non-standard snapshots and clones.
3915 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3917 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3918 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3919 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3920 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3921 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3924 (void) snprintf(snap1name, sizeof (snap1name),
3925 "%s@s1_%llu", osname, id);
3926 (void) snprintf(clone1name, sizeof (clone1name),
3927 "%s/c1_%llu", osname, id);
3928 (void) snprintf(snap2name, sizeof (snap2name),
3929 "%s@s2_%llu", clone1name, id);
3930 (void) snprintf(clone2name, sizeof (clone2name),
3931 "%s/c2_%llu", osname, id);
3932 (void) snprintf(snap3name, sizeof (snap3name),
3933 "%s@s3_%llu", clone1name, id);
3935 error = dsl_destroy_head(clone2name);
3936 if (error && error != ENOENT)
3937 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3938 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3939 if (error && error != ENOENT)
3940 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3941 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3942 if (error && error != ENOENT)
3943 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3944 error = dsl_destroy_head(clone1name);
3945 if (error && error != ENOENT)
3946 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3947 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3948 if (error && error != ENOENT)
3949 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3953 * Verify dsl_dataset_promote handles EBUSY
3956 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3959 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3960 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3961 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3962 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3963 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3964 char *osname = zd->zd_name;
3967 rw_enter(&ztest_name_lock, RW_READER);
3969 ztest_dsl_dataset_cleanup(osname, id);
3971 (void) snprintf(snap1name, sizeof (snap1name),
3972 "%s@s1_%llu", osname, id);
3973 (void) snprintf(clone1name, sizeof (clone1name),
3974 "%s/c1_%llu", osname, id);
3975 (void) snprintf(snap2name, sizeof (snap2name),
3976 "%s@s2_%llu", clone1name, id);
3977 (void) snprintf(clone2name, sizeof (clone2name),
3978 "%s/c2_%llu", osname, id);
3979 (void) snprintf(snap3name, sizeof (snap3name),
3980 "%s@s3_%llu", clone1name, id);
3982 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3983 if (error && error != EEXIST) {
3984 if (error == ENOSPC) {
3985 ztest_record_enospc(FTAG);
3988 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3991 error = dmu_objset_clone(clone1name, snap1name);
3993 if (error == ENOSPC) {
3994 ztest_record_enospc(FTAG);
3997 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
4000 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
4001 if (error && error != EEXIST) {
4002 if (error == ENOSPC) {
4003 ztest_record_enospc(FTAG);
4006 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
4009 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
4010 if (error && error != EEXIST) {
4011 if (error == ENOSPC) {
4012 ztest_record_enospc(FTAG);
4015 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
4018 error = dmu_objset_clone(clone2name, snap3name);
4020 if (error == ENOSPC) {
4021 ztest_record_enospc(FTAG);
4024 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
4027 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
4029 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
4030 error = dsl_dataset_promote(clone2name, NULL);
4031 if (error == ENOSPC) {
4032 dmu_objset_disown(os, FTAG);
4033 ztest_record_enospc(FTAG);
4037 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
4039 dmu_objset_disown(os, FTAG);
4042 ztest_dsl_dataset_cleanup(osname, id);
4044 rw_exit(&ztest_name_lock);
4048 * Verify that dmu_object_{alloc,free} work as expected.
4051 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
4054 int batchsize = sizeof (od) / sizeof (od[0]);
4056 for (int b = 0; b < batchsize; b++) {
4057 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER,
4062 * Destroy the previous batch of objects, create a new batch,
4063 * and do some I/O on the new objects.
4065 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
4068 while (ztest_random(4 * batchsize) != 0)
4069 ztest_io(zd, od[ztest_random(batchsize)].od_object,
4070 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4074 * Rewind the global allocator to verify object allocation backfilling.
4077 ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
4079 objset_t *os = zd->zd_os;
4080 int dnodes_per_chunk = 1 << dmu_object_alloc_chunk_shift;
4084 * Rewind the global allocator randomly back to a lower object number
4085 * to force backfilling and reclamation of recently freed dnodes.
4087 mutex_enter(&os->os_obj_lock);
4088 object = ztest_random(os->os_obj_next_chunk);
4089 os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
4090 mutex_exit(&os->os_obj_lock);
4094 * Verify that dmu_{read,write} work as expected.
4097 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
4099 objset_t *os = zd->zd_os;
4102 int i, freeit, error;
4104 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
4105 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4106 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
4107 uint64_t regions = 997;
4108 uint64_t stride = 123456789ULL;
4109 uint64_t width = 40;
4110 int free_percent = 5;
4113 * This test uses two objects, packobj and bigobj, that are always
4114 * updated together (i.e. in the same tx) so that their contents are
4115 * in sync and can be compared. Their contents relate to each other
4116 * in a simple way: packobj is a dense array of 'bufwad' structures,
4117 * while bigobj is a sparse array of the same bufwads. Specifically,
4118 * for any index n, there are three bufwads that should be identical:
4120 * packobj, at offset n * sizeof (bufwad_t)
4121 * bigobj, at the head of the nth chunk
4122 * bigobj, at the tail of the nth chunk
4124 * The chunk size is arbitrary. It doesn't have to be a power of two,
4125 * and it doesn't have any relation to the object blocksize.
4126 * The only requirement is that it can hold at least two bufwads.
4128 * Normally, we write the bufwad to each of these locations.
4129 * However, free_percent of the time we instead write zeroes to
4130 * packobj and perform a dmu_free_range() on bigobj. By comparing
4131 * bigobj to packobj, we can verify that the DMU is correctly
4132 * tracking which parts of an object are allocated and free,
4133 * and that the contents of the allocated blocks are correct.
4137 * Read the directory info. If it's the first time, set things up.
4139 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0,
4141 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4144 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4147 bigobj = od[0].od_object;
4148 packobj = od[1].od_object;
4149 chunksize = od[0].od_gen;
4150 ASSERT(chunksize == od[1].od_gen);
4153 * Prefetch a random chunk of the big object.
4154 * Our aim here is to get some async reads in flight
4155 * for blocks that we may free below; the DMU should
4156 * handle this race correctly.
4158 n = ztest_random(regions) * stride + ztest_random(width);
4159 s = 1 + ztest_random(2 * width - 1);
4160 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
4161 ZIO_PRIORITY_SYNC_READ);
4164 * Pick a random index and compute the offsets into packobj and bigobj.
4166 n = ztest_random(regions) * stride + ztest_random(width);
4167 s = 1 + ztest_random(width - 1);
4169 packoff = n * sizeof (bufwad_t);
4170 packsize = s * sizeof (bufwad_t);
4172 bigoff = n * chunksize;
4173 bigsize = s * chunksize;
4175 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
4176 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
4179 * free_percent of the time, free a range of bigobj rather than
4182 freeit = (ztest_random(100) < free_percent);
4185 * Read the current contents of our objects.
4187 error = dmu_read(os, packobj, packoff, packsize, packbuf,
4190 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
4195 * Get a tx for the mods to both packobj and bigobj.
4197 tx = dmu_tx_create(os);
4199 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4202 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4204 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4206 /* This accounts for setting the checksum/compression. */
4207 dmu_tx_hold_bonus(tx, bigobj);
4209 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4211 umem_free(packbuf, packsize);
4212 umem_free(bigbuf, bigsize);
4216 enum zio_checksum cksum;
4218 cksum = (enum zio_checksum)
4219 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4220 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4221 dmu_object_set_checksum(os, bigobj, cksum, tx);
4223 enum zio_compress comp;
4225 comp = (enum zio_compress)
4226 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4227 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4228 dmu_object_set_compress(os, bigobj, comp, tx);
4231 * For each index from n to n + s, verify that the existing bufwad
4232 * in packobj matches the bufwads at the head and tail of the
4233 * corresponding chunk in bigobj. Then update all three bufwads
4234 * with the new values we want to write out.
4236 for (i = 0; i < s; i++) {
4238 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4240 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4242 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4244 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4245 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4247 if (pack->bw_txg > txg)
4248 fatal(0, "future leak: got %llx, open txg is %llx",
4251 if (pack->bw_data != 0 && pack->bw_index != n + i)
4252 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4253 pack->bw_index, n, i);
4255 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4256 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4258 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4259 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4262 bzero(pack, sizeof (bufwad_t));
4264 pack->bw_index = n + i;
4266 pack->bw_data = 1 + ztest_random(-2ULL);
4273 * We've verified all the old bufwads, and made new ones.
4274 * Now write them out.
4276 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4279 if (ztest_opts.zo_verbose >= 7) {
4280 (void) printf("freeing offset %llx size %llx"
4282 (u_longlong_t)bigoff,
4283 (u_longlong_t)bigsize,
4286 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4288 if (ztest_opts.zo_verbose >= 7) {
4289 (void) printf("writing offset %llx size %llx"
4291 (u_longlong_t)bigoff,
4292 (u_longlong_t)bigsize,
4295 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4301 * Sanity check the stuff we just wrote.
4304 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4305 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4307 VERIFY(0 == dmu_read(os, packobj, packoff,
4308 packsize, packcheck, DMU_READ_PREFETCH));
4309 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4310 bigsize, bigcheck, DMU_READ_PREFETCH));
4312 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4313 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4315 umem_free(packcheck, packsize);
4316 umem_free(bigcheck, bigsize);
4319 umem_free(packbuf, packsize);
4320 umem_free(bigbuf, bigsize);
4324 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4325 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4333 * For each index from n to n + s, verify that the existing bufwad
4334 * in packobj matches the bufwads at the head and tail of the
4335 * corresponding chunk in bigobj. Then update all three bufwads
4336 * with the new values we want to write out.
4338 for (i = 0; i < s; i++) {
4340 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4342 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4344 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4346 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4347 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4349 if (pack->bw_txg > txg)
4350 fatal(0, "future leak: got %llx, open txg is %llx",
4353 if (pack->bw_data != 0 && pack->bw_index != n + i)
4354 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4355 pack->bw_index, n, i);
4357 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4358 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4360 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4361 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4363 pack->bw_index = n + i;
4365 pack->bw_data = 1 + ztest_random(-2ULL);
4373 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4375 objset_t *os = zd->zd_os;
4381 bufwad_t *packbuf, *bigbuf;
4382 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4383 uint64_t blocksize = ztest_random_blocksize();
4384 uint64_t chunksize = blocksize;
4385 uint64_t regions = 997;
4386 uint64_t stride = 123456789ULL;
4388 dmu_buf_t *bonus_db;
4389 arc_buf_t **bigbuf_arcbufs;
4390 dmu_object_info_t doi;
4393 * This test uses two objects, packobj and bigobj, that are always
4394 * updated together (i.e. in the same tx) so that their contents are
4395 * in sync and can be compared. Their contents relate to each other
4396 * in a simple way: packobj is a dense array of 'bufwad' structures,
4397 * while bigobj is a sparse array of the same bufwads. Specifically,
4398 * for any index n, there are three bufwads that should be identical:
4400 * packobj, at offset n * sizeof (bufwad_t)
4401 * bigobj, at the head of the nth chunk
4402 * bigobj, at the tail of the nth chunk
4404 * The chunk size is set equal to bigobj block size so that
4405 * dmu_assign_arcbuf() can be tested for object updates.
4409 * Read the directory info. If it's the first time, set things up.
4411 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4413 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4416 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4419 bigobj = od[0].od_object;
4420 packobj = od[1].od_object;
4421 blocksize = od[0].od_blocksize;
4422 chunksize = blocksize;
4423 ASSERT(chunksize == od[1].od_gen);
4425 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4426 VERIFY(ISP2(doi.doi_data_block_size));
4427 VERIFY(chunksize == doi.doi_data_block_size);
4428 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4431 * Pick a random index and compute the offsets into packobj and bigobj.
4433 n = ztest_random(regions) * stride + ztest_random(width);
4434 s = 1 + ztest_random(width - 1);
4436 packoff = n * sizeof (bufwad_t);
4437 packsize = s * sizeof (bufwad_t);
4439 bigoff = n * chunksize;
4440 bigsize = s * chunksize;
4442 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4443 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4445 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4447 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4450 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4451 * Iteration 1 test zcopy to already referenced dbufs.
4452 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4453 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4454 * Iteration 4 test zcopy when dbuf is no longer dirty.
4455 * Iteration 5 test zcopy when it can't be done.
4456 * Iteration 6 one more zcopy write.
4458 for (i = 0; i < 7; i++) {
4463 * In iteration 5 (i == 5) use arcbufs
4464 * that don't match bigobj blksz to test
4465 * dmu_assign_arcbuf() when it can't directly
4466 * assign an arcbuf to a dbuf.
4468 for (j = 0; j < s; j++) {
4471 dmu_request_arcbuf(bonus_db, chunksize);
4473 bigbuf_arcbufs[2 * j] =
4474 dmu_request_arcbuf(bonus_db, chunksize / 2);
4475 bigbuf_arcbufs[2 * j + 1] =
4476 dmu_request_arcbuf(bonus_db, chunksize / 2);
4481 * Get a tx for the mods to both packobj and bigobj.
4483 tx = dmu_tx_create(os);
4485 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4486 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4488 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4490 umem_free(packbuf, packsize);
4491 umem_free(bigbuf, bigsize);
4492 for (j = 0; j < s; j++) {
4494 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4497 bigbuf_arcbufs[2 * j]);
4499 bigbuf_arcbufs[2 * j + 1]);
4502 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4503 dmu_buf_rele(bonus_db, FTAG);
4508 * 50% of the time don't read objects in the 1st iteration to
4509 * test dmu_assign_arcbuf() for the case when there're no
4510 * existing dbufs for the specified offsets.
4512 if (i != 0 || ztest_random(2) != 0) {
4513 error = dmu_read(os, packobj, packoff,
4514 packsize, packbuf, DMU_READ_PREFETCH);
4516 error = dmu_read(os, bigobj, bigoff, bigsize,
4517 bigbuf, DMU_READ_PREFETCH);
4520 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4524 * We've verified all the old bufwads, and made new ones.
4525 * Now write them out.
4527 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4528 if (ztest_opts.zo_verbose >= 7) {
4529 (void) printf("writing offset %llx size %llx"
4531 (u_longlong_t)bigoff,
4532 (u_longlong_t)bigsize,
4535 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4538 bcopy((caddr_t)bigbuf + (off - bigoff),
4539 bigbuf_arcbufs[j]->b_data, chunksize);
4541 bcopy((caddr_t)bigbuf + (off - bigoff),
4542 bigbuf_arcbufs[2 * j]->b_data,
4544 bcopy((caddr_t)bigbuf + (off - bigoff) +
4546 bigbuf_arcbufs[2 * j + 1]->b_data,
4551 VERIFY(dmu_buf_hold(os, bigobj, off,
4552 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4555 dmu_assign_arcbuf(bonus_db, off,
4556 bigbuf_arcbufs[j], tx);
4558 dmu_assign_arcbuf(bonus_db, off,
4559 bigbuf_arcbufs[2 * j], tx);
4560 dmu_assign_arcbuf(bonus_db,
4561 off + chunksize / 2,
4562 bigbuf_arcbufs[2 * j + 1], tx);
4565 dmu_buf_rele(dbt, FTAG);
4571 * Sanity check the stuff we just wrote.
4574 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4575 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4577 VERIFY(0 == dmu_read(os, packobj, packoff,
4578 packsize, packcheck, DMU_READ_PREFETCH));
4579 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4580 bigsize, bigcheck, DMU_READ_PREFETCH));
4582 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4583 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4585 umem_free(packcheck, packsize);
4586 umem_free(bigcheck, bigsize);
4589 txg_wait_open(dmu_objset_pool(os), 0);
4590 } else if (i == 3) {
4591 txg_wait_synced(dmu_objset_pool(os), 0);
4595 dmu_buf_rele(bonus_db, FTAG);
4596 umem_free(packbuf, packsize);
4597 umem_free(bigbuf, bigsize);
4598 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4603 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4606 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4607 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4610 * Have multiple threads write to large offsets in an object
4611 * to verify that parallel writes to an object -- even to the
4612 * same blocks within the object -- doesn't cause any trouble.
4614 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER,
4617 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4620 while (ztest_random(10) != 0)
4621 ztest_io(zd, od[0].od_object, offset);
4625 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4628 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4629 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4630 uint64_t count = ztest_random(20) + 1;
4631 uint64_t blocksize = ztest_random_blocksize();
4634 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
4637 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4640 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4643 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4645 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4647 while (ztest_random(count) != 0) {
4648 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4649 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4652 while (ztest_random(4) != 0)
4653 ztest_io(zd, od[0].od_object, randoff);
4656 umem_free(data, blocksize);
4660 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4662 #define ZTEST_ZAP_MIN_INTS 1
4663 #define ZTEST_ZAP_MAX_INTS 4
4664 #define ZTEST_ZAP_MAX_PROPS 1000
4667 ztest_zap(ztest_ds_t *zd, uint64_t id)
4669 objset_t *os = zd->zd_os;
4672 uint64_t txg, last_txg;
4673 uint64_t value[ZTEST_ZAP_MAX_INTS];
4674 uint64_t zl_ints, zl_intsize, prop;
4677 char propname[100], txgname[100];
4679 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4681 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4683 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4686 object = od[0].od_object;
4689 * Generate a known hash collision, and verify that
4690 * we can lookup and remove both entries.
4692 tx = dmu_tx_create(os);
4693 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4694 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4697 for (i = 0; i < 2; i++) {
4699 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4702 for (i = 0; i < 2; i++) {
4703 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4704 sizeof (uint64_t), 1, &value[i], tx));
4706 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4707 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4708 ASSERT3U(zl_ints, ==, 1);
4710 for (i = 0; i < 2; i++) {
4711 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4716 * Generate a buch of random entries.
4718 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4720 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4721 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4722 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4723 bzero(value, sizeof (value));
4727 * If these zap entries already exist, validate their contents.
4729 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4731 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4732 ASSERT3U(zl_ints, ==, 1);
4734 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4735 zl_ints, &last_txg) == 0);
4737 VERIFY(zap_length(os, object, propname, &zl_intsize,
4740 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4741 ASSERT3U(zl_ints, ==, ints);
4743 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4744 zl_ints, value) == 0);
4746 for (i = 0; i < ints; i++) {
4747 ASSERT3U(value[i], ==, last_txg + object + i);
4750 ASSERT3U(error, ==, ENOENT);
4754 * Atomically update two entries in our zap object.
4755 * The first is named txg_%llu, and contains the txg
4756 * in which the property was last updated. The second
4757 * is named prop_%llu, and the nth element of its value
4758 * should be txg + object + n.
4760 tx = dmu_tx_create(os);
4761 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4762 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4767 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4769 for (i = 0; i < ints; i++)
4770 value[i] = txg + object + i;
4772 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4774 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4780 * Remove a random pair of entries.
4782 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4783 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4784 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4786 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4788 if (error == ENOENT)
4793 tx = dmu_tx_create(os);
4794 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4795 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4798 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4799 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4804 * Testcase to test the upgrading of a microzap to fatzap.
4807 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4809 objset_t *os = zd->zd_os;
4811 uint64_t object, txg;
4813 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4815 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4818 object = od[0].od_object;
4821 * Add entries to this ZAP and make sure it spills over
4822 * and gets upgraded to a fatzap. Also, since we are adding
4823 * 2050 entries we should see ptrtbl growth and leaf-block split.
4825 for (int i = 0; i < 2050; i++) {
4826 char name[ZFS_MAX_DATASET_NAME_LEN];
4831 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4834 tx = dmu_tx_create(os);
4835 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4836 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4839 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4841 ASSERT(error == 0 || error == EEXIST);
4848 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4850 objset_t *os = zd->zd_os;
4852 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4854 int i, namelen, error;
4855 int micro = ztest_random(2);
4856 char name[20], string_value[20];
4859 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER,
4862 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4865 object = od[0].od_object;
4868 * Generate a random name of the form 'xxx.....' where each
4869 * x is a random printable character and the dots are dots.
4870 * There are 94 such characters, and the name length goes from
4871 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4873 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4875 for (i = 0; i < 3; i++)
4876 name[i] = '!' + ztest_random('~' - '!' + 1);
4877 for (; i < namelen - 1; i++)
4881 if ((namelen & 1) || micro) {
4882 wsize = sizeof (txg);
4888 data = string_value;
4892 VERIFY0(zap_count(os, object, &count));
4893 ASSERT(count != -1ULL);
4896 * Select an operation: length, lookup, add, update, remove.
4898 i = ztest_random(5);
4901 tx = dmu_tx_create(os);
4902 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4903 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4906 bcopy(name, string_value, namelen);
4910 bzero(string_value, namelen);
4916 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4918 ASSERT3U(wsize, ==, zl_wsize);
4919 ASSERT3U(wc, ==, zl_wc);
4921 ASSERT3U(error, ==, ENOENT);
4926 error = zap_lookup(os, object, name, wsize, wc, data);
4928 if (data == string_value &&
4929 bcmp(name, data, namelen) != 0)
4930 fatal(0, "name '%s' != val '%s' len %d",
4931 name, data, namelen);
4933 ASSERT3U(error, ==, ENOENT);
4938 error = zap_add(os, object, name, wsize, wc, data, tx);
4939 ASSERT(error == 0 || error == EEXIST);
4943 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4947 error = zap_remove(os, object, name, tx);
4948 ASSERT(error == 0 || error == ENOENT);
4957 * Commit callback data.
4959 typedef struct ztest_cb_data {
4960 list_node_t zcd_node;
4962 int zcd_expected_err;
4963 boolean_t zcd_added;
4964 boolean_t zcd_called;
4968 /* This is the actual commit callback function */
4970 ztest_commit_callback(void *arg, int error)
4972 ztest_cb_data_t *data = arg;
4973 uint64_t synced_txg;
4975 VERIFY(data != NULL);
4976 VERIFY3S(data->zcd_expected_err, ==, error);
4977 VERIFY(!data->zcd_called);
4979 synced_txg = spa_last_synced_txg(data->zcd_spa);
4980 if (data->zcd_txg > synced_txg)
4981 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4982 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4985 data->zcd_called = B_TRUE;
4987 if (error == ECANCELED) {
4988 ASSERT0(data->zcd_txg);
4989 ASSERT(!data->zcd_added);
4992 * The private callback data should be destroyed here, but
4993 * since we are going to check the zcd_called field after
4994 * dmu_tx_abort(), we will destroy it there.
4999 /* Was this callback added to the global callback list? */
5000 if (!data->zcd_added)
5003 ASSERT3U(data->zcd_txg, !=, 0);
5005 /* Remove our callback from the list */
5006 mutex_enter(&zcl.zcl_callbacks_lock);
5007 list_remove(&zcl.zcl_callbacks, data);
5008 mutex_exit(&zcl.zcl_callbacks_lock);
5011 umem_free(data, sizeof (ztest_cb_data_t));
5014 /* Allocate and initialize callback data structure */
5015 static ztest_cb_data_t *
5016 ztest_create_cb_data(objset_t *os, uint64_t txg)
5018 ztest_cb_data_t *cb_data;
5020 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
5022 cb_data->zcd_txg = txg;
5023 cb_data->zcd_spa = dmu_objset_spa(os);
5029 * If a number of txgs equal to this threshold have been created after a commit
5030 * callback has been registered but not called, then we assume there is an
5031 * implementation bug.
5033 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
5036 * Commit callback test.
5039 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
5041 objset_t *os = zd->zd_os;
5044 ztest_cb_data_t *cb_data[3], *tmp_cb;
5045 uint64_t old_txg, txg;
5048 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
5050 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5053 tx = dmu_tx_create(os);
5055 cb_data[0] = ztest_create_cb_data(os, 0);
5056 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
5058 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
5060 /* Every once in a while, abort the transaction on purpose */
5061 if (ztest_random(100) == 0)
5065 error = dmu_tx_assign(tx, TXG_NOWAIT);
5067 txg = error ? 0 : dmu_tx_get_txg(tx);
5069 cb_data[0]->zcd_txg = txg;
5070 cb_data[1] = ztest_create_cb_data(os, txg);
5071 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
5075 * It's not a strict requirement to call the registered
5076 * callbacks from inside dmu_tx_abort(), but that's what
5077 * it's supposed to happen in the current implementation
5078 * so we will check for that.
5080 for (i = 0; i < 2; i++) {
5081 cb_data[i]->zcd_expected_err = ECANCELED;
5082 VERIFY(!cb_data[i]->zcd_called);
5087 for (i = 0; i < 2; i++) {
5088 VERIFY(cb_data[i]->zcd_called);
5089 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
5095 cb_data[2] = ztest_create_cb_data(os, txg);
5096 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
5099 * Read existing data to make sure there isn't a future leak.
5101 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
5102 &old_txg, DMU_READ_PREFETCH));
5105 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
5108 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
5110 mutex_enter(&zcl.zcl_callbacks_lock);
5113 * Since commit callbacks don't have any ordering requirement and since
5114 * it is theoretically possible for a commit callback to be called
5115 * after an arbitrary amount of time has elapsed since its txg has been
5116 * synced, it is difficult to reliably determine whether a commit
5117 * callback hasn't been called due to high load or due to a flawed
5120 * In practice, we will assume that if after a certain number of txgs a
5121 * commit callback hasn't been called, then most likely there's an
5122 * implementation bug..
5124 tmp_cb = list_head(&zcl.zcl_callbacks);
5125 if (tmp_cb != NULL &&
5126 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
5127 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
5128 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
5132 * Let's find the place to insert our callbacks.
5134 * Even though the list is ordered by txg, it is possible for the
5135 * insertion point to not be the end because our txg may already be
5136 * quiescing at this point and other callbacks in the open txg
5137 * (from other objsets) may have sneaked in.
5139 tmp_cb = list_tail(&zcl.zcl_callbacks);
5140 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
5141 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
5143 /* Add the 3 callbacks to the list */
5144 for (i = 0; i < 3; i++) {
5146 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
5148 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
5151 cb_data[i]->zcd_added = B_TRUE;
5152 VERIFY(!cb_data[i]->zcd_called);
5154 tmp_cb = cb_data[i];
5157 mutex_exit(&zcl.zcl_callbacks_lock);
5163 * Visit each object in the dataset. Verify that its properties
5164 * are consistent what was stored in the block tag when it was created,
5165 * and that its unused bonus buffer space has not been overwritten.
5168 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5170 objset_t *os = zd->zd_os;
5174 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5175 ztest_block_tag_t *bt = NULL;
5176 dmu_object_info_t doi;
5179 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
5182 dmu_object_info_from_db(db, &doi);
5183 if (doi.doi_bonus_size >= sizeof (*bt))
5184 bt = ztest_bt_bonus(db);
5186 if (bt && bt->bt_magic == BT_MAGIC) {
5187 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5188 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5190 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5193 dmu_buf_rele(db, FTAG);
5199 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5201 zfs_prop_t proplist[] = {
5203 ZFS_PROP_COMPRESSION,
5208 rw_enter(&ztest_name_lock, RW_READER);
5210 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5211 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5212 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5214 rw_exit(&ztest_name_lock);
5219 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
5221 rw_enter(&ztest_name_lock, RW_READER);
5223 int error = dmu_objset_remap_indirects(zd->zd_name);
5224 if (error == ENOSPC)
5228 rw_exit(&ztest_name_lock);
5233 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5235 nvlist_t *props = NULL;
5237 rw_enter(&ztest_name_lock, RW_READER);
5239 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5240 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5242 VERIFY0(spa_prop_get(ztest_spa, &props));
5244 if (ztest_opts.zo_verbose >= 6)
5245 dump_nvlist(props, 4);
5249 rw_exit(&ztest_name_lock);
5253 user_release_one(const char *snapname, const char *holdname)
5255 nvlist_t *snaps, *holds;
5258 snaps = fnvlist_alloc();
5259 holds = fnvlist_alloc();
5260 fnvlist_add_boolean(holds, holdname);
5261 fnvlist_add_nvlist(snaps, snapname, holds);
5262 fnvlist_free(holds);
5263 error = dsl_dataset_user_release(snaps, NULL);
5264 fnvlist_free(snaps);
5269 * Test snapshot hold/release and deferred destroy.
5272 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5275 objset_t *os = zd->zd_os;
5279 char clonename[100];
5281 char osname[ZFS_MAX_DATASET_NAME_LEN];
5284 rw_enter(&ztest_name_lock, RW_READER);
5286 dmu_objset_name(os, osname);
5288 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
5289 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5290 (void) snprintf(clonename, sizeof (clonename),
5291 "%s/ch1_%llu", osname, id);
5292 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
5295 * Clean up from any previous run.
5297 error = dsl_destroy_head(clonename);
5298 if (error != ENOENT)
5300 error = user_release_one(fullname, tag);
5301 if (error != ESRCH && error != ENOENT)
5303 error = dsl_destroy_snapshot(fullname, B_FALSE);
5304 if (error != ENOENT)
5308 * Create snapshot, clone it, mark snap for deferred destroy,
5309 * destroy clone, verify snap was also destroyed.
5311 error = dmu_objset_snapshot_one(osname, snapname);
5313 if (error == ENOSPC) {
5314 ztest_record_enospc("dmu_objset_snapshot");
5317 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5320 error = dmu_objset_clone(clonename, fullname);
5322 if (error == ENOSPC) {
5323 ztest_record_enospc("dmu_objset_clone");
5326 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5329 error = dsl_destroy_snapshot(fullname, B_TRUE);
5331 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5335 error = dsl_destroy_head(clonename);
5337 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5339 error = dmu_objset_hold(fullname, FTAG, &origin);
5340 if (error != ENOENT)
5341 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5344 * Create snapshot, add temporary hold, verify that we can't
5345 * destroy a held snapshot, mark for deferred destroy,
5346 * release hold, verify snapshot was destroyed.
5348 error = dmu_objset_snapshot_one(osname, snapname);
5350 if (error == ENOSPC) {
5351 ztest_record_enospc("dmu_objset_snapshot");
5354 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5357 holds = fnvlist_alloc();
5358 fnvlist_add_string(holds, fullname, tag);
5359 error = dsl_dataset_user_hold(holds, 0, NULL);
5360 fnvlist_free(holds);
5362 if (error == ENOSPC) {
5363 ztest_record_enospc("dsl_dataset_user_hold");
5366 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5367 fullname, tag, error);
5370 error = dsl_destroy_snapshot(fullname, B_FALSE);
5371 if (error != EBUSY) {
5372 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5376 error = dsl_destroy_snapshot(fullname, B_TRUE);
5378 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5382 error = user_release_one(fullname, tag);
5384 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5386 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5389 rw_exit(&ztest_name_lock);
5393 * Inject random faults into the on-disk data.
5397 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5399 ztest_shared_t *zs = ztest_shared;
5400 spa_t *spa = ztest_spa;
5404 uint64_t bad = 0x1990c0ffeedecadeULL;
5406 char path0[MAXPATHLEN];
5407 char pathrand[MAXPATHLEN];
5409 int bshift = SPA_MAXBLOCKSHIFT + 2;
5415 boolean_t islog = B_FALSE;
5417 mutex_enter(&ztest_vdev_lock);
5420 * Device removal is in progress, fault injection must be disabled
5421 * until it completes and the pool is scrubbed. The fault injection
5422 * strategy for damaging blocks does not take in to account evacuated
5423 * blocks which may have already been damaged.
5425 if (ztest_device_removal_active) {
5426 mutex_exit(&ztest_vdev_lock);
5430 maxfaults = MAXFAULTS();
5431 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5432 mirror_save = zs->zs_mirrors;
5433 mutex_exit(&ztest_vdev_lock);
5435 ASSERT(leaves >= 1);
5438 * Grab the name lock as reader. There are some operations
5439 * which don't like to have their vdevs changed while
5440 * they are in progress (i.e. spa_change_guid). Those
5441 * operations will have grabbed the name lock as writer.
5443 rw_enter(&ztest_name_lock, RW_READER);
5446 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5448 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5450 if (ztest_random(2) == 0) {
5452 * Inject errors on a normal data device or slog device.
5454 top = ztest_random_vdev_top(spa, B_TRUE);
5455 leaf = ztest_random(leaves) + zs->zs_splits;
5458 * Generate paths to the first leaf in this top-level vdev,
5459 * and to the random leaf we selected. We'll induce transient
5460 * write failures and random online/offline activity on leaf 0,
5461 * and we'll write random garbage to the randomly chosen leaf.
5463 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5464 ztest_opts.zo_dir, ztest_opts.zo_pool,
5465 top * leaves + zs->zs_splits);
5466 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5467 ztest_opts.zo_dir, ztest_opts.zo_pool,
5468 top * leaves + leaf);
5470 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5471 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5475 * If the top-level vdev needs to be resilvered
5476 * then we only allow faults on the device that is
5479 if (vd0 != NULL && maxfaults != 1 &&
5480 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5481 vd0->vdev_resilver_txg != 0)) {
5483 * Make vd0 explicitly claim to be unreadable,
5484 * or unwriteable, or reach behind its back
5485 * and close the underlying fd. We can do this if
5486 * maxfaults == 0 because we'll fail and reexecute,
5487 * and we can do it if maxfaults >= 2 because we'll
5488 * have enough redundancy. If maxfaults == 1, the
5489 * combination of this with injection of random data
5490 * corruption below exceeds the pool's fault tolerance.
5492 vdev_file_t *vf = vd0->vdev_tsd;
5494 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5495 (long long)vd0->vdev_id, (int)maxfaults);
5497 if (vf != NULL && ztest_random(3) == 0) {
5498 (void) close(vf->vf_vnode->v_fd);
5499 vf->vf_vnode->v_fd = -1;
5500 } else if (ztest_random(2) == 0) {
5501 vd0->vdev_cant_read = B_TRUE;
5503 vd0->vdev_cant_write = B_TRUE;
5505 guid0 = vd0->vdev_guid;
5509 * Inject errors on an l2cache device.
5511 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5513 if (sav->sav_count == 0) {
5514 spa_config_exit(spa, SCL_STATE, FTAG);
5515 rw_exit(&ztest_name_lock);
5518 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5519 guid0 = vd0->vdev_guid;
5520 (void) strcpy(path0, vd0->vdev_path);
5521 (void) strcpy(pathrand, vd0->vdev_path);
5525 maxfaults = INT_MAX; /* no limit on cache devices */
5528 spa_config_exit(spa, SCL_STATE, FTAG);
5529 rw_exit(&ztest_name_lock);
5532 * If we can tolerate two or more faults, or we're dealing
5533 * with a slog, randomly online/offline vd0.
5535 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5536 if (ztest_random(10) < 6) {
5537 int flags = (ztest_random(2) == 0 ?
5538 ZFS_OFFLINE_TEMPORARY : 0);
5541 * We have to grab the zs_name_lock as writer to
5542 * prevent a race between offlining a slog and
5543 * destroying a dataset. Offlining the slog will
5544 * grab a reference on the dataset which may cause
5545 * dmu_objset_destroy() to fail with EBUSY thus
5546 * leaving the dataset in an inconsistent state.
5549 rw_enter(&ztest_name_lock, RW_WRITER);
5551 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5554 rw_exit(&ztest_name_lock);
5557 * Ideally we would like to be able to randomly
5558 * call vdev_[on|off]line without holding locks
5559 * to force unpredictable failures but the side
5560 * effects of vdev_[on|off]line prevent us from
5561 * doing so. We grab the ztest_vdev_lock here to
5562 * prevent a race between injection testing and
5565 mutex_enter(&ztest_vdev_lock);
5566 (void) vdev_online(spa, guid0, 0, NULL);
5567 mutex_exit(&ztest_vdev_lock);
5575 * We have at least single-fault tolerance, so inject data corruption.
5577 fd = open(pathrand, O_RDWR);
5579 if (fd == -1) /* we hit a gap in the device namespace */
5582 fsize = lseek(fd, 0, SEEK_END);
5584 while (--iters != 0) {
5586 * The offset must be chosen carefully to ensure that
5587 * we do not inject a given logical block with errors
5588 * on two different leaf devices, because ZFS can not
5589 * tolerate that (if maxfaults==1).
5591 * We divide each leaf into chunks of size
5592 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5593 * there is a series of ranges to which we can inject errors.
5594 * Each range can accept errors on only a single leaf vdev.
5595 * The error injection ranges are separated by ranges
5596 * which we will not inject errors on any device (DMZs).
5597 * Each DMZ must be large enough such that a single block
5598 * can not straddle it, so that a single block can not be
5599 * a target in two different injection ranges (on different
5602 * For example, with 3 leaves, each chunk looks like:
5603 * 0 to 32M: injection range for leaf 0
5604 * 32M to 64M: DMZ - no injection allowed
5605 * 64M to 96M: injection range for leaf 1
5606 * 96M to 128M: DMZ - no injection allowed
5607 * 128M to 160M: injection range for leaf 2
5608 * 160M to 192M: DMZ - no injection allowed
5610 offset = ztest_random(fsize / (leaves << bshift)) *
5611 (leaves << bshift) + (leaf << bshift) +
5612 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5615 * Only allow damage to the labels at one end of the vdev.
5617 * If all labels are damaged, the device will be totally
5618 * inaccessible, which will result in loss of data,
5619 * because we also damage (parts of) the other side of
5622 * Additionally, we will always have both an even and an
5623 * odd label, so that we can handle crashes in the
5624 * middle of vdev_config_sync().
5626 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5630 * The two end labels are stored at the "end" of the disk, but
5631 * the end of the disk (vdev_psize) is aligned to
5632 * sizeof (vdev_label_t).
5634 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5635 if ((leaf & 1) == 1 &&
5636 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5639 mutex_enter(&ztest_vdev_lock);
5640 if (mirror_save != zs->zs_mirrors) {
5641 mutex_exit(&ztest_vdev_lock);
5646 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5647 fatal(1, "can't inject bad word at 0x%llx in %s",
5650 mutex_exit(&ztest_vdev_lock);
5652 if (ztest_opts.zo_verbose >= 7)
5653 (void) printf("injected bad word into %s,"
5654 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5661 * Verify that DDT repair works as expected.
5664 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5666 ztest_shared_t *zs = ztest_shared;
5667 spa_t *spa = ztest_spa;
5668 objset_t *os = zd->zd_os;
5670 uint64_t object, blocksize, txg, pattern, psize;
5671 enum zio_checksum checksum = spa_dedup_checksum(spa);
5676 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5678 blocksize = ztest_random_blocksize();
5679 blocksize = MIN(blocksize, 2048); /* because we write so many */
5681 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize,
5684 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5688 * Take the name lock as writer to prevent anyone else from changing
5689 * the pool and dataset properies we need to maintain during this test.
5691 rw_enter(&ztest_name_lock, RW_WRITER);
5693 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5695 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5697 rw_exit(&ztest_name_lock);
5701 dmu_objset_stats_t dds;
5702 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5703 dmu_objset_fast_stat(os, &dds);
5704 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5706 object = od[0].od_object;
5707 blocksize = od[0].od_blocksize;
5708 pattern = zs->zs_guid ^ dds.dds_guid;
5710 ASSERT(object != 0);
5712 tx = dmu_tx_create(os);
5713 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5714 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5716 rw_exit(&ztest_name_lock);
5721 * Write all the copies of our block.
5723 for (int i = 0; i < copies; i++) {
5724 uint64_t offset = i * blocksize;
5725 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5726 DMU_READ_NO_PREFETCH);
5728 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5729 os, (long long)object, (long long) offset, error);
5731 ASSERT(db->db_offset == offset);
5732 ASSERT(db->db_size == blocksize);
5733 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5734 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5735 dmu_buf_will_fill(db, tx);
5736 ztest_pattern_set(db->db_data, db->db_size, pattern);
5737 dmu_buf_rele(db, FTAG);
5741 txg_wait_synced(spa_get_dsl(spa), txg);
5744 * Find out what block we got.
5746 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5747 DMU_READ_NO_PREFETCH));
5748 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5749 dmu_buf_rele(db, FTAG);
5752 * Damage the block. Dedup-ditto will save us when we read it later.
5754 psize = BP_GET_PSIZE(&blk);
5755 abd = abd_alloc_linear(psize, B_TRUE);
5756 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5758 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5759 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5760 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5764 rw_exit(&ztest_name_lock);
5772 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5774 spa_t *spa = ztest_spa;
5777 * Scrub in progress by device removal.
5779 if (ztest_device_removal_active)
5782 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5783 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5784 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5788 * Change the guid for the pool.
5792 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5794 spa_t *spa = ztest_spa;
5795 uint64_t orig, load;
5798 if (ztest_opts.zo_mmp_test)
5801 orig = spa_guid(spa);
5802 load = spa_load_guid(spa);
5804 rw_enter(&ztest_name_lock, RW_WRITER);
5805 error = spa_change_guid(spa);
5806 rw_exit(&ztest_name_lock);
5811 if (ztest_opts.zo_verbose >= 4) {
5812 (void) printf("Changed guid old %llu -> %llu\n",
5813 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5816 VERIFY3U(orig, !=, spa_guid(spa));
5817 VERIFY3U(load, ==, spa_load_guid(spa));
5821 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5826 if (vd->vdev_children == 0)
5829 vdev_t *eligible[vd->vdev_children];
5830 int eligible_idx = 0, i;
5831 for (i = 0; i < vd->vdev_children; i++) {
5832 vdev_t *cvd = vd->vdev_child[i];
5833 if (cvd->vdev_top->vdev_removing)
5835 if (cvd->vdev_children > 0 ||
5836 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5837 eligible[eligible_idx++] = cvd;
5840 VERIFY(eligible_idx > 0);
5842 uint64_t child_no = ztest_random(eligible_idx);
5843 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5848 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5850 spa_t *spa = ztest_spa;
5853 mutex_enter(&ztest_vdev_lock);
5855 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5857 /* Random leaf vdev */
5858 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5859 if (rand_vd == NULL) {
5860 spa_config_exit(spa, SCL_VDEV, FTAG);
5861 mutex_exit(&ztest_vdev_lock);
5866 * The random vdev we've selected may change as soon as we
5867 * drop the spa_config_lock. We create local copies of things
5868 * we're interested in.
5870 uint64_t guid = rand_vd->vdev_guid;
5871 char *path = strdup(rand_vd->vdev_path);
5872 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5874 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5875 spa_config_exit(spa, SCL_VDEV, FTAG);
5877 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5878 error = spa_vdev_initialize(spa, guid, cmd);
5880 case POOL_INITIALIZE_CANCEL:
5881 if (ztest_opts.zo_verbose >= 4) {
5882 (void) printf("Cancel initialize %s", path);
5884 (void) printf(" failed (no initialize active)");
5885 (void) printf("\n");
5888 case POOL_INITIALIZE_DO:
5889 if (ztest_opts.zo_verbose >= 4) {
5890 (void) printf("Start initialize %s", path);
5891 if (active && error == 0)
5892 (void) printf(" failed (already active)");
5893 else if (error != 0)
5894 (void) printf(" failed (error %d)", error);
5895 (void) printf("\n");
5898 case POOL_INITIALIZE_SUSPEND:
5899 if (ztest_opts.zo_verbose >= 4) {
5900 (void) printf("Suspend initialize %s", path);
5902 (void) printf(" failed (no initialize active)");
5903 (void) printf("\n");
5908 mutex_exit(&ztest_vdev_lock);
5912 * Verify pool integrity by running zdb.
5915 ztest_run_zdb(char *pool)
5918 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5926 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5928 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5929 bin = strstr(zdb, "/usr/bin/");
5930 ztest = strstr(bin, "/ztest");
5932 isalen = ztest - isa;
5936 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s "
5937 "-o zfs_reconstruct_indirect_combinations_max=65536 %s",
5940 ztest_opts.zo_verbose >= 3 ? "s" : "",
5941 ztest_opts.zo_verbose >= 4 ? "v" : "",
5946 if (ztest_opts.zo_verbose >= 5)
5947 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5949 fp = popen(zdb, "r");
5952 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5953 if (ztest_opts.zo_verbose >= 3)
5954 (void) printf("%s", zbuf);
5956 status = pclose(fp);
5961 ztest_dump_core = 0;
5962 if (WIFEXITED(status))
5963 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5965 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5969 ztest_walk_pool_directory(char *header)
5973 if (ztest_opts.zo_verbose >= 6)
5974 (void) printf("%s\n", header);
5976 mutex_enter(&spa_namespace_lock);
5977 while ((spa = spa_next(spa)) != NULL)
5978 if (ztest_opts.zo_verbose >= 6)
5979 (void) printf("\t%s\n", spa_name(spa));
5980 mutex_exit(&spa_namespace_lock);
5984 ztest_spa_import_export(char *oldname, char *newname)
5986 nvlist_t *config, *newconfig;
5991 if (ztest_opts.zo_verbose >= 4) {
5992 (void) printf("import/export: old = %s, new = %s\n",
5997 * Clean up from previous runs.
5999 (void) spa_destroy(newname);
6002 * Get the pool's configuration and guid.
6004 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
6007 * Kick off a scrub to tickle scrub/export races.
6009 if (ztest_random(2) == 0)
6010 (void) spa_scan(spa, POOL_SCAN_SCRUB);
6012 pool_guid = spa_guid(spa);
6013 spa_close(spa, FTAG);
6015 ztest_walk_pool_directory("pools before export");
6020 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
6022 ztest_walk_pool_directory("pools after export");
6027 newconfig = spa_tryimport(config);
6028 ASSERT(newconfig != NULL);
6029 nvlist_free(newconfig);
6032 * Import it under the new name.
6034 error = spa_import(newname, config, NULL, 0);
6036 dump_nvlist(config, 0);
6037 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6038 oldname, newname, error);
6041 ztest_walk_pool_directory("pools after import");
6044 * Try to import it again -- should fail with EEXIST.
6046 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6049 * Try to import it under a different name -- should fail with EEXIST.
6051 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6054 * Verify that the pool is no longer visible under the old name.
6056 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6059 * Verify that we can open and close the pool using the new name.
6061 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6062 ASSERT(pool_guid == spa_guid(spa));
6063 spa_close(spa, FTAG);
6065 nvlist_free(config);
6069 ztest_resume(spa_t *spa)
6071 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6072 (void) printf("resuming from suspended state\n");
6073 spa_vdev_state_enter(spa, SCL_NONE);
6074 vdev_clear(spa, NULL);
6075 (void) spa_vdev_state_exit(spa, NULL, 0);
6076 (void) zio_resume(spa);
6080 ztest_resume_thread(void *arg)
6084 while (!ztest_exiting) {
6085 if (spa_suspended(spa))
6087 (void) poll(NULL, 0, 100);
6090 * Periodically change the zfs_compressed_arc_enabled setting.
6092 if (ztest_random(10) == 0)
6093 zfs_compressed_arc_enabled = ztest_random(2);
6096 * Periodically change the zfs_abd_scatter_enabled setting.
6098 if (ztest_random(10) == 0)
6099 zfs_abd_scatter_enabled = ztest_random(2);
6105 ztest_deadman_thread(void *arg)
6107 ztest_shared_t *zs = arg;
6108 spa_t *spa = ztest_spa;
6109 hrtime_t delta, total = 0;
6112 delta = zs->zs_thread_stop - zs->zs_thread_start +
6113 MSEC2NSEC(zfs_deadman_synctime_ms);
6115 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
6118 * If the pool is suspended then fail immediately. Otherwise,
6119 * check to see if the pool is making any progress. If
6120 * vdev_deadman() discovers that there hasn't been any recent
6121 * I/Os then it will end up aborting the tests.
6123 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
6124 fatal(0, "aborting test after %llu seconds because "
6125 "pool has transitioned to a suspended state.",
6126 zfs_deadman_synctime_ms / 1000);
6129 vdev_deadman(spa->spa_root_vdev);
6131 total += zfs_deadman_synctime_ms/1000;
6132 (void) printf("ztest has been running for %lld seconds\n",
6138 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6140 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6141 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6142 hrtime_t functime = gethrtime();
6144 for (int i = 0; i < zi->zi_iters; i++)
6145 zi->zi_func(zd, id);
6147 functime = gethrtime() - functime;
6149 atomic_add_64(&zc->zc_count, 1);
6150 atomic_add_64(&zc->zc_time, functime);
6152 if (ztest_opts.zo_verbose >= 4) {
6154 (void) dladdr((void *)zi->zi_func, &dli);
6155 (void) printf("%6.2f sec in %s\n",
6156 (double)functime / NANOSEC, dli.dli_sname);
6161 ztest_thread(void *arg)
6164 uint64_t id = (uintptr_t)arg;
6165 ztest_shared_t *zs = ztest_shared;
6169 ztest_shared_callstate_t *zc;
6171 while ((now = gethrtime()) < zs->zs_thread_stop) {
6173 * See if it's time to force a crash.
6175 if (now > zs->zs_thread_kill)
6179 * If we're getting ENOSPC with some regularity, stop.
6181 if (zs->zs_enospc_count > 10)
6185 * Pick a random function to execute.
6187 rand = ztest_random(ZTEST_FUNCS);
6188 zi = &ztest_info[rand];
6189 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6190 call_next = zc->zc_next;
6192 if (now >= call_next &&
6193 atomic_cas_64(&zc->zc_next, call_next, call_next +
6194 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6195 ztest_execute(rand, zi, id);
6203 ztest_dataset_name(char *dsname, char *pool, int d)
6205 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6209 ztest_dataset_destroy(int d)
6211 char name[ZFS_MAX_DATASET_NAME_LEN];
6213 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6215 if (ztest_opts.zo_verbose >= 3)
6216 (void) printf("Destroying %s to free up space\n", name);
6219 * Cleanup any non-standard clones and snapshots. In general,
6220 * ztest thread t operates on dataset (t % zopt_datasets),
6221 * so there may be more than one thing to clean up.
6223 for (int t = d; t < ztest_opts.zo_threads;
6224 t += ztest_opts.zo_datasets) {
6225 ztest_dsl_dataset_cleanup(name, t);
6228 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6229 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6233 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6235 uint64_t usedobjs, dirobjs, scratch;
6238 * ZTEST_DIROBJ is the object directory for the entire dataset.
6239 * Therefore, the number of objects in use should equal the
6240 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6241 * If not, we have an object leak.
6243 * Note that we can only check this in ztest_dataset_open(),
6244 * when the open-context and syncing-context values agree.
6245 * That's because zap_count() returns the open-context value,
6246 * while dmu_objset_space() returns the rootbp fill count.
6248 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6249 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6250 ASSERT3U(dirobjs + 1, ==, usedobjs);
6254 ztest_dataset_open(int d)
6256 ztest_ds_t *zd = &ztest_ds[d];
6257 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6260 char name[ZFS_MAX_DATASET_NAME_LEN];
6263 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6265 rw_enter(&ztest_name_lock, RW_READER);
6267 error = ztest_dataset_create(name);
6268 if (error == ENOSPC) {
6269 rw_exit(&ztest_name_lock);
6270 ztest_record_enospc(FTAG);
6273 ASSERT(error == 0 || error == EEXIST);
6275 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6276 rw_exit(&ztest_name_lock);
6278 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6280 zilog = zd->zd_zilog;
6282 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6283 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6284 fatal(0, "missing log records: claimed %llu < committed %llu",
6285 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6287 ztest_dataset_dirobj_verify(zd);
6289 zil_replay(os, zd, ztest_replay_vector);
6291 ztest_dataset_dirobj_verify(zd);
6293 if (ztest_opts.zo_verbose >= 6)
6294 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6296 (u_longlong_t)zilog->zl_parse_blk_count,
6297 (u_longlong_t)zilog->zl_parse_lr_count,
6298 (u_longlong_t)zilog->zl_replaying_seq);
6300 zilog = zil_open(os, ztest_get_data);
6302 if (zilog->zl_replaying_seq != 0 &&
6303 zilog->zl_replaying_seq < committed_seq)
6304 fatal(0, "missing log records: replayed %llu < committed %llu",
6305 zilog->zl_replaying_seq, committed_seq);
6311 ztest_dataset_close(int d)
6313 ztest_ds_t *zd = &ztest_ds[d];
6315 zil_close(zd->zd_zilog);
6316 dmu_objset_disown(zd->zd_os, zd);
6322 * Kick off threads to run tests on all datasets in parallel.
6325 ztest_run(ztest_shared_t *zs)
6330 thread_t resume_tid;
6333 ztest_exiting = B_FALSE;
6336 * Initialize parent/child shared state.
6338 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6339 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6340 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6342 zs->zs_thread_start = gethrtime();
6343 zs->zs_thread_stop =
6344 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6345 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6346 zs->zs_thread_kill = zs->zs_thread_stop;
6347 if (ztest_random(100) < ztest_opts.zo_killrate) {
6348 zs->zs_thread_kill -=
6349 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6352 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6354 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6355 offsetof(ztest_cb_data_t, zcd_node));
6360 kernel_init(FREAD | FWRITE);
6361 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6362 metaslab_preload_limit = ztest_random(20) + 1;
6365 dmu_objset_stats_t dds;
6366 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6367 DMU_OST_ANY, B_TRUE, FTAG, &os));
6368 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6369 dmu_objset_fast_stat(os, &dds);
6370 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6371 zs->zs_guid = dds.dds_guid;
6372 dmu_objset_disown(os, FTAG);
6374 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6377 * We don't expect the pool to suspend unless maxfaults == 0,
6378 * in which case ztest_fault_inject() temporarily takes away
6379 * the only valid replica.
6381 if (MAXFAULTS() == 0)
6382 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6384 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6387 * Create a thread to periodically resume suspended I/O.
6389 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6393 * Create a deadman thread to abort() if we hang.
6395 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6399 * Verify that we can safely inquire about any object,
6400 * whether it's allocated or not. To make it interesting,
6401 * we probe a 5-wide window around each power of two.
6402 * This hits all edge cases, including zero and the max.
6404 for (int t = 0; t < 64; t++) {
6405 for (int d = -5; d <= 5; d++) {
6406 error = dmu_object_info(spa->spa_meta_objset,
6407 (1ULL << t) + d, NULL);
6408 ASSERT(error == 0 || error == ENOENT ||
6414 * If we got any ENOSPC errors on the previous run, destroy something.
6416 if (zs->zs_enospc_count != 0) {
6417 int d = ztest_random(ztest_opts.zo_datasets);
6418 ztest_dataset_destroy(d);
6420 zs->zs_enospc_count = 0;
6422 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6425 if (ztest_opts.zo_verbose >= 4)
6426 (void) printf("starting main threads...\n");
6429 * Kick off all the tests that run in parallel.
6431 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6432 if (t < ztest_opts.zo_datasets &&
6433 ztest_dataset_open(t) != 0)
6435 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6436 THR_BOUND, &tid[t]) == 0);
6440 * Wait for all of the tests to complete. We go in reverse order
6441 * so we don't close datasets while threads are still using them.
6443 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6444 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6445 if (t < ztest_opts.zo_datasets)
6446 ztest_dataset_close(t);
6449 txg_wait_synced(spa_get_dsl(spa), 0);
6451 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6452 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6453 zfs_dbgmsg_print(FTAG);
6455 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6457 /* Kill the resume thread */
6458 ztest_exiting = B_TRUE;
6459 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6463 * Right before closing the pool, kick off a bunch of async I/O;
6464 * spa_close() should wait for it to complete.
6466 for (uint64_t object = 1; object < 50; object++) {
6467 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6468 ZIO_PRIORITY_SYNC_READ);
6471 spa_close(spa, FTAG);
6474 * Verify that we can loop over all pools.
6476 mutex_enter(&spa_namespace_lock);
6477 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6478 if (ztest_opts.zo_verbose > 3)
6479 (void) printf("spa_next: found %s\n", spa_name(spa));
6480 mutex_exit(&spa_namespace_lock);
6483 * Verify that we can export the pool and reimport it under a
6486 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6487 char name[ZFS_MAX_DATASET_NAME_LEN];
6488 (void) snprintf(name, sizeof (name), "%s_import",
6489 ztest_opts.zo_pool);
6490 ztest_spa_import_export(ztest_opts.zo_pool, name);
6491 ztest_spa_import_export(name, ztest_opts.zo_pool);
6496 list_destroy(&zcl.zcl_callbacks);
6498 mutex_destroy(&zcl.zcl_callbacks_lock);
6500 rw_destroy(&ztest_name_lock);
6501 mutex_destroy(&ztest_vdev_lock);
6502 mutex_destroy(&ztest_checkpoint_lock);
6508 ztest_ds_t *zd = &ztest_ds[0];
6512 if (ztest_opts.zo_verbose >= 3)
6513 (void) printf("testing spa_freeze()...\n");
6515 kernel_init(FREAD | FWRITE);
6516 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6517 VERIFY3U(0, ==, ztest_dataset_open(0));
6521 * Force the first log block to be transactionally allocated.
6522 * We have to do this before we freeze the pool -- otherwise
6523 * the log chain won't be anchored.
6525 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6526 ztest_dmu_object_alloc_free(zd, 0);
6527 zil_commit(zd->zd_zilog, 0);
6530 txg_wait_synced(spa_get_dsl(spa), 0);
6533 * Freeze the pool. This stops spa_sync() from doing anything,
6534 * so that the only way to record changes from now on is the ZIL.
6539 * Because it is hard to predict how much space a write will actually
6540 * require beforehand, we leave ourselves some fudge space to write over
6543 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6546 * Run tests that generate log records but don't alter the pool config
6547 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6548 * We do a txg_wait_synced() after each iteration to force the txg
6549 * to increase well beyond the last synced value in the uberblock.
6550 * The ZIL should be OK with that.
6552 * Run a random number of times less than zo_maxloops and ensure we do
6553 * not run out of space on the pool.
6555 while (ztest_random(10) != 0 &&
6556 numloops++ < ztest_opts.zo_maxloops &&
6557 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6559 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6560 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6561 ztest_io(zd, od.od_object,
6562 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6563 txg_wait_synced(spa_get_dsl(spa), 0);
6567 * Commit all of the changes we just generated.
6569 zil_commit(zd->zd_zilog, 0);
6570 txg_wait_synced(spa_get_dsl(spa), 0);
6573 * Close our dataset and close the pool.
6575 ztest_dataset_close(0);
6576 spa_close(spa, FTAG);
6580 * Open and close the pool and dataset to induce log replay.
6582 kernel_init(FREAD | FWRITE);
6583 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6584 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6585 VERIFY3U(0, ==, ztest_dataset_open(0));
6586 ztest_dataset_close(0);
6589 txg_wait_synced(spa_get_dsl(spa), 0);
6590 ztest_reguid(NULL, 0);
6592 spa_close(spa, FTAG);
6597 print_time(hrtime_t t, char *timebuf)
6599 hrtime_t s = t / NANOSEC;
6600 hrtime_t m = s / 60;
6601 hrtime_t h = m / 60;
6602 hrtime_t d = h / 24;
6611 (void) sprintf(timebuf,
6612 "%llud%02lluh%02llum%02llus", d, h, m, s);
6614 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6616 (void) sprintf(timebuf, "%llum%02llus", m, s);
6618 (void) sprintf(timebuf, "%llus", s);
6626 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6628 if (ztest_random(2) == 0)
6630 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6636 * Import a storage pool with the given name.
6639 ztest_import(ztest_shared_t *zs)
6641 libzfs_handle_t *hdl;
6642 importargs_t args = { 0 };
6644 nvlist_t *cfg = NULL;
6646 char *searchdirs[nsearch];
6647 char *name = ztest_opts.zo_pool;
6648 int flags = ZFS_IMPORT_MISSING_LOG;
6651 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6652 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6654 kernel_init(FREAD | FWRITE);
6655 hdl = libzfs_init();
6657 searchdirs[0] = ztest_opts.zo_dir;
6658 args.paths = nsearch;
6659 args.path = searchdirs;
6660 args.can_be_active = B_FALSE;
6662 error = zpool_tryimport(hdl, name, &cfg, &args);
6664 (void) fatal(0, "No pools found\n");
6666 VERIFY0(spa_import(name, cfg, NULL, flags));
6667 VERIFY0(spa_open(name, &spa, FTAG));
6668 zs->zs_metaslab_sz =
6669 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6670 spa_close(spa, FTAG);
6675 if (!ztest_opts.zo_mmp_test) {
6676 ztest_run_zdb(ztest_opts.zo_pool);
6678 ztest_run_zdb(ztest_opts.zo_pool);
6681 rw_destroy(&ztest_name_lock);
6682 mutex_destroy(&ztest_vdev_lock);
6686 * Create a storage pool with the given name and initial vdev size.
6687 * Then test spa_freeze() functionality.
6690 ztest_init(ztest_shared_t *zs)
6693 nvlist_t *nvroot, *props;
6695 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6696 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6697 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6699 kernel_init(FREAD | FWRITE);
6702 * Create the storage pool.
6704 (void) spa_destroy(ztest_opts.zo_pool);
6705 ztest_shared->zs_vdev_next_leaf = 0;
6707 zs->zs_mirrors = ztest_opts.zo_mirrors;
6708 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6709 NULL, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6710 props = make_random_props();
6711 for (int i = 0; i < SPA_FEATURES; i++) {
6713 (void) snprintf(buf, sizeof (buf), "feature@%s",
6714 spa_feature_table[i].fi_uname);
6715 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6717 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6718 nvlist_free(nvroot);
6721 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6722 zs->zs_metaslab_sz =
6723 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6725 spa_close(spa, FTAG);
6729 if (!ztest_opts.zo_mmp_test) {
6730 ztest_run_zdb(ztest_opts.zo_pool);
6732 ztest_run_zdb(ztest_opts.zo_pool);
6735 rw_destroy(&ztest_name_lock);
6736 mutex_destroy(&ztest_vdev_lock);
6737 mutex_destroy(&ztest_checkpoint_lock);
6743 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6745 ztest_fd_data = mkstemp(ztest_name_data);
6746 ASSERT3S(ztest_fd_data, >=, 0);
6747 (void) unlink(ztest_name_data);
6752 shared_data_size(ztest_shared_hdr_t *hdr)
6756 size = hdr->zh_hdr_size;
6757 size += hdr->zh_opts_size;
6758 size += hdr->zh_size;
6759 size += hdr->zh_stats_size * hdr->zh_stats_count;
6760 size += hdr->zh_ds_size * hdr->zh_ds_count;
6769 ztest_shared_hdr_t *hdr;
6771 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6772 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6773 ASSERT(hdr != MAP_FAILED);
6775 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6777 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6778 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6779 hdr->zh_size = sizeof (ztest_shared_t);
6780 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6781 hdr->zh_stats_count = ZTEST_FUNCS;
6782 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6783 hdr->zh_ds_count = ztest_opts.zo_datasets;
6785 size = shared_data_size(hdr);
6786 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6788 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6795 ztest_shared_hdr_t *hdr;
6798 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6799 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6800 ASSERT(hdr != MAP_FAILED);
6802 size = shared_data_size(hdr);
6804 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6805 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6806 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6807 ASSERT(hdr != MAP_FAILED);
6808 buf = (uint8_t *)hdr;
6810 offset = hdr->zh_hdr_size;
6811 ztest_shared_opts = (void *)&buf[offset];
6812 offset += hdr->zh_opts_size;
6813 ztest_shared = (void *)&buf[offset];
6814 offset += hdr->zh_size;
6815 ztest_shared_callstate = (void *)&buf[offset];
6816 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6817 ztest_shared_ds = (void *)&buf[offset];
6821 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6825 char *cmdbuf = NULL;
6830 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6831 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6836 fatal(1, "fork failed");
6838 if (pid == 0) { /* child */
6839 char *emptyargv[2] = { cmd, NULL };
6840 char fd_data_str[12];
6842 struct rlimit rl = { 1024, 1024 };
6843 (void) setrlimit(RLIMIT_NOFILE, &rl);
6845 (void) close(ztest_fd_rand);
6847 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6848 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6850 (void) enable_extended_FILE_stdio(-1, -1);
6851 if (libpath != NULL)
6852 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6854 (void) execv(cmd, emptyargv);
6856 (void) execvp(cmd, emptyargv);
6858 ztest_dump_core = B_FALSE;
6859 fatal(B_TRUE, "exec failed: %s", cmd);
6862 if (cmdbuf != NULL) {
6863 umem_free(cmdbuf, MAXPATHLEN);
6867 while (waitpid(pid, &status, 0) != pid)
6869 if (statusp != NULL)
6872 if (WIFEXITED(status)) {
6873 if (WEXITSTATUS(status) != 0) {
6874 (void) fprintf(stderr, "child exited with code %d\n",
6875 WEXITSTATUS(status));
6879 } else if (WIFSIGNALED(status)) {
6880 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6881 (void) fprintf(stderr, "child died with signal %d\n",
6887 (void) fprintf(stderr, "something strange happened to child\n");
6894 ztest_run_init(void)
6896 ztest_shared_t *zs = ztest_shared;
6899 * Blow away any existing copy of zpool.cache
6901 (void) remove(spa_config_path);
6903 if (ztest_opts.zo_init == 0) {
6904 if (ztest_opts.zo_verbose >= 1)
6905 (void) printf("Importing pool %s\n",
6906 ztest_opts.zo_pool);
6912 * Create and initialize our storage pool.
6914 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6915 bzero(zs, sizeof (ztest_shared_t));
6916 if (ztest_opts.zo_verbose >= 3 &&
6917 ztest_opts.zo_init != 1) {
6918 (void) printf("ztest_init(), pass %d\n", i);
6925 main(int argc, char **argv)
6933 ztest_shared_callstate_t *zc;
6935 char numbuf[NN_NUMBUF_SZ];
6938 char *fd_data_str = getenv("ZTEST_FD_DATA");
6940 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6942 dprintf_setup(&argc, argv);
6943 zfs_deadman_synctime_ms = 300000;
6945 * As two-word space map entries may not come up often (especially
6946 * if pool and vdev sizes are small) we want to force at least some
6947 * of them so the feature get tested.
6949 zfs_force_some_double_word_sm_entries = B_TRUE;
6952 * Verify that even extensively damaged split blocks with many
6953 * segments can be reconstructed in a reasonable amount of time
6954 * when reconstruction is known to be possible.
6956 zfs_reconstruct_indirect_damage_fraction = 4;
6958 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6959 ASSERT3S(ztest_fd_rand, >=, 0);
6962 process_options(argc, argv);
6967 bcopy(&ztest_opts, ztest_shared_opts,
6968 sizeof (*ztest_shared_opts));
6970 ztest_fd_data = atoi(fd_data_str);
6972 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6974 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6976 /* Override location of zpool.cache */
6977 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6978 ztest_opts.zo_dir), !=, -1);
6980 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6985 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6986 metaslab_df_alloc_threshold =
6987 zs->zs_metaslab_df_alloc_threshold;
6996 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6998 if (ztest_opts.zo_verbose >= 1) {
6999 (void) printf("%llu vdevs, %d datasets, %d threads,"
7000 " %llu seconds...\n",
7001 (u_longlong_t)ztest_opts.zo_vdevs,
7002 ztest_opts.zo_datasets,
7003 ztest_opts.zo_threads,
7004 (u_longlong_t)ztest_opts.zo_time);
7007 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
7008 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
7010 zs->zs_do_init = B_TRUE;
7011 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
7012 if (ztest_opts.zo_verbose >= 1) {
7013 (void) printf("Executing older ztest for "
7014 "initialization: %s\n", ztest_opts.zo_alt_ztest);
7016 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
7017 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
7019 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
7021 zs->zs_do_init = B_FALSE;
7023 zs->zs_proc_start = gethrtime();
7024 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
7026 for (int f = 0; f < ZTEST_FUNCS; f++) {
7027 zi = &ztest_info[f];
7028 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7029 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
7030 zc->zc_next = UINT64_MAX;
7032 zc->zc_next = zs->zs_proc_start +
7033 ztest_random(2 * zi->zi_interval[0] + 1);
7037 * Run the tests in a loop. These tests include fault injection
7038 * to verify that self-healing data works, and forced crashes
7039 * to verify that we never lose on-disk consistency.
7041 while (gethrtime() < zs->zs_proc_stop) {
7046 * Initialize the workload counters for each function.
7048 for (int f = 0; f < ZTEST_FUNCS; f++) {
7049 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7054 /* Set the allocation switch size */
7055 zs->zs_metaslab_df_alloc_threshold =
7056 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7058 if (!hasalt || ztest_random(2) == 0) {
7059 if (hasalt && ztest_opts.zo_verbose >= 1) {
7060 (void) printf("Executing newer ztest: %s\n",
7064 killed = exec_child(cmd, NULL, B_TRUE, &status);
7066 if (hasalt && ztest_opts.zo_verbose >= 1) {
7067 (void) printf("Executing older ztest: %s\n",
7068 ztest_opts.zo_alt_ztest);
7071 killed = exec_child(ztest_opts.zo_alt_ztest,
7072 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7079 if (ztest_opts.zo_verbose >= 1) {
7080 hrtime_t now = gethrtime();
7082 now = MIN(now, zs->zs_proc_stop);
7083 print_time(zs->zs_proc_stop - now, timebuf);
7084 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
7086 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7087 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7089 WIFEXITED(status) ? "Complete" : "SIGKILL",
7090 (u_longlong_t)zs->zs_enospc_count,
7091 100.0 * zs->zs_alloc / zs->zs_space,
7093 100.0 * (now - zs->zs_proc_start) /
7094 (ztest_opts.zo_time * NANOSEC), timebuf);
7097 if (ztest_opts.zo_verbose >= 2) {
7098 (void) printf("\nWorkload summary:\n\n");
7099 (void) printf("%7s %9s %s\n",
7100 "Calls", "Time", "Function");
7101 (void) printf("%7s %9s %s\n",
7102 "-----", "----", "--------");
7103 for (int f = 0; f < ZTEST_FUNCS; f++) {
7106 zi = &ztest_info[f];
7107 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7108 print_time(zc->zc_time, timebuf);
7109 (void) dladdr((void *)zi->zi_func, &dli);
7110 (void) printf("%7llu %9s %s\n",
7111 (u_longlong_t)zc->zc_count, timebuf,
7114 (void) printf("\n");
7117 if (!ztest_opts.zo_mmp_test)
7118 ztest_run_zdb(ztest_opts.zo_pool);
7121 if (ztest_opts.zo_verbose >= 1) {
7123 (void) printf("%d runs of older ztest: %s\n", older,
7124 ztest_opts.zo_alt_ztest);
7125 (void) printf("%d runs of newer ztest: %s\n", newer,
7128 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7129 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7132 umem_free(cmd, MAXNAMELEN);