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, 2017 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 2017 RackTop Systems.
33 * The objective of this program is to provide a DMU/ZAP/SPA stress test
34 * that runs entirely in userland, is easy to use, and easy to extend.
36 * The overall design of the ztest program is as follows:
38 * (1) For each major functional area (e.g. adding vdevs to a pool,
39 * creating and destroying datasets, reading and writing objects, etc)
40 * we have a simple routine to test that functionality. These
41 * individual routines do not have to do anything "stressful".
43 * (2) We turn these simple functionality tests into a stress test by
44 * running them all in parallel, with as many threads as desired,
45 * and spread across as many datasets, objects, and vdevs as desired.
47 * (3) While all this is happening, we inject faults into the pool to
48 * verify that self-healing data really works.
50 * (4) Every time we open a dataset, we change its checksum and compression
51 * functions. Thus even individual objects vary from block to block
52 * in which checksum they use and whether they're compressed.
54 * (5) To verify that we never lose on-disk consistency after a crash,
55 * we run the entire test in a child of the main process.
56 * At random times, the child self-immolates with a SIGKILL.
57 * This is the software equivalent of pulling the power cord.
58 * The parent then runs the test again, using the existing
59 * storage pool, as many times as desired. If backwards compatibility
60 * testing is enabled ztest will sometimes run the "older" version
61 * of ztest after a SIGKILL.
63 * (6) To verify that we don't have future leaks or temporal incursions,
64 * many of the functional tests record the transaction group number
65 * as part of their data. When reading old data, they verify that
66 * the transaction group number is less than the current, open txg.
67 * If you add a new test, please do this if applicable.
69 * When run with no arguments, ztest runs for about five minutes and
70 * produces no output if successful. To get a little bit of information,
71 * specify -V. To get more information, specify -VV, and so on.
73 * To turn this into an overnight stress test, use -T to specify run time.
75 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
76 * to increase the pool capacity, fanout, and overall stress level.
78 * Use the -k option to set the desired frequency of kills.
80 * When ztest invokes itself it passes all relevant information through a
81 * temporary file which is mmap-ed in the child process. This allows shared
82 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
83 * stored at offset 0 of this file and contains information on the size and
84 * number of shared structures in the file. The information stored in this file
85 * must remain backwards compatible with older versions of ztest so that
86 * ztest can invoke them during backwards compatibility testing (-B).
89 #include <sys/zfs_context.h>
95 #include <sys/dmu_objset.h>
100 #include <sys/mman.h>
101 #include <sys/resource.h>
104 #include <sys/zil_impl.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/vdev_initialize.h>
108 #include <sys/spa_impl.h>
109 #include <sys/metaslab_impl.h>
110 #include <sys/dsl_prop.h>
111 #include <sys/dsl_dataset.h>
112 #include <sys/dsl_destroy.h>
113 #include <sys/dsl_scan.h>
114 #include <sys/zio_checksum.h>
115 #include <sys/refcount.h>
116 #include <sys/zfeature.h>
117 #include <sys/dsl_userhold.h>
120 #include <stdio_ext.h>
129 #include <sys/fs/zfs.h>
130 #include <libnvpair.h>
131 #include <libcmdutils.h>
133 static int ztest_fd_data = -1;
134 static int ztest_fd_rand = -1;
136 typedef struct ztest_shared_hdr {
137 uint64_t zh_hdr_size;
138 uint64_t zh_opts_size;
140 uint64_t zh_stats_size;
141 uint64_t zh_stats_count;
143 uint64_t zh_ds_count;
144 } ztest_shared_hdr_t;
146 static ztest_shared_hdr_t *ztest_shared_hdr;
148 typedef struct ztest_shared_opts {
149 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
150 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
151 char zo_alt_ztest[MAXNAMELEN];
152 char zo_alt_libpath[MAXNAMELEN];
154 uint64_t zo_vdevtime;
162 uint64_t zo_passtime;
163 uint64_t zo_killrate;
167 uint64_t zo_maxloops;
168 uint64_t zo_metaslab_force_ganging;
169 } ztest_shared_opts_t;
171 static const ztest_shared_opts_t ztest_opts_defaults = {
172 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
173 .zo_dir = { '/', 't', 'm', 'p', '\0' },
174 .zo_alt_ztest = { '\0' },
175 .zo_alt_libpath = { '\0' },
177 .zo_ashift = SPA_MINBLOCKSHIFT,
180 .zo_raidz_parity = 1,
181 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
184 .zo_passtime = 60, /* 60 seconds */
185 .zo_killrate = 70, /* 70% kill rate */
188 .zo_time = 300, /* 5 minutes */
189 .zo_maxloops = 50, /* max loops during spa_freeze() */
190 .zo_metaslab_force_ganging = 32 << 10
193 extern uint64_t metaslab_force_ganging;
194 extern uint64_t metaslab_df_alloc_threshold;
195 extern uint64_t zfs_deadman_synctime_ms;
196 extern int metaslab_preload_limit;
197 extern boolean_t zfs_compressed_arc_enabled;
198 extern boolean_t zfs_abd_scatter_enabled;
199 extern boolean_t zfs_force_some_double_word_sm_entries;
201 static ztest_shared_opts_t *ztest_shared_opts;
202 static ztest_shared_opts_t ztest_opts;
204 typedef struct ztest_shared_ds {
208 static ztest_shared_ds_t *ztest_shared_ds;
209 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
211 #define BT_MAGIC 0x123456789abcdefULL
212 #define MAXFAULTS() \
213 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
217 ZTEST_IO_WRITE_PATTERN,
218 ZTEST_IO_WRITE_ZEROES,
225 typedef struct ztest_block_tag {
235 typedef struct bufwad {
242 * XXX -- fix zfs range locks to be generic so we can use them here.
264 #define ZTEST_RANGE_LOCKS 64
265 #define ZTEST_OBJECT_LOCKS 64
268 * Object descriptor. Used as a template for object lookup/create/remove.
270 typedef struct ztest_od {
273 dmu_object_type_t od_type;
274 dmu_object_type_t od_crtype;
275 uint64_t od_blocksize;
276 uint64_t od_crblocksize;
279 char od_name[ZFS_MAX_DATASET_NAME_LEN];
285 typedef struct ztest_ds {
286 ztest_shared_ds_t *zd_shared;
288 krwlock_t zd_zilog_lock;
290 ztest_od_t *zd_od; /* debugging aid */
291 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
292 kmutex_t zd_dirobj_lock;
293 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
294 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
298 * Per-iteration state.
300 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
302 typedef struct ztest_info {
303 ztest_func_t *zi_func; /* test function */
304 uint64_t zi_iters; /* iterations per execution */
305 uint64_t *zi_interval; /* execute every <interval> seconds */
308 typedef struct ztest_shared_callstate {
309 uint64_t zc_count; /* per-pass count */
310 uint64_t zc_time; /* per-pass time */
311 uint64_t zc_next; /* next time to call this function */
312 } ztest_shared_callstate_t;
314 static ztest_shared_callstate_t *ztest_shared_callstate;
315 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
318 * Note: these aren't static because we want dladdr() to work.
320 ztest_func_t ztest_dmu_read_write;
321 ztest_func_t ztest_dmu_write_parallel;
322 ztest_func_t ztest_dmu_object_alloc_free;
323 ztest_func_t ztest_dmu_commit_callbacks;
324 ztest_func_t ztest_zap;
325 ztest_func_t ztest_zap_parallel;
326 ztest_func_t ztest_zil_commit;
327 ztest_func_t ztest_zil_remount;
328 ztest_func_t ztest_dmu_read_write_zcopy;
329 ztest_func_t ztest_dmu_objset_create_destroy;
330 ztest_func_t ztest_dmu_prealloc;
331 ztest_func_t ztest_fzap;
332 ztest_func_t ztest_dmu_snapshot_create_destroy;
333 ztest_func_t ztest_dsl_prop_get_set;
334 ztest_func_t ztest_spa_prop_get_set;
335 ztest_func_t ztest_spa_create_destroy;
336 ztest_func_t ztest_fault_inject;
337 ztest_func_t ztest_ddt_repair;
338 ztest_func_t ztest_dmu_snapshot_hold;
339 ztest_func_t ztest_spa_rename;
340 ztest_func_t ztest_scrub;
341 ztest_func_t ztest_dsl_dataset_promote_busy;
342 ztest_func_t ztest_vdev_attach_detach;
343 ztest_func_t ztest_vdev_LUN_growth;
344 ztest_func_t ztest_vdev_add_remove;
345 ztest_func_t ztest_vdev_aux_add_remove;
346 ztest_func_t ztest_split_pool;
347 ztest_func_t ztest_reguid;
348 ztest_func_t ztest_spa_upgrade;
349 ztest_func_t ztest_device_removal;
350 ztest_func_t ztest_remap_blocks;
351 ztest_func_t ztest_spa_checkpoint_create_discard;
352 ztest_func_t ztest_initialize;
354 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
355 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
356 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
357 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
358 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
360 ztest_info_t ztest_info[] = {
361 { ztest_dmu_read_write, 1, &zopt_always },
362 { ztest_dmu_write_parallel, 10, &zopt_always },
363 { ztest_dmu_object_alloc_free, 1, &zopt_always },
364 { ztest_dmu_commit_callbacks, 1, &zopt_always },
365 { ztest_zap, 30, &zopt_always },
366 { ztest_zap_parallel, 100, &zopt_always },
367 { ztest_split_pool, 1, &zopt_always },
368 { ztest_zil_commit, 1, &zopt_incessant },
369 { ztest_zil_remount, 1, &zopt_sometimes },
370 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
371 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
372 { ztest_dsl_prop_get_set, 1, &zopt_often },
373 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
375 { ztest_dmu_prealloc, 1, &zopt_sometimes },
377 { ztest_fzap, 1, &zopt_sometimes },
378 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
379 { ztest_spa_create_destroy, 1, &zopt_sometimes },
380 { ztest_fault_inject, 1, &zopt_incessant },
381 { ztest_ddt_repair, 1, &zopt_sometimes },
382 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
383 { ztest_reguid, 1, &zopt_rarely },
384 { ztest_spa_rename, 1, &zopt_rarely },
385 { ztest_scrub, 1, &zopt_often },
386 { ztest_spa_upgrade, 1, &zopt_rarely },
387 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
388 { ztest_vdev_attach_detach, 1, &zopt_incessant },
389 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
390 { ztest_vdev_add_remove, 1,
391 &ztest_opts.zo_vdevtime },
392 { ztest_vdev_aux_add_remove, 1,
393 &ztest_opts.zo_vdevtime },
394 { ztest_device_removal, 1, &zopt_sometimes },
395 { ztest_remap_blocks, 1, &zopt_sometimes },
396 { ztest_spa_checkpoint_create_discard, 1, &zopt_rarely },
397 { ztest_initialize, 1, &zopt_sometimes }
400 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
403 * The following struct is used to hold a list of uncalled commit callbacks.
404 * The callbacks are ordered by txg number.
406 typedef struct ztest_cb_list {
407 kmutex_t zcl_callbacks_lock;
408 list_t zcl_callbacks;
412 * Stuff we need to share writably between parent and child.
414 typedef struct ztest_shared {
415 boolean_t zs_do_init;
416 hrtime_t zs_proc_start;
417 hrtime_t zs_proc_stop;
418 hrtime_t zs_thread_start;
419 hrtime_t zs_thread_stop;
420 hrtime_t zs_thread_kill;
421 uint64_t zs_enospc_count;
422 uint64_t zs_vdev_next_leaf;
423 uint64_t zs_vdev_aux;
428 uint64_t zs_metaslab_sz;
429 uint64_t zs_metaslab_df_alloc_threshold;
433 #define ID_PARALLEL -1ULL
435 static char ztest_dev_template[] = "%s/%s.%llua";
436 static char ztest_aux_template[] = "%s/%s.%s.%llu";
437 ztest_shared_t *ztest_shared;
439 static spa_t *ztest_spa = NULL;
440 static ztest_ds_t *ztest_ds;
442 static kmutex_t ztest_vdev_lock;
443 static kmutex_t ztest_checkpoint_lock;
444 static boolean_t ztest_device_removal_active = B_FALSE;
447 * The ztest_name_lock protects the pool and dataset namespace used by
448 * the individual tests. To modify the namespace, consumers must grab
449 * this lock as writer. Grabbing the lock as reader will ensure that the
450 * namespace does not change while the lock is held.
452 static krwlock_t ztest_name_lock;
454 static boolean_t ztest_dump_core = B_TRUE;
455 static boolean_t ztest_exiting;
457 /* Global commit callback list */
458 static ztest_cb_list_t zcl;
461 ZTEST_META_DNODE = 0,
466 static void usage(boolean_t) __NORETURN;
469 * These libumem hooks provide a reasonable set of defaults for the allocator's
470 * debugging facilities.
475 return ("default,verbose"); /* $UMEM_DEBUG setting */
479 _umem_logging_init(void)
481 return ("fail,contents"); /* $UMEM_LOGGING setting */
484 #define FATAL_MSG_SZ 1024
489 fatal(int do_perror, char *message, ...)
492 int save_errno = errno;
493 char buf[FATAL_MSG_SZ];
495 (void) fflush(stdout);
497 va_start(args, message);
498 (void) sprintf(buf, "ztest: ");
500 (void) vsprintf(buf + strlen(buf), message, args);
503 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
504 ": %s", strerror(save_errno));
506 (void) fprintf(stderr, "%s\n", buf);
507 fatal_msg = buf; /* to ease debugging */
514 str2shift(const char *buf)
516 const char *ends = "BKMGTPEZ";
521 for (i = 0; i < strlen(ends); i++) {
522 if (toupper(buf[0]) == ends[i])
525 if (i == strlen(ends)) {
526 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
530 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
533 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
539 nicenumtoull(const char *buf)
544 val = strtoull(buf, &end, 0);
546 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
548 } else if (end[0] == '.') {
549 double fval = strtod(buf, &end);
550 fval *= pow(2, str2shift(end));
551 if (fval > UINT64_MAX) {
552 (void) fprintf(stderr, "ztest: value too large: %s\n",
556 val = (uint64_t)fval;
558 int shift = str2shift(end);
559 if (shift >= 64 || (val << shift) >> shift != val) {
560 (void) fprintf(stderr, "ztest: value too large: %s\n",
570 usage(boolean_t requested)
572 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
574 char nice_vdev_size[NN_NUMBUF_SZ];
575 char nice_force_ganging[NN_NUMBUF_SZ];
576 FILE *fp = requested ? stdout : stderr;
578 nicenum(zo->zo_vdev_size, nice_vdev_size, sizeof (nice_vdev_size));
579 nicenum(zo->zo_metaslab_force_ganging, nice_force_ganging,
580 sizeof (nice_force_ganging));
582 (void) fprintf(fp, "Usage: %s\n"
583 "\t[-v vdevs (default: %llu)]\n"
584 "\t[-s size_of_each_vdev (default: %s)]\n"
585 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
586 "\t[-m mirror_copies (default: %d)]\n"
587 "\t[-r raidz_disks (default: %d)]\n"
588 "\t[-R raidz_parity (default: %d)]\n"
589 "\t[-d datasets (default: %d)]\n"
590 "\t[-t threads (default: %d)]\n"
591 "\t[-g gang_block_threshold (default: %s)]\n"
592 "\t[-i init_count (default: %d)] initialize pool i times\n"
593 "\t[-k kill_percentage (default: %llu%%)]\n"
594 "\t[-p pool_name (default: %s)]\n"
595 "\t[-f dir (default: %s)] file directory for vdev files\n"
596 "\t[-V] verbose (use multiple times for ever more blather)\n"
597 "\t[-E] use existing pool instead of creating new one\n"
598 "\t[-T time (default: %llu sec)] total run time\n"
599 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
600 "\t[-P passtime (default: %llu sec)] time per pass\n"
601 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
602 "\t[-o variable=value] ... set global variable to an unsigned\n"
603 "\t 32-bit integer value\n"
604 "\t[-h] (print help)\n"
607 (u_longlong_t)zo->zo_vdevs, /* -v */
608 nice_vdev_size, /* -s */
609 zo->zo_ashift, /* -a */
610 zo->zo_mirrors, /* -m */
611 zo->zo_raidz, /* -r */
612 zo->zo_raidz_parity, /* -R */
613 zo->zo_datasets, /* -d */
614 zo->zo_threads, /* -t */
615 nice_force_ganging, /* -g */
616 zo->zo_init, /* -i */
617 (u_longlong_t)zo->zo_killrate, /* -k */
618 zo->zo_pool, /* -p */
620 (u_longlong_t)zo->zo_time, /* -T */
621 (u_longlong_t)zo->zo_maxloops, /* -F */
622 (u_longlong_t)zo->zo_passtime);
623 exit(requested ? 0 : 1);
627 process_options(int argc, char **argv)
630 ztest_shared_opts_t *zo = &ztest_opts;
634 char altdir[MAXNAMELEN] = { 0 };
636 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
638 while ((opt = getopt(argc, argv,
639 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:o:")) != EOF) {
656 value = nicenumtoull(optarg);
660 zo->zo_vdevs = value;
663 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
666 zo->zo_ashift = value;
669 zo->zo_mirrors = value;
672 zo->zo_raidz = MAX(1, value);
675 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
678 zo->zo_datasets = MAX(1, value);
681 zo->zo_threads = MAX(1, value);
684 zo->zo_metaslab_force_ganging =
685 MAX(SPA_MINBLOCKSIZE << 1, value);
691 zo->zo_killrate = value;
694 (void) strlcpy(zo->zo_pool, optarg,
695 sizeof (zo->zo_pool));
698 path = realpath(optarg, NULL);
700 (void) fprintf(stderr, "error: %s: %s\n",
701 optarg, strerror(errno));
704 (void) strlcpy(zo->zo_dir, path,
705 sizeof (zo->zo_dir));
718 zo->zo_passtime = MAX(1, value);
721 zo->zo_maxloops = MAX(1, value);
724 (void) strlcpy(altdir, optarg, sizeof (altdir));
727 if (set_global_var(optarg) != 0)
740 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
743 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
746 if (strlen(altdir) > 0) {
754 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
755 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
757 VERIFY(NULL != realpath(getexecname(), cmd));
758 if (0 != access(altdir, F_OK)) {
759 ztest_dump_core = B_FALSE;
760 fatal(B_TRUE, "invalid alternate ztest path: %s",
763 VERIFY(NULL != realpath(altdir, realaltdir));
766 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
767 * We want to extract <isa> to determine if we should use
768 * 32 or 64 bit binaries.
770 bin = strstr(cmd, "/usr/bin/");
771 ztest = strstr(bin, "/ztest");
773 isalen = ztest - isa;
774 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
775 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
776 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
777 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
779 if (0 != access(zo->zo_alt_ztest, X_OK)) {
780 ztest_dump_core = B_FALSE;
781 fatal(B_TRUE, "invalid alternate ztest: %s",
783 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
784 ztest_dump_core = B_FALSE;
785 fatal(B_TRUE, "invalid alternate lib directory %s",
789 umem_free(cmd, MAXPATHLEN);
790 umem_free(realaltdir, MAXPATHLEN);
795 ztest_kill(ztest_shared_t *zs)
797 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
798 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
801 * Before we kill off ztest, make sure that the config is updated.
802 * See comment above spa_write_cachefile().
804 mutex_enter(&spa_namespace_lock);
805 spa_write_cachefile(ztest_spa, B_FALSE, B_FALSE);
806 mutex_exit(&spa_namespace_lock);
808 zfs_dbgmsg_print(FTAG);
809 (void) kill(getpid(), SIGKILL);
813 ztest_random(uint64_t range)
817 ASSERT3S(ztest_fd_rand, >=, 0);
822 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
823 fatal(1, "short read from /dev/urandom");
830 ztest_record_enospc(const char *s)
832 ztest_shared->zs_enospc_count++;
836 ztest_get_ashift(void)
838 if (ztest_opts.zo_ashift == 0)
839 return (SPA_MINBLOCKSHIFT + ztest_random(5));
840 return (ztest_opts.zo_ashift);
844 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
846 char pathbuf[MAXPATHLEN];
851 ashift = ztest_get_ashift();
857 vdev = ztest_shared->zs_vdev_aux;
858 (void) snprintf(path, sizeof (pathbuf),
859 ztest_aux_template, ztest_opts.zo_dir,
860 pool == NULL ? ztest_opts.zo_pool : pool,
863 vdev = ztest_shared->zs_vdev_next_leaf++;
864 (void) snprintf(path, sizeof (pathbuf),
865 ztest_dev_template, ztest_opts.zo_dir,
866 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
871 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
873 fatal(1, "can't open %s", path);
874 if (ftruncate(fd, size) != 0)
875 fatal(1, "can't ftruncate %s", path);
879 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
880 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
881 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
882 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
888 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
889 uint64_t ashift, int r)
891 nvlist_t *raidz, **child;
895 return (make_vdev_file(path, aux, pool, size, ashift));
896 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
898 for (c = 0; c < r; c++)
899 child[c] = make_vdev_file(path, aux, pool, size, ashift);
901 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
902 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
903 VDEV_TYPE_RAIDZ) == 0);
904 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
905 ztest_opts.zo_raidz_parity) == 0);
906 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
909 for (c = 0; c < r; c++)
910 nvlist_free(child[c]);
912 umem_free(child, r * sizeof (nvlist_t *));
918 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
919 uint64_t ashift, int r, int m)
921 nvlist_t *mirror, **child;
925 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
927 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
929 for (c = 0; c < m; c++)
930 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
932 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
933 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
934 VDEV_TYPE_MIRROR) == 0);
935 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
938 for (c = 0; c < m; c++)
939 nvlist_free(child[c]);
941 umem_free(child, m * sizeof (nvlist_t *));
947 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
948 int log, int r, int m, int t)
950 nvlist_t *root, **child;
955 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
957 for (c = 0; c < t; c++) {
958 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
960 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
964 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
965 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
966 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
969 for (c = 0; c < t; c++)
970 nvlist_free(child[c]);
972 umem_free(child, t * sizeof (nvlist_t *));
978 * Find a random spa version. Returns back a random spa version in the
979 * range [initial_version, SPA_VERSION_FEATURES].
982 ztest_random_spa_version(uint64_t initial_version)
984 uint64_t version = initial_version;
986 if (version <= SPA_VERSION_BEFORE_FEATURES) {
988 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
991 if (version > SPA_VERSION_BEFORE_FEATURES)
992 version = SPA_VERSION_FEATURES;
994 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
999 ztest_random_blocksize(void)
1001 uint64_t block_shift;
1003 * Choose a block size >= the ashift.
1004 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1006 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1007 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1009 block_shift = ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1010 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1014 ztest_random_ibshift(void)
1016 return (DN_MIN_INDBLKSHIFT +
1017 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1021 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1024 vdev_t *rvd = spa->spa_root_vdev;
1027 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1030 top = ztest_random(rvd->vdev_children);
1031 tvd = rvd->vdev_child[top];
1032 } while (!vdev_is_concrete(tvd) || (tvd->vdev_islog && !log_ok) ||
1033 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1039 ztest_random_dsl_prop(zfs_prop_t prop)
1044 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1045 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1051 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1054 const char *propname = zfs_prop_to_name(prop);
1055 const char *valname;
1056 char setpoint[MAXPATHLEN];
1060 error = dsl_prop_set_int(osname, propname,
1061 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1063 if (error == ENOSPC) {
1064 ztest_record_enospc(FTAG);
1069 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1071 if (ztest_opts.zo_verbose >= 6) {
1072 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1073 (void) printf("%s %s = %s at '%s'\n",
1074 osname, propname, valname, setpoint);
1081 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1083 spa_t *spa = ztest_spa;
1084 nvlist_t *props = NULL;
1087 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1088 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1090 error = spa_prop_set(spa, props);
1094 if (error == ENOSPC) {
1095 ztest_record_enospc(FTAG);
1104 ztest_rll_init(rll_t *rll)
1106 rll->rll_writer = NULL;
1107 rll->rll_readers = 0;
1108 mutex_init(&rll->rll_lock, NULL, USYNC_THREAD, NULL);
1109 cv_init(&rll->rll_cv, NULL, USYNC_THREAD, NULL);
1113 ztest_rll_destroy(rll_t *rll)
1115 ASSERT(rll->rll_writer == NULL);
1116 ASSERT(rll->rll_readers == 0);
1117 mutex_destroy(&rll->rll_lock);
1118 cv_destroy(&rll->rll_cv);
1122 ztest_rll_lock(rll_t *rll, rl_type_t type)
1124 mutex_enter(&rll->rll_lock);
1126 if (type == RL_READER) {
1127 while (rll->rll_writer != NULL)
1128 cv_wait(&rll->rll_cv, &rll->rll_lock);
1131 while (rll->rll_writer != NULL || rll->rll_readers)
1132 cv_wait(&rll->rll_cv, &rll->rll_lock);
1133 rll->rll_writer = curthread;
1136 mutex_exit(&rll->rll_lock);
1140 ztest_rll_unlock(rll_t *rll)
1142 mutex_enter(&rll->rll_lock);
1144 if (rll->rll_writer) {
1145 ASSERT(rll->rll_readers == 0);
1146 rll->rll_writer = NULL;
1148 ASSERT(rll->rll_readers != 0);
1149 ASSERT(rll->rll_writer == NULL);
1153 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1154 cv_broadcast(&rll->rll_cv);
1156 mutex_exit(&rll->rll_lock);
1160 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1162 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1164 ztest_rll_lock(rll, type);
1168 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1170 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1172 ztest_rll_unlock(rll);
1176 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1177 uint64_t size, rl_type_t type)
1179 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1180 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1183 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1184 rl->rl_object = object;
1185 rl->rl_offset = offset;
1189 ztest_rll_lock(rll, type);
1195 ztest_range_unlock(rl_t *rl)
1197 rll_t *rll = rl->rl_lock;
1199 ztest_rll_unlock(rll);
1201 umem_free(rl, sizeof (*rl));
1205 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1208 zd->zd_zilog = dmu_objset_zil(os);
1209 zd->zd_shared = szd;
1210 dmu_objset_name(os, zd->zd_name);
1212 if (zd->zd_shared != NULL)
1213 zd->zd_shared->zd_seq = 0;
1215 rw_init(&zd->zd_zilog_lock, NULL, USYNC_THREAD, NULL);
1216 mutex_init(&zd->zd_dirobj_lock, NULL, USYNC_THREAD, NULL);
1218 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1219 ztest_rll_init(&zd->zd_object_lock[l]);
1221 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1222 ztest_rll_init(&zd->zd_range_lock[l]);
1226 ztest_zd_fini(ztest_ds_t *zd)
1228 mutex_destroy(&zd->zd_dirobj_lock);
1230 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1231 ztest_rll_destroy(&zd->zd_object_lock[l]);
1233 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1234 ztest_rll_destroy(&zd->zd_range_lock[l]);
1237 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1240 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1246 * Attempt to assign tx to some transaction group.
1248 error = dmu_tx_assign(tx, txg_how);
1250 if (error == ERESTART) {
1251 ASSERT(txg_how == TXG_NOWAIT);
1254 ASSERT3U(error, ==, ENOSPC);
1255 ztest_record_enospc(tag);
1260 txg = dmu_tx_get_txg(tx);
1266 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1269 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1276 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1279 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1283 diff |= (value - *ip++);
1289 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1290 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1292 bt->bt_magic = BT_MAGIC;
1293 bt->bt_objset = dmu_objset_id(os);
1294 bt->bt_object = object;
1295 bt->bt_offset = offset;
1298 bt->bt_crtxg = crtxg;
1302 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1303 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1305 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1306 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1307 ASSERT3U(bt->bt_object, ==, object);
1308 ASSERT3U(bt->bt_offset, ==, offset);
1309 ASSERT3U(bt->bt_gen, <=, gen);
1310 ASSERT3U(bt->bt_txg, <=, txg);
1311 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1314 static ztest_block_tag_t *
1315 ztest_bt_bonus(dmu_buf_t *db)
1317 dmu_object_info_t doi;
1318 ztest_block_tag_t *bt;
1320 dmu_object_info_from_db(db, &doi);
1321 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1322 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1323 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1332 #define lrz_type lr_mode
1333 #define lrz_blocksize lr_uid
1334 #define lrz_ibshift lr_gid
1335 #define lrz_bonustype lr_rdev
1336 #define lrz_bonuslen lr_crtime[1]
1339 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1341 char *name = (void *)(lr + 1); /* name follows lr */
1342 size_t namesize = strlen(name) + 1;
1345 if (zil_replaying(zd->zd_zilog, tx))
1348 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1349 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1350 sizeof (*lr) + namesize - sizeof (lr_t));
1352 zil_itx_assign(zd->zd_zilog, itx, tx);
1356 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1358 char *name = (void *)(lr + 1); /* name follows lr */
1359 size_t namesize = strlen(name) + 1;
1362 if (zil_replaying(zd->zd_zilog, tx))
1365 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1366 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1367 sizeof (*lr) + namesize - sizeof (lr_t));
1369 itx->itx_oid = object;
1370 zil_itx_assign(zd->zd_zilog, itx, tx);
1374 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1377 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1379 if (zil_replaying(zd->zd_zilog, tx))
1382 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1383 write_state = WR_INDIRECT;
1385 itx = zil_itx_create(TX_WRITE,
1386 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1388 if (write_state == WR_COPIED &&
1389 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1390 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1391 zil_itx_destroy(itx);
1392 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1393 write_state = WR_NEED_COPY;
1395 itx->itx_private = zd;
1396 itx->itx_wr_state = write_state;
1397 itx->itx_sync = (ztest_random(8) == 0);
1399 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1400 sizeof (*lr) - sizeof (lr_t));
1402 zil_itx_assign(zd->zd_zilog, itx, tx);
1406 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1410 if (zil_replaying(zd->zd_zilog, tx))
1413 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1414 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1415 sizeof (*lr) - sizeof (lr_t));
1417 itx->itx_sync = B_FALSE;
1418 zil_itx_assign(zd->zd_zilog, itx, tx);
1422 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1426 if (zil_replaying(zd->zd_zilog, tx))
1429 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1430 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1431 sizeof (*lr) - sizeof (lr_t));
1433 itx->itx_sync = B_FALSE;
1434 zil_itx_assign(zd->zd_zilog, itx, tx);
1441 ztest_replay_create(void *arg1, void *arg2, boolean_t byteswap)
1443 ztest_ds_t *zd = arg1;
1444 lr_create_t *lr = arg2;
1445 char *name = (void *)(lr + 1); /* name follows lr */
1446 objset_t *os = zd->zd_os;
1447 ztest_block_tag_t *bbt;
1454 byteswap_uint64_array(lr, sizeof (*lr));
1456 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1457 ASSERT(name[0] != '\0');
1459 tx = dmu_tx_create(os);
1461 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1463 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1464 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1466 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1469 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1473 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1475 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1476 if (lr->lr_foid == 0) {
1477 lr->lr_foid = zap_create(os,
1478 lr->lrz_type, lr->lrz_bonustype,
1479 lr->lrz_bonuslen, tx);
1481 error = zap_create_claim(os, lr->lr_foid,
1482 lr->lrz_type, lr->lrz_bonustype,
1483 lr->lrz_bonuslen, tx);
1486 if (lr->lr_foid == 0) {
1487 lr->lr_foid = dmu_object_alloc(os,
1488 lr->lrz_type, 0, lr->lrz_bonustype,
1489 lr->lrz_bonuslen, tx);
1491 error = dmu_object_claim(os, lr->lr_foid,
1492 lr->lrz_type, 0, lr->lrz_bonustype,
1493 lr->lrz_bonuslen, tx);
1498 ASSERT3U(error, ==, EEXIST);
1499 ASSERT(zd->zd_zilog->zl_replay);
1504 ASSERT(lr->lr_foid != 0);
1506 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1507 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1508 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1510 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1511 bbt = ztest_bt_bonus(db);
1512 dmu_buf_will_dirty(db, tx);
1513 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1514 dmu_buf_rele(db, FTAG);
1516 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1519 (void) ztest_log_create(zd, tx, lr);
1527 ztest_replay_remove(void *arg1, void *arg2, boolean_t byteswap)
1529 ztest_ds_t *zd = arg1;
1530 lr_remove_t *lr = arg2;
1531 char *name = (void *)(lr + 1); /* name follows lr */
1532 objset_t *os = zd->zd_os;
1533 dmu_object_info_t doi;
1535 uint64_t object, txg;
1538 byteswap_uint64_array(lr, sizeof (*lr));
1540 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1541 ASSERT(name[0] != '\0');
1544 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1545 ASSERT(object != 0);
1547 ztest_object_lock(zd, object, RL_WRITER);
1549 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1551 tx = dmu_tx_create(os);
1553 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1554 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1556 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1558 ztest_object_unlock(zd, object);
1562 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1563 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1565 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1568 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1570 (void) ztest_log_remove(zd, tx, lr, object);
1574 ztest_object_unlock(zd, object);
1580 ztest_replay_write(void *arg1, void *arg2, boolean_t byteswap)
1582 ztest_ds_t *zd = arg1;
1583 lr_write_t *lr = arg2;
1584 objset_t *os = zd->zd_os;
1585 void *data = lr + 1; /* data follows lr */
1586 uint64_t offset, length;
1587 ztest_block_tag_t *bt = data;
1588 ztest_block_tag_t *bbt;
1589 uint64_t gen, txg, lrtxg, crtxg;
1590 dmu_object_info_t doi;
1593 arc_buf_t *abuf = NULL;
1597 byteswap_uint64_array(lr, sizeof (*lr));
1599 offset = lr->lr_offset;
1600 length = lr->lr_length;
1602 /* If it's a dmu_sync() block, write the whole block */
1603 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1604 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1605 if (length < blocksize) {
1606 offset -= offset % blocksize;
1611 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1612 byteswap_uint64_array(bt, sizeof (*bt));
1614 if (bt->bt_magic != BT_MAGIC)
1617 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1618 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1620 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1622 dmu_object_info_from_db(db, &doi);
1624 bbt = ztest_bt_bonus(db);
1625 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1627 crtxg = bbt->bt_crtxg;
1628 lrtxg = lr->lr_common.lrc_txg;
1630 tx = dmu_tx_create(os);
1632 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1634 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1635 P2PHASE(offset, length) == 0)
1636 abuf = dmu_request_arcbuf(db, length);
1638 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1641 dmu_return_arcbuf(abuf);
1642 dmu_buf_rele(db, FTAG);
1643 ztest_range_unlock(rl);
1644 ztest_object_unlock(zd, lr->lr_foid);
1650 * Usually, verify the old data before writing new data --
1651 * but not always, because we also want to verify correct
1652 * behavior when the data was not recently read into cache.
1654 ASSERT(offset % doi.doi_data_block_size == 0);
1655 if (ztest_random(4) != 0) {
1656 int prefetch = ztest_random(2) ?
1657 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1658 ztest_block_tag_t rbt;
1660 VERIFY(dmu_read(os, lr->lr_foid, offset,
1661 sizeof (rbt), &rbt, prefetch) == 0);
1662 if (rbt.bt_magic == BT_MAGIC) {
1663 ztest_bt_verify(&rbt, os, lr->lr_foid,
1664 offset, gen, txg, crtxg);
1669 * Writes can appear to be newer than the bonus buffer because
1670 * the ztest_get_data() callback does a dmu_read() of the
1671 * open-context data, which may be different than the data
1672 * as it was when the write was generated.
1674 if (zd->zd_zilog->zl_replay) {
1675 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1676 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1681 * Set the bt's gen/txg to the bonus buffer's gen/txg
1682 * so that all of the usual ASSERTs will work.
1684 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1688 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1690 bcopy(data, abuf->b_data, length);
1691 dmu_assign_arcbuf(db, offset, abuf, tx);
1694 (void) ztest_log_write(zd, tx, lr);
1696 dmu_buf_rele(db, FTAG);
1700 ztest_range_unlock(rl);
1701 ztest_object_unlock(zd, lr->lr_foid);
1707 ztest_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
1709 ztest_ds_t *zd = arg1;
1710 lr_truncate_t *lr = arg2;
1711 objset_t *os = zd->zd_os;
1717 byteswap_uint64_array(lr, sizeof (*lr));
1719 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1720 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1723 tx = dmu_tx_create(os);
1725 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1727 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1729 ztest_range_unlock(rl);
1730 ztest_object_unlock(zd, lr->lr_foid);
1734 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1735 lr->lr_length, tx) == 0);
1737 (void) ztest_log_truncate(zd, tx, lr);
1741 ztest_range_unlock(rl);
1742 ztest_object_unlock(zd, lr->lr_foid);
1748 ztest_replay_setattr(void *arg1, void *arg2, boolean_t byteswap)
1750 ztest_ds_t *zd = arg1;
1751 lr_setattr_t *lr = arg2;
1752 objset_t *os = zd->zd_os;
1755 ztest_block_tag_t *bbt;
1756 uint64_t txg, lrtxg, crtxg;
1759 byteswap_uint64_array(lr, sizeof (*lr));
1761 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1763 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1765 tx = dmu_tx_create(os);
1766 dmu_tx_hold_bonus(tx, lr->lr_foid);
1768 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1770 dmu_buf_rele(db, FTAG);
1771 ztest_object_unlock(zd, lr->lr_foid);
1775 bbt = ztest_bt_bonus(db);
1776 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1777 crtxg = bbt->bt_crtxg;
1778 lrtxg = lr->lr_common.lrc_txg;
1780 if (zd->zd_zilog->zl_replay) {
1781 ASSERT(lr->lr_size != 0);
1782 ASSERT(lr->lr_mode != 0);
1786 * Randomly change the size and increment the generation.
1788 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1790 lr->lr_mode = bbt->bt_gen + 1;
1795 * Verify that the current bonus buffer is not newer than our txg.
1797 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1798 MAX(txg, lrtxg), crtxg);
1800 dmu_buf_will_dirty(db, tx);
1802 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1803 ASSERT3U(lr->lr_size, <=, db->db_size);
1804 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1805 bbt = ztest_bt_bonus(db);
1807 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1809 dmu_buf_rele(db, FTAG);
1811 (void) ztest_log_setattr(zd, tx, lr);
1815 ztest_object_unlock(zd, lr->lr_foid);
1820 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1821 NULL, /* 0 no such transaction type */
1822 ztest_replay_create, /* TX_CREATE */
1823 NULL, /* TX_MKDIR */
1824 NULL, /* TX_MKXATTR */
1825 NULL, /* TX_SYMLINK */
1826 ztest_replay_remove, /* TX_REMOVE */
1827 NULL, /* TX_RMDIR */
1829 NULL, /* TX_RENAME */
1830 ztest_replay_write, /* TX_WRITE */
1831 ztest_replay_truncate, /* TX_TRUNCATE */
1832 ztest_replay_setattr, /* TX_SETATTR */
1834 NULL, /* TX_CREATE_ACL */
1835 NULL, /* TX_CREATE_ATTR */
1836 NULL, /* TX_CREATE_ACL_ATTR */
1837 NULL, /* TX_MKDIR_ACL */
1838 NULL, /* TX_MKDIR_ATTR */
1839 NULL, /* TX_MKDIR_ACL_ATTR */
1840 NULL, /* TX_WRITE2 */
1844 * ZIL get_data callbacks
1848 ztest_get_done(zgd_t *zgd, int error)
1850 ztest_ds_t *zd = zgd->zgd_private;
1851 uint64_t object = zgd->zgd_rl->rl_object;
1854 dmu_buf_rele(zgd->zgd_db, zgd);
1856 ztest_range_unlock(zgd->zgd_rl);
1857 ztest_object_unlock(zd, object);
1859 if (error == 0 && zgd->zgd_bp)
1860 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1862 umem_free(zgd, sizeof (*zgd));
1866 ztest_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb,
1869 ztest_ds_t *zd = arg;
1870 objset_t *os = zd->zd_os;
1871 uint64_t object = lr->lr_foid;
1872 uint64_t offset = lr->lr_offset;
1873 uint64_t size = lr->lr_length;
1874 uint64_t txg = lr->lr_common.lrc_txg;
1876 dmu_object_info_t doi;
1881 ASSERT3P(lwb, !=, NULL);
1882 ASSERT3P(zio, !=, NULL);
1883 ASSERT3U(size, !=, 0);
1885 ztest_object_lock(zd, object, RL_READER);
1886 error = dmu_bonus_hold(os, object, FTAG, &db);
1888 ztest_object_unlock(zd, object);
1892 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1894 if (crtxg == 0 || crtxg > txg) {
1895 dmu_buf_rele(db, FTAG);
1896 ztest_object_unlock(zd, object);
1900 dmu_object_info_from_db(db, &doi);
1901 dmu_buf_rele(db, FTAG);
1904 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1906 zgd->zgd_private = zd;
1908 if (buf != NULL) { /* immediate write */
1909 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1912 error = dmu_read(os, object, offset, size, buf,
1913 DMU_READ_NO_PREFETCH);
1916 size = doi.doi_data_block_size;
1918 offset = P2ALIGN(offset, size);
1920 ASSERT(offset < size);
1924 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1927 error = dmu_buf_hold(os, object, offset, zgd, &db,
1928 DMU_READ_NO_PREFETCH);
1931 blkptr_t *bp = &lr->lr_blkptr;
1936 ASSERT(db->db_offset == offset);
1937 ASSERT(db->db_size == size);
1939 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1940 ztest_get_done, zgd);
1947 ztest_get_done(zgd, error);
1953 ztest_lr_alloc(size_t lrsize, char *name)
1956 size_t namesize = name ? strlen(name) + 1 : 0;
1958 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1961 bcopy(name, lr + lrsize, namesize);
1967 ztest_lr_free(void *lr, size_t lrsize, char *name)
1969 size_t namesize = name ? strlen(name) + 1 : 0;
1971 umem_free(lr, lrsize + namesize);
1975 * Lookup a bunch of objects. Returns the number of objects not found.
1978 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1983 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
1985 for (int i = 0; i < count; i++, od++) {
1987 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1988 sizeof (uint64_t), 1, &od->od_object);
1990 ASSERT(error == ENOENT);
1991 ASSERT(od->od_object == 0);
1995 ztest_block_tag_t *bbt;
1996 dmu_object_info_t doi;
1998 ASSERT(od->od_object != 0);
1999 ASSERT(missing == 0); /* there should be no gaps */
2001 ztest_object_lock(zd, od->od_object, RL_READER);
2002 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2003 od->od_object, FTAG, &db));
2004 dmu_object_info_from_db(db, &doi);
2005 bbt = ztest_bt_bonus(db);
2006 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2007 od->od_type = doi.doi_type;
2008 od->od_blocksize = doi.doi_data_block_size;
2009 od->od_gen = bbt->bt_gen;
2010 dmu_buf_rele(db, FTAG);
2011 ztest_object_unlock(zd, od->od_object);
2019 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2023 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2025 for (int i = 0; i < count; i++, od++) {
2032 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2034 lr->lr_doid = od->od_dir;
2035 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2036 lr->lrz_type = od->od_crtype;
2037 lr->lrz_blocksize = od->od_crblocksize;
2038 lr->lrz_ibshift = ztest_random_ibshift();
2039 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2040 lr->lrz_bonuslen = dmu_bonus_max();
2041 lr->lr_gen = od->od_crgen;
2042 lr->lr_crtime[0] = time(NULL);
2044 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2045 ASSERT(missing == 0);
2049 od->od_object = lr->lr_foid;
2050 od->od_type = od->od_crtype;
2051 od->od_blocksize = od->od_crblocksize;
2052 od->od_gen = od->od_crgen;
2053 ASSERT(od->od_object != 0);
2056 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2063 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2068 ASSERT(MUTEX_HELD(&zd->zd_dirobj_lock));
2072 for (int i = count - 1; i >= 0; i--, od--) {
2079 * No object was found.
2081 if (od->od_object == 0)
2084 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2086 lr->lr_doid = od->od_dir;
2088 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2089 ASSERT3U(error, ==, ENOSPC);
2094 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2101 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2107 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2109 lr->lr_foid = object;
2110 lr->lr_offset = offset;
2111 lr->lr_length = size;
2113 BP_ZERO(&lr->lr_blkptr);
2115 bcopy(data, lr + 1, size);
2117 error = ztest_replay_write(zd, lr, B_FALSE);
2119 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2125 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2130 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2132 lr->lr_foid = object;
2133 lr->lr_offset = offset;
2134 lr->lr_length = size;
2136 error = ztest_replay_truncate(zd, lr, B_FALSE);
2138 ztest_lr_free(lr, sizeof (*lr), NULL);
2144 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2149 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2151 lr->lr_foid = object;
2155 error = ztest_replay_setattr(zd, lr, B_FALSE);
2157 ztest_lr_free(lr, sizeof (*lr), NULL);
2163 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2165 objset_t *os = zd->zd_os;
2170 txg_wait_synced(dmu_objset_pool(os), 0);
2172 ztest_object_lock(zd, object, RL_READER);
2173 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2175 tx = dmu_tx_create(os);
2177 dmu_tx_hold_write(tx, object, offset, size);
2179 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2182 dmu_prealloc(os, object, offset, size, tx);
2184 txg_wait_synced(dmu_objset_pool(os), txg);
2186 (void) dmu_free_long_range(os, object, offset, size);
2189 ztest_range_unlock(rl);
2190 ztest_object_unlock(zd, object);
2194 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2197 ztest_block_tag_t wbt;
2198 dmu_object_info_t doi;
2199 enum ztest_io_type io_type;
2203 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2204 blocksize = doi.doi_data_block_size;
2205 data = umem_alloc(blocksize, UMEM_NOFAIL);
2208 * Pick an i/o type at random, biased toward writing block tags.
2210 io_type = ztest_random(ZTEST_IO_TYPES);
2211 if (ztest_random(2) == 0)
2212 io_type = ZTEST_IO_WRITE_TAG;
2214 rw_enter(&zd->zd_zilog_lock, RW_READER);
2218 case ZTEST_IO_WRITE_TAG:
2219 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2220 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2223 case ZTEST_IO_WRITE_PATTERN:
2224 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2225 if (ztest_random(2) == 0) {
2227 * Induce fletcher2 collisions to ensure that
2228 * zio_ddt_collision() detects and resolves them
2229 * when using fletcher2-verify for deduplication.
2231 ((uint64_t *)data)[0] ^= 1ULL << 63;
2232 ((uint64_t *)data)[4] ^= 1ULL << 63;
2234 (void) ztest_write(zd, object, offset, blocksize, data);
2237 case ZTEST_IO_WRITE_ZEROES:
2238 bzero(data, blocksize);
2239 (void) ztest_write(zd, object, offset, blocksize, data);
2242 case ZTEST_IO_TRUNCATE:
2243 (void) ztest_truncate(zd, object, offset, blocksize);
2246 case ZTEST_IO_SETATTR:
2247 (void) ztest_setattr(zd, object);
2250 case ZTEST_IO_REWRITE:
2251 rw_enter(&ztest_name_lock, RW_READER);
2252 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2253 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2255 VERIFY(err == 0 || err == ENOSPC);
2256 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2257 ZFS_PROP_COMPRESSION,
2258 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2260 VERIFY(err == 0 || err == ENOSPC);
2261 rw_exit(&ztest_name_lock);
2263 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2264 DMU_READ_NO_PREFETCH));
2266 (void) ztest_write(zd, object, offset, blocksize, data);
2270 rw_exit(&zd->zd_zilog_lock);
2272 umem_free(data, blocksize);
2276 * Initialize an object description template.
2279 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2280 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2282 od->od_dir = ZTEST_DIROBJ;
2285 od->od_crtype = type;
2286 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2289 od->od_type = DMU_OT_NONE;
2290 od->od_blocksize = 0;
2293 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2294 tag, (int64_t)id, index);
2298 * Lookup or create the objects for a test using the od template.
2299 * If the objects do not all exist, or if 'remove' is specified,
2300 * remove any existing objects and create new ones. Otherwise,
2301 * use the existing objects.
2304 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2306 int count = size / sizeof (*od);
2309 mutex_enter(&zd->zd_dirobj_lock);
2310 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2311 (ztest_remove(zd, od, count) != 0 ||
2312 ztest_create(zd, od, count) != 0))
2315 mutex_exit(&zd->zd_dirobj_lock);
2322 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2324 zilog_t *zilog = zd->zd_zilog;
2326 rw_enter(&zd->zd_zilog_lock, RW_READER);
2328 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2331 * Remember the committed values in zd, which is in parent/child
2332 * shared memory. If we die, the next iteration of ztest_run()
2333 * will verify that the log really does contain this record.
2335 mutex_enter(&zilog->zl_lock);
2336 ASSERT(zd->zd_shared != NULL);
2337 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2338 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2339 mutex_exit(&zilog->zl_lock);
2341 rw_exit(&zd->zd_zilog_lock);
2345 * This function is designed to simulate the operations that occur during a
2346 * mount/unmount operation. We hold the dataset across these operations in an
2347 * attempt to expose any implicit assumptions about ZIL management.
2351 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2353 objset_t *os = zd->zd_os;
2356 * We grab the zd_dirobj_lock to ensure that no other thread is
2357 * updating the zil (i.e. adding in-memory log records) and the
2358 * zd_zilog_lock to block any I/O.
2360 mutex_enter(&zd->zd_dirobj_lock);
2361 rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2363 /* zfsvfs_teardown() */
2364 zil_close(zd->zd_zilog);
2366 /* zfsvfs_setup() */
2367 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2368 zil_replay(os, zd, ztest_replay_vector);
2370 rw_exit(&zd->zd_zilog_lock);
2371 mutex_exit(&zd->zd_dirobj_lock);
2375 * Verify that we can't destroy an active pool, create an existing pool,
2376 * or create a pool with a bad vdev spec.
2380 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2382 ztest_shared_opts_t *zo = &ztest_opts;
2387 * Attempt to create using a bad file.
2389 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2390 VERIFY3U(ENOENT, ==,
2391 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2392 nvlist_free(nvroot);
2395 * Attempt to create using a bad mirror.
2397 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2398 VERIFY3U(ENOENT, ==,
2399 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2400 nvlist_free(nvroot);
2403 * Attempt to create an existing pool. It shouldn't matter
2404 * what's in the nvroot; we should fail with EEXIST.
2406 rw_enter(&ztest_name_lock, RW_READER);
2407 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2408 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2409 nvlist_free(nvroot);
2410 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2411 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2412 spa_close(spa, FTAG);
2414 rw_exit(&ztest_name_lock);
2419 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2422 uint64_t initial_version = SPA_VERSION_INITIAL;
2423 uint64_t version, newversion;
2424 nvlist_t *nvroot, *props;
2427 mutex_enter(&ztest_vdev_lock);
2428 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2431 * Clean up from previous runs.
2433 (void) spa_destroy(name);
2435 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2436 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2439 * If we're configuring a RAIDZ device then make sure that the
2440 * the initial version is capable of supporting that feature.
2442 switch (ztest_opts.zo_raidz_parity) {
2445 initial_version = SPA_VERSION_INITIAL;
2448 initial_version = SPA_VERSION_RAIDZ2;
2451 initial_version = SPA_VERSION_RAIDZ3;
2456 * Create a pool with a spa version that can be upgraded. Pick
2457 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2460 version = ztest_random_spa_version(initial_version);
2461 } while (version > SPA_VERSION_BEFORE_FEATURES);
2463 props = fnvlist_alloc();
2464 fnvlist_add_uint64(props,
2465 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2466 VERIFY0(spa_create(name, nvroot, props, NULL));
2467 fnvlist_free(nvroot);
2468 fnvlist_free(props);
2470 VERIFY0(spa_open(name, &spa, FTAG));
2471 VERIFY3U(spa_version(spa), ==, version);
2472 newversion = ztest_random_spa_version(version + 1);
2474 if (ztest_opts.zo_verbose >= 4) {
2475 (void) printf("upgrading spa version from %llu to %llu\n",
2476 (u_longlong_t)version, (u_longlong_t)newversion);
2479 spa_upgrade(spa, newversion);
2480 VERIFY3U(spa_version(spa), >, version);
2481 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2482 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2483 spa_close(spa, FTAG);
2486 mutex_exit(&ztest_vdev_lock);
2490 ztest_spa_checkpoint(spa_t *spa)
2492 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2494 int error = spa_checkpoint(spa->spa_name);
2498 case ZFS_ERR_DEVRM_IN_PROGRESS:
2499 case ZFS_ERR_DISCARDING_CHECKPOINT:
2500 case ZFS_ERR_CHECKPOINT_EXISTS:
2503 ztest_record_enospc(FTAG);
2506 fatal(0, "spa_checkpoint(%s) = %d", spa->spa_name, error);
2511 ztest_spa_discard_checkpoint(spa_t *spa)
2513 ASSERT(MUTEX_HELD(&ztest_checkpoint_lock));
2515 int error = spa_checkpoint_discard(spa->spa_name);
2519 case ZFS_ERR_DISCARDING_CHECKPOINT:
2520 case ZFS_ERR_NO_CHECKPOINT:
2523 fatal(0, "spa_discard_checkpoint(%s) = %d",
2524 spa->spa_name, error);
2531 ztest_spa_checkpoint_create_discard(ztest_ds_t *zd, uint64_t id)
2533 spa_t *spa = ztest_spa;
2535 mutex_enter(&ztest_checkpoint_lock);
2536 if (ztest_random(2) == 0) {
2537 ztest_spa_checkpoint(spa);
2539 ztest_spa_discard_checkpoint(spa);
2541 mutex_exit(&ztest_checkpoint_lock);
2546 vdev_lookup_by_path(vdev_t *vd, const char *path)
2550 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2553 for (int c = 0; c < vd->vdev_children; c++)
2554 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2562 * Find the first available hole which can be used as a top-level.
2565 find_vdev_hole(spa_t *spa)
2567 vdev_t *rvd = spa->spa_root_vdev;
2570 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2572 for (c = 0; c < rvd->vdev_children; c++) {
2573 vdev_t *cvd = rvd->vdev_child[c];
2575 if (cvd->vdev_ishole)
2582 * Verify that vdev_add() works as expected.
2586 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2588 ztest_shared_t *zs = ztest_shared;
2589 spa_t *spa = ztest_spa;
2595 mutex_enter(&ztest_vdev_lock);
2596 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2598 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2600 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2603 * If we have slogs then remove them 1/4 of the time.
2605 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2607 * Grab the guid from the head of the log class rotor.
2609 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2611 spa_config_exit(spa, SCL_VDEV, FTAG);
2614 * We have to grab the zs_name_lock as writer to
2615 * prevent a race between removing a slog (dmu_objset_find)
2616 * and destroying a dataset. Removing the slog will
2617 * grab a reference on the dataset which may cause
2618 * dmu_objset_destroy() to fail with EBUSY thus
2619 * leaving the dataset in an inconsistent state.
2621 rw_enter(&ztest_name_lock, RW_WRITER);
2622 error = spa_vdev_remove(spa, guid, B_FALSE);
2623 rw_exit(&ztest_name_lock);
2628 case ZFS_ERR_CHECKPOINT_EXISTS:
2629 case ZFS_ERR_DISCARDING_CHECKPOINT:
2632 fatal(0, "spa_vdev_remove() = %d", error);
2635 spa_config_exit(spa, SCL_VDEV, FTAG);
2638 * Make 1/4 of the devices be log devices.
2640 nvroot = make_vdev_root(NULL, NULL, NULL,
2641 ztest_opts.zo_vdev_size, 0,
2642 ztest_random(4) == 0, ztest_opts.zo_raidz,
2645 error = spa_vdev_add(spa, nvroot);
2646 nvlist_free(nvroot);
2652 ztest_record_enospc("spa_vdev_add");
2655 fatal(0, "spa_vdev_add() = %d", error);
2659 mutex_exit(&ztest_vdev_lock);
2663 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2667 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2669 ztest_shared_t *zs = ztest_shared;
2670 spa_t *spa = ztest_spa;
2671 vdev_t *rvd = spa->spa_root_vdev;
2672 spa_aux_vdev_t *sav;
2677 if (ztest_random(2) == 0) {
2678 sav = &spa->spa_spares;
2679 aux = ZPOOL_CONFIG_SPARES;
2681 sav = &spa->spa_l2cache;
2682 aux = ZPOOL_CONFIG_L2CACHE;
2685 mutex_enter(&ztest_vdev_lock);
2687 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2689 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2691 * Pick a random device to remove.
2693 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2696 * Find an unused device we can add.
2698 zs->zs_vdev_aux = 0;
2700 char path[MAXPATHLEN];
2702 (void) snprintf(path, sizeof (path), ztest_aux_template,
2703 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2705 for (c = 0; c < sav->sav_count; c++)
2706 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2709 if (c == sav->sav_count &&
2710 vdev_lookup_by_path(rvd, path) == NULL)
2716 spa_config_exit(spa, SCL_VDEV, FTAG);
2722 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2723 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2724 error = spa_vdev_add(spa, nvroot);
2730 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2732 nvlist_free(nvroot);
2735 * Remove an existing device. Sometimes, dirty its
2736 * vdev state first to make sure we handle removal
2737 * of devices that have pending state changes.
2739 if (ztest_random(2) == 0)
2740 (void) vdev_online(spa, guid, 0, NULL);
2742 error = spa_vdev_remove(spa, guid, B_FALSE);
2747 case ZFS_ERR_CHECKPOINT_EXISTS:
2748 case ZFS_ERR_DISCARDING_CHECKPOINT:
2751 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2755 mutex_exit(&ztest_vdev_lock);
2759 * split a pool if it has mirror tlvdevs
2763 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2765 ztest_shared_t *zs = ztest_shared;
2766 spa_t *spa = ztest_spa;
2767 vdev_t *rvd = spa->spa_root_vdev;
2768 nvlist_t *tree, **child, *config, *split, **schild;
2769 uint_t c, children, schildren = 0, lastlogid = 0;
2772 mutex_enter(&ztest_vdev_lock);
2774 /* ensure we have a useable config; mirrors of raidz aren't supported */
2775 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2776 mutex_exit(&ztest_vdev_lock);
2780 /* clean up the old pool, if any */
2781 (void) spa_destroy("splitp");
2783 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2785 /* generate a config from the existing config */
2786 mutex_enter(&spa->spa_props_lock);
2787 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2789 mutex_exit(&spa->spa_props_lock);
2791 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2794 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2795 for (c = 0; c < children; c++) {
2796 vdev_t *tvd = rvd->vdev_child[c];
2800 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2801 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2803 VERIFY(nvlist_add_string(schild[schildren],
2804 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2805 VERIFY(nvlist_add_uint64(schild[schildren],
2806 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2808 lastlogid = schildren;
2813 VERIFY(nvlist_lookup_nvlist_array(child[c],
2814 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2815 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2818 /* OK, create a config that can be used to split */
2819 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2820 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2821 VDEV_TYPE_ROOT) == 0);
2822 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2823 lastlogid != 0 ? lastlogid : schildren) == 0);
2825 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2826 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2828 for (c = 0; c < schildren; c++)
2829 nvlist_free(schild[c]);
2833 spa_config_exit(spa, SCL_VDEV, FTAG);
2835 rw_enter(&ztest_name_lock, RW_WRITER);
2836 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2837 rw_exit(&ztest_name_lock);
2839 nvlist_free(config);
2842 (void) printf("successful split - results:\n");
2843 mutex_enter(&spa_namespace_lock);
2844 show_pool_stats(spa);
2845 show_pool_stats(spa_lookup("splitp"));
2846 mutex_exit(&spa_namespace_lock);
2850 mutex_exit(&ztest_vdev_lock);
2854 * Verify that we can attach and detach devices.
2858 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2860 ztest_shared_t *zs = ztest_shared;
2861 spa_t *spa = ztest_spa;
2862 spa_aux_vdev_t *sav = &spa->spa_spares;
2863 vdev_t *rvd = spa->spa_root_vdev;
2864 vdev_t *oldvd, *newvd, *pvd;
2868 uint64_t ashift = ztest_get_ashift();
2869 uint64_t oldguid, pguid;
2870 uint64_t oldsize, newsize;
2871 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2873 int oldvd_has_siblings = B_FALSE;
2874 int newvd_is_spare = B_FALSE;
2876 int error, expected_error;
2878 mutex_enter(&ztest_vdev_lock);
2879 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2881 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2884 * If a vdev is in the process of being removed, its removal may
2885 * finish while we are in progress, leading to an unexpected error
2886 * value. Don't bother trying to attach while we are in the middle
2889 if (ztest_device_removal_active) {
2890 spa_config_exit(spa, SCL_ALL, FTAG);
2891 mutex_exit(&ztest_vdev_lock);
2896 * Decide whether to do an attach or a replace.
2898 replacing = ztest_random(2);
2901 * Pick a random top-level vdev.
2903 top = ztest_random_vdev_top(spa, B_TRUE);
2906 * Pick a random leaf within it.
2908 leaf = ztest_random(leaves);
2913 oldvd = rvd->vdev_child[top];
2914 if (zs->zs_mirrors >= 1) {
2915 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2916 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2917 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2919 if (ztest_opts.zo_raidz > 1) {
2920 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2921 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2922 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2926 * If we're already doing an attach or replace, oldvd may be a
2927 * mirror vdev -- in which case, pick a random child.
2929 while (oldvd->vdev_children != 0) {
2930 oldvd_has_siblings = B_TRUE;
2931 ASSERT(oldvd->vdev_children >= 2);
2932 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2935 oldguid = oldvd->vdev_guid;
2936 oldsize = vdev_get_min_asize(oldvd);
2937 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2938 (void) strcpy(oldpath, oldvd->vdev_path);
2939 pvd = oldvd->vdev_parent;
2940 pguid = pvd->vdev_guid;
2943 * If oldvd has siblings, then half of the time, detach it.
2945 if (oldvd_has_siblings && ztest_random(2) == 0) {
2946 spa_config_exit(spa, SCL_ALL, FTAG);
2947 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2948 if (error != 0 && error != ENODEV && error != EBUSY &&
2949 error != ENOTSUP && error != ZFS_ERR_CHECKPOINT_EXISTS &&
2950 error != ZFS_ERR_DISCARDING_CHECKPOINT)
2951 fatal(0, "detach (%s) returned %d", oldpath, error);
2952 mutex_exit(&ztest_vdev_lock);
2957 * For the new vdev, choose with equal probability between the two
2958 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2960 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2961 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2962 newvd_is_spare = B_TRUE;
2963 (void) strcpy(newpath, newvd->vdev_path);
2965 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2966 ztest_opts.zo_dir, ztest_opts.zo_pool,
2967 top * leaves + leaf);
2968 if (ztest_random(2) == 0)
2969 newpath[strlen(newpath) - 1] = 'b';
2970 newvd = vdev_lookup_by_path(rvd, newpath);
2975 * Reopen to ensure the vdev's asize field isn't stale.
2978 newsize = vdev_get_min_asize(newvd);
2981 * Make newsize a little bigger or smaller than oldsize.
2982 * If it's smaller, the attach should fail.
2983 * If it's larger, and we're doing a replace,
2984 * we should get dynamic LUN growth when we're done.
2986 newsize = 10 * oldsize / (9 + ztest_random(3));
2990 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2991 * unless it's a replace; in that case any non-replacing parent is OK.
2993 * If newvd is already part of the pool, it should fail with EBUSY.
2995 * If newvd is too small, it should fail with EOVERFLOW.
2997 if (pvd->vdev_ops != &vdev_mirror_ops &&
2998 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2999 pvd->vdev_ops == &vdev_replacing_ops ||
3000 pvd->vdev_ops == &vdev_spare_ops))
3001 expected_error = ENOTSUP;
3002 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3003 expected_error = ENOTSUP;
3004 else if (newvd == oldvd)
3005 expected_error = replacing ? 0 : EBUSY;
3006 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3007 expected_error = EBUSY;
3008 else if (newsize < oldsize)
3009 expected_error = EOVERFLOW;
3010 else if (ashift > oldvd->vdev_top->vdev_ashift)
3011 expected_error = EDOM;
3015 spa_config_exit(spa, SCL_ALL, FTAG);
3018 * Build the nvlist describing newpath.
3020 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3021 ashift, 0, 0, 0, 1);
3023 error = spa_vdev_attach(spa, oldguid, root, replacing);
3028 * If our parent was the replacing vdev, but the replace completed,
3029 * then instead of failing with ENOTSUP we may either succeed,
3030 * fail with ENODEV, or fail with EOVERFLOW.
3032 if (expected_error == ENOTSUP &&
3033 (error == 0 || error == ENODEV || error == EOVERFLOW))
3034 expected_error = error;
3037 * If someone grew the LUN, the replacement may be too small.
3039 if (error == EOVERFLOW || error == EBUSY)
3040 expected_error = error;
3042 if (error == ZFS_ERR_CHECKPOINT_EXISTS ||
3043 error == ZFS_ERR_DISCARDING_CHECKPOINT)
3044 expected_error = error;
3046 /* XXX workaround 6690467 */
3047 if (error != expected_error && expected_error != EBUSY) {
3048 fatal(0, "attach (%s %llu, %s %llu, %d) "
3049 "returned %d, expected %d",
3050 oldpath, oldsize, newpath,
3051 newsize, replacing, error, expected_error);
3054 mutex_exit(&ztest_vdev_lock);
3059 ztest_device_removal(ztest_ds_t *zd, uint64_t id)
3061 spa_t *spa = ztest_spa;
3066 mutex_enter(&ztest_vdev_lock);
3068 if (ztest_device_removal_active) {
3069 mutex_exit(&ztest_vdev_lock);
3074 * Remove a random top-level vdev and wait for removal to finish.
3076 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3077 vd = vdev_lookup_top(spa, ztest_random_vdev_top(spa, B_FALSE));
3078 guid = vd->vdev_guid;
3079 spa_config_exit(spa, SCL_VDEV, FTAG);
3081 error = spa_vdev_remove(spa, guid, B_FALSE);
3083 ztest_device_removal_active = B_TRUE;
3084 mutex_exit(&ztest_vdev_lock);
3086 while (spa->spa_vdev_removal != NULL)
3087 txg_wait_synced(spa_get_dsl(spa), 0);
3089 mutex_exit(&ztest_vdev_lock);
3094 * The pool needs to be scrubbed after completing device removal.
3095 * Failure to do so may result in checksum errors due to the
3096 * strategy employed by ztest_fault_inject() when selecting which
3097 * offset are redundant and can be damaged.
3099 error = spa_scan(spa, POOL_SCAN_SCRUB);
3101 while (dsl_scan_scrubbing(spa_get_dsl(spa)))
3102 txg_wait_synced(spa_get_dsl(spa), 0);
3105 mutex_enter(&ztest_vdev_lock);
3106 ztest_device_removal_active = B_FALSE;
3107 mutex_exit(&ztest_vdev_lock);
3111 * Callback function which expands the physical size of the vdev.
3114 grow_vdev(vdev_t *vd, void *arg)
3116 spa_t *spa = vd->vdev_spa;
3117 size_t *newsize = arg;
3121 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3122 ASSERT(vd->vdev_ops->vdev_op_leaf);
3124 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3127 fsize = lseek(fd, 0, SEEK_END);
3128 (void) ftruncate(fd, *newsize);
3130 if (ztest_opts.zo_verbose >= 6) {
3131 (void) printf("%s grew from %lu to %lu bytes\n",
3132 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3139 * Callback function which expands a given vdev by calling vdev_online().
3143 online_vdev(vdev_t *vd, void *arg)
3145 spa_t *spa = vd->vdev_spa;
3146 vdev_t *tvd = vd->vdev_top;
3147 uint64_t guid = vd->vdev_guid;
3148 uint64_t generation = spa->spa_config_generation + 1;
3149 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3152 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3153 ASSERT(vd->vdev_ops->vdev_op_leaf);
3155 /* Calling vdev_online will initialize the new metaslabs */
3156 spa_config_exit(spa, SCL_STATE, spa);
3157 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3158 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3161 * If vdev_online returned an error or the underlying vdev_open
3162 * failed then we abort the expand. The only way to know that
3163 * vdev_open fails is by checking the returned newstate.
3165 if (error || newstate != VDEV_STATE_HEALTHY) {
3166 if (ztest_opts.zo_verbose >= 5) {
3167 (void) printf("Unable to expand vdev, state %llu, "
3168 "error %d\n", (u_longlong_t)newstate, error);
3172 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3175 * Since we dropped the lock we need to ensure that we're
3176 * still talking to the original vdev. It's possible this
3177 * vdev may have been detached/replaced while we were
3178 * trying to online it.
3180 if (generation != spa->spa_config_generation) {
3181 if (ztest_opts.zo_verbose >= 5) {
3182 (void) printf("vdev configuration has changed, "
3183 "guid %llu, state %llu, expected gen %llu, "
3186 (u_longlong_t)tvd->vdev_state,
3187 (u_longlong_t)generation,
3188 (u_longlong_t)spa->spa_config_generation);
3196 * Traverse the vdev tree calling the supplied function.
3197 * We continue to walk the tree until we either have walked all
3198 * children or we receive a non-NULL return from the callback.
3199 * If a NULL callback is passed, then we just return back the first
3200 * leaf vdev we encounter.
3203 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3205 if (vd->vdev_ops->vdev_op_leaf) {
3209 return (func(vd, arg));
3212 for (uint_t c = 0; c < vd->vdev_children; c++) {
3213 vdev_t *cvd = vd->vdev_child[c];
3214 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3221 * Verify that dynamic LUN growth works as expected.
3225 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3227 spa_t *spa = ztest_spa;
3229 metaslab_class_t *mc;
3230 metaslab_group_t *mg;
3231 size_t psize, newsize;
3233 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3235 mutex_enter(&ztest_checkpoint_lock);
3236 mutex_enter(&ztest_vdev_lock);
3237 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3240 * If there is a vdev removal in progress, it could complete while
3241 * we are running, in which case we would not be able to verify
3242 * that the metaslab_class space increased (because it decreases
3243 * when the device removal completes).
3245 if (ztest_device_removal_active) {
3246 spa_config_exit(spa, SCL_STATE, spa);
3247 mutex_exit(&ztest_vdev_lock);
3248 mutex_exit(&ztest_checkpoint_lock);
3252 top = ztest_random_vdev_top(spa, B_TRUE);
3254 tvd = spa->spa_root_vdev->vdev_child[top];
3257 old_ms_count = tvd->vdev_ms_count;
3258 old_class_space = metaslab_class_get_space(mc);
3261 * Determine the size of the first leaf vdev associated with
3262 * our top-level device.
3264 vd = vdev_walk_tree(tvd, NULL, NULL);
3265 ASSERT3P(vd, !=, NULL);
3266 ASSERT(vd->vdev_ops->vdev_op_leaf);
3268 psize = vd->vdev_psize;
3271 * We only try to expand the vdev if it's healthy, less than 4x its
3272 * original size, and it has a valid psize.
3274 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3275 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3276 spa_config_exit(spa, SCL_STATE, spa);
3277 mutex_exit(&ztest_vdev_lock);
3278 mutex_exit(&ztest_checkpoint_lock);
3282 newsize = psize + psize / 8;
3283 ASSERT3U(newsize, >, psize);
3285 if (ztest_opts.zo_verbose >= 6) {
3286 (void) printf("Expanding LUN %s from %lu to %lu\n",
3287 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3291 * Growing the vdev is a two step process:
3292 * 1). expand the physical size (i.e. relabel)
3293 * 2). online the vdev to create the new metaslabs
3295 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3296 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3297 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3298 if (ztest_opts.zo_verbose >= 5) {
3299 (void) printf("Could not expand LUN because "
3300 "the vdev configuration changed.\n");
3302 spa_config_exit(spa, SCL_STATE, spa);
3303 mutex_exit(&ztest_vdev_lock);
3304 mutex_exit(&ztest_checkpoint_lock);
3308 spa_config_exit(spa, SCL_STATE, spa);
3311 * Expanding the LUN will update the config asynchronously,
3312 * thus we must wait for the async thread to complete any
3313 * pending tasks before proceeding.
3317 mutex_enter(&spa->spa_async_lock);
3318 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3319 mutex_exit(&spa->spa_async_lock);
3322 txg_wait_synced(spa_get_dsl(spa), 0);
3323 (void) poll(NULL, 0, 100);
3326 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3328 tvd = spa->spa_root_vdev->vdev_child[top];
3329 new_ms_count = tvd->vdev_ms_count;
3330 new_class_space = metaslab_class_get_space(mc);
3332 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3333 if (ztest_opts.zo_verbose >= 5) {
3334 (void) printf("Could not verify LUN expansion due to "
3335 "intervening vdev offline or remove.\n");
3337 spa_config_exit(spa, SCL_STATE, spa);
3338 mutex_exit(&ztest_vdev_lock);
3339 mutex_exit(&ztest_checkpoint_lock);
3344 * Make sure we were able to grow the vdev.
3346 if (new_ms_count <= old_ms_count) {
3347 fatal(0, "LUN expansion failed: ms_count %llu < %llu\n",
3348 old_ms_count, new_ms_count);
3352 * Make sure we were able to grow the pool.
3354 if (new_class_space <= old_class_space) {
3355 fatal(0, "LUN expansion failed: class_space %llu < %llu\n",
3356 old_class_space, new_class_space);
3359 if (ztest_opts.zo_verbose >= 5) {
3360 char oldnumbuf[NN_NUMBUF_SZ], newnumbuf[NN_NUMBUF_SZ];
3362 nicenum(old_class_space, oldnumbuf, sizeof (oldnumbuf));
3363 nicenum(new_class_space, newnumbuf, sizeof (newnumbuf));
3364 (void) printf("%s grew from %s to %s\n",
3365 spa->spa_name, oldnumbuf, newnumbuf);
3368 spa_config_exit(spa, SCL_STATE, spa);
3369 mutex_exit(&ztest_vdev_lock);
3370 mutex_exit(&ztest_checkpoint_lock);
3374 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3378 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3381 * Create the objects common to all ztest datasets.
3383 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3384 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3388 ztest_dataset_create(char *dsname)
3390 uint64_t zilset = ztest_random(100);
3391 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3392 ztest_objset_create_cb, NULL);
3394 if (err || zilset < 80)
3397 if (ztest_opts.zo_verbose >= 6)
3398 (void) printf("Setting dataset %s to sync always\n", dsname);
3399 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3400 ZFS_SYNC_ALWAYS, B_FALSE));
3405 ztest_objset_destroy_cb(const char *name, void *arg)
3408 dmu_object_info_t doi;
3412 * Verify that the dataset contains a directory object.
3414 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3415 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3416 if (error != ENOENT) {
3417 /* We could have crashed in the middle of destroying it */
3419 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3420 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3422 dmu_objset_disown(os, FTAG);
3425 * Destroy the dataset.
3427 if (strchr(name, '@') != NULL) {
3428 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3430 VERIFY0(dsl_destroy_head(name));
3436 ztest_snapshot_create(char *osname, uint64_t id)
3438 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3441 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3443 error = dmu_objset_snapshot_one(osname, snapname);
3444 if (error == ENOSPC) {
3445 ztest_record_enospc(FTAG);
3448 if (error != 0 && error != EEXIST) {
3449 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3456 ztest_snapshot_destroy(char *osname, uint64_t id)
3458 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3461 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3464 error = dsl_destroy_snapshot(snapname, B_FALSE);
3465 if (error != 0 && error != ENOENT)
3466 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3472 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3478 char name[ZFS_MAX_DATASET_NAME_LEN];
3481 rw_enter(&ztest_name_lock, RW_READER);
3483 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3484 ztest_opts.zo_pool, (u_longlong_t)id);
3487 * If this dataset exists from a previous run, process its replay log
3488 * half of the time. If we don't replay it, then dmu_objset_destroy()
3489 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3491 if (ztest_random(2) == 0 &&
3492 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3493 ztest_zd_init(&zdtmp, NULL, os);
3494 zil_replay(os, &zdtmp, ztest_replay_vector);
3495 ztest_zd_fini(&zdtmp);
3496 dmu_objset_disown(os, FTAG);
3500 * There may be an old instance of the dataset we're about to
3501 * create lying around from a previous run. If so, destroy it
3502 * and all of its snapshots.
3504 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3505 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3508 * Verify that the destroyed dataset is no longer in the namespace.
3510 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3514 * Verify that we can create a new dataset.
3516 error = ztest_dataset_create(name);
3518 if (error == ENOSPC) {
3519 ztest_record_enospc(FTAG);
3520 rw_exit(&ztest_name_lock);
3523 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3526 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3528 ztest_zd_init(&zdtmp, NULL, os);
3531 * Open the intent log for it.
3533 zilog = zil_open(os, ztest_get_data);
3536 * Put some objects in there, do a little I/O to them,
3537 * and randomly take a couple of snapshots along the way.
3539 iters = ztest_random(5);
3540 for (int i = 0; i < iters; i++) {
3541 ztest_dmu_object_alloc_free(&zdtmp, id);
3542 if (ztest_random(iters) == 0)
3543 (void) ztest_snapshot_create(name, i);
3547 * Verify that we cannot create an existing dataset.
3549 VERIFY3U(EEXIST, ==,
3550 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3553 * Verify that we can hold an objset that is also owned.
3555 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3556 dmu_objset_rele(os2, FTAG);
3559 * Verify that we cannot own an objset that is already owned.
3562 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3565 dmu_objset_disown(os, FTAG);
3566 ztest_zd_fini(&zdtmp);
3568 rw_exit(&ztest_name_lock);
3572 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3575 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3577 rw_enter(&ztest_name_lock, RW_READER);
3578 (void) ztest_snapshot_destroy(zd->zd_name, id);
3579 (void) ztest_snapshot_create(zd->zd_name, id);
3580 rw_exit(&ztest_name_lock);
3584 * Cleanup non-standard snapshots and clones.
3587 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3589 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3590 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3591 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3592 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3593 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3596 (void) snprintf(snap1name, sizeof (snap1name),
3597 "%s@s1_%llu", osname, id);
3598 (void) snprintf(clone1name, sizeof (clone1name),
3599 "%s/c1_%llu", osname, id);
3600 (void) snprintf(snap2name, sizeof (snap2name),
3601 "%s@s2_%llu", clone1name, id);
3602 (void) snprintf(clone2name, sizeof (clone2name),
3603 "%s/c2_%llu", osname, id);
3604 (void) snprintf(snap3name, sizeof (snap3name),
3605 "%s@s3_%llu", clone1name, id);
3607 error = dsl_destroy_head(clone2name);
3608 if (error && error != ENOENT)
3609 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3610 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3611 if (error && error != ENOENT)
3612 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3613 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3614 if (error && error != ENOENT)
3615 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3616 error = dsl_destroy_head(clone1name);
3617 if (error && error != ENOENT)
3618 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3619 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3620 if (error && error != ENOENT)
3621 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3625 * Verify dsl_dataset_promote handles EBUSY
3628 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3631 char snap1name[ZFS_MAX_DATASET_NAME_LEN];
3632 char clone1name[ZFS_MAX_DATASET_NAME_LEN];
3633 char snap2name[ZFS_MAX_DATASET_NAME_LEN];
3634 char clone2name[ZFS_MAX_DATASET_NAME_LEN];
3635 char snap3name[ZFS_MAX_DATASET_NAME_LEN];
3636 char *osname = zd->zd_name;
3639 rw_enter(&ztest_name_lock, RW_READER);
3641 ztest_dsl_dataset_cleanup(osname, id);
3643 (void) snprintf(snap1name, sizeof (snap1name),
3644 "%s@s1_%llu", osname, id);
3645 (void) snprintf(clone1name, sizeof (clone1name),
3646 "%s/c1_%llu", osname, id);
3647 (void) snprintf(snap2name, sizeof (snap2name),
3648 "%s@s2_%llu", clone1name, id);
3649 (void) snprintf(clone2name, sizeof (clone2name),
3650 "%s/c2_%llu", osname, id);
3651 (void) snprintf(snap3name, sizeof (snap3name),
3652 "%s@s3_%llu", clone1name, id);
3654 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3655 if (error && error != EEXIST) {
3656 if (error == ENOSPC) {
3657 ztest_record_enospc(FTAG);
3660 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3663 error = dmu_objset_clone(clone1name, snap1name);
3665 if (error == ENOSPC) {
3666 ztest_record_enospc(FTAG);
3669 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3672 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3673 if (error && error != EEXIST) {
3674 if (error == ENOSPC) {
3675 ztest_record_enospc(FTAG);
3678 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3681 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3682 if (error && error != EEXIST) {
3683 if (error == ENOSPC) {
3684 ztest_record_enospc(FTAG);
3687 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3690 error = dmu_objset_clone(clone2name, snap3name);
3692 if (error == ENOSPC) {
3693 ztest_record_enospc(FTAG);
3696 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3699 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3701 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3702 error = dsl_dataset_promote(clone2name, NULL);
3703 if (error == ENOSPC) {
3704 dmu_objset_disown(os, FTAG);
3705 ztest_record_enospc(FTAG);
3709 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3711 dmu_objset_disown(os, FTAG);
3714 ztest_dsl_dataset_cleanup(osname, id);
3716 rw_exit(&ztest_name_lock);
3720 * Verify that dmu_object_{alloc,free} work as expected.
3723 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3726 int batchsize = sizeof (od) / sizeof (od[0]);
3728 for (int b = 0; b < batchsize; b++)
3729 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3732 * Destroy the previous batch of objects, create a new batch,
3733 * and do some I/O on the new objects.
3735 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3738 while (ztest_random(4 * batchsize) != 0)
3739 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3740 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3744 * Verify that dmu_{read,write} work as expected.
3747 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3749 objset_t *os = zd->zd_os;
3752 int i, freeit, error;
3754 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3755 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3756 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3757 uint64_t regions = 997;
3758 uint64_t stride = 123456789ULL;
3759 uint64_t width = 40;
3760 int free_percent = 5;
3763 * This test uses two objects, packobj and bigobj, that are always
3764 * updated together (i.e. in the same tx) so that their contents are
3765 * in sync and can be compared. Their contents relate to each other
3766 * in a simple way: packobj is a dense array of 'bufwad' structures,
3767 * while bigobj is a sparse array of the same bufwads. Specifically,
3768 * for any index n, there are three bufwads that should be identical:
3770 * packobj, at offset n * sizeof (bufwad_t)
3771 * bigobj, at the head of the nth chunk
3772 * bigobj, at the tail of the nth chunk
3774 * The chunk size is arbitrary. It doesn't have to be a power of two,
3775 * and it doesn't have any relation to the object blocksize.
3776 * The only requirement is that it can hold at least two bufwads.
3778 * Normally, we write the bufwad to each of these locations.
3779 * However, free_percent of the time we instead write zeroes to
3780 * packobj and perform a dmu_free_range() on bigobj. By comparing
3781 * bigobj to packobj, we can verify that the DMU is correctly
3782 * tracking which parts of an object are allocated and free,
3783 * and that the contents of the allocated blocks are correct.
3787 * Read the directory info. If it's the first time, set things up.
3789 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3790 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3792 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3795 bigobj = od[0].od_object;
3796 packobj = od[1].od_object;
3797 chunksize = od[0].od_gen;
3798 ASSERT(chunksize == od[1].od_gen);
3801 * Prefetch a random chunk of the big object.
3802 * Our aim here is to get some async reads in flight
3803 * for blocks that we may free below; the DMU should
3804 * handle this race correctly.
3806 n = ztest_random(regions) * stride + ztest_random(width);
3807 s = 1 + ztest_random(2 * width - 1);
3808 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3809 ZIO_PRIORITY_SYNC_READ);
3812 * Pick a random index and compute the offsets into packobj and bigobj.
3814 n = ztest_random(regions) * stride + ztest_random(width);
3815 s = 1 + ztest_random(width - 1);
3817 packoff = n * sizeof (bufwad_t);
3818 packsize = s * sizeof (bufwad_t);
3820 bigoff = n * chunksize;
3821 bigsize = s * chunksize;
3823 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3824 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3827 * free_percent of the time, free a range of bigobj rather than
3830 freeit = (ztest_random(100) < free_percent);
3833 * Read the current contents of our objects.
3835 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3838 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3843 * Get a tx for the mods to both packobj and bigobj.
3845 tx = dmu_tx_create(os);
3847 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3850 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3852 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3854 /* This accounts for setting the checksum/compression. */
3855 dmu_tx_hold_bonus(tx, bigobj);
3857 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3859 umem_free(packbuf, packsize);
3860 umem_free(bigbuf, bigsize);
3864 enum zio_checksum cksum;
3866 cksum = (enum zio_checksum)
3867 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3868 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3869 dmu_object_set_checksum(os, bigobj, cksum, tx);
3871 enum zio_compress comp;
3873 comp = (enum zio_compress)
3874 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3875 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3876 dmu_object_set_compress(os, bigobj, comp, tx);
3879 * For each index from n to n + s, verify that the existing bufwad
3880 * in packobj matches the bufwads at the head and tail of the
3881 * corresponding chunk in bigobj. Then update all three bufwads
3882 * with the new values we want to write out.
3884 for (i = 0; i < s; i++) {
3886 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3888 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3890 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3892 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3893 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3895 if (pack->bw_txg > txg)
3896 fatal(0, "future leak: got %llx, open txg is %llx",
3899 if (pack->bw_data != 0 && pack->bw_index != n + i)
3900 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3901 pack->bw_index, n, i);
3903 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3904 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3906 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3907 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3910 bzero(pack, sizeof (bufwad_t));
3912 pack->bw_index = n + i;
3914 pack->bw_data = 1 + ztest_random(-2ULL);
3921 * We've verified all the old bufwads, and made new ones.
3922 * Now write them out.
3924 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3927 if (ztest_opts.zo_verbose >= 7) {
3928 (void) printf("freeing offset %llx size %llx"
3930 (u_longlong_t)bigoff,
3931 (u_longlong_t)bigsize,
3934 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3936 if (ztest_opts.zo_verbose >= 7) {
3937 (void) printf("writing offset %llx size %llx"
3939 (u_longlong_t)bigoff,
3940 (u_longlong_t)bigsize,
3943 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3949 * Sanity check the stuff we just wrote.
3952 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3953 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3955 VERIFY(0 == dmu_read(os, packobj, packoff,
3956 packsize, packcheck, DMU_READ_PREFETCH));
3957 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3958 bigsize, bigcheck, DMU_READ_PREFETCH));
3960 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3961 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3963 umem_free(packcheck, packsize);
3964 umem_free(bigcheck, bigsize);
3967 umem_free(packbuf, packsize);
3968 umem_free(bigbuf, bigsize);
3972 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3973 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3981 * For each index from n to n + s, verify that the existing bufwad
3982 * in packobj matches the bufwads at the head and tail of the
3983 * corresponding chunk in bigobj. Then update all three bufwads
3984 * with the new values we want to write out.
3986 for (i = 0; i < s; i++) {
3988 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3990 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3992 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3994 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3995 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3997 if (pack->bw_txg > txg)
3998 fatal(0, "future leak: got %llx, open txg is %llx",
4001 if (pack->bw_data != 0 && pack->bw_index != n + i)
4002 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4003 pack->bw_index, n, i);
4005 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4006 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4008 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4009 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4011 pack->bw_index = n + i;
4013 pack->bw_data = 1 + ztest_random(-2ULL);
4021 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4023 objset_t *os = zd->zd_os;
4029 bufwad_t *packbuf, *bigbuf;
4030 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4031 uint64_t blocksize = ztest_random_blocksize();
4032 uint64_t chunksize = blocksize;
4033 uint64_t regions = 997;
4034 uint64_t stride = 123456789ULL;
4036 dmu_buf_t *bonus_db;
4037 arc_buf_t **bigbuf_arcbufs;
4038 dmu_object_info_t doi;
4041 * This test uses two objects, packobj and bigobj, that are always
4042 * updated together (i.e. in the same tx) so that their contents are
4043 * in sync and can be compared. Their contents relate to each other
4044 * in a simple way: packobj is a dense array of 'bufwad' structures,
4045 * while bigobj is a sparse array of the same bufwads. Specifically,
4046 * for any index n, there are three bufwads that should be identical:
4048 * packobj, at offset n * sizeof (bufwad_t)
4049 * bigobj, at the head of the nth chunk
4050 * bigobj, at the tail of the nth chunk
4052 * The chunk size is set equal to bigobj block size so that
4053 * dmu_assign_arcbuf() can be tested for object updates.
4057 * Read the directory info. If it's the first time, set things up.
4059 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4060 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
4062 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4065 bigobj = od[0].od_object;
4066 packobj = od[1].od_object;
4067 blocksize = od[0].od_blocksize;
4068 chunksize = blocksize;
4069 ASSERT(chunksize == od[1].od_gen);
4071 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4072 VERIFY(ISP2(doi.doi_data_block_size));
4073 VERIFY(chunksize == doi.doi_data_block_size);
4074 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4077 * Pick a random index and compute the offsets into packobj and bigobj.
4079 n = ztest_random(regions) * stride + ztest_random(width);
4080 s = 1 + ztest_random(width - 1);
4082 packoff = n * sizeof (bufwad_t);
4083 packsize = s * sizeof (bufwad_t);
4085 bigoff = n * chunksize;
4086 bigsize = s * chunksize;
4088 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4089 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4091 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4093 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4096 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4097 * Iteration 1 test zcopy to already referenced dbufs.
4098 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4099 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4100 * Iteration 4 test zcopy when dbuf is no longer dirty.
4101 * Iteration 5 test zcopy when it can't be done.
4102 * Iteration 6 one more zcopy write.
4104 for (i = 0; i < 7; i++) {
4109 * In iteration 5 (i == 5) use arcbufs
4110 * that don't match bigobj blksz to test
4111 * dmu_assign_arcbuf() when it can't directly
4112 * assign an arcbuf to a dbuf.
4114 for (j = 0; j < s; j++) {
4117 dmu_request_arcbuf(bonus_db, chunksize);
4119 bigbuf_arcbufs[2 * j] =
4120 dmu_request_arcbuf(bonus_db, chunksize / 2);
4121 bigbuf_arcbufs[2 * j + 1] =
4122 dmu_request_arcbuf(bonus_db, chunksize / 2);
4127 * Get a tx for the mods to both packobj and bigobj.
4129 tx = dmu_tx_create(os);
4131 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4132 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4134 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4136 umem_free(packbuf, packsize);
4137 umem_free(bigbuf, bigsize);
4138 for (j = 0; j < s; j++) {
4140 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4143 bigbuf_arcbufs[2 * j]);
4145 bigbuf_arcbufs[2 * j + 1]);
4148 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4149 dmu_buf_rele(bonus_db, FTAG);
4154 * 50% of the time don't read objects in the 1st iteration to
4155 * test dmu_assign_arcbuf() for the case when there're no
4156 * existing dbufs for the specified offsets.
4158 if (i != 0 || ztest_random(2) != 0) {
4159 error = dmu_read(os, packobj, packoff,
4160 packsize, packbuf, DMU_READ_PREFETCH);
4162 error = dmu_read(os, bigobj, bigoff, bigsize,
4163 bigbuf, DMU_READ_PREFETCH);
4166 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4170 * We've verified all the old bufwads, and made new ones.
4171 * Now write them out.
4173 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4174 if (ztest_opts.zo_verbose >= 7) {
4175 (void) printf("writing offset %llx size %llx"
4177 (u_longlong_t)bigoff,
4178 (u_longlong_t)bigsize,
4181 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4184 bcopy((caddr_t)bigbuf + (off - bigoff),
4185 bigbuf_arcbufs[j]->b_data, chunksize);
4187 bcopy((caddr_t)bigbuf + (off - bigoff),
4188 bigbuf_arcbufs[2 * j]->b_data,
4190 bcopy((caddr_t)bigbuf + (off - bigoff) +
4192 bigbuf_arcbufs[2 * j + 1]->b_data,
4197 VERIFY(dmu_buf_hold(os, bigobj, off,
4198 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4201 dmu_assign_arcbuf(bonus_db, off,
4202 bigbuf_arcbufs[j], tx);
4204 dmu_assign_arcbuf(bonus_db, off,
4205 bigbuf_arcbufs[2 * j], tx);
4206 dmu_assign_arcbuf(bonus_db,
4207 off + chunksize / 2,
4208 bigbuf_arcbufs[2 * j + 1], tx);
4211 dmu_buf_rele(dbt, FTAG);
4217 * Sanity check the stuff we just wrote.
4220 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4221 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4223 VERIFY(0 == dmu_read(os, packobj, packoff,
4224 packsize, packcheck, DMU_READ_PREFETCH));
4225 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4226 bigsize, bigcheck, DMU_READ_PREFETCH));
4228 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4229 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4231 umem_free(packcheck, packsize);
4232 umem_free(bigcheck, bigsize);
4235 txg_wait_open(dmu_objset_pool(os), 0);
4236 } else if (i == 3) {
4237 txg_wait_synced(dmu_objset_pool(os), 0);
4241 dmu_buf_rele(bonus_db, FTAG);
4242 umem_free(packbuf, packsize);
4243 umem_free(bigbuf, bigsize);
4244 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4249 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4252 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4253 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4256 * Have multiple threads write to large offsets in an object
4257 * to verify that parallel writes to an object -- even to the
4258 * same blocks within the object -- doesn't cause any trouble.
4260 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4262 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4265 while (ztest_random(10) != 0)
4266 ztest_io(zd, od[0].od_object, offset);
4270 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4273 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4274 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4275 uint64_t count = ztest_random(20) + 1;
4276 uint64_t blocksize = ztest_random_blocksize();
4279 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4281 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4284 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
4287 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
4289 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4291 while (ztest_random(count) != 0) {
4292 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4293 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
4296 while (ztest_random(4) != 0)
4297 ztest_io(zd, od[0].od_object, randoff);
4300 umem_free(data, blocksize);
4304 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4306 #define ZTEST_ZAP_MIN_INTS 1
4307 #define ZTEST_ZAP_MAX_INTS 4
4308 #define ZTEST_ZAP_MAX_PROPS 1000
4311 ztest_zap(ztest_ds_t *zd, uint64_t id)
4313 objset_t *os = zd->zd_os;
4316 uint64_t txg, last_txg;
4317 uint64_t value[ZTEST_ZAP_MAX_INTS];
4318 uint64_t zl_ints, zl_intsize, prop;
4321 char propname[100], txgname[100];
4323 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4325 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4327 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4330 object = od[0].od_object;
4333 * Generate a known hash collision, and verify that
4334 * we can lookup and remove both entries.
4336 tx = dmu_tx_create(os);
4337 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4338 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4341 for (i = 0; i < 2; i++) {
4343 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4346 for (i = 0; i < 2; i++) {
4347 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4348 sizeof (uint64_t), 1, &value[i], tx));
4350 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4351 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4352 ASSERT3U(zl_ints, ==, 1);
4354 for (i = 0; i < 2; i++) {
4355 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4360 * Generate a buch of random entries.
4362 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4364 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4365 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4366 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4367 bzero(value, sizeof (value));
4371 * If these zap entries already exist, validate their contents.
4373 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4375 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4376 ASSERT3U(zl_ints, ==, 1);
4378 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4379 zl_ints, &last_txg) == 0);
4381 VERIFY(zap_length(os, object, propname, &zl_intsize,
4384 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4385 ASSERT3U(zl_ints, ==, ints);
4387 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4388 zl_ints, value) == 0);
4390 for (i = 0; i < ints; i++) {
4391 ASSERT3U(value[i], ==, last_txg + object + i);
4394 ASSERT3U(error, ==, ENOENT);
4398 * Atomically update two entries in our zap object.
4399 * The first is named txg_%llu, and contains the txg
4400 * in which the property was last updated. The second
4401 * is named prop_%llu, and the nth element of its value
4402 * should be txg + object + n.
4404 tx = dmu_tx_create(os);
4405 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4406 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4411 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4413 for (i = 0; i < ints; i++)
4414 value[i] = txg + object + i;
4416 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4418 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4424 * Remove a random pair of entries.
4426 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4427 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4428 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4430 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4432 if (error == ENOENT)
4437 tx = dmu_tx_create(os);
4438 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4439 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4442 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4443 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4448 * Testcase to test the upgrading of a microzap to fatzap.
4451 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4453 objset_t *os = zd->zd_os;
4455 uint64_t object, txg;
4457 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4459 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
4462 object = od[0].od_object;
4465 * Add entries to this ZAP and make sure it spills over
4466 * and gets upgraded to a fatzap. Also, since we are adding
4467 * 2050 entries we should see ptrtbl growth and leaf-block split.
4469 for (int i = 0; i < 2050; i++) {
4470 char name[ZFS_MAX_DATASET_NAME_LEN];
4475 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4478 tx = dmu_tx_create(os);
4479 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4480 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4483 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4485 ASSERT(error == 0 || error == EEXIST);
4492 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4494 objset_t *os = zd->zd_os;
4496 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4498 int i, namelen, error;
4499 int micro = ztest_random(2);
4500 char name[20], string_value[20];
4503 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4505 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4508 object = od[0].od_object;
4511 * Generate a random name of the form 'xxx.....' where each
4512 * x is a random printable character and the dots are dots.
4513 * There are 94 such characters, and the name length goes from
4514 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4516 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4518 for (i = 0; i < 3; i++)
4519 name[i] = '!' + ztest_random('~' - '!' + 1);
4520 for (; i < namelen - 1; i++)
4524 if ((namelen & 1) || micro) {
4525 wsize = sizeof (txg);
4531 data = string_value;
4535 VERIFY0(zap_count(os, object, &count));
4536 ASSERT(count != -1ULL);
4539 * Select an operation: length, lookup, add, update, remove.
4541 i = ztest_random(5);
4544 tx = dmu_tx_create(os);
4545 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4546 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4549 bcopy(name, string_value, namelen);
4553 bzero(string_value, namelen);
4559 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4561 ASSERT3U(wsize, ==, zl_wsize);
4562 ASSERT3U(wc, ==, zl_wc);
4564 ASSERT3U(error, ==, ENOENT);
4569 error = zap_lookup(os, object, name, wsize, wc, data);
4571 if (data == string_value &&
4572 bcmp(name, data, namelen) != 0)
4573 fatal(0, "name '%s' != val '%s' len %d",
4574 name, data, namelen);
4576 ASSERT3U(error, ==, ENOENT);
4581 error = zap_add(os, object, name, wsize, wc, data, tx);
4582 ASSERT(error == 0 || error == EEXIST);
4586 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4590 error = zap_remove(os, object, name, tx);
4591 ASSERT(error == 0 || error == ENOENT);
4600 * Commit callback data.
4602 typedef struct ztest_cb_data {
4603 list_node_t zcd_node;
4605 int zcd_expected_err;
4606 boolean_t zcd_added;
4607 boolean_t zcd_called;
4611 /* This is the actual commit callback function */
4613 ztest_commit_callback(void *arg, int error)
4615 ztest_cb_data_t *data = arg;
4616 uint64_t synced_txg;
4618 VERIFY(data != NULL);
4619 VERIFY3S(data->zcd_expected_err, ==, error);
4620 VERIFY(!data->zcd_called);
4622 synced_txg = spa_last_synced_txg(data->zcd_spa);
4623 if (data->zcd_txg > synced_txg)
4624 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4625 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4628 data->zcd_called = B_TRUE;
4630 if (error == ECANCELED) {
4631 ASSERT0(data->zcd_txg);
4632 ASSERT(!data->zcd_added);
4635 * The private callback data should be destroyed here, but
4636 * since we are going to check the zcd_called field after
4637 * dmu_tx_abort(), we will destroy it there.
4642 /* Was this callback added to the global callback list? */
4643 if (!data->zcd_added)
4646 ASSERT3U(data->zcd_txg, !=, 0);
4648 /* Remove our callback from the list */
4649 mutex_enter(&zcl.zcl_callbacks_lock);
4650 list_remove(&zcl.zcl_callbacks, data);
4651 mutex_exit(&zcl.zcl_callbacks_lock);
4654 umem_free(data, sizeof (ztest_cb_data_t));
4657 /* Allocate and initialize callback data structure */
4658 static ztest_cb_data_t *
4659 ztest_create_cb_data(objset_t *os, uint64_t txg)
4661 ztest_cb_data_t *cb_data;
4663 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4665 cb_data->zcd_txg = txg;
4666 cb_data->zcd_spa = dmu_objset_spa(os);
4672 * If a number of txgs equal to this threshold have been created after a commit
4673 * callback has been registered but not called, then we assume there is an
4674 * implementation bug.
4676 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4679 * Commit callback test.
4682 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4684 objset_t *os = zd->zd_os;
4687 ztest_cb_data_t *cb_data[3], *tmp_cb;
4688 uint64_t old_txg, txg;
4691 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4693 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4696 tx = dmu_tx_create(os);
4698 cb_data[0] = ztest_create_cb_data(os, 0);
4699 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4701 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4703 /* Every once in a while, abort the transaction on purpose */
4704 if (ztest_random(100) == 0)
4708 error = dmu_tx_assign(tx, TXG_NOWAIT);
4710 txg = error ? 0 : dmu_tx_get_txg(tx);
4712 cb_data[0]->zcd_txg = txg;
4713 cb_data[1] = ztest_create_cb_data(os, txg);
4714 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4718 * It's not a strict requirement to call the registered
4719 * callbacks from inside dmu_tx_abort(), but that's what
4720 * it's supposed to happen in the current implementation
4721 * so we will check for that.
4723 for (i = 0; i < 2; i++) {
4724 cb_data[i]->zcd_expected_err = ECANCELED;
4725 VERIFY(!cb_data[i]->zcd_called);
4730 for (i = 0; i < 2; i++) {
4731 VERIFY(cb_data[i]->zcd_called);
4732 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4738 cb_data[2] = ztest_create_cb_data(os, txg);
4739 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4742 * Read existing data to make sure there isn't a future leak.
4744 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4745 &old_txg, DMU_READ_PREFETCH));
4748 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4751 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4753 mutex_enter(&zcl.zcl_callbacks_lock);
4756 * Since commit callbacks don't have any ordering requirement and since
4757 * it is theoretically possible for a commit callback to be called
4758 * after an arbitrary amount of time has elapsed since its txg has been
4759 * synced, it is difficult to reliably determine whether a commit
4760 * callback hasn't been called due to high load or due to a flawed
4763 * In practice, we will assume that if after a certain number of txgs a
4764 * commit callback hasn't been called, then most likely there's an
4765 * implementation bug..
4767 tmp_cb = list_head(&zcl.zcl_callbacks);
4768 if (tmp_cb != NULL &&
4769 (txg - ZTEST_COMMIT_CALLBACK_THRESH) > tmp_cb->zcd_txg) {
4770 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4771 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4775 * Let's find the place to insert our callbacks.
4777 * Even though the list is ordered by txg, it is possible for the
4778 * insertion point to not be the end because our txg may already be
4779 * quiescing at this point and other callbacks in the open txg
4780 * (from other objsets) may have sneaked in.
4782 tmp_cb = list_tail(&zcl.zcl_callbacks);
4783 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4784 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4786 /* Add the 3 callbacks to the list */
4787 for (i = 0; i < 3; i++) {
4789 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4791 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4794 cb_data[i]->zcd_added = B_TRUE;
4795 VERIFY(!cb_data[i]->zcd_called);
4797 tmp_cb = cb_data[i];
4800 mutex_exit(&zcl.zcl_callbacks_lock);
4807 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4809 zfs_prop_t proplist[] = {
4811 ZFS_PROP_COMPRESSION,
4816 rw_enter(&ztest_name_lock, RW_READER);
4818 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4819 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4820 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4822 rw_exit(&ztest_name_lock);
4827 ztest_remap_blocks(ztest_ds_t *zd, uint64_t id)
4829 rw_enter(&ztest_name_lock, RW_READER);
4831 int error = dmu_objset_remap_indirects(zd->zd_name);
4832 if (error == ENOSPC)
4836 rw_exit(&ztest_name_lock);
4841 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4843 nvlist_t *props = NULL;
4845 rw_enter(&ztest_name_lock, RW_READER);
4847 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4848 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4850 VERIFY0(spa_prop_get(ztest_spa, &props));
4852 if (ztest_opts.zo_verbose >= 6)
4853 dump_nvlist(props, 4);
4857 rw_exit(&ztest_name_lock);
4861 user_release_one(const char *snapname, const char *holdname)
4863 nvlist_t *snaps, *holds;
4866 snaps = fnvlist_alloc();
4867 holds = fnvlist_alloc();
4868 fnvlist_add_boolean(holds, holdname);
4869 fnvlist_add_nvlist(snaps, snapname, holds);
4870 fnvlist_free(holds);
4871 error = dsl_dataset_user_release(snaps, NULL);
4872 fnvlist_free(snaps);
4877 * Test snapshot hold/release and deferred destroy.
4880 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4883 objset_t *os = zd->zd_os;
4887 char clonename[100];
4889 char osname[ZFS_MAX_DATASET_NAME_LEN];
4892 rw_enter(&ztest_name_lock, RW_READER);
4894 dmu_objset_name(os, osname);
4896 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu", id);
4897 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4898 (void) snprintf(clonename, sizeof (clonename),
4899 "%s/ch1_%llu", osname, id);
4900 (void) snprintf(tag, sizeof (tag), "tag_%llu", id);
4903 * Clean up from any previous run.
4905 error = dsl_destroy_head(clonename);
4906 if (error != ENOENT)
4908 error = user_release_one(fullname, tag);
4909 if (error != ESRCH && error != ENOENT)
4911 error = dsl_destroy_snapshot(fullname, B_FALSE);
4912 if (error != ENOENT)
4916 * Create snapshot, clone it, mark snap for deferred destroy,
4917 * destroy clone, verify snap was also destroyed.
4919 error = dmu_objset_snapshot_one(osname, snapname);
4921 if (error == ENOSPC) {
4922 ztest_record_enospc("dmu_objset_snapshot");
4925 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4928 error = dmu_objset_clone(clonename, fullname);
4930 if (error == ENOSPC) {
4931 ztest_record_enospc("dmu_objset_clone");
4934 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4937 error = dsl_destroy_snapshot(fullname, B_TRUE);
4939 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4943 error = dsl_destroy_head(clonename);
4945 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4947 error = dmu_objset_hold(fullname, FTAG, &origin);
4948 if (error != ENOENT)
4949 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4952 * Create snapshot, add temporary hold, verify that we can't
4953 * destroy a held snapshot, mark for deferred destroy,
4954 * release hold, verify snapshot was destroyed.
4956 error = dmu_objset_snapshot_one(osname, snapname);
4958 if (error == ENOSPC) {
4959 ztest_record_enospc("dmu_objset_snapshot");
4962 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4965 holds = fnvlist_alloc();
4966 fnvlist_add_string(holds, fullname, tag);
4967 error = dsl_dataset_user_hold(holds, 0, NULL);
4968 fnvlist_free(holds);
4970 if (error == ENOSPC) {
4971 ztest_record_enospc("dsl_dataset_user_hold");
4974 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
4975 fullname, tag, error);
4978 error = dsl_destroy_snapshot(fullname, B_FALSE);
4979 if (error != EBUSY) {
4980 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4984 error = dsl_destroy_snapshot(fullname, B_TRUE);
4986 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4990 error = user_release_one(fullname, tag);
4992 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4994 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4997 rw_exit(&ztest_name_lock);
5001 * Inject random faults into the on-disk data.
5005 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5007 ztest_shared_t *zs = ztest_shared;
5008 spa_t *spa = ztest_spa;
5012 uint64_t bad = 0x1990c0ffeedecadeULL;
5014 char path0[MAXPATHLEN];
5015 char pathrand[MAXPATHLEN];
5017 int bshift = SPA_MAXBLOCKSHIFT + 2;
5023 boolean_t islog = B_FALSE;
5025 mutex_enter(&ztest_vdev_lock);
5028 * Device removal is in progress, fault injection must be disabled
5029 * until it completes and the pool is scrubbed. The fault injection
5030 * strategy for damaging blocks does not take in to account evacuated
5031 * blocks which may have already been damaged.
5033 if (ztest_device_removal_active) {
5034 mutex_exit(&ztest_vdev_lock);
5038 maxfaults = MAXFAULTS();
5039 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5040 mirror_save = zs->zs_mirrors;
5041 mutex_exit(&ztest_vdev_lock);
5043 ASSERT(leaves >= 1);
5046 * Grab the name lock as reader. There are some operations
5047 * which don't like to have their vdevs changed while
5048 * they are in progress (i.e. spa_change_guid). Those
5049 * operations will have grabbed the name lock as writer.
5051 rw_enter(&ztest_name_lock, RW_READER);
5054 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5056 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5058 if (ztest_random(2) == 0) {
5060 * Inject errors on a normal data device or slog device.
5062 top = ztest_random_vdev_top(spa, B_TRUE);
5063 leaf = ztest_random(leaves) + zs->zs_splits;
5066 * Generate paths to the first leaf in this top-level vdev,
5067 * and to the random leaf we selected. We'll induce transient
5068 * write failures and random online/offline activity on leaf 0,
5069 * and we'll write random garbage to the randomly chosen leaf.
5071 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
5072 ztest_opts.zo_dir, ztest_opts.zo_pool,
5073 top * leaves + zs->zs_splits);
5074 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
5075 ztest_opts.zo_dir, ztest_opts.zo_pool,
5076 top * leaves + leaf);
5078 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5079 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5083 * If the top-level vdev needs to be resilvered
5084 * then we only allow faults on the device that is
5087 if (vd0 != NULL && maxfaults != 1 &&
5088 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5089 vd0->vdev_resilver_txg != 0)) {
5091 * Make vd0 explicitly claim to be unreadable,
5092 * or unwriteable, or reach behind its back
5093 * and close the underlying fd. We can do this if
5094 * maxfaults == 0 because we'll fail and reexecute,
5095 * and we can do it if maxfaults >= 2 because we'll
5096 * have enough redundancy. If maxfaults == 1, the
5097 * combination of this with injection of random data
5098 * corruption below exceeds the pool's fault tolerance.
5100 vdev_file_t *vf = vd0->vdev_tsd;
5102 zfs_dbgmsg("injecting fault to vdev %llu; maxfaults=%d",
5103 (long long)vd0->vdev_id, (int)maxfaults);
5105 if (vf != NULL && ztest_random(3) == 0) {
5106 (void) close(vf->vf_vnode->v_fd);
5107 vf->vf_vnode->v_fd = -1;
5108 } else if (ztest_random(2) == 0) {
5109 vd0->vdev_cant_read = B_TRUE;
5111 vd0->vdev_cant_write = B_TRUE;
5113 guid0 = vd0->vdev_guid;
5117 * Inject errors on an l2cache device.
5119 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5121 if (sav->sav_count == 0) {
5122 spa_config_exit(spa, SCL_STATE, FTAG);
5123 rw_exit(&ztest_name_lock);
5126 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5127 guid0 = vd0->vdev_guid;
5128 (void) strcpy(path0, vd0->vdev_path);
5129 (void) strcpy(pathrand, vd0->vdev_path);
5133 maxfaults = INT_MAX; /* no limit on cache devices */
5136 spa_config_exit(spa, SCL_STATE, FTAG);
5137 rw_exit(&ztest_name_lock);
5140 * If we can tolerate two or more faults, or we're dealing
5141 * with a slog, randomly online/offline vd0.
5143 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5144 if (ztest_random(10) < 6) {
5145 int flags = (ztest_random(2) == 0 ?
5146 ZFS_OFFLINE_TEMPORARY : 0);
5149 * We have to grab the zs_name_lock as writer to
5150 * prevent a race between offlining a slog and
5151 * destroying a dataset. Offlining the slog will
5152 * grab a reference on the dataset which may cause
5153 * dmu_objset_destroy() to fail with EBUSY thus
5154 * leaving the dataset in an inconsistent state.
5157 rw_enter(&ztest_name_lock, RW_WRITER);
5159 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5162 rw_exit(&ztest_name_lock);
5165 * Ideally we would like to be able to randomly
5166 * call vdev_[on|off]line without holding locks
5167 * to force unpredictable failures but the side
5168 * effects of vdev_[on|off]line prevent us from
5169 * doing so. We grab the ztest_vdev_lock here to
5170 * prevent a race between injection testing and
5173 mutex_enter(&ztest_vdev_lock);
5174 (void) vdev_online(spa, guid0, 0, NULL);
5175 mutex_exit(&ztest_vdev_lock);
5183 * We have at least single-fault tolerance, so inject data corruption.
5185 fd = open(pathrand, O_RDWR);
5187 if (fd == -1) /* we hit a gap in the device namespace */
5190 fsize = lseek(fd, 0, SEEK_END);
5192 while (--iters != 0) {
5194 * The offset must be chosen carefully to ensure that
5195 * we do not inject a given logical block with errors
5196 * on two different leaf devices, because ZFS can not
5197 * tolerate that (if maxfaults==1).
5199 * We divide each leaf into chunks of size
5200 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5201 * there is a series of ranges to which we can inject errors.
5202 * Each range can accept errors on only a single leaf vdev.
5203 * The error injection ranges are separated by ranges
5204 * which we will not inject errors on any device (DMZs).
5205 * Each DMZ must be large enough such that a single block
5206 * can not straddle it, so that a single block can not be
5207 * a target in two different injection ranges (on different
5210 * For example, with 3 leaves, each chunk looks like:
5211 * 0 to 32M: injection range for leaf 0
5212 * 32M to 64M: DMZ - no injection allowed
5213 * 64M to 96M: injection range for leaf 1
5214 * 96M to 128M: DMZ - no injection allowed
5215 * 128M to 160M: injection range for leaf 2
5216 * 160M to 192M: DMZ - no injection allowed
5218 offset = ztest_random(fsize / (leaves << bshift)) *
5219 (leaves << bshift) + (leaf << bshift) +
5220 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5223 * Only allow damage to the labels at one end of the vdev.
5225 * If all labels are damaged, the device will be totally
5226 * inaccessible, which will result in loss of data,
5227 * because we also damage (parts of) the other side of
5230 * Additionally, we will always have both an even and an
5231 * odd label, so that we can handle crashes in the
5232 * middle of vdev_config_sync().
5234 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5238 * The two end labels are stored at the "end" of the disk, but
5239 * the end of the disk (vdev_psize) is aligned to
5240 * sizeof (vdev_label_t).
5242 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5243 if ((leaf & 1) == 1 &&
5244 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5247 mutex_enter(&ztest_vdev_lock);
5248 if (mirror_save != zs->zs_mirrors) {
5249 mutex_exit(&ztest_vdev_lock);
5254 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5255 fatal(1, "can't inject bad word at 0x%llx in %s",
5258 mutex_exit(&ztest_vdev_lock);
5260 if (ztest_opts.zo_verbose >= 7)
5261 (void) printf("injected bad word into %s,"
5262 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5269 * Verify that DDT repair works as expected.
5272 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5274 ztest_shared_t *zs = ztest_shared;
5275 spa_t *spa = ztest_spa;
5276 objset_t *os = zd->zd_os;
5278 uint64_t object, blocksize, txg, pattern, psize;
5279 enum zio_checksum checksum = spa_dedup_checksum(spa);
5284 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5286 blocksize = ztest_random_blocksize();
5287 blocksize = MIN(blocksize, 2048); /* because we write so many */
5289 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5291 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
5295 * Take the name lock as writer to prevent anyone else from changing
5296 * the pool and dataset properies we need to maintain during this test.
5298 rw_enter(&ztest_name_lock, RW_WRITER);
5300 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5302 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5304 rw_exit(&ztest_name_lock);
5308 dmu_objset_stats_t dds;
5309 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5310 dmu_objset_fast_stat(os, &dds);
5311 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5313 object = od[0].od_object;
5314 blocksize = od[0].od_blocksize;
5315 pattern = zs->zs_guid ^ dds.dds_guid;
5317 ASSERT(object != 0);
5319 tx = dmu_tx_create(os);
5320 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5321 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5323 rw_exit(&ztest_name_lock);
5328 * Write all the copies of our block.
5330 for (int i = 0; i < copies; i++) {
5331 uint64_t offset = i * blocksize;
5332 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5333 DMU_READ_NO_PREFETCH);
5335 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5336 os, (long long)object, (long long) offset, error);
5338 ASSERT(db->db_offset == offset);
5339 ASSERT(db->db_size == blocksize);
5340 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5341 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5342 dmu_buf_will_fill(db, tx);
5343 ztest_pattern_set(db->db_data, db->db_size, pattern);
5344 dmu_buf_rele(db, FTAG);
5348 txg_wait_synced(spa_get_dsl(spa), txg);
5351 * Find out what block we got.
5353 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5354 DMU_READ_NO_PREFETCH));
5355 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5356 dmu_buf_rele(db, FTAG);
5359 * Damage the block. Dedup-ditto will save us when we read it later.
5361 psize = BP_GET_PSIZE(&blk);
5362 abd = abd_alloc_linear(psize, B_TRUE);
5363 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5365 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5366 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5367 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5371 rw_exit(&ztest_name_lock);
5379 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5381 spa_t *spa = ztest_spa;
5384 * Scrub in progress by device removal.
5386 if (ztest_device_removal_active)
5389 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5390 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5391 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5395 * Change the guid for the pool.
5399 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5401 spa_t *spa = ztest_spa;
5402 uint64_t orig, load;
5405 orig = spa_guid(spa);
5406 load = spa_load_guid(spa);
5408 rw_enter(&ztest_name_lock, RW_WRITER);
5409 error = spa_change_guid(spa);
5410 rw_exit(&ztest_name_lock);
5415 if (ztest_opts.zo_verbose >= 4) {
5416 (void) printf("Changed guid old %llu -> %llu\n",
5417 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5420 VERIFY3U(orig, !=, spa_guid(spa));
5421 VERIFY3U(load, ==, spa_load_guid(spa));
5425 * Rename the pool to a different name and then rename it back.
5429 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5431 char *oldname, *newname;
5434 rw_enter(&ztest_name_lock, RW_WRITER);
5436 oldname = ztest_opts.zo_pool;
5437 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5438 (void) strcpy(newname, oldname);
5439 (void) strcat(newname, "_tmp");
5444 VERIFY3U(0, ==, spa_rename(oldname, newname));
5447 * Try to open it under the old name, which shouldn't exist
5449 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5452 * Open it under the new name and make sure it's still the same spa_t.
5454 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5456 ASSERT(spa == ztest_spa);
5457 spa_close(spa, FTAG);
5460 * Rename it back to the original
5462 VERIFY3U(0, ==, spa_rename(newname, oldname));
5465 * Make sure it can still be opened
5467 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5469 ASSERT(spa == ztest_spa);
5470 spa_close(spa, FTAG);
5472 umem_free(newname, strlen(newname) + 1);
5474 rw_exit(&ztest_name_lock);
5478 ztest_random_concrete_vdev_leaf(vdev_t *vd)
5483 if (vd->vdev_children == 0)
5486 vdev_t *eligible[vd->vdev_children];
5487 int eligible_idx = 0, i;
5488 for (i = 0; i < vd->vdev_children; i++) {
5489 vdev_t *cvd = vd->vdev_child[i];
5490 if (cvd->vdev_top->vdev_removing)
5492 if (cvd->vdev_children > 0 ||
5493 (vdev_is_concrete(cvd) && !cvd->vdev_detached)) {
5494 eligible[eligible_idx++] = cvd;
5497 VERIFY(eligible_idx > 0);
5499 uint64_t child_no = ztest_random(eligible_idx);
5500 return (ztest_random_concrete_vdev_leaf(eligible[child_no]));
5505 ztest_initialize(ztest_ds_t *zd, uint64_t id)
5507 spa_t *spa = ztest_spa;
5510 mutex_enter(&ztest_vdev_lock);
5512 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
5514 /* Random leaf vdev */
5515 vdev_t *rand_vd = ztest_random_concrete_vdev_leaf(spa->spa_root_vdev);
5516 if (rand_vd == NULL) {
5517 spa_config_exit(spa, SCL_VDEV, FTAG);
5518 mutex_exit(&ztest_vdev_lock);
5523 * The random vdev we've selected may change as soon as we
5524 * drop the spa_config_lock. We create local copies of things
5525 * we're interested in.
5527 uint64_t guid = rand_vd->vdev_guid;
5528 char *path = strdup(rand_vd->vdev_path);
5529 boolean_t active = rand_vd->vdev_initialize_thread != NULL;
5531 zfs_dbgmsg("vd %p, guid %llu", rand_vd, guid);
5532 spa_config_exit(spa, SCL_VDEV, FTAG);
5534 uint64_t cmd = ztest_random(POOL_INITIALIZE_FUNCS);
5535 error = spa_vdev_initialize(spa, guid, cmd);
5537 case POOL_INITIALIZE_CANCEL:
5538 if (ztest_opts.zo_verbose >= 4) {
5539 (void) printf("Cancel initialize %s", path);
5541 (void) printf(" failed (no initialize active)");
5542 (void) printf("\n");
5545 case POOL_INITIALIZE_DO:
5546 if (ztest_opts.zo_verbose >= 4) {
5547 (void) printf("Start initialize %s", path);
5548 if (active && error == 0)
5549 (void) printf(" failed (already active)");
5550 else if (error != 0)
5551 (void) printf(" failed (error %d)", error);
5552 (void) printf("\n");
5555 case POOL_INITIALIZE_SUSPEND:
5556 if (ztest_opts.zo_verbose >= 4) {
5557 (void) printf("Suspend initialize %s", path);
5559 (void) printf(" failed (no initialize active)");
5560 (void) printf("\n");
5565 mutex_exit(&ztest_vdev_lock);
5569 * Verify pool integrity by running zdb.
5572 ztest_run_zdb(char *pool)
5575 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
5583 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
5585 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
5586 bin = strstr(zdb, "/usr/bin/");
5587 ztest = strstr(bin, "/ztest");
5589 isalen = ztest - isa;
5593 "/usr/sbin%.*s/zdb -bcc%s%s -G -d -U %s %s",
5596 ztest_opts.zo_verbose >= 3 ? "s" : "",
5597 ztest_opts.zo_verbose >= 4 ? "v" : "",
5602 if (ztest_opts.zo_verbose >= 5)
5603 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5605 fp = popen(zdb, "r");
5608 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5609 if (ztest_opts.zo_verbose >= 3)
5610 (void) printf("%s", zbuf);
5612 status = pclose(fp);
5617 ztest_dump_core = 0;
5618 if (WIFEXITED(status))
5619 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5621 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5625 ztest_walk_pool_directory(char *header)
5629 if (ztest_opts.zo_verbose >= 6)
5630 (void) printf("%s\n", header);
5632 mutex_enter(&spa_namespace_lock);
5633 while ((spa = spa_next(spa)) != NULL)
5634 if (ztest_opts.zo_verbose >= 6)
5635 (void) printf("\t%s\n", spa_name(spa));
5636 mutex_exit(&spa_namespace_lock);
5640 ztest_spa_import_export(char *oldname, char *newname)
5642 nvlist_t *config, *newconfig;
5647 if (ztest_opts.zo_verbose >= 4) {
5648 (void) printf("import/export: old = %s, new = %s\n",
5653 * Clean up from previous runs.
5655 (void) spa_destroy(newname);
5658 * Get the pool's configuration and guid.
5660 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5663 * Kick off a scrub to tickle scrub/export races.
5665 if (ztest_random(2) == 0)
5666 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5668 pool_guid = spa_guid(spa);
5669 spa_close(spa, FTAG);
5671 ztest_walk_pool_directory("pools before export");
5676 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5678 ztest_walk_pool_directory("pools after export");
5683 newconfig = spa_tryimport(config);
5684 ASSERT(newconfig != NULL);
5685 nvlist_free(newconfig);
5688 * Import it under the new name.
5690 error = spa_import(newname, config, NULL, 0);
5692 dump_nvlist(config, 0);
5693 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5694 oldname, newname, error);
5697 ztest_walk_pool_directory("pools after import");
5700 * Try to import it again -- should fail with EEXIST.
5702 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5705 * Try to import it under a different name -- should fail with EEXIST.
5707 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5710 * Verify that the pool is no longer visible under the old name.
5712 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5715 * Verify that we can open and close the pool using the new name.
5717 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5718 ASSERT(pool_guid == spa_guid(spa));
5719 spa_close(spa, FTAG);
5721 nvlist_free(config);
5725 ztest_resume(spa_t *spa)
5727 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5728 (void) printf("resuming from suspended state\n");
5729 spa_vdev_state_enter(spa, SCL_NONE);
5730 vdev_clear(spa, NULL);
5731 (void) spa_vdev_state_exit(spa, NULL, 0);
5732 (void) zio_resume(spa);
5736 ztest_resume_thread(void *arg)
5740 while (!ztest_exiting) {
5741 if (spa_suspended(spa))
5743 (void) poll(NULL, 0, 100);
5746 * Periodically change the zfs_compressed_arc_enabled setting.
5748 if (ztest_random(10) == 0)
5749 zfs_compressed_arc_enabled = ztest_random(2);
5752 * Periodically change the zfs_abd_scatter_enabled setting.
5754 if (ztest_random(10) == 0)
5755 zfs_abd_scatter_enabled = ztest_random(2);
5761 ztest_deadman_thread(void *arg)
5763 ztest_shared_t *zs = arg;
5764 spa_t *spa = ztest_spa;
5765 hrtime_t delta, total = 0;
5768 delta = zs->zs_thread_stop - zs->zs_thread_start +
5769 MSEC2NSEC(zfs_deadman_synctime_ms);
5771 (void) poll(NULL, 0, (int)NSEC2MSEC(delta));
5774 * If the pool is suspended then fail immediately. Otherwise,
5775 * check to see if the pool is making any progress. If
5776 * vdev_deadman() discovers that there hasn't been any recent
5777 * I/Os then it will end up aborting the tests.
5779 if (spa_suspended(spa) || spa->spa_root_vdev == NULL) {
5780 fatal(0, "aborting test after %llu seconds because "
5781 "pool has transitioned to a suspended state.",
5782 zfs_deadman_synctime_ms / 1000);
5785 vdev_deadman(spa->spa_root_vdev);
5787 total += zfs_deadman_synctime_ms/1000;
5788 (void) printf("ztest has been running for %lld seconds\n",
5794 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5796 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5797 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5798 hrtime_t functime = gethrtime();
5800 for (int i = 0; i < zi->zi_iters; i++)
5801 zi->zi_func(zd, id);
5803 functime = gethrtime() - functime;
5805 atomic_add_64(&zc->zc_count, 1);
5806 atomic_add_64(&zc->zc_time, functime);
5808 if (ztest_opts.zo_verbose >= 4) {
5810 (void) dladdr((void *)zi->zi_func, &dli);
5811 (void) printf("%6.2f sec in %s\n",
5812 (double)functime / NANOSEC, dli.dli_sname);
5817 ztest_thread(void *arg)
5820 uint64_t id = (uintptr_t)arg;
5821 ztest_shared_t *zs = ztest_shared;
5825 ztest_shared_callstate_t *zc;
5827 while ((now = gethrtime()) < zs->zs_thread_stop) {
5829 * See if it's time to force a crash.
5831 if (now > zs->zs_thread_kill)
5835 * If we're getting ENOSPC with some regularity, stop.
5837 if (zs->zs_enospc_count > 10)
5841 * Pick a random function to execute.
5843 rand = ztest_random(ZTEST_FUNCS);
5844 zi = &ztest_info[rand];
5845 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5846 call_next = zc->zc_next;
5848 if (now >= call_next &&
5849 atomic_cas_64(&zc->zc_next, call_next, call_next +
5850 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5851 ztest_execute(rand, zi, id);
5859 ztest_dataset_name(char *dsname, char *pool, int d)
5861 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
5865 ztest_dataset_destroy(int d)
5867 char name[ZFS_MAX_DATASET_NAME_LEN];
5869 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5871 if (ztest_opts.zo_verbose >= 3)
5872 (void) printf("Destroying %s to free up space\n", name);
5875 * Cleanup any non-standard clones and snapshots. In general,
5876 * ztest thread t operates on dataset (t % zopt_datasets),
5877 * so there may be more than one thing to clean up.
5879 for (int t = d; t < ztest_opts.zo_threads;
5880 t += ztest_opts.zo_datasets) {
5881 ztest_dsl_dataset_cleanup(name, t);
5884 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5885 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5889 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5891 uint64_t usedobjs, dirobjs, scratch;
5894 * ZTEST_DIROBJ is the object directory for the entire dataset.
5895 * Therefore, the number of objects in use should equal the
5896 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5897 * If not, we have an object leak.
5899 * Note that we can only check this in ztest_dataset_open(),
5900 * when the open-context and syncing-context values agree.
5901 * That's because zap_count() returns the open-context value,
5902 * while dmu_objset_space() returns the rootbp fill count.
5904 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5905 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5906 ASSERT3U(dirobjs + 1, ==, usedobjs);
5910 ztest_dataset_open(int d)
5912 ztest_ds_t *zd = &ztest_ds[d];
5913 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5916 char name[ZFS_MAX_DATASET_NAME_LEN];
5919 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5921 rw_enter(&ztest_name_lock, RW_READER);
5923 error = ztest_dataset_create(name);
5924 if (error == ENOSPC) {
5925 rw_exit(&ztest_name_lock);
5926 ztest_record_enospc(FTAG);
5929 ASSERT(error == 0 || error == EEXIST);
5931 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5932 rw_exit(&ztest_name_lock);
5934 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5936 zilog = zd->zd_zilog;
5938 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5939 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5940 fatal(0, "missing log records: claimed %llu < committed %llu",
5941 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5943 ztest_dataset_dirobj_verify(zd);
5945 zil_replay(os, zd, ztest_replay_vector);
5947 ztest_dataset_dirobj_verify(zd);
5949 if (ztest_opts.zo_verbose >= 6)
5950 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5952 (u_longlong_t)zilog->zl_parse_blk_count,
5953 (u_longlong_t)zilog->zl_parse_lr_count,
5954 (u_longlong_t)zilog->zl_replaying_seq);
5956 zilog = zil_open(os, ztest_get_data);
5958 if (zilog->zl_replaying_seq != 0 &&
5959 zilog->zl_replaying_seq < committed_seq)
5960 fatal(0, "missing log records: replayed %llu < committed %llu",
5961 zilog->zl_replaying_seq, committed_seq);
5967 ztest_dataset_close(int d)
5969 ztest_ds_t *zd = &ztest_ds[d];
5971 zil_close(zd->zd_zilog);
5972 dmu_objset_disown(zd->zd_os, zd);
5978 * Kick off threads to run tests on all datasets in parallel.
5981 ztest_run(ztest_shared_t *zs)
5986 thread_t resume_tid;
5989 ztest_exiting = B_FALSE;
5992 * Initialize parent/child shared state.
5994 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
5995 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
5996 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
5998 zs->zs_thread_start = gethrtime();
5999 zs->zs_thread_stop =
6000 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6001 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6002 zs->zs_thread_kill = zs->zs_thread_stop;
6003 if (ztest_random(100) < ztest_opts.zo_killrate) {
6004 zs->zs_thread_kill -=
6005 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6008 mutex_init(&zcl.zcl_callbacks_lock, NULL, USYNC_THREAD, NULL);
6010 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6011 offsetof(ztest_cb_data_t, zcd_node));
6016 kernel_init(FREAD | FWRITE);
6017 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6018 metaslab_preload_limit = ztest_random(20) + 1;
6021 dmu_objset_stats_t dds;
6022 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6023 DMU_OST_ANY, B_TRUE, FTAG, &os));
6024 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6025 dmu_objset_fast_stat(os, &dds);
6026 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6027 zs->zs_guid = dds.dds_guid;
6028 dmu_objset_disown(os, FTAG);
6030 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6033 * We don't expect the pool to suspend unless maxfaults == 0,
6034 * in which case ztest_fault_inject() temporarily takes away
6035 * the only valid replica.
6037 if (MAXFAULTS() == 0)
6038 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6040 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6043 * Create a thread to periodically resume suspended I/O.
6045 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
6049 * Create a deadman thread to abort() if we hang.
6051 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
6055 * Verify that we can safely inquire about any object,
6056 * whether it's allocated or not. To make it interesting,
6057 * we probe a 5-wide window around each power of two.
6058 * This hits all edge cases, including zero and the max.
6060 for (int t = 0; t < 64; t++) {
6061 for (int d = -5; d <= 5; d++) {
6062 error = dmu_object_info(spa->spa_meta_objset,
6063 (1ULL << t) + d, NULL);
6064 ASSERT(error == 0 || error == ENOENT ||
6070 * If we got any ENOSPC errors on the previous run, destroy something.
6072 if (zs->zs_enospc_count != 0) {
6073 int d = ztest_random(ztest_opts.zo_datasets);
6074 ztest_dataset_destroy(d);
6076 zs->zs_enospc_count = 0;
6078 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (thread_t),
6081 if (ztest_opts.zo_verbose >= 4)
6082 (void) printf("starting main threads...\n");
6085 * Kick off all the tests that run in parallel.
6087 for (int t = 0; t < ztest_opts.zo_threads; t++) {
6088 if (t < ztest_opts.zo_datasets &&
6089 ztest_dataset_open(t) != 0)
6091 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
6092 THR_BOUND, &tid[t]) == 0);
6096 * Wait for all of the tests to complete. We go in reverse order
6097 * so we don't close datasets while threads are still using them.
6099 for (int t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6100 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
6101 if (t < ztest_opts.zo_datasets)
6102 ztest_dataset_close(t);
6105 txg_wait_synced(spa_get_dsl(spa), 0);
6107 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6108 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6109 zfs_dbgmsg_print(FTAG);
6111 umem_free(tid, ztest_opts.zo_threads * sizeof (thread_t));
6113 /* Kill the resume thread */
6114 ztest_exiting = B_TRUE;
6115 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
6119 * Right before closing the pool, kick off a bunch of async I/O;
6120 * spa_close() should wait for it to complete.
6122 for (uint64_t object = 1; object < 50; object++) {
6123 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6124 ZIO_PRIORITY_SYNC_READ);
6127 spa_close(spa, FTAG);
6130 * Verify that we can loop over all pools.
6132 mutex_enter(&spa_namespace_lock);
6133 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6134 if (ztest_opts.zo_verbose > 3)
6135 (void) printf("spa_next: found %s\n", spa_name(spa));
6136 mutex_exit(&spa_namespace_lock);
6139 * Verify that we can export the pool and reimport it under a
6142 if (ztest_random(2) == 0) {
6143 char name[ZFS_MAX_DATASET_NAME_LEN];
6144 (void) snprintf(name, sizeof (name), "%s_import",
6145 ztest_opts.zo_pool);
6146 ztest_spa_import_export(ztest_opts.zo_pool, name);
6147 ztest_spa_import_export(name, ztest_opts.zo_pool);
6152 list_destroy(&zcl.zcl_callbacks);
6154 mutex_destroy(&zcl.zcl_callbacks_lock);
6156 rw_destroy(&ztest_name_lock);
6157 mutex_destroy(&ztest_vdev_lock);
6158 mutex_destroy(&ztest_checkpoint_lock);
6164 ztest_ds_t *zd = &ztest_ds[0];
6168 if (ztest_opts.zo_verbose >= 3)
6169 (void) printf("testing spa_freeze()...\n");
6171 kernel_init(FREAD | FWRITE);
6172 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6173 VERIFY3U(0, ==, ztest_dataset_open(0));
6177 * Force the first log block to be transactionally allocated.
6178 * We have to do this before we freeze the pool -- otherwise
6179 * the log chain won't be anchored.
6181 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6182 ztest_dmu_object_alloc_free(zd, 0);
6183 zil_commit(zd->zd_zilog, 0);
6186 txg_wait_synced(spa_get_dsl(spa), 0);
6189 * Freeze the pool. This stops spa_sync() from doing anything,
6190 * so that the only way to record changes from now on is the ZIL.
6195 * Because it is hard to predict how much space a write will actually
6196 * require beforehand, we leave ourselves some fudge space to write over
6199 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6202 * Run tests that generate log records but don't alter the pool config
6203 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6204 * We do a txg_wait_synced() after each iteration to force the txg
6205 * to increase well beyond the last synced value in the uberblock.
6206 * The ZIL should be OK with that.
6208 * Run a random number of times less than zo_maxloops and ensure we do
6209 * not run out of space on the pool.
6211 while (ztest_random(10) != 0 &&
6212 numloops++ < ztest_opts.zo_maxloops &&
6213 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6215 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
6216 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6217 ztest_io(zd, od.od_object,
6218 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6219 txg_wait_synced(spa_get_dsl(spa), 0);
6223 * Commit all of the changes we just generated.
6225 zil_commit(zd->zd_zilog, 0);
6226 txg_wait_synced(spa_get_dsl(spa), 0);
6229 * Close our dataset and close the pool.
6231 ztest_dataset_close(0);
6232 spa_close(spa, FTAG);
6236 * Open and close the pool and dataset to induce log replay.
6238 kernel_init(FREAD | FWRITE);
6239 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6240 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6241 VERIFY3U(0, ==, ztest_dataset_open(0));
6242 ztest_dataset_close(0);
6245 txg_wait_synced(spa_get_dsl(spa), 0);
6246 ztest_reguid(NULL, 0);
6248 spa_close(spa, FTAG);
6253 print_time(hrtime_t t, char *timebuf)
6255 hrtime_t s = t / NANOSEC;
6256 hrtime_t m = s / 60;
6257 hrtime_t h = m / 60;
6258 hrtime_t d = h / 24;
6267 (void) sprintf(timebuf,
6268 "%llud%02lluh%02llum%02llus", d, h, m, s);
6270 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6272 (void) sprintf(timebuf, "%llum%02llus", m, s);
6274 (void) sprintf(timebuf, "%llus", s);
6282 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6283 if (ztest_random(2) == 0)
6285 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6291 * Create a storage pool with the given name and initial vdev size.
6292 * Then test spa_freeze() functionality.
6295 ztest_init(ztest_shared_t *zs)
6298 nvlist_t *nvroot, *props;
6300 mutex_init(&ztest_vdev_lock, NULL, USYNC_THREAD, NULL);
6301 mutex_init(&ztest_checkpoint_lock, NULL, USYNC_THREAD, NULL);
6302 rw_init(&ztest_name_lock, NULL, USYNC_THREAD, NULL);
6304 kernel_init(FREAD | FWRITE);
6307 * Create the storage pool.
6309 (void) spa_destroy(ztest_opts.zo_pool);
6310 ztest_shared->zs_vdev_next_leaf = 0;
6312 zs->zs_mirrors = ztest_opts.zo_mirrors;
6313 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6314 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6315 props = make_random_props();
6316 for (int i = 0; i < SPA_FEATURES; i++) {
6318 (void) snprintf(buf, sizeof (buf), "feature@%s",
6319 spa_feature_table[i].fi_uname);
6320 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6322 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6323 nvlist_free(nvroot);
6326 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6327 zs->zs_metaslab_sz =
6328 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6330 spa_close(spa, FTAG);
6334 ztest_run_zdb(ztest_opts.zo_pool);
6338 ztest_run_zdb(ztest_opts.zo_pool);
6340 rw_destroy(&ztest_name_lock);
6341 mutex_destroy(&ztest_vdev_lock);
6342 mutex_destroy(&ztest_checkpoint_lock);
6348 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6350 ztest_fd_data = mkstemp(ztest_name_data);
6351 ASSERT3S(ztest_fd_data, >=, 0);
6352 (void) unlink(ztest_name_data);
6357 shared_data_size(ztest_shared_hdr_t *hdr)
6361 size = hdr->zh_hdr_size;
6362 size += hdr->zh_opts_size;
6363 size += hdr->zh_size;
6364 size += hdr->zh_stats_size * hdr->zh_stats_count;
6365 size += hdr->zh_ds_size * hdr->zh_ds_count;
6374 ztest_shared_hdr_t *hdr;
6376 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6377 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6378 ASSERT(hdr != MAP_FAILED);
6380 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6382 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6383 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6384 hdr->zh_size = sizeof (ztest_shared_t);
6385 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6386 hdr->zh_stats_count = ZTEST_FUNCS;
6387 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6388 hdr->zh_ds_count = ztest_opts.zo_datasets;
6390 size = shared_data_size(hdr);
6391 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6393 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6400 ztest_shared_hdr_t *hdr;
6403 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6404 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6405 ASSERT(hdr != MAP_FAILED);
6407 size = shared_data_size(hdr);
6409 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6410 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6411 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6412 ASSERT(hdr != MAP_FAILED);
6413 buf = (uint8_t *)hdr;
6415 offset = hdr->zh_hdr_size;
6416 ztest_shared_opts = (void *)&buf[offset];
6417 offset += hdr->zh_opts_size;
6418 ztest_shared = (void *)&buf[offset];
6419 offset += hdr->zh_size;
6420 ztest_shared_callstate = (void *)&buf[offset];
6421 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6422 ztest_shared_ds = (void *)&buf[offset];
6426 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6430 char *cmdbuf = NULL;
6435 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6436 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6441 fatal(1, "fork failed");
6443 if (pid == 0) { /* child */
6444 char *emptyargv[2] = { cmd, NULL };
6445 char fd_data_str[12];
6447 struct rlimit rl = { 1024, 1024 };
6448 (void) setrlimit(RLIMIT_NOFILE, &rl);
6450 (void) close(ztest_fd_rand);
6452 snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6453 VERIFY0(setenv("ZTEST_FD_DATA", fd_data_str, 1));
6455 (void) enable_extended_FILE_stdio(-1, -1);
6456 if (libpath != NULL)
6457 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6459 (void) execv(cmd, emptyargv);
6461 (void) execvp(cmd, emptyargv);
6463 ztest_dump_core = B_FALSE;
6464 fatal(B_TRUE, "exec failed: %s", cmd);
6467 if (cmdbuf != NULL) {
6468 umem_free(cmdbuf, MAXPATHLEN);
6472 while (waitpid(pid, &status, 0) != pid)
6474 if (statusp != NULL)
6477 if (WIFEXITED(status)) {
6478 if (WEXITSTATUS(status) != 0) {
6479 (void) fprintf(stderr, "child exited with code %d\n",
6480 WEXITSTATUS(status));
6484 } else if (WIFSIGNALED(status)) {
6485 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6486 (void) fprintf(stderr, "child died with signal %d\n",
6492 (void) fprintf(stderr, "something strange happened to child\n");
6499 ztest_run_init(void)
6501 ztest_shared_t *zs = ztest_shared;
6503 ASSERT(ztest_opts.zo_init != 0);
6506 * Blow away any existing copy of zpool.cache
6508 (void) remove(spa_config_path);
6511 * Create and initialize our storage pool.
6513 for (int i = 1; i <= ztest_opts.zo_init; i++) {
6514 bzero(zs, sizeof (ztest_shared_t));
6515 if (ztest_opts.zo_verbose >= 3 &&
6516 ztest_opts.zo_init != 1) {
6517 (void) printf("ztest_init(), pass %d\n", i);
6524 main(int argc, char **argv)
6532 ztest_shared_callstate_t *zc;
6534 char numbuf[NN_NUMBUF_SZ];
6538 char *fd_data_str = getenv("ZTEST_FD_DATA");
6540 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6542 dprintf_setup(&argc, argv);
6543 zfs_deadman_synctime_ms = 300000;
6545 * As two-word space map entries may not come up often (especially
6546 * if pool and vdev sizes are small) we want to force at least some
6547 * of them so the feature get tested.
6549 zfs_force_some_double_word_sm_entries = B_TRUE;
6551 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6552 ASSERT3S(ztest_fd_rand, >=, 0);
6555 process_options(argc, argv);
6560 bcopy(&ztest_opts, ztest_shared_opts,
6561 sizeof (*ztest_shared_opts));
6563 ztest_fd_data = atoi(fd_data_str);
6565 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6567 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6569 /* Override location of zpool.cache */
6570 VERIFY3U(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6571 ztest_opts.zo_dir), !=, -1);
6573 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6578 metaslab_force_ganging = ztest_opts.zo_metaslab_force_ganging;
6579 metaslab_df_alloc_threshold =
6580 zs->zs_metaslab_df_alloc_threshold;
6589 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6591 if (ztest_opts.zo_verbose >= 1) {
6592 (void) printf("%llu vdevs, %d datasets, %d threads,"
6593 " %llu seconds...\n",
6594 (u_longlong_t)ztest_opts.zo_vdevs,
6595 ztest_opts.zo_datasets,
6596 ztest_opts.zo_threads,
6597 (u_longlong_t)ztest_opts.zo_time);
6600 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6601 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6603 zs->zs_do_init = B_TRUE;
6604 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6605 if (ztest_opts.zo_verbose >= 1) {
6606 (void) printf("Executing older ztest for "
6607 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6609 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6610 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6612 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6614 zs->zs_do_init = B_FALSE;
6616 zs->zs_proc_start = gethrtime();
6617 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6619 for (int f = 0; f < ZTEST_FUNCS; f++) {
6620 zi = &ztest_info[f];
6621 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6622 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6623 zc->zc_next = UINT64_MAX;
6625 zc->zc_next = zs->zs_proc_start +
6626 ztest_random(2 * zi->zi_interval[0] + 1);
6630 * Run the tests in a loop. These tests include fault injection
6631 * to verify that self-healing data works, and forced crashes
6632 * to verify that we never lose on-disk consistency.
6634 while (gethrtime() < zs->zs_proc_stop) {
6639 * Initialize the workload counters for each function.
6641 for (int f = 0; f < ZTEST_FUNCS; f++) {
6642 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6647 /* Set the allocation switch size */
6648 zs->zs_metaslab_df_alloc_threshold =
6649 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6651 if (!hasalt || ztest_random(2) == 0) {
6652 if (hasalt && ztest_opts.zo_verbose >= 1) {
6653 (void) printf("Executing newer ztest: %s\n",
6657 killed = exec_child(cmd, NULL, B_TRUE, &status);
6659 if (hasalt && ztest_opts.zo_verbose >= 1) {
6660 (void) printf("Executing older ztest: %s\n",
6661 ztest_opts.zo_alt_ztest);
6664 killed = exec_child(ztest_opts.zo_alt_ztest,
6665 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6672 if (ztest_opts.zo_verbose >= 1) {
6673 hrtime_t now = gethrtime();
6675 now = MIN(now, zs->zs_proc_stop);
6676 print_time(zs->zs_proc_stop - now, timebuf);
6677 nicenum(zs->zs_space, numbuf, sizeof (numbuf));
6679 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6680 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6682 WIFEXITED(status) ? "Complete" : "SIGKILL",
6683 (u_longlong_t)zs->zs_enospc_count,
6684 100.0 * zs->zs_alloc / zs->zs_space,
6686 100.0 * (now - zs->zs_proc_start) /
6687 (ztest_opts.zo_time * NANOSEC), timebuf);
6690 if (ztest_opts.zo_verbose >= 2) {
6691 (void) printf("\nWorkload summary:\n\n");
6692 (void) printf("%7s %9s %s\n",
6693 "Calls", "Time", "Function");
6694 (void) printf("%7s %9s %s\n",
6695 "-----", "----", "--------");
6696 for (int f = 0; f < ZTEST_FUNCS; f++) {
6699 zi = &ztest_info[f];
6700 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6701 print_time(zc->zc_time, timebuf);
6702 (void) dladdr((void *)zi->zi_func, &dli);
6703 (void) printf("%7llu %9s %s\n",
6704 (u_longlong_t)zc->zc_count, timebuf,
6707 (void) printf("\n");
6711 * It's possible that we killed a child during a rename test,
6712 * in which case we'll have a 'ztest_tmp' pool lying around
6713 * instead of 'ztest'. Do a blind rename in case this happened.
6716 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6717 spa_close(spa, FTAG);
6719 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
6721 kernel_init(FREAD | FWRITE);
6722 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6723 ztest_opts.zo_pool);
6724 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6728 ztest_run_zdb(ztest_opts.zo_pool);
6731 if (ztest_opts.zo_verbose >= 1) {
6733 (void) printf("%d runs of older ztest: %s\n", older,
6734 ztest_opts.zo_alt_ztest);
6735 (void) printf("%d runs of newer ztest: %s\n", newer,
6738 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6739 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6742 umem_free(cmd, MAXNAMELEN);