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 2008 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/refcount.h>
98 #include <stdio_ext.h>
107 #include <sys/fs/zfs.h>
109 static char cmdname[] = "ztest";
110 static char *zopt_pool = cmdname;
111 static char *progname;
113 static uint64_t zopt_vdevs = 5;
114 static uint64_t zopt_vdevtime;
115 static int zopt_ashift = SPA_MINBLOCKSHIFT;
116 static int zopt_mirrors = 2;
117 static int zopt_raidz = 4;
118 static int zopt_raidz_parity = 1;
119 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
120 static int zopt_datasets = 7;
121 static int zopt_threads = 23;
122 static uint64_t zopt_passtime = 60; /* 60 seconds */
123 static uint64_t zopt_killrate = 70; /* 70% kill rate */
124 static int zopt_verbose = 0;
125 static int zopt_init = 1;
126 static char *zopt_dir = "/tmp";
127 static uint64_t zopt_time = 300; /* 5 minutes */
128 static int zopt_maxfaults;
130 typedef struct ztest_block_tag {
139 typedef struct ztest_args {
140 char za_pool[MAXNAMELEN];
145 uint64_t za_instance;
148 uint64_t za_diroff_shared;
153 traverse_handle_t *za_th;
155 * Thread-local variables can go here to aid debugging.
157 ztest_block_tag_t za_rbt;
158 ztest_block_tag_t za_wbt;
159 dmu_object_info_t za_doi;
163 typedef void ztest_func_t(ztest_args_t *);
166 * Note: these aren't static because we want dladdr() to work.
168 ztest_func_t ztest_dmu_read_write;
169 ztest_func_t ztest_dmu_write_parallel;
170 ztest_func_t ztest_dmu_object_alloc_free;
171 ztest_func_t ztest_zap;
172 ztest_func_t ztest_zap_parallel;
173 ztest_func_t ztest_traverse;
174 ztest_func_t ztest_dsl_prop_get_set;
175 ztest_func_t ztest_dmu_objset_create_destroy;
176 ztest_func_t ztest_dmu_snapshot_create_destroy;
177 ztest_func_t ztest_spa_create_destroy;
178 ztest_func_t ztest_fault_inject;
179 ztest_func_t ztest_spa_rename;
180 ztest_func_t ztest_vdev_attach_detach;
181 ztest_func_t ztest_vdev_LUN_growth;
182 ztest_func_t ztest_vdev_add_remove;
183 ztest_func_t ztest_vdev_aux_add_remove;
184 ztest_func_t ztest_scrub;
186 typedef struct ztest_info {
187 ztest_func_t *zi_func; /* test function */
188 uint64_t zi_iters; /* iterations per execution */
189 uint64_t *zi_interval; /* execute every <interval> seconds */
190 uint64_t zi_calls; /* per-pass count */
191 uint64_t zi_call_time; /* per-pass time */
192 uint64_t zi_call_total; /* cumulative total */
193 uint64_t zi_call_target; /* target cumulative total */
196 uint64_t zopt_always = 0; /* all the time */
197 uint64_t zopt_often = 1; /* every second */
198 uint64_t zopt_sometimes = 10; /* every 10 seconds */
199 uint64_t zopt_rarely = 60; /* every 60 seconds */
201 ztest_info_t ztest_info[] = {
202 { ztest_dmu_read_write, 1, &zopt_always },
203 { ztest_dmu_write_parallel, 30, &zopt_always },
204 { ztest_dmu_object_alloc_free, 1, &zopt_always },
205 { ztest_zap, 30, &zopt_always },
206 { ztest_zap_parallel, 100, &zopt_always },
207 { ztest_traverse, 1, &zopt_often },
208 { ztest_dsl_prop_get_set, 1, &zopt_sometimes },
209 { ztest_dmu_objset_create_destroy, 1, &zopt_sometimes },
210 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
211 { ztest_spa_create_destroy, 1, &zopt_sometimes },
212 { ztest_fault_inject, 1, &zopt_sometimes },
213 { ztest_spa_rename, 1, &zopt_rarely },
214 { ztest_vdev_attach_detach, 1, &zopt_rarely },
215 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
216 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
217 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
218 { ztest_scrub, 1, &zopt_vdevtime },
221 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
223 #define ZTEST_SYNC_LOCKS 16
226 * Stuff we need to share writably between parent and child.
228 typedef struct ztest_shared {
229 mutex_t zs_vdev_lock;
230 rwlock_t zs_name_lock;
231 uint64_t zs_vdev_primaries;
232 uint64_t zs_vdev_aux;
233 uint64_t zs_enospc_count;
234 hrtime_t zs_start_time;
235 hrtime_t zs_stop_time;
238 ztest_info_t zs_info[ZTEST_FUNCS];
239 mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
240 uint64_t zs_seq[ZTEST_SYNC_LOCKS];
243 static char ztest_dev_template[] = "%s/%s.%llua";
244 static char ztest_aux_template[] = "%s/%s.%s.%llu";
245 static ztest_shared_t *ztest_shared;
247 static int ztest_random_fd;
248 static int ztest_dump_core = 1;
250 static boolean_t ztest_exiting;
252 extern uint64_t metaslab_gang_bang;
254 #define ZTEST_DIROBJ 1
255 #define ZTEST_MICROZAP_OBJ 2
256 #define ZTEST_FATZAP_OBJ 3
258 #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10)
259 #define ZTEST_DIRSIZE 256
261 static void usage(boolean_t) __NORETURN;
264 * These libumem hooks provide a reasonable set of defaults for the allocator's
265 * debugging facilities.
270 return ("default,verbose"); /* $UMEM_DEBUG setting */
274 _umem_logging_init(void)
276 return ("fail,contents"); /* $UMEM_LOGGING setting */
279 #define FATAL_MSG_SZ 1024
284 fatal(int do_perror, char *message, ...)
287 int save_errno = errno;
288 char buf[FATAL_MSG_SZ];
290 (void) fflush(stdout);
292 va_start(args, message);
293 (void) sprintf(buf, "ztest: ");
295 (void) vsprintf(buf + strlen(buf), message, args);
298 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
299 ": %s", strerror(save_errno));
301 (void) fprintf(stderr, "%s\n", buf);
302 fatal_msg = buf; /* to ease debugging */
309 str2shift(const char *buf)
311 const char *ends = "BKMGTPEZ";
316 for (i = 0; i < strlen(ends); i++) {
317 if (toupper(buf[0]) == ends[i])
320 if (i == strlen(ends)) {
321 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
325 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
328 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
334 nicenumtoull(const char *buf)
339 val = strtoull(buf, &end, 0);
341 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
343 } else if (end[0] == '.') {
344 double fval = strtod(buf, &end);
345 fval *= pow(2, str2shift(end));
346 if (fval > UINT64_MAX) {
347 (void) fprintf(stderr, "ztest: value too large: %s\n",
351 val = (uint64_t)fval;
353 int shift = str2shift(end);
354 if (shift >= 64 || (val << shift) >> shift != val) {
355 (void) fprintf(stderr, "ztest: value too large: %s\n",
365 usage(boolean_t requested)
367 char nice_vdev_size[10];
368 char nice_gang_bang[10];
369 FILE *fp = requested ? stdout : stderr;
371 nicenum(zopt_vdev_size, nice_vdev_size);
372 nicenum(metaslab_gang_bang, nice_gang_bang);
374 (void) fprintf(fp, "Usage: %s\n"
375 "\t[-v vdevs (default: %llu)]\n"
376 "\t[-s size_of_each_vdev (default: %s)]\n"
377 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
378 "\t[-m mirror_copies (default: %d)]\n"
379 "\t[-r raidz_disks (default: %d)]\n"
380 "\t[-R raidz_parity (default: %d)]\n"
381 "\t[-d datasets (default: %d)]\n"
382 "\t[-t threads (default: %d)]\n"
383 "\t[-g gang_block_threshold (default: %s)]\n"
384 "\t[-i initialize pool i times (default: %d)]\n"
385 "\t[-k kill percentage (default: %llu%%)]\n"
386 "\t[-p pool_name (default: %s)]\n"
387 "\t[-f file directory for vdev files (default: %s)]\n"
388 "\t[-V(erbose)] (use multiple times for ever more blather)\n"
389 "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
390 "\t[-T time] total run time (default: %llu sec)\n"
391 "\t[-P passtime] time per pass (default: %llu sec)\n"
392 "\t[-h] (print help)\n"
395 (u_longlong_t)zopt_vdevs, /* -v */
396 nice_vdev_size, /* -s */
397 zopt_ashift, /* -a */
398 zopt_mirrors, /* -m */
400 zopt_raidz_parity, /* -R */
401 zopt_datasets, /* -d */
402 zopt_threads, /* -t */
403 nice_gang_bang, /* -g */
405 (u_longlong_t)zopt_killrate, /* -k */
408 (u_longlong_t)zopt_time, /* -T */
409 (u_longlong_t)zopt_passtime); /* -P */
410 exit(requested ? 0 : 1);
414 ztest_random(uint64_t range)
421 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
422 fatal(1, "short read from /dev/urandom");
428 ztest_record_enospc(char *s)
430 dprintf("ENOSPC doing: %s\n", s ? s : "<unknown>");
431 ztest_shared->zs_enospc_count++;
435 process_options(int argc, char **argv)
440 /* Remember program name. */
443 /* By default, test gang blocks for blocks 32K and greater */
444 metaslab_gang_bang = 32 << 10;
446 while ((opt = getopt(argc, argv,
447 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
463 value = nicenumtoull(optarg);
470 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
476 zopt_mirrors = value;
479 zopt_raidz = MAX(1, value);
482 zopt_raidz_parity = MIN(MAX(value, 1), 2);
485 zopt_datasets = MAX(1, value);
488 zopt_threads = MAX(1, value);
491 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
497 zopt_killrate = value;
500 zopt_pool = strdup(optarg);
503 zopt_dir = strdup(optarg);
515 zopt_passtime = MAX(1, value);
527 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
529 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
530 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
534 ztest_get_ashift(void)
536 if (zopt_ashift == 0)
537 return (SPA_MINBLOCKSHIFT + ztest_random(3));
538 return (zopt_ashift);
542 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
544 char pathbuf[MAXPATHLEN];
549 ashift = ztest_get_ashift();
555 vdev = ztest_shared->zs_vdev_aux;
556 (void) sprintf(path, ztest_aux_template,
557 zopt_dir, zopt_pool, aux, vdev);
559 vdev = ztest_shared->zs_vdev_primaries++;
560 (void) sprintf(path, ztest_dev_template,
561 zopt_dir, zopt_pool, vdev);
566 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
568 fatal(1, "can't open %s", path);
569 if (ftruncate(fd, size) != 0)
570 fatal(1, "can't ftruncate %s", path);
574 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
575 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
576 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
577 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
583 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
585 nvlist_t *raidz, **child;
589 return (make_vdev_file(path, aux, size, ashift));
590 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
592 for (c = 0; c < r; c++)
593 child[c] = make_vdev_file(path, aux, size, ashift);
595 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
596 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
597 VDEV_TYPE_RAIDZ) == 0);
598 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
599 zopt_raidz_parity) == 0);
600 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
603 for (c = 0; c < r; c++)
604 nvlist_free(child[c]);
606 umem_free(child, r * sizeof (nvlist_t *));
612 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
615 nvlist_t *mirror, **child;
619 return (make_vdev_raidz(path, aux, size, ashift, r));
621 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
623 for (c = 0; c < m; c++)
624 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
626 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
627 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
628 VDEV_TYPE_MIRROR) == 0);
629 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
632 for (c = 0; c < m; c++)
633 nvlist_free(child[c]);
635 umem_free(child, m * sizeof (nvlist_t *));
641 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
642 int log, int r, int m, int t)
644 nvlist_t *root, **child;
649 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
651 for (c = 0; c < t; c++) {
652 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
653 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
657 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
658 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
659 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
662 for (c = 0; c < t; c++)
663 nvlist_free(child[c]);
665 umem_free(child, t * sizeof (nvlist_t *));
671 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
673 int bs = SPA_MINBLOCKSHIFT +
674 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
675 int ibs = DN_MIN_INDBLKSHIFT +
676 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
679 error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
682 dmu_objset_name(os, osname);
683 fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
684 osname, object, 1 << bs, ibs, error);
689 ztest_random_checksum(void)
694 checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
695 } while (zio_checksum_table[checksum].ci_zbt);
697 if (checksum == ZIO_CHECKSUM_OFF)
698 checksum = ZIO_CHECKSUM_ON;
704 ztest_random_compress(void)
706 return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
709 typedef struct ztest_replay {
715 ztest_replay_create(ztest_replay_t *zr, lr_create_t *lr, boolean_t byteswap)
717 objset_t *os = zr->zr_os;
722 byteswap_uint64_array(lr, sizeof (*lr));
724 tx = dmu_tx_create(os);
725 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
726 error = dmu_tx_assign(tx, zr->zr_assign);
732 error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
734 ASSERT3U(error, ==, 0);
737 if (zopt_verbose >= 5) {
738 char osname[MAXNAMELEN];
739 dmu_objset_name(os, osname);
740 (void) printf("replay create of %s object %llu"
741 " in txg %llu = %d\n",
742 osname, (u_longlong_t)lr->lr_doid,
743 (u_longlong_t)zr->zr_assign, error);
750 ztest_replay_remove(ztest_replay_t *zr, lr_remove_t *lr, boolean_t byteswap)
752 objset_t *os = zr->zr_os;
757 byteswap_uint64_array(lr, sizeof (*lr));
759 tx = dmu_tx_create(os);
760 dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
761 error = dmu_tx_assign(tx, zr->zr_assign);
767 error = dmu_object_free(os, lr->lr_doid, tx);
773 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
774 NULL, /* 0 no such transaction type */
775 ztest_replay_create, /* TX_CREATE */
777 NULL, /* TX_MKXATTR */
778 NULL, /* TX_SYMLINK */
779 ztest_replay_remove, /* TX_REMOVE */
782 NULL, /* TX_RENAME */
784 NULL, /* TX_TRUNCATE */
785 NULL, /* TX_SETATTR */
790 * Verify that we can't destroy an active pool, create an existing pool,
791 * or create a pool with a bad vdev spec.
794 ztest_spa_create_destroy(ztest_args_t *za)
801 * Attempt to create using a bad file.
803 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
804 error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
807 fatal(0, "spa_create(bad_file) = %d", error);
810 * Attempt to create using a bad mirror.
812 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
813 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
816 fatal(0, "spa_create(bad_mirror) = %d", error);
819 * Attempt to create an existing pool. It shouldn't matter
820 * what's in the nvroot; we should fail with EEXIST.
822 (void) rw_rdlock(&ztest_shared->zs_name_lock);
823 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
824 error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
827 fatal(0, "spa_create(whatever) = %d", error);
829 error = spa_open(za->za_pool, &spa, FTAG);
831 fatal(0, "spa_open() = %d", error);
833 error = spa_destroy(za->za_pool);
835 fatal(0, "spa_destroy() = %d", error);
837 spa_close(spa, FTAG);
838 (void) rw_unlock(&ztest_shared->zs_name_lock);
842 vdev_lookup_by_path(vdev_t *vd, const char *path)
846 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
849 for (int c = 0; c < vd->vdev_children; c++)
850 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
858 * Verify that vdev_add() works as expected.
861 ztest_vdev_add_remove(ztest_args_t *za)
863 spa_t *spa = za->za_spa;
864 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
868 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
870 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
872 ztest_shared->zs_vdev_primaries =
873 spa->spa_root_vdev->vdev_children * leaves;
875 spa_config_exit(spa, SCL_VDEV, FTAG);
878 * Make 1/4 of the devices be log devices.
880 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
881 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
883 error = spa_vdev_add(spa, nvroot);
886 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
889 ztest_record_enospc("spa_vdev_add");
891 fatal(0, "spa_vdev_add() = %d", error);
895 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
898 ztest_vdev_aux_add_remove(ztest_args_t *za)
900 spa_t *spa = za->za_spa;
901 vdev_t *rvd = spa->spa_root_vdev;
907 if (ztest_random(2) == 0) {
908 sav = &spa->spa_spares;
909 aux = ZPOOL_CONFIG_SPARES;
911 sav = &spa->spa_l2cache;
912 aux = ZPOOL_CONFIG_L2CACHE;
915 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
917 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
919 if (sav->sav_count != 0 && ztest_random(4) == 0) {
921 * Pick a random device to remove.
923 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
926 * Find an unused device we can add.
928 ztest_shared->zs_vdev_aux = 0;
930 char path[MAXPATHLEN];
932 (void) sprintf(path, ztest_aux_template, zopt_dir,
933 zopt_pool, aux, ztest_shared->zs_vdev_aux);
934 for (c = 0; c < sav->sav_count; c++)
935 if (strcmp(sav->sav_vdevs[c]->vdev_path,
938 if (c == sav->sav_count &&
939 vdev_lookup_by_path(rvd, path) == NULL)
941 ztest_shared->zs_vdev_aux++;
945 spa_config_exit(spa, SCL_VDEV, FTAG);
951 nvlist_t *nvroot = make_vdev_root(NULL, aux,
952 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
953 error = spa_vdev_add(spa, nvroot);
955 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
959 * Remove an existing device. Sometimes, dirty its
960 * vdev state first to make sure we handle removal
961 * of devices that have pending state changes.
963 if (ztest_random(2) == 0)
964 (void) vdev_online(spa, guid, B_FALSE, NULL);
966 error = spa_vdev_remove(spa, guid, B_FALSE);
967 if (error != 0 && error != EBUSY)
968 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
971 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
975 * Verify that we can attach and detach devices.
978 ztest_vdev_attach_detach(ztest_args_t *za)
980 spa_t *spa = za->za_spa;
981 spa_aux_vdev_t *sav = &spa->spa_spares;
982 vdev_t *rvd = spa->spa_root_vdev;
983 vdev_t *oldvd, *newvd, *pvd;
985 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
987 uint64_t ashift = ztest_get_ashift();
989 size_t oldsize, newsize;
990 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
992 int oldvd_has_siblings = B_FALSE;
993 int newvd_is_spare = B_FALSE;
995 int error, expected_error;
997 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
999 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1002 * Decide whether to do an attach or a replace.
1004 replacing = ztest_random(2);
1007 * Pick a random top-level vdev.
1009 top = ztest_random(rvd->vdev_children);
1012 * Pick a random leaf within it.
1014 leaf = ztest_random(leaves);
1019 oldvd = rvd->vdev_child[top];
1020 if (zopt_mirrors >= 1)
1021 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
1023 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
1026 * If we're already doing an attach or replace, oldvd may be a
1027 * mirror vdev -- in which case, pick a random child.
1029 while (oldvd->vdev_children != 0) {
1030 oldvd_has_siblings = B_TRUE;
1031 ASSERT(oldvd->vdev_children == 2);
1032 oldvd = oldvd->vdev_child[ztest_random(2)];
1035 oldguid = oldvd->vdev_guid;
1036 oldsize = vdev_get_rsize(oldvd);
1037 oldvd_is_log = oldvd->vdev_top->vdev_islog;
1038 (void) strcpy(oldpath, oldvd->vdev_path);
1039 pvd = oldvd->vdev_parent;
1042 * If oldvd has siblings, then half of the time, detach it.
1044 if (oldvd_has_siblings && ztest_random(2) == 0) {
1045 spa_config_exit(spa, SCL_VDEV, FTAG);
1046 error = spa_vdev_detach(spa, oldguid, B_FALSE);
1047 if (error != 0 && error != ENODEV && error != EBUSY)
1048 fatal(0, "detach (%s) returned %d",
1050 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1055 * For the new vdev, choose with equal probability between the two
1056 * standard paths (ending in either 'a' or 'b') or a random hot spare.
1058 if (sav->sav_count != 0 && ztest_random(3) == 0) {
1059 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
1060 newvd_is_spare = B_TRUE;
1061 (void) strcpy(newpath, newvd->vdev_path);
1063 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
1064 zopt_dir, zopt_pool, top * leaves + leaf);
1065 if (ztest_random(2) == 0)
1066 newpath[strlen(newpath) - 1] = 'b';
1067 newvd = vdev_lookup_by_path(rvd, newpath);
1071 newsize = vdev_get_rsize(newvd);
1074 * Make newsize a little bigger or smaller than oldsize.
1075 * If it's smaller, the attach should fail.
1076 * If it's larger, and we're doing a replace,
1077 * we should get dynamic LUN growth when we're done.
1079 newsize = 10 * oldsize / (9 + ztest_random(3));
1083 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1084 * unless it's a replace; in that case any non-replacing parent is OK.
1086 * If newvd is already part of the pool, it should fail with EBUSY.
1088 * If newvd is too small, it should fail with EOVERFLOW.
1090 if (pvd->vdev_ops != &vdev_mirror_ops &&
1091 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
1092 pvd->vdev_ops == &vdev_replacing_ops ||
1093 pvd->vdev_ops == &vdev_spare_ops))
1094 expected_error = ENOTSUP;
1095 else if (newvd_is_spare && (!replacing || oldvd_is_log))
1096 expected_error = ENOTSUP;
1097 else if (newvd == oldvd)
1098 expected_error = replacing ? 0 : EBUSY;
1099 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
1100 expected_error = EBUSY;
1101 else if (newsize < oldsize)
1102 expected_error = EOVERFLOW;
1103 else if (ashift > oldvd->vdev_top->vdev_ashift)
1104 expected_error = EDOM;
1108 spa_config_exit(spa, SCL_VDEV, FTAG);
1111 * Build the nvlist describing newpath.
1113 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1114 ashift, 0, 0, 0, 1);
1116 error = spa_vdev_attach(spa, oldguid, root, replacing);
1121 * If our parent was the replacing vdev, but the replace completed,
1122 * then instead of failing with ENOTSUP we may either succeed,
1123 * fail with ENODEV, or fail with EOVERFLOW.
1125 if (expected_error == ENOTSUP &&
1126 (error == 0 || error == ENODEV || error == EOVERFLOW))
1127 expected_error = error;
1130 * If someone grew the LUN, the replacement may be too small.
1132 if (error == EOVERFLOW || error == EBUSY)
1133 expected_error = error;
1135 /* XXX workaround 6690467 */
1136 if (error != expected_error && expected_error != EBUSY) {
1137 fatal(0, "attach (%s %llu, %s %llu, %d) "
1138 "returned %d, expected %d",
1139 oldpath, (longlong_t)oldsize, newpath,
1140 (longlong_t)newsize, replacing, error, expected_error);
1143 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1147 * Verify that dynamic LUN growth works as expected.
1151 ztest_vdev_LUN_growth(ztest_args_t *za)
1153 spa_t *spa = za->za_spa;
1154 char dev_name[MAXPATHLEN];
1155 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
1160 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
1163 * Pick a random leaf vdev.
1165 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1166 vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves);
1167 spa_config_exit(spa, SCL_VDEV, FTAG);
1169 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
1171 if ((fd = open(dev_name, O_RDWR)) != -1) {
1173 * Determine the size.
1175 fsize = lseek(fd, 0, SEEK_END);
1178 * If it's less than 2x the original size, grow by around 3%.
1180 if (fsize < 2 * zopt_vdev_size) {
1181 size_t newsize = fsize + ztest_random(fsize / 32);
1182 (void) ftruncate(fd, newsize);
1183 if (zopt_verbose >= 6) {
1184 (void) printf("%s grew from %lu to %lu bytes\n",
1185 dev_name, (ulong_t)fsize, (ulong_t)newsize);
1191 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1196 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1199 * Create the directory object.
1201 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1202 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1203 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1205 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1206 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1208 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1209 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1213 ztest_destroy_cb(char *name, void *arg)
1215 ztest_args_t *za = arg;
1217 dmu_object_info_t *doi = &za->za_doi;
1221 * Verify that the dataset contains a directory object.
1223 error = dmu_objset_open(name, DMU_OST_OTHER,
1224 DS_MODE_USER | DS_MODE_READONLY, &os);
1225 ASSERT3U(error, ==, 0);
1226 error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1227 if (error != ENOENT) {
1228 /* We could have crashed in the middle of destroying it */
1229 ASSERT3U(error, ==, 0);
1230 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1231 ASSERT3S(doi->doi_physical_blks, >=, 0);
1233 dmu_objset_close(os);
1236 * Destroy the dataset.
1238 error = dmu_objset_destroy(name);
1240 (void) dmu_objset_open(name, DMU_OST_OTHER,
1241 DS_MODE_USER | DS_MODE_READONLY, &os);
1242 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", &os, error);
1248 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1251 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1258 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1259 namesize = strlen(name) + 1;
1261 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1262 ztest_random(ZIL_MAX_BLKSZ));
1263 lr = (lr_create_t *)&itx->itx_lr;
1264 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1265 lr->lr_doid = object;
1270 lr->lr_gen = dmu_tx_get_txg(tx);
1271 lr->lr_crtime[0] = time(NULL);
1272 lr->lr_crtime[1] = 0;
1274 bcopy(name, (char *)(lr + 1), namesize);
1276 return (zil_itx_assign(zilog, itx, tx));
1280 ztest_dmu_objset_create_destroy(ztest_args_t *za)
1285 int basemode, expected_error;
1291 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1292 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1293 (u_longlong_t)za->za_instance);
1295 basemode = DS_MODE_TYPE(za->za_instance);
1296 if (basemode != DS_MODE_USER && basemode != DS_MODE_OWNER)
1297 basemode = DS_MODE_USER;
1300 * If this dataset exists from a previous run, process its replay log
1301 * half of the time. If we don't replay it, then dmu_objset_destroy()
1302 * (invoked from ztest_destroy_cb() below) should just throw it away.
1304 if (ztest_random(2) == 0 &&
1305 dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os) == 0) {
1307 zil_replay(os, &zr, &zr.zr_assign, ztest_replay_vector, NULL);
1308 dmu_objset_close(os);
1312 * There may be an old instance of the dataset we're about to
1313 * create lying around from a previous run. If so, destroy it
1314 * and all of its snapshots.
1316 (void) dmu_objset_find(name, ztest_destroy_cb, za,
1317 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1320 * Verify that the destroyed dataset is no longer in the namespace.
1322 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1323 if (error != ENOENT)
1324 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1328 * Verify that we can create a new dataset.
1330 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
1331 ztest_create_cb, NULL);
1333 if (error == ENOSPC) {
1334 ztest_record_enospc("dmu_objset_create");
1335 (void) rw_unlock(&ztest_shared->zs_name_lock);
1338 fatal(0, "dmu_objset_create(%s) = %d", name, error);
1341 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1343 fatal(0, "dmu_objset_open(%s) = %d", name, error);
1347 * Open the intent log for it.
1349 zilog = zil_open(os, NULL);
1352 * Put a random number of objects in there.
1354 objects = ztest_random(20);
1356 while (objects-- != 0) {
1358 dmu_tx_t *tx = dmu_tx_create(os);
1359 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1360 error = dmu_tx_assign(tx, TXG_WAIT);
1364 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1365 DMU_OT_NONE, 0, tx);
1366 ztest_set_random_blocksize(os, object, tx);
1367 seq = ztest_log_create(zilog, tx, object,
1368 DMU_OT_UINT64_OTHER);
1369 dmu_write(os, object, 0, sizeof (name), name, tx);
1372 if (ztest_random(5) == 0) {
1373 zil_commit(zilog, seq, object);
1375 if (ztest_random(100) == 0) {
1376 error = zil_suspend(zilog);
1384 * Verify that we cannot create an existing dataset.
1386 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, NULL, NULL);
1387 if (error != EEXIST)
1388 fatal(0, "created existing dataset, error = %d", error);
1391 * Verify that multiple dataset holds are allowed, but only when
1392 * the new access mode is compatible with the base mode.
1394 if (basemode == DS_MODE_OWNER) {
1395 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_USER,
1398 fatal(0, "dmu_objset_open('%s') = %d", name, error);
1400 dmu_objset_close(os2);
1402 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os2);
1403 expected_error = (basemode == DS_MODE_OWNER) ? EBUSY : 0;
1404 if (error != expected_error)
1405 fatal(0, "dmu_objset_open('%s') = %d, expected %d",
1406 name, error, expected_error);
1408 dmu_objset_close(os2);
1411 dmu_objset_close(os);
1413 error = dmu_objset_destroy(name);
1415 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1417 (void) rw_unlock(&ztest_shared->zs_name_lock);
1421 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1424 ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1427 objset_t *os = za->za_os;
1429 char osname[MAXNAMELEN];
1431 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1432 dmu_objset_name(os, osname);
1433 (void) snprintf(snapname, 100, "%s@%llu", osname,
1434 (u_longlong_t)za->za_instance);
1436 error = dmu_objset_destroy(snapname);
1437 if (error != 0 && error != ENOENT)
1438 fatal(0, "dmu_objset_destroy() = %d", error);
1439 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE);
1440 if (error == ENOSPC)
1441 ztest_record_enospc("dmu_take_snapshot");
1442 else if (error != 0 && error != EEXIST)
1443 fatal(0, "dmu_take_snapshot() = %d", error);
1444 (void) rw_unlock(&ztest_shared->zs_name_lock);
1447 #define ZTEST_TRAVERSE_BLOCKS 1000
1450 ztest_blk_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1452 ztest_args_t *za = arg;
1453 zbookmark_t *zb = &bc->bc_bookmark;
1454 blkptr_t *bp = &bc->bc_blkptr;
1455 dnode_phys_t *dnp = bc->bc_dnode;
1456 traverse_handle_t *th = za->za_th;
1457 uint64_t size = BP_GET_LSIZE(bp);
1460 * Level -1 indicates the objset_phys_t or something in its intent log.
1462 if (zb->zb_level == -1) {
1463 if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
1464 ASSERT3U(zb->zb_object, ==, 0);
1465 ASSERT3U(zb->zb_blkid, ==, 0);
1466 ASSERT3U(size, ==, sizeof (objset_phys_t));
1468 } else if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) {
1469 ASSERT3U(zb->zb_object, ==, 0);
1470 ASSERT3U(zb->zb_blkid, >, za->za_zil_seq);
1471 za->za_zil_seq = zb->zb_blkid;
1473 ASSERT3U(zb->zb_object, !=, 0); /* lr_write_t */
1479 ASSERT(dnp != NULL);
1485 * Once in a while, abort the traverse. We only do this to odd
1486 * instance numbers to ensure that even ones can run to completion.
1488 if ((za->za_instance & 1) && ztest_random(10000) == 0)
1491 if (bp->blk_birth == 0) {
1492 ASSERT(th->th_advance & ADVANCE_HOLES);
1496 if (zb->zb_level == 0 && !(th->th_advance & ADVANCE_DATA) &&
1497 bc == &th->th_cache[ZB_DN_CACHE][0]) {
1498 ASSERT(bc->bc_data == NULL);
1502 ASSERT(bc->bc_data != NULL);
1505 * This is an expensive question, so don't ask it too often.
1507 if (((za->za_random ^ th->th_callbacks) & 0xff) == 0) {
1508 void *xbuf = umem_alloc(size, UMEM_NOFAIL);
1509 if (arc_tryread(spa, bp, xbuf) == 0) {
1510 ASSERT(bcmp(bc->bc_data, xbuf, size) == 0);
1512 umem_free(xbuf, size);
1515 if (zb->zb_level > 0) {
1516 ASSERT3U(size, ==, 1ULL << dnp->dn_indblkshift);
1520 ASSERT(zb->zb_level == 0);
1521 ASSERT3U(size, ==, dnp->dn_datablkszsec << DEV_BSHIFT);
1527 * Verify that live pool traversal works.
1530 ztest_traverse(ztest_args_t *za)
1532 spa_t *spa = za->za_spa;
1533 traverse_handle_t *th = za->za_th;
1535 uint64_t cbstart, cblimit;
1540 if (ztest_random(2) == 0)
1541 advance |= ADVANCE_PRE;
1543 if (ztest_random(2) == 0)
1544 advance |= ADVANCE_PRUNE;
1546 if (ztest_random(2) == 0)
1547 advance |= ADVANCE_DATA;
1549 if (ztest_random(2) == 0)
1550 advance |= ADVANCE_HOLES;
1552 if (ztest_random(2) == 0)
1553 advance |= ADVANCE_ZIL;
1555 th = za->za_th = traverse_init(spa, ztest_blk_cb, za, advance,
1558 traverse_add_pool(th, 0, -1ULL);
1561 advance = th->th_advance;
1562 cbstart = th->th_callbacks;
1563 cblimit = cbstart + ((advance & ADVANCE_DATA) ? 100 : 1000);
1565 while ((rc = traverse_more(th)) == EAGAIN && th->th_callbacks < cblimit)
1568 if (zopt_verbose >= 5)
1569 (void) printf("traverse %s%s%s%s %llu blocks to "
1570 "<%llu, %llu, %lld, %llx>%s\n",
1571 (advance & ADVANCE_PRE) ? "pre" : "post",
1572 (advance & ADVANCE_PRUNE) ? "|prune" : "",
1573 (advance & ADVANCE_DATA) ? "|data" : "",
1574 (advance & ADVANCE_HOLES) ? "|holes" : "",
1575 (u_longlong_t)(th->th_callbacks - cbstart),
1576 (u_longlong_t)th->th_lastcb.zb_objset,
1577 (u_longlong_t)th->th_lastcb.zb_object,
1578 (u_longlong_t)th->th_lastcb.zb_level,
1579 (u_longlong_t)th->th_lastcb.zb_blkid,
1580 rc == 0 ? " [done]" :
1581 rc == EINTR ? " [aborted]" :
1586 if (rc != 0 && rc != EINTR)
1587 fatal(0, "traverse_more(%p) = %d", th, rc);
1594 * Verify that dmu_object_{alloc,free} work as expected.
1597 ztest_dmu_object_alloc_free(ztest_args_t *za)
1599 objset_t *os = za->za_os;
1602 uint64_t batchobj, object, batchsize, endoff, temp;
1603 int b, c, error, bonuslen;
1604 dmu_object_info_t *doi = &za->za_doi;
1605 char osname[MAXNAMELEN];
1607 dmu_objset_name(os, osname);
1613 * Create a batch object if necessary, and record it in the directory.
1615 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1616 sizeof (uint64_t), &batchobj));
1617 if (batchobj == 0) {
1618 tx = dmu_tx_create(os);
1619 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1621 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1622 error = dmu_tx_assign(tx, TXG_WAIT);
1624 ztest_record_enospc("create a batch object");
1628 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1629 DMU_OT_NONE, 0, tx);
1630 ztest_set_random_blocksize(os, batchobj, tx);
1631 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1632 sizeof (uint64_t), &batchobj, tx);
1637 * Destroy the previous batch of objects.
1639 for (b = 0; b < batchsize; b++) {
1640 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
1641 sizeof (uint64_t), &object));
1645 * Read and validate contents.
1646 * We expect the nth byte of the bonus buffer to be n.
1648 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1651 dmu_object_info_from_db(db, doi);
1652 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1653 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1654 ASSERT3S(doi->doi_physical_blks, >=, 0);
1656 bonuslen = doi->doi_bonus_size;
1658 for (c = 0; c < bonuslen; c++) {
1659 if (((uint8_t *)db->db_data)[c] !=
1660 (uint8_t)(c + bonuslen)) {
1662 "bad bonus: %s, obj %llu, off %d: %u != %u",
1664 ((uint8_t *)db->db_data)[c],
1665 (uint8_t)(c + bonuslen));
1669 dmu_buf_rele(db, FTAG);
1673 * We expect the word at endoff to be our object number.
1675 VERIFY(0 == dmu_read(os, object, endoff,
1676 sizeof (uint64_t), &temp));
1678 if (temp != object) {
1679 fatal(0, "bad data in %s, got %llu, expected %llu",
1680 osname, temp, object);
1684 * Destroy old object and clear batch entry.
1686 tx = dmu_tx_create(os);
1687 dmu_tx_hold_write(tx, batchobj,
1688 b * sizeof (uint64_t), sizeof (uint64_t));
1689 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1690 error = dmu_tx_assign(tx, TXG_WAIT);
1692 ztest_record_enospc("free object");
1696 error = dmu_object_free(os, object, tx);
1698 fatal(0, "dmu_object_free('%s', %llu) = %d",
1699 osname, object, error);
1703 dmu_object_set_checksum(os, batchobj,
1704 ztest_random_checksum(), tx);
1705 dmu_object_set_compress(os, batchobj,
1706 ztest_random_compress(), tx);
1708 dmu_write(os, batchobj, b * sizeof (uint64_t),
1709 sizeof (uint64_t), &object, tx);
1715 * Before creating the new batch of objects, generate a bunch of churn.
1717 for (b = ztest_random(100); b > 0; b--) {
1718 tx = dmu_tx_create(os);
1719 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1720 error = dmu_tx_assign(tx, TXG_WAIT);
1722 ztest_record_enospc("churn objects");
1726 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1727 DMU_OT_NONE, 0, tx);
1728 ztest_set_random_blocksize(os, object, tx);
1729 error = dmu_object_free(os, object, tx);
1731 fatal(0, "dmu_object_free('%s', %llu) = %d",
1732 osname, object, error);
1738 * Create a new batch of objects with randomly chosen
1739 * blocksizes and record them in the batch directory.
1741 for (b = 0; b < batchsize; b++) {
1742 uint32_t va_blksize;
1743 u_longlong_t va_nblocks;
1745 tx = dmu_tx_create(os);
1746 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1748 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1749 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1751 error = dmu_tx_assign(tx, TXG_WAIT);
1753 ztest_record_enospc("create batchobj");
1757 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1759 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1760 DMU_OT_PLAIN_OTHER, bonuslen, tx);
1762 ztest_set_random_blocksize(os, object, tx);
1764 dmu_object_set_checksum(os, object,
1765 ztest_random_checksum(), tx);
1766 dmu_object_set_compress(os, object,
1767 ztest_random_compress(), tx);
1769 dmu_write(os, batchobj, b * sizeof (uint64_t),
1770 sizeof (uint64_t), &object, tx);
1773 * Write to both the bonus buffer and the regular data.
1775 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1777 ASSERT3U(bonuslen, <=, db->db_size);
1779 dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1780 ASSERT3S(va_nblocks, >=, 0);
1782 dmu_buf_will_dirty(db, tx);
1785 * See comments above regarding the contents of
1786 * the bonus buffer and the word at endoff.
1788 for (c = 0; c < bonuslen; c++)
1789 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1791 dmu_buf_rele(db, FTAG);
1795 * Write to a large offset to increase indirection.
1797 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1804 * Verify that dmu_{read,write} work as expected.
1806 typedef struct bufwad {
1812 typedef struct dmu_read_write_dir {
1813 uint64_t dd_packobj;
1816 } dmu_read_write_dir_t;
1819 ztest_dmu_read_write(ztest_args_t *za)
1821 objset_t *os = za->za_os;
1822 dmu_read_write_dir_t dd;
1824 int i, freeit, error;
1826 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1827 uint64_t packoff, packsize, bigoff, bigsize;
1828 uint64_t regions = 997;
1829 uint64_t stride = 123456789ULL;
1830 uint64_t width = 40;
1831 int free_percent = 5;
1834 * This test uses two objects, packobj and bigobj, that are always
1835 * updated together (i.e. in the same tx) so that their contents are
1836 * in sync and can be compared. Their contents relate to each other
1837 * in a simple way: packobj is a dense array of 'bufwad' structures,
1838 * while bigobj is a sparse array of the same bufwads. Specifically,
1839 * for any index n, there are three bufwads that should be identical:
1841 * packobj, at offset n * sizeof (bufwad_t)
1842 * bigobj, at the head of the nth chunk
1843 * bigobj, at the tail of the nth chunk
1845 * The chunk size is arbitrary. It doesn't have to be a power of two,
1846 * and it doesn't have any relation to the object blocksize.
1847 * The only requirement is that it can hold at least two bufwads.
1849 * Normally, we write the bufwad to each of these locations.
1850 * However, free_percent of the time we instead write zeroes to
1851 * packobj and perform a dmu_free_range() on bigobj. By comparing
1852 * bigobj to packobj, we can verify that the DMU is correctly
1853 * tracking which parts of an object are allocated and free,
1854 * and that the contents of the allocated blocks are correct.
1858 * Read the directory info. If it's the first time, set things up.
1860 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1862 if (dd.dd_chunk == 0) {
1863 ASSERT(dd.dd_packobj == 0);
1864 ASSERT(dd.dd_bigobj == 0);
1865 tx = dmu_tx_create(os);
1866 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1867 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1868 error = dmu_tx_assign(tx, TXG_WAIT);
1870 ztest_record_enospc("create r/w directory");
1875 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1876 DMU_OT_NONE, 0, tx);
1877 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1878 DMU_OT_NONE, 0, tx);
1879 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
1881 ztest_set_random_blocksize(os, dd.dd_packobj, tx);
1882 ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
1884 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
1890 * Prefetch a random chunk of the big object.
1891 * Our aim here is to get some async reads in flight
1892 * for blocks that we may free below; the DMU should
1893 * handle this race correctly.
1895 n = ztest_random(regions) * stride + ztest_random(width);
1896 s = 1 + ztest_random(2 * width - 1);
1897 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
1900 * Pick a random index and compute the offsets into packobj and bigobj.
1902 n = ztest_random(regions) * stride + ztest_random(width);
1903 s = 1 + ztest_random(width - 1);
1905 packoff = n * sizeof (bufwad_t);
1906 packsize = s * sizeof (bufwad_t);
1908 bigoff = n * dd.dd_chunk;
1909 bigsize = s * dd.dd_chunk;
1911 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
1912 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
1915 * free_percent of the time, free a range of bigobj rather than
1918 freeit = (ztest_random(100) < free_percent);
1921 * Read the current contents of our objects.
1923 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf);
1924 ASSERT3U(error, ==, 0);
1925 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf);
1926 ASSERT3U(error, ==, 0);
1929 * Get a tx for the mods to both packobj and bigobj.
1931 tx = dmu_tx_create(os);
1933 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
1936 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
1938 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
1940 error = dmu_tx_assign(tx, TXG_WAIT);
1943 ztest_record_enospc("dmu r/w range");
1945 umem_free(packbuf, packsize);
1946 umem_free(bigbuf, bigsize);
1950 txg = dmu_tx_get_txg(tx);
1953 * For each index from n to n + s, verify that the existing bufwad
1954 * in packobj matches the bufwads at the head and tail of the
1955 * corresponding chunk in bigobj. Then update all three bufwads
1956 * with the new values we want to write out.
1958 for (i = 0; i < s; i++) {
1960 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
1962 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
1964 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
1966 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
1967 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
1969 if (pack->bw_txg > txg)
1970 fatal(0, "future leak: got %llx, open txg is %llx",
1973 if (pack->bw_data != 0 && pack->bw_index != n + i)
1974 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
1975 pack->bw_index, n, i);
1977 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
1978 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
1980 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
1981 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
1984 bzero(pack, sizeof (bufwad_t));
1986 pack->bw_index = n + i;
1988 pack->bw_data = 1 + ztest_random(-2ULL);
1995 * We've verified all the old bufwads, and made new ones.
1996 * Now write them out.
1998 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
2001 if (zopt_verbose >= 6) {
2002 (void) printf("freeing offset %llx size %llx"
2004 (u_longlong_t)bigoff,
2005 (u_longlong_t)bigsize,
2008 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
2011 if (zopt_verbose >= 6) {
2012 (void) printf("writing offset %llx size %llx"
2014 (u_longlong_t)bigoff,
2015 (u_longlong_t)bigsize,
2018 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
2024 * Sanity check the stuff we just wrote.
2027 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
2028 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
2030 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
2031 packsize, packcheck));
2032 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
2033 bigsize, bigcheck));
2035 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
2036 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
2038 umem_free(packcheck, packsize);
2039 umem_free(bigcheck, bigsize);
2042 umem_free(packbuf, packsize);
2043 umem_free(bigbuf, bigsize);
2047 ztest_dmu_check_future_leak(ztest_args_t *za)
2049 objset_t *os = za->za_os;
2051 ztest_block_tag_t *bt;
2052 dmu_object_info_t *doi = &za->za_doi;
2055 * Make sure that, if there is a write record in the bonus buffer
2056 * of the ZTEST_DIROBJ, that the txg for this record is <= the
2057 * last synced txg of the pool.
2059 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2061 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
2062 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
2063 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2064 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
2065 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
2066 if (bt->bt_objset != 0) {
2067 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
2068 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
2069 ASSERT3U(bt->bt_offset, ==, -1ULL);
2070 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
2072 dmu_buf_rele(db, FTAG);
2077 ztest_dmu_write_parallel(ztest_args_t *za)
2079 objset_t *os = za->za_os;
2080 ztest_block_tag_t *rbt = &za->za_rbt;
2081 ztest_block_tag_t *wbt = &za->za_wbt;
2082 const size_t btsize = sizeof (ztest_block_tag_t);
2085 int bs = ZTEST_DIROBJ_BLOCKSIZE;
2087 uint64_t off, txg, txg_how;
2089 char osname[MAXNAMELEN];
2090 char iobuf[SPA_MAXBLOCKSIZE];
2091 blkptr_t blk = { 0 };
2094 dmu_tx_t *tx = dmu_tx_create(os);
2096 dmu_objset_name(os, osname);
2099 * Have multiple threads write to large offsets in ZTEST_DIROBJ
2100 * to verify that having multiple threads writing to the same object
2101 * in parallel doesn't cause any trouble.
2103 if (ztest_random(4) == 0) {
2105 * Do the bonus buffer instead of a regular block.
2106 * We need a lock to serialize resize vs. others,
2107 * so we hash on the objset ID.
2109 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
2111 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
2113 b = ztest_random(ZTEST_SYNC_LOCKS);
2114 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
2115 if (ztest_random(4) == 0) {
2117 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
2119 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
2123 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
2124 error = dmu_tx_assign(tx, txg_how);
2126 if (error == ERESTART) {
2127 ASSERT(txg_how == TXG_NOWAIT);
2130 ztest_record_enospc("dmu write parallel");
2135 txg = dmu_tx_get_txg(tx);
2137 lp = &ztest_shared->zs_sync_lock[b];
2138 (void) mutex_lock(lp);
2140 wbt->bt_objset = dmu_objset_id(os);
2141 wbt->bt_object = ZTEST_DIROBJ;
2142 wbt->bt_offset = off;
2144 wbt->bt_thread = za->za_instance;
2145 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */
2148 * Occasionally, write an all-zero block to test the behavior
2149 * of blocks that compress into holes.
2151 if (off != -1ULL && ztest_random(8) == 0)
2155 dmu_object_info_t *doi = &za->za_doi;
2158 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2160 dmu_object_info_from_db(db, doi);
2161 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2162 ASSERT3U(doi->doi_bonus_size, >=, btsize);
2163 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2164 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2165 bcopy(dboff, rbt, btsize);
2166 if (rbt->bt_objset != 0) {
2167 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2168 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2169 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2170 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2172 if (ztest_random(10) == 0) {
2173 int newsize = (ztest_random(db->db_size /
2174 btsize) + 1) * btsize;
2176 ASSERT3U(newsize, >=, btsize);
2177 ASSERT3U(newsize, <=, db->db_size);
2178 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2179 dboff = (char *)db->db_data + newsize - btsize;
2181 dmu_buf_will_dirty(db, tx);
2182 bcopy(wbt, dboff, btsize);
2183 dmu_buf_rele(db, FTAG);
2185 } else if (do_free) {
2186 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2188 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2191 (void) mutex_unlock(lp);
2193 if (ztest_random(1000) == 0)
2194 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
2198 if (ztest_random(10000) == 0)
2199 txg_wait_synced(dmu_objset_pool(os), txg);
2201 if (off == -1ULL || do_free)
2204 if (ztest_random(2) != 0)
2208 * dmu_sync() the block we just wrote.
2210 (void) mutex_lock(lp);
2212 blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2213 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2216 dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n",
2217 osname, ZTEST_DIROBJ, blkoff, error);
2218 (void) mutex_unlock(lp);
2221 blkoff = off - blkoff;
2222 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2223 dmu_buf_rele(db, FTAG);
2226 (void) mutex_unlock(lp);
2229 dprintf("dmu_sync(%s, %d, %llx) = %d\n",
2230 osname, ZTEST_DIROBJ, off, error);
2234 if (blk.blk_birth == 0) /* concurrent free */
2237 txg_suspend(dmu_objset_pool(os));
2239 ASSERT(blk.blk_fill == 1);
2240 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2241 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2242 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2245 * Read the block that dmu_sync() returned to make sure its contents
2246 * match what we wrote. We do this while still txg_suspend()ed
2247 * to ensure that the block can't be reused before we read it.
2249 zb.zb_objset = dmu_objset_id(os);
2250 zb.zb_object = ZTEST_DIROBJ;
2252 zb.zb_blkid = off / bs;
2253 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2254 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2255 ASSERT3U(error, ==, 0);
2257 txg_resume(dmu_objset_pool(os));
2259 bcopy(&iobuf[blkoff], rbt, btsize);
2261 if (rbt->bt_objset == 0) /* concurrent free */
2264 if (wbt->bt_objset == 0) /* all-zero overwrite */
2267 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2268 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2269 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2272 * The semantic of dmu_sync() is that we always push the most recent
2273 * version of the data, so in the face of concurrent updates we may
2274 * see a newer version of the block. That's OK.
2276 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2277 if (rbt->bt_thread == wbt->bt_thread)
2278 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2280 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2284 * Verify that zap_{create,destroy,add,remove,update} work as expected.
2286 #define ZTEST_ZAP_MIN_INTS 1
2287 #define ZTEST_ZAP_MAX_INTS 4
2288 #define ZTEST_ZAP_MAX_PROPS 1000
2291 ztest_zap(ztest_args_t *za)
2293 objset_t *os = za->za_os;
2295 uint64_t txg, last_txg;
2296 uint64_t value[ZTEST_ZAP_MAX_INTS];
2297 uint64_t zl_ints, zl_intsize, prop;
2300 char propname[100], txgname[100];
2302 char osname[MAXNAMELEN];
2303 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2305 dmu_objset_name(os, osname);
2308 * Create a new object if necessary, and record it in the directory.
2310 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2311 sizeof (uint64_t), &object));
2314 tx = dmu_tx_create(os);
2315 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2317 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2318 error = dmu_tx_assign(tx, TXG_WAIT);
2320 ztest_record_enospc("create zap test obj");
2324 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2326 fatal(0, "zap_create('%s', %llu) = %d",
2327 osname, object, error);
2329 ASSERT(object != 0);
2330 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2331 sizeof (uint64_t), &object, tx);
2333 * Generate a known hash collision, and verify that
2334 * we can lookup and remove both entries.
2336 for (i = 0; i < 2; i++) {
2338 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2340 ASSERT3U(error, ==, 0);
2342 for (i = 0; i < 2; i++) {
2343 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2345 ASSERT3U(error, ==, EEXIST);
2346 error = zap_length(os, object, hc[i],
2347 &zl_intsize, &zl_ints);
2348 ASSERT3U(error, ==, 0);
2349 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2350 ASSERT3U(zl_ints, ==, 1);
2352 for (i = 0; i < 2; i++) {
2353 error = zap_remove(os, object, hc[i], tx);
2354 ASSERT3U(error, ==, 0);
2360 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2362 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2363 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2364 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2365 bzero(value, sizeof (value));
2369 * If these zap entries already exist, validate their contents.
2371 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2373 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2374 ASSERT3U(zl_ints, ==, 1);
2376 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2377 zl_ints, &last_txg) == 0);
2379 VERIFY(zap_length(os, object, propname, &zl_intsize,
2382 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2383 ASSERT3U(zl_ints, ==, ints);
2385 VERIFY(zap_lookup(os, object, propname, zl_intsize,
2386 zl_ints, value) == 0);
2388 for (i = 0; i < ints; i++) {
2389 ASSERT3U(value[i], ==, last_txg + object + i);
2392 ASSERT3U(error, ==, ENOENT);
2396 * Atomically update two entries in our zap object.
2397 * The first is named txg_%llu, and contains the txg
2398 * in which the property was last updated. The second
2399 * is named prop_%llu, and the nth element of its value
2400 * should be txg + object + n.
2402 tx = dmu_tx_create(os);
2403 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2404 error = dmu_tx_assign(tx, TXG_WAIT);
2406 ztest_record_enospc("create zap entry");
2410 txg = dmu_tx_get_txg(tx);
2413 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2415 for (i = 0; i < ints; i++)
2416 value[i] = txg + object + i;
2418 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2420 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2421 osname, object, txgname, error);
2423 error = zap_update(os, object, propname, sizeof (uint64_t),
2426 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2427 osname, object, propname, error);
2432 * Remove a random pair of entries.
2434 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2435 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2436 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2438 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2440 if (error == ENOENT)
2443 ASSERT3U(error, ==, 0);
2445 tx = dmu_tx_create(os);
2446 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2447 error = dmu_tx_assign(tx, TXG_WAIT);
2449 ztest_record_enospc("remove zap entry");
2453 error = zap_remove(os, object, txgname, tx);
2455 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2456 osname, object, txgname, error);
2458 error = zap_remove(os, object, propname, tx);
2460 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2461 osname, object, propname, error);
2466 * Once in a while, destroy the object.
2468 if (ztest_random(1000) != 0)
2471 tx = dmu_tx_create(os);
2472 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2473 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2474 error = dmu_tx_assign(tx, TXG_WAIT);
2476 ztest_record_enospc("destroy zap object");
2480 error = zap_destroy(os, object, tx);
2482 fatal(0, "zap_destroy('%s', %llu) = %d",
2483 osname, object, error);
2485 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2491 ztest_zap_parallel(ztest_args_t *za)
2493 objset_t *os = za->za_os;
2494 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2496 int i, namelen, error;
2497 char name[20], string_value[20];
2501 * Generate a random name of the form 'xxx.....' where each
2502 * x is a random printable character and the dots are dots.
2503 * There are 94 such characters, and the name length goes from
2504 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2506 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2508 for (i = 0; i < 3; i++)
2509 name[i] = '!' + ztest_random('~' - '!' + 1);
2510 for (; i < namelen - 1; i++)
2514 if (ztest_random(2) == 0)
2515 object = ZTEST_MICROZAP_OBJ;
2517 object = ZTEST_FATZAP_OBJ;
2519 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2520 wsize = sizeof (txg);
2526 data = string_value;
2530 VERIFY(zap_count(os, object, &count) == 0);
2531 ASSERT(count != -1ULL);
2534 * Select an operation: length, lookup, add, update, remove.
2536 i = ztest_random(5);
2539 tx = dmu_tx_create(os);
2540 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2541 error = dmu_tx_assign(tx, TXG_WAIT);
2543 ztest_record_enospc("zap parallel");
2547 txg = dmu_tx_get_txg(tx);
2548 bcopy(name, string_value, namelen);
2552 bzero(string_value, namelen);
2558 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
2560 ASSERT3U(wsize, ==, zl_wsize);
2561 ASSERT3U(wc, ==, zl_wc);
2563 ASSERT3U(error, ==, ENOENT);
2568 error = zap_lookup(os, object, name, wsize, wc, data);
2570 if (data == string_value &&
2571 bcmp(name, data, namelen) != 0)
2572 fatal(0, "name '%s' != val '%s' len %d",
2573 name, data, namelen);
2575 ASSERT3U(error, ==, ENOENT);
2580 error = zap_add(os, object, name, wsize, wc, data, tx);
2581 ASSERT(error == 0 || error == EEXIST);
2585 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
2589 error = zap_remove(os, object, name, tx);
2590 ASSERT(error == 0 || error == ENOENT);
2599 ztest_dsl_prop_get_set(ztest_args_t *za)
2601 objset_t *os = za->za_os;
2604 const char *prop, *valname;
2605 char setpoint[MAXPATHLEN];
2606 char osname[MAXNAMELEN];
2609 (void) rw_rdlock(&ztest_shared->zs_name_lock);
2611 dmu_objset_name(os, osname);
2613 for (i = 0; i < 2; i++) {
2616 value = ztest_random_checksum();
2617 inherit = (value == ZIO_CHECKSUM_INHERIT);
2619 prop = "compression";
2620 value = ztest_random_compress();
2621 inherit = (value == ZIO_COMPRESS_INHERIT);
2624 error = dsl_prop_set(osname, prop, sizeof (value),
2627 if (error == ENOSPC) {
2628 ztest_record_enospc("dsl_prop_set");
2632 ASSERT3U(error, ==, 0);
2634 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
2635 1, &value, setpoint), ==, 0);
2638 valname = zio_checksum_table[value].ci_name;
2640 valname = zio_compress_table[value].ci_name;
2642 if (zopt_verbose >= 6) {
2643 (void) printf("%s %s = %s for '%s'\n",
2644 osname, prop, valname, setpoint);
2648 (void) rw_unlock(&ztest_shared->zs_name_lock);
2652 * Inject random faults into the on-disk data.
2655 ztest_fault_inject(ztest_args_t *za)
2659 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2660 uint64_t bad = 0x1990c0ffeedecadeULL;
2662 char path0[MAXPATHLEN];
2663 char pathrand[MAXPATHLEN];
2665 spa_t *spa = za->za_spa;
2666 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
2668 int maxfaults = zopt_maxfaults;
2672 ASSERT(leaves >= 1);
2675 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
2677 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
2679 if (ztest_random(2) == 0) {
2681 * Inject errors on a normal data device.
2683 top = ztest_random(spa->spa_root_vdev->vdev_children);
2684 leaf = ztest_random(leaves);
2687 * Generate paths to the first leaf in this top-level vdev,
2688 * and to the random leaf we selected. We'll induce transient
2689 * write failures and random online/offline activity on leaf 0,
2690 * and we'll write random garbage to the randomly chosen leaf.
2692 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
2693 zopt_dir, zopt_pool, top * leaves + 0);
2694 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
2695 zopt_dir, zopt_pool, top * leaves + leaf);
2697 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
2698 if (vd0 != NULL && maxfaults != 1) {
2700 * Make vd0 explicitly claim to be unreadable,
2701 * or unwriteable, or reach behind its back
2702 * and close the underlying fd. We can do this if
2703 * maxfaults == 0 because we'll fail and reexecute,
2704 * and we can do it if maxfaults >= 2 because we'll
2705 * have enough redundancy. If maxfaults == 1, the
2706 * combination of this with injection of random data
2707 * corruption below exceeds the pool's fault tolerance.
2709 vdev_file_t *vf = vd0->vdev_tsd;
2711 if (vf != NULL && ztest_random(3) == 0) {
2712 (void) close(vf->vf_vnode->v_fd);
2713 vf->vf_vnode->v_fd = -1;
2714 } else if (ztest_random(2) == 0) {
2715 vd0->vdev_cant_read = B_TRUE;
2717 vd0->vdev_cant_write = B_TRUE;
2719 guid0 = vd0->vdev_guid;
2723 * Inject errors on an l2cache device.
2725 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2727 if (sav->sav_count == 0) {
2728 spa_config_exit(spa, SCL_STATE, FTAG);
2731 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
2732 guid0 = vd0->vdev_guid;
2733 (void) strcpy(path0, vd0->vdev_path);
2734 (void) strcpy(pathrand, vd0->vdev_path);
2738 maxfaults = INT_MAX; /* no limit on cache devices */
2741 dprintf("damaging %s and %s\n", path0, pathrand);
2743 spa_config_exit(spa, SCL_STATE, FTAG);
2749 * If we can tolerate two or more faults, randomly online/offline vd0.
2751 if (maxfaults >= 2 && guid0 != 0) {
2752 if (ztest_random(10) < 6)
2753 (void) vdev_offline(spa, guid0, B_TRUE);
2755 (void) vdev_online(spa, guid0, B_FALSE, NULL);
2759 * We have at least single-fault tolerance, so inject data corruption.
2761 fd = open(pathrand, O_RDWR);
2763 if (fd == -1) /* we hit a gap in the device namespace */
2766 fsize = lseek(fd, 0, SEEK_END);
2768 while (--iters != 0) {
2769 offset = ztest_random(fsize / (leaves << bshift)) *
2770 (leaves << bshift) + (leaf << bshift) +
2771 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
2773 if (offset >= fsize)
2776 if (zopt_verbose >= 6)
2777 (void) printf("injecting bad word into %s,"
2778 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
2780 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
2781 fatal(1, "can't inject bad word at 0x%llx in %s",
2792 ztest_scrub(ztest_args_t *za)
2794 spa_t *spa = za->za_spa;
2796 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2797 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
2798 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2802 * Rename the pool to a different name and then rename it back.
2805 ztest_spa_rename(ztest_args_t *za)
2807 char *oldname, *newname;
2811 (void) rw_wrlock(&ztest_shared->zs_name_lock);
2813 oldname = za->za_pool;
2814 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
2815 (void) strcpy(newname, oldname);
2816 (void) strcat(newname, "_tmp");
2821 error = spa_rename(oldname, newname);
2823 fatal(0, "spa_rename('%s', '%s') = %d", oldname,
2827 * Try to open it under the old name, which shouldn't exist
2829 error = spa_open(oldname, &spa, FTAG);
2830 if (error != ENOENT)
2831 fatal(0, "spa_open('%s') = %d", oldname, error);
2834 * Open it under the new name and make sure it's still the same spa_t.
2836 error = spa_open(newname, &spa, FTAG);
2838 fatal(0, "spa_open('%s') = %d", newname, error);
2840 ASSERT(spa == za->za_spa);
2841 spa_close(spa, FTAG);
2844 * Rename it back to the original
2846 error = spa_rename(newname, oldname);
2848 fatal(0, "spa_rename('%s', '%s') = %d", newname,
2852 * Make sure it can still be opened
2854 error = spa_open(oldname, &spa, FTAG);
2856 fatal(0, "spa_open('%s') = %d", oldname, error);
2858 ASSERT(spa == za->za_spa);
2859 spa_close(spa, FTAG);
2861 umem_free(newname, strlen(newname) + 1);
2863 (void) rw_unlock(&ztest_shared->zs_name_lock);
2868 * Completely obliterate one disk.
2871 ztest_obliterate_one_disk(uint64_t vdev)
2874 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
2877 if (zopt_maxfaults < 2)
2880 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2881 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
2883 fd = open(dev_name, O_RDWR);
2886 fatal(1, "can't open %s", dev_name);
2889 * Determine the size.
2891 fsize = lseek(fd, 0, SEEK_END);
2896 * Rename the old device to dev_name.old (useful for debugging).
2898 VERIFY(rename(dev_name, copy_name) == 0);
2903 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
2904 VERIFY(ftruncate(fd, fsize) == 0);
2909 ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
2911 char dev_name[MAXPATHLEN];
2917 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2920 * Build the nvlist describing dev_name.
2922 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
2924 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2925 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
2928 guid = vd->vdev_guid;
2929 spa_config_exit(spa, SCL_VDEV, FTAG);
2930 error = spa_vdev_attach(spa, guid, root, B_TRUE);
2936 fatal(0, "spa_vdev_attach(in-place) = %d", error);
2942 ztest_verify_blocks(char *pool)
2945 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
2953 if (realpath(progname, zdb) == NULL)
2954 assert(!"realpath() failed");
2956 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
2957 bin = strstr(zdb, "/usr/bin/");
2958 ztest = strstr(bin, "/ztest");
2960 isalen = ztest - isa;
2964 "/usr/sbin%.*s/zdb -bc%s%s -U /tmp/zpool.cache -O %s %s",
2967 zopt_verbose >= 3 ? "s" : "",
2968 zopt_verbose >= 4 ? "v" : "",
2969 ztest_random(2) == 0 ? "pre" : "post", pool);
2972 if (zopt_verbose >= 5)
2973 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
2975 fp = popen(zdb, "r");
2978 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
2979 if (zopt_verbose >= 3)
2980 (void) printf("%s", zbuf);
2982 status = pclose(fp);
2987 ztest_dump_core = 0;
2988 if (WIFEXITED(status))
2989 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
2991 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
2995 ztest_walk_pool_directory(char *header)
2999 if (zopt_verbose >= 6)
3000 (void) printf("%s\n", header);
3002 mutex_enter(&spa_namespace_lock);
3003 while ((spa = spa_next(spa)) != NULL)
3004 if (zopt_verbose >= 6)
3005 (void) printf("\t%s\n", spa_name(spa));
3006 mutex_exit(&spa_namespace_lock);
3010 ztest_spa_import_export(char *oldname, char *newname)
3017 if (zopt_verbose >= 4) {
3018 (void) printf("import/export: old = %s, new = %s\n",
3023 * Clean up from previous runs.
3025 (void) spa_destroy(newname);
3028 * Get the pool's configuration and guid.
3030 error = spa_open(oldname, &spa, FTAG);
3032 fatal(0, "spa_open('%s') = %d", oldname, error);
3034 pool_guid = spa_guid(spa);
3035 spa_close(spa, FTAG);
3037 ztest_walk_pool_directory("pools before export");
3042 error = spa_export(oldname, &config, B_FALSE);
3044 fatal(0, "spa_export('%s') = %d", oldname, error);
3046 ztest_walk_pool_directory("pools after export");
3049 * Import it under the new name.
3051 error = spa_import(newname, config, NULL);
3053 fatal(0, "spa_import('%s') = %d", newname, error);
3055 ztest_walk_pool_directory("pools after import");
3058 * Try to import it again -- should fail with EEXIST.
3060 error = spa_import(newname, config, NULL);
3061 if (error != EEXIST)
3062 fatal(0, "spa_import('%s') twice", newname);
3065 * Try to import it under a different name -- should fail with EEXIST.
3067 error = spa_import(oldname, config, NULL);
3068 if (error != EEXIST)
3069 fatal(0, "spa_import('%s') under multiple names", newname);
3072 * Verify that the pool is no longer visible under the old name.
3074 error = spa_open(oldname, &spa, FTAG);
3075 if (error != ENOENT)
3076 fatal(0, "spa_open('%s') = %d", newname, error);
3079 * Verify that we can open and close the pool using the new name.
3081 error = spa_open(newname, &spa, FTAG);
3083 fatal(0, "spa_open('%s') = %d", newname, error);
3084 ASSERT(pool_guid == spa_guid(spa));
3085 spa_close(spa, FTAG);
3087 nvlist_free(config);
3091 ztest_resume(void *arg)
3095 while (!ztest_exiting) {
3096 (void) poll(NULL, 0, 1000);
3098 if (!spa_suspended(spa))
3101 spa_vdev_state_enter(spa);
3102 vdev_clear(spa, NULL);
3103 (void) spa_vdev_state_exit(spa, NULL, 0);
3111 ztest_thread(void *arg)
3113 ztest_args_t *za = arg;
3114 ztest_shared_t *zs = ztest_shared;
3115 hrtime_t now, functime;
3119 while ((now = gethrtime()) < za->za_stop) {
3121 * See if it's time to force a crash.
3123 if (now > za->za_kill) {
3124 zs->zs_alloc = spa_get_alloc(za->za_spa);
3125 zs->zs_space = spa_get_space(za->za_spa);
3126 (void) kill(getpid(), SIGKILL);
3130 * Pick a random function.
3132 f = ztest_random(ZTEST_FUNCS);
3133 zi = &zs->zs_info[f];
3136 * Decide whether to call it, based on the requested frequency.
3138 if (zi->zi_call_target == 0 ||
3139 (double)zi->zi_call_total / zi->zi_call_target >
3140 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
3143 atomic_add_64(&zi->zi_calls, 1);
3144 atomic_add_64(&zi->zi_call_total, 1);
3146 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3148 za->za_diroff_shared = (1ULL << 63);
3150 for (i = 0; i < zi->zi_iters; i++)
3153 functime = gethrtime() - now;
3155 atomic_add_64(&zi->zi_call_time, functime);
3157 if (zopt_verbose >= 4) {
3159 (void) dladdr((void *)zi->zi_func, &dli);
3160 (void) printf("%6.2f sec in %s\n",
3161 (double)functime / NANOSEC, dli.dli_sname);
3165 * If we're getting ENOSPC with some regularity, stop.
3167 if (zs->zs_enospc_count > 10)
3175 * Kick off threads to run tests on all datasets in parallel.
3178 ztest_run(char *pool)
3181 ztest_shared_t *zs = ztest_shared;
3185 thread_t resume_tid;
3187 ztest_exiting = B_FALSE;
3189 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3190 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3192 for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3193 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3196 * Destroy one disk before we even start.
3197 * It's mirrored, so everything should work just fine.
3198 * This makes us exercise fault handling very early in spa_load().
3200 ztest_obliterate_one_disk(0);
3203 * Verify that the sum of the sizes of all blocks in the pool
3204 * equals the SPA's allocated space total.
3206 ztest_verify_blocks(pool);
3209 * Kick off a replacement of the disk we just obliterated.
3211 kernel_init(FREAD | FWRITE);
3212 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3213 ztest_replace_one_disk(spa, 0);
3214 if (zopt_verbose >= 5)
3215 show_pool_stats(spa);
3216 spa_close(spa, FTAG);
3219 kernel_init(FREAD | FWRITE);
3222 * Verify that we can export the pool and reimport it under a
3225 if (ztest_random(2) == 0) {
3226 (void) snprintf(name, 100, "%s_import", pool);
3227 ztest_spa_import_export(pool, name);
3228 ztest_spa_import_export(name, pool);
3232 * Verify that we can loop over all pools.
3234 mutex_enter(&spa_namespace_lock);
3235 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3236 if (zopt_verbose > 3) {
3237 (void) printf("spa_next: found %s\n", spa_name(spa));
3240 mutex_exit(&spa_namespace_lock);
3245 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3248 * Create a thread to periodically resume suspended I/O.
3250 VERIFY(thr_create(0, 0, ztest_resume, spa, THR_BOUND,
3254 * Verify that we can safely inquire about about any object,
3255 * whether it's allocated or not. To make it interesting,
3256 * we probe a 5-wide window around each power of two.
3257 * This hits all edge cases, including zero and the max.
3259 for (t = 0; t < 64; t++) {
3260 for (d = -5; d <= 5; d++) {
3261 error = dmu_object_info(spa->spa_meta_objset,
3262 (1ULL << t) + d, NULL);
3263 ASSERT(error == 0 || error == ENOENT ||
3269 * Now kick off all the tests that run in parallel.
3271 zs->zs_enospc_count = 0;
3273 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3275 if (zopt_verbose >= 4)
3276 (void) printf("starting main threads...\n");
3278 za[0].za_start = gethrtime();
3279 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3280 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3281 za[0].za_kill = za[0].za_stop;
3282 if (ztest_random(100) < zopt_killrate)
3283 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3285 for (t = 0; t < zopt_threads; t++) {
3286 d = t % zopt_datasets;
3288 (void) strcpy(za[t].za_pool, pool);
3289 za[t].za_os = za[d].za_os;
3291 za[t].za_zilog = za[d].za_zilog;
3292 za[t].za_instance = t;
3293 za[t].za_random = ztest_random(-1ULL);
3294 za[t].za_start = za[0].za_start;
3295 za[t].za_stop = za[0].za_stop;
3296 za[t].za_kill = za[0].za_kill;
3298 if (t < zopt_datasets) {
3300 int test_future = FALSE;
3301 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3302 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3303 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
3304 ztest_create_cb, NULL);
3305 if (error == EEXIST) {
3307 } else if (error == ENOSPC) {
3308 zs->zs_enospc_count++;
3309 (void) rw_unlock(&ztest_shared->zs_name_lock);
3311 } else if (error != 0) {
3312 fatal(0, "dmu_objset_create(%s) = %d",
3315 error = dmu_objset_open(name, DMU_OST_OTHER,
3316 DS_MODE_USER, &za[d].za_os);
3318 fatal(0, "dmu_objset_open('%s') = %d",
3320 (void) rw_unlock(&ztest_shared->zs_name_lock);
3322 ztest_dmu_check_future_leak(&za[t]);
3323 zr.zr_os = za[d].za_os;
3324 zil_replay(zr.zr_os, &zr, &zr.zr_assign,
3325 ztest_replay_vector, NULL);
3326 za[d].za_zilog = zil_open(za[d].za_os, NULL);
3329 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3330 &za[t].za_thread) == 0);
3334 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3336 traverse_fini(za[t].za_th);
3337 if (t < zopt_datasets) {
3338 zil_close(za[t].za_zilog);
3339 dmu_objset_close(za[t].za_os);
3343 if (zopt_verbose >= 3)
3344 show_pool_stats(spa);
3346 txg_wait_synced(spa_get_dsl(spa), 0);
3348 zs->zs_alloc = spa_get_alloc(spa);
3349 zs->zs_space = spa_get_space(spa);
3352 * If we had out-of-space errors, destroy a random objset.
3354 if (zs->zs_enospc_count != 0) {
3355 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3356 d = (int)ztest_random(zopt_datasets);
3357 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3358 if (zopt_verbose >= 3)
3359 (void) printf("Destroying %s to free up space\n", name);
3360 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
3361 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3362 (void) rw_unlock(&ztest_shared->zs_name_lock);
3365 txg_wait_synced(spa_get_dsl(spa), 0);
3367 umem_free(za, zopt_threads * sizeof (ztest_args_t));
3369 /* Kill the resume thread */
3370 ztest_exiting = B_TRUE;
3371 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
3374 * Right before closing the pool, kick off a bunch of async I/O;
3375 * spa_close() should wait for it to complete.
3377 for (t = 1; t < 50; t++)
3378 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3380 spa_close(spa, FTAG);
3386 print_time(hrtime_t t, char *timebuf)
3388 hrtime_t s = t / NANOSEC;
3389 hrtime_t m = s / 60;
3390 hrtime_t h = m / 60;
3391 hrtime_t d = h / 24;
3400 (void) sprintf(timebuf,
3401 "%llud%02lluh%02llum%02llus", d, h, m, s);
3403 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3405 (void) sprintf(timebuf, "%llum%02llus", m, s);
3407 (void) sprintf(timebuf, "%llus", s);
3411 * Create a storage pool with the given name and initial vdev size.
3412 * Then create the specified number of datasets in the pool.
3415 ztest_init(char *pool)
3421 kernel_init(FREAD | FWRITE);
3424 * Create the storage pool.
3426 (void) spa_destroy(pool);
3427 ztest_shared->zs_vdev_primaries = 0;
3428 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
3429 0, zopt_raidz, zopt_mirrors, 1);
3430 error = spa_create(pool, nvroot, NULL, NULL, NULL);
3431 nvlist_free(nvroot);
3434 fatal(0, "spa_create() = %d", error);
3435 error = spa_open(pool, &spa, FTAG);
3437 fatal(0, "spa_open() = %d", error);
3439 if (zopt_verbose >= 3)
3440 show_pool_stats(spa);
3442 spa_close(spa, FTAG);
3448 main(int argc, char **argv)
3458 (void) setvbuf(stdout, NULL, _IOLBF, 0);
3460 /* Override location of zpool.cache */
3461 spa_config_path = "/tmp/zpool.cache";
3463 ztest_random_fd = open("/dev/urandom", O_RDONLY);
3465 process_options(argc, argv);
3470 dprintf_setup(&argc, argv);
3473 * Blow away any existing copy of zpool.cache
3476 (void) remove("/tmp/zpool.cache");
3478 zs = ztest_shared = (void *)mmap(0,
3479 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3480 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3482 if (zopt_verbose >= 1) {
3483 (void) printf("%llu vdevs, %d datasets, %d threads,"
3484 " %llu seconds...\n",
3485 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3486 (u_longlong_t)zopt_time);
3490 * Create and initialize our storage pool.
3492 for (i = 1; i <= zopt_init; i++) {
3493 bzero(zs, sizeof (ztest_shared_t));
3494 if (zopt_verbose >= 3 && zopt_init != 1)
3495 (void) printf("ztest_init(), pass %d\n", i);
3496 ztest_init(zopt_pool);
3500 * Initialize the call targets for each function.
3502 for (f = 0; f < ZTEST_FUNCS; f++) {
3503 zi = &zs->zs_info[f];
3505 *zi = ztest_info[f];
3507 if (*zi->zi_interval == 0)
3508 zi->zi_call_target = UINT64_MAX;
3510 zi->zi_call_target = zopt_time / *zi->zi_interval;
3513 zs->zs_start_time = gethrtime();
3514 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3517 * Run the tests in a loop. These tests include fault injection
3518 * to verify that self-healing data works, and forced crashes
3519 * to verify that we never lose on-disk consistency.
3521 while (gethrtime() < zs->zs_stop_time) {
3527 * Initialize the workload counters for each function.
3529 for (f = 0; f < ZTEST_FUNCS; f++) {
3530 zi = &zs->zs_info[f];
3532 zi->zi_call_time = 0;
3538 fatal(1, "fork failed");
3540 if (pid == 0) { /* child */
3541 struct rlimit rl = { 1024, 1024 };
3542 (void) setrlimit(RLIMIT_NOFILE, &rl);
3543 (void) enable_extended_FILE_stdio(-1, -1);
3544 ztest_run(zopt_pool);
3548 while (waitpid(pid, &status, 0) != pid)
3551 if (WIFEXITED(status)) {
3552 if (WEXITSTATUS(status) != 0) {
3553 (void) fprintf(stderr,
3554 "child exited with code %d\n",
3555 WEXITSTATUS(status));
3558 } else if (WIFSIGNALED(status)) {
3559 if (WTERMSIG(status) != SIGKILL) {
3560 (void) fprintf(stderr,
3561 "child died with signal %d\n",
3567 (void) fprintf(stderr, "something strange happened "
3574 if (zopt_verbose >= 1) {
3575 hrtime_t now = gethrtime();
3577 now = MIN(now, zs->zs_stop_time);
3578 print_time(zs->zs_stop_time - now, timebuf);
3579 nicenum(zs->zs_space, numbuf);
3581 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
3582 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
3584 WIFEXITED(status) ? "Complete" : "SIGKILL",
3585 (u_longlong_t)zs->zs_enospc_count,
3586 100.0 * zs->zs_alloc / zs->zs_space,
3588 100.0 * (now - zs->zs_start_time) /
3589 (zopt_time * NANOSEC), timebuf);
3592 if (zopt_verbose >= 2) {
3593 (void) printf("\nWorkload summary:\n\n");
3594 (void) printf("%7s %9s %s\n",
3595 "Calls", "Time", "Function");
3596 (void) printf("%7s %9s %s\n",
3597 "-----", "----", "--------");
3598 for (f = 0; f < ZTEST_FUNCS; f++) {
3601 zi = &zs->zs_info[f];
3602 print_time(zi->zi_call_time, timebuf);
3603 (void) dladdr((void *)zi->zi_func, &dli);
3604 (void) printf("%7llu %9s %s\n",
3605 (u_longlong_t)zi->zi_calls, timebuf,
3608 (void) printf("\n");
3612 * It's possible that we killed a child during a rename test, in
3613 * which case we'll have a 'ztest_tmp' pool lying around instead
3614 * of 'ztest'. Do a blind rename in case this happened.
3616 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
3617 (void) strcpy(tmp, zopt_pool);
3618 (void) strcat(tmp, "_tmp");
3619 kernel_init(FREAD | FWRITE);
3620 (void) spa_rename(tmp, zopt_pool);
3622 umem_free(tmp, strlen(tmp) + 1);
3625 ztest_verify_blocks(zopt_pool);
3627 if (zopt_verbose >= 1) {
3628 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
3629 kills, iters - kills, (100.0 * kills) / MAX(1, iters));