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_objset.h>
86 #include <sys/resource.h>
88 #include <sys/zio_checksum.h>
89 #include <sys/zio_compress.h>
91 #include <sys/vdev_impl.h>
92 #include <sys/vdev_file.h>
93 #include <sys/spa_impl.h>
94 #include <sys/dsl_prop.h>
95 #include <sys/refcount.h>
97 #include <stdio_ext.h>
105 #include <sys/fs/zfs.h>
107 static char cmdname[] = "ztest";
108 static char *zopt_pool = cmdname;
110 static uint64_t zopt_vdevs = 5;
111 static uint64_t zopt_vdevtime;
112 static int zopt_ashift = SPA_MINBLOCKSHIFT;
113 static int zopt_mirrors = 2;
114 static int zopt_raidz = 4;
115 static int zopt_raidz_parity = 1;
116 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
117 static int zopt_datasets = 7;
118 static int zopt_threads = 23;
119 static uint64_t zopt_passtime = 60; /* 60 seconds */
120 static uint64_t zopt_killrate = 70; /* 70% kill rate */
121 static int zopt_verbose = 0;
122 static int zopt_init = 1;
123 static char *zopt_dir = "/tmp";
124 static uint64_t zopt_time = 300; /* 5 minutes */
125 static int zopt_maxfaults;
127 typedef struct ztest_block_tag {
136 typedef struct ztest_args {
137 char za_pool[MAXNAMELEN];
142 uint64_t za_instance;
145 uint64_t za_diroff_shared;
151 * Thread-local variables can go here to aid debugging.
153 ztest_block_tag_t za_rbt;
154 ztest_block_tag_t za_wbt;
155 dmu_object_info_t za_doi;
159 typedef void ztest_func_t(ztest_args_t *);
162 * Note: these aren't static because we want dladdr() to work.
164 ztest_func_t ztest_dmu_read_write;
165 ztest_func_t ztest_dmu_write_parallel;
166 ztest_func_t ztest_dmu_object_alloc_free;
167 ztest_func_t ztest_zap;
168 ztest_func_t ztest_zap_parallel;
169 ztest_func_t ztest_traverse;
170 ztest_func_t ztest_dsl_prop_get_set;
171 ztest_func_t ztest_dmu_objset_create_destroy;
172 ztest_func_t ztest_dmu_snapshot_create_destroy;
173 ztest_func_t ztest_spa_create_destroy;
174 ztest_func_t ztest_fault_inject;
175 ztest_func_t ztest_spa_rename;
176 ztest_func_t ztest_vdev_attach_detach;
177 ztest_func_t ztest_vdev_LUN_growth;
178 ztest_func_t ztest_vdev_add_remove;
179 ztest_func_t ztest_vdev_aux_add_remove;
180 ztest_func_t ztest_scrub;
182 typedef struct ztest_info {
183 ztest_func_t *zi_func; /* test function */
184 uint64_t zi_iters; /* iterations per execution */
185 uint64_t *zi_interval; /* execute every <interval> seconds */
186 uint64_t zi_calls; /* per-pass count */
187 uint64_t zi_call_time; /* per-pass time */
188 uint64_t zi_call_total; /* cumulative total */
189 uint64_t zi_call_target; /* target cumulative total */
192 uint64_t zopt_always = 0; /* all the time */
193 uint64_t zopt_often = 1; /* every second */
194 uint64_t zopt_sometimes = 10; /* every 10 seconds */
195 uint64_t zopt_rarely = 60; /* every 60 seconds */
197 ztest_info_t ztest_info[] = {
198 { ztest_dmu_read_write, 1, &zopt_always },
199 { ztest_dmu_write_parallel, 30, &zopt_always },
200 { ztest_dmu_object_alloc_free, 1, &zopt_always },
201 { ztest_zap, 30, &zopt_always },
202 { ztest_zap_parallel, 100, &zopt_always },
203 { ztest_dsl_prop_get_set, 1, &zopt_sometimes },
204 { ztest_dmu_objset_create_destroy, 1, &zopt_sometimes },
205 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
206 { ztest_spa_create_destroy, 1, &zopt_sometimes },
207 { ztest_fault_inject, 1, &zopt_sometimes },
208 { ztest_spa_rename, 1, &zopt_rarely },
209 { ztest_vdev_attach_detach, 1, &zopt_rarely },
210 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
211 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
212 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
213 { ztest_scrub, 1, &zopt_vdevtime },
216 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
218 #define ZTEST_SYNC_LOCKS 16
221 * Stuff we need to share writably between parent and child.
223 typedef struct ztest_shared {
224 mutex_t zs_vdev_lock;
225 rwlock_t zs_name_lock;
226 uint64_t zs_vdev_primaries;
227 uint64_t zs_vdev_aux;
228 uint64_t zs_enospc_count;
229 hrtime_t zs_start_time;
230 hrtime_t zs_stop_time;
233 ztest_info_t zs_info[ZTEST_FUNCS];
234 mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
235 uint64_t zs_seq[ZTEST_SYNC_LOCKS];
238 static char ztest_dev_template[] = "%s/%s.%llua";
239 static char ztest_aux_template[] = "%s/%s.%s.%llu";
240 static ztest_shared_t *ztest_shared;
242 static int ztest_random_fd;
243 static int ztest_dump_core = 1;
245 static boolean_t ztest_exiting;
247 extern uint64_t metaslab_gang_bang;
249 #define ZTEST_DIROBJ 1
250 #define ZTEST_MICROZAP_OBJ 2
251 #define ZTEST_FATZAP_OBJ 3
253 #define ZTEST_DIROBJ_BLOCKSIZE (1 << 10)
254 #define ZTEST_DIRSIZE 256
256 static void usage(boolean_t) __NORETURN;
259 * These libumem hooks provide a reasonable set of defaults for the allocator's
260 * debugging facilities.
265 return ("default,verbose"); /* $UMEM_DEBUG setting */
269 _umem_logging_init(void)
271 return ("fail,contents"); /* $UMEM_LOGGING setting */
274 #define FATAL_MSG_SZ 1024
279 fatal(int do_perror, char *message, ...)
282 int save_errno = errno;
283 char buf[FATAL_MSG_SZ];
285 (void) fflush(stdout);
287 va_start(args, message);
288 (void) sprintf(buf, "ztest: ");
290 (void) vsprintf(buf + strlen(buf), message, args);
293 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
294 ": %s", strerror(save_errno));
296 (void) fprintf(stderr, "%s\n", buf);
297 fatal_msg = buf; /* to ease debugging */
304 str2shift(const char *buf)
306 const char *ends = "BKMGTPEZ";
311 for (i = 0; i < strlen(ends); i++) {
312 if (toupper(buf[0]) == ends[i])
315 if (i == strlen(ends)) {
316 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
320 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
323 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
329 nicenumtoull(const char *buf)
334 val = strtoull(buf, &end, 0);
336 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
338 } else if (end[0] == '.') {
339 double fval = strtod(buf, &end);
340 fval *= pow(2, str2shift(end));
341 if (fval > UINT64_MAX) {
342 (void) fprintf(stderr, "ztest: value too large: %s\n",
346 val = (uint64_t)fval;
348 int shift = str2shift(end);
349 if (shift >= 64 || (val << shift) >> shift != val) {
350 (void) fprintf(stderr, "ztest: value too large: %s\n",
360 usage(boolean_t requested)
362 char nice_vdev_size[10];
363 char nice_gang_bang[10];
364 FILE *fp = requested ? stdout : stderr;
366 nicenum(zopt_vdev_size, nice_vdev_size);
367 nicenum(metaslab_gang_bang, nice_gang_bang);
369 (void) fprintf(fp, "Usage: %s\n"
370 "\t[-v vdevs (default: %llu)]\n"
371 "\t[-s size_of_each_vdev (default: %s)]\n"
372 "\t[-a alignment_shift (default: %d) (use 0 for random)]\n"
373 "\t[-m mirror_copies (default: %d)]\n"
374 "\t[-r raidz_disks (default: %d)]\n"
375 "\t[-R raidz_parity (default: %d)]\n"
376 "\t[-d datasets (default: %d)]\n"
377 "\t[-t threads (default: %d)]\n"
378 "\t[-g gang_block_threshold (default: %s)]\n"
379 "\t[-i initialize pool i times (default: %d)]\n"
380 "\t[-k kill percentage (default: %llu%%)]\n"
381 "\t[-p pool_name (default: %s)]\n"
382 "\t[-f file directory for vdev files (default: %s)]\n"
383 "\t[-V(erbose)] (use multiple times for ever more blather)\n"
384 "\t[-E(xisting)] (use existing pool instead of creating new one)\n"
385 "\t[-T time] total run time (default: %llu sec)\n"
386 "\t[-P passtime] time per pass (default: %llu sec)\n"
387 "\t[-h] (print help)\n"
390 (u_longlong_t)zopt_vdevs, /* -v */
391 nice_vdev_size, /* -s */
392 zopt_ashift, /* -a */
393 zopt_mirrors, /* -m */
395 zopt_raidz_parity, /* -R */
396 zopt_datasets, /* -d */
397 zopt_threads, /* -t */
398 nice_gang_bang, /* -g */
400 (u_longlong_t)zopt_killrate, /* -k */
403 (u_longlong_t)zopt_time, /* -T */
404 (u_longlong_t)zopt_passtime); /* -P */
405 exit(requested ? 0 : 1);
409 ztest_random(uint64_t range)
416 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
417 fatal(1, "short read from /dev/urandom");
424 ztest_record_enospc(char *s)
426 ztest_shared->zs_enospc_count++;
430 process_options(int argc, char **argv)
435 /* By default, test gang blocks for blocks 32K and greater */
436 metaslab_gang_bang = 32 << 10;
438 while ((opt = getopt(argc, argv,
439 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:h")) != EOF) {
455 value = nicenumtoull(optarg);
462 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
468 zopt_mirrors = value;
471 zopt_raidz = MAX(1, value);
474 zopt_raidz_parity = MIN(MAX(value, 1), 2);
477 zopt_datasets = MAX(1, value);
480 zopt_threads = MAX(1, value);
483 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
489 zopt_killrate = value;
492 zopt_pool = strdup(optarg);
495 zopt_dir = strdup(optarg);
507 zopt_passtime = MAX(1, value);
519 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
521 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time / zopt_vdevs : UINT64_MAX);
522 zopt_maxfaults = MAX(zopt_mirrors, 1) * (zopt_raidz_parity + 1) - 1;
526 ztest_get_ashift(void)
528 if (zopt_ashift == 0)
529 return (SPA_MINBLOCKSHIFT + ztest_random(3));
530 return (zopt_ashift);
534 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
536 char pathbuf[MAXPATHLEN];
541 ashift = ztest_get_ashift();
547 vdev = ztest_shared->zs_vdev_aux;
548 (void) sprintf(path, ztest_aux_template,
549 zopt_dir, zopt_pool, aux, vdev);
551 vdev = ztest_shared->zs_vdev_primaries++;
552 (void) sprintf(path, ztest_dev_template,
553 zopt_dir, zopt_pool, vdev);
558 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
560 fatal(1, "can't open %s", path);
561 if (ftruncate(fd, size) != 0)
562 fatal(1, "can't ftruncate %s", path);
566 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
567 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
568 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
569 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
575 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
577 nvlist_t *raidz, **child;
581 return (make_vdev_file(path, aux, size, ashift));
582 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
584 for (c = 0; c < r; c++)
585 child[c] = make_vdev_file(path, aux, size, ashift);
587 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
588 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
589 VDEV_TYPE_RAIDZ) == 0);
590 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
591 zopt_raidz_parity) == 0);
592 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
595 for (c = 0; c < r; c++)
596 nvlist_free(child[c]);
598 umem_free(child, r * sizeof (nvlist_t *));
604 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
607 nvlist_t *mirror, **child;
611 return (make_vdev_raidz(path, aux, size, ashift, r));
613 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
615 for (c = 0; c < m; c++)
616 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
618 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
619 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
620 VDEV_TYPE_MIRROR) == 0);
621 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
624 for (c = 0; c < m; c++)
625 nvlist_free(child[c]);
627 umem_free(child, m * sizeof (nvlist_t *));
633 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
634 int log, int r, int m, int t)
636 nvlist_t *root, **child;
641 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
643 for (c = 0; c < t; c++) {
644 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
645 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
649 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
650 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
651 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
654 for (c = 0; c < t; c++)
655 nvlist_free(child[c]);
657 umem_free(child, t * sizeof (nvlist_t *));
663 ztest_set_random_blocksize(objset_t *os, uint64_t object, dmu_tx_t *tx)
665 int bs = SPA_MINBLOCKSHIFT +
666 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1);
667 int ibs = DN_MIN_INDBLKSHIFT +
668 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1);
671 error = dmu_object_set_blocksize(os, object, 1ULL << bs, ibs, tx);
674 dmu_objset_name(os, osname);
675 fatal(0, "dmu_object_set_blocksize('%s', %llu, %d, %d) = %d",
676 osname, object, 1 << bs, ibs, error);
681 ztest_random_checksum(void)
686 checksum = ztest_random(ZIO_CHECKSUM_FUNCTIONS);
687 } while (zio_checksum_table[checksum].ci_zbt);
689 if (checksum == ZIO_CHECKSUM_OFF)
690 checksum = ZIO_CHECKSUM_ON;
696 ztest_random_compress(void)
698 return ((uint8_t)ztest_random(ZIO_COMPRESS_FUNCTIONS));
702 ztest_replay_create(objset_t *os, lr_create_t *lr, boolean_t byteswap)
708 byteswap_uint64_array(lr, sizeof (*lr));
710 tx = dmu_tx_create(os);
711 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
712 error = dmu_tx_assign(tx, TXG_WAIT);
718 error = dmu_object_claim(os, lr->lr_doid, lr->lr_mode, 0,
720 ASSERT3U(error, ==, 0);
723 if (zopt_verbose >= 5) {
724 char osname[MAXNAMELEN];
725 dmu_objset_name(os, osname);
726 (void) printf("replay create of %s object %llu"
727 " in txg %llu = %d\n",
728 osname, (u_longlong_t)lr->lr_doid,
729 (u_longlong_t)dmu_tx_get_txg(tx), error);
736 ztest_replay_remove(objset_t *os, lr_remove_t *lr, boolean_t byteswap)
742 byteswap_uint64_array(lr, sizeof (*lr));
744 tx = dmu_tx_create(os);
745 dmu_tx_hold_free(tx, lr->lr_doid, 0, DMU_OBJECT_END);
746 error = dmu_tx_assign(tx, TXG_WAIT);
752 error = dmu_object_free(os, lr->lr_doid, tx);
758 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
759 NULL, /* 0 no such transaction type */
760 ztest_replay_create, /* TX_CREATE */
762 NULL, /* TX_MKXATTR */
763 NULL, /* TX_SYMLINK */
764 ztest_replay_remove, /* TX_REMOVE */
767 NULL, /* TX_RENAME */
769 NULL, /* TX_TRUNCATE */
770 NULL, /* TX_SETATTR */
775 * Verify that we can't destroy an active pool, create an existing pool,
776 * or create a pool with a bad vdev spec.
779 ztest_spa_create_destroy(ztest_args_t *za)
786 * Attempt to create using a bad file.
788 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
789 error = spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL);
792 fatal(0, "spa_create(bad_file) = %d", error);
795 * Attempt to create using a bad mirror.
797 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
798 error = spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL);
801 fatal(0, "spa_create(bad_mirror) = %d", error);
804 * Attempt to create an existing pool. It shouldn't matter
805 * what's in the nvroot; we should fail with EEXIST.
807 (void) rw_rdlock(&ztest_shared->zs_name_lock);
808 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
809 error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
812 fatal(0, "spa_create(whatever) = %d", error);
814 error = spa_open(za->za_pool, &spa, FTAG);
816 fatal(0, "spa_open() = %d", error);
818 error = spa_destroy(za->za_pool);
820 fatal(0, "spa_destroy() = %d", error);
822 spa_close(spa, FTAG);
823 (void) rw_unlock(&ztest_shared->zs_name_lock);
827 vdev_lookup_by_path(vdev_t *vd, const char *path)
831 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
834 for (int c = 0; c < vd->vdev_children; c++)
835 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
843 * Verify that vdev_add() works as expected.
846 ztest_vdev_add_remove(ztest_args_t *za)
848 spa_t *spa = za->za_spa;
849 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
853 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
855 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
857 ztest_shared->zs_vdev_primaries =
858 spa->spa_root_vdev->vdev_children * leaves;
860 spa_config_exit(spa, SCL_VDEV, FTAG);
863 * Make 1/4 of the devices be log devices.
865 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
866 ztest_random(4) == 0, zopt_raidz, zopt_mirrors, 1);
868 error = spa_vdev_add(spa, nvroot);
871 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
874 ztest_record_enospc("spa_vdev_add");
876 fatal(0, "spa_vdev_add() = %d", error);
880 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
883 ztest_vdev_aux_add_remove(ztest_args_t *za)
885 spa_t *spa = za->za_spa;
886 vdev_t *rvd = spa->spa_root_vdev;
892 if (ztest_random(2) == 0) {
893 sav = &spa->spa_spares;
894 aux = ZPOOL_CONFIG_SPARES;
896 sav = &spa->spa_l2cache;
897 aux = ZPOOL_CONFIG_L2CACHE;
900 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
902 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
904 if (sav->sav_count != 0 && ztest_random(4) == 0) {
906 * Pick a random device to remove.
908 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
911 * Find an unused device we can add.
913 ztest_shared->zs_vdev_aux = 0;
915 char path[MAXPATHLEN];
917 (void) sprintf(path, ztest_aux_template, zopt_dir,
918 zopt_pool, aux, ztest_shared->zs_vdev_aux);
919 for (c = 0; c < sav->sav_count; c++)
920 if (strcmp(sav->sav_vdevs[c]->vdev_path,
923 if (c == sav->sav_count &&
924 vdev_lookup_by_path(rvd, path) == NULL)
926 ztest_shared->zs_vdev_aux++;
930 spa_config_exit(spa, SCL_VDEV, FTAG);
936 nvlist_t *nvroot = make_vdev_root(NULL, aux,
937 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
938 error = spa_vdev_add(spa, nvroot);
940 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
944 * Remove an existing device. Sometimes, dirty its
945 * vdev state first to make sure we handle removal
946 * of devices that have pending state changes.
948 if (ztest_random(2) == 0)
949 (void) vdev_online(spa, guid, B_FALSE, NULL);
951 error = spa_vdev_remove(spa, guid, B_FALSE);
952 if (error != 0 && error != EBUSY)
953 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
956 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
960 * Verify that we can attach and detach devices.
963 ztest_vdev_attach_detach(ztest_args_t *za)
965 spa_t *spa = za->za_spa;
966 spa_aux_vdev_t *sav = &spa->spa_spares;
967 vdev_t *rvd = spa->spa_root_vdev;
968 vdev_t *oldvd, *newvd, *pvd;
970 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
972 uint64_t ashift = ztest_get_ashift();
973 uint64_t oldguid, pguid;
974 size_t oldsize, newsize;
975 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
977 int oldvd_has_siblings = B_FALSE;
978 int newvd_is_spare = B_FALSE;
980 int error, expected_error;
982 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
984 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
987 * Decide whether to do an attach or a replace.
989 replacing = ztest_random(2);
992 * Pick a random top-level vdev.
994 top = ztest_random(rvd->vdev_children);
997 * Pick a random leaf within it.
999 leaf = ztest_random(leaves);
1004 oldvd = rvd->vdev_child[top];
1005 if (zopt_mirrors >= 1) {
1006 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
1007 ASSERT(oldvd->vdev_children >= zopt_mirrors);
1008 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
1010 if (zopt_raidz > 1) {
1011 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
1012 ASSERT(oldvd->vdev_children == zopt_raidz);
1013 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
1017 * If we're already doing an attach or replace, oldvd may be a
1018 * mirror vdev -- in which case, pick a random child.
1020 while (oldvd->vdev_children != 0) {
1021 oldvd_has_siblings = B_TRUE;
1022 ASSERT(oldvd->vdev_children >= 2);
1023 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
1026 oldguid = oldvd->vdev_guid;
1027 oldsize = vdev_get_rsize(oldvd);
1028 oldvd_is_log = oldvd->vdev_top->vdev_islog;
1029 (void) strcpy(oldpath, oldvd->vdev_path);
1030 pvd = oldvd->vdev_parent;
1031 pguid = pvd->vdev_guid;
1034 * If oldvd has siblings, then half of the time, detach it.
1036 if (oldvd_has_siblings && ztest_random(2) == 0) {
1037 spa_config_exit(spa, SCL_VDEV, FTAG);
1038 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
1039 if (error != 0 && error != ENODEV && error != EBUSY &&
1041 fatal(0, "detach (%s) returned %d", oldpath, error);
1042 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1047 * For the new vdev, choose with equal probability between the two
1048 * standard paths (ending in either 'a' or 'b') or a random hot spare.
1050 if (sav->sav_count != 0 && ztest_random(3) == 0) {
1051 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
1052 newvd_is_spare = B_TRUE;
1053 (void) strcpy(newpath, newvd->vdev_path);
1055 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
1056 zopt_dir, zopt_pool, top * leaves + leaf);
1057 if (ztest_random(2) == 0)
1058 newpath[strlen(newpath) - 1] = 'b';
1059 newvd = vdev_lookup_by_path(rvd, newpath);
1063 newsize = vdev_get_rsize(newvd);
1066 * Make newsize a little bigger or smaller than oldsize.
1067 * If it's smaller, the attach should fail.
1068 * If it's larger, and we're doing a replace,
1069 * we should get dynamic LUN growth when we're done.
1071 newsize = 10 * oldsize / (9 + ztest_random(3));
1075 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
1076 * unless it's a replace; in that case any non-replacing parent is OK.
1078 * If newvd is already part of the pool, it should fail with EBUSY.
1080 * If newvd is too small, it should fail with EOVERFLOW.
1082 if (pvd->vdev_ops != &vdev_mirror_ops &&
1083 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
1084 pvd->vdev_ops == &vdev_replacing_ops ||
1085 pvd->vdev_ops == &vdev_spare_ops))
1086 expected_error = ENOTSUP;
1087 else if (newvd_is_spare && (!replacing || oldvd_is_log))
1088 expected_error = ENOTSUP;
1089 else if (newvd == oldvd)
1090 expected_error = replacing ? 0 : EBUSY;
1091 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
1092 expected_error = EBUSY;
1093 else if (newsize < oldsize)
1094 expected_error = EOVERFLOW;
1095 else if (ashift > oldvd->vdev_top->vdev_ashift)
1096 expected_error = EDOM;
1100 spa_config_exit(spa, SCL_VDEV, FTAG);
1103 * Build the nvlist describing newpath.
1105 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
1106 ashift, 0, 0, 0, 1);
1108 error = spa_vdev_attach(spa, oldguid, root, replacing);
1113 * If our parent was the replacing vdev, but the replace completed,
1114 * then instead of failing with ENOTSUP we may either succeed,
1115 * fail with ENODEV, or fail with EOVERFLOW.
1117 if (expected_error == ENOTSUP &&
1118 (error == 0 || error == ENODEV || error == EOVERFLOW))
1119 expected_error = error;
1122 * If someone grew the LUN, the replacement may be too small.
1124 if (error == EOVERFLOW || error == EBUSY)
1125 expected_error = error;
1127 /* XXX workaround 6690467 */
1128 if (error != expected_error && expected_error != EBUSY) {
1129 fatal(0, "attach (%s %llu, %s %llu, %d) "
1130 "returned %d, expected %d",
1131 oldpath, (longlong_t)oldsize, newpath,
1132 (longlong_t)newsize, replacing, error, expected_error);
1135 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1139 * Verify that dynamic LUN growth works as expected.
1142 ztest_vdev_LUN_growth(ztest_args_t *za)
1144 spa_t *spa = za->za_spa;
1145 char dev_name[MAXPATHLEN];
1146 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
1151 (void) mutex_lock(&ztest_shared->zs_vdev_lock);
1154 * Pick a random leaf vdev.
1156 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
1157 vdev = ztest_random(spa->spa_root_vdev->vdev_children * leaves);
1158 spa_config_exit(spa, SCL_VDEV, FTAG);
1160 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
1162 if ((fd = open(dev_name, O_RDWR)) != -1) {
1164 * Determine the size.
1166 fsize = lseek(fd, 0, SEEK_END);
1169 * If it's less than 2x the original size, grow by around 3%.
1171 if (fsize < 2 * zopt_vdev_size) {
1172 size_t newsize = fsize + ztest_random(fsize / 32);
1173 (void) ftruncate(fd, newsize);
1174 if (zopt_verbose >= 6) {
1175 (void) printf("%s grew from %lu to %lu bytes\n",
1176 dev_name, (ulong_t)fsize, (ulong_t)newsize);
1182 (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
1187 ztest_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1190 * Create the directory object.
1192 VERIFY(dmu_object_claim(os, ZTEST_DIROBJ,
1193 DMU_OT_UINT64_OTHER, ZTEST_DIROBJ_BLOCKSIZE,
1194 DMU_OT_UINT64_OTHER, 5 * sizeof (ztest_block_tag_t), tx) == 0);
1196 VERIFY(zap_create_claim(os, ZTEST_MICROZAP_OBJ,
1197 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1199 VERIFY(zap_create_claim(os, ZTEST_FATZAP_OBJ,
1200 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
1204 ztest_destroy_cb(char *name, void *arg)
1206 ztest_args_t *za = arg;
1208 dmu_object_info_t *doi = &za->za_doi;
1212 * Verify that the dataset contains a directory object.
1214 error = dmu_objset_open(name, DMU_OST_OTHER,
1215 DS_MODE_USER | DS_MODE_READONLY, &os);
1216 ASSERT3U(error, ==, 0);
1217 error = dmu_object_info(os, ZTEST_DIROBJ, doi);
1218 if (error != ENOENT) {
1219 /* We could have crashed in the middle of destroying it */
1220 ASSERT3U(error, ==, 0);
1221 ASSERT3U(doi->doi_type, ==, DMU_OT_UINT64_OTHER);
1222 ASSERT3S(doi->doi_physical_blks, >=, 0);
1224 dmu_objset_close(os);
1227 * Destroy the dataset.
1229 error = dmu_objset_destroy(name);
1231 (void) dmu_objset_open(name, DMU_OST_OTHER,
1232 DS_MODE_USER | DS_MODE_READONLY, &os);
1233 fatal(0, "dmu_objset_destroy(os=%p) = %d\n", &os, error);
1239 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
1242 ztest_log_create(zilog_t *zilog, dmu_tx_t *tx, uint64_t object, int mode)
1249 (void) sprintf(name, "ZOBJ_%llu", (u_longlong_t)object);
1250 namesize = strlen(name) + 1;
1252 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize +
1253 ztest_random(ZIL_MAX_BLKSZ));
1254 lr = (lr_create_t *)&itx->itx_lr;
1255 bzero(lr + 1, lr->lr_common.lrc_reclen - sizeof (*lr));
1256 lr->lr_doid = object;
1261 lr->lr_gen = dmu_tx_get_txg(tx);
1262 lr->lr_crtime[0] = time(NULL);
1263 lr->lr_crtime[1] = 0;
1265 bcopy(name, (char *)(lr + 1), namesize);
1267 return (zil_itx_assign(zilog, itx, tx));
1271 ztest_dmu_objset_create_destroy(ztest_args_t *za)
1276 int basemode, expected_error;
1281 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1282 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
1283 (u_longlong_t)za->za_instance);
1285 basemode = DS_MODE_TYPE(za->za_instance);
1286 if (basemode != DS_MODE_USER && basemode != DS_MODE_OWNER)
1287 basemode = DS_MODE_USER;
1290 * If this dataset exists from a previous run, process its replay log
1291 * half of the time. If we don't replay it, then dmu_objset_destroy()
1292 * (invoked from ztest_destroy_cb() below) should just throw it away.
1294 if (ztest_random(2) == 0 &&
1295 dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os) == 0) {
1296 zil_replay(os, os, ztest_replay_vector);
1297 dmu_objset_close(os);
1301 * There may be an old instance of the dataset we're about to
1302 * create lying around from a previous run. If so, destroy it
1303 * and all of its snapshots.
1305 (void) dmu_objset_find(name, ztest_destroy_cb, za,
1306 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1309 * Verify that the destroyed dataset is no longer in the namespace.
1311 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1312 if (error != ENOENT)
1313 fatal(1, "dmu_objset_open(%s) found destroyed dataset %p",
1317 * Verify that we can create a new dataset.
1319 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
1320 ztest_create_cb, NULL);
1322 if (error == ENOSPC) {
1323 ztest_record_enospc("dmu_objset_create");
1324 (void) rw_unlock(&ztest_shared->zs_name_lock);
1327 fatal(0, "dmu_objset_create(%s) = %d", name, error);
1330 error = dmu_objset_open(name, DMU_OST_OTHER, basemode, &os);
1332 fatal(0, "dmu_objset_open(%s) = %d", name, error);
1336 * Open the intent log for it.
1338 zilog = zil_open(os, NULL);
1341 * Put a random number of objects in there.
1343 objects = ztest_random(20);
1345 while (objects-- != 0) {
1347 dmu_tx_t *tx = dmu_tx_create(os);
1348 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, sizeof (name));
1349 error = dmu_tx_assign(tx, TXG_WAIT);
1353 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1354 DMU_OT_NONE, 0, tx);
1355 ztest_set_random_blocksize(os, object, tx);
1356 seq = ztest_log_create(zilog, tx, object,
1357 DMU_OT_UINT64_OTHER);
1358 dmu_write(os, object, 0, sizeof (name), name, tx);
1361 if (ztest_random(5) == 0) {
1362 zil_commit(zilog, seq, object);
1364 if (ztest_random(100) == 0) {
1365 error = zil_suspend(zilog);
1373 * Verify that we cannot create an existing dataset.
1375 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0, NULL, NULL);
1376 if (error != EEXIST)
1377 fatal(0, "created existing dataset, error = %d", error);
1380 * Verify that multiple dataset holds are allowed, but only when
1381 * the new access mode is compatible with the base mode.
1383 if (basemode == DS_MODE_OWNER) {
1384 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_USER,
1387 fatal(0, "dmu_objset_open('%s') = %d", name, error);
1389 dmu_objset_close(os2);
1391 error = dmu_objset_open(name, DMU_OST_OTHER, DS_MODE_OWNER, &os2);
1392 expected_error = (basemode == DS_MODE_OWNER) ? EBUSY : 0;
1393 if (error != expected_error)
1394 fatal(0, "dmu_objset_open('%s') = %d, expected %d",
1395 name, error, expected_error);
1397 dmu_objset_close(os2);
1400 dmu_objset_close(os);
1402 error = dmu_objset_destroy(name);
1404 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
1406 (void) rw_unlock(&ztest_shared->zs_name_lock);
1410 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
1413 ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
1416 objset_t *os = za->za_os;
1418 char osname[MAXNAMELEN];
1420 (void) rw_rdlock(&ztest_shared->zs_name_lock);
1421 dmu_objset_name(os, osname);
1422 (void) snprintf(snapname, 100, "%s@%llu", osname,
1423 (u_longlong_t)za->za_instance);
1425 error = dmu_objset_destroy(snapname);
1426 if (error != 0 && error != ENOENT)
1427 fatal(0, "dmu_objset_destroy() = %d", error);
1428 error = dmu_objset_snapshot(osname, strchr(snapname, '@')+1, FALSE);
1429 if (error == ENOSPC)
1430 ztest_record_enospc("dmu_take_snapshot");
1431 else if (error != 0 && error != EEXIST)
1432 fatal(0, "dmu_take_snapshot() = %d", error);
1433 (void) rw_unlock(&ztest_shared->zs_name_lock);
1437 * Verify that dmu_object_{alloc,free} work as expected.
1440 ztest_dmu_object_alloc_free(ztest_args_t *za)
1442 objset_t *os = za->za_os;
1445 uint64_t batchobj, object, batchsize, endoff, temp;
1446 int b, c, error, bonuslen;
1447 dmu_object_info_t *doi = &za->za_doi;
1448 char osname[MAXNAMELEN];
1450 dmu_objset_name(os, osname);
1456 * Create a batch object if necessary, and record it in the directory.
1458 VERIFY3U(0, ==, dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1459 sizeof (uint64_t), &batchobj));
1460 if (batchobj == 0) {
1461 tx = dmu_tx_create(os);
1462 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
1464 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1465 error = dmu_tx_assign(tx, TXG_WAIT);
1467 ztest_record_enospc("create a batch object");
1471 batchobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1472 DMU_OT_NONE, 0, tx);
1473 ztest_set_random_blocksize(os, batchobj, tx);
1474 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
1475 sizeof (uint64_t), &batchobj, tx);
1480 * Destroy the previous batch of objects.
1482 for (b = 0; b < batchsize; b++) {
1483 VERIFY3U(0, ==, dmu_read(os, batchobj, b * sizeof (uint64_t),
1484 sizeof (uint64_t), &object));
1488 * Read and validate contents.
1489 * We expect the nth byte of the bonus buffer to be n.
1491 VERIFY(0 == dmu_bonus_hold(os, object, FTAG, &db));
1494 dmu_object_info_from_db(db, doi);
1495 ASSERT(doi->doi_type == DMU_OT_UINT64_OTHER);
1496 ASSERT(doi->doi_bonus_type == DMU_OT_PLAIN_OTHER);
1497 ASSERT3S(doi->doi_physical_blks, >=, 0);
1499 bonuslen = doi->doi_bonus_size;
1501 for (c = 0; c < bonuslen; c++) {
1502 if (((uint8_t *)db->db_data)[c] !=
1503 (uint8_t)(c + bonuslen)) {
1505 "bad bonus: %s, obj %llu, off %d: %u != %u",
1507 ((uint8_t *)db->db_data)[c],
1508 (uint8_t)(c + bonuslen));
1512 dmu_buf_rele(db, FTAG);
1516 * We expect the word at endoff to be our object number.
1518 VERIFY(0 == dmu_read(os, object, endoff,
1519 sizeof (uint64_t), &temp));
1521 if (temp != object) {
1522 fatal(0, "bad data in %s, got %llu, expected %llu",
1523 osname, temp, object);
1527 * Destroy old object and clear batch entry.
1529 tx = dmu_tx_create(os);
1530 dmu_tx_hold_write(tx, batchobj,
1531 b * sizeof (uint64_t), sizeof (uint64_t));
1532 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1533 error = dmu_tx_assign(tx, TXG_WAIT);
1535 ztest_record_enospc("free object");
1539 error = dmu_object_free(os, object, tx);
1541 fatal(0, "dmu_object_free('%s', %llu) = %d",
1542 osname, object, error);
1546 dmu_object_set_checksum(os, batchobj,
1547 ztest_random_checksum(), tx);
1548 dmu_object_set_compress(os, batchobj,
1549 ztest_random_compress(), tx);
1551 dmu_write(os, batchobj, b * sizeof (uint64_t),
1552 sizeof (uint64_t), &object, tx);
1558 * Before creating the new batch of objects, generate a bunch of churn.
1560 for (b = ztest_random(100); b > 0; b--) {
1561 tx = dmu_tx_create(os);
1562 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1563 error = dmu_tx_assign(tx, TXG_WAIT);
1565 ztest_record_enospc("churn objects");
1569 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1570 DMU_OT_NONE, 0, tx);
1571 ztest_set_random_blocksize(os, object, tx);
1572 error = dmu_object_free(os, object, tx);
1574 fatal(0, "dmu_object_free('%s', %llu) = %d",
1575 osname, object, error);
1581 * Create a new batch of objects with randomly chosen
1582 * blocksizes and record them in the batch directory.
1584 for (b = 0; b < batchsize; b++) {
1585 uint32_t va_blksize;
1586 u_longlong_t va_nblocks;
1588 tx = dmu_tx_create(os);
1589 dmu_tx_hold_write(tx, batchobj, b * sizeof (uint64_t),
1591 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1592 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, endoff,
1594 error = dmu_tx_assign(tx, TXG_WAIT);
1596 ztest_record_enospc("create batchobj");
1600 bonuslen = (int)ztest_random(dmu_bonus_max()) + 1;
1602 object = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1603 DMU_OT_PLAIN_OTHER, bonuslen, tx);
1605 ztest_set_random_blocksize(os, object, tx);
1607 dmu_object_set_checksum(os, object,
1608 ztest_random_checksum(), tx);
1609 dmu_object_set_compress(os, object,
1610 ztest_random_compress(), tx);
1612 dmu_write(os, batchobj, b * sizeof (uint64_t),
1613 sizeof (uint64_t), &object, tx);
1616 * Write to both the bonus buffer and the regular data.
1618 VERIFY(dmu_bonus_hold(os, object, FTAG, &db) == 0);
1620 ASSERT3U(bonuslen, <=, db->db_size);
1622 dmu_object_size_from_db(db, &va_blksize, &va_nblocks);
1623 ASSERT3S(va_nblocks, >=, 0);
1625 dmu_buf_will_dirty(db, tx);
1628 * See comments above regarding the contents of
1629 * the bonus buffer and the word at endoff.
1631 for (c = 0; c < bonuslen; c++)
1632 ((uint8_t *)db->db_data)[c] = (uint8_t)(c + bonuslen);
1634 dmu_buf_rele(db, FTAG);
1638 * Write to a large offset to increase indirection.
1640 dmu_write(os, object, endoff, sizeof (uint64_t), &object, tx);
1647 * Verify that dmu_{read,write} work as expected.
1649 typedef struct bufwad {
1655 typedef struct dmu_read_write_dir {
1656 uint64_t dd_packobj;
1659 } dmu_read_write_dir_t;
1662 ztest_dmu_read_write(ztest_args_t *za)
1664 objset_t *os = za->za_os;
1665 dmu_read_write_dir_t dd;
1667 int i, freeit, error;
1669 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
1670 uint64_t packoff, packsize, bigoff, bigsize;
1671 uint64_t regions = 997;
1672 uint64_t stride = 123456789ULL;
1673 uint64_t width = 40;
1674 int free_percent = 5;
1677 * This test uses two objects, packobj and bigobj, that are always
1678 * updated together (i.e. in the same tx) so that their contents are
1679 * in sync and can be compared. Their contents relate to each other
1680 * in a simple way: packobj is a dense array of 'bufwad' structures,
1681 * while bigobj is a sparse array of the same bufwads. Specifically,
1682 * for any index n, there are three bufwads that should be identical:
1684 * packobj, at offset n * sizeof (bufwad_t)
1685 * bigobj, at the head of the nth chunk
1686 * bigobj, at the tail of the nth chunk
1688 * The chunk size is arbitrary. It doesn't have to be a power of two,
1689 * and it doesn't have any relation to the object blocksize.
1690 * The only requirement is that it can hold at least two bufwads.
1692 * Normally, we write the bufwad to each of these locations.
1693 * However, free_percent of the time we instead write zeroes to
1694 * packobj and perform a dmu_free_range() on bigobj. By comparing
1695 * bigobj to packobj, we can verify that the DMU is correctly
1696 * tracking which parts of an object are allocated and free,
1697 * and that the contents of the allocated blocks are correct.
1701 * Read the directory info. If it's the first time, set things up.
1703 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
1705 if (dd.dd_chunk == 0) {
1706 ASSERT(dd.dd_packobj == 0);
1707 ASSERT(dd.dd_bigobj == 0);
1708 tx = dmu_tx_create(os);
1709 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (dd));
1710 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1711 error = dmu_tx_assign(tx, TXG_WAIT);
1713 ztest_record_enospc("create r/w directory");
1718 dd.dd_packobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1719 DMU_OT_NONE, 0, tx);
1720 dd.dd_bigobj = dmu_object_alloc(os, DMU_OT_UINT64_OTHER, 0,
1721 DMU_OT_NONE, 0, tx);
1722 dd.dd_chunk = (1000 + ztest_random(1000)) * sizeof (uint64_t);
1724 ztest_set_random_blocksize(os, dd.dd_packobj, tx);
1725 ztest_set_random_blocksize(os, dd.dd_bigobj, tx);
1727 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (dd), &dd,
1733 * Prefetch a random chunk of the big object.
1734 * Our aim here is to get some async reads in flight
1735 * for blocks that we may free below; the DMU should
1736 * handle this race correctly.
1738 n = ztest_random(regions) * stride + ztest_random(width);
1739 s = 1 + ztest_random(2 * width - 1);
1740 dmu_prefetch(os, dd.dd_bigobj, n * dd.dd_chunk, s * dd.dd_chunk);
1743 * Pick a random index and compute the offsets into packobj and bigobj.
1745 n = ztest_random(regions) * stride + ztest_random(width);
1746 s = 1 + ztest_random(width - 1);
1748 packoff = n * sizeof (bufwad_t);
1749 packsize = s * sizeof (bufwad_t);
1751 bigoff = n * dd.dd_chunk;
1752 bigsize = s * dd.dd_chunk;
1754 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
1755 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
1758 * free_percent of the time, free a range of bigobj rather than
1761 freeit = (ztest_random(100) < free_percent);
1764 * Read the current contents of our objects.
1766 error = dmu_read(os, dd.dd_packobj, packoff, packsize, packbuf);
1767 ASSERT3U(error, ==, 0);
1768 error = dmu_read(os, dd.dd_bigobj, bigoff, bigsize, bigbuf);
1769 ASSERT3U(error, ==, 0);
1772 * Get a tx for the mods to both packobj and bigobj.
1774 tx = dmu_tx_create(os);
1776 dmu_tx_hold_write(tx, dd.dd_packobj, packoff, packsize);
1779 dmu_tx_hold_free(tx, dd.dd_bigobj, bigoff, bigsize);
1781 dmu_tx_hold_write(tx, dd.dd_bigobj, bigoff, bigsize);
1783 error = dmu_tx_assign(tx, TXG_WAIT);
1786 ztest_record_enospc("dmu r/w range");
1788 umem_free(packbuf, packsize);
1789 umem_free(bigbuf, bigsize);
1793 txg = dmu_tx_get_txg(tx);
1796 * For each index from n to n + s, verify that the existing bufwad
1797 * in packobj matches the bufwads at the head and tail of the
1798 * corresponding chunk in bigobj. Then update all three bufwads
1799 * with the new values we want to write out.
1801 for (i = 0; i < s; i++) {
1803 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
1805 bigH = (bufwad_t *)((char *)bigbuf + i * dd.dd_chunk);
1807 bigT = (bufwad_t *)((char *)bigH + dd.dd_chunk) - 1;
1809 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
1810 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
1812 if (pack->bw_txg > txg)
1813 fatal(0, "future leak: got %llx, open txg is %llx",
1816 if (pack->bw_data != 0 && pack->bw_index != n + i)
1817 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
1818 pack->bw_index, n, i);
1820 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
1821 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
1823 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
1824 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
1827 bzero(pack, sizeof (bufwad_t));
1829 pack->bw_index = n + i;
1831 pack->bw_data = 1 + ztest_random(-2ULL);
1838 * We've verified all the old bufwads, and made new ones.
1839 * Now write them out.
1841 dmu_write(os, dd.dd_packobj, packoff, packsize, packbuf, tx);
1844 if (zopt_verbose >= 6) {
1845 (void) printf("freeing offset %llx size %llx"
1847 (u_longlong_t)bigoff,
1848 (u_longlong_t)bigsize,
1851 VERIFY(0 == dmu_free_range(os, dd.dd_bigobj, bigoff,
1854 if (zopt_verbose >= 6) {
1855 (void) printf("writing offset %llx size %llx"
1857 (u_longlong_t)bigoff,
1858 (u_longlong_t)bigsize,
1861 dmu_write(os, dd.dd_bigobj, bigoff, bigsize, bigbuf, tx);
1867 * Sanity check the stuff we just wrote.
1870 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
1871 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
1873 VERIFY(0 == dmu_read(os, dd.dd_packobj, packoff,
1874 packsize, packcheck));
1875 VERIFY(0 == dmu_read(os, dd.dd_bigobj, bigoff,
1876 bigsize, bigcheck));
1878 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
1879 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
1881 umem_free(packcheck, packsize);
1882 umem_free(bigcheck, bigsize);
1885 umem_free(packbuf, packsize);
1886 umem_free(bigbuf, bigsize);
1890 ztest_dmu_check_future_leak(ztest_args_t *za)
1892 objset_t *os = za->za_os;
1894 ztest_block_tag_t *bt;
1895 dmu_object_info_t *doi = &za->za_doi;
1898 * Make sure that, if there is a write record in the bonus buffer
1899 * of the ZTEST_DIROBJ, that the txg for this record is <= the
1900 * last synced txg of the pool.
1902 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
1904 VERIFY(dmu_object_info(os, ZTEST_DIROBJ, doi) == 0);
1905 ASSERT3U(doi->doi_bonus_size, >=, sizeof (*bt));
1906 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
1907 ASSERT3U(doi->doi_bonus_size % sizeof (*bt), ==, 0);
1908 bt = (void *)((char *)db->db_data + doi->doi_bonus_size - sizeof (*bt));
1909 if (bt->bt_objset != 0) {
1910 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1911 ASSERT3U(bt->bt_object, ==, ZTEST_DIROBJ);
1912 ASSERT3U(bt->bt_offset, ==, -1ULL);
1913 ASSERT3U(bt->bt_txg, <, spa_first_txg(za->za_spa));
1915 dmu_buf_rele(db, FTAG);
1920 ztest_dmu_write_parallel(ztest_args_t *za)
1922 objset_t *os = za->za_os;
1923 ztest_block_tag_t *rbt = &za->za_rbt;
1924 ztest_block_tag_t *wbt = &za->za_wbt;
1925 const size_t btsize = sizeof (ztest_block_tag_t);
1928 int bs = ZTEST_DIROBJ_BLOCKSIZE;
1930 uint64_t off, txg, txg_how;
1932 char osname[MAXNAMELEN];
1933 char iobuf[SPA_MAXBLOCKSIZE];
1934 blkptr_t blk = { 0 };
1937 dmu_tx_t *tx = dmu_tx_create(os);
1939 dmu_objset_name(os, osname);
1942 * Have multiple threads write to large offsets in ZTEST_DIROBJ
1943 * to verify that having multiple threads writing to the same object
1944 * in parallel doesn't cause any trouble.
1946 if (ztest_random(4) == 0) {
1948 * Do the bonus buffer instead of a regular block.
1949 * We need a lock to serialize resize vs. others,
1950 * so we hash on the objset ID.
1952 b = dmu_objset_id(os) % ZTEST_SYNC_LOCKS;
1954 dmu_tx_hold_bonus(tx, ZTEST_DIROBJ);
1956 b = ztest_random(ZTEST_SYNC_LOCKS);
1957 off = za->za_diroff_shared + (b << SPA_MAXBLOCKSHIFT);
1958 if (ztest_random(4) == 0) {
1960 dmu_tx_hold_free(tx, ZTEST_DIROBJ, off, bs);
1962 dmu_tx_hold_write(tx, ZTEST_DIROBJ, off, bs);
1966 txg_how = ztest_random(2) == 0 ? TXG_WAIT : TXG_NOWAIT;
1967 error = dmu_tx_assign(tx, txg_how);
1969 if (error == ERESTART) {
1970 ASSERT(txg_how == TXG_NOWAIT);
1973 ztest_record_enospc("dmu write parallel");
1978 txg = dmu_tx_get_txg(tx);
1980 lp = &ztest_shared->zs_sync_lock[b];
1981 (void) mutex_lock(lp);
1983 wbt->bt_objset = dmu_objset_id(os);
1984 wbt->bt_object = ZTEST_DIROBJ;
1985 wbt->bt_offset = off;
1987 wbt->bt_thread = za->za_instance;
1988 wbt->bt_seq = ztest_shared->zs_seq[b]++; /* protected by lp */
1991 * Occasionally, write an all-zero block to test the behavior
1992 * of blocks that compress into holes.
1994 if (off != -1ULL && ztest_random(8) == 0)
1998 dmu_object_info_t *doi = &za->za_doi;
2001 VERIFY(dmu_bonus_hold(os, ZTEST_DIROBJ, FTAG, &db) == 0);
2003 dmu_object_info_from_db(db, doi);
2004 ASSERT3U(doi->doi_bonus_size, <=, db->db_size);
2005 ASSERT3U(doi->doi_bonus_size, >=, btsize);
2006 ASSERT3U(doi->doi_bonus_size % btsize, ==, 0);
2007 dboff = (char *)db->db_data + doi->doi_bonus_size - btsize;
2008 bcopy(dboff, rbt, btsize);
2009 if (rbt->bt_objset != 0) {
2010 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2011 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2012 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2013 ASSERT3U(rbt->bt_txg, <=, wbt->bt_txg);
2015 if (ztest_random(10) == 0) {
2016 int newsize = (ztest_random(db->db_size /
2017 btsize) + 1) * btsize;
2019 ASSERT3U(newsize, >=, btsize);
2020 ASSERT3U(newsize, <=, db->db_size);
2021 VERIFY3U(dmu_set_bonus(db, newsize, tx), ==, 0);
2022 dboff = (char *)db->db_data + newsize - btsize;
2024 dmu_buf_will_dirty(db, tx);
2025 bcopy(wbt, dboff, btsize);
2026 dmu_buf_rele(db, FTAG);
2028 } else if (do_free) {
2029 VERIFY(dmu_free_range(os, ZTEST_DIROBJ, off, bs, tx) == 0);
2031 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
2034 (void) mutex_unlock(lp);
2036 if (ztest_random(1000) == 0)
2037 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
2041 if (ztest_random(10000) == 0)
2042 txg_wait_synced(dmu_objset_pool(os), txg);
2044 if (off == -1ULL || do_free)
2047 if (ztest_random(2) != 0)
2051 * dmu_sync() the block we just wrote.
2053 (void) mutex_lock(lp);
2055 blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
2056 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
2059 (void) mutex_unlock(lp);
2062 blkoff = off - blkoff;
2063 error = dmu_sync(NULL, db, &blk, txg, NULL, NULL);
2064 dmu_buf_rele(db, FTAG);
2067 (void) mutex_unlock(lp);
2072 if (blk.blk_birth == 0) /* concurrent free */
2075 txg_suspend(dmu_objset_pool(os));
2077 ASSERT(blk.blk_fill == 1);
2078 ASSERT3U(BP_GET_TYPE(&blk), ==, DMU_OT_UINT64_OTHER);
2079 ASSERT3U(BP_GET_LEVEL(&blk), ==, 0);
2080 ASSERT3U(BP_GET_LSIZE(&blk), ==, bs);
2083 * Read the block that dmu_sync() returned to make sure its contents
2084 * match what we wrote. We do this while still txg_suspend()ed
2085 * to ensure that the block can't be reused before we read it.
2087 zb.zb_objset = dmu_objset_id(os);
2088 zb.zb_object = ZTEST_DIROBJ;
2090 zb.zb_blkid = off / bs;
2091 error = zio_wait(zio_read(NULL, za->za_spa, &blk, iobuf, bs,
2092 NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_MUSTSUCCEED, &zb));
2093 ASSERT3U(error, ==, 0);
2095 txg_resume(dmu_objset_pool(os));
2097 bcopy(&iobuf[blkoff], rbt, btsize);
2099 if (rbt->bt_objset == 0) /* concurrent free */
2102 if (wbt->bt_objset == 0) /* all-zero overwrite */
2105 ASSERT3U(rbt->bt_objset, ==, wbt->bt_objset);
2106 ASSERT3U(rbt->bt_object, ==, wbt->bt_object);
2107 ASSERT3U(rbt->bt_offset, ==, wbt->bt_offset);
2110 * The semantic of dmu_sync() is that we always push the most recent
2111 * version of the data, so in the face of concurrent updates we may
2112 * see a newer version of the block. That's OK.
2114 ASSERT3U(rbt->bt_txg, >=, wbt->bt_txg);
2115 if (rbt->bt_thread == wbt->bt_thread)
2116 ASSERT3U(rbt->bt_seq, ==, wbt->bt_seq);
2118 ASSERT3U(rbt->bt_seq, >, wbt->bt_seq);
2122 * Verify that zap_{create,destroy,add,remove,update} work as expected.
2124 #define ZTEST_ZAP_MIN_INTS 1
2125 #define ZTEST_ZAP_MAX_INTS 4
2126 #define ZTEST_ZAP_MAX_PROPS 1000
2129 ztest_zap(ztest_args_t *za)
2131 objset_t *os = za->za_os;
2133 uint64_t txg, last_txg;
2134 uint64_t value[ZTEST_ZAP_MAX_INTS];
2135 uint64_t zl_ints, zl_intsize, prop;
2138 char propname[100], txgname[100];
2140 char osname[MAXNAMELEN];
2141 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
2143 dmu_objset_name(os, osname);
2146 * Create a new object if necessary, and record it in the directory.
2148 VERIFY(0 == dmu_read(os, ZTEST_DIROBJ, za->za_diroff,
2149 sizeof (uint64_t), &object));
2152 tx = dmu_tx_create(os);
2153 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff,
2155 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL);
2156 error = dmu_tx_assign(tx, TXG_WAIT);
2158 ztest_record_enospc("create zap test obj");
2162 object = zap_create(os, DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx);
2164 fatal(0, "zap_create('%s', %llu) = %d",
2165 osname, object, error);
2167 ASSERT(object != 0);
2168 dmu_write(os, ZTEST_DIROBJ, za->za_diroff,
2169 sizeof (uint64_t), &object, tx);
2171 * Generate a known hash collision, and verify that
2172 * we can lookup and remove both entries.
2174 for (i = 0; i < 2; i++) {
2176 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2178 ASSERT3U(error, ==, 0);
2180 for (i = 0; i < 2; i++) {
2181 error = zap_add(os, object, hc[i], sizeof (uint64_t),
2183 ASSERT3U(error, ==, EEXIST);
2184 error = zap_length(os, object, hc[i],
2185 &zl_intsize, &zl_ints);
2186 ASSERT3U(error, ==, 0);
2187 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2188 ASSERT3U(zl_ints, ==, 1);
2190 for (i = 0; i < 2; i++) {
2191 error = zap_remove(os, object, hc[i], tx);
2192 ASSERT3U(error, ==, 0);
2198 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
2200 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2201 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2202 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2203 bzero(value, sizeof (value));
2207 * If these zap entries already exist, validate their contents.
2209 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2211 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2212 ASSERT3U(zl_ints, ==, 1);
2214 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
2215 zl_ints, &last_txg) == 0);
2217 VERIFY(zap_length(os, object, propname, &zl_intsize,
2220 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
2221 ASSERT3U(zl_ints, ==, ints);
2223 VERIFY(zap_lookup(os, object, propname, zl_intsize,
2224 zl_ints, value) == 0);
2226 for (i = 0; i < ints; i++) {
2227 ASSERT3U(value[i], ==, last_txg + object + i);
2230 ASSERT3U(error, ==, ENOENT);
2234 * Atomically update two entries in our zap object.
2235 * The first is named txg_%llu, and contains the txg
2236 * in which the property was last updated. The second
2237 * is named prop_%llu, and the nth element of its value
2238 * should be txg + object + n.
2240 tx = dmu_tx_create(os);
2241 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2242 error = dmu_tx_assign(tx, TXG_WAIT);
2244 ztest_record_enospc("create zap entry");
2248 txg = dmu_tx_get_txg(tx);
2251 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
2253 for (i = 0; i < ints; i++)
2254 value[i] = txg + object + i;
2256 error = zap_update(os, object, txgname, sizeof (uint64_t), 1, &txg, tx);
2258 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2259 osname, object, txgname, error);
2261 error = zap_update(os, object, propname, sizeof (uint64_t),
2264 fatal(0, "zap_update('%s', %llu, '%s') = %d",
2265 osname, object, propname, error);
2270 * Remove a random pair of entries.
2272 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
2273 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
2274 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
2276 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
2278 if (error == ENOENT)
2281 ASSERT3U(error, ==, 0);
2283 tx = dmu_tx_create(os);
2284 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2285 error = dmu_tx_assign(tx, TXG_WAIT);
2287 ztest_record_enospc("remove zap entry");
2291 error = zap_remove(os, object, txgname, tx);
2293 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2294 osname, object, txgname, error);
2296 error = zap_remove(os, object, propname, tx);
2298 fatal(0, "zap_remove('%s', %llu, '%s') = %d",
2299 osname, object, propname, error);
2304 * Once in a while, destroy the object.
2306 if (ztest_random(1000) != 0)
2309 tx = dmu_tx_create(os);
2310 dmu_tx_hold_write(tx, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t));
2311 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
2312 error = dmu_tx_assign(tx, TXG_WAIT);
2314 ztest_record_enospc("destroy zap object");
2318 error = zap_destroy(os, object, tx);
2320 fatal(0, "zap_destroy('%s', %llu) = %d",
2321 osname, object, error);
2323 dmu_write(os, ZTEST_DIROBJ, za->za_diroff, sizeof (uint64_t),
2329 ztest_zap_parallel(ztest_args_t *za)
2331 objset_t *os = za->za_os;
2332 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
2334 int i, namelen, error;
2335 char name[20], string_value[20];
2339 * Generate a random name of the form 'xxx.....' where each
2340 * x is a random printable character and the dots are dots.
2341 * There are 94 such characters, and the name length goes from
2342 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
2344 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
2346 for (i = 0; i < 3; i++)
2347 name[i] = '!' + ztest_random('~' - '!' + 1);
2348 for (; i < namelen - 1; i++)
2352 if (ztest_random(2) == 0)
2353 object = ZTEST_MICROZAP_OBJ;
2355 object = ZTEST_FATZAP_OBJ;
2357 if ((namelen & 1) || object == ZTEST_MICROZAP_OBJ) {
2358 wsize = sizeof (txg);
2364 data = string_value;
2368 VERIFY(zap_count(os, object, &count) == 0);
2369 ASSERT(count != -1ULL);
2372 * Select an operation: length, lookup, add, update, remove.
2374 i = ztest_random(5);
2377 tx = dmu_tx_create(os);
2378 dmu_tx_hold_zap(tx, object, TRUE, NULL);
2379 error = dmu_tx_assign(tx, TXG_WAIT);
2381 ztest_record_enospc("zap parallel");
2385 txg = dmu_tx_get_txg(tx);
2386 bcopy(name, string_value, namelen);
2390 bzero(string_value, namelen);
2396 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
2398 ASSERT3U(wsize, ==, zl_wsize);
2399 ASSERT3U(wc, ==, zl_wc);
2401 ASSERT3U(error, ==, ENOENT);
2406 error = zap_lookup(os, object, name, wsize, wc, data);
2408 if (data == string_value &&
2409 bcmp(name, data, namelen) != 0)
2410 fatal(0, "name '%s' != val '%s' len %d",
2411 name, data, namelen);
2413 ASSERT3U(error, ==, ENOENT);
2418 error = zap_add(os, object, name, wsize, wc, data, tx);
2419 ASSERT(error == 0 || error == EEXIST);
2423 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
2427 error = zap_remove(os, object, name, tx);
2428 ASSERT(error == 0 || error == ENOENT);
2437 ztest_dsl_prop_get_set(ztest_args_t *za)
2439 objset_t *os = za->za_os;
2442 const char *prop, *valname;
2443 char setpoint[MAXPATHLEN];
2444 char osname[MAXNAMELEN];
2447 (void) rw_rdlock(&ztest_shared->zs_name_lock);
2449 dmu_objset_name(os, osname);
2451 for (i = 0; i < 2; i++) {
2454 value = ztest_random_checksum();
2455 inherit = (value == ZIO_CHECKSUM_INHERIT);
2457 prop = "compression";
2458 value = ztest_random_compress();
2459 inherit = (value == ZIO_COMPRESS_INHERIT);
2462 error = dsl_prop_set(osname, prop, sizeof (value),
2465 if (error == ENOSPC) {
2466 ztest_record_enospc("dsl_prop_set");
2470 ASSERT3U(error, ==, 0);
2472 VERIFY3U(dsl_prop_get(osname, prop, sizeof (value),
2473 1, &value, setpoint), ==, 0);
2476 valname = zio_checksum_table[value].ci_name;
2478 valname = zio_compress_table[value].ci_name;
2480 if (zopt_verbose >= 6) {
2481 (void) printf("%s %s = %s for '%s'\n",
2482 osname, prop, valname, setpoint);
2486 (void) rw_unlock(&ztest_shared->zs_name_lock);
2490 * Inject random faults into the on-disk data.
2493 ztest_fault_inject(ztest_args_t *za)
2497 uint64_t leaves = MAX(zopt_mirrors, 1) * zopt_raidz;
2498 uint64_t bad = 0x1990c0ffeedecade;
2500 char path0[MAXPATHLEN];
2501 char pathrand[MAXPATHLEN];
2503 spa_t *spa = za->za_spa;
2504 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
2506 int maxfaults = zopt_maxfaults;
2510 ASSERT(leaves >= 1);
2513 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
2515 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
2517 if (ztest_random(2) == 0) {
2519 * Inject errors on a normal data device.
2521 top = ztest_random(spa->spa_root_vdev->vdev_children);
2522 leaf = ztest_random(leaves);
2525 * Generate paths to the first leaf in this top-level vdev,
2526 * and to the random leaf we selected. We'll induce transient
2527 * write failures and random online/offline activity on leaf 0,
2528 * and we'll write random garbage to the randomly chosen leaf.
2530 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
2531 zopt_dir, zopt_pool, top * leaves + 0);
2532 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
2533 zopt_dir, zopt_pool, top * leaves + leaf);
2535 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
2536 if (vd0 != NULL && maxfaults != 1) {
2538 * Make vd0 explicitly claim to be unreadable,
2539 * or unwriteable, or reach behind its back
2540 * and close the underlying fd. We can do this if
2541 * maxfaults == 0 because we'll fail and reexecute,
2542 * and we can do it if maxfaults >= 2 because we'll
2543 * have enough redundancy. If maxfaults == 1, the
2544 * combination of this with injection of random data
2545 * corruption below exceeds the pool's fault tolerance.
2547 vdev_file_t *vf = vd0->vdev_tsd;
2549 if (vf != NULL && ztest_random(3) == 0) {
2550 (void) close(vf->vf_vnode->v_fd);
2551 vf->vf_vnode->v_fd = -1;
2552 } else if (ztest_random(2) == 0) {
2553 vd0->vdev_cant_read = B_TRUE;
2555 vd0->vdev_cant_write = B_TRUE;
2557 guid0 = vd0->vdev_guid;
2561 * Inject errors on an l2cache device.
2563 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2565 if (sav->sav_count == 0) {
2566 spa_config_exit(spa, SCL_STATE, FTAG);
2569 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
2570 guid0 = vd0->vdev_guid;
2571 (void) strcpy(path0, vd0->vdev_path);
2572 (void) strcpy(pathrand, vd0->vdev_path);
2576 maxfaults = INT_MAX; /* no limit on cache devices */
2579 spa_config_exit(spa, SCL_STATE, FTAG);
2585 * If we can tolerate two or more faults, randomly online/offline vd0.
2587 if (maxfaults >= 2 && guid0 != 0) {
2588 if (ztest_random(10) < 6) {
2589 int flags = (ztest_random(2) == 0 ?
2590 ZFS_OFFLINE_TEMPORARY : 0);
2591 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
2593 (void) vdev_online(spa, guid0, 0, NULL);
2598 * We have at least single-fault tolerance, so inject data corruption.
2600 fd = open(pathrand, O_RDWR);
2602 if (fd == -1) /* we hit a gap in the device namespace */
2605 fsize = lseek(fd, 0, SEEK_END);
2607 while (--iters != 0) {
2608 offset = ztest_random(fsize / (leaves << bshift)) *
2609 (leaves << bshift) + (leaf << bshift) +
2610 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
2612 if (offset >= fsize)
2615 if (zopt_verbose >= 6)
2616 (void) printf("injecting bad word into %s,"
2617 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
2619 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
2620 fatal(1, "can't inject bad word at 0x%llx in %s",
2631 ztest_scrub(ztest_args_t *za)
2633 spa_t *spa = za->za_spa;
2635 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2636 (void) poll(NULL, 0, 1000); /* wait a second, then force a restart */
2637 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2641 * Rename the pool to a different name and then rename it back.
2644 ztest_spa_rename(ztest_args_t *za)
2646 char *oldname, *newname;
2650 (void) rw_wrlock(&ztest_shared->zs_name_lock);
2652 oldname = za->za_pool;
2653 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
2654 (void) strcpy(newname, oldname);
2655 (void) strcat(newname, "_tmp");
2660 error = spa_rename(oldname, newname);
2662 fatal(0, "spa_rename('%s', '%s') = %d", oldname,
2666 * Try to open it under the old name, which shouldn't exist
2668 error = spa_open(oldname, &spa, FTAG);
2669 if (error != ENOENT)
2670 fatal(0, "spa_open('%s') = %d", oldname, error);
2673 * Open it under the new name and make sure it's still the same spa_t.
2675 error = spa_open(newname, &spa, FTAG);
2677 fatal(0, "spa_open('%s') = %d", newname, error);
2679 ASSERT(spa == za->za_spa);
2680 spa_close(spa, FTAG);
2683 * Rename it back to the original
2685 error = spa_rename(newname, oldname);
2687 fatal(0, "spa_rename('%s', '%s') = %d", newname,
2691 * Make sure it can still be opened
2693 error = spa_open(oldname, &spa, FTAG);
2695 fatal(0, "spa_open('%s') = %d", oldname, error);
2697 ASSERT(spa == za->za_spa);
2698 spa_close(spa, FTAG);
2700 umem_free(newname, strlen(newname) + 1);
2702 (void) rw_unlock(&ztest_shared->zs_name_lock);
2707 * Completely obliterate one disk.
2710 ztest_obliterate_one_disk(uint64_t vdev)
2713 char dev_name[MAXPATHLEN], copy_name[MAXPATHLEN];
2716 if (zopt_maxfaults < 2)
2719 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2720 (void) snprintf(copy_name, MAXPATHLEN, "%s.old", dev_name);
2722 fd = open(dev_name, O_RDWR);
2725 fatal(1, "can't open %s", dev_name);
2728 * Determine the size.
2730 fsize = lseek(fd, 0, SEEK_END);
2735 * Rename the old device to dev_name.old (useful for debugging).
2737 VERIFY(rename(dev_name, copy_name) == 0);
2742 VERIFY((fd = open(dev_name, O_RDWR | O_CREAT | O_TRUNC, 0666)) >= 0);
2743 VERIFY(ftruncate(fd, fsize) == 0);
2748 ztest_replace_one_disk(spa_t *spa, uint64_t vdev)
2750 char dev_name[MAXPATHLEN];
2756 (void) sprintf(dev_name, ztest_dev_template, zopt_dir, zopt_pool, vdev);
2759 * Build the nvlist describing dev_name.
2761 root = make_vdev_root(dev_name, NULL, 0, 0, 0, 0, 0, 1);
2763 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2764 if ((vd = vdev_lookup_by_path(spa->spa_root_vdev, dev_name)) == NULL)
2767 guid = vd->vdev_guid;
2768 spa_config_exit(spa, SCL_VDEV, FTAG);
2769 error = spa_vdev_attach(spa, guid, root, B_TRUE);
2775 fatal(0, "spa_vdev_attach(in-place) = %d", error);
2781 ztest_verify_blocks(char *pool)
2784 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
2792 (void) realpath(getexecname(), zdb);
2794 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
2795 bin = strstr(zdb, "/usr/bin/");
2796 ztest = strstr(bin, "/ztest");
2798 isalen = ztest - isa;
2802 "/usr/sbin%.*s/zdb -bc%s%s -U /tmp/zpool.cache %s",
2805 zopt_verbose >= 3 ? "s" : "",
2806 zopt_verbose >= 4 ? "v" : "",
2810 if (zopt_verbose >= 5)
2811 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
2813 fp = popen(zdb, "r");
2815 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
2816 if (zopt_verbose >= 3)
2817 (void) printf("%s", zbuf);
2819 status = pclose(fp);
2824 ztest_dump_core = 0;
2825 if (WIFEXITED(status))
2826 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
2828 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
2832 ztest_walk_pool_directory(char *header)
2836 if (zopt_verbose >= 6)
2837 (void) printf("%s\n", header);
2839 mutex_enter(&spa_namespace_lock);
2840 while ((spa = spa_next(spa)) != NULL)
2841 if (zopt_verbose >= 6)
2842 (void) printf("\t%s\n", spa_name(spa));
2843 mutex_exit(&spa_namespace_lock);
2847 ztest_spa_import_export(char *oldname, char *newname)
2849 nvlist_t *config, *newconfig;
2854 if (zopt_verbose >= 4) {
2855 (void) printf("import/export: old = %s, new = %s\n",
2860 * Clean up from previous runs.
2862 (void) spa_destroy(newname);
2865 * Get the pool's configuration and guid.
2867 error = spa_open(oldname, &spa, FTAG);
2869 fatal(0, "spa_open('%s') = %d", oldname, error);
2872 * Kick off a scrub to tickle scrub/export races.
2874 if (ztest_random(2) == 0)
2875 (void) spa_scrub(spa, POOL_SCRUB_EVERYTHING);
2877 pool_guid = spa_guid(spa);
2878 spa_close(spa, FTAG);
2880 ztest_walk_pool_directory("pools before export");
2885 error = spa_export(oldname, &config, B_FALSE, B_FALSE);
2887 fatal(0, "spa_export('%s') = %d", oldname, error);
2889 ztest_walk_pool_directory("pools after export");
2894 newconfig = spa_tryimport(config);
2895 ASSERT(newconfig != NULL);
2896 nvlist_free(newconfig);
2899 * Import it under the new name.
2901 error = spa_import(newname, config, NULL);
2903 fatal(0, "spa_import('%s') = %d", newname, error);
2905 ztest_walk_pool_directory("pools after import");
2908 * Try to import it again -- should fail with EEXIST.
2910 error = spa_import(newname, config, NULL);
2911 if (error != EEXIST)
2912 fatal(0, "spa_import('%s') twice", newname);
2915 * Try to import it under a different name -- should fail with EEXIST.
2917 error = spa_import(oldname, config, NULL);
2918 if (error != EEXIST)
2919 fatal(0, "spa_import('%s') under multiple names", newname);
2922 * Verify that the pool is no longer visible under the old name.
2924 error = spa_open(oldname, &spa, FTAG);
2925 if (error != ENOENT)
2926 fatal(0, "spa_open('%s') = %d", newname, error);
2929 * Verify that we can open and close the pool using the new name.
2931 error = spa_open(newname, &spa, FTAG);
2933 fatal(0, "spa_open('%s') = %d", newname, error);
2934 ASSERT(pool_guid == spa_guid(spa));
2935 spa_close(spa, FTAG);
2937 nvlist_free(config);
2941 ztest_resume(spa_t *spa)
2943 if (spa_suspended(spa)) {
2944 spa_vdev_state_enter(spa);
2945 vdev_clear(spa, NULL);
2946 (void) spa_vdev_state_exit(spa, NULL, 0);
2952 ztest_resume_thread(void *arg)
2956 while (!ztest_exiting) {
2957 (void) poll(NULL, 0, 1000);
2964 ztest_thread(void *arg)
2966 ztest_args_t *za = arg;
2967 ztest_shared_t *zs = ztest_shared;
2968 hrtime_t now, functime;
2972 while ((now = gethrtime()) < za->za_stop) {
2974 * See if it's time to force a crash.
2976 if (now > za->za_kill) {
2977 zs->zs_alloc = spa_get_alloc(za->za_spa);
2978 zs->zs_space = spa_get_space(za->za_spa);
2979 (void) kill(getpid(), SIGKILL);
2983 * Pick a random function.
2985 f = ztest_random(ZTEST_FUNCS);
2986 zi = &zs->zs_info[f];
2989 * Decide whether to call it, based on the requested frequency.
2991 if (zi->zi_call_target == 0 ||
2992 (double)zi->zi_call_total / zi->zi_call_target >
2993 (double)(now - zs->zs_start_time) / (zopt_time * NANOSEC))
2996 atomic_add_64(&zi->zi_calls, 1);
2997 atomic_add_64(&zi->zi_call_total, 1);
2999 za->za_diroff = (za->za_instance * ZTEST_FUNCS + f) *
3001 za->za_diroff_shared = (1ULL << 63);
3003 for (i = 0; i < zi->zi_iters; i++)
3006 functime = gethrtime() - now;
3008 atomic_add_64(&zi->zi_call_time, functime);
3010 if (zopt_verbose >= 4) {
3012 (void) dladdr((void *)zi->zi_func, &dli);
3013 (void) printf("%6.2f sec in %s\n",
3014 (double)functime / NANOSEC, dli.dli_sname);
3018 * If we're getting ENOSPC with some regularity, stop.
3020 if (zs->zs_enospc_count > 10)
3028 * Kick off threads to run tests on all datasets in parallel.
3031 ztest_run(char *pool)
3034 ztest_shared_t *zs = ztest_shared;
3038 thread_t resume_tid;
3040 ztest_exiting = B_FALSE;
3042 (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
3043 (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
3045 for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
3046 (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
3049 * Destroy one disk before we even start.
3050 * It's mirrored, so everything should work just fine.
3051 * This makes us exercise fault handling very early in spa_load().
3053 ztest_obliterate_one_disk(0);
3056 * Verify that the sum of the sizes of all blocks in the pool
3057 * equals the SPA's allocated space total.
3059 ztest_verify_blocks(pool);
3062 * Kick off a replacement of the disk we just obliterated.
3064 kernel_init(FREAD | FWRITE);
3065 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3066 ztest_replace_one_disk(spa, 0);
3067 if (zopt_verbose >= 5)
3068 show_pool_stats(spa);
3069 spa_close(spa, FTAG);
3072 kernel_init(FREAD | FWRITE);
3075 * Verify that we can export the pool and reimport it under a
3078 if (ztest_random(2) == 0) {
3079 (void) snprintf(name, 100, "%s_import", pool);
3080 ztest_spa_import_export(pool, name);
3081 ztest_spa_import_export(name, pool);
3085 * Verify that we can loop over all pools.
3087 mutex_enter(&spa_namespace_lock);
3088 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa)) {
3089 if (zopt_verbose > 3) {
3090 (void) printf("spa_next: found %s\n", spa_name(spa));
3093 mutex_exit(&spa_namespace_lock);
3098 VERIFY(spa_open(pool, &spa, FTAG) == 0);
3101 * We don't expect the pool to suspend unless maxfaults == 0,
3102 * in which case ztest_fault_inject() temporarily takes away
3103 * the only valid replica.
3105 if (zopt_maxfaults == 0)
3106 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
3108 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
3111 * Create a thread to periodically resume suspended I/O.
3113 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
3117 * Verify that we can safely inquire about about any object,
3118 * whether it's allocated or not. To make it interesting,
3119 * we probe a 5-wide window around each power of two.
3120 * This hits all edge cases, including zero and the max.
3122 for (t = 0; t < 64; t++) {
3123 for (d = -5; d <= 5; d++) {
3124 error = dmu_object_info(spa->spa_meta_objset,
3125 (1ULL << t) + d, NULL);
3126 ASSERT(error == 0 || error == ENOENT ||
3132 * Now kick off all the tests that run in parallel.
3134 zs->zs_enospc_count = 0;
3136 za = umem_zalloc(zopt_threads * sizeof (ztest_args_t), UMEM_NOFAIL);
3138 if (zopt_verbose >= 4)
3139 (void) printf("starting main threads...\n");
3141 za[0].za_start = gethrtime();
3142 za[0].za_stop = za[0].za_start + zopt_passtime * NANOSEC;
3143 za[0].za_stop = MIN(za[0].za_stop, zs->zs_stop_time);
3144 za[0].za_kill = za[0].za_stop;
3145 if (ztest_random(100) < zopt_killrate)
3146 za[0].za_kill -= ztest_random(zopt_passtime * NANOSEC);
3148 for (t = 0; t < zopt_threads; t++) {
3149 d = t % zopt_datasets;
3151 (void) strcpy(za[t].za_pool, pool);
3152 za[t].za_os = za[d].za_os;
3154 za[t].za_zilog = za[d].za_zilog;
3155 za[t].za_instance = t;
3156 za[t].za_random = ztest_random(-1ULL);
3157 za[t].za_start = za[0].za_start;
3158 za[t].za_stop = za[0].za_stop;
3159 za[t].za_kill = za[0].za_kill;
3161 if (t < zopt_datasets) {
3162 int test_future = FALSE;
3163 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3164 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3165 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
3166 ztest_create_cb, NULL);
3167 if (error == EEXIST) {
3169 } else if (error == ENOSPC) {
3170 zs->zs_enospc_count++;
3171 (void) rw_unlock(&ztest_shared->zs_name_lock);
3173 } else if (error != 0) {
3174 fatal(0, "dmu_objset_create(%s) = %d",
3177 error = dmu_objset_open(name, DMU_OST_OTHER,
3178 DS_MODE_USER, &za[d].za_os);
3180 fatal(0, "dmu_objset_open('%s') = %d",
3182 (void) rw_unlock(&ztest_shared->zs_name_lock);
3184 ztest_dmu_check_future_leak(&za[t]);
3185 zil_replay(za[d].za_os, za[d].za_os,
3186 ztest_replay_vector);
3187 za[d].za_zilog = zil_open(za[d].za_os, NULL);
3190 VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
3191 &za[t].za_thread) == 0);
3195 VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
3196 if (t < zopt_datasets) {
3197 zil_close(za[t].za_zilog);
3198 dmu_objset_close(za[t].za_os);
3202 if (zopt_verbose >= 3)
3203 show_pool_stats(spa);
3205 txg_wait_synced(spa_get_dsl(spa), 0);
3207 zs->zs_alloc = spa_get_alloc(spa);
3208 zs->zs_space = spa_get_space(spa);
3211 * If we had out-of-space errors, destroy a random objset.
3213 if (zs->zs_enospc_count != 0) {
3214 (void) rw_rdlock(&ztest_shared->zs_name_lock);
3215 d = (int)ztest_random(zopt_datasets);
3216 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
3217 if (zopt_verbose >= 3)
3218 (void) printf("Destroying %s to free up space\n", name);
3219 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
3220 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3221 (void) rw_unlock(&ztest_shared->zs_name_lock);
3224 txg_wait_synced(spa_get_dsl(spa), 0);
3226 umem_free(za, zopt_threads * sizeof (ztest_args_t));
3228 /* Kill the resume thread */
3229 ztest_exiting = B_TRUE;
3230 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
3234 * Right before closing the pool, kick off a bunch of async I/O;
3235 * spa_close() should wait for it to complete.
3237 for (t = 1; t < 50; t++)
3238 dmu_prefetch(spa->spa_meta_objset, t, 0, 1 << 15);
3240 spa_close(spa, FTAG);
3246 print_time(hrtime_t t, char *timebuf)
3248 hrtime_t s = t / NANOSEC;
3249 hrtime_t m = s / 60;
3250 hrtime_t h = m / 60;
3251 hrtime_t d = h / 24;
3260 (void) sprintf(timebuf,
3261 "%llud%02lluh%02llum%02llus", d, h, m, s);
3263 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
3265 (void) sprintf(timebuf, "%llum%02llus", m, s);
3267 (void) sprintf(timebuf, "%llus", s);
3271 * Create a storage pool with the given name and initial vdev size.
3272 * Then create the specified number of datasets in the pool.
3275 ztest_init(char *pool)
3281 kernel_init(FREAD | FWRITE);
3284 * Create the storage pool.
3286 (void) spa_destroy(pool);
3287 ztest_shared->zs_vdev_primaries = 0;
3288 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
3289 0, zopt_raidz, zopt_mirrors, 1);
3290 error = spa_create(pool, nvroot, NULL, NULL, NULL);
3291 nvlist_free(nvroot);
3294 fatal(0, "spa_create() = %d", error);
3295 error = spa_open(pool, &spa, FTAG);
3297 fatal(0, "spa_open() = %d", error);
3299 if (zopt_verbose >= 3)
3300 show_pool_stats(spa);
3302 spa_close(spa, FTAG);
3308 main(int argc, char **argv)
3318 (void) setvbuf(stdout, NULL, _IOLBF, 0);
3320 /* Override location of zpool.cache */
3321 spa_config_path = "/tmp/zpool.cache";
3323 ztest_random_fd = open("/dev/urandom", O_RDONLY);
3325 process_options(argc, argv);
3328 * Blow away any existing copy of zpool.cache
3331 (void) remove("/tmp/zpool.cache");
3333 zs = ztest_shared = (void *)mmap(0,
3334 P2ROUNDUP(sizeof (ztest_shared_t), getpagesize()),
3335 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
3337 if (zopt_verbose >= 1) {
3338 (void) printf("%llu vdevs, %d datasets, %d threads,"
3339 " %llu seconds...\n",
3340 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
3341 (u_longlong_t)zopt_time);
3345 * Create and initialize our storage pool.
3347 for (i = 1; i <= zopt_init; i++) {
3348 bzero(zs, sizeof (ztest_shared_t));
3349 if (zopt_verbose >= 3 && zopt_init != 1)
3350 (void) printf("ztest_init(), pass %d\n", i);
3351 ztest_init(zopt_pool);
3355 * Initialize the call targets for each function.
3357 for (f = 0; f < ZTEST_FUNCS; f++) {
3358 zi = &zs->zs_info[f];
3360 *zi = ztest_info[f];
3362 if (*zi->zi_interval == 0)
3363 zi->zi_call_target = UINT64_MAX;
3365 zi->zi_call_target = zopt_time / *zi->zi_interval;
3368 zs->zs_start_time = gethrtime();
3369 zs->zs_stop_time = zs->zs_start_time + zopt_time * NANOSEC;
3372 * Run the tests in a loop. These tests include fault injection
3373 * to verify that self-healing data works, and forced crashes
3374 * to verify that we never lose on-disk consistency.
3376 while (gethrtime() < zs->zs_stop_time) {
3382 * Initialize the workload counters for each function.
3384 for (f = 0; f < ZTEST_FUNCS; f++) {
3385 zi = &zs->zs_info[f];
3387 zi->zi_call_time = 0;
3393 fatal(1, "fork failed");
3395 if (pid == 0) { /* child */
3396 struct rlimit rl = { 1024, 1024 };
3397 (void) setrlimit(RLIMIT_NOFILE, &rl);
3398 (void) enable_extended_FILE_stdio(-1, -1);
3399 ztest_run(zopt_pool);
3403 while (waitpid(pid, &status, 0) != pid)
3406 if (WIFEXITED(status)) {
3407 if (WEXITSTATUS(status) != 0) {
3408 (void) fprintf(stderr,
3409 "child exited with code %d\n",
3410 WEXITSTATUS(status));
3413 } else if (WIFSIGNALED(status)) {
3414 if (WTERMSIG(status) != SIGKILL) {
3415 (void) fprintf(stderr,
3416 "child died with signal %d\n",
3422 (void) fprintf(stderr, "something strange happened "
3429 if (zopt_verbose >= 1) {
3430 hrtime_t now = gethrtime();
3432 now = MIN(now, zs->zs_stop_time);
3433 print_time(zs->zs_stop_time - now, timebuf);
3434 nicenum(zs->zs_space, numbuf);
3436 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
3437 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
3439 WIFEXITED(status) ? "Complete" : "SIGKILL",
3440 (u_longlong_t)zs->zs_enospc_count,
3441 100.0 * zs->zs_alloc / zs->zs_space,
3443 100.0 * (now - zs->zs_start_time) /
3444 (zopt_time * NANOSEC), timebuf);
3447 if (zopt_verbose >= 2) {
3448 (void) printf("\nWorkload summary:\n\n");
3449 (void) printf("%7s %9s %s\n",
3450 "Calls", "Time", "Function");
3451 (void) printf("%7s %9s %s\n",
3452 "-----", "----", "--------");
3453 for (f = 0; f < ZTEST_FUNCS; f++) {
3456 zi = &zs->zs_info[f];
3457 print_time(zi->zi_call_time, timebuf);
3458 (void) dladdr((void *)zi->zi_func, &dli);
3459 (void) printf("%7llu %9s %s\n",
3460 (u_longlong_t)zi->zi_calls, timebuf,
3463 (void) printf("\n");
3467 * It's possible that we killed a child during a rename test, in
3468 * which case we'll have a 'ztest_tmp' pool lying around instead
3469 * of 'ztest'. Do a blind rename in case this happened.
3471 tmp = umem_alloc(strlen(zopt_pool) + 5, UMEM_NOFAIL);
3472 (void) strcpy(tmp, zopt_pool);
3473 (void) strcat(tmp, "_tmp");
3474 kernel_init(FREAD | FWRITE);
3475 (void) spa_rename(tmp, zopt_pool);
3477 umem_free(tmp, strlen(tmp) + 1);
3480 ztest_verify_blocks(zopt_pool);
3482 if (zopt_verbose >= 1) {
3483 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
3484 kills, iters - kills, (100.0 * kills) / MAX(1, iters));