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;
344 /* Commit cb delay */
345 static uint64_t zc_min_txg_delay = UINT64_MAX;
346 static int zc_cb_counter = 0;
349 * Minimum number of commit callbacks that need to be registered for us to check
350 * whether the minimum txg delay is acceptable.
352 #define ZTEST_COMMIT_CB_MIN_REG 100
355 * If a number of txgs equal to this threshold have been created after a commit
356 * callback has been registered but not called, then we assume there is an
357 * implementation bug.
359 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
361 extern uint64_t metaslab_gang_bang;
362 extern uint64_t metaslab_df_alloc_threshold;
363 static uint64_t metaslab_sz;
366 ZTEST_META_DNODE = 0,
371 static void usage(boolean_t) __NORETURN;
374 * These libumem hooks provide a reasonable set of defaults for the allocator's
375 * debugging facilities.
378 _umem_debug_init(void)
380 return ("default,verbose"); /* $UMEM_DEBUG setting */
384 _umem_logging_init(void)
386 return ("fail,contents"); /* $UMEM_LOGGING setting */
389 #define FATAL_MSG_SZ 1024
394 fatal(int do_perror, char *message, ...)
397 int save_errno = errno;
398 char buf[FATAL_MSG_SZ];
400 (void) fflush(stdout);
402 va_start(args, message);
403 (void) sprintf(buf, "ztest: ");
405 (void) vsprintf(buf + strlen(buf), message, args);
408 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
409 ": %s", strerror(save_errno));
411 (void) fprintf(stderr, "%s\n", buf);
412 fatal_msg = buf; /* to ease debugging */
419 str2shift(const char *buf)
421 const char *ends = "BKMGTPEZ";
426 for (i = 0; i < strlen(ends); i++) {
427 if (toupper(buf[0]) == ends[i])
430 if (i == strlen(ends)) {
431 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
435 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
438 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
444 nicenumtoull(const char *buf)
449 val = strtoull(buf, &end, 0);
451 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
453 } else if (end[0] == '.') {
454 double fval = strtod(buf, &end);
455 fval *= pow(2, str2shift(end));
456 if (fval > UINT64_MAX) {
457 (void) fprintf(stderr, "ztest: value too large: %s\n",
461 val = (uint64_t)fval;
463 int shift = str2shift(end);
464 if (shift >= 64 || (val << shift) >> shift != val) {
465 (void) fprintf(stderr, "ztest: value too large: %s\n",
475 usage(boolean_t requested)
477 char nice_vdev_size[10];
478 char nice_gang_bang[10];
479 FILE *fp = requested ? stdout : stderr;
481 nicenum(zopt_vdev_size, nice_vdev_size);
482 nicenum(metaslab_gang_bang, nice_gang_bang);
484 (void) fprintf(fp, "Usage: %s\n"
485 "\t[-v vdevs (default: %llu)]\n"
486 "\t[-s size_of_each_vdev (default: %s)]\n"
487 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
488 "\t[-m mirror_copies (default: %d)]\n"
489 "\t[-r raidz_disks (default: %d)]\n"
490 "\t[-R raidz_parity (default: %d)]\n"
491 "\t[-d datasets (default: %d)]\n"
492 "\t[-t threads (default: %d)]\n"
493 "\t[-g gang_block_threshold (default: %s)]\n"
494 "\t[-i init_count (default: %d)] initialize pool i times\n"
495 "\t[-k kill_percentage (default: %llu%%)]\n"
496 "\t[-p pool_name (default: %s)]\n"
497 "\t[-f dir (default: %s)] file directory for vdev files\n"
498 "\t[-V] verbose (use multiple times for ever more blather)\n"
499 "\t[-E] use existing pool instead of creating new one\n"
500 "\t[-T time (default: %llu sec)] total run time\n"
501 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
502 "\t[-P passtime (default: %llu sec)] time per pass\n"
503 "\t[-h] (print help)\n"
506 (u_longlong_t)zopt_vdevs, /* -v */
507 nice_vdev_size, /* -s */
508 zopt_ashift, /* -a */
509 zopt_mirrors, /* -m */
511 zopt_raidz_parity, /* -R */
512 zopt_datasets, /* -d */
513 zopt_threads, /* -t */
514 nice_gang_bang, /* -g */
516 (u_longlong_t)zopt_killrate, /* -k */
519 (u_longlong_t)zopt_time, /* -T */
520 (u_longlong_t)zopt_maxloops, /* -F */
521 (u_longlong_t)zopt_passtime); /* -P */
522 exit(requested ? 0 : 1);
526 process_options(int argc, char **argv)
531 /* By default, test gang blocks for blocks 32K and greater */
532 metaslab_gang_bang = 32 << 10;
534 while ((opt = getopt(argc, argv,
535 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
552 value = nicenumtoull(optarg);
559 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
565 zopt_mirrors = value;
568 zopt_raidz = MAX(1, value);
571 zopt_raidz_parity = MIN(MAX(value, 1), 3);
574 zopt_datasets = MAX(1, value);
577 zopt_threads = MAX(1, value);
580 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
586 zopt_killrate = value;
589 zopt_pool = strdup(optarg);
592 zopt_dir = strdup(optarg);
604 zopt_passtime = MAX(1, value);
607 zopt_maxloops = MAX(1, value);
619 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
621 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
626 ztest_kill(ztest_shared_t *zs)
628 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
629 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
630 (void) kill(getpid(), SIGKILL);
634 ztest_random(uint64_t range)
641 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
642 fatal(1, "short read from /dev/urandom");
649 ztest_record_enospc(const char *s)
651 ztest_shared->zs_enospc_count++;
655 ztest_get_ashift(void)
657 if (zopt_ashift == 0)
658 return (SPA_MINBLOCKSHIFT + ztest_random(3));
659 return (zopt_ashift);
663 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
665 char pathbuf[MAXPATHLEN];
670 ashift = ztest_get_ashift();
676 vdev = ztest_shared->zs_vdev_aux;
677 (void) sprintf(path, ztest_aux_template,
678 zopt_dir, zopt_pool, aux, vdev);
680 vdev = ztest_shared->zs_vdev_next_leaf++;
681 (void) sprintf(path, ztest_dev_template,
682 zopt_dir, zopt_pool, vdev);
687 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
689 fatal(1, "can't open %s", path);
690 if (ftruncate(fd, size) != 0)
691 fatal(1, "can't ftruncate %s", path);
695 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
696 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
697 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
698 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
704 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
706 nvlist_t *raidz, **child;
710 return (make_vdev_file(path, aux, size, ashift));
711 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
713 for (c = 0; c < r; c++)
714 child[c] = make_vdev_file(path, aux, size, ashift);
716 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
717 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
718 VDEV_TYPE_RAIDZ) == 0);
719 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
720 zopt_raidz_parity) == 0);
721 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
724 for (c = 0; c < r; c++)
725 nvlist_free(child[c]);
727 umem_free(child, r * sizeof (nvlist_t *));
733 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
736 nvlist_t *mirror, **child;
740 return (make_vdev_raidz(path, aux, size, ashift, r));
742 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
744 for (c = 0; c < m; c++)
745 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
747 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
748 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
749 VDEV_TYPE_MIRROR) == 0);
750 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
753 for (c = 0; c < m; c++)
754 nvlist_free(child[c]);
756 umem_free(child, m * sizeof (nvlist_t *));
762 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
763 int log, int r, int m, int t)
765 nvlist_t *root, **child;
770 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
772 for (c = 0; c < t; c++) {
773 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
774 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
778 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
779 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
780 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
783 for (c = 0; c < t; c++)
784 nvlist_free(child[c]);
786 umem_free(child, t * sizeof (nvlist_t *));
792 ztest_random_blocksize(void)
794 return (1 << (SPA_MINBLOCKSHIFT +
795 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
799 ztest_random_ibshift(void)
801 return (DN_MIN_INDBLKSHIFT +
802 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
806 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
809 vdev_t *rvd = spa->spa_root_vdev;
812 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
815 top = ztest_random(rvd->vdev_children);
816 tvd = rvd->vdev_child[top];
817 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
818 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
824 ztest_random_dsl_prop(zfs_prop_t prop)
829 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
830 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
836 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
839 const char *propname = zfs_prop_to_name(prop);
841 char setpoint[MAXPATHLEN];
845 error = dsl_prop_set(osname, propname,
846 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
847 sizeof (value), 1, &value);
849 if (error == ENOSPC) {
850 ztest_record_enospc(FTAG);
853 ASSERT3U(error, ==, 0);
855 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
856 1, &curval, setpoint), ==, 0);
858 if (zopt_verbose >= 6) {
859 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
860 (void) printf("%s %s = %s at '%s'\n",
861 osname, propname, valname, setpoint);
868 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
870 spa_t *spa = zs->zs_spa;
871 nvlist_t *props = NULL;
874 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
875 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
877 error = spa_prop_set(spa, props);
881 if (error == ENOSPC) {
882 ztest_record_enospc(FTAG);
885 ASSERT3U(error, ==, 0);
891 ztest_rll_init(rll_t *rll)
893 rll->rll_writer = NULL;
894 rll->rll_readers = 0;
895 VERIFY(_mutex_init(&rll->rll_lock, USYNC_THREAD, NULL) == 0);
896 VERIFY(cond_init(&rll->rll_cv, USYNC_THREAD, NULL) == 0);
900 ztest_rll_destroy(rll_t *rll)
902 ASSERT(rll->rll_writer == NULL);
903 ASSERT(rll->rll_readers == 0);
904 VERIFY(_mutex_destroy(&rll->rll_lock) == 0);
905 VERIFY(cond_destroy(&rll->rll_cv) == 0);
909 ztest_rll_lock(rll_t *rll, rl_type_t type)
911 VERIFY(mutex_lock(&rll->rll_lock) == 0);
913 if (type == RL_READER) {
914 while (rll->rll_writer != NULL)
915 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
918 while (rll->rll_writer != NULL || rll->rll_readers)
919 (void) cond_wait(&rll->rll_cv, &rll->rll_lock);
920 rll->rll_writer = curthread;
923 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
927 ztest_rll_unlock(rll_t *rll)
929 VERIFY(mutex_lock(&rll->rll_lock) == 0);
931 if (rll->rll_writer) {
932 ASSERT(rll->rll_readers == 0);
933 rll->rll_writer = NULL;
935 ASSERT(rll->rll_readers != 0);
936 ASSERT(rll->rll_writer == NULL);
940 if (rll->rll_writer == NULL && rll->rll_readers == 0)
941 VERIFY(cond_broadcast(&rll->rll_cv) == 0);
943 VERIFY(mutex_unlock(&rll->rll_lock) == 0);
947 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
949 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
951 ztest_rll_lock(rll, type);
955 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
957 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
959 ztest_rll_unlock(rll);
963 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
964 uint64_t size, rl_type_t type)
966 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
967 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
970 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
971 rl->rl_object = object;
972 rl->rl_offset = offset;
976 ztest_rll_lock(rll, type);
982 ztest_range_unlock(rl_t *rl)
984 rll_t *rll = rl->rl_lock;
986 ztest_rll_unlock(rll);
988 umem_free(rl, sizeof (*rl));
992 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
995 zd->zd_zilog = dmu_objset_zil(os);
997 dmu_objset_name(os, zd->zd_name);
1000 VERIFY(_mutex_init(&zd->zd_dirobj_lock, USYNC_THREAD, NULL) == 0);
1002 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1003 ztest_rll_init(&zd->zd_object_lock[l]);
1005 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1006 ztest_rll_init(&zd->zd_range_lock[l]);
1010 ztest_zd_fini(ztest_ds_t *zd)
1014 VERIFY(_mutex_destroy(&zd->zd_dirobj_lock) == 0);
1016 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1017 ztest_rll_destroy(&zd->zd_object_lock[l]);
1019 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1020 ztest_rll_destroy(&zd->zd_range_lock[l]);
1023 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1026 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1032 * Attempt to assign tx to some transaction group.
1034 error = dmu_tx_assign(tx, txg_how);
1036 if (error == ERESTART) {
1037 ASSERT(txg_how == TXG_NOWAIT);
1040 ASSERT3U(error, ==, ENOSPC);
1041 ztest_record_enospc(tag);
1046 txg = dmu_tx_get_txg(tx);
1052 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1055 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1063 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1066 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1070 diff |= (value - *ip++);
1077 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1078 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1080 bt->bt_magic = BT_MAGIC;
1081 bt->bt_objset = dmu_objset_id(os);
1082 bt->bt_object = object;
1083 bt->bt_offset = offset;
1086 bt->bt_crtxg = crtxg;
1090 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1091 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1093 ASSERT(bt->bt_magic == BT_MAGIC);
1094 ASSERT(bt->bt_objset == dmu_objset_id(os));
1095 ASSERT(bt->bt_object == object);
1096 ASSERT(bt->bt_offset == offset);
1097 ASSERT(bt->bt_gen <= gen);
1098 ASSERT(bt->bt_txg <= txg);
1099 ASSERT(bt->bt_crtxg == crtxg);
1102 static ztest_block_tag_t *
1103 ztest_bt_bonus(dmu_buf_t *db)
1105 dmu_object_info_t doi;
1106 ztest_block_tag_t *bt;
1108 dmu_object_info_from_db(db, &doi);
1109 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1110 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1111 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1120 #define lrz_type lr_mode
1121 #define lrz_blocksize lr_uid
1122 #define lrz_ibshift lr_gid
1123 #define lrz_bonustype lr_rdev
1124 #define lrz_bonuslen lr_crtime[1]
1127 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1129 char *name = (void *)(lr + 1); /* name follows lr */
1130 size_t namesize = strlen(name) + 1;
1133 if (zil_replaying(zd->zd_zilog, tx))
1136 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1137 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1138 sizeof (*lr) + namesize - sizeof (lr_t));
1140 zil_itx_assign(zd->zd_zilog, itx, tx);
1144 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1146 char *name = (void *)(lr + 1); /* name follows lr */
1147 size_t namesize = strlen(name) + 1;
1150 if (zil_replaying(zd->zd_zilog, tx))
1153 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1154 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1155 sizeof (*lr) + namesize - sizeof (lr_t));
1157 itx->itx_oid = object;
1158 zil_itx_assign(zd->zd_zilog, itx, tx);
1162 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1165 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1167 if (zil_replaying(zd->zd_zilog, tx))
1170 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1171 write_state = WR_INDIRECT;
1173 itx = zil_itx_create(TX_WRITE,
1174 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1176 if (write_state == WR_COPIED &&
1177 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1178 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1179 zil_itx_destroy(itx);
1180 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1181 write_state = WR_NEED_COPY;
1183 itx->itx_private = zd;
1184 itx->itx_wr_state = write_state;
1185 itx->itx_sync = (ztest_random(8) == 0);
1186 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1188 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1189 sizeof (*lr) - sizeof (lr_t));
1191 zil_itx_assign(zd->zd_zilog, itx, tx);
1195 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1199 if (zil_replaying(zd->zd_zilog, tx))
1202 itx = zil_itx_create(TX_TRUNCATE, 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);
1211 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1215 if (zil_replaying(zd->zd_zilog, tx))
1218 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1219 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1220 sizeof (*lr) - sizeof (lr_t));
1222 itx->itx_sync = B_FALSE;
1223 zil_itx_assign(zd->zd_zilog, itx, tx);
1230 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1232 char *name = (void *)(lr + 1); /* name follows lr */
1233 objset_t *os = zd->zd_os;
1234 ztest_block_tag_t *bbt;
1241 byteswap_uint64_array(lr, sizeof (*lr));
1243 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1244 ASSERT(name[0] != '\0');
1246 tx = dmu_tx_create(os);
1248 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1250 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1251 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1253 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1256 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1260 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1262 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1263 if (lr->lr_foid == 0) {
1264 lr->lr_foid = zap_create(os,
1265 lr->lrz_type, lr->lrz_bonustype,
1266 lr->lrz_bonuslen, tx);
1268 error = zap_create_claim(os, lr->lr_foid,
1269 lr->lrz_type, lr->lrz_bonustype,
1270 lr->lrz_bonuslen, tx);
1273 if (lr->lr_foid == 0) {
1274 lr->lr_foid = dmu_object_alloc(os,
1275 lr->lrz_type, 0, lr->lrz_bonustype,
1276 lr->lrz_bonuslen, tx);
1278 error = dmu_object_claim(os, lr->lr_foid,
1279 lr->lrz_type, 0, lr->lrz_bonustype,
1280 lr->lrz_bonuslen, tx);
1285 ASSERT3U(error, ==, EEXIST);
1286 ASSERT(zd->zd_zilog->zl_replay);
1291 ASSERT(lr->lr_foid != 0);
1293 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1294 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1295 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1297 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1298 bbt = ztest_bt_bonus(db);
1299 dmu_buf_will_dirty(db, tx);
1300 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1301 dmu_buf_rele(db, FTAG);
1303 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1306 (void) ztest_log_create(zd, tx, lr);
1314 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1316 char *name = (void *)(lr + 1); /* name follows lr */
1317 objset_t *os = zd->zd_os;
1318 dmu_object_info_t doi;
1320 uint64_t object, txg;
1323 byteswap_uint64_array(lr, sizeof (*lr));
1325 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1326 ASSERT(name[0] != '\0');
1329 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1330 ASSERT(object != 0);
1332 ztest_object_lock(zd, object, RL_WRITER);
1334 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1336 tx = dmu_tx_create(os);
1338 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1339 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1341 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1343 ztest_object_unlock(zd, object);
1347 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1348 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1350 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1353 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1355 (void) ztest_log_remove(zd, tx, lr, object);
1359 ztest_object_unlock(zd, object);
1365 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1367 objset_t *os = zd->zd_os;
1368 void *data = lr + 1; /* data follows lr */
1369 uint64_t offset, length;
1370 ztest_block_tag_t *bt = data;
1371 ztest_block_tag_t *bbt;
1372 uint64_t gen, txg, lrtxg, crtxg;
1373 dmu_object_info_t doi;
1376 arc_buf_t *abuf = NULL;
1380 byteswap_uint64_array(lr, sizeof (*lr));
1382 offset = lr->lr_offset;
1383 length = lr->lr_length;
1385 /* If it's a dmu_sync() block, write the whole block */
1386 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1387 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1388 if (length < blocksize) {
1389 offset -= offset % blocksize;
1394 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1395 byteswap_uint64_array(bt, sizeof (*bt));
1397 if (bt->bt_magic != BT_MAGIC)
1400 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1401 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1403 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1405 dmu_object_info_from_db(db, &doi);
1407 bbt = ztest_bt_bonus(db);
1408 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1410 crtxg = bbt->bt_crtxg;
1411 lrtxg = lr->lr_common.lrc_txg;
1413 tx = dmu_tx_create(os);
1415 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1417 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1418 P2PHASE(offset, length) == 0)
1419 abuf = dmu_request_arcbuf(db, length);
1421 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1424 dmu_return_arcbuf(abuf);
1425 dmu_buf_rele(db, FTAG);
1426 ztest_range_unlock(rl);
1427 ztest_object_unlock(zd, lr->lr_foid);
1433 * Usually, verify the old data before writing new data --
1434 * but not always, because we also want to verify correct
1435 * behavior when the data was not recently read into cache.
1437 ASSERT(offset % doi.doi_data_block_size == 0);
1438 if (ztest_random(4) != 0) {
1439 int prefetch = ztest_random(2) ?
1440 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1441 ztest_block_tag_t rbt;
1443 VERIFY(dmu_read(os, lr->lr_foid, offset,
1444 sizeof (rbt), &rbt, prefetch) == 0);
1445 if (rbt.bt_magic == BT_MAGIC) {
1446 ztest_bt_verify(&rbt, os, lr->lr_foid,
1447 offset, gen, txg, crtxg);
1452 * Writes can appear to be newer than the bonus buffer because
1453 * the ztest_get_data() callback does a dmu_read() of the
1454 * open-context data, which may be different than the data
1455 * as it was when the write was generated.
1457 if (zd->zd_zilog->zl_replay) {
1458 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1459 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1464 * Set the bt's gen/txg to the bonus buffer's gen/txg
1465 * so that all of the usual ASSERTs will work.
1467 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1471 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1473 bcopy(data, abuf->b_data, length);
1474 dmu_assign_arcbuf(db, offset, abuf, tx);
1477 (void) ztest_log_write(zd, tx, lr);
1479 dmu_buf_rele(db, FTAG);
1483 ztest_range_unlock(rl);
1484 ztest_object_unlock(zd, lr->lr_foid);
1490 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1492 objset_t *os = zd->zd_os;
1498 byteswap_uint64_array(lr, sizeof (*lr));
1500 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1501 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1504 tx = dmu_tx_create(os);
1506 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1508 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1510 ztest_range_unlock(rl);
1511 ztest_object_unlock(zd, lr->lr_foid);
1515 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1516 lr->lr_length, tx) == 0);
1518 (void) ztest_log_truncate(zd, tx, lr);
1522 ztest_range_unlock(rl);
1523 ztest_object_unlock(zd, lr->lr_foid);
1529 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1531 objset_t *os = zd->zd_os;
1534 ztest_block_tag_t *bbt;
1535 uint64_t txg, lrtxg, crtxg;
1538 byteswap_uint64_array(lr, sizeof (*lr));
1540 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1542 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1544 tx = dmu_tx_create(os);
1545 dmu_tx_hold_bonus(tx, lr->lr_foid);
1547 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1549 dmu_buf_rele(db, FTAG);
1550 ztest_object_unlock(zd, lr->lr_foid);
1554 bbt = ztest_bt_bonus(db);
1555 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1556 crtxg = bbt->bt_crtxg;
1557 lrtxg = lr->lr_common.lrc_txg;
1559 if (zd->zd_zilog->zl_replay) {
1560 ASSERT(lr->lr_size != 0);
1561 ASSERT(lr->lr_mode != 0);
1565 * Randomly change the size and increment the generation.
1567 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1569 lr->lr_mode = bbt->bt_gen + 1;
1574 * Verify that the current bonus buffer is not newer than our txg.
1576 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1577 MAX(txg, lrtxg), crtxg);
1579 dmu_buf_will_dirty(db, tx);
1581 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1582 ASSERT3U(lr->lr_size, <=, db->db_size);
1583 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1584 bbt = ztest_bt_bonus(db);
1586 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1588 dmu_buf_rele(db, FTAG);
1590 (void) ztest_log_setattr(zd, tx, lr);
1594 ztest_object_unlock(zd, lr->lr_foid);
1599 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1600 NULL, /* 0 no such transaction type */
1601 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1602 NULL, /* TX_MKDIR */
1603 NULL, /* TX_MKXATTR */
1604 NULL, /* TX_SYMLINK */
1605 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1606 NULL, /* TX_RMDIR */
1608 NULL, /* TX_RENAME */
1609 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1610 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1611 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1613 NULL, /* TX_CREATE_ACL */
1614 NULL, /* TX_CREATE_ATTR */
1615 NULL, /* TX_CREATE_ACL_ATTR */
1616 NULL, /* TX_MKDIR_ACL */
1617 NULL, /* TX_MKDIR_ATTR */
1618 NULL, /* TX_MKDIR_ACL_ATTR */
1619 NULL, /* TX_WRITE2 */
1623 * ZIL get_data callbacks
1627 ztest_get_done(zgd_t *zgd, int error)
1629 ztest_ds_t *zd = zgd->zgd_private;
1630 uint64_t object = zgd->zgd_rl->rl_object;
1633 dmu_buf_rele(zgd->zgd_db, zgd);
1635 ztest_range_unlock(zgd->zgd_rl);
1636 ztest_object_unlock(zd, object);
1638 if (error == 0 && zgd->zgd_bp)
1639 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1641 umem_free(zgd, sizeof (*zgd));
1645 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1647 ztest_ds_t *zd = arg;
1648 objset_t *os = zd->zd_os;
1649 uint64_t object = lr->lr_foid;
1650 uint64_t offset = lr->lr_offset;
1651 uint64_t size = lr->lr_length;
1652 blkptr_t *bp = &lr->lr_blkptr;
1653 uint64_t txg = lr->lr_common.lrc_txg;
1655 dmu_object_info_t doi;
1660 ztest_object_lock(zd, object, RL_READER);
1661 error = dmu_bonus_hold(os, object, FTAG, &db);
1663 ztest_object_unlock(zd, object);
1667 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1669 if (crtxg == 0 || crtxg > txg) {
1670 dmu_buf_rele(db, FTAG);
1671 ztest_object_unlock(zd, object);
1675 dmu_object_info_from_db(db, &doi);
1676 dmu_buf_rele(db, FTAG);
1679 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1680 zgd->zgd_zilog = zd->zd_zilog;
1681 zgd->zgd_private = zd;
1683 if (buf != NULL) { /* immediate write */
1684 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1687 error = dmu_read(os, object, offset, size, buf,
1688 DMU_READ_NO_PREFETCH);
1691 size = doi.doi_data_block_size;
1693 offset = P2ALIGN(offset, size);
1695 ASSERT(offset < size);
1699 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1702 error = dmu_buf_hold(os, object, offset, zgd, &db,
1703 DMU_READ_NO_PREFETCH);
1709 ASSERT(db->db_offset == offset);
1710 ASSERT(db->db_size == size);
1712 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1713 ztest_get_done, zgd);
1720 ztest_get_done(zgd, error);
1726 ztest_lr_alloc(size_t lrsize, char *name)
1729 size_t namesize = name ? strlen(name) + 1 : 0;
1731 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1734 bcopy(name, lr + lrsize, namesize);
1740 ztest_lr_free(void *lr, size_t lrsize, char *name)
1742 size_t namesize = name ? strlen(name) + 1 : 0;
1744 umem_free(lr, lrsize + namesize);
1748 * Lookup a bunch of objects. Returns the number of objects not found.
1751 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1757 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1759 for (i = 0; i < count; i++, od++) {
1761 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1762 sizeof (uint64_t), 1, &od->od_object);
1764 ASSERT(error == ENOENT);
1765 ASSERT(od->od_object == 0);
1769 ztest_block_tag_t *bbt;
1770 dmu_object_info_t doi;
1772 ASSERT(od->od_object != 0);
1773 ASSERT(missing == 0); /* there should be no gaps */
1775 ztest_object_lock(zd, od->od_object, RL_READER);
1776 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1777 od->od_object, FTAG, &db));
1778 dmu_object_info_from_db(db, &doi);
1779 bbt = ztest_bt_bonus(db);
1780 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1781 od->od_type = doi.doi_type;
1782 od->od_blocksize = doi.doi_data_block_size;
1783 od->od_gen = bbt->bt_gen;
1784 dmu_buf_rele(db, FTAG);
1785 ztest_object_unlock(zd, od->od_object);
1793 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1798 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1800 for (i = 0; i < count; i++, od++) {
1807 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1809 lr->lr_doid = od->od_dir;
1810 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1811 lr->lrz_type = od->od_crtype;
1812 lr->lrz_blocksize = od->od_crblocksize;
1813 lr->lrz_ibshift = ztest_random_ibshift();
1814 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1815 lr->lrz_bonuslen = dmu_bonus_max();
1816 lr->lr_gen = od->od_crgen;
1817 lr->lr_crtime[0] = time(NULL);
1819 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1820 ASSERT(missing == 0);
1824 od->od_object = lr->lr_foid;
1825 od->od_type = od->od_crtype;
1826 od->od_blocksize = od->od_crblocksize;
1827 od->od_gen = od->od_crgen;
1828 ASSERT(od->od_object != 0);
1831 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1838 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1844 ASSERT(_mutex_held(&zd->zd_dirobj_lock));
1848 for (i = count - 1; i >= 0; i--, od--) {
1854 if (od->od_object == 0)
1857 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1859 lr->lr_doid = od->od_dir;
1861 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1862 ASSERT3U(error, ==, ENOSPC);
1867 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1874 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1880 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1882 lr->lr_foid = object;
1883 lr->lr_offset = offset;
1884 lr->lr_length = size;
1886 BP_ZERO(&lr->lr_blkptr);
1888 bcopy(data, lr + 1, size);
1890 error = ztest_replay_write(zd, lr, B_FALSE);
1892 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1898 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1903 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1905 lr->lr_foid = object;
1906 lr->lr_offset = offset;
1907 lr->lr_length = size;
1909 error = ztest_replay_truncate(zd, lr, B_FALSE);
1911 ztest_lr_free(lr, sizeof (*lr), NULL);
1917 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1922 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1924 lr->lr_foid = object;
1928 error = ztest_replay_setattr(zd, lr, B_FALSE);
1930 ztest_lr_free(lr, sizeof (*lr), NULL);
1936 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1938 objset_t *os = zd->zd_os;
1943 txg_wait_synced(dmu_objset_pool(os), 0);
1945 ztest_object_lock(zd, object, RL_READER);
1946 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1948 tx = dmu_tx_create(os);
1950 dmu_tx_hold_write(tx, object, offset, size);
1952 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1955 dmu_prealloc(os, object, offset, size, tx);
1957 txg_wait_synced(dmu_objset_pool(os), txg);
1959 (void) dmu_free_long_range(os, object, offset, size);
1962 ztest_range_unlock(rl);
1963 ztest_object_unlock(zd, object);
1967 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1969 ztest_block_tag_t wbt;
1970 dmu_object_info_t doi;
1971 enum ztest_io_type io_type;
1975 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1976 blocksize = doi.doi_data_block_size;
1977 data = umem_alloc(blocksize, UMEM_NOFAIL);
1980 * Pick an i/o type at random, biased toward writing block tags.
1982 io_type = ztest_random(ZTEST_IO_TYPES);
1983 if (ztest_random(2) == 0)
1984 io_type = ZTEST_IO_WRITE_TAG;
1988 case ZTEST_IO_WRITE_TAG:
1989 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
1990 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
1993 case ZTEST_IO_WRITE_PATTERN:
1994 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
1995 if (ztest_random(2) == 0) {
1997 * Induce fletcher2 collisions to ensure that
1998 * zio_ddt_collision() detects and resolves them
1999 * when using fletcher2-verify for deduplication.
2001 ((uint64_t *)data)[0] ^= 1ULL << 63;
2002 ((uint64_t *)data)[4] ^= 1ULL << 63;
2004 (void) ztest_write(zd, object, offset, blocksize, data);
2007 case ZTEST_IO_WRITE_ZEROES:
2008 bzero(data, blocksize);
2009 (void) ztest_write(zd, object, offset, blocksize, data);
2012 case ZTEST_IO_TRUNCATE:
2013 (void) ztest_truncate(zd, object, offset, blocksize);
2016 case ZTEST_IO_SETATTR:
2017 (void) ztest_setattr(zd, object);
2023 umem_free(data, blocksize);
2027 * Initialize an object description template.
2030 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2031 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2033 od->od_dir = ZTEST_DIROBJ;
2036 od->od_crtype = type;
2037 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2040 od->od_type = DMU_OT_NONE;
2041 od->od_blocksize = 0;
2044 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2045 tag, (longlong_t)id, (u_longlong_t)index);
2049 * Lookup or create the objects for a test using the od template.
2050 * If the objects do not all exist, or if 'remove' is specified,
2051 * remove any existing objects and create new ones. Otherwise,
2052 * use the existing objects.
2055 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2057 int count = size / sizeof (*od);
2060 VERIFY(mutex_lock(&zd->zd_dirobj_lock) == 0);
2061 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2062 (ztest_remove(zd, od, count) != 0 ||
2063 ztest_create(zd, od, count) != 0))
2066 VERIFY(mutex_unlock(&zd->zd_dirobj_lock) == 0);
2073 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2075 zilog_t *zilog = zd->zd_zilog;
2077 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2080 * Remember the committed values in zd, which is in parent/child
2081 * shared memory. If we die, the next iteration of ztest_run()
2082 * will verify that the log really does contain this record.
2084 mutex_enter(&zilog->zl_lock);
2085 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2086 zd->zd_seq = zilog->zl_commit_lr_seq;
2087 mutex_exit(&zilog->zl_lock);
2091 * Verify that we can't destroy an active pool, create an existing pool,
2092 * or create a pool with a bad vdev spec.
2096 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2098 ztest_shared_t *zs = ztest_shared;
2103 * Attempt to create using a bad file.
2105 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2106 VERIFY3U(ENOENT, ==,
2107 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2108 nvlist_free(nvroot);
2111 * Attempt to create using a bad mirror.
2113 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2114 VERIFY3U(ENOENT, ==,
2115 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2116 nvlist_free(nvroot);
2119 * Attempt to create an existing pool. It shouldn't matter
2120 * what's in the nvroot; we should fail with EEXIST.
2122 (void) rw_rdlock(&zs->zs_name_lock);
2123 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2124 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2125 nvlist_free(nvroot);
2126 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2127 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2128 spa_close(spa, FTAG);
2130 (void) rw_unlock(&zs->zs_name_lock);
2134 vdev_lookup_by_path(vdev_t *vd, const char *path)
2139 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2142 for (c = 0; c < vd->vdev_children; c++)
2143 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2151 * Find the first available hole which can be used as a top-level.
2154 find_vdev_hole(spa_t *spa)
2156 vdev_t *rvd = spa->spa_root_vdev;
2159 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2161 for (c = 0; c < rvd->vdev_children; c++) {
2162 vdev_t *cvd = rvd->vdev_child[c];
2164 if (cvd->vdev_ishole)
2171 * Verify that vdev_add() works as expected.
2175 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2177 ztest_shared_t *zs = ztest_shared;
2178 spa_t *spa = zs->zs_spa;
2184 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2185 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2187 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2189 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2192 * If we have slogs then remove them 1/4 of the time.
2194 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2196 * Grab the guid from the head of the log class rotor.
2198 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2200 spa_config_exit(spa, SCL_VDEV, FTAG);
2203 * We have to grab the zs_name_lock as writer to
2204 * prevent a race between removing a slog (dmu_objset_find)
2205 * and destroying a dataset. Removing the slog will
2206 * grab a reference on the dataset which may cause
2207 * dmu_objset_destroy() to fail with EBUSY thus
2208 * leaving the dataset in an inconsistent state.
2210 VERIFY(rw_wrlock(&ztest_shared->zs_name_lock) == 0);
2211 error = spa_vdev_remove(spa, guid, B_FALSE);
2212 VERIFY(rw_unlock(&ztest_shared->zs_name_lock) == 0);
2214 if (error && error != EEXIST)
2215 fatal(0, "spa_vdev_remove() = %d", error);
2217 spa_config_exit(spa, SCL_VDEV, FTAG);
2220 * Make 1/4 of the devices be log devices.
2222 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2223 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2225 error = spa_vdev_add(spa, nvroot);
2226 nvlist_free(nvroot);
2228 if (error == ENOSPC)
2229 ztest_record_enospc("spa_vdev_add");
2230 else if (error != 0)
2231 fatal(0, "spa_vdev_add() = %d", error);
2234 VERIFY(mutex_unlock(&ztest_shared->zs_vdev_lock) == 0);
2238 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2242 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2244 ztest_shared_t *zs = ztest_shared;
2245 spa_t *spa = zs->zs_spa;
2246 vdev_t *rvd = spa->spa_root_vdev;
2247 spa_aux_vdev_t *sav;
2252 if (ztest_random(2) == 0) {
2253 sav = &spa->spa_spares;
2254 aux = ZPOOL_CONFIG_SPARES;
2256 sav = &spa->spa_l2cache;
2257 aux = ZPOOL_CONFIG_L2CACHE;
2260 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2262 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2264 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2266 * Pick a random device to remove.
2268 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2271 * Find an unused device we can add.
2273 zs->zs_vdev_aux = 0;
2275 char path[MAXPATHLEN];
2277 (void) sprintf(path, ztest_aux_template, zopt_dir,
2278 zopt_pool, aux, zs->zs_vdev_aux);
2279 for (c = 0; c < sav->sav_count; c++)
2280 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2283 if (c == sav->sav_count &&
2284 vdev_lookup_by_path(rvd, path) == NULL)
2290 spa_config_exit(spa, SCL_VDEV, FTAG);
2296 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2297 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2298 error = spa_vdev_add(spa, nvroot);
2300 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2301 nvlist_free(nvroot);
2304 * Remove an existing device. Sometimes, dirty its
2305 * vdev state first to make sure we handle removal
2306 * of devices that have pending state changes.
2308 if (ztest_random(2) == 0)
2309 (void) vdev_online(spa, guid, 0, NULL);
2311 error = spa_vdev_remove(spa, guid, B_FALSE);
2312 if (error != 0 && error != EBUSY)
2313 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2316 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2320 * split a pool if it has mirror tlvdevs
2324 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2326 ztest_shared_t *zs = ztest_shared;
2327 spa_t *spa = zs->zs_spa;
2328 vdev_t *rvd = spa->spa_root_vdev;
2329 nvlist_t *tree, **child, *config, *split, **schild;
2330 uint_t c, children, schildren = 0, lastlogid = 0;
2333 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2335 /* ensure we have a useable config; mirrors of raidz aren't supported */
2336 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2337 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2341 /* clean up the old pool, if any */
2342 (void) spa_destroy("splitp");
2344 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2346 /* generate a config from the existing config */
2347 mutex_enter(&spa->spa_props_lock);
2348 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2350 mutex_exit(&spa->spa_props_lock);
2352 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2355 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2356 for (c = 0; c < children; c++) {
2357 vdev_t *tvd = rvd->vdev_child[c];
2361 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2362 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2364 VERIFY(nvlist_add_string(schild[schildren],
2365 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2366 VERIFY(nvlist_add_uint64(schild[schildren],
2367 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2369 lastlogid = schildren;
2374 VERIFY(nvlist_lookup_nvlist_array(child[c],
2375 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2376 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2379 /* OK, create a config that can be used to split */
2380 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2381 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2382 VDEV_TYPE_ROOT) == 0);
2383 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2384 lastlogid != 0 ? lastlogid : schildren) == 0);
2386 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2387 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2389 for (c = 0; c < schildren; c++)
2390 nvlist_free(schild[c]);
2394 spa_config_exit(spa, SCL_VDEV, FTAG);
2396 (void) rw_wrlock(&zs->zs_name_lock);
2397 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2398 (void) rw_unlock(&zs->zs_name_lock);
2400 nvlist_free(config);
2403 (void) printf("successful split - results:\n");
2404 mutex_enter(&spa_namespace_lock);
2405 show_pool_stats(spa);
2406 show_pool_stats(spa_lookup("splitp"));
2407 mutex_exit(&spa_namespace_lock);
2411 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2416 * Verify that we can attach and detach devices.
2420 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2422 ztest_shared_t *zs = ztest_shared;
2423 spa_t *spa = zs->zs_spa;
2424 spa_aux_vdev_t *sav = &spa->spa_spares;
2425 vdev_t *rvd = spa->spa_root_vdev;
2426 vdev_t *oldvd, *newvd, *pvd;
2430 uint64_t ashift = ztest_get_ashift();
2431 uint64_t oldguid, pguid;
2432 size_t oldsize, newsize;
2433 char oldpath[MAXPATHLEN], newpath[MAXPATHLEN];
2435 int oldvd_has_siblings = B_FALSE;
2436 int newvd_is_spare = B_FALSE;
2438 int error, expected_error;
2440 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2441 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2443 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2446 * Decide whether to do an attach or a replace.
2448 replacing = ztest_random(2);
2451 * Pick a random top-level vdev.
2453 top = ztest_random_vdev_top(spa, B_TRUE);
2456 * Pick a random leaf within it.
2458 leaf = ztest_random(leaves);
2463 oldvd = rvd->vdev_child[top];
2464 if (zs->zs_mirrors >= 1) {
2465 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2466 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2467 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2469 if (zopt_raidz > 1) {
2470 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2471 ASSERT(oldvd->vdev_children == zopt_raidz);
2472 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2476 * If we're already doing an attach or replace, oldvd may be a
2477 * mirror vdev -- in which case, pick a random child.
2479 while (oldvd->vdev_children != 0) {
2480 oldvd_has_siblings = B_TRUE;
2481 ASSERT(oldvd->vdev_children >= 2);
2482 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2485 oldguid = oldvd->vdev_guid;
2486 oldsize = vdev_get_min_asize(oldvd);
2487 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2488 (void) strcpy(oldpath, oldvd->vdev_path);
2489 pvd = oldvd->vdev_parent;
2490 pguid = pvd->vdev_guid;
2493 * If oldvd has siblings, then half of the time, detach it.
2495 if (oldvd_has_siblings && ztest_random(2) == 0) {
2496 spa_config_exit(spa, SCL_VDEV, FTAG);
2497 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2498 if (error != 0 && error != ENODEV && error != EBUSY &&
2500 fatal(0, "detach (%s) returned %d", oldpath, error);
2501 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2506 * For the new vdev, choose with equal probability between the two
2507 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2509 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2510 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2511 newvd_is_spare = B_TRUE;
2512 (void) strcpy(newpath, newvd->vdev_path);
2514 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2515 zopt_dir, zopt_pool, top * leaves + leaf);
2516 if (ztest_random(2) == 0)
2517 newpath[strlen(newpath) - 1] = 'b';
2518 newvd = vdev_lookup_by_path(rvd, newpath);
2522 newsize = vdev_get_min_asize(newvd);
2525 * Make newsize a little bigger or smaller than oldsize.
2526 * If it's smaller, the attach should fail.
2527 * If it's larger, and we're doing a replace,
2528 * we should get dynamic LUN growth when we're done.
2530 newsize = 10 * oldsize / (9 + ztest_random(3));
2534 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2535 * unless it's a replace; in that case any non-replacing parent is OK.
2537 * If newvd is already part of the pool, it should fail with EBUSY.
2539 * If newvd is too small, it should fail with EOVERFLOW.
2541 if (pvd->vdev_ops != &vdev_mirror_ops &&
2542 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2543 pvd->vdev_ops == &vdev_replacing_ops ||
2544 pvd->vdev_ops == &vdev_spare_ops))
2545 expected_error = ENOTSUP;
2546 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2547 expected_error = ENOTSUP;
2548 else if (newvd == oldvd)
2549 expected_error = replacing ? 0 : EBUSY;
2550 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2551 expected_error = EBUSY;
2552 else if (newsize < oldsize)
2553 expected_error = EOVERFLOW;
2554 else if (ashift > oldvd->vdev_top->vdev_ashift)
2555 expected_error = EDOM;
2559 spa_config_exit(spa, SCL_VDEV, FTAG);
2562 * Build the nvlist describing newpath.
2564 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2565 ashift, 0, 0, 0, 1);
2567 error = spa_vdev_attach(spa, oldguid, root, replacing);
2572 * If our parent was the replacing vdev, but the replace completed,
2573 * then instead of failing with ENOTSUP we may either succeed,
2574 * fail with ENODEV, or fail with EOVERFLOW.
2576 if (expected_error == ENOTSUP &&
2577 (error == 0 || error == ENODEV || error == EOVERFLOW))
2578 expected_error = error;
2581 * If someone grew the LUN, the replacement may be too small.
2583 if (error == EOVERFLOW || error == EBUSY)
2584 expected_error = error;
2586 /* XXX workaround 6690467 */
2587 if (error != expected_error && expected_error != EBUSY) {
2588 fatal(0, "attach (%s %llu, %s %llu, %d) "
2589 "returned %d, expected %d",
2590 oldpath, (longlong_t)oldsize, newpath,
2591 (longlong_t)newsize, replacing, error, expected_error);
2594 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2598 * Callback function which expands the physical size of the vdev.
2601 grow_vdev(vdev_t *vd, void *arg)
2603 ASSERTV(spa_t *spa = vd->vdev_spa);
2604 size_t *newsize = arg;
2608 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2609 ASSERT(vd->vdev_ops->vdev_op_leaf);
2611 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2614 fsize = lseek(fd, 0, SEEK_END);
2615 VERIFY(ftruncate(fd, *newsize) == 0);
2617 if (zopt_verbose >= 6) {
2618 (void) printf("%s grew from %lu to %lu bytes\n",
2619 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2626 * Callback function which expands a given vdev by calling vdev_online().
2630 online_vdev(vdev_t *vd, void *arg)
2632 spa_t *spa = vd->vdev_spa;
2633 vdev_t *tvd = vd->vdev_top;
2634 uint64_t guid = vd->vdev_guid;
2635 uint64_t generation = spa->spa_config_generation + 1;
2636 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2639 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2640 ASSERT(vd->vdev_ops->vdev_op_leaf);
2642 /* Calling vdev_online will initialize the new metaslabs */
2643 spa_config_exit(spa, SCL_STATE, spa);
2644 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2645 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2648 * If vdev_online returned an error or the underlying vdev_open
2649 * failed then we abort the expand. The only way to know that
2650 * vdev_open fails is by checking the returned newstate.
2652 if (error || newstate != VDEV_STATE_HEALTHY) {
2653 if (zopt_verbose >= 5) {
2654 (void) printf("Unable to expand vdev, state %llu, "
2655 "error %d\n", (u_longlong_t)newstate, error);
2659 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2662 * Since we dropped the lock we need to ensure that we're
2663 * still talking to the original vdev. It's possible this
2664 * vdev may have been detached/replaced while we were
2665 * trying to online it.
2667 if (generation != spa->spa_config_generation) {
2668 if (zopt_verbose >= 5) {
2669 (void) printf("vdev configuration has changed, "
2670 "guid %llu, state %llu, expected gen %llu, "
2673 (u_longlong_t)tvd->vdev_state,
2674 (u_longlong_t)generation,
2675 (u_longlong_t)spa->spa_config_generation);
2683 * Traverse the vdev tree calling the supplied function.
2684 * We continue to walk the tree until we either have walked all
2685 * children or we receive a non-NULL return from the callback.
2686 * If a NULL callback is passed, then we just return back the first
2687 * leaf vdev we encounter.
2690 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2694 if (vd->vdev_ops->vdev_op_leaf) {
2698 return (func(vd, arg));
2701 for (c = 0; c < vd->vdev_children; c++) {
2702 vdev_t *cvd = vd->vdev_child[c];
2703 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2710 * Verify that dynamic LUN growth works as expected.
2714 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2716 ztest_shared_t *zs = ztest_shared;
2717 spa_t *spa = zs->zs_spa;
2719 metaslab_class_t *mc;
2720 metaslab_group_t *mg;
2721 size_t psize, newsize;
2723 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2725 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
2726 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2728 top = ztest_random_vdev_top(spa, B_TRUE);
2730 tvd = spa->spa_root_vdev->vdev_child[top];
2733 old_ms_count = tvd->vdev_ms_count;
2734 old_class_space = metaslab_class_get_space(mc);
2737 * Determine the size of the first leaf vdev associated with
2738 * our top-level device.
2740 vd = vdev_walk_tree(tvd, NULL, NULL);
2741 ASSERT3P(vd, !=, NULL);
2742 ASSERT(vd->vdev_ops->vdev_op_leaf);
2744 psize = vd->vdev_psize;
2747 * We only try to expand the vdev if it's healthy, less than 4x its
2748 * original size, and it has a valid psize.
2750 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2751 psize == 0 || psize >= 4 * zopt_vdev_size) {
2752 spa_config_exit(spa, SCL_STATE, spa);
2753 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2757 newsize = psize + psize / 8;
2758 ASSERT3U(newsize, >, psize);
2760 if (zopt_verbose >= 6) {
2761 (void) printf("Expanding LUN %s from %lu to %lu\n",
2762 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2766 * Growing the vdev is a two step process:
2767 * 1). expand the physical size (i.e. relabel)
2768 * 2). online the vdev to create the new metaslabs
2770 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2771 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2772 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2773 if (zopt_verbose >= 5) {
2774 (void) printf("Could not expand LUN because "
2775 "the vdev configuration changed.\n");
2777 spa_config_exit(spa, SCL_STATE, spa);
2778 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2782 spa_config_exit(spa, SCL_STATE, spa);
2785 * Expanding the LUN will update the config asynchronously,
2786 * thus we must wait for the async thread to complete any
2787 * pending tasks before proceeding.
2791 mutex_enter(&spa->spa_async_lock);
2792 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2793 mutex_exit(&spa->spa_async_lock);
2796 txg_wait_synced(spa_get_dsl(spa), 0);
2797 (void) poll(NULL, 0, 100);
2800 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2802 tvd = spa->spa_root_vdev->vdev_child[top];
2803 new_ms_count = tvd->vdev_ms_count;
2804 new_class_space = metaslab_class_get_space(mc);
2806 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2807 if (zopt_verbose >= 5) {
2808 (void) printf("Could not verify LUN expansion due to "
2809 "intervening vdev offline or remove.\n");
2811 spa_config_exit(spa, SCL_STATE, spa);
2812 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2817 * Make sure we were able to grow the vdev.
2819 if (new_ms_count <= old_ms_count)
2820 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2821 old_ms_count, new_ms_count);
2824 * Make sure we were able to grow the pool.
2826 if (new_class_space <= old_class_space)
2827 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2828 old_class_space, new_class_space);
2830 if (zopt_verbose >= 5) {
2831 char oldnumbuf[6], newnumbuf[6];
2833 nicenum(old_class_space, oldnumbuf);
2834 nicenum(new_class_space, newnumbuf);
2835 (void) printf("%s grew from %s to %s\n",
2836 spa->spa_name, oldnumbuf, newnumbuf);
2839 spa_config_exit(spa, SCL_STATE, spa);
2840 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
2844 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2848 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2851 * Create the objects common to all ztest datasets.
2853 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2854 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2858 ztest_dataset_create(char *dsname)
2860 uint64_t zilset = ztest_random(100);
2861 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2862 ztest_objset_create_cb, NULL);
2864 if (err || zilset < 80)
2867 (void) printf("Setting dataset %s to sync always\n", dsname);
2868 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2869 ZFS_SYNC_ALWAYS, B_FALSE));
2874 ztest_objset_destroy_cb(const char *name, void *arg)
2877 dmu_object_info_t doi;
2881 * Verify that the dataset contains a directory object.
2883 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2884 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2885 if (error != ENOENT) {
2886 /* We could have crashed in the middle of destroying it */
2887 ASSERT3U(error, ==, 0);
2888 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2889 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2891 dmu_objset_rele(os, FTAG);
2894 * Destroy the dataset.
2896 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2901 ztest_snapshot_create(char *osname, uint64_t id)
2903 char snapname[MAXNAMELEN];
2906 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2909 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2910 NULL, NULL, B_FALSE, B_FALSE, -1);
2911 if (error == ENOSPC) {
2912 ztest_record_enospc(FTAG);
2915 if (error != 0 && error != EEXIST)
2916 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2921 ztest_snapshot_destroy(char *osname, uint64_t id)
2923 char snapname[MAXNAMELEN];
2926 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2929 error = dmu_objset_destroy(snapname, B_FALSE);
2930 if (error != 0 && error != ENOENT)
2931 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2937 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2939 ztest_shared_t *zs = ztest_shared;
2944 char name[MAXNAMELEN];
2948 (void) rw_rdlock(&zs->zs_name_lock);
2950 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2951 zs->zs_pool, (u_longlong_t)id);
2954 * If this dataset exists from a previous run, process its replay log
2955 * half of the time. If we don't replay it, then dmu_objset_destroy()
2956 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2958 if (ztest_random(2) == 0 &&
2959 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2960 ztest_zd_init(&zdtmp, os);
2961 zil_replay(os, &zdtmp, ztest_replay_vector);
2962 ztest_zd_fini(&zdtmp);
2963 dmu_objset_disown(os, FTAG);
2967 * There may be an old instance of the dataset we're about to
2968 * create lying around from a previous run. If so, destroy it
2969 * and all of its snapshots.
2971 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
2972 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2975 * Verify that the destroyed dataset is no longer in the namespace.
2977 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
2980 * Verify that we can create a new dataset.
2982 error = ztest_dataset_create(name);
2984 if (error == ENOSPC) {
2985 ztest_record_enospc(FTAG);
2986 (void) rw_unlock(&zs->zs_name_lock);
2989 fatal(0, "dmu_objset_create(%s) = %d", name, error);
2993 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
2995 ztest_zd_init(&zdtmp, os);
2998 * Open the intent log for it.
3000 zilog = zil_open(os, ztest_get_data);
3003 * Put some objects in there, do a little I/O to them,
3004 * and randomly take a couple of snapshots along the way.
3006 iters = ztest_random(5);
3007 for (i = 0; i < iters; i++) {
3008 ztest_dmu_object_alloc_free(&zdtmp, id);
3009 if (ztest_random(iters) == 0)
3010 (void) ztest_snapshot_create(name, i);
3014 * Verify that we cannot create an existing dataset.
3016 VERIFY3U(EEXIST, ==,
3017 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3020 * Verify that we can hold an objset that is also owned.
3022 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3023 dmu_objset_rele(os2, FTAG);
3026 * Verify that we cannot own an objset that is already owned.
3029 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3032 dmu_objset_disown(os, FTAG);
3033 ztest_zd_fini(&zdtmp);
3035 (void) rw_unlock(&zs->zs_name_lock);
3039 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3042 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3044 ztest_shared_t *zs = ztest_shared;
3046 (void) rw_rdlock(&zs->zs_name_lock);
3047 (void) ztest_snapshot_destroy(zd->zd_name, id);
3048 (void) ztest_snapshot_create(zd->zd_name, id);
3049 (void) rw_unlock(&zs->zs_name_lock);
3053 * Cleanup non-standard snapshots and clones.
3056 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3058 char snap1name[MAXNAMELEN];
3059 char clone1name[MAXNAMELEN];
3060 char snap2name[MAXNAMELEN];
3061 char clone2name[MAXNAMELEN];
3062 char snap3name[MAXNAMELEN];
3065 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3066 osname, (u_longlong_t)id);
3067 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3068 osname, (u_longlong_t)id);
3069 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3070 clone1name, (u_longlong_t)id);
3071 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3072 osname, (u_longlong_t)id);
3073 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3074 clone1name, (u_longlong_t)id);
3076 error = dmu_objset_destroy(clone2name, B_FALSE);
3077 if (error && error != ENOENT)
3078 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3079 error = dmu_objset_destroy(snap3name, B_FALSE);
3080 if (error && error != ENOENT)
3081 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3082 error = dmu_objset_destroy(snap2name, B_FALSE);
3083 if (error && error != ENOENT)
3084 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3085 error = dmu_objset_destroy(clone1name, B_FALSE);
3086 if (error && error != ENOENT)
3087 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3088 error = dmu_objset_destroy(snap1name, B_FALSE);
3089 if (error && error != ENOENT)
3090 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3094 * Verify dsl_dataset_promote handles EBUSY
3097 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3099 ztest_shared_t *zs = ztest_shared;
3102 char snap1name[MAXNAMELEN];
3103 char clone1name[MAXNAMELEN];
3104 char snap2name[MAXNAMELEN];
3105 char clone2name[MAXNAMELEN];
3106 char snap3name[MAXNAMELEN];
3107 char *osname = zd->zd_name;
3110 (void) rw_rdlock(&zs->zs_name_lock);
3112 ztest_dsl_dataset_cleanup(osname, id);
3114 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3115 osname, (u_longlong_t)id);
3116 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3117 osname, (u_longlong_t)id);
3118 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3119 clone1name, (u_longlong_t)id);
3120 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3121 osname, (u_longlong_t)id);
3122 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3123 clone1name, (u_longlong_t)id);
3125 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3126 NULL, NULL, B_FALSE, B_FALSE, -1);
3127 if (error && error != EEXIST) {
3128 if (error == ENOSPC) {
3129 ztest_record_enospc(FTAG);
3132 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3135 error = dmu_objset_hold(snap1name, FTAG, &clone);
3137 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3139 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3140 dmu_objset_rele(clone, FTAG);
3142 if (error == ENOSPC) {
3143 ztest_record_enospc(FTAG);
3146 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3149 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3150 NULL, NULL, B_FALSE, B_FALSE, -1);
3151 if (error && error != EEXIST) {
3152 if (error == ENOSPC) {
3153 ztest_record_enospc(FTAG);
3156 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3159 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3160 NULL, NULL, B_FALSE, B_FALSE, -1);
3161 if (error && error != EEXIST) {
3162 if (error == ENOSPC) {
3163 ztest_record_enospc(FTAG);
3166 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3169 error = dmu_objset_hold(snap3name, FTAG, &clone);
3171 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3173 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3174 dmu_objset_rele(clone, FTAG);
3176 if (error == ENOSPC) {
3177 ztest_record_enospc(FTAG);
3180 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3183 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3185 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3186 error = dsl_dataset_promote(clone2name, NULL);
3188 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3190 dsl_dataset_disown(ds, FTAG);
3193 ztest_dsl_dataset_cleanup(osname, id);
3195 (void) rw_unlock(&zs->zs_name_lock);
3199 * Verify that dmu_object_{alloc,free} work as expected.
3202 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3205 int batchsize = sizeof (od) / sizeof (od[0]);
3208 for (b = 0; b < batchsize; b++)
3209 ztest_od_init(&od[b], id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3212 * Destroy the previous batch of objects, create a new batch,
3213 * and do some I/O on the new objects.
3215 if (ztest_object_init(zd, od, sizeof (od), B_TRUE) != 0)
3218 while (ztest_random(4 * batchsize) != 0)
3219 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3220 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3224 * Verify that dmu_{read,write} work as expected.
3227 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3229 objset_t *os = zd->zd_os;
3232 int i, freeit, error;
3234 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3235 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3236 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3237 uint64_t regions = 997;
3238 uint64_t stride = 123456789ULL;
3239 uint64_t width = 40;
3240 int free_percent = 5;
3243 * This test uses two objects, packobj and bigobj, that are always
3244 * updated together (i.e. in the same tx) so that their contents are
3245 * in sync and can be compared. Their contents relate to each other
3246 * in a simple way: packobj is a dense array of 'bufwad' structures,
3247 * while bigobj is a sparse array of the same bufwads. Specifically,
3248 * for any index n, there are three bufwads that should be identical:
3250 * packobj, at offset n * sizeof (bufwad_t)
3251 * bigobj, at the head of the nth chunk
3252 * bigobj, at the tail of the nth chunk
3254 * The chunk size is arbitrary. It doesn't have to be a power of two,
3255 * and it doesn't have any relation to the object blocksize.
3256 * The only requirement is that it can hold at least two bufwads.
3258 * Normally, we write the bufwad to each of these locations.
3259 * However, free_percent of the time we instead write zeroes to
3260 * packobj and perform a dmu_free_range() on bigobj. By comparing
3261 * bigobj to packobj, we can verify that the DMU is correctly
3262 * tracking which parts of an object are allocated and free,
3263 * and that the contents of the allocated blocks are correct.
3267 * Read the directory info. If it's the first time, set things up.
3269 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3270 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3272 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3275 bigobj = od[0].od_object;
3276 packobj = od[1].od_object;
3277 chunksize = od[0].od_gen;
3278 ASSERT(chunksize == od[1].od_gen);
3281 * Prefetch a random chunk of the big object.
3282 * Our aim here is to get some async reads in flight
3283 * for blocks that we may free below; the DMU should
3284 * handle this race correctly.
3286 n = ztest_random(regions) * stride + ztest_random(width);
3287 s = 1 + ztest_random(2 * width - 1);
3288 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3291 * Pick a random index and compute the offsets into packobj and bigobj.
3293 n = ztest_random(regions) * stride + ztest_random(width);
3294 s = 1 + ztest_random(width - 1);
3296 packoff = n * sizeof (bufwad_t);
3297 packsize = s * sizeof (bufwad_t);
3299 bigoff = n * chunksize;
3300 bigsize = s * chunksize;
3302 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3303 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3306 * free_percent of the time, free a range of bigobj rather than
3309 freeit = (ztest_random(100) < free_percent);
3312 * Read the current contents of our objects.
3314 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3316 ASSERT3U(error, ==, 0);
3317 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3319 ASSERT3U(error, ==, 0);
3322 * Get a tx for the mods to both packobj and bigobj.
3324 tx = dmu_tx_create(os);
3326 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3329 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3331 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3333 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3335 umem_free(packbuf, packsize);
3336 umem_free(bigbuf, bigsize);
3340 dmu_object_set_checksum(os, bigobj,
3341 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3343 dmu_object_set_compress(os, bigobj,
3344 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3347 * For each index from n to n + s, verify that the existing bufwad
3348 * in packobj matches the bufwads at the head and tail of the
3349 * corresponding chunk in bigobj. Then update all three bufwads
3350 * with the new values we want to write out.
3352 for (i = 0; i < s; i++) {
3354 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3356 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3358 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3360 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3361 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3363 if (pack->bw_txg > txg)
3364 fatal(0, "future leak: got %llx, open txg is %llx",
3367 if (pack->bw_data != 0 && pack->bw_index != n + i)
3368 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3369 pack->bw_index, n, i);
3371 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3372 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3374 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3375 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3378 bzero(pack, sizeof (bufwad_t));
3380 pack->bw_index = n + i;
3382 pack->bw_data = 1 + ztest_random(-2ULL);
3389 * We've verified all the old bufwads, and made new ones.
3390 * Now write them out.
3392 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3395 if (zopt_verbose >= 7) {
3396 (void) printf("freeing offset %llx size %llx"
3398 (u_longlong_t)bigoff,
3399 (u_longlong_t)bigsize,
3402 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3404 if (zopt_verbose >= 7) {
3405 (void) printf("writing offset %llx size %llx"
3407 (u_longlong_t)bigoff,
3408 (u_longlong_t)bigsize,
3411 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3417 * Sanity check the stuff we just wrote.
3420 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3421 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3423 VERIFY(0 == dmu_read(os, packobj, packoff,
3424 packsize, packcheck, DMU_READ_PREFETCH));
3425 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3426 bigsize, bigcheck, DMU_READ_PREFETCH));
3428 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3429 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3431 umem_free(packcheck, packsize);
3432 umem_free(bigcheck, bigsize);
3435 umem_free(packbuf, packsize);
3436 umem_free(bigbuf, bigsize);
3440 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3441 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3449 * For each index from n to n + s, verify that the existing bufwad
3450 * in packobj matches the bufwads at the head and tail of the
3451 * corresponding chunk in bigobj. Then update all three bufwads
3452 * with the new values we want to write out.
3454 for (i = 0; i < s; i++) {
3456 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3458 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3460 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3462 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3463 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3465 if (pack->bw_txg > txg)
3466 fatal(0, "future leak: got %llx, open txg is %llx",
3469 if (pack->bw_data != 0 && pack->bw_index != n + i)
3470 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3471 pack->bw_index, n, i);
3473 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3474 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3476 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3477 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3479 pack->bw_index = n + i;
3481 pack->bw_data = 1 + ztest_random(-2ULL);
3489 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3491 objset_t *os = zd->zd_os;
3497 bufwad_t *packbuf, *bigbuf;
3498 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3499 uint64_t blocksize = ztest_random_blocksize();
3500 uint64_t chunksize = blocksize;
3501 uint64_t regions = 997;
3502 uint64_t stride = 123456789ULL;
3504 dmu_buf_t *bonus_db;
3505 arc_buf_t **bigbuf_arcbufs;
3506 dmu_object_info_t doi;
3509 * This test uses two objects, packobj and bigobj, that are always
3510 * updated together (i.e. in the same tx) so that their contents are
3511 * in sync and can be compared. Their contents relate to each other
3512 * in a simple way: packobj is a dense array of 'bufwad' structures,
3513 * while bigobj is a sparse array of the same bufwads. Specifically,
3514 * for any index n, there are three bufwads that should be identical:
3516 * packobj, at offset n * sizeof (bufwad_t)
3517 * bigobj, at the head of the nth chunk
3518 * bigobj, at the tail of the nth chunk
3520 * The chunk size is set equal to bigobj block size so that
3521 * dmu_assign_arcbuf() can be tested for object updates.
3525 * Read the directory info. If it's the first time, set things up.
3527 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3528 ztest_od_init(&od[1], id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3530 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3533 bigobj = od[0].od_object;
3534 packobj = od[1].od_object;
3535 blocksize = od[0].od_blocksize;
3536 chunksize = blocksize;
3537 ASSERT(chunksize == od[1].od_gen);
3539 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3540 VERIFY(ISP2(doi.doi_data_block_size));
3541 VERIFY(chunksize == doi.doi_data_block_size);
3542 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3545 * Pick a random index and compute the offsets into packobj and bigobj.
3547 n = ztest_random(regions) * stride + ztest_random(width);
3548 s = 1 + ztest_random(width - 1);
3550 packoff = n * sizeof (bufwad_t);
3551 packsize = s * sizeof (bufwad_t);
3553 bigoff = n * chunksize;
3554 bigsize = s * chunksize;
3556 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3557 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3559 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3561 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3564 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3565 * Iteration 1 test zcopy to already referenced dbufs.
3566 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3567 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3568 * Iteration 4 test zcopy when dbuf is no longer dirty.
3569 * Iteration 5 test zcopy when it can't be done.
3570 * Iteration 6 one more zcopy write.
3572 for (i = 0; i < 7; i++) {
3577 * In iteration 5 (i == 5) use arcbufs
3578 * that don't match bigobj blksz to test
3579 * dmu_assign_arcbuf() when it can't directly
3580 * assign an arcbuf to a dbuf.
3582 for (j = 0; j < s; j++) {
3585 dmu_request_arcbuf(bonus_db, chunksize);
3587 bigbuf_arcbufs[2 * j] =
3588 dmu_request_arcbuf(bonus_db, chunksize / 2);
3589 bigbuf_arcbufs[2 * j + 1] =
3590 dmu_request_arcbuf(bonus_db, chunksize / 2);
3595 * Get a tx for the mods to both packobj and bigobj.
3597 tx = dmu_tx_create(os);
3599 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3600 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3602 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3604 umem_free(packbuf, packsize);
3605 umem_free(bigbuf, bigsize);
3606 for (j = 0; j < s; j++) {
3608 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3611 bigbuf_arcbufs[2 * j]);
3613 bigbuf_arcbufs[2 * j + 1]);
3616 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3617 dmu_buf_rele(bonus_db, FTAG);
3622 * 50% of the time don't read objects in the 1st iteration to
3623 * test dmu_assign_arcbuf() for the case when there're no
3624 * existing dbufs for the specified offsets.
3626 if (i != 0 || ztest_random(2) != 0) {
3627 error = dmu_read(os, packobj, packoff,
3628 packsize, packbuf, DMU_READ_PREFETCH);
3629 ASSERT3U(error, ==, 0);
3630 error = dmu_read(os, bigobj, bigoff, bigsize,
3631 bigbuf, DMU_READ_PREFETCH);
3632 ASSERT3U(error, ==, 0);
3634 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3638 * We've verified all the old bufwads, and made new ones.
3639 * Now write them out.
3641 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3642 if (zopt_verbose >= 7) {
3643 (void) printf("writing offset %llx size %llx"
3645 (u_longlong_t)bigoff,
3646 (u_longlong_t)bigsize,
3649 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3652 bcopy((caddr_t)bigbuf + (off - bigoff),
3653 bigbuf_arcbufs[j]->b_data, chunksize);
3655 bcopy((caddr_t)bigbuf + (off - bigoff),
3656 bigbuf_arcbufs[2 * j]->b_data,
3658 bcopy((caddr_t)bigbuf + (off - bigoff) +
3660 bigbuf_arcbufs[2 * j + 1]->b_data,
3665 VERIFY(dmu_buf_hold(os, bigobj, off,
3666 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3669 dmu_assign_arcbuf(bonus_db, off,
3670 bigbuf_arcbufs[j], tx);
3672 dmu_assign_arcbuf(bonus_db, off,
3673 bigbuf_arcbufs[2 * j], tx);
3674 dmu_assign_arcbuf(bonus_db,
3675 off + chunksize / 2,
3676 bigbuf_arcbufs[2 * j + 1], tx);
3679 dmu_buf_rele(dbt, FTAG);
3685 * Sanity check the stuff we just wrote.
3688 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3689 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3691 VERIFY(0 == dmu_read(os, packobj, packoff,
3692 packsize, packcheck, DMU_READ_PREFETCH));
3693 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3694 bigsize, bigcheck, DMU_READ_PREFETCH));
3696 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3697 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3699 umem_free(packcheck, packsize);
3700 umem_free(bigcheck, bigsize);
3703 txg_wait_open(dmu_objset_pool(os), 0);
3704 } else if (i == 3) {
3705 txg_wait_synced(dmu_objset_pool(os), 0);
3709 dmu_buf_rele(bonus_db, FTAG);
3710 umem_free(packbuf, packsize);
3711 umem_free(bigbuf, bigsize);
3712 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3717 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3720 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3721 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3724 * Have multiple threads write to large offsets in an object
3725 * to verify that parallel writes to an object -- even to the
3726 * same blocks within the object -- doesn't cause any trouble.
3728 ztest_od_init(&od[0], ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3730 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3733 while (ztest_random(10) != 0)
3734 ztest_io(zd, od[0].od_object, offset);
3738 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3741 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3742 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3743 uint64_t count = ztest_random(20) + 1;
3744 uint64_t blocksize = ztest_random_blocksize();
3747 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3749 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3752 if (ztest_truncate(zd, od[0].od_object, offset, count * blocksize) != 0)
3755 ztest_prealloc(zd, od[0].od_object, offset, count * blocksize);
3757 data = umem_zalloc(blocksize, UMEM_NOFAIL);
3759 while (ztest_random(count) != 0) {
3760 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3761 if (ztest_write(zd, od[0].od_object, randoff, blocksize,
3764 while (ztest_random(4) != 0)
3765 ztest_io(zd, od[0].od_object, randoff);
3768 umem_free(data, blocksize);
3772 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3774 #define ZTEST_ZAP_MIN_INTS 1
3775 #define ZTEST_ZAP_MAX_INTS 4
3776 #define ZTEST_ZAP_MAX_PROPS 1000
3779 ztest_zap(ztest_ds_t *zd, uint64_t id)
3781 objset_t *os = zd->zd_os;
3784 uint64_t txg, last_txg;
3785 uint64_t value[ZTEST_ZAP_MAX_INTS];
3786 uint64_t zl_ints, zl_intsize, prop;
3789 char propname[100], txgname[100];
3791 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3793 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3795 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3798 object = od[0].od_object;
3801 * Generate a known hash collision, and verify that
3802 * we can lookup and remove both entries.
3804 tx = dmu_tx_create(os);
3805 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3806 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3809 for (i = 0; i < 2; i++) {
3811 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3814 for (i = 0; i < 2; i++) {
3815 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3816 sizeof (uint64_t), 1, &value[i], tx));
3818 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3819 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3820 ASSERT3U(zl_ints, ==, 1);
3822 for (i = 0; i < 2; i++) {
3823 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3828 * Generate a buch of random entries.
3830 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3832 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3833 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3834 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3835 bzero(value, sizeof (value));
3839 * If these zap entries already exist, validate their contents.
3841 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3843 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3844 ASSERT3U(zl_ints, ==, 1);
3846 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3847 zl_ints, &last_txg) == 0);
3849 VERIFY(zap_length(os, object, propname, &zl_intsize,
3852 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3853 ASSERT3U(zl_ints, ==, ints);
3855 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3856 zl_ints, value) == 0);
3858 for (i = 0; i < ints; i++) {
3859 ASSERT3U(value[i], ==, last_txg + object + i);
3862 ASSERT3U(error, ==, ENOENT);
3866 * Atomically update two entries in our zap object.
3867 * The first is named txg_%llu, and contains the txg
3868 * in which the property was last updated. The second
3869 * is named prop_%llu, and the nth element of its value
3870 * should be txg + object + n.
3872 tx = dmu_tx_create(os);
3873 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3874 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3879 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3881 for (i = 0; i < ints; i++)
3882 value[i] = txg + object + i;
3884 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3886 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3892 * Remove a random pair of entries.
3894 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3895 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3896 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3898 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3900 if (error == ENOENT)
3903 ASSERT3U(error, ==, 0);
3905 tx = dmu_tx_create(os);
3906 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3907 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3910 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
3911 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
3916 * Testcase to test the upgrading of a microzap to fatzap.
3919 ztest_fzap(ztest_ds_t *zd, uint64_t id)
3921 objset_t *os = zd->zd_os;
3923 uint64_t object, txg;
3926 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3928 if (ztest_object_init(zd, od, sizeof (od), !ztest_random(2)) != 0)
3931 object = od[0].od_object;
3934 * Add entries to this ZAP and make sure it spills over
3935 * and gets upgraded to a fatzap. Also, since we are adding
3936 * 2050 entries we should see ptrtbl growth and leaf-block split.
3938 for (i = 0; i < 2050; i++) {
3939 char name[MAXNAMELEN];
3944 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
3945 (u_longlong_t)id, (u_longlong_t)value);
3947 tx = dmu_tx_create(os);
3948 dmu_tx_hold_zap(tx, object, B_TRUE, name);
3949 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3952 error = zap_add(os, object, name, sizeof (uint64_t), 1,
3954 ASSERT(error == 0 || error == EEXIST);
3961 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
3963 objset_t *os = zd->zd_os;
3965 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
3967 int i, namelen, error;
3968 int micro = ztest_random(2);
3969 char name[20], string_value[20];
3972 ztest_od_init(&od[0], ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
3974 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
3977 object = od[0].od_object;
3980 * Generate a random name of the form 'xxx.....' where each
3981 * x is a random printable character and the dots are dots.
3982 * There are 94 such characters, and the name length goes from
3983 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
3985 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
3987 for (i = 0; i < 3; i++)
3988 name[i] = '!' + ztest_random('~' - '!' + 1);
3989 for (; i < namelen - 1; i++)
3993 if ((namelen & 1) || micro) {
3994 wsize = sizeof (txg);
4000 data = string_value;
4004 VERIFY(zap_count(os, object, &count) == 0);
4005 ASSERT(count != -1ULL);
4008 * Select an operation: length, lookup, add, update, remove.
4010 i = ztest_random(5);
4013 tx = dmu_tx_create(os);
4014 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4015 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4018 bcopy(name, string_value, namelen);
4022 bzero(string_value, namelen);
4028 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4030 ASSERT3U(wsize, ==, zl_wsize);
4031 ASSERT3U(wc, ==, zl_wc);
4033 ASSERT3U(error, ==, ENOENT);
4038 error = zap_lookup(os, object, name, wsize, wc, data);
4040 if (data == string_value &&
4041 bcmp(name, data, namelen) != 0)
4042 fatal(0, "name '%s' != val '%s' len %d",
4043 name, data, namelen);
4045 ASSERT3U(error, ==, ENOENT);
4050 error = zap_add(os, object, name, wsize, wc, data, tx);
4051 ASSERT(error == 0 || error == EEXIST);
4055 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4059 error = zap_remove(os, object, name, tx);
4060 ASSERT(error == 0 || error == ENOENT);
4069 * Commit callback data.
4071 typedef struct ztest_cb_data {
4072 list_node_t zcd_node;
4074 int zcd_expected_err;
4075 boolean_t zcd_added;
4076 boolean_t zcd_called;
4080 /* This is the actual commit callback function */
4082 ztest_commit_callback(void *arg, int error)
4084 ztest_cb_data_t *data = arg;
4085 uint64_t synced_txg;
4087 VERIFY(data != NULL);
4088 VERIFY3S(data->zcd_expected_err, ==, error);
4089 VERIFY(!data->zcd_called);
4091 synced_txg = spa_last_synced_txg(data->zcd_spa);
4092 if (data->zcd_txg > synced_txg)
4093 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4094 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4097 data->zcd_called = B_TRUE;
4099 if (error == ECANCELED) {
4100 ASSERT3U(data->zcd_txg, ==, 0);
4101 ASSERT(!data->zcd_added);
4104 * The private callback data should be destroyed here, but
4105 * since we are going to check the zcd_called field after
4106 * dmu_tx_abort(), we will destroy it there.
4111 ASSERT(data->zcd_added);
4112 ASSERT3U(data->zcd_txg, !=, 0);
4114 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4116 /* See if this cb was called more quickly */
4117 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4118 zc_min_txg_delay = synced_txg - data->zcd_txg;
4120 /* Remove our callback from the list */
4121 list_remove(&zcl.zcl_callbacks, data);
4123 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4125 umem_free(data, sizeof (ztest_cb_data_t));
4128 /* Allocate and initialize callback data structure */
4129 static ztest_cb_data_t *
4130 ztest_create_cb_data(objset_t *os, uint64_t txg)
4132 ztest_cb_data_t *cb_data;
4134 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4136 cb_data->zcd_txg = txg;
4137 cb_data->zcd_spa = dmu_objset_spa(os);
4143 * Commit callback test.
4146 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4148 objset_t *os = zd->zd_os;
4151 ztest_cb_data_t *cb_data[3], *tmp_cb;
4152 uint64_t old_txg, txg;
4155 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4157 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4160 tx = dmu_tx_create(os);
4162 cb_data[0] = ztest_create_cb_data(os, 0);
4163 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4165 dmu_tx_hold_write(tx, od[0].od_object, 0, sizeof (uint64_t));
4167 /* Every once in a while, abort the transaction on purpose */
4168 if (ztest_random(100) == 0)
4172 error = dmu_tx_assign(tx, TXG_NOWAIT);
4174 txg = error ? 0 : dmu_tx_get_txg(tx);
4176 cb_data[0]->zcd_txg = txg;
4177 cb_data[1] = ztest_create_cb_data(os, txg);
4178 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4182 * It's not a strict requirement to call the registered
4183 * callbacks from inside dmu_tx_abort(), but that's what
4184 * it's supposed to happen in the current implementation
4185 * so we will check for that.
4187 for (i = 0; i < 2; i++) {
4188 cb_data[i]->zcd_expected_err = ECANCELED;
4189 VERIFY(!cb_data[i]->zcd_called);
4194 for (i = 0; i < 2; i++) {
4195 VERIFY(cb_data[i]->zcd_called);
4196 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4202 cb_data[2] = ztest_create_cb_data(os, txg);
4203 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4206 * Read existing data to make sure there isn't a future leak.
4208 VERIFY(0 == dmu_read(os, od[0].od_object, 0, sizeof (uint64_t),
4209 &old_txg, DMU_READ_PREFETCH));
4212 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4215 dmu_write(os, od[0].od_object, 0, sizeof (uint64_t), &txg, tx);
4217 (void) mutex_lock(&zcl.zcl_callbacks_lock);
4220 * Since commit callbacks don't have any ordering requirement and since
4221 * it is theoretically possible for a commit callback to be called
4222 * after an arbitrary amount of time has elapsed since its txg has been
4223 * synced, it is difficult to reliably determine whether a commit
4224 * callback hasn't been called due to high load or due to a flawed
4227 * In practice, we will assume that if after a certain number of txgs a
4228 * commit callback hasn't been called, then most likely there's an
4229 * implementation bug..
4231 tmp_cb = list_head(&zcl.zcl_callbacks);
4232 if (tmp_cb != NULL &&
4233 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4234 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4235 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4239 * Let's find the place to insert our callbacks.
4241 * Even though the list is ordered by txg, it is possible for the
4242 * insertion point to not be the end because our txg may already be
4243 * quiescing at this point and other callbacks in the open txg
4244 * (from other objsets) may have sneaked in.
4246 tmp_cb = list_tail(&zcl.zcl_callbacks);
4247 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4248 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4250 /* Add the 3 callbacks to the list */
4251 for (i = 0; i < 3; i++) {
4253 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4255 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4258 cb_data[i]->zcd_added = B_TRUE;
4259 VERIFY(!cb_data[i]->zcd_called);
4261 tmp_cb = cb_data[i];
4266 (void) mutex_unlock(&zcl.zcl_callbacks_lock);
4273 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4275 zfs_prop_t proplist[] = {
4277 ZFS_PROP_COMPRESSION,
4281 ztest_shared_t *zs = ztest_shared;
4284 (void) rw_rdlock(&zs->zs_name_lock);
4286 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4287 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4288 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4290 (void) rw_unlock(&zs->zs_name_lock);
4295 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4297 ztest_shared_t *zs = ztest_shared;
4298 nvlist_t *props = NULL;
4300 (void) rw_rdlock(&zs->zs_name_lock);
4302 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4303 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4305 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4307 if (zopt_verbose >= 6)
4308 dump_nvlist(props, 4);
4312 (void) rw_unlock(&zs->zs_name_lock);
4316 * Test snapshot hold/release and deferred destroy.
4319 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4322 objset_t *os = zd->zd_os;
4326 char clonename[100];
4328 char osname[MAXNAMELEN];
4330 (void) rw_rdlock(&ztest_shared->zs_name_lock);
4332 dmu_objset_name(os, osname);
4334 (void) snprintf(snapname, 100, "sh1_%llu", id);
4335 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4336 (void) snprintf(clonename, 100, "%s/ch1_%llu", osname, id);
4337 (void) snprintf(tag, 100, "%tag_%llu", id);
4340 * Clean up from any previous run.
4342 (void) dmu_objset_destroy(clonename, B_FALSE);
4343 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4344 (void) dmu_objset_destroy(fullname, B_FALSE);
4347 * Create snapshot, clone it, mark snap for deferred destroy,
4348 * destroy clone, verify snap was also destroyed.
4350 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4353 if (error == ENOSPC) {
4354 ztest_record_enospc("dmu_objset_snapshot");
4357 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4360 error = dmu_objset_hold(fullname, FTAG, &origin);
4362 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4364 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4365 dmu_objset_rele(origin, FTAG);
4367 if (error == ENOSPC) {
4368 ztest_record_enospc("dmu_objset_clone");
4371 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4374 error = dmu_objset_destroy(fullname, B_TRUE);
4376 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4380 error = dmu_objset_destroy(clonename, B_FALSE);
4382 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4384 error = dmu_objset_hold(fullname, FTAG, &origin);
4385 if (error != ENOENT)
4386 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4389 * Create snapshot, add temporary hold, verify that we can't
4390 * destroy a held snapshot, mark for deferred destroy,
4391 * release hold, verify snapshot was destroyed.
4393 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4396 if (error == ENOSPC) {
4397 ztest_record_enospc("dmu_objset_snapshot");
4400 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4403 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4406 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4408 error = dmu_objset_destroy(fullname, B_FALSE);
4409 if (error != EBUSY) {
4410 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4414 error = dmu_objset_destroy(fullname, B_TRUE);
4416 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4420 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4422 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4424 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4427 (void) rw_unlock(&ztest_shared->zs_name_lock);
4431 * Inject random faults into the on-disk data.
4435 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4437 ztest_shared_t *zs = ztest_shared;
4438 spa_t *spa = zs->zs_spa;
4442 uint64_t bad = 0x1990c0ffeedecadeull;
4444 char path0[MAXPATHLEN];
4445 char pathrand[MAXPATHLEN];
4447 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4453 boolean_t islog = B_FALSE;
4455 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4456 maxfaults = MAXFAULTS();
4457 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4458 mirror_save = zs->zs_mirrors;
4459 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4461 ASSERT(leaves >= 1);
4464 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4466 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4468 if (ztest_random(2) == 0) {
4470 * Inject errors on a normal data device or slog device.
4472 top = ztest_random_vdev_top(spa, B_TRUE);
4473 leaf = ztest_random(leaves) + zs->zs_splits;
4476 * Generate paths to the first leaf in this top-level vdev,
4477 * and to the random leaf we selected. We'll induce transient
4478 * write failures and random online/offline activity on leaf 0,
4479 * and we'll write random garbage to the randomly chosen leaf.
4481 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
4482 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4483 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4484 zopt_dir, zopt_pool, top * leaves + leaf);
4486 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4487 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4490 if (vd0 != NULL && maxfaults != 1) {
4492 * Make vd0 explicitly claim to be unreadable,
4493 * or unwriteable, or reach behind its back
4494 * and close the underlying fd. We can do this if
4495 * maxfaults == 0 because we'll fail and reexecute,
4496 * and we can do it if maxfaults >= 2 because we'll
4497 * have enough redundancy. If maxfaults == 1, the
4498 * combination of this with injection of random data
4499 * corruption below exceeds the pool's fault tolerance.
4501 vdev_file_t *vf = vd0->vdev_tsd;
4503 if (vf != NULL && ztest_random(3) == 0) {
4504 (void) close(vf->vf_vnode->v_fd);
4505 vf->vf_vnode->v_fd = -1;
4506 } else if (ztest_random(2) == 0) {
4507 vd0->vdev_cant_read = B_TRUE;
4509 vd0->vdev_cant_write = B_TRUE;
4511 guid0 = vd0->vdev_guid;
4515 * Inject errors on an l2cache device.
4517 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4519 if (sav->sav_count == 0) {
4520 spa_config_exit(spa, SCL_STATE, FTAG);
4523 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4524 guid0 = vd0->vdev_guid;
4525 (void) strcpy(path0, vd0->vdev_path);
4526 (void) strcpy(pathrand, vd0->vdev_path);
4530 maxfaults = INT_MAX; /* no limit on cache devices */
4533 spa_config_exit(spa, SCL_STATE, FTAG);
4536 * If we can tolerate two or more faults, or we're dealing
4537 * with a slog, randomly online/offline vd0.
4539 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4540 if (ztest_random(10) < 6) {
4541 int flags = (ztest_random(2) == 0 ?
4542 ZFS_OFFLINE_TEMPORARY : 0);
4545 * We have to grab the zs_name_lock as writer to
4546 * prevent a race between offlining a slog and
4547 * destroying a dataset. Offlining the slog will
4548 * grab a reference on the dataset which may cause
4549 * dmu_objset_destroy() to fail with EBUSY thus
4550 * leaving the dataset in an inconsistent state.
4553 (void) rw_wrlock(&ztest_shared->zs_name_lock);
4555 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4558 (void) rw_unlock(&ztest_shared->zs_name_lock);
4560 (void) vdev_online(spa, guid0, 0, NULL);
4568 * We have at least single-fault tolerance, so inject data corruption.
4570 fd = open(pathrand, O_RDWR);
4572 if (fd == -1) /* we hit a gap in the device namespace */
4575 fsize = lseek(fd, 0, SEEK_END);
4577 while (--iters != 0) {
4578 offset = ztest_random(fsize / (leaves << bshift)) *
4579 (leaves << bshift) + (leaf << bshift) +
4580 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4582 if (offset >= fsize)
4585 VERIFY(mutex_lock(&zs->zs_vdev_lock) == 0);
4586 if (mirror_save != zs->zs_mirrors) {
4587 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4592 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4593 fatal(1, "can't inject bad word at 0x%llx in %s",
4596 VERIFY(mutex_unlock(&zs->zs_vdev_lock) == 0);
4598 if (zopt_verbose >= 7)
4599 (void) printf("injected bad word into %s,"
4600 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4607 * Verify that DDT repair works as expected.
4610 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4612 ztest_shared_t *zs = ztest_shared;
4613 spa_t *spa = zs->zs_spa;
4614 objset_t *os = zd->zd_os;
4616 uint64_t object, blocksize, txg, pattern, psize;
4617 enum zio_checksum checksum = spa_dedup_checksum(spa);
4622 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4625 blocksize = ztest_random_blocksize();
4626 blocksize = MIN(blocksize, 2048); /* because we write so many */
4628 ztest_od_init(&od[0], id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4630 if (ztest_object_init(zd, od, sizeof (od), B_FALSE) != 0)
4634 * Take the name lock as writer to prevent anyone else from changing
4635 * the pool and dataset properies we need to maintain during this test.
4637 (void) rw_wrlock(&zs->zs_name_lock);
4639 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4641 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4643 (void) rw_unlock(&zs->zs_name_lock);
4647 object = od[0].od_object;
4648 blocksize = od[0].od_blocksize;
4649 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4651 ASSERT(object != 0);
4653 tx = dmu_tx_create(os);
4654 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4655 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4657 (void) rw_unlock(&zs->zs_name_lock);
4662 * Write all the copies of our block.
4664 for (i = 0; i < copies; i++) {
4665 uint64_t offset = i * blocksize;
4666 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4667 DMU_READ_NO_PREFETCH) == 0);
4668 ASSERT(db->db_offset == offset);
4669 ASSERT(db->db_size == blocksize);
4670 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4671 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4672 dmu_buf_will_fill(db, tx);
4673 ztest_pattern_set(db->db_data, db->db_size, pattern);
4674 dmu_buf_rele(db, FTAG);
4678 txg_wait_synced(spa_get_dsl(spa), txg);
4681 * Find out what block we got.
4683 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4684 DMU_READ_NO_PREFETCH) == 0);
4685 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4686 dmu_buf_rele(db, FTAG);
4689 * Damage the block. Dedup-ditto will save us when we read it later.
4691 psize = BP_GET_PSIZE(&blk);
4692 buf = zio_buf_alloc(psize);
4693 ztest_pattern_set(buf, psize, ~pattern);
4695 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4696 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4697 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4699 zio_buf_free(buf, psize);
4701 (void) rw_unlock(&zs->zs_name_lock);
4709 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4711 ztest_shared_t *zs = ztest_shared;
4712 spa_t *spa = zs->zs_spa;
4714 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4715 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4716 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4720 * Rename the pool to a different name and then rename it back.
4724 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4726 ztest_shared_t *zs = ztest_shared;
4727 char *oldname, *newname;
4730 (void) rw_wrlock(&zs->zs_name_lock);
4732 oldname = zs->zs_pool;
4733 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4734 (void) strcpy(newname, oldname);
4735 (void) strcat(newname, "_tmp");
4740 VERIFY3U(0, ==, spa_rename(oldname, newname));
4743 * Try to open it under the old name, which shouldn't exist
4745 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4748 * Open it under the new name and make sure it's still the same spa_t.
4750 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4752 ASSERT(spa == zs->zs_spa);
4753 spa_close(spa, FTAG);
4756 * Rename it back to the original
4758 VERIFY3U(0, ==, spa_rename(newname, oldname));
4761 * Make sure it can still be opened
4763 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4765 ASSERT(spa == zs->zs_spa);
4766 spa_close(spa, FTAG);
4768 umem_free(newname, strlen(newname) + 1);
4770 (void) rw_unlock(&zs->zs_name_lock);
4774 * Verify pool integrity by running zdb.
4777 ztest_run_zdb(char *pool)
4780 char zdb[MAXPATHLEN + MAXNAMELEN + 20];
4788 (void) realpath(getexecname(), zdb);
4790 /* zdb lives in /usr/sbin, while ztest lives in /usr/bin */
4791 bin = strstr(zdb, "/usr/bin/");
4792 ztest = strstr(bin, "/ztest");
4794 isalen = ztest - isa;
4798 "/usr/sbin%.*s/zdb -bcc%s%s -U %s %s",
4801 zopt_verbose >= 3 ? "s" : "",
4802 zopt_verbose >= 4 ? "v" : "",
4807 if (zopt_verbose >= 5)
4808 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4810 fp = popen(zdb, "r");
4812 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4813 if (zopt_verbose >= 3)
4814 (void) printf("%s", zbuf);
4816 status = pclose(fp);
4821 ztest_dump_core = 0;
4822 if (WIFEXITED(status))
4823 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4825 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4829 ztest_walk_pool_directory(char *header)
4833 if (zopt_verbose >= 6)
4834 (void) printf("%s\n", header);
4836 mutex_enter(&spa_namespace_lock);
4837 while ((spa = spa_next(spa)) != NULL)
4838 if (zopt_verbose >= 6)
4839 (void) printf("\t%s\n", spa_name(spa));
4840 mutex_exit(&spa_namespace_lock);
4844 ztest_spa_import_export(char *oldname, char *newname)
4846 nvlist_t *config, *newconfig;
4850 if (zopt_verbose >= 4) {
4851 (void) printf("import/export: old = %s, new = %s\n",
4856 * Clean up from previous runs.
4858 (void) spa_destroy(newname);
4861 * Get the pool's configuration and guid.
4863 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4866 * Kick off a scrub to tickle scrub/export races.
4868 if (ztest_random(2) == 0)
4869 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4871 pool_guid = spa_guid(spa);
4872 spa_close(spa, FTAG);
4874 ztest_walk_pool_directory("pools before export");
4879 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
4881 ztest_walk_pool_directory("pools after export");
4886 newconfig = spa_tryimport(config);
4887 ASSERT(newconfig != NULL);
4888 nvlist_free(newconfig);
4891 * Import it under the new name.
4893 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
4895 ztest_walk_pool_directory("pools after import");
4898 * Try to import it again -- should fail with EEXIST.
4900 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
4903 * Try to import it under a different name -- should fail with EEXIST.
4905 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
4908 * Verify that the pool is no longer visible under the old name.
4910 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4913 * Verify that we can open and close the pool using the new name.
4915 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4916 ASSERT(pool_guid == spa_guid(spa));
4917 spa_close(spa, FTAG);
4919 nvlist_free(config);
4923 ztest_resume(spa_t *spa)
4925 if (spa_suspended(spa) && zopt_verbose >= 6)
4926 (void) printf("resuming from suspended state\n");
4927 spa_vdev_state_enter(spa, SCL_NONE);
4928 vdev_clear(spa, NULL);
4929 (void) spa_vdev_state_exit(spa, NULL, 0);
4930 (void) zio_resume(spa);
4934 ztest_resume_thread(void *arg)
4938 while (!ztest_exiting) {
4939 if (spa_suspended(spa))
4941 (void) poll(NULL, 0, 100);
4947 ztest_deadman_thread(void *arg)
4949 ztest_shared_t *zs = arg;
4953 delta = (zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + grace;
4955 (void) poll(NULL, 0, (int)(1000 * delta));
4957 fatal(0, "failed to complete within %d seconds of deadline", grace);
4963 ztest_execute(ztest_info_t *zi, uint64_t id)
4965 ztest_shared_t *zs = ztest_shared;
4966 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
4967 hrtime_t functime = gethrtime();
4970 for (i = 0; i < zi->zi_iters; i++)
4971 zi->zi_func(zd, id);
4973 functime = gethrtime() - functime;
4975 atomic_add_64(&zi->zi_call_count, 1);
4976 atomic_add_64(&zi->zi_call_time, functime);
4978 if (zopt_verbose >= 4) {
4980 (void) dladdr((void *)zi->zi_func, &dli);
4981 (void) printf("%6.2f sec in %s\n",
4982 (double)functime / NANOSEC, dli.dli_sname);
4987 ztest_thread(void *arg)
4989 uint64_t id = (uintptr_t)arg;
4990 ztest_shared_t *zs = ztest_shared;
4995 while ((now = gethrtime()) < zs->zs_thread_stop) {
4997 * See if it's time to force a crash.
4999 if (now > zs->zs_thread_kill)
5003 * If we're getting ENOSPC with some regularity, stop.
5005 if (zs->zs_enospc_count > 10)
5009 * Pick a random function to execute.
5011 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
5012 call_next = zi->zi_call_next;
5014 if (now >= call_next &&
5015 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5016 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5017 ztest_execute(zi, id);
5024 ztest_dataset_name(char *dsname, char *pool, int d)
5026 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5030 ztest_dataset_destroy(ztest_shared_t *zs, int d)
5032 char name[MAXNAMELEN];
5035 ztest_dataset_name(name, zs->zs_pool, d);
5037 if (zopt_verbose >= 3)
5038 (void) printf("Destroying %s to free up space\n", name);
5041 * Cleanup any non-standard clones and snapshots. In general,
5042 * ztest thread t operates on dataset (t % zopt_datasets),
5043 * so there may be more than one thing to clean up.
5045 for (t = d; t < zopt_threads; t += zopt_datasets)
5046 ztest_dsl_dataset_cleanup(name, t);
5048 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5049 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5053 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5055 uint64_t usedobjs, dirobjs, scratch;
5058 * ZTEST_DIROBJ is the object directory for the entire dataset.
5059 * Therefore, the number of objects in use should equal the
5060 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5061 * If not, we have an object leak.
5063 * Note that we can only check this in ztest_dataset_open(),
5064 * when the open-context and syncing-context values agree.
5065 * That's because zap_count() returns the open-context value,
5066 * while dmu_objset_space() returns the rootbp fill count.
5068 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5069 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5070 ASSERT3U(dirobjs + 1, ==, usedobjs);
5074 ztest_dataset_open(ztest_shared_t *zs, int d)
5076 ztest_ds_t *zd = &zs->zs_zd[d];
5077 uint64_t committed_seq = zd->zd_seq;
5080 char name[MAXNAMELEN];
5083 ztest_dataset_name(name, zs->zs_pool, d);
5085 (void) rw_rdlock(&zs->zs_name_lock);
5087 error = ztest_dataset_create(name);
5088 if (error == ENOSPC) {
5089 (void) rw_unlock(&zs->zs_name_lock);
5090 ztest_record_enospc(FTAG);
5093 ASSERT(error == 0 || error == EEXIST);
5095 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5096 (void) rw_unlock(&zs->zs_name_lock);
5098 ztest_zd_init(zd, os);
5100 zilog = zd->zd_zilog;
5102 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5103 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5104 fatal(0, "missing log records: claimed %llu < committed %llu",
5105 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5107 ztest_dataset_dirobj_verify(zd);
5109 zil_replay(os, zd, ztest_replay_vector);
5111 ztest_dataset_dirobj_verify(zd);
5113 if (zopt_verbose >= 6)
5114 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5116 (u_longlong_t)zilog->zl_parse_blk_count,
5117 (u_longlong_t)zilog->zl_parse_lr_count,
5118 (u_longlong_t)zilog->zl_replaying_seq);
5120 zilog = zil_open(os, ztest_get_data);
5122 if (zilog->zl_replaying_seq != 0 &&
5123 zilog->zl_replaying_seq < committed_seq)
5124 fatal(0, "missing log records: replayed %llu < committed %llu",
5125 zilog->zl_replaying_seq, committed_seq);
5131 ztest_dataset_close(ztest_shared_t *zs, int d)
5133 ztest_ds_t *zd = &zs->zs_zd[d];
5135 zil_close(zd->zd_zilog);
5136 dmu_objset_rele(zd->zd_os, zd);
5142 * Kick off threads to run tests on all datasets in parallel.
5145 ztest_run(ztest_shared_t *zs)
5149 thread_t resume_tid;
5153 ztest_exiting = B_FALSE;
5156 * Initialize parent/child shared state.
5158 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5159 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5161 zs->zs_thread_start = gethrtime();
5162 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5163 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5164 zs->zs_thread_kill = zs->zs_thread_stop;
5165 if (ztest_random(100) < zopt_killrate)
5166 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5168 (void) _mutex_init(&zcl.zcl_callbacks_lock, USYNC_THREAD, NULL);
5170 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5171 offsetof(ztest_cb_data_t, zcd_node));
5176 kernel_init(FREAD | FWRITE);
5177 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5180 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5183 * We don't expect the pool to suspend unless maxfaults == 0,
5184 * in which case ztest_fault_inject() temporarily takes away
5185 * the only valid replica.
5187 if (MAXFAULTS() == 0)
5188 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5190 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5193 * Create a thread to periodically resume suspended I/O.
5195 VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
5199 * Create a deadman thread to abort() if we hang.
5201 VERIFY(thr_create(0, 0, ztest_deadman_thread, zs, THR_BOUND,
5205 * Verify that we can safely inquire about about any object,
5206 * whether it's allocated or not. To make it interesting,
5207 * we probe a 5-wide window around each power of two.
5208 * This hits all edge cases, including zero and the max.
5210 for (t = 0; t < 64; t++) {
5211 for (d = -5; d <= 5; d++) {
5212 error = dmu_object_info(spa->spa_meta_objset,
5213 (1ULL << t) + d, NULL);
5214 ASSERT(error == 0 || error == ENOENT ||
5220 * If we got any ENOSPC errors on the previous run, destroy something.
5222 if (zs->zs_enospc_count != 0) {
5223 int d = ztest_random(zopt_datasets);
5224 ztest_dataset_destroy(zs, d);
5226 zs->zs_enospc_count = 0;
5228 tid = umem_zalloc(zopt_threads * sizeof (thread_t), UMEM_NOFAIL);
5230 if (zopt_verbose >= 4)
5231 (void) printf("starting main threads...\n");
5234 * Kick off all the tests that run in parallel.
5236 for (t = 0; t < zopt_threads; t++) {
5237 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5239 VERIFY(thr_create(0, 0, ztest_thread, (void *)(uintptr_t)t,
5240 THR_BOUND, &tid[t]) == 0);
5244 * Wait for all of the tests to complete. We go in reverse order
5245 * so we don't close datasets while threads are still using them.
5247 for (t = zopt_threads - 1; t >= 0; t--) {
5248 VERIFY(thr_join(tid[t], NULL, NULL) == 0);
5249 if (t < zopt_datasets)
5250 ztest_dataset_close(zs, t);
5253 txg_wait_synced(spa_get_dsl(spa), 0);
5255 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5256 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5258 umem_free(tid, zopt_threads * sizeof (thread_t));
5260 /* Kill the resume thread */
5261 ztest_exiting = B_TRUE;
5262 VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
5266 * Right before closing the pool, kick off a bunch of async I/O;
5267 * spa_close() should wait for it to complete.
5269 for (uint64_t object = 1; object < 50; object++)
5270 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5272 /* Verify that at least one commit cb was called in a timely fashion */
5273 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5274 VERIFY3U(zc_min_txg_delay, ==, 0);
5276 spa_close(spa, FTAG);
5279 * Verify that we can loop over all pools.
5281 mutex_enter(&spa_namespace_lock);
5282 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5283 if (zopt_verbose > 3)
5284 (void) printf("spa_next: found %s\n", spa_name(spa));
5285 mutex_exit(&spa_namespace_lock);
5288 * Verify that we can export the pool and reimport it under a
5291 if (ztest_random(2) == 0) {
5292 char name[MAXNAMELEN];
5293 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5294 ztest_spa_import_export(zs->zs_pool, name);
5295 ztest_spa_import_export(name, zs->zs_pool);
5300 list_destroy(&zcl.zcl_callbacks);
5302 (void) _mutex_destroy(&zcl.zcl_callbacks_lock);
5304 (void) rwlock_destroy(&zs->zs_name_lock);
5305 (void) _mutex_destroy(&zs->zs_vdev_lock);
5309 ztest_freeze(ztest_shared_t *zs)
5311 ztest_ds_t *zd = &zs->zs_zd[0];
5315 if (zopt_verbose >= 3)
5316 (void) printf("testing spa_freeze()...\n");
5318 kernel_init(FREAD | FWRITE);
5319 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5320 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5323 * Force the first log block to be transactionally allocated.
5324 * We have to do this before we freeze the pool -- otherwise
5325 * the log chain won't be anchored.
5327 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5328 ztest_dmu_object_alloc_free(zd, 0);
5329 zil_commit(zd->zd_zilog, 0);
5332 txg_wait_synced(spa_get_dsl(spa), 0);
5335 * Freeze the pool. This stops spa_sync() from doing anything,
5336 * so that the only way to record changes from now on is the ZIL.
5341 * Run tests that generate log records but don't alter the pool config
5342 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5343 * We do a txg_wait_synced() after each iteration to force the txg
5344 * to increase well beyond the last synced value in the uberblock.
5345 * The ZIL should be OK with that.
5347 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5348 ztest_dmu_write_parallel(zd, 0);
5349 ztest_dmu_object_alloc_free(zd, 0);
5350 txg_wait_synced(spa_get_dsl(spa), 0);
5354 * Commit all of the changes we just generated.
5356 zil_commit(zd->zd_zilog, 0);
5357 txg_wait_synced(spa_get_dsl(spa), 0);
5360 * Close our dataset and close the pool.
5362 ztest_dataset_close(zs, 0);
5363 spa_close(spa, FTAG);
5367 * Open and close the pool and dataset to induce log replay.
5369 kernel_init(FREAD | FWRITE);
5370 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5371 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5372 ztest_dataset_close(zs, 0);
5373 spa_close(spa, FTAG);
5378 print_time(hrtime_t t, char *timebuf)
5380 hrtime_t s = t / NANOSEC;
5381 hrtime_t m = s / 60;
5382 hrtime_t h = m / 60;
5383 hrtime_t d = h / 24;
5392 (void) sprintf(timebuf,
5393 "%llud%02lluh%02llum%02llus", d, h, m, s);
5395 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5397 (void) sprintf(timebuf, "%llum%02llus", m, s);
5399 (void) sprintf(timebuf, "%llus", s);
5403 make_random_props(void)
5407 if (ztest_random(2) == 0)
5410 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5411 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5413 (void) printf("props:\n");
5414 dump_nvlist(props, 4);
5420 * Create a storage pool with the given name and initial vdev size.
5421 * Then test spa_freeze() functionality.
5424 ztest_init(ztest_shared_t *zs)
5427 nvlist_t *nvroot, *props;
5429 VERIFY(_mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL) == 0);
5430 VERIFY(rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL) == 0);
5432 kernel_init(FREAD | FWRITE);
5435 * Create the storage pool.
5437 (void) spa_destroy(zs->zs_pool);
5438 ztest_shared->zs_vdev_next_leaf = 0;
5440 zs->zs_mirrors = zopt_mirrors;
5441 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5442 0, zopt_raidz, zs->zs_mirrors, 1);
5443 props = make_random_props();
5444 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5445 nvlist_free(nvroot);
5447 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5448 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5449 spa_close(spa, FTAG);
5453 ztest_run_zdb(zs->zs_pool);
5457 ztest_run_zdb(zs->zs_pool);
5459 (void) rwlock_destroy(&zs->zs_name_lock);
5460 (void) _mutex_destroy(&zs->zs_vdev_lock);
5464 main(int argc, char **argv)
5476 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5478 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5480 process_options(argc, argv);
5482 /* Override location of zpool.cache */
5483 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
5487 * Blow away any existing copy of zpool.cache
5490 (void) remove(spa_config_path);
5492 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5494 zs = ztest_shared = (void *)mmap(0,
5495 P2ROUNDUP(shared_size, getpagesize()),
5496 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5498 if (zopt_verbose >= 1) {
5499 (void) printf("%llu vdevs, %d datasets, %d threads,"
5500 " %llu seconds...\n",
5501 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5502 (u_longlong_t)zopt_time);
5506 * Create and initialize our storage pool.
5508 for (i = 1; i <= zopt_init; i++) {
5509 bzero(zs, sizeof (ztest_shared_t));
5510 if (zopt_verbose >= 3 && zopt_init != 1)
5511 (void) printf("ztest_init(), pass %d\n", i);
5512 zs->zs_pool = zopt_pool;
5516 zs->zs_pool = zopt_pool;
5517 zs->zs_proc_start = gethrtime();
5518 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5520 for (f = 0; f < ZTEST_FUNCS; f++) {
5521 zi = &zs->zs_info[f];
5522 *zi = ztest_info[f];
5523 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5524 zi->zi_call_next = UINT64_MAX;
5526 zi->zi_call_next = zs->zs_proc_start +
5527 ztest_random(2 * zi->zi_interval[0] + 1);
5531 * Run the tests in a loop. These tests include fault injection
5532 * to verify that self-healing data works, and forced crashes
5533 * to verify that we never lose on-disk consistency.
5535 while (gethrtime() < zs->zs_proc_stop) {
5540 * Initialize the workload counters for each function.
5542 for (f = 0; f < ZTEST_FUNCS; f++) {
5543 zi = &zs->zs_info[f];
5544 zi->zi_call_count = 0;
5545 zi->zi_call_time = 0;
5548 /* Set the allocation switch size */
5549 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5554 fatal(1, "fork failed");
5556 if (pid == 0) { /* child */
5557 struct rlimit rl = { 1024, 1024 };
5558 (void) setrlimit(RLIMIT_NOFILE, &rl);
5559 (void) enable_extended_FILE_stdio(-1, -1);
5564 while (waitpid(pid, &status, 0) != pid)
5567 if (WIFEXITED(status)) {
5568 if (WEXITSTATUS(status) != 0) {
5569 (void) fprintf(stderr,
5570 "child exited with code %d\n",
5571 WEXITSTATUS(status));
5574 } else if (WIFSIGNALED(status)) {
5575 if (WTERMSIG(status) != SIGKILL) {
5576 (void) fprintf(stderr,
5577 "child died with signal %d\n",
5583 (void) fprintf(stderr, "something strange happened "
5590 if (zopt_verbose >= 1) {
5591 hrtime_t now = gethrtime();
5593 now = MIN(now, zs->zs_proc_stop);
5594 print_time(zs->zs_proc_stop - now, timebuf);
5595 nicenum(zs->zs_space, numbuf);
5597 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5598 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5600 WIFEXITED(status) ? "Complete" : "SIGKILL",
5601 (u_longlong_t)zs->zs_enospc_count,
5602 100.0 * zs->zs_alloc / zs->zs_space,
5604 100.0 * (now - zs->zs_proc_start) /
5605 (zopt_time * NANOSEC), timebuf);
5608 if (zopt_verbose >= 2) {
5609 (void) printf("\nWorkload summary:\n\n");
5610 (void) printf("%7s %9s %s\n",
5611 "Calls", "Time", "Function");
5612 (void) printf("%7s %9s %s\n",
5613 "-----", "----", "--------");
5614 for (f = 0; f < ZTEST_FUNCS; f++) {
5617 zi = &zs->zs_info[f];
5618 print_time(zi->zi_call_time, timebuf);
5619 (void) dladdr((void *)zi->zi_func, &dli);
5620 (void) printf("%7llu %9s %s\n",
5621 (u_longlong_t)zi->zi_call_count, timebuf,
5624 (void) printf("\n");
5628 * It's possible that we killed a child during a rename test,
5629 * in which case we'll have a 'ztest_tmp' pool lying around
5630 * instead of 'ztest'. Do a blind rename in case this happened.
5633 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5634 spa_close(spa, FTAG);
5636 char tmpname[MAXNAMELEN];
5638 kernel_init(FREAD | FWRITE);
5639 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5641 (void) spa_rename(tmpname, zopt_pool);
5645 ztest_run_zdb(zopt_pool);
5648 if (zopt_verbose >= 1) {
5649 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5650 kills, iters - kills, (100.0 * kills) / MAX(1, iters));