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, 2016 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/zfs_rlock.h>
105 #include <sys/vdev_impl.h>
106 #include <sys/vdev_file.h>
107 #include <sys/spa_impl.h>
108 #include <sys/metaslab_impl.h>
109 #include <sys/dsl_prop.h>
110 #include <sys/dsl_dataset.h>
111 #include <sys/dsl_destroy.h>
112 #include <sys/dsl_scan.h>
113 #include <sys/zio_checksum.h>
114 #include <sys/refcount.h>
115 #include <sys/zfeature.h>
116 #include <sys/dsl_userhold.h>
119 #include <stdio_ext.h>
126 #include <sys/fs/zfs.h>
127 #include <zfs_fletcher.h>
128 #include <libnvpair.h>
131 #include <execinfo.h> /* for backtrace() */
134 static int ztest_fd_data = -1;
135 static int ztest_fd_rand = -1;
137 typedef struct ztest_shared_hdr {
138 uint64_t zh_hdr_size;
139 uint64_t zh_opts_size;
141 uint64_t zh_stats_size;
142 uint64_t zh_stats_count;
144 uint64_t zh_ds_count;
145 } ztest_shared_hdr_t;
147 static ztest_shared_hdr_t *ztest_shared_hdr;
149 typedef struct ztest_shared_opts {
150 char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
151 char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
152 char zo_alt_ztest[MAXNAMELEN];
153 char zo_alt_libpath[MAXNAMELEN];
155 uint64_t zo_vdevtime;
163 uint64_t zo_passtime;
164 uint64_t zo_killrate;
168 uint64_t zo_maxloops;
169 uint64_t zo_metaslab_gang_bang;
171 } ztest_shared_opts_t;
173 static const ztest_shared_opts_t ztest_opts_defaults = {
174 .zo_pool = { 'z', 't', 'e', 's', 't', '\0' },
175 .zo_dir = { '/', 't', 'm', 'p', '\0' },
176 .zo_alt_ztest = { '\0' },
177 .zo_alt_libpath = { '\0' },
179 .zo_ashift = SPA_MINBLOCKSHIFT,
182 .zo_raidz_parity = 1,
183 .zo_vdev_size = SPA_MINDEVSIZE * 4, /* 256m default size */
186 .zo_passtime = 60, /* 60 seconds */
187 .zo_killrate = 70, /* 70% kill rate */
191 .zo_time = 300, /* 5 minutes */
192 .zo_maxloops = 50, /* max loops during spa_freeze() */
193 .zo_metaslab_gang_bang = 32 << 10
196 extern uint64_t metaslab_gang_bang;
197 extern uint64_t metaslab_df_alloc_threshold;
198 extern int metaslab_preload_limit;
199 extern boolean_t zfs_compressed_arc_enabled;
200 extern int zfs_abd_scatter_enabled;
202 static ztest_shared_opts_t *ztest_shared_opts;
203 static ztest_shared_opts_t ztest_opts;
205 typedef struct ztest_shared_ds {
209 static ztest_shared_ds_t *ztest_shared_ds;
210 #define ZTEST_GET_SHARED_DS(d) (&ztest_shared_ds[d])
212 #define BT_MAGIC 0x123456789abcdefULL
213 #define MAXFAULTS() \
214 (MAX(zs->zs_mirrors, 1) * (ztest_opts.zo_raidz_parity + 1) - 1)
218 ZTEST_IO_WRITE_PATTERN,
219 ZTEST_IO_WRITE_ZEROES,
226 typedef struct ztest_block_tag {
230 uint64_t bt_dnodesize;
237 typedef struct bufwad {
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;
268 uint64_t od_crdnodesize;
271 char od_name[ZFS_MAX_DATASET_NAME_LEN];
277 typedef struct ztest_ds {
278 ztest_shared_ds_t *zd_shared;
280 rwlock_t zd_zilog_lock;
282 ztest_od_t *zd_od; /* debugging aid */
283 char zd_name[ZFS_MAX_DATASET_NAME_LEN];
284 kmutex_t zd_dirobj_lock;
285 rll_t zd_object_lock[ZTEST_OBJECT_LOCKS];
286 zll_t zd_range_lock[ZTEST_RANGE_LOCKS];
290 * Per-iteration state.
292 typedef void ztest_func_t(ztest_ds_t *zd, uint64_t id);
294 typedef struct ztest_info {
295 ztest_func_t *zi_func; /* test function */
296 uint64_t zi_iters; /* iterations per execution */
297 uint64_t *zi_interval; /* execute every <interval> seconds */
298 const char *zi_funcname; /* name of test function */
301 typedef struct ztest_shared_callstate {
302 uint64_t zc_count; /* per-pass count */
303 uint64_t zc_time; /* per-pass time */
304 uint64_t zc_next; /* next time to call this function */
305 } ztest_shared_callstate_t;
307 static ztest_shared_callstate_t *ztest_shared_callstate;
308 #define ZTEST_GET_SHARED_CALLSTATE(c) (&ztest_shared_callstate[c])
310 ztest_func_t ztest_dmu_read_write;
311 ztest_func_t ztest_dmu_write_parallel;
312 ztest_func_t ztest_dmu_object_alloc_free;
313 ztest_func_t ztest_dmu_commit_callbacks;
314 ztest_func_t ztest_zap;
315 ztest_func_t ztest_zap_parallel;
316 ztest_func_t ztest_zil_commit;
317 ztest_func_t ztest_zil_remount;
318 ztest_func_t ztest_dmu_read_write_zcopy;
319 ztest_func_t ztest_dmu_objset_create_destroy;
320 ztest_func_t ztest_dmu_prealloc;
321 ztest_func_t ztest_fzap;
322 ztest_func_t ztest_dmu_snapshot_create_destroy;
323 ztest_func_t ztest_dsl_prop_get_set;
324 ztest_func_t ztest_spa_prop_get_set;
325 ztest_func_t ztest_spa_create_destroy;
326 ztest_func_t ztest_fault_inject;
327 ztest_func_t ztest_ddt_repair;
328 ztest_func_t ztest_dmu_snapshot_hold;
329 ztest_func_t ztest_spa_rename;
330 ztest_func_t ztest_scrub;
331 ztest_func_t ztest_dsl_dataset_promote_busy;
332 ztest_func_t ztest_vdev_attach_detach;
333 ztest_func_t ztest_vdev_LUN_growth;
334 ztest_func_t ztest_vdev_add_remove;
335 ztest_func_t ztest_vdev_aux_add_remove;
336 ztest_func_t ztest_split_pool;
337 ztest_func_t ztest_reguid;
338 ztest_func_t ztest_spa_upgrade;
339 ztest_func_t ztest_fletcher;
340 ztest_func_t ztest_fletcher_incr;
341 ztest_func_t ztest_verify_dnode_bt;
343 uint64_t zopt_always = 0ULL * NANOSEC; /* all the time */
344 uint64_t zopt_incessant = 1ULL * NANOSEC / 10; /* every 1/10 second */
345 uint64_t zopt_often = 1ULL * NANOSEC; /* every second */
346 uint64_t zopt_sometimes = 10ULL * NANOSEC; /* every 10 seconds */
347 uint64_t zopt_rarely = 60ULL * NANOSEC; /* every 60 seconds */
349 #define ZTI_INIT(func, iters, interval) \
350 { .zi_func = (func), \
351 .zi_iters = (iters), \
352 .zi_interval = (interval), \
353 .zi_funcname = # func }
355 ztest_info_t ztest_info[] = {
356 ZTI_INIT(ztest_dmu_read_write, 1, &zopt_always),
357 ZTI_INIT(ztest_dmu_write_parallel, 10, &zopt_always),
358 ZTI_INIT(ztest_dmu_object_alloc_free, 1, &zopt_always),
359 ZTI_INIT(ztest_dmu_commit_callbacks, 1, &zopt_always),
360 ZTI_INIT(ztest_zap, 30, &zopt_always),
361 ZTI_INIT(ztest_zap_parallel, 100, &zopt_always),
362 ZTI_INIT(ztest_split_pool, 1, &zopt_always),
363 ZTI_INIT(ztest_zil_commit, 1, &zopt_incessant),
364 ZTI_INIT(ztest_zil_remount, 1, &zopt_sometimes),
365 ZTI_INIT(ztest_dmu_read_write_zcopy, 1, &zopt_often),
366 ZTI_INIT(ztest_dmu_objset_create_destroy, 1, &zopt_often),
367 ZTI_INIT(ztest_dsl_prop_get_set, 1, &zopt_often),
368 ZTI_INIT(ztest_spa_prop_get_set, 1, &zopt_sometimes),
370 ZTI_INIT(ztest_dmu_prealloc, 1, &zopt_sometimes),
372 ZTI_INIT(ztest_fzap, 1, &zopt_sometimes),
373 ZTI_INIT(ztest_dmu_snapshot_create_destroy, 1, &zopt_sometimes),
374 ZTI_INIT(ztest_spa_create_destroy, 1, &zopt_sometimes),
375 ZTI_INIT(ztest_fault_inject, 1, &zopt_sometimes),
376 ZTI_INIT(ztest_ddt_repair, 1, &zopt_sometimes),
377 ZTI_INIT(ztest_dmu_snapshot_hold, 1, &zopt_sometimes),
378 ZTI_INIT(ztest_reguid, 1, &zopt_rarely),
379 ZTI_INIT(ztest_spa_rename, 1, &zopt_rarely),
380 ZTI_INIT(ztest_scrub, 1, &zopt_rarely),
381 ZTI_INIT(ztest_spa_upgrade, 1, &zopt_rarely),
382 ZTI_INIT(ztest_dsl_dataset_promote_busy, 1, &zopt_rarely),
383 ZTI_INIT(ztest_vdev_attach_detach, 1, &zopt_sometimes),
384 ZTI_INIT(ztest_vdev_LUN_growth, 1, &zopt_rarely),
385 ZTI_INIT(ztest_vdev_add_remove, 1, &ztest_opts.zo_vdevtime),
386 ZTI_INIT(ztest_vdev_aux_add_remove, 1, &ztest_opts.zo_vdevtime),
387 ZTI_INIT(ztest_fletcher, 1, &zopt_rarely),
388 ZTI_INIT(ztest_fletcher_incr, 1, &zopt_rarely),
389 ZTI_INIT(ztest_verify_dnode_bt, 1, &zopt_sometimes),
392 #define ZTEST_FUNCS (sizeof (ztest_info) / sizeof (ztest_info_t))
395 * The following struct is used to hold a list of uncalled commit callbacks.
396 * The callbacks are ordered by txg number.
398 typedef struct ztest_cb_list {
399 kmutex_t zcl_callbacks_lock;
400 list_t zcl_callbacks;
404 * Stuff we need to share writably between parent and child.
406 typedef struct ztest_shared {
407 boolean_t zs_do_init;
408 hrtime_t zs_proc_start;
409 hrtime_t zs_proc_stop;
410 hrtime_t zs_thread_start;
411 hrtime_t zs_thread_stop;
412 hrtime_t zs_thread_kill;
413 uint64_t zs_enospc_count;
414 uint64_t zs_vdev_next_leaf;
415 uint64_t zs_vdev_aux;
420 uint64_t zs_metaslab_sz;
421 uint64_t zs_metaslab_df_alloc_threshold;
425 #define ID_PARALLEL -1ULL
427 static char ztest_dev_template[] = "%s/%s.%llua";
428 static char ztest_aux_template[] = "%s/%s.%s.%llu";
429 ztest_shared_t *ztest_shared;
431 static spa_t *ztest_spa = NULL;
432 static ztest_ds_t *ztest_ds;
434 static kmutex_t ztest_vdev_lock;
437 * The ztest_name_lock protects the pool and dataset namespace used by
438 * the individual tests. To modify the namespace, consumers must grab
439 * this lock as writer. Grabbing the lock as reader will ensure that the
440 * namespace does not change while the lock is held.
442 static rwlock_t ztest_name_lock;
444 static boolean_t ztest_dump_core = B_TRUE;
445 static boolean_t ztest_exiting;
447 /* Global commit callback list */
448 static ztest_cb_list_t zcl;
449 /* Commit cb delay */
450 static uint64_t zc_min_txg_delay = UINT64_MAX;
451 static int zc_cb_counter = 0;
454 * Minimum number of commit callbacks that need to be registered for us to check
455 * whether the minimum txg delay is acceptable.
457 #define ZTEST_COMMIT_CB_MIN_REG 100
460 * If a number of txgs equal to this threshold have been created after a commit
461 * callback has been registered but not called, then we assume there is an
462 * implementation bug.
464 #define ZTEST_COMMIT_CB_THRESH (TXG_CONCURRENT_STATES + 1000)
466 extern uint64_t metaslab_gang_bang;
467 extern uint64_t metaslab_df_alloc_threshold;
470 ZTEST_META_DNODE = 0,
475 static void usage(boolean_t) __NORETURN;
478 * These libumem hooks provide a reasonable set of defaults for the allocator's
479 * debugging facilities.
482 _umem_debug_init(void)
484 return ("default,verbose"); /* $UMEM_DEBUG setting */
488 _umem_logging_init(void)
490 return ("fail,contents"); /* $UMEM_LOGGING setting */
493 #define BACKTRACE_SZ 100
495 static void sig_handler(int signo)
497 struct sigaction action;
498 #ifdef __GLIBC__ /* backtrace() is a GNU extension */
500 void *buffer[BACKTRACE_SZ];
502 nptrs = backtrace(buffer, BACKTRACE_SZ);
503 backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
507 * Restore default action and re-raise signal so SIGSEGV and
508 * SIGABRT can trigger a core dump.
510 action.sa_handler = SIG_DFL;
511 sigemptyset(&action.sa_mask);
513 (void) sigaction(signo, &action, NULL);
517 #define FATAL_MSG_SZ 1024
522 fatal(int do_perror, char *message, ...)
525 int save_errno = errno;
528 (void) fflush(stdout);
529 buf = umem_alloc(FATAL_MSG_SZ, UMEM_NOFAIL);
531 va_start(args, message);
532 (void) sprintf(buf, "ztest: ");
534 (void) vsprintf(buf + strlen(buf), message, args);
537 (void) snprintf(buf + strlen(buf), FATAL_MSG_SZ - strlen(buf),
538 ": %s", strerror(save_errno));
540 (void) fprintf(stderr, "%s\n", buf);
541 fatal_msg = buf; /* to ease debugging */
548 str2shift(const char *buf)
550 const char *ends = "BKMGTPEZ";
555 for (i = 0; i < strlen(ends); i++) {
556 if (toupper(buf[0]) == ends[i])
559 if (i == strlen(ends)) {
560 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n",
564 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0')) {
567 (void) fprintf(stderr, "ztest: invalid bytes suffix: %s\n", buf);
573 nicenumtoull(const char *buf)
578 val = strtoull(buf, &end, 0);
580 (void) fprintf(stderr, "ztest: bad numeric value: %s\n", buf);
582 } else if (end[0] == '.') {
583 double fval = strtod(buf, &end);
584 fval *= pow(2, str2shift(end));
585 if (fval > UINT64_MAX) {
586 (void) fprintf(stderr, "ztest: value too large: %s\n",
590 val = (uint64_t)fval;
592 int shift = str2shift(end);
593 if (shift >= 64 || (val << shift) >> shift != val) {
594 (void) fprintf(stderr, "ztest: value too large: %s\n",
604 usage(boolean_t requested)
606 const ztest_shared_opts_t *zo = &ztest_opts_defaults;
608 char nice_vdev_size[10];
609 char nice_gang_bang[10];
610 FILE *fp = requested ? stdout : stderr;
612 nicenum(zo->zo_vdev_size, nice_vdev_size);
613 nicenum(zo->zo_metaslab_gang_bang, nice_gang_bang);
615 (void) fprintf(fp, "Usage: %s\n"
616 "\t[-v vdevs (default: %llu)]\n"
617 "\t[-s size_of_each_vdev (default: %s)]\n"
618 "\t[-a alignment_shift (default: %d)] use 0 for random\n"
619 "\t[-m mirror_copies (default: %d)]\n"
620 "\t[-r raidz_disks (default: %d)]\n"
621 "\t[-R raidz_parity (default: %d)]\n"
622 "\t[-d datasets (default: %d)]\n"
623 "\t[-t threads (default: %d)]\n"
624 "\t[-g gang_block_threshold (default: %s)]\n"
625 "\t[-i init_count (default: %d)] initialize pool i times\n"
626 "\t[-k kill_percentage (default: %llu%%)]\n"
627 "\t[-p pool_name (default: %s)]\n"
628 "\t[-f dir (default: %s)] file directory for vdev files\n"
629 "\t[-M] Multi-host simulate pool imported on remote host\n"
630 "\t[-V] verbose (use multiple times for ever more blather)\n"
631 "\t[-E] use existing pool instead of creating new one\n"
632 "\t[-T time (default: %llu sec)] total run time\n"
633 "\t[-F freezeloops (default: %llu)] max loops in spa_freeze()\n"
634 "\t[-P passtime (default: %llu sec)] time per pass\n"
635 "\t[-B alt_ztest (default: <none>)] alternate ztest path\n"
636 "\t[-o variable=value] ... set global variable to an unsigned\n"
637 "\t 32-bit integer value\n"
638 "\t[-h] (print help)\n"
641 (u_longlong_t)zo->zo_vdevs, /* -v */
642 nice_vdev_size, /* -s */
643 zo->zo_ashift, /* -a */
644 zo->zo_mirrors, /* -m */
645 zo->zo_raidz, /* -r */
646 zo->zo_raidz_parity, /* -R */
647 zo->zo_datasets, /* -d */
648 zo->zo_threads, /* -t */
649 nice_gang_bang, /* -g */
650 zo->zo_init, /* -i */
651 (u_longlong_t)zo->zo_killrate, /* -k */
652 zo->zo_pool, /* -p */
654 (u_longlong_t)zo->zo_time, /* -T */
655 (u_longlong_t)zo->zo_maxloops, /* -F */
656 (u_longlong_t)zo->zo_passtime);
657 exit(requested ? 0 : 1);
661 process_options(int argc, char **argv)
664 ztest_shared_opts_t *zo = &ztest_opts;
668 char altdir[MAXNAMELEN] = { 0 };
670 bcopy(&ztest_opts_defaults, zo, sizeof (*zo));
672 while ((opt = getopt(argc, argv,
673 "v:s:a:m:r:R:d:t:g:i:k:p:f:MVET:P:hF:B:o:")) != EOF) {
690 value = nicenumtoull(optarg);
694 zo->zo_vdevs = value;
697 zo->zo_vdev_size = MAX(SPA_MINDEVSIZE, value);
700 zo->zo_ashift = value;
703 zo->zo_mirrors = value;
706 zo->zo_raidz = MAX(1, value);
709 zo->zo_raidz_parity = MIN(MAX(value, 1), 3);
712 zo->zo_datasets = MAX(1, value);
715 zo->zo_threads = MAX(1, value);
718 zo->zo_metaslab_gang_bang = MAX(SPA_MINBLOCKSIZE << 1,
725 zo->zo_killrate = value;
728 (void) strlcpy(zo->zo_pool, optarg,
729 sizeof (zo->zo_pool));
732 path = realpath(optarg, NULL);
734 (void) fprintf(stderr, "error: %s: %s\n",
735 optarg, strerror(errno));
738 (void) strlcpy(zo->zo_dir, path,
739 sizeof (zo->zo_dir));
756 zo->zo_passtime = MAX(1, value);
759 zo->zo_maxloops = MAX(1, value);
762 (void) strlcpy(altdir, optarg, sizeof (altdir));
765 if (set_global_var(optarg) != 0)
778 zo->zo_raidz_parity = MIN(zo->zo_raidz_parity, zo->zo_raidz - 1);
781 (zo->zo_vdevs > 0 ? zo->zo_time * NANOSEC / zo->zo_vdevs :
784 if (strlen(altdir) > 0) {
792 cmd = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
793 realaltdir = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
795 VERIFY(NULL != realpath(getexecname(), cmd));
796 if (0 != access(altdir, F_OK)) {
797 ztest_dump_core = B_FALSE;
798 fatal(B_TRUE, "invalid alternate ztest path: %s",
801 VERIFY(NULL != realpath(altdir, realaltdir));
804 * 'cmd' should be of the form "<anything>/usr/bin/<isa>/ztest".
805 * We want to extract <isa> to determine if we should use
806 * 32 or 64 bit binaries.
808 bin = strstr(cmd, "/usr/bin/");
809 ztest = strstr(bin, "/ztest");
811 isalen = ztest - isa;
812 (void) snprintf(zo->zo_alt_ztest, sizeof (zo->zo_alt_ztest),
813 "%s/usr/bin/%.*s/ztest", realaltdir, isalen, isa);
814 (void) snprintf(zo->zo_alt_libpath, sizeof (zo->zo_alt_libpath),
815 "%s/usr/lib/%.*s", realaltdir, isalen, isa);
817 if (0 != access(zo->zo_alt_ztest, X_OK)) {
818 ztest_dump_core = B_FALSE;
819 fatal(B_TRUE, "invalid alternate ztest: %s",
821 } else if (0 != access(zo->zo_alt_libpath, X_OK)) {
822 ztest_dump_core = B_FALSE;
823 fatal(B_TRUE, "invalid alternate lib directory %s",
827 umem_free(cmd, MAXPATHLEN);
828 umem_free(realaltdir, MAXPATHLEN);
833 ztest_kill(ztest_shared_t *zs)
835 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(ztest_spa));
836 zs->zs_space = metaslab_class_get_space(spa_normal_class(ztest_spa));
839 * Before we kill off ztest, make sure that the config is updated.
840 * See comment above spa_config_sync().
842 mutex_enter(&spa_namespace_lock);
843 spa_config_sync(ztest_spa, B_FALSE, B_FALSE);
844 mutex_exit(&spa_namespace_lock);
846 (void) kill(getpid(), SIGKILL);
850 ztest_random(uint64_t range)
854 ASSERT3S(ztest_fd_rand, >=, 0);
859 if (read(ztest_fd_rand, &r, sizeof (r)) != sizeof (r))
860 fatal(1, "short read from /dev/urandom");
867 ztest_record_enospc(const char *s)
869 ztest_shared->zs_enospc_count++;
873 ztest_get_ashift(void)
875 if (ztest_opts.zo_ashift == 0)
876 return (SPA_MINBLOCKSHIFT + ztest_random(5));
877 return (ztest_opts.zo_ashift);
881 make_vdev_file(char *path, char *aux, char *pool, size_t size, uint64_t ashift)
887 pathbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
890 ashift = ztest_get_ashift();
896 vdev = ztest_shared->zs_vdev_aux;
897 (void) snprintf(path, MAXPATHLEN,
898 ztest_aux_template, ztest_opts.zo_dir,
899 pool == NULL ? ztest_opts.zo_pool : pool,
902 vdev = ztest_shared->zs_vdev_next_leaf++;
903 (void) snprintf(path, MAXPATHLEN,
904 ztest_dev_template, ztest_opts.zo_dir,
905 pool == NULL ? ztest_opts.zo_pool : pool, vdev);
910 int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
912 fatal(1, "can't open %s", path);
913 if (ftruncate(fd, size) != 0)
914 fatal(1, "can't ftruncate %s", path);
918 VERIFY(nvlist_alloc(&file, NV_UNIQUE_NAME, 0) == 0);
919 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE) == 0);
920 VERIFY(nvlist_add_string(file, ZPOOL_CONFIG_PATH, path) == 0);
921 VERIFY(nvlist_add_uint64(file, ZPOOL_CONFIG_ASHIFT, ashift) == 0);
922 umem_free(pathbuf, MAXPATHLEN);
928 make_vdev_raidz(char *path, char *aux, char *pool, size_t size,
929 uint64_t ashift, int r)
931 nvlist_t *raidz, **child;
935 return (make_vdev_file(path, aux, pool, size, ashift));
936 child = umem_alloc(r * sizeof (nvlist_t *), UMEM_NOFAIL);
938 for (c = 0; c < r; c++)
939 child[c] = make_vdev_file(path, aux, pool, size, ashift);
941 VERIFY(nvlist_alloc(&raidz, NV_UNIQUE_NAME, 0) == 0);
942 VERIFY(nvlist_add_string(raidz, ZPOOL_CONFIG_TYPE,
943 VDEV_TYPE_RAIDZ) == 0);
944 VERIFY(nvlist_add_uint64(raidz, ZPOOL_CONFIG_NPARITY,
945 ztest_opts.zo_raidz_parity) == 0);
946 VERIFY(nvlist_add_nvlist_array(raidz, ZPOOL_CONFIG_CHILDREN,
949 for (c = 0; c < r; c++)
950 nvlist_free(child[c]);
952 umem_free(child, r * sizeof (nvlist_t *));
958 make_vdev_mirror(char *path, char *aux, char *pool, size_t size,
959 uint64_t ashift, int r, int m)
961 nvlist_t *mirror, **child;
965 return (make_vdev_raidz(path, aux, pool, size, ashift, r));
967 child = umem_alloc(m * sizeof (nvlist_t *), UMEM_NOFAIL);
969 for (c = 0; c < m; c++)
970 child[c] = make_vdev_raidz(path, aux, pool, size, ashift, r);
972 VERIFY(nvlist_alloc(&mirror, NV_UNIQUE_NAME, 0) == 0);
973 VERIFY(nvlist_add_string(mirror, ZPOOL_CONFIG_TYPE,
974 VDEV_TYPE_MIRROR) == 0);
975 VERIFY(nvlist_add_nvlist_array(mirror, ZPOOL_CONFIG_CHILDREN,
978 for (c = 0; c < m; c++)
979 nvlist_free(child[c]);
981 umem_free(child, m * sizeof (nvlist_t *));
987 make_vdev_root(char *path, char *aux, char *pool, size_t size, uint64_t ashift,
988 int log, int r, int m, int t)
990 nvlist_t *root, **child;
995 child = umem_alloc(t * sizeof (nvlist_t *), UMEM_NOFAIL);
997 for (c = 0; c < t; c++) {
998 child[c] = make_vdev_mirror(path, aux, pool, size, ashift,
1000 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
1004 VERIFY(nvlist_alloc(&root, NV_UNIQUE_NAME, 0) == 0);
1005 VERIFY(nvlist_add_string(root, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) == 0);
1006 VERIFY(nvlist_add_nvlist_array(root, aux ? aux : ZPOOL_CONFIG_CHILDREN,
1009 for (c = 0; c < t; c++)
1010 nvlist_free(child[c]);
1012 umem_free(child, t * sizeof (nvlist_t *));
1018 * Find a random spa version. Returns back a random spa version in the
1019 * range [initial_version, SPA_VERSION_FEATURES].
1022 ztest_random_spa_version(uint64_t initial_version)
1024 uint64_t version = initial_version;
1026 if (version <= SPA_VERSION_BEFORE_FEATURES) {
1028 ztest_random(SPA_VERSION_BEFORE_FEATURES - version + 1);
1031 if (version > SPA_VERSION_BEFORE_FEATURES)
1032 version = SPA_VERSION_FEATURES;
1034 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
1039 ztest_random_blocksize(void)
1042 * Choose a block size >= the ashift.
1043 * If the SPA supports new MAXBLOCKSIZE, test up to 1MB blocks.
1045 int maxbs = SPA_OLD_MAXBLOCKSHIFT;
1046 if (spa_maxblocksize(ztest_spa) == SPA_MAXBLOCKSIZE)
1048 uint64_t block_shift =
1049 ztest_random(maxbs - ztest_spa->spa_max_ashift + 1);
1050 return (1 << (SPA_MINBLOCKSHIFT + block_shift));
1054 ztest_random_dnodesize(void)
1057 int max_slots = spa_maxdnodesize(ztest_spa) >> DNODE_SHIFT;
1059 if (max_slots == DNODE_MIN_SLOTS)
1060 return (DNODE_MIN_SIZE);
1063 * Weight the random distribution more heavily toward smaller
1064 * dnode sizes since that is more likely to reflect real-world
1067 ASSERT3U(max_slots, >, 4);
1068 switch (ztest_random(10)) {
1070 slots = 5 + ztest_random(max_slots - 4);
1073 slots = 2 + ztest_random(3);
1080 return (slots << DNODE_SHIFT);
1084 ztest_random_ibshift(void)
1086 return (DN_MIN_INDBLKSHIFT +
1087 ztest_random(DN_MAX_INDBLKSHIFT - DN_MIN_INDBLKSHIFT + 1));
1091 ztest_random_vdev_top(spa_t *spa, boolean_t log_ok)
1094 vdev_t *rvd = spa->spa_root_vdev;
1097 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1100 top = ztest_random(rvd->vdev_children);
1101 tvd = rvd->vdev_child[top];
1102 } while (tvd->vdev_ishole || (tvd->vdev_islog && !log_ok) ||
1103 tvd->vdev_mg == NULL || tvd->vdev_mg->mg_class == NULL);
1109 ztest_random_dsl_prop(zfs_prop_t prop)
1114 value = zfs_prop_random_value(prop, ztest_random(-1ULL));
1115 } while (prop == ZFS_PROP_CHECKSUM && value == ZIO_CHECKSUM_OFF);
1121 ztest_dsl_prop_set_uint64(char *osname, zfs_prop_t prop, uint64_t value,
1124 const char *propname = zfs_prop_to_name(prop);
1125 const char *valname;
1130 error = dsl_prop_set_int(osname, propname,
1131 (inherit ? ZPROP_SRC_NONE : ZPROP_SRC_LOCAL), value);
1133 if (error == ENOSPC) {
1134 ztest_record_enospc(FTAG);
1139 setpoint = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
1140 VERIFY0(dsl_prop_get_integer(osname, propname, &curval, setpoint));
1142 if (ztest_opts.zo_verbose >= 6) {
1145 err = zfs_prop_index_to_string(prop, curval, &valname);
1147 (void) printf("%s %s = %llu at '%s'\n", osname,
1148 propname, (unsigned long long)curval, setpoint);
1150 (void) printf("%s %s = %s at '%s'\n",
1151 osname, propname, valname, setpoint);
1153 umem_free(setpoint, MAXPATHLEN);
1159 ztest_spa_prop_set_uint64(zpool_prop_t prop, uint64_t value)
1161 spa_t *spa = ztest_spa;
1162 nvlist_t *props = NULL;
1165 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
1166 VERIFY(nvlist_add_uint64(props, zpool_prop_to_name(prop), value) == 0);
1168 error = spa_prop_set(spa, props);
1172 if (error == ENOSPC) {
1173 ztest_record_enospc(FTAG);
1183 * Object and range lock mechanics
1186 list_node_t z_lnode;
1187 refcount_t z_refcnt;
1189 zfs_rlock_t z_range_lock;
1194 ztest_znode_t *z_ztznode;
1197 static ztest_znode_t *
1198 ztest_znode_init(uint64_t object)
1200 ztest_znode_t *zp = umem_alloc(sizeof (*zp), UMEM_NOFAIL);
1202 list_link_init(&zp->z_lnode);
1203 refcount_create(&zp->z_refcnt);
1204 zp->z_object = object;
1205 zfs_rlock_init(&zp->z_range_lock);
1211 ztest_znode_fini(ztest_znode_t *zp)
1213 ASSERT(refcount_is_zero(&zp->z_refcnt));
1214 zfs_rlock_destroy(&zp->z_range_lock);
1216 refcount_destroy(&zp->z_refcnt);
1217 list_link_init(&zp->z_lnode);
1218 umem_free(zp, sizeof (*zp));
1222 ztest_zll_init(zll_t *zll)
1224 mutex_init(&zll->z_lock, NULL, MUTEX_DEFAULT, NULL);
1225 list_create(&zll->z_list, sizeof (ztest_znode_t),
1226 offsetof(ztest_znode_t, z_lnode));
1230 ztest_zll_destroy(zll_t *zll)
1232 list_destroy(&zll->z_list);
1233 mutex_destroy(&zll->z_lock);
1236 #define RL_TAG "range_lock"
1237 static ztest_znode_t *
1238 ztest_znode_get(ztest_ds_t *zd, uint64_t object)
1240 zll_t *zll = &zd->zd_range_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1241 ztest_znode_t *zp = NULL;
1242 mutex_enter(&zll->z_lock);
1243 for (zp = list_head(&zll->z_list); (zp);
1244 zp = list_next(&zll->z_list, zp)) {
1245 if (zp->z_object == object) {
1246 refcount_add(&zp->z_refcnt, RL_TAG);
1251 zp = ztest_znode_init(object);
1252 refcount_add(&zp->z_refcnt, RL_TAG);
1253 list_insert_head(&zll->z_list, zp);
1255 mutex_exit(&zll->z_lock);
1260 ztest_znode_put(ztest_ds_t *zd, ztest_znode_t *zp)
1263 ASSERT3U(zp->z_object, !=, 0);
1264 zll = &zd->zd_range_lock[zp->z_object & (ZTEST_OBJECT_LOCKS - 1)];
1265 mutex_enter(&zll->z_lock);
1266 refcount_remove(&zp->z_refcnt, RL_TAG);
1267 if (refcount_is_zero(&zp->z_refcnt)) {
1268 list_remove(&zll->z_list, zp);
1269 ztest_znode_fini(zp);
1271 mutex_exit(&zll->z_lock);
1276 ztest_rll_init(rll_t *rll)
1278 rll->rll_writer = NULL;
1279 rll->rll_readers = 0;
1280 mutex_init(&rll->rll_lock, NULL, MUTEX_DEFAULT, NULL);
1281 cv_init(&rll->rll_cv, NULL, CV_DEFAULT, NULL);
1285 ztest_rll_destroy(rll_t *rll)
1287 ASSERT(rll->rll_writer == NULL);
1288 ASSERT(rll->rll_readers == 0);
1289 mutex_destroy(&rll->rll_lock);
1290 cv_destroy(&rll->rll_cv);
1294 ztest_rll_lock(rll_t *rll, rl_type_t type)
1296 mutex_enter(&rll->rll_lock);
1298 if (type == RL_READER) {
1299 while (rll->rll_writer != NULL)
1300 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1303 while (rll->rll_writer != NULL || rll->rll_readers)
1304 (void) cv_wait(&rll->rll_cv, &rll->rll_lock);
1305 rll->rll_writer = curthread;
1308 mutex_exit(&rll->rll_lock);
1312 ztest_rll_unlock(rll_t *rll)
1314 mutex_enter(&rll->rll_lock);
1316 if (rll->rll_writer) {
1317 ASSERT(rll->rll_readers == 0);
1318 rll->rll_writer = NULL;
1320 ASSERT(rll->rll_readers != 0);
1321 ASSERT(rll->rll_writer == NULL);
1325 if (rll->rll_writer == NULL && rll->rll_readers == 0)
1326 cv_broadcast(&rll->rll_cv);
1328 mutex_exit(&rll->rll_lock);
1332 ztest_object_lock(ztest_ds_t *zd, uint64_t object, rl_type_t type)
1334 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1336 ztest_rll_lock(rll, type);
1340 ztest_object_unlock(ztest_ds_t *zd, uint64_t object)
1342 rll_t *rll = &zd->zd_object_lock[object & (ZTEST_OBJECT_LOCKS - 1)];
1344 ztest_rll_unlock(rll);
1347 static ztest_zrl_t *
1348 ztest_zrl_init(rl_t *rl, ztest_znode_t *zp)
1350 ztest_zrl_t *zrl = umem_alloc(sizeof (*zrl), UMEM_NOFAIL);
1352 zrl->z_ztznode = zp;
1357 ztest_zrl_fini(ztest_zrl_t *zrl)
1359 umem_free(zrl, sizeof (*zrl));
1362 static ztest_zrl_t *
1363 ztest_range_lock(ztest_ds_t *zd, uint64_t object, uint64_t offset,
1364 uint64_t size, rl_type_t type)
1366 ztest_znode_t *zp = ztest_znode_get(zd, object);
1367 rl_t *rl = zfs_range_lock(&zp->z_range_lock, offset,
1369 return (ztest_zrl_init(rl, zp));
1373 ztest_range_unlock(ztest_ds_t *zd, ztest_zrl_t *zrl)
1375 zfs_range_unlock(zrl->z_rl);
1376 ztest_znode_put(zd, zrl->z_ztznode);
1377 ztest_zrl_fini(zrl);
1381 ztest_zd_init(ztest_ds_t *zd, ztest_shared_ds_t *szd, objset_t *os)
1384 zd->zd_zilog = dmu_objset_zil(os);
1385 zd->zd_shared = szd;
1386 dmu_objset_name(os, zd->zd_name);
1389 if (zd->zd_shared != NULL)
1390 zd->zd_shared->zd_seq = 0;
1392 VERIFY(rwlock_init(&zd->zd_zilog_lock, USYNC_THREAD, NULL) == 0);
1393 mutex_init(&zd->zd_dirobj_lock, NULL, MUTEX_DEFAULT, NULL);
1395 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1396 ztest_rll_init(&zd->zd_object_lock[l]);
1398 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1399 ztest_zll_init(&zd->zd_range_lock[l]);
1403 ztest_zd_fini(ztest_ds_t *zd)
1407 mutex_destroy(&zd->zd_dirobj_lock);
1408 (void) rwlock_destroy(&zd->zd_zilog_lock);
1410 for (l = 0; l < ZTEST_OBJECT_LOCKS; l++)
1411 ztest_rll_destroy(&zd->zd_object_lock[l]);
1413 for (l = 0; l < ZTEST_RANGE_LOCKS; l++)
1414 ztest_zll_destroy(&zd->zd_range_lock[l]);
1417 #define TXG_MIGHTWAIT (ztest_random(10) == 0 ? TXG_NOWAIT : TXG_WAIT)
1420 ztest_tx_assign(dmu_tx_t *tx, uint64_t txg_how, const char *tag)
1426 * Attempt to assign tx to some transaction group.
1428 error = dmu_tx_assign(tx, txg_how);
1430 if (error == ERESTART) {
1431 ASSERT(txg_how == TXG_NOWAIT);
1434 ASSERT3U(error, ==, ENOSPC);
1435 ztest_record_enospc(tag);
1440 txg = dmu_tx_get_txg(tx);
1446 ztest_pattern_set(void *buf, uint64_t size, uint64_t value)
1449 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1457 ztest_pattern_match(void *buf, uint64_t size, uint64_t value)
1460 uint64_t *ip_end = (uint64_t *)((uintptr_t)buf + (uintptr_t)size);
1464 diff |= (value - *ip++);
1471 ztest_bt_generate(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1472 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1475 bt->bt_magic = BT_MAGIC;
1476 bt->bt_objset = dmu_objset_id(os);
1477 bt->bt_object = object;
1478 bt->bt_dnodesize = dnodesize;
1479 bt->bt_offset = offset;
1482 bt->bt_crtxg = crtxg;
1486 ztest_bt_verify(ztest_block_tag_t *bt, objset_t *os, uint64_t object,
1487 uint64_t dnodesize, uint64_t offset, uint64_t gen, uint64_t txg,
1490 ASSERT3U(bt->bt_magic, ==, BT_MAGIC);
1491 ASSERT3U(bt->bt_objset, ==, dmu_objset_id(os));
1492 ASSERT3U(bt->bt_object, ==, object);
1493 ASSERT3U(bt->bt_dnodesize, ==, dnodesize);
1494 ASSERT3U(bt->bt_offset, ==, offset);
1495 ASSERT3U(bt->bt_gen, <=, gen);
1496 ASSERT3U(bt->bt_txg, <=, txg);
1497 ASSERT3U(bt->bt_crtxg, ==, crtxg);
1500 static ztest_block_tag_t *
1501 ztest_bt_bonus(dmu_buf_t *db)
1503 dmu_object_info_t doi;
1504 ztest_block_tag_t *bt;
1506 dmu_object_info_from_db(db, &doi);
1507 ASSERT3U(doi.doi_bonus_size, <=, db->db_size);
1508 ASSERT3U(doi.doi_bonus_size, >=, sizeof (*bt));
1509 bt = (void *)((char *)db->db_data + doi.doi_bonus_size - sizeof (*bt));
1515 * Generate a token to fill up unused bonus buffer space. Try to make
1516 * it unique to the object, generation, and offset to verify that data
1517 * is not getting overwritten by data from other dnodes.
1519 #define ZTEST_BONUS_FILL_TOKEN(obj, ds, gen, offset) \
1520 (((ds) << 48) | ((gen) << 32) | ((obj) << 8) | (offset))
1523 * Fill up the unused bonus buffer region before the block tag with a
1524 * verifiable pattern. Filling the whole bonus area with non-zero data
1525 * helps ensure that all dnode traversal code properly skips the
1526 * interior regions of large dnodes.
1529 ztest_fill_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1530 objset_t *os, uint64_t gen)
1534 ASSERT(IS_P2ALIGNED((char *)end - (char *)db->db_data, 8));
1536 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1537 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1538 gen, bonusp - (uint64_t *)db->db_data);
1544 * Verify that the unused area of a bonus buffer is filled with the
1548 ztest_verify_unused_bonus(dmu_buf_t *db, void *end, uint64_t obj,
1549 objset_t *os, uint64_t gen)
1553 for (bonusp = db->db_data; bonusp < (uint64_t *)end; bonusp++) {
1554 uint64_t token = ZTEST_BONUS_FILL_TOKEN(obj, dmu_objset_id(os),
1555 gen, bonusp - (uint64_t *)db->db_data);
1556 VERIFY3U(*bonusp, ==, token);
1564 #define lrz_type lr_mode
1565 #define lrz_blocksize lr_uid
1566 #define lrz_ibshift lr_gid
1567 #define lrz_bonustype lr_rdev
1568 #define lrz_dnodesize lr_crtime[1]
1571 ztest_log_create(ztest_ds_t *zd, dmu_tx_t *tx, lr_create_t *lr)
1573 char *name = (void *)(lr + 1); /* name follows lr */
1574 size_t namesize = strlen(name) + 1;
1577 if (zil_replaying(zd->zd_zilog, tx))
1580 itx = zil_itx_create(TX_CREATE, sizeof (*lr) + namesize);
1581 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1582 sizeof (*lr) + namesize - sizeof (lr_t));
1584 zil_itx_assign(zd->zd_zilog, itx, tx);
1588 ztest_log_remove(ztest_ds_t *zd, dmu_tx_t *tx, lr_remove_t *lr, uint64_t object)
1590 char *name = (void *)(lr + 1); /* name follows lr */
1591 size_t namesize = strlen(name) + 1;
1594 if (zil_replaying(zd->zd_zilog, tx))
1597 itx = zil_itx_create(TX_REMOVE, sizeof (*lr) + namesize);
1598 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1599 sizeof (*lr) + namesize - sizeof (lr_t));
1601 itx->itx_oid = object;
1602 zil_itx_assign(zd->zd_zilog, itx, tx);
1606 ztest_log_write(ztest_ds_t *zd, dmu_tx_t *tx, lr_write_t *lr)
1609 itx_wr_state_t write_state = ztest_random(WR_NUM_STATES);
1611 if (zil_replaying(zd->zd_zilog, tx))
1614 if (lr->lr_length > ZIL_MAX_LOG_DATA)
1615 write_state = WR_INDIRECT;
1617 itx = zil_itx_create(TX_WRITE,
1618 sizeof (*lr) + (write_state == WR_COPIED ? lr->lr_length : 0));
1620 if (write_state == WR_COPIED &&
1621 dmu_read(zd->zd_os, lr->lr_foid, lr->lr_offset, lr->lr_length,
1622 ((lr_write_t *)&itx->itx_lr) + 1, DMU_READ_NO_PREFETCH) != 0) {
1623 zil_itx_destroy(itx);
1624 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1625 write_state = WR_NEED_COPY;
1627 itx->itx_private = zd;
1628 itx->itx_wr_state = write_state;
1629 itx->itx_sync = (ztest_random(8) == 0);
1631 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1632 sizeof (*lr) - sizeof (lr_t));
1634 zil_itx_assign(zd->zd_zilog, itx, tx);
1638 ztest_log_truncate(ztest_ds_t *zd, dmu_tx_t *tx, lr_truncate_t *lr)
1642 if (zil_replaying(zd->zd_zilog, tx))
1645 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1646 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1647 sizeof (*lr) - sizeof (lr_t));
1649 itx->itx_sync = B_FALSE;
1650 zil_itx_assign(zd->zd_zilog, itx, tx);
1654 ztest_log_setattr(ztest_ds_t *zd, dmu_tx_t *tx, lr_setattr_t *lr)
1658 if (zil_replaying(zd->zd_zilog, tx))
1661 itx = zil_itx_create(TX_SETATTR, sizeof (*lr));
1662 bcopy(&lr->lr_common + 1, &itx->itx_lr + 1,
1663 sizeof (*lr) - sizeof (lr_t));
1665 itx->itx_sync = B_FALSE;
1666 zil_itx_assign(zd->zd_zilog, itx, tx);
1673 ztest_replay_create(ztest_ds_t *zd, lr_create_t *lr, boolean_t byteswap)
1675 char *name = (void *)(lr + 1); /* name follows lr */
1676 objset_t *os = zd->zd_os;
1677 ztest_block_tag_t *bbt;
1685 byteswap_uint64_array(lr, sizeof (*lr));
1687 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1688 ASSERT(name[0] != '\0');
1690 tx = dmu_tx_create(os);
1692 dmu_tx_hold_zap(tx, lr->lr_doid, B_TRUE, name);
1694 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1695 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1697 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1700 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1704 ASSERT(dmu_objset_zil(os)->zl_replay == !!lr->lr_foid);
1705 bonuslen = DN_BONUS_SIZE(lr->lrz_dnodesize);
1707 if (lr->lrz_type == DMU_OT_ZAP_OTHER) {
1708 if (lr->lr_foid == 0) {
1709 lr->lr_foid = zap_create_dnsize(os,
1710 lr->lrz_type, lr->lrz_bonustype,
1711 bonuslen, lr->lrz_dnodesize, tx);
1713 error = zap_create_claim_dnsize(os, lr->lr_foid,
1714 lr->lrz_type, lr->lrz_bonustype,
1715 bonuslen, lr->lrz_dnodesize, tx);
1718 if (lr->lr_foid == 0) {
1719 lr->lr_foid = dmu_object_alloc_dnsize(os,
1720 lr->lrz_type, 0, lr->lrz_bonustype,
1721 bonuslen, lr->lrz_dnodesize, tx);
1723 error = dmu_object_claim_dnsize(os, lr->lr_foid,
1724 lr->lrz_type, 0, lr->lrz_bonustype,
1725 bonuslen, lr->lrz_dnodesize, tx);
1730 ASSERT3U(error, ==, EEXIST);
1731 ASSERT(zd->zd_zilog->zl_replay);
1736 ASSERT(lr->lr_foid != 0);
1738 if (lr->lrz_type != DMU_OT_ZAP_OTHER)
1739 VERIFY3U(0, ==, dmu_object_set_blocksize(os, lr->lr_foid,
1740 lr->lrz_blocksize, lr->lrz_ibshift, tx));
1742 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1743 bbt = ztest_bt_bonus(db);
1744 dmu_buf_will_dirty(db, tx);
1745 ztest_bt_generate(bbt, os, lr->lr_foid, lr->lrz_dnodesize, -1ULL,
1746 lr->lr_gen, txg, txg);
1747 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, lr->lr_gen);
1748 dmu_buf_rele(db, FTAG);
1750 VERIFY3U(0, ==, zap_add(os, lr->lr_doid, name, sizeof (uint64_t), 1,
1753 (void) ztest_log_create(zd, tx, lr);
1761 ztest_replay_remove(ztest_ds_t *zd, lr_remove_t *lr, boolean_t byteswap)
1763 char *name = (void *)(lr + 1); /* name follows lr */
1764 objset_t *os = zd->zd_os;
1765 dmu_object_info_t doi;
1767 uint64_t object, txg;
1770 byteswap_uint64_array(lr, sizeof (*lr));
1772 ASSERT(lr->lr_doid == ZTEST_DIROBJ);
1773 ASSERT(name[0] != '\0');
1776 zap_lookup(os, lr->lr_doid, name, sizeof (object), 1, &object));
1777 ASSERT(object != 0);
1779 ztest_object_lock(zd, object, RL_WRITER);
1781 VERIFY3U(0, ==, dmu_object_info(os, object, &doi));
1783 tx = dmu_tx_create(os);
1785 dmu_tx_hold_zap(tx, lr->lr_doid, B_FALSE, name);
1786 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1788 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1790 ztest_object_unlock(zd, object);
1794 if (doi.doi_type == DMU_OT_ZAP_OTHER) {
1795 VERIFY3U(0, ==, zap_destroy(os, object, tx));
1797 VERIFY3U(0, ==, dmu_object_free(os, object, tx));
1800 VERIFY3U(0, ==, zap_remove(os, lr->lr_doid, name, tx));
1802 (void) ztest_log_remove(zd, tx, lr, object);
1806 ztest_object_unlock(zd, object);
1812 ztest_replay_write(ztest_ds_t *zd, lr_write_t *lr, boolean_t byteswap)
1814 objset_t *os = zd->zd_os;
1815 void *data = lr + 1; /* data follows lr */
1816 uint64_t offset, length;
1817 ztest_block_tag_t *bt = data;
1818 ztest_block_tag_t *bbt;
1819 uint64_t gen, txg, lrtxg, crtxg;
1820 dmu_object_info_t doi;
1823 arc_buf_t *abuf = NULL;
1827 byteswap_uint64_array(lr, sizeof (*lr));
1829 offset = lr->lr_offset;
1830 length = lr->lr_length;
1832 /* If it's a dmu_sync() block, write the whole block */
1833 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
1834 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
1835 if (length < blocksize) {
1836 offset -= offset % blocksize;
1841 if (bt->bt_magic == BSWAP_64(BT_MAGIC))
1842 byteswap_uint64_array(bt, sizeof (*bt));
1844 if (bt->bt_magic != BT_MAGIC)
1847 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1848 rl = ztest_range_lock(zd, lr->lr_foid, offset, length, RL_WRITER);
1850 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1852 dmu_object_info_from_db(db, &doi);
1854 bbt = ztest_bt_bonus(db);
1855 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
1857 crtxg = bbt->bt_crtxg;
1858 lrtxg = lr->lr_common.lrc_txg;
1860 tx = dmu_tx_create(os);
1862 dmu_tx_hold_write(tx, lr->lr_foid, offset, length);
1864 if (ztest_random(8) == 0 && length == doi.doi_data_block_size &&
1865 P2PHASE(offset, length) == 0)
1866 abuf = dmu_request_arcbuf(db, length);
1868 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1871 dmu_return_arcbuf(abuf);
1872 dmu_buf_rele(db, FTAG);
1873 ztest_range_unlock(zd, rl);
1874 ztest_object_unlock(zd, lr->lr_foid);
1880 * Usually, verify the old data before writing new data --
1881 * but not always, because we also want to verify correct
1882 * behavior when the data was not recently read into cache.
1884 ASSERT(offset % doi.doi_data_block_size == 0);
1885 if (ztest_random(4) != 0) {
1886 int prefetch = ztest_random(2) ?
1887 DMU_READ_PREFETCH : DMU_READ_NO_PREFETCH;
1888 ztest_block_tag_t rbt;
1890 VERIFY(dmu_read(os, lr->lr_foid, offset,
1891 sizeof (rbt), &rbt, prefetch) == 0);
1892 if (rbt.bt_magic == BT_MAGIC) {
1893 ztest_bt_verify(&rbt, os, lr->lr_foid, 0,
1894 offset, gen, txg, crtxg);
1899 * Writes can appear to be newer than the bonus buffer because
1900 * the ztest_get_data() callback does a dmu_read() of the
1901 * open-context data, which may be different than the data
1902 * as it was when the write was generated.
1904 if (zd->zd_zilog->zl_replay) {
1905 ztest_bt_verify(bt, os, lr->lr_foid, 0, offset,
1906 MAX(gen, bt->bt_gen), MAX(txg, lrtxg),
1911 * Set the bt's gen/txg to the bonus buffer's gen/txg
1912 * so that all of the usual ASSERTs will work.
1914 ztest_bt_generate(bt, os, lr->lr_foid, 0, offset, gen, txg,
1919 dmu_write(os, lr->lr_foid, offset, length, data, tx);
1921 bcopy(data, abuf->b_data, length);
1922 dmu_assign_arcbuf(db, offset, abuf, tx);
1925 (void) ztest_log_write(zd, tx, lr);
1927 dmu_buf_rele(db, FTAG);
1931 ztest_range_unlock(zd, rl);
1932 ztest_object_unlock(zd, lr->lr_foid);
1938 ztest_replay_truncate(ztest_ds_t *zd, lr_truncate_t *lr, boolean_t byteswap)
1940 objset_t *os = zd->zd_os;
1946 byteswap_uint64_array(lr, sizeof (*lr));
1948 ztest_object_lock(zd, lr->lr_foid, RL_READER);
1949 rl = ztest_range_lock(zd, lr->lr_foid, lr->lr_offset, lr->lr_length,
1952 tx = dmu_tx_create(os);
1954 dmu_tx_hold_free(tx, lr->lr_foid, lr->lr_offset, lr->lr_length);
1956 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1958 ztest_range_unlock(zd, rl);
1959 ztest_object_unlock(zd, lr->lr_foid);
1963 VERIFY(dmu_free_range(os, lr->lr_foid, lr->lr_offset,
1964 lr->lr_length, tx) == 0);
1966 (void) ztest_log_truncate(zd, tx, lr);
1970 ztest_range_unlock(zd, rl);
1971 ztest_object_unlock(zd, lr->lr_foid);
1977 ztest_replay_setattr(ztest_ds_t *zd, lr_setattr_t *lr, boolean_t byteswap)
1979 objset_t *os = zd->zd_os;
1982 ztest_block_tag_t *bbt;
1983 uint64_t txg, lrtxg, crtxg, dnodesize;
1986 byteswap_uint64_array(lr, sizeof (*lr));
1988 ztest_object_lock(zd, lr->lr_foid, RL_WRITER);
1990 VERIFY3U(0, ==, dmu_bonus_hold(os, lr->lr_foid, FTAG, &db));
1992 tx = dmu_tx_create(os);
1993 dmu_tx_hold_bonus(tx, lr->lr_foid);
1995 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
1997 dmu_buf_rele(db, FTAG);
1998 ztest_object_unlock(zd, lr->lr_foid);
2002 bbt = ztest_bt_bonus(db);
2003 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2004 crtxg = bbt->bt_crtxg;
2005 lrtxg = lr->lr_common.lrc_txg;
2006 dnodesize = bbt->bt_dnodesize;
2008 if (zd->zd_zilog->zl_replay) {
2009 ASSERT(lr->lr_size != 0);
2010 ASSERT(lr->lr_mode != 0);
2014 * Randomly change the size and increment the generation.
2016 lr->lr_size = (ztest_random(db->db_size / sizeof (*bbt)) + 1) *
2018 lr->lr_mode = bbt->bt_gen + 1;
2023 * Verify that the current bonus buffer is not newer than our txg.
2025 ztest_bt_verify(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2026 MAX(txg, lrtxg), crtxg);
2028 dmu_buf_will_dirty(db, tx);
2030 ASSERT3U(lr->lr_size, >=, sizeof (*bbt));
2031 ASSERT3U(lr->lr_size, <=, db->db_size);
2032 VERIFY0(dmu_set_bonus(db, lr->lr_size, tx));
2033 bbt = ztest_bt_bonus(db);
2035 ztest_bt_generate(bbt, os, lr->lr_foid, dnodesize, -1ULL, lr->lr_mode,
2037 ztest_fill_unused_bonus(db, bbt, lr->lr_foid, os, bbt->bt_gen);
2038 dmu_buf_rele(db, FTAG);
2040 (void) ztest_log_setattr(zd, tx, lr);
2044 ztest_object_unlock(zd, lr->lr_foid);
2049 zil_replay_func_t ztest_replay_vector[TX_MAX_TYPE] = {
2050 NULL, /* 0 no such transaction type */
2051 (zil_replay_func_t)ztest_replay_create, /* TX_CREATE */
2052 NULL, /* TX_MKDIR */
2053 NULL, /* TX_MKXATTR */
2054 NULL, /* TX_SYMLINK */
2055 (zil_replay_func_t)ztest_replay_remove, /* TX_REMOVE */
2056 NULL, /* TX_RMDIR */
2058 NULL, /* TX_RENAME */
2059 (zil_replay_func_t)ztest_replay_write, /* TX_WRITE */
2060 (zil_replay_func_t)ztest_replay_truncate, /* TX_TRUNCATE */
2061 (zil_replay_func_t)ztest_replay_setattr, /* TX_SETATTR */
2063 NULL, /* TX_CREATE_ACL */
2064 NULL, /* TX_CREATE_ATTR */
2065 NULL, /* TX_CREATE_ACL_ATTR */
2066 NULL, /* TX_MKDIR_ACL */
2067 NULL, /* TX_MKDIR_ATTR */
2068 NULL, /* TX_MKDIR_ACL_ATTR */
2069 NULL, /* TX_WRITE2 */
2073 * ZIL get_data callbacks
2075 typedef struct ztest_zgd_private {
2079 } ztest_zgd_private_t;
2082 ztest_get_done(zgd_t *zgd, int error)
2084 ztest_zgd_private_t *zzp = zgd->zgd_private;
2085 ztest_ds_t *zd = zzp->z_zd;
2086 uint64_t object = zzp->z_object;
2089 dmu_buf_rele(zgd->zgd_db, zgd);
2091 ztest_range_unlock(zd, zzp->z_rl);
2092 ztest_object_unlock(zd, object);
2094 if (error == 0 && zgd->zgd_bp)
2095 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
2097 umem_free(zgd, sizeof (*zgd));
2098 umem_free(zzp, sizeof (*zzp));
2102 ztest_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
2104 ztest_ds_t *zd = arg;
2105 objset_t *os = zd->zd_os;
2106 uint64_t object = lr->lr_foid;
2107 uint64_t offset = lr->lr_offset;
2108 uint64_t size = lr->lr_length;
2109 uint64_t txg = lr->lr_common.lrc_txg;
2111 dmu_object_info_t doi;
2115 ztest_zgd_private_t *zgd_private;
2117 ztest_object_lock(zd, object, RL_READER);
2118 error = dmu_bonus_hold(os, object, FTAG, &db);
2120 ztest_object_unlock(zd, object);
2124 crtxg = ztest_bt_bonus(db)->bt_crtxg;
2126 if (crtxg == 0 || crtxg > txg) {
2127 dmu_buf_rele(db, FTAG);
2128 ztest_object_unlock(zd, object);
2132 dmu_object_info_from_db(db, &doi);
2133 dmu_buf_rele(db, FTAG);
2136 zgd = umem_zalloc(sizeof (*zgd), UMEM_NOFAIL);
2137 zgd->zgd_zilog = zd->zd_zilog;
2138 zgd_private = umem_zalloc(sizeof (ztest_zgd_private_t), UMEM_NOFAIL);
2139 zgd_private->z_zd = zd;
2140 zgd_private->z_object = object;
2141 zgd->zgd_private = zgd_private;
2143 if (buf != NULL) { /* immediate write */
2144 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2147 error = dmu_read(os, object, offset, size, buf,
2148 DMU_READ_NO_PREFETCH);
2151 size = doi.doi_data_block_size;
2153 offset = P2ALIGN(offset, size);
2155 ASSERT(offset < size);
2159 zgd_private->z_rl = ztest_range_lock(zd, object, offset, size,
2162 error = dmu_buf_hold(os, object, offset, zgd, &db,
2163 DMU_READ_NO_PREFETCH);
2166 blkptr_t *bp = &lr->lr_blkptr;
2171 ASSERT(db->db_offset == offset);
2172 ASSERT(db->db_size == size);
2174 error = dmu_sync(zio, lr->lr_common.lrc_txg,
2175 ztest_get_done, zgd);
2182 ztest_get_done(zgd, error);
2188 ztest_lr_alloc(size_t lrsize, char *name)
2191 size_t namesize = name ? strlen(name) + 1 : 0;
2193 lr = umem_zalloc(lrsize + namesize, UMEM_NOFAIL);
2196 bcopy(name, lr + lrsize, namesize);
2202 ztest_lr_free(void *lr, size_t lrsize, char *name)
2204 size_t namesize = name ? strlen(name) + 1 : 0;
2206 umem_free(lr, lrsize + namesize);
2210 * Lookup a bunch of objects. Returns the number of objects not found.
2213 ztest_lookup(ztest_ds_t *zd, ztest_od_t *od, int count)
2219 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2221 for (i = 0; i < count; i++, od++) {
2223 error = zap_lookup(zd->zd_os, od->od_dir, od->od_name,
2224 sizeof (uint64_t), 1, &od->od_object);
2226 ASSERT(error == ENOENT);
2227 ASSERT(od->od_object == 0);
2231 ztest_block_tag_t *bbt;
2232 dmu_object_info_t doi;
2234 ASSERT(od->od_object != 0);
2235 ASSERT(missing == 0); /* there should be no gaps */
2237 ztest_object_lock(zd, od->od_object, RL_READER);
2238 VERIFY3U(0, ==, dmu_bonus_hold(zd->zd_os,
2239 od->od_object, FTAG, &db));
2240 dmu_object_info_from_db(db, &doi);
2241 bbt = ztest_bt_bonus(db);
2242 ASSERT3U(bbt->bt_magic, ==, BT_MAGIC);
2243 od->od_type = doi.doi_type;
2244 od->od_blocksize = doi.doi_data_block_size;
2245 od->od_gen = bbt->bt_gen;
2246 dmu_buf_rele(db, FTAG);
2247 ztest_object_unlock(zd, od->od_object);
2255 ztest_create(ztest_ds_t *zd, ztest_od_t *od, int count)
2260 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2262 for (i = 0; i < count; i++, od++) {
2269 lr_create_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2271 lr->lr_doid = od->od_dir;
2272 lr->lr_foid = 0; /* 0 to allocate, > 0 to claim */
2273 lr->lrz_type = od->od_crtype;
2274 lr->lrz_blocksize = od->od_crblocksize;
2275 lr->lrz_ibshift = ztest_random_ibshift();
2276 lr->lrz_bonustype = DMU_OT_UINT64_OTHER;
2277 lr->lrz_dnodesize = od->od_crdnodesize;
2278 lr->lr_gen = od->od_crgen;
2279 lr->lr_crtime[0] = time(NULL);
2281 if (ztest_replay_create(zd, lr, B_FALSE) != 0) {
2282 ASSERT(missing == 0);
2286 od->od_object = lr->lr_foid;
2287 od->od_type = od->od_crtype;
2288 od->od_blocksize = od->od_crblocksize;
2289 od->od_gen = od->od_crgen;
2290 ASSERT(od->od_object != 0);
2293 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2300 ztest_remove(ztest_ds_t *zd, ztest_od_t *od, int count)
2306 ASSERT(mutex_held(&zd->zd_dirobj_lock));
2310 for (i = count - 1; i >= 0; i--, od--) {
2317 * No object was found.
2319 if (od->od_object == 0)
2322 lr_remove_t *lr = ztest_lr_alloc(sizeof (*lr), od->od_name);
2324 lr->lr_doid = od->od_dir;
2326 if ((error = ztest_replay_remove(zd, lr, B_FALSE)) != 0) {
2327 ASSERT3U(error, ==, ENOSPC);
2332 ztest_lr_free(lr, sizeof (*lr), od->od_name);
2339 ztest_write(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size,
2345 lr = ztest_lr_alloc(sizeof (*lr) + size, NULL);
2347 lr->lr_foid = object;
2348 lr->lr_offset = offset;
2349 lr->lr_length = size;
2351 BP_ZERO(&lr->lr_blkptr);
2353 bcopy(data, lr + 1, size);
2355 error = ztest_replay_write(zd, lr, B_FALSE);
2357 ztest_lr_free(lr, sizeof (*lr) + size, NULL);
2363 ztest_truncate(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2368 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2370 lr->lr_foid = object;
2371 lr->lr_offset = offset;
2372 lr->lr_length = size;
2374 error = ztest_replay_truncate(zd, lr, B_FALSE);
2376 ztest_lr_free(lr, sizeof (*lr), NULL);
2382 ztest_setattr(ztest_ds_t *zd, uint64_t object)
2387 lr = ztest_lr_alloc(sizeof (*lr), NULL);
2389 lr->lr_foid = object;
2393 error = ztest_replay_setattr(zd, lr, B_FALSE);
2395 ztest_lr_free(lr, sizeof (*lr), NULL);
2401 ztest_prealloc(ztest_ds_t *zd, uint64_t object, uint64_t offset, uint64_t size)
2403 objset_t *os = zd->zd_os;
2408 txg_wait_synced(dmu_objset_pool(os), 0);
2410 ztest_object_lock(zd, object, RL_READER);
2411 rl = ztest_range_lock(zd, object, offset, size, RL_WRITER);
2413 tx = dmu_tx_create(os);
2415 dmu_tx_hold_write(tx, object, offset, size);
2417 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
2420 dmu_prealloc(os, object, offset, size, tx);
2422 txg_wait_synced(dmu_objset_pool(os), txg);
2424 (void) dmu_free_long_range(os, object, offset, size);
2427 ztest_range_unlock(zd, rl);
2428 ztest_object_unlock(zd, object);
2432 ztest_io(ztest_ds_t *zd, uint64_t object, uint64_t offset)
2435 ztest_block_tag_t wbt;
2436 dmu_object_info_t doi;
2437 enum ztest_io_type io_type;
2441 VERIFY(dmu_object_info(zd->zd_os, object, &doi) == 0);
2442 blocksize = doi.doi_data_block_size;
2443 data = umem_alloc(blocksize, UMEM_NOFAIL);
2446 * Pick an i/o type at random, biased toward writing block tags.
2448 io_type = ztest_random(ZTEST_IO_TYPES);
2449 if (ztest_random(2) == 0)
2450 io_type = ZTEST_IO_WRITE_TAG;
2452 (void) rw_rdlock(&zd->zd_zilog_lock);
2456 case ZTEST_IO_WRITE_TAG:
2457 ztest_bt_generate(&wbt, zd->zd_os, object, doi.doi_dnodesize,
2459 (void) ztest_write(zd, object, offset, sizeof (wbt), &wbt);
2462 case ZTEST_IO_WRITE_PATTERN:
2463 (void) memset(data, 'a' + (object + offset) % 5, blocksize);
2464 if (ztest_random(2) == 0) {
2466 * Induce fletcher2 collisions to ensure that
2467 * zio_ddt_collision() detects and resolves them
2468 * when using fletcher2-verify for deduplication.
2470 ((uint64_t *)data)[0] ^= 1ULL << 63;
2471 ((uint64_t *)data)[4] ^= 1ULL << 63;
2473 (void) ztest_write(zd, object, offset, blocksize, data);
2476 case ZTEST_IO_WRITE_ZEROES:
2477 bzero(data, blocksize);
2478 (void) ztest_write(zd, object, offset, blocksize, data);
2481 case ZTEST_IO_TRUNCATE:
2482 (void) ztest_truncate(zd, object, offset, blocksize);
2485 case ZTEST_IO_SETATTR:
2486 (void) ztest_setattr(zd, object);
2491 case ZTEST_IO_REWRITE:
2492 (void) rw_rdlock(&ztest_name_lock);
2493 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2494 ZFS_PROP_CHECKSUM, spa_dedup_checksum(ztest_spa),
2496 VERIFY(err == 0 || err == ENOSPC);
2497 err = ztest_dsl_prop_set_uint64(zd->zd_name,
2498 ZFS_PROP_COMPRESSION,
2499 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION),
2501 VERIFY(err == 0 || err == ENOSPC);
2502 (void) rw_unlock(&ztest_name_lock);
2504 VERIFY0(dmu_read(zd->zd_os, object, offset, blocksize, data,
2505 DMU_READ_NO_PREFETCH));
2507 (void) ztest_write(zd, object, offset, blocksize, data);
2511 (void) rw_unlock(&zd->zd_zilog_lock);
2513 umem_free(data, blocksize);
2517 * Initialize an object description template.
2520 ztest_od_init(ztest_od_t *od, uint64_t id, char *tag, uint64_t index,
2521 dmu_object_type_t type, uint64_t blocksize, uint64_t dnodesize,
2524 od->od_dir = ZTEST_DIROBJ;
2527 od->od_crtype = type;
2528 od->od_crblocksize = blocksize ? blocksize : ztest_random_blocksize();
2529 od->od_crdnodesize = dnodesize ? dnodesize : ztest_random_dnodesize();
2532 od->od_type = DMU_OT_NONE;
2533 od->od_blocksize = 0;
2536 (void) snprintf(od->od_name, sizeof (od->od_name), "%s(%lld)[%llu]",
2537 tag, (longlong_t)id, (u_longlong_t)index);
2541 * Lookup or create the objects for a test using the od template.
2542 * If the objects do not all exist, or if 'remove' is specified,
2543 * remove any existing objects and create new ones. Otherwise,
2544 * use the existing objects.
2547 ztest_object_init(ztest_ds_t *zd, ztest_od_t *od, size_t size, boolean_t remove)
2549 int count = size / sizeof (*od);
2552 mutex_enter(&zd->zd_dirobj_lock);
2553 if ((ztest_lookup(zd, od, count) != 0 || remove) &&
2554 (ztest_remove(zd, od, count) != 0 ||
2555 ztest_create(zd, od, count) != 0))
2558 mutex_exit(&zd->zd_dirobj_lock);
2565 ztest_zil_commit(ztest_ds_t *zd, uint64_t id)
2567 zilog_t *zilog = zd->zd_zilog;
2569 (void) rw_rdlock(&zd->zd_zilog_lock);
2571 zil_commit(zilog, ztest_random(ZTEST_OBJECTS));
2574 * Remember the committed values in zd, which is in parent/child
2575 * shared memory. If we die, the next iteration of ztest_run()
2576 * will verify that the log really does contain this record.
2578 mutex_enter(&zilog->zl_lock);
2579 ASSERT(zd->zd_shared != NULL);
2580 ASSERT3U(zd->zd_shared->zd_seq, <=, zilog->zl_commit_lr_seq);
2581 zd->zd_shared->zd_seq = zilog->zl_commit_lr_seq;
2582 mutex_exit(&zilog->zl_lock);
2584 (void) rw_unlock(&zd->zd_zilog_lock);
2588 * This function is designed to simulate the operations that occur during a
2589 * mount/unmount operation. We hold the dataset across these operations in an
2590 * attempt to expose any implicit assumptions about ZIL management.
2594 ztest_zil_remount(ztest_ds_t *zd, uint64_t id)
2596 objset_t *os = zd->zd_os;
2599 * We grab the zd_dirobj_lock to ensure that no other thread is
2600 * updating the zil (i.e. adding in-memory log records) and the
2601 * zd_zilog_lock to block any I/O.
2603 mutex_enter(&zd->zd_dirobj_lock);
2604 (void) rw_wrlock(&zd->zd_zilog_lock);
2606 /* zfsvfs_teardown() */
2607 zil_close(zd->zd_zilog);
2609 /* zfsvfs_setup() */
2610 VERIFY(zil_open(os, ztest_get_data) == zd->zd_zilog);
2611 zil_replay(os, zd, ztest_replay_vector);
2613 (void) rw_unlock(&zd->zd_zilog_lock);
2614 mutex_exit(&zd->zd_dirobj_lock);
2618 * Verify that we can't destroy an active pool, create an existing pool,
2619 * or create a pool with a bad vdev spec.
2623 ztest_spa_create_destroy(ztest_ds_t *zd, uint64_t id)
2625 ztest_shared_opts_t *zo = &ztest_opts;
2629 if (zo->zo_mmp_test)
2633 * Attempt to create using a bad file.
2635 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2636 VERIFY3U(ENOENT, ==,
2637 spa_create("ztest_bad_file", nvroot, NULL, NULL));
2638 nvlist_free(nvroot);
2641 * Attempt to create using a bad mirror.
2643 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 2, 1);
2644 VERIFY3U(ENOENT, ==,
2645 spa_create("ztest_bad_mirror", nvroot, NULL, NULL));
2646 nvlist_free(nvroot);
2649 * Attempt to create an existing pool. It shouldn't matter
2650 * what's in the nvroot; we should fail with EEXIST.
2652 (void) rw_rdlock(&ztest_name_lock);
2653 nvroot = make_vdev_root("/dev/bogus", NULL, NULL, 0, 0, 0, 0, 0, 1);
2654 VERIFY3U(EEXIST, ==, spa_create(zo->zo_pool, nvroot, NULL, NULL));
2655 nvlist_free(nvroot);
2656 VERIFY3U(0, ==, spa_open(zo->zo_pool, &spa, FTAG));
2657 VERIFY3U(EBUSY, ==, spa_destroy(zo->zo_pool));
2658 spa_close(spa, FTAG);
2660 (void) rw_unlock(&ztest_name_lock);
2665 ztest_spa_upgrade(ztest_ds_t *zd, uint64_t id)
2668 uint64_t initial_version = SPA_VERSION_INITIAL;
2669 uint64_t version, newversion;
2670 nvlist_t *nvroot, *props;
2673 if (ztest_opts.zo_mmp_test)
2676 mutex_enter(&ztest_vdev_lock);
2677 name = kmem_asprintf("%s_upgrade", ztest_opts.zo_pool);
2680 * Clean up from previous runs.
2682 (void) spa_destroy(name);
2684 nvroot = make_vdev_root(NULL, NULL, name, ztest_opts.zo_vdev_size, 0,
2685 0, ztest_opts.zo_raidz, ztest_opts.zo_mirrors, 1);
2688 * If we're configuring a RAIDZ device then make sure that the
2689 * the initial version is capable of supporting that feature.
2691 switch (ztest_opts.zo_raidz_parity) {
2694 initial_version = SPA_VERSION_INITIAL;
2697 initial_version = SPA_VERSION_RAIDZ2;
2700 initial_version = SPA_VERSION_RAIDZ3;
2705 * Create a pool with a spa version that can be upgraded. Pick
2706 * a value between initial_version and SPA_VERSION_BEFORE_FEATURES.
2709 version = ztest_random_spa_version(initial_version);
2710 } while (version > SPA_VERSION_BEFORE_FEATURES);
2712 props = fnvlist_alloc();
2713 fnvlist_add_uint64(props,
2714 zpool_prop_to_name(ZPOOL_PROP_VERSION), version);
2715 VERIFY3S(spa_create(name, nvroot, props, NULL), ==, 0);
2716 fnvlist_free(nvroot);
2717 fnvlist_free(props);
2719 VERIFY3S(spa_open(name, &spa, FTAG), ==, 0);
2720 VERIFY3U(spa_version(spa), ==, version);
2721 newversion = ztest_random_spa_version(version + 1);
2723 if (ztest_opts.zo_verbose >= 4) {
2724 (void) printf("upgrading spa version from %llu to %llu\n",
2725 (u_longlong_t)version, (u_longlong_t)newversion);
2728 spa_upgrade(spa, newversion);
2729 VERIFY3U(spa_version(spa), >, version);
2730 VERIFY3U(spa_version(spa), ==, fnvlist_lookup_uint64(spa->spa_config,
2731 zpool_prop_to_name(ZPOOL_PROP_VERSION)));
2732 spa_close(spa, FTAG);
2735 mutex_exit(&ztest_vdev_lock);
2739 vdev_lookup_by_path(vdev_t *vd, const char *path)
2744 if (vd->vdev_path != NULL && strcmp(path, vd->vdev_path) == 0)
2747 for (c = 0; c < vd->vdev_children; c++)
2748 if ((mvd = vdev_lookup_by_path(vd->vdev_child[c], path)) !=
2756 * Find the first available hole which can be used as a top-level.
2759 find_vdev_hole(spa_t *spa)
2761 vdev_t *rvd = spa->spa_root_vdev;
2764 ASSERT(spa_config_held(spa, SCL_VDEV, RW_READER) == SCL_VDEV);
2766 for (c = 0; c < rvd->vdev_children; c++) {
2767 vdev_t *cvd = rvd->vdev_child[c];
2769 if (cvd->vdev_ishole)
2776 * Verify that vdev_add() works as expected.
2780 ztest_vdev_add_remove(ztest_ds_t *zd, uint64_t id)
2782 ztest_shared_t *zs = ztest_shared;
2783 spa_t *spa = ztest_spa;
2789 if (ztest_opts.zo_mmp_test)
2792 mutex_enter(&ztest_vdev_lock);
2793 leaves = MAX(zs->zs_mirrors + zs->zs_splits, 1) * ztest_opts.zo_raidz;
2795 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2797 ztest_shared->zs_vdev_next_leaf = find_vdev_hole(spa) * leaves;
2800 * If we have slogs then remove them 1/4 of the time.
2802 if (spa_has_slogs(spa) && ztest_random(4) == 0) {
2804 * Grab the guid from the head of the log class rotor.
2806 guid = spa_log_class(spa)->mc_rotor->mg_vd->vdev_guid;
2808 spa_config_exit(spa, SCL_VDEV, FTAG);
2811 * We have to grab the zs_name_lock as writer to
2812 * prevent a race between removing a slog (dmu_objset_find)
2813 * and destroying a dataset. Removing the slog will
2814 * grab a reference on the dataset which may cause
2815 * dsl_destroy_head() to fail with EBUSY thus
2816 * leaving the dataset in an inconsistent state.
2818 rw_wrlock(&ztest_name_lock);
2819 error = spa_vdev_remove(spa, guid, B_FALSE);
2820 rw_unlock(&ztest_name_lock);
2822 if (error && error != EEXIST)
2823 fatal(0, "spa_vdev_remove() = %d", error);
2825 spa_config_exit(spa, SCL_VDEV, FTAG);
2828 * Make 1/4 of the devices be log devices.
2830 nvroot = make_vdev_root(NULL, NULL, NULL,
2831 ztest_opts.zo_vdev_size, 0,
2832 ztest_random(4) == 0, ztest_opts.zo_raidz,
2835 error = spa_vdev_add(spa, nvroot);
2836 nvlist_free(nvroot);
2838 if (error == ENOSPC)
2839 ztest_record_enospc("spa_vdev_add");
2840 else if (error != 0)
2841 fatal(0, "spa_vdev_add() = %d", error);
2844 mutex_exit(&ztest_vdev_lock);
2848 * Verify that adding/removing aux devices (l2arc, hot spare) works as expected.
2852 ztest_vdev_aux_add_remove(ztest_ds_t *zd, uint64_t id)
2854 ztest_shared_t *zs = ztest_shared;
2855 spa_t *spa = ztest_spa;
2856 vdev_t *rvd = spa->spa_root_vdev;
2857 spa_aux_vdev_t *sav;
2863 if (ztest_opts.zo_mmp_test)
2866 path = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
2868 if (ztest_random(2) == 0) {
2869 sav = &spa->spa_spares;
2870 aux = ZPOOL_CONFIG_SPARES;
2872 sav = &spa->spa_l2cache;
2873 aux = ZPOOL_CONFIG_L2CACHE;
2876 mutex_enter(&ztest_vdev_lock);
2878 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2880 if (sav->sav_count != 0 && ztest_random(4) == 0) {
2882 * Pick a random device to remove.
2884 guid = sav->sav_vdevs[ztest_random(sav->sav_count)]->vdev_guid;
2887 * Find an unused device we can add.
2889 zs->zs_vdev_aux = 0;
2892 (void) snprintf(path, MAXPATHLEN, ztest_aux_template,
2893 ztest_opts.zo_dir, ztest_opts.zo_pool, aux,
2895 for (c = 0; c < sav->sav_count; c++)
2896 if (strcmp(sav->sav_vdevs[c]->vdev_path,
2899 if (c == sav->sav_count &&
2900 vdev_lookup_by_path(rvd, path) == NULL)
2906 spa_config_exit(spa, SCL_VDEV, FTAG);
2912 nvlist_t *nvroot = make_vdev_root(NULL, aux, NULL,
2913 (ztest_opts.zo_vdev_size * 5) / 4, 0, 0, 0, 0, 1);
2914 error = spa_vdev_add(spa, nvroot);
2916 fatal(0, "spa_vdev_add(%p) = %d", nvroot, error);
2917 nvlist_free(nvroot);
2920 * Remove an existing device. Sometimes, dirty its
2921 * vdev state first to make sure we handle removal
2922 * of devices that have pending state changes.
2924 if (ztest_random(2) == 0)
2925 (void) vdev_online(spa, guid, 0, NULL);
2927 error = spa_vdev_remove(spa, guid, B_FALSE);
2928 if (error != 0 && error != EBUSY)
2929 fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
2932 mutex_exit(&ztest_vdev_lock);
2934 umem_free(path, MAXPATHLEN);
2938 * split a pool if it has mirror tlvdevs
2942 ztest_split_pool(ztest_ds_t *zd, uint64_t id)
2944 ztest_shared_t *zs = ztest_shared;
2945 spa_t *spa = ztest_spa;
2946 vdev_t *rvd = spa->spa_root_vdev;
2947 nvlist_t *tree, **child, *config, *split, **schild;
2948 uint_t c, children, schildren = 0, lastlogid = 0;
2951 if (ztest_opts.zo_mmp_test)
2954 mutex_enter(&ztest_vdev_lock);
2956 /* ensure we have a useable config; mirrors of raidz aren't supported */
2957 if (zs->zs_mirrors < 3 || ztest_opts.zo_raidz > 1) {
2958 mutex_exit(&ztest_vdev_lock);
2962 /* clean up the old pool, if any */
2963 (void) spa_destroy("splitp");
2965 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
2967 /* generate a config from the existing config */
2968 mutex_enter(&spa->spa_props_lock);
2969 VERIFY(nvlist_lookup_nvlist(spa->spa_config, ZPOOL_CONFIG_VDEV_TREE,
2971 mutex_exit(&spa->spa_props_lock);
2973 VERIFY(nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2976 schild = malloc(rvd->vdev_children * sizeof (nvlist_t *));
2977 for (c = 0; c < children; c++) {
2978 vdev_t *tvd = rvd->vdev_child[c];
2982 if (tvd->vdev_islog || tvd->vdev_ops == &vdev_hole_ops) {
2983 VERIFY(nvlist_alloc(&schild[schildren], NV_UNIQUE_NAME,
2985 VERIFY(nvlist_add_string(schild[schildren],
2986 ZPOOL_CONFIG_TYPE, VDEV_TYPE_HOLE) == 0);
2987 VERIFY(nvlist_add_uint64(schild[schildren],
2988 ZPOOL_CONFIG_IS_HOLE, 1) == 0);
2990 lastlogid = schildren;
2995 VERIFY(nvlist_lookup_nvlist_array(child[c],
2996 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2997 VERIFY(nvlist_dup(mchild[0], &schild[schildren++], 0) == 0);
3000 /* OK, create a config that can be used to split */
3001 VERIFY(nvlist_alloc(&split, NV_UNIQUE_NAME, 0) == 0);
3002 VERIFY(nvlist_add_string(split, ZPOOL_CONFIG_TYPE,
3003 VDEV_TYPE_ROOT) == 0);
3004 VERIFY(nvlist_add_nvlist_array(split, ZPOOL_CONFIG_CHILDREN, schild,
3005 lastlogid != 0 ? lastlogid : schildren) == 0);
3007 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
3008 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, split) == 0);
3010 for (c = 0; c < schildren; c++)
3011 nvlist_free(schild[c]);
3015 spa_config_exit(spa, SCL_VDEV, FTAG);
3017 (void) rw_wrlock(&ztest_name_lock);
3018 error = spa_vdev_split_mirror(spa, "splitp", config, NULL, B_FALSE);
3019 (void) rw_unlock(&ztest_name_lock);
3021 nvlist_free(config);
3024 (void) printf("successful split - results:\n");
3025 mutex_enter(&spa_namespace_lock);
3026 show_pool_stats(spa);
3027 show_pool_stats(spa_lookup("splitp"));
3028 mutex_exit(&spa_namespace_lock);
3032 mutex_exit(&ztest_vdev_lock);
3037 * Verify that we can attach and detach devices.
3041 ztest_vdev_attach_detach(ztest_ds_t *zd, uint64_t id)
3043 ztest_shared_t *zs = ztest_shared;
3044 spa_t *spa = ztest_spa;
3045 spa_aux_vdev_t *sav = &spa->spa_spares;
3046 vdev_t *rvd = spa->spa_root_vdev;
3047 vdev_t *oldvd, *newvd, *pvd;
3051 uint64_t ashift = ztest_get_ashift();
3052 uint64_t oldguid, pguid;
3053 uint64_t oldsize, newsize;
3054 char *oldpath, *newpath;
3056 int oldvd_has_siblings = B_FALSE;
3057 int newvd_is_spare = B_FALSE;
3059 int error, expected_error;
3061 if (ztest_opts.zo_mmp_test)
3064 oldpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3065 newpath = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
3067 mutex_enter(&ztest_vdev_lock);
3068 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
3070 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
3073 * Decide whether to do an attach or a replace.
3075 replacing = ztest_random(2);
3078 * Pick a random top-level vdev.
3080 top = ztest_random_vdev_top(spa, B_TRUE);
3083 * Pick a random leaf within it.
3085 leaf = ztest_random(leaves);
3090 oldvd = rvd->vdev_child[top];
3091 if (zs->zs_mirrors >= 1) {
3092 ASSERT(oldvd->vdev_ops == &vdev_mirror_ops);
3093 ASSERT(oldvd->vdev_children >= zs->zs_mirrors);
3094 oldvd = oldvd->vdev_child[leaf / ztest_opts.zo_raidz];
3096 if (ztest_opts.zo_raidz > 1) {
3097 ASSERT(oldvd->vdev_ops == &vdev_raidz_ops);
3098 ASSERT(oldvd->vdev_children == ztest_opts.zo_raidz);
3099 oldvd = oldvd->vdev_child[leaf % ztest_opts.zo_raidz];
3103 * If we're already doing an attach or replace, oldvd may be a
3104 * mirror vdev -- in which case, pick a random child.
3106 while (oldvd->vdev_children != 0) {
3107 oldvd_has_siblings = B_TRUE;
3108 ASSERT(oldvd->vdev_children >= 2);
3109 oldvd = oldvd->vdev_child[ztest_random(oldvd->vdev_children)];
3112 oldguid = oldvd->vdev_guid;
3113 oldsize = vdev_get_min_asize(oldvd);
3114 oldvd_is_log = oldvd->vdev_top->vdev_islog;
3115 (void) strcpy(oldpath, oldvd->vdev_path);
3116 pvd = oldvd->vdev_parent;
3117 pguid = pvd->vdev_guid;
3120 * If oldvd has siblings, then half of the time, detach it.
3122 if (oldvd_has_siblings && ztest_random(2) == 0) {
3123 spa_config_exit(spa, SCL_VDEV, FTAG);
3124 error = spa_vdev_detach(spa, oldguid, pguid, B_FALSE);
3125 if (error != 0 && error != ENODEV && error != EBUSY &&
3127 fatal(0, "detach (%s) returned %d", oldpath, error);
3132 * For the new vdev, choose with equal probability between the two
3133 * standard paths (ending in either 'a' or 'b') or a random hot spare.
3135 if (sav->sav_count != 0 && ztest_random(3) == 0) {
3136 newvd = sav->sav_vdevs[ztest_random(sav->sav_count)];
3137 newvd_is_spare = B_TRUE;
3138 (void) strcpy(newpath, newvd->vdev_path);
3140 (void) snprintf(newpath, MAXPATHLEN, ztest_dev_template,
3141 ztest_opts.zo_dir, ztest_opts.zo_pool,
3142 top * leaves + leaf);
3143 if (ztest_random(2) == 0)
3144 newpath[strlen(newpath) - 1] = 'b';
3145 newvd = vdev_lookup_by_path(rvd, newpath);
3149 newsize = vdev_get_min_asize(newvd);
3152 * Make newsize a little bigger or smaller than oldsize.
3153 * If it's smaller, the attach should fail.
3154 * If it's larger, and we're doing a replace,
3155 * we should get dynamic LUN growth when we're done.
3157 newsize = 10 * oldsize / (9 + ztest_random(3));
3161 * If pvd is not a mirror or root, the attach should fail with ENOTSUP,
3162 * unless it's a replace; in that case any non-replacing parent is OK.
3164 * If newvd is already part of the pool, it should fail with EBUSY.
3166 * If newvd is too small, it should fail with EOVERFLOW.
3168 if (pvd->vdev_ops != &vdev_mirror_ops &&
3169 pvd->vdev_ops != &vdev_root_ops && (!replacing ||
3170 pvd->vdev_ops == &vdev_replacing_ops ||
3171 pvd->vdev_ops == &vdev_spare_ops))
3172 expected_error = ENOTSUP;
3173 else if (newvd_is_spare && (!replacing || oldvd_is_log))
3174 expected_error = ENOTSUP;
3175 else if (newvd == oldvd)
3176 expected_error = replacing ? 0 : EBUSY;
3177 else if (vdev_lookup_by_path(rvd, newpath) != NULL)
3178 expected_error = EBUSY;
3179 else if (newsize < oldsize)
3180 expected_error = EOVERFLOW;
3181 else if (ashift > oldvd->vdev_top->vdev_ashift)
3182 expected_error = EDOM;
3186 spa_config_exit(spa, SCL_VDEV, FTAG);
3189 * Build the nvlist describing newpath.
3191 root = make_vdev_root(newpath, NULL, NULL, newvd == NULL ? newsize : 0,
3192 ashift, 0, 0, 0, 1);
3194 error = spa_vdev_attach(spa, oldguid, root, replacing);
3199 * If our parent was the replacing vdev, but the replace completed,
3200 * then instead of failing with ENOTSUP we may either succeed,
3201 * fail with ENODEV, or fail with EOVERFLOW.
3203 if (expected_error == ENOTSUP &&
3204 (error == 0 || error == ENODEV || error == EOVERFLOW))
3205 expected_error = error;
3208 * If someone grew the LUN, the replacement may be too small.
3210 if (error == EOVERFLOW || error == EBUSY)
3211 expected_error = error;
3213 /* XXX workaround 6690467 */
3214 if (error != expected_error && expected_error != EBUSY) {
3215 fatal(0, "attach (%s %llu, %s %llu, %d) "
3216 "returned %d, expected %d",
3217 oldpath, oldsize, newpath,
3218 newsize, replacing, error, expected_error);
3221 mutex_exit(&ztest_vdev_lock);
3223 umem_free(oldpath, MAXPATHLEN);
3224 umem_free(newpath, MAXPATHLEN);
3228 * Callback function which expands the physical size of the vdev.
3231 grow_vdev(vdev_t *vd, void *arg)
3233 ASSERTV(spa_t *spa = vd->vdev_spa);
3234 size_t *newsize = arg;
3238 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3239 ASSERT(vd->vdev_ops->vdev_op_leaf);
3241 if ((fd = open(vd->vdev_path, O_RDWR)) == -1)
3244 fsize = lseek(fd, 0, SEEK_END);
3245 VERIFY(ftruncate(fd, *newsize) == 0);
3247 if (ztest_opts.zo_verbose >= 6) {
3248 (void) printf("%s grew from %lu to %lu bytes\n",
3249 vd->vdev_path, (ulong_t)fsize, (ulong_t)*newsize);
3256 * Callback function which expands a given vdev by calling vdev_online().
3260 online_vdev(vdev_t *vd, void *arg)
3262 spa_t *spa = vd->vdev_spa;
3263 vdev_t *tvd = vd->vdev_top;
3264 uint64_t guid = vd->vdev_guid;
3265 uint64_t generation = spa->spa_config_generation + 1;
3266 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
3269 ASSERT(spa_config_held(spa, SCL_STATE, RW_READER) == SCL_STATE);
3270 ASSERT(vd->vdev_ops->vdev_op_leaf);
3272 /* Calling vdev_online will initialize the new metaslabs */
3273 spa_config_exit(spa, SCL_STATE, spa);
3274 error = vdev_online(spa, guid, ZFS_ONLINE_EXPAND, &newstate);
3275 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3278 * If vdev_online returned an error or the underlying vdev_open
3279 * failed then we abort the expand. The only way to know that
3280 * vdev_open fails is by checking the returned newstate.
3282 if (error || newstate != VDEV_STATE_HEALTHY) {
3283 if (ztest_opts.zo_verbose >= 5) {
3284 (void) printf("Unable to expand vdev, state %llu, "
3285 "error %d\n", (u_longlong_t)newstate, error);
3289 ASSERT3U(newstate, ==, VDEV_STATE_HEALTHY);
3292 * Since we dropped the lock we need to ensure that we're
3293 * still talking to the original vdev. It's possible this
3294 * vdev may have been detached/replaced while we were
3295 * trying to online it.
3297 if (generation != spa->spa_config_generation) {
3298 if (ztest_opts.zo_verbose >= 5) {
3299 (void) printf("vdev configuration has changed, "
3300 "guid %llu, state %llu, expected gen %llu, "
3303 (u_longlong_t)tvd->vdev_state,
3304 (u_longlong_t)generation,
3305 (u_longlong_t)spa->spa_config_generation);
3313 * Traverse the vdev tree calling the supplied function.
3314 * We continue to walk the tree until we either have walked all
3315 * children or we receive a non-NULL return from the callback.
3316 * If a NULL callback is passed, then we just return back the first
3317 * leaf vdev we encounter.
3320 vdev_walk_tree(vdev_t *vd, vdev_t *(*func)(vdev_t *, void *), void *arg)
3324 if (vd->vdev_ops->vdev_op_leaf) {
3328 return (func(vd, arg));
3331 for (c = 0; c < vd->vdev_children; c++) {
3332 vdev_t *cvd = vd->vdev_child[c];
3333 if ((cvd = vdev_walk_tree(cvd, func, arg)) != NULL)
3340 * Verify that dynamic LUN growth works as expected.
3344 ztest_vdev_LUN_growth(ztest_ds_t *zd, uint64_t id)
3346 spa_t *spa = ztest_spa;
3348 metaslab_class_t *mc;
3349 metaslab_group_t *mg;
3350 size_t psize, newsize;
3352 uint64_t old_class_space, new_class_space, old_ms_count, new_ms_count;
3354 mutex_enter(&ztest_vdev_lock);
3355 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3357 top = ztest_random_vdev_top(spa, B_TRUE);
3359 tvd = spa->spa_root_vdev->vdev_child[top];
3362 old_ms_count = tvd->vdev_ms_count;
3363 old_class_space = metaslab_class_get_space(mc);
3366 * Determine the size of the first leaf vdev associated with
3367 * our top-level device.
3369 vd = vdev_walk_tree(tvd, NULL, NULL);
3370 ASSERT3P(vd, !=, NULL);
3371 ASSERT(vd->vdev_ops->vdev_op_leaf);
3373 psize = vd->vdev_psize;
3376 * We only try to expand the vdev if it's healthy, less than 4x its
3377 * original size, and it has a valid psize.
3379 if (tvd->vdev_state != VDEV_STATE_HEALTHY ||
3380 psize == 0 || psize >= 4 * ztest_opts.zo_vdev_size) {
3381 spa_config_exit(spa, SCL_STATE, spa);
3382 mutex_exit(&ztest_vdev_lock);
3386 newsize = psize + psize / 8;
3387 ASSERT3U(newsize, >, psize);
3389 if (ztest_opts.zo_verbose >= 6) {
3390 (void) printf("Expanding LUN %s from %lu to %lu\n",
3391 vd->vdev_path, (ulong_t)psize, (ulong_t)newsize);
3395 * Growing the vdev is a two step process:
3396 * 1). expand the physical size (i.e. relabel)
3397 * 2). online the vdev to create the new metaslabs
3399 if (vdev_walk_tree(tvd, grow_vdev, &newsize) != NULL ||
3400 vdev_walk_tree(tvd, online_vdev, NULL) != NULL ||
3401 tvd->vdev_state != VDEV_STATE_HEALTHY) {
3402 if (ztest_opts.zo_verbose >= 5) {
3403 (void) printf("Could not expand LUN because "
3404 "the vdev configuration changed.\n");
3406 spa_config_exit(spa, SCL_STATE, spa);
3407 mutex_exit(&ztest_vdev_lock);
3411 spa_config_exit(spa, SCL_STATE, spa);
3414 * Expanding the LUN will update the config asynchronously,
3415 * thus we must wait for the async thread to complete any
3416 * pending tasks before proceeding.
3420 mutex_enter(&spa->spa_async_lock);
3421 done = (spa->spa_async_thread == NULL && !spa->spa_async_tasks);
3422 mutex_exit(&spa->spa_async_lock);
3425 txg_wait_synced(spa_get_dsl(spa), 0);
3426 (void) poll(NULL, 0, 100);
3429 spa_config_enter(spa, SCL_STATE, spa, RW_READER);
3431 tvd = spa->spa_root_vdev->vdev_child[top];
3432 new_ms_count = tvd->vdev_ms_count;
3433 new_class_space = metaslab_class_get_space(mc);
3435 if (tvd->vdev_mg != mg || mg->mg_class != mc) {
3436 if (ztest_opts.zo_verbose >= 5) {
3437 (void) printf("Could not verify LUN expansion due to "
3438 "intervening vdev offline or remove.\n");
3440 spa_config_exit(spa, SCL_STATE, spa);
3441 mutex_exit(&ztest_vdev_lock);
3446 * Make sure we were able to grow the vdev.
3448 if (new_ms_count <= old_ms_count)
3449 fatal(0, "LUN expansion failed: ms_count %llu <= %llu\n",
3450 old_ms_count, new_ms_count);
3453 * Make sure we were able to grow the pool.
3455 if (new_class_space <= old_class_space)
3456 fatal(0, "LUN expansion failed: class_space %llu <= %llu\n",
3457 old_class_space, new_class_space);
3459 if (ztest_opts.zo_verbose >= 5) {
3460 char oldnumbuf[6], newnumbuf[6];
3462 nicenum(old_class_space, oldnumbuf);
3463 nicenum(new_class_space, newnumbuf);
3464 (void) printf("%s grew from %s to %s\n",
3465 spa->spa_name, oldnumbuf, newnumbuf);
3468 spa_config_exit(spa, SCL_STATE, spa);
3469 mutex_exit(&ztest_vdev_lock);
3473 * Verify that dmu_objset_{create,destroy,open,close} work as expected.
3477 ztest_objset_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3480 * Create the objects common to all ztest datasets.
3482 VERIFY(zap_create_claim(os, ZTEST_DIROBJ,
3483 DMU_OT_ZAP_OTHER, DMU_OT_NONE, 0, tx) == 0);
3487 ztest_dataset_create(char *dsname)
3489 uint64_t zilset = ztest_random(100);
3490 int err = dmu_objset_create(dsname, DMU_OST_OTHER, 0,
3491 ztest_objset_create_cb, NULL);
3493 if (err || zilset < 80)
3496 if (ztest_opts.zo_verbose >= 5)
3497 (void) printf("Setting dataset %s to sync always\n", dsname);
3498 return (ztest_dsl_prop_set_uint64(dsname, ZFS_PROP_SYNC,
3499 ZFS_SYNC_ALWAYS, B_FALSE));
3504 ztest_objset_destroy_cb(const char *name, void *arg)
3507 dmu_object_info_t doi;
3511 * Verify that the dataset contains a directory object.
3513 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_TRUE, FTAG, &os));
3514 error = dmu_object_info(os, ZTEST_DIROBJ, &doi);
3515 if (error != ENOENT) {
3516 /* We could have crashed in the middle of destroying it */
3518 ASSERT3U(doi.doi_type, ==, DMU_OT_ZAP_OTHER);
3519 ASSERT3S(doi.doi_physical_blocks_512, >=, 0);
3521 dmu_objset_disown(os, FTAG);
3524 * Destroy the dataset.
3526 if (strchr(name, '@') != NULL) {
3527 VERIFY0(dsl_destroy_snapshot(name, B_TRUE));
3529 error = dsl_destroy_head(name);
3530 /* There could be a hold on this dataset */
3538 ztest_snapshot_create(char *osname, uint64_t id)
3540 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3543 (void) snprintf(snapname, sizeof (snapname), "%llu", (u_longlong_t)id);
3545 error = dmu_objset_snapshot_one(osname, snapname);
3546 if (error == ENOSPC) {
3547 ztest_record_enospc(FTAG);
3550 if (error != 0 && error != EEXIST) {
3551 fatal(0, "ztest_snapshot_create(%s@%s) = %d", osname,
3558 ztest_snapshot_destroy(char *osname, uint64_t id)
3560 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3563 (void) snprintf(snapname, sizeof (snapname), "%s@%llu", osname,
3566 error = dsl_destroy_snapshot(snapname, B_FALSE);
3567 if (error != 0 && error != ENOENT)
3568 fatal(0, "ztest_snapshot_destroy(%s) = %d", snapname, error);
3574 ztest_dmu_objset_create_destroy(ztest_ds_t *zd, uint64_t id)
3580 char name[ZFS_MAX_DATASET_NAME_LEN];
3584 zdtmp = umem_alloc(sizeof (ztest_ds_t), UMEM_NOFAIL);
3586 (void) rw_rdlock(&ztest_name_lock);
3588 (void) snprintf(name, sizeof (name), "%s/temp_%llu",
3589 ztest_opts.zo_pool, (u_longlong_t)id);
3592 * If this dataset exists from a previous run, process its replay log
3593 * half of the time. If we don't replay it, then dsl_destroy_head()
3594 * (invoked from ztest_objset_destroy_cb()) should just throw it away.
3596 if (ztest_random(2) == 0 &&
3597 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os) == 0) {
3598 ztest_zd_init(zdtmp, NULL, os);
3599 zil_replay(os, zdtmp, ztest_replay_vector);
3600 ztest_zd_fini(zdtmp);
3601 dmu_objset_disown(os, FTAG);
3605 * There may be an old instance of the dataset we're about to
3606 * create lying around from a previous run. If so, destroy it
3607 * and all of its snapshots.
3609 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
3610 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
3613 * Verify that the destroyed dataset is no longer in the namespace.
3615 VERIFY3U(ENOENT, ==, dmu_objset_own(name, DMU_OST_OTHER, B_TRUE,
3619 * Verify that we can create a new dataset.
3621 error = ztest_dataset_create(name);
3623 if (error == ENOSPC) {
3624 ztest_record_enospc(FTAG);
3627 fatal(0, "dmu_objset_create(%s) = %d", name, error);
3630 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os));
3632 ztest_zd_init(zdtmp, NULL, os);
3635 * Open the intent log for it.
3637 zilog = zil_open(os, ztest_get_data);
3640 * Put some objects in there, do a little I/O to them,
3641 * and randomly take a couple of snapshots along the way.
3643 iters = ztest_random(5);
3644 for (i = 0; i < iters; i++) {
3645 ztest_dmu_object_alloc_free(zdtmp, id);
3646 if (ztest_random(iters) == 0)
3647 (void) ztest_snapshot_create(name, i);
3651 * Verify that we cannot create an existing dataset.
3653 VERIFY3U(EEXIST, ==,
3654 dmu_objset_create(name, DMU_OST_OTHER, 0, NULL, NULL));
3657 * Verify that we can hold an objset that is also owned.
3659 VERIFY3U(0, ==, dmu_objset_hold(name, FTAG, &os2));
3660 dmu_objset_rele(os2, FTAG);
3663 * Verify that we cannot own an objset that is already owned.
3666 dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, FTAG, &os2));
3669 dmu_objset_disown(os, FTAG);
3670 ztest_zd_fini(zdtmp);
3672 (void) rw_unlock(&ztest_name_lock);
3674 umem_free(zdtmp, sizeof (ztest_ds_t));
3678 * Verify that dmu_snapshot_{create,destroy,open,close} work as expected.
3681 ztest_dmu_snapshot_create_destroy(ztest_ds_t *zd, uint64_t id)
3683 (void) rw_rdlock(&ztest_name_lock);
3684 (void) ztest_snapshot_destroy(zd->zd_name, id);
3685 (void) ztest_snapshot_create(zd->zd_name, id);
3686 (void) rw_unlock(&ztest_name_lock);
3690 * Cleanup non-standard snapshots and clones.
3693 ztest_dsl_dataset_cleanup(char *osname, uint64_t id)
3702 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3703 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3704 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3705 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3706 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3708 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3709 "%s@s1_%llu", osname, (u_longlong_t)id);
3710 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3711 "%s/c1_%llu", osname, (u_longlong_t)id);
3712 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3713 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3714 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3715 "%s/c2_%llu", osname, (u_longlong_t)id);
3716 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3717 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3719 error = dsl_destroy_head(clone2name);
3720 if (error && error != ENOENT)
3721 fatal(0, "dsl_destroy_head(%s) = %d", clone2name, error);
3722 error = dsl_destroy_snapshot(snap3name, B_FALSE);
3723 if (error && error != ENOENT)
3724 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap3name, error);
3725 error = dsl_destroy_snapshot(snap2name, B_FALSE);
3726 if (error && error != ENOENT)
3727 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap2name, error);
3728 error = dsl_destroy_head(clone1name);
3729 if (error && error != ENOENT)
3730 fatal(0, "dsl_destroy_head(%s) = %d", clone1name, error);
3731 error = dsl_destroy_snapshot(snap1name, B_FALSE);
3732 if (error && error != ENOENT)
3733 fatal(0, "dsl_destroy_snapshot(%s) = %d", snap1name, error);
3735 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3736 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3737 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3738 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3739 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3743 * Verify dsl_dataset_promote handles EBUSY
3746 ztest_dsl_dataset_promote_busy(ztest_ds_t *zd, uint64_t id)
3754 char *osname = zd->zd_name;
3757 snap1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3758 clone1name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3759 snap2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3760 clone2name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3761 snap3name = umem_alloc(ZFS_MAX_DATASET_NAME_LEN, UMEM_NOFAIL);
3763 (void) rw_rdlock(&ztest_name_lock);
3765 ztest_dsl_dataset_cleanup(osname, id);
3767 (void) snprintf(snap1name, ZFS_MAX_DATASET_NAME_LEN,
3768 "%s@s1_%llu", osname, (u_longlong_t)id);
3769 (void) snprintf(clone1name, ZFS_MAX_DATASET_NAME_LEN,
3770 "%s/c1_%llu", osname, (u_longlong_t)id);
3771 (void) snprintf(snap2name, ZFS_MAX_DATASET_NAME_LEN,
3772 "%s@s2_%llu", clone1name, (u_longlong_t)id);
3773 (void) snprintf(clone2name, ZFS_MAX_DATASET_NAME_LEN,
3774 "%s/c2_%llu", osname, (u_longlong_t)id);
3775 (void) snprintf(snap3name, ZFS_MAX_DATASET_NAME_LEN,
3776 "%s@s3_%llu", clone1name, (u_longlong_t)id);
3778 error = dmu_objset_snapshot_one(osname, strchr(snap1name, '@') + 1);
3779 if (error && error != EEXIST) {
3780 if (error == ENOSPC) {
3781 ztest_record_enospc(FTAG);
3784 fatal(0, "dmu_take_snapshot(%s) = %d", snap1name, error);
3787 error = dmu_objset_clone(clone1name, snap1name);
3789 if (error == ENOSPC) {
3790 ztest_record_enospc(FTAG);
3793 fatal(0, "dmu_objset_create(%s) = %d", clone1name, error);
3796 error = dmu_objset_snapshot_one(clone1name, strchr(snap2name, '@') + 1);
3797 if (error && error != EEXIST) {
3798 if (error == ENOSPC) {
3799 ztest_record_enospc(FTAG);
3802 fatal(0, "dmu_open_snapshot(%s) = %d", snap2name, error);
3805 error = dmu_objset_snapshot_one(clone1name, strchr(snap3name, '@') + 1);
3806 if (error && error != EEXIST) {
3807 if (error == ENOSPC) {
3808 ztest_record_enospc(FTAG);
3811 fatal(0, "dmu_open_snapshot(%s) = %d", snap3name, error);
3814 error = dmu_objset_clone(clone2name, snap3name);
3816 if (error == ENOSPC) {
3817 ztest_record_enospc(FTAG);
3820 fatal(0, "dmu_objset_create(%s) = %d", clone2name, error);
3823 error = dmu_objset_own(snap2name, DMU_OST_ANY, B_TRUE, FTAG, &os);
3825 fatal(0, "dmu_objset_own(%s) = %d", snap2name, error);
3826 error = dsl_dataset_promote(clone2name, NULL);
3827 if (error == ENOSPC) {
3828 dmu_objset_disown(os, FTAG);
3829 ztest_record_enospc(FTAG);
3833 fatal(0, "dsl_dataset_promote(%s), %d, not EBUSY", clone2name,
3835 dmu_objset_disown(os, FTAG);
3838 ztest_dsl_dataset_cleanup(osname, id);
3840 (void) rw_unlock(&ztest_name_lock);
3842 umem_free(snap1name, ZFS_MAX_DATASET_NAME_LEN);
3843 umem_free(clone1name, ZFS_MAX_DATASET_NAME_LEN);
3844 umem_free(snap2name, ZFS_MAX_DATASET_NAME_LEN);
3845 umem_free(clone2name, ZFS_MAX_DATASET_NAME_LEN);
3846 umem_free(snap3name, ZFS_MAX_DATASET_NAME_LEN);
3849 #undef OD_ARRAY_SIZE
3850 #define OD_ARRAY_SIZE 4
3853 * Verify that dmu_object_{alloc,free} work as expected.
3856 ztest_dmu_object_alloc_free(ztest_ds_t *zd, uint64_t id)
3863 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3864 od = umem_alloc(size, UMEM_NOFAIL);
3865 batchsize = OD_ARRAY_SIZE;
3867 for (b = 0; b < batchsize; b++)
3868 ztest_od_init(od + b, id, FTAG, b, DMU_OT_UINT64_OTHER,
3872 * Destroy the previous batch of objects, create a new batch,
3873 * and do some I/O on the new objects.
3875 if (ztest_object_init(zd, od, size, B_TRUE) != 0)
3878 while (ztest_random(4 * batchsize) != 0)
3879 ztest_io(zd, od[ztest_random(batchsize)].od_object,
3880 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
3882 umem_free(od, size);
3885 #undef OD_ARRAY_SIZE
3886 #define OD_ARRAY_SIZE 2
3889 * Verify that dmu_{read,write} work as expected.
3892 ztest_dmu_read_write(ztest_ds_t *zd, uint64_t id)
3897 objset_t *os = zd->zd_os;
3898 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
3899 od = umem_alloc(size, UMEM_NOFAIL);
3901 int i, freeit, error;
3903 bufwad_t *packbuf, *bigbuf, *pack, *bigH, *bigT;
3904 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
3905 uint64_t chunksize = (1000 + ztest_random(1000)) * sizeof (uint64_t);
3906 uint64_t regions = 997;
3907 uint64_t stride = 123456789ULL;
3908 uint64_t width = 40;
3909 int free_percent = 5;
3912 * This test uses two objects, packobj and bigobj, that are always
3913 * updated together (i.e. in the same tx) so that their contents are
3914 * in sync and can be compared. Their contents relate to each other
3915 * in a simple way: packobj is a dense array of 'bufwad' structures,
3916 * while bigobj is a sparse array of the same bufwads. Specifically,
3917 * for any index n, there are three bufwads that should be identical:
3919 * packobj, at offset n * sizeof (bufwad_t)
3920 * bigobj, at the head of the nth chunk
3921 * bigobj, at the tail of the nth chunk
3923 * The chunk size is arbitrary. It doesn't have to be a power of two,
3924 * and it doesn't have any relation to the object blocksize.
3925 * The only requirement is that it can hold at least two bufwads.
3927 * Normally, we write the bufwad to each of these locations.
3928 * However, free_percent of the time we instead write zeroes to
3929 * packobj and perform a dmu_free_range() on bigobj. By comparing
3930 * bigobj to packobj, we can verify that the DMU is correctly
3931 * tracking which parts of an object are allocated and free,
3932 * and that the contents of the allocated blocks are correct.
3936 * Read the directory info. If it's the first time, set things up.
3938 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, chunksize);
3939 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
3942 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
3943 umem_free(od, size);
3947 bigobj = od[0].od_object;
3948 packobj = od[1].od_object;
3949 chunksize = od[0].od_gen;
3950 ASSERT(chunksize == od[1].od_gen);
3953 * Prefetch a random chunk of the big object.
3954 * Our aim here is to get some async reads in flight
3955 * for blocks that we may free below; the DMU should
3956 * handle this race correctly.
3958 n = ztest_random(regions) * stride + ztest_random(width);
3959 s = 1 + ztest_random(2 * width - 1);
3960 dmu_prefetch(os, bigobj, 0, n * chunksize, s * chunksize,
3961 ZIO_PRIORITY_SYNC_READ);
3964 * Pick a random index and compute the offsets into packobj and bigobj.
3966 n = ztest_random(regions) * stride + ztest_random(width);
3967 s = 1 + ztest_random(width - 1);
3969 packoff = n * sizeof (bufwad_t);
3970 packsize = s * sizeof (bufwad_t);
3972 bigoff = n * chunksize;
3973 bigsize = s * chunksize;
3975 packbuf = umem_alloc(packsize, UMEM_NOFAIL);
3976 bigbuf = umem_alloc(bigsize, UMEM_NOFAIL);
3979 * free_percent of the time, free a range of bigobj rather than
3982 freeit = (ztest_random(100) < free_percent);
3985 * Read the current contents of our objects.
3987 error = dmu_read(os, packobj, packoff, packsize, packbuf,
3990 error = dmu_read(os, bigobj, bigoff, bigsize, bigbuf,
3995 * Get a tx for the mods to both packobj and bigobj.
3997 tx = dmu_tx_create(os);
3999 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4002 dmu_tx_hold_free(tx, bigobj, bigoff, bigsize);
4004 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4006 /* This accounts for setting the checksum/compression. */
4007 dmu_tx_hold_bonus(tx, bigobj);
4009 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4011 umem_free(packbuf, packsize);
4012 umem_free(bigbuf, bigsize);
4013 umem_free(od, size);
4017 enum zio_checksum cksum;
4019 cksum = (enum zio_checksum)
4020 ztest_random_dsl_prop(ZFS_PROP_CHECKSUM);
4021 } while (cksum >= ZIO_CHECKSUM_LEGACY_FUNCTIONS);
4022 dmu_object_set_checksum(os, bigobj, cksum, tx);
4024 enum zio_compress comp;
4026 comp = (enum zio_compress)
4027 ztest_random_dsl_prop(ZFS_PROP_COMPRESSION);
4028 } while (comp >= ZIO_COMPRESS_LEGACY_FUNCTIONS);
4029 dmu_object_set_compress(os, bigobj, comp, tx);
4032 * For each index from n to n + s, verify that the existing bufwad
4033 * in packobj matches the bufwads at the head and tail of the
4034 * corresponding chunk in bigobj. Then update all three bufwads
4035 * with the new values we want to write out.
4037 for (i = 0; i < s; i++) {
4039 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4041 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4043 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4045 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4046 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4048 if (pack->bw_txg > txg)
4049 fatal(0, "future leak: got %llx, open txg is %llx",
4052 if (pack->bw_data != 0 && pack->bw_index != n + i)
4053 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4054 pack->bw_index, n, i);
4056 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4057 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4059 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4060 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4063 bzero(pack, sizeof (bufwad_t));
4065 pack->bw_index = n + i;
4067 pack->bw_data = 1 + ztest_random(-2ULL);
4074 * We've verified all the old bufwads, and made new ones.
4075 * Now write them out.
4077 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4080 if (ztest_opts.zo_verbose >= 7) {
4081 (void) printf("freeing offset %llx size %llx"
4083 (u_longlong_t)bigoff,
4084 (u_longlong_t)bigsize,
4087 VERIFY(0 == dmu_free_range(os, bigobj, bigoff, bigsize, tx));
4089 if (ztest_opts.zo_verbose >= 7) {
4090 (void) printf("writing offset %llx size %llx"
4092 (u_longlong_t)bigoff,
4093 (u_longlong_t)bigsize,
4096 dmu_write(os, bigobj, bigoff, bigsize, bigbuf, tx);
4102 * Sanity check the stuff we just wrote.
4105 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4106 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4108 VERIFY(0 == dmu_read(os, packobj, packoff,
4109 packsize, packcheck, DMU_READ_PREFETCH));
4110 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4111 bigsize, bigcheck, DMU_READ_PREFETCH));
4113 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4114 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4116 umem_free(packcheck, packsize);
4117 umem_free(bigcheck, bigsize);
4120 umem_free(packbuf, packsize);
4121 umem_free(bigbuf, bigsize);
4122 umem_free(od, size);
4126 compare_and_update_pbbufs(uint64_t s, bufwad_t *packbuf, bufwad_t *bigbuf,
4127 uint64_t bigsize, uint64_t n, uint64_t chunksize, uint64_t txg)
4135 * For each index from n to n + s, verify that the existing bufwad
4136 * in packobj matches the bufwads at the head and tail of the
4137 * corresponding chunk in bigobj. Then update all three bufwads
4138 * with the new values we want to write out.
4140 for (i = 0; i < s; i++) {
4142 pack = (bufwad_t *)((char *)packbuf + i * sizeof (bufwad_t));
4144 bigH = (bufwad_t *)((char *)bigbuf + i * chunksize);
4146 bigT = (bufwad_t *)((char *)bigH + chunksize) - 1;
4148 ASSERT((uintptr_t)bigH - (uintptr_t)bigbuf < bigsize);
4149 ASSERT((uintptr_t)bigT - (uintptr_t)bigbuf < bigsize);
4151 if (pack->bw_txg > txg)
4152 fatal(0, "future leak: got %llx, open txg is %llx",
4155 if (pack->bw_data != 0 && pack->bw_index != n + i)
4156 fatal(0, "wrong index: got %llx, wanted %llx+%llx",
4157 pack->bw_index, n, i);
4159 if (bcmp(pack, bigH, sizeof (bufwad_t)) != 0)
4160 fatal(0, "pack/bigH mismatch in %p/%p", pack, bigH);
4162 if (bcmp(pack, bigT, sizeof (bufwad_t)) != 0)
4163 fatal(0, "pack/bigT mismatch in %p/%p", pack, bigT);
4165 pack->bw_index = n + i;
4167 pack->bw_data = 1 + ztest_random(-2ULL);
4174 #undef OD_ARRAY_SIZE
4175 #define OD_ARRAY_SIZE 2
4178 ztest_dmu_read_write_zcopy(ztest_ds_t *zd, uint64_t id)
4180 objset_t *os = zd->zd_os;
4187 bufwad_t *packbuf, *bigbuf;
4188 uint64_t packobj, packoff, packsize, bigobj, bigoff, bigsize;
4189 uint64_t blocksize = ztest_random_blocksize();
4190 uint64_t chunksize = blocksize;
4191 uint64_t regions = 997;
4192 uint64_t stride = 123456789ULL;
4194 dmu_buf_t *bonus_db;
4195 arc_buf_t **bigbuf_arcbufs;
4196 dmu_object_info_t doi;
4198 size = sizeof (ztest_od_t) * OD_ARRAY_SIZE;
4199 od = umem_alloc(size, UMEM_NOFAIL);
4202 * This test uses two objects, packobj and bigobj, that are always
4203 * updated together (i.e. in the same tx) so that their contents are
4204 * in sync and can be compared. Their contents relate to each other
4205 * in a simple way: packobj is a dense array of 'bufwad' structures,
4206 * while bigobj is a sparse array of the same bufwads. Specifically,
4207 * for any index n, there are three bufwads that should be identical:
4209 * packobj, at offset n * sizeof (bufwad_t)
4210 * bigobj, at the head of the nth chunk
4211 * bigobj, at the tail of the nth chunk
4213 * The chunk size is set equal to bigobj block size so that
4214 * dmu_assign_arcbuf() can be tested for object updates.
4218 * Read the directory info. If it's the first time, set things up.
4220 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4221 ztest_od_init(od + 1, id, FTAG, 1, DMU_OT_UINT64_OTHER, 0, 0,
4225 if (ztest_object_init(zd, od, size, B_FALSE) != 0) {
4226 umem_free(od, size);
4230 bigobj = od[0].od_object;
4231 packobj = od[1].od_object;
4232 blocksize = od[0].od_blocksize;
4233 chunksize = blocksize;
4234 ASSERT(chunksize == od[1].od_gen);
4236 VERIFY(dmu_object_info(os, bigobj, &doi) == 0);
4237 VERIFY(ISP2(doi.doi_data_block_size));
4238 VERIFY(chunksize == doi.doi_data_block_size);
4239 VERIFY(chunksize >= 2 * sizeof (bufwad_t));
4242 * Pick a random index and compute the offsets into packobj and bigobj.
4244 n = ztest_random(regions) * stride + ztest_random(width);
4245 s = 1 + ztest_random(width - 1);
4247 packoff = n * sizeof (bufwad_t);
4248 packsize = s * sizeof (bufwad_t);
4250 bigoff = n * chunksize;
4251 bigsize = s * chunksize;
4253 packbuf = umem_zalloc(packsize, UMEM_NOFAIL);
4254 bigbuf = umem_zalloc(bigsize, UMEM_NOFAIL);
4256 VERIFY3U(0, ==, dmu_bonus_hold(os, bigobj, FTAG, &bonus_db));
4258 bigbuf_arcbufs = umem_zalloc(2 * s * sizeof (arc_buf_t *), UMEM_NOFAIL);
4261 * Iteration 0 test zcopy for DB_UNCACHED dbufs.
4262 * Iteration 1 test zcopy to already referenced dbufs.
4263 * Iteration 2 test zcopy to dirty dbuf in the same txg.
4264 * Iteration 3 test zcopy to dbuf dirty in previous txg.
4265 * Iteration 4 test zcopy when dbuf is no longer dirty.
4266 * Iteration 5 test zcopy when it can't be done.
4267 * Iteration 6 one more zcopy write.
4269 for (i = 0; i < 7; i++) {
4274 * In iteration 5 (i == 5) use arcbufs
4275 * that don't match bigobj blksz to test
4276 * dmu_assign_arcbuf() when it can't directly
4277 * assign an arcbuf to a dbuf.
4279 for (j = 0; j < s; j++) {
4280 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4282 dmu_request_arcbuf(bonus_db, chunksize);
4284 bigbuf_arcbufs[2 * j] =
4285 dmu_request_arcbuf(bonus_db, chunksize / 2);
4286 bigbuf_arcbufs[2 * j + 1] =
4287 dmu_request_arcbuf(bonus_db, chunksize / 2);
4292 * Get a tx for the mods to both packobj and bigobj.
4294 tx = dmu_tx_create(os);
4296 dmu_tx_hold_write(tx, packobj, packoff, packsize);
4297 dmu_tx_hold_write(tx, bigobj, bigoff, bigsize);
4299 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4301 umem_free(packbuf, packsize);
4302 umem_free(bigbuf, bigsize);
4303 for (j = 0; j < s; j++) {
4305 chunksize < (SPA_MINBLOCKSIZE * 2)) {
4306 dmu_return_arcbuf(bigbuf_arcbufs[j]);
4309 bigbuf_arcbufs[2 * j]);
4311 bigbuf_arcbufs[2 * j + 1]);
4314 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4315 umem_free(od, size);
4316 dmu_buf_rele(bonus_db, FTAG);
4321 * 50% of the time don't read objects in the 1st iteration to
4322 * test dmu_assign_arcbuf() for the case when there're no
4323 * existing dbufs for the specified offsets.
4325 if (i != 0 || ztest_random(2) != 0) {
4326 error = dmu_read(os, packobj, packoff,
4327 packsize, packbuf, DMU_READ_PREFETCH);
4329 error = dmu_read(os, bigobj, bigoff, bigsize,
4330 bigbuf, DMU_READ_PREFETCH);
4333 compare_and_update_pbbufs(s, packbuf, bigbuf, bigsize,
4337 * We've verified all the old bufwads, and made new ones.
4338 * Now write them out.
4340 dmu_write(os, packobj, packoff, packsize, packbuf, tx);
4341 if (ztest_opts.zo_verbose >= 7) {
4342 (void) printf("writing offset %llx size %llx"
4344 (u_longlong_t)bigoff,
4345 (u_longlong_t)bigsize,
4348 for (off = bigoff, j = 0; j < s; j++, off += chunksize) {
4350 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4351 bcopy((caddr_t)bigbuf + (off - bigoff),
4352 bigbuf_arcbufs[j]->b_data, chunksize);
4354 bcopy((caddr_t)bigbuf + (off - bigoff),
4355 bigbuf_arcbufs[2 * j]->b_data,
4357 bcopy((caddr_t)bigbuf + (off - bigoff) +
4359 bigbuf_arcbufs[2 * j + 1]->b_data,
4364 VERIFY(dmu_buf_hold(os, bigobj, off,
4365 FTAG, &dbt, DMU_READ_NO_PREFETCH) == 0);
4367 if (i != 5 || chunksize < (SPA_MINBLOCKSIZE * 2)) {
4368 dmu_assign_arcbuf(bonus_db, off,
4369 bigbuf_arcbufs[j], tx);
4371 dmu_assign_arcbuf(bonus_db, off,
4372 bigbuf_arcbufs[2 * j], tx);
4373 dmu_assign_arcbuf(bonus_db,
4374 off + chunksize / 2,
4375 bigbuf_arcbufs[2 * j + 1], tx);
4378 dmu_buf_rele(dbt, FTAG);
4384 * Sanity check the stuff we just wrote.
4387 void *packcheck = umem_alloc(packsize, UMEM_NOFAIL);
4388 void *bigcheck = umem_alloc(bigsize, UMEM_NOFAIL);
4390 VERIFY(0 == dmu_read(os, packobj, packoff,
4391 packsize, packcheck, DMU_READ_PREFETCH));
4392 VERIFY(0 == dmu_read(os, bigobj, bigoff,
4393 bigsize, bigcheck, DMU_READ_PREFETCH));
4395 ASSERT(bcmp(packbuf, packcheck, packsize) == 0);
4396 ASSERT(bcmp(bigbuf, bigcheck, bigsize) == 0);
4398 umem_free(packcheck, packsize);
4399 umem_free(bigcheck, bigsize);
4402 txg_wait_open(dmu_objset_pool(os), 0);
4403 } else if (i == 3) {
4404 txg_wait_synced(dmu_objset_pool(os), 0);
4408 dmu_buf_rele(bonus_db, FTAG);
4409 umem_free(packbuf, packsize);
4410 umem_free(bigbuf, bigsize);
4411 umem_free(bigbuf_arcbufs, 2 * s * sizeof (arc_buf_t *));
4412 umem_free(od, size);
4417 ztest_dmu_write_parallel(ztest_ds_t *zd, uint64_t id)
4421 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4422 uint64_t offset = (1ULL << (ztest_random(20) + 43)) +
4423 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4426 * Have multiple threads write to large offsets in an object
4427 * to verify that parallel writes to an object -- even to the
4428 * same blocks within the object -- doesn't cause any trouble.
4430 ztest_od_init(od, ID_PARALLEL, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4432 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0)
4435 while (ztest_random(10) != 0)
4436 ztest_io(zd, od->od_object, offset);
4438 umem_free(od, sizeof (ztest_od_t));
4442 ztest_dmu_prealloc(ztest_ds_t *zd, uint64_t id)
4445 uint64_t offset = (1ULL << (ztest_random(4) + SPA_MAXBLOCKSHIFT)) +
4446 (ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
4447 uint64_t count = ztest_random(20) + 1;
4448 uint64_t blocksize = ztest_random_blocksize();
4451 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4453 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
4455 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4456 !ztest_random(2)) != 0) {
4457 umem_free(od, sizeof (ztest_od_t));
4461 if (ztest_truncate(zd, od->od_object, offset, count * blocksize) != 0) {
4462 umem_free(od, sizeof (ztest_od_t));
4466 ztest_prealloc(zd, od->od_object, offset, count * blocksize);
4468 data = umem_zalloc(blocksize, UMEM_NOFAIL);
4470 while (ztest_random(count) != 0) {
4471 uint64_t randoff = offset + (ztest_random(count) * blocksize);
4472 if (ztest_write(zd, od->od_object, randoff, blocksize,
4475 while (ztest_random(4) != 0)
4476 ztest_io(zd, od->od_object, randoff);
4479 umem_free(data, blocksize);
4480 umem_free(od, sizeof (ztest_od_t));
4484 * Verify that zap_{create,destroy,add,remove,update} work as expected.
4486 #define ZTEST_ZAP_MIN_INTS 1
4487 #define ZTEST_ZAP_MAX_INTS 4
4488 #define ZTEST_ZAP_MAX_PROPS 1000
4491 ztest_zap(ztest_ds_t *zd, uint64_t id)
4493 objset_t *os = zd->zd_os;
4496 uint64_t txg, last_txg;
4497 uint64_t value[ZTEST_ZAP_MAX_INTS];
4498 uint64_t zl_ints, zl_intsize, prop;
4501 char propname[100], txgname[100];
4503 char *hc[2] = { "s.acl.h", ".s.open.h.hyLZlg" };
4505 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4506 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4508 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4509 !ztest_random(2)) != 0)
4512 object = od->od_object;
4515 * Generate a known hash collision, and verify that
4516 * we can lookup and remove both entries.
4518 tx = dmu_tx_create(os);
4519 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4520 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4523 for (i = 0; i < 2; i++) {
4525 VERIFY3U(0, ==, zap_add(os, object, hc[i], sizeof (uint64_t),
4528 for (i = 0; i < 2; i++) {
4529 VERIFY3U(EEXIST, ==, zap_add(os, object, hc[i],
4530 sizeof (uint64_t), 1, &value[i], tx));
4532 zap_length(os, object, hc[i], &zl_intsize, &zl_ints));
4533 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4534 ASSERT3U(zl_ints, ==, 1);
4536 for (i = 0; i < 2; i++) {
4537 VERIFY3U(0, ==, zap_remove(os, object, hc[i], tx));
4542 * Generate a buch of random entries.
4544 ints = MAX(ZTEST_ZAP_MIN_INTS, object % ZTEST_ZAP_MAX_INTS);
4546 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4547 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4548 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4549 bzero(value, sizeof (value));
4553 * If these zap entries already exist, validate their contents.
4555 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4557 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4558 ASSERT3U(zl_ints, ==, 1);
4560 VERIFY(zap_lookup(os, object, txgname, zl_intsize,
4561 zl_ints, &last_txg) == 0);
4563 VERIFY(zap_length(os, object, propname, &zl_intsize,
4566 ASSERT3U(zl_intsize, ==, sizeof (uint64_t));
4567 ASSERT3U(zl_ints, ==, ints);
4569 VERIFY(zap_lookup(os, object, propname, zl_intsize,
4570 zl_ints, value) == 0);
4572 for (i = 0; i < ints; i++) {
4573 ASSERT3U(value[i], ==, last_txg + object + i);
4576 ASSERT3U(error, ==, ENOENT);
4580 * Atomically update two entries in our zap object.
4581 * The first is named txg_%llu, and contains the txg
4582 * in which the property was last updated. The second
4583 * is named prop_%llu, and the nth element of its value
4584 * should be txg + object + n.
4586 tx = dmu_tx_create(os);
4587 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4588 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4593 fatal(0, "zap future leak: old %llu new %llu", last_txg, txg);
4595 for (i = 0; i < ints; i++)
4596 value[i] = txg + object + i;
4598 VERIFY3U(0, ==, zap_update(os, object, txgname, sizeof (uint64_t),
4600 VERIFY3U(0, ==, zap_update(os, object, propname, sizeof (uint64_t),
4606 * Remove a random pair of entries.
4608 prop = ztest_random(ZTEST_ZAP_MAX_PROPS);
4609 (void) sprintf(propname, "prop_%llu", (u_longlong_t)prop);
4610 (void) sprintf(txgname, "txg_%llu", (u_longlong_t)prop);
4612 error = zap_length(os, object, txgname, &zl_intsize, &zl_ints);
4614 if (error == ENOENT)
4619 tx = dmu_tx_create(os);
4620 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4621 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4624 VERIFY3U(0, ==, zap_remove(os, object, txgname, tx));
4625 VERIFY3U(0, ==, zap_remove(os, object, propname, tx));
4628 umem_free(od, sizeof (ztest_od_t));
4632 * Testcase to test the upgrading of a microzap to fatzap.
4635 ztest_fzap(ztest_ds_t *zd, uint64_t id)
4637 objset_t *os = zd->zd_os;
4639 uint64_t object, txg;
4642 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4643 ztest_od_init(od, id, FTAG, 0, DMU_OT_ZAP_OTHER, 0, 0, 0);
4645 if (ztest_object_init(zd, od, sizeof (ztest_od_t),
4646 !ztest_random(2)) != 0)
4648 object = od->od_object;
4651 * Add entries to this ZAP and make sure it spills over
4652 * and gets upgraded to a fatzap. Also, since we are adding
4653 * 2050 entries we should see ptrtbl growth and leaf-block split.
4655 for (i = 0; i < 2050; i++) {
4656 char name[ZFS_MAX_DATASET_NAME_LEN];
4661 (void) snprintf(name, sizeof (name), "fzap-%llu-%llu",
4662 (u_longlong_t)id, (u_longlong_t)value);
4664 tx = dmu_tx_create(os);
4665 dmu_tx_hold_zap(tx, object, B_TRUE, name);
4666 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4669 error = zap_add(os, object, name, sizeof (uint64_t), 1,
4671 ASSERT(error == 0 || error == EEXIST);
4675 umem_free(od, sizeof (ztest_od_t));
4680 ztest_zap_parallel(ztest_ds_t *zd, uint64_t id)
4682 objset_t *os = zd->zd_os;
4684 uint64_t txg, object, count, wsize, wc, zl_wsize, zl_wc;
4686 int i, namelen, error;
4687 int micro = ztest_random(2);
4688 char name[20], string_value[20];
4691 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4692 ztest_od_init(od, ID_PARALLEL, FTAG, micro, DMU_OT_ZAP_OTHER, 0, 0, 0);
4694 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4695 umem_free(od, sizeof (ztest_od_t));
4699 object = od->od_object;
4702 * Generate a random name of the form 'xxx.....' where each
4703 * x is a random printable character and the dots are dots.
4704 * There are 94 such characters, and the name length goes from
4705 * 6 to 20, so there are 94^3 * 15 = 12,458,760 possible names.
4707 namelen = ztest_random(sizeof (name) - 5) + 5 + 1;
4709 for (i = 0; i < 3; i++)
4710 name[i] = '!' + ztest_random('~' - '!' + 1);
4711 for (; i < namelen - 1; i++)
4715 if ((namelen & 1) || micro) {
4716 wsize = sizeof (txg);
4722 data = string_value;
4726 VERIFY0(zap_count(os, object, &count));
4727 ASSERT(count != -1ULL);
4730 * Select an operation: length, lookup, add, update, remove.
4732 i = ztest_random(5);
4735 tx = dmu_tx_create(os);
4736 dmu_tx_hold_zap(tx, object, B_TRUE, NULL);
4737 txg = ztest_tx_assign(tx, TXG_MIGHTWAIT, FTAG);
4739 umem_free(od, sizeof (ztest_od_t));
4742 bcopy(name, string_value, namelen);
4746 bzero(string_value, namelen);
4752 error = zap_length(os, object, name, &zl_wsize, &zl_wc);
4754 ASSERT3U(wsize, ==, zl_wsize);
4755 ASSERT3U(wc, ==, zl_wc);
4757 ASSERT3U(error, ==, ENOENT);
4762 error = zap_lookup(os, object, name, wsize, wc, data);
4764 if (data == string_value &&
4765 bcmp(name, data, namelen) != 0)
4766 fatal(0, "name '%s' != val '%s' len %d",
4767 name, data, namelen);
4769 ASSERT3U(error, ==, ENOENT);
4774 error = zap_add(os, object, name, wsize, wc, data, tx);
4775 ASSERT(error == 0 || error == EEXIST);
4779 VERIFY(zap_update(os, object, name, wsize, wc, data, tx) == 0);
4783 error = zap_remove(os, object, name, tx);
4784 ASSERT(error == 0 || error == ENOENT);
4791 umem_free(od, sizeof (ztest_od_t));
4795 * Commit callback data.
4797 typedef struct ztest_cb_data {
4798 list_node_t zcd_node;
4800 int zcd_expected_err;
4801 boolean_t zcd_added;
4802 boolean_t zcd_called;
4806 /* This is the actual commit callback function */
4808 ztest_commit_callback(void *arg, int error)
4810 ztest_cb_data_t *data = arg;
4811 uint64_t synced_txg;
4813 VERIFY(data != NULL);
4814 VERIFY3S(data->zcd_expected_err, ==, error);
4815 VERIFY(!data->zcd_called);
4817 synced_txg = spa_last_synced_txg(data->zcd_spa);
4818 if (data->zcd_txg > synced_txg)
4819 fatal(0, "commit callback of txg %" PRIu64 " called prematurely"
4820 ", last synced txg = %" PRIu64 "\n", data->zcd_txg,
4823 data->zcd_called = B_TRUE;
4825 if (error == ECANCELED) {
4826 ASSERT0(data->zcd_txg);
4827 ASSERT(!data->zcd_added);
4830 * The private callback data should be destroyed here, but
4831 * since we are going to check the zcd_called field after
4832 * dmu_tx_abort(), we will destroy it there.
4837 ASSERT(data->zcd_added);
4838 ASSERT3U(data->zcd_txg, !=, 0);
4840 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4842 /* See if this cb was called more quickly */
4843 if ((synced_txg - data->zcd_txg) < zc_min_txg_delay)
4844 zc_min_txg_delay = synced_txg - data->zcd_txg;
4846 /* Remove our callback from the list */
4847 list_remove(&zcl.zcl_callbacks, data);
4849 (void) mutex_exit(&zcl.zcl_callbacks_lock);
4851 umem_free(data, sizeof (ztest_cb_data_t));
4854 /* Allocate and initialize callback data structure */
4855 static ztest_cb_data_t *
4856 ztest_create_cb_data(objset_t *os, uint64_t txg)
4858 ztest_cb_data_t *cb_data;
4860 cb_data = umem_zalloc(sizeof (ztest_cb_data_t), UMEM_NOFAIL);
4862 cb_data->zcd_txg = txg;
4863 cb_data->zcd_spa = dmu_objset_spa(os);
4864 list_link_init(&cb_data->zcd_node);
4870 * Commit callback test.
4873 ztest_dmu_commit_callbacks(ztest_ds_t *zd, uint64_t id)
4875 objset_t *os = zd->zd_os;
4878 ztest_cb_data_t *cb_data[3], *tmp_cb;
4879 uint64_t old_txg, txg;
4882 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
4883 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
4885 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
4886 umem_free(od, sizeof (ztest_od_t));
4890 tx = dmu_tx_create(os);
4892 cb_data[0] = ztest_create_cb_data(os, 0);
4893 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[0]);
4895 dmu_tx_hold_write(tx, od->od_object, 0, sizeof (uint64_t));
4897 /* Every once in a while, abort the transaction on purpose */
4898 if (ztest_random(100) == 0)
4902 error = dmu_tx_assign(tx, TXG_NOWAIT);
4904 txg = error ? 0 : dmu_tx_get_txg(tx);
4906 cb_data[0]->zcd_txg = txg;
4907 cb_data[1] = ztest_create_cb_data(os, txg);
4908 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[1]);
4912 * It's not a strict requirement to call the registered
4913 * callbacks from inside dmu_tx_abort(), but that's what
4914 * it's supposed to happen in the current implementation
4915 * so we will check for that.
4917 for (i = 0; i < 2; i++) {
4918 cb_data[i]->zcd_expected_err = ECANCELED;
4919 VERIFY(!cb_data[i]->zcd_called);
4924 for (i = 0; i < 2; i++) {
4925 VERIFY(cb_data[i]->zcd_called);
4926 umem_free(cb_data[i], sizeof (ztest_cb_data_t));
4929 umem_free(od, sizeof (ztest_od_t));
4933 cb_data[2] = ztest_create_cb_data(os, txg);
4934 dmu_tx_callback_register(tx, ztest_commit_callback, cb_data[2]);
4937 * Read existing data to make sure there isn't a future leak.
4939 VERIFY(0 == dmu_read(os, od->od_object, 0, sizeof (uint64_t),
4940 &old_txg, DMU_READ_PREFETCH));
4943 fatal(0, "future leak: got %" PRIu64 ", open txg is %" PRIu64,
4946 dmu_write(os, od->od_object, 0, sizeof (uint64_t), &txg, tx);
4948 (void) mutex_enter(&zcl.zcl_callbacks_lock);
4951 * Since commit callbacks don't have any ordering requirement and since
4952 * it is theoretically possible for a commit callback to be called
4953 * after an arbitrary amount of time has elapsed since its txg has been
4954 * synced, it is difficult to reliably determine whether a commit
4955 * callback hasn't been called due to high load or due to a flawed
4958 * In practice, we will assume that if after a certain number of txgs a
4959 * commit callback hasn't been called, then most likely there's an
4960 * implementation bug..
4962 tmp_cb = list_head(&zcl.zcl_callbacks);
4963 if (tmp_cb != NULL &&
4964 tmp_cb->zcd_txg + ZTEST_COMMIT_CB_THRESH < txg) {
4965 fatal(0, "Commit callback threshold exceeded, oldest txg: %"
4966 PRIu64 ", open txg: %" PRIu64 "\n", tmp_cb->zcd_txg, txg);
4970 * Let's find the place to insert our callbacks.
4972 * Even though the list is ordered by txg, it is possible for the
4973 * insertion point to not be the end because our txg may already be
4974 * quiescing at this point and other callbacks in the open txg
4975 * (from other objsets) may have sneaked in.
4977 tmp_cb = list_tail(&zcl.zcl_callbacks);
4978 while (tmp_cb != NULL && tmp_cb->zcd_txg > txg)
4979 tmp_cb = list_prev(&zcl.zcl_callbacks, tmp_cb);
4981 /* Add the 3 callbacks to the list */
4982 for (i = 0; i < 3; i++) {
4984 list_insert_head(&zcl.zcl_callbacks, cb_data[i]);
4986 list_insert_after(&zcl.zcl_callbacks, tmp_cb,
4989 cb_data[i]->zcd_added = B_TRUE;
4990 VERIFY(!cb_data[i]->zcd_called);
4992 tmp_cb = cb_data[i];
4997 (void) mutex_exit(&zcl.zcl_callbacks_lock);
5001 umem_free(od, sizeof (ztest_od_t));
5005 * Visit each object in the dataset. Verify that its properties
5006 * are consistent what was stored in the block tag when it was created,
5007 * and that its unused bonus buffer space has not been overwritten.
5011 ztest_verify_dnode_bt(ztest_ds_t *zd, uint64_t id)
5013 objset_t *os = zd->zd_os;
5017 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
5018 ztest_block_tag_t *bt = NULL;
5019 dmu_object_info_t doi;
5022 if (dmu_bonus_hold(os, obj, FTAG, &db) != 0)
5025 dmu_object_info_from_db(db, &doi);
5026 if (doi.doi_bonus_size >= sizeof (*bt))
5027 bt = ztest_bt_bonus(db);
5029 if (bt && bt->bt_magic == BT_MAGIC) {
5030 ztest_bt_verify(bt, os, obj, doi.doi_dnodesize,
5031 bt->bt_offset, bt->bt_gen, bt->bt_txg,
5033 ztest_verify_unused_bonus(db, bt, obj, os, bt->bt_gen);
5036 dmu_buf_rele(db, FTAG);
5042 ztest_dsl_prop_get_set(ztest_ds_t *zd, uint64_t id)
5044 zfs_prop_t proplist[] = {
5046 ZFS_PROP_COMPRESSION,
5052 (void) rw_rdlock(&ztest_name_lock);
5054 for (p = 0; p < sizeof (proplist) / sizeof (proplist[0]); p++)
5055 (void) ztest_dsl_prop_set_uint64(zd->zd_name, proplist[p],
5056 ztest_random_dsl_prop(proplist[p]), (int)ztest_random(2));
5058 VERIFY0(ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_RECORDSIZE,
5059 ztest_random_blocksize(), (int)ztest_random(2)));
5061 (void) rw_unlock(&ztest_name_lock);
5066 ztest_spa_prop_get_set(ztest_ds_t *zd, uint64_t id)
5068 nvlist_t *props = NULL;
5070 (void) rw_rdlock(&ztest_name_lock);
5072 (void) ztest_spa_prop_set_uint64(ZPOOL_PROP_DEDUPDITTO,
5073 ZIO_DEDUPDITTO_MIN + ztest_random(ZIO_DEDUPDITTO_MIN));
5075 VERIFY0(spa_prop_get(ztest_spa, &props));
5077 if (ztest_opts.zo_verbose >= 6)
5078 dump_nvlist(props, 4);
5082 (void) rw_unlock(&ztest_name_lock);
5086 user_release_one(const char *snapname, const char *holdname)
5088 nvlist_t *snaps, *holds;
5091 snaps = fnvlist_alloc();
5092 holds = fnvlist_alloc();
5093 fnvlist_add_boolean(holds, holdname);
5094 fnvlist_add_nvlist(snaps, snapname, holds);
5095 fnvlist_free(holds);
5096 error = dsl_dataset_user_release(snaps, NULL);
5097 fnvlist_free(snaps);
5102 * Test snapshot hold/release and deferred destroy.
5105 ztest_dmu_snapshot_hold(ztest_ds_t *zd, uint64_t id)
5108 objset_t *os = zd->zd_os;
5112 char clonename[100];
5114 char osname[ZFS_MAX_DATASET_NAME_LEN];
5117 (void) rw_rdlock(&ztest_name_lock);
5119 dmu_objset_name(os, osname);
5121 (void) snprintf(snapname, sizeof (snapname), "sh1_%llu",
5123 (void) snprintf(fullname, sizeof (fullname), "%s@%s", osname, snapname);
5124 (void) snprintf(clonename, sizeof (clonename),
5125 "%s/ch1_%llu", osname, (u_longlong_t)id);
5126 (void) snprintf(tag, sizeof (tag), "tag_%llu", (u_longlong_t)id);
5129 * Clean up from any previous run.
5131 error = dsl_destroy_head(clonename);
5132 if (error != ENOENT)
5134 error = user_release_one(fullname, tag);
5135 if (error != ESRCH && error != ENOENT)
5137 error = dsl_destroy_snapshot(fullname, B_FALSE);
5138 if (error != ENOENT)
5142 * Create snapshot, clone it, mark snap for deferred destroy,
5143 * destroy clone, verify snap was also destroyed.
5145 error = dmu_objset_snapshot_one(osname, snapname);
5147 if (error == ENOSPC) {
5148 ztest_record_enospc("dmu_objset_snapshot");
5151 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5154 error = dmu_objset_clone(clonename, fullname);
5156 if (error == ENOSPC) {
5157 ztest_record_enospc("dmu_objset_clone");
5160 fatal(0, "dmu_objset_clone(%s) = %d", clonename, error);
5163 error = dsl_destroy_snapshot(fullname, B_TRUE);
5165 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5169 error = dsl_destroy_head(clonename);
5171 fatal(0, "dsl_destroy_head(%s) = %d", clonename, error);
5173 error = dmu_objset_hold(fullname, FTAG, &origin);
5174 if (error != ENOENT)
5175 fatal(0, "dmu_objset_hold(%s) = %d", fullname, error);
5178 * Create snapshot, add temporary hold, verify that we can't
5179 * destroy a held snapshot, mark for deferred destroy,
5180 * release hold, verify snapshot was destroyed.
5182 error = dmu_objset_snapshot_one(osname, snapname);
5184 if (error == ENOSPC) {
5185 ztest_record_enospc("dmu_objset_snapshot");
5188 fatal(0, "dmu_objset_snapshot(%s) = %d", fullname, error);
5191 holds = fnvlist_alloc();
5192 fnvlist_add_string(holds, fullname, tag);
5193 error = dsl_dataset_user_hold(holds, 0, NULL);
5194 fnvlist_free(holds);
5196 if (error == ENOSPC) {
5197 ztest_record_enospc("dsl_dataset_user_hold");
5200 fatal(0, "dsl_dataset_user_hold(%s, %s) = %u",
5201 fullname, tag, error);
5204 error = dsl_destroy_snapshot(fullname, B_FALSE);
5205 if (error != EBUSY) {
5206 fatal(0, "dsl_destroy_snapshot(%s, B_FALSE) = %d",
5210 error = dsl_destroy_snapshot(fullname, B_TRUE);
5212 fatal(0, "dsl_destroy_snapshot(%s, B_TRUE) = %d",
5216 error = user_release_one(fullname, tag);
5218 fatal(0, "user_release_one(%s, %s) = %d", fullname, tag, error);
5220 VERIFY3U(dmu_objset_hold(fullname, FTAG, &origin), ==, ENOENT);
5223 (void) rw_unlock(&ztest_name_lock);
5227 * Inject random faults into the on-disk data.
5231 ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
5233 ztest_shared_t *zs = ztest_shared;
5234 spa_t *spa = ztest_spa;
5238 uint64_t bad = 0x1990c0ffeedecadeull;
5243 int bshift = SPA_MAXBLOCKSHIFT + 2;
5249 boolean_t islog = B_FALSE;
5251 path0 = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5252 pathrand = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
5254 mutex_enter(&ztest_vdev_lock);
5255 maxfaults = MAXFAULTS();
5256 leaves = MAX(zs->zs_mirrors, 1) * ztest_opts.zo_raidz;
5257 mirror_save = zs->zs_mirrors;
5258 mutex_exit(&ztest_vdev_lock);
5260 ASSERT(leaves >= 1);
5263 * Grab the name lock as reader. There are some operations
5264 * which don't like to have their vdevs changed while
5265 * they are in progress (i.e. spa_change_guid). Those
5266 * operations will have grabbed the name lock as writer.
5268 (void) rw_rdlock(&ztest_name_lock);
5271 * We need SCL_STATE here because we're going to look at vd0->vdev_tsd.
5273 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5275 if (ztest_random(2) == 0) {
5277 * Inject errors on a normal data device or slog device.
5279 top = ztest_random_vdev_top(spa, B_TRUE);
5280 leaf = ztest_random(leaves) + zs->zs_splits;
5283 * Generate paths to the first leaf in this top-level vdev,
5284 * and to the random leaf we selected. We'll induce transient
5285 * write failures and random online/offline activity on leaf 0,
5286 * and we'll write random garbage to the randomly chosen leaf.
5288 (void) snprintf(path0, MAXPATHLEN, ztest_dev_template,
5289 ztest_opts.zo_dir, ztest_opts.zo_pool,
5290 top * leaves + zs->zs_splits);
5291 (void) snprintf(pathrand, MAXPATHLEN, ztest_dev_template,
5292 ztest_opts.zo_dir, ztest_opts.zo_pool,
5293 top * leaves + leaf);
5295 vd0 = vdev_lookup_by_path(spa->spa_root_vdev, path0);
5296 if (vd0 != NULL && vd0->vdev_top->vdev_islog)
5300 * If the top-level vdev needs to be resilvered
5301 * then we only allow faults on the device that is
5304 if (vd0 != NULL && maxfaults != 1 &&
5305 (!vdev_resilver_needed(vd0->vdev_top, NULL, NULL) ||
5306 vd0->vdev_resilver_txg != 0)) {
5308 * Make vd0 explicitly claim to be unreadable,
5309 * or unwriteable, or reach behind its back
5310 * and close the underlying fd. We can do this if
5311 * maxfaults == 0 because we'll fail and reexecute,
5312 * and we can do it if maxfaults >= 2 because we'll
5313 * have enough redundancy. If maxfaults == 1, the
5314 * combination of this with injection of random data
5315 * corruption below exceeds the pool's fault tolerance.
5317 vdev_file_t *vf = vd0->vdev_tsd;
5319 if (vf != NULL && ztest_random(3) == 0) {
5320 (void) close(vf->vf_vnode->v_fd);
5321 vf->vf_vnode->v_fd = -1;
5322 } else if (ztest_random(2) == 0) {
5323 vd0->vdev_cant_read = B_TRUE;
5325 vd0->vdev_cant_write = B_TRUE;
5327 guid0 = vd0->vdev_guid;
5331 * Inject errors on an l2cache device.
5333 spa_aux_vdev_t *sav = &spa->spa_l2cache;
5335 if (sav->sav_count == 0) {
5336 spa_config_exit(spa, SCL_STATE, FTAG);
5337 (void) rw_unlock(&ztest_name_lock);
5340 vd0 = sav->sav_vdevs[ztest_random(sav->sav_count)];
5341 guid0 = vd0->vdev_guid;
5342 (void) strcpy(path0, vd0->vdev_path);
5343 (void) strcpy(pathrand, vd0->vdev_path);
5347 maxfaults = INT_MAX; /* no limit on cache devices */
5350 spa_config_exit(spa, SCL_STATE, FTAG);
5351 (void) rw_unlock(&ztest_name_lock);
5354 * If we can tolerate two or more faults, or we're dealing
5355 * with a slog, randomly online/offline vd0.
5357 if ((maxfaults >= 2 || islog) && guid0 != 0) {
5358 if (ztest_random(10) < 6) {
5359 int flags = (ztest_random(2) == 0 ?
5360 ZFS_OFFLINE_TEMPORARY : 0);
5363 * We have to grab the zs_name_lock as writer to
5364 * prevent a race between offlining a slog and
5365 * destroying a dataset. Offlining the slog will
5366 * grab a reference on the dataset which may cause
5367 * dsl_destroy_head() to fail with EBUSY thus
5368 * leaving the dataset in an inconsistent state.
5371 (void) rw_wrlock(&ztest_name_lock);
5373 VERIFY(vdev_offline(spa, guid0, flags) != EBUSY);
5376 (void) rw_unlock(&ztest_name_lock);
5379 * Ideally we would like to be able to randomly
5380 * call vdev_[on|off]line without holding locks
5381 * to force unpredictable failures but the side
5382 * effects of vdev_[on|off]line prevent us from
5383 * doing so. We grab the ztest_vdev_lock here to
5384 * prevent a race between injection testing and
5387 mutex_enter(&ztest_vdev_lock);
5388 (void) vdev_online(spa, guid0, 0, NULL);
5389 mutex_exit(&ztest_vdev_lock);
5397 * We have at least single-fault tolerance, so inject data corruption.
5399 fd = open(pathrand, O_RDWR);
5401 if (fd == -1) /* we hit a gap in the device namespace */
5404 fsize = lseek(fd, 0, SEEK_END);
5406 while (--iters != 0) {
5408 * The offset must be chosen carefully to ensure that
5409 * we do not inject a given logical block with errors
5410 * on two different leaf devices, because ZFS can not
5411 * tolerate that (if maxfaults==1).
5413 * We divide each leaf into chunks of size
5414 * (# leaves * SPA_MAXBLOCKSIZE * 4). Within each chunk
5415 * there is a series of ranges to which we can inject errors.
5416 * Each range can accept errors on only a single leaf vdev.
5417 * The error injection ranges are separated by ranges
5418 * which we will not inject errors on any device (DMZs).
5419 * Each DMZ must be large enough such that a single block
5420 * can not straddle it, so that a single block can not be
5421 * a target in two different injection ranges (on different
5424 * For example, with 3 leaves, each chunk looks like:
5425 * 0 to 32M: injection range for leaf 0
5426 * 32M to 64M: DMZ - no injection allowed
5427 * 64M to 96M: injection range for leaf 1
5428 * 96M to 128M: DMZ - no injection allowed
5429 * 128M to 160M: injection range for leaf 2
5430 * 160M to 192M: DMZ - no injection allowed
5432 offset = ztest_random(fsize / (leaves << bshift)) *
5433 (leaves << bshift) + (leaf << bshift) +
5434 (ztest_random(1ULL << (bshift - 1)) & -8ULL);
5437 * Only allow damage to the labels at one end of the vdev.
5439 * If all labels are damaged, the device will be totally
5440 * inaccessible, which will result in loss of data,
5441 * because we also damage (parts of) the other side of
5444 * Additionally, we will always have both an even and an
5445 * odd label, so that we can handle crashes in the
5446 * middle of vdev_config_sync().
5448 if ((leaf & 1) == 0 && offset < VDEV_LABEL_START_SIZE)
5452 * The two end labels are stored at the "end" of the disk, but
5453 * the end of the disk (vdev_psize) is aligned to
5454 * sizeof (vdev_label_t).
5456 uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
5457 if ((leaf & 1) == 1 &&
5458 offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
5461 mutex_enter(&ztest_vdev_lock);
5462 if (mirror_save != zs->zs_mirrors) {
5463 mutex_exit(&ztest_vdev_lock);
5468 if (pwrite(fd, &bad, sizeof (bad), offset) != sizeof (bad))
5469 fatal(1, "can't inject bad word at 0x%llx in %s",
5472 mutex_exit(&ztest_vdev_lock);
5474 if (ztest_opts.zo_verbose >= 7)
5475 (void) printf("injected bad word into %s,"
5476 " offset 0x%llx\n", pathrand, (u_longlong_t)offset);
5481 umem_free(path0, MAXPATHLEN);
5482 umem_free(pathrand, MAXPATHLEN);
5486 * Verify that DDT repair works as expected.
5489 ztest_ddt_repair(ztest_ds_t *zd, uint64_t id)
5491 ztest_shared_t *zs = ztest_shared;
5492 spa_t *spa = ztest_spa;
5493 objset_t *os = zd->zd_os;
5495 uint64_t object, blocksize, txg, pattern, psize;
5496 enum zio_checksum checksum = spa_dedup_checksum(spa);
5501 int copies = 2 * ZIO_DEDUPDITTO_MIN;
5504 blocksize = ztest_random_blocksize();
5505 blocksize = MIN(blocksize, 2048); /* because we write so many */
5507 od = umem_alloc(sizeof (ztest_od_t), UMEM_NOFAIL);
5508 ztest_od_init(od, id, FTAG, 0, DMU_OT_UINT64_OTHER, blocksize, 0, 0);
5510 if (ztest_object_init(zd, od, sizeof (ztest_od_t), B_FALSE) != 0) {
5511 umem_free(od, sizeof (ztest_od_t));
5516 * Take the name lock as writer to prevent anyone else from changing
5517 * the pool and dataset properies we need to maintain during this test.
5519 (void) rw_wrlock(&ztest_name_lock);
5521 if (ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_DEDUP, checksum,
5523 ztest_dsl_prop_set_uint64(zd->zd_name, ZFS_PROP_COPIES, 1,
5525 (void) rw_unlock(&ztest_name_lock);
5526 umem_free(od, sizeof (ztest_od_t));
5530 dmu_objset_stats_t dds;
5531 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
5532 dmu_objset_fast_stat(os, &dds);
5533 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
5535 object = od[0].od_object;
5536 blocksize = od[0].od_blocksize;
5537 pattern = zs->zs_guid ^ dds.dds_guid;
5539 ASSERT(object != 0);
5541 tx = dmu_tx_create(os);
5542 dmu_tx_hold_write(tx, object, 0, copies * blocksize);
5543 txg = ztest_tx_assign(tx, TXG_WAIT, FTAG);
5545 (void) rw_unlock(&ztest_name_lock);
5546 umem_free(od, sizeof (ztest_od_t));
5551 * Write all the copies of our block.
5553 for (i = 0; i < copies; i++) {
5554 uint64_t offset = i * blocksize;
5555 int error = dmu_buf_hold(os, object, offset, FTAG, &db,
5556 DMU_READ_NO_PREFETCH);
5558 fatal(B_FALSE, "dmu_buf_hold(%p, %llu, %llu) = %u",
5559 os, (long long)object, (long long) offset, error);
5561 ASSERT(db->db_offset == offset);
5562 ASSERT(db->db_size == blocksize);
5563 ASSERT(ztest_pattern_match(db->db_data, db->db_size, pattern) ||
5564 ztest_pattern_match(db->db_data, db->db_size, 0ULL));
5565 dmu_buf_will_fill(db, tx);
5566 ztest_pattern_set(db->db_data, db->db_size, pattern);
5567 dmu_buf_rele(db, FTAG);
5571 txg_wait_synced(spa_get_dsl(spa), txg);
5574 * Find out what block we got.
5576 VERIFY0(dmu_buf_hold(os, object, 0, FTAG, &db,
5577 DMU_READ_NO_PREFETCH));
5578 blk = *((dmu_buf_impl_t *)db)->db_blkptr;
5579 dmu_buf_rele(db, FTAG);
5582 * Damage the block. Dedup-ditto will save us when we read it later.
5584 psize = BP_GET_PSIZE(&blk);
5585 abd = abd_alloc_linear(psize, B_TRUE);
5586 ztest_pattern_set(abd_to_buf(abd), psize, ~pattern);
5588 (void) zio_wait(zio_rewrite(NULL, spa, 0, &blk,
5589 abd, psize, NULL, NULL, ZIO_PRIORITY_SYNC_WRITE,
5590 ZIO_FLAG_CANFAIL | ZIO_FLAG_INDUCE_DAMAGE, NULL));
5594 (void) rw_unlock(&ztest_name_lock);
5595 umem_free(od, sizeof (ztest_od_t));
5603 ztest_scrub(ztest_ds_t *zd, uint64_t id)
5605 spa_t *spa = ztest_spa;
5607 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5608 (void) poll(NULL, 0, 100); /* wait a moment, then force a restart */
5609 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5613 * Change the guid for the pool.
5617 ztest_reguid(ztest_ds_t *zd, uint64_t id)
5619 spa_t *spa = ztest_spa;
5620 uint64_t orig, load;
5623 orig = spa_guid(spa);
5624 load = spa_load_guid(spa);
5626 (void) rw_wrlock(&ztest_name_lock);
5627 error = spa_change_guid(spa);
5628 (void) rw_unlock(&ztest_name_lock);
5633 if (ztest_opts.zo_verbose >= 4) {
5634 (void) printf("Changed guid old %llu -> %llu\n",
5635 (u_longlong_t)orig, (u_longlong_t)spa_guid(spa));
5638 VERIFY3U(orig, !=, spa_guid(spa));
5639 VERIFY3U(load, ==, spa_load_guid(spa));
5643 * Rename the pool to a different name and then rename it back.
5647 ztest_spa_rename(ztest_ds_t *zd, uint64_t id)
5649 char *oldname, *newname;
5652 if (ztest_opts.zo_mmp_test)
5655 (void) rw_wrlock(&ztest_name_lock);
5657 oldname = ztest_opts.zo_pool;
5658 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
5659 (void) strcpy(newname, oldname);
5660 (void) strcat(newname, "_tmp");
5665 VERIFY3U(0, ==, spa_rename(oldname, newname));
5668 * Try to open it under the old name, which shouldn't exist
5670 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
5673 * Open it under the new name and make sure it's still the same spa_t.
5675 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
5677 ASSERT(spa == ztest_spa);
5678 spa_close(spa, FTAG);
5681 * Rename it back to the original
5683 VERIFY3U(0, ==, spa_rename(newname, oldname));
5686 * Make sure it can still be opened
5688 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5690 ASSERT(spa == ztest_spa);
5691 spa_close(spa, FTAG);
5693 umem_free(newname, strlen(newname) + 1);
5695 (void) rw_unlock(&ztest_name_lock);
5699 ztest_fletcher(ztest_ds_t *zd, uint64_t id)
5701 hrtime_t end = gethrtime() + NANOSEC;
5703 while (gethrtime() <= end) {
5704 int run_count = 100;
5706 struct abd *abd_data, *abd_meta;
5711 zio_cksum_t zc_ref_byteswap;
5713 size = ztest_random_blocksize();
5715 buf = umem_alloc(size, UMEM_NOFAIL);
5716 abd_data = abd_alloc(size, B_FALSE);
5717 abd_meta = abd_alloc(size, B_TRUE);
5719 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5720 *ptr = ztest_random(UINT_MAX);
5722 abd_copy_from_buf_off(abd_data, buf, 0, size);
5723 abd_copy_from_buf_off(abd_meta, buf, 0, size);
5725 VERIFY0(fletcher_4_impl_set("scalar"));
5726 fletcher_4_native(buf, size, NULL, &zc_ref);
5727 fletcher_4_byteswap(buf, size, NULL, &zc_ref_byteswap);
5729 VERIFY0(fletcher_4_impl_set("cycle"));
5730 while (run_count-- > 0) {
5732 zio_cksum_t zc_byteswap;
5734 fletcher_4_byteswap(buf, size, NULL, &zc_byteswap);
5735 fletcher_4_native(buf, size, NULL, &zc);
5737 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5738 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5739 sizeof (zc_byteswap)));
5741 /* Test ABD - data */
5742 abd_fletcher_4_byteswap(abd_data, size, NULL,
5744 abd_fletcher_4_native(abd_data, size, NULL, &zc);
5746 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5747 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5748 sizeof (zc_byteswap)));
5750 /* Test ABD - metadata */
5751 abd_fletcher_4_byteswap(abd_meta, size, NULL,
5753 abd_fletcher_4_native(abd_meta, size, NULL, &zc);
5755 VERIFY0(bcmp(&zc, &zc_ref, sizeof (zc)));
5756 VERIFY0(bcmp(&zc_byteswap, &zc_ref_byteswap,
5757 sizeof (zc_byteswap)));
5761 umem_free(buf, size);
5768 ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
5775 zio_cksum_t zc_ref_bswap;
5777 hrtime_t end = gethrtime() + NANOSEC;
5779 while (gethrtime() <= end) {
5780 int run_count = 100;
5782 size = ztest_random_blocksize();
5783 buf = umem_alloc(size, UMEM_NOFAIL);
5785 for (i = 0, ptr = buf; i < size / sizeof (*ptr); i++, ptr++)
5786 *ptr = ztest_random(UINT_MAX);
5788 VERIFY0(fletcher_4_impl_set("scalar"));
5789 fletcher_4_native(buf, size, NULL, &zc_ref);
5790 fletcher_4_byteswap(buf, size, NULL, &zc_ref_bswap);
5792 VERIFY0(fletcher_4_impl_set("cycle"));
5794 while (run_count-- > 0) {
5796 zio_cksum_t zc_bswap;
5799 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5800 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5802 while (pos < size) {
5803 size_t inc = 64 * ztest_random(size / 67);
5804 /* sometimes add few bytes to test non-simd */
5805 if (ztest_random(100) < 10)
5806 inc += P2ALIGN(ztest_random(64),
5809 if (inc > (size - pos))
5812 fletcher_4_incremental_native(buf + pos, inc,
5814 fletcher_4_incremental_byteswap(buf + pos, inc,
5820 VERIFY3U(pos, ==, size);
5822 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5823 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5826 * verify if incremental on the whole buffer is
5827 * equivalent to non-incremental version
5829 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
5830 ZIO_SET_CHECKSUM(&zc_bswap, 0, 0, 0, 0);
5832 fletcher_4_incremental_native(buf, size, &zc);
5833 fletcher_4_incremental_byteswap(buf, size, &zc_bswap);
5835 VERIFY(ZIO_CHECKSUM_EQUAL(zc, zc_ref));
5836 VERIFY(ZIO_CHECKSUM_EQUAL(zc_bswap, zc_ref_bswap));
5839 umem_free(buf, size);
5844 ztest_check_path(char *path)
5847 /* return true on success */
5848 return (!stat(path, &s));
5852 ztest_get_zdb_bin(char *bin, int len)
5856 * Try to use ZDB_PATH and in-tree zdb path. If not successful, just
5857 * let popen to search through PATH.
5859 if ((zdb_path = getenv("ZDB_PATH"))) {
5860 strlcpy(bin, zdb_path, len); /* In env */
5861 if (!ztest_check_path(bin)) {
5862 ztest_dump_core = 0;
5863 fatal(1, "invalid ZDB_PATH '%s'", bin);
5868 VERIFY(realpath(getexecname(), bin) != NULL);
5869 if (strstr(bin, "/ztest/")) {
5870 strstr(bin, "/ztest/")[0] = '\0'; /* In-tree */
5871 strcat(bin, "/zdb/zdb");
5872 if (ztest_check_path(bin))
5879 * Verify pool integrity by running zdb.
5882 ztest_run_zdb(char *pool)
5888 const int len = MAXPATHLEN + MAXNAMELEN + 20;
5891 bin = umem_alloc(len, UMEM_NOFAIL);
5892 zdb = umem_alloc(len, UMEM_NOFAIL);
5893 zbuf = umem_alloc(1024, UMEM_NOFAIL);
5895 ztest_get_zdb_bin(bin, len);
5898 "%s -bcc%s%s -G -d -U %s %s",
5900 ztest_opts.zo_verbose >= 3 ? "s" : "",
5901 ztest_opts.zo_verbose >= 4 ? "v" : "",
5905 if (ztest_opts.zo_verbose >= 5)
5906 (void) printf("Executing %s\n", strstr(zdb, "zdb "));
5908 fp = popen(zdb, "r");
5910 while (fgets(zbuf, 1024, fp) != NULL)
5911 if (ztest_opts.zo_verbose >= 3)
5912 (void) printf("%s", zbuf);
5914 status = pclose(fp);
5919 ztest_dump_core = 0;
5920 if (WIFEXITED(status))
5921 fatal(0, "'%s' exit code %d", zdb, WEXITSTATUS(status));
5923 fatal(0, "'%s' died with signal %d", zdb, WTERMSIG(status));
5925 umem_free(bin, len);
5926 umem_free(zdb, len);
5927 umem_free(zbuf, 1024);
5931 ztest_walk_pool_directory(char *header)
5935 if (ztest_opts.zo_verbose >= 6)
5936 (void) printf("%s\n", header);
5938 mutex_enter(&spa_namespace_lock);
5939 while ((spa = spa_next(spa)) != NULL)
5940 if (ztest_opts.zo_verbose >= 6)
5941 (void) printf("\t%s\n", spa_name(spa));
5942 mutex_exit(&spa_namespace_lock);
5946 ztest_spa_import_export(char *oldname, char *newname)
5948 nvlist_t *config, *newconfig;
5953 if (ztest_opts.zo_verbose >= 4) {
5954 (void) printf("import/export: old = %s, new = %s\n",
5959 * Clean up from previous runs.
5961 (void) spa_destroy(newname);
5964 * Get the pool's configuration and guid.
5966 VERIFY3U(0, ==, spa_open(oldname, &spa, FTAG));
5969 * Kick off a scrub to tickle scrub/export races.
5971 if (ztest_random(2) == 0)
5972 (void) spa_scan(spa, POOL_SCAN_SCRUB);
5974 pool_guid = spa_guid(spa);
5975 spa_close(spa, FTAG);
5977 ztest_walk_pool_directory("pools before export");
5982 VERIFY3U(0, ==, spa_export(oldname, &config, B_FALSE, B_FALSE));
5984 ztest_walk_pool_directory("pools after export");
5989 newconfig = spa_tryimport(config);
5990 ASSERT(newconfig != NULL);
5991 nvlist_free(newconfig);
5994 * Import it under the new name.
5996 error = spa_import(newname, config, NULL, 0);
5998 dump_nvlist(config, 0);
5999 fatal(B_FALSE, "couldn't import pool %s as %s: error %u",
6000 oldname, newname, error);
6003 ztest_walk_pool_directory("pools after import");
6006 * Try to import it again -- should fail with EEXIST.
6008 VERIFY3U(EEXIST, ==, spa_import(newname, config, NULL, 0));
6011 * Try to import it under a different name -- should fail with EEXIST.
6013 VERIFY3U(EEXIST, ==, spa_import(oldname, config, NULL, 0));
6016 * Verify that the pool is no longer visible under the old name.
6018 VERIFY3U(ENOENT, ==, spa_open(oldname, &spa, FTAG));
6021 * Verify that we can open and close the pool using the new name.
6023 VERIFY3U(0, ==, spa_open(newname, &spa, FTAG));
6024 ASSERT(pool_guid == spa_guid(spa));
6025 spa_close(spa, FTAG);
6027 nvlist_free(config);
6031 ztest_resume(spa_t *spa)
6033 if (spa_suspended(spa) && ztest_opts.zo_verbose >= 6)
6034 (void) printf("resuming from suspended state\n");
6035 spa_vdev_state_enter(spa, SCL_NONE);
6036 vdev_clear(spa, NULL);
6037 (void) spa_vdev_state_exit(spa, NULL, 0);
6038 (void) zio_resume(spa);
6042 ztest_resume_thread(void *arg)
6046 while (!ztest_exiting) {
6047 if (spa_suspended(spa))
6049 (void) poll(NULL, 0, 100);
6052 * Periodically change the zfs_compressed_arc_enabled setting.
6054 if (ztest_random(10) == 0)
6055 zfs_compressed_arc_enabled = ztest_random(2);
6058 * Periodically change the zfs_abd_scatter_enabled setting.
6060 if (ztest_random(10) == 0)
6061 zfs_abd_scatter_enabled = ztest_random(2);
6073 ztest_deadman_alarm(int sig)
6075 fatal(0, "failed to complete within %d seconds of deadline", GRACE);
6080 ztest_execute(int test, ztest_info_t *zi, uint64_t id)
6082 ztest_ds_t *zd = &ztest_ds[id % ztest_opts.zo_datasets];
6083 ztest_shared_callstate_t *zc = ZTEST_GET_SHARED_CALLSTATE(test);
6084 hrtime_t functime = gethrtime();
6087 for (i = 0; i < zi->zi_iters; i++)
6088 zi->zi_func(zd, id);
6090 functime = gethrtime() - functime;
6092 atomic_add_64(&zc->zc_count, 1);
6093 atomic_add_64(&zc->zc_time, functime);
6095 if (ztest_opts.zo_verbose >= 4)
6096 (void) printf("%6.2f sec in %s\n",
6097 (double)functime / NANOSEC, zi->zi_funcname);
6101 ztest_thread(void *arg)
6104 uint64_t id = (uintptr_t)arg;
6105 ztest_shared_t *zs = ztest_shared;
6109 ztest_shared_callstate_t *zc;
6111 while ((now = gethrtime()) < zs->zs_thread_stop) {
6113 * See if it's time to force a crash.
6115 if (now > zs->zs_thread_kill)
6119 * If we're getting ENOSPC with some regularity, stop.
6121 if (zs->zs_enospc_count > 10)
6125 * Pick a random function to execute.
6127 rand = ztest_random(ZTEST_FUNCS);
6128 zi = &ztest_info[rand];
6129 zc = ZTEST_GET_SHARED_CALLSTATE(rand);
6130 call_next = zc->zc_next;
6132 if (now >= call_next &&
6133 atomic_cas_64(&zc->zc_next, call_next, call_next +
6134 ztest_random(2 * zi->zi_interval[0] + 1)) == call_next) {
6135 ztest_execute(rand, zi, id);
6145 ztest_dataset_name(char *dsname, char *pool, int d)
6147 (void) snprintf(dsname, ZFS_MAX_DATASET_NAME_LEN, "%s/ds_%d", pool, d);
6151 ztest_dataset_destroy(int d)
6153 char name[ZFS_MAX_DATASET_NAME_LEN];
6156 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6158 if (ztest_opts.zo_verbose >= 3)
6159 (void) printf("Destroying %s to free up space\n", name);
6162 * Cleanup any non-standard clones and snapshots. In general,
6163 * ztest thread t operates on dataset (t % zopt_datasets),
6164 * so there may be more than one thing to clean up.
6166 for (t = d; t < ztest_opts.zo_threads;
6167 t += ztest_opts.zo_datasets)
6168 ztest_dsl_dataset_cleanup(name, t);
6170 (void) dmu_objset_find(name, ztest_objset_destroy_cb, NULL,
6171 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
6175 ztest_dataset_dirobj_verify(ztest_ds_t *zd)
6177 uint64_t usedobjs, dirobjs, scratch;
6180 * ZTEST_DIROBJ is the object directory for the entire dataset.
6181 * Therefore, the number of objects in use should equal the
6182 * number of ZTEST_DIROBJ entries, +1 for ZTEST_DIROBJ itself.
6183 * If not, we have an object leak.
6185 * Note that we can only check this in ztest_dataset_open(),
6186 * when the open-context and syncing-context values agree.
6187 * That's because zap_count() returns the open-context value,
6188 * while dmu_objset_space() returns the rootbp fill count.
6190 VERIFY3U(0, ==, zap_count(zd->zd_os, ZTEST_DIROBJ, &dirobjs));
6191 dmu_objset_space(zd->zd_os, &scratch, &scratch, &usedobjs, &scratch);
6192 ASSERT3U(dirobjs + 1, ==, usedobjs);
6196 ztest_dataset_open(int d)
6198 ztest_ds_t *zd = &ztest_ds[d];
6199 uint64_t committed_seq = ZTEST_GET_SHARED_DS(d)->zd_seq;
6202 char name[ZFS_MAX_DATASET_NAME_LEN];
6205 ztest_dataset_name(name, ztest_opts.zo_pool, d);
6207 (void) rw_rdlock(&ztest_name_lock);
6209 error = ztest_dataset_create(name);
6210 if (error == ENOSPC) {
6211 (void) rw_unlock(&ztest_name_lock);
6212 ztest_record_enospc(FTAG);
6215 ASSERT(error == 0 || error == EEXIST);
6217 VERIFY0(dmu_objset_own(name, DMU_OST_OTHER, B_FALSE, zd, &os));
6218 (void) rw_unlock(&ztest_name_lock);
6220 ztest_zd_init(zd, ZTEST_GET_SHARED_DS(d), os);
6222 zilog = zd->zd_zilog;
6224 if (zilog->zl_header->zh_claim_lr_seq != 0 &&
6225 zilog->zl_header->zh_claim_lr_seq < committed_seq)
6226 fatal(0, "missing log records: claimed %llu < committed %llu",
6227 zilog->zl_header->zh_claim_lr_seq, committed_seq);
6229 ztest_dataset_dirobj_verify(zd);
6231 zil_replay(os, zd, ztest_replay_vector);
6233 ztest_dataset_dirobj_verify(zd);
6235 if (ztest_opts.zo_verbose >= 6)
6236 (void) printf("%s replay %llu blocks, %llu records, seq %llu\n",
6238 (u_longlong_t)zilog->zl_parse_blk_count,
6239 (u_longlong_t)zilog->zl_parse_lr_count,
6240 (u_longlong_t)zilog->zl_replaying_seq);
6242 zilog = zil_open(os, ztest_get_data);
6244 if (zilog->zl_replaying_seq != 0 &&
6245 zilog->zl_replaying_seq < committed_seq)
6246 fatal(0, "missing log records: replayed %llu < committed %llu",
6247 zilog->zl_replaying_seq, committed_seq);
6253 ztest_dataset_close(int d)
6255 ztest_ds_t *zd = &ztest_ds[d];
6257 zil_close(zd->zd_zilog);
6258 dmu_objset_disown(zd->zd_os, zd);
6264 * Kick off threads to run tests on all datasets in parallel.
6267 ztest_run(ztest_shared_t *zs)
6272 kthread_t *resume_thread;
6277 ztest_exiting = B_FALSE;
6280 * Initialize parent/child shared state.
6282 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6283 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6285 zs->zs_thread_start = gethrtime();
6286 zs->zs_thread_stop =
6287 zs->zs_thread_start + ztest_opts.zo_passtime * NANOSEC;
6288 zs->zs_thread_stop = MIN(zs->zs_thread_stop, zs->zs_proc_stop);
6289 zs->zs_thread_kill = zs->zs_thread_stop;
6290 if (ztest_random(100) < ztest_opts.zo_killrate) {
6291 zs->zs_thread_kill -=
6292 ztest_random(ztest_opts.zo_passtime * NANOSEC);
6295 mutex_init(&zcl.zcl_callbacks_lock, NULL, MUTEX_DEFAULT, NULL);
6297 list_create(&zcl.zcl_callbacks, sizeof (ztest_cb_data_t),
6298 offsetof(ztest_cb_data_t, zcd_node));
6303 kernel_init(FREAD | FWRITE);
6304 VERIFY0(spa_open(ztest_opts.zo_pool, &spa, FTAG));
6305 spa->spa_debug = B_TRUE;
6306 metaslab_preload_limit = ztest_random(20) + 1;
6309 dmu_objset_stats_t dds;
6310 VERIFY0(dmu_objset_own(ztest_opts.zo_pool,
6311 DMU_OST_ANY, B_TRUE, FTAG, &os));
6312 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
6313 dmu_objset_fast_stat(os, &dds);
6314 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
6315 zs->zs_guid = dds.dds_guid;
6316 dmu_objset_disown(os, FTAG);
6318 spa->spa_dedup_ditto = 2 * ZIO_DEDUPDITTO_MIN;
6321 * We don't expect the pool to suspend unless maxfaults == 0,
6322 * in which case ztest_fault_inject() temporarily takes away
6323 * the only valid replica.
6325 if (MAXFAULTS() == 0)
6326 spa->spa_failmode = ZIO_FAILURE_MODE_WAIT;
6328 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
6331 * Create a thread to periodically resume suspended I/O.
6333 VERIFY3P((resume_thread = zk_thread_create(NULL, 0,
6334 (thread_func_t)ztest_resume_thread, spa, 0, NULL, TS_RUN, 0,
6335 PTHREAD_CREATE_JOINABLE)), !=, NULL);
6339 * Set a deadman alarm to abort() if we hang.
6341 signal(SIGALRM, ztest_deadman_alarm);
6342 alarm((zs->zs_thread_stop - zs->zs_thread_start) / NANOSEC + GRACE);
6346 * Verify that we can safely inquire about about any object,
6347 * whether it's allocated or not. To make it interesting,
6348 * we probe a 5-wide window around each power of two.
6349 * This hits all edge cases, including zero and the max.
6351 for (t = 0; t < 64; t++) {
6352 for (d = -5; d <= 5; d++) {
6353 error = dmu_object_info(spa->spa_meta_objset,
6354 (1ULL << t) + d, NULL);
6355 ASSERT(error == 0 || error == ENOENT ||
6361 * If we got any ENOSPC errors on the previous run, destroy something.
6363 if (zs->zs_enospc_count != 0) {
6364 int d = ztest_random(ztest_opts.zo_datasets);
6365 ztest_dataset_destroy(d);
6367 zs->zs_enospc_count = 0;
6369 tid = umem_zalloc(ztest_opts.zo_threads * sizeof (kt_did_t),
6372 if (ztest_opts.zo_verbose >= 4)
6373 (void) printf("starting main threads...\n");
6376 * Kick off all the tests that run in parallel.
6378 for (t = 0; t < ztest_opts.zo_threads; t++) {
6381 if (t < ztest_opts.zo_datasets &&
6382 ztest_dataset_open(t) != 0) {
6384 ztest_opts.zo_threads * sizeof (kt_did_t));
6388 VERIFY3P(thread = zk_thread_create(NULL, 0,
6389 (thread_func_t)ztest_thread,
6390 (void *)(uintptr_t)t, 0, NULL, TS_RUN, 0,
6391 PTHREAD_CREATE_JOINABLE), !=, NULL);
6392 tid[t] = thread->t_tid;
6396 * Wait for all of the tests to complete. We go in reverse order
6397 * so we don't close datasets while threads are still using them.
6399 for (t = ztest_opts.zo_threads - 1; t >= 0; t--) {
6400 thread_join(tid[t]);
6401 if (t < ztest_opts.zo_datasets)
6402 ztest_dataset_close(t);
6405 txg_wait_synced(spa_get_dsl(spa), 0);
6407 zs->zs_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
6408 zs->zs_space = metaslab_class_get_space(spa_normal_class(spa));
6410 umem_free(tid, ztest_opts.zo_threads * sizeof (kt_did_t));
6412 /* Kill the resume thread */
6413 ztest_exiting = B_TRUE;
6414 thread_join(resume_thread->t_tid);
6418 * Right before closing the pool, kick off a bunch of async I/O;
6419 * spa_close() should wait for it to complete.
6421 for (object = 1; object < 50; object++) {
6422 dmu_prefetch(spa->spa_meta_objset, object, 0, 0, 1ULL << 20,
6423 ZIO_PRIORITY_SYNC_READ);
6426 /* Verify that at least one commit cb was called in a timely fashion */
6427 if (zc_cb_counter >= ZTEST_COMMIT_CB_MIN_REG)
6428 VERIFY0(zc_min_txg_delay);
6430 spa_close(spa, FTAG);
6433 * Verify that we can loop over all pools.
6435 mutex_enter(&spa_namespace_lock);
6436 for (spa = spa_next(NULL); spa != NULL; spa = spa_next(spa))
6437 if (ztest_opts.zo_verbose > 3)
6438 (void) printf("spa_next: found %s\n", spa_name(spa));
6439 mutex_exit(&spa_namespace_lock);
6442 * Verify that we can export the pool and reimport it under a
6445 if ((ztest_random(2) == 0) && !ztest_opts.zo_mmp_test) {
6446 char name[ZFS_MAX_DATASET_NAME_LEN];
6447 (void) snprintf(name, sizeof (name), "%s_import",
6448 ztest_opts.zo_pool);
6449 ztest_spa_import_export(ztest_opts.zo_pool, name);
6450 ztest_spa_import_export(name, ztest_opts.zo_pool);
6455 list_destroy(&zcl.zcl_callbacks);
6456 mutex_destroy(&zcl.zcl_callbacks_lock);
6457 (void) rwlock_destroy(&ztest_name_lock);
6458 mutex_destroy(&ztest_vdev_lock);
6464 ztest_ds_t *zd = &ztest_ds[0];
6468 if (ztest_opts.zo_verbose >= 3)
6469 (void) printf("testing spa_freeze()...\n");
6471 kernel_init(FREAD | FWRITE);
6472 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6473 VERIFY3U(0, ==, ztest_dataset_open(0));
6474 spa->spa_debug = B_TRUE;
6478 * Force the first log block to be transactionally allocated.
6479 * We have to do this before we freeze the pool -- otherwise
6480 * the log chain won't be anchored.
6482 while (BP_IS_HOLE(&zd->zd_zilog->zl_header->zh_log)) {
6483 ztest_dmu_object_alloc_free(zd, 0);
6484 zil_commit(zd->zd_zilog, 0);
6487 txg_wait_synced(spa_get_dsl(spa), 0);
6490 * Freeze the pool. This stops spa_sync() from doing anything,
6491 * so that the only way to record changes from now on is the ZIL.
6496 * Because it is hard to predict how much space a write will actually
6497 * require beforehand, we leave ourselves some fudge space to write over
6500 uint64_t capacity = metaslab_class_get_space(spa_normal_class(spa)) / 2;
6503 * Run tests that generate log records but don't alter the pool config
6504 * or depend on DSL sync tasks (snapshots, objset create/destroy, etc).
6505 * We do a txg_wait_synced() after each iteration to force the txg
6506 * to increase well beyond the last synced value in the uberblock.
6507 * The ZIL should be OK with that.
6509 * Run a random number of times less than zo_maxloops and ensure we do
6510 * not run out of space on the pool.
6512 while (ztest_random(10) != 0 &&
6513 numloops++ < ztest_opts.zo_maxloops &&
6514 metaslab_class_get_alloc(spa_normal_class(spa)) < capacity) {
6516 ztest_od_init(&od, 0, FTAG, 0, DMU_OT_UINT64_OTHER, 0, 0, 0);
6517 VERIFY0(ztest_object_init(zd, &od, sizeof (od), B_FALSE));
6518 ztest_io(zd, od.od_object,
6519 ztest_random(ZTEST_RANGE_LOCKS) << SPA_MAXBLOCKSHIFT);
6520 txg_wait_synced(spa_get_dsl(spa), 0);
6524 * Commit all of the changes we just generated.
6526 zil_commit(zd->zd_zilog, 0);
6527 txg_wait_synced(spa_get_dsl(spa), 0);
6530 * Close our dataset and close the pool.
6532 ztest_dataset_close(0);
6533 spa_close(spa, FTAG);
6537 * Open and close the pool and dataset to induce log replay.
6539 kernel_init(FREAD | FWRITE);
6540 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6541 ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
6542 VERIFY3U(0, ==, ztest_dataset_open(0));
6543 ztest_dataset_close(0);
6545 spa->spa_debug = B_TRUE;
6547 txg_wait_synced(spa_get_dsl(spa), 0);
6548 ztest_reguid(NULL, 0);
6550 spa_close(spa, FTAG);
6555 print_time(hrtime_t t, char *timebuf)
6557 hrtime_t s = t / NANOSEC;
6558 hrtime_t m = s / 60;
6559 hrtime_t h = m / 60;
6560 hrtime_t d = h / 24;
6569 (void) sprintf(timebuf,
6570 "%llud%02lluh%02llum%02llus", d, h, m, s);
6572 (void) sprintf(timebuf, "%lluh%02llum%02llus", h, m, s);
6574 (void) sprintf(timebuf, "%llum%02llus", m, s);
6576 (void) sprintf(timebuf, "%llus", s);
6580 make_random_props(void)
6584 VERIFY(nvlist_alloc(&props, NV_UNIQUE_NAME, 0) == 0);
6585 if (ztest_random(2) == 0)
6587 VERIFY(nvlist_add_uint64(props, "autoreplace", 1) == 0);
6593 * Import a storage pool with the given name.
6596 ztest_import(ztest_shared_t *zs)
6598 libzfs_handle_t *hdl;
6599 importargs_t args = { 0 };
6601 nvlist_t *cfg = NULL;
6603 char *searchdirs[nsearch];
6604 char *name = ztest_opts.zo_pool;
6605 int flags = ZFS_IMPORT_MISSING_LOG;
6608 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6609 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6611 kernel_init(FREAD | FWRITE);
6612 hdl = libzfs_init();
6614 searchdirs[0] = ztest_opts.zo_dir;
6615 args.paths = nsearch;
6616 args.path = searchdirs;
6617 args.can_be_active = B_FALSE;
6619 error = zpool_tryimport(hdl, name, &cfg, &args);
6621 (void) fatal(0, "No pools found\n");
6623 VERIFY0(spa_import(name, cfg, NULL, flags));
6624 VERIFY0(spa_open(name, &spa, FTAG));
6625 zs->zs_metaslab_sz =
6626 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6627 spa_close(spa, FTAG);
6632 if (!ztest_opts.zo_mmp_test) {
6633 ztest_run_zdb(ztest_opts.zo_pool);
6635 ztest_run_zdb(ztest_opts.zo_pool);
6638 (void) rwlock_destroy(&ztest_name_lock);
6639 mutex_destroy(&ztest_vdev_lock);
6643 * Create a storage pool with the given name and initial vdev size.
6644 * Then test spa_freeze() functionality.
6647 ztest_init(ztest_shared_t *zs)
6650 nvlist_t *nvroot, *props;
6653 mutex_init(&ztest_vdev_lock, NULL, MUTEX_DEFAULT, NULL);
6654 VERIFY(rwlock_init(&ztest_name_lock, USYNC_THREAD, NULL) == 0);
6656 kernel_init(FREAD | FWRITE);
6659 * Create the storage pool.
6661 (void) spa_destroy(ztest_opts.zo_pool);
6662 ztest_shared->zs_vdev_next_leaf = 0;
6664 zs->zs_mirrors = ztest_opts.zo_mirrors;
6665 nvroot = make_vdev_root(NULL, NULL, NULL, ztest_opts.zo_vdev_size, 0,
6666 0, ztest_opts.zo_raidz, zs->zs_mirrors, 1);
6667 props = make_random_props();
6668 for (i = 0; i < SPA_FEATURES; i++) {
6670 VERIFY3S(-1, !=, asprintf(&buf, "feature@%s",
6671 spa_feature_table[i].fi_uname));
6672 VERIFY3U(0, ==, nvlist_add_uint64(props, buf, 0));
6675 VERIFY3U(0, ==, spa_create(ztest_opts.zo_pool, nvroot, props, NULL));
6676 nvlist_free(nvroot);
6679 VERIFY3U(0, ==, spa_open(ztest_opts.zo_pool, &spa, FTAG));
6680 zs->zs_metaslab_sz =
6681 1ULL << spa->spa_root_vdev->vdev_child[0]->vdev_ms_shift;
6682 spa_close(spa, FTAG);
6686 if (!ztest_opts.zo_mmp_test) {
6687 ztest_run_zdb(ztest_opts.zo_pool);
6689 ztest_run_zdb(ztest_opts.zo_pool);
6692 (void) rwlock_destroy(&ztest_name_lock);
6693 mutex_destroy(&ztest_vdev_lock);
6699 static char ztest_name_data[] = "/tmp/ztest.data.XXXXXX";
6701 ztest_fd_data = mkstemp(ztest_name_data);
6702 ASSERT3S(ztest_fd_data, >=, 0);
6703 (void) unlink(ztest_name_data);
6707 shared_data_size(ztest_shared_hdr_t *hdr)
6711 size = hdr->zh_hdr_size;
6712 size += hdr->zh_opts_size;
6713 size += hdr->zh_size;
6714 size += hdr->zh_stats_size * hdr->zh_stats_count;
6715 size += hdr->zh_ds_size * hdr->zh_ds_count;
6724 ztest_shared_hdr_t *hdr;
6726 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6727 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6728 ASSERT(hdr != MAP_FAILED);
6730 VERIFY3U(0, ==, ftruncate(ztest_fd_data, sizeof (ztest_shared_hdr_t)));
6732 hdr->zh_hdr_size = sizeof (ztest_shared_hdr_t);
6733 hdr->zh_opts_size = sizeof (ztest_shared_opts_t);
6734 hdr->zh_size = sizeof (ztest_shared_t);
6735 hdr->zh_stats_size = sizeof (ztest_shared_callstate_t);
6736 hdr->zh_stats_count = ZTEST_FUNCS;
6737 hdr->zh_ds_size = sizeof (ztest_shared_ds_t);
6738 hdr->zh_ds_count = ztest_opts.zo_datasets;
6740 size = shared_data_size(hdr);
6741 VERIFY3U(0, ==, ftruncate(ztest_fd_data, size));
6743 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6750 ztest_shared_hdr_t *hdr;
6753 hdr = (void *)mmap(0, P2ROUNDUP(sizeof (*hdr), getpagesize()),
6754 PROT_READ, MAP_SHARED, ztest_fd_data, 0);
6755 ASSERT(hdr != MAP_FAILED);
6757 size = shared_data_size(hdr);
6759 (void) munmap((caddr_t)hdr, P2ROUNDUP(sizeof (*hdr), getpagesize()));
6760 hdr = ztest_shared_hdr = (void *)mmap(0, P2ROUNDUP(size, getpagesize()),
6761 PROT_READ | PROT_WRITE, MAP_SHARED, ztest_fd_data, 0);
6762 ASSERT(hdr != MAP_FAILED);
6763 buf = (uint8_t *)hdr;
6765 offset = hdr->zh_hdr_size;
6766 ztest_shared_opts = (void *)&buf[offset];
6767 offset += hdr->zh_opts_size;
6768 ztest_shared = (void *)&buf[offset];
6769 offset += hdr->zh_size;
6770 ztest_shared_callstate = (void *)&buf[offset];
6771 offset += hdr->zh_stats_size * hdr->zh_stats_count;
6772 ztest_shared_ds = (void *)&buf[offset];
6776 exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
6780 char *cmdbuf = NULL;
6785 cmdbuf = umem_alloc(MAXPATHLEN, UMEM_NOFAIL);
6786 (void) strlcpy(cmdbuf, getexecname(), MAXPATHLEN);
6791 fatal(1, "fork failed");
6793 if (pid == 0) { /* child */
6794 char *emptyargv[2] = { cmd, NULL };
6795 char fd_data_str[12];
6797 struct rlimit rl = { 1024, 1024 };
6798 (void) setrlimit(RLIMIT_NOFILE, &rl);
6800 (void) close(ztest_fd_rand);
6801 VERIFY(11 >= snprintf(fd_data_str, 12, "%d", ztest_fd_data));
6802 VERIFY(0 == setenv("ZTEST_FD_DATA", fd_data_str, 1));
6804 (void) enable_extended_FILE_stdio(-1, -1);
6805 if (libpath != NULL)
6806 VERIFY(0 == setenv("LD_LIBRARY_PATH", libpath, 1));
6807 (void) execv(cmd, emptyargv);
6808 ztest_dump_core = B_FALSE;
6809 fatal(B_TRUE, "exec failed: %s", cmd);
6812 if (cmdbuf != NULL) {
6813 umem_free(cmdbuf, MAXPATHLEN);
6817 while (waitpid(pid, &status, 0) != pid)
6819 if (statusp != NULL)
6822 if (WIFEXITED(status)) {
6823 if (WEXITSTATUS(status) != 0) {
6824 (void) fprintf(stderr, "child exited with code %d\n",
6825 WEXITSTATUS(status));
6829 } else if (WIFSIGNALED(status)) {
6830 if (!ignorekill || WTERMSIG(status) != SIGKILL) {
6831 (void) fprintf(stderr, "child died with signal %d\n",
6837 (void) fprintf(stderr, "something strange happened to child\n");
6844 ztest_run_init(void)
6848 ztest_shared_t *zs = ztest_shared;
6851 * Blow away any existing copy of zpool.cache
6853 (void) remove(spa_config_path);
6855 if (ztest_opts.zo_init == 0) {
6856 if (ztest_opts.zo_verbose >= 1)
6857 (void) printf("Importing pool %s\n",
6858 ztest_opts.zo_pool);
6864 * Create and initialize our storage pool.
6866 for (i = 1; i <= ztest_opts.zo_init; i++) {
6867 bzero(zs, sizeof (ztest_shared_t));
6868 if (ztest_opts.zo_verbose >= 3 &&
6869 ztest_opts.zo_init != 1) {
6870 (void) printf("ztest_init(), pass %d\n", i);
6877 main(int argc, char **argv)
6885 ztest_shared_callstate_t *zc;
6892 char *fd_data_str = getenv("ZTEST_FD_DATA");
6893 struct sigaction action;
6895 (void) setvbuf(stdout, NULL, _IOLBF, 0);
6897 dprintf_setup(&argc, argv);
6899 action.sa_handler = sig_handler;
6900 sigemptyset(&action.sa_mask);
6901 action.sa_flags = 0;
6903 if (sigaction(SIGSEGV, &action, NULL) < 0) {
6904 (void) fprintf(stderr, "ztest: cannot catch SIGSEGV: %s.\n",
6909 if (sigaction(SIGABRT, &action, NULL) < 0) {
6910 (void) fprintf(stderr, "ztest: cannot catch SIGABRT: %s.\n",
6915 ztest_fd_rand = open("/dev/urandom", O_RDONLY);
6916 ASSERT3S(ztest_fd_rand, >=, 0);
6919 process_options(argc, argv);
6924 bcopy(&ztest_opts, ztest_shared_opts,
6925 sizeof (*ztest_shared_opts));
6927 ztest_fd_data = atoi(fd_data_str);
6929 bcopy(ztest_shared_opts, &ztest_opts, sizeof (ztest_opts));
6931 ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
6933 /* Override location of zpool.cache */
6934 VERIFY(asprintf((char **)&spa_config_path, "%s/zpool.cache",
6935 ztest_opts.zo_dir) != -1);
6937 ztest_ds = umem_alloc(ztest_opts.zo_datasets * sizeof (ztest_ds_t),
6942 metaslab_gang_bang = ztest_opts.zo_metaslab_gang_bang;
6943 metaslab_df_alloc_threshold =
6944 zs->zs_metaslab_df_alloc_threshold;
6953 hasalt = (strlen(ztest_opts.zo_alt_ztest) != 0);
6955 if (ztest_opts.zo_verbose >= 1) {
6956 (void) printf("%llu vdevs, %d datasets, %d threads,"
6957 " %llu seconds...\n",
6958 (u_longlong_t)ztest_opts.zo_vdevs,
6959 ztest_opts.zo_datasets,
6960 ztest_opts.zo_threads,
6961 (u_longlong_t)ztest_opts.zo_time);
6964 cmd = umem_alloc(MAXNAMELEN, UMEM_NOFAIL);
6965 (void) strlcpy(cmd, getexecname(), MAXNAMELEN);
6967 zs->zs_do_init = B_TRUE;
6968 if (strlen(ztest_opts.zo_alt_ztest) != 0) {
6969 if (ztest_opts.zo_verbose >= 1) {
6970 (void) printf("Executing older ztest for "
6971 "initialization: %s\n", ztest_opts.zo_alt_ztest);
6973 VERIFY(!exec_child(ztest_opts.zo_alt_ztest,
6974 ztest_opts.zo_alt_libpath, B_FALSE, NULL));
6976 VERIFY(!exec_child(NULL, NULL, B_FALSE, NULL));
6978 zs->zs_do_init = B_FALSE;
6980 zs->zs_proc_start = gethrtime();
6981 zs->zs_proc_stop = zs->zs_proc_start + ztest_opts.zo_time * NANOSEC;
6983 for (f = 0; f < ZTEST_FUNCS; f++) {
6984 zi = &ztest_info[f];
6985 zc = ZTEST_GET_SHARED_CALLSTATE(f);
6986 if (zs->zs_proc_start + zi->zi_interval[0] > zs->zs_proc_stop)
6987 zc->zc_next = UINT64_MAX;
6989 zc->zc_next = zs->zs_proc_start +
6990 ztest_random(2 * zi->zi_interval[0] + 1);
6994 * Run the tests in a loop. These tests include fault injection
6995 * to verify that self-healing data works, and forced crashes
6996 * to verify that we never lose on-disk consistency.
6998 while (gethrtime() < zs->zs_proc_stop) {
7003 * Initialize the workload counters for each function.
7005 for (f = 0; f < ZTEST_FUNCS; f++) {
7006 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7011 /* Set the allocation switch size */
7012 zs->zs_metaslab_df_alloc_threshold =
7013 ztest_random(zs->zs_metaslab_sz / 4) + 1;
7015 if (!hasalt || ztest_random(2) == 0) {
7016 if (hasalt && ztest_opts.zo_verbose >= 1) {
7017 (void) printf("Executing newer ztest: %s\n",
7021 killed = exec_child(cmd, NULL, B_TRUE, &status);
7023 if (hasalt && ztest_opts.zo_verbose >= 1) {
7024 (void) printf("Executing older ztest: %s\n",
7025 ztest_opts.zo_alt_ztest);
7028 killed = exec_child(ztest_opts.zo_alt_ztest,
7029 ztest_opts.zo_alt_libpath, B_TRUE, &status);
7036 if (ztest_opts.zo_verbose >= 1) {
7037 hrtime_t now = gethrtime();
7039 now = MIN(now, zs->zs_proc_stop);
7040 print_time(zs->zs_proc_stop - now, timebuf);
7041 nicenum(zs->zs_space, numbuf);
7043 (void) printf("Pass %3d, %8s, %3llu ENOSPC, "
7044 "%4.1f%% of %5s used, %3.0f%% done, %8s to go\n",
7046 WIFEXITED(status) ? "Complete" : "SIGKILL",
7047 (u_longlong_t)zs->zs_enospc_count,
7048 100.0 * zs->zs_alloc / zs->zs_space,
7050 100.0 * (now - zs->zs_proc_start) /
7051 (ztest_opts.zo_time * NANOSEC), timebuf);
7054 if (ztest_opts.zo_verbose >= 2) {
7055 (void) printf("\nWorkload summary:\n\n");
7056 (void) printf("%7s %9s %s\n",
7057 "Calls", "Time", "Function");
7058 (void) printf("%7s %9s %s\n",
7059 "-----", "----", "--------");
7060 for (f = 0; f < ZTEST_FUNCS; f++) {
7061 zi = &ztest_info[f];
7062 zc = ZTEST_GET_SHARED_CALLSTATE(f);
7063 print_time(zc->zc_time, timebuf);
7064 (void) printf("%7llu %9s %s\n",
7065 (u_longlong_t)zc->zc_count, timebuf,
7068 (void) printf("\n");
7072 * It's possible that we killed a child during a rename test,
7073 * in which case we'll have a 'ztest_tmp' pool lying around
7074 * instead of 'ztest'. Do a blind rename in case this happened.
7077 if (spa_open(ztest_opts.zo_pool, &spa, FTAG) == 0) {
7078 spa_close(spa, FTAG);
7080 char tmpname[ZFS_MAX_DATASET_NAME_LEN];
7082 kernel_init(FREAD | FWRITE);
7083 (void) snprintf(tmpname, sizeof (tmpname), "%s_tmp",
7084 ztest_opts.zo_pool);
7085 (void) spa_rename(tmpname, ztest_opts.zo_pool);
7089 if (!ztest_opts.zo_mmp_test)
7090 ztest_run_zdb(ztest_opts.zo_pool);
7093 if (ztest_opts.zo_verbose >= 1) {
7095 (void) printf("%d runs of older ztest: %s\n", older,
7096 ztest_opts.zo_alt_ztest);
7097 (void) printf("%d runs of newer ztest: %s\n", newer,
7100 (void) printf("%d killed, %d completed, %.0f%% kill rate\n",
7101 kills, iters - kills, (100.0 * kills) / MAX(1, iters));
7104 umem_free(cmd, MAXNAMELEN);