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.
27 * The objective of this program is to provide a DMU/ZAP/SPA stress test
28 * that runs entirely in userland, is easy to use, and easy to extend.
30 * The overall design of the ztest program is as follows:
32 * (1) For each major functional area (e.g. adding vdevs to a pool,
33 * creating and destroying datasets, reading and writing objects, etc)
34 * we have a simple routine to test that functionality. These
35 * individual routines do not have to do anything "stressful".
37 * (2) We turn these simple functionality tests into a stress test by
38 * running them all in parallel, with as many threads as desired,
39 * and spread across as many datasets, objects, and vdevs as desired.
41 * (3) While all this is happening, we inject faults into the pool to
42 * verify that self-healing data really works.
44 * (4) Every time we open a dataset, we change its checksum and compression
45 * functions. Thus even individual objects vary from block to block
46 * in which checksum they use and whether they're compressed.
48 * (5) To verify that we never lose on-disk consistency after a crash,
49 * we run the entire test in a child of the main process.
50 * At random times, the child self-immolates with a SIGKILL.
51 * This is the software equivalent of pulling the power cord.
52 * The parent then runs the test again, using the existing
53 * storage pool, as many times as desired.
55 * (6) To verify that we don't have future leaks or temporal incursions,
56 * many of the functional tests record the transaction group number
57 * as part of their data. When reading old data, they verify that
58 * the transaction group number is less than the current, open txg.
59 * If you add a new test, please do this if applicable.
61 * (7) Threads are created with a reduced stack size, for sanity checking.
62 * Therefore, it's important not to allocate huge buffers on the stack.
64 * When run with no arguments, ztest runs for about five minutes and
65 * produces no output if successful. To get a little bit of information,
66 * specify -V. To get more information, specify -VV, and so on.
68 * To turn this into an overnight stress test, use -T to specify run time.
70 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
71 * to increase the pool capacity, fanout, and overall stress level.
73 * The -N(okill) option will suppress kills, so each child runs to completion.
74 * This can be useful when you're trying to distinguish temporal incursions
75 * from plain old race conditions.
78 #include <sys/zfs_context.h>
84 #include <sys/dmu_objset.h>
90 #include <sys/resource.h>
93 #include <sys/zil_impl.h>
94 #include <sys/vdev_impl.h>
95 #include <sys/vdev_file.h>
96 #include <sys/spa_impl.h>
97 #include <sys/metaslab_impl.h>
98 #include <sys/dsl_prop.h>
99 #include <sys/dsl_dataset.h>
100 #include <sys/dsl_scan.h>
101 #include <sys/zio_checksum.h>
102 #include <sys/refcount.h>
104 #include <stdio_ext.h>
112 #include <sys/fs/zfs.h>
113 #include <libnvpair.h>
115 static char cmdname[] = "ztest";
116 static char *zopt_pool = cmdname;
118 static uint64_t zopt_vdevs = 5;
119 static uint64_t zopt_vdevtime;
120 static int zopt_ashift = SPA_MINBLOCKSHIFT;
121 static int zopt_mirrors = 2;
122 static int zopt_raidz = 4;
123 static int zopt_raidz_parity = 1;
124 static size_t zopt_vdev_size = SPA_MINDEVSIZE;
125 static int zopt_datasets = 7;
126 static int zopt_threads = 23;
127 static uint64_t zopt_passtime = 60; /* 60 seconds */
128 static uint64_t zopt_killrate = 70; /* 70% kill rate */
129 static int zopt_verbose = 0;
130 static int zopt_init = 1;
131 static char *zopt_dir = "/tmp";
132 static uint64_t zopt_time = 300; /* 5 minutes */
133 static uint64_t zopt_maxloops = 50; /* max loops during spa_freeze() */
135 #define BT_MAGIC 0x123456789abcdefULL
136 #define MAXFAULTS() (MAX(zs->zs_mirrors, 1) * (zopt_raidz_parity + 1) - 1)
140 ZTEST_IO_WRITE_PATTERN,
141 ZTEST_IO_WRITE_ZEROES,
147 typedef struct ztest_block_tag {
157 typedef struct bufwad {
164 * XXX -- fix zfs range locks to be generic so we can use them here.
186 #define ZTEST_RANGE_LOCKS 64
187 #define ZTEST_OBJECT_LOCKS 64
190 * Object descriptor. Used as a template for object lookup/create/remove.
192 typedef struct ztest_od {
195 dmu_object_type_t od_type;
196 dmu_object_type_t od_crtype;
197 uint64_t od_blocksize;
198 uint64_t od_crblocksize;
201 char od_name[MAXNAMELEN];
207 typedef struct ztest_ds {
211 ztest_od_t *zd_od; /* debugging aid */
212 char zd_name[MAXNAMELEN];
213 kmutex_t zd_dirobj_lock;
214 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
215 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
219 * Per-iteration state.
221 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
223 typedef struct ztest_info {
224 ztest_func_t *zi_func; /* test function */
225 uint64_t zi_iters; /* iterations per execution */
226 uint64_t *zi_interval; /* execute every <interval> seconds */
227 uint64_t zi_call_count; /* per-pass count */
228 uint64_t zi_call_time; /* per-pass time */
229 uint64_t zi_call_next; /* next time to call this function */
233 * Note: these aren't static because we want dladdr() to work.
235 ztest_func_t ztest_dmu_read_write;
236 ztest_func_t ztest_dmu_write_parallel;
237 ztest_func_t ztest_dmu_object_alloc_free;
238 ztest_func_t ztest_dmu_commit_callbacks;
239 ztest_func_t ztest_zap;
240 ztest_func_t ztest_zap_parallel;
241 ztest_func_t ztest_zil_commit;
242 ztest_func_t ztest_dmu_read_write_zcopy;
243 ztest_func_t ztest_dmu_objset_create_destroy;
244 ztest_func_t ztest_dmu_prealloc;
245 ztest_func_t ztest_fzap;
246 ztest_func_t ztest_dmu_snapshot_create_destroy;
247 ztest_func_t ztest_dsl_prop_get_set;
248 ztest_func_t ztest_spa_prop_get_set;
249 ztest_func_t ztest_spa_create_destroy;
250 ztest_func_t ztest_fault_inject;
251 ztest_func_t ztest_ddt_repair;
252 ztest_func_t ztest_dmu_snapshot_hold;
253 ztest_func_t ztest_spa_rename;
254 ztest_func_t ztest_scrub;
255 ztest_func_t ztest_dsl_dataset_promote_busy;
256 ztest_func_t ztest_vdev_attach_detach;
257 ztest_func_t ztest_vdev_LUN_growth;
258 ztest_func_t ztest_vdev_add_remove;
259 ztest_func_t ztest_vdev_aux_add_remove;
260 ztest_func_t ztest_split_pool;
262 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
263 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
264 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
265 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
266 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
268 ztest_info_t ztest_info[] = {
269 { ztest_dmu_read_write, 1, &zopt_always },
270 { ztest_dmu_write_parallel, 10, &zopt_always },
271 { ztest_dmu_object_alloc_free, 1, &zopt_always },
272 { ztest_dmu_commit_callbacks, 1, &zopt_always },
273 { ztest_zap, 30, &zopt_always },
274 { ztest_zap_parallel, 100, &zopt_always },
275 { ztest_split_pool, 1, &zopt_always },
276 { ztest_zil_commit, 1, &zopt_incessant },
277 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
278 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
279 { ztest_dsl_prop_get_set, 1, &zopt_often },
280 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
282 { ztest_dmu_prealloc, 1, &zopt_sometimes },
284 { ztest_fzap, 1, &zopt_sometimes },
285 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
286 { ztest_spa_create_destroy, 1, &zopt_sometimes },
287 { ztest_fault_inject, 1, &zopt_sometimes },
288 { ztest_ddt_repair, 1, &zopt_sometimes },
289 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
290 { ztest_spa_rename, 1, &zopt_rarely },
291 { ztest_scrub, 1, &zopt_rarely },
292 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
293 { ztest_vdev_attach_detach, 1, &zopt_rarely },
294 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
295 { ztest_vdev_add_remove, 1, &zopt_vdevtime },
296 { ztest_vdev_aux_add_remove, 1, &zopt_vdevtime },
299 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
302 * The following struct is used to hold a list of uncalled commit callbacks.
303 * The callbacks are ordered by txg number.
305 typedef struct ztest_cb_list {
306 kmutex_t zcl_callbacks_lock;
307 list_t zcl_callbacks;
311 * Stuff we need to share writably between parent and child.
313 typedef struct ztest_shared {
316 hrtime_t zs_proc_start;
317 hrtime_t zs_proc_stop;
318 hrtime_t zs_thread_start;
319 hrtime_t zs_thread_stop;
320 hrtime_t zs_thread_kill;
321 uint64_t zs_enospc_count;
322 uint64_t zs_vdev_next_leaf;
323 uint64_t zs_vdev_aux;
326 kmutex_t zs_vdev_lock;
327 krwlock_t zs_name_lock;
328 ztest_info_t zs_info[ZTEST_FUNCS];
334 #define ID_PARALLEL -1ULL
336 static char ztest_dev_template[] = "%s/%s.%llua";
337 static char ztest_aux_template[] = "%s/%s.%s.%llu";
338 ztest_shared_t *ztest_shared;
341 static int ztest_random_fd;
342 static int ztest_dump_core = 1;
344 static boolean_t ztest_exiting;
346 /* Global commit callback list */
347 static ztest_cb_list_t zcl;
348 /* Commit cb delay */
349 static uint64_t zc_min_txg_delay = UINT64_MAX;
350 static int zc_cb_counter = 0;
353 * Minimum number of commit callbacks that need to be registered for us to check
354 * whether the minimum txg delay is acceptable.
356 #define ZTEST_COMMIT_CB_MIN_REG 100
359 * If a number of txgs equal to this threshold have been created after a commit
360 * callback has been registered but not called, then we assume there is an
361 * implementation bug.
363 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
365 extern uint64_t metaslab_gang_bang;
366 extern uint64_t metaslab_df_alloc_threshold;
367 static uint64_t metaslab_sz;
370 ZTEST_META_DNODE = 0,
375 static void usage(boolean_t) __NORETURN;
378 * These libumem hooks provide a reasonable set of defaults for the allocator's
379 * debugging facilities.
382 _umem_debug_init(void)
384 return ("default,verbose"); /* $UMEM_DEBUG setting */
388 _umem_logging_init(void)
390 return ("fail,contents"); /* $UMEM_LOGGING setting */
393 #define FATAL_MSG_SZ 1024
398 fatal(int do_perror, char *message, ...)
401 int save_errno = errno;
404 (void) fflush(stdout);
405 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
407 va_start(args, message);
408 (void) sprintf(buf, "ztest: ");
410 (void) vsprintf(buf + strlen(buf), message, args);
413 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
414 ": %s", strerror(save_errno));
416 (void) fprintf(stderr, "%s\n", buf);
417 fatal_msg = buf; /* to ease debugging */
424 str2shift(const char *buf)
426 const char *ends = "BKMGTPEZ";
431 for (i = 0; i < strlen(ends); i++) {
432 if (toupper(buf[0]) == ends[i])
435 if (i == strlen(ends)) {
436 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
440 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
443 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
449 nicenumtoull(const char *buf)
454 val = strtoull(buf, &end, 0);
456 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
458 } else if (end[0] == '.') {
459 double fval = strtod(buf, &end);
460 fval *= pow(2, str2shift(end));
461 if (fval > UINT64_MAX) {
462 (void) fprintf(stderr, "ztest: value too large: %s\n",
466 val = (uint64_t)fval;
468 int shift = str2shift(end);
469 if (shift >= 64 || (val << shift) >> shift != val) {
470 (void) fprintf(stderr, "ztest: value too large: %s\n",
480 usage(boolean_t requested)
482 char nice_vdev_size[10];
483 char nice_gang_bang[10];
484 FILE *fp = requested ? stdout : stderr;
486 nicenum(zopt_vdev_size, nice_vdev_size);
487 nicenum(metaslab_gang_bang, nice_gang_bang);
489 (void) fprintf(fp, "Usage: %s\n"
490 "\t[-v vdevs (default: %llu)]\n"
491 "\t[-s size_of_each_vdev (default: %s)]\n"
492 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
493 "\t[-m mirror_copies (default: %d)]\n"
494 "\t[-r raidz_disks (default: %d)]\n"
495 "\t[-R raidz_parity (default: %d)]\n"
496 "\t[-d datasets (default: %d)]\n"
497 "\t[-t threads (default: %d)]\n"
498 "\t[-g gang_block_threshold (default: %s)]\n"
499 "\t[-i init_count (default: %d)] initialize pool i times\n"
500 "\t[-k kill_percentage (default: %llu%%)]\n"
501 "\t[-p pool_name (default: %s)]\n"
502 "\t[-f dir (default: %s)] file directory for vdev files\n"
503 "\t[-V] verbose (use multiple times for ever more blather)\n"
504 "\t[-E] use existing pool instead of creating new one\n"
505 "\t[-T time (default: %llu sec)] total run time\n"
506 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
507 "\t[-P passtime (default: %llu sec)] time per pass\n"
508 "\t[-h] (print help)\n"
511 (u_longlong_t)zopt_vdevs, /* -v */
512 nice_vdev_size, /* -s */
513 zopt_ashift, /* -a */
514 zopt_mirrors, /* -m */
516 zopt_raidz_parity, /* -R */
517 zopt_datasets, /* -d */
518 zopt_threads, /* -t */
519 nice_gang_bang, /* -g */
521 (u_longlong_t)zopt_killrate, /* -k */
524 (u_longlong_t)zopt_time, /* -T */
525 (u_longlong_t)zopt_maxloops, /* -F */
526 (u_longlong_t)zopt_passtime); /* -P */
527 exit(requested ? 0 : 1);
531 process_options(int argc, char **argv)
536 /* By default, test gang blocks for blocks 32K and greater */
537 metaslab_gang_bang = 32 << 10;
539 while ((opt = getopt(argc, argv,
540 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:")) != EOF) {
557 value = nicenumtoull(optarg);
564 zopt_vdev_size = MAX(SPA_MINDEVSIZE, value);
570 zopt_mirrors = value;
573 zopt_raidz = MAX(1, value);
576 zopt_raidz_parity = MIN(MAX(value, 1), 3);
579 zopt_datasets = MAX(1, value);
582 zopt_threads = MAX(1, value);
585 metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1, value);
591 zopt_killrate = value;
594 zopt_pool = strdup(optarg);
597 zopt_dir = strdup(optarg);
609 zopt_passtime = MAX(1, value);
612 zopt_maxloops = MAX(1, value);
624 zopt_raidz_parity = MIN(zopt_raidz_parity, zopt_raidz - 1);
626 zopt_vdevtime = (zopt_vdevs > 0 ? zopt_time * NANOSEC / zopt_vdevs :
631 ztest_kill(ztest_shared_t *zs)
633 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(zs->zs_spa));
634 zs->zs_space = metaslab_class_get_space(spa_normal_class(zs->zs_spa));
635 (void) kill(getpid(), SIGKILL);
639 ztest_random(uint64_t range)
646 if (read(ztest_random_fd, &r, sizeof (r)) != sizeof (r))
647 fatal(1, "short read from /dev/urandom");
654 ztest_record_enospc(const char *s)
656 ztest_shared->zs_enospc_count++;
660 ztest_get_ashift(void)
662 if (zopt_ashift == 0)
663 return (SPA_MINBLOCKSHIFT + ztest_random(3));
664 return (zopt_ashift);
668 make_vdev_file(char *path, char *aux, size_t size, uint64_t ashift)
674 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
677 ashift = ztest_get_ashift();
683 vdev = ztest_shared->zs_vdev_aux;
684 (void) sprintf(path, ztest_aux_template,
685 zopt_dir, zopt_pool, aux, vdev);
687 vdev = ztest_shared->zs_vdev_next_leaf++;
688 (void) sprintf(path, ztest_dev_template,
689 zopt_dir, zopt_pool, vdev);
694 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
696 fatal(1, "can't open %s", path);
697 if (ftruncate(fd, size) != 0)
698 fatal(1, "can't ftruncate %s", path);
702 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
703 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
704 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
705 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
706 umem_free(pathbuf, MAXPATHLEN);
712 make_vdev_raidz(char *path, char *aux, size_t size, uint64_t ashift, int r)
714 nvlist_t *raidz, **child;
718 return (make_vdev_file(path, aux, size, ashift));
719 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
721 for (c = 0; c < r; c++)
722 child[c] = make_vdev_file(path, aux, size, ashift);
724 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
725 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
726 VDEV_TYPE_RAIDZ) == 0);
727 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
728 zopt_raidz_parity) == 0);
729 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
732 for (c = 0; c < r; c++)
733 nvlist_free(child[c]);
735 umem_free(child, r * sizeof (nvlist_t *));
741 make_vdev_mirror(char *path, char *aux, size_t size, uint64_t ashift,
744 nvlist_t *mirror, **child;
748 return (make_vdev_raidz(path, aux, size, ashift, r));
750 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
752 for (c = 0; c < m; c++)
753 child[c] = make_vdev_raidz(path, aux, size, ashift, r);
755 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
756 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
757 VDEV_TYPE_MIRROR) == 0);
758 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
761 for (c = 0; c < m; c++)
762 nvlist_free(child[c]);
764 umem_free(child, m * sizeof (nvlist_t *));
770 make_vdev_root(char *path, char *aux, size_t size, uint64_t ashift,
771 int log, int r, int m, int t)
773 nvlist_t *root, **child;
778 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
780 for (c = 0; c < t; c++) {
781 child[c] = make_vdev_mirror(path, aux, size, ashift, r, m);
782 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
786 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
787 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
788 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
791 for (c = 0; c < t; c++)
792 nvlist_free(child[c]);
794 umem_free(child, t * sizeof (nvlist_t *));
800 ztest_random_blocksize(void)
802 return (1 << (SPA_MINBLOCKSHIFT +
803 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
807 ztest_random_ibshift(void)
809 return (DN_MIN_INDBLKSHIFT +
810 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
814 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
817 vdev_t *rvd = spa->spa_root_vdev;
820 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
823 top = ztest_random(rvd->vdev_children);
824 tvd = rvd->vdev_child[top];
825 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
826 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
832 ztest_random_dsl_prop(zfs_prop_t prop)
837 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
838 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
844 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
847 const char *propname = zfs_prop_to_name(prop);
853 error = dsl_prop_set(osname, propname,
854 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL),
855 sizeof (value), 1, &value);
857 if (error == ENOSPC) {
858 ztest_record_enospc(FTAG);
861 ASSERT3U(error, ==, 0);
863 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
864 VERIFY3U(dsl_prop_get(osname, propname, sizeof (curval),
865 1, &curval, setpoint), ==, 0);
867 if (zopt_verbose >= 6) {
868 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
869 (void) printf("%s %s = %s at '%s'\n",
870 osname, propname, valname, setpoint);
872 umem_free(setpoint, MAXPATHLEN);
878 ztest_spa_prop_set_uint64(ztest_shared_t *zs, zpool_prop_t prop, uint64_t value)
880 spa_t *spa = zs->zs_spa;
881 nvlist_t *props = NULL;
884 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
885 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
887 error = spa_prop_set(spa, props);
891 if (error == ENOSPC) {
892 ztest_record_enospc(FTAG);
895 ASSERT3U(error, ==, 0);
901 ztest_rll_init(rll_t *rll)
903 rll->rll_writer = NULL;
904 rll->rll_readers = 0;
905 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
906 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
910 ztest_rll_destroy(rll_t *rll)
912 ASSERT(rll->rll_writer == NULL);
913 ASSERT(rll->rll_readers == 0);
914 mutex_destroy(&rll->rll_lock);
915 cv_destroy(&rll->rll_cv);
919 ztest_rll_lock(rll_t *rll, rl_type_t type)
921 mutex_enter(&rll->rll_lock);
923 if (type == RL_READER) {
924 while (rll->rll_writer != NULL)
925 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
928 while (rll->rll_writer != NULL || rll->rll_readers)
929 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
930 rll->rll_writer = curthread;
933 mutex_exit(&rll->rll_lock);
937 ztest_rll_unlock(rll_t *rll)
939 mutex_enter(&rll->rll_lock);
941 if (rll->rll_writer) {
942 ASSERT(rll->rll_readers == 0);
943 rll->rll_writer = NULL;
945 ASSERT(rll->rll_readers != 0);
946 ASSERT(rll->rll_writer == NULL);
950 if (rll->rll_writer == NULL && rll->rll_readers == 0)
951 cv_broadcast(&rll->rll_cv);
953 mutex_exit(&rll->rll_lock);
957 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
959 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
961 ztest_rll_lock(rll, type);
965 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
967 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
969 ztest_rll_unlock(rll);
973 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
974 uint64_t size, rl_type_t type)
976 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
977 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
980 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
981 rl->rl_object = object;
982 rl->rl_offset = offset;
986 ztest_rll_lock(rll, type);
992 ztest_range_unlock(rl_t *rl)
994 rll_t *rll = rl->rl_lock;
996 ztest_rll_unlock(rll);
998 umem_free(rl, sizeof (*rl));
1002 ztest_zd_init(ztest_ds_t *zd, objset_t *os)
1005 zd->zd_zilog = dmu_objset_zil(os);
1007 dmu_objset_name(os, zd->zd_name);
1010 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1012 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1013 ztest_rll_init(&zd->zd_object_lock[l]);
1015 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1016 ztest_rll_init(&zd->zd_range_lock[l]);
1020 ztest_zd_fini(ztest_ds_t *zd)
1024 mutex_destroy(&zd->zd_dirobj_lock);
1026 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1027 ztest_rll_destroy(&zd->zd_object_lock[l]);
1029 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1030 ztest_rll_destroy(&zd->zd_range_lock[l]);
1033 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1036 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1042 * Attempt to assign tx to some transaction group.
1044 error = dmu_tx_assign(tx, txg_how);
1046 if (error == ERESTART) {
1047 ASSERT(txg_how == TXG_NOWAIT);
1050 ASSERT3U(error, ==, ENOSPC);
1051 ztest_record_enospc(tag);
1056 txg = dmu_tx_get_txg(tx);
1062 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1065 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1073 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1076 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1080 diff |= (value - *ip++);
1087 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1088 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1090 bt->bt_magic = BT_MAGIC;
1091 bt->bt_objset = dmu_objset_id(os);
1092 bt->bt_object = object;
1093 bt->bt_offset = offset;
1096 bt->bt_crtxg = crtxg;
1100 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1101 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1103 ASSERT(bt->bt_magic == BT_MAGIC);
1104 ASSERT(bt->bt_objset == dmu_objset_id(os));
1105 ASSERT(bt->bt_object == object);
1106 ASSERT(bt->bt_offset == offset);
1107 ASSERT(bt->bt_gen <= gen);
1108 ASSERT(bt->bt_txg <= txg);
1109 ASSERT(bt->bt_crtxg == crtxg);
1112 static ztest_block_tag_t *
1113 ztest_bt_bonus(dmu_buf_t *db)
1115 dmu_object_info_t doi;
1116 ztest_block_tag_t *bt;
1118 dmu_object_info_from_db(db, &doi);
1119 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1120 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1121 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1130 #define lrz_type lr_mode
1131 #define lrz_blocksize lr_uid
1132 #define lrz_ibshift lr_gid
1133 #define lrz_bonustype lr_rdev
1134 #define lrz_bonuslen lr_crtime[1]
1137 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1139 char *name = (void *)(lr + 1); /* name follows lr */
1140 size_t namesize = strlen(name) + 1;
1143 if (zil_replaying(zd->zd_zilog, tx))
1146 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1147 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1148 sizeof (*lr) + namesize - sizeof (lr_t));
1150 zil_itx_assign(zd->zd_zilog, itx, tx);
1154 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1156 char *name = (void *)(lr + 1); /* name follows lr */
1157 size_t namesize = strlen(name) + 1;
1160 if (zil_replaying(zd->zd_zilog, tx))
1163 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1164 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1165 sizeof (*lr) + namesize - sizeof (lr_t));
1167 itx->itx_oid = object;
1168 zil_itx_assign(zd->zd_zilog, itx, tx);
1172 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1175 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1177 if (zil_replaying(zd->zd_zilog, tx))
1180 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1181 write_state = WR_INDIRECT;
1183 itx = zil_itx_create(TX_WRITE,
1184 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1186 if (write_state == WR_COPIED &&
1187 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1188 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1189 zil_itx_destroy(itx);
1190 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1191 write_state = WR_NEED_COPY;
1193 itx->itx_private = zd;
1194 itx->itx_wr_state = write_state;
1195 itx->itx_sync = (ztest_random(8) == 0);
1196 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1198 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1199 sizeof (*lr) - sizeof (lr_t));
1201 zil_itx_assign(zd->zd_zilog, itx, tx);
1205 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1209 if (zil_replaying(zd->zd_zilog, tx))
1212 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1213 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1214 sizeof (*lr) - sizeof (lr_t));
1216 itx->itx_sync = B_FALSE;
1217 zil_itx_assign(zd->zd_zilog, itx, tx);
1221 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1225 if (zil_replaying(zd->zd_zilog, tx))
1228 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1229 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1230 sizeof (*lr) - sizeof (lr_t));
1232 itx->itx_sync = B_FALSE;
1233 zil_itx_assign(zd->zd_zilog, itx, tx);
1240 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1242 char *name = (void *)(lr + 1); /* name follows lr */
1243 objset_t *os = zd->zd_os;
1244 ztest_block_tag_t *bbt;
1251 byteswap_uint64_array(lr, sizeof (*lr));
1253 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1254 ASSERT(name[0] != '\0');
1256 tx = dmu_tx_create(os);
1258 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1260 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1261 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1263 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1266 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1270 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1272 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1273 if (lr->lr_foid == 0) {
1274 lr->lr_foid = zap_create(os,
1275 lr->lrz_type, lr->lrz_bonustype,
1276 lr->lrz_bonuslen, tx);
1278 error = zap_create_claim(os, lr->lr_foid,
1279 lr->lrz_type, lr->lrz_bonustype,
1280 lr->lrz_bonuslen, tx);
1283 if (lr->lr_foid == 0) {
1284 lr->lr_foid = dmu_object_alloc(os,
1285 lr->lrz_type, 0, lr->lrz_bonustype,
1286 lr->lrz_bonuslen, tx);
1288 error = dmu_object_claim(os, lr->lr_foid,
1289 lr->lrz_type, 0, lr->lrz_bonustype,
1290 lr->lrz_bonuslen, tx);
1295 ASSERT3U(error, ==, EEXIST);
1296 ASSERT(zd->zd_zilog->zl_replay);
1301 ASSERT(lr->lr_foid != 0);
1303 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1304 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1305 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1307 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1308 bbt = ztest_bt_bonus(db);
1309 dmu_buf_will_dirty(db, tx);
1310 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1311 dmu_buf_rele(db, FTAG);
1313 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1316 (void) ztest_log_create(zd, tx, lr);
1324 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1326 char *name = (void *)(lr + 1); /* name follows lr */
1327 objset_t *os = zd->zd_os;
1328 dmu_object_info_t doi;
1330 uint64_t object, txg;
1333 byteswap_uint64_array(lr, sizeof (*lr));
1335 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1336 ASSERT(name[0] != '\0');
1339 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1340 ASSERT(object != 0);
1342 ztest_object_lock(zd, object, RL_WRITER);
1344 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1346 tx = dmu_tx_create(os);
1348 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1349 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1351 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1353 ztest_object_unlock(zd, object);
1357 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1358 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1360 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1363 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1365 (void) ztest_log_remove(zd, tx, lr, object);
1369 ztest_object_unlock(zd, object);
1375 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1377 objset_t *os = zd->zd_os;
1378 void *data = lr + 1; /* data follows lr */
1379 uint64_t offset, length;
1380 ztest_block_tag_t *bt = data;
1381 ztest_block_tag_t *bbt;
1382 uint64_t gen, txg, lrtxg, crtxg;
1383 dmu_object_info_t doi;
1386 arc_buf_t *abuf = NULL;
1390 byteswap_uint64_array(lr, sizeof (*lr));
1392 offset = lr->lr_offset;
1393 length = lr->lr_length;
1395 /* If it's a dmu_sync() block, write the whole block */
1396 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1397 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1398 if (length < blocksize) {
1399 offset -= offset % blocksize;
1404 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1405 byteswap_uint64_array(bt, sizeof (*bt));
1407 if (bt->bt_magic != BT_MAGIC)
1410 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1411 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1413 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1415 dmu_object_info_from_db(db, &doi);
1417 bbt = ztest_bt_bonus(db);
1418 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1420 crtxg = bbt->bt_crtxg;
1421 lrtxg = lr->lr_common.lrc_txg;
1423 tx = dmu_tx_create(os);
1425 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1427 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1428 P2PHASE(offset, length) == 0)
1429 abuf = dmu_request_arcbuf(db, length);
1431 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1434 dmu_return_arcbuf(abuf);
1435 dmu_buf_rele(db, FTAG);
1436 ztest_range_unlock(rl);
1437 ztest_object_unlock(zd, lr->lr_foid);
1443 * Usually, verify the old data before writing new data --
1444 * but not always, because we also want to verify correct
1445 * behavior when the data was not recently read into cache.
1447 ASSERT(offset % doi.doi_data_block_size == 0);
1448 if (ztest_random(4) != 0) {
1449 int prefetch = ztest_random(2) ?
1450 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1451 ztest_block_tag_t rbt;
1453 VERIFY(dmu_read(os, lr->lr_foid, offset,
1454 sizeof (rbt), &rbt, prefetch) == 0);
1455 if (rbt.bt_magic == BT_MAGIC) {
1456 ztest_bt_verify(&rbt, os, lr->lr_foid,
1457 offset, gen, txg, crtxg);
1462 * Writes can appear to be newer than the bonus buffer because
1463 * the ztest_get_data() callback does a dmu_read() of the
1464 * open-context data, which may be different than the data
1465 * as it was when the write was generated.
1467 if (zd->zd_zilog->zl_replay) {
1468 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1469 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1474 * Set the bt's gen/txg to the bonus buffer's gen/txg
1475 * so that all of the usual ASSERTs will work.
1477 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1481 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1483 bcopy(data, abuf->b_data, length);
1484 dmu_assign_arcbuf(db, offset, abuf, tx);
1487 (void) ztest_log_write(zd, tx, lr);
1489 dmu_buf_rele(db, FTAG);
1493 ztest_range_unlock(rl);
1494 ztest_object_unlock(zd, lr->lr_foid);
1500 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1502 objset_t *os = zd->zd_os;
1508 byteswap_uint64_array(lr, sizeof (*lr));
1510 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1511 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1514 tx = dmu_tx_create(os);
1516 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1518 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1520 ztest_range_unlock(rl);
1521 ztest_object_unlock(zd, lr->lr_foid);
1525 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1526 lr->lr_length, tx) == 0);
1528 (void) ztest_log_truncate(zd, tx, lr);
1532 ztest_range_unlock(rl);
1533 ztest_object_unlock(zd, lr->lr_foid);
1539 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1541 objset_t *os = zd->zd_os;
1544 ztest_block_tag_t *bbt;
1545 uint64_t txg, lrtxg, crtxg;
1548 byteswap_uint64_array(lr, sizeof (*lr));
1550 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1552 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1554 tx = dmu_tx_create(os);
1555 dmu_tx_hold_bonus(tx, lr->lr_foid);
1557 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1559 dmu_buf_rele(db, FTAG);
1560 ztest_object_unlock(zd, lr->lr_foid);
1564 bbt = ztest_bt_bonus(db);
1565 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1566 crtxg = bbt->bt_crtxg;
1567 lrtxg = lr->lr_common.lrc_txg;
1569 if (zd->zd_zilog->zl_replay) {
1570 ASSERT(lr->lr_size != 0);
1571 ASSERT(lr->lr_mode != 0);
1575 * Randomly change the size and increment the generation.
1577 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1579 lr->lr_mode = bbt->bt_gen + 1;
1584 * Verify that the current bonus buffer is not newer than our txg.
1586 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1587 MAX(txg, lrtxg), crtxg);
1589 dmu_buf_will_dirty(db, tx);
1591 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1592 ASSERT3U(lr->lr_size, <=, db->db_size);
1593 VERIFY3U(dmu_set_bonus(db, lr->lr_size, tx), ==, 0);
1594 bbt = ztest_bt_bonus(db);
1596 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1598 dmu_buf_rele(db, FTAG);
1600 (void) ztest_log_setattr(zd, tx, lr);
1604 ztest_object_unlock(zd, lr->lr_foid);
1609 zil_replay_func_t *ztest_replay_vector[TX_MAX_TYPE] = {
1610 NULL, /* 0 no such transaction type */
1611 (zil_replay_func_t *)ztest_replay_create, /* TX_CREATE */
1612 NULL, /* TX_MKDIR */
1613 NULL, /* TX_MKXATTR */
1614 NULL, /* TX_SYMLINK */
1615 (zil_replay_func_t *)ztest_replay_remove, /* TX_REMOVE */
1616 NULL, /* TX_RMDIR */
1618 NULL, /* TX_RENAME */
1619 (zil_replay_func_t *)ztest_replay_write, /* TX_WRITE */
1620 (zil_replay_func_t *)ztest_replay_truncate, /* TX_TRUNCATE */
1621 (zil_replay_func_t *)ztest_replay_setattr, /* TX_SETATTR */
1623 NULL, /* TX_CREATE_ACL */
1624 NULL, /* TX_CREATE_ATTR */
1625 NULL, /* TX_CREATE_ACL_ATTR */
1626 NULL, /* TX_MKDIR_ACL */
1627 NULL, /* TX_MKDIR_ATTR */
1628 NULL, /* TX_MKDIR_ACL_ATTR */
1629 NULL, /* TX_WRITE2 */
1633 * ZIL get_data callbacks
1637 ztest_get_done(zgd_t *zgd, int error)
1639 ztest_ds_t *zd = zgd->zgd_private;
1640 uint64_t object = zgd->zgd_rl->rl_object;
1643 dmu_buf_rele(zgd->zgd_db, zgd);
1645 ztest_range_unlock(zgd->zgd_rl);
1646 ztest_object_unlock(zd, object);
1648 if (error == 0 && zgd->zgd_bp)
1649 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1651 umem_free(zgd, sizeof (*zgd));
1655 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1657 ztest_ds_t *zd = arg;
1658 objset_t *os = zd->zd_os;
1659 uint64_t object = lr->lr_foid;
1660 uint64_t offset = lr->lr_offset;
1661 uint64_t size = lr->lr_length;
1662 blkptr_t *bp = &lr->lr_blkptr;
1663 uint64_t txg = lr->lr_common.lrc_txg;
1665 dmu_object_info_t doi;
1670 ztest_object_lock(zd, object, RL_READER);
1671 error = dmu_bonus_hold(os, object, FTAG, &db);
1673 ztest_object_unlock(zd, object);
1677 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1679 if (crtxg == 0 || crtxg > txg) {
1680 dmu_buf_rele(db, FTAG);
1681 ztest_object_unlock(zd, object);
1685 dmu_object_info_from_db(db, &doi);
1686 dmu_buf_rele(db, FTAG);
1689 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1690 zgd->zgd_zilog = zd->zd_zilog;
1691 zgd->zgd_private = zd;
1693 if (buf != NULL) { /* immediate write */
1694 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1697 error = dmu_read(os, object, offset, size, buf,
1698 DMU_READ_NO_PREFETCH);
1701 size = doi.doi_data_block_size;
1703 offset = P2ALIGN(offset, size);
1705 ASSERT(offset < size);
1709 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1712 error = dmu_buf_hold(os, object, offset, zgd, &db,
1713 DMU_READ_NO_PREFETCH);
1719 ASSERT(db->db_offset == offset);
1720 ASSERT(db->db_size == size);
1722 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1723 ztest_get_done, zgd);
1730 ztest_get_done(zgd, error);
1736 ztest_lr_alloc(size_t lrsize, char *name)
1739 size_t namesize = name ? strlen(name) + 1 : 0;
1741 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1744 bcopy(name, lr + lrsize, namesize);
1750 ztest_lr_free(void *lr, size_t lrsize, char *name)
1752 size_t namesize = name ? strlen(name) + 1 : 0;
1754 umem_free(lr, lrsize + namesize);
1758 * Lookup a bunch of objects. Returns the number of objects not found.
1761 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1767 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1769 for (i = 0; i < count; i++, od++) {
1771 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1772 sizeof (uint64_t), 1, &od->od_object);
1774 ASSERT(error == ENOENT);
1775 ASSERT(od->od_object == 0);
1779 ztest_block_tag_t *bbt;
1780 dmu_object_info_t doi;
1782 ASSERT(od->od_object != 0);
1783 ASSERT(missing == 0); /* there should be no gaps */
1785 ztest_object_lock(zd, od->od_object, RL_READER);
1786 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1787 od->od_object, FTAG, &db));
1788 dmu_object_info_from_db(db, &doi);
1789 bbt = ztest_bt_bonus(db);
1790 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1791 od->od_type = doi.doi_type;
1792 od->od_blocksize = doi.doi_data_block_size;
1793 od->od_gen = bbt->bt_gen;
1794 dmu_buf_rele(db, FTAG);
1795 ztest_object_unlock(zd, od->od_object);
1803 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
1808 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1810 for (i = 0; i < count; i++, od++) {
1817 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1819 lr->lr_doid = od->od_dir;
1820 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
1821 lr->lrz_type = od->od_crtype;
1822 lr->lrz_blocksize = od->od_crblocksize;
1823 lr->lrz_ibshift = ztest_random_ibshift();
1824 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
1825 lr->lrz_bonuslen = dmu_bonus_max();
1826 lr->lr_gen = od->od_crgen;
1827 lr->lr_crtime[0] = time(NULL);
1829 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
1830 ASSERT(missing == 0);
1834 od->od_object = lr->lr_foid;
1835 od->od_type = od->od_crtype;
1836 od->od_blocksize = od->od_crblocksize;
1837 od->od_gen = od->od_crgen;
1838 ASSERT(od->od_object != 0);
1841 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1848 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
1854 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1858 for (i = count - 1; i >= 0; i--, od--) {
1864 if (od->od_object == 0)
1867 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
1869 lr->lr_doid = od->od_dir;
1871 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
1872 ASSERT3U(error, ==, ENOSPC);
1877 ztest_lr_free(lr, sizeof (*lr), od->od_name);
1884 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
1890 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
1892 lr->lr_foid = object;
1893 lr->lr_offset = offset;
1894 lr->lr_length = size;
1896 BP_ZERO(&lr->lr_blkptr);
1898 bcopy(data, lr + 1, size);
1900 error = ztest_replay_write(zd, lr, B_FALSE);
1902 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
1908 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1913 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1915 lr->lr_foid = object;
1916 lr->lr_offset = offset;
1917 lr->lr_length = size;
1919 error = ztest_replay_truncate(zd, lr, B_FALSE);
1921 ztest_lr_free(lr, sizeof (*lr), NULL);
1927 ztest_setattr(ztest_ds_t *zd, uint64_t object)
1932 lr = ztest_lr_alloc(sizeof (*lr), NULL);
1934 lr->lr_foid = object;
1938 error = ztest_replay_setattr(zd, lr, B_FALSE);
1940 ztest_lr_free(lr, sizeof (*lr), NULL);
1946 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
1948 objset_t *os = zd->zd_os;
1953 txg_wait_synced(dmu_objset_pool(os), 0);
1955 ztest_object_lock(zd, object, RL_READER);
1956 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
1958 tx = dmu_tx_create(os);
1960 dmu_tx_hold_write(tx, object, offset, size);
1962 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1965 dmu_prealloc(os, object, offset, size, tx);
1967 txg_wait_synced(dmu_objset_pool(os), txg);
1969 (void) dmu_free_long_range(os, object, offset, size);
1972 ztest_range_unlock(rl);
1973 ztest_object_unlock(zd, object);
1977 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
1979 ztest_block_tag_t wbt;
1980 dmu_object_info_t doi;
1981 enum ztest_io_type io_type;
1985 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
1986 blocksize = doi.doi_data_block_size;
1987 data = umem_alloc(blocksize, UMEM_NOFAIL);
1990 * Pick an i/o type at random, biased toward writing block tags.
1992 io_type = ztest_random(ZTEST_IO_TYPES);
1993 if (ztest_random(2) == 0)
1994 io_type = ZTEST_IO_WRITE_TAG;
1998 case ZTEST_IO_WRITE_TAG:
1999 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2000 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2003 case ZTEST_IO_WRITE_PATTERN:
2004 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2005 if (ztest_random(2) == 0) {
2007 * Induce fletcher2 collisions to ensure that
2008 * zio_ddt_collision() detects and resolves them
2009 * when using fletcher2-verify for deduplication.
2011 ((uint64_t *)data)[0] ^= 1ULL << 63;
2012 ((uint64_t *)data)[4] ^= 1ULL << 63;
2014 (void) ztest_write(zd, object, offset, blocksize, data);
2017 case ZTEST_IO_WRITE_ZEROES:
2018 bzero(data, blocksize);
2019 (void) ztest_write(zd, object, offset, blocksize, data);
2022 case ZTEST_IO_TRUNCATE:
2023 (void) ztest_truncate(zd, object, offset, blocksize);
2026 case ZTEST_IO_SETATTR:
2027 (void) ztest_setattr(zd, object);
2033 umem_free(data, blocksize);
2037 * Initialize an object description template.
2040 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2041 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2043 od->od_dir = ZTEST_DIROBJ;
2046 od->od_crtype = type;
2047 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2050 od->od_type = DMU_OT_NONE;
2051 od->od_blocksize = 0;
2054 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2055 tag, (longlong_t)id, (u_longlong_t)index);
2059 * Lookup or create the objects for a test using the od template.
2060 * If the objects do not all exist, or if 'remove' is specified,
2061 * remove any existing objects and create new ones. Otherwise,
2062 * use the existing objects.
2065 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2067 int count = size / sizeof (*od);
2070 mutex_enter(&zd->zd_dirobj_lock);
2071 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2072 (ztest_remove(zd, od, count) != 0 ||
2073 ztest_create(zd, od, count) != 0))
2076 mutex_exit(&zd->zd_dirobj_lock);
2083 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2085 zilog_t *zilog = zd->zd_zilog;
2087 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2090 * Remember the committed values in zd, which is in parent/child
2091 * shared memory. If we die, the next iteration of ztest_run()
2092 * will verify that the log really does contain this record.
2094 mutex_enter(&zilog->zl_lock);
2095 ASSERT(zd->zd_seq <= zilog->zl_commit_lr_seq);
2096 zd->zd_seq = zilog->zl_commit_lr_seq;
2097 mutex_exit(&zilog->zl_lock);
2101 * Verify that we can't destroy an active pool, create an existing pool,
2102 * or create a pool with a bad vdev spec.
2106 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2108 ztest_shared_t *zs = ztest_shared;
2113 * Attempt to create using a bad file.
2115 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2116 VERIFY3U(ENOENT, ==,
2117 spa_create("ztest_bad_file", nvroot, NULL, NULL, NULL));
2118 nvlist_free(nvroot);
2121 * Attempt to create using a bad mirror.
2123 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 2, 1);
2124 VERIFY3U(ENOENT, ==,
2125 spa_create("ztest_bad_mirror", nvroot, NULL, NULL, NULL));
2126 nvlist_free(nvroot);
2129 * Attempt to create an existing pool. It shouldn't matter
2130 * what's in the nvroot; we should fail with EEXIST.
2132 (void) rw_enter(&zs->zs_name_lock, RW_READER);
2133 nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
2134 VERIFY3U(EEXIST, ==, spa_create(zs->zs_pool, nvroot, NULL, NULL, NULL));
2135 nvlist_free(nvroot);
2136 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
2137 VERIFY3U(EBUSY, ==, spa_destroy(zs->zs_pool));
2138 spa_close(spa, FTAG);
2140 (void) rw_exit(&zs->zs_name_lock);
2144 vdev_lookup_by_path(vdev_t *vd, const char *path)
2149 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2152 for (c = 0; c < vd->vdev_children; c++)
2153 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2161 * Find the first available hole which can be used as a top-level.
2164 find_vdev_hole(spa_t *spa)
2166 vdev_t *rvd = spa->spa_root_vdev;
2169 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2171 for (c = 0; c < rvd->vdev_children; c++) {
2172 vdev_t *cvd = rvd->vdev_child[c];
2174 if (cvd->vdev_ishole)
2181 * Verify that vdev_add() works as expected.
2185 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2187 ztest_shared_t *zs = ztest_shared;
2188 spa_t *spa = zs->zs_spa;
2194 mutex_enter(&zs->zs_vdev_lock);
2195 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * zopt_raidz;
2197 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2199 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2202 * If we have slogs then remove them 1/4 of the time.
2204 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2206 * Grab the guid from the head of the log class rotor.
2208 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2210 spa_config_exit(spa, SCL_VDEV, FTAG);
2213 * We have to grab the zs_name_lock as writer to
2214 * prevent a race between removing a slog (dmu_objset_find)
2215 * and destroying a dataset. Removing the slog will
2216 * grab a reference on the dataset which may cause
2217 * dmu_objset_destroy() to fail with EBUSY thus
2218 * leaving the dataset in an inconsistent state.
2220 rw_enter(&ztest_shared->zs_name_lock, RW_WRITER);
2221 error = spa_vdev_remove(spa, guid, B_FALSE);
2222 rw_exit(&ztest_shared->zs_name_lock);
2224 if (error && error != EEXIST)
2225 fatal(0, "spa_vdev_remove() = %d", error);
2227 spa_config_exit(spa, SCL_VDEV, FTAG);
2230 * Make 1/4 of the devices be log devices.
2232 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
2233 ztest_random(4) == 0, zopt_raidz, zs->zs_mirrors, 1);
2235 error = spa_vdev_add(spa, nvroot);
2236 nvlist_free(nvroot);
2238 if (error == ENOSPC)
2239 ztest_record_enospc("spa_vdev_add");
2240 else if (error != 0)
2241 fatal(0, "spa_vdev_add() = %d", error);
2244 mutex_exit(&ztest_shared->zs_vdev_lock);
2248 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2252 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2254 ztest_shared_t *zs = ztest_shared;
2255 spa_t *spa = zs->zs_spa;
2256 vdev_t *rvd = spa->spa_root_vdev;
2257 spa_aux_vdev_t *sav;
2263 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2265 if (ztest_random(2) == 0) {
2266 sav = &spa->spa_spares;
2267 aux = ZPOOL_CONFIG_SPARES;
2269 sav = &spa->spa_l2cache;
2270 aux = ZPOOL_CONFIG_L2CACHE;
2273 mutex_enter(&zs->zs_vdev_lock);
2275 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2277 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2279 * Pick a random device to remove.
2281 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2284 * Find an unused device we can add.
2286 zs->zs_vdev_aux = 0;
2289 (void) sprintf(path, ztest_aux_template, zopt_dir,
2290 zopt_pool, aux, zs->zs_vdev_aux);
2291 for (c = 0; c < sav->sav_count; c++)
2292 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2295 if (c == sav->sav_count &&
2296 vdev_lookup_by_path(rvd, path) == NULL)
2302 spa_config_exit(spa, SCL_VDEV, FTAG);
2308 nvlist_t *nvroot = make_vdev_root(NULL, aux,
2309 (zopt_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2310 error = spa_vdev_add(spa, nvroot);
2312 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2313 nvlist_free(nvroot);
2316 * Remove an existing device. Sometimes, dirty its
2317 * vdev state first to make sure we handle removal
2318 * of devices that have pending state changes.
2320 if (ztest_random(2) == 0)
2321 (void) vdev_online(spa, guid, 0, NULL);
2323 error = spa_vdev_remove(spa, guid, B_FALSE);
2324 if (error != 0 && error != EBUSY)
2325 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2328 mutex_exit(&zs->zs_vdev_lock);
2330 umem_free(path, MAXPATHLEN);
2334 * split a pool if it has mirror tlvdevs
2338 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2340 ztest_shared_t *zs = ztest_shared;
2341 spa_t *spa = zs->zs_spa;
2342 vdev_t *rvd = spa->spa_root_vdev;
2343 nvlist_t *tree, **child, *config, *split, **schild;
2344 uint_t c, children, schildren = 0, lastlogid = 0;
2347 mutex_enter(&zs->zs_vdev_lock);
2349 /* ensure we have a useable config; mirrors of raidz aren't supported */
2350 if (zs->zs_mirrors < 3 || zopt_raidz > 1) {
2351 mutex_exit(&zs->zs_vdev_lock);
2355 /* clean up the old pool, if any */
2356 (void) spa_destroy("splitp");
2358 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2360 /* generate a config from the existing config */
2361 mutex_enter(&spa->spa_props_lock);
2362 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2364 mutex_exit(&spa->spa_props_lock);
2366 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2369 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2370 for (c = 0; c < children; c++) {
2371 vdev_t *tvd = rvd->vdev_child[c];
2375 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2376 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2378 VERIFY(nvlist_add_string(schild[schildren],
2379 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2380 VERIFY(nvlist_add_uint64(schild[schildren],
2381 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2383 lastlogid = schildren;
2388 VERIFY(nvlist_lookup_nvlist_array(child[c],
2389 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2390 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2393 /* OK, create a config that can be used to split */
2394 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2395 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2396 VDEV_TYPE_ROOT) == 0);
2397 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2398 lastlogid != 0 ? lastlogid : schildren) == 0);
2400 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2401 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2403 for (c = 0; c < schildren; c++)
2404 nvlist_free(schild[c]);
2408 spa_config_exit(spa, SCL_VDEV, FTAG);
2410 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
2411 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2412 (void) rw_exit(&zs->zs_name_lock);
2414 nvlist_free(config);
2417 (void) printf("successful split - results:\n");
2418 mutex_enter(&spa_namespace_lock);
2419 show_pool_stats(spa);
2420 show_pool_stats(spa_lookup("splitp"));
2421 mutex_exit(&spa_namespace_lock);
2425 mutex_exit(&zs->zs_vdev_lock);
2430 * Verify that we can attach and detach devices.
2434 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2436 ztest_shared_t *zs = ztest_shared;
2437 spa_t *spa = zs->zs_spa;
2438 spa_aux_vdev_t *sav = &spa->spa_spares;
2439 vdev_t *rvd = spa->spa_root_vdev;
2440 vdev_t *oldvd, *newvd, *pvd;
2444 uint64_t ashift = ztest_get_ashift();
2445 uint64_t oldguid, pguid;
2446 size_t oldsize, newsize;
2447 char *oldpath, *newpath;
2449 int oldvd_has_siblings = B_FALSE;
2450 int newvd_is_spare = B_FALSE;
2452 int error, expected_error;
2454 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2455 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2457 mutex_enter(&zs->zs_vdev_lock);
2458 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
2460 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2463 * Decide whether to do an attach or a replace.
2465 replacing = ztest_random(2);
2468 * Pick a random top-level vdev.
2470 top = ztest_random_vdev_top(spa, B_TRUE);
2473 * Pick a random leaf within it.
2475 leaf = ztest_random(leaves);
2480 oldvd = rvd->vdev_child[top];
2481 if (zs->zs_mirrors >= 1) {
2482 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2483 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2484 oldvd = oldvd->vdev_child[leaf / zopt_raidz];
2486 if (zopt_raidz > 1) {
2487 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2488 ASSERT(oldvd->vdev_children == zopt_raidz);
2489 oldvd = oldvd->vdev_child[leaf % zopt_raidz];
2493 * If we're already doing an attach or replace, oldvd may be a
2494 * mirror vdev -- in which case, pick a random child.
2496 while (oldvd->vdev_children != 0) {
2497 oldvd_has_siblings = B_TRUE;
2498 ASSERT(oldvd->vdev_children >= 2);
2499 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2502 oldguid = oldvd->vdev_guid;
2503 oldsize = vdev_get_min_asize(oldvd);
2504 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2505 (void) strcpy(oldpath, oldvd->vdev_path);
2506 pvd = oldvd->vdev_parent;
2507 pguid = pvd->vdev_guid;
2510 * If oldvd has siblings, then half of the time, detach it.
2512 if (oldvd_has_siblings && ztest_random(2) == 0) {
2513 spa_config_exit(spa, SCL_VDEV, FTAG);
2514 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2515 if (error != 0 && error != ENODEV && error != EBUSY &&
2517 fatal(0, "detach (%s) returned %d", oldpath, error);
2522 * For the new vdev, choose with equal probability between the two
2523 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2525 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2526 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2527 newvd_is_spare = B_TRUE;
2528 (void) strcpy(newpath, newvd->vdev_path);
2530 (void) snprintf(newpath, sizeof (newpath), ztest_dev_template,
2531 zopt_dir, zopt_pool, top * leaves + leaf);
2532 if (ztest_random(2) == 0)
2533 newpath[strlen(newpath) - 1] = 'b';
2534 newvd = vdev_lookup_by_path(rvd, newpath);
2538 newsize = vdev_get_min_asize(newvd);
2541 * Make newsize a little bigger or smaller than oldsize.
2542 * If it's smaller, the attach should fail.
2543 * If it's larger, and we're doing a replace,
2544 * we should get dynamic LUN growth when we're done.
2546 newsize = 10 * oldsize / (9 + ztest_random(3));
2550 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2551 * unless it's a replace; in that case any non-replacing parent is OK.
2553 * If newvd is already part of the pool, it should fail with EBUSY.
2555 * If newvd is too small, it should fail with EOVERFLOW.
2557 if (pvd->vdev_ops != &vdev_mirror_ops &&
2558 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2559 pvd->vdev_ops == &vdev_replacing_ops ||
2560 pvd->vdev_ops == &vdev_spare_ops))
2561 expected_error = ENOTSUP;
2562 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2563 expected_error = ENOTSUP;
2564 else if (newvd == oldvd)
2565 expected_error = replacing ? 0 : EBUSY;
2566 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2567 expected_error = EBUSY;
2568 else if (newsize < oldsize)
2569 expected_error = EOVERFLOW;
2570 else if (ashift > oldvd->vdev_top->vdev_ashift)
2571 expected_error = EDOM;
2575 spa_config_exit(spa, SCL_VDEV, FTAG);
2578 * Build the nvlist describing newpath.
2580 root = make_vdev_root(newpath, NULL, newvd == NULL ? newsize : 0,
2581 ashift, 0, 0, 0, 1);
2583 error = spa_vdev_attach(spa, oldguid, root, replacing);
2588 * If our parent was the replacing vdev, but the replace completed,
2589 * then instead of failing with ENOTSUP we may either succeed,
2590 * fail with ENODEV, or fail with EOVERFLOW.
2592 if (expected_error == ENOTSUP &&
2593 (error == 0 || error == ENODEV || error == EOVERFLOW))
2594 expected_error = error;
2597 * If someone grew the LUN, the replacement may be too small.
2599 if (error == EOVERFLOW || error == EBUSY)
2600 expected_error = error;
2602 /* XXX workaround 6690467 */
2603 if (error != expected_error && expected_error != EBUSY) {
2604 fatal(0, "attach (%s %llu, %s %llu, %d) "
2605 "returned %d, expected %d",
2606 oldpath, (longlong_t)oldsize, newpath,
2607 (longlong_t)newsize, replacing, error, expected_error);
2610 mutex_exit(&zs->zs_vdev_lock);
2612 umem_free(oldpath, MAXPATHLEN);
2613 umem_free(newpath, MAXPATHLEN);
2617 * Callback function which expands the physical size of the vdev.
2620 grow_vdev(vdev_t *vd, void *arg)
2622 ASSERTV(spa_t *spa = vd->vdev_spa);
2623 size_t *newsize = arg;
2627 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2628 ASSERT(vd->vdev_ops->vdev_op_leaf);
2630 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2633 fsize = lseek(fd, 0, SEEK_END);
2634 VERIFY(ftruncate(fd, *newsize) == 0);
2636 if (zopt_verbose >= 6) {
2637 (void) printf("%s grew from %lu to %lu bytes\n",
2638 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2645 * Callback function which expands a given vdev by calling vdev_online().
2649 online_vdev(vdev_t *vd, void *arg)
2651 spa_t *spa = vd->vdev_spa;
2652 vdev_t *tvd = vd->vdev_top;
2653 uint64_t guid = vd->vdev_guid;
2654 uint64_t generation = spa->spa_config_generation + 1;
2655 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2658 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2659 ASSERT(vd->vdev_ops->vdev_op_leaf);
2661 /* Calling vdev_online will initialize the new metaslabs */
2662 spa_config_exit(spa, SCL_STATE, spa);
2663 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
2664 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2667 * If vdev_online returned an error or the underlying vdev_open
2668 * failed then we abort the expand. The only way to know that
2669 * vdev_open fails is by checking the returned newstate.
2671 if (error || newstate != VDEV_STATE_HEALTHY) {
2672 if (zopt_verbose >= 5) {
2673 (void) printf("Unable to expand vdev, state %llu, "
2674 "error %d\n", (u_longlong_t)newstate, error);
2678 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
2681 * Since we dropped the lock we need to ensure that we're
2682 * still talking to the original vdev. It's possible this
2683 * vdev may have been detached/replaced while we were
2684 * trying to online it.
2686 if (generation != spa->spa_config_generation) {
2687 if (zopt_verbose >= 5) {
2688 (void) printf("vdev configuration has changed, "
2689 "guid %llu, state %llu, expected gen %llu, "
2692 (u_longlong_t)tvd->vdev_state,
2693 (u_longlong_t)generation,
2694 (u_longlong_t)spa->spa_config_generation);
2702 * Traverse the vdev tree calling the supplied function.
2703 * We continue to walk the tree until we either have walked all
2704 * children or we receive a non-NULL return from the callback.
2705 * If a NULL callback is passed, then we just return back the first
2706 * leaf vdev we encounter.
2709 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
2713 if (vd->vdev_ops->vdev_op_leaf) {
2717 return (func(vd, arg));
2720 for (c = 0; c < vd->vdev_children; c++) {
2721 vdev_t *cvd = vd->vdev_child[c];
2722 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
2729 * Verify that dynamic LUN growth works as expected.
2733 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
2735 ztest_shared_t *zs = ztest_shared;
2736 spa_t *spa = zs->zs_spa;
2738 metaslab_class_t *mc;
2739 metaslab_group_t *mg;
2740 size_t psize, newsize;
2742 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
2744 mutex_enter(&zs->zs_vdev_lock);
2745 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2747 top = ztest_random_vdev_top(spa, B_TRUE);
2749 tvd = spa->spa_root_vdev->vdev_child[top];
2752 old_ms_count = tvd->vdev_ms_count;
2753 old_class_space = metaslab_class_get_space(mc);
2756 * Determine the size of the first leaf vdev associated with
2757 * our top-level device.
2759 vd = vdev_walk_tree(tvd, NULL, NULL);
2760 ASSERT3P(vd, !=, NULL);
2761 ASSERT(vd->vdev_ops->vdev_op_leaf);
2763 psize = vd->vdev_psize;
2766 * We only try to expand the vdev if it's healthy, less than 4x its
2767 * original size, and it has a valid psize.
2769 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
2770 psize == 0 || psize >= 4 * zopt_vdev_size) {
2771 spa_config_exit(spa, SCL_STATE, spa);
2772 mutex_exit(&zs->zs_vdev_lock);
2776 newsize = psize + psize / 8;
2777 ASSERT3U(newsize, >, psize);
2779 if (zopt_verbose >= 6) {
2780 (void) printf("Expanding LUN %s from %lu to %lu\n",
2781 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
2785 * Growing the vdev is a two step process:
2786 * 1). expand the physical size (i.e. relabel)
2787 * 2). online the vdev to create the new metaslabs
2789 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
2790 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
2791 tvd->vdev_state != VDEV_STATE_HEALTHY) {
2792 if (zopt_verbose >= 5) {
2793 (void) printf("Could not expand LUN because "
2794 "the vdev configuration changed.\n");
2796 spa_config_exit(spa, SCL_STATE, spa);
2797 mutex_exit(&zs->zs_vdev_lock);
2801 spa_config_exit(spa, SCL_STATE, spa);
2804 * Expanding the LUN will update the config asynchronously,
2805 * thus we must wait for the async thread to complete any
2806 * pending tasks before proceeding.
2810 mutex_enter(&spa->spa_async_lock);
2811 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
2812 mutex_exit(&spa->spa_async_lock);
2815 txg_wait_synced(spa_get_dsl(spa), 0);
2816 (void) poll(NULL, 0, 100);
2819 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
2821 tvd = spa->spa_root_vdev->vdev_child[top];
2822 new_ms_count = tvd->vdev_ms_count;
2823 new_class_space = metaslab_class_get_space(mc);
2825 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
2826 if (zopt_verbose >= 5) {
2827 (void) printf("Could not verify LUN expansion due to "
2828 "intervening vdev offline or remove.\n");
2830 spa_config_exit(spa, SCL_STATE, spa);
2831 mutex_exit(&zs->zs_vdev_lock);
2836 * Make sure we were able to grow the vdev.
2838 if (new_ms_count <= old_ms_count)
2839 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
2840 old_ms_count, new_ms_count);
2843 * Make sure we were able to grow the pool.
2845 if (new_class_space <= old_class_space)
2846 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
2847 old_class_space, new_class_space);
2849 if (zopt_verbose >= 5) {
2850 char oldnumbuf[6], newnumbuf[6];
2852 nicenum(old_class_space, oldnumbuf);
2853 nicenum(new_class_space, newnumbuf);
2854 (void) printf("%s grew from %s to %s\n",
2855 spa->spa_name, oldnumbuf, newnumbuf);
2858 spa_config_exit(spa, SCL_STATE, spa);
2859 mutex_exit(&zs->zs_vdev_lock);
2863 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
2867 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2870 * Create the objects common to all ztest datasets.
2872 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
2873 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
2877 ztest_dataset_create(char *dsname)
2879 uint64_t zilset = ztest_random(100);
2880 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
2881 ztest_objset_create_cb, NULL);
2883 if (err || zilset < 80)
2886 (void) printf("Setting dataset %s to sync always\n", dsname);
2887 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
2888 ZFS_SYNC_ALWAYS, B_FALSE));
2893 ztest_objset_destroy_cb(const char *name, void *arg)
2896 dmu_object_info_t doi;
2900 * Verify that the dataset contains a directory object.
2902 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os));
2903 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
2904 if (error != ENOENT) {
2905 /* We could have crashed in the middle of destroying it */
2906 ASSERT3U(error, ==, 0);
2907 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
2908 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
2910 dmu_objset_rele(os, FTAG);
2913 * Destroy the dataset.
2915 VERIFY3U(0, ==, dmu_objset_destroy(name, B_FALSE));
2920 ztest_snapshot_create(char *osname, uint64_t id)
2922 char snapname[MAXNAMELEN];
2925 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2928 error = dmu_objset_snapshot(osname, strchr(snapname, '@') + 1,
2929 NULL, NULL, B_FALSE, B_FALSE, -1);
2930 if (error == ENOSPC) {
2931 ztest_record_enospc(FTAG);
2934 if (error != 0 && error != EEXIST)
2935 fatal(0, "ztest_snapshot_create(%s) = %d", snapname, error);
2940 ztest_snapshot_destroy(char *osname, uint64_t id)
2942 char snapname[MAXNAMELEN];
2945 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
2948 error = dmu_objset_destroy(snapname, B_FALSE);
2949 if (error != 0 && error != ENOENT)
2950 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
2956 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
2958 ztest_shared_t *zs = ztest_shared;
2967 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
2968 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
2970 (void) rw_enter(&zs->zs_name_lock, RW_READER);
2972 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
2973 zs->zs_pool, (u_longlong_t)id);
2976 * If this dataset exists from a previous run, process its replay log
2977 * half of the time. If we don't replay it, then dmu_objset_destroy()
2978 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
2980 if (ztest_random(2) == 0 &&
2981 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
2982 ztest_zd_init(zdtmp, os);
2983 zil_replay(os, zdtmp, ztest_replay_vector);
2984 ztest_zd_fini(zdtmp);
2985 dmu_objset_disown(os, FTAG);
2989 * There may be an old instance of the dataset we're about to
2990 * create lying around from a previous run. If so, destroy it
2991 * and all of its snapshots.
2993 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
2994 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2997 * Verify that the destroyed dataset is no longer in the namespace.
2999 VERIFY3U(ENOENT, ==, dmu_objset_hold(name, FTAG, &os));
3002 * Verify that we can create a new dataset.
3004 error = ztest_dataset_create(name);
3006 if (error == ENOSPC) {
3007 ztest_record_enospc(FTAG);
3010 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3014 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3016 ztest_zd_init(zdtmp, os);
3019 * Open the intent log for it.
3021 zilog = zil_open(os, ztest_get_data);
3024 * Put some objects in there, do a little I/O to them,
3025 * and randomly take a couple of snapshots along the way.
3027 iters = ztest_random(5);
3028 for (i = 0; i < iters; i++) {
3029 ztest_dmu_object_alloc_free(zdtmp, id);
3030 if (ztest_random(iters) == 0)
3031 (void) ztest_snapshot_create(name, i);
3035 * Verify that we cannot create an existing dataset.
3037 VERIFY3U(EEXIST, ==,
3038 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3041 * Verify that we can hold an objset that is also owned.
3043 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3044 dmu_objset_rele(os2, FTAG);
3047 * Verify that we cannot own an objset that is already owned.
3050 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3053 dmu_objset_disown(os, FTAG);
3054 ztest_zd_fini(zdtmp);
3056 (void) rw_exit(&zs->zs_name_lock);
3058 umem_free(name, MAXNAMELEN);
3059 umem_free(zdtmp, sizeof (ztest_ds_t));
3063 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3066 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3068 ztest_shared_t *zs = ztest_shared;
3070 (void) rw_enter(&zs->zs_name_lock, RW_READER);
3071 (void) ztest_snapshot_destroy(zd->zd_name, id);
3072 (void) ztest_snapshot_create(zd->zd_name, id);
3073 (void) rw_exit(&zs->zs_name_lock);
3077 * Cleanup non-standard snapshots and clones.
3080 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3089 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3090 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3091 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3092 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3093 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3095 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3096 osname, (u_longlong_t)id);
3097 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3098 osname, (u_longlong_t)id);
3099 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3100 clone1name, (u_longlong_t)id);
3101 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3102 osname, (u_longlong_t)id);
3103 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3104 clone1name, (u_longlong_t)id);
3106 error = dmu_objset_destroy(clone2name, B_FALSE);
3107 if (error && error != ENOENT)
3108 fatal(0, "dmu_objset_destroy(%s) = %d", clone2name, error);
3109 error = dmu_objset_destroy(snap3name, B_FALSE);
3110 if (error && error != ENOENT)
3111 fatal(0, "dmu_objset_destroy(%s) = %d", snap3name, error);
3112 error = dmu_objset_destroy(snap2name, B_FALSE);
3113 if (error && error != ENOENT)
3114 fatal(0, "dmu_objset_destroy(%s) = %d", snap2name, error);
3115 error = dmu_objset_destroy(clone1name, B_FALSE);
3116 if (error && error != ENOENT)
3117 fatal(0, "dmu_objset_destroy(%s) = %d", clone1name, error);
3118 error = dmu_objset_destroy(snap1name, B_FALSE);
3119 if (error && error != ENOENT)
3120 fatal(0, "dmu_objset_destroy(%s) = %d", snap1name, error);
3122 umem_free(snap1name, MAXNAMELEN);
3123 umem_free(clone1name, MAXNAMELEN);
3124 umem_free(snap2name, MAXNAMELEN);
3125 umem_free(clone2name, MAXNAMELEN);
3126 umem_free(snap3name, MAXNAMELEN);
3130 * Verify dsl_dataset_promote handles EBUSY
3133 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3135 ztest_shared_t *zs = ztest_shared;
3143 char *osname = zd->zd_name;
3146 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3147 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3148 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3149 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3150 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3152 (void) rw_enter(&zs->zs_name_lock, RW_READER);
3154 ztest_dsl_dataset_cleanup(osname, id);
3156 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3157 osname, (u_longlong_t)id);
3158 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3159 osname, (u_longlong_t)id);
3160 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3161 clone1name, (u_longlong_t)id);
3162 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3163 osname, (u_longlong_t)id);
3164 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3165 clone1name, (u_longlong_t)id);
3167 error = dmu_objset_snapshot(osname, strchr(snap1name, '@')+1,
3168 NULL, NULL, B_FALSE, B_FALSE, -1);
3169 if (error && error != EEXIST) {
3170 if (error == ENOSPC) {
3171 ztest_record_enospc(FTAG);
3174 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3177 error = dmu_objset_hold(snap1name, FTAG, &clone);
3179 fatal(0, "dmu_open_snapshot(%s) = %d", snap1name, error);
3181 error = dmu_objset_clone(clone1name, dmu_objset_ds(clone), 0);
3182 dmu_objset_rele(clone, FTAG);
3184 if (error == ENOSPC) {
3185 ztest_record_enospc(FTAG);
3188 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3191 error = dmu_objset_snapshot(clone1name, strchr(snap2name, '@')+1,
3192 NULL, NULL, B_FALSE, B_FALSE, -1);
3193 if (error && error != EEXIST) {
3194 if (error == ENOSPC) {
3195 ztest_record_enospc(FTAG);
3198 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3201 error = dmu_objset_snapshot(clone1name, strchr(snap3name, '@')+1,
3202 NULL, NULL, B_FALSE, B_FALSE, -1);
3203 if (error && error != EEXIST) {
3204 if (error == ENOSPC) {
3205 ztest_record_enospc(FTAG);
3208 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3211 error = dmu_objset_hold(snap3name, FTAG, &clone);
3213 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3215 error = dmu_objset_clone(clone2name, dmu_objset_ds(clone), 0);
3216 dmu_objset_rele(clone, FTAG);
3218 if (error == ENOSPC) {
3219 ztest_record_enospc(FTAG);
3222 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3225 error = dsl_dataset_own(snap2name, B_FALSE, FTAG, &ds);
3227 fatal(0, "dsl_dataset_own(%s) = %d", snap2name, error);
3228 error = dsl_dataset_promote(clone2name, NULL);
3230 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3232 dsl_dataset_disown(ds, FTAG);
3235 ztest_dsl_dataset_cleanup(osname, id);
3237 (void) rw_exit(&zs->zs_name_lock);
3239 umem_free(snap1name, MAXNAMELEN);
3240 umem_free(clone1name, MAXNAMELEN);
3241 umem_free(snap2name, MAXNAMELEN);
3242 umem_free(clone2name, MAXNAMELEN);
3243 umem_free(snap3name, MAXNAMELEN);
3246 #undef OD_ARRAY_SIZE
3247 #define OD_ARRAY_SIZE 4
3250 * Verify that dmu_object_{alloc,free} work as expected.
3253 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3260 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3261 od = umem_alloc(size, UMEM_NOFAIL);
3262 batchsize = OD_ARRAY_SIZE;
3264 for (b = 0; b < batchsize; b++)
3265 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3268 * Destroy the previous batch of objects, create a new batch,
3269 * and do some I/O on the new objects.
3271 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3274 while (ztest_random(4 * batchsize) != 0)
3275 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3276 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3278 umem_free(od, size);
3281 #undef OD_ARRAY_SIZE
3282 #define OD_ARRAY_SIZE 2
3285 * Verify that dmu_{read,write} work as expected.
3288 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3293 objset_t *os = zd->zd_os;
3294 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3295 od = umem_alloc(size, UMEM_NOFAIL);
3297 int i, freeit, error;
3299 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3300 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3301 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3302 uint64_t regions = 997;
3303 uint64_t stride = 123456789ULL;
3304 uint64_t width = 40;
3305 int free_percent = 5;
3308 * This test uses two objects, packobj and bigobj, that are always
3309 * updated together (i.e. in the same tx) so that their contents are
3310 * in sync and can be compared. Their contents relate to each other
3311 * in a simple way: packobj is a dense array of 'bufwad' structures,
3312 * while bigobj is a sparse array of the same bufwads. Specifically,
3313 * for any index n, there are three bufwads that should be identical:
3315 * packobj, at offset n * sizeof (bufwad_t)
3316 * bigobj, at the head of the nth chunk
3317 * bigobj, at the tail of the nth chunk
3319 * The chunk size is arbitrary. It doesn't have to be a power of two,
3320 * and it doesn't have any relation to the object blocksize.
3321 * The only requirement is that it can hold at least two bufwads.
3323 * Normally, we write the bufwad to each of these locations.
3324 * However, free_percent of the time we instead write zeroes to
3325 * packobj and perform a dmu_free_range() on bigobj. By comparing
3326 * bigobj to packobj, we can verify that the DMU is correctly
3327 * tracking which parts of an object are allocated and free,
3328 * and that the contents of the allocated blocks are correct.
3332 * Read the directory info. If it's the first time, set things up.
3334 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3335 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3337 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3338 umem_free(od, size);
3342 bigobj = od[0].od_object;
3343 packobj = od[1].od_object;
3344 chunksize = od[0].od_gen;
3345 ASSERT(chunksize == od[1].od_gen);
3348 * Prefetch a random chunk of the big object.
3349 * Our aim here is to get some async reads in flight
3350 * for blocks that we may free below; the DMU should
3351 * handle this race correctly.
3353 n = ztest_random(regions) * stride + ztest_random(width);
3354 s = 1 + ztest_random(2 * width - 1);
3355 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3358 * Pick a random index and compute the offsets into packobj and bigobj.
3360 n = ztest_random(regions) * stride + ztest_random(width);
3361 s = 1 + ztest_random(width - 1);
3363 packoff = n * sizeof (bufwad_t);
3364 packsize = s * sizeof (bufwad_t);
3366 bigoff = n * chunksize;
3367 bigsize = s * chunksize;
3369 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3370 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3373 * free_percent of the time, free a range of bigobj rather than
3376 freeit = (ztest_random(100) < free_percent);
3379 * Read the current contents of our objects.
3381 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3383 ASSERT3U(error, ==, 0);
3384 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3386 ASSERT3U(error, ==, 0);
3389 * Get a tx for the mods to both packobj and bigobj.
3391 tx = dmu_tx_create(os);
3393 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3396 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3398 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3400 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3402 umem_free(packbuf, packsize);
3403 umem_free(bigbuf, bigsize);
3404 umem_free(od, size);
3408 dmu_object_set_checksum(os, bigobj,
3409 (enum zio_checksum)ztest_random_dsl_prop(ZFS_PROP_CHECKSUM), tx);
3411 dmu_object_set_compress(os, bigobj,
3412 (enum zio_compress)ztest_random_dsl_prop(ZFS_PROP_COMPRESSION), tx);
3415 * For each index from n to n + s, verify that the existing bufwad
3416 * in packobj matches the bufwads at the head and tail of the
3417 * corresponding chunk in bigobj. Then update all three bufwads
3418 * with the new values we want to write out.
3420 for (i = 0; i < s; i++) {
3422 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3424 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3426 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3428 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3429 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3431 if (pack->bw_txg > txg)
3432 fatal(0, "future leak: got %llx, open txg is %llx",
3435 if (pack->bw_data != 0 && pack->bw_index != n + i)
3436 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3437 pack->bw_index, n, i);
3439 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3440 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3442 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3443 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3446 bzero(pack, sizeof (bufwad_t));
3448 pack->bw_index = n + i;
3450 pack->bw_data = 1 + ztest_random(-2ULL);
3457 * We've verified all the old bufwads, and made new ones.
3458 * Now write them out.
3460 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3463 if (zopt_verbose >= 7) {
3464 (void) printf("freeing offset %llx size %llx"
3466 (u_longlong_t)bigoff,
3467 (u_longlong_t)bigsize,
3470 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3472 if (zopt_verbose >= 7) {
3473 (void) printf("writing offset %llx size %llx"
3475 (u_longlong_t)bigoff,
3476 (u_longlong_t)bigsize,
3479 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3485 * Sanity check the stuff we just wrote.
3488 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3489 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3491 VERIFY(0 == dmu_read(os, packobj, packoff,
3492 packsize, packcheck, DMU_READ_PREFETCH));
3493 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3494 bigsize, bigcheck, DMU_READ_PREFETCH));
3496 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3497 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3499 umem_free(packcheck, packsize);
3500 umem_free(bigcheck, bigsize);
3503 umem_free(packbuf, packsize);
3504 umem_free(bigbuf, bigsize);
3505 umem_free(od, size);
3509 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3510 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3518 * For each index from n to n + s, verify that the existing bufwad
3519 * in packobj matches the bufwads at the head and tail of the
3520 * corresponding chunk in bigobj. Then update all three bufwads
3521 * with the new values we want to write out.
3523 for (i = 0; i < s; i++) {
3525 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3527 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3529 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3531 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3532 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3534 if (pack->bw_txg > txg)
3535 fatal(0, "future leak: got %llx, open txg is %llx",
3538 if (pack->bw_data != 0 && pack->bw_index != n + i)
3539 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3540 pack->bw_index, n, i);
3542 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3543 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3545 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3546 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3548 pack->bw_index = n + i;
3550 pack->bw_data = 1 + ztest_random(-2ULL);
3557 #undef OD_ARRAY_SIZE
3558 #define OD_ARRAY_SIZE 2
3561 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3563 objset_t *os = zd->zd_os;
3570 bufwad_t *packbuf, *bigbuf;
3571 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3572 uint64_t blocksize = ztest_random_blocksize();
3573 uint64_t chunksize = blocksize;
3574 uint64_t regions = 997;
3575 uint64_t stride = 123456789ULL;
3577 dmu_buf_t *bonus_db;
3578 arc_buf_t **bigbuf_arcbufs;
3579 dmu_object_info_t doi;
3581 size = sizeof(ztest_od_t) * OD_ARRAY_SIZE;
3582 od = umem_alloc(size, UMEM_NOFAIL);
3585 * This test uses two objects, packobj and bigobj, that are always
3586 * updated together (i.e. in the same tx) so that their contents are
3587 * in sync and can be compared. Their contents relate to each other
3588 * in a simple way: packobj is a dense array of 'bufwad' structures,
3589 * while bigobj is a sparse array of the same bufwads. Specifically,
3590 * for any index n, there are three bufwads that should be identical:
3592 * packobj, at offset n * sizeof (bufwad_t)
3593 * bigobj, at the head of the nth chunk
3594 * bigobj, at the tail of the nth chunk
3596 * The chunk size is set equal to bigobj block size so that
3597 * dmu_assign_arcbuf() can be tested for object updates.
3601 * Read the directory info. If it's the first time, set things up.
3603 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3604 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3607 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3608 umem_free(od, size);
3612 bigobj = od[0].od_object;
3613 packobj = od[1].od_object;
3614 blocksize = od[0].od_blocksize;
3615 chunksize = blocksize;
3616 ASSERT(chunksize == od[1].od_gen);
3618 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3619 VERIFY(ISP2(doi.doi_data_block_size));
3620 VERIFY(chunksize == doi.doi_data_block_size);
3621 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3624 * Pick a random index and compute the offsets into packobj and bigobj.
3626 n = ztest_random(regions) * stride + ztest_random(width);
3627 s = 1 + ztest_random(width - 1);
3629 packoff = n * sizeof (bufwad_t);
3630 packsize = s * sizeof (bufwad_t);
3632 bigoff = n * chunksize;
3633 bigsize = s * chunksize;
3635 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3636 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3638 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3640 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3643 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3644 * Iteration 1 test zcopy to already referenced dbufs.
3645 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3646 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3647 * Iteration 4 test zcopy when dbuf is no longer dirty.
3648 * Iteration 5 test zcopy when it can't be done.
3649 * Iteration 6 one more zcopy write.
3651 for (i = 0; i < 7; i++) {
3656 * In iteration 5 (i == 5) use arcbufs
3657 * that don't match bigobj blksz to test
3658 * dmu_assign_arcbuf() when it can't directly
3659 * assign an arcbuf to a dbuf.
3661 for (j = 0; j < s; j++) {
3664 dmu_request_arcbuf(bonus_db, chunksize);
3666 bigbuf_arcbufs[2 * j] =
3667 dmu_request_arcbuf(bonus_db, chunksize / 2);
3668 bigbuf_arcbufs[2 * j + 1] =
3669 dmu_request_arcbuf(bonus_db, chunksize / 2);
3674 * Get a tx for the mods to both packobj and bigobj.
3676 tx = dmu_tx_create(os);
3678 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3679 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3681 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3683 umem_free(packbuf, packsize);
3684 umem_free(bigbuf, bigsize);
3685 for (j = 0; j < s; j++) {
3687 dmu_return_arcbuf(bigbuf_arcbufs[j]);
3690 bigbuf_arcbufs[2 * j]);
3692 bigbuf_arcbufs[2 * j + 1]);
3695 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3696 umem_free(od, size);
3697 dmu_buf_rele(bonus_db, FTAG);
3702 * 50% of the time don't read objects in the 1st iteration to
3703 * test dmu_assign_arcbuf() for the case when there're no
3704 * existing dbufs for the specified offsets.
3706 if (i != 0 || ztest_random(2) != 0) {
3707 error = dmu_read(os, packobj, packoff,
3708 packsize, packbuf, DMU_READ_PREFETCH);
3709 ASSERT3U(error, ==, 0);
3710 error = dmu_read(os, bigobj, bigoff, bigsize,
3711 bigbuf, DMU_READ_PREFETCH);
3712 ASSERT3U(error, ==, 0);
3714 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
3718 * We've verified all the old bufwads, and made new ones.
3719 * Now write them out.
3721 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3722 if (zopt_verbose >= 7) {
3723 (void) printf("writing offset %llx size %llx"
3725 (u_longlong_t)bigoff,
3726 (u_longlong_t)bigsize,
3729 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
3732 bcopy((caddr_t)bigbuf + (off - bigoff),
3733 bigbuf_arcbufs[j]->b_data, chunksize);
3735 bcopy((caddr_t)bigbuf + (off - bigoff),
3736 bigbuf_arcbufs[2 * j]->b_data,
3738 bcopy((caddr_t)bigbuf + (off - bigoff) +
3740 bigbuf_arcbufs[2 * j + 1]->b_data,
3745 VERIFY(dmu_buf_hold(os, bigobj, off,
3746 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
3749 dmu_assign_arcbuf(bonus_db, off,
3750 bigbuf_arcbufs[j], tx);
3752 dmu_assign_arcbuf(bonus_db, off,
3753 bigbuf_arcbufs[2 * j], tx);
3754 dmu_assign_arcbuf(bonus_db,
3755 off + chunksize / 2,
3756 bigbuf_arcbufs[2 * j + 1], tx);
3759 dmu_buf_rele(dbt, FTAG);
3765 * Sanity check the stuff we just wrote.
3768 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3769 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3771 VERIFY(0 == dmu_read(os, packobj, packoff,
3772 packsize, packcheck, DMU_READ_PREFETCH));
3773 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3774 bigsize, bigcheck, DMU_READ_PREFETCH));
3776 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3777 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3779 umem_free(packcheck, packsize);
3780 umem_free(bigcheck, bigsize);
3783 txg_wait_open(dmu_objset_pool(os), 0);
3784 } else if (i == 3) {
3785 txg_wait_synced(dmu_objset_pool(os), 0);
3789 dmu_buf_rele(bonus_db, FTAG);
3790 umem_free(packbuf, packsize);
3791 umem_free(bigbuf, bigsize);
3792 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
3793 umem_free(od, size);
3798 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
3802 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3803 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
3804 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3807 * Have multiple threads write to large offsets in an object
3808 * to verify that parallel writes to an object -- even to the
3809 * same blocks within the object -- doesn't cause any trouble.
3811 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
3813 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
3816 while (ztest_random(10) != 0)
3817 ztest_io(zd, od->od_object, offset);
3819 umem_free(od, sizeof(ztest_od_t));
3823 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
3826 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
3827 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3828 uint64_t count = ztest_random(20) + 1;
3829 uint64_t blocksize = ztest_random_blocksize();
3832 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3834 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3836 if (ztest_object_init(zd, od, sizeof (ztest_od_t), !ztest_random(2)) != 0) {
3837 umem_free(od, sizeof(ztest_od_t));
3841 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
3842 umem_free(od, sizeof(ztest_od_t));
3846 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
3848 data = umem_zalloc(blocksize, UMEM_NOFAIL);
3850 while (ztest_random(count) != 0) {
3851 uint64_t randoff = offset + (ztest_random(count) * blocksize);
3852 if (ztest_write(zd, od->od_object, randoff, blocksize,
3855 while (ztest_random(4) != 0)
3856 ztest_io(zd, od->od_object, randoff);
3859 umem_free(data, blocksize);
3860 umem_free(od, sizeof(ztest_od_t));
3864 * Verify that zap_{create,destroy,add,remove,update} work as expected.
3866 #define ZTEST_ZAP_MIN_INTS 1
3867 #define ZTEST_ZAP_MAX_INTS 4
3868 #define ZTEST_ZAP_MAX_PROPS 1000
3871 ztest_zap(ztest_ds_t *zd, uint64_t id)
3873 objset_t *os = zd->zd_os;
3876 uint64_t txg, last_txg;
3877 uint64_t value[ZTEST_ZAP_MAX_INTS];
3878 uint64_t zl_ints, zl_intsize, prop;
3881 char propname[100], txgname[100];
3883 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
3885 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
3886 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
3888 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
3889 !ztest_random(2)) != 0)
3892 object = od->od_object;
3895 * Generate a known hash collision, and verify that
3896 * we can lookup and remove both entries.
3898 tx = dmu_tx_create(os);
3899 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3900 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3903 for (i = 0; i < 2; i++) {
3905 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
3908 for (i = 0; i < 2; i++) {
3909 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
3910 sizeof (uint64_t), 1, &value[i], tx));
3912 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
3913 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3914 ASSERT3U(zl_ints, ==, 1);
3916 for (i = 0; i < 2; i++) {
3917 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
3922 * Generate a buch of random entries.
3924 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
3926 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3927 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3928 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3929 bzero(value, sizeof (value));
3933 * If these zap entries already exist, validate their contents.
3935 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3937 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3938 ASSERT3U(zl_ints, ==, 1);
3940 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
3941 zl_ints, &last_txg) == 0);
3943 VERIFY(zap_length(os, object, propname, &zl_intsize,
3946 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
3947 ASSERT3U(zl_ints, ==, ints);
3949 VERIFY(zap_lookup(os, object, propname, zl_intsize,
3950 zl_ints, value) == 0);
3952 for (i = 0; i < ints; i++) {
3953 ASSERT3U(value[i], ==, last_txg + object + i);
3956 ASSERT3U(error, ==, ENOENT);
3960 * Atomically update two entries in our zap object.
3961 * The first is named txg_%llu, and contains the txg
3962 * in which the property was last updated. The second
3963 * is named prop_%llu, and the nth element of its value
3964 * should be txg + object + n.
3966 tx = dmu_tx_create(os);
3967 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
3968 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3973 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
3975 for (i = 0; i < ints; i++)
3976 value[i] = txg + object + i;
3978 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
3980 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
3986 * Remove a random pair of entries.
3988 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
3989 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
3990 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
3992 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
3994 if (error == ENOENT)
3997 ASSERT3U(error, ==, 0);
3999 tx = dmu_tx_create(os);
4000 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4001 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4004 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4005 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4008 umem_free(od, sizeof(ztest_od_t));
4012 * Testcase to test the upgrading of a microzap to fatzap.
4015 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4017 objset_t *os = zd->zd_os;
4019 uint64_t object, txg;
4022 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4023 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4025 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4026 !ztest_random(2)) != 0)
4028 object = od->od_object;
4031 * Add entries to this ZAP and make sure it spills over
4032 * and gets upgraded to a fatzap. Also, since we are adding
4033 * 2050 entries we should see ptrtbl growth and leaf-block split.
4035 for (i = 0; i < 2050; i++) {
4036 char name[MAXNAMELEN];
4041 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4042 (u_longlong_t)id, (u_longlong_t)value);
4044 tx = dmu_tx_create(os);
4045 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4046 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4049 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4051 ASSERT(error == 0 || error == EEXIST);
4055 umem_free(od, sizeof(ztest_od_t));
4060 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4062 objset_t *os = zd->zd_os;
4064 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4066 int i, namelen, error;
4067 int micro = ztest_random(2);
4068 char name[20], string_value[20];
4071 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4072 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4074 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4075 umem_free(od, sizeof(ztest_od_t));
4079 object = od->od_object;
4082 * Generate a random name of the form 'xxx.....' where each
4083 * x is a random printable character and the dots are dots.
4084 * There are 94 such characters, and the name length goes from
4085 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4087 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4089 for (i = 0; i < 3; i++)
4090 name[i] = '!' + ztest_random('~' - '!' + 1);
4091 for (; i < namelen - 1; i++)
4095 if ((namelen & 1) || micro) {
4096 wsize = sizeof (txg);
4102 data = string_value;
4106 VERIFY(zap_count(os, object, &count) == 0);
4107 ASSERT(count != -1ULL);
4110 * Select an operation: length, lookup, add, update, remove.
4112 i = ztest_random(5);
4115 tx = dmu_tx_create(os);
4116 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4117 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4120 bcopy(name, string_value, namelen);
4124 bzero(string_value, namelen);
4130 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4132 ASSERT3U(wsize, ==, zl_wsize);
4133 ASSERT3U(wc, ==, zl_wc);
4135 ASSERT3U(error, ==, ENOENT);
4140 error = zap_lookup(os, object, name, wsize, wc, data);
4142 if (data == string_value &&
4143 bcmp(name, data, namelen) != 0)
4144 fatal(0, "name '%s' != val '%s' len %d",
4145 name, data, namelen);
4147 ASSERT3U(error, ==, ENOENT);
4152 error = zap_add(os, object, name, wsize, wc, data, tx);
4153 ASSERT(error == 0 || error == EEXIST);
4157 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4161 error = zap_remove(os, object, name, tx);
4162 ASSERT(error == 0 || error == ENOENT);
4169 umem_free(od, sizeof(ztest_od_t));
4173 * Commit callback data.
4175 typedef struct ztest_cb_data {
4176 list_node_t zcd_node;
4178 int zcd_expected_err;
4179 boolean_t zcd_added;
4180 boolean_t zcd_called;
4184 /* This is the actual commit callback function */
4186 ztest_commit_callback(void *arg, int error)
4188 ztest_cb_data_t *data = arg;
4189 uint64_t synced_txg;
4191 VERIFY(data != NULL);
4192 VERIFY3S(data->zcd_expected_err, ==, error);
4193 VERIFY(!data->zcd_called);
4195 synced_txg = spa_last_synced_txg(data->zcd_spa);
4196 if (data->zcd_txg > synced_txg)
4197 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4198 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4201 data->zcd_called = B_TRUE;
4203 if (error == ECANCELED) {
4204 ASSERT3U(data->zcd_txg, ==, 0);
4205 ASSERT(!data->zcd_added);
4208 * The private callback data should be destroyed here, but
4209 * since we are going to check the zcd_called field after
4210 * dmu_tx_abort(), we will destroy it there.
4215 ASSERT(data->zcd_added);
4216 ASSERT3U(data->zcd_txg, !=, 0);
4218 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4220 /* See if this cb was called more quickly */
4221 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4222 zc_min_txg_delay = synced_txg - data->zcd_txg;
4224 /* Remove our callback from the list */
4225 list_remove(&zcl.zcl_callbacks, data);
4227 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4229 umem_free(data, sizeof (ztest_cb_data_t));
4232 /* Allocate and initialize callback data structure */
4233 static ztest_cb_data_t *
4234 ztest_create_cb_data(objset_t *os, uint64_t txg)
4236 ztest_cb_data_t *cb_data;
4238 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4240 cb_data->zcd_txg = txg;
4241 cb_data->zcd_spa = dmu_objset_spa(os);
4242 list_link_init(&cb_data->zcd_node);
4248 * Commit callback test.
4251 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4253 objset_t *os = zd->zd_os;
4256 ztest_cb_data_t *cb_data[3], *tmp_cb;
4257 uint64_t old_txg, txg;
4260 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4261 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4263 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4264 umem_free(od, sizeof(ztest_od_t));
4268 tx = dmu_tx_create(os);
4270 cb_data[0] = ztest_create_cb_data(os, 0);
4271 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4273 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4275 /* Every once in a while, abort the transaction on purpose */
4276 if (ztest_random(100) == 0)
4280 error = dmu_tx_assign(tx, TXG_NOWAIT);
4282 txg = error ? 0 : dmu_tx_get_txg(tx);
4284 cb_data[0]->zcd_txg = txg;
4285 cb_data[1] = ztest_create_cb_data(os, txg);
4286 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4290 * It's not a strict requirement to call the registered
4291 * callbacks from inside dmu_tx_abort(), but that's what
4292 * it's supposed to happen in the current implementation
4293 * so we will check for that.
4295 for (i = 0; i < 2; i++) {
4296 cb_data[i]->zcd_expected_err = ECANCELED;
4297 VERIFY(!cb_data[i]->zcd_called);
4302 for (i = 0; i < 2; i++) {
4303 VERIFY(cb_data[i]->zcd_called);
4304 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4307 umem_free(od, sizeof(ztest_od_t));
4311 cb_data[2] = ztest_create_cb_data(os, txg);
4312 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4315 * Read existing data to make sure there isn't a future leak.
4317 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4318 &old_txg, DMU_READ_PREFETCH));
4321 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4324 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4326 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4329 * Since commit callbacks don't have any ordering requirement and since
4330 * it is theoretically possible for a commit callback to be called
4331 * after an arbitrary amount of time has elapsed since its txg has been
4332 * synced, it is difficult to reliably determine whether a commit
4333 * callback hasn't been called due to high load or due to a flawed
4336 * In practice, we will assume that if after a certain number of txgs a
4337 * commit callback hasn't been called, then most likely there's an
4338 * implementation bug..
4340 tmp_cb = list_head(&zcl.zcl_callbacks);
4341 if (tmp_cb != NULL &&
4342 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4343 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4344 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4348 * Let's find the place to insert our callbacks.
4350 * Even though the list is ordered by txg, it is possible for the
4351 * insertion point to not be the end because our txg may already be
4352 * quiescing at this point and other callbacks in the open txg
4353 * (from other objsets) may have sneaked in.
4355 tmp_cb = list_tail(&zcl.zcl_callbacks);
4356 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4357 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4359 /* Add the 3 callbacks to the list */
4360 for (i = 0; i < 3; i++) {
4362 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4364 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4367 cb_data[i]->zcd_added = B_TRUE;
4368 VERIFY(!cb_data[i]->zcd_called);
4370 tmp_cb = cb_data[i];
4375 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4379 umem_free(od, sizeof(ztest_od_t));
4384 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4386 zfs_prop_t proplist[] = {
4388 ZFS_PROP_COMPRESSION,
4392 ztest_shared_t *zs = ztest_shared;
4395 (void) rw_enter(&zs->zs_name_lock, RW_READER);
4397 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4398 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4399 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4401 (void) rw_exit(&zs->zs_name_lock);
4406 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4408 ztest_shared_t *zs = ztest_shared;
4409 nvlist_t *props = NULL;
4411 (void) rw_enter(&zs->zs_name_lock, RW_READER);
4413 (void) ztest_spa_prop_set_uint64(zs, ZPOOL_PROP_DEDUPDITTO,
4414 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4416 VERIFY3U(spa_prop_get(zs->zs_spa, &props), ==, 0);
4418 if (zopt_verbose >= 6)
4419 dump_nvlist(props, 4);
4423 (void) rw_exit(&zs->zs_name_lock);
4427 * Test snapshot hold/release and deferred destroy.
4430 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4433 objset_t *os = zd->zd_os;
4437 char clonename[100];
4439 char osname[MAXNAMELEN];
4441 (void) rw_enter(&ztest_shared->zs_name_lock, RW_READER);
4443 dmu_objset_name(os, osname);
4445 (void) snprintf(snapname, 100, "sh1_%llu", (u_longlong_t)id);
4446 (void) snprintf(fullname, 100, "%s@%s", osname, snapname);
4447 (void) snprintf(clonename, 100, "%s/ch1_%llu",osname,(u_longlong_t)id);
4448 (void) snprintf(tag, 100, "tag_%llu", (u_longlong_t)id);
4451 * Clean up from any previous run.
4453 (void) dmu_objset_destroy(clonename, B_FALSE);
4454 (void) dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4455 (void) dmu_objset_destroy(fullname, B_FALSE);
4458 * Create snapshot, clone it, mark snap for deferred destroy,
4459 * destroy clone, verify snap was also destroyed.
4461 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4464 if (error == ENOSPC) {
4465 ztest_record_enospc("dmu_objset_snapshot");
4468 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4471 error = dmu_objset_hold(fullname, FTAG, &origin);
4473 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4475 error = dmu_objset_clone(clonename, dmu_objset_ds(origin), 0);
4476 dmu_objset_rele(origin, FTAG);
4478 if (error == ENOSPC) {
4479 ztest_record_enospc("dmu_objset_clone");
4482 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4485 error = dmu_objset_destroy(fullname, B_TRUE);
4487 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4491 error = dmu_objset_destroy(clonename, B_FALSE);
4493 fatal(0, "dmu_objset_destroy(%s) = %d", clonename, error);
4495 error = dmu_objset_hold(fullname, FTAG, &origin);
4496 if (error != ENOENT)
4497 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4500 * Create snapshot, add temporary hold, verify that we can't
4501 * destroy a held snapshot, mark for deferred destroy,
4502 * release hold, verify snapshot was destroyed.
4504 error = dmu_objset_snapshot(osname, snapname, NULL, NULL, FALSE,
4507 if (error == ENOSPC) {
4508 ztest_record_enospc("dmu_objset_snapshot");
4511 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4514 error = dsl_dataset_user_hold(osname, snapname, tag, B_FALSE,
4517 fatal(0, "dsl_dataset_user_hold(%s)", fullname, tag);
4519 error = dmu_objset_destroy(fullname, B_FALSE);
4520 if (error != EBUSY) {
4521 fatal(0, "dmu_objset_destroy(%s, B_FALSE) = %d",
4525 error = dmu_objset_destroy(fullname, B_TRUE);
4527 fatal(0, "dmu_objset_destroy(%s, B_TRUE) = %d",
4531 error = dsl_dataset_user_release(osname, snapname, tag, B_FALSE);
4533 fatal(0, "dsl_dataset_user_release(%s)", fullname, tag);
4535 VERIFY(dmu_objset_hold(fullname, FTAG, &origin) == ENOENT);
4538 (void) rw_exit(&ztest_shared->zs_name_lock);
4542 * Inject random faults into the on-disk data.
4546 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4548 ztest_shared_t *zs = ztest_shared;
4549 spa_t *spa = zs->zs_spa;
4553 uint64_t bad = 0x1990c0ffeedecadeull;
4558 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4564 boolean_t islog = B_FALSE;
4566 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4567 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4569 mutex_enter(&zs->zs_vdev_lock);
4570 maxfaults = MAXFAULTS();
4571 leaves = MAX(zs->zs_mirrors, 1) * zopt_raidz;
4572 mirror_save = zs->zs_mirrors;
4573 mutex_exit(&zs->zs_vdev_lock);
4575 ASSERT(leaves >= 1);
4578 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4580 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4582 if (ztest_random(2) == 0) {
4584 * Inject errors on a normal data device or slog device.
4586 top = ztest_random_vdev_top(spa, B_TRUE);
4587 leaf = ztest_random(leaves) + zs->zs_splits;
4590 * Generate paths to the first leaf in this top-level vdev,
4591 * and to the random leaf we selected. We'll induce transient
4592 * write failures and random online/offline activity on leaf 0,
4593 * and we'll write random garbage to the randomly chosen leaf.
4595 (void) snprintf(path0, sizeof (path0), ztest_dev_template,
4596 zopt_dir, zopt_pool, top * leaves + zs->zs_splits);
4597 (void) snprintf(pathrand, sizeof (pathrand), ztest_dev_template,
4598 zopt_dir, zopt_pool, top * leaves + leaf);
4600 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4601 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4604 if (vd0 != NULL && maxfaults != 1) {
4606 * Make vd0 explicitly claim to be unreadable,
4607 * or unwriteable, or reach behind its back
4608 * and close the underlying fd. We can do this if
4609 * maxfaults == 0 because we'll fail and reexecute,
4610 * and we can do it if maxfaults >= 2 because we'll
4611 * have enough redundancy. If maxfaults == 1, the
4612 * combination of this with injection of random data
4613 * corruption below exceeds the pool's fault tolerance.
4615 vdev_file_t *vf = vd0->vdev_tsd;
4617 if (vf != NULL && ztest_random(3) == 0) {
4618 (void) close(vf->vf_vnode->v_fd);
4619 vf->vf_vnode->v_fd = -1;
4620 } else if (ztest_random(2) == 0) {
4621 vd0->vdev_cant_read = B_TRUE;
4623 vd0->vdev_cant_write = B_TRUE;
4625 guid0 = vd0->vdev_guid;
4629 * Inject errors on an l2cache device.
4631 spa_aux_vdev_t *sav = &spa->spa_l2cache;
4633 if (sav->sav_count == 0) {
4634 spa_config_exit(spa, SCL_STATE, FTAG);
4637 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
4638 guid0 = vd0->vdev_guid;
4639 (void) strcpy(path0, vd0->vdev_path);
4640 (void) strcpy(pathrand, vd0->vdev_path);
4644 maxfaults = INT_MAX; /* no limit on cache devices */
4647 spa_config_exit(spa, SCL_STATE, FTAG);
4650 * If we can tolerate two or more faults, or we're dealing
4651 * with a slog, randomly online/offline vd0.
4653 if ((maxfaults >= 2 || islog) && guid0 != 0) {
4654 if (ztest_random(10) < 6) {
4655 int flags = (ztest_random(2) == 0 ?
4656 ZFS_OFFLINE_TEMPORARY : 0);
4659 * We have to grab the zs_name_lock as writer to
4660 * prevent a race between offlining a slog and
4661 * destroying a dataset. Offlining the slog will
4662 * grab a reference on the dataset which may cause
4663 * dmu_objset_destroy() to fail with EBUSY thus
4664 * leaving the dataset in an inconsistent state.
4667 (void) rw_enter(&ztest_shared->zs_name_lock,
4670 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
4673 (void) rw_exit(&ztest_shared->zs_name_lock);
4675 (void) vdev_online(spa, guid0, 0, NULL);
4683 * We have at least single-fault tolerance, so inject data corruption.
4685 fd = open(pathrand, O_RDWR);
4687 if (fd == -1) /* we hit a gap in the device namespace */
4690 fsize = lseek(fd, 0, SEEK_END);
4692 while (--iters != 0) {
4693 offset = ztest_random(fsize / (leaves << bshift)) *
4694 (leaves << bshift) + (leaf << bshift) +
4695 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
4697 if (offset >= fsize)
4700 mutex_enter(&zs->zs_vdev_lock);
4701 if (mirror_save != zs->zs_mirrors) {
4702 mutex_exit(&zs->zs_vdev_lock);
4707 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
4708 fatal(1, "can't inject bad word at 0x%llx in %s",
4711 mutex_exit(&zs->zs_vdev_lock);
4713 if (zopt_verbose >= 7)
4714 (void) printf("injected bad word into %s,"
4715 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
4720 umem_free(path0, MAXPATHLEN);
4721 umem_free(pathrand, MAXPATHLEN);
4725 * Verify that DDT repair works as expected.
4728 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
4730 ztest_shared_t *zs = ztest_shared;
4731 spa_t *spa = zs->zs_spa;
4732 objset_t *os = zd->zd_os;
4734 uint64_t object, blocksize, txg, pattern, psize;
4735 enum zio_checksum checksum = spa_dedup_checksum(spa);
4740 int copies = 2 * ZIO_DEDUPDITTO_MIN;
4743 blocksize = ztest_random_blocksize();
4744 blocksize = MIN(blocksize, 2048); /* because we write so many */
4746 od = umem_alloc(sizeof(ztest_od_t), UMEM_NOFAIL);
4747 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4749 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4750 umem_free(od, sizeof(ztest_od_t));
4755 * Take the name lock as writer to prevent anyone else from changing
4756 * the pool and dataset properies we need to maintain during this test.
4758 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
4760 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
4762 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
4764 (void) rw_exit(&zs->zs_name_lock);
4765 umem_free(od, sizeof(ztest_od_t));
4769 object = od[0].od_object;
4770 blocksize = od[0].od_blocksize;
4771 pattern = spa_guid(spa) ^ dmu_objset_fsid_guid(os);
4773 ASSERT(object != 0);
4775 tx = dmu_tx_create(os);
4776 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
4777 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
4779 (void) rw_exit(&zs->zs_name_lock);
4780 umem_free(od, sizeof(ztest_od_t));
4785 * Write all the copies of our block.
4787 for (i = 0; i < copies; i++) {
4788 uint64_t offset = i * blocksize;
4789 VERIFY(dmu_buf_hold(os, object, offset, FTAG, &db,
4790 DMU_READ_NO_PREFETCH) == 0);
4791 ASSERT(db->db_offset == offset);
4792 ASSERT(db->db_size == blocksize);
4793 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
4794 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
4795 dmu_buf_will_fill(db, tx);
4796 ztest_pattern_set(db->db_data, db->db_size, pattern);
4797 dmu_buf_rele(db, FTAG);
4801 txg_wait_synced(spa_get_dsl(spa), txg);
4804 * Find out what block we got.
4806 VERIFY(dmu_buf_hold(os, object, 0, FTAG, &db,
4807 DMU_READ_NO_PREFETCH) == 0);
4808 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
4809 dmu_buf_rele(db, FTAG);
4812 * Damage the block. Dedup-ditto will save us when we read it later.
4814 psize = BP_GET_PSIZE(&blk);
4815 buf = zio_buf_alloc(psize);
4816 ztest_pattern_set(buf, psize, ~pattern);
4818 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
4819 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
4820 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
4822 zio_buf_free(buf, psize);
4824 (void) rw_exit(&zs->zs_name_lock);
4825 umem_free(od, sizeof(ztest_od_t));
4833 ztest_scrub(ztest_ds_t *zd, uint64_t id)
4835 ztest_shared_t *zs = ztest_shared;
4836 spa_t *spa = zs->zs_spa;
4838 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4839 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
4840 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4844 * Rename the pool to a different name and then rename it back.
4848 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
4850 ztest_shared_t *zs = ztest_shared;
4851 char *oldname, *newname;
4854 (void) rw_enter(&zs->zs_name_lock, RW_WRITER);
4856 oldname = zs->zs_pool;
4857 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
4858 (void) strcpy(newname, oldname);
4859 (void) strcat(newname, "_tmp");
4864 VERIFY3U(0, ==, spa_rename(oldname, newname));
4867 * Try to open it under the old name, which shouldn't exist
4869 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
4872 * Open it under the new name and make sure it's still the same spa_t.
4874 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
4876 ASSERT(spa == zs->zs_spa);
4877 spa_close(spa, FTAG);
4880 * Rename it back to the original
4882 VERIFY3U(0, ==, spa_rename(newname, oldname));
4885 * Make sure it can still be opened
4887 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4889 ASSERT(spa == zs->zs_spa);
4890 spa_close(spa, FTAG);
4892 umem_free(newname, strlen(newname) + 1);
4894 (void) rw_exit(&zs->zs_name_lock);
4898 * Verify pool integrity by running zdb.
4901 ztest_run_zdb(char *pool)
4909 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
4910 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
4911 zbuf = umem_alloc(1024, UMEM_NOFAIL);
4913 VERIFY(realpath(getexecname(), bin) != NULL);
4914 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
4915 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
4916 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
4917 strcpy(bin, "/sbin/zdb"); /* Installed */
4919 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
4920 strcat(bin, "/zdb/zdb");
4924 "%s -bcc%s%s -U %s %s",
4926 zopt_verbose >= 3 ? "s" : "",
4927 zopt_verbose >= 4 ? "v" : "",
4931 if (zopt_verbose >= 5)
4932 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
4934 fp = popen(zdb, "r");
4936 while (fgets(zbuf, sizeof (zbuf), fp) != NULL)
4937 if (zopt_verbose >= 3)
4938 (void) printf("%s", zbuf);
4940 status = pclose(fp);
4945 ztest_dump_core = 0;
4946 if (WIFEXITED(status))
4947 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
4949 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
4951 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
4952 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
4953 umem_free(zbuf, 1024);
4957 ztest_walk_pool_directory(char *header)
4961 if (zopt_verbose >= 6)
4962 (void) printf("%s\n", header);
4964 mutex_enter(&spa_namespace_lock);
4965 while ((spa = spa_next(spa)) != NULL)
4966 if (zopt_verbose >= 6)
4967 (void) printf("\t%s\n", spa_name(spa));
4968 mutex_exit(&spa_namespace_lock);
4972 ztest_spa_import_export(char *oldname, char *newname)
4974 nvlist_t *config, *newconfig;
4978 if (zopt_verbose >= 4) {
4979 (void) printf("import/export: old = %s, new = %s\n",
4984 * Clean up from previous runs.
4986 (void) spa_destroy(newname);
4989 * Get the pool's configuration and guid.
4991 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
4994 * Kick off a scrub to tickle scrub/export races.
4996 if (ztest_random(2) == 0)
4997 (void) spa_scan(spa, POOL_SCAN_SCRUB);
4999 pool_guid = spa_guid(spa);
5000 spa_close(spa, FTAG);
5002 ztest_walk_pool_directory("pools before export");
5007 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5009 ztest_walk_pool_directory("pools after export");
5014 newconfig = spa_tryimport(config);
5015 ASSERT(newconfig != NULL);
5016 nvlist_free(newconfig);
5019 * Import it under the new name.
5021 VERIFY3U(0, ==, spa_import(newname, config, NULL, 0));
5023 ztest_walk_pool_directory("pools after import");
5026 * Try to import it again -- should fail with EEXIST.
5028 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5031 * Try to import it under a different name -- should fail with EEXIST.
5033 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5036 * Verify that the pool is no longer visible under the old name.
5038 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5041 * Verify that we can open and close the pool using the new name.
5043 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5044 ASSERT(pool_guid == spa_guid(spa));
5045 spa_close(spa, FTAG);
5047 nvlist_free(config);
5051 ztest_resume(spa_t *spa)
5053 if (spa_suspended(spa) && zopt_verbose >= 6)
5054 (void) printf("resuming from suspended state\n");
5055 spa_vdev_state_enter(spa, SCL_NONE);
5056 vdev_clear(spa, NULL);
5057 (void) spa_vdev_state_exit(spa, NULL, 0);
5058 (void) zio_resume(spa);
5062 ztest_resume_thread(void *arg)
5066 while (!ztest_exiting) {
5067 if (spa_suspended(spa))
5069 (void) poll(NULL, 0, 100);
5080 ztest_deadman_alarm(int sig)
5082 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5086 ztest_execute(ztest_info_t *zi, uint64_t id)
5088 ztest_shared_t *zs = ztest_shared;
5089 ztest_ds_t *zd = &zs->zs_zd[id % zopt_datasets];
5090 hrtime_t functime = gethrtime();
5093 for (i = 0; i < zi->zi_iters; i++)
5094 zi->zi_func(zd, id);
5096 functime = gethrtime() - functime;
5098 atomic_add_64(&zi->zi_call_count, 1);
5099 atomic_add_64(&zi->zi_call_time, functime);
5101 if (zopt_verbose >= 4) {
5103 (void) dladdr((void *)zi->zi_func, &dli);
5104 (void) printf("%6.2f sec in %s\n",
5105 (double)functime / NANOSEC, dli.dli_sname);
5110 ztest_thread(void *arg)
5112 uint64_t id = (uintptr_t)arg;
5113 ztest_shared_t *zs = ztest_shared;
5118 while ((now = gethrtime()) < zs->zs_thread_stop) {
5120 * See if it's time to force a crash.
5122 if (now > zs->zs_thread_kill)
5126 * If we're getting ENOSPC with some regularity, stop.
5128 if (zs->zs_enospc_count > 10)
5132 * Pick a random function to execute.
5134 zi = &zs->zs_info[ztest_random(ZTEST_FUNCS)];
5135 call_next = zi->zi_call_next;
5137 if (now >= call_next &&
5138 atomic_cas_64(&zi->zi_call_next, call_next, call_next +
5139 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next)
5140 ztest_execute(zi, id);
5149 ztest_dataset_name(char *dsname, char *pool, int d)
5151 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5155 ztest_dataset_destroy(ztest_shared_t *zs, int d)
5157 char name[MAXNAMELEN];
5160 ztest_dataset_name(name, zs->zs_pool, d);
5162 if (zopt_verbose >= 3)
5163 (void) printf("Destroying %s to free up space\n", name);
5166 * Cleanup any non-standard clones and snapshots. In general,
5167 * ztest thread t operates on dataset (t % zopt_datasets),
5168 * so there may be more than one thing to clean up.
5170 for (t = d; t < zopt_threads; t += zopt_datasets)
5171 ztest_dsl_dataset_cleanup(name, t);
5173 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5174 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5178 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5180 uint64_t usedobjs, dirobjs, scratch;
5183 * ZTEST_DIROBJ is the object directory for the entire dataset.
5184 * Therefore, the number of objects in use should equal the
5185 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5186 * If not, we have an object leak.
5188 * Note that we can only check this in ztest_dataset_open(),
5189 * when the open-context and syncing-context values agree.
5190 * That's because zap_count() returns the open-context value,
5191 * while dmu_objset_space() returns the rootbp fill count.
5193 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5194 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5195 ASSERT3U(dirobjs + 1, ==, usedobjs);
5199 ztest_dataset_open(ztest_shared_t *zs, int d)
5201 ztest_ds_t *zd = &zs->zs_zd[d];
5202 uint64_t committed_seq = zd->zd_seq;
5205 char name[MAXNAMELEN];
5208 ztest_dataset_name(name, zs->zs_pool, d);
5210 (void) rw_enter(&zs->zs_name_lock, RW_READER);
5212 error = ztest_dataset_create(name);
5213 if (error == ENOSPC) {
5214 (void) rw_exit(&zs->zs_name_lock);
5215 ztest_record_enospc(FTAG);
5218 ASSERT(error == 0 || error == EEXIST);
5220 VERIFY3U(dmu_objset_hold(name, zd, &os), ==, 0);
5221 (void) rw_exit(&zs->zs_name_lock);
5223 ztest_zd_init(zd, os);
5225 zilog = zd->zd_zilog;
5227 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5228 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5229 fatal(0, "missing log records: claimed %llu < committed %llu",
5230 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5232 ztest_dataset_dirobj_verify(zd);
5234 zil_replay(os, zd, ztest_replay_vector);
5236 ztest_dataset_dirobj_verify(zd);
5238 if (zopt_verbose >= 6)
5239 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5241 (u_longlong_t)zilog->zl_parse_blk_count,
5242 (u_longlong_t)zilog->zl_parse_lr_count,
5243 (u_longlong_t)zilog->zl_replaying_seq);
5245 zilog = zil_open(os, ztest_get_data);
5247 if (zilog->zl_replaying_seq != 0 &&
5248 zilog->zl_replaying_seq < committed_seq)
5249 fatal(0, "missing log records: replayed %llu < committed %llu",
5250 zilog->zl_replaying_seq, committed_seq);
5256 ztest_dataset_close(ztest_shared_t *zs, int d)
5258 ztest_ds_t *zd = &zs->zs_zd[d];
5260 zil_close(zd->zd_zilog);
5261 dmu_objset_rele(zd->zd_os, zd);
5267 * Kick off threads to run tests on all datasets in parallel.
5270 ztest_run(ztest_shared_t *zs)
5274 kthread_t *resume_thread;
5279 ztest_exiting = B_FALSE;
5282 * Initialize parent/child shared state.
5284 mutex_init(&zs->zs_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5285 rw_init(&zs->zs_name_lock, NULL, RW_DEFAULT, NULL);
5287 zs->zs_thread_start = gethrtime();
5288 zs->zs_thread_stop = zs->zs_thread_start + zopt_passtime * NANOSEC;
5289 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5290 zs->zs_thread_kill = zs->zs_thread_stop;
5291 if (ztest_random(100) < zopt_killrate)
5292 zs->zs_thread_kill -= ztest_random(zopt_passtime * NANOSEC);
5294 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5296 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5297 offsetof(ztest_cb_data_t, zcd_node));
5302 kernel_init(FREAD | FWRITE);
5303 VERIFY(spa_open(zs->zs_pool, &spa, FTAG) == 0);
5304 spa->spa_debug = B_TRUE;
5307 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5310 * We don't expect the pool to suspend unless maxfaults == 0,
5311 * in which case ztest_fault_inject() temporarily takes away
5312 * the only valid replica.
5314 if (MAXFAULTS() == 0)
5315 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5317 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5320 * Create a thread to periodically resume suspended I/O.
5322 VERIFY3P((resume_thread = thread_create(NULL, 0, ztest_resume_thread,
5323 spa, TS_RUN, NULL, 0, 0)), !=, NULL);
5326 * Set a deadman alarm to abort() if we hang.
5328 signal(SIGALRM, ztest_deadman_alarm);
5329 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5332 * Verify that we can safely inquire about about any object,
5333 * whether it's allocated or not. To make it interesting,
5334 * we probe a 5-wide window around each power of two.
5335 * This hits all edge cases, including zero and the max.
5337 for (t = 0; t < 64; t++) {
5338 for (d = -5; d <= 5; d++) {
5339 error = dmu_object_info(spa->spa_meta_objset,
5340 (1ULL << t) + d, NULL);
5341 ASSERT(error == 0 || error == ENOENT ||
5347 * If we got any ENOSPC errors on the previous run, destroy something.
5349 if (zs->zs_enospc_count != 0) {
5350 int d = ztest_random(zopt_datasets);
5351 ztest_dataset_destroy(zs, d);
5353 zs->zs_enospc_count = 0;
5355 tid = umem_zalloc(zopt_threads * sizeof (kt_did_t), UMEM_NOFAIL);
5357 if (zopt_verbose >= 4)
5358 (void) printf("starting main threads...\n");
5361 * Kick off all the tests that run in parallel.
5363 for (t = 0; t < zopt_threads; t++) {
5366 if (t < zopt_datasets && ztest_dataset_open(zs, t) != 0)
5369 VERIFY3P(thread = thread_create(NULL, 0, ztest_thread,
5370 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0), !=, NULL);
5371 tid[t] = thread->t_tid;
5375 * Wait for all of the tests to complete. We go in reverse order
5376 * so we don't close datasets while threads are still using them.
5378 for (t = zopt_threads - 1; t >= 0; t--) {
5379 thread_join(tid[t]);
5380 if (t < zopt_datasets)
5381 ztest_dataset_close(zs, t);
5384 txg_wait_synced(spa_get_dsl(spa), 0);
5386 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5387 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5389 umem_free(tid, zopt_threads * sizeof (kt_did_t));
5391 /* Kill the resume thread */
5392 ztest_exiting = B_TRUE;
5393 thread_join(resume_thread->t_tid);
5397 * Right before closing the pool, kick off a bunch of async I/O;
5398 * spa_close() should wait for it to complete.
5400 for (object = 1; object < 50; object++)
5401 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5403 /* Verify that at least one commit cb was called in a timely fashion */
5404 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5405 VERIFY3U(zc_min_txg_delay, ==, 0);
5407 spa_close(spa, FTAG);
5410 * Verify that we can loop over all pools.
5412 mutex_enter(&spa_namespace_lock);
5413 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5414 if (zopt_verbose > 3)
5415 (void) printf("spa_next: found %s\n", spa_name(spa));
5416 mutex_exit(&spa_namespace_lock);
5419 * Verify that we can export the pool and reimport it under a
5422 if (ztest_random(2) == 0) {
5423 char name[MAXNAMELEN];
5424 (void) snprintf(name, MAXNAMELEN, "%s_import", zs->zs_pool);
5425 ztest_spa_import_export(zs->zs_pool, name);
5426 ztest_spa_import_export(name, zs->zs_pool);
5431 list_destroy(&zcl.zcl_callbacks);
5432 mutex_destroy(&zcl.zcl_callbacks_lock);
5433 rw_destroy(&zs->zs_name_lock);
5434 mutex_destroy(&zs->zs_vdev_lock);
5438 ztest_freeze(ztest_shared_t *zs)
5440 ztest_ds_t *zd = &zs->zs_zd[0];
5444 if (zopt_verbose >= 3)
5445 (void) printf("testing spa_freeze()...\n");
5447 kernel_init(FREAD | FWRITE);
5448 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5449 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5452 * Force the first log block to be transactionally allocated.
5453 * We have to do this before we freeze the pool -- otherwise
5454 * the log chain won't be anchored.
5456 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5457 ztest_dmu_object_alloc_free(zd, 0);
5458 zil_commit(zd->zd_zilog, 0);
5461 txg_wait_synced(spa_get_dsl(spa), 0);
5464 * Freeze the pool. This stops spa_sync() from doing anything,
5465 * so that the only way to record changes from now on is the ZIL.
5470 * Run tests that generate log records but don't alter the pool config
5471 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5472 * We do a txg_wait_synced() after each iteration to force the txg
5473 * to increase well beyond the last synced value in the uberblock.
5474 * The ZIL should be OK with that.
5476 while (ztest_random(10) != 0 && numloops++ < zopt_maxloops) {
5477 ztest_dmu_write_parallel(zd, 0);
5478 ztest_dmu_object_alloc_free(zd, 0);
5479 txg_wait_synced(spa_get_dsl(spa), 0);
5483 * Commit all of the changes we just generated.
5485 zil_commit(zd->zd_zilog, 0);
5486 txg_wait_synced(spa_get_dsl(spa), 0);
5489 * Close our dataset and close the pool.
5491 ztest_dataset_close(zs, 0);
5492 spa_close(spa, FTAG);
5496 * Open and close the pool and dataset to induce log replay.
5498 kernel_init(FREAD | FWRITE);
5499 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5500 VERIFY3U(0, ==, ztest_dataset_open(zs, 0));
5501 ztest_dataset_close(zs, 0);
5502 spa_close(spa, FTAG);
5507 print_time(hrtime_t t, char *timebuf)
5509 hrtime_t s = t / NANOSEC;
5510 hrtime_t m = s / 60;
5511 hrtime_t h = m / 60;
5512 hrtime_t d = h / 24;
5521 (void) sprintf(timebuf,
5522 "%llud%02lluh%02llum%02llus", d, h, m, s);
5524 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
5526 (void) sprintf(timebuf, "%llum%02llus", m, s);
5528 (void) sprintf(timebuf, "%llus", s);
5532 make_random_props(void)
5536 if (ztest_random(2) == 0)
5539 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
5540 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
5542 (void) printf("props:\n");
5543 dump_nvlist(props, 4);
5549 * Create a storage pool with the given name and initial vdev size.
5550 * Then test spa_freeze() functionality.
5553 ztest_init(ztest_shared_t *zs)
5556 nvlist_t *nvroot, *props;
5558 mutex_init(&zs->zs_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5559 rw_init(&zs->zs_name_lock, NULL, RW_DEFAULT, NULL);
5561 kernel_init(FREAD | FWRITE);
5564 * Create the storage pool.
5566 (void) spa_destroy(zs->zs_pool);
5567 ztest_shared->zs_vdev_next_leaf = 0;
5569 zs->zs_mirrors = zopt_mirrors;
5570 nvroot = make_vdev_root(NULL, NULL, zopt_vdev_size, 0,
5571 0, zopt_raidz, zs->zs_mirrors, 1);
5572 props = make_random_props();
5573 VERIFY3U(0, ==, spa_create(zs->zs_pool, nvroot, props, NULL, NULL));
5574 nvlist_free(nvroot);
5576 VERIFY3U(0, ==, spa_open(zs->zs_pool, &spa, FTAG));
5577 metaslab_sz = 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
5578 spa_close(spa, FTAG);
5582 ztest_run_zdb(zs->zs_pool);
5586 ztest_run_zdb(zs->zs_pool);
5588 (void) rw_destroy(&zs->zs_name_lock);
5589 (void) mutex_destroy(&zs->zs_vdev_lock);
5593 main(int argc, char **argv)
5605 (void) setvbuf(stdout, NULL, _IOLBF, 0);
5607 ztest_random_fd = open("/dev/urandom", O_RDONLY);
5609 dprintf_setup(&argc, argv);
5610 process_options(argc, argv);
5612 /* Override location of zpool.cache */
5613 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
5617 * Blow away any existing copy of zpool.cache
5620 (void) remove(spa_config_path);
5622 shared_size = sizeof (*zs) + zopt_datasets * sizeof (ztest_ds_t);
5624 zs = ztest_shared = (void *)mmap(0,
5625 P2ROUNDUP(shared_size, getpagesize()),
5626 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
5628 if (zopt_verbose >= 1) {
5629 (void) printf("%llu vdevs, %d datasets, %d threads,"
5630 " %llu seconds...\n",
5631 (u_longlong_t)zopt_vdevs, zopt_datasets, zopt_threads,
5632 (u_longlong_t)zopt_time);
5636 * Create and initialize our storage pool.
5638 for (i = 1; i <= zopt_init; i++) {
5639 bzero(zs, sizeof (ztest_shared_t));
5640 if (zopt_verbose >= 3 && zopt_init != 1)
5641 (void) printf("ztest_init(), pass %d\n", i);
5642 zs->zs_pool = zopt_pool;
5646 zs->zs_pool = zopt_pool;
5647 zs->zs_proc_start = gethrtime();
5648 zs->zs_proc_stop = zs->zs_proc_start + zopt_time * NANOSEC;
5650 for (f = 0; f < ZTEST_FUNCS; f++) {
5651 zi = &zs->zs_info[f];
5652 *zi = ztest_info[f];
5653 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
5654 zi->zi_call_next = UINT64_MAX;
5656 zi->zi_call_next = zs->zs_proc_start +
5657 ztest_random(2 * zi->zi_interval[0] + 1);
5661 * Run the tests in a loop. These tests include fault injection
5662 * to verify that self-healing data works, and forced crashes
5663 * to verify that we never lose on-disk consistency.
5665 while (gethrtime() < zs->zs_proc_stop) {
5670 * Initialize the workload counters for each function.
5672 for (f = 0; f < ZTEST_FUNCS; f++) {
5673 zi = &zs->zs_info[f];
5674 zi->zi_call_count = 0;
5675 zi->zi_call_time = 0;
5678 /* Set the allocation switch size */
5679 metaslab_df_alloc_threshold = ztest_random(metaslab_sz / 4) + 1;
5684 fatal(1, "fork failed");
5686 if (pid == 0) { /* child */
5687 struct rlimit rl = { 1024, 1024 };
5688 (void) setrlimit(RLIMIT_NOFILE, &rl);
5689 (void) enable_extended_FILE_stdio(-1, -1);
5694 while (waitpid(pid, &status, 0) != pid)
5697 if (WIFEXITED(status)) {
5698 if (WEXITSTATUS(status) != 0) {
5699 (void) fprintf(stderr,
5700 "child exited with code %d\n",
5701 WEXITSTATUS(status));
5704 } else if (WIFSIGNALED(status)) {
5705 if (WTERMSIG(status) != SIGKILL) {
5706 (void) fprintf(stderr,
5707 "child died with signal %d\n",
5713 (void) fprintf(stderr, "something strange happened "
5720 if (zopt_verbose >= 1) {
5721 hrtime_t now = gethrtime();
5723 now = MIN(now, zs->zs_proc_stop);
5724 print_time(zs->zs_proc_stop - now, timebuf);
5725 nicenum(zs->zs_space, numbuf);
5727 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
5728 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
5730 WIFEXITED(status) ? "Complete" : "SIGKILL",
5731 (u_longlong_t)zs->zs_enospc_count,
5732 100.0 * zs->zs_alloc / zs->zs_space,
5734 100.0 * (now - zs->zs_proc_start) /
5735 (zopt_time * NANOSEC), timebuf);
5738 if (zopt_verbose >= 2) {
5739 (void) printf("\nWorkload summary:\n\n");
5740 (void) printf("%7s %9s %s\n",
5741 "Calls", "Time", "Function");
5742 (void) printf("%7s %9s %s\n",
5743 "-----", "----", "--------");
5744 for (f = 0; f < ZTEST_FUNCS; f++) {
5747 zi = &zs->zs_info[f];
5748 print_time(zi->zi_call_time, timebuf);
5749 (void) dladdr((void *)zi->zi_func, &dli);
5750 (void) printf("%7llu %9s %s\n",
5751 (u_longlong_t)zi->zi_call_count, timebuf,
5754 (void) printf("\n");
5758 * It's possible that we killed a child during a rename test,
5759 * in which case we'll have a 'ztest_tmp' pool lying around
5760 * instead of 'ztest'. Do a blind rename in case this happened.
5763 if (spa_open(zopt_pool, &spa, FTAG) == 0) {
5764 spa_close(spa, FTAG);
5766 char tmpname[MAXNAMELEN];
5768 kernel_init(FREAD | FWRITE);
5769 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
5771 (void) spa_rename(tmpname, zopt_pool);
5775 ztest_run_zdb(zopt_pool);
5778 if (zopt_verbose >= 1) {
5779 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
5780 kills, iters - kills, (100.0 * kills) / MAX(1, iters));