4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * The objective of this program is to provide a DMU/ZAP/SPA stress test
27 * that runs entirely in userland, is easy to use, and easy to extend.
29 * The overall design of the ztest program is as follows:
31 * (1) For each major functional area (e.g. adding vdevs to a pool,
32 * creating and destroying datasets, reading and writing objects, etc)
33 * we have a simple routine to test that functionality. These
34 * individual routines do not have to do anything "stressful".
36 * (2) We turn these simple functionality tests into a stress test by
37 * running them all in parallel, with as many threads as desired,
38 * and spread across as many datasets, objects, and vdevs as desired.
40 * (3) While all this is happening, we inject faults into the pool to
41 * verify that self-healing data really works.
43 * (4) Every time we open a dataset, we change its checksum and compression
44 * functions. Thus even individual objects vary from block to block
45 * in which checksum they use and whether they're compressed.
47 * (5) To verify that we never lose on-disk consistency after a crash,
48 * we run the entire test in a child of the main process.
49 * At random times, the child self-immolates with a SIGKILL.
50 * This is the software equivalent of pulling the power cord.
51 * The parent then runs the test again, using the existing
52 * storage pool, as many times as desired.
54 * (6) To verify that we don't have future leaks or temporal incursions,
55 * many of the functional tests record the transaction group number
56 * as part of their data. When reading old data, they verify that
57 * the transaction group number is less than the current, open txg.
58 * If you add a new test, please do this if applicable.
60 * When run with no arguments, ztest runs for about five minutes and
61 * produces no output if successful. To get a little bit of information,
62 * specify -V. To get more information, specify -VV, and so on.
64 * To turn this into an overnight stress test, use -T to specify run time.
66 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
67 * to increase the pool capacity, fanout, and overall stress level.
69 * The -N(okill) option will suppress kills, so each child runs to completion.
70 * This can be useful when you're trying to distinguish temporal incursions
71 * from plain old race conditions.
74 #include <sys/zfs_context.h>
80 #include <sys/dmu_objset.h>
86 #include <sys/resource.h>
89 #include <sys/zil_impl.h>
90 #include <sys/vdev_impl.h>
91 #include <sys/vdev_file.h>
92 #include <sys/spa_impl.h>
93 #include <sys/metaslab_impl.h>
94 #include <sys/dsl_prop.h>
95 #include <sys/dsl_dataset.h>
96 #include <sys/dsl_scan.h>
97 #include <sys/zio_checksum.h>
98 #include <sys/refcount.h>
100 #include <stdio_ext.h>
109 #include <sys/fs/zfs.h>
110 #include <libnvpair.h>
112 static char cmdname[] = "ztest";
113 static char *zopt_pool = cmdname;
114 static char *progname;
116 static uint64_t zopt_vdevs = 5;
117 static uint64_t zopt_vdevtime;
118 static int zopt_ashift = SPA_MINBLOCKSHIFT;
119 static int zopt_mirrors = 2;
120 static int zopt_raidz = 4;
121 static int zopt_raidz_parity = 1;
122 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
123 static int zopt_datasets = 7;
124 static int zopt_threads = 23;
125 static uint64_t zopt_passtime = 60; /* 60 seconds */
126 static uint64_t zopt_killrate = 70; /* 70% kill rate */
127 static int zopt_verbose = 0;
128 static int zopt_init = 1;
129 static char *zopt_dir = "/tmp";
130 static uint64_t zopt_time = 300; /* 5 minutes */
131 static uint64_t zopt_maxloops = 50; /* max loops during spa_freeze() */
133 #define BT_MAGIC 0x123456789abcdefULL
134 #define MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
138 ZTEST_IO_WRITE_PATTERN,
139 ZTEST_IO_WRITE_ZEROES,
145 typedef struct ztest_block_tag {
155 typedef struct bufwad {
162 * XXX -- fix zfs range locks to be generic so we can use them here.
184 #define ZTEST_RANGE_LOCKS 64
185 #define ZTEST_OBJECT_LOCKS 64
188 * Object descriptor. Used as a template for object lookup/create/remove.
190 typedef struct ztest_od {
193 dmu_object_type_t od_type;
194 dmu_object_type_t od_crtype;
195 uint64_t od_blocksize;
196 uint64_t od_crblocksize;
199 char od_name[MAXNAMELEN];
205 typedef struct ztest_ds {
209 ztest_od_t *zd_od; /* debugging aid */
210 char zd_name[MAXNAMELEN];
211 mutex_t zd_dirobj_lock;
212 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
213 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
217 * Per-iteration state.
219 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
221 typedef struct ztest_info {
222 ztest_func_t *zi_func; /* test function */
223 uint64_t zi_iters; /* iterations per execution */
224 uint64_t *zi_interval; /* execute every <interval> seconds */
225 uint64_t zi_call_count; /* per-pass count */
226 uint64_t zi_call_time; /* per-pass time */
227 uint64_t zi_call_next; /* next time to call this function */
231 * Note: these aren't static because we want dladdr() to work.
233 ztest_func_t ztest_dmu_read_write;
234 ztest_func_t ztest_dmu_write_parallel;
235 ztest_func_t ztest_dmu_object_alloc_free;
236 ztest_func_t ztest_dmu_commit_callbacks;
237 ztest_func_t ztest_zap;
238 ztest_func_t ztest_zap_parallel;
239 ztest_func_t ztest_zil_commit;
240 ztest_func_t ztest_dmu_read_write_zcopy;
241 ztest_func_t ztest_dmu_objset_create_destroy;
242 ztest_func_t ztest_dmu_prealloc;
243 ztest_func_t ztest_fzap;
244 ztest_func_t ztest_dmu_snapshot_create_destroy;
245 ztest_func_t ztest_dsl_prop_get_set;
246 ztest_func_t ztest_spa_prop_get_set;
247 ztest_func_t ztest_spa_create_destroy;
248 ztest_func_t ztest_fault_inject;
249 ztest_func_t ztest_ddt_repair;
250 ztest_func_t ztest_dmu_snapshot_hold;
251 ztest_func_t ztest_spa_rename;
252 ztest_func_t ztest_scrub;
253 ztest_func_t ztest_dsl_dataset_promote_busy;
254 ztest_func_t ztest_vdev_attach_detach;
255 ztest_func_t ztest_vdev_LUN_growth;
256 ztest_func_t ztest_vdev_add_remove;
257 ztest_func_t ztest_vdev_aux_add_remove;
258 ztest_func_t ztest_split_pool;
260 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
261 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
262 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
263 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
264 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
266 ztest_info_t ztest_info[] = {
267 { ztest_dmu_read_write, 1, &zopt_always },
268 { ztest_dmu_write_parallel, 10, &zopt_always },
269 { ztest_dmu_object_alloc_free, 1, &zopt_always },
270 { ztest_dmu_commit_callbacks, 1, &zopt_always },
271 { ztest_zap, 30, &zopt_always },
272 { ztest_zap_parallel, 100, &zopt_always },
273 { ztest_split_pool, 1, &zopt_always },
274 { ztest_zil_commit, 1, &zopt_incessant },
275 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
276 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
277 { ztest_dsl_prop_get_set, 1, &zopt_often },
278 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
280 { ztest_dmu_prealloc, 1, &zopt_sometimes },
282 { ztest_fzap, 1, &zopt_sometimes },
283 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
284 { ztest_spa_create_destroy, 1, &zopt_sometimes },
285 { ztest_fault_inject, 1, &zopt_sometimes },
286 { ztest_ddt_repair, 1, &zopt_sometimes },
287 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
288 { ztest_spa_rename, 1, &zopt_rarely },
289 { ztest_scrub, 1, &zopt_rarely },
290 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
291 { ztest_vdev_attach_detach, 1, &zopt_rarely },
292 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
293 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
294 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
297 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
300 * The following struct is used to hold a list of uncalled commit callbacks.
301 * The callbacks are ordered by txg number.
303 typedef struct ztest_cb_list {
304 mutex_t zcl_callbacks_lock;
305 list_t zcl_callbacks;
309 * Stuff we need to share writably between parent and child.
311 typedef struct ztest_shared {
314 hrtime_t zs_proc_start;
315 hrtime_t zs_proc_stop;
316 hrtime_t zs_thread_start;
317 hrtime_t zs_thread_stop;
318 hrtime_t zs_thread_kill;
319 uint64_t zs_enospc_count;
320 uint64_t zs_vdev_next_leaf;
321 uint64_t zs_vdev_aux;
324 mutex_t zs_vdev_lock;
325 rwlock_t zs_name_lock;
326 ztest_info_t zs_info[ZTEST_FUNCS];
332 #define ID_PARALLEL -1ULL
334 static char ztest_dev_template[] = "%s/%s.%llua";
335 static char ztest_aux_template[] = "%s/%s.%s.%llu";
336 ztest_shared_t *ztest_shared;
339 static int ztest_random_fd;
340 static int ztest_dump_core = 1;
342 static boolean_t ztest_exiting;
344 /* Global commit callback list */
345 static ztest_cb_list_t zcl;
347 extern uint64_t metaslab_gang_bang;
348 extern uint64_t metaslab_df_alloc_threshold;
349 static uint64_t metaslab_sz;
352 ZTEST_META_DNODE = 0,
357 static void usage(boolean_t) __NORETURN;
360 * These libumem hooks provide a reasonable set of defaults for the allocator's
361 * debugging facilities.
366 return ("default,verbose"); /* $UMEM_DEBUG setting */
370 _umem_logging_init(void)
372 return ("fail,contents"); /* $UMEM_LOGGING setting */
375 #define FATAL_MSG_SZ 1024
380 fatal(int do_perror, char *message, ...)
383 int save_errno = errno;
384 char buf[FATAL_MSG_SZ];
386 (void) fflush(stdout);
388 va_start(args, message);
389 (void) sprintf(buf, "ztest: ");
391 (void) vsprintf(buf + strlen(buf), message, args);
394 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
395 ": %s", strerror(save_errno));
397 (void) fprintf(stderr, "%s\n", buf);
398 fatal_msg = buf; /* to ease debugging */
405 str2shift(const char *buf)
407 const char *ends = "BKMGTPEZ";
412 for (i = 0; i < strlen(ends); i++) {
413 if (toupper(buf[0]) == ends[i])
416 if (i == strlen(ends)) {
417 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
421 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
424 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
430 nicenumtoull(const char *buf)
435 val = strtoull(buf, &end, 0);
437 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
439 } else if (end[0] == '.') {
440 double fval = strtod(buf, &end);
441 fval *= pow(2, str2shift(end));
442 if (fval > UINT64_MAX) {
443 (void) fprintf(stderr, "ztest: value too large: %s\n",
447 val = (uint64_t)fval;
449 int shift = str2shift(end);
450 if (shift >= 64 || (val << shift) >> shift != val) {
451 (void) fprintf(stderr, "ztest: value too large: %s\n",
461 usage(boolean_t requested)
463 char nice_vdev_size[10];
464 char nice_gang_bang[10];
465 FILE *fp = requested ? stdout : stderr;
467 nicenum(zopt_vdev_size, nice_vdev_size);
468 nicenum(metaslab_gang_bang, nice_gang_bang);
470 (void) fprintf(fp, "Usage: %s\n"
471 "\t[-v vdevs (default: %llu)]\n"
472 "\t[-s size_of_each_vdev (default: %s)]\n"
473 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
474 "\t[-m mirror_copies (default: %d)]\n"
475 "\t[-r raidz_disks (default: %d)]\n"
476 "\t[-R raidz_parity (default: %d)]\n"
477 "\t[-d datasets (default: %d)]\n"
478 "\t[-t threads (default: %d)]\n"
479 "\t[-g gang_block_threshold (default: %s)]\n"
480 "\t[-i init_count (default: %d)] initialize pool i times\n"
481 "\t[-k kill_percentage (default: %llu%%)]\n"
482 "\t[-p pool_name (default: %s)]\n"
483 "\t[-f dir (default: %s)] file directory for vdev files\n"
484 "\t[-V] verbose (use multiple times for ever more blather)\n"
485 "\t[-E] use existing pool instead of creating new one\n"
486 "\t[-T time (default: %llu sec)] total run time\n"
487 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
488 "\t[-P passtime (default: %llu sec)] time per pass\n"
489 "\t[-h] (print help)\n"
492 (u_longlong_t)zopt_vdevs, /* -v */
493 nice_vdev_size, /* -s */
494 zopt_ashift, /* -a */
495 zopt_mirrors, /* -m */
497 zopt_raidz_parity, /* -R */
498 zopt_datasets, /* -d */
499 zopt_threads, /* -t */
500 nice_gang_bang, /* -g */
502 (u_longlong_t)zopt_killrate, /* -k */
505 (u_longlong_t)zopt_time, /* -T */
506 (u_longlong_t)zopt_maxloops, /* -F */
507 (u_longlong_t)zopt_passtime); /* -P */
508 exit(requested ? 0 : 1);
512 process_options(int argc, char **argv)
517 /* Remember program name. */
520 /* By default, test gang blocks for blocks 32K and greater */
521 metaslab_gang_bang = 32 << 10;
523 while ((opt = getopt(argc, argv,
524 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
541 value = nicenumtoull(optarg);
548 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
554 zopt_mirrors = value;
557 zopt_raidz = MAX(1, value);
560 zopt_raidz_parity = MIN(MAX(value, 1), 3);
563 zopt_datasets = MAX(1, value);
566 zopt_threads = MAX(1, value);
569 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
575 zopt_killrate = value;
578 zopt_pool = strdup(optarg);
581 zopt_dir = strdup(optarg);
593 zopt_passtime = MAX(1, value);
596 zopt_maxloops = MAX(1, value);
608 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
610 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
615 ztest_kill(ztest_shared_t *zs)
617 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
618 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
619 (void) kill(getpid(), SIGKILL);
623 ztest_random(uint64_t range)
630 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
631 fatal(1, "short read from /dev/urandom");
638 ztest_record_enospc(const char *s)
640 ztest_shared->zs_enospc_count++;
644 ztest_get_ashift(void)
646 if (zopt_ashift == 0)
647 return (SPA_MINBLOCKSHIFT + ztest_random(3));
648 return (zopt_ashift);
652 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
654 char pathbuf[MAXPATHLEN];
659 ashift = ztest_get_ashift();
665 vdev = ztest_shared->zs_vdev_aux;
666 (void) sprintf(path, ztest_aux_template,
667 zopt_dir, zopt_pool, aux, vdev);
669 vdev = ztest_shared->zs_vdev_next_leaf++;
670 (void) sprintf(path, ztest_dev_template,
671 zopt_dir, zopt_pool, vdev);
676 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
678 fatal(1, "can't open %s", path);
679 if (ftruncate(fd, size) != 0)
680 fatal(1, "can't ftruncate %s", path);
684 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
685 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
686 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
687 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
693 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
695 nvlist_t *raidz, **child;
699 return (make_vdev_file(path, aux, size, ashift));
700 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
702 for (c = 0; c < r; c++)
703 child[c] = make_vdev_file(path, aux, size, ashift);
705 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
706 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
707 VDEV_TYPE_RAIDZ) == 0);
708 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
709 zopt_raidz_parity) == 0);
710 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
713 for (c = 0; c < r; c++)
714 nvlist_free(child[c]);
716 umem_free(child, r * sizeof (nvlist_t *));
722 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
725 nvlist_t *mirror, **child;
729 return (make_vdev_raidz(path, aux, size, ashift, r));
731 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
733 for (c = 0; c < m; c++)
734 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
736 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
737 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
738 VDEV_TYPE_MIRROR) == 0);
739 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
742 for (c = 0; c < m; c++)
743 nvlist_free(child[c]);
745 umem_free(child, m * sizeof (nvlist_t *));
751 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
752 int log, int r, int m, int t)
754 nvlist_t *root, **child;
759 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
761 for (c = 0; c < t; c++) {
762 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
763 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
767 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
768 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
769 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
772 for (c = 0; c < t; c++)
773 nvlist_free(child[c]);
775 umem_free(child, t * sizeof (nvlist_t *));
781 ztest_random_blocksize(void)
783 return (1 << (SPA_MINBLOCKSHIFT +
784 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
788 ztest_random_ibshift(void)
790 return (DN_MIN_INDBLKSHIFT +
791 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
795 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
798 vdev_t *rvd = spa->spa_root_vdev;
801 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
804 top = ztest_random(rvd->vdev_children);
805 tvd = rvd->vdev_child[top];
806 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
807 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
813 ztest_random_dsl_prop(zfs_prop_t prop)
818 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
819 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
825 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
828 const char *propname = zfs_prop_to_name(prop);
830 char setpoint[MAXPATHLEN];
834 error = dsl_prop_set(osname, propname,
835 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
836 sizeof (value), 1, &value);
838 if (error == ENOSPC) {
839 ztest_record_enospc(FTAG);
842 ASSERT3U(error, ==, 0);
844 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
845 1, &curval, setpoint), ==, 0);
847 if (zopt_verbose >= 6) {
848 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
849 (void) printf("%s %s = %s at '%s'\n",
850 osname, propname, valname, setpoint);
857 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
859 spa_t *spa = zs->zs_spa;
860 nvlist_t *props = NULL;
863 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
864 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
866 error = spa_prop_set(spa, props);
870 if (error == ENOSPC) {
871 ztest_record_enospc(FTAG);
874 ASSERT3U(error, ==, 0);
880 ztest_rll_init(rll_t *rll)
882 rll->rll_writer = NULL;
883 rll->rll_readers = 0;
884 VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
885 VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
889 ztest_rll_destroy(rll_t *rll)
891 ASSERT(rll->rll_writer == NULL);
892 ASSERT(rll->rll_readers == 0);
893 VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
894 VERIFY(cond_destroy(&rll->rll_cv) == 0);
898 ztest_rll_lock(rll_t *rll, rl_type_t type)
900 VERIFY(mutex_lock(&rll->rll_lock) == 0);
902 if (type == RL_READER) {
903 while (rll->rll_writer != NULL)
904 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
907 while (rll->rll_writer != NULL || rll->rll_readers)
908 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
909 rll->rll_writer = curthread;
912 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
916 ztest_rll_unlock(rll_t *rll)
918 VERIFY(mutex_lock(&rll->rll_lock) == 0);
920 if (rll->rll_writer) {
921 ASSERT(rll->rll_readers == 0);
922 rll->rll_writer = NULL;
924 ASSERT(rll->rll_readers != 0);
925 ASSERT(rll->rll_writer == NULL);
929 if (rll->rll_writer == NULL && rll->rll_readers == 0)
930 VERIFY(cond_broadcast(&rll->rll_cv) == 0);
932 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
936 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
938 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
940 ztest_rll_lock(rll, type);
944 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
946 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
948 ztest_rll_unlock(rll);
952 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
953 uint64_t size, rl_type_t type)
955 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
956 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
959 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
960 rl->rl_object = object;
961 rl->rl_offset = offset;
965 ztest_rll_lock(rll, type);
971 ztest_range_unlock(rl_t *rl)
973 rll_t *rll = rl->rl_lock;
975 ztest_rll_unlock(rll);
977 umem_free(rl, sizeof (*rl));
981 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
984 zd->zd_zilog = dmu_objset_zil(os);
986 dmu_objset_name(os, zd->zd_name);
988 VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
990 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
991 ztest_rll_init(&zd->zd_object_lock[l]);
993 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
994 ztest_rll_init(&zd->zd_range_lock[l]);
998 ztest_zd_fini(ztest_ds_t *zd)
1000 VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1002 for (int l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1003 ztest_rll_destroy(&zd->zd_object_lock[l]);
1005 for (int l = 0; l < ZTEST_RANGE_LOCKS; l++)
1006 ztest_rll_destroy(&zd->zd_range_lock[l]);
1009 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1012 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1018 * Attempt to assign tx to some transaction group.
1020 error = dmu_tx_assign(tx, txg_how);
1022 if (error == ERESTART) {
1023 ASSERT(txg_how == TXG_NOWAIT);
1026 ASSERT3U(error, ==, ENOSPC);
1027 ztest_record_enospc(tag);
1032 txg = dmu_tx_get_txg(tx);
1038 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1041 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1048 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1051 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1055 diff |= (value - *ip++);
1061 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1062 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1064 bt->bt_magic = BT_MAGIC;
1065 bt->bt_objset = dmu_objset_id(os);
1066 bt->bt_object = object;
1067 bt->bt_offset = offset;
1070 bt->bt_crtxg = crtxg;
1074 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1075 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1077 ASSERT(bt->bt_magic == BT_MAGIC);
1078 ASSERT(bt->bt_objset == dmu_objset_id(os));
1079 ASSERT(bt->bt_object == object);
1080 ASSERT(bt->bt_offset == offset);
1081 ASSERT(bt->bt_gen <= gen);
1082 ASSERT(bt->bt_txg <= txg);
1083 ASSERT(bt->bt_crtxg == crtxg);
1086 static ztest_block_tag_t *
1087 ztest_bt_bonus(dmu_buf_t *db)
1089 dmu_object_info_t doi;
1090 ztest_block_tag_t *bt;
1092 dmu_object_info_from_db(db, &doi);
1093 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1094 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1095 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1104 #define lrz_type lr_mode
1105 #define lrz_blocksize lr_uid
1106 #define lrz_ibshift lr_gid
1107 #define lrz_bonustype lr_rdev
1108 #define lrz_bonuslen lr_crtime[1]
1111 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1113 char *name = (void *)(lr + 1); /* name follows lr */
1114 size_t namesize = strlen(name) + 1;
1117 if (zil_replaying(zd->zd_zilog, tx))
1120 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1121 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1122 sizeof (*lr) + namesize - sizeof (lr_t));
1124 zil_itx_assign(zd->zd_zilog, itx, tx);
1128 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1130 char *name = (void *)(lr + 1); /* name follows lr */
1131 size_t namesize = strlen(name) + 1;
1134 if (zil_replaying(zd->zd_zilog, tx))
1137 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1138 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1139 sizeof (*lr) + namesize - sizeof (lr_t));
1141 itx->itx_oid = object;
1142 zil_itx_assign(zd->zd_zilog, itx, tx);
1146 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1149 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1151 if (zil_replaying(zd->zd_zilog, tx))
1154 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1155 write_state = WR_INDIRECT;
1157 itx = zil_itx_create(TX_WRITE,
1158 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1160 if (write_state == WR_COPIED &&
1161 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1162 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1163 zil_itx_destroy(itx);
1164 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1165 write_state = WR_NEED_COPY;
1167 itx->itx_private = zd;
1168 itx->itx_wr_state = write_state;
1169 itx->itx_sync = (ztest_random(8) == 0);
1170 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1172 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1173 sizeof (*lr) - sizeof (lr_t));
1175 zil_itx_assign(zd->zd_zilog, itx, tx);
1179 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1183 if (zil_replaying(zd->zd_zilog, tx))
1186 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1187 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1188 sizeof (*lr) - sizeof (lr_t));
1190 itx->itx_sync = B_FALSE;
1191 zil_itx_assign(zd->zd_zilog, itx, tx);
1195 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1199 if (zil_replaying(zd->zd_zilog, tx))
1202 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1203 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1204 sizeof (*lr) - sizeof (lr_t));
1206 itx->itx_sync = B_FALSE;
1207 zil_itx_assign(zd->zd_zilog, itx, tx);
1214 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1216 char *name = (void *)(lr + 1); /* name follows lr */
1217 objset_t *os = zd->zd_os;
1218 ztest_block_tag_t *bbt;
1225 byteswap_uint64_array(lr, sizeof (*lr));
1227 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1228 ASSERT(name[0] != '\0');
1230 tx = dmu_tx_create(os);
1232 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1234 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1235 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1237 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1240 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1244 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1246 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1247 if (lr->lr_foid == 0) {
1248 lr->lr_foid = zap_create(os,
1249 lr->lrz_type, lr->lrz_bonustype,
1250 lr->lrz_bonuslen, tx);
1252 error = zap_create_claim(os, lr->lr_foid,
1253 lr->lrz_type, lr->lrz_bonustype,
1254 lr->lrz_bonuslen, tx);
1257 if (lr->lr_foid == 0) {
1258 lr->lr_foid = dmu_object_alloc(os,
1259 lr->lrz_type, 0, lr->lrz_bonustype,
1260 lr->lrz_bonuslen, tx);
1262 error = dmu_object_claim(os, lr->lr_foid,
1263 lr->lrz_type, 0, lr->lrz_bonustype,
1264 lr->lrz_bonuslen, tx);
1269 ASSERT3U(error, ==, EEXIST);
1270 ASSERT(zd->zd_zilog->zl_replay);
1275 ASSERT(lr->lr_foid != 0);
1277 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1278 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1279 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1281 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1282 bbt = ztest_bt_bonus(db);
1283 dmu_buf_will_dirty(db, tx);
1284 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1285 dmu_buf_rele(db, FTAG);
1287 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1290 (void) ztest_log_create(zd, tx, lr);
1298 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1300 char *name = (void *)(lr + 1); /* name follows lr */
1301 objset_t *os = zd->zd_os;
1302 dmu_object_info_t doi;
1304 uint64_t object, txg;
1307 byteswap_uint64_array(lr, sizeof (*lr));
1309 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1310 ASSERT(name[0] != '\0');
1313 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1314 ASSERT(object != 0);
1316 ztest_object_lock(zd, object, RL_WRITER);
1318 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1320 tx = dmu_tx_create(os);
1322 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1323 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1325 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1327 ztest_object_unlock(zd, object);
1331 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1332 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1334 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1337 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1339 (void) ztest_log_remove(zd, tx, lr, object);
1343 ztest_object_unlock(zd, object);
1349 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1351 objset_t *os = zd->zd_os;
1352 void *data = lr + 1; /* data follows lr */
1353 uint64_t offset, length;
1354 ztest_block_tag_t *bt = data;
1355 ztest_block_tag_t *bbt;
1356 uint64_t gen, txg, lrtxg, crtxg;
1357 dmu_object_info_t doi;
1360 arc_buf_t *abuf = NULL;
1364 byteswap_uint64_array(lr, sizeof (*lr));
1366 offset = lr->lr_offset;
1367 length = lr->lr_length;
1369 /* If it's a dmu_sync() block, write the whole block */
1370 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1371 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1372 if (length < blocksize) {
1373 offset -= offset % blocksize;
1378 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1379 byteswap_uint64_array(bt, sizeof (*bt));
1381 if (bt->bt_magic != BT_MAGIC)
1384 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1385 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1387 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1389 dmu_object_info_from_db(db, &doi);
1391 bbt = ztest_bt_bonus(db);
1392 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1394 crtxg = bbt->bt_crtxg;
1395 lrtxg = lr->lr_common.lrc_txg;
1397 tx = dmu_tx_create(os);
1399 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1401 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1402 P2PHASE(offset, length) == 0)
1403 abuf = dmu_request_arcbuf(db, length);
1405 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1408 dmu_return_arcbuf(abuf);
1409 dmu_buf_rele(db, FTAG);
1410 ztest_range_unlock(rl);
1411 ztest_object_unlock(zd, lr->lr_foid);
1417 * Usually, verify the old data before writing new data --
1418 * but not always, because we also want to verify correct
1419 * behavior when the data was not recently read into cache.
1421 ASSERT(offset % doi.doi_data_block_size == 0);
1422 if (ztest_random(4) != 0) {
1423 int prefetch = ztest_random(2) ?
1424 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1425 ztest_block_tag_t rbt;
1427 VERIFY(dmu_read(os, lr->lr_foid, offset,
1428 sizeof (rbt), &rbt, prefetch) == 0);
1429 if (rbt.bt_magic == BT_MAGIC) {
1430 ztest_bt_verify(&rbt, os, lr->lr_foid,
1431 offset, gen, txg, crtxg);
1436 * Writes can appear to be newer than the bonus buffer because
1437 * the ztest_get_data() callback does a dmu_read() of the
1438 * open-context data, which may be different than the data
1439 * as it was when the write was generated.
1441 if (zd->zd_zilog->zl_replay) {
1442 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1443 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1448 * Set the bt's gen/txg to the bonus buffer's gen/txg
1449 * so that all of the usual ASSERTs will work.
1451 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1455 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1457 bcopy(data, abuf->b_data, length);
1458 dmu_assign_arcbuf(db, offset, abuf, tx);
1461 (void) ztest_log_write(zd, tx, lr);
1463 dmu_buf_rele(db, FTAG);
1467 ztest_range_unlock(rl);
1468 ztest_object_unlock(zd, lr->lr_foid);
1474 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1476 objset_t *os = zd->zd_os;
1482 byteswap_uint64_array(lr, sizeof (*lr));
1484 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1485 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1488 tx = dmu_tx_create(os);
1490 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1492 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1494 ztest_range_unlock(rl);
1495 ztest_object_unlock(zd, lr->lr_foid);
1499 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1500 lr->lr_length, tx) == 0);
1502 (void) ztest_log_truncate(zd, tx, lr);
1506 ztest_range_unlock(rl);
1507 ztest_object_unlock(zd, lr->lr_foid);
1513 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1515 objset_t *os = zd->zd_os;
1518 ztest_block_tag_t *bbt;
1519 uint64_t txg, lrtxg, crtxg;
1522 byteswap_uint64_array(lr, sizeof (*lr));
1524 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1526 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1528 tx = dmu_tx_create(os);
1529 dmu_tx_hold_bonus(tx, lr->lr_foid);
1531 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1533 dmu_buf_rele(db, FTAG);
1534 ztest_object_unlock(zd, lr->lr_foid);
1538 bbt = ztest_bt_bonus(db);
1539 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1540 crtxg = bbt->bt_crtxg;
1541 lrtxg = lr->lr_common.lrc_txg;
1543 if (zd->zd_zilog->zl_replay) {
1544 ASSERT(lr->lr_size != 0);
1545 ASSERT(lr->lr_mode != 0);
1549 * Randomly change the size and increment the generation.
1551 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1553 lr->lr_mode = bbt->bt_gen + 1;
1558 * Verify that the current bonus buffer is not newer than our txg.
1560 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1561 MAX(txg, lrtxg), crtxg);
1563 dmu_buf_will_dirty(db, tx);
1565 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1566 ASSERT3U(lr->lr_size, <=, db->db_size);
1567 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1568 bbt = ztest_bt_bonus(db);
1570 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1572 dmu_buf_rele(db, FTAG);
1574 (void) ztest_log_setattr(zd, tx, lr);
1578 ztest_object_unlock(zd, lr->lr_foid);
1583 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1584 NULL, /* 0 no such transaction type */
1585 ztest_replay_create, /* TX_CREATE */
1586 NULL, /* TX_MKDIR */
1587 NULL, /* TX_MKXATTR */
1588 NULL, /* TX_SYMLINK */
1589 ztest_replay_remove, /* TX_REMOVE */
1590 NULL, /* TX_RMDIR */
1592 NULL, /* TX_RENAME */
1593 ztest_replay_write, /* TX_WRITE */
1594 ztest_replay_truncate, /* TX_TRUNCATE */
1595 ztest_replay_setattr, /* TX_SETATTR */
1597 NULL, /* TX_CREATE_ACL */
1598 NULL, /* TX_CREATE_ATTR */
1599 NULL, /* TX_CREATE_ACL_ATTR */
1600 NULL, /* TX_MKDIR_ACL */
1601 NULL, /* TX_MKDIR_ATTR */
1602 NULL, /* TX_MKDIR_ACL_ATTR */
1603 NULL, /* TX_WRITE2 */
1607 * ZIL get_data callbacks
1611 ztest_get_done(zgd_t *zgd, int error)
1613 ztest_ds_t *zd = zgd->zgd_private;
1614 uint64_t object = zgd->zgd_rl->rl_object;
1617 dmu_buf_rele(zgd->zgd_db, zgd);
1619 ztest_range_unlock(zgd->zgd_rl);
1620 ztest_object_unlock(zd, object);
1622 if (error == 0 && zgd->zgd_bp)
1623 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1625 umem_free(zgd, sizeof (*zgd));
1629 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1631 ztest_ds_t *zd = arg;
1632 objset_t *os = zd->zd_os;
1633 uint64_t object = lr->lr_foid;
1634 uint64_t offset = lr->lr_offset;
1635 uint64_t size = lr->lr_length;
1636 blkptr_t *bp = &lr->lr_blkptr;
1637 uint64_t txg = lr->lr_common.lrc_txg;
1639 dmu_object_info_t doi;
1644 ztest_object_lock(zd, object, RL_READER);
1645 error = dmu_bonus_hold(os, object, FTAG, &db);
1647 ztest_object_unlock(zd, object);
1651 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1653 if (crtxg == 0 || crtxg > txg) {
1654 dmu_buf_rele(db, FTAG);
1655 ztest_object_unlock(zd, object);
1659 dmu_object_info_from_db(db, &doi);
1660 dmu_buf_rele(db, FTAG);
1663 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1664 zgd->zgd_zilog = zd->zd_zilog;
1665 zgd->zgd_private = zd;
1667 if (buf != NULL) { /* immediate write */
1668 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1671 error = dmu_read(os, object, offset, size, buf,
1672 DMU_READ_NO_PREFETCH);
1675 size = doi.doi_data_block_size;
1677 offset = P2ALIGN(offset, size);
1679 ASSERT(offset < size);
1683 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1686 error = dmu_buf_hold(os, object, offset, zgd, &db,
1687 DMU_READ_NO_PREFETCH);
1693 ASSERT(db->db_offset == offset);
1694 ASSERT(db->db_size == size);
1696 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1697 ztest_get_done, zgd);
1704 ztest_get_done(zgd, error);
1710 ztest_lr_alloc(size_t lrsize, char *name)
1713 size_t namesize = name ? strlen(name) + 1 : 0;
1715 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1718 bcopy(name, lr + lrsize, namesize);
1724 ztest_lr_free(void *lr, size_t lrsize, char *name)
1726 size_t namesize = name ? strlen(name) + 1 : 0;
1728 umem_free(lr, lrsize + namesize);
1732 * Lookup a bunch of objects. Returns the number of objects not found.
1735 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1740 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1742 for (int i = 0; i < count; i++, od++) {
1744 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1745 sizeof (uint64_t), 1, &od->od_object);
1747 ASSERT(error == ENOENT);
1748 ASSERT(od->od_object == 0);
1752 ztest_block_tag_t *bbt;
1753 dmu_object_info_t doi;
1755 ASSERT(od->od_object != 0);
1756 ASSERT(missing == 0); /* there should be no gaps */
1758 ztest_object_lock(zd, od->od_object, RL_READER);
1759 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1760 od->od_object, FTAG, &db));
1761 dmu_object_info_from_db(db, &doi);
1762 bbt = ztest_bt_bonus(db);
1763 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1764 od->od_type = doi.doi_type;
1765 od->od_blocksize = doi.doi_data_block_size;
1766 od->od_gen = bbt->bt_gen;
1767 dmu_buf_rele(db, FTAG);
1768 ztest_object_unlock(zd, od->od_object);
1776 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1780 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1782 for (int i = 0; i < count; i++, od++) {
1789 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1791 lr->lr_doid = od->od_dir;
1792 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1793 lr->lrz_type = od->od_crtype;
1794 lr->lrz_blocksize = od->od_crblocksize;
1795 lr->lrz_ibshift = ztest_random_ibshift();
1796 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1797 lr->lrz_bonuslen = dmu_bonus_max();
1798 lr->lr_gen = od->od_crgen;
1799 lr->lr_crtime[0] = time(NULL);
1801 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1802 ASSERT(missing == 0);
1806 od->od_object = lr->lr_foid;
1807 od->od_type = od->od_crtype;
1808 od->od_blocksize = od->od_crblocksize;
1809 od->od_gen = od->od_crgen;
1810 ASSERT(od->od_object != 0);
1813 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1820 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1825 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1829 for (int i = count - 1; i >= 0; i--, od--) {
1835 if (od->od_object == 0)
1838 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1840 lr->lr_doid = od->od_dir;
1842 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1843 ASSERT3U(error, ==, ENOSPC);
1848 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1855 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1861 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1863 lr->lr_foid = object;
1864 lr->lr_offset = offset;
1865 lr->lr_length = size;
1867 BP_ZERO(&lr->lr_blkptr);
1869 bcopy(data, lr + 1, size);
1871 error = ztest_replay_write(zd, lr, B_FALSE);
1873 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1879 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1884 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1886 lr->lr_foid = object;
1887 lr->lr_offset = offset;
1888 lr->lr_length = size;
1890 error = ztest_replay_truncate(zd, lr, B_FALSE);
1892 ztest_lr_free(lr, sizeof (*lr), NULL);
1898 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1903 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1905 lr->lr_foid = object;
1909 error = ztest_replay_setattr(zd, lr, B_FALSE);
1911 ztest_lr_free(lr, sizeof (*lr), NULL);
1917 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1919 objset_t *os = zd->zd_os;
1924 txg_wait_synced(dmu_objset_pool(os), 0);
1926 ztest_object_lock(zd, object, RL_READER);
1927 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1929 tx = dmu_tx_create(os);
1931 dmu_tx_hold_write(tx, object, offset, size);
1933 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1936 dmu_prealloc(os, object, offset, size, tx);
1938 txg_wait_synced(dmu_objset_pool(os), txg);
1940 (void) dmu_free_long_range(os, object, offset, size);
1943 ztest_range_unlock(rl);
1944 ztest_object_unlock(zd, object);
1948 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1950 ztest_block_tag_t wbt;
1951 dmu_object_info_t doi;
1952 enum ztest_io_type io_type;
1956 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1957 blocksize = doi.doi_data_block_size;
1958 data = umem_alloc(blocksize, UMEM_NOFAIL);
1961 * Pick an i/o type at random, biased toward writing block tags.
1963 io_type = ztest_random(ZTEST_IO_TYPES);
1964 if (ztest_random(2) == 0)
1965 io_type = ZTEST_IO_WRITE_TAG;
1969 case ZTEST_IO_WRITE_TAG:
1970 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1971 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1974 case ZTEST_IO_WRITE_PATTERN:
1975 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
1976 if (ztest_random(2) == 0) {
1978 * Induce fletcher2 collisions to ensure that
1979 * zio_ddt_collision() detects and resolves them
1980 * when using fletcher2-verify for deduplication.
1982 ((uint64_t *)data)[0] ^= 1ULL << 63;
1983 ((uint64_t *)data)[4] ^= 1ULL << 63;
1985 (void) ztest_write(zd, object, offset, blocksize, data);
1988 case ZTEST_IO_WRITE_ZEROES:
1989 bzero(data, blocksize);
1990 (void) ztest_write(zd, object, offset, blocksize, data);
1993 case ZTEST_IO_TRUNCATE:
1994 (void) ztest_truncate(zd, object, offset, blocksize);
1997 case ZTEST_IO_SETATTR:
1998 (void) ztest_setattr(zd, object);
2002 umem_free(data, blocksize);
2006 * Initialize an object description template.
2009 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2010 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2012 od->od_dir = ZTEST_DIROBJ;
2015 od->od_crtype = type;
2016 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2019 od->od_type = DMU_OT_NONE;
2020 od->od_blocksize = 0;
2023 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2024 tag, (int64_t)id, index);
2028 * Lookup or create the objects for a test using the od template.
2029 * If the objects do not all exist, or if 'remove' is specified,
2030 * remove any existing objects and create new ones. Otherwise,
2031 * use the existing objects.
2034 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2036 int count = size / sizeof (*od);
2039 VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2040 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2041 (ztest_remove(zd, od, count) != 0 ||
2042 ztest_create(zd, od, count) != 0))
2045 VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2052 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2054 zilog_t *zilog = zd->zd_zilog;
2056 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2059 * Remember the committed values in zd, which is in parent/child
2060 * shared memory. If we die, the next iteration of ztest_run()
2061 * will verify that the log really does contain this record.
2063 mutex_enter(&zilog->zl_lock);
2064 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2065 zd->zd_seq = zilog->zl_commit_lr_seq;
2066 mutex_exit(&zilog->zl_lock);
2070 * Verify that we can't destroy an active pool, create an existing pool,
2071 * or create a pool with a bad vdev spec.
2075 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2077 ztest_shared_t *zs = ztest_shared;
2082 * Attempt to create using a bad file.
2084 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2085 VERIFY3U(ENOENT, ==,
2086 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2087 nvlist_free(nvroot);
2090 * Attempt to create using a bad mirror.
2092 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2093 VERIFY3U(ENOENT, ==,
2094 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2095 nvlist_free(nvroot);
2098 * Attempt to create an existing pool. It shouldn't matter
2099 * what's in the nvroot; we should fail with EEXIST.
2101 (void) rw_rdlock(&zs->zs_name_lock);
2102 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2103 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2104 nvlist_free(nvroot);
2105 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2106 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2107 spa_close(spa, FTAG);
2109 (void) rw_unlock(&zs->zs_name_lock);
2113 vdev_lookup_by_path(vdev_t *vd, const char *path)
2117 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2120 for (int c = 0; c < vd->vdev_children; c++)
2121 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2129 * Find the first available hole which can be used as a top-level.
2132 find_vdev_hole(spa_t *spa)
2134 vdev_t *rvd = spa->spa_root_vdev;
2137 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2139 for (c = 0; c < rvd->vdev_children; c++) {
2140 vdev_t *cvd = rvd->vdev_child[c];
2142 if (cvd->vdev_ishole)
2149 * Verify that vdev_add() works as expected.
2153 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2155 ztest_shared_t *zs = ztest_shared;
2156 spa_t *spa = zs->zs_spa;
2162 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2163 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2165 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2167 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2170 * If we have slogs then remove them 1/4 of the time.
2172 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2174 * Grab the guid from the head of the log class rotor.
2176 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2178 spa_config_exit(spa, SCL_VDEV, FTAG);
2181 * We have to grab the zs_name_lock as writer to
2182 * prevent a race between removing a slog (dmu_objset_find)
2183 * and destroying a dataset. Removing the slog will
2184 * grab a reference on the dataset which may cause
2185 * dmu_objset_destroy() to fail with EBUSY thus
2186 * leaving the dataset in an inconsistent state.
2188 VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
2189 error = spa_vdev_remove(spa, guid, B_FALSE);
2190 VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
2192 if (error && error != EEXIST)
2193 fatal(0, "spa_vdev_remove() = %d", error);
2195 spa_config_exit(spa, SCL_VDEV, FTAG);
2198 * Make 1/4 of the devices be log devices.
2200 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2201 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2203 error = spa_vdev_add(spa, nvroot);
2204 nvlist_free(nvroot);
2206 if (error == ENOSPC)
2207 ztest_record_enospc("spa_vdev_add");
2208 else if (error != 0)
2209 fatal(0, "spa_vdev_add() = %d", error);
2212 VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
2216 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2220 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2222 ztest_shared_t *zs = ztest_shared;
2223 spa_t *spa = zs->zs_spa;
2224 vdev_t *rvd = spa->spa_root_vdev;
2225 spa_aux_vdev_t *sav;
2230 if (ztest_random(2) == 0) {
2231 sav = &spa->spa_spares;
2232 aux = ZPOOL_CONFIG_SPARES;
2234 sav = &spa->spa_l2cache;
2235 aux = ZPOOL_CONFIG_L2CACHE;
2238 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2240 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2242 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2244 * Pick a random device to remove.
2246 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2249 * Find an unused device we can add.
2251 zs->zs_vdev_aux = 0;
2253 char path[MAXPATHLEN];
2255 (void) sprintf(path, ztest_aux_template, zopt_dir,
2256 zopt_pool, aux, zs->zs_vdev_aux);
2257 for (c = 0; c < sav->sav_count; c++)
2258 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2261 if (c == sav->sav_count &&
2262 vdev_lookup_by_path(rvd, path) == NULL)
2268 spa_config_exit(spa, SCL_VDEV, FTAG);
2274 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2275 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2276 error = spa_vdev_add(spa, nvroot);
2278 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2279 nvlist_free(nvroot);
2282 * Remove an existing device. Sometimes, dirty its
2283 * vdev state first to make sure we handle removal
2284 * of devices that have pending state changes.
2286 if (ztest_random(2) == 0)
2287 (void) vdev_online(spa, guid, 0, NULL);
2289 error = spa_vdev_remove(spa, guid, B_FALSE);
2290 if (error != 0 && error != EBUSY)
2291 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2294 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2298 * split a pool if it has mirror tlvdevs
2302 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2304 ztest_shared_t *zs = ztest_shared;
2305 spa_t *spa = zs->zs_spa;
2306 vdev_t *rvd = spa->spa_root_vdev;
2307 nvlist_t *tree, **child, *config, *split, **schild;
2308 uint_t c, children, schildren = 0, lastlogid = 0;
2311 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2313 /* ensure we have a useable config; mirrors of raidz aren't supported */
2314 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2315 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2319 /* clean up the old pool, if any */
2320 (void) spa_destroy("splitp");
2322 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2324 /* generate a config from the existing config */
2325 mutex_enter(&spa->spa_props_lock);
2326 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2328 mutex_exit(&spa->spa_props_lock);
2330 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2333 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2334 for (c = 0; c < children; c++) {
2335 vdev_t *tvd = rvd->vdev_child[c];
2339 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2340 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2342 VERIFY(nvlist_add_string(schild[schildren],
2343 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2344 VERIFY(nvlist_add_uint64(schild[schildren],
2345 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2347 lastlogid = schildren;
2352 VERIFY(nvlist_lookup_nvlist_array(child[c],
2353 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2354 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2357 /* OK, create a config that can be used to split */
2358 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2359 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2360 VDEV_TYPE_ROOT) == 0);
2361 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2362 lastlogid != 0 ? lastlogid : schildren) == 0);
2364 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2365 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2367 for (c = 0; c < schildren; c++)
2368 nvlist_free(schild[c]);
2372 spa_config_exit(spa, SCL_VDEV, FTAG);
2374 (void) rw_wrlock(&zs->zs_name_lock);
2375 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2376 (void) rw_unlock(&zs->zs_name_lock);
2378 nvlist_free(config);
2381 (void) printf("successful split - results:\n");
2382 mutex_enter(&spa_namespace_lock);
2383 show_pool_stats(spa);
2384 show_pool_stats(spa_lookup("splitp"));
2385 mutex_exit(&spa_namespace_lock);
2389 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2394 * Verify that we can attach and detach devices.
2398 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2400 ztest_shared_t *zs = ztest_shared;
2401 spa_t *spa = zs->zs_spa;
2402 spa_aux_vdev_t *sav = &spa->spa_spares;
2403 vdev_t *rvd = spa->spa_root_vdev;
2404 vdev_t *oldvd, *newvd, *pvd;
2408 uint64_t ashift = ztest_get_ashift();
2409 uint64_t oldguid, pguid;
2410 size_t oldsize, newsize;
2411 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2413 int oldvd_has_siblings = B_FALSE;
2414 int newvd_is_spare = B_FALSE;
2416 int error, expected_error;
2418 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2419 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2421 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2424 * Decide whether to do an attach or a replace.
2426 replacing = ztest_random(2);
2429 * Pick a random top-level vdev.
2431 top = ztest_random_vdev_top(spa, B_TRUE);
2434 * Pick a random leaf within it.
2436 leaf = ztest_random(leaves);
2441 oldvd = rvd->vdev_child[top];
2442 if (zs->zs_mirrors >= 1) {
2443 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2444 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2445 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2447 if (zopt_raidz > 1) {
2448 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2449 ASSERT(oldvd->vdev_children == zopt_raidz);
2450 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2454 * If we're already doing an attach or replace, oldvd may be a
2455 * mirror vdev -- in which case, pick a random child.
2457 while (oldvd->vdev_children != 0) {
2458 oldvd_has_siblings = B_TRUE;
2459 ASSERT(oldvd->vdev_children >= 2);
2460 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2463 oldguid = oldvd->vdev_guid;
2464 oldsize = vdev_get_min_asize(oldvd);
2465 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2466 (void) strcpy(oldpath, oldvd->vdev_path);
2467 pvd = oldvd->vdev_parent;
2468 pguid = pvd->vdev_guid;
2471 * If oldvd has siblings, then half of the time, detach it.
2473 if (oldvd_has_siblings && ztest_random(2) == 0) {
2474 spa_config_exit(spa, SCL_VDEV, FTAG);
2475 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2476 if (error != 0 && error != ENODEV && error != EBUSY &&
2478 fatal(0, "detach (%s) returned %d", oldpath, error);
2479 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2484 * For the new vdev, choose with equal probability between the two
2485 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2487 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2488 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2489 newvd_is_spare = B_TRUE;
2490 (void) strcpy(newpath, newvd->vdev_path);
2492 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2493 zopt_dir, zopt_pool, top * leaves + leaf);
2494 if (ztest_random(2) == 0)
2495 newpath[strlen(newpath) - 1] = 'b';
2496 newvd = vdev_lookup_by_path(rvd, newpath);
2500 newsize = vdev_get_min_asize(newvd);
2503 * Make newsize a little bigger or smaller than oldsize.
2504 * If it's smaller, the attach should fail.
2505 * If it's larger, and we're doing a replace,
2506 * we should get dynamic LUN growth when we're done.
2508 newsize = 10 * oldsize / (9 + ztest_random(3));
2512 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2513 * unless it's a replace; in that case any non-replacing parent is OK.
2515 * If newvd is already part of the pool, it should fail with EBUSY.
2517 * If newvd is too small, it should fail with EOVERFLOW.
2519 if (pvd->vdev_ops != &vdev_mirror_ops &&
2520 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2521 pvd->vdev_ops == &vdev_replacing_ops ||
2522 pvd->vdev_ops == &vdev_spare_ops))
2523 expected_error = ENOTSUP;
2524 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2525 expected_error = ENOTSUP;
2526 else if (newvd == oldvd)
2527 expected_error = replacing ? 0 : EBUSY;
2528 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2529 expected_error = EBUSY;
2530 else if (newsize < oldsize)
2531 expected_error = EOVERFLOW;
2532 else if (ashift > oldvd->vdev_top->vdev_ashift)
2533 expected_error = EDOM;
2537 spa_config_exit(spa, SCL_VDEV, FTAG);
2540 * Build the nvlist describing newpath.
2542 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2543 ashift, 0, 0, 0, 1);
2545 error = spa_vdev_attach(spa, oldguid, root, replacing);
2550 * If our parent was the replacing vdev, but the replace completed,
2551 * then instead of failing with ENOTSUP we may either succeed,
2552 * fail with ENODEV, or fail with EOVERFLOW.
2554 if (expected_error == ENOTSUP &&
2555 (error == 0 || error == ENODEV || error == EOVERFLOW))
2556 expected_error = error;
2559 * If someone grew the LUN, the replacement may be too small.
2561 if (error == EOVERFLOW || error == EBUSY)
2562 expected_error = error;
2564 /* XXX workaround 6690467 */
2565 if (error != expected_error && expected_error != EBUSY) {
2566 fatal(0, "attach (%s %llu, %s %llu, %d) "
2567 "returned %d, expected %d",
2568 oldpath, (longlong_t)oldsize, newpath,
2569 (longlong_t)newsize, replacing, error, expected_error);
2572 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2576 * Callback function which expands the physical size of the vdev.
2579 grow_vdev(vdev_t *vd, void *arg)
2581 spa_t *spa = vd->vdev_spa;
2582 size_t *newsize = arg;
2586 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2587 ASSERT(vd->vdev_ops->vdev_op_leaf);
2589 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2592 fsize = lseek(fd, 0, SEEK_END);
2593 (void) ftruncate(fd, *newsize);
2595 if (zopt_verbose >= 6) {
2596 (void) printf("%s grew from %lu to %lu bytes\n",
2597 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2604 * Callback function which expands a given vdev by calling vdev_online().
2608 online_vdev(vdev_t *vd, void *arg)
2610 spa_t *spa = vd->vdev_spa;
2611 vdev_t *tvd = vd->vdev_top;
2612 uint64_t guid = vd->vdev_guid;
2613 uint64_t generation = spa->spa_config_generation + 1;
2614 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2617 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2618 ASSERT(vd->vdev_ops->vdev_op_leaf);
2620 /* Calling vdev_online will initialize the new metaslabs */
2621 spa_config_exit(spa, SCL_STATE, spa);
2622 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2623 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2626 * If vdev_online returned an error or the underlying vdev_open
2627 * failed then we abort the expand. The only way to know that
2628 * vdev_open fails is by checking the returned newstate.
2630 if (error || newstate != VDEV_STATE_HEALTHY) {
2631 if (zopt_verbose >= 5) {
2632 (void) printf("Unable to expand vdev, state %llu, "
2633 "error %d\n", (u_longlong_t)newstate, error);
2637 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2640 * Since we dropped the lock we need to ensure that we're
2641 * still talking to the original vdev. It's possible this
2642 * vdev may have been detached/replaced while we were
2643 * trying to online it.
2645 if (generation != spa->spa_config_generation) {
2646 if (zopt_verbose >= 5) {
2647 (void) printf("vdev configuration has changed, "
2648 "guid %llu, state %llu, expected gen %llu, "
2651 (u_longlong_t)tvd->vdev_state,
2652 (u_longlong_t)generation,
2653 (u_longlong_t)spa->spa_config_generation);
2661 * Traverse the vdev tree calling the supplied function.
2662 * We continue to walk the tree until we either have walked all
2663 * children or we receive a non-NULL return from the callback.
2664 * If a NULL callback is passed, then we just return back the first
2665 * leaf vdev we encounter.
2668 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2670 if (vd->vdev_ops->vdev_op_leaf) {
2674 return (func(vd, arg));
2677 for (uint_t c = 0; c < vd->vdev_children; c++) {
2678 vdev_t *cvd = vd->vdev_child[c];
2679 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2686 * Verify that dynamic LUN growth works as expected.
2690 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2692 ztest_shared_t *zs = ztest_shared;
2693 spa_t *spa = zs->zs_spa;
2695 metaslab_class_t *mc;
2696 metaslab_group_t *mg;
2697 size_t psize, newsize;
2699 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2701 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2702 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2704 top = ztest_random_vdev_top(spa, B_TRUE);
2706 tvd = spa->spa_root_vdev->vdev_child[top];
2709 old_ms_count = tvd->vdev_ms_count;
2710 old_class_space = metaslab_class_get_space(mc);
2713 * Determine the size of the first leaf vdev associated with
2714 * our top-level device.
2716 vd = vdev_walk_tree(tvd, NULL, NULL);
2717 ASSERT3P(vd, !=, NULL);
2718 ASSERT(vd->vdev_ops->vdev_op_leaf);
2720 psize = vd->vdev_psize;
2723 * We only try to expand the vdev if it's healthy, less than 4x its
2724 * original size, and it has a valid psize.
2726 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2727 psize == 0 || psize >= 4 * zopt_vdev_size) {
2728 spa_config_exit(spa, SCL_STATE, spa);
2729 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2733 newsize = psize + psize / 8;
2734 ASSERT3U(newsize, >, psize);
2736 if (zopt_verbose >= 6) {
2737 (void) printf("Expanding LUN %s from %lu to %lu\n",
2738 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2742 * Growing the vdev is a two step process:
2743 * 1). expand the physical size (i.e. relabel)
2744 * 2). online the vdev to create the new metaslabs
2746 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2747 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2748 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2749 if (zopt_verbose >= 5) {
2750 (void) printf("Could not expand LUN because "
2751 "the vdev configuration changed.\n");
2753 spa_config_exit(spa, SCL_STATE, spa);
2754 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2758 spa_config_exit(spa, SCL_STATE, spa);
2761 * Expanding the LUN will update the config asynchronously,
2762 * thus we must wait for the async thread to complete any
2763 * pending tasks before proceeding.
2767 mutex_enter(&spa->spa_async_lock);
2768 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2769 mutex_exit(&spa->spa_async_lock);
2772 txg_wait_synced(spa_get_dsl(spa), 0);
2773 (void) poll(NULL, 0, 100);
2776 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2778 tvd = spa->spa_root_vdev->vdev_child[top];
2779 new_ms_count = tvd->vdev_ms_count;
2780 new_class_space = metaslab_class_get_space(mc);
2782 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2783 if (zopt_verbose >= 5) {
2784 (void) printf("Could not verify LUN expansion due to "
2785 "intervening vdev offline or remove.\n");
2787 spa_config_exit(spa, SCL_STATE, spa);
2788 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2793 * Make sure we were able to grow the vdev.
2795 if (new_ms_count <= old_ms_count)
2796 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2797 old_ms_count, new_ms_count);
2800 * Make sure we were able to grow the pool.
2802 if (new_class_space <= old_class_space)
2803 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2804 old_class_space, new_class_space);
2806 if (zopt_verbose >= 5) {
2807 char oldnumbuf[6], newnumbuf[6];
2809 nicenum(old_class_space, oldnumbuf);
2810 nicenum(new_class_space, newnumbuf);
2811 (void) printf("%s grew from %s to %s\n",
2812 spa->spa_name, oldnumbuf, newnumbuf);
2815 spa_config_exit(spa, SCL_STATE, spa);
2816 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2820 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2824 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2827 * Create the objects common to all ztest datasets.
2829 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2830 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2834 ztest_dataset_create(char *dsname)
2836 uint64_t zilset = ztest_random(100);
2837 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2838 ztest_objset_create_cb, NULL);
2840 if (err || zilset < 80)
2843 (void) printf("Setting dataset %s to sync always\n", dsname);
2844 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2845 ZFS_SYNC_ALWAYS, B_FALSE));
2850 ztest_objset_destroy_cb(const char *name, void *arg)
2853 dmu_object_info_t doi;
2857 * Verify that the dataset contains a directory object.
2859 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2860 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2861 if (error != ENOENT) {
2862 /* We could have crashed in the middle of destroying it */
2863 ASSERT3U(error, ==, 0);
2864 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2865 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2867 dmu_objset_rele(os, FTAG);
2870 * Destroy the dataset.
2872 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2877 ztest_snapshot_create(char *osname, uint64_t id)
2879 char snapname[MAXNAMELEN];
2882 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2885 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2886 NULL, NULL, B_FALSE, B_FALSE, -1);
2887 if (error == ENOSPC) {
2888 ztest_record_enospc(FTAG);
2891 if (error != 0 && error != EEXIST)
2892 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2897 ztest_snapshot_destroy(char *osname, uint64_t id)
2899 char snapname[MAXNAMELEN];
2902 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2905 error = dmu_objset_destroy(snapname, B_FALSE);
2906 if (error != 0 && error != ENOENT)
2907 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2913 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2915 ztest_shared_t *zs = ztest_shared;
2920 char name[MAXNAMELEN];
2923 (void) rw_rdlock(&zs->zs_name_lock);
2925 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2926 zs->zs_pool, (u_longlong_t)id);
2929 * If this dataset exists from a previous run, process its replay log
2930 * half of the time. If we don't replay it, then dmu_objset_destroy()
2931 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2933 if (ztest_random(2) == 0 &&
2934 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2935 ztest_zd_init(&zdtmp, os);
2936 zil_replay(os, &zdtmp, ztest_replay_vector);
2937 ztest_zd_fini(&zdtmp);
2938 dmu_objset_disown(os, FTAG);
2942 * There may be an old instance of the dataset we're about to
2943 * create lying around from a previous run. If so, destroy it
2944 * and all of its snapshots.
2946 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
2947 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2950 * Verify that the destroyed dataset is no longer in the namespace.
2952 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2955 * Verify that we can create a new dataset.
2957 error = ztest_dataset_create(name);
2959 if (error == ENOSPC) {
2960 ztest_record_enospc(FTAG);
2961 (void) rw_unlock(&zs->zs_name_lock);
2964 fatal(0, "dmu_objset_create(%s) = %d", name, error);
2968 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
2970 ztest_zd_init(&zdtmp, os);
2973 * Open the intent log for it.
2975 zilog = zil_open(os, ztest_get_data);
2978 * Put some objects in there, do a little I/O to them,
2979 * and randomly take a couple of snapshots along the way.
2981 iters = ztest_random(5);
2982 for (int i = 0; i < iters; i++) {
2983 ztest_dmu_object_alloc_free(&zdtmp, id);
2984 if (ztest_random(iters) == 0)
2985 (void) ztest_snapshot_create(name, i);
2989 * Verify that we cannot create an existing dataset.
2991 VERIFY3U(EEXIST, ==,
2992 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
2995 * Verify that we can hold an objset that is also owned.
2997 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
2998 dmu_objset_rele(os2, FTAG);
3001 * Verify that we cannot own an objset that is already owned.
3004 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3007 dmu_objset_disown(os, FTAG);
3008 ztest_zd_fini(&zdtmp);
3010 (void) rw_unlock(&zs->zs_name_lock);
3014 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3017 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3019 ztest_shared_t *zs = ztest_shared;
3021 (void) rw_rdlock(&zs->zs_name_lock);
3022 (void) ztest_snapshot_destroy(zd->zd_name, id);
3023 (void) ztest_snapshot_create(zd->zd_name, id);
3024 (void) rw_unlock(&zs->zs_name_lock);
3028 * Cleanup non-standard snapshots and clones.
3031 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3033 char snap1name[MAXNAMELEN];
3034 char clone1name[MAXNAMELEN];
3035 char snap2name[MAXNAMELEN];
3036 char clone2name[MAXNAMELEN];
3037 char snap3name[MAXNAMELEN];
3040 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3041 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3042 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3043 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3044 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
3046 error = dmu_objset_destroy(clone2name, B_FALSE);
3047 if (error && error != ENOENT)
3048 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3049 error = dmu_objset_destroy(snap3name, B_FALSE);
3050 if (error && error != ENOENT)
3051 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3052 error = dmu_objset_destroy(snap2name, B_FALSE);
3053 if (error && error != ENOENT)
3054 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3055 error = dmu_objset_destroy(clone1name, B_FALSE);
3056 if (error && error != ENOENT)
3057 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3058 error = dmu_objset_destroy(snap1name, B_FALSE);
3059 if (error && error != ENOENT)
3060 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3064 * Verify dsl_dataset_promote handles EBUSY
3067 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3069 ztest_shared_t *zs = ztest_shared;
3072 char snap1name[MAXNAMELEN];
3073 char clone1name[MAXNAMELEN];
3074 char snap2name[MAXNAMELEN];
3075 char clone2name[MAXNAMELEN];
3076 char snap3name[MAXNAMELEN];
3077 char *osname = zd->zd_name;
3080 (void) rw_rdlock(&zs->zs_name_lock);
3082 ztest_dsl_dataset_cleanup(osname, id);
3084 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu", osname, id);
3085 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu", osname, id);
3086 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu", clone1name, id);
3087 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu", osname, id);
3088 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu", clone1name, id);
3090 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3091 NULL, NULL, B_FALSE, B_FALSE, -1);
3092 if (error && error != EEXIST) {
3093 if (error == ENOSPC) {
3094 ztest_record_enospc(FTAG);
3097 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3100 error = dmu_objset_hold(snap1name, FTAG, &clone);
3102 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3104 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3105 dmu_objset_rele(clone, FTAG);
3107 if (error == ENOSPC) {
3108 ztest_record_enospc(FTAG);
3111 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3114 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3115 NULL, NULL, B_FALSE, B_FALSE, -1);
3116 if (error && error != EEXIST) {
3117 if (error == ENOSPC) {
3118 ztest_record_enospc(FTAG);
3121 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3124 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3125 NULL, NULL, B_FALSE, B_FALSE, -1);
3126 if (error && error != EEXIST) {
3127 if (error == ENOSPC) {
3128 ztest_record_enospc(FTAG);
3131 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3134 error = dmu_objset_hold(snap3name, FTAG, &clone);
3136 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3138 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3139 dmu_objset_rele(clone, FTAG);
3141 if (error == ENOSPC) {
3142 ztest_record_enospc(FTAG);
3145 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3148 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3150 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3151 error = dsl_dataset_promote(clone2name, NULL);
3153 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3155 dsl_dataset_disown(ds, FTAG);
3158 ztest_dsl_dataset_cleanup(osname, id);
3160 (void) rw_unlock(&zs->zs_name_lock);
3164 * Verify that dmu_object_{alloc,free} work as expected.
3167 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3170 int batchsize = sizeof (od) / sizeof (od[0]);
3172 for (int b = 0; b < batchsize; b++)
3173 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3176 * Destroy the previous batch of objects, create a new batch,
3177 * and do some I/O on the new objects.
3179 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3182 while (ztest_random(4 * batchsize) != 0)
3183 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3184 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3188 * Verify that dmu_{read,write} work as expected.
3191 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3193 objset_t *os = zd->zd_os;
3196 int i, freeit, error;
3198 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3199 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3200 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3201 uint64_t regions = 997;
3202 uint64_t stride = 123456789ULL;
3203 uint64_t width = 40;
3204 int free_percent = 5;
3207 * This test uses two objects, packobj and bigobj, that are always
3208 * updated together (i.e. in the same tx) so that their contents are
3209 * in sync and can be compared. Their contents relate to each other
3210 * in a simple way: packobj is a dense array of 'bufwad' structures,
3211 * while bigobj is a sparse array of the same bufwads. Specifically,
3212 * for any index n, there are three bufwads that should be identical:
3214 * packobj, at offset n * sizeof (bufwad_t)
3215 * bigobj, at the head of the nth chunk
3216 * bigobj, at the tail of the nth chunk
3218 * The chunk size is arbitrary. It doesn't have to be a power of two,
3219 * and it doesn't have any relation to the object blocksize.
3220 * The only requirement is that it can hold at least two bufwads.
3222 * Normally, we write the bufwad to each of these locations.
3223 * However, free_percent of the time we instead write zeroes to
3224 * packobj and perform a dmu_free_range() on bigobj. By comparing
3225 * bigobj to packobj, we can verify that the DMU is correctly
3226 * tracking which parts of an object are allocated and free,
3227 * and that the contents of the allocated blocks are correct.
3231 * Read the directory info. If it's the first time, set things up.
3233 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3234 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3236 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3239 bigobj = od[0].od_object;
3240 packobj = od[1].od_object;
3241 chunksize = od[0].od_gen;
3242 ASSERT(chunksize == od[1].od_gen);
3245 * Prefetch a random chunk of the big object.
3246 * Our aim here is to get some async reads in flight
3247 * for blocks that we may free below; the DMU should
3248 * handle this race correctly.
3250 n = ztest_random(regions) * stride + ztest_random(width);
3251 s = 1 + ztest_random(2 * width - 1);
3252 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3255 * Pick a random index and compute the offsets into packobj and bigobj.
3257 n = ztest_random(regions) * stride + ztest_random(width);
3258 s = 1 + ztest_random(width - 1);
3260 packoff = n * sizeof (bufwad_t);
3261 packsize = s * sizeof (bufwad_t);
3263 bigoff = n * chunksize;
3264 bigsize = s * chunksize;
3266 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3267 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3270 * free_percent of the time, free a range of bigobj rather than
3273 freeit = (ztest_random(100) < free_percent);
3276 * Read the current contents of our objects.
3278 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3280 ASSERT3U(error, ==, 0);
3281 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3283 ASSERT3U(error, ==, 0);
3286 * Get a tx for the mods to both packobj and bigobj.
3288 tx = dmu_tx_create(os);
3290 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3293 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3295 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3297 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3299 umem_free(packbuf, packsize);
3300 umem_free(bigbuf, bigsize);
3304 dmu_object_set_checksum(os, bigobj,
3305 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3307 dmu_object_set_compress(os, bigobj,
3308 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3311 * For each index from n to n + s, verify that the existing bufwad
3312 * in packobj matches the bufwads at the head and tail of the
3313 * corresponding chunk in bigobj. Then update all three bufwads
3314 * with the new values we want to write out.
3316 for (i = 0; i < s; i++) {
3318 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3320 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3322 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3324 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3325 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3327 if (pack->bw_txg > txg)
3328 fatal(0, "future leak: got %llx, open txg is %llx",
3331 if (pack->bw_data != 0 && pack->bw_index != n + i)
3332 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3333 pack->bw_index, n, i);
3335 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3336 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3338 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3339 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3342 bzero(pack, sizeof (bufwad_t));
3344 pack->bw_index = n + i;
3346 pack->bw_data = 1 + ztest_random(-2ULL);
3353 * We've verified all the old bufwads, and made new ones.
3354 * Now write them out.
3356 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3359 if (zopt_verbose >= 7) {
3360 (void) printf("freeing offset %llx size %llx"
3362 (u_longlong_t)bigoff,
3363 (u_longlong_t)bigsize,
3366 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3368 if (zopt_verbose >= 7) {
3369 (void) printf("writing offset %llx size %llx"
3371 (u_longlong_t)bigoff,
3372 (u_longlong_t)bigsize,
3375 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3381 * Sanity check the stuff we just wrote.
3384 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3385 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3387 VERIFY(0 == dmu_read(os, packobj, packoff,
3388 packsize, packcheck, DMU_READ_PREFETCH));
3389 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3390 bigsize, bigcheck, DMU_READ_PREFETCH));
3392 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3393 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3395 umem_free(packcheck, packsize);
3396 umem_free(bigcheck, bigsize);
3399 umem_free(packbuf, packsize);
3400 umem_free(bigbuf, bigsize);
3404 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3405 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3413 * For each index from n to n + s, verify that the existing bufwad
3414 * in packobj matches the bufwads at the head and tail of the
3415 * corresponding chunk in bigobj. Then update all three bufwads
3416 * with the new values we want to write out.
3418 for (i = 0; i < s; i++) {
3420 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3422 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3424 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3426 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3427 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3429 if (pack->bw_txg > txg)
3430 fatal(0, "future leak: got %llx, open txg is %llx",
3433 if (pack->bw_data != 0 && pack->bw_index != n + i)
3434 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3435 pack->bw_index, n, i);
3437 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3438 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3440 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3441 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3443 pack->bw_index = n + i;
3445 pack->bw_data = 1 + ztest_random(-2ULL);
3453 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3455 objset_t *os = zd->zd_os;
3461 bufwad_t *packbuf, *bigbuf;
3462 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3463 uint64_t blocksize = ztest_random_blocksize();
3464 uint64_t chunksize = blocksize;
3465 uint64_t regions = 997;
3466 uint64_t stride = 123456789ULL;
3468 dmu_buf_t *bonus_db;
3469 arc_buf_t **bigbuf_arcbufs;
3470 dmu_object_info_t doi;
3473 * This test uses two objects, packobj and bigobj, that are always
3474 * updated together (i.e. in the same tx) so that their contents are
3475 * in sync and can be compared. Their contents relate to each other
3476 * in a simple way: packobj is a dense array of 'bufwad' structures,
3477 * while bigobj is a sparse array of the same bufwads. Specifically,
3478 * for any index n, there are three bufwads that should be identical:
3480 * packobj, at offset n * sizeof (bufwad_t)
3481 * bigobj, at the head of the nth chunk
3482 * bigobj, at the tail of the nth chunk
3484 * The chunk size is set equal to bigobj block size so that
3485 * dmu_assign_arcbuf() can be tested for object updates.
3489 * Read the directory info. If it's the first time, set things up.
3491 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3492 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3494 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3497 bigobj = od[0].od_object;
3498 packobj = od[1].od_object;
3499 blocksize = od[0].od_blocksize;
3500 chunksize = blocksize;
3501 ASSERT(chunksize == od[1].od_gen);
3503 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3504 VERIFY(ISP2(doi.doi_data_block_size));
3505 VERIFY(chunksize == doi.doi_data_block_size);
3506 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3509 * Pick a random index and compute the offsets into packobj and bigobj.
3511 n = ztest_random(regions) * stride + ztest_random(width);
3512 s = 1 + ztest_random(width - 1);
3514 packoff = n * sizeof (bufwad_t);
3515 packsize = s * sizeof (bufwad_t);
3517 bigoff = n * chunksize;
3518 bigsize = s * chunksize;
3520 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3521 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3523 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3525 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3528 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3529 * Iteration 1 test zcopy to already referenced dbufs.
3530 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3531 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3532 * Iteration 4 test zcopy when dbuf is no longer dirty.
3533 * Iteration 5 test zcopy when it can't be done.
3534 * Iteration 6 one more zcopy write.
3536 for (i = 0; i < 7; i++) {
3541 * In iteration 5 (i == 5) use arcbufs
3542 * that don't match bigobj blksz to test
3543 * dmu_assign_arcbuf() when it can't directly
3544 * assign an arcbuf to a dbuf.
3546 for (j = 0; j < s; j++) {
3549 dmu_request_arcbuf(bonus_db, chunksize);
3551 bigbuf_arcbufs[2 * j] =
3552 dmu_request_arcbuf(bonus_db, chunksize / 2);
3553 bigbuf_arcbufs[2 * j + 1] =
3554 dmu_request_arcbuf(bonus_db, chunksize / 2);
3559 * Get a tx for the mods to both packobj and bigobj.
3561 tx = dmu_tx_create(os);
3563 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3564 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3566 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3568 umem_free(packbuf, packsize);
3569 umem_free(bigbuf, bigsize);
3570 for (j = 0; j < s; j++) {
3572 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3575 bigbuf_arcbufs[2 * j]);
3577 bigbuf_arcbufs[2 * j + 1]);
3580 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3581 dmu_buf_rele(bonus_db, FTAG);
3586 * 50% of the time don't read objects in the 1st iteration to
3587 * test dmu_assign_arcbuf() for the case when there're no
3588 * existing dbufs for the specified offsets.
3590 if (i != 0 || ztest_random(2) != 0) {
3591 error = dmu_read(os, packobj, packoff,
3592 packsize, packbuf, DMU_READ_PREFETCH);
3593 ASSERT3U(error, ==, 0);
3594 error = dmu_read(os, bigobj, bigoff, bigsize,
3595 bigbuf, DMU_READ_PREFETCH);
3596 ASSERT3U(error, ==, 0);
3598 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3602 * We've verified all the old bufwads, and made new ones.
3603 * Now write them out.
3605 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3606 if (zopt_verbose >= 7) {
3607 (void) printf("writing offset %llx size %llx"
3609 (u_longlong_t)bigoff,
3610 (u_longlong_t)bigsize,
3613 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3616 bcopy((caddr_t)bigbuf + (off - bigoff),
3617 bigbuf_arcbufs[j]->b_data, chunksize);
3619 bcopy((caddr_t)bigbuf + (off - bigoff),
3620 bigbuf_arcbufs[2 * j]->b_data,
3622 bcopy((caddr_t)bigbuf + (off - bigoff) +
3624 bigbuf_arcbufs[2 * j + 1]->b_data,
3629 VERIFY(dmu_buf_hold(os, bigobj, off,
3630 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3633 dmu_assign_arcbuf(bonus_db, off,
3634 bigbuf_arcbufs[j], tx);
3636 dmu_assign_arcbuf(bonus_db, off,
3637 bigbuf_arcbufs[2 * j], tx);
3638 dmu_assign_arcbuf(bonus_db,
3639 off + chunksize / 2,
3640 bigbuf_arcbufs[2 * j + 1], tx);
3643 dmu_buf_rele(dbt, FTAG);
3649 * Sanity check the stuff we just wrote.
3652 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3653 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3655 VERIFY(0 == dmu_read(os, packobj, packoff,
3656 packsize, packcheck, DMU_READ_PREFETCH));
3657 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3658 bigsize, bigcheck, DMU_READ_PREFETCH));
3660 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3661 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3663 umem_free(packcheck, packsize);
3664 umem_free(bigcheck, bigsize);
3667 txg_wait_open(dmu_objset_pool(os), 0);
3668 } else if (i == 3) {
3669 txg_wait_synced(dmu_objset_pool(os), 0);
3673 dmu_buf_rele(bonus_db, FTAG);
3674 umem_free(packbuf, packsize);
3675 umem_free(bigbuf, bigsize);
3676 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3681 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3684 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3685 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3688 * Have multiple threads write to large offsets in an object
3689 * to verify that parallel writes to an object -- even to the
3690 * same blocks within the object -- doesn't cause any trouble.
3692 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3694 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3697 while (ztest_random(10) != 0)
3698 ztest_io(zd, od[0].od_object, offset);
3702 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3705 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3706 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3707 uint64_t count = ztest_random(20) + 1;
3708 uint64_t blocksize = ztest_random_blocksize();
3711 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3713 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3716 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
3719 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
3721 data = umem_zalloc(blocksize, UMEM_NOFAIL);
3723 while (ztest_random(count) != 0) {
3724 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3725 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3728 while (ztest_random(4) != 0)
3729 ztest_io(zd, od[0].od_object, randoff);
3732 umem_free(data, blocksize);
3736 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3738 #define ZTEST_ZAP_MIN_INTS 1
3739 #define ZTEST_ZAP_MAX_INTS 4
3740 #define ZTEST_ZAP_MAX_PROPS 1000
3743 ztest_zap(ztest_ds_t *zd, uint64_t id)
3745 objset_t *os = zd->zd_os;
3748 uint64_t txg, last_txg;
3749 uint64_t value[ZTEST_ZAP_MAX_INTS];
3750 uint64_t zl_ints, zl_intsize, prop;
3753 char propname[100], txgname[100];
3755 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3757 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3759 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3762 object = od[0].od_object;
3765 * Generate a known hash collision, and verify that
3766 * we can lookup and remove both entries.
3768 tx = dmu_tx_create(os);
3769 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3770 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3773 for (i = 0; i < 2; i++) {
3775 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3778 for (i = 0; i < 2; i++) {
3779 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3780 sizeof (uint64_t), 1, &value[i], tx));
3782 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3783 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3784 ASSERT3U(zl_ints, ==, 1);
3786 for (i = 0; i < 2; i++) {
3787 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3792 * Generate a buch of random entries.
3794 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3796 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3797 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3798 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3799 bzero(value, sizeof (value));
3803 * If these zap entries already exist, validate their contents.
3805 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3807 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3808 ASSERT3U(zl_ints, ==, 1);
3810 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3811 zl_ints, &last_txg) == 0);
3813 VERIFY(zap_length(os, object, propname, &zl_intsize,
3816 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3817 ASSERT3U(zl_ints, ==, ints);
3819 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3820 zl_ints, value) == 0);
3822 for (i = 0; i < ints; i++) {
3823 ASSERT3U(value[i], ==, last_txg + object + i);
3826 ASSERT3U(error, ==, ENOENT);
3830 * Atomically update two entries in our zap object.
3831 * The first is named txg_%llu, and contains the txg
3832 * in which the property was last updated. The second
3833 * is named prop_%llu, and the nth element of its value
3834 * should be txg + object + n.
3836 tx = dmu_tx_create(os);
3837 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3838 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3843 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3845 for (i = 0; i < ints; i++)
3846 value[i] = txg + object + i;
3848 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3850 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3856 * Remove a random pair of entries.
3858 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3859 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3860 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3862 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3864 if (error == ENOENT)
3867 ASSERT3U(error, ==, 0);
3869 tx = dmu_tx_create(os);
3870 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3871 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3874 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3875 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3880 * Testcase to test the upgrading of a microzap to fatzap.
3883 ztest_fzap(ztest_ds_t *zd, uint64_t id)
3885 objset_t *os = zd->zd_os;
3887 uint64_t object, txg;
3889 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3891 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3894 object = od[0].od_object;
3897 * Add entries to this ZAP and make sure it spills over
3898 * and gets upgraded to a fatzap. Also, since we are adding
3899 * 2050 entries we should see ptrtbl growth and leaf-block split.
3901 for (int i = 0; i < 2050; i++) {
3902 char name[MAXNAMELEN];
3907 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
3910 tx = dmu_tx_create(os);
3911 dmu_tx_hold_zap(tx, object, B_TRUE, name);
3912 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3915 error = zap_add(os, object, name, sizeof (uint64_t), 1,
3917 ASSERT(error == 0 || error == EEXIST);
3924 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3926 objset_t *os = zd->zd_os;
3928 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3930 int i, namelen, error;
3931 int micro = ztest_random(2);
3932 char name[20], string_value[20];
3935 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3937 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3940 object = od[0].od_object;
3943 * Generate a random name of the form 'xxx.....' where each
3944 * x is a random printable character and the dots are dots.
3945 * There are 94 such characters, and the name length goes from
3946 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3948 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3950 for (i = 0; i < 3; i++)
3951 name[i] = '!' + ztest_random('~' - '!' + 1);
3952 for (; i < namelen - 1; i++)
3956 if ((namelen & 1) || micro) {
3957 wsize = sizeof (txg);
3963 data = string_value;
3967 VERIFY(zap_count(os, object, &count) == 0);
3968 ASSERT(count != -1ULL);
3971 * Select an operation: length, lookup, add, update, remove.
3973 i = ztest_random(5);
3976 tx = dmu_tx_create(os);
3977 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3978 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3981 bcopy(name, string_value, namelen);
3985 bzero(string_value, namelen);
3991 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
3993 ASSERT3U(wsize, ==, zl_wsize);
3994 ASSERT3U(wc, ==, zl_wc);
3996 ASSERT3U(error, ==, ENOENT);
4001 error = zap_lookup(os, object, name, wsize, wc, data);
4003 if (data == string_value &&
4004 bcmp(name, data, namelen) != 0)
4005 fatal(0, "name '%s' != val '%s' len %d",
4006 name, data, namelen);
4008 ASSERT3U(error, ==, ENOENT);
4013 error = zap_add(os, object, name, wsize, wc, data, tx);
4014 ASSERT(error == 0 || error == EEXIST);
4018 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4022 error = zap_remove(os, object, name, tx);
4023 ASSERT(error == 0 || error == ENOENT);
4032 * Commit callback data.
4034 typedef struct ztest_cb_data {
4035 list_node_t zcd_node;
4037 int zcd_expected_err;
4038 boolean_t zcd_added;
4039 boolean_t zcd_called;
4043 /* This is the actual commit callback function */
4045 ztest_commit_callback(void *arg, int error)
4047 ztest_cb_data_t *data = arg;
4048 uint64_t synced_txg;
4050 VERIFY(data != NULL);
4051 VERIFY3S(data->zcd_expected_err, ==, error);
4052 VERIFY(!data->zcd_called);
4054 synced_txg = spa_last_synced_txg(data->zcd_spa);
4055 if (data->zcd_txg > synced_txg)
4056 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4057 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4060 data->zcd_called = B_TRUE;
4062 if (error == ECANCELED) {
4063 ASSERT3U(data->zcd_txg, ==, 0);
4064 ASSERT(!data->zcd_added);
4067 * The private callback data should be destroyed here, but
4068 * since we are going to check the zcd_called field after
4069 * dmu_tx_abort(), we will destroy it there.
4074 /* Was this callback added to the global callback list? */
4075 if (!data->zcd_added)
4078 ASSERT3U(data->zcd_txg, !=, 0);
4080 /* Remove our callback from the list */
4081 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4082 list_remove(&zcl.zcl_callbacks, data);
4083 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4086 umem_free(data, sizeof (ztest_cb_data_t));
4089 /* Allocate and initialize callback data structure */
4090 static ztest_cb_data_t *
4091 ztest_create_cb_data(objset_t *os, uint64_t txg)
4093 ztest_cb_data_t *cb_data;
4095 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4097 cb_data->zcd_txg = txg;
4098 cb_data->zcd_spa = dmu_objset_spa(os);
4104 * If a number of txgs equal to this threshold have been created after a commit
4105 * callback has been registered but not called, then we assume there is an
4106 * implementation bug.
4108 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4111 * Commit callback test.
4114 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4116 objset_t *os = zd->zd_os;
4119 ztest_cb_data_t *cb_data[3], *tmp_cb;
4120 uint64_t old_txg, txg;
4123 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4125 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4128 tx = dmu_tx_create(os);
4130 cb_data[0] = ztest_create_cb_data(os, 0);
4131 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4133 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4135 /* Every once in a while, abort the transaction on purpose */
4136 if (ztest_random(100) == 0)
4140 error = dmu_tx_assign(tx, TXG_NOWAIT);
4142 txg = error ? 0 : dmu_tx_get_txg(tx);
4144 cb_data[0]->zcd_txg = txg;
4145 cb_data[1] = ztest_create_cb_data(os, txg);
4146 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4150 * It's not a strict requirement to call the registered
4151 * callbacks from inside dmu_tx_abort(), but that's what
4152 * it's supposed to happen in the current implementation
4153 * so we will check for that.
4155 for (i = 0; i < 2; i++) {
4156 cb_data[i]->zcd_expected_err = ECANCELED;
4157 VERIFY(!cb_data[i]->zcd_called);
4162 for (i = 0; i < 2; i++) {
4163 VERIFY(cb_data[i]->zcd_called);
4164 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4170 cb_data[2] = ztest_create_cb_data(os, txg);
4171 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4174 * Read existing data to make sure there isn't a future leak.
4176 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4177 &old_txg, DMU_READ_PREFETCH));
4180 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4183 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4185 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4188 * Since commit callbacks don't have any ordering requirement and since
4189 * it is theoretically possible for a commit callback to be called
4190 * after an arbitrary amount of time has elapsed since its txg has been
4191 * synced, it is difficult to reliably determine whether a commit
4192 * callback hasn't been called due to high load or due to a flawed
4195 * In practice, we will assume that if after a certain number of txgs a
4196 * commit callback hasn't been called, then most likely there's an
4197 * implementation bug..
4199 tmp_cb = list_head(&zcl.zcl_callbacks);
4200 if (tmp_cb != NULL &&
4201 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
4202 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4203 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4207 * Let's find the place to insert our callbacks.
4209 * Even though the list is ordered by txg, it is possible for the
4210 * insertion point to not be the end because our txg may already be
4211 * quiescing at this point and other callbacks in the open txg
4212 * (from other objsets) may have sneaked in.
4214 tmp_cb = list_tail(&zcl.zcl_callbacks);
4215 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4216 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4218 /* Add the 3 callbacks to the list */
4219 for (i = 0; i < 3; i++) {
4221 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4223 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4226 cb_data[i]->zcd_added = B_TRUE;
4227 VERIFY(!cb_data[i]->zcd_called);
4229 tmp_cb = cb_data[i];
4232 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4239 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4241 zfs_prop_t proplist[] = {
4243 ZFS_PROP_COMPRESSION,
4247 ztest_shared_t *zs = ztest_shared;
4249 (void) rw_rdlock(&zs->zs_name_lock);
4251 for (int p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4252 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4253 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4255 (void) rw_unlock(&zs->zs_name_lock);
4260 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4262 ztest_shared_t *zs = ztest_shared;
4263 nvlist_t *props = NULL;
4265 (void) rw_rdlock(&zs->zs_name_lock);
4267 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4268 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4270 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4272 if (zopt_verbose >= 6)
4273 dump_nvlist(props, 4);
4277 (void) rw_unlock(&zs->zs_name_lock);
4281 * Test snapshot hold/release and deferred destroy.
4284 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4287 objset_t *os = zd->zd_os;
4291 char clonename[100];
4293 char osname[MAXNAMELEN];
4295 (void) rw_rdlock(&ztest_shared->zs_name_lock);
4297 dmu_objset_name(os, osname);
4299 (void) snprintf(snapname, 100, "sh1_%llu", id);
4300 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4301 (void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4302 (void) snprintf(tag, 100, "%tag_%llu", id);
4305 * Clean up from any previous run.
4307 (void) dmu_objset_destroy(clonename, B_FALSE);
4308 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4309 (void) dmu_objset_destroy(fullname, B_FALSE);
4312 * Create snapshot, clone it, mark snap for deferred destroy,
4313 * destroy clone, verify snap was also destroyed.
4315 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4318 if (error == ENOSPC) {
4319 ztest_record_enospc("dmu_objset_snapshot");
4322 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4325 error = dmu_objset_hold(fullname, FTAG, &origin);
4327 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4329 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4330 dmu_objset_rele(origin, FTAG);
4332 if (error == ENOSPC) {
4333 ztest_record_enospc("dmu_objset_clone");
4336 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4339 error = dmu_objset_destroy(fullname, B_TRUE);
4341 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4345 error = dmu_objset_destroy(clonename, B_FALSE);
4347 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4349 error = dmu_objset_hold(fullname, FTAG, &origin);
4350 if (error != ENOENT)
4351 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4354 * Create snapshot, add temporary hold, verify that we can't
4355 * destroy a held snapshot, mark for deferred destroy,
4356 * release hold, verify snapshot was destroyed.
4358 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4361 if (error == ENOSPC) {
4362 ztest_record_enospc("dmu_objset_snapshot");
4365 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4368 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4371 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4373 error = dmu_objset_destroy(fullname, B_FALSE);
4374 if (error != EBUSY) {
4375 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4379 error = dmu_objset_destroy(fullname, B_TRUE);
4381 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4385 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4387 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4389 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4392 (void) rw_unlock(&ztest_shared->zs_name_lock);
4396 * Inject random faults into the on-disk data.
4400 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4402 ztest_shared_t *zs = ztest_shared;
4403 spa_t *spa = zs->zs_spa;
4407 uint64_t bad = 0x1990c0ffeedecadeULL;
4409 char path0[MAXPATHLEN];
4410 char pathrand[MAXPATHLEN];
4412 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4418 boolean_t islog = B_FALSE;
4420 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4421 maxfaults = MAXFAULTS();
4422 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4423 mirror_save = zs->zs_mirrors;
4424 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4426 ASSERT(leaves >= 1);
4429 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4431 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4433 if (ztest_random(2) == 0) {
4435 * Inject errors on a normal data device or slog device.
4437 top = ztest_random_vdev_top(spa, B_TRUE);
4438 leaf = ztest_random(leaves) + zs->zs_splits;
4441 * Generate paths to the first leaf in this top-level vdev,
4442 * and to the random leaf we selected. We'll induce transient
4443 * write failures and random online/offline activity on leaf 0,
4444 * and we'll write random garbage to the randomly chosen leaf.
4446 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
4447 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4448 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4449 zopt_dir, zopt_pool, top * leaves + leaf);
4451 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4452 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4455 if (vd0 != NULL && maxfaults != 1) {
4457 * Make vd0 explicitly claim to be unreadable,
4458 * or unwriteable, or reach behind its back
4459 * and close the underlying fd. We can do this if
4460 * maxfaults == 0 because we'll fail and reexecute,
4461 * and we can do it if maxfaults >= 2 because we'll
4462 * have enough redundancy. If maxfaults == 1, the
4463 * combination of this with injection of random data
4464 * corruption below exceeds the pool's fault tolerance.
4466 vdev_file_t *vf = vd0->vdev_tsd;
4468 if (vf != NULL && ztest_random(3) == 0) {
4469 (void) close(vf->vf_vnode->v_fd);
4470 vf->vf_vnode->v_fd = -1;
4471 } else if (ztest_random(2) == 0) {
4472 vd0->vdev_cant_read = B_TRUE;
4474 vd0->vdev_cant_write = B_TRUE;
4476 guid0 = vd0->vdev_guid;
4480 * Inject errors on an l2cache device.
4482 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4484 if (sav->sav_count == 0) {
4485 spa_config_exit(spa, SCL_STATE, FTAG);
4488 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4489 guid0 = vd0->vdev_guid;
4490 (void) strcpy(path0, vd0->vdev_path);
4491 (void) strcpy(pathrand, vd0->vdev_path);
4495 maxfaults = INT_MAX; /* no limit on cache devices */
4498 spa_config_exit(spa, SCL_STATE, FTAG);
4501 * If we can tolerate two or more faults, or we're dealing
4502 * with a slog, randomly online/offline vd0.
4504 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4505 if (ztest_random(10) < 6) {
4506 int flags = (ztest_random(2) == 0 ?
4507 ZFS_OFFLINE_TEMPORARY : 0);
4510 * We have to grab the zs_name_lock as writer to
4511 * prevent a race between offlining a slog and
4512 * destroying a dataset. Offlining the slog will
4513 * grab a reference on the dataset which may cause
4514 * dmu_objset_destroy() to fail with EBUSY thus
4515 * leaving the dataset in an inconsistent state.
4518 (void) rw_wrlock(&ztest_shared->zs_name_lock);
4520 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4523 (void) rw_unlock(&ztest_shared->zs_name_lock);
4525 (void) vdev_online(spa, guid0, 0, NULL);
4533 * We have at least single-fault tolerance, so inject data corruption.
4535 fd = open(pathrand, O_RDWR);
4537 if (fd == -1) /* we hit a gap in the device namespace */
4540 fsize = lseek(fd, 0, SEEK_END);
4542 while (--iters != 0) {
4543 offset = ztest_random(fsize / (leaves << bshift)) *
4544 (leaves << bshift) + (leaf << bshift) +
4545 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4547 if (offset >= fsize)
4550 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4551 if (mirror_save != zs->zs_mirrors) {
4552 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4557 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4558 fatal(1, "can't inject bad word at 0x%llx in %s",
4561 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4563 if (zopt_verbose >= 7)
4564 (void) printf("injected bad word into %s,"
4565 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4572 * Verify that DDT repair works as expected.
4575 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4577 ztest_shared_t *zs = ztest_shared;
4578 spa_t *spa = zs->zs_spa;
4579 objset_t *os = zd->zd_os;
4581 uint64_t object, blocksize, txg, pattern, psize;
4582 enum zio_checksum checksum = spa_dedup_checksum(spa);
4587 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4589 blocksize = ztest_random_blocksize();
4590 blocksize = MIN(blocksize, 2048); /* because we write so many */
4592 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4594 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4598 * Take the name lock as writer to prevent anyone else from changing
4599 * the pool and dataset properies we need to maintain during this test.
4601 (void) rw_wrlock(&zs->zs_name_lock);
4603 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4605 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4607 (void) rw_unlock(&zs->zs_name_lock);
4611 object = od[0].od_object;
4612 blocksize = od[0].od_blocksize;
4613 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4615 ASSERT(object != 0);
4617 tx = dmu_tx_create(os);
4618 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4619 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4621 (void) rw_unlock(&zs->zs_name_lock);
4626 * Write all the copies of our block.
4628 for (int i = 0; i < copies; i++) {
4629 uint64_t offset = i * blocksize;
4630 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4631 DMU_READ_NO_PREFETCH) == 0);
4632 ASSERT(db->db_offset == offset);
4633 ASSERT(db->db_size == blocksize);
4634 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4635 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4636 dmu_buf_will_fill(db, tx);
4637 ztest_pattern_set(db->db_data, db->db_size, pattern);
4638 dmu_buf_rele(db, FTAG);
4642 txg_wait_synced(spa_get_dsl(spa), txg);
4645 * Find out what block we got.
4647 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4648 DMU_READ_NO_PREFETCH) == 0);
4649 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4650 dmu_buf_rele(db, FTAG);
4653 * Damage the block. Dedup-ditto will save us when we read it later.
4655 psize = BP_GET_PSIZE(&blk);
4656 buf = zio_buf_alloc(psize);
4657 ztest_pattern_set(buf, psize, ~pattern);
4659 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4660 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4661 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4663 zio_buf_free(buf, psize);
4665 (void) rw_unlock(&zs->zs_name_lock);
4673 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4675 ztest_shared_t *zs = ztest_shared;
4676 spa_t *spa = zs->zs_spa;
4678 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4679 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4680 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4684 * Rename the pool to a different name and then rename it back.
4688 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4690 ztest_shared_t *zs = ztest_shared;
4691 char *oldname, *newname;
4694 (void) rw_wrlock(&zs->zs_name_lock);
4696 oldname = zs->zs_pool;
4697 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4698 (void) strcpy(newname, oldname);
4699 (void) strcat(newname, "_tmp");
4704 VERIFY3U(0, ==, spa_rename(oldname, newname));
4707 * Try to open it under the old name, which shouldn't exist
4709 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4712 * Open it under the new name and make sure it's still the same spa_t.
4714 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4716 ASSERT(spa == zs->zs_spa);
4717 spa_close(spa, FTAG);
4720 * Rename it back to the original
4722 VERIFY3U(0, ==, spa_rename(newname, oldname));
4725 * Make sure it can still be opened
4727 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4729 ASSERT(spa == zs->zs_spa);
4730 spa_close(spa, FTAG);
4732 umem_free(newname, strlen(newname) + 1);
4734 (void) rw_unlock(&zs->zs_name_lock);
4738 * Verify pool integrity by running zdb.
4741 ztest_run_zdb(char *pool)
4744 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4752 strlcpy(zdb, "/usr/bin/ztest", sizeof(zdb));
4754 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4755 bin = strstr(zdb, "/usr/bin/");
4756 ztest = strstr(bin, "/ztest");
4758 isalen = ztest - isa;
4762 "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
4765 zopt_verbose >= 3 ? "s" : "",
4766 zopt_verbose >= 4 ? "v" : "",
4771 if (zopt_verbose >= 5)
4772 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4774 fp = popen(zdb, "r");
4777 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4778 if (zopt_verbose >= 3)
4779 (void) printf("%s", zbuf);
4781 status = pclose(fp);
4786 ztest_dump_core = 0;
4787 if (WIFEXITED(status))
4788 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4790 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4794 ztest_walk_pool_directory(char *header)
4798 if (zopt_verbose >= 6)
4799 (void) printf("%s\n", header);
4801 mutex_enter(&spa_namespace_lock);
4802 while ((spa = spa_next(spa)) != NULL)
4803 if (zopt_verbose >= 6)
4804 (void) printf("\t%s\n", spa_name(spa));
4805 mutex_exit(&spa_namespace_lock);
4809 ztest_spa_import_export(char *oldname, char *newname)
4811 nvlist_t *config, *newconfig;
4815 if (zopt_verbose >= 4) {
4816 (void) printf("import/export: old = %s, new = %s\n",
4821 * Clean up from previous runs.
4823 (void) spa_destroy(newname);
4826 * Get the pool's configuration and guid.
4828 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4831 * Kick off a scrub to tickle scrub/export races.
4833 if (ztest_random(2) == 0)
4834 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4836 pool_guid = spa_guid(spa);
4837 spa_close(spa, FTAG);
4839 ztest_walk_pool_directory("pools before export");
4844 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4846 ztest_walk_pool_directory("pools after export");
4851 newconfig = spa_tryimport(config);
4852 ASSERT(newconfig != NULL);
4853 nvlist_free(newconfig);
4856 * Import it under the new name.
4858 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
4860 ztest_walk_pool_directory("pools after import");
4863 * Try to import it again -- should fail with EEXIST.
4865 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
4868 * Try to import it under a different name -- should fail with EEXIST.
4870 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
4873 * Verify that the pool is no longer visible under the old name.
4875 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4878 * Verify that we can open and close the pool using the new name.
4880 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4881 ASSERT(pool_guid == spa_guid(spa));
4882 spa_close(spa, FTAG);
4884 nvlist_free(config);
4888 ztest_resume(spa_t *spa)
4890 if (spa_suspended(spa) && zopt_verbose >= 6)
4891 (void) printf("resuming from suspended state\n");
4892 spa_vdev_state_enter(spa, SCL_NONE);
4893 vdev_clear(spa, NULL);
4894 (void) spa_vdev_state_exit(spa, NULL, 0);
4895 (void) zio_resume(spa);
4899 ztest_resume_thread(void *arg)
4903 while (!ztest_exiting) {
4904 if (spa_suspended(spa))
4906 (void) poll(NULL, 0, 100);
4912 ztest_deadman_thread(void *arg)
4914 ztest_shared_t *zs = arg;
4918 delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4920 (void) poll(NULL, 0, (int)(1000 * delta));
4922 fatal(0, "failed to complete within %d seconds of deadline", grace);
4928 ztest_execute(ztest_info_t *zi, uint64_t id)
4930 ztest_shared_t *zs = ztest_shared;
4931 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4932 hrtime_t functime = gethrtime();
4934 for (int i = 0; i < zi->zi_iters; i++)
4935 zi->zi_func(zd, id);
4937 functime = gethrtime() - functime;
4939 atomic_add_64(&zi->zi_call_count, 1);
4940 atomic_add_64(&zi->zi_call_time, functime);
4942 if (zopt_verbose >= 4) {
4944 (void) dladdr((void *)zi->zi_func, &dli);
4945 (void) printf("%6.2f sec in %s\n",
4946 (double)functime / NANOSEC, dli.dli_sname);
4951 ztest_thread(void *arg)
4953 uint64_t id = (uintptr_t)arg;
4954 ztest_shared_t *zs = ztest_shared;
4959 while ((now = gethrtime()) < zs->zs_thread_stop) {
4961 * See if it's time to force a crash.
4963 if (now > zs->zs_thread_kill)
4967 * If we're getting ENOSPC with some regularity, stop.
4969 if (zs->zs_enospc_count > 10)
4973 * Pick a random function to execute.
4975 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
4976 call_next = zi->zi_call_next;
4978 if (now >= call_next &&
4979 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
4980 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
4981 ztest_execute(zi, id);
4988 ztest_dataset_name(char *dsname, char *pool, int d)
4990 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
4994 ztest_dataset_destroy(ztest_shared_t *zs, int d)
4996 char name[MAXNAMELEN];
4998 ztest_dataset_name(name, zs->zs_pool, d);
5000 if (zopt_verbose >= 3)
5001 (void) printf("Destroying %s to free up space\n", name);
5004 * Cleanup any non-standard clones and snapshots. In general,
5005 * ztest thread t operates on dataset (t % zopt_datasets),
5006 * so there may be more than one thing to clean up.
5008 for (int t = d; t < zopt_threads; t += zopt_datasets)
5009 ztest_dsl_dataset_cleanup(name, t);
5011 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5012 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5016 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5018 uint64_t usedobjs, dirobjs, scratch;
5021 * ZTEST_DIROBJ is the object directory for the entire dataset.
5022 * Therefore, the number of objects in use should equal the
5023 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5024 * If not, we have an object leak.
5026 * Note that we can only check this in ztest_dataset_open(),
5027 * when the open-context and syncing-context values agree.
5028 * That's because zap_count() returns the open-context value,
5029 * while dmu_objset_space() returns the rootbp fill count.
5031 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5032 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5033 ASSERT3U(dirobjs + 1, ==, usedobjs);
5037 ztest_dataset_open(ztest_shared_t *zs, int d)
5039 ztest_ds_t *zd = &zs->zs_zd[d];
5040 uint64_t committed_seq = zd->zd_seq;
5043 char name[MAXNAMELEN];
5046 ztest_dataset_name(name, zs->zs_pool, d);
5048 (void) rw_rdlock(&zs->zs_name_lock);
5050 error = ztest_dataset_create(name);
5051 if (error == ENOSPC) {
5052 (void) rw_unlock(&zs->zs_name_lock);
5053 ztest_record_enospc(FTAG);
5056 ASSERT(error == 0 || error == EEXIST);
5058 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5059 (void) rw_unlock(&zs->zs_name_lock);
5061 ztest_zd_init(zd, os);
5063 zilog = zd->zd_zilog;
5065 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5066 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5067 fatal(0, "missing log records: claimed %llu < committed %llu",
5068 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5070 ztest_dataset_dirobj_verify(zd);
5072 zil_replay(os, zd, ztest_replay_vector);
5074 ztest_dataset_dirobj_verify(zd);
5076 if (zopt_verbose >= 6)
5077 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5079 (u_longlong_t)zilog->zl_parse_blk_count,
5080 (u_longlong_t)zilog->zl_parse_lr_count,
5081 (u_longlong_t)zilog->zl_replaying_seq);
5083 zilog = zil_open(os, ztest_get_data);
5085 if (zilog->zl_replaying_seq != 0 &&
5086 zilog->zl_replaying_seq < committed_seq)
5087 fatal(0, "missing log records: replayed %llu < committed %llu",
5088 zilog->zl_replaying_seq, committed_seq);
5094 ztest_dataset_close(ztest_shared_t *zs, int d)
5096 ztest_ds_t *zd = &zs->zs_zd[d];
5098 zil_close(zd->zd_zilog);
5099 dmu_objset_rele(zd->zd_os, zd);
5105 * Kick off threads to run tests on all datasets in parallel.
5108 ztest_run(ztest_shared_t *zs)
5112 thread_t resume_tid;
5115 ztest_exiting = B_FALSE;
5118 * Initialize parent/child shared state.
5120 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5121 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5123 zs->zs_thread_start = gethrtime();
5124 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5125 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5126 zs->zs_thread_kill = zs->zs_thread_stop;
5127 if (ztest_random(100) < zopt_killrate)
5128 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5130 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5132 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5133 offsetof(ztest_cb_data_t, zcd_node));
5138 kernel_init(FREAD | FWRITE);
5139 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5142 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5145 * We don't expect the pool to suspend unless maxfaults == 0,
5146 * in which case ztest_fault_inject() temporarily takes away
5147 * the only valid replica.
5149 if (MAXFAULTS() == 0)
5150 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5152 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5155 * Create a thread to periodically resume suspended I/O.
5157 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5161 * Create a deadman thread to abort() if we hang.
5163 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5167 * Verify that we can safely inquire about about any object,
5168 * whether it's allocated or not. To make it interesting,
5169 * we probe a 5-wide window around each power of two.
5170 * This hits all edge cases, including zero and the max.
5172 for (int t = 0; t < 64; t++) {
5173 for (int d = -5; d <= 5; d++) {
5174 error = dmu_object_info(spa->spa_meta_objset,
5175 (1ULL << t) + d, NULL);
5176 ASSERT(error == 0 || error == ENOENT ||
5182 * If we got any ENOSPC errors on the previous run, destroy something.
5184 if (zs->zs_enospc_count != 0) {
5185 int d = ztest_random(zopt_datasets);
5186 ztest_dataset_destroy(zs, d);
5188 zs->zs_enospc_count = 0;
5190 tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5192 if (zopt_verbose >= 4)
5193 (void) printf("starting main threads...\n");
5196 * Kick off all the tests that run in parallel.
5198 for (int t = 0; t < zopt_threads; t++) {
5199 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5201 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5202 THR_BOUND, &tid[t]) == 0);
5206 * Wait for all of the tests to complete. We go in reverse order
5207 * so we don't close datasets while threads are still using them.
5209 for (int t = zopt_threads - 1; t >= 0; t--) {
5210 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5211 if (t < zopt_datasets)
5212 ztest_dataset_close(zs, t);
5215 txg_wait_synced(spa_get_dsl(spa), 0);
5217 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5218 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5220 umem_free(tid, zopt_threads * sizeof (thread_t));
5222 /* Kill the resume thread */
5223 ztest_exiting = B_TRUE;
5224 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5228 * Right before closing the pool, kick off a bunch of async I/O;
5229 * spa_close() should wait for it to complete.
5231 for (uint64_t object = 1; object < 50; object++)
5232 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5234 spa_close(spa, FTAG);
5237 * Verify that we can loop over all pools.
5239 mutex_enter(&spa_namespace_lock);
5240 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5241 if (zopt_verbose > 3)
5242 (void) printf("spa_next: found %s\n", spa_name(spa));
5243 mutex_exit(&spa_namespace_lock);
5246 * Verify that we can export the pool and reimport it under a
5249 if (ztest_random(2) == 0) {
5250 char name[MAXNAMELEN];
5251 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5252 ztest_spa_import_export(zs->zs_pool, name);
5253 ztest_spa_import_export(name, zs->zs_pool);
5258 list_destroy(&zcl.zcl_callbacks);
5260 (void) _mutex_destroy(&zcl.zcl_callbacks_lock);
5262 (void) rwlock_destroy(&zs->zs_name_lock);
5263 (void) _mutex_destroy(&zs->zs_vdev_lock);
5267 ztest_freeze(ztest_shared_t *zs)
5269 ztest_ds_t *zd = &zs->zs_zd[0];
5273 if (zopt_verbose >= 3)
5274 (void) printf("testing spa_freeze()...\n");
5276 kernel_init(FREAD | FWRITE);
5277 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5278 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5281 * Force the first log block to be transactionally allocated.
5282 * We have to do this before we freeze the pool -- otherwise
5283 * the log chain won't be anchored.
5285 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5286 ztest_dmu_object_alloc_free(zd, 0);
5287 zil_commit(zd->zd_zilog, 0);
5290 txg_wait_synced(spa_get_dsl(spa), 0);
5293 * Freeze the pool. This stops spa_sync() from doing anything,
5294 * so that the only way to record changes from now on is the ZIL.
5299 * Run tests that generate log records but don't alter the pool config
5300 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5301 * We do a txg_wait_synced() after each iteration to force the txg
5302 * to increase well beyond the last synced value in the uberblock.
5303 * The ZIL should be OK with that.
5305 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5306 ztest_dmu_write_parallel(zd, 0);
5307 ztest_dmu_object_alloc_free(zd, 0);
5308 txg_wait_synced(spa_get_dsl(spa), 0);
5312 * Commit all of the changes we just generated.
5314 zil_commit(zd->zd_zilog, 0);
5315 txg_wait_synced(spa_get_dsl(spa), 0);
5318 * Close our dataset and close the pool.
5320 ztest_dataset_close(zs, 0);
5321 spa_close(spa, FTAG);
5325 * Open and close the pool and dataset to induce log replay.
5327 kernel_init(FREAD | FWRITE);
5328 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5329 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5330 ztest_dataset_close(zs, 0);
5331 spa_close(spa, FTAG);
5336 print_time(hrtime_t t, char *timebuf)
5338 hrtime_t s = t / NANOSEC;
5339 hrtime_t m = s / 60;
5340 hrtime_t h = m / 60;
5341 hrtime_t d = h / 24;
5350 (void) sprintf(timebuf,
5351 "%llud%02lluh%02llum%02llus", d, h, m, s);
5353 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5355 (void) sprintf(timebuf, "%llum%02llus", m, s);
5357 (void) sprintf(timebuf, "%llus", s);
5365 if (ztest_random(2) == 0)
5368 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5369 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5371 (void) printf("props:\n");
5372 dump_nvlist(props, 4);
5378 * Create a storage pool with the given name and initial vdev size.
5379 * Then test spa_freeze() functionality.
5382 ztest_init(ztest_shared_t *zs)
5385 nvlist_t *nvroot, *props;
5387 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5388 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5390 kernel_init(FREAD | FWRITE);
5393 * Create the storage pool.
5395 (void) spa_destroy(zs->zs_pool);
5396 ztest_shared->zs_vdev_next_leaf = 0;
5398 zs->zs_mirrors = zopt_mirrors;
5399 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5400 0, zopt_raidz, zs->zs_mirrors, 1);
5401 props = make_random_props();
5402 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5403 nvlist_free(nvroot);
5405 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5406 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5407 spa_close(spa, FTAG);
5411 ztest_run_zdb(zs->zs_pool);
5415 ztest_run_zdb(zs->zs_pool);
5417 (void) rwlock_destroy(&zs->zs_name_lock);
5418 (void) _mutex_destroy(&zs->zs_vdev_lock);
5422 main(int argc, char **argv)
5433 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5435 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5437 process_options(argc, argv);
5439 /* Override location of zpool.cache */
5440 (void) asprintf((char **)&spa_config_path, "%s/zpool.cache", zopt_dir);
5443 * Blow away any existing copy of zpool.cache
5446 (void) remove(spa_config_path);
5448 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5450 zs = ztest_shared = (void *)mmap(0,
5451 P2ROUNDUP(shared_size, getpagesize()),
5452 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5454 if (zopt_verbose >= 1) {
5455 (void) printf("%llu vdevs, %d datasets, %d threads,"
5456 " %llu seconds...\n",
5457 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5458 (u_longlong_t)zopt_time);
5462 * Create and initialize our storage pool.
5464 for (int i = 1; i <= zopt_init; i++) {
5465 bzero(zs, sizeof (ztest_shared_t));
5466 if (zopt_verbose >= 3 && zopt_init != 1)
5467 (void) printf("ztest_init(), pass %d\n", i);
5468 zs->zs_pool = zopt_pool;
5472 zs->zs_pool = zopt_pool;
5473 zs->zs_proc_start = gethrtime();
5474 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5476 for (int f = 0; f < ZTEST_FUNCS; f++) {
5477 zi = &zs->zs_info[f];
5478 *zi = ztest_info[f];
5479 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5480 zi->zi_call_next = UINT64_MAX;
5482 zi->zi_call_next = zs->zs_proc_start +
5483 ztest_random(2 * zi->zi_interval[0] + 1);
5487 * Run the tests in a loop. These tests include fault injection
5488 * to verify that self-healing data works, and forced crashes
5489 * to verify that we never lose on-disk consistency.
5491 while (gethrtime() < zs->zs_proc_stop) {
5496 * Initialize the workload counters for each function.
5498 for (int f = 0; f < ZTEST_FUNCS; f++) {
5499 zi = &zs->zs_info[f];
5500 zi->zi_call_count = 0;
5501 zi->zi_call_time = 0;
5504 /* Set the allocation switch size */
5505 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5510 fatal(1, "fork failed");
5512 if (pid == 0) { /* child */
5513 struct rlimit rl = { 1024, 1024 };
5514 (void) setrlimit(RLIMIT_NOFILE, &rl);
5515 (void) enable_extended_FILE_stdio(-1, -1);
5520 while (waitpid(pid, &status, 0) != pid)
5523 if (WIFEXITED(status)) {
5524 if (WEXITSTATUS(status) != 0) {
5525 (void) fprintf(stderr,
5526 "child exited with code %d\n",
5527 WEXITSTATUS(status));
5530 } else if (WIFSIGNALED(status)) {
5531 if (WTERMSIG(status) != SIGKILL) {
5532 (void) fprintf(stderr,
5533 "child died with signal %d\n",
5539 (void) fprintf(stderr, "something strange happened "
5546 if (zopt_verbose >= 1) {
5547 hrtime_t now = gethrtime();
5549 now = MIN(now, zs->zs_proc_stop);
5550 print_time(zs->zs_proc_stop - now, timebuf);
5551 nicenum(zs->zs_space, numbuf);
5553 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5554 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5556 WIFEXITED(status) ? "Complete" : "SIGKILL",
5557 (u_longlong_t)zs->zs_enospc_count,
5558 100.0 * zs->zs_alloc / zs->zs_space,
5560 100.0 * (now - zs->zs_proc_start) /
5561 (zopt_time * NANOSEC), timebuf);
5564 if (zopt_verbose >= 2) {
5565 (void) printf("\nWorkload summary:\n\n");
5566 (void) printf("%7s %9s %s\n",
5567 "Calls", "Time", "Function");
5568 (void) printf("%7s %9s %s\n",
5569 "-----", "----", "--------");
5570 for (int f = 0; f < ZTEST_FUNCS; f++) {
5573 zi = &zs->zs_info[f];
5574 print_time(zi->zi_call_time, timebuf);
5575 (void) dladdr((void *)zi->zi_func, &dli);
5576 (void) printf("%7llu %9s %s\n",
5577 (u_longlong_t)zi->zi_call_count, timebuf,
5580 (void) printf("\n");
5584 * It's possible that we killed a child during a rename test,
5585 * in which case we'll have a 'ztest_tmp' pool lying around
5586 * instead of 'ztest'. Do a blind rename in case this happened.
5589 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5590 spa_close(spa, FTAG);
5592 char tmpname[MAXNAMELEN];
5594 kernel_init(FREAD | FWRITE);
5595 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5597 (void) spa_rename(tmpname, zopt_pool);
5601 ztest_run_zdb(zopt_pool);
5604 if (zopt_verbose >= 1) {
5605 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5606 kills, iters - kills, (100.0 * kills) / MAX(1, iters));