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>
108 #include <sys/fs/zfs.h>
109 #include <libnvpair.h>
111 static char cmdname[] = "ztest";
112 static char *zopt_pool = cmdname;
114 static uint64_t zopt_vdevs = 5;
115 static uint64_t zopt_vdevtime;
116 static int zopt_ashift = SPA_MINBLOCKSHIFT;
117 static int zopt_mirrors = 2;
118 static int zopt_raidz = 4;
119 static int zopt_raidz_parity = 1;
120 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
121 static int zopt_datasets = 7;
122 static int zopt_threads = 23;
123 static uint64_t zopt_passtime = 60; /* 60 seconds */
124 static uint64_t zopt_killrate = 70; /* 70% kill rate */
125 static int zopt_verbose = 0;
126 static int zopt_init = 1;
127 static char *zopt_dir = "/tmp";
128 static uint64_t zopt_time = 300; /* 5 minutes */
129 static uint64_t zopt_maxloops = 50; /* max loops during spa_freeze() */
131 #define BT_MAGIC 0x123456789abcdefULL
132 #define MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
136 ZTEST_IO_WRITE_PATTERN,
137 ZTEST_IO_WRITE_ZEROES,
143 typedef struct ztest_block_tag {
153 typedef struct bufwad {
160 * XXX -- fix zfs range locks to be generic so we can use them here.
182 #define ZTEST_RANGE_LOCKS 64
183 #define ZTEST_OBJECT_LOCKS 64
186 * Object descriptor. Used as a template for object lookup/create/remove.
188 typedef struct ztest_od {
191 dmu_object_type_t od_type;
192 dmu_object_type_t od_crtype;
193 uint64_t od_blocksize;
194 uint64_t od_crblocksize;
197 char od_name[MAXNAMELEN];
203 typedef struct ztest_ds {
207 ztest_od_t *zd_od; /* debugging aid */
208 char zd_name[MAXNAMELEN];
209 mutex_t zd_dirobj_lock;
210 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
211 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
215 * Per-iteration state.
217 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
219 typedef struct ztest_info {
220 ztest_func_t *zi_func; /* test function */
221 uint64_t zi_iters; /* iterations per execution */
222 uint64_t *zi_interval; /* execute every <interval> seconds */
223 uint64_t zi_call_count; /* per-pass count */
224 uint64_t zi_call_time; /* per-pass time */
225 uint64_t zi_call_next; /* next time to call this function */
229 * Note: these aren't static because we want dladdr() to work.
231 ztest_func_t ztest_dmu_read_write;
232 ztest_func_t ztest_dmu_write_parallel;
233 ztest_func_t ztest_dmu_object_alloc_free;
234 ztest_func_t ztest_dmu_commit_callbacks;
235 ztest_func_t ztest_zap;
236 ztest_func_t ztest_zap_parallel;
237 ztest_func_t ztest_zil_commit;
238 ztest_func_t ztest_dmu_read_write_zcopy;
239 ztest_func_t ztest_dmu_objset_create_destroy;
240 ztest_func_t ztest_dmu_prealloc;
241 ztest_func_t ztest_fzap;
242 ztest_func_t ztest_dmu_snapshot_create_destroy;
243 ztest_func_t ztest_dsl_prop_get_set;
244 ztest_func_t ztest_spa_prop_get_set;
245 ztest_func_t ztest_spa_create_destroy;
246 ztest_func_t ztest_fault_inject;
247 ztest_func_t ztest_ddt_repair;
248 ztest_func_t ztest_dmu_snapshot_hold;
249 ztest_func_t ztest_spa_rename;
250 ztest_func_t ztest_scrub;
251 ztest_func_t ztest_dsl_dataset_promote_busy;
252 ztest_func_t ztest_vdev_attach_detach;
253 ztest_func_t ztest_vdev_LUN_growth;
254 ztest_func_t ztest_vdev_add_remove;
255 ztest_func_t ztest_vdev_aux_add_remove;
256 ztest_func_t ztest_split_pool;
258 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
259 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
260 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
261 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
262 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
264 ztest_info_t ztest_info[] = {
265 { ztest_dmu_read_write, 1, &zopt_always },
266 { ztest_dmu_write_parallel, 10, &zopt_always },
267 { ztest_dmu_object_alloc_free, 1, &zopt_always },
268 { ztest_dmu_commit_callbacks, 1, &zopt_always },
269 { ztest_zap, 30, &zopt_always },
270 { ztest_zap_parallel, 100, &zopt_always },
271 { ztest_split_pool, 1, &zopt_always },
272 { ztest_zil_commit, 1, &zopt_incessant },
273 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
274 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
275 { ztest_dsl_prop_get_set, 1, &zopt_often },
276 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
278 { ztest_dmu_prealloc, 1, &zopt_sometimes },
280 { ztest_fzap, 1, &zopt_sometimes },
281 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
282 { ztest_spa_create_destroy, 1, &zopt_sometimes },
283 { ztest_fault_inject, 1, &zopt_sometimes },
284 { ztest_ddt_repair, 1, &zopt_sometimes },
285 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
286 { ztest_spa_rename, 1, &zopt_rarely },
287 { ztest_scrub, 1, &zopt_rarely },
288 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
289 { ztest_vdev_attach_detach, 1, &zopt_rarely },
290 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
291 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
292 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
295 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
298 * The following struct is used to hold a list of uncalled commit callbacks.
299 * The callbacks are ordered by txg number.
301 typedef struct ztest_cb_list {
302 mutex_t zcl_callbacks_lock;
303 list_t zcl_callbacks;
307 * Stuff we need to share writably between parent and child.
309 typedef struct ztest_shared {
312 hrtime_t zs_proc_start;
313 hrtime_t zs_proc_stop;
314 hrtime_t zs_thread_start;
315 hrtime_t zs_thread_stop;
316 hrtime_t zs_thread_kill;
317 uint64_t zs_enospc_count;
318 uint64_t zs_vdev_next_leaf;
319 uint64_t zs_vdev_aux;
322 mutex_t zs_vdev_lock;
323 rwlock_t zs_name_lock;
324 ztest_info_t zs_info[ZTEST_FUNCS];
330 #define ID_PARALLEL -1ULL
332 static char ztest_dev_template[] = "%s/%s.%llua";
333 static char ztest_aux_template[] = "%s/%s.%s.%llu";
334 ztest_shared_t *ztest_shared;
337 static int ztest_random_fd;
338 static int ztest_dump_core = 1;
340 static boolean_t ztest_exiting;
342 /* Global commit callback list */
343 static ztest_cb_list_t zcl;
345 extern uint64_t metaslab_gang_bang;
346 extern uint64_t metaslab_df_alloc_threshold;
347 static uint64_t metaslab_sz;
350 ZTEST_META_DNODE = 0,
355 static void usage(boolean_t) __NORETURN;
358 * These libumem hooks provide a reasonable set of defaults for the allocator's
359 * debugging facilities.
362 _umem_debug_init(void)
364 return ("default,verbose"); /* $UMEM_DEBUG setting */
368 _umem_logging_init(void)
370 return ("fail,contents"); /* $UMEM_LOGGING setting */
373 #define FATAL_MSG_SZ 1024
378 fatal(int do_perror, char *message, ...)
381 int save_errno = errno;
382 char buf[FATAL_MSG_SZ];
384 (void) fflush(stdout);
386 va_start(args, message);
387 (void) sprintf(buf, "ztest: ");
389 (void) vsprintf(buf + strlen(buf), message, args);
392 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
393 ": %s", strerror(save_errno));
395 (void) fprintf(stderr, "%s\n", buf);
396 fatal_msg = buf; /* to ease debugging */
403 str2shift(const char *buf)
405 const char *ends = "BKMGTPEZ";
410 for (i = 0; i < strlen(ends); i++) {
411 if (toupper(buf[0]) == ends[i])
414 if (i == strlen(ends)) {
415 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
419 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
422 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
428 nicenumtoull(const char *buf)
433 val = strtoull(buf, &end, 0);
435 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
437 } else if (end[0] == '.') {
438 double fval = strtod(buf, &end);
439 fval *= pow(2, str2shift(end));
440 if (fval > UINT64_MAX) {
441 (void) fprintf(stderr, "ztest: value too large: %s\n",
445 val = (uint64_t)fval;
447 int shift = str2shift(end);
448 if (shift >= 64 || (val << shift) >> shift != val) {
449 (void) fprintf(stderr, "ztest: value too large: %s\n",
459 usage(boolean_t requested)
461 char nice_vdev_size[10];
462 char nice_gang_bang[10];
463 FILE *fp = requested ? stdout : stderr;
465 nicenum(zopt_vdev_size, nice_vdev_size);
466 nicenum(metaslab_gang_bang, nice_gang_bang);
468 (void) fprintf(fp, "Usage: %s\n"
469 "\t[-v vdevs (default: %llu)]\n"
470 "\t[-s size_of_each_vdev (default: %s)]\n"
471 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
472 "\t[-m mirror_copies (default: %d)]\n"
473 "\t[-r raidz_disks (default: %d)]\n"
474 "\t[-R raidz_parity (default: %d)]\n"
475 "\t[-d datasets (default: %d)]\n"
476 "\t[-t threads (default: %d)]\n"
477 "\t[-g gang_block_threshold (default: %s)]\n"
478 "\t[-i init_count (default: %d)] initialize pool i times\n"
479 "\t[-k kill_percentage (default: %llu%%)]\n"
480 "\t[-p pool_name (default: %s)]\n"
481 "\t[-f dir (default: %s)] file directory for vdev files\n"
482 "\t[-V] verbose (use multiple times for ever more blather)\n"
483 "\t[-E] use existing pool instead of creating new one\n"
484 "\t[-T time (default: %llu sec)] total run time\n"
485 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
486 "\t[-P passtime (default: %llu sec)] time per pass\n"
487 "\t[-h] (print help)\n"
490 (u_longlong_t)zopt_vdevs, /* -v */
491 nice_vdev_size, /* -s */
492 zopt_ashift, /* -a */
493 zopt_mirrors, /* -m */
495 zopt_raidz_parity, /* -R */
496 zopt_datasets, /* -d */
497 zopt_threads, /* -t */
498 nice_gang_bang, /* -g */
500 (u_longlong_t)zopt_killrate, /* -k */
503 (u_longlong_t)zopt_time, /* -T */
504 (u_longlong_t)zopt_maxloops, /* -F */
505 (u_longlong_t)zopt_passtime); /* -P */
506 exit(requested ? 0 : 1);
510 process_options(int argc, char **argv)
515 /* By default, test gang blocks for blocks 32K and greater */
516 metaslab_gang_bang = 32 << 10;
518 while ((opt = getopt(argc, argv,
519 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
536 value = nicenumtoull(optarg);
543 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
549 zopt_mirrors = value;
552 zopt_raidz = MAX(1, value);
555 zopt_raidz_parity = MIN(MAX(value, 1), 3);
558 zopt_datasets = MAX(1, value);
561 zopt_threads = MAX(1, value);
564 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
570 zopt_killrate = value;
573 zopt_pool = strdup(optarg);
576 zopt_dir = strdup(optarg);
588 zopt_passtime = MAX(1, value);
591 zopt_maxloops = MAX(1, value);
603 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
605 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
610 ztest_kill(ztest_shared_t *zs)
612 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
613 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
614 (void) kill(getpid(), SIGKILL);
618 ztest_random(uint64_t range)
625 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
626 fatal(1, "short read from /dev/urandom");
633 ztest_record_enospc(const char *s)
635 ztest_shared->zs_enospc_count++;
639 ztest_get_ashift(void)
641 if (zopt_ashift == 0)
642 return (SPA_MINBLOCKSHIFT + ztest_random(3));
643 return (zopt_ashift);
647 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
649 char pathbuf[MAXPATHLEN];
654 ashift = ztest_get_ashift();
660 vdev = ztest_shared->zs_vdev_aux;
661 (void) sprintf(path, ztest_aux_template,
662 zopt_dir, zopt_pool, aux, vdev);
664 vdev = ztest_shared->zs_vdev_next_leaf++;
665 (void) sprintf(path, ztest_dev_template,
666 zopt_dir, zopt_pool, vdev);
671 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
673 fatal(1, "can't open %s", path);
674 if (ftruncate(fd, size) != 0)
675 fatal(1, "can't ftruncate %s", path);
679 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
680 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
681 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
682 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
688 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
690 nvlist_t *raidz, **child;
694 return (make_vdev_file(path, aux, size, ashift));
695 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
697 for (c = 0; c < r; c++)
698 child[c] = make_vdev_file(path, aux, size, ashift);
700 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
701 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
702 VDEV_TYPE_RAIDZ) == 0);
703 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
704 zopt_raidz_parity) == 0);
705 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
708 for (c = 0; c < r; c++)
709 nvlist_free(child[c]);
711 umem_free(child, r * sizeof (nvlist_t *));
717 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
720 nvlist_t *mirror, **child;
724 return (make_vdev_raidz(path, aux, size, ashift, r));
726 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
728 for (c = 0; c < m; c++)
729 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
731 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
732 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
733 VDEV_TYPE_MIRROR) == 0);
734 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
737 for (c = 0; c < m; c++)
738 nvlist_free(child[c]);
740 umem_free(child, m * sizeof (nvlist_t *));
746 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
747 int log, int r, int m, int t)
749 nvlist_t *root, **child;
754 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
756 for (c = 0; c < t; c++) {
757 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
758 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
762 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
763 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
764 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
767 for (c = 0; c < t; c++)
768 nvlist_free(child[c]);
770 umem_free(child, t * sizeof (nvlist_t *));
776 ztest_random_blocksize(void)
778 return (1 << (SPA_MINBLOCKSHIFT +
779 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
783 ztest_random_ibshift(void)
785 return (DN_MIN_INDBLKSHIFT +
786 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
790 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
793 vdev_t *rvd = spa->spa_root_vdev;
796 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
799 top = ztest_random(rvd->vdev_children);
800 tvd = rvd->vdev_child[top];
801 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
802 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
808 ztest_random_dsl_prop(zfs_prop_t prop)
813 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
814 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
820 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
823 const char *propname = zfs_prop_to_name(prop);
825 char setpoint[MAXPATHLEN];
829 error = dsl_prop_set(osname, propname,
830 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
831 sizeof (value), 1, &value);
833 if (error == ENOSPC) {
834 ztest_record_enospc(FTAG);
837 ASSERT3U(error, ==, 0);
839 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
840 1, &curval, setpoint), ==, 0);
842 if (zopt_verbose >= 6) {
843 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
844 (void) printf("%s %s = %s at '%s'\n",
845 osname, propname, valname, setpoint);
852 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
854 spa_t *spa = zs->zs_spa;
855 nvlist_t *props = NULL;
858 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
859 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
861 error = spa_prop_set(spa, props);
865 if (error == ENOSPC) {
866 ztest_record_enospc(FTAG);
869 ASSERT3U(error, ==, 0);
875 ztest_rll_init(rll_t *rll)
877 rll->rll_writer = NULL;
878 rll->rll_readers = 0;
879 VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
880 VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
884 ztest_rll_destroy(rll_t *rll)
886 ASSERT(rll->rll_writer == NULL);
887 ASSERT(rll->rll_readers == 0);
888 VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
889 VERIFY(cond_destroy(&rll->rll_cv) == 0);
893 ztest_rll_lock(rll_t *rll, rl_type_t type)
895 VERIFY(mutex_lock(&rll->rll_lock) == 0);
897 if (type == RL_READER) {
898 while (rll->rll_writer != NULL)
899 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
902 while (rll->rll_writer != NULL || rll->rll_readers)
903 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
904 rll->rll_writer = curthread;
907 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
911 ztest_rll_unlock(rll_t *rll)
913 VERIFY(mutex_lock(&rll->rll_lock) == 0);
915 if (rll->rll_writer) {
916 ASSERT(rll->rll_readers == 0);
917 rll->rll_writer = NULL;
919 ASSERT(rll->rll_readers != 0);
920 ASSERT(rll->rll_writer == NULL);
924 if (rll->rll_writer == NULL && rll->rll_readers == 0)
925 VERIFY(cond_broadcast(&rll->rll_cv) == 0);
927 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
931 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
933 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
935 ztest_rll_lock(rll, type);
939 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
941 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
943 ztest_rll_unlock(rll);
947 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
948 uint64_t size, rl_type_t type)
950 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
951 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
954 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
955 rl->rl_object = object;
956 rl->rl_offset = offset;
960 ztest_rll_lock(rll, type);
966 ztest_range_unlock(rl_t *rl)
968 rll_t *rll = rl->rl_lock;
970 ztest_rll_unlock(rll);
972 umem_free(rl, sizeof (*rl));
976 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
979 zd->zd_zilog = dmu_objset_zil(os);
981 dmu_objset_name(os, zd->zd_name);
984 VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
986 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
987 ztest_rll_init(&zd->zd_object_lock[l]);
989 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
990 ztest_rll_init(&zd->zd_range_lock[l]);
994 ztest_zd_fini(ztest_ds_t *zd)
998 VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1000 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1001 ztest_rll_destroy(&zd->zd_object_lock[l]);
1003 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1004 ztest_rll_destroy(&zd->zd_range_lock[l]);
1007 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1010 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1016 * Attempt to assign tx to some transaction group.
1018 error = dmu_tx_assign(tx, txg_how);
1020 if (error == ERESTART) {
1021 ASSERT(txg_how == TXG_NOWAIT);
1024 ASSERT3U(error, ==, ENOSPC);
1025 ztest_record_enospc(tag);
1030 txg = dmu_tx_get_txg(tx);
1036 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1039 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1047 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1050 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1054 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 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1586 NULL, /* TX_MKDIR */
1587 NULL, /* TX_MKXATTR */
1588 NULL, /* TX_SYMLINK */
1589 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1590 NULL, /* TX_RMDIR */
1592 NULL, /* TX_RENAME */
1593 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1594 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1595 (zil_replay_func_t *)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)
1741 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1743 for (i = 0; i < count; i++, od++) {
1745 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1746 sizeof (uint64_t), 1, &od->od_object);
1748 ASSERT(error == ENOENT);
1749 ASSERT(od->od_object == 0);
1753 ztest_block_tag_t *bbt;
1754 dmu_object_info_t doi;
1756 ASSERT(od->od_object != 0);
1757 ASSERT(missing == 0); /* there should be no gaps */
1759 ztest_object_lock(zd, od->od_object, RL_READER);
1760 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1761 od->od_object, FTAG, &db));
1762 dmu_object_info_from_db(db, &doi);
1763 bbt = ztest_bt_bonus(db);
1764 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1765 od->od_type = doi.doi_type;
1766 od->od_blocksize = doi.doi_data_block_size;
1767 od->od_gen = bbt->bt_gen;
1768 dmu_buf_rele(db, FTAG);
1769 ztest_object_unlock(zd, od->od_object);
1777 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1782 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1784 for (i = 0; i < count; i++, od++) {
1791 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1793 lr->lr_doid = od->od_dir;
1794 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1795 lr->lrz_type = od->od_crtype;
1796 lr->lrz_blocksize = od->od_crblocksize;
1797 lr->lrz_ibshift = ztest_random_ibshift();
1798 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1799 lr->lrz_bonuslen = dmu_bonus_max();
1800 lr->lr_gen = od->od_crgen;
1801 lr->lr_crtime[0] = time(NULL);
1803 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1804 ASSERT(missing == 0);
1808 od->od_object = lr->lr_foid;
1809 od->od_type = od->od_crtype;
1810 od->od_blocksize = od->od_crblocksize;
1811 od->od_gen = od->od_crgen;
1812 ASSERT(od->od_object != 0);
1815 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1822 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1828 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1832 for (i = count - 1; i >= 0; i--, od--) {
1838 if (od->od_object == 0)
1841 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1843 lr->lr_doid = od->od_dir;
1845 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1846 ASSERT3U(error, ==, ENOSPC);
1851 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1858 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1864 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1866 lr->lr_foid = object;
1867 lr->lr_offset = offset;
1868 lr->lr_length = size;
1870 BP_ZERO(&lr->lr_blkptr);
1872 bcopy(data, lr + 1, size);
1874 error = ztest_replay_write(zd, lr, B_FALSE);
1876 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1882 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1887 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1889 lr->lr_foid = object;
1890 lr->lr_offset = offset;
1891 lr->lr_length = size;
1893 error = ztest_replay_truncate(zd, lr, B_FALSE);
1895 ztest_lr_free(lr, sizeof (*lr), NULL);
1901 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1906 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1908 lr->lr_foid = object;
1912 error = ztest_replay_setattr(zd, lr, B_FALSE);
1914 ztest_lr_free(lr, sizeof (*lr), NULL);
1920 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1922 objset_t *os = zd->zd_os;
1927 txg_wait_synced(dmu_objset_pool(os), 0);
1929 ztest_object_lock(zd, object, RL_READER);
1930 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1932 tx = dmu_tx_create(os);
1934 dmu_tx_hold_write(tx, object, offset, size);
1936 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1939 dmu_prealloc(os, object, offset, size, tx);
1941 txg_wait_synced(dmu_objset_pool(os), txg);
1943 (void) dmu_free_long_range(os, object, offset, size);
1946 ztest_range_unlock(rl);
1947 ztest_object_unlock(zd, object);
1951 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1953 ztest_block_tag_t wbt;
1954 dmu_object_info_t doi;
1955 enum ztest_io_type io_type;
1959 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1960 blocksize = doi.doi_data_block_size;
1961 data = umem_alloc(blocksize, UMEM_NOFAIL);
1964 * Pick an i/o type at random, biased toward writing block tags.
1966 io_type = ztest_random(ZTEST_IO_TYPES);
1967 if (ztest_random(2) == 0)
1968 io_type = ZTEST_IO_WRITE_TAG;
1972 case ZTEST_IO_WRITE_TAG:
1973 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1974 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1977 case ZTEST_IO_WRITE_PATTERN:
1978 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
1979 if (ztest_random(2) == 0) {
1981 * Induce fletcher2 collisions to ensure that
1982 * zio_ddt_collision() detects and resolves them
1983 * when using fletcher2-verify for deduplication.
1985 ((uint64_t *)data)[0] ^= 1ULL << 63;
1986 ((uint64_t *)data)[4] ^= 1ULL << 63;
1988 (void) ztest_write(zd, object, offset, blocksize, data);
1991 case ZTEST_IO_WRITE_ZEROES:
1992 bzero(data, blocksize);
1993 (void) ztest_write(zd, object, offset, blocksize, data);
1996 case ZTEST_IO_TRUNCATE:
1997 (void) ztest_truncate(zd, object, offset, blocksize);
2000 case ZTEST_IO_SETATTR:
2001 (void) ztest_setattr(zd, object);
2007 umem_free(data, blocksize);
2011 * Initialize an object description template.
2014 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2015 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2017 od->od_dir = ZTEST_DIROBJ;
2020 od->od_crtype = type;
2021 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2024 od->od_type = DMU_OT_NONE;
2025 od->od_blocksize = 0;
2028 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2029 tag, (longlong_t)id, (u_longlong_t)index);
2033 * Lookup or create the objects for a test using the od template.
2034 * If the objects do not all exist, or if 'remove' is specified,
2035 * remove any existing objects and create new ones. Otherwise,
2036 * use the existing objects.
2039 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2041 int count = size / sizeof (*od);
2044 VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2045 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2046 (ztest_remove(zd, od, count) != 0 ||
2047 ztest_create(zd, od, count) != 0))
2050 VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2057 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2059 zilog_t *zilog = zd->zd_zilog;
2061 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2064 * Remember the committed values in zd, which is in parent/child
2065 * shared memory. If we die, the next iteration of ztest_run()
2066 * will verify that the log really does contain this record.
2068 mutex_enter(&zilog->zl_lock);
2069 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2070 zd->zd_seq = zilog->zl_commit_lr_seq;
2071 mutex_exit(&zilog->zl_lock);
2075 * Verify that we can't destroy an active pool, create an existing pool,
2076 * or create a pool with a bad vdev spec.
2080 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2082 ztest_shared_t *zs = ztest_shared;
2087 * Attempt to create using a bad file.
2089 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2090 VERIFY3U(ENOENT, ==,
2091 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2092 nvlist_free(nvroot);
2095 * Attempt to create using a bad mirror.
2097 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2098 VERIFY3U(ENOENT, ==,
2099 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2100 nvlist_free(nvroot);
2103 * Attempt to create an existing pool. It shouldn't matter
2104 * what's in the nvroot; we should fail with EEXIST.
2106 (void) rw_rdlock(&zs->zs_name_lock);
2107 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2108 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2109 nvlist_free(nvroot);
2110 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2111 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2112 spa_close(spa, FTAG);
2114 (void) rw_unlock(&zs->zs_name_lock);
2118 vdev_lookup_by_path(vdev_t *vd, const char *path)
2123 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2126 for (c = 0; c < vd->vdev_children; c++)
2127 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2135 * Find the first available hole which can be used as a top-level.
2138 find_vdev_hole(spa_t *spa)
2140 vdev_t *rvd = spa->spa_root_vdev;
2143 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2145 for (c = 0; c < rvd->vdev_children; c++) {
2146 vdev_t *cvd = rvd->vdev_child[c];
2148 if (cvd->vdev_ishole)
2155 * Verify that vdev_add() works as expected.
2159 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2161 ztest_shared_t *zs = ztest_shared;
2162 spa_t *spa = zs->zs_spa;
2168 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2169 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2171 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2173 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2176 * If we have slogs then remove them 1/4 of the time.
2178 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2180 * Grab the guid from the head of the log class rotor.
2182 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2184 spa_config_exit(spa, SCL_VDEV, FTAG);
2187 * We have to grab the zs_name_lock as writer to
2188 * prevent a race between removing a slog (dmu_objset_find)
2189 * and destroying a dataset. Removing the slog will
2190 * grab a reference on the dataset which may cause
2191 * dmu_objset_destroy() to fail with EBUSY thus
2192 * leaving the dataset in an inconsistent state.
2194 VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
2195 error = spa_vdev_remove(spa, guid, B_FALSE);
2196 VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
2198 if (error && error != EEXIST)
2199 fatal(0, "spa_vdev_remove() = %d", error);
2201 spa_config_exit(spa, SCL_VDEV, FTAG);
2204 * Make 1/4 of the devices be log devices.
2206 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2207 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2209 error = spa_vdev_add(spa, nvroot);
2210 nvlist_free(nvroot);
2212 if (error == ENOSPC)
2213 ztest_record_enospc("spa_vdev_add");
2214 else if (error != 0)
2215 fatal(0, "spa_vdev_add() = %d", error);
2218 VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
2222 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2226 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2228 ztest_shared_t *zs = ztest_shared;
2229 spa_t *spa = zs->zs_spa;
2230 vdev_t *rvd = spa->spa_root_vdev;
2231 spa_aux_vdev_t *sav;
2236 if (ztest_random(2) == 0) {
2237 sav = &spa->spa_spares;
2238 aux = ZPOOL_CONFIG_SPARES;
2240 sav = &spa->spa_l2cache;
2241 aux = ZPOOL_CONFIG_L2CACHE;
2244 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2246 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2248 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2250 * Pick a random device to remove.
2252 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2255 * Find an unused device we can add.
2257 zs->zs_vdev_aux = 0;
2259 char path[MAXPATHLEN];
2261 (void) sprintf(path, ztest_aux_template, zopt_dir,
2262 zopt_pool, aux, zs->zs_vdev_aux);
2263 for (c = 0; c < sav->sav_count; c++)
2264 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2267 if (c == sav->sav_count &&
2268 vdev_lookup_by_path(rvd, path) == NULL)
2274 spa_config_exit(spa, SCL_VDEV, FTAG);
2280 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2281 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2282 error = spa_vdev_add(spa, nvroot);
2284 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2285 nvlist_free(nvroot);
2288 * Remove an existing device. Sometimes, dirty its
2289 * vdev state first to make sure we handle removal
2290 * of devices that have pending state changes.
2292 if (ztest_random(2) == 0)
2293 (void) vdev_online(spa, guid, 0, NULL);
2295 error = spa_vdev_remove(spa, guid, B_FALSE);
2296 if (error != 0 && error != EBUSY)
2297 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2300 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2304 * split a pool if it has mirror tlvdevs
2308 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2310 ztest_shared_t *zs = ztest_shared;
2311 spa_t *spa = zs->zs_spa;
2312 vdev_t *rvd = spa->spa_root_vdev;
2313 nvlist_t *tree, **child, *config, *split, **schild;
2314 uint_t c, children, schildren = 0, lastlogid = 0;
2317 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2319 /* ensure we have a useable config; mirrors of raidz aren't supported */
2320 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2321 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2325 /* clean up the old pool, if any */
2326 (void) spa_destroy("splitp");
2328 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2330 /* generate a config from the existing config */
2331 mutex_enter(&spa->spa_props_lock);
2332 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2334 mutex_exit(&spa->spa_props_lock);
2336 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2339 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2340 for (c = 0; c < children; c++) {
2341 vdev_t *tvd = rvd->vdev_child[c];
2345 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2346 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2348 VERIFY(nvlist_add_string(schild[schildren],
2349 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2350 VERIFY(nvlist_add_uint64(schild[schildren],
2351 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2353 lastlogid = schildren;
2358 VERIFY(nvlist_lookup_nvlist_array(child[c],
2359 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2360 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2363 /* OK, create a config that can be used to split */
2364 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2365 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2366 VDEV_TYPE_ROOT) == 0);
2367 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2368 lastlogid != 0 ? lastlogid : schildren) == 0);
2370 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2371 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2373 for (c = 0; c < schildren; c++)
2374 nvlist_free(schild[c]);
2378 spa_config_exit(spa, SCL_VDEV, FTAG);
2380 (void) rw_wrlock(&zs->zs_name_lock);
2381 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2382 (void) rw_unlock(&zs->zs_name_lock);
2384 nvlist_free(config);
2387 (void) printf("successful split - results:\n");
2388 mutex_enter(&spa_namespace_lock);
2389 show_pool_stats(spa);
2390 show_pool_stats(spa_lookup("splitp"));
2391 mutex_exit(&spa_namespace_lock);
2395 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2400 * Verify that we can attach and detach devices.
2404 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2406 ztest_shared_t *zs = ztest_shared;
2407 spa_t *spa = zs->zs_spa;
2408 spa_aux_vdev_t *sav = &spa->spa_spares;
2409 vdev_t *rvd = spa->spa_root_vdev;
2410 vdev_t *oldvd, *newvd, *pvd;
2414 uint64_t ashift = ztest_get_ashift();
2415 uint64_t oldguid, pguid;
2416 size_t oldsize, newsize;
2417 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2419 int oldvd_has_siblings = B_FALSE;
2420 int newvd_is_spare = B_FALSE;
2422 int error, expected_error;
2424 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2425 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2427 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2430 * Decide whether to do an attach or a replace.
2432 replacing = ztest_random(2);
2435 * Pick a random top-level vdev.
2437 top = ztest_random_vdev_top(spa, B_TRUE);
2440 * Pick a random leaf within it.
2442 leaf = ztest_random(leaves);
2447 oldvd = rvd->vdev_child[top];
2448 if (zs->zs_mirrors >= 1) {
2449 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2450 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2451 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2453 if (zopt_raidz > 1) {
2454 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2455 ASSERT(oldvd->vdev_children == zopt_raidz);
2456 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2460 * If we're already doing an attach or replace, oldvd may be a
2461 * mirror vdev -- in which case, pick a random child.
2463 while (oldvd->vdev_children != 0) {
2464 oldvd_has_siblings = B_TRUE;
2465 ASSERT(oldvd->vdev_children >= 2);
2466 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2469 oldguid = oldvd->vdev_guid;
2470 oldsize = vdev_get_min_asize(oldvd);
2471 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2472 (void) strcpy(oldpath, oldvd->vdev_path);
2473 pvd = oldvd->vdev_parent;
2474 pguid = pvd->vdev_guid;
2477 * If oldvd has siblings, then half of the time, detach it.
2479 if (oldvd_has_siblings && ztest_random(2) == 0) {
2480 spa_config_exit(spa, SCL_VDEV, FTAG);
2481 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2482 if (error != 0 && error != ENODEV && error != EBUSY &&
2484 fatal(0, "detach (%s) returned %d", oldpath, error);
2485 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2490 * For the new vdev, choose with equal probability between the two
2491 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2493 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2494 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2495 newvd_is_spare = B_TRUE;
2496 (void) strcpy(newpath, newvd->vdev_path);
2498 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2499 zopt_dir, zopt_pool, top * leaves + leaf);
2500 if (ztest_random(2) == 0)
2501 newpath[strlen(newpath) - 1] = 'b';
2502 newvd = vdev_lookup_by_path(rvd, newpath);
2506 newsize = vdev_get_min_asize(newvd);
2509 * Make newsize a little bigger or smaller than oldsize.
2510 * If it's smaller, the attach should fail.
2511 * If it's larger, and we're doing a replace,
2512 * we should get dynamic LUN growth when we're done.
2514 newsize = 10 * oldsize / (9 + ztest_random(3));
2518 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2519 * unless it's a replace; in that case any non-replacing parent is OK.
2521 * If newvd is already part of the pool, it should fail with EBUSY.
2523 * If newvd is too small, it should fail with EOVERFLOW.
2525 if (pvd->vdev_ops != &vdev_mirror_ops &&
2526 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2527 pvd->vdev_ops == &vdev_replacing_ops ||
2528 pvd->vdev_ops == &vdev_spare_ops))
2529 expected_error = ENOTSUP;
2530 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2531 expected_error = ENOTSUP;
2532 else if (newvd == oldvd)
2533 expected_error = replacing ? 0 : EBUSY;
2534 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2535 expected_error = EBUSY;
2536 else if (newsize < oldsize)
2537 expected_error = EOVERFLOW;
2538 else if (ashift > oldvd->vdev_top->vdev_ashift)
2539 expected_error = EDOM;
2543 spa_config_exit(spa, SCL_VDEV, FTAG);
2546 * Build the nvlist describing newpath.
2548 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2549 ashift, 0, 0, 0, 1);
2551 error = spa_vdev_attach(spa, oldguid, root, replacing);
2556 * If our parent was the replacing vdev, but the replace completed,
2557 * then instead of failing with ENOTSUP we may either succeed,
2558 * fail with ENODEV, or fail with EOVERFLOW.
2560 if (expected_error == ENOTSUP &&
2561 (error == 0 || error == ENODEV || error == EOVERFLOW))
2562 expected_error = error;
2565 * If someone grew the LUN, the replacement may be too small.
2567 if (error == EOVERFLOW || error == EBUSY)
2568 expected_error = error;
2570 /* XXX workaround 6690467 */
2571 if (error != expected_error && expected_error != EBUSY) {
2572 fatal(0, "attach (%s %llu, %s %llu, %d) "
2573 "returned %d, expected %d",
2574 oldpath, (longlong_t)oldsize, newpath,
2575 (longlong_t)newsize, replacing, error, expected_error);
2578 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2582 * Callback function which expands the physical size of the vdev.
2585 grow_vdev(vdev_t *vd, void *arg)
2587 ASSERTV(spa_t *spa = vd->vdev_spa);
2588 size_t *newsize = arg;
2592 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2593 ASSERT(vd->vdev_ops->vdev_op_leaf);
2595 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2598 fsize = lseek(fd, 0, SEEK_END);
2599 VERIFY(ftruncate(fd, *newsize) == 0);
2601 if (zopt_verbose >= 6) {
2602 (void) printf("%s grew from %lu to %lu bytes\n",
2603 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2610 * Callback function which expands a given vdev by calling vdev_online().
2614 online_vdev(vdev_t *vd, void *arg)
2616 spa_t *spa = vd->vdev_spa;
2617 vdev_t *tvd = vd->vdev_top;
2618 uint64_t guid = vd->vdev_guid;
2619 uint64_t generation = spa->spa_config_generation + 1;
2620 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2623 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2624 ASSERT(vd->vdev_ops->vdev_op_leaf);
2626 /* Calling vdev_online will initialize the new metaslabs */
2627 spa_config_exit(spa, SCL_STATE, spa);
2628 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2629 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2632 * If vdev_online returned an error or the underlying vdev_open
2633 * failed then we abort the expand. The only way to know that
2634 * vdev_open fails is by checking the returned newstate.
2636 if (error || newstate != VDEV_STATE_HEALTHY) {
2637 if (zopt_verbose >= 5) {
2638 (void) printf("Unable to expand vdev, state %llu, "
2639 "error %d\n", (u_longlong_t)newstate, error);
2643 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2646 * Since we dropped the lock we need to ensure that we're
2647 * still talking to the original vdev. It's possible this
2648 * vdev may have been detached/replaced while we were
2649 * trying to online it.
2651 if (generation != spa->spa_config_generation) {
2652 if (zopt_verbose >= 5) {
2653 (void) printf("vdev configuration has changed, "
2654 "guid %llu, state %llu, expected gen %llu, "
2657 (u_longlong_t)tvd->vdev_state,
2658 (u_longlong_t)generation,
2659 (u_longlong_t)spa->spa_config_generation);
2667 * Traverse the vdev tree calling the supplied function.
2668 * We continue to walk the tree until we either have walked all
2669 * children or we receive a non-NULL return from the callback.
2670 * If a NULL callback is passed, then we just return back the first
2671 * leaf vdev we encounter.
2674 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2678 if (vd->vdev_ops->vdev_op_leaf) {
2682 return (func(vd, arg));
2685 for (c = 0; c < vd->vdev_children; c++) {
2686 vdev_t *cvd = vd->vdev_child[c];
2687 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2694 * Verify that dynamic LUN growth works as expected.
2698 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2700 ztest_shared_t *zs = ztest_shared;
2701 spa_t *spa = zs->zs_spa;
2703 metaslab_class_t *mc;
2704 metaslab_group_t *mg;
2705 size_t psize, newsize;
2707 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2709 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2710 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2712 top = ztest_random_vdev_top(spa, B_TRUE);
2714 tvd = spa->spa_root_vdev->vdev_child[top];
2717 old_ms_count = tvd->vdev_ms_count;
2718 old_class_space = metaslab_class_get_space(mc);
2721 * Determine the size of the first leaf vdev associated with
2722 * our top-level device.
2724 vd = vdev_walk_tree(tvd, NULL, NULL);
2725 ASSERT3P(vd, !=, NULL);
2726 ASSERT(vd->vdev_ops->vdev_op_leaf);
2728 psize = vd->vdev_psize;
2731 * We only try to expand the vdev if it's healthy, less than 4x its
2732 * original size, and it has a valid psize.
2734 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2735 psize == 0 || psize >= 4 * zopt_vdev_size) {
2736 spa_config_exit(spa, SCL_STATE, spa);
2737 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2741 newsize = psize + psize / 8;
2742 ASSERT3U(newsize, >, psize);
2744 if (zopt_verbose >= 6) {
2745 (void) printf("Expanding LUN %s from %lu to %lu\n",
2746 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2750 * Growing the vdev is a two step process:
2751 * 1). expand the physical size (i.e. relabel)
2752 * 2). online the vdev to create the new metaslabs
2754 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2755 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2756 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2757 if (zopt_verbose >= 5) {
2758 (void) printf("Could not expand LUN because "
2759 "the vdev configuration changed.\n");
2761 spa_config_exit(spa, SCL_STATE, spa);
2762 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2766 spa_config_exit(spa, SCL_STATE, spa);
2769 * Expanding the LUN will update the config asynchronously,
2770 * thus we must wait for the async thread to complete any
2771 * pending tasks before proceeding.
2775 mutex_enter(&spa->spa_async_lock);
2776 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2777 mutex_exit(&spa->spa_async_lock);
2780 txg_wait_synced(spa_get_dsl(spa), 0);
2781 (void) poll(NULL, 0, 100);
2784 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2786 tvd = spa->spa_root_vdev->vdev_child[top];
2787 new_ms_count = tvd->vdev_ms_count;
2788 new_class_space = metaslab_class_get_space(mc);
2790 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2791 if (zopt_verbose >= 5) {
2792 (void) printf("Could not verify LUN expansion due to "
2793 "intervening vdev offline or remove.\n");
2795 spa_config_exit(spa, SCL_STATE, spa);
2796 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2801 * Make sure we were able to grow the vdev.
2803 if (new_ms_count <= old_ms_count)
2804 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2805 old_ms_count, new_ms_count);
2808 * Make sure we were able to grow the pool.
2810 if (new_class_space <= old_class_space)
2811 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2812 old_class_space, new_class_space);
2814 if (zopt_verbose >= 5) {
2815 char oldnumbuf[6], newnumbuf[6];
2817 nicenum(old_class_space, oldnumbuf);
2818 nicenum(new_class_space, newnumbuf);
2819 (void) printf("%s grew from %s to %s\n",
2820 spa->spa_name, oldnumbuf, newnumbuf);
2823 spa_config_exit(spa, SCL_STATE, spa);
2824 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2828 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2832 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2835 * Create the objects common to all ztest datasets.
2837 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2838 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2842 ztest_dataset_create(char *dsname)
2844 uint64_t zilset = ztest_random(100);
2845 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2846 ztest_objset_create_cb, NULL);
2848 if (err || zilset < 80)
2851 (void) printf("Setting dataset %s to sync always\n", dsname);
2852 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2853 ZFS_SYNC_ALWAYS, B_FALSE));
2858 ztest_objset_destroy_cb(const char *name, void *arg)
2861 dmu_object_info_t doi;
2865 * Verify that the dataset contains a directory object.
2867 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2868 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2869 if (error != ENOENT) {
2870 /* We could have crashed in the middle of destroying it */
2871 ASSERT3U(error, ==, 0);
2872 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2873 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2875 dmu_objset_rele(os, FTAG);
2878 * Destroy the dataset.
2880 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2885 ztest_snapshot_create(char *osname, uint64_t id)
2887 char snapname[MAXNAMELEN];
2890 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2893 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2894 NULL, NULL, B_FALSE, B_FALSE, -1);
2895 if (error == ENOSPC) {
2896 ztest_record_enospc(FTAG);
2899 if (error != 0 && error != EEXIST)
2900 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2905 ztest_snapshot_destroy(char *osname, uint64_t id)
2907 char snapname[MAXNAMELEN];
2910 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2913 error = dmu_objset_destroy(snapname, B_FALSE);
2914 if (error != 0 && error != ENOENT)
2915 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2921 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2923 ztest_shared_t *zs = ztest_shared;
2928 char name[MAXNAMELEN];
2932 (void) rw_rdlock(&zs->zs_name_lock);
2934 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2935 zs->zs_pool, (u_longlong_t)id);
2938 * If this dataset exists from a previous run, process its replay log
2939 * half of the time. If we don't replay it, then dmu_objset_destroy()
2940 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2942 if (ztest_random(2) == 0 &&
2943 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2944 ztest_zd_init(&zdtmp, os);
2945 zil_replay(os, &zdtmp, ztest_replay_vector);
2946 ztest_zd_fini(&zdtmp);
2947 dmu_objset_disown(os, FTAG);
2951 * There may be an old instance of the dataset we're about to
2952 * create lying around from a previous run. If so, destroy it
2953 * and all of its snapshots.
2955 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
2956 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2959 * Verify that the destroyed dataset is no longer in the namespace.
2961 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2964 * Verify that we can create a new dataset.
2966 error = ztest_dataset_create(name);
2968 if (error == ENOSPC) {
2969 ztest_record_enospc(FTAG);
2970 (void) rw_unlock(&zs->zs_name_lock);
2973 fatal(0, "dmu_objset_create(%s) = %d", name, error);
2977 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
2979 ztest_zd_init(&zdtmp, os);
2982 * Open the intent log for it.
2984 zilog = zil_open(os, ztest_get_data);
2987 * Put some objects in there, do a little I/O to them,
2988 * and randomly take a couple of snapshots along the way.
2990 iters = ztest_random(5);
2991 for (i = 0; i < iters; i++) {
2992 ztest_dmu_object_alloc_free(&zdtmp, id);
2993 if (ztest_random(iters) == 0)
2994 (void) ztest_snapshot_create(name, i);
2998 * Verify that we cannot create an existing dataset.
3000 VERIFY3U(EEXIST, ==,
3001 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3004 * Verify that we can hold an objset that is also owned.
3006 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3007 dmu_objset_rele(os2, FTAG);
3010 * Verify that we cannot own an objset that is already owned.
3013 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3016 dmu_objset_disown(os, FTAG);
3017 ztest_zd_fini(&zdtmp);
3019 (void) rw_unlock(&zs->zs_name_lock);
3023 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3026 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3028 ztest_shared_t *zs = ztest_shared;
3030 (void) rw_rdlock(&zs->zs_name_lock);
3031 (void) ztest_snapshot_destroy(zd->zd_name, id);
3032 (void) ztest_snapshot_create(zd->zd_name, id);
3033 (void) rw_unlock(&zs->zs_name_lock);
3037 * Cleanup non-standard snapshots and clones.
3040 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3042 char snap1name[MAXNAMELEN];
3043 char clone1name[MAXNAMELEN];
3044 char snap2name[MAXNAMELEN];
3045 char clone2name[MAXNAMELEN];
3046 char snap3name[MAXNAMELEN];
3049 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3050 osname, (u_longlong_t)id);
3051 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3052 osname, (u_longlong_t)id);
3053 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3054 clone1name, (u_longlong_t)id);
3055 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3056 osname, (u_longlong_t)id);
3057 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3058 clone1name, (u_longlong_t)id);
3060 error = dmu_objset_destroy(clone2name, B_FALSE);
3061 if (error && error != ENOENT)
3062 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3063 error = dmu_objset_destroy(snap3name, B_FALSE);
3064 if (error && error != ENOENT)
3065 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3066 error = dmu_objset_destroy(snap2name, B_FALSE);
3067 if (error && error != ENOENT)
3068 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3069 error = dmu_objset_destroy(clone1name, B_FALSE);
3070 if (error && error != ENOENT)
3071 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3072 error = dmu_objset_destroy(snap1name, B_FALSE);
3073 if (error && error != ENOENT)
3074 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3078 * Verify dsl_dataset_promote handles EBUSY
3081 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3083 ztest_shared_t *zs = ztest_shared;
3086 char snap1name[MAXNAMELEN];
3087 char clone1name[MAXNAMELEN];
3088 char snap2name[MAXNAMELEN];
3089 char clone2name[MAXNAMELEN];
3090 char snap3name[MAXNAMELEN];
3091 char *osname = zd->zd_name;
3094 (void) rw_rdlock(&zs->zs_name_lock);
3096 ztest_dsl_dataset_cleanup(osname, id);
3098 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3099 osname, (u_longlong_t)id);
3100 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3101 osname, (u_longlong_t)id);
3102 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3103 clone1name, (u_longlong_t)id);
3104 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3105 osname, (u_longlong_t)id);
3106 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3107 clone1name, (u_longlong_t)id);
3109 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3110 NULL, NULL, B_FALSE, B_FALSE, -1);
3111 if (error && error != EEXIST) {
3112 if (error == ENOSPC) {
3113 ztest_record_enospc(FTAG);
3116 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3119 error = dmu_objset_hold(snap1name, FTAG, &clone);
3121 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3123 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3124 dmu_objset_rele(clone, FTAG);
3126 if (error == ENOSPC) {
3127 ztest_record_enospc(FTAG);
3130 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3133 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3134 NULL, NULL, B_FALSE, B_FALSE, -1);
3135 if (error && error != EEXIST) {
3136 if (error == ENOSPC) {
3137 ztest_record_enospc(FTAG);
3140 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3143 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3144 NULL, NULL, B_FALSE, B_FALSE, -1);
3145 if (error && error != EEXIST) {
3146 if (error == ENOSPC) {
3147 ztest_record_enospc(FTAG);
3150 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3153 error = dmu_objset_hold(snap3name, FTAG, &clone);
3155 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3157 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3158 dmu_objset_rele(clone, FTAG);
3160 if (error == ENOSPC) {
3161 ztest_record_enospc(FTAG);
3164 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3167 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3169 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3170 error = dsl_dataset_promote(clone2name, NULL);
3172 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3174 dsl_dataset_disown(ds, FTAG);
3177 ztest_dsl_dataset_cleanup(osname, id);
3179 (void) rw_unlock(&zs->zs_name_lock);
3183 * Verify that dmu_object_{alloc,free} work as expected.
3186 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3189 int batchsize = sizeof (od) / sizeof (od[0]);
3192 for (b = 0; b < batchsize; b++)
3193 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3196 * Destroy the previous batch of objects, create a new batch,
3197 * and do some I/O on the new objects.
3199 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3202 while (ztest_random(4 * batchsize) != 0)
3203 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3204 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3208 * Verify that dmu_{read,write} work as expected.
3211 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3213 objset_t *os = zd->zd_os;
3216 int i, freeit, error;
3218 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3219 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3220 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3221 uint64_t regions = 997;
3222 uint64_t stride = 123456789ULL;
3223 uint64_t width = 40;
3224 int free_percent = 5;
3227 * This test uses two objects, packobj and bigobj, that are always
3228 * updated together (i.e. in the same tx) so that their contents are
3229 * in sync and can be compared. Their contents relate to each other
3230 * in a simple way: packobj is a dense array of 'bufwad' structures,
3231 * while bigobj is a sparse array of the same bufwads. Specifically,
3232 * for any index n, there are three bufwads that should be identical:
3234 * packobj, at offset n * sizeof (bufwad_t)
3235 * bigobj, at the head of the nth chunk
3236 * bigobj, at the tail of the nth chunk
3238 * The chunk size is arbitrary. It doesn't have to be a power of two,
3239 * and it doesn't have any relation to the object blocksize.
3240 * The only requirement is that it can hold at least two bufwads.
3242 * Normally, we write the bufwad to each of these locations.
3243 * However, free_percent of the time we instead write zeroes to
3244 * packobj and perform a dmu_free_range() on bigobj. By comparing
3245 * bigobj to packobj, we can verify that the DMU is correctly
3246 * tracking which parts of an object are allocated and free,
3247 * and that the contents of the allocated blocks are correct.
3251 * Read the directory info. If it's the first time, set things up.
3253 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3254 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3256 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3259 bigobj = od[0].od_object;
3260 packobj = od[1].od_object;
3261 chunksize = od[0].od_gen;
3262 ASSERT(chunksize == od[1].od_gen);
3265 * Prefetch a random chunk of the big object.
3266 * Our aim here is to get some async reads in flight
3267 * for blocks that we may free below; the DMU should
3268 * handle this race correctly.
3270 n = ztest_random(regions) * stride + ztest_random(width);
3271 s = 1 + ztest_random(2 * width - 1);
3272 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3275 * Pick a random index and compute the offsets into packobj and bigobj.
3277 n = ztest_random(regions) * stride + ztest_random(width);
3278 s = 1 + ztest_random(width - 1);
3280 packoff = n * sizeof (bufwad_t);
3281 packsize = s * sizeof (bufwad_t);
3283 bigoff = n * chunksize;
3284 bigsize = s * chunksize;
3286 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3287 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3290 * free_percent of the time, free a range of bigobj rather than
3293 freeit = (ztest_random(100) < free_percent);
3296 * Read the current contents of our objects.
3298 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3300 ASSERT3U(error, ==, 0);
3301 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3303 ASSERT3U(error, ==, 0);
3306 * Get a tx for the mods to both packobj and bigobj.
3308 tx = dmu_tx_create(os);
3310 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3313 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3315 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3317 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3319 umem_free(packbuf, packsize);
3320 umem_free(bigbuf, bigsize);
3324 dmu_object_set_checksum(os, bigobj,
3325 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3327 dmu_object_set_compress(os, bigobj,
3328 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3331 * For each index from n to n + s, verify that the existing bufwad
3332 * in packobj matches the bufwads at the head and tail of the
3333 * corresponding chunk in bigobj. Then update all three bufwads
3334 * with the new values we want to write out.
3336 for (i = 0; i < s; i++) {
3338 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3340 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3342 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3344 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3345 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3347 if (pack->bw_txg > txg)
3348 fatal(0, "future leak: got %llx, open txg is %llx",
3351 if (pack->bw_data != 0 && pack->bw_index != n + i)
3352 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3353 pack->bw_index, n, i);
3355 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3356 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3358 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3359 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3362 bzero(pack, sizeof (bufwad_t));
3364 pack->bw_index = n + i;
3366 pack->bw_data = 1 + ztest_random(-2ULL);
3373 * We've verified all the old bufwads, and made new ones.
3374 * Now write them out.
3376 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3379 if (zopt_verbose >= 7) {
3380 (void) printf("freeing offset %llx size %llx"
3382 (u_longlong_t)bigoff,
3383 (u_longlong_t)bigsize,
3386 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3388 if (zopt_verbose >= 7) {
3389 (void) printf("writing offset %llx size %llx"
3391 (u_longlong_t)bigoff,
3392 (u_longlong_t)bigsize,
3395 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3401 * Sanity check the stuff we just wrote.
3404 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3405 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3407 VERIFY(0 == dmu_read(os, packobj, packoff,
3408 packsize, packcheck, DMU_READ_PREFETCH));
3409 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3410 bigsize, bigcheck, DMU_READ_PREFETCH));
3412 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3413 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3415 umem_free(packcheck, packsize);
3416 umem_free(bigcheck, bigsize);
3419 umem_free(packbuf, packsize);
3420 umem_free(bigbuf, bigsize);
3424 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3425 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3433 * For each index from n to n + s, verify that the existing bufwad
3434 * in packobj matches the bufwads at the head and tail of the
3435 * corresponding chunk in bigobj. Then update all three bufwads
3436 * with the new values we want to write out.
3438 for (i = 0; i < s; i++) {
3440 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3442 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3444 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3446 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3447 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3449 if (pack->bw_txg > txg)
3450 fatal(0, "future leak: got %llx, open txg is %llx",
3453 if (pack->bw_data != 0 && pack->bw_index != n + i)
3454 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3455 pack->bw_index, n, i);
3457 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3458 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3460 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3461 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3463 pack->bw_index = n + i;
3465 pack->bw_data = 1 + ztest_random(-2ULL);
3473 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3475 objset_t *os = zd->zd_os;
3481 bufwad_t *packbuf, *bigbuf;
3482 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3483 uint64_t blocksize = ztest_random_blocksize();
3484 uint64_t chunksize = blocksize;
3485 uint64_t regions = 997;
3486 uint64_t stride = 123456789ULL;
3488 dmu_buf_t *bonus_db;
3489 arc_buf_t **bigbuf_arcbufs;
3490 dmu_object_info_t doi;
3493 * This test uses two objects, packobj and bigobj, that are always
3494 * updated together (i.e. in the same tx) so that their contents are
3495 * in sync and can be compared. Their contents relate to each other
3496 * in a simple way: packobj is a dense array of 'bufwad' structures,
3497 * while bigobj is a sparse array of the same bufwads. Specifically,
3498 * for any index n, there are three bufwads that should be identical:
3500 * packobj, at offset n * sizeof (bufwad_t)
3501 * bigobj, at the head of the nth chunk
3502 * bigobj, at the tail of the nth chunk
3504 * The chunk size is set equal to bigobj block size so that
3505 * dmu_assign_arcbuf() can be tested for object updates.
3509 * Read the directory info. If it's the first time, set things up.
3511 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3512 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3514 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3517 bigobj = od[0].od_object;
3518 packobj = od[1].od_object;
3519 blocksize = od[0].od_blocksize;
3520 chunksize = blocksize;
3521 ASSERT(chunksize == od[1].od_gen);
3523 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3524 VERIFY(ISP2(doi.doi_data_block_size));
3525 VERIFY(chunksize == doi.doi_data_block_size);
3526 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3529 * Pick a random index and compute the offsets into packobj and bigobj.
3531 n = ztest_random(regions) * stride + ztest_random(width);
3532 s = 1 + ztest_random(width - 1);
3534 packoff = n * sizeof (bufwad_t);
3535 packsize = s * sizeof (bufwad_t);
3537 bigoff = n * chunksize;
3538 bigsize = s * chunksize;
3540 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3541 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3543 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3545 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3548 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3549 * Iteration 1 test zcopy to already referenced dbufs.
3550 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3551 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3552 * Iteration 4 test zcopy when dbuf is no longer dirty.
3553 * Iteration 5 test zcopy when it can't be done.
3554 * Iteration 6 one more zcopy write.
3556 for (i = 0; i < 7; i++) {
3561 * In iteration 5 (i == 5) use arcbufs
3562 * that don't match bigobj blksz to test
3563 * dmu_assign_arcbuf() when it can't directly
3564 * assign an arcbuf to a dbuf.
3566 for (j = 0; j < s; j++) {
3569 dmu_request_arcbuf(bonus_db, chunksize);
3571 bigbuf_arcbufs[2 * j] =
3572 dmu_request_arcbuf(bonus_db, chunksize / 2);
3573 bigbuf_arcbufs[2 * j + 1] =
3574 dmu_request_arcbuf(bonus_db, chunksize / 2);
3579 * Get a tx for the mods to both packobj and bigobj.
3581 tx = dmu_tx_create(os);
3583 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3584 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3586 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3588 umem_free(packbuf, packsize);
3589 umem_free(bigbuf, bigsize);
3590 for (j = 0; j < s; j++) {
3592 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3595 bigbuf_arcbufs[2 * j]);
3597 bigbuf_arcbufs[2 * j + 1]);
3600 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3601 dmu_buf_rele(bonus_db, FTAG);
3606 * 50% of the time don't read objects in the 1st iteration to
3607 * test dmu_assign_arcbuf() for the case when there're no
3608 * existing dbufs for the specified offsets.
3610 if (i != 0 || ztest_random(2) != 0) {
3611 error = dmu_read(os, packobj, packoff,
3612 packsize, packbuf, DMU_READ_PREFETCH);
3613 ASSERT3U(error, ==, 0);
3614 error = dmu_read(os, bigobj, bigoff, bigsize,
3615 bigbuf, DMU_READ_PREFETCH);
3616 ASSERT3U(error, ==, 0);
3618 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3622 * We've verified all the old bufwads, and made new ones.
3623 * Now write them out.
3625 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3626 if (zopt_verbose >= 7) {
3627 (void) printf("writing offset %llx size %llx"
3629 (u_longlong_t)bigoff,
3630 (u_longlong_t)bigsize,
3633 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3636 bcopy((caddr_t)bigbuf + (off - bigoff),
3637 bigbuf_arcbufs[j]->b_data, chunksize);
3639 bcopy((caddr_t)bigbuf + (off - bigoff),
3640 bigbuf_arcbufs[2 * j]->b_data,
3642 bcopy((caddr_t)bigbuf + (off - bigoff) +
3644 bigbuf_arcbufs[2 * j + 1]->b_data,
3649 VERIFY(dmu_buf_hold(os, bigobj, off,
3650 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3653 dmu_assign_arcbuf(bonus_db, off,
3654 bigbuf_arcbufs[j], tx);
3656 dmu_assign_arcbuf(bonus_db, off,
3657 bigbuf_arcbufs[2 * j], tx);
3658 dmu_assign_arcbuf(bonus_db,
3659 off + chunksize / 2,
3660 bigbuf_arcbufs[2 * j + 1], tx);
3663 dmu_buf_rele(dbt, FTAG);
3669 * Sanity check the stuff we just wrote.
3672 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3673 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3675 VERIFY(0 == dmu_read(os, packobj, packoff,
3676 packsize, packcheck, DMU_READ_PREFETCH));
3677 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3678 bigsize, bigcheck, DMU_READ_PREFETCH));
3680 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3681 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3683 umem_free(packcheck, packsize);
3684 umem_free(bigcheck, bigsize);
3687 txg_wait_open(dmu_objset_pool(os), 0);
3688 } else if (i == 3) {
3689 txg_wait_synced(dmu_objset_pool(os), 0);
3693 dmu_buf_rele(bonus_db, FTAG);
3694 umem_free(packbuf, packsize);
3695 umem_free(bigbuf, bigsize);
3696 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3701 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3704 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3705 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3708 * Have multiple threads write to large offsets in an object
3709 * to verify that parallel writes to an object -- even to the
3710 * same blocks within the object -- doesn't cause any trouble.
3712 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3714 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3717 while (ztest_random(10) != 0)
3718 ztest_io(zd, od[0].od_object, offset);
3722 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3725 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3726 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3727 uint64_t count = ztest_random(20) + 1;
3728 uint64_t blocksize = ztest_random_blocksize();
3731 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3733 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3736 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
3739 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
3741 data = umem_zalloc(blocksize, UMEM_NOFAIL);
3743 while (ztest_random(count) != 0) {
3744 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3745 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3748 while (ztest_random(4) != 0)
3749 ztest_io(zd, od[0].od_object, randoff);
3752 umem_free(data, blocksize);
3756 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3758 #define ZTEST_ZAP_MIN_INTS 1
3759 #define ZTEST_ZAP_MAX_INTS 4
3760 #define ZTEST_ZAP_MAX_PROPS 1000
3763 ztest_zap(ztest_ds_t *zd, uint64_t id)
3765 objset_t *os = zd->zd_os;
3768 uint64_t txg, last_txg;
3769 uint64_t value[ZTEST_ZAP_MAX_INTS];
3770 uint64_t zl_ints, zl_intsize, prop;
3773 char propname[100], txgname[100];
3775 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3777 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3779 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3782 object = od[0].od_object;
3785 * Generate a known hash collision, and verify that
3786 * we can lookup and remove both entries.
3788 tx = dmu_tx_create(os);
3789 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3790 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3793 for (i = 0; i < 2; i++) {
3795 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3798 for (i = 0; i < 2; i++) {
3799 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3800 sizeof (uint64_t), 1, &value[i], tx));
3802 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3803 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3804 ASSERT3U(zl_ints, ==, 1);
3806 for (i = 0; i < 2; i++) {
3807 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3812 * Generate a buch of random entries.
3814 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3816 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3817 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3818 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3819 bzero(value, sizeof (value));
3823 * If these zap entries already exist, validate their contents.
3825 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3827 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3828 ASSERT3U(zl_ints, ==, 1);
3830 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3831 zl_ints, &last_txg) == 0);
3833 VERIFY(zap_length(os, object, propname, &zl_intsize,
3836 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3837 ASSERT3U(zl_ints, ==, ints);
3839 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3840 zl_ints, value) == 0);
3842 for (i = 0; i < ints; i++) {
3843 ASSERT3U(value[i], ==, last_txg + object + i);
3846 ASSERT3U(error, ==, ENOENT);
3850 * Atomically update two entries in our zap object.
3851 * The first is named txg_%llu, and contains the txg
3852 * in which the property was last updated. The second
3853 * is named prop_%llu, and the nth element of its value
3854 * should be txg + object + n.
3856 tx = dmu_tx_create(os);
3857 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3858 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3863 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3865 for (i = 0; i < ints; i++)
3866 value[i] = txg + object + i;
3868 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3870 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3876 * Remove a random pair of entries.
3878 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3879 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3880 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3882 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3884 if (error == ENOENT)
3887 ASSERT3U(error, ==, 0);
3889 tx = dmu_tx_create(os);
3890 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3891 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3894 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3895 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3900 * Testcase to test the upgrading of a microzap to fatzap.
3903 ztest_fzap(ztest_ds_t *zd, uint64_t id)
3905 objset_t *os = zd->zd_os;
3907 uint64_t object, txg;
3910 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3912 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3915 object = od[0].od_object;
3918 * Add entries to this ZAP and make sure it spills over
3919 * and gets upgraded to a fatzap. Also, since we are adding
3920 * 2050 entries we should see ptrtbl growth and leaf-block split.
3922 for (i = 0; i < 2050; i++) {
3923 char name[MAXNAMELEN];
3928 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
3929 (u_longlong_t)id, (u_longlong_t)value);
3931 tx = dmu_tx_create(os);
3932 dmu_tx_hold_zap(tx, object, B_TRUE, name);
3933 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3936 error = zap_add(os, object, name, sizeof (uint64_t), 1,
3938 ASSERT(error == 0 || error == EEXIST);
3945 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3947 objset_t *os = zd->zd_os;
3949 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3951 int i, namelen, error;
3952 int micro = ztest_random(2);
3953 char name[20], string_value[20];
3956 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3958 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3961 object = od[0].od_object;
3964 * Generate a random name of the form 'xxx.....' where each
3965 * x is a random printable character and the dots are dots.
3966 * There are 94 such characters, and the name length goes from
3967 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3969 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3971 for (i = 0; i < 3; i++)
3972 name[i] = '!' + ztest_random('~' - '!' + 1);
3973 for (; i < namelen - 1; i++)
3977 if ((namelen & 1) || micro) {
3978 wsize = sizeof (txg);
3984 data = string_value;
3988 VERIFY(zap_count(os, object, &count) == 0);
3989 ASSERT(count != -1ULL);
3992 * Select an operation: length, lookup, add, update, remove.
3994 i = ztest_random(5);
3997 tx = dmu_tx_create(os);
3998 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3999 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4002 bcopy(name, string_value, namelen);
4006 bzero(string_value, namelen);
4012 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4014 ASSERT3U(wsize, ==, zl_wsize);
4015 ASSERT3U(wc, ==, zl_wc);
4017 ASSERT3U(error, ==, ENOENT);
4022 error = zap_lookup(os, object, name, wsize, wc, data);
4024 if (data == string_value &&
4025 bcmp(name, data, namelen) != 0)
4026 fatal(0, "name '%s' != val '%s' len %d",
4027 name, data, namelen);
4029 ASSERT3U(error, ==, ENOENT);
4034 error = zap_add(os, object, name, wsize, wc, data, tx);
4035 ASSERT(error == 0 || error == EEXIST);
4039 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4043 error = zap_remove(os, object, name, tx);
4044 ASSERT(error == 0 || error == ENOENT);
4053 * Commit callback data.
4055 typedef struct ztest_cb_data {
4056 list_node_t zcd_node;
4058 int zcd_expected_err;
4059 boolean_t zcd_added;
4060 boolean_t zcd_called;
4064 /* This is the actual commit callback function */
4066 ztest_commit_callback(void *arg, int error)
4068 ztest_cb_data_t *data = arg;
4069 uint64_t synced_txg;
4071 VERIFY(data != NULL);
4072 VERIFY3S(data->zcd_expected_err, ==, error);
4073 VERIFY(!data->zcd_called);
4075 synced_txg = spa_last_synced_txg(data->zcd_spa);
4076 if (data->zcd_txg > synced_txg)
4077 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4078 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4081 data->zcd_called = B_TRUE;
4083 if (error == ECANCELED) {
4084 ASSERT3U(data->zcd_txg, ==, 0);
4085 ASSERT(!data->zcd_added);
4088 * The private callback data should be destroyed here, but
4089 * since we are going to check the zcd_called field after
4090 * dmu_tx_abort(), we will destroy it there.
4095 /* Was this callback added to the global callback list? */
4096 if (!data->zcd_added)
4099 ASSERT3U(data->zcd_txg, !=, 0);
4101 /* Remove our callback from the list */
4102 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4103 list_remove(&zcl.zcl_callbacks, data);
4104 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4107 umem_free(data, sizeof (ztest_cb_data_t));
4110 /* Allocate and initialize callback data structure */
4111 static ztest_cb_data_t *
4112 ztest_create_cb_data(objset_t *os, uint64_t txg)
4114 ztest_cb_data_t *cb_data;
4116 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4118 cb_data->zcd_txg = txg;
4119 cb_data->zcd_spa = dmu_objset_spa(os);
4125 * If a number of txgs equal to this threshold have been created after a commit
4126 * callback has been registered but not called, then we assume there is an
4127 * implementation bug.
4129 #define ZTEST_COMMIT_CALLBACK_THRESH (TXG_CONCURRENT_STATES + 2)
4132 * Commit callback test.
4135 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4137 objset_t *os = zd->zd_os;
4140 ztest_cb_data_t *cb_data[3], *tmp_cb;
4141 uint64_t old_txg, txg;
4144 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4146 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4149 tx = dmu_tx_create(os);
4151 cb_data[0] = ztest_create_cb_data(os, 0);
4152 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4154 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4156 /* Every once in a while, abort the transaction on purpose */
4157 if (ztest_random(100) == 0)
4161 error = dmu_tx_assign(tx, TXG_NOWAIT);
4163 txg = error ? 0 : dmu_tx_get_txg(tx);
4165 cb_data[0]->zcd_txg = txg;
4166 cb_data[1] = ztest_create_cb_data(os, txg);
4167 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4171 * It's not a strict requirement to call the registered
4172 * callbacks from inside dmu_tx_abort(), but that's what
4173 * it's supposed to happen in the current implementation
4174 * so we will check for that.
4176 for (i = 0; i < 2; i++) {
4177 cb_data[i]->zcd_expected_err = ECANCELED;
4178 VERIFY(!cb_data[i]->zcd_called);
4183 for (i = 0; i < 2; i++) {
4184 VERIFY(cb_data[i]->zcd_called);
4185 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4191 cb_data[2] = ztest_create_cb_data(os, txg);
4192 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4195 * Read existing data to make sure there isn't a future leak.
4197 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4198 &old_txg, DMU_READ_PREFETCH));
4201 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4204 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4206 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4209 * Since commit callbacks don't have any ordering requirement and since
4210 * it is theoretically possible for a commit callback to be called
4211 * after an arbitrary amount of time has elapsed since its txg has been
4212 * synced, it is difficult to reliably determine whether a commit
4213 * callback hasn't been called due to high load or due to a flawed
4216 * In practice, we will assume that if after a certain number of txgs a
4217 * commit callback hasn't been called, then most likely there's an
4218 * implementation bug..
4220 tmp_cb = list_head(&zcl.zcl_callbacks);
4221 if (tmp_cb != NULL &&
4222 tmp_cb->zcd_txg > txg - ZTEST_COMMIT_CALLBACK_THRESH) {
4223 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4224 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4228 * Let's find the place to insert our callbacks.
4230 * Even though the list is ordered by txg, it is possible for the
4231 * insertion point to not be the end because our txg may already be
4232 * quiescing at this point and other callbacks in the open txg
4233 * (from other objsets) may have sneaked in.
4235 tmp_cb = list_tail(&zcl.zcl_callbacks);
4236 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4237 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4239 /* Add the 3 callbacks to the list */
4240 for (i = 0; i < 3; i++) {
4242 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4244 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4247 cb_data[i]->zcd_added = B_TRUE;
4248 VERIFY(!cb_data[i]->zcd_called);
4250 tmp_cb = cb_data[i];
4253 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4260 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4262 zfs_prop_t proplist[] = {
4264 ZFS_PROP_COMPRESSION,
4268 ztest_shared_t *zs = ztest_shared;
4271 (void) rw_rdlock(&zs->zs_name_lock);
4273 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4274 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4275 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4277 (void) rw_unlock(&zs->zs_name_lock);
4282 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4284 ztest_shared_t *zs = ztest_shared;
4285 nvlist_t *props = NULL;
4287 (void) rw_rdlock(&zs->zs_name_lock);
4289 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4290 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4292 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4294 if (zopt_verbose >= 6)
4295 dump_nvlist(props, 4);
4299 (void) rw_unlock(&zs->zs_name_lock);
4303 * Test snapshot hold/release and deferred destroy.
4306 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4309 objset_t *os = zd->zd_os;
4313 char clonename[100];
4315 char osname[MAXNAMELEN];
4317 (void) rw_rdlock(&ztest_shared->zs_name_lock);
4319 dmu_objset_name(os, osname);
4321 (void) snprintf(snapname, 100, "sh1_%llu", id);
4322 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4323 (void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4324 (void) snprintf(tag, 100, "%tag_%llu", id);
4327 * Clean up from any previous run.
4329 (void) dmu_objset_destroy(clonename, B_FALSE);
4330 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4331 (void) dmu_objset_destroy(fullname, B_FALSE);
4334 * Create snapshot, clone it, mark snap for deferred destroy,
4335 * destroy clone, verify snap was also destroyed.
4337 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4340 if (error == ENOSPC) {
4341 ztest_record_enospc("dmu_objset_snapshot");
4344 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4347 error = dmu_objset_hold(fullname, FTAG, &origin);
4349 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4351 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4352 dmu_objset_rele(origin, FTAG);
4354 if (error == ENOSPC) {
4355 ztest_record_enospc("dmu_objset_clone");
4358 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4361 error = dmu_objset_destroy(fullname, B_TRUE);
4363 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4367 error = dmu_objset_destroy(clonename, B_FALSE);
4369 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4371 error = dmu_objset_hold(fullname, FTAG, &origin);
4372 if (error != ENOENT)
4373 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4376 * Create snapshot, add temporary hold, verify that we can't
4377 * destroy a held snapshot, mark for deferred destroy,
4378 * release hold, verify snapshot was destroyed.
4380 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4383 if (error == ENOSPC) {
4384 ztest_record_enospc("dmu_objset_snapshot");
4387 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4390 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4393 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4395 error = dmu_objset_destroy(fullname, B_FALSE);
4396 if (error != EBUSY) {
4397 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4401 error = dmu_objset_destroy(fullname, B_TRUE);
4403 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4407 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4409 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4411 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4414 (void) rw_unlock(&ztest_shared->zs_name_lock);
4418 * Inject random faults into the on-disk data.
4422 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4424 ztest_shared_t *zs = ztest_shared;
4425 spa_t *spa = zs->zs_spa;
4429 uint64_t bad = 0x1990c0ffeedecadeull;
4431 char path0[MAXPATHLEN];
4432 char pathrand[MAXPATHLEN];
4434 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4440 boolean_t islog = B_FALSE;
4442 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4443 maxfaults = MAXFAULTS();
4444 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4445 mirror_save = zs->zs_mirrors;
4446 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4448 ASSERT(leaves >= 1);
4451 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4453 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4455 if (ztest_random(2) == 0) {
4457 * Inject errors on a normal data device or slog device.
4459 top = ztest_random_vdev_top(spa, B_TRUE);
4460 leaf = ztest_random(leaves) + zs->zs_splits;
4463 * Generate paths to the first leaf in this top-level vdev,
4464 * and to the random leaf we selected. We'll induce transient
4465 * write failures and random online/offline activity on leaf 0,
4466 * and we'll write random garbage to the randomly chosen leaf.
4468 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
4469 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4470 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4471 zopt_dir, zopt_pool, top * leaves + leaf);
4473 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4474 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4477 if (vd0 != NULL && maxfaults != 1) {
4479 * Make vd0 explicitly claim to be unreadable,
4480 * or unwriteable, or reach behind its back
4481 * and close the underlying fd. We can do this if
4482 * maxfaults == 0 because we'll fail and reexecute,
4483 * and we can do it if maxfaults >= 2 because we'll
4484 * have enough redundancy. If maxfaults == 1, the
4485 * combination of this with injection of random data
4486 * corruption below exceeds the pool's fault tolerance.
4488 vdev_file_t *vf = vd0->vdev_tsd;
4490 if (vf != NULL && ztest_random(3) == 0) {
4491 (void) close(vf->vf_vnode->v_fd);
4492 vf->vf_vnode->v_fd = -1;
4493 } else if (ztest_random(2) == 0) {
4494 vd0->vdev_cant_read = B_TRUE;
4496 vd0->vdev_cant_write = B_TRUE;
4498 guid0 = vd0->vdev_guid;
4502 * Inject errors on an l2cache device.
4504 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4506 if (sav->sav_count == 0) {
4507 spa_config_exit(spa, SCL_STATE, FTAG);
4510 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4511 guid0 = vd0->vdev_guid;
4512 (void) strcpy(path0, vd0->vdev_path);
4513 (void) strcpy(pathrand, vd0->vdev_path);
4517 maxfaults = INT_MAX; /* no limit on cache devices */
4520 spa_config_exit(spa, SCL_STATE, FTAG);
4523 * If we can tolerate two or more faults, or we're dealing
4524 * with a slog, randomly online/offline vd0.
4526 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4527 if (ztest_random(10) < 6) {
4528 int flags = (ztest_random(2) == 0 ?
4529 ZFS_OFFLINE_TEMPORARY : 0);
4532 * We have to grab the zs_name_lock as writer to
4533 * prevent a race between offlining a slog and
4534 * destroying a dataset. Offlining the slog will
4535 * grab a reference on the dataset which may cause
4536 * dmu_objset_destroy() to fail with EBUSY thus
4537 * leaving the dataset in an inconsistent state.
4540 (void) rw_wrlock(&ztest_shared->zs_name_lock);
4542 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4545 (void) rw_unlock(&ztest_shared->zs_name_lock);
4547 (void) vdev_online(spa, guid0, 0, NULL);
4555 * We have at least single-fault tolerance, so inject data corruption.
4557 fd = open(pathrand, O_RDWR);
4559 if (fd == -1) /* we hit a gap in the device namespace */
4562 fsize = lseek(fd, 0, SEEK_END);
4564 while (--iters != 0) {
4565 offset = ztest_random(fsize / (leaves << bshift)) *
4566 (leaves << bshift) + (leaf << bshift) +
4567 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4569 if (offset >= fsize)
4572 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4573 if (mirror_save != zs->zs_mirrors) {
4574 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4579 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4580 fatal(1, "can't inject bad word at 0x%llx in %s",
4583 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4585 if (zopt_verbose >= 7)
4586 (void) printf("injected bad word into %s,"
4587 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4594 * Verify that DDT repair works as expected.
4597 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4599 ztest_shared_t *zs = ztest_shared;
4600 spa_t *spa = zs->zs_spa;
4601 objset_t *os = zd->zd_os;
4603 uint64_t object, blocksize, txg, pattern, psize;
4604 enum zio_checksum checksum = spa_dedup_checksum(spa);
4609 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4612 blocksize = ztest_random_blocksize();
4613 blocksize = MIN(blocksize, 2048); /* because we write so many */
4615 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4617 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4621 * Take the name lock as writer to prevent anyone else from changing
4622 * the pool and dataset properies we need to maintain during this test.
4624 (void) rw_wrlock(&zs->zs_name_lock);
4626 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4628 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4630 (void) rw_unlock(&zs->zs_name_lock);
4634 object = od[0].od_object;
4635 blocksize = od[0].od_blocksize;
4636 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4638 ASSERT(object != 0);
4640 tx = dmu_tx_create(os);
4641 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4642 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4644 (void) rw_unlock(&zs->zs_name_lock);
4649 * Write all the copies of our block.
4651 for (i = 0; i < copies; i++) {
4652 uint64_t offset = i * blocksize;
4653 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4654 DMU_READ_NO_PREFETCH) == 0);
4655 ASSERT(db->db_offset == offset);
4656 ASSERT(db->db_size == blocksize);
4657 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4658 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4659 dmu_buf_will_fill(db, tx);
4660 ztest_pattern_set(db->db_data, db->db_size, pattern);
4661 dmu_buf_rele(db, FTAG);
4665 txg_wait_synced(spa_get_dsl(spa), txg);
4668 * Find out what block we got.
4670 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4671 DMU_READ_NO_PREFETCH) == 0);
4672 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4673 dmu_buf_rele(db, FTAG);
4676 * Damage the block. Dedup-ditto will save us when we read it later.
4678 psize = BP_GET_PSIZE(&blk);
4679 buf = zio_buf_alloc(psize);
4680 ztest_pattern_set(buf, psize, ~pattern);
4682 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4683 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4684 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4686 zio_buf_free(buf, psize);
4688 (void) rw_unlock(&zs->zs_name_lock);
4696 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4698 ztest_shared_t *zs = ztest_shared;
4699 spa_t *spa = zs->zs_spa;
4701 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4702 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4703 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4707 * Rename the pool to a different name and then rename it back.
4711 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4713 ztest_shared_t *zs = ztest_shared;
4714 char *oldname, *newname;
4717 (void) rw_wrlock(&zs->zs_name_lock);
4719 oldname = zs->zs_pool;
4720 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4721 (void) strcpy(newname, oldname);
4722 (void) strcat(newname, "_tmp");
4727 VERIFY3U(0, ==, spa_rename(oldname, newname));
4730 * Try to open it under the old name, which shouldn't exist
4732 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4735 * Open it under the new name and make sure it's still the same spa_t.
4737 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4739 ASSERT(spa == zs->zs_spa);
4740 spa_close(spa, FTAG);
4743 * Rename it back to the original
4745 VERIFY3U(0, ==, spa_rename(newname, oldname));
4748 * Make sure it can still be opened
4750 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4752 ASSERT(spa == zs->zs_spa);
4753 spa_close(spa, FTAG);
4755 umem_free(newname, strlen(newname) + 1);
4757 (void) rw_unlock(&zs->zs_name_lock);
4761 * Verify pool integrity by running zdb.
4764 ztest_run_zdb(char *pool)
4767 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4775 (void) realpath(getexecname(), zdb);
4777 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4778 bin = strstr(zdb, "/usr/bin/");
4779 ztest = strstr(bin, "/ztest");
4781 isalen = ztest - isa;
4785 "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
4788 zopt_verbose >= 3 ? "s" : "",
4789 zopt_verbose >= 4 ? "v" : "",
4794 if (zopt_verbose >= 5)
4795 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4797 fp = popen(zdb, "r");
4799 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4800 if (zopt_verbose >= 3)
4801 (void) printf("%s", zbuf);
4803 status = pclose(fp);
4808 ztest_dump_core = 0;
4809 if (WIFEXITED(status))
4810 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4812 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4816 ztest_walk_pool_directory(char *header)
4820 if (zopt_verbose >= 6)
4821 (void) printf("%s\n", header);
4823 mutex_enter(&spa_namespace_lock);
4824 while ((spa = spa_next(spa)) != NULL)
4825 if (zopt_verbose >= 6)
4826 (void) printf("\t%s\n", spa_name(spa));
4827 mutex_exit(&spa_namespace_lock);
4831 ztest_spa_import_export(char *oldname, char *newname)
4833 nvlist_t *config, *newconfig;
4837 if (zopt_verbose >= 4) {
4838 (void) printf("import/export: old = %s, new = %s\n",
4843 * Clean up from previous runs.
4845 (void) spa_destroy(newname);
4848 * Get the pool's configuration and guid.
4850 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4853 * Kick off a scrub to tickle scrub/export races.
4855 if (ztest_random(2) == 0)
4856 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4858 pool_guid = spa_guid(spa);
4859 spa_close(spa, FTAG);
4861 ztest_walk_pool_directory("pools before export");
4866 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4868 ztest_walk_pool_directory("pools after export");
4873 newconfig = spa_tryimport(config);
4874 ASSERT(newconfig != NULL);
4875 nvlist_free(newconfig);
4878 * Import it under the new name.
4880 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
4882 ztest_walk_pool_directory("pools after import");
4885 * Try to import it again -- should fail with EEXIST.
4887 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
4890 * Try to import it under a different name -- should fail with EEXIST.
4892 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
4895 * Verify that the pool is no longer visible under the old name.
4897 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4900 * Verify that we can open and close the pool using the new name.
4902 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4903 ASSERT(pool_guid == spa_guid(spa));
4904 spa_close(spa, FTAG);
4906 nvlist_free(config);
4910 ztest_resume(spa_t *spa)
4912 if (spa_suspended(spa) && zopt_verbose >= 6)
4913 (void) printf("resuming from suspended state\n");
4914 spa_vdev_state_enter(spa, SCL_NONE);
4915 vdev_clear(spa, NULL);
4916 (void) spa_vdev_state_exit(spa, NULL, 0);
4917 (void) zio_resume(spa);
4921 ztest_resume_thread(void *arg)
4925 while (!ztest_exiting) {
4926 if (spa_suspended(spa))
4928 (void) poll(NULL, 0, 100);
4934 ztest_deadman_thread(void *arg)
4936 ztest_shared_t *zs = arg;
4940 delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4942 (void) poll(NULL, 0, (int)(1000 * delta));
4944 fatal(0, "failed to complete within %d seconds of deadline", grace);
4950 ztest_execute(ztest_info_t *zi, uint64_t id)
4952 ztest_shared_t *zs = ztest_shared;
4953 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4954 hrtime_t functime = gethrtime();
4957 for (i = 0; i < zi->zi_iters; i++)
4958 zi->zi_func(zd, id);
4960 functime = gethrtime() - functime;
4962 atomic_add_64(&zi->zi_call_count, 1);
4963 atomic_add_64(&zi->zi_call_time, functime);
4965 if (zopt_verbose >= 4) {
4967 (void) dladdr((void *)zi->zi_func, &dli);
4968 (void) printf("%6.2f sec in %s\n",
4969 (double)functime / NANOSEC, dli.dli_sname);
4974 ztest_thread(void *arg)
4976 uint64_t id = (uintptr_t)arg;
4977 ztest_shared_t *zs = ztest_shared;
4982 while ((now = gethrtime()) < zs->zs_thread_stop) {
4984 * See if it's time to force a crash.
4986 if (now > zs->zs_thread_kill)
4990 * If we're getting ENOSPC with some regularity, stop.
4992 if (zs->zs_enospc_count > 10)
4996 * Pick a random function to execute.
4998 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
4999 call_next = zi->zi_call_next;
5001 if (now >= call_next &&
5002 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5003 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5004 ztest_execute(zi, id);
5011 ztest_dataset_name(char *dsname, char *pool, int d)
5013 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5017 ztest_dataset_destroy(ztest_shared_t *zs, int d)
5019 char name[MAXNAMELEN];
5022 ztest_dataset_name(name, zs->zs_pool, d);
5024 if (zopt_verbose >= 3)
5025 (void) printf("Destroying %s to free up space\n", name);
5028 * Cleanup any non-standard clones and snapshots. In general,
5029 * ztest thread t operates on dataset (t % zopt_datasets),
5030 * so there may be more than one thing to clean up.
5032 for (t = d; t < zopt_threads; t += zopt_datasets)
5033 ztest_dsl_dataset_cleanup(name, t);
5035 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5036 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5040 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5042 uint64_t usedobjs, dirobjs, scratch;
5045 * ZTEST_DIROBJ is the object directory for the entire dataset.
5046 * Therefore, the number of objects in use should equal the
5047 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5048 * If not, we have an object leak.
5050 * Note that we can only check this in ztest_dataset_open(),
5051 * when the open-context and syncing-context values agree.
5052 * That's because zap_count() returns the open-context value,
5053 * while dmu_objset_space() returns the rootbp fill count.
5055 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5056 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5057 ASSERT3U(dirobjs + 1, ==, usedobjs);
5061 ztest_dataset_open(ztest_shared_t *zs, int d)
5063 ztest_ds_t *zd = &zs->zs_zd[d];
5064 uint64_t committed_seq = zd->zd_seq;
5067 char name[MAXNAMELEN];
5070 ztest_dataset_name(name, zs->zs_pool, d);
5072 (void) rw_rdlock(&zs->zs_name_lock);
5074 error = ztest_dataset_create(name);
5075 if (error == ENOSPC) {
5076 (void) rw_unlock(&zs->zs_name_lock);
5077 ztest_record_enospc(FTAG);
5080 ASSERT(error == 0 || error == EEXIST);
5082 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5083 (void) rw_unlock(&zs->zs_name_lock);
5085 ztest_zd_init(zd, os);
5087 zilog = zd->zd_zilog;
5089 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5090 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5091 fatal(0, "missing log records: claimed %llu < committed %llu",
5092 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5094 ztest_dataset_dirobj_verify(zd);
5096 zil_replay(os, zd, ztest_replay_vector);
5098 ztest_dataset_dirobj_verify(zd);
5100 if (zopt_verbose >= 6)
5101 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5103 (u_longlong_t)zilog->zl_parse_blk_count,
5104 (u_longlong_t)zilog->zl_parse_lr_count,
5105 (u_longlong_t)zilog->zl_replaying_seq);
5107 zilog = zil_open(os, ztest_get_data);
5109 if (zilog->zl_replaying_seq != 0 &&
5110 zilog->zl_replaying_seq < committed_seq)
5111 fatal(0, "missing log records: replayed %llu < committed %llu",
5112 zilog->zl_replaying_seq, committed_seq);
5118 ztest_dataset_close(ztest_shared_t *zs, int d)
5120 ztest_ds_t *zd = &zs->zs_zd[d];
5122 zil_close(zd->zd_zilog);
5123 dmu_objset_rele(zd->zd_os, zd);
5129 * Kick off threads to run tests on all datasets in parallel.
5132 ztest_run(ztest_shared_t *zs)
5136 thread_t resume_tid;
5140 ztest_exiting = B_FALSE;
5143 * Initialize parent/child shared state.
5145 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5146 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5148 zs->zs_thread_start = gethrtime();
5149 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5150 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5151 zs->zs_thread_kill = zs->zs_thread_stop;
5152 if (ztest_random(100) < zopt_killrate)
5153 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5155 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5157 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5158 offsetof(ztest_cb_data_t, zcd_node));
5163 kernel_init(FREAD | FWRITE);
5164 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5167 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5170 * We don't expect the pool to suspend unless maxfaults == 0,
5171 * in which case ztest_fault_inject() temporarily takes away
5172 * the only valid replica.
5174 if (MAXFAULTS() == 0)
5175 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5177 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5180 * Create a thread to periodically resume suspended I/O.
5182 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5186 * Create a deadman thread to abort() if we hang.
5188 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5192 * Verify that we can safely inquire about about any object,
5193 * whether it's allocated or not. To make it interesting,
5194 * we probe a 5-wide window around each power of two.
5195 * This hits all edge cases, including zero and the max.
5197 for (t = 0; t < 64; t++) {
5198 for (d = -5; d <= 5; d++) {
5199 error = dmu_object_info(spa->spa_meta_objset,
5200 (1ULL << t) + d, NULL);
5201 ASSERT(error == 0 || error == ENOENT ||
5207 * If we got any ENOSPC errors on the previous run, destroy something.
5209 if (zs->zs_enospc_count != 0) {
5210 int d = ztest_random(zopt_datasets);
5211 ztest_dataset_destroy(zs, d);
5213 zs->zs_enospc_count = 0;
5215 tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5217 if (zopt_verbose >= 4)
5218 (void) printf("starting main threads...\n");
5221 * Kick off all the tests that run in parallel.
5223 for (t = 0; t < zopt_threads; t++) {
5224 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5226 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5227 THR_BOUND, &tid[t]) == 0);
5231 * Wait for all of the tests to complete. We go in reverse order
5232 * so we don't close datasets while threads are still using them.
5234 for (t = zopt_threads - 1; t >= 0; t--) {
5235 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5236 if (t < zopt_datasets)
5237 ztest_dataset_close(zs, t);
5240 txg_wait_synced(spa_get_dsl(spa), 0);
5242 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5243 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5245 umem_free(tid, zopt_threads * sizeof (thread_t));
5247 /* Kill the resume thread */
5248 ztest_exiting = B_TRUE;
5249 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5253 * Right before closing the pool, kick off a bunch of async I/O;
5254 * spa_close() should wait for it to complete.
5256 for (uint64_t object = 1; object < 50; object++)
5257 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5259 spa_close(spa, FTAG);
5262 * Verify that we can loop over all pools.
5264 mutex_enter(&spa_namespace_lock);
5265 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5266 if (zopt_verbose > 3)
5267 (void) printf("spa_next: found %s\n", spa_name(spa));
5268 mutex_exit(&spa_namespace_lock);
5271 * Verify that we can export the pool and reimport it under a
5274 if (ztest_random(2) == 0) {
5275 char name[MAXNAMELEN];
5276 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5277 ztest_spa_import_export(zs->zs_pool, name);
5278 ztest_spa_import_export(name, zs->zs_pool);
5283 list_destroy(&zcl.zcl_callbacks);
5285 (void) _mutex_destroy(&zcl.zcl_callbacks_lock);
5287 (void) rwlock_destroy(&zs->zs_name_lock);
5288 (void) _mutex_destroy(&zs->zs_vdev_lock);
5292 ztest_freeze(ztest_shared_t *zs)
5294 ztest_ds_t *zd = &zs->zs_zd[0];
5298 if (zopt_verbose >= 3)
5299 (void) printf("testing spa_freeze()...\n");
5301 kernel_init(FREAD | FWRITE);
5302 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5303 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5306 * Force the first log block to be transactionally allocated.
5307 * We have to do this before we freeze the pool -- otherwise
5308 * the log chain won't be anchored.
5310 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5311 ztest_dmu_object_alloc_free(zd, 0);
5312 zil_commit(zd->zd_zilog, 0);
5315 txg_wait_synced(spa_get_dsl(spa), 0);
5318 * Freeze the pool. This stops spa_sync() from doing anything,
5319 * so that the only way to record changes from now on is the ZIL.
5324 * Run tests that generate log records but don't alter the pool config
5325 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5326 * We do a txg_wait_synced() after each iteration to force the txg
5327 * to increase well beyond the last synced value in the uberblock.
5328 * The ZIL should be OK with that.
5330 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5331 ztest_dmu_write_parallel(zd, 0);
5332 ztest_dmu_object_alloc_free(zd, 0);
5333 txg_wait_synced(spa_get_dsl(spa), 0);
5337 * Commit all of the changes we just generated.
5339 zil_commit(zd->zd_zilog, 0);
5340 txg_wait_synced(spa_get_dsl(spa), 0);
5343 * Close our dataset and close the pool.
5345 ztest_dataset_close(zs, 0);
5346 spa_close(spa, FTAG);
5350 * Open and close the pool and dataset to induce log replay.
5352 kernel_init(FREAD | FWRITE);
5353 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5354 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5355 ztest_dataset_close(zs, 0);
5356 spa_close(spa, FTAG);
5361 print_time(hrtime_t t, char *timebuf)
5363 hrtime_t s = t / NANOSEC;
5364 hrtime_t m = s / 60;
5365 hrtime_t h = m / 60;
5366 hrtime_t d = h / 24;
5375 (void) sprintf(timebuf,
5376 "%llud%02lluh%02llum%02llus", d, h, m, s);
5378 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5380 (void) sprintf(timebuf, "%llum%02llus", m, s);
5382 (void) sprintf(timebuf, "%llus", s);
5386 make_random_props(void)
5390 if (ztest_random(2) == 0)
5393 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5394 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5396 (void) printf("props:\n");
5397 dump_nvlist(props, 4);
5403 * Create a storage pool with the given name and initial vdev size.
5404 * Then test spa_freeze() functionality.
5407 ztest_init(ztest_shared_t *zs)
5410 nvlist_t *nvroot, *props;
5412 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5413 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5415 kernel_init(FREAD | FWRITE);
5418 * Create the storage pool.
5420 (void) spa_destroy(zs->zs_pool);
5421 ztest_shared->zs_vdev_next_leaf = 0;
5423 zs->zs_mirrors = zopt_mirrors;
5424 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5425 0, zopt_raidz, zs->zs_mirrors, 1);
5426 props = make_random_props();
5427 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5428 nvlist_free(nvroot);
5430 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5431 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5432 spa_close(spa, FTAG);
5436 ztest_run_zdb(zs->zs_pool);
5440 ztest_run_zdb(zs->zs_pool);
5442 (void) rwlock_destroy(&zs->zs_name_lock);
5443 (void) _mutex_destroy(&zs->zs_vdev_lock);
5447 main(int argc, char **argv)
5459 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5461 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5463 process_options(argc, argv);
5465 /* Override location of zpool.cache */
5466 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
5470 * Blow away any existing copy of zpool.cache
5473 (void) remove(spa_config_path);
5475 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5477 zs = ztest_shared = (void *)mmap(0,
5478 P2ROUNDUP(shared_size, getpagesize()),
5479 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5481 if (zopt_verbose >= 1) {
5482 (void) printf("%llu vdevs, %d datasets, %d threads,"
5483 " %llu seconds...\n",
5484 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5485 (u_longlong_t)zopt_time);
5489 * Create and initialize our storage pool.
5491 for (i = 1; i <= zopt_init; i++) {
5492 bzero(zs, sizeof (ztest_shared_t));
5493 if (zopt_verbose >= 3 && zopt_init != 1)
5494 (void) printf("ztest_init(), pass %d\n", i);
5495 zs->zs_pool = zopt_pool;
5499 zs->zs_pool = zopt_pool;
5500 zs->zs_proc_start = gethrtime();
5501 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5503 for (f = 0; f < ZTEST_FUNCS; f++) {
5504 zi = &zs->zs_info[f];
5505 *zi = ztest_info[f];
5506 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5507 zi->zi_call_next = UINT64_MAX;
5509 zi->zi_call_next = zs->zs_proc_start +
5510 ztest_random(2 * zi->zi_interval[0] + 1);
5514 * Run the tests in a loop. These tests include fault injection
5515 * to verify that self-healing data works, and forced crashes
5516 * to verify that we never lose on-disk consistency.
5518 while (gethrtime() < zs->zs_proc_stop) {
5523 * Initialize the workload counters for each function.
5525 for (f = 0; f < ZTEST_FUNCS; f++) {
5526 zi = &zs->zs_info[f];
5527 zi->zi_call_count = 0;
5528 zi->zi_call_time = 0;
5531 /* Set the allocation switch size */
5532 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5537 fatal(1, "fork failed");
5539 if (pid == 0) { /* child */
5540 struct rlimit rl = { 1024, 1024 };
5541 (void) setrlimit(RLIMIT_NOFILE, &rl);
5542 (void) enable_extended_FILE_stdio(-1, -1);
5547 while (waitpid(pid, &status, 0) != pid)
5550 if (WIFEXITED(status)) {
5551 if (WEXITSTATUS(status) != 0) {
5552 (void) fprintf(stderr,
5553 "child exited with code %d\n",
5554 WEXITSTATUS(status));
5557 } else if (WIFSIGNALED(status)) {
5558 if (WTERMSIG(status) != SIGKILL) {
5559 (void) fprintf(stderr,
5560 "child died with signal %d\n",
5566 (void) fprintf(stderr, "something strange happened "
5573 if (zopt_verbose >= 1) {
5574 hrtime_t now = gethrtime();
5576 now = MIN(now, zs->zs_proc_stop);
5577 print_time(zs->zs_proc_stop - now, timebuf);
5578 nicenum(zs->zs_space, numbuf);
5580 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5581 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5583 WIFEXITED(status) ? "Complete" : "SIGKILL",
5584 (u_longlong_t)zs->zs_enospc_count,
5585 100.0 * zs->zs_alloc / zs->zs_space,
5587 100.0 * (now - zs->zs_proc_start) /
5588 (zopt_time * NANOSEC), timebuf);
5591 if (zopt_verbose >= 2) {
5592 (void) printf("\nWorkload summary:\n\n");
5593 (void) printf("%7s %9s %s\n",
5594 "Calls", "Time", "Function");
5595 (void) printf("%7s %9s %s\n",
5596 "-----", "----", "--------");
5597 for (f = 0; f < ZTEST_FUNCS; f++) {
5600 zi = &zs->zs_info[f];
5601 print_time(zi->zi_call_time, timebuf);
5602 (void) dladdr((void *)zi->zi_func, &dli);
5603 (void) printf("%7llu %9s %s\n",
5604 (u_longlong_t)zi->zi_call_count, timebuf,
5607 (void) printf("\n");
5611 * It's possible that we killed a child during a rename test,
5612 * in which case we'll have a 'ztest_tmp' pool lying around
5613 * instead of 'ztest'. Do a blind rename in case this happened.
5616 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5617 spa_close(spa, FTAG);
5619 char tmpname[MAXNAMELEN];
5621 kernel_init(FREAD | FWRITE);
5622 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5624 (void) spa_rename(tmpname, zopt_pool);
5628 ztest_run_zdb(zopt_pool);
5631 if (zopt_verbose >= 1) {
5632 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5633 kills, iters - kills, (100.0 * kills) / MAX(1, iters));