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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * The objective of this program is to provide a DMU/ZAP/SPA stress test
28 * that runs entirely in userland, is easy to use, and easy to extend.
30 * The overall design of the ztest program is as follows:
32 * (1) For each major functional area (e.g. adding vdevs to a pool,
33 * creating and destroying datasets, reading and writing objects, etc)
34 * we have a simple routine to test that functionality. These
35 * individual routines do not have to do anything "stressful".
37 * (2) We turn these simple functionality tests into a stress test by
38 * running them all in parallel, with as many threads as desired,
39 * and spread across as many datasets, objects, and vdevs as desired.
41 * (3) While all this is happening, we inject faults into the pool to
42 * verify that self-healing data really works.
44 * (4) Every time we open a dataset, we change its checksum and compression
45 * functions. Thus even individual objects vary from block to block
46 * in which checksum they use and whether they're compressed.
48 * (5) To verify that we never lose on-disk consistency after a crash,
49 * we run the entire test in a child of the main process.
50 * At random times, the child self-immolates with a SIGKILL.
51 * This is the software equivalent of pulling the power cord.
52 * The parent then runs the test again, using the existing
53 * storage pool, as many times as desired.
55 * (6) To verify that we don't have future leaks or temporal incursions,
56 * many of the functional tests record the transaction group number
57 * as part of their data. When reading old data, they verify that
58 * the transaction group number is less than the current, open txg.
59 * If you add a new test, please do this if applicable.
61 * When run with no arguments, ztest runs for about five minutes and
62 * produces no output if successful. To get a little bit of information,
63 * specify -V. To get more information, specify -VV, and so on.
65 * To turn this into an overnight stress test, use -T to specify run time.
67 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
68 * to increase the pool capacity, fanout, and overall stress level.
70 * The -N(okill) option will suppress kills, so each child runs to completion.
71 * This can be useful when you're trying to distinguish temporal incursions
72 * from plain old race conditions.
75 #include <sys/zfs_context.h>
80 #include <sys/dmu_traverse.h>
81 #include <sys/dmu_objset.h>
87 #include <sys/resource.h>
89 #include <sys/zio_checksum.h>
90 #include <sys/zio_compress.h>
92 #include <sys/vdev_impl.h>
93 #include <sys/vdev_file.h>
94 #include <sys/spa_impl.h>
95 #include <sys/dsl_prop.h>
96 #include <sys/dsl_dataset.h>
97 #include <sys/refcount.h>
99 #include <stdio_ext.h>
108 #include <sys/fs/zfs.h>
110 static char cmdname[] = "ztest";
111 static char *zopt_pool = cmdname;
112 static char *progname;
114 static uint64_t zopt_vdevs = 5;
115 static uint64_t zopt_vdevtime;
116 static int zopt_ashift = SPA_MINBLOCKSHIFT;
117 static int zopt_mirrors = 2;
118 static int zopt_raidz = 4;
119 static int zopt_raidz_parity = 1;
120 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
121 static int zopt_datasets = 7;
122 static int zopt_threads = 23;
123 static uint64_t zopt_passtime = 60; /* 60 seconds */
124 static uint64_t zopt_killrate = 70; /* 70% kill rate */
125 static int zopt_verbose = 0;
126 static int zopt_init = 1;
127 static char *zopt_dir = "/tmp";
128 static uint64_t zopt_time = 300; /* 5 minutes */
129 static int zopt_maxfaults;
131 typedef struct ztest_block_tag {
140 typedef struct ztest_args {
141 char za_pool[MAXNAMELEN];
146 uint64_t za_instance;
149 uint64_t za_diroff_shared;
154 traverse_handle_t *za_th;
156 * Thread-local variables can go here to aid debugging.
158 ztest_block_tag_t za_rbt;
159 ztest_block_tag_t za_wbt;
160 dmu_object_info_t za_doi;
164 typedef void ztest_func_t(ztest_args_t *);
167 * Note: these aren't static because we want dladdr() to work.
169 ztest_func_t ztest_dmu_read_write;
170 ztest_func_t ztest_dmu_write_parallel;
171 ztest_func_t ztest_dmu_object_alloc_free;
172 ztest_func_t ztest_zap;
173 ztest_func_t ztest_zap_parallel;
174 ztest_func_t ztest_traverse;
175 ztest_func_t ztest_dsl_prop_get_set;
176 ztest_func_t ztest_dmu_objset_create_destroy;
177 ztest_func_t ztest_dmu_snapshot_create_destroy;
178 ztest_func_t ztest_dsl_dataset_promote_busy;
179 ztest_func_t ztest_spa_create_destroy;
180 ztest_func_t ztest_fault_inject;
181 ztest_func_t ztest_spa_rename;
182 ztest_func_t ztest_vdev_attach_detach;
183 ztest_func_t ztest_vdev_LUN_growth;
184 ztest_func_t ztest_vdev_add_remove;
185 ztest_func_t ztest_vdev_aux_add_remove;
186 ztest_func_t ztest_scrub;
188 typedef struct ztest_info {
189 ztest_func_t *zi_func; /* test function */
190 uint64_t zi_iters; /* iterations per execution */
191 uint64_t *zi_interval; /* execute every <interval> seconds */
192 uint64_t zi_calls; /* per-pass count */
193 uint64_t zi_call_time; /* per-pass time */
194 uint64_t zi_call_total; /* cumulative total */
195 uint64_t zi_call_target; /* target cumulative total */
198 uint64_t zopt_always = 0; /* all the time */
199 uint64_t zopt_often = 1; /* every second */
200 uint64_t zopt_sometimes = 10; /* every 10 seconds */
201 uint64_t zopt_rarely = 60; /* every 60 seconds */
203 ztest_info_t ztest_info[] = {
204 { ztest_dmu_read_write, 1, &zopt_always },
205 { ztest_dmu_write_parallel, 30, &zopt_always },
206 { ztest_dmu_object_alloc_free, 1, &zopt_always },
207 { ztest_zap, 30, &zopt_always },
208 { ztest_zap_parallel, 100, &zopt_always },
209 { ztest_traverse, 1, &zopt_often },
210 { ztest_dsl_prop_get_set, 1, &zopt_sometimes },
211 { ztest_dmu_objset_create_destroy, 1, &zopt_sometimes },
212 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
213 { ztest_dsl_dataset_promote_busy, 1, &zopt_sometimes },
214 { ztest_spa_create_destroy, 1, &zopt_sometimes },
215 { ztest_fault_inject, 1, &zopt_sometimes },
216 { ztest_spa_rename, 1, &zopt_rarely },
217 { ztest_vdev_attach_detach, 1, &zopt_rarely },
218 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
219 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
220 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
221 { ztest_scrub, 1, &zopt_vdevtime },
224 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
226 #define ZTEST_SYNC_LOCKS 16
229 * Stuff we need to share writably between parent and child.
231 typedef struct ztest_shared {
232 mutex_t zs_vdev_lock;
233 rwlock_t zs_name_lock;
234 uint64_t zs_vdev_primaries;
235 uint64_t zs_vdev_aux;
236 uint64_t zs_enospc_count;
237 hrtime_t zs_start_time;
238 hrtime_t zs_stop_time;
241 ztest_info_t zs_info[ZTEST_FUNCS];
242 mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
243 uint64_t zs_seq[ZTEST_SYNC_LOCKS];
246 static char ztest_dev_template[] = "%s/%s.%llua";
247 static char ztest_aux_template[] = "%s/%s.%s.%llu";
248 static ztest_shared_t *ztest_shared;
250 static int ztest_random_fd;
251 static int ztest_dump_core = 1;
253 static boolean_t ztest_exiting;
255 extern uint64_t metaslab_gang_bang;
257 #define ZTEST_DIROBJ 1
258 #define ZTEST_MICROZAP_OBJ 2
259 #define ZTEST_FATZAP_OBJ 3
261 #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10)
262 #define ZTEST_DIRSIZE 256
264 static void usage(boolean_t) __NORETURN;
267 * These libumem hooks provide a reasonable set of defaults for the allocator's
268 * debugging facilities.
273 return ("default,verbose"); /* $UMEM_DEBUG setting */
277 _umem_logging_init(void)
279 return ("fail,contents"); /* $UMEM_LOGGING setting */
282 #define FATAL_MSG_SZ 1024
287 fatal(int do_perror, char *message, ...)
290 int save_errno = errno;
291 char buf[FATAL_MSG_SZ];
293 (void) fflush(stdout);
295 va_start(args, message);
296 (void) sprintf(buf, "ztest: ");
298 (void) vsprintf(buf + strlen(buf), message, args);
301 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
302 ": %s", strerror(save_errno));
304 (void) fprintf(stderr, "%s\n", buf);
305 fatal_msg = buf; /* to ease debugging */
312 str2shift(const char *buf)
314 const char *ends = "BKMGTPEZ";
319 for (i = 0; i < strlen(ends); i++) {
320 if (toupper(buf[0]) == ends[i])
323 if (i == strlen(ends)) {
324 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
328 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
331 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
337 nicenumtoull(const char *buf)
342 val = strtoull(buf, &end, 0);
344 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
346 } else if (end[0] == '.') {
347 double fval = strtod(buf, &end);
348 fval *= pow(2, str2shift(end));
349 if (fval > UINT64_MAX) {
350 (void) fprintf(stderr, "ztest: value too large: %s\n",
354 val = (uint64_t)fval;
356 int shift = str2shift(end);
357 if (shift >= 64 || (val << shift) >> shift != val) {
358 (void) fprintf(stderr, "ztest: value too large: %s\n",
368 usage(boolean_t requested)
370 char nice_vdev_size[10];
371 char nice_gang_bang[10];
372 FILE *fp = requested ? stdout : stderr;
374 nicenum(zopt_vdev_size, nice_vdev_size);
375 nicenum(metaslab_gang_bang, nice_gang_bang);
377 (void) fprintf(fp, "Usage: %s\n"
378 "\t[-v vdevs (default: %llu)]\n"
379 "\t[-s size_of_each_vdev (default: %s)]\n"
380 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
381 "\t[-m mirror_copies (default: %d)]\n"
382 "\t[-r raidz_disks (default: %d)]\n"
383 "\t[-R raidz_parity (default: %d)]\n"
384 "\t[-d datasets (default: %d)]\n"
385 "\t[-t threads (default: %d)]\n"
386 "\t[-g gang_block_threshold (default: %s)]\n"
387 "\t[-i initialize pool i times (default: %d)]\n"
388 "\t[-k kill percentage (default: %llu%%)]\n"
389 "\t[-p pool_name (default: %s)]\n"
390 "\t[-f file directory for vdev files (default: %s)]\n"
391 "\t[-V(erbose)] (use multiple times for ever more blather)\n"
392 "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
393 "\t[-T time] total run time (default: %llu sec)\n"
394 "\t[-P passtime] time per pass (default: %llu sec)\n"
395 "\t[-h] (print help)\n"
398 (u_longlong_t)zopt_vdevs, /* -v */
399 nice_vdev_size, /* -s */
400 zopt_ashift, /* -a */
401 zopt_mirrors, /* -m */
403 zopt_raidz_parity, /* -R */
404 zopt_datasets, /* -d */
405 zopt_threads, /* -t */
406 nice_gang_bang, /* -g */
408 (u_longlong_t)zopt_killrate, /* -k */
411 (u_longlong_t)zopt_time, /* -T */
412 (u_longlong_t)zopt_passtime); /* -P */
413 exit(requested ? 0 : 1);
417 ztest_random(uint64_t range)
424 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
425 fatal(1, "short read from /dev/urandom");
431 ztest_record_enospc(char *s)
433 dprintf("ENOSPC doing: %s\n", s ? s : "<unknown>");
434 ztest_shared->zs_enospc_count++;
438 process_options(int argc, char **argv)
443 /* Remember program name. */
446 /* By default, test gang blocks for blocks 32K and greater */
447 metaslab_gang_bang = 32 << 10;
449 while ((opt = getopt(argc, argv,
450 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
466 value = nicenumtoull(optarg);
473 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
479 zopt_mirrors = value;
482 zopt_raidz = MAX(1, value);
485 zopt_raidz_parity = MIN(MAX(value, 1), 2);
488 zopt_datasets = MAX(1, value);
491 zopt_threads = MAX(1, value);
494 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
500 zopt_killrate = value;
503 zopt_pool = strdup(optarg);
506 zopt_dir = strdup(optarg);
518 zopt_passtime = MAX(1, value);
530 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
532 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
533 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
537 ztest_get_ashift(void)
539 if (zopt_ashift == 0)
540 return (SPA_MINBLOCKSHIFT + ztest_random(3));
541 return (zopt_ashift);
545 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
547 char pathbuf[MAXPATHLEN];
552 ashift = ztest_get_ashift();
558 vdev = ztest_shared->zs_vdev_aux;
559 (void) sprintf(path, ztest_aux_template,
560 zopt_dir, zopt_pool, aux, vdev);
562 vdev = ztest_shared->zs_vdev_primaries++;
563 (void) sprintf(path, ztest_dev_template,
564 zopt_dir, zopt_pool, vdev);
569 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
571 fatal(1, "can't open %s", path);
572 if (ftruncate(fd, size) != 0)
573 fatal(1, "can't ftruncate %s", path);
577 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
578 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
579 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
580 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
586 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
588 nvlist_t *raidz, **child;
592 return (make_vdev_file(path, aux, size, ashift));
593 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
595 for (c = 0; c < r; c++)
596 child[c] = make_vdev_file(path, aux, size, ashift);
598 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
599 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
600 VDEV_TYPE_RAIDZ) == 0);
601 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
602 zopt_raidz_parity) == 0);
603 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
606 for (c = 0; c < r; c++)
607 nvlist_free(child[c]);
609 umem_free(child, r * sizeof (nvlist_t *));
615 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
618 nvlist_t *mirror, **child;
622 return (make_vdev_raidz(path, aux, size, ashift, r));
624 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
626 for (c = 0; c < m; c++)
627 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
629 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
630 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
631 VDEV_TYPE_MIRROR) == 0);
632 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
635 for (c = 0; c < m; c++)
636 nvlist_free(child[c]);
638 umem_free(child, m * sizeof (nvlist_t *));
644 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
645 int log, int r, int m, int t)
647 nvlist_t *root, **child;
652 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
654 for (c = 0; c < t; c++) {
655 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
656 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
660 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
661 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
662 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
665 for (c = 0; c < t; c++)
666 nvlist_free(child[c]);
668 umem_free(child, t * sizeof (nvlist_t *));
674 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
676 int bs = SPA_MINBLOCKSHIFT +
677 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
678 int ibs = DN_MIN_INDBLKSHIFT +
679 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
682 error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
685 dmu_objset_name(os, osname);
686 fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
687 osname, object, 1 << bs, ibs, error);
692 ztest_random_checksum(void)
697 checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
698 } while (zio_checksum_table[checksum].ci_zbt);
700 if (checksum == ZIO_CHECKSUM_OFF)
701 checksum = ZIO_CHECKSUM_ON;
707 ztest_random_compress(void)
709 return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
712 typedef struct ztest_replay {
718 ztest_replay_create(ztest_replay_t *zr, lr_create_t *lr, boolean_t byteswap)
720 objset_t *os = zr->zr_os;
725 byteswap_uint64_array(lr, sizeof (*lr));
727 tx = dmu_tx_create(os);
728 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
729 error = dmu_tx_assign(tx, zr->zr_assign);
735 error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
737 ASSERT3U(error, ==, 0);
740 if (zopt_verbose >= 5) {
741 char osname[MAXNAMELEN];
742 dmu_objset_name(os, osname);
743 (void) printf("replay create of %s object %llu"
744 " in txg %llu = %d\n",
745 osname, (u_longlong_t)lr->lr_doid,
746 (u_longlong_t)zr->zr_assign, error);
753 ztest_replay_remove(ztest_replay_t *zr, lr_remove_t *lr, boolean_t byteswap)
755 objset_t *os = zr->zr_os;
760 byteswap_uint64_array(lr, sizeof (*lr));
762 tx = dmu_tx_create(os);
763 dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
764 error = dmu_tx_assign(tx, zr->zr_assign);
770 error = dmu_object_free(os, lr->lr_doid, tx);
776 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
777 NULL, /* 0 no such transaction type */
778 ztest_replay_create, /* TX_CREATE */
780 NULL, /* TX_MKXATTR */
781 NULL, /* TX_SYMLINK */
782 ztest_replay_remove, /* TX_REMOVE */
785 NULL, /* TX_RENAME */
787 NULL, /* TX_TRUNCATE */
788 NULL, /* TX_SETATTR */
793 * Verify that we can't destroy an active pool, create an existing pool,
794 * or create a pool with a bad vdev spec.
797 ztest_spa_create_destroy(ztest_args_t *za)
804 * Attempt to create using a bad file.
806 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
807 error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
810 fatal(0, "spa_create(bad_file) = %d", error);
813 * Attempt to create using a bad mirror.
815 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
816 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
819 fatal(0, "spa_create(bad_mirror) = %d", error);
822 * Attempt to create an existing pool. It shouldn't matter
823 * what's in the nvroot; we should fail with EEXIST.
825 (void) rw_rdlock(&ztest_shared->zs_name_lock);
826 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
827 error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
830 fatal(0, "spa_create(whatever) = %d", error);
832 error = spa_open(za->za_pool, &spa, FTAG);
834 fatal(0, "spa_open() = %d", error);
836 error = spa_destroy(za->za_pool);
838 fatal(0, "spa_destroy() = %d", error);
840 spa_close(spa, FTAG);
841 (void) rw_unlock(&ztest_shared->zs_name_lock);
845 vdev_lookup_by_path(vdev_t *vd, const char *path)
849 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
852 for (int c = 0; c < vd->vdev_children; c++)
853 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
861 * Verify that vdev_add() works as expected.
864 ztest_vdev_add_remove(ztest_args_t *za)
866 spa_t *spa = za->za_spa;
867 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
871 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
873 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
875 ztest_shared->zs_vdev_primaries =
876 spa->spa_root_vdev->vdev_children * leaves;
878 spa_config_exit(spa, SCL_VDEV, FTAG);
881 * Make 1/4 of the devices be log devices.
883 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
884 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
886 error = spa_vdev_add(spa, nvroot);
889 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
892 ztest_record_enospc("spa_vdev_add");
894 fatal(0, "spa_vdev_add() = %d", error);
898 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
901 ztest_vdev_aux_add_remove(ztest_args_t *za)
903 spa_t *spa = za->za_spa;
904 vdev_t *rvd = spa->spa_root_vdev;
910 if (ztest_random(2) == 0) {
911 sav = &spa->spa_spares;
912 aux = ZPOOL_CONFIG_SPARES;
914 sav = &spa->spa_l2cache;
915 aux = ZPOOL_CONFIG_L2CACHE;
918 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
920 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
922 if (sav->sav_count != 0 && ztest_random(4) == 0) {
924 * Pick a random device to remove.
926 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
929 * Find an unused device we can add.
931 ztest_shared->zs_vdev_aux = 0;
933 char path[MAXPATHLEN];
935 (void) sprintf(path, ztest_aux_template, zopt_dir,
936 zopt_pool, aux, ztest_shared->zs_vdev_aux);
937 for (c = 0; c < sav->sav_count; c++)
938 if (strcmp(sav->sav_vdevs[c]->vdev_path,
941 if (c == sav->sav_count &&
942 vdev_lookup_by_path(rvd, path) == NULL)
944 ztest_shared->zs_vdev_aux++;
948 spa_config_exit(spa, SCL_VDEV, FTAG);
954 nvlist_t *nvroot = make_vdev_root(NULL, aux,
955 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
956 error = spa_vdev_add(spa, nvroot);
958 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
962 * Remove an existing device. Sometimes, dirty its
963 * vdev state first to make sure we handle removal
964 * of devices that have pending state changes.
966 if (ztest_random(2) == 0)
967 (void) vdev_online(spa, guid, B_FALSE, NULL);
969 error = spa_vdev_remove(spa, guid, B_FALSE);
970 if (error != 0 && error != EBUSY)
971 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
974 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
978 * Verify that we can attach and detach devices.
981 ztest_vdev_attach_detach(ztest_args_t *za)
983 spa_t *spa = za->za_spa;
984 spa_aux_vdev_t *sav = &spa->spa_spares;
985 vdev_t *rvd = spa->spa_root_vdev;
986 vdev_t *oldvd, *newvd, *pvd;
988 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
990 uint64_t ashift = ztest_get_ashift();
992 size_t oldsize, newsize;
993 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
995 int oldvd_has_siblings = B_FALSE;
996 int newvd_is_spare = B_FALSE;
998 int error, expected_error;
1000 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
1002 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1005 * Decide whether to do an attach or a replace.
1007 replacing = ztest_random(2);
1010 * Pick a random top-level vdev.
1012 top = ztest_random(rvd->vdev_children);
1015 * Pick a random leaf within it.
1017 leaf = ztest_random(leaves);
1022 oldvd = rvd->vdev_child[top];
1023 if (zopt_mirrors >= 1)
1024 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
1026 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
1029 * If we're already doing an attach or replace, oldvd may be a
1030 * mirror vdev -- in which case, pick a random child.
1032 while (oldvd->vdev_children != 0) {
1033 oldvd_has_siblings = B_TRUE;
1034 ASSERT(oldvd->vdev_children == 2);
1035 oldvd = oldvd->vdev_child[ztest_random(2)];
1038 oldguid = oldvd->vdev_guid;
1039 oldsize = vdev_get_rsize(oldvd);
1040 oldvd_is_log = oldvd->vdev_top->vdev_islog;
1041 (void) strcpy(oldpath, oldvd->vdev_path);
1042 pvd = oldvd->vdev_parent;
1045 * If oldvd has siblings, then half of the time, detach it.
1047 if (oldvd_has_siblings && ztest_random(2) == 0) {
1048 spa_config_exit(spa, SCL_VDEV, FTAG);
1049 error = spa_vdev_detach(spa, oldguid, B_FALSE);
1050 if (error != 0 && error != ENODEV && error != EBUSY)
1051 fatal(0, "detach (%s) returned %d",
1053 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1058 * For the new vdev, choose with equal probability between the two
1059 * standard paths (ending in either 'a' or 'b') or a random hot spare.
1061 if (sav->sav_count != 0 && ztest_random(3) == 0) {
1062 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
1063 newvd_is_spare = B_TRUE;
1064 (void) strcpy(newpath, newvd->vdev_path);
1066 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
1067 zopt_dir, zopt_pool, top * leaves + leaf);
1068 if (ztest_random(2) == 0)
1069 newpath[strlen(newpath) - 1] = 'b';
1070 newvd = vdev_lookup_by_path(rvd, newpath);
1074 newsize = vdev_get_rsize(newvd);
1077 * Make newsize a little bigger or smaller than oldsize.
1078 * If it's smaller, the attach should fail.
1079 * If it's larger, and we're doing a replace,
1080 * we should get dynamic LUN growth when we're done.
1082 newsize = 10 * oldsize / (9 + ztest_random(3));
1086 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1087 * unless it's a replace; in that case any non-replacing parent is OK.
1089 * If newvd is already part of the pool, it should fail with EBUSY.
1091 * If newvd is too small, it should fail with EOVERFLOW.
1093 if (pvd->vdev_ops != &vdev_mirror_ops &&
1094 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
1095 pvd->vdev_ops == &vdev_replacing_ops ||
1096 pvd->vdev_ops == &vdev_spare_ops))
1097 expected_error = ENOTSUP;
1098 else if (newvd_is_spare && (!replacing || oldvd_is_log))
1099 expected_error = ENOTSUP;
1100 else if (newvd == oldvd)
1101 expected_error = replacing ? 0 : EBUSY;
1102 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
1103 expected_error = EBUSY;
1104 else if (newsize < oldsize)
1105 expected_error = EOVERFLOW;
1106 else if (ashift > oldvd->vdev_top->vdev_ashift)
1107 expected_error = EDOM;
1111 spa_config_exit(spa, SCL_VDEV, FTAG);
1114 * Build the nvlist describing newpath.
1116 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1117 ashift, 0, 0, 0, 1);
1119 error = spa_vdev_attach(spa, oldguid, root, replacing);
1124 * If our parent was the replacing vdev, but the replace completed,
1125 * then instead of failing with ENOTSUP we may either succeed,
1126 * fail with ENODEV, or fail with EOVERFLOW.
1128 if (expected_error == ENOTSUP &&
1129 (error == 0 || error == ENODEV || error == EOVERFLOW))
1130 expected_error = error;
1133 * If someone grew the LUN, the replacement may be too small.
1135 if (error == EOVERFLOW || error == EBUSY)
1136 expected_error = error;
1138 /* XXX workaround 6690467 */
1139 if (error != expected_error && expected_error != EBUSY) {
1140 fatal(0, "attach (%s %llu, %s %llu, %d) "
1141 "returned %d, expected %d",
1142 oldpath, (longlong_t)oldsize, newpath,
1143 (longlong_t)newsize, replacing, error, expected_error);
1146 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1150 * Verify that dynamic LUN growth works as expected.
1154 ztest_vdev_LUN_growth(ztest_args_t *za)
1156 spa_t *spa = za->za_spa;
1157 char dev_name[MAXPATHLEN];
1158 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
1163 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
1166 * Pick a random leaf vdev.
1168 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1169 vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves);
1170 spa_config_exit(spa, SCL_VDEV, FTAG);
1172 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
1174 if ((fd = open(dev_name, O_RDWR)) != -1) {
1176 * Determine the size.
1178 fsize = lseek(fd, 0, SEEK_END);
1181 * If it's less than 2x the original size, grow by around 3%.
1183 if (fsize < 2 * zopt_vdev_size) {
1184 size_t newsize = fsize + ztest_random(fsize / 32);
1185 (void) ftruncate(fd, newsize);
1186 if (zopt_verbose >= 6) {
1187 (void) printf("%s grew from %lu to %lu bytes\n",
1188 dev_name, (ulong_t)fsize, (ulong_t)newsize);
1194 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1199 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1202 * Create the directory object.
1204 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1205 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1206 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1208 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1209 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1211 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1212 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1216 ztest_destroy_cb(char *name, void *arg)
1218 ztest_args_t *za = arg;
1220 dmu_object_info_t *doi = &za->za_doi;
1224 * Verify that the dataset contains a directory object.
1226 error = dmu_objset_open(name, DMU_OST_OTHER,
1227 DS_MODE_USER | DS_MODE_READONLY, &os);
1228 ASSERT3U(error, ==, 0);
1229 error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1230 if (error != ENOENT) {
1231 /* We could have crashed in the middle of destroying it */
1232 ASSERT3U(error, ==, 0);
1233 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1234 ASSERT3S(doi->doi_physical_blks, >=, 0);
1236 dmu_objset_close(os);
1239 * Destroy the dataset.
1241 error = dmu_objset_destroy(name);
1243 (void) dmu_objset_open(name, DMU_OST_OTHER,
1244 DS_MODE_USER | DS_MODE_READONLY, &os);
1245 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", &os, error);
1251 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1254 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1261 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1262 namesize = strlen(name) + 1;
1264 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1265 ztest_random(ZIL_MAX_BLKSZ));
1266 lr = (lr_create_t *)&itx->itx_lr;
1267 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1268 lr->lr_doid = object;
1273 lr->lr_gen = dmu_tx_get_txg(tx);
1274 lr->lr_crtime[0] = time(NULL);
1275 lr->lr_crtime[1] = 0;
1277 bcopy(name, (char *)(lr + 1), namesize);
1279 return (zil_itx_assign(zilog, itx, tx));
1283 ztest_dmu_objset_create_destroy(ztest_args_t *za)
1288 int basemode, expected_error;
1294 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1295 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1296 (u_longlong_t)za->za_instance);
1298 basemode = DS_MODE_TYPE(za->za_instance);
1299 if (basemode != DS_MODE_USER && basemode != DS_MODE_OWNER)
1300 basemode = DS_MODE_USER;
1303 * If this dataset exists from a previous run, process its replay log
1304 * half of the time. If we don't replay it, then dmu_objset_destroy()
1305 * (invoked from ztest_destroy_cb() below) should just throw it away.
1307 if (ztest_random(2) == 0 &&
1308 dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os) == 0) {
1310 zil_replay(os, &zr, &zr.zr_assign, ztest_replay_vector, NULL);
1311 dmu_objset_close(os);
1315 * There may be an old instance of the dataset we're about to
1316 * create lying around from a previous run. If so, destroy it
1317 * and all of its snapshots.
1319 (void) dmu_objset_find(name, ztest_destroy_cb, za,
1320 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1323 * Verify that the destroyed dataset is no longer in the namespace.
1325 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1326 if (error != ENOENT)
1327 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1331 * Verify that we can create a new dataset.
1333 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
1334 ztest_create_cb, NULL);
1336 if (error == ENOSPC) {
1337 ztest_record_enospc("dmu_objset_create");
1338 (void) rw_unlock(&ztest_shared->zs_name_lock);
1341 fatal(0, "dmu_objset_create(%s) = %d", name, error);
1344 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1346 fatal(0, "dmu_objset_open(%s) = %d", name, error);
1350 * Open the intent log for it.
1352 zilog = zil_open(os, NULL);
1355 * Put a random number of objects in there.
1357 objects = ztest_random(20);
1359 while (objects-- != 0) {
1361 dmu_tx_t *tx = dmu_tx_create(os);
1362 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1363 error = dmu_tx_assign(tx, TXG_WAIT);
1367 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1368 DMU_OT_NONE, 0, tx);
1369 ztest_set_random_blocksize(os, object, tx);
1370 seq = ztest_log_create(zilog, tx, object,
1371 DMU_OT_UINT64_OTHER);
1372 dmu_write(os, object, 0, sizeof (name), name, tx);
1375 if (ztest_random(5) == 0) {
1376 zil_commit(zilog, seq, object);
1378 if (ztest_random(100) == 0) {
1379 error = zil_suspend(zilog);
1387 * Verify that we cannot create an existing dataset.
1389 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, NULL, NULL);
1390 if (error != EEXIST)
1391 fatal(0, "created existing dataset, error = %d", error);
1394 * Verify that multiple dataset holds are allowed, but only when
1395 * the new access mode is compatible with the base mode.
1397 if (basemode == DS_MODE_OWNER) {
1398 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_USER,
1401 fatal(0, "dmu_objset_open('%s') = %d", name, error);
1403 dmu_objset_close(os2);
1405 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os2);
1406 expected_error = (basemode == DS_MODE_OWNER) ? EBUSY : 0;
1407 if (error != expected_error)
1408 fatal(0, "dmu_objset_open('%s') = %d, expected %d",
1409 name, error, expected_error);
1411 dmu_objset_close(os2);
1414 dmu_objset_close(os);
1416 error = dmu_objset_destroy(name);
1418 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1420 (void) rw_unlock(&ztest_shared->zs_name_lock);
1424 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1427 ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1430 objset_t *os = za->za_os;
1432 char osname[MAXNAMELEN];
1434 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1435 dmu_objset_name(os, osname);
1436 (void) snprintf(snapname, 100, "%s@%llu", osname,
1437 (u_longlong_t)za->za_instance);
1439 error = dmu_objset_destroy(snapname);
1440 if (error != 0 && error != ENOENT)
1441 fatal(0, "dmu_objset_destroy() = %d", error);
1442 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE);
1443 if (error == ENOSPC)
1444 ztest_record_enospc("dmu_take_snapshot");
1445 else if (error != 0 && error != EEXIST)
1446 fatal(0, "dmu_take_snapshot() = %d", error);
1447 (void) rw_unlock(&ztest_shared->zs_name_lock);
1450 #define ZTEST_TRAVERSE_BLOCKS 1000
1453 ztest_blk_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1455 ztest_args_t *za = arg;
1456 zbookmark_t *zb = &bc->bc_bookmark;
1457 blkptr_t *bp = &bc->bc_blkptr;
1458 dnode_phys_t *dnp = bc->bc_dnode;
1459 traverse_handle_t *th = za->za_th;
1460 uint64_t size = BP_GET_LSIZE(bp);
1463 * Level -1 indicates the objset_phys_t or something in its intent log.
1465 if (zb->zb_level == -1) {
1466 if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
1467 ASSERT3U(zb->zb_object, ==, 0);
1468 ASSERT3U(zb->zb_blkid, ==, 0);
1469 ASSERT3U(size, ==, sizeof (objset_phys_t));
1471 } else if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) {
1472 ASSERT3U(zb->zb_object, ==, 0);
1473 ASSERT3U(zb->zb_blkid, >, za->za_zil_seq);
1474 za->za_zil_seq = zb->zb_blkid;
1476 ASSERT3U(zb->zb_object, !=, 0); /* lr_write_t */
1482 ASSERT(dnp != NULL);
1488 * Once in a while, abort the traverse. We only do this to odd
1489 * instance numbers to ensure that even ones can run to completion.
1491 if ((za->za_instance & 1) && ztest_random(10000) == 0)
1494 if (bp->blk_birth == 0) {
1495 ASSERT(th->th_advance & ADVANCE_HOLES);
1499 if (zb->zb_level == 0 && !(th->th_advance & ADVANCE_DATA) &&
1500 bc == &th->th_cache[ZB_DN_CACHE][0]) {
1501 ASSERT(bc->bc_data == NULL);
1505 ASSERT(bc->bc_data != NULL);
1508 * This is an expensive question, so don't ask it too often.
1510 if (((za->za_random ^ th->th_callbacks) & 0xff) == 0) {
1511 void *xbuf = umem_alloc(size, UMEM_NOFAIL);
1512 if (arc_tryread(spa, bp, xbuf) == 0) {
1513 ASSERT(bcmp(bc->bc_data, xbuf, size) == 0);
1515 umem_free(xbuf, size);
1518 if (zb->zb_level > 0) {
1519 ASSERT3U(size, ==, 1ULL << dnp->dn_indblkshift);
1523 ASSERT(zb->zb_level == 0);
1524 ASSERT3U(size, ==, dnp->dn_datablkszsec << DEV_BSHIFT);
1530 * Verify that live pool traversal works.
1533 ztest_traverse(ztest_args_t *za)
1535 spa_t *spa = za->za_spa;
1536 traverse_handle_t *th = za->za_th;
1538 uint64_t cbstart, cblimit;
1543 if (ztest_random(2) == 0)
1544 advance |= ADVANCE_PRE;
1546 if (ztest_random(2) == 0)
1547 advance |= ADVANCE_PRUNE;
1549 if (ztest_random(2) == 0)
1550 advance |= ADVANCE_DATA;
1552 if (ztest_random(2) == 0)
1553 advance |= ADVANCE_HOLES;
1555 if (ztest_random(2) == 0)
1556 advance |= ADVANCE_ZIL;
1558 th = za->za_th = traverse_init(spa, ztest_blk_cb, za, advance,
1561 traverse_add_pool(th, 0, -1ULL);
1564 advance = th->th_advance;
1565 cbstart = th->th_callbacks;
1566 cblimit = cbstart + ((advance & ADVANCE_DATA) ? 100 : 1000);
1568 while ((rc = traverse_more(th)) == EAGAIN && th->th_callbacks < cblimit)
1571 if (zopt_verbose >= 5)
1572 (void) printf("traverse %s%s%s%s %llu blocks to "
1573 "<%llu, %llu, %lld, %llx>%s\n",
1574 (advance & ADVANCE_PRE) ? "pre" : "post",
1575 (advance & ADVANCE_PRUNE) ? "|prune" : "",
1576 (advance & ADVANCE_DATA) ? "|data" : "",
1577 (advance & ADVANCE_HOLES) ? "|holes" : "",
1578 (u_longlong_t)(th->th_callbacks - cbstart),
1579 (u_longlong_t)th->th_lastcb.zb_objset,
1580 (u_longlong_t)th->th_lastcb.zb_object,
1581 (u_longlong_t)th->th_lastcb.zb_level,
1582 (u_longlong_t)th->th_lastcb.zb_blkid,
1583 rc == 0 ? " [done]" :
1584 rc == EINTR ? " [aborted]" :
1589 if (rc != 0 && rc != EINTR)
1590 fatal(0, "traverse_more(%p) = %d", th, rc);
1597 * Verify dsl_dataset_promote handles EBUSY
1600 ztest_dsl_dataset_promote_busy(ztest_args_t *za)
1603 objset_t *os = za->za_os;
1606 char snap1name[100];
1607 char clone1name[100];
1608 char snap2name[100];
1609 char clone2name[100];
1610 char snap3name[100];
1611 char osname[MAXNAMELEN];
1612 static uint64_t uniq = 0;
1615 curval = atomic_add_64_nv(&uniq, 5) - 5;
1617 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1619 dmu_objset_name(os, osname);
1620 (void) snprintf(snap1name, 100, "%s@s1_%llu", osname, curval++);
1621 (void) snprintf(clone1name, 100, "%s/c1_%llu", osname, curval++);
1622 (void) snprintf(snap2name, 100, "%s@s2_%llu", clone1name, curval++);
1623 (void) snprintf(clone2name, 100, "%s/c2_%llu", osname, curval++);
1624 (void) snprintf(snap3name, 100, "%s@s3_%llu", clone1name, curval++);
1626 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1, FALSE);
1627 if (error == ENOSPC)
1628 ztest_record_enospc("dmu_take_snapshot");
1629 else if (error != 0 && error != EEXIST)
1630 fatal(0, "dmu_take_snapshot = %d", error);
1632 error = dmu_objset_open(snap1name, DMU_OST_OTHER,
1633 DS_MODE_USER | DS_MODE_READONLY, &clone);
1635 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
1637 error = dmu_objset_create(clone1name, DMU_OST_OTHER, clone, 0,
1640 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
1641 dmu_objset_close(clone);
1643 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
1645 if (error == ENOSPC)
1646 ztest_record_enospc("dmu_take_snapshot");
1647 else if (error != 0 && error != EEXIST)
1648 fatal(0, "dmu_take_snapshot = %d", error);
1650 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
1652 if (error == ENOSPC)
1653 ztest_record_enospc("dmu_take_snapshot");
1654 else if (error != 0 && error != EEXIST)
1655 fatal(0, "dmu_take_snapshot = %d", error);
1657 error = dmu_objset_open(snap3name, DMU_OST_OTHER,
1658 DS_MODE_USER | DS_MODE_READONLY, &clone);
1660 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
1662 error = dmu_objset_create(clone2name, DMU_OST_OTHER, clone, 0,
1665 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
1666 dmu_objset_close(clone);
1668 error = dsl_dataset_own(snap1name, 0, FTAG, &ds);
1670 fatal(0, "dsl_dataset_own(%s) = %d", snap1name, error);
1671 error = dsl_dataset_promote(clone2name);
1673 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
1675 dsl_dataset_disown(ds, FTAG);
1677 error = dmu_objset_destroy(clone2name);
1679 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
1681 error = dmu_objset_destroy(snap3name);
1683 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
1685 error = dmu_objset_destroy(snap2name);
1687 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
1689 error = dmu_objset_destroy(clone1name);
1691 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
1692 error = dmu_objset_destroy(snap1name);
1694 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
1696 (void) rw_unlock(&ztest_shared->zs_name_lock);
1700 * Verify that dmu_object_{alloc,free} work as expected.
1703 ztest_dmu_object_alloc_free(ztest_args_t *za)
1705 objset_t *os = za->za_os;
1708 uint64_t batchobj, object, batchsize, endoff, temp;
1709 int b, c, error, bonuslen;
1710 dmu_object_info_t *doi = &za->za_doi;
1711 char osname[MAXNAMELEN];
1713 dmu_objset_name(os, osname);
1719 * Create a batch object if necessary, and record it in the directory.
1721 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1722 sizeof (uint64_t), &batchobj));
1723 if (batchobj == 0) {
1724 tx = dmu_tx_create(os);
1725 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1727 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1728 error = dmu_tx_assign(tx, TXG_WAIT);
1730 ztest_record_enospc("create a batch object");
1734 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1735 DMU_OT_NONE, 0, tx);
1736 ztest_set_random_blocksize(os, batchobj, tx);
1737 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1738 sizeof (uint64_t), &batchobj, tx);
1743 * Destroy the previous batch of objects.
1745 for (b = 0; b < batchsize; b++) {
1746 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
1747 sizeof (uint64_t), &object));
1751 * Read and validate contents.
1752 * We expect the nth byte of the bonus buffer to be n.
1754 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1757 dmu_object_info_from_db(db, doi);
1758 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1759 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1760 ASSERT3S(doi->doi_physical_blks, >=, 0);
1762 bonuslen = doi->doi_bonus_size;
1764 for (c = 0; c < bonuslen; c++) {
1765 if (((uint8_t *)db->db_data)[c] !=
1766 (uint8_t)(c + bonuslen)) {
1768 "bad bonus: %s, obj %llu, off %d: %u != %u",
1770 ((uint8_t *)db->db_data)[c],
1771 (uint8_t)(c + bonuslen));
1775 dmu_buf_rele(db, FTAG);
1779 * We expect the word at endoff to be our object number.
1781 VERIFY(0 == dmu_read(os, object, endoff,
1782 sizeof (uint64_t), &temp));
1784 if (temp != object) {
1785 fatal(0, "bad data in %s, got %llu, expected %llu",
1786 osname, temp, object);
1790 * Destroy old object and clear batch entry.
1792 tx = dmu_tx_create(os);
1793 dmu_tx_hold_write(tx, batchobj,
1794 b * sizeof (uint64_t), sizeof (uint64_t));
1795 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1796 error = dmu_tx_assign(tx, TXG_WAIT);
1798 ztest_record_enospc("free object");
1802 error = dmu_object_free(os, object, tx);
1804 fatal(0, "dmu_object_free('%s', %llu) = %d",
1805 osname, object, error);
1809 dmu_object_set_checksum(os, batchobj,
1810 ztest_random_checksum(), tx);
1811 dmu_object_set_compress(os, batchobj,
1812 ztest_random_compress(), tx);
1814 dmu_write(os, batchobj, b * sizeof (uint64_t),
1815 sizeof (uint64_t), &object, tx);
1821 * Before creating the new batch of objects, generate a bunch of churn.
1823 for (b = ztest_random(100); b > 0; b--) {
1824 tx = dmu_tx_create(os);
1825 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1826 error = dmu_tx_assign(tx, TXG_WAIT);
1828 ztest_record_enospc("churn objects");
1832 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1833 DMU_OT_NONE, 0, tx);
1834 ztest_set_random_blocksize(os, object, tx);
1835 error = dmu_object_free(os, object, tx);
1837 fatal(0, "dmu_object_free('%s', %llu) = %d",
1838 osname, object, error);
1844 * Create a new batch of objects with randomly chosen
1845 * blocksizes and record them in the batch directory.
1847 for (b = 0; b < batchsize; b++) {
1848 uint32_t va_blksize;
1849 u_longlong_t va_nblocks;
1851 tx = dmu_tx_create(os);
1852 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1854 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1855 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1857 error = dmu_tx_assign(tx, TXG_WAIT);
1859 ztest_record_enospc("create batchobj");
1863 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1865 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1866 DMU_OT_PLAIN_OTHER, bonuslen, tx);
1868 ztest_set_random_blocksize(os, object, tx);
1870 dmu_object_set_checksum(os, object,
1871 ztest_random_checksum(), tx);
1872 dmu_object_set_compress(os, object,
1873 ztest_random_compress(), tx);
1875 dmu_write(os, batchobj, b * sizeof (uint64_t),
1876 sizeof (uint64_t), &object, tx);
1879 * Write to both the bonus buffer and the regular data.
1881 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1883 ASSERT3U(bonuslen, <=, db->db_size);
1885 dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1886 ASSERT3S(va_nblocks, >=, 0);
1888 dmu_buf_will_dirty(db, tx);
1891 * See comments above regarding the contents of
1892 * the bonus buffer and the word at endoff.
1894 for (c = 0; c < bonuslen; c++)
1895 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1897 dmu_buf_rele(db, FTAG);
1901 * Write to a large offset to increase indirection.
1903 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1910 * Verify that dmu_{read,write} work as expected.
1912 typedef struct bufwad {
1918 typedef struct dmu_read_write_dir {
1919 uint64_t dd_packobj;
1922 } dmu_read_write_dir_t;
1925 ztest_dmu_read_write(ztest_args_t *za)
1927 objset_t *os = za->za_os;
1928 dmu_read_write_dir_t dd;
1930 int i, freeit, error;
1932 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1933 uint64_t packoff, packsize, bigoff, bigsize;
1934 uint64_t regions = 997;
1935 uint64_t stride = 123456789ULL;
1936 uint64_t width = 40;
1937 int free_percent = 5;
1940 * This test uses two objects, packobj and bigobj, that are always
1941 * updated together (i.e. in the same tx) so that their contents are
1942 * in sync and can be compared. Their contents relate to each other
1943 * in a simple way: packobj is a dense array of 'bufwad' structures,
1944 * while bigobj is a sparse array of the same bufwads. Specifically,
1945 * for any index n, there are three bufwads that should be identical:
1947 * packobj, at offset n * sizeof (bufwad_t)
1948 * bigobj, at the head of the nth chunk
1949 * bigobj, at the tail of the nth chunk
1951 * The chunk size is arbitrary. It doesn't have to be a power of two,
1952 * and it doesn't have any relation to the object blocksize.
1953 * The only requirement is that it can hold at least two bufwads.
1955 * Normally, we write the bufwad to each of these locations.
1956 * However, free_percent of the time we instead write zeroes to
1957 * packobj and perform a dmu_free_range() on bigobj. By comparing
1958 * bigobj to packobj, we can verify that the DMU is correctly
1959 * tracking which parts of an object are allocated and free,
1960 * and that the contents of the allocated blocks are correct.
1964 * Read the directory info. If it's the first time, set things up.
1966 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1968 if (dd.dd_chunk == 0) {
1969 ASSERT(dd.dd_packobj == 0);
1970 ASSERT(dd.dd_bigobj == 0);
1971 tx = dmu_tx_create(os);
1972 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1973 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1974 error = dmu_tx_assign(tx, TXG_WAIT);
1976 ztest_record_enospc("create r/w directory");
1981 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1982 DMU_OT_NONE, 0, tx);
1983 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1984 DMU_OT_NONE, 0, tx);
1985 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
1987 ztest_set_random_blocksize(os, dd.dd_packobj, tx);
1988 ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
1990 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
1996 * Prefetch a random chunk of the big object.
1997 * Our aim here is to get some async reads in flight
1998 * for blocks that we may free below; the DMU should
1999 * handle this race correctly.
2001 n = ztest_random(regions) * stride + ztest_random(width);
2002 s = 1 + ztest_random(2 * width - 1);
2003 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
2006 * Pick a random index and compute the offsets into packobj and bigobj.
2008 n = ztest_random(regions) * stride + ztest_random(width);
2009 s = 1 + ztest_random(width - 1);
2011 packoff = n * sizeof (bufwad_t);
2012 packsize = s * sizeof (bufwad_t);
2014 bigoff = n * dd.dd_chunk;
2015 bigsize = s * dd.dd_chunk;
2017 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
2018 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
2021 * free_percent of the time, free a range of bigobj rather than
2024 freeit = (ztest_random(100) < free_percent);
2027 * Read the current contents of our objects.
2029 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf);
2030 ASSERT3U(error, ==, 0);
2031 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf);
2032 ASSERT3U(error, ==, 0);
2035 * Get a tx for the mods to both packobj and bigobj.
2037 tx = dmu_tx_create(os);
2039 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
2042 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
2044 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
2046 error = dmu_tx_assign(tx, TXG_WAIT);
2049 ztest_record_enospc("dmu r/w range");
2051 umem_free(packbuf, packsize);
2052 umem_free(bigbuf, bigsize);
2056 txg = dmu_tx_get_txg(tx);
2059 * For each index from n to n + s, verify that the existing bufwad
2060 * in packobj matches the bufwads at the head and tail of the
2061 * corresponding chunk in bigobj. Then update all three bufwads
2062 * with the new values we want to write out.
2064 for (i = 0; i < s; i++) {
2066 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
2068 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
2070 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
2072 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
2073 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
2075 if (pack->bw_txg > txg)
2076 fatal(0, "future leak: got %llx, open txg is %llx",
2079 if (pack->bw_data != 0 && pack->bw_index != n + i)
2080 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
2081 pack->bw_index, n, i);
2083 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
2084 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
2086 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
2087 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
2090 bzero(pack, sizeof (bufwad_t));
2092 pack->bw_index = n + i;
2094 pack->bw_data = 1 + ztest_random(-2ULL);
2101 * We've verified all the old bufwads, and made new ones.
2102 * Now write them out.
2104 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
2107 if (zopt_verbose >= 6) {
2108 (void) printf("freeing offset %llx size %llx"
2110 (u_longlong_t)bigoff,
2111 (u_longlong_t)bigsize,
2114 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
2117 if (zopt_verbose >= 6) {
2118 (void) printf("writing offset %llx size %llx"
2120 (u_longlong_t)bigoff,
2121 (u_longlong_t)bigsize,
2124 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
2130 * Sanity check the stuff we just wrote.
2133 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
2134 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
2136 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
2137 packsize, packcheck));
2138 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
2139 bigsize, bigcheck));
2141 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
2142 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
2144 umem_free(packcheck, packsize);
2145 umem_free(bigcheck, bigsize);
2148 umem_free(packbuf, packsize);
2149 umem_free(bigbuf, bigsize);
2153 ztest_dmu_check_future_leak(ztest_args_t *za)
2155 objset_t *os = za->za_os;
2157 ztest_block_tag_t *bt;
2158 dmu_object_info_t *doi = &za->za_doi;
2161 * Make sure that, if there is a write record in the bonus buffer
2162 * of the ZTEST_DIROBJ, that the txg for this record is <= the
2163 * last synced txg of the pool.
2165 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2167 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
2168 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
2169 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2170 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
2171 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
2172 if (bt->bt_objset != 0) {
2173 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
2174 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
2175 ASSERT3U(bt->bt_offset, ==, -1ULL);
2176 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
2178 dmu_buf_rele(db, FTAG);
2183 ztest_dmu_write_parallel(ztest_args_t *za)
2185 objset_t *os = za->za_os;
2186 ztest_block_tag_t *rbt = &za->za_rbt;
2187 ztest_block_tag_t *wbt = &za->za_wbt;
2188 const size_t btsize = sizeof (ztest_block_tag_t);
2191 int bs = ZTEST_DIROBJ_BLOCKSIZE;
2193 uint64_t off, txg, txg_how;
2195 char osname[MAXNAMELEN];
2196 char iobuf[SPA_MAXBLOCKSIZE];
2197 blkptr_t blk = { 0 };
2200 dmu_tx_t *tx = dmu_tx_create(os);
2202 dmu_objset_name(os, osname);
2205 * Have multiple threads write to large offsets in ZTEST_DIROBJ
2206 * to verify that having multiple threads writing to the same object
2207 * in parallel doesn't cause any trouble.
2209 if (ztest_random(4) == 0) {
2211 * Do the bonus buffer instead of a regular block.
2212 * We need a lock to serialize resize vs. others,
2213 * so we hash on the objset ID.
2215 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
2217 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
2219 b = ztest_random(ZTEST_SYNC_LOCKS);
2220 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
2221 if (ztest_random(4) == 0) {
2223 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
2225 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
2229 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
2230 error = dmu_tx_assign(tx, txg_how);
2232 if (error == ERESTART) {
2233 ASSERT(txg_how == TXG_NOWAIT);
2236 ztest_record_enospc("dmu write parallel");
2241 txg = dmu_tx_get_txg(tx);
2243 lp = &ztest_shared->zs_sync_lock[b];
2244 (void) mutex_lock(lp);
2246 wbt->bt_objset = dmu_objset_id(os);
2247 wbt->bt_object = ZTEST_DIROBJ;
2248 wbt->bt_offset = off;
2250 wbt->bt_thread = za->za_instance;
2251 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */
2254 * Occasionally, write an all-zero block to test the behavior
2255 * of blocks that compress into holes.
2257 if (off != -1ULL && ztest_random(8) == 0)
2261 dmu_object_info_t *doi = &za->za_doi;
2264 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2266 dmu_object_info_from_db(db, doi);
2267 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2268 ASSERT3U(doi->doi_bonus_size, >=, btsize);
2269 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2270 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2271 bcopy(dboff, rbt, btsize);
2272 if (rbt->bt_objset != 0) {
2273 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2274 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2275 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2276 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2278 if (ztest_random(10) == 0) {
2279 int newsize = (ztest_random(db->db_size /
2280 btsize) + 1) * btsize;
2282 ASSERT3U(newsize, >=, btsize);
2283 ASSERT3U(newsize, <=, db->db_size);
2284 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2285 dboff = (char *)db->db_data + newsize - btsize;
2287 dmu_buf_will_dirty(db, tx);
2288 bcopy(wbt, dboff, btsize);
2289 dmu_buf_rele(db, FTAG);
2291 } else if (do_free) {
2292 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2294 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2297 (void) mutex_unlock(lp);
2299 if (ztest_random(1000) == 0)
2300 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
2304 if (ztest_random(10000) == 0)
2305 txg_wait_synced(dmu_objset_pool(os), txg);
2307 if (off == -1ULL || do_free)
2310 if (ztest_random(2) != 0)
2314 * dmu_sync() the block we just wrote.
2316 (void) mutex_lock(lp);
2318 blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2319 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2322 dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n",
2323 osname, ZTEST_DIROBJ, blkoff, error);
2324 (void) mutex_unlock(lp);
2327 blkoff = off - blkoff;
2328 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2329 dmu_buf_rele(db, FTAG);
2332 (void) mutex_unlock(lp);
2335 dprintf("dmu_sync(%s, %d, %llx) = %d\n",
2336 osname, ZTEST_DIROBJ, off, error);
2340 if (blk.blk_birth == 0) /* concurrent free */
2343 txg_suspend(dmu_objset_pool(os));
2345 ASSERT(blk.blk_fill == 1);
2346 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2347 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2348 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2351 * Read the block that dmu_sync() returned to make sure its contents
2352 * match what we wrote. We do this while still txg_suspend()ed
2353 * to ensure that the block can't be reused before we read it.
2355 zb.zb_objset = dmu_objset_id(os);
2356 zb.zb_object = ZTEST_DIROBJ;
2358 zb.zb_blkid = off / bs;
2359 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2360 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2361 ASSERT3U(error, ==, 0);
2363 txg_resume(dmu_objset_pool(os));
2365 bcopy(&iobuf[blkoff], rbt, btsize);
2367 if (rbt->bt_objset == 0) /* concurrent free */
2370 if (wbt->bt_objset == 0) /* all-zero overwrite */
2373 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2374 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2375 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2378 * The semantic of dmu_sync() is that we always push the most recent
2379 * version of the data, so in the face of concurrent updates we may
2380 * see a newer version of the block. That's OK.
2382 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2383 if (rbt->bt_thread == wbt->bt_thread)
2384 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2386 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2390 * Verify that zap_{create,destroy,add,remove,update} work as expected.
2392 #define ZTEST_ZAP_MIN_INTS 1
2393 #define ZTEST_ZAP_MAX_INTS 4
2394 #define ZTEST_ZAP_MAX_PROPS 1000
2397 ztest_zap(ztest_args_t *za)
2399 objset_t *os = za->za_os;
2401 uint64_t txg, last_txg;
2402 uint64_t value[ZTEST_ZAP_MAX_INTS];
2403 uint64_t zl_ints, zl_intsize, prop;
2406 char propname[100], txgname[100];
2408 char osname[MAXNAMELEN];
2409 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2411 dmu_objset_name(os, osname);
2414 * Create a new object if necessary, and record it in the directory.
2416 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2417 sizeof (uint64_t), &object));
2420 tx = dmu_tx_create(os);
2421 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2423 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2424 error = dmu_tx_assign(tx, TXG_WAIT);
2426 ztest_record_enospc("create zap test obj");
2430 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2432 fatal(0, "zap_create('%s', %llu) = %d",
2433 osname, object, error);
2435 ASSERT(object != 0);
2436 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2437 sizeof (uint64_t), &object, tx);
2439 * Generate a known hash collision, and verify that
2440 * we can lookup and remove both entries.
2442 for (i = 0; i < 2; i++) {
2444 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2446 ASSERT3U(error, ==, 0);
2448 for (i = 0; i < 2; i++) {
2449 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2451 ASSERT3U(error, ==, EEXIST);
2452 error = zap_length(os, object, hc[i],
2453 &zl_intsize, &zl_ints);
2454 ASSERT3U(error, ==, 0);
2455 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2456 ASSERT3U(zl_ints, ==, 1);
2458 for (i = 0; i < 2; i++) {
2459 error = zap_remove(os, object, hc[i], tx);
2460 ASSERT3U(error, ==, 0);
2466 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2468 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2469 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2470 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2471 bzero(value, sizeof (value));
2475 * If these zap entries already exist, validate their contents.
2477 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2479 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2480 ASSERT3U(zl_ints, ==, 1);
2482 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2483 zl_ints, &last_txg) == 0);
2485 VERIFY(zap_length(os, object, propname, &zl_intsize,
2488 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2489 ASSERT3U(zl_ints, ==, ints);
2491 VERIFY(zap_lookup(os, object, propname, zl_intsize,
2492 zl_ints, value) == 0);
2494 for (i = 0; i < ints; i++) {
2495 ASSERT3U(value[i], ==, last_txg + object + i);
2498 ASSERT3U(error, ==, ENOENT);
2502 * Atomically update two entries in our zap object.
2503 * The first is named txg_%llu, and contains the txg
2504 * in which the property was last updated. The second
2505 * is named prop_%llu, and the nth element of its value
2506 * should be txg + object + n.
2508 tx = dmu_tx_create(os);
2509 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2510 error = dmu_tx_assign(tx, TXG_WAIT);
2512 ztest_record_enospc("create zap entry");
2516 txg = dmu_tx_get_txg(tx);
2519 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2521 for (i = 0; i < ints; i++)
2522 value[i] = txg + object + i;
2524 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2526 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2527 osname, object, txgname, error);
2529 error = zap_update(os, object, propname, sizeof (uint64_t),
2532 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2533 osname, object, propname, error);
2538 * Remove a random pair of entries.
2540 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2541 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2542 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2544 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2546 if (error == ENOENT)
2549 ASSERT3U(error, ==, 0);
2551 tx = dmu_tx_create(os);
2552 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2553 error = dmu_tx_assign(tx, TXG_WAIT);
2555 ztest_record_enospc("remove zap entry");
2559 error = zap_remove(os, object, txgname, tx);
2561 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2562 osname, object, txgname, error);
2564 error = zap_remove(os, object, propname, tx);
2566 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2567 osname, object, propname, error);
2572 * Once in a while, destroy the object.
2574 if (ztest_random(1000) != 0)
2577 tx = dmu_tx_create(os);
2578 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2579 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2580 error = dmu_tx_assign(tx, TXG_WAIT);
2582 ztest_record_enospc("destroy zap object");
2586 error = zap_destroy(os, object, tx);
2588 fatal(0, "zap_destroy('%s', %llu) = %d",
2589 osname, object, error);
2591 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2597 ztest_zap_parallel(ztest_args_t *za)
2599 objset_t *os = za->za_os;
2600 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2602 int i, namelen, error;
2603 char name[20], string_value[20];
2607 * Generate a random name of the form 'xxx.....' where each
2608 * x is a random printable character and the dots are dots.
2609 * There are 94 such characters, and the name length goes from
2610 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2612 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2614 for (i = 0; i < 3; i++)
2615 name[i] = '!' + ztest_random('~' - '!' + 1);
2616 for (; i < namelen - 1; i++)
2620 if (ztest_random(2) == 0)
2621 object = ZTEST_MICROZAP_OBJ;
2623 object = ZTEST_FATZAP_OBJ;
2625 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2626 wsize = sizeof (txg);
2632 data = string_value;
2636 VERIFY(zap_count(os, object, &count) == 0);
2637 ASSERT(count != -1ULL);
2640 * Select an operation: length, lookup, add, update, remove.
2642 i = ztest_random(5);
2645 tx = dmu_tx_create(os);
2646 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2647 error = dmu_tx_assign(tx, TXG_WAIT);
2649 ztest_record_enospc("zap parallel");
2653 txg = dmu_tx_get_txg(tx);
2654 bcopy(name, string_value, namelen);
2658 bzero(string_value, namelen);
2664 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
2666 ASSERT3U(wsize, ==, zl_wsize);
2667 ASSERT3U(wc, ==, zl_wc);
2669 ASSERT3U(error, ==, ENOENT);
2674 error = zap_lookup(os, object, name, wsize, wc, data);
2676 if (data == string_value &&
2677 bcmp(name, data, namelen) != 0)
2678 fatal(0, "name '%s' != val '%s' len %d",
2679 name, data, namelen);
2681 ASSERT3U(error, ==, ENOENT);
2686 error = zap_add(os, object, name, wsize, wc, data, tx);
2687 ASSERT(error == 0 || error == EEXIST);
2691 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
2695 error = zap_remove(os, object, name, tx);
2696 ASSERT(error == 0 || error == ENOENT);
2705 ztest_dsl_prop_get_set(ztest_args_t *za)
2707 objset_t *os = za->za_os;
2710 const char *prop, *valname;
2711 char setpoint[MAXPATHLEN];
2712 char osname[MAXNAMELEN];
2715 (void) rw_rdlock(&ztest_shared->zs_name_lock);
2717 dmu_objset_name(os, osname);
2719 for (i = 0; i < 2; i++) {
2722 value = ztest_random_checksum();
2723 inherit = (value == ZIO_CHECKSUM_INHERIT);
2725 prop = "compression";
2726 value = ztest_random_compress();
2727 inherit = (value == ZIO_COMPRESS_INHERIT);
2730 error = dsl_prop_set(osname, prop, sizeof (value),
2733 if (error == ENOSPC) {
2734 ztest_record_enospc("dsl_prop_set");
2738 ASSERT3U(error, ==, 0);
2740 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
2741 1, &value, setpoint), ==, 0);
2744 valname = zio_checksum_table[value].ci_name;
2746 valname = zio_compress_table[value].ci_name;
2748 if (zopt_verbose >= 6) {
2749 (void) printf("%s %s = %s for '%s'\n",
2750 osname, prop, valname, setpoint);
2754 (void) rw_unlock(&ztest_shared->zs_name_lock);
2758 * Inject random faults into the on-disk data.
2761 ztest_fault_inject(ztest_args_t *za)
2765 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2766 uint64_t bad = 0x1990c0ffeedecadeULL;
2768 char path0[MAXPATHLEN];
2769 char pathrand[MAXPATHLEN];
2771 spa_t *spa = za->za_spa;
2772 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
2774 int maxfaults = zopt_maxfaults;
2778 ASSERT(leaves >= 1);
2781 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
2783 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
2785 if (ztest_random(2) == 0) {
2787 * Inject errors on a normal data device.
2789 top = ztest_random(spa->spa_root_vdev->vdev_children);
2790 leaf = ztest_random(leaves);
2793 * Generate paths to the first leaf in this top-level vdev,
2794 * and to the random leaf we selected. We'll induce transient
2795 * write failures and random online/offline activity on leaf 0,
2796 * and we'll write random garbage to the randomly chosen leaf.
2798 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
2799 zopt_dir, zopt_pool, top * leaves + 0);
2800 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
2801 zopt_dir, zopt_pool, top * leaves + leaf);
2803 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
2804 if (vd0 != NULL && maxfaults != 1) {
2806 * Make vd0 explicitly claim to be unreadable,
2807 * or unwriteable, or reach behind its back
2808 * and close the underlying fd. We can do this if
2809 * maxfaults == 0 because we'll fail and reexecute,
2810 * and we can do it if maxfaults >= 2 because we'll
2811 * have enough redundancy. If maxfaults == 1, the
2812 * combination of this with injection of random data
2813 * corruption below exceeds the pool's fault tolerance.
2815 vdev_file_t *vf = vd0->vdev_tsd;
2817 if (vf != NULL && ztest_random(3) == 0) {
2818 (void) close(vf->vf_vnode->v_fd);
2819 vf->vf_vnode->v_fd = -1;
2820 } else if (ztest_random(2) == 0) {
2821 vd0->vdev_cant_read = B_TRUE;
2823 vd0->vdev_cant_write = B_TRUE;
2825 guid0 = vd0->vdev_guid;
2829 * Inject errors on an l2cache device.
2831 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2833 if (sav->sav_count == 0) {
2834 spa_config_exit(spa, SCL_STATE, FTAG);
2837 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
2838 guid0 = vd0->vdev_guid;
2839 (void) strcpy(path0, vd0->vdev_path);
2840 (void) strcpy(pathrand, vd0->vdev_path);
2844 maxfaults = INT_MAX; /* no limit on cache devices */
2847 dprintf("damaging %s and %s\n", path0, pathrand);
2849 spa_config_exit(spa, SCL_STATE, FTAG);
2855 * If we can tolerate two or more faults, randomly online/offline vd0.
2857 if (maxfaults >= 2 && guid0 != 0) {
2858 if (ztest_random(10) < 6)
2859 (void) vdev_offline(spa, guid0, B_TRUE);
2861 (void) vdev_online(spa, guid0, B_FALSE, NULL);
2865 * We have at least single-fault tolerance, so inject data corruption.
2867 fd = open(pathrand, O_RDWR);
2869 if (fd == -1) /* we hit a gap in the device namespace */
2872 fsize = lseek(fd, 0, SEEK_END);
2874 while (--iters != 0) {
2875 offset = ztest_random(fsize / (leaves << bshift)) *
2876 (leaves << bshift) + (leaf << bshift) +
2877 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
2879 if (offset >= fsize)
2882 if (zopt_verbose >= 6)
2883 (void) printf("injecting bad word into %s,"
2884 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
2886 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
2887 fatal(1, "can't inject bad word at 0x%llx in %s",
2898 ztest_scrub(ztest_args_t *za)
2900 spa_t *spa = za->za_spa;
2902 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2903 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
2904 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2908 * Rename the pool to a different name and then rename it back.
2911 ztest_spa_rename(ztest_args_t *za)
2913 char *oldname, *newname;
2917 (void) rw_wrlock(&ztest_shared->zs_name_lock);
2919 oldname = za->za_pool;
2920 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
2921 (void) strcpy(newname, oldname);
2922 (void) strcat(newname, "_tmp");
2927 error = spa_rename(oldname, newname);
2929 fatal(0, "spa_rename('%s', '%s') = %d", oldname,
2933 * Try to open it under the old name, which shouldn't exist
2935 error = spa_open(oldname, &spa, FTAG);
2936 if (error != ENOENT)
2937 fatal(0, "spa_open('%s') = %d", oldname, error);
2940 * Open it under the new name and make sure it's still the same spa_t.
2942 error = spa_open(newname, &spa, FTAG);
2944 fatal(0, "spa_open('%s') = %d", newname, error);
2946 ASSERT(spa == za->za_spa);
2947 spa_close(spa, FTAG);
2950 * Rename it back to the original
2952 error = spa_rename(newname, oldname);
2954 fatal(0, "spa_rename('%s', '%s') = %d", newname,
2958 * Make sure it can still be opened
2960 error = spa_open(oldname, &spa, FTAG);
2962 fatal(0, "spa_open('%s') = %d", oldname, error);
2964 ASSERT(spa == za->za_spa);
2965 spa_close(spa, FTAG);
2967 umem_free(newname, strlen(newname) + 1);
2969 (void) rw_unlock(&ztest_shared->zs_name_lock);
2974 * Completely obliterate one disk.
2977 ztest_obliterate_one_disk(uint64_t vdev)
2980 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
2983 if (zopt_maxfaults < 2)
2986 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2987 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
2989 fd = open(dev_name, O_RDWR);
2992 fatal(1, "can't open %s", dev_name);
2995 * Determine the size.
2997 fsize = lseek(fd, 0, SEEK_END);
3002 * Rename the old device to dev_name.old (useful for debugging).
3004 VERIFY(rename(dev_name, copy_name) == 0);
3009 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
3010 VERIFY(ftruncate(fd, fsize) == 0);
3015 ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
3017 char dev_name[MAXPATHLEN];
3023 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
3026 * Build the nvlist describing dev_name.
3028 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
3030 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3031 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
3034 guid = vd->vdev_guid;
3035 spa_config_exit(spa, SCL_VDEV, FTAG);
3036 error = spa_vdev_attach(spa, guid, root, B_TRUE);
3042 fatal(0, "spa_vdev_attach(in-place) = %d", error);
3048 ztest_verify_blocks(char *pool)
3051 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
3059 if (realpath(progname, zdb) == NULL)
3060 assert(!"realpath() failed");
3062 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
3063 bin = strstr(zdb, "/usr/bin/");
3064 ztest = strstr(bin, "/ztest");
3066 isalen = ztest - isa;
3070 "/usr/sbin%.*s/zdb -bc%s%s -U /tmp/zpool.cache -O %s %s",
3073 zopt_verbose >= 3 ? "s" : "",
3074 zopt_verbose >= 4 ? "v" : "",
3075 ztest_random(2) == 0 ? "pre" : "post", pool);
3078 if (zopt_verbose >= 5)
3079 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
3081 fp = popen(zdb, "r");
3084 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
3085 if (zopt_verbose >= 3)
3086 (void) printf("%s", zbuf);
3088 status = pclose(fp);
3093 ztest_dump_core = 0;
3094 if (WIFEXITED(status))
3095 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
3097 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
3101 ztest_walk_pool_directory(char *header)
3105 if (zopt_verbose >= 6)
3106 (void) printf("%s\n", header);
3108 mutex_enter(&spa_namespace_lock);
3109 while ((spa = spa_next(spa)) != NULL)
3110 if (zopt_verbose >= 6)
3111 (void) printf("\t%s\n", spa_name(spa));
3112 mutex_exit(&spa_namespace_lock);
3116 ztest_spa_import_export(char *oldname, char *newname)
3123 if (zopt_verbose >= 4) {
3124 (void) printf("import/export: old = %s, new = %s\n",
3129 * Clean up from previous runs.
3131 (void) spa_destroy(newname);
3134 * Get the pool's configuration and guid.
3136 error = spa_open(oldname, &spa, FTAG);
3138 fatal(0, "spa_open('%s') = %d", oldname, error);
3140 pool_guid = spa_guid(spa);
3141 spa_close(spa, FTAG);
3143 ztest_walk_pool_directory("pools before export");
3148 error = spa_export(oldname, &config, B_FALSE, B_FALSE);
3150 fatal(0, "spa_export('%s') = %d", oldname, error);
3152 ztest_walk_pool_directory("pools after export");
3155 * Import it under the new name.
3157 error = spa_import(newname, config, NULL);
3159 fatal(0, "spa_import('%s') = %d", newname, error);
3161 ztest_walk_pool_directory("pools after import");
3164 * Try to import it again -- should fail with EEXIST.
3166 error = spa_import(newname, config, NULL);
3167 if (error != EEXIST)
3168 fatal(0, "spa_import('%s') twice", newname);
3171 * Try to import it under a different name -- should fail with EEXIST.
3173 error = spa_import(oldname, config, NULL);
3174 if (error != EEXIST)
3175 fatal(0, "spa_import('%s') under multiple names", newname);
3178 * Verify that the pool is no longer visible under the old name.
3180 error = spa_open(oldname, &spa, FTAG);
3181 if (error != ENOENT)
3182 fatal(0, "spa_open('%s') = %d", newname, error);
3185 * Verify that we can open and close the pool using the new name.
3187 error = spa_open(newname, &spa, FTAG);
3189 fatal(0, "spa_open('%s') = %d", newname, error);
3190 ASSERT(pool_guid == spa_guid(spa));
3191 spa_close(spa, FTAG);
3193 nvlist_free(config);
3197 ztest_resume(void *arg)
3201 while (!ztest_exiting) {
3202 (void) poll(NULL, 0, 1000);
3204 if (!spa_suspended(spa))
3207 spa_vdev_state_enter(spa);
3208 vdev_clear(spa, NULL);
3209 (void) spa_vdev_state_exit(spa, NULL, 0);
3217 ztest_thread(void *arg)
3219 ztest_args_t *za = arg;
3220 ztest_shared_t *zs = ztest_shared;
3221 hrtime_t now, functime;
3225 while ((now = gethrtime()) < za->za_stop) {
3227 * See if it's time to force a crash.
3229 if (now > za->za_kill) {
3230 zs->zs_alloc = spa_get_alloc(za->za_spa);
3231 zs->zs_space = spa_get_space(za->za_spa);
3232 (void) kill(getpid(), SIGKILL);
3236 * Pick a random function.
3238 f = ztest_random(ZTEST_FUNCS);
3239 zi = &zs->zs_info[f];
3242 * Decide whether to call it, based on the requested frequency.
3244 if (zi->zi_call_target == 0 ||
3245 (double)zi->zi_call_total / zi->zi_call_target >
3246 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3249 atomic_add_64(&zi->zi_calls, 1);
3250 atomic_add_64(&zi->zi_call_total, 1);
3252 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3254 za->za_diroff_shared = (1ULL << 63);
3256 for (i = 0; i < zi->zi_iters; i++)
3259 functime = gethrtime() - now;
3261 atomic_add_64(&zi->zi_call_time, functime);
3263 if (zopt_verbose >= 4) {
3265 (void) dladdr((void *)zi->zi_func, &dli);
3266 (void) printf("%6.2f sec in %s\n",
3267 (double)functime / NANOSEC, dli.dli_sname);
3271 * If we're getting ENOSPC with some regularity, stop.
3273 if (zs->zs_enospc_count > 10)
3281 * Kick off threads to run tests on all datasets in parallel.
3284 ztest_run(char *pool)
3287 ztest_shared_t *zs = ztest_shared;
3291 thread_t resume_tid;
3293 ztest_exiting = B_FALSE;
3295 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3296 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3298 for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3299 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3302 * Destroy one disk before we even start.
3303 * It's mirrored, so everything should work just fine.
3304 * This makes us exercise fault handling very early in spa_load().
3306 ztest_obliterate_one_disk(0);
3309 * Verify that the sum of the sizes of all blocks in the pool
3310 * equals the SPA's allocated space total.
3312 ztest_verify_blocks(pool);
3315 * Kick off a replacement of the disk we just obliterated.
3317 kernel_init(FREAD | FWRITE);
3318 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3319 ztest_replace_one_disk(spa, 0);
3320 if (zopt_verbose >= 5)
3321 show_pool_stats(spa);
3322 spa_close(spa, FTAG);
3325 kernel_init(FREAD | FWRITE);
3328 * Verify that we can export the pool and reimport it under a
3331 if (ztest_random(2) == 0) {
3332 (void) snprintf(name, 100, "%s_import", pool);
3333 ztest_spa_import_export(pool, name);
3334 ztest_spa_import_export(name, pool);
3338 * Verify that we can loop over all pools.
3340 mutex_enter(&spa_namespace_lock);
3341 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3342 if (zopt_verbose > 3) {
3343 (void) printf("spa_next: found %s\n", spa_name(spa));
3346 mutex_exit(&spa_namespace_lock);
3351 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3354 * Create a thread to periodically resume suspended I/O.
3356 VERIFY(thr_create(0, 0, ztest_resume, spa, THR_BOUND,
3360 * Verify that we can safely inquire about about any object,
3361 * whether it's allocated or not. To make it interesting,
3362 * we probe a 5-wide window around each power of two.
3363 * This hits all edge cases, including zero and the max.
3365 for (t = 0; t < 64; t++) {
3366 for (d = -5; d <= 5; d++) {
3367 error = dmu_object_info(spa->spa_meta_objset,
3368 (1ULL << t) + d, NULL);
3369 ASSERT(error == 0 || error == ENOENT ||
3375 * Now kick off all the tests that run in parallel.
3377 zs->zs_enospc_count = 0;
3379 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3381 if (zopt_verbose >= 4)
3382 (void) printf("starting main threads...\n");
3384 za[0].za_start = gethrtime();
3385 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3386 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3387 za[0].za_kill = za[0].za_stop;
3388 if (ztest_random(100) < zopt_killrate)
3389 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3391 for (t = 0; t < zopt_threads; t++) {
3392 d = t % zopt_datasets;
3394 (void) strcpy(za[t].za_pool, pool);
3395 za[t].za_os = za[d].za_os;
3397 za[t].za_zilog = za[d].za_zilog;
3398 za[t].za_instance = t;
3399 za[t].za_random = ztest_random(-1ULL);
3400 za[t].za_start = za[0].za_start;
3401 za[t].za_stop = za[0].za_stop;
3402 za[t].za_kill = za[0].za_kill;
3404 if (t < zopt_datasets) {
3406 int test_future = FALSE;
3407 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3408 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3409 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
3410 ztest_create_cb, NULL);
3411 if (error == EEXIST) {
3413 } else if (error == ENOSPC) {
3414 zs->zs_enospc_count++;
3415 (void) rw_unlock(&ztest_shared->zs_name_lock);
3417 } else if (error != 0) {
3418 fatal(0, "dmu_objset_create(%s) = %d",
3421 error = dmu_objset_open(name, DMU_OST_OTHER,
3422 DS_MODE_USER, &za[d].za_os);
3424 fatal(0, "dmu_objset_open('%s') = %d",
3426 (void) rw_unlock(&ztest_shared->zs_name_lock);
3428 ztest_dmu_check_future_leak(&za[t]);
3429 zr.zr_os = za[d].za_os;
3430 zil_replay(zr.zr_os, &zr, &zr.zr_assign,
3431 ztest_replay_vector, NULL);
3432 za[d].za_zilog = zil_open(za[d].za_os, NULL);
3435 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3436 &za[t].za_thread) == 0);
3440 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3442 traverse_fini(za[t].za_th);
3443 if (t < zopt_datasets) {
3444 zil_close(za[t].za_zilog);
3445 dmu_objset_close(za[t].za_os);
3449 if (zopt_verbose >= 3)
3450 show_pool_stats(spa);
3452 txg_wait_synced(spa_get_dsl(spa), 0);
3454 zs->zs_alloc = spa_get_alloc(spa);
3455 zs->zs_space = spa_get_space(spa);
3458 * If we had out-of-space errors, destroy a random objset.
3460 if (zs->zs_enospc_count != 0) {
3461 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3462 d = (int)ztest_random(zopt_datasets);
3463 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3464 if (zopt_verbose >= 3)
3465 (void) printf("Destroying %s to free up space\n", name);
3466 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
3467 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3468 (void) rw_unlock(&ztest_shared->zs_name_lock);
3471 txg_wait_synced(spa_get_dsl(spa), 0);
3473 umem_free(za, zopt_threads * sizeof (ztest_args_t));
3475 /* Kill the resume thread */
3476 ztest_exiting = B_TRUE;
3477 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
3480 * Right before closing the pool, kick off a bunch of async I/O;
3481 * spa_close() should wait for it to complete.
3483 for (t = 1; t < 50; t++)
3484 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3486 spa_close(spa, FTAG);
3492 print_time(hrtime_t t, char *timebuf)
3494 hrtime_t s = t / NANOSEC;
3495 hrtime_t m = s / 60;
3496 hrtime_t h = m / 60;
3497 hrtime_t d = h / 24;
3506 (void) sprintf(timebuf,
3507 "%llud%02lluh%02llum%02llus", d, h, m, s);
3509 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3511 (void) sprintf(timebuf, "%llum%02llus", m, s);
3513 (void) sprintf(timebuf, "%llus", s);
3517 * Create a storage pool with the given name and initial vdev size.
3518 * Then create the specified number of datasets in the pool.
3521 ztest_init(char *pool)
3527 kernel_init(FREAD | FWRITE);
3530 * Create the storage pool.
3532 (void) spa_destroy(pool);
3533 ztest_shared->zs_vdev_primaries = 0;
3534 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
3535 0, zopt_raidz, zopt_mirrors, 1);
3536 error = spa_create(pool, nvroot, NULL, NULL, NULL);
3537 nvlist_free(nvroot);
3540 fatal(0, "spa_create() = %d", error);
3541 error = spa_open(pool, &spa, FTAG);
3543 fatal(0, "spa_open() = %d", error);
3545 if (zopt_verbose >= 3)
3546 show_pool_stats(spa);
3548 spa_close(spa, FTAG);
3554 main(int argc, char **argv)
3564 (void) setvbuf(stdout, NULL, _IOLBF, 0);
3566 /* Override location of zpool.cache */
3567 spa_config_path = "/tmp/zpool.cache";
3569 ztest_random_fd = open("/dev/urandom", O_RDONLY);
3571 process_options(argc, argv);
3576 dprintf_setup(&argc, argv);
3579 * Blow away any existing copy of zpool.cache
3582 (void) remove("/tmp/zpool.cache");
3584 zs = ztest_shared = (void *)mmap(0,
3585 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3586 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3588 if (zopt_verbose >= 1) {
3589 (void) printf("%llu vdevs, %d datasets, %d threads,"
3590 " %llu seconds...\n",
3591 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3592 (u_longlong_t)zopt_time);
3596 * Create and initialize our storage pool.
3598 for (i = 1; i <= zopt_init; i++) {
3599 bzero(zs, sizeof (ztest_shared_t));
3600 if (zopt_verbose >= 3 && zopt_init != 1)
3601 (void) printf("ztest_init(), pass %d\n", i);
3602 ztest_init(zopt_pool);
3606 * Initialize the call targets for each function.
3608 for (f = 0; f < ZTEST_FUNCS; f++) {
3609 zi = &zs->zs_info[f];
3611 *zi = ztest_info[f];
3613 if (*zi->zi_interval == 0)
3614 zi->zi_call_target = UINT64_MAX;
3616 zi->zi_call_target = zopt_time / *zi->zi_interval;
3619 zs->zs_start_time = gethrtime();
3620 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3623 * Run the tests in a loop. These tests include fault injection
3624 * to verify that self-healing data works, and forced crashes
3625 * to verify that we never lose on-disk consistency.
3627 while (gethrtime() < zs->zs_stop_time) {
3633 * Initialize the workload counters for each function.
3635 for (f = 0; f < ZTEST_FUNCS; f++) {
3636 zi = &zs->zs_info[f];
3638 zi->zi_call_time = 0;
3644 fatal(1, "fork failed");
3646 if (pid == 0) { /* child */
3647 struct rlimit rl = { 1024, 1024 };
3648 (void) setrlimit(RLIMIT_NOFILE, &rl);
3649 (void) enable_extended_FILE_stdio(-1, -1);
3650 ztest_run(zopt_pool);
3654 while (waitpid(pid, &status, 0) != pid)
3657 if (WIFEXITED(status)) {
3658 if (WEXITSTATUS(status) != 0) {
3659 (void) fprintf(stderr,
3660 "child exited with code %d\n",
3661 WEXITSTATUS(status));
3664 } else if (WIFSIGNALED(status)) {
3665 if (WTERMSIG(status) != SIGKILL) {
3666 (void) fprintf(stderr,
3667 "child died with signal %d\n",
3673 (void) fprintf(stderr, "something strange happened "
3680 if (zopt_verbose >= 1) {
3681 hrtime_t now = gethrtime();
3683 now = MIN(now, zs->zs_stop_time);
3684 print_time(zs->zs_stop_time - now, timebuf);
3685 nicenum(zs->zs_space, numbuf);
3687 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
3688 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
3690 WIFEXITED(status) ? "Complete" : "SIGKILL",
3691 (u_longlong_t)zs->zs_enospc_count,
3692 100.0 * zs->zs_alloc / zs->zs_space,
3694 100.0 * (now - zs->zs_start_time) /
3695 (zopt_time * NANOSEC), timebuf);
3698 if (zopt_verbose >= 2) {
3699 (void) printf("\nWorkload summary:\n\n");
3700 (void) printf("%7s %9s %s\n",
3701 "Calls", "Time", "Function");
3702 (void) printf("%7s %9s %s\n",
3703 "-----", "----", "--------");
3704 for (f = 0; f < ZTEST_FUNCS; f++) {
3707 zi = &zs->zs_info[f];
3708 print_time(zi->zi_call_time, timebuf);
3709 (void) dladdr((void *)zi->zi_func, &dli);
3710 (void) printf("%7llu %9s %s\n",
3711 (u_longlong_t)zi->zi_calls, timebuf,
3714 (void) printf("\n");
3718 * It's possible that we killed a child during a rename test, in
3719 * which case we'll have a 'ztest_tmp' pool lying around instead
3720 * of 'ztest'. Do a blind rename in case this happened.
3722 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
3723 (void) strcpy(tmp, zopt_pool);
3724 (void) strcat(tmp, "_tmp");
3725 kernel_init(FREAD | FWRITE);
3726 (void) spa_rename(tmp, zopt_pool);
3728 umem_free(tmp, strlen(tmp) + 1);
3731 ztest_verify_blocks(zopt_pool);
3733 if (zopt_verbose >= 1) {
3734 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
3735 kills, iters - kills, (100.0 * kills) / MAX(1, iters));