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.
23 * Copyright (c) 2011 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 * The objective of this program is to provide a DMU/ZAP/SPA stress test
29 * that runs entirely in userland, is easy to use, and easy to extend.
31 * The overall design of the ztest program is as follows:
33 * (1) For each major functional area (e.g. adding vdevs to a pool,
34 * creating and destroying datasets, reading and writing objects, etc)
35 * we have a simple routine to test that functionality. These
36 * individual routines do not have to do anything "stressful".
38 * (2) We turn these simple functionality tests into a stress test by
39 * running them all in parallel, with as many threads as desired,
40 * and spread across as many datasets, objects, and vdevs as desired.
42 * (3) While all this is happening, we inject faults into the pool to
43 * verify that self-healing data really works.
45 * (4) Every time we open a dataset, we change its checksum and compression
46 * functions. Thus even individual objects vary from block to block
47 * in which checksum they use and whether they're compressed.
49 * (5) To verify that we never lose on-disk consistency after a crash,
50 * we run the entire test in a child of the main process.
51 * At random times, the child self-immolates with a SIGKILL.
52 * This is the software equivalent of pulling the power cord.
53 * The parent then runs the test again, using the existing
54 * storage pool, as many times as desired.
56 * (6) To verify that we don't have future leaks or temporal incursions,
57 * many of the functional tests record the transaction group number
58 * as part of their data. When reading old data, they verify that
59 * the transaction group number is less than the current, open txg.
60 * If you add a new test, please do this if applicable.
62 * (7) Threads are created with a reduced stack size, for sanity checking.
63 * Therefore, it's important not to allocate huge buffers on the stack.
65 * When run with no arguments, ztest runs for about five minutes and
66 * produces no output if successful. To get a little bit of information,
67 * specify -V. To get more information, specify -VV, and so on.
69 * To turn this into an overnight stress test, use -T to specify run time.
71 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
72 * to increase the pool capacity, fanout, and overall stress level.
74 * The -N(okill) option will suppress kills, so each child runs to completion.
75 * This can be useful when you're trying to distinguish temporal incursions
76 * from plain old race conditions.
79 #include <sys/zfs_context.h>
85 #include <sys/dmu_objset.h>
91 #include <sys/resource.h>
94 #include <sys/zil_impl.h>
95 #include <sys/vdev_impl.h>
96 #include <sys/vdev_file.h>
97 #include <sys/spa_impl.h>
98 #include <sys/metaslab_impl.h>
99 #include <sys/dsl_prop.h>
100 #include <sys/dsl_dataset.h>
101 #include <sys/dsl_scan.h>
102 #include <sys/zio_checksum.h>
103 #include <sys/refcount.h>
105 #include <stdio_ext.h>
113 #include <sys/fs/zfs.h>
114 #include <libnvpair.h>
116 static char cmdname[] = "ztest";
117 static char *zopt_pool = cmdname;
119 static uint64_t zopt_vdevs = 5;
120 static uint64_t zopt_vdevtime;
121 static int zopt_ashift = SPA_MINBLOCKSHIFT;
122 static int zopt_mirrors = 2;
123 static int zopt_raidz = 4;
124 static int zopt_raidz_parity = 1;
125 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
126 static int zopt_datasets = 7;
127 static int zopt_threads = 23;
128 static uint64_t zopt_passtime = 60; /* 60 seconds */
129 static uint64_t zopt_killrate = 70; /* 70% kill rate */
130 static int zopt_verbose = 0;
131 static int zopt_init = 1;
132 static char *zopt_dir = "/tmp";
133 static uint64_t zopt_time = 300; /* 5 minutes */
134 static uint64_t zopt_maxloops = 50; /* max loops during spa_freeze() */
136 #define BT_MAGIC 0x123456789abcdefULL
137 #define MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
141 ZTEST_IO_WRITE_PATTERN,
142 ZTEST_IO_WRITE_ZEROES,
148 typedef struct ztest_block_tag {
158 typedef struct bufwad {
165 * XXX -- fix zfs range locks to be generic so we can use them here.
187 #define ZTEST_RANGE_LOCKS 64
188 #define ZTEST_OBJECT_LOCKS 64
191 * Object descriptor. Used as a template for object lookup/create/remove.
193 typedef struct ztest_od {
196 dmu_object_type_t od_type;
197 dmu_object_type_t od_crtype;
198 uint64_t od_blocksize;
199 uint64_t od_crblocksize;
202 char od_name[MAXNAMELEN];
208 typedef struct ztest_ds {
210 krwlock_t zd_zilog_lock;
213 ztest_od_t *zd_od; /* debugging aid */
214 char zd_name[MAXNAMELEN];
215 kmutex_t zd_dirobj_lock;
216 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
217 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
221 * Per-iteration state.
223 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
225 typedef struct ztest_info {
226 ztest_func_t *zi_func; /* test function */
227 uint64_t zi_iters; /* iterations per execution */
228 uint64_t *zi_interval; /* execute every <interval> seconds */
229 uint64_t zi_call_count; /* per-pass count */
230 uint64_t zi_call_time; /* per-pass time */
231 uint64_t zi_call_next; /* next time to call this function */
235 * Note: these aren't static because we want dladdr() to work.
237 ztest_func_t ztest_dmu_read_write;
238 ztest_func_t ztest_dmu_write_parallel;
239 ztest_func_t ztest_dmu_object_alloc_free;
240 ztest_func_t ztest_dmu_commit_callbacks;
241 ztest_func_t ztest_zap;
242 ztest_func_t ztest_zap_parallel;
243 ztest_func_t ztest_zil_commit;
244 ztest_func_t ztest_zil_remount;
245 ztest_func_t ztest_dmu_read_write_zcopy;
246 ztest_func_t ztest_dmu_objset_create_destroy;
247 ztest_func_t ztest_dmu_prealloc;
248 ztest_func_t ztest_fzap;
249 ztest_func_t ztest_dmu_snapshot_create_destroy;
250 ztest_func_t ztest_dsl_prop_get_set;
251 ztest_func_t ztest_spa_prop_get_set;
252 ztest_func_t ztest_spa_create_destroy;
253 ztest_func_t ztest_fault_inject;
254 ztest_func_t ztest_ddt_repair;
255 ztest_func_t ztest_dmu_snapshot_hold;
256 ztest_func_t ztest_spa_rename;
257 ztest_func_t ztest_scrub;
258 ztest_func_t ztest_dsl_dataset_promote_busy;
259 ztest_func_t ztest_vdev_attach_detach;
260 ztest_func_t ztest_vdev_LUN_growth;
261 ztest_func_t ztest_vdev_add_remove;
262 ztest_func_t ztest_vdev_aux_add_remove;
263 ztest_func_t ztest_split_pool;
264 ztest_func_t ztest_reguid;
266 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
267 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
268 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
269 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
270 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
272 ztest_info_t ztest_info[] = {
273 { ztest_dmu_read_write, 1, &zopt_always },
274 { ztest_dmu_write_parallel, 10, &zopt_always },
275 { ztest_dmu_object_alloc_free, 1, &zopt_always },
276 { ztest_dmu_commit_callbacks, 1, &zopt_always },
277 { ztest_zap, 30, &zopt_always },
278 { ztest_zap_parallel, 100, &zopt_always },
279 { ztest_split_pool, 1, &zopt_always },
280 { ztest_zil_commit, 1, &zopt_incessant },
281 { ztest_zil_remount, 1, &zopt_sometimes },
282 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
283 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
284 { ztest_dsl_prop_get_set, 1, &zopt_often },
285 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
287 { ztest_dmu_prealloc, 1, &zopt_sometimes },
289 { ztest_fzap, 1, &zopt_sometimes },
290 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
291 { ztest_spa_create_destroy, 1, &zopt_sometimes },
292 { ztest_fault_inject, 1, &zopt_sometimes },
293 { ztest_ddt_repair, 1, &zopt_sometimes },
294 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
295 { ztest_reguid, 1, &zopt_sometimes },
296 { ztest_spa_rename, 1, &zopt_rarely },
297 { ztest_scrub, 1, &zopt_rarely },
298 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
299 { ztest_vdev_attach_detach, 1, &zopt_rarely },
300 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
301 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
302 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
305 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
308 * The following struct is used to hold a list of uncalled commit callbacks.
309 * The callbacks are ordered by txg number.
311 typedef struct ztest_cb_list {
312 kmutex_t zcl_callbacks_lock;
313 list_t zcl_callbacks;
317 * Stuff we need to share writably between parent and child.
319 typedef struct ztest_shared {
322 hrtime_t zs_proc_start;
323 hrtime_t zs_proc_stop;
324 hrtime_t zs_thread_start;
325 hrtime_t zs_thread_stop;
326 hrtime_t zs_thread_kill;
327 uint64_t zs_enospc_count;
328 uint64_t zs_vdev_next_leaf;
329 uint64_t zs_vdev_aux;
333 kmutex_t zs_vdev_lock;
334 krwlock_t zs_name_lock;
335 ztest_info_t zs_info[ZTEST_FUNCS];
341 #define ID_PARALLEL -1ULL
343 static char ztest_dev_template[] = "%s/%s.%llua";
344 static char ztest_aux_template[] = "%s/%s.%s.%llu";
345 ztest_shared_t *ztest_shared;
348 static int ztest_random_fd;
349 static int ztest_dump_core = 1;
351 static boolean_t ztest_exiting;
353 /* Global commit callback list */
354 static ztest_cb_list_t zcl;
355 /* Commit cb delay */
356 static uint64_t zc_min_txg_delay = UINT64_MAX;
357 static int zc_cb_counter = 0;
360 * Minimum number of commit callbacks that need to be registered for us to check
361 * whether the minimum txg delay is acceptable.
363 #define ZTEST_COMMIT_CB_MIN_REG 100
366 * If a number of txgs equal to this threshold have been created after a commit
367 * callback has been registered but not called, then we assume there is an
368 * implementation bug.
370 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
372 extern uint64_t metaslab_gang_bang;
373 extern uint64_t metaslab_df_alloc_threshold;
374 static uint64_t metaslab_sz;
377 ZTEST_META_DNODE = 0,
382 static void usage(boolean_t) __NORETURN;
385 * These libumem hooks provide a reasonable set of defaults for the allocator's
386 * debugging facilities.
389 _umem_debug_init(void)
391 return ("default,verbose"); /* $UMEM_DEBUG setting */
395 _umem_logging_init(void)
397 return ("fail,contents"); /* $UMEM_LOGGING setting */
400 #define FATAL_MSG_SZ 1024
405 fatal(int do_perror, char *message, ...)
408 int save_errno = errno;
411 (void) fflush(stdout);
412 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
414 va_start(args, message);
415 (void) sprintf(buf, "ztest: ");
417 (void) vsprintf(buf + strlen(buf), message, args);
420 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
421 ": %s", strerror(save_errno));
423 (void) fprintf(stderr, "%s\n", buf);
424 fatal_msg = buf; /* to ease debugging */
431 str2shift(const char *buf)
433 const char *ends = "BKMGTPEZ";
438 for (i = 0; i < strlen(ends); i++) {
439 if (toupper(buf[0]) == ends[i])
442 if (i == strlen(ends)) {
443 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
447 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
450 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
456 nicenumtoull(const char *buf)
461 val = strtoull(buf, &end, 0);
463 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
465 } else if (end[0] == '.') {
466 double fval = strtod(buf, &end);
467 fval *= pow(2, str2shift(end));
468 if (fval > UINT64_MAX) {
469 (void) fprintf(stderr, "ztest: value too large: %s\n",
473 val = (uint64_t)fval;
475 int shift = str2shift(end);
476 if (shift >= 64 || (val << shift) >> shift != val) {
477 (void) fprintf(stderr, "ztest: value too large: %s\n",
487 usage(boolean_t requested)
489 char nice_vdev_size[10];
490 char nice_gang_bang[10];
491 FILE *fp = requested ? stdout : stderr;
493 nicenum(zopt_vdev_size, nice_vdev_size);
494 nicenum(metaslab_gang_bang, nice_gang_bang);
496 (void) fprintf(fp, "Usage: %s\n"
497 "\t[-v vdevs (default: %llu)]\n"
498 "\t[-s size_of_each_vdev (default: %s)]\n"
499 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
500 "\t[-m mirror_copies (default: %d)]\n"
501 "\t[-r raidz_disks (default: %d)]\n"
502 "\t[-R raidz_parity (default: %d)]\n"
503 "\t[-d datasets (default: %d)]\n"
504 "\t[-t threads (default: %d)]\n"
505 "\t[-g gang_block_threshold (default: %s)]\n"
506 "\t[-i init_count (default: %d)] initialize pool i times\n"
507 "\t[-k kill_percentage (default: %llu%%)]\n"
508 "\t[-p pool_name (default: %s)]\n"
509 "\t[-f dir (default: %s)] file directory for vdev files\n"
510 "\t[-V] verbose (use multiple times for ever more blather)\n"
511 "\t[-E] use existing pool instead of creating new one\n"
512 "\t[-T time (default: %llu sec)] total run time\n"
513 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
514 "\t[-P passtime (default: %llu sec)] time per pass\n"
515 "\t[-h] (print help)\n"
518 (u_longlong_t)zopt_vdevs, /* -v */
519 nice_vdev_size, /* -s */
520 zopt_ashift, /* -a */
521 zopt_mirrors, /* -m */
523 zopt_raidz_parity, /* -R */
524 zopt_datasets, /* -d */
525 zopt_threads, /* -t */
526 nice_gang_bang, /* -g */
528 (u_longlong_t)zopt_killrate, /* -k */
531 (u_longlong_t)zopt_time, /* -T */
532 (u_longlong_t)zopt_maxloops, /* -F */
533 (u_longlong_t)zopt_passtime); /* -P */
534 exit(requested ? 0 : 1);
538 process_options(int argc, char **argv)
543 /* By default, test gang blocks for blocks 32K and greater */
544 metaslab_gang_bang = 32 << 10;
546 while ((opt = getopt(argc, argv,
547 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
564 value = nicenumtoull(optarg);
571 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
577 zopt_mirrors = value;
580 zopt_raidz = MAX(1, value);
583 zopt_raidz_parity = MIN(MAX(value, 1), 3);
586 zopt_datasets = MAX(1, value);
589 zopt_threads = MAX(1, value);
592 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
598 zopt_killrate = value;
601 zopt_pool = strdup(optarg);
604 zopt_dir = strdup(optarg);
616 zopt_passtime = MAX(1, value);
619 zopt_maxloops = MAX(1, value);
631 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
633 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
638 ztest_kill(ztest_shared_t *zs)
640 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
641 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
642 (void) kill(getpid(), SIGKILL);
646 ztest_random(uint64_t range)
653 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
654 fatal(1, "short read from /dev/urandom");
661 ztest_record_enospc(const char *s)
663 ztest_shared->zs_enospc_count++;
667 ztest_get_ashift(void)
669 if (zopt_ashift == 0)
670 return (SPA_MINBLOCKSHIFT + ztest_random(3));
671 return (zopt_ashift);
675 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
681 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
684 ashift = ztest_get_ashift();
690 vdev = ztest_shared->zs_vdev_aux;
691 (void) sprintf(path, ztest_aux_template,
692 zopt_dir, zopt_pool, aux, vdev);
694 vdev = ztest_shared->zs_vdev_next_leaf++;
695 (void) sprintf(path, ztest_dev_template,
696 zopt_dir, zopt_pool, vdev);
701 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
703 fatal(1, "can't open %s", path);
704 if (ftruncate(fd, size) != 0)
705 fatal(1, "can't ftruncate %s", path);
709 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
710 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
711 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
712 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
713 umem_free(pathbuf, MAXPATHLEN);
719 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
721 nvlist_t *raidz, **child;
725 return (make_vdev_file(path, aux, size, ashift));
726 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
728 for (c = 0; c < r; c++)
729 child[c] = make_vdev_file(path, aux, size, ashift);
731 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
732 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
733 VDEV_TYPE_RAIDZ) == 0);
734 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
735 zopt_raidz_parity) == 0);
736 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
739 for (c = 0; c < r; c++)
740 nvlist_free(child[c]);
742 umem_free(child, r * sizeof (nvlist_t *));
748 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
751 nvlist_t *mirror, **child;
755 return (make_vdev_raidz(path, aux, size, ashift, r));
757 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
759 for (c = 0; c < m; c++)
760 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
762 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
763 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
764 VDEV_TYPE_MIRROR) == 0);
765 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
768 for (c = 0; c < m; c++)
769 nvlist_free(child[c]);
771 umem_free(child, m * sizeof (nvlist_t *));
777 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
778 int log, int r, int m, int t)
780 nvlist_t *root, **child;
785 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
787 for (c = 0; c < t; c++) {
788 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
789 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
793 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
794 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
795 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
798 for (c = 0; c < t; c++)
799 nvlist_free(child[c]);
801 umem_free(child, t * sizeof (nvlist_t *));
807 ztest_random_blocksize(void)
809 return (1 << (SPA_MINBLOCKSHIFT +
810 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
814 ztest_random_ibshift(void)
816 return (DN_MIN_INDBLKSHIFT +
817 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
821 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
824 vdev_t *rvd = spa->spa_root_vdev;
827 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
830 top = ztest_random(rvd->vdev_children);
831 tvd = rvd->vdev_child[top];
832 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
833 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
839 ztest_random_dsl_prop(zfs_prop_t prop)
844 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
845 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
851 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
854 const char *propname = zfs_prop_to_name(prop);
860 error = dsl_prop_set(osname, propname,
861 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
862 sizeof (value), 1, &value);
864 if (error == ENOSPC) {
865 ztest_record_enospc(FTAG);
868 ASSERT3U(error, ==, 0);
870 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
871 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
872 1, &curval, setpoint), ==, 0);
874 if (zopt_verbose >= 6) {
875 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
876 (void) printf("%s %s = %s at '%s'\n",
877 osname, propname, valname, setpoint);
879 umem_free(setpoint, MAXPATHLEN);
885 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
887 spa_t *spa = zs->zs_spa;
888 nvlist_t *props = NULL;
891 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
892 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
894 error = spa_prop_set(spa, props);
898 if (error == ENOSPC) {
899 ztest_record_enospc(FTAG);
902 ASSERT3U(error, ==, 0);
908 ztest_rll_init(rll_t *rll)
910 rll->rll_writer = NULL;
911 rll->rll_readers = 0;
912 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
913 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
917 ztest_rll_destroy(rll_t *rll)
919 ASSERT(rll->rll_writer == NULL);
920 ASSERT(rll->rll_readers == 0);
921 mutex_destroy(&rll->rll_lock);
922 cv_destroy(&rll->rll_cv);
926 ztest_rll_lock(rll_t *rll, rl_type_t type)
928 mutex_enter(&rll->rll_lock);
930 if (type == RL_READER) {
931 while (rll->rll_writer != NULL)
932 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
935 while (rll->rll_writer != NULL || rll->rll_readers)
936 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
937 rll->rll_writer = curthread;
940 mutex_exit(&rll->rll_lock);
944 ztest_rll_unlock(rll_t *rll)
946 mutex_enter(&rll->rll_lock);
948 if (rll->rll_writer) {
949 ASSERT(rll->rll_readers == 0);
950 rll->rll_writer = NULL;
952 ASSERT(rll->rll_readers != 0);
953 ASSERT(rll->rll_writer == NULL);
957 if (rll->rll_writer == NULL && rll->rll_readers == 0)
958 cv_broadcast(&rll->rll_cv);
960 mutex_exit(&rll->rll_lock);
964 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
966 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
968 ztest_rll_lock(rll, type);
972 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
974 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
976 ztest_rll_unlock(rll);
980 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
981 uint64_t size, rl_type_t type)
983 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
984 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
987 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
988 rl->rl_object = object;
989 rl->rl_offset = offset;
993 ztest_rll_lock(rll, type);
999 ztest_range_unlock(rl_t *rl)
1001 rll_t *rll = rl->rl_lock;
1003 ztest_rll_unlock(rll);
1005 umem_free(rl, sizeof (*rl));
1009 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
1012 zd->zd_zilog = dmu_objset_zil(os);
1014 dmu_objset_name(os, zd->zd_name);
1017 rw_init(&zd->zd_zilog_lock, NULL, RW_DEFAULT, NULL);
1018 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1020 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1021 ztest_rll_init(&zd->zd_object_lock[l]);
1023 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1024 ztest_rll_init(&zd->zd_range_lock[l]);
1028 ztest_zd_fini(ztest_ds_t *zd)
1032 mutex_destroy(&zd->zd_dirobj_lock);
1033 rw_destroy(&zd->zd_zilog_lock);
1035 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1036 ztest_rll_destroy(&zd->zd_object_lock[l]);
1038 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1039 ztest_rll_destroy(&zd->zd_range_lock[l]);
1042 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1045 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1051 * Attempt to assign tx to some transaction group.
1053 error = dmu_tx_assign(tx, txg_how);
1055 if (error == ERESTART) {
1056 ASSERT(txg_how == TXG_NOWAIT);
1059 ASSERT3U(error, ==, ENOSPC);
1060 ztest_record_enospc(tag);
1065 txg = dmu_tx_get_txg(tx);
1071 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1074 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1082 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1085 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1089 diff |= (value - *ip++);
1096 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1097 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1099 bt->bt_magic = BT_MAGIC;
1100 bt->bt_objset = dmu_objset_id(os);
1101 bt->bt_object = object;
1102 bt->bt_offset = offset;
1105 bt->bt_crtxg = crtxg;
1109 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1110 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1112 ASSERT(bt->bt_magic == BT_MAGIC);
1113 ASSERT(bt->bt_objset == dmu_objset_id(os));
1114 ASSERT(bt->bt_object == object);
1115 ASSERT(bt->bt_offset == offset);
1116 ASSERT(bt->bt_gen <= gen);
1117 ASSERT(bt->bt_txg <= txg);
1118 ASSERT(bt->bt_crtxg == crtxg);
1121 static ztest_block_tag_t *
1122 ztest_bt_bonus(dmu_buf_t *db)
1124 dmu_object_info_t doi;
1125 ztest_block_tag_t *bt;
1127 dmu_object_info_from_db(db, &doi);
1128 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1129 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1130 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1139 #define lrz_type lr_mode
1140 #define lrz_blocksize lr_uid
1141 #define lrz_ibshift lr_gid
1142 #define lrz_bonustype lr_rdev
1143 #define lrz_bonuslen lr_crtime[1]
1146 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1148 char *name = (void *)(lr + 1); /* name follows lr */
1149 size_t namesize = strlen(name) + 1;
1152 if (zil_replaying(zd->zd_zilog, tx))
1155 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1156 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1157 sizeof (*lr) + namesize - sizeof (lr_t));
1159 zil_itx_assign(zd->zd_zilog, itx, tx);
1163 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1165 char *name = (void *)(lr + 1); /* name follows lr */
1166 size_t namesize = strlen(name) + 1;
1169 if (zil_replaying(zd->zd_zilog, tx))
1172 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1173 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1174 sizeof (*lr) + namesize - sizeof (lr_t));
1176 itx->itx_oid = object;
1177 zil_itx_assign(zd->zd_zilog, itx, tx);
1181 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1184 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1186 if (zil_replaying(zd->zd_zilog, tx))
1189 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1190 write_state = WR_INDIRECT;
1192 itx = zil_itx_create(TX_WRITE,
1193 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1195 if (write_state == WR_COPIED &&
1196 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1197 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1198 zil_itx_destroy(itx);
1199 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1200 write_state = WR_NEED_COPY;
1202 itx->itx_private = zd;
1203 itx->itx_wr_state = write_state;
1204 itx->itx_sync = (ztest_random(8) == 0);
1205 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1207 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1208 sizeof (*lr) - sizeof (lr_t));
1210 zil_itx_assign(zd->zd_zilog, itx, tx);
1214 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1218 if (zil_replaying(zd->zd_zilog, tx))
1221 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1222 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1223 sizeof (*lr) - sizeof (lr_t));
1225 itx->itx_sync = B_FALSE;
1226 zil_itx_assign(zd->zd_zilog, itx, tx);
1230 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1234 if (zil_replaying(zd->zd_zilog, tx))
1237 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1238 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1239 sizeof (*lr) - sizeof (lr_t));
1241 itx->itx_sync = B_FALSE;
1242 zil_itx_assign(zd->zd_zilog, itx, tx);
1249 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1251 char *name = (void *)(lr + 1); /* name follows lr */
1252 objset_t *os = zd->zd_os;
1253 ztest_block_tag_t *bbt;
1260 byteswap_uint64_array(lr, sizeof (*lr));
1262 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1263 ASSERT(name[0] != '\0');
1265 tx = dmu_tx_create(os);
1267 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1269 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1270 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1272 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1275 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1279 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1281 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1282 if (lr->lr_foid == 0) {
1283 lr->lr_foid = zap_create(os,
1284 lr->lrz_type, lr->lrz_bonustype,
1285 lr->lrz_bonuslen, tx);
1287 error = zap_create_claim(os, lr->lr_foid,
1288 lr->lrz_type, lr->lrz_bonustype,
1289 lr->lrz_bonuslen, tx);
1292 if (lr->lr_foid == 0) {
1293 lr->lr_foid = dmu_object_alloc(os,
1294 lr->lrz_type, 0, lr->lrz_bonustype,
1295 lr->lrz_bonuslen, tx);
1297 error = dmu_object_claim(os, lr->lr_foid,
1298 lr->lrz_type, 0, lr->lrz_bonustype,
1299 lr->lrz_bonuslen, tx);
1304 ASSERT3U(error, ==, EEXIST);
1305 ASSERT(zd->zd_zilog->zl_replay);
1310 ASSERT(lr->lr_foid != 0);
1312 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1313 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1314 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1316 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1317 bbt = ztest_bt_bonus(db);
1318 dmu_buf_will_dirty(db, tx);
1319 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1320 dmu_buf_rele(db, FTAG);
1322 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1325 (void) ztest_log_create(zd, tx, lr);
1333 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1335 char *name = (void *)(lr + 1); /* name follows lr */
1336 objset_t *os = zd->zd_os;
1337 dmu_object_info_t doi;
1339 uint64_t object, txg;
1342 byteswap_uint64_array(lr, sizeof (*lr));
1344 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1345 ASSERT(name[0] != '\0');
1348 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1349 ASSERT(object != 0);
1351 ztest_object_lock(zd, object, RL_WRITER);
1353 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1355 tx = dmu_tx_create(os);
1357 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1358 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1360 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1362 ztest_object_unlock(zd, object);
1366 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1367 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1369 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1372 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1374 (void) ztest_log_remove(zd, tx, lr, object);
1378 ztest_object_unlock(zd, object);
1384 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1386 objset_t *os = zd->zd_os;
1387 void *data = lr + 1; /* data follows lr */
1388 uint64_t offset, length;
1389 ztest_block_tag_t *bt = data;
1390 ztest_block_tag_t *bbt;
1391 uint64_t gen, txg, lrtxg, crtxg;
1392 dmu_object_info_t doi;
1395 arc_buf_t *abuf = NULL;
1399 byteswap_uint64_array(lr, sizeof (*lr));
1401 offset = lr->lr_offset;
1402 length = lr->lr_length;
1404 /* If it's a dmu_sync() block, write the whole block */
1405 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1406 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1407 if (length < blocksize) {
1408 offset -= offset % blocksize;
1413 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1414 byteswap_uint64_array(bt, sizeof (*bt));
1416 if (bt->bt_magic != BT_MAGIC)
1419 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1420 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1422 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1424 dmu_object_info_from_db(db, &doi);
1426 bbt = ztest_bt_bonus(db);
1427 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1429 crtxg = bbt->bt_crtxg;
1430 lrtxg = lr->lr_common.lrc_txg;
1432 tx = dmu_tx_create(os);
1434 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1436 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1437 P2PHASE(offset, length) == 0)
1438 abuf = dmu_request_arcbuf(db, length);
1440 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1443 dmu_return_arcbuf(abuf);
1444 dmu_buf_rele(db, FTAG);
1445 ztest_range_unlock(rl);
1446 ztest_object_unlock(zd, lr->lr_foid);
1452 * Usually, verify the old data before writing new data --
1453 * but not always, because we also want to verify correct
1454 * behavior when the data was not recently read into cache.
1456 ASSERT(offset % doi.doi_data_block_size == 0);
1457 if (ztest_random(4) != 0) {
1458 int prefetch = ztest_random(2) ?
1459 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1460 ztest_block_tag_t rbt;
1462 VERIFY(dmu_read(os, lr->lr_foid, offset,
1463 sizeof (rbt), &rbt, prefetch) == 0);
1464 if (rbt.bt_magic == BT_MAGIC) {
1465 ztest_bt_verify(&rbt, os, lr->lr_foid,
1466 offset, gen, txg, crtxg);
1471 * Writes can appear to be newer than the bonus buffer because
1472 * the ztest_get_data() callback does a dmu_read() of the
1473 * open-context data, which may be different than the data
1474 * as it was when the write was generated.
1476 if (zd->zd_zilog->zl_replay) {
1477 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1478 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1483 * Set the bt's gen/txg to the bonus buffer's gen/txg
1484 * so that all of the usual ASSERTs will work.
1486 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1490 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1492 bcopy(data, abuf->b_data, length);
1493 dmu_assign_arcbuf(db, offset, abuf, tx);
1496 (void) ztest_log_write(zd, tx, lr);
1498 dmu_buf_rele(db, FTAG);
1502 ztest_range_unlock(rl);
1503 ztest_object_unlock(zd, lr->lr_foid);
1509 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1511 objset_t *os = zd->zd_os;
1517 byteswap_uint64_array(lr, sizeof (*lr));
1519 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1520 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1523 tx = dmu_tx_create(os);
1525 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1527 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1529 ztest_range_unlock(rl);
1530 ztest_object_unlock(zd, lr->lr_foid);
1534 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1535 lr->lr_length, tx) == 0);
1537 (void) ztest_log_truncate(zd, tx, lr);
1541 ztest_range_unlock(rl);
1542 ztest_object_unlock(zd, lr->lr_foid);
1548 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1550 objset_t *os = zd->zd_os;
1553 ztest_block_tag_t *bbt;
1554 uint64_t txg, lrtxg, crtxg;
1557 byteswap_uint64_array(lr, sizeof (*lr));
1559 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1561 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1563 tx = dmu_tx_create(os);
1564 dmu_tx_hold_bonus(tx, lr->lr_foid);
1566 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1568 dmu_buf_rele(db, FTAG);
1569 ztest_object_unlock(zd, lr->lr_foid);
1573 bbt = ztest_bt_bonus(db);
1574 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1575 crtxg = bbt->bt_crtxg;
1576 lrtxg = lr->lr_common.lrc_txg;
1578 if (zd->zd_zilog->zl_replay) {
1579 ASSERT(lr->lr_size != 0);
1580 ASSERT(lr->lr_mode != 0);
1584 * Randomly change the size and increment the generation.
1586 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1588 lr->lr_mode = bbt->bt_gen + 1;
1593 * Verify that the current bonus buffer is not newer than our txg.
1595 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1596 MAX(txg, lrtxg), crtxg);
1598 dmu_buf_will_dirty(db, tx);
1600 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1601 ASSERT3U(lr->lr_size, <=, db->db_size);
1602 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1603 bbt = ztest_bt_bonus(db);
1605 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1607 dmu_buf_rele(db, FTAG);
1609 (void) ztest_log_setattr(zd, tx, lr);
1613 ztest_object_unlock(zd, lr->lr_foid);
1618 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1619 NULL, /* 0 no such transaction type */
1620 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1621 NULL, /* TX_MKDIR */
1622 NULL, /* TX_MKXATTR */
1623 NULL, /* TX_SYMLINK */
1624 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1625 NULL, /* TX_RMDIR */
1627 NULL, /* TX_RENAME */
1628 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1629 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1630 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1632 NULL, /* TX_CREATE_ACL */
1633 NULL, /* TX_CREATE_ATTR */
1634 NULL, /* TX_CREATE_ACL_ATTR */
1635 NULL, /* TX_MKDIR_ACL */
1636 NULL, /* TX_MKDIR_ATTR */
1637 NULL, /* TX_MKDIR_ACL_ATTR */
1638 NULL, /* TX_WRITE2 */
1642 * ZIL get_data callbacks
1646 ztest_get_done(zgd_t *zgd, int error)
1648 ztest_ds_t *zd = zgd->zgd_private;
1649 uint64_t object = zgd->zgd_rl->rl_object;
1652 dmu_buf_rele(zgd->zgd_db, zgd);
1654 ztest_range_unlock(zgd->zgd_rl);
1655 ztest_object_unlock(zd, object);
1657 if (error == 0 && zgd->zgd_bp)
1658 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1660 umem_free(zgd, sizeof (*zgd));
1664 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1666 ztest_ds_t *zd = arg;
1667 objset_t *os = zd->zd_os;
1668 uint64_t object = lr->lr_foid;
1669 uint64_t offset = lr->lr_offset;
1670 uint64_t size = lr->lr_length;
1671 blkptr_t *bp = &lr->lr_blkptr;
1672 uint64_t txg = lr->lr_common.lrc_txg;
1674 dmu_object_info_t doi;
1679 ztest_object_lock(zd, object, RL_READER);
1680 error = dmu_bonus_hold(os, object, FTAG, &db);
1682 ztest_object_unlock(zd, object);
1686 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1688 if (crtxg == 0 || crtxg > txg) {
1689 dmu_buf_rele(db, FTAG);
1690 ztest_object_unlock(zd, object);
1694 dmu_object_info_from_db(db, &doi);
1695 dmu_buf_rele(db, FTAG);
1698 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1699 zgd->zgd_zilog = zd->zd_zilog;
1700 zgd->zgd_private = zd;
1702 if (buf != NULL) { /* immediate write */
1703 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1706 error = dmu_read(os, object, offset, size, buf,
1707 DMU_READ_NO_PREFETCH);
1710 size = doi.doi_data_block_size;
1712 offset = P2ALIGN(offset, size);
1714 ASSERT(offset < size);
1718 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1721 error = dmu_buf_hold(os, object, offset, zgd, &db,
1722 DMU_READ_NO_PREFETCH);
1728 ASSERT(db->db_offset == offset);
1729 ASSERT(db->db_size == size);
1731 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1732 ztest_get_done, zgd);
1739 ztest_get_done(zgd, error);
1745 ztest_lr_alloc(size_t lrsize, char *name)
1748 size_t namesize = name ? strlen(name) + 1 : 0;
1750 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1753 bcopy(name, lr + lrsize, namesize);
1759 ztest_lr_free(void *lr, size_t lrsize, char *name)
1761 size_t namesize = name ? strlen(name) + 1 : 0;
1763 umem_free(lr, lrsize + namesize);
1767 * Lookup a bunch of objects. Returns the number of objects not found.
1770 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1776 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1778 for (i = 0; i < count; i++, od++) {
1780 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1781 sizeof (uint64_t), 1, &od->od_object);
1783 ASSERT(error == ENOENT);
1784 ASSERT(od->od_object == 0);
1788 ztest_block_tag_t *bbt;
1789 dmu_object_info_t doi;
1791 ASSERT(od->od_object != 0);
1792 ASSERT(missing == 0); /* there should be no gaps */
1794 ztest_object_lock(zd, od->od_object, RL_READER);
1795 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1796 od->od_object, FTAG, &db));
1797 dmu_object_info_from_db(db, &doi);
1798 bbt = ztest_bt_bonus(db);
1799 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1800 od->od_type = doi.doi_type;
1801 od->od_blocksize = doi.doi_data_block_size;
1802 od->od_gen = bbt->bt_gen;
1803 dmu_buf_rele(db, FTAG);
1804 ztest_object_unlock(zd, od->od_object);
1812 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1817 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1819 for (i = 0; i < count; i++, od++) {
1826 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1828 lr->lr_doid = od->od_dir;
1829 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1830 lr->lrz_type = od->od_crtype;
1831 lr->lrz_blocksize = od->od_crblocksize;
1832 lr->lrz_ibshift = ztest_random_ibshift();
1833 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1834 lr->lrz_bonuslen = dmu_bonus_max();
1835 lr->lr_gen = od->od_crgen;
1836 lr->lr_crtime[0] = time(NULL);
1838 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1839 ASSERT(missing == 0);
1843 od->od_object = lr->lr_foid;
1844 od->od_type = od->od_crtype;
1845 od->od_blocksize = od->od_crblocksize;
1846 od->od_gen = od->od_crgen;
1847 ASSERT(od->od_object != 0);
1850 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1857 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1863 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1867 for (i = count - 1; i >= 0; i--, od--) {
1873 if (od->od_object == 0)
1876 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1878 lr->lr_doid = od->od_dir;
1880 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1881 ASSERT3U(error, ==, ENOSPC);
1886 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1893 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1899 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1901 lr->lr_foid = object;
1902 lr->lr_offset = offset;
1903 lr->lr_length = size;
1905 BP_ZERO(&lr->lr_blkptr);
1907 bcopy(data, lr + 1, size);
1909 error = ztest_replay_write(zd, lr, B_FALSE);
1911 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1917 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1922 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1924 lr->lr_foid = object;
1925 lr->lr_offset = offset;
1926 lr->lr_length = size;
1928 error = ztest_replay_truncate(zd, lr, B_FALSE);
1930 ztest_lr_free(lr, sizeof (*lr), NULL);
1936 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1941 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1943 lr->lr_foid = object;
1947 error = ztest_replay_setattr(zd, lr, B_FALSE);
1949 ztest_lr_free(lr, sizeof (*lr), NULL);
1955 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1957 objset_t *os = zd->zd_os;
1962 txg_wait_synced(dmu_objset_pool(os), 0);
1964 ztest_object_lock(zd, object, RL_READER);
1965 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1967 tx = dmu_tx_create(os);
1969 dmu_tx_hold_write(tx, object, offset, size);
1971 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1974 dmu_prealloc(os, object, offset, size, tx);
1976 txg_wait_synced(dmu_objset_pool(os), txg);
1978 (void) dmu_free_long_range(os, object, offset, size);
1981 ztest_range_unlock(rl);
1982 ztest_object_unlock(zd, object);
1986 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1988 ztest_block_tag_t wbt;
1989 dmu_object_info_t doi;
1990 enum ztest_io_type io_type;
1994 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1995 blocksize = doi.doi_data_block_size;
1996 data = umem_alloc(blocksize, UMEM_NOFAIL);
1999 * Pick an i/o type at random, biased toward writing block tags.
2001 io_type = ztest_random(ZTEST_IO_TYPES);
2002 if (ztest_random(2) == 0)
2003 io_type = ZTEST_IO_WRITE_TAG;
2005 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2009 case ZTEST_IO_WRITE_TAG:
2010 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2011 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2014 case ZTEST_IO_WRITE_PATTERN:
2015 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2016 if (ztest_random(2) == 0) {
2018 * Induce fletcher2 collisions to ensure that
2019 * zio_ddt_collision() detects and resolves them
2020 * when using fletcher2-verify for deduplication.
2022 ((uint64_t *)data)[0] ^= 1ULL << 63;
2023 ((uint64_t *)data)[4] ^= 1ULL << 63;
2025 (void) ztest_write(zd, object, offset, blocksize, data);
2028 case ZTEST_IO_WRITE_ZEROES:
2029 bzero(data, blocksize);
2030 (void) ztest_write(zd, object, offset, blocksize, data);
2033 case ZTEST_IO_TRUNCATE:
2034 (void) ztest_truncate(zd, object, offset, blocksize);
2037 case ZTEST_IO_SETATTR:
2038 (void) ztest_setattr(zd, object);
2044 (void) rw_exit(&zd->zd_zilog_lock);
2046 umem_free(data, blocksize);
2050 * Initialize an object description template.
2053 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2054 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2056 od->od_dir = ZTEST_DIROBJ;
2059 od->od_crtype = type;
2060 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2063 od->od_type = DMU_OT_NONE;
2064 od->od_blocksize = 0;
2067 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2068 tag, (longlong_t)id, (u_longlong_t)index);
2072 * Lookup or create the objects for a test using the od template.
2073 * If the objects do not all exist, or if 'remove' is specified,
2074 * remove any existing objects and create new ones. Otherwise,
2075 * use the existing objects.
2078 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2080 int count = size / sizeof (*od);
2083 mutex_enter(&zd->zd_dirobj_lock);
2084 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2085 (ztest_remove(zd, od, count) != 0 ||
2086 ztest_create(zd, od, count) != 0))
2089 mutex_exit(&zd->zd_dirobj_lock);
2096 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2098 zilog_t *zilog = zd->zd_zilog;
2100 (void) rw_enter(&zd->zd_zilog_lock, RW_READER);
2102 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2105 * Remember the committed values in zd, which is in parent/child
2106 * shared memory. If we die, the next iteration of ztest_run()
2107 * will verify that the log really does contain this record.
2109 mutex_enter(&zilog->zl_lock);
2110 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2111 zd->zd_seq = zilog->zl_commit_lr_seq;
2112 mutex_exit(&zilog->zl_lock);
2114 (void) rw_exit(&zd->zd_zilog_lock);
2118 * This function is designed to simulate the operations that occur during a
2119 * mount/unmount operation. We hold the dataset across these operations in an
2120 * attempt to expose any implicit assumptions about ZIL management.
2124 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2126 objset_t *os = zd->zd_os;
2128 (void) rw_enter(&zd->zd_zilog_lock, RW_WRITER);
2130 /* zfs_sb_teardown() */
2131 zil_close(zd->zd_zilog);
2133 /* zfsvfs_setup() */
2134 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2135 zil_replay(os, zd, ztest_replay_vector);
2137 (void) rw_exit(&zd->zd_zilog_lock);
2141 * Verify that we can't destroy an active pool, create an existing pool,
2142 * or create a pool with a bad vdev spec.
2146 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2148 ztest_shared_t *zs = ztest_shared;
2153 * Attempt to create using a bad file.
2155 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2156 VERIFY3U(ENOENT, ==,
2157 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2158 nvlist_free(nvroot);
2161 * Attempt to create using a bad mirror.
2163 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2164 VERIFY3U(ENOENT, ==,
2165 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2166 nvlist_free(nvroot);
2169 * Attempt to create an existing pool. It shouldn't matter
2170 * what's in the nvroot; we should fail with EEXIST.
2172 (void) rw_enter(&zs->zs_name_lock, RW_READER);
2173 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2174 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2175 nvlist_free(nvroot);
2176 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2177 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2178 spa_close(spa, FTAG);
2180 (void) rw_exit(&zs->zs_name_lock);
2184 vdev_lookup_by_path(vdev_t *vd, const char *path)
2189 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2192 for (c = 0; c < vd->vdev_children; c++)
2193 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2201 * Find the first available hole which can be used as a top-level.
2204 find_vdev_hole(spa_t *spa)
2206 vdev_t *rvd = spa->spa_root_vdev;
2209 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2211 for (c = 0; c < rvd->vdev_children; c++) {
2212 vdev_t *cvd = rvd->vdev_child[c];
2214 if (cvd->vdev_ishole)
2221 * Verify that vdev_add() works as expected.
2225 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2227 ztest_shared_t *zs = ztest_shared;
2228 spa_t *spa = zs->zs_spa;
2234 mutex_enter(&zs->zs_vdev_lock);
2235 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2237 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2239 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2242 * If we have slogs then remove them 1/4 of the time.
2244 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2246 * Grab the guid from the head of the log class rotor.
2248 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2250 spa_config_exit(spa, SCL_VDEV, FTAG);
2253 * We have to grab the zs_name_lock as writer to
2254 * prevent a race between removing a slog (dmu_objset_find)
2255 * and destroying a dataset. Removing the slog will
2256 * grab a reference on the dataset which may cause
2257 * dmu_objset_destroy() to fail with EBUSY thus
2258 * leaving the dataset in an inconsistent state.
2260 rw_enter(&ztest_shared->zs_name_lock, RW_WRITER);
2261 error = spa_vdev_remove(spa, guid, B_FALSE);
2262 rw_exit(&ztest_shared->zs_name_lock);
2264 if (error && error != EEXIST)
2265 fatal(0, "spa_vdev_remove() = %d", error);
2267 spa_config_exit(spa, SCL_VDEV, FTAG);
2270 * Make 1/4 of the devices be log devices.
2272 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2273 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2275 error = spa_vdev_add(spa, nvroot);
2276 nvlist_free(nvroot);
2278 if (error == ENOSPC)
2279 ztest_record_enospc("spa_vdev_add");
2280 else if (error != 0)
2281 fatal(0, "spa_vdev_add() = %d", error);
2284 mutex_exit(&ztest_shared->zs_vdev_lock);
2288 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2292 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2294 ztest_shared_t *zs = ztest_shared;
2295 spa_t *spa = zs->zs_spa;
2296 vdev_t *rvd = spa->spa_root_vdev;
2297 spa_aux_vdev_t *sav;
2303 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2305 if (ztest_random(2) == 0) {
2306 sav = &spa->spa_spares;
2307 aux = ZPOOL_CONFIG_SPARES;
2309 sav = &spa->spa_l2cache;
2310 aux = ZPOOL_CONFIG_L2CACHE;
2313 mutex_enter(&zs->zs_vdev_lock);
2315 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2317 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2319 * Pick a random device to remove.
2321 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2324 * Find an unused device we can add.
2326 zs->zs_vdev_aux = 0;
2329 (void) sprintf(path, ztest_aux_template, zopt_dir,
2330 zopt_pool, aux, zs->zs_vdev_aux);
2331 for (c = 0; c < sav->sav_count; c++)
2332 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2335 if (c == sav->sav_count &&
2336 vdev_lookup_by_path(rvd, path) == NULL)
2342 spa_config_exit(spa, SCL_VDEV, FTAG);
2348 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2349 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2350 error = spa_vdev_add(spa, nvroot);
2352 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2353 nvlist_free(nvroot);
2356 * Remove an existing device. Sometimes, dirty its
2357 * vdev state first to make sure we handle removal
2358 * of devices that have pending state changes.
2360 if (ztest_random(2) == 0)
2361 (void) vdev_online(spa, guid, 0, NULL);
2363 error = spa_vdev_remove(spa, guid, B_FALSE);
2364 if (error != 0 && error != EBUSY)
2365 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2368 mutex_exit(&zs->zs_vdev_lock);
2370 umem_free(path, MAXPATHLEN);
2374 * split a pool if it has mirror tlvdevs
2378 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2380 ztest_shared_t *zs = ztest_shared;
2381 spa_t *spa = zs->zs_spa;
2382 vdev_t *rvd = spa->spa_root_vdev;
2383 nvlist_t *tree, **child, *config, *split, **schild;
2384 uint_t c, children, schildren = 0, lastlogid = 0;
2387 mutex_enter(&zs->zs_vdev_lock);
2389 /* ensure we have a useable config; mirrors of raidz aren't supported */
2390 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2391 mutex_exit(&zs->zs_vdev_lock);
2395 /* clean up the old pool, if any */
2396 (void) spa_destroy("splitp");
2398 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2400 /* generate a config from the existing config */
2401 mutex_enter(&spa->spa_props_lock);
2402 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2404 mutex_exit(&spa->spa_props_lock);
2406 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2409 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2410 for (c = 0; c < children; c++) {
2411 vdev_t *tvd = rvd->vdev_child[c];
2415 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2416 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2418 VERIFY(nvlist_add_string(schild[schildren],
2419 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2420 VERIFY(nvlist_add_uint64(schild[schildren],
2421 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2423 lastlogid = schildren;
2428 VERIFY(nvlist_lookup_nvlist_array(child[c],
2429 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2430 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2433 /* OK, create a config that can be used to split */
2434 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2435 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2436 VDEV_TYPE_ROOT) == 0);
2437 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2438 lastlogid != 0 ? lastlogid : schildren) == 0);
2440 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2441 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2443 for (c = 0; c < schildren; c++)
2444 nvlist_free(schild[c]);
2448 spa_config_exit(spa, SCL_VDEV, FTAG);
2450 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
2451 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2452 (void) rw_exit(&zs->zs_name_lock);
2454 nvlist_free(config);
2457 (void) printf("successful split - results:\n");
2458 mutex_enter(&spa_namespace_lock);
2459 show_pool_stats(spa);
2460 show_pool_stats(spa_lookup("splitp"));
2461 mutex_exit(&spa_namespace_lock);
2465 mutex_exit(&zs->zs_vdev_lock);
2470 * Verify that we can attach and detach devices.
2474 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2476 ztest_shared_t *zs = ztest_shared;
2477 spa_t *spa = zs->zs_spa;
2478 spa_aux_vdev_t *sav = &spa->spa_spares;
2479 vdev_t *rvd = spa->spa_root_vdev;
2480 vdev_t *oldvd, *newvd, *pvd;
2484 uint64_t ashift = ztest_get_ashift();
2485 uint64_t oldguid, pguid;
2486 size_t oldsize, newsize;
2487 char *oldpath, *newpath;
2489 int oldvd_has_siblings = B_FALSE;
2490 int newvd_is_spare = B_FALSE;
2492 int error, expected_error;
2494 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2495 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2497 mutex_enter(&zs->zs_vdev_lock);
2498 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2500 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2503 * Decide whether to do an attach or a replace.
2505 replacing = ztest_random(2);
2508 * Pick a random top-level vdev.
2510 top = ztest_random_vdev_top(spa, B_TRUE);
2513 * Pick a random leaf within it.
2515 leaf = ztest_random(leaves);
2520 oldvd = rvd->vdev_child[top];
2521 if (zs->zs_mirrors >= 1) {
2522 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2523 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2524 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2526 if (zopt_raidz > 1) {
2527 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2528 ASSERT(oldvd->vdev_children == zopt_raidz);
2529 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2533 * If we're already doing an attach or replace, oldvd may be a
2534 * mirror vdev -- in which case, pick a random child.
2536 while (oldvd->vdev_children != 0) {
2537 oldvd_has_siblings = B_TRUE;
2538 ASSERT(oldvd->vdev_children >= 2);
2539 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2542 oldguid = oldvd->vdev_guid;
2543 oldsize = vdev_get_min_asize(oldvd);
2544 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2545 (void) strcpy(oldpath, oldvd->vdev_path);
2546 pvd = oldvd->vdev_parent;
2547 pguid = pvd->vdev_guid;
2550 * If oldvd has siblings, then half of the time, detach it.
2552 if (oldvd_has_siblings && ztest_random(2) == 0) {
2553 spa_config_exit(spa, SCL_VDEV, FTAG);
2554 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2555 if (error != 0 && error != ENODEV && error != EBUSY &&
2557 fatal(0, "detach (%s) returned %d", oldpath, error);
2562 * For the new vdev, choose with equal probability between the two
2563 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2565 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2566 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2567 newvd_is_spare = B_TRUE;
2568 (void) strcpy(newpath, newvd->vdev_path);
2570 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2571 zopt_dir, zopt_pool, top * leaves + leaf);
2572 if (ztest_random(2) == 0)
2573 newpath[strlen(newpath) - 1] = 'b';
2574 newvd = vdev_lookup_by_path(rvd, newpath);
2578 newsize = vdev_get_min_asize(newvd);
2581 * Make newsize a little bigger or smaller than oldsize.
2582 * If it's smaller, the attach should fail.
2583 * If it's larger, and we're doing a replace,
2584 * we should get dynamic LUN growth when we're done.
2586 newsize = 10 * oldsize / (9 + ztest_random(3));
2590 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2591 * unless it's a replace; in that case any non-replacing parent is OK.
2593 * If newvd is already part of the pool, it should fail with EBUSY.
2595 * If newvd is too small, it should fail with EOVERFLOW.
2597 if (pvd->vdev_ops != &vdev_mirror_ops &&
2598 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2599 pvd->vdev_ops == &vdev_replacing_ops ||
2600 pvd->vdev_ops == &vdev_spare_ops))
2601 expected_error = ENOTSUP;
2602 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2603 expected_error = ENOTSUP;
2604 else if (newvd == oldvd)
2605 expected_error = replacing ? 0 : EBUSY;
2606 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2607 expected_error = EBUSY;
2608 else if (newsize < oldsize)
2609 expected_error = EOVERFLOW;
2610 else if (ashift > oldvd->vdev_top->vdev_ashift)
2611 expected_error = EDOM;
2615 spa_config_exit(spa, SCL_VDEV, FTAG);
2618 * Build the nvlist describing newpath.
2620 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2621 ashift, 0, 0, 0, 1);
2623 error = spa_vdev_attach(spa, oldguid, root, replacing);
2628 * If our parent was the replacing vdev, but the replace completed,
2629 * then instead of failing with ENOTSUP we may either succeed,
2630 * fail with ENODEV, or fail with EOVERFLOW.
2632 if (expected_error == ENOTSUP &&
2633 (error == 0 || error == ENODEV || error == EOVERFLOW))
2634 expected_error = error;
2637 * If someone grew the LUN, the replacement may be too small.
2639 if (error == EOVERFLOW || error == EBUSY)
2640 expected_error = error;
2642 /* XXX workaround 6690467 */
2643 if (error != expected_error && expected_error != EBUSY) {
2644 fatal(0, "attach (%s %llu, %s %llu, %d) "
2645 "returned %d, expected %d",
2646 oldpath, (longlong_t)oldsize, newpath,
2647 (longlong_t)newsize, replacing, error, expected_error);
2650 mutex_exit(&zs->zs_vdev_lock);
2652 umem_free(oldpath, MAXPATHLEN);
2653 umem_free(newpath, MAXPATHLEN);
2657 * Callback function which expands the physical size of the vdev.
2660 grow_vdev(vdev_t *vd, void *arg)
2662 ASSERTV(spa_t *spa = vd->vdev_spa);
2663 size_t *newsize = arg;
2667 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2668 ASSERT(vd->vdev_ops->vdev_op_leaf);
2670 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2673 fsize = lseek(fd, 0, SEEK_END);
2674 VERIFY(ftruncate(fd, *newsize) == 0);
2676 if (zopt_verbose >= 6) {
2677 (void) printf("%s grew from %lu to %lu bytes\n",
2678 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2685 * Callback function which expands a given vdev by calling vdev_online().
2689 online_vdev(vdev_t *vd, void *arg)
2691 spa_t *spa = vd->vdev_spa;
2692 vdev_t *tvd = vd->vdev_top;
2693 uint64_t guid = vd->vdev_guid;
2694 uint64_t generation = spa->spa_config_generation + 1;
2695 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2698 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2699 ASSERT(vd->vdev_ops->vdev_op_leaf);
2701 /* Calling vdev_online will initialize the new metaslabs */
2702 spa_config_exit(spa, SCL_STATE, spa);
2703 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2704 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2707 * If vdev_online returned an error or the underlying vdev_open
2708 * failed then we abort the expand. The only way to know that
2709 * vdev_open fails is by checking the returned newstate.
2711 if (error || newstate != VDEV_STATE_HEALTHY) {
2712 if (zopt_verbose >= 5) {
2713 (void) printf("Unable to expand vdev, state %llu, "
2714 "error %d\n", (u_longlong_t)newstate, error);
2718 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2721 * Since we dropped the lock we need to ensure that we're
2722 * still talking to the original vdev. It's possible this
2723 * vdev may have been detached/replaced while we were
2724 * trying to online it.
2726 if (generation != spa->spa_config_generation) {
2727 if (zopt_verbose >= 5) {
2728 (void) printf("vdev configuration has changed, "
2729 "guid %llu, state %llu, expected gen %llu, "
2732 (u_longlong_t)tvd->vdev_state,
2733 (u_longlong_t)generation,
2734 (u_longlong_t)spa->spa_config_generation);
2742 * Traverse the vdev tree calling the supplied function.
2743 * We continue to walk the tree until we either have walked all
2744 * children or we receive a non-NULL return from the callback.
2745 * If a NULL callback is passed, then we just return back the first
2746 * leaf vdev we encounter.
2749 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2753 if (vd->vdev_ops->vdev_op_leaf) {
2757 return (func(vd, arg));
2760 for (c = 0; c < vd->vdev_children; c++) {
2761 vdev_t *cvd = vd->vdev_child[c];
2762 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2769 * Verify that dynamic LUN growth works as expected.
2773 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2775 ztest_shared_t *zs = ztest_shared;
2776 spa_t *spa = zs->zs_spa;
2778 metaslab_class_t *mc;
2779 metaslab_group_t *mg;
2780 size_t psize, newsize;
2782 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2784 mutex_enter(&zs->zs_vdev_lock);
2785 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2787 top = ztest_random_vdev_top(spa, B_TRUE);
2789 tvd = spa->spa_root_vdev->vdev_child[top];
2792 old_ms_count = tvd->vdev_ms_count;
2793 old_class_space = metaslab_class_get_space(mc);
2796 * Determine the size of the first leaf vdev associated with
2797 * our top-level device.
2799 vd = vdev_walk_tree(tvd, NULL, NULL);
2800 ASSERT3P(vd, !=, NULL);
2801 ASSERT(vd->vdev_ops->vdev_op_leaf);
2803 psize = vd->vdev_psize;
2806 * We only try to expand the vdev if it's healthy, less than 4x its
2807 * original size, and it has a valid psize.
2809 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2810 psize == 0 || psize >= 4 * zopt_vdev_size) {
2811 spa_config_exit(spa, SCL_STATE, spa);
2812 mutex_exit(&zs->zs_vdev_lock);
2816 newsize = psize + psize / 8;
2817 ASSERT3U(newsize, >, psize);
2819 if (zopt_verbose >= 6) {
2820 (void) printf("Expanding LUN %s from %lu to %lu\n",
2821 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2825 * Growing the vdev is a two step process:
2826 * 1). expand the physical size (i.e. relabel)
2827 * 2). online the vdev to create the new metaslabs
2829 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2830 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2831 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2832 if (zopt_verbose >= 5) {
2833 (void) printf("Could not expand LUN because "
2834 "the vdev configuration changed.\n");
2836 spa_config_exit(spa, SCL_STATE, spa);
2837 mutex_exit(&zs->zs_vdev_lock);
2841 spa_config_exit(spa, SCL_STATE, spa);
2844 * Expanding the LUN will update the config asynchronously,
2845 * thus we must wait for the async thread to complete any
2846 * pending tasks before proceeding.
2850 mutex_enter(&spa->spa_async_lock);
2851 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2852 mutex_exit(&spa->spa_async_lock);
2855 txg_wait_synced(spa_get_dsl(spa), 0);
2856 (void) poll(NULL, 0, 100);
2859 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2861 tvd = spa->spa_root_vdev->vdev_child[top];
2862 new_ms_count = tvd->vdev_ms_count;
2863 new_class_space = metaslab_class_get_space(mc);
2865 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2866 if (zopt_verbose >= 5) {
2867 (void) printf("Could not verify LUN expansion due to "
2868 "intervening vdev offline or remove.\n");
2870 spa_config_exit(spa, SCL_STATE, spa);
2871 mutex_exit(&zs->zs_vdev_lock);
2876 * Make sure we were able to grow the vdev.
2878 if (new_ms_count <= old_ms_count)
2879 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2880 old_ms_count, new_ms_count);
2883 * Make sure we were able to grow the pool.
2885 if (new_class_space <= old_class_space)
2886 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2887 old_class_space, new_class_space);
2889 if (zopt_verbose >= 5) {
2890 char oldnumbuf[6], newnumbuf[6];
2892 nicenum(old_class_space, oldnumbuf);
2893 nicenum(new_class_space, newnumbuf);
2894 (void) printf("%s grew from %s to %s\n",
2895 spa->spa_name, oldnumbuf, newnumbuf);
2898 spa_config_exit(spa, SCL_STATE, spa);
2899 mutex_exit(&zs->zs_vdev_lock);
2903 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2907 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2910 * Create the objects common to all ztest datasets.
2912 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2913 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2917 ztest_dataset_create(char *dsname)
2919 uint64_t zilset = ztest_random(100);
2920 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2921 ztest_objset_create_cb, NULL);
2923 if (err || zilset < 80)
2926 (void) printf("Setting dataset %s to sync always\n", dsname);
2927 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2928 ZFS_SYNC_ALWAYS, B_FALSE));
2933 ztest_objset_destroy_cb(const char *name, void *arg)
2936 dmu_object_info_t doi;
2940 * Verify that the dataset contains a directory object.
2942 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2943 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2944 if (error != ENOENT) {
2945 /* We could have crashed in the middle of destroying it */
2946 ASSERT3U(error, ==, 0);
2947 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2948 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2950 dmu_objset_rele(os, FTAG);
2953 * Destroy the dataset.
2955 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2960 ztest_snapshot_create(char *osname, uint64_t id)
2962 char snapname[MAXNAMELEN];
2965 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2968 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2969 NULL, NULL, B_FALSE, B_FALSE, -1);
2970 if (error == ENOSPC) {
2971 ztest_record_enospc(FTAG);
2974 if (error != 0 && error != EEXIST)
2975 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2980 ztest_snapshot_destroy(char *osname, uint64_t id)
2982 char snapname[MAXNAMELEN];
2985 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2988 error = dmu_objset_destroy(snapname, B_FALSE);
2989 if (error != 0 && error != ENOENT)
2990 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2996 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2998 ztest_shared_t *zs = ztest_shared;
3007 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3008 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3010 (void) rw_enter(&zs->zs_name_lock, RW_READER);
3012 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3013 zs->zs_pool, (u_longlong_t)id);
3016 * If this dataset exists from a previous run, process its replay log
3017 * half of the time. If we don't replay it, then dmu_objset_destroy()
3018 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3020 if (ztest_random(2) == 0 &&
3021 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3022 ztest_zd_init(zdtmp, os);
3023 zil_replay(os, zdtmp, ztest_replay_vector);
3024 ztest_zd_fini(zdtmp);
3025 dmu_objset_disown(os, FTAG);
3029 * There may be an old instance of the dataset we're about to
3030 * create lying around from a previous run. If so, destroy it
3031 * and all of its snapshots.
3033 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3034 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3037 * Verify that the destroyed dataset is no longer in the namespace.
3039 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
3042 * Verify that we can create a new dataset.
3044 error = ztest_dataset_create(name);
3046 if (error == ENOSPC) {
3047 ztest_record_enospc(FTAG);
3050 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3054 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3056 ztest_zd_init(zdtmp, os);
3059 * Open the intent log for it.
3061 zilog = zil_open(os, ztest_get_data);
3064 * Put some objects in there, do a little I/O to them,
3065 * and randomly take a couple of snapshots along the way.
3067 iters = ztest_random(5);
3068 for (i = 0; i < iters; i++) {
3069 ztest_dmu_object_alloc_free(zdtmp, id);
3070 if (ztest_random(iters) == 0)
3071 (void) ztest_snapshot_create(name, i);
3075 * Verify that we cannot create an existing dataset.
3077 VERIFY3U(EEXIST, ==,
3078 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3081 * Verify that we can hold an objset that is also owned.
3083 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3084 dmu_objset_rele(os2, FTAG);
3087 * Verify that we cannot own an objset that is already owned.
3090 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3093 dmu_objset_disown(os, FTAG);
3094 ztest_zd_fini(zdtmp);
3096 (void) rw_exit(&zs->zs_name_lock);
3098 umem_free(name, MAXNAMELEN);
3099 umem_free(zdtmp, sizeof (ztest_ds_t));
3103 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3106 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3108 ztest_shared_t *zs = ztest_shared;
3110 (void) rw_enter(&zs->zs_name_lock, RW_READER);
3111 (void) ztest_snapshot_destroy(zd->zd_name, id);
3112 (void) ztest_snapshot_create(zd->zd_name, id);
3113 (void) rw_exit(&zs->zs_name_lock);
3117 * Cleanup non-standard snapshots and clones.
3120 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3129 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3130 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3131 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3132 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3133 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3135 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3136 osname, (u_longlong_t)id);
3137 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3138 osname, (u_longlong_t)id);
3139 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3140 clone1name, (u_longlong_t)id);
3141 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3142 osname, (u_longlong_t)id);
3143 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3144 clone1name, (u_longlong_t)id);
3146 error = dmu_objset_destroy(clone2name, B_FALSE);
3147 if (error && error != ENOENT)
3148 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3149 error = dmu_objset_destroy(snap3name, B_FALSE);
3150 if (error && error != ENOENT)
3151 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3152 error = dmu_objset_destroy(snap2name, B_FALSE);
3153 if (error && error != ENOENT)
3154 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3155 error = dmu_objset_destroy(clone1name, B_FALSE);
3156 if (error && error != ENOENT)
3157 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3158 error = dmu_objset_destroy(snap1name, B_FALSE);
3159 if (error && error != ENOENT)
3160 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3162 umem_free(snap1name, MAXNAMELEN);
3163 umem_free(clone1name, MAXNAMELEN);
3164 umem_free(snap2name, MAXNAMELEN);
3165 umem_free(clone2name, MAXNAMELEN);
3166 umem_free(snap3name, MAXNAMELEN);
3170 * Verify dsl_dataset_promote handles EBUSY
3173 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3175 ztest_shared_t *zs = ztest_shared;
3183 char *osname = zd->zd_name;
3186 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3187 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3188 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3189 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3190 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3192 (void) rw_enter(&zs->zs_name_lock, RW_READER);
3194 ztest_dsl_dataset_cleanup(osname, id);
3196 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3197 osname, (u_longlong_t)id);
3198 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3199 osname, (u_longlong_t)id);
3200 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3201 clone1name, (u_longlong_t)id);
3202 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3203 osname, (u_longlong_t)id);
3204 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3205 clone1name, (u_longlong_t)id);
3207 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3208 NULL, NULL, B_FALSE, B_FALSE, -1);
3209 if (error && error != EEXIST) {
3210 if (error == ENOSPC) {
3211 ztest_record_enospc(FTAG);
3214 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3217 error = dmu_objset_hold(snap1name, FTAG, &clone);
3219 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3221 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3222 dmu_objset_rele(clone, FTAG);
3224 if (error == ENOSPC) {
3225 ztest_record_enospc(FTAG);
3228 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3231 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3232 NULL, NULL, B_FALSE, B_FALSE, -1);
3233 if (error && error != EEXIST) {
3234 if (error == ENOSPC) {
3235 ztest_record_enospc(FTAG);
3238 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3241 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3242 NULL, NULL, B_FALSE, B_FALSE, -1);
3243 if (error && error != EEXIST) {
3244 if (error == ENOSPC) {
3245 ztest_record_enospc(FTAG);
3248 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3251 error = dmu_objset_hold(snap3name, FTAG, &clone);
3253 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3255 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3256 dmu_objset_rele(clone, FTAG);
3258 if (error == ENOSPC) {
3259 ztest_record_enospc(FTAG);
3262 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3265 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3267 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3268 error = dsl_dataset_promote(clone2name, NULL);
3270 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3272 dsl_dataset_disown(ds, FTAG);
3275 ztest_dsl_dataset_cleanup(osname, id);
3277 (void) rw_exit(&zs->zs_name_lock);
3279 umem_free(snap1name, MAXNAMELEN);
3280 umem_free(clone1name, MAXNAMELEN);
3281 umem_free(snap2name, MAXNAMELEN);
3282 umem_free(clone2name, MAXNAMELEN);
3283 umem_free(snap3name, MAXNAMELEN);
3286 #undef OD_ARRAY_SIZE
3287 #define OD_ARRAY_SIZE 4
3290 * Verify that dmu_object_{alloc,free} work as expected.
3293 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3300 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3301 od = umem_alloc(size, UMEM_NOFAIL);
3302 batchsize = OD_ARRAY_SIZE;
3304 for (b = 0; b < batchsize; b++)
3305 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3308 * Destroy the previous batch of objects, create a new batch,
3309 * and do some I/O on the new objects.
3311 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3314 while (ztest_random(4 * batchsize) != 0)
3315 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3316 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3318 umem_free(od, size);
3321 #undef OD_ARRAY_SIZE
3322 #define OD_ARRAY_SIZE 2
3325 * Verify that dmu_{read,write} work as expected.
3328 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3333 objset_t *os = zd->zd_os;
3334 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3335 od = umem_alloc(size, UMEM_NOFAIL);
3337 int i, freeit, error;
3339 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3340 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3341 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3342 uint64_t regions = 997;
3343 uint64_t stride = 123456789ULL;
3344 uint64_t width = 40;
3345 int free_percent = 5;
3348 * This test uses two objects, packobj and bigobj, that are always
3349 * updated together (i.e. in the same tx) so that their contents are
3350 * in sync and can be compared. Their contents relate to each other
3351 * in a simple way: packobj is a dense array of 'bufwad' structures,
3352 * while bigobj is a sparse array of the same bufwads. Specifically,
3353 * for any index n, there are three bufwads that should be identical:
3355 * packobj, at offset n * sizeof (bufwad_t)
3356 * bigobj, at the head of the nth chunk
3357 * bigobj, at the tail of the nth chunk
3359 * The chunk size is arbitrary. It doesn't have to be a power of two,
3360 * and it doesn't have any relation to the object blocksize.
3361 * The only requirement is that it can hold at least two bufwads.
3363 * Normally, we write the bufwad to each of these locations.
3364 * However, free_percent of the time we instead write zeroes to
3365 * packobj and perform a dmu_free_range() on bigobj. By comparing
3366 * bigobj to packobj, we can verify that the DMU is correctly
3367 * tracking which parts of an object are allocated and free,
3368 * and that the contents of the allocated blocks are correct.
3372 * Read the directory info. If it's the first time, set things up.
3374 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3375 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3377 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3378 umem_free(od, size);
3382 bigobj = od[0].od_object;
3383 packobj = od[1].od_object;
3384 chunksize = od[0].od_gen;
3385 ASSERT(chunksize == od[1].od_gen);
3388 * Prefetch a random chunk of the big object.
3389 * Our aim here is to get some async reads in flight
3390 * for blocks that we may free below; the DMU should
3391 * handle this race correctly.
3393 n = ztest_random(regions) * stride + ztest_random(width);
3394 s = 1 + ztest_random(2 * width - 1);
3395 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3398 * Pick a random index and compute the offsets into packobj and bigobj.
3400 n = ztest_random(regions) * stride + ztest_random(width);
3401 s = 1 + ztest_random(width - 1);
3403 packoff = n * sizeof (bufwad_t);
3404 packsize = s * sizeof (bufwad_t);
3406 bigoff = n * chunksize;
3407 bigsize = s * chunksize;
3409 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3410 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3413 * free_percent of the time, free a range of bigobj rather than
3416 freeit = (ztest_random(100) < free_percent);
3419 * Read the current contents of our objects.
3421 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3423 ASSERT3U(error, ==, 0);
3424 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3426 ASSERT3U(error, ==, 0);
3429 * Get a tx for the mods to both packobj and bigobj.
3431 tx = dmu_tx_create(os);
3433 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3436 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3438 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3440 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3442 umem_free(packbuf, packsize);
3443 umem_free(bigbuf, bigsize);
3444 umem_free(od, size);
3448 dmu_object_set_checksum(os, bigobj,
3449 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3451 dmu_object_set_compress(os, bigobj,
3452 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3455 * For each index from n to n + s, verify that the existing bufwad
3456 * in packobj matches the bufwads at the head and tail of the
3457 * corresponding chunk in bigobj. Then update all three bufwads
3458 * with the new values we want to write out.
3460 for (i = 0; i < s; i++) {
3462 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3464 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3466 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3468 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3469 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3471 if (pack->bw_txg > txg)
3472 fatal(0, "future leak: got %llx, open txg is %llx",
3475 if (pack->bw_data != 0 && pack->bw_index != n + i)
3476 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3477 pack->bw_index, n, i);
3479 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3480 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3482 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3483 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3486 bzero(pack, sizeof (bufwad_t));
3488 pack->bw_index = n + i;
3490 pack->bw_data = 1 + ztest_random(-2ULL);
3497 * We've verified all the old bufwads, and made new ones.
3498 * Now write them out.
3500 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3503 if (zopt_verbose >= 7) {
3504 (void) printf("freeing offset %llx size %llx"
3506 (u_longlong_t)bigoff,
3507 (u_longlong_t)bigsize,
3510 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3512 if (zopt_verbose >= 7) {
3513 (void) printf("writing offset %llx size %llx"
3515 (u_longlong_t)bigoff,
3516 (u_longlong_t)bigsize,
3519 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3525 * Sanity check the stuff we just wrote.
3528 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3529 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3531 VERIFY(0 == dmu_read(os, packobj, packoff,
3532 packsize, packcheck, DMU_READ_PREFETCH));
3533 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3534 bigsize, bigcheck, DMU_READ_PREFETCH));
3536 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3537 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3539 umem_free(packcheck, packsize);
3540 umem_free(bigcheck, bigsize);
3543 umem_free(packbuf, packsize);
3544 umem_free(bigbuf, bigsize);
3545 umem_free(od, size);
3549 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3550 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3558 * For each index from n to n + s, verify that the existing bufwad
3559 * in packobj matches the bufwads at the head and tail of the
3560 * corresponding chunk in bigobj. Then update all three bufwads
3561 * with the new values we want to write out.
3563 for (i = 0; i < s; i++) {
3565 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3567 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3569 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3571 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3572 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3574 if (pack->bw_txg > txg)
3575 fatal(0, "future leak: got %llx, open txg is %llx",
3578 if (pack->bw_data != 0 && pack->bw_index != n + i)
3579 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3580 pack->bw_index, n, i);
3582 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3583 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3585 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3586 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3588 pack->bw_index = n + i;
3590 pack->bw_data = 1 + ztest_random(-2ULL);
3597 #undef OD_ARRAY_SIZE
3598 #define OD_ARRAY_SIZE 2
3601 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3603 objset_t *os = zd->zd_os;
3610 bufwad_t *packbuf, *bigbuf;
3611 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3612 uint64_t blocksize = ztest_random_blocksize();
3613 uint64_t chunksize = blocksize;
3614 uint64_t regions = 997;
3615 uint64_t stride = 123456789ULL;
3617 dmu_buf_t *bonus_db;
3618 arc_buf_t **bigbuf_arcbufs;
3619 dmu_object_info_t doi;
3621 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3622 od = umem_alloc(size, UMEM_NOFAIL);
3625 * This test uses two objects, packobj and bigobj, that are always
3626 * updated together (i.e. in the same tx) so that their contents are
3627 * in sync and can be compared. Their contents relate to each other
3628 * in a simple way: packobj is a dense array of 'bufwad' structures,
3629 * while bigobj is a sparse array of the same bufwads. Specifically,
3630 * for any index n, there are three bufwads that should be identical:
3632 * packobj, at offset n * sizeof (bufwad_t)
3633 * bigobj, at the head of the nth chunk
3634 * bigobj, at the tail of the nth chunk
3636 * The chunk size is set equal to bigobj block size so that
3637 * dmu_assign_arcbuf() can be tested for object updates.
3641 * Read the directory info. If it's the first time, set things up.
3643 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3644 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3647 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3648 umem_free(od, size);
3652 bigobj = od[0].od_object;
3653 packobj = od[1].od_object;
3654 blocksize = od[0].od_blocksize;
3655 chunksize = blocksize;
3656 ASSERT(chunksize == od[1].od_gen);
3658 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3659 VERIFY(ISP2(doi.doi_data_block_size));
3660 VERIFY(chunksize == doi.doi_data_block_size);
3661 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3664 * Pick a random index and compute the offsets into packobj and bigobj.
3666 n = ztest_random(regions) * stride + ztest_random(width);
3667 s = 1 + ztest_random(width - 1);
3669 packoff = n * sizeof (bufwad_t);
3670 packsize = s * sizeof (bufwad_t);
3672 bigoff = n * chunksize;
3673 bigsize = s * chunksize;
3675 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3676 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3678 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3680 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3683 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3684 * Iteration 1 test zcopy to already referenced dbufs.
3685 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3686 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3687 * Iteration 4 test zcopy when dbuf is no longer dirty.
3688 * Iteration 5 test zcopy when it can't be done.
3689 * Iteration 6 one more zcopy write.
3691 for (i = 0; i < 7; i++) {
3696 * In iteration 5 (i == 5) use arcbufs
3697 * that don't match bigobj blksz to test
3698 * dmu_assign_arcbuf() when it can't directly
3699 * assign an arcbuf to a dbuf.
3701 for (j = 0; j < s; j++) {
3704 dmu_request_arcbuf(bonus_db, chunksize);
3706 bigbuf_arcbufs[2 * j] =
3707 dmu_request_arcbuf(bonus_db, chunksize / 2);
3708 bigbuf_arcbufs[2 * j + 1] =
3709 dmu_request_arcbuf(bonus_db, chunksize / 2);
3714 * Get a tx for the mods to both packobj and bigobj.
3716 tx = dmu_tx_create(os);
3718 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3719 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3721 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3723 umem_free(packbuf, packsize);
3724 umem_free(bigbuf, bigsize);
3725 for (j = 0; j < s; j++) {
3727 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3730 bigbuf_arcbufs[2 * j]);
3732 bigbuf_arcbufs[2 * j + 1]);
3735 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3736 umem_free(od, size);
3737 dmu_buf_rele(bonus_db, FTAG);
3742 * 50% of the time don't read objects in the 1st iteration to
3743 * test dmu_assign_arcbuf() for the case when there're no
3744 * existing dbufs for the specified offsets.
3746 if (i != 0 || ztest_random(2) != 0) {
3747 error = dmu_read(os, packobj, packoff,
3748 packsize, packbuf, DMU_READ_PREFETCH);
3749 ASSERT3U(error, ==, 0);
3750 error = dmu_read(os, bigobj, bigoff, bigsize,
3751 bigbuf, DMU_READ_PREFETCH);
3752 ASSERT3U(error, ==, 0);
3754 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3758 * We've verified all the old bufwads, and made new ones.
3759 * Now write them out.
3761 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3762 if (zopt_verbose >= 7) {
3763 (void) printf("writing offset %llx size %llx"
3765 (u_longlong_t)bigoff,
3766 (u_longlong_t)bigsize,
3769 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3772 bcopy((caddr_t)bigbuf + (off - bigoff),
3773 bigbuf_arcbufs[j]->b_data, chunksize);
3775 bcopy((caddr_t)bigbuf + (off - bigoff),
3776 bigbuf_arcbufs[2 * j]->b_data,
3778 bcopy((caddr_t)bigbuf + (off - bigoff) +
3780 bigbuf_arcbufs[2 * j + 1]->b_data,
3785 VERIFY(dmu_buf_hold(os, bigobj, off,
3786 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3789 dmu_assign_arcbuf(bonus_db, off,
3790 bigbuf_arcbufs[j], tx);
3792 dmu_assign_arcbuf(bonus_db, off,
3793 bigbuf_arcbufs[2 * j], tx);
3794 dmu_assign_arcbuf(bonus_db,
3795 off + chunksize / 2,
3796 bigbuf_arcbufs[2 * j + 1], tx);
3799 dmu_buf_rele(dbt, FTAG);
3805 * Sanity check the stuff we just wrote.
3808 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3809 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3811 VERIFY(0 == dmu_read(os, packobj, packoff,
3812 packsize, packcheck, DMU_READ_PREFETCH));
3813 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3814 bigsize, bigcheck, DMU_READ_PREFETCH));
3816 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3817 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3819 umem_free(packcheck, packsize);
3820 umem_free(bigcheck, bigsize);
3823 txg_wait_open(dmu_objset_pool(os), 0);
3824 } else if (i == 3) {
3825 txg_wait_synced(dmu_objset_pool(os), 0);
3829 dmu_buf_rele(bonus_db, FTAG);
3830 umem_free(packbuf, packsize);
3831 umem_free(bigbuf, bigsize);
3832 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3833 umem_free(od, size);
3838 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3842 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3843 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3844 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3847 * Have multiple threads write to large offsets in an object
3848 * to verify that parallel writes to an object -- even to the
3849 * same blocks within the object -- doesn't cause any trouble.
3851 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3853 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
3856 while (ztest_random(10) != 0)
3857 ztest_io(zd, od->od_object, offset);
3859 umem_free(od, sizeof(ztest_od_t));
3863 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3866 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3867 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3868 uint64_t count = ztest_random(20) + 1;
3869 uint64_t blocksize = ztest_random_blocksize();
3872 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3874 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3876 if (ztest_object_init(zd, od, sizeof (ztest_od_t), !ztest_random(2)) != 0) {
3877 umem_free(od, sizeof(ztest_od_t));
3881 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
3882 umem_free(od, sizeof(ztest_od_t));
3886 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
3888 data = umem_zalloc(blocksize, UMEM_NOFAIL);
3890 while (ztest_random(count) != 0) {
3891 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3892 if (ztest_write(zd, od->od_object, randoff, blocksize,
3895 while (ztest_random(4) != 0)
3896 ztest_io(zd, od->od_object, randoff);
3899 umem_free(data, blocksize);
3900 umem_free(od, sizeof(ztest_od_t));
3904 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3906 #define ZTEST_ZAP_MIN_INTS 1
3907 #define ZTEST_ZAP_MAX_INTS 4
3908 #define ZTEST_ZAP_MAX_PROPS 1000
3911 ztest_zap(ztest_ds_t *zd, uint64_t id)
3913 objset_t *os = zd->zd_os;
3916 uint64_t txg, last_txg;
3917 uint64_t value[ZTEST_ZAP_MAX_INTS];
3918 uint64_t zl_ints, zl_intsize, prop;
3921 char propname[100], txgname[100];
3923 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3925 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3926 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3928 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
3929 !ztest_random(2)) != 0)
3932 object = od->od_object;
3935 * Generate a known hash collision, and verify that
3936 * we can lookup and remove both entries.
3938 tx = dmu_tx_create(os);
3939 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3940 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3943 for (i = 0; i < 2; i++) {
3945 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3948 for (i = 0; i < 2; i++) {
3949 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3950 sizeof (uint64_t), 1, &value[i], tx));
3952 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3953 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3954 ASSERT3U(zl_ints, ==, 1);
3956 for (i = 0; i < 2; i++) {
3957 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3962 * Generate a buch of random entries.
3964 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3966 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3967 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3968 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3969 bzero(value, sizeof (value));
3973 * If these zap entries already exist, validate their contents.
3975 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3977 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3978 ASSERT3U(zl_ints, ==, 1);
3980 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3981 zl_ints, &last_txg) == 0);
3983 VERIFY(zap_length(os, object, propname, &zl_intsize,
3986 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3987 ASSERT3U(zl_ints, ==, ints);
3989 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3990 zl_ints, value) == 0);
3992 for (i = 0; i < ints; i++) {
3993 ASSERT3U(value[i], ==, last_txg + object + i);
3996 ASSERT3U(error, ==, ENOENT);
4000 * Atomically update two entries in our zap object.
4001 * The first is named txg_%llu, and contains the txg
4002 * in which the property was last updated. The second
4003 * is named prop_%llu, and the nth element of its value
4004 * should be txg + object + n.
4006 tx = dmu_tx_create(os);
4007 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4008 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4013 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4015 for (i = 0; i < ints; i++)
4016 value[i] = txg + object + i;
4018 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4020 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4026 * Remove a random pair of entries.
4028 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4029 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4030 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4032 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4034 if (error == ENOENT)
4037 ASSERT3U(error, ==, 0);
4039 tx = dmu_tx_create(os);
4040 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4041 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4044 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4045 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4048 umem_free(od, sizeof(ztest_od_t));
4052 * Testcase to test the upgrading of a microzap to fatzap.
4055 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4057 objset_t *os = zd->zd_os;
4059 uint64_t object, txg;
4062 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4063 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4065 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4066 !ztest_random(2)) != 0)
4068 object = od->od_object;
4071 * Add entries to this ZAP and make sure it spills over
4072 * and gets upgraded to a fatzap. Also, since we are adding
4073 * 2050 entries we should see ptrtbl growth and leaf-block split.
4075 for (i = 0; i < 2050; i++) {
4076 char name[MAXNAMELEN];
4081 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4082 (u_longlong_t)id, (u_longlong_t)value);
4084 tx = dmu_tx_create(os);
4085 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4086 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4089 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4091 ASSERT(error == 0 || error == EEXIST);
4095 umem_free(od, sizeof(ztest_od_t));
4100 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4102 objset_t *os = zd->zd_os;
4104 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4106 int i, namelen, error;
4107 int micro = ztest_random(2);
4108 char name[20], string_value[20];
4111 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4112 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4114 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4115 umem_free(od, sizeof(ztest_od_t));
4119 object = od->od_object;
4122 * Generate a random name of the form 'xxx.....' where each
4123 * x is a random printable character and the dots are dots.
4124 * There are 94 such characters, and the name length goes from
4125 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4127 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4129 for (i = 0; i < 3; i++)
4130 name[i] = '!' + ztest_random('~' - '!' + 1);
4131 for (; i < namelen - 1; i++)
4135 if ((namelen & 1) || micro) {
4136 wsize = sizeof (txg);
4142 data = string_value;
4146 VERIFY(zap_count(os, object, &count) == 0);
4147 ASSERT(count != -1ULL);
4150 * Select an operation: length, lookup, add, update, remove.
4152 i = ztest_random(5);
4155 tx = dmu_tx_create(os);
4156 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4157 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4160 bcopy(name, string_value, namelen);
4164 bzero(string_value, namelen);
4170 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4172 ASSERT3U(wsize, ==, zl_wsize);
4173 ASSERT3U(wc, ==, zl_wc);
4175 ASSERT3U(error, ==, ENOENT);
4180 error = zap_lookup(os, object, name, wsize, wc, data);
4182 if (data == string_value &&
4183 bcmp(name, data, namelen) != 0)
4184 fatal(0, "name '%s' != val '%s' len %d",
4185 name, data, namelen);
4187 ASSERT3U(error, ==, ENOENT);
4192 error = zap_add(os, object, name, wsize, wc, data, tx);
4193 ASSERT(error == 0 || error == EEXIST);
4197 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4201 error = zap_remove(os, object, name, tx);
4202 ASSERT(error == 0 || error == ENOENT);
4209 umem_free(od, sizeof(ztest_od_t));
4213 * Commit callback data.
4215 typedef struct ztest_cb_data {
4216 list_node_t zcd_node;
4218 int zcd_expected_err;
4219 boolean_t zcd_added;
4220 boolean_t zcd_called;
4224 /* This is the actual commit callback function */
4226 ztest_commit_callback(void *arg, int error)
4228 ztest_cb_data_t *data = arg;
4229 uint64_t synced_txg;
4231 VERIFY(data != NULL);
4232 VERIFY3S(data->zcd_expected_err, ==, error);
4233 VERIFY(!data->zcd_called);
4235 synced_txg = spa_last_synced_txg(data->zcd_spa);
4236 if (data->zcd_txg > synced_txg)
4237 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4238 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4241 data->zcd_called = B_TRUE;
4243 if (error == ECANCELED) {
4244 ASSERT3U(data->zcd_txg, ==, 0);
4245 ASSERT(!data->zcd_added);
4248 * The private callback data should be destroyed here, but
4249 * since we are going to check the zcd_called field after
4250 * dmu_tx_abort(), we will destroy it there.
4255 ASSERT(data->zcd_added);
4256 ASSERT3U(data->zcd_txg, !=, 0);
4258 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4260 /* See if this cb was called more quickly */
4261 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4262 zc_min_txg_delay = synced_txg - data->zcd_txg;
4264 /* Remove our callback from the list */
4265 list_remove(&zcl.zcl_callbacks, data);
4267 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4269 umem_free(data, sizeof (ztest_cb_data_t));
4272 /* Allocate and initialize callback data structure */
4273 static ztest_cb_data_t *
4274 ztest_create_cb_data(objset_t *os, uint64_t txg)
4276 ztest_cb_data_t *cb_data;
4278 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4280 cb_data->zcd_txg = txg;
4281 cb_data->zcd_spa = dmu_objset_spa(os);
4282 list_link_init(&cb_data->zcd_node);
4288 * Commit callback test.
4291 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4293 objset_t *os = zd->zd_os;
4296 ztest_cb_data_t *cb_data[3], *tmp_cb;
4297 uint64_t old_txg, txg;
4300 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4301 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4303 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4304 umem_free(od, sizeof(ztest_od_t));
4308 tx = dmu_tx_create(os);
4310 cb_data[0] = ztest_create_cb_data(os, 0);
4311 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4313 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4315 /* Every once in a while, abort the transaction on purpose */
4316 if (ztest_random(100) == 0)
4320 error = dmu_tx_assign(tx, TXG_NOWAIT);
4322 txg = error ? 0 : dmu_tx_get_txg(tx);
4324 cb_data[0]->zcd_txg = txg;
4325 cb_data[1] = ztest_create_cb_data(os, txg);
4326 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4330 * It's not a strict requirement to call the registered
4331 * callbacks from inside dmu_tx_abort(), but that's what
4332 * it's supposed to happen in the current implementation
4333 * so we will check for that.
4335 for (i = 0; i < 2; i++) {
4336 cb_data[i]->zcd_expected_err = ECANCELED;
4337 VERIFY(!cb_data[i]->zcd_called);
4342 for (i = 0; i < 2; i++) {
4343 VERIFY(cb_data[i]->zcd_called);
4344 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4347 umem_free(od, sizeof(ztest_od_t));
4351 cb_data[2] = ztest_create_cb_data(os, txg);
4352 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4355 * Read existing data to make sure there isn't a future leak.
4357 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4358 &old_txg, DMU_READ_PREFETCH));
4361 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4364 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4366 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4369 * Since commit callbacks don't have any ordering requirement and since
4370 * it is theoretically possible for a commit callback to be called
4371 * after an arbitrary amount of time has elapsed since its txg has been
4372 * synced, it is difficult to reliably determine whether a commit
4373 * callback hasn't been called due to high load or due to a flawed
4376 * In practice, we will assume that if after a certain number of txgs a
4377 * commit callback hasn't been called, then most likely there's an
4378 * implementation bug..
4380 tmp_cb = list_head(&zcl.zcl_callbacks);
4381 if (tmp_cb != NULL &&
4382 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4383 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4384 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4388 * Let's find the place to insert our callbacks.
4390 * Even though the list is ordered by txg, it is possible for the
4391 * insertion point to not be the end because our txg may already be
4392 * quiescing at this point and other callbacks in the open txg
4393 * (from other objsets) may have sneaked in.
4395 tmp_cb = list_tail(&zcl.zcl_callbacks);
4396 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4397 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4399 /* Add the 3 callbacks to the list */
4400 for (i = 0; i < 3; i++) {
4402 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4404 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4407 cb_data[i]->zcd_added = B_TRUE;
4408 VERIFY(!cb_data[i]->zcd_called);
4410 tmp_cb = cb_data[i];
4415 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4419 umem_free(od, sizeof(ztest_od_t));
4424 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4426 zfs_prop_t proplist[] = {
4428 ZFS_PROP_COMPRESSION,
4432 ztest_shared_t *zs = ztest_shared;
4435 (void) rw_enter(&zs->zs_name_lock, RW_READER);
4437 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4438 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4439 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4441 (void) rw_exit(&zs->zs_name_lock);
4446 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4448 ztest_shared_t *zs = ztest_shared;
4449 nvlist_t *props = NULL;
4451 (void) rw_enter(&zs->zs_name_lock, RW_READER);
4453 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4454 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4456 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4458 if (zopt_verbose >= 6)
4459 dump_nvlist(props, 4);
4463 (void) rw_exit(&zs->zs_name_lock);
4467 * Test snapshot hold/release and deferred destroy.
4470 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4473 objset_t *os = zd->zd_os;
4477 char clonename[100];
4479 char osname[MAXNAMELEN];
4481 (void) rw_enter(&ztest_shared->zs_name_lock, RW_READER);
4483 dmu_objset_name(os, osname);
4485 (void) snprintf(snapname, 100, "sh1_%llu", (u_longlong_t)id);
4486 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4487 (void) snprintf(clonename, 100, "%s/ch1_%llu",osname,(u_longlong_t)id);
4488 (void) snprintf(tag, 100, "tag_%llu", (u_longlong_t)id);
4491 * Clean up from any previous run.
4493 (void) dmu_objset_destroy(clonename, B_FALSE);
4494 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4495 (void) dmu_objset_destroy(fullname, B_FALSE);
4498 * Create snapshot, clone it, mark snap for deferred destroy,
4499 * destroy clone, verify snap was also destroyed.
4501 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4504 if (error == ENOSPC) {
4505 ztest_record_enospc("dmu_objset_snapshot");
4508 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4511 error = dmu_objset_hold(fullname, FTAG, &origin);
4513 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4515 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4516 dmu_objset_rele(origin, FTAG);
4518 if (error == ENOSPC) {
4519 ztest_record_enospc("dmu_objset_clone");
4522 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4525 error = dmu_objset_destroy(fullname, B_TRUE);
4527 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4531 error = dmu_objset_destroy(clonename, B_FALSE);
4533 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4535 error = dmu_objset_hold(fullname, FTAG, &origin);
4536 if (error != ENOENT)
4537 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4540 * Create snapshot, add temporary hold, verify that we can't
4541 * destroy a held snapshot, mark for deferred destroy,
4542 * release hold, verify snapshot was destroyed.
4544 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4547 if (error == ENOSPC) {
4548 ztest_record_enospc("dmu_objset_snapshot");
4551 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4554 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4557 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4559 error = dmu_objset_destroy(fullname, B_FALSE);
4560 if (error != EBUSY) {
4561 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4565 error = dmu_objset_destroy(fullname, B_TRUE);
4567 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4571 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4573 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4575 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4578 (void) rw_exit(&ztest_shared->zs_name_lock);
4582 * Inject random faults into the on-disk data.
4586 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4588 ztest_shared_t *zs = ztest_shared;
4589 spa_t *spa = zs->zs_spa;
4593 uint64_t bad = 0x1990c0ffeedecadeull;
4598 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4604 boolean_t islog = B_FALSE;
4606 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4607 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4609 mutex_enter(&zs->zs_vdev_lock);
4610 maxfaults = MAXFAULTS();
4611 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4612 mirror_save = zs->zs_mirrors;
4613 mutex_exit(&zs->zs_vdev_lock);
4615 ASSERT(leaves >= 1);
4618 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4620 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4622 if (ztest_random(2) == 0) {
4624 * Inject errors on a normal data device or slog device.
4626 top = ztest_random_vdev_top(spa, B_TRUE);
4627 leaf = ztest_random(leaves) + zs->zs_splits;
4630 * Generate paths to the first leaf in this top-level vdev,
4631 * and to the random leaf we selected. We'll induce transient
4632 * write failures and random online/offline activity on leaf 0,
4633 * and we'll write random garbage to the randomly chosen leaf.
4635 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
4636 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4637 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4638 zopt_dir, zopt_pool, top * leaves + leaf);
4640 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4641 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4644 if (vd0 != NULL && maxfaults != 1) {
4646 * Make vd0 explicitly claim to be unreadable,
4647 * or unwriteable, or reach behind its back
4648 * and close the underlying fd. We can do this if
4649 * maxfaults == 0 because we'll fail and reexecute,
4650 * and we can do it if maxfaults >= 2 because we'll
4651 * have enough redundancy. If maxfaults == 1, the
4652 * combination of this with injection of random data
4653 * corruption below exceeds the pool's fault tolerance.
4655 vdev_file_t *vf = vd0->vdev_tsd;
4657 if (vf != NULL && ztest_random(3) == 0) {
4658 (void) close(vf->vf_vnode->v_fd);
4659 vf->vf_vnode->v_fd = -1;
4660 } else if (ztest_random(2) == 0) {
4661 vd0->vdev_cant_read = B_TRUE;
4663 vd0->vdev_cant_write = B_TRUE;
4665 guid0 = vd0->vdev_guid;
4669 * Inject errors on an l2cache device.
4671 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4673 if (sav->sav_count == 0) {
4674 spa_config_exit(spa, SCL_STATE, FTAG);
4677 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4678 guid0 = vd0->vdev_guid;
4679 (void) strcpy(path0, vd0->vdev_path);
4680 (void) strcpy(pathrand, vd0->vdev_path);
4684 maxfaults = INT_MAX; /* no limit on cache devices */
4687 spa_config_exit(spa, SCL_STATE, FTAG);
4690 * If we can tolerate two or more faults, or we're dealing
4691 * with a slog, randomly online/offline vd0.
4693 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4694 if (ztest_random(10) < 6) {
4695 int flags = (ztest_random(2) == 0 ?
4696 ZFS_OFFLINE_TEMPORARY : 0);
4699 * We have to grab the zs_name_lock as writer to
4700 * prevent a race between offlining a slog and
4701 * destroying a dataset. Offlining the slog will
4702 * grab a reference on the dataset which may cause
4703 * dmu_objset_destroy() to fail with EBUSY thus
4704 * leaving the dataset in an inconsistent state.
4707 (void) rw_enter(&ztest_shared->zs_name_lock,
4710 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4713 (void) rw_exit(&ztest_shared->zs_name_lock);
4715 (void) vdev_online(spa, guid0, 0, NULL);
4723 * We have at least single-fault tolerance, so inject data corruption.
4725 fd = open(pathrand, O_RDWR);
4727 if (fd == -1) /* we hit a gap in the device namespace */
4730 fsize = lseek(fd, 0, SEEK_END);
4732 while (--iters != 0) {
4733 offset = ztest_random(fsize / (leaves << bshift)) *
4734 (leaves << bshift) + (leaf << bshift) +
4735 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4737 if (offset >= fsize)
4740 mutex_enter(&zs->zs_vdev_lock);
4741 if (mirror_save != zs->zs_mirrors) {
4742 mutex_exit(&zs->zs_vdev_lock);
4747 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4748 fatal(1, "can't inject bad word at 0x%llx in %s",
4751 mutex_exit(&zs->zs_vdev_lock);
4753 if (zopt_verbose >= 7)
4754 (void) printf("injected bad word into %s,"
4755 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4760 umem_free(path0, MAXPATHLEN);
4761 umem_free(pathrand, MAXPATHLEN);
4765 * Verify that DDT repair works as expected.
4768 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4770 ztest_shared_t *zs = ztest_shared;
4771 spa_t *spa = zs->zs_spa;
4772 objset_t *os = zd->zd_os;
4774 uint64_t object, blocksize, txg, pattern, psize;
4775 enum zio_checksum checksum = spa_dedup_checksum(spa);
4780 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4783 blocksize = ztest_random_blocksize();
4784 blocksize = MIN(blocksize, 2048); /* because we write so many */
4786 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4787 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4789 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4790 umem_free(od, sizeof(ztest_od_t));
4795 * Take the name lock as writer to prevent anyone else from changing
4796 * the pool and dataset properies we need to maintain during this test.
4798 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
4800 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4802 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4804 (void) rw_exit(&zs->zs_name_lock);
4805 umem_free(od, sizeof(ztest_od_t));
4809 object = od[0].od_object;
4810 blocksize = od[0].od_blocksize;
4811 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
4813 ASSERT(object != 0);
4815 tx = dmu_tx_create(os);
4816 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4817 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4819 (void) rw_exit(&zs->zs_name_lock);
4820 umem_free(od, sizeof(ztest_od_t));
4825 * Write all the copies of our block.
4827 for (i = 0; i < copies; i++) {
4828 uint64_t offset = i * blocksize;
4829 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4830 DMU_READ_NO_PREFETCH) == 0);
4831 ASSERT(db->db_offset == offset);
4832 ASSERT(db->db_size == blocksize);
4833 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4834 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4835 dmu_buf_will_fill(db, tx);
4836 ztest_pattern_set(db->db_data, db->db_size, pattern);
4837 dmu_buf_rele(db, FTAG);
4841 txg_wait_synced(spa_get_dsl(spa), txg);
4844 * Find out what block we got.
4846 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4847 DMU_READ_NO_PREFETCH) == 0);
4848 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4849 dmu_buf_rele(db, FTAG);
4852 * Damage the block. Dedup-ditto will save us when we read it later.
4854 psize = BP_GET_PSIZE(&blk);
4855 buf = zio_buf_alloc(psize);
4856 ztest_pattern_set(buf, psize, ~pattern);
4858 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4859 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4860 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4862 zio_buf_free(buf, psize);
4864 (void) rw_exit(&zs->zs_name_lock);
4865 umem_free(od, sizeof(ztest_od_t));
4873 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4875 ztest_shared_t *zs = ztest_shared;
4876 spa_t *spa = zs->zs_spa;
4878 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4879 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4880 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4884 * Change the guid for the pool.
4888 ztest_reguid(ztest_ds_t *zd, uint64_t id)
4890 ztest_shared_t *zs = ztest_shared;
4891 spa_t *spa = zs->zs_spa;
4892 uint64_t orig, load;
4894 orig = spa_guid(spa);
4895 load = spa_load_guid(spa);
4896 if (spa_change_guid(spa) != 0)
4899 if (zopt_verbose >= 3) {
4900 (void) printf("Changed guid old %llu -> %llu\n",
4901 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
4904 VERIFY3U(orig, !=, spa_guid(spa));
4905 VERIFY3U(load, ==, spa_load_guid(spa));
4909 * Rename the pool to a different name and then rename it back.
4913 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4915 ztest_shared_t *zs = ztest_shared;
4916 char *oldname, *newname;
4919 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
4921 oldname = zs->zs_pool;
4922 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4923 (void) strcpy(newname, oldname);
4924 (void) strcat(newname, "_tmp");
4929 VERIFY3U(0, ==, spa_rename(oldname, newname));
4932 * Try to open it under the old name, which shouldn't exist
4934 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4937 * Open it under the new name and make sure it's still the same spa_t.
4939 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4941 ASSERT(spa == zs->zs_spa);
4942 spa_close(spa, FTAG);
4945 * Rename it back to the original
4947 VERIFY3U(0, ==, spa_rename(newname, oldname));
4950 * Make sure it can still be opened
4952 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4954 ASSERT(spa == zs->zs_spa);
4955 spa_close(spa, FTAG);
4957 umem_free(newname, strlen(newname) + 1);
4959 (void) rw_exit(&zs->zs_name_lock);
4963 * Verify pool integrity by running zdb.
4966 ztest_run_zdb(char *pool)
4974 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
4975 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
4976 zbuf = umem_alloc(1024, UMEM_NOFAIL);
4978 VERIFY(realpath(getexecname(), bin) != NULL);
4979 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
4980 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
4981 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
4982 strcpy(bin, "/sbin/zdb"); /* Installed */
4984 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
4985 strcat(bin, "/zdb/zdb");
4989 "%s -bcc%s%s -U %s %s",
4991 zopt_verbose >= 3 ? "s" : "",
4992 zopt_verbose >= 4 ? "v" : "",
4996 if (zopt_verbose >= 5)
4997 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4999 fp = popen(zdb, "r");
5001 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
5002 if (zopt_verbose >= 3)
5003 (void) printf("%s", zbuf);
5005 status = pclose(fp);
5010 ztest_dump_core = 0;
5011 if (WIFEXITED(status))
5012 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5014 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5016 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5017 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5018 umem_free(zbuf, 1024);
5022 ztest_walk_pool_directory(char *header)
5026 if (zopt_verbose >= 6)
5027 (void) printf("%s\n", header);
5029 mutex_enter(&spa_namespace_lock);
5030 while ((spa = spa_next(spa)) != NULL)
5031 if (zopt_verbose >= 6)
5032 (void) printf("\t%s\n", spa_name(spa));
5033 mutex_exit(&spa_namespace_lock);
5037 ztest_spa_import_export(char *oldname, char *newname)
5039 nvlist_t *config, *newconfig;
5043 if (zopt_verbose >= 4) {
5044 (void) printf("import/export: old = %s, new = %s\n",
5049 * Clean up from previous runs.
5051 (void) spa_destroy(newname);
5054 * Get the pool's configuration and guid.
5056 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5059 * Kick off a scrub to tickle scrub/export races.
5061 if (ztest_random(2) == 0)
5062 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5064 pool_guid = spa_guid(spa);
5065 spa_close(spa, FTAG);
5067 ztest_walk_pool_directory("pools before export");
5072 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5074 ztest_walk_pool_directory("pools after export");
5079 newconfig = spa_tryimport(config);
5080 ASSERT(newconfig != NULL);
5081 nvlist_free(newconfig);
5084 * Import it under the new name.
5086 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
5088 ztest_walk_pool_directory("pools after import");
5091 * Try to import it again -- should fail with EEXIST.
5093 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5096 * Try to import it under a different name -- should fail with EEXIST.
5098 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5101 * Verify that the pool is no longer visible under the old name.
5103 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5106 * Verify that we can open and close the pool using the new name.
5108 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5109 ASSERT(pool_guid == spa_guid(spa));
5110 spa_close(spa, FTAG);
5112 nvlist_free(config);
5116 ztest_resume(spa_t *spa)
5118 if (spa_suspended(spa) && zopt_verbose >= 6)
5119 (void) printf("resuming from suspended state\n");
5120 spa_vdev_state_enter(spa, SCL_NONE);
5121 vdev_clear(spa, NULL);
5122 (void) spa_vdev_state_exit(spa, NULL, 0);
5123 (void) zio_resume(spa);
5127 ztest_resume_thread(void *arg)
5131 while (!ztest_exiting) {
5132 if (spa_suspended(spa))
5134 (void) poll(NULL, 0, 100);
5145 ztest_deadman_alarm(int sig)
5147 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5151 ztest_execute(ztest_info_t *zi, uint64_t id)
5153 ztest_shared_t *zs = ztest_shared;
5154 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
5155 hrtime_t functime = gethrtime();
5158 for (i = 0; i < zi->zi_iters; i++)
5159 zi->zi_func(zd, id);
5161 functime = gethrtime() - functime;
5163 atomic_add_64(&zi->zi_call_count, 1);
5164 atomic_add_64(&zi->zi_call_time, functime);
5166 if (zopt_verbose >= 4) {
5168 (void) dladdr((void *)zi->zi_func, &dli);
5169 (void) printf("%6.2f sec in %s\n",
5170 (double)functime / NANOSEC, dli.dli_sname);
5175 ztest_thread(void *arg)
5177 uint64_t id = (uintptr_t)arg;
5178 ztest_shared_t *zs = ztest_shared;
5183 while ((now = gethrtime()) < zs->zs_thread_stop) {
5185 * See if it's time to force a crash.
5187 if (now > zs->zs_thread_kill)
5191 * If we're getting ENOSPC with some regularity, stop.
5193 if (zs->zs_enospc_count > 10)
5197 * Pick a random function to execute.
5199 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
5200 call_next = zi->zi_call_next;
5202 if (now >= call_next &&
5203 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5204 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5205 ztest_execute(zi, id);
5214 ztest_dataset_name(char *dsname, char *pool, int d)
5216 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5220 ztest_dataset_destroy(ztest_shared_t *zs, int d)
5222 char name[MAXNAMELEN];
5225 ztest_dataset_name(name, zs->zs_pool, d);
5227 if (zopt_verbose >= 3)
5228 (void) printf("Destroying %s to free up space\n", name);
5231 * Cleanup any non-standard clones and snapshots. In general,
5232 * ztest thread t operates on dataset (t % zopt_datasets),
5233 * so there may be more than one thing to clean up.
5235 for (t = d; t < zopt_threads; t += zopt_datasets)
5236 ztest_dsl_dataset_cleanup(name, t);
5238 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5239 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5243 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5245 uint64_t usedobjs, dirobjs, scratch;
5248 * ZTEST_DIROBJ is the object directory for the entire dataset.
5249 * Therefore, the number of objects in use should equal the
5250 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5251 * If not, we have an object leak.
5253 * Note that we can only check this in ztest_dataset_open(),
5254 * when the open-context and syncing-context values agree.
5255 * That's because zap_count() returns the open-context value,
5256 * while dmu_objset_space() returns the rootbp fill count.
5258 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5259 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5260 ASSERT3U(dirobjs + 1, ==, usedobjs);
5264 ztest_dataset_open(ztest_shared_t *zs, int d)
5266 ztest_ds_t *zd = &zs->zs_zd[d];
5267 uint64_t committed_seq = zd->zd_seq;
5270 char name[MAXNAMELEN];
5273 ztest_dataset_name(name, zs->zs_pool, d);
5275 (void) rw_enter(&zs->zs_name_lock, RW_READER);
5277 error = ztest_dataset_create(name);
5278 if (error == ENOSPC) {
5279 (void) rw_exit(&zs->zs_name_lock);
5280 ztest_record_enospc(FTAG);
5283 ASSERT(error == 0 || error == EEXIST);
5285 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5286 (void) rw_exit(&zs->zs_name_lock);
5288 ztest_zd_init(zd, os);
5290 zilog = zd->zd_zilog;
5292 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5293 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5294 fatal(0, "missing log records: claimed %llu < committed %llu",
5295 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5297 ztest_dataset_dirobj_verify(zd);
5299 zil_replay(os, zd, ztest_replay_vector);
5301 ztest_dataset_dirobj_verify(zd);
5303 if (zopt_verbose >= 6)
5304 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5306 (u_longlong_t)zilog->zl_parse_blk_count,
5307 (u_longlong_t)zilog->zl_parse_lr_count,
5308 (u_longlong_t)zilog->zl_replaying_seq);
5310 zilog = zil_open(os, ztest_get_data);
5312 if (zilog->zl_replaying_seq != 0 &&
5313 zilog->zl_replaying_seq < committed_seq)
5314 fatal(0, "missing log records: replayed %llu < committed %llu",
5315 zilog->zl_replaying_seq, committed_seq);
5321 ztest_dataset_close(ztest_shared_t *zs, int d)
5323 ztest_ds_t *zd = &zs->zs_zd[d];
5325 zil_close(zd->zd_zilog);
5326 dmu_objset_rele(zd->zd_os, zd);
5332 * Kick off threads to run tests on all datasets in parallel.
5335 ztest_run(ztest_shared_t *zs)
5340 kthread_t *resume_thread;
5345 ztest_exiting = B_FALSE;
5348 * Initialize parent/child shared state.
5350 mutex_init(&zs->zs_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5351 rw_init(&zs->zs_name_lock, NULL, RW_DEFAULT, NULL);
5353 zs->zs_thread_start = gethrtime();
5354 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5355 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5356 zs->zs_thread_kill = zs->zs_thread_stop;
5357 if (ztest_random(100) < zopt_killrate)
5358 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5360 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5362 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5363 offsetof(ztest_cb_data_t, zcd_node));
5368 kernel_init(FREAD | FWRITE);
5369 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5370 spa->spa_debug = B_TRUE;
5373 VERIFY3U(0, ==, dmu_objset_hold(zs->zs_pool, FTAG, &os));
5374 zs->zs_guid = dmu_objset_fsid_guid(os);
5375 dmu_objset_rele(os, FTAG);
5377 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5380 * We don't expect the pool to suspend unless maxfaults == 0,
5381 * in which case ztest_fault_inject() temporarily takes away
5382 * the only valid replica.
5384 if (MAXFAULTS() == 0)
5385 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5387 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5390 * Create a thread to periodically resume suspended I/O.
5392 VERIFY3P((resume_thread = thread_create(NULL, 0, ztest_resume_thread,
5393 spa, TS_RUN, NULL, 0, 0)), !=, NULL);
5396 * Set a deadman alarm to abort() if we hang.
5398 signal(SIGALRM, ztest_deadman_alarm);
5399 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5402 * Verify that we can safely inquire about about any object,
5403 * whether it's allocated or not. To make it interesting,
5404 * we probe a 5-wide window around each power of two.
5405 * This hits all edge cases, including zero and the max.
5407 for (t = 0; t < 64; t++) {
5408 for (d = -5; d <= 5; d++) {
5409 error = dmu_object_info(spa->spa_meta_objset,
5410 (1ULL << t) + d, NULL);
5411 ASSERT(error == 0 || error == ENOENT ||
5417 * If we got any ENOSPC errors on the previous run, destroy something.
5419 if (zs->zs_enospc_count != 0) {
5420 int d = ztest_random(zopt_datasets);
5421 ztest_dataset_destroy(zs, d);
5423 zs->zs_enospc_count = 0;
5425 tid = umem_zalloc(zopt_threads * sizeof (kt_did_t), UMEM_NOFAIL);
5427 if (zopt_verbose >= 4)
5428 (void) printf("starting main threads...\n");
5431 * Kick off all the tests that run in parallel.
5433 for (t = 0; t < zopt_threads; t++) {
5436 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5439 VERIFY3P(thread = thread_create(NULL, 0, ztest_thread,
5440 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0), !=, NULL);
5441 tid[t] = thread->t_tid;
5445 * Wait for all of the tests to complete. We go in reverse order
5446 * so we don't close datasets while threads are still using them.
5448 for (t = zopt_threads - 1; t >= 0; t--) {
5449 thread_join(tid[t]);
5450 if (t < zopt_datasets)
5451 ztest_dataset_close(zs, t);
5454 txg_wait_synced(spa_get_dsl(spa), 0);
5456 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5457 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5459 umem_free(tid, zopt_threads * sizeof (kt_did_t));
5461 /* Kill the resume thread */
5462 ztest_exiting = B_TRUE;
5463 thread_join(resume_thread->t_tid);
5467 * Right before closing the pool, kick off a bunch of async I/O;
5468 * spa_close() should wait for it to complete.
5470 for (object = 1; object < 50; object++)
5471 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5473 /* Verify that at least one commit cb was called in a timely fashion */
5474 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5475 VERIFY3U(zc_min_txg_delay, ==, 0);
5477 spa_close(spa, FTAG);
5480 * Verify that we can loop over all pools.
5482 mutex_enter(&spa_namespace_lock);
5483 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5484 if (zopt_verbose > 3)
5485 (void) printf("spa_next: found %s\n", spa_name(spa));
5486 mutex_exit(&spa_namespace_lock);
5489 * Verify that we can export the pool and reimport it under a
5492 if (ztest_random(2) == 0) {
5493 char name[MAXNAMELEN];
5494 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5495 ztest_spa_import_export(zs->zs_pool, name);
5496 ztest_spa_import_export(name, zs->zs_pool);
5501 list_destroy(&zcl.zcl_callbacks);
5502 mutex_destroy(&zcl.zcl_callbacks_lock);
5503 rw_destroy(&zs->zs_name_lock);
5504 mutex_destroy(&zs->zs_vdev_lock);
5508 ztest_freeze(ztest_shared_t *zs)
5510 ztest_ds_t *zd = &zs->zs_zd[0];
5514 if (zopt_verbose >= 3)
5515 (void) printf("testing spa_freeze()...\n");
5517 kernel_init(FREAD | FWRITE);
5518 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5519 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5522 * Force the first log block to be transactionally allocated.
5523 * We have to do this before we freeze the pool -- otherwise
5524 * the log chain won't be anchored.
5526 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5527 ztest_dmu_object_alloc_free(zd, 0);
5528 zil_commit(zd->zd_zilog, 0);
5531 txg_wait_synced(spa_get_dsl(spa), 0);
5534 * Freeze the pool. This stops spa_sync() from doing anything,
5535 * so that the only way to record changes from now on is the ZIL.
5540 * Run tests that generate log records but don't alter the pool config
5541 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5542 * We do a txg_wait_synced() after each iteration to force the txg
5543 * to increase well beyond the last synced value in the uberblock.
5544 * The ZIL should be OK with that.
5546 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5547 ztest_dmu_write_parallel(zd, 0);
5548 ztest_dmu_object_alloc_free(zd, 0);
5549 txg_wait_synced(spa_get_dsl(spa), 0);
5553 * Commit all of the changes we just generated.
5555 zil_commit(zd->zd_zilog, 0);
5556 txg_wait_synced(spa_get_dsl(spa), 0);
5559 * Close our dataset and close the pool.
5561 ztest_dataset_close(zs, 0);
5562 spa_close(spa, FTAG);
5566 * Open and close the pool and dataset to induce log replay.
5568 kernel_init(FREAD | FWRITE);
5569 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5570 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5571 ztest_dataset_close(zs, 0);
5572 spa_close(spa, FTAG);
5577 print_time(hrtime_t t, char *timebuf)
5579 hrtime_t s = t / NANOSEC;
5580 hrtime_t m = s / 60;
5581 hrtime_t h = m / 60;
5582 hrtime_t d = h / 24;
5591 (void) sprintf(timebuf,
5592 "%llud%02lluh%02llum%02llus", d, h, m, s);
5594 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5596 (void) sprintf(timebuf, "%llum%02llus", m, s);
5598 (void) sprintf(timebuf, "%llus", s);
5602 make_random_props(void)
5606 if (ztest_random(2) == 0)
5609 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5610 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5612 (void) printf("props:\n");
5613 dump_nvlist(props, 4);
5619 * Create a storage pool with the given name and initial vdev size.
5620 * Then test spa_freeze() functionality.
5623 ztest_init(ztest_shared_t *zs)
5626 nvlist_t *nvroot, *props;
5628 mutex_init(&zs->zs_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5629 rw_init(&zs->zs_name_lock, NULL, RW_DEFAULT, NULL);
5631 kernel_init(FREAD | FWRITE);
5634 * Create the storage pool.
5636 (void) spa_destroy(zs->zs_pool);
5637 ztest_shared->zs_vdev_next_leaf = 0;
5639 zs->zs_mirrors = zopt_mirrors;
5640 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5641 0, zopt_raidz, zs->zs_mirrors, 1);
5642 props = make_random_props();
5643 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5644 nvlist_free(nvroot);
5646 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5647 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5648 spa_close(spa, FTAG);
5652 ztest_run_zdb(zs->zs_pool);
5656 ztest_run_zdb(zs->zs_pool);
5658 (void) rw_destroy(&zs->zs_name_lock);
5659 (void) mutex_destroy(&zs->zs_vdev_lock);
5663 main(int argc, char **argv)
5675 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5677 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5679 dprintf_setup(&argc, argv);
5680 process_options(argc, argv);
5682 /* Override location of zpool.cache */
5683 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
5687 * Blow away any existing copy of zpool.cache
5690 (void) remove(spa_config_path);
5692 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5694 zs = ztest_shared = (void *)mmap(0,
5695 P2ROUNDUP(shared_size, getpagesize()),
5696 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5698 if (zopt_verbose >= 1) {
5699 (void) printf("%llu vdevs, %d datasets, %d threads,"
5700 " %llu seconds...\n",
5701 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5702 (u_longlong_t)zopt_time);
5706 * Create and initialize our storage pool.
5708 for (i = 1; i <= zopt_init; i++) {
5709 bzero(zs, sizeof (ztest_shared_t));
5710 if (zopt_verbose >= 3 && zopt_init != 1)
5711 (void) printf("ztest_init(), pass %d\n", i);
5712 zs->zs_pool = zopt_pool;
5716 zs->zs_pool = zopt_pool;
5717 zs->zs_proc_start = gethrtime();
5718 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5720 for (f = 0; f < ZTEST_FUNCS; f++) {
5721 zi = &zs->zs_info[f];
5722 *zi = ztest_info[f];
5723 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5724 zi->zi_call_next = UINT64_MAX;
5726 zi->zi_call_next = zs->zs_proc_start +
5727 ztest_random(2 * zi->zi_interval[0] + 1);
5731 * Run the tests in a loop. These tests include fault injection
5732 * to verify that self-healing data works, and forced crashes
5733 * to verify that we never lose on-disk consistency.
5735 while (gethrtime() < zs->zs_proc_stop) {
5740 * Initialize the workload counters for each function.
5742 for (f = 0; f < ZTEST_FUNCS; f++) {
5743 zi = &zs->zs_info[f];
5744 zi->zi_call_count = 0;
5745 zi->zi_call_time = 0;
5748 /* Set the allocation switch size */
5749 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5754 fatal(1, "fork failed");
5756 if (pid == 0) { /* child */
5757 struct rlimit rl = { 1024, 1024 };
5758 (void) setrlimit(RLIMIT_NOFILE, &rl);
5759 (void) enable_extended_FILE_stdio(-1, -1);
5764 while (waitpid(pid, &status, 0) != pid)
5767 if (WIFEXITED(status)) {
5768 if (WEXITSTATUS(status) != 0) {
5769 (void) fprintf(stderr,
5770 "child exited with code %d\n",
5771 WEXITSTATUS(status));
5774 } else if (WIFSIGNALED(status)) {
5775 if (WTERMSIG(status) != SIGKILL) {
5776 (void) fprintf(stderr,
5777 "child died with signal %d\n",
5783 (void) fprintf(stderr, "something strange happened "
5790 if (zopt_verbose >= 1) {
5791 hrtime_t now = gethrtime();
5793 now = MIN(now, zs->zs_proc_stop);
5794 print_time(zs->zs_proc_stop - now, timebuf);
5795 nicenum(zs->zs_space, numbuf);
5797 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5798 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5800 WIFEXITED(status) ? "Complete" : "SIGKILL",
5801 (u_longlong_t)zs->zs_enospc_count,
5802 100.0 * zs->zs_alloc / zs->zs_space,
5804 100.0 * (now - zs->zs_proc_start) /
5805 (zopt_time * NANOSEC), timebuf);
5808 if (zopt_verbose >= 2) {
5809 (void) printf("\nWorkload summary:\n\n");
5810 (void) printf("%7s %9s %s\n",
5811 "Calls", "Time", "Function");
5812 (void) printf("%7s %9s %s\n",
5813 "-----", "----", "--------");
5814 for (f = 0; f < ZTEST_FUNCS; f++) {
5817 zi = &zs->zs_info[f];
5818 print_time(zi->zi_call_time, timebuf);
5819 (void) dladdr((void *)zi->zi_func, &dli);
5820 (void) printf("%7llu %9s %s\n",
5821 (u_longlong_t)zi->zi_call_count, timebuf,
5824 (void) printf("\n");
5828 * It's possible that we killed a child during a rename test,
5829 * in which case we'll have a 'ztest_tmp' pool lying around
5830 * instead of 'ztest'. Do a blind rename in case this happened.
5833 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5834 spa_close(spa, FTAG);
5836 char tmpname[MAXNAMELEN];
5838 kernel_init(FREAD | FWRITE);
5839 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5841 (void) spa_rename(tmpname, zopt_pool);
5845 ztest_run_zdb(zopt_pool);
5848 if (zopt_verbose >= 1) {
5849 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5850 kills, iters - kills, (100.0 * kills) / MAX(1, iters));