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, 2014 by Delphix. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * The objective of this program is to provide a DMU/ZAP/SPA stress test
30 * that runs entirely in userland, is easy to use, and easy to extend.
32 * The overall design of the ztest program is as follows:
34 * (1) For each major functional area (e.g. adding vdevs to a pool,
35 * creating and destroying datasets, reading and writing objects, etc)
36 * we have a simple routine to test that functionality. These
37 * individual routines do not have to do anything "stressful".
39 * (2) We turn these simple functionality tests into a stress test by
40 * running them all in parallel, with as many threads as desired,
41 * and spread across as many datasets, objects, and vdevs as desired.
43 * (3) While all this is happening, we inject faults into the pool to
44 * verify that self-healing data really works.
46 * (4) Every time we open a dataset, we change its checksum and compression
47 * functions. Thus even individual objects vary from block to block
48 * in which checksum they use and whether they're compressed.
50 * (5) To verify that we never lose on-disk consistency after a crash,
51 * we run the entire test in a child of the main process.
52 * At random times, the child self-immolates with a SIGKILL.
53 * This is the software equivalent of pulling the power cord.
54 * The parent then runs the test again, using the existing
55 * storage pool, as many times as desired. If backwards compatibility
56 * testing is enabled ztest will sometimes run the "older" version
57 * of ztest after a SIGKILL.
59 * (6) To verify that we don't have future leaks or temporal incursions,
60 * many of the functional tests record the transaction group number
61 * as part of their data. When reading old data, they verify that
62 * the transaction group number is less than the current, open txg.
63 * If you add a new test, please do this if applicable.
65 * (7) Threads are created with a reduced stack size, for sanity checking.
66 * Therefore, it's important not to allocate huge buffers on the stack.
68 * When run with no arguments, ztest runs for about five minutes and
69 * produces no output if successful. To get a little bit of information,
70 * specify -V. To get more information, specify -VV, and so on.
72 * To turn this into an overnight stress test, use -T to specify run time.
74 * You can ask more more vdevs [-v], datasets [-d], or threads [-t]
75 * to increase the pool capacity, fanout, and overall stress level.
77 * Use the -k option to set the desired frequency of kills.
79 * When ztest invokes itself it passes all relevant information through a
80 * temporary file which is mmap-ed in the child process. This allows shared
81 * memory to survive the exec syscall. The ztest_shared_hdr_t struct is always
82 * stored at offset 0 of this file and contains information on the size and
83 * number of shared structures in the file. The information stored in this file
84 * must remain backwards compatible with older versions of ztest so that
85 * ztest can invoke them during backwards compatibility testing (-B).
88 #include <sys/zfs_context.h>
94 #include <sys/dmu_objset.h>
100 #include <sys/resource.h>
103 #include <sys/zil_impl.h>
104 #include <sys/vdev_impl.h>
105 #include <sys/vdev_file.h>
106 #include <sys/spa_impl.h>
107 #include <sys/metaslab_impl.h>
108 #include <sys/dsl_prop.h>
109 #include <sys/dsl_dataset.h>
110 #include <sys/dsl_destroy.h>
111 #include <sys/dsl_scan.h>
112 #include <sys/zio_checksum.h>
113 #include <sys/refcount.h>
114 #include <sys/zfeature.h>
115 #include <sys/dsl_userhold.h>
117 #include <stdio_ext.h>
125 #include <sys/fs/zfs.h>
126 #include <libnvpair.h>
128 static int ztest_fd_data = -1;
129 static int ztest_fd_rand = -1;
131 typedef struct ztest_shared_hdr {
132 uint64_t zh_hdr_size;
133 uint64_t zh_opts_size;
135 uint64_t zh_stats_size;
136 uint64_t zh_stats_count;
138 uint64_t zh_ds_count;
139 } ztest_shared_hdr_t;
141 static ztest_shared_hdr_t *ztest_shared_hdr;
143 typedef struct ztest_shared_opts {
144 char zo_pool[MAXNAMELEN];
145 char zo_dir[MAXNAMELEN];
146 char zo_alt_ztest[MAXNAMELEN];
147 char zo_alt_libpath[MAXNAMELEN];
149 uint64_t zo_vdevtime;
157 uint64_t zo_passtime;
158 uint64_t zo_killrate;
162 uint64_t zo_maxloops;
163 uint64_t zo_metaslab_gang_bang;
164 } ztest_shared_opts_t;
166 static const ztest_shared_opts_t ztest_opts_defaults = {
167 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
168 .zo_dir = { '/', 't', 'm', 'p', '\0' },
169 .zo_alt_ztest = { '\0' },
170 .zo_alt_libpath = { '\0' },
172 .zo_ashift = SPA_MINBLOCKSHIFT,
175 .zo_raidz_parity = 1,
176 .zo_vdev_size = SPA_MINDEVSIZE,
179 .zo_passtime = 60, /* 60 seconds */
180 .zo_killrate = 70, /* 70% kill rate */
183 .zo_time = 300, /* 5 minutes */
184 .zo_maxloops = 50, /* max loops during spa_freeze() */
185 .zo_metaslab_gang_bang = 32 << 10
188 extern uint64_t metaslab_gang_bang;
189 extern uint64_t metaslab_df_alloc_threshold;
190 extern int metaslab_preload_limit;
192 static ztest_shared_opts_t *ztest_shared_opts;
193 static ztest_shared_opts_t ztest_opts;
195 typedef struct ztest_shared_ds {
199 static ztest_shared_ds_t *ztest_shared_ds;
200 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
202 #define BT_MAGIC 0x123456789abcdefULL
203 #define MAXFAULTS() \
204 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
208 ZTEST_IO_WRITE_PATTERN,
209 ZTEST_IO_WRITE_ZEROES,
216 typedef struct ztest_block_tag {
226 typedef struct bufwad {
233 * XXX -- fix zfs range locks to be generic so we can use them here.
255 #define ZTEST_RANGE_LOCKS 64
256 #define ZTEST_OBJECT_LOCKS 64
259 * Object descriptor. Used as a template for object lookup/create/remove.
261 typedef struct ztest_od {
264 dmu_object_type_t od_type;
265 dmu_object_type_t od_crtype;
266 uint64_t od_blocksize;
267 uint64_t od_crblocksize;
270 char od_name[MAXNAMELEN];
276 typedef struct ztest_ds {
277 ztest_shared_ds_t *zd_shared;
279 rwlock_t zd_zilog_lock;
281 ztest_od_t *zd_od; /* debugging aid */
282 char zd_name[MAXNAMELEN];
283 kmutex_t zd_dirobj_lock;
284 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
285 rll_t zd_range_lock[ZTEST_RANGE_LOCKS];
289 * Per-iteration state.
291 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
293 typedef struct ztest_info {
294 ztest_func_t *zi_func; /* test function */
295 uint64_t zi_iters; /* iterations per execution */
296 uint64_t *zi_interval; /* execute every <interval> seconds */
299 typedef struct ztest_shared_callstate {
300 uint64_t zc_count; /* per-pass count */
301 uint64_t zc_time; /* per-pass time */
302 uint64_t zc_next; /* next time to call this function */
303 } ztest_shared_callstate_t;
305 static ztest_shared_callstate_t *ztest_shared_callstate;
306 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
309 * Note: these aren't static because we want dladdr() to work.
311 ztest_func_t ztest_dmu_read_write;
312 ztest_func_t ztest_dmu_write_parallel;
313 ztest_func_t ztest_dmu_object_alloc_free;
314 ztest_func_t ztest_dmu_commit_callbacks;
315 ztest_func_t ztest_zap;
316 ztest_func_t ztest_zap_parallel;
317 ztest_func_t ztest_zil_commit;
318 ztest_func_t ztest_zil_remount;
319 ztest_func_t ztest_dmu_read_write_zcopy;
320 ztest_func_t ztest_dmu_objset_create_destroy;
321 ztest_func_t ztest_dmu_prealloc;
322 ztest_func_t ztest_fzap;
323 ztest_func_t ztest_dmu_snapshot_create_destroy;
324 ztest_func_t ztest_dsl_prop_get_set;
325 ztest_func_t ztest_spa_prop_get_set;
326 ztest_func_t ztest_spa_create_destroy;
327 ztest_func_t ztest_fault_inject;
328 ztest_func_t ztest_ddt_repair;
329 ztest_func_t ztest_dmu_snapshot_hold;
330 ztest_func_t ztest_spa_rename;
331 ztest_func_t ztest_scrub;
332 ztest_func_t ztest_dsl_dataset_promote_busy;
333 ztest_func_t ztest_vdev_attach_detach;
334 ztest_func_t ztest_vdev_LUN_growth;
335 ztest_func_t ztest_vdev_add_remove;
336 ztest_func_t ztest_vdev_aux_add_remove;
337 ztest_func_t ztest_split_pool;
338 ztest_func_t ztest_reguid;
339 ztest_func_t ztest_spa_upgrade;
341 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
342 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
343 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
344 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
345 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
347 ztest_info_t ztest_info[] = {
348 { ztest_dmu_read_write, 1, &zopt_always },
349 { ztest_dmu_write_parallel, 10, &zopt_always },
350 { ztest_dmu_object_alloc_free, 1, &zopt_always },
351 { ztest_dmu_commit_callbacks, 1, &zopt_always },
352 { ztest_zap, 30, &zopt_always },
353 { ztest_zap_parallel, 100, &zopt_always },
354 { ztest_split_pool, 1, &zopt_always },
355 { ztest_zil_commit, 1, &zopt_incessant },
356 { ztest_zil_remount, 1, &zopt_sometimes },
357 { ztest_dmu_read_write_zcopy, 1, &zopt_often },
358 { ztest_dmu_objset_create_destroy, 1, &zopt_often },
359 { ztest_dsl_prop_get_set, 1, &zopt_often },
360 { ztest_spa_prop_get_set, 1, &zopt_sometimes },
362 { ztest_dmu_prealloc, 1, &zopt_sometimes },
364 { ztest_fzap, 1, &zopt_sometimes },
365 { ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes },
366 { ztest_spa_create_destroy, 1, &zopt_sometimes },
367 { ztest_fault_inject, 1, &zopt_sometimes },
368 { ztest_ddt_repair, 1, &zopt_sometimes },
369 { ztest_dmu_snapshot_hold, 1, &zopt_sometimes },
370 { ztest_reguid, 1, &zopt_rarely },
371 { ztest_spa_rename, 1, &zopt_rarely },
372 { ztest_scrub, 1, &zopt_rarely },
373 { ztest_spa_upgrade, 1, &zopt_rarely },
374 { ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
375 { ztest_vdev_attach_detach, 1, &zopt_sometimes },
376 { ztest_vdev_LUN_growth, 1, &zopt_rarely },
377 { ztest_vdev_add_remove, 1,
378 &ztest_opts.zo_vdevtime },
379 { ztest_vdev_aux_add_remove, 1,
380 &ztest_opts.zo_vdevtime },
383 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
386 * The following struct is used to hold a list of uncalled commit callbacks.
387 * The callbacks are ordered by txg number.
389 typedef struct ztest_cb_list {
390 kmutex_t zcl_callbacks_lock;
391 list_t zcl_callbacks;
395 * Stuff we need to share writably between parent and child.
397 typedef struct ztest_shared {
398 boolean_t zs_do_init;
399 hrtime_t zs_proc_start;
400 hrtime_t zs_proc_stop;
401 hrtime_t zs_thread_start;
402 hrtime_t zs_thread_stop;
403 hrtime_t zs_thread_kill;
404 uint64_t zs_enospc_count;
405 uint64_t zs_vdev_next_leaf;
406 uint64_t zs_vdev_aux;
411 uint64_t zs_metaslab_sz;
412 uint64_t zs_metaslab_df_alloc_threshold;
416 #define ID_PARALLEL -1ULL
418 static char ztest_dev_template[] = "%s/%s.%llua";
419 static char ztest_aux_template[] = "%s/%s.%s.%llu";
420 ztest_shared_t *ztest_shared;
422 static spa_t *ztest_spa = NULL;
423 static ztest_ds_t *ztest_ds;
425 static kmutex_t ztest_vdev_lock;
428 * The ztest_name_lock protects the pool and dataset namespace used by
429 * the individual tests. To modify the namespace, consumers must grab
430 * this lock as writer. Grabbing the lock as reader will ensure that the
431 * namespace does not change while the lock is held.
433 static rwlock_t ztest_name_lock;
435 static boolean_t ztest_dump_core = B_TRUE;
436 static boolean_t ztest_exiting;
438 /* Global commit callback list */
439 static ztest_cb_list_t zcl;
440 /* Commit cb delay */
441 static uint64_t zc_min_txg_delay = UINT64_MAX;
442 static int zc_cb_counter = 0;
445 * Minimum number of commit callbacks that need to be registered for us to check
446 * whether the minimum txg delay is acceptable.
448 #define ZTEST_COMMIT_CB_MIN_REG 100
451 * If a number of txgs equal to this threshold have been created after a commit
452 * callback has been registered but not called, then we assume there is an
453 * implementation bug.
455 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
457 extern uint64_t metaslab_gang_bang;
458 extern uint64_t metaslab_df_alloc_threshold;
461 ZTEST_META_DNODE = 0,
466 static void usage(boolean_t) __NORETURN;
469 * These libumem hooks provide a reasonable set of defaults for the allocator's
470 * debugging facilities.
473 _umem_debug_init(void)
475 return ("default,verbose"); /* $UMEM_DEBUG setting */
479 _umem_logging_init(void)
481 return ("fail,contents"); /* $UMEM_LOGGING setting */
484 #define FATAL_MSG_SZ 1024
489 fatal(int do_perror, char *message, ...)
492 int save_errno = errno;
495 (void) fflush(stdout);
496 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
498 va_start(args, message);
499 (void) sprintf(buf, "ztest: ");
501 (void) vsprintf(buf + strlen(buf), message, args);
504 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
505 ": %s", strerror(save_errno));
507 (void) fprintf(stderr, "%s\n", buf);
508 fatal_msg = buf; /* to ease debugging */
515 str2shift(const char *buf)
517 const char *ends = "BKMGTPEZ";
522 for (i = 0; i < strlen(ends); i++) {
523 if (toupper(buf[0]) == ends[i])
526 if (i == strlen(ends)) {
527 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
531 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
534 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
540 nicenumtoull(const char *buf)
545 val = strtoull(buf, &end, 0);
547 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
549 } else if (end[0] == '.') {
550 double fval = strtod(buf, &end);
551 fval *= pow(2, str2shift(end));
552 if (fval > UINT64_MAX) {
553 (void) fprintf(stderr, "ztest: value too large: %s\n",
557 val = (uint64_t)fval;
559 int shift = str2shift(end);
560 if (shift >= 64 || (val << shift) >> shift != val) {
561 (void) fprintf(stderr, "ztest: value too large: %s\n",
571 usage(boolean_t requested)
573 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
575 char nice_vdev_size[10];
576 char nice_gang_bang[10];
577 FILE *fp = requested ? stdout : stderr;
579 nicenum(zo->zo_vdev_size, nice_vdev_size);
580 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
582 (void) fprintf(fp, "Usage: %s\n"
583 "\t[-v vdevs (default: %llu)]\n"
584 "\t[-s size_of_each_vdev (default: %s)]\n"
585 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
586 "\t[-m mirror_copies (default: %d)]\n"
587 "\t[-r raidz_disks (default: %d)]\n"
588 "\t[-R raidz_parity (default: %d)]\n"
589 "\t[-d datasets (default: %d)]\n"
590 "\t[-t threads (default: %d)]\n"
591 "\t[-g gang_block_threshold (default: %s)]\n"
592 "\t[-i init_count (default: %d)] initialize pool i times\n"
593 "\t[-k kill_percentage (default: %llu%%)]\n"
594 "\t[-p pool_name (default: %s)]\n"
595 "\t[-f dir (default: %s)] file directory for vdev files\n"
596 "\t[-V] verbose (use multiple times for ever more blather)\n"
597 "\t[-E] use existing pool instead of creating new one\n"
598 "\t[-T time (default: %llu sec)] total run time\n"
599 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
600 "\t[-P passtime (default: %llu sec)] time per pass\n"
601 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
602 "\t[-h] (print help)\n"
605 (u_longlong_t)zo->zo_vdevs, /* -v */
606 nice_vdev_size, /* -s */
607 zo->zo_ashift, /* -a */
608 zo->zo_mirrors, /* -m */
609 zo->zo_raidz, /* -r */
610 zo->zo_raidz_parity, /* -R */
611 zo->zo_datasets, /* -d */
612 zo->zo_threads, /* -t */
613 nice_gang_bang, /* -g */
614 zo->zo_init, /* -i */
615 (u_longlong_t)zo->zo_killrate, /* -k */
616 zo->zo_pool, /* -p */
618 (u_longlong_t)zo->zo_time, /* -T */
619 (u_longlong_t)zo->zo_maxloops, /* -F */
620 (u_longlong_t)zo->zo_passtime);
621 exit(requested ? 0 : 1);
625 process_options(int argc, char **argv)
628 ztest_shared_opts_t *zo = &ztest_opts;
632 char altdir[MAXNAMELEN] = { 0 };
634 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
636 while ((opt = getopt(argc, argv,
637 "v:s:a:m:r:R:d:t:g:i:k:p:f:VET:P:hF:B:")) != EOF) {
654 value = nicenumtoull(optarg);
658 zo->zo_vdevs = value;
661 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
664 zo->zo_ashift = value;
667 zo->zo_mirrors = value;
670 zo->zo_raidz = MAX(1, value);
673 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
676 zo->zo_datasets = MAX(1, value);
679 zo->zo_threads = MAX(1, value);
682 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
689 zo->zo_killrate = value;
692 (void) strlcpy(zo->zo_pool, optarg,
693 sizeof (zo->zo_pool));
696 path = realpath(optarg, NULL);
698 (void) fprintf(stderr, "error: %s: %s\n",
699 optarg, strerror(errno));
702 (void) strlcpy(zo->zo_dir, path,
703 sizeof (zo->zo_dir));
716 zo->zo_passtime = MAX(1, value);
719 zo->zo_maxloops = MAX(1, value);
722 (void) strlcpy(altdir, optarg, sizeof (altdir));
734 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
737 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
740 if (strlen(altdir) > 0) {
748 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
749 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
751 VERIFY(NULL != realpath(getexecname(), cmd));
752 if (0 != access(altdir, F_OK)) {
753 ztest_dump_core = B_FALSE;
754 fatal(B_TRUE, "invalid alternate ztest path: %s",
757 VERIFY(NULL != realpath(altdir, realaltdir));
760 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
761 * We want to extract <isa> to determine if we should use
762 * 32 or 64 bit binaries.
764 bin = strstr(cmd, "/usr/bin/");
765 ztest = strstr(bin, "/ztest");
767 isalen = ztest - isa;
768 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
769 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
770 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
771 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
773 if (0 != access(zo->zo_alt_ztest, X_OK)) {
774 ztest_dump_core = B_FALSE;
775 fatal(B_TRUE, "invalid alternate ztest: %s",
777 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
778 ztest_dump_core = B_FALSE;
779 fatal(B_TRUE, "invalid alternate lib directory %s",
783 umem_free(cmd, MAXPATHLEN);
784 umem_free(realaltdir, MAXPATHLEN);
789 ztest_kill(ztest_shared_t *zs)
791 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
792 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
795 * Before we kill off ztest, make sure that the config is updated.
796 * See comment above spa_config_sync().
798 mutex_enter(&spa_namespace_lock);
799 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
800 mutex_exit(&spa_namespace_lock);
802 if (ztest_opts.zo_verbose >= 3)
803 zfs_dbgmsg_print(FTAG);
805 (void) kill(getpid(), SIGKILL);
809 ztest_random(uint64_t range)
813 ASSERT3S(ztest_fd_rand, >=, 0);
818 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
819 fatal(1, "short read from /dev/urandom");
826 ztest_record_enospc(const char *s)
828 ztest_shared->zs_enospc_count++;
832 ztest_get_ashift(void)
834 if (ztest_opts.zo_ashift == 0)
835 return (SPA_MINBLOCKSHIFT + ztest_random(3));
836 return (ztest_opts.zo_ashift);
840 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
846 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
849 ashift = ztest_get_ashift();
855 vdev = ztest_shared->zs_vdev_aux;
856 (void) snprintf(path, MAXPATHLEN,
857 ztest_aux_template, ztest_opts.zo_dir,
858 pool == NULL ? ztest_opts.zo_pool : pool,
861 vdev = ztest_shared->zs_vdev_next_leaf++;
862 (void) snprintf(path, MAXPATHLEN,
863 ztest_dev_template, ztest_opts.zo_dir,
864 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
869 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
871 fatal(1, "can't open %s", path);
872 if (ftruncate(fd, size) != 0)
873 fatal(1, "can't ftruncate %s", path);
877 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
878 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
879 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
880 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
881 umem_free(pathbuf, MAXPATHLEN);
887 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
888 uint64_t ashift, int r)
890 nvlist_t *raidz, **child;
894 return (make_vdev_file(path, aux, pool, size, ashift));
895 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
897 for (c = 0; c < r; c++)
898 child[c] = make_vdev_file(path, aux, pool, size, ashift);
900 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
901 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
902 VDEV_TYPE_RAIDZ) == 0);
903 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
904 ztest_opts.zo_raidz_parity) == 0);
905 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
908 for (c = 0; c < r; c++)
909 nvlist_free(child[c]);
911 umem_free(child, r * sizeof (nvlist_t *));
917 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
918 uint64_t ashift, int r, int m)
920 nvlist_t *mirror, **child;
924 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
926 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
928 for (c = 0; c < m; c++)
929 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
931 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
932 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
933 VDEV_TYPE_MIRROR) == 0);
934 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
937 for (c = 0; c < m; c++)
938 nvlist_free(child[c]);
940 umem_free(child, m * sizeof (nvlist_t *));
946 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
947 int log, int r, int m, int t)
949 nvlist_t *root, **child;
954 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
956 for (c = 0; c < t; c++) {
957 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
959 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
963 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
964 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
965 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
968 for (c = 0; c < t; c++)
969 nvlist_free(child[c]);
971 umem_free(child, t * sizeof (nvlist_t *));
977 * Find a random spa version. Returns back a random spa version in the
978 * range [initial_version, SPA_VERSION_FEATURES].
981 ztest_random_spa_version(uint64_t initial_version)
983 uint64_t version = initial_version;
985 if (version <= SPA_VERSION_BEFORE_FEATURES) {
987 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
990 if (version > SPA_VERSION_BEFORE_FEATURES)
991 version = SPA_VERSION_FEATURES;
993 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
998 ztest_random_blocksize(void)
1000 return (1 << (SPA_MINBLOCKSHIFT +
1001 ztest_random(SPA_MAXBLOCKSHIFT - SPA_MINBLOCKSHIFT + 1)));
1005 ztest_random_ibshift(void)
1007 return (DN_MIN_INDBLKSHIFT +
1008 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1012 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1015 vdev_t *rvd = spa->spa_root_vdev;
1018 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1021 top = ztest_random(rvd->vdev_children);
1022 tvd = rvd->vdev_child[top];
1023 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1024 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1030 ztest_random_dsl_prop(zfs_prop_t prop)
1035 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1036 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1042 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1045 const char *propname = zfs_prop_to_name(prop);
1046 const char *valname;
1051 error = dsl_prop_set_int(osname, propname,
1052 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1054 if (error == ENOSPC) {
1055 ztest_record_enospc(FTAG);
1060 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1061 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1063 if (ztest_opts.zo_verbose >= 6) {
1064 VERIFY(zfs_prop_index_to_string(prop, curval, &valname) == 0);
1065 (void) printf("%s %s = %s at '%s'\n",
1066 osname, propname, valname, setpoint);
1068 umem_free(setpoint, MAXPATHLEN);
1074 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1076 spa_t *spa = ztest_spa;
1077 nvlist_t *props = NULL;
1080 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1081 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1083 error = spa_prop_set(spa, props);
1087 if (error == ENOSPC) {
1088 ztest_record_enospc(FTAG);
1097 ztest_rll_init(rll_t *rll)
1099 rll->rll_writer = NULL;
1100 rll->rll_readers = 0;
1101 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1102 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1106 ztest_rll_destroy(rll_t *rll)
1108 ASSERT(rll->rll_writer == NULL);
1109 ASSERT(rll->rll_readers == 0);
1110 mutex_destroy(&rll->rll_lock);
1111 cv_destroy(&rll->rll_cv);
1115 ztest_rll_lock(rll_t *rll, rl_type_t type)
1117 mutex_enter(&rll->rll_lock);
1119 if (type == RL_READER) {
1120 while (rll->rll_writer != NULL)
1121 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1124 while (rll->rll_writer != NULL || rll->rll_readers)
1125 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1126 rll->rll_writer = curthread;
1129 mutex_exit(&rll->rll_lock);
1133 ztest_rll_unlock(rll_t *rll)
1135 mutex_enter(&rll->rll_lock);
1137 if (rll->rll_writer) {
1138 ASSERT(rll->rll_readers == 0);
1139 rll->rll_writer = NULL;
1141 ASSERT(rll->rll_readers != 0);
1142 ASSERT(rll->rll_writer == NULL);
1146 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1147 cv_broadcast(&rll->rll_cv);
1149 mutex_exit(&rll->rll_lock);
1153 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1155 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1157 ztest_rll_lock(rll, type);
1161 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1163 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1165 ztest_rll_unlock(rll);
1169 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1170 uint64_t size, rl_type_t type)
1172 uint64_t hash = object ^ (offset % (ZTEST_RANGE_LOCKS + 1));
1173 rll_t *rll = &zd->zd_range_lock[hash & (ZTEST_RANGE_LOCKS - 1)];
1176 rl = umem_alloc(sizeof (*rl), UMEM_NOFAIL);
1177 rl->rl_object = object;
1178 rl->rl_offset = offset;
1182 ztest_rll_lock(rll, type);
1188 ztest_range_unlock(rl_t *rl)
1190 rll_t *rll = rl->rl_lock;
1192 ztest_rll_unlock(rll);
1194 umem_free(rl, sizeof (*rl));
1198 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1201 zd->zd_zilog = dmu_objset_zil(os);
1202 zd->zd_shared = szd;
1203 dmu_objset_name(os, zd->zd_name);
1206 if (zd->zd_shared != NULL)
1207 zd->zd_shared->zd_seq = 0;
1209 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1210 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1212 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1213 ztest_rll_init(&zd->zd_object_lock[l]);
1215 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1216 ztest_rll_init(&zd->zd_range_lock[l]);
1220 ztest_zd_fini(ztest_ds_t *zd)
1224 mutex_destroy(&zd->zd_dirobj_lock);
1225 (void) rwlock_destroy(&zd->zd_zilog_lock);
1227 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1228 ztest_rll_destroy(&zd->zd_object_lock[l]);
1230 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1231 ztest_rll_destroy(&zd->zd_range_lock[l]);
1234 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1237 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1243 * Attempt to assign tx to some transaction group.
1245 error = dmu_tx_assign(tx, txg_how);
1247 if (error == ERESTART) {
1248 ASSERT(txg_how == TXG_NOWAIT);
1251 ASSERT3U(error, ==, ENOSPC);
1252 ztest_record_enospc(tag);
1257 txg = dmu_tx_get_txg(tx);
1263 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1266 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1274 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1277 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1281 diff |= (value - *ip++);
1288 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1289 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1291 bt->bt_magic = BT_MAGIC;
1292 bt->bt_objset = dmu_objset_id(os);
1293 bt->bt_object = object;
1294 bt->bt_offset = offset;
1297 bt->bt_crtxg = crtxg;
1301 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1302 uint64_t offset, uint64_t gen, uint64_t txg, uint64_t crtxg)
1304 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1305 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1306 ASSERT3U(bt->bt_object, ==, object);
1307 ASSERT3U(bt->bt_offset, ==, offset);
1308 ASSERT3U(bt->bt_gen, <=, gen);
1309 ASSERT3U(bt->bt_txg, <=, txg);
1310 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1313 static ztest_block_tag_t *
1314 ztest_bt_bonus(dmu_buf_t *db)
1316 dmu_object_info_t doi;
1317 ztest_block_tag_t *bt;
1319 dmu_object_info_from_db(db, &doi);
1320 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1321 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1322 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1331 #define lrz_type lr_mode
1332 #define lrz_blocksize lr_uid
1333 #define lrz_ibshift lr_gid
1334 #define lrz_bonustype lr_rdev
1335 #define lrz_bonuslen lr_crtime[1]
1338 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1340 char *name = (void *)(lr + 1); /* name follows lr */
1341 size_t namesize = strlen(name) + 1;
1344 if (zil_replaying(zd->zd_zilog, tx))
1347 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1348 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1349 sizeof (*lr) + namesize - sizeof (lr_t));
1351 zil_itx_assign(zd->zd_zilog, itx, tx);
1355 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1357 char *name = (void *)(lr + 1); /* name follows lr */
1358 size_t namesize = strlen(name) + 1;
1361 if (zil_replaying(zd->zd_zilog, tx))
1364 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1365 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1366 sizeof (*lr) + namesize - sizeof (lr_t));
1368 itx->itx_oid = object;
1369 zil_itx_assign(zd->zd_zilog, itx, tx);
1373 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1376 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1378 if (zil_replaying(zd->zd_zilog, tx))
1381 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1382 write_state = WR_INDIRECT;
1384 itx = zil_itx_create(TX_WRITE,
1385 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1387 if (write_state == WR_COPIED &&
1388 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1389 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1390 zil_itx_destroy(itx);
1391 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1392 write_state = WR_NEED_COPY;
1394 itx->itx_private = zd;
1395 itx->itx_wr_state = write_state;
1396 itx->itx_sync = (ztest_random(8) == 0);
1397 itx->itx_sod += (write_state == WR_NEED_COPY ? lr->lr_length : 0);
1399 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1400 sizeof (*lr) - sizeof (lr_t));
1402 zil_itx_assign(zd->zd_zilog, itx, tx);
1406 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1410 if (zil_replaying(zd->zd_zilog, tx))
1413 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1414 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1415 sizeof (*lr) - sizeof (lr_t));
1417 itx->itx_sync = B_FALSE;
1418 zil_itx_assign(zd->zd_zilog, itx, tx);
1422 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1426 if (zil_replaying(zd->zd_zilog, tx))
1429 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1430 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1431 sizeof (*lr) - sizeof (lr_t));
1433 itx->itx_sync = B_FALSE;
1434 zil_itx_assign(zd->zd_zilog, itx, tx);
1441 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1443 char *name = (void *)(lr + 1); /* name follows lr */
1444 objset_t *os = zd->zd_os;
1445 ztest_block_tag_t *bbt;
1452 byteswap_uint64_array(lr, sizeof (*lr));
1454 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1455 ASSERT(name[0] != '\0');
1457 tx = dmu_tx_create(os);
1459 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1461 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1462 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1464 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1467 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1471 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1473 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1474 if (lr->lr_foid == 0) {
1475 lr->lr_foid = zap_create(os,
1476 lr->lrz_type, lr->lrz_bonustype,
1477 lr->lrz_bonuslen, tx);
1479 error = zap_create_claim(os, lr->lr_foid,
1480 lr->lrz_type, lr->lrz_bonustype,
1481 lr->lrz_bonuslen, tx);
1484 if (lr->lr_foid == 0) {
1485 lr->lr_foid = dmu_object_alloc(os,
1486 lr->lrz_type, 0, lr->lrz_bonustype,
1487 lr->lrz_bonuslen, tx);
1489 error = dmu_object_claim(os, lr->lr_foid,
1490 lr->lrz_type, 0, lr->lrz_bonustype,
1491 lr->lrz_bonuslen, tx);
1496 ASSERT3U(error, ==, EEXIST);
1497 ASSERT(zd->zd_zilog->zl_replay);
1502 ASSERT(lr->lr_foid != 0);
1504 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1505 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1506 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1508 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1509 bbt = ztest_bt_bonus(db);
1510 dmu_buf_will_dirty(db, tx);
1511 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_gen, txg, txg);
1512 dmu_buf_rele(db, FTAG);
1514 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1517 (void) ztest_log_create(zd, tx, lr);
1525 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1527 char *name = (void *)(lr + 1); /* name follows lr */
1528 objset_t *os = zd->zd_os;
1529 dmu_object_info_t doi;
1531 uint64_t object, txg;
1534 byteswap_uint64_array(lr, sizeof (*lr));
1536 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1537 ASSERT(name[0] != '\0');
1540 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1541 ASSERT(object != 0);
1543 ztest_object_lock(zd, object, RL_WRITER);
1545 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1547 tx = dmu_tx_create(os);
1549 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1550 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1552 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1554 ztest_object_unlock(zd, object);
1558 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1559 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1561 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1564 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1566 (void) ztest_log_remove(zd, tx, lr, object);
1570 ztest_object_unlock(zd, object);
1576 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1578 objset_t *os = zd->zd_os;
1579 void *data = lr + 1; /* data follows lr */
1580 uint64_t offset, length;
1581 ztest_block_tag_t *bt = data;
1582 ztest_block_tag_t *bbt;
1583 uint64_t gen, txg, lrtxg, crtxg;
1584 dmu_object_info_t doi;
1587 arc_buf_t *abuf = NULL;
1591 byteswap_uint64_array(lr, sizeof (*lr));
1593 offset = lr->lr_offset;
1594 length = lr->lr_length;
1596 /* If it's a dmu_sync() block, write the whole block */
1597 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1598 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1599 if (length < blocksize) {
1600 offset -= offset % blocksize;
1605 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1606 byteswap_uint64_array(bt, sizeof (*bt));
1608 if (bt->bt_magic != BT_MAGIC)
1611 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1612 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1614 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1616 dmu_object_info_from_db(db, &doi);
1618 bbt = ztest_bt_bonus(db);
1619 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1621 crtxg = bbt->bt_crtxg;
1622 lrtxg = lr->lr_common.lrc_txg;
1624 tx = dmu_tx_create(os);
1626 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1628 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1629 P2PHASE(offset, length) == 0)
1630 abuf = dmu_request_arcbuf(db, length);
1632 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1635 dmu_return_arcbuf(abuf);
1636 dmu_buf_rele(db, FTAG);
1637 ztest_range_unlock(rl);
1638 ztest_object_unlock(zd, lr->lr_foid);
1644 * Usually, verify the old data before writing new data --
1645 * but not always, because we also want to verify correct
1646 * behavior when the data was not recently read into cache.
1648 ASSERT(offset % doi.doi_data_block_size == 0);
1649 if (ztest_random(4) != 0) {
1650 int prefetch = ztest_random(2) ?
1651 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1652 ztest_block_tag_t rbt;
1654 VERIFY(dmu_read(os, lr->lr_foid, offset,
1655 sizeof (rbt), &rbt, prefetch) == 0);
1656 if (rbt.bt_magic == BT_MAGIC) {
1657 ztest_bt_verify(&rbt, os, lr->lr_foid,
1658 offset, gen, txg, crtxg);
1663 * Writes can appear to be newer than the bonus buffer because
1664 * the ztest_get_data() callback does a dmu_read() of the
1665 * open-context data, which may be different than the data
1666 * as it was when the write was generated.
1668 if (zd->zd_zilog->zl_replay) {
1669 ztest_bt_verify(bt, os, lr->lr_foid, offset,
1670 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1675 * Set the bt's gen/txg to the bonus buffer's gen/txg
1676 * so that all of the usual ASSERTs will work.
1678 ztest_bt_generate(bt, os, lr->lr_foid, offset, gen, txg, crtxg);
1682 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1684 bcopy(data, abuf->b_data, length);
1685 dmu_assign_arcbuf(db, offset, abuf, tx);
1688 (void) ztest_log_write(zd, tx, lr);
1690 dmu_buf_rele(db, FTAG);
1694 ztest_range_unlock(rl);
1695 ztest_object_unlock(zd, lr->lr_foid);
1701 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1703 objset_t *os = zd->zd_os;
1709 byteswap_uint64_array(lr, sizeof (*lr));
1711 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1712 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1715 tx = dmu_tx_create(os);
1717 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1719 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1721 ztest_range_unlock(rl);
1722 ztest_object_unlock(zd, lr->lr_foid);
1726 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1727 lr->lr_length, tx) == 0);
1729 (void) ztest_log_truncate(zd, tx, lr);
1733 ztest_range_unlock(rl);
1734 ztest_object_unlock(zd, lr->lr_foid);
1740 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1742 objset_t *os = zd->zd_os;
1745 ztest_block_tag_t *bbt;
1746 uint64_t txg, lrtxg, crtxg;
1749 byteswap_uint64_array(lr, sizeof (*lr));
1751 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1753 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1755 tx = dmu_tx_create(os);
1756 dmu_tx_hold_bonus(tx, lr->lr_foid);
1758 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1760 dmu_buf_rele(db, FTAG);
1761 ztest_object_unlock(zd, lr->lr_foid);
1765 bbt = ztest_bt_bonus(db);
1766 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1767 crtxg = bbt->bt_crtxg;
1768 lrtxg = lr->lr_common.lrc_txg;
1770 if (zd->zd_zilog->zl_replay) {
1771 ASSERT(lr->lr_size != 0);
1772 ASSERT(lr->lr_mode != 0);
1776 * Randomly change the size and increment the generation.
1778 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
1780 lr->lr_mode = bbt->bt_gen + 1;
1785 * Verify that the current bonus buffer is not newer than our txg.
1787 ztest_bt_verify(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode,
1788 MAX(txg, lrtxg), crtxg);
1790 dmu_buf_will_dirty(db, tx);
1792 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
1793 ASSERT3U(lr->lr_size, <=, db->db_size);
1794 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
1795 bbt = ztest_bt_bonus(db);
1797 ztest_bt_generate(bbt, os, lr->lr_foid, -1ULL, lr->lr_mode, txg, crtxg);
1799 dmu_buf_rele(db, FTAG);
1801 (void) ztest_log_setattr(zd, tx, lr);
1805 ztest_object_unlock(zd, lr->lr_foid);
1810 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
1811 NULL, /* 0 no such transaction type */
1812 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
1813 NULL, /* TX_MKDIR */
1814 NULL, /* TX_MKXATTR */
1815 NULL, /* TX_SYMLINK */
1816 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
1817 NULL, /* TX_RMDIR */
1819 NULL, /* TX_RENAME */
1820 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
1821 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
1822 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
1824 NULL, /* TX_CREATE_ACL */
1825 NULL, /* TX_CREATE_ATTR */
1826 NULL, /* TX_CREATE_ACL_ATTR */
1827 NULL, /* TX_MKDIR_ACL */
1828 NULL, /* TX_MKDIR_ATTR */
1829 NULL, /* TX_MKDIR_ACL_ATTR */
1830 NULL, /* TX_WRITE2 */
1834 * ZIL get_data callbacks
1838 ztest_get_done(zgd_t *zgd, int error)
1840 ztest_ds_t *zd = zgd->zgd_private;
1841 uint64_t object = zgd->zgd_rl->rl_object;
1844 dmu_buf_rele(zgd->zgd_db, zgd);
1846 ztest_range_unlock(zgd->zgd_rl);
1847 ztest_object_unlock(zd, object);
1849 if (error == 0 && zgd->zgd_bp)
1850 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1852 umem_free(zgd, sizeof (*zgd));
1856 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1858 ztest_ds_t *zd = arg;
1859 objset_t *os = zd->zd_os;
1860 uint64_t object = lr->lr_foid;
1861 uint64_t offset = lr->lr_offset;
1862 uint64_t size = lr->lr_length;
1863 blkptr_t *bp = &lr->lr_blkptr;
1864 uint64_t txg = lr->lr_common.lrc_txg;
1866 dmu_object_info_t doi;
1871 ztest_object_lock(zd, object, RL_READER);
1872 error = dmu_bonus_hold(os, object, FTAG, &db);
1874 ztest_object_unlock(zd, object);
1878 crtxg = ztest_bt_bonus(db)->bt_crtxg;
1880 if (crtxg == 0 || crtxg > txg) {
1881 dmu_buf_rele(db, FTAG);
1882 ztest_object_unlock(zd, object);
1886 dmu_object_info_from_db(db, &doi);
1887 dmu_buf_rele(db, FTAG);
1890 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
1891 zgd->zgd_zilog = zd->zd_zilog;
1892 zgd->zgd_private = zd;
1894 if (buf != NULL) { /* immediate write */
1895 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1898 error = dmu_read(os, object, offset, size, buf,
1899 DMU_READ_NO_PREFETCH);
1902 size = doi.doi_data_block_size;
1904 offset = P2ALIGN(offset, size);
1906 ASSERT(offset < size);
1910 zgd->zgd_rl = ztest_range_lock(zd, object, offset, size,
1913 error = dmu_buf_hold(os, object, offset, zgd, &db,
1914 DMU_READ_NO_PREFETCH);
1917 blkptr_t *obp = dmu_buf_get_blkptr(db);
1919 ASSERT(BP_IS_HOLE(bp));
1926 ASSERT(db->db_offset == offset);
1927 ASSERT(db->db_size == size);
1929 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1930 ztest_get_done, zgd);
1937 ztest_get_done(zgd, error);
1943 ztest_lr_alloc(size_t lrsize, char *name)
1946 size_t namesize = name ? strlen(name) + 1 : 0;
1948 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
1951 bcopy(name, lr + lrsize, namesize);
1957 ztest_lr_free(void *lr, size_t lrsize, char *name)
1959 size_t namesize = name ? strlen(name) + 1 : 0;
1961 umem_free(lr, lrsize + namesize);
1965 * Lookup a bunch of objects. Returns the number of objects not found.
1968 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
1974 ASSERT(mutex_held(&zd->zd_dirobj_lock));
1976 for (i = 0; i < count; i++, od++) {
1978 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
1979 sizeof (uint64_t), 1, &od->od_object);
1981 ASSERT(error == ENOENT);
1982 ASSERT(od->od_object == 0);
1986 ztest_block_tag_t *bbt;
1987 dmu_object_info_t doi;
1989 ASSERT(od->od_object != 0);
1990 ASSERT(missing == 0); /* there should be no gaps */
1992 ztest_object_lock(zd, od->od_object, RL_READER);
1993 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
1994 od->od_object, FTAG, &db));
1995 dmu_object_info_from_db(db, &doi);
1996 bbt = ztest_bt_bonus(db);
1997 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1998 od->od_type = doi.doi_type;
1999 od->od_blocksize = doi.doi_data_block_size;
2000 od->od_gen = bbt->bt_gen;
2001 dmu_buf_rele(db, FTAG);
2002 ztest_object_unlock(zd, od->od_object);
2010 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2015 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2017 for (i = 0; i < count; i++, od++) {
2024 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2026 lr->lr_doid = od->od_dir;
2027 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2028 lr->lrz_type = od->od_crtype;
2029 lr->lrz_blocksize = od->od_crblocksize;
2030 lr->lrz_ibshift = ztest_random_ibshift();
2031 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2032 lr->lrz_bonuslen = dmu_bonus_max();
2033 lr->lr_gen = od->od_crgen;
2034 lr->lr_crtime[0] = time(NULL);
2036 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2037 ASSERT(missing == 0);
2041 od->od_object = lr->lr_foid;
2042 od->od_type = od->od_crtype;
2043 od->od_blocksize = od->od_crblocksize;
2044 od->od_gen = od->od_crgen;
2045 ASSERT(od->od_object != 0);
2048 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2055 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2061 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2065 for (i = count - 1; i >= 0; i--, od--) {
2072 * No object was found.
2074 if (od->od_object == 0)
2077 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2079 lr->lr_doid = od->od_dir;
2081 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2082 ASSERT3U(error, ==, ENOSPC);
2087 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2094 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2100 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2102 lr->lr_foid = object;
2103 lr->lr_offset = offset;
2104 lr->lr_length = size;
2106 BP_ZERO(&lr->lr_blkptr);
2108 bcopy(data, lr + 1, size);
2110 error = ztest_replay_write(zd, lr, B_FALSE);
2112 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2118 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2123 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2125 lr->lr_foid = object;
2126 lr->lr_offset = offset;
2127 lr->lr_length = size;
2129 error = ztest_replay_truncate(zd, lr, B_FALSE);
2131 ztest_lr_free(lr, sizeof (*lr), NULL);
2137 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2142 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2144 lr->lr_foid = object;
2148 error = ztest_replay_setattr(zd, lr, B_FALSE);
2150 ztest_lr_free(lr, sizeof (*lr), NULL);
2156 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2158 objset_t *os = zd->zd_os;
2163 txg_wait_synced(dmu_objset_pool(os), 0);
2165 ztest_object_lock(zd, object, RL_READER);
2166 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2168 tx = dmu_tx_create(os);
2170 dmu_tx_hold_write(tx, object, offset, size);
2172 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2175 dmu_prealloc(os, object, offset, size, tx);
2177 txg_wait_synced(dmu_objset_pool(os), txg);
2179 (void) dmu_free_long_range(os, object, offset, size);
2182 ztest_range_unlock(rl);
2183 ztest_object_unlock(zd, object);
2187 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2190 ztest_block_tag_t wbt;
2191 dmu_object_info_t doi;
2192 enum ztest_io_type io_type;
2196 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2197 blocksize = doi.doi_data_block_size;
2198 data = umem_alloc(blocksize, UMEM_NOFAIL);
2201 * Pick an i/o type at random, biased toward writing block tags.
2203 io_type = ztest_random(ZTEST_IO_TYPES);
2204 if (ztest_random(2) == 0)
2205 io_type = ZTEST_IO_WRITE_TAG;
2207 (void) rw_rdlock(&zd->zd_zilog_lock);
2211 case ZTEST_IO_WRITE_TAG:
2212 ztest_bt_generate(&wbt, zd->zd_os, object, offset, 0, 0, 0);
2213 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2216 case ZTEST_IO_WRITE_PATTERN:
2217 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2218 if (ztest_random(2) == 0) {
2220 * Induce fletcher2 collisions to ensure that
2221 * zio_ddt_collision() detects and resolves them
2222 * when using fletcher2-verify for deduplication.
2224 ((uint64_t *)data)[0] ^= 1ULL << 63;
2225 ((uint64_t *)data)[4] ^= 1ULL << 63;
2227 (void) ztest_write(zd, object, offset, blocksize, data);
2230 case ZTEST_IO_WRITE_ZEROES:
2231 bzero(data, blocksize);
2232 (void) ztest_write(zd, object, offset, blocksize, data);
2235 case ZTEST_IO_TRUNCATE:
2236 (void) ztest_truncate(zd, object, offset, blocksize);
2239 case ZTEST_IO_SETATTR:
2240 (void) ztest_setattr(zd, object);
2245 case ZTEST_IO_REWRITE:
2246 (void) rw_rdlock(&ztest_name_lock);
2247 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2248 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2250 VERIFY(err == 0 || err == ENOSPC);
2251 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2252 ZFS_PROP_COMPRESSION,
2253 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2255 VERIFY(err == 0 || err == ENOSPC);
2256 (void) rw_unlock(&ztest_name_lock);
2258 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2259 DMU_READ_NO_PREFETCH));
2261 (void) ztest_write(zd, object, offset, blocksize, data);
2265 (void) rw_unlock(&zd->zd_zilog_lock);
2267 umem_free(data, blocksize);
2271 * Initialize an object description template.
2274 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2275 dmu_object_type_t type, uint64_t blocksize, uint64_t gen)
2277 od->od_dir = ZTEST_DIROBJ;
2280 od->od_crtype = type;
2281 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2284 od->od_type = DMU_OT_NONE;
2285 od->od_blocksize = 0;
2288 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2289 tag, (longlong_t)id, (u_longlong_t)index);
2293 * Lookup or create the objects for a test using the od template.
2294 * If the objects do not all exist, or if 'remove' is specified,
2295 * remove any existing objects and create new ones. Otherwise,
2296 * use the existing objects.
2299 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2301 int count = size / sizeof (*od);
2304 mutex_enter(&zd->zd_dirobj_lock);
2305 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2306 (ztest_remove(zd, od, count) != 0 ||
2307 ztest_create(zd, od, count) != 0))
2310 mutex_exit(&zd->zd_dirobj_lock);
2317 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2319 zilog_t *zilog = zd->zd_zilog;
2321 (void) rw_rdlock(&zd->zd_zilog_lock);
2323 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2326 * Remember the committed values in zd, which is in parent/child
2327 * shared memory. If we die, the next iteration of ztest_run()
2328 * will verify that the log really does contain this record.
2330 mutex_enter(&zilog->zl_lock);
2331 ASSERT(zd->zd_shared != NULL);
2332 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2333 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2334 mutex_exit(&zilog->zl_lock);
2336 (void) rw_unlock(&zd->zd_zilog_lock);
2340 * This function is designed to simulate the operations that occur during a
2341 * mount/unmount operation. We hold the dataset across these operations in an
2342 * attempt to expose any implicit assumptions about ZIL management.
2346 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2348 objset_t *os = zd->zd_os;
2351 * We grab the zd_dirobj_lock to ensure that no other thread is
2352 * updating the zil (i.e. adding in-memory log records) and the
2353 * zd_zilog_lock to block any I/O.
2355 mutex_enter(&zd->zd_dirobj_lock);
2356 (void) rw_wrlock(&zd->zd_zilog_lock);
2358 /* zfs_sb_teardown() */
2359 zil_close(zd->zd_zilog);
2361 /* zfsvfs_setup() */
2362 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2363 zil_replay(os, zd, ztest_replay_vector);
2365 (void) rw_unlock(&zd->zd_zilog_lock);
2366 mutex_exit(&zd->zd_dirobj_lock);
2370 * Verify that we can't destroy an active pool, create an existing pool,
2371 * or create a pool with a bad vdev spec.
2375 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2377 ztest_shared_opts_t *zo = &ztest_opts;
2382 * Attempt to create using a bad file.
2384 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2385 VERIFY3U(ENOENT, ==,
2386 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2387 nvlist_free(nvroot);
2390 * Attempt to create using a bad mirror.
2392 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2393 VERIFY3U(ENOENT, ==,
2394 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2395 nvlist_free(nvroot);
2398 * Attempt to create an existing pool. It shouldn't matter
2399 * what's in the nvroot; we should fail with EEXIST.
2401 (void) rw_rdlock(&ztest_name_lock);
2402 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2403 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2404 nvlist_free(nvroot);
2405 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2406 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2407 spa_close(spa, FTAG);
2409 (void) rw_unlock(&ztest_name_lock);
2414 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2417 uint64_t initial_version = SPA_VERSION_INITIAL;
2418 uint64_t version, newversion;
2419 nvlist_t *nvroot, *props;
2422 mutex_enter(&ztest_vdev_lock);
2423 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2426 * Clean up from previous runs.
2428 (void) spa_destroy(name);
2430 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2431 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2434 * If we're configuring a RAIDZ device then make sure that the
2435 * the initial version is capable of supporting that feature.
2437 switch (ztest_opts.zo_raidz_parity) {
2440 initial_version = SPA_VERSION_INITIAL;
2443 initial_version = SPA_VERSION_RAIDZ2;
2446 initial_version = SPA_VERSION_RAIDZ3;
2451 * Create a pool with a spa version that can be upgraded. Pick
2452 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2455 version = ztest_random_spa_version(initial_version);
2456 } while (version > SPA_VERSION_BEFORE_FEATURES);
2458 props = fnvlist_alloc();
2459 fnvlist_add_uint64(props,
2460 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2461 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2462 fnvlist_free(nvroot);
2463 fnvlist_free(props);
2465 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2466 VERIFY3U(spa_version(spa), ==, version);
2467 newversion = ztest_random_spa_version(version + 1);
2469 if (ztest_opts.zo_verbose >= 4) {
2470 (void) printf("upgrading spa version from %llu to %llu\n",
2471 (u_longlong_t)version, (u_longlong_t)newversion);
2474 spa_upgrade(spa, newversion);
2475 VERIFY3U(spa_version(spa), >, version);
2476 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2477 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2478 spa_close(spa, FTAG);
2481 mutex_exit(&ztest_vdev_lock);
2485 vdev_lookup_by_path(vdev_t *vd, const char *path)
2490 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2493 for (c = 0; c < vd->vdev_children; c++)
2494 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2502 * Find the first available hole which can be used as a top-level.
2505 find_vdev_hole(spa_t *spa)
2507 vdev_t *rvd = spa->spa_root_vdev;
2510 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2512 for (c = 0; c < rvd->vdev_children; c++) {
2513 vdev_t *cvd = rvd->vdev_child[c];
2515 if (cvd->vdev_ishole)
2522 * Verify that vdev_add() works as expected.
2526 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2528 ztest_shared_t *zs = ztest_shared;
2529 spa_t *spa = ztest_spa;
2535 mutex_enter(&ztest_vdev_lock);
2536 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2538 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2540 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2543 * If we have slogs then remove them 1/4 of the time.
2545 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2547 * Grab the guid from the head of the log class rotor.
2549 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2551 spa_config_exit(spa, SCL_VDEV, FTAG);
2554 * We have to grab the zs_name_lock as writer to
2555 * prevent a race between removing a slog (dmu_objset_find)
2556 * and destroying a dataset. Removing the slog will
2557 * grab a reference on the dataset which may cause
2558 * dsl_destroy_head() to fail with EBUSY thus
2559 * leaving the dataset in an inconsistent state.
2561 rw_wrlock(&ztest_name_lock);
2562 error = spa_vdev_remove(spa, guid, B_FALSE);
2563 rw_unlock(&ztest_name_lock);
2565 if (error && error != EEXIST)
2566 fatal(0, "spa_vdev_remove() = %d", error);
2568 spa_config_exit(spa, SCL_VDEV, FTAG);
2571 * Make 1/4 of the devices be log devices.
2573 nvroot = make_vdev_root(NULL, NULL, NULL,
2574 ztest_opts.zo_vdev_size, 0,
2575 ztest_random(4) == 0, ztest_opts.zo_raidz,
2578 error = spa_vdev_add(spa, nvroot);
2579 nvlist_free(nvroot);
2581 if (error == ENOSPC)
2582 ztest_record_enospc("spa_vdev_add");
2583 else if (error != 0)
2584 fatal(0, "spa_vdev_add() = %d", error);
2587 mutex_exit(&ztest_vdev_lock);
2591 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2595 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2597 ztest_shared_t *zs = ztest_shared;
2598 spa_t *spa = ztest_spa;
2599 vdev_t *rvd = spa->spa_root_vdev;
2600 spa_aux_vdev_t *sav;
2606 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2608 if (ztest_random(2) == 0) {
2609 sav = &spa->spa_spares;
2610 aux = ZPOOL_CONFIG_SPARES;
2612 sav = &spa->spa_l2cache;
2613 aux = ZPOOL_CONFIG_L2CACHE;
2616 mutex_enter(&ztest_vdev_lock);
2618 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2620 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2622 * Pick a random device to remove.
2624 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2627 * Find an unused device we can add.
2629 zs->zs_vdev_aux = 0;
2632 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2633 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2635 for (c = 0; c < sav->sav_count; c++)
2636 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2639 if (c == sav->sav_count &&
2640 vdev_lookup_by_path(rvd, path) == NULL)
2646 spa_config_exit(spa, SCL_VDEV, FTAG);
2652 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2653 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2654 error = spa_vdev_add(spa, nvroot);
2656 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2657 nvlist_free(nvroot);
2660 * Remove an existing device. Sometimes, dirty its
2661 * vdev state first to make sure we handle removal
2662 * of devices that have pending state changes.
2664 if (ztest_random(2) == 0)
2665 (void) vdev_online(spa, guid, 0, NULL);
2667 error = spa_vdev_remove(spa, guid, B_FALSE);
2668 if (error != 0 && error != EBUSY)
2669 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2672 mutex_exit(&ztest_vdev_lock);
2674 umem_free(path, MAXPATHLEN);
2678 * split a pool if it has mirror tlvdevs
2682 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2684 ztest_shared_t *zs = ztest_shared;
2685 spa_t *spa = ztest_spa;
2686 vdev_t *rvd = spa->spa_root_vdev;
2687 nvlist_t *tree, **child, *config, *split, **schild;
2688 uint_t c, children, schildren = 0, lastlogid = 0;
2691 mutex_enter(&ztest_vdev_lock);
2693 /* ensure we have a useable config; mirrors of raidz aren't supported */
2694 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2695 mutex_exit(&ztest_vdev_lock);
2699 /* clean up the old pool, if any */
2700 (void) spa_destroy("splitp");
2702 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2704 /* generate a config from the existing config */
2705 mutex_enter(&spa->spa_props_lock);
2706 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2708 mutex_exit(&spa->spa_props_lock);
2710 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2713 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2714 for (c = 0; c < children; c++) {
2715 vdev_t *tvd = rvd->vdev_child[c];
2719 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2720 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2722 VERIFY(nvlist_add_string(schild[schildren],
2723 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2724 VERIFY(nvlist_add_uint64(schild[schildren],
2725 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2727 lastlogid = schildren;
2732 VERIFY(nvlist_lookup_nvlist_array(child[c],
2733 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2734 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
2737 /* OK, create a config that can be used to split */
2738 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
2739 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
2740 VDEV_TYPE_ROOT) == 0);
2741 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
2742 lastlogid != 0 ? lastlogid : schildren) == 0);
2744 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
2745 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
2747 for (c = 0; c < schildren; c++)
2748 nvlist_free(schild[c]);
2752 spa_config_exit(spa, SCL_VDEV, FTAG);
2754 (void) rw_wrlock(&ztest_name_lock);
2755 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
2756 (void) rw_unlock(&ztest_name_lock);
2758 nvlist_free(config);
2761 (void) printf("successful split - results:\n");
2762 mutex_enter(&spa_namespace_lock);
2763 show_pool_stats(spa);
2764 show_pool_stats(spa_lookup("splitp"));
2765 mutex_exit(&spa_namespace_lock);
2769 mutex_exit(&ztest_vdev_lock);
2774 * Verify that we can attach and detach devices.
2778 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
2780 ztest_shared_t *zs = ztest_shared;
2781 spa_t *spa = ztest_spa;
2782 spa_aux_vdev_t *sav = &spa->spa_spares;
2783 vdev_t *rvd = spa->spa_root_vdev;
2784 vdev_t *oldvd, *newvd, *pvd;
2788 uint64_t ashift = ztest_get_ashift();
2789 uint64_t oldguid, pguid;
2790 uint64_t oldsize, newsize;
2791 char *oldpath, *newpath;
2793 int oldvd_has_siblings = B_FALSE;
2794 int newvd_is_spare = B_FALSE;
2796 int error, expected_error;
2798 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2799 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2801 mutex_enter(&ztest_vdev_lock);
2802 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
2804 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2807 * Decide whether to do an attach or a replace.
2809 replacing = ztest_random(2);
2812 * Pick a random top-level vdev.
2814 top = ztest_random_vdev_top(spa, B_TRUE);
2817 * Pick a random leaf within it.
2819 leaf = ztest_random(leaves);
2824 oldvd = rvd->vdev_child[top];
2825 if (zs->zs_mirrors >= 1) {
2826 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
2827 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
2828 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
2830 if (ztest_opts.zo_raidz > 1) {
2831 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
2832 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
2833 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
2837 * If we're already doing an attach or replace, oldvd may be a
2838 * mirror vdev -- in which case, pick a random child.
2840 while (oldvd->vdev_children != 0) {
2841 oldvd_has_siblings = B_TRUE;
2842 ASSERT(oldvd->vdev_children >= 2);
2843 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
2846 oldguid = oldvd->vdev_guid;
2847 oldsize = vdev_get_min_asize(oldvd);
2848 oldvd_is_log = oldvd->vdev_top->vdev_islog;
2849 (void) strcpy(oldpath, oldvd->vdev_path);
2850 pvd = oldvd->vdev_parent;
2851 pguid = pvd->vdev_guid;
2854 * If oldvd has siblings, then half of the time, detach it.
2856 if (oldvd_has_siblings && ztest_random(2) == 0) {
2857 spa_config_exit(spa, SCL_VDEV, FTAG);
2858 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
2859 if (error != 0 && error != ENODEV && error != EBUSY &&
2861 fatal(0, "detach (%s) returned %d", oldpath, error);
2866 * For the new vdev, choose with equal probability between the two
2867 * standard paths (ending in either 'a' or 'b') or a random hot spare.
2869 if (sav->sav_count != 0 && ztest_random(3) == 0) {
2870 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
2871 newvd_is_spare = B_TRUE;
2872 (void) strcpy(newpath, newvd->vdev_path);
2874 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
2875 ztest_opts.zo_dir, ztest_opts.zo_pool,
2876 top * leaves + leaf);
2877 if (ztest_random(2) == 0)
2878 newpath[strlen(newpath) - 1] = 'b';
2879 newvd = vdev_lookup_by_path(rvd, newpath);
2883 newsize = vdev_get_min_asize(newvd);
2886 * Make newsize a little bigger or smaller than oldsize.
2887 * If it's smaller, the attach should fail.
2888 * If it's larger, and we're doing a replace,
2889 * we should get dynamic LUN growth when we're done.
2891 newsize = 10 * oldsize / (9 + ztest_random(3));
2895 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
2896 * unless it's a replace; in that case any non-replacing parent is OK.
2898 * If newvd is already part of the pool, it should fail with EBUSY.
2900 * If newvd is too small, it should fail with EOVERFLOW.
2902 if (pvd->vdev_ops != &vdev_mirror_ops &&
2903 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
2904 pvd->vdev_ops == &vdev_replacing_ops ||
2905 pvd->vdev_ops == &vdev_spare_ops))
2906 expected_error = ENOTSUP;
2907 else if (newvd_is_spare && (!replacing || oldvd_is_log))
2908 expected_error = ENOTSUP;
2909 else if (newvd == oldvd)
2910 expected_error = replacing ? 0 : EBUSY;
2911 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
2912 expected_error = EBUSY;
2913 else if (newsize < oldsize)
2914 expected_error = EOVERFLOW;
2915 else if (ashift > oldvd->vdev_top->vdev_ashift)
2916 expected_error = EDOM;
2920 spa_config_exit(spa, SCL_VDEV, FTAG);
2923 * Build the nvlist describing newpath.
2925 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
2926 ashift, 0, 0, 0, 1);
2928 error = spa_vdev_attach(spa, oldguid, root, replacing);
2933 * If our parent was the replacing vdev, but the replace completed,
2934 * then instead of failing with ENOTSUP we may either succeed,
2935 * fail with ENODEV, or fail with EOVERFLOW.
2937 if (expected_error == ENOTSUP &&
2938 (error == 0 || error == ENODEV || error == EOVERFLOW))
2939 expected_error = error;
2942 * If someone grew the LUN, the replacement may be too small.
2944 if (error == EOVERFLOW || error == EBUSY)
2945 expected_error = error;
2947 /* XXX workaround 6690467 */
2948 if (error != expected_error && expected_error != EBUSY) {
2949 fatal(0, "attach (%s %llu, %s %llu, %d) "
2950 "returned %d, expected %d",
2951 oldpath, oldsize, newpath,
2952 newsize, replacing, error, expected_error);
2955 mutex_exit(&ztest_vdev_lock);
2957 umem_free(oldpath, MAXPATHLEN);
2958 umem_free(newpath, MAXPATHLEN);
2962 * Callback function which expands the physical size of the vdev.
2965 grow_vdev(vdev_t *vd, void *arg)
2967 ASSERTV(spa_t *spa = vd->vdev_spa);
2968 size_t *newsize = arg;
2972 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
2973 ASSERT(vd->vdev_ops->vdev_op_leaf);
2975 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
2978 fsize = lseek(fd, 0, SEEK_END);
2979 VERIFY(ftruncate(fd, *newsize) == 0);
2981 if (ztest_opts.zo_verbose >= 6) {
2982 (void) printf("%s grew from %lu to %lu bytes\n",
2983 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
2990 * Callback function which expands a given vdev by calling vdev_online().
2994 online_vdev(vdev_t *vd, void *arg)
2996 spa_t *spa = vd->vdev_spa;
2997 vdev_t *tvd = vd->vdev_top;
2998 uint64_t guid = vd->vdev_guid;
2999 uint64_t generation = spa->spa_config_generation + 1;
3000 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3003 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3004 ASSERT(vd->vdev_ops->vdev_op_leaf);
3006 /* Calling vdev_online will initialize the new metaslabs */
3007 spa_config_exit(spa, SCL_STATE, spa);
3008 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3009 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3012 * If vdev_online returned an error or the underlying vdev_open
3013 * failed then we abort the expand. The only way to know that
3014 * vdev_open fails is by checking the returned newstate.
3016 if (error || newstate != VDEV_STATE_HEALTHY) {
3017 if (ztest_opts.zo_verbose >= 5) {
3018 (void) printf("Unable to expand vdev, state %llu, "
3019 "error %d\n", (u_longlong_t)newstate, error);
3023 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3026 * Since we dropped the lock we need to ensure that we're
3027 * still talking to the original vdev. It's possible this
3028 * vdev may have been detached/replaced while we were
3029 * trying to online it.
3031 if (generation != spa->spa_config_generation) {
3032 if (ztest_opts.zo_verbose >= 5) {
3033 (void) printf("vdev configuration has changed, "
3034 "guid %llu, state %llu, expected gen %llu, "
3037 (u_longlong_t)tvd->vdev_state,
3038 (u_longlong_t)generation,
3039 (u_longlong_t)spa->spa_config_generation);
3047 * Traverse the vdev tree calling the supplied function.
3048 * We continue to walk the tree until we either have walked all
3049 * children or we receive a non-NULL return from the callback.
3050 * If a NULL callback is passed, then we just return back the first
3051 * leaf vdev we encounter.
3054 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3058 if (vd->vdev_ops->vdev_op_leaf) {
3062 return (func(vd, arg));
3065 for (c = 0; c < vd->vdev_children; c++) {
3066 vdev_t *cvd = vd->vdev_child[c];
3067 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3074 * Verify that dynamic LUN growth works as expected.
3078 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3080 spa_t *spa = ztest_spa;
3082 metaslab_class_t *mc;
3083 metaslab_group_t *mg;
3084 size_t psize, newsize;
3086 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3088 mutex_enter(&ztest_vdev_lock);
3089 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3091 top = ztest_random_vdev_top(spa, B_TRUE);
3093 tvd = spa->spa_root_vdev->vdev_child[top];
3096 old_ms_count = tvd->vdev_ms_count;
3097 old_class_space = metaslab_class_get_space(mc);
3100 * Determine the size of the first leaf vdev associated with
3101 * our top-level device.
3103 vd = vdev_walk_tree(tvd, NULL, NULL);
3104 ASSERT3P(vd, !=, NULL);
3105 ASSERT(vd->vdev_ops->vdev_op_leaf);
3107 psize = vd->vdev_psize;
3110 * We only try to expand the vdev if it's healthy, less than 4x its
3111 * original size, and it has a valid psize.
3113 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3114 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3115 spa_config_exit(spa, SCL_STATE, spa);
3116 mutex_exit(&ztest_vdev_lock);
3120 newsize = psize + psize / 8;
3121 ASSERT3U(newsize, >, psize);
3123 if (ztest_opts.zo_verbose >= 6) {
3124 (void) printf("Expanding LUN %s from %lu to %lu\n",
3125 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3129 * Growing the vdev is a two step process:
3130 * 1). expand the physical size (i.e. relabel)
3131 * 2). online the vdev to create the new metaslabs
3133 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3134 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3135 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3136 if (ztest_opts.zo_verbose >= 5) {
3137 (void) printf("Could not expand LUN because "
3138 "the vdev configuration changed.\n");
3140 spa_config_exit(spa, SCL_STATE, spa);
3141 mutex_exit(&ztest_vdev_lock);
3145 spa_config_exit(spa, SCL_STATE, spa);
3148 * Expanding the LUN will update the config asynchronously,
3149 * thus we must wait for the async thread to complete any
3150 * pending tasks before proceeding.
3154 mutex_enter(&spa->spa_async_lock);
3155 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3156 mutex_exit(&spa->spa_async_lock);
3159 txg_wait_synced(spa_get_dsl(spa), 0);
3160 (void) poll(NULL, 0, 100);
3163 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3165 tvd = spa->spa_root_vdev->vdev_child[top];
3166 new_ms_count = tvd->vdev_ms_count;
3167 new_class_space = metaslab_class_get_space(mc);
3169 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3170 if (ztest_opts.zo_verbose >= 5) {
3171 (void) printf("Could not verify LUN expansion due to "
3172 "intervening vdev offline or remove.\n");
3174 spa_config_exit(spa, SCL_STATE, spa);
3175 mutex_exit(&ztest_vdev_lock);
3180 * Make sure we were able to grow the vdev.
3182 if (new_ms_count <= old_ms_count)
3183 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3184 old_ms_count, new_ms_count);
3187 * Make sure we were able to grow the pool.
3189 if (new_class_space <= old_class_space)
3190 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3191 old_class_space, new_class_space);
3193 if (ztest_opts.zo_verbose >= 5) {
3194 char oldnumbuf[6], newnumbuf[6];
3196 nicenum(old_class_space, oldnumbuf);
3197 nicenum(new_class_space, newnumbuf);
3198 (void) printf("%s grew from %s to %s\n",
3199 spa->spa_name, oldnumbuf, newnumbuf);
3202 spa_config_exit(spa, SCL_STATE, spa);
3203 mutex_exit(&ztest_vdev_lock);
3207 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3211 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3214 * Create the objects common to all ztest datasets.
3216 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3217 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3221 ztest_dataset_create(char *dsname)
3223 uint64_t zilset = ztest_random(100);
3224 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3225 ztest_objset_create_cb, NULL);
3227 if (err || zilset < 80)
3230 if (ztest_opts.zo_verbose >= 5)
3231 (void) printf("Setting dataset %s to sync always\n", dsname);
3232 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3233 ZFS_SYNC_ALWAYS, B_FALSE));
3238 ztest_objset_destroy_cb(const char *name, void *arg)
3241 dmu_object_info_t doi;
3245 * Verify that the dataset contains a directory object.
3247 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3248 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3249 if (error != ENOENT) {
3250 /* We could have crashed in the middle of destroying it */
3252 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3253 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3255 dmu_objset_disown(os, FTAG);
3258 * Destroy the dataset.
3260 if (strchr(name, '@') != NULL) {
3261 VERIFY0(dsl_destroy_snapshot(name, B_FALSE));
3263 VERIFY0(dsl_destroy_head(name));
3269 ztest_snapshot_create(char *osname, uint64_t id)
3271 char snapname[MAXNAMELEN];
3274 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3276 error = dmu_objset_snapshot_one(osname, snapname);
3277 if (error == ENOSPC) {
3278 ztest_record_enospc(FTAG);
3281 if (error != 0 && error != EEXIST) {
3282 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3289 ztest_snapshot_destroy(char *osname, uint64_t id)
3291 char snapname[MAXNAMELEN];
3294 (void) snprintf(snapname, MAXNAMELEN, "%s@%llu", osname,
3297 error = dsl_destroy_snapshot(snapname, B_FALSE);
3298 if (error != 0 && error != ENOENT)
3299 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3305 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3315 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3316 name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3318 (void) rw_rdlock(&ztest_name_lock);
3320 (void) snprintf(name, MAXNAMELEN, "%s/temp_%llu",
3321 ztest_opts.zo_pool, (u_longlong_t)id);
3324 * If this dataset exists from a previous run, process its replay log
3325 * half of the time. If we don't replay it, then dsl_destroy_head()
3326 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3328 if (ztest_random(2) == 0 &&
3329 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3330 ztest_zd_init(zdtmp, NULL, os);
3331 zil_replay(os, zdtmp, ztest_replay_vector);
3332 ztest_zd_fini(zdtmp);
3333 dmu_objset_disown(os, FTAG);
3337 * There may be an old instance of the dataset we're about to
3338 * create lying around from a previous run. If so, destroy it
3339 * and all of its snapshots.
3341 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3342 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3345 * Verify that the destroyed dataset is no longer in the namespace.
3347 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3351 * Verify that we can create a new dataset.
3353 error = ztest_dataset_create(name);
3355 if (error == ENOSPC) {
3356 ztest_record_enospc(FTAG);
3359 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3362 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3364 ztest_zd_init(zdtmp, NULL, os);
3367 * Open the intent log for it.
3369 zilog = zil_open(os, ztest_get_data);
3372 * Put some objects in there, do a little I/O to them,
3373 * and randomly take a couple of snapshots along the way.
3375 iters = ztest_random(5);
3376 for (i = 0; i < iters; i++) {
3377 ztest_dmu_object_alloc_free(zdtmp, id);
3378 if (ztest_random(iters) == 0)
3379 (void) ztest_snapshot_create(name, i);
3383 * Verify that we cannot create an existing dataset.
3385 VERIFY3U(EEXIST, ==,
3386 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3389 * Verify that we can hold an objset that is also owned.
3391 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3392 dmu_objset_rele(os2, FTAG);
3395 * Verify that we cannot own an objset that is already owned.
3398 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3401 dmu_objset_disown(os, FTAG);
3402 ztest_zd_fini(zdtmp);
3404 (void) rw_unlock(&ztest_name_lock);
3406 umem_free(name, MAXNAMELEN);
3407 umem_free(zdtmp, sizeof (ztest_ds_t));
3411 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3414 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3416 (void) rw_rdlock(&ztest_name_lock);
3417 (void) ztest_snapshot_destroy(zd->zd_name, id);
3418 (void) ztest_snapshot_create(zd->zd_name, id);
3419 (void) rw_unlock(&ztest_name_lock);
3423 * Cleanup non-standard snapshots and clones.
3426 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3435 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3436 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3437 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3438 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3439 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3441 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3442 osname, (u_longlong_t)id);
3443 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3444 osname, (u_longlong_t)id);
3445 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3446 clone1name, (u_longlong_t)id);
3447 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3448 osname, (u_longlong_t)id);
3449 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3450 clone1name, (u_longlong_t)id);
3452 error = dsl_destroy_head(clone2name);
3453 if (error && error != ENOENT)
3454 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3455 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3456 if (error && error != ENOENT)
3457 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3458 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3459 if (error && error != ENOENT)
3460 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3461 error = dsl_destroy_head(clone1name);
3462 if (error && error != ENOENT)
3463 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3464 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3465 if (error && error != ENOENT)
3466 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3468 umem_free(snap1name, MAXNAMELEN);
3469 umem_free(clone1name, MAXNAMELEN);
3470 umem_free(snap2name, MAXNAMELEN);
3471 umem_free(clone2name, MAXNAMELEN);
3472 umem_free(snap3name, MAXNAMELEN);
3476 * Verify dsl_dataset_promote handles EBUSY
3479 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3487 char *osname = zd->zd_name;
3490 snap1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3491 clone1name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3492 snap2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3493 clone2name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3494 snap3name = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
3496 (void) rw_rdlock(&ztest_name_lock);
3498 ztest_dsl_dataset_cleanup(osname, id);
3500 (void) snprintf(snap1name, MAXNAMELEN, "%s@s1_%llu",
3501 osname, (u_longlong_t)id);
3502 (void) snprintf(clone1name, MAXNAMELEN, "%s/c1_%llu",
3503 osname, (u_longlong_t)id);
3504 (void) snprintf(snap2name, MAXNAMELEN, "%s@s2_%llu",
3505 clone1name, (u_longlong_t)id);
3506 (void) snprintf(clone2name, MAXNAMELEN, "%s/c2_%llu",
3507 osname, (u_longlong_t)id);
3508 (void) snprintf(snap3name, MAXNAMELEN, "%s@s3_%llu",
3509 clone1name, (u_longlong_t)id);
3511 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3512 if (error && error != EEXIST) {
3513 if (error == ENOSPC) {
3514 ztest_record_enospc(FTAG);
3517 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3520 error = dmu_objset_clone(clone1name, snap1name);
3522 if (error == ENOSPC) {
3523 ztest_record_enospc(FTAG);
3526 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3529 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3530 if (error && error != EEXIST) {
3531 if (error == ENOSPC) {
3532 ztest_record_enospc(FTAG);
3535 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3538 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3539 if (error && error != EEXIST) {
3540 if (error == ENOSPC) {
3541 ztest_record_enospc(FTAG);
3544 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3547 error = dmu_objset_clone(clone2name, snap3name);
3549 if (error == ENOSPC) {
3550 ztest_record_enospc(FTAG);
3553 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3556 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3558 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3559 error = dsl_dataset_promote(clone2name, NULL);
3560 if (error == ENOSPC) {
3561 dmu_objset_disown(os, FTAG);
3562 ztest_record_enospc(FTAG);
3566 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3568 dmu_objset_disown(os, FTAG);
3571 ztest_dsl_dataset_cleanup(osname, id);
3573 (void) rw_unlock(&ztest_name_lock);
3575 umem_free(snap1name, MAXNAMELEN);
3576 umem_free(clone1name, MAXNAMELEN);
3577 umem_free(snap2name, MAXNAMELEN);
3578 umem_free(clone2name, MAXNAMELEN);
3579 umem_free(snap3name, MAXNAMELEN);
3582 #undef OD_ARRAY_SIZE
3583 #define OD_ARRAY_SIZE 4
3586 * Verify that dmu_object_{alloc,free} work as expected.
3589 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3596 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3597 od = umem_alloc(size, UMEM_NOFAIL);
3598 batchsize = OD_ARRAY_SIZE;
3600 for (b = 0; b < batchsize; b++)
3601 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER, 0, 0);
3604 * Destroy the previous batch of objects, create a new batch,
3605 * and do some I/O on the new objects.
3607 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3610 while (ztest_random(4 * batchsize) != 0)
3611 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3612 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3614 umem_free(od, size);
3617 #undef OD_ARRAY_SIZE
3618 #define OD_ARRAY_SIZE 2
3621 * Verify that dmu_{read,write} work as expected.
3624 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3629 objset_t *os = zd->zd_os;
3630 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3631 od = umem_alloc(size, UMEM_NOFAIL);
3633 int i, freeit, error;
3635 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3636 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3637 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3638 uint64_t regions = 997;
3639 uint64_t stride = 123456789ULL;
3640 uint64_t width = 40;
3641 int free_percent = 5;
3644 * This test uses two objects, packobj and bigobj, that are always
3645 * updated together (i.e. in the same tx) so that their contents are
3646 * in sync and can be compared. Their contents relate to each other
3647 * in a simple way: packobj is a dense array of 'bufwad' structures,
3648 * while bigobj is a sparse array of the same bufwads. Specifically,
3649 * for any index n, there are three bufwads that should be identical:
3651 * packobj, at offset n * sizeof (bufwad_t)
3652 * bigobj, at the head of the nth chunk
3653 * bigobj, at the tail of the nth chunk
3655 * The chunk size is arbitrary. It doesn't have to be a power of two,
3656 * and it doesn't have any relation to the object blocksize.
3657 * The only requirement is that it can hold at least two bufwads.
3659 * Normally, we write the bufwad to each of these locations.
3660 * However, free_percent of the time we instead write zeroes to
3661 * packobj and perform a dmu_free_range() on bigobj. By comparing
3662 * bigobj to packobj, we can verify that the DMU is correctly
3663 * tracking which parts of an object are allocated and free,
3664 * and that the contents of the allocated blocks are correct.
3668 * Read the directory info. If it's the first time, set things up.
3670 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, chunksize);
3671 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3673 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3674 umem_free(od, size);
3678 bigobj = od[0].od_object;
3679 packobj = od[1].od_object;
3680 chunksize = od[0].od_gen;
3681 ASSERT(chunksize == od[1].od_gen);
3684 * Prefetch a random chunk of the big object.
3685 * Our aim here is to get some async reads in flight
3686 * for blocks that we may free below; the DMU should
3687 * handle this race correctly.
3689 n = ztest_random(regions) * stride + ztest_random(width);
3690 s = 1 + ztest_random(2 * width - 1);
3691 dmu_prefetch(os, bigobj, n * chunksize, s * chunksize);
3694 * Pick a random index and compute the offsets into packobj and bigobj.
3696 n = ztest_random(regions) * stride + ztest_random(width);
3697 s = 1 + ztest_random(width - 1);
3699 packoff = n * sizeof (bufwad_t);
3700 packsize = s * sizeof (bufwad_t);
3702 bigoff = n * chunksize;
3703 bigsize = s * chunksize;
3705 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3706 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3709 * free_percent of the time, free a range of bigobj rather than
3712 freeit = (ztest_random(100) < free_percent);
3715 * Read the current contents of our objects.
3717 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3720 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3725 * Get a tx for the mods to both packobj and bigobj.
3727 tx = dmu_tx_create(os);
3729 dmu_tx_hold_write(tx, packobj, packoff, packsize);
3732 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
3734 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
3736 /* This accounts for setting the checksum/compression. */
3737 dmu_tx_hold_bonus(tx, bigobj);
3739 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
3741 umem_free(packbuf, packsize);
3742 umem_free(bigbuf, bigsize);
3743 umem_free(od, size);
3747 enum zio_checksum cksum;
3749 cksum = (enum zio_checksum)
3750 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
3751 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
3752 dmu_object_set_checksum(os, bigobj, cksum, tx);
3754 enum zio_compress comp;
3756 comp = (enum zio_compress)
3757 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
3758 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
3759 dmu_object_set_compress(os, bigobj, comp, tx);
3762 * For each index from n to n + s, verify that the existing bufwad
3763 * in packobj matches the bufwads at the head and tail of the
3764 * corresponding chunk in bigobj. Then update all three bufwads
3765 * with the new values we want to write out.
3767 for (i = 0; i < s; i++) {
3769 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3771 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3773 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3775 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3776 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3778 if (pack->bw_txg > txg)
3779 fatal(0, "future leak: got %llx, open txg is %llx",
3782 if (pack->bw_data != 0 && pack->bw_index != n + i)
3783 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3784 pack->bw_index, n, i);
3786 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3787 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3789 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3790 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3793 bzero(pack, sizeof (bufwad_t));
3795 pack->bw_index = n + i;
3797 pack->bw_data = 1 + ztest_random(-2ULL);
3804 * We've verified all the old bufwads, and made new ones.
3805 * Now write them out.
3807 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
3810 if (ztest_opts.zo_verbose >= 7) {
3811 (void) printf("freeing offset %llx size %llx"
3813 (u_longlong_t)bigoff,
3814 (u_longlong_t)bigsize,
3817 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
3819 if (ztest_opts.zo_verbose >= 7) {
3820 (void) printf("writing offset %llx size %llx"
3822 (u_longlong_t)bigoff,
3823 (u_longlong_t)bigsize,
3826 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
3832 * Sanity check the stuff we just wrote.
3835 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
3836 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
3838 VERIFY(0 == dmu_read(os, packobj, packoff,
3839 packsize, packcheck, DMU_READ_PREFETCH));
3840 VERIFY(0 == dmu_read(os, bigobj, bigoff,
3841 bigsize, bigcheck, DMU_READ_PREFETCH));
3843 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
3844 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
3846 umem_free(packcheck, packsize);
3847 umem_free(bigcheck, bigsize);
3850 umem_free(packbuf, packsize);
3851 umem_free(bigbuf, bigsize);
3852 umem_free(od, size);
3856 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
3857 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
3865 * For each index from n to n + s, verify that the existing bufwad
3866 * in packobj matches the bufwads at the head and tail of the
3867 * corresponding chunk in bigobj. Then update all three bufwads
3868 * with the new values we want to write out.
3870 for (i = 0; i < s; i++) {
3872 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
3874 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
3876 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
3878 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
3879 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
3881 if (pack->bw_txg > txg)
3882 fatal(0, "future leak: got %llx, open txg is %llx",
3885 if (pack->bw_data != 0 && pack->bw_index != n + i)
3886 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
3887 pack->bw_index, n, i);
3889 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
3890 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
3892 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
3893 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
3895 pack->bw_index = n + i;
3897 pack->bw_data = 1 + ztest_random(-2ULL);
3904 #undef OD_ARRAY_SIZE
3905 #define OD_ARRAY_SIZE 2
3908 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
3910 objset_t *os = zd->zd_os;
3917 bufwad_t *packbuf, *bigbuf;
3918 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3919 uint64_t blocksize = ztest_random_blocksize();
3920 uint64_t chunksize = blocksize;
3921 uint64_t regions = 997;
3922 uint64_t stride = 123456789ULL;
3924 dmu_buf_t *bonus_db;
3925 arc_buf_t **bigbuf_arcbufs;
3926 dmu_object_info_t doi;
3928 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3929 od = umem_alloc(size, UMEM_NOFAIL);
3932 * This test uses two objects, packobj and bigobj, that are always
3933 * updated together (i.e. in the same tx) so that their contents are
3934 * in sync and can be compared. Their contents relate to each other
3935 * in a simple way: packobj is a dense array of 'bufwad' structures,
3936 * while bigobj is a sparse array of the same bufwads. Specifically,
3937 * for any index n, there are three bufwads that should be identical:
3939 * packobj, at offset n * sizeof (bufwad_t)
3940 * bigobj, at the head of the nth chunk
3941 * bigobj, at the tail of the nth chunk
3943 * The chunk size is set equal to bigobj block size so that
3944 * dmu_assign_arcbuf() can be tested for object updates.
3948 * Read the directory info. If it's the first time, set things up.
3950 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
3951 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, chunksize);
3954 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3955 umem_free(od, size);
3959 bigobj = od[0].od_object;
3960 packobj = od[1].od_object;
3961 blocksize = od[0].od_blocksize;
3962 chunksize = blocksize;
3963 ASSERT(chunksize == od[1].od_gen);
3965 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
3966 VERIFY(ISP2(doi.doi_data_block_size));
3967 VERIFY(chunksize == doi.doi_data_block_size);
3968 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
3971 * Pick a random index and compute the offsets into packobj and bigobj.
3973 n = ztest_random(regions) * stride + ztest_random(width);
3974 s = 1 + ztest_random(width - 1);
3976 packoff = n * sizeof (bufwad_t);
3977 packsize = s * sizeof (bufwad_t);
3979 bigoff = n * chunksize;
3980 bigsize = s * chunksize;
3982 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
3983 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
3985 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
3987 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
3990 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
3991 * Iteration 1 test zcopy to already referenced dbufs.
3992 * Iteration 2 test zcopy to dirty dbuf in the same txg.
3993 * Iteration 3 test zcopy to dbuf dirty in previous txg.
3994 * Iteration 4 test zcopy when dbuf is no longer dirty.
3995 * Iteration 5 test zcopy when it can't be done.
3996 * Iteration 6 one more zcopy write.
3998 for (i = 0; i < 7; i++) {
4003 * In iteration 5 (i == 5) use arcbufs
4004 * that don't match bigobj blksz to test
4005 * dmu_assign_arcbuf() when it can't directly
4006 * assign an arcbuf to a dbuf.
4008 for (j = 0; j < s; j++) {
4011 dmu_request_arcbuf(bonus_db, chunksize);
4013 bigbuf_arcbufs[2 * j] =
4014 dmu_request_arcbuf(bonus_db, chunksize / 2);
4015 bigbuf_arcbufs[2 * j + 1] =
4016 dmu_request_arcbuf(bonus_db, chunksize / 2);
4021 * Get a tx for the mods to both packobj and bigobj.
4023 tx = dmu_tx_create(os);
4025 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4026 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4028 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4030 umem_free(packbuf, packsize);
4031 umem_free(bigbuf, bigsize);
4032 for (j = 0; j < s; j++) {
4034 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4037 bigbuf_arcbufs[2 * j]);
4039 bigbuf_arcbufs[2 * j + 1]);
4042 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4043 umem_free(od, size);
4044 dmu_buf_rele(bonus_db, FTAG);
4049 * 50% of the time don't read objects in the 1st iteration to
4050 * test dmu_assign_arcbuf() for the case when there're no
4051 * existing dbufs for the specified offsets.
4053 if (i != 0 || ztest_random(2) != 0) {
4054 error = dmu_read(os, packobj, packoff,
4055 packsize, packbuf, DMU_READ_PREFETCH);
4057 error = dmu_read(os, bigobj, bigoff, bigsize,
4058 bigbuf, DMU_READ_PREFETCH);
4061 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4065 * We've verified all the old bufwads, and made new ones.
4066 * Now write them out.
4068 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4069 if (ztest_opts.zo_verbose >= 7) {
4070 (void) printf("writing offset %llx size %llx"
4072 (u_longlong_t)bigoff,
4073 (u_longlong_t)bigsize,
4076 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4079 bcopy((caddr_t)bigbuf + (off - bigoff),
4080 bigbuf_arcbufs[j]->b_data, chunksize);
4082 bcopy((caddr_t)bigbuf + (off - bigoff),
4083 bigbuf_arcbufs[2 * j]->b_data,
4085 bcopy((caddr_t)bigbuf + (off - bigoff) +
4087 bigbuf_arcbufs[2 * j + 1]->b_data,
4092 VERIFY(dmu_buf_hold(os, bigobj, off,
4093 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4096 dmu_assign_arcbuf(bonus_db, off,
4097 bigbuf_arcbufs[j], tx);
4099 dmu_assign_arcbuf(bonus_db, off,
4100 bigbuf_arcbufs[2 * j], tx);
4101 dmu_assign_arcbuf(bonus_db,
4102 off + chunksize / 2,
4103 bigbuf_arcbufs[2 * j + 1], tx);
4106 dmu_buf_rele(dbt, FTAG);
4112 * Sanity check the stuff we just wrote.
4115 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4116 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4118 VERIFY(0 == dmu_read(os, packobj, packoff,
4119 packsize, packcheck, DMU_READ_PREFETCH));
4120 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4121 bigsize, bigcheck, DMU_READ_PREFETCH));
4123 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4124 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4126 umem_free(packcheck, packsize);
4127 umem_free(bigcheck, bigsize);
4130 txg_wait_open(dmu_objset_pool(os), 0);
4131 } else if (i == 3) {
4132 txg_wait_synced(dmu_objset_pool(os), 0);
4136 dmu_buf_rele(bonus_db, FTAG);
4137 umem_free(packbuf, packsize);
4138 umem_free(bigbuf, bigsize);
4139 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4140 umem_free(od, size);
4145 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4149 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4150 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4151 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4154 * Have multiple threads write to large offsets in an object
4155 * to verify that parallel writes to an object -- even to the
4156 * same blocks within the object -- doesn't cause any trouble.
4158 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4160 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4163 while (ztest_random(10) != 0)
4164 ztest_io(zd, od->od_object, offset);
4166 umem_free(od, sizeof (ztest_od_t));
4170 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4173 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4174 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4175 uint64_t count = ztest_random(20) + 1;
4176 uint64_t blocksize = ztest_random_blocksize();
4179 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4181 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
4183 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4184 !ztest_random(2)) != 0) {
4185 umem_free(od, sizeof (ztest_od_t));
4189 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4190 umem_free(od, sizeof (ztest_od_t));
4194 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4196 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4198 while (ztest_random(count) != 0) {
4199 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4200 if (ztest_write(zd, od->od_object, randoff, blocksize,
4203 while (ztest_random(4) != 0)
4204 ztest_io(zd, od->od_object, randoff);
4207 umem_free(data, blocksize);
4208 umem_free(od, sizeof (ztest_od_t));
4212 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4214 #define ZTEST_ZAP_MIN_INTS 1
4215 #define ZTEST_ZAP_MAX_INTS 4
4216 #define ZTEST_ZAP_MAX_PROPS 1000
4219 ztest_zap(ztest_ds_t *zd, uint64_t id)
4221 objset_t *os = zd->zd_os;
4224 uint64_t txg, last_txg;
4225 uint64_t value[ZTEST_ZAP_MAX_INTS];
4226 uint64_t zl_ints, zl_intsize, prop;
4229 char propname[100], txgname[100];
4231 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4233 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4234 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4236 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4237 !ztest_random(2)) != 0)
4240 object = od->od_object;
4243 * Generate a known hash collision, and verify that
4244 * we can lookup and remove both entries.
4246 tx = dmu_tx_create(os);
4247 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4248 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4251 for (i = 0; i < 2; i++) {
4253 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4256 for (i = 0; i < 2; i++) {
4257 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4258 sizeof (uint64_t), 1, &value[i], tx));
4260 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4261 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4262 ASSERT3U(zl_ints, ==, 1);
4264 for (i = 0; i < 2; i++) {
4265 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4270 * Generate a buch of random entries.
4272 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4274 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4275 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4276 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4277 bzero(value, sizeof (value));
4281 * If these zap entries already exist, validate their contents.
4283 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4285 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4286 ASSERT3U(zl_ints, ==, 1);
4288 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4289 zl_ints, &last_txg) == 0);
4291 VERIFY(zap_length(os, object, propname, &zl_intsize,
4294 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4295 ASSERT3U(zl_ints, ==, ints);
4297 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4298 zl_ints, value) == 0);
4300 for (i = 0; i < ints; i++) {
4301 ASSERT3U(value[i], ==, last_txg + object + i);
4304 ASSERT3U(error, ==, ENOENT);
4308 * Atomically update two entries in our zap object.
4309 * The first is named txg_%llu, and contains the txg
4310 * in which the property was last updated. The second
4311 * is named prop_%llu, and the nth element of its value
4312 * should be txg + object + n.
4314 tx = dmu_tx_create(os);
4315 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4316 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4321 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4323 for (i = 0; i < ints; i++)
4324 value[i] = txg + object + i;
4326 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4328 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4334 * Remove a random pair of entries.
4336 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4337 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4338 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4340 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4342 if (error == ENOENT)
4347 tx = dmu_tx_create(os);
4348 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4349 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4352 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4353 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4356 umem_free(od, sizeof (ztest_od_t));
4360 * Testcase to test the upgrading of a microzap to fatzap.
4363 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4365 objset_t *os = zd->zd_os;
4367 uint64_t object, txg;
4370 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4371 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0);
4373 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4374 !ztest_random(2)) != 0)
4376 object = od->od_object;
4379 * Add entries to this ZAP and make sure it spills over
4380 * and gets upgraded to a fatzap. Also, since we are adding
4381 * 2050 entries we should see ptrtbl growth and leaf-block split.
4383 for (i = 0; i < 2050; i++) {
4384 char name[MAXNAMELEN];
4389 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4390 (u_longlong_t)id, (u_longlong_t)value);
4392 tx = dmu_tx_create(os);
4393 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4394 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4397 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4399 ASSERT(error == 0 || error == EEXIST);
4403 umem_free(od, sizeof (ztest_od_t));
4408 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4410 objset_t *os = zd->zd_os;
4412 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4414 int i, namelen, error;
4415 int micro = ztest_random(2);
4416 char name[20], string_value[20];
4419 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4420 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0);
4422 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4423 umem_free(od, sizeof (ztest_od_t));
4427 object = od->od_object;
4430 * Generate a random name of the form 'xxx.....' where each
4431 * x is a random printable character and the dots are dots.
4432 * There are 94 such characters, and the name length goes from
4433 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4435 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4437 for (i = 0; i < 3; i++)
4438 name[i] = '!' + ztest_random('~' - '!' + 1);
4439 for (; i < namelen - 1; i++)
4443 if ((namelen & 1) || micro) {
4444 wsize = sizeof (txg);
4450 data = string_value;
4454 VERIFY0(zap_count(os, object, &count));
4455 ASSERT(count != -1ULL);
4458 * Select an operation: length, lookup, add, update, remove.
4460 i = ztest_random(5);
4463 tx = dmu_tx_create(os);
4464 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4465 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4468 bcopy(name, string_value, namelen);
4472 bzero(string_value, namelen);
4478 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4480 ASSERT3U(wsize, ==, zl_wsize);
4481 ASSERT3U(wc, ==, zl_wc);
4483 ASSERT3U(error, ==, ENOENT);
4488 error = zap_lookup(os, object, name, wsize, wc, data);
4490 if (data == string_value &&
4491 bcmp(name, data, namelen) != 0)
4492 fatal(0, "name '%s' != val '%s' len %d",
4493 name, data, namelen);
4495 ASSERT3U(error, ==, ENOENT);
4500 error = zap_add(os, object, name, wsize, wc, data, tx);
4501 ASSERT(error == 0 || error == EEXIST);
4505 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4509 error = zap_remove(os, object, name, tx);
4510 ASSERT(error == 0 || error == ENOENT);
4517 umem_free(od, sizeof (ztest_od_t));
4521 * Commit callback data.
4523 typedef struct ztest_cb_data {
4524 list_node_t zcd_node;
4526 int zcd_expected_err;
4527 boolean_t zcd_added;
4528 boolean_t zcd_called;
4532 /* This is the actual commit callback function */
4534 ztest_commit_callback(void *arg, int error)
4536 ztest_cb_data_t *data = arg;
4537 uint64_t synced_txg;
4539 VERIFY(data != NULL);
4540 VERIFY3S(data->zcd_expected_err, ==, error);
4541 VERIFY(!data->zcd_called);
4543 synced_txg = spa_last_synced_txg(data->zcd_spa);
4544 if (data->zcd_txg > synced_txg)
4545 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4546 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4549 data->zcd_called = B_TRUE;
4551 if (error == ECANCELED) {
4552 ASSERT0(data->zcd_txg);
4553 ASSERT(!data->zcd_added);
4556 * The private callback data should be destroyed here, but
4557 * since we are going to check the zcd_called field after
4558 * dmu_tx_abort(), we will destroy it there.
4563 ASSERT(data->zcd_added);
4564 ASSERT3U(data->zcd_txg, !=, 0);
4566 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4568 /* See if this cb was called more quickly */
4569 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4570 zc_min_txg_delay = synced_txg - data->zcd_txg;
4572 /* Remove our callback from the list */
4573 list_remove(&zcl.zcl_callbacks, data);
4575 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4577 umem_free(data, sizeof (ztest_cb_data_t));
4580 /* Allocate and initialize callback data structure */
4581 static ztest_cb_data_t *
4582 ztest_create_cb_data(objset_t *os, uint64_t txg)
4584 ztest_cb_data_t *cb_data;
4586 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4588 cb_data->zcd_txg = txg;
4589 cb_data->zcd_spa = dmu_objset_spa(os);
4590 list_link_init(&cb_data->zcd_node);
4596 * Commit callback test.
4599 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4601 objset_t *os = zd->zd_os;
4604 ztest_cb_data_t *cb_data[3], *tmp_cb;
4605 uint64_t old_txg, txg;
4608 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4609 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0);
4611 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4612 umem_free(od, sizeof (ztest_od_t));
4616 tx = dmu_tx_create(os);
4618 cb_data[0] = ztest_create_cb_data(os, 0);
4619 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4621 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4623 /* Every once in a while, abort the transaction on purpose */
4624 if (ztest_random(100) == 0)
4628 error = dmu_tx_assign(tx, TXG_NOWAIT);
4630 txg = error ? 0 : dmu_tx_get_txg(tx);
4632 cb_data[0]->zcd_txg = txg;
4633 cb_data[1] = ztest_create_cb_data(os, txg);
4634 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4638 * It's not a strict requirement to call the registered
4639 * callbacks from inside dmu_tx_abort(), but that's what
4640 * it's supposed to happen in the current implementation
4641 * so we will check for that.
4643 for (i = 0; i < 2; i++) {
4644 cb_data[i]->zcd_expected_err = ECANCELED;
4645 VERIFY(!cb_data[i]->zcd_called);
4650 for (i = 0; i < 2; i++) {
4651 VERIFY(cb_data[i]->zcd_called);
4652 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4655 umem_free(od, sizeof (ztest_od_t));
4659 cb_data[2] = ztest_create_cb_data(os, txg);
4660 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4663 * Read existing data to make sure there isn't a future leak.
4665 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4666 &old_txg, DMU_READ_PREFETCH));
4669 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4672 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4674 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4677 * Since commit callbacks don't have any ordering requirement and since
4678 * it is theoretically possible for a commit callback to be called
4679 * after an arbitrary amount of time has elapsed since its txg has been
4680 * synced, it is difficult to reliably determine whether a commit
4681 * callback hasn't been called due to high load or due to a flawed
4684 * In practice, we will assume that if after a certain number of txgs a
4685 * commit callback hasn't been called, then most likely there's an
4686 * implementation bug..
4688 tmp_cb = list_head(&zcl.zcl_callbacks);
4689 if (tmp_cb != NULL &&
4690 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4691 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4692 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4696 * Let's find the place to insert our callbacks.
4698 * Even though the list is ordered by txg, it is possible for the
4699 * insertion point to not be the end because our txg may already be
4700 * quiescing at this point and other callbacks in the open txg
4701 * (from other objsets) may have sneaked in.
4703 tmp_cb = list_tail(&zcl.zcl_callbacks);
4704 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4705 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4707 /* Add the 3 callbacks to the list */
4708 for (i = 0; i < 3; i++) {
4710 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4712 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4715 cb_data[i]->zcd_added = B_TRUE;
4716 VERIFY(!cb_data[i]->zcd_called);
4718 tmp_cb = cb_data[i];
4723 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4727 umem_free(od, sizeof (ztest_od_t));
4732 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
4734 zfs_prop_t proplist[] = {
4736 ZFS_PROP_COMPRESSION,
4742 (void) rw_rdlock(&ztest_name_lock);
4744 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
4745 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
4746 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
4748 (void) rw_unlock(&ztest_name_lock);
4753 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
4755 nvlist_t *props = NULL;
4757 (void) rw_rdlock(&ztest_name_lock);
4759 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
4760 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
4762 VERIFY0(spa_prop_get(ztest_spa, &props));
4764 if (ztest_opts.zo_verbose >= 6)
4765 dump_nvlist(props, 4);
4769 (void) rw_unlock(&ztest_name_lock);
4773 user_release_one(const char *snapname, const char *holdname)
4775 nvlist_t *snaps, *holds;
4778 snaps = fnvlist_alloc();
4779 holds = fnvlist_alloc();
4780 fnvlist_add_boolean(holds, holdname);
4781 fnvlist_add_nvlist(snaps, snapname, holds);
4782 fnvlist_free(holds);
4783 error = dsl_dataset_user_release(snaps, NULL);
4784 fnvlist_free(snaps);
4789 * Test snapshot hold/release and deferred destroy.
4792 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
4795 objset_t *os = zd->zd_os;
4799 char clonename[100];
4801 char osname[MAXNAMELEN];
4804 (void) rw_rdlock(&ztest_name_lock);
4806 dmu_objset_name(os, osname);
4808 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
4810 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
4811 (void) snprintf(clonename, sizeof (clonename),
4812 "%s/ch1_%llu", osname, (u_longlong_t)id);
4813 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
4816 * Clean up from any previous run.
4818 error = dsl_destroy_head(clonename);
4819 if (error != ENOENT)
4821 error = user_release_one(fullname, tag);
4822 if (error != ESRCH && error != ENOENT)
4824 error = dsl_destroy_snapshot(fullname, B_FALSE);
4825 if (error != ENOENT)
4829 * Create snapshot, clone it, mark snap for deferred destroy,
4830 * destroy clone, verify snap was also destroyed.
4832 error = dmu_objset_snapshot_one(osname, snapname);
4834 if (error == ENOSPC) {
4835 ztest_record_enospc("dmu_objset_snapshot");
4838 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4841 error = dmu_objset_clone(clonename, fullname);
4843 if (error == ENOSPC) {
4844 ztest_record_enospc("dmu_objset_clone");
4847 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
4850 error = dsl_destroy_snapshot(fullname, B_TRUE);
4852 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4856 error = dsl_destroy_head(clonename);
4858 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
4860 error = dmu_objset_hold(fullname, FTAG, &origin);
4861 if (error != ENOENT)
4862 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
4865 * Create snapshot, add temporary hold, verify that we can't
4866 * destroy a held snapshot, mark for deferred destroy,
4867 * release hold, verify snapshot was destroyed.
4869 error = dmu_objset_snapshot_one(osname, snapname);
4871 if (error == ENOSPC) {
4872 ztest_record_enospc("dmu_objset_snapshot");
4875 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
4878 holds = fnvlist_alloc();
4879 fnvlist_add_string(holds, fullname, tag);
4880 error = dsl_dataset_user_hold(holds, 0, NULL);
4881 fnvlist_free(holds);
4883 if (error == ENOSPC) {
4884 ztest_record_enospc("dsl_dataset_user_hold");
4887 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
4888 fullname, tag, error);
4891 error = dsl_destroy_snapshot(fullname, B_FALSE);
4892 if (error != EBUSY) {
4893 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
4897 error = dsl_destroy_snapshot(fullname, B_TRUE);
4899 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
4903 error = user_release_one(fullname, tag);
4905 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
4907 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
4910 (void) rw_unlock(&ztest_name_lock);
4914 * Inject random faults into the on-disk data.
4918 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
4920 ztest_shared_t *zs = ztest_shared;
4921 spa_t *spa = ztest_spa;
4925 uint64_t bad = 0x1990c0ffeedecadeull;
4930 int bshift = SPA_MAXBLOCKSHIFT + 2; /* don't scrog all labels */
4936 boolean_t islog = B_FALSE;
4938 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4939 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
4941 mutex_enter(&ztest_vdev_lock);
4942 maxfaults = MAXFAULTS();
4943 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
4944 mirror_save = zs->zs_mirrors;
4945 mutex_exit(&ztest_vdev_lock);
4947 ASSERT(leaves >= 1);
4950 * Grab the name lock as reader. There are some operations
4951 * which don't like to have their vdevs changed while
4952 * they are in progress (i.e. spa_change_guid). Those
4953 * operations will have grabbed the name lock as writer.
4955 (void) rw_rdlock(&ztest_name_lock);
4958 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
4960 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4962 if (ztest_random(2) == 0) {
4964 * Inject errors on a normal data device or slog device.
4966 top = ztest_random_vdev_top(spa, B_TRUE);
4967 leaf = ztest_random(leaves) + zs->zs_splits;
4970 * Generate paths to the first leaf in this top-level vdev,
4971 * and to the random leaf we selected. We'll induce transient
4972 * write failures and random online/offline activity on leaf 0,
4973 * and we'll write random garbage to the randomly chosen leaf.
4975 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
4976 ztest_opts.zo_dir, ztest_opts.zo_pool,
4977 top * leaves + zs->zs_splits);
4978 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
4979 ztest_opts.zo_dir, ztest_opts.zo_pool,
4980 top * leaves + leaf);
4982 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
4983 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
4987 * If the top-level vdev needs to be resilvered
4988 * then we only allow faults on the device that is
4991 if (vd0 != NULL && maxfaults != 1 &&
4992 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
4993 vd0->vdev_resilver_txg != 0)) {
4995 * Make vd0 explicitly claim to be unreadable,
4996 * or unwriteable, or reach behind its back
4997 * and close the underlying fd. We can do this if
4998 * maxfaults == 0 because we'll fail and reexecute,
4999 * and we can do it if maxfaults >= 2 because we'll
5000 * have enough redundancy. If maxfaults == 1, the
5001 * combination of this with injection of random data
5002 * corruption below exceeds the pool's fault tolerance.
5004 vdev_file_t *vf = vd0->vdev_tsd;
5006 if (vf != NULL && ztest_random(3) == 0) {
5007 (void) close(vf->vf_vnode->v_fd);
5008 vf->vf_vnode->v_fd = -1;
5009 } else if (ztest_random(2) == 0) {
5010 vd0->vdev_cant_read = B_TRUE;
5012 vd0->vdev_cant_write = B_TRUE;
5014 guid0 = vd0->vdev_guid;
5018 * Inject errors on an l2cache device.
5020 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5022 if (sav->sav_count == 0) {
5023 spa_config_exit(spa, SCL_STATE, FTAG);
5024 (void) rw_unlock(&ztest_name_lock);
5027 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5028 guid0 = vd0->vdev_guid;
5029 (void) strcpy(path0, vd0->vdev_path);
5030 (void) strcpy(pathrand, vd0->vdev_path);
5034 maxfaults = INT_MAX; /* no limit on cache devices */
5037 spa_config_exit(spa, SCL_STATE, FTAG);
5038 (void) rw_unlock(&ztest_name_lock);
5041 * If we can tolerate two or more faults, or we're dealing
5042 * with a slog, randomly online/offline vd0.
5044 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5045 if (ztest_random(10) < 6) {
5046 int flags = (ztest_random(2) == 0 ?
5047 ZFS_OFFLINE_TEMPORARY : 0);
5050 * We have to grab the zs_name_lock as writer to
5051 * prevent a race between offlining a slog and
5052 * destroying a dataset. Offlining the slog will
5053 * grab a reference on the dataset which may cause
5054 * dsl_destroy_head() to fail with EBUSY thus
5055 * leaving the dataset in an inconsistent state.
5058 (void) rw_wrlock(&ztest_name_lock);
5060 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5063 (void) rw_unlock(&ztest_name_lock);
5066 * Ideally we would like to be able to randomly
5067 * call vdev_[on|off]line without holding locks
5068 * to force unpredictable failures but the side
5069 * effects of vdev_[on|off]line prevent us from
5070 * doing so. We grab the ztest_vdev_lock here to
5071 * prevent a race between injection testing and
5074 mutex_enter(&ztest_vdev_lock);
5075 (void) vdev_online(spa, guid0, 0, NULL);
5076 mutex_exit(&ztest_vdev_lock);
5084 * We have at least single-fault tolerance, so inject data corruption.
5086 fd = open(pathrand, O_RDWR);
5088 if (fd == -1) /* we hit a gap in the device namespace */
5091 fsize = lseek(fd, 0, SEEK_END);
5093 while (--iters != 0) {
5094 offset = ztest_random(fsize / (leaves << bshift)) *
5095 (leaves << bshift) + (leaf << bshift) +
5096 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5098 if (offset >= fsize)
5101 mutex_enter(&ztest_vdev_lock);
5102 if (mirror_save != zs->zs_mirrors) {
5103 mutex_exit(&ztest_vdev_lock);
5108 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5109 fatal(1, "can't inject bad word at 0x%llx in %s",
5112 mutex_exit(&ztest_vdev_lock);
5114 if (ztest_opts.zo_verbose >= 7)
5115 (void) printf("injected bad word into %s,"
5116 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5121 umem_free(path0, MAXPATHLEN);
5122 umem_free(pathrand, MAXPATHLEN);
5126 * Verify that DDT repair works as expected.
5129 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5131 ztest_shared_t *zs = ztest_shared;
5132 spa_t *spa = ztest_spa;
5133 objset_t *os = zd->zd_os;
5135 uint64_t object, blocksize, txg, pattern, psize;
5136 enum zio_checksum checksum = spa_dedup_checksum(spa);
5141 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5144 blocksize = ztest_random_blocksize();
5145 blocksize = MIN(blocksize, 2048); /* because we write so many */
5147 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5148 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0);
5150 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5151 umem_free(od, sizeof (ztest_od_t));
5156 * Take the name lock as writer to prevent anyone else from changing
5157 * the pool and dataset properies we need to maintain during this test.
5159 (void) rw_wrlock(&ztest_name_lock);
5161 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5163 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5165 (void) rw_unlock(&ztest_name_lock);
5166 umem_free(od, sizeof (ztest_od_t));
5170 object = od[0].od_object;
5171 blocksize = od[0].od_blocksize;
5172 pattern = zs->zs_guid ^ dmu_objset_fsid_guid(os);
5174 ASSERT(object != 0);
5176 tx = dmu_tx_create(os);
5177 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5178 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5180 (void) rw_unlock(&ztest_name_lock);
5181 umem_free(od, sizeof (ztest_od_t));
5186 * Write all the copies of our block.
5188 for (i = 0; i < copies; i++) {
5189 uint64_t offset = i * blocksize;
5190 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5191 DMU_READ_NO_PREFETCH);
5193 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5194 os, (long long)object, (long long) offset, error);
5196 ASSERT(db->db_offset == offset);
5197 ASSERT(db->db_size == blocksize);
5198 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5199 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5200 dmu_buf_will_fill(db, tx);
5201 ztest_pattern_set(db->db_data, db->db_size, pattern);
5202 dmu_buf_rele(db, FTAG);
5206 txg_wait_synced(spa_get_dsl(spa), txg);
5209 * Find out what block we got.
5211 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5212 DMU_READ_NO_PREFETCH));
5213 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5214 dmu_buf_rele(db, FTAG);
5217 * Damage the block. Dedup-ditto will save us when we read it later.
5219 psize = BP_GET_PSIZE(&blk);
5220 buf = zio_buf_alloc(psize);
5221 ztest_pattern_set(buf, psize, ~pattern);
5223 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5224 buf, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5225 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5227 zio_buf_free(buf, psize);
5229 (void) rw_unlock(&ztest_name_lock);
5230 umem_free(od, sizeof (ztest_od_t));
5238 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5240 spa_t *spa = ztest_spa;
5242 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5243 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5244 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5248 * Change the guid for the pool.
5252 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5254 spa_t *spa = ztest_spa;
5255 uint64_t orig, load;
5258 orig = spa_guid(spa);
5259 load = spa_load_guid(spa);
5261 (void) rw_wrlock(&ztest_name_lock);
5262 error = spa_change_guid(spa);
5263 (void) rw_unlock(&ztest_name_lock);
5268 if (ztest_opts.zo_verbose >= 4) {
5269 (void) printf("Changed guid old %llu -> %llu\n",
5270 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5273 VERIFY3U(orig, !=, spa_guid(spa));
5274 VERIFY3U(load, ==, spa_load_guid(spa));
5278 * Rename the pool to a different name and then rename it back.
5282 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5284 char *oldname, *newname;
5287 (void) rw_wrlock(&ztest_name_lock);
5289 oldname = ztest_opts.zo_pool;
5290 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5291 (void) strcpy(newname, oldname);
5292 (void) strcat(newname, "_tmp");
5297 VERIFY3U(0, ==, spa_rename(oldname, newname));
5300 * Try to open it under the old name, which shouldn't exist
5302 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5305 * Open it under the new name and make sure it's still the same spa_t.
5307 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5309 ASSERT(spa == ztest_spa);
5310 spa_close(spa, FTAG);
5313 * Rename it back to the original
5315 VERIFY3U(0, ==, spa_rename(newname, oldname));
5318 * Make sure it can still be opened
5320 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5322 ASSERT(spa == ztest_spa);
5323 spa_close(spa, FTAG);
5325 umem_free(newname, strlen(newname) + 1);
5327 (void) rw_unlock(&ztest_name_lock);
5331 * Verify pool integrity by running zdb.
5334 ztest_run_zdb(char *pool)
5342 bin = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5343 zdb = umem_alloc(MAXPATHLEN + MAXNAMELEN + 20, UMEM_NOFAIL);
5344 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5346 VERIFY(realpath(getexecname(), bin) != NULL);
5347 if (strncmp(bin, "/usr/sbin/ztest", 15) == 0) {
5348 strcpy(bin, "/usr/sbin/zdb"); /* Installed */
5349 } else if (strncmp(bin, "/sbin/ztest", 11) == 0) {
5350 strcpy(bin, "/sbin/zdb"); /* Installed */
5352 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5353 strcat(bin, "/zdb/zdb");
5357 "%s -bcc%s%s -d -U %s %s",
5359 ztest_opts.zo_verbose >= 3 ? "s" : "",
5360 ztest_opts.zo_verbose >= 4 ? "v" : "",
5364 if (ztest_opts.zo_verbose >= 5)
5365 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5367 fp = popen(zdb, "r");
5369 while (fgets(zbuf, 1024, fp) != NULL)
5370 if (ztest_opts.zo_verbose >= 3)
5371 (void) printf("%s", zbuf);
5373 status = pclose(fp);
5378 ztest_dump_core = 0;
5379 if (WIFEXITED(status))
5380 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5382 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5384 umem_free(bin, MAXPATHLEN + MAXNAMELEN + 20);
5385 umem_free(zdb, MAXPATHLEN + MAXNAMELEN + 20);
5386 umem_free(zbuf, 1024);
5390 ztest_walk_pool_directory(char *header)
5394 if (ztest_opts.zo_verbose >= 6)
5395 (void) printf("%s\n", header);
5397 mutex_enter(&spa_namespace_lock);
5398 while ((spa = spa_next(spa)) != NULL)
5399 if (ztest_opts.zo_verbose >= 6)
5400 (void) printf("\t%s\n", spa_name(spa));
5401 mutex_exit(&spa_namespace_lock);
5405 ztest_spa_import_export(char *oldname, char *newname)
5407 nvlist_t *config, *newconfig;
5412 if (ztest_opts.zo_verbose >= 4) {
5413 (void) printf("import/export: old = %s, new = %s\n",
5418 * Clean up from previous runs.
5420 (void) spa_destroy(newname);
5423 * Get the pool's configuration and guid.
5425 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5428 * Kick off a scrub to tickle scrub/export races.
5430 if (ztest_random(2) == 0)
5431 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5433 pool_guid = spa_guid(spa);
5434 spa_close(spa, FTAG);
5436 ztest_walk_pool_directory("pools before export");
5441 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5443 ztest_walk_pool_directory("pools after export");
5448 newconfig = spa_tryimport(config);
5449 ASSERT(newconfig != NULL);
5450 nvlist_free(newconfig);
5453 * Import it under the new name.
5455 error = spa_import(newname, config, NULL, 0);
5457 dump_nvlist(config, 0);
5458 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
5459 oldname, newname, error);
5462 ztest_walk_pool_directory("pools after import");
5465 * Try to import it again -- should fail with EEXIST.
5467 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
5470 * Try to import it under a different name -- should fail with EEXIST.
5472 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
5475 * Verify that the pool is no longer visible under the old name.
5477 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5480 * Verify that we can open and close the pool using the new name.
5482 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5483 ASSERT(pool_guid == spa_guid(spa));
5484 spa_close(spa, FTAG);
5486 nvlist_free(config);
5490 ztest_resume(spa_t *spa)
5492 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
5493 (void) printf("resuming from suspended state\n");
5494 spa_vdev_state_enter(spa, SCL_NONE);
5495 vdev_clear(spa, NULL);
5496 (void) spa_vdev_state_exit(spa, NULL, 0);
5497 (void) zio_resume(spa);
5501 ztest_resume_thread(void *arg)
5505 while (!ztest_exiting) {
5506 if (spa_suspended(spa))
5508 (void) poll(NULL, 0, 100);
5520 ztest_deadman_alarm(int sig)
5522 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
5527 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
5529 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
5530 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
5531 hrtime_t functime = gethrtime();
5534 for (i = 0; i < zi->zi_iters; i++)
5535 zi->zi_func(zd, id);
5537 functime = gethrtime() - functime;
5539 atomic_add_64(&zc->zc_count, 1);
5540 atomic_add_64(&zc->zc_time, functime);
5542 if (ztest_opts.zo_verbose >= 4) {
5544 (void) dladdr((void *)zi->zi_func, &dli);
5545 (void) printf("%6.2f sec in %s\n",
5546 (double)functime / NANOSEC, dli.dli_sname);
5551 ztest_thread(void *arg)
5554 uint64_t id = (uintptr_t)arg;
5555 ztest_shared_t *zs = ztest_shared;
5559 ztest_shared_callstate_t *zc;
5561 while ((now = gethrtime()) < zs->zs_thread_stop) {
5563 * See if it's time to force a crash.
5565 if (now > zs->zs_thread_kill)
5569 * If we're getting ENOSPC with some regularity, stop.
5571 if (zs->zs_enospc_count > 10)
5575 * Pick a random function to execute.
5577 rand = ztest_random(ZTEST_FUNCS);
5578 zi = &ztest_info[rand];
5579 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
5580 call_next = zc->zc_next;
5582 if (now >= call_next &&
5583 atomic_cas_64(&zc->zc_next, call_next, call_next +
5584 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
5585 ztest_execute(rand, zi, id);
5595 ztest_dataset_name(char *dsname, char *pool, int d)
5597 (void) snprintf(dsname, MAXNAMELEN, "%s/ds_%d", pool, d);
5601 ztest_dataset_destroy(int d)
5603 char name[MAXNAMELEN];
5606 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5608 if (ztest_opts.zo_verbose >= 3)
5609 (void) printf("Destroying %s to free up space\n", name);
5612 * Cleanup any non-standard clones and snapshots. In general,
5613 * ztest thread t operates on dataset (t % zopt_datasets),
5614 * so there may be more than one thing to clean up.
5616 for (t = d; t < ztest_opts.zo_threads;
5617 t += ztest_opts.zo_datasets)
5618 ztest_dsl_dataset_cleanup(name, t);
5620 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
5621 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5625 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
5627 uint64_t usedobjs, dirobjs, scratch;
5630 * ZTEST_DIROBJ is the object directory for the entire dataset.
5631 * Therefore, the number of objects in use should equal the
5632 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
5633 * If not, we have an object leak.
5635 * Note that we can only check this in ztest_dataset_open(),
5636 * when the open-context and syncing-context values agree.
5637 * That's because zap_count() returns the open-context value,
5638 * while dmu_objset_space() returns the rootbp fill count.
5640 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
5641 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
5642 ASSERT3U(dirobjs + 1, ==, usedobjs);
5646 ztest_dataset_open(int d)
5648 ztest_ds_t *zd = &ztest_ds[d];
5649 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
5652 char name[MAXNAMELEN];
5655 ztest_dataset_name(name, ztest_opts.zo_pool, d);
5657 (void) rw_rdlock(&ztest_name_lock);
5659 error = ztest_dataset_create(name);
5660 if (error == ENOSPC) {
5661 (void) rw_unlock(&ztest_name_lock);
5662 ztest_record_enospc(FTAG);
5665 ASSERT(error == 0 || error == EEXIST);
5667 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
5668 (void) rw_unlock(&ztest_name_lock);
5670 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
5672 zilog = zd->zd_zilog;
5674 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
5675 zilog->zl_header->zh_claim_lr_seq < committed_seq)
5676 fatal(0, "missing log records: claimed %llu < committed %llu",
5677 zilog->zl_header->zh_claim_lr_seq, committed_seq);
5679 ztest_dataset_dirobj_verify(zd);
5681 zil_replay(os, zd, ztest_replay_vector);
5683 ztest_dataset_dirobj_verify(zd);
5685 if (ztest_opts.zo_verbose >= 6)
5686 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
5688 (u_longlong_t)zilog->zl_parse_blk_count,
5689 (u_longlong_t)zilog->zl_parse_lr_count,
5690 (u_longlong_t)zilog->zl_replaying_seq);
5692 zilog = zil_open(os, ztest_get_data);
5694 if (zilog->zl_replaying_seq != 0 &&
5695 zilog->zl_replaying_seq < committed_seq)
5696 fatal(0, "missing log records: replayed %llu < committed %llu",
5697 zilog->zl_replaying_seq, committed_seq);
5703 ztest_dataset_close(int d)
5705 ztest_ds_t *zd = &ztest_ds[d];
5707 zil_close(zd->zd_zilog);
5708 dmu_objset_disown(zd->zd_os, zd);
5714 * Kick off threads to run tests on all datasets in parallel.
5717 ztest_run(ztest_shared_t *zs)
5722 kthread_t *resume_thread;
5727 ztest_exiting = B_FALSE;
5730 * Initialize parent/child shared state.
5732 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
5733 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
5735 zs->zs_thread_start = gethrtime();
5736 zs->zs_thread_stop =
5737 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
5738 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
5739 zs->zs_thread_kill = zs->zs_thread_stop;
5740 if (ztest_random(100) < ztest_opts.zo_killrate) {
5741 zs->zs_thread_kill -=
5742 ztest_random(ztest_opts.zo_passtime * NANOSEC);
5745 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
5747 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
5748 offsetof(ztest_cb_data_t, zcd_node));
5753 kernel_init(FREAD | FWRITE);
5754 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
5755 spa->spa_debug = B_TRUE;
5756 metaslab_preload_limit = ztest_random(20) + 1;
5759 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
5760 DMU_OST_ANY, B_TRUE, FTAG, &os));
5761 zs->zs_guid = dmu_objset_fsid_guid(os);
5762 dmu_objset_disown(os, FTAG);
5764 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
5767 * We don't expect the pool to suspend unless maxfaults == 0,
5768 * in which case ztest_fault_inject() temporarily takes away
5769 * the only valid replica.
5771 if (MAXFAULTS() == 0)
5772 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
5774 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
5777 * Create a thread to periodically resume suspended I/O.
5779 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
5780 (thread_func_t)ztest_resume_thread, spa, TS_RUN, NULL, 0, 0,
5781 PTHREAD_CREATE_JOINABLE)), !=, NULL);
5785 * Set a deadman alarm to abort() if we hang.
5787 signal(SIGALRM, ztest_deadman_alarm);
5788 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
5792 * Verify that we can safely inquire about about any object,
5793 * whether it's allocated or not. To make it interesting,
5794 * we probe a 5-wide window around each power of two.
5795 * This hits all edge cases, including zero and the max.
5797 for (t = 0; t < 64; t++) {
5798 for (d = -5; d <= 5; d++) {
5799 error = dmu_object_info(spa->spa_meta_objset,
5800 (1ULL << t) + d, NULL);
5801 ASSERT(error == 0 || error == ENOENT ||
5807 * If we got any ENOSPC errors on the previous run, destroy something.
5809 if (zs->zs_enospc_count != 0) {
5810 int d = ztest_random(ztest_opts.zo_datasets);
5811 ztest_dataset_destroy(d);
5813 zs->zs_enospc_count = 0;
5815 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
5818 if (ztest_opts.zo_verbose >= 4)
5819 (void) printf("starting main threads...\n");
5822 * Kick off all the tests that run in parallel.
5824 for (t = 0; t < ztest_opts.zo_threads; t++) {
5827 if (t < ztest_opts.zo_datasets &&
5828 ztest_dataset_open(t) != 0)
5831 VERIFY3P(thread = zk_thread_create(NULL, 0,
5832 (thread_func_t)ztest_thread,
5833 (void *)(uintptr_t)t, TS_RUN, NULL, 0, 0,
5834 PTHREAD_CREATE_JOINABLE), !=, NULL);
5835 tid[t] = thread->t_tid;
5839 * Wait for all of the tests to complete. We go in reverse order
5840 * so we don't close datasets while threads are still using them.
5842 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
5843 thread_join(tid[t]);
5844 if (t < ztest_opts.zo_datasets)
5845 ztest_dataset_close(t);
5848 txg_wait_synced(spa_get_dsl(spa), 0);
5850 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
5851 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
5853 if (ztest_opts.zo_verbose >= 3)
5854 zfs_dbgmsg_print(FTAG);
5856 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
5858 /* Kill the resume thread */
5859 ztest_exiting = B_TRUE;
5860 thread_join(resume_thread->t_tid);
5864 * Right before closing the pool, kick off a bunch of async I/O;
5865 * spa_close() should wait for it to complete.
5867 for (object = 1; object < 50; object++)
5868 dmu_prefetch(spa->spa_meta_objset, object, 0, 1ULL << 20);
5870 /* Verify that at least one commit cb was called in a timely fashion */
5871 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
5872 VERIFY0(zc_min_txg_delay);
5874 spa_close(spa, FTAG);
5877 * Verify that we can loop over all pools.
5879 mutex_enter(&spa_namespace_lock);
5880 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
5881 if (ztest_opts.zo_verbose > 3)
5882 (void) printf("spa_next: found %s\n", spa_name(spa));
5883 mutex_exit(&spa_namespace_lock);
5886 * Verify that we can export the pool and reimport it under a
5889 if (ztest_random(2) == 0) {
5890 char name[MAXNAMELEN];
5891 (void) snprintf(name, MAXNAMELEN, "%s_import",
5892 ztest_opts.zo_pool);
5893 ztest_spa_import_export(ztest_opts.zo_pool, name);
5894 ztest_spa_import_export(name, ztest_opts.zo_pool);
5899 list_destroy(&zcl.zcl_callbacks);
5900 mutex_destroy(&zcl.zcl_callbacks_lock);
5901 (void) rwlock_destroy(&ztest_name_lock);
5902 mutex_destroy(&ztest_vdev_lock);
5908 ztest_ds_t *zd = &ztest_ds[0];
5912 if (ztest_opts.zo_verbose >= 3)
5913 (void) printf("testing spa_freeze()...\n");
5915 kernel_init(FREAD | FWRITE);
5916 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5917 VERIFY3U(0, ==, ztest_dataset_open(0));
5918 spa->spa_debug = B_TRUE;
5922 * Force the first log block to be transactionally allocated.
5923 * We have to do this before we freeze the pool -- otherwise
5924 * the log chain won't be anchored.
5926 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
5927 ztest_dmu_object_alloc_free(zd, 0);
5928 zil_commit(zd->zd_zilog, 0);
5931 txg_wait_synced(spa_get_dsl(spa), 0);
5934 * Freeze the pool. This stops spa_sync() from doing anything,
5935 * so that the only way to record changes from now on is the ZIL.
5940 * Run tests that generate log records but don't alter the pool config
5941 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
5942 * We do a txg_wait_synced() after each iteration to force the txg
5943 * to increase well beyond the last synced value in the uberblock.
5944 * The ZIL should be OK with that.
5946 while (ztest_random(10) != 0 &&
5947 numloops++ < ztest_opts.zo_maxloops) {
5948 ztest_dmu_write_parallel(zd, 0);
5949 ztest_dmu_object_alloc_free(zd, 0);
5950 txg_wait_synced(spa_get_dsl(spa), 0);
5954 * Commit all of the changes we just generated.
5956 zil_commit(zd->zd_zilog, 0);
5957 txg_wait_synced(spa_get_dsl(spa), 0);
5960 * Close our dataset and close the pool.
5962 ztest_dataset_close(0);
5963 spa_close(spa, FTAG);
5967 * Open and close the pool and dataset to induce log replay.
5969 kernel_init(FREAD | FWRITE);
5970 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
5971 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
5972 VERIFY3U(0, ==, ztest_dataset_open(0));
5973 ztest_dataset_close(0);
5975 spa->spa_debug = B_TRUE;
5977 txg_wait_synced(spa_get_dsl(spa), 0);
5978 ztest_reguid(NULL, 0);
5980 spa_close(spa, FTAG);
5985 print_time(hrtime_t t, char *timebuf)
5987 hrtime_t s = t / NANOSEC;
5988 hrtime_t m = s / 60;
5989 hrtime_t h = m / 60;
5990 hrtime_t d = h / 24;
5999 (void) sprintf(timebuf,
6000 "%llud%02lluh%02llum%02llus", d, h, m, s);
6002 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6004 (void) sprintf(timebuf, "%llum%02llus", m, s);
6006 (void) sprintf(timebuf, "%llus", s);
6010 make_random_props(void)
6014 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6015 if (ztest_random(2) == 0)
6017 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6023 * Create a storage pool with the given name and initial vdev size.
6024 * Then test spa_freeze() functionality.
6027 ztest_init(ztest_shared_t *zs)
6030 nvlist_t *nvroot, *props;
6033 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6034 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6036 kernel_init(FREAD | FWRITE);
6039 * Create the storage pool.
6041 (void) spa_destroy(ztest_opts.zo_pool);
6042 ztest_shared->zs_vdev_next_leaf = 0;
6044 zs->zs_mirrors = ztest_opts.zo_mirrors;
6045 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6046 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6047 props = make_random_props();
6048 for (i = 0; i < SPA_FEATURES; i++) {
6050 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6051 spa_feature_table[i].fi_uname));
6052 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6055 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6056 nvlist_free(nvroot);
6059 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6060 zs->zs_metaslab_sz =
6061 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6062 spa_close(spa, FTAG);
6066 ztest_run_zdb(ztest_opts.zo_pool);
6070 ztest_run_zdb(ztest_opts.zo_pool);
6072 (void) rwlock_destroy(&ztest_name_lock);
6073 mutex_destroy(&ztest_vdev_lock);
6079 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6081 ztest_fd_data = mkstemp(ztest_name_data);
6082 ASSERT3S(ztest_fd_data, >=, 0);
6083 (void) unlink(ztest_name_data);
6087 shared_data_size(ztest_shared_hdr_t *hdr)
6091 size = hdr->zh_hdr_size;
6092 size += hdr->zh_opts_size;
6093 size += hdr->zh_size;
6094 size += hdr->zh_stats_size * hdr->zh_stats_count;
6095 size += hdr->zh_ds_size * hdr->zh_ds_count;
6104 ztest_shared_hdr_t *hdr;
6106 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6107 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6108 VERIFY3P(hdr, !=, MAP_FAILED);
6110 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6112 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6113 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6114 hdr->zh_size = sizeof (ztest_shared_t);
6115 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6116 hdr->zh_stats_count = ZTEST_FUNCS;
6117 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6118 hdr->zh_ds_count = ztest_opts.zo_datasets;
6120 size = shared_data_size(hdr);
6121 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6123 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6130 ztest_shared_hdr_t *hdr;
6133 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6134 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6135 VERIFY3P(hdr, !=, MAP_FAILED);
6137 size = shared_data_size(hdr);
6139 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6140 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6141 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6142 VERIFY3P(hdr, !=, MAP_FAILED);
6143 buf = (uint8_t *)hdr;
6145 offset = hdr->zh_hdr_size;
6146 ztest_shared_opts = (void *)&buf[offset];
6147 offset += hdr->zh_opts_size;
6148 ztest_shared = (void *)&buf[offset];
6149 offset += hdr->zh_size;
6150 ztest_shared_callstate = (void *)&buf[offset];
6151 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6152 ztest_shared_ds = (void *)&buf[offset];
6156 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6160 char *cmdbuf = NULL;
6165 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6166 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6171 fatal(1, "fork failed");
6173 if (pid == 0) { /* child */
6174 char *emptyargv[2] = { cmd, NULL };
6175 char fd_data_str[12];
6177 struct rlimit rl = { 1024, 1024 };
6178 (void) setrlimit(RLIMIT_NOFILE, &rl);
6180 (void) close(ztest_fd_rand);
6181 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6182 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6184 (void) enable_extended_FILE_stdio(-1, -1);
6185 if (libpath != NULL)
6186 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6187 (void) execv(cmd, emptyargv);
6188 ztest_dump_core = B_FALSE;
6189 fatal(B_TRUE, "exec failed: %s", cmd);
6192 if (cmdbuf != NULL) {
6193 umem_free(cmdbuf, MAXPATHLEN);
6197 while (waitpid(pid, &status, 0) != pid)
6199 if (statusp != NULL)
6202 if (WIFEXITED(status)) {
6203 if (WEXITSTATUS(status) != 0) {
6204 (void) fprintf(stderr, "child exited with code %d\n",
6205 WEXITSTATUS(status));
6209 } else if (WIFSIGNALED(status)) {
6210 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6211 (void) fprintf(stderr, "child died with signal %d\n",
6217 (void) fprintf(stderr, "something strange happened to child\n");
6224 ztest_run_init(void)
6228 ztest_shared_t *zs = ztest_shared;
6230 ASSERT(ztest_opts.zo_init != 0);
6233 * Blow away any existing copy of zpool.cache
6235 (void) remove(spa_config_path);
6238 * Create and initialize our storage pool.
6240 for (i = 1; i <= ztest_opts.zo_init; i++) {
6241 bzero(zs, sizeof (ztest_shared_t));
6242 if (ztest_opts.zo_verbose >= 3 &&
6243 ztest_opts.zo_init != 1) {
6244 (void) printf("ztest_init(), pass %d\n", i);
6251 main(int argc, char **argv)
6259 ztest_shared_callstate_t *zc;
6266 char *fd_data_str = getenv("ZTEST_FD_DATA");
6268 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6270 dprintf_setup(&argc, argv);
6272 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6273 ASSERT3S(ztest_fd_rand, >=, 0);
6276 process_options(argc, argv);
6281 bcopy(&ztest_opts, ztest_shared_opts,
6282 sizeof (*ztest_shared_opts));
6284 ztest_fd_data = atoi(fd_data_str);
6286 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6288 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6290 /* Override location of zpool.cache */
6291 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6292 ztest_opts.zo_dir) != -1);
6294 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6299 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6300 metaslab_df_alloc_threshold =
6301 zs->zs_metaslab_df_alloc_threshold;
6310 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6312 if (ztest_opts.zo_verbose >= 1) {
6313 (void) printf("%llu vdevs, %d datasets, %d threads,"
6314 " %llu seconds...\n",
6315 (u_longlong_t)ztest_opts.zo_vdevs,
6316 ztest_opts.zo_datasets,
6317 ztest_opts.zo_threads,
6318 (u_longlong_t)ztest_opts.zo_time);
6321 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6322 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6324 zs->zs_do_init = B_TRUE;
6325 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6326 if (ztest_opts.zo_verbose >= 1) {
6327 (void) printf("Executing older ztest for "
6328 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6330 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6331 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6333 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6335 zs->zs_do_init = B_FALSE;
6337 zs->zs_proc_start = gethrtime();
6338 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6340 for (f = 0; f < ZTEST_FUNCS; f++) {
6341 zi = &ztest_info[f];
6342 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6343 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6344 zc->zc_next = UINT64_MAX;
6346 zc->zc_next = zs->zs_proc_start +
6347 ztest_random(2 * zi->zi_interval[0] + 1);
6351 * Run the tests in a loop. These tests include fault injection
6352 * to verify that self-healing data works, and forced crashes
6353 * to verify that we never lose on-disk consistency.
6355 while (gethrtime() < zs->zs_proc_stop) {
6360 * Initialize the workload counters for each function.
6362 for (f = 0; f < ZTEST_FUNCS; f++) {
6363 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6368 /* Set the allocation switch size */
6369 zs->zs_metaslab_df_alloc_threshold =
6370 ztest_random(zs->zs_metaslab_sz / 4) + 1;
6372 if (!hasalt || ztest_random(2) == 0) {
6373 if (hasalt && ztest_opts.zo_verbose >= 1) {
6374 (void) printf("Executing newer ztest: %s\n",
6378 killed = exec_child(cmd, NULL, B_TRUE, &status);
6380 if (hasalt && ztest_opts.zo_verbose >= 1) {
6381 (void) printf("Executing older ztest: %s\n",
6382 ztest_opts.zo_alt_ztest);
6385 killed = exec_child(ztest_opts.zo_alt_ztest,
6386 ztest_opts.zo_alt_libpath, B_TRUE, &status);
6393 if (ztest_opts.zo_verbose >= 1) {
6394 hrtime_t now = gethrtime();
6396 now = MIN(now, zs->zs_proc_stop);
6397 print_time(zs->zs_proc_stop - now, timebuf);
6398 nicenum(zs->zs_space, numbuf);
6400 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
6401 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
6403 WIFEXITED(status) ? "Complete" : "SIGKILL",
6404 (u_longlong_t)zs->zs_enospc_count,
6405 100.0 * zs->zs_alloc / zs->zs_space,
6407 100.0 * (now - zs->zs_proc_start) /
6408 (ztest_opts.zo_time * NANOSEC), timebuf);
6411 if (ztest_opts.zo_verbose >= 2) {
6412 (void) printf("\nWorkload summary:\n\n");
6413 (void) printf("%7s %9s %s\n",
6414 "Calls", "Time", "Function");
6415 (void) printf("%7s %9s %s\n",
6416 "-----", "----", "--------");
6417 for (f = 0; f < ZTEST_FUNCS; f++) {
6420 zi = &ztest_info[f];
6421 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6422 print_time(zc->zc_time, timebuf);
6423 (void) dladdr((void *)zi->zi_func, &dli);
6424 (void) printf("%7llu %9s %s\n",
6425 (u_longlong_t)zc->zc_count, timebuf,
6428 (void) printf("\n");
6432 * It's possible that we killed a child during a rename test,
6433 * in which case we'll have a 'ztest_tmp' pool lying around
6434 * instead of 'ztest'. Do a blind rename in case this happened.
6437 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
6438 spa_close(spa, FTAG);
6440 char tmpname[MAXNAMELEN];
6442 kernel_init(FREAD | FWRITE);
6443 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
6444 ztest_opts.zo_pool);
6445 (void) spa_rename(tmpname, ztest_opts.zo_pool);
6449 ztest_run_zdb(ztest_opts.zo_pool);
6452 if (ztest_opts.zo_verbose >= 1) {
6454 (void) printf("%d runs of older ztest: %s\n", older,
6455 ztest_opts.zo_alt_ztest);
6456 (void) printf("%d runs of newer ztest: %s\n", newer,
6459 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
6460 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
6463 umem_free(cmd, MAXNAMELEN);